PR c++/79500 - ICE with non-template deduction guide
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43
44 /* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46 typedef int (*tree_fn_t) (tree, void*);
47
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
54 };
55
56 static GTY(()) struct pending_template *pending_templates;
57 static GTY(()) struct pending_template *last_pending_template;
58
59 int processing_template_parmlist;
60 static int template_header_count;
61
62 static GTY(()) tree saved_trees;
63 static vec<int> inline_parm_levels;
64
65 static GTY(()) struct tinst_level *current_tinst_level;
66
67 static GTY(()) tree saved_access_scope;
68
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr;
73
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
76 //
77 // Implementation of the RAII helper for creating new local
78 // specializations.
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
81 {
82 local_specializations = new hash_map<tree, tree>;
83 }
84
85 local_specialization_stack::~local_specialization_stack ()
86 {
87 delete local_specializations;
88 local_specializations = saved;
89 }
90
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth;
93
94 struct GTY((for_user)) spec_entry
95 {
96 tree tmpl;
97 tree args;
98 tree spec;
99 };
100
101 struct spec_hasher : ggc_ptr_hash<spec_entry>
102 {
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
105 };
106
107 static GTY (()) hash_table<spec_hasher> *decl_specializations;
108
109 static GTY (()) hash_table<spec_hasher> *type_specializations;
110
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
116
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
125
126 enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
130 };
131
132 static void push_access_scope (tree);
133 static void pop_access_scope (tree);
134 static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137 static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139 static int unify (tree, tree, tree, tree, int, bool);
140 static void add_pending_template (tree);
141 static tree reopen_tinst_level (struct tinst_level *);
142 static tree tsubst_initializer_list (tree, tree);
143 static tree get_partial_spec_bindings (tree, tree, tree);
144 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
147 bool, bool);
148 static void tsubst_enum (tree, tree, tree);
149 static tree add_to_template_args (tree, tree);
150 static tree add_outermost_template_args (tree, tree);
151 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
152 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
153 tree);
154 static int type_unification_real (tree, tree, tree, const tree *,
155 unsigned int, int, unification_kind_t, int,
156 vec<deferred_access_check, va_gc> **,
157 bool);
158 static void note_template_header (int);
159 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
160 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
161 static tree convert_template_argument (tree, tree, tree,
162 tsubst_flags_t, int, tree);
163 static tree for_each_template_parm (tree, tree_fn_t, void*,
164 hash_set<tree> *, bool, tree_fn_t = NULL);
165 static tree expand_template_argument_pack (tree);
166 static tree build_template_parm_index (int, int, int, tree, tree);
167 static bool inline_needs_template_parms (tree, bool);
168 static void push_inline_template_parms_recursive (tree, int);
169 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170 static int mark_template_parm (tree, void *);
171 static int template_parm_this_level_p (tree, void *);
172 static tree tsubst_friend_function (tree, tree);
173 static tree tsubst_friend_class (tree, tree);
174 static int can_complete_type_without_circularity (tree);
175 static tree get_bindings (tree, tree, tree, bool);
176 static int template_decl_level (tree);
177 static int check_cv_quals_for_unify (int, tree, tree);
178 static void template_parm_level_and_index (tree, int*, int*);
179 static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181 static tree copy_template_args (tree);
182 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
184 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189 static bool check_specialization_scope (void);
190 static tree process_partial_specialization (tree);
191 static void set_current_access_from_decl (tree);
192 static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194 static tree try_class_unification (tree, tree, tree, tree, bool);
195 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197 static bool template_template_parm_bindings_ok_p (tree, tree);
198 static void tsubst_default_arguments (tree, tsubst_flags_t);
199 static tree for_each_template_parm_r (tree *, int *, void *);
200 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
201 static void copy_default_args_to_explicit_spec (tree);
202 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
203 static bool dependent_template_arg_p (tree);
204 static bool any_template_arguments_need_structural_equality_p (tree);
205 static bool dependent_type_p_r (tree);
206 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_decl (tree, tree, tsubst_flags_t);
208 static void perform_typedefs_access_check (tree tmpl, tree targs);
209 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
218
219 /* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
223
224 static void
225 push_access_scope (tree t)
226 {
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
228 || TREE_CODE (t) == TYPE_DECL);
229
230 if (DECL_FRIEND_CONTEXT (t))
231 push_nested_class (DECL_FRIEND_CONTEXT (t));
232 else if (DECL_CLASS_SCOPE_P (t))
233 push_nested_class (DECL_CONTEXT (t));
234 else
235 push_to_top_level ();
236
237 if (TREE_CODE (t) == FUNCTION_DECL)
238 {
239 saved_access_scope = tree_cons
240 (NULL_TREE, current_function_decl, saved_access_scope);
241 current_function_decl = t;
242 }
243 }
244
245 /* Restore the scope set up by push_access_scope. T is the node we
246 are processing. */
247
248 static void
249 pop_access_scope (tree t)
250 {
251 if (TREE_CODE (t) == FUNCTION_DECL)
252 {
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
255 }
256
257 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
261 }
262
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
267
268 tree
269 finish_member_template_decl (tree decl)
270 {
271 if (decl == error_mark_node)
272 return error_mark_node;
273
274 gcc_assert (DECL_P (decl));
275
276 if (TREE_CODE (decl) == TYPE_DECL)
277 {
278 tree type;
279
280 type = TREE_TYPE (decl);
281 if (type == error_mark_node)
282 return error_mark_node;
283 if (MAYBE_CLASS_TYPE_P (type)
284 && CLASSTYPE_TEMPLATE_INFO (type)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
286 {
287 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
288 check_member_template (tmpl);
289 return tmpl;
290 }
291 return NULL_TREE;
292 }
293 else if (TREE_CODE (decl) == FIELD_DECL)
294 error ("data member %qD cannot be a member template", decl);
295 else if (DECL_TEMPLATE_INFO (decl))
296 {
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
298 {
299 check_member_template (DECL_TI_TEMPLATE (decl));
300 return DECL_TI_TEMPLATE (decl);
301 }
302 else
303 return decl;
304 }
305 else
306 error ("invalid member template declaration %qD", decl);
307
308 return error_mark_node;
309 }
310
311 /* Create a template info node. */
312
313 tree
314 build_template_info (tree template_decl, tree template_args)
315 {
316 tree result = make_node (TEMPLATE_INFO);
317 TI_TEMPLATE (result) = template_decl;
318 TI_ARGS (result) = template_args;
319 return result;
320 }
321
322 /* Return the template info node corresponding to T, whatever T is. */
323
324 tree
325 get_template_info (const_tree t)
326 {
327 tree tinfo = NULL_TREE;
328
329 if (!t || t == error_mark_node)
330 return NULL;
331
332 if (TREE_CODE (t) == NAMESPACE_DECL
333 || TREE_CODE (t) == PARM_DECL)
334 return NULL;
335
336 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
337 tinfo = DECL_TEMPLATE_INFO (t);
338
339 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
340 t = TREE_TYPE (t);
341
342 if (OVERLOAD_TYPE_P (t))
343 tinfo = TYPE_TEMPLATE_INFO (t);
344 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
345 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
346
347 return tinfo;
348 }
349
350 /* Returns the template nesting level of the indicated class TYPE.
351
352 For example, in:
353 template <class T>
354 struct A
355 {
356 template <class U>
357 struct B {};
358 };
359
360 A<T>::B<U> has depth two, while A<T> has depth one.
361 Both A<T>::B<int> and A<int>::B<U> have depth one, if
362 they are instantiations, not specializations.
363
364 This function is guaranteed to return 0 if passed NULL_TREE so
365 that, for example, `template_class_depth (current_class_type)' is
366 always safe. */
367
368 int
369 template_class_depth (tree type)
370 {
371 int depth;
372
373 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
374 {
375 tree tinfo = get_template_info (type);
376
377 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
378 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
379 ++depth;
380
381 if (DECL_P (type))
382 type = CP_DECL_CONTEXT (type);
383 else if (LAMBDA_TYPE_P (type))
384 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
385 else
386 type = CP_TYPE_CONTEXT (type);
387 }
388
389 return depth;
390 }
391
392 /* Subroutine of maybe_begin_member_template_processing.
393 Returns true if processing DECL needs us to push template parms. */
394
395 static bool
396 inline_needs_template_parms (tree decl, bool nsdmi)
397 {
398 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
399 return false;
400
401 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
402 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
403 }
404
405 /* Subroutine of maybe_begin_member_template_processing.
406 Push the template parms in PARMS, starting from LEVELS steps into the
407 chain, and ending at the beginning, since template parms are listed
408 innermost first. */
409
410 static void
411 push_inline_template_parms_recursive (tree parmlist, int levels)
412 {
413 tree parms = TREE_VALUE (parmlist);
414 int i;
415
416 if (levels > 1)
417 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
418
419 ++processing_template_decl;
420 current_template_parms
421 = tree_cons (size_int (processing_template_decl),
422 parms, current_template_parms);
423 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
424
425 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
426 NULL);
427 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
428 {
429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
430
431 if (error_operand_p (parm))
432 continue;
433
434 gcc_assert (DECL_P (parm));
435
436 switch (TREE_CODE (parm))
437 {
438 case TYPE_DECL:
439 case TEMPLATE_DECL:
440 pushdecl (parm);
441 break;
442
443 case PARM_DECL:
444 /* Push the CONST_DECL. */
445 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
446 break;
447
448 default:
449 gcc_unreachable ();
450 }
451 }
452 }
453
454 /* Restore the template parameter context for a member template, a
455 friend template defined in a class definition, or a non-template
456 member of template class. */
457
458 void
459 maybe_begin_member_template_processing (tree decl)
460 {
461 tree parms;
462 int levels = 0;
463 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
464
465 if (nsdmi)
466 {
467 tree ctx = DECL_CONTEXT (decl);
468 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
469 /* Disregard full specializations (c++/60999). */
470 && uses_template_parms (ctx)
471 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
472 }
473
474 if (inline_needs_template_parms (decl, nsdmi))
475 {
476 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
477 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
478
479 if (DECL_TEMPLATE_SPECIALIZATION (decl))
480 {
481 --levels;
482 parms = TREE_CHAIN (parms);
483 }
484
485 push_inline_template_parms_recursive (parms, levels);
486 }
487
488 /* Remember how many levels of template parameters we pushed so that
489 we can pop them later. */
490 inline_parm_levels.safe_push (levels);
491 }
492
493 /* Undo the effects of maybe_begin_member_template_processing. */
494
495 void
496 maybe_end_member_template_processing (void)
497 {
498 int i;
499 int last;
500
501 if (inline_parm_levels.length () == 0)
502 return;
503
504 last = inline_parm_levels.pop ();
505 for (i = 0; i < last; ++i)
506 {
507 --processing_template_decl;
508 current_template_parms = TREE_CHAIN (current_template_parms);
509 poplevel (0, 0, 0);
510 }
511 }
512
513 /* Return a new template argument vector which contains all of ARGS,
514 but has as its innermost set of arguments the EXTRA_ARGS. */
515
516 static tree
517 add_to_template_args (tree args, tree extra_args)
518 {
519 tree new_args;
520 int extra_depth;
521 int i;
522 int j;
523
524 if (args == NULL_TREE || extra_args == error_mark_node)
525 return extra_args;
526
527 extra_depth = TMPL_ARGS_DEPTH (extra_args);
528 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
529
530 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
531 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
532
533 for (j = 1; j <= extra_depth; ++j, ++i)
534 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
535
536 return new_args;
537 }
538
539 /* Like add_to_template_args, but only the outermost ARGS are added to
540 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
541 (EXTRA_ARGS) levels are added. This function is used to combine
542 the template arguments from a partial instantiation with the
543 template arguments used to attain the full instantiation from the
544 partial instantiation. */
545
546 static tree
547 add_outermost_template_args (tree args, tree extra_args)
548 {
549 tree new_args;
550
551 /* If there are more levels of EXTRA_ARGS than there are ARGS,
552 something very fishy is going on. */
553 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
554
555 /* If *all* the new arguments will be the EXTRA_ARGS, just return
556 them. */
557 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
558 return extra_args;
559
560 /* For the moment, we make ARGS look like it contains fewer levels. */
561 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
562
563 new_args = add_to_template_args (args, extra_args);
564
565 /* Now, we restore ARGS to its full dimensions. */
566 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
567
568 return new_args;
569 }
570
571 /* Return the N levels of innermost template arguments from the ARGS. */
572
573 tree
574 get_innermost_template_args (tree args, int n)
575 {
576 tree new_args;
577 int extra_levels;
578 int i;
579
580 gcc_assert (n >= 0);
581
582 /* If N is 1, just return the innermost set of template arguments. */
583 if (n == 1)
584 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
585
586 /* If we're not removing anything, just return the arguments we were
587 given. */
588 extra_levels = TMPL_ARGS_DEPTH (args) - n;
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
592
593 /* Make a new set of arguments, not containing the outer arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i + extra_levels));
598
599 return new_args;
600 }
601
602 /* The inverse of get_innermost_template_args: Return all but the innermost
603 EXTRA_LEVELS levels of template arguments from the ARGS. */
604
605 static tree
606 strip_innermost_template_args (tree args, int extra_levels)
607 {
608 tree new_args;
609 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
610 int i;
611
612 gcc_assert (n >= 0);
613
614 /* If N is 1, just return the outermost set of template arguments. */
615 if (n == 1)
616 return TMPL_ARGS_LEVEL (args, 1);
617
618 /* If we're not removing anything, just return the arguments we were
619 given. */
620 gcc_assert (extra_levels >= 0);
621 if (extra_levels == 0)
622 return args;
623
624 /* Make a new set of arguments, not containing the inner arguments. */
625 new_args = make_tree_vec (n);
626 for (i = 1; i <= n; ++i)
627 SET_TMPL_ARGS_LEVEL (new_args, i,
628 TMPL_ARGS_LEVEL (args, i));
629
630 return new_args;
631 }
632
633 /* We've got a template header coming up; push to a new level for storing
634 the parms. */
635
636 void
637 begin_template_parm_list (void)
638 {
639 /* We use a non-tag-transparent scope here, which causes pushtag to
640 put tags in this scope, rather than in the enclosing class or
641 namespace scope. This is the right thing, since we want
642 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
643 global template class, push_template_decl handles putting the
644 TEMPLATE_DECL into top-level scope. For a nested template class,
645 e.g.:
646
647 template <class T> struct S1 {
648 template <class T> struct S2 {};
649 };
650
651 pushtag contains special code to call pushdecl_with_scope on the
652 TEMPLATE_DECL for S2. */
653 begin_scope (sk_template_parms, NULL);
654 ++processing_template_decl;
655 ++processing_template_parmlist;
656 note_template_header (0);
657
658 /* Add a dummy parameter level while we process the parameter list. */
659 current_template_parms
660 = tree_cons (size_int (processing_template_decl),
661 make_tree_vec (0),
662 current_template_parms);
663 }
664
665 /* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
668
669 static bool
670 check_specialization_scope (void)
671 {
672 tree scope = current_scope ();
673
674 /* [temp.expl.spec]
675
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
682 is a member. */
683 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
684 {
685 error ("explicit specialization in non-namespace scope %qD", scope);
686 return false;
687 }
688
689 /* [temp.expl.spec]
690
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms)
698 {
699 error ("enclosing class templates are not explicitly specialized");
700 return false;
701 }
702
703 return true;
704 }
705
706 /* We've just seen template <>. */
707
708 bool
709 begin_specialization (void)
710 {
711 begin_scope (sk_template_spec, NULL);
712 note_template_header (1);
713 return check_specialization_scope ();
714 }
715
716 /* Called at then end of processing a declaration preceded by
717 template<>. */
718
719 void
720 end_specialization (void)
721 {
722 finish_scope ();
723 reset_specialization ();
724 }
725
726 /* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
728
729 void
730 reset_specialization (void)
731 {
732 processing_specialization = 0;
733 template_header_count = 0;
734 }
735
736 /* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
738
739 static void
740 note_template_header (int specialization)
741 {
742 processing_specialization = specialization;
743 template_header_count++;
744 }
745
746 /* We're beginning an explicit instantiation. */
747
748 void
749 begin_explicit_instantiation (void)
750 {
751 gcc_assert (!processing_explicit_instantiation);
752 processing_explicit_instantiation = true;
753 }
754
755
756 void
757 end_explicit_instantiation (void)
758 {
759 gcc_assert (processing_explicit_instantiation);
760 processing_explicit_instantiation = false;
761 }
762
763 /* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
767
768 static bool
769 check_specialization_namespace (tree tmpl)
770 {
771 tree tpl_ns = decl_namespace_context (tmpl);
772
773 /* [tmpl.expl.spec]
774
775 An explicit specialization shall be declared in a namespace enclosing the
776 specialized template. An explicit specialization whose declarator-id is
777 not qualified shall be declared in the nearest enclosing namespace of the
778 template, or, if the namespace is inline (7.3.1), any namespace from its
779 enclosing namespace set. */
780 if (current_scope() != DECL_CONTEXT (tmpl)
781 && !at_namespace_scope_p ())
782 {
783 error ("specialization of %qD must appear at namespace scope", tmpl);
784 return false;
785 }
786
787 if (cxx_dialect < cxx11
788 ? is_associated_namespace (current_namespace, tpl_ns)
789 : is_ancestor (current_namespace, tpl_ns))
790 /* Same or enclosing namespace. */
791 return true;
792 else
793 {
794 permerror (input_location,
795 "specialization of %qD in different namespace", tmpl);
796 inform (DECL_SOURCE_LOCATION (tmpl),
797 " from definition of %q#D", tmpl);
798 return false;
799 }
800 }
801
802 /* SPEC is an explicit instantiation. Check that it is valid to
803 perform this explicit instantiation in the current namespace. */
804
805 static void
806 check_explicit_instantiation_namespace (tree spec)
807 {
808 tree ns;
809
810 /* DR 275: An explicit instantiation shall appear in an enclosing
811 namespace of its template. */
812 ns = decl_namespace_context (spec);
813 if (!is_ancestor (current_namespace, ns))
814 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
815 "(which does not enclose namespace %qD)",
816 spec, current_namespace, ns);
817 }
818
819 // Returns the type of a template specialization only if that
820 // specialization needs to be defined. Otherwise (e.g., if the type has
821 // already been defined), the function returns NULL_TREE.
822 static tree
823 maybe_new_partial_specialization (tree type)
824 {
825 // An implicit instantiation of an incomplete type implies
826 // the definition of a new class template.
827 //
828 // template<typename T>
829 // struct S;
830 //
831 // template<typename T>
832 // struct S<T*>;
833 //
834 // Here, S<T*> is an implicit instantiation of S whose type
835 // is incomplete.
836 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
837 return type;
838
839 // It can also be the case that TYPE is a completed specialization.
840 // Continuing the previous example, suppose we also declare:
841 //
842 // template<typename T>
843 // requires Integral<T>
844 // struct S<T*>;
845 //
846 // Here, S<T*> refers to the specialization S<T*> defined
847 // above. However, we need to differentiate definitions because
848 // we intend to define a new partial specialization. In this case,
849 // we rely on the fact that the constraints are different for
850 // this declaration than that above.
851 //
852 // Note that we also get here for injected class names and
853 // late-parsed template definitions. We must ensure that we
854 // do not create new type declarations for those cases.
855 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
856 {
857 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
858 tree args = CLASSTYPE_TI_ARGS (type);
859
860 // If there are no template parameters, this cannot be a new
861 // partial template specializtion?
862 if (!current_template_parms)
863 return NULL_TREE;
864
865 // The injected-class-name is not a new partial specialization.
866 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
867 return NULL_TREE;
868
869 // If the constraints are not the same as those of the primary
870 // then, we can probably create a new specialization.
871 tree type_constr = current_template_constraints ();
872
873 if (type == TREE_TYPE (tmpl))
874 {
875 tree main_constr = get_constraints (tmpl);
876 if (equivalent_constraints (type_constr, main_constr))
877 return NULL_TREE;
878 }
879
880 // Also, if there's a pre-existing specialization with matching
881 // constraints, then this also isn't new.
882 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
883 while (specs)
884 {
885 tree spec_tmpl = TREE_VALUE (specs);
886 tree spec_args = TREE_PURPOSE (specs);
887 tree spec_constr = get_constraints (spec_tmpl);
888 if (comp_template_args (args, spec_args)
889 && equivalent_constraints (type_constr, spec_constr))
890 return NULL_TREE;
891 specs = TREE_CHAIN (specs);
892 }
893
894 // Create a new type node (and corresponding type decl)
895 // for the newly declared specialization.
896 tree t = make_class_type (TREE_CODE (type));
897 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
898 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
899
900 /* We only need a separate type node for storing the definition of this
901 partial specialization; uses of S<T*> are unconstrained, so all are
902 equivalent. So keep TYPE_CANONICAL the same. */
903 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
904
905 // Build the corresponding type decl.
906 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
907 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
908 DECL_SOURCE_LOCATION (d) = input_location;
909
910 return t;
911 }
912
913 return NULL_TREE;
914 }
915
916 /* The TYPE is being declared. If it is a template type, that means it
917 is a partial specialization. Do appropriate error-checking. */
918
919 tree
920 maybe_process_partial_specialization (tree type)
921 {
922 tree context;
923
924 if (type == error_mark_node)
925 return error_mark_node;
926
927 /* A lambda that appears in specialization context is not itself a
928 specialization. */
929 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
930 return type;
931
932 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
933 {
934 error ("name of class shadows template template parameter %qD",
935 TYPE_NAME (type));
936 return error_mark_node;
937 }
938
939 context = TYPE_CONTEXT (type);
940
941 if (TYPE_ALIAS_P (type))
942 {
943 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
944
945 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
946 error ("specialization of alias template %qD",
947 TI_TEMPLATE (tinfo));
948 else
949 error ("explicit specialization of non-template %qT", type);
950 return error_mark_node;
951 }
952 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
953 {
954 /* This is for ordinary explicit specialization and partial
955 specialization of a template class such as:
956
957 template <> class C<int>;
958
959 or:
960
961 template <class T> class C<T*>;
962
963 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
964
965 if (tree t = maybe_new_partial_specialization (type))
966 {
967 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
968 && !at_namespace_scope_p ())
969 return error_mark_node;
970 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
971 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
972 if (processing_template_decl)
973 {
974 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
975 if (decl == error_mark_node)
976 return error_mark_node;
977 return TREE_TYPE (decl);
978 }
979 }
980 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
981 error ("specialization of %qT after instantiation", type);
982 else if (errorcount && !processing_specialization
983 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
984 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
985 /* Trying to define a specialization either without a template<> header
986 or in an inappropriate place. We've already given an error, so just
987 bail now so we don't actually define the specialization. */
988 return error_mark_node;
989 }
990 else if (CLASS_TYPE_P (type)
991 && !CLASSTYPE_USE_TEMPLATE (type)
992 && CLASSTYPE_TEMPLATE_INFO (type)
993 && context && CLASS_TYPE_P (context)
994 && CLASSTYPE_TEMPLATE_INFO (context))
995 {
996 /* This is for an explicit specialization of member class
997 template according to [temp.expl.spec/18]:
998
999 template <> template <class U> class C<int>::D;
1000
1001 The context `C<int>' must be an implicit instantiation.
1002 Otherwise this is just a member class template declared
1003 earlier like:
1004
1005 template <> class C<int> { template <class U> class D; };
1006 template <> template <class U> class C<int>::D;
1007
1008 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1009 while in the second case, `C<int>::D' is a primary template
1010 and `C<T>::D' may not exist. */
1011
1012 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1013 && !COMPLETE_TYPE_P (type))
1014 {
1015 tree t;
1016 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1017
1018 if (current_namespace
1019 != decl_namespace_context (tmpl))
1020 {
1021 permerror (input_location,
1022 "specializing %q#T in different namespace", type);
1023 permerror (DECL_SOURCE_LOCATION (tmpl),
1024 " from definition of %q#D", tmpl);
1025 }
1026
1027 /* Check for invalid specialization after instantiation:
1028
1029 template <> template <> class C<int>::D<int>;
1030 template <> template <class U> class C<int>::D; */
1031
1032 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1033 t; t = TREE_CHAIN (t))
1034 {
1035 tree inst = TREE_VALUE (t);
1036 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1037 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1038 {
1039 /* We already have a full specialization of this partial
1040 instantiation, or a full specialization has been
1041 looked up but not instantiated. Reassign it to the
1042 new member specialization template. */
1043 spec_entry elt;
1044 spec_entry *entry;
1045
1046 elt.tmpl = most_general_template (tmpl);
1047 elt.args = CLASSTYPE_TI_ARGS (inst);
1048 elt.spec = inst;
1049
1050 type_specializations->remove_elt (&elt);
1051
1052 elt.tmpl = tmpl;
1053 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1054
1055 spec_entry **slot
1056 = type_specializations->find_slot (&elt, INSERT);
1057 entry = ggc_alloc<spec_entry> ();
1058 *entry = elt;
1059 *slot = entry;
1060 }
1061 else
1062 /* But if we've had an implicit instantiation, that's a
1063 problem ([temp.expl.spec]/6). */
1064 error ("specialization %qT after instantiation %qT",
1065 type, inst);
1066 }
1067
1068 /* Mark TYPE as a specialization. And as a result, we only
1069 have one level of template argument for the innermost
1070 class template. */
1071 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1072 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1073 CLASSTYPE_TI_ARGS (type)
1074 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1075 }
1076 }
1077 else if (processing_specialization)
1078 {
1079 /* Someday C++0x may allow for enum template specialization. */
1080 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1081 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1082 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1083 "of %qD not allowed by ISO C++", type);
1084 else
1085 {
1086 error ("explicit specialization of non-template %qT", type);
1087 return error_mark_node;
1088 }
1089 }
1090
1091 return type;
1092 }
1093
1094 /* Returns nonzero if we can optimize the retrieval of specializations
1095 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1096 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1097
1098 static inline bool
1099 optimize_specialization_lookup_p (tree tmpl)
1100 {
1101 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1102 && DECL_CLASS_SCOPE_P (tmpl)
1103 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1104 parameter. */
1105 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1106 /* The optimized lookup depends on the fact that the
1107 template arguments for the member function template apply
1108 purely to the containing class, which is not true if the
1109 containing class is an explicit or partial
1110 specialization. */
1111 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1112 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1113 && !DECL_CONV_FN_P (tmpl)
1114 /* It is possible to have a template that is not a member
1115 template and is not a member of a template class:
1116
1117 template <typename T>
1118 struct S { friend A::f(); };
1119
1120 Here, the friend function is a template, but the context does
1121 not have template information. The optimized lookup relies
1122 on having ARGS be the template arguments for both the class
1123 and the function template. */
1124 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1125 }
1126
1127 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1128 gone through coerce_template_parms by now. */
1129
1130 static void
1131 verify_unstripped_args (tree args)
1132 {
1133 ++processing_template_decl;
1134 if (!any_dependent_template_arguments_p (args))
1135 {
1136 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1137 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1138 {
1139 tree arg = TREE_VEC_ELT (inner, i);
1140 if (TREE_CODE (arg) == TEMPLATE_DECL)
1141 /* OK */;
1142 else if (TYPE_P (arg))
1143 gcc_assert (strip_typedefs (arg, NULL) == arg);
1144 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1145 /* Allow typedefs on the type of a non-type argument, since a
1146 parameter can have them. */;
1147 else
1148 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1149 }
1150 }
1151 --processing_template_decl;
1152 }
1153
1154 /* Retrieve the specialization (in the sense of [temp.spec] - a
1155 specialization is either an instantiation or an explicit
1156 specialization) of TMPL for the given template ARGS. If there is
1157 no such specialization, return NULL_TREE. The ARGS are a vector of
1158 arguments, or a vector of vectors of arguments, in the case of
1159 templates with more than one level of parameters.
1160
1161 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1162 then we search for a partial specialization matching ARGS. This
1163 parameter is ignored if TMPL is not a class template.
1164
1165 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1166 result is a NONTYPE_ARGUMENT_PACK. */
1167
1168 static tree
1169 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1170 {
1171 if (tmpl == NULL_TREE)
1172 return NULL_TREE;
1173
1174 if (args == error_mark_node)
1175 return NULL_TREE;
1176
1177 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1178 || TREE_CODE (tmpl) == FIELD_DECL);
1179
1180 /* There should be as many levels of arguments as there are
1181 levels of parameters. */
1182 gcc_assert (TMPL_ARGS_DEPTH (args)
1183 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1185 : template_class_depth (DECL_CONTEXT (tmpl))));
1186
1187 if (flag_checking)
1188 verify_unstripped_args (args);
1189
1190 if (optimize_specialization_lookup_p (tmpl))
1191 {
1192 tree class_template;
1193 tree class_specialization;
1194 vec<tree, va_gc> *methods;
1195 tree fns;
1196 int idx;
1197
1198 /* The template arguments actually apply to the containing
1199 class. Find the class specialization with those
1200 arguments. */
1201 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1202 class_specialization
1203 = retrieve_specialization (class_template, args, 0);
1204 if (!class_specialization)
1205 return NULL_TREE;
1206 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1207 for the specialization. */
1208 idx = class_method_index_for_fn (class_specialization, tmpl);
1209 if (idx == -1)
1210 return NULL_TREE;
1211 /* Iterate through the methods with the indicated name, looking
1212 for the one that has an instance of TMPL. */
1213 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1214 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1215 {
1216 tree fn = OVL_CURRENT (fns);
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1222 }
1223 return NULL_TREE;
1224 }
1225 else
1226 {
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1230
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1234
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1239
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1245 }
1246
1247 return NULL_TREE;
1248 }
1249
1250 /* Like retrieve_specialization, but for local declarations. */
1251
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1254 {
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1257
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1260 }
1261
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263
1264 int
1265 is_specialization_of (tree decl, tree tmpl)
1266 {
1267 tree t;
1268
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1270 {
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1276 }
1277 else
1278 {
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1280
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1287 }
1288
1289 return 0;
1290 }
1291
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1294
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1297 {
1298 bool need_template = true;
1299 int template_depth;
1300
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1303
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1310 {
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1314 }
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1318
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1322
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1325
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1331
1332 For example, given a template friend declaration
1333
1334 template <class T> friend void A<T>::f();
1335
1336 the member function below is considered a friend
1337
1338 template <> struct A<int> {
1339 void f();
1340 };
1341
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1345
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1351 {
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1354
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1357
1358 and DECL's are
1359
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1362
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1367
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1371
1372 while (current_depth < template_depth)
1373 {
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1375 {
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1381 }
1382 context = TYPE_CONTEXT (context);
1383 }
1384
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1386 {
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1392
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1400 {
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1409
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1411 }
1412 else
1413 decl_type = TREE_TYPE (decl);
1414
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1419
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1423
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1432
1433 return compparms (decl_args_type, friend_args_type);
1434 }
1435 else
1436 {
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1440
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1446
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1450 {
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1457
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1465 }
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1469 }
1470 }
1471 return false;
1472 }
1473
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1478
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1481
1482 static tree
1483 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1484 hashval_t hash)
1485 {
1486 tree fn;
1487 spec_entry **slot = NULL;
1488 spec_entry elt;
1489
1490 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1491 || (TREE_CODE (tmpl) == FIELD_DECL
1492 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1493
1494 if (TREE_CODE (spec) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1502
1503 template <class T> void f(T t1, T t = T())
1504
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1507 return spec;
1508
1509 if (optimize_specialization_lookup_p (tmpl))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn = retrieve_specialization (tmpl, args, 0);
1513 else
1514 {
1515 elt.tmpl = tmpl;
1516 elt.args = args;
1517 elt.spec = spec;
1518
1519 if (hash == 0)
1520 hash = spec_hasher::hash (&elt);
1521
1522 slot =
1523 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1524 if (*slot)
1525 fn = ((spec_entry *) *slot)->spec;
1526 else
1527 fn = NULL_TREE;
1528 }
1529
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1535 if (fn == spec)
1536 return spec;
1537 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1538 {
1539 if (DECL_TEMPLATE_INSTANTIATION (fn))
1540 {
1541 if (DECL_ODR_USED (fn)
1542 || DECL_EXPLICIT_INSTANTIATION (fn))
1543 {
1544 error ("specialization of %qD after instantiation",
1545 fn);
1546 return error_mark_node;
1547 }
1548 else
1549 {
1550 tree clone;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1557 it later.
1558
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1561 specialization:
1562
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1566
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1569 declaration.
1570
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn) = NULL_TREE;
1575 duplicate_decls (spec, fn, is_friend);
1576 /* The call to duplicate_decls will have applied
1577 [temp.expl.spec]:
1578
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1582 is.
1583
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone, fn)
1587 {
1588 DECL_DECLARED_INLINE_P (clone)
1589 = DECL_DECLARED_INLINE_P (fn);
1590 DECL_SOURCE_LOCATION (clone)
1591 = DECL_SOURCE_LOCATION (fn);
1592 DECL_DELETED_FN (clone)
1593 = DECL_DELETED_FN (fn);
1594 }
1595 check_specialization_namespace (tmpl);
1596
1597 return fn;
1598 }
1599 }
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1601 {
1602 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1603 /* Dup decl failed, but this is a new definition. Set the
1604 line number so any errors match this new
1605 definition. */
1606 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1607
1608 return fn;
1609 }
1610 }
1611 else if (fn)
1612 return duplicate_decls (spec, fn, is_friend);
1613
1614 /* A specialization must be declared in the same namespace as the
1615 template it is specializing. */
1616 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1617 && !check_specialization_namespace (tmpl))
1618 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1619
1620 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1621 {
1622 spec_entry *entry = ggc_alloc<spec_entry> ();
1623 gcc_assert (tmpl && args && spec);
1624 *entry = elt;
1625 *slot = entry;
1626 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1627 && PRIMARY_TEMPLATE_P (tmpl)
1628 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1629 || variable_template_p (tmpl))
1630 /* If TMPL is a forward declaration of a template function, keep a list
1631 of all specializations in case we need to reassign them to a friend
1632 template later in tsubst_friend_function.
1633
1634 Also keep a list of all variable template instantiations so that
1635 process_partial_specialization can check whether a later partial
1636 specialization would have used it. */
1637 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1638 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1639 }
1640
1641 return spec;
1642 }
1643
1644 /* Returns true iff two spec_entry nodes are equivalent. */
1645
1646 int comparing_specializations;
1647
1648 bool
1649 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1650 {
1651 int equal;
1652
1653 ++comparing_specializations;
1654 equal = (e1->tmpl == e2->tmpl
1655 && comp_template_args (e1->args, e2->args));
1656 if (equal && flag_concepts
1657 /* tmpl could be a FIELD_DECL for a capture pack. */
1658 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1659 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1660 && uses_template_parms (e1->args))
1661 {
1662 /* Partial specializations of a variable template can be distinguished by
1663 constraints. */
1664 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1665 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1666 equal = equivalent_constraints (c1, c2);
1667 }
1668 --comparing_specializations;
1669
1670 return equal;
1671 }
1672
1673 /* Returns a hash for a template TMPL and template arguments ARGS. */
1674
1675 static hashval_t
1676 hash_tmpl_and_args (tree tmpl, tree args)
1677 {
1678 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1679 return iterative_hash_template_arg (args, val);
1680 }
1681
1682 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1683 ignoring SPEC. */
1684
1685 hashval_t
1686 spec_hasher::hash (spec_entry *e)
1687 {
1688 return hash_tmpl_and_args (e->tmpl, e->args);
1689 }
1690
1691 /* Recursively calculate a hash value for a template argument ARG, for use
1692 in the hash tables of template specializations. */
1693
1694 hashval_t
1695 iterative_hash_template_arg (tree arg, hashval_t val)
1696 {
1697 unsigned HOST_WIDE_INT i;
1698 enum tree_code code;
1699 char tclass;
1700
1701 if (arg == NULL_TREE)
1702 return iterative_hash_object (arg, val);
1703
1704 if (!TYPE_P (arg))
1705 STRIP_NOPS (arg);
1706
1707 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1708 gcc_unreachable ();
1709
1710 code = TREE_CODE (arg);
1711 tclass = TREE_CODE_CLASS (code);
1712
1713 val = iterative_hash_object (code, val);
1714
1715 switch (code)
1716 {
1717 case ERROR_MARK:
1718 return val;
1719
1720 case IDENTIFIER_NODE:
1721 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1722
1723 case TREE_VEC:
1724 {
1725 int i, len = TREE_VEC_LENGTH (arg);
1726 for (i = 0; i < len; ++i)
1727 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1728 return val;
1729 }
1730
1731 case TYPE_PACK_EXPANSION:
1732 case EXPR_PACK_EXPANSION:
1733 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1734 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1735
1736 case TYPE_ARGUMENT_PACK:
1737 case NONTYPE_ARGUMENT_PACK:
1738 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1739
1740 case TREE_LIST:
1741 for (; arg; arg = TREE_CHAIN (arg))
1742 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1743 return val;
1744
1745 case OVERLOAD:
1746 for (; arg; arg = OVL_NEXT (arg))
1747 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1748 return val;
1749
1750 case CONSTRUCTOR:
1751 {
1752 tree field, value;
1753 iterative_hash_template_arg (TREE_TYPE (arg), val);
1754 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1755 {
1756 val = iterative_hash_template_arg (field, val);
1757 val = iterative_hash_template_arg (value, val);
1758 }
1759 return val;
1760 }
1761
1762 case PARM_DECL:
1763 if (!DECL_ARTIFICIAL (arg))
1764 {
1765 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1766 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1767 }
1768 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1769
1770 case TARGET_EXPR:
1771 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1772
1773 case PTRMEM_CST:
1774 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1775 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1776
1777 case TEMPLATE_PARM_INDEX:
1778 val = iterative_hash_template_arg
1779 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1780 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1781 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1782
1783 case TRAIT_EXPR:
1784 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1785 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1786 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1787
1788 case BASELINK:
1789 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1790 val);
1791 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1792 val);
1793
1794 case MODOP_EXPR:
1795 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1796 code = TREE_CODE (TREE_OPERAND (arg, 1));
1797 val = iterative_hash_object (code, val);
1798 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1799
1800 case LAMBDA_EXPR:
1801 /* A lambda can't appear in a template arg, but don't crash on
1802 erroneous input. */
1803 gcc_assert (seen_error ());
1804 return val;
1805
1806 case CAST_EXPR:
1807 case IMPLICIT_CONV_EXPR:
1808 case STATIC_CAST_EXPR:
1809 case REINTERPRET_CAST_EXPR:
1810 case CONST_CAST_EXPR:
1811 case DYNAMIC_CAST_EXPR:
1812 case NEW_EXPR:
1813 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1814 /* Now hash operands as usual. */
1815 break;
1816
1817 default:
1818 break;
1819 }
1820
1821 switch (tclass)
1822 {
1823 case tcc_type:
1824 if (alias_template_specialization_p (arg))
1825 {
1826 // We want an alias specialization that survived strip_typedefs
1827 // to hash differently from its TYPE_CANONICAL, to avoid hash
1828 // collisions that compare as different in template_args_equal.
1829 // These could be dependent specializations that strip_typedefs
1830 // left alone, or untouched specializations because
1831 // coerce_template_parms returns the unconverted template
1832 // arguments if it sees incomplete argument packs.
1833 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1834 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1835 }
1836 if (TYPE_CANONICAL (arg))
1837 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1838 val);
1839 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1840 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1841 /* Otherwise just compare the types during lookup. */
1842 return val;
1843
1844 case tcc_declaration:
1845 case tcc_constant:
1846 return iterative_hash_expr (arg, val);
1847
1848 default:
1849 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1850 {
1851 unsigned n = cp_tree_operand_length (arg);
1852 for (i = 0; i < n; ++i)
1853 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1854 return val;
1855 }
1856 }
1857 gcc_unreachable ();
1858 return 0;
1859 }
1860
1861 /* Unregister the specialization SPEC as a specialization of TMPL.
1862 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1863 if the SPEC was listed as a specialization of TMPL.
1864
1865 Note that SPEC has been ggc_freed, so we can't look inside it. */
1866
1867 bool
1868 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1869 {
1870 spec_entry *entry;
1871 spec_entry elt;
1872
1873 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1874 elt.args = TI_ARGS (tinfo);
1875 elt.spec = NULL_TREE;
1876
1877 entry = decl_specializations->find (&elt);
1878 if (entry != NULL)
1879 {
1880 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1881 gcc_assert (new_spec != NULL_TREE);
1882 entry->spec = new_spec;
1883 return 1;
1884 }
1885
1886 return 0;
1887 }
1888
1889 /* Like register_specialization, but for local declarations. We are
1890 registering SPEC, an instantiation of TMPL. */
1891
1892 void
1893 register_local_specialization (tree spec, tree tmpl)
1894 {
1895 local_specializations->put (tmpl, spec);
1896 }
1897
1898 /* TYPE is a class type. Returns true if TYPE is an explicitly
1899 specialized class. */
1900
1901 bool
1902 explicit_class_specialization_p (tree type)
1903 {
1904 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1905 return false;
1906 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1907 }
1908
1909 /* Print the list of functions at FNS, going through all the overloads
1910 for each element of the list. Alternatively, FNS can not be a
1911 TREE_LIST, in which case it will be printed together with all the
1912 overloads.
1913
1914 MORE and *STR should respectively be FALSE and NULL when the function
1915 is called from the outside. They are used internally on recursive
1916 calls. print_candidates manages the two parameters and leaves NULL
1917 in *STR when it ends. */
1918
1919 static void
1920 print_candidates_1 (tree fns, bool more, const char **str)
1921 {
1922 tree fn, fn2;
1923 char *spaces = NULL;
1924
1925 for (fn = fns; fn; fn = OVL_NEXT (fn))
1926 if (TREE_CODE (fn) == TREE_LIST)
1927 {
1928 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1929 print_candidates_1 (TREE_VALUE (fn2),
1930 TREE_CHAIN (fn2) || more, str);
1931 }
1932 else
1933 {
1934 tree cand = OVL_CURRENT (fn);
1935 if (!*str)
1936 {
1937 /* Pick the prefix string. */
1938 if (!more && !OVL_NEXT (fns))
1939 {
1940 inform (DECL_SOURCE_LOCATION (cand),
1941 "candidate is: %#D", cand);
1942 continue;
1943 }
1944
1945 *str = _("candidates are:");
1946 spaces = get_spaces (*str);
1947 }
1948 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1949 *str = spaces ? spaces : *str;
1950 }
1951
1952 if (!more)
1953 {
1954 free (spaces);
1955 *str = NULL;
1956 }
1957 }
1958
1959 /* Print the list of candidate FNS in an error message. FNS can also
1960 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1961
1962 void
1963 print_candidates (tree fns)
1964 {
1965 const char *str = NULL;
1966 print_candidates_1 (fns, false, &str);
1967 gcc_assert (str == NULL);
1968 }
1969
1970 /* Get a (possibly) constrained template declaration for the
1971 purpose of ordering candidates. */
1972 static tree
1973 get_template_for_ordering (tree list)
1974 {
1975 gcc_assert (TREE_CODE (list) == TREE_LIST);
1976 tree f = TREE_VALUE (list);
1977 if (tree ti = DECL_TEMPLATE_INFO (f))
1978 return TI_TEMPLATE (ti);
1979 return f;
1980 }
1981
1982 /* Among candidates having the same signature, return the
1983 most constrained or NULL_TREE if there is no best candidate.
1984 If the signatures of candidates vary (e.g., template
1985 specialization vs. member function), then there can be no
1986 most constrained.
1987
1988 Note that we don't compare constraints on the functions
1989 themselves, but rather those of their templates. */
1990 static tree
1991 most_constrained_function (tree candidates)
1992 {
1993 // Try to find the best candidate in a first pass.
1994 tree champ = candidates;
1995 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1996 {
1997 int winner = more_constrained (get_template_for_ordering (champ),
1998 get_template_for_ordering (c));
1999 if (winner == -1)
2000 champ = c; // The candidate is more constrained
2001 else if (winner == 0)
2002 return NULL_TREE; // Neither is more constrained
2003 }
2004
2005 // Verify that the champ is better than previous candidates.
2006 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2007 if (!more_constrained (get_template_for_ordering (champ),
2008 get_template_for_ordering (c)))
2009 return NULL_TREE;
2010 }
2011
2012 return champ;
2013 }
2014
2015
2016 /* Returns the template (one of the functions given by TEMPLATE_ID)
2017 which can be specialized to match the indicated DECL with the
2018 explicit template args given in TEMPLATE_ID. The DECL may be
2019 NULL_TREE if none is available. In that case, the functions in
2020 TEMPLATE_ID are non-members.
2021
2022 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2023 specialization of a member template.
2024
2025 The TEMPLATE_COUNT is the number of references to qualifying
2026 template classes that appeared in the name of the function. See
2027 check_explicit_specialization for a more accurate description.
2028
2029 TSK indicates what kind of template declaration (if any) is being
2030 declared. TSK_TEMPLATE indicates that the declaration given by
2031 DECL, though a FUNCTION_DECL, has template parameters, and is
2032 therefore a template function.
2033
2034 The template args (those explicitly specified and those deduced)
2035 are output in a newly created vector *TARGS_OUT.
2036
2037 If it is impossible to determine the result, an error message is
2038 issued. The error_mark_node is returned to indicate failure. */
2039
2040 static tree
2041 determine_specialization (tree template_id,
2042 tree decl,
2043 tree* targs_out,
2044 int need_member_template,
2045 int template_count,
2046 tmpl_spec_kind tsk)
2047 {
2048 tree fns;
2049 tree targs;
2050 tree explicit_targs;
2051 tree candidates = NULL_TREE;
2052
2053 /* A TREE_LIST of templates of which DECL may be a specialization.
2054 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2055 corresponding TREE_PURPOSE is the set of template arguments that,
2056 when used to instantiate the template, would produce a function
2057 with the signature of DECL. */
2058 tree templates = NULL_TREE;
2059 int header_count;
2060 cp_binding_level *b;
2061
2062 *targs_out = NULL_TREE;
2063
2064 if (template_id == error_mark_node || decl == error_mark_node)
2065 return error_mark_node;
2066
2067 /* We shouldn't be specializing a member template of an
2068 unspecialized class template; we already gave an error in
2069 check_specialization_scope, now avoid crashing. */
2070 if (template_count && DECL_CLASS_SCOPE_P (decl)
2071 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2072 {
2073 gcc_assert (errorcount);
2074 return error_mark_node;
2075 }
2076
2077 fns = TREE_OPERAND (template_id, 0);
2078 explicit_targs = TREE_OPERAND (template_id, 1);
2079
2080 if (fns == error_mark_node)
2081 return error_mark_node;
2082
2083 /* Check for baselinks. */
2084 if (BASELINK_P (fns))
2085 fns = BASELINK_FUNCTIONS (fns);
2086
2087 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2088 {
2089 error ("%qD is not a function template", fns);
2090 return error_mark_node;
2091 }
2092 else if (VAR_P (decl) && !variable_template_p (fns))
2093 {
2094 error ("%qD is not a variable template", fns);
2095 return error_mark_node;
2096 }
2097
2098 /* Count the number of template headers specified for this
2099 specialization. */
2100 header_count = 0;
2101 for (b = current_binding_level;
2102 b->kind == sk_template_parms;
2103 b = b->level_chain)
2104 ++header_count;
2105
2106 tree orig_fns = fns;
2107
2108 if (variable_template_p (fns))
2109 {
2110 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2111 targs = coerce_template_parms (parms, explicit_targs, fns,
2112 tf_warning_or_error,
2113 /*req_all*/true, /*use_defarg*/true);
2114 if (targs != error_mark_node)
2115 templates = tree_cons (targs, fns, templates);
2116 }
2117 else for (; fns; fns = OVL_NEXT (fns))
2118 {
2119 tree fn = OVL_CURRENT (fns);
2120
2121 if (TREE_CODE (fn) == TEMPLATE_DECL)
2122 {
2123 tree decl_arg_types;
2124 tree fn_arg_types;
2125 tree insttype;
2126
2127 /* In case of explicit specialization, we need to check if
2128 the number of template headers appearing in the specialization
2129 is correct. This is usually done in check_explicit_specialization,
2130 but the check done there cannot be exhaustive when specializing
2131 member functions. Consider the following code:
2132
2133 template <> void A<int>::f(int);
2134 template <> template <> void A<int>::f(int);
2135
2136 Assuming that A<int> is not itself an explicit specialization
2137 already, the first line specializes "f" which is a non-template
2138 member function, whilst the second line specializes "f" which
2139 is a template member function. So both lines are syntactically
2140 correct, and check_explicit_specialization does not reject
2141 them.
2142
2143 Here, we can do better, as we are matching the specialization
2144 against the declarations. We count the number of template
2145 headers, and we check if they match TEMPLATE_COUNT + 1
2146 (TEMPLATE_COUNT is the number of qualifying template classes,
2147 plus there must be another header for the member template
2148 itself).
2149
2150 Notice that if header_count is zero, this is not a
2151 specialization but rather a template instantiation, so there
2152 is no check we can perform here. */
2153 if (header_count && header_count != template_count + 1)
2154 continue;
2155
2156 /* Check that the number of template arguments at the
2157 innermost level for DECL is the same as for FN. */
2158 if (current_binding_level->kind == sk_template_parms
2159 && !current_binding_level->explicit_spec_p
2160 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2161 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2162 (current_template_parms))))
2163 continue;
2164
2165 /* DECL might be a specialization of FN. */
2166 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2167 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2168
2169 /* For a non-static member function, we need to make sure
2170 that the const qualification is the same. Since
2171 get_bindings does not try to merge the "this" parameter,
2172 we must do the comparison explicitly. */
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2174 && !same_type_p (TREE_VALUE (fn_arg_types),
2175 TREE_VALUE (decl_arg_types)))
2176 continue;
2177
2178 /* Skip the "this" parameter and, for constructors of
2179 classes with virtual bases, the VTT parameter. A
2180 full specialization of a constructor will have a VTT
2181 parameter, but a template never will. */
2182 decl_arg_types
2183 = skip_artificial_parms_for (decl, decl_arg_types);
2184 fn_arg_types
2185 = skip_artificial_parms_for (fn, fn_arg_types);
2186
2187 /* Function templates cannot be specializations; there are
2188 no partial specializations of functions. Therefore, if
2189 the type of DECL does not match FN, there is no
2190 match.
2191
2192 Note that it should never be the case that we have both
2193 candidates added here, and for regular member functions
2194 below. */
2195 if (tsk == tsk_template)
2196 {
2197 if (compparms (fn_arg_types, decl_arg_types))
2198 candidates = tree_cons (NULL_TREE, fn, candidates);
2199 continue;
2200 }
2201
2202 /* See whether this function might be a specialization of this
2203 template. Suppress access control because we might be trying
2204 to make this specialization a friend, and we have already done
2205 access control for the declaration of the specialization. */
2206 push_deferring_access_checks (dk_no_check);
2207 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2208 pop_deferring_access_checks ();
2209
2210 if (!targs)
2211 /* We cannot deduce template arguments that when used to
2212 specialize TMPL will produce DECL. */
2213 continue;
2214
2215 /* Remove, from the set of candidates, all those functions
2216 whose constraints are not satisfied. */
2217 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2218 continue;
2219
2220 // Then, try to form the new function type.
2221 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2222 if (insttype == error_mark_node)
2223 continue;
2224 fn_arg_types
2225 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2226 if (!compparms (fn_arg_types, decl_arg_types))
2227 continue;
2228
2229 /* Save this template, and the arguments deduced. */
2230 templates = tree_cons (targs, fn, templates);
2231 }
2232 else if (need_member_template)
2233 /* FN is an ordinary member function, and we need a
2234 specialization of a member template. */
2235 ;
2236 else if (TREE_CODE (fn) != FUNCTION_DECL)
2237 /* We can get IDENTIFIER_NODEs here in certain erroneous
2238 cases. */
2239 ;
2240 else if (!DECL_FUNCTION_MEMBER_P (fn))
2241 /* This is just an ordinary non-member function. Nothing can
2242 be a specialization of that. */
2243 ;
2244 else if (DECL_ARTIFICIAL (fn))
2245 /* Cannot specialize functions that are created implicitly. */
2246 ;
2247 else
2248 {
2249 tree decl_arg_types;
2250
2251 /* This is an ordinary member function. However, since
2252 we're here, we can assume its enclosing class is a
2253 template class. For example,
2254
2255 template <typename T> struct S { void f(); };
2256 template <> void S<int>::f() {}
2257
2258 Here, S<int>::f is a non-template, but S<int> is a
2259 template class. If FN has the same type as DECL, we
2260 might be in business. */
2261
2262 if (!DECL_TEMPLATE_INFO (fn))
2263 /* Its enclosing class is an explicit specialization
2264 of a template class. This is not a candidate. */
2265 continue;
2266
2267 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2268 TREE_TYPE (TREE_TYPE (fn))))
2269 /* The return types differ. */
2270 continue;
2271
2272 /* Adjust the type of DECL in case FN is a static member. */
2273 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2274 if (DECL_STATIC_FUNCTION_P (fn)
2275 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2276 decl_arg_types = TREE_CHAIN (decl_arg_types);
2277
2278 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2279 decl_arg_types))
2280 continue;
2281
2282 // If the deduced arguments do not satisfy the constraints,
2283 // this is not a candidate.
2284 if (flag_concepts && !constraints_satisfied_p (fn))
2285 continue;
2286
2287 // Add the candidate.
2288 candidates = tree_cons (NULL_TREE, fn, candidates);
2289 }
2290 }
2291
2292 if (templates && TREE_CHAIN (templates))
2293 {
2294 /* We have:
2295
2296 [temp.expl.spec]
2297
2298 It is possible for a specialization with a given function
2299 signature to be instantiated from more than one function
2300 template. In such cases, explicit specification of the
2301 template arguments must be used to uniquely identify the
2302 function template specialization being specialized.
2303
2304 Note that here, there's no suggestion that we're supposed to
2305 determine which of the candidate templates is most
2306 specialized. However, we, also have:
2307
2308 [temp.func.order]
2309
2310 Partial ordering of overloaded function template
2311 declarations is used in the following contexts to select
2312 the function template to which a function template
2313 specialization refers:
2314
2315 -- when an explicit specialization refers to a function
2316 template.
2317
2318 So, we do use the partial ordering rules, at least for now.
2319 This extension can only serve to make invalid programs valid,
2320 so it's safe. And, there is strong anecdotal evidence that
2321 the committee intended the partial ordering rules to apply;
2322 the EDG front end has that behavior, and John Spicer claims
2323 that the committee simply forgot to delete the wording in
2324 [temp.expl.spec]. */
2325 tree tmpl = most_specialized_instantiation (templates);
2326 if (tmpl != error_mark_node)
2327 {
2328 templates = tmpl;
2329 TREE_CHAIN (templates) = NULL_TREE;
2330 }
2331 }
2332
2333 // Concepts allows multiple declarations of member functions
2334 // with the same signature. Like above, we need to rely on
2335 // on the partial ordering of those candidates to determine which
2336 // is the best.
2337 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2338 {
2339 if (tree cand = most_constrained_function (candidates))
2340 {
2341 candidates = cand;
2342 TREE_CHAIN (cand) = NULL_TREE;
2343 }
2344 }
2345
2346 if (templates == NULL_TREE && candidates == NULL_TREE)
2347 {
2348 error ("template-id %qD for %q+D does not match any template "
2349 "declaration", template_id, decl);
2350 if (header_count && header_count != template_count + 1)
2351 inform (input_location, "saw %d %<template<>%>, need %d for "
2352 "specializing a member function template",
2353 header_count, template_count + 1);
2354 else
2355 print_candidates (orig_fns);
2356 return error_mark_node;
2357 }
2358 else if ((templates && TREE_CHAIN (templates))
2359 || (candidates && TREE_CHAIN (candidates))
2360 || (templates && candidates))
2361 {
2362 error ("ambiguous template specialization %qD for %q+D",
2363 template_id, decl);
2364 candidates = chainon (candidates, templates);
2365 print_candidates (candidates);
2366 return error_mark_node;
2367 }
2368
2369 /* We have one, and exactly one, match. */
2370 if (candidates)
2371 {
2372 tree fn = TREE_VALUE (candidates);
2373 *targs_out = copy_node (DECL_TI_ARGS (fn));
2374
2375 // Propagate the candidate's constraints to the declaration.
2376 set_constraints (decl, get_constraints (fn));
2377
2378 /* DECL is a re-declaration or partial instantiation of a template
2379 function. */
2380 if (TREE_CODE (fn) == TEMPLATE_DECL)
2381 return fn;
2382 /* It was a specialization of an ordinary member function in a
2383 template class. */
2384 return DECL_TI_TEMPLATE (fn);
2385 }
2386
2387 /* It was a specialization of a template. */
2388 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2389 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2390 {
2391 *targs_out = copy_node (targs);
2392 SET_TMPL_ARGS_LEVEL (*targs_out,
2393 TMPL_ARGS_DEPTH (*targs_out),
2394 TREE_PURPOSE (templates));
2395 }
2396 else
2397 *targs_out = TREE_PURPOSE (templates);
2398 return TREE_VALUE (templates);
2399 }
2400
2401 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2402 but with the default argument values filled in from those in the
2403 TMPL_TYPES. */
2404
2405 static tree
2406 copy_default_args_to_explicit_spec_1 (tree spec_types,
2407 tree tmpl_types)
2408 {
2409 tree new_spec_types;
2410
2411 if (!spec_types)
2412 return NULL_TREE;
2413
2414 if (spec_types == void_list_node)
2415 return void_list_node;
2416
2417 /* Substitute into the rest of the list. */
2418 new_spec_types =
2419 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2420 TREE_CHAIN (tmpl_types));
2421
2422 /* Add the default argument for this parameter. */
2423 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2424 TREE_VALUE (spec_types),
2425 new_spec_types);
2426 }
2427
2428 /* DECL is an explicit specialization. Replicate default arguments
2429 from the template it specializes. (That way, code like:
2430
2431 template <class T> void f(T = 3);
2432 template <> void f(double);
2433 void g () { f (); }
2434
2435 works, as required.) An alternative approach would be to look up
2436 the correct default arguments at the call-site, but this approach
2437 is consistent with how implicit instantiations are handled. */
2438
2439 static void
2440 copy_default_args_to_explicit_spec (tree decl)
2441 {
2442 tree tmpl;
2443 tree spec_types;
2444 tree tmpl_types;
2445 tree new_spec_types;
2446 tree old_type;
2447 tree new_type;
2448 tree t;
2449 tree object_type = NULL_TREE;
2450 tree in_charge = NULL_TREE;
2451 tree vtt = NULL_TREE;
2452
2453 /* See if there's anything we need to do. */
2454 tmpl = DECL_TI_TEMPLATE (decl);
2455 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2456 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2457 if (TREE_PURPOSE (t))
2458 break;
2459 if (!t)
2460 return;
2461
2462 old_type = TREE_TYPE (decl);
2463 spec_types = TYPE_ARG_TYPES (old_type);
2464
2465 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2466 {
2467 /* Remove the this pointer, but remember the object's type for
2468 CV quals. */
2469 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2470 spec_types = TREE_CHAIN (spec_types);
2471 tmpl_types = TREE_CHAIN (tmpl_types);
2472
2473 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2474 {
2475 /* DECL may contain more parameters than TMPL due to the extra
2476 in-charge parameter in constructors and destructors. */
2477 in_charge = spec_types;
2478 spec_types = TREE_CHAIN (spec_types);
2479 }
2480 if (DECL_HAS_VTT_PARM_P (decl))
2481 {
2482 vtt = spec_types;
2483 spec_types = TREE_CHAIN (spec_types);
2484 }
2485 }
2486
2487 /* Compute the merged default arguments. */
2488 new_spec_types =
2489 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2490
2491 /* Compute the new FUNCTION_TYPE. */
2492 if (object_type)
2493 {
2494 if (vtt)
2495 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2496 TREE_VALUE (vtt),
2497 new_spec_types);
2498
2499 if (in_charge)
2500 /* Put the in-charge parameter back. */
2501 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2502 TREE_VALUE (in_charge),
2503 new_spec_types);
2504
2505 new_type = build_method_type_directly (object_type,
2506 TREE_TYPE (old_type),
2507 new_spec_types);
2508 }
2509 else
2510 new_type = build_function_type (TREE_TYPE (old_type),
2511 new_spec_types);
2512 new_type = cp_build_type_attribute_variant (new_type,
2513 TYPE_ATTRIBUTES (old_type));
2514 new_type = build_exception_variant (new_type,
2515 TYPE_RAISES_EXCEPTIONS (old_type));
2516
2517 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2518 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2519
2520 TREE_TYPE (decl) = new_type;
2521 }
2522
2523 /* Return the number of template headers we expect to see for a definition
2524 or specialization of CTYPE or one of its non-template members. */
2525
2526 int
2527 num_template_headers_for_class (tree ctype)
2528 {
2529 int num_templates = 0;
2530
2531 while (ctype && CLASS_TYPE_P (ctype))
2532 {
2533 /* You're supposed to have one `template <...>' for every
2534 template class, but you don't need one for a full
2535 specialization. For example:
2536
2537 template <class T> struct S{};
2538 template <> struct S<int> { void f(); };
2539 void S<int>::f () {}
2540
2541 is correct; there shouldn't be a `template <>' for the
2542 definition of `S<int>::f'. */
2543 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2544 /* If CTYPE does not have template information of any
2545 kind, then it is not a template, nor is it nested
2546 within a template. */
2547 break;
2548 if (explicit_class_specialization_p (ctype))
2549 break;
2550 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2551 ++num_templates;
2552
2553 ctype = TYPE_CONTEXT (ctype);
2554 }
2555
2556 return num_templates;
2557 }
2558
2559 /* Do a simple sanity check on the template headers that precede the
2560 variable declaration DECL. */
2561
2562 void
2563 check_template_variable (tree decl)
2564 {
2565 tree ctx = CP_DECL_CONTEXT (decl);
2566 int wanted = num_template_headers_for_class (ctx);
2567 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2568 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2569 {
2570 if (cxx_dialect < cxx14)
2571 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2572 "variable templates only available with "
2573 "-std=c++14 or -std=gnu++14");
2574
2575 // Namespace-scope variable templates should have a template header.
2576 ++wanted;
2577 }
2578 if (template_header_count > wanted)
2579 {
2580 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2581 "too many template headers for %D (should be %d)",
2582 decl, wanted);
2583 if (warned && CLASS_TYPE_P (ctx)
2584 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2585 inform (DECL_SOURCE_LOCATION (decl),
2586 "members of an explicitly specialized class are defined "
2587 "without a template header");
2588 }
2589 }
2590
2591 /* An explicit specialization whose declarator-id or class-head-name is not
2592 qualified shall be declared in the nearest enclosing namespace of the
2593 template, or, if the namespace is inline (7.3.1), any namespace from its
2594 enclosing namespace set.
2595
2596 If the name declared in the explicit instantiation is an unqualified name,
2597 the explicit instantiation shall appear in the namespace where its template
2598 is declared or, if that namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set. */
2600
2601 void
2602 check_unqualified_spec_or_inst (tree t, location_t loc)
2603 {
2604 tree tmpl = most_general_template (t);
2605 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2606 && !is_associated_namespace (current_namespace,
2607 CP_DECL_CONTEXT (tmpl)))
2608 {
2609 if (processing_specialization)
2610 permerror (loc, "explicit specialization of %qD outside its "
2611 "namespace must use a nested-name-specifier", tmpl);
2612 else if (processing_explicit_instantiation
2613 && cxx_dialect >= cxx11)
2614 /* This was allowed in C++98, so only pedwarn. */
2615 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2616 "outside its namespace must use a nested-name-"
2617 "specifier", tmpl);
2618 }
2619 }
2620
2621 /* Check to see if the function just declared, as indicated in
2622 DECLARATOR, and in DECL, is a specialization of a function
2623 template. We may also discover that the declaration is an explicit
2624 instantiation at this point.
2625
2626 Returns DECL, or an equivalent declaration that should be used
2627 instead if all goes well. Issues an error message if something is
2628 amiss. Returns error_mark_node if the error is not easily
2629 recoverable.
2630
2631 FLAGS is a bitmask consisting of the following flags:
2632
2633 2: The function has a definition.
2634 4: The function is a friend.
2635
2636 The TEMPLATE_COUNT is the number of references to qualifying
2637 template classes that appeared in the name of the function. For
2638 example, in
2639
2640 template <class T> struct S { void f(); };
2641 void S<int>::f();
2642
2643 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2644 classes are not counted in the TEMPLATE_COUNT, so that in
2645
2646 template <class T> struct S {};
2647 template <> struct S<int> { void f(); }
2648 template <> void S<int>::f();
2649
2650 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2651 invalid; there should be no template <>.)
2652
2653 If the function is a specialization, it is marked as such via
2654 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2655 is set up correctly, and it is added to the list of specializations
2656 for that template. */
2657
2658 tree
2659 check_explicit_specialization (tree declarator,
2660 tree decl,
2661 int template_count,
2662 int flags)
2663 {
2664 int have_def = flags & 2;
2665 int is_friend = flags & 4;
2666 bool is_concept = flags & 8;
2667 int specialization = 0;
2668 int explicit_instantiation = 0;
2669 int member_specialization = 0;
2670 tree ctype = DECL_CLASS_CONTEXT (decl);
2671 tree dname = DECL_NAME (decl);
2672 tmpl_spec_kind tsk;
2673
2674 if (is_friend)
2675 {
2676 if (!processing_specialization)
2677 tsk = tsk_none;
2678 else
2679 tsk = tsk_excessive_parms;
2680 }
2681 else
2682 tsk = current_tmpl_spec_kind (template_count);
2683
2684 switch (tsk)
2685 {
2686 case tsk_none:
2687 if (processing_specialization && !VAR_P (decl))
2688 {
2689 specialization = 1;
2690 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2691 }
2692 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2693 {
2694 if (is_friend)
2695 /* This could be something like:
2696
2697 template <class T> void f(T);
2698 class S { friend void f<>(int); } */
2699 specialization = 1;
2700 else
2701 {
2702 /* This case handles bogus declarations like template <>
2703 template <class T> void f<int>(); */
2704
2705 error ("template-id %qD in declaration of primary template",
2706 declarator);
2707 return decl;
2708 }
2709 }
2710 break;
2711
2712 case tsk_invalid_member_spec:
2713 /* The error has already been reported in
2714 check_specialization_scope. */
2715 return error_mark_node;
2716
2717 case tsk_invalid_expl_inst:
2718 error ("template parameter list used in explicit instantiation");
2719
2720 /* Fall through. */
2721
2722 case tsk_expl_inst:
2723 if (have_def)
2724 error ("definition provided for explicit instantiation");
2725
2726 explicit_instantiation = 1;
2727 break;
2728
2729 case tsk_excessive_parms:
2730 case tsk_insufficient_parms:
2731 if (tsk == tsk_excessive_parms)
2732 error ("too many template parameter lists in declaration of %qD",
2733 decl);
2734 else if (template_header_count)
2735 error("too few template parameter lists in declaration of %qD", decl);
2736 else
2737 error("explicit specialization of %qD must be introduced by "
2738 "%<template <>%>", decl);
2739
2740 /* Fall through. */
2741 case tsk_expl_spec:
2742 if (is_concept)
2743 error ("explicit specialization declared %<concept%>");
2744
2745 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2746 /* In cases like template<> constexpr bool v = true;
2747 We'll give an error in check_template_variable. */
2748 break;
2749
2750 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2751 if (ctype)
2752 member_specialization = 1;
2753 else
2754 specialization = 1;
2755 break;
2756
2757 case tsk_template:
2758 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2759 {
2760 /* This case handles bogus declarations like template <>
2761 template <class T> void f<int>(); */
2762
2763 if (!uses_template_parms (declarator))
2764 error ("template-id %qD in declaration of primary template",
2765 declarator);
2766 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2767 {
2768 /* Partial specialization of variable template. */
2769 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2770 specialization = 1;
2771 goto ok;
2772 }
2773 else if (cxx_dialect < cxx14)
2774 error ("non-type partial specialization %qD "
2775 "is not allowed", declarator);
2776 else
2777 error ("non-class, non-variable partial specialization %qD "
2778 "is not allowed", declarator);
2779 return decl;
2780 ok:;
2781 }
2782
2783 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2784 /* This is a specialization of a member template, without
2785 specialization the containing class. Something like:
2786
2787 template <class T> struct S {
2788 template <class U> void f (U);
2789 };
2790 template <> template <class U> void S<int>::f(U) {}
2791
2792 That's a specialization -- but of the entire template. */
2793 specialization = 1;
2794 break;
2795
2796 default:
2797 gcc_unreachable ();
2798 }
2799
2800 if ((specialization || member_specialization)
2801 /* This doesn't apply to variable templates. */
2802 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2803 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2804 {
2805 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2806 for (; t; t = TREE_CHAIN (t))
2807 if (TREE_PURPOSE (t))
2808 {
2809 permerror (input_location,
2810 "default argument specified in explicit specialization");
2811 break;
2812 }
2813 }
2814
2815 if (specialization || member_specialization || explicit_instantiation)
2816 {
2817 tree tmpl = NULL_TREE;
2818 tree targs = NULL_TREE;
2819 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2820
2821 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2822 if (!was_template_id)
2823 {
2824 tree fns;
2825
2826 gcc_assert (identifier_p (declarator));
2827 if (ctype)
2828 fns = dname;
2829 else
2830 {
2831 /* If there is no class context, the explicit instantiation
2832 must be at namespace scope. */
2833 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2834
2835 /* Find the namespace binding, using the declaration
2836 context. */
2837 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2838 false, true);
2839 if (fns == error_mark_node)
2840 /* If lookup fails, look for a friend declaration so we can
2841 give a better diagnostic. */
2842 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2843 /*type*/false, /*complain*/true,
2844 /*hidden*/true);
2845
2846 if (fns == error_mark_node || !is_overloaded_fn (fns))
2847 {
2848 error ("%qD is not a template function", dname);
2849 fns = error_mark_node;
2850 }
2851 }
2852
2853 declarator = lookup_template_function (fns, NULL_TREE);
2854 }
2855
2856 if (declarator == error_mark_node)
2857 return error_mark_node;
2858
2859 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2860 {
2861 if (!explicit_instantiation)
2862 /* A specialization in class scope. This is invalid,
2863 but the error will already have been flagged by
2864 check_specialization_scope. */
2865 return error_mark_node;
2866 else
2867 {
2868 /* It's not valid to write an explicit instantiation in
2869 class scope, e.g.:
2870
2871 class C { template void f(); }
2872
2873 This case is caught by the parser. However, on
2874 something like:
2875
2876 template class C { void f(); };
2877
2878 (which is invalid) we can get here. The error will be
2879 issued later. */
2880 ;
2881 }
2882
2883 return decl;
2884 }
2885 else if (ctype != NULL_TREE
2886 && (identifier_p (TREE_OPERAND (declarator, 0))))
2887 {
2888 // We'll match variable templates in start_decl.
2889 if (VAR_P (decl))
2890 return decl;
2891
2892 /* Find the list of functions in ctype that have the same
2893 name as the declared function. */
2894 tree name = TREE_OPERAND (declarator, 0);
2895 tree fns = NULL_TREE;
2896 int idx;
2897
2898 if (constructor_name_p (name, ctype))
2899 {
2900 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2901
2902 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2903 : !CLASSTYPE_DESTRUCTORS (ctype))
2904 {
2905 /* From [temp.expl.spec]:
2906
2907 If such an explicit specialization for the member
2908 of a class template names an implicitly-declared
2909 special member function (clause _special_), the
2910 program is ill-formed.
2911
2912 Similar language is found in [temp.explicit]. */
2913 error ("specialization of implicitly-declared special member function");
2914 return error_mark_node;
2915 }
2916
2917 name = is_constructor ? ctor_identifier : dtor_identifier;
2918 }
2919
2920 if (!DECL_CONV_FN_P (decl))
2921 {
2922 idx = lookup_fnfields_1 (ctype, name);
2923 if (idx >= 0)
2924 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2925 }
2926 else
2927 {
2928 vec<tree, va_gc> *methods;
2929 tree ovl;
2930
2931 /* For a type-conversion operator, we cannot do a
2932 name-based lookup. We might be looking for `operator
2933 int' which will be a specialization of `operator T'.
2934 So, we find *all* the conversion operators, and then
2935 select from them. */
2936 fns = NULL_TREE;
2937
2938 methods = CLASSTYPE_METHOD_VEC (ctype);
2939 if (methods)
2940 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2941 methods->iterate (idx, &ovl);
2942 ++idx)
2943 {
2944 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2945 /* There are no more conversion functions. */
2946 break;
2947
2948 /* Glue all these conversion functions together
2949 with those we already have. */
2950 for (; ovl; ovl = OVL_NEXT (ovl))
2951 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2952 }
2953 }
2954
2955 if (fns == NULL_TREE)
2956 {
2957 error ("no member function %qD declared in %qT", name, ctype);
2958 return error_mark_node;
2959 }
2960 else
2961 TREE_OPERAND (declarator, 0) = fns;
2962 }
2963
2964 /* Figure out what exactly is being specialized at this point.
2965 Note that for an explicit instantiation, even one for a
2966 member function, we cannot tell apriori whether the
2967 instantiation is for a member template, or just a member
2968 function of a template class. Even if a member template is
2969 being instantiated, the member template arguments may be
2970 elided if they can be deduced from the rest of the
2971 declaration. */
2972 tmpl = determine_specialization (declarator, decl,
2973 &targs,
2974 member_specialization,
2975 template_count,
2976 tsk);
2977
2978 if (!tmpl || tmpl == error_mark_node)
2979 /* We couldn't figure out what this declaration was
2980 specializing. */
2981 return error_mark_node;
2982 else
2983 {
2984 if (TREE_CODE (decl) == FUNCTION_DECL
2985 && DECL_HIDDEN_FRIEND_P (tmpl))
2986 {
2987 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2988 "friend declaration %qD is not visible to "
2989 "explicit specialization", tmpl))
2990 inform (DECL_SOURCE_LOCATION (tmpl),
2991 "friend declaration here");
2992 }
2993 else if (!ctype && !is_friend
2994 && CP_DECL_CONTEXT (decl) == current_namespace)
2995 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2996
2997 tree gen_tmpl = most_general_template (tmpl);
2998
2999 if (explicit_instantiation)
3000 {
3001 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3002 is done by do_decl_instantiation later. */
3003
3004 int arg_depth = TMPL_ARGS_DEPTH (targs);
3005 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3006
3007 if (arg_depth > parm_depth)
3008 {
3009 /* If TMPL is not the most general template (for
3010 example, if TMPL is a friend template that is
3011 injected into namespace scope), then there will
3012 be too many levels of TARGS. Remove some of them
3013 here. */
3014 int i;
3015 tree new_targs;
3016
3017 new_targs = make_tree_vec (parm_depth);
3018 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3019 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3020 = TREE_VEC_ELT (targs, i);
3021 targs = new_targs;
3022 }
3023
3024 return instantiate_template (tmpl, targs, tf_error);
3025 }
3026
3027 /* If we thought that the DECL was a member function, but it
3028 turns out to be specializing a static member function,
3029 make DECL a static member function as well. */
3030 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3031 && DECL_STATIC_FUNCTION_P (tmpl)
3032 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3033 revert_static_member_fn (decl);
3034
3035 /* If this is a specialization of a member template of a
3036 template class, we want to return the TEMPLATE_DECL, not
3037 the specialization of it. */
3038 if (tsk == tsk_template && !was_template_id)
3039 {
3040 tree result = DECL_TEMPLATE_RESULT (tmpl);
3041 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3042 DECL_INITIAL (result) = NULL_TREE;
3043 if (have_def)
3044 {
3045 tree parm;
3046 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3047 DECL_SOURCE_LOCATION (result)
3048 = DECL_SOURCE_LOCATION (decl);
3049 /* We want to use the argument list specified in the
3050 definition, not in the original declaration. */
3051 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3052 for (parm = DECL_ARGUMENTS (result); parm;
3053 parm = DECL_CHAIN (parm))
3054 DECL_CONTEXT (parm) = result;
3055 }
3056 return register_specialization (tmpl, gen_tmpl, targs,
3057 is_friend, 0);
3058 }
3059
3060 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3061 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3062
3063 if (was_template_id)
3064 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3065
3066 /* Inherit default function arguments from the template
3067 DECL is specializing. */
3068 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3069 copy_default_args_to_explicit_spec (decl);
3070
3071 /* This specialization has the same protection as the
3072 template it specializes. */
3073 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3074 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3075
3076 /* 7.1.1-1 [dcl.stc]
3077
3078 A storage-class-specifier shall not be specified in an
3079 explicit specialization...
3080
3081 The parser rejects these, so unless action is taken here,
3082 explicit function specializations will always appear with
3083 global linkage.
3084
3085 The action recommended by the C++ CWG in response to C++
3086 defect report 605 is to make the storage class and linkage
3087 of the explicit specialization match the templated function:
3088
3089 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3090 */
3091 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3092 {
3093 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3094 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3095
3096 /* A concept cannot be specialized. */
3097 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3098 {
3099 error ("explicit specialization of function concept %qD",
3100 gen_tmpl);
3101 return error_mark_node;
3102 }
3103
3104 /* This specialization has the same linkage and visibility as
3105 the function template it specializes. */
3106 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3107 if (! TREE_PUBLIC (decl))
3108 {
3109 DECL_INTERFACE_KNOWN (decl) = 1;
3110 DECL_NOT_REALLY_EXTERN (decl) = 1;
3111 }
3112 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3113 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3114 {
3115 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3116 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3117 }
3118 }
3119
3120 /* If DECL is a friend declaration, declared using an
3121 unqualified name, the namespace associated with DECL may
3122 have been set incorrectly. For example, in:
3123
3124 template <typename T> void f(T);
3125 namespace N {
3126 struct S { friend void f<int>(int); }
3127 }
3128
3129 we will have set the DECL_CONTEXT for the friend
3130 declaration to N, rather than to the global namespace. */
3131 if (DECL_NAMESPACE_SCOPE_P (decl))
3132 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3133
3134 if (is_friend && !have_def)
3135 /* This is not really a declaration of a specialization.
3136 It's just the name of an instantiation. But, it's not
3137 a request for an instantiation, either. */
3138 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3139 else if (TREE_CODE (decl) == FUNCTION_DECL)
3140 /* A specialization is not necessarily COMDAT. */
3141 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3142 && DECL_DECLARED_INLINE_P (decl));
3143 else if (VAR_P (decl))
3144 DECL_COMDAT (decl) = false;
3145
3146 /* If this is a full specialization, register it so that we can find
3147 it again. Partial specializations will be registered in
3148 process_partial_specialization. */
3149 if (!processing_template_decl)
3150 decl = register_specialization (decl, gen_tmpl, targs,
3151 is_friend, 0);
3152
3153 /* A 'structor should already have clones. */
3154 gcc_assert (decl == error_mark_node
3155 || variable_template_p (tmpl)
3156 || !(DECL_CONSTRUCTOR_P (decl)
3157 || DECL_DESTRUCTOR_P (decl))
3158 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3159 }
3160 }
3161
3162 return decl;
3163 }
3164
3165 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3166 parameters. These are represented in the same format used for
3167 DECL_TEMPLATE_PARMS. */
3168
3169 int
3170 comp_template_parms (const_tree parms1, const_tree parms2)
3171 {
3172 const_tree p1;
3173 const_tree p2;
3174
3175 if (parms1 == parms2)
3176 return 1;
3177
3178 for (p1 = parms1, p2 = parms2;
3179 p1 != NULL_TREE && p2 != NULL_TREE;
3180 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3181 {
3182 tree t1 = TREE_VALUE (p1);
3183 tree t2 = TREE_VALUE (p2);
3184 int i;
3185
3186 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3187 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3188
3189 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3190 return 0;
3191
3192 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3193 {
3194 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3195 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3196
3197 /* If either of the template parameters are invalid, assume
3198 they match for the sake of error recovery. */
3199 if (error_operand_p (parm1) || error_operand_p (parm2))
3200 return 1;
3201
3202 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3203 return 0;
3204
3205 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3206 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3207 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3208 continue;
3209 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3210 return 0;
3211 }
3212 }
3213
3214 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3215 /* One set of parameters has more parameters lists than the
3216 other. */
3217 return 0;
3218
3219 return 1;
3220 }
3221
3222 /* Determine whether PARM is a parameter pack. */
3223
3224 bool
3225 template_parameter_pack_p (const_tree parm)
3226 {
3227 /* Determine if we have a non-type template parameter pack. */
3228 if (TREE_CODE (parm) == PARM_DECL)
3229 return (DECL_TEMPLATE_PARM_P (parm)
3230 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3231 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3232 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3233
3234 /* If this is a list of template parameters, we could get a
3235 TYPE_DECL or a TEMPLATE_DECL. */
3236 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3237 parm = TREE_TYPE (parm);
3238
3239 /* Otherwise it must be a type template parameter. */
3240 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3241 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3242 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3243 }
3244
3245 /* Determine if T is a function parameter pack. */
3246
3247 bool
3248 function_parameter_pack_p (const_tree t)
3249 {
3250 if (t && TREE_CODE (t) == PARM_DECL)
3251 return DECL_PACK_P (t);
3252 return false;
3253 }
3254
3255 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3256 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3257
3258 tree
3259 get_function_template_decl (const_tree primary_func_tmpl_inst)
3260 {
3261 if (! primary_func_tmpl_inst
3262 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3263 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3264 return NULL;
3265
3266 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3267 }
3268
3269 /* Return true iff the function parameter PARAM_DECL was expanded
3270 from the function parameter pack PACK. */
3271
3272 bool
3273 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3274 {
3275 if (DECL_ARTIFICIAL (param_decl)
3276 || !function_parameter_pack_p (pack))
3277 return false;
3278
3279 /* The parameter pack and its pack arguments have the same
3280 DECL_PARM_INDEX. */
3281 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3282 }
3283
3284 /* Determine whether ARGS describes a variadic template args list,
3285 i.e., one that is terminated by a template argument pack. */
3286
3287 static bool
3288 template_args_variadic_p (tree args)
3289 {
3290 int nargs;
3291 tree last_parm;
3292
3293 if (args == NULL_TREE)
3294 return false;
3295
3296 args = INNERMOST_TEMPLATE_ARGS (args);
3297 nargs = TREE_VEC_LENGTH (args);
3298
3299 if (nargs == 0)
3300 return false;
3301
3302 last_parm = TREE_VEC_ELT (args, nargs - 1);
3303
3304 return ARGUMENT_PACK_P (last_parm);
3305 }
3306
3307 /* Generate a new name for the parameter pack name NAME (an
3308 IDENTIFIER_NODE) that incorporates its */
3309
3310 static tree
3311 make_ith_pack_parameter_name (tree name, int i)
3312 {
3313 /* Munge the name to include the parameter index. */
3314 #define NUMBUF_LEN 128
3315 char numbuf[NUMBUF_LEN];
3316 char* newname;
3317 int newname_len;
3318
3319 if (name == NULL_TREE)
3320 return name;
3321 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3322 newname_len = IDENTIFIER_LENGTH (name)
3323 + strlen (numbuf) + 2;
3324 newname = (char*)alloca (newname_len);
3325 snprintf (newname, newname_len,
3326 "%s#%i", IDENTIFIER_POINTER (name), i);
3327 return get_identifier (newname);
3328 }
3329
3330 /* Return true if T is a primary function, class or alias template
3331 instantiation. */
3332
3333 bool
3334 primary_template_instantiation_p (const_tree t)
3335 {
3336 if (!t)
3337 return false;
3338
3339 if (TREE_CODE (t) == FUNCTION_DECL)
3340 return DECL_LANG_SPECIFIC (t)
3341 && DECL_TEMPLATE_INSTANTIATION (t)
3342 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3343 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3344 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3345 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3346 else if (alias_template_specialization_p (t))
3347 return true;
3348 return false;
3349 }
3350
3351 /* Return true if PARM is a template template parameter. */
3352
3353 bool
3354 template_template_parameter_p (const_tree parm)
3355 {
3356 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3357 }
3358
3359 /* Return true iff PARM is a DECL representing a type template
3360 parameter. */
3361
3362 bool
3363 template_type_parameter_p (const_tree parm)
3364 {
3365 return (parm
3366 && (TREE_CODE (parm) == TYPE_DECL
3367 || TREE_CODE (parm) == TEMPLATE_DECL)
3368 && DECL_TEMPLATE_PARM_P (parm));
3369 }
3370
3371 /* Return the template parameters of T if T is a
3372 primary template instantiation, NULL otherwise. */
3373
3374 tree
3375 get_primary_template_innermost_parameters (const_tree t)
3376 {
3377 tree parms = NULL, template_info = NULL;
3378
3379 if ((template_info = get_template_info (t))
3380 && primary_template_instantiation_p (t))
3381 parms = INNERMOST_TEMPLATE_PARMS
3382 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3383
3384 return parms;
3385 }
3386
3387 /* Return the template parameters of the LEVELth level from the full list
3388 of template parameters PARMS. */
3389
3390 tree
3391 get_template_parms_at_level (tree parms, int level)
3392 {
3393 tree p;
3394 if (!parms
3395 || TREE_CODE (parms) != TREE_LIST
3396 || level > TMPL_PARMS_DEPTH (parms))
3397 return NULL_TREE;
3398
3399 for (p = parms; p; p = TREE_CHAIN (p))
3400 if (TMPL_PARMS_DEPTH (p) == level)
3401 return p;
3402
3403 return NULL_TREE;
3404 }
3405
3406 /* Returns the template arguments of T if T is a template instantiation,
3407 NULL otherwise. */
3408
3409 tree
3410 get_template_innermost_arguments (const_tree t)
3411 {
3412 tree args = NULL, template_info = NULL;
3413
3414 if ((template_info = get_template_info (t))
3415 && TI_ARGS (template_info))
3416 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3417
3418 return args;
3419 }
3420
3421 /* Return the argument pack elements of T if T is a template argument pack,
3422 NULL otherwise. */
3423
3424 tree
3425 get_template_argument_pack_elems (const_tree t)
3426 {
3427 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3428 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3429 return NULL;
3430
3431 return ARGUMENT_PACK_ARGS (t);
3432 }
3433
3434 /* Structure used to track the progress of find_parameter_packs_r. */
3435 struct find_parameter_pack_data
3436 {
3437 /* TREE_LIST that will contain all of the parameter packs found by
3438 the traversal. */
3439 tree* parameter_packs;
3440
3441 /* Set of AST nodes that have been visited by the traversal. */
3442 hash_set<tree> *visited;
3443
3444 /* True iff we're making a type pack expansion. */
3445 bool type_pack_expansion_p;
3446 };
3447
3448 /* Identifies all of the argument packs that occur in a template
3449 argument and appends them to the TREE_LIST inside DATA, which is a
3450 find_parameter_pack_data structure. This is a subroutine of
3451 make_pack_expansion and uses_parameter_packs. */
3452 static tree
3453 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3454 {
3455 tree t = *tp;
3456 struct find_parameter_pack_data* ppd =
3457 (struct find_parameter_pack_data*)data;
3458 bool parameter_pack_p = false;
3459
3460 /* Handle type aliases/typedefs. */
3461 if (TYPE_ALIAS_P (t))
3462 {
3463 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3464 cp_walk_tree (&TI_ARGS (tinfo),
3465 &find_parameter_packs_r,
3466 ppd, ppd->visited);
3467 *walk_subtrees = 0;
3468 return NULL_TREE;
3469 }
3470
3471 /* Identify whether this is a parameter pack or not. */
3472 switch (TREE_CODE (t))
3473 {
3474 case TEMPLATE_PARM_INDEX:
3475 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3476 parameter_pack_p = true;
3477 break;
3478
3479 case TEMPLATE_TYPE_PARM:
3480 t = TYPE_MAIN_VARIANT (t);
3481 /* FALLTHRU */
3482 case TEMPLATE_TEMPLATE_PARM:
3483 /* If the placeholder appears in the decl-specifier-seq of a function
3484 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3485 is a pack expansion, the invented template parameter is a template
3486 parameter pack. */
3487 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3488 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3489 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3490 parameter_pack_p = true;
3491 break;
3492
3493 case FIELD_DECL:
3494 case PARM_DECL:
3495 if (DECL_PACK_P (t))
3496 {
3497 /* We don't want to walk into the type of a PARM_DECL,
3498 because we don't want to see the type parameter pack. */
3499 *walk_subtrees = 0;
3500 parameter_pack_p = true;
3501 }
3502 break;
3503
3504 /* Look through a lambda capture proxy to the field pack. */
3505 case VAR_DECL:
3506 if (DECL_HAS_VALUE_EXPR_P (t))
3507 {
3508 tree v = DECL_VALUE_EXPR (t);
3509 cp_walk_tree (&v,
3510 &find_parameter_packs_r,
3511 ppd, ppd->visited);
3512 *walk_subtrees = 0;
3513 }
3514 else if (variable_template_specialization_p (t))
3515 {
3516 cp_walk_tree (&DECL_TI_ARGS (t),
3517 find_parameter_packs_r,
3518 ppd, ppd->visited);
3519 *walk_subtrees = 0;
3520 }
3521 break;
3522
3523 case BASES:
3524 parameter_pack_p = true;
3525 break;
3526 default:
3527 /* Not a parameter pack. */
3528 break;
3529 }
3530
3531 if (parameter_pack_p)
3532 {
3533 /* Add this parameter pack to the list. */
3534 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3535 }
3536
3537 if (TYPE_P (t))
3538 cp_walk_tree (&TYPE_CONTEXT (t),
3539 &find_parameter_packs_r, ppd, ppd->visited);
3540
3541 /* This switch statement will return immediately if we don't find a
3542 parameter pack. */
3543 switch (TREE_CODE (t))
3544 {
3545 case TEMPLATE_PARM_INDEX:
3546 return NULL_TREE;
3547
3548 case BOUND_TEMPLATE_TEMPLATE_PARM:
3549 /* Check the template itself. */
3550 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3551 &find_parameter_packs_r, ppd, ppd->visited);
3552 /* Check the template arguments. */
3553 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3554 ppd->visited);
3555 *walk_subtrees = 0;
3556 return NULL_TREE;
3557
3558 case TEMPLATE_TYPE_PARM:
3559 case TEMPLATE_TEMPLATE_PARM:
3560 return NULL_TREE;
3561
3562 case PARM_DECL:
3563 return NULL_TREE;
3564
3565 case RECORD_TYPE:
3566 if (TYPE_PTRMEMFUNC_P (t))
3567 return NULL_TREE;
3568 /* Fall through. */
3569
3570 case UNION_TYPE:
3571 case ENUMERAL_TYPE:
3572 if (TYPE_TEMPLATE_INFO (t))
3573 cp_walk_tree (&TYPE_TI_ARGS (t),
3574 &find_parameter_packs_r, ppd, ppd->visited);
3575
3576 *walk_subtrees = 0;
3577 return NULL_TREE;
3578
3579 case CONSTRUCTOR:
3580 case TEMPLATE_DECL:
3581 cp_walk_tree (&TREE_TYPE (t),
3582 &find_parameter_packs_r, ppd, ppd->visited);
3583 return NULL_TREE;
3584
3585 case TYPENAME_TYPE:
3586 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3587 ppd, ppd->visited);
3588 *walk_subtrees = 0;
3589 return NULL_TREE;
3590
3591 case TYPE_PACK_EXPANSION:
3592 case EXPR_PACK_EXPANSION:
3593 *walk_subtrees = 0;
3594 return NULL_TREE;
3595
3596 case INTEGER_TYPE:
3597 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3598 ppd, ppd->visited);
3599 *walk_subtrees = 0;
3600 return NULL_TREE;
3601
3602 case IDENTIFIER_NODE:
3603 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3604 ppd->visited);
3605 *walk_subtrees = 0;
3606 return NULL_TREE;
3607
3608 case DECLTYPE_TYPE:
3609 {
3610 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3611 type_pack_expansion_p to false so that any placeholders
3612 within the expression don't get marked as parameter packs. */
3613 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3614 ppd->type_pack_expansion_p = false;
3615 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3616 ppd, ppd->visited);
3617 ppd->type_pack_expansion_p = type_pack_expansion_p;
3618 *walk_subtrees = 0;
3619 return NULL_TREE;
3620 }
3621
3622 default:
3623 return NULL_TREE;
3624 }
3625
3626 return NULL_TREE;
3627 }
3628
3629 /* Determines if the expression or type T uses any parameter packs. */
3630 bool
3631 uses_parameter_packs (tree t)
3632 {
3633 tree parameter_packs = NULL_TREE;
3634 struct find_parameter_pack_data ppd;
3635 ppd.parameter_packs = &parameter_packs;
3636 ppd.visited = new hash_set<tree>;
3637 ppd.type_pack_expansion_p = false;
3638 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3639 delete ppd.visited;
3640 return parameter_packs != NULL_TREE;
3641 }
3642
3643 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3644 representation a base-class initializer into a parameter pack
3645 expansion. If all goes well, the resulting node will be an
3646 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3647 respectively. */
3648 tree
3649 make_pack_expansion (tree arg)
3650 {
3651 tree result;
3652 tree parameter_packs = NULL_TREE;
3653 bool for_types = false;
3654 struct find_parameter_pack_data ppd;
3655
3656 if (!arg || arg == error_mark_node)
3657 return arg;
3658
3659 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3660 {
3661 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3662 class initializer. In this case, the TREE_PURPOSE will be a
3663 _TYPE node (representing the base class expansion we're
3664 initializing) and the TREE_VALUE will be a TREE_LIST
3665 containing the initialization arguments.
3666
3667 The resulting expansion looks somewhat different from most
3668 expansions. Rather than returning just one _EXPANSION, we
3669 return a TREE_LIST whose TREE_PURPOSE is a
3670 TYPE_PACK_EXPANSION containing the bases that will be
3671 initialized. The TREE_VALUE will be identical to the
3672 original TREE_VALUE, which is a list of arguments that will
3673 be passed to each base. We do not introduce any new pack
3674 expansion nodes into the TREE_VALUE (although it is possible
3675 that some already exist), because the TREE_PURPOSE and
3676 TREE_VALUE all need to be expanded together with the same
3677 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3678 resulting TREE_PURPOSE will mention the parameter packs in
3679 both the bases and the arguments to the bases. */
3680 tree purpose;
3681 tree value;
3682 tree parameter_packs = NULL_TREE;
3683
3684 /* Determine which parameter packs will be used by the base
3685 class expansion. */
3686 ppd.visited = new hash_set<tree>;
3687 ppd.parameter_packs = &parameter_packs;
3688 ppd.type_pack_expansion_p = true;
3689 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3690 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3691 &ppd, ppd.visited);
3692
3693 if (parameter_packs == NULL_TREE)
3694 {
3695 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3696 delete ppd.visited;
3697 return error_mark_node;
3698 }
3699
3700 if (TREE_VALUE (arg) != void_type_node)
3701 {
3702 /* Collect the sets of parameter packs used in each of the
3703 initialization arguments. */
3704 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3705 {
3706 /* Determine which parameter packs will be expanded in this
3707 argument. */
3708 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3709 &ppd, ppd.visited);
3710 }
3711 }
3712
3713 delete ppd.visited;
3714
3715 /* Create the pack expansion type for the base type. */
3716 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3717 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3718 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3719
3720 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3721 they will rarely be compared to anything. */
3722 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3723
3724 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3725 }
3726
3727 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3728 for_types = true;
3729
3730 /* Build the PACK_EXPANSION_* node. */
3731 result = for_types
3732 ? cxx_make_type (TYPE_PACK_EXPANSION)
3733 : make_node (EXPR_PACK_EXPANSION);
3734 SET_PACK_EXPANSION_PATTERN (result, arg);
3735 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3736 {
3737 /* Propagate type and const-expression information. */
3738 TREE_TYPE (result) = TREE_TYPE (arg);
3739 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3740 /* Mark this read now, since the expansion might be length 0. */
3741 mark_exp_read (arg);
3742 }
3743 else
3744 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3745 they will rarely be compared to anything. */
3746 SET_TYPE_STRUCTURAL_EQUALITY (result);
3747
3748 /* Determine which parameter packs will be expanded. */
3749 ppd.parameter_packs = &parameter_packs;
3750 ppd.visited = new hash_set<tree>;
3751 ppd.type_pack_expansion_p = TYPE_P (arg);
3752 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3753 delete ppd.visited;
3754
3755 /* Make sure we found some parameter packs. */
3756 if (parameter_packs == NULL_TREE)
3757 {
3758 if (TYPE_P (arg))
3759 error ("expansion pattern %<%T%> contains no argument packs", arg);
3760 else
3761 error ("expansion pattern %<%E%> contains no argument packs", arg);
3762 return error_mark_node;
3763 }
3764 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3765
3766 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3767
3768 return result;
3769 }
3770
3771 /* Checks T for any "bare" parameter packs, which have not yet been
3772 expanded, and issues an error if any are found. This operation can
3773 only be done on full expressions or types (e.g., an expression
3774 statement, "if" condition, etc.), because we could have expressions like:
3775
3776 foo(f(g(h(args)))...)
3777
3778 where "args" is a parameter pack. check_for_bare_parameter_packs
3779 should not be called for the subexpressions args, h(args),
3780 g(h(args)), or f(g(h(args))), because we would produce erroneous
3781 error messages.
3782
3783 Returns TRUE and emits an error if there were bare parameter packs,
3784 returns FALSE otherwise. */
3785 bool
3786 check_for_bare_parameter_packs (tree t)
3787 {
3788 tree parameter_packs = NULL_TREE;
3789 struct find_parameter_pack_data ppd;
3790
3791 if (!processing_template_decl || !t || t == error_mark_node)
3792 return false;
3793
3794 if (TREE_CODE (t) == TYPE_DECL)
3795 t = TREE_TYPE (t);
3796
3797 ppd.parameter_packs = &parameter_packs;
3798 ppd.visited = new hash_set<tree>;
3799 ppd.type_pack_expansion_p = false;
3800 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3801 delete ppd.visited;
3802
3803 if (parameter_packs)
3804 {
3805 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3806 error_at (loc, "parameter packs not expanded with %<...%>:");
3807 while (parameter_packs)
3808 {
3809 tree pack = TREE_VALUE (parameter_packs);
3810 tree name = NULL_TREE;
3811
3812 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3813 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3814 name = TYPE_NAME (pack);
3815 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3816 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3817 else
3818 name = DECL_NAME (pack);
3819
3820 if (name)
3821 inform (loc, " %qD", name);
3822 else
3823 inform (loc, " <anonymous>");
3824
3825 parameter_packs = TREE_CHAIN (parameter_packs);
3826 }
3827
3828 return true;
3829 }
3830
3831 return false;
3832 }
3833
3834 /* Expand any parameter packs that occur in the template arguments in
3835 ARGS. */
3836 tree
3837 expand_template_argument_pack (tree args)
3838 {
3839 if (args == error_mark_node)
3840 return error_mark_node;
3841
3842 tree result_args = NULL_TREE;
3843 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3844 int num_result_args = -1;
3845 int non_default_args_count = -1;
3846
3847 /* First, determine if we need to expand anything, and the number of
3848 slots we'll need. */
3849 for (in_arg = 0; in_arg < nargs; ++in_arg)
3850 {
3851 tree arg = TREE_VEC_ELT (args, in_arg);
3852 if (arg == NULL_TREE)
3853 return args;
3854 if (ARGUMENT_PACK_P (arg))
3855 {
3856 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3857 if (num_result_args < 0)
3858 num_result_args = in_arg + num_packed;
3859 else
3860 num_result_args += num_packed;
3861 }
3862 else
3863 {
3864 if (num_result_args >= 0)
3865 num_result_args++;
3866 }
3867 }
3868
3869 /* If no expansion is necessary, we're done. */
3870 if (num_result_args < 0)
3871 return args;
3872
3873 /* Expand arguments. */
3874 result_args = make_tree_vec (num_result_args);
3875 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3876 non_default_args_count =
3877 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3878 for (in_arg = 0; in_arg < nargs; ++in_arg)
3879 {
3880 tree arg = TREE_VEC_ELT (args, in_arg);
3881 if (ARGUMENT_PACK_P (arg))
3882 {
3883 tree packed = ARGUMENT_PACK_ARGS (arg);
3884 int i, num_packed = TREE_VEC_LENGTH (packed);
3885 for (i = 0; i < num_packed; ++i, ++out_arg)
3886 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3887 if (non_default_args_count > 0)
3888 non_default_args_count += num_packed - 1;
3889 }
3890 else
3891 {
3892 TREE_VEC_ELT (result_args, out_arg) = arg;
3893 ++out_arg;
3894 }
3895 }
3896 if (non_default_args_count >= 0)
3897 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3898 return result_args;
3899 }
3900
3901 /* Checks if DECL shadows a template parameter.
3902
3903 [temp.local]: A template-parameter shall not be redeclared within its
3904 scope (including nested scopes).
3905
3906 Emits an error and returns TRUE if the DECL shadows a parameter,
3907 returns FALSE otherwise. */
3908
3909 bool
3910 check_template_shadow (tree decl)
3911 {
3912 tree olddecl;
3913
3914 /* If we're not in a template, we can't possibly shadow a template
3915 parameter. */
3916 if (!current_template_parms)
3917 return true;
3918
3919 /* Figure out what we're shadowing. */
3920 if (TREE_CODE (decl) == OVERLOAD)
3921 decl = OVL_CURRENT (decl);
3922 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3923
3924 /* If there's no previous binding for this name, we're not shadowing
3925 anything, let alone a template parameter. */
3926 if (!olddecl)
3927 return true;
3928
3929 /* If we're not shadowing a template parameter, we're done. Note
3930 that OLDDECL might be an OVERLOAD (or perhaps even an
3931 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3932 node. */
3933 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3934 return true;
3935
3936 /* We check for decl != olddecl to avoid bogus errors for using a
3937 name inside a class. We check TPFI to avoid duplicate errors for
3938 inline member templates. */
3939 if (decl == olddecl
3940 || (DECL_TEMPLATE_PARM_P (decl)
3941 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3942 return true;
3943
3944 /* Don't complain about the injected class name, as we've already
3945 complained about the class itself. */
3946 if (DECL_SELF_REFERENCE_P (decl))
3947 return false;
3948
3949 if (DECL_TEMPLATE_PARM_P (decl))
3950 error ("declaration of template parameter %q+D shadows "
3951 "template parameter", decl);
3952 else
3953 error ("declaration of %q+#D shadows template parameter", decl);
3954 inform (DECL_SOURCE_LOCATION (olddecl),
3955 "template parameter %qD declared here", olddecl);
3956 return false;
3957 }
3958
3959 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3960 ORIG_LEVEL, DECL, and TYPE. */
3961
3962 static tree
3963 build_template_parm_index (int index,
3964 int level,
3965 int orig_level,
3966 tree decl,
3967 tree type)
3968 {
3969 tree t = make_node (TEMPLATE_PARM_INDEX);
3970 TEMPLATE_PARM_IDX (t) = index;
3971 TEMPLATE_PARM_LEVEL (t) = level;
3972 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3973 TEMPLATE_PARM_DECL (t) = decl;
3974 TREE_TYPE (t) = type;
3975 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3976 TREE_READONLY (t) = TREE_READONLY (decl);
3977
3978 return t;
3979 }
3980
3981 /* Find the canonical type parameter for the given template type
3982 parameter. Returns the canonical type parameter, which may be TYPE
3983 if no such parameter existed. */
3984
3985 static tree
3986 canonical_type_parameter (tree type)
3987 {
3988 tree list;
3989 int idx = TEMPLATE_TYPE_IDX (type);
3990 if (!canonical_template_parms)
3991 vec_alloc (canonical_template_parms, idx+1);
3992
3993 while (canonical_template_parms->length () <= (unsigned)idx)
3994 vec_safe_push (canonical_template_parms, NULL_TREE);
3995
3996 list = (*canonical_template_parms)[idx];
3997 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3998 list = TREE_CHAIN (list);
3999
4000 if (list)
4001 return TREE_VALUE (list);
4002 else
4003 {
4004 (*canonical_template_parms)[idx]
4005 = tree_cons (NULL_TREE, type,
4006 (*canonical_template_parms)[idx]);
4007 return type;
4008 }
4009 }
4010
4011 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4012 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4013 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4014 new one is created. */
4015
4016 static tree
4017 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4018 tsubst_flags_t complain)
4019 {
4020 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4021 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4022 != TEMPLATE_PARM_LEVEL (index) - levels)
4023 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4024 {
4025 tree orig_decl = TEMPLATE_PARM_DECL (index);
4026 tree decl, t;
4027
4028 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4029 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4030 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4031 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4032 DECL_ARTIFICIAL (decl) = 1;
4033 SET_DECL_TEMPLATE_PARM_P (decl);
4034
4035 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4036 TEMPLATE_PARM_LEVEL (index) - levels,
4037 TEMPLATE_PARM_ORIG_LEVEL (index),
4038 decl, type);
4039 TEMPLATE_PARM_DESCENDANTS (index) = t;
4040 TEMPLATE_PARM_PARAMETER_PACK (t)
4041 = TEMPLATE_PARM_PARAMETER_PACK (index);
4042
4043 /* Template template parameters need this. */
4044 if (TREE_CODE (decl) == TEMPLATE_DECL)
4045 {
4046 DECL_TEMPLATE_RESULT (decl)
4047 = build_decl (DECL_SOURCE_LOCATION (decl),
4048 TYPE_DECL, DECL_NAME (decl), type);
4049 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4050 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4051 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4052 }
4053 }
4054
4055 return TEMPLATE_PARM_DESCENDANTS (index);
4056 }
4057
4058 /* Process information from new template parameter PARM and append it
4059 to the LIST being built. This new parameter is a non-type
4060 parameter iff IS_NON_TYPE is true. This new parameter is a
4061 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4062 is in PARM_LOC. */
4063
4064 tree
4065 process_template_parm (tree list, location_t parm_loc, tree parm,
4066 bool is_non_type, bool is_parameter_pack)
4067 {
4068 tree decl = 0;
4069 int idx = 0;
4070
4071 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4072 tree defval = TREE_PURPOSE (parm);
4073 tree constr = TREE_TYPE (parm);
4074
4075 if (list)
4076 {
4077 tree p = tree_last (list);
4078
4079 if (p && TREE_VALUE (p) != error_mark_node)
4080 {
4081 p = TREE_VALUE (p);
4082 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4083 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4084 else
4085 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4086 }
4087
4088 ++idx;
4089 }
4090
4091 if (is_non_type)
4092 {
4093 parm = TREE_VALUE (parm);
4094
4095 SET_DECL_TEMPLATE_PARM_P (parm);
4096
4097 if (TREE_TYPE (parm) != error_mark_node)
4098 {
4099 /* [temp.param]
4100
4101 The top-level cv-qualifiers on the template-parameter are
4102 ignored when determining its type. */
4103 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4104 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4105 TREE_TYPE (parm) = error_mark_node;
4106 else if (uses_parameter_packs (TREE_TYPE (parm))
4107 && !is_parameter_pack
4108 /* If we're in a nested template parameter list, the template
4109 template parameter could be a parameter pack. */
4110 && processing_template_parmlist == 1)
4111 {
4112 /* This template parameter is not a parameter pack, but it
4113 should be. Complain about "bare" parameter packs. */
4114 check_for_bare_parameter_packs (TREE_TYPE (parm));
4115
4116 /* Recover by calling this a parameter pack. */
4117 is_parameter_pack = true;
4118 }
4119 }
4120
4121 /* A template parameter is not modifiable. */
4122 TREE_CONSTANT (parm) = 1;
4123 TREE_READONLY (parm) = 1;
4124 decl = build_decl (parm_loc,
4125 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4126 TREE_CONSTANT (decl) = 1;
4127 TREE_READONLY (decl) = 1;
4128 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4129 = build_template_parm_index (idx, processing_template_decl,
4130 processing_template_decl,
4131 decl, TREE_TYPE (parm));
4132
4133 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4134 = is_parameter_pack;
4135 }
4136 else
4137 {
4138 tree t;
4139 parm = TREE_VALUE (TREE_VALUE (parm));
4140
4141 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4142 {
4143 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4144 /* This is for distinguishing between real templates and template
4145 template parameters */
4146 TREE_TYPE (parm) = t;
4147 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4148 decl = parm;
4149 }
4150 else
4151 {
4152 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4153 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4154 decl = build_decl (parm_loc,
4155 TYPE_DECL, parm, t);
4156 }
4157
4158 TYPE_NAME (t) = decl;
4159 TYPE_STUB_DECL (t) = decl;
4160 parm = decl;
4161 TEMPLATE_TYPE_PARM_INDEX (t)
4162 = build_template_parm_index (idx, processing_template_decl,
4163 processing_template_decl,
4164 decl, TREE_TYPE (parm));
4165 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4166 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4167 }
4168 DECL_ARTIFICIAL (decl) = 1;
4169 SET_DECL_TEMPLATE_PARM_P (decl);
4170
4171 /* Build requirements for the type/template parameter.
4172 This must be done after SET_DECL_TEMPLATE_PARM_P or
4173 process_template_parm could fail. */
4174 tree reqs = finish_shorthand_constraint (parm, constr);
4175
4176 pushdecl (decl);
4177
4178 /* Build the parameter node linking the parameter declaration,
4179 its default argument (if any), and its constraints (if any). */
4180 parm = build_tree_list (defval, parm);
4181 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4182
4183 return chainon (list, parm);
4184 }
4185
4186 /* The end of a template parameter list has been reached. Process the
4187 tree list into a parameter vector, converting each parameter into a more
4188 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4189 as PARM_DECLs. */
4190
4191 tree
4192 end_template_parm_list (tree parms)
4193 {
4194 int nparms;
4195 tree parm, next;
4196 tree saved_parmlist = make_tree_vec (list_length (parms));
4197
4198 /* Pop the dummy parameter level and add the real one. */
4199 current_template_parms = TREE_CHAIN (current_template_parms);
4200
4201 current_template_parms
4202 = tree_cons (size_int (processing_template_decl),
4203 saved_parmlist, current_template_parms);
4204
4205 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4206 {
4207 next = TREE_CHAIN (parm);
4208 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4209 TREE_CHAIN (parm) = NULL_TREE;
4210 }
4211
4212 --processing_template_parmlist;
4213
4214 return saved_parmlist;
4215 }
4216
4217 // Explicitly indicate the end of the template parameter list. We assume
4218 // that the current template parameters have been constructed and/or
4219 // managed explicitly, as when creating new template template parameters
4220 // from a shorthand constraint.
4221 void
4222 end_template_parm_list ()
4223 {
4224 --processing_template_parmlist;
4225 }
4226
4227 /* end_template_decl is called after a template declaration is seen. */
4228
4229 void
4230 end_template_decl (void)
4231 {
4232 reset_specialization ();
4233
4234 if (! processing_template_decl)
4235 return;
4236
4237 /* This matches the pushlevel in begin_template_parm_list. */
4238 finish_scope ();
4239
4240 --processing_template_decl;
4241 current_template_parms = TREE_CHAIN (current_template_parms);
4242 }
4243
4244 /* Takes a TREE_LIST representing a template parameter and convert it
4245 into an argument suitable to be passed to the type substitution
4246 functions. Note that If the TREE_LIST contains an error_mark
4247 node, the returned argument is error_mark_node. */
4248
4249 tree
4250 template_parm_to_arg (tree t)
4251 {
4252
4253 if (t == NULL_TREE
4254 || TREE_CODE (t) != TREE_LIST)
4255 return t;
4256
4257 if (error_operand_p (TREE_VALUE (t)))
4258 return error_mark_node;
4259
4260 t = TREE_VALUE (t);
4261
4262 if (TREE_CODE (t) == TYPE_DECL
4263 || TREE_CODE (t) == TEMPLATE_DECL)
4264 {
4265 t = TREE_TYPE (t);
4266
4267 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4268 {
4269 /* Turn this argument into a TYPE_ARGUMENT_PACK
4270 with a single element, which expands T. */
4271 tree vec = make_tree_vec (1);
4272 if (CHECKING_P)
4273 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4274
4275 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4276
4277 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4278 SET_ARGUMENT_PACK_ARGS (t, vec);
4279 }
4280 }
4281 else
4282 {
4283 t = DECL_INITIAL (t);
4284
4285 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4286 {
4287 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4288 with a single element, which expands T. */
4289 tree vec = make_tree_vec (1);
4290 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4291 if (CHECKING_P)
4292 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4293
4294 t = convert_from_reference (t);
4295 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4296
4297 t = make_node (NONTYPE_ARGUMENT_PACK);
4298 SET_ARGUMENT_PACK_ARGS (t, vec);
4299 TREE_TYPE (t) = type;
4300 }
4301 else
4302 t = convert_from_reference (t);
4303 }
4304 return t;
4305 }
4306
4307 /* Given a single level of template parameters (a TREE_VEC), return it
4308 as a set of template arguments. */
4309
4310 static tree
4311 template_parms_level_to_args (tree parms)
4312 {
4313 tree a = copy_node (parms);
4314 TREE_TYPE (a) = NULL_TREE;
4315 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4316 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4317
4318 if (CHECKING_P)
4319 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4320
4321 return a;
4322 }
4323
4324 /* Given a set of template parameters, return them as a set of template
4325 arguments. The template parameters are represented as a TREE_VEC, in
4326 the form documented in cp-tree.h for template arguments. */
4327
4328 static tree
4329 template_parms_to_args (tree parms)
4330 {
4331 tree header;
4332 tree args = NULL_TREE;
4333 int length = TMPL_PARMS_DEPTH (parms);
4334 int l = length;
4335
4336 /* If there is only one level of template parameters, we do not
4337 create a TREE_VEC of TREE_VECs. Instead, we return a single
4338 TREE_VEC containing the arguments. */
4339 if (length > 1)
4340 args = make_tree_vec (length);
4341
4342 for (header = parms; header; header = TREE_CHAIN (header))
4343 {
4344 tree a = template_parms_level_to_args (TREE_VALUE (header));
4345
4346 if (length > 1)
4347 TREE_VEC_ELT (args, --l) = a;
4348 else
4349 args = a;
4350 }
4351
4352 return args;
4353 }
4354
4355 /* Within the declaration of a template, return the currently active
4356 template parameters as an argument TREE_VEC. */
4357
4358 static tree
4359 current_template_args (void)
4360 {
4361 return template_parms_to_args (current_template_parms);
4362 }
4363
4364 /* Update the declared TYPE by doing any lookups which were thought to be
4365 dependent, but are not now that we know the SCOPE of the declarator. */
4366
4367 tree
4368 maybe_update_decl_type (tree orig_type, tree scope)
4369 {
4370 tree type = orig_type;
4371
4372 if (type == NULL_TREE)
4373 return type;
4374
4375 if (TREE_CODE (orig_type) == TYPE_DECL)
4376 type = TREE_TYPE (type);
4377
4378 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4379 && dependent_type_p (type)
4380 /* Don't bother building up the args in this case. */
4381 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4382 {
4383 /* tsubst in the args corresponding to the template parameters,
4384 including auto if present. Most things will be unchanged, but
4385 make_typename_type and tsubst_qualified_id will resolve
4386 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4387 tree args = current_template_args ();
4388 tree auto_node = type_uses_auto (type);
4389 tree pushed;
4390 if (auto_node)
4391 {
4392 tree auto_vec = make_tree_vec (1);
4393 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4394 args = add_to_template_args (args, auto_vec);
4395 }
4396 pushed = push_scope (scope);
4397 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4398 if (pushed)
4399 pop_scope (scope);
4400 }
4401
4402 if (type == error_mark_node)
4403 return orig_type;
4404
4405 if (TREE_CODE (orig_type) == TYPE_DECL)
4406 {
4407 if (same_type_p (type, TREE_TYPE (orig_type)))
4408 type = orig_type;
4409 else
4410 type = TYPE_NAME (type);
4411 }
4412 return type;
4413 }
4414
4415 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4416 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4417 the new template is a member template. */
4418
4419 tree
4420 build_template_decl (tree decl, tree parms, bool member_template_p)
4421 {
4422 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4423 DECL_TEMPLATE_PARMS (tmpl) = parms;
4424 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4425 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4426 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4427
4428 return tmpl;
4429 }
4430
4431 struct template_parm_data
4432 {
4433 /* The level of the template parameters we are currently
4434 processing. */
4435 int level;
4436
4437 /* The index of the specialization argument we are currently
4438 processing. */
4439 int current_arg;
4440
4441 /* An array whose size is the number of template parameters. The
4442 elements are nonzero if the parameter has been used in any one
4443 of the arguments processed so far. */
4444 int* parms;
4445
4446 /* An array whose size is the number of template arguments. The
4447 elements are nonzero if the argument makes use of template
4448 parameters of this level. */
4449 int* arg_uses_template_parms;
4450 };
4451
4452 /* Subroutine of push_template_decl used to see if each template
4453 parameter in a partial specialization is used in the explicit
4454 argument list. If T is of the LEVEL given in DATA (which is
4455 treated as a template_parm_data*), then DATA->PARMS is marked
4456 appropriately. */
4457
4458 static int
4459 mark_template_parm (tree t, void* data)
4460 {
4461 int level;
4462 int idx;
4463 struct template_parm_data* tpd = (struct template_parm_data*) data;
4464
4465 template_parm_level_and_index (t, &level, &idx);
4466
4467 if (level == tpd->level)
4468 {
4469 tpd->parms[idx] = 1;
4470 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4471 }
4472
4473 /* In C++17 the type of a non-type argument is a deduced context. */
4474 if (cxx_dialect >= cxx1z
4475 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4476 for_each_template_parm (TREE_TYPE (t),
4477 &mark_template_parm,
4478 data,
4479 NULL,
4480 /*include_nondeduced_p=*/false);
4481
4482 /* Return zero so that for_each_template_parm will continue the
4483 traversal of the tree; we want to mark *every* template parm. */
4484 return 0;
4485 }
4486
4487 /* Process the partial specialization DECL. */
4488
4489 static tree
4490 process_partial_specialization (tree decl)
4491 {
4492 tree type = TREE_TYPE (decl);
4493 tree tinfo = get_template_info (decl);
4494 tree maintmpl = TI_TEMPLATE (tinfo);
4495 tree specargs = TI_ARGS (tinfo);
4496 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4497 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4498 tree inner_parms;
4499 tree inst;
4500 int nargs = TREE_VEC_LENGTH (inner_args);
4501 int ntparms;
4502 int i;
4503 bool did_error_intro = false;
4504 struct template_parm_data tpd;
4505 struct template_parm_data tpd2;
4506
4507 gcc_assert (current_template_parms);
4508
4509 /* A concept cannot be specialized. */
4510 if (flag_concepts && variable_concept_p (maintmpl))
4511 {
4512 error ("specialization of variable concept %q#D", maintmpl);
4513 return error_mark_node;
4514 }
4515
4516 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4517 ntparms = TREE_VEC_LENGTH (inner_parms);
4518
4519 /* We check that each of the template parameters given in the
4520 partial specialization is used in the argument list to the
4521 specialization. For example:
4522
4523 template <class T> struct S;
4524 template <class T> struct S<T*>;
4525
4526 The second declaration is OK because `T*' uses the template
4527 parameter T, whereas
4528
4529 template <class T> struct S<int>;
4530
4531 is no good. Even trickier is:
4532
4533 template <class T>
4534 struct S1
4535 {
4536 template <class U>
4537 struct S2;
4538 template <class U>
4539 struct S2<T>;
4540 };
4541
4542 The S2<T> declaration is actually invalid; it is a
4543 full-specialization. Of course,
4544
4545 template <class U>
4546 struct S2<T (*)(U)>;
4547
4548 or some such would have been OK. */
4549 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4550 tpd.parms = XALLOCAVEC (int, ntparms);
4551 memset (tpd.parms, 0, sizeof (int) * ntparms);
4552
4553 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4554 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4555 for (i = 0; i < nargs; ++i)
4556 {
4557 tpd.current_arg = i;
4558 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4559 &mark_template_parm,
4560 &tpd,
4561 NULL,
4562 /*include_nondeduced_p=*/false);
4563 }
4564 for (i = 0; i < ntparms; ++i)
4565 if (tpd.parms[i] == 0)
4566 {
4567 /* One of the template parms was not used in a deduced context in the
4568 specialization. */
4569 if (!did_error_intro)
4570 {
4571 error ("template parameters not deducible in "
4572 "partial specialization:");
4573 did_error_intro = true;
4574 }
4575
4576 inform (input_location, " %qD",
4577 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4578 }
4579
4580 if (did_error_intro)
4581 return error_mark_node;
4582
4583 /* [temp.class.spec]
4584
4585 The argument list of the specialization shall not be identical to
4586 the implicit argument list of the primary template. */
4587 tree main_args
4588 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4589 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4590 && (!flag_concepts
4591 || !strictly_subsumes (current_template_constraints (),
4592 get_constraints (maintmpl))))
4593 {
4594 if (!flag_concepts)
4595 error ("partial specialization %q+D does not specialize "
4596 "any template arguments", decl);
4597 else
4598 error ("partial specialization %q+D does not specialize any "
4599 "template arguments and is not more constrained than", decl);
4600 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4601 }
4602
4603 /* A partial specialization that replaces multiple parameters of the
4604 primary template with a pack expansion is less specialized for those
4605 parameters. */
4606 if (nargs < DECL_NTPARMS (maintmpl))
4607 {
4608 error ("partial specialization is not more specialized than the "
4609 "primary template because it replaces multiple parameters "
4610 "with a pack expansion");
4611 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4612 /* Avoid crash in process_partial_specialization. */
4613 return decl;
4614 }
4615
4616 /* If we aren't in a dependent class, we can actually try deduction. */
4617 else if (tpd.level == 1
4618 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4619 {
4620 if (permerror (input_location, "partial specialization %qD is not "
4621 "more specialized than", decl))
4622 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4623 maintmpl);
4624 }
4625
4626 /* [temp.class.spec]
4627
4628 A partially specialized non-type argument expression shall not
4629 involve template parameters of the partial specialization except
4630 when the argument expression is a simple identifier.
4631
4632 The type of a template parameter corresponding to a specialized
4633 non-type argument shall not be dependent on a parameter of the
4634 specialization.
4635
4636 Also, we verify that pack expansions only occur at the
4637 end of the argument list. */
4638 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4639 tpd2.parms = 0;
4640 for (i = 0; i < nargs; ++i)
4641 {
4642 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4643 tree arg = TREE_VEC_ELT (inner_args, i);
4644 tree packed_args = NULL_TREE;
4645 int j, len = 1;
4646
4647 if (ARGUMENT_PACK_P (arg))
4648 {
4649 /* Extract the arguments from the argument pack. We'll be
4650 iterating over these in the following loop. */
4651 packed_args = ARGUMENT_PACK_ARGS (arg);
4652 len = TREE_VEC_LENGTH (packed_args);
4653 }
4654
4655 for (j = 0; j < len; j++)
4656 {
4657 if (packed_args)
4658 /* Get the Jth argument in the parameter pack. */
4659 arg = TREE_VEC_ELT (packed_args, j);
4660
4661 if (PACK_EXPANSION_P (arg))
4662 {
4663 /* Pack expansions must come at the end of the
4664 argument list. */
4665 if ((packed_args && j < len - 1)
4666 || (!packed_args && i < nargs - 1))
4667 {
4668 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4669 error ("parameter pack argument %qE must be at the "
4670 "end of the template argument list", arg);
4671 else
4672 error ("parameter pack argument %qT must be at the "
4673 "end of the template argument list", arg);
4674 }
4675 }
4676
4677 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4678 /* We only care about the pattern. */
4679 arg = PACK_EXPANSION_PATTERN (arg);
4680
4681 if (/* These first two lines are the `non-type' bit. */
4682 !TYPE_P (arg)
4683 && TREE_CODE (arg) != TEMPLATE_DECL
4684 /* This next two lines are the `argument expression is not just a
4685 simple identifier' condition and also the `specialized
4686 non-type argument' bit. */
4687 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4688 && !(REFERENCE_REF_P (arg)
4689 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4690 {
4691 if ((!packed_args && tpd.arg_uses_template_parms[i])
4692 || (packed_args && uses_template_parms (arg)))
4693 error ("template argument %qE involves template parameter(s)",
4694 arg);
4695 else
4696 {
4697 /* Look at the corresponding template parameter,
4698 marking which template parameters its type depends
4699 upon. */
4700 tree type = TREE_TYPE (parm);
4701
4702 if (!tpd2.parms)
4703 {
4704 /* We haven't yet initialized TPD2. Do so now. */
4705 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4706 /* The number of parameters here is the number in the
4707 main template, which, as checked in the assertion
4708 above, is NARGS. */
4709 tpd2.parms = XALLOCAVEC (int, nargs);
4710 tpd2.level =
4711 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4712 }
4713
4714 /* Mark the template parameters. But this time, we're
4715 looking for the template parameters of the main
4716 template, not in the specialization. */
4717 tpd2.current_arg = i;
4718 tpd2.arg_uses_template_parms[i] = 0;
4719 memset (tpd2.parms, 0, sizeof (int) * nargs);
4720 for_each_template_parm (type,
4721 &mark_template_parm,
4722 &tpd2,
4723 NULL,
4724 /*include_nondeduced_p=*/false);
4725
4726 if (tpd2.arg_uses_template_parms [i])
4727 {
4728 /* The type depended on some template parameters.
4729 If they are fully specialized in the
4730 specialization, that's OK. */
4731 int j;
4732 int count = 0;
4733 for (j = 0; j < nargs; ++j)
4734 if (tpd2.parms[j] != 0
4735 && tpd.arg_uses_template_parms [j])
4736 ++count;
4737 if (count != 0)
4738 error_n (input_location, count,
4739 "type %qT of template argument %qE depends "
4740 "on a template parameter",
4741 "type %qT of template argument %qE depends "
4742 "on template parameters",
4743 type,
4744 arg);
4745 }
4746 }
4747 }
4748 }
4749 }
4750
4751 /* We should only get here once. */
4752 if (TREE_CODE (decl) == TYPE_DECL)
4753 gcc_assert (!COMPLETE_TYPE_P (type));
4754
4755 // Build the template decl.
4756 tree tmpl = build_template_decl (decl, current_template_parms,
4757 DECL_MEMBER_TEMPLATE_P (maintmpl));
4758 TREE_TYPE (tmpl) = type;
4759 DECL_TEMPLATE_RESULT (tmpl) = decl;
4760 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4761 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4762 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4763
4764 /* Give template template parms a DECL_CONTEXT of the template
4765 for which they are a parameter. */
4766 for (i = 0; i < ntparms; ++i)
4767 {
4768 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4769 if (TREE_CODE (parm) == TEMPLATE_DECL)
4770 DECL_CONTEXT (parm) = tmpl;
4771 }
4772
4773 if (VAR_P (decl))
4774 /* We didn't register this in check_explicit_specialization so we could
4775 wait until the constraints were set. */
4776 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4777 else
4778 associate_classtype_constraints (type);
4779
4780 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4781 = tree_cons (specargs, tmpl,
4782 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4783 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4784
4785 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4786 inst = TREE_CHAIN (inst))
4787 {
4788 tree instance = TREE_VALUE (inst);
4789 if (TYPE_P (instance)
4790 ? (COMPLETE_TYPE_P (instance)
4791 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4792 : DECL_TEMPLATE_INSTANTIATION (instance))
4793 {
4794 tree spec = most_specialized_partial_spec (instance, tf_none);
4795 tree inst_decl = (DECL_P (instance)
4796 ? instance : TYPE_NAME (instance));
4797 if (!spec)
4798 /* OK */;
4799 else if (spec == error_mark_node)
4800 permerror (input_location,
4801 "declaration of %qD ambiguates earlier template "
4802 "instantiation for %qD", decl, inst_decl);
4803 else if (TREE_VALUE (spec) == tmpl)
4804 permerror (input_location,
4805 "partial specialization of %qD after instantiation "
4806 "of %qD", decl, inst_decl);
4807 }
4808 }
4809
4810 return decl;
4811 }
4812
4813 /* PARM is a template parameter of some form; return the corresponding
4814 TEMPLATE_PARM_INDEX. */
4815
4816 static tree
4817 get_template_parm_index (tree parm)
4818 {
4819 if (TREE_CODE (parm) == PARM_DECL
4820 || TREE_CODE (parm) == CONST_DECL)
4821 parm = DECL_INITIAL (parm);
4822 else if (TREE_CODE (parm) == TYPE_DECL
4823 || TREE_CODE (parm) == TEMPLATE_DECL)
4824 parm = TREE_TYPE (parm);
4825 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4826 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4827 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4828 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4829 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4830 return parm;
4831 }
4832
4833 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4834 parameter packs used by the template parameter PARM. */
4835
4836 static void
4837 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4838 {
4839 /* A type parm can't refer to another parm. */
4840 if (TREE_CODE (parm) == TYPE_DECL)
4841 return;
4842 else if (TREE_CODE (parm) == PARM_DECL)
4843 {
4844 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4845 ppd, ppd->visited);
4846 return;
4847 }
4848
4849 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4850
4851 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4852 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4853 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4854 }
4855
4856 /* PARM is a template parameter pack. Return any parameter packs used in
4857 its type or the type of any of its template parameters. If there are
4858 any such packs, it will be instantiated into a fixed template parameter
4859 list by partial instantiation rather than be fully deduced. */
4860
4861 tree
4862 fixed_parameter_pack_p (tree parm)
4863 {
4864 /* This can only be true in a member template. */
4865 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4866 return NULL_TREE;
4867 /* This can only be true for a parameter pack. */
4868 if (!template_parameter_pack_p (parm))
4869 return NULL_TREE;
4870 /* A type parm can't refer to another parm. */
4871 if (TREE_CODE (parm) == TYPE_DECL)
4872 return NULL_TREE;
4873
4874 tree parameter_packs = NULL_TREE;
4875 struct find_parameter_pack_data ppd;
4876 ppd.parameter_packs = &parameter_packs;
4877 ppd.visited = new hash_set<tree>;
4878 ppd.type_pack_expansion_p = false;
4879
4880 fixed_parameter_pack_p_1 (parm, &ppd);
4881
4882 delete ppd.visited;
4883 return parameter_packs;
4884 }
4885
4886 /* Check that a template declaration's use of default arguments and
4887 parameter packs is not invalid. Here, PARMS are the template
4888 parameters. IS_PRIMARY is true if DECL is the thing declared by
4889 a primary template. IS_PARTIAL is true if DECL is a partial
4890 specialization.
4891
4892 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4893 declaration (but not a definition); 1 indicates a declaration, 2
4894 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4895 emitted for extraneous default arguments.
4896
4897 Returns TRUE if there were no errors found, FALSE otherwise. */
4898
4899 bool
4900 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4901 bool is_partial, int is_friend_decl)
4902 {
4903 const char *msg;
4904 int last_level_to_check;
4905 tree parm_level;
4906 bool no_errors = true;
4907
4908 /* [temp.param]
4909
4910 A default template-argument shall not be specified in a
4911 function template declaration or a function template definition, nor
4912 in the template-parameter-list of the definition of a member of a
4913 class template. */
4914
4915 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4916 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4917 /* You can't have a function template declaration in a local
4918 scope, nor you can you define a member of a class template in a
4919 local scope. */
4920 return true;
4921
4922 if ((TREE_CODE (decl) == TYPE_DECL
4923 && TREE_TYPE (decl)
4924 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4925 || (TREE_CODE (decl) == FUNCTION_DECL
4926 && LAMBDA_FUNCTION_P (decl)))
4927 /* A lambda doesn't have an explicit declaration; don't complain
4928 about the parms of the enclosing class. */
4929 return true;
4930
4931 if (current_class_type
4932 && !TYPE_BEING_DEFINED (current_class_type)
4933 && DECL_LANG_SPECIFIC (decl)
4934 && DECL_DECLARES_FUNCTION_P (decl)
4935 /* If this is either a friend defined in the scope of the class
4936 or a member function. */
4937 && (DECL_FUNCTION_MEMBER_P (decl)
4938 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4939 : DECL_FRIEND_CONTEXT (decl)
4940 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4941 : false)
4942 /* And, if it was a member function, it really was defined in
4943 the scope of the class. */
4944 && (!DECL_FUNCTION_MEMBER_P (decl)
4945 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4946 /* We already checked these parameters when the template was
4947 declared, so there's no need to do it again now. This function
4948 was defined in class scope, but we're processing its body now
4949 that the class is complete. */
4950 return true;
4951
4952 /* Core issue 226 (C++0x only): the following only applies to class
4953 templates. */
4954 if (is_primary
4955 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4956 {
4957 /* [temp.param]
4958
4959 If a template-parameter has a default template-argument, all
4960 subsequent template-parameters shall have a default
4961 template-argument supplied. */
4962 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4963 {
4964 tree inner_parms = TREE_VALUE (parm_level);
4965 int ntparms = TREE_VEC_LENGTH (inner_parms);
4966 int seen_def_arg_p = 0;
4967 int i;
4968
4969 for (i = 0; i < ntparms; ++i)
4970 {
4971 tree parm = TREE_VEC_ELT (inner_parms, i);
4972
4973 if (parm == error_mark_node)
4974 continue;
4975
4976 if (TREE_PURPOSE (parm))
4977 seen_def_arg_p = 1;
4978 else if (seen_def_arg_p
4979 && !template_parameter_pack_p (TREE_VALUE (parm)))
4980 {
4981 error ("no default argument for %qD", TREE_VALUE (parm));
4982 /* For better subsequent error-recovery, we indicate that
4983 there should have been a default argument. */
4984 TREE_PURPOSE (parm) = error_mark_node;
4985 no_errors = false;
4986 }
4987 else if (!is_partial
4988 && !is_friend_decl
4989 /* Don't complain about an enclosing partial
4990 specialization. */
4991 && parm_level == parms
4992 && TREE_CODE (decl) == TYPE_DECL
4993 && i < ntparms - 1
4994 && template_parameter_pack_p (TREE_VALUE (parm))
4995 /* A fixed parameter pack will be partially
4996 instantiated into a fixed length list. */
4997 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4998 {
4999 /* A primary class template can only have one
5000 parameter pack, at the end of the template
5001 parameter list. */
5002
5003 error ("parameter pack %q+D must be at the end of the"
5004 " template parameter list", TREE_VALUE (parm));
5005
5006 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5007 = error_mark_node;
5008 no_errors = false;
5009 }
5010 }
5011 }
5012 }
5013
5014 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5015 || is_partial
5016 || !is_primary
5017 || is_friend_decl)
5018 /* For an ordinary class template, default template arguments are
5019 allowed at the innermost level, e.g.:
5020 template <class T = int>
5021 struct S {};
5022 but, in a partial specialization, they're not allowed even
5023 there, as we have in [temp.class.spec]:
5024
5025 The template parameter list of a specialization shall not
5026 contain default template argument values.
5027
5028 So, for a partial specialization, or for a function template
5029 (in C++98/C++03), we look at all of them. */
5030 ;
5031 else
5032 /* But, for a primary class template that is not a partial
5033 specialization we look at all template parameters except the
5034 innermost ones. */
5035 parms = TREE_CHAIN (parms);
5036
5037 /* Figure out what error message to issue. */
5038 if (is_friend_decl == 2)
5039 msg = G_("default template arguments may not be used in function template "
5040 "friend re-declaration");
5041 else if (is_friend_decl)
5042 msg = G_("default template arguments may not be used in function template "
5043 "friend declarations");
5044 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5045 msg = G_("default template arguments may not be used in function templates "
5046 "without -std=c++11 or -std=gnu++11");
5047 else if (is_partial)
5048 msg = G_("default template arguments may not be used in "
5049 "partial specializations");
5050 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5051 msg = G_("default argument for template parameter for class enclosing %qD");
5052 else
5053 /* Per [temp.param]/9, "A default template-argument shall not be
5054 specified in the template-parameter-lists of the definition of
5055 a member of a class template that appears outside of the member's
5056 class.", thus if we aren't handling a member of a class template
5057 there is no need to examine the parameters. */
5058 return true;
5059
5060 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5061 /* If we're inside a class definition, there's no need to
5062 examine the parameters to the class itself. On the one
5063 hand, they will be checked when the class is defined, and,
5064 on the other, default arguments are valid in things like:
5065 template <class T = double>
5066 struct S { template <class U> void f(U); };
5067 Here the default argument for `S' has no bearing on the
5068 declaration of `f'. */
5069 last_level_to_check = template_class_depth (current_class_type) + 1;
5070 else
5071 /* Check everything. */
5072 last_level_to_check = 0;
5073
5074 for (parm_level = parms;
5075 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5076 parm_level = TREE_CHAIN (parm_level))
5077 {
5078 tree inner_parms = TREE_VALUE (parm_level);
5079 int i;
5080 int ntparms;
5081
5082 ntparms = TREE_VEC_LENGTH (inner_parms);
5083 for (i = 0; i < ntparms; ++i)
5084 {
5085 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5086 continue;
5087
5088 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5089 {
5090 if (msg)
5091 {
5092 no_errors = false;
5093 if (is_friend_decl == 2)
5094 return no_errors;
5095
5096 error (msg, decl);
5097 msg = 0;
5098 }
5099
5100 /* Clear out the default argument so that we are not
5101 confused later. */
5102 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5103 }
5104 }
5105
5106 /* At this point, if we're still interested in issuing messages,
5107 they must apply to classes surrounding the object declared. */
5108 if (msg)
5109 msg = G_("default argument for template parameter for class "
5110 "enclosing %qD");
5111 }
5112
5113 return no_errors;
5114 }
5115
5116 /* Worker for push_template_decl_real, called via
5117 for_each_template_parm. DATA is really an int, indicating the
5118 level of the parameters we are interested in. If T is a template
5119 parameter of that level, return nonzero. */
5120
5121 static int
5122 template_parm_this_level_p (tree t, void* data)
5123 {
5124 int this_level = *(int *)data;
5125 int level;
5126
5127 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5128 level = TEMPLATE_PARM_LEVEL (t);
5129 else
5130 level = TEMPLATE_TYPE_LEVEL (t);
5131 return level == this_level;
5132 }
5133
5134 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5135 DATA is really an int, indicating the innermost outer level of parameters.
5136 If T is a template parameter of that level or further out, return
5137 nonzero. */
5138
5139 static int
5140 template_parm_outer_level (tree t, void *data)
5141 {
5142 int this_level = *(int *)data;
5143 int level;
5144
5145 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5146 level = TEMPLATE_PARM_LEVEL (t);
5147 else
5148 level = TEMPLATE_TYPE_LEVEL (t);
5149 return level <= this_level;
5150 }
5151
5152 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5153 parameters given by current_template_args, or reuses a
5154 previously existing one, if appropriate. Returns the DECL, or an
5155 equivalent one, if it is replaced via a call to duplicate_decls.
5156
5157 If IS_FRIEND is true, DECL is a friend declaration. */
5158
5159 tree
5160 push_template_decl_real (tree decl, bool is_friend)
5161 {
5162 tree tmpl;
5163 tree args;
5164 tree info;
5165 tree ctx;
5166 bool is_primary;
5167 bool is_partial;
5168 int new_template_p = 0;
5169 /* True if the template is a member template, in the sense of
5170 [temp.mem]. */
5171 bool member_template_p = false;
5172
5173 if (decl == error_mark_node || !current_template_parms)
5174 return error_mark_node;
5175
5176 /* See if this is a partial specialization. */
5177 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5178 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5179 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5180 || (VAR_P (decl)
5181 && DECL_LANG_SPECIFIC (decl)
5182 && DECL_TEMPLATE_SPECIALIZATION (decl)
5183 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5184
5185 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5186 is_friend = true;
5187
5188 if (is_friend)
5189 /* For a friend, we want the context of the friend function, not
5190 the type of which it is a friend. */
5191 ctx = CP_DECL_CONTEXT (decl);
5192 else if (CP_DECL_CONTEXT (decl)
5193 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5194 /* In the case of a virtual function, we want the class in which
5195 it is defined. */
5196 ctx = CP_DECL_CONTEXT (decl);
5197 else
5198 /* Otherwise, if we're currently defining some class, the DECL
5199 is assumed to be a member of the class. */
5200 ctx = current_scope ();
5201
5202 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5203 ctx = NULL_TREE;
5204
5205 if (!DECL_CONTEXT (decl))
5206 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5207
5208 /* See if this is a primary template. */
5209 if (is_friend && ctx
5210 && uses_template_parms_level (ctx, processing_template_decl))
5211 /* A friend template that specifies a class context, i.e.
5212 template <typename T> friend void A<T>::f();
5213 is not primary. */
5214 is_primary = false;
5215 else if (TREE_CODE (decl) == TYPE_DECL
5216 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5217 is_primary = false;
5218 else
5219 is_primary = template_parm_scope_p ();
5220
5221 if (is_primary)
5222 {
5223 warning (OPT_Wtemplates, "template %qD declared", decl);
5224
5225 if (DECL_CLASS_SCOPE_P (decl))
5226 member_template_p = true;
5227 if (TREE_CODE (decl) == TYPE_DECL
5228 && anon_aggrname_p (DECL_NAME (decl)))
5229 {
5230 error ("template class without a name");
5231 return error_mark_node;
5232 }
5233 else if (TREE_CODE (decl) == FUNCTION_DECL)
5234 {
5235 if (member_template_p)
5236 {
5237 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5238 error ("member template %qD may not have virt-specifiers", decl);
5239 }
5240 if (DECL_DESTRUCTOR_P (decl))
5241 {
5242 /* [temp.mem]
5243
5244 A destructor shall not be a member template. */
5245 error ("destructor %qD declared as member template", decl);
5246 return error_mark_node;
5247 }
5248 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5249 && (!prototype_p (TREE_TYPE (decl))
5250 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5251 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5252 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5253 == void_list_node)))
5254 {
5255 /* [basic.stc.dynamic.allocation]
5256
5257 An allocation function can be a function
5258 template. ... Template allocation functions shall
5259 have two or more parameters. */
5260 error ("invalid template declaration of %qD", decl);
5261 return error_mark_node;
5262 }
5263 }
5264 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5265 && CLASS_TYPE_P (TREE_TYPE (decl)))
5266 {
5267 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5268 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5269 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5270 {
5271 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5272 if (TREE_CODE (t) == TYPE_DECL)
5273 t = TREE_TYPE (t);
5274 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5275 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5276 }
5277 }
5278 else if (TREE_CODE (decl) == TYPE_DECL
5279 && TYPE_DECL_ALIAS_P (decl))
5280 /* alias-declaration */
5281 gcc_assert (!DECL_ARTIFICIAL (decl));
5282 else if (VAR_P (decl))
5283 /* C++14 variable template. */;
5284 else
5285 {
5286 error ("template declaration of %q#D", decl);
5287 return error_mark_node;
5288 }
5289 }
5290
5291 /* Check to see that the rules regarding the use of default
5292 arguments are not being violated. */
5293 check_default_tmpl_args (decl, current_template_parms,
5294 is_primary, is_partial, /*is_friend_decl=*/0);
5295
5296 /* Ensure that there are no parameter packs in the type of this
5297 declaration that have not been expanded. */
5298 if (TREE_CODE (decl) == FUNCTION_DECL)
5299 {
5300 /* Check each of the arguments individually to see if there are
5301 any bare parameter packs. */
5302 tree type = TREE_TYPE (decl);
5303 tree arg = DECL_ARGUMENTS (decl);
5304 tree argtype = TYPE_ARG_TYPES (type);
5305
5306 while (arg && argtype)
5307 {
5308 if (!DECL_PACK_P (arg)
5309 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5310 {
5311 /* This is a PARM_DECL that contains unexpanded parameter
5312 packs. We have already complained about this in the
5313 check_for_bare_parameter_packs call, so just replace
5314 these types with ERROR_MARK_NODE. */
5315 TREE_TYPE (arg) = error_mark_node;
5316 TREE_VALUE (argtype) = error_mark_node;
5317 }
5318
5319 arg = DECL_CHAIN (arg);
5320 argtype = TREE_CHAIN (argtype);
5321 }
5322
5323 /* Check for bare parameter packs in the return type and the
5324 exception specifiers. */
5325 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5326 /* Errors were already issued, set return type to int
5327 as the frontend doesn't expect error_mark_node as
5328 the return type. */
5329 TREE_TYPE (type) = integer_type_node;
5330 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5331 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5332 }
5333 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5334 && TYPE_DECL_ALIAS_P (decl))
5335 ? DECL_ORIGINAL_TYPE (decl)
5336 : TREE_TYPE (decl)))
5337 {
5338 TREE_TYPE (decl) = error_mark_node;
5339 return error_mark_node;
5340 }
5341
5342 if (is_partial)
5343 return process_partial_specialization (decl);
5344
5345 args = current_template_args ();
5346
5347 if (!ctx
5348 || TREE_CODE (ctx) == FUNCTION_DECL
5349 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5350 || (TREE_CODE (decl) == TYPE_DECL
5351 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5352 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5353 {
5354 if (DECL_LANG_SPECIFIC (decl)
5355 && DECL_TEMPLATE_INFO (decl)
5356 && DECL_TI_TEMPLATE (decl))
5357 tmpl = DECL_TI_TEMPLATE (decl);
5358 /* If DECL is a TYPE_DECL for a class-template, then there won't
5359 be DECL_LANG_SPECIFIC. The information equivalent to
5360 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5361 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5362 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5363 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5364 {
5365 /* Since a template declaration already existed for this
5366 class-type, we must be redeclaring it here. Make sure
5367 that the redeclaration is valid. */
5368 redeclare_class_template (TREE_TYPE (decl),
5369 current_template_parms,
5370 current_template_constraints ());
5371 /* We don't need to create a new TEMPLATE_DECL; just use the
5372 one we already had. */
5373 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5374 }
5375 else
5376 {
5377 tmpl = build_template_decl (decl, current_template_parms,
5378 member_template_p);
5379 new_template_p = 1;
5380
5381 if (DECL_LANG_SPECIFIC (decl)
5382 && DECL_TEMPLATE_SPECIALIZATION (decl))
5383 {
5384 /* A specialization of a member template of a template
5385 class. */
5386 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5387 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5388 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5389 }
5390 }
5391 }
5392 else
5393 {
5394 tree a, t, current, parms;
5395 int i;
5396 tree tinfo = get_template_info (decl);
5397
5398 if (!tinfo)
5399 {
5400 error ("template definition of non-template %q#D", decl);
5401 return error_mark_node;
5402 }
5403
5404 tmpl = TI_TEMPLATE (tinfo);
5405
5406 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5407 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5408 && DECL_TEMPLATE_SPECIALIZATION (decl)
5409 && DECL_MEMBER_TEMPLATE_P (tmpl))
5410 {
5411 tree new_tmpl;
5412
5413 /* The declaration is a specialization of a member
5414 template, declared outside the class. Therefore, the
5415 innermost template arguments will be NULL, so we
5416 replace them with the arguments determined by the
5417 earlier call to check_explicit_specialization. */
5418 args = DECL_TI_ARGS (decl);
5419
5420 new_tmpl
5421 = build_template_decl (decl, current_template_parms,
5422 member_template_p);
5423 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5424 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5425 DECL_TI_TEMPLATE (decl) = new_tmpl;
5426 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5427 DECL_TEMPLATE_INFO (new_tmpl)
5428 = build_template_info (tmpl, args);
5429
5430 register_specialization (new_tmpl,
5431 most_general_template (tmpl),
5432 args,
5433 is_friend, 0);
5434 return decl;
5435 }
5436
5437 /* Make sure the template headers we got make sense. */
5438
5439 parms = DECL_TEMPLATE_PARMS (tmpl);
5440 i = TMPL_PARMS_DEPTH (parms);
5441 if (TMPL_ARGS_DEPTH (args) != i)
5442 {
5443 error ("expected %d levels of template parms for %q#D, got %d",
5444 i, decl, TMPL_ARGS_DEPTH (args));
5445 DECL_INTERFACE_KNOWN (decl) = 1;
5446 return error_mark_node;
5447 }
5448 else
5449 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5450 {
5451 a = TMPL_ARGS_LEVEL (args, i);
5452 t = INNERMOST_TEMPLATE_PARMS (parms);
5453
5454 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5455 {
5456 if (current == decl)
5457 error ("got %d template parameters for %q#D",
5458 TREE_VEC_LENGTH (a), decl);
5459 else
5460 error ("got %d template parameters for %q#T",
5461 TREE_VEC_LENGTH (a), current);
5462 error (" but %d required", TREE_VEC_LENGTH (t));
5463 /* Avoid crash in import_export_decl. */
5464 DECL_INTERFACE_KNOWN (decl) = 1;
5465 return error_mark_node;
5466 }
5467
5468 if (current == decl)
5469 current = ctx;
5470 else if (current == NULL_TREE)
5471 /* Can happen in erroneous input. */
5472 break;
5473 else
5474 current = get_containing_scope (current);
5475 }
5476
5477 /* Check that the parms are used in the appropriate qualifying scopes
5478 in the declarator. */
5479 if (!comp_template_args
5480 (TI_ARGS (tinfo),
5481 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5482 {
5483 error ("\
5484 template arguments to %qD do not match original template %qD",
5485 decl, DECL_TEMPLATE_RESULT (tmpl));
5486 if (!uses_template_parms (TI_ARGS (tinfo)))
5487 inform (input_location, "use template<> for an explicit specialization");
5488 /* Avoid crash in import_export_decl. */
5489 DECL_INTERFACE_KNOWN (decl) = 1;
5490 return error_mark_node;
5491 }
5492 }
5493
5494 DECL_TEMPLATE_RESULT (tmpl) = decl;
5495 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5496
5497 /* Push template declarations for global functions and types. Note
5498 that we do not try to push a global template friend declared in a
5499 template class; such a thing may well depend on the template
5500 parameters of the class. */
5501 if (new_template_p && !ctx
5502 && !(is_friend && template_class_depth (current_class_type) > 0))
5503 {
5504 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5505 if (tmpl == error_mark_node)
5506 return error_mark_node;
5507
5508 /* Hide template friend classes that haven't been declared yet. */
5509 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5510 {
5511 DECL_ANTICIPATED (tmpl) = 1;
5512 DECL_FRIEND_P (tmpl) = 1;
5513 }
5514 }
5515
5516 if (is_primary)
5517 {
5518 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5519 int i;
5520
5521 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5522 if (DECL_CONV_FN_P (tmpl))
5523 {
5524 int depth = TMPL_PARMS_DEPTH (parms);
5525
5526 /* It is a conversion operator. See if the type converted to
5527 depends on innermost template operands. */
5528
5529 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5530 depth))
5531 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5532 }
5533
5534 /* Give template template parms a DECL_CONTEXT of the template
5535 for which they are a parameter. */
5536 parms = INNERMOST_TEMPLATE_PARMS (parms);
5537 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5538 {
5539 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5540 if (TREE_CODE (parm) == TEMPLATE_DECL)
5541 DECL_CONTEXT (parm) = tmpl;
5542 }
5543
5544 if (TREE_CODE (decl) == TYPE_DECL
5545 && TYPE_DECL_ALIAS_P (decl)
5546 && complex_alias_template_p (tmpl))
5547 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5548 }
5549
5550 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5551 back to its most general template. If TMPL is a specialization,
5552 ARGS may only have the innermost set of arguments. Add the missing
5553 argument levels if necessary. */
5554 if (DECL_TEMPLATE_INFO (tmpl))
5555 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5556
5557 info = build_template_info (tmpl, args);
5558
5559 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5560 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5561 else
5562 {
5563 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5564 retrofit_lang_decl (decl);
5565 if (DECL_LANG_SPECIFIC (decl))
5566 DECL_TEMPLATE_INFO (decl) = info;
5567 }
5568
5569 if (flag_implicit_templates
5570 && !is_friend
5571 && TREE_PUBLIC (decl)
5572 && VAR_OR_FUNCTION_DECL_P (decl))
5573 /* Set DECL_COMDAT on template instantiations; if we force
5574 them to be emitted by explicit instantiation or -frepo,
5575 mark_needed will tell cgraph to do the right thing. */
5576 DECL_COMDAT (decl) = true;
5577
5578 return DECL_TEMPLATE_RESULT (tmpl);
5579 }
5580
5581 tree
5582 push_template_decl (tree decl)
5583 {
5584 return push_template_decl_real (decl, false);
5585 }
5586
5587 /* FN is an inheriting constructor that inherits from the constructor
5588 template INHERITED; turn FN into a constructor template with a matching
5589 template header. */
5590
5591 tree
5592 add_inherited_template_parms (tree fn, tree inherited)
5593 {
5594 tree inner_parms
5595 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5596 inner_parms = copy_node (inner_parms);
5597 tree parms
5598 = tree_cons (size_int (processing_template_decl + 1),
5599 inner_parms, current_template_parms);
5600 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5601 tree args = template_parms_to_args (parms);
5602 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5603 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5604 DECL_TEMPLATE_RESULT (tmpl) = fn;
5605 DECL_ARTIFICIAL (tmpl) = true;
5606 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5607 return tmpl;
5608 }
5609
5610 /* Called when a class template TYPE is redeclared with the indicated
5611 template PARMS, e.g.:
5612
5613 template <class T> struct S;
5614 template <class T> struct S {}; */
5615
5616 bool
5617 redeclare_class_template (tree type, tree parms, tree cons)
5618 {
5619 tree tmpl;
5620 tree tmpl_parms;
5621 int i;
5622
5623 if (!TYPE_TEMPLATE_INFO (type))
5624 {
5625 error ("%qT is not a template type", type);
5626 return false;
5627 }
5628
5629 tmpl = TYPE_TI_TEMPLATE (type);
5630 if (!PRIMARY_TEMPLATE_P (tmpl))
5631 /* The type is nested in some template class. Nothing to worry
5632 about here; there are no new template parameters for the nested
5633 type. */
5634 return true;
5635
5636 if (!parms)
5637 {
5638 error ("template specifiers not specified in declaration of %qD",
5639 tmpl);
5640 return false;
5641 }
5642
5643 parms = INNERMOST_TEMPLATE_PARMS (parms);
5644 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5645
5646 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5647 {
5648 error_n (input_location, TREE_VEC_LENGTH (parms),
5649 "redeclared with %d template parameter",
5650 "redeclared with %d template parameters",
5651 TREE_VEC_LENGTH (parms));
5652 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5653 "previous declaration %qD used %d template parameter",
5654 "previous declaration %qD used %d template parameters",
5655 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5656 return false;
5657 }
5658
5659 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5660 {
5661 tree tmpl_parm;
5662 tree parm;
5663 tree tmpl_default;
5664 tree parm_default;
5665
5666 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5667 || TREE_VEC_ELT (parms, i) == error_mark_node)
5668 continue;
5669
5670 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5671 if (error_operand_p (tmpl_parm))
5672 return false;
5673
5674 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5675 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5676 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5677
5678 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5679 TEMPLATE_DECL. */
5680 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5681 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5682 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5683 || (TREE_CODE (tmpl_parm) != PARM_DECL
5684 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5685 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5686 || (TREE_CODE (tmpl_parm) == PARM_DECL
5687 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5688 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5689 {
5690 error ("template parameter %q+#D", tmpl_parm);
5691 error ("redeclared here as %q#D", parm);
5692 return false;
5693 }
5694
5695 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5696 {
5697 /* We have in [temp.param]:
5698
5699 A template-parameter may not be given default arguments
5700 by two different declarations in the same scope. */
5701 error_at (input_location, "redefinition of default argument for %q#D", parm);
5702 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5703 "original definition appeared here");
5704 return false;
5705 }
5706
5707 if (parm_default != NULL_TREE)
5708 /* Update the previous template parameters (which are the ones
5709 that will really count) with the new default value. */
5710 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5711 else if (tmpl_default != NULL_TREE)
5712 /* Update the new parameters, too; they'll be used as the
5713 parameters for any members. */
5714 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5715
5716 /* Give each template template parm in this redeclaration a
5717 DECL_CONTEXT of the template for which they are a parameter. */
5718 if (TREE_CODE (parm) == TEMPLATE_DECL)
5719 {
5720 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5721 DECL_CONTEXT (parm) = tmpl;
5722 }
5723 }
5724
5725 // Cannot redeclare a class template with a different set of constraints.
5726 if (!equivalent_constraints (get_constraints (tmpl), cons))
5727 {
5728 error_at (input_location, "redeclaration %q#D with different "
5729 "constraints", tmpl);
5730 inform (DECL_SOURCE_LOCATION (tmpl),
5731 "original declaration appeared here");
5732 }
5733
5734 return true;
5735 }
5736
5737 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5738 to be used when the caller has already checked
5739 (processing_template_decl
5740 && !instantiation_dependent_expression_p (expr)
5741 && potential_constant_expression (expr))
5742 and cleared processing_template_decl. */
5743
5744 tree
5745 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5746 {
5747 return tsubst_copy_and_build (expr,
5748 /*args=*/NULL_TREE,
5749 complain,
5750 /*in_decl=*/NULL_TREE,
5751 /*function_p=*/false,
5752 /*integral_constant_expression_p=*/true);
5753 }
5754
5755 /* Simplify EXPR if it is a non-dependent expression. Returns the
5756 (possibly simplified) expression. */
5757
5758 tree
5759 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5760 {
5761 if (expr == NULL_TREE)
5762 return NULL_TREE;
5763
5764 /* If we're in a template, but EXPR isn't value dependent, simplify
5765 it. We're supposed to treat:
5766
5767 template <typename T> void f(T[1 + 1]);
5768 template <typename T> void f(T[2]);
5769
5770 as two declarations of the same function, for example. */
5771 if (processing_template_decl
5772 && potential_nondependent_constant_expression (expr))
5773 {
5774 processing_template_decl_sentinel s;
5775 expr = instantiate_non_dependent_expr_internal (expr, complain);
5776 }
5777 return expr;
5778 }
5779
5780 tree
5781 instantiate_non_dependent_expr (tree expr)
5782 {
5783 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5784 }
5785
5786 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5787 an uninstantiated expression. */
5788
5789 tree
5790 instantiate_non_dependent_or_null (tree expr)
5791 {
5792 if (expr == NULL_TREE)
5793 return NULL_TREE;
5794 if (processing_template_decl)
5795 {
5796 if (!potential_nondependent_constant_expression (expr))
5797 expr = NULL_TREE;
5798 else
5799 {
5800 processing_template_decl_sentinel s;
5801 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5802 }
5803 }
5804 return expr;
5805 }
5806
5807 /* True iff T is a specialization of a variable template. */
5808
5809 bool
5810 variable_template_specialization_p (tree t)
5811 {
5812 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5813 return false;
5814 tree tmpl = DECL_TI_TEMPLATE (t);
5815 return variable_template_p (tmpl);
5816 }
5817
5818 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5819 template declaration, or a TYPE_DECL for an alias declaration. */
5820
5821 bool
5822 alias_type_or_template_p (tree t)
5823 {
5824 if (t == NULL_TREE)
5825 return false;
5826 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5827 || (TYPE_P (t)
5828 && TYPE_NAME (t)
5829 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5830 || DECL_ALIAS_TEMPLATE_P (t));
5831 }
5832
5833 /* Return TRUE iff T is a specialization of an alias template. */
5834
5835 bool
5836 alias_template_specialization_p (const_tree t)
5837 {
5838 /* It's an alias template specialization if it's an alias and its
5839 TYPE_NAME is a specialization of a primary template. */
5840 if (TYPE_ALIAS_P (t))
5841 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5842 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5843
5844 return false;
5845 }
5846
5847 /* An alias template is complex from a SFINAE perspective if a template-id
5848 using that alias can be ill-formed when the expansion is not, as with
5849 the void_t template. We determine this by checking whether the
5850 expansion for the alias template uses all its template parameters. */
5851
5852 struct uses_all_template_parms_data
5853 {
5854 int level;
5855 bool *seen;
5856 };
5857
5858 static int
5859 uses_all_template_parms_r (tree t, void *data_)
5860 {
5861 struct uses_all_template_parms_data &data
5862 = *(struct uses_all_template_parms_data*)data_;
5863 tree idx = get_template_parm_index (t);
5864
5865 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5866 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5867 return 0;
5868 }
5869
5870 static bool
5871 complex_alias_template_p (const_tree tmpl)
5872 {
5873 struct uses_all_template_parms_data data;
5874 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5875 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5876 data.level = TMPL_PARMS_DEPTH (parms);
5877 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5878 data.seen = XALLOCAVEC (bool, len);
5879 for (int i = 0; i < len; ++i)
5880 data.seen[i] = false;
5881
5882 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5883 for (int i = 0; i < len; ++i)
5884 if (!data.seen[i])
5885 return true;
5886 return false;
5887 }
5888
5889 /* Return TRUE iff T is a specialization of a complex alias template with
5890 dependent template-arguments. */
5891
5892 bool
5893 dependent_alias_template_spec_p (const_tree t)
5894 {
5895 if (!alias_template_specialization_p (t))
5896 return false;
5897
5898 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5899 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5900 return false;
5901
5902 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5903 if (!any_dependent_template_arguments_p (args))
5904 return false;
5905
5906 return true;
5907 }
5908
5909 /* Return the number of innermost template parameters in TMPL. */
5910
5911 static int
5912 num_innermost_template_parms (tree tmpl)
5913 {
5914 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5915 return TREE_VEC_LENGTH (parms);
5916 }
5917
5918 /* Return either TMPL or another template that it is equivalent to under DR
5919 1286: An alias that just changes the name of a template is equivalent to
5920 the other template. */
5921
5922 static tree
5923 get_underlying_template (tree tmpl)
5924 {
5925 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5926 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5927 {
5928 /* Determine if the alias is equivalent to an underlying template. */
5929 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5930 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5931 if (!tinfo)
5932 break;
5933
5934 tree underlying = TI_TEMPLATE (tinfo);
5935 if (!PRIMARY_TEMPLATE_P (underlying)
5936 || (num_innermost_template_parms (tmpl)
5937 != num_innermost_template_parms (underlying)))
5938 break;
5939
5940 tree alias_args = INNERMOST_TEMPLATE_ARGS
5941 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5942 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
5943 break;
5944
5945 /* Alias is equivalent. Strip it and repeat. */
5946 tmpl = underlying;
5947 }
5948
5949 return tmpl;
5950 }
5951
5952 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5953 must be a reference-to-function or a pointer-to-function type, as specified
5954 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5955 and check that the resulting function has external linkage. */
5956
5957 static tree
5958 convert_nontype_argument_function (tree type, tree expr,
5959 tsubst_flags_t complain)
5960 {
5961 tree fns = expr;
5962 tree fn, fn_no_ptr;
5963 linkage_kind linkage;
5964
5965 fn = instantiate_type (type, fns, tf_none);
5966 if (fn == error_mark_node)
5967 return error_mark_node;
5968
5969 if (value_dependent_expression_p (fn))
5970 return fn;
5971
5972 fn_no_ptr = strip_fnptr_conv (fn);
5973 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5974 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5975 if (BASELINK_P (fn_no_ptr))
5976 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5977
5978 /* [temp.arg.nontype]/1
5979
5980 A template-argument for a non-type, non-template template-parameter
5981 shall be one of:
5982 [...]
5983 -- the address of an object or function with external [C++11: or
5984 internal] linkage. */
5985
5986 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5987 {
5988 if (complain & tf_error)
5989 {
5990 error ("%qE is not a valid template argument for type %qT",
5991 expr, type);
5992 if (TYPE_PTR_P (type))
5993 inform (input_location, "it must be the address of a function "
5994 "with external linkage");
5995 else
5996 inform (input_location, "it must be the name of a function with "
5997 "external linkage");
5998 }
5999 return NULL_TREE;
6000 }
6001
6002 linkage = decl_linkage (fn_no_ptr);
6003 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6004 {
6005 if (complain & tf_error)
6006 {
6007 if (cxx_dialect >= cxx11)
6008 error ("%qE is not a valid template argument for type %qT "
6009 "because %qD has no linkage",
6010 expr, type, fn_no_ptr);
6011 else
6012 error ("%qE is not a valid template argument for type %qT "
6013 "because %qD does not have external linkage",
6014 expr, type, fn_no_ptr);
6015 }
6016 return NULL_TREE;
6017 }
6018
6019 if (TREE_CODE (type) == REFERENCE_TYPE)
6020 fn = build_address (fn);
6021 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6022 fn = build_nop (type, fn);
6023
6024 return fn;
6025 }
6026
6027 /* Subroutine of convert_nontype_argument.
6028 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6029 Emit an error otherwise. */
6030
6031 static bool
6032 check_valid_ptrmem_cst_expr (tree type, tree expr,
6033 tsubst_flags_t complain)
6034 {
6035 STRIP_NOPS (expr);
6036 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6037 return true;
6038 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6039 return true;
6040 if (processing_template_decl
6041 && TREE_CODE (expr) == ADDR_EXPR
6042 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6043 return true;
6044 if (complain & tf_error)
6045 {
6046 error ("%qE is not a valid template argument for type %qT",
6047 expr, type);
6048 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6049 }
6050 return false;
6051 }
6052
6053 /* Returns TRUE iff the address of OP is value-dependent.
6054
6055 14.6.2.4 [temp.dep.temp]:
6056 A non-integral non-type template-argument is dependent if its type is
6057 dependent or it has either of the following forms
6058 qualified-id
6059 & qualified-id
6060 and contains a nested-name-specifier which specifies a class-name that
6061 names a dependent type.
6062
6063 We generalize this to just say that the address of a member of a
6064 dependent class is value-dependent; the above doesn't cover the
6065 address of a static data member named with an unqualified-id. */
6066
6067 static bool
6068 has_value_dependent_address (tree op)
6069 {
6070 /* We could use get_inner_reference here, but there's no need;
6071 this is only relevant for template non-type arguments, which
6072 can only be expressed as &id-expression. */
6073 if (DECL_P (op))
6074 {
6075 tree ctx = CP_DECL_CONTEXT (op);
6076 if (TYPE_P (ctx) && dependent_type_p (ctx))
6077 return true;
6078 }
6079
6080 return false;
6081 }
6082
6083 /* The next set of functions are used for providing helpful explanatory
6084 diagnostics for failed overload resolution. Their messages should be
6085 indented by two spaces for consistency with the messages in
6086 call.c */
6087
6088 static int
6089 unify_success (bool /*explain_p*/)
6090 {
6091 return 0;
6092 }
6093
6094 static int
6095 unify_parameter_deduction_failure (bool explain_p, tree parm)
6096 {
6097 if (explain_p)
6098 inform (input_location,
6099 " couldn't deduce template parameter %qD", parm);
6100 return 1;
6101 }
6102
6103 static int
6104 unify_invalid (bool /*explain_p*/)
6105 {
6106 return 1;
6107 }
6108
6109 static int
6110 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6111 {
6112 if (explain_p)
6113 inform (input_location,
6114 " types %qT and %qT have incompatible cv-qualifiers",
6115 parm, arg);
6116 return 1;
6117 }
6118
6119 static int
6120 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6121 {
6122 if (explain_p)
6123 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6124 return 1;
6125 }
6126
6127 static int
6128 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6129 {
6130 if (explain_p)
6131 inform (input_location,
6132 " template parameter %qD is not a parameter pack, but "
6133 "argument %qD is",
6134 parm, arg);
6135 return 1;
6136 }
6137
6138 static int
6139 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6140 {
6141 if (explain_p)
6142 inform (input_location,
6143 " template argument %qE does not match "
6144 "pointer-to-member constant %qE",
6145 arg, parm);
6146 return 1;
6147 }
6148
6149 static int
6150 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6151 {
6152 if (explain_p)
6153 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6154 return 1;
6155 }
6156
6157 static int
6158 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6159 {
6160 if (explain_p)
6161 inform (input_location,
6162 " inconsistent parameter pack deduction with %qT and %qT",
6163 old_arg, new_arg);
6164 return 1;
6165 }
6166
6167 static int
6168 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6169 {
6170 if (explain_p)
6171 {
6172 if (TYPE_P (parm))
6173 inform (input_location,
6174 " deduced conflicting types for parameter %qT (%qT and %qT)",
6175 parm, first, second);
6176 else
6177 inform (input_location,
6178 " deduced conflicting values for non-type parameter "
6179 "%qE (%qE and %qE)", parm, first, second);
6180 }
6181 return 1;
6182 }
6183
6184 static int
6185 unify_vla_arg (bool explain_p, tree arg)
6186 {
6187 if (explain_p)
6188 inform (input_location,
6189 " variable-sized array type %qT is not "
6190 "a valid template argument",
6191 arg);
6192 return 1;
6193 }
6194
6195 static int
6196 unify_method_type_error (bool explain_p, tree arg)
6197 {
6198 if (explain_p)
6199 inform (input_location,
6200 " member function type %qT is not a valid template argument",
6201 arg);
6202 return 1;
6203 }
6204
6205 static int
6206 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6207 {
6208 if (explain_p)
6209 {
6210 if (least_p)
6211 inform_n (input_location, wanted,
6212 " candidate expects at least %d argument, %d provided",
6213 " candidate expects at least %d arguments, %d provided",
6214 wanted, have);
6215 else
6216 inform_n (input_location, wanted,
6217 " candidate expects %d argument, %d provided",
6218 " candidate expects %d arguments, %d provided",
6219 wanted, have);
6220 }
6221 return 1;
6222 }
6223
6224 static int
6225 unify_too_many_arguments (bool explain_p, int have, int wanted)
6226 {
6227 return unify_arity (explain_p, have, wanted);
6228 }
6229
6230 static int
6231 unify_too_few_arguments (bool explain_p, int have, int wanted,
6232 bool least_p = false)
6233 {
6234 return unify_arity (explain_p, have, wanted, least_p);
6235 }
6236
6237 static int
6238 unify_arg_conversion (bool explain_p, tree to_type,
6239 tree from_type, tree arg)
6240 {
6241 if (explain_p)
6242 inform (EXPR_LOC_OR_LOC (arg, input_location),
6243 " cannot convert %qE (type %qT) to type %qT",
6244 arg, from_type, to_type);
6245 return 1;
6246 }
6247
6248 static int
6249 unify_no_common_base (bool explain_p, enum template_base_result r,
6250 tree parm, tree arg)
6251 {
6252 if (explain_p)
6253 switch (r)
6254 {
6255 case tbr_ambiguous_baseclass:
6256 inform (input_location, " %qT is an ambiguous base class of %qT",
6257 parm, arg);
6258 break;
6259 default:
6260 inform (input_location, " %qT is not derived from %qT", arg, parm);
6261 break;
6262 }
6263 return 1;
6264 }
6265
6266 static int
6267 unify_inconsistent_template_template_parameters (bool explain_p)
6268 {
6269 if (explain_p)
6270 inform (input_location,
6271 " template parameters of a template template argument are "
6272 "inconsistent with other deduced template arguments");
6273 return 1;
6274 }
6275
6276 static int
6277 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6278 {
6279 if (explain_p)
6280 inform (input_location,
6281 " can't deduce a template for %qT from non-template type %qT",
6282 parm, arg);
6283 return 1;
6284 }
6285
6286 static int
6287 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6288 {
6289 if (explain_p)
6290 inform (input_location,
6291 " template argument %qE does not match %qE", arg, parm);
6292 return 1;
6293 }
6294
6295 static int
6296 unify_overload_resolution_failure (bool explain_p, tree arg)
6297 {
6298 if (explain_p)
6299 inform (input_location,
6300 " could not resolve address from overloaded function %qE",
6301 arg);
6302 return 1;
6303 }
6304
6305 /* Attempt to convert the non-type template parameter EXPR to the
6306 indicated TYPE. If the conversion is successful, return the
6307 converted value. If the conversion is unsuccessful, return
6308 NULL_TREE if we issued an error message, or error_mark_node if we
6309 did not. We issue error messages for out-and-out bad template
6310 parameters, but not simply because the conversion failed, since we
6311 might be just trying to do argument deduction. Both TYPE and EXPR
6312 must be non-dependent.
6313
6314 The conversion follows the special rules described in
6315 [temp.arg.nontype], and it is much more strict than an implicit
6316 conversion.
6317
6318 This function is called twice for each template argument (see
6319 lookup_template_class for a more accurate description of this
6320 problem). This means that we need to handle expressions which
6321 are not valid in a C++ source, but can be created from the
6322 first call (for instance, casts to perform conversions). These
6323 hacks can go away after we fix the double coercion problem. */
6324
6325 static tree
6326 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6327 {
6328 tree expr_type;
6329
6330 /* Detect immediately string literals as invalid non-type argument.
6331 This special-case is not needed for correctness (we would easily
6332 catch this later), but only to provide better diagnostic for this
6333 common user mistake. As suggested by DR 100, we do not mention
6334 linkage issues in the diagnostic as this is not the point. */
6335 /* FIXME we're making this OK. */
6336 if (TREE_CODE (expr) == STRING_CST)
6337 {
6338 if (complain & tf_error)
6339 error ("%qE is not a valid template argument for type %qT "
6340 "because string literals can never be used in this context",
6341 expr, type);
6342 return NULL_TREE;
6343 }
6344
6345 /* Add the ADDR_EXPR now for the benefit of
6346 value_dependent_expression_p. */
6347 if (TYPE_PTROBV_P (type)
6348 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6349 {
6350 expr = decay_conversion (expr, complain);
6351 if (expr == error_mark_node)
6352 return error_mark_node;
6353 }
6354
6355 /* If we are in a template, EXPR may be non-dependent, but still
6356 have a syntactic, rather than semantic, form. For example, EXPR
6357 might be a SCOPE_REF, rather than the VAR_DECL to which the
6358 SCOPE_REF refers. Preserving the qualifying scope is necessary
6359 so that access checking can be performed when the template is
6360 instantiated -- but here we need the resolved form so that we can
6361 convert the argument. */
6362 bool non_dep = false;
6363 if (TYPE_REF_OBJ_P (type)
6364 && has_value_dependent_address (expr))
6365 /* If we want the address and it's value-dependent, don't fold. */;
6366 else if (processing_template_decl
6367 && potential_nondependent_constant_expression (expr))
6368 non_dep = true;
6369 if (error_operand_p (expr))
6370 return error_mark_node;
6371 expr_type = TREE_TYPE (expr);
6372 if (TREE_CODE (type) == REFERENCE_TYPE)
6373 expr = mark_lvalue_use (expr);
6374 else
6375 expr = mark_rvalue_use (expr);
6376
6377 /* If the argument is non-dependent, perform any conversions in
6378 non-dependent context as well. */
6379 processing_template_decl_sentinel s (non_dep);
6380 if (non_dep)
6381 expr = instantiate_non_dependent_expr_internal (expr, complain);
6382
6383 if (value_dependent_expression_p (expr))
6384 expr = canonicalize_expr_argument (expr, complain);
6385
6386 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6387 to a non-type argument of "nullptr". */
6388 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6389 expr = fold_simple (convert (type, expr));
6390
6391 /* In C++11, integral or enumeration non-type template arguments can be
6392 arbitrary constant expressions. Pointer and pointer to
6393 member arguments can be general constant expressions that evaluate
6394 to a null value, but otherwise still need to be of a specific form. */
6395 if (cxx_dialect >= cxx11)
6396 {
6397 if (TREE_CODE (expr) == PTRMEM_CST)
6398 /* A PTRMEM_CST is already constant, and a valid template
6399 argument for a parameter of pointer to member type, we just want
6400 to leave it in that form rather than lower it to a
6401 CONSTRUCTOR. */;
6402 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6403 /* Constant value checking is done later with type conversion. */;
6404 else if (cxx_dialect >= cxx1z)
6405 {
6406 if (TREE_CODE (type) != REFERENCE_TYPE)
6407 expr = maybe_constant_value (expr);
6408 else if (REFERENCE_REF_P (expr))
6409 {
6410 expr = TREE_OPERAND (expr, 0);
6411 expr = maybe_constant_value (expr);
6412 expr = convert_from_reference (expr);
6413 }
6414 }
6415 else if (TYPE_PTR_OR_PTRMEM_P (type))
6416 {
6417 tree folded = maybe_constant_value (expr);
6418 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6419 : null_member_pointer_value_p (folded))
6420 expr = folded;
6421 }
6422 }
6423
6424 /* HACK: Due to double coercion, we can get a
6425 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6426 which is the tree that we built on the first call (see
6427 below when coercing to reference to object or to reference to
6428 function). We just strip everything and get to the arg.
6429 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6430 for examples. */
6431 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6432 {
6433 tree probe_type, probe = expr;
6434 if (REFERENCE_REF_P (probe))
6435 probe = TREE_OPERAND (probe, 0);
6436 probe_type = TREE_TYPE (probe);
6437 if (TREE_CODE (probe) == NOP_EXPR)
6438 {
6439 /* ??? Maybe we could use convert_from_reference here, but we
6440 would need to relax its constraints because the NOP_EXPR
6441 could actually change the type to something more cv-qualified,
6442 and this is not folded by convert_from_reference. */
6443 tree addr = TREE_OPERAND (probe, 0);
6444 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6445 && TREE_CODE (addr) == ADDR_EXPR
6446 && TYPE_PTR_P (TREE_TYPE (addr))
6447 && (same_type_ignoring_top_level_qualifiers_p
6448 (TREE_TYPE (probe_type),
6449 TREE_TYPE (TREE_TYPE (addr)))))
6450 {
6451 expr = TREE_OPERAND (addr, 0);
6452 expr_type = TREE_TYPE (probe_type);
6453 }
6454 }
6455 }
6456
6457 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6458 parameter is a pointer to object, through decay and
6459 qualification conversion. Let's strip everything. */
6460 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6461 {
6462 tree probe = expr;
6463 STRIP_NOPS (probe);
6464 if (TREE_CODE (probe) == ADDR_EXPR
6465 && TYPE_PTR_P (TREE_TYPE (probe)))
6466 {
6467 /* Skip the ADDR_EXPR only if it is part of the decay for
6468 an array. Otherwise, it is part of the original argument
6469 in the source code. */
6470 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6471 probe = TREE_OPERAND (probe, 0);
6472 expr = probe;
6473 expr_type = TREE_TYPE (expr);
6474 }
6475 }
6476
6477 /* [temp.arg.nontype]/5, bullet 1
6478
6479 For a non-type template-parameter of integral or enumeration type,
6480 integral promotions (_conv.prom_) and integral conversions
6481 (_conv.integral_) are applied. */
6482 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6483 {
6484 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6485 t = maybe_constant_value (t);
6486 if (t != error_mark_node)
6487 expr = t;
6488
6489 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6490 return error_mark_node;
6491
6492 /* Notice that there are constant expressions like '4 % 0' which
6493 do not fold into integer constants. */
6494 if (TREE_CODE (expr) != INTEGER_CST
6495 && !value_dependent_expression_p (expr))
6496 {
6497 if (complain & tf_error)
6498 {
6499 int errs = errorcount, warns = warningcount + werrorcount;
6500 if (processing_template_decl
6501 && !require_potential_constant_expression (expr))
6502 return NULL_TREE;
6503 expr = cxx_constant_value (expr);
6504 if (errorcount > errs || warningcount + werrorcount > warns)
6505 inform (EXPR_LOC_OR_LOC (expr, input_location),
6506 "in template argument for type %qT ", type);
6507 if (expr == error_mark_node)
6508 return NULL_TREE;
6509 /* else cxx_constant_value complained but gave us
6510 a real constant, so go ahead. */
6511 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6512 }
6513 else
6514 return NULL_TREE;
6515 }
6516
6517 /* Avoid typedef problems. */
6518 if (TREE_TYPE (expr) != type)
6519 expr = fold_convert (type, expr);
6520 }
6521 /* [temp.arg.nontype]/5, bullet 2
6522
6523 For a non-type template-parameter of type pointer to object,
6524 qualification conversions (_conv.qual_) and the array-to-pointer
6525 conversion (_conv.array_) are applied. */
6526 else if (TYPE_PTROBV_P (type))
6527 {
6528 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6529
6530 A template-argument for a non-type, non-template template-parameter
6531 shall be one of: [...]
6532
6533 -- the name of a non-type template-parameter;
6534 -- the address of an object or function with external linkage, [...]
6535 expressed as "& id-expression" where the & is optional if the name
6536 refers to a function or array, or if the corresponding
6537 template-parameter is a reference.
6538
6539 Here, we do not care about functions, as they are invalid anyway
6540 for a parameter of type pointer-to-object. */
6541
6542 if (value_dependent_expression_p (expr))
6543 /* Non-type template parameters are OK. */
6544 ;
6545 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6546 /* Null pointer values are OK in C++11. */;
6547 else if (TREE_CODE (expr) != ADDR_EXPR
6548 && TREE_CODE (expr_type) != ARRAY_TYPE)
6549 {
6550 if (VAR_P (expr))
6551 {
6552 if (complain & tf_error)
6553 error ("%qD is not a valid template argument "
6554 "because %qD is a variable, not the address of "
6555 "a variable", expr, expr);
6556 return NULL_TREE;
6557 }
6558 if (POINTER_TYPE_P (expr_type))
6559 {
6560 if (complain & tf_error)
6561 error ("%qE is not a valid template argument for %qT "
6562 "because it is not the address of a variable",
6563 expr, type);
6564 return NULL_TREE;
6565 }
6566 /* Other values, like integer constants, might be valid
6567 non-type arguments of some other type. */
6568 return error_mark_node;
6569 }
6570 else
6571 {
6572 tree decl;
6573
6574 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6575 ? TREE_OPERAND (expr, 0) : expr);
6576 if (!VAR_P (decl))
6577 {
6578 if (complain & tf_error)
6579 error ("%qE is not a valid template argument of type %qT "
6580 "because %qE is not a variable", expr, type, decl);
6581 return NULL_TREE;
6582 }
6583 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6584 {
6585 if (complain & tf_error)
6586 error ("%qE is not a valid template argument of type %qT "
6587 "because %qD does not have external linkage",
6588 expr, type, decl);
6589 return NULL_TREE;
6590 }
6591 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6592 {
6593 if (complain & tf_error)
6594 error ("%qE is not a valid template argument of type %qT "
6595 "because %qD has no linkage", expr, type, decl);
6596 return NULL_TREE;
6597 }
6598 }
6599
6600 expr = decay_conversion (expr, complain);
6601 if (expr == error_mark_node)
6602 return error_mark_node;
6603
6604 expr = perform_qualification_conversions (type, expr);
6605 if (expr == error_mark_node)
6606 return error_mark_node;
6607 }
6608 /* [temp.arg.nontype]/5, bullet 3
6609
6610 For a non-type template-parameter of type reference to object, no
6611 conversions apply. The type referred to by the reference may be more
6612 cv-qualified than the (otherwise identical) type of the
6613 template-argument. The template-parameter is bound directly to the
6614 template-argument, which must be an lvalue. */
6615 else if (TYPE_REF_OBJ_P (type))
6616 {
6617 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6618 expr_type))
6619 return error_mark_node;
6620
6621 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6622 {
6623 if (complain & tf_error)
6624 error ("%qE is not a valid template argument for type %qT "
6625 "because of conflicts in cv-qualification", expr, type);
6626 return NULL_TREE;
6627 }
6628
6629 if (!lvalue_p (expr))
6630 {
6631 if (complain & tf_error)
6632 error ("%qE is not a valid template argument for type %qT "
6633 "because it is not an lvalue", expr, type);
6634 return NULL_TREE;
6635 }
6636
6637 /* [temp.arg.nontype]/1
6638
6639 A template-argument for a non-type, non-template template-parameter
6640 shall be one of: [...]
6641
6642 -- the address of an object or function with external linkage. */
6643 if (INDIRECT_REF_P (expr)
6644 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6645 {
6646 expr = TREE_OPERAND (expr, 0);
6647 if (DECL_P (expr))
6648 {
6649 if (complain & tf_error)
6650 error ("%q#D is not a valid template argument for type %qT "
6651 "because a reference variable does not have a constant "
6652 "address", expr, type);
6653 return NULL_TREE;
6654 }
6655 }
6656
6657 if (!value_dependent_expression_p (expr))
6658 {
6659 if (!DECL_P (expr))
6660 {
6661 if (complain & tf_error)
6662 error ("%qE is not a valid template argument for type %qT "
6663 "because it is not an object with linkage",
6664 expr, type);
6665 return NULL_TREE;
6666 }
6667
6668 /* DR 1155 allows internal linkage in C++11 and up. */
6669 linkage_kind linkage = decl_linkage (expr);
6670 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6671 {
6672 if (complain & tf_error)
6673 error ("%qE is not a valid template argument for type %qT "
6674 "because object %qD does not have linkage",
6675 expr, type, expr);
6676 return NULL_TREE;
6677 }
6678
6679 expr = build_nop (type, build_address (expr));
6680 }
6681 }
6682 /* [temp.arg.nontype]/5, bullet 4
6683
6684 For a non-type template-parameter of type pointer to function, only
6685 the function-to-pointer conversion (_conv.func_) is applied. If the
6686 template-argument represents a set of overloaded functions (or a
6687 pointer to such), the matching function is selected from the set
6688 (_over.over_). */
6689 else if (TYPE_PTRFN_P (type))
6690 {
6691 /* If the argument is a template-id, we might not have enough
6692 context information to decay the pointer. */
6693 if (!type_unknown_p (expr_type))
6694 {
6695 expr = decay_conversion (expr, complain);
6696 if (expr == error_mark_node)
6697 return error_mark_node;
6698 }
6699
6700 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6701 /* Null pointer values are OK in C++11. */
6702 return perform_qualification_conversions (type, expr);
6703
6704 expr = convert_nontype_argument_function (type, expr, complain);
6705 if (!expr || expr == error_mark_node)
6706 return expr;
6707 }
6708 /* [temp.arg.nontype]/5, bullet 5
6709
6710 For a non-type template-parameter of type reference to function, no
6711 conversions apply. If the template-argument represents a set of
6712 overloaded functions, the matching function is selected from the set
6713 (_over.over_). */
6714 else if (TYPE_REFFN_P (type))
6715 {
6716 if (TREE_CODE (expr) == ADDR_EXPR)
6717 {
6718 if (complain & tf_error)
6719 {
6720 error ("%qE is not a valid template argument for type %qT "
6721 "because it is a pointer", expr, type);
6722 inform (input_location, "try using %qE instead",
6723 TREE_OPERAND (expr, 0));
6724 }
6725 return NULL_TREE;
6726 }
6727
6728 expr = convert_nontype_argument_function (type, expr, complain);
6729 if (!expr || expr == error_mark_node)
6730 return expr;
6731 }
6732 /* [temp.arg.nontype]/5, bullet 6
6733
6734 For a non-type template-parameter of type pointer to member function,
6735 no conversions apply. If the template-argument represents a set of
6736 overloaded member functions, the matching member function is selected
6737 from the set (_over.over_). */
6738 else if (TYPE_PTRMEMFUNC_P (type))
6739 {
6740 expr = instantiate_type (type, expr, tf_none);
6741 if (expr == error_mark_node)
6742 return error_mark_node;
6743
6744 /* [temp.arg.nontype] bullet 1 says the pointer to member
6745 expression must be a pointer-to-member constant. */
6746 if (!value_dependent_expression_p (expr)
6747 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6748 return error_mark_node;
6749
6750 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6751 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6752 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6753 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6754
6755 /* There is no way to disable standard conversions in
6756 resolve_address_of_overloaded_function (called by
6757 instantiate_type). It is possible that the call succeeded by
6758 converting &B::I to &D::I (where B is a base of D), so we need
6759 to reject this conversion here.
6760
6761 Actually, even if there was a way to disable standard conversions,
6762 it would still be better to reject them here so that we can
6763 provide a superior diagnostic. */
6764 if (!same_type_p (TREE_TYPE (expr), type))
6765 {
6766 if (complain & tf_error)
6767 {
6768 error ("%qE is not a valid template argument for type %qT "
6769 "because it is of type %qT", expr, type,
6770 TREE_TYPE (expr));
6771 /* If we are just one standard conversion off, explain. */
6772 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6773 inform (input_location,
6774 "standard conversions are not allowed in this context");
6775 }
6776 return NULL_TREE;
6777 }
6778 }
6779 /* [temp.arg.nontype]/5, bullet 7
6780
6781 For a non-type template-parameter of type pointer to data member,
6782 qualification conversions (_conv.qual_) are applied. */
6783 else if (TYPE_PTRDATAMEM_P (type))
6784 {
6785 /* [temp.arg.nontype] bullet 1 says the pointer to member
6786 expression must be a pointer-to-member constant. */
6787 if (!value_dependent_expression_p (expr)
6788 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6789 return error_mark_node;
6790
6791 expr = perform_qualification_conversions (type, expr);
6792 if (expr == error_mark_node)
6793 return expr;
6794 }
6795 else if (NULLPTR_TYPE_P (type))
6796 {
6797 if (expr != nullptr_node)
6798 {
6799 if (complain & tf_error)
6800 error ("%qE is not a valid template argument for type %qT "
6801 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6802 return NULL_TREE;
6803 }
6804 return expr;
6805 }
6806 /* A template non-type parameter must be one of the above. */
6807 else
6808 gcc_unreachable ();
6809
6810 /* Sanity check: did we actually convert the argument to the
6811 right type? */
6812 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6813 (type, TREE_TYPE (expr)));
6814 return convert_from_reference (expr);
6815 }
6816
6817 /* Subroutine of coerce_template_template_parms, which returns 1 if
6818 PARM_PARM and ARG_PARM match using the rule for the template
6819 parameters of template template parameters. Both PARM and ARG are
6820 template parameters; the rest of the arguments are the same as for
6821 coerce_template_template_parms.
6822 */
6823 static int
6824 coerce_template_template_parm (tree parm,
6825 tree arg,
6826 tsubst_flags_t complain,
6827 tree in_decl,
6828 tree outer_args)
6829 {
6830 if (arg == NULL_TREE || error_operand_p (arg)
6831 || parm == NULL_TREE || error_operand_p (parm))
6832 return 0;
6833
6834 if (TREE_CODE (arg) != TREE_CODE (parm))
6835 return 0;
6836
6837 switch (TREE_CODE (parm))
6838 {
6839 case TEMPLATE_DECL:
6840 /* We encounter instantiations of templates like
6841 template <template <template <class> class> class TT>
6842 class C; */
6843 {
6844 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6845 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6846
6847 if (!coerce_template_template_parms
6848 (parmparm, argparm, complain, in_decl, outer_args))
6849 return 0;
6850 }
6851 /* Fall through. */
6852
6853 case TYPE_DECL:
6854 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6855 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6856 /* Argument is a parameter pack but parameter is not. */
6857 return 0;
6858 break;
6859
6860 case PARM_DECL:
6861 /* The tsubst call is used to handle cases such as
6862
6863 template <int> class C {};
6864 template <class T, template <T> class TT> class D {};
6865 D<int, C> d;
6866
6867 i.e. the parameter list of TT depends on earlier parameters. */
6868 if (!uses_template_parms (TREE_TYPE (arg)))
6869 {
6870 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6871 if (!uses_template_parms (t)
6872 && !same_type_p (t, TREE_TYPE (arg)))
6873 return 0;
6874 }
6875
6876 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6877 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6878 /* Argument is a parameter pack but parameter is not. */
6879 return 0;
6880
6881 break;
6882
6883 default:
6884 gcc_unreachable ();
6885 }
6886
6887 return 1;
6888 }
6889
6890 /* Coerce template argument list ARGLIST for use with template
6891 template-parameter TEMPL. */
6892
6893 static tree
6894 coerce_template_args_for_ttp (tree templ, tree arglist,
6895 tsubst_flags_t complain)
6896 {
6897 /* Consider an example where a template template parameter declared as
6898
6899 template <class T, class U = std::allocator<T> > class TT
6900
6901 The template parameter level of T and U are one level larger than
6902 of TT. To proper process the default argument of U, say when an
6903 instantiation `TT<int>' is seen, we need to build the full
6904 arguments containing {int} as the innermost level. Outer levels,
6905 available when not appearing as default template argument, can be
6906 obtained from the arguments of the enclosing template.
6907
6908 Suppose that TT is later substituted with std::vector. The above
6909 instantiation is `TT<int, std::allocator<T> >' with TT at
6910 level 1, and T at level 2, while the template arguments at level 1
6911 becomes {std::vector} and the inner level 2 is {int}. */
6912
6913 tree outer = DECL_CONTEXT (templ);
6914 if (outer)
6915 {
6916 if (DECL_TEMPLATE_SPECIALIZATION (outer))
6917 /* We want arguments for the partial specialization, not arguments for
6918 the primary template. */
6919 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
6920 else
6921 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6922 }
6923 else if (current_template_parms)
6924 {
6925 /* This is an argument of the current template, so we haven't set
6926 DECL_CONTEXT yet. */
6927 tree relevant_template_parms;
6928
6929 /* Parameter levels that are greater than the level of the given
6930 template template parm are irrelevant. */
6931 relevant_template_parms = current_template_parms;
6932 while (TMPL_PARMS_DEPTH (relevant_template_parms)
6933 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
6934 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
6935
6936 outer = template_parms_to_args (relevant_template_parms);
6937 }
6938
6939 if (outer)
6940 arglist = add_to_template_args (outer, arglist);
6941
6942 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6943 return coerce_template_parms (parmlist, arglist, templ,
6944 complain,
6945 /*require_all_args=*/true,
6946 /*use_default_args=*/true);
6947 }
6948
6949 /* A cache of template template parameters with match-all default
6950 arguments. */
6951 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
6952 static void
6953 store_defaulted_ttp (tree v, tree t)
6954 {
6955 if (!defaulted_ttp_cache)
6956 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
6957 defaulted_ttp_cache->put (v, t);
6958 }
6959 static tree
6960 lookup_defaulted_ttp (tree v)
6961 {
6962 if (defaulted_ttp_cache)
6963 if (tree *p = defaulted_ttp_cache->get (v))
6964 return *p;
6965 return NULL_TREE;
6966 }
6967
6968 /* T is a bound template template-parameter. Copy its arguments into default
6969 arguments of the template template-parameter's template parameters. */
6970
6971 static tree
6972 add_defaults_to_ttp (tree otmpl)
6973 {
6974 if (tree c = lookup_defaulted_ttp (otmpl))
6975 return c;
6976
6977 tree ntmpl = copy_node (otmpl);
6978
6979 tree ntype = copy_node (TREE_TYPE (otmpl));
6980 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
6981 TYPE_MAIN_VARIANT (ntype) = ntype;
6982 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
6983 TYPE_NAME (ntype) = ntmpl;
6984 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
6985
6986 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
6987 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
6988 TEMPLATE_PARM_DECL (idx) = ntmpl;
6989 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
6990
6991 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
6992 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
6993 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
6994 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
6995 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
6996 {
6997 tree o = TREE_VEC_ELT (vec, i);
6998 if (!template_parameter_pack_p (TREE_VALUE (o)))
6999 {
7000 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7001 TREE_PURPOSE (n) = any_targ_node;
7002 }
7003 }
7004
7005 store_defaulted_ttp (otmpl, ntmpl);
7006 return ntmpl;
7007 }
7008
7009 /* ARG is a bound potential template template-argument, and PARGS is a list
7010 of arguments for the corresponding template template-parameter. Adjust
7011 PARGS as appropriate for application to ARG's template, and if ARG is a
7012 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7013 arguments to the template template parameter. */
7014
7015 static tree
7016 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7017 {
7018 ++processing_template_decl;
7019 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7021 {
7022 /* When comparing two template template-parameters in partial ordering,
7023 rewrite the one currently being used as an argument to have default
7024 arguments for all parameters. */
7025 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7026 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7027 if (pargs != error_mark_node)
7028 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7029 TYPE_TI_ARGS (arg));
7030 }
7031 else
7032 {
7033 tree aparms
7034 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7035 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7036 /*require_all*/true,
7037 /*use_default*/true);
7038 }
7039 --processing_template_decl;
7040 return pargs;
7041 }
7042
7043 /* Subroutine of unify for the case when PARM is a
7044 BOUND_TEMPLATE_TEMPLATE_PARM. */
7045
7046 static int
7047 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7048 bool explain_p)
7049 {
7050 tree parmvec = TYPE_TI_ARGS (parm);
7051 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7052
7053 /* The template template parm might be variadic and the argument
7054 not, so flatten both argument lists. */
7055 parmvec = expand_template_argument_pack (parmvec);
7056 argvec = expand_template_argument_pack (argvec);
7057
7058 tree nparmvec = parmvec;
7059 if (flag_new_ttp)
7060 {
7061 /* In keeping with P0522R0, adjust P's template arguments
7062 to apply to A's template; then flatten it again. */
7063 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7064 nparmvec = expand_template_argument_pack (nparmvec);
7065 }
7066
7067 if (unify (tparms, targs, nparmvec, argvec,
7068 UNIFY_ALLOW_NONE, explain_p))
7069 return 1;
7070
7071 /* If the P0522 adjustment eliminated a pack expansion, deduce
7072 empty packs. */
7073 if (flag_new_ttp
7074 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7075 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7076 DEDUCE_EXACT, /*sub*/true, explain_p))
7077 return 1;
7078
7079 return 0;
7080 }
7081
7082 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7083 template template parameters. Both PARM_PARMS and ARG_PARMS are
7084 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7085 or PARM_DECL.
7086
7087 Consider the example:
7088 template <class T> class A;
7089 template<template <class U> class TT> class B;
7090
7091 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7092 the parameters to A, and OUTER_ARGS contains A. */
7093
7094 static int
7095 coerce_template_template_parms (tree parm_parms,
7096 tree arg_parms,
7097 tsubst_flags_t complain,
7098 tree in_decl,
7099 tree outer_args)
7100 {
7101 int nparms, nargs, i;
7102 tree parm, arg;
7103 int variadic_p = 0;
7104
7105 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7106 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7107
7108 nparms = TREE_VEC_LENGTH (parm_parms);
7109 nargs = TREE_VEC_LENGTH (arg_parms);
7110
7111 if (flag_new_ttp)
7112 {
7113 /* P0522R0: A template template-parameter P is at least as specialized as
7114 a template template-argument A if, given the following rewrite to two
7115 function templates, the function template corresponding to P is at
7116 least as specialized as the function template corresponding to A
7117 according to the partial ordering rules for function templates
7118 ([temp.func.order]). Given an invented class template X with the
7119 template parameter list of A (including default arguments):
7120
7121 * Each of the two function templates has the same template parameters,
7122 respectively, as P or A.
7123
7124 * Each function template has a single function parameter whose type is
7125 a specialization of X with template arguments corresponding to the
7126 template parameters from the respective function template where, for
7127 each template parameter PP in the template parameter list of the
7128 function template, a corresponding template argument AA is formed. If
7129 PP declares a parameter pack, then AA is the pack expansion
7130 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7131
7132 If the rewrite produces an invalid type, then P is not at least as
7133 specialized as A. */
7134
7135 /* So coerce P's args to apply to A's parms, and then deduce between A's
7136 args and the converted args. If that succeeds, A is at least as
7137 specialized as P, so they match.*/
7138 tree pargs = template_parms_level_to_args (parm_parms);
7139 ++processing_template_decl;
7140 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7141 /*require_all*/true, /*use_default*/true);
7142 --processing_template_decl;
7143 if (pargs != error_mark_node)
7144 {
7145 tree targs = make_tree_vec (nargs);
7146 tree aargs = template_parms_level_to_args (arg_parms);
7147 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7148 /*explain*/false))
7149 return 1;
7150 }
7151 }
7152
7153 /* Determine whether we have a parameter pack at the end of the
7154 template template parameter's template parameter list. */
7155 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7156 {
7157 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7158
7159 if (error_operand_p (parm))
7160 return 0;
7161
7162 switch (TREE_CODE (parm))
7163 {
7164 case TEMPLATE_DECL:
7165 case TYPE_DECL:
7166 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7167 variadic_p = 1;
7168 break;
7169
7170 case PARM_DECL:
7171 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7172 variadic_p = 1;
7173 break;
7174
7175 default:
7176 gcc_unreachable ();
7177 }
7178 }
7179
7180 if (nargs != nparms
7181 && !(variadic_p && nargs >= nparms - 1))
7182 return 0;
7183
7184 /* Check all of the template parameters except the parameter pack at
7185 the end (if any). */
7186 for (i = 0; i < nparms - variadic_p; ++i)
7187 {
7188 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7189 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7190 continue;
7191
7192 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7193 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7194
7195 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7196 outer_args))
7197 return 0;
7198
7199 }
7200
7201 if (variadic_p)
7202 {
7203 /* Check each of the template parameters in the template
7204 argument against the template parameter pack at the end of
7205 the template template parameter. */
7206 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7207 return 0;
7208
7209 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7210
7211 for (; i < nargs; ++i)
7212 {
7213 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7214 continue;
7215
7216 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7217
7218 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7219 outer_args))
7220 return 0;
7221 }
7222 }
7223
7224 return 1;
7225 }
7226
7227 /* Verifies that the deduced template arguments (in TARGS) for the
7228 template template parameters (in TPARMS) represent valid bindings,
7229 by comparing the template parameter list of each template argument
7230 to the template parameter list of its corresponding template
7231 template parameter, in accordance with DR150. This
7232 routine can only be called after all template arguments have been
7233 deduced. It will return TRUE if all of the template template
7234 parameter bindings are okay, FALSE otherwise. */
7235 bool
7236 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7237 {
7238 int i, ntparms = TREE_VEC_LENGTH (tparms);
7239 bool ret = true;
7240
7241 /* We're dealing with template parms in this process. */
7242 ++processing_template_decl;
7243
7244 targs = INNERMOST_TEMPLATE_ARGS (targs);
7245
7246 for (i = 0; i < ntparms; ++i)
7247 {
7248 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7249 tree targ = TREE_VEC_ELT (targs, i);
7250
7251 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7252 {
7253 tree packed_args = NULL_TREE;
7254 int idx, len = 1;
7255
7256 if (ARGUMENT_PACK_P (targ))
7257 {
7258 /* Look inside the argument pack. */
7259 packed_args = ARGUMENT_PACK_ARGS (targ);
7260 len = TREE_VEC_LENGTH (packed_args);
7261 }
7262
7263 for (idx = 0; idx < len; ++idx)
7264 {
7265 tree targ_parms = NULL_TREE;
7266
7267 if (packed_args)
7268 /* Extract the next argument from the argument
7269 pack. */
7270 targ = TREE_VEC_ELT (packed_args, idx);
7271
7272 if (PACK_EXPANSION_P (targ))
7273 /* Look at the pattern of the pack expansion. */
7274 targ = PACK_EXPANSION_PATTERN (targ);
7275
7276 /* Extract the template parameters from the template
7277 argument. */
7278 if (TREE_CODE (targ) == TEMPLATE_DECL)
7279 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7280 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7281 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7282
7283 /* Verify that we can coerce the template template
7284 parameters from the template argument to the template
7285 parameter. This requires an exact match. */
7286 if (targ_parms
7287 && !coerce_template_template_parms
7288 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7289 targ_parms,
7290 tf_none,
7291 tparm,
7292 targs))
7293 {
7294 ret = false;
7295 goto out;
7296 }
7297 }
7298 }
7299 }
7300
7301 out:
7302
7303 --processing_template_decl;
7304 return ret;
7305 }
7306
7307 /* Since type attributes aren't mangled, we need to strip them from
7308 template type arguments. */
7309
7310 static tree
7311 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7312 {
7313 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7314 return arg;
7315 bool removed_attributes = false;
7316 tree canon = strip_typedefs (arg, &removed_attributes);
7317 if (removed_attributes
7318 && (complain & tf_warning))
7319 warning (OPT_Wignored_attributes,
7320 "ignoring attributes on template argument %qT", arg);
7321 return canon;
7322 }
7323
7324 /* And from inside dependent non-type arguments like sizeof(Type). */
7325
7326 static tree
7327 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7328 {
7329 if (!arg || arg == error_mark_node)
7330 return arg;
7331 bool removed_attributes = false;
7332 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7333 if (removed_attributes
7334 && (complain & tf_warning))
7335 warning (OPT_Wignored_attributes,
7336 "ignoring attributes in template argument %qE", arg);
7337 return canon;
7338 }
7339
7340 // A template declaration can be substituted for a constrained
7341 // template template parameter only when the argument is more
7342 // constrained than the parameter.
7343 static bool
7344 is_compatible_template_arg (tree parm, tree arg)
7345 {
7346 tree parm_cons = get_constraints (parm);
7347
7348 /* For now, allow constrained template template arguments
7349 and unconstrained template template parameters. */
7350 if (parm_cons == NULL_TREE)
7351 return true;
7352
7353 tree arg_cons = get_constraints (arg);
7354
7355 // If the template parameter is constrained, we need to rewrite its
7356 // constraints in terms of the ARG's template parameters. This ensures
7357 // that all of the template parameter types will have the same depth.
7358 //
7359 // Note that this is only valid when coerce_template_template_parm is
7360 // true for the innermost template parameters of PARM and ARG. In other
7361 // words, because coercion is successful, this conversion will be valid.
7362 if (parm_cons)
7363 {
7364 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7365 parm_cons = tsubst_constraint_info (parm_cons,
7366 INNERMOST_TEMPLATE_ARGS (args),
7367 tf_none, NULL_TREE);
7368 if (parm_cons == error_mark_node)
7369 return false;
7370 }
7371
7372 return subsumes (parm_cons, arg_cons);
7373 }
7374
7375 // Convert a placeholder argument into a binding to the original
7376 // parameter. The original parameter is saved as the TREE_TYPE of
7377 // ARG.
7378 static inline tree
7379 convert_wildcard_argument (tree parm, tree arg)
7380 {
7381 TREE_TYPE (arg) = parm;
7382 return arg;
7383 }
7384
7385 /* Convert the indicated template ARG as necessary to match the
7386 indicated template PARM. Returns the converted ARG, or
7387 error_mark_node if the conversion was unsuccessful. Error and
7388 warning messages are issued under control of COMPLAIN. This
7389 conversion is for the Ith parameter in the parameter list. ARGS is
7390 the full set of template arguments deduced so far. */
7391
7392 static tree
7393 convert_template_argument (tree parm,
7394 tree arg,
7395 tree args,
7396 tsubst_flags_t complain,
7397 int i,
7398 tree in_decl)
7399 {
7400 tree orig_arg;
7401 tree val;
7402 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7403
7404 if (parm == error_mark_node)
7405 return error_mark_node;
7406
7407 /* Trivially convert placeholders. */
7408 if (TREE_CODE (arg) == WILDCARD_DECL)
7409 return convert_wildcard_argument (parm, arg);
7410
7411 if (arg == any_targ_node)
7412 return arg;
7413
7414 if (TREE_CODE (arg) == TREE_LIST
7415 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7416 {
7417 /* The template argument was the name of some
7418 member function. That's usually
7419 invalid, but static members are OK. In any
7420 case, grab the underlying fields/functions
7421 and issue an error later if required. */
7422 orig_arg = TREE_VALUE (arg);
7423 TREE_TYPE (arg) = unknown_type_node;
7424 }
7425
7426 orig_arg = arg;
7427
7428 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7429 requires_type = (TREE_CODE (parm) == TYPE_DECL
7430 || requires_tmpl_type);
7431
7432 /* When determining whether an argument pack expansion is a template,
7433 look at the pattern. */
7434 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7435 arg = PACK_EXPANSION_PATTERN (arg);
7436
7437 /* Deal with an injected-class-name used as a template template arg. */
7438 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7439 {
7440 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7441 if (TREE_CODE (t) == TEMPLATE_DECL)
7442 {
7443 if (cxx_dialect >= cxx11)
7444 /* OK under DR 1004. */;
7445 else if (complain & tf_warning_or_error)
7446 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7447 " used as template template argument", TYPE_NAME (arg));
7448 else if (flag_pedantic_errors)
7449 t = arg;
7450
7451 arg = t;
7452 }
7453 }
7454
7455 is_tmpl_type =
7456 ((TREE_CODE (arg) == TEMPLATE_DECL
7457 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7458 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7459 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7460 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7461
7462 if (is_tmpl_type
7463 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7464 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7465 arg = TYPE_STUB_DECL (arg);
7466
7467 is_type = TYPE_P (arg) || is_tmpl_type;
7468
7469 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7470 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7471 {
7472 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7473 {
7474 if (complain & tf_error)
7475 error ("invalid use of destructor %qE as a type", orig_arg);
7476 return error_mark_node;
7477 }
7478
7479 permerror (input_location,
7480 "to refer to a type member of a template parameter, "
7481 "use %<typename %E%>", orig_arg);
7482
7483 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7484 TREE_OPERAND (arg, 1),
7485 typename_type,
7486 complain);
7487 arg = orig_arg;
7488 is_type = 1;
7489 }
7490 if (is_type != requires_type)
7491 {
7492 if (in_decl)
7493 {
7494 if (complain & tf_error)
7495 {
7496 error ("type/value mismatch at argument %d in template "
7497 "parameter list for %qD",
7498 i + 1, in_decl);
7499 if (is_type)
7500 inform (input_location,
7501 " expected a constant of type %qT, got %qT",
7502 TREE_TYPE (parm),
7503 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7504 else if (requires_tmpl_type)
7505 inform (input_location,
7506 " expected a class template, got %qE", orig_arg);
7507 else
7508 inform (input_location,
7509 " expected a type, got %qE", orig_arg);
7510 }
7511 }
7512 return error_mark_node;
7513 }
7514 if (is_tmpl_type ^ requires_tmpl_type)
7515 {
7516 if (in_decl && (complain & tf_error))
7517 {
7518 error ("type/value mismatch at argument %d in template "
7519 "parameter list for %qD",
7520 i + 1, in_decl);
7521 if (is_tmpl_type)
7522 inform (input_location,
7523 " expected a type, got %qT", DECL_NAME (arg));
7524 else
7525 inform (input_location,
7526 " expected a class template, got %qT", orig_arg);
7527 }
7528 return error_mark_node;
7529 }
7530
7531 if (is_type)
7532 {
7533 if (requires_tmpl_type)
7534 {
7535 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7536 val = orig_arg;
7537 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7538 /* The number of argument required is not known yet.
7539 Just accept it for now. */
7540 val = TREE_TYPE (arg);
7541 else
7542 {
7543 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7544 tree argparm;
7545
7546 /* Strip alias templates that are equivalent to another
7547 template. */
7548 arg = get_underlying_template (arg);
7549 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7550
7551 if (coerce_template_template_parms (parmparm, argparm,
7552 complain, in_decl,
7553 args))
7554 {
7555 val = arg;
7556
7557 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7558 TEMPLATE_DECL. */
7559 if (val != error_mark_node)
7560 {
7561 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7562 val = TREE_TYPE (val);
7563 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7564 val = make_pack_expansion (val);
7565 }
7566 }
7567 else
7568 {
7569 if (in_decl && (complain & tf_error))
7570 {
7571 error ("type/value mismatch at argument %d in "
7572 "template parameter list for %qD",
7573 i + 1, in_decl);
7574 inform (input_location,
7575 " expected a template of type %qD, got %qT",
7576 parm, orig_arg);
7577 }
7578
7579 val = error_mark_node;
7580 }
7581
7582 // Check that the constraints are compatible before allowing the
7583 // substitution.
7584 if (val != error_mark_node)
7585 if (!is_compatible_template_arg (parm, arg))
7586 {
7587 if (in_decl && (complain & tf_error))
7588 {
7589 error ("constraint mismatch at argument %d in "
7590 "template parameter list for %qD",
7591 i + 1, in_decl);
7592 inform (input_location, " expected %qD but got %qD",
7593 parm, arg);
7594 }
7595 val = error_mark_node;
7596 }
7597 }
7598 }
7599 else
7600 val = orig_arg;
7601 /* We only form one instance of each template specialization.
7602 Therefore, if we use a non-canonical variant (i.e., a
7603 typedef), any future messages referring to the type will use
7604 the typedef, which is confusing if those future uses do not
7605 themselves also use the typedef. */
7606 if (TYPE_P (val))
7607 val = canonicalize_type_argument (val, complain);
7608 }
7609 else
7610 {
7611 tree t = TREE_TYPE (parm);
7612
7613 if (tree a = type_uses_auto (t))
7614 {
7615 if (ARGUMENT_PACK_P (orig_arg))
7616 /* There's nothing to check for an auto argument pack. */
7617 return orig_arg;
7618
7619 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7620 if (t == error_mark_node)
7621 return error_mark_node;
7622 }
7623 else
7624 t = tsubst (t, args, complain, in_decl);
7625
7626 if (invalid_nontype_parm_type_p (t, complain))
7627 return error_mark_node;
7628
7629 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7630 {
7631 if (same_type_p (t, TREE_TYPE (orig_arg)))
7632 val = orig_arg;
7633 else
7634 {
7635 /* Not sure if this is reachable, but it doesn't hurt
7636 to be robust. */
7637 error ("type mismatch in nontype parameter pack");
7638 val = error_mark_node;
7639 }
7640 }
7641 else if (!type_dependent_expression_p (orig_arg)
7642 && !uses_template_parms (t))
7643 /* We used to call digest_init here. However, digest_init
7644 will report errors, which we don't want when complain
7645 is zero. More importantly, digest_init will try too
7646 hard to convert things: for example, `0' should not be
7647 converted to pointer type at this point according to
7648 the standard. Accepting this is not merely an
7649 extension, since deciding whether or not these
7650 conversions can occur is part of determining which
7651 function template to call, or whether a given explicit
7652 argument specification is valid. */
7653 val = convert_nontype_argument (t, orig_arg, complain);
7654 else
7655 val = canonicalize_expr_argument (orig_arg, complain);
7656
7657 if (val == NULL_TREE)
7658 val = error_mark_node;
7659 else if (val == error_mark_node && (complain & tf_error))
7660 error ("could not convert template argument %qE from %qT to %qT",
7661 orig_arg, TREE_TYPE (orig_arg), t);
7662
7663 if (INDIRECT_REF_P (val))
7664 {
7665 /* Reject template arguments that are references to built-in
7666 functions with no library fallbacks. */
7667 const_tree inner = TREE_OPERAND (val, 0);
7668 const_tree innertype = TREE_TYPE (inner);
7669 if (innertype
7670 && TREE_CODE (innertype) == REFERENCE_TYPE
7671 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7672 && 0 < TREE_OPERAND_LENGTH (inner)
7673 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7674 return error_mark_node;
7675 }
7676
7677 if (TREE_CODE (val) == SCOPE_REF)
7678 {
7679 /* Strip typedefs from the SCOPE_REF. */
7680 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7681 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7682 complain);
7683 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7684 QUALIFIED_NAME_IS_TEMPLATE (val));
7685 }
7686 }
7687
7688 return val;
7689 }
7690
7691 /* Coerces the remaining template arguments in INNER_ARGS (from
7692 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7693 Returns the coerced argument pack. PARM_IDX is the position of this
7694 parameter in the template parameter list. ARGS is the original
7695 template argument list. */
7696 static tree
7697 coerce_template_parameter_pack (tree parms,
7698 int parm_idx,
7699 tree args,
7700 tree inner_args,
7701 int arg_idx,
7702 tree new_args,
7703 int* lost,
7704 tree in_decl,
7705 tsubst_flags_t complain)
7706 {
7707 tree parm = TREE_VEC_ELT (parms, parm_idx);
7708 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7709 tree packed_args;
7710 tree argument_pack;
7711 tree packed_parms = NULL_TREE;
7712
7713 if (arg_idx > nargs)
7714 arg_idx = nargs;
7715
7716 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7717 {
7718 /* When the template parameter is a non-type template parameter pack
7719 or template template parameter pack whose type or template
7720 parameters use parameter packs, we know exactly how many arguments
7721 we are looking for. Build a vector of the instantiated decls for
7722 these template parameters in PACKED_PARMS. */
7723 /* We can't use make_pack_expansion here because it would interpret a
7724 _DECL as a use rather than a declaration. */
7725 tree decl = TREE_VALUE (parm);
7726 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7727 SET_PACK_EXPANSION_PATTERN (exp, decl);
7728 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7729 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7730
7731 TREE_VEC_LENGTH (args)--;
7732 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7733 TREE_VEC_LENGTH (args)++;
7734
7735 if (packed_parms == error_mark_node)
7736 return error_mark_node;
7737
7738 /* If we're doing a partial instantiation of a member template,
7739 verify that all of the types used for the non-type
7740 template parameter pack are, in fact, valid for non-type
7741 template parameters. */
7742 if (arg_idx < nargs
7743 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7744 {
7745 int j, len = TREE_VEC_LENGTH (packed_parms);
7746 for (j = 0; j < len; ++j)
7747 {
7748 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7749 if (invalid_nontype_parm_type_p (t, complain))
7750 return error_mark_node;
7751 }
7752 /* We don't know how many args we have yet, just
7753 use the unconverted ones for now. */
7754 return NULL_TREE;
7755 }
7756
7757 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7758 }
7759 /* Check if we have a placeholder pack, which indicates we're
7760 in the context of a introduction list. In that case we want
7761 to match this pack to the single placeholder. */
7762 else if (arg_idx < nargs
7763 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7764 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7765 {
7766 nargs = arg_idx + 1;
7767 packed_args = make_tree_vec (1);
7768 }
7769 else
7770 packed_args = make_tree_vec (nargs - arg_idx);
7771
7772 /* Convert the remaining arguments, which will be a part of the
7773 parameter pack "parm". */
7774 int first_pack_arg = arg_idx;
7775 for (; arg_idx < nargs; ++arg_idx)
7776 {
7777 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7778 tree actual_parm = TREE_VALUE (parm);
7779 int pack_idx = arg_idx - first_pack_arg;
7780
7781 if (packed_parms)
7782 {
7783 /* Once we've packed as many args as we have types, stop. */
7784 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7785 break;
7786 else if (PACK_EXPANSION_P (arg))
7787 /* We don't know how many args we have yet, just
7788 use the unconverted ones for now. */
7789 return NULL_TREE;
7790 else
7791 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7792 }
7793
7794 if (arg == error_mark_node)
7795 {
7796 if (complain & tf_error)
7797 error ("template argument %d is invalid", arg_idx + 1);
7798 }
7799 else
7800 arg = convert_template_argument (actual_parm,
7801 arg, new_args, complain, parm_idx,
7802 in_decl);
7803 if (arg == error_mark_node)
7804 (*lost)++;
7805 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7806 }
7807
7808 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7809 && TREE_VEC_LENGTH (packed_args) > 0)
7810 {
7811 if (complain & tf_error)
7812 error ("wrong number of template arguments (%d, should be %d)",
7813 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7814 return error_mark_node;
7815 }
7816
7817 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7818 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7819 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7820 else
7821 {
7822 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7823 TREE_TYPE (argument_pack)
7824 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7825 TREE_CONSTANT (argument_pack) = 1;
7826 }
7827
7828 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7829 if (CHECKING_P)
7830 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7831 TREE_VEC_LENGTH (packed_args));
7832 return argument_pack;
7833 }
7834
7835 /* Returns the number of pack expansions in the template argument vector
7836 ARGS. */
7837
7838 static int
7839 pack_expansion_args_count (tree args)
7840 {
7841 int i;
7842 int count = 0;
7843 if (args)
7844 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7845 {
7846 tree elt = TREE_VEC_ELT (args, i);
7847 if (elt && PACK_EXPANSION_P (elt))
7848 ++count;
7849 }
7850 return count;
7851 }
7852
7853 /* Convert all template arguments to their appropriate types, and
7854 return a vector containing the innermost resulting template
7855 arguments. If any error occurs, return error_mark_node. Error and
7856 warning messages are issued under control of COMPLAIN.
7857
7858 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7859 for arguments not specified in ARGS. Otherwise, if
7860 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7861 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7862 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7863 ARGS. */
7864
7865 static tree
7866 coerce_template_parms (tree parms,
7867 tree args,
7868 tree in_decl,
7869 tsubst_flags_t complain,
7870 bool require_all_args,
7871 bool use_default_args)
7872 {
7873 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7874 tree orig_inner_args;
7875 tree inner_args;
7876 tree new_args;
7877 tree new_inner_args;
7878 int saved_unevaluated_operand;
7879 int saved_inhibit_evaluation_warnings;
7880
7881 /* When used as a boolean value, indicates whether this is a
7882 variadic template parameter list. Since it's an int, we can also
7883 subtract it from nparms to get the number of non-variadic
7884 parameters. */
7885 int variadic_p = 0;
7886 int variadic_args_p = 0;
7887 int post_variadic_parms = 0;
7888
7889 /* Likewise for parameters with default arguments. */
7890 int default_p = 0;
7891
7892 if (args == error_mark_node)
7893 return error_mark_node;
7894
7895 nparms = TREE_VEC_LENGTH (parms);
7896
7897 /* Determine if there are any parameter packs or default arguments. */
7898 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7899 {
7900 tree parm = TREE_VEC_ELT (parms, parm_idx);
7901 if (variadic_p)
7902 ++post_variadic_parms;
7903 if (template_parameter_pack_p (TREE_VALUE (parm)))
7904 ++variadic_p;
7905 if (TREE_PURPOSE (parm))
7906 ++default_p;
7907 }
7908
7909 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7910 /* If there are no parameters that follow a parameter pack, we need to
7911 expand any argument packs so that we can deduce a parameter pack from
7912 some non-packed args followed by an argument pack, as in variadic85.C.
7913 If there are such parameters, we need to leave argument packs intact
7914 so the arguments are assigned properly. This can happen when dealing
7915 with a nested class inside a partial specialization of a class
7916 template, as in variadic92.C, or when deducing a template parameter pack
7917 from a sub-declarator, as in variadic114.C. */
7918 if (!post_variadic_parms)
7919 inner_args = expand_template_argument_pack (inner_args);
7920
7921 /* Count any pack expansion args. */
7922 variadic_args_p = pack_expansion_args_count (inner_args);
7923
7924 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7925 if ((nargs - variadic_args_p > nparms && !variadic_p)
7926 || (nargs < nparms - variadic_p
7927 && require_all_args
7928 && !variadic_args_p
7929 && (!use_default_args
7930 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7931 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7932 {
7933 if (complain & tf_error)
7934 {
7935 if (variadic_p || default_p)
7936 {
7937 nparms -= variadic_p + default_p;
7938 error ("wrong number of template arguments "
7939 "(%d, should be at least %d)", nargs, nparms);
7940 }
7941 else
7942 error ("wrong number of template arguments "
7943 "(%d, should be %d)", nargs, nparms);
7944
7945 if (in_decl)
7946 inform (DECL_SOURCE_LOCATION (in_decl),
7947 "provided for %qD", in_decl);
7948 }
7949
7950 return error_mark_node;
7951 }
7952 /* We can't pass a pack expansion to a non-pack parameter of an alias
7953 template (DR 1430). */
7954 else if (in_decl
7955 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7956 || concept_template_p (in_decl))
7957 && variadic_args_p
7958 && nargs - variadic_args_p < nparms - variadic_p)
7959 {
7960 if (complain & tf_error)
7961 {
7962 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7963 {
7964 tree arg = TREE_VEC_ELT (inner_args, i);
7965 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7966
7967 if (PACK_EXPANSION_P (arg)
7968 && !template_parameter_pack_p (parm))
7969 {
7970 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7971 error_at (location_of (arg),
7972 "pack expansion argument for non-pack parameter "
7973 "%qD of alias template %qD", parm, in_decl);
7974 else
7975 error_at (location_of (arg),
7976 "pack expansion argument for non-pack parameter "
7977 "%qD of concept %qD", parm, in_decl);
7978 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7979 goto found;
7980 }
7981 }
7982 gcc_unreachable ();
7983 found:;
7984 }
7985 return error_mark_node;
7986 }
7987
7988 /* We need to evaluate the template arguments, even though this
7989 template-id may be nested within a "sizeof". */
7990 saved_unevaluated_operand = cp_unevaluated_operand;
7991 cp_unevaluated_operand = 0;
7992 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7993 c_inhibit_evaluation_warnings = 0;
7994 new_inner_args = make_tree_vec (nparms);
7995 new_args = add_outermost_template_args (args, new_inner_args);
7996 int pack_adjust = 0;
7997 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7998 {
7999 tree arg;
8000 tree parm;
8001
8002 /* Get the Ith template parameter. */
8003 parm = TREE_VEC_ELT (parms, parm_idx);
8004
8005 if (parm == error_mark_node)
8006 {
8007 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8008 continue;
8009 }
8010
8011 /* Calculate the next argument. */
8012 if (arg_idx < nargs)
8013 arg = TREE_VEC_ELT (inner_args, arg_idx);
8014 else
8015 arg = NULL_TREE;
8016
8017 if (template_parameter_pack_p (TREE_VALUE (parm))
8018 && !(arg && ARGUMENT_PACK_P (arg)))
8019 {
8020 /* Some arguments will be placed in the
8021 template parameter pack PARM. */
8022 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8023 inner_args, arg_idx,
8024 new_args, &lost,
8025 in_decl, complain);
8026
8027 if (arg == NULL_TREE)
8028 {
8029 /* We don't know how many args we have yet, just use the
8030 unconverted (and still packed) ones for now. */
8031 new_inner_args = orig_inner_args;
8032 arg_idx = nargs;
8033 break;
8034 }
8035
8036 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8037
8038 /* Store this argument. */
8039 if (arg == error_mark_node)
8040 {
8041 lost++;
8042 /* We are done with all of the arguments. */
8043 arg_idx = nargs;
8044 }
8045 else
8046 {
8047 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8048 arg_idx += pack_adjust;
8049 }
8050
8051 continue;
8052 }
8053 else if (arg)
8054 {
8055 if (PACK_EXPANSION_P (arg))
8056 {
8057 /* "If every valid specialization of a variadic template
8058 requires an empty template parameter pack, the template is
8059 ill-formed, no diagnostic required." So check that the
8060 pattern works with this parameter. */
8061 tree pattern = PACK_EXPANSION_PATTERN (arg);
8062 tree conv = convert_template_argument (TREE_VALUE (parm),
8063 pattern, new_args,
8064 complain, parm_idx,
8065 in_decl);
8066 if (conv == error_mark_node)
8067 {
8068 if (complain & tf_error)
8069 inform (input_location, "so any instantiation with a "
8070 "non-empty parameter pack would be ill-formed");
8071 ++lost;
8072 }
8073 else if (TYPE_P (conv) && !TYPE_P (pattern))
8074 /* Recover from missing typename. */
8075 TREE_VEC_ELT (inner_args, arg_idx)
8076 = make_pack_expansion (conv);
8077
8078 /* We don't know how many args we have yet, just
8079 use the unconverted ones for now. */
8080 new_inner_args = inner_args;
8081 arg_idx = nargs;
8082 break;
8083 }
8084 }
8085 else if (require_all_args)
8086 {
8087 /* There must be a default arg in this case. */
8088 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8089 complain, in_decl);
8090 /* The position of the first default template argument,
8091 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8092 Record that. */
8093 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8094 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8095 arg_idx - pack_adjust);
8096 }
8097 else
8098 break;
8099
8100 if (arg == error_mark_node)
8101 {
8102 if (complain & tf_error)
8103 error ("template argument %d is invalid", arg_idx + 1);
8104 }
8105 else if (!arg)
8106 /* This only occurs if there was an error in the template
8107 parameter list itself (which we would already have
8108 reported) that we are trying to recover from, e.g., a class
8109 template with a parameter list such as
8110 template<typename..., typename>. */
8111 ++lost;
8112 else
8113 arg = convert_template_argument (TREE_VALUE (parm),
8114 arg, new_args, complain,
8115 parm_idx, in_decl);
8116
8117 if (arg == error_mark_node)
8118 lost++;
8119 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8120 }
8121 cp_unevaluated_operand = saved_unevaluated_operand;
8122 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8123
8124 if (variadic_p && arg_idx < nargs)
8125 {
8126 if (complain & tf_error)
8127 {
8128 error ("wrong number of template arguments "
8129 "(%d, should be %d)", nargs, arg_idx);
8130 if (in_decl)
8131 error ("provided for %q+D", in_decl);
8132 }
8133 return error_mark_node;
8134 }
8135
8136 if (lost)
8137 return error_mark_node;
8138
8139 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8140 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8141 TREE_VEC_LENGTH (new_inner_args));
8142
8143 return new_inner_args;
8144 }
8145
8146 /* Convert all template arguments to their appropriate types, and
8147 return a vector containing the innermost resulting template
8148 arguments. If any error occurs, return error_mark_node. Error and
8149 warning messages are not issued.
8150
8151 Note that no function argument deduction is performed, and default
8152 arguments are used to fill in unspecified arguments. */
8153 tree
8154 coerce_template_parms (tree parms, tree args, tree in_decl)
8155 {
8156 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8157 }
8158
8159 /* Convert all template arguments to their appropriate type, and
8160 instantiate default arguments as needed. This returns a vector
8161 containing the innermost resulting template arguments, or
8162 error_mark_node if unsuccessful. */
8163 tree
8164 coerce_template_parms (tree parms, tree args, tree in_decl,
8165 tsubst_flags_t complain)
8166 {
8167 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8168 }
8169
8170 /* Like coerce_template_parms. If PARMS represents all template
8171 parameters levels, this function returns a vector of vectors
8172 representing all the resulting argument levels. Note that in this
8173 case, only the innermost arguments are coerced because the
8174 outermost ones are supposed to have been coerced already.
8175
8176 Otherwise, if PARMS represents only (the innermost) vector of
8177 parameters, this function returns a vector containing just the
8178 innermost resulting arguments. */
8179
8180 static tree
8181 coerce_innermost_template_parms (tree parms,
8182 tree args,
8183 tree in_decl,
8184 tsubst_flags_t complain,
8185 bool require_all_args,
8186 bool use_default_args)
8187 {
8188 int parms_depth = TMPL_PARMS_DEPTH (parms);
8189 int args_depth = TMPL_ARGS_DEPTH (args);
8190 tree coerced_args;
8191
8192 if (parms_depth > 1)
8193 {
8194 coerced_args = make_tree_vec (parms_depth);
8195 tree level;
8196 int cur_depth;
8197
8198 for (level = parms, cur_depth = parms_depth;
8199 parms_depth > 0 && level != NULL_TREE;
8200 level = TREE_CHAIN (level), --cur_depth)
8201 {
8202 tree l;
8203 if (cur_depth == args_depth)
8204 l = coerce_template_parms (TREE_VALUE (level),
8205 args, in_decl, complain,
8206 require_all_args,
8207 use_default_args);
8208 else
8209 l = TMPL_ARGS_LEVEL (args, cur_depth);
8210
8211 if (l == error_mark_node)
8212 return error_mark_node;
8213
8214 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8215 }
8216 }
8217 else
8218 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8219 args, in_decl, complain,
8220 require_all_args,
8221 use_default_args);
8222 return coerced_args;
8223 }
8224
8225 /* Returns 1 if template args OT and NT are equivalent. */
8226
8227 int
8228 template_args_equal (tree ot, tree nt)
8229 {
8230 if (nt == ot)
8231 return 1;
8232 if (nt == NULL_TREE || ot == NULL_TREE)
8233 return false;
8234 if (nt == any_targ_node || ot == any_targ_node)
8235 return true;
8236
8237 if (TREE_CODE (nt) == TREE_VEC)
8238 /* For member templates */
8239 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8240 else if (PACK_EXPANSION_P (ot))
8241 return (PACK_EXPANSION_P (nt)
8242 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8243 PACK_EXPANSION_PATTERN (nt))
8244 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8245 PACK_EXPANSION_EXTRA_ARGS (nt)));
8246 else if (ARGUMENT_PACK_P (ot))
8247 {
8248 int i, len;
8249 tree opack, npack;
8250
8251 if (!ARGUMENT_PACK_P (nt))
8252 return 0;
8253
8254 opack = ARGUMENT_PACK_ARGS (ot);
8255 npack = ARGUMENT_PACK_ARGS (nt);
8256 len = TREE_VEC_LENGTH (opack);
8257 if (TREE_VEC_LENGTH (npack) != len)
8258 return 0;
8259 for (i = 0; i < len; ++i)
8260 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8261 TREE_VEC_ELT (npack, i)))
8262 return 0;
8263 return 1;
8264 }
8265 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8266 gcc_unreachable ();
8267 else if (TYPE_P (nt))
8268 {
8269 if (!TYPE_P (ot))
8270 return false;
8271 /* Don't treat an alias template specialization with dependent
8272 arguments as equivalent to its underlying type when used as a
8273 template argument; we need them to be distinct so that we
8274 substitute into the specialization arguments at instantiation
8275 time. And aliases can't be equivalent without being ==, so
8276 we don't need to look any deeper. */
8277 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
8278 return false;
8279 else
8280 return same_type_p (ot, nt);
8281 }
8282 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8283 return 0;
8284 else
8285 {
8286 /* Try to treat a template non-type argument that has been converted
8287 to the parameter type as equivalent to one that hasn't yet. */
8288 for (enum tree_code code1 = TREE_CODE (ot);
8289 CONVERT_EXPR_CODE_P (code1)
8290 || code1 == NON_LVALUE_EXPR;
8291 code1 = TREE_CODE (ot))
8292 ot = TREE_OPERAND (ot, 0);
8293 for (enum tree_code code2 = TREE_CODE (nt);
8294 CONVERT_EXPR_CODE_P (code2)
8295 || code2 == NON_LVALUE_EXPR;
8296 code2 = TREE_CODE (nt))
8297 nt = TREE_OPERAND (nt, 0);
8298
8299 return cp_tree_equal (ot, nt);
8300 }
8301 }
8302
8303 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8304 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8305 NEWARG_PTR with the offending arguments if they are non-NULL. */
8306
8307 int
8308 comp_template_args (tree oldargs, tree newargs,
8309 tree *oldarg_ptr, tree *newarg_ptr)
8310 {
8311 int i;
8312
8313 if (oldargs == newargs)
8314 return 1;
8315
8316 if (!oldargs || !newargs)
8317 return 0;
8318
8319 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8320 return 0;
8321
8322 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8323 {
8324 tree nt = TREE_VEC_ELT (newargs, i);
8325 tree ot = TREE_VEC_ELT (oldargs, i);
8326
8327 if (! template_args_equal (ot, nt))
8328 {
8329 if (oldarg_ptr != NULL)
8330 *oldarg_ptr = ot;
8331 if (newarg_ptr != NULL)
8332 *newarg_ptr = nt;
8333 return 0;
8334 }
8335 }
8336 return 1;
8337 }
8338
8339 static void
8340 add_pending_template (tree d)
8341 {
8342 tree ti = (TYPE_P (d)
8343 ? CLASSTYPE_TEMPLATE_INFO (d)
8344 : DECL_TEMPLATE_INFO (d));
8345 struct pending_template *pt;
8346 int level;
8347
8348 if (TI_PENDING_TEMPLATE_FLAG (ti))
8349 return;
8350
8351 /* We are called both from instantiate_decl, where we've already had a
8352 tinst_level pushed, and instantiate_template, where we haven't.
8353 Compensate. */
8354 level = !current_tinst_level || current_tinst_level->decl != d;
8355
8356 if (level)
8357 push_tinst_level (d);
8358
8359 pt = ggc_alloc<pending_template> ();
8360 pt->next = NULL;
8361 pt->tinst = current_tinst_level;
8362 if (last_pending_template)
8363 last_pending_template->next = pt;
8364 else
8365 pending_templates = pt;
8366
8367 last_pending_template = pt;
8368
8369 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8370
8371 if (level)
8372 pop_tinst_level ();
8373 }
8374
8375
8376 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8377 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8378 documentation for TEMPLATE_ID_EXPR. */
8379
8380 tree
8381 lookup_template_function (tree fns, tree arglist)
8382 {
8383 tree type;
8384
8385 if (fns == error_mark_node || arglist == error_mark_node)
8386 return error_mark_node;
8387
8388 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8389
8390 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8391 {
8392 error ("%q#D is not a function template", fns);
8393 return error_mark_node;
8394 }
8395
8396 if (BASELINK_P (fns))
8397 {
8398 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8399 unknown_type_node,
8400 BASELINK_FUNCTIONS (fns),
8401 arglist);
8402 return fns;
8403 }
8404
8405 type = TREE_TYPE (fns);
8406 if (TREE_CODE (fns) == OVERLOAD || !type)
8407 type = unknown_type_node;
8408
8409 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8410 }
8411
8412 /* Within the scope of a template class S<T>, the name S gets bound
8413 (in build_self_reference) to a TYPE_DECL for the class, not a
8414 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8415 or one of its enclosing classes, and that type is a template,
8416 return the associated TEMPLATE_DECL. Otherwise, the original
8417 DECL is returned.
8418
8419 Also handle the case when DECL is a TREE_LIST of ambiguous
8420 injected-class-names from different bases. */
8421
8422 tree
8423 maybe_get_template_decl_from_type_decl (tree decl)
8424 {
8425 if (decl == NULL_TREE)
8426 return decl;
8427
8428 /* DR 176: A lookup that finds an injected-class-name (10.2
8429 [class.member.lookup]) can result in an ambiguity in certain cases
8430 (for example, if it is found in more than one base class). If all of
8431 the injected-class-names that are found refer to specializations of
8432 the same class template, and if the name is followed by a
8433 template-argument-list, the reference refers to the class template
8434 itself and not a specialization thereof, and is not ambiguous. */
8435 if (TREE_CODE (decl) == TREE_LIST)
8436 {
8437 tree t, tmpl = NULL_TREE;
8438 for (t = decl; t; t = TREE_CHAIN (t))
8439 {
8440 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8441 if (!tmpl)
8442 tmpl = elt;
8443 else if (tmpl != elt)
8444 break;
8445 }
8446 if (tmpl && t == NULL_TREE)
8447 return tmpl;
8448 else
8449 return decl;
8450 }
8451
8452 return (decl != NULL_TREE
8453 && DECL_SELF_REFERENCE_P (decl)
8454 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8455 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8456 }
8457
8458 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8459 parameters, find the desired type.
8460
8461 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8462
8463 IN_DECL, if non-NULL, is the template declaration we are trying to
8464 instantiate.
8465
8466 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8467 the class we are looking up.
8468
8469 Issue error and warning messages under control of COMPLAIN.
8470
8471 If the template class is really a local class in a template
8472 function, then the FUNCTION_CONTEXT is the function in which it is
8473 being instantiated.
8474
8475 ??? Note that this function is currently called *twice* for each
8476 template-id: the first time from the parser, while creating the
8477 incomplete type (finish_template_type), and the second type during the
8478 real instantiation (instantiate_template_class). This is surely something
8479 that we want to avoid. It also causes some problems with argument
8480 coercion (see convert_nontype_argument for more information on this). */
8481
8482 static tree
8483 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8484 int entering_scope, tsubst_flags_t complain)
8485 {
8486 tree templ = NULL_TREE, parmlist;
8487 tree t;
8488 spec_entry **slot;
8489 spec_entry *entry;
8490 spec_entry elt;
8491 hashval_t hash;
8492
8493 if (identifier_p (d1))
8494 {
8495 tree value = innermost_non_namespace_value (d1);
8496 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8497 templ = value;
8498 else
8499 {
8500 if (context)
8501 push_decl_namespace (context);
8502 templ = lookup_name (d1);
8503 templ = maybe_get_template_decl_from_type_decl (templ);
8504 if (context)
8505 pop_decl_namespace ();
8506 }
8507 if (templ)
8508 context = DECL_CONTEXT (templ);
8509 }
8510 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8511 {
8512 tree type = TREE_TYPE (d1);
8513
8514 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8515 an implicit typename for the second A. Deal with it. */
8516 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8517 type = TREE_TYPE (type);
8518
8519 if (CLASSTYPE_TEMPLATE_INFO (type))
8520 {
8521 templ = CLASSTYPE_TI_TEMPLATE (type);
8522 d1 = DECL_NAME (templ);
8523 }
8524 }
8525 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8526 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8527 {
8528 templ = TYPE_TI_TEMPLATE (d1);
8529 d1 = DECL_NAME (templ);
8530 }
8531 else if (DECL_TYPE_TEMPLATE_P (d1))
8532 {
8533 templ = d1;
8534 d1 = DECL_NAME (templ);
8535 context = DECL_CONTEXT (templ);
8536 }
8537 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8538 {
8539 templ = d1;
8540 d1 = DECL_NAME (templ);
8541 }
8542
8543 /* Issue an error message if we didn't find a template. */
8544 if (! templ)
8545 {
8546 if (complain & tf_error)
8547 error ("%qT is not a template", d1);
8548 return error_mark_node;
8549 }
8550
8551 if (TREE_CODE (templ) != TEMPLATE_DECL
8552 /* Make sure it's a user visible template, if it was named by
8553 the user. */
8554 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8555 && !PRIMARY_TEMPLATE_P (templ)))
8556 {
8557 if (complain & tf_error)
8558 {
8559 error ("non-template type %qT used as a template", d1);
8560 if (in_decl)
8561 error ("for template declaration %q+D", in_decl);
8562 }
8563 return error_mark_node;
8564 }
8565
8566 complain &= ~tf_user;
8567
8568 /* An alias that just changes the name of a template is equivalent to the
8569 other template, so if any of the arguments are pack expansions, strip
8570 the alias to avoid problems with a pack expansion passed to a non-pack
8571 alias template parameter (DR 1430). */
8572 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8573 templ = get_underlying_template (templ);
8574
8575 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8576 {
8577 tree parm;
8578 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8579 if (arglist2 == error_mark_node
8580 || (!uses_template_parms (arglist2)
8581 && check_instantiated_args (templ, arglist2, complain)))
8582 return error_mark_node;
8583
8584 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8585 return parm;
8586 }
8587 else
8588 {
8589 tree template_type = TREE_TYPE (templ);
8590 tree gen_tmpl;
8591 tree type_decl;
8592 tree found = NULL_TREE;
8593 int arg_depth;
8594 int parm_depth;
8595 int is_dependent_type;
8596 int use_partial_inst_tmpl = false;
8597
8598 if (template_type == error_mark_node)
8599 /* An error occurred while building the template TEMPL, and a
8600 diagnostic has most certainly been emitted for that
8601 already. Let's propagate that error. */
8602 return error_mark_node;
8603
8604 gen_tmpl = most_general_template (templ);
8605 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8606 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8607 arg_depth = TMPL_ARGS_DEPTH (arglist);
8608
8609 if (arg_depth == 1 && parm_depth > 1)
8610 {
8611 /* We've been given an incomplete set of template arguments.
8612 For example, given:
8613
8614 template <class T> struct S1 {
8615 template <class U> struct S2 {};
8616 template <class U> struct S2<U*> {};
8617 };
8618
8619 we will be called with an ARGLIST of `U*', but the
8620 TEMPLATE will be `template <class T> template
8621 <class U> struct S1<T>::S2'. We must fill in the missing
8622 arguments. */
8623 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8624 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8625 arg_depth = TMPL_ARGS_DEPTH (arglist);
8626 }
8627
8628 /* Now we should have enough arguments. */
8629 gcc_assert (parm_depth == arg_depth);
8630
8631 /* From here on, we're only interested in the most general
8632 template. */
8633
8634 /* Calculate the BOUND_ARGS. These will be the args that are
8635 actually tsubst'd into the definition to create the
8636 instantiation. */
8637 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8638 complain,
8639 /*require_all_args=*/true,
8640 /*use_default_args=*/true);
8641
8642 if (arglist == error_mark_node)
8643 /* We were unable to bind the arguments. */
8644 return error_mark_node;
8645
8646 /* In the scope of a template class, explicit references to the
8647 template class refer to the type of the template, not any
8648 instantiation of it. For example, in:
8649
8650 template <class T> class C { void f(C<T>); }
8651
8652 the `C<T>' is just the same as `C'. Outside of the
8653 class, however, such a reference is an instantiation. */
8654 if (entering_scope
8655 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8656 || currently_open_class (template_type))
8657 {
8658 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (template_type);
8659
8660 if (comp_template_args (TI_ARGS (tinfo), arglist))
8661 return template_type;
8662 }
8663
8664 /* If we already have this specialization, return it. */
8665 elt.tmpl = gen_tmpl;
8666 elt.args = arglist;
8667 elt.spec = NULL_TREE;
8668 hash = spec_hasher::hash (&elt);
8669 entry = type_specializations->find_with_hash (&elt, hash);
8670
8671 if (entry)
8672 return entry->spec;
8673
8674 /* If the the template's constraints are not satisfied,
8675 then we cannot form a valid type.
8676
8677 Note that the check is deferred until after the hash
8678 lookup. This prevents redundant checks on previously
8679 instantiated specializations. */
8680 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8681 {
8682 if (complain & tf_error)
8683 {
8684 error ("template constraint failure");
8685 diagnose_constraints (input_location, gen_tmpl, arglist);
8686 }
8687 return error_mark_node;
8688 }
8689
8690 is_dependent_type = uses_template_parms (arglist);
8691
8692 /* If the deduced arguments are invalid, then the binding
8693 failed. */
8694 if (!is_dependent_type
8695 && check_instantiated_args (gen_tmpl,
8696 INNERMOST_TEMPLATE_ARGS (arglist),
8697 complain))
8698 return error_mark_node;
8699
8700 if (!is_dependent_type
8701 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8702 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8703 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8704 {
8705 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8706 DECL_NAME (gen_tmpl),
8707 /*tag_scope=*/ts_global);
8708 return found;
8709 }
8710
8711 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8712 complain, in_decl);
8713 if (context == error_mark_node)
8714 return error_mark_node;
8715
8716 if (!context)
8717 context = global_namespace;
8718
8719 /* Create the type. */
8720 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8721 {
8722 /* The user referred to a specialization of an alias
8723 template represented by GEN_TMPL.
8724
8725 [temp.alias]/2 says:
8726
8727 When a template-id refers to the specialization of an
8728 alias template, it is equivalent to the associated
8729 type obtained by substitution of its
8730 template-arguments for the template-parameters in the
8731 type-id of the alias template. */
8732
8733 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8734 /* Note that the call above (by indirectly calling
8735 register_specialization in tsubst_decl) registers the
8736 TYPE_DECL representing the specialization of the alias
8737 template. So next time someone substitutes ARGLIST for
8738 the template parms into the alias template (GEN_TMPL),
8739 she'll get that TYPE_DECL back. */
8740
8741 if (t == error_mark_node)
8742 return t;
8743 }
8744 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8745 {
8746 if (!is_dependent_type)
8747 {
8748 set_current_access_from_decl (TYPE_NAME (template_type));
8749 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8750 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8751 arglist, complain, in_decl),
8752 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8753 arglist, complain, in_decl),
8754 SCOPED_ENUM_P (template_type), NULL);
8755
8756 if (t == error_mark_node)
8757 return t;
8758 }
8759 else
8760 {
8761 /* We don't want to call start_enum for this type, since
8762 the values for the enumeration constants may involve
8763 template parameters. And, no one should be interested
8764 in the enumeration constants for such a type. */
8765 t = cxx_make_type (ENUMERAL_TYPE);
8766 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8767 }
8768 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8769 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8770 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8771 }
8772 else if (CLASS_TYPE_P (template_type))
8773 {
8774 t = make_class_type (TREE_CODE (template_type));
8775 CLASSTYPE_DECLARED_CLASS (t)
8776 = CLASSTYPE_DECLARED_CLASS (template_type);
8777 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8778
8779 /* A local class. Make sure the decl gets registered properly. */
8780 if (context == current_function_decl)
8781 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8782
8783 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8784 /* This instantiation is another name for the primary
8785 template type. Set the TYPE_CANONICAL field
8786 appropriately. */
8787 TYPE_CANONICAL (t) = template_type;
8788 else if (any_template_arguments_need_structural_equality_p (arglist))
8789 /* Some of the template arguments require structural
8790 equality testing, so this template class requires
8791 structural equality testing. */
8792 SET_TYPE_STRUCTURAL_EQUALITY (t);
8793 }
8794 else
8795 gcc_unreachable ();
8796
8797 /* If we called start_enum or pushtag above, this information
8798 will already be set up. */
8799 if (!TYPE_NAME (t))
8800 {
8801 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8802
8803 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8804 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8805 DECL_SOURCE_LOCATION (type_decl)
8806 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8807 }
8808 else
8809 type_decl = TYPE_NAME (t);
8810
8811 if (CLASS_TYPE_P (template_type))
8812 {
8813 TREE_PRIVATE (type_decl)
8814 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8815 TREE_PROTECTED (type_decl)
8816 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8817 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8818 {
8819 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8820 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8821 }
8822 }
8823
8824 if (OVERLOAD_TYPE_P (t)
8825 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8826 {
8827 static const char *tags[] = {"abi_tag", "may_alias"};
8828
8829 for (unsigned ix = 0; ix != 2; ix++)
8830 {
8831 tree attributes
8832 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8833
8834 if (attributes)
8835 TYPE_ATTRIBUTES (t)
8836 = tree_cons (TREE_PURPOSE (attributes),
8837 TREE_VALUE (attributes),
8838 TYPE_ATTRIBUTES (t));
8839 }
8840 }
8841
8842 /* Let's consider the explicit specialization of a member
8843 of a class template specialization that is implicitly instantiated,
8844 e.g.:
8845 template<class T>
8846 struct S
8847 {
8848 template<class U> struct M {}; //#0
8849 };
8850
8851 template<>
8852 template<>
8853 struct S<int>::M<char> //#1
8854 {
8855 int i;
8856 };
8857 [temp.expl.spec]/4 says this is valid.
8858
8859 In this case, when we write:
8860 S<int>::M<char> m;
8861
8862 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8863 the one of #0.
8864
8865 When we encounter #1, we want to store the partial instantiation
8866 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8867
8868 For all cases other than this "explicit specialization of member of a
8869 class template", we just want to store the most general template into
8870 the CLASSTYPE_TI_TEMPLATE of M.
8871
8872 This case of "explicit specialization of member of a class template"
8873 only happens when:
8874 1/ the enclosing class is an instantiation of, and therefore not
8875 the same as, the context of the most general template, and
8876 2/ we aren't looking at the partial instantiation itself, i.e.
8877 the innermost arguments are not the same as the innermost parms of
8878 the most general template.
8879
8880 So it's only when 1/ and 2/ happens that we want to use the partial
8881 instantiation of the member template in lieu of its most general
8882 template. */
8883
8884 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8885 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8886 /* the enclosing class must be an instantiation... */
8887 && CLASS_TYPE_P (context)
8888 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8889 {
8890 TREE_VEC_LENGTH (arglist)--;
8891 ++processing_template_decl;
8892 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
8893 tree partial_inst_args =
8894 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
8895 arglist, complain, NULL_TREE);
8896 --processing_template_decl;
8897 TREE_VEC_LENGTH (arglist)++;
8898 if (partial_inst_args == error_mark_node)
8899 return error_mark_node;
8900 use_partial_inst_tmpl =
8901 /*...and we must not be looking at the partial instantiation
8902 itself. */
8903 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8904 partial_inst_args);
8905 }
8906
8907 if (!use_partial_inst_tmpl)
8908 /* This case is easy; there are no member templates involved. */
8909 found = gen_tmpl;
8910 else
8911 {
8912 /* This is a full instantiation of a member template. Find
8913 the partial instantiation of which this is an instance. */
8914
8915 /* Temporarily reduce by one the number of levels in the ARGLIST
8916 so as to avoid comparing the last set of arguments. */
8917 TREE_VEC_LENGTH (arglist)--;
8918 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8919 TREE_VEC_LENGTH (arglist)++;
8920 /* FOUND is either a proper class type, or an alias
8921 template specialization. In the later case, it's a
8922 TYPE_DECL, resulting from the substituting of arguments
8923 for parameters in the TYPE_DECL of the alias template
8924 done earlier. So be careful while getting the template
8925 of FOUND. */
8926 found = (TREE_CODE (found) == TEMPLATE_DECL
8927 ? found
8928 : (TREE_CODE (found) == TYPE_DECL
8929 ? DECL_TI_TEMPLATE (found)
8930 : CLASSTYPE_TI_TEMPLATE (found)));
8931 }
8932
8933 // Build template info for the new specialization.
8934 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8935
8936 elt.spec = t;
8937 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8938 entry = ggc_alloc<spec_entry> ();
8939 *entry = elt;
8940 *slot = entry;
8941
8942 /* Note this use of the partial instantiation so we can check it
8943 later in maybe_process_partial_specialization. */
8944 DECL_TEMPLATE_INSTANTIATIONS (found)
8945 = tree_cons (arglist, t,
8946 DECL_TEMPLATE_INSTANTIATIONS (found));
8947
8948 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8949 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8950 /* Now that the type has been registered on the instantiations
8951 list, we set up the enumerators. Because the enumeration
8952 constants may involve the enumeration type itself, we make
8953 sure to register the type first, and then create the
8954 constants. That way, doing tsubst_expr for the enumeration
8955 constants won't result in recursive calls here; we'll find
8956 the instantiation and exit above. */
8957 tsubst_enum (template_type, t, arglist);
8958
8959 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8960 /* If the type makes use of template parameters, the
8961 code that generates debugging information will crash. */
8962 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8963
8964 /* Possibly limit visibility based on template args. */
8965 TREE_PUBLIC (type_decl) = 1;
8966 determine_visibility (type_decl);
8967
8968 inherit_targ_abi_tags (t);
8969
8970 return t;
8971 }
8972 }
8973
8974 /* Wrapper for lookup_template_class_1. */
8975
8976 tree
8977 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8978 int entering_scope, tsubst_flags_t complain)
8979 {
8980 tree ret;
8981 timevar_push (TV_TEMPLATE_INST);
8982 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8983 entering_scope, complain);
8984 timevar_pop (TV_TEMPLATE_INST);
8985 return ret;
8986 }
8987
8988 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8989
8990 tree
8991 lookup_template_variable (tree templ, tree arglist)
8992 {
8993 /* The type of the expression is NULL_TREE since the template-id could refer
8994 to an explicit or partial specialization. */
8995 tree type = NULL_TREE;
8996 if (flag_concepts && variable_concept_p (templ))
8997 /* Except that concepts are always bool. */
8998 type = boolean_type_node;
8999 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9000 }
9001
9002 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9003
9004 tree
9005 finish_template_variable (tree var, tsubst_flags_t complain)
9006 {
9007 tree templ = TREE_OPERAND (var, 0);
9008 tree arglist = TREE_OPERAND (var, 1);
9009
9010 /* We never want to return a VAR_DECL for a variable concept, since they
9011 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9012 bool concept_p = flag_concepts && variable_concept_p (templ);
9013 if (concept_p && processing_template_decl)
9014 return var;
9015
9016 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9017 arglist = add_outermost_template_args (tmpl_args, arglist);
9018
9019 templ = most_general_template (templ);
9020 tree parms = DECL_TEMPLATE_PARMS (templ);
9021 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9022 /*req_all*/true,
9023 /*use_default*/true);
9024
9025 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9026 {
9027 if (complain & tf_error)
9028 {
9029 error ("use of invalid variable template %qE", var);
9030 diagnose_constraints (location_of (var), templ, arglist);
9031 }
9032 return error_mark_node;
9033 }
9034
9035 /* If a template-id refers to a specialization of a variable
9036 concept, then the expression is true if and only if the
9037 concept's constraints are satisfied by the given template
9038 arguments.
9039
9040 NOTE: This is an extension of Concepts Lite TS that
9041 allows constraints to be used in expressions. */
9042 if (concept_p)
9043 {
9044 tree decl = DECL_TEMPLATE_RESULT (templ);
9045 return evaluate_variable_concept (decl, arglist);
9046 }
9047
9048 return instantiate_template (templ, arglist, complain);
9049 }
9050
9051 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9052 TARGS template args, and instantiate it if it's not dependent. */
9053
9054 tree
9055 lookup_and_finish_template_variable (tree templ, tree targs,
9056 tsubst_flags_t complain)
9057 {
9058 templ = lookup_template_variable (templ, targs);
9059 if (!any_dependent_template_arguments_p (targs))
9060 {
9061 templ = finish_template_variable (templ, complain);
9062 mark_used (templ);
9063 }
9064
9065 return convert_from_reference (templ);
9066 }
9067
9068 \f
9069 struct pair_fn_data
9070 {
9071 tree_fn_t fn;
9072 tree_fn_t any_fn;
9073 void *data;
9074 /* True when we should also visit template parameters that occur in
9075 non-deduced contexts. */
9076 bool include_nondeduced_p;
9077 hash_set<tree> *visited;
9078 };
9079
9080 /* Called from for_each_template_parm via walk_tree. */
9081
9082 static tree
9083 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9084 {
9085 tree t = *tp;
9086 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9087 tree_fn_t fn = pfd->fn;
9088 void *data = pfd->data;
9089 tree result = NULL_TREE;
9090
9091 #define WALK_SUBTREE(NODE) \
9092 do \
9093 { \
9094 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9095 pfd->include_nondeduced_p, \
9096 pfd->any_fn); \
9097 if (result) goto out; \
9098 } \
9099 while (0)
9100
9101 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9102 return t;
9103
9104 if (TYPE_P (t)
9105 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9106 WALK_SUBTREE (TYPE_CONTEXT (t));
9107
9108 switch (TREE_CODE (t))
9109 {
9110 case RECORD_TYPE:
9111 if (TYPE_PTRMEMFUNC_P (t))
9112 break;
9113 /* Fall through. */
9114
9115 case UNION_TYPE:
9116 case ENUMERAL_TYPE:
9117 if (!TYPE_TEMPLATE_INFO (t))
9118 *walk_subtrees = 0;
9119 else
9120 WALK_SUBTREE (TYPE_TI_ARGS (t));
9121 break;
9122
9123 case INTEGER_TYPE:
9124 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9125 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9126 break;
9127
9128 case METHOD_TYPE:
9129 /* Since we're not going to walk subtrees, we have to do this
9130 explicitly here. */
9131 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9132 /* Fall through. */
9133
9134 case FUNCTION_TYPE:
9135 /* Check the return type. */
9136 WALK_SUBTREE (TREE_TYPE (t));
9137
9138 /* Check the parameter types. Since default arguments are not
9139 instantiated until they are needed, the TYPE_ARG_TYPES may
9140 contain expressions that involve template parameters. But,
9141 no-one should be looking at them yet. And, once they're
9142 instantiated, they don't contain template parameters, so
9143 there's no point in looking at them then, either. */
9144 {
9145 tree parm;
9146
9147 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9148 WALK_SUBTREE (TREE_VALUE (parm));
9149
9150 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9151 want walk_tree walking into them itself. */
9152 *walk_subtrees = 0;
9153 }
9154
9155 if (flag_noexcept_type)
9156 {
9157 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9158 if (spec)
9159 WALK_SUBTREE (TREE_PURPOSE (spec));
9160 }
9161 break;
9162
9163 case TYPEOF_TYPE:
9164 case UNDERLYING_TYPE:
9165 if (pfd->include_nondeduced_p
9166 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9167 pfd->visited,
9168 pfd->include_nondeduced_p,
9169 pfd->any_fn))
9170 return error_mark_node;
9171 break;
9172
9173 case FUNCTION_DECL:
9174 case VAR_DECL:
9175 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9176 WALK_SUBTREE (DECL_TI_ARGS (t));
9177 /* Fall through. */
9178
9179 case PARM_DECL:
9180 case CONST_DECL:
9181 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9182 WALK_SUBTREE (DECL_INITIAL (t));
9183 if (DECL_CONTEXT (t)
9184 && pfd->include_nondeduced_p)
9185 WALK_SUBTREE (DECL_CONTEXT (t));
9186 break;
9187
9188 case BOUND_TEMPLATE_TEMPLATE_PARM:
9189 /* Record template parameters such as `T' inside `TT<T>'. */
9190 WALK_SUBTREE (TYPE_TI_ARGS (t));
9191 /* Fall through. */
9192
9193 case TEMPLATE_TEMPLATE_PARM:
9194 case TEMPLATE_TYPE_PARM:
9195 case TEMPLATE_PARM_INDEX:
9196 if (fn && (*fn)(t, data))
9197 return t;
9198 else if (!fn)
9199 return t;
9200 break;
9201
9202 case TEMPLATE_DECL:
9203 /* A template template parameter is encountered. */
9204 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9205 WALK_SUBTREE (TREE_TYPE (t));
9206
9207 /* Already substituted template template parameter */
9208 *walk_subtrees = 0;
9209 break;
9210
9211 case TYPENAME_TYPE:
9212 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9213 partial instantiation. */
9214 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9215 break;
9216
9217 case CONSTRUCTOR:
9218 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9219 && pfd->include_nondeduced_p)
9220 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9221 break;
9222
9223 case INDIRECT_REF:
9224 case COMPONENT_REF:
9225 /* If there's no type, then this thing must be some expression
9226 involving template parameters. */
9227 if (!fn && !TREE_TYPE (t))
9228 return error_mark_node;
9229 break;
9230
9231 case MODOP_EXPR:
9232 case CAST_EXPR:
9233 case IMPLICIT_CONV_EXPR:
9234 case REINTERPRET_CAST_EXPR:
9235 case CONST_CAST_EXPR:
9236 case STATIC_CAST_EXPR:
9237 case DYNAMIC_CAST_EXPR:
9238 case ARROW_EXPR:
9239 case DOTSTAR_EXPR:
9240 case TYPEID_EXPR:
9241 case PSEUDO_DTOR_EXPR:
9242 if (!fn)
9243 return error_mark_node;
9244 break;
9245
9246 default:
9247 break;
9248 }
9249
9250 #undef WALK_SUBTREE
9251
9252 /* We didn't find any template parameters we liked. */
9253 out:
9254 return result;
9255 }
9256
9257 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9258 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9259 call FN with the parameter and the DATA.
9260 If FN returns nonzero, the iteration is terminated, and
9261 for_each_template_parm returns 1. Otherwise, the iteration
9262 continues. If FN never returns a nonzero value, the value
9263 returned by for_each_template_parm is 0. If FN is NULL, it is
9264 considered to be the function which always returns 1.
9265
9266 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9267 parameters that occur in non-deduced contexts. When false, only
9268 visits those template parameters that can be deduced. */
9269
9270 static tree
9271 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9272 hash_set<tree> *visited,
9273 bool include_nondeduced_p,
9274 tree_fn_t any_fn)
9275 {
9276 struct pair_fn_data pfd;
9277 tree result;
9278
9279 /* Set up. */
9280 pfd.fn = fn;
9281 pfd.any_fn = any_fn;
9282 pfd.data = data;
9283 pfd.include_nondeduced_p = include_nondeduced_p;
9284
9285 /* Walk the tree. (Conceptually, we would like to walk without
9286 duplicates, but for_each_template_parm_r recursively calls
9287 for_each_template_parm, so we would need to reorganize a fair
9288 bit to use walk_tree_without_duplicates, so we keep our own
9289 visited list.) */
9290 if (visited)
9291 pfd.visited = visited;
9292 else
9293 pfd.visited = new hash_set<tree>;
9294 result = cp_walk_tree (&t,
9295 for_each_template_parm_r,
9296 &pfd,
9297 pfd.visited);
9298
9299 /* Clean up. */
9300 if (!visited)
9301 {
9302 delete pfd.visited;
9303 pfd.visited = 0;
9304 }
9305
9306 return result;
9307 }
9308
9309 /* Returns true if T depends on any template parameter. */
9310
9311 int
9312 uses_template_parms (tree t)
9313 {
9314 if (t == NULL_TREE)
9315 return false;
9316
9317 bool dependent_p;
9318 int saved_processing_template_decl;
9319
9320 saved_processing_template_decl = processing_template_decl;
9321 if (!saved_processing_template_decl)
9322 processing_template_decl = 1;
9323 if (TYPE_P (t))
9324 dependent_p = dependent_type_p (t);
9325 else if (TREE_CODE (t) == TREE_VEC)
9326 dependent_p = any_dependent_template_arguments_p (t);
9327 else if (TREE_CODE (t) == TREE_LIST)
9328 dependent_p = (uses_template_parms (TREE_VALUE (t))
9329 || uses_template_parms (TREE_CHAIN (t)));
9330 else if (TREE_CODE (t) == TYPE_DECL)
9331 dependent_p = dependent_type_p (TREE_TYPE (t));
9332 else if (DECL_P (t)
9333 || EXPR_P (t)
9334 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9335 || TREE_CODE (t) == OVERLOAD
9336 || BASELINK_P (t)
9337 || identifier_p (t)
9338 || TREE_CODE (t) == TRAIT_EXPR
9339 || TREE_CODE (t) == CONSTRUCTOR
9340 || CONSTANT_CLASS_P (t))
9341 dependent_p = (type_dependent_expression_p (t)
9342 || value_dependent_expression_p (t));
9343 else
9344 {
9345 gcc_assert (t == error_mark_node);
9346 dependent_p = false;
9347 }
9348
9349 processing_template_decl = saved_processing_template_decl;
9350
9351 return dependent_p;
9352 }
9353
9354 /* Returns true iff current_function_decl is an incompletely instantiated
9355 template. Useful instead of processing_template_decl because the latter
9356 is set to 0 during instantiate_non_dependent_expr. */
9357
9358 bool
9359 in_template_function (void)
9360 {
9361 tree fn = current_function_decl;
9362 bool ret;
9363 ++processing_template_decl;
9364 ret = (fn && DECL_LANG_SPECIFIC (fn)
9365 && DECL_TEMPLATE_INFO (fn)
9366 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9367 --processing_template_decl;
9368 return ret;
9369 }
9370
9371 /* Returns true if T depends on any template parameter with level LEVEL. */
9372
9373 bool
9374 uses_template_parms_level (tree t, int level)
9375 {
9376 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9377 /*include_nondeduced_p=*/true);
9378 }
9379
9380 /* Returns true if the signature of DECL depends on any template parameter from
9381 its enclosing class. */
9382
9383 bool
9384 uses_outer_template_parms (tree decl)
9385 {
9386 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9387 if (depth == 0)
9388 return false;
9389 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9390 &depth, NULL, /*include_nondeduced_p=*/true))
9391 return true;
9392 if (PRIMARY_TEMPLATE_P (decl)
9393 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9394 (DECL_TEMPLATE_PARMS (decl)),
9395 template_parm_outer_level,
9396 &depth, NULL, /*include_nondeduced_p=*/true))
9397 return true;
9398 tree ci = get_constraints (decl);
9399 if (ci)
9400 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9401 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9402 &depth, NULL, /*nondeduced*/true))
9403 return true;
9404 return false;
9405 }
9406
9407 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9408 ill-formed translation unit, i.e. a variable or function that isn't
9409 usable in a constant expression. */
9410
9411 static inline bool
9412 neglectable_inst_p (tree d)
9413 {
9414 return (DECL_P (d)
9415 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9416 : decl_maybe_constant_var_p (d)));
9417 }
9418
9419 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9420 neglectable and instantiated from within an erroneous instantiation. */
9421
9422 static bool
9423 limit_bad_template_recursion (tree decl)
9424 {
9425 struct tinst_level *lev = current_tinst_level;
9426 int errs = errorcount + sorrycount;
9427 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9428 return false;
9429
9430 for (; lev; lev = lev->next)
9431 if (neglectable_inst_p (lev->decl))
9432 break;
9433
9434 return (lev && errs > lev->errors);
9435 }
9436
9437 static int tinst_depth;
9438 extern int max_tinst_depth;
9439 int depth_reached;
9440
9441 static GTY(()) struct tinst_level *last_error_tinst_level;
9442
9443 /* We're starting to instantiate D; record the template instantiation context
9444 for diagnostics and to restore it later. */
9445
9446 bool
9447 push_tinst_level (tree d)
9448 {
9449 return push_tinst_level_loc (d, input_location);
9450 }
9451
9452 /* We're starting to instantiate D; record the template instantiation context
9453 at LOC for diagnostics and to restore it later. */
9454
9455 bool
9456 push_tinst_level_loc (tree d, location_t loc)
9457 {
9458 struct tinst_level *new_level;
9459
9460 if (tinst_depth >= max_tinst_depth)
9461 {
9462 /* Tell error.c not to try to instantiate any templates. */
9463 at_eof = 2;
9464 fatal_error (input_location,
9465 "template instantiation depth exceeds maximum of %d"
9466 " (use -ftemplate-depth= to increase the maximum)",
9467 max_tinst_depth);
9468 return false;
9469 }
9470
9471 /* If the current instantiation caused problems, don't let it instantiate
9472 anything else. Do allow deduction substitution and decls usable in
9473 constant expressions. */
9474 if (limit_bad_template_recursion (d))
9475 return false;
9476
9477 /* When not -quiet, dump template instantiations other than functions, since
9478 announce_function will take care of those. */
9479 if (!quiet_flag
9480 && TREE_CODE (d) != TREE_LIST
9481 && TREE_CODE (d) != FUNCTION_DECL)
9482 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9483
9484 new_level = ggc_alloc<tinst_level> ();
9485 new_level->decl = d;
9486 new_level->locus = loc;
9487 new_level->errors = errorcount+sorrycount;
9488 new_level->in_system_header_p = in_system_header_at (input_location);
9489 new_level->next = current_tinst_level;
9490 current_tinst_level = new_level;
9491
9492 ++tinst_depth;
9493 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9494 depth_reached = tinst_depth;
9495
9496 return true;
9497 }
9498
9499 /* We're done instantiating this template; return to the instantiation
9500 context. */
9501
9502 void
9503 pop_tinst_level (void)
9504 {
9505 /* Restore the filename and line number stashed away when we started
9506 this instantiation. */
9507 input_location = current_tinst_level->locus;
9508 current_tinst_level = current_tinst_level->next;
9509 --tinst_depth;
9510 }
9511
9512 /* We're instantiating a deferred template; restore the template
9513 instantiation context in which the instantiation was requested, which
9514 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9515
9516 static tree
9517 reopen_tinst_level (struct tinst_level *level)
9518 {
9519 struct tinst_level *t;
9520
9521 tinst_depth = 0;
9522 for (t = level; t; t = t->next)
9523 ++tinst_depth;
9524
9525 current_tinst_level = level;
9526 pop_tinst_level ();
9527 if (current_tinst_level)
9528 current_tinst_level->errors = errorcount+sorrycount;
9529 return level->decl;
9530 }
9531
9532 /* Returns the TINST_LEVEL which gives the original instantiation
9533 context. */
9534
9535 struct tinst_level *
9536 outermost_tinst_level (void)
9537 {
9538 struct tinst_level *level = current_tinst_level;
9539 if (level)
9540 while (level->next)
9541 level = level->next;
9542 return level;
9543 }
9544
9545 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9546 vector of template arguments, as for tsubst.
9547
9548 Returns an appropriate tsubst'd friend declaration. */
9549
9550 static tree
9551 tsubst_friend_function (tree decl, tree args)
9552 {
9553 tree new_friend;
9554
9555 if (TREE_CODE (decl) == FUNCTION_DECL
9556 && DECL_TEMPLATE_INSTANTIATION (decl)
9557 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9558 /* This was a friend declared with an explicit template
9559 argument list, e.g.:
9560
9561 friend void f<>(T);
9562
9563 to indicate that f was a template instantiation, not a new
9564 function declaration. Now, we have to figure out what
9565 instantiation of what template. */
9566 {
9567 tree template_id, arglist, fns;
9568 tree new_args;
9569 tree tmpl;
9570 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9571
9572 /* Friend functions are looked up in the containing namespace scope.
9573 We must enter that scope, to avoid finding member functions of the
9574 current class with same name. */
9575 push_nested_namespace (ns);
9576 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9577 tf_warning_or_error, NULL_TREE,
9578 /*integral_constant_expression_p=*/false);
9579 pop_nested_namespace (ns);
9580 arglist = tsubst (DECL_TI_ARGS (decl), args,
9581 tf_warning_or_error, NULL_TREE);
9582 template_id = lookup_template_function (fns, arglist);
9583
9584 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9585 tmpl = determine_specialization (template_id, new_friend,
9586 &new_args,
9587 /*need_member_template=*/0,
9588 TREE_VEC_LENGTH (args),
9589 tsk_none);
9590 return instantiate_template (tmpl, new_args, tf_error);
9591 }
9592
9593 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9594
9595 /* The NEW_FRIEND will look like an instantiation, to the
9596 compiler, but is not an instantiation from the point of view of
9597 the language. For example, we might have had:
9598
9599 template <class T> struct S {
9600 template <class U> friend void f(T, U);
9601 };
9602
9603 Then, in S<int>, template <class U> void f(int, U) is not an
9604 instantiation of anything. */
9605 if (new_friend == error_mark_node)
9606 return error_mark_node;
9607
9608 DECL_USE_TEMPLATE (new_friend) = 0;
9609 if (TREE_CODE (decl) == TEMPLATE_DECL)
9610 {
9611 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9612 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9613 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9614 }
9615
9616 /* The mangled name for the NEW_FRIEND is incorrect. The function
9617 is not a template instantiation and should not be mangled like
9618 one. Therefore, we forget the mangling here; we'll recompute it
9619 later if we need it. */
9620 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9621 {
9622 SET_DECL_RTL (new_friend, NULL);
9623 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9624 }
9625
9626 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9627 {
9628 tree old_decl;
9629 tree new_friend_template_info;
9630 tree new_friend_result_template_info;
9631 tree ns;
9632 int new_friend_is_defn;
9633
9634 /* We must save some information from NEW_FRIEND before calling
9635 duplicate decls since that function will free NEW_FRIEND if
9636 possible. */
9637 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9638 new_friend_is_defn =
9639 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9640 (template_for_substitution (new_friend)))
9641 != NULL_TREE);
9642 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9643 {
9644 /* This declaration is a `primary' template. */
9645 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9646
9647 new_friend_result_template_info
9648 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9649 }
9650 else
9651 new_friend_result_template_info = NULL_TREE;
9652
9653 /* Inside pushdecl_namespace_level, we will push into the
9654 current namespace. However, the friend function should go
9655 into the namespace of the template. */
9656 ns = decl_namespace_context (new_friend);
9657 push_nested_namespace (ns);
9658 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9659 pop_nested_namespace (ns);
9660
9661 if (old_decl == error_mark_node)
9662 return error_mark_node;
9663
9664 if (old_decl != new_friend)
9665 {
9666 /* This new friend declaration matched an existing
9667 declaration. For example, given:
9668
9669 template <class T> void f(T);
9670 template <class U> class C {
9671 template <class T> friend void f(T) {}
9672 };
9673
9674 the friend declaration actually provides the definition
9675 of `f', once C has been instantiated for some type. So,
9676 old_decl will be the out-of-class template declaration,
9677 while new_friend is the in-class definition.
9678
9679 But, if `f' was called before this point, the
9680 instantiation of `f' will have DECL_TI_ARGS corresponding
9681 to `T' but not to `U', references to which might appear
9682 in the definition of `f'. Previously, the most general
9683 template for an instantiation of `f' was the out-of-class
9684 version; now it is the in-class version. Therefore, we
9685 run through all specialization of `f', adding to their
9686 DECL_TI_ARGS appropriately. In particular, they need a
9687 new set of outer arguments, corresponding to the
9688 arguments for this class instantiation.
9689
9690 The same situation can arise with something like this:
9691
9692 friend void f(int);
9693 template <class T> class C {
9694 friend void f(T) {}
9695 };
9696
9697 when `C<int>' is instantiated. Now, `f(int)' is defined
9698 in the class. */
9699
9700 if (!new_friend_is_defn)
9701 /* On the other hand, if the in-class declaration does
9702 *not* provide a definition, then we don't want to alter
9703 existing definitions. We can just leave everything
9704 alone. */
9705 ;
9706 else
9707 {
9708 tree new_template = TI_TEMPLATE (new_friend_template_info);
9709 tree new_args = TI_ARGS (new_friend_template_info);
9710
9711 /* Overwrite whatever template info was there before, if
9712 any, with the new template information pertaining to
9713 the declaration. */
9714 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9715
9716 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9717 {
9718 /* We should have called reregister_specialization in
9719 duplicate_decls. */
9720 gcc_assert (retrieve_specialization (new_template,
9721 new_args, 0)
9722 == old_decl);
9723
9724 /* Instantiate it if the global has already been used. */
9725 if (DECL_ODR_USED (old_decl))
9726 instantiate_decl (old_decl, /*defer_ok=*/true,
9727 /*expl_inst_class_mem_p=*/false);
9728 }
9729 else
9730 {
9731 tree t;
9732
9733 /* Indicate that the old function template is a partial
9734 instantiation. */
9735 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9736 = new_friend_result_template_info;
9737
9738 gcc_assert (new_template
9739 == most_general_template (new_template));
9740 gcc_assert (new_template != old_decl);
9741
9742 /* Reassign any specializations already in the hash table
9743 to the new more general template, and add the
9744 additional template args. */
9745 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9746 t != NULL_TREE;
9747 t = TREE_CHAIN (t))
9748 {
9749 tree spec = TREE_VALUE (t);
9750 spec_entry elt;
9751
9752 elt.tmpl = old_decl;
9753 elt.args = DECL_TI_ARGS (spec);
9754 elt.spec = NULL_TREE;
9755
9756 decl_specializations->remove_elt (&elt);
9757
9758 DECL_TI_ARGS (spec)
9759 = add_outermost_template_args (new_args,
9760 DECL_TI_ARGS (spec));
9761
9762 register_specialization
9763 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9764
9765 }
9766 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9767 }
9768 }
9769
9770 /* The information from NEW_FRIEND has been merged into OLD_DECL
9771 by duplicate_decls. */
9772 new_friend = old_decl;
9773 }
9774 }
9775 else
9776 {
9777 tree context = DECL_CONTEXT (new_friend);
9778 bool dependent_p;
9779
9780 /* In the code
9781 template <class T> class C {
9782 template <class U> friend void C1<U>::f (); // case 1
9783 friend void C2<T>::f (); // case 2
9784 };
9785 we only need to make sure CONTEXT is a complete type for
9786 case 2. To distinguish between the two cases, we note that
9787 CONTEXT of case 1 remains dependent type after tsubst while
9788 this isn't true for case 2. */
9789 ++processing_template_decl;
9790 dependent_p = dependent_type_p (context);
9791 --processing_template_decl;
9792
9793 if (!dependent_p
9794 && !complete_type_or_else (context, NULL_TREE))
9795 return error_mark_node;
9796
9797 if (COMPLETE_TYPE_P (context))
9798 {
9799 tree fn = new_friend;
9800 /* do_friend adds the TEMPLATE_DECL for any member friend
9801 template even if it isn't a member template, i.e.
9802 template <class T> friend A<T>::f();
9803 Look through it in that case. */
9804 if (TREE_CODE (fn) == TEMPLATE_DECL
9805 && !PRIMARY_TEMPLATE_P (fn))
9806 fn = DECL_TEMPLATE_RESULT (fn);
9807 /* Check to see that the declaration is really present, and,
9808 possibly obtain an improved declaration. */
9809 fn = check_classfn (context, fn, NULL_TREE);
9810
9811 if (fn)
9812 new_friend = fn;
9813 }
9814 }
9815
9816 return new_friend;
9817 }
9818
9819 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9820 template arguments, as for tsubst.
9821
9822 Returns an appropriate tsubst'd friend type or error_mark_node on
9823 failure. */
9824
9825 static tree
9826 tsubst_friend_class (tree friend_tmpl, tree args)
9827 {
9828 tree friend_type;
9829 tree tmpl;
9830 tree context;
9831
9832 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9833 {
9834 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9835 return TREE_TYPE (t);
9836 }
9837
9838 context = CP_DECL_CONTEXT (friend_tmpl);
9839
9840 if (context != global_namespace)
9841 {
9842 if (TREE_CODE (context) == NAMESPACE_DECL)
9843 push_nested_namespace (context);
9844 else
9845 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9846 }
9847
9848 /* Look for a class template declaration. We look for hidden names
9849 because two friend declarations of the same template are the
9850 same. For example, in:
9851
9852 struct A {
9853 template <typename> friend class F;
9854 };
9855 template <typename> struct B {
9856 template <typename> friend class F;
9857 };
9858
9859 both F templates are the same. */
9860 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9861 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9862
9863 /* But, if we don't find one, it might be because we're in a
9864 situation like this:
9865
9866 template <class T>
9867 struct S {
9868 template <class U>
9869 friend struct S;
9870 };
9871
9872 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9873 for `S<int>', not the TEMPLATE_DECL. */
9874 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9875 {
9876 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9877 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9878 }
9879
9880 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9881 {
9882 /* The friend template has already been declared. Just
9883 check to see that the declarations match, and install any new
9884 default parameters. We must tsubst the default parameters,
9885 of course. We only need the innermost template parameters
9886 because that is all that redeclare_class_template will look
9887 at. */
9888 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9889 > TMPL_ARGS_DEPTH (args))
9890 {
9891 tree parms;
9892 location_t saved_input_location;
9893 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9894 args, tf_warning_or_error);
9895
9896 saved_input_location = input_location;
9897 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9898 tree cons = get_constraints (tmpl);
9899 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9900 input_location = saved_input_location;
9901
9902 }
9903
9904 friend_type = TREE_TYPE (tmpl);
9905 }
9906 else
9907 {
9908 /* The friend template has not already been declared. In this
9909 case, the instantiation of the template class will cause the
9910 injection of this template into the global scope. */
9911 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9912 if (tmpl == error_mark_node)
9913 return error_mark_node;
9914
9915 /* The new TMPL is not an instantiation of anything, so we
9916 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9917 the new type because that is supposed to be the corresponding
9918 template decl, i.e., TMPL. */
9919 DECL_USE_TEMPLATE (tmpl) = 0;
9920 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9921 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9922 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9923 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9924
9925 /* Inject this template into the global scope. */
9926 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9927 }
9928
9929 if (context != global_namespace)
9930 {
9931 if (TREE_CODE (context) == NAMESPACE_DECL)
9932 pop_nested_namespace (context);
9933 else
9934 pop_nested_class ();
9935 }
9936
9937 return friend_type;
9938 }
9939
9940 /* Returns zero if TYPE cannot be completed later due to circularity.
9941 Otherwise returns one. */
9942
9943 static int
9944 can_complete_type_without_circularity (tree type)
9945 {
9946 if (type == NULL_TREE || type == error_mark_node)
9947 return 0;
9948 else if (COMPLETE_TYPE_P (type))
9949 return 1;
9950 else if (TREE_CODE (type) == ARRAY_TYPE)
9951 return can_complete_type_without_circularity (TREE_TYPE (type));
9952 else if (CLASS_TYPE_P (type)
9953 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9954 return 0;
9955 else
9956 return 1;
9957 }
9958
9959 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9960 tsubst_flags_t, tree);
9961
9962 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9963 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9964
9965 static tree
9966 tsubst_attribute (tree t, tree *decl_p, tree args,
9967 tsubst_flags_t complain, tree in_decl)
9968 {
9969 gcc_assert (ATTR_IS_DEPENDENT (t));
9970
9971 tree val = TREE_VALUE (t);
9972 if (val == NULL_TREE)
9973 /* Nothing to do. */;
9974 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9975 && is_attribute_p ("omp declare simd",
9976 get_attribute_name (t)))
9977 {
9978 tree clauses = TREE_VALUE (val);
9979 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9980 complain, in_decl);
9981 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9982 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9983 tree parms = DECL_ARGUMENTS (*decl_p);
9984 clauses
9985 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9986 if (clauses)
9987 val = build_tree_list (NULL_TREE, clauses);
9988 else
9989 val = NULL_TREE;
9990 }
9991 /* If the first attribute argument is an identifier, don't
9992 pass it through tsubst. Attributes like mode, format,
9993 cleanup and several target specific attributes expect it
9994 unmodified. */
9995 else if (attribute_takes_identifier_p (get_attribute_name (t)))
9996 {
9997 tree chain
9998 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
9999 /*integral_constant_expression_p=*/false);
10000 if (chain != TREE_CHAIN (val))
10001 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10002 }
10003 else if (PACK_EXPANSION_P (val))
10004 {
10005 /* An attribute pack expansion. */
10006 tree purp = TREE_PURPOSE (t);
10007 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10008 int len = TREE_VEC_LENGTH (pack);
10009 tree list = NULL_TREE;
10010 tree *q = &list;
10011 for (int i = 0; i < len; ++i)
10012 {
10013 tree elt = TREE_VEC_ELT (pack, i);
10014 *q = build_tree_list (purp, elt);
10015 q = &TREE_CHAIN (*q);
10016 }
10017 return list;
10018 }
10019 else
10020 val = tsubst_expr (val, args, complain, in_decl,
10021 /*integral_constant_expression_p=*/false);
10022
10023 if (val != TREE_VALUE (t))
10024 return build_tree_list (TREE_PURPOSE (t), val);
10025 return t;
10026 }
10027
10028 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10029 unchanged or a new TREE_LIST chain. */
10030
10031 static tree
10032 tsubst_attributes (tree attributes, tree args,
10033 tsubst_flags_t complain, tree in_decl)
10034 {
10035 tree last_dep = NULL_TREE;
10036
10037 for (tree t = attributes; t; t = TREE_CHAIN (t))
10038 if (ATTR_IS_DEPENDENT (t))
10039 {
10040 last_dep = t;
10041 attributes = copy_list (attributes);
10042 break;
10043 }
10044
10045 if (last_dep)
10046 for (tree *p = &attributes; *p; )
10047 {
10048 tree t = *p;
10049 if (ATTR_IS_DEPENDENT (t))
10050 {
10051 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10052 if (subst != t)
10053 {
10054 *p = subst;
10055 do
10056 p = &TREE_CHAIN (*p);
10057 while (*p);
10058 *p = TREE_CHAIN (t);
10059 continue;
10060 }
10061 }
10062 p = &TREE_CHAIN (*p);
10063 }
10064
10065 return attributes;
10066 }
10067
10068 /* Apply any attributes which had to be deferred until instantiation
10069 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10070 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10071
10072 static void
10073 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10074 tree args, tsubst_flags_t complain, tree in_decl)
10075 {
10076 tree last_dep = NULL_TREE;
10077 tree t;
10078 tree *p;
10079
10080 if (attributes == NULL_TREE)
10081 return;
10082
10083 if (DECL_P (*decl_p))
10084 {
10085 if (TREE_TYPE (*decl_p) == error_mark_node)
10086 return;
10087 p = &DECL_ATTRIBUTES (*decl_p);
10088 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10089 to our attributes parameter. */
10090 gcc_assert (*p == attributes);
10091 }
10092 else
10093 {
10094 p = &TYPE_ATTRIBUTES (*decl_p);
10095 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10096 lookup_template_class_1, and should be preserved. */
10097 gcc_assert (*p != attributes);
10098 while (*p)
10099 p = &TREE_CHAIN (*p);
10100 }
10101
10102 for (t = attributes; t; t = TREE_CHAIN (t))
10103 if (ATTR_IS_DEPENDENT (t))
10104 {
10105 last_dep = t;
10106 attributes = copy_list (attributes);
10107 break;
10108 }
10109
10110 *p = attributes;
10111 if (last_dep)
10112 {
10113 tree late_attrs = NULL_TREE;
10114 tree *q = &late_attrs;
10115
10116 for (; *p; )
10117 {
10118 t = *p;
10119 if (ATTR_IS_DEPENDENT (t))
10120 {
10121 *p = TREE_CHAIN (t);
10122 TREE_CHAIN (t) = NULL_TREE;
10123 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10124 do
10125 q = &TREE_CHAIN (*q);
10126 while (*q);
10127 }
10128 else
10129 p = &TREE_CHAIN (t);
10130 }
10131
10132 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10133 }
10134 }
10135
10136 /* Perform (or defer) access check for typedefs that were referenced
10137 from within the template TMPL code.
10138 This is a subroutine of instantiate_decl and instantiate_class_template.
10139 TMPL is the template to consider and TARGS is the list of arguments of
10140 that template. */
10141
10142 static void
10143 perform_typedefs_access_check (tree tmpl, tree targs)
10144 {
10145 location_t saved_location;
10146 unsigned i;
10147 qualified_typedef_usage_t *iter;
10148
10149 if (!tmpl
10150 || (!CLASS_TYPE_P (tmpl)
10151 && TREE_CODE (tmpl) != FUNCTION_DECL))
10152 return;
10153
10154 saved_location = input_location;
10155 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10156 {
10157 tree type_decl = iter->typedef_decl;
10158 tree type_scope = iter->context;
10159
10160 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10161 continue;
10162
10163 if (uses_template_parms (type_decl))
10164 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10165 if (uses_template_parms (type_scope))
10166 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10167
10168 /* Make access check error messages point to the location
10169 of the use of the typedef. */
10170 input_location = iter->locus;
10171 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10172 type_decl, type_decl,
10173 tf_warning_or_error);
10174 }
10175 input_location = saved_location;
10176 }
10177
10178 static tree
10179 instantiate_class_template_1 (tree type)
10180 {
10181 tree templ, args, pattern, t, member;
10182 tree typedecl;
10183 tree pbinfo;
10184 tree base_list;
10185 unsigned int saved_maximum_field_alignment;
10186 tree fn_context;
10187
10188 if (type == error_mark_node)
10189 return error_mark_node;
10190
10191 if (COMPLETE_OR_OPEN_TYPE_P (type)
10192 || uses_template_parms (type))
10193 return type;
10194
10195 /* Figure out which template is being instantiated. */
10196 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10197 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10198
10199 /* Determine what specialization of the original template to
10200 instantiate. */
10201 t = most_specialized_partial_spec (type, tf_warning_or_error);
10202 if (t == error_mark_node)
10203 {
10204 TYPE_BEING_DEFINED (type) = 1;
10205 return error_mark_node;
10206 }
10207 else if (t)
10208 {
10209 /* This TYPE is actually an instantiation of a partial
10210 specialization. We replace the innermost set of ARGS with
10211 the arguments appropriate for substitution. For example,
10212 given:
10213
10214 template <class T> struct S {};
10215 template <class T> struct S<T*> {};
10216
10217 and supposing that we are instantiating S<int*>, ARGS will
10218 presently be {int*} -- but we need {int}. */
10219 pattern = TREE_TYPE (t);
10220 args = TREE_PURPOSE (t);
10221 }
10222 else
10223 {
10224 pattern = TREE_TYPE (templ);
10225 args = CLASSTYPE_TI_ARGS (type);
10226 }
10227
10228 /* If the template we're instantiating is incomplete, then clearly
10229 there's nothing we can do. */
10230 if (!COMPLETE_TYPE_P (pattern))
10231 return type;
10232
10233 /* If we've recursively instantiated too many templates, stop. */
10234 if (! push_tinst_level (type))
10235 return type;
10236
10237 /* Now we're really doing the instantiation. Mark the type as in
10238 the process of being defined. */
10239 TYPE_BEING_DEFINED (type) = 1;
10240
10241 /* We may be in the middle of deferred access check. Disable
10242 it now. */
10243 push_deferring_access_checks (dk_no_deferred);
10244
10245 int saved_unevaluated_operand = cp_unevaluated_operand;
10246 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10247
10248 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10249 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10250 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10251 fn_context = error_mark_node;
10252 if (!fn_context)
10253 push_to_top_level ();
10254 else
10255 {
10256 cp_unevaluated_operand = 0;
10257 c_inhibit_evaluation_warnings = 0;
10258 }
10259 /* Use #pragma pack from the template context. */
10260 saved_maximum_field_alignment = maximum_field_alignment;
10261 maximum_field_alignment = TYPE_PRECISION (pattern);
10262
10263 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10264
10265 /* Set the input location to the most specialized template definition.
10266 This is needed if tsubsting causes an error. */
10267 typedecl = TYPE_MAIN_DECL (pattern);
10268 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10269 DECL_SOURCE_LOCATION (typedecl);
10270
10271 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10272 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10273 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10274 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10275 if (ANON_AGGR_TYPE_P (pattern))
10276 SET_ANON_AGGR_TYPE_P (type);
10277 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10278 {
10279 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10280 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10281 /* Adjust visibility for template arguments. */
10282 determine_visibility (TYPE_MAIN_DECL (type));
10283 }
10284 if (CLASS_TYPE_P (type))
10285 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10286
10287 pbinfo = TYPE_BINFO (pattern);
10288
10289 /* We should never instantiate a nested class before its enclosing
10290 class; we need to look up the nested class by name before we can
10291 instantiate it, and that lookup should instantiate the enclosing
10292 class. */
10293 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10294 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10295
10296 base_list = NULL_TREE;
10297 if (BINFO_N_BASE_BINFOS (pbinfo))
10298 {
10299 tree pbase_binfo;
10300 tree pushed_scope;
10301 int i;
10302
10303 /* We must enter the scope containing the type, as that is where
10304 the accessibility of types named in dependent bases are
10305 looked up from. */
10306 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10307
10308 /* Substitute into each of the bases to determine the actual
10309 basetypes. */
10310 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10311 {
10312 tree base;
10313 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10314 tree expanded_bases = NULL_TREE;
10315 int idx, len = 1;
10316
10317 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10318 {
10319 expanded_bases =
10320 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10321 args, tf_error, NULL_TREE);
10322 if (expanded_bases == error_mark_node)
10323 continue;
10324
10325 len = TREE_VEC_LENGTH (expanded_bases);
10326 }
10327
10328 for (idx = 0; idx < len; idx++)
10329 {
10330 if (expanded_bases)
10331 /* Extract the already-expanded base class. */
10332 base = TREE_VEC_ELT (expanded_bases, idx);
10333 else
10334 /* Substitute to figure out the base class. */
10335 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10336 NULL_TREE);
10337
10338 if (base == error_mark_node)
10339 continue;
10340
10341 base_list = tree_cons (access, base, base_list);
10342 if (BINFO_VIRTUAL_P (pbase_binfo))
10343 TREE_TYPE (base_list) = integer_type_node;
10344 }
10345 }
10346
10347 /* The list is now in reverse order; correct that. */
10348 base_list = nreverse (base_list);
10349
10350 if (pushed_scope)
10351 pop_scope (pushed_scope);
10352 }
10353 /* Now call xref_basetypes to set up all the base-class
10354 information. */
10355 xref_basetypes (type, base_list);
10356
10357 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10358 (int) ATTR_FLAG_TYPE_IN_PLACE,
10359 args, tf_error, NULL_TREE);
10360 fixup_attribute_variants (type);
10361
10362 /* Now that our base classes are set up, enter the scope of the
10363 class, so that name lookups into base classes, etc. will work
10364 correctly. This is precisely analogous to what we do in
10365 begin_class_definition when defining an ordinary non-template
10366 class, except we also need to push the enclosing classes. */
10367 push_nested_class (type);
10368
10369 /* Now members are processed in the order of declaration. */
10370 for (member = CLASSTYPE_DECL_LIST (pattern);
10371 member; member = TREE_CHAIN (member))
10372 {
10373 tree t = TREE_VALUE (member);
10374
10375 if (TREE_PURPOSE (member))
10376 {
10377 if (TYPE_P (t))
10378 {
10379 /* Build new CLASSTYPE_NESTED_UTDS. */
10380
10381 tree newtag;
10382 bool class_template_p;
10383
10384 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10385 && TYPE_LANG_SPECIFIC (t)
10386 && CLASSTYPE_IS_TEMPLATE (t));
10387 /* If the member is a class template, then -- even after
10388 substitution -- there may be dependent types in the
10389 template argument list for the class. We increment
10390 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10391 that function will assume that no types are dependent
10392 when outside of a template. */
10393 if (class_template_p)
10394 ++processing_template_decl;
10395 newtag = tsubst (t, args, tf_error, NULL_TREE);
10396 if (class_template_p)
10397 --processing_template_decl;
10398 if (newtag == error_mark_node)
10399 continue;
10400
10401 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10402 {
10403 tree name = TYPE_IDENTIFIER (t);
10404
10405 if (class_template_p)
10406 /* Unfortunately, lookup_template_class sets
10407 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10408 instantiation (i.e., for the type of a member
10409 template class nested within a template class.)
10410 This behavior is required for
10411 maybe_process_partial_specialization to work
10412 correctly, but is not accurate in this case;
10413 the TAG is not an instantiation of anything.
10414 (The corresponding TEMPLATE_DECL is an
10415 instantiation, but the TYPE is not.) */
10416 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10417
10418 /* Now, we call pushtag to put this NEWTAG into the scope of
10419 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10420 pushtag calling push_template_decl. We don't have to do
10421 this for enums because it will already have been done in
10422 tsubst_enum. */
10423 if (name)
10424 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10425 pushtag (name, newtag, /*tag_scope=*/ts_current);
10426 }
10427 }
10428 else if (DECL_DECLARES_FUNCTION_P (t))
10429 {
10430 /* Build new TYPE_METHODS. */
10431 tree r;
10432
10433 if (TREE_CODE (t) == TEMPLATE_DECL)
10434 ++processing_template_decl;
10435 r = tsubst (t, args, tf_error, NULL_TREE);
10436 if (TREE_CODE (t) == TEMPLATE_DECL)
10437 --processing_template_decl;
10438 set_current_access_from_decl (r);
10439 finish_member_declaration (r);
10440 /* Instantiate members marked with attribute used. */
10441 if (r != error_mark_node && DECL_PRESERVE_P (r))
10442 mark_used (r);
10443 if (TREE_CODE (r) == FUNCTION_DECL
10444 && DECL_OMP_DECLARE_REDUCTION_P (r))
10445 cp_check_omp_declare_reduction (r);
10446 }
10447 else if (DECL_CLASS_TEMPLATE_P (t)
10448 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10449 /* A closure type for a lambda in a default argument for a
10450 member template. Ignore it; it will be instantiated with
10451 the default argument. */;
10452 else
10453 {
10454 /* Build new TYPE_FIELDS. */
10455 if (TREE_CODE (t) == STATIC_ASSERT)
10456 {
10457 tree condition;
10458
10459 ++c_inhibit_evaluation_warnings;
10460 condition =
10461 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10462 tf_warning_or_error, NULL_TREE,
10463 /*integral_constant_expression_p=*/true);
10464 --c_inhibit_evaluation_warnings;
10465
10466 finish_static_assert (condition,
10467 STATIC_ASSERT_MESSAGE (t),
10468 STATIC_ASSERT_SOURCE_LOCATION (t),
10469 /*member_p=*/true);
10470 }
10471 else if (TREE_CODE (t) != CONST_DECL)
10472 {
10473 tree r;
10474 tree vec = NULL_TREE;
10475 int len = 1;
10476
10477 /* The file and line for this declaration, to
10478 assist in error message reporting. Since we
10479 called push_tinst_level above, we don't need to
10480 restore these. */
10481 input_location = DECL_SOURCE_LOCATION (t);
10482
10483 if (TREE_CODE (t) == TEMPLATE_DECL)
10484 ++processing_template_decl;
10485 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10486 if (TREE_CODE (t) == TEMPLATE_DECL)
10487 --processing_template_decl;
10488
10489 if (TREE_CODE (r) == TREE_VEC)
10490 {
10491 /* A capture pack became multiple fields. */
10492 vec = r;
10493 len = TREE_VEC_LENGTH (vec);
10494 }
10495
10496 for (int i = 0; i < len; ++i)
10497 {
10498 if (vec)
10499 r = TREE_VEC_ELT (vec, i);
10500 if (VAR_P (r))
10501 {
10502 /* In [temp.inst]:
10503
10504 [t]he initialization (and any associated
10505 side-effects) of a static data member does
10506 not occur unless the static data member is
10507 itself used in a way that requires the
10508 definition of the static data member to
10509 exist.
10510
10511 Therefore, we do not substitute into the
10512 initialized for the static data member here. */
10513 finish_static_data_member_decl
10514 (r,
10515 /*init=*/NULL_TREE,
10516 /*init_const_expr_p=*/false,
10517 /*asmspec_tree=*/NULL_TREE,
10518 /*flags=*/0);
10519 /* Instantiate members marked with attribute used. */
10520 if (r != error_mark_node && DECL_PRESERVE_P (r))
10521 mark_used (r);
10522 }
10523 else if (TREE_CODE (r) == FIELD_DECL)
10524 {
10525 /* Determine whether R has a valid type and can be
10526 completed later. If R is invalid, then its type
10527 is replaced by error_mark_node. */
10528 tree rtype = TREE_TYPE (r);
10529 if (can_complete_type_without_circularity (rtype))
10530 complete_type (rtype);
10531
10532 if (!complete_or_array_type_p (rtype))
10533 {
10534 /* If R's type couldn't be completed and
10535 it isn't a flexible array member (whose
10536 type is incomplete by definition) give
10537 an error. */
10538 cxx_incomplete_type_error (r, rtype);
10539 TREE_TYPE (r) = error_mark_node;
10540 }
10541 }
10542
10543 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10544 such a thing will already have been added to the field
10545 list by tsubst_enum in finish_member_declaration in the
10546 CLASSTYPE_NESTED_UTDS case above. */
10547 if (!(TREE_CODE (r) == TYPE_DECL
10548 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10549 && DECL_ARTIFICIAL (r)))
10550 {
10551 set_current_access_from_decl (r);
10552 finish_member_declaration (r);
10553 }
10554 }
10555 }
10556 }
10557 }
10558 else
10559 {
10560 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10561 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10562 {
10563 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10564
10565 tree friend_type = t;
10566 bool adjust_processing_template_decl = false;
10567
10568 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10569 {
10570 /* template <class T> friend class C; */
10571 friend_type = tsubst_friend_class (friend_type, args);
10572 adjust_processing_template_decl = true;
10573 }
10574 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10575 {
10576 /* template <class T> friend class C::D; */
10577 friend_type = tsubst (friend_type, args,
10578 tf_warning_or_error, NULL_TREE);
10579 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10580 friend_type = TREE_TYPE (friend_type);
10581 adjust_processing_template_decl = true;
10582 }
10583 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10584 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10585 {
10586 /* This could be either
10587
10588 friend class T::C;
10589
10590 when dependent_type_p is false or
10591
10592 template <class U> friend class T::C;
10593
10594 otherwise. */
10595 /* Bump processing_template_decl in case this is something like
10596 template <class T> friend struct A<T>::B. */
10597 ++processing_template_decl;
10598 friend_type = tsubst (friend_type, args,
10599 tf_warning_or_error, NULL_TREE);
10600 if (dependent_type_p (friend_type))
10601 adjust_processing_template_decl = true;
10602 --processing_template_decl;
10603 }
10604 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10605 && hidden_name_p (TYPE_NAME (friend_type)))
10606 {
10607 /* friend class C;
10608
10609 where C hasn't been declared yet. Let's lookup name
10610 from namespace scope directly, bypassing any name that
10611 come from dependent base class. */
10612 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10613
10614 /* The call to xref_tag_from_type does injection for friend
10615 classes. */
10616 push_nested_namespace (ns);
10617 friend_type =
10618 xref_tag_from_type (friend_type, NULL_TREE,
10619 /*tag_scope=*/ts_current);
10620 pop_nested_namespace (ns);
10621 }
10622 else if (uses_template_parms (friend_type))
10623 /* friend class C<T>; */
10624 friend_type = tsubst (friend_type, args,
10625 tf_warning_or_error, NULL_TREE);
10626 /* Otherwise it's
10627
10628 friend class C;
10629
10630 where C is already declared or
10631
10632 friend class C<int>;
10633
10634 We don't have to do anything in these cases. */
10635
10636 if (adjust_processing_template_decl)
10637 /* Trick make_friend_class into realizing that the friend
10638 we're adding is a template, not an ordinary class. It's
10639 important that we use make_friend_class since it will
10640 perform some error-checking and output cross-reference
10641 information. */
10642 ++processing_template_decl;
10643
10644 if (friend_type != error_mark_node)
10645 make_friend_class (type, friend_type, /*complain=*/false);
10646
10647 if (adjust_processing_template_decl)
10648 --processing_template_decl;
10649 }
10650 else
10651 {
10652 /* Build new DECL_FRIENDLIST. */
10653 tree r;
10654
10655 /* The file and line for this declaration, to
10656 assist in error message reporting. Since we
10657 called push_tinst_level above, we don't need to
10658 restore these. */
10659 input_location = DECL_SOURCE_LOCATION (t);
10660
10661 if (TREE_CODE (t) == TEMPLATE_DECL)
10662 {
10663 ++processing_template_decl;
10664 push_deferring_access_checks (dk_no_check);
10665 }
10666
10667 r = tsubst_friend_function (t, args);
10668 add_friend (type, r, /*complain=*/false);
10669 if (TREE_CODE (t) == TEMPLATE_DECL)
10670 {
10671 pop_deferring_access_checks ();
10672 --processing_template_decl;
10673 }
10674 }
10675 }
10676 }
10677
10678 if (fn_context)
10679 {
10680 /* Restore these before substituting into the lambda capture
10681 initializers. */
10682 cp_unevaluated_operand = saved_unevaluated_operand;
10683 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10684 }
10685
10686 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10687 {
10688 tree decl = lambda_function (type);
10689 if (decl)
10690 {
10691 if (cxx_dialect >= cxx1z)
10692 CLASSTYPE_LITERAL_P (type) = true;
10693
10694 if (!DECL_TEMPLATE_INFO (decl)
10695 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10696 {
10697 /* Set function_depth to avoid garbage collection. */
10698 ++function_depth;
10699 instantiate_decl (decl, /*defer_ok=*/false, false);
10700 --function_depth;
10701 }
10702
10703 /* We need to instantiate the capture list from the template
10704 after we've instantiated the closure members, but before we
10705 consider adding the conversion op. Also keep any captures
10706 that may have been added during instantiation of the op(). */
10707 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10708 tree tmpl_cap
10709 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10710 args, tf_warning_or_error, NULL_TREE,
10711 false, false);
10712
10713 LAMBDA_EXPR_CAPTURE_LIST (expr)
10714 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10715
10716 maybe_add_lambda_conv_op (type);
10717 }
10718 else
10719 gcc_assert (errorcount);
10720 }
10721
10722 /* Set the file and line number information to whatever is given for
10723 the class itself. This puts error messages involving generated
10724 implicit functions at a predictable point, and the same point
10725 that would be used for non-template classes. */
10726 input_location = DECL_SOURCE_LOCATION (typedecl);
10727
10728 unreverse_member_declarations (type);
10729 finish_struct_1 (type);
10730 TYPE_BEING_DEFINED (type) = 0;
10731
10732 /* We don't instantiate default arguments for member functions. 14.7.1:
10733
10734 The implicit instantiation of a class template specialization causes
10735 the implicit instantiation of the declarations, but not of the
10736 definitions or default arguments, of the class member functions,
10737 member classes, static data members and member templates.... */
10738
10739 /* Some typedefs referenced from within the template code need to be access
10740 checked at template instantiation time, i.e now. These types were
10741 added to the template at parsing time. Let's get those and perform
10742 the access checks then. */
10743 perform_typedefs_access_check (pattern, args);
10744 perform_deferred_access_checks (tf_warning_or_error);
10745 pop_nested_class ();
10746 maximum_field_alignment = saved_maximum_field_alignment;
10747 if (!fn_context)
10748 pop_from_top_level ();
10749 pop_deferring_access_checks ();
10750 pop_tinst_level ();
10751
10752 /* The vtable for a template class can be emitted in any translation
10753 unit in which the class is instantiated. When there is no key
10754 method, however, finish_struct_1 will already have added TYPE to
10755 the keyed_classes list. */
10756 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10757 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10758
10759 return type;
10760 }
10761
10762 /* Wrapper for instantiate_class_template_1. */
10763
10764 tree
10765 instantiate_class_template (tree type)
10766 {
10767 tree ret;
10768 timevar_push (TV_TEMPLATE_INST);
10769 ret = instantiate_class_template_1 (type);
10770 timevar_pop (TV_TEMPLATE_INST);
10771 return ret;
10772 }
10773
10774 static tree
10775 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10776 {
10777 tree r;
10778
10779 if (!t)
10780 r = t;
10781 else if (TYPE_P (t))
10782 r = tsubst (t, args, complain, in_decl);
10783 else
10784 {
10785 if (!(complain & tf_warning))
10786 ++c_inhibit_evaluation_warnings;
10787 r = tsubst_expr (t, args, complain, in_decl,
10788 /*integral_constant_expression_p=*/true);
10789 if (!(complain & tf_warning))
10790 --c_inhibit_evaluation_warnings;
10791 }
10792 return r;
10793 }
10794
10795 /* Given a function parameter pack TMPL_PARM and some function parameters
10796 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10797 and set *SPEC_P to point at the next point in the list. */
10798
10799 tree
10800 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10801 {
10802 /* Collect all of the extra "packed" parameters into an
10803 argument pack. */
10804 tree parmvec;
10805 tree parmtypevec;
10806 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10807 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10808 tree spec_parm = *spec_p;
10809 int i, len;
10810
10811 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10812 if (tmpl_parm
10813 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10814 break;
10815
10816 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10817 parmvec = make_tree_vec (len);
10818 parmtypevec = make_tree_vec (len);
10819 spec_parm = *spec_p;
10820 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10821 {
10822 TREE_VEC_ELT (parmvec, i) = spec_parm;
10823 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10824 }
10825
10826 /* Build the argument packs. */
10827 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10828 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10829 TREE_TYPE (argpack) = argtypepack;
10830 *spec_p = spec_parm;
10831
10832 return argpack;
10833 }
10834
10835 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10836 NONTYPE_ARGUMENT_PACK. */
10837
10838 static tree
10839 make_fnparm_pack (tree spec_parm)
10840 {
10841 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10842 }
10843
10844 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10845 pack expansion with no extra args, 2 if it has extra args, or 0
10846 if it is not a pack expansion. */
10847
10848 static int
10849 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10850 {
10851 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10852 if (i >= TREE_VEC_LENGTH (vec))
10853 return 0;
10854 tree elt = TREE_VEC_ELT (vec, i);
10855 if (DECL_P (elt))
10856 /* A decl pack is itself an expansion. */
10857 elt = TREE_TYPE (elt);
10858 if (!PACK_EXPANSION_P (elt))
10859 return 0;
10860 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10861 return 2;
10862 return 1;
10863 }
10864
10865
10866 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10867
10868 static tree
10869 make_argument_pack_select (tree arg_pack, unsigned index)
10870 {
10871 tree aps = make_node (ARGUMENT_PACK_SELECT);
10872
10873 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10874 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10875
10876 return aps;
10877 }
10878
10879 /* This is a subroutine of tsubst_pack_expansion.
10880
10881 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10882 mechanism to store the (non complete list of) arguments of the
10883 substitution and return a non substituted pack expansion, in order
10884 to wait for when we have enough arguments to really perform the
10885 substitution. */
10886
10887 static bool
10888 use_pack_expansion_extra_args_p (tree parm_packs,
10889 int arg_pack_len,
10890 bool has_empty_arg)
10891 {
10892 /* If one pack has an expansion and another pack has a normal
10893 argument or if one pack has an empty argument and an another
10894 one hasn't then tsubst_pack_expansion cannot perform the
10895 substitution and need to fall back on the
10896 PACK_EXPANSION_EXTRA mechanism. */
10897 if (parm_packs == NULL_TREE)
10898 return false;
10899 else if (has_empty_arg)
10900 return true;
10901
10902 bool has_expansion_arg = false;
10903 for (int i = 0 ; i < arg_pack_len; ++i)
10904 {
10905 bool has_non_expansion_arg = false;
10906 for (tree parm_pack = parm_packs;
10907 parm_pack;
10908 parm_pack = TREE_CHAIN (parm_pack))
10909 {
10910 tree arg = TREE_VALUE (parm_pack);
10911
10912 int exp = argument_pack_element_is_expansion_p (arg, i);
10913 if (exp == 2)
10914 /* We can't substitute a pack expansion with extra args into
10915 our pattern. */
10916 return true;
10917 else if (exp)
10918 has_expansion_arg = true;
10919 else
10920 has_non_expansion_arg = true;
10921 }
10922
10923 if (has_expansion_arg && has_non_expansion_arg)
10924 return true;
10925 }
10926 return false;
10927 }
10928
10929 /* [temp.variadic]/6 says that:
10930
10931 The instantiation of a pack expansion [...]
10932 produces a list E1,E2, ..., En, where N is the number of elements
10933 in the pack expansion parameters.
10934
10935 This subroutine of tsubst_pack_expansion produces one of these Ei.
10936
10937 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10938 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10939 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10940 INDEX is the index 'i' of the element Ei to produce. ARGS,
10941 COMPLAIN, and IN_DECL are the same parameters as for the
10942 tsubst_pack_expansion function.
10943
10944 The function returns the resulting Ei upon successful completion,
10945 or error_mark_node.
10946
10947 Note that this function possibly modifies the ARGS parameter, so
10948 it's the responsibility of the caller to restore it. */
10949
10950 static tree
10951 gen_elem_of_pack_expansion_instantiation (tree pattern,
10952 tree parm_packs,
10953 unsigned index,
10954 tree args /* This parm gets
10955 modified. */,
10956 tsubst_flags_t complain,
10957 tree in_decl)
10958 {
10959 tree t;
10960 bool ith_elem_is_expansion = false;
10961
10962 /* For each parameter pack, change the substitution of the parameter
10963 pack to the ith argument in its argument pack, then expand the
10964 pattern. */
10965 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10966 {
10967 tree parm = TREE_PURPOSE (pack);
10968 tree arg_pack = TREE_VALUE (pack);
10969 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10970
10971 ith_elem_is_expansion |=
10972 argument_pack_element_is_expansion_p (arg_pack, index);
10973
10974 /* Select the Ith argument from the pack. */
10975 if (TREE_CODE (parm) == PARM_DECL
10976 || TREE_CODE (parm) == FIELD_DECL)
10977 {
10978 if (index == 0)
10979 {
10980 aps = make_argument_pack_select (arg_pack, index);
10981 if (!mark_used (parm, complain) && !(complain & tf_error))
10982 return error_mark_node;
10983 register_local_specialization (aps, parm);
10984 }
10985 else
10986 aps = retrieve_local_specialization (parm);
10987 }
10988 else
10989 {
10990 int idx, level;
10991 template_parm_level_and_index (parm, &level, &idx);
10992
10993 if (index == 0)
10994 {
10995 aps = make_argument_pack_select (arg_pack, index);
10996 /* Update the corresponding argument. */
10997 TMPL_ARG (args, level, idx) = aps;
10998 }
10999 else
11000 /* Re-use the ARGUMENT_PACK_SELECT. */
11001 aps = TMPL_ARG (args, level, idx);
11002 }
11003 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11004 }
11005
11006 /* Substitute into the PATTERN with the (possibly altered)
11007 arguments. */
11008 if (pattern == in_decl)
11009 /* Expanding a fixed parameter pack from
11010 coerce_template_parameter_pack. */
11011 t = tsubst_decl (pattern, args, complain);
11012 else if (pattern == error_mark_node)
11013 t = error_mark_node;
11014 else if (constraint_p (pattern))
11015 {
11016 if (processing_template_decl)
11017 t = tsubst_constraint (pattern, args, complain, in_decl);
11018 else
11019 t = (constraints_satisfied_p (pattern, args)
11020 ? boolean_true_node : boolean_false_node);
11021 }
11022 else if (!TYPE_P (pattern))
11023 t = tsubst_expr (pattern, args, complain, in_decl,
11024 /*integral_constant_expression_p=*/false);
11025 else
11026 t = tsubst (pattern, args, complain, in_decl);
11027
11028 /* If the Ith argument pack element is a pack expansion, then
11029 the Ith element resulting from the substituting is going to
11030 be a pack expansion as well. */
11031 if (ith_elem_is_expansion)
11032 t = make_pack_expansion (t);
11033
11034 return t;
11035 }
11036
11037 /* When the unexpanded parameter pack in a fold expression expands to an empty
11038 sequence, the value of the expression is as follows; the program is
11039 ill-formed if the operator is not listed in this table.
11040
11041 && true
11042 || false
11043 , void() */
11044
11045 tree
11046 expand_empty_fold (tree t, tsubst_flags_t complain)
11047 {
11048 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11049 if (!FOLD_EXPR_MODIFY_P (t))
11050 switch (code)
11051 {
11052 case TRUTH_ANDIF_EXPR:
11053 return boolean_true_node;
11054 case TRUTH_ORIF_EXPR:
11055 return boolean_false_node;
11056 case COMPOUND_EXPR:
11057 return void_node;
11058 default:
11059 break;
11060 }
11061
11062 if (complain & tf_error)
11063 error_at (location_of (t),
11064 "fold of empty expansion over %O", code);
11065 return error_mark_node;
11066 }
11067
11068 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11069 form an expression that combines the two terms using the
11070 operator of T. */
11071
11072 static tree
11073 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11074 {
11075 tree op = FOLD_EXPR_OP (t);
11076 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11077
11078 // Handle compound assignment operators.
11079 if (FOLD_EXPR_MODIFY_P (t))
11080 return build_x_modify_expr (input_location, left, code, right, complain);
11081
11082 switch (code)
11083 {
11084 case COMPOUND_EXPR:
11085 return build_x_compound_expr (input_location, left, right, complain);
11086 case DOTSTAR_EXPR:
11087 return build_m_component_ref (left, right, complain);
11088 default:
11089 return build_x_binary_op (input_location, code,
11090 left, TREE_CODE (left),
11091 right, TREE_CODE (right),
11092 /*overload=*/NULL,
11093 complain);
11094 }
11095 }
11096
11097 /* Substitute ARGS into the pack of a fold expression T. */
11098
11099 static inline tree
11100 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11101 {
11102 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11103 }
11104
11105 /* Substitute ARGS into the pack of a fold expression T. */
11106
11107 static inline tree
11108 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11109 {
11110 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11111 }
11112
11113 /* Expand a PACK of arguments into a grouped as left fold.
11114 Given a pack containing elements A0, A1, ..., An and an
11115 operator @, this builds the expression:
11116
11117 ((A0 @ A1) @ A2) ... @ An
11118
11119 Note that PACK must not be empty.
11120
11121 The operator is defined by the original fold expression T. */
11122
11123 static tree
11124 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11125 {
11126 tree left = TREE_VEC_ELT (pack, 0);
11127 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11128 {
11129 tree right = TREE_VEC_ELT (pack, i);
11130 left = fold_expression (t, left, right, complain);
11131 }
11132 return left;
11133 }
11134
11135 /* Substitute into a unary left fold expression. */
11136
11137 static tree
11138 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11139 tree in_decl)
11140 {
11141 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11142 if (pack == error_mark_node)
11143 return error_mark_node;
11144 if (PACK_EXPANSION_P (pack))
11145 {
11146 tree r = copy_node (t);
11147 FOLD_EXPR_PACK (r) = pack;
11148 return r;
11149 }
11150 if (TREE_VEC_LENGTH (pack) == 0)
11151 return expand_empty_fold (t, complain);
11152 else
11153 return expand_left_fold (t, pack, complain);
11154 }
11155
11156 /* Substitute into a binary left fold expression.
11157
11158 Do ths by building a single (non-empty) vector of argumnts and
11159 building the expression from those elements. */
11160
11161 static tree
11162 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11163 tree in_decl)
11164 {
11165 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11166 if (pack == error_mark_node)
11167 return error_mark_node;
11168 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11169 if (init == error_mark_node)
11170 return error_mark_node;
11171
11172 if (PACK_EXPANSION_P (pack))
11173 {
11174 tree r = copy_node (t);
11175 FOLD_EXPR_PACK (r) = pack;
11176 FOLD_EXPR_INIT (r) = init;
11177 return r;
11178 }
11179
11180 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11181 TREE_VEC_ELT (vec, 0) = init;
11182 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11183 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11184
11185 return expand_left_fold (t, vec, complain);
11186 }
11187
11188 /* Expand a PACK of arguments into a grouped as right fold.
11189 Given a pack containing elementns A0, A1, ..., and an
11190 operator @, this builds the expression:
11191
11192 A0@ ... (An-2 @ (An-1 @ An))
11193
11194 Note that PACK must not be empty.
11195
11196 The operator is defined by the original fold expression T. */
11197
11198 tree
11199 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11200 {
11201 // Build the expression.
11202 int n = TREE_VEC_LENGTH (pack);
11203 tree right = TREE_VEC_ELT (pack, n - 1);
11204 for (--n; n != 0; --n)
11205 {
11206 tree left = TREE_VEC_ELT (pack, n - 1);
11207 right = fold_expression (t, left, right, complain);
11208 }
11209 return right;
11210 }
11211
11212 /* Substitute into a unary right fold expression. */
11213
11214 static tree
11215 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11216 tree in_decl)
11217 {
11218 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11219 if (pack == error_mark_node)
11220 return error_mark_node;
11221 if (PACK_EXPANSION_P (pack))
11222 {
11223 tree r = copy_node (t);
11224 FOLD_EXPR_PACK (r) = pack;
11225 return r;
11226 }
11227 if (TREE_VEC_LENGTH (pack) == 0)
11228 return expand_empty_fold (t, complain);
11229 else
11230 return expand_right_fold (t, pack, complain);
11231 }
11232
11233 /* Substitute into a binary right fold expression.
11234
11235 Do ths by building a single (non-empty) vector of arguments and
11236 building the expression from those elements. */
11237
11238 static tree
11239 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11240 tree in_decl)
11241 {
11242 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11243 if (pack == error_mark_node)
11244 return error_mark_node;
11245 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11246 if (init == error_mark_node)
11247 return error_mark_node;
11248
11249 if (PACK_EXPANSION_P (pack))
11250 {
11251 tree r = copy_node (t);
11252 FOLD_EXPR_PACK (r) = pack;
11253 FOLD_EXPR_INIT (r) = init;
11254 return r;
11255 }
11256
11257 int n = TREE_VEC_LENGTH (pack);
11258 tree vec = make_tree_vec (n + 1);
11259 for (int i = 0; i < n; ++i)
11260 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11261 TREE_VEC_ELT (vec, n) = init;
11262
11263 return expand_right_fold (t, vec, complain);
11264 }
11265
11266
11267 /* Substitute ARGS into T, which is an pack expansion
11268 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11269 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11270 (if only a partial substitution could be performed) or
11271 ERROR_MARK_NODE if there was an error. */
11272 tree
11273 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11274 tree in_decl)
11275 {
11276 tree pattern;
11277 tree pack, packs = NULL_TREE;
11278 bool unsubstituted_packs = false;
11279 int i, len = -1;
11280 tree result;
11281 hash_map<tree, tree> *saved_local_specializations = NULL;
11282 bool need_local_specializations = false;
11283 int levels;
11284
11285 gcc_assert (PACK_EXPANSION_P (t));
11286 pattern = PACK_EXPANSION_PATTERN (t);
11287
11288 /* Add in any args remembered from an earlier partial instantiation. */
11289 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11290
11291 levels = TMPL_ARGS_DEPTH (args);
11292
11293 /* Determine the argument packs that will instantiate the parameter
11294 packs used in the expansion expression. While we're at it,
11295 compute the number of arguments to be expanded and make sure it
11296 is consistent. */
11297 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11298 pack = TREE_CHAIN (pack))
11299 {
11300 tree parm_pack = TREE_VALUE (pack);
11301 tree arg_pack = NULL_TREE;
11302 tree orig_arg = NULL_TREE;
11303 int level = 0;
11304
11305 if (TREE_CODE (parm_pack) == BASES)
11306 {
11307 if (BASES_DIRECT (parm_pack))
11308 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11309 args, complain, in_decl, false));
11310 else
11311 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11312 args, complain, in_decl, false));
11313 }
11314 if (TREE_CODE (parm_pack) == PARM_DECL)
11315 {
11316 /* We know we have correct local_specializations if this
11317 expansion is at function scope, or if we're dealing with a
11318 local parameter in a requires expression; for the latter,
11319 tsubst_requires_expr set it up appropriately. */
11320 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11321 arg_pack = retrieve_local_specialization (parm_pack);
11322 else
11323 /* We can't rely on local_specializations for a parameter
11324 name used later in a function declaration (such as in a
11325 late-specified return type). Even if it exists, it might
11326 have the wrong value for a recursive call. */
11327 need_local_specializations = true;
11328
11329 if (!arg_pack)
11330 {
11331 /* This parameter pack was used in an unevaluated context. Just
11332 make a dummy decl, since it's only used for its type. */
11333 arg_pack = tsubst_decl (parm_pack, args, complain);
11334 if (arg_pack && DECL_PACK_P (arg_pack))
11335 /* Partial instantiation of the parm_pack, we can't build
11336 up an argument pack yet. */
11337 arg_pack = NULL_TREE;
11338 else
11339 arg_pack = make_fnparm_pack (arg_pack);
11340 }
11341 }
11342 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11343 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11344 else
11345 {
11346 int idx;
11347 template_parm_level_and_index (parm_pack, &level, &idx);
11348
11349 if (level <= levels)
11350 arg_pack = TMPL_ARG (args, level, idx);
11351 }
11352
11353 orig_arg = arg_pack;
11354 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11355 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11356
11357 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11358 /* This can only happen if we forget to expand an argument
11359 pack somewhere else. Just return an error, silently. */
11360 {
11361 result = make_tree_vec (1);
11362 TREE_VEC_ELT (result, 0) = error_mark_node;
11363 return result;
11364 }
11365
11366 if (arg_pack)
11367 {
11368 int my_len =
11369 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11370
11371 /* Don't bother trying to do a partial substitution with
11372 incomplete packs; we'll try again after deduction. */
11373 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11374 return t;
11375
11376 if (len < 0)
11377 len = my_len;
11378 else if (len != my_len)
11379 {
11380 if (!(complain & tf_error))
11381 /* Fail quietly. */;
11382 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11383 error ("mismatched argument pack lengths while expanding "
11384 "%<%T%>",
11385 pattern);
11386 else
11387 error ("mismatched argument pack lengths while expanding "
11388 "%<%E%>",
11389 pattern);
11390 return error_mark_node;
11391 }
11392
11393 /* Keep track of the parameter packs and their corresponding
11394 argument packs. */
11395 packs = tree_cons (parm_pack, arg_pack, packs);
11396 TREE_TYPE (packs) = orig_arg;
11397 }
11398 else
11399 {
11400 /* We can't substitute for this parameter pack. We use a flag as
11401 well as the missing_level counter because function parameter
11402 packs don't have a level. */
11403 gcc_assert (processing_template_decl);
11404 unsubstituted_packs = true;
11405 }
11406 }
11407
11408 /* If the expansion is just T..., return the matching argument pack, unless
11409 we need to call convert_from_reference on all the elements. This is an
11410 important optimization; see c++/68422. */
11411 if (!unsubstituted_packs
11412 && TREE_PURPOSE (packs) == pattern)
11413 {
11414 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11415 /* Types need no adjustment, nor does sizeof..., and if we still have
11416 some pack expansion args we won't do anything yet. */
11417 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11418 || PACK_EXPANSION_SIZEOF_P (t)
11419 || pack_expansion_args_count (args))
11420 return args;
11421 /* Also optimize expression pack expansions if we can tell that the
11422 elements won't have reference type. */
11423 tree type = TREE_TYPE (pattern);
11424 if (type && TREE_CODE (type) != REFERENCE_TYPE
11425 && !PACK_EXPANSION_P (type)
11426 && !WILDCARD_TYPE_P (type))
11427 return args;
11428 /* Otherwise use the normal path so we get convert_from_reference. */
11429 }
11430
11431 /* We cannot expand this expansion expression, because we don't have
11432 all of the argument packs we need. */
11433 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11434 {
11435 /* We got some full packs, but we can't substitute them in until we
11436 have values for all the packs. So remember these until then. */
11437
11438 t = make_pack_expansion (pattern);
11439 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11440 return t;
11441 }
11442 else if (unsubstituted_packs)
11443 {
11444 /* There were no real arguments, we're just replacing a parameter
11445 pack with another version of itself. Substitute into the
11446 pattern and return a PACK_EXPANSION_*. The caller will need to
11447 deal with that. */
11448 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11449 t = tsubst_expr (pattern, args, complain, in_decl,
11450 /*integral_constant_expression_p=*/false);
11451 else
11452 t = tsubst (pattern, args, complain, in_decl);
11453 t = make_pack_expansion (t);
11454 return t;
11455 }
11456
11457 gcc_assert (len >= 0);
11458
11459 if (need_local_specializations)
11460 {
11461 /* We're in a late-specified return type, so create our own local
11462 specializations map; the current map is either NULL or (in the
11463 case of recursive unification) might have bindings that we don't
11464 want to use or alter. */
11465 saved_local_specializations = local_specializations;
11466 local_specializations = new hash_map<tree, tree>;
11467 }
11468
11469 /* For each argument in each argument pack, substitute into the
11470 pattern. */
11471 result = make_tree_vec (len);
11472 tree elem_args = copy_template_args (args);
11473 for (i = 0; i < len; ++i)
11474 {
11475 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11476 i,
11477 elem_args, complain,
11478 in_decl);
11479 TREE_VEC_ELT (result, i) = t;
11480 if (t == error_mark_node)
11481 {
11482 result = error_mark_node;
11483 break;
11484 }
11485 }
11486
11487 /* Update ARGS to restore the substitution from parameter packs to
11488 their argument packs. */
11489 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11490 {
11491 tree parm = TREE_PURPOSE (pack);
11492
11493 if (TREE_CODE (parm) == PARM_DECL
11494 || TREE_CODE (parm) == FIELD_DECL)
11495 register_local_specialization (TREE_TYPE (pack), parm);
11496 else
11497 {
11498 int idx, level;
11499
11500 if (TREE_VALUE (pack) == NULL_TREE)
11501 continue;
11502
11503 template_parm_level_and_index (parm, &level, &idx);
11504
11505 /* Update the corresponding argument. */
11506 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11507 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11508 TREE_TYPE (pack);
11509 else
11510 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11511 }
11512 }
11513
11514 if (need_local_specializations)
11515 {
11516 delete local_specializations;
11517 local_specializations = saved_local_specializations;
11518 }
11519
11520 /* If the dependent pack arguments were such that we end up with only a
11521 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11522 if (len == 1 && TREE_CODE (result) == TREE_VEC
11523 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11524 return TREE_VEC_ELT (result, 0);
11525
11526 return result;
11527 }
11528
11529 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11530 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11531 parameter packs; all parms generated from a function parameter pack will
11532 have the same DECL_PARM_INDEX. */
11533
11534 tree
11535 get_pattern_parm (tree parm, tree tmpl)
11536 {
11537 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11538 tree patparm;
11539
11540 if (DECL_ARTIFICIAL (parm))
11541 {
11542 for (patparm = DECL_ARGUMENTS (pattern);
11543 patparm; patparm = DECL_CHAIN (patparm))
11544 if (DECL_ARTIFICIAL (patparm)
11545 && DECL_NAME (parm) == DECL_NAME (patparm))
11546 break;
11547 }
11548 else
11549 {
11550 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11551 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11552 gcc_assert (DECL_PARM_INDEX (patparm)
11553 == DECL_PARM_INDEX (parm));
11554 }
11555
11556 return patparm;
11557 }
11558
11559 /* Make an argument pack out of the TREE_VEC VEC. */
11560
11561 static tree
11562 make_argument_pack (tree vec)
11563 {
11564 tree pack;
11565 tree elt = TREE_VEC_ELT (vec, 0);
11566 if (TYPE_P (elt))
11567 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11568 else
11569 {
11570 pack = make_node (NONTYPE_ARGUMENT_PACK);
11571 TREE_TYPE (pack) = TREE_TYPE (elt);
11572 TREE_CONSTANT (pack) = 1;
11573 }
11574 SET_ARGUMENT_PACK_ARGS (pack, vec);
11575 return pack;
11576 }
11577
11578 /* Return an exact copy of template args T that can be modified
11579 independently. */
11580
11581 static tree
11582 copy_template_args (tree t)
11583 {
11584 if (t == error_mark_node)
11585 return t;
11586
11587 int len = TREE_VEC_LENGTH (t);
11588 tree new_vec = make_tree_vec (len);
11589
11590 for (int i = 0; i < len; ++i)
11591 {
11592 tree elt = TREE_VEC_ELT (t, i);
11593 if (elt && TREE_CODE (elt) == TREE_VEC)
11594 elt = copy_template_args (elt);
11595 TREE_VEC_ELT (new_vec, i) = elt;
11596 }
11597
11598 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11599 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11600
11601 return new_vec;
11602 }
11603
11604 /* Substitute ARGS into the vector or list of template arguments T. */
11605
11606 static tree
11607 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11608 {
11609 tree orig_t = t;
11610 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11611 tree *elts;
11612
11613 if (t == error_mark_node)
11614 return error_mark_node;
11615
11616 len = TREE_VEC_LENGTH (t);
11617 elts = XALLOCAVEC (tree, len);
11618
11619 for (i = 0; i < len; i++)
11620 {
11621 tree orig_arg = TREE_VEC_ELT (t, i);
11622 tree new_arg;
11623
11624 if (TREE_CODE (orig_arg) == TREE_VEC)
11625 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11626 else if (PACK_EXPANSION_P (orig_arg))
11627 {
11628 /* Substitute into an expansion expression. */
11629 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11630
11631 if (TREE_CODE (new_arg) == TREE_VEC)
11632 /* Add to the expanded length adjustment the number of
11633 expanded arguments. We subtract one from this
11634 measurement, because the argument pack expression
11635 itself is already counted as 1 in
11636 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11637 the argument pack is empty. */
11638 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11639 }
11640 else if (ARGUMENT_PACK_P (orig_arg))
11641 {
11642 /* Substitute into each of the arguments. */
11643 new_arg = TYPE_P (orig_arg)
11644 ? cxx_make_type (TREE_CODE (orig_arg))
11645 : make_node (TREE_CODE (orig_arg));
11646
11647 SET_ARGUMENT_PACK_ARGS (
11648 new_arg,
11649 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11650 args, complain, in_decl));
11651
11652 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11653 new_arg = error_mark_node;
11654
11655 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11656 if (type_uses_auto (TREE_TYPE (orig_arg)))
11657 TREE_TYPE (new_arg) = TREE_TYPE (orig_arg);
11658 else
11659 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11660 complain, in_decl);
11661 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11662
11663 if (TREE_TYPE (new_arg) == error_mark_node)
11664 new_arg = error_mark_node;
11665 }
11666 }
11667 else
11668 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11669
11670 if (new_arg == error_mark_node)
11671 return error_mark_node;
11672
11673 elts[i] = new_arg;
11674 if (new_arg != orig_arg)
11675 need_new = 1;
11676 }
11677
11678 if (!need_new)
11679 return t;
11680
11681 /* Make space for the expanded arguments coming from template
11682 argument packs. */
11683 t = make_tree_vec (len + expanded_len_adjust);
11684 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11685 arguments for a member template.
11686 In that case each TREE_VEC in ORIG_T represents a level of template
11687 arguments, and ORIG_T won't carry any non defaulted argument count.
11688 It will rather be the nested TREE_VECs that will carry one.
11689 In other words, ORIG_T carries a non defaulted argument count only
11690 if it doesn't contain any nested TREE_VEC. */
11691 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11692 {
11693 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11694 count += expanded_len_adjust;
11695 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11696 }
11697 for (i = 0, out = 0; i < len; i++)
11698 {
11699 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11700 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11701 && TREE_CODE (elts[i]) == TREE_VEC)
11702 {
11703 int idx;
11704
11705 /* Now expand the template argument pack "in place". */
11706 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11707 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11708 }
11709 else
11710 {
11711 TREE_VEC_ELT (t, out) = elts[i];
11712 out++;
11713 }
11714 }
11715
11716 return t;
11717 }
11718
11719 /* Substitute ARGS into one level PARMS of template parameters. */
11720
11721 static tree
11722 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11723 {
11724 if (parms == error_mark_node)
11725 return error_mark_node;
11726
11727 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11728
11729 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11730 {
11731 tree tuple = TREE_VEC_ELT (parms, i);
11732
11733 if (tuple == error_mark_node)
11734 continue;
11735
11736 TREE_VEC_ELT (new_vec, i) =
11737 tsubst_template_parm (tuple, args, complain);
11738 }
11739
11740 return new_vec;
11741 }
11742
11743 /* Return the result of substituting ARGS into the template parameters
11744 given by PARMS. If there are m levels of ARGS and m + n levels of
11745 PARMS, then the result will contain n levels of PARMS. For
11746 example, if PARMS is `template <class T> template <class U>
11747 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11748 result will be `template <int*, double, class V>'. */
11749
11750 static tree
11751 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11752 {
11753 tree r = NULL_TREE;
11754 tree* new_parms;
11755
11756 /* When substituting into a template, we must set
11757 PROCESSING_TEMPLATE_DECL as the template parameters may be
11758 dependent if they are based on one-another, and the dependency
11759 predicates are short-circuit outside of templates. */
11760 ++processing_template_decl;
11761
11762 for (new_parms = &r;
11763 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11764 new_parms = &(TREE_CHAIN (*new_parms)),
11765 parms = TREE_CHAIN (parms))
11766 {
11767 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11768 args, complain);
11769 *new_parms =
11770 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11771 - TMPL_ARGS_DEPTH (args)),
11772 new_vec, NULL_TREE);
11773 }
11774
11775 --processing_template_decl;
11776
11777 return r;
11778 }
11779
11780 /* Return the result of substituting ARGS into one template parameter
11781 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11782 parameter and which TREE_PURPOSE is the default argument of the
11783 template parameter. */
11784
11785 static tree
11786 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11787 {
11788 tree default_value, parm_decl;
11789
11790 if (args == NULL_TREE
11791 || t == NULL_TREE
11792 || t == error_mark_node)
11793 return t;
11794
11795 gcc_assert (TREE_CODE (t) == TREE_LIST);
11796
11797 default_value = TREE_PURPOSE (t);
11798 parm_decl = TREE_VALUE (t);
11799
11800 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11801 if (TREE_CODE (parm_decl) == PARM_DECL
11802 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11803 parm_decl = error_mark_node;
11804 default_value = tsubst_template_arg (default_value, args,
11805 complain, NULL_TREE);
11806
11807 return build_tree_list (default_value, parm_decl);
11808 }
11809
11810 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11811 type T. If T is not an aggregate or enumeration type, it is
11812 handled as if by tsubst. IN_DECL is as for tsubst. If
11813 ENTERING_SCOPE is nonzero, T is the context for a template which
11814 we are presently tsubst'ing. Return the substituted value. */
11815
11816 static tree
11817 tsubst_aggr_type (tree t,
11818 tree args,
11819 tsubst_flags_t complain,
11820 tree in_decl,
11821 int entering_scope)
11822 {
11823 if (t == NULL_TREE)
11824 return NULL_TREE;
11825
11826 switch (TREE_CODE (t))
11827 {
11828 case RECORD_TYPE:
11829 if (TYPE_PTRMEMFUNC_P (t))
11830 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11831
11832 /* Fall through. */
11833 case ENUMERAL_TYPE:
11834 case UNION_TYPE:
11835 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11836 {
11837 tree argvec;
11838 tree context;
11839 tree r;
11840 int saved_unevaluated_operand;
11841 int saved_inhibit_evaluation_warnings;
11842
11843 /* In "sizeof(X<I>)" we need to evaluate "I". */
11844 saved_unevaluated_operand = cp_unevaluated_operand;
11845 cp_unevaluated_operand = 0;
11846 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11847 c_inhibit_evaluation_warnings = 0;
11848
11849 /* First, determine the context for the type we are looking
11850 up. */
11851 context = TYPE_CONTEXT (t);
11852 if (context && TYPE_P (context))
11853 {
11854 context = tsubst_aggr_type (context, args, complain,
11855 in_decl, /*entering_scope=*/1);
11856 /* If context is a nested class inside a class template,
11857 it may still need to be instantiated (c++/33959). */
11858 context = complete_type (context);
11859 }
11860
11861 /* Then, figure out what arguments are appropriate for the
11862 type we are trying to find. For example, given:
11863
11864 template <class T> struct S;
11865 template <class T, class U> void f(T, U) { S<U> su; }
11866
11867 and supposing that we are instantiating f<int, double>,
11868 then our ARGS will be {int, double}, but, when looking up
11869 S we only want {double}. */
11870 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11871 complain, in_decl);
11872 if (argvec == error_mark_node)
11873 r = error_mark_node;
11874 else
11875 {
11876 r = lookup_template_class (t, argvec, in_decl, context,
11877 entering_scope, complain);
11878 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11879 }
11880
11881 cp_unevaluated_operand = saved_unevaluated_operand;
11882 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11883
11884 return r;
11885 }
11886 else
11887 /* This is not a template type, so there's nothing to do. */
11888 return t;
11889
11890 default:
11891 return tsubst (t, args, complain, in_decl);
11892 }
11893 }
11894
11895 /* Substitute into the default argument ARG (a default argument for
11896 FN), which has the indicated TYPE. */
11897
11898 tree
11899 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11900 {
11901 tree saved_class_ptr = NULL_TREE;
11902 tree saved_class_ref = NULL_TREE;
11903 int errs = errorcount + sorrycount;
11904
11905 /* This can happen in invalid code. */
11906 if (TREE_CODE (arg) == DEFAULT_ARG)
11907 return arg;
11908
11909 /* This default argument came from a template. Instantiate the
11910 default argument here, not in tsubst. In the case of
11911 something like:
11912
11913 template <class T>
11914 struct S {
11915 static T t();
11916 void f(T = t());
11917 };
11918
11919 we must be careful to do name lookup in the scope of S<T>,
11920 rather than in the current class. */
11921 push_access_scope (fn);
11922 /* The "this" pointer is not valid in a default argument. */
11923 if (cfun)
11924 {
11925 saved_class_ptr = current_class_ptr;
11926 cp_function_chain->x_current_class_ptr = NULL_TREE;
11927 saved_class_ref = current_class_ref;
11928 cp_function_chain->x_current_class_ref = NULL_TREE;
11929 }
11930
11931 push_deferring_access_checks(dk_no_deferred);
11932 /* The default argument expression may cause implicitly defined
11933 member functions to be synthesized, which will result in garbage
11934 collection. We must treat this situation as if we were within
11935 the body of function so as to avoid collecting live data on the
11936 stack. */
11937 ++function_depth;
11938 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11939 complain, NULL_TREE,
11940 /*integral_constant_expression_p=*/false);
11941 --function_depth;
11942 pop_deferring_access_checks();
11943
11944 /* Restore the "this" pointer. */
11945 if (cfun)
11946 {
11947 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11948 cp_function_chain->x_current_class_ref = saved_class_ref;
11949 }
11950
11951 if (errorcount+sorrycount > errs
11952 && (complain & tf_warning_or_error))
11953 inform (input_location,
11954 " when instantiating default argument for call to %D", fn);
11955
11956 /* Make sure the default argument is reasonable. */
11957 arg = check_default_argument (type, arg, complain);
11958
11959 pop_access_scope (fn);
11960
11961 return arg;
11962 }
11963
11964 /* Substitute into all the default arguments for FN. */
11965
11966 static void
11967 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11968 {
11969 tree arg;
11970 tree tmpl_args;
11971
11972 tmpl_args = DECL_TI_ARGS (fn);
11973
11974 /* If this function is not yet instantiated, we certainly don't need
11975 its default arguments. */
11976 if (uses_template_parms (tmpl_args))
11977 return;
11978 /* Don't do this again for clones. */
11979 if (DECL_CLONED_FUNCTION_P (fn))
11980 return;
11981
11982 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11983 arg;
11984 arg = TREE_CHAIN (arg))
11985 if (TREE_PURPOSE (arg))
11986 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11987 TREE_VALUE (arg),
11988 TREE_PURPOSE (arg),
11989 complain);
11990 }
11991
11992 /* Substitute the ARGS into the T, which is a _DECL. Return the
11993 result of the substitution. Issue error and warning messages under
11994 control of COMPLAIN. */
11995
11996 static tree
11997 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11998 {
11999 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12000 location_t saved_loc;
12001 tree r = NULL_TREE;
12002 tree in_decl = t;
12003 hashval_t hash = 0;
12004
12005 /* Set the filename and linenumber to improve error-reporting. */
12006 saved_loc = input_location;
12007 input_location = DECL_SOURCE_LOCATION (t);
12008
12009 switch (TREE_CODE (t))
12010 {
12011 case TEMPLATE_DECL:
12012 {
12013 /* We can get here when processing a member function template,
12014 member class template, or template template parameter. */
12015 tree decl = DECL_TEMPLATE_RESULT (t);
12016 tree spec;
12017 tree tmpl_args;
12018 tree full_args;
12019
12020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12021 {
12022 /* Template template parameter is treated here. */
12023 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12024 if (new_type == error_mark_node)
12025 r = error_mark_node;
12026 /* If we get a real template back, return it. This can happen in
12027 the context of most_specialized_partial_spec. */
12028 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12029 r = new_type;
12030 else
12031 /* The new TEMPLATE_DECL was built in
12032 reduce_template_parm_level. */
12033 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12034 break;
12035 }
12036
12037 /* We might already have an instance of this template.
12038 The ARGS are for the surrounding class type, so the
12039 full args contain the tsubst'd args for the context,
12040 plus the innermost args from the template decl. */
12041 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12042 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12043 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12044 /* Because this is a template, the arguments will still be
12045 dependent, even after substitution. If
12046 PROCESSING_TEMPLATE_DECL is not set, the dependency
12047 predicates will short-circuit. */
12048 ++processing_template_decl;
12049 full_args = tsubst_template_args (tmpl_args, args,
12050 complain, in_decl);
12051 --processing_template_decl;
12052 if (full_args == error_mark_node)
12053 RETURN (error_mark_node);
12054
12055 /* If this is a default template template argument,
12056 tsubst might not have changed anything. */
12057 if (full_args == tmpl_args)
12058 RETURN (t);
12059
12060 hash = hash_tmpl_and_args (t, full_args);
12061 spec = retrieve_specialization (t, full_args, hash);
12062 if (spec != NULL_TREE)
12063 {
12064 r = spec;
12065 break;
12066 }
12067
12068 /* Make a new template decl. It will be similar to the
12069 original, but will record the current template arguments.
12070 We also create a new function declaration, which is just
12071 like the old one, but points to this new template, rather
12072 than the old one. */
12073 r = copy_decl (t);
12074 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12075 DECL_CHAIN (r) = NULL_TREE;
12076
12077 // Build new template info linking to the original template decl.
12078 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12079
12080 if (TREE_CODE (decl) == TYPE_DECL
12081 && !TYPE_DECL_ALIAS_P (decl))
12082 {
12083 tree new_type;
12084 ++processing_template_decl;
12085 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12086 --processing_template_decl;
12087 if (new_type == error_mark_node)
12088 RETURN (error_mark_node);
12089
12090 TREE_TYPE (r) = new_type;
12091 /* For a partial specialization, we need to keep pointing to
12092 the primary template. */
12093 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12094 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12095 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12096 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12097 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12098 }
12099 else
12100 {
12101 tree new_decl;
12102 ++processing_template_decl;
12103 new_decl = tsubst (decl, args, complain, in_decl);
12104 --processing_template_decl;
12105 if (new_decl == error_mark_node)
12106 RETURN (error_mark_node);
12107
12108 DECL_TEMPLATE_RESULT (r) = new_decl;
12109 DECL_TI_TEMPLATE (new_decl) = r;
12110 TREE_TYPE (r) = TREE_TYPE (new_decl);
12111 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12112 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12113 }
12114
12115 SET_DECL_IMPLICIT_INSTANTIATION (r);
12116 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12117 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12118
12119 /* The template parameters for this new template are all the
12120 template parameters for the old template, except the
12121 outermost level of parameters. */
12122 DECL_TEMPLATE_PARMS (r)
12123 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12124 complain);
12125
12126 if (PRIMARY_TEMPLATE_P (t))
12127 DECL_PRIMARY_TEMPLATE (r) = r;
12128
12129 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12130 /* Record this non-type partial instantiation. */
12131 register_specialization (r, t,
12132 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12133 false, hash);
12134 }
12135 break;
12136
12137 case FUNCTION_DECL:
12138 {
12139 tree ctx;
12140 tree argvec = NULL_TREE;
12141 tree *friends;
12142 tree gen_tmpl;
12143 tree type;
12144 int member;
12145 int args_depth;
12146 int parms_depth;
12147
12148 /* Nobody should be tsubst'ing into non-template functions. */
12149 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12150
12151 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12152 {
12153 tree spec;
12154
12155 /* If T is not dependent, just return it. */
12156 if (!uses_template_parms (DECL_TI_ARGS (t)))
12157 RETURN (t);
12158
12159 /* Calculate the most general template of which R is a
12160 specialization, and the complete set of arguments used to
12161 specialize R. */
12162 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12163 argvec = tsubst_template_args (DECL_TI_ARGS
12164 (DECL_TEMPLATE_RESULT
12165 (DECL_TI_TEMPLATE (t))),
12166 args, complain, in_decl);
12167 if (argvec == error_mark_node)
12168 RETURN (error_mark_node);
12169
12170 /* Check to see if we already have this specialization. */
12171 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12172 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12173
12174 if (spec)
12175 {
12176 r = spec;
12177 break;
12178 }
12179
12180 /* We can see more levels of arguments than parameters if
12181 there was a specialization of a member template, like
12182 this:
12183
12184 template <class T> struct S { template <class U> void f(); }
12185 template <> template <class U> void S<int>::f(U);
12186
12187 Here, we'll be substituting into the specialization,
12188 because that's where we can find the code we actually
12189 want to generate, but we'll have enough arguments for
12190 the most general template.
12191
12192 We also deal with the peculiar case:
12193
12194 template <class T> struct S {
12195 template <class U> friend void f();
12196 };
12197 template <class U> void f() {}
12198 template S<int>;
12199 template void f<double>();
12200
12201 Here, the ARGS for the instantiation of will be {int,
12202 double}. But, we only need as many ARGS as there are
12203 levels of template parameters in CODE_PATTERN. We are
12204 careful not to get fooled into reducing the ARGS in
12205 situations like:
12206
12207 template <class T> struct S { template <class U> void f(U); }
12208 template <class T> template <> void S<T>::f(int) {}
12209
12210 which we can spot because the pattern will be a
12211 specialization in this case. */
12212 args_depth = TMPL_ARGS_DEPTH (args);
12213 parms_depth =
12214 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12215 if (args_depth > parms_depth
12216 && !DECL_TEMPLATE_SPECIALIZATION (t))
12217 args = get_innermost_template_args (args, parms_depth);
12218 }
12219 else
12220 {
12221 /* This special case arises when we have something like this:
12222
12223 template <class T> struct S {
12224 friend void f<int>(int, double);
12225 };
12226
12227 Here, the DECL_TI_TEMPLATE for the friend declaration
12228 will be an IDENTIFIER_NODE. We are being called from
12229 tsubst_friend_function, and we want only to create a
12230 new decl (R) with appropriate types so that we can call
12231 determine_specialization. */
12232 gen_tmpl = NULL_TREE;
12233 }
12234
12235 if (DECL_CLASS_SCOPE_P (t))
12236 {
12237 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12238 member = 2;
12239 else
12240 member = 1;
12241 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12242 complain, t, /*entering_scope=*/1);
12243 }
12244 else
12245 {
12246 member = 0;
12247 ctx = DECL_CONTEXT (t);
12248 }
12249 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12250 if (type == error_mark_node)
12251 RETURN (error_mark_node);
12252
12253 /* If we hit excessive deduction depth, the type is bogus even if
12254 it isn't error_mark_node, so don't build a decl. */
12255 if (excessive_deduction_depth)
12256 RETURN (error_mark_node);
12257
12258 /* We do NOT check for matching decls pushed separately at this
12259 point, as they may not represent instantiations of this
12260 template, and in any case are considered separate under the
12261 discrete model. */
12262 r = copy_decl (t);
12263 DECL_USE_TEMPLATE (r) = 0;
12264 TREE_TYPE (r) = type;
12265 /* Clear out the mangled name and RTL for the instantiation. */
12266 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12267 SET_DECL_RTL (r, NULL);
12268 /* Leave DECL_INITIAL set on deleted instantiations. */
12269 if (!DECL_DELETED_FN (r))
12270 DECL_INITIAL (r) = NULL_TREE;
12271 DECL_CONTEXT (r) = ctx;
12272
12273 /* OpenMP UDRs have the only argument a reference to the declared
12274 type. We want to diagnose if the declared type is a reference,
12275 which is invalid, but as references to references are usually
12276 quietly merged, diagnose it here. */
12277 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12278 {
12279 tree argtype
12280 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12281 argtype = tsubst (argtype, args, complain, in_decl);
12282 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12283 error_at (DECL_SOURCE_LOCATION (t),
12284 "reference type %qT in "
12285 "%<#pragma omp declare reduction%>", argtype);
12286 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12287 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12288 argtype);
12289 }
12290
12291 if (member && DECL_CONV_FN_P (r))
12292 /* Type-conversion operator. Reconstruct the name, in
12293 case it's the name of one of the template's parameters. */
12294 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12295
12296 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12297 complain, t);
12298 DECL_RESULT (r) = NULL_TREE;
12299
12300 TREE_STATIC (r) = 0;
12301 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12302 DECL_EXTERNAL (r) = 1;
12303 /* If this is an instantiation of a function with internal
12304 linkage, we already know what object file linkage will be
12305 assigned to the instantiation. */
12306 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12307 DECL_DEFER_OUTPUT (r) = 0;
12308 DECL_CHAIN (r) = NULL_TREE;
12309 DECL_PENDING_INLINE_INFO (r) = 0;
12310 DECL_PENDING_INLINE_P (r) = 0;
12311 DECL_SAVED_TREE (r) = NULL_TREE;
12312 DECL_STRUCT_FUNCTION (r) = NULL;
12313 TREE_USED (r) = 0;
12314 /* We'll re-clone as appropriate in instantiate_template. */
12315 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12316
12317 /* If we aren't complaining now, return on error before we register
12318 the specialization so that we'll complain eventually. */
12319 if ((complain & tf_error) == 0
12320 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12321 && !grok_op_properties (r, /*complain=*/false))
12322 RETURN (error_mark_node);
12323
12324 /* When instantiating a constrained member, substitute
12325 into the constraints to create a new constraint. */
12326 if (tree ci = get_constraints (t))
12327 if (member)
12328 {
12329 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12330 set_constraints (r, ci);
12331 }
12332
12333 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12334 this in the special friend case mentioned above where
12335 GEN_TMPL is NULL. */
12336 if (gen_tmpl)
12337 {
12338 DECL_TEMPLATE_INFO (r)
12339 = build_template_info (gen_tmpl, argvec);
12340 SET_DECL_IMPLICIT_INSTANTIATION (r);
12341
12342 tree new_r
12343 = register_specialization (r, gen_tmpl, argvec, false, hash);
12344 if (new_r != r)
12345 /* We instantiated this while substituting into
12346 the type earlier (template/friend54.C). */
12347 RETURN (new_r);
12348
12349 /* We're not supposed to instantiate default arguments
12350 until they are called, for a template. But, for a
12351 declaration like:
12352
12353 template <class T> void f ()
12354 { extern void g(int i = T()); }
12355
12356 we should do the substitution when the template is
12357 instantiated. We handle the member function case in
12358 instantiate_class_template since the default arguments
12359 might refer to other members of the class. */
12360 if (!member
12361 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12362 && !uses_template_parms (argvec))
12363 tsubst_default_arguments (r, complain);
12364 }
12365 else
12366 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12367
12368 /* Copy the list of befriending classes. */
12369 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12370 *friends;
12371 friends = &TREE_CHAIN (*friends))
12372 {
12373 *friends = copy_node (*friends);
12374 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12375 args, complain,
12376 in_decl);
12377 }
12378
12379 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12380 {
12381 maybe_retrofit_in_chrg (r);
12382 if (DECL_CONSTRUCTOR_P (r))
12383 grok_ctor_properties (ctx, r);
12384 /* If this is an instantiation of a member template, clone it.
12385 If it isn't, that'll be handled by
12386 clone_constructors_and_destructors. */
12387 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12388 clone_function_decl (r, /*update_method_vec_p=*/0);
12389 }
12390 else if ((complain & tf_error) != 0
12391 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12392 && !grok_op_properties (r, /*complain=*/true))
12393 RETURN (error_mark_node);
12394
12395 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12396 SET_DECL_FRIEND_CONTEXT (r,
12397 tsubst (DECL_FRIEND_CONTEXT (t),
12398 args, complain, in_decl));
12399
12400 /* Possibly limit visibility based on template args. */
12401 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12402 if (DECL_VISIBILITY_SPECIFIED (t))
12403 {
12404 DECL_VISIBILITY_SPECIFIED (r) = 0;
12405 DECL_ATTRIBUTES (r)
12406 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12407 }
12408 determine_visibility (r);
12409 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12410 && !processing_template_decl)
12411 defaulted_late_check (r);
12412
12413 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12414 args, complain, in_decl);
12415 }
12416 break;
12417
12418 case PARM_DECL:
12419 {
12420 tree type = NULL_TREE;
12421 int i, len = 1;
12422 tree expanded_types = NULL_TREE;
12423 tree prev_r = NULL_TREE;
12424 tree first_r = NULL_TREE;
12425
12426 if (DECL_PACK_P (t))
12427 {
12428 /* If there is a local specialization that isn't a
12429 parameter pack, it means that we're doing a "simple"
12430 substitution from inside tsubst_pack_expansion. Just
12431 return the local specialization (which will be a single
12432 parm). */
12433 tree spec = retrieve_local_specialization (t);
12434 if (spec
12435 && TREE_CODE (spec) == PARM_DECL
12436 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12437 RETURN (spec);
12438
12439 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12440 the parameters in this function parameter pack. */
12441 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12442 complain, in_decl);
12443 if (TREE_CODE (expanded_types) == TREE_VEC)
12444 {
12445 len = TREE_VEC_LENGTH (expanded_types);
12446
12447 /* Zero-length parameter packs are boring. Just substitute
12448 into the chain. */
12449 if (len == 0)
12450 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12451 TREE_CHAIN (t)));
12452 }
12453 else
12454 {
12455 /* All we did was update the type. Make a note of that. */
12456 type = expanded_types;
12457 expanded_types = NULL_TREE;
12458 }
12459 }
12460
12461 /* Loop through all of the parameters we'll build. When T is
12462 a function parameter pack, LEN is the number of expanded
12463 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12464 r = NULL_TREE;
12465 for (i = 0; i < len; ++i)
12466 {
12467 prev_r = r;
12468 r = copy_node (t);
12469 if (DECL_TEMPLATE_PARM_P (t))
12470 SET_DECL_TEMPLATE_PARM_P (r);
12471
12472 if (expanded_types)
12473 /* We're on the Ith parameter of the function parameter
12474 pack. */
12475 {
12476 /* Get the Ith type. */
12477 type = TREE_VEC_ELT (expanded_types, i);
12478
12479 /* Rename the parameter to include the index. */
12480 DECL_NAME (r)
12481 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12482 }
12483 else if (!type)
12484 /* We're dealing with a normal parameter. */
12485 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12486
12487 type = type_decays_to (type);
12488 TREE_TYPE (r) = type;
12489 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12490
12491 if (DECL_INITIAL (r))
12492 {
12493 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12494 DECL_INITIAL (r) = TREE_TYPE (r);
12495 else
12496 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12497 complain, in_decl);
12498 }
12499
12500 DECL_CONTEXT (r) = NULL_TREE;
12501
12502 if (!DECL_TEMPLATE_PARM_P (r))
12503 DECL_ARG_TYPE (r) = type_passed_as (type);
12504
12505 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12506 args, complain, in_decl);
12507
12508 /* Keep track of the first new parameter we
12509 generate. That's what will be returned to the
12510 caller. */
12511 if (!first_r)
12512 first_r = r;
12513
12514 /* Build a proper chain of parameters when substituting
12515 into a function parameter pack. */
12516 if (prev_r)
12517 DECL_CHAIN (prev_r) = r;
12518 }
12519
12520 /* If cp_unevaluated_operand is set, we're just looking for a
12521 single dummy parameter, so don't keep going. */
12522 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12523 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12524 complain, DECL_CHAIN (t));
12525
12526 /* FIRST_R contains the start of the chain we've built. */
12527 r = first_r;
12528 }
12529 break;
12530
12531 case FIELD_DECL:
12532 {
12533 tree type = NULL_TREE;
12534 tree vec = NULL_TREE;
12535 tree expanded_types = NULL_TREE;
12536 int len = 1;
12537
12538 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12539 {
12540 /* This field is a lambda capture pack. Return a TREE_VEC of
12541 the expanded fields to instantiate_class_template_1 and
12542 store them in the specializations hash table as a
12543 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12544 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12545 complain, in_decl);
12546 if (TREE_CODE (expanded_types) == TREE_VEC)
12547 {
12548 len = TREE_VEC_LENGTH (expanded_types);
12549 vec = make_tree_vec (len);
12550 }
12551 else
12552 {
12553 /* All we did was update the type. Make a note of that. */
12554 type = expanded_types;
12555 expanded_types = NULL_TREE;
12556 }
12557 }
12558
12559 for (int i = 0; i < len; ++i)
12560 {
12561 r = copy_decl (t);
12562 if (expanded_types)
12563 {
12564 type = TREE_VEC_ELT (expanded_types, i);
12565 DECL_NAME (r)
12566 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12567 }
12568 else if (!type)
12569 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12570
12571 if (type == error_mark_node)
12572 RETURN (error_mark_node);
12573 TREE_TYPE (r) = type;
12574 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12575
12576 if (DECL_C_BIT_FIELD (r))
12577 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12578 non-bit-fields DECL_INITIAL is a non-static data member
12579 initializer, which gets deferred instantiation. */
12580 DECL_INITIAL (r)
12581 = tsubst_expr (DECL_INITIAL (t), args,
12582 complain, in_decl,
12583 /*integral_constant_expression_p=*/true);
12584 else if (DECL_INITIAL (t))
12585 {
12586 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12587 NSDMI in perform_member_init. Still set DECL_INITIAL
12588 so that we know there is one. */
12589 DECL_INITIAL (r) = void_node;
12590 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12591 retrofit_lang_decl (r);
12592 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12593 }
12594 /* We don't have to set DECL_CONTEXT here; it is set by
12595 finish_member_declaration. */
12596 DECL_CHAIN (r) = NULL_TREE;
12597
12598 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12599 args, complain, in_decl);
12600
12601 if (vec)
12602 TREE_VEC_ELT (vec, i) = r;
12603 }
12604
12605 if (vec)
12606 {
12607 r = vec;
12608 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12609 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12610 SET_ARGUMENT_PACK_ARGS (pack, vec);
12611 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12612 TREE_TYPE (pack) = tpack;
12613 register_specialization (pack, t, args, false, 0);
12614 }
12615 }
12616 break;
12617
12618 case USING_DECL:
12619 /* We reach here only for member using decls. We also need to check
12620 uses_template_parms because DECL_DEPENDENT_P is not set for a
12621 using-declaration that designates a member of the current
12622 instantiation (c++/53549). */
12623 if (DECL_DEPENDENT_P (t)
12624 || uses_template_parms (USING_DECL_SCOPE (t)))
12625 {
12626 tree scope = USING_DECL_SCOPE (t);
12627 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12628 if (PACK_EXPANSION_P (scope))
12629 {
12630 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12631 int len = TREE_VEC_LENGTH (vec);
12632 r = make_tree_vec (len);
12633 for (int i = 0; i < len; ++i)
12634 {
12635 tree escope = TREE_VEC_ELT (vec, i);
12636 tree elt = do_class_using_decl (escope, name);
12637 if (!elt)
12638 {
12639 r = error_mark_node;
12640 break;
12641 }
12642 else
12643 {
12644 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12645 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12646 }
12647 TREE_VEC_ELT (r, i) = elt;
12648 }
12649 }
12650 else
12651 {
12652 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12653 complain, in_decl);
12654 r = do_class_using_decl (inst_scope, name);
12655 if (!r)
12656 r = error_mark_node;
12657 else
12658 {
12659 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12660 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12661 }
12662 }
12663 }
12664 else
12665 {
12666 r = copy_node (t);
12667 DECL_CHAIN (r) = NULL_TREE;
12668 }
12669 break;
12670
12671 case TYPE_DECL:
12672 case VAR_DECL:
12673 {
12674 tree argvec = NULL_TREE;
12675 tree gen_tmpl = NULL_TREE;
12676 tree spec;
12677 tree tmpl = NULL_TREE;
12678 tree ctx;
12679 tree type = NULL_TREE;
12680 bool local_p;
12681
12682 if (TREE_TYPE (t) == error_mark_node)
12683 RETURN (error_mark_node);
12684
12685 if (TREE_CODE (t) == TYPE_DECL
12686 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12687 {
12688 /* If this is the canonical decl, we don't have to
12689 mess with instantiations, and often we can't (for
12690 typename, template type parms and such). Note that
12691 TYPE_NAME is not correct for the above test if
12692 we've copied the type for a typedef. */
12693 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12694 if (type == error_mark_node)
12695 RETURN (error_mark_node);
12696 r = TYPE_NAME (type);
12697 break;
12698 }
12699
12700 /* Check to see if we already have the specialization we
12701 need. */
12702 spec = NULL_TREE;
12703 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12704 {
12705 /* T is a static data member or namespace-scope entity.
12706 We have to substitute into namespace-scope variables
12707 (not just variable templates) because of cases like:
12708
12709 template <class T> void f() { extern T t; }
12710
12711 where the entity referenced is not known until
12712 instantiation time. */
12713 local_p = false;
12714 ctx = DECL_CONTEXT (t);
12715 if (DECL_CLASS_SCOPE_P (t))
12716 {
12717 ctx = tsubst_aggr_type (ctx, args,
12718 complain,
12719 in_decl, /*entering_scope=*/1);
12720 /* If CTX is unchanged, then T is in fact the
12721 specialization we want. That situation occurs when
12722 referencing a static data member within in its own
12723 class. We can use pointer equality, rather than
12724 same_type_p, because DECL_CONTEXT is always
12725 canonical... */
12726 if (ctx == DECL_CONTEXT (t)
12727 /* ... unless T is a member template; in which
12728 case our caller can be willing to create a
12729 specialization of that template represented
12730 by T. */
12731 && !(DECL_TI_TEMPLATE (t)
12732 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12733 spec = t;
12734 }
12735
12736 if (!spec)
12737 {
12738 tmpl = DECL_TI_TEMPLATE (t);
12739 gen_tmpl = most_general_template (tmpl);
12740 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12741 if (argvec != error_mark_node)
12742 argvec = (coerce_innermost_template_parms
12743 (DECL_TEMPLATE_PARMS (gen_tmpl),
12744 argvec, t, complain,
12745 /*all*/true, /*defarg*/true));
12746 if (argvec == error_mark_node)
12747 RETURN (error_mark_node);
12748 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12749 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12750 }
12751 }
12752 else
12753 {
12754 /* A local variable. */
12755 local_p = true;
12756 /* Subsequent calls to pushdecl will fill this in. */
12757 ctx = NULL_TREE;
12758 /* Unless this is a reference to a static variable from an
12759 enclosing function, in which case we need to fill it in now. */
12760 if (TREE_STATIC (t))
12761 {
12762 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12763 if (fn != current_function_decl)
12764 ctx = fn;
12765 }
12766 spec = retrieve_local_specialization (t);
12767 }
12768 /* If we already have the specialization we need, there is
12769 nothing more to do. */
12770 if (spec)
12771 {
12772 r = spec;
12773 break;
12774 }
12775
12776 /* Create a new node for the specialization we need. */
12777 r = copy_decl (t);
12778 if (type == NULL_TREE)
12779 {
12780 if (is_typedef_decl (t))
12781 type = DECL_ORIGINAL_TYPE (t);
12782 else
12783 type = TREE_TYPE (t);
12784 if (VAR_P (t)
12785 && VAR_HAD_UNKNOWN_BOUND (t)
12786 && type != error_mark_node)
12787 type = strip_array_domain (type);
12788 type = tsubst (type, args, complain, in_decl);
12789 }
12790 if (VAR_P (r))
12791 {
12792 /* Even if the original location is out of scope, the
12793 newly substituted one is not. */
12794 DECL_DEAD_FOR_LOCAL (r) = 0;
12795 DECL_INITIALIZED_P (r) = 0;
12796 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12797 if (type == error_mark_node)
12798 RETURN (error_mark_node);
12799 if (TREE_CODE (type) == FUNCTION_TYPE)
12800 {
12801 /* It may seem that this case cannot occur, since:
12802
12803 typedef void f();
12804 void g() { f x; }
12805
12806 declares a function, not a variable. However:
12807
12808 typedef void f();
12809 template <typename T> void g() { T t; }
12810 template void g<f>();
12811
12812 is an attempt to declare a variable with function
12813 type. */
12814 error ("variable %qD has function type",
12815 /* R is not yet sufficiently initialized, so we
12816 just use its name. */
12817 DECL_NAME (r));
12818 RETURN (error_mark_node);
12819 }
12820 type = complete_type (type);
12821 /* Wait until cp_finish_decl to set this again, to handle
12822 circular dependency (template/instantiate6.C). */
12823 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12824 type = check_var_type (DECL_NAME (r), type);
12825
12826 if (DECL_HAS_VALUE_EXPR_P (t))
12827 {
12828 tree ve = DECL_VALUE_EXPR (t);
12829 ve = tsubst_expr (ve, args, complain, in_decl,
12830 /*constant_expression_p=*/false);
12831 if (REFERENCE_REF_P (ve))
12832 {
12833 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12834 ve = TREE_OPERAND (ve, 0);
12835 }
12836 SET_DECL_VALUE_EXPR (r, ve);
12837 }
12838 if (CP_DECL_THREAD_LOCAL_P (r)
12839 && !processing_template_decl)
12840 set_decl_tls_model (r, decl_default_tls_model (r));
12841 }
12842 else if (DECL_SELF_REFERENCE_P (t))
12843 SET_DECL_SELF_REFERENCE_P (r);
12844 TREE_TYPE (r) = type;
12845 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12846 DECL_CONTEXT (r) = ctx;
12847 /* Clear out the mangled name and RTL for the instantiation. */
12848 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12849 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12850 SET_DECL_RTL (r, NULL);
12851 /* The initializer must not be expanded until it is required;
12852 see [temp.inst]. */
12853 DECL_INITIAL (r) = NULL_TREE;
12854 if (VAR_P (r))
12855 SET_DECL_MODE (r, VOIDmode);
12856 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12857 SET_DECL_RTL (r, NULL);
12858 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12859 if (VAR_P (r))
12860 {
12861 /* Possibly limit visibility based on template args. */
12862 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12863 if (DECL_VISIBILITY_SPECIFIED (t))
12864 {
12865 DECL_VISIBILITY_SPECIFIED (r) = 0;
12866 DECL_ATTRIBUTES (r)
12867 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12868 }
12869 determine_visibility (r);
12870 }
12871
12872 if (!local_p)
12873 {
12874 /* A static data member declaration is always marked
12875 external when it is declared in-class, even if an
12876 initializer is present. We mimic the non-template
12877 processing here. */
12878 DECL_EXTERNAL (r) = 1;
12879 if (DECL_NAMESPACE_SCOPE_P (t))
12880 DECL_NOT_REALLY_EXTERN (r) = 1;
12881
12882 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12883 SET_DECL_IMPLICIT_INSTANTIATION (r);
12884 register_specialization (r, gen_tmpl, argvec, false, hash);
12885 }
12886 else
12887 {
12888 if (DECL_LANG_SPECIFIC (r))
12889 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12890 if (!cp_unevaluated_operand)
12891 register_local_specialization (r, t);
12892 }
12893
12894 DECL_CHAIN (r) = NULL_TREE;
12895
12896 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12897 /*flags=*/0,
12898 args, complain, in_decl);
12899
12900 /* Preserve a typedef that names a type. */
12901 if (is_typedef_decl (r) && type != error_mark_node)
12902 {
12903 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12904 set_underlying_type (r);
12905 if (TYPE_DECL_ALIAS_P (r))
12906 /* An alias template specialization can be dependent
12907 even if its underlying type is not. */
12908 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12909 }
12910
12911 layout_decl (r, 0);
12912 }
12913 break;
12914
12915 default:
12916 gcc_unreachable ();
12917 }
12918 #undef RETURN
12919
12920 out:
12921 /* Restore the file and line information. */
12922 input_location = saved_loc;
12923
12924 return r;
12925 }
12926
12927 /* Substitute into the ARG_TYPES of a function type.
12928 If END is a TREE_CHAIN, leave it and any following types
12929 un-substituted. */
12930
12931 static tree
12932 tsubst_arg_types (tree arg_types,
12933 tree args,
12934 tree end,
12935 tsubst_flags_t complain,
12936 tree in_decl)
12937 {
12938 tree remaining_arg_types;
12939 tree type = NULL_TREE;
12940 int i = 1;
12941 tree expanded_args = NULL_TREE;
12942 tree default_arg;
12943
12944 if (!arg_types || arg_types == void_list_node || arg_types == end)
12945 return arg_types;
12946
12947 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12948 args, end, complain, in_decl);
12949 if (remaining_arg_types == error_mark_node)
12950 return error_mark_node;
12951
12952 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12953 {
12954 /* For a pack expansion, perform substitution on the
12955 entire expression. Later on, we'll handle the arguments
12956 one-by-one. */
12957 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12958 args, complain, in_decl);
12959
12960 if (TREE_CODE (expanded_args) == TREE_VEC)
12961 /* So that we'll spin through the parameters, one by one. */
12962 i = TREE_VEC_LENGTH (expanded_args);
12963 else
12964 {
12965 /* We only partially substituted into the parameter
12966 pack. Our type is TYPE_PACK_EXPANSION. */
12967 type = expanded_args;
12968 expanded_args = NULL_TREE;
12969 }
12970 }
12971
12972 while (i > 0) {
12973 --i;
12974
12975 if (expanded_args)
12976 type = TREE_VEC_ELT (expanded_args, i);
12977 else if (!type)
12978 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12979
12980 if (type == error_mark_node)
12981 return error_mark_node;
12982 if (VOID_TYPE_P (type))
12983 {
12984 if (complain & tf_error)
12985 {
12986 error ("invalid parameter type %qT", type);
12987 if (in_decl)
12988 error ("in declaration %q+D", in_decl);
12989 }
12990 return error_mark_node;
12991 }
12992 /* DR 657. */
12993 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12994 return error_mark_node;
12995
12996 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12997 top-level qualifiers as required. */
12998 type = cv_unqualified (type_decays_to (type));
12999
13000 /* We do not substitute into default arguments here. The standard
13001 mandates that they be instantiated only when needed, which is
13002 done in build_over_call. */
13003 default_arg = TREE_PURPOSE (arg_types);
13004
13005 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13006 {
13007 /* We've instantiated a template before its default arguments
13008 have been parsed. This can happen for a nested template
13009 class, and is not an error unless we require the default
13010 argument in a call of this function. */
13011 remaining_arg_types =
13012 tree_cons (default_arg, type, remaining_arg_types);
13013 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13014 }
13015 else
13016 remaining_arg_types =
13017 hash_tree_cons (default_arg, type, remaining_arg_types);
13018 }
13019
13020 return remaining_arg_types;
13021 }
13022
13023 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13024 *not* handle the exception-specification for FNTYPE, because the
13025 initial substitution of explicitly provided template parameters
13026 during argument deduction forbids substitution into the
13027 exception-specification:
13028
13029 [temp.deduct]
13030
13031 All references in the function type of the function template to the
13032 corresponding template parameters are replaced by the specified tem-
13033 plate argument values. If a substitution in a template parameter or
13034 in the function type of the function template results in an invalid
13035 type, type deduction fails. [Note: The equivalent substitution in
13036 exception specifications is done only when the function is instanti-
13037 ated, at which point a program is ill-formed if the substitution
13038 results in an invalid type.] */
13039
13040 static tree
13041 tsubst_function_type (tree t,
13042 tree args,
13043 tsubst_flags_t complain,
13044 tree in_decl)
13045 {
13046 tree return_type;
13047 tree arg_types = NULL_TREE;
13048 tree fntype;
13049
13050 /* The TYPE_CONTEXT is not used for function/method types. */
13051 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13052
13053 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13054 failure. */
13055 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13056
13057 if (late_return_type_p)
13058 {
13059 /* Substitute the argument types. */
13060 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13061 complain, in_decl);
13062 if (arg_types == error_mark_node)
13063 return error_mark_node;
13064
13065 tree save_ccp = current_class_ptr;
13066 tree save_ccr = current_class_ref;
13067 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13068 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13069 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13070 if (do_inject)
13071 {
13072 /* DR 1207: 'this' is in scope in the trailing return type. */
13073 inject_this_parameter (this_type, cp_type_quals (this_type));
13074 }
13075
13076 /* Substitute the return type. */
13077 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13078
13079 if (do_inject)
13080 {
13081 current_class_ptr = save_ccp;
13082 current_class_ref = save_ccr;
13083 }
13084 }
13085 else
13086 /* Substitute the return type. */
13087 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13088
13089 if (return_type == error_mark_node)
13090 return error_mark_node;
13091 /* DR 486 clarifies that creation of a function type with an
13092 invalid return type is a deduction failure. */
13093 if (TREE_CODE (return_type) == ARRAY_TYPE
13094 || TREE_CODE (return_type) == FUNCTION_TYPE)
13095 {
13096 if (complain & tf_error)
13097 {
13098 if (TREE_CODE (return_type) == ARRAY_TYPE)
13099 error ("function returning an array");
13100 else
13101 error ("function returning a function");
13102 }
13103 return error_mark_node;
13104 }
13105 /* And DR 657. */
13106 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13107 return error_mark_node;
13108
13109 if (!late_return_type_p)
13110 {
13111 /* Substitute the argument types. */
13112 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13113 complain, in_decl);
13114 if (arg_types == error_mark_node)
13115 return error_mark_node;
13116 }
13117
13118 /* Construct a new type node and return it. */
13119 if (TREE_CODE (t) == FUNCTION_TYPE)
13120 {
13121 fntype = build_function_type (return_type, arg_types);
13122 fntype = apply_memfn_quals (fntype,
13123 type_memfn_quals (t),
13124 type_memfn_rqual (t));
13125 }
13126 else
13127 {
13128 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13129 /* Don't pick up extra function qualifiers from the basetype. */
13130 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13131 if (! MAYBE_CLASS_TYPE_P (r))
13132 {
13133 /* [temp.deduct]
13134
13135 Type deduction may fail for any of the following
13136 reasons:
13137
13138 -- Attempting to create "pointer to member of T" when T
13139 is not a class type. */
13140 if (complain & tf_error)
13141 error ("creating pointer to member function of non-class type %qT",
13142 r);
13143 return error_mark_node;
13144 }
13145
13146 fntype = build_method_type_directly (r, return_type,
13147 TREE_CHAIN (arg_types));
13148 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13149 }
13150 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13151
13152 if (late_return_type_p)
13153 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13154
13155 return fntype;
13156 }
13157
13158 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13159 ARGS into that specification, and return the substituted
13160 specification. If there is no specification, return NULL_TREE. */
13161
13162 static tree
13163 tsubst_exception_specification (tree fntype,
13164 tree args,
13165 tsubst_flags_t complain,
13166 tree in_decl,
13167 bool defer_ok)
13168 {
13169 tree specs;
13170 tree new_specs;
13171
13172 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13173 new_specs = NULL_TREE;
13174 if (specs && TREE_PURPOSE (specs))
13175 {
13176 /* A noexcept-specifier. */
13177 tree expr = TREE_PURPOSE (specs);
13178 if (TREE_CODE (expr) == INTEGER_CST)
13179 new_specs = expr;
13180 else if (defer_ok)
13181 {
13182 /* Defer instantiation of noexcept-specifiers to avoid
13183 excessive instantiations (c++/49107). */
13184 new_specs = make_node (DEFERRED_NOEXCEPT);
13185 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13186 {
13187 /* We already partially instantiated this member template,
13188 so combine the new args with the old. */
13189 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13190 = DEFERRED_NOEXCEPT_PATTERN (expr);
13191 DEFERRED_NOEXCEPT_ARGS (new_specs)
13192 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13193 }
13194 else
13195 {
13196 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13197 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13198 }
13199 }
13200 else
13201 new_specs = tsubst_copy_and_build
13202 (expr, args, complain, in_decl, /*function_p=*/false,
13203 /*integral_constant_expression_p=*/true);
13204 new_specs = build_noexcept_spec (new_specs, complain);
13205 }
13206 else if (specs)
13207 {
13208 if (! TREE_VALUE (specs))
13209 new_specs = specs;
13210 else
13211 while (specs)
13212 {
13213 tree spec;
13214 int i, len = 1;
13215 tree expanded_specs = NULL_TREE;
13216
13217 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13218 {
13219 /* Expand the pack expansion type. */
13220 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13221 args, complain,
13222 in_decl);
13223
13224 if (expanded_specs == error_mark_node)
13225 return error_mark_node;
13226 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13227 len = TREE_VEC_LENGTH (expanded_specs);
13228 else
13229 {
13230 /* We're substituting into a member template, so
13231 we got a TYPE_PACK_EXPANSION back. Add that
13232 expansion and move on. */
13233 gcc_assert (TREE_CODE (expanded_specs)
13234 == TYPE_PACK_EXPANSION);
13235 new_specs = add_exception_specifier (new_specs,
13236 expanded_specs,
13237 complain);
13238 specs = TREE_CHAIN (specs);
13239 continue;
13240 }
13241 }
13242
13243 for (i = 0; i < len; ++i)
13244 {
13245 if (expanded_specs)
13246 spec = TREE_VEC_ELT (expanded_specs, i);
13247 else
13248 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13249 if (spec == error_mark_node)
13250 return spec;
13251 new_specs = add_exception_specifier (new_specs, spec,
13252 complain);
13253 }
13254
13255 specs = TREE_CHAIN (specs);
13256 }
13257 }
13258 return new_specs;
13259 }
13260
13261 /* Take the tree structure T and replace template parameters used
13262 therein with the argument vector ARGS. IN_DECL is an associated
13263 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13264 Issue error and warning messages under control of COMPLAIN. Note
13265 that we must be relatively non-tolerant of extensions here, in
13266 order to preserve conformance; if we allow substitutions that
13267 should not be allowed, we may allow argument deductions that should
13268 not succeed, and therefore report ambiguous overload situations
13269 where there are none. In theory, we could allow the substitution,
13270 but indicate that it should have failed, and allow our caller to
13271 make sure that the right thing happens, but we don't try to do this
13272 yet.
13273
13274 This function is used for dealing with types, decls and the like;
13275 for expressions, use tsubst_expr or tsubst_copy. */
13276
13277 tree
13278 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13279 {
13280 enum tree_code code;
13281 tree type, r = NULL_TREE;
13282
13283 if (t == NULL_TREE || t == error_mark_node
13284 || t == integer_type_node
13285 || t == void_type_node
13286 || t == char_type_node
13287 || t == unknown_type_node
13288 || TREE_CODE (t) == NAMESPACE_DECL
13289 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13290 return t;
13291
13292 if (DECL_P (t))
13293 return tsubst_decl (t, args, complain);
13294
13295 if (args == NULL_TREE)
13296 return t;
13297
13298 code = TREE_CODE (t);
13299
13300 if (code == IDENTIFIER_NODE)
13301 type = IDENTIFIER_TYPE_VALUE (t);
13302 else
13303 type = TREE_TYPE (t);
13304
13305 gcc_assert (type != unknown_type_node);
13306
13307 /* Reuse typedefs. We need to do this to handle dependent attributes,
13308 such as attribute aligned. */
13309 if (TYPE_P (t)
13310 && typedef_variant_p (t))
13311 {
13312 tree decl = TYPE_NAME (t);
13313
13314 if (alias_template_specialization_p (t))
13315 {
13316 /* DECL represents an alias template and we want to
13317 instantiate it. */
13318 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13319 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13320 r = instantiate_alias_template (tmpl, gen_args, complain);
13321 }
13322 else if (DECL_CLASS_SCOPE_P (decl)
13323 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13324 && uses_template_parms (DECL_CONTEXT (decl)))
13325 {
13326 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13327 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13328 r = retrieve_specialization (tmpl, gen_args, 0);
13329 }
13330 else if (DECL_FUNCTION_SCOPE_P (decl)
13331 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13332 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13333 r = retrieve_local_specialization (decl);
13334 else
13335 /* The typedef is from a non-template context. */
13336 return t;
13337
13338 if (r)
13339 {
13340 r = TREE_TYPE (r);
13341 r = cp_build_qualified_type_real
13342 (r, cp_type_quals (t) | cp_type_quals (r),
13343 complain | tf_ignore_bad_quals);
13344 return r;
13345 }
13346 else
13347 {
13348 /* We don't have an instantiation yet, so drop the typedef. */
13349 int quals = cp_type_quals (t);
13350 t = DECL_ORIGINAL_TYPE (decl);
13351 t = cp_build_qualified_type_real (t, quals,
13352 complain | tf_ignore_bad_quals);
13353 }
13354 }
13355
13356 bool fndecl_type = (complain & tf_fndecl_type);
13357 complain &= ~tf_fndecl_type;
13358
13359 if (type
13360 && code != TYPENAME_TYPE
13361 && code != TEMPLATE_TYPE_PARM
13362 && code != IDENTIFIER_NODE
13363 && code != FUNCTION_TYPE
13364 && code != METHOD_TYPE)
13365 type = tsubst (type, args, complain, in_decl);
13366 if (type == error_mark_node)
13367 return error_mark_node;
13368
13369 switch (code)
13370 {
13371 case RECORD_TYPE:
13372 case UNION_TYPE:
13373 case ENUMERAL_TYPE:
13374 return tsubst_aggr_type (t, args, complain, in_decl,
13375 /*entering_scope=*/0);
13376
13377 case ERROR_MARK:
13378 case IDENTIFIER_NODE:
13379 case VOID_TYPE:
13380 case REAL_TYPE:
13381 case COMPLEX_TYPE:
13382 case VECTOR_TYPE:
13383 case BOOLEAN_TYPE:
13384 case NULLPTR_TYPE:
13385 case LANG_TYPE:
13386 return t;
13387
13388 case INTEGER_TYPE:
13389 if (t == integer_type_node)
13390 return t;
13391
13392 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13393 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13394 return t;
13395
13396 {
13397 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13398
13399 max = tsubst_expr (omax, args, complain, in_decl,
13400 /*integral_constant_expression_p=*/false);
13401
13402 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13403 needed. */
13404 if (TREE_CODE (max) == NOP_EXPR
13405 && TREE_SIDE_EFFECTS (omax)
13406 && !TREE_TYPE (max))
13407 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13408
13409 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13410 with TREE_SIDE_EFFECTS that indicates this is not an integral
13411 constant expression. */
13412 if (processing_template_decl
13413 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13414 {
13415 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13416 TREE_SIDE_EFFECTS (max) = 1;
13417 }
13418
13419 return compute_array_index_type (NULL_TREE, max, complain);
13420 }
13421
13422 case TEMPLATE_TYPE_PARM:
13423 case TEMPLATE_TEMPLATE_PARM:
13424 case BOUND_TEMPLATE_TEMPLATE_PARM:
13425 case TEMPLATE_PARM_INDEX:
13426 {
13427 int idx;
13428 int level;
13429 int levels;
13430 tree arg = NULL_TREE;
13431
13432 /* Early in template argument deduction substitution, we don't
13433 want to reduce the level of 'auto', or it will be confused
13434 with a normal template parm in subsequent deduction. */
13435 if (is_auto (t) && (complain & tf_partial))
13436 return t;
13437
13438 r = NULL_TREE;
13439
13440 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13441 template_parm_level_and_index (t, &level, &idx);
13442
13443 levels = TMPL_ARGS_DEPTH (args);
13444 if (level <= levels
13445 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13446 {
13447 arg = TMPL_ARG (args, level, idx);
13448
13449 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13450 {
13451 /* See through ARGUMENT_PACK_SELECT arguments. */
13452 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13453 /* If the selected argument is an expansion E, that most
13454 likely means we were called from
13455 gen_elem_of_pack_expansion_instantiation during the
13456 substituting of pack an argument pack (which Ith
13457 element is a pack expansion, where I is
13458 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13459 In this case, the Ith element resulting from this
13460 substituting is going to be a pack expansion, which
13461 pattern is the pattern of E. Let's return the
13462 pattern of E, and
13463 gen_elem_of_pack_expansion_instantiation will
13464 build the resulting pack expansion from it. */
13465 if (PACK_EXPANSION_P (arg))
13466 {
13467 /* Make sure we aren't throwing away arg info. */
13468 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13469 arg = PACK_EXPANSION_PATTERN (arg);
13470 }
13471 }
13472 }
13473
13474 if (arg == error_mark_node)
13475 return error_mark_node;
13476 else if (arg != NULL_TREE)
13477 {
13478 if (ARGUMENT_PACK_P (arg))
13479 /* If ARG is an argument pack, we don't actually want to
13480 perform a substitution here, because substitutions
13481 for argument packs are only done
13482 element-by-element. We can get to this point when
13483 substituting the type of a non-type template
13484 parameter pack, when that type actually contains
13485 template parameter packs from an outer template, e.g.,
13486
13487 template<typename... Types> struct A {
13488 template<Types... Values> struct B { };
13489 }; */
13490 return t;
13491
13492 if (code == TEMPLATE_TYPE_PARM)
13493 {
13494 int quals;
13495 gcc_assert (TYPE_P (arg));
13496
13497 quals = cp_type_quals (arg) | cp_type_quals (t);
13498
13499 return cp_build_qualified_type_real
13500 (arg, quals, complain | tf_ignore_bad_quals);
13501 }
13502 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13503 {
13504 /* We are processing a type constructed from a
13505 template template parameter. */
13506 tree argvec = tsubst (TYPE_TI_ARGS (t),
13507 args, complain, in_decl);
13508 if (argvec == error_mark_node)
13509 return error_mark_node;
13510
13511 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13512 || TREE_CODE (arg) == TEMPLATE_DECL
13513 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13514
13515 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13516 /* Consider this code:
13517
13518 template <template <class> class Template>
13519 struct Internal {
13520 template <class Arg> using Bind = Template<Arg>;
13521 };
13522
13523 template <template <class> class Template, class Arg>
13524 using Instantiate = Template<Arg>; //#0
13525
13526 template <template <class> class Template,
13527 class Argument>
13528 using Bind =
13529 Instantiate<Internal<Template>::template Bind,
13530 Argument>; //#1
13531
13532 When #1 is parsed, the
13533 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13534 parameter `Template' in #0 matches the
13535 UNBOUND_CLASS_TEMPLATE representing the argument
13536 `Internal<Template>::template Bind'; We then want
13537 to assemble the type `Bind<Argument>' that can't
13538 be fully created right now, because
13539 `Internal<Template>' not being complete, the Bind
13540 template cannot be looked up in that context. So
13541 we need to "store" `Bind<Argument>' for later
13542 when the context of Bind becomes complete. Let's
13543 store that in a TYPENAME_TYPE. */
13544 return make_typename_type (TYPE_CONTEXT (arg),
13545 build_nt (TEMPLATE_ID_EXPR,
13546 TYPE_IDENTIFIER (arg),
13547 argvec),
13548 typename_type,
13549 complain);
13550
13551 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13552 are resolving nested-types in the signature of a
13553 member function templates. Otherwise ARG is a
13554 TEMPLATE_DECL and is the real template to be
13555 instantiated. */
13556 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13557 arg = TYPE_NAME (arg);
13558
13559 r = lookup_template_class (arg,
13560 argvec, in_decl,
13561 DECL_CONTEXT (arg),
13562 /*entering_scope=*/0,
13563 complain);
13564 return cp_build_qualified_type_real
13565 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13566 }
13567 else if (code == TEMPLATE_TEMPLATE_PARM)
13568 return arg;
13569 else
13570 /* TEMPLATE_PARM_INDEX. */
13571 return convert_from_reference (unshare_expr (arg));
13572 }
13573
13574 if (level == 1)
13575 /* This can happen during the attempted tsubst'ing in
13576 unify. This means that we don't yet have any information
13577 about the template parameter in question. */
13578 return t;
13579
13580 /* If we get here, we must have been looking at a parm for a
13581 more deeply nested template. Make a new version of this
13582 template parameter, but with a lower level. */
13583 switch (code)
13584 {
13585 case TEMPLATE_TYPE_PARM:
13586 case TEMPLATE_TEMPLATE_PARM:
13587 case BOUND_TEMPLATE_TEMPLATE_PARM:
13588 if (cp_type_quals (t))
13589 {
13590 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13591 r = cp_build_qualified_type_real
13592 (r, cp_type_quals (t),
13593 complain | (code == TEMPLATE_TYPE_PARM
13594 ? tf_ignore_bad_quals : 0));
13595 }
13596 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13597 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13598 && (r = (TEMPLATE_PARM_DESCENDANTS
13599 (TEMPLATE_TYPE_PARM_INDEX (t))))
13600 && (r = TREE_TYPE (r))
13601 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13602 /* Break infinite recursion when substituting the constraints
13603 of a constrained placeholder. */;
13604 else
13605 {
13606 r = copy_type (t);
13607 TEMPLATE_TYPE_PARM_INDEX (r)
13608 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13609 r, levels, args, complain);
13610 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13611 TYPE_MAIN_VARIANT (r) = r;
13612 TYPE_POINTER_TO (r) = NULL_TREE;
13613 TYPE_REFERENCE_TO (r) = NULL_TREE;
13614
13615 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13616 {
13617 /* Propagate constraints on placeholders. */
13618 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13619 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13620 = tsubst_constraint (constr, args, complain, in_decl);
13621 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13622 {
13623 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13624 pl = tsubst (pl, args, complain, in_decl);
13625 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13626 }
13627 }
13628
13629 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13630 /* We have reduced the level of the template
13631 template parameter, but not the levels of its
13632 template parameters, so canonical_type_parameter
13633 will not be able to find the canonical template
13634 template parameter for this level. Thus, we
13635 require structural equality checking to compare
13636 TEMPLATE_TEMPLATE_PARMs. */
13637 SET_TYPE_STRUCTURAL_EQUALITY (r);
13638 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13639 SET_TYPE_STRUCTURAL_EQUALITY (r);
13640 else
13641 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13642
13643 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13644 {
13645 tree tinfo = TYPE_TEMPLATE_INFO (t);
13646 /* We might need to substitute into the types of non-type
13647 template parameters. */
13648 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13649 complain, in_decl);
13650 if (tmpl == error_mark_node)
13651 return error_mark_node;
13652 tree argvec = tsubst (TI_ARGS (tinfo), args,
13653 complain, in_decl);
13654 if (argvec == error_mark_node)
13655 return error_mark_node;
13656
13657 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13658 = build_template_info (tmpl, argvec);
13659 }
13660 }
13661 break;
13662
13663 case TEMPLATE_PARM_INDEX:
13664 r = reduce_template_parm_level (t, type, levels, args, complain);
13665 break;
13666
13667 default:
13668 gcc_unreachable ();
13669 }
13670
13671 return r;
13672 }
13673
13674 case TREE_LIST:
13675 {
13676 tree purpose, value, chain;
13677
13678 if (t == void_list_node)
13679 return t;
13680
13681 purpose = TREE_PURPOSE (t);
13682 if (purpose)
13683 {
13684 purpose = tsubst (purpose, args, complain, in_decl);
13685 if (purpose == error_mark_node)
13686 return error_mark_node;
13687 }
13688 value = TREE_VALUE (t);
13689 if (value)
13690 {
13691 value = tsubst (value, args, complain, in_decl);
13692 if (value == error_mark_node)
13693 return error_mark_node;
13694 }
13695 chain = TREE_CHAIN (t);
13696 if (chain && chain != void_type_node)
13697 {
13698 chain = tsubst (chain, args, complain, in_decl);
13699 if (chain == error_mark_node)
13700 return error_mark_node;
13701 }
13702 if (purpose == TREE_PURPOSE (t)
13703 && value == TREE_VALUE (t)
13704 && chain == TREE_CHAIN (t))
13705 return t;
13706 return hash_tree_cons (purpose, value, chain);
13707 }
13708
13709 case TREE_BINFO:
13710 /* We should never be tsubsting a binfo. */
13711 gcc_unreachable ();
13712
13713 case TREE_VEC:
13714 /* A vector of template arguments. */
13715 gcc_assert (!type);
13716 return tsubst_template_args (t, args, complain, in_decl);
13717
13718 case POINTER_TYPE:
13719 case REFERENCE_TYPE:
13720 {
13721 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13722 return t;
13723
13724 /* [temp.deduct]
13725
13726 Type deduction may fail for any of the following
13727 reasons:
13728
13729 -- Attempting to create a pointer to reference type.
13730 -- Attempting to create a reference to a reference type or
13731 a reference to void.
13732
13733 Core issue 106 says that creating a reference to a reference
13734 during instantiation is no longer a cause for failure. We
13735 only enforce this check in strict C++98 mode. */
13736 if ((TREE_CODE (type) == REFERENCE_TYPE
13737 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13738 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13739 {
13740 static location_t last_loc;
13741
13742 /* We keep track of the last time we issued this error
13743 message to avoid spewing a ton of messages during a
13744 single bad template instantiation. */
13745 if (complain & tf_error
13746 && last_loc != input_location)
13747 {
13748 if (VOID_TYPE_P (type))
13749 error ("forming reference to void");
13750 else if (code == POINTER_TYPE)
13751 error ("forming pointer to reference type %qT", type);
13752 else
13753 error ("forming reference to reference type %qT", type);
13754 last_loc = input_location;
13755 }
13756
13757 return error_mark_node;
13758 }
13759 else if (TREE_CODE (type) == FUNCTION_TYPE
13760 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13761 || type_memfn_rqual (type) != REF_QUAL_NONE))
13762 {
13763 if (complain & tf_error)
13764 {
13765 if (code == POINTER_TYPE)
13766 error ("forming pointer to qualified function type %qT",
13767 type);
13768 else
13769 error ("forming reference to qualified function type %qT",
13770 type);
13771 }
13772 return error_mark_node;
13773 }
13774 else if (code == POINTER_TYPE)
13775 {
13776 r = build_pointer_type (type);
13777 if (TREE_CODE (type) == METHOD_TYPE)
13778 r = build_ptrmemfunc_type (r);
13779 }
13780 else if (TREE_CODE (type) == REFERENCE_TYPE)
13781 /* In C++0x, during template argument substitution, when there is an
13782 attempt to create a reference to a reference type, reference
13783 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13784
13785 "If a template-argument for a template-parameter T names a type
13786 that is a reference to a type A, an attempt to create the type
13787 'lvalue reference to cv T' creates the type 'lvalue reference to
13788 A,' while an attempt to create the type type rvalue reference to
13789 cv T' creates the type T"
13790 */
13791 r = cp_build_reference_type
13792 (TREE_TYPE (type),
13793 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13794 else
13795 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13796 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13797
13798 if (r != error_mark_node)
13799 /* Will this ever be needed for TYPE_..._TO values? */
13800 layout_type (r);
13801
13802 return r;
13803 }
13804 case OFFSET_TYPE:
13805 {
13806 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13807 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13808 {
13809 /* [temp.deduct]
13810
13811 Type deduction may fail for any of the following
13812 reasons:
13813
13814 -- Attempting to create "pointer to member of T" when T
13815 is not a class type. */
13816 if (complain & tf_error)
13817 error ("creating pointer to member of non-class type %qT", r);
13818 return error_mark_node;
13819 }
13820 if (TREE_CODE (type) == REFERENCE_TYPE)
13821 {
13822 if (complain & tf_error)
13823 error ("creating pointer to member reference type %qT", type);
13824 return error_mark_node;
13825 }
13826 if (VOID_TYPE_P (type))
13827 {
13828 if (complain & tf_error)
13829 error ("creating pointer to member of type void");
13830 return error_mark_node;
13831 }
13832 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13833 if (TREE_CODE (type) == FUNCTION_TYPE)
13834 {
13835 /* The type of the implicit object parameter gets its
13836 cv-qualifiers from the FUNCTION_TYPE. */
13837 tree memptr;
13838 tree method_type
13839 = build_memfn_type (type, r, type_memfn_quals (type),
13840 type_memfn_rqual (type));
13841 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13842 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13843 complain);
13844 }
13845 else
13846 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13847 cp_type_quals (t),
13848 complain);
13849 }
13850 case FUNCTION_TYPE:
13851 case METHOD_TYPE:
13852 {
13853 tree fntype;
13854 tree specs;
13855 fntype = tsubst_function_type (t, args, complain, in_decl);
13856 if (fntype == error_mark_node)
13857 return error_mark_node;
13858
13859 /* Substitute the exception specification. */
13860 specs = tsubst_exception_specification (t, args, complain, in_decl,
13861 /*defer_ok*/fndecl_type);
13862 if (specs == error_mark_node)
13863 return error_mark_node;
13864 if (specs)
13865 fntype = build_exception_variant (fntype, specs);
13866 return fntype;
13867 }
13868 case ARRAY_TYPE:
13869 {
13870 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13871 if (domain == error_mark_node)
13872 return error_mark_node;
13873
13874 /* As an optimization, we avoid regenerating the array type if
13875 it will obviously be the same as T. */
13876 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13877 return t;
13878
13879 /* These checks should match the ones in create_array_type_for_decl.
13880
13881 [temp.deduct]
13882
13883 The deduction may fail for any of the following reasons:
13884
13885 -- Attempting to create an array with an element type that
13886 is void, a function type, or a reference type, or [DR337]
13887 an abstract class type. */
13888 if (VOID_TYPE_P (type)
13889 || TREE_CODE (type) == FUNCTION_TYPE
13890 || (TREE_CODE (type) == ARRAY_TYPE
13891 && TYPE_DOMAIN (type) == NULL_TREE)
13892 || TREE_CODE (type) == REFERENCE_TYPE)
13893 {
13894 if (complain & tf_error)
13895 error ("creating array of %qT", type);
13896 return error_mark_node;
13897 }
13898
13899 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13900 return error_mark_node;
13901
13902 r = build_cplus_array_type (type, domain);
13903
13904 if (TYPE_USER_ALIGN (t))
13905 {
13906 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13907 TYPE_USER_ALIGN (r) = 1;
13908 }
13909
13910 return r;
13911 }
13912
13913 case TYPENAME_TYPE:
13914 {
13915 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13916 in_decl, /*entering_scope=*/1);
13917 if (ctx == error_mark_node)
13918 return error_mark_node;
13919
13920 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13921 complain, in_decl);
13922 if (f == error_mark_node)
13923 return error_mark_node;
13924
13925 if (!MAYBE_CLASS_TYPE_P (ctx))
13926 {
13927 if (complain & tf_error)
13928 error ("%qT is not a class, struct, or union type", ctx);
13929 return error_mark_node;
13930 }
13931 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13932 {
13933 /* Normally, make_typename_type does not require that the CTX
13934 have complete type in order to allow things like:
13935
13936 template <class T> struct S { typename S<T>::X Y; };
13937
13938 But, such constructs have already been resolved by this
13939 point, so here CTX really should have complete type, unless
13940 it's a partial instantiation. */
13941 ctx = complete_type (ctx);
13942 if (!COMPLETE_TYPE_P (ctx))
13943 {
13944 if (complain & tf_error)
13945 cxx_incomplete_type_error (NULL_TREE, ctx);
13946 return error_mark_node;
13947 }
13948 }
13949
13950 f = make_typename_type (ctx, f, typename_type,
13951 complain | tf_keep_type_decl);
13952 if (f == error_mark_node)
13953 return f;
13954 if (TREE_CODE (f) == TYPE_DECL)
13955 {
13956 complain |= tf_ignore_bad_quals;
13957 f = TREE_TYPE (f);
13958 }
13959
13960 if (TREE_CODE (f) != TYPENAME_TYPE)
13961 {
13962 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13963 {
13964 if (complain & tf_error)
13965 error ("%qT resolves to %qT, which is not an enumeration type",
13966 t, f);
13967 else
13968 return error_mark_node;
13969 }
13970 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13971 {
13972 if (complain & tf_error)
13973 error ("%qT resolves to %qT, which is is not a class type",
13974 t, f);
13975 else
13976 return error_mark_node;
13977 }
13978 }
13979
13980 return cp_build_qualified_type_real
13981 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13982 }
13983
13984 case UNBOUND_CLASS_TEMPLATE:
13985 {
13986 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13987 in_decl, /*entering_scope=*/1);
13988 tree name = TYPE_IDENTIFIER (t);
13989 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13990
13991 if (ctx == error_mark_node || name == error_mark_node)
13992 return error_mark_node;
13993
13994 if (parm_list)
13995 parm_list = tsubst_template_parms (parm_list, args, complain);
13996 return make_unbound_class_template (ctx, name, parm_list, complain);
13997 }
13998
13999 case TYPEOF_TYPE:
14000 {
14001 tree type;
14002
14003 ++cp_unevaluated_operand;
14004 ++c_inhibit_evaluation_warnings;
14005
14006 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14007 complain, in_decl,
14008 /*integral_constant_expression_p=*/false);
14009
14010 --cp_unevaluated_operand;
14011 --c_inhibit_evaluation_warnings;
14012
14013 type = finish_typeof (type);
14014 return cp_build_qualified_type_real (type,
14015 cp_type_quals (t)
14016 | cp_type_quals (type),
14017 complain);
14018 }
14019
14020 case DECLTYPE_TYPE:
14021 {
14022 tree type;
14023
14024 ++cp_unevaluated_operand;
14025 ++c_inhibit_evaluation_warnings;
14026
14027 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14028 complain|tf_decltype, in_decl,
14029 /*function_p*/false,
14030 /*integral_constant_expression*/false);
14031
14032 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14033 {
14034 if (type == NULL_TREE)
14035 {
14036 if (complain & tf_error)
14037 error ("empty initializer in lambda init-capture");
14038 type = error_mark_node;
14039 }
14040 else if (TREE_CODE (type) == TREE_LIST)
14041 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14042 }
14043
14044 --cp_unevaluated_operand;
14045 --c_inhibit_evaluation_warnings;
14046
14047 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14048 type = lambda_capture_field_type (type,
14049 DECLTYPE_FOR_INIT_CAPTURE (t),
14050 DECLTYPE_FOR_REF_CAPTURE (t));
14051 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14052 type = lambda_proxy_type (type);
14053 else
14054 {
14055 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14056 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14057 && EXPR_P (type))
14058 /* In a template ~id could be either a complement expression
14059 or an unqualified-id naming a destructor; if instantiating
14060 it produces an expression, it's not an id-expression or
14061 member access. */
14062 id = false;
14063 type = finish_decltype_type (type, id, complain);
14064 }
14065 return cp_build_qualified_type_real (type,
14066 cp_type_quals (t)
14067 | cp_type_quals (type),
14068 complain | tf_ignore_bad_quals);
14069 }
14070
14071 case UNDERLYING_TYPE:
14072 {
14073 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14074 complain, in_decl);
14075 return finish_underlying_type (type);
14076 }
14077
14078 case TYPE_ARGUMENT_PACK:
14079 case NONTYPE_ARGUMENT_PACK:
14080 {
14081 tree r;
14082
14083 if (code == NONTYPE_ARGUMENT_PACK)
14084 {
14085 r = make_node (code);
14086 /* Set the already-substituted type. */
14087 TREE_TYPE (r) = type;
14088 }
14089 else
14090 r = cxx_make_type (code);
14091
14092 tree pack_args = ARGUMENT_PACK_ARGS (t);
14093 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14094 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14095
14096 return r;
14097 }
14098
14099 case VOID_CST:
14100 case INTEGER_CST:
14101 case REAL_CST:
14102 case STRING_CST:
14103 case PLUS_EXPR:
14104 case MINUS_EXPR:
14105 case NEGATE_EXPR:
14106 case NOP_EXPR:
14107 case INDIRECT_REF:
14108 case ADDR_EXPR:
14109 case CALL_EXPR:
14110 case ARRAY_REF:
14111 case SCOPE_REF:
14112 /* We should use one of the expression tsubsts for these codes. */
14113 gcc_unreachable ();
14114
14115 default:
14116 sorry ("use of %qs in template", get_tree_code_name (code));
14117 return error_mark_node;
14118 }
14119 }
14120
14121 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14122 type of the expression on the left-hand side of the "." or "->"
14123 operator. */
14124
14125 static tree
14126 tsubst_baselink (tree baselink, tree object_type,
14127 tree args, tsubst_flags_t complain, tree in_decl)
14128 {
14129 tree name;
14130 tree qualifying_scope;
14131 tree fns;
14132 tree optype;
14133 tree template_args = 0;
14134 bool template_id_p = false;
14135 bool qualified = BASELINK_QUALIFIED_P (baselink);
14136
14137 /* A baselink indicates a function from a base class. Both the
14138 BASELINK_ACCESS_BINFO and the base class referenced may
14139 indicate bases of the template class, rather than the
14140 instantiated class. In addition, lookups that were not
14141 ambiguous before may be ambiguous now. Therefore, we perform
14142 the lookup again. */
14143 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14144 qualifying_scope = tsubst (qualifying_scope, args,
14145 complain, in_decl);
14146 fns = BASELINK_FUNCTIONS (baselink);
14147 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14148 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14149 {
14150 template_id_p = true;
14151 template_args = TREE_OPERAND (fns, 1);
14152 fns = TREE_OPERAND (fns, 0);
14153 if (template_args)
14154 template_args = tsubst_template_args (template_args, args,
14155 complain, in_decl);
14156 }
14157 name = DECL_NAME (get_first_fn (fns));
14158 if (IDENTIFIER_TYPENAME_P (name))
14159 name = mangle_conv_op_name_for_type (optype);
14160 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14161 if (!baselink)
14162 {
14163 if (constructor_name_p (name, qualifying_scope))
14164 {
14165 if (complain & tf_error)
14166 error ("cannot call constructor %<%T::%D%> directly",
14167 qualifying_scope, name);
14168 }
14169 return error_mark_node;
14170 }
14171
14172 /* If lookup found a single function, mark it as used at this
14173 point. (If it lookup found multiple functions the one selected
14174 later by overload resolution will be marked as used at that
14175 point.) */
14176 if (BASELINK_P (baselink))
14177 fns = BASELINK_FUNCTIONS (baselink);
14178 if (!template_id_p && !really_overloaded_fn (fns)
14179 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
14180 return error_mark_node;
14181
14182 /* Add back the template arguments, if present. */
14183 if (BASELINK_P (baselink) && template_id_p)
14184 BASELINK_FUNCTIONS (baselink)
14185 = build2 (TEMPLATE_ID_EXPR,
14186 unknown_type_node,
14187 BASELINK_FUNCTIONS (baselink),
14188 template_args);
14189 /* Update the conversion operator type. */
14190 if (BASELINK_P (baselink))
14191 BASELINK_OPTYPE (baselink) = optype;
14192
14193 if (!object_type)
14194 object_type = current_class_type;
14195
14196 if (qualified || name == complete_dtor_identifier)
14197 {
14198 baselink = adjust_result_of_qualified_name_lookup (baselink,
14199 qualifying_scope,
14200 object_type);
14201 if (!qualified)
14202 /* We need to call adjust_result_of_qualified_name_lookup in case the
14203 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14204 so that we still get virtual function binding. */
14205 BASELINK_QUALIFIED_P (baselink) = false;
14206 }
14207 return baselink;
14208 }
14209
14210 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14211 true if the qualified-id will be a postfix-expression in-and-of
14212 itself; false if more of the postfix-expression follows the
14213 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14214 of "&". */
14215
14216 static tree
14217 tsubst_qualified_id (tree qualified_id, tree args,
14218 tsubst_flags_t complain, tree in_decl,
14219 bool done, bool address_p)
14220 {
14221 tree expr;
14222 tree scope;
14223 tree name;
14224 bool is_template;
14225 tree template_args;
14226 location_t loc = UNKNOWN_LOCATION;
14227
14228 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14229
14230 /* Figure out what name to look up. */
14231 name = TREE_OPERAND (qualified_id, 1);
14232 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14233 {
14234 is_template = true;
14235 loc = EXPR_LOCATION (name);
14236 template_args = TREE_OPERAND (name, 1);
14237 if (template_args)
14238 template_args = tsubst_template_args (template_args, args,
14239 complain, in_decl);
14240 if (template_args == error_mark_node)
14241 return error_mark_node;
14242 name = TREE_OPERAND (name, 0);
14243 }
14244 else
14245 {
14246 is_template = false;
14247 template_args = NULL_TREE;
14248 }
14249
14250 /* Substitute into the qualifying scope. When there are no ARGS, we
14251 are just trying to simplify a non-dependent expression. In that
14252 case the qualifying scope may be dependent, and, in any case,
14253 substituting will not help. */
14254 scope = TREE_OPERAND (qualified_id, 0);
14255 if (args)
14256 {
14257 scope = tsubst (scope, args, complain, in_decl);
14258 expr = tsubst_copy (name, args, complain, in_decl);
14259 }
14260 else
14261 expr = name;
14262
14263 if (dependent_scope_p (scope))
14264 {
14265 if (is_template)
14266 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14267 tree r = build_qualified_name (NULL_TREE, scope, expr,
14268 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14269 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14270 return r;
14271 }
14272
14273 if (!BASELINK_P (name) && !DECL_P (expr))
14274 {
14275 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14276 {
14277 /* A BIT_NOT_EXPR is used to represent a destructor. */
14278 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14279 {
14280 error ("qualifying type %qT does not match destructor name ~%qT",
14281 scope, TREE_OPERAND (expr, 0));
14282 expr = error_mark_node;
14283 }
14284 else
14285 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14286 /*is_type_p=*/0, false);
14287 }
14288 else
14289 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14290 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14291 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14292 {
14293 if (complain & tf_error)
14294 {
14295 error ("dependent-name %qE is parsed as a non-type, but "
14296 "instantiation yields a type", qualified_id);
14297 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14298 }
14299 return error_mark_node;
14300 }
14301 }
14302
14303 if (DECL_P (expr))
14304 {
14305 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14306 scope);
14307 /* Remember that there was a reference to this entity. */
14308 if (!mark_used (expr, complain) && !(complain & tf_error))
14309 return error_mark_node;
14310 }
14311
14312 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14313 {
14314 if (complain & tf_error)
14315 qualified_name_lookup_error (scope,
14316 TREE_OPERAND (qualified_id, 1),
14317 expr, input_location);
14318 return error_mark_node;
14319 }
14320
14321 if (is_template)
14322 {
14323 if (variable_template_p (expr))
14324 expr = lookup_and_finish_template_variable (expr, template_args,
14325 complain);
14326 else
14327 expr = lookup_template_function (expr, template_args);
14328 }
14329
14330 if (expr == error_mark_node && complain & tf_error)
14331 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14332 expr, input_location);
14333 else if (TYPE_P (scope))
14334 {
14335 expr = (adjust_result_of_qualified_name_lookup
14336 (expr, scope, current_nonlambda_class_type ()));
14337 expr = (finish_qualified_id_expr
14338 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14339 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14340 /*template_arg_p=*/false, complain));
14341 }
14342
14343 /* Expressions do not generally have reference type. */
14344 if (TREE_CODE (expr) != SCOPE_REF
14345 /* However, if we're about to form a pointer-to-member, we just
14346 want the referenced member referenced. */
14347 && TREE_CODE (expr) != OFFSET_REF)
14348 expr = convert_from_reference (expr);
14349
14350 if (REF_PARENTHESIZED_P (qualified_id))
14351 expr = force_paren_expr (expr);
14352
14353 return expr;
14354 }
14355
14356 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14357 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14358 for tsubst. */
14359
14360 static tree
14361 tsubst_init (tree init, tree decl, tree args,
14362 tsubst_flags_t complain, tree in_decl)
14363 {
14364 if (!init)
14365 return NULL_TREE;
14366
14367 init = tsubst_expr (init, args, complain, in_decl, false);
14368
14369 if (!init && TREE_TYPE (decl) != error_mark_node)
14370 {
14371 /* If we had an initializer but it
14372 instantiated to nothing,
14373 value-initialize the object. This will
14374 only occur when the initializer was a
14375 pack expansion where the parameter packs
14376 used in that expansion were of length
14377 zero. */
14378 init = build_value_init (TREE_TYPE (decl),
14379 complain);
14380 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14381 init = get_target_expr_sfinae (init, complain);
14382 }
14383
14384 return init;
14385 }
14386
14387 /* Like tsubst, but deals with expressions. This function just replaces
14388 template parms; to finish processing the resultant expression, use
14389 tsubst_copy_and_build or tsubst_expr. */
14390
14391 static tree
14392 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14393 {
14394 enum tree_code code;
14395 tree r;
14396
14397 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14398 return t;
14399
14400 code = TREE_CODE (t);
14401
14402 switch (code)
14403 {
14404 case PARM_DECL:
14405 r = retrieve_local_specialization (t);
14406
14407 if (r == NULL_TREE)
14408 {
14409 /* We get here for a use of 'this' in an NSDMI as part of a
14410 constructor call or as part of an aggregate initialization. */
14411 if (DECL_NAME (t) == this_identifier
14412 && ((current_function_decl
14413 && DECL_CONSTRUCTOR_P (current_function_decl))
14414 || (current_class_ref
14415 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14416 return current_class_ptr;
14417
14418 /* This can happen for a parameter name used later in a function
14419 declaration (such as in a late-specified return type). Just
14420 make a dummy decl, since it's only used for its type. */
14421 gcc_assert (cp_unevaluated_operand != 0);
14422 r = tsubst_decl (t, args, complain);
14423 /* Give it the template pattern as its context; its true context
14424 hasn't been instantiated yet and this is good enough for
14425 mangling. */
14426 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14427 }
14428
14429 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14430 r = ARGUMENT_PACK_SELECT_ARG (r);
14431 if (!mark_used (r, complain) && !(complain & tf_error))
14432 return error_mark_node;
14433 return r;
14434
14435 case CONST_DECL:
14436 {
14437 tree enum_type;
14438 tree v;
14439
14440 if (DECL_TEMPLATE_PARM_P (t))
14441 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14442 /* There is no need to substitute into namespace-scope
14443 enumerators. */
14444 if (DECL_NAMESPACE_SCOPE_P (t))
14445 return t;
14446 /* If ARGS is NULL, then T is known to be non-dependent. */
14447 if (args == NULL_TREE)
14448 return scalar_constant_value (t);
14449
14450 /* Unfortunately, we cannot just call lookup_name here.
14451 Consider:
14452
14453 template <int I> int f() {
14454 enum E { a = I };
14455 struct S { void g() { E e = a; } };
14456 };
14457
14458 When we instantiate f<7>::S::g(), say, lookup_name is not
14459 clever enough to find f<7>::a. */
14460 enum_type
14461 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14462 /*entering_scope=*/0);
14463
14464 for (v = TYPE_VALUES (enum_type);
14465 v != NULL_TREE;
14466 v = TREE_CHAIN (v))
14467 if (TREE_PURPOSE (v) == DECL_NAME (t))
14468 return TREE_VALUE (v);
14469
14470 /* We didn't find the name. That should never happen; if
14471 name-lookup found it during preliminary parsing, we
14472 should find it again here during instantiation. */
14473 gcc_unreachable ();
14474 }
14475 return t;
14476
14477 case FIELD_DECL:
14478 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14479 {
14480 /* Check for a local specialization set up by
14481 tsubst_pack_expansion. */
14482 if (tree r = retrieve_local_specialization (t))
14483 {
14484 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14485 r = ARGUMENT_PACK_SELECT_ARG (r);
14486 return r;
14487 }
14488
14489 /* When retrieving a capture pack from a generic lambda, remove the
14490 lambda call op's own template argument list from ARGS. Only the
14491 template arguments active for the closure type should be used to
14492 retrieve the pack specialization. */
14493 if (LAMBDA_FUNCTION_P (current_function_decl)
14494 && (template_class_depth (DECL_CONTEXT (t))
14495 != TMPL_ARGS_DEPTH (args)))
14496 args = strip_innermost_template_args (args, 1);
14497
14498 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14499 tsubst_decl put in the hash table. */
14500 return retrieve_specialization (t, args, 0);
14501 }
14502
14503 if (DECL_CONTEXT (t))
14504 {
14505 tree ctx;
14506
14507 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14508 /*entering_scope=*/1);
14509 if (ctx != DECL_CONTEXT (t))
14510 {
14511 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14512 if (!r)
14513 {
14514 if (complain & tf_error)
14515 error ("using invalid field %qD", t);
14516 return error_mark_node;
14517 }
14518 return r;
14519 }
14520 }
14521
14522 return t;
14523
14524 case VAR_DECL:
14525 case FUNCTION_DECL:
14526 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14527 r = tsubst (t, args, complain, in_decl);
14528 else if (local_variable_p (t)
14529 && uses_template_parms (DECL_CONTEXT (t)))
14530 {
14531 r = retrieve_local_specialization (t);
14532 if (r == NULL_TREE)
14533 {
14534 /* First try name lookup to find the instantiation. */
14535 r = lookup_name (DECL_NAME (t));
14536 if (r)
14537 {
14538 /* Make sure that the one we found is the one we want. */
14539 tree ctx = DECL_CONTEXT (t);
14540 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14541 ctx = tsubst (ctx, args, complain, in_decl);
14542 if (ctx != DECL_CONTEXT (r))
14543 r = NULL_TREE;
14544 }
14545
14546 if (r)
14547 /* OK */;
14548 else
14549 {
14550 /* This can happen for a variable used in a
14551 late-specified return type of a local lambda, or for a
14552 local static or constant. Building a new VAR_DECL
14553 should be OK in all those cases. */
14554 r = tsubst_decl (t, args, complain);
14555 if (decl_maybe_constant_var_p (r))
14556 {
14557 /* We can't call cp_finish_decl, so handle the
14558 initializer by hand. */
14559 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14560 complain, in_decl);
14561 if (!processing_template_decl)
14562 init = maybe_constant_init (init);
14563 if (processing_template_decl
14564 ? potential_constant_expression (init)
14565 : reduced_constant_expression_p (init))
14566 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14567 = TREE_CONSTANT (r) = true;
14568 DECL_INITIAL (r) = init;
14569 }
14570 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14571 || decl_constant_var_p (r)
14572 || errorcount || sorrycount);
14573 if (!processing_template_decl
14574 && !TREE_STATIC (r))
14575 r = process_outer_var_ref (r, complain);
14576 }
14577 /* Remember this for subsequent uses. */
14578 if (local_specializations)
14579 register_local_specialization (r, t);
14580 }
14581 }
14582 else
14583 r = t;
14584 if (!mark_used (r, complain))
14585 return error_mark_node;
14586 return r;
14587
14588 case NAMESPACE_DECL:
14589 return t;
14590
14591 case OVERLOAD:
14592 /* An OVERLOAD will always be a non-dependent overload set; an
14593 overload set from function scope will just be represented with an
14594 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14595 gcc_assert (!uses_template_parms (t));
14596 return t;
14597
14598 case BASELINK:
14599 return tsubst_baselink (t, current_nonlambda_class_type (),
14600 args, complain, in_decl);
14601
14602 case TEMPLATE_DECL:
14603 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14604 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14605 args, complain, in_decl);
14606 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14607 return tsubst (t, args, complain, in_decl);
14608 else if (DECL_CLASS_SCOPE_P (t)
14609 && uses_template_parms (DECL_CONTEXT (t)))
14610 {
14611 /* Template template argument like the following example need
14612 special treatment:
14613
14614 template <template <class> class TT> struct C {};
14615 template <class T> struct D {
14616 template <class U> struct E {};
14617 C<E> c; // #1
14618 };
14619 D<int> d; // #2
14620
14621 We are processing the template argument `E' in #1 for
14622 the template instantiation #2. Originally, `E' is a
14623 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14624 have to substitute this with one having context `D<int>'. */
14625
14626 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14627 return lookup_field (context, DECL_NAME(t), 0, false);
14628 }
14629 else
14630 /* Ordinary template template argument. */
14631 return t;
14632
14633 case CAST_EXPR:
14634 case REINTERPRET_CAST_EXPR:
14635 case CONST_CAST_EXPR:
14636 case STATIC_CAST_EXPR:
14637 case DYNAMIC_CAST_EXPR:
14638 case IMPLICIT_CONV_EXPR:
14639 case CONVERT_EXPR:
14640 case NOP_EXPR:
14641 {
14642 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14643 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14644 return build1 (code, type, op0);
14645 }
14646
14647 case SIZEOF_EXPR:
14648 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14649 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14650 {
14651 tree expanded, op = TREE_OPERAND (t, 0);
14652 int len = 0;
14653
14654 if (SIZEOF_EXPR_TYPE_P (t))
14655 op = TREE_TYPE (op);
14656
14657 ++cp_unevaluated_operand;
14658 ++c_inhibit_evaluation_warnings;
14659 /* We only want to compute the number of arguments. */
14660 if (PACK_EXPANSION_P (op))
14661 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14662 else
14663 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14664 args, complain, in_decl);
14665 --cp_unevaluated_operand;
14666 --c_inhibit_evaluation_warnings;
14667
14668 if (TREE_CODE (expanded) == TREE_VEC)
14669 {
14670 len = TREE_VEC_LENGTH (expanded);
14671 /* Set TREE_USED for the benefit of -Wunused. */
14672 for (int i = 0; i < len; i++)
14673 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14674 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14675 }
14676
14677 if (expanded == error_mark_node)
14678 return error_mark_node;
14679 else if (PACK_EXPANSION_P (expanded)
14680 || (TREE_CODE (expanded) == TREE_VEC
14681 && pack_expansion_args_count (expanded)))
14682
14683 {
14684 if (PACK_EXPANSION_P (expanded))
14685 /* OK. */;
14686 else if (TREE_VEC_LENGTH (expanded) == 1)
14687 expanded = TREE_VEC_ELT (expanded, 0);
14688 else
14689 expanded = make_argument_pack (expanded);
14690
14691 if (TYPE_P (expanded))
14692 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14693 complain & tf_error);
14694 else
14695 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14696 complain & tf_error);
14697 }
14698 else
14699 return build_int_cst (size_type_node, len);
14700 }
14701 if (SIZEOF_EXPR_TYPE_P (t))
14702 {
14703 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14704 args, complain, in_decl);
14705 r = build1 (NOP_EXPR, r, error_mark_node);
14706 r = build1 (SIZEOF_EXPR,
14707 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14708 SIZEOF_EXPR_TYPE_P (r) = 1;
14709 return r;
14710 }
14711 /* Fall through */
14712
14713 case INDIRECT_REF:
14714 case NEGATE_EXPR:
14715 case TRUTH_NOT_EXPR:
14716 case BIT_NOT_EXPR:
14717 case ADDR_EXPR:
14718 case UNARY_PLUS_EXPR: /* Unary + */
14719 case ALIGNOF_EXPR:
14720 case AT_ENCODE_EXPR:
14721 case ARROW_EXPR:
14722 case THROW_EXPR:
14723 case TYPEID_EXPR:
14724 case REALPART_EXPR:
14725 case IMAGPART_EXPR:
14726 case PAREN_EXPR:
14727 {
14728 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14729 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14730 return build1 (code, type, op0);
14731 }
14732
14733 case COMPONENT_REF:
14734 {
14735 tree object;
14736 tree name;
14737
14738 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14739 name = TREE_OPERAND (t, 1);
14740 if (TREE_CODE (name) == BIT_NOT_EXPR)
14741 {
14742 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14743 complain, in_decl);
14744 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14745 }
14746 else if (TREE_CODE (name) == SCOPE_REF
14747 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14748 {
14749 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14750 complain, in_decl);
14751 name = TREE_OPERAND (name, 1);
14752 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14753 complain, in_decl);
14754 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14755 name = build_qualified_name (/*type=*/NULL_TREE,
14756 base, name,
14757 /*template_p=*/false);
14758 }
14759 else if (BASELINK_P (name))
14760 name = tsubst_baselink (name,
14761 non_reference (TREE_TYPE (object)),
14762 args, complain,
14763 in_decl);
14764 else
14765 name = tsubst_copy (name, args, complain, in_decl);
14766 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14767 }
14768
14769 case PLUS_EXPR:
14770 case MINUS_EXPR:
14771 case MULT_EXPR:
14772 case TRUNC_DIV_EXPR:
14773 case CEIL_DIV_EXPR:
14774 case FLOOR_DIV_EXPR:
14775 case ROUND_DIV_EXPR:
14776 case EXACT_DIV_EXPR:
14777 case BIT_AND_EXPR:
14778 case BIT_IOR_EXPR:
14779 case BIT_XOR_EXPR:
14780 case TRUNC_MOD_EXPR:
14781 case FLOOR_MOD_EXPR:
14782 case TRUTH_ANDIF_EXPR:
14783 case TRUTH_ORIF_EXPR:
14784 case TRUTH_AND_EXPR:
14785 case TRUTH_OR_EXPR:
14786 case RSHIFT_EXPR:
14787 case LSHIFT_EXPR:
14788 case RROTATE_EXPR:
14789 case LROTATE_EXPR:
14790 case EQ_EXPR:
14791 case NE_EXPR:
14792 case MAX_EXPR:
14793 case MIN_EXPR:
14794 case LE_EXPR:
14795 case GE_EXPR:
14796 case LT_EXPR:
14797 case GT_EXPR:
14798 case COMPOUND_EXPR:
14799 case DOTSTAR_EXPR:
14800 case MEMBER_REF:
14801 case PREDECREMENT_EXPR:
14802 case PREINCREMENT_EXPR:
14803 case POSTDECREMENT_EXPR:
14804 case POSTINCREMENT_EXPR:
14805 {
14806 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14807 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14808 return build_nt (code, op0, op1);
14809 }
14810
14811 case SCOPE_REF:
14812 {
14813 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14814 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14815 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14816 QUALIFIED_NAME_IS_TEMPLATE (t));
14817 }
14818
14819 case ARRAY_REF:
14820 {
14821 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14822 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14823 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14824 }
14825
14826 case CALL_EXPR:
14827 {
14828 int n = VL_EXP_OPERAND_LENGTH (t);
14829 tree result = build_vl_exp (CALL_EXPR, n);
14830 int i;
14831 for (i = 0; i < n; i++)
14832 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14833 complain, in_decl);
14834 return result;
14835 }
14836
14837 case COND_EXPR:
14838 case MODOP_EXPR:
14839 case PSEUDO_DTOR_EXPR:
14840 case VEC_PERM_EXPR:
14841 {
14842 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14843 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14844 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14845 r = build_nt (code, op0, op1, op2);
14846 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14847 return r;
14848 }
14849
14850 case NEW_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 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14855 r = build_nt (code, op0, op1, op2);
14856 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14857 return r;
14858 }
14859
14860 case DELETE_EXPR:
14861 {
14862 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14863 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14864 r = build_nt (code, op0, op1);
14865 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14866 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14867 return r;
14868 }
14869
14870 case TEMPLATE_ID_EXPR:
14871 {
14872 /* Substituted template arguments */
14873 tree fn = TREE_OPERAND (t, 0);
14874 tree targs = TREE_OPERAND (t, 1);
14875
14876 fn = tsubst_copy (fn, args, complain, in_decl);
14877 if (targs)
14878 targs = tsubst_template_args (targs, args, complain, in_decl);
14879
14880 return lookup_template_function (fn, targs);
14881 }
14882
14883 case TREE_LIST:
14884 {
14885 tree purpose, value, chain;
14886
14887 if (t == void_list_node)
14888 return t;
14889
14890 purpose = TREE_PURPOSE (t);
14891 if (purpose)
14892 purpose = tsubst_copy (purpose, args, complain, in_decl);
14893 value = TREE_VALUE (t);
14894 if (value)
14895 value = tsubst_copy (value, args, complain, in_decl);
14896 chain = TREE_CHAIN (t);
14897 if (chain && chain != void_type_node)
14898 chain = tsubst_copy (chain, args, complain, in_decl);
14899 if (purpose == TREE_PURPOSE (t)
14900 && value == TREE_VALUE (t)
14901 && chain == TREE_CHAIN (t))
14902 return t;
14903 return tree_cons (purpose, value, chain);
14904 }
14905
14906 case RECORD_TYPE:
14907 case UNION_TYPE:
14908 case ENUMERAL_TYPE:
14909 case INTEGER_TYPE:
14910 case TEMPLATE_TYPE_PARM:
14911 case TEMPLATE_TEMPLATE_PARM:
14912 case BOUND_TEMPLATE_TEMPLATE_PARM:
14913 case TEMPLATE_PARM_INDEX:
14914 case POINTER_TYPE:
14915 case REFERENCE_TYPE:
14916 case OFFSET_TYPE:
14917 case FUNCTION_TYPE:
14918 case METHOD_TYPE:
14919 case ARRAY_TYPE:
14920 case TYPENAME_TYPE:
14921 case UNBOUND_CLASS_TEMPLATE:
14922 case TYPEOF_TYPE:
14923 case DECLTYPE_TYPE:
14924 case TYPE_DECL:
14925 return tsubst (t, args, complain, in_decl);
14926
14927 case USING_DECL:
14928 t = DECL_NAME (t);
14929 /* Fall through. */
14930 case IDENTIFIER_NODE:
14931 if (IDENTIFIER_TYPENAME_P (t))
14932 {
14933 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14934 return mangle_conv_op_name_for_type (new_type);
14935 }
14936 else
14937 return t;
14938
14939 case CONSTRUCTOR:
14940 /* This is handled by tsubst_copy_and_build. */
14941 gcc_unreachable ();
14942
14943 case VA_ARG_EXPR:
14944 {
14945 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14946 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14947 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14948 }
14949
14950 case CLEANUP_POINT_EXPR:
14951 /* We shouldn't have built any of these during initial template
14952 generation. Instead, they should be built during instantiation
14953 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14954 gcc_unreachable ();
14955
14956 case OFFSET_REF:
14957 {
14958 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14959 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14960 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14961 r = build2 (code, type, op0, op1);
14962 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14963 if (!mark_used (TREE_OPERAND (r, 1), complain)
14964 && !(complain & tf_error))
14965 return error_mark_node;
14966 return r;
14967 }
14968
14969 case EXPR_PACK_EXPANSION:
14970 error ("invalid use of pack expansion expression");
14971 return error_mark_node;
14972
14973 case NONTYPE_ARGUMENT_PACK:
14974 error ("use %<...%> to expand argument pack");
14975 return error_mark_node;
14976
14977 case VOID_CST:
14978 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14979 return t;
14980
14981 case INTEGER_CST:
14982 case REAL_CST:
14983 case STRING_CST:
14984 case COMPLEX_CST:
14985 {
14986 /* Instantiate any typedefs in the type. */
14987 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14988 r = fold_convert (type, t);
14989 gcc_assert (TREE_CODE (r) == code);
14990 return r;
14991 }
14992
14993 case PTRMEM_CST:
14994 /* These can sometimes show up in a partial instantiation, but never
14995 involve template parms. */
14996 gcc_assert (!uses_template_parms (t));
14997 return t;
14998
14999 case UNARY_LEFT_FOLD_EXPR:
15000 return tsubst_unary_left_fold (t, args, complain, in_decl);
15001 case UNARY_RIGHT_FOLD_EXPR:
15002 return tsubst_unary_right_fold (t, args, complain, in_decl);
15003 case BINARY_LEFT_FOLD_EXPR:
15004 return tsubst_binary_left_fold (t, args, complain, in_decl);
15005 case BINARY_RIGHT_FOLD_EXPR:
15006 return tsubst_binary_right_fold (t, args, complain, in_decl);
15007
15008 default:
15009 /* We shouldn't get here, but keep going if !flag_checking. */
15010 if (flag_checking)
15011 gcc_unreachable ();
15012 return t;
15013 }
15014 }
15015
15016 /* Helper function for tsubst_omp_clauses, used for instantiation of
15017 OMP_CLAUSE_DECL of clauses. */
15018
15019 static tree
15020 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15021 tree in_decl)
15022 {
15023 if (decl == NULL_TREE)
15024 return NULL_TREE;
15025
15026 /* Handle an OpenMP array section represented as a TREE_LIST (or
15027 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15028 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15029 TREE_LIST. We can handle it exactly the same as an array section
15030 (purpose, value, and a chain), even though the nomenclature
15031 (low_bound, length, etc) is different. */
15032 if (TREE_CODE (decl) == TREE_LIST)
15033 {
15034 tree low_bound
15035 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15036 /*integral_constant_expression_p=*/false);
15037 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15038 /*integral_constant_expression_p=*/false);
15039 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15040 in_decl);
15041 if (TREE_PURPOSE (decl) == low_bound
15042 && TREE_VALUE (decl) == length
15043 && TREE_CHAIN (decl) == chain)
15044 return decl;
15045 tree ret = tree_cons (low_bound, length, chain);
15046 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15047 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15048 return ret;
15049 }
15050 tree ret = tsubst_expr (decl, args, complain, in_decl,
15051 /*integral_constant_expression_p=*/false);
15052 /* Undo convert_from_reference tsubst_expr could have called. */
15053 if (decl
15054 && REFERENCE_REF_P (ret)
15055 && !REFERENCE_REF_P (decl))
15056 ret = TREE_OPERAND (ret, 0);
15057 return ret;
15058 }
15059
15060 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15061
15062 static tree
15063 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15064 tree args, tsubst_flags_t complain, tree in_decl)
15065 {
15066 tree new_clauses = NULL_TREE, nc, oc;
15067 tree linear_no_step = NULL_TREE;
15068
15069 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15070 {
15071 nc = copy_node (oc);
15072 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15073 new_clauses = nc;
15074
15075 switch (OMP_CLAUSE_CODE (nc))
15076 {
15077 case OMP_CLAUSE_LASTPRIVATE:
15078 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15079 {
15080 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15081 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15082 in_decl, /*integral_constant_expression_p=*/false);
15083 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15084 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15085 }
15086 /* FALLTHRU */
15087 case OMP_CLAUSE_PRIVATE:
15088 case OMP_CLAUSE_SHARED:
15089 case OMP_CLAUSE_FIRSTPRIVATE:
15090 case OMP_CLAUSE_COPYIN:
15091 case OMP_CLAUSE_COPYPRIVATE:
15092 case OMP_CLAUSE_UNIFORM:
15093 case OMP_CLAUSE_DEPEND:
15094 case OMP_CLAUSE_FROM:
15095 case OMP_CLAUSE_TO:
15096 case OMP_CLAUSE_MAP:
15097 case OMP_CLAUSE_USE_DEVICE_PTR:
15098 case OMP_CLAUSE_IS_DEVICE_PTR:
15099 OMP_CLAUSE_DECL (nc)
15100 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15101 in_decl);
15102 break;
15103 case OMP_CLAUSE_TILE:
15104 case OMP_CLAUSE_IF:
15105 case OMP_CLAUSE_NUM_THREADS:
15106 case OMP_CLAUSE_SCHEDULE:
15107 case OMP_CLAUSE_COLLAPSE:
15108 case OMP_CLAUSE_FINAL:
15109 case OMP_CLAUSE_DEVICE:
15110 case OMP_CLAUSE_DIST_SCHEDULE:
15111 case OMP_CLAUSE_NUM_TEAMS:
15112 case OMP_CLAUSE_THREAD_LIMIT:
15113 case OMP_CLAUSE_SAFELEN:
15114 case OMP_CLAUSE_SIMDLEN:
15115 case OMP_CLAUSE_NUM_TASKS:
15116 case OMP_CLAUSE_GRAINSIZE:
15117 case OMP_CLAUSE_PRIORITY:
15118 case OMP_CLAUSE_ORDERED:
15119 case OMP_CLAUSE_HINT:
15120 case OMP_CLAUSE_NUM_GANGS:
15121 case OMP_CLAUSE_NUM_WORKERS:
15122 case OMP_CLAUSE_VECTOR_LENGTH:
15123 case OMP_CLAUSE_WORKER:
15124 case OMP_CLAUSE_VECTOR:
15125 case OMP_CLAUSE_ASYNC:
15126 case OMP_CLAUSE_WAIT:
15127 OMP_CLAUSE_OPERAND (nc, 0)
15128 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15129 in_decl, /*integral_constant_expression_p=*/false);
15130 break;
15131 case OMP_CLAUSE_REDUCTION:
15132 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15133 {
15134 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15135 if (TREE_CODE (placeholder) == SCOPE_REF)
15136 {
15137 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15138 complain, in_decl);
15139 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15140 = build_qualified_name (NULL_TREE, scope,
15141 TREE_OPERAND (placeholder, 1),
15142 false);
15143 }
15144 else
15145 gcc_assert (identifier_p (placeholder));
15146 }
15147 OMP_CLAUSE_DECL (nc)
15148 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15149 in_decl);
15150 break;
15151 case OMP_CLAUSE_GANG:
15152 case OMP_CLAUSE_ALIGNED:
15153 OMP_CLAUSE_DECL (nc)
15154 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15155 in_decl);
15156 OMP_CLAUSE_OPERAND (nc, 1)
15157 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15158 in_decl, /*integral_constant_expression_p=*/false);
15159 break;
15160 case OMP_CLAUSE_LINEAR:
15161 OMP_CLAUSE_DECL (nc)
15162 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15163 in_decl);
15164 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15165 {
15166 gcc_assert (!linear_no_step);
15167 linear_no_step = nc;
15168 }
15169 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15170 OMP_CLAUSE_LINEAR_STEP (nc)
15171 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15172 complain, in_decl);
15173 else
15174 OMP_CLAUSE_LINEAR_STEP (nc)
15175 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15176 in_decl,
15177 /*integral_constant_expression_p=*/false);
15178 break;
15179 case OMP_CLAUSE_NOWAIT:
15180 case OMP_CLAUSE_DEFAULT:
15181 case OMP_CLAUSE_UNTIED:
15182 case OMP_CLAUSE_MERGEABLE:
15183 case OMP_CLAUSE_INBRANCH:
15184 case OMP_CLAUSE_NOTINBRANCH:
15185 case OMP_CLAUSE_PROC_BIND:
15186 case OMP_CLAUSE_FOR:
15187 case OMP_CLAUSE_PARALLEL:
15188 case OMP_CLAUSE_SECTIONS:
15189 case OMP_CLAUSE_TASKGROUP:
15190 case OMP_CLAUSE_NOGROUP:
15191 case OMP_CLAUSE_THREADS:
15192 case OMP_CLAUSE_SIMD:
15193 case OMP_CLAUSE_DEFAULTMAP:
15194 case OMP_CLAUSE_INDEPENDENT:
15195 case OMP_CLAUSE_AUTO:
15196 case OMP_CLAUSE_SEQ:
15197 break;
15198 default:
15199 gcc_unreachable ();
15200 }
15201 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15202 switch (OMP_CLAUSE_CODE (nc))
15203 {
15204 case OMP_CLAUSE_SHARED:
15205 case OMP_CLAUSE_PRIVATE:
15206 case OMP_CLAUSE_FIRSTPRIVATE:
15207 case OMP_CLAUSE_LASTPRIVATE:
15208 case OMP_CLAUSE_COPYPRIVATE:
15209 case OMP_CLAUSE_LINEAR:
15210 case OMP_CLAUSE_REDUCTION:
15211 case OMP_CLAUSE_USE_DEVICE_PTR:
15212 case OMP_CLAUSE_IS_DEVICE_PTR:
15213 /* tsubst_expr on SCOPE_REF results in returning
15214 finish_non_static_data_member result. Undo that here. */
15215 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15216 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15217 == IDENTIFIER_NODE))
15218 {
15219 tree t = OMP_CLAUSE_DECL (nc);
15220 tree v = t;
15221 while (v)
15222 switch (TREE_CODE (v))
15223 {
15224 case COMPONENT_REF:
15225 case MEM_REF:
15226 case INDIRECT_REF:
15227 CASE_CONVERT:
15228 case POINTER_PLUS_EXPR:
15229 v = TREE_OPERAND (v, 0);
15230 continue;
15231 case PARM_DECL:
15232 if (DECL_CONTEXT (v) == current_function_decl
15233 && DECL_ARTIFICIAL (v)
15234 && DECL_NAME (v) == this_identifier)
15235 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15236 /* FALLTHRU */
15237 default:
15238 v = NULL_TREE;
15239 break;
15240 }
15241 }
15242 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15243 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15244 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15245 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15246 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15247 {
15248 tree decl = OMP_CLAUSE_DECL (nc);
15249 if (VAR_P (decl))
15250 {
15251 if (!DECL_LANG_SPECIFIC (decl))
15252 retrofit_lang_decl (decl);
15253 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15254 }
15255 }
15256 break;
15257 default:
15258 break;
15259 }
15260 }
15261
15262 new_clauses = nreverse (new_clauses);
15263 if (ort != C_ORT_OMP_DECLARE_SIMD)
15264 {
15265 new_clauses = finish_omp_clauses (new_clauses, ort);
15266 if (linear_no_step)
15267 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15268 if (nc == linear_no_step)
15269 {
15270 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15271 break;
15272 }
15273 }
15274 return new_clauses;
15275 }
15276
15277 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15278
15279 static tree
15280 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15281 tree in_decl)
15282 {
15283 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15284
15285 tree purpose, value, chain;
15286
15287 if (t == NULL)
15288 return t;
15289
15290 if (TREE_CODE (t) != TREE_LIST)
15291 return tsubst_copy_and_build (t, args, complain, in_decl,
15292 /*function_p=*/false,
15293 /*integral_constant_expression_p=*/false);
15294
15295 if (t == void_list_node)
15296 return t;
15297
15298 purpose = TREE_PURPOSE (t);
15299 if (purpose)
15300 purpose = RECUR (purpose);
15301 value = TREE_VALUE (t);
15302 if (value)
15303 {
15304 if (TREE_CODE (value) != LABEL_DECL)
15305 value = RECUR (value);
15306 else
15307 {
15308 value = lookup_label (DECL_NAME (value));
15309 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15310 TREE_USED (value) = 1;
15311 }
15312 }
15313 chain = TREE_CHAIN (t);
15314 if (chain && chain != void_type_node)
15315 chain = RECUR (chain);
15316 return tree_cons (purpose, value, chain);
15317 #undef RECUR
15318 }
15319
15320 /* Used to temporarily communicate the list of #pragma omp parallel
15321 clauses to #pragma omp for instantiation if they are combined
15322 together. */
15323
15324 static tree *omp_parallel_combined_clauses;
15325
15326 /* Substitute one OMP_FOR iterator. */
15327
15328 static void
15329 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15330 tree initv, tree condv, tree incrv, tree *clauses,
15331 tree args, tsubst_flags_t complain, tree in_decl,
15332 bool integral_constant_expression_p)
15333 {
15334 #define RECUR(NODE) \
15335 tsubst_expr ((NODE), args, complain, in_decl, \
15336 integral_constant_expression_p)
15337 tree decl, init, cond, incr;
15338
15339 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15340 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15341
15342 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15343 {
15344 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15345 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15346 }
15347
15348 decl = TREE_OPERAND (init, 0);
15349 init = TREE_OPERAND (init, 1);
15350 tree decl_expr = NULL_TREE;
15351 if (init && TREE_CODE (init) == DECL_EXPR)
15352 {
15353 /* We need to jump through some hoops to handle declarations in the
15354 init-statement, since we might need to handle auto deduction,
15355 but we need to keep control of initialization. */
15356 decl_expr = init;
15357 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15358 decl = tsubst_decl (decl, args, complain);
15359 }
15360 else
15361 {
15362 if (TREE_CODE (decl) == SCOPE_REF)
15363 {
15364 decl = RECUR (decl);
15365 if (TREE_CODE (decl) == COMPONENT_REF)
15366 {
15367 tree v = decl;
15368 while (v)
15369 switch (TREE_CODE (v))
15370 {
15371 case COMPONENT_REF:
15372 case MEM_REF:
15373 case INDIRECT_REF:
15374 CASE_CONVERT:
15375 case POINTER_PLUS_EXPR:
15376 v = TREE_OPERAND (v, 0);
15377 continue;
15378 case PARM_DECL:
15379 if (DECL_CONTEXT (v) == current_function_decl
15380 && DECL_ARTIFICIAL (v)
15381 && DECL_NAME (v) == this_identifier)
15382 {
15383 decl = TREE_OPERAND (decl, 1);
15384 decl = omp_privatize_field (decl, false);
15385 }
15386 /* FALLTHRU */
15387 default:
15388 v = NULL_TREE;
15389 break;
15390 }
15391 }
15392 }
15393 else
15394 decl = RECUR (decl);
15395 }
15396 init = RECUR (init);
15397
15398 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15399 if (auto_node && init)
15400 TREE_TYPE (decl)
15401 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15402
15403 gcc_assert (!type_dependent_expression_p (decl));
15404
15405 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15406 {
15407 if (decl_expr)
15408 {
15409 /* Declare the variable, but don't let that initialize it. */
15410 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15411 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15412 RECUR (decl_expr);
15413 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15414 }
15415
15416 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15417 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15418 if (TREE_CODE (incr) == MODIFY_EXPR)
15419 {
15420 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15421 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15422 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15423 NOP_EXPR, rhs, complain);
15424 }
15425 else
15426 incr = RECUR (incr);
15427 TREE_VEC_ELT (declv, i) = decl;
15428 TREE_VEC_ELT (initv, i) = init;
15429 TREE_VEC_ELT (condv, i) = cond;
15430 TREE_VEC_ELT (incrv, i) = incr;
15431 return;
15432 }
15433
15434 if (decl_expr)
15435 {
15436 /* Declare and initialize the variable. */
15437 RECUR (decl_expr);
15438 init = NULL_TREE;
15439 }
15440 else if (init)
15441 {
15442 tree *pc;
15443 int j;
15444 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15445 {
15446 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15447 {
15448 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15449 && OMP_CLAUSE_DECL (*pc) == decl)
15450 break;
15451 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15452 && OMP_CLAUSE_DECL (*pc) == decl)
15453 {
15454 if (j)
15455 break;
15456 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15457 tree c = *pc;
15458 *pc = OMP_CLAUSE_CHAIN (c);
15459 OMP_CLAUSE_CHAIN (c) = *clauses;
15460 *clauses = c;
15461 }
15462 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15463 && OMP_CLAUSE_DECL (*pc) == decl)
15464 {
15465 error ("iteration variable %qD should not be firstprivate",
15466 decl);
15467 *pc = OMP_CLAUSE_CHAIN (*pc);
15468 }
15469 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15470 && OMP_CLAUSE_DECL (*pc) == decl)
15471 {
15472 error ("iteration variable %qD should not be reduction",
15473 decl);
15474 *pc = OMP_CLAUSE_CHAIN (*pc);
15475 }
15476 else
15477 pc = &OMP_CLAUSE_CHAIN (*pc);
15478 }
15479 if (*pc)
15480 break;
15481 }
15482 if (*pc == NULL_TREE)
15483 {
15484 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15485 OMP_CLAUSE_DECL (c) = decl;
15486 c = finish_omp_clauses (c, C_ORT_OMP);
15487 if (c)
15488 {
15489 OMP_CLAUSE_CHAIN (c) = *clauses;
15490 *clauses = c;
15491 }
15492 }
15493 }
15494 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15495 if (COMPARISON_CLASS_P (cond))
15496 {
15497 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15498 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15499 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15500 }
15501 else
15502 cond = RECUR (cond);
15503 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15504 switch (TREE_CODE (incr))
15505 {
15506 case PREINCREMENT_EXPR:
15507 case PREDECREMENT_EXPR:
15508 case POSTINCREMENT_EXPR:
15509 case POSTDECREMENT_EXPR:
15510 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15511 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15512 break;
15513 case MODIFY_EXPR:
15514 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15515 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15516 {
15517 tree rhs = TREE_OPERAND (incr, 1);
15518 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15519 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15520 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15521 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15522 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15523 rhs0, rhs1));
15524 }
15525 else
15526 incr = RECUR (incr);
15527 break;
15528 case MODOP_EXPR:
15529 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15530 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15531 {
15532 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15533 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15534 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15535 TREE_TYPE (decl), lhs,
15536 RECUR (TREE_OPERAND (incr, 2))));
15537 }
15538 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15539 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15540 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15541 {
15542 tree rhs = TREE_OPERAND (incr, 2);
15543 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15544 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15545 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15546 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15547 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15548 rhs0, rhs1));
15549 }
15550 else
15551 incr = RECUR (incr);
15552 break;
15553 default:
15554 incr = RECUR (incr);
15555 break;
15556 }
15557
15558 TREE_VEC_ELT (declv, i) = decl;
15559 TREE_VEC_ELT (initv, i) = init;
15560 TREE_VEC_ELT (condv, i) = cond;
15561 TREE_VEC_ELT (incrv, i) = incr;
15562 #undef RECUR
15563 }
15564
15565 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15566 of OMP_TARGET's body. */
15567
15568 static tree
15569 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15570 {
15571 *walk_subtrees = 0;
15572 switch (TREE_CODE (*tp))
15573 {
15574 case OMP_TEAMS:
15575 return *tp;
15576 case BIND_EXPR:
15577 case STATEMENT_LIST:
15578 *walk_subtrees = 1;
15579 break;
15580 default:
15581 break;
15582 }
15583 return NULL_TREE;
15584 }
15585
15586 /* Helper function for tsubst_expr. For decomposition declaration
15587 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15588 also the corresponding decls representing the identifiers
15589 of the decomposition declaration. Return DECL if successful
15590 or error_mark_node otherwise, set *FIRST to the first decl
15591 in the list chained through DECL_CHAIN and *CNT to the number
15592 of such decls. */
15593
15594 static tree
15595 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15596 tsubst_flags_t complain, tree in_decl, tree *first,
15597 unsigned int *cnt)
15598 {
15599 tree decl2, decl3, prev = decl;
15600 *cnt = 0;
15601 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15602 for (decl2 = DECL_CHAIN (pattern_decl);
15603 decl2
15604 && VAR_P (decl2)
15605 && DECL_DECOMPOSITION_P (decl2)
15606 && DECL_NAME (decl2);
15607 decl2 = DECL_CHAIN (decl2))
15608 {
15609 (*cnt)++;
15610 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15611 tree v = DECL_VALUE_EXPR (decl2);
15612 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15613 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15614 decl3 = tsubst (decl2, args, complain, in_decl);
15615 SET_DECL_VALUE_EXPR (decl2, v);
15616 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15617 if (VAR_P (decl3))
15618 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15619 maybe_push_decl (decl3);
15620 if (error_operand_p (decl3))
15621 decl = error_mark_node;
15622 else if (decl != error_mark_node
15623 && DECL_CHAIN (decl3) != prev)
15624 {
15625 gcc_assert (errorcount);
15626 decl = error_mark_node;
15627 }
15628 else
15629 prev = decl3;
15630 }
15631 *first = prev;
15632 return decl;
15633 }
15634
15635 /* Like tsubst_copy for expressions, etc. but also does semantic
15636 processing. */
15637
15638 tree
15639 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15640 bool integral_constant_expression_p)
15641 {
15642 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15643 #define RECUR(NODE) \
15644 tsubst_expr ((NODE), args, complain, in_decl, \
15645 integral_constant_expression_p)
15646
15647 tree stmt, tmp;
15648 tree r;
15649 location_t loc;
15650
15651 if (t == NULL_TREE || t == error_mark_node)
15652 return t;
15653
15654 loc = input_location;
15655 if (EXPR_HAS_LOCATION (t))
15656 input_location = EXPR_LOCATION (t);
15657 if (STATEMENT_CODE_P (TREE_CODE (t)))
15658 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15659
15660 switch (TREE_CODE (t))
15661 {
15662 case STATEMENT_LIST:
15663 {
15664 tree_stmt_iterator i;
15665 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15666 RECUR (tsi_stmt (i));
15667 break;
15668 }
15669
15670 case CTOR_INITIALIZER:
15671 finish_mem_initializers (tsubst_initializer_list
15672 (TREE_OPERAND (t, 0), args));
15673 break;
15674
15675 case RETURN_EXPR:
15676 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15677 break;
15678
15679 case EXPR_STMT:
15680 tmp = RECUR (EXPR_STMT_EXPR (t));
15681 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15682 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15683 else
15684 finish_expr_stmt (tmp);
15685 break;
15686
15687 case USING_STMT:
15688 do_using_directive (USING_STMT_NAMESPACE (t));
15689 break;
15690
15691 case DECL_EXPR:
15692 {
15693 tree decl, pattern_decl;
15694 tree init;
15695
15696 pattern_decl = decl = DECL_EXPR_DECL (t);
15697 if (TREE_CODE (decl) == LABEL_DECL)
15698 finish_label_decl (DECL_NAME (decl));
15699 else if (TREE_CODE (decl) == USING_DECL)
15700 {
15701 tree scope = USING_DECL_SCOPE (decl);
15702 tree name = DECL_NAME (decl);
15703
15704 scope = tsubst (scope, args, complain, in_decl);
15705 decl = lookup_qualified_name (scope, name,
15706 /*is_type_p=*/false,
15707 /*complain=*/false);
15708 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15709 qualified_name_lookup_error (scope, name, decl, input_location);
15710 else
15711 do_local_using_decl (decl, scope, name);
15712 }
15713 else if (DECL_PACK_P (decl))
15714 {
15715 /* Don't build up decls for a variadic capture proxy, we'll
15716 instantiate the elements directly as needed. */
15717 break;
15718 }
15719 else
15720 {
15721 init = DECL_INITIAL (decl);
15722 decl = tsubst (decl, args, complain, in_decl);
15723 if (decl != error_mark_node)
15724 {
15725 /* By marking the declaration as instantiated, we avoid
15726 trying to instantiate it. Since instantiate_decl can't
15727 handle local variables, and since we've already done
15728 all that needs to be done, that's the right thing to
15729 do. */
15730 if (VAR_P (decl))
15731 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15732 if (VAR_P (decl)
15733 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15734 /* Anonymous aggregates are a special case. */
15735 finish_anon_union (decl);
15736 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15737 {
15738 DECL_CONTEXT (decl) = current_function_decl;
15739 if (DECL_NAME (decl) == this_identifier)
15740 {
15741 tree lam = DECL_CONTEXT (current_function_decl);
15742 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15743 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15744 }
15745 insert_capture_proxy (decl);
15746 }
15747 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15748 /* We already did a pushtag. */;
15749 else if (TREE_CODE (decl) == FUNCTION_DECL
15750 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15751 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15752 {
15753 DECL_CONTEXT (decl) = NULL_TREE;
15754 pushdecl (decl);
15755 DECL_CONTEXT (decl) = current_function_decl;
15756 cp_check_omp_declare_reduction (decl);
15757 }
15758 else
15759 {
15760 int const_init = false;
15761 maybe_push_decl (decl);
15762 if (VAR_P (decl)
15763 && DECL_PRETTY_FUNCTION_P (decl))
15764 {
15765 /* For __PRETTY_FUNCTION__ we have to adjust the
15766 initializer. */
15767 const char *const name
15768 = cxx_printable_name (current_function_decl, 2);
15769 init = cp_fname_init (name, &TREE_TYPE (decl));
15770 }
15771 else
15772 init = tsubst_init (init, decl, args, complain, in_decl);
15773
15774 if (VAR_P (decl))
15775 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15776 (pattern_decl));
15777 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15778 if (VAR_P (decl)
15779 && DECL_DECOMPOSITION_P (decl)
15780 && TREE_TYPE (pattern_decl) != error_mark_node)
15781 {
15782 unsigned int cnt;
15783 tree first;
15784 decl = tsubst_decomp_names (decl, pattern_decl, args,
15785 complain, in_decl, &first,
15786 &cnt);
15787 if (decl != error_mark_node)
15788 cp_finish_decomp (decl, first, cnt);
15789 }
15790 }
15791 }
15792 }
15793
15794 break;
15795 }
15796
15797 case FOR_STMT:
15798 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15799 RECUR (FOR_INIT_STMT (t));
15800 finish_init_stmt (stmt);
15801 tmp = RECUR (FOR_COND (t));
15802 finish_for_cond (tmp, stmt, false);
15803 tmp = RECUR (FOR_EXPR (t));
15804 finish_for_expr (tmp, stmt);
15805 RECUR (FOR_BODY (t));
15806 finish_for_stmt (stmt);
15807 break;
15808
15809 case RANGE_FOR_STMT:
15810 {
15811 tree decl, expr;
15812 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15813 decl = RANGE_FOR_DECL (t);
15814 decl = tsubst (decl, args, complain, in_decl);
15815 maybe_push_decl (decl);
15816 expr = RECUR (RANGE_FOR_EXPR (t));
15817 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15818 {
15819 unsigned int cnt;
15820 tree first;
15821 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15822 complain, in_decl, &first, &cnt);
15823 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15824 RANGE_FOR_IVDEP (t));
15825 }
15826 else
15827 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15828 RANGE_FOR_IVDEP (t));
15829 RECUR (RANGE_FOR_BODY (t));
15830 finish_for_stmt (stmt);
15831 }
15832 break;
15833
15834 case WHILE_STMT:
15835 stmt = begin_while_stmt ();
15836 tmp = RECUR (WHILE_COND (t));
15837 finish_while_stmt_cond (tmp, stmt, false);
15838 RECUR (WHILE_BODY (t));
15839 finish_while_stmt (stmt);
15840 break;
15841
15842 case DO_STMT:
15843 stmt = begin_do_stmt ();
15844 RECUR (DO_BODY (t));
15845 finish_do_body (stmt);
15846 tmp = RECUR (DO_COND (t));
15847 finish_do_stmt (tmp, stmt, false);
15848 break;
15849
15850 case IF_STMT:
15851 stmt = begin_if_stmt ();
15852 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15853 tmp = RECUR (IF_COND (t));
15854 tmp = finish_if_stmt_cond (tmp, stmt);
15855 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15856 /* Don't instantiate the THEN_CLAUSE. */;
15857 else
15858 {
15859 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15860 if (inhibit)
15861 ++c_inhibit_evaluation_warnings;
15862 RECUR (THEN_CLAUSE (t));
15863 if (inhibit)
15864 --c_inhibit_evaluation_warnings;
15865 }
15866 finish_then_clause (stmt);
15867
15868 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15869 /* Don't instantiate the ELSE_CLAUSE. */;
15870 else if (ELSE_CLAUSE (t))
15871 {
15872 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15873 begin_else_clause (stmt);
15874 if (inhibit)
15875 ++c_inhibit_evaluation_warnings;
15876 RECUR (ELSE_CLAUSE (t));
15877 if (inhibit)
15878 --c_inhibit_evaluation_warnings;
15879 finish_else_clause (stmt);
15880 }
15881
15882 finish_if_stmt (stmt);
15883 break;
15884
15885 case BIND_EXPR:
15886 if (BIND_EXPR_BODY_BLOCK (t))
15887 stmt = begin_function_body ();
15888 else
15889 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15890 ? BCS_TRY_BLOCK : 0);
15891
15892 RECUR (BIND_EXPR_BODY (t));
15893
15894 if (BIND_EXPR_BODY_BLOCK (t))
15895 finish_function_body (stmt);
15896 else
15897 finish_compound_stmt (stmt);
15898 break;
15899
15900 case BREAK_STMT:
15901 finish_break_stmt ();
15902 break;
15903
15904 case CONTINUE_STMT:
15905 finish_continue_stmt ();
15906 break;
15907
15908 case SWITCH_STMT:
15909 stmt = begin_switch_stmt ();
15910 tmp = RECUR (SWITCH_STMT_COND (t));
15911 finish_switch_cond (tmp, stmt);
15912 RECUR (SWITCH_STMT_BODY (t));
15913 finish_switch_stmt (stmt);
15914 break;
15915
15916 case CASE_LABEL_EXPR:
15917 {
15918 tree low = RECUR (CASE_LOW (t));
15919 tree high = RECUR (CASE_HIGH (t));
15920 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15921 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15922 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15923 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15924 }
15925 break;
15926
15927 case LABEL_EXPR:
15928 {
15929 tree decl = LABEL_EXPR_LABEL (t);
15930 tree label;
15931
15932 label = finish_label_stmt (DECL_NAME (decl));
15933 if (TREE_CODE (label) == LABEL_DECL)
15934 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15935 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15936 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15937 }
15938 break;
15939
15940 case GOTO_EXPR:
15941 tmp = GOTO_DESTINATION (t);
15942 if (TREE_CODE (tmp) != LABEL_DECL)
15943 /* Computed goto's must be tsubst'd into. On the other hand,
15944 non-computed gotos must not be; the identifier in question
15945 will have no binding. */
15946 tmp = RECUR (tmp);
15947 else
15948 tmp = DECL_NAME (tmp);
15949 finish_goto_stmt (tmp);
15950 break;
15951
15952 case ASM_EXPR:
15953 {
15954 tree string = RECUR (ASM_STRING (t));
15955 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15956 complain, in_decl);
15957 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15958 complain, in_decl);
15959 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15960 complain, in_decl);
15961 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15962 complain, in_decl);
15963 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15964 clobbers, labels);
15965 tree asm_expr = tmp;
15966 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15967 asm_expr = TREE_OPERAND (asm_expr, 0);
15968 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15969 }
15970 break;
15971
15972 case TRY_BLOCK:
15973 if (CLEANUP_P (t))
15974 {
15975 stmt = begin_try_block ();
15976 RECUR (TRY_STMTS (t));
15977 finish_cleanup_try_block (stmt);
15978 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15979 }
15980 else
15981 {
15982 tree compound_stmt = NULL_TREE;
15983
15984 if (FN_TRY_BLOCK_P (t))
15985 stmt = begin_function_try_block (&compound_stmt);
15986 else
15987 stmt = begin_try_block ();
15988
15989 RECUR (TRY_STMTS (t));
15990
15991 if (FN_TRY_BLOCK_P (t))
15992 finish_function_try_block (stmt);
15993 else
15994 finish_try_block (stmt);
15995
15996 RECUR (TRY_HANDLERS (t));
15997 if (FN_TRY_BLOCK_P (t))
15998 finish_function_handler_sequence (stmt, compound_stmt);
15999 else
16000 finish_handler_sequence (stmt);
16001 }
16002 break;
16003
16004 case HANDLER:
16005 {
16006 tree decl = HANDLER_PARMS (t);
16007
16008 if (decl)
16009 {
16010 decl = tsubst (decl, args, complain, in_decl);
16011 /* Prevent instantiate_decl from trying to instantiate
16012 this variable. We've already done all that needs to be
16013 done. */
16014 if (decl != error_mark_node)
16015 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16016 }
16017 stmt = begin_handler ();
16018 finish_handler_parms (decl, stmt);
16019 RECUR (HANDLER_BODY (t));
16020 finish_handler (stmt);
16021 }
16022 break;
16023
16024 case TAG_DEFN:
16025 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16026 if (CLASS_TYPE_P (tmp))
16027 {
16028 /* Local classes are not independent templates; they are
16029 instantiated along with their containing function. And this
16030 way we don't have to deal with pushing out of one local class
16031 to instantiate a member of another local class. */
16032 tree fn;
16033 /* Closures are handled by the LAMBDA_EXPR. */
16034 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16035 complete_type (tmp);
16036 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16037 if (!DECL_ARTIFICIAL (fn))
16038 instantiate_decl (fn, /*defer_ok=*/false,
16039 /*expl_inst_class=*/false);
16040 }
16041 break;
16042
16043 case STATIC_ASSERT:
16044 {
16045 tree condition;
16046
16047 ++c_inhibit_evaluation_warnings;
16048 condition =
16049 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16050 args,
16051 complain, in_decl,
16052 /*integral_constant_expression_p=*/true);
16053 --c_inhibit_evaluation_warnings;
16054
16055 finish_static_assert (condition,
16056 STATIC_ASSERT_MESSAGE (t),
16057 STATIC_ASSERT_SOURCE_LOCATION (t),
16058 /*member_p=*/false);
16059 }
16060 break;
16061
16062 case OACC_KERNELS:
16063 case OACC_PARALLEL:
16064 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16065 in_decl);
16066 stmt = begin_omp_parallel ();
16067 RECUR (OMP_BODY (t));
16068 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16069 break;
16070
16071 case OMP_PARALLEL:
16072 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16073 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16074 complain, in_decl);
16075 if (OMP_PARALLEL_COMBINED (t))
16076 omp_parallel_combined_clauses = &tmp;
16077 stmt = begin_omp_parallel ();
16078 RECUR (OMP_PARALLEL_BODY (t));
16079 gcc_assert (omp_parallel_combined_clauses == NULL);
16080 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16081 = OMP_PARALLEL_COMBINED (t);
16082 pop_omp_privatization_clauses (r);
16083 break;
16084
16085 case OMP_TASK:
16086 r = push_omp_privatization_clauses (false);
16087 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16088 complain, in_decl);
16089 stmt = begin_omp_task ();
16090 RECUR (OMP_TASK_BODY (t));
16091 finish_omp_task (tmp, stmt);
16092 pop_omp_privatization_clauses (r);
16093 break;
16094
16095 case OMP_FOR:
16096 case OMP_SIMD:
16097 case CILK_SIMD:
16098 case CILK_FOR:
16099 case OMP_DISTRIBUTE:
16100 case OMP_TASKLOOP:
16101 case OACC_LOOP:
16102 {
16103 tree clauses, body, pre_body;
16104 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16105 tree orig_declv = NULL_TREE;
16106 tree incrv = NULL_TREE;
16107 enum c_omp_region_type ort = C_ORT_OMP;
16108 int i;
16109
16110 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16111 ort = C_ORT_CILK;
16112 else if (TREE_CODE (t) == OACC_LOOP)
16113 ort = C_ORT_ACC;
16114
16115 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16116 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16117 in_decl);
16118 if (OMP_FOR_INIT (t) != NULL_TREE)
16119 {
16120 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16121 if (OMP_FOR_ORIG_DECLS (t))
16122 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16123 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16124 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16125 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16126 }
16127
16128 stmt = begin_omp_structured_block ();
16129
16130 pre_body = push_stmt_list ();
16131 RECUR (OMP_FOR_PRE_BODY (t));
16132 pre_body = pop_stmt_list (pre_body);
16133
16134 if (OMP_FOR_INIT (t) != NULL_TREE)
16135 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16136 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16137 incrv, &clauses, args, complain, in_decl,
16138 integral_constant_expression_p);
16139 omp_parallel_combined_clauses = NULL;
16140
16141 body = push_stmt_list ();
16142 RECUR (OMP_FOR_BODY (t));
16143 body = pop_stmt_list (body);
16144
16145 if (OMP_FOR_INIT (t) != NULL_TREE)
16146 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16147 orig_declv, initv, condv, incrv, body, pre_body,
16148 NULL, clauses);
16149 else
16150 {
16151 t = make_node (TREE_CODE (t));
16152 TREE_TYPE (t) = void_type_node;
16153 OMP_FOR_BODY (t) = body;
16154 OMP_FOR_PRE_BODY (t) = pre_body;
16155 OMP_FOR_CLAUSES (t) = clauses;
16156 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16157 add_stmt (t);
16158 }
16159
16160 add_stmt (finish_omp_structured_block (stmt));
16161 pop_omp_privatization_clauses (r);
16162 }
16163 break;
16164
16165 case OMP_SECTIONS:
16166 omp_parallel_combined_clauses = NULL;
16167 /* FALLTHRU */
16168 case OMP_SINGLE:
16169 case OMP_TEAMS:
16170 case OMP_CRITICAL:
16171 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16172 && OMP_TEAMS_COMBINED (t));
16173 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16174 in_decl);
16175 stmt = push_stmt_list ();
16176 RECUR (OMP_BODY (t));
16177 stmt = pop_stmt_list (stmt);
16178
16179 t = copy_node (t);
16180 OMP_BODY (t) = stmt;
16181 OMP_CLAUSES (t) = tmp;
16182 add_stmt (t);
16183 pop_omp_privatization_clauses (r);
16184 break;
16185
16186 case OACC_DATA:
16187 case OMP_TARGET_DATA:
16188 case OMP_TARGET:
16189 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16190 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16191 in_decl);
16192 keep_next_level (true);
16193 stmt = begin_omp_structured_block ();
16194
16195 RECUR (OMP_BODY (t));
16196 stmt = finish_omp_structured_block (stmt);
16197
16198 t = copy_node (t);
16199 OMP_BODY (t) = stmt;
16200 OMP_CLAUSES (t) = tmp;
16201 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16202 {
16203 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16204 if (teams)
16205 {
16206 /* For combined target teams, ensure the num_teams and
16207 thread_limit clause expressions are evaluated on the host,
16208 before entering the target construct. */
16209 tree c;
16210 for (c = OMP_TEAMS_CLAUSES (teams);
16211 c; c = OMP_CLAUSE_CHAIN (c))
16212 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16213 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16214 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16215 {
16216 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16217 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16218 if (expr == error_mark_node)
16219 continue;
16220 tmp = TARGET_EXPR_SLOT (expr);
16221 add_stmt (expr);
16222 OMP_CLAUSE_OPERAND (c, 0) = expr;
16223 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16224 OMP_CLAUSE_FIRSTPRIVATE);
16225 OMP_CLAUSE_DECL (tc) = tmp;
16226 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16227 OMP_TARGET_CLAUSES (t) = tc;
16228 }
16229 }
16230 }
16231 add_stmt (t);
16232 break;
16233
16234 case OACC_DECLARE:
16235 t = copy_node (t);
16236 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16237 complain, in_decl);
16238 OACC_DECLARE_CLAUSES (t) = tmp;
16239 add_stmt (t);
16240 break;
16241
16242 case OMP_TARGET_UPDATE:
16243 case OMP_TARGET_ENTER_DATA:
16244 case OMP_TARGET_EXIT_DATA:
16245 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16246 complain, in_decl);
16247 t = copy_node (t);
16248 OMP_STANDALONE_CLAUSES (t) = tmp;
16249 add_stmt (t);
16250 break;
16251
16252 case OACC_ENTER_DATA:
16253 case OACC_EXIT_DATA:
16254 case OACC_UPDATE:
16255 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16256 complain, in_decl);
16257 t = copy_node (t);
16258 OMP_STANDALONE_CLAUSES (t) = tmp;
16259 add_stmt (t);
16260 break;
16261
16262 case OMP_ORDERED:
16263 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16264 complain, in_decl);
16265 stmt = push_stmt_list ();
16266 RECUR (OMP_BODY (t));
16267 stmt = pop_stmt_list (stmt);
16268
16269 t = copy_node (t);
16270 OMP_BODY (t) = stmt;
16271 OMP_ORDERED_CLAUSES (t) = tmp;
16272 add_stmt (t);
16273 break;
16274
16275 case OMP_SECTION:
16276 case OMP_MASTER:
16277 case OMP_TASKGROUP:
16278 stmt = push_stmt_list ();
16279 RECUR (OMP_BODY (t));
16280 stmt = pop_stmt_list (stmt);
16281
16282 t = copy_node (t);
16283 OMP_BODY (t) = stmt;
16284 add_stmt (t);
16285 break;
16286
16287 case OMP_ATOMIC:
16288 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16289 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16290 {
16291 tree op1 = TREE_OPERAND (t, 1);
16292 tree rhs1 = NULL_TREE;
16293 tree lhs, rhs;
16294 if (TREE_CODE (op1) == COMPOUND_EXPR)
16295 {
16296 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16297 op1 = TREE_OPERAND (op1, 1);
16298 }
16299 lhs = RECUR (TREE_OPERAND (op1, 0));
16300 rhs = RECUR (TREE_OPERAND (op1, 1));
16301 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16302 NULL_TREE, NULL_TREE, rhs1,
16303 OMP_ATOMIC_SEQ_CST (t));
16304 }
16305 else
16306 {
16307 tree op1 = TREE_OPERAND (t, 1);
16308 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16309 tree rhs1 = NULL_TREE;
16310 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16311 enum tree_code opcode = NOP_EXPR;
16312 if (code == OMP_ATOMIC_READ)
16313 {
16314 v = RECUR (TREE_OPERAND (op1, 0));
16315 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16316 }
16317 else if (code == OMP_ATOMIC_CAPTURE_OLD
16318 || code == OMP_ATOMIC_CAPTURE_NEW)
16319 {
16320 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16321 v = RECUR (TREE_OPERAND (op1, 0));
16322 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16323 if (TREE_CODE (op11) == COMPOUND_EXPR)
16324 {
16325 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16326 op11 = TREE_OPERAND (op11, 1);
16327 }
16328 lhs = RECUR (TREE_OPERAND (op11, 0));
16329 rhs = RECUR (TREE_OPERAND (op11, 1));
16330 opcode = TREE_CODE (op11);
16331 if (opcode == MODIFY_EXPR)
16332 opcode = NOP_EXPR;
16333 }
16334 else
16335 {
16336 code = OMP_ATOMIC;
16337 lhs = RECUR (TREE_OPERAND (op1, 0));
16338 rhs = RECUR (TREE_OPERAND (op1, 1));
16339 }
16340 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16341 OMP_ATOMIC_SEQ_CST (t));
16342 }
16343 break;
16344
16345 case TRANSACTION_EXPR:
16346 {
16347 int flags = 0;
16348 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16349 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16350
16351 if (TRANSACTION_EXPR_IS_STMT (t))
16352 {
16353 tree body = TRANSACTION_EXPR_BODY (t);
16354 tree noex = NULL_TREE;
16355 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16356 {
16357 noex = MUST_NOT_THROW_COND (body);
16358 if (noex == NULL_TREE)
16359 noex = boolean_true_node;
16360 body = TREE_OPERAND (body, 0);
16361 }
16362 stmt = begin_transaction_stmt (input_location, NULL, flags);
16363 RECUR (body);
16364 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16365 }
16366 else
16367 {
16368 stmt = build_transaction_expr (EXPR_LOCATION (t),
16369 RECUR (TRANSACTION_EXPR_BODY (t)),
16370 flags, NULL_TREE);
16371 RETURN (stmt);
16372 }
16373 }
16374 break;
16375
16376 case MUST_NOT_THROW_EXPR:
16377 {
16378 tree op0 = RECUR (TREE_OPERAND (t, 0));
16379 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16380 RETURN (build_must_not_throw_expr (op0, cond));
16381 }
16382
16383 case EXPR_PACK_EXPANSION:
16384 error ("invalid use of pack expansion expression");
16385 RETURN (error_mark_node);
16386
16387 case NONTYPE_ARGUMENT_PACK:
16388 error ("use %<...%> to expand argument pack");
16389 RETURN (error_mark_node);
16390
16391 case CILK_SPAWN_STMT:
16392 cfun->calls_cilk_spawn = 1;
16393 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16394
16395 case CILK_SYNC_STMT:
16396 RETURN (build_cilk_sync ());
16397
16398 case COMPOUND_EXPR:
16399 tmp = RECUR (TREE_OPERAND (t, 0));
16400 if (tmp == NULL_TREE)
16401 /* If the first operand was a statement, we're done with it. */
16402 RETURN (RECUR (TREE_OPERAND (t, 1)));
16403 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16404 RECUR (TREE_OPERAND (t, 1)),
16405 complain));
16406
16407 case ANNOTATE_EXPR:
16408 tmp = RECUR (TREE_OPERAND (t, 0));
16409 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16410 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16411
16412 default:
16413 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16414
16415 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16416 /*function_p=*/false,
16417 integral_constant_expression_p));
16418 }
16419
16420 RETURN (NULL_TREE);
16421 out:
16422 input_location = loc;
16423 return r;
16424 #undef RECUR
16425 #undef RETURN
16426 }
16427
16428 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16429 function. For description of the body see comment above
16430 cp_parser_omp_declare_reduction_exprs. */
16431
16432 static void
16433 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16434 {
16435 if (t == NULL_TREE || t == error_mark_node)
16436 return;
16437
16438 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16439
16440 tree_stmt_iterator tsi;
16441 int i;
16442 tree stmts[7];
16443 memset (stmts, 0, sizeof stmts);
16444 for (i = 0, tsi = tsi_start (t);
16445 i < 7 && !tsi_end_p (tsi);
16446 i++, tsi_next (&tsi))
16447 stmts[i] = tsi_stmt (tsi);
16448 gcc_assert (tsi_end_p (tsi));
16449
16450 if (i >= 3)
16451 {
16452 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16453 && TREE_CODE (stmts[1]) == DECL_EXPR);
16454 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16455 args, complain, in_decl);
16456 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16457 args, complain, in_decl);
16458 DECL_CONTEXT (omp_out) = current_function_decl;
16459 DECL_CONTEXT (omp_in) = current_function_decl;
16460 keep_next_level (true);
16461 tree block = begin_omp_structured_block ();
16462 tsubst_expr (stmts[2], args, complain, in_decl, false);
16463 block = finish_omp_structured_block (block);
16464 block = maybe_cleanup_point_expr_void (block);
16465 add_decl_expr (omp_out);
16466 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16467 TREE_NO_WARNING (omp_out) = 1;
16468 add_decl_expr (omp_in);
16469 finish_expr_stmt (block);
16470 }
16471 if (i >= 6)
16472 {
16473 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16474 && TREE_CODE (stmts[4]) == DECL_EXPR);
16475 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16476 args, complain, in_decl);
16477 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16478 args, complain, in_decl);
16479 DECL_CONTEXT (omp_priv) = current_function_decl;
16480 DECL_CONTEXT (omp_orig) = current_function_decl;
16481 keep_next_level (true);
16482 tree block = begin_omp_structured_block ();
16483 tsubst_expr (stmts[5], args, complain, in_decl, false);
16484 block = finish_omp_structured_block (block);
16485 block = maybe_cleanup_point_expr_void (block);
16486 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16487 add_decl_expr (omp_priv);
16488 add_decl_expr (omp_orig);
16489 finish_expr_stmt (block);
16490 if (i == 7)
16491 add_decl_expr (omp_orig);
16492 }
16493 }
16494
16495 /* T is a postfix-expression that is not being used in a function
16496 call. Return the substituted version of T. */
16497
16498 static tree
16499 tsubst_non_call_postfix_expression (tree t, tree args,
16500 tsubst_flags_t complain,
16501 tree in_decl)
16502 {
16503 if (TREE_CODE (t) == SCOPE_REF)
16504 t = tsubst_qualified_id (t, args, complain, in_decl,
16505 /*done=*/false, /*address_p=*/false);
16506 else
16507 t = tsubst_copy_and_build (t, args, complain, in_decl,
16508 /*function_p=*/false,
16509 /*integral_constant_expression_p=*/false);
16510
16511 return t;
16512 }
16513
16514 /* Like tsubst but deals with expressions and performs semantic
16515 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16516
16517 tree
16518 tsubst_copy_and_build (tree t,
16519 tree args,
16520 tsubst_flags_t complain,
16521 tree in_decl,
16522 bool function_p,
16523 bool integral_constant_expression_p)
16524 {
16525 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16526 #define RECUR(NODE) \
16527 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16528 /*function_p=*/false, \
16529 integral_constant_expression_p)
16530
16531 tree retval, op1;
16532 location_t loc;
16533
16534 if (t == NULL_TREE || t == error_mark_node)
16535 return t;
16536
16537 loc = input_location;
16538 if (EXPR_HAS_LOCATION (t))
16539 input_location = EXPR_LOCATION (t);
16540
16541 /* N3276 decltype magic only applies to calls at the top level or on the
16542 right side of a comma. */
16543 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16544 complain &= ~tf_decltype;
16545
16546 switch (TREE_CODE (t))
16547 {
16548 case USING_DECL:
16549 t = DECL_NAME (t);
16550 /* Fall through. */
16551 case IDENTIFIER_NODE:
16552 {
16553 tree decl;
16554 cp_id_kind idk;
16555 bool non_integral_constant_expression_p;
16556 const char *error_msg;
16557
16558 if (IDENTIFIER_TYPENAME_P (t))
16559 {
16560 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16561 t = mangle_conv_op_name_for_type (new_type);
16562 }
16563
16564 /* Look up the name. */
16565 decl = lookup_name (t);
16566
16567 /* By convention, expressions use ERROR_MARK_NODE to indicate
16568 failure, not NULL_TREE. */
16569 if (decl == NULL_TREE)
16570 decl = error_mark_node;
16571
16572 decl = finish_id_expression (t, decl, NULL_TREE,
16573 &idk,
16574 integral_constant_expression_p,
16575 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16576 &non_integral_constant_expression_p,
16577 /*template_p=*/false,
16578 /*done=*/true,
16579 /*address_p=*/false,
16580 /*template_arg_p=*/false,
16581 &error_msg,
16582 input_location);
16583 if (error_msg)
16584 error (error_msg);
16585 if (!function_p && identifier_p (decl))
16586 {
16587 if (complain & tf_error)
16588 unqualified_name_lookup_error (decl);
16589 decl = error_mark_node;
16590 }
16591 RETURN (decl);
16592 }
16593
16594 case TEMPLATE_ID_EXPR:
16595 {
16596 tree object;
16597 tree templ = RECUR (TREE_OPERAND (t, 0));
16598 tree targs = TREE_OPERAND (t, 1);
16599
16600 if (targs)
16601 targs = tsubst_template_args (targs, args, complain, in_decl);
16602 if (targs == error_mark_node)
16603 return error_mark_node;
16604
16605 if (variable_template_p (templ))
16606 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16607
16608 if (TREE_CODE (templ) == COMPONENT_REF)
16609 {
16610 object = TREE_OPERAND (templ, 0);
16611 templ = TREE_OPERAND (templ, 1);
16612 }
16613 else
16614 object = NULL_TREE;
16615 templ = lookup_template_function (templ, targs);
16616
16617 if (object)
16618 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16619 object, templ, NULL_TREE));
16620 else
16621 RETURN (baselink_for_fns (templ));
16622 }
16623
16624 case INDIRECT_REF:
16625 {
16626 tree r = RECUR (TREE_OPERAND (t, 0));
16627
16628 if (REFERENCE_REF_P (t))
16629 {
16630 /* A type conversion to reference type will be enclosed in
16631 such an indirect ref, but the substitution of the cast
16632 will have also added such an indirect ref. */
16633 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16634 r = convert_from_reference (r);
16635 }
16636 else
16637 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16638 complain|decltype_flag);
16639
16640 if (TREE_CODE (r) == INDIRECT_REF)
16641 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16642
16643 RETURN (r);
16644 }
16645
16646 case NOP_EXPR:
16647 {
16648 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16649 tree op0 = RECUR (TREE_OPERAND (t, 0));
16650 RETURN (build_nop (type, op0));
16651 }
16652
16653 case IMPLICIT_CONV_EXPR:
16654 {
16655 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16656 tree expr = RECUR (TREE_OPERAND (t, 0));
16657 int flags = LOOKUP_IMPLICIT;
16658 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16659 flags = LOOKUP_NORMAL;
16660 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16661 flags));
16662 }
16663
16664 case CONVERT_EXPR:
16665 {
16666 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16667 tree op0 = RECUR (TREE_OPERAND (t, 0));
16668 RETURN (build1 (CONVERT_EXPR, type, op0));
16669 }
16670
16671 case CAST_EXPR:
16672 case REINTERPRET_CAST_EXPR:
16673 case CONST_CAST_EXPR:
16674 case DYNAMIC_CAST_EXPR:
16675 case STATIC_CAST_EXPR:
16676 {
16677 tree type;
16678 tree op, r = NULL_TREE;
16679
16680 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16681 if (integral_constant_expression_p
16682 && !cast_valid_in_integral_constant_expression_p (type))
16683 {
16684 if (complain & tf_error)
16685 error ("a cast to a type other than an integral or "
16686 "enumeration type cannot appear in a constant-expression");
16687 RETURN (error_mark_node);
16688 }
16689
16690 op = RECUR (TREE_OPERAND (t, 0));
16691
16692 warning_sentinel s(warn_useless_cast);
16693 switch (TREE_CODE (t))
16694 {
16695 case CAST_EXPR:
16696 r = build_functional_cast (type, op, complain);
16697 break;
16698 case REINTERPRET_CAST_EXPR:
16699 r = build_reinterpret_cast (type, op, complain);
16700 break;
16701 case CONST_CAST_EXPR:
16702 r = build_const_cast (type, op, complain);
16703 break;
16704 case DYNAMIC_CAST_EXPR:
16705 r = build_dynamic_cast (type, op, complain);
16706 break;
16707 case STATIC_CAST_EXPR:
16708 r = build_static_cast (type, op, complain);
16709 break;
16710 default:
16711 gcc_unreachable ();
16712 }
16713
16714 RETURN (r);
16715 }
16716
16717 case POSTDECREMENT_EXPR:
16718 case POSTINCREMENT_EXPR:
16719 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16720 args, complain, in_decl);
16721 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16722 complain|decltype_flag));
16723
16724 case PREDECREMENT_EXPR:
16725 case PREINCREMENT_EXPR:
16726 case NEGATE_EXPR:
16727 case BIT_NOT_EXPR:
16728 case ABS_EXPR:
16729 case TRUTH_NOT_EXPR:
16730 case UNARY_PLUS_EXPR: /* Unary + */
16731 case REALPART_EXPR:
16732 case IMAGPART_EXPR:
16733 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16734 RECUR (TREE_OPERAND (t, 0)),
16735 complain|decltype_flag));
16736
16737 case FIX_TRUNC_EXPR:
16738 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16739 false, complain));
16740
16741 case ADDR_EXPR:
16742 op1 = TREE_OPERAND (t, 0);
16743 if (TREE_CODE (op1) == LABEL_DECL)
16744 RETURN (finish_label_address_expr (DECL_NAME (op1),
16745 EXPR_LOCATION (op1)));
16746 if (TREE_CODE (op1) == SCOPE_REF)
16747 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16748 /*done=*/true, /*address_p=*/true);
16749 else
16750 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16751 in_decl);
16752 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16753 complain|decltype_flag));
16754
16755 case PLUS_EXPR:
16756 case MINUS_EXPR:
16757 case MULT_EXPR:
16758 case TRUNC_DIV_EXPR:
16759 case CEIL_DIV_EXPR:
16760 case FLOOR_DIV_EXPR:
16761 case ROUND_DIV_EXPR:
16762 case EXACT_DIV_EXPR:
16763 case BIT_AND_EXPR:
16764 case BIT_IOR_EXPR:
16765 case BIT_XOR_EXPR:
16766 case TRUNC_MOD_EXPR:
16767 case FLOOR_MOD_EXPR:
16768 case TRUTH_ANDIF_EXPR:
16769 case TRUTH_ORIF_EXPR:
16770 case TRUTH_AND_EXPR:
16771 case TRUTH_OR_EXPR:
16772 case RSHIFT_EXPR:
16773 case LSHIFT_EXPR:
16774 case RROTATE_EXPR:
16775 case LROTATE_EXPR:
16776 case EQ_EXPR:
16777 case NE_EXPR:
16778 case MAX_EXPR:
16779 case MIN_EXPR:
16780 case LE_EXPR:
16781 case GE_EXPR:
16782 case LT_EXPR:
16783 case GT_EXPR:
16784 case MEMBER_REF:
16785 case DOTSTAR_EXPR:
16786 {
16787 warning_sentinel s1(warn_type_limits);
16788 warning_sentinel s2(warn_div_by_zero);
16789 warning_sentinel s3(warn_logical_op);
16790 warning_sentinel s4(warn_tautological_compare);
16791 tree op0 = RECUR (TREE_OPERAND (t, 0));
16792 tree op1 = RECUR (TREE_OPERAND (t, 1));
16793 tree r = build_x_binary_op
16794 (input_location, TREE_CODE (t),
16795 op0,
16796 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16797 ? ERROR_MARK
16798 : TREE_CODE (TREE_OPERAND (t, 0))),
16799 op1,
16800 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16801 ? ERROR_MARK
16802 : TREE_CODE (TREE_OPERAND (t, 1))),
16803 /*overload=*/NULL,
16804 complain|decltype_flag);
16805 if (EXPR_P (r) && TREE_NO_WARNING (t))
16806 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16807
16808 RETURN (r);
16809 }
16810
16811 case POINTER_PLUS_EXPR:
16812 {
16813 tree op0 = RECUR (TREE_OPERAND (t, 0));
16814 tree op1 = RECUR (TREE_OPERAND (t, 1));
16815 return fold_build_pointer_plus (op0, op1);
16816 }
16817
16818 case SCOPE_REF:
16819 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16820 /*address_p=*/false));
16821 case ARRAY_REF:
16822 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16823 args, complain, in_decl);
16824 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16825 RECUR (TREE_OPERAND (t, 1)),
16826 complain|decltype_flag));
16827
16828 case ARRAY_NOTATION_REF:
16829 {
16830 tree start_index, length, stride;
16831 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16832 args, complain, in_decl);
16833 start_index = RECUR (ARRAY_NOTATION_START (t));
16834 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16835 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16836 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16837 length, stride, TREE_TYPE (op1)));
16838 }
16839 case SIZEOF_EXPR:
16840 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16841 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16842 RETURN (tsubst_copy (t, args, complain, in_decl));
16843 /* Fall through */
16844
16845 case ALIGNOF_EXPR:
16846 {
16847 tree r;
16848
16849 op1 = TREE_OPERAND (t, 0);
16850 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16851 op1 = TREE_TYPE (op1);
16852 if (!args)
16853 {
16854 /* When there are no ARGS, we are trying to evaluate a
16855 non-dependent expression from the parser. Trying to do
16856 the substitutions may not work. */
16857 if (!TYPE_P (op1))
16858 op1 = TREE_TYPE (op1);
16859 }
16860 else
16861 {
16862 ++cp_unevaluated_operand;
16863 ++c_inhibit_evaluation_warnings;
16864 if (TYPE_P (op1))
16865 op1 = tsubst (op1, args, complain, in_decl);
16866 else
16867 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16868 /*function_p=*/false,
16869 /*integral_constant_expression_p=*/
16870 false);
16871 --cp_unevaluated_operand;
16872 --c_inhibit_evaluation_warnings;
16873 }
16874 if (TYPE_P (op1))
16875 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16876 complain & tf_error);
16877 else
16878 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16879 complain & tf_error);
16880 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16881 {
16882 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16883 {
16884 if (!processing_template_decl && TYPE_P (op1))
16885 {
16886 r = build_min (SIZEOF_EXPR, size_type_node,
16887 build1 (NOP_EXPR, op1, error_mark_node));
16888 SIZEOF_EXPR_TYPE_P (r) = 1;
16889 }
16890 else
16891 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16892 TREE_SIDE_EFFECTS (r) = 0;
16893 TREE_READONLY (r) = 1;
16894 }
16895 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16896 }
16897 RETURN (r);
16898 }
16899
16900 case AT_ENCODE_EXPR:
16901 {
16902 op1 = TREE_OPERAND (t, 0);
16903 ++cp_unevaluated_operand;
16904 ++c_inhibit_evaluation_warnings;
16905 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16906 /*function_p=*/false,
16907 /*integral_constant_expression_p=*/false);
16908 --cp_unevaluated_operand;
16909 --c_inhibit_evaluation_warnings;
16910 RETURN (objc_build_encode_expr (op1));
16911 }
16912
16913 case NOEXCEPT_EXPR:
16914 op1 = TREE_OPERAND (t, 0);
16915 ++cp_unevaluated_operand;
16916 ++c_inhibit_evaluation_warnings;
16917 ++cp_noexcept_operand;
16918 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16919 /*function_p=*/false,
16920 /*integral_constant_expression_p=*/false);
16921 --cp_unevaluated_operand;
16922 --c_inhibit_evaluation_warnings;
16923 --cp_noexcept_operand;
16924 RETURN (finish_noexcept_expr (op1, complain));
16925
16926 case MODOP_EXPR:
16927 {
16928 warning_sentinel s(warn_div_by_zero);
16929 tree lhs = RECUR (TREE_OPERAND (t, 0));
16930 tree rhs = RECUR (TREE_OPERAND (t, 2));
16931 tree r = build_x_modify_expr
16932 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16933 complain|decltype_flag);
16934 /* TREE_NO_WARNING must be set if either the expression was
16935 parenthesized or it uses an operator such as >>= rather
16936 than plain assignment. In the former case, it was already
16937 set and must be copied. In the latter case,
16938 build_x_modify_expr sets it and it must not be reset
16939 here. */
16940 if (TREE_NO_WARNING (t))
16941 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16942
16943 RETURN (r);
16944 }
16945
16946 case ARROW_EXPR:
16947 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16948 args, complain, in_decl);
16949 /* Remember that there was a reference to this entity. */
16950 if (DECL_P (op1)
16951 && !mark_used (op1, complain) && !(complain & tf_error))
16952 RETURN (error_mark_node);
16953 RETURN (build_x_arrow (input_location, op1, complain));
16954
16955 case NEW_EXPR:
16956 {
16957 tree placement = RECUR (TREE_OPERAND (t, 0));
16958 tree init = RECUR (TREE_OPERAND (t, 3));
16959 vec<tree, va_gc> *placement_vec;
16960 vec<tree, va_gc> *init_vec;
16961 tree ret;
16962
16963 if (placement == NULL_TREE)
16964 placement_vec = NULL;
16965 else
16966 {
16967 placement_vec = make_tree_vector ();
16968 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16969 vec_safe_push (placement_vec, TREE_VALUE (placement));
16970 }
16971
16972 /* If there was an initializer in the original tree, but it
16973 instantiated to an empty list, then we should pass a
16974 non-NULL empty vector to tell build_new that it was an
16975 empty initializer() rather than no initializer. This can
16976 only happen when the initializer is a pack expansion whose
16977 parameter packs are of length zero. */
16978 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16979 init_vec = NULL;
16980 else
16981 {
16982 init_vec = make_tree_vector ();
16983 if (init == void_node)
16984 gcc_assert (init_vec != NULL);
16985 else
16986 {
16987 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16988 vec_safe_push (init_vec, TREE_VALUE (init));
16989 }
16990 }
16991
16992 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16993 tree op2 = RECUR (TREE_OPERAND (t, 2));
16994 ret = build_new (&placement_vec, op1, op2, &init_vec,
16995 NEW_EXPR_USE_GLOBAL (t),
16996 complain);
16997
16998 if (placement_vec != NULL)
16999 release_tree_vector (placement_vec);
17000 if (init_vec != NULL)
17001 release_tree_vector (init_vec);
17002
17003 RETURN (ret);
17004 }
17005
17006 case DELETE_EXPR:
17007 {
17008 tree op0 = RECUR (TREE_OPERAND (t, 0));
17009 tree op1 = RECUR (TREE_OPERAND (t, 1));
17010 RETURN (delete_sanity (op0, op1,
17011 DELETE_EXPR_USE_VEC (t),
17012 DELETE_EXPR_USE_GLOBAL (t),
17013 complain));
17014 }
17015
17016 case COMPOUND_EXPR:
17017 {
17018 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17019 complain & ~tf_decltype, in_decl,
17020 /*function_p=*/false,
17021 integral_constant_expression_p);
17022 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17023 op0,
17024 RECUR (TREE_OPERAND (t, 1)),
17025 complain|decltype_flag));
17026 }
17027
17028 case CALL_EXPR:
17029 {
17030 tree function;
17031 vec<tree, va_gc> *call_args;
17032 unsigned int nargs, i;
17033 bool qualified_p;
17034 bool koenig_p;
17035 tree ret;
17036
17037 function = CALL_EXPR_FN (t);
17038 /* Internal function with no arguments. */
17039 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17040 RETURN (t);
17041
17042 /* When we parsed the expression, we determined whether or
17043 not Koenig lookup should be performed. */
17044 koenig_p = KOENIG_LOOKUP_P (t);
17045 if (function == NULL_TREE)
17046 {
17047 koenig_p = false;
17048 qualified_p = false;
17049 }
17050 else if (TREE_CODE (function) == SCOPE_REF)
17051 {
17052 qualified_p = true;
17053 function = tsubst_qualified_id (function, args, complain, in_decl,
17054 /*done=*/false,
17055 /*address_p=*/false);
17056 }
17057 else if (koenig_p && identifier_p (function))
17058 {
17059 /* Do nothing; calling tsubst_copy_and_build on an identifier
17060 would incorrectly perform unqualified lookup again.
17061
17062 Note that we can also have an IDENTIFIER_NODE if the earlier
17063 unqualified lookup found a member function; in that case
17064 koenig_p will be false and we do want to do the lookup
17065 again to find the instantiated member function.
17066
17067 FIXME but doing that causes c++/15272, so we need to stop
17068 using IDENTIFIER_NODE in that situation. */
17069 qualified_p = false;
17070 }
17071 else
17072 {
17073 if (TREE_CODE (function) == COMPONENT_REF)
17074 {
17075 tree op = TREE_OPERAND (function, 1);
17076
17077 qualified_p = (TREE_CODE (op) == SCOPE_REF
17078 || (BASELINK_P (op)
17079 && BASELINK_QUALIFIED_P (op)));
17080 }
17081 else
17082 qualified_p = false;
17083
17084 if (TREE_CODE (function) == ADDR_EXPR
17085 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17086 /* Avoid error about taking the address of a constructor. */
17087 function = TREE_OPERAND (function, 0);
17088
17089 function = tsubst_copy_and_build (function, args, complain,
17090 in_decl,
17091 !qualified_p,
17092 integral_constant_expression_p);
17093
17094 if (BASELINK_P (function))
17095 qualified_p = true;
17096 }
17097
17098 nargs = call_expr_nargs (t);
17099 call_args = make_tree_vector ();
17100 for (i = 0; i < nargs; ++i)
17101 {
17102 tree arg = CALL_EXPR_ARG (t, i);
17103
17104 if (!PACK_EXPANSION_P (arg))
17105 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17106 else
17107 {
17108 /* Expand the pack expansion and push each entry onto
17109 CALL_ARGS. */
17110 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17111 if (TREE_CODE (arg) == TREE_VEC)
17112 {
17113 unsigned int len, j;
17114
17115 len = TREE_VEC_LENGTH (arg);
17116 for (j = 0; j < len; ++j)
17117 {
17118 tree value = TREE_VEC_ELT (arg, j);
17119 if (value != NULL_TREE)
17120 value = convert_from_reference (value);
17121 vec_safe_push (call_args, value);
17122 }
17123 }
17124 else
17125 {
17126 /* A partial substitution. Add one entry. */
17127 vec_safe_push (call_args, arg);
17128 }
17129 }
17130 }
17131
17132 /* We do not perform argument-dependent lookup if normal
17133 lookup finds a non-function, in accordance with the
17134 expected resolution of DR 218. */
17135 if (koenig_p
17136 && ((is_overloaded_fn (function)
17137 /* If lookup found a member function, the Koenig lookup is
17138 not appropriate, even if an unqualified-name was used
17139 to denote the function. */
17140 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17141 || identifier_p (function))
17142 /* Only do this when substitution turns a dependent call
17143 into a non-dependent call. */
17144 && type_dependent_expression_p_push (t)
17145 && !any_type_dependent_arguments_p (call_args))
17146 function = perform_koenig_lookup (function, call_args, tf_none);
17147
17148 if (function != NULL_TREE
17149 && identifier_p (function)
17150 && !any_type_dependent_arguments_p (call_args))
17151 {
17152 if (koenig_p && (complain & tf_warning_or_error))
17153 {
17154 /* For backwards compatibility and good diagnostics, try
17155 the unqualified lookup again if we aren't in SFINAE
17156 context. */
17157 tree unq = (tsubst_copy_and_build
17158 (function, args, complain, in_decl, true,
17159 integral_constant_expression_p));
17160 if (unq == error_mark_node)
17161 {
17162 release_tree_vector (call_args);
17163 RETURN (error_mark_node);
17164 }
17165
17166 if (unq != function)
17167 {
17168 /* In a lambda fn, we have to be careful to not
17169 introduce new this captures. Legacy code can't
17170 be using lambdas anyway, so it's ok to be
17171 stricter. */
17172 bool in_lambda = (current_class_type
17173 && LAMBDA_TYPE_P (current_class_type));
17174 char const *msg = "%qD was not declared in this scope, "
17175 "and no declarations were found by "
17176 "argument-dependent lookup at the point "
17177 "of instantiation";
17178
17179 bool diag = true;
17180 if (in_lambda)
17181 error_at (EXPR_LOC_OR_LOC (t, input_location),
17182 msg, function);
17183 else
17184 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17185 msg, function);
17186 if (diag)
17187 {
17188 tree fn = unq;
17189 if (INDIRECT_REF_P (fn))
17190 fn = TREE_OPERAND (fn, 0);
17191 if (TREE_CODE (fn) == COMPONENT_REF)
17192 fn = TREE_OPERAND (fn, 1);
17193 if (is_overloaded_fn (fn))
17194 fn = get_first_fn (fn);
17195
17196 if (!DECL_P (fn))
17197 /* Can't say anything more. */;
17198 else if (DECL_CLASS_SCOPE_P (fn))
17199 {
17200 location_t loc = EXPR_LOC_OR_LOC (t,
17201 input_location);
17202 inform (loc,
17203 "declarations in dependent base %qT are "
17204 "not found by unqualified lookup",
17205 DECL_CLASS_CONTEXT (fn));
17206 if (current_class_ptr)
17207 inform (loc,
17208 "use %<this->%D%> instead", function);
17209 else
17210 inform (loc,
17211 "use %<%T::%D%> instead",
17212 current_class_name, function);
17213 }
17214 else
17215 inform (DECL_SOURCE_LOCATION (fn),
17216 "%qD declared here, later in the "
17217 "translation unit", fn);
17218 if (in_lambda)
17219 {
17220 release_tree_vector (call_args);
17221 RETURN (error_mark_node);
17222 }
17223 }
17224
17225 function = unq;
17226 }
17227 }
17228 if (identifier_p (function))
17229 {
17230 if (complain & tf_error)
17231 unqualified_name_lookup_error (function);
17232 release_tree_vector (call_args);
17233 RETURN (error_mark_node);
17234 }
17235 }
17236
17237 /* Remember that there was a reference to this entity. */
17238 if (function != NULL_TREE
17239 && DECL_P (function)
17240 && !mark_used (function, complain) && !(complain & tf_error))
17241 {
17242 release_tree_vector (call_args);
17243 RETURN (error_mark_node);
17244 }
17245
17246 /* Put back tf_decltype for the actual call. */
17247 complain |= decltype_flag;
17248
17249 if (function == NULL_TREE)
17250 switch (CALL_EXPR_IFN (t))
17251 {
17252 case IFN_LAUNDER:
17253 gcc_assert (nargs == 1);
17254 if (vec_safe_length (call_args) != 1)
17255 {
17256 error_at (EXPR_LOC_OR_LOC (t, input_location),
17257 "wrong number of arguments to "
17258 "%<__builtin_launder%>");
17259 ret = error_mark_node;
17260 }
17261 else
17262 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17263 input_location),
17264 (*call_args)[0], complain);
17265 break;
17266
17267 default:
17268 /* Unsupported internal function with arguments. */
17269 gcc_unreachable ();
17270 }
17271 else if (TREE_CODE (function) == OFFSET_REF)
17272 ret = build_offset_ref_call_from_tree (function, &call_args,
17273 complain);
17274 else if (TREE_CODE (function) == COMPONENT_REF)
17275 {
17276 tree instance = TREE_OPERAND (function, 0);
17277 tree fn = TREE_OPERAND (function, 1);
17278
17279 if (processing_template_decl
17280 && (type_dependent_expression_p (instance)
17281 || (!BASELINK_P (fn)
17282 && TREE_CODE (fn) != FIELD_DECL)
17283 || type_dependent_expression_p (fn)
17284 || any_type_dependent_arguments_p (call_args)))
17285 ret = build_nt_call_vec (function, call_args);
17286 else if (!BASELINK_P (fn))
17287 ret = finish_call_expr (function, &call_args,
17288 /*disallow_virtual=*/false,
17289 /*koenig_p=*/false,
17290 complain);
17291 else
17292 ret = (build_new_method_call
17293 (instance, fn,
17294 &call_args, NULL_TREE,
17295 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17296 /*fn_p=*/NULL,
17297 complain));
17298 }
17299 else
17300 ret = finish_call_expr (function, &call_args,
17301 /*disallow_virtual=*/qualified_p,
17302 koenig_p,
17303 complain);
17304
17305 release_tree_vector (call_args);
17306
17307 if (ret != error_mark_node)
17308 {
17309 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17310 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17311 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17312 bool thk = CALL_FROM_THUNK_P (t);
17313 if (op || ord || rev || thk)
17314 {
17315 function = extract_call_expr (ret);
17316 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17317 CALL_EXPR_ORDERED_ARGS (function) = ord;
17318 CALL_EXPR_REVERSE_ARGS (function) = rev;
17319 if (thk)
17320 {
17321 CALL_FROM_THUNK_P (function) = true;
17322 /* The thunk location is not interesting. */
17323 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17324 }
17325 }
17326 }
17327
17328 RETURN (ret);
17329 }
17330
17331 case COND_EXPR:
17332 {
17333 tree cond = RECUR (TREE_OPERAND (t, 0));
17334 tree folded_cond = fold_non_dependent_expr (cond);
17335 tree exp1, exp2;
17336
17337 if (TREE_CODE (folded_cond) == INTEGER_CST)
17338 {
17339 if (integer_zerop (folded_cond))
17340 {
17341 ++c_inhibit_evaluation_warnings;
17342 exp1 = RECUR (TREE_OPERAND (t, 1));
17343 --c_inhibit_evaluation_warnings;
17344 exp2 = RECUR (TREE_OPERAND (t, 2));
17345 }
17346 else
17347 {
17348 exp1 = RECUR (TREE_OPERAND (t, 1));
17349 ++c_inhibit_evaluation_warnings;
17350 exp2 = RECUR (TREE_OPERAND (t, 2));
17351 --c_inhibit_evaluation_warnings;
17352 }
17353 cond = folded_cond;
17354 }
17355 else
17356 {
17357 exp1 = RECUR (TREE_OPERAND (t, 1));
17358 exp2 = RECUR (TREE_OPERAND (t, 2));
17359 }
17360
17361 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17362 cond, exp1, exp2, complain));
17363 }
17364
17365 case PSEUDO_DTOR_EXPR:
17366 {
17367 tree op0 = RECUR (TREE_OPERAND (t, 0));
17368 tree op1 = RECUR (TREE_OPERAND (t, 1));
17369 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17370 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17371 input_location));
17372 }
17373
17374 case TREE_LIST:
17375 {
17376 tree purpose, value, chain;
17377
17378 if (t == void_list_node)
17379 RETURN (t);
17380
17381 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17382 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17383 {
17384 /* We have pack expansions, so expand those and
17385 create a new list out of it. */
17386 tree purposevec = NULL_TREE;
17387 tree valuevec = NULL_TREE;
17388 tree chain;
17389 int i, len = -1;
17390
17391 /* Expand the argument expressions. */
17392 if (TREE_PURPOSE (t))
17393 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17394 complain, in_decl);
17395 if (TREE_VALUE (t))
17396 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17397 complain, in_decl);
17398
17399 /* Build the rest of the list. */
17400 chain = TREE_CHAIN (t);
17401 if (chain && chain != void_type_node)
17402 chain = RECUR (chain);
17403
17404 /* Determine the number of arguments. */
17405 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17406 {
17407 len = TREE_VEC_LENGTH (purposevec);
17408 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17409 }
17410 else if (TREE_CODE (valuevec) == TREE_VEC)
17411 len = TREE_VEC_LENGTH (valuevec);
17412 else
17413 {
17414 /* Since we only performed a partial substitution into
17415 the argument pack, we only RETURN (a single list
17416 node. */
17417 if (purposevec == TREE_PURPOSE (t)
17418 && valuevec == TREE_VALUE (t)
17419 && chain == TREE_CHAIN (t))
17420 RETURN (t);
17421
17422 RETURN (tree_cons (purposevec, valuevec, chain));
17423 }
17424
17425 /* Convert the argument vectors into a TREE_LIST */
17426 i = len;
17427 while (i > 0)
17428 {
17429 /* Grab the Ith values. */
17430 i--;
17431 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17432 : NULL_TREE;
17433 value
17434 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17435 : NULL_TREE;
17436
17437 /* Build the list (backwards). */
17438 chain = tree_cons (purpose, value, chain);
17439 }
17440
17441 RETURN (chain);
17442 }
17443
17444 purpose = TREE_PURPOSE (t);
17445 if (purpose)
17446 purpose = RECUR (purpose);
17447 value = TREE_VALUE (t);
17448 if (value)
17449 value = RECUR (value);
17450 chain = TREE_CHAIN (t);
17451 if (chain && chain != void_type_node)
17452 chain = RECUR (chain);
17453 if (purpose == TREE_PURPOSE (t)
17454 && value == TREE_VALUE (t)
17455 && chain == TREE_CHAIN (t))
17456 RETURN (t);
17457 RETURN (tree_cons (purpose, value, chain));
17458 }
17459
17460 case COMPONENT_REF:
17461 {
17462 tree object;
17463 tree object_type;
17464 tree member;
17465 tree r;
17466
17467 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17468 args, complain, in_decl);
17469 /* Remember that there was a reference to this entity. */
17470 if (DECL_P (object)
17471 && !mark_used (object, complain) && !(complain & tf_error))
17472 RETURN (error_mark_node);
17473 object_type = TREE_TYPE (object);
17474
17475 member = TREE_OPERAND (t, 1);
17476 if (BASELINK_P (member))
17477 member = tsubst_baselink (member,
17478 non_reference (TREE_TYPE (object)),
17479 args, complain, in_decl);
17480 else
17481 member = tsubst_copy (member, args, complain, in_decl);
17482 if (member == error_mark_node)
17483 RETURN (error_mark_node);
17484
17485 if (TREE_CODE (member) == FIELD_DECL)
17486 {
17487 r = finish_non_static_data_member (member, object, NULL_TREE);
17488 if (TREE_CODE (r) == COMPONENT_REF)
17489 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17490 RETURN (r);
17491 }
17492 else if (type_dependent_expression_p (object))
17493 /* We can't do much here. */;
17494 else if (!CLASS_TYPE_P (object_type))
17495 {
17496 if (scalarish_type_p (object_type))
17497 {
17498 tree s = NULL_TREE;
17499 tree dtor = member;
17500
17501 if (TREE_CODE (dtor) == SCOPE_REF)
17502 {
17503 s = TREE_OPERAND (dtor, 0);
17504 dtor = TREE_OPERAND (dtor, 1);
17505 }
17506 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17507 {
17508 dtor = TREE_OPERAND (dtor, 0);
17509 if (TYPE_P (dtor))
17510 RETURN (finish_pseudo_destructor_expr
17511 (object, s, dtor, input_location));
17512 }
17513 }
17514 }
17515 else if (TREE_CODE (member) == SCOPE_REF
17516 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17517 {
17518 /* Lookup the template functions now that we know what the
17519 scope is. */
17520 tree scope = TREE_OPERAND (member, 0);
17521 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17522 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17523 member = lookup_qualified_name (scope, tmpl,
17524 /*is_type_p=*/false,
17525 /*complain=*/false);
17526 if (BASELINK_P (member))
17527 {
17528 BASELINK_FUNCTIONS (member)
17529 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17530 args);
17531 member = (adjust_result_of_qualified_name_lookup
17532 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17533 object_type));
17534 }
17535 else
17536 {
17537 qualified_name_lookup_error (scope, tmpl, member,
17538 input_location);
17539 RETURN (error_mark_node);
17540 }
17541 }
17542 else if (TREE_CODE (member) == SCOPE_REF
17543 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17544 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17545 {
17546 if (complain & tf_error)
17547 {
17548 if (TYPE_P (TREE_OPERAND (member, 0)))
17549 error ("%qT is not a class or namespace",
17550 TREE_OPERAND (member, 0));
17551 else
17552 error ("%qD is not a class or namespace",
17553 TREE_OPERAND (member, 0));
17554 }
17555 RETURN (error_mark_node);
17556 }
17557
17558 r = finish_class_member_access_expr (object, member,
17559 /*template_p=*/false,
17560 complain);
17561 if (TREE_CODE (r) == COMPONENT_REF)
17562 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17563 RETURN (r);
17564 }
17565
17566 case THROW_EXPR:
17567 RETURN (build_throw
17568 (RECUR (TREE_OPERAND (t, 0))));
17569
17570 case CONSTRUCTOR:
17571 {
17572 vec<constructor_elt, va_gc> *n;
17573 constructor_elt *ce;
17574 unsigned HOST_WIDE_INT idx;
17575 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17576 bool process_index_p;
17577 int newlen;
17578 bool need_copy_p = false;
17579 tree r;
17580
17581 if (type == error_mark_node)
17582 RETURN (error_mark_node);
17583
17584 /* digest_init will do the wrong thing if we let it. */
17585 if (type && TYPE_PTRMEMFUNC_P (type))
17586 RETURN (t);
17587
17588 /* We do not want to process the index of aggregate
17589 initializers as they are identifier nodes which will be
17590 looked up by digest_init. */
17591 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17592
17593 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17594 newlen = vec_safe_length (n);
17595 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17596 {
17597 if (ce->index && process_index_p
17598 /* An identifier index is looked up in the type
17599 being initialized, not the current scope. */
17600 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17601 ce->index = RECUR (ce->index);
17602
17603 if (PACK_EXPANSION_P (ce->value))
17604 {
17605 /* Substitute into the pack expansion. */
17606 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17607 in_decl);
17608
17609 if (ce->value == error_mark_node
17610 || PACK_EXPANSION_P (ce->value))
17611 ;
17612 else if (TREE_VEC_LENGTH (ce->value) == 1)
17613 /* Just move the argument into place. */
17614 ce->value = TREE_VEC_ELT (ce->value, 0);
17615 else
17616 {
17617 /* Update the length of the final CONSTRUCTOR
17618 arguments vector, and note that we will need to
17619 copy.*/
17620 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17621 need_copy_p = true;
17622 }
17623 }
17624 else
17625 ce->value = RECUR (ce->value);
17626 }
17627
17628 if (need_copy_p)
17629 {
17630 vec<constructor_elt, va_gc> *old_n = n;
17631
17632 vec_alloc (n, newlen);
17633 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17634 {
17635 if (TREE_CODE (ce->value) == TREE_VEC)
17636 {
17637 int i, len = TREE_VEC_LENGTH (ce->value);
17638 for (i = 0; i < len; ++i)
17639 CONSTRUCTOR_APPEND_ELT (n, 0,
17640 TREE_VEC_ELT (ce->value, i));
17641 }
17642 else
17643 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17644 }
17645 }
17646
17647 r = build_constructor (init_list_type_node, n);
17648 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17649
17650 if (TREE_HAS_CONSTRUCTOR (t))
17651 RETURN (finish_compound_literal (type, r, complain));
17652
17653 TREE_TYPE (r) = type;
17654 RETURN (r);
17655 }
17656
17657 case TYPEID_EXPR:
17658 {
17659 tree operand_0 = TREE_OPERAND (t, 0);
17660 if (TYPE_P (operand_0))
17661 {
17662 operand_0 = tsubst (operand_0, args, complain, in_decl);
17663 RETURN (get_typeid (operand_0, complain));
17664 }
17665 else
17666 {
17667 operand_0 = RECUR (operand_0);
17668 RETURN (build_typeid (operand_0, complain));
17669 }
17670 }
17671
17672 case VAR_DECL:
17673 if (!args)
17674 RETURN (t);
17675 else if (DECL_PACK_P (t))
17676 {
17677 /* We don't build decls for an instantiation of a
17678 variadic capture proxy, we instantiate the elements
17679 when needed. */
17680 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17681 return RECUR (DECL_VALUE_EXPR (t));
17682 }
17683 /* Fall through */
17684
17685 case PARM_DECL:
17686 {
17687 tree r = tsubst_copy (t, args, complain, in_decl);
17688 /* ??? We're doing a subset of finish_id_expression here. */
17689 if (VAR_P (r)
17690 && !processing_template_decl
17691 && !cp_unevaluated_operand
17692 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17693 && CP_DECL_THREAD_LOCAL_P (r))
17694 {
17695 if (tree wrap = get_tls_wrapper_fn (r))
17696 /* Replace an evaluated use of the thread_local variable with
17697 a call to its wrapper. */
17698 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17699 }
17700 else if (outer_automatic_var_p (r))
17701 {
17702 r = process_outer_var_ref (r, complain);
17703 if (is_capture_proxy (r))
17704 register_local_specialization (r, t);
17705 }
17706
17707 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17708 /* If the original type was a reference, we'll be wrapped in
17709 the appropriate INDIRECT_REF. */
17710 r = convert_from_reference (r);
17711 RETURN (r);
17712 }
17713
17714 case VA_ARG_EXPR:
17715 {
17716 tree op0 = RECUR (TREE_OPERAND (t, 0));
17717 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17718 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17719 }
17720
17721 case OFFSETOF_EXPR:
17722 {
17723 tree object_ptr
17724 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17725 in_decl, /*function_p=*/false,
17726 /*integral_constant_expression_p=*/false);
17727 RETURN (finish_offsetof (object_ptr,
17728 RECUR (TREE_OPERAND (t, 0)),
17729 EXPR_LOCATION (t)));
17730 }
17731
17732 case ADDRESSOF_EXPR:
17733 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17734 RECUR (TREE_OPERAND (t, 0)), complain));
17735
17736 case TRAIT_EXPR:
17737 {
17738 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17739 complain, in_decl);
17740
17741 tree type2 = TRAIT_EXPR_TYPE2 (t);
17742 if (type2 && TREE_CODE (type2) == TREE_LIST)
17743 type2 = RECUR (type2);
17744 else if (type2)
17745 type2 = tsubst (type2, args, complain, in_decl);
17746
17747 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17748 }
17749
17750 case STMT_EXPR:
17751 {
17752 tree old_stmt_expr = cur_stmt_expr;
17753 tree stmt_expr = begin_stmt_expr ();
17754
17755 cur_stmt_expr = stmt_expr;
17756 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17757 integral_constant_expression_p);
17758 stmt_expr = finish_stmt_expr (stmt_expr, false);
17759 cur_stmt_expr = old_stmt_expr;
17760
17761 /* If the resulting list of expression statement is empty,
17762 fold it further into void_node. */
17763 if (empty_expr_stmt_p (stmt_expr))
17764 stmt_expr = void_node;
17765
17766 RETURN (stmt_expr);
17767 }
17768
17769 case LAMBDA_EXPR:
17770 {
17771 tree r = build_lambda_expr ();
17772
17773 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17774 LAMBDA_EXPR_CLOSURE (r) = type;
17775 CLASSTYPE_LAMBDA_EXPR (type) = r;
17776
17777 LAMBDA_EXPR_LOCATION (r)
17778 = LAMBDA_EXPR_LOCATION (t);
17779 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17780 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17781 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17782 LAMBDA_EXPR_DISCRIMINATOR (r)
17783 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17784 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17785 if (!scope)
17786 /* No substitution needed. */;
17787 else if (VAR_OR_FUNCTION_DECL_P (scope))
17788 /* For a function or variable scope, we want to use tsubst so that we
17789 don't complain about referring to an auto before deduction. */
17790 scope = tsubst (scope, args, complain, in_decl);
17791 else if (TREE_CODE (scope) == PARM_DECL)
17792 {
17793 /* Look up the parameter we want directly, as tsubst_copy
17794 doesn't do what we need. */
17795 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17796 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17797 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17798 parm = DECL_CHAIN (parm);
17799 scope = parm;
17800 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17801 if (DECL_CONTEXT (scope) == NULL_TREE)
17802 DECL_CONTEXT (scope) = fn;
17803 }
17804 else if (TREE_CODE (scope) == FIELD_DECL)
17805 /* For a field, use tsubst_copy so that we look up the existing field
17806 rather than build a new one. */
17807 scope = RECUR (scope);
17808 else
17809 gcc_unreachable ();
17810 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17811
17812 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17813 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17814
17815 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17816 determine_visibility (TYPE_NAME (type));
17817 /* Now that we know visibility, instantiate the type so we have a
17818 declaration of the op() for later calls to lambda_function. */
17819 complete_type (type);
17820
17821 if (tree fn = lambda_function (type))
17822 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17823
17824 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17825
17826 insert_pending_capture_proxies ();
17827
17828 RETURN (build_lambda_object (r));
17829 }
17830
17831 case TARGET_EXPR:
17832 /* We can get here for a constant initializer of non-dependent type.
17833 FIXME stop folding in cp_parser_initializer_clause. */
17834 {
17835 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17836 complain);
17837 RETURN (r);
17838 }
17839
17840 case TRANSACTION_EXPR:
17841 RETURN (tsubst_expr(t, args, complain, in_decl,
17842 integral_constant_expression_p));
17843
17844 case PAREN_EXPR:
17845 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17846
17847 case VEC_PERM_EXPR:
17848 {
17849 tree op0 = RECUR (TREE_OPERAND (t, 0));
17850 tree op1 = RECUR (TREE_OPERAND (t, 1));
17851 tree op2 = RECUR (TREE_OPERAND (t, 2));
17852 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17853 complain));
17854 }
17855
17856 case REQUIRES_EXPR:
17857 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17858
17859 default:
17860 /* Handle Objective-C++ constructs, if appropriate. */
17861 {
17862 tree subst
17863 = objcp_tsubst_copy_and_build (t, args, complain,
17864 in_decl, /*function_p=*/false);
17865 if (subst)
17866 RETURN (subst);
17867 }
17868 RETURN (tsubst_copy (t, args, complain, in_decl));
17869 }
17870
17871 #undef RECUR
17872 #undef RETURN
17873 out:
17874 input_location = loc;
17875 return retval;
17876 }
17877
17878 /* Verify that the instantiated ARGS are valid. For type arguments,
17879 make sure that the type's linkage is ok. For non-type arguments,
17880 make sure they are constants if they are integral or enumerations.
17881 Emit an error under control of COMPLAIN, and return TRUE on error. */
17882
17883 static bool
17884 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17885 {
17886 if (dependent_template_arg_p (t))
17887 return false;
17888 if (ARGUMENT_PACK_P (t))
17889 {
17890 tree vec = ARGUMENT_PACK_ARGS (t);
17891 int len = TREE_VEC_LENGTH (vec);
17892 bool result = false;
17893 int i;
17894
17895 for (i = 0; i < len; ++i)
17896 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17897 result = true;
17898 return result;
17899 }
17900 else if (TYPE_P (t))
17901 {
17902 /* [basic.link]: A name with no linkage (notably, the name
17903 of a class or enumeration declared in a local scope)
17904 shall not be used to declare an entity with linkage.
17905 This implies that names with no linkage cannot be used as
17906 template arguments
17907
17908 DR 757 relaxes this restriction for C++0x. */
17909 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17910 : no_linkage_check (t, /*relaxed_p=*/false));
17911
17912 if (nt)
17913 {
17914 /* DR 488 makes use of a type with no linkage cause
17915 type deduction to fail. */
17916 if (complain & tf_error)
17917 {
17918 if (TYPE_UNNAMED_P (nt))
17919 error ("%qT is/uses unnamed type", t);
17920 else
17921 error ("template argument for %qD uses local type %qT",
17922 tmpl, t);
17923 }
17924 return true;
17925 }
17926 /* In order to avoid all sorts of complications, we do not
17927 allow variably-modified types as template arguments. */
17928 else if (variably_modified_type_p (t, NULL_TREE))
17929 {
17930 if (complain & tf_error)
17931 error ("%qT is a variably modified type", t);
17932 return true;
17933 }
17934 }
17935 /* Class template and alias template arguments should be OK. */
17936 else if (DECL_TYPE_TEMPLATE_P (t))
17937 ;
17938 /* A non-type argument of integral or enumerated type must be a
17939 constant. */
17940 else if (TREE_TYPE (t)
17941 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17942 && !REFERENCE_REF_P (t)
17943 && !TREE_CONSTANT (t))
17944 {
17945 if (complain & tf_error)
17946 error ("integral expression %qE is not constant", t);
17947 return true;
17948 }
17949 return false;
17950 }
17951
17952 static bool
17953 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17954 {
17955 int ix, len = DECL_NTPARMS (tmpl);
17956 bool result = false;
17957
17958 for (ix = 0; ix != len; ix++)
17959 {
17960 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17961 result = true;
17962 }
17963 if (result && (complain & tf_error))
17964 error (" trying to instantiate %qD", tmpl);
17965 return result;
17966 }
17967
17968 /* We're out of SFINAE context now, so generate diagnostics for the access
17969 errors we saw earlier when instantiating D from TMPL and ARGS. */
17970
17971 static void
17972 recheck_decl_substitution (tree d, tree tmpl, tree args)
17973 {
17974 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17975 tree type = TREE_TYPE (pattern);
17976 location_t loc = input_location;
17977
17978 push_access_scope (d);
17979 push_deferring_access_checks (dk_no_deferred);
17980 input_location = DECL_SOURCE_LOCATION (pattern);
17981 tsubst (type, args, tf_warning_or_error, d);
17982 input_location = loc;
17983 pop_deferring_access_checks ();
17984 pop_access_scope (d);
17985 }
17986
17987 /* Instantiate the indicated variable, function, or alias template TMPL with
17988 the template arguments in TARG_PTR. */
17989
17990 static tree
17991 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17992 {
17993 tree targ_ptr = orig_args;
17994 tree fndecl;
17995 tree gen_tmpl;
17996 tree spec;
17997 bool access_ok = true;
17998
17999 if (tmpl == error_mark_node)
18000 return error_mark_node;
18001
18002 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18003
18004 /* If this function is a clone, handle it specially. */
18005 if (DECL_CLONED_FUNCTION_P (tmpl))
18006 {
18007 tree spec;
18008 tree clone;
18009
18010 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18011 DECL_CLONED_FUNCTION. */
18012 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18013 targ_ptr, complain);
18014 if (spec == error_mark_node)
18015 return error_mark_node;
18016
18017 /* Look for the clone. */
18018 FOR_EACH_CLONE (clone, spec)
18019 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18020 return clone;
18021 /* We should always have found the clone by now. */
18022 gcc_unreachable ();
18023 return NULL_TREE;
18024 }
18025
18026 if (targ_ptr == error_mark_node)
18027 return error_mark_node;
18028
18029 /* Check to see if we already have this specialization. */
18030 gen_tmpl = most_general_template (tmpl);
18031 if (TMPL_ARGS_DEPTH (targ_ptr)
18032 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18033 /* targ_ptr only has the innermost template args, so add the outer ones
18034 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18035 the case of a non-dependent call within a template definition). */
18036 targ_ptr = (add_outermost_template_args
18037 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18038 targ_ptr));
18039
18040 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18041 but it doesn't seem to be on the hot path. */
18042 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18043
18044 gcc_assert (tmpl == gen_tmpl
18045 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18046 == spec)
18047 || fndecl == NULL_TREE);
18048
18049 if (spec != NULL_TREE)
18050 {
18051 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18052 {
18053 if (complain & tf_error)
18054 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18055 return error_mark_node;
18056 }
18057 return spec;
18058 }
18059
18060 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18061 complain))
18062 return error_mark_node;
18063
18064 /* We are building a FUNCTION_DECL, during which the access of its
18065 parameters and return types have to be checked. However this
18066 FUNCTION_DECL which is the desired context for access checking
18067 is not built yet. We solve this chicken-and-egg problem by
18068 deferring all checks until we have the FUNCTION_DECL. */
18069 push_deferring_access_checks (dk_deferred);
18070
18071 /* Instantiation of the function happens in the context of the function
18072 template, not the context of the overload resolution we're doing. */
18073 push_to_top_level ();
18074 /* If there are dependent arguments, e.g. because we're doing partial
18075 ordering, make sure processing_template_decl stays set. */
18076 if (uses_template_parms (targ_ptr))
18077 ++processing_template_decl;
18078 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18079 {
18080 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18081 complain, gen_tmpl, true);
18082 push_nested_class (ctx);
18083 }
18084
18085 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18086
18087 fndecl = NULL_TREE;
18088 if (VAR_P (pattern))
18089 {
18090 /* We need to determine if we're using a partial or explicit
18091 specialization now, because the type of the variable could be
18092 different. */
18093 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18094 tree elt = most_specialized_partial_spec (tid, complain);
18095 if (elt == error_mark_node)
18096 pattern = error_mark_node;
18097 else if (elt)
18098 {
18099 tree partial_tmpl = TREE_VALUE (elt);
18100 tree partial_args = TREE_PURPOSE (elt);
18101 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18102 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18103 }
18104 }
18105
18106 /* Substitute template parameters to obtain the specialization. */
18107 if (fndecl == NULL_TREE)
18108 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18109 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18110 pop_nested_class ();
18111 pop_from_top_level ();
18112
18113 if (fndecl == error_mark_node)
18114 {
18115 pop_deferring_access_checks ();
18116 return error_mark_node;
18117 }
18118
18119 /* The DECL_TI_TEMPLATE should always be the immediate parent
18120 template, not the most general template. */
18121 DECL_TI_TEMPLATE (fndecl) = tmpl;
18122 DECL_TI_ARGS (fndecl) = targ_ptr;
18123
18124 /* Now we know the specialization, compute access previously
18125 deferred. Do no access control for inheriting constructors,
18126 as we already checked access for the inherited constructor. */
18127 if (!(flag_new_inheriting_ctors
18128 && DECL_INHERITED_CTOR (fndecl)))
18129 {
18130 push_access_scope (fndecl);
18131 if (!perform_deferred_access_checks (complain))
18132 access_ok = false;
18133 pop_access_scope (fndecl);
18134 }
18135 pop_deferring_access_checks ();
18136
18137 /* If we've just instantiated the main entry point for a function,
18138 instantiate all the alternate entry points as well. We do this
18139 by cloning the instantiation of the main entry point, not by
18140 instantiating the template clones. */
18141 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18142 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
18143
18144 if (!access_ok)
18145 {
18146 if (!(complain & tf_error))
18147 {
18148 /* Remember to reinstantiate when we're out of SFINAE so the user
18149 can see the errors. */
18150 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18151 }
18152 return error_mark_node;
18153 }
18154 return fndecl;
18155 }
18156
18157 /* Wrapper for instantiate_template_1. */
18158
18159 tree
18160 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18161 {
18162 tree ret;
18163 timevar_push (TV_TEMPLATE_INST);
18164 ret = instantiate_template_1 (tmpl, orig_args, complain);
18165 timevar_pop (TV_TEMPLATE_INST);
18166 return ret;
18167 }
18168
18169 /* Instantiate the alias template TMPL with ARGS. Also push a template
18170 instantiation level, which instantiate_template doesn't do because
18171 functions and variables have sufficient context established by the
18172 callers. */
18173
18174 static tree
18175 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18176 {
18177 struct pending_template *old_last_pend = last_pending_template;
18178 struct tinst_level *old_error_tinst = last_error_tinst_level;
18179 if (tmpl == error_mark_node || args == error_mark_node)
18180 return error_mark_node;
18181 tree tinst = build_tree_list (tmpl, args);
18182 if (!push_tinst_level (tinst))
18183 {
18184 ggc_free (tinst);
18185 return error_mark_node;
18186 }
18187
18188 args =
18189 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18190 args, tmpl, complain,
18191 /*require_all_args=*/true,
18192 /*use_default_args=*/true);
18193
18194 tree r = instantiate_template (tmpl, args, complain);
18195 pop_tinst_level ();
18196 /* We can't free this if a pending_template entry or last_error_tinst_level
18197 is pointing at it. */
18198 if (last_pending_template == old_last_pend
18199 && last_error_tinst_level == old_error_tinst)
18200 ggc_free (tinst);
18201
18202 return r;
18203 }
18204
18205 /* PARM is a template parameter pack for FN. Returns true iff
18206 PARM is used in a deducible way in the argument list of FN. */
18207
18208 static bool
18209 pack_deducible_p (tree parm, tree fn)
18210 {
18211 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18212 for (; t; t = TREE_CHAIN (t))
18213 {
18214 tree type = TREE_VALUE (t);
18215 tree packs;
18216 if (!PACK_EXPANSION_P (type))
18217 continue;
18218 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18219 packs; packs = TREE_CHAIN (packs))
18220 if (template_args_equal (TREE_VALUE (packs), parm))
18221 {
18222 /* The template parameter pack is used in a function parameter
18223 pack. If this is the end of the parameter list, the
18224 template parameter pack is deducible. */
18225 if (TREE_CHAIN (t) == void_list_node)
18226 return true;
18227 else
18228 /* Otherwise, not. Well, it could be deduced from
18229 a non-pack parameter, but doing so would end up with
18230 a deduction mismatch, so don't bother. */
18231 return false;
18232 }
18233 }
18234 /* The template parameter pack isn't used in any function parameter
18235 packs, but it might be used deeper, e.g. tuple<Args...>. */
18236 return true;
18237 }
18238
18239 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18240 NARGS elements of the arguments that are being used when calling
18241 it. TARGS is a vector into which the deduced template arguments
18242 are placed.
18243
18244 Returns either a FUNCTION_DECL for the matching specialization of FN or
18245 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18246 true, diagnostics will be printed to explain why it failed.
18247
18248 If FN is a conversion operator, or we are trying to produce a specific
18249 specialization, RETURN_TYPE is the return type desired.
18250
18251 The EXPLICIT_TARGS are explicit template arguments provided via a
18252 template-id.
18253
18254 The parameter STRICT is one of:
18255
18256 DEDUCE_CALL:
18257 We are deducing arguments for a function call, as in
18258 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18259 deducing arguments for a call to the result of a conversion
18260 function template, as in [over.call.object].
18261
18262 DEDUCE_CONV:
18263 We are deducing arguments for a conversion function, as in
18264 [temp.deduct.conv].
18265
18266 DEDUCE_EXACT:
18267 We are deducing arguments when doing an explicit instantiation
18268 as in [temp.explicit], when determining an explicit specialization
18269 as in [temp.expl.spec], or when taking the address of a function
18270 template, as in [temp.deduct.funcaddr]. */
18271
18272 tree
18273 fn_type_unification (tree fn,
18274 tree explicit_targs,
18275 tree targs,
18276 const tree *args,
18277 unsigned int nargs,
18278 tree return_type,
18279 unification_kind_t strict,
18280 int flags,
18281 bool explain_p,
18282 bool decltype_p)
18283 {
18284 tree parms;
18285 tree fntype;
18286 tree decl = NULL_TREE;
18287 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18288 bool ok;
18289 static int deduction_depth;
18290 struct pending_template *old_last_pend = last_pending_template;
18291 struct tinst_level *old_error_tinst = last_error_tinst_level;
18292
18293 tree orig_fn = fn;
18294 if (flag_new_inheriting_ctors)
18295 fn = strip_inheriting_ctors (fn);
18296
18297 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18298 tree tinst;
18299 tree r = error_mark_node;
18300
18301 tree full_targs = targs;
18302 if (TMPL_ARGS_DEPTH (targs)
18303 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18304 full_targs = (add_outermost_template_args
18305 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18306 targs));
18307
18308 if (decltype_p)
18309 complain |= tf_decltype;
18310
18311 /* In C++0x, it's possible to have a function template whose type depends
18312 on itself recursively. This is most obvious with decltype, but can also
18313 occur with enumeration scope (c++/48969). So we need to catch infinite
18314 recursion and reject the substitution at deduction time; this function
18315 will return error_mark_node for any repeated substitution.
18316
18317 This also catches excessive recursion such as when f<N> depends on
18318 f<N-1> across all integers, and returns error_mark_node for all the
18319 substitutions back up to the initial one.
18320
18321 This is, of course, not reentrant. */
18322 if (excessive_deduction_depth)
18323 return error_mark_node;
18324 tinst = build_tree_list (fn, NULL_TREE);
18325 ++deduction_depth;
18326
18327 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18328
18329 fntype = TREE_TYPE (fn);
18330 if (explicit_targs)
18331 {
18332 /* [temp.deduct]
18333
18334 The specified template arguments must match the template
18335 parameters in kind (i.e., type, nontype, template), and there
18336 must not be more arguments than there are parameters;
18337 otherwise type deduction fails.
18338
18339 Nontype arguments must match the types of the corresponding
18340 nontype template parameters, or must be convertible to the
18341 types of the corresponding nontype parameters as specified in
18342 _temp.arg.nontype_, otherwise type deduction fails.
18343
18344 All references in the function type of the function template
18345 to the corresponding template parameters are replaced by the
18346 specified template argument values. If a substitution in a
18347 template parameter or in the function type of the function
18348 template results in an invalid type, type deduction fails. */
18349 int i, len = TREE_VEC_LENGTH (tparms);
18350 location_t loc = input_location;
18351 bool incomplete = false;
18352
18353 if (explicit_targs == error_mark_node)
18354 goto fail;
18355
18356 if (TMPL_ARGS_DEPTH (explicit_targs)
18357 < TMPL_ARGS_DEPTH (full_targs))
18358 explicit_targs = add_outermost_template_args (full_targs,
18359 explicit_targs);
18360
18361 /* Adjust any explicit template arguments before entering the
18362 substitution context. */
18363 explicit_targs
18364 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18365 complain,
18366 /*require_all_args=*/false,
18367 /*use_default_args=*/false));
18368 if (explicit_targs == error_mark_node)
18369 goto fail;
18370
18371 /* Substitute the explicit args into the function type. This is
18372 necessary so that, for instance, explicitly declared function
18373 arguments can match null pointed constants. If we were given
18374 an incomplete set of explicit args, we must not do semantic
18375 processing during substitution as we could create partial
18376 instantiations. */
18377 for (i = 0; i < len; i++)
18378 {
18379 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18380 bool parameter_pack = false;
18381 tree targ = TREE_VEC_ELT (explicit_targs, i);
18382
18383 /* Dig out the actual parm. */
18384 if (TREE_CODE (parm) == TYPE_DECL
18385 || TREE_CODE (parm) == TEMPLATE_DECL)
18386 {
18387 parm = TREE_TYPE (parm);
18388 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18389 }
18390 else if (TREE_CODE (parm) == PARM_DECL)
18391 {
18392 parm = DECL_INITIAL (parm);
18393 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18394 }
18395
18396 if (!parameter_pack && targ == NULL_TREE)
18397 /* No explicit argument for this template parameter. */
18398 incomplete = true;
18399
18400 if (parameter_pack && pack_deducible_p (parm, fn))
18401 {
18402 /* Mark the argument pack as "incomplete". We could
18403 still deduce more arguments during unification.
18404 We remove this mark in type_unification_real. */
18405 if (targ)
18406 {
18407 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18408 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18409 = ARGUMENT_PACK_ARGS (targ);
18410 }
18411
18412 /* We have some incomplete argument packs. */
18413 incomplete = true;
18414 }
18415 }
18416
18417 TREE_VALUE (tinst) = explicit_targs;
18418 if (!push_tinst_level (tinst))
18419 {
18420 excessive_deduction_depth = true;
18421 goto fail;
18422 }
18423 processing_template_decl += incomplete;
18424 input_location = DECL_SOURCE_LOCATION (fn);
18425 /* Ignore any access checks; we'll see them again in
18426 instantiate_template and they might have the wrong
18427 access path at this point. */
18428 push_deferring_access_checks (dk_deferred);
18429 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18430 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18431 pop_deferring_access_checks ();
18432 input_location = loc;
18433 processing_template_decl -= incomplete;
18434 pop_tinst_level ();
18435
18436 if (fntype == error_mark_node)
18437 goto fail;
18438
18439 /* Place the explicitly specified arguments in TARGS. */
18440 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18441 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18442 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18443 }
18444
18445 /* Never do unification on the 'this' parameter. */
18446 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18447
18448 if (return_type && strict == DEDUCE_CALL)
18449 {
18450 /* We're deducing for a call to the result of a template conversion
18451 function. The parms we really want are in return_type. */
18452 if (POINTER_TYPE_P (return_type))
18453 return_type = TREE_TYPE (return_type);
18454 parms = TYPE_ARG_TYPES (return_type);
18455 }
18456 else if (return_type)
18457 {
18458 tree *new_args;
18459
18460 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18461 new_args = XALLOCAVEC (tree, nargs + 1);
18462 new_args[0] = return_type;
18463 memcpy (new_args + 1, args, nargs * sizeof (tree));
18464 args = new_args;
18465 ++nargs;
18466 }
18467
18468 /* We allow incomplete unification without an error message here
18469 because the standard doesn't seem to explicitly prohibit it. Our
18470 callers must be ready to deal with unification failures in any
18471 event. */
18472
18473 TREE_VALUE (tinst) = targs;
18474 /* If we aren't explaining yet, push tinst context so we can see where
18475 any errors (e.g. from class instantiations triggered by instantiation
18476 of default template arguments) come from. If we are explaining, this
18477 context is redundant. */
18478 if (!explain_p && !push_tinst_level (tinst))
18479 {
18480 excessive_deduction_depth = true;
18481 goto fail;
18482 }
18483
18484 /* type_unification_real will pass back any access checks from default
18485 template argument substitution. */
18486 vec<deferred_access_check, va_gc> *checks;
18487 checks = NULL;
18488
18489 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18490 full_targs, parms, args, nargs, /*subr=*/0,
18491 strict, flags, &checks, explain_p);
18492 if (!explain_p)
18493 pop_tinst_level ();
18494 if (!ok)
18495 goto fail;
18496
18497 /* Now that we have bindings for all of the template arguments,
18498 ensure that the arguments deduced for the template template
18499 parameters have compatible template parameter lists. We cannot
18500 check this property before we have deduced all template
18501 arguments, because the template parameter types of a template
18502 template parameter might depend on prior template parameters
18503 deduced after the template template parameter. The following
18504 ill-formed example illustrates this issue:
18505
18506 template<typename T, template<T> class C> void f(C<5>, T);
18507
18508 template<int N> struct X {};
18509
18510 void g() {
18511 f(X<5>(), 5l); // error: template argument deduction fails
18512 }
18513
18514 The template parameter list of 'C' depends on the template type
18515 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18516 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18517 time that we deduce 'C'. */
18518 if (!template_template_parm_bindings_ok_p
18519 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18520 {
18521 unify_inconsistent_template_template_parameters (explain_p);
18522 goto fail;
18523 }
18524
18525 /* All is well so far. Now, check:
18526
18527 [temp.deduct]
18528
18529 When all template arguments have been deduced, all uses of
18530 template parameters in nondeduced contexts are replaced with
18531 the corresponding deduced argument values. If the
18532 substitution results in an invalid type, as described above,
18533 type deduction fails. */
18534 TREE_VALUE (tinst) = targs;
18535 if (!push_tinst_level (tinst))
18536 {
18537 excessive_deduction_depth = true;
18538 goto fail;
18539 }
18540
18541 /* Also collect access checks from the instantiation. */
18542 reopen_deferring_access_checks (checks);
18543
18544 decl = instantiate_template (fn, targs, complain);
18545
18546 checks = get_deferred_access_checks ();
18547 pop_deferring_access_checks ();
18548
18549 pop_tinst_level ();
18550
18551 if (decl == error_mark_node)
18552 goto fail;
18553
18554 /* Now perform any access checks encountered during substitution. */
18555 push_access_scope (decl);
18556 ok = perform_access_checks (checks, complain);
18557 pop_access_scope (decl);
18558 if (!ok)
18559 goto fail;
18560
18561 /* If we're looking for an exact match, check that what we got
18562 is indeed an exact match. It might not be if some template
18563 parameters are used in non-deduced contexts. But don't check
18564 for an exact match if we have dependent template arguments;
18565 in that case we're doing partial ordering, and we already know
18566 that we have two candidates that will provide the actual type. */
18567 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18568 {
18569 tree substed = TREE_TYPE (decl);
18570 unsigned int i;
18571
18572 tree sarg
18573 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18574 if (return_type)
18575 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18576 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18577 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18578 {
18579 unify_type_mismatch (explain_p, args[i],
18580 TREE_VALUE (sarg));
18581 goto fail;
18582 }
18583 }
18584
18585 /* After doing deduction with the inherited constructor, actually return an
18586 instantiation of the inheriting constructor. */
18587 if (orig_fn != fn)
18588 decl = instantiate_template (orig_fn, targs, complain);
18589
18590 r = decl;
18591
18592 fail:
18593 --deduction_depth;
18594 if (excessive_deduction_depth)
18595 {
18596 if (deduction_depth == 0)
18597 /* Reset once we're all the way out. */
18598 excessive_deduction_depth = false;
18599 }
18600
18601 /* We can't free this if a pending_template entry or last_error_tinst_level
18602 is pointing at it. */
18603 if (last_pending_template == old_last_pend
18604 && last_error_tinst_level == old_error_tinst)
18605 ggc_free (tinst);
18606
18607 return r;
18608 }
18609
18610 /* Adjust types before performing type deduction, as described in
18611 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18612 sections are symmetric. PARM is the type of a function parameter
18613 or the return type of the conversion function. ARG is the type of
18614 the argument passed to the call, or the type of the value
18615 initialized with the result of the conversion function.
18616 ARG_EXPR is the original argument expression, which may be null. */
18617
18618 static int
18619 maybe_adjust_types_for_deduction (unification_kind_t strict,
18620 tree* parm,
18621 tree* arg,
18622 tree arg_expr)
18623 {
18624 int result = 0;
18625
18626 switch (strict)
18627 {
18628 case DEDUCE_CALL:
18629 break;
18630
18631 case DEDUCE_CONV:
18632 /* Swap PARM and ARG throughout the remainder of this
18633 function; the handling is precisely symmetric since PARM
18634 will initialize ARG rather than vice versa. */
18635 std::swap (parm, arg);
18636 break;
18637
18638 case DEDUCE_EXACT:
18639 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18640 too, but here handle it by stripping the reference from PARM
18641 rather than by adding it to ARG. */
18642 if (TREE_CODE (*parm) == REFERENCE_TYPE
18643 && TYPE_REF_IS_RVALUE (*parm)
18644 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18645 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18646 && TREE_CODE (*arg) == REFERENCE_TYPE
18647 && !TYPE_REF_IS_RVALUE (*arg))
18648 *parm = TREE_TYPE (*parm);
18649 /* Nothing else to do in this case. */
18650 return 0;
18651
18652 default:
18653 gcc_unreachable ();
18654 }
18655
18656 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18657 {
18658 /* [temp.deduct.call]
18659
18660 If P is not a reference type:
18661
18662 --If A is an array type, the pointer type produced by the
18663 array-to-pointer standard conversion (_conv.array_) is
18664 used in place of A for type deduction; otherwise,
18665
18666 --If A is a function type, the pointer type produced by
18667 the function-to-pointer standard conversion
18668 (_conv.func_) is used in place of A for type deduction;
18669 otherwise,
18670
18671 --If A is a cv-qualified type, the top level
18672 cv-qualifiers of A's type are ignored for type
18673 deduction. */
18674 if (TREE_CODE (*arg) == ARRAY_TYPE)
18675 *arg = build_pointer_type (TREE_TYPE (*arg));
18676 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18677 *arg = build_pointer_type (*arg);
18678 else
18679 *arg = TYPE_MAIN_VARIANT (*arg);
18680 }
18681
18682 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18683 reference to a cv-unqualified template parameter that does not represent a
18684 template parameter of a class template (during class template argument
18685 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18686 an lvalue, the type "lvalue reference to A" is used in place of A for type
18687 deduction. */
18688 if (TREE_CODE (*parm) == REFERENCE_TYPE
18689 && TYPE_REF_IS_RVALUE (*parm)
18690 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18691 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18692 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18693 && (arg_expr ? lvalue_p (arg_expr)
18694 /* try_one_overload doesn't provide an arg_expr, but
18695 functions are always lvalues. */
18696 : TREE_CODE (*arg) == FUNCTION_TYPE))
18697 *arg = build_reference_type (*arg);
18698
18699 /* [temp.deduct.call]
18700
18701 If P is a cv-qualified type, the top level cv-qualifiers
18702 of P's type are ignored for type deduction. If P is a
18703 reference type, the type referred to by P is used for
18704 type deduction. */
18705 *parm = TYPE_MAIN_VARIANT (*parm);
18706 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18707 {
18708 *parm = TREE_TYPE (*parm);
18709 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18710 }
18711
18712 /* DR 322. For conversion deduction, remove a reference type on parm
18713 too (which has been swapped into ARG). */
18714 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18715 *arg = TREE_TYPE (*arg);
18716
18717 return result;
18718 }
18719
18720 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18721 template which does contain any deducible template parameters; check if
18722 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18723 unify_one_argument. */
18724
18725 static int
18726 check_non_deducible_conversion (tree parm, tree arg, int strict,
18727 int flags, bool explain_p)
18728 {
18729 tree type;
18730
18731 if (!TYPE_P (arg))
18732 type = TREE_TYPE (arg);
18733 else
18734 type = arg;
18735
18736 if (same_type_p (parm, type))
18737 return unify_success (explain_p);
18738
18739 if (strict == DEDUCE_CONV)
18740 {
18741 if (can_convert_arg (type, parm, NULL_TREE, flags,
18742 explain_p ? tf_warning_or_error : tf_none))
18743 return unify_success (explain_p);
18744 }
18745 else if (strict != DEDUCE_EXACT)
18746 {
18747 if (can_convert_arg (parm, type,
18748 TYPE_P (arg) ? NULL_TREE : arg,
18749 flags, explain_p ? tf_warning_or_error : tf_none))
18750 return unify_success (explain_p);
18751 }
18752
18753 if (strict == DEDUCE_EXACT)
18754 return unify_type_mismatch (explain_p, parm, arg);
18755 else
18756 return unify_arg_conversion (explain_p, parm, type, arg);
18757 }
18758
18759 static bool uses_deducible_template_parms (tree type);
18760
18761 /* Returns true iff the expression EXPR is one from which a template
18762 argument can be deduced. In other words, if it's an undecorated
18763 use of a template non-type parameter. */
18764
18765 static bool
18766 deducible_expression (tree expr)
18767 {
18768 /* Strip implicit conversions. */
18769 while (CONVERT_EXPR_P (expr))
18770 expr = TREE_OPERAND (expr, 0);
18771 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18772 }
18773
18774 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18775 deducible way; that is, if it has a max value of <PARM> - 1. */
18776
18777 static bool
18778 deducible_array_bound (tree domain)
18779 {
18780 if (domain == NULL_TREE)
18781 return false;
18782
18783 tree max = TYPE_MAX_VALUE (domain);
18784 if (TREE_CODE (max) != MINUS_EXPR)
18785 return false;
18786
18787 return deducible_expression (TREE_OPERAND (max, 0));
18788 }
18789
18790 /* Returns true iff the template arguments ARGS use a template parameter
18791 in a deducible way. */
18792
18793 static bool
18794 deducible_template_args (tree args)
18795 {
18796 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18797 {
18798 bool deducible;
18799 tree elt = TREE_VEC_ELT (args, i);
18800 if (ARGUMENT_PACK_P (elt))
18801 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18802 else
18803 {
18804 if (PACK_EXPANSION_P (elt))
18805 elt = PACK_EXPANSION_PATTERN (elt);
18806 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18807 deducible = true;
18808 else if (TYPE_P (elt))
18809 deducible = uses_deducible_template_parms (elt);
18810 else
18811 deducible = deducible_expression (elt);
18812 }
18813 if (deducible)
18814 return true;
18815 }
18816 return false;
18817 }
18818
18819 /* Returns true iff TYPE contains any deducible references to template
18820 parameters, as per 14.8.2.5. */
18821
18822 static bool
18823 uses_deducible_template_parms (tree type)
18824 {
18825 if (PACK_EXPANSION_P (type))
18826 type = PACK_EXPANSION_PATTERN (type);
18827
18828 /* T
18829 cv-list T
18830 TT<T>
18831 TT<i>
18832 TT<> */
18833 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18834 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18835 return true;
18836
18837 /* T*
18838 T&
18839 T&& */
18840 if (POINTER_TYPE_P (type))
18841 return uses_deducible_template_parms (TREE_TYPE (type));
18842
18843 /* T[integer-constant ]
18844 type [i] */
18845 if (TREE_CODE (type) == ARRAY_TYPE)
18846 return (uses_deducible_template_parms (TREE_TYPE (type))
18847 || deducible_array_bound (TYPE_DOMAIN (type)));
18848
18849 /* T type ::*
18850 type T::*
18851 T T::*
18852 T (type ::*)()
18853 type (T::*)()
18854 type (type ::*)(T)
18855 type (T::*)(T)
18856 T (type ::*)(T)
18857 T (T::*)()
18858 T (T::*)(T) */
18859 if (TYPE_PTRMEM_P (type))
18860 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18861 || (uses_deducible_template_parms
18862 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18863
18864 /* template-name <T> (where template-name refers to a class template)
18865 template-name <i> (where template-name refers to a class template) */
18866 if (CLASS_TYPE_P (type)
18867 && CLASSTYPE_TEMPLATE_INFO (type)
18868 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18869 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18870 (CLASSTYPE_TI_ARGS (type)));
18871
18872 /* type (T)
18873 T()
18874 T(T) */
18875 if (TREE_CODE (type) == FUNCTION_TYPE
18876 || TREE_CODE (type) == METHOD_TYPE)
18877 {
18878 if (uses_deducible_template_parms (TREE_TYPE (type)))
18879 return true;
18880 tree parm = TYPE_ARG_TYPES (type);
18881 if (TREE_CODE (type) == METHOD_TYPE)
18882 parm = TREE_CHAIN (parm);
18883 for (; parm; parm = TREE_CHAIN (parm))
18884 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18885 return true;
18886 }
18887
18888 return false;
18889 }
18890
18891 /* Subroutine of type_unification_real and unify_pack_expansion to
18892 handle unification of a single P/A pair. Parameters are as
18893 for those functions. */
18894
18895 static int
18896 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18897 int subr, unification_kind_t strict,
18898 bool explain_p)
18899 {
18900 tree arg_expr = NULL_TREE;
18901 int arg_strict;
18902
18903 if (arg == error_mark_node || parm == error_mark_node)
18904 return unify_invalid (explain_p);
18905 if (arg == unknown_type_node)
18906 /* We can't deduce anything from this, but we might get all the
18907 template args from other function args. */
18908 return unify_success (explain_p);
18909
18910 /* Implicit conversions (Clause 4) will be performed on a function
18911 argument to convert it to the type of the corresponding function
18912 parameter if the parameter type contains no template-parameters that
18913 participate in template argument deduction. */
18914 if (strict != DEDUCE_EXACT
18915 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18916 /* For function parameters with no deducible template parameters,
18917 just return. We'll check non-dependent conversions later. */
18918 return unify_success (explain_p);
18919
18920 switch (strict)
18921 {
18922 case DEDUCE_CALL:
18923 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18924 | UNIFY_ALLOW_MORE_CV_QUAL
18925 | UNIFY_ALLOW_DERIVED);
18926 break;
18927
18928 case DEDUCE_CONV:
18929 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18930 break;
18931
18932 case DEDUCE_EXACT:
18933 arg_strict = UNIFY_ALLOW_NONE;
18934 break;
18935
18936 default:
18937 gcc_unreachable ();
18938 }
18939
18940 /* We only do these transformations if this is the top-level
18941 parameter_type_list in a call or declaration matching; in other
18942 situations (nested function declarators, template argument lists) we
18943 won't be comparing a type to an expression, and we don't do any type
18944 adjustments. */
18945 if (!subr)
18946 {
18947 if (!TYPE_P (arg))
18948 {
18949 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18950 if (type_unknown_p (arg))
18951 {
18952 /* [temp.deduct.type] A template-argument can be
18953 deduced from a pointer to function or pointer
18954 to member function argument if the set of
18955 overloaded functions does not contain function
18956 templates and at most one of a set of
18957 overloaded functions provides a unique
18958 match. */
18959
18960 if (resolve_overloaded_unification
18961 (tparms, targs, parm, arg, strict,
18962 arg_strict, explain_p))
18963 return unify_success (explain_p);
18964 return unify_overload_resolution_failure (explain_p, arg);
18965 }
18966
18967 arg_expr = arg;
18968 arg = unlowered_expr_type (arg);
18969 if (arg == error_mark_node)
18970 return unify_invalid (explain_p);
18971 }
18972
18973 arg_strict |=
18974 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18975 }
18976 else
18977 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18978 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18979 return unify_template_argument_mismatch (explain_p, parm, arg);
18980
18981 /* For deduction from an init-list we need the actual list. */
18982 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18983 arg = arg_expr;
18984 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18985 }
18986
18987 /* for_each_template_parm callback that always returns 0. */
18988
18989 static int
18990 zero_r (tree, void *)
18991 {
18992 return 0;
18993 }
18994
18995 /* for_each_template_parm any_fn callback to handle deduction of a template
18996 type argument from the type of an array bound. */
18997
18998 static int
18999 array_deduction_r (tree t, void *data)
19000 {
19001 tree_pair_p d = (tree_pair_p)data;
19002 tree &tparms = d->purpose;
19003 tree &targs = d->value;
19004
19005 if (TREE_CODE (t) == ARRAY_TYPE)
19006 if (tree dom = TYPE_DOMAIN (t))
19007 if (tree max = TYPE_MAX_VALUE (dom))
19008 {
19009 if (TREE_CODE (max) == MINUS_EXPR)
19010 max = TREE_OPERAND (max, 0);
19011 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19012 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19013 UNIFY_ALLOW_NONE, /*explain*/false);
19014 }
19015
19016 /* Keep walking. */
19017 return 0;
19018 }
19019
19020 /* Try to deduce any not-yet-deduced template type arguments from the type of
19021 an array bound. This is handled separately from unify because 14.8.2.5 says
19022 "The type of a type parameter is only deduced from an array bound if it is
19023 not otherwise deduced." */
19024
19025 static void
19026 try_array_deduction (tree tparms, tree targs, tree parm)
19027 {
19028 tree_pair_s data = { tparms, targs };
19029 hash_set<tree> visited;
19030 for_each_template_parm (parm, zero_r, &data, &visited,
19031 /*nondeduced*/false, array_deduction_r);
19032 }
19033
19034 /* Most parms like fn_type_unification.
19035
19036 If SUBR is 1, we're being called recursively (to unify the
19037 arguments of a function or method parameter of a function
19038 template).
19039
19040 CHECKS is a pointer to a vector of access checks encountered while
19041 substituting default template arguments. */
19042
19043 static int
19044 type_unification_real (tree tparms,
19045 tree full_targs,
19046 tree xparms,
19047 const tree *xargs,
19048 unsigned int xnargs,
19049 int subr,
19050 unification_kind_t strict,
19051 int flags,
19052 vec<deferred_access_check, va_gc> **checks,
19053 bool explain_p)
19054 {
19055 tree parm, arg;
19056 int i;
19057 int ntparms = TREE_VEC_LENGTH (tparms);
19058 int saw_undeduced = 0;
19059 tree parms;
19060 const tree *args;
19061 unsigned int nargs;
19062 unsigned int ia;
19063
19064 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19065 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19066 gcc_assert (ntparms > 0);
19067
19068 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19069
19070 /* Reset the number of non-defaulted template arguments contained
19071 in TARGS. */
19072 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19073
19074 again:
19075 parms = xparms;
19076 args = xargs;
19077 nargs = xnargs;
19078
19079 ia = 0;
19080 while (parms && parms != void_list_node
19081 && ia < nargs)
19082 {
19083 parm = TREE_VALUE (parms);
19084
19085 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19086 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19087 /* For a function parameter pack that occurs at the end of the
19088 parameter-declaration-list, the type A of each remaining
19089 argument of the call is compared with the type P of the
19090 declarator-id of the function parameter pack. */
19091 break;
19092
19093 parms = TREE_CHAIN (parms);
19094
19095 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19096 /* For a function parameter pack that does not occur at the
19097 end of the parameter-declaration-list, the type of the
19098 parameter pack is a non-deduced context. */
19099 continue;
19100
19101 arg = args[ia];
19102 ++ia;
19103
19104 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19105 explain_p))
19106 return 1;
19107 }
19108
19109 if (parms
19110 && parms != void_list_node
19111 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19112 {
19113 /* Unify the remaining arguments with the pack expansion type. */
19114 tree argvec;
19115 tree parmvec = make_tree_vec (1);
19116
19117 /* Allocate a TREE_VEC and copy in all of the arguments */
19118 argvec = make_tree_vec (nargs - ia);
19119 for (i = 0; ia < nargs; ++ia, ++i)
19120 TREE_VEC_ELT (argvec, i) = args[ia];
19121
19122 /* Copy the parameter into parmvec. */
19123 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19124 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19125 /*subr=*/subr, explain_p))
19126 return 1;
19127
19128 /* Advance to the end of the list of parameters. */
19129 parms = TREE_CHAIN (parms);
19130 }
19131
19132 /* Fail if we've reached the end of the parm list, and more args
19133 are present, and the parm list isn't variadic. */
19134 if (ia < nargs && parms == void_list_node)
19135 return unify_too_many_arguments (explain_p, nargs, ia);
19136 /* Fail if parms are left and they don't have default values and
19137 they aren't all deduced as empty packs (c++/57397). This is
19138 consistent with sufficient_parms_p. */
19139 if (parms && parms != void_list_node
19140 && TREE_PURPOSE (parms) == NULL_TREE)
19141 {
19142 unsigned int count = nargs;
19143 tree p = parms;
19144 bool type_pack_p;
19145 do
19146 {
19147 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19148 if (!type_pack_p)
19149 count++;
19150 p = TREE_CHAIN (p);
19151 }
19152 while (p && p != void_list_node);
19153 if (count != nargs)
19154 return unify_too_few_arguments (explain_p, ia, count,
19155 type_pack_p);
19156 }
19157
19158 if (!subr)
19159 {
19160 tsubst_flags_t complain = (explain_p
19161 ? tf_warning_or_error
19162 : tf_none);
19163 bool tried_array_deduction = (cxx_dialect < cxx1z);
19164
19165 for (i = 0; i < ntparms; i++)
19166 {
19167 tree targ = TREE_VEC_ELT (targs, i);
19168 tree tparm = TREE_VEC_ELT (tparms, i);
19169
19170 /* Clear the "incomplete" flags on all argument packs now so that
19171 substituting them into later default arguments works. */
19172 if (targ && ARGUMENT_PACK_P (targ))
19173 {
19174 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19175 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19176 }
19177
19178 if (targ || tparm == error_mark_node)
19179 continue;
19180 tparm = TREE_VALUE (tparm);
19181
19182 if (TREE_CODE (tparm) == TYPE_DECL
19183 && !tried_array_deduction)
19184 {
19185 try_array_deduction (tparms, targs, xparms);
19186 tried_array_deduction = true;
19187 if (TREE_VEC_ELT (targs, i))
19188 continue;
19189 }
19190
19191 /* If this is an undeduced nontype parameter that depends on
19192 a type parameter, try another pass; its type may have been
19193 deduced from a later argument than the one from which
19194 this parameter can be deduced. */
19195 if (TREE_CODE (tparm) == PARM_DECL
19196 && uses_template_parms (TREE_TYPE (tparm))
19197 && saw_undeduced < 2)
19198 {
19199 saw_undeduced = 1;
19200 continue;
19201 }
19202
19203 /* Core issue #226 (C++0x) [temp.deduct]:
19204
19205 If a template argument has not been deduced, its
19206 default template argument, if any, is used.
19207
19208 When we are in C++98 mode, TREE_PURPOSE will either
19209 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19210 to explicitly check cxx_dialect here. */
19211 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19212 /* OK, there is a default argument. Wait until after the
19213 conversion check to do substitution. */
19214 continue;
19215
19216 /* If the type parameter is a parameter pack, then it will
19217 be deduced to an empty parameter pack. */
19218 if (template_parameter_pack_p (tparm))
19219 {
19220 tree arg;
19221
19222 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19223 {
19224 arg = make_node (NONTYPE_ARGUMENT_PACK);
19225 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
19226 TREE_CONSTANT (arg) = 1;
19227 }
19228 else
19229 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19230
19231 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19232
19233 TREE_VEC_ELT (targs, i) = arg;
19234 continue;
19235 }
19236
19237 return unify_parameter_deduction_failure (explain_p, tparm);
19238 }
19239
19240 /* DR 1391: All parameters have args, now check non-dependent parms for
19241 convertibility. */
19242 if (saw_undeduced < 2)
19243 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19244 parms && parms != void_list_node && ia < nargs; )
19245 {
19246 parm = TREE_VALUE (parms);
19247
19248 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19249 && (!TREE_CHAIN (parms)
19250 || TREE_CHAIN (parms) == void_list_node))
19251 /* For a function parameter pack that occurs at the end of the
19252 parameter-declaration-list, the type A of each remaining
19253 argument of the call is compared with the type P of the
19254 declarator-id of the function parameter pack. */
19255 break;
19256
19257 parms = TREE_CHAIN (parms);
19258
19259 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19260 /* For a function parameter pack that does not occur at the
19261 end of the parameter-declaration-list, the type of the
19262 parameter pack is a non-deduced context. */
19263 continue;
19264
19265 arg = args[ia];
19266 ++ia;
19267
19268 if (uses_template_parms (parm))
19269 continue;
19270 if (check_non_deducible_conversion (parm, arg, strict, flags,
19271 explain_p))
19272 return 1;
19273 }
19274
19275 /* Now substitute into the default template arguments. */
19276 for (i = 0; i < ntparms; i++)
19277 {
19278 tree targ = TREE_VEC_ELT (targs, i);
19279 tree tparm = TREE_VEC_ELT (tparms, i);
19280
19281 if (targ || tparm == error_mark_node)
19282 continue;
19283 tree parm = TREE_VALUE (tparm);
19284
19285 if (TREE_CODE (parm) == PARM_DECL
19286 && uses_template_parms (TREE_TYPE (parm))
19287 && saw_undeduced < 2)
19288 continue;
19289
19290 tree arg = TREE_PURPOSE (tparm);
19291 reopen_deferring_access_checks (*checks);
19292 location_t save_loc = input_location;
19293 if (DECL_P (parm))
19294 input_location = DECL_SOURCE_LOCATION (parm);
19295 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19296 if (!uses_template_parms (arg))
19297 arg = convert_template_argument (parm, arg, full_targs, complain,
19298 i, NULL_TREE);
19299 else if (saw_undeduced < 2)
19300 arg = NULL_TREE;
19301 else
19302 arg = error_mark_node;
19303 input_location = save_loc;
19304 *checks = get_deferred_access_checks ();
19305 pop_deferring_access_checks ();
19306 if (arg == error_mark_node)
19307 return 1;
19308 else if (arg)
19309 {
19310 TREE_VEC_ELT (targs, i) = arg;
19311 /* The position of the first default template argument,
19312 is also the number of non-defaulted arguments in TARGS.
19313 Record that. */
19314 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19315 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19316 }
19317 }
19318
19319 if (saw_undeduced++ == 1)
19320 goto again;
19321 }
19322
19323 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19324 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19325
19326 return unify_success (explain_p);
19327 }
19328
19329 /* Subroutine of type_unification_real. Args are like the variables
19330 at the call site. ARG is an overloaded function (or template-id);
19331 we try deducing template args from each of the overloads, and if
19332 only one succeeds, we go with that. Modifies TARGS and returns
19333 true on success. */
19334
19335 static bool
19336 resolve_overloaded_unification (tree tparms,
19337 tree targs,
19338 tree parm,
19339 tree arg,
19340 unification_kind_t strict,
19341 int sub_strict,
19342 bool explain_p)
19343 {
19344 tree tempargs = copy_node (targs);
19345 int good = 0;
19346 tree goodfn = NULL_TREE;
19347 bool addr_p;
19348
19349 if (TREE_CODE (arg) == ADDR_EXPR)
19350 {
19351 arg = TREE_OPERAND (arg, 0);
19352 addr_p = true;
19353 }
19354 else
19355 addr_p = false;
19356
19357 if (TREE_CODE (arg) == COMPONENT_REF)
19358 /* Handle `&x' where `x' is some static or non-static member
19359 function name. */
19360 arg = TREE_OPERAND (arg, 1);
19361
19362 if (TREE_CODE (arg) == OFFSET_REF)
19363 arg = TREE_OPERAND (arg, 1);
19364
19365 /* Strip baselink information. */
19366 if (BASELINK_P (arg))
19367 arg = BASELINK_FUNCTIONS (arg);
19368
19369 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19370 {
19371 /* If we got some explicit template args, we need to plug them into
19372 the affected templates before we try to unify, in case the
19373 explicit args will completely resolve the templates in question. */
19374
19375 int ok = 0;
19376 tree expl_subargs = TREE_OPERAND (arg, 1);
19377 arg = TREE_OPERAND (arg, 0);
19378
19379 for (; arg; arg = OVL_NEXT (arg))
19380 {
19381 tree fn = OVL_CURRENT (arg);
19382 tree subargs, elem;
19383
19384 if (TREE_CODE (fn) != TEMPLATE_DECL)
19385 continue;
19386
19387 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19388 expl_subargs, NULL_TREE, tf_none,
19389 /*require_all_args=*/true,
19390 /*use_default_args=*/true);
19391 if (subargs != error_mark_node
19392 && !any_dependent_template_arguments_p (subargs))
19393 {
19394 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19395 if (try_one_overload (tparms, targs, tempargs, parm,
19396 elem, strict, sub_strict, addr_p, explain_p)
19397 && (!goodfn || !same_type_p (goodfn, elem)))
19398 {
19399 goodfn = elem;
19400 ++good;
19401 }
19402 }
19403 else if (subargs)
19404 ++ok;
19405 }
19406 /* If no templates (or more than one) are fully resolved by the
19407 explicit arguments, this template-id is a non-deduced context; it
19408 could still be OK if we deduce all template arguments for the
19409 enclosing call through other arguments. */
19410 if (good != 1)
19411 good = ok;
19412 }
19413 else if (TREE_CODE (arg) != OVERLOAD
19414 && TREE_CODE (arg) != FUNCTION_DECL)
19415 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19416 -- but the deduction does not succeed because the expression is
19417 not just the function on its own. */
19418 return false;
19419 else
19420 for (; arg; arg = OVL_NEXT (arg))
19421 if (try_one_overload (tparms, targs, tempargs, parm,
19422 TREE_TYPE (OVL_CURRENT (arg)),
19423 strict, sub_strict, addr_p, explain_p)
19424 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
19425 {
19426 goodfn = OVL_CURRENT (arg);
19427 ++good;
19428 }
19429
19430 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19431 to function or pointer to member function argument if the set of
19432 overloaded functions does not contain function templates and at most
19433 one of a set of overloaded functions provides a unique match.
19434
19435 So if we found multiple possibilities, we return success but don't
19436 deduce anything. */
19437
19438 if (good == 1)
19439 {
19440 int i = TREE_VEC_LENGTH (targs);
19441 for (; i--; )
19442 if (TREE_VEC_ELT (tempargs, i))
19443 {
19444 tree old = TREE_VEC_ELT (targs, i);
19445 tree new_ = TREE_VEC_ELT (tempargs, i);
19446 if (new_ && old && ARGUMENT_PACK_P (old)
19447 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19448 /* Don't forget explicit template arguments in a pack. */
19449 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19450 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19451 TREE_VEC_ELT (targs, i) = new_;
19452 }
19453 }
19454 if (good)
19455 return true;
19456
19457 return false;
19458 }
19459
19460 /* Core DR 115: In contexts where deduction is done and fails, or in
19461 contexts where deduction is not done, if a template argument list is
19462 specified and it, along with any default template arguments, identifies
19463 a single function template specialization, then the template-id is an
19464 lvalue for the function template specialization. */
19465
19466 tree
19467 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19468 {
19469 tree expr, offset, baselink;
19470 bool addr;
19471
19472 if (!type_unknown_p (orig_expr))
19473 return orig_expr;
19474
19475 expr = orig_expr;
19476 addr = false;
19477 offset = NULL_TREE;
19478 baselink = NULL_TREE;
19479
19480 if (TREE_CODE (expr) == ADDR_EXPR)
19481 {
19482 expr = TREE_OPERAND (expr, 0);
19483 addr = true;
19484 }
19485 if (TREE_CODE (expr) == OFFSET_REF)
19486 {
19487 offset = expr;
19488 expr = TREE_OPERAND (expr, 1);
19489 }
19490 if (BASELINK_P (expr))
19491 {
19492 baselink = expr;
19493 expr = BASELINK_FUNCTIONS (expr);
19494 }
19495
19496 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19497 {
19498 int good = 0;
19499 tree goodfn = NULL_TREE;
19500
19501 /* If we got some explicit template args, we need to plug them into
19502 the affected templates before we try to unify, in case the
19503 explicit args will completely resolve the templates in question. */
19504
19505 tree expl_subargs = TREE_OPERAND (expr, 1);
19506 tree arg = TREE_OPERAND (expr, 0);
19507 tree badfn = NULL_TREE;
19508 tree badargs = NULL_TREE;
19509
19510 for (; arg; arg = OVL_NEXT (arg))
19511 {
19512 tree fn = OVL_CURRENT (arg);
19513 tree subargs, elem;
19514
19515 if (TREE_CODE (fn) != TEMPLATE_DECL)
19516 continue;
19517
19518 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19519 expl_subargs, NULL_TREE, tf_none,
19520 /*require_all_args=*/true,
19521 /*use_default_args=*/true);
19522 if (subargs != error_mark_node
19523 && !any_dependent_template_arguments_p (subargs))
19524 {
19525 elem = instantiate_template (fn, subargs, tf_none);
19526 if (elem == error_mark_node)
19527 {
19528 badfn = fn;
19529 badargs = subargs;
19530 }
19531 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19532 {
19533 goodfn = elem;
19534 ++good;
19535 }
19536 }
19537 }
19538 if (good == 1)
19539 {
19540 mark_used (goodfn);
19541 expr = goodfn;
19542 if (baselink)
19543 expr = build_baselink (BASELINK_BINFO (baselink),
19544 BASELINK_ACCESS_BINFO (baselink),
19545 expr, BASELINK_OPTYPE (baselink));
19546 if (offset)
19547 {
19548 tree base
19549 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19550 expr = build_offset_ref (base, expr, addr, complain);
19551 }
19552 if (addr)
19553 expr = cp_build_addr_expr (expr, complain);
19554 return expr;
19555 }
19556 else if (good == 0 && badargs && (complain & tf_error))
19557 /* There were no good options and at least one bad one, so let the
19558 user know what the problem is. */
19559 instantiate_template (badfn, badargs, complain);
19560 }
19561 return orig_expr;
19562 }
19563
19564 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19565 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19566 different overloads deduce different arguments for a given parm.
19567 ADDR_P is true if the expression for which deduction is being
19568 performed was of the form "& fn" rather than simply "fn".
19569
19570 Returns 1 on success. */
19571
19572 static int
19573 try_one_overload (tree tparms,
19574 tree orig_targs,
19575 tree targs,
19576 tree parm,
19577 tree arg,
19578 unification_kind_t strict,
19579 int sub_strict,
19580 bool addr_p,
19581 bool explain_p)
19582 {
19583 int nargs;
19584 tree tempargs;
19585 int i;
19586
19587 if (arg == error_mark_node)
19588 return 0;
19589
19590 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19591 to function or pointer to member function argument if the set of
19592 overloaded functions does not contain function templates and at most
19593 one of a set of overloaded functions provides a unique match.
19594
19595 So if this is a template, just return success. */
19596
19597 if (uses_template_parms (arg))
19598 return 1;
19599
19600 if (TREE_CODE (arg) == METHOD_TYPE)
19601 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19602 else if (addr_p)
19603 arg = build_pointer_type (arg);
19604
19605 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19606
19607 /* We don't copy orig_targs for this because if we have already deduced
19608 some template args from previous args, unify would complain when we
19609 try to deduce a template parameter for the same argument, even though
19610 there isn't really a conflict. */
19611 nargs = TREE_VEC_LENGTH (targs);
19612 tempargs = make_tree_vec (nargs);
19613
19614 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19615 return 0;
19616
19617 /* First make sure we didn't deduce anything that conflicts with
19618 explicitly specified args. */
19619 for (i = nargs; i--; )
19620 {
19621 tree elt = TREE_VEC_ELT (tempargs, i);
19622 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19623
19624 if (!elt)
19625 /*NOP*/;
19626 else if (uses_template_parms (elt))
19627 /* Since we're unifying against ourselves, we will fill in
19628 template args used in the function parm list with our own
19629 template parms. Discard them. */
19630 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19631 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19632 {
19633 /* Check that the argument at each index of the deduced argument pack
19634 is equivalent to the corresponding explicitly specified argument.
19635 We may have deduced more arguments than were explicitly specified,
19636 and that's OK. */
19637 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
19638 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
19639 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
19640
19641 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19642 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19643
19644 if (TREE_VEC_LENGTH (deduced_pack)
19645 < TREE_VEC_LENGTH (explicit_pack))
19646 return 0;
19647
19648 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19649 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19650 TREE_VEC_ELT (deduced_pack, j)))
19651 return 0;
19652 }
19653 else if (oldelt && !template_args_equal (oldelt, elt))
19654 return 0;
19655 }
19656
19657 for (i = nargs; i--; )
19658 {
19659 tree elt = TREE_VEC_ELT (tempargs, i);
19660
19661 if (elt)
19662 TREE_VEC_ELT (targs, i) = elt;
19663 }
19664
19665 return 1;
19666 }
19667
19668 /* PARM is a template class (perhaps with unbound template
19669 parameters). ARG is a fully instantiated type. If ARG can be
19670 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19671 TARGS are as for unify. */
19672
19673 static tree
19674 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19675 bool explain_p)
19676 {
19677 tree copy_of_targs;
19678
19679 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19680 return NULL_TREE;
19681 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19682 /* Matches anything. */;
19683 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19684 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19685 return NULL_TREE;
19686
19687 /* We need to make a new template argument vector for the call to
19688 unify. If we used TARGS, we'd clutter it up with the result of
19689 the attempted unification, even if this class didn't work out.
19690 We also don't want to commit ourselves to all the unifications
19691 we've already done, since unification is supposed to be done on
19692 an argument-by-argument basis. In other words, consider the
19693 following pathological case:
19694
19695 template <int I, int J, int K>
19696 struct S {};
19697
19698 template <int I, int J>
19699 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19700
19701 template <int I, int J, int K>
19702 void f(S<I, J, K>, S<I, I, I>);
19703
19704 void g() {
19705 S<0, 0, 0> s0;
19706 S<0, 1, 2> s2;
19707
19708 f(s0, s2);
19709 }
19710
19711 Now, by the time we consider the unification involving `s2', we
19712 already know that we must have `f<0, 0, 0>'. But, even though
19713 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19714 because there are two ways to unify base classes of S<0, 1, 2>
19715 with S<I, I, I>. If we kept the already deduced knowledge, we
19716 would reject the possibility I=1. */
19717 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19718
19719 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19720 {
19721 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19722 return NULL_TREE;
19723 return arg;
19724 }
19725
19726 /* If unification failed, we're done. */
19727 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19728 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19729 return NULL_TREE;
19730
19731 return arg;
19732 }
19733
19734 /* Given a template type PARM and a class type ARG, find the unique
19735 base type in ARG that is an instance of PARM. We do not examine
19736 ARG itself; only its base-classes. If there is not exactly one
19737 appropriate base class, return NULL_TREE. PARM may be the type of
19738 a partial specialization, as well as a plain template type. Used
19739 by unify. */
19740
19741 static enum template_base_result
19742 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19743 bool explain_p, tree *result)
19744 {
19745 tree rval = NULL_TREE;
19746 tree binfo;
19747
19748 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19749
19750 binfo = TYPE_BINFO (complete_type (arg));
19751 if (!binfo)
19752 {
19753 /* The type could not be completed. */
19754 *result = NULL_TREE;
19755 return tbr_incomplete_type;
19756 }
19757
19758 /* Walk in inheritance graph order. The search order is not
19759 important, and this avoids multiple walks of virtual bases. */
19760 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19761 {
19762 tree r = try_class_unification (tparms, targs, parm,
19763 BINFO_TYPE (binfo), explain_p);
19764
19765 if (r)
19766 {
19767 /* If there is more than one satisfactory baseclass, then:
19768
19769 [temp.deduct.call]
19770
19771 If they yield more than one possible deduced A, the type
19772 deduction fails.
19773
19774 applies. */
19775 if (rval && !same_type_p (r, rval))
19776 {
19777 *result = NULL_TREE;
19778 return tbr_ambiguous_baseclass;
19779 }
19780
19781 rval = r;
19782 }
19783 }
19784
19785 *result = rval;
19786 return tbr_success;
19787 }
19788
19789 /* Returns the level of DECL, which declares a template parameter. */
19790
19791 static int
19792 template_decl_level (tree decl)
19793 {
19794 switch (TREE_CODE (decl))
19795 {
19796 case TYPE_DECL:
19797 case TEMPLATE_DECL:
19798 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19799
19800 case PARM_DECL:
19801 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19802
19803 default:
19804 gcc_unreachable ();
19805 }
19806 return 0;
19807 }
19808
19809 /* Decide whether ARG can be unified with PARM, considering only the
19810 cv-qualifiers of each type, given STRICT as documented for unify.
19811 Returns nonzero iff the unification is OK on that basis. */
19812
19813 static int
19814 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19815 {
19816 int arg_quals = cp_type_quals (arg);
19817 int parm_quals = cp_type_quals (parm);
19818
19819 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19820 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19821 {
19822 /* Although a CVR qualifier is ignored when being applied to a
19823 substituted template parameter ([8.3.2]/1 for example), that
19824 does not allow us to unify "const T" with "int&" because both
19825 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19826 It is ok when we're allowing additional CV qualifiers
19827 at the outer level [14.8.2.1]/3,1st bullet. */
19828 if ((TREE_CODE (arg) == REFERENCE_TYPE
19829 || TREE_CODE (arg) == FUNCTION_TYPE
19830 || TREE_CODE (arg) == METHOD_TYPE)
19831 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19832 return 0;
19833
19834 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19835 && (parm_quals & TYPE_QUAL_RESTRICT))
19836 return 0;
19837 }
19838
19839 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19840 && (arg_quals & parm_quals) != parm_quals)
19841 return 0;
19842
19843 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19844 && (parm_quals & arg_quals) != arg_quals)
19845 return 0;
19846
19847 return 1;
19848 }
19849
19850 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19851 void
19852 template_parm_level_and_index (tree parm, int* level, int* index)
19853 {
19854 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19855 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19856 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19857 {
19858 *index = TEMPLATE_TYPE_IDX (parm);
19859 *level = TEMPLATE_TYPE_LEVEL (parm);
19860 }
19861 else
19862 {
19863 *index = TEMPLATE_PARM_IDX (parm);
19864 *level = TEMPLATE_PARM_LEVEL (parm);
19865 }
19866 }
19867
19868 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19869 do { \
19870 if (unify (TP, TA, P, A, S, EP)) \
19871 return 1; \
19872 } while (0)
19873
19874 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19875 expansion at the end of PACKED_PARMS. Returns 0 if the type
19876 deduction succeeds, 1 otherwise. STRICT is the same as in
19877 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19878 function call argument list. We'll need to adjust the arguments to make them
19879 types. SUBR tells us if this is from a recursive call to
19880 type_unification_real, or for comparing two template argument
19881 lists. */
19882
19883 static int
19884 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19885 tree packed_args, unification_kind_t strict,
19886 bool subr, bool explain_p)
19887 {
19888 tree parm
19889 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19890 tree pattern = PACK_EXPANSION_PATTERN (parm);
19891 tree pack, packs = NULL_TREE;
19892 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19893
19894 packed_args = expand_template_argument_pack (packed_args);
19895
19896 int len = TREE_VEC_LENGTH (packed_args);
19897
19898 /* Determine the parameter packs we will be deducing from the
19899 pattern, and record their current deductions. */
19900 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19901 pack; pack = TREE_CHAIN (pack))
19902 {
19903 tree parm_pack = TREE_VALUE (pack);
19904 int idx, level;
19905
19906 /* Determine the index and level of this parameter pack. */
19907 template_parm_level_and_index (parm_pack, &level, &idx);
19908
19909 /* Keep track of the parameter packs and their corresponding
19910 argument packs. */
19911 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19912 TREE_TYPE (packs) = make_tree_vec (len - start);
19913 }
19914
19915 /* Loop through all of the arguments that have not yet been
19916 unified and unify each with the pattern. */
19917 for (i = start; i < len; i++)
19918 {
19919 tree parm;
19920 bool any_explicit = false;
19921 tree arg = TREE_VEC_ELT (packed_args, i);
19922
19923 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19924 or the element of its argument pack at the current index if
19925 this argument was explicitly specified. */
19926 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19927 {
19928 int idx, level;
19929 tree arg, pargs;
19930 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19931
19932 arg = NULL_TREE;
19933 if (TREE_VALUE (pack)
19934 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19935 && (i - start < TREE_VEC_LENGTH (pargs)))
19936 {
19937 any_explicit = true;
19938 arg = TREE_VEC_ELT (pargs, i - start);
19939 }
19940 TMPL_ARG (targs, level, idx) = arg;
19941 }
19942
19943 /* If we had explicit template arguments, substitute them into the
19944 pattern before deduction. */
19945 if (any_explicit)
19946 {
19947 /* Some arguments might still be unspecified or dependent. */
19948 bool dependent;
19949 ++processing_template_decl;
19950 dependent = any_dependent_template_arguments_p (targs);
19951 if (!dependent)
19952 --processing_template_decl;
19953 parm = tsubst (pattern, targs,
19954 explain_p ? tf_warning_or_error : tf_none,
19955 NULL_TREE);
19956 if (dependent)
19957 --processing_template_decl;
19958 if (parm == error_mark_node)
19959 return 1;
19960 }
19961 else
19962 parm = pattern;
19963
19964 /* Unify the pattern with the current argument. */
19965 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19966 explain_p))
19967 return 1;
19968
19969 /* For each parameter pack, collect the deduced value. */
19970 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19971 {
19972 int idx, level;
19973 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19974
19975 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19976 TMPL_ARG (targs, level, idx);
19977 }
19978 }
19979
19980 /* Verify that the results of unification with the parameter packs
19981 produce results consistent with what we've seen before, and make
19982 the deduced argument packs available. */
19983 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19984 {
19985 tree old_pack = TREE_VALUE (pack);
19986 tree new_args = TREE_TYPE (pack);
19987 int i, len = TREE_VEC_LENGTH (new_args);
19988 int idx, level;
19989 bool nondeduced_p = false;
19990
19991 /* By default keep the original deduced argument pack.
19992 If necessary, more specific code is going to update the
19993 resulting deduced argument later down in this function. */
19994 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19995 TMPL_ARG (targs, level, idx) = old_pack;
19996
19997 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19998 actually deduce anything. */
19999 for (i = 0; i < len && !nondeduced_p; ++i)
20000 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20001 nondeduced_p = true;
20002 if (nondeduced_p)
20003 continue;
20004
20005 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20006 {
20007 /* If we had fewer function args than explicit template args,
20008 just use the explicits. */
20009 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20010 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20011 if (len < explicit_len)
20012 new_args = explicit_args;
20013 }
20014
20015 if (!old_pack)
20016 {
20017 tree result;
20018 /* Build the deduced *_ARGUMENT_PACK. */
20019 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20020 {
20021 result = make_node (NONTYPE_ARGUMENT_PACK);
20022 TREE_TYPE (result) =
20023 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
20024 TREE_CONSTANT (result) = 1;
20025 }
20026 else
20027 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20028
20029 SET_ARGUMENT_PACK_ARGS (result, new_args);
20030
20031 /* Note the deduced argument packs for this parameter
20032 pack. */
20033 TMPL_ARG (targs, level, idx) = result;
20034 }
20035 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20036 && (ARGUMENT_PACK_ARGS (old_pack)
20037 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20038 {
20039 /* We only had the explicitly-provided arguments before, but
20040 now we have a complete set of arguments. */
20041 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20042
20043 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20044 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20045 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20046 }
20047 else
20048 {
20049 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20050 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20051
20052 if (!comp_template_args (old_args, new_args,
20053 &bad_old_arg, &bad_new_arg))
20054 /* Inconsistent unification of this parameter pack. */
20055 return unify_parameter_pack_inconsistent (explain_p,
20056 bad_old_arg,
20057 bad_new_arg);
20058 }
20059 }
20060
20061 return unify_success (explain_p);
20062 }
20063
20064 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20065 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20066 parameters and return value are as for unify. */
20067
20068 static int
20069 unify_array_domain (tree tparms, tree targs,
20070 tree parm_dom, tree arg_dom,
20071 bool explain_p)
20072 {
20073 tree parm_max;
20074 tree arg_max;
20075 bool parm_cst;
20076 bool arg_cst;
20077
20078 /* Our representation of array types uses "N - 1" as the
20079 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20080 not an integer constant. We cannot unify arbitrarily
20081 complex expressions, so we eliminate the MINUS_EXPRs
20082 here. */
20083 parm_max = TYPE_MAX_VALUE (parm_dom);
20084 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20085 if (!parm_cst)
20086 {
20087 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20088 parm_max = TREE_OPERAND (parm_max, 0);
20089 }
20090 arg_max = TYPE_MAX_VALUE (arg_dom);
20091 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20092 if (!arg_cst)
20093 {
20094 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20095 trying to unify the type of a variable with the type
20096 of a template parameter. For example:
20097
20098 template <unsigned int N>
20099 void f (char (&) [N]);
20100 int g();
20101 void h(int i) {
20102 char a[g(i)];
20103 f(a);
20104 }
20105
20106 Here, the type of the ARG will be "int [g(i)]", and
20107 may be a SAVE_EXPR, etc. */
20108 if (TREE_CODE (arg_max) != MINUS_EXPR)
20109 return unify_vla_arg (explain_p, arg_dom);
20110 arg_max = TREE_OPERAND (arg_max, 0);
20111 }
20112
20113 /* If only one of the bounds used a MINUS_EXPR, compensate
20114 by adding one to the other bound. */
20115 if (parm_cst && !arg_cst)
20116 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20117 integer_type_node,
20118 parm_max,
20119 integer_one_node);
20120 else if (arg_cst && !parm_cst)
20121 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20122 integer_type_node,
20123 arg_max,
20124 integer_one_node);
20125
20126 return unify (tparms, targs, parm_max, arg_max,
20127 UNIFY_ALLOW_INTEGER, explain_p);
20128 }
20129
20130 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20131
20132 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20133
20134 static pa_kind_t
20135 pa_kind (tree t)
20136 {
20137 if (PACK_EXPANSION_P (t))
20138 t = PACK_EXPANSION_PATTERN (t);
20139 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20140 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20141 || DECL_TYPE_TEMPLATE_P (t))
20142 return pa_tmpl;
20143 else if (TYPE_P (t))
20144 return pa_type;
20145 else
20146 return pa_expr;
20147 }
20148
20149 /* Deduce the value of template parameters. TPARMS is the (innermost)
20150 set of template parameters to a template. TARGS is the bindings
20151 for those template parameters, as determined thus far; TARGS may
20152 include template arguments for outer levels of template parameters
20153 as well. PARM is a parameter to a template function, or a
20154 subcomponent of that parameter; ARG is the corresponding argument.
20155 This function attempts to match PARM with ARG in a manner
20156 consistent with the existing assignments in TARGS. If more values
20157 are deduced, then TARGS is updated.
20158
20159 Returns 0 if the type deduction succeeds, 1 otherwise. The
20160 parameter STRICT is a bitwise or of the following flags:
20161
20162 UNIFY_ALLOW_NONE:
20163 Require an exact match between PARM and ARG.
20164 UNIFY_ALLOW_MORE_CV_QUAL:
20165 Allow the deduced ARG to be more cv-qualified (by qualification
20166 conversion) than ARG.
20167 UNIFY_ALLOW_LESS_CV_QUAL:
20168 Allow the deduced ARG to be less cv-qualified than ARG.
20169 UNIFY_ALLOW_DERIVED:
20170 Allow the deduced ARG to be a template base class of ARG,
20171 or a pointer to a template base class of the type pointed to by
20172 ARG.
20173 UNIFY_ALLOW_INTEGER:
20174 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20175 case for more information.
20176 UNIFY_ALLOW_OUTER_LEVEL:
20177 This is the outermost level of a deduction. Used to determine validity
20178 of qualification conversions. A valid qualification conversion must
20179 have const qualified pointers leading up to the inner type which
20180 requires additional CV quals, except at the outer level, where const
20181 is not required [conv.qual]. It would be normal to set this flag in
20182 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20183 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20184 This is the outermost level of a deduction, and PARM can be more CV
20185 qualified at this point.
20186 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20187 This is the outermost level of a deduction, and PARM can be less CV
20188 qualified at this point. */
20189
20190 static int
20191 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20192 bool explain_p)
20193 {
20194 int idx;
20195 tree targ;
20196 tree tparm;
20197 int strict_in = strict;
20198 tsubst_flags_t complain = (explain_p
20199 ? tf_warning_or_error
20200 : tf_none);
20201
20202 /* I don't think this will do the right thing with respect to types.
20203 But the only case I've seen it in so far has been array bounds, where
20204 signedness is the only information lost, and I think that will be
20205 okay. */
20206 while (CONVERT_EXPR_P (parm))
20207 parm = TREE_OPERAND (parm, 0);
20208
20209 if (arg == error_mark_node)
20210 return unify_invalid (explain_p);
20211 if (arg == unknown_type_node
20212 || arg == init_list_type_node)
20213 /* We can't deduce anything from this, but we might get all the
20214 template args from other function args. */
20215 return unify_success (explain_p);
20216
20217 if (parm == any_targ_node || arg == any_targ_node)
20218 return unify_success (explain_p);
20219
20220 /* If PARM uses template parameters, then we can't bail out here,
20221 even if ARG == PARM, since we won't record unifications for the
20222 template parameters. We might need them if we're trying to
20223 figure out which of two things is more specialized. */
20224 if (arg == parm && !uses_template_parms (parm))
20225 return unify_success (explain_p);
20226
20227 /* Handle init lists early, so the rest of the function can assume
20228 we're dealing with a type. */
20229 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20230 {
20231 tree elt, elttype;
20232 unsigned i;
20233 tree orig_parm = parm;
20234
20235 /* Replace T with std::initializer_list<T> for deduction. */
20236 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20237 && flag_deduce_init_list)
20238 parm = listify (parm);
20239
20240 if (!is_std_init_list (parm)
20241 && TREE_CODE (parm) != ARRAY_TYPE)
20242 /* We can only deduce from an initializer list argument if the
20243 parameter is std::initializer_list or an array; otherwise this
20244 is a non-deduced context. */
20245 return unify_success (explain_p);
20246
20247 if (TREE_CODE (parm) == ARRAY_TYPE)
20248 elttype = TREE_TYPE (parm);
20249 else
20250 {
20251 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20252 /* Deduction is defined in terms of a single type, so just punt
20253 on the (bizarre) std::initializer_list<T...>. */
20254 if (PACK_EXPANSION_P (elttype))
20255 return unify_success (explain_p);
20256 }
20257
20258 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20259 {
20260 int elt_strict = strict;
20261
20262 if (elt == error_mark_node)
20263 return unify_invalid (explain_p);
20264
20265 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20266 {
20267 tree type = TREE_TYPE (elt);
20268 if (type == error_mark_node)
20269 return unify_invalid (explain_p);
20270 /* It should only be possible to get here for a call. */
20271 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20272 elt_strict |= maybe_adjust_types_for_deduction
20273 (DEDUCE_CALL, &elttype, &type, elt);
20274 elt = type;
20275 }
20276
20277 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20278 explain_p);
20279 }
20280
20281 if (TREE_CODE (parm) == ARRAY_TYPE
20282 && deducible_array_bound (TYPE_DOMAIN (parm)))
20283 {
20284 /* Also deduce from the length of the initializer list. */
20285 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20286 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20287 if (idx == error_mark_node)
20288 return unify_invalid (explain_p);
20289 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20290 idx, explain_p);
20291 }
20292
20293 /* If the std::initializer_list<T> deduction worked, replace the
20294 deduced A with std::initializer_list<A>. */
20295 if (orig_parm != parm)
20296 {
20297 idx = TEMPLATE_TYPE_IDX (orig_parm);
20298 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20299 targ = listify (targ);
20300 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20301 }
20302 return unify_success (explain_p);
20303 }
20304
20305 /* If parm and arg aren't the same kind of thing (template, type, or
20306 expression), fail early. */
20307 if (pa_kind (parm) != pa_kind (arg))
20308 return unify_invalid (explain_p);
20309
20310 /* Immediately reject some pairs that won't unify because of
20311 cv-qualification mismatches. */
20312 if (TREE_CODE (arg) == TREE_CODE (parm)
20313 && TYPE_P (arg)
20314 /* It is the elements of the array which hold the cv quals of an array
20315 type, and the elements might be template type parms. We'll check
20316 when we recurse. */
20317 && TREE_CODE (arg) != ARRAY_TYPE
20318 /* We check the cv-qualifiers when unifying with template type
20319 parameters below. We want to allow ARG `const T' to unify with
20320 PARM `T' for example, when computing which of two templates
20321 is more specialized, for example. */
20322 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20323 && !check_cv_quals_for_unify (strict_in, arg, parm))
20324 return unify_cv_qual_mismatch (explain_p, parm, arg);
20325
20326 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20327 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20328 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20329 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20330 strict &= ~UNIFY_ALLOW_DERIVED;
20331 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20332 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20333
20334 switch (TREE_CODE (parm))
20335 {
20336 case TYPENAME_TYPE:
20337 case SCOPE_REF:
20338 case UNBOUND_CLASS_TEMPLATE:
20339 /* In a type which contains a nested-name-specifier, template
20340 argument values cannot be deduced for template parameters used
20341 within the nested-name-specifier. */
20342 return unify_success (explain_p);
20343
20344 case TEMPLATE_TYPE_PARM:
20345 case TEMPLATE_TEMPLATE_PARM:
20346 case BOUND_TEMPLATE_TEMPLATE_PARM:
20347 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20348 if (error_operand_p (tparm))
20349 return unify_invalid (explain_p);
20350
20351 if (TEMPLATE_TYPE_LEVEL (parm)
20352 != template_decl_level (tparm))
20353 /* The PARM is not one we're trying to unify. Just check
20354 to see if it matches ARG. */
20355 {
20356 if (TREE_CODE (arg) == TREE_CODE (parm)
20357 && (is_auto (parm) ? is_auto (arg)
20358 : same_type_p (parm, arg)))
20359 return unify_success (explain_p);
20360 else
20361 return unify_type_mismatch (explain_p, parm, arg);
20362 }
20363 idx = TEMPLATE_TYPE_IDX (parm);
20364 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20365 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20366 if (error_operand_p (tparm))
20367 return unify_invalid (explain_p);
20368
20369 /* Check for mixed types and values. */
20370 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20371 && TREE_CODE (tparm) != TYPE_DECL)
20372 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20373 && TREE_CODE (tparm) != TEMPLATE_DECL))
20374 gcc_unreachable ();
20375
20376 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20377 {
20378 if ((strict_in & UNIFY_ALLOW_DERIVED)
20379 && CLASS_TYPE_P (arg))
20380 {
20381 /* First try to match ARG directly. */
20382 tree t = try_class_unification (tparms, targs, parm, arg,
20383 explain_p);
20384 if (!t)
20385 {
20386 /* Otherwise, look for a suitable base of ARG, as below. */
20387 enum template_base_result r;
20388 r = get_template_base (tparms, targs, parm, arg,
20389 explain_p, &t);
20390 if (!t)
20391 return unify_no_common_base (explain_p, r, parm, arg);
20392 arg = t;
20393 }
20394 }
20395 /* ARG must be constructed from a template class or a template
20396 template parameter. */
20397 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20398 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20399 return unify_template_deduction_failure (explain_p, parm, arg);
20400
20401 /* Deduce arguments T, i from TT<T> or TT<i>. */
20402 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20403 return 1;
20404
20405 arg = TYPE_TI_TEMPLATE (arg);
20406
20407 /* Fall through to deduce template name. */
20408 }
20409
20410 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20411 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20412 {
20413 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20414
20415 /* Simple cases: Value already set, does match or doesn't. */
20416 if (targ != NULL_TREE && template_args_equal (targ, arg))
20417 return unify_success (explain_p);
20418 else if (targ)
20419 return unify_inconsistency (explain_p, parm, targ, arg);
20420 }
20421 else
20422 {
20423 /* If PARM is `const T' and ARG is only `int', we don't have
20424 a match unless we are allowing additional qualification.
20425 If ARG is `const int' and PARM is just `T' that's OK;
20426 that binds `const int' to `T'. */
20427 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20428 arg, parm))
20429 return unify_cv_qual_mismatch (explain_p, parm, arg);
20430
20431 /* Consider the case where ARG is `const volatile int' and
20432 PARM is `const T'. Then, T should be `volatile int'. */
20433 arg = cp_build_qualified_type_real
20434 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20435 if (arg == error_mark_node)
20436 return unify_invalid (explain_p);
20437
20438 /* Simple cases: Value already set, does match or doesn't. */
20439 if (targ != NULL_TREE && same_type_p (targ, arg))
20440 return unify_success (explain_p);
20441 else if (targ)
20442 return unify_inconsistency (explain_p, parm, targ, arg);
20443
20444 /* Make sure that ARG is not a variable-sized array. (Note
20445 that were talking about variable-sized arrays (like
20446 `int[n]'), rather than arrays of unknown size (like
20447 `int[]').) We'll get very confused by such a type since
20448 the bound of the array is not constant, and therefore
20449 not mangleable. Besides, such types are not allowed in
20450 ISO C++, so we can do as we please here. We do allow
20451 them for 'auto' deduction, since that isn't ABI-exposed. */
20452 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20453 return unify_vla_arg (explain_p, arg);
20454
20455 /* Strip typedefs as in convert_template_argument. */
20456 arg = canonicalize_type_argument (arg, tf_none);
20457 }
20458
20459 /* If ARG is a parameter pack or an expansion, we cannot unify
20460 against it unless PARM is also a parameter pack. */
20461 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20462 && !template_parameter_pack_p (parm))
20463 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20464
20465 /* If the argument deduction results is a METHOD_TYPE,
20466 then there is a problem.
20467 METHOD_TYPE doesn't map to any real C++ type the result of
20468 the deduction can not be of that type. */
20469 if (TREE_CODE (arg) == METHOD_TYPE)
20470 return unify_method_type_error (explain_p, arg);
20471
20472 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20473 return unify_success (explain_p);
20474
20475 case TEMPLATE_PARM_INDEX:
20476 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20477 if (error_operand_p (tparm))
20478 return unify_invalid (explain_p);
20479
20480 if (TEMPLATE_PARM_LEVEL (parm)
20481 != template_decl_level (tparm))
20482 {
20483 /* The PARM is not one we're trying to unify. Just check
20484 to see if it matches ARG. */
20485 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20486 && cp_tree_equal (parm, arg));
20487 if (result)
20488 unify_expression_unequal (explain_p, parm, arg);
20489 return result;
20490 }
20491
20492 idx = TEMPLATE_PARM_IDX (parm);
20493 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20494
20495 if (targ)
20496 {
20497 int x = !cp_tree_equal (targ, arg);
20498 if (x)
20499 unify_inconsistency (explain_p, parm, targ, arg);
20500 return x;
20501 }
20502
20503 if (cxx_dialect >= cxx1z
20504 /* We deduce from array bounds in try_array_deduction. */
20505 && !(strict & UNIFY_ALLOW_INTEGER)
20506 && uses_template_parms (TREE_TYPE (parm))
20507 && !type_uses_auto (TREE_TYPE (parm)))
20508 {
20509 tree atype = TREE_TYPE (arg);
20510 RECUR_AND_CHECK_FAILURE (tparms, targs,
20511 TREE_TYPE (parm), atype,
20512 UNIFY_ALLOW_NONE, explain_p);
20513 }
20514
20515 /* [temp.deduct.type] If, in the declaration of a function template
20516 with a non-type template-parameter, the non-type
20517 template-parameter is used in an expression in the function
20518 parameter-list and, if the corresponding template-argument is
20519 deduced, the template-argument type shall match the type of the
20520 template-parameter exactly, except that a template-argument
20521 deduced from an array bound may be of any integral type.
20522 The non-type parameter might use already deduced type parameters. */
20523 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20524 if (tree a = type_uses_auto (tparm))
20525 {
20526 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20527 if (tparm == error_mark_node)
20528 return 1;
20529 }
20530
20531 if (!TREE_TYPE (arg))
20532 /* Template-parameter dependent expression. Just accept it for now.
20533 It will later be processed in convert_template_argument. */
20534 ;
20535 else if (same_type_p (TREE_TYPE (arg), tparm))
20536 /* OK */;
20537 else if ((strict & UNIFY_ALLOW_INTEGER)
20538 && CP_INTEGRAL_TYPE_P (tparm))
20539 /* Convert the ARG to the type of PARM; the deduced non-type
20540 template argument must exactly match the types of the
20541 corresponding parameter. */
20542 arg = fold (build_nop (tparm, arg));
20543 else if (uses_template_parms (tparm))
20544 /* We haven't deduced the type of this parameter yet. Try again
20545 later. */
20546 return unify_success (explain_p);
20547 else
20548 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20549
20550 /* If ARG is a parameter pack or an expansion, we cannot unify
20551 against it unless PARM is also a parameter pack. */
20552 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20553 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20554 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20555
20556 {
20557 bool removed_attr = false;
20558 arg = strip_typedefs_expr (arg, &removed_attr);
20559 }
20560 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20561 return unify_success (explain_p);
20562
20563 case PTRMEM_CST:
20564 {
20565 /* A pointer-to-member constant can be unified only with
20566 another constant. */
20567 if (TREE_CODE (arg) != PTRMEM_CST)
20568 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20569
20570 /* Just unify the class member. It would be useless (and possibly
20571 wrong, depending on the strict flags) to unify also
20572 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20573 arg refer to the same variable, even if through different
20574 classes. For instance:
20575
20576 struct A { int x; };
20577 struct B : A { };
20578
20579 Unification of &A::x and &B::x must succeed. */
20580 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20581 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20582 }
20583
20584 case POINTER_TYPE:
20585 {
20586 if (!TYPE_PTR_P (arg))
20587 return unify_type_mismatch (explain_p, parm, arg);
20588
20589 /* [temp.deduct.call]
20590
20591 A can be another pointer or pointer to member type that can
20592 be converted to the deduced A via a qualification
20593 conversion (_conv.qual_).
20594
20595 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20596 This will allow for additional cv-qualification of the
20597 pointed-to types if appropriate. */
20598
20599 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20600 /* The derived-to-base conversion only persists through one
20601 level of pointers. */
20602 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20603
20604 return unify (tparms, targs, TREE_TYPE (parm),
20605 TREE_TYPE (arg), strict, explain_p);
20606 }
20607
20608 case REFERENCE_TYPE:
20609 if (TREE_CODE (arg) != REFERENCE_TYPE)
20610 return unify_type_mismatch (explain_p, parm, arg);
20611 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20612 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20613
20614 case ARRAY_TYPE:
20615 if (TREE_CODE (arg) != ARRAY_TYPE)
20616 return unify_type_mismatch (explain_p, parm, arg);
20617 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20618 != (TYPE_DOMAIN (arg) == NULL_TREE))
20619 return unify_type_mismatch (explain_p, parm, arg);
20620 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20621 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20622 if (TYPE_DOMAIN (parm) != NULL_TREE)
20623 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20624 TYPE_DOMAIN (arg), explain_p);
20625 return unify_success (explain_p);
20626
20627 case REAL_TYPE:
20628 case COMPLEX_TYPE:
20629 case VECTOR_TYPE:
20630 case INTEGER_TYPE:
20631 case BOOLEAN_TYPE:
20632 case ENUMERAL_TYPE:
20633 case VOID_TYPE:
20634 case NULLPTR_TYPE:
20635 if (TREE_CODE (arg) != TREE_CODE (parm))
20636 return unify_type_mismatch (explain_p, parm, arg);
20637
20638 /* We have already checked cv-qualification at the top of the
20639 function. */
20640 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20641 return unify_type_mismatch (explain_p, parm, arg);
20642
20643 /* As far as unification is concerned, this wins. Later checks
20644 will invalidate it if necessary. */
20645 return unify_success (explain_p);
20646
20647 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20648 /* Type INTEGER_CST can come from ordinary constant template args. */
20649 case INTEGER_CST:
20650 while (CONVERT_EXPR_P (arg))
20651 arg = TREE_OPERAND (arg, 0);
20652
20653 if (TREE_CODE (arg) != INTEGER_CST)
20654 return unify_template_argument_mismatch (explain_p, parm, arg);
20655 return (tree_int_cst_equal (parm, arg)
20656 ? unify_success (explain_p)
20657 : unify_template_argument_mismatch (explain_p, parm, arg));
20658
20659 case TREE_VEC:
20660 {
20661 int i, len, argslen;
20662 int parm_variadic_p = 0;
20663
20664 if (TREE_CODE (arg) != TREE_VEC)
20665 return unify_template_argument_mismatch (explain_p, parm, arg);
20666
20667 len = TREE_VEC_LENGTH (parm);
20668 argslen = TREE_VEC_LENGTH (arg);
20669
20670 /* Check for pack expansions in the parameters. */
20671 for (i = 0; i < len; ++i)
20672 {
20673 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20674 {
20675 if (i == len - 1)
20676 /* We can unify against something with a trailing
20677 parameter pack. */
20678 parm_variadic_p = 1;
20679 else
20680 /* [temp.deduct.type]/9: If the template argument list of
20681 P contains a pack expansion that is not the last
20682 template argument, the entire template argument list
20683 is a non-deduced context. */
20684 return unify_success (explain_p);
20685 }
20686 }
20687
20688 /* If we don't have enough arguments to satisfy the parameters
20689 (not counting the pack expression at the end), or we have
20690 too many arguments for a parameter list that doesn't end in
20691 a pack expression, we can't unify. */
20692 if (parm_variadic_p
20693 ? argslen < len - parm_variadic_p
20694 : argslen != len)
20695 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20696
20697 /* Unify all of the parameters that precede the (optional)
20698 pack expression. */
20699 for (i = 0; i < len - parm_variadic_p; ++i)
20700 {
20701 RECUR_AND_CHECK_FAILURE (tparms, targs,
20702 TREE_VEC_ELT (parm, i),
20703 TREE_VEC_ELT (arg, i),
20704 UNIFY_ALLOW_NONE, explain_p);
20705 }
20706 if (parm_variadic_p)
20707 return unify_pack_expansion (tparms, targs, parm, arg,
20708 DEDUCE_EXACT,
20709 /*subr=*/true, explain_p);
20710 return unify_success (explain_p);
20711 }
20712
20713 case RECORD_TYPE:
20714 case UNION_TYPE:
20715 if (TREE_CODE (arg) != TREE_CODE (parm))
20716 return unify_type_mismatch (explain_p, parm, arg);
20717
20718 if (TYPE_PTRMEMFUNC_P (parm))
20719 {
20720 if (!TYPE_PTRMEMFUNC_P (arg))
20721 return unify_type_mismatch (explain_p, parm, arg);
20722
20723 return unify (tparms, targs,
20724 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20725 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20726 strict, explain_p);
20727 }
20728 else if (TYPE_PTRMEMFUNC_P (arg))
20729 return unify_type_mismatch (explain_p, parm, arg);
20730
20731 if (CLASSTYPE_TEMPLATE_INFO (parm))
20732 {
20733 tree t = NULL_TREE;
20734
20735 if (strict_in & UNIFY_ALLOW_DERIVED)
20736 {
20737 /* First, we try to unify the PARM and ARG directly. */
20738 t = try_class_unification (tparms, targs,
20739 parm, arg, explain_p);
20740
20741 if (!t)
20742 {
20743 /* Fallback to the special case allowed in
20744 [temp.deduct.call]:
20745
20746 If P is a class, and P has the form
20747 template-id, then A can be a derived class of
20748 the deduced A. Likewise, if P is a pointer to
20749 a class of the form template-id, A can be a
20750 pointer to a derived class pointed to by the
20751 deduced A. */
20752 enum template_base_result r;
20753 r = get_template_base (tparms, targs, parm, arg,
20754 explain_p, &t);
20755
20756 if (!t)
20757 {
20758 /* Don't give the derived diagnostic if we're
20759 already dealing with the same template. */
20760 bool same_template
20761 = (CLASSTYPE_TEMPLATE_INFO (arg)
20762 && (CLASSTYPE_TI_TEMPLATE (parm)
20763 == CLASSTYPE_TI_TEMPLATE (arg)));
20764 return unify_no_common_base (explain_p && !same_template,
20765 r, parm, arg);
20766 }
20767 }
20768 }
20769 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20770 && (CLASSTYPE_TI_TEMPLATE (parm)
20771 == CLASSTYPE_TI_TEMPLATE (arg)))
20772 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20773 Then, we should unify `int' and `U'. */
20774 t = arg;
20775 else
20776 /* There's no chance of unification succeeding. */
20777 return unify_type_mismatch (explain_p, parm, arg);
20778
20779 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20780 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20781 }
20782 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20783 return unify_type_mismatch (explain_p, parm, arg);
20784 return unify_success (explain_p);
20785
20786 case METHOD_TYPE:
20787 case FUNCTION_TYPE:
20788 {
20789 unsigned int nargs;
20790 tree *args;
20791 tree a;
20792 unsigned int i;
20793
20794 if (TREE_CODE (arg) != TREE_CODE (parm))
20795 return unify_type_mismatch (explain_p, parm, arg);
20796
20797 /* CV qualifications for methods can never be deduced, they must
20798 match exactly. We need to check them explicitly here,
20799 because type_unification_real treats them as any other
20800 cv-qualified parameter. */
20801 if (TREE_CODE (parm) == METHOD_TYPE
20802 && (!check_cv_quals_for_unify
20803 (UNIFY_ALLOW_NONE,
20804 class_of_this_parm (arg),
20805 class_of_this_parm (parm))))
20806 return unify_cv_qual_mismatch (explain_p, parm, arg);
20807 if (TREE_CODE (arg) == FUNCTION_TYPE
20808 && type_memfn_quals (parm) != type_memfn_quals (arg))
20809 return unify_cv_qual_mismatch (explain_p, parm, arg);
20810 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20811 return unify_type_mismatch (explain_p, parm, arg);
20812
20813 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20814 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20815
20816 nargs = list_length (TYPE_ARG_TYPES (arg));
20817 args = XALLOCAVEC (tree, nargs);
20818 for (a = TYPE_ARG_TYPES (arg), i = 0;
20819 a != NULL_TREE && a != void_list_node;
20820 a = TREE_CHAIN (a), ++i)
20821 args[i] = TREE_VALUE (a);
20822 nargs = i;
20823
20824 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20825 args, nargs, 1, DEDUCE_EXACT,
20826 LOOKUP_NORMAL, NULL, explain_p))
20827 return 1;
20828
20829 if (flag_noexcept_type)
20830 {
20831 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20832 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20833 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20834 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20835 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20836 && uses_template_parms (TREE_PURPOSE (pspec)))
20837 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20838 TREE_PURPOSE (aspec),
20839 UNIFY_ALLOW_NONE, explain_p);
20840 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20841 return unify_type_mismatch (explain_p, parm, arg);
20842 }
20843
20844 return 0;
20845 }
20846
20847 case OFFSET_TYPE:
20848 /* Unify a pointer to member with a pointer to member function, which
20849 deduces the type of the member as a function type. */
20850 if (TYPE_PTRMEMFUNC_P (arg))
20851 {
20852 /* Check top-level cv qualifiers */
20853 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20854 return unify_cv_qual_mismatch (explain_p, parm, arg);
20855
20856 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20857 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20858 UNIFY_ALLOW_NONE, explain_p);
20859
20860 /* Determine the type of the function we are unifying against. */
20861 tree fntype = static_fn_type (arg);
20862
20863 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20864 }
20865
20866 if (TREE_CODE (arg) != OFFSET_TYPE)
20867 return unify_type_mismatch (explain_p, parm, arg);
20868 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20869 TYPE_OFFSET_BASETYPE (arg),
20870 UNIFY_ALLOW_NONE, explain_p);
20871 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20872 strict, explain_p);
20873
20874 case CONST_DECL:
20875 if (DECL_TEMPLATE_PARM_P (parm))
20876 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20877 if (arg != scalar_constant_value (parm))
20878 return unify_template_argument_mismatch (explain_p, parm, arg);
20879 return unify_success (explain_p);
20880
20881 case FIELD_DECL:
20882 case TEMPLATE_DECL:
20883 /* Matched cases are handled by the ARG == PARM test above. */
20884 return unify_template_argument_mismatch (explain_p, parm, arg);
20885
20886 case VAR_DECL:
20887 /* We might get a variable as a non-type template argument in parm if the
20888 corresponding parameter is type-dependent. Make any necessary
20889 adjustments based on whether arg is a reference. */
20890 if (CONSTANT_CLASS_P (arg))
20891 parm = fold_non_dependent_expr (parm);
20892 else if (REFERENCE_REF_P (arg))
20893 {
20894 tree sub = TREE_OPERAND (arg, 0);
20895 STRIP_NOPS (sub);
20896 if (TREE_CODE (sub) == ADDR_EXPR)
20897 arg = TREE_OPERAND (sub, 0);
20898 }
20899 /* Now use the normal expression code to check whether they match. */
20900 goto expr;
20901
20902 case TYPE_ARGUMENT_PACK:
20903 case NONTYPE_ARGUMENT_PACK:
20904 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20905 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20906
20907 case TYPEOF_TYPE:
20908 case DECLTYPE_TYPE:
20909 case UNDERLYING_TYPE:
20910 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20911 or UNDERLYING_TYPE nodes. */
20912 return unify_success (explain_p);
20913
20914 case ERROR_MARK:
20915 /* Unification fails if we hit an error node. */
20916 return unify_invalid (explain_p);
20917
20918 case INDIRECT_REF:
20919 if (REFERENCE_REF_P (parm))
20920 {
20921 if (REFERENCE_REF_P (arg))
20922 arg = TREE_OPERAND (arg, 0);
20923 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20924 strict, explain_p);
20925 }
20926 /* FALLTHRU */
20927
20928 default:
20929 /* An unresolved overload is a nondeduced context. */
20930 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20931 return unify_success (explain_p);
20932 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20933 expr:
20934 /* We must be looking at an expression. This can happen with
20935 something like:
20936
20937 template <int I>
20938 void foo(S<I>, S<I + 2>);
20939
20940 This is a "nondeduced context":
20941
20942 [deduct.type]
20943
20944 The nondeduced contexts are:
20945
20946 --A type that is a template-id in which one or more of
20947 the template-arguments is an expression that references
20948 a template-parameter.
20949
20950 In these cases, we assume deduction succeeded, but don't
20951 actually infer any unifications. */
20952
20953 if (!uses_template_parms (parm)
20954 && !template_args_equal (parm, arg))
20955 return unify_expression_unequal (explain_p, parm, arg);
20956 else
20957 return unify_success (explain_p);
20958 }
20959 }
20960 #undef RECUR_AND_CHECK_FAILURE
20961 \f
20962 /* Note that DECL can be defined in this translation unit, if
20963 required. */
20964
20965 static void
20966 mark_definable (tree decl)
20967 {
20968 tree clone;
20969 DECL_NOT_REALLY_EXTERN (decl) = 1;
20970 FOR_EACH_CLONE (clone, decl)
20971 DECL_NOT_REALLY_EXTERN (clone) = 1;
20972 }
20973
20974 /* Called if RESULT is explicitly instantiated, or is a member of an
20975 explicitly instantiated class. */
20976
20977 void
20978 mark_decl_instantiated (tree result, int extern_p)
20979 {
20980 SET_DECL_EXPLICIT_INSTANTIATION (result);
20981
20982 /* If this entity has already been written out, it's too late to
20983 make any modifications. */
20984 if (TREE_ASM_WRITTEN (result))
20985 return;
20986
20987 /* For anonymous namespace we don't need to do anything. */
20988 if (decl_anon_ns_mem_p (result))
20989 {
20990 gcc_assert (!TREE_PUBLIC (result));
20991 return;
20992 }
20993
20994 if (TREE_CODE (result) != FUNCTION_DECL)
20995 /* The TREE_PUBLIC flag for function declarations will have been
20996 set correctly by tsubst. */
20997 TREE_PUBLIC (result) = 1;
20998
20999 /* This might have been set by an earlier implicit instantiation. */
21000 DECL_COMDAT (result) = 0;
21001
21002 if (extern_p)
21003 DECL_NOT_REALLY_EXTERN (result) = 0;
21004 else
21005 {
21006 mark_definable (result);
21007 mark_needed (result);
21008 /* Always make artificials weak. */
21009 if (DECL_ARTIFICIAL (result) && flag_weak)
21010 comdat_linkage (result);
21011 /* For WIN32 we also want to put explicit instantiations in
21012 linkonce sections. */
21013 else if (TREE_PUBLIC (result))
21014 maybe_make_one_only (result);
21015 }
21016
21017 /* If EXTERN_P, then this function will not be emitted -- unless
21018 followed by an explicit instantiation, at which point its linkage
21019 will be adjusted. If !EXTERN_P, then this function will be
21020 emitted here. In neither circumstance do we want
21021 import_export_decl to adjust the linkage. */
21022 DECL_INTERFACE_KNOWN (result) = 1;
21023 }
21024
21025 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21026 important template arguments. If any are missing, we check whether
21027 they're important by using error_mark_node for substituting into any
21028 args that were used for partial ordering (the ones between ARGS and END)
21029 and seeing if it bubbles up. */
21030
21031 static bool
21032 check_undeduced_parms (tree targs, tree args, tree end)
21033 {
21034 bool found = false;
21035 int i;
21036 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21037 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21038 {
21039 found = true;
21040 TREE_VEC_ELT (targs, i) = error_mark_node;
21041 }
21042 if (found)
21043 {
21044 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21045 if (substed == error_mark_node)
21046 return true;
21047 }
21048 return false;
21049 }
21050
21051 /* Given two function templates PAT1 and PAT2, return:
21052
21053 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21054 -1 if PAT2 is more specialized than PAT1.
21055 0 if neither is more specialized.
21056
21057 LEN indicates the number of parameters we should consider
21058 (defaulted parameters should not be considered).
21059
21060 The 1998 std underspecified function template partial ordering, and
21061 DR214 addresses the issue. We take pairs of arguments, one from
21062 each of the templates, and deduce them against each other. One of
21063 the templates will be more specialized if all the *other*
21064 template's arguments deduce against its arguments and at least one
21065 of its arguments *does* *not* deduce against the other template's
21066 corresponding argument. Deduction is done as for class templates.
21067 The arguments used in deduction have reference and top level cv
21068 qualifiers removed. Iff both arguments were originally reference
21069 types *and* deduction succeeds in both directions, an lvalue reference
21070 wins against an rvalue reference and otherwise the template
21071 with the more cv-qualified argument wins for that pairing (if
21072 neither is more cv-qualified, they both are equal). Unlike regular
21073 deduction, after all the arguments have been deduced in this way,
21074 we do *not* verify the deduced template argument values can be
21075 substituted into non-deduced contexts.
21076
21077 The logic can be a bit confusing here, because we look at deduce1 and
21078 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21079 can find template arguments for pat1 to make arg1 look like arg2, that
21080 means that arg2 is at least as specialized as arg1. */
21081
21082 int
21083 more_specialized_fn (tree pat1, tree pat2, int len)
21084 {
21085 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21086 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21087 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21088 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21089 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21090 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21091 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21092 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21093 tree origs1, origs2;
21094 bool lose1 = false;
21095 bool lose2 = false;
21096
21097 /* Remove the this parameter from non-static member functions. If
21098 one is a non-static member function and the other is not a static
21099 member function, remove the first parameter from that function
21100 also. This situation occurs for operator functions where we
21101 locate both a member function (with this pointer) and non-member
21102 operator (with explicit first operand). */
21103 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21104 {
21105 len--; /* LEN is the number of significant arguments for DECL1 */
21106 args1 = TREE_CHAIN (args1);
21107 if (!DECL_STATIC_FUNCTION_P (decl2))
21108 args2 = TREE_CHAIN (args2);
21109 }
21110 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21111 {
21112 args2 = TREE_CHAIN (args2);
21113 if (!DECL_STATIC_FUNCTION_P (decl1))
21114 {
21115 len--;
21116 args1 = TREE_CHAIN (args1);
21117 }
21118 }
21119
21120 /* If only one is a conversion operator, they are unordered. */
21121 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21122 return 0;
21123
21124 /* Consider the return type for a conversion function */
21125 if (DECL_CONV_FN_P (decl1))
21126 {
21127 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21128 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21129 len++;
21130 }
21131
21132 processing_template_decl++;
21133
21134 origs1 = args1;
21135 origs2 = args2;
21136
21137 while (len--
21138 /* Stop when an ellipsis is seen. */
21139 && args1 != NULL_TREE && args2 != NULL_TREE)
21140 {
21141 tree arg1 = TREE_VALUE (args1);
21142 tree arg2 = TREE_VALUE (args2);
21143 int deduce1, deduce2;
21144 int quals1 = -1;
21145 int quals2 = -1;
21146 int ref1 = 0;
21147 int ref2 = 0;
21148
21149 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21150 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21151 {
21152 /* When both arguments are pack expansions, we need only
21153 unify the patterns themselves. */
21154 arg1 = PACK_EXPANSION_PATTERN (arg1);
21155 arg2 = PACK_EXPANSION_PATTERN (arg2);
21156
21157 /* This is the last comparison we need to do. */
21158 len = 0;
21159 }
21160
21161 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21162 {
21163 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21164 arg1 = TREE_TYPE (arg1);
21165 quals1 = cp_type_quals (arg1);
21166 }
21167
21168 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21169 {
21170 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21171 arg2 = TREE_TYPE (arg2);
21172 quals2 = cp_type_quals (arg2);
21173 }
21174
21175 arg1 = TYPE_MAIN_VARIANT (arg1);
21176 arg2 = TYPE_MAIN_VARIANT (arg2);
21177
21178 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21179 {
21180 int i, len2 = remaining_arguments (args2);
21181 tree parmvec = make_tree_vec (1);
21182 tree argvec = make_tree_vec (len2);
21183 tree ta = args2;
21184
21185 /* Setup the parameter vector, which contains only ARG1. */
21186 TREE_VEC_ELT (parmvec, 0) = arg1;
21187
21188 /* Setup the argument vector, which contains the remaining
21189 arguments. */
21190 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21191 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21192
21193 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21194 argvec, DEDUCE_EXACT,
21195 /*subr=*/true, /*explain_p=*/false)
21196 == 0);
21197
21198 /* We cannot deduce in the other direction, because ARG1 is
21199 a pack expansion but ARG2 is not. */
21200 deduce2 = 0;
21201 }
21202 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21203 {
21204 int i, len1 = remaining_arguments (args1);
21205 tree parmvec = make_tree_vec (1);
21206 tree argvec = make_tree_vec (len1);
21207 tree ta = args1;
21208
21209 /* Setup the parameter vector, which contains only ARG1. */
21210 TREE_VEC_ELT (parmvec, 0) = arg2;
21211
21212 /* Setup the argument vector, which contains the remaining
21213 arguments. */
21214 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21215 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21216
21217 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21218 argvec, DEDUCE_EXACT,
21219 /*subr=*/true, /*explain_p=*/false)
21220 == 0);
21221
21222 /* We cannot deduce in the other direction, because ARG2 is
21223 a pack expansion but ARG1 is not.*/
21224 deduce1 = 0;
21225 }
21226
21227 else
21228 {
21229 /* The normal case, where neither argument is a pack
21230 expansion. */
21231 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21232 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21233 == 0);
21234 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21235 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21236 == 0);
21237 }
21238
21239 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21240 arg2, then arg2 is not as specialized as arg1. */
21241 if (!deduce1)
21242 lose2 = true;
21243 if (!deduce2)
21244 lose1 = true;
21245
21246 /* "If, for a given type, deduction succeeds in both directions
21247 (i.e., the types are identical after the transformations above)
21248 and both P and A were reference types (before being replaced with
21249 the type referred to above):
21250 - if the type from the argument template was an lvalue reference and
21251 the type from the parameter template was not, the argument type is
21252 considered to be more specialized than the other; otherwise,
21253 - if the type from the argument template is more cv-qualified
21254 than the type from the parameter template (as described above),
21255 the argument type is considered to be more specialized than the other;
21256 otherwise,
21257 - neither type is more specialized than the other." */
21258
21259 if (deduce1 && deduce2)
21260 {
21261 if (ref1 && ref2 && ref1 != ref2)
21262 {
21263 if (ref1 > ref2)
21264 lose1 = true;
21265 else
21266 lose2 = true;
21267 }
21268 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21269 {
21270 if ((quals1 & quals2) == quals2)
21271 lose2 = true;
21272 if ((quals1 & quals2) == quals1)
21273 lose1 = true;
21274 }
21275 }
21276
21277 if (lose1 && lose2)
21278 /* We've failed to deduce something in either direction.
21279 These must be unordered. */
21280 break;
21281
21282 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21283 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21284 /* We have already processed all of the arguments in our
21285 handing of the pack expansion type. */
21286 len = 0;
21287
21288 args1 = TREE_CHAIN (args1);
21289 args2 = TREE_CHAIN (args2);
21290 }
21291
21292 /* "In most cases, all template parameters must have values in order for
21293 deduction to succeed, but for partial ordering purposes a template
21294 parameter may remain without a value provided it is not used in the
21295 types being used for partial ordering."
21296
21297 Thus, if we are missing any of the targs1 we need to substitute into
21298 origs1, then pat2 is not as specialized as pat1. This can happen when
21299 there is a nondeduced context. */
21300 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21301 lose2 = true;
21302 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21303 lose1 = true;
21304
21305 processing_template_decl--;
21306
21307 /* If both deductions succeed, the partial ordering selects the more
21308 constrained template. */
21309 if (!lose1 && !lose2)
21310 {
21311 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21312 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21313 lose1 = !subsumes_constraints (c1, c2);
21314 lose2 = !subsumes_constraints (c2, c1);
21315 }
21316
21317 /* All things being equal, if the next argument is a pack expansion
21318 for one function but not for the other, prefer the
21319 non-variadic function. FIXME this is bogus; see c++/41958. */
21320 if (lose1 == lose2
21321 && args1 && TREE_VALUE (args1)
21322 && args2 && TREE_VALUE (args2))
21323 {
21324 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21325 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21326 }
21327
21328 if (lose1 == lose2)
21329 return 0;
21330 else if (!lose1)
21331 return 1;
21332 else
21333 return -1;
21334 }
21335
21336 /* Determine which of two partial specializations of TMPL is more
21337 specialized.
21338
21339 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21340 to the first partial specialization. The TREE_PURPOSE is the
21341 innermost set of template parameters for the partial
21342 specialization. PAT2 is similar, but for the second template.
21343
21344 Return 1 if the first partial specialization is more specialized;
21345 -1 if the second is more specialized; 0 if neither is more
21346 specialized.
21347
21348 See [temp.class.order] for information about determining which of
21349 two templates is more specialized. */
21350
21351 static int
21352 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21353 {
21354 tree targs;
21355 int winner = 0;
21356 bool any_deductions = false;
21357
21358 tree tmpl1 = TREE_VALUE (pat1);
21359 tree tmpl2 = TREE_VALUE (pat2);
21360 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21361 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21362
21363 /* Just like what happens for functions, if we are ordering between
21364 different template specializations, we may encounter dependent
21365 types in the arguments, and we need our dependency check functions
21366 to behave correctly. */
21367 ++processing_template_decl;
21368 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21369 if (targs)
21370 {
21371 --winner;
21372 any_deductions = true;
21373 }
21374
21375 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21376 if (targs)
21377 {
21378 ++winner;
21379 any_deductions = true;
21380 }
21381 --processing_template_decl;
21382
21383 /* If both deductions succeed, the partial ordering selects the more
21384 constrained template. */
21385 if (!winner && any_deductions)
21386 return more_constrained (tmpl1, tmpl2);
21387
21388 /* In the case of a tie where at least one of the templates
21389 has a parameter pack at the end, the template with the most
21390 non-packed parameters wins. */
21391 if (winner == 0
21392 && any_deductions
21393 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21394 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21395 {
21396 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21397 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21398 int len1 = TREE_VEC_LENGTH (args1);
21399 int len2 = TREE_VEC_LENGTH (args2);
21400
21401 /* We don't count the pack expansion at the end. */
21402 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21403 --len1;
21404 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21405 --len2;
21406
21407 if (len1 > len2)
21408 return 1;
21409 else if (len1 < len2)
21410 return -1;
21411 }
21412
21413 return winner;
21414 }
21415
21416 /* Return the template arguments that will produce the function signature
21417 DECL from the function template FN, with the explicit template
21418 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21419 also match. Return NULL_TREE if no satisfactory arguments could be
21420 found. */
21421
21422 static tree
21423 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21424 {
21425 int ntparms = DECL_NTPARMS (fn);
21426 tree targs = make_tree_vec (ntparms);
21427 tree decl_type = TREE_TYPE (decl);
21428 tree decl_arg_types;
21429 tree *args;
21430 unsigned int nargs, ix;
21431 tree arg;
21432
21433 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21434
21435 /* Never do unification on the 'this' parameter. */
21436 decl_arg_types = skip_artificial_parms_for (decl,
21437 TYPE_ARG_TYPES (decl_type));
21438
21439 nargs = list_length (decl_arg_types);
21440 args = XALLOCAVEC (tree, nargs);
21441 for (arg = decl_arg_types, ix = 0;
21442 arg != NULL_TREE && arg != void_list_node;
21443 arg = TREE_CHAIN (arg), ++ix)
21444 args[ix] = TREE_VALUE (arg);
21445
21446 if (fn_type_unification (fn, explicit_args, targs,
21447 args, ix,
21448 (check_rettype || DECL_CONV_FN_P (fn)
21449 ? TREE_TYPE (decl_type) : NULL_TREE),
21450 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21451 /*decltype*/false)
21452 == error_mark_node)
21453 return NULL_TREE;
21454
21455 return targs;
21456 }
21457
21458 /* Return the innermost template arguments that, when applied to a partial
21459 specialization SPEC_TMPL of TMPL, yield the ARGS.
21460
21461 For example, suppose we have:
21462
21463 template <class T, class U> struct S {};
21464 template <class T> struct S<T*, int> {};
21465
21466 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21467 partial specialization and the ARGS will be {double*, int}. The resulting
21468 vector will be {double}, indicating that `T' is bound to `double'. */
21469
21470 static tree
21471 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21472 {
21473 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21474 tree spec_args
21475 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21476 int i, ntparms = TREE_VEC_LENGTH (tparms);
21477 tree deduced_args;
21478 tree innermost_deduced_args;
21479
21480 innermost_deduced_args = make_tree_vec (ntparms);
21481 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21482 {
21483 deduced_args = copy_node (args);
21484 SET_TMPL_ARGS_LEVEL (deduced_args,
21485 TMPL_ARGS_DEPTH (deduced_args),
21486 innermost_deduced_args);
21487 }
21488 else
21489 deduced_args = innermost_deduced_args;
21490
21491 bool tried_array_deduction = (cxx_dialect < cxx1z);
21492 again:
21493 if (unify (tparms, deduced_args,
21494 INNERMOST_TEMPLATE_ARGS (spec_args),
21495 INNERMOST_TEMPLATE_ARGS (args),
21496 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21497 return NULL_TREE;
21498
21499 for (i = 0; i < ntparms; ++i)
21500 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21501 {
21502 if (!tried_array_deduction)
21503 {
21504 try_array_deduction (tparms, innermost_deduced_args,
21505 INNERMOST_TEMPLATE_ARGS (spec_args));
21506 tried_array_deduction = true;
21507 if (TREE_VEC_ELT (innermost_deduced_args, i))
21508 goto again;
21509 }
21510 return NULL_TREE;
21511 }
21512
21513 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21514 if (!push_tinst_level (tinst))
21515 {
21516 excessive_deduction_depth = true;
21517 return NULL_TREE;
21518 }
21519
21520 /* Verify that nondeduced template arguments agree with the type
21521 obtained from argument deduction.
21522
21523 For example:
21524
21525 struct A { typedef int X; };
21526 template <class T, class U> struct C {};
21527 template <class T> struct C<T, typename T::X> {};
21528
21529 Then with the instantiation `C<A, int>', we can deduce that
21530 `T' is `A' but unify () does not check whether `typename T::X'
21531 is `int'. */
21532 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21533 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21534 spec_args, tmpl,
21535 tf_none, false, false);
21536
21537 pop_tinst_level ();
21538
21539 if (spec_args == error_mark_node
21540 /* We only need to check the innermost arguments; the other
21541 arguments will always agree. */
21542 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
21543 INNERMOST_TEMPLATE_ARGS (args)))
21544 return NULL_TREE;
21545
21546 /* Now that we have bindings for all of the template arguments,
21547 ensure that the arguments deduced for the template template
21548 parameters have compatible template parameter lists. See the use
21549 of template_template_parm_bindings_ok_p in fn_type_unification
21550 for more information. */
21551 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21552 return NULL_TREE;
21553
21554 return deduced_args;
21555 }
21556
21557 // Compare two function templates T1 and T2 by deducing bindings
21558 // from one against the other. If both deductions succeed, compare
21559 // constraints to see which is more constrained.
21560 static int
21561 more_specialized_inst (tree t1, tree t2)
21562 {
21563 int fate = 0;
21564 int count = 0;
21565
21566 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21567 {
21568 --fate;
21569 ++count;
21570 }
21571
21572 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21573 {
21574 ++fate;
21575 ++count;
21576 }
21577
21578 // If both deductions succeed, then one may be more constrained.
21579 if (count == 2 && fate == 0)
21580 fate = more_constrained (t1, t2);
21581
21582 return fate;
21583 }
21584
21585 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21586 Return the TREE_LIST node with the most specialized template, if
21587 any. If there is no most specialized template, the error_mark_node
21588 is returned.
21589
21590 Note that this function does not look at, or modify, the
21591 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21592 returned is one of the elements of INSTANTIATIONS, callers may
21593 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21594 and retrieve it from the value returned. */
21595
21596 tree
21597 most_specialized_instantiation (tree templates)
21598 {
21599 tree fn, champ;
21600
21601 ++processing_template_decl;
21602
21603 champ = templates;
21604 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21605 {
21606 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21607 if (fate == -1)
21608 champ = fn;
21609 else if (!fate)
21610 {
21611 /* Equally specialized, move to next function. If there
21612 is no next function, nothing's most specialized. */
21613 fn = TREE_CHAIN (fn);
21614 champ = fn;
21615 if (!fn)
21616 break;
21617 }
21618 }
21619
21620 if (champ)
21621 /* Now verify that champ is better than everything earlier in the
21622 instantiation list. */
21623 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21624 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21625 {
21626 champ = NULL_TREE;
21627 break;
21628 }
21629 }
21630
21631 processing_template_decl--;
21632
21633 if (!champ)
21634 return error_mark_node;
21635
21636 return champ;
21637 }
21638
21639 /* If DECL is a specialization of some template, return the most
21640 general such template. Otherwise, returns NULL_TREE.
21641
21642 For example, given:
21643
21644 template <class T> struct S { template <class U> void f(U); };
21645
21646 if TMPL is `template <class U> void S<int>::f(U)' this will return
21647 the full template. This function will not trace past partial
21648 specializations, however. For example, given in addition:
21649
21650 template <class T> struct S<T*> { template <class U> void f(U); };
21651
21652 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21653 `template <class T> template <class U> S<T*>::f(U)'. */
21654
21655 tree
21656 most_general_template (tree decl)
21657 {
21658 if (TREE_CODE (decl) != TEMPLATE_DECL)
21659 {
21660 if (tree tinfo = get_template_info (decl))
21661 decl = TI_TEMPLATE (tinfo);
21662 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21663 template friend, or a FIELD_DECL for a capture pack. */
21664 if (TREE_CODE (decl) != TEMPLATE_DECL)
21665 return NULL_TREE;
21666 }
21667
21668 /* Look for more and more general templates. */
21669 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21670 {
21671 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21672 (See cp-tree.h for details.) */
21673 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21674 break;
21675
21676 if (CLASS_TYPE_P (TREE_TYPE (decl))
21677 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21678 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21679 break;
21680
21681 /* Stop if we run into an explicitly specialized class template. */
21682 if (!DECL_NAMESPACE_SCOPE_P (decl)
21683 && DECL_CONTEXT (decl)
21684 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21685 break;
21686
21687 decl = DECL_TI_TEMPLATE (decl);
21688 }
21689
21690 return decl;
21691 }
21692
21693 /* Return the most specialized of the template partial specializations
21694 which can produce TARGET, a specialization of some class or variable
21695 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21696 a TEMPLATE_DECL node corresponding to the partial specialization, while
21697 the TREE_PURPOSE is the set of template arguments that must be
21698 substituted into the template pattern in order to generate TARGET.
21699
21700 If the choice of partial specialization is ambiguous, a diagnostic
21701 is issued, and the error_mark_node is returned. If there are no
21702 partial specializations matching TARGET, then NULL_TREE is
21703 returned, indicating that the primary template should be used. */
21704
21705 static tree
21706 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21707 {
21708 tree list = NULL_TREE;
21709 tree t;
21710 tree champ;
21711 int fate;
21712 bool ambiguous_p;
21713 tree outer_args = NULL_TREE;
21714 tree tmpl, args;
21715
21716 if (TYPE_P (target))
21717 {
21718 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21719 tmpl = TI_TEMPLATE (tinfo);
21720 args = TI_ARGS (tinfo);
21721 }
21722 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21723 {
21724 tmpl = TREE_OPERAND (target, 0);
21725 args = TREE_OPERAND (target, 1);
21726 }
21727 else if (VAR_P (target))
21728 {
21729 tree tinfo = DECL_TEMPLATE_INFO (target);
21730 tmpl = TI_TEMPLATE (tinfo);
21731 args = TI_ARGS (tinfo);
21732 }
21733 else
21734 gcc_unreachable ();
21735
21736 tree main_tmpl = most_general_template (tmpl);
21737
21738 /* For determining which partial specialization to use, only the
21739 innermost args are interesting. */
21740 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21741 {
21742 outer_args = strip_innermost_template_args (args, 1);
21743 args = INNERMOST_TEMPLATE_ARGS (args);
21744 }
21745
21746 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21747 {
21748 tree spec_args;
21749 tree spec_tmpl = TREE_VALUE (t);
21750
21751 if (outer_args)
21752 {
21753 /* Substitute in the template args from the enclosing class. */
21754 ++processing_template_decl;
21755 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21756 --processing_template_decl;
21757 }
21758
21759 if (spec_tmpl == error_mark_node)
21760 return error_mark_node;
21761
21762 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21763 if (spec_args)
21764 {
21765 if (outer_args)
21766 spec_args = add_to_template_args (outer_args, spec_args);
21767
21768 /* Keep the candidate only if the constraints are satisfied,
21769 or if we're not compiling with concepts. */
21770 if (!flag_concepts
21771 || constraints_satisfied_p (spec_tmpl, spec_args))
21772 {
21773 list = tree_cons (spec_args, TREE_VALUE (t), list);
21774 TREE_TYPE (list) = TREE_TYPE (t);
21775 }
21776 }
21777 }
21778
21779 if (! list)
21780 return NULL_TREE;
21781
21782 ambiguous_p = false;
21783 t = list;
21784 champ = t;
21785 t = TREE_CHAIN (t);
21786 for (; t; t = TREE_CHAIN (t))
21787 {
21788 fate = more_specialized_partial_spec (tmpl, champ, t);
21789 if (fate == 1)
21790 ;
21791 else
21792 {
21793 if (fate == 0)
21794 {
21795 t = TREE_CHAIN (t);
21796 if (! t)
21797 {
21798 ambiguous_p = true;
21799 break;
21800 }
21801 }
21802 champ = t;
21803 }
21804 }
21805
21806 if (!ambiguous_p)
21807 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21808 {
21809 fate = more_specialized_partial_spec (tmpl, champ, t);
21810 if (fate != 1)
21811 {
21812 ambiguous_p = true;
21813 break;
21814 }
21815 }
21816
21817 if (ambiguous_p)
21818 {
21819 const char *str;
21820 char *spaces = NULL;
21821 if (!(complain & tf_error))
21822 return error_mark_node;
21823 if (TYPE_P (target))
21824 error ("ambiguous template instantiation for %q#T", target);
21825 else
21826 error ("ambiguous template instantiation for %q#D", target);
21827 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21828 for (t = list; t; t = TREE_CHAIN (t))
21829 {
21830 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21831 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21832 "%s %#S", spaces ? spaces : str, subst);
21833 spaces = spaces ? spaces : get_spaces (str);
21834 }
21835 free (spaces);
21836 return error_mark_node;
21837 }
21838
21839 return champ;
21840 }
21841
21842 /* Explicitly instantiate DECL. */
21843
21844 void
21845 do_decl_instantiation (tree decl, tree storage)
21846 {
21847 tree result = NULL_TREE;
21848 int extern_p = 0;
21849
21850 if (!decl || decl == error_mark_node)
21851 /* An error occurred, for which grokdeclarator has already issued
21852 an appropriate message. */
21853 return;
21854 else if (! DECL_LANG_SPECIFIC (decl))
21855 {
21856 error ("explicit instantiation of non-template %q#D", decl);
21857 return;
21858 }
21859
21860 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21861 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21862
21863 if (VAR_P (decl) && !var_templ)
21864 {
21865 /* There is an asymmetry here in the way VAR_DECLs and
21866 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21867 the latter, the DECL we get back will be marked as a
21868 template instantiation, and the appropriate
21869 DECL_TEMPLATE_INFO will be set up. This does not happen for
21870 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21871 should handle VAR_DECLs as it currently handles
21872 FUNCTION_DECLs. */
21873 if (!DECL_CLASS_SCOPE_P (decl))
21874 {
21875 error ("%qD is not a static data member of a class template", decl);
21876 return;
21877 }
21878 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21879 if (!result || !VAR_P (result))
21880 {
21881 error ("no matching template for %qD found", decl);
21882 return;
21883 }
21884 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21885 {
21886 error ("type %qT for explicit instantiation %qD does not match "
21887 "declared type %qT", TREE_TYPE (result), decl,
21888 TREE_TYPE (decl));
21889 return;
21890 }
21891 }
21892 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21893 {
21894 error ("explicit instantiation of %q#D", decl);
21895 return;
21896 }
21897 else
21898 result = decl;
21899
21900 /* Check for various error cases. Note that if the explicit
21901 instantiation is valid the RESULT will currently be marked as an
21902 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21903 until we get here. */
21904
21905 if (DECL_TEMPLATE_SPECIALIZATION (result))
21906 {
21907 /* DR 259 [temp.spec].
21908
21909 Both an explicit instantiation and a declaration of an explicit
21910 specialization shall not appear in a program unless the explicit
21911 instantiation follows a declaration of the explicit specialization.
21912
21913 For a given set of template parameters, if an explicit
21914 instantiation of a template appears after a declaration of an
21915 explicit specialization for that template, the explicit
21916 instantiation has no effect. */
21917 return;
21918 }
21919 else if (DECL_EXPLICIT_INSTANTIATION (result))
21920 {
21921 /* [temp.spec]
21922
21923 No program shall explicitly instantiate any template more
21924 than once.
21925
21926 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21927 the first instantiation was `extern' and the second is not,
21928 and EXTERN_P for the opposite case. */
21929 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21930 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21931 /* If an "extern" explicit instantiation follows an ordinary
21932 explicit instantiation, the template is instantiated. */
21933 if (extern_p)
21934 return;
21935 }
21936 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21937 {
21938 error ("no matching template for %qD found", result);
21939 return;
21940 }
21941 else if (!DECL_TEMPLATE_INFO (result))
21942 {
21943 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21944 return;
21945 }
21946
21947 if (storage == NULL_TREE)
21948 ;
21949 else if (storage == ridpointers[(int) RID_EXTERN])
21950 {
21951 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21952 pedwarn (input_location, OPT_Wpedantic,
21953 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21954 "instantiations");
21955 extern_p = 1;
21956 }
21957 else
21958 error ("storage class %qD applied to template instantiation", storage);
21959
21960 check_explicit_instantiation_namespace (result);
21961 mark_decl_instantiated (result, extern_p);
21962 if (! extern_p)
21963 instantiate_decl (result, /*defer_ok=*/true,
21964 /*expl_inst_class_mem_p=*/false);
21965 }
21966
21967 static void
21968 mark_class_instantiated (tree t, int extern_p)
21969 {
21970 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21971 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21972 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21973 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21974 if (! extern_p)
21975 {
21976 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21977 rest_of_type_compilation (t, 1);
21978 }
21979 }
21980
21981 /* Called from do_type_instantiation through binding_table_foreach to
21982 do recursive instantiation for the type bound in ENTRY. */
21983 static void
21984 bt_instantiate_type_proc (binding_entry entry, void *data)
21985 {
21986 tree storage = *(tree *) data;
21987
21988 if (MAYBE_CLASS_TYPE_P (entry->type)
21989 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
21990 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
21991 }
21992
21993 /* Called from do_type_instantiation to instantiate a member
21994 (a member function or a static member variable) of an
21995 explicitly instantiated class template. */
21996 static void
21997 instantiate_class_member (tree decl, int extern_p)
21998 {
21999 mark_decl_instantiated (decl, extern_p);
22000 if (! extern_p)
22001 instantiate_decl (decl, /*defer_ok=*/true,
22002 /*expl_inst_class_mem_p=*/true);
22003 }
22004
22005 /* Perform an explicit instantiation of template class T. STORAGE, if
22006 non-null, is the RID for extern, inline or static. COMPLAIN is
22007 nonzero if this is called from the parser, zero if called recursively,
22008 since the standard is unclear (as detailed below). */
22009
22010 void
22011 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22012 {
22013 int extern_p = 0;
22014 int nomem_p = 0;
22015 int static_p = 0;
22016 int previous_instantiation_extern_p = 0;
22017
22018 if (TREE_CODE (t) == TYPE_DECL)
22019 t = TREE_TYPE (t);
22020
22021 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22022 {
22023 tree tmpl =
22024 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22025 if (tmpl)
22026 error ("explicit instantiation of non-class template %qD", tmpl);
22027 else
22028 error ("explicit instantiation of non-template type %qT", t);
22029 return;
22030 }
22031
22032 complete_type (t);
22033
22034 if (!COMPLETE_TYPE_P (t))
22035 {
22036 if (complain & tf_error)
22037 error ("explicit instantiation of %q#T before definition of template",
22038 t);
22039 return;
22040 }
22041
22042 if (storage != NULL_TREE)
22043 {
22044 if (!in_system_header_at (input_location))
22045 {
22046 if (storage == ridpointers[(int) RID_EXTERN])
22047 {
22048 if (cxx_dialect == cxx98)
22049 pedwarn (input_location, OPT_Wpedantic,
22050 "ISO C++ 1998 forbids the use of %<extern%> on "
22051 "explicit instantiations");
22052 }
22053 else
22054 pedwarn (input_location, OPT_Wpedantic,
22055 "ISO C++ forbids the use of %qE"
22056 " on explicit instantiations", storage);
22057 }
22058
22059 if (storage == ridpointers[(int) RID_INLINE])
22060 nomem_p = 1;
22061 else if (storage == ridpointers[(int) RID_EXTERN])
22062 extern_p = 1;
22063 else if (storage == ridpointers[(int) RID_STATIC])
22064 static_p = 1;
22065 else
22066 {
22067 error ("storage class %qD applied to template instantiation",
22068 storage);
22069 extern_p = 0;
22070 }
22071 }
22072
22073 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22074 {
22075 /* DR 259 [temp.spec].
22076
22077 Both an explicit instantiation and a declaration of an explicit
22078 specialization shall not appear in a program unless the explicit
22079 instantiation follows a declaration of the explicit specialization.
22080
22081 For a given set of template parameters, if an explicit
22082 instantiation of a template appears after a declaration of an
22083 explicit specialization for that template, the explicit
22084 instantiation has no effect. */
22085 return;
22086 }
22087 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22088 {
22089 /* [temp.spec]
22090
22091 No program shall explicitly instantiate any template more
22092 than once.
22093
22094 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22095 instantiation was `extern'. If EXTERN_P then the second is.
22096 These cases are OK. */
22097 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22098
22099 if (!previous_instantiation_extern_p && !extern_p
22100 && (complain & tf_error))
22101 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22102
22103 /* If we've already instantiated the template, just return now. */
22104 if (!CLASSTYPE_INTERFACE_ONLY (t))
22105 return;
22106 }
22107
22108 check_explicit_instantiation_namespace (TYPE_NAME (t));
22109 mark_class_instantiated (t, extern_p);
22110
22111 if (nomem_p)
22112 return;
22113
22114 {
22115 tree tmp;
22116
22117 /* In contrast to implicit instantiation, where only the
22118 declarations, and not the definitions, of members are
22119 instantiated, we have here:
22120
22121 [temp.explicit]
22122
22123 The explicit instantiation of a class template specialization
22124 implies the instantiation of all of its members not
22125 previously explicitly specialized in the translation unit
22126 containing the explicit instantiation.
22127
22128 Of course, we can't instantiate member template classes, since
22129 we don't have any arguments for them. Note that the standard
22130 is unclear on whether the instantiation of the members are
22131 *explicit* instantiations or not. However, the most natural
22132 interpretation is that it should be an explicit instantiation. */
22133
22134 if (! static_p)
22135 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22136 if (TREE_CODE (tmp) == FUNCTION_DECL
22137 && DECL_TEMPLATE_INSTANTIATION (tmp)
22138 && user_provided_p (tmp))
22139 instantiate_class_member (tmp, extern_p);
22140
22141 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22142 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22143 instantiate_class_member (tmp, extern_p);
22144
22145 if (CLASSTYPE_NESTED_UTDS (t))
22146 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22147 bt_instantiate_type_proc, &storage);
22148 }
22149 }
22150
22151 /* Given a function DECL, which is a specialization of TMPL, modify
22152 DECL to be a re-instantiation of TMPL with the same template
22153 arguments. TMPL should be the template into which tsubst'ing
22154 should occur for DECL, not the most general template.
22155
22156 One reason for doing this is a scenario like this:
22157
22158 template <class T>
22159 void f(const T&, int i);
22160
22161 void g() { f(3, 7); }
22162
22163 template <class T>
22164 void f(const T& t, const int i) { }
22165
22166 Note that when the template is first instantiated, with
22167 instantiate_template, the resulting DECL will have no name for the
22168 first parameter, and the wrong type for the second. So, when we go
22169 to instantiate the DECL, we regenerate it. */
22170
22171 static void
22172 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22173 {
22174 /* The arguments used to instantiate DECL, from the most general
22175 template. */
22176 tree code_pattern;
22177
22178 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22179
22180 /* Make sure that we can see identifiers, and compute access
22181 correctly. */
22182 push_access_scope (decl);
22183
22184 if (TREE_CODE (decl) == FUNCTION_DECL)
22185 {
22186 tree decl_parm;
22187 tree pattern_parm;
22188 tree specs;
22189 int args_depth;
22190 int parms_depth;
22191
22192 args_depth = TMPL_ARGS_DEPTH (args);
22193 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22194 if (args_depth > parms_depth)
22195 args = get_innermost_template_args (args, parms_depth);
22196
22197 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22198 args, tf_error, NULL_TREE,
22199 /*defer_ok*/false);
22200 if (specs && specs != error_mark_node)
22201 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22202 specs);
22203
22204 /* Merge parameter declarations. */
22205 decl_parm = skip_artificial_parms_for (decl,
22206 DECL_ARGUMENTS (decl));
22207 pattern_parm
22208 = skip_artificial_parms_for (code_pattern,
22209 DECL_ARGUMENTS (code_pattern));
22210 while (decl_parm && !DECL_PACK_P (pattern_parm))
22211 {
22212 tree parm_type;
22213 tree attributes;
22214
22215 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22216 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22217 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22218 NULL_TREE);
22219 parm_type = type_decays_to (parm_type);
22220 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22221 TREE_TYPE (decl_parm) = parm_type;
22222 attributes = DECL_ATTRIBUTES (pattern_parm);
22223 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22224 {
22225 DECL_ATTRIBUTES (decl_parm) = attributes;
22226 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22227 }
22228 decl_parm = DECL_CHAIN (decl_parm);
22229 pattern_parm = DECL_CHAIN (pattern_parm);
22230 }
22231 /* Merge any parameters that match with the function parameter
22232 pack. */
22233 if (pattern_parm && DECL_PACK_P (pattern_parm))
22234 {
22235 int i, len;
22236 tree expanded_types;
22237 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22238 the parameters in this function parameter pack. */
22239 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22240 args, tf_error, NULL_TREE);
22241 len = TREE_VEC_LENGTH (expanded_types);
22242 for (i = 0; i < len; i++)
22243 {
22244 tree parm_type;
22245 tree attributes;
22246
22247 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22248 /* Rename the parameter to include the index. */
22249 DECL_NAME (decl_parm) =
22250 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22251 parm_type = TREE_VEC_ELT (expanded_types, i);
22252 parm_type = type_decays_to (parm_type);
22253 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22254 TREE_TYPE (decl_parm) = parm_type;
22255 attributes = DECL_ATTRIBUTES (pattern_parm);
22256 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22257 {
22258 DECL_ATTRIBUTES (decl_parm) = attributes;
22259 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22260 }
22261 decl_parm = DECL_CHAIN (decl_parm);
22262 }
22263 }
22264 /* Merge additional specifiers from the CODE_PATTERN. */
22265 if (DECL_DECLARED_INLINE_P (code_pattern)
22266 && !DECL_DECLARED_INLINE_P (decl))
22267 DECL_DECLARED_INLINE_P (decl) = 1;
22268 }
22269 else if (VAR_P (decl))
22270 {
22271 DECL_INITIAL (decl) =
22272 tsubst_expr (DECL_INITIAL (code_pattern), args,
22273 tf_error, DECL_TI_TEMPLATE (decl),
22274 /*integral_constant_expression_p=*/false);
22275 if (VAR_HAD_UNKNOWN_BOUND (decl))
22276 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22277 tf_error, DECL_TI_TEMPLATE (decl));
22278 }
22279 else
22280 gcc_unreachable ();
22281
22282 pop_access_scope (decl);
22283 }
22284
22285 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22286 substituted to get DECL. */
22287
22288 tree
22289 template_for_substitution (tree decl)
22290 {
22291 tree tmpl = DECL_TI_TEMPLATE (decl);
22292
22293 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22294 for the instantiation. This is not always the most general
22295 template. Consider, for example:
22296
22297 template <class T>
22298 struct S { template <class U> void f();
22299 template <> void f<int>(); };
22300
22301 and an instantiation of S<double>::f<int>. We want TD to be the
22302 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22303 while (/* An instantiation cannot have a definition, so we need a
22304 more general template. */
22305 DECL_TEMPLATE_INSTANTIATION (tmpl)
22306 /* We must also deal with friend templates. Given:
22307
22308 template <class T> struct S {
22309 template <class U> friend void f() {};
22310 };
22311
22312 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22313 so far as the language is concerned, but that's still
22314 where we get the pattern for the instantiation from. On
22315 other hand, if the definition comes outside the class, say:
22316
22317 template <class T> struct S {
22318 template <class U> friend void f();
22319 };
22320 template <class U> friend void f() {}
22321
22322 we don't need to look any further. That's what the check for
22323 DECL_INITIAL is for. */
22324 || (TREE_CODE (decl) == FUNCTION_DECL
22325 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22326 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22327 {
22328 /* The present template, TD, should not be a definition. If it
22329 were a definition, we should be using it! Note that we
22330 cannot restructure the loop to just keep going until we find
22331 a template with a definition, since that might go too far if
22332 a specialization was declared, but not defined. */
22333
22334 /* Fetch the more general template. */
22335 tmpl = DECL_TI_TEMPLATE (tmpl);
22336 }
22337
22338 return tmpl;
22339 }
22340
22341 /* Returns true if we need to instantiate this template instance even if we
22342 know we aren't going to emit it. */
22343
22344 bool
22345 always_instantiate_p (tree decl)
22346 {
22347 /* We always instantiate inline functions so that we can inline them. An
22348 explicit instantiation declaration prohibits implicit instantiation of
22349 non-inline functions. With high levels of optimization, we would
22350 normally inline non-inline functions -- but we're not allowed to do
22351 that for "extern template" functions. Therefore, we check
22352 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22353 return ((TREE_CODE (decl) == FUNCTION_DECL
22354 && (DECL_DECLARED_INLINE_P (decl)
22355 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22356 /* And we need to instantiate static data members so that
22357 their initializers are available in integral constant
22358 expressions. */
22359 || (VAR_P (decl)
22360 && decl_maybe_constant_var_p (decl)));
22361 }
22362
22363 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22364 instantiate it now, modifying TREE_TYPE (fn). */
22365
22366 void
22367 maybe_instantiate_noexcept (tree fn)
22368 {
22369 tree fntype, spec, noex, clone;
22370
22371 /* Don't instantiate a noexcept-specification from template context. */
22372 if (processing_template_decl)
22373 return;
22374
22375 if (DECL_CLONED_FUNCTION_P (fn))
22376 fn = DECL_CLONED_FUNCTION (fn);
22377 fntype = TREE_TYPE (fn);
22378 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22379
22380 if (!spec || !TREE_PURPOSE (spec))
22381 return;
22382
22383 noex = TREE_PURPOSE (spec);
22384
22385 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22386 {
22387 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22388 spec = get_defaulted_eh_spec (fn);
22389 else if (push_tinst_level (fn))
22390 {
22391 push_access_scope (fn);
22392 push_deferring_access_checks (dk_no_deferred);
22393 input_location = DECL_SOURCE_LOCATION (fn);
22394 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22395 DEFERRED_NOEXCEPT_ARGS (noex),
22396 tf_warning_or_error, fn,
22397 /*function_p=*/false,
22398 /*integral_constant_expression_p=*/true);
22399 pop_deferring_access_checks ();
22400 pop_access_scope (fn);
22401 pop_tinst_level ();
22402 spec = build_noexcept_spec (noex, tf_warning_or_error);
22403 if (spec == error_mark_node)
22404 spec = noexcept_false_spec;
22405 }
22406 else
22407 spec = noexcept_false_spec;
22408
22409 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22410 }
22411
22412 FOR_EACH_CLONE (clone, fn)
22413 {
22414 if (TREE_TYPE (clone) == fntype)
22415 TREE_TYPE (clone) = TREE_TYPE (fn);
22416 else
22417 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22418 }
22419 }
22420
22421 /* Produce the definition of D, a _DECL generated from a template. If
22422 DEFER_OK is true, then we don't have to actually do the
22423 instantiation now; we just have to do it sometime. Normally it is
22424 an error if this is an explicit instantiation but D is undefined.
22425 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22426 instantiated class template. */
22427
22428 tree
22429 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22430 {
22431 tree tmpl = DECL_TI_TEMPLATE (d);
22432 tree gen_args;
22433 tree args;
22434 tree td;
22435 tree code_pattern;
22436 tree spec;
22437 tree gen_tmpl;
22438 bool pattern_defined;
22439 location_t saved_loc = input_location;
22440 int saved_unevaluated_operand = cp_unevaluated_operand;
22441 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22442 bool external_p;
22443 bool deleted_p;
22444
22445 /* This function should only be used to instantiate templates for
22446 functions and static member variables. */
22447 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22448
22449 /* A concept is never instantiated. */
22450 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22451
22452 /* Variables are never deferred; if instantiation is required, they
22453 are instantiated right away. That allows for better code in the
22454 case that an expression refers to the value of the variable --
22455 if the variable has a constant value the referring expression can
22456 take advantage of that fact. */
22457 if (VAR_P (d))
22458 defer_ok = false;
22459
22460 /* Don't instantiate cloned functions. Instead, instantiate the
22461 functions they cloned. */
22462 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22463 d = DECL_CLONED_FUNCTION (d);
22464
22465 if (DECL_TEMPLATE_INSTANTIATED (d)
22466 || (TREE_CODE (d) == FUNCTION_DECL
22467 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22468 || DECL_TEMPLATE_SPECIALIZATION (d))
22469 /* D has already been instantiated or explicitly specialized, so
22470 there's nothing for us to do here.
22471
22472 It might seem reasonable to check whether or not D is an explicit
22473 instantiation, and, if so, stop here. But when an explicit
22474 instantiation is deferred until the end of the compilation,
22475 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22476 the instantiation. */
22477 return d;
22478
22479 /* Check to see whether we know that this template will be
22480 instantiated in some other file, as with "extern template"
22481 extension. */
22482 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22483
22484 /* In general, we do not instantiate such templates. */
22485 if (external_p && !always_instantiate_p (d))
22486 return d;
22487
22488 gen_tmpl = most_general_template (tmpl);
22489 gen_args = DECL_TI_ARGS (d);
22490
22491 if (tmpl != gen_tmpl)
22492 /* We should already have the extra args. */
22493 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22494 == TMPL_ARGS_DEPTH (gen_args));
22495 /* And what's in the hash table should match D. */
22496 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22497 || spec == NULL_TREE);
22498
22499 /* This needs to happen before any tsubsting. */
22500 if (! push_tinst_level (d))
22501 return d;
22502
22503 timevar_push (TV_TEMPLATE_INST);
22504
22505 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22506 for the instantiation. */
22507 td = template_for_substitution (d);
22508 args = gen_args;
22509
22510 if (VAR_P (d))
22511 {
22512 /* Look up an explicit specialization, if any. */
22513 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22514 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22515 if (elt && elt != error_mark_node)
22516 {
22517 td = TREE_VALUE (elt);
22518 args = TREE_PURPOSE (elt);
22519 }
22520 }
22521
22522 code_pattern = DECL_TEMPLATE_RESULT (td);
22523
22524 /* We should never be trying to instantiate a member of a class
22525 template or partial specialization. */
22526 gcc_assert (d != code_pattern);
22527
22528 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22529 || DECL_TEMPLATE_SPECIALIZATION (td))
22530 /* In the case of a friend template whose definition is provided
22531 outside the class, we may have too many arguments. Drop the
22532 ones we don't need. The same is true for specializations. */
22533 args = get_innermost_template_args
22534 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22535
22536 if (TREE_CODE (d) == FUNCTION_DECL)
22537 {
22538 deleted_p = DECL_DELETED_FN (code_pattern);
22539 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22540 && DECL_INITIAL (code_pattern) != error_mark_node)
22541 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22542 || deleted_p);
22543 }
22544 else
22545 {
22546 deleted_p = false;
22547 if (DECL_CLASS_SCOPE_P (code_pattern))
22548 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22549 || DECL_INLINE_VAR_P (code_pattern));
22550 else
22551 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22552 }
22553
22554 /* We may be in the middle of deferred access check. Disable it now. */
22555 push_deferring_access_checks (dk_no_deferred);
22556
22557 /* Unless an explicit instantiation directive has already determined
22558 the linkage of D, remember that a definition is available for
22559 this entity. */
22560 if (pattern_defined
22561 && !DECL_INTERFACE_KNOWN (d)
22562 && !DECL_NOT_REALLY_EXTERN (d))
22563 mark_definable (d);
22564
22565 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22566 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22567 input_location = DECL_SOURCE_LOCATION (d);
22568
22569 /* If D is a member of an explicitly instantiated class template,
22570 and no definition is available, treat it like an implicit
22571 instantiation. */
22572 if (!pattern_defined && expl_inst_class_mem_p
22573 && DECL_EXPLICIT_INSTANTIATION (d))
22574 {
22575 /* Leave linkage flags alone on instantiations with anonymous
22576 visibility. */
22577 if (TREE_PUBLIC (d))
22578 {
22579 DECL_NOT_REALLY_EXTERN (d) = 0;
22580 DECL_INTERFACE_KNOWN (d) = 0;
22581 }
22582 SET_DECL_IMPLICIT_INSTANTIATION (d);
22583 }
22584
22585 /* Defer all other templates, unless we have been explicitly
22586 forbidden from doing so. */
22587 if (/* If there is no definition, we cannot instantiate the
22588 template. */
22589 ! pattern_defined
22590 /* If it's OK to postpone instantiation, do so. */
22591 || defer_ok
22592 /* If this is a static data member that will be defined
22593 elsewhere, we don't want to instantiate the entire data
22594 member, but we do want to instantiate the initializer so that
22595 we can substitute that elsewhere. */
22596 || (external_p && VAR_P (d))
22597 /* Handle here a deleted function too, avoid generating
22598 its body (c++/61080). */
22599 || deleted_p)
22600 {
22601 /* The definition of the static data member is now required so
22602 we must substitute the initializer. */
22603 if (VAR_P (d)
22604 && !DECL_INITIAL (d)
22605 && DECL_INITIAL (code_pattern))
22606 {
22607 tree ns;
22608 tree init;
22609 bool const_init = false;
22610 bool enter_context = DECL_CLASS_SCOPE_P (d);
22611
22612 ns = decl_namespace_context (d);
22613 push_nested_namespace (ns);
22614 if (enter_context)
22615 push_nested_class (DECL_CONTEXT (d));
22616 init = tsubst_expr (DECL_INITIAL (code_pattern),
22617 args,
22618 tf_warning_or_error, NULL_TREE,
22619 /*integral_constant_expression_p=*/false);
22620 /* If instantiating the initializer involved instantiating this
22621 again, don't call cp_finish_decl twice. */
22622 if (!DECL_INITIAL (d))
22623 {
22624 /* Make sure the initializer is still constant, in case of
22625 circular dependency (template/instantiate6.C). */
22626 const_init
22627 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22628 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22629 /*asmspec_tree=*/NULL_TREE,
22630 LOOKUP_ONLYCONVERTING);
22631 }
22632 if (enter_context)
22633 pop_nested_class ();
22634 pop_nested_namespace (ns);
22635 }
22636
22637 /* We restore the source position here because it's used by
22638 add_pending_template. */
22639 input_location = saved_loc;
22640
22641 if (at_eof && !pattern_defined
22642 && DECL_EXPLICIT_INSTANTIATION (d)
22643 && DECL_NOT_REALLY_EXTERN (d))
22644 /* [temp.explicit]
22645
22646 The definition of a non-exported function template, a
22647 non-exported member function template, or a non-exported
22648 member function or static data member of a class template
22649 shall be present in every translation unit in which it is
22650 explicitly instantiated. */
22651 permerror (input_location, "explicit instantiation of %qD "
22652 "but no definition available", d);
22653
22654 /* If we're in unevaluated context, we just wanted to get the
22655 constant value; this isn't an odr use, so don't queue
22656 a full instantiation. */
22657 if (cp_unevaluated_operand != 0)
22658 goto out;
22659 /* ??? Historically, we have instantiated inline functions, even
22660 when marked as "extern template". */
22661 if (!(external_p && VAR_P (d)))
22662 add_pending_template (d);
22663 goto out;
22664 }
22665 /* Tell the repository that D is available in this translation unit
22666 -- and see if it is supposed to be instantiated here. */
22667 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22668 {
22669 /* In a PCH file, despite the fact that the repository hasn't
22670 requested instantiation in the PCH it is still possible that
22671 an instantiation will be required in a file that includes the
22672 PCH. */
22673 if (pch_file)
22674 add_pending_template (d);
22675 /* Instantiate inline functions so that the inliner can do its
22676 job, even though we'll not be emitting a copy of this
22677 function. */
22678 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22679 goto out;
22680 }
22681
22682 bool push_to_top, nested;
22683 tree fn_context;
22684 fn_context = decl_function_context (d);
22685 nested = current_function_decl != NULL_TREE;
22686 push_to_top = !(nested && fn_context == current_function_decl);
22687
22688 vec<tree> omp_privatization_save;
22689 if (nested)
22690 save_omp_privatization_clauses (omp_privatization_save);
22691
22692 if (push_to_top)
22693 push_to_top_level ();
22694 else
22695 {
22696 push_function_context ();
22697 cp_unevaluated_operand = 0;
22698 c_inhibit_evaluation_warnings = 0;
22699 }
22700
22701 /* Mark D as instantiated so that recursive calls to
22702 instantiate_decl do not try to instantiate it again. */
22703 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22704
22705 /* Regenerate the declaration in case the template has been modified
22706 by a subsequent redeclaration. */
22707 regenerate_decl_from_template (d, td, args);
22708
22709 /* We already set the file and line above. Reset them now in case
22710 they changed as a result of calling regenerate_decl_from_template. */
22711 input_location = DECL_SOURCE_LOCATION (d);
22712
22713 if (VAR_P (d))
22714 {
22715 tree init;
22716 bool const_init = false;
22717
22718 /* Clear out DECL_RTL; whatever was there before may not be right
22719 since we've reset the type of the declaration. */
22720 SET_DECL_RTL (d, NULL);
22721 DECL_IN_AGGR_P (d) = 0;
22722
22723 /* The initializer is placed in DECL_INITIAL by
22724 regenerate_decl_from_template so we don't need to
22725 push/pop_access_scope again here. Pull it out so that
22726 cp_finish_decl can process it. */
22727 init = DECL_INITIAL (d);
22728 DECL_INITIAL (d) = NULL_TREE;
22729 DECL_INITIALIZED_P (d) = 0;
22730
22731 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22732 initializer. That function will defer actual emission until
22733 we have a chance to determine linkage. */
22734 DECL_EXTERNAL (d) = 0;
22735
22736 /* Enter the scope of D so that access-checking works correctly. */
22737 bool enter_context = DECL_CLASS_SCOPE_P (d);
22738 if (enter_context)
22739 push_nested_class (DECL_CONTEXT (d));
22740
22741 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22742 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22743
22744 if (enter_context)
22745 pop_nested_class ();
22746
22747 if (variable_template_p (gen_tmpl))
22748 note_variable_template_instantiation (d);
22749 }
22750 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22751 synthesize_method (d);
22752 else if (TREE_CODE (d) == FUNCTION_DECL)
22753 {
22754 hash_map<tree, tree> *saved_local_specializations;
22755 tree tmpl_parm;
22756 tree spec_parm;
22757 tree block = NULL_TREE;
22758 tree lambda_ctx = NULL_TREE;
22759
22760 /* Save away the current list, in case we are instantiating one
22761 template from within the body of another. */
22762 saved_local_specializations = local_specializations;
22763
22764 /* Set up the list of local specializations. */
22765 local_specializations = new hash_map<tree, tree>;
22766
22767 /* Set up context. */
22768 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22769 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22770 block = push_stmt_list ();
22771 else
22772 {
22773 if (push_to_top && LAMBDA_FUNCTION_P (d))
22774 {
22775 /* When instantiating a lambda's templated function
22776 operator, we need to push the non-lambda class scope
22777 of the lambda itself so that the nested function
22778 stack is sufficiently correct to deal with this
22779 capture. */
22780 lambda_ctx = DECL_CONTEXT (d);
22781 do
22782 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22783 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22784 if (lambda_ctx)
22785 push_nested_class (lambda_ctx);
22786 }
22787 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22788 }
22789
22790 /* Some typedefs referenced from within the template code need to be
22791 access checked at template instantiation time, i.e now. These
22792 types were added to the template at parsing time. Let's get those
22793 and perform the access checks then. */
22794 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22795 args);
22796
22797 /* Create substitution entries for the parameters. */
22798 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22799 spec_parm = DECL_ARGUMENTS (d);
22800 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22801 {
22802 register_local_specialization (spec_parm, tmpl_parm);
22803 spec_parm = skip_artificial_parms_for (d, spec_parm);
22804 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22805 }
22806 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22807 {
22808 if (!DECL_PACK_P (tmpl_parm))
22809 {
22810 register_local_specialization (spec_parm, tmpl_parm);
22811 spec_parm = DECL_CHAIN (spec_parm);
22812 }
22813 else
22814 {
22815 /* Register the (value) argument pack as a specialization of
22816 TMPL_PARM, then move on. */
22817 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22818 register_local_specialization (argpack, tmpl_parm);
22819 }
22820 }
22821 gcc_assert (!spec_parm);
22822
22823 /* Substitute into the body of the function. */
22824 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22825 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22826 tf_warning_or_error, tmpl);
22827 else
22828 {
22829 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22830 tf_warning_or_error, tmpl,
22831 /*integral_constant_expression_p=*/false);
22832
22833 /* Set the current input_location to the end of the function
22834 so that finish_function knows where we are. */
22835 input_location
22836 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22837
22838 /* Remember if we saw an infinite loop in the template. */
22839 current_function_infinite_loop
22840 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22841 }
22842
22843 /* We don't need the local specializations any more. */
22844 delete local_specializations;
22845 local_specializations = saved_local_specializations;
22846
22847 /* Finish the function. */
22848 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22849 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22850 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22851 else
22852 {
22853 d = finish_function (0);
22854 expand_or_defer_fn (d);
22855 }
22856 if (lambda_ctx)
22857 pop_nested_class ();
22858
22859 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22860 cp_check_omp_declare_reduction (d);
22861 }
22862
22863 /* We're not deferring instantiation any more. */
22864 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22865
22866 if (push_to_top)
22867 pop_from_top_level ();
22868 else
22869 pop_function_context ();
22870
22871 if (nested)
22872 restore_omp_privatization_clauses (omp_privatization_save);
22873
22874 out:
22875 pop_deferring_access_checks ();
22876 timevar_pop (TV_TEMPLATE_INST);
22877 pop_tinst_level ();
22878 input_location = saved_loc;
22879 cp_unevaluated_operand = saved_unevaluated_operand;
22880 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22881
22882 return d;
22883 }
22884
22885 /* Run through the list of templates that we wish we could
22886 instantiate, and instantiate any we can. RETRIES is the
22887 number of times we retry pending template instantiation. */
22888
22889 void
22890 instantiate_pending_templates (int retries)
22891 {
22892 int reconsider;
22893 location_t saved_loc = input_location;
22894
22895 /* Instantiating templates may trigger vtable generation. This in turn
22896 may require further template instantiations. We place a limit here
22897 to avoid infinite loop. */
22898 if (pending_templates && retries >= max_tinst_depth)
22899 {
22900 tree decl = pending_templates->tinst->decl;
22901
22902 fatal_error (input_location,
22903 "template instantiation depth exceeds maximum of %d"
22904 " instantiating %q+D, possibly from virtual table generation"
22905 " (use -ftemplate-depth= to increase the maximum)",
22906 max_tinst_depth, decl);
22907 if (TREE_CODE (decl) == FUNCTION_DECL)
22908 /* Pretend that we defined it. */
22909 DECL_INITIAL (decl) = error_mark_node;
22910 return;
22911 }
22912
22913 do
22914 {
22915 struct pending_template **t = &pending_templates;
22916 struct pending_template *last = NULL;
22917 reconsider = 0;
22918 while (*t)
22919 {
22920 tree instantiation = reopen_tinst_level ((*t)->tinst);
22921 bool complete = false;
22922
22923 if (TYPE_P (instantiation))
22924 {
22925 tree fn;
22926
22927 if (!COMPLETE_TYPE_P (instantiation))
22928 {
22929 instantiate_class_template (instantiation);
22930 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22931 for (fn = TYPE_METHODS (instantiation);
22932 fn;
22933 fn = TREE_CHAIN (fn))
22934 if (! DECL_ARTIFICIAL (fn))
22935 instantiate_decl (fn,
22936 /*defer_ok=*/false,
22937 /*expl_inst_class_mem_p=*/false);
22938 if (COMPLETE_TYPE_P (instantiation))
22939 reconsider = 1;
22940 }
22941
22942 complete = COMPLETE_TYPE_P (instantiation);
22943 }
22944 else
22945 {
22946 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22947 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22948 {
22949 instantiation
22950 = instantiate_decl (instantiation,
22951 /*defer_ok=*/false,
22952 /*expl_inst_class_mem_p=*/false);
22953 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22954 reconsider = 1;
22955 }
22956
22957 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22958 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22959 }
22960
22961 if (complete)
22962 /* If INSTANTIATION has been instantiated, then we don't
22963 need to consider it again in the future. */
22964 *t = (*t)->next;
22965 else
22966 {
22967 last = *t;
22968 t = &(*t)->next;
22969 }
22970 tinst_depth = 0;
22971 current_tinst_level = NULL;
22972 }
22973 last_pending_template = last;
22974 }
22975 while (reconsider);
22976
22977 input_location = saved_loc;
22978 }
22979
22980 /* Substitute ARGVEC into T, which is a list of initializers for
22981 either base class or a non-static data member. The TREE_PURPOSEs
22982 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22983 instantiate_decl. */
22984
22985 static tree
22986 tsubst_initializer_list (tree t, tree argvec)
22987 {
22988 tree inits = NULL_TREE;
22989
22990 for (; t; t = TREE_CHAIN (t))
22991 {
22992 tree decl;
22993 tree init;
22994 tree expanded_bases = NULL_TREE;
22995 tree expanded_arguments = NULL_TREE;
22996 int i, len = 1;
22997
22998 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
22999 {
23000 tree expr;
23001 tree arg;
23002
23003 /* Expand the base class expansion type into separate base
23004 classes. */
23005 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23006 tf_warning_or_error,
23007 NULL_TREE);
23008 if (expanded_bases == error_mark_node)
23009 continue;
23010
23011 /* We'll be building separate TREE_LISTs of arguments for
23012 each base. */
23013 len = TREE_VEC_LENGTH (expanded_bases);
23014 expanded_arguments = make_tree_vec (len);
23015 for (i = 0; i < len; i++)
23016 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23017
23018 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23019 expand each argument in the TREE_VALUE of t. */
23020 expr = make_node (EXPR_PACK_EXPANSION);
23021 PACK_EXPANSION_LOCAL_P (expr) = true;
23022 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23023 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23024
23025 if (TREE_VALUE (t) == void_type_node)
23026 /* VOID_TYPE_NODE is used to indicate
23027 value-initialization. */
23028 {
23029 for (i = 0; i < len; i++)
23030 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23031 }
23032 else
23033 {
23034 /* Substitute parameter packs into each argument in the
23035 TREE_LIST. */
23036 in_base_initializer = 1;
23037 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23038 {
23039 tree expanded_exprs;
23040
23041 /* Expand the argument. */
23042 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23043 expanded_exprs
23044 = tsubst_pack_expansion (expr, argvec,
23045 tf_warning_or_error,
23046 NULL_TREE);
23047 if (expanded_exprs == error_mark_node)
23048 continue;
23049
23050 /* Prepend each of the expanded expressions to the
23051 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23052 for (i = 0; i < len; i++)
23053 {
23054 TREE_VEC_ELT (expanded_arguments, i) =
23055 tree_cons (NULL_TREE,
23056 TREE_VEC_ELT (expanded_exprs, i),
23057 TREE_VEC_ELT (expanded_arguments, i));
23058 }
23059 }
23060 in_base_initializer = 0;
23061
23062 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23063 since we built them backwards. */
23064 for (i = 0; i < len; i++)
23065 {
23066 TREE_VEC_ELT (expanded_arguments, i) =
23067 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23068 }
23069 }
23070 }
23071
23072 for (i = 0; i < len; ++i)
23073 {
23074 if (expanded_bases)
23075 {
23076 decl = TREE_VEC_ELT (expanded_bases, i);
23077 decl = expand_member_init (decl);
23078 init = TREE_VEC_ELT (expanded_arguments, i);
23079 }
23080 else
23081 {
23082 tree tmp;
23083 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23084 tf_warning_or_error, NULL_TREE);
23085
23086 decl = expand_member_init (decl);
23087 if (decl && !DECL_P (decl))
23088 in_base_initializer = 1;
23089
23090 init = TREE_VALUE (t);
23091 tmp = init;
23092 if (init != void_type_node)
23093 init = tsubst_expr (init, argvec,
23094 tf_warning_or_error, NULL_TREE,
23095 /*integral_constant_expression_p=*/false);
23096 if (init == NULL_TREE && tmp != NULL_TREE)
23097 /* If we had an initializer but it instantiated to nothing,
23098 value-initialize the object. This will only occur when
23099 the initializer was a pack expansion where the parameter
23100 packs used in that expansion were of length zero. */
23101 init = void_type_node;
23102 in_base_initializer = 0;
23103 }
23104
23105 if (decl)
23106 {
23107 init = build_tree_list (decl, init);
23108 TREE_CHAIN (init) = inits;
23109 inits = init;
23110 }
23111 }
23112 }
23113 return inits;
23114 }
23115
23116 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23117
23118 static void
23119 set_current_access_from_decl (tree decl)
23120 {
23121 if (TREE_PRIVATE (decl))
23122 current_access_specifier = access_private_node;
23123 else if (TREE_PROTECTED (decl))
23124 current_access_specifier = access_protected_node;
23125 else
23126 current_access_specifier = access_public_node;
23127 }
23128
23129 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23130 is the instantiation (which should have been created with
23131 start_enum) and ARGS are the template arguments to use. */
23132
23133 static void
23134 tsubst_enum (tree tag, tree newtag, tree args)
23135 {
23136 tree e;
23137
23138 if (SCOPED_ENUM_P (newtag))
23139 begin_scope (sk_scoped_enum, newtag);
23140
23141 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23142 {
23143 tree value;
23144 tree decl;
23145
23146 decl = TREE_VALUE (e);
23147 /* Note that in a template enum, the TREE_VALUE is the
23148 CONST_DECL, not the corresponding INTEGER_CST. */
23149 value = tsubst_expr (DECL_INITIAL (decl),
23150 args, tf_warning_or_error, NULL_TREE,
23151 /*integral_constant_expression_p=*/true);
23152
23153 /* Give this enumeration constant the correct access. */
23154 set_current_access_from_decl (decl);
23155
23156 /* Actually build the enumerator itself. Here we're assuming that
23157 enumerators can't have dependent attributes. */
23158 build_enumerator (DECL_NAME (decl), value, newtag,
23159 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23160 }
23161
23162 if (SCOPED_ENUM_P (newtag))
23163 finish_scope ();
23164
23165 finish_enum_value_list (newtag);
23166 finish_enum (newtag);
23167
23168 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23169 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23170 }
23171
23172 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23173 its type -- but without substituting the innermost set of template
23174 arguments. So, innermost set of template parameters will appear in
23175 the type. */
23176
23177 tree
23178 get_mostly_instantiated_function_type (tree decl)
23179 {
23180 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23181 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23182 }
23183
23184 /* Return truthvalue if we're processing a template different from
23185 the last one involved in diagnostics. */
23186 bool
23187 problematic_instantiation_changed (void)
23188 {
23189 return current_tinst_level != last_error_tinst_level;
23190 }
23191
23192 /* Remember current template involved in diagnostics. */
23193 void
23194 record_last_problematic_instantiation (void)
23195 {
23196 last_error_tinst_level = current_tinst_level;
23197 }
23198
23199 struct tinst_level *
23200 current_instantiation (void)
23201 {
23202 return current_tinst_level;
23203 }
23204
23205 /* Return TRUE if current_function_decl is being instantiated, false
23206 otherwise. */
23207
23208 bool
23209 instantiating_current_function_p (void)
23210 {
23211 return (current_instantiation ()
23212 && current_instantiation ()->decl == current_function_decl);
23213 }
23214
23215 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23216 type. Return zero for ok, nonzero for disallowed. Issue error and
23217 warning messages under control of COMPLAIN. */
23218
23219 static int
23220 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23221 {
23222 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23223 return 0;
23224 else if (POINTER_TYPE_P (type))
23225 return 0;
23226 else if (TYPE_PTRMEM_P (type))
23227 return 0;
23228 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23229 return 0;
23230 else if (TREE_CODE (type) == TYPENAME_TYPE)
23231 return 0;
23232 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23233 return 0;
23234 else if (TREE_CODE (type) == NULLPTR_TYPE)
23235 return 0;
23236 /* A bound template template parm could later be instantiated to have a valid
23237 nontype parm type via an alias template. */
23238 else if (cxx_dialect >= cxx11
23239 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23240 return 0;
23241
23242 if (complain & tf_error)
23243 {
23244 if (type == error_mark_node)
23245 inform (input_location, "invalid template non-type parameter");
23246 else
23247 error ("%q#T is not a valid type for a template non-type parameter",
23248 type);
23249 }
23250 return 1;
23251 }
23252
23253 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23254 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23255
23256 static bool
23257 dependent_type_p_r (tree type)
23258 {
23259 tree scope;
23260
23261 /* [temp.dep.type]
23262
23263 A type is dependent if it is:
23264
23265 -- a template parameter. Template template parameters are types
23266 for us (since TYPE_P holds true for them) so we handle
23267 them here. */
23268 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23269 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23270 return true;
23271 /* -- a qualified-id with a nested-name-specifier which contains a
23272 class-name that names a dependent type or whose unqualified-id
23273 names a dependent type. */
23274 if (TREE_CODE (type) == TYPENAME_TYPE)
23275 return true;
23276
23277 /* An alias template specialization can be dependent even if the
23278 resulting type is not. */
23279 if (dependent_alias_template_spec_p (type))
23280 return true;
23281
23282 /* -- a cv-qualified type where the cv-unqualified type is
23283 dependent.
23284 No code is necessary for this bullet; the code below handles
23285 cv-qualified types, and we don't want to strip aliases with
23286 TYPE_MAIN_VARIANT because of DR 1558. */
23287 /* -- a compound type constructed from any dependent type. */
23288 if (TYPE_PTRMEM_P (type))
23289 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23290 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23291 (type)));
23292 else if (TYPE_PTR_P (type)
23293 || TREE_CODE (type) == REFERENCE_TYPE)
23294 return dependent_type_p (TREE_TYPE (type));
23295 else if (TREE_CODE (type) == FUNCTION_TYPE
23296 || TREE_CODE (type) == METHOD_TYPE)
23297 {
23298 tree arg_type;
23299
23300 if (dependent_type_p (TREE_TYPE (type)))
23301 return true;
23302 for (arg_type = TYPE_ARG_TYPES (type);
23303 arg_type;
23304 arg_type = TREE_CHAIN (arg_type))
23305 if (dependent_type_p (TREE_VALUE (arg_type)))
23306 return true;
23307 return false;
23308 }
23309 /* -- an array type constructed from any dependent type or whose
23310 size is specified by a constant expression that is
23311 value-dependent.
23312
23313 We checked for type- and value-dependence of the bounds in
23314 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23315 if (TREE_CODE (type) == ARRAY_TYPE)
23316 {
23317 if (TYPE_DOMAIN (type)
23318 && dependent_type_p (TYPE_DOMAIN (type)))
23319 return true;
23320 return dependent_type_p (TREE_TYPE (type));
23321 }
23322
23323 /* -- a template-id in which either the template name is a template
23324 parameter ... */
23325 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23326 return true;
23327 /* ... or any of the template arguments is a dependent type or
23328 an expression that is type-dependent or value-dependent. */
23329 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23330 && (any_dependent_template_arguments_p
23331 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23332 return true;
23333
23334 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23335 dependent; if the argument of the `typeof' expression is not
23336 type-dependent, then it should already been have resolved. */
23337 if (TREE_CODE (type) == TYPEOF_TYPE
23338 || TREE_CODE (type) == DECLTYPE_TYPE
23339 || TREE_CODE (type) == UNDERLYING_TYPE)
23340 return true;
23341
23342 /* A template argument pack is dependent if any of its packed
23343 arguments are. */
23344 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23345 {
23346 tree args = ARGUMENT_PACK_ARGS (type);
23347 int i, len = TREE_VEC_LENGTH (args);
23348 for (i = 0; i < len; ++i)
23349 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23350 return true;
23351 }
23352
23353 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23354 be template parameters. */
23355 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23356 return true;
23357
23358 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23359 return true;
23360
23361 /* The standard does not specifically mention types that are local
23362 to template functions or local classes, but they should be
23363 considered dependent too. For example:
23364
23365 template <int I> void f() {
23366 enum E { a = I };
23367 S<sizeof (E)> s;
23368 }
23369
23370 The size of `E' cannot be known until the value of `I' has been
23371 determined. Therefore, `E' must be considered dependent. */
23372 scope = TYPE_CONTEXT (type);
23373 if (scope && TYPE_P (scope))
23374 return dependent_type_p (scope);
23375 /* Don't use type_dependent_expression_p here, as it can lead
23376 to infinite recursion trying to determine whether a lambda
23377 nested in a lambda is dependent (c++/47687). */
23378 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23379 && DECL_LANG_SPECIFIC (scope)
23380 && DECL_TEMPLATE_INFO (scope)
23381 && (any_dependent_template_arguments_p
23382 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23383 return true;
23384
23385 /* Other types are non-dependent. */
23386 return false;
23387 }
23388
23389 /* Returns TRUE if TYPE is dependent, in the sense of
23390 [temp.dep.type]. Note that a NULL type is considered dependent. */
23391
23392 bool
23393 dependent_type_p (tree type)
23394 {
23395 /* If there are no template parameters in scope, then there can't be
23396 any dependent types. */
23397 if (!processing_template_decl)
23398 {
23399 /* If we are not processing a template, then nobody should be
23400 providing us with a dependent type. */
23401 gcc_assert (type);
23402 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23403 return false;
23404 }
23405
23406 /* If the type is NULL, we have not computed a type for the entity
23407 in question; in that case, the type is dependent. */
23408 if (!type)
23409 return true;
23410
23411 /* Erroneous types can be considered non-dependent. */
23412 if (type == error_mark_node)
23413 return false;
23414
23415 /* If we have not already computed the appropriate value for TYPE,
23416 do so now. */
23417 if (!TYPE_DEPENDENT_P_VALID (type))
23418 {
23419 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23420 TYPE_DEPENDENT_P_VALID (type) = 1;
23421 }
23422
23423 return TYPE_DEPENDENT_P (type);
23424 }
23425
23426 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23427 lookup. In other words, a dependent type that is not the current
23428 instantiation. */
23429
23430 bool
23431 dependent_scope_p (tree scope)
23432 {
23433 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23434 && !currently_open_class (scope));
23435 }
23436
23437 /* T is a SCOPE_REF; return whether we need to consider it
23438 instantiation-dependent so that we can check access at instantiation
23439 time even though we know which member it resolves to. */
23440
23441 static bool
23442 instantiation_dependent_scope_ref_p (tree t)
23443 {
23444 if (DECL_P (TREE_OPERAND (t, 1))
23445 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23446 && accessible_in_template_p (TREE_OPERAND (t, 0),
23447 TREE_OPERAND (t, 1)))
23448 return false;
23449 else
23450 return true;
23451 }
23452
23453 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23454 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23455 expression. */
23456
23457 /* Note that this predicate is not appropriate for general expressions;
23458 only constant expressions (that satisfy potential_constant_expression)
23459 can be tested for value dependence. */
23460
23461 bool
23462 value_dependent_expression_p (tree expression)
23463 {
23464 if (!processing_template_decl || expression == NULL_TREE)
23465 return false;
23466
23467 /* A name declared with a dependent type. */
23468 if (DECL_P (expression) && type_dependent_expression_p (expression))
23469 return true;
23470
23471 switch (TREE_CODE (expression))
23472 {
23473 case BASELINK:
23474 /* A dependent member function of the current instantiation. */
23475 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23476
23477 case FUNCTION_DECL:
23478 /* A dependent member function of the current instantiation. */
23479 if (DECL_CLASS_SCOPE_P (expression)
23480 && dependent_type_p (DECL_CONTEXT (expression)))
23481 return true;
23482 break;
23483
23484 case IDENTIFIER_NODE:
23485 /* A name that has not been looked up -- must be dependent. */
23486 return true;
23487
23488 case TEMPLATE_PARM_INDEX:
23489 /* A non-type template parm. */
23490 return true;
23491
23492 case CONST_DECL:
23493 /* A non-type template parm. */
23494 if (DECL_TEMPLATE_PARM_P (expression))
23495 return true;
23496 return value_dependent_expression_p (DECL_INITIAL (expression));
23497
23498 case VAR_DECL:
23499 /* A constant with literal type and is initialized
23500 with an expression that is value-dependent.
23501
23502 Note that a non-dependent parenthesized initializer will have
23503 already been replaced with its constant value, so if we see
23504 a TREE_LIST it must be dependent. */
23505 if (DECL_INITIAL (expression)
23506 && decl_constant_var_p (expression)
23507 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23508 /* cp_finish_decl doesn't fold reference initializers. */
23509 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23510 || type_dependent_expression_p (DECL_INITIAL (expression))
23511 || value_dependent_expression_p (DECL_INITIAL (expression))))
23512 return true;
23513 return false;
23514
23515 case DYNAMIC_CAST_EXPR:
23516 case STATIC_CAST_EXPR:
23517 case CONST_CAST_EXPR:
23518 case REINTERPRET_CAST_EXPR:
23519 case CAST_EXPR:
23520 /* These expressions are value-dependent if the type to which
23521 the cast occurs is dependent or the expression being casted
23522 is value-dependent. */
23523 {
23524 tree type = TREE_TYPE (expression);
23525
23526 if (dependent_type_p (type))
23527 return true;
23528
23529 /* A functional cast has a list of operands. */
23530 expression = TREE_OPERAND (expression, 0);
23531 if (!expression)
23532 {
23533 /* If there are no operands, it must be an expression such
23534 as "int()". This should not happen for aggregate types
23535 because it would form non-constant expressions. */
23536 gcc_assert (cxx_dialect >= cxx11
23537 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23538
23539 return false;
23540 }
23541
23542 if (TREE_CODE (expression) == TREE_LIST)
23543 return any_value_dependent_elements_p (expression);
23544
23545 return value_dependent_expression_p (expression);
23546 }
23547
23548 case SIZEOF_EXPR:
23549 if (SIZEOF_EXPR_TYPE_P (expression))
23550 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23551 /* FALLTHRU */
23552 case ALIGNOF_EXPR:
23553 case TYPEID_EXPR:
23554 /* A `sizeof' expression is value-dependent if the operand is
23555 type-dependent or is a pack expansion. */
23556 expression = TREE_OPERAND (expression, 0);
23557 if (PACK_EXPANSION_P (expression))
23558 return true;
23559 else if (TYPE_P (expression))
23560 return dependent_type_p (expression);
23561 return instantiation_dependent_uneval_expression_p (expression);
23562
23563 case AT_ENCODE_EXPR:
23564 /* An 'encode' expression is value-dependent if the operand is
23565 type-dependent. */
23566 expression = TREE_OPERAND (expression, 0);
23567 return dependent_type_p (expression);
23568
23569 case NOEXCEPT_EXPR:
23570 expression = TREE_OPERAND (expression, 0);
23571 return instantiation_dependent_uneval_expression_p (expression);
23572
23573 case SCOPE_REF:
23574 /* All instantiation-dependent expressions should also be considered
23575 value-dependent. */
23576 return instantiation_dependent_scope_ref_p (expression);
23577
23578 case COMPONENT_REF:
23579 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23580 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23581
23582 case NONTYPE_ARGUMENT_PACK:
23583 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23584 is value-dependent. */
23585 {
23586 tree values = ARGUMENT_PACK_ARGS (expression);
23587 int i, len = TREE_VEC_LENGTH (values);
23588
23589 for (i = 0; i < len; ++i)
23590 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23591 return true;
23592
23593 return false;
23594 }
23595
23596 case TRAIT_EXPR:
23597 {
23598 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23599 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23600 || (type2 ? dependent_type_p (type2) : false));
23601 }
23602
23603 case MODOP_EXPR:
23604 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23605 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23606
23607 case ARRAY_REF:
23608 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23609 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23610
23611 case ADDR_EXPR:
23612 {
23613 tree op = TREE_OPERAND (expression, 0);
23614 return (value_dependent_expression_p (op)
23615 || has_value_dependent_address (op));
23616 }
23617
23618 case REQUIRES_EXPR:
23619 /* Treat all requires-expressions as value-dependent so
23620 we don't try to fold them. */
23621 return true;
23622
23623 case TYPE_REQ:
23624 return dependent_type_p (TREE_OPERAND (expression, 0));
23625
23626 case CALL_EXPR:
23627 {
23628 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23629 return true;
23630 tree fn = get_callee_fndecl (expression);
23631 int i, nargs;
23632 nargs = call_expr_nargs (expression);
23633 for (i = 0; i < nargs; ++i)
23634 {
23635 tree op = CALL_EXPR_ARG (expression, i);
23636 /* In a call to a constexpr member function, look through the
23637 implicit ADDR_EXPR on the object argument so that it doesn't
23638 cause the call to be considered value-dependent. We also
23639 look through it in potential_constant_expression. */
23640 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23641 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23642 && TREE_CODE (op) == ADDR_EXPR)
23643 op = TREE_OPERAND (op, 0);
23644 if (value_dependent_expression_p (op))
23645 return true;
23646 }
23647 return false;
23648 }
23649
23650 case TEMPLATE_ID_EXPR:
23651 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23652 type-dependent. */
23653 return type_dependent_expression_p (expression)
23654 || variable_concept_p (TREE_OPERAND (expression, 0));
23655
23656 case CONSTRUCTOR:
23657 {
23658 unsigned ix;
23659 tree val;
23660 if (dependent_type_p (TREE_TYPE (expression)))
23661 return true;
23662 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23663 if (value_dependent_expression_p (val))
23664 return true;
23665 return false;
23666 }
23667
23668 case STMT_EXPR:
23669 /* Treat a GNU statement expression as dependent to avoid crashing
23670 under instantiate_non_dependent_expr; it can't be constant. */
23671 return true;
23672
23673 default:
23674 /* A constant expression is value-dependent if any subexpression is
23675 value-dependent. */
23676 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23677 {
23678 case tcc_reference:
23679 case tcc_unary:
23680 case tcc_comparison:
23681 case tcc_binary:
23682 case tcc_expression:
23683 case tcc_vl_exp:
23684 {
23685 int i, len = cp_tree_operand_length (expression);
23686
23687 for (i = 0; i < len; i++)
23688 {
23689 tree t = TREE_OPERAND (expression, i);
23690
23691 /* In some cases, some of the operands may be missing.l
23692 (For example, in the case of PREDECREMENT_EXPR, the
23693 amount to increment by may be missing.) That doesn't
23694 make the expression dependent. */
23695 if (t && value_dependent_expression_p (t))
23696 return true;
23697 }
23698 }
23699 break;
23700 default:
23701 break;
23702 }
23703 break;
23704 }
23705
23706 /* The expression is not value-dependent. */
23707 return false;
23708 }
23709
23710 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23711 [temp.dep.expr]. Note that an expression with no type is
23712 considered dependent. Other parts of the compiler arrange for an
23713 expression with type-dependent subexpressions to have no type, so
23714 this function doesn't have to be fully recursive. */
23715
23716 bool
23717 type_dependent_expression_p (tree expression)
23718 {
23719 if (!processing_template_decl)
23720 return false;
23721
23722 if (expression == NULL_TREE || expression == error_mark_node)
23723 return false;
23724
23725 /* An unresolved name is always dependent. */
23726 if (identifier_p (expression)
23727 || TREE_CODE (expression) == USING_DECL
23728 || TREE_CODE (expression) == WILDCARD_DECL)
23729 return true;
23730
23731 /* A fold expression is type-dependent. */
23732 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23733 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23734 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23735 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23736 return true;
23737
23738 /* Some expression forms are never type-dependent. */
23739 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23740 || TREE_CODE (expression) == SIZEOF_EXPR
23741 || TREE_CODE (expression) == ALIGNOF_EXPR
23742 || TREE_CODE (expression) == AT_ENCODE_EXPR
23743 || TREE_CODE (expression) == NOEXCEPT_EXPR
23744 || TREE_CODE (expression) == TRAIT_EXPR
23745 || TREE_CODE (expression) == TYPEID_EXPR
23746 || TREE_CODE (expression) == DELETE_EXPR
23747 || TREE_CODE (expression) == VEC_DELETE_EXPR
23748 || TREE_CODE (expression) == THROW_EXPR
23749 || TREE_CODE (expression) == REQUIRES_EXPR)
23750 return false;
23751
23752 /* The types of these expressions depends only on the type to which
23753 the cast occurs. */
23754 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23755 || TREE_CODE (expression) == STATIC_CAST_EXPR
23756 || TREE_CODE (expression) == CONST_CAST_EXPR
23757 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23758 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23759 || TREE_CODE (expression) == CAST_EXPR)
23760 return dependent_type_p (TREE_TYPE (expression));
23761
23762 /* The types of these expressions depends only on the type created
23763 by the expression. */
23764 if (TREE_CODE (expression) == NEW_EXPR
23765 || TREE_CODE (expression) == VEC_NEW_EXPR)
23766 {
23767 /* For NEW_EXPR tree nodes created inside a template, either
23768 the object type itself or a TREE_LIST may appear as the
23769 operand 1. */
23770 tree type = TREE_OPERAND (expression, 1);
23771 if (TREE_CODE (type) == TREE_LIST)
23772 /* This is an array type. We need to check array dimensions
23773 as well. */
23774 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23775 || value_dependent_expression_p
23776 (TREE_OPERAND (TREE_VALUE (type), 1));
23777 else
23778 return dependent_type_p (type);
23779 }
23780
23781 if (TREE_CODE (expression) == SCOPE_REF)
23782 {
23783 tree scope = TREE_OPERAND (expression, 0);
23784 tree name = TREE_OPERAND (expression, 1);
23785
23786 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23787 contains an identifier associated by name lookup with one or more
23788 declarations declared with a dependent type, or...a
23789 nested-name-specifier or qualified-id that names a member of an
23790 unknown specialization. */
23791 return (type_dependent_expression_p (name)
23792 || dependent_scope_p (scope));
23793 }
23794
23795 if (TREE_CODE (expression) == TEMPLATE_DECL
23796 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23797 return uses_outer_template_parms (expression);
23798
23799 if (TREE_CODE (expression) == STMT_EXPR)
23800 expression = stmt_expr_value_expr (expression);
23801
23802 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23803 {
23804 tree elt;
23805 unsigned i;
23806
23807 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23808 {
23809 if (type_dependent_expression_p (elt))
23810 return true;
23811 }
23812 return false;
23813 }
23814
23815 /* A static data member of the current instantiation with incomplete
23816 array type is type-dependent, as the definition and specializations
23817 can have different bounds. */
23818 if (VAR_P (expression)
23819 && DECL_CLASS_SCOPE_P (expression)
23820 && dependent_type_p (DECL_CONTEXT (expression))
23821 && VAR_HAD_UNKNOWN_BOUND (expression))
23822 return true;
23823
23824 /* An array of unknown bound depending on a variadic parameter, eg:
23825
23826 template<typename... Args>
23827 void foo (Args... args)
23828 {
23829 int arr[] = { args... };
23830 }
23831
23832 template<int... vals>
23833 void bar ()
23834 {
23835 int arr[] = { vals... };
23836 }
23837
23838 If the array has no length and has an initializer, it must be that
23839 we couldn't determine its length in cp_complete_array_type because
23840 it is dependent. */
23841 if (VAR_P (expression)
23842 && TREE_TYPE (expression) != NULL_TREE
23843 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23844 && !TYPE_DOMAIN (TREE_TYPE (expression))
23845 && DECL_INITIAL (expression))
23846 return true;
23847
23848 /* A function or variable template-id is type-dependent if it has any
23849 dependent template arguments. Note that we only consider the innermost
23850 template arguments here, since those are the ones that come from the
23851 template-id; the template arguments for the enclosing class do not make it
23852 type-dependent, they only make a member function value-dependent. */
23853 if (VAR_OR_FUNCTION_DECL_P (expression)
23854 && DECL_LANG_SPECIFIC (expression)
23855 && DECL_TEMPLATE_INFO (expression)
23856 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23857 && (any_dependent_template_arguments_p
23858 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23859 return true;
23860
23861 /* Always dependent, on the number of arguments if nothing else. */
23862 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23863 return true;
23864
23865 if (TREE_TYPE (expression) == unknown_type_node)
23866 {
23867 if (TREE_CODE (expression) == ADDR_EXPR)
23868 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23869 if (TREE_CODE (expression) == COMPONENT_REF
23870 || TREE_CODE (expression) == OFFSET_REF)
23871 {
23872 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23873 return true;
23874 expression = TREE_OPERAND (expression, 1);
23875 if (identifier_p (expression))
23876 return false;
23877 }
23878 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23879 if (TREE_CODE (expression) == SCOPE_REF)
23880 return false;
23881
23882 if (BASELINK_P (expression))
23883 {
23884 if (BASELINK_OPTYPE (expression)
23885 && dependent_type_p (BASELINK_OPTYPE (expression)))
23886 return true;
23887 expression = BASELINK_FUNCTIONS (expression);
23888 }
23889
23890 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23891 {
23892 if (any_dependent_template_arguments_p
23893 (TREE_OPERAND (expression, 1)))
23894 return true;
23895 expression = TREE_OPERAND (expression, 0);
23896 if (identifier_p (expression))
23897 return true;
23898 }
23899
23900 gcc_assert (TREE_CODE (expression) == OVERLOAD
23901 || TREE_CODE (expression) == FUNCTION_DECL);
23902
23903 while (expression)
23904 {
23905 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23906 return true;
23907 expression = OVL_NEXT (expression);
23908 }
23909 return false;
23910 }
23911
23912 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23913
23914 /* Dependent type attributes might not have made it from the decl to
23915 the type yet. */
23916 if (DECL_P (expression)
23917 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23918 return true;
23919
23920 return (dependent_type_p (TREE_TYPE (expression)));
23921 }
23922
23923 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23924 type-dependent if the expression refers to a member of the current
23925 instantiation and the type of the referenced member is dependent, or the
23926 class member access expression refers to a member of an unknown
23927 specialization.
23928
23929 This function returns true if the OBJECT in such a class member access
23930 expression is of an unknown specialization. */
23931
23932 bool
23933 type_dependent_object_expression_p (tree object)
23934 {
23935 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
23936 dependent. */
23937 if (TREE_CODE (object) == IDENTIFIER_NODE)
23938 return true;
23939 tree scope = TREE_TYPE (object);
23940 return (!scope || dependent_scope_p (scope));
23941 }
23942
23943 /* walk_tree callback function for instantiation_dependent_expression_p,
23944 below. Returns non-zero if a dependent subexpression is found. */
23945
23946 static tree
23947 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23948 void * /*data*/)
23949 {
23950 if (TYPE_P (*tp))
23951 {
23952 /* We don't have to worry about decltype currently because decltype
23953 of an instantiation-dependent expr is a dependent type. This
23954 might change depending on the resolution of DR 1172. */
23955 *walk_subtrees = false;
23956 return NULL_TREE;
23957 }
23958 enum tree_code code = TREE_CODE (*tp);
23959 switch (code)
23960 {
23961 /* Don't treat an argument list as dependent just because it has no
23962 TREE_TYPE. */
23963 case TREE_LIST:
23964 case TREE_VEC:
23965 return NULL_TREE;
23966
23967 case TEMPLATE_PARM_INDEX:
23968 return *tp;
23969
23970 /* Handle expressions with type operands. */
23971 case SIZEOF_EXPR:
23972 case ALIGNOF_EXPR:
23973 case TYPEID_EXPR:
23974 case AT_ENCODE_EXPR:
23975 {
23976 tree op = TREE_OPERAND (*tp, 0);
23977 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
23978 op = TREE_TYPE (op);
23979 if (TYPE_P (op))
23980 {
23981 if (dependent_type_p (op))
23982 return *tp;
23983 else
23984 {
23985 *walk_subtrees = false;
23986 return NULL_TREE;
23987 }
23988 }
23989 break;
23990 }
23991
23992 case COMPONENT_REF:
23993 if (identifier_p (TREE_OPERAND (*tp, 1)))
23994 /* In a template, finish_class_member_access_expr creates a
23995 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
23996 type-dependent, so that we can check access control at
23997 instantiation time (PR 42277). See also Core issue 1273. */
23998 return *tp;
23999 break;
24000
24001 case SCOPE_REF:
24002 if (instantiation_dependent_scope_ref_p (*tp))
24003 return *tp;
24004 else
24005 break;
24006
24007 /* Treat statement-expressions as dependent. */
24008 case BIND_EXPR:
24009 return *tp;
24010
24011 /* Treat requires-expressions as dependent. */
24012 case REQUIRES_EXPR:
24013 return *tp;
24014
24015 case CALL_EXPR:
24016 /* Treat calls to function concepts as dependent. */
24017 if (function_concept_check_p (*tp))
24018 return *tp;
24019 break;
24020
24021 case TEMPLATE_ID_EXPR:
24022 /* And variable concepts. */
24023 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24024 return *tp;
24025 break;
24026
24027 default:
24028 break;
24029 }
24030
24031 if (type_dependent_expression_p (*tp))
24032 return *tp;
24033 else
24034 return NULL_TREE;
24035 }
24036
24037 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24038 sense defined by the ABI:
24039
24040 "An expression is instantiation-dependent if it is type-dependent
24041 or value-dependent, or it has a subexpression that is type-dependent
24042 or value-dependent."
24043
24044 Except don't actually check value-dependence for unevaluated expressions,
24045 because in sizeof(i) we don't care about the value of i. Checking
24046 type-dependence will in turn check value-dependence of array bounds/template
24047 arguments as needed. */
24048
24049 bool
24050 instantiation_dependent_uneval_expression_p (tree expression)
24051 {
24052 tree result;
24053
24054 if (!processing_template_decl)
24055 return false;
24056
24057 if (expression == error_mark_node)
24058 return false;
24059
24060 result = cp_walk_tree_without_duplicates (&expression,
24061 instantiation_dependent_r, NULL);
24062 return result != NULL_TREE;
24063 }
24064
24065 /* As above, but also check value-dependence of the expression as a whole. */
24066
24067 bool
24068 instantiation_dependent_expression_p (tree expression)
24069 {
24070 return (instantiation_dependent_uneval_expression_p (expression)
24071 || value_dependent_expression_p (expression));
24072 }
24073
24074 /* Like type_dependent_expression_p, but it also works while not processing
24075 a template definition, i.e. during substitution or mangling. */
24076
24077 bool
24078 type_dependent_expression_p_push (tree expr)
24079 {
24080 bool b;
24081 ++processing_template_decl;
24082 b = type_dependent_expression_p (expr);
24083 --processing_template_decl;
24084 return b;
24085 }
24086
24087 /* Returns TRUE if ARGS contains a type-dependent expression. */
24088
24089 bool
24090 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24091 {
24092 unsigned int i;
24093 tree arg;
24094
24095 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24096 {
24097 if (type_dependent_expression_p (arg))
24098 return true;
24099 }
24100 return false;
24101 }
24102
24103 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24104 expressions) contains any type-dependent expressions. */
24105
24106 bool
24107 any_type_dependent_elements_p (const_tree list)
24108 {
24109 for (; list; list = TREE_CHAIN (list))
24110 if (type_dependent_expression_p (TREE_VALUE (list)))
24111 return true;
24112
24113 return false;
24114 }
24115
24116 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24117 expressions) contains any value-dependent expressions. */
24118
24119 bool
24120 any_value_dependent_elements_p (const_tree list)
24121 {
24122 for (; list; list = TREE_CHAIN (list))
24123 if (value_dependent_expression_p (TREE_VALUE (list)))
24124 return true;
24125
24126 return false;
24127 }
24128
24129 /* Returns TRUE if the ARG (a template argument) is dependent. */
24130
24131 bool
24132 dependent_template_arg_p (tree arg)
24133 {
24134 if (!processing_template_decl)
24135 return false;
24136
24137 /* Assume a template argument that was wrongly written by the user
24138 is dependent. This is consistent with what
24139 any_dependent_template_arguments_p [that calls this function]
24140 does. */
24141 if (!arg || arg == error_mark_node)
24142 return true;
24143
24144 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24145 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24146
24147 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24148 return true;
24149 if (TREE_CODE (arg) == TEMPLATE_DECL)
24150 {
24151 if (DECL_TEMPLATE_PARM_P (arg))
24152 return true;
24153 /* A member template of a dependent class is not necessarily
24154 type-dependent, but it is a dependent template argument because it
24155 will be a member of an unknown specialization to that template. */
24156 tree scope = CP_DECL_CONTEXT (arg);
24157 return TYPE_P (scope) && dependent_type_p (scope);
24158 }
24159 else if (ARGUMENT_PACK_P (arg))
24160 {
24161 tree args = ARGUMENT_PACK_ARGS (arg);
24162 int i, len = TREE_VEC_LENGTH (args);
24163 for (i = 0; i < len; ++i)
24164 {
24165 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24166 return true;
24167 }
24168
24169 return false;
24170 }
24171 else if (TYPE_P (arg))
24172 return dependent_type_p (arg);
24173 else
24174 return (type_dependent_expression_p (arg)
24175 || value_dependent_expression_p (arg));
24176 }
24177
24178 /* Returns true if ARGS (a collection of template arguments) contains
24179 any types that require structural equality testing. */
24180
24181 bool
24182 any_template_arguments_need_structural_equality_p (tree args)
24183 {
24184 int i;
24185 int j;
24186
24187 if (!args)
24188 return false;
24189 if (args == error_mark_node)
24190 return true;
24191
24192 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24193 {
24194 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24195 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24196 {
24197 tree arg = TREE_VEC_ELT (level, j);
24198 tree packed_args = NULL_TREE;
24199 int k, len = 1;
24200
24201 if (ARGUMENT_PACK_P (arg))
24202 {
24203 /* Look inside the argument pack. */
24204 packed_args = ARGUMENT_PACK_ARGS (arg);
24205 len = TREE_VEC_LENGTH (packed_args);
24206 }
24207
24208 for (k = 0; k < len; ++k)
24209 {
24210 if (packed_args)
24211 arg = TREE_VEC_ELT (packed_args, k);
24212
24213 if (error_operand_p (arg))
24214 return true;
24215 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24216 continue;
24217 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24218 return true;
24219 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24220 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24221 return true;
24222 }
24223 }
24224 }
24225
24226 return false;
24227 }
24228
24229 /* Returns true if ARGS (a collection of template arguments) contains
24230 any dependent arguments. */
24231
24232 bool
24233 any_dependent_template_arguments_p (const_tree args)
24234 {
24235 int i;
24236 int j;
24237
24238 if (!args)
24239 return false;
24240 if (args == error_mark_node)
24241 return true;
24242
24243 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24244 {
24245 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24246 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24247 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24248 return true;
24249 }
24250
24251 return false;
24252 }
24253
24254 /* Returns TRUE if the template TMPL is type-dependent. */
24255
24256 bool
24257 dependent_template_p (tree tmpl)
24258 {
24259 if (TREE_CODE (tmpl) == OVERLOAD)
24260 {
24261 while (tmpl)
24262 {
24263 if (dependent_template_p (OVL_CURRENT (tmpl)))
24264 return true;
24265 tmpl = OVL_NEXT (tmpl);
24266 }
24267 return false;
24268 }
24269
24270 /* Template template parameters are dependent. */
24271 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24272 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24273 return true;
24274 /* So are names that have not been looked up. */
24275 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24276 return true;
24277 return false;
24278 }
24279
24280 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24281
24282 bool
24283 dependent_template_id_p (tree tmpl, tree args)
24284 {
24285 return (dependent_template_p (tmpl)
24286 || any_dependent_template_arguments_p (args));
24287 }
24288
24289 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24290 are dependent. */
24291
24292 bool
24293 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24294 {
24295 int i;
24296
24297 if (!processing_template_decl)
24298 return false;
24299
24300 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24301 {
24302 tree decl = TREE_VEC_ELT (declv, i);
24303 tree init = TREE_VEC_ELT (initv, i);
24304 tree cond = TREE_VEC_ELT (condv, i);
24305 tree incr = TREE_VEC_ELT (incrv, i);
24306
24307 if (type_dependent_expression_p (decl)
24308 || TREE_CODE (decl) == SCOPE_REF)
24309 return true;
24310
24311 if (init && type_dependent_expression_p (init))
24312 return true;
24313
24314 if (type_dependent_expression_p (cond))
24315 return true;
24316
24317 if (COMPARISON_CLASS_P (cond)
24318 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24319 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24320 return true;
24321
24322 if (TREE_CODE (incr) == MODOP_EXPR)
24323 {
24324 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24325 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24326 return true;
24327 }
24328 else if (type_dependent_expression_p (incr))
24329 return true;
24330 else if (TREE_CODE (incr) == MODIFY_EXPR)
24331 {
24332 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24333 return true;
24334 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24335 {
24336 tree t = TREE_OPERAND (incr, 1);
24337 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24338 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24339 return true;
24340 }
24341 }
24342 }
24343
24344 return false;
24345 }
24346
24347 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24348 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24349 no such TYPE can be found. Note that this function peers inside
24350 uninstantiated templates and therefore should be used only in
24351 extremely limited situations. ONLY_CURRENT_P restricts this
24352 peering to the currently open classes hierarchy (which is required
24353 when comparing types). */
24354
24355 tree
24356 resolve_typename_type (tree type, bool only_current_p)
24357 {
24358 tree scope;
24359 tree name;
24360 tree decl;
24361 int quals;
24362 tree pushed_scope;
24363 tree result;
24364
24365 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24366
24367 scope = TYPE_CONTEXT (type);
24368 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24369 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24370 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24371 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24372 identifier of the TYPENAME_TYPE anymore.
24373 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24374 TYPENAME_TYPE instead, we avoid messing up with a possible
24375 typedef variant case. */
24376 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24377
24378 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24379 it first before we can figure out what NAME refers to. */
24380 if (TREE_CODE (scope) == TYPENAME_TYPE)
24381 {
24382 if (TYPENAME_IS_RESOLVING_P (scope))
24383 /* Given a class template A with a dependent base with nested type C,
24384 typedef typename A::C::C C will land us here, as trying to resolve
24385 the initial A::C leads to the local C typedef, which leads back to
24386 A::C::C. So we break the recursion now. */
24387 return type;
24388 else
24389 scope = resolve_typename_type (scope, only_current_p);
24390 }
24391 /* If we don't know what SCOPE refers to, then we cannot resolve the
24392 TYPENAME_TYPE. */
24393 if (!CLASS_TYPE_P (scope))
24394 return type;
24395 /* If this is a typedef, we don't want to look inside (c++/11987). */
24396 if (typedef_variant_p (type))
24397 return type;
24398 /* If SCOPE isn't the template itself, it will not have a valid
24399 TYPE_FIELDS list. */
24400 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24401 /* scope is either the template itself or a compatible instantiation
24402 like X<T>, so look up the name in the original template. */
24403 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24404 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24405 gcc_checking_assert (uses_template_parms (scope));
24406 /* If scope has no fields, it can't be a current instantiation. Check this
24407 before currently_open_class to avoid infinite recursion (71515). */
24408 if (!TYPE_FIELDS (scope))
24409 return type;
24410 /* If the SCOPE is not the current instantiation, there's no reason
24411 to look inside it. */
24412 if (only_current_p && !currently_open_class (scope))
24413 return type;
24414 /* Enter the SCOPE so that name lookup will be resolved as if we
24415 were in the class definition. In particular, SCOPE will no
24416 longer be considered a dependent type. */
24417 pushed_scope = push_scope (scope);
24418 /* Look up the declaration. */
24419 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24420 tf_warning_or_error);
24421
24422 result = NULL_TREE;
24423
24424 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24425 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24426 if (!decl)
24427 /*nop*/;
24428 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24429 && TREE_CODE (decl) == TYPE_DECL)
24430 {
24431 result = TREE_TYPE (decl);
24432 if (result == error_mark_node)
24433 result = NULL_TREE;
24434 }
24435 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24436 && DECL_CLASS_TEMPLATE_P (decl))
24437 {
24438 tree tmpl;
24439 tree args;
24440 /* Obtain the template and the arguments. */
24441 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24442 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24443 /* Instantiate the template. */
24444 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24445 /*entering_scope=*/0,
24446 tf_error | tf_user);
24447 if (result == error_mark_node)
24448 result = NULL_TREE;
24449 }
24450
24451 /* Leave the SCOPE. */
24452 if (pushed_scope)
24453 pop_scope (pushed_scope);
24454
24455 /* If we failed to resolve it, return the original typename. */
24456 if (!result)
24457 return type;
24458
24459 /* If lookup found a typename type, resolve that too. */
24460 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24461 {
24462 /* Ill-formed programs can cause infinite recursion here, so we
24463 must catch that. */
24464 TYPENAME_IS_RESOLVING_P (result) = 1;
24465 result = resolve_typename_type (result, only_current_p);
24466 TYPENAME_IS_RESOLVING_P (result) = 0;
24467 }
24468
24469 /* Qualify the resulting type. */
24470 quals = cp_type_quals (type);
24471 if (quals)
24472 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24473
24474 return result;
24475 }
24476
24477 /* EXPR is an expression which is not type-dependent. Return a proxy
24478 for EXPR that can be used to compute the types of larger
24479 expressions containing EXPR. */
24480
24481 tree
24482 build_non_dependent_expr (tree expr)
24483 {
24484 tree inner_expr;
24485
24486 /* When checking, try to get a constant value for all non-dependent
24487 expressions in order to expose bugs in *_dependent_expression_p
24488 and constexpr. This can affect code generation, see PR70704, so
24489 only do this for -fchecking=2. */
24490 if (flag_checking > 1
24491 && cxx_dialect >= cxx11
24492 /* Don't do this during nsdmi parsing as it can lead to
24493 unexpected recursive instantiations. */
24494 && !parsing_nsdmi ()
24495 /* Don't do this during concept expansion either and for
24496 the same reason. */
24497 && !expanding_concept ())
24498 fold_non_dependent_expr (expr);
24499
24500 /* Preserve OVERLOADs; the functions must be available to resolve
24501 types. */
24502 inner_expr = expr;
24503 if (TREE_CODE (inner_expr) == STMT_EXPR)
24504 inner_expr = stmt_expr_value_expr (inner_expr);
24505 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24506 inner_expr = TREE_OPERAND (inner_expr, 0);
24507 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24508 inner_expr = TREE_OPERAND (inner_expr, 1);
24509 if (is_overloaded_fn (inner_expr)
24510 || TREE_CODE (inner_expr) == OFFSET_REF)
24511 return expr;
24512 /* There is no need to return a proxy for a variable. */
24513 if (VAR_P (expr))
24514 return expr;
24515 /* Preserve string constants; conversions from string constants to
24516 "char *" are allowed, even though normally a "const char *"
24517 cannot be used to initialize a "char *". */
24518 if (TREE_CODE (expr) == STRING_CST)
24519 return expr;
24520 /* Preserve void and arithmetic constants, as an optimization -- there is no
24521 reason to create a new node. */
24522 if (TREE_CODE (expr) == VOID_CST
24523 || TREE_CODE (expr) == INTEGER_CST
24524 || TREE_CODE (expr) == REAL_CST)
24525 return expr;
24526 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24527 There is at least one place where we want to know that a
24528 particular expression is a throw-expression: when checking a ?:
24529 expression, there are special rules if the second or third
24530 argument is a throw-expression. */
24531 if (TREE_CODE (expr) == THROW_EXPR)
24532 return expr;
24533
24534 /* Don't wrap an initializer list, we need to be able to look inside. */
24535 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24536 return expr;
24537
24538 /* Don't wrap a dummy object, we need to be able to test for it. */
24539 if (is_dummy_object (expr))
24540 return expr;
24541
24542 if (TREE_CODE (expr) == COND_EXPR)
24543 return build3 (COND_EXPR,
24544 TREE_TYPE (expr),
24545 TREE_OPERAND (expr, 0),
24546 (TREE_OPERAND (expr, 1)
24547 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24548 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24549 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24550 if (TREE_CODE (expr) == COMPOUND_EXPR
24551 && !COMPOUND_EXPR_OVERLOADED (expr))
24552 return build2 (COMPOUND_EXPR,
24553 TREE_TYPE (expr),
24554 TREE_OPERAND (expr, 0),
24555 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24556
24557 /* If the type is unknown, it can't really be non-dependent */
24558 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24559
24560 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24561 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24562 }
24563
24564 /* ARGS is a vector of expressions as arguments to a function call.
24565 Replace the arguments with equivalent non-dependent expressions.
24566 This modifies ARGS in place. */
24567
24568 void
24569 make_args_non_dependent (vec<tree, va_gc> *args)
24570 {
24571 unsigned int ix;
24572 tree arg;
24573
24574 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24575 {
24576 tree newarg = build_non_dependent_expr (arg);
24577 if (newarg != arg)
24578 (*args)[ix] = newarg;
24579 }
24580 }
24581
24582 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24583 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24584 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24585
24586 static tree
24587 make_auto_1 (tree name, bool set_canonical)
24588 {
24589 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24590 TYPE_NAME (au) = build_decl (input_location,
24591 TYPE_DECL, name, au);
24592 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24593 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24594 (0, processing_template_decl + 1, processing_template_decl + 1,
24595 TYPE_NAME (au), NULL_TREE);
24596 if (set_canonical)
24597 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24598 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24599 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24600
24601 return au;
24602 }
24603
24604 tree
24605 make_decltype_auto (void)
24606 {
24607 return make_auto_1 (decltype_auto_identifier, true);
24608 }
24609
24610 tree
24611 make_auto (void)
24612 {
24613 return make_auto_1 (auto_identifier, true);
24614 }
24615
24616 /* Return a C++17 deduction placeholder for class template TMPL. */
24617
24618 tree
24619 make_template_placeholder (tree tmpl)
24620 {
24621 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24622 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24623 return t;
24624 }
24625
24626 /* Make a "constrained auto" type-specifier. This is an
24627 auto type with constraints that must be associated after
24628 deduction. The constraint is formed from the given
24629 CONC and its optional sequence of arguments, which are
24630 non-null if written as partial-concept-id. */
24631
24632 tree
24633 make_constrained_auto (tree con, tree args)
24634 {
24635 tree type = make_auto_1 (auto_identifier, false);
24636
24637 /* Build the constraint. */
24638 tree tmpl = DECL_TI_TEMPLATE (con);
24639 tree expr;
24640 if (VAR_P (con))
24641 expr = build_concept_check (tmpl, type, args);
24642 else
24643 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24644
24645 tree constr = normalize_expression (expr);
24646 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24647
24648 /* Our canonical type depends on the constraint. */
24649 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24650
24651 /* Attach the constraint to the type declaration. */
24652 tree decl = TYPE_NAME (type);
24653 return decl;
24654 }
24655
24656 /* Given type ARG, return std::initializer_list<ARG>. */
24657
24658 static tree
24659 listify (tree arg)
24660 {
24661 tree std_init_list = namespace_binding
24662 (get_identifier ("initializer_list"), std_node);
24663 tree argvec;
24664 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24665 {
24666 error ("deducing from brace-enclosed initializer list requires "
24667 "#include <initializer_list>");
24668 return error_mark_node;
24669 }
24670 argvec = make_tree_vec (1);
24671 TREE_VEC_ELT (argvec, 0) = arg;
24672 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24673 NULL_TREE, 0, tf_warning_or_error);
24674 }
24675
24676 /* Replace auto in TYPE with std::initializer_list<auto>. */
24677
24678 static tree
24679 listify_autos (tree type, tree auto_node)
24680 {
24681 tree init_auto = listify (auto_node);
24682 tree argvec = make_tree_vec (1);
24683 TREE_VEC_ELT (argvec, 0) = init_auto;
24684 if (processing_template_decl)
24685 argvec = add_to_template_args (current_template_args (), argvec);
24686 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24687 }
24688
24689 /* Hash traits for hashing possibly constrained 'auto'
24690 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24691
24692 struct auto_hash : default_hash_traits<tree>
24693 {
24694 static inline hashval_t hash (tree);
24695 static inline bool equal (tree, tree);
24696 };
24697
24698 /* Hash the 'auto' T. */
24699
24700 inline hashval_t
24701 auto_hash::hash (tree t)
24702 {
24703 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24704 /* Matching constrained-type-specifiers denote the same template
24705 parameter, so hash the constraint. */
24706 return hash_placeholder_constraint (c);
24707 else
24708 /* But unconstrained autos are all separate, so just hash the pointer. */
24709 return iterative_hash_object (t, 0);
24710 }
24711
24712 /* Compare two 'auto's. */
24713
24714 inline bool
24715 auto_hash::equal (tree t1, tree t2)
24716 {
24717 if (t1 == t2)
24718 return true;
24719
24720 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24721 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24722
24723 /* Two unconstrained autos are distinct. */
24724 if (!c1 || !c2)
24725 return false;
24726
24727 return equivalent_placeholder_constraints (c1, c2);
24728 }
24729
24730 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24731 constrained) auto, add it to the vector. */
24732
24733 static int
24734 extract_autos_r (tree t, void *data)
24735 {
24736 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24737 if (is_auto_or_concept (t))
24738 {
24739 /* All the autos were built with index 0; fix that up now. */
24740 tree *p = hash.find_slot (t, INSERT);
24741 unsigned idx;
24742 if (*p)
24743 /* If this is a repeated constrained-type-specifier, use the index we
24744 chose before. */
24745 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24746 else
24747 {
24748 /* Otherwise this is new, so use the current count. */
24749 *p = t;
24750 idx = hash.elements () - 1;
24751 }
24752 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24753 }
24754
24755 /* Always keep walking. */
24756 return 0;
24757 }
24758
24759 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24760 says they can appear anywhere in the type. */
24761
24762 static tree
24763 extract_autos (tree type)
24764 {
24765 hash_set<tree> visited;
24766 hash_table<auto_hash> hash (2);
24767
24768 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24769
24770 tree tree_vec = make_tree_vec (hash.elements());
24771 for (hash_table<auto_hash>::iterator iter = hash.begin();
24772 iter != hash.end(); ++iter)
24773 {
24774 tree elt = *iter;
24775 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24776 TREE_VEC_ELT (tree_vec, i)
24777 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24778 }
24779
24780 return tree_vec;
24781 }
24782
24783 /* The stem for deduction guide names. */
24784 const char *const dguide_base = "__dguide_";
24785
24786 /* Return the name for a deduction guide for class template TMPL. */
24787
24788 tree
24789 dguide_name (tree tmpl)
24790 {
24791 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24792 tree tname = TYPE_IDENTIFIER (type);
24793 char *buf = (char *) alloca (1 + strlen (dguide_base)
24794 + IDENTIFIER_LENGTH (tname));
24795 memcpy (buf, dguide_base, strlen (dguide_base));
24796 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24797 IDENTIFIER_LENGTH (tname) + 1);
24798 tree dname = get_identifier (buf);
24799 TREE_TYPE (dname) = type;
24800 return dname;
24801 }
24802
24803 /* True if NAME is the name of a deduction guide. */
24804
24805 bool
24806 dguide_name_p (tree name)
24807 {
24808 return (TREE_TYPE (name)
24809 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24810 strlen (dguide_base)));
24811 }
24812
24813 /* True if FN is a deduction guide. */
24814
24815 bool
24816 deduction_guide_p (const_tree fn)
24817 {
24818 if (DECL_P (fn))
24819 if (tree name = DECL_NAME (fn))
24820 return dguide_name_p (name);
24821 return false;
24822 }
24823
24824 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24825 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24826 template parameter types. Note that the handling of template template
24827 parameters relies on current_template_parms being set appropriately for the
24828 new template. */
24829
24830 static tree
24831 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24832 tree tsubst_args, tsubst_flags_t complain)
24833 {
24834 tree oldidx = get_template_parm_index (olddecl);
24835
24836 tree newtype;
24837 if (TREE_CODE (olddecl) == TYPE_DECL
24838 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24839 {
24840 tree oldtype = TREE_TYPE (olddecl);
24841 newtype = cxx_make_type (TREE_CODE (oldtype));
24842 TYPE_MAIN_VARIANT (newtype) = newtype;
24843 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
24844 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
24845 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
24846 }
24847 else
24848 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24849 complain, NULL_TREE);
24850
24851 tree newdecl
24852 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24853 DECL_NAME (olddecl), newtype);
24854 SET_DECL_TEMPLATE_PARM_P (newdecl);
24855
24856 tree newidx;
24857 if (TREE_CODE (olddecl) == TYPE_DECL
24858 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24859 {
24860 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24861 = build_template_parm_index (index, level, level,
24862 newdecl, newtype);
24863 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24864 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24865
24866 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24867 {
24868 DECL_TEMPLATE_RESULT (newdecl)
24869 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24870 DECL_NAME (olddecl), newtype);
24871 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24872 // First create a copy (ttargs) of tsubst_args with an
24873 // additional level for the template template parameter's own
24874 // template parameters (ttparms).
24875 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24876 (DECL_TEMPLATE_PARMS (olddecl)));
24877 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24878 tree ttargs = make_tree_vec (depth + 1);
24879 for (int i = 0; i < depth; ++i)
24880 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24881 TREE_VEC_ELT (ttargs, depth)
24882 = template_parms_level_to_args (ttparms);
24883 // Substitute ttargs into ttparms to fix references to
24884 // other template parameters.
24885 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24886 complain);
24887 // Now substitute again with args based on tparms, to reduce
24888 // the level of the ttparms.
24889 ttargs = current_template_args ();
24890 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24891 complain);
24892 // Finally, tack the adjusted parms onto tparms.
24893 ttparms = tree_cons (size_int (depth), ttparms,
24894 current_template_parms);
24895 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24896 }
24897 }
24898 else
24899 {
24900 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24901 tree newconst
24902 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24903 TREE_CODE (oldconst),
24904 DECL_NAME (oldconst), newtype);
24905 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
24906 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
24907 SET_DECL_TEMPLATE_PARM_P (newconst);
24908 newidx = build_template_parm_index (index, level, level,
24909 newconst, newtype);
24910 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
24911 }
24912
24913 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24914 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24915 return newdecl;
24916 }
24917
24918 /* Returns a C++17 class deduction guide template based on the constructor
24919 CTOR. */
24920
24921 static tree
24922 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
24923 {
24924 if (outer_args)
24925 ctor = tsubst (ctor, outer_args, complain, ctor);
24926 tree type = DECL_CONTEXT (ctor);
24927 tree fn_tmpl;
24928 if (TREE_CODE (ctor) == TEMPLATE_DECL)
24929 {
24930 fn_tmpl = ctor;
24931 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
24932 }
24933 else
24934 fn_tmpl = DECL_TI_TEMPLATE (ctor);
24935
24936 tree tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
24937 /* If type is a member class template, DECL_TI_ARGS (ctor) will have fully
24938 specialized args for the enclosing class. Strip those off, as the
24939 deduction guide won't have those template parameters. */
24940 tree targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
24941 TMPL_PARMS_DEPTH (tparms));
24942 /* Discard the 'this' parameter. */
24943 tree fparms = FUNCTION_ARG_CHAIN (ctor);
24944 tree fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
24945 tree ci = get_constraints (ctor);
24946
24947 if (PRIMARY_TEMPLATE_P (fn_tmpl))
24948 {
24949 /* For a member template constructor, we need to flatten the two template
24950 parameter lists into one, and then adjust the function signature
24951 accordingly. This gets...complicated. */
24952 ++processing_template_decl;
24953 tree save_parms = current_template_parms;
24954
24955 /* For a member template we should have two levels of parms/args, one for
24956 the class and one for the constructor. We stripped specialized args
24957 for further enclosing classes above. */
24958 const int depth = 2;
24959 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
24960
24961 /* Template args for translating references to the two-level template
24962 parameters into references to the one-level template parameters we are
24963 creating. */
24964 tree tsubst_args = copy_node (targs);
24965 TMPL_ARGS_LEVEL (tsubst_args, depth)
24966 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
24967
24968 /* Template parms for the constructor template. */
24969 tree ftparms = TREE_VALUE (tparms);
24970 unsigned flen = TREE_VEC_LENGTH (ftparms);
24971 /* Template parms for the class template. */
24972 tparms = TREE_CHAIN (tparms);
24973 tree ctparms = TREE_VALUE (tparms);
24974 unsigned clen = TREE_VEC_LENGTH (ctparms);
24975 /* Template parms for the deduction guide start as a copy of the template
24976 parms for the class. We set current_template_parms for
24977 lookup_template_class_1. */
24978 current_template_parms = tparms = copy_node (tparms);
24979 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
24980 for (unsigned i = 0; i < clen; ++i)
24981 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
24982
24983 /* Now we need to rewrite the constructor parms to append them to the
24984 class parms. */
24985 for (unsigned i = 0; i < flen; ++i)
24986 {
24987 unsigned index = i + clen;
24988 unsigned level = 1;
24989 tree oldelt = TREE_VEC_ELT (ftparms, i);
24990 tree olddecl = TREE_VALUE (oldelt);
24991 tree newdecl = rewrite_template_parm (olddecl, index, level,
24992 tsubst_args, complain);
24993 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
24994 tsubst_args, complain, ctor);
24995 tree list = build_tree_list (newdef, newdecl);
24996 TEMPLATE_PARM_CONSTRAINTS (list)
24997 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
24998 tsubst_args, complain, ctor);
24999 TREE_VEC_ELT (new_vec, index) = list;
25000 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25001 }
25002
25003 /* Now we have a final set of template parms to substitute into the
25004 function signature. */
25005 targs = template_parms_to_args (tparms);
25006 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25007 complain, ctor);
25008 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25009 if (ci)
25010 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25011
25012 current_template_parms = save_parms;
25013 --processing_template_decl;
25014 }
25015 else
25016 {
25017 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25018 tparms = copy_node (tparms);
25019 INNERMOST_TEMPLATE_PARMS (tparms)
25020 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25021 }
25022
25023 tree fntype = build_function_type (type, fparms);
25024 tree ded_fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (ctor),
25025 FUNCTION_DECL,
25026 dguide_name (type), fntype);
25027 DECL_ARGUMENTS (ded_fn) = fargs;
25028 DECL_ARTIFICIAL (ded_fn) = true;
25029 DECL_NONCONVERTING_P (ded_fn) = DECL_NONCONVERTING_P (ctor);
25030 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25031 DECL_ARTIFICIAL (ded_tmpl) = true;
25032 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25033 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25034 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25035 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25036 if (ci)
25037 set_constraints (ded_tmpl, ci);
25038
25039 return ded_tmpl;
25040 }
25041
25042 /* Deduce template arguments for the class template placeholder PTYPE for
25043 template TMPL based on the initializer INIT, and return the resulting
25044 type. */
25045
25046 static tree
25047 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25048 tsubst_flags_t complain)
25049 {
25050 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25051 {
25052 /* We should have handled this in the caller. */
25053 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25054 return ptype;
25055 if (complain & tf_error)
25056 error ("non-class template %qT used without template arguments", tmpl);
25057 return error_mark_node;
25058 }
25059
25060 tree type = TREE_TYPE (tmpl);
25061
25062 vec<tree,va_gc> *args;
25063 if (TREE_CODE (init) == TREE_LIST)
25064 args = make_tree_vector_from_list (init);
25065 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25066 args = make_tree_vector_from_ctor (init);
25067 else
25068 args = make_tree_vector_single (init);
25069
25070 if (args->length() == 1)
25071 {
25072 /* First try to deduce directly, since we don't have implicitly-declared
25073 constructors yet. */
25074 tree parms = build_tree_list (NULL_TREE, type);
25075 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25076 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25077 int err = type_unification_real (tparms, targs, parms, &(*args)[0],
25078 1, /*subr*/false, DEDUCE_CALL,
25079 LOOKUP_NORMAL, NULL, /*explain*/false);
25080 if (err == 0)
25081 return tsubst (type, targs, complain, tmpl);
25082 }
25083
25084 tree dname = dguide_name (tmpl);
25085 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25086 /*type*/false, /*complain*/false,
25087 /*hidden*/false);
25088 if (cands == error_mark_node)
25089 cands = NULL_TREE;
25090
25091 tree outer_args = NULL_TREE;
25092 if (DECL_CLASS_SCOPE_P (tmpl)
25093 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25094 {
25095 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25096 type = TREE_TYPE (most_general_template (tmpl));
25097 }
25098
25099 if (CLASSTYPE_METHOD_VEC (type))
25100 // FIXME cache artificial deduction guides
25101 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
25102 {
25103 tree fn = OVL_CURRENT (fns);
25104 tree guide = build_deduction_guide (fn, outer_args, complain);
25105 cands = ovl_cons (guide, cands);
25106 }
25107
25108 if (cands == NULL_TREE)
25109 {
25110 error ("cannot deduce template arguments for %qT, as it has "
25111 "no deduction guides or user-declared constructors", type);
25112 return error_mark_node;
25113 }
25114
25115 /* Prune explicit deduction guides in copy-initialization context. */
25116 tree old_cands = cands;
25117 if (flags & LOOKUP_ONLYCONVERTING)
25118 {
25119 tree t = cands;
25120 for (; t; t = OVL_NEXT (t))
25121 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (OVL_CURRENT (t))))
25122 break;
25123 if (t)
25124 {
25125 tree pruned = NULL_TREE;
25126 for (t = cands; t; t = OVL_NEXT (t))
25127 {
25128 tree f = OVL_CURRENT (t);
25129 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (f)))
25130 pruned = build_overload (f, pruned);
25131 }
25132 cands = pruned;
25133 if (cands == NULL_TREE)
25134 {
25135 error ("cannot deduce template arguments for copy-initialization"
25136 " of %qT, as it has no non-explicit deduction guides or "
25137 "user-declared constructors", type);
25138 return error_mark_node;
25139 }
25140 }
25141 }
25142
25143 ++cp_unevaluated_operand;
25144 tree t = build_new_function_call (cands, &args, /*koenig*/false,
25145 tf_decltype);
25146
25147 if (t == error_mark_node && (complain & tf_warning_or_error))
25148 {
25149 error ("class template argument deduction failed:");
25150 t = build_new_function_call (cands, &args, /*koenig*/false,
25151 complain | tf_decltype);
25152 if (old_cands != cands)
25153 inform (input_location, "explicit deduction guides not considered "
25154 "for copy-initialization");
25155 }
25156
25157 --cp_unevaluated_operand;
25158 release_tree_vector (args);
25159
25160 return TREE_TYPE (t);
25161 }
25162
25163 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25164 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25165
25166 tree
25167 do_auto_deduction (tree type, tree init, tree auto_node)
25168 {
25169 return do_auto_deduction (type, init, auto_node,
25170 tf_warning_or_error,
25171 adc_unspecified);
25172 }
25173
25174 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25175 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25176 The CONTEXT determines the context in which auto deduction is performed
25177 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25178 OUTER_TARGS are used during template argument deduction
25179 (context == adc_unify) to properly substitute the result, and is ignored
25180 in other contexts.
25181
25182 For partial-concept-ids, extra args may be appended to the list of deduced
25183 template arguments prior to determining constraint satisfaction. */
25184
25185 tree
25186 do_auto_deduction (tree type, tree init, tree auto_node,
25187 tsubst_flags_t complain, auto_deduction_context context,
25188 tree outer_targs, int flags)
25189 {
25190 tree targs;
25191
25192 if (init == error_mark_node)
25193 return error_mark_node;
25194
25195 if (type_dependent_expression_p (init)
25196 && context != adc_unify)
25197 /* Defining a subset of type-dependent expressions that we can deduce
25198 from ahead of time isn't worth the trouble. */
25199 return type;
25200
25201 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25202 /* C++17 class template argument deduction. */
25203 return do_class_deduction (type, tmpl, init, flags, complain);
25204
25205 if (TREE_TYPE (init) == NULL_TREE)
25206 /* Nothing we can do with this, even in deduction context. */
25207 return type;
25208
25209 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25210 with either a new invented type template parameter U or, if the
25211 initializer is a braced-init-list (8.5.4), with
25212 std::initializer_list<U>. */
25213 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25214 {
25215 if (!DIRECT_LIST_INIT_P (init))
25216 type = listify_autos (type, auto_node);
25217 else if (CONSTRUCTOR_NELTS (init) == 1)
25218 init = CONSTRUCTOR_ELT (init, 0)->value;
25219 else
25220 {
25221 if (complain & tf_warning_or_error)
25222 {
25223 if (permerror (input_location, "direct-list-initialization of "
25224 "%<auto%> requires exactly one element"))
25225 inform (input_location,
25226 "for deduction to %<std::initializer_list%>, use copy-"
25227 "list-initialization (i.e. add %<=%> before the %<{%>)");
25228 }
25229 type = listify_autos (type, auto_node);
25230 }
25231 }
25232
25233 if (type == error_mark_node)
25234 return error_mark_node;
25235
25236 init = resolve_nondeduced_context (init, complain);
25237
25238 if (context == adc_decomp_type
25239 && auto_node == type
25240 && init != error_mark_node
25241 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25242 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25243 and initializer has array type, deduce cv-qualified array type. */
25244 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25245 complain);
25246 else if (AUTO_IS_DECLTYPE (auto_node))
25247 {
25248 bool id = (DECL_P (init)
25249 || ((TREE_CODE (init) == COMPONENT_REF
25250 || TREE_CODE (init) == SCOPE_REF)
25251 && !REF_PARENTHESIZED_P (init)));
25252 targs = make_tree_vec (1);
25253 TREE_VEC_ELT (targs, 0)
25254 = finish_decltype_type (init, id, tf_warning_or_error);
25255 if (type != auto_node)
25256 {
25257 if (complain & tf_error)
25258 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25259 return error_mark_node;
25260 }
25261 }
25262 else
25263 {
25264 tree parms = build_tree_list (NULL_TREE, type);
25265 tree tparms;
25266
25267 if (flag_concepts)
25268 tparms = extract_autos (type);
25269 else
25270 {
25271 tparms = make_tree_vec (1);
25272 TREE_VEC_ELT (tparms, 0)
25273 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25274 }
25275
25276 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25277 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25278 DEDUCE_CALL, LOOKUP_NORMAL,
25279 NULL, /*explain_p=*/false);
25280 if (val > 0)
25281 {
25282 if (processing_template_decl)
25283 /* Try again at instantiation time. */
25284 return type;
25285 if (type && type != error_mark_node
25286 && (complain & tf_error))
25287 /* If type is error_mark_node a diagnostic must have been
25288 emitted by now. Also, having a mention to '<type error>'
25289 in the diagnostic is not really useful to the user. */
25290 {
25291 if (cfun && auto_node == current_function_auto_return_pattern
25292 && LAMBDA_FUNCTION_P (current_function_decl))
25293 error ("unable to deduce lambda return type from %qE", init);
25294 else
25295 error ("unable to deduce %qT from %qE", type, init);
25296 type_unification_real (tparms, targs, parms, &init, 1, 0,
25297 DEDUCE_CALL, LOOKUP_NORMAL,
25298 NULL, /*explain_p=*/true);
25299 }
25300 return error_mark_node;
25301 }
25302 }
25303
25304 /* Check any placeholder constraints against the deduced type. */
25305 if (flag_concepts && !processing_template_decl)
25306 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25307 {
25308 /* Use the deduced type to check the associated constraints. If we
25309 have a partial-concept-id, rebuild the argument list so that
25310 we check using the extra arguments. */
25311 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25312 tree cargs = CHECK_CONSTR_ARGS (constr);
25313 if (TREE_VEC_LENGTH (cargs) > 1)
25314 {
25315 cargs = copy_node (cargs);
25316 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25317 }
25318 else
25319 cargs = targs;
25320 if (!constraints_satisfied_p (constr, cargs))
25321 {
25322 if (complain & tf_warning_or_error)
25323 {
25324 switch (context)
25325 {
25326 case adc_unspecified:
25327 case adc_unify:
25328 error("placeholder constraints not satisfied");
25329 break;
25330 case adc_variable_type:
25331 case adc_decomp_type:
25332 error ("deduced initializer does not satisfy "
25333 "placeholder constraints");
25334 break;
25335 case adc_return_type:
25336 error ("deduced return type does not satisfy "
25337 "placeholder constraints");
25338 break;
25339 case adc_requirement:
25340 error ("deduced expression type does not satisfy "
25341 "placeholder constraints");
25342 break;
25343 }
25344 diagnose_constraints (input_location, constr, targs);
25345 }
25346 return error_mark_node;
25347 }
25348 }
25349
25350 if (processing_template_decl && context != adc_unify)
25351 outer_targs = current_template_args ();
25352 targs = add_to_template_args (outer_targs, targs);
25353 return tsubst (type, targs, complain, NULL_TREE);
25354 }
25355
25356 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25357 result. */
25358
25359 tree
25360 splice_late_return_type (tree type, tree late_return_type)
25361 {
25362 if (is_auto (type))
25363 {
25364 if (late_return_type)
25365 return late_return_type;
25366
25367 tree idx = get_template_parm_index (type);
25368 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25369 /* In an abbreviated function template we didn't know we were dealing
25370 with a function template when we saw the auto return type, so update
25371 it to have the correct level. */
25372 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25373 }
25374 return type;
25375 }
25376
25377 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25378 'decltype(auto)' or a deduced class template. */
25379
25380 bool
25381 is_auto (const_tree type)
25382 {
25383 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25384 && (TYPE_IDENTIFIER (type) == auto_identifier
25385 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25386 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25387 return true;
25388 else
25389 return false;
25390 }
25391
25392 /* for_each_template_parm callback for type_uses_auto. */
25393
25394 int
25395 is_auto_r (tree tp, void */*data*/)
25396 {
25397 return is_auto_or_concept (tp);
25398 }
25399
25400 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25401 a use of `auto'. Returns NULL_TREE otherwise. */
25402
25403 tree
25404 type_uses_auto (tree type)
25405 {
25406 if (type == NULL_TREE)
25407 return NULL_TREE;
25408 else if (flag_concepts)
25409 {
25410 /* The Concepts TS allows multiple autos in one type-specifier; just
25411 return the first one we find, do_auto_deduction will collect all of
25412 them. */
25413 if (uses_template_parms (type))
25414 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25415 /*visited*/NULL, /*nondeduced*/true);
25416 else
25417 return NULL_TREE;
25418 }
25419 else
25420 return find_type_usage (type, is_auto);
25421 }
25422
25423 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25424 'decltype(auto)' or a concept. */
25425
25426 bool
25427 is_auto_or_concept (const_tree type)
25428 {
25429 return is_auto (type); // or concept
25430 }
25431
25432 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25433 a concept identifier) iff TYPE contains a use of a generic type. Returns
25434 NULL_TREE otherwise. */
25435
25436 tree
25437 type_uses_auto_or_concept (tree type)
25438 {
25439 return find_type_usage (type, is_auto_or_concept);
25440 }
25441
25442
25443 /* For a given template T, return the vector of typedefs referenced
25444 in T for which access check is needed at T instantiation time.
25445 T is either a FUNCTION_DECL or a RECORD_TYPE.
25446 Those typedefs were added to T by the function
25447 append_type_to_template_for_access_check. */
25448
25449 vec<qualified_typedef_usage_t, va_gc> *
25450 get_types_needing_access_check (tree t)
25451 {
25452 tree ti;
25453 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25454
25455 if (!t || t == error_mark_node)
25456 return NULL;
25457
25458 if (!(ti = get_template_info (t)))
25459 return NULL;
25460
25461 if (CLASS_TYPE_P (t)
25462 || TREE_CODE (t) == FUNCTION_DECL)
25463 {
25464 if (!TI_TEMPLATE (ti))
25465 return NULL;
25466
25467 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25468 }
25469
25470 return result;
25471 }
25472
25473 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25474 tied to T. That list of typedefs will be access checked at
25475 T instantiation time.
25476 T is either a FUNCTION_DECL or a RECORD_TYPE.
25477 TYPE_DECL is a TYPE_DECL node representing a typedef.
25478 SCOPE is the scope through which TYPE_DECL is accessed.
25479 LOCATION is the location of the usage point of TYPE_DECL.
25480
25481 This function is a subroutine of
25482 append_type_to_template_for_access_check. */
25483
25484 static void
25485 append_type_to_template_for_access_check_1 (tree t,
25486 tree type_decl,
25487 tree scope,
25488 location_t location)
25489 {
25490 qualified_typedef_usage_t typedef_usage;
25491 tree ti;
25492
25493 if (!t || t == error_mark_node)
25494 return;
25495
25496 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25497 || CLASS_TYPE_P (t))
25498 && type_decl
25499 && TREE_CODE (type_decl) == TYPE_DECL
25500 && scope);
25501
25502 if (!(ti = get_template_info (t)))
25503 return;
25504
25505 gcc_assert (TI_TEMPLATE (ti));
25506
25507 typedef_usage.typedef_decl = type_decl;
25508 typedef_usage.context = scope;
25509 typedef_usage.locus = location;
25510
25511 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25512 }
25513
25514 /* Append TYPE_DECL to the template TEMPL.
25515 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25516 At TEMPL instanciation time, TYPE_DECL will be checked to see
25517 if it can be accessed through SCOPE.
25518 LOCATION is the location of the usage point of TYPE_DECL.
25519
25520 e.g. consider the following code snippet:
25521
25522 class C
25523 {
25524 typedef int myint;
25525 };
25526
25527 template<class U> struct S
25528 {
25529 C::myint mi; // <-- usage point of the typedef C::myint
25530 };
25531
25532 S<char> s;
25533
25534 At S<char> instantiation time, we need to check the access of C::myint
25535 In other words, we need to check the access of the myint typedef through
25536 the C scope. For that purpose, this function will add the myint typedef
25537 and the scope C through which its being accessed to a list of typedefs
25538 tied to the template S. That list will be walked at template instantiation
25539 time and access check performed on each typedefs it contains.
25540 Note that this particular code snippet should yield an error because
25541 myint is private to C. */
25542
25543 void
25544 append_type_to_template_for_access_check (tree templ,
25545 tree type_decl,
25546 tree scope,
25547 location_t location)
25548 {
25549 qualified_typedef_usage_t *iter;
25550 unsigned i;
25551
25552 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25553
25554 /* Make sure we don't append the type to the template twice. */
25555 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25556 if (iter->typedef_decl == type_decl && scope == iter->context)
25557 return;
25558
25559 append_type_to_template_for_access_check_1 (templ, type_decl,
25560 scope, location);
25561 }
25562
25563 /* Convert the generic type parameters in PARM that match the types given in the
25564 range [START_IDX, END_IDX) from the current_template_parms into generic type
25565 packs. */
25566
25567 tree
25568 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25569 {
25570 tree current = current_template_parms;
25571 int depth = TMPL_PARMS_DEPTH (current);
25572 current = INNERMOST_TEMPLATE_PARMS (current);
25573 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25574
25575 for (int i = 0; i < start_idx; ++i)
25576 TREE_VEC_ELT (replacement, i)
25577 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25578
25579 for (int i = start_idx; i < end_idx; ++i)
25580 {
25581 /* Create a distinct parameter pack type from the current parm and add it
25582 to the replacement args to tsubst below into the generic function
25583 parameter. */
25584
25585 tree o = TREE_TYPE (TREE_VALUE
25586 (TREE_VEC_ELT (current, i)));
25587 tree t = copy_type (o);
25588 TEMPLATE_TYPE_PARM_INDEX (t)
25589 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25590 o, 0, 0, tf_none);
25591 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25592 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25593 TYPE_MAIN_VARIANT (t) = t;
25594 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25595 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25596 TREE_VEC_ELT (replacement, i) = t;
25597 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25598 }
25599
25600 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25601 TREE_VEC_ELT (replacement, i)
25602 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25603
25604 /* If there are more levels then build up the replacement with the outer
25605 template parms. */
25606 if (depth > 1)
25607 replacement = add_to_template_args (template_parms_to_args
25608 (TREE_CHAIN (current_template_parms)),
25609 replacement);
25610
25611 return tsubst (parm, replacement, tf_none, NULL_TREE);
25612 }
25613
25614 /* Entries in the decl_constraint hash table. */
25615 struct GTY((for_user)) constr_entry
25616 {
25617 tree decl;
25618 tree ci;
25619 };
25620
25621 /* Hashing function and equality for constraint entries. */
25622 struct constr_hasher : ggc_ptr_hash<constr_entry>
25623 {
25624 static hashval_t hash (constr_entry *e)
25625 {
25626 return (hashval_t)DECL_UID (e->decl);
25627 }
25628
25629 static bool equal (constr_entry *e1, constr_entry *e2)
25630 {
25631 return e1->decl == e2->decl;
25632 }
25633 };
25634
25635 /* A mapping from declarations to constraint information. Note that
25636 both templates and their underlying declarations are mapped to the
25637 same constraint information.
25638
25639 FIXME: This is defined in pt.c because garbage collection
25640 code is not being generated for constraint.cc. */
25641
25642 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25643
25644 /* Returns the template constraints of declaration T. If T is not
25645 constrained, return NULL_TREE. Note that T must be non-null. */
25646
25647 tree
25648 get_constraints (tree t)
25649 {
25650 if (!flag_concepts)
25651 return NULL_TREE;
25652
25653 gcc_assert (DECL_P (t));
25654 if (TREE_CODE (t) == TEMPLATE_DECL)
25655 t = DECL_TEMPLATE_RESULT (t);
25656 constr_entry elt = { t, NULL_TREE };
25657 constr_entry* found = decl_constraints->find (&elt);
25658 if (found)
25659 return found->ci;
25660 else
25661 return NULL_TREE;
25662 }
25663
25664 /* Associate the given constraint information CI with the declaration
25665 T. If T is a template, then the constraints are associated with
25666 its underlying declaration. Don't build associations if CI is
25667 NULL_TREE. */
25668
25669 void
25670 set_constraints (tree t, tree ci)
25671 {
25672 if (!ci)
25673 return;
25674 gcc_assert (t && flag_concepts);
25675 if (TREE_CODE (t) == TEMPLATE_DECL)
25676 t = DECL_TEMPLATE_RESULT (t);
25677 gcc_assert (!get_constraints (t));
25678 constr_entry elt = {t, ci};
25679 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25680 constr_entry* entry = ggc_alloc<constr_entry> ();
25681 *entry = elt;
25682 *slot = entry;
25683 }
25684
25685 /* Remove the associated constraints of the declaration T. */
25686
25687 void
25688 remove_constraints (tree t)
25689 {
25690 gcc_assert (DECL_P (t));
25691 if (TREE_CODE (t) == TEMPLATE_DECL)
25692 t = DECL_TEMPLATE_RESULT (t);
25693
25694 constr_entry elt = {t, NULL_TREE};
25695 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25696 if (slot)
25697 decl_constraints->clear_slot (slot);
25698 }
25699
25700 /* Memoized satisfaction results for declarations. This
25701 maps the pair (constraint_info, arguments) to the result computed
25702 by constraints_satisfied_p. */
25703
25704 struct GTY((for_user)) constraint_sat_entry
25705 {
25706 tree ci;
25707 tree args;
25708 tree result;
25709 };
25710
25711 /* Hashing function and equality for constraint entries. */
25712
25713 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25714 {
25715 static hashval_t hash (constraint_sat_entry *e)
25716 {
25717 hashval_t val = iterative_hash_object(e->ci, 0);
25718 return iterative_hash_template_arg (e->args, val);
25719 }
25720
25721 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25722 {
25723 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25724 }
25725 };
25726
25727 /* Memoized satisfaction results for concept checks. */
25728
25729 struct GTY((for_user)) concept_spec_entry
25730 {
25731 tree tmpl;
25732 tree args;
25733 tree result;
25734 };
25735
25736 /* Hashing function and equality for constraint entries. */
25737
25738 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25739 {
25740 static hashval_t hash (concept_spec_entry *e)
25741 {
25742 return hash_tmpl_and_args (e->tmpl, e->args);
25743 }
25744
25745 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25746 {
25747 ++comparing_specializations;
25748 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25749 --comparing_specializations;
25750 return eq;
25751 }
25752 };
25753
25754 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25755 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25756
25757 /* Search for a memoized satisfaction result. Returns one of the
25758 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25759
25760 tree
25761 lookup_constraint_satisfaction (tree ci, tree args)
25762 {
25763 constraint_sat_entry elt = { ci, args, NULL_TREE };
25764 constraint_sat_entry* found = constraint_memos->find (&elt);
25765 if (found)
25766 return found->result;
25767 else
25768 return NULL_TREE;
25769 }
25770
25771 /* Memoize the result of a satisfication test. Returns the saved result. */
25772
25773 tree
25774 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25775 {
25776 constraint_sat_entry elt = {ci, args, result};
25777 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25778 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25779 *entry = elt;
25780 *slot = entry;
25781 return result;
25782 }
25783
25784 /* Search for a memoized satisfaction result for a concept. */
25785
25786 tree
25787 lookup_concept_satisfaction (tree tmpl, tree args)
25788 {
25789 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25790 concept_spec_entry* found = concept_memos->find (&elt);
25791 if (found)
25792 return found->result;
25793 else
25794 return NULL_TREE;
25795 }
25796
25797 /* Memoize the result of a concept check. Returns the saved result. */
25798
25799 tree
25800 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25801 {
25802 concept_spec_entry elt = {tmpl, args, result};
25803 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25804 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25805 *entry = elt;
25806 *slot = entry;
25807 return result;
25808 }
25809
25810 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25811
25812 /* Returns a prior concept specialization. This returns the substituted
25813 and normalized constraints defined by the concept. */
25814
25815 tree
25816 get_concept_expansion (tree tmpl, tree args)
25817 {
25818 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25819 concept_spec_entry* found = concept_expansions->find (&elt);
25820 if (found)
25821 return found->result;
25822 else
25823 return NULL_TREE;
25824 }
25825
25826 /* Save a concept expansion for later. */
25827
25828 tree
25829 save_concept_expansion (tree tmpl, tree args, tree def)
25830 {
25831 concept_spec_entry elt = {tmpl, args, def};
25832 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25833 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25834 *entry = elt;
25835 *slot = entry;
25836 return def;
25837 }
25838
25839 static hashval_t
25840 hash_subsumption_args (tree t1, tree t2)
25841 {
25842 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25843 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25844 int val = 0;
25845 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25846 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25847 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25848 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25849 return val;
25850 }
25851
25852 /* Compare the constraints of two subsumption entries. The LEFT1 and
25853 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25854 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25855
25856 static bool
25857 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25858 {
25859 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25860 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25861 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25862 CHECK_CONSTR_ARGS (right1)))
25863 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25864 CHECK_CONSTR_ARGS (right2));
25865 return false;
25866 }
25867
25868 /* Key/value pair for learning and memoizing subsumption results. This
25869 associates a pair of check constraints (including arguments) with
25870 a boolean value indicating the result. */
25871
25872 struct GTY((for_user)) subsumption_entry
25873 {
25874 tree t1;
25875 tree t2;
25876 bool result;
25877 };
25878
25879 /* Hashing function and equality for constraint entries. */
25880
25881 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
25882 {
25883 static hashval_t hash (subsumption_entry *e)
25884 {
25885 return hash_subsumption_args (e->t1, e->t2);
25886 }
25887
25888 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
25889 {
25890 ++comparing_specializations;
25891 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
25892 --comparing_specializations;
25893 return eq;
25894 }
25895 };
25896
25897 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
25898
25899 /* Search for a previously cached subsumption result. */
25900
25901 bool*
25902 lookup_subsumption_result (tree t1, tree t2)
25903 {
25904 subsumption_entry elt = { t1, t2, false };
25905 subsumption_entry* found = subsumption_table->find (&elt);
25906 if (found)
25907 return &found->result;
25908 else
25909 return 0;
25910 }
25911
25912 /* Save a subsumption result. */
25913
25914 bool
25915 save_subsumption_result (tree t1, tree t2, bool result)
25916 {
25917 subsumption_entry elt = {t1, t2, result};
25918 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
25919 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
25920 *entry = elt;
25921 *slot = entry;
25922 return result;
25923 }
25924
25925 /* Set up the hash table for constraint association. */
25926
25927 void
25928 init_constraint_processing (void)
25929 {
25930 if (!flag_concepts)
25931 return;
25932
25933 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
25934 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
25935 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
25936 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
25937 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
25938 }
25939
25940 /* Set up the hash tables for template instantiations. */
25941
25942 void
25943 init_template_processing (void)
25944 {
25945 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
25946 type_specializations = hash_table<spec_hasher>::create_ggc (37);
25947 }
25948
25949 /* Print stats about the template hash tables for -fstats. */
25950
25951 void
25952 print_template_statistics (void)
25953 {
25954 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
25955 "%f collisions\n", (long) decl_specializations->size (),
25956 (long) decl_specializations->elements (),
25957 decl_specializations->collisions ());
25958 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
25959 "%f collisions\n", (long) type_specializations->size (),
25960 (long) type_specializations->elements (),
25961 type_specializations->collisions ());
25962 }
25963
25964 #include "gt-cp-pt.h"