]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Rename TYPE_ANONYMOUS_P to TYPE_UNNAMED_P.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2016 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);
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
218 /* Make the current scope suitable for access checking when we are
219 processing T. T can be FUNCTION_DECL for instantiated function
220 template, VAR_DECL for static member variable, or TYPE_DECL for
221 alias template (needed by instantiate_decl). */
222
223 static void
224 push_access_scope (tree t)
225 {
226 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
227 || TREE_CODE (t) == TYPE_DECL);
228
229 if (DECL_FRIEND_CONTEXT (t))
230 push_nested_class (DECL_FRIEND_CONTEXT (t));
231 else if (DECL_CLASS_SCOPE_P (t))
232 push_nested_class (DECL_CONTEXT (t));
233 else
234 push_to_top_level ();
235
236 if (TREE_CODE (t) == FUNCTION_DECL)
237 {
238 saved_access_scope = tree_cons
239 (NULL_TREE, current_function_decl, saved_access_scope);
240 current_function_decl = t;
241 }
242 }
243
244 /* Restore the scope set up by push_access_scope. T is the node we
245 are processing. */
246
247 static void
248 pop_access_scope (tree t)
249 {
250 if (TREE_CODE (t) == FUNCTION_DECL)
251 {
252 current_function_decl = TREE_VALUE (saved_access_scope);
253 saved_access_scope = TREE_CHAIN (saved_access_scope);
254 }
255
256 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
257 pop_nested_class ();
258 else
259 pop_from_top_level ();
260 }
261
262 /* Do any processing required when DECL (a member template
263 declaration) is finished. Returns the TEMPLATE_DECL corresponding
264 to DECL, unless it is a specialization, in which case the DECL
265 itself is returned. */
266
267 tree
268 finish_member_template_decl (tree decl)
269 {
270 if (decl == error_mark_node)
271 return error_mark_node;
272
273 gcc_assert (DECL_P (decl));
274
275 if (TREE_CODE (decl) == TYPE_DECL)
276 {
277 tree type;
278
279 type = TREE_TYPE (decl);
280 if (type == error_mark_node)
281 return error_mark_node;
282 if (MAYBE_CLASS_TYPE_P (type)
283 && CLASSTYPE_TEMPLATE_INFO (type)
284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
285 {
286 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
287 check_member_template (tmpl);
288 return tmpl;
289 }
290 return NULL_TREE;
291 }
292 else if (TREE_CODE (decl) == FIELD_DECL)
293 error ("data member %qD cannot be a member template", decl);
294 else if (DECL_TEMPLATE_INFO (decl))
295 {
296 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
297 {
298 check_member_template (DECL_TI_TEMPLATE (decl));
299 return DECL_TI_TEMPLATE (decl);
300 }
301 else
302 return decl;
303 }
304 else
305 error ("invalid member template declaration %qD", decl);
306
307 return error_mark_node;
308 }
309
310 /* Create a template info node. */
311
312 tree
313 build_template_info (tree template_decl, tree template_args)
314 {
315 tree result = make_node (TEMPLATE_INFO);
316 TI_TEMPLATE (result) = template_decl;
317 TI_ARGS (result) = template_args;
318 return result;
319 }
320
321 /* Return the template info node corresponding to T, whatever T is. */
322
323 tree
324 get_template_info (const_tree t)
325 {
326 tree tinfo = NULL_TREE;
327
328 if (!t || t == error_mark_node)
329 return NULL;
330
331 if (TREE_CODE (t) == NAMESPACE_DECL
332 || TREE_CODE (t) == PARM_DECL)
333 return NULL;
334
335 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
336 tinfo = DECL_TEMPLATE_INFO (t);
337
338 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
339 t = TREE_TYPE (t);
340
341 if (OVERLOAD_TYPE_P (t))
342 tinfo = TYPE_TEMPLATE_INFO (t);
343 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
344 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
345
346 return tinfo;
347 }
348
349 /* Returns the template nesting level of the indicated class TYPE.
350
351 For example, in:
352 template <class T>
353 struct A
354 {
355 template <class U>
356 struct B {};
357 };
358
359 A<T>::B<U> has depth two, while A<T> has depth one.
360 Both A<T>::B<int> and A<int>::B<U> have depth one, if
361 they are instantiations, not specializations.
362
363 This function is guaranteed to return 0 if passed NULL_TREE so
364 that, for example, `template_class_depth (current_class_type)' is
365 always safe. */
366
367 int
368 template_class_depth (tree type)
369 {
370 int depth;
371
372 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
373 {
374 tree tinfo = get_template_info (type);
375
376 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
377 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
378 ++depth;
379
380 if (DECL_P (type))
381 type = CP_DECL_CONTEXT (type);
382 else if (LAMBDA_TYPE_P (type))
383 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
384 else
385 type = CP_TYPE_CONTEXT (type);
386 }
387
388 return depth;
389 }
390
391 /* Subroutine of maybe_begin_member_template_processing.
392 Returns true if processing DECL needs us to push template parms. */
393
394 static bool
395 inline_needs_template_parms (tree decl, bool nsdmi)
396 {
397 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
398 return false;
399
400 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
401 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
402 }
403
404 /* Subroutine of maybe_begin_member_template_processing.
405 Push the template parms in PARMS, starting from LEVELS steps into the
406 chain, and ending at the beginning, since template parms are listed
407 innermost first. */
408
409 static void
410 push_inline_template_parms_recursive (tree parmlist, int levels)
411 {
412 tree parms = TREE_VALUE (parmlist);
413 int i;
414
415 if (levels > 1)
416 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
417
418 ++processing_template_decl;
419 current_template_parms
420 = tree_cons (size_int (processing_template_decl),
421 parms, current_template_parms);
422 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
423
424 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
425 NULL);
426 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
427 {
428 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
429
430 if (error_operand_p (parm))
431 continue;
432
433 gcc_assert (DECL_P (parm));
434
435 switch (TREE_CODE (parm))
436 {
437 case TYPE_DECL:
438 case TEMPLATE_DECL:
439 pushdecl (parm);
440 break;
441
442 case PARM_DECL:
443 /* Push the CONST_DECL. */
444 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
445 break;
446
447 default:
448 gcc_unreachable ();
449 }
450 }
451 }
452
453 /* Restore the template parameter context for a member template, a
454 friend template defined in a class definition, or a non-template
455 member of template class. */
456
457 void
458 maybe_begin_member_template_processing (tree decl)
459 {
460 tree parms;
461 int levels = 0;
462 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
463
464 if (nsdmi)
465 {
466 tree ctx = DECL_CONTEXT (decl);
467 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
468 /* Disregard full specializations (c++/60999). */
469 && uses_template_parms (ctx)
470 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
471 }
472
473 if (inline_needs_template_parms (decl, nsdmi))
474 {
475 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
476 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
477
478 if (DECL_TEMPLATE_SPECIALIZATION (decl))
479 {
480 --levels;
481 parms = TREE_CHAIN (parms);
482 }
483
484 push_inline_template_parms_recursive (parms, levels);
485 }
486
487 /* Remember how many levels of template parameters we pushed so that
488 we can pop them later. */
489 inline_parm_levels.safe_push (levels);
490 }
491
492 /* Undo the effects of maybe_begin_member_template_processing. */
493
494 void
495 maybe_end_member_template_processing (void)
496 {
497 int i;
498 int last;
499
500 if (inline_parm_levels.length () == 0)
501 return;
502
503 last = inline_parm_levels.pop ();
504 for (i = 0; i < last; ++i)
505 {
506 --processing_template_decl;
507 current_template_parms = TREE_CHAIN (current_template_parms);
508 poplevel (0, 0, 0);
509 }
510 }
511
512 /* Return a new template argument vector which contains all of ARGS,
513 but has as its innermost set of arguments the EXTRA_ARGS. */
514
515 static tree
516 add_to_template_args (tree args, tree extra_args)
517 {
518 tree new_args;
519 int extra_depth;
520 int i;
521 int j;
522
523 if (args == NULL_TREE || extra_args == error_mark_node)
524 return extra_args;
525
526 extra_depth = TMPL_ARGS_DEPTH (extra_args);
527 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
528
529 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
530 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
531
532 for (j = 1; j <= extra_depth; ++j, ++i)
533 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
534
535 return new_args;
536 }
537
538 /* Like add_to_template_args, but only the outermost ARGS are added to
539 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
540 (EXTRA_ARGS) levels are added. This function is used to combine
541 the template arguments from a partial instantiation with the
542 template arguments used to attain the full instantiation from the
543 partial instantiation. */
544
545 static tree
546 add_outermost_template_args (tree args, tree extra_args)
547 {
548 tree new_args;
549
550 /* If there are more levels of EXTRA_ARGS than there are ARGS,
551 something very fishy is going on. */
552 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
553
554 /* If *all* the new arguments will be the EXTRA_ARGS, just return
555 them. */
556 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
557 return extra_args;
558
559 /* For the moment, we make ARGS look like it contains fewer levels. */
560 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
561
562 new_args = add_to_template_args (args, extra_args);
563
564 /* Now, we restore ARGS to its full dimensions. */
565 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
566
567 return new_args;
568 }
569
570 /* Return the N levels of innermost template arguments from the ARGS. */
571
572 tree
573 get_innermost_template_args (tree args, int n)
574 {
575 tree new_args;
576 int extra_levels;
577 int i;
578
579 gcc_assert (n >= 0);
580
581 /* If N is 1, just return the innermost set of template arguments. */
582 if (n == 1)
583 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
584
585 /* If we're not removing anything, just return the arguments we were
586 given. */
587 extra_levels = TMPL_ARGS_DEPTH (args) - n;
588 gcc_assert (extra_levels >= 0);
589 if (extra_levels == 0)
590 return args;
591
592 /* Make a new set of arguments, not containing the outer arguments. */
593 new_args = make_tree_vec (n);
594 for (i = 1; i <= n; ++i)
595 SET_TMPL_ARGS_LEVEL (new_args, i,
596 TMPL_ARGS_LEVEL (args, i + extra_levels));
597
598 return new_args;
599 }
600
601 /* The inverse of get_innermost_template_args: Return all but the innermost
602 EXTRA_LEVELS levels of template arguments from the ARGS. */
603
604 static tree
605 strip_innermost_template_args (tree args, int extra_levels)
606 {
607 tree new_args;
608 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
609 int i;
610
611 gcc_assert (n >= 0);
612
613 /* If N is 1, just return the outermost set of template arguments. */
614 if (n == 1)
615 return TMPL_ARGS_LEVEL (args, 1);
616
617 /* If we're not removing anything, just return the arguments we were
618 given. */
619 gcc_assert (extra_levels >= 0);
620 if (extra_levels == 0)
621 return args;
622
623 /* Make a new set of arguments, not containing the inner arguments. */
624 new_args = make_tree_vec (n);
625 for (i = 1; i <= n; ++i)
626 SET_TMPL_ARGS_LEVEL (new_args, i,
627 TMPL_ARGS_LEVEL (args, i));
628
629 return new_args;
630 }
631
632 /* We've got a template header coming up; push to a new level for storing
633 the parms. */
634
635 void
636 begin_template_parm_list (void)
637 {
638 /* We use a non-tag-transparent scope here, which causes pushtag to
639 put tags in this scope, rather than in the enclosing class or
640 namespace scope. This is the right thing, since we want
641 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
642 global template class, push_template_decl handles putting the
643 TEMPLATE_DECL into top-level scope. For a nested template class,
644 e.g.:
645
646 template <class T> struct S1 {
647 template <class T> struct S2 {};
648 };
649
650 pushtag contains special code to call pushdecl_with_scope on the
651 TEMPLATE_DECL for S2. */
652 begin_scope (sk_template_parms, NULL);
653 ++processing_template_decl;
654 ++processing_template_parmlist;
655 note_template_header (0);
656
657 /* Add a dummy parameter level while we process the parameter list. */
658 current_template_parms
659 = tree_cons (size_int (processing_template_decl),
660 make_tree_vec (0),
661 current_template_parms);
662 }
663
664 /* This routine is called when a specialization is declared. If it is
665 invalid to declare a specialization here, an error is reported and
666 false is returned, otherwise this routine will return true. */
667
668 static bool
669 check_specialization_scope (void)
670 {
671 tree scope = current_scope ();
672
673 /* [temp.expl.spec]
674
675 An explicit specialization shall be declared in the namespace of
676 which the template is a member, or, for member templates, in the
677 namespace of which the enclosing class or enclosing class
678 template is a member. An explicit specialization of a member
679 function, member class or static data member of a class template
680 shall be declared in the namespace of which the class template
681 is a member. */
682 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
683 {
684 error ("explicit specialization in non-namespace scope %qD", scope);
685 return false;
686 }
687
688 /* [temp.expl.spec]
689
690 In an explicit specialization declaration for a member of a class
691 template or a member template that appears in namespace scope,
692 the member template and some of its enclosing class templates may
693 remain unspecialized, except that the declaration shall not
694 explicitly specialize a class member template if its enclosing
695 class templates are not explicitly specialized as well. */
696 if (current_template_parms)
697 {
698 error ("enclosing class templates are not explicitly specialized");
699 return false;
700 }
701
702 return true;
703 }
704
705 /* We've just seen template <>. */
706
707 bool
708 begin_specialization (void)
709 {
710 begin_scope (sk_template_spec, NULL);
711 note_template_header (1);
712 return check_specialization_scope ();
713 }
714
715 /* Called at then end of processing a declaration preceded by
716 template<>. */
717
718 void
719 end_specialization (void)
720 {
721 finish_scope ();
722 reset_specialization ();
723 }
724
725 /* Any template <>'s that we have seen thus far are not referring to a
726 function specialization. */
727
728 void
729 reset_specialization (void)
730 {
731 processing_specialization = 0;
732 template_header_count = 0;
733 }
734
735 /* We've just seen a template header. If SPECIALIZATION is nonzero,
736 it was of the form template <>. */
737
738 static void
739 note_template_header (int specialization)
740 {
741 processing_specialization = specialization;
742 template_header_count++;
743 }
744
745 /* We're beginning an explicit instantiation. */
746
747 void
748 begin_explicit_instantiation (void)
749 {
750 gcc_assert (!processing_explicit_instantiation);
751 processing_explicit_instantiation = true;
752 }
753
754
755 void
756 end_explicit_instantiation (void)
757 {
758 gcc_assert (processing_explicit_instantiation);
759 processing_explicit_instantiation = false;
760 }
761
762 /* An explicit specialization or partial specialization of TMPL is being
763 declared. Check that the namespace in which the specialization is
764 occurring is permissible. Returns false iff it is invalid to
765 specialize TMPL in the current namespace. */
766
767 static bool
768 check_specialization_namespace (tree tmpl)
769 {
770 tree tpl_ns = decl_namespace_context (tmpl);
771
772 /* [tmpl.expl.spec]
773
774 An explicit specialization shall be declared in the namespace of
775 which the template is a member, or, for member templates, in the
776 namespace of which the enclosing class or enclosing class
777 template is a member. An explicit specialization of a member
778 function, member class or static data member of a class template
779 shall be declared in the namespace of which the class template is
780 a member. */
781 if (current_scope() != DECL_CONTEXT (tmpl)
782 && !at_namespace_scope_p ())
783 {
784 error ("specialization of %qD must appear at namespace scope", tmpl);
785 return false;
786 }
787 if (is_associated_namespace (current_namespace, tpl_ns))
788 /* Same or super-using namespace. */
789 return true;
790 else
791 {
792 permerror (input_location,
793 "specialization of %qD in different namespace", tmpl);
794 permerror (DECL_SOURCE_LOCATION (tmpl),
795 " from definition of %q#D", tmpl);
796 return false;
797 }
798 }
799
800 /* SPEC is an explicit instantiation. Check that it is valid to
801 perform this explicit instantiation in the current namespace. */
802
803 static void
804 check_explicit_instantiation_namespace (tree spec)
805 {
806 tree ns;
807
808 /* DR 275: An explicit instantiation shall appear in an enclosing
809 namespace of its template. */
810 ns = decl_namespace_context (spec);
811 if (!is_ancestor (current_namespace, ns))
812 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
813 "(which does not enclose namespace %qD)",
814 spec, current_namespace, ns);
815 }
816
817 // Returns the type of a template specialization only if that
818 // specialization needs to be defined. Otherwise (e.g., if the type has
819 // already been defined), the function returns NULL_TREE.
820 static tree
821 maybe_new_partial_specialization (tree type)
822 {
823 // An implicit instantiation of an incomplete type implies
824 // the definition of a new class template.
825 //
826 // template<typename T>
827 // struct S;
828 //
829 // template<typename T>
830 // struct S<T*>;
831 //
832 // Here, S<T*> is an implicit instantiation of S whose type
833 // is incomplete.
834 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
835 return type;
836
837 // It can also be the case that TYPE is a completed specialization.
838 // Continuing the previous example, suppose we also declare:
839 //
840 // template<typename T>
841 // requires Integral<T>
842 // struct S<T*>;
843 //
844 // Here, S<T*> refers to the specialization S<T*> defined
845 // above. However, we need to differentiate definitions because
846 // we intend to define a new partial specialization. In this case,
847 // we rely on the fact that the constraints are different for
848 // this declaration than that above.
849 //
850 // Note that we also get here for injected class names and
851 // late-parsed template definitions. We must ensure that we
852 // do not create new type declarations for those cases.
853 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
854 {
855 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
856 tree args = CLASSTYPE_TI_ARGS (type);
857
858 // If there are no template parameters, this cannot be a new
859 // partial template specializtion?
860 if (!current_template_parms)
861 return NULL_TREE;
862
863 // The injected-class-name is not a new partial specialization.
864 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
865 return NULL_TREE;
866
867 // If the constraints are not the same as those of the primary
868 // then, we can probably create a new specialization.
869 tree type_constr = current_template_constraints ();
870
871 if (type == TREE_TYPE (tmpl))
872 {
873 tree main_constr = get_constraints (tmpl);
874 if (equivalent_constraints (type_constr, main_constr))
875 return NULL_TREE;
876 }
877
878 // Also, if there's a pre-existing specialization with matching
879 // constraints, then this also isn't new.
880 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
881 while (specs)
882 {
883 tree spec_tmpl = TREE_VALUE (specs);
884 tree spec_args = TREE_PURPOSE (specs);
885 tree spec_constr = get_constraints (spec_tmpl);
886 if (comp_template_args (args, spec_args)
887 && equivalent_constraints (type_constr, spec_constr))
888 return NULL_TREE;
889 specs = TREE_CHAIN (specs);
890 }
891
892 // Create a new type node (and corresponding type decl)
893 // for the newly declared specialization.
894 tree t = make_class_type (TREE_CODE (type));
895 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
896 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
897 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
898
899 /* We only need a separate type node for storing the definition of this
900 partial specialization; uses of S<T*> are unconstrained, so all are
901 equivalent. So keep TYPE_CANONICAL the same. */
902 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
903
904 // Build the corresponding type decl.
905 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
906 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
907 DECL_SOURCE_LOCATION (d) = input_location;
908
909 return t;
910 }
911
912 return NULL_TREE;
913 }
914
915 /* The TYPE is being declared. If it is a template type, that means it
916 is a partial specialization. Do appropriate error-checking. */
917
918 tree
919 maybe_process_partial_specialization (tree type)
920 {
921 tree context;
922
923 if (type == error_mark_node)
924 return error_mark_node;
925
926 /* A lambda that appears in specialization context is not itself a
927 specialization. */
928 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
929 return type;
930
931 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
932 {
933 error ("name of class shadows template template parameter %qD",
934 TYPE_NAME (type));
935 return error_mark_node;
936 }
937
938 context = TYPE_CONTEXT (type);
939
940 if (TYPE_ALIAS_P (type))
941 {
942 if (TYPE_TEMPLATE_INFO (type)
943 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
944 error ("specialization of alias template %qD",
945 TYPE_TI_TEMPLATE (type));
946 else
947 error ("explicit specialization of non-template %qT", type);
948 return error_mark_node;
949 }
950 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
951 {
952 /* This is for ordinary explicit specialization and partial
953 specialization of a template class such as:
954
955 template <> class C<int>;
956
957 or:
958
959 template <class T> class C<T*>;
960
961 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
962
963 if (tree t = maybe_new_partial_specialization (type))
964 {
965 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
966 && !at_namespace_scope_p ())
967 return error_mark_node;
968 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
969 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
970 if (processing_template_decl)
971 {
972 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
973 if (decl == error_mark_node)
974 return error_mark_node;
975 return TREE_TYPE (decl);
976 }
977 }
978 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
979 error ("specialization of %qT after instantiation", type);
980 else if (errorcount && !processing_specialization
981 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
982 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
983 /* Trying to define a specialization either without a template<> header
984 or in an inappropriate place. We've already given an error, so just
985 bail now so we don't actually define the specialization. */
986 return error_mark_node;
987 }
988 else if (CLASS_TYPE_P (type)
989 && !CLASSTYPE_USE_TEMPLATE (type)
990 && CLASSTYPE_TEMPLATE_INFO (type)
991 && context && CLASS_TYPE_P (context)
992 && CLASSTYPE_TEMPLATE_INFO (context))
993 {
994 /* This is for an explicit specialization of member class
995 template according to [temp.expl.spec/18]:
996
997 template <> template <class U> class C<int>::D;
998
999 The context `C<int>' must be an implicit instantiation.
1000 Otherwise this is just a member class template declared
1001 earlier like:
1002
1003 template <> class C<int> { template <class U> class D; };
1004 template <> template <class U> class C<int>::D;
1005
1006 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1007 while in the second case, `C<int>::D' is a primary template
1008 and `C<T>::D' may not exist. */
1009
1010 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1011 && !COMPLETE_TYPE_P (type))
1012 {
1013 tree t;
1014 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1015
1016 if (current_namespace
1017 != decl_namespace_context (tmpl))
1018 {
1019 permerror (input_location,
1020 "specializing %q#T in different namespace", type);
1021 permerror (DECL_SOURCE_LOCATION (tmpl),
1022 " from definition of %q#D", tmpl);
1023 }
1024
1025 /* Check for invalid specialization after instantiation:
1026
1027 template <> template <> class C<int>::D<int>;
1028 template <> template <class U> class C<int>::D; */
1029
1030 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1031 t; t = TREE_CHAIN (t))
1032 {
1033 tree inst = TREE_VALUE (t);
1034 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1035 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1036 {
1037 /* We already have a full specialization of this partial
1038 instantiation, or a full specialization has been
1039 looked up but not instantiated. Reassign it to the
1040 new member specialization template. */
1041 spec_entry elt;
1042 spec_entry *entry;
1043
1044 elt.tmpl = most_general_template (tmpl);
1045 elt.args = CLASSTYPE_TI_ARGS (inst);
1046 elt.spec = inst;
1047
1048 type_specializations->remove_elt (&elt);
1049
1050 elt.tmpl = tmpl;
1051 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1052
1053 spec_entry **slot
1054 = type_specializations->find_slot (&elt, INSERT);
1055 entry = ggc_alloc<spec_entry> ();
1056 *entry = elt;
1057 *slot = entry;
1058 }
1059 else
1060 /* But if we've had an implicit instantiation, that's a
1061 problem ([temp.expl.spec]/6). */
1062 error ("specialization %qT after instantiation %qT",
1063 type, inst);
1064 }
1065
1066 /* Mark TYPE as a specialization. And as a result, we only
1067 have one level of template argument for the innermost
1068 class template. */
1069 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1070 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1071 CLASSTYPE_TI_ARGS (type)
1072 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1073 }
1074 }
1075 else if (processing_specialization)
1076 {
1077 /* Someday C++0x may allow for enum template specialization. */
1078 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1079 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1080 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1081 "of %qD not allowed by ISO C++", type);
1082 else
1083 {
1084 error ("explicit specialization of non-template %qT", type);
1085 return error_mark_node;
1086 }
1087 }
1088
1089 return type;
1090 }
1091
1092 /* Returns nonzero if we can optimize the retrieval of specializations
1093 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1094 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1095
1096 static inline bool
1097 optimize_specialization_lookup_p (tree tmpl)
1098 {
1099 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1100 && DECL_CLASS_SCOPE_P (tmpl)
1101 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1102 parameter. */
1103 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1104 /* The optimized lookup depends on the fact that the
1105 template arguments for the member function template apply
1106 purely to the containing class, which is not true if the
1107 containing class is an explicit or partial
1108 specialization. */
1109 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1110 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1111 && !DECL_CONV_FN_P (tmpl)
1112 /* It is possible to have a template that is not a member
1113 template and is not a member of a template class:
1114
1115 template <typename T>
1116 struct S { friend A::f(); };
1117
1118 Here, the friend function is a template, but the context does
1119 not have template information. The optimized lookup relies
1120 on having ARGS be the template arguments for both the class
1121 and the function template. */
1122 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1123 }
1124
1125 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1126 gone through coerce_template_parms by now. */
1127
1128 static void
1129 verify_unstripped_args (tree args)
1130 {
1131 ++processing_template_decl;
1132 if (!any_dependent_template_arguments_p (args))
1133 {
1134 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1135 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1136 {
1137 tree arg = TREE_VEC_ELT (inner, i);
1138 if (TREE_CODE (arg) == TEMPLATE_DECL)
1139 /* OK */;
1140 else if (TYPE_P (arg))
1141 gcc_assert (strip_typedefs (arg, NULL) == arg);
1142 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1143 /* Allow typedefs on the type of a non-type argument, since a
1144 parameter can have them. */;
1145 else
1146 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1147 }
1148 }
1149 --processing_template_decl;
1150 }
1151
1152 /* Retrieve the specialization (in the sense of [temp.spec] - a
1153 specialization is either an instantiation or an explicit
1154 specialization) of TMPL for the given template ARGS. If there is
1155 no such specialization, return NULL_TREE. The ARGS are a vector of
1156 arguments, or a vector of vectors of arguments, in the case of
1157 templates with more than one level of parameters.
1158
1159 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1160 then we search for a partial specialization matching ARGS. This
1161 parameter is ignored if TMPL is not a class template.
1162
1163 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1164 result is a NONTYPE_ARGUMENT_PACK. */
1165
1166 static tree
1167 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1168 {
1169 if (tmpl == NULL_TREE)
1170 return NULL_TREE;
1171
1172 if (args == error_mark_node)
1173 return NULL_TREE;
1174
1175 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1176 || TREE_CODE (tmpl) == FIELD_DECL);
1177
1178 /* There should be as many levels of arguments as there are
1179 levels of parameters. */
1180 gcc_assert (TMPL_ARGS_DEPTH (args)
1181 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1183 : template_class_depth (DECL_CONTEXT (tmpl))));
1184
1185 if (flag_checking)
1186 verify_unstripped_args (args);
1187
1188 if (optimize_specialization_lookup_p (tmpl))
1189 {
1190 tree class_template;
1191 tree class_specialization;
1192 vec<tree, va_gc> *methods;
1193 tree fns;
1194 int idx;
1195
1196 /* The template arguments actually apply to the containing
1197 class. Find the class specialization with those
1198 arguments. */
1199 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1200 class_specialization
1201 = retrieve_specialization (class_template, args, 0);
1202 if (!class_specialization)
1203 return NULL_TREE;
1204 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1205 for the specialization. */
1206 idx = class_method_index_for_fn (class_specialization, tmpl);
1207 if (idx == -1)
1208 return NULL_TREE;
1209 /* Iterate through the methods with the indicated name, looking
1210 for the one that has an instance of TMPL. */
1211 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1212 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1213 {
1214 tree fn = OVL_CURRENT (fns);
1215 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn) == class_specialization)
1219 return fn;
1220 }
1221 return NULL_TREE;
1222 }
1223 else
1224 {
1225 spec_entry *found;
1226 spec_entry elt;
1227 hash_table<spec_hasher> *specializations;
1228
1229 elt.tmpl = tmpl;
1230 elt.args = args;
1231 elt.spec = NULL_TREE;
1232
1233 if (DECL_CLASS_TEMPLATE_P (tmpl))
1234 specializations = type_specializations;
1235 else
1236 specializations = decl_specializations;
1237
1238 if (hash == 0)
1239 hash = spec_hasher::hash (&elt);
1240 found = specializations->find_with_hash (&elt, hash);
1241 if (found)
1242 return found->spec;
1243 }
1244
1245 return NULL_TREE;
1246 }
1247
1248 /* Like retrieve_specialization, but for local declarations. */
1249
1250 tree
1251 retrieve_local_specialization (tree tmpl)
1252 {
1253 if (local_specializations == NULL)
1254 return NULL_TREE;
1255
1256 tree *slot = local_specializations->get (tmpl);
1257 return slot ? *slot : NULL_TREE;
1258 }
1259
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1261
1262 int
1263 is_specialization_of (tree decl, tree tmpl)
1264 {
1265 tree t;
1266
1267 if (TREE_CODE (decl) == FUNCTION_DECL)
1268 {
1269 for (t = decl;
1270 t != NULL_TREE;
1271 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1272 if (t == tmpl)
1273 return 1;
1274 }
1275 else
1276 {
1277 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1278
1279 for (t = TREE_TYPE (decl);
1280 t != NULL_TREE;
1281 t = CLASSTYPE_USE_TEMPLATE (t)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1283 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1284 return 1;
1285 }
1286
1287 return 0;
1288 }
1289
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1292
1293 bool
1294 is_specialization_of_friend (tree decl, tree friend_decl)
1295 {
1296 bool need_template = true;
1297 int template_depth;
1298
1299 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1300 || TREE_CODE (decl) == TYPE_DECL);
1301
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1304 if this. */
1305 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl)
1307 && !DECL_USE_TEMPLATE (friend_decl))
1308 {
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1311 need_template = false;
1312 }
1313 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl))
1315 need_template = false;
1316
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1319 return false;
1320
1321 if (is_specialization_of (decl, friend_decl))
1322 return true;
1323
1324 /* [temp.friend/6]
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1329
1330 For example, given a template friend declaration
1331
1332 template <class T> friend void A<T>::f();
1333
1334 the member function below is considered a friend
1335
1336 template <> struct A<int> {
1337 void f();
1338 };
1339
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1343
1344 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1345 if (template_depth
1346 && DECL_CLASS_SCOPE_P (decl)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1349 {
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1352
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1355
1356 and DECL's are
1357
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1360
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1365
1366 tree context = DECL_CONTEXT (decl);
1367 tree args = NULL_TREE;
1368 int current_depth = 0;
1369
1370 while (current_depth < template_depth)
1371 {
1372 if (CLASSTYPE_TEMPLATE_INFO (context))
1373 {
1374 if (current_depth == 0)
1375 args = TYPE_TI_ARGS (context);
1376 else
1377 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1378 current_depth++;
1379 }
1380 context = TYPE_CONTEXT (context);
1381 }
1382
1383 if (TREE_CODE (decl) == FUNCTION_DECL)
1384 {
1385 bool is_template;
1386 tree friend_type;
1387 tree decl_type;
1388 tree friend_args_type;
1389 tree decl_args_type;
1390
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1392 non-templates. */
1393 is_template = DECL_TEMPLATE_INFO (decl)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1395 if (need_template ^ is_template)
1396 return false;
1397 else if (is_template)
1398 {
1399 /* If both are templates, check template parameter list. */
1400 tree friend_parms
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1402 args, tf_none);
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1405 friend_parms))
1406 return false;
1407
1408 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1409 }
1410 else
1411 decl_type = TREE_TYPE (decl);
1412
1413 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1414 tf_none, NULL_TREE);
1415 if (friend_type == error_mark_node)
1416 return false;
1417
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1420 return false;
1421
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type = TYPE_ARG_TYPES (friend_type);
1425 decl_args_type = TYPE_ARG_TYPES (decl_type);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1427 friend_args_type = TREE_CHAIN (friend_args_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1429 decl_args_type = TREE_CHAIN (decl_args_type);
1430
1431 return compparms (decl_args_type, friend_args_type);
1432 }
1433 else
1434 {
1435 /* DECL is a TYPE_DECL */
1436 bool is_template;
1437 tree decl_type = TREE_TYPE (decl);
1438
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1444
1445 if (need_template ^ is_template)
1446 return false;
1447 else if (is_template)
1448 {
1449 tree friend_parms;
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1453 != DECL_NAME (friend_decl))
1454 return false;
1455
1456 /* Now check template parameter list. */
1457 friend_parms
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1459 args, tf_none);
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1462 friend_parms);
1463 }
1464 else
1465 return (DECL_NAME (decl)
1466 == DECL_NAME (friend_decl));
1467 }
1468 }
1469 return false;
1470 }
1471
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1476
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1479
1480 static tree
1481 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1482 hashval_t hash)
1483 {
1484 tree fn;
1485 spec_entry **slot = NULL;
1486 spec_entry elt;
1487
1488 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1489 || (TREE_CODE (tmpl) == FIELD_DECL
1490 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1491
1492 if (TREE_CODE (spec) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1500
1501 template <class T> void f(T t1, T t = T())
1502
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1505 return spec;
1506
1507 if (optimize_specialization_lookup_p (tmpl))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn = retrieve_specialization (tmpl, args, 0);
1511 else
1512 {
1513 elt.tmpl = tmpl;
1514 elt.args = args;
1515 elt.spec = spec;
1516
1517 if (hash == 0)
1518 hash = spec_hasher::hash (&elt);
1519
1520 slot =
1521 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1522 if (*slot)
1523 fn = ((spec_entry *) *slot)->spec;
1524 else
1525 fn = NULL_TREE;
1526 }
1527
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1533 if (fn == spec)
1534 return spec;
1535 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1536 {
1537 if (DECL_TEMPLATE_INSTANTIATION (fn))
1538 {
1539 if (DECL_ODR_USED (fn)
1540 || DECL_EXPLICIT_INSTANTIATION (fn))
1541 {
1542 error ("specialization of %qD after instantiation",
1543 fn);
1544 return error_mark_node;
1545 }
1546 else
1547 {
1548 tree clone;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1555 it later.
1556
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1559 specialization:
1560
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1564
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1567 declaration.
1568
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn) = NULL_TREE;
1573 duplicate_decls (spec, fn, is_friend);
1574 /* The call to duplicate_decls will have applied
1575 [temp.expl.spec]:
1576
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1580 is.
1581
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone, fn)
1585 {
1586 DECL_DECLARED_INLINE_P (clone)
1587 = DECL_DECLARED_INLINE_P (fn);
1588 DECL_SOURCE_LOCATION (clone)
1589 = DECL_SOURCE_LOCATION (fn);
1590 DECL_DELETED_FN (clone)
1591 = DECL_DELETED_FN (fn);
1592 }
1593 check_specialization_namespace (tmpl);
1594
1595 return fn;
1596 }
1597 }
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1599 {
1600 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1601 /* Dup decl failed, but this is a new definition. Set the
1602 line number so any errors match this new
1603 definition. */
1604 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1605
1606 return fn;
1607 }
1608 }
1609 else if (fn)
1610 return duplicate_decls (spec, fn, is_friend);
1611
1612 /* A specialization must be declared in the same namespace as the
1613 template it is specializing. */
1614 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1615 && !check_specialization_namespace (tmpl))
1616 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1617
1618 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1619 {
1620 spec_entry *entry = ggc_alloc<spec_entry> ();
1621 gcc_assert (tmpl && args && spec);
1622 *entry = elt;
1623 *slot = entry;
1624 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1625 && PRIMARY_TEMPLATE_P (tmpl)
1626 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1627 || variable_template_p (tmpl))
1628 /* If TMPL is a forward declaration of a template function, keep a list
1629 of all specializations in case we need to reassign them to a friend
1630 template later in tsubst_friend_function.
1631
1632 Also keep a list of all variable template instantiations so that
1633 process_partial_specialization can check whether a later partial
1634 specialization would have used it. */
1635 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1636 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1637 }
1638
1639 return spec;
1640 }
1641
1642 /* Returns true iff two spec_entry nodes are equivalent. */
1643
1644 int comparing_specializations;
1645
1646 bool
1647 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1648 {
1649 int equal;
1650
1651 ++comparing_specializations;
1652 equal = (e1->tmpl == e2->tmpl
1653 && comp_template_args (e1->args, e2->args));
1654 if (equal && flag_concepts
1655 /* tmpl could be a FIELD_DECL for a capture pack. */
1656 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1657 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1658 && uses_template_parms (e1->args))
1659 {
1660 /* Partial specializations of a variable template can be distinguished by
1661 constraints. */
1662 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1663 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1664 equal = equivalent_constraints (c1, c2);
1665 }
1666 --comparing_specializations;
1667
1668 return equal;
1669 }
1670
1671 /* Returns a hash for a template TMPL and template arguments ARGS. */
1672
1673 static hashval_t
1674 hash_tmpl_and_args (tree tmpl, tree args)
1675 {
1676 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1677 return iterative_hash_template_arg (args, val);
1678 }
1679
1680 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1681 ignoring SPEC. */
1682
1683 hashval_t
1684 spec_hasher::hash (spec_entry *e)
1685 {
1686 return hash_tmpl_and_args (e->tmpl, e->args);
1687 }
1688
1689 /* Recursively calculate a hash value for a template argument ARG, for use
1690 in the hash tables of template specializations. */
1691
1692 hashval_t
1693 iterative_hash_template_arg (tree arg, hashval_t val)
1694 {
1695 unsigned HOST_WIDE_INT i;
1696 enum tree_code code;
1697 char tclass;
1698
1699 if (arg == NULL_TREE)
1700 return iterative_hash_object (arg, val);
1701
1702 if (!TYPE_P (arg))
1703 STRIP_NOPS (arg);
1704
1705 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1706 gcc_unreachable ();
1707
1708 code = TREE_CODE (arg);
1709 tclass = TREE_CODE_CLASS (code);
1710
1711 val = iterative_hash_object (code, val);
1712
1713 switch (code)
1714 {
1715 case ERROR_MARK:
1716 return val;
1717
1718 case IDENTIFIER_NODE:
1719 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1720
1721 case TREE_VEC:
1722 {
1723 int i, len = TREE_VEC_LENGTH (arg);
1724 for (i = 0; i < len; ++i)
1725 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1726 return val;
1727 }
1728
1729 case TYPE_PACK_EXPANSION:
1730 case EXPR_PACK_EXPANSION:
1731 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1732 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1733
1734 case TYPE_ARGUMENT_PACK:
1735 case NONTYPE_ARGUMENT_PACK:
1736 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1737
1738 case TREE_LIST:
1739 for (; arg; arg = TREE_CHAIN (arg))
1740 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1741 return val;
1742
1743 case OVERLOAD:
1744 for (; arg; arg = OVL_NEXT (arg))
1745 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1746 return val;
1747
1748 case CONSTRUCTOR:
1749 {
1750 tree field, value;
1751 iterative_hash_template_arg (TREE_TYPE (arg), val);
1752 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1753 {
1754 val = iterative_hash_template_arg (field, val);
1755 val = iterative_hash_template_arg (value, val);
1756 }
1757 return val;
1758 }
1759
1760 case PARM_DECL:
1761 if (!DECL_ARTIFICIAL (arg))
1762 {
1763 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1764 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1765 }
1766 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1767
1768 case TARGET_EXPR:
1769 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1770
1771 case PTRMEM_CST:
1772 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1773 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1774
1775 case TEMPLATE_PARM_INDEX:
1776 val = iterative_hash_template_arg
1777 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1778 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1779 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1780
1781 case TRAIT_EXPR:
1782 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1783 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1784 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1785
1786 case BASELINK:
1787 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1788 val);
1789 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1790 val);
1791
1792 case MODOP_EXPR:
1793 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1794 code = TREE_CODE (TREE_OPERAND (arg, 1));
1795 val = iterative_hash_object (code, val);
1796 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1797
1798 case LAMBDA_EXPR:
1799 /* A lambda can't appear in a template arg, but don't crash on
1800 erroneous input. */
1801 gcc_assert (seen_error ());
1802 return val;
1803
1804 case CAST_EXPR:
1805 case IMPLICIT_CONV_EXPR:
1806 case STATIC_CAST_EXPR:
1807 case REINTERPRET_CAST_EXPR:
1808 case CONST_CAST_EXPR:
1809 case DYNAMIC_CAST_EXPR:
1810 case NEW_EXPR:
1811 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1812 /* Now hash operands as usual. */
1813 break;
1814
1815 default:
1816 break;
1817 }
1818
1819 switch (tclass)
1820 {
1821 case tcc_type:
1822 if (alias_template_specialization_p (arg))
1823 {
1824 // We want an alias specialization that survived strip_typedefs
1825 // to hash differently from its TYPE_CANONICAL, to avoid hash
1826 // collisions that compare as different in template_args_equal.
1827 // These could be dependent specializations that strip_typedefs
1828 // left alone, or untouched specializations because
1829 // coerce_template_parms returns the unconverted template
1830 // arguments if it sees incomplete argument packs.
1831 tree ti = TYPE_TEMPLATE_INFO (arg);
1832 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1833 }
1834 if (TYPE_CANONICAL (arg))
1835 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1836 val);
1837 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1838 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1839 /* Otherwise just compare the types during lookup. */
1840 return val;
1841
1842 case tcc_declaration:
1843 case tcc_constant:
1844 return iterative_hash_expr (arg, val);
1845
1846 default:
1847 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1848 {
1849 unsigned n = cp_tree_operand_length (arg);
1850 for (i = 0; i < n; ++i)
1851 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1852 return val;
1853 }
1854 }
1855 gcc_unreachable ();
1856 return 0;
1857 }
1858
1859 /* Unregister the specialization SPEC as a specialization of TMPL.
1860 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1861 if the SPEC was listed as a specialization of TMPL.
1862
1863 Note that SPEC has been ggc_freed, so we can't look inside it. */
1864
1865 bool
1866 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1867 {
1868 spec_entry *entry;
1869 spec_entry elt;
1870
1871 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1872 elt.args = TI_ARGS (tinfo);
1873 elt.spec = NULL_TREE;
1874
1875 entry = decl_specializations->find (&elt);
1876 if (entry != NULL)
1877 {
1878 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1879 gcc_assert (new_spec != NULL_TREE);
1880 entry->spec = new_spec;
1881 return 1;
1882 }
1883
1884 return 0;
1885 }
1886
1887 /* Like register_specialization, but for local declarations. We are
1888 registering SPEC, an instantiation of TMPL. */
1889
1890 void
1891 register_local_specialization (tree spec, tree tmpl)
1892 {
1893 local_specializations->put (tmpl, spec);
1894 }
1895
1896 /* TYPE is a class type. Returns true if TYPE is an explicitly
1897 specialized class. */
1898
1899 bool
1900 explicit_class_specialization_p (tree type)
1901 {
1902 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1903 return false;
1904 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1905 }
1906
1907 /* Print the list of functions at FNS, going through all the overloads
1908 for each element of the list. Alternatively, FNS can not be a
1909 TREE_LIST, in which case it will be printed together with all the
1910 overloads.
1911
1912 MORE and *STR should respectively be FALSE and NULL when the function
1913 is called from the outside. They are used internally on recursive
1914 calls. print_candidates manages the two parameters and leaves NULL
1915 in *STR when it ends. */
1916
1917 static void
1918 print_candidates_1 (tree fns, bool more, const char **str)
1919 {
1920 tree fn, fn2;
1921 char *spaces = NULL;
1922
1923 for (fn = fns; fn; fn = OVL_NEXT (fn))
1924 if (TREE_CODE (fn) == TREE_LIST)
1925 {
1926 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1927 print_candidates_1 (TREE_VALUE (fn2),
1928 TREE_CHAIN (fn2) || more, str);
1929 }
1930 else
1931 {
1932 tree cand = OVL_CURRENT (fn);
1933 if (!*str)
1934 {
1935 /* Pick the prefix string. */
1936 if (!more && !OVL_NEXT (fns))
1937 {
1938 inform (DECL_SOURCE_LOCATION (cand),
1939 "candidate is: %#D", cand);
1940 continue;
1941 }
1942
1943 *str = _("candidates are:");
1944 spaces = get_spaces (*str);
1945 }
1946 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1947 *str = spaces ? spaces : *str;
1948 }
1949
1950 if (!more)
1951 {
1952 free (spaces);
1953 *str = NULL;
1954 }
1955 }
1956
1957 /* Print the list of candidate FNS in an error message. FNS can also
1958 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1959
1960 void
1961 print_candidates (tree fns)
1962 {
1963 const char *str = NULL;
1964 print_candidates_1 (fns, false, &str);
1965 gcc_assert (str == NULL);
1966 }
1967
1968 /* Get a (possibly) constrained template declaration for the
1969 purpose of ordering candidates. */
1970 static tree
1971 get_template_for_ordering (tree list)
1972 {
1973 gcc_assert (TREE_CODE (list) == TREE_LIST);
1974 tree f = TREE_VALUE (list);
1975 if (tree ti = DECL_TEMPLATE_INFO (f))
1976 return TI_TEMPLATE (ti);
1977 return f;
1978 }
1979
1980 /* Among candidates having the same signature, return the
1981 most constrained or NULL_TREE if there is no best candidate.
1982 If the signatures of candidates vary (e.g., template
1983 specialization vs. member function), then there can be no
1984 most constrained.
1985
1986 Note that we don't compare constraints on the functions
1987 themselves, but rather those of their templates. */
1988 static tree
1989 most_constrained_function (tree candidates)
1990 {
1991 // Try to find the best candidate in a first pass.
1992 tree champ = candidates;
1993 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1994 {
1995 int winner = more_constrained (get_template_for_ordering (champ),
1996 get_template_for_ordering (c));
1997 if (winner == -1)
1998 champ = c; // The candidate is more constrained
1999 else if (winner == 0)
2000 return NULL_TREE; // Neither is more constrained
2001 }
2002
2003 // Verify that the champ is better than previous candidates.
2004 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2005 if (!more_constrained (get_template_for_ordering (champ),
2006 get_template_for_ordering (c)))
2007 return NULL_TREE;
2008 }
2009
2010 return champ;
2011 }
2012
2013
2014 /* Returns the template (one of the functions given by TEMPLATE_ID)
2015 which can be specialized to match the indicated DECL with the
2016 explicit template args given in TEMPLATE_ID. The DECL may be
2017 NULL_TREE if none is available. In that case, the functions in
2018 TEMPLATE_ID are non-members.
2019
2020 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2021 specialization of a member template.
2022
2023 The TEMPLATE_COUNT is the number of references to qualifying
2024 template classes that appeared in the name of the function. See
2025 check_explicit_specialization for a more accurate description.
2026
2027 TSK indicates what kind of template declaration (if any) is being
2028 declared. TSK_TEMPLATE indicates that the declaration given by
2029 DECL, though a FUNCTION_DECL, has template parameters, and is
2030 therefore a template function.
2031
2032 The template args (those explicitly specified and those deduced)
2033 are output in a newly created vector *TARGS_OUT.
2034
2035 If it is impossible to determine the result, an error message is
2036 issued. The error_mark_node is returned to indicate failure. */
2037
2038 static tree
2039 determine_specialization (tree template_id,
2040 tree decl,
2041 tree* targs_out,
2042 int need_member_template,
2043 int template_count,
2044 tmpl_spec_kind tsk)
2045 {
2046 tree fns;
2047 tree targs;
2048 tree explicit_targs;
2049 tree candidates = NULL_TREE;
2050
2051 /* A TREE_LIST of templates of which DECL may be a specialization.
2052 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2053 corresponding TREE_PURPOSE is the set of template arguments that,
2054 when used to instantiate the template, would produce a function
2055 with the signature of DECL. */
2056 tree templates = NULL_TREE;
2057 int header_count;
2058 cp_binding_level *b;
2059
2060 *targs_out = NULL_TREE;
2061
2062 if (template_id == error_mark_node || decl == error_mark_node)
2063 return error_mark_node;
2064
2065 /* We shouldn't be specializing a member template of an
2066 unspecialized class template; we already gave an error in
2067 check_specialization_scope, now avoid crashing. */
2068 if (template_count && DECL_CLASS_SCOPE_P (decl)
2069 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2070 {
2071 gcc_assert (errorcount);
2072 return error_mark_node;
2073 }
2074
2075 fns = TREE_OPERAND (template_id, 0);
2076 explicit_targs = TREE_OPERAND (template_id, 1);
2077
2078 if (fns == error_mark_node)
2079 return error_mark_node;
2080
2081 /* Check for baselinks. */
2082 if (BASELINK_P (fns))
2083 fns = BASELINK_FUNCTIONS (fns);
2084
2085 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2086 {
2087 error ("%qD is not a function template", fns);
2088 return error_mark_node;
2089 }
2090 else if (VAR_P (decl) && !variable_template_p (fns))
2091 {
2092 error ("%qD is not a variable template", fns);
2093 return error_mark_node;
2094 }
2095
2096 /* Count the number of template headers specified for this
2097 specialization. */
2098 header_count = 0;
2099 for (b = current_binding_level;
2100 b->kind == sk_template_parms;
2101 b = b->level_chain)
2102 ++header_count;
2103
2104 tree orig_fns = fns;
2105
2106 if (variable_template_p (fns))
2107 {
2108 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2109 targs = coerce_template_parms (parms, explicit_targs, fns,
2110 tf_warning_or_error,
2111 /*req_all*/true, /*use_defarg*/true);
2112 if (targs != error_mark_node)
2113 templates = tree_cons (targs, fns, templates);
2114 }
2115 else for (; fns; fns = OVL_NEXT (fns))
2116 {
2117 tree fn = OVL_CURRENT (fns);
2118
2119 if (TREE_CODE (fn) == TEMPLATE_DECL)
2120 {
2121 tree decl_arg_types;
2122 tree fn_arg_types;
2123 tree insttype;
2124
2125 /* In case of explicit specialization, we need to check if
2126 the number of template headers appearing in the specialization
2127 is correct. This is usually done in check_explicit_specialization,
2128 but the check done there cannot be exhaustive when specializing
2129 member functions. Consider the following code:
2130
2131 template <> void A<int>::f(int);
2132 template <> template <> void A<int>::f(int);
2133
2134 Assuming that A<int> is not itself an explicit specialization
2135 already, the first line specializes "f" which is a non-template
2136 member function, whilst the second line specializes "f" which
2137 is a template member function. So both lines are syntactically
2138 correct, and check_explicit_specialization does not reject
2139 them.
2140
2141 Here, we can do better, as we are matching the specialization
2142 against the declarations. We count the number of template
2143 headers, and we check if they match TEMPLATE_COUNT + 1
2144 (TEMPLATE_COUNT is the number of qualifying template classes,
2145 plus there must be another header for the member template
2146 itself).
2147
2148 Notice that if header_count is zero, this is not a
2149 specialization but rather a template instantiation, so there
2150 is no check we can perform here. */
2151 if (header_count && header_count != template_count + 1)
2152 continue;
2153
2154 /* Check that the number of template arguments at the
2155 innermost level for DECL is the same as for FN. */
2156 if (current_binding_level->kind == sk_template_parms
2157 && !current_binding_level->explicit_spec_p
2158 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2159 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2160 (current_template_parms))))
2161 continue;
2162
2163 /* DECL might be a specialization of FN. */
2164 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2165 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2166
2167 /* For a non-static member function, we need to make sure
2168 that the const qualification is the same. Since
2169 get_bindings does not try to merge the "this" parameter,
2170 we must do the comparison explicitly. */
2171 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2172 && !same_type_p (TREE_VALUE (fn_arg_types),
2173 TREE_VALUE (decl_arg_types)))
2174 continue;
2175
2176 /* Skip the "this" parameter and, for constructors of
2177 classes with virtual bases, the VTT parameter. A
2178 full specialization of a constructor will have a VTT
2179 parameter, but a template never will. */
2180 decl_arg_types
2181 = skip_artificial_parms_for (decl, decl_arg_types);
2182 fn_arg_types
2183 = skip_artificial_parms_for (fn, fn_arg_types);
2184
2185 /* Function templates cannot be specializations; there are
2186 no partial specializations of functions. Therefore, if
2187 the type of DECL does not match FN, there is no
2188 match.
2189
2190 Note that it should never be the case that we have both
2191 candidates added here, and for regular member functions
2192 below. */
2193 if (tsk == tsk_template)
2194 {
2195 if (compparms (fn_arg_types, decl_arg_types))
2196 candidates = tree_cons (NULL_TREE, fn, candidates);
2197 continue;
2198 }
2199
2200 /* See whether this function might be a specialization of this
2201 template. Suppress access control because we might be trying
2202 to make this specialization a friend, and we have already done
2203 access control for the declaration of the specialization. */
2204 push_deferring_access_checks (dk_no_check);
2205 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2206 pop_deferring_access_checks ();
2207
2208 if (!targs)
2209 /* We cannot deduce template arguments that when used to
2210 specialize TMPL will produce DECL. */
2211 continue;
2212
2213 /* Remove, from the set of candidates, all those functions
2214 whose constraints are not satisfied. */
2215 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2216 continue;
2217
2218 // Then, try to form the new function type.
2219 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2220 if (insttype == error_mark_node)
2221 continue;
2222 fn_arg_types
2223 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2224 if (!compparms (fn_arg_types, decl_arg_types))
2225 continue;
2226
2227 /* Save this template, and the arguments deduced. */
2228 templates = tree_cons (targs, fn, templates);
2229 }
2230 else if (need_member_template)
2231 /* FN is an ordinary member function, and we need a
2232 specialization of a member template. */
2233 ;
2234 else if (TREE_CODE (fn) != FUNCTION_DECL)
2235 /* We can get IDENTIFIER_NODEs here in certain erroneous
2236 cases. */
2237 ;
2238 else if (!DECL_FUNCTION_MEMBER_P (fn))
2239 /* This is just an ordinary non-member function. Nothing can
2240 be a specialization of that. */
2241 ;
2242 else if (DECL_ARTIFICIAL (fn))
2243 /* Cannot specialize functions that are created implicitly. */
2244 ;
2245 else
2246 {
2247 tree decl_arg_types;
2248
2249 /* This is an ordinary member function. However, since
2250 we're here, we can assume its enclosing class is a
2251 template class. For example,
2252
2253 template <typename T> struct S { void f(); };
2254 template <> void S<int>::f() {}
2255
2256 Here, S<int>::f is a non-template, but S<int> is a
2257 template class. If FN has the same type as DECL, we
2258 might be in business. */
2259
2260 if (!DECL_TEMPLATE_INFO (fn))
2261 /* Its enclosing class is an explicit specialization
2262 of a template class. This is not a candidate. */
2263 continue;
2264
2265 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2266 TREE_TYPE (TREE_TYPE (fn))))
2267 /* The return types differ. */
2268 continue;
2269
2270 /* Adjust the type of DECL in case FN is a static member. */
2271 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2272 if (DECL_STATIC_FUNCTION_P (fn)
2273 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2274 decl_arg_types = TREE_CHAIN (decl_arg_types);
2275
2276 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2277 decl_arg_types))
2278 continue;
2279
2280 // If the deduced arguments do not satisfy the constraints,
2281 // this is not a candidate.
2282 if (flag_concepts && !constraints_satisfied_p (fn))
2283 continue;
2284
2285 // Add the candidate.
2286 candidates = tree_cons (NULL_TREE, fn, candidates);
2287 }
2288 }
2289
2290 if (templates && TREE_CHAIN (templates))
2291 {
2292 /* We have:
2293
2294 [temp.expl.spec]
2295
2296 It is possible for a specialization with a given function
2297 signature to be instantiated from more than one function
2298 template. In such cases, explicit specification of the
2299 template arguments must be used to uniquely identify the
2300 function template specialization being specialized.
2301
2302 Note that here, there's no suggestion that we're supposed to
2303 determine which of the candidate templates is most
2304 specialized. However, we, also have:
2305
2306 [temp.func.order]
2307
2308 Partial ordering of overloaded function template
2309 declarations is used in the following contexts to select
2310 the function template to which a function template
2311 specialization refers:
2312
2313 -- when an explicit specialization refers to a function
2314 template.
2315
2316 So, we do use the partial ordering rules, at least for now.
2317 This extension can only serve to make invalid programs valid,
2318 so it's safe. And, there is strong anecdotal evidence that
2319 the committee intended the partial ordering rules to apply;
2320 the EDG front end has that behavior, and John Spicer claims
2321 that the committee simply forgot to delete the wording in
2322 [temp.expl.spec]. */
2323 tree tmpl = most_specialized_instantiation (templates);
2324 if (tmpl != error_mark_node)
2325 {
2326 templates = tmpl;
2327 TREE_CHAIN (templates) = NULL_TREE;
2328 }
2329 }
2330
2331 // Concepts allows multiple declarations of member functions
2332 // with the same signature. Like above, we need to rely on
2333 // on the partial ordering of those candidates to determine which
2334 // is the best.
2335 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2336 {
2337 if (tree cand = most_constrained_function (candidates))
2338 {
2339 candidates = cand;
2340 TREE_CHAIN (cand) = NULL_TREE;
2341 }
2342 }
2343
2344 if (templates == NULL_TREE && candidates == NULL_TREE)
2345 {
2346 error ("template-id %qD for %q+D does not match any template "
2347 "declaration", template_id, decl);
2348 if (header_count && header_count != template_count + 1)
2349 inform (input_location, "saw %d %<template<>%>, need %d for "
2350 "specializing a member function template",
2351 header_count, template_count + 1);
2352 else
2353 print_candidates (orig_fns);
2354 return error_mark_node;
2355 }
2356 else if ((templates && TREE_CHAIN (templates))
2357 || (candidates && TREE_CHAIN (candidates))
2358 || (templates && candidates))
2359 {
2360 error ("ambiguous template specialization %qD for %q+D",
2361 template_id, decl);
2362 candidates = chainon (candidates, templates);
2363 print_candidates (candidates);
2364 return error_mark_node;
2365 }
2366
2367 /* We have one, and exactly one, match. */
2368 if (candidates)
2369 {
2370 tree fn = TREE_VALUE (candidates);
2371 *targs_out = copy_node (DECL_TI_ARGS (fn));
2372
2373 // Propagate the candidate's constraints to the declaration.
2374 set_constraints (decl, get_constraints (fn));
2375
2376 /* DECL is a re-declaration or partial instantiation of a template
2377 function. */
2378 if (TREE_CODE (fn) == TEMPLATE_DECL)
2379 return fn;
2380 /* It was a specialization of an ordinary member function in a
2381 template class. */
2382 return DECL_TI_TEMPLATE (fn);
2383 }
2384
2385 /* It was a specialization of a template. */
2386 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2387 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2388 {
2389 *targs_out = copy_node (targs);
2390 SET_TMPL_ARGS_LEVEL (*targs_out,
2391 TMPL_ARGS_DEPTH (*targs_out),
2392 TREE_PURPOSE (templates));
2393 }
2394 else
2395 *targs_out = TREE_PURPOSE (templates);
2396 return TREE_VALUE (templates);
2397 }
2398
2399 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2400 but with the default argument values filled in from those in the
2401 TMPL_TYPES. */
2402
2403 static tree
2404 copy_default_args_to_explicit_spec_1 (tree spec_types,
2405 tree tmpl_types)
2406 {
2407 tree new_spec_types;
2408
2409 if (!spec_types)
2410 return NULL_TREE;
2411
2412 if (spec_types == void_list_node)
2413 return void_list_node;
2414
2415 /* Substitute into the rest of the list. */
2416 new_spec_types =
2417 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2418 TREE_CHAIN (tmpl_types));
2419
2420 /* Add the default argument for this parameter. */
2421 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2422 TREE_VALUE (spec_types),
2423 new_spec_types);
2424 }
2425
2426 /* DECL is an explicit specialization. Replicate default arguments
2427 from the template it specializes. (That way, code like:
2428
2429 template <class T> void f(T = 3);
2430 template <> void f(double);
2431 void g () { f (); }
2432
2433 works, as required.) An alternative approach would be to look up
2434 the correct default arguments at the call-site, but this approach
2435 is consistent with how implicit instantiations are handled. */
2436
2437 static void
2438 copy_default_args_to_explicit_spec (tree decl)
2439 {
2440 tree tmpl;
2441 tree spec_types;
2442 tree tmpl_types;
2443 tree new_spec_types;
2444 tree old_type;
2445 tree new_type;
2446 tree t;
2447 tree object_type = NULL_TREE;
2448 tree in_charge = NULL_TREE;
2449 tree vtt = NULL_TREE;
2450
2451 /* See if there's anything we need to do. */
2452 tmpl = DECL_TI_TEMPLATE (decl);
2453 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2454 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2455 if (TREE_PURPOSE (t))
2456 break;
2457 if (!t)
2458 return;
2459
2460 old_type = TREE_TYPE (decl);
2461 spec_types = TYPE_ARG_TYPES (old_type);
2462
2463 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2464 {
2465 /* Remove the this pointer, but remember the object's type for
2466 CV quals. */
2467 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2468 spec_types = TREE_CHAIN (spec_types);
2469 tmpl_types = TREE_CHAIN (tmpl_types);
2470
2471 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2472 {
2473 /* DECL may contain more parameters than TMPL due to the extra
2474 in-charge parameter in constructors and destructors. */
2475 in_charge = spec_types;
2476 spec_types = TREE_CHAIN (spec_types);
2477 }
2478 if (DECL_HAS_VTT_PARM_P (decl))
2479 {
2480 vtt = spec_types;
2481 spec_types = TREE_CHAIN (spec_types);
2482 }
2483 }
2484
2485 /* Compute the merged default arguments. */
2486 new_spec_types =
2487 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2488
2489 /* Compute the new FUNCTION_TYPE. */
2490 if (object_type)
2491 {
2492 if (vtt)
2493 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2494 TREE_VALUE (vtt),
2495 new_spec_types);
2496
2497 if (in_charge)
2498 /* Put the in-charge parameter back. */
2499 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2500 TREE_VALUE (in_charge),
2501 new_spec_types);
2502
2503 new_type = build_method_type_directly (object_type,
2504 TREE_TYPE (old_type),
2505 new_spec_types);
2506 }
2507 else
2508 new_type = build_function_type (TREE_TYPE (old_type),
2509 new_spec_types);
2510 new_type = cp_build_type_attribute_variant (new_type,
2511 TYPE_ATTRIBUTES (old_type));
2512 new_type = build_exception_variant (new_type,
2513 TYPE_RAISES_EXCEPTIONS (old_type));
2514
2515 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2516 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2517
2518 TREE_TYPE (decl) = new_type;
2519 }
2520
2521 /* Return the number of template headers we expect to see for a definition
2522 or specialization of CTYPE or one of its non-template members. */
2523
2524 int
2525 num_template_headers_for_class (tree ctype)
2526 {
2527 int num_templates = 0;
2528
2529 while (ctype && CLASS_TYPE_P (ctype))
2530 {
2531 /* You're supposed to have one `template <...>' for every
2532 template class, but you don't need one for a full
2533 specialization. For example:
2534
2535 template <class T> struct S{};
2536 template <> struct S<int> { void f(); };
2537 void S<int>::f () {}
2538
2539 is correct; there shouldn't be a `template <>' for the
2540 definition of `S<int>::f'. */
2541 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2542 /* If CTYPE does not have template information of any
2543 kind, then it is not a template, nor is it nested
2544 within a template. */
2545 break;
2546 if (explicit_class_specialization_p (ctype))
2547 break;
2548 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2549 ++num_templates;
2550
2551 ctype = TYPE_CONTEXT (ctype);
2552 }
2553
2554 return num_templates;
2555 }
2556
2557 /* Do a simple sanity check on the template headers that precede the
2558 variable declaration DECL. */
2559
2560 void
2561 check_template_variable (tree decl)
2562 {
2563 tree ctx = CP_DECL_CONTEXT (decl);
2564 int wanted = num_template_headers_for_class (ctx);
2565 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2566 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2567 {
2568 if (cxx_dialect < cxx14)
2569 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2570 "variable templates only available with "
2571 "-std=c++14 or -std=gnu++14");
2572
2573 // Namespace-scope variable templates should have a template header.
2574 ++wanted;
2575 }
2576 if (template_header_count > wanted)
2577 {
2578 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2579 "too many template headers for %D (should be %d)",
2580 decl, wanted);
2581 if (warned && CLASS_TYPE_P (ctx)
2582 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2583 inform (DECL_SOURCE_LOCATION (decl),
2584 "members of an explicitly specialized class are defined "
2585 "without a template header");
2586 }
2587 }
2588
2589 /* Check to see if the function just declared, as indicated in
2590 DECLARATOR, and in DECL, is a specialization of a function
2591 template. We may also discover that the declaration is an explicit
2592 instantiation at this point.
2593
2594 Returns DECL, or an equivalent declaration that should be used
2595 instead if all goes well. Issues an error message if something is
2596 amiss. Returns error_mark_node if the error is not easily
2597 recoverable.
2598
2599 FLAGS is a bitmask consisting of the following flags:
2600
2601 2: The function has a definition.
2602 4: The function is a friend.
2603
2604 The TEMPLATE_COUNT is the number of references to qualifying
2605 template classes that appeared in the name of the function. For
2606 example, in
2607
2608 template <class T> struct S { void f(); };
2609 void S<int>::f();
2610
2611 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2612 classes are not counted in the TEMPLATE_COUNT, so that in
2613
2614 template <class T> struct S {};
2615 template <> struct S<int> { void f(); }
2616 template <> void S<int>::f();
2617
2618 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2619 invalid; there should be no template <>.)
2620
2621 If the function is a specialization, it is marked as such via
2622 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2623 is set up correctly, and it is added to the list of specializations
2624 for that template. */
2625
2626 tree
2627 check_explicit_specialization (tree declarator,
2628 tree decl,
2629 int template_count,
2630 int flags)
2631 {
2632 int have_def = flags & 2;
2633 int is_friend = flags & 4;
2634 bool is_concept = flags & 8;
2635 int specialization = 0;
2636 int explicit_instantiation = 0;
2637 int member_specialization = 0;
2638 tree ctype = DECL_CLASS_CONTEXT (decl);
2639 tree dname = DECL_NAME (decl);
2640 tmpl_spec_kind tsk;
2641
2642 if (is_friend)
2643 {
2644 if (!processing_specialization)
2645 tsk = tsk_none;
2646 else
2647 tsk = tsk_excessive_parms;
2648 }
2649 else
2650 tsk = current_tmpl_spec_kind (template_count);
2651
2652 switch (tsk)
2653 {
2654 case tsk_none:
2655 if (processing_specialization && !VAR_P (decl))
2656 {
2657 specialization = 1;
2658 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2659 }
2660 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2661 {
2662 if (is_friend)
2663 /* This could be something like:
2664
2665 template <class T> void f(T);
2666 class S { friend void f<>(int); } */
2667 specialization = 1;
2668 else
2669 {
2670 /* This case handles bogus declarations like template <>
2671 template <class T> void f<int>(); */
2672
2673 error ("template-id %qD in declaration of primary template",
2674 declarator);
2675 return decl;
2676 }
2677 }
2678 break;
2679
2680 case tsk_invalid_member_spec:
2681 /* The error has already been reported in
2682 check_specialization_scope. */
2683 return error_mark_node;
2684
2685 case tsk_invalid_expl_inst:
2686 error ("template parameter list used in explicit instantiation");
2687
2688 /* Fall through. */
2689
2690 case tsk_expl_inst:
2691 if (have_def)
2692 error ("definition provided for explicit instantiation");
2693
2694 explicit_instantiation = 1;
2695 break;
2696
2697 case tsk_excessive_parms:
2698 case tsk_insufficient_parms:
2699 if (tsk == tsk_excessive_parms)
2700 error ("too many template parameter lists in declaration of %qD",
2701 decl);
2702 else if (template_header_count)
2703 error("too few template parameter lists in declaration of %qD", decl);
2704 else
2705 error("explicit specialization of %qD must be introduced by "
2706 "%<template <>%>", decl);
2707
2708 /* Fall through. */
2709 case tsk_expl_spec:
2710 if (is_concept)
2711 error ("explicit specialization declared %<concept%>");
2712
2713 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2714 /* In cases like template<> constexpr bool v = true;
2715 We'll give an error in check_template_variable. */
2716 break;
2717
2718 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2719 if (ctype)
2720 member_specialization = 1;
2721 else
2722 specialization = 1;
2723 break;
2724
2725 case tsk_template:
2726 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2727 {
2728 /* This case handles bogus declarations like template <>
2729 template <class T> void f<int>(); */
2730
2731 if (!uses_template_parms (declarator))
2732 error ("template-id %qD in declaration of primary template",
2733 declarator);
2734 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2735 {
2736 /* Partial specialization of variable template. */
2737 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2738 specialization = 1;
2739 goto ok;
2740 }
2741 else if (cxx_dialect < cxx14)
2742 error ("non-type partial specialization %qD "
2743 "is not allowed", declarator);
2744 else
2745 error ("non-class, non-variable partial specialization %qD "
2746 "is not allowed", declarator);
2747 return decl;
2748 ok:;
2749 }
2750
2751 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2752 /* This is a specialization of a member template, without
2753 specialization the containing class. Something like:
2754
2755 template <class T> struct S {
2756 template <class U> void f (U);
2757 };
2758 template <> template <class U> void S<int>::f(U) {}
2759
2760 That's a specialization -- but of the entire template. */
2761 specialization = 1;
2762 break;
2763
2764 default:
2765 gcc_unreachable ();
2766 }
2767
2768 if ((specialization || member_specialization)
2769 /* This doesn't apply to variable templates. */
2770 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2771 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2772 {
2773 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2774 for (; t; t = TREE_CHAIN (t))
2775 if (TREE_PURPOSE (t))
2776 {
2777 permerror (input_location,
2778 "default argument specified in explicit specialization");
2779 break;
2780 }
2781 }
2782
2783 if (specialization || member_specialization || explicit_instantiation)
2784 {
2785 tree tmpl = NULL_TREE;
2786 tree targs = NULL_TREE;
2787 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2788
2789 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2790 if (!was_template_id)
2791 {
2792 tree fns;
2793
2794 gcc_assert (identifier_p (declarator));
2795 if (ctype)
2796 fns = dname;
2797 else
2798 {
2799 /* If there is no class context, the explicit instantiation
2800 must be at namespace scope. */
2801 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2802
2803 /* Find the namespace binding, using the declaration
2804 context. */
2805 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2806 false, true);
2807 if (fns == error_mark_node)
2808 /* If lookup fails, look for a friend declaration so we can
2809 give a better diagnostic. */
2810 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2811 /*type*/false, /*complain*/true,
2812 /*hidden*/true);
2813
2814 if (fns == error_mark_node || !is_overloaded_fn (fns))
2815 {
2816 error ("%qD is not a template function", dname);
2817 fns = error_mark_node;
2818 }
2819 }
2820
2821 declarator = lookup_template_function (fns, NULL_TREE);
2822 }
2823
2824 if (declarator == error_mark_node)
2825 return error_mark_node;
2826
2827 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2828 {
2829 if (!explicit_instantiation)
2830 /* A specialization in class scope. This is invalid,
2831 but the error will already have been flagged by
2832 check_specialization_scope. */
2833 return error_mark_node;
2834 else
2835 {
2836 /* It's not valid to write an explicit instantiation in
2837 class scope, e.g.:
2838
2839 class C { template void f(); }
2840
2841 This case is caught by the parser. However, on
2842 something like:
2843
2844 template class C { void f(); };
2845
2846 (which is invalid) we can get here. The error will be
2847 issued later. */
2848 ;
2849 }
2850
2851 return decl;
2852 }
2853 else if (ctype != NULL_TREE
2854 && (identifier_p (TREE_OPERAND (declarator, 0))))
2855 {
2856 // We'll match variable templates in start_decl.
2857 if (VAR_P (decl))
2858 return decl;
2859
2860 /* Find the list of functions in ctype that have the same
2861 name as the declared function. */
2862 tree name = TREE_OPERAND (declarator, 0);
2863 tree fns = NULL_TREE;
2864 int idx;
2865
2866 if (constructor_name_p (name, ctype))
2867 {
2868 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2869
2870 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2871 : !CLASSTYPE_DESTRUCTORS (ctype))
2872 {
2873 /* From [temp.expl.spec]:
2874
2875 If such an explicit specialization for the member
2876 of a class template names an implicitly-declared
2877 special member function (clause _special_), the
2878 program is ill-formed.
2879
2880 Similar language is found in [temp.explicit]. */
2881 error ("specialization of implicitly-declared special member function");
2882 return error_mark_node;
2883 }
2884
2885 name = is_constructor ? ctor_identifier : dtor_identifier;
2886 }
2887
2888 if (!DECL_CONV_FN_P (decl))
2889 {
2890 idx = lookup_fnfields_1 (ctype, name);
2891 if (idx >= 0)
2892 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2893 }
2894 else
2895 {
2896 vec<tree, va_gc> *methods;
2897 tree ovl;
2898
2899 /* For a type-conversion operator, we cannot do a
2900 name-based lookup. We might be looking for `operator
2901 int' which will be a specialization of `operator T'.
2902 So, we find *all* the conversion operators, and then
2903 select from them. */
2904 fns = NULL_TREE;
2905
2906 methods = CLASSTYPE_METHOD_VEC (ctype);
2907 if (methods)
2908 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2909 methods->iterate (idx, &ovl);
2910 ++idx)
2911 {
2912 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2913 /* There are no more conversion functions. */
2914 break;
2915
2916 /* Glue all these conversion functions together
2917 with those we already have. */
2918 for (; ovl; ovl = OVL_NEXT (ovl))
2919 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2920 }
2921 }
2922
2923 if (fns == NULL_TREE)
2924 {
2925 error ("no member function %qD declared in %qT", name, ctype);
2926 return error_mark_node;
2927 }
2928 else
2929 TREE_OPERAND (declarator, 0) = fns;
2930 }
2931
2932 /* Figure out what exactly is being specialized at this point.
2933 Note that for an explicit instantiation, even one for a
2934 member function, we cannot tell apriori whether the
2935 instantiation is for a member template, or just a member
2936 function of a template class. Even if a member template is
2937 being instantiated, the member template arguments may be
2938 elided if they can be deduced from the rest of the
2939 declaration. */
2940 tmpl = determine_specialization (declarator, decl,
2941 &targs,
2942 member_specialization,
2943 template_count,
2944 tsk);
2945
2946 if (!tmpl || tmpl == error_mark_node)
2947 /* We couldn't figure out what this declaration was
2948 specializing. */
2949 return error_mark_node;
2950 else
2951 {
2952 if (!ctype && !was_template_id
2953 && (specialization || member_specialization
2954 || explicit_instantiation)
2955 && !is_associated_namespace (CP_DECL_CONTEXT (decl),
2956 CP_DECL_CONTEXT (tmpl)))
2957 error ("%qD is not declared in %qD",
2958 tmpl, current_namespace);
2959 else if (TREE_CODE (decl) == FUNCTION_DECL
2960 && DECL_HIDDEN_FRIEND_P (tmpl))
2961 {
2962 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2963 "friend declaration %qD is not visible to "
2964 "explicit specialization", tmpl))
2965 inform (DECL_SOURCE_LOCATION (tmpl),
2966 "friend declaration here");
2967 }
2968
2969 tree gen_tmpl = most_general_template (tmpl);
2970
2971 if (explicit_instantiation)
2972 {
2973 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2974 is done by do_decl_instantiation later. */
2975
2976 int arg_depth = TMPL_ARGS_DEPTH (targs);
2977 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2978
2979 if (arg_depth > parm_depth)
2980 {
2981 /* If TMPL is not the most general template (for
2982 example, if TMPL is a friend template that is
2983 injected into namespace scope), then there will
2984 be too many levels of TARGS. Remove some of them
2985 here. */
2986 int i;
2987 tree new_targs;
2988
2989 new_targs = make_tree_vec (parm_depth);
2990 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2991 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2992 = TREE_VEC_ELT (targs, i);
2993 targs = new_targs;
2994 }
2995
2996 return instantiate_template (tmpl, targs, tf_error);
2997 }
2998
2999 /* If we thought that the DECL was a member function, but it
3000 turns out to be specializing a static member function,
3001 make DECL a static member function as well. */
3002 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3003 && DECL_STATIC_FUNCTION_P (tmpl)
3004 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3005 revert_static_member_fn (decl);
3006
3007 /* If this is a specialization of a member template of a
3008 template class, we want to return the TEMPLATE_DECL, not
3009 the specialization of it. */
3010 if (tsk == tsk_template && !was_template_id)
3011 {
3012 tree result = DECL_TEMPLATE_RESULT (tmpl);
3013 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3014 DECL_INITIAL (result) = NULL_TREE;
3015 if (have_def)
3016 {
3017 tree parm;
3018 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3019 DECL_SOURCE_LOCATION (result)
3020 = DECL_SOURCE_LOCATION (decl);
3021 /* We want to use the argument list specified in the
3022 definition, not in the original declaration. */
3023 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3024 for (parm = DECL_ARGUMENTS (result); parm;
3025 parm = DECL_CHAIN (parm))
3026 DECL_CONTEXT (parm) = result;
3027 }
3028 return register_specialization (tmpl, gen_tmpl, targs,
3029 is_friend, 0);
3030 }
3031
3032 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3033 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3034
3035 if (was_template_id)
3036 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3037
3038 /* Inherit default function arguments from the template
3039 DECL is specializing. */
3040 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3041 copy_default_args_to_explicit_spec (decl);
3042
3043 /* This specialization has the same protection as the
3044 template it specializes. */
3045 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3046 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3047
3048 /* 7.1.1-1 [dcl.stc]
3049
3050 A storage-class-specifier shall not be specified in an
3051 explicit specialization...
3052
3053 The parser rejects these, so unless action is taken here,
3054 explicit function specializations will always appear with
3055 global linkage.
3056
3057 The action recommended by the C++ CWG in response to C++
3058 defect report 605 is to make the storage class and linkage
3059 of the explicit specialization match the templated function:
3060
3061 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3062 */
3063 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3064 {
3065 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3066 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3067
3068 /* A concept cannot be specialized. */
3069 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3070 {
3071 error ("explicit specialization of function concept %qD",
3072 gen_tmpl);
3073 return error_mark_node;
3074 }
3075
3076 /* This specialization has the same linkage and visibility as
3077 the function template it specializes. */
3078 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3079 if (! TREE_PUBLIC (decl))
3080 {
3081 DECL_INTERFACE_KNOWN (decl) = 1;
3082 DECL_NOT_REALLY_EXTERN (decl) = 1;
3083 }
3084 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3085 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3086 {
3087 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3088 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3089 }
3090 }
3091
3092 /* If DECL is a friend declaration, declared using an
3093 unqualified name, the namespace associated with DECL may
3094 have been set incorrectly. For example, in:
3095
3096 template <typename T> void f(T);
3097 namespace N {
3098 struct S { friend void f<int>(int); }
3099 }
3100
3101 we will have set the DECL_CONTEXT for the friend
3102 declaration to N, rather than to the global namespace. */
3103 if (DECL_NAMESPACE_SCOPE_P (decl))
3104 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3105
3106 if (is_friend && !have_def)
3107 /* This is not really a declaration of a specialization.
3108 It's just the name of an instantiation. But, it's not
3109 a request for an instantiation, either. */
3110 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3111 else if (TREE_CODE (decl) == FUNCTION_DECL)
3112 /* A specialization is not necessarily COMDAT. */
3113 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3114 && DECL_DECLARED_INLINE_P (decl));
3115 else if (VAR_P (decl))
3116 DECL_COMDAT (decl) = false;
3117
3118 /* If this is a full specialization, register it so that we can find
3119 it again. Partial specializations will be registered in
3120 process_partial_specialization. */
3121 if (!processing_template_decl)
3122 decl = register_specialization (decl, gen_tmpl, targs,
3123 is_friend, 0);
3124
3125 /* A 'structor should already have clones. */
3126 gcc_assert (decl == error_mark_node
3127 || variable_template_p (tmpl)
3128 || !(DECL_CONSTRUCTOR_P (decl)
3129 || DECL_DESTRUCTOR_P (decl))
3130 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3131 }
3132 }
3133
3134 return decl;
3135 }
3136
3137 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3138 parameters. These are represented in the same format used for
3139 DECL_TEMPLATE_PARMS. */
3140
3141 int
3142 comp_template_parms (const_tree parms1, const_tree parms2)
3143 {
3144 const_tree p1;
3145 const_tree p2;
3146
3147 if (parms1 == parms2)
3148 return 1;
3149
3150 for (p1 = parms1, p2 = parms2;
3151 p1 != NULL_TREE && p2 != NULL_TREE;
3152 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3153 {
3154 tree t1 = TREE_VALUE (p1);
3155 tree t2 = TREE_VALUE (p2);
3156 int i;
3157
3158 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3159 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3160
3161 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3162 return 0;
3163
3164 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3165 {
3166 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3167 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3168
3169 /* If either of the template parameters are invalid, assume
3170 they match for the sake of error recovery. */
3171 if (error_operand_p (parm1) || error_operand_p (parm2))
3172 return 1;
3173
3174 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3175 return 0;
3176
3177 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3178 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3179 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3180 continue;
3181 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3182 return 0;
3183 }
3184 }
3185
3186 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3187 /* One set of parameters has more parameters lists than the
3188 other. */
3189 return 0;
3190
3191 return 1;
3192 }
3193
3194 /* Determine whether PARM is a parameter pack. */
3195
3196 bool
3197 template_parameter_pack_p (const_tree parm)
3198 {
3199 /* Determine if we have a non-type template parameter pack. */
3200 if (TREE_CODE (parm) == PARM_DECL)
3201 return (DECL_TEMPLATE_PARM_P (parm)
3202 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3203 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3204 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3205
3206 /* If this is a list of template parameters, we could get a
3207 TYPE_DECL or a TEMPLATE_DECL. */
3208 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3209 parm = TREE_TYPE (parm);
3210
3211 /* Otherwise it must be a type template parameter. */
3212 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3213 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3214 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3215 }
3216
3217 /* Determine if T is a function parameter pack. */
3218
3219 bool
3220 function_parameter_pack_p (const_tree t)
3221 {
3222 if (t && TREE_CODE (t) == PARM_DECL)
3223 return DECL_PACK_P (t);
3224 return false;
3225 }
3226
3227 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3228 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3229
3230 tree
3231 get_function_template_decl (const_tree primary_func_tmpl_inst)
3232 {
3233 if (! primary_func_tmpl_inst
3234 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3235 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3236 return NULL;
3237
3238 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3239 }
3240
3241 /* Return true iff the function parameter PARAM_DECL was expanded
3242 from the function parameter pack PACK. */
3243
3244 bool
3245 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3246 {
3247 if (DECL_ARTIFICIAL (param_decl)
3248 || !function_parameter_pack_p (pack))
3249 return false;
3250
3251 /* The parameter pack and its pack arguments have the same
3252 DECL_PARM_INDEX. */
3253 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3254 }
3255
3256 /* Determine whether ARGS describes a variadic template args list,
3257 i.e., one that is terminated by a template argument pack. */
3258
3259 static bool
3260 template_args_variadic_p (tree args)
3261 {
3262 int nargs;
3263 tree last_parm;
3264
3265 if (args == NULL_TREE)
3266 return false;
3267
3268 args = INNERMOST_TEMPLATE_ARGS (args);
3269 nargs = TREE_VEC_LENGTH (args);
3270
3271 if (nargs == 0)
3272 return false;
3273
3274 last_parm = TREE_VEC_ELT (args, nargs - 1);
3275
3276 return ARGUMENT_PACK_P (last_parm);
3277 }
3278
3279 /* Generate a new name for the parameter pack name NAME (an
3280 IDENTIFIER_NODE) that incorporates its */
3281
3282 static tree
3283 make_ith_pack_parameter_name (tree name, int i)
3284 {
3285 /* Munge the name to include the parameter index. */
3286 #define NUMBUF_LEN 128
3287 char numbuf[NUMBUF_LEN];
3288 char* newname;
3289 int newname_len;
3290
3291 if (name == NULL_TREE)
3292 return name;
3293 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3294 newname_len = IDENTIFIER_LENGTH (name)
3295 + strlen (numbuf) + 2;
3296 newname = (char*)alloca (newname_len);
3297 snprintf (newname, newname_len,
3298 "%s#%i", IDENTIFIER_POINTER (name), i);
3299 return get_identifier (newname);
3300 }
3301
3302 /* Return true if T is a primary function, class or alias template
3303 instantiation. */
3304
3305 bool
3306 primary_template_instantiation_p (const_tree t)
3307 {
3308 if (!t)
3309 return false;
3310
3311 if (TREE_CODE (t) == FUNCTION_DECL)
3312 return DECL_LANG_SPECIFIC (t)
3313 && DECL_TEMPLATE_INSTANTIATION (t)
3314 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3315 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3316 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3317 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3318 else if (alias_template_specialization_p (t))
3319 return true;
3320 return false;
3321 }
3322
3323 /* Return true if PARM is a template template parameter. */
3324
3325 bool
3326 template_template_parameter_p (const_tree parm)
3327 {
3328 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3329 }
3330
3331 /* Return true iff PARM is a DECL representing a type template
3332 parameter. */
3333
3334 bool
3335 template_type_parameter_p (const_tree parm)
3336 {
3337 return (parm
3338 && (TREE_CODE (parm) == TYPE_DECL
3339 || TREE_CODE (parm) == TEMPLATE_DECL)
3340 && DECL_TEMPLATE_PARM_P (parm));
3341 }
3342
3343 /* Return the template parameters of T if T is a
3344 primary template instantiation, NULL otherwise. */
3345
3346 tree
3347 get_primary_template_innermost_parameters (const_tree t)
3348 {
3349 tree parms = NULL, template_info = NULL;
3350
3351 if ((template_info = get_template_info (t))
3352 && primary_template_instantiation_p (t))
3353 parms = INNERMOST_TEMPLATE_PARMS
3354 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3355
3356 return parms;
3357 }
3358
3359 /* Return the template parameters of the LEVELth level from the full list
3360 of template parameters PARMS. */
3361
3362 tree
3363 get_template_parms_at_level (tree parms, int level)
3364 {
3365 tree p;
3366 if (!parms
3367 || TREE_CODE (parms) != TREE_LIST
3368 || level > TMPL_PARMS_DEPTH (parms))
3369 return NULL_TREE;
3370
3371 for (p = parms; p; p = TREE_CHAIN (p))
3372 if (TMPL_PARMS_DEPTH (p) == level)
3373 return p;
3374
3375 return NULL_TREE;
3376 }
3377
3378 /* Returns the template arguments of T if T is a template instantiation,
3379 NULL otherwise. */
3380
3381 tree
3382 get_template_innermost_arguments (const_tree t)
3383 {
3384 tree args = NULL, template_info = NULL;
3385
3386 if ((template_info = get_template_info (t))
3387 && TI_ARGS (template_info))
3388 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3389
3390 return args;
3391 }
3392
3393 /* Return the argument pack elements of T if T is a template argument pack,
3394 NULL otherwise. */
3395
3396 tree
3397 get_template_argument_pack_elems (const_tree t)
3398 {
3399 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3400 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3401 return NULL;
3402
3403 return ARGUMENT_PACK_ARGS (t);
3404 }
3405
3406 /* Structure used to track the progress of find_parameter_packs_r. */
3407 struct find_parameter_pack_data
3408 {
3409 /* TREE_LIST that will contain all of the parameter packs found by
3410 the traversal. */
3411 tree* parameter_packs;
3412
3413 /* Set of AST nodes that have been visited by the traversal. */
3414 hash_set<tree> *visited;
3415
3416 /* True iff we're making a type pack expansion. */
3417 bool type_pack_expansion_p;
3418 };
3419
3420 /* Identifies all of the argument packs that occur in a template
3421 argument and appends them to the TREE_LIST inside DATA, which is a
3422 find_parameter_pack_data structure. This is a subroutine of
3423 make_pack_expansion and uses_parameter_packs. */
3424 static tree
3425 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3426 {
3427 tree t = *tp;
3428 struct find_parameter_pack_data* ppd =
3429 (struct find_parameter_pack_data*)data;
3430 bool parameter_pack_p = false;
3431
3432 /* Handle type aliases/typedefs. */
3433 if (TYPE_ALIAS_P (t))
3434 {
3435 if (TYPE_TEMPLATE_INFO (t))
3436 cp_walk_tree (&TYPE_TI_ARGS (t),
3437 &find_parameter_packs_r,
3438 ppd, ppd->visited);
3439 *walk_subtrees = 0;
3440 return NULL_TREE;
3441 }
3442
3443 /* Identify whether this is a parameter pack or not. */
3444 switch (TREE_CODE (t))
3445 {
3446 case TEMPLATE_PARM_INDEX:
3447 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3448 parameter_pack_p = true;
3449 break;
3450
3451 case TEMPLATE_TYPE_PARM:
3452 t = TYPE_MAIN_VARIANT (t);
3453 case TEMPLATE_TEMPLATE_PARM:
3454 /* If the placeholder appears in the decl-specifier-seq of a function
3455 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3456 is a pack expansion, the invented template parameter is a template
3457 parameter pack. */
3458 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3459 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3460 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3461 parameter_pack_p = true;
3462 break;
3463
3464 case FIELD_DECL:
3465 case PARM_DECL:
3466 if (DECL_PACK_P (t))
3467 {
3468 /* We don't want to walk into the type of a PARM_DECL,
3469 because we don't want to see the type parameter pack. */
3470 *walk_subtrees = 0;
3471 parameter_pack_p = true;
3472 }
3473 break;
3474
3475 /* Look through a lambda capture proxy to the field pack. */
3476 case VAR_DECL:
3477 if (DECL_HAS_VALUE_EXPR_P (t))
3478 {
3479 tree v = DECL_VALUE_EXPR (t);
3480 cp_walk_tree (&v,
3481 &find_parameter_packs_r,
3482 ppd, ppd->visited);
3483 *walk_subtrees = 0;
3484 }
3485 else if (variable_template_specialization_p (t))
3486 {
3487 cp_walk_tree (&DECL_TI_ARGS (t),
3488 find_parameter_packs_r,
3489 ppd, ppd->visited);
3490 *walk_subtrees = 0;
3491 }
3492 break;
3493
3494 case BASES:
3495 parameter_pack_p = true;
3496 break;
3497 default:
3498 /* Not a parameter pack. */
3499 break;
3500 }
3501
3502 if (parameter_pack_p)
3503 {
3504 /* Add this parameter pack to the list. */
3505 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3506 }
3507
3508 if (TYPE_P (t))
3509 cp_walk_tree (&TYPE_CONTEXT (t),
3510 &find_parameter_packs_r, ppd, ppd->visited);
3511
3512 /* This switch statement will return immediately if we don't find a
3513 parameter pack. */
3514 switch (TREE_CODE (t))
3515 {
3516 case TEMPLATE_PARM_INDEX:
3517 return NULL_TREE;
3518
3519 case BOUND_TEMPLATE_TEMPLATE_PARM:
3520 /* Check the template itself. */
3521 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3522 &find_parameter_packs_r, ppd, ppd->visited);
3523 /* Check the template arguments. */
3524 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3525 ppd->visited);
3526 *walk_subtrees = 0;
3527 return NULL_TREE;
3528
3529 case TEMPLATE_TYPE_PARM:
3530 case TEMPLATE_TEMPLATE_PARM:
3531 return NULL_TREE;
3532
3533 case PARM_DECL:
3534 return NULL_TREE;
3535
3536 case RECORD_TYPE:
3537 if (TYPE_PTRMEMFUNC_P (t))
3538 return NULL_TREE;
3539 /* Fall through. */
3540
3541 case UNION_TYPE:
3542 case ENUMERAL_TYPE:
3543 if (TYPE_TEMPLATE_INFO (t))
3544 cp_walk_tree (&TYPE_TI_ARGS (t),
3545 &find_parameter_packs_r, ppd, ppd->visited);
3546
3547 *walk_subtrees = 0;
3548 return NULL_TREE;
3549
3550 case CONSTRUCTOR:
3551 case TEMPLATE_DECL:
3552 cp_walk_tree (&TREE_TYPE (t),
3553 &find_parameter_packs_r, ppd, ppd->visited);
3554 return NULL_TREE;
3555
3556 case TYPENAME_TYPE:
3557 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3558 ppd, ppd->visited);
3559 *walk_subtrees = 0;
3560 return NULL_TREE;
3561
3562 case TYPE_PACK_EXPANSION:
3563 case EXPR_PACK_EXPANSION:
3564 *walk_subtrees = 0;
3565 return NULL_TREE;
3566
3567 case INTEGER_TYPE:
3568 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3569 ppd, ppd->visited);
3570 *walk_subtrees = 0;
3571 return NULL_TREE;
3572
3573 case IDENTIFIER_NODE:
3574 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3575 ppd->visited);
3576 *walk_subtrees = 0;
3577 return NULL_TREE;
3578
3579 case DECLTYPE_TYPE:
3580 {
3581 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3582 type_pack_expansion_p to false so that any placeholders
3583 within the expression don't get marked as parameter packs. */
3584 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3585 ppd->type_pack_expansion_p = false;
3586 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3587 ppd, ppd->visited);
3588 ppd->type_pack_expansion_p = type_pack_expansion_p;
3589 *walk_subtrees = 0;
3590 return NULL_TREE;
3591 }
3592
3593 default:
3594 return NULL_TREE;
3595 }
3596
3597 return NULL_TREE;
3598 }
3599
3600 /* Determines if the expression or type T uses any parameter packs. */
3601 bool
3602 uses_parameter_packs (tree t)
3603 {
3604 tree parameter_packs = NULL_TREE;
3605 struct find_parameter_pack_data ppd;
3606 ppd.parameter_packs = &parameter_packs;
3607 ppd.visited = new hash_set<tree>;
3608 ppd.type_pack_expansion_p = false;
3609 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3610 delete ppd.visited;
3611 return parameter_packs != NULL_TREE;
3612 }
3613
3614 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3615 representation a base-class initializer into a parameter pack
3616 expansion. If all goes well, the resulting node will be an
3617 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3618 respectively. */
3619 tree
3620 make_pack_expansion (tree arg)
3621 {
3622 tree result;
3623 tree parameter_packs = NULL_TREE;
3624 bool for_types = false;
3625 struct find_parameter_pack_data ppd;
3626
3627 if (!arg || arg == error_mark_node)
3628 return arg;
3629
3630 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3631 {
3632 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3633 class initializer. In this case, the TREE_PURPOSE will be a
3634 _TYPE node (representing the base class expansion we're
3635 initializing) and the TREE_VALUE will be a TREE_LIST
3636 containing the initialization arguments.
3637
3638 The resulting expansion looks somewhat different from most
3639 expansions. Rather than returning just one _EXPANSION, we
3640 return a TREE_LIST whose TREE_PURPOSE is a
3641 TYPE_PACK_EXPANSION containing the bases that will be
3642 initialized. The TREE_VALUE will be identical to the
3643 original TREE_VALUE, which is a list of arguments that will
3644 be passed to each base. We do not introduce any new pack
3645 expansion nodes into the TREE_VALUE (although it is possible
3646 that some already exist), because the TREE_PURPOSE and
3647 TREE_VALUE all need to be expanded together with the same
3648 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3649 resulting TREE_PURPOSE will mention the parameter packs in
3650 both the bases and the arguments to the bases. */
3651 tree purpose;
3652 tree value;
3653 tree parameter_packs = NULL_TREE;
3654
3655 /* Determine which parameter packs will be used by the base
3656 class expansion. */
3657 ppd.visited = new hash_set<tree>;
3658 ppd.parameter_packs = &parameter_packs;
3659 ppd.type_pack_expansion_p = true;
3660 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3661 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3662 &ppd, ppd.visited);
3663
3664 if (parameter_packs == NULL_TREE)
3665 {
3666 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3667 delete ppd.visited;
3668 return error_mark_node;
3669 }
3670
3671 if (TREE_VALUE (arg) != void_type_node)
3672 {
3673 /* Collect the sets of parameter packs used in each of the
3674 initialization arguments. */
3675 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3676 {
3677 /* Determine which parameter packs will be expanded in this
3678 argument. */
3679 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3680 &ppd, ppd.visited);
3681 }
3682 }
3683
3684 delete ppd.visited;
3685
3686 /* Create the pack expansion type for the base type. */
3687 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3688 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3689 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3690
3691 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3692 they will rarely be compared to anything. */
3693 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3694
3695 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3696 }
3697
3698 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3699 for_types = true;
3700
3701 /* Build the PACK_EXPANSION_* node. */
3702 result = for_types
3703 ? cxx_make_type (TYPE_PACK_EXPANSION)
3704 : make_node (EXPR_PACK_EXPANSION);
3705 SET_PACK_EXPANSION_PATTERN (result, arg);
3706 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3707 {
3708 /* Propagate type and const-expression information. */
3709 TREE_TYPE (result) = TREE_TYPE (arg);
3710 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3711 /* Mark this read now, since the expansion might be length 0. */
3712 mark_exp_read (arg);
3713 }
3714 else
3715 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3716 they will rarely be compared to anything. */
3717 SET_TYPE_STRUCTURAL_EQUALITY (result);
3718
3719 /* Determine which parameter packs will be expanded. */
3720 ppd.parameter_packs = &parameter_packs;
3721 ppd.visited = new hash_set<tree>;
3722 ppd.type_pack_expansion_p = TYPE_P (arg);
3723 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3724 delete ppd.visited;
3725
3726 /* Make sure we found some parameter packs. */
3727 if (parameter_packs == NULL_TREE)
3728 {
3729 if (TYPE_P (arg))
3730 error ("expansion pattern %<%T%> contains no argument packs", arg);
3731 else
3732 error ("expansion pattern %<%E%> contains no argument packs", arg);
3733 return error_mark_node;
3734 }
3735 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3736
3737 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3738
3739 return result;
3740 }
3741
3742 /* Checks T for any "bare" parameter packs, which have not yet been
3743 expanded, and issues an error if any are found. This operation can
3744 only be done on full expressions or types (e.g., an expression
3745 statement, "if" condition, etc.), because we could have expressions like:
3746
3747 foo(f(g(h(args)))...)
3748
3749 where "args" is a parameter pack. check_for_bare_parameter_packs
3750 should not be called for the subexpressions args, h(args),
3751 g(h(args)), or f(g(h(args))), because we would produce erroneous
3752 error messages.
3753
3754 Returns TRUE and emits an error if there were bare parameter packs,
3755 returns FALSE otherwise. */
3756 bool
3757 check_for_bare_parameter_packs (tree t)
3758 {
3759 tree parameter_packs = NULL_TREE;
3760 struct find_parameter_pack_data ppd;
3761
3762 if (!processing_template_decl || !t || t == error_mark_node)
3763 return false;
3764
3765 if (TREE_CODE (t) == TYPE_DECL)
3766 t = TREE_TYPE (t);
3767
3768 ppd.parameter_packs = &parameter_packs;
3769 ppd.visited = new hash_set<tree>;
3770 ppd.type_pack_expansion_p = false;
3771 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3772 delete ppd.visited;
3773
3774 if (parameter_packs)
3775 {
3776 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3777 error_at (loc, "parameter packs not expanded with %<...%>:");
3778 while (parameter_packs)
3779 {
3780 tree pack = TREE_VALUE (parameter_packs);
3781 tree name = NULL_TREE;
3782
3783 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3784 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3785 name = TYPE_NAME (pack);
3786 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3787 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3788 else
3789 name = DECL_NAME (pack);
3790
3791 if (name)
3792 inform (loc, " %qD", name);
3793 else
3794 inform (loc, " <anonymous>");
3795
3796 parameter_packs = TREE_CHAIN (parameter_packs);
3797 }
3798
3799 return true;
3800 }
3801
3802 return false;
3803 }
3804
3805 /* Expand any parameter packs that occur in the template arguments in
3806 ARGS. */
3807 tree
3808 expand_template_argument_pack (tree args)
3809 {
3810 tree result_args = NULL_TREE;
3811 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3812 int num_result_args = -1;
3813 int non_default_args_count = -1;
3814
3815 /* First, determine if we need to expand anything, and the number of
3816 slots we'll need. */
3817 for (in_arg = 0; in_arg < nargs; ++in_arg)
3818 {
3819 tree arg = TREE_VEC_ELT (args, in_arg);
3820 if (arg == NULL_TREE)
3821 return args;
3822 if (ARGUMENT_PACK_P (arg))
3823 {
3824 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3825 if (num_result_args < 0)
3826 num_result_args = in_arg + num_packed;
3827 else
3828 num_result_args += num_packed;
3829 }
3830 else
3831 {
3832 if (num_result_args >= 0)
3833 num_result_args++;
3834 }
3835 }
3836
3837 /* If no expansion is necessary, we're done. */
3838 if (num_result_args < 0)
3839 return args;
3840
3841 /* Expand arguments. */
3842 result_args = make_tree_vec (num_result_args);
3843 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3844 non_default_args_count =
3845 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3846 for (in_arg = 0; in_arg < nargs; ++in_arg)
3847 {
3848 tree arg = TREE_VEC_ELT (args, in_arg);
3849 if (ARGUMENT_PACK_P (arg))
3850 {
3851 tree packed = ARGUMENT_PACK_ARGS (arg);
3852 int i, num_packed = TREE_VEC_LENGTH (packed);
3853 for (i = 0; i < num_packed; ++i, ++out_arg)
3854 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3855 if (non_default_args_count > 0)
3856 non_default_args_count += num_packed - 1;
3857 }
3858 else
3859 {
3860 TREE_VEC_ELT (result_args, out_arg) = arg;
3861 ++out_arg;
3862 }
3863 }
3864 if (non_default_args_count >= 0)
3865 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3866 return result_args;
3867 }
3868
3869 /* Checks if DECL shadows a template parameter.
3870
3871 [temp.local]: A template-parameter shall not be redeclared within its
3872 scope (including nested scopes).
3873
3874 Emits an error and returns TRUE if the DECL shadows a parameter,
3875 returns FALSE otherwise. */
3876
3877 bool
3878 check_template_shadow (tree decl)
3879 {
3880 tree olddecl;
3881
3882 /* If we're not in a template, we can't possibly shadow a template
3883 parameter. */
3884 if (!current_template_parms)
3885 return true;
3886
3887 /* Figure out what we're shadowing. */
3888 if (TREE_CODE (decl) == OVERLOAD)
3889 decl = OVL_CURRENT (decl);
3890 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3891
3892 /* If there's no previous binding for this name, we're not shadowing
3893 anything, let alone a template parameter. */
3894 if (!olddecl)
3895 return true;
3896
3897 /* If we're not shadowing a template parameter, we're done. Note
3898 that OLDDECL might be an OVERLOAD (or perhaps even an
3899 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3900 node. */
3901 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3902 return true;
3903
3904 /* We check for decl != olddecl to avoid bogus errors for using a
3905 name inside a class. We check TPFI to avoid duplicate errors for
3906 inline member templates. */
3907 if (decl == olddecl
3908 || (DECL_TEMPLATE_PARM_P (decl)
3909 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3910 return true;
3911
3912 /* Don't complain about the injected class name, as we've already
3913 complained about the class itself. */
3914 if (DECL_SELF_REFERENCE_P (decl))
3915 return false;
3916
3917 if (DECL_TEMPLATE_PARM_P (decl))
3918 error ("declaration of template parameter %q+D shadows "
3919 "template parameter", decl);
3920 else
3921 error ("declaration of %q+#D shadows template parameter", decl);
3922 inform (DECL_SOURCE_LOCATION (olddecl),
3923 "template parameter %qD declared here", olddecl);
3924 return false;
3925 }
3926
3927 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3928 ORIG_LEVEL, DECL, and TYPE. */
3929
3930 static tree
3931 build_template_parm_index (int index,
3932 int level,
3933 int orig_level,
3934 tree decl,
3935 tree type)
3936 {
3937 tree t = make_node (TEMPLATE_PARM_INDEX);
3938 TEMPLATE_PARM_IDX (t) = index;
3939 TEMPLATE_PARM_LEVEL (t) = level;
3940 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3941 TEMPLATE_PARM_DECL (t) = decl;
3942 TREE_TYPE (t) = type;
3943 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3944 TREE_READONLY (t) = TREE_READONLY (decl);
3945
3946 return t;
3947 }
3948
3949 /* Find the canonical type parameter for the given template type
3950 parameter. Returns the canonical type parameter, which may be TYPE
3951 if no such parameter existed. */
3952
3953 static tree
3954 canonical_type_parameter (tree type)
3955 {
3956 tree list;
3957 int idx = TEMPLATE_TYPE_IDX (type);
3958 if (!canonical_template_parms)
3959 vec_alloc (canonical_template_parms, idx+1);
3960
3961 while (canonical_template_parms->length () <= (unsigned)idx)
3962 vec_safe_push (canonical_template_parms, NULL_TREE);
3963
3964 list = (*canonical_template_parms)[idx];
3965 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3966 list = TREE_CHAIN (list);
3967
3968 if (list)
3969 return TREE_VALUE (list);
3970 else
3971 {
3972 (*canonical_template_parms)[idx]
3973 = tree_cons (NULL_TREE, type,
3974 (*canonical_template_parms)[idx]);
3975 return type;
3976 }
3977 }
3978
3979 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3980 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3981 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3982 new one is created. */
3983
3984 static tree
3985 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3986 tsubst_flags_t complain)
3987 {
3988 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3989 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3990 != TEMPLATE_PARM_LEVEL (index) - levels)
3991 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3992 {
3993 tree orig_decl = TEMPLATE_PARM_DECL (index);
3994 tree decl, t;
3995
3996 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3997 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3998 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3999 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4000 DECL_ARTIFICIAL (decl) = 1;
4001 SET_DECL_TEMPLATE_PARM_P (decl);
4002
4003 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4004 TEMPLATE_PARM_LEVEL (index) - levels,
4005 TEMPLATE_PARM_ORIG_LEVEL (index),
4006 decl, type);
4007 TEMPLATE_PARM_DESCENDANTS (index) = t;
4008 TEMPLATE_PARM_PARAMETER_PACK (t)
4009 = TEMPLATE_PARM_PARAMETER_PACK (index);
4010
4011 /* Template template parameters need this. */
4012 if (TREE_CODE (decl) == TEMPLATE_DECL)
4013 {
4014 DECL_TEMPLATE_RESULT (decl)
4015 = build_decl (DECL_SOURCE_LOCATION (decl),
4016 TYPE_DECL, DECL_NAME (decl), type);
4017 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4018 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4019 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4020 }
4021 }
4022
4023 return TEMPLATE_PARM_DESCENDANTS (index);
4024 }
4025
4026 /* Process information from new template parameter PARM and append it
4027 to the LIST being built. This new parameter is a non-type
4028 parameter iff IS_NON_TYPE is true. This new parameter is a
4029 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4030 is in PARM_LOC. */
4031
4032 tree
4033 process_template_parm (tree list, location_t parm_loc, tree parm,
4034 bool is_non_type, bool is_parameter_pack)
4035 {
4036 tree decl = 0;
4037 int idx = 0;
4038
4039 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4040 tree defval = TREE_PURPOSE (parm);
4041 tree constr = TREE_TYPE (parm);
4042
4043 if (list)
4044 {
4045 tree p = tree_last (list);
4046
4047 if (p && TREE_VALUE (p) != error_mark_node)
4048 {
4049 p = TREE_VALUE (p);
4050 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4051 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4052 else
4053 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4054 }
4055
4056 ++idx;
4057 }
4058
4059 if (is_non_type)
4060 {
4061 parm = TREE_VALUE (parm);
4062
4063 SET_DECL_TEMPLATE_PARM_P (parm);
4064
4065 if (TREE_TYPE (parm) != error_mark_node)
4066 {
4067 /* [temp.param]
4068
4069 The top-level cv-qualifiers on the template-parameter are
4070 ignored when determining its type. */
4071 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4072 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4073 TREE_TYPE (parm) = error_mark_node;
4074 else if (uses_parameter_packs (TREE_TYPE (parm))
4075 && !is_parameter_pack
4076 /* If we're in a nested template parameter list, the template
4077 template parameter could be a parameter pack. */
4078 && processing_template_parmlist == 1)
4079 {
4080 /* This template parameter is not a parameter pack, but it
4081 should be. Complain about "bare" parameter packs. */
4082 check_for_bare_parameter_packs (TREE_TYPE (parm));
4083
4084 /* Recover by calling this a parameter pack. */
4085 is_parameter_pack = true;
4086 }
4087 }
4088
4089 /* A template parameter is not modifiable. */
4090 TREE_CONSTANT (parm) = 1;
4091 TREE_READONLY (parm) = 1;
4092 decl = build_decl (parm_loc,
4093 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4094 TREE_CONSTANT (decl) = 1;
4095 TREE_READONLY (decl) = 1;
4096 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4097 = build_template_parm_index (idx, processing_template_decl,
4098 processing_template_decl,
4099 decl, TREE_TYPE (parm));
4100
4101 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4102 = is_parameter_pack;
4103 }
4104 else
4105 {
4106 tree t;
4107 parm = TREE_VALUE (TREE_VALUE (parm));
4108
4109 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4110 {
4111 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4112 /* This is for distinguishing between real templates and template
4113 template parameters */
4114 TREE_TYPE (parm) = t;
4115 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4116 decl = parm;
4117 }
4118 else
4119 {
4120 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4121 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4122 decl = build_decl (parm_loc,
4123 TYPE_DECL, parm, t);
4124 }
4125
4126 TYPE_NAME (t) = decl;
4127 TYPE_STUB_DECL (t) = decl;
4128 parm = decl;
4129 TEMPLATE_TYPE_PARM_INDEX (t)
4130 = build_template_parm_index (idx, processing_template_decl,
4131 processing_template_decl,
4132 decl, TREE_TYPE (parm));
4133 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4134 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4135 }
4136 DECL_ARTIFICIAL (decl) = 1;
4137 SET_DECL_TEMPLATE_PARM_P (decl);
4138
4139 /* Build requirements for the type/template parameter.
4140 This must be done after SET_DECL_TEMPLATE_PARM_P or
4141 process_template_parm could fail. */
4142 tree reqs = finish_shorthand_constraint (parm, constr);
4143
4144 pushdecl (decl);
4145
4146 /* Build the parameter node linking the parameter declaration,
4147 its default argument (if any), and its constraints (if any). */
4148 parm = build_tree_list (defval, parm);
4149 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4150
4151 return chainon (list, parm);
4152 }
4153
4154 /* The end of a template parameter list has been reached. Process the
4155 tree list into a parameter vector, converting each parameter into a more
4156 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4157 as PARM_DECLs. */
4158
4159 tree
4160 end_template_parm_list (tree parms)
4161 {
4162 int nparms;
4163 tree parm, next;
4164 tree saved_parmlist = make_tree_vec (list_length (parms));
4165
4166 /* Pop the dummy parameter level and add the real one. */
4167 current_template_parms = TREE_CHAIN (current_template_parms);
4168
4169 current_template_parms
4170 = tree_cons (size_int (processing_template_decl),
4171 saved_parmlist, current_template_parms);
4172
4173 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4174 {
4175 next = TREE_CHAIN (parm);
4176 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4177 TREE_CHAIN (parm) = NULL_TREE;
4178 }
4179
4180 --processing_template_parmlist;
4181
4182 return saved_parmlist;
4183 }
4184
4185 // Explicitly indicate the end of the template parameter list. We assume
4186 // that the current template parameters have been constructed and/or
4187 // managed explicitly, as when creating new template template parameters
4188 // from a shorthand constraint.
4189 void
4190 end_template_parm_list ()
4191 {
4192 --processing_template_parmlist;
4193 }
4194
4195 /* end_template_decl is called after a template declaration is seen. */
4196
4197 void
4198 end_template_decl (void)
4199 {
4200 reset_specialization ();
4201
4202 if (! processing_template_decl)
4203 return;
4204
4205 /* This matches the pushlevel in begin_template_parm_list. */
4206 finish_scope ();
4207
4208 --processing_template_decl;
4209 current_template_parms = TREE_CHAIN (current_template_parms);
4210 }
4211
4212 /* Takes a TREE_LIST representing a template parameter and convert it
4213 into an argument suitable to be passed to the type substitution
4214 functions. Note that If the TREE_LIST contains an error_mark
4215 node, the returned argument is error_mark_node. */
4216
4217 tree
4218 template_parm_to_arg (tree t)
4219 {
4220
4221 if (t == NULL_TREE
4222 || TREE_CODE (t) != TREE_LIST)
4223 return t;
4224
4225 if (error_operand_p (TREE_VALUE (t)))
4226 return error_mark_node;
4227
4228 t = TREE_VALUE (t);
4229
4230 if (TREE_CODE (t) == TYPE_DECL
4231 || TREE_CODE (t) == TEMPLATE_DECL)
4232 {
4233 t = TREE_TYPE (t);
4234
4235 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4236 {
4237 /* Turn this argument into a TYPE_ARGUMENT_PACK
4238 with a single element, which expands T. */
4239 tree vec = make_tree_vec (1);
4240 if (CHECKING_P)
4241 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4242
4243 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4244
4245 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4246 SET_ARGUMENT_PACK_ARGS (t, vec);
4247 }
4248 }
4249 else
4250 {
4251 t = DECL_INITIAL (t);
4252
4253 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4254 {
4255 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4256 with a single element, which expands T. */
4257 tree vec = make_tree_vec (1);
4258 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4259 if (CHECKING_P)
4260 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4261
4262 t = convert_from_reference (t);
4263 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4264
4265 t = make_node (NONTYPE_ARGUMENT_PACK);
4266 SET_ARGUMENT_PACK_ARGS (t, vec);
4267 TREE_TYPE (t) = type;
4268 }
4269 else
4270 t = convert_from_reference (t);
4271 }
4272 return t;
4273 }
4274
4275 /* Given a set of template parameters, return them as a set of template
4276 arguments. The template parameters are represented as a TREE_VEC, in
4277 the form documented in cp-tree.h for template arguments. */
4278
4279 static tree
4280 template_parms_to_args (tree parms)
4281 {
4282 tree header;
4283 tree args = NULL_TREE;
4284 int length = TMPL_PARMS_DEPTH (parms);
4285 int l = length;
4286
4287 /* If there is only one level of template parameters, we do not
4288 create a TREE_VEC of TREE_VECs. Instead, we return a single
4289 TREE_VEC containing the arguments. */
4290 if (length > 1)
4291 args = make_tree_vec (length);
4292
4293 for (header = parms; header; header = TREE_CHAIN (header))
4294 {
4295 tree a = copy_node (TREE_VALUE (header));
4296 int i;
4297
4298 TREE_TYPE (a) = NULL_TREE;
4299 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4300 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4301
4302 if (CHECKING_P)
4303 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4304
4305 if (length > 1)
4306 TREE_VEC_ELT (args, --l) = a;
4307 else
4308 args = a;
4309 }
4310
4311 return args;
4312 }
4313
4314 /* Within the declaration of a template, return the currently active
4315 template parameters as an argument TREE_VEC. */
4316
4317 static tree
4318 current_template_args (void)
4319 {
4320 return template_parms_to_args (current_template_parms);
4321 }
4322
4323 /* Update the declared TYPE by doing any lookups which were thought to be
4324 dependent, but are not now that we know the SCOPE of the declarator. */
4325
4326 tree
4327 maybe_update_decl_type (tree orig_type, tree scope)
4328 {
4329 tree type = orig_type;
4330
4331 if (type == NULL_TREE)
4332 return type;
4333
4334 if (TREE_CODE (orig_type) == TYPE_DECL)
4335 type = TREE_TYPE (type);
4336
4337 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4338 && dependent_type_p (type)
4339 /* Don't bother building up the args in this case. */
4340 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4341 {
4342 /* tsubst in the args corresponding to the template parameters,
4343 including auto if present. Most things will be unchanged, but
4344 make_typename_type and tsubst_qualified_id will resolve
4345 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4346 tree args = current_template_args ();
4347 tree auto_node = type_uses_auto (type);
4348 tree pushed;
4349 if (auto_node)
4350 {
4351 tree auto_vec = make_tree_vec (1);
4352 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4353 args = add_to_template_args (args, auto_vec);
4354 }
4355 pushed = push_scope (scope);
4356 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4357 if (pushed)
4358 pop_scope (scope);
4359 }
4360
4361 if (type == error_mark_node)
4362 return orig_type;
4363
4364 if (TREE_CODE (orig_type) == TYPE_DECL)
4365 {
4366 if (same_type_p (type, TREE_TYPE (orig_type)))
4367 type = orig_type;
4368 else
4369 type = TYPE_NAME (type);
4370 }
4371 return type;
4372 }
4373
4374 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4375 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4376 the new template is a member template. */
4377
4378 tree
4379 build_template_decl (tree decl, tree parms, bool member_template_p)
4380 {
4381 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4382 DECL_TEMPLATE_PARMS (tmpl) = parms;
4383 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4384 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4385 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4386
4387 return tmpl;
4388 }
4389
4390 struct template_parm_data
4391 {
4392 /* The level of the template parameters we are currently
4393 processing. */
4394 int level;
4395
4396 /* The index of the specialization argument we are currently
4397 processing. */
4398 int current_arg;
4399
4400 /* An array whose size is the number of template parameters. The
4401 elements are nonzero if the parameter has been used in any one
4402 of the arguments processed so far. */
4403 int* parms;
4404
4405 /* An array whose size is the number of template arguments. The
4406 elements are nonzero if the argument makes use of template
4407 parameters of this level. */
4408 int* arg_uses_template_parms;
4409 };
4410
4411 /* Subroutine of push_template_decl used to see if each template
4412 parameter in a partial specialization is used in the explicit
4413 argument list. If T is of the LEVEL given in DATA (which is
4414 treated as a template_parm_data*), then DATA->PARMS is marked
4415 appropriately. */
4416
4417 static int
4418 mark_template_parm (tree t, void* data)
4419 {
4420 int level;
4421 int idx;
4422 struct template_parm_data* tpd = (struct template_parm_data*) data;
4423
4424 template_parm_level_and_index (t, &level, &idx);
4425
4426 if (level == tpd->level)
4427 {
4428 tpd->parms[idx] = 1;
4429 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4430 }
4431
4432 /* Return zero so that for_each_template_parm will continue the
4433 traversal of the tree; we want to mark *every* template parm. */
4434 return 0;
4435 }
4436
4437 /* Process the partial specialization DECL. */
4438
4439 static tree
4440 process_partial_specialization (tree decl)
4441 {
4442 tree type = TREE_TYPE (decl);
4443 tree tinfo = get_template_info (decl);
4444 tree maintmpl = TI_TEMPLATE (tinfo);
4445 tree specargs = TI_ARGS (tinfo);
4446 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4447 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4448 tree inner_parms;
4449 tree inst;
4450 int nargs = TREE_VEC_LENGTH (inner_args);
4451 int ntparms;
4452 int i;
4453 bool did_error_intro = false;
4454 struct template_parm_data tpd;
4455 struct template_parm_data tpd2;
4456
4457 gcc_assert (current_template_parms);
4458
4459 /* A concept cannot be specialized. */
4460 if (flag_concepts && variable_concept_p (maintmpl))
4461 {
4462 error ("specialization of variable concept %q#D", maintmpl);
4463 return error_mark_node;
4464 }
4465
4466 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4467 ntparms = TREE_VEC_LENGTH (inner_parms);
4468
4469 /* We check that each of the template parameters given in the
4470 partial specialization is used in the argument list to the
4471 specialization. For example:
4472
4473 template <class T> struct S;
4474 template <class T> struct S<T*>;
4475
4476 The second declaration is OK because `T*' uses the template
4477 parameter T, whereas
4478
4479 template <class T> struct S<int>;
4480
4481 is no good. Even trickier is:
4482
4483 template <class T>
4484 struct S1
4485 {
4486 template <class U>
4487 struct S2;
4488 template <class U>
4489 struct S2<T>;
4490 };
4491
4492 The S2<T> declaration is actually invalid; it is a
4493 full-specialization. Of course,
4494
4495 template <class U>
4496 struct S2<T (*)(U)>;
4497
4498 or some such would have been OK. */
4499 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4500 tpd.parms = XALLOCAVEC (int, ntparms);
4501 memset (tpd.parms, 0, sizeof (int) * ntparms);
4502
4503 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4504 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4505 for (i = 0; i < nargs; ++i)
4506 {
4507 tpd.current_arg = i;
4508 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4509 &mark_template_parm,
4510 &tpd,
4511 NULL,
4512 /*include_nondeduced_p=*/false);
4513 }
4514 for (i = 0; i < ntparms; ++i)
4515 if (tpd.parms[i] == 0)
4516 {
4517 /* One of the template parms was not used in a deduced context in the
4518 specialization. */
4519 if (!did_error_intro)
4520 {
4521 error ("template parameters not deducible in "
4522 "partial specialization:");
4523 did_error_intro = true;
4524 }
4525
4526 inform (input_location, " %qD",
4527 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4528 }
4529
4530 if (did_error_intro)
4531 return error_mark_node;
4532
4533 /* [temp.class.spec]
4534
4535 The argument list of the specialization shall not be identical to
4536 the implicit argument list of the primary template. */
4537 tree main_args
4538 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4539 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4540 && (!flag_concepts
4541 || !strictly_subsumes (current_template_constraints (),
4542 get_constraints (maintmpl))))
4543 {
4544 if (!flag_concepts)
4545 error ("partial specialization %q+D does not specialize "
4546 "any template arguments", decl);
4547 else
4548 error ("partial specialization %q+D does not specialize any "
4549 "template arguments and is not more constrained than", decl);
4550 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4551 }
4552
4553 /* A partial specialization that replaces multiple parameters of the
4554 primary template with a pack expansion is less specialized for those
4555 parameters. */
4556 if (nargs < DECL_NTPARMS (maintmpl))
4557 {
4558 error ("partial specialization is not more specialized than the "
4559 "primary template because it replaces multiple parameters "
4560 "with a pack expansion");
4561 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4562 return decl;
4563 }
4564
4565 /* [temp.class.spec]
4566
4567 A partially specialized non-type argument expression shall not
4568 involve template parameters of the partial specialization except
4569 when the argument expression is a simple identifier.
4570
4571 The type of a template parameter corresponding to a specialized
4572 non-type argument shall not be dependent on a parameter of the
4573 specialization.
4574
4575 Also, we verify that pack expansions only occur at the
4576 end of the argument list. */
4577 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4578 tpd2.parms = 0;
4579 for (i = 0; i < nargs; ++i)
4580 {
4581 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4582 tree arg = TREE_VEC_ELT (inner_args, i);
4583 tree packed_args = NULL_TREE;
4584 int j, len = 1;
4585
4586 if (ARGUMENT_PACK_P (arg))
4587 {
4588 /* Extract the arguments from the argument pack. We'll be
4589 iterating over these in the following loop. */
4590 packed_args = ARGUMENT_PACK_ARGS (arg);
4591 len = TREE_VEC_LENGTH (packed_args);
4592 }
4593
4594 for (j = 0; j < len; j++)
4595 {
4596 if (packed_args)
4597 /* Get the Jth argument in the parameter pack. */
4598 arg = TREE_VEC_ELT (packed_args, j);
4599
4600 if (PACK_EXPANSION_P (arg))
4601 {
4602 /* Pack expansions must come at the end of the
4603 argument list. */
4604 if ((packed_args && j < len - 1)
4605 || (!packed_args && i < nargs - 1))
4606 {
4607 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4608 error ("parameter pack argument %qE must be at the "
4609 "end of the template argument list", arg);
4610 else
4611 error ("parameter pack argument %qT must be at the "
4612 "end of the template argument list", arg);
4613 }
4614 }
4615
4616 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4617 /* We only care about the pattern. */
4618 arg = PACK_EXPANSION_PATTERN (arg);
4619
4620 if (/* These first two lines are the `non-type' bit. */
4621 !TYPE_P (arg)
4622 && TREE_CODE (arg) != TEMPLATE_DECL
4623 /* This next two lines are the `argument expression is not just a
4624 simple identifier' condition and also the `specialized
4625 non-type argument' bit. */
4626 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4627 && !(REFERENCE_REF_P (arg)
4628 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4629 {
4630 if ((!packed_args && tpd.arg_uses_template_parms[i])
4631 || (packed_args && uses_template_parms (arg)))
4632 error ("template argument %qE involves template parameter(s)",
4633 arg);
4634 else
4635 {
4636 /* Look at the corresponding template parameter,
4637 marking which template parameters its type depends
4638 upon. */
4639 tree type = TREE_TYPE (parm);
4640
4641 if (!tpd2.parms)
4642 {
4643 /* We haven't yet initialized TPD2. Do so now. */
4644 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4645 /* The number of parameters here is the number in the
4646 main template, which, as checked in the assertion
4647 above, is NARGS. */
4648 tpd2.parms = XALLOCAVEC (int, nargs);
4649 tpd2.level =
4650 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4651 }
4652
4653 /* Mark the template parameters. But this time, we're
4654 looking for the template parameters of the main
4655 template, not in the specialization. */
4656 tpd2.current_arg = i;
4657 tpd2.arg_uses_template_parms[i] = 0;
4658 memset (tpd2.parms, 0, sizeof (int) * nargs);
4659 for_each_template_parm (type,
4660 &mark_template_parm,
4661 &tpd2,
4662 NULL,
4663 /*include_nondeduced_p=*/false);
4664
4665 if (tpd2.arg_uses_template_parms [i])
4666 {
4667 /* The type depended on some template parameters.
4668 If they are fully specialized in the
4669 specialization, that's OK. */
4670 int j;
4671 int count = 0;
4672 for (j = 0; j < nargs; ++j)
4673 if (tpd2.parms[j] != 0
4674 && tpd.arg_uses_template_parms [j])
4675 ++count;
4676 if (count != 0)
4677 error_n (input_location, count,
4678 "type %qT of template argument %qE depends "
4679 "on a template parameter",
4680 "type %qT of template argument %qE depends "
4681 "on template parameters",
4682 type,
4683 arg);
4684 }
4685 }
4686 }
4687 }
4688 }
4689
4690 /* We should only get here once. */
4691 if (TREE_CODE (decl) == TYPE_DECL)
4692 gcc_assert (!COMPLETE_TYPE_P (type));
4693
4694 // Build the template decl.
4695 tree tmpl = build_template_decl (decl, current_template_parms,
4696 DECL_MEMBER_TEMPLATE_P (maintmpl));
4697 TREE_TYPE (tmpl) = type;
4698 DECL_TEMPLATE_RESULT (tmpl) = decl;
4699 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4700 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4701 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4702
4703 if (VAR_P (decl))
4704 /* We didn't register this in check_explicit_specialization so we could
4705 wait until the constraints were set. */
4706 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4707 else
4708 associate_classtype_constraints (type);
4709
4710 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4711 = tree_cons (specargs, tmpl,
4712 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4713 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4714
4715 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4716 inst = TREE_CHAIN (inst))
4717 {
4718 tree instance = TREE_VALUE (inst);
4719 if (TYPE_P (instance)
4720 ? (COMPLETE_TYPE_P (instance)
4721 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4722 : DECL_TEMPLATE_INSTANTIATION (instance))
4723 {
4724 tree spec = most_specialized_partial_spec (instance, tf_none);
4725 tree inst_decl = (DECL_P (instance)
4726 ? instance : TYPE_NAME (instance));
4727 if (!spec)
4728 /* OK */;
4729 else if (spec == error_mark_node)
4730 permerror (input_location,
4731 "declaration of %qD ambiguates earlier template "
4732 "instantiation for %qD", decl, inst_decl);
4733 else if (TREE_VALUE (spec) == tmpl)
4734 permerror (input_location,
4735 "partial specialization of %qD after instantiation "
4736 "of %qD", decl, inst_decl);
4737 }
4738 }
4739
4740 return decl;
4741 }
4742
4743 /* PARM is a template parameter of some form; return the corresponding
4744 TEMPLATE_PARM_INDEX. */
4745
4746 static tree
4747 get_template_parm_index (tree parm)
4748 {
4749 if (TREE_CODE (parm) == PARM_DECL
4750 || TREE_CODE (parm) == CONST_DECL)
4751 parm = DECL_INITIAL (parm);
4752 else if (TREE_CODE (parm) == TYPE_DECL
4753 || TREE_CODE (parm) == TEMPLATE_DECL)
4754 parm = TREE_TYPE (parm);
4755 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4756 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4757 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4758 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4759 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4760 return parm;
4761 }
4762
4763 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4764 parameter packs used by the template parameter PARM. */
4765
4766 static void
4767 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4768 {
4769 /* A type parm can't refer to another parm. */
4770 if (TREE_CODE (parm) == TYPE_DECL)
4771 return;
4772 else if (TREE_CODE (parm) == PARM_DECL)
4773 {
4774 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4775 ppd, ppd->visited);
4776 return;
4777 }
4778
4779 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4780
4781 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4782 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4783 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4784 }
4785
4786 /* PARM is a template parameter pack. Return any parameter packs used in
4787 its type or the type of any of its template parameters. If there are
4788 any such packs, it will be instantiated into a fixed template parameter
4789 list by partial instantiation rather than be fully deduced. */
4790
4791 tree
4792 fixed_parameter_pack_p (tree parm)
4793 {
4794 /* This can only be true in a member template. */
4795 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4796 return NULL_TREE;
4797 /* This can only be true for a parameter pack. */
4798 if (!template_parameter_pack_p (parm))
4799 return NULL_TREE;
4800 /* A type parm can't refer to another parm. */
4801 if (TREE_CODE (parm) == TYPE_DECL)
4802 return NULL_TREE;
4803
4804 tree parameter_packs = NULL_TREE;
4805 struct find_parameter_pack_data ppd;
4806 ppd.parameter_packs = &parameter_packs;
4807 ppd.visited = new hash_set<tree>;
4808 ppd.type_pack_expansion_p = false;
4809
4810 fixed_parameter_pack_p_1 (parm, &ppd);
4811
4812 delete ppd.visited;
4813 return parameter_packs;
4814 }
4815
4816 /* Check that a template declaration's use of default arguments and
4817 parameter packs is not invalid. Here, PARMS are the template
4818 parameters. IS_PRIMARY is true if DECL is the thing declared by
4819 a primary template. IS_PARTIAL is true if DECL is a partial
4820 specialization.
4821
4822 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4823 declaration (but not a definition); 1 indicates a declaration, 2
4824 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4825 emitted for extraneous default arguments.
4826
4827 Returns TRUE if there were no errors found, FALSE otherwise. */
4828
4829 bool
4830 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4831 bool is_partial, int is_friend_decl)
4832 {
4833 const char *msg;
4834 int last_level_to_check;
4835 tree parm_level;
4836 bool no_errors = true;
4837
4838 /* [temp.param]
4839
4840 A default template-argument shall not be specified in a
4841 function template declaration or a function template definition, nor
4842 in the template-parameter-list of the definition of a member of a
4843 class template. */
4844
4845 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4846 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4847 /* You can't have a function template declaration in a local
4848 scope, nor you can you define a member of a class template in a
4849 local scope. */
4850 return true;
4851
4852 if ((TREE_CODE (decl) == TYPE_DECL
4853 && TREE_TYPE (decl)
4854 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4855 || (TREE_CODE (decl) == FUNCTION_DECL
4856 && LAMBDA_FUNCTION_P (decl)))
4857 /* A lambda doesn't have an explicit declaration; don't complain
4858 about the parms of the enclosing class. */
4859 return true;
4860
4861 if (current_class_type
4862 && !TYPE_BEING_DEFINED (current_class_type)
4863 && DECL_LANG_SPECIFIC (decl)
4864 && DECL_DECLARES_FUNCTION_P (decl)
4865 /* If this is either a friend defined in the scope of the class
4866 or a member function. */
4867 && (DECL_FUNCTION_MEMBER_P (decl)
4868 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4869 : DECL_FRIEND_CONTEXT (decl)
4870 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4871 : false)
4872 /* And, if it was a member function, it really was defined in
4873 the scope of the class. */
4874 && (!DECL_FUNCTION_MEMBER_P (decl)
4875 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4876 /* We already checked these parameters when the template was
4877 declared, so there's no need to do it again now. This function
4878 was defined in class scope, but we're processing its body now
4879 that the class is complete. */
4880 return true;
4881
4882 /* Core issue 226 (C++0x only): the following only applies to class
4883 templates. */
4884 if (is_primary
4885 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4886 {
4887 /* [temp.param]
4888
4889 If a template-parameter has a default template-argument, all
4890 subsequent template-parameters shall have a default
4891 template-argument supplied. */
4892 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4893 {
4894 tree inner_parms = TREE_VALUE (parm_level);
4895 int ntparms = TREE_VEC_LENGTH (inner_parms);
4896 int seen_def_arg_p = 0;
4897 int i;
4898
4899 for (i = 0; i < ntparms; ++i)
4900 {
4901 tree parm = TREE_VEC_ELT (inner_parms, i);
4902
4903 if (parm == error_mark_node)
4904 continue;
4905
4906 if (TREE_PURPOSE (parm))
4907 seen_def_arg_p = 1;
4908 else if (seen_def_arg_p
4909 && !template_parameter_pack_p (TREE_VALUE (parm)))
4910 {
4911 error ("no default argument for %qD", TREE_VALUE (parm));
4912 /* For better subsequent error-recovery, we indicate that
4913 there should have been a default argument. */
4914 TREE_PURPOSE (parm) = error_mark_node;
4915 no_errors = false;
4916 }
4917 else if (!is_partial
4918 && !is_friend_decl
4919 /* Don't complain about an enclosing partial
4920 specialization. */
4921 && parm_level == parms
4922 && TREE_CODE (decl) == TYPE_DECL
4923 && i < ntparms - 1
4924 && template_parameter_pack_p (TREE_VALUE (parm))
4925 /* A fixed parameter pack will be partially
4926 instantiated into a fixed length list. */
4927 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4928 {
4929 /* A primary class template can only have one
4930 parameter pack, at the end of the template
4931 parameter list. */
4932
4933 error ("parameter pack %q+D must be at the end of the"
4934 " template parameter list", TREE_VALUE (parm));
4935
4936 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4937 = error_mark_node;
4938 no_errors = false;
4939 }
4940 }
4941 }
4942 }
4943
4944 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4945 || is_partial
4946 || !is_primary
4947 || is_friend_decl)
4948 /* For an ordinary class template, default template arguments are
4949 allowed at the innermost level, e.g.:
4950 template <class T = int>
4951 struct S {};
4952 but, in a partial specialization, they're not allowed even
4953 there, as we have in [temp.class.spec]:
4954
4955 The template parameter list of a specialization shall not
4956 contain default template argument values.
4957
4958 So, for a partial specialization, or for a function template
4959 (in C++98/C++03), we look at all of them. */
4960 ;
4961 else
4962 /* But, for a primary class template that is not a partial
4963 specialization we look at all template parameters except the
4964 innermost ones. */
4965 parms = TREE_CHAIN (parms);
4966
4967 /* Figure out what error message to issue. */
4968 if (is_friend_decl == 2)
4969 msg = G_("default template arguments may not be used in function template "
4970 "friend re-declaration");
4971 else if (is_friend_decl)
4972 msg = G_("default template arguments may not be used in function template "
4973 "friend declarations");
4974 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4975 msg = G_("default template arguments may not be used in function templates "
4976 "without -std=c++11 or -std=gnu++11");
4977 else if (is_partial)
4978 msg = G_("default template arguments may not be used in "
4979 "partial specializations");
4980 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
4981 msg = G_("default argument for template parameter for class enclosing %qD");
4982 else
4983 /* Per [temp.param]/9, "A default template-argument shall not be
4984 specified in the template-parameter-lists of the definition of
4985 a member of a class template that appears outside of the member's
4986 class.", thus if we aren't handling a member of a class template
4987 there is no need to examine the parameters. */
4988 return true;
4989
4990 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4991 /* If we're inside a class definition, there's no need to
4992 examine the parameters to the class itself. On the one
4993 hand, they will be checked when the class is defined, and,
4994 on the other, default arguments are valid in things like:
4995 template <class T = double>
4996 struct S { template <class U> void f(U); };
4997 Here the default argument for `S' has no bearing on the
4998 declaration of `f'. */
4999 last_level_to_check = template_class_depth (current_class_type) + 1;
5000 else
5001 /* Check everything. */
5002 last_level_to_check = 0;
5003
5004 for (parm_level = parms;
5005 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5006 parm_level = TREE_CHAIN (parm_level))
5007 {
5008 tree inner_parms = TREE_VALUE (parm_level);
5009 int i;
5010 int ntparms;
5011
5012 ntparms = TREE_VEC_LENGTH (inner_parms);
5013 for (i = 0; i < ntparms; ++i)
5014 {
5015 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5016 continue;
5017
5018 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5019 {
5020 if (msg)
5021 {
5022 no_errors = false;
5023 if (is_friend_decl == 2)
5024 return no_errors;
5025
5026 error (msg, decl);
5027 msg = 0;
5028 }
5029
5030 /* Clear out the default argument so that we are not
5031 confused later. */
5032 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5033 }
5034 }
5035
5036 /* At this point, if we're still interested in issuing messages,
5037 they must apply to classes surrounding the object declared. */
5038 if (msg)
5039 msg = G_("default argument for template parameter for class "
5040 "enclosing %qD");
5041 }
5042
5043 return no_errors;
5044 }
5045
5046 /* Worker for push_template_decl_real, called via
5047 for_each_template_parm. DATA is really an int, indicating the
5048 level of the parameters we are interested in. If T is a template
5049 parameter of that level, return nonzero. */
5050
5051 static int
5052 template_parm_this_level_p (tree t, void* data)
5053 {
5054 int this_level = *(int *)data;
5055 int level;
5056
5057 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5058 level = TEMPLATE_PARM_LEVEL (t);
5059 else
5060 level = TEMPLATE_TYPE_LEVEL (t);
5061 return level == this_level;
5062 }
5063
5064 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5065 DATA is really an int, indicating the innermost outer level of parameters.
5066 If T is a template parameter of that level or further out, return
5067 nonzero. */
5068
5069 static int
5070 template_parm_outer_level (tree t, void *data)
5071 {
5072 int this_level = *(int *)data;
5073 int level;
5074
5075 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5076 level = TEMPLATE_PARM_LEVEL (t);
5077 else
5078 level = TEMPLATE_TYPE_LEVEL (t);
5079 return level <= this_level;
5080 }
5081
5082 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5083 parameters given by current_template_args, or reuses a
5084 previously existing one, if appropriate. Returns the DECL, or an
5085 equivalent one, if it is replaced via a call to duplicate_decls.
5086
5087 If IS_FRIEND is true, DECL is a friend declaration. */
5088
5089 tree
5090 push_template_decl_real (tree decl, bool is_friend)
5091 {
5092 tree tmpl;
5093 tree args;
5094 tree info;
5095 tree ctx;
5096 bool is_primary;
5097 bool is_partial;
5098 int new_template_p = 0;
5099 /* True if the template is a member template, in the sense of
5100 [temp.mem]. */
5101 bool member_template_p = false;
5102
5103 if (decl == error_mark_node || !current_template_parms)
5104 return error_mark_node;
5105
5106 /* See if this is a partial specialization. */
5107 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5108 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5109 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5110 || (VAR_P (decl)
5111 && DECL_LANG_SPECIFIC (decl)
5112 && DECL_TEMPLATE_SPECIALIZATION (decl)
5113 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5114
5115 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5116 is_friend = true;
5117
5118 if (is_friend)
5119 /* For a friend, we want the context of the friend function, not
5120 the type of which it is a friend. */
5121 ctx = CP_DECL_CONTEXT (decl);
5122 else if (CP_DECL_CONTEXT (decl)
5123 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5124 /* In the case of a virtual function, we want the class in which
5125 it is defined. */
5126 ctx = CP_DECL_CONTEXT (decl);
5127 else
5128 /* Otherwise, if we're currently defining some class, the DECL
5129 is assumed to be a member of the class. */
5130 ctx = current_scope ();
5131
5132 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5133 ctx = NULL_TREE;
5134
5135 if (!DECL_CONTEXT (decl))
5136 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5137
5138 /* See if this is a primary template. */
5139 if (is_friend && ctx
5140 && uses_template_parms_level (ctx, processing_template_decl))
5141 /* A friend template that specifies a class context, i.e.
5142 template <typename T> friend void A<T>::f();
5143 is not primary. */
5144 is_primary = false;
5145 else if (TREE_CODE (decl) == TYPE_DECL
5146 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5147 is_primary = false;
5148 else
5149 is_primary = template_parm_scope_p ();
5150
5151 if (is_primary)
5152 {
5153 warning (OPT_Wtemplates, "template %qD declared", decl);
5154
5155 if (DECL_CLASS_SCOPE_P (decl))
5156 member_template_p = true;
5157 if (TREE_CODE (decl) == TYPE_DECL
5158 && anon_aggrname_p (DECL_NAME (decl)))
5159 {
5160 error ("template class without a name");
5161 return error_mark_node;
5162 }
5163 else if (TREE_CODE (decl) == FUNCTION_DECL)
5164 {
5165 if (member_template_p)
5166 {
5167 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5168 error ("member template %qD may not have virt-specifiers", decl);
5169 }
5170 if (DECL_DESTRUCTOR_P (decl))
5171 {
5172 /* [temp.mem]
5173
5174 A destructor shall not be a member template. */
5175 error ("destructor %qD declared as member template", decl);
5176 return error_mark_node;
5177 }
5178 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5179 && (!prototype_p (TREE_TYPE (decl))
5180 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5181 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5182 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5183 == void_list_node)))
5184 {
5185 /* [basic.stc.dynamic.allocation]
5186
5187 An allocation function can be a function
5188 template. ... Template allocation functions shall
5189 have two or more parameters. */
5190 error ("invalid template declaration of %qD", decl);
5191 return error_mark_node;
5192 }
5193 }
5194 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5195 && CLASS_TYPE_P (TREE_TYPE (decl)))
5196 /* OK */;
5197 else if (TREE_CODE (decl) == TYPE_DECL
5198 && TYPE_DECL_ALIAS_P (decl))
5199 /* alias-declaration */
5200 gcc_assert (!DECL_ARTIFICIAL (decl));
5201 else if (VAR_P (decl))
5202 /* C++14 variable template. */;
5203 else
5204 {
5205 error ("template declaration of %q#D", decl);
5206 return error_mark_node;
5207 }
5208 }
5209
5210 /* Check to see that the rules regarding the use of default
5211 arguments are not being violated. */
5212 check_default_tmpl_args (decl, current_template_parms,
5213 is_primary, is_partial, /*is_friend_decl=*/0);
5214
5215 /* Ensure that there are no parameter packs in the type of this
5216 declaration that have not been expanded. */
5217 if (TREE_CODE (decl) == FUNCTION_DECL)
5218 {
5219 /* Check each of the arguments individually to see if there are
5220 any bare parameter packs. */
5221 tree type = TREE_TYPE (decl);
5222 tree arg = DECL_ARGUMENTS (decl);
5223 tree argtype = TYPE_ARG_TYPES (type);
5224
5225 while (arg && argtype)
5226 {
5227 if (!DECL_PACK_P (arg)
5228 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5229 {
5230 /* This is a PARM_DECL that contains unexpanded parameter
5231 packs. We have already complained about this in the
5232 check_for_bare_parameter_packs call, so just replace
5233 these types with ERROR_MARK_NODE. */
5234 TREE_TYPE (arg) = error_mark_node;
5235 TREE_VALUE (argtype) = error_mark_node;
5236 }
5237
5238 arg = DECL_CHAIN (arg);
5239 argtype = TREE_CHAIN (argtype);
5240 }
5241
5242 /* Check for bare parameter packs in the return type and the
5243 exception specifiers. */
5244 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5245 /* Errors were already issued, set return type to int
5246 as the frontend doesn't expect error_mark_node as
5247 the return type. */
5248 TREE_TYPE (type) = integer_type_node;
5249 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5250 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5251 }
5252 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5253 && TYPE_DECL_ALIAS_P (decl))
5254 ? DECL_ORIGINAL_TYPE (decl)
5255 : TREE_TYPE (decl)))
5256 {
5257 TREE_TYPE (decl) = error_mark_node;
5258 return error_mark_node;
5259 }
5260
5261 if (is_partial)
5262 return process_partial_specialization (decl);
5263
5264 args = current_template_args ();
5265
5266 if (!ctx
5267 || TREE_CODE (ctx) == FUNCTION_DECL
5268 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5269 || (TREE_CODE (decl) == TYPE_DECL
5270 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5271 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5272 {
5273 if (DECL_LANG_SPECIFIC (decl)
5274 && DECL_TEMPLATE_INFO (decl)
5275 && DECL_TI_TEMPLATE (decl))
5276 tmpl = DECL_TI_TEMPLATE (decl);
5277 /* If DECL is a TYPE_DECL for a class-template, then there won't
5278 be DECL_LANG_SPECIFIC. The information equivalent to
5279 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5280 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5281 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5282 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5283 {
5284 /* Since a template declaration already existed for this
5285 class-type, we must be redeclaring it here. Make sure
5286 that the redeclaration is valid. */
5287 redeclare_class_template (TREE_TYPE (decl),
5288 current_template_parms,
5289 current_template_constraints ());
5290 /* We don't need to create a new TEMPLATE_DECL; just use the
5291 one we already had. */
5292 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5293 }
5294 else
5295 {
5296 tmpl = build_template_decl (decl, current_template_parms,
5297 member_template_p);
5298 new_template_p = 1;
5299
5300 if (DECL_LANG_SPECIFIC (decl)
5301 && DECL_TEMPLATE_SPECIALIZATION (decl))
5302 {
5303 /* A specialization of a member template of a template
5304 class. */
5305 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5306 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5307 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5308 }
5309 }
5310 }
5311 else
5312 {
5313 tree a, t, current, parms;
5314 int i;
5315 tree tinfo = get_template_info (decl);
5316
5317 if (!tinfo)
5318 {
5319 error ("template definition of non-template %q#D", decl);
5320 return error_mark_node;
5321 }
5322
5323 tmpl = TI_TEMPLATE (tinfo);
5324
5325 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5326 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5327 && DECL_TEMPLATE_SPECIALIZATION (decl)
5328 && DECL_MEMBER_TEMPLATE_P (tmpl))
5329 {
5330 tree new_tmpl;
5331
5332 /* The declaration is a specialization of a member
5333 template, declared outside the class. Therefore, the
5334 innermost template arguments will be NULL, so we
5335 replace them with the arguments determined by the
5336 earlier call to check_explicit_specialization. */
5337 args = DECL_TI_ARGS (decl);
5338
5339 new_tmpl
5340 = build_template_decl (decl, current_template_parms,
5341 member_template_p);
5342 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5343 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5344 DECL_TI_TEMPLATE (decl) = new_tmpl;
5345 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5346 DECL_TEMPLATE_INFO (new_tmpl)
5347 = build_template_info (tmpl, args);
5348
5349 register_specialization (new_tmpl,
5350 most_general_template (tmpl),
5351 args,
5352 is_friend, 0);
5353 return decl;
5354 }
5355
5356 /* Make sure the template headers we got make sense. */
5357
5358 parms = DECL_TEMPLATE_PARMS (tmpl);
5359 i = TMPL_PARMS_DEPTH (parms);
5360 if (TMPL_ARGS_DEPTH (args) != i)
5361 {
5362 error ("expected %d levels of template parms for %q#D, got %d",
5363 i, decl, TMPL_ARGS_DEPTH (args));
5364 DECL_INTERFACE_KNOWN (decl) = 1;
5365 return error_mark_node;
5366 }
5367 else
5368 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5369 {
5370 a = TMPL_ARGS_LEVEL (args, i);
5371 t = INNERMOST_TEMPLATE_PARMS (parms);
5372
5373 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5374 {
5375 if (current == decl)
5376 error ("got %d template parameters for %q#D",
5377 TREE_VEC_LENGTH (a), decl);
5378 else
5379 error ("got %d template parameters for %q#T",
5380 TREE_VEC_LENGTH (a), current);
5381 error (" but %d required", TREE_VEC_LENGTH (t));
5382 /* Avoid crash in import_export_decl. */
5383 DECL_INTERFACE_KNOWN (decl) = 1;
5384 return error_mark_node;
5385 }
5386
5387 if (current == decl)
5388 current = ctx;
5389 else if (current == NULL_TREE)
5390 /* Can happen in erroneous input. */
5391 break;
5392 else
5393 current = get_containing_scope (current);
5394 }
5395
5396 /* Check that the parms are used in the appropriate qualifying scopes
5397 in the declarator. */
5398 if (!comp_template_args
5399 (TI_ARGS (tinfo),
5400 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5401 {
5402 error ("\
5403 template arguments to %qD do not match original template %qD",
5404 decl, DECL_TEMPLATE_RESULT (tmpl));
5405 if (!uses_template_parms (TI_ARGS (tinfo)))
5406 inform (input_location, "use template<> for an explicit specialization");
5407 /* Avoid crash in import_export_decl. */
5408 DECL_INTERFACE_KNOWN (decl) = 1;
5409 return error_mark_node;
5410 }
5411 }
5412
5413 DECL_TEMPLATE_RESULT (tmpl) = decl;
5414 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5415
5416 /* Push template declarations for global functions and types. Note
5417 that we do not try to push a global template friend declared in a
5418 template class; such a thing may well depend on the template
5419 parameters of the class. */
5420 if (new_template_p && !ctx
5421 && !(is_friend && template_class_depth (current_class_type) > 0))
5422 {
5423 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5424 if (tmpl == error_mark_node)
5425 return error_mark_node;
5426
5427 /* Hide template friend classes that haven't been declared yet. */
5428 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5429 {
5430 DECL_ANTICIPATED (tmpl) = 1;
5431 DECL_FRIEND_P (tmpl) = 1;
5432 }
5433 }
5434
5435 if (is_primary)
5436 {
5437 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5438 int i;
5439
5440 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5441 if (DECL_CONV_FN_P (tmpl))
5442 {
5443 int depth = TMPL_PARMS_DEPTH (parms);
5444
5445 /* It is a conversion operator. See if the type converted to
5446 depends on innermost template operands. */
5447
5448 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5449 depth))
5450 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5451 }
5452
5453 /* Give template template parms a DECL_CONTEXT of the template
5454 for which they are a parameter. */
5455 parms = INNERMOST_TEMPLATE_PARMS (parms);
5456 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5457 {
5458 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5459 if (TREE_CODE (parm) == TEMPLATE_DECL)
5460 DECL_CONTEXT (parm) = tmpl;
5461 }
5462
5463 if (TREE_CODE (decl) == TYPE_DECL
5464 && TYPE_DECL_ALIAS_P (decl)
5465 && complex_alias_template_p (tmpl))
5466 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5467 }
5468
5469 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5470 back to its most general template. If TMPL is a specialization,
5471 ARGS may only have the innermost set of arguments. Add the missing
5472 argument levels if necessary. */
5473 if (DECL_TEMPLATE_INFO (tmpl))
5474 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5475
5476 info = build_template_info (tmpl, args);
5477
5478 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5479 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5480 else
5481 {
5482 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5483 retrofit_lang_decl (decl);
5484 if (DECL_LANG_SPECIFIC (decl))
5485 DECL_TEMPLATE_INFO (decl) = info;
5486 }
5487
5488 if (flag_implicit_templates
5489 && !is_friend
5490 && TREE_PUBLIC (decl)
5491 && VAR_OR_FUNCTION_DECL_P (decl))
5492 /* Set DECL_COMDAT on template instantiations; if we force
5493 them to be emitted by explicit instantiation or -frepo,
5494 mark_needed will tell cgraph to do the right thing. */
5495 DECL_COMDAT (decl) = true;
5496
5497 return DECL_TEMPLATE_RESULT (tmpl);
5498 }
5499
5500 tree
5501 push_template_decl (tree decl)
5502 {
5503 return push_template_decl_real (decl, false);
5504 }
5505
5506 /* FN is an inheriting constructor that inherits from the constructor
5507 template INHERITED; turn FN into a constructor template with a matching
5508 template header. */
5509
5510 tree
5511 add_inherited_template_parms (tree fn, tree inherited)
5512 {
5513 tree inner_parms
5514 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5515 inner_parms = copy_node (inner_parms);
5516 tree parms
5517 = tree_cons (size_int (processing_template_decl + 1),
5518 inner_parms, current_template_parms);
5519 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5520 tree args = template_parms_to_args (parms);
5521 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5522 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5523 DECL_TEMPLATE_RESULT (tmpl) = fn;
5524 DECL_ARTIFICIAL (tmpl) = true;
5525 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5526 return tmpl;
5527 }
5528
5529 /* Called when a class template TYPE is redeclared with the indicated
5530 template PARMS, e.g.:
5531
5532 template <class T> struct S;
5533 template <class T> struct S {}; */
5534
5535 bool
5536 redeclare_class_template (tree type, tree parms, tree cons)
5537 {
5538 tree tmpl;
5539 tree tmpl_parms;
5540 int i;
5541
5542 if (!TYPE_TEMPLATE_INFO (type))
5543 {
5544 error ("%qT is not a template type", type);
5545 return false;
5546 }
5547
5548 tmpl = TYPE_TI_TEMPLATE (type);
5549 if (!PRIMARY_TEMPLATE_P (tmpl))
5550 /* The type is nested in some template class. Nothing to worry
5551 about here; there are no new template parameters for the nested
5552 type. */
5553 return true;
5554
5555 if (!parms)
5556 {
5557 error ("template specifiers not specified in declaration of %qD",
5558 tmpl);
5559 return false;
5560 }
5561
5562 parms = INNERMOST_TEMPLATE_PARMS (parms);
5563 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5564
5565 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5566 {
5567 error_n (input_location, TREE_VEC_LENGTH (parms),
5568 "redeclared with %d template parameter",
5569 "redeclared with %d template parameters",
5570 TREE_VEC_LENGTH (parms));
5571 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5572 "previous declaration %qD used %d template parameter",
5573 "previous declaration %qD used %d template parameters",
5574 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5575 return false;
5576 }
5577
5578 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5579 {
5580 tree tmpl_parm;
5581 tree parm;
5582 tree tmpl_default;
5583 tree parm_default;
5584
5585 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5586 || TREE_VEC_ELT (parms, i) == error_mark_node)
5587 continue;
5588
5589 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5590 if (error_operand_p (tmpl_parm))
5591 return false;
5592
5593 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5594 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5595 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5596
5597 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5598 TEMPLATE_DECL. */
5599 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5600 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5601 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5602 || (TREE_CODE (tmpl_parm) != PARM_DECL
5603 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5604 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5605 || (TREE_CODE (tmpl_parm) == PARM_DECL
5606 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5607 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5608 {
5609 error ("template parameter %q+#D", tmpl_parm);
5610 error ("redeclared here as %q#D", parm);
5611 return false;
5612 }
5613
5614 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5615 {
5616 /* We have in [temp.param]:
5617
5618 A template-parameter may not be given default arguments
5619 by two different declarations in the same scope. */
5620 error_at (input_location, "redefinition of default argument for %q#D", parm);
5621 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5622 "original definition appeared here");
5623 return false;
5624 }
5625
5626 if (parm_default != NULL_TREE)
5627 /* Update the previous template parameters (which are the ones
5628 that will really count) with the new default value. */
5629 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5630 else if (tmpl_default != NULL_TREE)
5631 /* Update the new parameters, too; they'll be used as the
5632 parameters for any members. */
5633 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5634
5635 /* Give each template template parm in this redeclaration a
5636 DECL_CONTEXT of the template for which they are a parameter. */
5637 if (TREE_CODE (parm) == TEMPLATE_DECL)
5638 {
5639 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5640 DECL_CONTEXT (parm) = tmpl;
5641 }
5642 }
5643
5644 // Cannot redeclare a class template with a different set of constraints.
5645 if (!equivalent_constraints (get_constraints (tmpl), cons))
5646 {
5647 error_at (input_location, "redeclaration %q#D with different "
5648 "constraints", tmpl);
5649 inform (DECL_SOURCE_LOCATION (tmpl),
5650 "original declaration appeared here");
5651 }
5652
5653 return true;
5654 }
5655
5656 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5657 to be used when the caller has already checked
5658 (processing_template_decl
5659 && !instantiation_dependent_expression_p (expr)
5660 && potential_constant_expression (expr))
5661 and cleared processing_template_decl. */
5662
5663 tree
5664 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5665 {
5666 return tsubst_copy_and_build (expr,
5667 /*args=*/NULL_TREE,
5668 complain,
5669 /*in_decl=*/NULL_TREE,
5670 /*function_p=*/false,
5671 /*integral_constant_expression_p=*/true);
5672 }
5673
5674 /* Simplify EXPR if it is a non-dependent expression. Returns the
5675 (possibly simplified) expression. */
5676
5677 tree
5678 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5679 {
5680 if (expr == NULL_TREE)
5681 return NULL_TREE;
5682
5683 /* If we're in a template, but EXPR isn't value dependent, simplify
5684 it. We're supposed to treat:
5685
5686 template <typename T> void f(T[1 + 1]);
5687 template <typename T> void f(T[2]);
5688
5689 as two declarations of the same function, for example. */
5690 if (processing_template_decl
5691 && potential_nondependent_constant_expression (expr))
5692 {
5693 processing_template_decl_sentinel s;
5694 expr = instantiate_non_dependent_expr_internal (expr, complain);
5695 }
5696 return expr;
5697 }
5698
5699 tree
5700 instantiate_non_dependent_expr (tree expr)
5701 {
5702 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5703 }
5704
5705 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5706 an uninstantiated expression. */
5707
5708 tree
5709 instantiate_non_dependent_or_null (tree expr)
5710 {
5711 if (expr == NULL_TREE)
5712 return NULL_TREE;
5713 if (processing_template_decl)
5714 {
5715 if (!potential_nondependent_constant_expression (expr))
5716 expr = NULL_TREE;
5717 else
5718 {
5719 processing_template_decl_sentinel s;
5720 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5721 }
5722 }
5723 return expr;
5724 }
5725
5726 /* True iff T is a specialization of a variable template. */
5727
5728 bool
5729 variable_template_specialization_p (tree t)
5730 {
5731 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5732 return false;
5733 tree tmpl = DECL_TI_TEMPLATE (t);
5734 return variable_template_p (tmpl);
5735 }
5736
5737 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5738 template declaration, or a TYPE_DECL for an alias declaration. */
5739
5740 bool
5741 alias_type_or_template_p (tree t)
5742 {
5743 if (t == NULL_TREE)
5744 return false;
5745 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5746 || (TYPE_P (t)
5747 && TYPE_NAME (t)
5748 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5749 || DECL_ALIAS_TEMPLATE_P (t));
5750 }
5751
5752 /* Return TRUE iff T is a specialization of an alias template. */
5753
5754 bool
5755 alias_template_specialization_p (const_tree t)
5756 {
5757 /* It's an alias template specialization if it's an alias and its
5758 TYPE_NAME is a specialization of a primary template. */
5759 if (TYPE_ALIAS_P (t))
5760 {
5761 tree name = TYPE_NAME (t);
5762 if (DECL_LANG_SPECIFIC (name))
5763 if (tree ti = DECL_TEMPLATE_INFO (name))
5764 {
5765 tree tmpl = TI_TEMPLATE (ti);
5766 return PRIMARY_TEMPLATE_P (tmpl);
5767 }
5768 }
5769 return false;
5770 }
5771
5772 /* An alias template is complex from a SFINAE perspective if a template-id
5773 using that alias can be ill-formed when the expansion is not, as with
5774 the void_t template. We determine this by checking whether the
5775 expansion for the alias template uses all its template parameters. */
5776
5777 struct uses_all_template_parms_data
5778 {
5779 int level;
5780 bool *seen;
5781 };
5782
5783 static int
5784 uses_all_template_parms_r (tree t, void *data_)
5785 {
5786 struct uses_all_template_parms_data &data
5787 = *(struct uses_all_template_parms_data*)data_;
5788 tree idx = get_template_parm_index (t);
5789
5790 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5791 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5792 return 0;
5793 }
5794
5795 static bool
5796 complex_alias_template_p (const_tree tmpl)
5797 {
5798 struct uses_all_template_parms_data data;
5799 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5800 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5801 data.level = TMPL_PARMS_DEPTH (parms);
5802 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5803 data.seen = XALLOCAVEC (bool, len);
5804 for (int i = 0; i < len; ++i)
5805 data.seen[i] = false;
5806
5807 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5808 for (int i = 0; i < len; ++i)
5809 if (!data.seen[i])
5810 return true;
5811 return false;
5812 }
5813
5814 /* Return TRUE iff T is a specialization of a complex alias template with
5815 dependent template-arguments. */
5816
5817 bool
5818 dependent_alias_template_spec_p (const_tree t)
5819 {
5820 return (alias_template_specialization_p (t)
5821 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5822 && (any_dependent_template_arguments_p
5823 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5824 }
5825
5826 /* Return the number of innermost template parameters in TMPL. */
5827
5828 static int
5829 num_innermost_template_parms (tree tmpl)
5830 {
5831 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5832 return TREE_VEC_LENGTH (parms);
5833 }
5834
5835 /* Return either TMPL or another template that it is equivalent to under DR
5836 1286: An alias that just changes the name of a template is equivalent to
5837 the other template. */
5838
5839 static tree
5840 get_underlying_template (tree tmpl)
5841 {
5842 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5843 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5844 {
5845 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5846 if (TYPE_TEMPLATE_INFO (result))
5847 {
5848 tree sub = TYPE_TI_TEMPLATE (result);
5849 if (PRIMARY_TEMPLATE_P (sub)
5850 && (num_innermost_template_parms (tmpl)
5851 == num_innermost_template_parms (sub)))
5852 {
5853 tree alias_args = INNERMOST_TEMPLATE_ARGS
5854 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5855 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5856 break;
5857 /* The alias type is equivalent to the pattern of the
5858 underlying template, so strip the alias. */
5859 tmpl = sub;
5860 continue;
5861 }
5862 }
5863 break;
5864 }
5865 return tmpl;
5866 }
5867
5868 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5869 must be a function or a pointer-to-function type, as specified
5870 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5871 and check that the resulting function has external linkage. */
5872
5873 static tree
5874 convert_nontype_argument_function (tree type, tree expr,
5875 tsubst_flags_t complain)
5876 {
5877 tree fns = expr;
5878 tree fn, fn_no_ptr;
5879 linkage_kind linkage;
5880
5881 fn = instantiate_type (type, fns, tf_none);
5882 if (fn == error_mark_node)
5883 return error_mark_node;
5884
5885 fn_no_ptr = fn;
5886 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5887 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5888 if (BASELINK_P (fn_no_ptr))
5889 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5890
5891 /* [temp.arg.nontype]/1
5892
5893 A template-argument for a non-type, non-template template-parameter
5894 shall be one of:
5895 [...]
5896 -- the address of an object or function with external [C++11: or
5897 internal] linkage. */
5898
5899 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5900 {
5901 if (complain & tf_error)
5902 {
5903 error ("%qE is not a valid template argument for type %qT",
5904 expr, type);
5905 if (TYPE_PTR_P (type))
5906 error ("it must be the address of a function with "
5907 "external linkage");
5908 else
5909 error ("it must be the name of a function with "
5910 "external linkage");
5911 }
5912 return NULL_TREE;
5913 }
5914
5915 linkage = decl_linkage (fn_no_ptr);
5916 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5917 {
5918 if (complain & tf_error)
5919 {
5920 if (cxx_dialect >= cxx11)
5921 error ("%qE is not a valid template argument for type %qT "
5922 "because %qD has no linkage",
5923 expr, type, fn_no_ptr);
5924 else
5925 error ("%qE is not a valid template argument for type %qT "
5926 "because %qD does not have external linkage",
5927 expr, type, fn_no_ptr);
5928 }
5929 return NULL_TREE;
5930 }
5931
5932 return fn;
5933 }
5934
5935 /* Subroutine of convert_nontype_argument.
5936 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5937 Emit an error otherwise. */
5938
5939 static bool
5940 check_valid_ptrmem_cst_expr (tree type, tree expr,
5941 tsubst_flags_t complain)
5942 {
5943 STRIP_NOPS (expr);
5944 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5945 return true;
5946 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5947 return true;
5948 if (processing_template_decl
5949 && TREE_CODE (expr) == ADDR_EXPR
5950 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5951 return true;
5952 if (complain & tf_error)
5953 {
5954 error ("%qE is not a valid template argument for type %qT",
5955 expr, type);
5956 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5957 }
5958 return false;
5959 }
5960
5961 /* Returns TRUE iff the address of OP is value-dependent.
5962
5963 14.6.2.4 [temp.dep.temp]:
5964 A non-integral non-type template-argument is dependent if its type is
5965 dependent or it has either of the following forms
5966 qualified-id
5967 & qualified-id
5968 and contains a nested-name-specifier which specifies a class-name that
5969 names a dependent type.
5970
5971 We generalize this to just say that the address of a member of a
5972 dependent class is value-dependent; the above doesn't cover the
5973 address of a static data member named with an unqualified-id. */
5974
5975 static bool
5976 has_value_dependent_address (tree op)
5977 {
5978 /* We could use get_inner_reference here, but there's no need;
5979 this is only relevant for template non-type arguments, which
5980 can only be expressed as &id-expression. */
5981 if (DECL_P (op))
5982 {
5983 tree ctx = CP_DECL_CONTEXT (op);
5984 if (TYPE_P (ctx) && dependent_type_p (ctx))
5985 return true;
5986 }
5987
5988 return false;
5989 }
5990
5991 /* The next set of functions are used for providing helpful explanatory
5992 diagnostics for failed overload resolution. Their messages should be
5993 indented by two spaces for consistency with the messages in
5994 call.c */
5995
5996 static int
5997 unify_success (bool /*explain_p*/)
5998 {
5999 return 0;
6000 }
6001
6002 static int
6003 unify_parameter_deduction_failure (bool explain_p, tree parm)
6004 {
6005 if (explain_p)
6006 inform (input_location,
6007 " couldn't deduce template parameter %qD", parm);
6008 return 1;
6009 }
6010
6011 static int
6012 unify_invalid (bool /*explain_p*/)
6013 {
6014 return 1;
6015 }
6016
6017 static int
6018 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6019 {
6020 if (explain_p)
6021 inform (input_location,
6022 " types %qT and %qT have incompatible cv-qualifiers",
6023 parm, arg);
6024 return 1;
6025 }
6026
6027 static int
6028 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6029 {
6030 if (explain_p)
6031 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6032 return 1;
6033 }
6034
6035 static int
6036 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6037 {
6038 if (explain_p)
6039 inform (input_location,
6040 " template parameter %qD is not a parameter pack, but "
6041 "argument %qD is",
6042 parm, arg);
6043 return 1;
6044 }
6045
6046 static int
6047 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6048 {
6049 if (explain_p)
6050 inform (input_location,
6051 " template argument %qE does not match "
6052 "pointer-to-member constant %qE",
6053 arg, parm);
6054 return 1;
6055 }
6056
6057 static int
6058 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6059 {
6060 if (explain_p)
6061 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6062 return 1;
6063 }
6064
6065 static int
6066 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6067 {
6068 if (explain_p)
6069 inform (input_location,
6070 " inconsistent parameter pack deduction with %qT and %qT",
6071 old_arg, new_arg);
6072 return 1;
6073 }
6074
6075 static int
6076 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6077 {
6078 if (explain_p)
6079 {
6080 if (TYPE_P (parm))
6081 inform (input_location,
6082 " deduced conflicting types for parameter %qT (%qT and %qT)",
6083 parm, first, second);
6084 else
6085 inform (input_location,
6086 " deduced conflicting values for non-type parameter "
6087 "%qE (%qE and %qE)", parm, first, second);
6088 }
6089 return 1;
6090 }
6091
6092 static int
6093 unify_vla_arg (bool explain_p, tree arg)
6094 {
6095 if (explain_p)
6096 inform (input_location,
6097 " variable-sized array type %qT is not "
6098 "a valid template argument",
6099 arg);
6100 return 1;
6101 }
6102
6103 static int
6104 unify_method_type_error (bool explain_p, tree arg)
6105 {
6106 if (explain_p)
6107 inform (input_location,
6108 " member function type %qT is not a valid template argument",
6109 arg);
6110 return 1;
6111 }
6112
6113 static int
6114 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6115 {
6116 if (explain_p)
6117 {
6118 if (least_p)
6119 inform_n (input_location, wanted,
6120 " candidate expects at least %d argument, %d provided",
6121 " candidate expects at least %d arguments, %d provided",
6122 wanted, have);
6123 else
6124 inform_n (input_location, wanted,
6125 " candidate expects %d argument, %d provided",
6126 " candidate expects %d arguments, %d provided",
6127 wanted, have);
6128 }
6129 return 1;
6130 }
6131
6132 static int
6133 unify_too_many_arguments (bool explain_p, int have, int wanted)
6134 {
6135 return unify_arity (explain_p, have, wanted);
6136 }
6137
6138 static int
6139 unify_too_few_arguments (bool explain_p, int have, int wanted,
6140 bool least_p = false)
6141 {
6142 return unify_arity (explain_p, have, wanted, least_p);
6143 }
6144
6145 static int
6146 unify_arg_conversion (bool explain_p, tree to_type,
6147 tree from_type, tree arg)
6148 {
6149 if (explain_p)
6150 inform (EXPR_LOC_OR_LOC (arg, input_location),
6151 " cannot convert %qE (type %qT) to type %qT",
6152 arg, from_type, to_type);
6153 return 1;
6154 }
6155
6156 static int
6157 unify_no_common_base (bool explain_p, enum template_base_result r,
6158 tree parm, tree arg)
6159 {
6160 if (explain_p)
6161 switch (r)
6162 {
6163 case tbr_ambiguous_baseclass:
6164 inform (input_location, " %qT is an ambiguous base class of %qT",
6165 parm, arg);
6166 break;
6167 default:
6168 inform (input_location, " %qT is not derived from %qT", arg, parm);
6169 break;
6170 }
6171 return 1;
6172 }
6173
6174 static int
6175 unify_inconsistent_template_template_parameters (bool explain_p)
6176 {
6177 if (explain_p)
6178 inform (input_location,
6179 " template parameters of a template template argument are "
6180 "inconsistent with other deduced template arguments");
6181 return 1;
6182 }
6183
6184 static int
6185 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6186 {
6187 if (explain_p)
6188 inform (input_location,
6189 " can't deduce a template for %qT from non-template type %qT",
6190 parm, arg);
6191 return 1;
6192 }
6193
6194 static int
6195 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6196 {
6197 if (explain_p)
6198 inform (input_location,
6199 " template argument %qE does not match %qE", arg, parm);
6200 return 1;
6201 }
6202
6203 static int
6204 unify_overload_resolution_failure (bool explain_p, tree arg)
6205 {
6206 if (explain_p)
6207 inform (input_location,
6208 " could not resolve address from overloaded function %qE",
6209 arg);
6210 return 1;
6211 }
6212
6213 /* Attempt to convert the non-type template parameter EXPR to the
6214 indicated TYPE. If the conversion is successful, return the
6215 converted value. If the conversion is unsuccessful, return
6216 NULL_TREE if we issued an error message, or error_mark_node if we
6217 did not. We issue error messages for out-and-out bad template
6218 parameters, but not simply because the conversion failed, since we
6219 might be just trying to do argument deduction. Both TYPE and EXPR
6220 must be non-dependent.
6221
6222 The conversion follows the special rules described in
6223 [temp.arg.nontype], and it is much more strict than an implicit
6224 conversion.
6225
6226 This function is called twice for each template argument (see
6227 lookup_template_class for a more accurate description of this
6228 problem). This means that we need to handle expressions which
6229 are not valid in a C++ source, but can be created from the
6230 first call (for instance, casts to perform conversions). These
6231 hacks can go away after we fix the double coercion problem. */
6232
6233 static tree
6234 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6235 {
6236 tree expr_type;
6237
6238 /* Detect immediately string literals as invalid non-type argument.
6239 This special-case is not needed for correctness (we would easily
6240 catch this later), but only to provide better diagnostic for this
6241 common user mistake. As suggested by DR 100, we do not mention
6242 linkage issues in the diagnostic as this is not the point. */
6243 /* FIXME we're making this OK. */
6244 if (TREE_CODE (expr) == STRING_CST)
6245 {
6246 if (complain & tf_error)
6247 error ("%qE is not a valid template argument for type %qT "
6248 "because string literals can never be used in this context",
6249 expr, type);
6250 return NULL_TREE;
6251 }
6252
6253 /* Add the ADDR_EXPR now for the benefit of
6254 value_dependent_expression_p. */
6255 if (TYPE_PTROBV_P (type)
6256 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6257 {
6258 expr = decay_conversion (expr, complain);
6259 if (expr == error_mark_node)
6260 return error_mark_node;
6261 }
6262
6263 /* If we are in a template, EXPR may be non-dependent, but still
6264 have a syntactic, rather than semantic, form. For example, EXPR
6265 might be a SCOPE_REF, rather than the VAR_DECL to which the
6266 SCOPE_REF refers. Preserving the qualifying scope is necessary
6267 so that access checking can be performed when the template is
6268 instantiated -- but here we need the resolved form so that we can
6269 convert the argument. */
6270 bool non_dep = false;
6271 if (TYPE_REF_OBJ_P (type)
6272 && has_value_dependent_address (expr))
6273 /* If we want the address and it's value-dependent, don't fold. */;
6274 else if (processing_template_decl
6275 && potential_nondependent_constant_expression (expr))
6276 non_dep = true;
6277 if (error_operand_p (expr))
6278 return error_mark_node;
6279 expr_type = TREE_TYPE (expr);
6280 if (TREE_CODE (type) == REFERENCE_TYPE)
6281 expr = mark_lvalue_use (expr);
6282 else
6283 expr = mark_rvalue_use (expr);
6284
6285 /* If the argument is non-dependent, perform any conversions in
6286 non-dependent context as well. */
6287 processing_template_decl_sentinel s (non_dep);
6288 if (non_dep)
6289 expr = instantiate_non_dependent_expr_internal (expr, complain);
6290
6291 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6292 to a non-type argument of "nullptr". */
6293 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6294 expr = fold_simple (convert (type, expr));
6295
6296 /* In C++11, integral or enumeration non-type template arguments can be
6297 arbitrary constant expressions. Pointer and pointer to
6298 member arguments can be general constant expressions that evaluate
6299 to a null value, but otherwise still need to be of a specific form. */
6300 if (cxx_dialect >= cxx11)
6301 {
6302 if (TREE_CODE (expr) == PTRMEM_CST)
6303 /* A PTRMEM_CST is already constant, and a valid template
6304 argument for a parameter of pointer to member type, we just want
6305 to leave it in that form rather than lower it to a
6306 CONSTRUCTOR. */;
6307 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6308 expr = maybe_constant_value (expr);
6309 else if (cxx_dialect >= cxx1z)
6310 {
6311 if (TREE_CODE (type) != REFERENCE_TYPE)
6312 expr = maybe_constant_value (expr);
6313 else if (REFERENCE_REF_P (expr))
6314 {
6315 expr = TREE_OPERAND (expr, 0);
6316 expr = maybe_constant_value (expr);
6317 expr = convert_from_reference (expr);
6318 }
6319 }
6320 else if (TYPE_PTR_OR_PTRMEM_P (type))
6321 {
6322 tree folded = maybe_constant_value (expr);
6323 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6324 : null_member_pointer_value_p (folded))
6325 expr = folded;
6326 }
6327 }
6328
6329 /* HACK: Due to double coercion, we can get a
6330 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6331 which is the tree that we built on the first call (see
6332 below when coercing to reference to object or to reference to
6333 function). We just strip everything and get to the arg.
6334 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6335 for examples. */
6336 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6337 {
6338 tree probe_type, probe = expr;
6339 if (REFERENCE_REF_P (probe))
6340 probe = TREE_OPERAND (probe, 0);
6341 probe_type = TREE_TYPE (probe);
6342 if (TREE_CODE (probe) == NOP_EXPR)
6343 {
6344 /* ??? Maybe we could use convert_from_reference here, but we
6345 would need to relax its constraints because the NOP_EXPR
6346 could actually change the type to something more cv-qualified,
6347 and this is not folded by convert_from_reference. */
6348 tree addr = TREE_OPERAND (probe, 0);
6349 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6350 && TREE_CODE (addr) == ADDR_EXPR
6351 && TYPE_PTR_P (TREE_TYPE (addr))
6352 && (same_type_ignoring_top_level_qualifiers_p
6353 (TREE_TYPE (probe_type),
6354 TREE_TYPE (TREE_TYPE (addr)))))
6355 {
6356 expr = TREE_OPERAND (addr, 0);
6357 expr_type = TREE_TYPE (probe_type);
6358 }
6359 }
6360 }
6361
6362 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6363 parameter is a pointer to object, through decay and
6364 qualification conversion. Let's strip everything. */
6365 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6366 {
6367 tree probe = expr;
6368 STRIP_NOPS (probe);
6369 if (TREE_CODE (probe) == ADDR_EXPR
6370 && TYPE_PTR_P (TREE_TYPE (probe)))
6371 {
6372 /* Skip the ADDR_EXPR only if it is part of the decay for
6373 an array. Otherwise, it is part of the original argument
6374 in the source code. */
6375 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6376 probe = TREE_OPERAND (probe, 0);
6377 expr = probe;
6378 expr_type = TREE_TYPE (expr);
6379 }
6380 }
6381
6382 /* [temp.arg.nontype]/5, bullet 1
6383
6384 For a non-type template-parameter of integral or enumeration type,
6385 integral promotions (_conv.prom_) and integral conversions
6386 (_conv.integral_) are applied. */
6387 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6388 {
6389 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6390 t = maybe_constant_value (t);
6391 if (t != error_mark_node)
6392 expr = t;
6393
6394 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6395 return error_mark_node;
6396
6397 /* Notice that there are constant expressions like '4 % 0' which
6398 do not fold into integer constants. */
6399 if (TREE_CODE (expr) != INTEGER_CST)
6400 {
6401 if (complain & tf_error)
6402 {
6403 int errs = errorcount, warns = warningcount + werrorcount;
6404 if (processing_template_decl
6405 && !require_potential_constant_expression (expr))
6406 return NULL_TREE;
6407 expr = cxx_constant_value (expr);
6408 if (errorcount > errs || warningcount + werrorcount > warns)
6409 inform (EXPR_LOC_OR_LOC (expr, input_location),
6410 "in template argument for type %qT ", type);
6411 if (expr == error_mark_node)
6412 return NULL_TREE;
6413 /* else cxx_constant_value complained but gave us
6414 a real constant, so go ahead. */
6415 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6416 }
6417 else
6418 return NULL_TREE;
6419 }
6420
6421 /* Avoid typedef problems. */
6422 if (TREE_TYPE (expr) != type)
6423 expr = fold_convert (type, expr);
6424 }
6425 /* [temp.arg.nontype]/5, bullet 2
6426
6427 For a non-type template-parameter of type pointer to object,
6428 qualification conversions (_conv.qual_) and the array-to-pointer
6429 conversion (_conv.array_) are applied. */
6430 else if (TYPE_PTROBV_P (type))
6431 {
6432 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6433
6434 A template-argument for a non-type, non-template template-parameter
6435 shall be one of: [...]
6436
6437 -- the name of a non-type template-parameter;
6438 -- the address of an object or function with external linkage, [...]
6439 expressed as "& id-expression" where the & is optional if the name
6440 refers to a function or array, or if the corresponding
6441 template-parameter is a reference.
6442
6443 Here, we do not care about functions, as they are invalid anyway
6444 for a parameter of type pointer-to-object. */
6445
6446 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6447 /* Non-type template parameters are OK. */
6448 ;
6449 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6450 /* Null pointer values are OK in C++11. */;
6451 else if (TREE_CODE (expr) != ADDR_EXPR
6452 && TREE_CODE (expr_type) != ARRAY_TYPE)
6453 {
6454 if (VAR_P (expr))
6455 {
6456 if (complain & tf_error)
6457 error ("%qD is not a valid template argument "
6458 "because %qD is a variable, not the address of "
6459 "a variable", expr, expr);
6460 return NULL_TREE;
6461 }
6462 if (POINTER_TYPE_P (expr_type))
6463 {
6464 if (complain & tf_error)
6465 error ("%qE is not a valid template argument for %qT "
6466 "because it is not the address of a variable",
6467 expr, type);
6468 return NULL_TREE;
6469 }
6470 /* Other values, like integer constants, might be valid
6471 non-type arguments of some other type. */
6472 return error_mark_node;
6473 }
6474 else
6475 {
6476 tree decl;
6477
6478 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6479 ? TREE_OPERAND (expr, 0) : expr);
6480 if (!VAR_P (decl))
6481 {
6482 if (complain & tf_error)
6483 error ("%qE is not a valid template argument of type %qT "
6484 "because %qE is not a variable", expr, type, decl);
6485 return NULL_TREE;
6486 }
6487 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6488 {
6489 if (complain & tf_error)
6490 error ("%qE is not a valid template argument of type %qT "
6491 "because %qD does not have external linkage",
6492 expr, type, decl);
6493 return NULL_TREE;
6494 }
6495 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6496 {
6497 if (complain & tf_error)
6498 error ("%qE is not a valid template argument of type %qT "
6499 "because %qD has no linkage", expr, type, decl);
6500 return NULL_TREE;
6501 }
6502 }
6503
6504 expr = decay_conversion (expr, complain);
6505 if (expr == error_mark_node)
6506 return error_mark_node;
6507
6508 expr = perform_qualification_conversions (type, expr);
6509 if (expr == error_mark_node)
6510 return error_mark_node;
6511 }
6512 /* [temp.arg.nontype]/5, bullet 3
6513
6514 For a non-type template-parameter of type reference to object, no
6515 conversions apply. The type referred to by the reference may be more
6516 cv-qualified than the (otherwise identical) type of the
6517 template-argument. The template-parameter is bound directly to the
6518 template-argument, which must be an lvalue. */
6519 else if (TYPE_REF_OBJ_P (type))
6520 {
6521 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6522 expr_type))
6523 return error_mark_node;
6524
6525 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6526 {
6527 if (complain & tf_error)
6528 error ("%qE is not a valid template argument for type %qT "
6529 "because of conflicts in cv-qualification", expr, type);
6530 return NULL_TREE;
6531 }
6532
6533 if (!lvalue_p (expr))
6534 {
6535 if (complain & tf_error)
6536 error ("%qE is not a valid template argument for type %qT "
6537 "because it is not an lvalue", expr, type);
6538 return NULL_TREE;
6539 }
6540
6541 /* [temp.arg.nontype]/1
6542
6543 A template-argument for a non-type, non-template template-parameter
6544 shall be one of: [...]
6545
6546 -- the address of an object or function with external linkage. */
6547 if (INDIRECT_REF_P (expr)
6548 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6549 {
6550 expr = TREE_OPERAND (expr, 0);
6551 if (DECL_P (expr))
6552 {
6553 if (complain & tf_error)
6554 error ("%q#D is not a valid template argument for type %qT "
6555 "because a reference variable does not have a constant "
6556 "address", expr, type);
6557 return NULL_TREE;
6558 }
6559 }
6560
6561 if (!DECL_P (expr))
6562 {
6563 if (complain & tf_error)
6564 error ("%qE is not a valid template argument for type %qT "
6565 "because it is not an object with linkage",
6566 expr, type);
6567 return NULL_TREE;
6568 }
6569
6570 /* DR 1155 allows internal linkage in C++11 and up. */
6571 linkage_kind linkage = decl_linkage (expr);
6572 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6573 {
6574 if (complain & tf_error)
6575 error ("%qE is not a valid template argument for type %qT "
6576 "because object %qD does not have linkage",
6577 expr, type, expr);
6578 return NULL_TREE;
6579 }
6580
6581 expr = build_nop (type, build_address (expr));
6582 }
6583 /* [temp.arg.nontype]/5, bullet 4
6584
6585 For a non-type template-parameter of type pointer to function, only
6586 the function-to-pointer conversion (_conv.func_) is applied. If the
6587 template-argument represents a set of overloaded functions (or a
6588 pointer to such), the matching function is selected from the set
6589 (_over.over_). */
6590 else if (TYPE_PTRFN_P (type))
6591 {
6592 /* If the argument is a template-id, we might not have enough
6593 context information to decay the pointer. */
6594 if (!type_unknown_p (expr_type))
6595 {
6596 expr = decay_conversion (expr, complain);
6597 if (expr == error_mark_node)
6598 return error_mark_node;
6599 }
6600
6601 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6602 /* Null pointer values are OK in C++11. */
6603 return perform_qualification_conversions (type, expr);
6604
6605 expr = convert_nontype_argument_function (type, expr, complain);
6606 if (!expr || expr == error_mark_node)
6607 return expr;
6608 }
6609 /* [temp.arg.nontype]/5, bullet 5
6610
6611 For a non-type template-parameter of type reference to function, no
6612 conversions apply. If the template-argument represents a set of
6613 overloaded functions, the matching function is selected from the set
6614 (_over.over_). */
6615 else if (TYPE_REFFN_P (type))
6616 {
6617 if (TREE_CODE (expr) == ADDR_EXPR)
6618 {
6619 if (complain & tf_error)
6620 {
6621 error ("%qE is not a valid template argument for type %qT "
6622 "because it is a pointer", expr, type);
6623 inform (input_location, "try using %qE instead",
6624 TREE_OPERAND (expr, 0));
6625 }
6626 return NULL_TREE;
6627 }
6628
6629 expr = convert_nontype_argument_function (type, expr, complain);
6630 if (!expr || expr == error_mark_node)
6631 return expr;
6632
6633 expr = build_nop (type, build_address (expr));
6634 }
6635 /* [temp.arg.nontype]/5, bullet 6
6636
6637 For a non-type template-parameter of type pointer to member function,
6638 no conversions apply. If the template-argument represents a set of
6639 overloaded member functions, the matching member function is selected
6640 from the set (_over.over_). */
6641 else if (TYPE_PTRMEMFUNC_P (type))
6642 {
6643 expr = instantiate_type (type, expr, tf_none);
6644 if (expr == error_mark_node)
6645 return error_mark_node;
6646
6647 /* [temp.arg.nontype] bullet 1 says the pointer to member
6648 expression must be a pointer-to-member constant. */
6649 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6650 return error_mark_node;
6651
6652 /* There is no way to disable standard conversions in
6653 resolve_address_of_overloaded_function (called by
6654 instantiate_type). It is possible that the call succeeded by
6655 converting &B::I to &D::I (where B is a base of D), so we need
6656 to reject this conversion here.
6657
6658 Actually, even if there was a way to disable standard conversions,
6659 it would still be better to reject them here so that we can
6660 provide a superior diagnostic. */
6661 if (!same_type_p (TREE_TYPE (expr), type))
6662 {
6663 if (complain & tf_error)
6664 {
6665 error ("%qE is not a valid template argument for type %qT "
6666 "because it is of type %qT", expr, type,
6667 TREE_TYPE (expr));
6668 /* If we are just one standard conversion off, explain. */
6669 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6670 inform (input_location,
6671 "standard conversions are not allowed in this context");
6672 }
6673 return NULL_TREE;
6674 }
6675 }
6676 /* [temp.arg.nontype]/5, bullet 7
6677
6678 For a non-type template-parameter of type pointer to data member,
6679 qualification conversions (_conv.qual_) are applied. */
6680 else if (TYPE_PTRDATAMEM_P (type))
6681 {
6682 /* [temp.arg.nontype] bullet 1 says the pointer to member
6683 expression must be a pointer-to-member constant. */
6684 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6685 return error_mark_node;
6686
6687 expr = perform_qualification_conversions (type, expr);
6688 if (expr == error_mark_node)
6689 return expr;
6690 }
6691 else if (NULLPTR_TYPE_P (type))
6692 {
6693 if (expr != nullptr_node)
6694 {
6695 if (complain & tf_error)
6696 error ("%qE is not a valid template argument for type %qT "
6697 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6698 return NULL_TREE;
6699 }
6700 return expr;
6701 }
6702 /* A template non-type parameter must be one of the above. */
6703 else
6704 gcc_unreachable ();
6705
6706 /* Sanity check: did we actually convert the argument to the
6707 right type? */
6708 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6709 (type, TREE_TYPE (expr)));
6710 return convert_from_reference (expr);
6711 }
6712
6713 /* Subroutine of coerce_template_template_parms, which returns 1 if
6714 PARM_PARM and ARG_PARM match using the rule for the template
6715 parameters of template template parameters. Both PARM and ARG are
6716 template parameters; the rest of the arguments are the same as for
6717 coerce_template_template_parms.
6718 */
6719 static int
6720 coerce_template_template_parm (tree parm,
6721 tree arg,
6722 tsubst_flags_t complain,
6723 tree in_decl,
6724 tree outer_args)
6725 {
6726 if (arg == NULL_TREE || error_operand_p (arg)
6727 || parm == NULL_TREE || error_operand_p (parm))
6728 return 0;
6729
6730 if (TREE_CODE (arg) != TREE_CODE (parm))
6731 return 0;
6732
6733 switch (TREE_CODE (parm))
6734 {
6735 case TEMPLATE_DECL:
6736 /* We encounter instantiations of templates like
6737 template <template <template <class> class> class TT>
6738 class C; */
6739 {
6740 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6741 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6742
6743 if (!coerce_template_template_parms
6744 (parmparm, argparm, complain, in_decl, outer_args))
6745 return 0;
6746 }
6747 /* Fall through. */
6748
6749 case TYPE_DECL:
6750 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6751 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6752 /* Argument is a parameter pack but parameter is not. */
6753 return 0;
6754 break;
6755
6756 case PARM_DECL:
6757 /* The tsubst call is used to handle cases such as
6758
6759 template <int> class C {};
6760 template <class T, template <T> class TT> class D {};
6761 D<int, C> d;
6762
6763 i.e. the parameter list of TT depends on earlier parameters. */
6764 if (!uses_template_parms (TREE_TYPE (arg)))
6765 {
6766 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6767 if (!uses_template_parms (t)
6768 && !same_type_p (t, TREE_TYPE (arg)))
6769 return 0;
6770 }
6771
6772 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6773 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6774 /* Argument is a parameter pack but parameter is not. */
6775 return 0;
6776
6777 break;
6778
6779 default:
6780 gcc_unreachable ();
6781 }
6782
6783 return 1;
6784 }
6785
6786
6787 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6788 template template parameters. Both PARM_PARMS and ARG_PARMS are
6789 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6790 or PARM_DECL.
6791
6792 Consider the example:
6793 template <class T> class A;
6794 template<template <class U> class TT> class B;
6795
6796 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6797 the parameters to A, and OUTER_ARGS contains A. */
6798
6799 static int
6800 coerce_template_template_parms (tree parm_parms,
6801 tree arg_parms,
6802 tsubst_flags_t complain,
6803 tree in_decl,
6804 tree outer_args)
6805 {
6806 int nparms, nargs, i;
6807 tree parm, arg;
6808 int variadic_p = 0;
6809
6810 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6811 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6812
6813 nparms = TREE_VEC_LENGTH (parm_parms);
6814 nargs = TREE_VEC_LENGTH (arg_parms);
6815
6816 /* Determine whether we have a parameter pack at the end of the
6817 template template parameter's template parameter list. */
6818 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6819 {
6820 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6821
6822 if (error_operand_p (parm))
6823 return 0;
6824
6825 switch (TREE_CODE (parm))
6826 {
6827 case TEMPLATE_DECL:
6828 case TYPE_DECL:
6829 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6830 variadic_p = 1;
6831 break;
6832
6833 case PARM_DECL:
6834 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6835 variadic_p = 1;
6836 break;
6837
6838 default:
6839 gcc_unreachable ();
6840 }
6841 }
6842
6843 if (nargs != nparms
6844 && !(variadic_p && nargs >= nparms - 1))
6845 return 0;
6846
6847 /* Check all of the template parameters except the parameter pack at
6848 the end (if any). */
6849 for (i = 0; i < nparms - variadic_p; ++i)
6850 {
6851 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6852 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6853 continue;
6854
6855 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6856 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6857
6858 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6859 outer_args))
6860 return 0;
6861
6862 }
6863
6864 if (variadic_p)
6865 {
6866 /* Check each of the template parameters in the template
6867 argument against the template parameter pack at the end of
6868 the template template parameter. */
6869 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6870 return 0;
6871
6872 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6873
6874 for (; i < nargs; ++i)
6875 {
6876 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6877 continue;
6878
6879 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6880
6881 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6882 outer_args))
6883 return 0;
6884 }
6885 }
6886
6887 return 1;
6888 }
6889
6890 /* Verifies that the deduced template arguments (in TARGS) for the
6891 template template parameters (in TPARMS) represent valid bindings,
6892 by comparing the template parameter list of each template argument
6893 to the template parameter list of its corresponding template
6894 template parameter, in accordance with DR150. This
6895 routine can only be called after all template arguments have been
6896 deduced. It will return TRUE if all of the template template
6897 parameter bindings are okay, FALSE otherwise. */
6898 bool
6899 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6900 {
6901 int i, ntparms = TREE_VEC_LENGTH (tparms);
6902 bool ret = true;
6903
6904 /* We're dealing with template parms in this process. */
6905 ++processing_template_decl;
6906
6907 targs = INNERMOST_TEMPLATE_ARGS (targs);
6908
6909 for (i = 0; i < ntparms; ++i)
6910 {
6911 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6912 tree targ = TREE_VEC_ELT (targs, i);
6913
6914 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6915 {
6916 tree packed_args = NULL_TREE;
6917 int idx, len = 1;
6918
6919 if (ARGUMENT_PACK_P (targ))
6920 {
6921 /* Look inside the argument pack. */
6922 packed_args = ARGUMENT_PACK_ARGS (targ);
6923 len = TREE_VEC_LENGTH (packed_args);
6924 }
6925
6926 for (idx = 0; idx < len; ++idx)
6927 {
6928 tree targ_parms = NULL_TREE;
6929
6930 if (packed_args)
6931 /* Extract the next argument from the argument
6932 pack. */
6933 targ = TREE_VEC_ELT (packed_args, idx);
6934
6935 if (PACK_EXPANSION_P (targ))
6936 /* Look at the pattern of the pack expansion. */
6937 targ = PACK_EXPANSION_PATTERN (targ);
6938
6939 /* Extract the template parameters from the template
6940 argument. */
6941 if (TREE_CODE (targ) == TEMPLATE_DECL)
6942 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6943 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6944 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6945
6946 /* Verify that we can coerce the template template
6947 parameters from the template argument to the template
6948 parameter. This requires an exact match. */
6949 if (targ_parms
6950 && !coerce_template_template_parms
6951 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6952 targ_parms,
6953 tf_none,
6954 tparm,
6955 targs))
6956 {
6957 ret = false;
6958 goto out;
6959 }
6960 }
6961 }
6962 }
6963
6964 out:
6965
6966 --processing_template_decl;
6967 return ret;
6968 }
6969
6970 /* Since type attributes aren't mangled, we need to strip them from
6971 template type arguments. */
6972
6973 static tree
6974 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6975 {
6976 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6977 return arg;
6978 bool removed_attributes = false;
6979 tree canon = strip_typedefs (arg, &removed_attributes);
6980 if (removed_attributes
6981 && (complain & tf_warning))
6982 warning (OPT_Wignored_attributes,
6983 "ignoring attributes on template argument %qT", arg);
6984 return canon;
6985 }
6986
6987 // A template declaration can be substituted for a constrained
6988 // template template parameter only when the argument is more
6989 // constrained than the parameter.
6990 static bool
6991 is_compatible_template_arg (tree parm, tree arg)
6992 {
6993 tree parm_cons = get_constraints (parm);
6994
6995 /* For now, allow constrained template template arguments
6996 and unconstrained template template parameters. */
6997 if (parm_cons == NULL_TREE)
6998 return true;
6999
7000 tree arg_cons = get_constraints (arg);
7001
7002 // If the template parameter is constrained, we need to rewrite its
7003 // constraints in terms of the ARG's template parameters. This ensures
7004 // that all of the template parameter types will have the same depth.
7005 //
7006 // Note that this is only valid when coerce_template_template_parm is
7007 // true for the innermost template parameters of PARM and ARG. In other
7008 // words, because coercion is successful, this conversion will be valid.
7009 if (parm_cons)
7010 {
7011 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7012 parm_cons = tsubst_constraint_info (parm_cons,
7013 INNERMOST_TEMPLATE_ARGS (args),
7014 tf_none, NULL_TREE);
7015 if (parm_cons == error_mark_node)
7016 return false;
7017 }
7018
7019 return subsumes (parm_cons, arg_cons);
7020 }
7021
7022 // Convert a placeholder argument into a binding to the original
7023 // parameter. The original parameter is saved as the TREE_TYPE of
7024 // ARG.
7025 static inline tree
7026 convert_wildcard_argument (tree parm, tree arg)
7027 {
7028 TREE_TYPE (arg) = parm;
7029 return arg;
7030 }
7031
7032 /* Convert the indicated template ARG as necessary to match the
7033 indicated template PARM. Returns the converted ARG, or
7034 error_mark_node if the conversion was unsuccessful. Error and
7035 warning messages are issued under control of COMPLAIN. This
7036 conversion is for the Ith parameter in the parameter list. ARGS is
7037 the full set of template arguments deduced so far. */
7038
7039 static tree
7040 convert_template_argument (tree parm,
7041 tree arg,
7042 tree args,
7043 tsubst_flags_t complain,
7044 int i,
7045 tree in_decl)
7046 {
7047 tree orig_arg;
7048 tree val;
7049 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7050
7051 if (parm == error_mark_node)
7052 return error_mark_node;
7053
7054 /* Trivially convert placeholders. */
7055 if (TREE_CODE (arg) == WILDCARD_DECL)
7056 return convert_wildcard_argument (parm, arg);
7057
7058 if (TREE_CODE (arg) == TREE_LIST
7059 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7060 {
7061 /* The template argument was the name of some
7062 member function. That's usually
7063 invalid, but static members are OK. In any
7064 case, grab the underlying fields/functions
7065 and issue an error later if required. */
7066 orig_arg = TREE_VALUE (arg);
7067 TREE_TYPE (arg) = unknown_type_node;
7068 }
7069
7070 orig_arg = arg;
7071
7072 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7073 requires_type = (TREE_CODE (parm) == TYPE_DECL
7074 || requires_tmpl_type);
7075
7076 /* When determining whether an argument pack expansion is a template,
7077 look at the pattern. */
7078 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7079 arg = PACK_EXPANSION_PATTERN (arg);
7080
7081 /* Deal with an injected-class-name used as a template template arg. */
7082 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7083 {
7084 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7085 if (TREE_CODE (t) == TEMPLATE_DECL)
7086 {
7087 if (cxx_dialect >= cxx11)
7088 /* OK under DR 1004. */;
7089 else if (complain & tf_warning_or_error)
7090 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7091 " used as template template argument", TYPE_NAME (arg));
7092 else if (flag_pedantic_errors)
7093 t = arg;
7094
7095 arg = t;
7096 }
7097 }
7098
7099 is_tmpl_type =
7100 ((TREE_CODE (arg) == TEMPLATE_DECL
7101 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7102 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7103 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7104 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7105
7106 if (is_tmpl_type
7107 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7108 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7109 arg = TYPE_STUB_DECL (arg);
7110
7111 is_type = TYPE_P (arg) || is_tmpl_type;
7112
7113 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7114 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7115 {
7116 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7117 {
7118 if (complain & tf_error)
7119 error ("invalid use of destructor %qE as a type", orig_arg);
7120 return error_mark_node;
7121 }
7122
7123 permerror (input_location,
7124 "to refer to a type member of a template parameter, "
7125 "use %<typename %E%>", orig_arg);
7126
7127 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7128 TREE_OPERAND (arg, 1),
7129 typename_type,
7130 complain);
7131 arg = orig_arg;
7132 is_type = 1;
7133 }
7134 if (is_type != requires_type)
7135 {
7136 if (in_decl)
7137 {
7138 if (complain & tf_error)
7139 {
7140 error ("type/value mismatch at argument %d in template "
7141 "parameter list for %qD",
7142 i + 1, in_decl);
7143 if (is_type)
7144 inform (input_location,
7145 " expected a constant of type %qT, got %qT",
7146 TREE_TYPE (parm),
7147 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7148 else if (requires_tmpl_type)
7149 inform (input_location,
7150 " expected a class template, got %qE", orig_arg);
7151 else
7152 inform (input_location,
7153 " expected a type, got %qE", orig_arg);
7154 }
7155 }
7156 return error_mark_node;
7157 }
7158 if (is_tmpl_type ^ requires_tmpl_type)
7159 {
7160 if (in_decl && (complain & tf_error))
7161 {
7162 error ("type/value mismatch at argument %d in template "
7163 "parameter list for %qD",
7164 i + 1, in_decl);
7165 if (is_tmpl_type)
7166 inform (input_location,
7167 " expected a type, got %qT", DECL_NAME (arg));
7168 else
7169 inform (input_location,
7170 " expected a class template, got %qT", orig_arg);
7171 }
7172 return error_mark_node;
7173 }
7174
7175 if (is_type)
7176 {
7177 if (requires_tmpl_type)
7178 {
7179 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7180 val = orig_arg;
7181 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7182 /* The number of argument required is not known yet.
7183 Just accept it for now. */
7184 val = TREE_TYPE (arg);
7185 else
7186 {
7187 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7188 tree argparm;
7189
7190 /* Strip alias templates that are equivalent to another
7191 template. */
7192 arg = get_underlying_template (arg);
7193 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7194
7195 if (coerce_template_template_parms (parmparm, argparm,
7196 complain, in_decl,
7197 args))
7198 {
7199 val = arg;
7200
7201 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7202 TEMPLATE_DECL. */
7203 if (val != error_mark_node)
7204 {
7205 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7206 val = TREE_TYPE (val);
7207 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7208 val = make_pack_expansion (val);
7209 }
7210 }
7211 else
7212 {
7213 if (in_decl && (complain & tf_error))
7214 {
7215 error ("type/value mismatch at argument %d in "
7216 "template parameter list for %qD",
7217 i + 1, in_decl);
7218 inform (input_location,
7219 " expected a template of type %qD, got %qT",
7220 parm, orig_arg);
7221 }
7222
7223 val = error_mark_node;
7224 }
7225
7226 // Check that the constraints are compatible before allowing the
7227 // substitution.
7228 if (val != error_mark_node)
7229 if (!is_compatible_template_arg (parm, arg))
7230 {
7231 if (in_decl && (complain & tf_error))
7232 {
7233 error ("constraint mismatch at argument %d in "
7234 "template parameter list for %qD",
7235 i + 1, in_decl);
7236 inform (input_location, " expected %qD but got %qD",
7237 parm, arg);
7238 }
7239 val = error_mark_node;
7240 }
7241 }
7242 }
7243 else
7244 val = orig_arg;
7245 /* We only form one instance of each template specialization.
7246 Therefore, if we use a non-canonical variant (i.e., a
7247 typedef), any future messages referring to the type will use
7248 the typedef, which is confusing if those future uses do not
7249 themselves also use the typedef. */
7250 if (TYPE_P (val))
7251 val = canonicalize_type_argument (val, complain);
7252 }
7253 else
7254 {
7255 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7256
7257 if (invalid_nontype_parm_type_p (t, complain))
7258 return error_mark_node;
7259
7260 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7261 {
7262 if (same_type_p (t, TREE_TYPE (orig_arg)))
7263 val = orig_arg;
7264 else
7265 {
7266 /* Not sure if this is reachable, but it doesn't hurt
7267 to be robust. */
7268 error ("type mismatch in nontype parameter pack");
7269 val = error_mark_node;
7270 }
7271 }
7272 else if (!dependent_template_arg_p (orig_arg)
7273 && !uses_template_parms (t))
7274 /* We used to call digest_init here. However, digest_init
7275 will report errors, which we don't want when complain
7276 is zero. More importantly, digest_init will try too
7277 hard to convert things: for example, `0' should not be
7278 converted to pointer type at this point according to
7279 the standard. Accepting this is not merely an
7280 extension, since deciding whether or not these
7281 conversions can occur is part of determining which
7282 function template to call, or whether a given explicit
7283 argument specification is valid. */
7284 val = convert_nontype_argument (t, orig_arg, complain);
7285 else
7286 {
7287 bool removed_attr = false;
7288 val = strip_typedefs_expr (orig_arg, &removed_attr);
7289 }
7290
7291 if (val == NULL_TREE)
7292 val = error_mark_node;
7293 else if (val == error_mark_node && (complain & tf_error))
7294 error ("could not convert template argument %qE to %qT", orig_arg, t);
7295
7296 if (INDIRECT_REF_P (val))
7297 {
7298 /* Reject template arguments that are references to built-in
7299 functions with no library fallbacks. */
7300 const_tree inner = TREE_OPERAND (val, 0);
7301 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7302 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner))) == FUNCTION_TYPE
7303 && TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7304 && 0 < TREE_OPERAND_LENGTH (inner)
7305 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7306 return error_mark_node;
7307 }
7308
7309 if (TREE_CODE (val) == SCOPE_REF)
7310 {
7311 /* Strip typedefs from the SCOPE_REF. */
7312 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7313 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7314 complain);
7315 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7316 QUALIFIED_NAME_IS_TEMPLATE (val));
7317 }
7318 }
7319
7320 return val;
7321 }
7322
7323 /* Coerces the remaining template arguments in INNER_ARGS (from
7324 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7325 Returns the coerced argument pack. PARM_IDX is the position of this
7326 parameter in the template parameter list. ARGS is the original
7327 template argument list. */
7328 static tree
7329 coerce_template_parameter_pack (tree parms,
7330 int parm_idx,
7331 tree args,
7332 tree inner_args,
7333 int arg_idx,
7334 tree new_args,
7335 int* lost,
7336 tree in_decl,
7337 tsubst_flags_t complain)
7338 {
7339 tree parm = TREE_VEC_ELT (parms, parm_idx);
7340 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7341 tree packed_args;
7342 tree argument_pack;
7343 tree packed_parms = NULL_TREE;
7344
7345 if (arg_idx > nargs)
7346 arg_idx = nargs;
7347
7348 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7349 {
7350 /* When the template parameter is a non-type template parameter pack
7351 or template template parameter pack whose type or template
7352 parameters use parameter packs, we know exactly how many arguments
7353 we are looking for. Build a vector of the instantiated decls for
7354 these template parameters in PACKED_PARMS. */
7355 /* We can't use make_pack_expansion here because it would interpret a
7356 _DECL as a use rather than a declaration. */
7357 tree decl = TREE_VALUE (parm);
7358 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7359 SET_PACK_EXPANSION_PATTERN (exp, decl);
7360 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7361 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7362
7363 TREE_VEC_LENGTH (args)--;
7364 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7365 TREE_VEC_LENGTH (args)++;
7366
7367 if (packed_parms == error_mark_node)
7368 return error_mark_node;
7369
7370 /* If we're doing a partial instantiation of a member template,
7371 verify that all of the types used for the non-type
7372 template parameter pack are, in fact, valid for non-type
7373 template parameters. */
7374 if (arg_idx < nargs
7375 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7376 {
7377 int j, len = TREE_VEC_LENGTH (packed_parms);
7378 for (j = 0; j < len; ++j)
7379 {
7380 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7381 if (invalid_nontype_parm_type_p (t, complain))
7382 return error_mark_node;
7383 }
7384 /* We don't know how many args we have yet, just
7385 use the unconverted ones for now. */
7386 return NULL_TREE;
7387 }
7388
7389 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7390 }
7391 /* Check if we have a placeholder pack, which indicates we're
7392 in the context of a introduction list. In that case we want
7393 to match this pack to the single placeholder. */
7394 else if (arg_idx < nargs
7395 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7396 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7397 {
7398 nargs = arg_idx + 1;
7399 packed_args = make_tree_vec (1);
7400 }
7401 else
7402 packed_args = make_tree_vec (nargs - arg_idx);
7403
7404 /* Convert the remaining arguments, which will be a part of the
7405 parameter pack "parm". */
7406 int first_pack_arg = arg_idx;
7407 for (; arg_idx < nargs; ++arg_idx)
7408 {
7409 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7410 tree actual_parm = TREE_VALUE (parm);
7411 int pack_idx = arg_idx - first_pack_arg;
7412
7413 if (packed_parms)
7414 {
7415 /* Once we've packed as many args as we have types, stop. */
7416 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7417 break;
7418 else if (PACK_EXPANSION_P (arg))
7419 /* We don't know how many args we have yet, just
7420 use the unconverted ones for now. */
7421 return NULL_TREE;
7422 else
7423 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7424 }
7425
7426 if (arg == error_mark_node)
7427 {
7428 if (complain & tf_error)
7429 error ("template argument %d is invalid", arg_idx + 1);
7430 }
7431 else
7432 arg = convert_template_argument (actual_parm,
7433 arg, new_args, complain, parm_idx,
7434 in_decl);
7435 if (arg == error_mark_node)
7436 (*lost)++;
7437 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7438 }
7439
7440 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7441 && TREE_VEC_LENGTH (packed_args) > 0)
7442 {
7443 if (complain & tf_error)
7444 error ("wrong number of template arguments (%d, should be %d)",
7445 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7446 return error_mark_node;
7447 }
7448
7449 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7450 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7451 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7452 else
7453 {
7454 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7455 TREE_TYPE (argument_pack)
7456 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7457 TREE_CONSTANT (argument_pack) = 1;
7458 }
7459
7460 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7461 if (CHECKING_P)
7462 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7463 TREE_VEC_LENGTH (packed_args));
7464 return argument_pack;
7465 }
7466
7467 /* Returns the number of pack expansions in the template argument vector
7468 ARGS. */
7469
7470 static int
7471 pack_expansion_args_count (tree args)
7472 {
7473 int i;
7474 int count = 0;
7475 if (args)
7476 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7477 {
7478 tree elt = TREE_VEC_ELT (args, i);
7479 if (elt && PACK_EXPANSION_P (elt))
7480 ++count;
7481 }
7482 return count;
7483 }
7484
7485 /* Convert all template arguments to their appropriate types, and
7486 return a vector containing the innermost resulting template
7487 arguments. If any error occurs, return error_mark_node. Error and
7488 warning messages are issued under control of COMPLAIN.
7489
7490 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7491 for arguments not specified in ARGS. Otherwise, if
7492 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7493 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7494 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7495 ARGS. */
7496
7497 static tree
7498 coerce_template_parms (tree parms,
7499 tree args,
7500 tree in_decl,
7501 tsubst_flags_t complain,
7502 bool require_all_args,
7503 bool use_default_args)
7504 {
7505 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7506 tree orig_inner_args;
7507 tree inner_args;
7508 tree new_args;
7509 tree new_inner_args;
7510 int saved_unevaluated_operand;
7511 int saved_inhibit_evaluation_warnings;
7512
7513 /* When used as a boolean value, indicates whether this is a
7514 variadic template parameter list. Since it's an int, we can also
7515 subtract it from nparms to get the number of non-variadic
7516 parameters. */
7517 int variadic_p = 0;
7518 int variadic_args_p = 0;
7519 int post_variadic_parms = 0;
7520
7521 /* Likewise for parameters with default arguments. */
7522 int default_p = 0;
7523
7524 if (args == error_mark_node)
7525 return error_mark_node;
7526
7527 nparms = TREE_VEC_LENGTH (parms);
7528
7529 /* Determine if there are any parameter packs or default arguments. */
7530 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7531 {
7532 tree parm = TREE_VEC_ELT (parms, parm_idx);
7533 if (variadic_p)
7534 ++post_variadic_parms;
7535 if (template_parameter_pack_p (TREE_VALUE (parm)))
7536 ++variadic_p;
7537 if (TREE_PURPOSE (parm))
7538 ++default_p;
7539 }
7540
7541 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7542 /* If there are no parameters that follow a parameter pack, we need to
7543 expand any argument packs so that we can deduce a parameter pack from
7544 some non-packed args followed by an argument pack, as in variadic85.C.
7545 If there are such parameters, we need to leave argument packs intact
7546 so the arguments are assigned properly. This can happen when dealing
7547 with a nested class inside a partial specialization of a class
7548 template, as in variadic92.C, or when deducing a template parameter pack
7549 from a sub-declarator, as in variadic114.C. */
7550 if (!post_variadic_parms)
7551 inner_args = expand_template_argument_pack (inner_args);
7552
7553 /* Count any pack expansion args. */
7554 variadic_args_p = pack_expansion_args_count (inner_args);
7555
7556 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7557 if ((nargs > nparms && !variadic_p)
7558 || (nargs < nparms - variadic_p
7559 && require_all_args
7560 && !variadic_args_p
7561 && (!use_default_args
7562 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7563 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7564 {
7565 if (complain & tf_error)
7566 {
7567 if (variadic_p || default_p)
7568 {
7569 nparms -= variadic_p + default_p;
7570 error ("wrong number of template arguments "
7571 "(%d, should be at least %d)", nargs, nparms);
7572 }
7573 else
7574 error ("wrong number of template arguments "
7575 "(%d, should be %d)", nargs, nparms);
7576
7577 if (in_decl)
7578 inform (DECL_SOURCE_LOCATION (in_decl),
7579 "provided for %qD", in_decl);
7580 }
7581
7582 return error_mark_node;
7583 }
7584 /* We can't pass a pack expansion to a non-pack parameter of an alias
7585 template (DR 1430). */
7586 else if (in_decl
7587 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7588 || concept_template_p (in_decl))
7589 && variadic_args_p
7590 && nargs - variadic_args_p < nparms - variadic_p)
7591 {
7592 if (complain & tf_error)
7593 {
7594 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7595 {
7596 tree arg = TREE_VEC_ELT (inner_args, i);
7597 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7598
7599 if (PACK_EXPANSION_P (arg)
7600 && !template_parameter_pack_p (parm))
7601 {
7602 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7603 error_at (location_of (arg),
7604 "pack expansion argument for non-pack parameter "
7605 "%qD of alias template %qD", parm, in_decl);
7606 else
7607 error_at (location_of (arg),
7608 "pack expansion argument for non-pack parameter "
7609 "%qD of concept %qD", parm, in_decl);
7610 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7611 goto found;
7612 }
7613 }
7614 gcc_unreachable ();
7615 found:;
7616 }
7617 return error_mark_node;
7618 }
7619
7620 /* We need to evaluate the template arguments, even though this
7621 template-id may be nested within a "sizeof". */
7622 saved_unevaluated_operand = cp_unevaluated_operand;
7623 cp_unevaluated_operand = 0;
7624 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7625 c_inhibit_evaluation_warnings = 0;
7626 new_inner_args = make_tree_vec (nparms);
7627 new_args = add_outermost_template_args (args, new_inner_args);
7628 int pack_adjust = 0;
7629 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7630 {
7631 tree arg;
7632 tree parm;
7633
7634 /* Get the Ith template parameter. */
7635 parm = TREE_VEC_ELT (parms, parm_idx);
7636
7637 if (parm == error_mark_node)
7638 {
7639 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7640 continue;
7641 }
7642
7643 /* Calculate the next argument. */
7644 if (arg_idx < nargs)
7645 arg = TREE_VEC_ELT (inner_args, arg_idx);
7646 else
7647 arg = NULL_TREE;
7648
7649 if (template_parameter_pack_p (TREE_VALUE (parm))
7650 && !(arg && ARGUMENT_PACK_P (arg)))
7651 {
7652 /* Some arguments will be placed in the
7653 template parameter pack PARM. */
7654 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7655 inner_args, arg_idx,
7656 new_args, &lost,
7657 in_decl, complain);
7658
7659 if (arg == NULL_TREE)
7660 {
7661 /* We don't know how many args we have yet, just use the
7662 unconverted (and still packed) ones for now. */
7663 new_inner_args = orig_inner_args;
7664 arg_idx = nargs;
7665 break;
7666 }
7667
7668 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7669
7670 /* Store this argument. */
7671 if (arg == error_mark_node)
7672 {
7673 lost++;
7674 /* We are done with all of the arguments. */
7675 arg_idx = nargs;
7676 }
7677 else
7678 {
7679 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7680 arg_idx += pack_adjust;
7681 }
7682
7683 continue;
7684 }
7685 else if (arg)
7686 {
7687 if (PACK_EXPANSION_P (arg))
7688 {
7689 /* "If every valid specialization of a variadic template
7690 requires an empty template parameter pack, the template is
7691 ill-formed, no diagnostic required." So check that the
7692 pattern works with this parameter. */
7693 tree pattern = PACK_EXPANSION_PATTERN (arg);
7694 tree conv = convert_template_argument (TREE_VALUE (parm),
7695 pattern, new_args,
7696 complain, parm_idx,
7697 in_decl);
7698 if (conv == error_mark_node)
7699 {
7700 inform (input_location, "so any instantiation with a "
7701 "non-empty parameter pack would be ill-formed");
7702 ++lost;
7703 }
7704 else if (TYPE_P (conv) && !TYPE_P (pattern))
7705 /* Recover from missing typename. */
7706 TREE_VEC_ELT (inner_args, arg_idx)
7707 = make_pack_expansion (conv);
7708
7709 /* We don't know how many args we have yet, just
7710 use the unconverted ones for now. */
7711 new_inner_args = inner_args;
7712 arg_idx = nargs;
7713 break;
7714 }
7715 }
7716 else if (require_all_args)
7717 {
7718 /* There must be a default arg in this case. */
7719 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7720 complain, in_decl);
7721 /* The position of the first default template argument,
7722 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7723 Record that. */
7724 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7725 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7726 arg_idx - pack_adjust);
7727 }
7728 else
7729 break;
7730
7731 if (arg == error_mark_node)
7732 {
7733 if (complain & tf_error)
7734 error ("template argument %d is invalid", arg_idx + 1);
7735 }
7736 else if (!arg)
7737 /* This only occurs if there was an error in the template
7738 parameter list itself (which we would already have
7739 reported) that we are trying to recover from, e.g., a class
7740 template with a parameter list such as
7741 template<typename..., typename>. */
7742 ++lost;
7743 else
7744 arg = convert_template_argument (TREE_VALUE (parm),
7745 arg, new_args, complain,
7746 parm_idx, in_decl);
7747
7748 if (arg == error_mark_node)
7749 lost++;
7750 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7751 }
7752 cp_unevaluated_operand = saved_unevaluated_operand;
7753 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7754
7755 if (variadic_p && arg_idx < nargs)
7756 {
7757 if (complain & tf_error)
7758 {
7759 error ("wrong number of template arguments "
7760 "(%d, should be %d)", nargs, arg_idx);
7761 if (in_decl)
7762 error ("provided for %q+D", in_decl);
7763 }
7764 return error_mark_node;
7765 }
7766
7767 if (lost)
7768 return error_mark_node;
7769
7770 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7771 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7772 TREE_VEC_LENGTH (new_inner_args));
7773
7774 return new_inner_args;
7775 }
7776
7777 /* Convert all template arguments to their appropriate types, and
7778 return a vector containing the innermost resulting template
7779 arguments. If any error occurs, return error_mark_node. Error and
7780 warning messages are not issued.
7781
7782 Note that no function argument deduction is performed, and default
7783 arguments are used to fill in unspecified arguments. */
7784 tree
7785 coerce_template_parms (tree parms, tree args, tree in_decl)
7786 {
7787 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7788 }
7789
7790 /* Convert all template arguments to their appropriate type, and
7791 instantiate default arguments as needed. This returns a vector
7792 containing the innermost resulting template arguments, or
7793 error_mark_node if unsuccessful. */
7794 tree
7795 coerce_template_parms (tree parms, tree args, tree in_decl,
7796 tsubst_flags_t complain)
7797 {
7798 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7799 }
7800
7801 /* Like coerce_template_parms. If PARMS represents all template
7802 parameters levels, this function returns a vector of vectors
7803 representing all the resulting argument levels. Note that in this
7804 case, only the innermost arguments are coerced because the
7805 outermost ones are supposed to have been coerced already.
7806
7807 Otherwise, if PARMS represents only (the innermost) vector of
7808 parameters, this function returns a vector containing just the
7809 innermost resulting arguments. */
7810
7811 static tree
7812 coerce_innermost_template_parms (tree parms,
7813 tree args,
7814 tree in_decl,
7815 tsubst_flags_t complain,
7816 bool require_all_args,
7817 bool use_default_args)
7818 {
7819 int parms_depth = TMPL_PARMS_DEPTH (parms);
7820 int args_depth = TMPL_ARGS_DEPTH (args);
7821 tree coerced_args;
7822
7823 if (parms_depth > 1)
7824 {
7825 coerced_args = make_tree_vec (parms_depth);
7826 tree level;
7827 int cur_depth;
7828
7829 for (level = parms, cur_depth = parms_depth;
7830 parms_depth > 0 && level != NULL_TREE;
7831 level = TREE_CHAIN (level), --cur_depth)
7832 {
7833 tree l;
7834 if (cur_depth == args_depth)
7835 l = coerce_template_parms (TREE_VALUE (level),
7836 args, in_decl, complain,
7837 require_all_args,
7838 use_default_args);
7839 else
7840 l = TMPL_ARGS_LEVEL (args, cur_depth);
7841
7842 if (l == error_mark_node)
7843 return error_mark_node;
7844
7845 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7846 }
7847 }
7848 else
7849 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7850 args, in_decl, complain,
7851 require_all_args,
7852 use_default_args);
7853 return coerced_args;
7854 }
7855
7856 /* Returns 1 if template args OT and NT are equivalent. */
7857
7858 int
7859 template_args_equal (tree ot, tree nt)
7860 {
7861 if (nt == ot)
7862 return 1;
7863 if (nt == NULL_TREE || ot == NULL_TREE)
7864 return false;
7865
7866 if (TREE_CODE (nt) == TREE_VEC)
7867 /* For member templates */
7868 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7869 else if (PACK_EXPANSION_P (ot))
7870 return (PACK_EXPANSION_P (nt)
7871 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7872 PACK_EXPANSION_PATTERN (nt))
7873 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7874 PACK_EXPANSION_EXTRA_ARGS (nt)));
7875 else if (ARGUMENT_PACK_P (ot))
7876 {
7877 int i, len;
7878 tree opack, npack;
7879
7880 if (!ARGUMENT_PACK_P (nt))
7881 return 0;
7882
7883 opack = ARGUMENT_PACK_ARGS (ot);
7884 npack = ARGUMENT_PACK_ARGS (nt);
7885 len = TREE_VEC_LENGTH (opack);
7886 if (TREE_VEC_LENGTH (npack) != len)
7887 return 0;
7888 for (i = 0; i < len; ++i)
7889 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7890 TREE_VEC_ELT (npack, i)))
7891 return 0;
7892 return 1;
7893 }
7894 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7895 gcc_unreachable ();
7896 else if (TYPE_P (nt))
7897 {
7898 if (!TYPE_P (ot))
7899 return false;
7900 /* Don't treat an alias template specialization with dependent
7901 arguments as equivalent to its underlying type when used as a
7902 template argument; we need them to be distinct so that we
7903 substitute into the specialization arguments at instantiation
7904 time. And aliases can't be equivalent without being ==, so
7905 we don't need to look any deeper. */
7906 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7907 return false;
7908 else
7909 return same_type_p (ot, nt);
7910 }
7911 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7912 return 0;
7913 else
7914 {
7915 /* Try to treat a template non-type argument that has been converted
7916 to the parameter type as equivalent to one that hasn't yet. */
7917 for (enum tree_code code1 = TREE_CODE (ot);
7918 CONVERT_EXPR_CODE_P (code1)
7919 || code1 == NON_LVALUE_EXPR;
7920 code1 = TREE_CODE (ot))
7921 ot = TREE_OPERAND (ot, 0);
7922 for (enum tree_code code2 = TREE_CODE (nt);
7923 CONVERT_EXPR_CODE_P (code2)
7924 || code2 == NON_LVALUE_EXPR;
7925 code2 = TREE_CODE (nt))
7926 nt = TREE_OPERAND (nt, 0);
7927
7928 return cp_tree_equal (ot, nt);
7929 }
7930 }
7931
7932 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7933 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7934 NEWARG_PTR with the offending arguments if they are non-NULL. */
7935
7936 int
7937 comp_template_args (tree oldargs, tree newargs,
7938 tree *oldarg_ptr, tree *newarg_ptr)
7939 {
7940 int i;
7941
7942 if (oldargs == newargs)
7943 return 1;
7944
7945 if (!oldargs || !newargs)
7946 return 0;
7947
7948 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7949 return 0;
7950
7951 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7952 {
7953 tree nt = TREE_VEC_ELT (newargs, i);
7954 tree ot = TREE_VEC_ELT (oldargs, i);
7955
7956 if (! template_args_equal (ot, nt))
7957 {
7958 if (oldarg_ptr != NULL)
7959 *oldarg_ptr = ot;
7960 if (newarg_ptr != NULL)
7961 *newarg_ptr = nt;
7962 return 0;
7963 }
7964 }
7965 return 1;
7966 }
7967
7968 static void
7969 add_pending_template (tree d)
7970 {
7971 tree ti = (TYPE_P (d)
7972 ? CLASSTYPE_TEMPLATE_INFO (d)
7973 : DECL_TEMPLATE_INFO (d));
7974 struct pending_template *pt;
7975 int level;
7976
7977 if (TI_PENDING_TEMPLATE_FLAG (ti))
7978 return;
7979
7980 /* We are called both from instantiate_decl, where we've already had a
7981 tinst_level pushed, and instantiate_template, where we haven't.
7982 Compensate. */
7983 level = !current_tinst_level || current_tinst_level->decl != d;
7984
7985 if (level)
7986 push_tinst_level (d);
7987
7988 pt = ggc_alloc<pending_template> ();
7989 pt->next = NULL;
7990 pt->tinst = current_tinst_level;
7991 if (last_pending_template)
7992 last_pending_template->next = pt;
7993 else
7994 pending_templates = pt;
7995
7996 last_pending_template = pt;
7997
7998 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7999
8000 if (level)
8001 pop_tinst_level ();
8002 }
8003
8004
8005 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8006 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8007 documentation for TEMPLATE_ID_EXPR. */
8008
8009 tree
8010 lookup_template_function (tree fns, tree arglist)
8011 {
8012 tree type;
8013
8014 if (fns == error_mark_node || arglist == error_mark_node)
8015 return error_mark_node;
8016
8017 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8018
8019 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8020 {
8021 error ("%q#D is not a function template", fns);
8022 return error_mark_node;
8023 }
8024
8025 if (BASELINK_P (fns))
8026 {
8027 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8028 unknown_type_node,
8029 BASELINK_FUNCTIONS (fns),
8030 arglist);
8031 return fns;
8032 }
8033
8034 type = TREE_TYPE (fns);
8035 if (TREE_CODE (fns) == OVERLOAD || !type)
8036 type = unknown_type_node;
8037
8038 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8039 }
8040
8041 /* Within the scope of a template class S<T>, the name S gets bound
8042 (in build_self_reference) to a TYPE_DECL for the class, not a
8043 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8044 or one of its enclosing classes, and that type is a template,
8045 return the associated TEMPLATE_DECL. Otherwise, the original
8046 DECL is returned.
8047
8048 Also handle the case when DECL is a TREE_LIST of ambiguous
8049 injected-class-names from different bases. */
8050
8051 tree
8052 maybe_get_template_decl_from_type_decl (tree decl)
8053 {
8054 if (decl == NULL_TREE)
8055 return decl;
8056
8057 /* DR 176: A lookup that finds an injected-class-name (10.2
8058 [class.member.lookup]) can result in an ambiguity in certain cases
8059 (for example, if it is found in more than one base class). If all of
8060 the injected-class-names that are found refer to specializations of
8061 the same class template, and if the name is followed by a
8062 template-argument-list, the reference refers to the class template
8063 itself and not a specialization thereof, and is not ambiguous. */
8064 if (TREE_CODE (decl) == TREE_LIST)
8065 {
8066 tree t, tmpl = NULL_TREE;
8067 for (t = decl; t; t = TREE_CHAIN (t))
8068 {
8069 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8070 if (!tmpl)
8071 tmpl = elt;
8072 else if (tmpl != elt)
8073 break;
8074 }
8075 if (tmpl && t == NULL_TREE)
8076 return tmpl;
8077 else
8078 return decl;
8079 }
8080
8081 return (decl != NULL_TREE
8082 && DECL_SELF_REFERENCE_P (decl)
8083 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8084 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8085 }
8086
8087 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8088 parameters, find the desired type.
8089
8090 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8091
8092 IN_DECL, if non-NULL, is the template declaration we are trying to
8093 instantiate.
8094
8095 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8096 the class we are looking up.
8097
8098 Issue error and warning messages under control of COMPLAIN.
8099
8100 If the template class is really a local class in a template
8101 function, then the FUNCTION_CONTEXT is the function in which it is
8102 being instantiated.
8103
8104 ??? Note that this function is currently called *twice* for each
8105 template-id: the first time from the parser, while creating the
8106 incomplete type (finish_template_type), and the second type during the
8107 real instantiation (instantiate_template_class). This is surely something
8108 that we want to avoid. It also causes some problems with argument
8109 coercion (see convert_nontype_argument for more information on this). */
8110
8111 static tree
8112 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8113 int entering_scope, tsubst_flags_t complain)
8114 {
8115 tree templ = NULL_TREE, parmlist;
8116 tree t;
8117 spec_entry **slot;
8118 spec_entry *entry;
8119 spec_entry elt;
8120 hashval_t hash;
8121
8122 if (identifier_p (d1))
8123 {
8124 tree value = innermost_non_namespace_value (d1);
8125 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8126 templ = value;
8127 else
8128 {
8129 if (context)
8130 push_decl_namespace (context);
8131 templ = lookup_name (d1);
8132 templ = maybe_get_template_decl_from_type_decl (templ);
8133 if (context)
8134 pop_decl_namespace ();
8135 }
8136 if (templ)
8137 context = DECL_CONTEXT (templ);
8138 }
8139 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8140 {
8141 tree type = TREE_TYPE (d1);
8142
8143 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8144 an implicit typename for the second A. Deal with it. */
8145 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8146 type = TREE_TYPE (type);
8147
8148 if (CLASSTYPE_TEMPLATE_INFO (type))
8149 {
8150 templ = CLASSTYPE_TI_TEMPLATE (type);
8151 d1 = DECL_NAME (templ);
8152 }
8153 }
8154 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8155 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8156 {
8157 templ = TYPE_TI_TEMPLATE (d1);
8158 d1 = DECL_NAME (templ);
8159 }
8160 else if (DECL_TYPE_TEMPLATE_P (d1))
8161 {
8162 templ = d1;
8163 d1 = DECL_NAME (templ);
8164 context = DECL_CONTEXT (templ);
8165 }
8166 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8167 {
8168 templ = d1;
8169 d1 = DECL_NAME (templ);
8170 }
8171
8172 /* Issue an error message if we didn't find a template. */
8173 if (! templ)
8174 {
8175 if (complain & tf_error)
8176 error ("%qT is not a template", d1);
8177 return error_mark_node;
8178 }
8179
8180 if (TREE_CODE (templ) != TEMPLATE_DECL
8181 /* Make sure it's a user visible template, if it was named by
8182 the user. */
8183 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8184 && !PRIMARY_TEMPLATE_P (templ)))
8185 {
8186 if (complain & tf_error)
8187 {
8188 error ("non-template type %qT used as a template", d1);
8189 if (in_decl)
8190 error ("for template declaration %q+D", in_decl);
8191 }
8192 return error_mark_node;
8193 }
8194
8195 complain &= ~tf_user;
8196
8197 /* An alias that just changes the name of a template is equivalent to the
8198 other template, so if any of the arguments are pack expansions, strip
8199 the alias to avoid problems with a pack expansion passed to a non-pack
8200 alias template parameter (DR 1430). */
8201 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8202 templ = get_underlying_template (templ);
8203
8204 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8205 {
8206 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8207 template arguments */
8208
8209 tree parm;
8210 tree arglist2;
8211 tree outer;
8212
8213 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8214
8215 /* Consider an example where a template template parameter declared as
8216
8217 template <class T, class U = std::allocator<T> > class TT
8218
8219 The template parameter level of T and U are one level larger than
8220 of TT. To proper process the default argument of U, say when an
8221 instantiation `TT<int>' is seen, we need to build the full
8222 arguments containing {int} as the innermost level. Outer levels,
8223 available when not appearing as default template argument, can be
8224 obtained from the arguments of the enclosing template.
8225
8226 Suppose that TT is later substituted with std::vector. The above
8227 instantiation is `TT<int, std::allocator<T> >' with TT at
8228 level 1, and T at level 2, while the template arguments at level 1
8229 becomes {std::vector} and the inner level 2 is {int}. */
8230
8231 outer = DECL_CONTEXT (templ);
8232 if (outer)
8233 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8234 else if (current_template_parms)
8235 {
8236 /* This is an argument of the current template, so we haven't set
8237 DECL_CONTEXT yet. */
8238 tree relevant_template_parms;
8239
8240 /* Parameter levels that are greater than the level of the given
8241 template template parm are irrelevant. */
8242 relevant_template_parms = current_template_parms;
8243 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8244 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8245 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8246
8247 outer = template_parms_to_args (relevant_template_parms);
8248 }
8249
8250 if (outer)
8251 arglist = add_to_template_args (outer, arglist);
8252
8253 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8254 complain,
8255 /*require_all_args=*/true,
8256 /*use_default_args=*/true);
8257 if (arglist2 == error_mark_node
8258 || (!uses_template_parms (arglist2)
8259 && check_instantiated_args (templ, arglist2, complain)))
8260 return error_mark_node;
8261
8262 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8263 return parm;
8264 }
8265 else
8266 {
8267 tree template_type = TREE_TYPE (templ);
8268 tree gen_tmpl;
8269 tree type_decl;
8270 tree found = NULL_TREE;
8271 int arg_depth;
8272 int parm_depth;
8273 int is_dependent_type;
8274 int use_partial_inst_tmpl = false;
8275
8276 if (template_type == error_mark_node)
8277 /* An error occurred while building the template TEMPL, and a
8278 diagnostic has most certainly been emitted for that
8279 already. Let's propagate that error. */
8280 return error_mark_node;
8281
8282 gen_tmpl = most_general_template (templ);
8283 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8284 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8285 arg_depth = TMPL_ARGS_DEPTH (arglist);
8286
8287 if (arg_depth == 1 && parm_depth > 1)
8288 {
8289 /* We've been given an incomplete set of template arguments.
8290 For example, given:
8291
8292 template <class T> struct S1 {
8293 template <class U> struct S2 {};
8294 template <class U> struct S2<U*> {};
8295 };
8296
8297 we will be called with an ARGLIST of `U*', but the
8298 TEMPLATE will be `template <class T> template
8299 <class U> struct S1<T>::S2'. We must fill in the missing
8300 arguments. */
8301 arglist
8302 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8303 arglist);
8304 arg_depth = TMPL_ARGS_DEPTH (arglist);
8305 }
8306
8307 /* Now we should have enough arguments. */
8308 gcc_assert (parm_depth == arg_depth);
8309
8310 /* From here on, we're only interested in the most general
8311 template. */
8312
8313 /* Calculate the BOUND_ARGS. These will be the args that are
8314 actually tsubst'd into the definition to create the
8315 instantiation. */
8316 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8317 complain,
8318 /*require_all_args=*/true,
8319 /*use_default_args=*/true);
8320
8321 if (arglist == error_mark_node)
8322 /* We were unable to bind the arguments. */
8323 return error_mark_node;
8324
8325 /* In the scope of a template class, explicit references to the
8326 template class refer to the type of the template, not any
8327 instantiation of it. For example, in:
8328
8329 template <class T> class C { void f(C<T>); }
8330
8331 the `C<T>' is just the same as `C'. Outside of the
8332 class, however, such a reference is an instantiation. */
8333 if ((entering_scope
8334 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8335 || currently_open_class (template_type))
8336 /* comp_template_args is expensive, check it last. */
8337 && comp_template_args (TYPE_TI_ARGS (template_type),
8338 arglist))
8339 return template_type;
8340
8341 /* If we already have this specialization, return it. */
8342 elt.tmpl = gen_tmpl;
8343 elt.args = arglist;
8344 elt.spec = NULL_TREE;
8345 hash = spec_hasher::hash (&elt);
8346 entry = type_specializations->find_with_hash (&elt, hash);
8347
8348 if (entry)
8349 return entry->spec;
8350
8351 /* If the the template's constraints are not satisfied,
8352 then we cannot form a valid type.
8353
8354 Note that the check is deferred until after the hash
8355 lookup. This prevents redundant checks on previously
8356 instantiated specializations. */
8357 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8358 {
8359 if (complain & tf_error)
8360 {
8361 error ("template constraint failure");
8362 diagnose_constraints (input_location, gen_tmpl, arglist);
8363 }
8364 return error_mark_node;
8365 }
8366
8367 is_dependent_type = uses_template_parms (arglist);
8368
8369 /* If the deduced arguments are invalid, then the binding
8370 failed. */
8371 if (!is_dependent_type
8372 && check_instantiated_args (gen_tmpl,
8373 INNERMOST_TEMPLATE_ARGS (arglist),
8374 complain))
8375 return error_mark_node;
8376
8377 if (!is_dependent_type
8378 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8379 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8380 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8381 {
8382 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8383 DECL_NAME (gen_tmpl),
8384 /*tag_scope=*/ts_global);
8385 return found;
8386 }
8387
8388 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8389 complain, in_decl);
8390 if (context == error_mark_node)
8391 return error_mark_node;
8392
8393 if (!context)
8394 context = global_namespace;
8395
8396 /* Create the type. */
8397 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8398 {
8399 /* The user referred to a specialization of an alias
8400 template represented by GEN_TMPL.
8401
8402 [temp.alias]/2 says:
8403
8404 When a template-id refers to the specialization of an
8405 alias template, it is equivalent to the associated
8406 type obtained by substitution of its
8407 template-arguments for the template-parameters in the
8408 type-id of the alias template. */
8409
8410 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8411 /* Note that the call above (by indirectly calling
8412 register_specialization in tsubst_decl) registers the
8413 TYPE_DECL representing the specialization of the alias
8414 template. So next time someone substitutes ARGLIST for
8415 the template parms into the alias template (GEN_TMPL),
8416 she'll get that TYPE_DECL back. */
8417
8418 if (t == error_mark_node)
8419 return t;
8420 }
8421 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8422 {
8423 if (!is_dependent_type)
8424 {
8425 set_current_access_from_decl (TYPE_NAME (template_type));
8426 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8427 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8428 arglist, complain, in_decl),
8429 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8430 arglist, complain, in_decl),
8431 SCOPED_ENUM_P (template_type), NULL);
8432
8433 if (t == error_mark_node)
8434 return t;
8435 }
8436 else
8437 {
8438 /* We don't want to call start_enum for this type, since
8439 the values for the enumeration constants may involve
8440 template parameters. And, no one should be interested
8441 in the enumeration constants for such a type. */
8442 t = cxx_make_type (ENUMERAL_TYPE);
8443 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8444 }
8445 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8446 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8447 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8448 }
8449 else if (CLASS_TYPE_P (template_type))
8450 {
8451 t = make_class_type (TREE_CODE (template_type));
8452 CLASSTYPE_DECLARED_CLASS (t)
8453 = CLASSTYPE_DECLARED_CLASS (template_type);
8454 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8455 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
8456
8457 /* A local class. Make sure the decl gets registered properly. */
8458 if (context == current_function_decl)
8459 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8460
8461 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8462 /* This instantiation is another name for the primary
8463 template type. Set the TYPE_CANONICAL field
8464 appropriately. */
8465 TYPE_CANONICAL (t) = template_type;
8466 else if (any_template_arguments_need_structural_equality_p (arglist))
8467 /* Some of the template arguments require structural
8468 equality testing, so this template class requires
8469 structural equality testing. */
8470 SET_TYPE_STRUCTURAL_EQUALITY (t);
8471 }
8472 else
8473 gcc_unreachable ();
8474
8475 /* If we called start_enum or pushtag above, this information
8476 will already be set up. */
8477 if (!TYPE_NAME (t))
8478 {
8479 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8480
8481 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8482 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8483 DECL_SOURCE_LOCATION (type_decl)
8484 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8485 }
8486 else
8487 type_decl = TYPE_NAME (t);
8488
8489 if (CLASS_TYPE_P (template_type))
8490 {
8491 TREE_PRIVATE (type_decl)
8492 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8493 TREE_PROTECTED (type_decl)
8494 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8495 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8496 {
8497 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8498 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8499 }
8500 }
8501
8502 if (OVERLOAD_TYPE_P (t)
8503 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8504 {
8505 static const char *tags[] = {"abi_tag", "may_alias"};
8506
8507 for (unsigned ix = 0; ix != 2; ix++)
8508 {
8509 tree attributes
8510 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8511
8512 if (attributes)
8513 TYPE_ATTRIBUTES (t)
8514 = tree_cons (TREE_PURPOSE (attributes),
8515 TREE_VALUE (attributes),
8516 TYPE_ATTRIBUTES (t));
8517 }
8518 }
8519
8520 /* Let's consider the explicit specialization of a member
8521 of a class template specialization that is implicitly instantiated,
8522 e.g.:
8523 template<class T>
8524 struct S
8525 {
8526 template<class U> struct M {}; //#0
8527 };
8528
8529 template<>
8530 template<>
8531 struct S<int>::M<char> //#1
8532 {
8533 int i;
8534 };
8535 [temp.expl.spec]/4 says this is valid.
8536
8537 In this case, when we write:
8538 S<int>::M<char> m;
8539
8540 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8541 the one of #0.
8542
8543 When we encounter #1, we want to store the partial instantiation
8544 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8545
8546 For all cases other than this "explicit specialization of member of a
8547 class template", we just want to store the most general template into
8548 the CLASSTYPE_TI_TEMPLATE of M.
8549
8550 This case of "explicit specialization of member of a class template"
8551 only happens when:
8552 1/ the enclosing class is an instantiation of, and therefore not
8553 the same as, the context of the most general template, and
8554 2/ we aren't looking at the partial instantiation itself, i.e.
8555 the innermost arguments are not the same as the innermost parms of
8556 the most general template.
8557
8558 So it's only when 1/ and 2/ happens that we want to use the partial
8559 instantiation of the member template in lieu of its most general
8560 template. */
8561
8562 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8563 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8564 /* the enclosing class must be an instantiation... */
8565 && CLASS_TYPE_P (context)
8566 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8567 {
8568 tree partial_inst_args;
8569 TREE_VEC_LENGTH (arglist)--;
8570 ++processing_template_decl;
8571 partial_inst_args =
8572 tsubst (INNERMOST_TEMPLATE_ARGS
8573 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8574 arglist, complain, NULL_TREE);
8575 --processing_template_decl;
8576 TREE_VEC_LENGTH (arglist)++;
8577 if (partial_inst_args == error_mark_node)
8578 return error_mark_node;
8579 use_partial_inst_tmpl =
8580 /*...and we must not be looking at the partial instantiation
8581 itself. */
8582 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8583 partial_inst_args);
8584 }
8585
8586 if (!use_partial_inst_tmpl)
8587 /* This case is easy; there are no member templates involved. */
8588 found = gen_tmpl;
8589 else
8590 {
8591 /* This is a full instantiation of a member template. Find
8592 the partial instantiation of which this is an instance. */
8593
8594 /* Temporarily reduce by one the number of levels in the ARGLIST
8595 so as to avoid comparing the last set of arguments. */
8596 TREE_VEC_LENGTH (arglist)--;
8597 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8598 TREE_VEC_LENGTH (arglist)++;
8599 /* FOUND is either a proper class type, or an alias
8600 template specialization. In the later case, it's a
8601 TYPE_DECL, resulting from the substituting of arguments
8602 for parameters in the TYPE_DECL of the alias template
8603 done earlier. So be careful while getting the template
8604 of FOUND. */
8605 found = TREE_CODE (found) == TEMPLATE_DECL
8606 ? found
8607 : TREE_CODE (found) == TYPE_DECL
8608 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8609 : CLASSTYPE_TI_TEMPLATE (found);
8610 }
8611
8612 // Build template info for the new specialization.
8613 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8614
8615 elt.spec = t;
8616 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8617 entry = ggc_alloc<spec_entry> ();
8618 *entry = elt;
8619 *slot = entry;
8620
8621 /* Note this use of the partial instantiation so we can check it
8622 later in maybe_process_partial_specialization. */
8623 DECL_TEMPLATE_INSTANTIATIONS (found)
8624 = tree_cons (arglist, t,
8625 DECL_TEMPLATE_INSTANTIATIONS (found));
8626
8627 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8628 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8629 /* Now that the type has been registered on the instantiations
8630 list, we set up the enumerators. Because the enumeration
8631 constants may involve the enumeration type itself, we make
8632 sure to register the type first, and then create the
8633 constants. That way, doing tsubst_expr for the enumeration
8634 constants won't result in recursive calls here; we'll find
8635 the instantiation and exit above. */
8636 tsubst_enum (template_type, t, arglist);
8637
8638 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8639 /* If the type makes use of template parameters, the
8640 code that generates debugging information will crash. */
8641 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8642
8643 /* Possibly limit visibility based on template args. */
8644 TREE_PUBLIC (type_decl) = 1;
8645 determine_visibility (type_decl);
8646
8647 inherit_targ_abi_tags (t);
8648
8649 return t;
8650 }
8651 }
8652
8653 /* Wrapper for lookup_template_class_1. */
8654
8655 tree
8656 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8657 int entering_scope, tsubst_flags_t complain)
8658 {
8659 tree ret;
8660 timevar_push (TV_TEMPLATE_INST);
8661 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8662 entering_scope, complain);
8663 timevar_pop (TV_TEMPLATE_INST);
8664 return ret;
8665 }
8666
8667 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8668
8669 tree
8670 lookup_template_variable (tree templ, tree arglist)
8671 {
8672 /* The type of the expression is NULL_TREE since the template-id could refer
8673 to an explicit or partial specialization. */
8674 tree type = NULL_TREE;
8675 if (flag_concepts && variable_concept_p (templ))
8676 /* Except that concepts are always bool. */
8677 type = boolean_type_node;
8678 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8679 }
8680
8681 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8682
8683 tree
8684 finish_template_variable (tree var, tsubst_flags_t complain)
8685 {
8686 tree templ = TREE_OPERAND (var, 0);
8687 tree arglist = TREE_OPERAND (var, 1);
8688
8689 /* We never want to return a VAR_DECL for a variable concept, since they
8690 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8691 bool concept_p = flag_concepts && variable_concept_p (templ);
8692 if (concept_p && processing_template_decl)
8693 return var;
8694
8695 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8696 arglist = add_outermost_template_args (tmpl_args, arglist);
8697
8698 tree parms = DECL_TEMPLATE_PARMS (templ);
8699 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8700 /*req_all*/true,
8701 /*use_default*/true);
8702
8703 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
8704 {
8705 if (complain & tf_error)
8706 {
8707 error ("use of invalid variable template %qE", var);
8708 diagnose_constraints (location_of (var), templ, arglist);
8709 }
8710 return error_mark_node;
8711 }
8712
8713 /* If a template-id refers to a specialization of a variable
8714 concept, then the expression is true if and only if the
8715 concept's constraints are satisfied by the given template
8716 arguments.
8717
8718 NOTE: This is an extension of Concepts Lite TS that
8719 allows constraints to be used in expressions. */
8720 if (concept_p)
8721 {
8722 tree decl = DECL_TEMPLATE_RESULT (templ);
8723 return evaluate_variable_concept (decl, arglist);
8724 }
8725
8726 return instantiate_template (templ, arglist, complain);
8727 }
8728
8729 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
8730 TARGS template args, and instantiate it if it's not dependent. */
8731
8732 static tree
8733 lookup_and_finish_template_variable (tree templ, tree targs,
8734 tsubst_flags_t complain)
8735 {
8736 templ = lookup_template_variable (templ, targs);
8737 if (!any_dependent_template_arguments_p (targs))
8738 {
8739 templ = finish_template_variable (templ, complain);
8740 mark_used (templ);
8741 }
8742
8743 return convert_from_reference (templ);
8744 }
8745
8746 \f
8747 struct pair_fn_data
8748 {
8749 tree_fn_t fn;
8750 void *data;
8751 /* True when we should also visit template parameters that occur in
8752 non-deduced contexts. */
8753 bool include_nondeduced_p;
8754 hash_set<tree> *visited;
8755 };
8756
8757 /* Called from for_each_template_parm via walk_tree. */
8758
8759 static tree
8760 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8761 {
8762 tree t = *tp;
8763 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8764 tree_fn_t fn = pfd->fn;
8765 void *data = pfd->data;
8766 tree result = NULL_TREE;
8767
8768 #define WALK_SUBTREE(NODE) \
8769 do \
8770 { \
8771 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8772 pfd->include_nondeduced_p); \
8773 if (result) goto out; \
8774 } \
8775 while (0)
8776
8777 if (TYPE_P (t)
8778 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
8779 WALK_SUBTREE (TYPE_CONTEXT (t));
8780
8781 switch (TREE_CODE (t))
8782 {
8783 case RECORD_TYPE:
8784 if (TYPE_PTRMEMFUNC_P (t))
8785 break;
8786 /* Fall through. */
8787
8788 case UNION_TYPE:
8789 case ENUMERAL_TYPE:
8790 if (!TYPE_TEMPLATE_INFO (t))
8791 *walk_subtrees = 0;
8792 else
8793 WALK_SUBTREE (TYPE_TI_ARGS (t));
8794 break;
8795
8796 case INTEGER_TYPE:
8797 WALK_SUBTREE (TYPE_MIN_VALUE (t));
8798 WALK_SUBTREE (TYPE_MAX_VALUE (t));
8799 break;
8800
8801 case METHOD_TYPE:
8802 /* Since we're not going to walk subtrees, we have to do this
8803 explicitly here. */
8804 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
8805 /* Fall through. */
8806
8807 case FUNCTION_TYPE:
8808 /* Check the return type. */
8809 WALK_SUBTREE (TREE_TYPE (t));
8810
8811 /* Check the parameter types. Since default arguments are not
8812 instantiated until they are needed, the TYPE_ARG_TYPES may
8813 contain expressions that involve template parameters. But,
8814 no-one should be looking at them yet. And, once they're
8815 instantiated, they don't contain template parameters, so
8816 there's no point in looking at them then, either. */
8817 {
8818 tree parm;
8819
8820 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8821 WALK_SUBTREE (TREE_VALUE (parm));
8822
8823 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8824 want walk_tree walking into them itself. */
8825 *walk_subtrees = 0;
8826 }
8827 break;
8828
8829 case TYPEOF_TYPE:
8830 case UNDERLYING_TYPE:
8831 if (pfd->include_nondeduced_p
8832 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8833 pfd->visited,
8834 pfd->include_nondeduced_p))
8835 return error_mark_node;
8836 break;
8837
8838 case FUNCTION_DECL:
8839 case VAR_DECL:
8840 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8841 WALK_SUBTREE (DECL_TI_ARGS (t));
8842 /* Fall through. */
8843
8844 case PARM_DECL:
8845 case CONST_DECL:
8846 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
8847 WALK_SUBTREE (DECL_INITIAL (t));
8848 if (DECL_CONTEXT (t)
8849 && pfd->include_nondeduced_p)
8850 WALK_SUBTREE (DECL_CONTEXT (t));
8851 break;
8852
8853 case BOUND_TEMPLATE_TEMPLATE_PARM:
8854 /* Record template parameters such as `T' inside `TT<T>'. */
8855 WALK_SUBTREE (TYPE_TI_ARGS (t));
8856 /* Fall through. */
8857
8858 case TEMPLATE_TEMPLATE_PARM:
8859 case TEMPLATE_TYPE_PARM:
8860 case TEMPLATE_PARM_INDEX:
8861 if (fn && (*fn)(t, data))
8862 return t;
8863 else if (!fn)
8864 return t;
8865 break;
8866
8867 case TEMPLATE_DECL:
8868 /* A template template parameter is encountered. */
8869 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8870 WALK_SUBTREE (TREE_TYPE (t));
8871
8872 /* Already substituted template template parameter */
8873 *walk_subtrees = 0;
8874 break;
8875
8876 case TYPENAME_TYPE:
8877 /* A template-id in a TYPENAME_TYPE might be a deduced context after
8878 partial instantiation. */
8879 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
8880 break;
8881
8882 case CONSTRUCTOR:
8883 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8884 && pfd->include_nondeduced_p)
8885 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
8886 break;
8887
8888 case INDIRECT_REF:
8889 case COMPONENT_REF:
8890 /* If there's no type, then this thing must be some expression
8891 involving template parameters. */
8892 if (!fn && !TREE_TYPE (t))
8893 return error_mark_node;
8894 break;
8895
8896 case MODOP_EXPR:
8897 case CAST_EXPR:
8898 case IMPLICIT_CONV_EXPR:
8899 case REINTERPRET_CAST_EXPR:
8900 case CONST_CAST_EXPR:
8901 case STATIC_CAST_EXPR:
8902 case DYNAMIC_CAST_EXPR:
8903 case ARROW_EXPR:
8904 case DOTSTAR_EXPR:
8905 case TYPEID_EXPR:
8906 case PSEUDO_DTOR_EXPR:
8907 if (!fn)
8908 return error_mark_node;
8909 break;
8910
8911 default:
8912 break;
8913 }
8914
8915 #undef WALK_SUBTREE
8916
8917 /* We didn't find any template parameters we liked. */
8918 out:
8919 return result;
8920 }
8921
8922 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8923 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8924 call FN with the parameter and the DATA.
8925 If FN returns nonzero, the iteration is terminated, and
8926 for_each_template_parm returns 1. Otherwise, the iteration
8927 continues. If FN never returns a nonzero value, the value
8928 returned by for_each_template_parm is 0. If FN is NULL, it is
8929 considered to be the function which always returns 1.
8930
8931 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8932 parameters that occur in non-deduced contexts. When false, only
8933 visits those template parameters that can be deduced. */
8934
8935 static tree
8936 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8937 hash_set<tree> *visited,
8938 bool include_nondeduced_p)
8939 {
8940 struct pair_fn_data pfd;
8941 tree result;
8942
8943 /* Set up. */
8944 pfd.fn = fn;
8945 pfd.data = data;
8946 pfd.include_nondeduced_p = include_nondeduced_p;
8947
8948 /* Walk the tree. (Conceptually, we would like to walk without
8949 duplicates, but for_each_template_parm_r recursively calls
8950 for_each_template_parm, so we would need to reorganize a fair
8951 bit to use walk_tree_without_duplicates, so we keep our own
8952 visited list.) */
8953 if (visited)
8954 pfd.visited = visited;
8955 else
8956 pfd.visited = new hash_set<tree>;
8957 result = cp_walk_tree (&t,
8958 for_each_template_parm_r,
8959 &pfd,
8960 pfd.visited);
8961
8962 /* Clean up. */
8963 if (!visited)
8964 {
8965 delete pfd.visited;
8966 pfd.visited = 0;
8967 }
8968
8969 return result;
8970 }
8971
8972 /* Returns true if T depends on any template parameter. */
8973
8974 int
8975 uses_template_parms (tree t)
8976 {
8977 if (t == NULL_TREE)
8978 return false;
8979
8980 bool dependent_p;
8981 int saved_processing_template_decl;
8982
8983 saved_processing_template_decl = processing_template_decl;
8984 if (!saved_processing_template_decl)
8985 processing_template_decl = 1;
8986 if (TYPE_P (t))
8987 dependent_p = dependent_type_p (t);
8988 else if (TREE_CODE (t) == TREE_VEC)
8989 dependent_p = any_dependent_template_arguments_p (t);
8990 else if (TREE_CODE (t) == TREE_LIST)
8991 dependent_p = (uses_template_parms (TREE_VALUE (t))
8992 || uses_template_parms (TREE_CHAIN (t)));
8993 else if (TREE_CODE (t) == TYPE_DECL)
8994 dependent_p = dependent_type_p (TREE_TYPE (t));
8995 else if (DECL_P (t)
8996 || EXPR_P (t)
8997 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8998 || TREE_CODE (t) == OVERLOAD
8999 || BASELINK_P (t)
9000 || identifier_p (t)
9001 || TREE_CODE (t) == TRAIT_EXPR
9002 || TREE_CODE (t) == CONSTRUCTOR
9003 || CONSTANT_CLASS_P (t))
9004 dependent_p = (type_dependent_expression_p (t)
9005 || value_dependent_expression_p (t));
9006 else
9007 {
9008 gcc_assert (t == error_mark_node);
9009 dependent_p = false;
9010 }
9011
9012 processing_template_decl = saved_processing_template_decl;
9013
9014 return dependent_p;
9015 }
9016
9017 /* Returns true iff current_function_decl is an incompletely instantiated
9018 template. Useful instead of processing_template_decl because the latter
9019 is set to 0 during instantiate_non_dependent_expr. */
9020
9021 bool
9022 in_template_function (void)
9023 {
9024 tree fn = current_function_decl;
9025 bool ret;
9026 ++processing_template_decl;
9027 ret = (fn && DECL_LANG_SPECIFIC (fn)
9028 && DECL_TEMPLATE_INFO (fn)
9029 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9030 --processing_template_decl;
9031 return ret;
9032 }
9033
9034 /* Returns true if T depends on any template parameter with level LEVEL. */
9035
9036 bool
9037 uses_template_parms_level (tree t, int level)
9038 {
9039 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9040 /*include_nondeduced_p=*/true);
9041 }
9042
9043 /* Returns true if the signature of DECL depends on any template parameter from
9044 its enclosing class. */
9045
9046 bool
9047 uses_outer_template_parms (tree decl)
9048 {
9049 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9050 if (depth == 0)
9051 return false;
9052 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9053 &depth, NULL, /*include_nondeduced_p=*/true))
9054 return true;
9055 if (PRIMARY_TEMPLATE_P (decl)
9056 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9057 (DECL_TEMPLATE_PARMS (decl)),
9058 template_parm_outer_level,
9059 &depth, NULL, /*include_nondeduced_p=*/true))
9060 return true;
9061 tree ci = get_constraints (decl);
9062 if (ci)
9063 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9064 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9065 &depth, NULL, /*nondeduced*/true))
9066 return true;
9067 return false;
9068 }
9069
9070 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9071 ill-formed translation unit, i.e. a variable or function that isn't
9072 usable in a constant expression. */
9073
9074 static inline bool
9075 neglectable_inst_p (tree d)
9076 {
9077 return (DECL_P (d)
9078 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9079 : decl_maybe_constant_var_p (d)));
9080 }
9081
9082 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9083 neglectable and instantiated from within an erroneous instantiation. */
9084
9085 static bool
9086 limit_bad_template_recursion (tree decl)
9087 {
9088 struct tinst_level *lev = current_tinst_level;
9089 int errs = errorcount + sorrycount;
9090 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9091 return false;
9092
9093 for (; lev; lev = lev->next)
9094 if (neglectable_inst_p (lev->decl))
9095 break;
9096
9097 return (lev && errs > lev->errors);
9098 }
9099
9100 static int tinst_depth;
9101 extern int max_tinst_depth;
9102 int depth_reached;
9103
9104 static GTY(()) struct tinst_level *last_error_tinst_level;
9105
9106 /* We're starting to instantiate D; record the template instantiation context
9107 for diagnostics and to restore it later. */
9108
9109 bool
9110 push_tinst_level (tree d)
9111 {
9112 return push_tinst_level_loc (d, input_location);
9113 }
9114
9115 /* We're starting to instantiate D; record the template instantiation context
9116 at LOC for diagnostics and to restore it later. */
9117
9118 bool
9119 push_tinst_level_loc (tree d, location_t loc)
9120 {
9121 struct tinst_level *new_level;
9122
9123 if (tinst_depth >= max_tinst_depth)
9124 {
9125 /* Tell error.c not to try to instantiate any templates. */
9126 at_eof = 2;
9127 fatal_error (input_location,
9128 "template instantiation depth exceeds maximum of %d"
9129 " (use -ftemplate-depth= to increase the maximum)",
9130 max_tinst_depth);
9131 return false;
9132 }
9133
9134 /* If the current instantiation caused problems, don't let it instantiate
9135 anything else. Do allow deduction substitution and decls usable in
9136 constant expressions. */
9137 if (limit_bad_template_recursion (d))
9138 return false;
9139
9140 new_level = ggc_alloc<tinst_level> ();
9141 new_level->decl = d;
9142 new_level->locus = loc;
9143 new_level->errors = errorcount+sorrycount;
9144 new_level->in_system_header_p = in_system_header_at (input_location);
9145 new_level->next = current_tinst_level;
9146 current_tinst_level = new_level;
9147
9148 ++tinst_depth;
9149 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9150 depth_reached = tinst_depth;
9151
9152 return true;
9153 }
9154
9155 /* We're done instantiating this template; return to the instantiation
9156 context. */
9157
9158 void
9159 pop_tinst_level (void)
9160 {
9161 /* Restore the filename and line number stashed away when we started
9162 this instantiation. */
9163 input_location = current_tinst_level->locus;
9164 current_tinst_level = current_tinst_level->next;
9165 --tinst_depth;
9166 }
9167
9168 /* We're instantiating a deferred template; restore the template
9169 instantiation context in which the instantiation was requested, which
9170 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9171
9172 static tree
9173 reopen_tinst_level (struct tinst_level *level)
9174 {
9175 struct tinst_level *t;
9176
9177 tinst_depth = 0;
9178 for (t = level; t; t = t->next)
9179 ++tinst_depth;
9180
9181 current_tinst_level = level;
9182 pop_tinst_level ();
9183 if (current_tinst_level)
9184 current_tinst_level->errors = errorcount+sorrycount;
9185 return level->decl;
9186 }
9187
9188 /* Returns the TINST_LEVEL which gives the original instantiation
9189 context. */
9190
9191 struct tinst_level *
9192 outermost_tinst_level (void)
9193 {
9194 struct tinst_level *level = current_tinst_level;
9195 if (level)
9196 while (level->next)
9197 level = level->next;
9198 return level;
9199 }
9200
9201 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9202 vector of template arguments, as for tsubst.
9203
9204 Returns an appropriate tsubst'd friend declaration. */
9205
9206 static tree
9207 tsubst_friend_function (tree decl, tree args)
9208 {
9209 tree new_friend;
9210
9211 if (TREE_CODE (decl) == FUNCTION_DECL
9212 && DECL_TEMPLATE_INSTANTIATION (decl)
9213 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9214 /* This was a friend declared with an explicit template
9215 argument list, e.g.:
9216
9217 friend void f<>(T);
9218
9219 to indicate that f was a template instantiation, not a new
9220 function declaration. Now, we have to figure out what
9221 instantiation of what template. */
9222 {
9223 tree template_id, arglist, fns;
9224 tree new_args;
9225 tree tmpl;
9226 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9227
9228 /* Friend functions are looked up in the containing namespace scope.
9229 We must enter that scope, to avoid finding member functions of the
9230 current class with same name. */
9231 push_nested_namespace (ns);
9232 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9233 tf_warning_or_error, NULL_TREE,
9234 /*integral_constant_expression_p=*/false);
9235 pop_nested_namespace (ns);
9236 arglist = tsubst (DECL_TI_ARGS (decl), args,
9237 tf_warning_or_error, NULL_TREE);
9238 template_id = lookup_template_function (fns, arglist);
9239
9240 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9241 tmpl = determine_specialization (template_id, new_friend,
9242 &new_args,
9243 /*need_member_template=*/0,
9244 TREE_VEC_LENGTH (args),
9245 tsk_none);
9246 return instantiate_template (tmpl, new_args, tf_error);
9247 }
9248
9249 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9250
9251 /* The NEW_FRIEND will look like an instantiation, to the
9252 compiler, but is not an instantiation from the point of view of
9253 the language. For example, we might have had:
9254
9255 template <class T> struct S {
9256 template <class U> friend void f(T, U);
9257 };
9258
9259 Then, in S<int>, template <class U> void f(int, U) is not an
9260 instantiation of anything. */
9261 if (new_friend == error_mark_node)
9262 return error_mark_node;
9263
9264 DECL_USE_TEMPLATE (new_friend) = 0;
9265 if (TREE_CODE (decl) == TEMPLATE_DECL)
9266 {
9267 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9268 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9269 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9270 }
9271
9272 /* The mangled name for the NEW_FRIEND is incorrect. The function
9273 is not a template instantiation and should not be mangled like
9274 one. Therefore, we forget the mangling here; we'll recompute it
9275 later if we need it. */
9276 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9277 {
9278 SET_DECL_RTL (new_friend, NULL);
9279 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9280 }
9281
9282 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9283 {
9284 tree old_decl;
9285 tree new_friend_template_info;
9286 tree new_friend_result_template_info;
9287 tree ns;
9288 int new_friend_is_defn;
9289
9290 /* We must save some information from NEW_FRIEND before calling
9291 duplicate decls since that function will free NEW_FRIEND if
9292 possible. */
9293 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9294 new_friend_is_defn =
9295 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9296 (template_for_substitution (new_friend)))
9297 != NULL_TREE);
9298 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9299 {
9300 /* This declaration is a `primary' template. */
9301 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9302
9303 new_friend_result_template_info
9304 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9305 }
9306 else
9307 new_friend_result_template_info = NULL_TREE;
9308
9309 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9310 if (new_friend_is_defn)
9311 DECL_INITIAL (new_friend) = error_mark_node;
9312
9313 /* Inside pushdecl_namespace_level, we will push into the
9314 current namespace. However, the friend function should go
9315 into the namespace of the template. */
9316 ns = decl_namespace_context (new_friend);
9317 push_nested_namespace (ns);
9318 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9319 pop_nested_namespace (ns);
9320
9321 if (old_decl == error_mark_node)
9322 return error_mark_node;
9323
9324 if (old_decl != new_friend)
9325 {
9326 /* This new friend declaration matched an existing
9327 declaration. For example, given:
9328
9329 template <class T> void f(T);
9330 template <class U> class C {
9331 template <class T> friend void f(T) {}
9332 };
9333
9334 the friend declaration actually provides the definition
9335 of `f', once C has been instantiated for some type. So,
9336 old_decl will be the out-of-class template declaration,
9337 while new_friend is the in-class definition.
9338
9339 But, if `f' was called before this point, the
9340 instantiation of `f' will have DECL_TI_ARGS corresponding
9341 to `T' but not to `U', references to which might appear
9342 in the definition of `f'. Previously, the most general
9343 template for an instantiation of `f' was the out-of-class
9344 version; now it is the in-class version. Therefore, we
9345 run through all specialization of `f', adding to their
9346 DECL_TI_ARGS appropriately. In particular, they need a
9347 new set of outer arguments, corresponding to the
9348 arguments for this class instantiation.
9349
9350 The same situation can arise with something like this:
9351
9352 friend void f(int);
9353 template <class T> class C {
9354 friend void f(T) {}
9355 };
9356
9357 when `C<int>' is instantiated. Now, `f(int)' is defined
9358 in the class. */
9359
9360 if (!new_friend_is_defn)
9361 /* On the other hand, if the in-class declaration does
9362 *not* provide a definition, then we don't want to alter
9363 existing definitions. We can just leave everything
9364 alone. */
9365 ;
9366 else
9367 {
9368 tree new_template = TI_TEMPLATE (new_friend_template_info);
9369 tree new_args = TI_ARGS (new_friend_template_info);
9370
9371 /* Overwrite whatever template info was there before, if
9372 any, with the new template information pertaining to
9373 the declaration. */
9374 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9375
9376 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9377 {
9378 /* We should have called reregister_specialization in
9379 duplicate_decls. */
9380 gcc_assert (retrieve_specialization (new_template,
9381 new_args, 0)
9382 == old_decl);
9383
9384 /* Instantiate it if the global has already been used. */
9385 if (DECL_ODR_USED (old_decl))
9386 instantiate_decl (old_decl, /*defer_ok=*/true,
9387 /*expl_inst_class_mem_p=*/false);
9388 }
9389 else
9390 {
9391 tree t;
9392
9393 /* Indicate that the old function template is a partial
9394 instantiation. */
9395 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9396 = new_friend_result_template_info;
9397
9398 gcc_assert (new_template
9399 == most_general_template (new_template));
9400 gcc_assert (new_template != old_decl);
9401
9402 /* Reassign any specializations already in the hash table
9403 to the new more general template, and add the
9404 additional template args. */
9405 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9406 t != NULL_TREE;
9407 t = TREE_CHAIN (t))
9408 {
9409 tree spec = TREE_VALUE (t);
9410 spec_entry elt;
9411
9412 elt.tmpl = old_decl;
9413 elt.args = DECL_TI_ARGS (spec);
9414 elt.spec = NULL_TREE;
9415
9416 decl_specializations->remove_elt (&elt);
9417
9418 DECL_TI_ARGS (spec)
9419 = add_outermost_template_args (new_args,
9420 DECL_TI_ARGS (spec));
9421
9422 register_specialization
9423 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9424
9425 }
9426 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9427 }
9428 }
9429
9430 /* The information from NEW_FRIEND has been merged into OLD_DECL
9431 by duplicate_decls. */
9432 new_friend = old_decl;
9433 }
9434 }
9435 else
9436 {
9437 tree context = DECL_CONTEXT (new_friend);
9438 bool dependent_p;
9439
9440 /* In the code
9441 template <class T> class C {
9442 template <class U> friend void C1<U>::f (); // case 1
9443 friend void C2<T>::f (); // case 2
9444 };
9445 we only need to make sure CONTEXT is a complete type for
9446 case 2. To distinguish between the two cases, we note that
9447 CONTEXT of case 1 remains dependent type after tsubst while
9448 this isn't true for case 2. */
9449 ++processing_template_decl;
9450 dependent_p = dependent_type_p (context);
9451 --processing_template_decl;
9452
9453 if (!dependent_p
9454 && !complete_type_or_else (context, NULL_TREE))
9455 return error_mark_node;
9456
9457 if (COMPLETE_TYPE_P (context))
9458 {
9459 tree fn = new_friend;
9460 /* do_friend adds the TEMPLATE_DECL for any member friend
9461 template even if it isn't a member template, i.e.
9462 template <class T> friend A<T>::f();
9463 Look through it in that case. */
9464 if (TREE_CODE (fn) == TEMPLATE_DECL
9465 && !PRIMARY_TEMPLATE_P (fn))
9466 fn = DECL_TEMPLATE_RESULT (fn);
9467 /* Check to see that the declaration is really present, and,
9468 possibly obtain an improved declaration. */
9469 fn = check_classfn (context, fn, NULL_TREE);
9470
9471 if (fn)
9472 new_friend = fn;
9473 }
9474 }
9475
9476 return new_friend;
9477 }
9478
9479 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9480 template arguments, as for tsubst.
9481
9482 Returns an appropriate tsubst'd friend type or error_mark_node on
9483 failure. */
9484
9485 static tree
9486 tsubst_friend_class (tree friend_tmpl, tree args)
9487 {
9488 tree friend_type;
9489 tree tmpl;
9490 tree context;
9491
9492 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9493 {
9494 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9495 return TREE_TYPE (t);
9496 }
9497
9498 context = CP_DECL_CONTEXT (friend_tmpl);
9499
9500 if (context != global_namespace)
9501 {
9502 if (TREE_CODE (context) == NAMESPACE_DECL)
9503 push_nested_namespace (context);
9504 else
9505 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9506 }
9507
9508 /* Look for a class template declaration. We look for hidden names
9509 because two friend declarations of the same template are the
9510 same. For example, in:
9511
9512 struct A {
9513 template <typename> friend class F;
9514 };
9515 template <typename> struct B {
9516 template <typename> friend class F;
9517 };
9518
9519 both F templates are the same. */
9520 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9521 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9522
9523 /* But, if we don't find one, it might be because we're in a
9524 situation like this:
9525
9526 template <class T>
9527 struct S {
9528 template <class U>
9529 friend struct S;
9530 };
9531
9532 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9533 for `S<int>', not the TEMPLATE_DECL. */
9534 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9535 {
9536 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9537 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9538 }
9539
9540 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9541 {
9542 /* The friend template has already been declared. Just
9543 check to see that the declarations match, and install any new
9544 default parameters. We must tsubst the default parameters,
9545 of course. We only need the innermost template parameters
9546 because that is all that redeclare_class_template will look
9547 at. */
9548 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9549 > TMPL_ARGS_DEPTH (args))
9550 {
9551 tree parms;
9552 location_t saved_input_location;
9553 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9554 args, tf_warning_or_error);
9555
9556 saved_input_location = input_location;
9557 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9558 tree cons = get_constraints (tmpl);
9559 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9560 input_location = saved_input_location;
9561
9562 }
9563
9564 friend_type = TREE_TYPE (tmpl);
9565 }
9566 else
9567 {
9568 /* The friend template has not already been declared. In this
9569 case, the instantiation of the template class will cause the
9570 injection of this template into the global scope. */
9571 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9572 if (tmpl == error_mark_node)
9573 return error_mark_node;
9574
9575 /* The new TMPL is not an instantiation of anything, so we
9576 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9577 the new type because that is supposed to be the corresponding
9578 template decl, i.e., TMPL. */
9579 DECL_USE_TEMPLATE (tmpl) = 0;
9580 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9581 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9582 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9583 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9584
9585 /* Inject this template into the global scope. */
9586 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9587 }
9588
9589 if (context != global_namespace)
9590 {
9591 if (TREE_CODE (context) == NAMESPACE_DECL)
9592 pop_nested_namespace (context);
9593 else
9594 pop_nested_class ();
9595 }
9596
9597 return friend_type;
9598 }
9599
9600 /* Returns zero if TYPE cannot be completed later due to circularity.
9601 Otherwise returns one. */
9602
9603 static int
9604 can_complete_type_without_circularity (tree type)
9605 {
9606 if (type == NULL_TREE || type == error_mark_node)
9607 return 0;
9608 else if (COMPLETE_TYPE_P (type))
9609 return 1;
9610 else if (TREE_CODE (type) == ARRAY_TYPE)
9611 return can_complete_type_without_circularity (TREE_TYPE (type));
9612 else if (CLASS_TYPE_P (type)
9613 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9614 return 0;
9615 else
9616 return 1;
9617 }
9618
9619 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9620 tsubst_flags_t, tree);
9621
9622 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9623 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9624
9625 static tree
9626 tsubst_attribute (tree t, tree *decl_p, tree args,
9627 tsubst_flags_t complain, tree in_decl)
9628 {
9629 gcc_assert (ATTR_IS_DEPENDENT (t));
9630
9631 tree val = TREE_VALUE (t);
9632 if (val == NULL_TREE)
9633 /* Nothing to do. */;
9634 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9635 && is_attribute_p ("omp declare simd",
9636 get_attribute_name (t)))
9637 {
9638 tree clauses = TREE_VALUE (val);
9639 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9640 complain, in_decl);
9641 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9642 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9643 tree parms = DECL_ARGUMENTS (*decl_p);
9644 clauses
9645 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9646 if (clauses)
9647 val = build_tree_list (NULL_TREE, clauses);
9648 else
9649 val = NULL_TREE;
9650 }
9651 /* If the first attribute argument is an identifier, don't
9652 pass it through tsubst. Attributes like mode, format,
9653 cleanup and several target specific attributes expect it
9654 unmodified. */
9655 else if (attribute_takes_identifier_p (get_attribute_name (t)))
9656 {
9657 tree chain
9658 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
9659 /*integral_constant_expression_p=*/false);
9660 if (chain != TREE_CHAIN (val))
9661 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
9662 }
9663 else if (PACK_EXPANSION_P (val))
9664 {
9665 /* An attribute pack expansion. */
9666 tree purp = TREE_PURPOSE (t);
9667 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
9668 int len = TREE_VEC_LENGTH (pack);
9669 tree list = NULL_TREE;
9670 tree *q = &list;
9671 for (int i = 0; i < len; ++i)
9672 {
9673 tree elt = TREE_VEC_ELT (pack, i);
9674 *q = build_tree_list (purp, elt);
9675 q = &TREE_CHAIN (*q);
9676 }
9677 return list;
9678 }
9679 else
9680 val = tsubst_expr (val, args, complain, in_decl,
9681 /*integral_constant_expression_p=*/false);
9682
9683 if (val != TREE_VALUE (t))
9684 return build_tree_list (TREE_PURPOSE (t), val);
9685 return t;
9686 }
9687
9688 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
9689 unchanged or a new TREE_LIST chain. */
9690
9691 static tree
9692 tsubst_attributes (tree attributes, tree args,
9693 tsubst_flags_t complain, tree in_decl)
9694 {
9695 tree last_dep = NULL_TREE;
9696
9697 for (tree t = attributes; t; t = TREE_CHAIN (t))
9698 if (ATTR_IS_DEPENDENT (t))
9699 {
9700 last_dep = t;
9701 attributes = copy_list (attributes);
9702 break;
9703 }
9704
9705 if (last_dep)
9706 for (tree *p = &attributes; *p; )
9707 {
9708 tree t = *p;
9709 if (ATTR_IS_DEPENDENT (t))
9710 {
9711 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
9712 if (subst != t)
9713 {
9714 *p = subst;
9715 do
9716 p = &TREE_CHAIN (*p);
9717 while (*p);
9718 *p = TREE_CHAIN (t);
9719 continue;
9720 }
9721 }
9722 p = &TREE_CHAIN (*p);
9723 }
9724
9725 return attributes;
9726 }
9727
9728 /* Apply any attributes which had to be deferred until instantiation
9729 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9730 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9731
9732 static void
9733 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9734 tree args, tsubst_flags_t complain, tree in_decl)
9735 {
9736 tree last_dep = NULL_TREE;
9737 tree t;
9738 tree *p;
9739
9740 for (t = attributes; t; t = TREE_CHAIN (t))
9741 if (ATTR_IS_DEPENDENT (t))
9742 {
9743 last_dep = t;
9744 attributes = copy_list (attributes);
9745 break;
9746 }
9747
9748 if (DECL_P (*decl_p))
9749 {
9750 if (TREE_TYPE (*decl_p) == error_mark_node)
9751 return;
9752 p = &DECL_ATTRIBUTES (*decl_p);
9753 }
9754 else
9755 p = &TYPE_ATTRIBUTES (*decl_p);
9756
9757 if (last_dep)
9758 {
9759 tree late_attrs = NULL_TREE;
9760 tree *q = &late_attrs;
9761
9762 for (*p = attributes; *p; )
9763 {
9764 t = *p;
9765 if (ATTR_IS_DEPENDENT (t))
9766 {
9767 *p = TREE_CHAIN (t);
9768 TREE_CHAIN (t) = NULL_TREE;
9769 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
9770 do
9771 q = &TREE_CHAIN (*q);
9772 while (*q);
9773 }
9774 else
9775 p = &TREE_CHAIN (t);
9776 }
9777
9778 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9779 }
9780 }
9781
9782 /* Perform (or defer) access check for typedefs that were referenced
9783 from within the template TMPL code.
9784 This is a subroutine of instantiate_decl and instantiate_class_template.
9785 TMPL is the template to consider and TARGS is the list of arguments of
9786 that template. */
9787
9788 static void
9789 perform_typedefs_access_check (tree tmpl, tree targs)
9790 {
9791 location_t saved_location;
9792 unsigned i;
9793 qualified_typedef_usage_t *iter;
9794
9795 if (!tmpl
9796 || (!CLASS_TYPE_P (tmpl)
9797 && TREE_CODE (tmpl) != FUNCTION_DECL))
9798 return;
9799
9800 saved_location = input_location;
9801 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9802 {
9803 tree type_decl = iter->typedef_decl;
9804 tree type_scope = iter->context;
9805
9806 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9807 continue;
9808
9809 if (uses_template_parms (type_decl))
9810 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9811 if (uses_template_parms (type_scope))
9812 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9813
9814 /* Make access check error messages point to the location
9815 of the use of the typedef. */
9816 input_location = iter->locus;
9817 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9818 type_decl, type_decl,
9819 tf_warning_or_error);
9820 }
9821 input_location = saved_location;
9822 }
9823
9824 static tree
9825 instantiate_class_template_1 (tree type)
9826 {
9827 tree templ, args, pattern, t, member;
9828 tree typedecl;
9829 tree pbinfo;
9830 tree base_list;
9831 unsigned int saved_maximum_field_alignment;
9832 tree fn_context;
9833
9834 if (type == error_mark_node)
9835 return error_mark_node;
9836
9837 if (COMPLETE_OR_OPEN_TYPE_P (type)
9838 || uses_template_parms (type))
9839 return type;
9840
9841 /* Figure out which template is being instantiated. */
9842 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9843 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9844
9845 /* Determine what specialization of the original template to
9846 instantiate. */
9847 t = most_specialized_partial_spec (type, tf_warning_or_error);
9848 if (t == error_mark_node)
9849 {
9850 TYPE_BEING_DEFINED (type) = 1;
9851 return error_mark_node;
9852 }
9853 else if (t)
9854 {
9855 /* This TYPE is actually an instantiation of a partial
9856 specialization. We replace the innermost set of ARGS with
9857 the arguments appropriate for substitution. For example,
9858 given:
9859
9860 template <class T> struct S {};
9861 template <class T> struct S<T*> {};
9862
9863 and supposing that we are instantiating S<int*>, ARGS will
9864 presently be {int*} -- but we need {int}. */
9865 pattern = TREE_TYPE (t);
9866 args = TREE_PURPOSE (t);
9867 }
9868 else
9869 {
9870 pattern = TREE_TYPE (templ);
9871 args = CLASSTYPE_TI_ARGS (type);
9872 }
9873
9874 /* If the template we're instantiating is incomplete, then clearly
9875 there's nothing we can do. */
9876 if (!COMPLETE_TYPE_P (pattern))
9877 return type;
9878
9879 /* If we've recursively instantiated too many templates, stop. */
9880 if (! push_tinst_level (type))
9881 return type;
9882
9883 /* Now we're really doing the instantiation. Mark the type as in
9884 the process of being defined. */
9885 TYPE_BEING_DEFINED (type) = 1;
9886
9887 /* We may be in the middle of deferred access check. Disable
9888 it now. */
9889 push_deferring_access_checks (dk_no_deferred);
9890
9891 int saved_unevaluated_operand = cp_unevaluated_operand;
9892 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9893
9894 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9895 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9896 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9897 fn_context = error_mark_node;
9898 if (!fn_context)
9899 push_to_top_level ();
9900 else
9901 {
9902 cp_unevaluated_operand = 0;
9903 c_inhibit_evaluation_warnings = 0;
9904 }
9905 /* Use #pragma pack from the template context. */
9906 saved_maximum_field_alignment = maximum_field_alignment;
9907 maximum_field_alignment = TYPE_PRECISION (pattern);
9908
9909 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9910
9911 /* Set the input location to the most specialized template definition.
9912 This is needed if tsubsting causes an error. */
9913 typedecl = TYPE_MAIN_DECL (pattern);
9914 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9915 DECL_SOURCE_LOCATION (typedecl);
9916
9917 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9918 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
9919 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9920 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9921 if (ANON_AGGR_TYPE_P (pattern))
9922 SET_ANON_AGGR_TYPE_P (type);
9923 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9924 {
9925 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9926 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9927 /* Adjust visibility for template arguments. */
9928 determine_visibility (TYPE_MAIN_DECL (type));
9929 }
9930 if (CLASS_TYPE_P (type))
9931 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9932
9933 pbinfo = TYPE_BINFO (pattern);
9934
9935 /* We should never instantiate a nested class before its enclosing
9936 class; we need to look up the nested class by name before we can
9937 instantiate it, and that lookup should instantiate the enclosing
9938 class. */
9939 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9940 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9941
9942 base_list = NULL_TREE;
9943 if (BINFO_N_BASE_BINFOS (pbinfo))
9944 {
9945 tree pbase_binfo;
9946 tree pushed_scope;
9947 int i;
9948
9949 /* We must enter the scope containing the type, as that is where
9950 the accessibility of types named in dependent bases are
9951 looked up from. */
9952 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9953
9954 /* Substitute into each of the bases to determine the actual
9955 basetypes. */
9956 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9957 {
9958 tree base;
9959 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9960 tree expanded_bases = NULL_TREE;
9961 int idx, len = 1;
9962
9963 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9964 {
9965 expanded_bases =
9966 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9967 args, tf_error, NULL_TREE);
9968 if (expanded_bases == error_mark_node)
9969 continue;
9970
9971 len = TREE_VEC_LENGTH (expanded_bases);
9972 }
9973
9974 for (idx = 0; idx < len; idx++)
9975 {
9976 if (expanded_bases)
9977 /* Extract the already-expanded base class. */
9978 base = TREE_VEC_ELT (expanded_bases, idx);
9979 else
9980 /* Substitute to figure out the base class. */
9981 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9982 NULL_TREE);
9983
9984 if (base == error_mark_node)
9985 continue;
9986
9987 base_list = tree_cons (access, base, base_list);
9988 if (BINFO_VIRTUAL_P (pbase_binfo))
9989 TREE_TYPE (base_list) = integer_type_node;
9990 }
9991 }
9992
9993 /* The list is now in reverse order; correct that. */
9994 base_list = nreverse (base_list);
9995
9996 if (pushed_scope)
9997 pop_scope (pushed_scope);
9998 }
9999 /* Now call xref_basetypes to set up all the base-class
10000 information. */
10001 xref_basetypes (type, base_list);
10002
10003 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10004 (int) ATTR_FLAG_TYPE_IN_PLACE,
10005 args, tf_error, NULL_TREE);
10006 fixup_attribute_variants (type);
10007
10008 /* Now that our base classes are set up, enter the scope of the
10009 class, so that name lookups into base classes, etc. will work
10010 correctly. This is precisely analogous to what we do in
10011 begin_class_definition when defining an ordinary non-template
10012 class, except we also need to push the enclosing classes. */
10013 push_nested_class (type);
10014
10015 /* Now members are processed in the order of declaration. */
10016 for (member = CLASSTYPE_DECL_LIST (pattern);
10017 member; member = TREE_CHAIN (member))
10018 {
10019 tree t = TREE_VALUE (member);
10020
10021 if (TREE_PURPOSE (member))
10022 {
10023 if (TYPE_P (t))
10024 {
10025 /* Build new CLASSTYPE_NESTED_UTDS. */
10026
10027 tree newtag;
10028 bool class_template_p;
10029
10030 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10031 && TYPE_LANG_SPECIFIC (t)
10032 && CLASSTYPE_IS_TEMPLATE (t));
10033 /* If the member is a class template, then -- even after
10034 substitution -- there may be dependent types in the
10035 template argument list for the class. We increment
10036 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10037 that function will assume that no types are dependent
10038 when outside of a template. */
10039 if (class_template_p)
10040 ++processing_template_decl;
10041 newtag = tsubst (t, args, tf_error, NULL_TREE);
10042 if (class_template_p)
10043 --processing_template_decl;
10044 if (newtag == error_mark_node)
10045 continue;
10046
10047 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10048 {
10049 tree name = TYPE_IDENTIFIER (t);
10050
10051 if (class_template_p)
10052 /* Unfortunately, lookup_template_class sets
10053 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10054 instantiation (i.e., for the type of a member
10055 template class nested within a template class.)
10056 This behavior is required for
10057 maybe_process_partial_specialization to work
10058 correctly, but is not accurate in this case;
10059 the TAG is not an instantiation of anything.
10060 (The corresponding TEMPLATE_DECL is an
10061 instantiation, but the TYPE is not.) */
10062 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10063
10064 /* Now, we call pushtag to put this NEWTAG into the scope of
10065 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10066 pushtag calling push_template_decl. We don't have to do
10067 this for enums because it will already have been done in
10068 tsubst_enum. */
10069 if (name)
10070 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10071 pushtag (name, newtag, /*tag_scope=*/ts_current);
10072 }
10073 }
10074 else if (DECL_DECLARES_FUNCTION_P (t))
10075 {
10076 /* Build new TYPE_METHODS. */
10077 tree r;
10078
10079 if (TREE_CODE (t) == TEMPLATE_DECL)
10080 ++processing_template_decl;
10081 r = tsubst (t, args, tf_error, NULL_TREE);
10082 if (TREE_CODE (t) == TEMPLATE_DECL)
10083 --processing_template_decl;
10084 set_current_access_from_decl (r);
10085 finish_member_declaration (r);
10086 /* Instantiate members marked with attribute used. */
10087 if (r != error_mark_node && DECL_PRESERVE_P (r))
10088 mark_used (r);
10089 if (TREE_CODE (r) == FUNCTION_DECL
10090 && DECL_OMP_DECLARE_REDUCTION_P (r))
10091 cp_check_omp_declare_reduction (r);
10092 }
10093 else if (DECL_CLASS_TEMPLATE_P (t)
10094 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10095 /* A closure type for a lambda in a default argument for a
10096 member template. Ignore it; it will be instantiated with
10097 the default argument. */;
10098 else
10099 {
10100 /* Build new TYPE_FIELDS. */
10101 if (TREE_CODE (t) == STATIC_ASSERT)
10102 {
10103 tree condition;
10104
10105 ++c_inhibit_evaluation_warnings;
10106 condition =
10107 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10108 tf_warning_or_error, NULL_TREE,
10109 /*integral_constant_expression_p=*/true);
10110 --c_inhibit_evaluation_warnings;
10111
10112 finish_static_assert (condition,
10113 STATIC_ASSERT_MESSAGE (t),
10114 STATIC_ASSERT_SOURCE_LOCATION (t),
10115 /*member_p=*/true);
10116 }
10117 else if (TREE_CODE (t) != CONST_DECL)
10118 {
10119 tree r;
10120 tree vec = NULL_TREE;
10121 int len = 1;
10122
10123 /* The file and line for this declaration, to
10124 assist in error message reporting. Since we
10125 called push_tinst_level above, we don't need to
10126 restore these. */
10127 input_location = DECL_SOURCE_LOCATION (t);
10128
10129 if (TREE_CODE (t) == TEMPLATE_DECL)
10130 ++processing_template_decl;
10131 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10132 if (TREE_CODE (t) == TEMPLATE_DECL)
10133 --processing_template_decl;
10134
10135 if (TREE_CODE (r) == TREE_VEC)
10136 {
10137 /* A capture pack became multiple fields. */
10138 vec = r;
10139 len = TREE_VEC_LENGTH (vec);
10140 }
10141
10142 for (int i = 0; i < len; ++i)
10143 {
10144 if (vec)
10145 r = TREE_VEC_ELT (vec, i);
10146 if (VAR_P (r))
10147 {
10148 /* In [temp.inst]:
10149
10150 [t]he initialization (and any associated
10151 side-effects) of a static data member does
10152 not occur unless the static data member is
10153 itself used in a way that requires the
10154 definition of the static data member to
10155 exist.
10156
10157 Therefore, we do not substitute into the
10158 initialized for the static data member here. */
10159 finish_static_data_member_decl
10160 (r,
10161 /*init=*/NULL_TREE,
10162 /*init_const_expr_p=*/false,
10163 /*asmspec_tree=*/NULL_TREE,
10164 /*flags=*/0);
10165 /* Instantiate members marked with attribute used. */
10166 if (r != error_mark_node && DECL_PRESERVE_P (r))
10167 mark_used (r);
10168 }
10169 else if (TREE_CODE (r) == FIELD_DECL)
10170 {
10171 /* Determine whether R has a valid type and can be
10172 completed later. If R is invalid, then its type
10173 is replaced by error_mark_node. */
10174 tree rtype = TREE_TYPE (r);
10175 if (can_complete_type_without_circularity (rtype))
10176 complete_type (rtype);
10177
10178 if (!complete_or_array_type_p (rtype))
10179 {
10180 /* If R's type couldn't be completed and
10181 it isn't a flexible array member (whose
10182 type is incomplete by definition) give
10183 an error. */
10184 cxx_incomplete_type_error (r, rtype);
10185 TREE_TYPE (r) = error_mark_node;
10186 }
10187 }
10188
10189 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10190 such a thing will already have been added to the field
10191 list by tsubst_enum in finish_member_declaration in the
10192 CLASSTYPE_NESTED_UTDS case above. */
10193 if (!(TREE_CODE (r) == TYPE_DECL
10194 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10195 && DECL_ARTIFICIAL (r)))
10196 {
10197 set_current_access_from_decl (r);
10198 finish_member_declaration (r);
10199 }
10200 }
10201 }
10202 }
10203 }
10204 else
10205 {
10206 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10207 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10208 {
10209 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10210
10211 tree friend_type = t;
10212 bool adjust_processing_template_decl = false;
10213
10214 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10215 {
10216 /* template <class T> friend class C; */
10217 friend_type = tsubst_friend_class (friend_type, args);
10218 adjust_processing_template_decl = true;
10219 }
10220 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10221 {
10222 /* template <class T> friend class C::D; */
10223 friend_type = tsubst (friend_type, args,
10224 tf_warning_or_error, NULL_TREE);
10225 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10226 friend_type = TREE_TYPE (friend_type);
10227 adjust_processing_template_decl = true;
10228 }
10229 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10230 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10231 {
10232 /* This could be either
10233
10234 friend class T::C;
10235
10236 when dependent_type_p is false or
10237
10238 template <class U> friend class T::C;
10239
10240 otherwise. */
10241 /* Bump processing_template_decl in case this is something like
10242 template <class T> friend struct A<T>::B. */
10243 ++processing_template_decl;
10244 friend_type = tsubst (friend_type, args,
10245 tf_warning_or_error, NULL_TREE);
10246 if (dependent_type_p (friend_type))
10247 adjust_processing_template_decl = true;
10248 --processing_template_decl;
10249 }
10250 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10251 && hidden_name_p (TYPE_NAME (friend_type)))
10252 {
10253 /* friend class C;
10254
10255 where C hasn't been declared yet. Let's lookup name
10256 from namespace scope directly, bypassing any name that
10257 come from dependent base class. */
10258 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10259
10260 /* The call to xref_tag_from_type does injection for friend
10261 classes. */
10262 push_nested_namespace (ns);
10263 friend_type =
10264 xref_tag_from_type (friend_type, NULL_TREE,
10265 /*tag_scope=*/ts_current);
10266 pop_nested_namespace (ns);
10267 }
10268 else if (uses_template_parms (friend_type))
10269 /* friend class C<T>; */
10270 friend_type = tsubst (friend_type, args,
10271 tf_warning_or_error, NULL_TREE);
10272 /* Otherwise it's
10273
10274 friend class C;
10275
10276 where C is already declared or
10277
10278 friend class C<int>;
10279
10280 We don't have to do anything in these cases. */
10281
10282 if (adjust_processing_template_decl)
10283 /* Trick make_friend_class into realizing that the friend
10284 we're adding is a template, not an ordinary class. It's
10285 important that we use make_friend_class since it will
10286 perform some error-checking and output cross-reference
10287 information. */
10288 ++processing_template_decl;
10289
10290 if (friend_type != error_mark_node)
10291 make_friend_class (type, friend_type, /*complain=*/false);
10292
10293 if (adjust_processing_template_decl)
10294 --processing_template_decl;
10295 }
10296 else
10297 {
10298 /* Build new DECL_FRIENDLIST. */
10299 tree r;
10300
10301 /* The file and line for this declaration, to
10302 assist in error message reporting. Since we
10303 called push_tinst_level above, we don't need to
10304 restore these. */
10305 input_location = DECL_SOURCE_LOCATION (t);
10306
10307 if (TREE_CODE (t) == TEMPLATE_DECL)
10308 {
10309 ++processing_template_decl;
10310 push_deferring_access_checks (dk_no_check);
10311 }
10312
10313 r = tsubst_friend_function (t, args);
10314 add_friend (type, r, /*complain=*/false);
10315 if (TREE_CODE (t) == TEMPLATE_DECL)
10316 {
10317 pop_deferring_access_checks ();
10318 --processing_template_decl;
10319 }
10320 }
10321 }
10322 }
10323
10324 if (fn_context)
10325 {
10326 /* Restore these before substituting into the lambda capture
10327 initializers. */
10328 cp_unevaluated_operand = saved_unevaluated_operand;
10329 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10330 }
10331
10332 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10333 {
10334 tree decl = lambda_function (type);
10335 if (decl)
10336 {
10337 if (!DECL_TEMPLATE_INFO (decl)
10338 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10339 {
10340 /* Set function_depth to avoid garbage collection. */
10341 ++function_depth;
10342 instantiate_decl (decl, false, false);
10343 --function_depth;
10344 }
10345
10346 /* We need to instantiate the capture list from the template
10347 after we've instantiated the closure members, but before we
10348 consider adding the conversion op. Also keep any captures
10349 that may have been added during instantiation of the op(). */
10350 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10351 tree tmpl_cap
10352 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10353 args, tf_warning_or_error, NULL_TREE,
10354 false, false);
10355
10356 LAMBDA_EXPR_CAPTURE_LIST (expr)
10357 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10358
10359 maybe_add_lambda_conv_op (type);
10360 }
10361 else
10362 gcc_assert (errorcount);
10363 }
10364
10365 /* Set the file and line number information to whatever is given for
10366 the class itself. This puts error messages involving generated
10367 implicit functions at a predictable point, and the same point
10368 that would be used for non-template classes. */
10369 input_location = DECL_SOURCE_LOCATION (typedecl);
10370
10371 unreverse_member_declarations (type);
10372 finish_struct_1 (type);
10373 TYPE_BEING_DEFINED (type) = 0;
10374
10375 /* We don't instantiate default arguments for member functions. 14.7.1:
10376
10377 The implicit instantiation of a class template specialization causes
10378 the implicit instantiation of the declarations, but not of the
10379 definitions or default arguments, of the class member functions,
10380 member classes, static data members and member templates.... */
10381
10382 /* Some typedefs referenced from within the template code need to be access
10383 checked at template instantiation time, i.e now. These types were
10384 added to the template at parsing time. Let's get those and perform
10385 the access checks then. */
10386 perform_typedefs_access_check (pattern, args);
10387 perform_deferred_access_checks (tf_warning_or_error);
10388 pop_nested_class ();
10389 maximum_field_alignment = saved_maximum_field_alignment;
10390 if (!fn_context)
10391 pop_from_top_level ();
10392 pop_deferring_access_checks ();
10393 pop_tinst_level ();
10394
10395 /* The vtable for a template class can be emitted in any translation
10396 unit in which the class is instantiated. When there is no key
10397 method, however, finish_struct_1 will already have added TYPE to
10398 the keyed_classes list. */
10399 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10400 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10401
10402 return type;
10403 }
10404
10405 /* Wrapper for instantiate_class_template_1. */
10406
10407 tree
10408 instantiate_class_template (tree type)
10409 {
10410 tree ret;
10411 timevar_push (TV_TEMPLATE_INST);
10412 ret = instantiate_class_template_1 (type);
10413 timevar_pop (TV_TEMPLATE_INST);
10414 return ret;
10415 }
10416
10417 static tree
10418 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10419 {
10420 tree r;
10421
10422 if (!t)
10423 r = t;
10424 else if (TYPE_P (t))
10425 r = tsubst (t, args, complain, in_decl);
10426 else
10427 {
10428 if (!(complain & tf_warning))
10429 ++c_inhibit_evaluation_warnings;
10430 r = tsubst_expr (t, args, complain, in_decl,
10431 /*integral_constant_expression_p=*/true);
10432 if (!(complain & tf_warning))
10433 --c_inhibit_evaluation_warnings;
10434 }
10435 return r;
10436 }
10437
10438 /* Given a function parameter pack TMPL_PARM and some function parameters
10439 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10440 and set *SPEC_P to point at the next point in the list. */
10441
10442 tree
10443 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10444 {
10445 /* Collect all of the extra "packed" parameters into an
10446 argument pack. */
10447 tree parmvec;
10448 tree parmtypevec;
10449 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10450 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10451 tree spec_parm = *spec_p;
10452 int i, len;
10453
10454 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10455 if (tmpl_parm
10456 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10457 break;
10458
10459 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10460 parmvec = make_tree_vec (len);
10461 parmtypevec = make_tree_vec (len);
10462 spec_parm = *spec_p;
10463 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10464 {
10465 TREE_VEC_ELT (parmvec, i) = spec_parm;
10466 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10467 }
10468
10469 /* Build the argument packs. */
10470 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10471 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10472 TREE_TYPE (argpack) = argtypepack;
10473 *spec_p = spec_parm;
10474
10475 return argpack;
10476 }
10477
10478 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10479 NONTYPE_ARGUMENT_PACK. */
10480
10481 static tree
10482 make_fnparm_pack (tree spec_parm)
10483 {
10484 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10485 }
10486
10487 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10488 pack expansion with no extra args, 2 if it has extra args, or 0
10489 if it is not a pack expansion. */
10490
10491 static int
10492 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10493 {
10494 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10495 if (i >= TREE_VEC_LENGTH (vec))
10496 return 0;
10497 tree elt = TREE_VEC_ELT (vec, i);
10498 if (DECL_P (elt))
10499 /* A decl pack is itself an expansion. */
10500 elt = TREE_TYPE (elt);
10501 if (!PACK_EXPANSION_P (elt))
10502 return 0;
10503 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10504 return 2;
10505 return 1;
10506 }
10507
10508
10509 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10510
10511 static tree
10512 make_argument_pack_select (tree arg_pack, unsigned index)
10513 {
10514 tree aps = make_node (ARGUMENT_PACK_SELECT);
10515
10516 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10517 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10518
10519 return aps;
10520 }
10521
10522 /* This is a subroutine of tsubst_pack_expansion.
10523
10524 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10525 mechanism to store the (non complete list of) arguments of the
10526 substitution and return a non substituted pack expansion, in order
10527 to wait for when we have enough arguments to really perform the
10528 substitution. */
10529
10530 static bool
10531 use_pack_expansion_extra_args_p (tree parm_packs,
10532 int arg_pack_len,
10533 bool has_empty_arg)
10534 {
10535 /* If one pack has an expansion and another pack has a normal
10536 argument or if one pack has an empty argument and an another
10537 one hasn't then tsubst_pack_expansion cannot perform the
10538 substitution and need to fall back on the
10539 PACK_EXPANSION_EXTRA mechanism. */
10540 if (parm_packs == NULL_TREE)
10541 return false;
10542 else if (has_empty_arg)
10543 return true;
10544
10545 bool has_expansion_arg = false;
10546 for (int i = 0 ; i < arg_pack_len; ++i)
10547 {
10548 bool has_non_expansion_arg = false;
10549 for (tree parm_pack = parm_packs;
10550 parm_pack;
10551 parm_pack = TREE_CHAIN (parm_pack))
10552 {
10553 tree arg = TREE_VALUE (parm_pack);
10554
10555 int exp = argument_pack_element_is_expansion_p (arg, i);
10556 if (exp == 2)
10557 /* We can't substitute a pack expansion with extra args into
10558 our pattern. */
10559 return true;
10560 else if (exp)
10561 has_expansion_arg = true;
10562 else
10563 has_non_expansion_arg = true;
10564 }
10565
10566 if (has_expansion_arg && has_non_expansion_arg)
10567 return true;
10568 }
10569 return false;
10570 }
10571
10572 /* [temp.variadic]/6 says that:
10573
10574 The instantiation of a pack expansion [...]
10575 produces a list E1,E2, ..., En, where N is the number of elements
10576 in the pack expansion parameters.
10577
10578 This subroutine of tsubst_pack_expansion produces one of these Ei.
10579
10580 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10581 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10582 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10583 INDEX is the index 'i' of the element Ei to produce. ARGS,
10584 COMPLAIN, and IN_DECL are the same parameters as for the
10585 tsubst_pack_expansion function.
10586
10587 The function returns the resulting Ei upon successful completion,
10588 or error_mark_node.
10589
10590 Note that this function possibly modifies the ARGS parameter, so
10591 it's the responsibility of the caller to restore it. */
10592
10593 static tree
10594 gen_elem_of_pack_expansion_instantiation (tree pattern,
10595 tree parm_packs,
10596 unsigned index,
10597 tree args /* This parm gets
10598 modified. */,
10599 tsubst_flags_t complain,
10600 tree in_decl)
10601 {
10602 tree t;
10603 bool ith_elem_is_expansion = false;
10604
10605 /* For each parameter pack, change the substitution of the parameter
10606 pack to the ith argument in its argument pack, then expand the
10607 pattern. */
10608 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10609 {
10610 tree parm = TREE_PURPOSE (pack);
10611 tree arg_pack = TREE_VALUE (pack);
10612 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10613
10614 ith_elem_is_expansion |=
10615 argument_pack_element_is_expansion_p (arg_pack, index);
10616
10617 /* Select the Ith argument from the pack. */
10618 if (TREE_CODE (parm) == PARM_DECL
10619 || TREE_CODE (parm) == FIELD_DECL)
10620 {
10621 if (index == 0)
10622 {
10623 aps = make_argument_pack_select (arg_pack, index);
10624 if (!mark_used (parm, complain) && !(complain & tf_error))
10625 return error_mark_node;
10626 register_local_specialization (aps, parm);
10627 }
10628 else
10629 aps = retrieve_local_specialization (parm);
10630 }
10631 else
10632 {
10633 int idx, level;
10634 template_parm_level_and_index (parm, &level, &idx);
10635
10636 if (index == 0)
10637 {
10638 aps = make_argument_pack_select (arg_pack, index);
10639 /* Update the corresponding argument. */
10640 TMPL_ARG (args, level, idx) = aps;
10641 }
10642 else
10643 /* Re-use the ARGUMENT_PACK_SELECT. */
10644 aps = TMPL_ARG (args, level, idx);
10645 }
10646 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10647 }
10648
10649 /* Substitute into the PATTERN with the (possibly altered)
10650 arguments. */
10651 if (pattern == in_decl)
10652 /* Expanding a fixed parameter pack from
10653 coerce_template_parameter_pack. */
10654 t = tsubst_decl (pattern, args, complain);
10655 else if (pattern == error_mark_node)
10656 t = error_mark_node;
10657 else if (constraint_p (pattern))
10658 {
10659 if (processing_template_decl)
10660 t = tsubst_constraint (pattern, args, complain, in_decl);
10661 else
10662 t = (constraints_satisfied_p (pattern, args)
10663 ? boolean_true_node : boolean_false_node);
10664 }
10665 else if (!TYPE_P (pattern))
10666 t = tsubst_expr (pattern, args, complain, in_decl,
10667 /*integral_constant_expression_p=*/false);
10668 else
10669 t = tsubst (pattern, args, complain, in_decl);
10670
10671 /* If the Ith argument pack element is a pack expansion, then
10672 the Ith element resulting from the substituting is going to
10673 be a pack expansion as well. */
10674 if (ith_elem_is_expansion)
10675 t = make_pack_expansion (t);
10676
10677 return t;
10678 }
10679
10680 /* When the unexpanded parameter pack in a fold expression expands to an empty
10681 sequence, the value of the expression is as follows; the program is
10682 ill-formed if the operator is not listed in this table.
10683
10684 && true
10685 || false
10686 , void() */
10687
10688 tree
10689 expand_empty_fold (tree t, tsubst_flags_t complain)
10690 {
10691 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
10692 if (!FOLD_EXPR_MODIFY_P (t))
10693 switch (code)
10694 {
10695 case TRUTH_ANDIF_EXPR:
10696 return boolean_true_node;
10697 case TRUTH_ORIF_EXPR:
10698 return boolean_false_node;
10699 case COMPOUND_EXPR:
10700 return void_node;
10701 default:
10702 break;
10703 }
10704
10705 if (complain & tf_error)
10706 error_at (location_of (t),
10707 "fold of empty expansion over %O", code);
10708 return error_mark_node;
10709 }
10710
10711 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10712 form an expression that combines the two terms using the
10713 operator of T. */
10714
10715 static tree
10716 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
10717 {
10718 tree op = FOLD_EXPR_OP (t);
10719 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
10720
10721 // Handle compound assignment operators.
10722 if (FOLD_EXPR_MODIFY_P (t))
10723 return build_x_modify_expr (input_location, left, code, right, complain);
10724
10725 switch (code)
10726 {
10727 case COMPOUND_EXPR:
10728 return build_x_compound_expr (input_location, left, right, complain);
10729 case DOTSTAR_EXPR:
10730 return build_m_component_ref (left, right, complain);
10731 default:
10732 return build_x_binary_op (input_location, code,
10733 left, TREE_CODE (left),
10734 right, TREE_CODE (right),
10735 /*overload=*/NULL,
10736 complain);
10737 }
10738 }
10739
10740 /* Substitute ARGS into the pack of a fold expression T. */
10741
10742 static inline tree
10743 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10744 {
10745 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
10746 }
10747
10748 /* Substitute ARGS into the pack of a fold expression T. */
10749
10750 static inline tree
10751 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10752 {
10753 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
10754 }
10755
10756 /* Expand a PACK of arguments into a grouped as left fold.
10757 Given a pack containing elements A0, A1, ..., An and an
10758 operator @, this builds the expression:
10759
10760 ((A0 @ A1) @ A2) ... @ An
10761
10762 Note that PACK must not be empty.
10763
10764 The operator is defined by the original fold expression T. */
10765
10766 static tree
10767 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
10768 {
10769 tree left = TREE_VEC_ELT (pack, 0);
10770 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
10771 {
10772 tree right = TREE_VEC_ELT (pack, i);
10773 left = fold_expression (t, left, right, complain);
10774 }
10775 return left;
10776 }
10777
10778 /* Substitute into a unary left fold expression. */
10779
10780 static tree
10781 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
10782 tree in_decl)
10783 {
10784 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10785 if (pack == error_mark_node)
10786 return error_mark_node;
10787 if (PACK_EXPANSION_P (pack))
10788 {
10789 tree r = copy_node (t);
10790 FOLD_EXPR_PACK (r) = pack;
10791 return r;
10792 }
10793 if (TREE_VEC_LENGTH (pack) == 0)
10794 return expand_empty_fold (t, complain);
10795 else
10796 return expand_left_fold (t, pack, complain);
10797 }
10798
10799 /* Substitute into a binary left fold expression.
10800
10801 Do ths by building a single (non-empty) vector of argumnts and
10802 building the expression from those elements. */
10803
10804 static tree
10805 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
10806 tree in_decl)
10807 {
10808 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10809 if (pack == error_mark_node)
10810 return error_mark_node;
10811 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10812 if (init == error_mark_node)
10813 return error_mark_node;
10814
10815 if (PACK_EXPANSION_P (pack))
10816 {
10817 tree r = copy_node (t);
10818 FOLD_EXPR_PACK (r) = pack;
10819 FOLD_EXPR_INIT (r) = init;
10820 return r;
10821 }
10822
10823 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
10824 TREE_VEC_ELT (vec, 0) = init;
10825 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
10826 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
10827
10828 return expand_left_fold (t, vec, complain);
10829 }
10830
10831 /* Expand a PACK of arguments into a grouped as right fold.
10832 Given a pack containing elementns A0, A1, ..., and an
10833 operator @, this builds the expression:
10834
10835 A0@ ... (An-2 @ (An-1 @ An))
10836
10837 Note that PACK must not be empty.
10838
10839 The operator is defined by the original fold expression T. */
10840
10841 tree
10842 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
10843 {
10844 // Build the expression.
10845 int n = TREE_VEC_LENGTH (pack);
10846 tree right = TREE_VEC_ELT (pack, n - 1);
10847 for (--n; n != 0; --n)
10848 {
10849 tree left = TREE_VEC_ELT (pack, n - 1);
10850 right = fold_expression (t, left, right, complain);
10851 }
10852 return right;
10853 }
10854
10855 /* Substitute into a unary right fold expression. */
10856
10857 static tree
10858 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
10859 tree in_decl)
10860 {
10861 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10862 if (pack == error_mark_node)
10863 return error_mark_node;
10864 if (PACK_EXPANSION_P (pack))
10865 {
10866 tree r = copy_node (t);
10867 FOLD_EXPR_PACK (r) = pack;
10868 return r;
10869 }
10870 if (TREE_VEC_LENGTH (pack) == 0)
10871 return expand_empty_fold (t, complain);
10872 else
10873 return expand_right_fold (t, pack, complain);
10874 }
10875
10876 /* Substitute into a binary right fold expression.
10877
10878 Do ths by building a single (non-empty) vector of arguments and
10879 building the expression from those elements. */
10880
10881 static tree
10882 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
10883 tree in_decl)
10884 {
10885 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10886 if (pack == error_mark_node)
10887 return error_mark_node;
10888 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10889 if (init == error_mark_node)
10890 return error_mark_node;
10891
10892 if (PACK_EXPANSION_P (pack))
10893 {
10894 tree r = copy_node (t);
10895 FOLD_EXPR_PACK (r) = pack;
10896 FOLD_EXPR_INIT (r) = init;
10897 return r;
10898 }
10899
10900 int n = TREE_VEC_LENGTH (pack);
10901 tree vec = make_tree_vec (n + 1);
10902 for (int i = 0; i < n; ++i)
10903 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
10904 TREE_VEC_ELT (vec, n) = init;
10905
10906 return expand_right_fold (t, vec, complain);
10907 }
10908
10909
10910 /* Substitute ARGS into T, which is an pack expansion
10911 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10912 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10913 (if only a partial substitution could be performed) or
10914 ERROR_MARK_NODE if there was an error. */
10915 tree
10916 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10917 tree in_decl)
10918 {
10919 tree pattern;
10920 tree pack, packs = NULL_TREE;
10921 bool unsubstituted_packs = false;
10922 int i, len = -1;
10923 tree result;
10924 hash_map<tree, tree> *saved_local_specializations = NULL;
10925 bool need_local_specializations = false;
10926 int levels;
10927
10928 gcc_assert (PACK_EXPANSION_P (t));
10929 pattern = PACK_EXPANSION_PATTERN (t);
10930
10931 /* Add in any args remembered from an earlier partial instantiation. */
10932 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10933
10934 levels = TMPL_ARGS_DEPTH (args);
10935
10936 /* Determine the argument packs that will instantiate the parameter
10937 packs used in the expansion expression. While we're at it,
10938 compute the number of arguments to be expanded and make sure it
10939 is consistent. */
10940 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10941 pack = TREE_CHAIN (pack))
10942 {
10943 tree parm_pack = TREE_VALUE (pack);
10944 tree arg_pack = NULL_TREE;
10945 tree orig_arg = NULL_TREE;
10946 int level = 0;
10947
10948 if (TREE_CODE (parm_pack) == BASES)
10949 {
10950 if (BASES_DIRECT (parm_pack))
10951 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10952 args, complain, in_decl, false));
10953 else
10954 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10955 args, complain, in_decl, false));
10956 }
10957 if (TREE_CODE (parm_pack) == PARM_DECL)
10958 {
10959 /* We know we have correct local_specializations if this
10960 expansion is at function scope, or if we're dealing with a
10961 local parameter in a requires expression; for the latter,
10962 tsubst_requires_expr set it up appropriately. */
10963 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
10964 arg_pack = retrieve_local_specialization (parm_pack);
10965 else
10966 /* We can't rely on local_specializations for a parameter
10967 name used later in a function declaration (such as in a
10968 late-specified return type). Even if it exists, it might
10969 have the wrong value for a recursive call. */
10970 need_local_specializations = true;
10971
10972 if (!arg_pack)
10973 {
10974 /* This parameter pack was used in an unevaluated context. Just
10975 make a dummy decl, since it's only used for its type. */
10976 arg_pack = tsubst_decl (parm_pack, args, complain);
10977 if (arg_pack && DECL_PACK_P (arg_pack))
10978 /* Partial instantiation of the parm_pack, we can't build
10979 up an argument pack yet. */
10980 arg_pack = NULL_TREE;
10981 else
10982 arg_pack = make_fnparm_pack (arg_pack);
10983 }
10984 }
10985 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10986 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10987 else
10988 {
10989 int idx;
10990 template_parm_level_and_index (parm_pack, &level, &idx);
10991
10992 if (level <= levels)
10993 arg_pack = TMPL_ARG (args, level, idx);
10994 }
10995
10996 orig_arg = arg_pack;
10997 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
10998 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
10999
11000 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11001 /* This can only happen if we forget to expand an argument
11002 pack somewhere else. Just return an error, silently. */
11003 {
11004 result = make_tree_vec (1);
11005 TREE_VEC_ELT (result, 0) = error_mark_node;
11006 return result;
11007 }
11008
11009 if (arg_pack)
11010 {
11011 int my_len =
11012 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11013
11014 /* Don't bother trying to do a partial substitution with
11015 incomplete packs; we'll try again after deduction. */
11016 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11017 return t;
11018
11019 if (len < 0)
11020 len = my_len;
11021 else if (len != my_len)
11022 {
11023 if (!(complain & tf_error))
11024 /* Fail quietly. */;
11025 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11026 error ("mismatched argument pack lengths while expanding "
11027 "%<%T%>",
11028 pattern);
11029 else
11030 error ("mismatched argument pack lengths while expanding "
11031 "%<%E%>",
11032 pattern);
11033 return error_mark_node;
11034 }
11035
11036 /* Keep track of the parameter packs and their corresponding
11037 argument packs. */
11038 packs = tree_cons (parm_pack, arg_pack, packs);
11039 TREE_TYPE (packs) = orig_arg;
11040 }
11041 else
11042 {
11043 /* We can't substitute for this parameter pack. We use a flag as
11044 well as the missing_level counter because function parameter
11045 packs don't have a level. */
11046 gcc_assert (processing_template_decl);
11047 unsubstituted_packs = true;
11048 }
11049 }
11050
11051 /* If the expansion is just T..., return the matching argument pack, unless
11052 we need to call convert_from_reference on all the elements. This is an
11053 important optimization; see c++/68422. */
11054 if (!unsubstituted_packs
11055 && TREE_PURPOSE (packs) == pattern)
11056 {
11057 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11058 /* Types need no adjustment, nor does sizeof..., and if we still have
11059 some pack expansion args we won't do anything yet. */
11060 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11061 || PACK_EXPANSION_SIZEOF_P (t)
11062 || pack_expansion_args_count (args))
11063 return args;
11064 /* Also optimize expression pack expansions if we can tell that the
11065 elements won't have reference type. */
11066 tree type = TREE_TYPE (pattern);
11067 if (type && TREE_CODE (type) != REFERENCE_TYPE
11068 && !PACK_EXPANSION_P (type)
11069 && !WILDCARD_TYPE_P (type))
11070 return args;
11071 /* Otherwise use the normal path so we get convert_from_reference. */
11072 }
11073
11074 /* We cannot expand this expansion expression, because we don't have
11075 all of the argument packs we need. */
11076 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11077 {
11078 /* We got some full packs, but we can't substitute them in until we
11079 have values for all the packs. So remember these until then. */
11080
11081 t = make_pack_expansion (pattern);
11082 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11083 return t;
11084 }
11085 else if (unsubstituted_packs)
11086 {
11087 /* There were no real arguments, we're just replacing a parameter
11088 pack with another version of itself. Substitute into the
11089 pattern and return a PACK_EXPANSION_*. The caller will need to
11090 deal with that. */
11091 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11092 t = tsubst_expr (pattern, args, complain, in_decl,
11093 /*integral_constant_expression_p=*/false);
11094 else
11095 t = tsubst (pattern, args, complain, in_decl);
11096 t = make_pack_expansion (t);
11097 return t;
11098 }
11099
11100 gcc_assert (len >= 0);
11101
11102 if (need_local_specializations)
11103 {
11104 /* We're in a late-specified return type, so create our own local
11105 specializations map; the current map is either NULL or (in the
11106 case of recursive unification) might have bindings that we don't
11107 want to use or alter. */
11108 saved_local_specializations = local_specializations;
11109 local_specializations = new hash_map<tree, tree>;
11110 }
11111
11112 /* For each argument in each argument pack, substitute into the
11113 pattern. */
11114 result = make_tree_vec (len);
11115 tree elem_args = copy_template_args (args);
11116 for (i = 0; i < len; ++i)
11117 {
11118 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11119 i,
11120 elem_args, complain,
11121 in_decl);
11122 TREE_VEC_ELT (result, i) = t;
11123 if (t == error_mark_node)
11124 {
11125 result = error_mark_node;
11126 break;
11127 }
11128 }
11129
11130 /* Update ARGS to restore the substitution from parameter packs to
11131 their argument packs. */
11132 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11133 {
11134 tree parm = TREE_PURPOSE (pack);
11135
11136 if (TREE_CODE (parm) == PARM_DECL
11137 || TREE_CODE (parm) == FIELD_DECL)
11138 register_local_specialization (TREE_TYPE (pack), parm);
11139 else
11140 {
11141 int idx, level;
11142
11143 if (TREE_VALUE (pack) == NULL_TREE)
11144 continue;
11145
11146 template_parm_level_and_index (parm, &level, &idx);
11147
11148 /* Update the corresponding argument. */
11149 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11150 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11151 TREE_TYPE (pack);
11152 else
11153 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11154 }
11155 }
11156
11157 if (need_local_specializations)
11158 {
11159 delete local_specializations;
11160 local_specializations = saved_local_specializations;
11161 }
11162
11163 return result;
11164 }
11165
11166 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11167 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11168 parameter packs; all parms generated from a function parameter pack will
11169 have the same DECL_PARM_INDEX. */
11170
11171 tree
11172 get_pattern_parm (tree parm, tree tmpl)
11173 {
11174 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11175 tree patparm;
11176
11177 if (DECL_ARTIFICIAL (parm))
11178 {
11179 for (patparm = DECL_ARGUMENTS (pattern);
11180 patparm; patparm = DECL_CHAIN (patparm))
11181 if (DECL_ARTIFICIAL (patparm)
11182 && DECL_NAME (parm) == DECL_NAME (patparm))
11183 break;
11184 }
11185 else
11186 {
11187 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11188 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11189 gcc_assert (DECL_PARM_INDEX (patparm)
11190 == DECL_PARM_INDEX (parm));
11191 }
11192
11193 return patparm;
11194 }
11195
11196 /* Make an argument pack out of the TREE_VEC VEC. */
11197
11198 static tree
11199 make_argument_pack (tree vec)
11200 {
11201 tree pack;
11202 tree elt = TREE_VEC_ELT (vec, 0);
11203 if (TYPE_P (elt))
11204 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11205 else
11206 {
11207 pack = make_node (NONTYPE_ARGUMENT_PACK);
11208 TREE_TYPE (pack) = TREE_TYPE (elt);
11209 TREE_CONSTANT (pack) = 1;
11210 }
11211 SET_ARGUMENT_PACK_ARGS (pack, vec);
11212 return pack;
11213 }
11214
11215 /* Return an exact copy of template args T that can be modified
11216 independently. */
11217
11218 static tree
11219 copy_template_args (tree t)
11220 {
11221 if (t == error_mark_node)
11222 return t;
11223
11224 int len = TREE_VEC_LENGTH (t);
11225 tree new_vec = make_tree_vec (len);
11226
11227 for (int i = 0; i < len; ++i)
11228 {
11229 tree elt = TREE_VEC_ELT (t, i);
11230 if (elt && TREE_CODE (elt) == TREE_VEC)
11231 elt = copy_template_args (elt);
11232 TREE_VEC_ELT (new_vec, i) = elt;
11233 }
11234
11235 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11236 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11237
11238 return new_vec;
11239 }
11240
11241 /* Substitute ARGS into the vector or list of template arguments T. */
11242
11243 static tree
11244 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11245 {
11246 tree orig_t = t;
11247 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11248 tree *elts;
11249
11250 if (t == error_mark_node)
11251 return error_mark_node;
11252
11253 len = TREE_VEC_LENGTH (t);
11254 elts = XALLOCAVEC (tree, len);
11255
11256 for (i = 0; i < len; i++)
11257 {
11258 tree orig_arg = TREE_VEC_ELT (t, i);
11259 tree new_arg;
11260
11261 if (TREE_CODE (orig_arg) == TREE_VEC)
11262 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11263 else if (PACK_EXPANSION_P (orig_arg))
11264 {
11265 /* Substitute into an expansion expression. */
11266 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11267
11268 if (TREE_CODE (new_arg) == TREE_VEC)
11269 /* Add to the expanded length adjustment the number of
11270 expanded arguments. We subtract one from this
11271 measurement, because the argument pack expression
11272 itself is already counted as 1 in
11273 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11274 the argument pack is empty. */
11275 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11276 }
11277 else if (ARGUMENT_PACK_P (orig_arg))
11278 {
11279 /* Substitute into each of the arguments. */
11280 new_arg = TYPE_P (orig_arg)
11281 ? cxx_make_type (TREE_CODE (orig_arg))
11282 : make_node (TREE_CODE (orig_arg));
11283
11284 SET_ARGUMENT_PACK_ARGS (
11285 new_arg,
11286 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11287 args, complain, in_decl));
11288
11289 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11290 new_arg = error_mark_node;
11291
11292 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11293 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11294 complain, in_decl);
11295 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11296
11297 if (TREE_TYPE (new_arg) == error_mark_node)
11298 new_arg = error_mark_node;
11299 }
11300 }
11301 else
11302 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11303
11304 if (new_arg == error_mark_node)
11305 return error_mark_node;
11306
11307 elts[i] = new_arg;
11308 if (new_arg != orig_arg)
11309 need_new = 1;
11310 }
11311
11312 if (!need_new)
11313 return t;
11314
11315 /* Make space for the expanded arguments coming from template
11316 argument packs. */
11317 t = make_tree_vec (len + expanded_len_adjust);
11318 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11319 arguments for a member template.
11320 In that case each TREE_VEC in ORIG_T represents a level of template
11321 arguments, and ORIG_T won't carry any non defaulted argument count.
11322 It will rather be the nested TREE_VECs that will carry one.
11323 In other words, ORIG_T carries a non defaulted argument count only
11324 if it doesn't contain any nested TREE_VEC. */
11325 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11326 {
11327 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11328 count += expanded_len_adjust;
11329 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11330 }
11331 for (i = 0, out = 0; i < len; i++)
11332 {
11333 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11334 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11335 && TREE_CODE (elts[i]) == TREE_VEC)
11336 {
11337 int idx;
11338
11339 /* Now expand the template argument pack "in place". */
11340 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11341 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11342 }
11343 else
11344 {
11345 TREE_VEC_ELT (t, out) = elts[i];
11346 out++;
11347 }
11348 }
11349
11350 return t;
11351 }
11352
11353 /* Return the result of substituting ARGS into the template parameters
11354 given by PARMS. If there are m levels of ARGS and m + n levels of
11355 PARMS, then the result will contain n levels of PARMS. For
11356 example, if PARMS is `template <class T> template <class U>
11357 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11358 result will be `template <int*, double, class V>'. */
11359
11360 static tree
11361 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11362 {
11363 tree r = NULL_TREE;
11364 tree* new_parms;
11365
11366 /* When substituting into a template, we must set
11367 PROCESSING_TEMPLATE_DECL as the template parameters may be
11368 dependent if they are based on one-another, and the dependency
11369 predicates are short-circuit outside of templates. */
11370 ++processing_template_decl;
11371
11372 for (new_parms = &r;
11373 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11374 new_parms = &(TREE_CHAIN (*new_parms)),
11375 parms = TREE_CHAIN (parms))
11376 {
11377 tree new_vec =
11378 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
11379 int i;
11380
11381 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11382 {
11383 tree tuple;
11384
11385 if (parms == error_mark_node)
11386 continue;
11387
11388 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
11389
11390 if (tuple == error_mark_node)
11391 continue;
11392
11393 TREE_VEC_ELT (new_vec, i) =
11394 tsubst_template_parm (tuple, args, complain);
11395 }
11396
11397 *new_parms =
11398 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11399 - TMPL_ARGS_DEPTH (args)),
11400 new_vec, NULL_TREE);
11401 }
11402
11403 --processing_template_decl;
11404
11405 return r;
11406 }
11407
11408 /* Return the result of substituting ARGS into one template parameter
11409 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11410 parameter and which TREE_PURPOSE is the default argument of the
11411 template parameter. */
11412
11413 static tree
11414 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11415 {
11416 tree default_value, parm_decl;
11417
11418 if (args == NULL_TREE
11419 || t == NULL_TREE
11420 || t == error_mark_node)
11421 return t;
11422
11423 gcc_assert (TREE_CODE (t) == TREE_LIST);
11424
11425 default_value = TREE_PURPOSE (t);
11426 parm_decl = TREE_VALUE (t);
11427
11428 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11429 if (TREE_CODE (parm_decl) == PARM_DECL
11430 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11431 parm_decl = error_mark_node;
11432 default_value = tsubst_template_arg (default_value, args,
11433 complain, NULL_TREE);
11434
11435 return build_tree_list (default_value, parm_decl);
11436 }
11437
11438 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11439 type T. If T is not an aggregate or enumeration type, it is
11440 handled as if by tsubst. IN_DECL is as for tsubst. If
11441 ENTERING_SCOPE is nonzero, T is the context for a template which
11442 we are presently tsubst'ing. Return the substituted value. */
11443
11444 static tree
11445 tsubst_aggr_type (tree t,
11446 tree args,
11447 tsubst_flags_t complain,
11448 tree in_decl,
11449 int entering_scope)
11450 {
11451 if (t == NULL_TREE)
11452 return NULL_TREE;
11453
11454 switch (TREE_CODE (t))
11455 {
11456 case RECORD_TYPE:
11457 if (TYPE_PTRMEMFUNC_P (t))
11458 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11459
11460 /* Else fall through. */
11461 case ENUMERAL_TYPE:
11462 case UNION_TYPE:
11463 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11464 {
11465 tree argvec;
11466 tree context;
11467 tree r;
11468 int saved_unevaluated_operand;
11469 int saved_inhibit_evaluation_warnings;
11470
11471 /* In "sizeof(X<I>)" we need to evaluate "I". */
11472 saved_unevaluated_operand = cp_unevaluated_operand;
11473 cp_unevaluated_operand = 0;
11474 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11475 c_inhibit_evaluation_warnings = 0;
11476
11477 /* First, determine the context for the type we are looking
11478 up. */
11479 context = TYPE_CONTEXT (t);
11480 if (context && TYPE_P (context))
11481 {
11482 context = tsubst_aggr_type (context, args, complain,
11483 in_decl, /*entering_scope=*/1);
11484 /* If context is a nested class inside a class template,
11485 it may still need to be instantiated (c++/33959). */
11486 context = complete_type (context);
11487 }
11488
11489 /* Then, figure out what arguments are appropriate for the
11490 type we are trying to find. For example, given:
11491
11492 template <class T> struct S;
11493 template <class T, class U> void f(T, U) { S<U> su; }
11494
11495 and supposing that we are instantiating f<int, double>,
11496 then our ARGS will be {int, double}, but, when looking up
11497 S we only want {double}. */
11498 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11499 complain, in_decl);
11500 if (argvec == error_mark_node)
11501 r = error_mark_node;
11502 else
11503 {
11504 r = lookup_template_class (t, argvec, in_decl, context,
11505 entering_scope, complain);
11506 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11507 }
11508
11509 cp_unevaluated_operand = saved_unevaluated_operand;
11510 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11511
11512 return r;
11513 }
11514 else
11515 /* This is not a template type, so there's nothing to do. */
11516 return t;
11517
11518 default:
11519 return tsubst (t, args, complain, in_decl);
11520 }
11521 }
11522
11523 /* Substitute into the default argument ARG (a default argument for
11524 FN), which has the indicated TYPE. */
11525
11526 tree
11527 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11528 {
11529 tree saved_class_ptr = NULL_TREE;
11530 tree saved_class_ref = NULL_TREE;
11531 int errs = errorcount + sorrycount;
11532
11533 /* This can happen in invalid code. */
11534 if (TREE_CODE (arg) == DEFAULT_ARG)
11535 return arg;
11536
11537 /* This default argument came from a template. Instantiate the
11538 default argument here, not in tsubst. In the case of
11539 something like:
11540
11541 template <class T>
11542 struct S {
11543 static T t();
11544 void f(T = t());
11545 };
11546
11547 we must be careful to do name lookup in the scope of S<T>,
11548 rather than in the current class. */
11549 push_access_scope (fn);
11550 /* The "this" pointer is not valid in a default argument. */
11551 if (cfun)
11552 {
11553 saved_class_ptr = current_class_ptr;
11554 cp_function_chain->x_current_class_ptr = NULL_TREE;
11555 saved_class_ref = current_class_ref;
11556 cp_function_chain->x_current_class_ref = NULL_TREE;
11557 }
11558
11559 push_deferring_access_checks(dk_no_deferred);
11560 /* The default argument expression may cause implicitly defined
11561 member functions to be synthesized, which will result in garbage
11562 collection. We must treat this situation as if we were within
11563 the body of function so as to avoid collecting live data on the
11564 stack. */
11565 ++function_depth;
11566 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11567 complain, NULL_TREE,
11568 /*integral_constant_expression_p=*/false);
11569 --function_depth;
11570 pop_deferring_access_checks();
11571
11572 /* Restore the "this" pointer. */
11573 if (cfun)
11574 {
11575 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11576 cp_function_chain->x_current_class_ref = saved_class_ref;
11577 }
11578
11579 if (errorcount+sorrycount > errs
11580 && (complain & tf_warning_or_error))
11581 inform (input_location,
11582 " when instantiating default argument for call to %D", fn);
11583
11584 /* Make sure the default argument is reasonable. */
11585 arg = check_default_argument (type, arg, complain);
11586
11587 pop_access_scope (fn);
11588
11589 return arg;
11590 }
11591
11592 /* Substitute into all the default arguments for FN. */
11593
11594 static void
11595 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11596 {
11597 tree arg;
11598 tree tmpl_args;
11599
11600 tmpl_args = DECL_TI_ARGS (fn);
11601
11602 /* If this function is not yet instantiated, we certainly don't need
11603 its default arguments. */
11604 if (uses_template_parms (tmpl_args))
11605 return;
11606 /* Don't do this again for clones. */
11607 if (DECL_CLONED_FUNCTION_P (fn))
11608 return;
11609
11610 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11611 arg;
11612 arg = TREE_CHAIN (arg))
11613 if (TREE_PURPOSE (arg))
11614 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11615 TREE_VALUE (arg),
11616 TREE_PURPOSE (arg),
11617 complain);
11618 }
11619
11620 /* Substitute the ARGS into the T, which is a _DECL. Return the
11621 result of the substitution. Issue error and warning messages under
11622 control of COMPLAIN. */
11623
11624 static tree
11625 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11626 {
11627 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11628 location_t saved_loc;
11629 tree r = NULL_TREE;
11630 tree in_decl = t;
11631 hashval_t hash = 0;
11632
11633 /* Set the filename and linenumber to improve error-reporting. */
11634 saved_loc = input_location;
11635 input_location = DECL_SOURCE_LOCATION (t);
11636
11637 switch (TREE_CODE (t))
11638 {
11639 case TEMPLATE_DECL:
11640 {
11641 /* We can get here when processing a member function template,
11642 member class template, or template template parameter. */
11643 tree decl = DECL_TEMPLATE_RESULT (t);
11644 tree spec;
11645 tree tmpl_args;
11646 tree full_args;
11647
11648 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11649 {
11650 /* Template template parameter is treated here. */
11651 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11652 if (new_type == error_mark_node)
11653 r = error_mark_node;
11654 /* If we get a real template back, return it. This can happen in
11655 the context of most_specialized_partial_spec. */
11656 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11657 r = new_type;
11658 else
11659 /* The new TEMPLATE_DECL was built in
11660 reduce_template_parm_level. */
11661 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
11662 break;
11663 }
11664
11665 /* We might already have an instance of this template.
11666 The ARGS are for the surrounding class type, so the
11667 full args contain the tsubst'd args for the context,
11668 plus the innermost args from the template decl. */
11669 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11670 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11671 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11672 /* Because this is a template, the arguments will still be
11673 dependent, even after substitution. If
11674 PROCESSING_TEMPLATE_DECL is not set, the dependency
11675 predicates will short-circuit. */
11676 ++processing_template_decl;
11677 full_args = tsubst_template_args (tmpl_args, args,
11678 complain, in_decl);
11679 --processing_template_decl;
11680 if (full_args == error_mark_node)
11681 RETURN (error_mark_node);
11682
11683 /* If this is a default template template argument,
11684 tsubst might not have changed anything. */
11685 if (full_args == tmpl_args)
11686 RETURN (t);
11687
11688 hash = hash_tmpl_and_args (t, full_args);
11689 spec = retrieve_specialization (t, full_args, hash);
11690 if (spec != NULL_TREE)
11691 {
11692 r = spec;
11693 break;
11694 }
11695
11696 /* Make a new template decl. It will be similar to the
11697 original, but will record the current template arguments.
11698 We also create a new function declaration, which is just
11699 like the old one, but points to this new template, rather
11700 than the old one. */
11701 r = copy_decl (t);
11702 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11703 DECL_CHAIN (r) = NULL_TREE;
11704
11705 // Build new template info linking to the original template decl.
11706 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11707
11708 if (TREE_CODE (decl) == TYPE_DECL
11709 && !TYPE_DECL_ALIAS_P (decl))
11710 {
11711 tree new_type;
11712 ++processing_template_decl;
11713 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11714 --processing_template_decl;
11715 if (new_type == error_mark_node)
11716 RETURN (error_mark_node);
11717
11718 TREE_TYPE (r) = new_type;
11719 /* For a partial specialization, we need to keep pointing to
11720 the primary template. */
11721 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11722 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11723 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11724 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11725 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11726 }
11727 else
11728 {
11729 tree new_decl;
11730 ++processing_template_decl;
11731 new_decl = tsubst (decl, args, complain, in_decl);
11732 --processing_template_decl;
11733 if (new_decl == error_mark_node)
11734 RETURN (error_mark_node);
11735
11736 DECL_TEMPLATE_RESULT (r) = new_decl;
11737 DECL_TI_TEMPLATE (new_decl) = r;
11738 TREE_TYPE (r) = TREE_TYPE (new_decl);
11739 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11740 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11741 }
11742
11743 SET_DECL_IMPLICIT_INSTANTIATION (r);
11744 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11745 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11746
11747 /* The template parameters for this new template are all the
11748 template parameters for the old template, except the
11749 outermost level of parameters. */
11750 DECL_TEMPLATE_PARMS (r)
11751 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11752 complain);
11753
11754 if (PRIMARY_TEMPLATE_P (t))
11755 DECL_PRIMARY_TEMPLATE (r) = r;
11756
11757 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
11758 /* Record this non-type partial instantiation. */
11759 register_specialization (r, t,
11760 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11761 false, hash);
11762 }
11763 break;
11764
11765 case FUNCTION_DECL:
11766 {
11767 tree ctx;
11768 tree argvec = NULL_TREE;
11769 tree *friends;
11770 tree gen_tmpl;
11771 tree type;
11772 int member;
11773 int args_depth;
11774 int parms_depth;
11775
11776 /* Nobody should be tsubst'ing into non-template functions. */
11777 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11778
11779 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11780 {
11781 tree spec;
11782
11783 /* If T is not dependent, just return it. */
11784 if (!uses_template_parms (DECL_TI_ARGS (t)))
11785 RETURN (t);
11786
11787 /* Calculate the most general template of which R is a
11788 specialization, and the complete set of arguments used to
11789 specialize R. */
11790 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11791 argvec = tsubst_template_args (DECL_TI_ARGS
11792 (DECL_TEMPLATE_RESULT
11793 (DECL_TI_TEMPLATE (t))),
11794 args, complain, in_decl);
11795 if (argvec == error_mark_node)
11796 RETURN (error_mark_node);
11797
11798 /* Check to see if we already have this specialization. */
11799 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11800 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11801
11802 if (spec)
11803 {
11804 r = spec;
11805 break;
11806 }
11807
11808 /* We can see more levels of arguments than parameters if
11809 there was a specialization of a member template, like
11810 this:
11811
11812 template <class T> struct S { template <class U> void f(); }
11813 template <> template <class U> void S<int>::f(U);
11814
11815 Here, we'll be substituting into the specialization,
11816 because that's where we can find the code we actually
11817 want to generate, but we'll have enough arguments for
11818 the most general template.
11819
11820 We also deal with the peculiar case:
11821
11822 template <class T> struct S {
11823 template <class U> friend void f();
11824 };
11825 template <class U> void f() {}
11826 template S<int>;
11827 template void f<double>();
11828
11829 Here, the ARGS for the instantiation of will be {int,
11830 double}. But, we only need as many ARGS as there are
11831 levels of template parameters in CODE_PATTERN. We are
11832 careful not to get fooled into reducing the ARGS in
11833 situations like:
11834
11835 template <class T> struct S { template <class U> void f(U); }
11836 template <class T> template <> void S<T>::f(int) {}
11837
11838 which we can spot because the pattern will be a
11839 specialization in this case. */
11840 args_depth = TMPL_ARGS_DEPTH (args);
11841 parms_depth =
11842 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11843 if (args_depth > parms_depth
11844 && !DECL_TEMPLATE_SPECIALIZATION (t))
11845 args = get_innermost_template_args (args, parms_depth);
11846 }
11847 else
11848 {
11849 /* This special case arises when we have something like this:
11850
11851 template <class T> struct S {
11852 friend void f<int>(int, double);
11853 };
11854
11855 Here, the DECL_TI_TEMPLATE for the friend declaration
11856 will be an IDENTIFIER_NODE. We are being called from
11857 tsubst_friend_function, and we want only to create a
11858 new decl (R) with appropriate types so that we can call
11859 determine_specialization. */
11860 gen_tmpl = NULL_TREE;
11861 }
11862
11863 if (DECL_CLASS_SCOPE_P (t))
11864 {
11865 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11866 member = 2;
11867 else
11868 member = 1;
11869 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11870 complain, t, /*entering_scope=*/1);
11871 }
11872 else
11873 {
11874 member = 0;
11875 ctx = DECL_CONTEXT (t);
11876 }
11877 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11878 if (type == error_mark_node)
11879 RETURN (error_mark_node);
11880
11881 /* If we hit excessive deduction depth, the type is bogus even if
11882 it isn't error_mark_node, so don't build a decl. */
11883 if (excessive_deduction_depth)
11884 RETURN (error_mark_node);
11885
11886 /* We do NOT check for matching decls pushed separately at this
11887 point, as they may not represent instantiations of this
11888 template, and in any case are considered separate under the
11889 discrete model. */
11890 r = copy_decl (t);
11891 DECL_USE_TEMPLATE (r) = 0;
11892 TREE_TYPE (r) = type;
11893 /* Clear out the mangled name and RTL for the instantiation. */
11894 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11895 SET_DECL_RTL (r, NULL);
11896 /* Leave DECL_INITIAL set on deleted instantiations. */
11897 if (!DECL_DELETED_FN (r))
11898 DECL_INITIAL (r) = NULL_TREE;
11899 DECL_CONTEXT (r) = ctx;
11900
11901 /* OpenMP UDRs have the only argument a reference to the declared
11902 type. We want to diagnose if the declared type is a reference,
11903 which is invalid, but as references to references are usually
11904 quietly merged, diagnose it here. */
11905 if (DECL_OMP_DECLARE_REDUCTION_P (t))
11906 {
11907 tree argtype
11908 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11909 argtype = tsubst (argtype, args, complain, in_decl);
11910 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11911 error_at (DECL_SOURCE_LOCATION (t),
11912 "reference type %qT in "
11913 "%<#pragma omp declare reduction%>", argtype);
11914 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11915 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11916 argtype);
11917 }
11918
11919 if (member && DECL_CONV_FN_P (r))
11920 /* Type-conversion operator. Reconstruct the name, in
11921 case it's the name of one of the template's parameters. */
11922 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11923
11924 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11925 complain, t);
11926 DECL_RESULT (r) = NULL_TREE;
11927
11928 TREE_STATIC (r) = 0;
11929 TREE_PUBLIC (r) = TREE_PUBLIC (t);
11930 DECL_EXTERNAL (r) = 1;
11931 /* If this is an instantiation of a function with internal
11932 linkage, we already know what object file linkage will be
11933 assigned to the instantiation. */
11934 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
11935 DECL_DEFER_OUTPUT (r) = 0;
11936 DECL_CHAIN (r) = NULL_TREE;
11937 DECL_PENDING_INLINE_INFO (r) = 0;
11938 DECL_PENDING_INLINE_P (r) = 0;
11939 DECL_SAVED_TREE (r) = NULL_TREE;
11940 DECL_STRUCT_FUNCTION (r) = NULL;
11941 TREE_USED (r) = 0;
11942 /* We'll re-clone as appropriate in instantiate_template. */
11943 DECL_CLONED_FUNCTION (r) = NULL_TREE;
11944
11945 /* If we aren't complaining now, return on error before we register
11946 the specialization so that we'll complain eventually. */
11947 if ((complain & tf_error) == 0
11948 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11949 && !grok_op_properties (r, /*complain=*/false))
11950 RETURN (error_mark_node);
11951
11952 /* When instantiating a constrained member, substitute
11953 into the constraints to create a new constraint. */
11954 if (tree ci = get_constraints (t))
11955 if (member)
11956 {
11957 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
11958 set_constraints (r, ci);
11959 }
11960
11961 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11962 this in the special friend case mentioned above where
11963 GEN_TMPL is NULL. */
11964 if (gen_tmpl)
11965 {
11966 DECL_TEMPLATE_INFO (r)
11967 = build_template_info (gen_tmpl, argvec);
11968 SET_DECL_IMPLICIT_INSTANTIATION (r);
11969
11970 tree new_r
11971 = register_specialization (r, gen_tmpl, argvec, false, hash);
11972 if (new_r != r)
11973 /* We instantiated this while substituting into
11974 the type earlier (template/friend54.C). */
11975 RETURN (new_r);
11976
11977 /* We're not supposed to instantiate default arguments
11978 until they are called, for a template. But, for a
11979 declaration like:
11980
11981 template <class T> void f ()
11982 { extern void g(int i = T()); }
11983
11984 we should do the substitution when the template is
11985 instantiated. We handle the member function case in
11986 instantiate_class_template since the default arguments
11987 might refer to other members of the class. */
11988 if (!member
11989 && !PRIMARY_TEMPLATE_P (gen_tmpl)
11990 && !uses_template_parms (argvec))
11991 tsubst_default_arguments (r, complain);
11992 }
11993 else
11994 DECL_TEMPLATE_INFO (r) = NULL_TREE;
11995
11996 /* Copy the list of befriending classes. */
11997 for (friends = &DECL_BEFRIENDING_CLASSES (r);
11998 *friends;
11999 friends = &TREE_CHAIN (*friends))
12000 {
12001 *friends = copy_node (*friends);
12002 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12003 args, complain,
12004 in_decl);
12005 }
12006
12007 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12008 {
12009 maybe_retrofit_in_chrg (r);
12010 if (DECL_CONSTRUCTOR_P (r))
12011 grok_ctor_properties (ctx, r);
12012 if (DECL_INHERITED_CTOR_BASE (r))
12013 deduce_inheriting_ctor (r);
12014 /* If this is an instantiation of a member template, clone it.
12015 If it isn't, that'll be handled by
12016 clone_constructors_and_destructors. */
12017 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12018 clone_function_decl (r, /*update_method_vec_p=*/0);
12019 }
12020 else if ((complain & tf_error) != 0
12021 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12022 && !grok_op_properties (r, /*complain=*/true))
12023 RETURN (error_mark_node);
12024
12025 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12026 SET_DECL_FRIEND_CONTEXT (r,
12027 tsubst (DECL_FRIEND_CONTEXT (t),
12028 args, complain, in_decl));
12029
12030 /* Possibly limit visibility based on template args. */
12031 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12032 if (DECL_VISIBILITY_SPECIFIED (t))
12033 {
12034 DECL_VISIBILITY_SPECIFIED (r) = 0;
12035 DECL_ATTRIBUTES (r)
12036 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12037 }
12038 determine_visibility (r);
12039 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12040 && !processing_template_decl)
12041 defaulted_late_check (r);
12042
12043 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12044 args, complain, in_decl);
12045 }
12046 break;
12047
12048 case PARM_DECL:
12049 {
12050 tree type = NULL_TREE;
12051 int i, len = 1;
12052 tree expanded_types = NULL_TREE;
12053 tree prev_r = NULL_TREE;
12054 tree first_r = NULL_TREE;
12055
12056 if (DECL_PACK_P (t))
12057 {
12058 /* If there is a local specialization that isn't a
12059 parameter pack, it means that we're doing a "simple"
12060 substitution from inside tsubst_pack_expansion. Just
12061 return the local specialization (which will be a single
12062 parm). */
12063 tree spec = retrieve_local_specialization (t);
12064 if (spec
12065 && TREE_CODE (spec) == PARM_DECL
12066 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12067 RETURN (spec);
12068
12069 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12070 the parameters in this function parameter pack. */
12071 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12072 complain, in_decl);
12073 if (TREE_CODE (expanded_types) == TREE_VEC)
12074 {
12075 len = TREE_VEC_LENGTH (expanded_types);
12076
12077 /* Zero-length parameter packs are boring. Just substitute
12078 into the chain. */
12079 if (len == 0)
12080 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12081 TREE_CHAIN (t)));
12082 }
12083 else
12084 {
12085 /* All we did was update the type. Make a note of that. */
12086 type = expanded_types;
12087 expanded_types = NULL_TREE;
12088 }
12089 }
12090
12091 /* Loop through all of the parameters we'll build. When T is
12092 a function parameter pack, LEN is the number of expanded
12093 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12094 r = NULL_TREE;
12095 for (i = 0; i < len; ++i)
12096 {
12097 prev_r = r;
12098 r = copy_node (t);
12099 if (DECL_TEMPLATE_PARM_P (t))
12100 SET_DECL_TEMPLATE_PARM_P (r);
12101
12102 if (expanded_types)
12103 /* We're on the Ith parameter of the function parameter
12104 pack. */
12105 {
12106 /* Get the Ith type. */
12107 type = TREE_VEC_ELT (expanded_types, i);
12108
12109 /* Rename the parameter to include the index. */
12110 DECL_NAME (r)
12111 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12112 }
12113 else if (!type)
12114 /* We're dealing with a normal parameter. */
12115 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12116
12117 type = type_decays_to (type);
12118 TREE_TYPE (r) = type;
12119 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12120
12121 if (DECL_INITIAL (r))
12122 {
12123 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12124 DECL_INITIAL (r) = TREE_TYPE (r);
12125 else
12126 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12127 complain, in_decl);
12128 }
12129
12130 DECL_CONTEXT (r) = NULL_TREE;
12131
12132 if (!DECL_TEMPLATE_PARM_P (r))
12133 DECL_ARG_TYPE (r) = type_passed_as (type);
12134
12135 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12136 args, complain, in_decl);
12137
12138 /* Keep track of the first new parameter we
12139 generate. That's what will be returned to the
12140 caller. */
12141 if (!first_r)
12142 first_r = r;
12143
12144 /* Build a proper chain of parameters when substituting
12145 into a function parameter pack. */
12146 if (prev_r)
12147 DECL_CHAIN (prev_r) = r;
12148 }
12149
12150 /* If cp_unevaluated_operand is set, we're just looking for a
12151 single dummy parameter, so don't keep going. */
12152 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12153 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12154 complain, DECL_CHAIN (t));
12155
12156 /* FIRST_R contains the start of the chain we've built. */
12157 r = first_r;
12158 }
12159 break;
12160
12161 case FIELD_DECL:
12162 {
12163 tree type = NULL_TREE;
12164 tree vec = NULL_TREE;
12165 tree expanded_types = NULL_TREE;
12166 int len = 1;
12167
12168 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12169 {
12170 /* This field is a lambda capture pack. Return a TREE_VEC of
12171 the expanded fields to instantiate_class_template_1 and
12172 store them in the specializations hash table as a
12173 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12174 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12175 complain, in_decl);
12176 if (TREE_CODE (expanded_types) == TREE_VEC)
12177 {
12178 len = TREE_VEC_LENGTH (expanded_types);
12179 vec = make_tree_vec (len);
12180 }
12181 else
12182 {
12183 /* All we did was update the type. Make a note of that. */
12184 type = expanded_types;
12185 expanded_types = NULL_TREE;
12186 }
12187 }
12188
12189 for (int i = 0; i < len; ++i)
12190 {
12191 r = copy_decl (t);
12192 if (expanded_types)
12193 {
12194 type = TREE_VEC_ELT (expanded_types, i);
12195 DECL_NAME (r)
12196 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12197 }
12198 else if (!type)
12199 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12200
12201 if (type == error_mark_node)
12202 RETURN (error_mark_node);
12203 TREE_TYPE (r) = type;
12204 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12205
12206 if (DECL_C_BIT_FIELD (r))
12207 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12208 non-bit-fields DECL_INITIAL is a non-static data member
12209 initializer, which gets deferred instantiation. */
12210 DECL_INITIAL (r)
12211 = tsubst_expr (DECL_INITIAL (t), args,
12212 complain, in_decl,
12213 /*integral_constant_expression_p=*/true);
12214 else if (DECL_INITIAL (t))
12215 {
12216 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12217 NSDMI in perform_member_init. Still set DECL_INITIAL
12218 so that we know there is one. */
12219 DECL_INITIAL (r) = void_node;
12220 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12221 retrofit_lang_decl (r);
12222 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12223 }
12224 /* We don't have to set DECL_CONTEXT here; it is set by
12225 finish_member_declaration. */
12226 DECL_CHAIN (r) = NULL_TREE;
12227
12228 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12229 args, complain, in_decl);
12230
12231 if (vec)
12232 TREE_VEC_ELT (vec, i) = r;
12233 }
12234
12235 if (vec)
12236 {
12237 r = vec;
12238 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12239 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12240 SET_ARGUMENT_PACK_ARGS (pack, vec);
12241 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12242 TREE_TYPE (pack) = tpack;
12243 register_specialization (pack, t, args, false, 0);
12244 }
12245 }
12246 break;
12247
12248 case USING_DECL:
12249 /* We reach here only for member using decls. We also need to check
12250 uses_template_parms because DECL_DEPENDENT_P is not set for a
12251 using-declaration that designates a member of the current
12252 instantiation (c++/53549). */
12253 if (DECL_DEPENDENT_P (t)
12254 || uses_template_parms (USING_DECL_SCOPE (t)))
12255 {
12256 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12257 complain, in_decl);
12258 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12259 r = do_class_using_decl (inst_scope, name);
12260 if (!r)
12261 r = error_mark_node;
12262 else
12263 {
12264 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12265 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12266 }
12267 }
12268 else
12269 {
12270 r = copy_node (t);
12271 DECL_CHAIN (r) = NULL_TREE;
12272 }
12273 break;
12274
12275 case TYPE_DECL:
12276 case VAR_DECL:
12277 {
12278 tree argvec = NULL_TREE;
12279 tree gen_tmpl = NULL_TREE;
12280 tree spec;
12281 tree tmpl = NULL_TREE;
12282 tree ctx;
12283 tree type = NULL_TREE;
12284 bool local_p;
12285
12286 if (TREE_TYPE (t) == error_mark_node)
12287 RETURN (error_mark_node);
12288
12289 if (TREE_CODE (t) == TYPE_DECL
12290 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12291 {
12292 /* If this is the canonical decl, we don't have to
12293 mess with instantiations, and often we can't (for
12294 typename, template type parms and such). Note that
12295 TYPE_NAME is not correct for the above test if
12296 we've copied the type for a typedef. */
12297 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12298 if (type == error_mark_node)
12299 RETURN (error_mark_node);
12300 r = TYPE_NAME (type);
12301 break;
12302 }
12303
12304 /* Check to see if we already have the specialization we
12305 need. */
12306 spec = NULL_TREE;
12307 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12308 {
12309 /* T is a static data member or namespace-scope entity.
12310 We have to substitute into namespace-scope variables
12311 (not just variable templates) because of cases like:
12312
12313 template <class T> void f() { extern T t; }
12314
12315 where the entity referenced is not known until
12316 instantiation time. */
12317 local_p = false;
12318 ctx = DECL_CONTEXT (t);
12319 if (DECL_CLASS_SCOPE_P (t))
12320 {
12321 ctx = tsubst_aggr_type (ctx, args,
12322 complain,
12323 in_decl, /*entering_scope=*/1);
12324 /* If CTX is unchanged, then T is in fact the
12325 specialization we want. That situation occurs when
12326 referencing a static data member within in its own
12327 class. We can use pointer equality, rather than
12328 same_type_p, because DECL_CONTEXT is always
12329 canonical... */
12330 if (ctx == DECL_CONTEXT (t)
12331 /* ... unless T is a member template; in which
12332 case our caller can be willing to create a
12333 specialization of that template represented
12334 by T. */
12335 && !(DECL_TI_TEMPLATE (t)
12336 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12337 spec = t;
12338 }
12339
12340 if (!spec)
12341 {
12342 tmpl = DECL_TI_TEMPLATE (t);
12343 gen_tmpl = most_general_template (tmpl);
12344 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12345 if (argvec != error_mark_node)
12346 argvec = (coerce_innermost_template_parms
12347 (DECL_TEMPLATE_PARMS (gen_tmpl),
12348 argvec, t, complain,
12349 /*all*/true, /*defarg*/true));
12350 if (argvec == error_mark_node)
12351 RETURN (error_mark_node);
12352 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12353 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12354 }
12355 }
12356 else
12357 {
12358 /* A local variable. */
12359 local_p = true;
12360 /* Subsequent calls to pushdecl will fill this in. */
12361 ctx = NULL_TREE;
12362 /* Unless this is a reference to a static variable from an
12363 enclosing function, in which case we need to fill it in now. */
12364 if (TREE_STATIC (t))
12365 {
12366 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12367 if (fn != current_function_decl)
12368 ctx = fn;
12369 }
12370 spec = retrieve_local_specialization (t);
12371 }
12372 /* If we already have the specialization we need, there is
12373 nothing more to do. */
12374 if (spec)
12375 {
12376 r = spec;
12377 break;
12378 }
12379
12380 /* Create a new node for the specialization we need. */
12381 r = copy_decl (t);
12382 if (type == NULL_TREE)
12383 {
12384 if (is_typedef_decl (t))
12385 type = DECL_ORIGINAL_TYPE (t);
12386 else
12387 type = TREE_TYPE (t);
12388 if (VAR_P (t)
12389 && VAR_HAD_UNKNOWN_BOUND (t)
12390 && type != error_mark_node)
12391 type = strip_array_domain (type);
12392 type = tsubst (type, args, complain, in_decl);
12393 }
12394 if (VAR_P (r))
12395 {
12396 /* Even if the original location is out of scope, the
12397 newly substituted one is not. */
12398 DECL_DEAD_FOR_LOCAL (r) = 0;
12399 DECL_INITIALIZED_P (r) = 0;
12400 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12401 if (type == error_mark_node)
12402 RETURN (error_mark_node);
12403 if (TREE_CODE (type) == FUNCTION_TYPE)
12404 {
12405 /* It may seem that this case cannot occur, since:
12406
12407 typedef void f();
12408 void g() { f x; }
12409
12410 declares a function, not a variable. However:
12411
12412 typedef void f();
12413 template <typename T> void g() { T t; }
12414 template void g<f>();
12415
12416 is an attempt to declare a variable with function
12417 type. */
12418 error ("variable %qD has function type",
12419 /* R is not yet sufficiently initialized, so we
12420 just use its name. */
12421 DECL_NAME (r));
12422 RETURN (error_mark_node);
12423 }
12424 type = complete_type (type);
12425 /* Wait until cp_finish_decl to set this again, to handle
12426 circular dependency (template/instantiate6.C). */
12427 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12428 type = check_var_type (DECL_NAME (r), type);
12429
12430 if (DECL_HAS_VALUE_EXPR_P (t))
12431 {
12432 tree ve = DECL_VALUE_EXPR (t);
12433 ve = tsubst_expr (ve, args, complain, in_decl,
12434 /*constant_expression_p=*/false);
12435 if (REFERENCE_REF_P (ve))
12436 {
12437 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12438 ve = TREE_OPERAND (ve, 0);
12439 }
12440 SET_DECL_VALUE_EXPR (r, ve);
12441 }
12442 if (CP_DECL_THREAD_LOCAL_P (r)
12443 && !processing_template_decl)
12444 set_decl_tls_model (r, decl_default_tls_model (r));
12445 }
12446 else if (DECL_SELF_REFERENCE_P (t))
12447 SET_DECL_SELF_REFERENCE_P (r);
12448 TREE_TYPE (r) = type;
12449 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12450 DECL_CONTEXT (r) = ctx;
12451 /* Clear out the mangled name and RTL for the instantiation. */
12452 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12453 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12454 SET_DECL_RTL (r, NULL);
12455 /* The initializer must not be expanded until it is required;
12456 see [temp.inst]. */
12457 DECL_INITIAL (r) = NULL_TREE;
12458 if (VAR_P (r))
12459 DECL_MODE (r) = VOIDmode;
12460 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12461 SET_DECL_RTL (r, NULL);
12462 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12463 if (VAR_P (r))
12464 {
12465 /* Possibly limit visibility based on template args. */
12466 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12467 if (DECL_VISIBILITY_SPECIFIED (t))
12468 {
12469 DECL_VISIBILITY_SPECIFIED (r) = 0;
12470 DECL_ATTRIBUTES (r)
12471 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12472 }
12473 determine_visibility (r);
12474 }
12475
12476 if (!local_p)
12477 {
12478 /* A static data member declaration is always marked
12479 external when it is declared in-class, even if an
12480 initializer is present. We mimic the non-template
12481 processing here. */
12482 DECL_EXTERNAL (r) = 1;
12483 if (DECL_NAMESPACE_SCOPE_P (t))
12484 DECL_NOT_REALLY_EXTERN (r) = 1;
12485
12486 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12487 SET_DECL_IMPLICIT_INSTANTIATION (r);
12488 register_specialization (r, gen_tmpl, argvec, false, hash);
12489 }
12490 else
12491 {
12492 if (DECL_LANG_SPECIFIC (r))
12493 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12494 if (!cp_unevaluated_operand)
12495 register_local_specialization (r, t);
12496 }
12497
12498 DECL_CHAIN (r) = NULL_TREE;
12499
12500 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12501 /*flags=*/0,
12502 args, complain, in_decl);
12503
12504 /* Preserve a typedef that names a type. */
12505 if (is_typedef_decl (r))
12506 {
12507 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12508 set_underlying_type (r);
12509 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12510 /* An alias template specialization can be dependent
12511 even if its underlying type is not. */
12512 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12513 }
12514
12515 layout_decl (r, 0);
12516 }
12517 break;
12518
12519 default:
12520 gcc_unreachable ();
12521 }
12522 #undef RETURN
12523
12524 out:
12525 /* Restore the file and line information. */
12526 input_location = saved_loc;
12527
12528 return r;
12529 }
12530
12531 /* Substitute into the ARG_TYPES of a function type.
12532 If END is a TREE_CHAIN, leave it and any following types
12533 un-substituted. */
12534
12535 static tree
12536 tsubst_arg_types (tree arg_types,
12537 tree args,
12538 tree end,
12539 tsubst_flags_t complain,
12540 tree in_decl)
12541 {
12542 tree remaining_arg_types;
12543 tree type = NULL_TREE;
12544 int i = 1;
12545 tree expanded_args = NULL_TREE;
12546 tree default_arg;
12547
12548 if (!arg_types || arg_types == void_list_node || arg_types == end)
12549 return arg_types;
12550
12551 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12552 args, end, complain, in_decl);
12553 if (remaining_arg_types == error_mark_node)
12554 return error_mark_node;
12555
12556 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12557 {
12558 /* For a pack expansion, perform substitution on the
12559 entire expression. Later on, we'll handle the arguments
12560 one-by-one. */
12561 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12562 args, complain, in_decl);
12563
12564 if (TREE_CODE (expanded_args) == TREE_VEC)
12565 /* So that we'll spin through the parameters, one by one. */
12566 i = TREE_VEC_LENGTH (expanded_args);
12567 else
12568 {
12569 /* We only partially substituted into the parameter
12570 pack. Our type is TYPE_PACK_EXPANSION. */
12571 type = expanded_args;
12572 expanded_args = NULL_TREE;
12573 }
12574 }
12575
12576 while (i > 0) {
12577 --i;
12578
12579 if (expanded_args)
12580 type = TREE_VEC_ELT (expanded_args, i);
12581 else if (!type)
12582 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12583
12584 if (type == error_mark_node)
12585 return error_mark_node;
12586 if (VOID_TYPE_P (type))
12587 {
12588 if (complain & tf_error)
12589 {
12590 error ("invalid parameter type %qT", type);
12591 if (in_decl)
12592 error ("in declaration %q+D", in_decl);
12593 }
12594 return error_mark_node;
12595 }
12596 /* DR 657. */
12597 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12598 return error_mark_node;
12599
12600 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12601 top-level qualifiers as required. */
12602 type = cv_unqualified (type_decays_to (type));
12603
12604 /* We do not substitute into default arguments here. The standard
12605 mandates that they be instantiated only when needed, which is
12606 done in build_over_call. */
12607 default_arg = TREE_PURPOSE (arg_types);
12608
12609 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12610 {
12611 /* We've instantiated a template before its default arguments
12612 have been parsed. This can happen for a nested template
12613 class, and is not an error unless we require the default
12614 argument in a call of this function. */
12615 remaining_arg_types =
12616 tree_cons (default_arg, type, remaining_arg_types);
12617 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12618 }
12619 else
12620 remaining_arg_types =
12621 hash_tree_cons (default_arg, type, remaining_arg_types);
12622 }
12623
12624 return remaining_arg_types;
12625 }
12626
12627 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12628 *not* handle the exception-specification for FNTYPE, because the
12629 initial substitution of explicitly provided template parameters
12630 during argument deduction forbids substitution into the
12631 exception-specification:
12632
12633 [temp.deduct]
12634
12635 All references in the function type of the function template to the
12636 corresponding template parameters are replaced by the specified tem-
12637 plate argument values. If a substitution in a template parameter or
12638 in the function type of the function template results in an invalid
12639 type, type deduction fails. [Note: The equivalent substitution in
12640 exception specifications is done only when the function is instanti-
12641 ated, at which point a program is ill-formed if the substitution
12642 results in an invalid type.] */
12643
12644 static tree
12645 tsubst_function_type (tree t,
12646 tree args,
12647 tsubst_flags_t complain,
12648 tree in_decl)
12649 {
12650 tree return_type;
12651 tree arg_types = NULL_TREE;
12652 tree fntype;
12653
12654 /* The TYPE_CONTEXT is not used for function/method types. */
12655 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12656
12657 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12658 failure. */
12659 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12660
12661 if (late_return_type_p)
12662 {
12663 /* Substitute the argument types. */
12664 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12665 complain, in_decl);
12666 if (arg_types == error_mark_node)
12667 return error_mark_node;
12668
12669 tree save_ccp = current_class_ptr;
12670 tree save_ccr = current_class_ref;
12671 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12672 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12673 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12674 if (do_inject)
12675 {
12676 /* DR 1207: 'this' is in scope in the trailing return type. */
12677 inject_this_parameter (this_type, cp_type_quals (this_type));
12678 }
12679
12680 /* Substitute the return type. */
12681 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12682
12683 if (do_inject)
12684 {
12685 current_class_ptr = save_ccp;
12686 current_class_ref = save_ccr;
12687 }
12688 }
12689 else
12690 /* Substitute the return type. */
12691 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12692
12693 if (return_type == error_mark_node)
12694 return error_mark_node;
12695 /* DR 486 clarifies that creation of a function type with an
12696 invalid return type is a deduction failure. */
12697 if (TREE_CODE (return_type) == ARRAY_TYPE
12698 || TREE_CODE (return_type) == FUNCTION_TYPE)
12699 {
12700 if (complain & tf_error)
12701 {
12702 if (TREE_CODE (return_type) == ARRAY_TYPE)
12703 error ("function returning an array");
12704 else
12705 error ("function returning a function");
12706 }
12707 return error_mark_node;
12708 }
12709 /* And DR 657. */
12710 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12711 return error_mark_node;
12712
12713 if (!late_return_type_p)
12714 {
12715 /* Substitute the argument types. */
12716 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12717 complain, in_decl);
12718 if (arg_types == error_mark_node)
12719 return error_mark_node;
12720 }
12721
12722 /* Construct a new type node and return it. */
12723 if (TREE_CODE (t) == FUNCTION_TYPE)
12724 {
12725 fntype = build_function_type (return_type, arg_types);
12726 fntype = apply_memfn_quals (fntype,
12727 type_memfn_quals (t),
12728 type_memfn_rqual (t));
12729 }
12730 else
12731 {
12732 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12733 /* Don't pick up extra function qualifiers from the basetype. */
12734 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12735 if (! MAYBE_CLASS_TYPE_P (r))
12736 {
12737 /* [temp.deduct]
12738
12739 Type deduction may fail for any of the following
12740 reasons:
12741
12742 -- Attempting to create "pointer to member of T" when T
12743 is not a class type. */
12744 if (complain & tf_error)
12745 error ("creating pointer to member function of non-class type %qT",
12746 r);
12747 return error_mark_node;
12748 }
12749
12750 fntype = build_method_type_directly (r, return_type,
12751 TREE_CHAIN (arg_types));
12752 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12753 }
12754 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12755
12756 if (late_return_type_p)
12757 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12758
12759 return fntype;
12760 }
12761
12762 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12763 ARGS into that specification, and return the substituted
12764 specification. If there is no specification, return NULL_TREE. */
12765
12766 static tree
12767 tsubst_exception_specification (tree fntype,
12768 tree args,
12769 tsubst_flags_t complain,
12770 tree in_decl,
12771 bool defer_ok)
12772 {
12773 tree specs;
12774 tree new_specs;
12775
12776 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12777 new_specs = NULL_TREE;
12778 if (specs && TREE_PURPOSE (specs))
12779 {
12780 /* A noexcept-specifier. */
12781 tree expr = TREE_PURPOSE (specs);
12782 if (TREE_CODE (expr) == INTEGER_CST)
12783 new_specs = expr;
12784 else if (defer_ok)
12785 {
12786 /* Defer instantiation of noexcept-specifiers to avoid
12787 excessive instantiations (c++/49107). */
12788 new_specs = make_node (DEFERRED_NOEXCEPT);
12789 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12790 {
12791 /* We already partially instantiated this member template,
12792 so combine the new args with the old. */
12793 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12794 = DEFERRED_NOEXCEPT_PATTERN (expr);
12795 DEFERRED_NOEXCEPT_ARGS (new_specs)
12796 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12797 }
12798 else
12799 {
12800 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12801 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12802 }
12803 }
12804 else
12805 new_specs = tsubst_copy_and_build
12806 (expr, args, complain, in_decl, /*function_p=*/false,
12807 /*integral_constant_expression_p=*/true);
12808 new_specs = build_noexcept_spec (new_specs, complain);
12809 }
12810 else if (specs)
12811 {
12812 if (! TREE_VALUE (specs))
12813 new_specs = specs;
12814 else
12815 while (specs)
12816 {
12817 tree spec;
12818 int i, len = 1;
12819 tree expanded_specs = NULL_TREE;
12820
12821 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12822 {
12823 /* Expand the pack expansion type. */
12824 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12825 args, complain,
12826 in_decl);
12827
12828 if (expanded_specs == error_mark_node)
12829 return error_mark_node;
12830 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12831 len = TREE_VEC_LENGTH (expanded_specs);
12832 else
12833 {
12834 /* We're substituting into a member template, so
12835 we got a TYPE_PACK_EXPANSION back. Add that
12836 expansion and move on. */
12837 gcc_assert (TREE_CODE (expanded_specs)
12838 == TYPE_PACK_EXPANSION);
12839 new_specs = add_exception_specifier (new_specs,
12840 expanded_specs,
12841 complain);
12842 specs = TREE_CHAIN (specs);
12843 continue;
12844 }
12845 }
12846
12847 for (i = 0; i < len; ++i)
12848 {
12849 if (expanded_specs)
12850 spec = TREE_VEC_ELT (expanded_specs, i);
12851 else
12852 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12853 if (spec == error_mark_node)
12854 return spec;
12855 new_specs = add_exception_specifier (new_specs, spec,
12856 complain);
12857 }
12858
12859 specs = TREE_CHAIN (specs);
12860 }
12861 }
12862 return new_specs;
12863 }
12864
12865 /* Take the tree structure T and replace template parameters used
12866 therein with the argument vector ARGS. IN_DECL is an associated
12867 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12868 Issue error and warning messages under control of COMPLAIN. Note
12869 that we must be relatively non-tolerant of extensions here, in
12870 order to preserve conformance; if we allow substitutions that
12871 should not be allowed, we may allow argument deductions that should
12872 not succeed, and therefore report ambiguous overload situations
12873 where there are none. In theory, we could allow the substitution,
12874 but indicate that it should have failed, and allow our caller to
12875 make sure that the right thing happens, but we don't try to do this
12876 yet.
12877
12878 This function is used for dealing with types, decls and the like;
12879 for expressions, use tsubst_expr or tsubst_copy. */
12880
12881 tree
12882 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12883 {
12884 enum tree_code code;
12885 tree type, r = NULL_TREE;
12886
12887 if (t == NULL_TREE || t == error_mark_node
12888 || t == integer_type_node
12889 || t == void_type_node
12890 || t == char_type_node
12891 || t == unknown_type_node
12892 || TREE_CODE (t) == NAMESPACE_DECL
12893 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12894 return t;
12895
12896 if (DECL_P (t))
12897 return tsubst_decl (t, args, complain);
12898
12899 if (args == NULL_TREE)
12900 return t;
12901
12902 code = TREE_CODE (t);
12903
12904 if (code == IDENTIFIER_NODE)
12905 type = IDENTIFIER_TYPE_VALUE (t);
12906 else
12907 type = TREE_TYPE (t);
12908
12909 gcc_assert (type != unknown_type_node);
12910
12911 /* Reuse typedefs. We need to do this to handle dependent attributes,
12912 such as attribute aligned. */
12913 if (TYPE_P (t)
12914 && typedef_variant_p (t))
12915 {
12916 tree decl = TYPE_NAME (t);
12917
12918 if (alias_template_specialization_p (t))
12919 {
12920 /* DECL represents an alias template and we want to
12921 instantiate it. */
12922 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12923 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12924 r = instantiate_alias_template (tmpl, gen_args, complain);
12925 }
12926 else if (DECL_CLASS_SCOPE_P (decl)
12927 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12928 && uses_template_parms (DECL_CONTEXT (decl)))
12929 {
12930 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12931 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12932 r = retrieve_specialization (tmpl, gen_args, 0);
12933 }
12934 else if (DECL_FUNCTION_SCOPE_P (decl)
12935 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12936 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12937 r = retrieve_local_specialization (decl);
12938 else
12939 /* The typedef is from a non-template context. */
12940 return t;
12941
12942 if (r)
12943 {
12944 r = TREE_TYPE (r);
12945 r = cp_build_qualified_type_real
12946 (r, cp_type_quals (t) | cp_type_quals (r),
12947 complain | tf_ignore_bad_quals);
12948 return r;
12949 }
12950 else
12951 {
12952 /* We don't have an instantiation yet, so drop the typedef. */
12953 int quals = cp_type_quals (t);
12954 t = DECL_ORIGINAL_TYPE (decl);
12955 t = cp_build_qualified_type_real (t, quals,
12956 complain | tf_ignore_bad_quals);
12957 }
12958 }
12959
12960 if (type
12961 && code != TYPENAME_TYPE
12962 && code != TEMPLATE_TYPE_PARM
12963 && code != IDENTIFIER_NODE
12964 && code != FUNCTION_TYPE
12965 && code != METHOD_TYPE)
12966 type = tsubst (type, args, complain, in_decl);
12967 if (type == error_mark_node)
12968 return error_mark_node;
12969
12970 switch (code)
12971 {
12972 case RECORD_TYPE:
12973 case UNION_TYPE:
12974 case ENUMERAL_TYPE:
12975 return tsubst_aggr_type (t, args, complain, in_decl,
12976 /*entering_scope=*/0);
12977
12978 case ERROR_MARK:
12979 case IDENTIFIER_NODE:
12980 case VOID_TYPE:
12981 case REAL_TYPE:
12982 case COMPLEX_TYPE:
12983 case VECTOR_TYPE:
12984 case BOOLEAN_TYPE:
12985 case NULLPTR_TYPE:
12986 case LANG_TYPE:
12987 return t;
12988
12989 case INTEGER_TYPE:
12990 if (t == integer_type_node)
12991 return t;
12992
12993 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
12994 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
12995 return t;
12996
12997 {
12998 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
12999
13000 max = tsubst_expr (omax, args, complain, in_decl,
13001 /*integral_constant_expression_p=*/false);
13002
13003 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13004 needed. */
13005 if (TREE_CODE (max) == NOP_EXPR
13006 && TREE_SIDE_EFFECTS (omax)
13007 && !TREE_TYPE (max))
13008 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13009
13010 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13011 with TREE_SIDE_EFFECTS that indicates this is not an integral
13012 constant expression. */
13013 if (processing_template_decl
13014 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13015 {
13016 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13017 TREE_SIDE_EFFECTS (max) = 1;
13018 }
13019
13020 return compute_array_index_type (NULL_TREE, max, complain);
13021 }
13022
13023 case TEMPLATE_TYPE_PARM:
13024 case TEMPLATE_TEMPLATE_PARM:
13025 case BOUND_TEMPLATE_TEMPLATE_PARM:
13026 case TEMPLATE_PARM_INDEX:
13027 {
13028 int idx;
13029 int level;
13030 int levels;
13031 tree arg = NULL_TREE;
13032
13033 /* Early in template argument deduction substitution, we don't
13034 want to reduce the level of 'auto', or it will be confused
13035 with a normal template parm in subsequent deduction. */
13036 if (is_auto (t) && (complain & tf_partial))
13037 return t;
13038
13039 r = NULL_TREE;
13040
13041 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13042 template_parm_level_and_index (t, &level, &idx);
13043
13044 levels = TMPL_ARGS_DEPTH (args);
13045 if (level <= levels
13046 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13047 {
13048 arg = TMPL_ARG (args, level, idx);
13049
13050 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13051 {
13052 /* See through ARGUMENT_PACK_SELECT arguments. */
13053 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13054 /* If the selected argument is an expansion E, that most
13055 likely means we were called from
13056 gen_elem_of_pack_expansion_instantiation during the
13057 substituting of pack an argument pack (which Ith
13058 element is a pack expansion, where I is
13059 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13060 In this case, the Ith element resulting from this
13061 substituting is going to be a pack expansion, which
13062 pattern is the pattern of E. Let's return the
13063 pattern of E, and
13064 gen_elem_of_pack_expansion_instantiation will
13065 build the resulting pack expansion from it. */
13066 if (PACK_EXPANSION_P (arg))
13067 {
13068 /* Make sure we aren't throwing away arg info. */
13069 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13070 arg = PACK_EXPANSION_PATTERN (arg);
13071 }
13072 }
13073 }
13074
13075 if (arg == error_mark_node)
13076 return error_mark_node;
13077 else if (arg != NULL_TREE)
13078 {
13079 if (ARGUMENT_PACK_P (arg))
13080 /* If ARG is an argument pack, we don't actually want to
13081 perform a substitution here, because substitutions
13082 for argument packs are only done
13083 element-by-element. We can get to this point when
13084 substituting the type of a non-type template
13085 parameter pack, when that type actually contains
13086 template parameter packs from an outer template, e.g.,
13087
13088 template<typename... Types> struct A {
13089 template<Types... Values> struct B { };
13090 }; */
13091 return t;
13092
13093 if (code == TEMPLATE_TYPE_PARM)
13094 {
13095 int quals;
13096 gcc_assert (TYPE_P (arg));
13097
13098 quals = cp_type_quals (arg) | cp_type_quals (t);
13099
13100 return cp_build_qualified_type_real
13101 (arg, quals, complain | tf_ignore_bad_quals);
13102 }
13103 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13104 {
13105 /* We are processing a type constructed from a
13106 template template parameter. */
13107 tree argvec = tsubst (TYPE_TI_ARGS (t),
13108 args, complain, in_decl);
13109 if (argvec == error_mark_node)
13110 return error_mark_node;
13111
13112 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13113 || TREE_CODE (arg) == TEMPLATE_DECL
13114 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13115
13116 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13117 /* Consider this code:
13118
13119 template <template <class> class Template>
13120 struct Internal {
13121 template <class Arg> using Bind = Template<Arg>;
13122 };
13123
13124 template <template <class> class Template, class Arg>
13125 using Instantiate = Template<Arg>; //#0
13126
13127 template <template <class> class Template,
13128 class Argument>
13129 using Bind =
13130 Instantiate<Internal<Template>::template Bind,
13131 Argument>; //#1
13132
13133 When #1 is parsed, the
13134 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13135 parameter `Template' in #0 matches the
13136 UNBOUND_CLASS_TEMPLATE representing the argument
13137 `Internal<Template>::template Bind'; We then want
13138 to assemble the type `Bind<Argument>' that can't
13139 be fully created right now, because
13140 `Internal<Template>' not being complete, the Bind
13141 template cannot be looked up in that context. So
13142 we need to "store" `Bind<Argument>' for later
13143 when the context of Bind becomes complete. Let's
13144 store that in a TYPENAME_TYPE. */
13145 return make_typename_type (TYPE_CONTEXT (arg),
13146 build_nt (TEMPLATE_ID_EXPR,
13147 TYPE_IDENTIFIER (arg),
13148 argvec),
13149 typename_type,
13150 complain);
13151
13152 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13153 are resolving nested-types in the signature of a
13154 member function templates. Otherwise ARG is a
13155 TEMPLATE_DECL and is the real template to be
13156 instantiated. */
13157 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13158 arg = TYPE_NAME (arg);
13159
13160 r = lookup_template_class (arg,
13161 argvec, in_decl,
13162 DECL_CONTEXT (arg),
13163 /*entering_scope=*/0,
13164 complain);
13165 return cp_build_qualified_type_real
13166 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13167 }
13168 else
13169 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
13170 return convert_from_reference (unshare_expr (arg));
13171 }
13172
13173 if (level == 1)
13174 /* This can happen during the attempted tsubst'ing in
13175 unify. This means that we don't yet have any information
13176 about the template parameter in question. */
13177 return t;
13178
13179 /* If we get here, we must have been looking at a parm for a
13180 more deeply nested template. Make a new version of this
13181 template parameter, but with a lower level. */
13182 switch (code)
13183 {
13184 case TEMPLATE_TYPE_PARM:
13185 case TEMPLATE_TEMPLATE_PARM:
13186 case BOUND_TEMPLATE_TEMPLATE_PARM:
13187 if (cp_type_quals (t))
13188 {
13189 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13190 r = cp_build_qualified_type_real
13191 (r, cp_type_quals (t),
13192 complain | (code == TEMPLATE_TYPE_PARM
13193 ? tf_ignore_bad_quals : 0));
13194 }
13195 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13196 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13197 && (r = (TEMPLATE_PARM_DESCENDANTS
13198 (TEMPLATE_TYPE_PARM_INDEX (t))))
13199 && (r = TREE_TYPE (r))
13200 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13201 /* Break infinite recursion when substituting the constraints
13202 of a constrained placeholder. */;
13203 else
13204 {
13205 r = copy_type (t);
13206 TEMPLATE_TYPE_PARM_INDEX (r)
13207 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13208 r, levels, args, complain);
13209 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13210 TYPE_MAIN_VARIANT (r) = r;
13211 TYPE_POINTER_TO (r) = NULL_TREE;
13212 TYPE_REFERENCE_TO (r) = NULL_TREE;
13213
13214 /* Propagate constraints on placeholders. */
13215 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13216 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13217 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13218 = tsubst_constraint (constr, args, complain, in_decl);
13219
13220 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13221 /* We have reduced the level of the template
13222 template parameter, but not the levels of its
13223 template parameters, so canonical_type_parameter
13224 will not be able to find the canonical template
13225 template parameter for this level. Thus, we
13226 require structural equality checking to compare
13227 TEMPLATE_TEMPLATE_PARMs. */
13228 SET_TYPE_STRUCTURAL_EQUALITY (r);
13229 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13230 SET_TYPE_STRUCTURAL_EQUALITY (r);
13231 else
13232 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13233
13234 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13235 {
13236 tree tinfo = TYPE_TEMPLATE_INFO (t);
13237 /* We might need to substitute into the types of non-type
13238 template parameters. */
13239 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13240 complain, in_decl);
13241 if (tmpl == error_mark_node)
13242 return error_mark_node;
13243 tree argvec = tsubst (TI_ARGS (tinfo), args,
13244 complain, in_decl);
13245 if (argvec == error_mark_node)
13246 return error_mark_node;
13247
13248 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13249 = build_template_info (tmpl, argvec);
13250 }
13251 }
13252 break;
13253
13254 case TEMPLATE_PARM_INDEX:
13255 r = reduce_template_parm_level (t, type, levels, args, complain);
13256 break;
13257
13258 default:
13259 gcc_unreachable ();
13260 }
13261
13262 return r;
13263 }
13264
13265 case TREE_LIST:
13266 {
13267 tree purpose, value, chain;
13268
13269 if (t == void_list_node)
13270 return t;
13271
13272 purpose = TREE_PURPOSE (t);
13273 if (purpose)
13274 {
13275 purpose = tsubst (purpose, args, complain, in_decl);
13276 if (purpose == error_mark_node)
13277 return error_mark_node;
13278 }
13279 value = TREE_VALUE (t);
13280 if (value)
13281 {
13282 value = tsubst (value, args, complain, in_decl);
13283 if (value == error_mark_node)
13284 return error_mark_node;
13285 }
13286 chain = TREE_CHAIN (t);
13287 if (chain && chain != void_type_node)
13288 {
13289 chain = tsubst (chain, args, complain, in_decl);
13290 if (chain == error_mark_node)
13291 return error_mark_node;
13292 }
13293 if (purpose == TREE_PURPOSE (t)
13294 && value == TREE_VALUE (t)
13295 && chain == TREE_CHAIN (t))
13296 return t;
13297 return hash_tree_cons (purpose, value, chain);
13298 }
13299
13300 case TREE_BINFO:
13301 /* We should never be tsubsting a binfo. */
13302 gcc_unreachable ();
13303
13304 case TREE_VEC:
13305 /* A vector of template arguments. */
13306 gcc_assert (!type);
13307 return tsubst_template_args (t, args, complain, in_decl);
13308
13309 case POINTER_TYPE:
13310 case REFERENCE_TYPE:
13311 {
13312 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13313 return t;
13314
13315 /* [temp.deduct]
13316
13317 Type deduction may fail for any of the following
13318 reasons:
13319
13320 -- Attempting to create a pointer to reference type.
13321 -- Attempting to create a reference to a reference type or
13322 a reference to void.
13323
13324 Core issue 106 says that creating a reference to a reference
13325 during instantiation is no longer a cause for failure. We
13326 only enforce this check in strict C++98 mode. */
13327 if ((TREE_CODE (type) == REFERENCE_TYPE
13328 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13329 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13330 {
13331 static location_t last_loc;
13332
13333 /* We keep track of the last time we issued this error
13334 message to avoid spewing a ton of messages during a
13335 single bad template instantiation. */
13336 if (complain & tf_error
13337 && last_loc != input_location)
13338 {
13339 if (VOID_TYPE_P (type))
13340 error ("forming reference to void");
13341 else if (code == POINTER_TYPE)
13342 error ("forming pointer to reference type %qT", type);
13343 else
13344 error ("forming reference to reference type %qT", type);
13345 last_loc = input_location;
13346 }
13347
13348 return error_mark_node;
13349 }
13350 else if (TREE_CODE (type) == FUNCTION_TYPE
13351 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13352 || type_memfn_rqual (type) != REF_QUAL_NONE))
13353 {
13354 if (complain & tf_error)
13355 {
13356 if (code == POINTER_TYPE)
13357 error ("forming pointer to qualified function type %qT",
13358 type);
13359 else
13360 error ("forming reference to qualified function type %qT",
13361 type);
13362 }
13363 return error_mark_node;
13364 }
13365 else if (code == POINTER_TYPE)
13366 {
13367 r = build_pointer_type (type);
13368 if (TREE_CODE (type) == METHOD_TYPE)
13369 r = build_ptrmemfunc_type (r);
13370 }
13371 else if (TREE_CODE (type) == REFERENCE_TYPE)
13372 /* In C++0x, during template argument substitution, when there is an
13373 attempt to create a reference to a reference type, reference
13374 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13375
13376 "If a template-argument for a template-parameter T names a type
13377 that is a reference to a type A, an attempt to create the type
13378 'lvalue reference to cv T' creates the type 'lvalue reference to
13379 A,' while an attempt to create the type type rvalue reference to
13380 cv T' creates the type T"
13381 */
13382 r = cp_build_reference_type
13383 (TREE_TYPE (type),
13384 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13385 else
13386 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13387 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13388
13389 if (r != error_mark_node)
13390 /* Will this ever be needed for TYPE_..._TO values? */
13391 layout_type (r);
13392
13393 return r;
13394 }
13395 case OFFSET_TYPE:
13396 {
13397 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13398 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13399 {
13400 /* [temp.deduct]
13401
13402 Type deduction may fail for any of the following
13403 reasons:
13404
13405 -- Attempting to create "pointer to member of T" when T
13406 is not a class type. */
13407 if (complain & tf_error)
13408 error ("creating pointer to member of non-class type %qT", r);
13409 return error_mark_node;
13410 }
13411 if (TREE_CODE (type) == REFERENCE_TYPE)
13412 {
13413 if (complain & tf_error)
13414 error ("creating pointer to member reference type %qT", type);
13415 return error_mark_node;
13416 }
13417 if (VOID_TYPE_P (type))
13418 {
13419 if (complain & tf_error)
13420 error ("creating pointer to member of type void");
13421 return error_mark_node;
13422 }
13423 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13424 if (TREE_CODE (type) == FUNCTION_TYPE)
13425 {
13426 /* The type of the implicit object parameter gets its
13427 cv-qualifiers from the FUNCTION_TYPE. */
13428 tree memptr;
13429 tree method_type
13430 = build_memfn_type (type, r, type_memfn_quals (type),
13431 type_memfn_rqual (type));
13432 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13433 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13434 complain);
13435 }
13436 else
13437 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13438 cp_type_quals (t),
13439 complain);
13440 }
13441 case FUNCTION_TYPE:
13442 case METHOD_TYPE:
13443 {
13444 tree fntype;
13445 tree specs;
13446 fntype = tsubst_function_type (t, args, complain, in_decl);
13447 if (fntype == error_mark_node)
13448 return error_mark_node;
13449
13450 /* Substitute the exception specification. */
13451 specs = tsubst_exception_specification (t, args, complain,
13452 in_decl, /*defer_ok*/true);
13453 if (specs == error_mark_node)
13454 return error_mark_node;
13455 if (specs)
13456 fntype = build_exception_variant (fntype, specs);
13457 return fntype;
13458 }
13459 case ARRAY_TYPE:
13460 {
13461 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13462 if (domain == error_mark_node)
13463 return error_mark_node;
13464
13465 /* As an optimization, we avoid regenerating the array type if
13466 it will obviously be the same as T. */
13467 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13468 return t;
13469
13470 /* These checks should match the ones in create_array_type_for_decl.
13471
13472 [temp.deduct]
13473
13474 The deduction may fail for any of the following reasons:
13475
13476 -- Attempting to create an array with an element type that
13477 is void, a function type, or a reference type, or [DR337]
13478 an abstract class type. */
13479 if (VOID_TYPE_P (type)
13480 || TREE_CODE (type) == FUNCTION_TYPE
13481 || (TREE_CODE (type) == ARRAY_TYPE
13482 && TYPE_DOMAIN (type) == NULL_TREE)
13483 || TREE_CODE (type) == REFERENCE_TYPE)
13484 {
13485 if (complain & tf_error)
13486 error ("creating array of %qT", type);
13487 return error_mark_node;
13488 }
13489
13490 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13491 return error_mark_node;
13492
13493 r = build_cplus_array_type (type, domain);
13494
13495 if (TYPE_USER_ALIGN (t))
13496 {
13497 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13498 TYPE_USER_ALIGN (r) = 1;
13499 }
13500
13501 return r;
13502 }
13503
13504 case TYPENAME_TYPE:
13505 {
13506 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13507 in_decl, /*entering_scope=*/1);
13508 if (ctx == error_mark_node)
13509 return error_mark_node;
13510
13511 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13512 complain, in_decl);
13513 if (f == error_mark_node)
13514 return error_mark_node;
13515
13516 if (!MAYBE_CLASS_TYPE_P (ctx))
13517 {
13518 if (complain & tf_error)
13519 error ("%qT is not a class, struct, or union type", ctx);
13520 return error_mark_node;
13521 }
13522 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13523 {
13524 /* Normally, make_typename_type does not require that the CTX
13525 have complete type in order to allow things like:
13526
13527 template <class T> struct S { typename S<T>::X Y; };
13528
13529 But, such constructs have already been resolved by this
13530 point, so here CTX really should have complete type, unless
13531 it's a partial instantiation. */
13532 ctx = complete_type (ctx);
13533 if (!COMPLETE_TYPE_P (ctx))
13534 {
13535 if (complain & tf_error)
13536 cxx_incomplete_type_error (NULL_TREE, ctx);
13537 return error_mark_node;
13538 }
13539 }
13540
13541 f = make_typename_type (ctx, f, typename_type,
13542 complain | tf_keep_type_decl);
13543 if (f == error_mark_node)
13544 return f;
13545 if (TREE_CODE (f) == TYPE_DECL)
13546 {
13547 complain |= tf_ignore_bad_quals;
13548 f = TREE_TYPE (f);
13549 }
13550
13551 if (TREE_CODE (f) != TYPENAME_TYPE)
13552 {
13553 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13554 {
13555 if (complain & tf_error)
13556 error ("%qT resolves to %qT, which is not an enumeration type",
13557 t, f);
13558 else
13559 return error_mark_node;
13560 }
13561 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13562 {
13563 if (complain & tf_error)
13564 error ("%qT resolves to %qT, which is is not a class type",
13565 t, f);
13566 else
13567 return error_mark_node;
13568 }
13569 }
13570
13571 return cp_build_qualified_type_real
13572 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13573 }
13574
13575 case UNBOUND_CLASS_TEMPLATE:
13576 {
13577 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13578 in_decl, /*entering_scope=*/1);
13579 tree name = TYPE_IDENTIFIER (t);
13580 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13581
13582 if (ctx == error_mark_node || name == error_mark_node)
13583 return error_mark_node;
13584
13585 if (parm_list)
13586 parm_list = tsubst_template_parms (parm_list, args, complain);
13587 return make_unbound_class_template (ctx, name, parm_list, complain);
13588 }
13589
13590 case TYPEOF_TYPE:
13591 {
13592 tree type;
13593
13594 ++cp_unevaluated_operand;
13595 ++c_inhibit_evaluation_warnings;
13596
13597 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13598 complain, in_decl,
13599 /*integral_constant_expression_p=*/false);
13600
13601 --cp_unevaluated_operand;
13602 --c_inhibit_evaluation_warnings;
13603
13604 type = finish_typeof (type);
13605 return cp_build_qualified_type_real (type,
13606 cp_type_quals (t)
13607 | cp_type_quals (type),
13608 complain);
13609 }
13610
13611 case DECLTYPE_TYPE:
13612 {
13613 tree type;
13614
13615 ++cp_unevaluated_operand;
13616 ++c_inhibit_evaluation_warnings;
13617
13618 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13619 complain|tf_decltype, in_decl,
13620 /*function_p*/false,
13621 /*integral_constant_expression*/false);
13622
13623 if (DECLTYPE_FOR_INIT_CAPTURE (t))
13624 {
13625 if (type == NULL_TREE)
13626 {
13627 if (complain & tf_error)
13628 error ("empty initializer in lambda init-capture");
13629 type = error_mark_node;
13630 }
13631 else if (TREE_CODE (type) == TREE_LIST)
13632 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
13633 }
13634
13635 --cp_unevaluated_operand;
13636 --c_inhibit_evaluation_warnings;
13637
13638 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13639 type = lambda_capture_field_type (type,
13640 DECLTYPE_FOR_INIT_CAPTURE (t));
13641 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13642 type = lambda_proxy_type (type);
13643 else
13644 {
13645 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13646 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13647 && EXPR_P (type))
13648 /* In a template ~id could be either a complement expression
13649 or an unqualified-id naming a destructor; if instantiating
13650 it produces an expression, it's not an id-expression or
13651 member access. */
13652 id = false;
13653 type = finish_decltype_type (type, id, complain);
13654 }
13655 return cp_build_qualified_type_real (type,
13656 cp_type_quals (t)
13657 | cp_type_quals (type),
13658 complain | tf_ignore_bad_quals);
13659 }
13660
13661 case UNDERLYING_TYPE:
13662 {
13663 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13664 complain, in_decl);
13665 return finish_underlying_type (type);
13666 }
13667
13668 case TYPE_ARGUMENT_PACK:
13669 case NONTYPE_ARGUMENT_PACK:
13670 {
13671 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13672 tree packed_out =
13673 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13674 args,
13675 complain,
13676 in_decl);
13677 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13678
13679 /* For template nontype argument packs, also substitute into
13680 the type. */
13681 if (code == NONTYPE_ARGUMENT_PACK)
13682 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13683
13684 return r;
13685 }
13686 break;
13687
13688 case VOID_CST:
13689 case INTEGER_CST:
13690 case REAL_CST:
13691 case STRING_CST:
13692 case PLUS_EXPR:
13693 case MINUS_EXPR:
13694 case NEGATE_EXPR:
13695 case NOP_EXPR:
13696 case INDIRECT_REF:
13697 case ADDR_EXPR:
13698 case CALL_EXPR:
13699 case ARRAY_REF:
13700 case SCOPE_REF:
13701 /* We should use one of the expression tsubsts for these codes. */
13702 gcc_unreachable ();
13703
13704 default:
13705 sorry ("use of %qs in template", get_tree_code_name (code));
13706 return error_mark_node;
13707 }
13708 }
13709
13710 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13711 type of the expression on the left-hand side of the "." or "->"
13712 operator. */
13713
13714 static tree
13715 tsubst_baselink (tree baselink, tree object_type,
13716 tree args, tsubst_flags_t complain, tree in_decl)
13717 {
13718 tree name;
13719 tree qualifying_scope;
13720 tree fns;
13721 tree optype;
13722 tree template_args = 0;
13723 bool template_id_p = false;
13724 bool qualified = BASELINK_QUALIFIED_P (baselink);
13725
13726 /* A baselink indicates a function from a base class. Both the
13727 BASELINK_ACCESS_BINFO and the base class referenced may
13728 indicate bases of the template class, rather than the
13729 instantiated class. In addition, lookups that were not
13730 ambiguous before may be ambiguous now. Therefore, we perform
13731 the lookup again. */
13732 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13733 qualifying_scope = tsubst (qualifying_scope, args,
13734 complain, in_decl);
13735 fns = BASELINK_FUNCTIONS (baselink);
13736 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13737 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13738 {
13739 template_id_p = true;
13740 template_args = TREE_OPERAND (fns, 1);
13741 fns = TREE_OPERAND (fns, 0);
13742 if (template_args)
13743 template_args = tsubst_template_args (template_args, args,
13744 complain, in_decl);
13745 }
13746 name = DECL_NAME (get_first_fn (fns));
13747 if (IDENTIFIER_TYPENAME_P (name))
13748 name = mangle_conv_op_name_for_type (optype);
13749 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13750 if (!baselink)
13751 {
13752 if (constructor_name_p (name, qualifying_scope))
13753 {
13754 if (complain & tf_error)
13755 error ("cannot call constructor %<%T::%D%> directly",
13756 qualifying_scope, name);
13757 }
13758 return error_mark_node;
13759 }
13760
13761 /* If lookup found a single function, mark it as used at this
13762 point. (If it lookup found multiple functions the one selected
13763 later by overload resolution will be marked as used at that
13764 point.) */
13765 if (BASELINK_P (baselink))
13766 fns = BASELINK_FUNCTIONS (baselink);
13767 if (!template_id_p && !really_overloaded_fn (fns)
13768 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13769 return error_mark_node;
13770
13771 /* Add back the template arguments, if present. */
13772 if (BASELINK_P (baselink) && template_id_p)
13773 BASELINK_FUNCTIONS (baselink)
13774 = build2 (TEMPLATE_ID_EXPR,
13775 unknown_type_node,
13776 BASELINK_FUNCTIONS (baselink),
13777 template_args);
13778 /* Update the conversion operator type. */
13779 if (BASELINK_P (baselink))
13780 BASELINK_OPTYPE (baselink) = optype;
13781
13782 if (!object_type)
13783 object_type = current_class_type;
13784
13785 if (qualified || name == complete_dtor_identifier)
13786 {
13787 baselink = adjust_result_of_qualified_name_lookup (baselink,
13788 qualifying_scope,
13789 object_type);
13790 if (!qualified)
13791 /* We need to call adjust_result_of_qualified_name_lookup in case the
13792 destructor names a base class, but we unset BASELINK_QUALIFIED_P
13793 so that we still get virtual function binding. */
13794 BASELINK_QUALIFIED_P (baselink) = false;
13795 }
13796 return baselink;
13797 }
13798
13799 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13800 true if the qualified-id will be a postfix-expression in-and-of
13801 itself; false if more of the postfix-expression follows the
13802 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13803 of "&". */
13804
13805 static tree
13806 tsubst_qualified_id (tree qualified_id, tree args,
13807 tsubst_flags_t complain, tree in_decl,
13808 bool done, bool address_p)
13809 {
13810 tree expr;
13811 tree scope;
13812 tree name;
13813 bool is_template;
13814 tree template_args;
13815 location_t loc = UNKNOWN_LOCATION;
13816
13817 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13818
13819 /* Figure out what name to look up. */
13820 name = TREE_OPERAND (qualified_id, 1);
13821 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13822 {
13823 is_template = true;
13824 loc = EXPR_LOCATION (name);
13825 template_args = TREE_OPERAND (name, 1);
13826 if (template_args)
13827 template_args = tsubst_template_args (template_args, args,
13828 complain, in_decl);
13829 name = TREE_OPERAND (name, 0);
13830 }
13831 else
13832 {
13833 is_template = false;
13834 template_args = NULL_TREE;
13835 }
13836
13837 /* Substitute into the qualifying scope. When there are no ARGS, we
13838 are just trying to simplify a non-dependent expression. In that
13839 case the qualifying scope may be dependent, and, in any case,
13840 substituting will not help. */
13841 scope = TREE_OPERAND (qualified_id, 0);
13842 if (args)
13843 {
13844 scope = tsubst (scope, args, complain, in_decl);
13845 expr = tsubst_copy (name, args, complain, in_decl);
13846 }
13847 else
13848 expr = name;
13849
13850 if (dependent_scope_p (scope))
13851 {
13852 if (is_template)
13853 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13854 tree r = build_qualified_name (NULL_TREE, scope, expr,
13855 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13856 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
13857 return r;
13858 }
13859
13860 if (!BASELINK_P (name) && !DECL_P (expr))
13861 {
13862 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13863 {
13864 /* A BIT_NOT_EXPR is used to represent a destructor. */
13865 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13866 {
13867 error ("qualifying type %qT does not match destructor name ~%qT",
13868 scope, TREE_OPERAND (expr, 0));
13869 expr = error_mark_node;
13870 }
13871 else
13872 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13873 /*is_type_p=*/0, false);
13874 }
13875 else
13876 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13877 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13878 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13879 {
13880 if (complain & tf_error)
13881 {
13882 error ("dependent-name %qE is parsed as a non-type, but "
13883 "instantiation yields a type", qualified_id);
13884 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13885 }
13886 return error_mark_node;
13887 }
13888 }
13889
13890 if (DECL_P (expr))
13891 {
13892 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13893 scope);
13894 /* Remember that there was a reference to this entity. */
13895 if (!mark_used (expr, complain) && !(complain & tf_error))
13896 return error_mark_node;
13897 }
13898
13899 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13900 {
13901 if (complain & tf_error)
13902 qualified_name_lookup_error (scope,
13903 TREE_OPERAND (qualified_id, 1),
13904 expr, input_location);
13905 return error_mark_node;
13906 }
13907
13908 if (is_template)
13909 {
13910 if (variable_template_p (expr))
13911 expr = lookup_and_finish_template_variable (expr, template_args,
13912 complain);
13913 else
13914 expr = lookup_template_function (expr, template_args);
13915 }
13916
13917 if (expr == error_mark_node && complain & tf_error)
13918 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
13919 expr, input_location);
13920 else if (TYPE_P (scope))
13921 {
13922 expr = (adjust_result_of_qualified_name_lookup
13923 (expr, scope, current_nonlambda_class_type ()));
13924 expr = (finish_qualified_id_expr
13925 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
13926 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
13927 /*template_arg_p=*/false, complain));
13928 }
13929
13930 /* Expressions do not generally have reference type. */
13931 if (TREE_CODE (expr) != SCOPE_REF
13932 /* However, if we're about to form a pointer-to-member, we just
13933 want the referenced member referenced. */
13934 && TREE_CODE (expr) != OFFSET_REF)
13935 expr = convert_from_reference (expr);
13936
13937 if (REF_PARENTHESIZED_P (qualified_id))
13938 expr = force_paren_expr (expr);
13939
13940 return expr;
13941 }
13942
13943 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13944 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13945 for tsubst. */
13946
13947 static tree
13948 tsubst_init (tree init, tree decl, tree args,
13949 tsubst_flags_t complain, tree in_decl)
13950 {
13951 if (!init)
13952 return NULL_TREE;
13953
13954 init = tsubst_expr (init, args, complain, in_decl, false);
13955
13956 if (!init)
13957 {
13958 /* If we had an initializer but it
13959 instantiated to nothing,
13960 value-initialize the object. This will
13961 only occur when the initializer was a
13962 pack expansion where the parameter packs
13963 used in that expansion were of length
13964 zero. */
13965 init = build_value_init (TREE_TYPE (decl),
13966 complain);
13967 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13968 init = get_target_expr_sfinae (init, complain);
13969 }
13970
13971 return init;
13972 }
13973
13974 /* Like tsubst, but deals with expressions. This function just replaces
13975 template parms; to finish processing the resultant expression, use
13976 tsubst_copy_and_build or tsubst_expr. */
13977
13978 static tree
13979 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13980 {
13981 enum tree_code code;
13982 tree r;
13983
13984 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
13985 return t;
13986
13987 code = TREE_CODE (t);
13988
13989 switch (code)
13990 {
13991 case PARM_DECL:
13992 r = retrieve_local_specialization (t);
13993
13994 if (r == NULL_TREE)
13995 {
13996 /* We get here for a use of 'this' in an NSDMI as part of a
13997 constructor call or as part of an aggregate initialization. */
13998 if (DECL_NAME (t) == this_identifier
13999 && ((current_function_decl
14000 && DECL_CONSTRUCTOR_P (current_function_decl))
14001 || (current_class_ref
14002 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14003 return current_class_ptr;
14004
14005 /* This can happen for a parameter name used later in a function
14006 declaration (such as in a late-specified return type). Just
14007 make a dummy decl, since it's only used for its type. */
14008 gcc_assert (cp_unevaluated_operand != 0);
14009 r = tsubst_decl (t, args, complain);
14010 /* Give it the template pattern as its context; its true context
14011 hasn't been instantiated yet and this is good enough for
14012 mangling. */
14013 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14014 }
14015
14016 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14017 r = ARGUMENT_PACK_SELECT_ARG (r);
14018 if (!mark_used (r, complain) && !(complain & tf_error))
14019 return error_mark_node;
14020 return r;
14021
14022 case CONST_DECL:
14023 {
14024 tree enum_type;
14025 tree v;
14026
14027 if (DECL_TEMPLATE_PARM_P (t))
14028 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14029 /* There is no need to substitute into namespace-scope
14030 enumerators. */
14031 if (DECL_NAMESPACE_SCOPE_P (t))
14032 return t;
14033 /* If ARGS is NULL, then T is known to be non-dependent. */
14034 if (args == NULL_TREE)
14035 return scalar_constant_value (t);
14036
14037 /* Unfortunately, we cannot just call lookup_name here.
14038 Consider:
14039
14040 template <int I> int f() {
14041 enum E { a = I };
14042 struct S { void g() { E e = a; } };
14043 };
14044
14045 When we instantiate f<7>::S::g(), say, lookup_name is not
14046 clever enough to find f<7>::a. */
14047 enum_type
14048 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14049 /*entering_scope=*/0);
14050
14051 for (v = TYPE_VALUES (enum_type);
14052 v != NULL_TREE;
14053 v = TREE_CHAIN (v))
14054 if (TREE_PURPOSE (v) == DECL_NAME (t))
14055 return TREE_VALUE (v);
14056
14057 /* We didn't find the name. That should never happen; if
14058 name-lookup found it during preliminary parsing, we
14059 should find it again here during instantiation. */
14060 gcc_unreachable ();
14061 }
14062 return t;
14063
14064 case FIELD_DECL:
14065 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14066 {
14067 /* Check for a local specialization set up by
14068 tsubst_pack_expansion. */
14069 if (tree r = retrieve_local_specialization (t))
14070 {
14071 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14072 r = ARGUMENT_PACK_SELECT_ARG (r);
14073 return r;
14074 }
14075
14076 /* When retrieving a capture pack from a generic lambda, remove the
14077 lambda call op's own template argument list from ARGS. Only the
14078 template arguments active for the closure type should be used to
14079 retrieve the pack specialization. */
14080 if (LAMBDA_FUNCTION_P (current_function_decl)
14081 && (template_class_depth (DECL_CONTEXT (t))
14082 != TMPL_ARGS_DEPTH (args)))
14083 args = strip_innermost_template_args (args, 1);
14084
14085 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14086 tsubst_decl put in the hash table. */
14087 return retrieve_specialization (t, args, 0);
14088 }
14089
14090 if (DECL_CONTEXT (t))
14091 {
14092 tree ctx;
14093
14094 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14095 /*entering_scope=*/1);
14096 if (ctx != DECL_CONTEXT (t))
14097 {
14098 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14099 if (!r)
14100 {
14101 if (complain & tf_error)
14102 error ("using invalid field %qD", t);
14103 return error_mark_node;
14104 }
14105 return r;
14106 }
14107 }
14108
14109 return t;
14110
14111 case VAR_DECL:
14112 case FUNCTION_DECL:
14113 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14114 r = tsubst (t, args, complain, in_decl);
14115 else if (local_variable_p (t)
14116 && uses_template_parms (DECL_CONTEXT (t)))
14117 {
14118 r = retrieve_local_specialization (t);
14119 if (r == NULL_TREE)
14120 {
14121 /* First try name lookup to find the instantiation. */
14122 r = lookup_name (DECL_NAME (t));
14123 if (r)
14124 {
14125 /* Make sure that the one we found is the one we want. */
14126 tree ctx = DECL_CONTEXT (t);
14127 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14128 ctx = tsubst (ctx, args, complain, in_decl);
14129 if (ctx != DECL_CONTEXT (r))
14130 r = NULL_TREE;
14131 }
14132
14133 if (r)
14134 /* OK */;
14135 else
14136 {
14137 /* This can happen for a variable used in a
14138 late-specified return type of a local lambda, or for a
14139 local static or constant. Building a new VAR_DECL
14140 should be OK in all those cases. */
14141 r = tsubst_decl (t, args, complain);
14142 if (decl_maybe_constant_var_p (r))
14143 {
14144 /* We can't call cp_finish_decl, so handle the
14145 initializer by hand. */
14146 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14147 complain, in_decl);
14148 if (!processing_template_decl)
14149 init = maybe_constant_init (init);
14150 if (processing_template_decl
14151 ? potential_constant_expression (init)
14152 : reduced_constant_expression_p (init))
14153 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14154 = TREE_CONSTANT (r) = true;
14155 DECL_INITIAL (r) = init;
14156 }
14157 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14158 || decl_constant_var_p (r)
14159 || errorcount || sorrycount);
14160 if (!processing_template_decl
14161 && !TREE_STATIC (r))
14162 r = process_outer_var_ref (r, complain);
14163 }
14164 /* Remember this for subsequent uses. */
14165 if (local_specializations)
14166 register_local_specialization (r, t);
14167 }
14168 }
14169 else
14170 r = t;
14171 if (!mark_used (r, complain) && !(complain & tf_error))
14172 return error_mark_node;
14173 return r;
14174
14175 case NAMESPACE_DECL:
14176 return t;
14177
14178 case OVERLOAD:
14179 /* An OVERLOAD will always be a non-dependent overload set; an
14180 overload set from function scope will just be represented with an
14181 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14182 gcc_assert (!uses_template_parms (t));
14183 return t;
14184
14185 case BASELINK:
14186 return tsubst_baselink (t, current_nonlambda_class_type (),
14187 args, complain, in_decl);
14188
14189 case TEMPLATE_DECL:
14190 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14191 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14192 args, complain, in_decl);
14193 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14194 return tsubst (t, args, complain, in_decl);
14195 else if (DECL_CLASS_SCOPE_P (t)
14196 && uses_template_parms (DECL_CONTEXT (t)))
14197 {
14198 /* Template template argument like the following example need
14199 special treatment:
14200
14201 template <template <class> class TT> struct C {};
14202 template <class T> struct D {
14203 template <class U> struct E {};
14204 C<E> c; // #1
14205 };
14206 D<int> d; // #2
14207
14208 We are processing the template argument `E' in #1 for
14209 the template instantiation #2. Originally, `E' is a
14210 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14211 have to substitute this with one having context `D<int>'. */
14212
14213 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14214 return lookup_field (context, DECL_NAME(t), 0, false);
14215 }
14216 else
14217 /* Ordinary template template argument. */
14218 return t;
14219
14220 case CAST_EXPR:
14221 case REINTERPRET_CAST_EXPR:
14222 case CONST_CAST_EXPR:
14223 case STATIC_CAST_EXPR:
14224 case DYNAMIC_CAST_EXPR:
14225 case IMPLICIT_CONV_EXPR:
14226 case CONVERT_EXPR:
14227 case NOP_EXPR:
14228 {
14229 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14230 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14231 return build1 (code, type, op0);
14232 }
14233
14234 case SIZEOF_EXPR:
14235 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14236 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14237 {
14238 tree expanded, op = TREE_OPERAND (t, 0);
14239 int len = 0;
14240
14241 if (SIZEOF_EXPR_TYPE_P (t))
14242 op = TREE_TYPE (op);
14243
14244 ++cp_unevaluated_operand;
14245 ++c_inhibit_evaluation_warnings;
14246 /* We only want to compute the number of arguments. */
14247 if (PACK_EXPANSION_P (op))
14248 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14249 else
14250 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14251 args, complain, in_decl);
14252 --cp_unevaluated_operand;
14253 --c_inhibit_evaluation_warnings;
14254
14255 if (TREE_CODE (expanded) == TREE_VEC)
14256 {
14257 len = TREE_VEC_LENGTH (expanded);
14258 /* Set TREE_USED for the benefit of -Wunused. */
14259 for (int i = 0; i < len; i++)
14260 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14261 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14262 }
14263
14264 if (expanded == error_mark_node)
14265 return error_mark_node;
14266 else if (PACK_EXPANSION_P (expanded)
14267 || (TREE_CODE (expanded) == TREE_VEC
14268 && pack_expansion_args_count (expanded)))
14269
14270 {
14271 if (PACK_EXPANSION_P (expanded))
14272 /* OK. */;
14273 else if (TREE_VEC_LENGTH (expanded) == 1)
14274 expanded = TREE_VEC_ELT (expanded, 0);
14275 else
14276 expanded = make_argument_pack (expanded);
14277
14278 if (TYPE_P (expanded))
14279 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14280 complain & tf_error);
14281 else
14282 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14283 complain & tf_error);
14284 }
14285 else
14286 return build_int_cst (size_type_node, len);
14287 }
14288 if (SIZEOF_EXPR_TYPE_P (t))
14289 {
14290 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14291 args, complain, in_decl);
14292 r = build1 (NOP_EXPR, r, error_mark_node);
14293 r = build1 (SIZEOF_EXPR,
14294 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14295 SIZEOF_EXPR_TYPE_P (r) = 1;
14296 return r;
14297 }
14298 /* Fall through */
14299
14300 case INDIRECT_REF:
14301 case NEGATE_EXPR:
14302 case TRUTH_NOT_EXPR:
14303 case BIT_NOT_EXPR:
14304 case ADDR_EXPR:
14305 case UNARY_PLUS_EXPR: /* Unary + */
14306 case ALIGNOF_EXPR:
14307 case AT_ENCODE_EXPR:
14308 case ARROW_EXPR:
14309 case THROW_EXPR:
14310 case TYPEID_EXPR:
14311 case REALPART_EXPR:
14312 case IMAGPART_EXPR:
14313 case PAREN_EXPR:
14314 {
14315 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14316 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14317 return build1 (code, type, op0);
14318 }
14319
14320 case COMPONENT_REF:
14321 {
14322 tree object;
14323 tree name;
14324
14325 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14326 name = TREE_OPERAND (t, 1);
14327 if (TREE_CODE (name) == BIT_NOT_EXPR)
14328 {
14329 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14330 complain, in_decl);
14331 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14332 }
14333 else if (TREE_CODE (name) == SCOPE_REF
14334 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14335 {
14336 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14337 complain, in_decl);
14338 name = TREE_OPERAND (name, 1);
14339 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14340 complain, in_decl);
14341 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14342 name = build_qualified_name (/*type=*/NULL_TREE,
14343 base, name,
14344 /*template_p=*/false);
14345 }
14346 else if (BASELINK_P (name))
14347 name = tsubst_baselink (name,
14348 non_reference (TREE_TYPE (object)),
14349 args, complain,
14350 in_decl);
14351 else
14352 name = tsubst_copy (name, args, complain, in_decl);
14353 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14354 }
14355
14356 case PLUS_EXPR:
14357 case MINUS_EXPR:
14358 case MULT_EXPR:
14359 case TRUNC_DIV_EXPR:
14360 case CEIL_DIV_EXPR:
14361 case FLOOR_DIV_EXPR:
14362 case ROUND_DIV_EXPR:
14363 case EXACT_DIV_EXPR:
14364 case BIT_AND_EXPR:
14365 case BIT_IOR_EXPR:
14366 case BIT_XOR_EXPR:
14367 case TRUNC_MOD_EXPR:
14368 case FLOOR_MOD_EXPR:
14369 case TRUTH_ANDIF_EXPR:
14370 case TRUTH_ORIF_EXPR:
14371 case TRUTH_AND_EXPR:
14372 case TRUTH_OR_EXPR:
14373 case RSHIFT_EXPR:
14374 case LSHIFT_EXPR:
14375 case RROTATE_EXPR:
14376 case LROTATE_EXPR:
14377 case EQ_EXPR:
14378 case NE_EXPR:
14379 case MAX_EXPR:
14380 case MIN_EXPR:
14381 case LE_EXPR:
14382 case GE_EXPR:
14383 case LT_EXPR:
14384 case GT_EXPR:
14385 case COMPOUND_EXPR:
14386 case DOTSTAR_EXPR:
14387 case MEMBER_REF:
14388 case PREDECREMENT_EXPR:
14389 case PREINCREMENT_EXPR:
14390 case POSTDECREMENT_EXPR:
14391 case POSTINCREMENT_EXPR:
14392 {
14393 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14394 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14395 return build_nt (code, op0, op1);
14396 }
14397
14398 case SCOPE_REF:
14399 {
14400 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14401 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14402 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14403 QUALIFIED_NAME_IS_TEMPLATE (t));
14404 }
14405
14406 case ARRAY_REF:
14407 {
14408 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14409 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14410 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14411 }
14412
14413 case CALL_EXPR:
14414 {
14415 int n = VL_EXP_OPERAND_LENGTH (t);
14416 tree result = build_vl_exp (CALL_EXPR, n);
14417 int i;
14418 for (i = 0; i < n; i++)
14419 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14420 complain, in_decl);
14421 return result;
14422 }
14423
14424 case COND_EXPR:
14425 case MODOP_EXPR:
14426 case PSEUDO_DTOR_EXPR:
14427 case VEC_PERM_EXPR:
14428 {
14429 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14430 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14431 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14432 r = build_nt (code, op0, op1, op2);
14433 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14434 return r;
14435 }
14436
14437 case NEW_EXPR:
14438 {
14439 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14440 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14441 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14442 r = build_nt (code, op0, op1, op2);
14443 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14444 return r;
14445 }
14446
14447 case DELETE_EXPR:
14448 {
14449 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14450 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14451 r = build_nt (code, op0, op1);
14452 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14453 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14454 return r;
14455 }
14456
14457 case TEMPLATE_ID_EXPR:
14458 {
14459 /* Substituted template arguments */
14460 tree fn = TREE_OPERAND (t, 0);
14461 tree targs = TREE_OPERAND (t, 1);
14462
14463 fn = tsubst_copy (fn, args, complain, in_decl);
14464 if (targs)
14465 targs = tsubst_template_args (targs, args, complain, in_decl);
14466
14467 return lookup_template_function (fn, targs);
14468 }
14469
14470 case TREE_LIST:
14471 {
14472 tree purpose, value, chain;
14473
14474 if (t == void_list_node)
14475 return t;
14476
14477 purpose = TREE_PURPOSE (t);
14478 if (purpose)
14479 purpose = tsubst_copy (purpose, args, complain, in_decl);
14480 value = TREE_VALUE (t);
14481 if (value)
14482 value = tsubst_copy (value, args, complain, in_decl);
14483 chain = TREE_CHAIN (t);
14484 if (chain && chain != void_type_node)
14485 chain = tsubst_copy (chain, args, complain, in_decl);
14486 if (purpose == TREE_PURPOSE (t)
14487 && value == TREE_VALUE (t)
14488 && chain == TREE_CHAIN (t))
14489 return t;
14490 return tree_cons (purpose, value, chain);
14491 }
14492
14493 case RECORD_TYPE:
14494 case UNION_TYPE:
14495 case ENUMERAL_TYPE:
14496 case INTEGER_TYPE:
14497 case TEMPLATE_TYPE_PARM:
14498 case TEMPLATE_TEMPLATE_PARM:
14499 case BOUND_TEMPLATE_TEMPLATE_PARM:
14500 case TEMPLATE_PARM_INDEX:
14501 case POINTER_TYPE:
14502 case REFERENCE_TYPE:
14503 case OFFSET_TYPE:
14504 case FUNCTION_TYPE:
14505 case METHOD_TYPE:
14506 case ARRAY_TYPE:
14507 case TYPENAME_TYPE:
14508 case UNBOUND_CLASS_TEMPLATE:
14509 case TYPEOF_TYPE:
14510 case DECLTYPE_TYPE:
14511 case TYPE_DECL:
14512 return tsubst (t, args, complain, in_decl);
14513
14514 case USING_DECL:
14515 t = DECL_NAME (t);
14516 /* Fall through. */
14517 case IDENTIFIER_NODE:
14518 if (IDENTIFIER_TYPENAME_P (t))
14519 {
14520 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14521 return mangle_conv_op_name_for_type (new_type);
14522 }
14523 else
14524 return t;
14525
14526 case CONSTRUCTOR:
14527 /* This is handled by tsubst_copy_and_build. */
14528 gcc_unreachable ();
14529
14530 case VA_ARG_EXPR:
14531 {
14532 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14533 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14534 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14535 }
14536
14537 case CLEANUP_POINT_EXPR:
14538 /* We shouldn't have built any of these during initial template
14539 generation. Instead, they should be built during instantiation
14540 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14541 gcc_unreachable ();
14542
14543 case OFFSET_REF:
14544 {
14545 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14546 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14547 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14548 r = build2 (code, type, op0, op1);
14549 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14550 if (!mark_used (TREE_OPERAND (r, 1), complain)
14551 && !(complain & tf_error))
14552 return error_mark_node;
14553 return r;
14554 }
14555
14556 case EXPR_PACK_EXPANSION:
14557 error ("invalid use of pack expansion expression");
14558 return error_mark_node;
14559
14560 case NONTYPE_ARGUMENT_PACK:
14561 error ("use %<...%> to expand argument pack");
14562 return error_mark_node;
14563
14564 case VOID_CST:
14565 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14566 return t;
14567
14568 case INTEGER_CST:
14569 case REAL_CST:
14570 case STRING_CST:
14571 case COMPLEX_CST:
14572 {
14573 /* Instantiate any typedefs in the type. */
14574 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14575 r = fold_convert (type, t);
14576 gcc_assert (TREE_CODE (r) == code);
14577 return r;
14578 }
14579
14580 case PTRMEM_CST:
14581 /* These can sometimes show up in a partial instantiation, but never
14582 involve template parms. */
14583 gcc_assert (!uses_template_parms (t));
14584 return t;
14585
14586 case UNARY_LEFT_FOLD_EXPR:
14587 return tsubst_unary_left_fold (t, args, complain, in_decl);
14588 case UNARY_RIGHT_FOLD_EXPR:
14589 return tsubst_unary_right_fold (t, args, complain, in_decl);
14590 case BINARY_LEFT_FOLD_EXPR:
14591 return tsubst_binary_left_fold (t, args, complain, in_decl);
14592 case BINARY_RIGHT_FOLD_EXPR:
14593 return tsubst_binary_right_fold (t, args, complain, in_decl);
14594
14595 default:
14596 /* We shouldn't get here, but keep going if !flag_checking. */
14597 if (flag_checking)
14598 gcc_unreachable ();
14599 return t;
14600 }
14601 }
14602
14603 /* Helper function for tsubst_omp_clauses, used for instantiation of
14604 OMP_CLAUSE_DECL of clauses. */
14605
14606 static tree
14607 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14608 tree in_decl)
14609 {
14610 if (decl == NULL_TREE)
14611 return NULL_TREE;
14612
14613 /* Handle an OpenMP array section represented as a TREE_LIST (or
14614 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14615 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14616 TREE_LIST. We can handle it exactly the same as an array section
14617 (purpose, value, and a chain), even though the nomenclature
14618 (low_bound, length, etc) is different. */
14619 if (TREE_CODE (decl) == TREE_LIST)
14620 {
14621 tree low_bound
14622 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14623 /*integral_constant_expression_p=*/false);
14624 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14625 /*integral_constant_expression_p=*/false);
14626 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14627 in_decl);
14628 if (TREE_PURPOSE (decl) == low_bound
14629 && TREE_VALUE (decl) == length
14630 && TREE_CHAIN (decl) == chain)
14631 return decl;
14632 tree ret = tree_cons (low_bound, length, chain);
14633 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
14634 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
14635 return ret;
14636 }
14637 tree ret = tsubst_expr (decl, args, complain, in_decl,
14638 /*integral_constant_expression_p=*/false);
14639 /* Undo convert_from_reference tsubst_expr could have called. */
14640 if (decl
14641 && REFERENCE_REF_P (ret)
14642 && !REFERENCE_REF_P (decl))
14643 ret = TREE_OPERAND (ret, 0);
14644 return ret;
14645 }
14646
14647 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14648
14649 static tree
14650 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
14651 tree args, tsubst_flags_t complain, tree in_decl)
14652 {
14653 tree new_clauses = NULL_TREE, nc, oc;
14654 tree linear_no_step = NULL_TREE;
14655
14656 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14657 {
14658 nc = copy_node (oc);
14659 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14660 new_clauses = nc;
14661
14662 switch (OMP_CLAUSE_CODE (nc))
14663 {
14664 case OMP_CLAUSE_LASTPRIVATE:
14665 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14666 {
14667 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14668 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14669 in_decl, /*integral_constant_expression_p=*/false);
14670 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14671 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14672 }
14673 /* FALLTHRU */
14674 case OMP_CLAUSE_PRIVATE:
14675 case OMP_CLAUSE_SHARED:
14676 case OMP_CLAUSE_FIRSTPRIVATE:
14677 case OMP_CLAUSE_COPYIN:
14678 case OMP_CLAUSE_COPYPRIVATE:
14679 case OMP_CLAUSE_UNIFORM:
14680 case OMP_CLAUSE_DEPEND:
14681 case OMP_CLAUSE_FROM:
14682 case OMP_CLAUSE_TO:
14683 case OMP_CLAUSE_MAP:
14684 case OMP_CLAUSE_USE_DEVICE_PTR:
14685 case OMP_CLAUSE_IS_DEVICE_PTR:
14686 OMP_CLAUSE_DECL (nc)
14687 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14688 in_decl);
14689 break;
14690 case OMP_CLAUSE_IF:
14691 case OMP_CLAUSE_NUM_THREADS:
14692 case OMP_CLAUSE_SCHEDULE:
14693 case OMP_CLAUSE_COLLAPSE:
14694 case OMP_CLAUSE_FINAL:
14695 case OMP_CLAUSE_DEVICE:
14696 case OMP_CLAUSE_DIST_SCHEDULE:
14697 case OMP_CLAUSE_NUM_TEAMS:
14698 case OMP_CLAUSE_THREAD_LIMIT:
14699 case OMP_CLAUSE_SAFELEN:
14700 case OMP_CLAUSE_SIMDLEN:
14701 case OMP_CLAUSE_NUM_TASKS:
14702 case OMP_CLAUSE_GRAINSIZE:
14703 case OMP_CLAUSE_PRIORITY:
14704 case OMP_CLAUSE_ORDERED:
14705 case OMP_CLAUSE_HINT:
14706 case OMP_CLAUSE_NUM_GANGS:
14707 case OMP_CLAUSE_NUM_WORKERS:
14708 case OMP_CLAUSE_VECTOR_LENGTH:
14709 case OMP_CLAUSE_WORKER:
14710 case OMP_CLAUSE_VECTOR:
14711 case OMP_CLAUSE_ASYNC:
14712 case OMP_CLAUSE_WAIT:
14713 OMP_CLAUSE_OPERAND (nc, 0)
14714 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14715 in_decl, /*integral_constant_expression_p=*/false);
14716 break;
14717 case OMP_CLAUSE_REDUCTION:
14718 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14719 {
14720 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14721 if (TREE_CODE (placeholder) == SCOPE_REF)
14722 {
14723 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14724 complain, in_decl);
14725 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14726 = build_qualified_name (NULL_TREE, scope,
14727 TREE_OPERAND (placeholder, 1),
14728 false);
14729 }
14730 else
14731 gcc_assert (identifier_p (placeholder));
14732 }
14733 OMP_CLAUSE_DECL (nc)
14734 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14735 in_decl);
14736 break;
14737 case OMP_CLAUSE_GANG:
14738 case OMP_CLAUSE_ALIGNED:
14739 OMP_CLAUSE_DECL (nc)
14740 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14741 in_decl);
14742 OMP_CLAUSE_OPERAND (nc, 1)
14743 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14744 in_decl, /*integral_constant_expression_p=*/false);
14745 break;
14746 case OMP_CLAUSE_LINEAR:
14747 OMP_CLAUSE_DECL (nc)
14748 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14749 in_decl);
14750 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
14751 {
14752 gcc_assert (!linear_no_step);
14753 linear_no_step = nc;
14754 }
14755 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
14756 OMP_CLAUSE_LINEAR_STEP (nc)
14757 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
14758 complain, in_decl);
14759 else
14760 OMP_CLAUSE_LINEAR_STEP (nc)
14761 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
14762 in_decl,
14763 /*integral_constant_expression_p=*/false);
14764 break;
14765 case OMP_CLAUSE_NOWAIT:
14766 case OMP_CLAUSE_DEFAULT:
14767 case OMP_CLAUSE_UNTIED:
14768 case OMP_CLAUSE_MERGEABLE:
14769 case OMP_CLAUSE_INBRANCH:
14770 case OMP_CLAUSE_NOTINBRANCH:
14771 case OMP_CLAUSE_PROC_BIND:
14772 case OMP_CLAUSE_FOR:
14773 case OMP_CLAUSE_PARALLEL:
14774 case OMP_CLAUSE_SECTIONS:
14775 case OMP_CLAUSE_TASKGROUP:
14776 case OMP_CLAUSE_NOGROUP:
14777 case OMP_CLAUSE_THREADS:
14778 case OMP_CLAUSE_SIMD:
14779 case OMP_CLAUSE_DEFAULTMAP:
14780 case OMP_CLAUSE_INDEPENDENT:
14781 case OMP_CLAUSE_AUTO:
14782 case OMP_CLAUSE_SEQ:
14783 break;
14784 case OMP_CLAUSE_TILE:
14785 {
14786 tree lnc, loc;
14787 for (lnc = OMP_CLAUSE_TILE_LIST (nc),
14788 loc = OMP_CLAUSE_TILE_LIST (oc);
14789 loc;
14790 loc = TREE_CHAIN (loc), lnc = TREE_CHAIN (lnc))
14791 {
14792 TREE_VALUE (lnc) = tsubst_expr (TREE_VALUE (loc), args,
14793 complain, in_decl, false);
14794 }
14795 }
14796 break;
14797 default:
14798 gcc_unreachable ();
14799 }
14800 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
14801 switch (OMP_CLAUSE_CODE (nc))
14802 {
14803 case OMP_CLAUSE_SHARED:
14804 case OMP_CLAUSE_PRIVATE:
14805 case OMP_CLAUSE_FIRSTPRIVATE:
14806 case OMP_CLAUSE_LASTPRIVATE:
14807 case OMP_CLAUSE_COPYPRIVATE:
14808 case OMP_CLAUSE_LINEAR:
14809 case OMP_CLAUSE_REDUCTION:
14810 case OMP_CLAUSE_USE_DEVICE_PTR:
14811 case OMP_CLAUSE_IS_DEVICE_PTR:
14812 /* tsubst_expr on SCOPE_REF results in returning
14813 finish_non_static_data_member result. Undo that here. */
14814 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
14815 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
14816 == IDENTIFIER_NODE))
14817 {
14818 tree t = OMP_CLAUSE_DECL (nc);
14819 tree v = t;
14820 while (v)
14821 switch (TREE_CODE (v))
14822 {
14823 case COMPONENT_REF:
14824 case MEM_REF:
14825 case INDIRECT_REF:
14826 CASE_CONVERT:
14827 case POINTER_PLUS_EXPR:
14828 v = TREE_OPERAND (v, 0);
14829 continue;
14830 case PARM_DECL:
14831 if (DECL_CONTEXT (v) == current_function_decl
14832 && DECL_ARTIFICIAL (v)
14833 && DECL_NAME (v) == this_identifier)
14834 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
14835 /* FALLTHRU */
14836 default:
14837 v = NULL_TREE;
14838 break;
14839 }
14840 }
14841 else if (VAR_P (OMP_CLAUSE_DECL (oc))
14842 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
14843 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
14844 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
14845 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
14846 {
14847 tree decl = OMP_CLAUSE_DECL (nc);
14848 if (VAR_P (decl))
14849 {
14850 if (!DECL_LANG_SPECIFIC (decl))
14851 retrofit_lang_decl (decl);
14852 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
14853 }
14854 }
14855 break;
14856 default:
14857 break;
14858 }
14859 }
14860
14861 new_clauses = nreverse (new_clauses);
14862 if (ort != C_ORT_OMP_DECLARE_SIMD)
14863 {
14864 new_clauses = finish_omp_clauses (new_clauses, ort);
14865 if (linear_no_step)
14866 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
14867 if (nc == linear_no_step)
14868 {
14869 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
14870 break;
14871 }
14872 }
14873 return new_clauses;
14874 }
14875
14876 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14877
14878 static tree
14879 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14880 tree in_decl)
14881 {
14882 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14883
14884 tree purpose, value, chain;
14885
14886 if (t == NULL)
14887 return t;
14888
14889 if (TREE_CODE (t) != TREE_LIST)
14890 return tsubst_copy_and_build (t, args, complain, in_decl,
14891 /*function_p=*/false,
14892 /*integral_constant_expression_p=*/false);
14893
14894 if (t == void_list_node)
14895 return t;
14896
14897 purpose = TREE_PURPOSE (t);
14898 if (purpose)
14899 purpose = RECUR (purpose);
14900 value = TREE_VALUE (t);
14901 if (value)
14902 {
14903 if (TREE_CODE (value) != LABEL_DECL)
14904 value = RECUR (value);
14905 else
14906 {
14907 value = lookup_label (DECL_NAME (value));
14908 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14909 TREE_USED (value) = 1;
14910 }
14911 }
14912 chain = TREE_CHAIN (t);
14913 if (chain && chain != void_type_node)
14914 chain = RECUR (chain);
14915 return tree_cons (purpose, value, chain);
14916 #undef RECUR
14917 }
14918
14919 /* Used to temporarily communicate the list of #pragma omp parallel
14920 clauses to #pragma omp for instantiation if they are combined
14921 together. */
14922
14923 static tree *omp_parallel_combined_clauses;
14924
14925 /* Substitute one OMP_FOR iterator. */
14926
14927 static void
14928 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
14929 tree initv, tree condv, tree incrv, tree *clauses,
14930 tree args, tsubst_flags_t complain, tree in_decl,
14931 bool integral_constant_expression_p)
14932 {
14933 #define RECUR(NODE) \
14934 tsubst_expr ((NODE), args, complain, in_decl, \
14935 integral_constant_expression_p)
14936 tree decl, init, cond, incr;
14937
14938 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
14939 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
14940
14941 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
14942 {
14943 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
14944 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
14945 }
14946
14947 decl = TREE_OPERAND (init, 0);
14948 init = TREE_OPERAND (init, 1);
14949 tree decl_expr = NULL_TREE;
14950 if (init && TREE_CODE (init) == DECL_EXPR)
14951 {
14952 /* We need to jump through some hoops to handle declarations in the
14953 for-init-statement, since we might need to handle auto deduction,
14954 but we need to keep control of initialization. */
14955 decl_expr = init;
14956 init = DECL_INITIAL (DECL_EXPR_DECL (init));
14957 decl = tsubst_decl (decl, args, complain);
14958 }
14959 else
14960 {
14961 if (TREE_CODE (decl) == SCOPE_REF)
14962 {
14963 decl = RECUR (decl);
14964 if (TREE_CODE (decl) == COMPONENT_REF)
14965 {
14966 tree v = decl;
14967 while (v)
14968 switch (TREE_CODE (v))
14969 {
14970 case COMPONENT_REF:
14971 case MEM_REF:
14972 case INDIRECT_REF:
14973 CASE_CONVERT:
14974 case POINTER_PLUS_EXPR:
14975 v = TREE_OPERAND (v, 0);
14976 continue;
14977 case PARM_DECL:
14978 if (DECL_CONTEXT (v) == current_function_decl
14979 && DECL_ARTIFICIAL (v)
14980 && DECL_NAME (v) == this_identifier)
14981 {
14982 decl = TREE_OPERAND (decl, 1);
14983 decl = omp_privatize_field (decl, false);
14984 }
14985 /* FALLTHRU */
14986 default:
14987 v = NULL_TREE;
14988 break;
14989 }
14990 }
14991 }
14992 else
14993 decl = RECUR (decl);
14994 }
14995 init = RECUR (init);
14996
14997 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14998 if (auto_node && init)
14999 TREE_TYPE (decl)
15000 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15001
15002 gcc_assert (!type_dependent_expression_p (decl));
15003
15004 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15005 {
15006 if (decl_expr)
15007 {
15008 /* Declare the variable, but don't let that initialize it. */
15009 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15010 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15011 RECUR (decl_expr);
15012 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15013 }
15014
15015 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15016 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15017 if (TREE_CODE (incr) == MODIFY_EXPR)
15018 {
15019 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15020 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15021 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15022 NOP_EXPR, rhs, complain);
15023 }
15024 else
15025 incr = RECUR (incr);
15026 TREE_VEC_ELT (declv, i) = decl;
15027 TREE_VEC_ELT (initv, i) = init;
15028 TREE_VEC_ELT (condv, i) = cond;
15029 TREE_VEC_ELT (incrv, i) = incr;
15030 return;
15031 }
15032
15033 if (decl_expr)
15034 {
15035 /* Declare and initialize the variable. */
15036 RECUR (decl_expr);
15037 init = NULL_TREE;
15038 }
15039 else if (init)
15040 {
15041 tree *pc;
15042 int j;
15043 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15044 {
15045 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15046 {
15047 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15048 && OMP_CLAUSE_DECL (*pc) == decl)
15049 break;
15050 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15051 && OMP_CLAUSE_DECL (*pc) == decl)
15052 {
15053 if (j)
15054 break;
15055 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15056 tree c = *pc;
15057 *pc = OMP_CLAUSE_CHAIN (c);
15058 OMP_CLAUSE_CHAIN (c) = *clauses;
15059 *clauses = c;
15060 }
15061 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15062 && OMP_CLAUSE_DECL (*pc) == decl)
15063 {
15064 error ("iteration variable %qD should not be firstprivate",
15065 decl);
15066 *pc = OMP_CLAUSE_CHAIN (*pc);
15067 }
15068 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15069 && OMP_CLAUSE_DECL (*pc) == decl)
15070 {
15071 error ("iteration variable %qD should not be reduction",
15072 decl);
15073 *pc = OMP_CLAUSE_CHAIN (*pc);
15074 }
15075 else
15076 pc = &OMP_CLAUSE_CHAIN (*pc);
15077 }
15078 if (*pc)
15079 break;
15080 }
15081 if (*pc == NULL_TREE)
15082 {
15083 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15084 OMP_CLAUSE_DECL (c) = decl;
15085 c = finish_omp_clauses (c, C_ORT_OMP);
15086 if (c)
15087 {
15088 OMP_CLAUSE_CHAIN (c) = *clauses;
15089 *clauses = c;
15090 }
15091 }
15092 }
15093 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15094 if (COMPARISON_CLASS_P (cond))
15095 {
15096 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15097 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15098 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15099 }
15100 else
15101 cond = RECUR (cond);
15102 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15103 switch (TREE_CODE (incr))
15104 {
15105 case PREINCREMENT_EXPR:
15106 case PREDECREMENT_EXPR:
15107 case POSTINCREMENT_EXPR:
15108 case POSTDECREMENT_EXPR:
15109 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15110 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15111 break;
15112 case MODIFY_EXPR:
15113 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15114 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15115 {
15116 tree rhs = TREE_OPERAND (incr, 1);
15117 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15118 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15119 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15120 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15121 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15122 rhs0, rhs1));
15123 }
15124 else
15125 incr = RECUR (incr);
15126 break;
15127 case MODOP_EXPR:
15128 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15129 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15130 {
15131 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15132 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15133 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15134 TREE_TYPE (decl), lhs,
15135 RECUR (TREE_OPERAND (incr, 2))));
15136 }
15137 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15138 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15139 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15140 {
15141 tree rhs = TREE_OPERAND (incr, 2);
15142 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15143 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15144 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15145 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15146 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15147 rhs0, rhs1));
15148 }
15149 else
15150 incr = RECUR (incr);
15151 break;
15152 default:
15153 incr = RECUR (incr);
15154 break;
15155 }
15156
15157 TREE_VEC_ELT (declv, i) = decl;
15158 TREE_VEC_ELT (initv, i) = init;
15159 TREE_VEC_ELT (condv, i) = cond;
15160 TREE_VEC_ELT (incrv, i) = incr;
15161 #undef RECUR
15162 }
15163
15164 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15165 of OMP_TARGET's body. */
15166
15167 static tree
15168 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15169 {
15170 *walk_subtrees = 0;
15171 switch (TREE_CODE (*tp))
15172 {
15173 case OMP_TEAMS:
15174 return *tp;
15175 case BIND_EXPR:
15176 case STATEMENT_LIST:
15177 *walk_subtrees = 1;
15178 break;
15179 default:
15180 break;
15181 }
15182 return NULL_TREE;
15183 }
15184
15185 /* Like tsubst_copy for expressions, etc. but also does semantic
15186 processing. */
15187
15188 tree
15189 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15190 bool integral_constant_expression_p)
15191 {
15192 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15193 #define RECUR(NODE) \
15194 tsubst_expr ((NODE), args, complain, in_decl, \
15195 integral_constant_expression_p)
15196
15197 tree stmt, tmp;
15198 tree r;
15199 location_t loc;
15200
15201 if (t == NULL_TREE || t == error_mark_node)
15202 return t;
15203
15204 loc = input_location;
15205 if (EXPR_HAS_LOCATION (t))
15206 input_location = EXPR_LOCATION (t);
15207 if (STATEMENT_CODE_P (TREE_CODE (t)))
15208 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15209
15210 switch (TREE_CODE (t))
15211 {
15212 case STATEMENT_LIST:
15213 {
15214 tree_stmt_iterator i;
15215 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15216 RECUR (tsi_stmt (i));
15217 break;
15218 }
15219
15220 case CTOR_INITIALIZER:
15221 finish_mem_initializers (tsubst_initializer_list
15222 (TREE_OPERAND (t, 0), args));
15223 break;
15224
15225 case RETURN_EXPR:
15226 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15227 break;
15228
15229 case EXPR_STMT:
15230 tmp = RECUR (EXPR_STMT_EXPR (t));
15231 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15232 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15233 else
15234 finish_expr_stmt (tmp);
15235 break;
15236
15237 case USING_STMT:
15238 do_using_directive (USING_STMT_NAMESPACE (t));
15239 break;
15240
15241 case DECL_EXPR:
15242 {
15243 tree decl, pattern_decl;
15244 tree init;
15245
15246 pattern_decl = decl = DECL_EXPR_DECL (t);
15247 if (TREE_CODE (decl) == LABEL_DECL)
15248 finish_label_decl (DECL_NAME (decl));
15249 else if (TREE_CODE (decl) == USING_DECL)
15250 {
15251 tree scope = USING_DECL_SCOPE (decl);
15252 tree name = DECL_NAME (decl);
15253
15254 scope = tsubst (scope, args, complain, in_decl);
15255 decl = lookup_qualified_name (scope, name,
15256 /*is_type_p=*/false,
15257 /*complain=*/false);
15258 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15259 qualified_name_lookup_error (scope, name, decl, input_location);
15260 else
15261 do_local_using_decl (decl, scope, name);
15262 }
15263 else if (DECL_PACK_P (decl))
15264 {
15265 /* Don't build up decls for a variadic capture proxy, we'll
15266 instantiate the elements directly as needed. */
15267 break;
15268 }
15269 else
15270 {
15271 init = DECL_INITIAL (decl);
15272 decl = tsubst (decl, args, complain, in_decl);
15273 if (decl != error_mark_node)
15274 {
15275 /* By marking the declaration as instantiated, we avoid
15276 trying to instantiate it. Since instantiate_decl can't
15277 handle local variables, and since we've already done
15278 all that needs to be done, that's the right thing to
15279 do. */
15280 if (VAR_P (decl))
15281 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15282 if (VAR_P (decl)
15283 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15284 /* Anonymous aggregates are a special case. */
15285 finish_anon_union (decl);
15286 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15287 {
15288 DECL_CONTEXT (decl) = current_function_decl;
15289 if (DECL_NAME (decl) == this_identifier)
15290 {
15291 tree lam = DECL_CONTEXT (current_function_decl);
15292 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15293 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15294 }
15295 insert_capture_proxy (decl);
15296 }
15297 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15298 /* We already did a pushtag. */;
15299 else if (TREE_CODE (decl) == FUNCTION_DECL
15300 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15301 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15302 {
15303 DECL_CONTEXT (decl) = NULL_TREE;
15304 pushdecl (decl);
15305 DECL_CONTEXT (decl) = current_function_decl;
15306 cp_check_omp_declare_reduction (decl);
15307 }
15308 else
15309 {
15310 int const_init = false;
15311 maybe_push_decl (decl);
15312 if (VAR_P (decl)
15313 && DECL_PRETTY_FUNCTION_P (decl))
15314 {
15315 /* For __PRETTY_FUNCTION__ we have to adjust the
15316 initializer. */
15317 const char *const name
15318 = cxx_printable_name (current_function_decl, 2);
15319 init = cp_fname_init (name, &TREE_TYPE (decl));
15320 }
15321 else
15322 init = tsubst_init (init, decl, args, complain, in_decl);
15323
15324 if (VAR_P (decl))
15325 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15326 (pattern_decl));
15327 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15328 }
15329 }
15330 }
15331
15332 break;
15333 }
15334
15335 case FOR_STMT:
15336 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15337 RECUR (FOR_INIT_STMT (t));
15338 finish_for_init_stmt (stmt);
15339 tmp = RECUR (FOR_COND (t));
15340 finish_for_cond (tmp, stmt, false);
15341 tmp = RECUR (FOR_EXPR (t));
15342 finish_for_expr (tmp, stmt);
15343 RECUR (FOR_BODY (t));
15344 finish_for_stmt (stmt);
15345 break;
15346
15347 case RANGE_FOR_STMT:
15348 {
15349 tree decl, expr;
15350 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15351 decl = RANGE_FOR_DECL (t);
15352 decl = tsubst (decl, args, complain, in_decl);
15353 maybe_push_decl (decl);
15354 expr = RECUR (RANGE_FOR_EXPR (t));
15355 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
15356 RECUR (RANGE_FOR_BODY (t));
15357 finish_for_stmt (stmt);
15358 }
15359 break;
15360
15361 case WHILE_STMT:
15362 stmt = begin_while_stmt ();
15363 tmp = RECUR (WHILE_COND (t));
15364 finish_while_stmt_cond (tmp, stmt, false);
15365 RECUR (WHILE_BODY (t));
15366 finish_while_stmt (stmt);
15367 break;
15368
15369 case DO_STMT:
15370 stmt = begin_do_stmt ();
15371 RECUR (DO_BODY (t));
15372 finish_do_body (stmt);
15373 tmp = RECUR (DO_COND (t));
15374 finish_do_stmt (tmp, stmt, false);
15375 break;
15376
15377 case IF_STMT:
15378 stmt = begin_if_stmt ();
15379 tmp = RECUR (IF_COND (t));
15380 finish_if_stmt_cond (tmp, stmt);
15381 RECUR (THEN_CLAUSE (t));
15382 finish_then_clause (stmt);
15383
15384 if (ELSE_CLAUSE (t))
15385 {
15386 begin_else_clause (stmt);
15387 RECUR (ELSE_CLAUSE (t));
15388 finish_else_clause (stmt);
15389 }
15390
15391 finish_if_stmt (stmt);
15392 break;
15393
15394 case BIND_EXPR:
15395 if (BIND_EXPR_BODY_BLOCK (t))
15396 stmt = begin_function_body ();
15397 else
15398 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15399 ? BCS_TRY_BLOCK : 0);
15400
15401 RECUR (BIND_EXPR_BODY (t));
15402
15403 if (BIND_EXPR_BODY_BLOCK (t))
15404 finish_function_body (stmt);
15405 else
15406 finish_compound_stmt (stmt);
15407 break;
15408
15409 case BREAK_STMT:
15410 finish_break_stmt ();
15411 break;
15412
15413 case CONTINUE_STMT:
15414 finish_continue_stmt ();
15415 break;
15416
15417 case SWITCH_STMT:
15418 stmt = begin_switch_stmt ();
15419 tmp = RECUR (SWITCH_STMT_COND (t));
15420 finish_switch_cond (tmp, stmt);
15421 RECUR (SWITCH_STMT_BODY (t));
15422 finish_switch_stmt (stmt);
15423 break;
15424
15425 case CASE_LABEL_EXPR:
15426 {
15427 tree low = RECUR (CASE_LOW (t));
15428 tree high = RECUR (CASE_HIGH (t));
15429 finish_case_label (EXPR_LOCATION (t), low, high);
15430 }
15431 break;
15432
15433 case LABEL_EXPR:
15434 {
15435 tree decl = LABEL_EXPR_LABEL (t);
15436 tree label;
15437
15438 label = finish_label_stmt (DECL_NAME (decl));
15439 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15440 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15441 }
15442 break;
15443
15444 case GOTO_EXPR:
15445 tmp = GOTO_DESTINATION (t);
15446 if (TREE_CODE (tmp) != LABEL_DECL)
15447 /* Computed goto's must be tsubst'd into. On the other hand,
15448 non-computed gotos must not be; the identifier in question
15449 will have no binding. */
15450 tmp = RECUR (tmp);
15451 else
15452 tmp = DECL_NAME (tmp);
15453 finish_goto_stmt (tmp);
15454 break;
15455
15456 case ASM_EXPR:
15457 {
15458 tree string = RECUR (ASM_STRING (t));
15459 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15460 complain, in_decl);
15461 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15462 complain, in_decl);
15463 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15464 complain, in_decl);
15465 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15466 complain, in_decl);
15467 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15468 clobbers, labels);
15469 tree asm_expr = tmp;
15470 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15471 asm_expr = TREE_OPERAND (asm_expr, 0);
15472 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15473 }
15474 break;
15475
15476 case TRY_BLOCK:
15477 if (CLEANUP_P (t))
15478 {
15479 stmt = begin_try_block ();
15480 RECUR (TRY_STMTS (t));
15481 finish_cleanup_try_block (stmt);
15482 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15483 }
15484 else
15485 {
15486 tree compound_stmt = NULL_TREE;
15487
15488 if (FN_TRY_BLOCK_P (t))
15489 stmt = begin_function_try_block (&compound_stmt);
15490 else
15491 stmt = begin_try_block ();
15492
15493 RECUR (TRY_STMTS (t));
15494
15495 if (FN_TRY_BLOCK_P (t))
15496 finish_function_try_block (stmt);
15497 else
15498 finish_try_block (stmt);
15499
15500 RECUR (TRY_HANDLERS (t));
15501 if (FN_TRY_BLOCK_P (t))
15502 finish_function_handler_sequence (stmt, compound_stmt);
15503 else
15504 finish_handler_sequence (stmt);
15505 }
15506 break;
15507
15508 case HANDLER:
15509 {
15510 tree decl = HANDLER_PARMS (t);
15511
15512 if (decl)
15513 {
15514 decl = tsubst (decl, args, complain, in_decl);
15515 /* Prevent instantiate_decl from trying to instantiate
15516 this variable. We've already done all that needs to be
15517 done. */
15518 if (decl != error_mark_node)
15519 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15520 }
15521 stmt = begin_handler ();
15522 finish_handler_parms (decl, stmt);
15523 RECUR (HANDLER_BODY (t));
15524 finish_handler (stmt);
15525 }
15526 break;
15527
15528 case TAG_DEFN:
15529 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
15530 if (CLASS_TYPE_P (tmp))
15531 {
15532 /* Local classes are not independent templates; they are
15533 instantiated along with their containing function. And this
15534 way we don't have to deal with pushing out of one local class
15535 to instantiate a member of another local class. */
15536 tree fn;
15537 /* Closures are handled by the LAMBDA_EXPR. */
15538 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
15539 complete_type (tmp);
15540 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
15541 if (!DECL_ARTIFICIAL (fn))
15542 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
15543 }
15544 break;
15545
15546 case STATIC_ASSERT:
15547 {
15548 tree condition;
15549
15550 ++c_inhibit_evaluation_warnings;
15551 condition =
15552 tsubst_expr (STATIC_ASSERT_CONDITION (t),
15553 args,
15554 complain, in_decl,
15555 /*integral_constant_expression_p=*/true);
15556 --c_inhibit_evaluation_warnings;
15557
15558 finish_static_assert (condition,
15559 STATIC_ASSERT_MESSAGE (t),
15560 STATIC_ASSERT_SOURCE_LOCATION (t),
15561 /*member_p=*/false);
15562 }
15563 break;
15564
15565 case OACC_KERNELS:
15566 case OACC_PARALLEL:
15567 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
15568 in_decl);
15569 stmt = begin_omp_parallel ();
15570 RECUR (OMP_BODY (t));
15571 finish_omp_construct (TREE_CODE (t), stmt, tmp);
15572 break;
15573
15574 case OMP_PARALLEL:
15575 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
15576 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
15577 complain, in_decl);
15578 if (OMP_PARALLEL_COMBINED (t))
15579 omp_parallel_combined_clauses = &tmp;
15580 stmt = begin_omp_parallel ();
15581 RECUR (OMP_PARALLEL_BODY (t));
15582 gcc_assert (omp_parallel_combined_clauses == NULL);
15583 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15584 = OMP_PARALLEL_COMBINED (t);
15585 pop_omp_privatization_clauses (r);
15586 break;
15587
15588 case OMP_TASK:
15589 r = push_omp_privatization_clauses (false);
15590 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
15591 complain, in_decl);
15592 stmt = begin_omp_task ();
15593 RECUR (OMP_TASK_BODY (t));
15594 finish_omp_task (tmp, stmt);
15595 pop_omp_privatization_clauses (r);
15596 break;
15597
15598 case OMP_FOR:
15599 case OMP_SIMD:
15600 case CILK_SIMD:
15601 case CILK_FOR:
15602 case OMP_DISTRIBUTE:
15603 case OMP_TASKLOOP:
15604 case OACC_LOOP:
15605 {
15606 tree clauses, body, pre_body;
15607 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15608 tree orig_declv = NULL_TREE;
15609 tree incrv = NULL_TREE;
15610 enum c_omp_region_type ort = C_ORT_OMP;
15611 int i;
15612
15613 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
15614 ort = C_ORT_CILK;
15615 else if (TREE_CODE (t) == OACC_LOOP)
15616 ort = C_ORT_ACC;
15617
15618 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
15619 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
15620 in_decl);
15621 if (OMP_FOR_INIT (t) != NULL_TREE)
15622 {
15623 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15624 if (OMP_FOR_ORIG_DECLS (t))
15625 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15626 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15627 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15628 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15629 }
15630
15631 stmt = begin_omp_structured_block ();
15632
15633 pre_body = push_stmt_list ();
15634 RECUR (OMP_FOR_PRE_BODY (t));
15635 pre_body = pop_stmt_list (pre_body);
15636
15637 if (OMP_FOR_INIT (t) != NULL_TREE)
15638 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15639 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
15640 incrv, &clauses, args, complain, in_decl,
15641 integral_constant_expression_p);
15642 omp_parallel_combined_clauses = NULL;
15643
15644 body = push_stmt_list ();
15645 RECUR (OMP_FOR_BODY (t));
15646 body = pop_stmt_list (body);
15647
15648 if (OMP_FOR_INIT (t) != NULL_TREE)
15649 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
15650 orig_declv, initv, condv, incrv, body, pre_body,
15651 NULL, clauses);
15652 else
15653 {
15654 t = make_node (TREE_CODE (t));
15655 TREE_TYPE (t) = void_type_node;
15656 OMP_FOR_BODY (t) = body;
15657 OMP_FOR_PRE_BODY (t) = pre_body;
15658 OMP_FOR_CLAUSES (t) = clauses;
15659 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15660 add_stmt (t);
15661 }
15662
15663 add_stmt (finish_omp_structured_block (stmt));
15664 pop_omp_privatization_clauses (r);
15665 }
15666 break;
15667
15668 case OMP_SECTIONS:
15669 omp_parallel_combined_clauses = NULL;
15670 /* FALLTHRU */
15671 case OMP_SINGLE:
15672 case OMP_TEAMS:
15673 case OMP_CRITICAL:
15674 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
15675 && OMP_TEAMS_COMBINED (t));
15676 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
15677 in_decl);
15678 stmt = push_stmt_list ();
15679 RECUR (OMP_BODY (t));
15680 stmt = pop_stmt_list (stmt);
15681
15682 t = copy_node (t);
15683 OMP_BODY (t) = stmt;
15684 OMP_CLAUSES (t) = tmp;
15685 add_stmt (t);
15686 pop_omp_privatization_clauses (r);
15687 break;
15688
15689 case OACC_DATA:
15690 case OMP_TARGET_DATA:
15691 case OMP_TARGET:
15692 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
15693 ? C_ORT_ACC : C_ORT_OMP, args, complain,
15694 in_decl);
15695 keep_next_level (true);
15696 stmt = begin_omp_structured_block ();
15697
15698 RECUR (OMP_BODY (t));
15699 stmt = finish_omp_structured_block (stmt);
15700
15701 t = copy_node (t);
15702 OMP_BODY (t) = stmt;
15703 OMP_CLAUSES (t) = tmp;
15704 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
15705 {
15706 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
15707 if (teams)
15708 {
15709 /* For combined target teams, ensure the num_teams and
15710 thread_limit clause expressions are evaluated on the host,
15711 before entering the target construct. */
15712 tree c;
15713 for (c = OMP_TEAMS_CLAUSES (teams);
15714 c; c = OMP_CLAUSE_CHAIN (c))
15715 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15716 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15717 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15718 {
15719 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15720 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
15721 if (expr == error_mark_node)
15722 continue;
15723 tmp = TARGET_EXPR_SLOT (expr);
15724 add_stmt (expr);
15725 OMP_CLAUSE_OPERAND (c, 0) = expr;
15726 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15727 OMP_CLAUSE_FIRSTPRIVATE);
15728 OMP_CLAUSE_DECL (tc) = tmp;
15729 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
15730 OMP_TARGET_CLAUSES (t) = tc;
15731 }
15732 }
15733 }
15734 add_stmt (t);
15735 break;
15736
15737 case OACC_DECLARE:
15738 t = copy_node (t);
15739 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
15740 complain, in_decl);
15741 OACC_DECLARE_CLAUSES (t) = tmp;
15742 add_stmt (t);
15743 break;
15744
15745 case OMP_TARGET_UPDATE:
15746 case OMP_TARGET_ENTER_DATA:
15747 case OMP_TARGET_EXIT_DATA:
15748 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
15749 complain, in_decl);
15750 t = copy_node (t);
15751 OMP_STANDALONE_CLAUSES (t) = tmp;
15752 add_stmt (t);
15753 break;
15754
15755 case OACC_ENTER_DATA:
15756 case OACC_EXIT_DATA:
15757 case OACC_UPDATE:
15758 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
15759 complain, in_decl);
15760 t = copy_node (t);
15761 OMP_STANDALONE_CLAUSES (t) = tmp;
15762 add_stmt (t);
15763 break;
15764
15765 case OMP_ORDERED:
15766 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
15767 complain, in_decl);
15768 stmt = push_stmt_list ();
15769 RECUR (OMP_BODY (t));
15770 stmt = pop_stmt_list (stmt);
15771
15772 t = copy_node (t);
15773 OMP_BODY (t) = stmt;
15774 OMP_ORDERED_CLAUSES (t) = tmp;
15775 add_stmt (t);
15776 break;
15777
15778 case OMP_SECTION:
15779 case OMP_MASTER:
15780 case OMP_TASKGROUP:
15781 stmt = push_stmt_list ();
15782 RECUR (OMP_BODY (t));
15783 stmt = pop_stmt_list (stmt);
15784
15785 t = copy_node (t);
15786 OMP_BODY (t) = stmt;
15787 add_stmt (t);
15788 break;
15789
15790 case OMP_ATOMIC:
15791 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
15792 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
15793 {
15794 tree op1 = TREE_OPERAND (t, 1);
15795 tree rhs1 = NULL_TREE;
15796 tree lhs, rhs;
15797 if (TREE_CODE (op1) == COMPOUND_EXPR)
15798 {
15799 rhs1 = RECUR (TREE_OPERAND (op1, 0));
15800 op1 = TREE_OPERAND (op1, 1);
15801 }
15802 lhs = RECUR (TREE_OPERAND (op1, 0));
15803 rhs = RECUR (TREE_OPERAND (op1, 1));
15804 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
15805 NULL_TREE, NULL_TREE, rhs1,
15806 OMP_ATOMIC_SEQ_CST (t));
15807 }
15808 else
15809 {
15810 tree op1 = TREE_OPERAND (t, 1);
15811 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
15812 tree rhs1 = NULL_TREE;
15813 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
15814 enum tree_code opcode = NOP_EXPR;
15815 if (code == OMP_ATOMIC_READ)
15816 {
15817 v = RECUR (TREE_OPERAND (op1, 0));
15818 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15819 }
15820 else if (code == OMP_ATOMIC_CAPTURE_OLD
15821 || code == OMP_ATOMIC_CAPTURE_NEW)
15822 {
15823 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
15824 v = RECUR (TREE_OPERAND (op1, 0));
15825 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15826 if (TREE_CODE (op11) == COMPOUND_EXPR)
15827 {
15828 rhs1 = RECUR (TREE_OPERAND (op11, 0));
15829 op11 = TREE_OPERAND (op11, 1);
15830 }
15831 lhs = RECUR (TREE_OPERAND (op11, 0));
15832 rhs = RECUR (TREE_OPERAND (op11, 1));
15833 opcode = TREE_CODE (op11);
15834 if (opcode == MODIFY_EXPR)
15835 opcode = NOP_EXPR;
15836 }
15837 else
15838 {
15839 code = OMP_ATOMIC;
15840 lhs = RECUR (TREE_OPERAND (op1, 0));
15841 rhs = RECUR (TREE_OPERAND (op1, 1));
15842 }
15843 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
15844 OMP_ATOMIC_SEQ_CST (t));
15845 }
15846 break;
15847
15848 case TRANSACTION_EXPR:
15849 {
15850 int flags = 0;
15851 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
15852 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
15853
15854 if (TRANSACTION_EXPR_IS_STMT (t))
15855 {
15856 tree body = TRANSACTION_EXPR_BODY (t);
15857 tree noex = NULL_TREE;
15858 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
15859 {
15860 noex = MUST_NOT_THROW_COND (body);
15861 if (noex == NULL_TREE)
15862 noex = boolean_true_node;
15863 body = TREE_OPERAND (body, 0);
15864 }
15865 stmt = begin_transaction_stmt (input_location, NULL, flags);
15866 RECUR (body);
15867 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
15868 }
15869 else
15870 {
15871 stmt = build_transaction_expr (EXPR_LOCATION (t),
15872 RECUR (TRANSACTION_EXPR_BODY (t)),
15873 flags, NULL_TREE);
15874 RETURN (stmt);
15875 }
15876 }
15877 break;
15878
15879 case MUST_NOT_THROW_EXPR:
15880 {
15881 tree op0 = RECUR (TREE_OPERAND (t, 0));
15882 tree cond = RECUR (MUST_NOT_THROW_COND (t));
15883 RETURN (build_must_not_throw_expr (op0, cond));
15884 }
15885
15886 case EXPR_PACK_EXPANSION:
15887 error ("invalid use of pack expansion expression");
15888 RETURN (error_mark_node);
15889
15890 case NONTYPE_ARGUMENT_PACK:
15891 error ("use %<...%> to expand argument pack");
15892 RETURN (error_mark_node);
15893
15894 case CILK_SPAWN_STMT:
15895 cfun->calls_cilk_spawn = 1;
15896 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
15897
15898 case CILK_SYNC_STMT:
15899 RETURN (build_cilk_sync ());
15900
15901 case COMPOUND_EXPR:
15902 tmp = RECUR (TREE_OPERAND (t, 0));
15903 if (tmp == NULL_TREE)
15904 /* If the first operand was a statement, we're done with it. */
15905 RETURN (RECUR (TREE_OPERAND (t, 1)));
15906 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
15907 RECUR (TREE_OPERAND (t, 1)),
15908 complain));
15909
15910 case ANNOTATE_EXPR:
15911 tmp = RECUR (TREE_OPERAND (t, 0));
15912 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
15913 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
15914
15915 default:
15916 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
15917
15918 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
15919 /*function_p=*/false,
15920 integral_constant_expression_p));
15921 }
15922
15923 RETURN (NULL_TREE);
15924 out:
15925 input_location = loc;
15926 return r;
15927 #undef RECUR
15928 #undef RETURN
15929 }
15930
15931 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15932 function. For description of the body see comment above
15933 cp_parser_omp_declare_reduction_exprs. */
15934
15935 static void
15936 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15937 {
15938 if (t == NULL_TREE || t == error_mark_node)
15939 return;
15940
15941 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
15942
15943 tree_stmt_iterator tsi;
15944 int i;
15945 tree stmts[7];
15946 memset (stmts, 0, sizeof stmts);
15947 for (i = 0, tsi = tsi_start (t);
15948 i < 7 && !tsi_end_p (tsi);
15949 i++, tsi_next (&tsi))
15950 stmts[i] = tsi_stmt (tsi);
15951 gcc_assert (tsi_end_p (tsi));
15952
15953 if (i >= 3)
15954 {
15955 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
15956 && TREE_CODE (stmts[1]) == DECL_EXPR);
15957 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
15958 args, complain, in_decl);
15959 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
15960 args, complain, in_decl);
15961 DECL_CONTEXT (omp_out) = current_function_decl;
15962 DECL_CONTEXT (omp_in) = current_function_decl;
15963 keep_next_level (true);
15964 tree block = begin_omp_structured_block ();
15965 tsubst_expr (stmts[2], args, complain, in_decl, false);
15966 block = finish_omp_structured_block (block);
15967 block = maybe_cleanup_point_expr_void (block);
15968 add_decl_expr (omp_out);
15969 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
15970 TREE_NO_WARNING (omp_out) = 1;
15971 add_decl_expr (omp_in);
15972 finish_expr_stmt (block);
15973 }
15974 if (i >= 6)
15975 {
15976 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
15977 && TREE_CODE (stmts[4]) == DECL_EXPR);
15978 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
15979 args, complain, in_decl);
15980 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
15981 args, complain, in_decl);
15982 DECL_CONTEXT (omp_priv) = current_function_decl;
15983 DECL_CONTEXT (omp_orig) = current_function_decl;
15984 keep_next_level (true);
15985 tree block = begin_omp_structured_block ();
15986 tsubst_expr (stmts[5], args, complain, in_decl, false);
15987 block = finish_omp_structured_block (block);
15988 block = maybe_cleanup_point_expr_void (block);
15989 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
15990 add_decl_expr (omp_priv);
15991 add_decl_expr (omp_orig);
15992 finish_expr_stmt (block);
15993 if (i == 7)
15994 add_decl_expr (omp_orig);
15995 }
15996 }
15997
15998 /* T is a postfix-expression that is not being used in a function
15999 call. Return the substituted version of T. */
16000
16001 static tree
16002 tsubst_non_call_postfix_expression (tree t, tree args,
16003 tsubst_flags_t complain,
16004 tree in_decl)
16005 {
16006 if (TREE_CODE (t) == SCOPE_REF)
16007 t = tsubst_qualified_id (t, args, complain, in_decl,
16008 /*done=*/false, /*address_p=*/false);
16009 else
16010 t = tsubst_copy_and_build (t, args, complain, in_decl,
16011 /*function_p=*/false,
16012 /*integral_constant_expression_p=*/false);
16013
16014 return t;
16015 }
16016
16017 /* Like tsubst but deals with expressions and performs semantic
16018 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16019
16020 tree
16021 tsubst_copy_and_build (tree t,
16022 tree args,
16023 tsubst_flags_t complain,
16024 tree in_decl,
16025 bool function_p,
16026 bool integral_constant_expression_p)
16027 {
16028 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16029 #define RECUR(NODE) \
16030 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16031 /*function_p=*/false, \
16032 integral_constant_expression_p)
16033
16034 tree retval, op1;
16035 location_t loc;
16036
16037 if (t == NULL_TREE || t == error_mark_node)
16038 return t;
16039
16040 loc = input_location;
16041 if (EXPR_HAS_LOCATION (t))
16042 input_location = EXPR_LOCATION (t);
16043
16044 /* N3276 decltype magic only applies to calls at the top level or on the
16045 right side of a comma. */
16046 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16047 complain &= ~tf_decltype;
16048
16049 switch (TREE_CODE (t))
16050 {
16051 case USING_DECL:
16052 t = DECL_NAME (t);
16053 /* Fall through. */
16054 case IDENTIFIER_NODE:
16055 {
16056 tree decl;
16057 cp_id_kind idk;
16058 bool non_integral_constant_expression_p;
16059 const char *error_msg;
16060
16061 if (IDENTIFIER_TYPENAME_P (t))
16062 {
16063 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16064 t = mangle_conv_op_name_for_type (new_type);
16065 }
16066
16067 /* Look up the name. */
16068 decl = lookup_name (t);
16069
16070 /* By convention, expressions use ERROR_MARK_NODE to indicate
16071 failure, not NULL_TREE. */
16072 if (decl == NULL_TREE)
16073 decl = error_mark_node;
16074
16075 decl = finish_id_expression (t, decl, NULL_TREE,
16076 &idk,
16077 integral_constant_expression_p,
16078 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16079 &non_integral_constant_expression_p,
16080 /*template_p=*/false,
16081 /*done=*/true,
16082 /*address_p=*/false,
16083 /*template_arg_p=*/false,
16084 &error_msg,
16085 input_location);
16086 if (error_msg)
16087 error (error_msg);
16088 if (!function_p && identifier_p (decl))
16089 {
16090 if (complain & tf_error)
16091 unqualified_name_lookup_error (decl);
16092 decl = error_mark_node;
16093 }
16094 RETURN (decl);
16095 }
16096
16097 case TEMPLATE_ID_EXPR:
16098 {
16099 tree object;
16100 tree templ = RECUR (TREE_OPERAND (t, 0));
16101 tree targs = TREE_OPERAND (t, 1);
16102
16103 if (targs)
16104 targs = tsubst_template_args (targs, args, complain, in_decl);
16105 if (targs == error_mark_node)
16106 return error_mark_node;
16107
16108 if (variable_template_p (templ))
16109 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16110
16111 if (TREE_CODE (templ) == COMPONENT_REF)
16112 {
16113 object = TREE_OPERAND (templ, 0);
16114 templ = TREE_OPERAND (templ, 1);
16115 }
16116 else
16117 object = NULL_TREE;
16118 templ = lookup_template_function (templ, targs);
16119
16120 if (object)
16121 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16122 object, templ, NULL_TREE));
16123 else
16124 RETURN (baselink_for_fns (templ));
16125 }
16126
16127 case INDIRECT_REF:
16128 {
16129 tree r = RECUR (TREE_OPERAND (t, 0));
16130
16131 if (REFERENCE_REF_P (t))
16132 {
16133 /* A type conversion to reference type will be enclosed in
16134 such an indirect ref, but the substitution of the cast
16135 will have also added such an indirect ref. */
16136 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16137 r = convert_from_reference (r);
16138 }
16139 else
16140 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16141 complain|decltype_flag);
16142
16143 if (TREE_CODE (r) == INDIRECT_REF)
16144 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16145
16146 RETURN (r);
16147 }
16148
16149 case NOP_EXPR:
16150 {
16151 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16152 tree op0 = RECUR (TREE_OPERAND (t, 0));
16153 RETURN (build_nop (type, op0));
16154 }
16155
16156 case IMPLICIT_CONV_EXPR:
16157 {
16158 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16159 tree expr = RECUR (TREE_OPERAND (t, 0));
16160 int flags = LOOKUP_IMPLICIT;
16161 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16162 flags = LOOKUP_NORMAL;
16163 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16164 flags));
16165 }
16166
16167 case CONVERT_EXPR:
16168 {
16169 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16170 tree op0 = RECUR (TREE_OPERAND (t, 0));
16171 RETURN (build1 (CONVERT_EXPR, type, op0));
16172 }
16173
16174 case CAST_EXPR:
16175 case REINTERPRET_CAST_EXPR:
16176 case CONST_CAST_EXPR:
16177 case DYNAMIC_CAST_EXPR:
16178 case STATIC_CAST_EXPR:
16179 {
16180 tree type;
16181 tree op, r = NULL_TREE;
16182
16183 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16184 if (integral_constant_expression_p
16185 && !cast_valid_in_integral_constant_expression_p (type))
16186 {
16187 if (complain & tf_error)
16188 error ("a cast to a type other than an integral or "
16189 "enumeration type cannot appear in a constant-expression");
16190 RETURN (error_mark_node);
16191 }
16192
16193 op = RECUR (TREE_OPERAND (t, 0));
16194
16195 warning_sentinel s(warn_useless_cast);
16196 switch (TREE_CODE (t))
16197 {
16198 case CAST_EXPR:
16199 r = build_functional_cast (type, op, complain);
16200 break;
16201 case REINTERPRET_CAST_EXPR:
16202 r = build_reinterpret_cast (type, op, complain);
16203 break;
16204 case CONST_CAST_EXPR:
16205 r = build_const_cast (type, op, complain);
16206 break;
16207 case DYNAMIC_CAST_EXPR:
16208 r = build_dynamic_cast (type, op, complain);
16209 break;
16210 case STATIC_CAST_EXPR:
16211 r = build_static_cast (type, op, complain);
16212 break;
16213 default:
16214 gcc_unreachable ();
16215 }
16216
16217 RETURN (r);
16218 }
16219
16220 case POSTDECREMENT_EXPR:
16221 case POSTINCREMENT_EXPR:
16222 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16223 args, complain, in_decl);
16224 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16225 complain|decltype_flag));
16226
16227 case PREDECREMENT_EXPR:
16228 case PREINCREMENT_EXPR:
16229 case NEGATE_EXPR:
16230 case BIT_NOT_EXPR:
16231 case ABS_EXPR:
16232 case TRUTH_NOT_EXPR:
16233 case UNARY_PLUS_EXPR: /* Unary + */
16234 case REALPART_EXPR:
16235 case IMAGPART_EXPR:
16236 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16237 RECUR (TREE_OPERAND (t, 0)),
16238 complain|decltype_flag));
16239
16240 case FIX_TRUNC_EXPR:
16241 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16242 0, complain));
16243
16244 case ADDR_EXPR:
16245 op1 = TREE_OPERAND (t, 0);
16246 if (TREE_CODE (op1) == LABEL_DECL)
16247 RETURN (finish_label_address_expr (DECL_NAME (op1),
16248 EXPR_LOCATION (op1)));
16249 if (TREE_CODE (op1) == SCOPE_REF)
16250 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16251 /*done=*/true, /*address_p=*/true);
16252 else
16253 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16254 in_decl);
16255 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16256 complain|decltype_flag));
16257
16258 case PLUS_EXPR:
16259 case MINUS_EXPR:
16260 case MULT_EXPR:
16261 case TRUNC_DIV_EXPR:
16262 case CEIL_DIV_EXPR:
16263 case FLOOR_DIV_EXPR:
16264 case ROUND_DIV_EXPR:
16265 case EXACT_DIV_EXPR:
16266 case BIT_AND_EXPR:
16267 case BIT_IOR_EXPR:
16268 case BIT_XOR_EXPR:
16269 case TRUNC_MOD_EXPR:
16270 case FLOOR_MOD_EXPR:
16271 case TRUTH_ANDIF_EXPR:
16272 case TRUTH_ORIF_EXPR:
16273 case TRUTH_AND_EXPR:
16274 case TRUTH_OR_EXPR:
16275 case RSHIFT_EXPR:
16276 case LSHIFT_EXPR:
16277 case RROTATE_EXPR:
16278 case LROTATE_EXPR:
16279 case EQ_EXPR:
16280 case NE_EXPR:
16281 case MAX_EXPR:
16282 case MIN_EXPR:
16283 case LE_EXPR:
16284 case GE_EXPR:
16285 case LT_EXPR:
16286 case GT_EXPR:
16287 case MEMBER_REF:
16288 case DOTSTAR_EXPR:
16289 {
16290 warning_sentinel s1(warn_type_limits);
16291 warning_sentinel s2(warn_div_by_zero);
16292 warning_sentinel s3(warn_logical_op);
16293 warning_sentinel s4(warn_tautological_compare);
16294 tree op0 = RECUR (TREE_OPERAND (t, 0));
16295 tree op1 = RECUR (TREE_OPERAND (t, 1));
16296 tree r = build_x_binary_op
16297 (input_location, TREE_CODE (t),
16298 op0,
16299 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16300 ? ERROR_MARK
16301 : TREE_CODE (TREE_OPERAND (t, 0))),
16302 op1,
16303 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16304 ? ERROR_MARK
16305 : TREE_CODE (TREE_OPERAND (t, 1))),
16306 /*overload=*/NULL,
16307 complain|decltype_flag);
16308 if (EXPR_P (r) && TREE_NO_WARNING (t))
16309 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16310
16311 RETURN (r);
16312 }
16313
16314 case POINTER_PLUS_EXPR:
16315 {
16316 tree op0 = RECUR (TREE_OPERAND (t, 0));
16317 tree op1 = RECUR (TREE_OPERAND (t, 1));
16318 return fold_build_pointer_plus (op0, op1);
16319 }
16320
16321 case SCOPE_REF:
16322 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16323 /*address_p=*/false));
16324 case ARRAY_REF:
16325 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16326 args, complain, in_decl);
16327 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16328 RECUR (TREE_OPERAND (t, 1)),
16329 complain|decltype_flag));
16330
16331 case ARRAY_NOTATION_REF:
16332 {
16333 tree start_index, length, stride;
16334 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16335 args, complain, in_decl);
16336 start_index = RECUR (ARRAY_NOTATION_START (t));
16337 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16338 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16339 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16340 length, stride, TREE_TYPE (op1)));
16341 }
16342 case SIZEOF_EXPR:
16343 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16344 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16345 RETURN (tsubst_copy (t, args, complain, in_decl));
16346 /* Fall through */
16347
16348 case ALIGNOF_EXPR:
16349 {
16350 tree r;
16351
16352 op1 = TREE_OPERAND (t, 0);
16353 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16354 op1 = TREE_TYPE (op1);
16355 if (!args)
16356 {
16357 /* When there are no ARGS, we are trying to evaluate a
16358 non-dependent expression from the parser. Trying to do
16359 the substitutions may not work. */
16360 if (!TYPE_P (op1))
16361 op1 = TREE_TYPE (op1);
16362 }
16363 else
16364 {
16365 ++cp_unevaluated_operand;
16366 ++c_inhibit_evaluation_warnings;
16367 if (TYPE_P (op1))
16368 op1 = tsubst (op1, args, complain, in_decl);
16369 else
16370 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16371 /*function_p=*/false,
16372 /*integral_constant_expression_p=*/
16373 false);
16374 --cp_unevaluated_operand;
16375 --c_inhibit_evaluation_warnings;
16376 }
16377 if (TYPE_P (op1))
16378 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16379 complain & tf_error);
16380 else
16381 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16382 complain & tf_error);
16383 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16384 {
16385 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16386 {
16387 if (!processing_template_decl && TYPE_P (op1))
16388 {
16389 r = build_min (SIZEOF_EXPR, size_type_node,
16390 build1 (NOP_EXPR, op1, error_mark_node));
16391 SIZEOF_EXPR_TYPE_P (r) = 1;
16392 }
16393 else
16394 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16395 TREE_SIDE_EFFECTS (r) = 0;
16396 TREE_READONLY (r) = 1;
16397 }
16398 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16399 }
16400 RETURN (r);
16401 }
16402
16403 case AT_ENCODE_EXPR:
16404 {
16405 op1 = TREE_OPERAND (t, 0);
16406 ++cp_unevaluated_operand;
16407 ++c_inhibit_evaluation_warnings;
16408 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16409 /*function_p=*/false,
16410 /*integral_constant_expression_p=*/false);
16411 --cp_unevaluated_operand;
16412 --c_inhibit_evaluation_warnings;
16413 RETURN (objc_build_encode_expr (op1));
16414 }
16415
16416 case NOEXCEPT_EXPR:
16417 op1 = TREE_OPERAND (t, 0);
16418 ++cp_unevaluated_operand;
16419 ++c_inhibit_evaluation_warnings;
16420 ++cp_noexcept_operand;
16421 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16422 /*function_p=*/false,
16423 /*integral_constant_expression_p=*/false);
16424 --cp_unevaluated_operand;
16425 --c_inhibit_evaluation_warnings;
16426 --cp_noexcept_operand;
16427 RETURN (finish_noexcept_expr (op1, complain));
16428
16429 case MODOP_EXPR:
16430 {
16431 warning_sentinel s(warn_div_by_zero);
16432 tree lhs = RECUR (TREE_OPERAND (t, 0));
16433 tree rhs = RECUR (TREE_OPERAND (t, 2));
16434 tree r = build_x_modify_expr
16435 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16436 complain|decltype_flag);
16437 /* TREE_NO_WARNING must be set if either the expression was
16438 parenthesized or it uses an operator such as >>= rather
16439 than plain assignment. In the former case, it was already
16440 set and must be copied. In the latter case,
16441 build_x_modify_expr sets it and it must not be reset
16442 here. */
16443 if (TREE_NO_WARNING (t))
16444 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16445
16446 RETURN (r);
16447 }
16448
16449 case ARROW_EXPR:
16450 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16451 args, complain, in_decl);
16452 /* Remember that there was a reference to this entity. */
16453 if (DECL_P (op1)
16454 && !mark_used (op1, complain) && !(complain & tf_error))
16455 RETURN (error_mark_node);
16456 RETURN (build_x_arrow (input_location, op1, complain));
16457
16458 case NEW_EXPR:
16459 {
16460 tree placement = RECUR (TREE_OPERAND (t, 0));
16461 tree init = RECUR (TREE_OPERAND (t, 3));
16462 vec<tree, va_gc> *placement_vec;
16463 vec<tree, va_gc> *init_vec;
16464 tree ret;
16465
16466 if (placement == NULL_TREE)
16467 placement_vec = NULL;
16468 else
16469 {
16470 placement_vec = make_tree_vector ();
16471 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16472 vec_safe_push (placement_vec, TREE_VALUE (placement));
16473 }
16474
16475 /* If there was an initializer in the original tree, but it
16476 instantiated to an empty list, then we should pass a
16477 non-NULL empty vector to tell build_new that it was an
16478 empty initializer() rather than no initializer. This can
16479 only happen when the initializer is a pack expansion whose
16480 parameter packs are of length zero. */
16481 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16482 init_vec = NULL;
16483 else
16484 {
16485 init_vec = make_tree_vector ();
16486 if (init == void_node)
16487 gcc_assert (init_vec != NULL);
16488 else
16489 {
16490 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16491 vec_safe_push (init_vec, TREE_VALUE (init));
16492 }
16493 }
16494
16495 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16496 tree op2 = RECUR (TREE_OPERAND (t, 2));
16497 ret = build_new (&placement_vec, op1, op2, &init_vec,
16498 NEW_EXPR_USE_GLOBAL (t),
16499 complain);
16500
16501 if (placement_vec != NULL)
16502 release_tree_vector (placement_vec);
16503 if (init_vec != NULL)
16504 release_tree_vector (init_vec);
16505
16506 RETURN (ret);
16507 }
16508
16509 case DELETE_EXPR:
16510 {
16511 tree op0 = RECUR (TREE_OPERAND (t, 0));
16512 tree op1 = RECUR (TREE_OPERAND (t, 1));
16513 RETURN (delete_sanity (op0, op1,
16514 DELETE_EXPR_USE_VEC (t),
16515 DELETE_EXPR_USE_GLOBAL (t),
16516 complain));
16517 }
16518
16519 case COMPOUND_EXPR:
16520 {
16521 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
16522 complain & ~tf_decltype, in_decl,
16523 /*function_p=*/false,
16524 integral_constant_expression_p);
16525 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
16526 op0,
16527 RECUR (TREE_OPERAND (t, 1)),
16528 complain|decltype_flag));
16529 }
16530
16531 case CALL_EXPR:
16532 {
16533 tree function;
16534 vec<tree, va_gc> *call_args;
16535 unsigned int nargs, i;
16536 bool qualified_p;
16537 bool koenig_p;
16538 tree ret;
16539
16540 function = CALL_EXPR_FN (t);
16541 /* When we parsed the expression, we determined whether or
16542 not Koenig lookup should be performed. */
16543 koenig_p = KOENIG_LOOKUP_P (t);
16544 if (TREE_CODE (function) == SCOPE_REF)
16545 {
16546 qualified_p = true;
16547 function = tsubst_qualified_id (function, args, complain, in_decl,
16548 /*done=*/false,
16549 /*address_p=*/false);
16550 }
16551 else if (koenig_p && identifier_p (function))
16552 {
16553 /* Do nothing; calling tsubst_copy_and_build on an identifier
16554 would incorrectly perform unqualified lookup again.
16555
16556 Note that we can also have an IDENTIFIER_NODE if the earlier
16557 unqualified lookup found a member function; in that case
16558 koenig_p will be false and we do want to do the lookup
16559 again to find the instantiated member function.
16560
16561 FIXME but doing that causes c++/15272, so we need to stop
16562 using IDENTIFIER_NODE in that situation. */
16563 qualified_p = false;
16564 }
16565 else
16566 {
16567 if (TREE_CODE (function) == COMPONENT_REF)
16568 {
16569 tree op = TREE_OPERAND (function, 1);
16570
16571 qualified_p = (TREE_CODE (op) == SCOPE_REF
16572 || (BASELINK_P (op)
16573 && BASELINK_QUALIFIED_P (op)));
16574 }
16575 else
16576 qualified_p = false;
16577
16578 if (TREE_CODE (function) == ADDR_EXPR
16579 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
16580 /* Avoid error about taking the address of a constructor. */
16581 function = TREE_OPERAND (function, 0);
16582
16583 function = tsubst_copy_and_build (function, args, complain,
16584 in_decl,
16585 !qualified_p,
16586 integral_constant_expression_p);
16587
16588 if (BASELINK_P (function))
16589 qualified_p = true;
16590 }
16591
16592 nargs = call_expr_nargs (t);
16593 call_args = make_tree_vector ();
16594 for (i = 0; i < nargs; ++i)
16595 {
16596 tree arg = CALL_EXPR_ARG (t, i);
16597
16598 if (!PACK_EXPANSION_P (arg))
16599 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
16600 else
16601 {
16602 /* Expand the pack expansion and push each entry onto
16603 CALL_ARGS. */
16604 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
16605 if (TREE_CODE (arg) == TREE_VEC)
16606 {
16607 unsigned int len, j;
16608
16609 len = TREE_VEC_LENGTH (arg);
16610 for (j = 0; j < len; ++j)
16611 {
16612 tree value = TREE_VEC_ELT (arg, j);
16613 if (value != NULL_TREE)
16614 value = convert_from_reference (value);
16615 vec_safe_push (call_args, value);
16616 }
16617 }
16618 else
16619 {
16620 /* A partial substitution. Add one entry. */
16621 vec_safe_push (call_args, arg);
16622 }
16623 }
16624 }
16625
16626 /* We do not perform argument-dependent lookup if normal
16627 lookup finds a non-function, in accordance with the
16628 expected resolution of DR 218. */
16629 if (koenig_p
16630 && ((is_overloaded_fn (function)
16631 /* If lookup found a member function, the Koenig lookup is
16632 not appropriate, even if an unqualified-name was used
16633 to denote the function. */
16634 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
16635 || identifier_p (function))
16636 /* Only do this when substitution turns a dependent call
16637 into a non-dependent call. */
16638 && type_dependent_expression_p_push (t)
16639 && !any_type_dependent_arguments_p (call_args))
16640 function = perform_koenig_lookup (function, call_args, tf_none);
16641
16642 if (identifier_p (function)
16643 && !any_type_dependent_arguments_p (call_args))
16644 {
16645 if (koenig_p && (complain & tf_warning_or_error))
16646 {
16647 /* For backwards compatibility and good diagnostics, try
16648 the unqualified lookup again if we aren't in SFINAE
16649 context. */
16650 tree unq = (tsubst_copy_and_build
16651 (function, args, complain, in_decl, true,
16652 integral_constant_expression_p));
16653 if (unq == error_mark_node)
16654 RETURN (error_mark_node);
16655
16656 if (unq != function)
16657 {
16658 tree fn = unq;
16659 if (INDIRECT_REF_P (fn))
16660 fn = TREE_OPERAND (fn, 0);
16661 if (TREE_CODE (fn) == COMPONENT_REF)
16662 fn = TREE_OPERAND (fn, 1);
16663 if (is_overloaded_fn (fn))
16664 fn = get_first_fn (fn);
16665 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16666 "%qD was not declared in this scope, "
16667 "and no declarations were found by "
16668 "argument-dependent lookup at the point "
16669 "of instantiation", function))
16670 {
16671 if (!DECL_P (fn))
16672 /* Can't say anything more. */;
16673 else if (DECL_CLASS_SCOPE_P (fn))
16674 {
16675 location_t loc = EXPR_LOC_OR_LOC (t,
16676 input_location);
16677 inform (loc,
16678 "declarations in dependent base %qT are "
16679 "not found by unqualified lookup",
16680 DECL_CLASS_CONTEXT (fn));
16681 if (current_class_ptr)
16682 inform (loc,
16683 "use %<this->%D%> instead", function);
16684 else
16685 inform (loc,
16686 "use %<%T::%D%> instead",
16687 current_class_name, function);
16688 }
16689 else
16690 inform (DECL_SOURCE_LOCATION (fn),
16691 "%qD declared here, later in the "
16692 "translation unit", fn);
16693 }
16694 function = unq;
16695 }
16696 }
16697 if (identifier_p (function))
16698 {
16699 if (complain & tf_error)
16700 unqualified_name_lookup_error (function);
16701 release_tree_vector (call_args);
16702 RETURN (error_mark_node);
16703 }
16704 }
16705
16706 /* Remember that there was a reference to this entity. */
16707 if (DECL_P (function)
16708 && !mark_used (function, complain) && !(complain & tf_error))
16709 RETURN (error_mark_node);
16710
16711 /* Put back tf_decltype for the actual call. */
16712 complain |= decltype_flag;
16713
16714 if (TREE_CODE (function) == OFFSET_REF)
16715 ret = build_offset_ref_call_from_tree (function, &call_args,
16716 complain);
16717 else if (TREE_CODE (function) == COMPONENT_REF)
16718 {
16719 tree instance = TREE_OPERAND (function, 0);
16720 tree fn = TREE_OPERAND (function, 1);
16721
16722 if (processing_template_decl
16723 && (type_dependent_expression_p (instance)
16724 || (!BASELINK_P (fn)
16725 && TREE_CODE (fn) != FIELD_DECL)
16726 || type_dependent_expression_p (fn)
16727 || any_type_dependent_arguments_p (call_args)))
16728 ret = build_nt_call_vec (function, call_args);
16729 else if (!BASELINK_P (fn))
16730 ret = finish_call_expr (function, &call_args,
16731 /*disallow_virtual=*/false,
16732 /*koenig_p=*/false,
16733 complain);
16734 else
16735 ret = (build_new_method_call
16736 (instance, fn,
16737 &call_args, NULL_TREE,
16738 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
16739 /*fn_p=*/NULL,
16740 complain));
16741 }
16742 else
16743 ret = finish_call_expr (function, &call_args,
16744 /*disallow_virtual=*/qualified_p,
16745 koenig_p,
16746 complain);
16747
16748 release_tree_vector (call_args);
16749
16750 if (ret != error_mark_node)
16751 {
16752 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
16753 bool ord = CALL_EXPR_ORDERED_ARGS (t);
16754 bool rev = CALL_EXPR_REVERSE_ARGS (t);
16755 if (op || ord || rev)
16756 {
16757 function = extract_call_expr (ret);
16758 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
16759 CALL_EXPR_ORDERED_ARGS (function) = ord;
16760 CALL_EXPR_REVERSE_ARGS (function) = rev;
16761 }
16762 }
16763
16764 RETURN (ret);
16765 }
16766
16767 case COND_EXPR:
16768 {
16769 tree cond = RECUR (TREE_OPERAND (t, 0));
16770 tree folded_cond = fold_non_dependent_expr (cond);
16771 tree exp1, exp2;
16772
16773 if (TREE_CODE (folded_cond) == INTEGER_CST)
16774 {
16775 if (integer_zerop (folded_cond))
16776 {
16777 ++c_inhibit_evaluation_warnings;
16778 exp1 = RECUR (TREE_OPERAND (t, 1));
16779 --c_inhibit_evaluation_warnings;
16780 exp2 = RECUR (TREE_OPERAND (t, 2));
16781 }
16782 else
16783 {
16784 exp1 = RECUR (TREE_OPERAND (t, 1));
16785 ++c_inhibit_evaluation_warnings;
16786 exp2 = RECUR (TREE_OPERAND (t, 2));
16787 --c_inhibit_evaluation_warnings;
16788 }
16789 cond = folded_cond;
16790 }
16791 else
16792 {
16793 exp1 = RECUR (TREE_OPERAND (t, 1));
16794 exp2 = RECUR (TREE_OPERAND (t, 2));
16795 }
16796
16797 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
16798 cond, exp1, exp2, complain));
16799 }
16800
16801 case PSEUDO_DTOR_EXPR:
16802 {
16803 tree op0 = RECUR (TREE_OPERAND (t, 0));
16804 tree op1 = RECUR (TREE_OPERAND (t, 1));
16805 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
16806 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
16807 input_location));
16808 }
16809
16810 case TREE_LIST:
16811 {
16812 tree purpose, value, chain;
16813
16814 if (t == void_list_node)
16815 RETURN (t);
16816
16817 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
16818 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
16819 {
16820 /* We have pack expansions, so expand those and
16821 create a new list out of it. */
16822 tree purposevec = NULL_TREE;
16823 tree valuevec = NULL_TREE;
16824 tree chain;
16825 int i, len = -1;
16826
16827 /* Expand the argument expressions. */
16828 if (TREE_PURPOSE (t))
16829 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
16830 complain, in_decl);
16831 if (TREE_VALUE (t))
16832 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
16833 complain, in_decl);
16834
16835 /* Build the rest of the list. */
16836 chain = TREE_CHAIN (t);
16837 if (chain && chain != void_type_node)
16838 chain = RECUR (chain);
16839
16840 /* Determine the number of arguments. */
16841 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
16842 {
16843 len = TREE_VEC_LENGTH (purposevec);
16844 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16845 }
16846 else if (TREE_CODE (valuevec) == TREE_VEC)
16847 len = TREE_VEC_LENGTH (valuevec);
16848 else
16849 {
16850 /* Since we only performed a partial substitution into
16851 the argument pack, we only RETURN (a single list
16852 node. */
16853 if (purposevec == TREE_PURPOSE (t)
16854 && valuevec == TREE_VALUE (t)
16855 && chain == TREE_CHAIN (t))
16856 RETURN (t);
16857
16858 RETURN (tree_cons (purposevec, valuevec, chain));
16859 }
16860
16861 /* Convert the argument vectors into a TREE_LIST */
16862 i = len;
16863 while (i > 0)
16864 {
16865 /* Grab the Ith values. */
16866 i--;
16867 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
16868 : NULL_TREE;
16869 value
16870 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
16871 : NULL_TREE;
16872
16873 /* Build the list (backwards). */
16874 chain = tree_cons (purpose, value, chain);
16875 }
16876
16877 RETURN (chain);
16878 }
16879
16880 purpose = TREE_PURPOSE (t);
16881 if (purpose)
16882 purpose = RECUR (purpose);
16883 value = TREE_VALUE (t);
16884 if (value)
16885 value = RECUR (value);
16886 chain = TREE_CHAIN (t);
16887 if (chain && chain != void_type_node)
16888 chain = RECUR (chain);
16889 if (purpose == TREE_PURPOSE (t)
16890 && value == TREE_VALUE (t)
16891 && chain == TREE_CHAIN (t))
16892 RETURN (t);
16893 RETURN (tree_cons (purpose, value, chain));
16894 }
16895
16896 case COMPONENT_REF:
16897 {
16898 tree object;
16899 tree object_type;
16900 tree member;
16901 tree r;
16902
16903 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16904 args, complain, in_decl);
16905 /* Remember that there was a reference to this entity. */
16906 if (DECL_P (object)
16907 && !mark_used (object, complain) && !(complain & tf_error))
16908 RETURN (error_mark_node);
16909 object_type = TREE_TYPE (object);
16910
16911 member = TREE_OPERAND (t, 1);
16912 if (BASELINK_P (member))
16913 member = tsubst_baselink (member,
16914 non_reference (TREE_TYPE (object)),
16915 args, complain, in_decl);
16916 else
16917 member = tsubst_copy (member, args, complain, in_decl);
16918 if (member == error_mark_node)
16919 RETURN (error_mark_node);
16920
16921 if (type_dependent_expression_p (object))
16922 /* We can't do much here. */;
16923 else if (!CLASS_TYPE_P (object_type))
16924 {
16925 if (scalarish_type_p (object_type))
16926 {
16927 tree s = NULL_TREE;
16928 tree dtor = member;
16929
16930 if (TREE_CODE (dtor) == SCOPE_REF)
16931 {
16932 s = TREE_OPERAND (dtor, 0);
16933 dtor = TREE_OPERAND (dtor, 1);
16934 }
16935 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
16936 {
16937 dtor = TREE_OPERAND (dtor, 0);
16938 if (TYPE_P (dtor))
16939 RETURN (finish_pseudo_destructor_expr
16940 (object, s, dtor, input_location));
16941 }
16942 }
16943 }
16944 else if (TREE_CODE (member) == SCOPE_REF
16945 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
16946 {
16947 /* Lookup the template functions now that we know what the
16948 scope is. */
16949 tree scope = TREE_OPERAND (member, 0);
16950 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
16951 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
16952 member = lookup_qualified_name (scope, tmpl,
16953 /*is_type_p=*/false,
16954 /*complain=*/false);
16955 if (BASELINK_P (member))
16956 {
16957 BASELINK_FUNCTIONS (member)
16958 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
16959 args);
16960 member = (adjust_result_of_qualified_name_lookup
16961 (member, BINFO_TYPE (BASELINK_BINFO (member)),
16962 object_type));
16963 }
16964 else
16965 {
16966 qualified_name_lookup_error (scope, tmpl, member,
16967 input_location);
16968 RETURN (error_mark_node);
16969 }
16970 }
16971 else if (TREE_CODE (member) == SCOPE_REF
16972 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
16973 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
16974 {
16975 if (complain & tf_error)
16976 {
16977 if (TYPE_P (TREE_OPERAND (member, 0)))
16978 error ("%qT is not a class or namespace",
16979 TREE_OPERAND (member, 0));
16980 else
16981 error ("%qD is not a class or namespace",
16982 TREE_OPERAND (member, 0));
16983 }
16984 RETURN (error_mark_node);
16985 }
16986 else if (TREE_CODE (member) == FIELD_DECL)
16987 {
16988 r = finish_non_static_data_member (member, object, NULL_TREE);
16989 if (TREE_CODE (r) == COMPONENT_REF)
16990 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16991 RETURN (r);
16992 }
16993
16994 r = finish_class_member_access_expr (object, member,
16995 /*template_p=*/false,
16996 complain);
16997 if (TREE_CODE (r) == COMPONENT_REF)
16998 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16999 RETURN (r);
17000 }
17001
17002 case THROW_EXPR:
17003 RETURN (build_throw
17004 (RECUR (TREE_OPERAND (t, 0))));
17005
17006 case CONSTRUCTOR:
17007 {
17008 vec<constructor_elt, va_gc> *n;
17009 constructor_elt *ce;
17010 unsigned HOST_WIDE_INT idx;
17011 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17012 bool process_index_p;
17013 int newlen;
17014 bool need_copy_p = false;
17015 tree r;
17016
17017 if (type == error_mark_node)
17018 RETURN (error_mark_node);
17019
17020 /* digest_init will do the wrong thing if we let it. */
17021 if (type && TYPE_PTRMEMFUNC_P (type))
17022 RETURN (t);
17023
17024 /* We do not want to process the index of aggregate
17025 initializers as they are identifier nodes which will be
17026 looked up by digest_init. */
17027 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17028
17029 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17030 newlen = vec_safe_length (n);
17031 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17032 {
17033 if (ce->index && process_index_p
17034 /* An identifier index is looked up in the type
17035 being initialized, not the current scope. */
17036 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17037 ce->index = RECUR (ce->index);
17038
17039 if (PACK_EXPANSION_P (ce->value))
17040 {
17041 /* Substitute into the pack expansion. */
17042 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17043 in_decl);
17044
17045 if (ce->value == error_mark_node
17046 || PACK_EXPANSION_P (ce->value))
17047 ;
17048 else if (TREE_VEC_LENGTH (ce->value) == 1)
17049 /* Just move the argument into place. */
17050 ce->value = TREE_VEC_ELT (ce->value, 0);
17051 else
17052 {
17053 /* Update the length of the final CONSTRUCTOR
17054 arguments vector, and note that we will need to
17055 copy.*/
17056 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17057 need_copy_p = true;
17058 }
17059 }
17060 else
17061 ce->value = RECUR (ce->value);
17062 }
17063
17064 if (need_copy_p)
17065 {
17066 vec<constructor_elt, va_gc> *old_n = n;
17067
17068 vec_alloc (n, newlen);
17069 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17070 {
17071 if (TREE_CODE (ce->value) == TREE_VEC)
17072 {
17073 int i, len = TREE_VEC_LENGTH (ce->value);
17074 for (i = 0; i < len; ++i)
17075 CONSTRUCTOR_APPEND_ELT (n, 0,
17076 TREE_VEC_ELT (ce->value, i));
17077 }
17078 else
17079 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17080 }
17081 }
17082
17083 r = build_constructor (init_list_type_node, n);
17084 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17085
17086 if (TREE_HAS_CONSTRUCTOR (t))
17087 RETURN (finish_compound_literal (type, r, complain));
17088
17089 TREE_TYPE (r) = type;
17090 RETURN (r);
17091 }
17092
17093 case TYPEID_EXPR:
17094 {
17095 tree operand_0 = TREE_OPERAND (t, 0);
17096 if (TYPE_P (operand_0))
17097 {
17098 operand_0 = tsubst (operand_0, args, complain, in_decl);
17099 RETURN (get_typeid (operand_0, complain));
17100 }
17101 else
17102 {
17103 operand_0 = RECUR (operand_0);
17104 RETURN (build_typeid (operand_0, complain));
17105 }
17106 }
17107
17108 case VAR_DECL:
17109 if (!args)
17110 RETURN (t);
17111 else if (DECL_PACK_P (t))
17112 {
17113 /* We don't build decls for an instantiation of a
17114 variadic capture proxy, we instantiate the elements
17115 when needed. */
17116 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17117 return RECUR (DECL_VALUE_EXPR (t));
17118 }
17119 /* Fall through */
17120
17121 case PARM_DECL:
17122 {
17123 tree r = tsubst_copy (t, args, complain, in_decl);
17124 /* ??? We're doing a subset of finish_id_expression here. */
17125 if (VAR_P (r)
17126 && !processing_template_decl
17127 && !cp_unevaluated_operand
17128 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17129 && CP_DECL_THREAD_LOCAL_P (r))
17130 {
17131 if (tree wrap = get_tls_wrapper_fn (r))
17132 /* Replace an evaluated use of the thread_local variable with
17133 a call to its wrapper. */
17134 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17135 }
17136 else if (outer_automatic_var_p (r))
17137 {
17138 r = process_outer_var_ref (r, complain);
17139 if (is_capture_proxy (r))
17140 register_local_specialization (r, t);
17141 }
17142
17143 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17144 /* If the original type was a reference, we'll be wrapped in
17145 the appropriate INDIRECT_REF. */
17146 r = convert_from_reference (r);
17147 RETURN (r);
17148 }
17149
17150 case VA_ARG_EXPR:
17151 {
17152 tree op0 = RECUR (TREE_OPERAND (t, 0));
17153 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17154 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17155 }
17156
17157 case OFFSETOF_EXPR:
17158 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
17159 EXPR_LOCATION (t)));
17160
17161 case TRAIT_EXPR:
17162 {
17163 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17164 complain, in_decl);
17165
17166 tree type2 = TRAIT_EXPR_TYPE2 (t);
17167 if (type2 && TREE_CODE (type2) == TREE_LIST)
17168 type2 = RECUR (type2);
17169 else if (type2)
17170 type2 = tsubst (type2, args, complain, in_decl);
17171
17172 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17173 }
17174
17175 case STMT_EXPR:
17176 {
17177 tree old_stmt_expr = cur_stmt_expr;
17178 tree stmt_expr = begin_stmt_expr ();
17179
17180 cur_stmt_expr = stmt_expr;
17181 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17182 integral_constant_expression_p);
17183 stmt_expr = finish_stmt_expr (stmt_expr, false);
17184 cur_stmt_expr = old_stmt_expr;
17185
17186 /* If the resulting list of expression statement is empty,
17187 fold it further into void_node. */
17188 if (empty_expr_stmt_p (stmt_expr))
17189 stmt_expr = void_node;
17190
17191 RETURN (stmt_expr);
17192 }
17193
17194 case LAMBDA_EXPR:
17195 {
17196 tree r = build_lambda_expr ();
17197
17198 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17199 LAMBDA_EXPR_CLOSURE (r) = type;
17200 CLASSTYPE_LAMBDA_EXPR (type) = r;
17201
17202 LAMBDA_EXPR_LOCATION (r)
17203 = LAMBDA_EXPR_LOCATION (t);
17204 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17205 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17206 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17207 LAMBDA_EXPR_DISCRIMINATOR (r)
17208 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17209 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17210 if (!scope)
17211 /* No substitution needed. */;
17212 else if (VAR_OR_FUNCTION_DECL_P (scope))
17213 /* For a function or variable scope, we want to use tsubst so that we
17214 don't complain about referring to an auto before deduction. */
17215 scope = tsubst (scope, args, complain, in_decl);
17216 else if (TREE_CODE (scope) == PARM_DECL)
17217 {
17218 /* Look up the parameter we want directly, as tsubst_copy
17219 doesn't do what we need. */
17220 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17221 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17222 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17223 parm = DECL_CHAIN (parm);
17224 scope = parm;
17225 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17226 if (DECL_CONTEXT (scope) == NULL_TREE)
17227 DECL_CONTEXT (scope) = fn;
17228 }
17229 else if (TREE_CODE (scope) == FIELD_DECL)
17230 /* For a field, use tsubst_copy so that we look up the existing field
17231 rather than build a new one. */
17232 scope = RECUR (scope);
17233 else
17234 gcc_unreachable ();
17235 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17236
17237 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17238 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17239
17240 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17241 determine_visibility (TYPE_NAME (type));
17242 /* Now that we know visibility, instantiate the type so we have a
17243 declaration of the op() for later calls to lambda_function. */
17244 complete_type (type);
17245
17246 if (tree fn = lambda_function (type))
17247 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17248
17249 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17250
17251 insert_pending_capture_proxies ();
17252
17253 RETURN (build_lambda_object (r));
17254 }
17255
17256 case TARGET_EXPR:
17257 /* We can get here for a constant initializer of non-dependent type.
17258 FIXME stop folding in cp_parser_initializer_clause. */
17259 {
17260 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17261 complain);
17262 RETURN (r);
17263 }
17264
17265 case TRANSACTION_EXPR:
17266 RETURN (tsubst_expr(t, args, complain, in_decl,
17267 integral_constant_expression_p));
17268
17269 case PAREN_EXPR:
17270 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17271
17272 case VEC_PERM_EXPR:
17273 {
17274 tree op0 = RECUR (TREE_OPERAND (t, 0));
17275 tree op1 = RECUR (TREE_OPERAND (t, 1));
17276 tree op2 = RECUR (TREE_OPERAND (t, 2));
17277 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17278 complain));
17279 }
17280
17281 case REQUIRES_EXPR:
17282 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17283
17284 default:
17285 /* Handle Objective-C++ constructs, if appropriate. */
17286 {
17287 tree subst
17288 = objcp_tsubst_copy_and_build (t, args, complain,
17289 in_decl, /*function_p=*/false);
17290 if (subst)
17291 RETURN (subst);
17292 }
17293 RETURN (tsubst_copy (t, args, complain, in_decl));
17294 }
17295
17296 #undef RECUR
17297 #undef RETURN
17298 out:
17299 input_location = loc;
17300 return retval;
17301 }
17302
17303 /* Verify that the instantiated ARGS are valid. For type arguments,
17304 make sure that the type's linkage is ok. For non-type arguments,
17305 make sure they are constants if they are integral or enumerations.
17306 Emit an error under control of COMPLAIN, and return TRUE on error. */
17307
17308 static bool
17309 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17310 {
17311 if (dependent_template_arg_p (t))
17312 return false;
17313 if (ARGUMENT_PACK_P (t))
17314 {
17315 tree vec = ARGUMENT_PACK_ARGS (t);
17316 int len = TREE_VEC_LENGTH (vec);
17317 bool result = false;
17318 int i;
17319
17320 for (i = 0; i < len; ++i)
17321 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17322 result = true;
17323 return result;
17324 }
17325 else if (TYPE_P (t))
17326 {
17327 /* [basic.link]: A name with no linkage (notably, the name
17328 of a class or enumeration declared in a local scope)
17329 shall not be used to declare an entity with linkage.
17330 This implies that names with no linkage cannot be used as
17331 template arguments
17332
17333 DR 757 relaxes this restriction for C++0x. */
17334 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17335 : no_linkage_check (t, /*relaxed_p=*/false));
17336
17337 if (nt)
17338 {
17339 /* DR 488 makes use of a type with no linkage cause
17340 type deduction to fail. */
17341 if (complain & tf_error)
17342 {
17343 if (TYPE_UNNAMED_P (nt))
17344 error ("%qT is/uses unnamed type", t);
17345 else
17346 error ("template argument for %qD uses local type %qT",
17347 tmpl, t);
17348 }
17349 return true;
17350 }
17351 /* In order to avoid all sorts of complications, we do not
17352 allow variably-modified types as template arguments. */
17353 else if (variably_modified_type_p (t, NULL_TREE))
17354 {
17355 if (complain & tf_error)
17356 error ("%qT is a variably modified type", t);
17357 return true;
17358 }
17359 }
17360 /* Class template and alias template arguments should be OK. */
17361 else if (DECL_TYPE_TEMPLATE_P (t))
17362 ;
17363 /* A non-type argument of integral or enumerated type must be a
17364 constant. */
17365 else if (TREE_TYPE (t)
17366 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17367 && !REFERENCE_REF_P (t)
17368 && !TREE_CONSTANT (t))
17369 {
17370 if (complain & tf_error)
17371 error ("integral expression %qE is not constant", t);
17372 return true;
17373 }
17374 return false;
17375 }
17376
17377 static bool
17378 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17379 {
17380 int ix, len = DECL_NTPARMS (tmpl);
17381 bool result = false;
17382
17383 for (ix = 0; ix != len; ix++)
17384 {
17385 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17386 result = true;
17387 }
17388 if (result && (complain & tf_error))
17389 error (" trying to instantiate %qD", tmpl);
17390 return result;
17391 }
17392
17393 /* We're out of SFINAE context now, so generate diagnostics for the access
17394 errors we saw earlier when instantiating D from TMPL and ARGS. */
17395
17396 static void
17397 recheck_decl_substitution (tree d, tree tmpl, tree args)
17398 {
17399 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17400 tree type = TREE_TYPE (pattern);
17401 location_t loc = input_location;
17402
17403 push_access_scope (d);
17404 push_deferring_access_checks (dk_no_deferred);
17405 input_location = DECL_SOURCE_LOCATION (pattern);
17406 tsubst (type, args, tf_warning_or_error, d);
17407 input_location = loc;
17408 pop_deferring_access_checks ();
17409 pop_access_scope (d);
17410 }
17411
17412 /* Instantiate the indicated variable, function, or alias template TMPL with
17413 the template arguments in TARG_PTR. */
17414
17415 static tree
17416 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17417 {
17418 tree targ_ptr = orig_args;
17419 tree fndecl;
17420 tree gen_tmpl;
17421 tree spec;
17422 bool access_ok = true;
17423
17424 if (tmpl == error_mark_node)
17425 return error_mark_node;
17426
17427 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
17428
17429 /* If this function is a clone, handle it specially. */
17430 if (DECL_CLONED_FUNCTION_P (tmpl))
17431 {
17432 tree spec;
17433 tree clone;
17434
17435 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17436 DECL_CLONED_FUNCTION. */
17437 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
17438 targ_ptr, complain);
17439 if (spec == error_mark_node)
17440 return error_mark_node;
17441
17442 /* Look for the clone. */
17443 FOR_EACH_CLONE (clone, spec)
17444 if (DECL_NAME (clone) == DECL_NAME (tmpl))
17445 return clone;
17446 /* We should always have found the clone by now. */
17447 gcc_unreachable ();
17448 return NULL_TREE;
17449 }
17450
17451 if (targ_ptr == error_mark_node)
17452 return error_mark_node;
17453
17454 /* Check to see if we already have this specialization. */
17455 gen_tmpl = most_general_template (tmpl);
17456 if (TMPL_ARGS_DEPTH (targ_ptr)
17457 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
17458 /* targ_ptr only has the innermost template args, so add the outer ones
17459 from tmpl, which could be either a partial instantiation or gen_tmpl (in
17460 the case of a non-dependent call within a template definition). */
17461 targ_ptr = (add_outermost_template_args
17462 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
17463 targ_ptr));
17464
17465 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17466 but it doesn't seem to be on the hot path. */
17467 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
17468
17469 gcc_assert (tmpl == gen_tmpl
17470 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
17471 == spec)
17472 || fndecl == NULL_TREE);
17473
17474 if (spec != NULL_TREE)
17475 {
17476 if (FNDECL_HAS_ACCESS_ERRORS (spec))
17477 {
17478 if (complain & tf_error)
17479 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
17480 return error_mark_node;
17481 }
17482 return spec;
17483 }
17484
17485 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
17486 complain))
17487 return error_mark_node;
17488
17489 /* We are building a FUNCTION_DECL, during which the access of its
17490 parameters and return types have to be checked. However this
17491 FUNCTION_DECL which is the desired context for access checking
17492 is not built yet. We solve this chicken-and-egg problem by
17493 deferring all checks until we have the FUNCTION_DECL. */
17494 push_deferring_access_checks (dk_deferred);
17495
17496 /* Instantiation of the function happens in the context of the function
17497 template, not the context of the overload resolution we're doing. */
17498 push_to_top_level ();
17499 /* If there are dependent arguments, e.g. because we're doing partial
17500 ordering, make sure processing_template_decl stays set. */
17501 if (uses_template_parms (targ_ptr))
17502 ++processing_template_decl;
17503 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17504 {
17505 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
17506 complain, gen_tmpl, true);
17507 push_nested_class (ctx);
17508 }
17509
17510 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
17511
17512 fndecl = NULL_TREE;
17513 if (VAR_P (pattern))
17514 {
17515 /* We need to determine if we're using a partial or explicit
17516 specialization now, because the type of the variable could be
17517 different. */
17518 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
17519 tree elt = most_specialized_partial_spec (tid, complain);
17520 if (elt == error_mark_node)
17521 pattern = error_mark_node;
17522 else if (elt)
17523 {
17524 tree partial_tmpl = TREE_VALUE (elt);
17525 tree partial_args = TREE_PURPOSE (elt);
17526 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
17527 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
17528 }
17529 }
17530
17531 /* Substitute template parameters to obtain the specialization. */
17532 if (fndecl == NULL_TREE)
17533 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
17534 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17535 pop_nested_class ();
17536 pop_from_top_level ();
17537
17538 if (fndecl == error_mark_node)
17539 {
17540 pop_deferring_access_checks ();
17541 return error_mark_node;
17542 }
17543
17544 /* The DECL_TI_TEMPLATE should always be the immediate parent
17545 template, not the most general template. */
17546 DECL_TI_TEMPLATE (fndecl) = tmpl;
17547 DECL_TI_ARGS (fndecl) = targ_ptr;
17548
17549 /* Now we know the specialization, compute access previously
17550 deferred. */
17551 push_access_scope (fndecl);
17552 if (!perform_deferred_access_checks (complain))
17553 access_ok = false;
17554 pop_access_scope (fndecl);
17555 pop_deferring_access_checks ();
17556
17557 /* If we've just instantiated the main entry point for a function,
17558 instantiate all the alternate entry points as well. We do this
17559 by cloning the instantiation of the main entry point, not by
17560 instantiating the template clones. */
17561 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
17562 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
17563
17564 if (!access_ok)
17565 {
17566 if (!(complain & tf_error))
17567 {
17568 /* Remember to reinstantiate when we're out of SFINAE so the user
17569 can see the errors. */
17570 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
17571 }
17572 return error_mark_node;
17573 }
17574 return fndecl;
17575 }
17576
17577 /* Wrapper for instantiate_template_1. */
17578
17579 tree
17580 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
17581 {
17582 tree ret;
17583 timevar_push (TV_TEMPLATE_INST);
17584 ret = instantiate_template_1 (tmpl, orig_args, complain);
17585 timevar_pop (TV_TEMPLATE_INST);
17586 return ret;
17587 }
17588
17589 /* Instantiate the alias template TMPL with ARGS. Also push a template
17590 instantiation level, which instantiate_template doesn't do because
17591 functions and variables have sufficient context established by the
17592 callers. */
17593
17594 static tree
17595 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
17596 {
17597 struct pending_template *old_last_pend = last_pending_template;
17598 struct tinst_level *old_error_tinst = last_error_tinst_level;
17599 if (tmpl == error_mark_node || args == error_mark_node)
17600 return error_mark_node;
17601 tree tinst = build_tree_list (tmpl, args);
17602 if (!push_tinst_level (tinst))
17603 {
17604 ggc_free (tinst);
17605 return error_mark_node;
17606 }
17607
17608 args =
17609 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
17610 args, tmpl, complain,
17611 /*require_all_args=*/true,
17612 /*use_default_args=*/true);
17613
17614 tree r = instantiate_template (tmpl, args, complain);
17615 pop_tinst_level ();
17616 /* We can't free this if a pending_template entry or last_error_tinst_level
17617 is pointing at it. */
17618 if (last_pending_template == old_last_pend
17619 && last_error_tinst_level == old_error_tinst)
17620 ggc_free (tinst);
17621
17622 return r;
17623 }
17624
17625 /* PARM is a template parameter pack for FN. Returns true iff
17626 PARM is used in a deducible way in the argument list of FN. */
17627
17628 static bool
17629 pack_deducible_p (tree parm, tree fn)
17630 {
17631 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
17632 for (; t; t = TREE_CHAIN (t))
17633 {
17634 tree type = TREE_VALUE (t);
17635 tree packs;
17636 if (!PACK_EXPANSION_P (type))
17637 continue;
17638 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
17639 packs; packs = TREE_CHAIN (packs))
17640 if (template_args_equal (TREE_VALUE (packs), parm))
17641 {
17642 /* The template parameter pack is used in a function parameter
17643 pack. If this is the end of the parameter list, the
17644 template parameter pack is deducible. */
17645 if (TREE_CHAIN (t) == void_list_node)
17646 return true;
17647 else
17648 /* Otherwise, not. Well, it could be deduced from
17649 a non-pack parameter, but doing so would end up with
17650 a deduction mismatch, so don't bother. */
17651 return false;
17652 }
17653 }
17654 /* The template parameter pack isn't used in any function parameter
17655 packs, but it might be used deeper, e.g. tuple<Args...>. */
17656 return true;
17657 }
17658
17659 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17660 NARGS elements of the arguments that are being used when calling
17661 it. TARGS is a vector into which the deduced template arguments
17662 are placed.
17663
17664 Returns either a FUNCTION_DECL for the matching specialization of FN or
17665 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17666 true, diagnostics will be printed to explain why it failed.
17667
17668 If FN is a conversion operator, or we are trying to produce a specific
17669 specialization, RETURN_TYPE is the return type desired.
17670
17671 The EXPLICIT_TARGS are explicit template arguments provided via a
17672 template-id.
17673
17674 The parameter STRICT is one of:
17675
17676 DEDUCE_CALL:
17677 We are deducing arguments for a function call, as in
17678 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17679 deducing arguments for a call to the result of a conversion
17680 function template, as in [over.call.object].
17681
17682 DEDUCE_CONV:
17683 We are deducing arguments for a conversion function, as in
17684 [temp.deduct.conv].
17685
17686 DEDUCE_EXACT:
17687 We are deducing arguments when doing an explicit instantiation
17688 as in [temp.explicit], when determining an explicit specialization
17689 as in [temp.expl.spec], or when taking the address of a function
17690 template, as in [temp.deduct.funcaddr]. */
17691
17692 tree
17693 fn_type_unification (tree fn,
17694 tree explicit_targs,
17695 tree targs,
17696 const tree *args,
17697 unsigned int nargs,
17698 tree return_type,
17699 unification_kind_t strict,
17700 int flags,
17701 bool explain_p,
17702 bool decltype_p)
17703 {
17704 tree parms;
17705 tree fntype;
17706 tree decl = NULL_TREE;
17707 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17708 bool ok;
17709 static int deduction_depth;
17710 struct pending_template *old_last_pend = last_pending_template;
17711 struct tinst_level *old_error_tinst = last_error_tinst_level;
17712 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17713 tree tinst;
17714 tree r = error_mark_node;
17715
17716 tree full_targs = targs;
17717 if (TMPL_ARGS_DEPTH (targs)
17718 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
17719 full_targs = (add_outermost_template_args
17720 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
17721 targs));
17722
17723 if (decltype_p)
17724 complain |= tf_decltype;
17725
17726 /* In C++0x, it's possible to have a function template whose type depends
17727 on itself recursively. This is most obvious with decltype, but can also
17728 occur with enumeration scope (c++/48969). So we need to catch infinite
17729 recursion and reject the substitution at deduction time; this function
17730 will return error_mark_node for any repeated substitution.
17731
17732 This also catches excessive recursion such as when f<N> depends on
17733 f<N-1> across all integers, and returns error_mark_node for all the
17734 substitutions back up to the initial one.
17735
17736 This is, of course, not reentrant. */
17737 if (excessive_deduction_depth)
17738 return error_mark_node;
17739 tinst = build_tree_list (fn, NULL_TREE);
17740 ++deduction_depth;
17741
17742 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
17743
17744 fntype = TREE_TYPE (fn);
17745 if (explicit_targs)
17746 {
17747 /* [temp.deduct]
17748
17749 The specified template arguments must match the template
17750 parameters in kind (i.e., type, nontype, template), and there
17751 must not be more arguments than there are parameters;
17752 otherwise type deduction fails.
17753
17754 Nontype arguments must match the types of the corresponding
17755 nontype template parameters, or must be convertible to the
17756 types of the corresponding nontype parameters as specified in
17757 _temp.arg.nontype_, otherwise type deduction fails.
17758
17759 All references in the function type of the function template
17760 to the corresponding template parameters are replaced by the
17761 specified template argument values. If a substitution in a
17762 template parameter or in the function type of the function
17763 template results in an invalid type, type deduction fails. */
17764 int i, len = TREE_VEC_LENGTH (tparms);
17765 location_t loc = input_location;
17766 bool incomplete = false;
17767
17768 if (explicit_targs == error_mark_node)
17769 goto fail;
17770
17771 if (TMPL_ARGS_DEPTH (explicit_targs)
17772 < TMPL_ARGS_DEPTH (full_targs))
17773 explicit_targs = add_outermost_template_args (full_targs,
17774 explicit_targs);
17775
17776 /* Adjust any explicit template arguments before entering the
17777 substitution context. */
17778 explicit_targs
17779 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
17780 complain,
17781 /*require_all_args=*/false,
17782 /*use_default_args=*/false));
17783 if (explicit_targs == error_mark_node)
17784 goto fail;
17785
17786 /* Substitute the explicit args into the function type. This is
17787 necessary so that, for instance, explicitly declared function
17788 arguments can match null pointed constants. If we were given
17789 an incomplete set of explicit args, we must not do semantic
17790 processing during substitution as we could create partial
17791 instantiations. */
17792 for (i = 0; i < len; i++)
17793 {
17794 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17795 bool parameter_pack = false;
17796 tree targ = TREE_VEC_ELT (explicit_targs, i);
17797
17798 /* Dig out the actual parm. */
17799 if (TREE_CODE (parm) == TYPE_DECL
17800 || TREE_CODE (parm) == TEMPLATE_DECL)
17801 {
17802 parm = TREE_TYPE (parm);
17803 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
17804 }
17805 else if (TREE_CODE (parm) == PARM_DECL)
17806 {
17807 parm = DECL_INITIAL (parm);
17808 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
17809 }
17810
17811 if (!parameter_pack && targ == NULL_TREE)
17812 /* No explicit argument for this template parameter. */
17813 incomplete = true;
17814
17815 if (parameter_pack && pack_deducible_p (parm, fn))
17816 {
17817 /* Mark the argument pack as "incomplete". We could
17818 still deduce more arguments during unification.
17819 We remove this mark in type_unification_real. */
17820 if (targ)
17821 {
17822 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
17823 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
17824 = ARGUMENT_PACK_ARGS (targ);
17825 }
17826
17827 /* We have some incomplete argument packs. */
17828 incomplete = true;
17829 }
17830 }
17831
17832 TREE_VALUE (tinst) = explicit_targs;
17833 if (!push_tinst_level (tinst))
17834 {
17835 excessive_deduction_depth = true;
17836 goto fail;
17837 }
17838 processing_template_decl += incomplete;
17839 input_location = DECL_SOURCE_LOCATION (fn);
17840 /* Ignore any access checks; we'll see them again in
17841 instantiate_template and they might have the wrong
17842 access path at this point. */
17843 push_deferring_access_checks (dk_deferred);
17844 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
17845 complain | tf_partial, NULL_TREE);
17846 pop_deferring_access_checks ();
17847 input_location = loc;
17848 processing_template_decl -= incomplete;
17849 pop_tinst_level ();
17850
17851 if (fntype == error_mark_node)
17852 goto fail;
17853
17854 /* Place the explicitly specified arguments in TARGS. */
17855 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
17856 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
17857 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
17858 }
17859
17860 /* Never do unification on the 'this' parameter. */
17861 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
17862
17863 if (return_type && strict == DEDUCE_CALL)
17864 {
17865 /* We're deducing for a call to the result of a template conversion
17866 function. The parms we really want are in return_type. */
17867 if (POINTER_TYPE_P (return_type))
17868 return_type = TREE_TYPE (return_type);
17869 parms = TYPE_ARG_TYPES (return_type);
17870 }
17871 else if (return_type)
17872 {
17873 tree *new_args;
17874
17875 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
17876 new_args = XALLOCAVEC (tree, nargs + 1);
17877 new_args[0] = return_type;
17878 memcpy (new_args + 1, args, nargs * sizeof (tree));
17879 args = new_args;
17880 ++nargs;
17881 }
17882
17883 /* We allow incomplete unification without an error message here
17884 because the standard doesn't seem to explicitly prohibit it. Our
17885 callers must be ready to deal with unification failures in any
17886 event. */
17887
17888 TREE_VALUE (tinst) = targs;
17889 /* If we aren't explaining yet, push tinst context so we can see where
17890 any errors (e.g. from class instantiations triggered by instantiation
17891 of default template arguments) come from. If we are explaining, this
17892 context is redundant. */
17893 if (!explain_p && !push_tinst_level (tinst))
17894 {
17895 excessive_deduction_depth = true;
17896 goto fail;
17897 }
17898
17899 /* type_unification_real will pass back any access checks from default
17900 template argument substitution. */
17901 vec<deferred_access_check, va_gc> *checks;
17902 checks = NULL;
17903
17904 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17905 full_targs, parms, args, nargs, /*subr=*/0,
17906 strict, flags, &checks, explain_p);
17907 if (!explain_p)
17908 pop_tinst_level ();
17909 if (!ok)
17910 goto fail;
17911
17912 /* Now that we have bindings for all of the template arguments,
17913 ensure that the arguments deduced for the template template
17914 parameters have compatible template parameter lists. We cannot
17915 check this property before we have deduced all template
17916 arguments, because the template parameter types of a template
17917 template parameter might depend on prior template parameters
17918 deduced after the template template parameter. The following
17919 ill-formed example illustrates this issue:
17920
17921 template<typename T, template<T> class C> void f(C<5>, T);
17922
17923 template<int N> struct X {};
17924
17925 void g() {
17926 f(X<5>(), 5l); // error: template argument deduction fails
17927 }
17928
17929 The template parameter list of 'C' depends on the template type
17930 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17931 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17932 time that we deduce 'C'. */
17933 if (!template_template_parm_bindings_ok_p
17934 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
17935 {
17936 unify_inconsistent_template_template_parameters (explain_p);
17937 goto fail;
17938 }
17939
17940 /* All is well so far. Now, check:
17941
17942 [temp.deduct]
17943
17944 When all template arguments have been deduced, all uses of
17945 template parameters in nondeduced contexts are replaced with
17946 the corresponding deduced argument values. If the
17947 substitution results in an invalid type, as described above,
17948 type deduction fails. */
17949 TREE_VALUE (tinst) = targs;
17950 if (!push_tinst_level (tinst))
17951 {
17952 excessive_deduction_depth = true;
17953 goto fail;
17954 }
17955
17956 /* Also collect access checks from the instantiation. */
17957 reopen_deferring_access_checks (checks);
17958
17959 decl = instantiate_template (fn, targs, complain);
17960
17961 checks = get_deferred_access_checks ();
17962 pop_deferring_access_checks ();
17963
17964 pop_tinst_level ();
17965
17966 if (decl == error_mark_node)
17967 goto fail;
17968
17969 /* Now perform any access checks encountered during substitution. */
17970 push_access_scope (decl);
17971 ok = perform_access_checks (checks, complain);
17972 pop_access_scope (decl);
17973 if (!ok)
17974 goto fail;
17975
17976 /* If we're looking for an exact match, check that what we got
17977 is indeed an exact match. It might not be if some template
17978 parameters are used in non-deduced contexts. But don't check
17979 for an exact match if we have dependent template arguments;
17980 in that case we're doing partial ordering, and we already know
17981 that we have two candidates that will provide the actual type. */
17982 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
17983 {
17984 tree substed = TREE_TYPE (decl);
17985 unsigned int i;
17986
17987 tree sarg
17988 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
17989 if (return_type)
17990 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
17991 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
17992 if (!same_type_p (args[i], TREE_VALUE (sarg)))
17993 {
17994 unify_type_mismatch (explain_p, args[i],
17995 TREE_VALUE (sarg));
17996 goto fail;
17997 }
17998 }
17999
18000 r = decl;
18001
18002 fail:
18003 --deduction_depth;
18004 if (excessive_deduction_depth)
18005 {
18006 if (deduction_depth == 0)
18007 /* Reset once we're all the way out. */
18008 excessive_deduction_depth = false;
18009 }
18010
18011 /* We can't free this if a pending_template entry or last_error_tinst_level
18012 is pointing at it. */
18013 if (last_pending_template == old_last_pend
18014 && last_error_tinst_level == old_error_tinst)
18015 ggc_free (tinst);
18016
18017 return r;
18018 }
18019
18020 /* Adjust types before performing type deduction, as described in
18021 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18022 sections are symmetric. PARM is the type of a function parameter
18023 or the return type of the conversion function. ARG is the type of
18024 the argument passed to the call, or the type of the value
18025 initialized with the result of the conversion function.
18026 ARG_EXPR is the original argument expression, which may be null. */
18027
18028 static int
18029 maybe_adjust_types_for_deduction (unification_kind_t strict,
18030 tree* parm,
18031 tree* arg,
18032 tree arg_expr)
18033 {
18034 int result = 0;
18035
18036 switch (strict)
18037 {
18038 case DEDUCE_CALL:
18039 break;
18040
18041 case DEDUCE_CONV:
18042 /* Swap PARM and ARG throughout the remainder of this
18043 function; the handling is precisely symmetric since PARM
18044 will initialize ARG rather than vice versa. */
18045 std::swap (parm, arg);
18046 break;
18047
18048 case DEDUCE_EXACT:
18049 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18050 too, but here handle it by stripping the reference from PARM
18051 rather than by adding it to ARG. */
18052 if (TREE_CODE (*parm) == REFERENCE_TYPE
18053 && TYPE_REF_IS_RVALUE (*parm)
18054 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18055 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18056 && TREE_CODE (*arg) == REFERENCE_TYPE
18057 && !TYPE_REF_IS_RVALUE (*arg))
18058 *parm = TREE_TYPE (*parm);
18059 /* Nothing else to do in this case. */
18060 return 0;
18061
18062 default:
18063 gcc_unreachable ();
18064 }
18065
18066 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18067 {
18068 /* [temp.deduct.call]
18069
18070 If P is not a reference type:
18071
18072 --If A is an array type, the pointer type produced by the
18073 array-to-pointer standard conversion (_conv.array_) is
18074 used in place of A for type deduction; otherwise,
18075
18076 --If A is a function type, the pointer type produced by
18077 the function-to-pointer standard conversion
18078 (_conv.func_) is used in place of A for type deduction;
18079 otherwise,
18080
18081 --If A is a cv-qualified type, the top level
18082 cv-qualifiers of A's type are ignored for type
18083 deduction. */
18084 if (TREE_CODE (*arg) == ARRAY_TYPE)
18085 *arg = build_pointer_type (TREE_TYPE (*arg));
18086 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18087 *arg = build_pointer_type (*arg);
18088 else
18089 *arg = TYPE_MAIN_VARIANT (*arg);
18090 }
18091
18092 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
18093 of the form T&&, where T is a template parameter, and the argument
18094 is an lvalue, T is deduced as A& */
18095 if (TREE_CODE (*parm) == REFERENCE_TYPE
18096 && TYPE_REF_IS_RVALUE (*parm)
18097 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18098 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18099 && (arg_expr ? lvalue_p (arg_expr)
18100 /* try_one_overload doesn't provide an arg_expr, but
18101 functions are always lvalues. */
18102 : TREE_CODE (*arg) == FUNCTION_TYPE))
18103 *arg = build_reference_type (*arg);
18104
18105 /* [temp.deduct.call]
18106
18107 If P is a cv-qualified type, the top level cv-qualifiers
18108 of P's type are ignored for type deduction. If P is a
18109 reference type, the type referred to by P is used for
18110 type deduction. */
18111 *parm = TYPE_MAIN_VARIANT (*parm);
18112 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18113 {
18114 *parm = TREE_TYPE (*parm);
18115 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18116 }
18117
18118 /* DR 322. For conversion deduction, remove a reference type on parm
18119 too (which has been swapped into ARG). */
18120 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18121 *arg = TREE_TYPE (*arg);
18122
18123 return result;
18124 }
18125
18126 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18127 template which does contain any deducible template parameters; check if
18128 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18129 unify_one_argument. */
18130
18131 static int
18132 check_non_deducible_conversion (tree parm, tree arg, int strict,
18133 int flags, bool explain_p)
18134 {
18135 tree type;
18136
18137 if (!TYPE_P (arg))
18138 type = TREE_TYPE (arg);
18139 else
18140 type = arg;
18141
18142 if (same_type_p (parm, type))
18143 return unify_success (explain_p);
18144
18145 if (strict == DEDUCE_CONV)
18146 {
18147 if (can_convert_arg (type, parm, NULL_TREE, flags,
18148 explain_p ? tf_warning_or_error : tf_none))
18149 return unify_success (explain_p);
18150 }
18151 else if (strict != DEDUCE_EXACT)
18152 {
18153 if (can_convert_arg (parm, type,
18154 TYPE_P (arg) ? NULL_TREE : arg,
18155 flags, explain_p ? tf_warning_or_error : tf_none))
18156 return unify_success (explain_p);
18157 }
18158
18159 if (strict == DEDUCE_EXACT)
18160 return unify_type_mismatch (explain_p, parm, arg);
18161 else
18162 return unify_arg_conversion (explain_p, parm, type, arg);
18163 }
18164
18165 static bool uses_deducible_template_parms (tree type);
18166
18167 /* Returns true iff the expression EXPR is one from which a template
18168 argument can be deduced. In other words, if it's an undecorated
18169 use of a template non-type parameter. */
18170
18171 static bool
18172 deducible_expression (tree expr)
18173 {
18174 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18175 }
18176
18177 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18178 deducible way; that is, if it has a max value of <PARM> - 1. */
18179
18180 static bool
18181 deducible_array_bound (tree domain)
18182 {
18183 if (domain == NULL_TREE)
18184 return false;
18185
18186 tree max = TYPE_MAX_VALUE (domain);
18187 if (TREE_CODE (max) != MINUS_EXPR)
18188 return false;
18189
18190 return deducible_expression (TREE_OPERAND (max, 0));
18191 }
18192
18193 /* Returns true iff the template arguments ARGS use a template parameter
18194 in a deducible way. */
18195
18196 static bool
18197 deducible_template_args (tree args)
18198 {
18199 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18200 {
18201 bool deducible;
18202 tree elt = TREE_VEC_ELT (args, i);
18203 if (ARGUMENT_PACK_P (elt))
18204 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18205 else
18206 {
18207 if (PACK_EXPANSION_P (elt))
18208 elt = PACK_EXPANSION_PATTERN (elt);
18209 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18210 deducible = true;
18211 else if (TYPE_P (elt))
18212 deducible = uses_deducible_template_parms (elt);
18213 else
18214 deducible = deducible_expression (elt);
18215 }
18216 if (deducible)
18217 return true;
18218 }
18219 return false;
18220 }
18221
18222 /* Returns true iff TYPE contains any deducible references to template
18223 parameters, as per 14.8.2.5. */
18224
18225 static bool
18226 uses_deducible_template_parms (tree type)
18227 {
18228 if (PACK_EXPANSION_P (type))
18229 type = PACK_EXPANSION_PATTERN (type);
18230
18231 /* T
18232 cv-list T
18233 TT<T>
18234 TT<i>
18235 TT<> */
18236 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18237 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18238 return true;
18239
18240 /* T*
18241 T&
18242 T&& */
18243 if (POINTER_TYPE_P (type))
18244 return uses_deducible_template_parms (TREE_TYPE (type));
18245
18246 /* T[integer-constant ]
18247 type [i] */
18248 if (TREE_CODE (type) == ARRAY_TYPE)
18249 return (uses_deducible_template_parms (TREE_TYPE (type))
18250 || deducible_array_bound (TYPE_DOMAIN (type)));
18251
18252 /* T type ::*
18253 type T::*
18254 T T::*
18255 T (type ::*)()
18256 type (T::*)()
18257 type (type ::*)(T)
18258 type (T::*)(T)
18259 T (type ::*)(T)
18260 T (T::*)()
18261 T (T::*)(T) */
18262 if (TYPE_PTRMEM_P (type))
18263 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18264 || (uses_deducible_template_parms
18265 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18266
18267 /* template-name <T> (where template-name refers to a class template)
18268 template-name <i> (where template-name refers to a class template) */
18269 if (CLASS_TYPE_P (type)
18270 && CLASSTYPE_TEMPLATE_INFO (type)
18271 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18272 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18273 (CLASSTYPE_TI_ARGS (type)));
18274
18275 /* type (T)
18276 T()
18277 T(T) */
18278 if (TREE_CODE (type) == FUNCTION_TYPE
18279 || TREE_CODE (type) == METHOD_TYPE)
18280 {
18281 if (uses_deducible_template_parms (TREE_TYPE (type)))
18282 return true;
18283 tree parm = TYPE_ARG_TYPES (type);
18284 if (TREE_CODE (type) == METHOD_TYPE)
18285 parm = TREE_CHAIN (parm);
18286 for (; parm; parm = TREE_CHAIN (parm))
18287 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18288 return true;
18289 }
18290
18291 return false;
18292 }
18293
18294 /* Subroutine of type_unification_real and unify_pack_expansion to
18295 handle unification of a single P/A pair. Parameters are as
18296 for those functions. */
18297
18298 static int
18299 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18300 int subr, unification_kind_t strict,
18301 bool explain_p)
18302 {
18303 tree arg_expr = NULL_TREE;
18304 int arg_strict;
18305
18306 if (arg == error_mark_node || parm == error_mark_node)
18307 return unify_invalid (explain_p);
18308 if (arg == unknown_type_node)
18309 /* We can't deduce anything from this, but we might get all the
18310 template args from other function args. */
18311 return unify_success (explain_p);
18312
18313 /* Implicit conversions (Clause 4) will be performed on a function
18314 argument to convert it to the type of the corresponding function
18315 parameter if the parameter type contains no template-parameters that
18316 participate in template argument deduction. */
18317 if (strict != DEDUCE_EXACT
18318 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18319 /* For function parameters with no deducible template parameters,
18320 just return. We'll check non-dependent conversions later. */
18321 return unify_success (explain_p);
18322
18323 switch (strict)
18324 {
18325 case DEDUCE_CALL:
18326 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18327 | UNIFY_ALLOW_MORE_CV_QUAL
18328 | UNIFY_ALLOW_DERIVED);
18329 break;
18330
18331 case DEDUCE_CONV:
18332 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18333 break;
18334
18335 case DEDUCE_EXACT:
18336 arg_strict = UNIFY_ALLOW_NONE;
18337 break;
18338
18339 default:
18340 gcc_unreachable ();
18341 }
18342
18343 /* We only do these transformations if this is the top-level
18344 parameter_type_list in a call or declaration matching; in other
18345 situations (nested function declarators, template argument lists) we
18346 won't be comparing a type to an expression, and we don't do any type
18347 adjustments. */
18348 if (!subr)
18349 {
18350 if (!TYPE_P (arg))
18351 {
18352 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18353 if (type_unknown_p (arg))
18354 {
18355 /* [temp.deduct.type] A template-argument can be
18356 deduced from a pointer to function or pointer
18357 to member function argument if the set of
18358 overloaded functions does not contain function
18359 templates and at most one of a set of
18360 overloaded functions provides a unique
18361 match. */
18362
18363 if (resolve_overloaded_unification
18364 (tparms, targs, parm, arg, strict,
18365 arg_strict, explain_p))
18366 return unify_success (explain_p);
18367 return unify_overload_resolution_failure (explain_p, arg);
18368 }
18369
18370 arg_expr = arg;
18371 arg = unlowered_expr_type (arg);
18372 if (arg == error_mark_node)
18373 return unify_invalid (explain_p);
18374 }
18375
18376 arg_strict |=
18377 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18378 }
18379 else
18380 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18381 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18382 return unify_template_argument_mismatch (explain_p, parm, arg);
18383
18384 /* For deduction from an init-list we need the actual list. */
18385 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18386 arg = arg_expr;
18387 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18388 }
18389
18390 /* Most parms like fn_type_unification.
18391
18392 If SUBR is 1, we're being called recursively (to unify the
18393 arguments of a function or method parameter of a function
18394 template).
18395
18396 CHECKS is a pointer to a vector of access checks encountered while
18397 substituting default template arguments. */
18398
18399 static int
18400 type_unification_real (tree tparms,
18401 tree full_targs,
18402 tree xparms,
18403 const tree *xargs,
18404 unsigned int xnargs,
18405 int subr,
18406 unification_kind_t strict,
18407 int flags,
18408 vec<deferred_access_check, va_gc> **checks,
18409 bool explain_p)
18410 {
18411 tree parm, arg;
18412 int i;
18413 int ntparms = TREE_VEC_LENGTH (tparms);
18414 int saw_undeduced = 0;
18415 tree parms;
18416 const tree *args;
18417 unsigned int nargs;
18418 unsigned int ia;
18419
18420 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
18421 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
18422 gcc_assert (ntparms > 0);
18423
18424 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
18425
18426 /* Reset the number of non-defaulted template arguments contained
18427 in TARGS. */
18428 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
18429
18430 again:
18431 parms = xparms;
18432 args = xargs;
18433 nargs = xnargs;
18434
18435 ia = 0;
18436 while (parms && parms != void_list_node
18437 && ia < nargs)
18438 {
18439 parm = TREE_VALUE (parms);
18440
18441 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18442 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
18443 /* For a function parameter pack that occurs at the end of the
18444 parameter-declaration-list, the type A of each remaining
18445 argument of the call is compared with the type P of the
18446 declarator-id of the function parameter pack. */
18447 break;
18448
18449 parms = TREE_CHAIN (parms);
18450
18451 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18452 /* For a function parameter pack that does not occur at the
18453 end of the parameter-declaration-list, the type of the
18454 parameter pack is a non-deduced context. */
18455 continue;
18456
18457 arg = args[ia];
18458 ++ia;
18459
18460 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
18461 explain_p))
18462 return 1;
18463 }
18464
18465 if (parms
18466 && parms != void_list_node
18467 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
18468 {
18469 /* Unify the remaining arguments with the pack expansion type. */
18470 tree argvec;
18471 tree parmvec = make_tree_vec (1);
18472
18473 /* Allocate a TREE_VEC and copy in all of the arguments */
18474 argvec = make_tree_vec (nargs - ia);
18475 for (i = 0; ia < nargs; ++ia, ++i)
18476 TREE_VEC_ELT (argvec, i) = args[ia];
18477
18478 /* Copy the parameter into parmvec. */
18479 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
18480 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
18481 /*subr=*/subr, explain_p))
18482 return 1;
18483
18484 /* Advance to the end of the list of parameters. */
18485 parms = TREE_CHAIN (parms);
18486 }
18487
18488 /* Fail if we've reached the end of the parm list, and more args
18489 are present, and the parm list isn't variadic. */
18490 if (ia < nargs && parms == void_list_node)
18491 return unify_too_many_arguments (explain_p, nargs, ia);
18492 /* Fail if parms are left and they don't have default values and
18493 they aren't all deduced as empty packs (c++/57397). This is
18494 consistent with sufficient_parms_p. */
18495 if (parms && parms != void_list_node
18496 && TREE_PURPOSE (parms) == NULL_TREE)
18497 {
18498 unsigned int count = nargs;
18499 tree p = parms;
18500 bool type_pack_p;
18501 do
18502 {
18503 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
18504 if (!type_pack_p)
18505 count++;
18506 p = TREE_CHAIN (p);
18507 }
18508 while (p && p != void_list_node);
18509 if (count != nargs)
18510 return unify_too_few_arguments (explain_p, ia, count,
18511 type_pack_p);
18512 }
18513
18514 if (!subr)
18515 {
18516 tsubst_flags_t complain = (explain_p
18517 ? tf_warning_or_error
18518 : tf_none);
18519
18520 for (i = 0; i < ntparms; i++)
18521 {
18522 tree targ = TREE_VEC_ELT (targs, i);
18523 tree tparm = TREE_VEC_ELT (tparms, i);
18524
18525 /* Clear the "incomplete" flags on all argument packs now so that
18526 substituting them into later default arguments works. */
18527 if (targ && ARGUMENT_PACK_P (targ))
18528 {
18529 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
18530 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
18531 }
18532
18533 if (targ || tparm == error_mark_node)
18534 continue;
18535 tparm = TREE_VALUE (tparm);
18536
18537 /* If this is an undeduced nontype parameter that depends on
18538 a type parameter, try another pass; its type may have been
18539 deduced from a later argument than the one from which
18540 this parameter can be deduced. */
18541 if (TREE_CODE (tparm) == PARM_DECL
18542 && uses_template_parms (TREE_TYPE (tparm))
18543 && saw_undeduced < 2)
18544 {
18545 saw_undeduced = 1;
18546 continue;
18547 }
18548
18549 /* Core issue #226 (C++0x) [temp.deduct]:
18550
18551 If a template argument has not been deduced, its
18552 default template argument, if any, is used.
18553
18554 When we are in C++98 mode, TREE_PURPOSE will either
18555 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18556 to explicitly check cxx_dialect here. */
18557 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
18558 /* OK, there is a default argument. Wait until after the
18559 conversion check to do substitution. */
18560 continue;
18561
18562 /* If the type parameter is a parameter pack, then it will
18563 be deduced to an empty parameter pack. */
18564 if (template_parameter_pack_p (tparm))
18565 {
18566 tree arg;
18567
18568 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
18569 {
18570 arg = make_node (NONTYPE_ARGUMENT_PACK);
18571 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
18572 TREE_CONSTANT (arg) = 1;
18573 }
18574 else
18575 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
18576
18577 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
18578
18579 TREE_VEC_ELT (targs, i) = arg;
18580 continue;
18581 }
18582
18583 return unify_parameter_deduction_failure (explain_p, tparm);
18584 }
18585
18586 /* DR 1391: All parameters have args, now check non-dependent parms for
18587 convertibility. */
18588 if (saw_undeduced < 2)
18589 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
18590 parms && parms != void_list_node && ia < nargs; )
18591 {
18592 parm = TREE_VALUE (parms);
18593
18594 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18595 && (!TREE_CHAIN (parms)
18596 || TREE_CHAIN (parms) == void_list_node))
18597 /* For a function parameter pack that occurs at the end of the
18598 parameter-declaration-list, the type A of each remaining
18599 argument of the call is compared with the type P of the
18600 declarator-id of the function parameter pack. */
18601 break;
18602
18603 parms = TREE_CHAIN (parms);
18604
18605 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18606 /* For a function parameter pack that does not occur at the
18607 end of the parameter-declaration-list, the type of the
18608 parameter pack is a non-deduced context. */
18609 continue;
18610
18611 arg = args[ia];
18612 ++ia;
18613
18614 if (uses_template_parms (parm))
18615 continue;
18616 if (check_non_deducible_conversion (parm, arg, strict, flags,
18617 explain_p))
18618 return 1;
18619 }
18620
18621 /* Now substitute into the default template arguments. */
18622 for (i = 0; i < ntparms; i++)
18623 {
18624 tree targ = TREE_VEC_ELT (targs, i);
18625 tree tparm = TREE_VEC_ELT (tparms, i);
18626
18627 if (targ || tparm == error_mark_node)
18628 continue;
18629 tree parm = TREE_VALUE (tparm);
18630
18631 if (TREE_CODE (parm) == PARM_DECL
18632 && uses_template_parms (TREE_TYPE (parm))
18633 && saw_undeduced < 2)
18634 continue;
18635
18636 tree arg = TREE_PURPOSE (tparm);
18637 reopen_deferring_access_checks (*checks);
18638 location_t save_loc = input_location;
18639 if (DECL_P (parm))
18640 input_location = DECL_SOURCE_LOCATION (parm);
18641 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
18642 arg = convert_template_argument (parm, arg, full_targs, complain,
18643 i, NULL_TREE);
18644 input_location = save_loc;
18645 *checks = get_deferred_access_checks ();
18646 pop_deferring_access_checks ();
18647 if (arg == error_mark_node)
18648 return 1;
18649 else
18650 {
18651 TREE_VEC_ELT (targs, i) = arg;
18652 /* The position of the first default template argument,
18653 is also the number of non-defaulted arguments in TARGS.
18654 Record that. */
18655 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18656 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
18657 continue;
18658 }
18659 }
18660
18661 if (saw_undeduced++ == 1)
18662 goto again;
18663 }
18664
18665 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18666 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
18667
18668 return unify_success (explain_p);
18669 }
18670
18671 /* Subroutine of type_unification_real. Args are like the variables
18672 at the call site. ARG is an overloaded function (or template-id);
18673 we try deducing template args from each of the overloads, and if
18674 only one succeeds, we go with that. Modifies TARGS and returns
18675 true on success. */
18676
18677 static bool
18678 resolve_overloaded_unification (tree tparms,
18679 tree targs,
18680 tree parm,
18681 tree arg,
18682 unification_kind_t strict,
18683 int sub_strict,
18684 bool explain_p)
18685 {
18686 tree tempargs = copy_node (targs);
18687 int good = 0;
18688 tree goodfn = NULL_TREE;
18689 bool addr_p;
18690
18691 if (TREE_CODE (arg) == ADDR_EXPR)
18692 {
18693 arg = TREE_OPERAND (arg, 0);
18694 addr_p = true;
18695 }
18696 else
18697 addr_p = false;
18698
18699 if (TREE_CODE (arg) == COMPONENT_REF)
18700 /* Handle `&x' where `x' is some static or non-static member
18701 function name. */
18702 arg = TREE_OPERAND (arg, 1);
18703
18704 if (TREE_CODE (arg) == OFFSET_REF)
18705 arg = TREE_OPERAND (arg, 1);
18706
18707 /* Strip baselink information. */
18708 if (BASELINK_P (arg))
18709 arg = BASELINK_FUNCTIONS (arg);
18710
18711 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
18712 {
18713 /* If we got some explicit template args, we need to plug them into
18714 the affected templates before we try to unify, in case the
18715 explicit args will completely resolve the templates in question. */
18716
18717 int ok = 0;
18718 tree expl_subargs = TREE_OPERAND (arg, 1);
18719 arg = TREE_OPERAND (arg, 0);
18720
18721 for (; arg; arg = OVL_NEXT (arg))
18722 {
18723 tree fn = OVL_CURRENT (arg);
18724 tree subargs, elem;
18725
18726 if (TREE_CODE (fn) != TEMPLATE_DECL)
18727 continue;
18728
18729 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18730 expl_subargs, NULL_TREE, tf_none,
18731 /*require_all_args=*/true,
18732 /*use_default_args=*/true);
18733 if (subargs != error_mark_node
18734 && !any_dependent_template_arguments_p (subargs))
18735 {
18736 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
18737 if (try_one_overload (tparms, targs, tempargs, parm,
18738 elem, strict, sub_strict, addr_p, explain_p)
18739 && (!goodfn || !same_type_p (goodfn, elem)))
18740 {
18741 goodfn = elem;
18742 ++good;
18743 }
18744 }
18745 else if (subargs)
18746 ++ok;
18747 }
18748 /* If no templates (or more than one) are fully resolved by the
18749 explicit arguments, this template-id is a non-deduced context; it
18750 could still be OK if we deduce all template arguments for the
18751 enclosing call through other arguments. */
18752 if (good != 1)
18753 good = ok;
18754 }
18755 else if (TREE_CODE (arg) != OVERLOAD
18756 && TREE_CODE (arg) != FUNCTION_DECL)
18757 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18758 -- but the deduction does not succeed because the expression is
18759 not just the function on its own. */
18760 return false;
18761 else
18762 for (; arg; arg = OVL_NEXT (arg))
18763 if (try_one_overload (tparms, targs, tempargs, parm,
18764 TREE_TYPE (OVL_CURRENT (arg)),
18765 strict, sub_strict, addr_p, explain_p)
18766 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
18767 {
18768 goodfn = OVL_CURRENT (arg);
18769 ++good;
18770 }
18771
18772 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18773 to function or pointer to member function argument if the set of
18774 overloaded functions does not contain function templates and at most
18775 one of a set of overloaded functions provides a unique match.
18776
18777 So if we found multiple possibilities, we return success but don't
18778 deduce anything. */
18779
18780 if (good == 1)
18781 {
18782 int i = TREE_VEC_LENGTH (targs);
18783 for (; i--; )
18784 if (TREE_VEC_ELT (tempargs, i))
18785 {
18786 tree old = TREE_VEC_ELT (targs, i);
18787 tree new_ = TREE_VEC_ELT (tempargs, i);
18788 if (new_ && old && ARGUMENT_PACK_P (old)
18789 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
18790 /* Don't forget explicit template arguments in a pack. */
18791 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
18792 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
18793 TREE_VEC_ELT (targs, i) = new_;
18794 }
18795 }
18796 if (good)
18797 return true;
18798
18799 return false;
18800 }
18801
18802 /* Core DR 115: In contexts where deduction is done and fails, or in
18803 contexts where deduction is not done, if a template argument list is
18804 specified and it, along with any default template arguments, identifies
18805 a single function template specialization, then the template-id is an
18806 lvalue for the function template specialization. */
18807
18808 tree
18809 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
18810 {
18811 tree expr, offset, baselink;
18812 bool addr;
18813
18814 if (!type_unknown_p (orig_expr))
18815 return orig_expr;
18816
18817 expr = orig_expr;
18818 addr = false;
18819 offset = NULL_TREE;
18820 baselink = NULL_TREE;
18821
18822 if (TREE_CODE (expr) == ADDR_EXPR)
18823 {
18824 expr = TREE_OPERAND (expr, 0);
18825 addr = true;
18826 }
18827 if (TREE_CODE (expr) == OFFSET_REF)
18828 {
18829 offset = expr;
18830 expr = TREE_OPERAND (expr, 1);
18831 }
18832 if (BASELINK_P (expr))
18833 {
18834 baselink = expr;
18835 expr = BASELINK_FUNCTIONS (expr);
18836 }
18837
18838 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
18839 {
18840 int good = 0;
18841 tree goodfn = NULL_TREE;
18842
18843 /* If we got some explicit template args, we need to plug them into
18844 the affected templates before we try to unify, in case the
18845 explicit args will completely resolve the templates in question. */
18846
18847 tree expl_subargs = TREE_OPERAND (expr, 1);
18848 tree arg = TREE_OPERAND (expr, 0);
18849 tree badfn = NULL_TREE;
18850 tree badargs = NULL_TREE;
18851
18852 for (; arg; arg = OVL_NEXT (arg))
18853 {
18854 tree fn = OVL_CURRENT (arg);
18855 tree subargs, elem;
18856
18857 if (TREE_CODE (fn) != TEMPLATE_DECL)
18858 continue;
18859
18860 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18861 expl_subargs, NULL_TREE, tf_none,
18862 /*require_all_args=*/true,
18863 /*use_default_args=*/true);
18864 if (subargs != error_mark_node
18865 && !any_dependent_template_arguments_p (subargs))
18866 {
18867 elem = instantiate_template (fn, subargs, tf_none);
18868 if (elem == error_mark_node)
18869 {
18870 badfn = fn;
18871 badargs = subargs;
18872 }
18873 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
18874 {
18875 goodfn = elem;
18876 ++good;
18877 }
18878 }
18879 }
18880 if (good == 1)
18881 {
18882 mark_used (goodfn);
18883 expr = goodfn;
18884 if (baselink)
18885 expr = build_baselink (BASELINK_BINFO (baselink),
18886 BASELINK_ACCESS_BINFO (baselink),
18887 expr, BASELINK_OPTYPE (baselink));
18888 if (offset)
18889 {
18890 tree base
18891 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
18892 expr = build_offset_ref (base, expr, addr, complain);
18893 }
18894 if (addr)
18895 expr = cp_build_addr_expr (expr, complain);
18896 return expr;
18897 }
18898 else if (good == 0 && badargs && (complain & tf_error))
18899 /* There were no good options and at least one bad one, so let the
18900 user know what the problem is. */
18901 instantiate_template (badfn, badargs, complain);
18902 }
18903 return orig_expr;
18904 }
18905
18906 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18907 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18908 different overloads deduce different arguments for a given parm.
18909 ADDR_P is true if the expression for which deduction is being
18910 performed was of the form "& fn" rather than simply "fn".
18911
18912 Returns 1 on success. */
18913
18914 static int
18915 try_one_overload (tree tparms,
18916 tree orig_targs,
18917 tree targs,
18918 tree parm,
18919 tree arg,
18920 unification_kind_t strict,
18921 int sub_strict,
18922 bool addr_p,
18923 bool explain_p)
18924 {
18925 int nargs;
18926 tree tempargs;
18927 int i;
18928
18929 if (arg == error_mark_node)
18930 return 0;
18931
18932 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18933 to function or pointer to member function argument if the set of
18934 overloaded functions does not contain function templates and at most
18935 one of a set of overloaded functions provides a unique match.
18936
18937 So if this is a template, just return success. */
18938
18939 if (uses_template_parms (arg))
18940 return 1;
18941
18942 if (TREE_CODE (arg) == METHOD_TYPE)
18943 arg = build_ptrmemfunc_type (build_pointer_type (arg));
18944 else if (addr_p)
18945 arg = build_pointer_type (arg);
18946
18947 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
18948
18949 /* We don't copy orig_targs for this because if we have already deduced
18950 some template args from previous args, unify would complain when we
18951 try to deduce a template parameter for the same argument, even though
18952 there isn't really a conflict. */
18953 nargs = TREE_VEC_LENGTH (targs);
18954 tempargs = make_tree_vec (nargs);
18955
18956 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
18957 return 0;
18958
18959 /* First make sure we didn't deduce anything that conflicts with
18960 explicitly specified args. */
18961 for (i = nargs; i--; )
18962 {
18963 tree elt = TREE_VEC_ELT (tempargs, i);
18964 tree oldelt = TREE_VEC_ELT (orig_targs, i);
18965
18966 if (!elt)
18967 /*NOP*/;
18968 else if (uses_template_parms (elt))
18969 /* Since we're unifying against ourselves, we will fill in
18970 template args used in the function parm list with our own
18971 template parms. Discard them. */
18972 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
18973 else if (oldelt && ARGUMENT_PACK_P (oldelt))
18974 {
18975 /* Check that the argument at each index of the deduced argument pack
18976 is equivalent to the corresponding explicitly specified argument.
18977 We may have deduced more arguments than were explicitly specified,
18978 and that's OK. */
18979 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
18980 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
18981 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
18982
18983 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
18984 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
18985
18986 if (TREE_VEC_LENGTH (deduced_pack)
18987 < TREE_VEC_LENGTH (explicit_pack))
18988 return 0;
18989
18990 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
18991 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
18992 TREE_VEC_ELT (deduced_pack, j)))
18993 return 0;
18994 }
18995 else if (oldelt && !template_args_equal (oldelt, elt))
18996 return 0;
18997 }
18998
18999 for (i = nargs; i--; )
19000 {
19001 tree elt = TREE_VEC_ELT (tempargs, i);
19002
19003 if (elt)
19004 TREE_VEC_ELT (targs, i) = elt;
19005 }
19006
19007 return 1;
19008 }
19009
19010 /* PARM is a template class (perhaps with unbound template
19011 parameters). ARG is a fully instantiated type. If ARG can be
19012 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19013 TARGS are as for unify. */
19014
19015 static tree
19016 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19017 bool explain_p)
19018 {
19019 tree copy_of_targs;
19020
19021 if (!CLASSTYPE_TEMPLATE_INFO (arg)
19022 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19023 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
19024 return NULL_TREE;
19025
19026 /* We need to make a new template argument vector for the call to
19027 unify. If we used TARGS, we'd clutter it up with the result of
19028 the attempted unification, even if this class didn't work out.
19029 We also don't want to commit ourselves to all the unifications
19030 we've already done, since unification is supposed to be done on
19031 an argument-by-argument basis. In other words, consider the
19032 following pathological case:
19033
19034 template <int I, int J, int K>
19035 struct S {};
19036
19037 template <int I, int J>
19038 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19039
19040 template <int I, int J, int K>
19041 void f(S<I, J, K>, S<I, I, I>);
19042
19043 void g() {
19044 S<0, 0, 0> s0;
19045 S<0, 1, 2> s2;
19046
19047 f(s0, s2);
19048 }
19049
19050 Now, by the time we consider the unification involving `s2', we
19051 already know that we must have `f<0, 0, 0>'. But, even though
19052 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19053 because there are two ways to unify base classes of S<0, 1, 2>
19054 with S<I, I, I>. If we kept the already deduced knowledge, we
19055 would reject the possibility I=1. */
19056 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19057
19058 /* If unification failed, we're done. */
19059 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19060 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19061 return NULL_TREE;
19062
19063 return arg;
19064 }
19065
19066 /* Given a template type PARM and a class type ARG, find the unique
19067 base type in ARG that is an instance of PARM. We do not examine
19068 ARG itself; only its base-classes. If there is not exactly one
19069 appropriate base class, return NULL_TREE. PARM may be the type of
19070 a partial specialization, as well as a plain template type. Used
19071 by unify. */
19072
19073 static enum template_base_result
19074 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19075 bool explain_p, tree *result)
19076 {
19077 tree rval = NULL_TREE;
19078 tree binfo;
19079
19080 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19081
19082 binfo = TYPE_BINFO (complete_type (arg));
19083 if (!binfo)
19084 {
19085 /* The type could not be completed. */
19086 *result = NULL_TREE;
19087 return tbr_incomplete_type;
19088 }
19089
19090 /* Walk in inheritance graph order. The search order is not
19091 important, and this avoids multiple walks of virtual bases. */
19092 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19093 {
19094 tree r = try_class_unification (tparms, targs, parm,
19095 BINFO_TYPE (binfo), explain_p);
19096
19097 if (r)
19098 {
19099 /* If there is more than one satisfactory baseclass, then:
19100
19101 [temp.deduct.call]
19102
19103 If they yield more than one possible deduced A, the type
19104 deduction fails.
19105
19106 applies. */
19107 if (rval && !same_type_p (r, rval))
19108 {
19109 *result = NULL_TREE;
19110 return tbr_ambiguous_baseclass;
19111 }
19112
19113 rval = r;
19114 }
19115 }
19116
19117 *result = rval;
19118 return tbr_success;
19119 }
19120
19121 /* Returns the level of DECL, which declares a template parameter. */
19122
19123 static int
19124 template_decl_level (tree decl)
19125 {
19126 switch (TREE_CODE (decl))
19127 {
19128 case TYPE_DECL:
19129 case TEMPLATE_DECL:
19130 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19131
19132 case PARM_DECL:
19133 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19134
19135 default:
19136 gcc_unreachable ();
19137 }
19138 return 0;
19139 }
19140
19141 /* Decide whether ARG can be unified with PARM, considering only the
19142 cv-qualifiers of each type, given STRICT as documented for unify.
19143 Returns nonzero iff the unification is OK on that basis. */
19144
19145 static int
19146 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19147 {
19148 int arg_quals = cp_type_quals (arg);
19149 int parm_quals = cp_type_quals (parm);
19150
19151 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19152 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19153 {
19154 /* Although a CVR qualifier is ignored when being applied to a
19155 substituted template parameter ([8.3.2]/1 for example), that
19156 does not allow us to unify "const T" with "int&" because both
19157 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19158 It is ok when we're allowing additional CV qualifiers
19159 at the outer level [14.8.2.1]/3,1st bullet. */
19160 if ((TREE_CODE (arg) == REFERENCE_TYPE
19161 || TREE_CODE (arg) == FUNCTION_TYPE
19162 || TREE_CODE (arg) == METHOD_TYPE)
19163 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19164 return 0;
19165
19166 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19167 && (parm_quals & TYPE_QUAL_RESTRICT))
19168 return 0;
19169 }
19170
19171 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19172 && (arg_quals & parm_quals) != parm_quals)
19173 return 0;
19174
19175 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19176 && (parm_quals & arg_quals) != arg_quals)
19177 return 0;
19178
19179 return 1;
19180 }
19181
19182 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19183 void
19184 template_parm_level_and_index (tree parm, int* level, int* index)
19185 {
19186 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19187 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19188 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19189 {
19190 *index = TEMPLATE_TYPE_IDX (parm);
19191 *level = TEMPLATE_TYPE_LEVEL (parm);
19192 }
19193 else
19194 {
19195 *index = TEMPLATE_PARM_IDX (parm);
19196 *level = TEMPLATE_PARM_LEVEL (parm);
19197 }
19198 }
19199
19200 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19201 do { \
19202 if (unify (TP, TA, P, A, S, EP)) \
19203 return 1; \
19204 } while (0);
19205
19206 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19207 expansion at the end of PACKED_PARMS. Returns 0 if the type
19208 deduction succeeds, 1 otherwise. STRICT is the same as in
19209 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
19210 call argument list. We'll need to adjust the arguments to make them
19211 types. SUBR tells us if this is from a recursive call to
19212 type_unification_real, or for comparing two template argument
19213 lists. */
19214
19215 static int
19216 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19217 tree packed_args, unification_kind_t strict,
19218 bool subr, bool explain_p)
19219 {
19220 tree parm
19221 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19222 tree pattern = PACK_EXPANSION_PATTERN (parm);
19223 tree pack, packs = NULL_TREE;
19224 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19225
19226 packed_args = expand_template_argument_pack (packed_args);
19227
19228 int len = TREE_VEC_LENGTH (packed_args);
19229
19230 /* Determine the parameter packs we will be deducing from the
19231 pattern, and record their current deductions. */
19232 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19233 pack; pack = TREE_CHAIN (pack))
19234 {
19235 tree parm_pack = TREE_VALUE (pack);
19236 int idx, level;
19237
19238 /* Determine the index and level of this parameter pack. */
19239 template_parm_level_and_index (parm_pack, &level, &idx);
19240
19241 /* Keep track of the parameter packs and their corresponding
19242 argument packs. */
19243 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19244 TREE_TYPE (packs) = make_tree_vec (len - start);
19245 }
19246
19247 /* Loop through all of the arguments that have not yet been
19248 unified and unify each with the pattern. */
19249 for (i = start; i < len; i++)
19250 {
19251 tree parm;
19252 bool any_explicit = false;
19253 tree arg = TREE_VEC_ELT (packed_args, i);
19254
19255 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19256 or the element of its argument pack at the current index if
19257 this argument was explicitly specified. */
19258 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19259 {
19260 int idx, level;
19261 tree arg, pargs;
19262 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19263
19264 arg = NULL_TREE;
19265 if (TREE_VALUE (pack)
19266 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19267 && (i - start < TREE_VEC_LENGTH (pargs)))
19268 {
19269 any_explicit = true;
19270 arg = TREE_VEC_ELT (pargs, i - start);
19271 }
19272 TMPL_ARG (targs, level, idx) = arg;
19273 }
19274
19275 /* If we had explicit template arguments, substitute them into the
19276 pattern before deduction. */
19277 if (any_explicit)
19278 {
19279 /* Some arguments might still be unspecified or dependent. */
19280 bool dependent;
19281 ++processing_template_decl;
19282 dependent = any_dependent_template_arguments_p (targs);
19283 if (!dependent)
19284 --processing_template_decl;
19285 parm = tsubst (pattern, targs,
19286 explain_p ? tf_warning_or_error : tf_none,
19287 NULL_TREE);
19288 if (dependent)
19289 --processing_template_decl;
19290 if (parm == error_mark_node)
19291 return 1;
19292 }
19293 else
19294 parm = pattern;
19295
19296 /* Unify the pattern with the current argument. */
19297 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19298 explain_p))
19299 return 1;
19300
19301 /* For each parameter pack, collect the deduced value. */
19302 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19303 {
19304 int idx, level;
19305 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19306
19307 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19308 TMPL_ARG (targs, level, idx);
19309 }
19310 }
19311
19312 /* Verify that the results of unification with the parameter packs
19313 produce results consistent with what we've seen before, and make
19314 the deduced argument packs available. */
19315 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19316 {
19317 tree old_pack = TREE_VALUE (pack);
19318 tree new_args = TREE_TYPE (pack);
19319 int i, len = TREE_VEC_LENGTH (new_args);
19320 int idx, level;
19321 bool nondeduced_p = false;
19322
19323 /* By default keep the original deduced argument pack.
19324 If necessary, more specific code is going to update the
19325 resulting deduced argument later down in this function. */
19326 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19327 TMPL_ARG (targs, level, idx) = old_pack;
19328
19329 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19330 actually deduce anything. */
19331 for (i = 0; i < len && !nondeduced_p; ++i)
19332 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
19333 nondeduced_p = true;
19334 if (nondeduced_p)
19335 continue;
19336
19337 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
19338 {
19339 /* If we had fewer function args than explicit template args,
19340 just use the explicits. */
19341 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19342 int explicit_len = TREE_VEC_LENGTH (explicit_args);
19343 if (len < explicit_len)
19344 new_args = explicit_args;
19345 }
19346
19347 if (!old_pack)
19348 {
19349 tree result;
19350 /* Build the deduced *_ARGUMENT_PACK. */
19351 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
19352 {
19353 result = make_node (NONTYPE_ARGUMENT_PACK);
19354 TREE_TYPE (result) =
19355 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
19356 TREE_CONSTANT (result) = 1;
19357 }
19358 else
19359 result = cxx_make_type (TYPE_ARGUMENT_PACK);
19360
19361 SET_ARGUMENT_PACK_ARGS (result, new_args);
19362
19363 /* Note the deduced argument packs for this parameter
19364 pack. */
19365 TMPL_ARG (targs, level, idx) = result;
19366 }
19367 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
19368 && (ARGUMENT_PACK_ARGS (old_pack)
19369 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
19370 {
19371 /* We only had the explicitly-provided arguments before, but
19372 now we have a complete set of arguments. */
19373 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19374
19375 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
19376 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
19377 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
19378 }
19379 else
19380 {
19381 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
19382 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
19383
19384 if (!comp_template_args (old_args, new_args,
19385 &bad_old_arg, &bad_new_arg))
19386 /* Inconsistent unification of this parameter pack. */
19387 return unify_parameter_pack_inconsistent (explain_p,
19388 bad_old_arg,
19389 bad_new_arg);
19390 }
19391 }
19392
19393 return unify_success (explain_p);
19394 }
19395
19396 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19397 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19398 parameters and return value are as for unify. */
19399
19400 static int
19401 unify_array_domain (tree tparms, tree targs,
19402 tree parm_dom, tree arg_dom,
19403 bool explain_p)
19404 {
19405 tree parm_max;
19406 tree arg_max;
19407 bool parm_cst;
19408 bool arg_cst;
19409
19410 /* Our representation of array types uses "N - 1" as the
19411 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19412 not an integer constant. We cannot unify arbitrarily
19413 complex expressions, so we eliminate the MINUS_EXPRs
19414 here. */
19415 parm_max = TYPE_MAX_VALUE (parm_dom);
19416 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
19417 if (!parm_cst)
19418 {
19419 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
19420 parm_max = TREE_OPERAND (parm_max, 0);
19421 }
19422 arg_max = TYPE_MAX_VALUE (arg_dom);
19423 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
19424 if (!arg_cst)
19425 {
19426 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19427 trying to unify the type of a variable with the type
19428 of a template parameter. For example:
19429
19430 template <unsigned int N>
19431 void f (char (&) [N]);
19432 int g();
19433 void h(int i) {
19434 char a[g(i)];
19435 f(a);
19436 }
19437
19438 Here, the type of the ARG will be "int [g(i)]", and
19439 may be a SAVE_EXPR, etc. */
19440 if (TREE_CODE (arg_max) != MINUS_EXPR)
19441 return unify_vla_arg (explain_p, arg_dom);
19442 arg_max = TREE_OPERAND (arg_max, 0);
19443 }
19444
19445 /* If only one of the bounds used a MINUS_EXPR, compensate
19446 by adding one to the other bound. */
19447 if (parm_cst && !arg_cst)
19448 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
19449 integer_type_node,
19450 parm_max,
19451 integer_one_node);
19452 else if (arg_cst && !parm_cst)
19453 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
19454 integer_type_node,
19455 arg_max,
19456 integer_one_node);
19457
19458 return unify (tparms, targs, parm_max, arg_max,
19459 UNIFY_ALLOW_INTEGER, explain_p);
19460 }
19461
19462 /* Deduce the value of template parameters. TPARMS is the (innermost)
19463 set of template parameters to a template. TARGS is the bindings
19464 for those template parameters, as determined thus far; TARGS may
19465 include template arguments for outer levels of template parameters
19466 as well. PARM is a parameter to a template function, or a
19467 subcomponent of that parameter; ARG is the corresponding argument.
19468 This function attempts to match PARM with ARG in a manner
19469 consistent with the existing assignments in TARGS. If more values
19470 are deduced, then TARGS is updated.
19471
19472 Returns 0 if the type deduction succeeds, 1 otherwise. The
19473 parameter STRICT is a bitwise or of the following flags:
19474
19475 UNIFY_ALLOW_NONE:
19476 Require an exact match between PARM and ARG.
19477 UNIFY_ALLOW_MORE_CV_QUAL:
19478 Allow the deduced ARG to be more cv-qualified (by qualification
19479 conversion) than ARG.
19480 UNIFY_ALLOW_LESS_CV_QUAL:
19481 Allow the deduced ARG to be less cv-qualified than ARG.
19482 UNIFY_ALLOW_DERIVED:
19483 Allow the deduced ARG to be a template base class of ARG,
19484 or a pointer to a template base class of the type pointed to by
19485 ARG.
19486 UNIFY_ALLOW_INTEGER:
19487 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19488 case for more information.
19489 UNIFY_ALLOW_OUTER_LEVEL:
19490 This is the outermost level of a deduction. Used to determine validity
19491 of qualification conversions. A valid qualification conversion must
19492 have const qualified pointers leading up to the inner type which
19493 requires additional CV quals, except at the outer level, where const
19494 is not required [conv.qual]. It would be normal to set this flag in
19495 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19496 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19497 This is the outermost level of a deduction, and PARM can be more CV
19498 qualified at this point.
19499 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19500 This is the outermost level of a deduction, and PARM can be less CV
19501 qualified at this point. */
19502
19503 static int
19504 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
19505 bool explain_p)
19506 {
19507 int idx;
19508 tree targ;
19509 tree tparm;
19510 int strict_in = strict;
19511
19512 /* I don't think this will do the right thing with respect to types.
19513 But the only case I've seen it in so far has been array bounds, where
19514 signedness is the only information lost, and I think that will be
19515 okay. */
19516 while (TREE_CODE (parm) == NOP_EXPR)
19517 parm = TREE_OPERAND (parm, 0);
19518
19519 if (arg == error_mark_node)
19520 return unify_invalid (explain_p);
19521 if (arg == unknown_type_node
19522 || arg == init_list_type_node)
19523 /* We can't deduce anything from this, but we might get all the
19524 template args from other function args. */
19525 return unify_success (explain_p);
19526
19527 /* If PARM uses template parameters, then we can't bail out here,
19528 even if ARG == PARM, since we won't record unifications for the
19529 template parameters. We might need them if we're trying to
19530 figure out which of two things is more specialized. */
19531 if (arg == parm && !uses_template_parms (parm))
19532 return unify_success (explain_p);
19533
19534 /* Handle init lists early, so the rest of the function can assume
19535 we're dealing with a type. */
19536 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
19537 {
19538 tree elt, elttype;
19539 unsigned i;
19540 tree orig_parm = parm;
19541
19542 /* Replace T with std::initializer_list<T> for deduction. */
19543 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19544 && flag_deduce_init_list)
19545 parm = listify (parm);
19546
19547 if (!is_std_init_list (parm)
19548 && TREE_CODE (parm) != ARRAY_TYPE)
19549 /* We can only deduce from an initializer list argument if the
19550 parameter is std::initializer_list or an array; otherwise this
19551 is a non-deduced context. */
19552 return unify_success (explain_p);
19553
19554 if (TREE_CODE (parm) == ARRAY_TYPE)
19555 elttype = TREE_TYPE (parm);
19556 else
19557 {
19558 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
19559 /* Deduction is defined in terms of a single type, so just punt
19560 on the (bizarre) std::initializer_list<T...>. */
19561 if (PACK_EXPANSION_P (elttype))
19562 return unify_success (explain_p);
19563 }
19564
19565 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
19566 {
19567 int elt_strict = strict;
19568
19569 if (elt == error_mark_node)
19570 return unify_invalid (explain_p);
19571
19572 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
19573 {
19574 tree type = TREE_TYPE (elt);
19575 if (type == error_mark_node)
19576 return unify_invalid (explain_p);
19577 /* It should only be possible to get here for a call. */
19578 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
19579 elt_strict |= maybe_adjust_types_for_deduction
19580 (DEDUCE_CALL, &elttype, &type, elt);
19581 elt = type;
19582 }
19583
19584 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
19585 explain_p);
19586 }
19587
19588 if (TREE_CODE (parm) == ARRAY_TYPE
19589 && deducible_array_bound (TYPE_DOMAIN (parm)))
19590 {
19591 /* Also deduce from the length of the initializer list. */
19592 tree max = size_int (CONSTRUCTOR_NELTS (arg));
19593 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
19594 if (idx == error_mark_node)
19595 return unify_invalid (explain_p);
19596 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19597 idx, explain_p);
19598 }
19599
19600 /* If the std::initializer_list<T> deduction worked, replace the
19601 deduced A with std::initializer_list<A>. */
19602 if (orig_parm != parm)
19603 {
19604 idx = TEMPLATE_TYPE_IDX (orig_parm);
19605 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19606 targ = listify (targ);
19607 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
19608 }
19609 return unify_success (explain_p);
19610 }
19611
19612 /* Immediately reject some pairs that won't unify because of
19613 cv-qualification mismatches. */
19614 if (TREE_CODE (arg) == TREE_CODE (parm)
19615 && TYPE_P (arg)
19616 /* It is the elements of the array which hold the cv quals of an array
19617 type, and the elements might be template type parms. We'll check
19618 when we recurse. */
19619 && TREE_CODE (arg) != ARRAY_TYPE
19620 /* We check the cv-qualifiers when unifying with template type
19621 parameters below. We want to allow ARG `const T' to unify with
19622 PARM `T' for example, when computing which of two templates
19623 is more specialized, for example. */
19624 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
19625 && !check_cv_quals_for_unify (strict_in, arg, parm))
19626 return unify_cv_qual_mismatch (explain_p, parm, arg);
19627
19628 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
19629 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
19630 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
19631 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
19632 strict &= ~UNIFY_ALLOW_DERIVED;
19633 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19634 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
19635
19636 switch (TREE_CODE (parm))
19637 {
19638 case TYPENAME_TYPE:
19639 case SCOPE_REF:
19640 case UNBOUND_CLASS_TEMPLATE:
19641 /* In a type which contains a nested-name-specifier, template
19642 argument values cannot be deduced for template parameters used
19643 within the nested-name-specifier. */
19644 return unify_success (explain_p);
19645
19646 case TEMPLATE_TYPE_PARM:
19647 case TEMPLATE_TEMPLATE_PARM:
19648 case BOUND_TEMPLATE_TEMPLATE_PARM:
19649 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19650 if (error_operand_p (tparm))
19651 return unify_invalid (explain_p);
19652
19653 if (TEMPLATE_TYPE_LEVEL (parm)
19654 != template_decl_level (tparm))
19655 /* The PARM is not one we're trying to unify. Just check
19656 to see if it matches ARG. */
19657 {
19658 if (TREE_CODE (arg) == TREE_CODE (parm)
19659 && (is_auto (parm) ? is_auto (arg)
19660 : same_type_p (parm, arg)))
19661 return unify_success (explain_p);
19662 else
19663 return unify_type_mismatch (explain_p, parm, arg);
19664 }
19665 idx = TEMPLATE_TYPE_IDX (parm);
19666 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19667 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
19668 if (error_operand_p (tparm))
19669 return unify_invalid (explain_p);
19670
19671 /* Check for mixed types and values. */
19672 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19673 && TREE_CODE (tparm) != TYPE_DECL)
19674 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19675 && TREE_CODE (tparm) != TEMPLATE_DECL))
19676 gcc_unreachable ();
19677
19678 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19679 {
19680 /* ARG must be constructed from a template class or a template
19681 template parameter. */
19682 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
19683 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19684 return unify_template_deduction_failure (explain_p, parm, arg);
19685 {
19686 tree parmvec = TYPE_TI_ARGS (parm);
19687 /* An alias template name is never deduced. */
19688 if (TYPE_ALIAS_P (arg))
19689 arg = strip_typedefs (arg);
19690 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
19691 tree full_argvec = add_to_template_args (targs, argvec);
19692 tree parm_parms
19693 = DECL_INNERMOST_TEMPLATE_PARMS
19694 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
19695 int i, len;
19696 int parm_variadic_p = 0;
19697
19698 /* The resolution to DR150 makes clear that default
19699 arguments for an N-argument may not be used to bind T
19700 to a template template parameter with fewer than N
19701 parameters. It is not safe to permit the binding of
19702 default arguments as an extension, as that may change
19703 the meaning of a conforming program. Consider:
19704
19705 struct Dense { static const unsigned int dim = 1; };
19706
19707 template <template <typename> class View,
19708 typename Block>
19709 void operator+(float, View<Block> const&);
19710
19711 template <typename Block,
19712 unsigned int Dim = Block::dim>
19713 struct Lvalue_proxy { operator float() const; };
19714
19715 void
19716 test_1d (void) {
19717 Lvalue_proxy<Dense> p;
19718 float b;
19719 b + p;
19720 }
19721
19722 Here, if Lvalue_proxy is permitted to bind to View, then
19723 the global operator+ will be used; if they are not, the
19724 Lvalue_proxy will be converted to float. */
19725 if (coerce_template_parms (parm_parms,
19726 full_argvec,
19727 TYPE_TI_TEMPLATE (parm),
19728 (explain_p
19729 ? tf_warning_or_error
19730 : tf_none),
19731 /*require_all_args=*/true,
19732 /*use_default_args=*/false)
19733 == error_mark_node)
19734 return 1;
19735
19736 /* Deduce arguments T, i from TT<T> or TT<i>.
19737 We check each element of PARMVEC and ARGVEC individually
19738 rather than the whole TREE_VEC since they can have
19739 different number of elements. */
19740
19741 parmvec = expand_template_argument_pack (parmvec);
19742 argvec = expand_template_argument_pack (argvec);
19743
19744 len = TREE_VEC_LENGTH (parmvec);
19745
19746 /* Check if the parameters end in a pack, making them
19747 variadic. */
19748 if (len > 0
19749 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
19750 parm_variadic_p = 1;
19751
19752 for (i = 0; i < len - parm_variadic_p; ++i)
19753 /* If the template argument list of P contains a pack
19754 expansion that is not the last template argument, the
19755 entire template argument list is a non-deduced
19756 context. */
19757 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
19758 return unify_success (explain_p);
19759
19760 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
19761 return unify_too_few_arguments (explain_p,
19762 TREE_VEC_LENGTH (argvec), len);
19763
19764 for (i = 0; i < len - parm_variadic_p; ++i)
19765 {
19766 RECUR_AND_CHECK_FAILURE (tparms, targs,
19767 TREE_VEC_ELT (parmvec, i),
19768 TREE_VEC_ELT (argvec, i),
19769 UNIFY_ALLOW_NONE, explain_p);
19770 }
19771
19772 if (parm_variadic_p
19773 && unify_pack_expansion (tparms, targs,
19774 parmvec, argvec,
19775 DEDUCE_EXACT,
19776 /*subr=*/true, explain_p))
19777 return 1;
19778 }
19779 arg = TYPE_TI_TEMPLATE (arg);
19780
19781 /* Fall through to deduce template name. */
19782 }
19783
19784 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19785 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19786 {
19787 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19788
19789 /* Simple cases: Value already set, does match or doesn't. */
19790 if (targ != NULL_TREE && template_args_equal (targ, arg))
19791 return unify_success (explain_p);
19792 else if (targ)
19793 return unify_inconsistency (explain_p, parm, targ, arg);
19794 }
19795 else
19796 {
19797 /* If PARM is `const T' and ARG is only `int', we don't have
19798 a match unless we are allowing additional qualification.
19799 If ARG is `const int' and PARM is just `T' that's OK;
19800 that binds `const int' to `T'. */
19801 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
19802 arg, parm))
19803 return unify_cv_qual_mismatch (explain_p, parm, arg);
19804
19805 /* Consider the case where ARG is `const volatile int' and
19806 PARM is `const T'. Then, T should be `volatile int'. */
19807 arg = cp_build_qualified_type_real
19808 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
19809 if (arg == error_mark_node)
19810 return unify_invalid (explain_p);
19811
19812 /* Simple cases: Value already set, does match or doesn't. */
19813 if (targ != NULL_TREE && same_type_p (targ, arg))
19814 return unify_success (explain_p);
19815 else if (targ)
19816 return unify_inconsistency (explain_p, parm, targ, arg);
19817
19818 /* Make sure that ARG is not a variable-sized array. (Note
19819 that were talking about variable-sized arrays (like
19820 `int[n]'), rather than arrays of unknown size (like
19821 `int[]').) We'll get very confused by such a type since
19822 the bound of the array is not constant, and therefore
19823 not mangleable. Besides, such types are not allowed in
19824 ISO C++, so we can do as we please here. We do allow
19825 them for 'auto' deduction, since that isn't ABI-exposed. */
19826 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
19827 return unify_vla_arg (explain_p, arg);
19828
19829 /* Strip typedefs as in convert_template_argument. */
19830 arg = canonicalize_type_argument (arg, tf_none);
19831 }
19832
19833 /* If ARG is a parameter pack or an expansion, we cannot unify
19834 against it unless PARM is also a parameter pack. */
19835 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19836 && !template_parameter_pack_p (parm))
19837 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19838
19839 /* If the argument deduction results is a METHOD_TYPE,
19840 then there is a problem.
19841 METHOD_TYPE doesn't map to any real C++ type the result of
19842 the deduction can not be of that type. */
19843 if (TREE_CODE (arg) == METHOD_TYPE)
19844 return unify_method_type_error (explain_p, arg);
19845
19846 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19847 return unify_success (explain_p);
19848
19849 case TEMPLATE_PARM_INDEX:
19850 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19851 if (error_operand_p (tparm))
19852 return unify_invalid (explain_p);
19853
19854 if (TEMPLATE_PARM_LEVEL (parm)
19855 != template_decl_level (tparm))
19856 {
19857 /* The PARM is not one we're trying to unify. Just check
19858 to see if it matches ARG. */
19859 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
19860 && cp_tree_equal (parm, arg));
19861 if (result)
19862 unify_expression_unequal (explain_p, parm, arg);
19863 return result;
19864 }
19865
19866 idx = TEMPLATE_PARM_IDX (parm);
19867 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19868
19869 if (targ)
19870 {
19871 int x = !cp_tree_equal (targ, arg);
19872 if (x)
19873 unify_inconsistency (explain_p, parm, targ, arg);
19874 return x;
19875 }
19876
19877 /* [temp.deduct.type] If, in the declaration of a function template
19878 with a non-type template-parameter, the non-type
19879 template-parameter is used in an expression in the function
19880 parameter-list and, if the corresponding template-argument is
19881 deduced, the template-argument type shall match the type of the
19882 template-parameter exactly, except that a template-argument
19883 deduced from an array bound may be of any integral type.
19884 The non-type parameter might use already deduced type parameters. */
19885 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
19886 if (!TREE_TYPE (arg))
19887 /* Template-parameter dependent expression. Just accept it for now.
19888 It will later be processed in convert_template_argument. */
19889 ;
19890 else if (same_type_p (TREE_TYPE (arg), tparm))
19891 /* OK */;
19892 else if ((strict & UNIFY_ALLOW_INTEGER)
19893 && CP_INTEGRAL_TYPE_P (tparm))
19894 /* Convert the ARG to the type of PARM; the deduced non-type
19895 template argument must exactly match the types of the
19896 corresponding parameter. */
19897 arg = fold (build_nop (tparm, arg));
19898 else if (uses_template_parms (tparm))
19899 /* We haven't deduced the type of this parameter yet. Try again
19900 later. */
19901 return unify_success (explain_p);
19902 else
19903 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
19904
19905 /* If ARG is a parameter pack or an expansion, we cannot unify
19906 against it unless PARM is also a parameter pack. */
19907 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19908 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
19909 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19910
19911 {
19912 bool removed_attr = false;
19913 arg = strip_typedefs_expr (arg, &removed_attr);
19914 }
19915 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19916 return unify_success (explain_p);
19917
19918 case PTRMEM_CST:
19919 {
19920 /* A pointer-to-member constant can be unified only with
19921 another constant. */
19922 if (TREE_CODE (arg) != PTRMEM_CST)
19923 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
19924
19925 /* Just unify the class member. It would be useless (and possibly
19926 wrong, depending on the strict flags) to unify also
19927 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19928 arg refer to the same variable, even if through different
19929 classes. For instance:
19930
19931 struct A { int x; };
19932 struct B : A { };
19933
19934 Unification of &A::x and &B::x must succeed. */
19935 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
19936 PTRMEM_CST_MEMBER (arg), strict, explain_p);
19937 }
19938
19939 case POINTER_TYPE:
19940 {
19941 if (!TYPE_PTR_P (arg))
19942 return unify_type_mismatch (explain_p, parm, arg);
19943
19944 /* [temp.deduct.call]
19945
19946 A can be another pointer or pointer to member type that can
19947 be converted to the deduced A via a qualification
19948 conversion (_conv.qual_).
19949
19950 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19951 This will allow for additional cv-qualification of the
19952 pointed-to types if appropriate. */
19953
19954 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
19955 /* The derived-to-base conversion only persists through one
19956 level of pointers. */
19957 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
19958
19959 return unify (tparms, targs, TREE_TYPE (parm),
19960 TREE_TYPE (arg), strict, explain_p);
19961 }
19962
19963 case REFERENCE_TYPE:
19964 if (TREE_CODE (arg) != REFERENCE_TYPE)
19965 return unify_type_mismatch (explain_p, parm, arg);
19966 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19967 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19968
19969 case ARRAY_TYPE:
19970 if (TREE_CODE (arg) != ARRAY_TYPE)
19971 return unify_type_mismatch (explain_p, parm, arg);
19972 if ((TYPE_DOMAIN (parm) == NULL_TREE)
19973 != (TYPE_DOMAIN (arg) == NULL_TREE))
19974 return unify_type_mismatch (explain_p, parm, arg);
19975 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19976 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19977 if (TYPE_DOMAIN (parm) != NULL_TREE)
19978 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19979 TYPE_DOMAIN (arg), explain_p);
19980 return unify_success (explain_p);
19981
19982 case REAL_TYPE:
19983 case COMPLEX_TYPE:
19984 case VECTOR_TYPE:
19985 case INTEGER_TYPE:
19986 case BOOLEAN_TYPE:
19987 case ENUMERAL_TYPE:
19988 case VOID_TYPE:
19989 case NULLPTR_TYPE:
19990 if (TREE_CODE (arg) != TREE_CODE (parm))
19991 return unify_type_mismatch (explain_p, parm, arg);
19992
19993 /* We have already checked cv-qualification at the top of the
19994 function. */
19995 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
19996 return unify_type_mismatch (explain_p, parm, arg);
19997
19998 /* As far as unification is concerned, this wins. Later checks
19999 will invalidate it if necessary. */
20000 return unify_success (explain_p);
20001
20002 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20003 /* Type INTEGER_CST can come from ordinary constant template args. */
20004 case INTEGER_CST:
20005 while (TREE_CODE (arg) == NOP_EXPR)
20006 arg = TREE_OPERAND (arg, 0);
20007
20008 if (TREE_CODE (arg) != INTEGER_CST)
20009 return unify_template_argument_mismatch (explain_p, parm, arg);
20010 return (tree_int_cst_equal (parm, arg)
20011 ? unify_success (explain_p)
20012 : unify_template_argument_mismatch (explain_p, parm, arg));
20013
20014 case TREE_VEC:
20015 {
20016 int i, len, argslen;
20017 int parm_variadic_p = 0;
20018
20019 if (TREE_CODE (arg) != TREE_VEC)
20020 return unify_template_argument_mismatch (explain_p, parm, arg);
20021
20022 len = TREE_VEC_LENGTH (parm);
20023 argslen = TREE_VEC_LENGTH (arg);
20024
20025 /* Check for pack expansions in the parameters. */
20026 for (i = 0; i < len; ++i)
20027 {
20028 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20029 {
20030 if (i == len - 1)
20031 /* We can unify against something with a trailing
20032 parameter pack. */
20033 parm_variadic_p = 1;
20034 else
20035 /* [temp.deduct.type]/9: If the template argument list of
20036 P contains a pack expansion that is not the last
20037 template argument, the entire template argument list
20038 is a non-deduced context. */
20039 return unify_success (explain_p);
20040 }
20041 }
20042
20043 /* If we don't have enough arguments to satisfy the parameters
20044 (not counting the pack expression at the end), or we have
20045 too many arguments for a parameter list that doesn't end in
20046 a pack expression, we can't unify. */
20047 if (parm_variadic_p
20048 ? argslen < len - parm_variadic_p
20049 : argslen != len)
20050 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20051
20052 /* Unify all of the parameters that precede the (optional)
20053 pack expression. */
20054 for (i = 0; i < len - parm_variadic_p; ++i)
20055 {
20056 RECUR_AND_CHECK_FAILURE (tparms, targs,
20057 TREE_VEC_ELT (parm, i),
20058 TREE_VEC_ELT (arg, i),
20059 UNIFY_ALLOW_NONE, explain_p);
20060 }
20061 if (parm_variadic_p)
20062 return unify_pack_expansion (tparms, targs, parm, arg,
20063 DEDUCE_EXACT,
20064 /*subr=*/true, explain_p);
20065 return unify_success (explain_p);
20066 }
20067
20068 case RECORD_TYPE:
20069 case UNION_TYPE:
20070 if (TREE_CODE (arg) != TREE_CODE (parm))
20071 return unify_type_mismatch (explain_p, parm, arg);
20072
20073 if (TYPE_PTRMEMFUNC_P (parm))
20074 {
20075 if (!TYPE_PTRMEMFUNC_P (arg))
20076 return unify_type_mismatch (explain_p, parm, arg);
20077
20078 return unify (tparms, targs,
20079 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20080 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20081 strict, explain_p);
20082 }
20083 else if (TYPE_PTRMEMFUNC_P (arg))
20084 return unify_type_mismatch (explain_p, parm, arg);
20085
20086 if (CLASSTYPE_TEMPLATE_INFO (parm))
20087 {
20088 tree t = NULL_TREE;
20089
20090 if (strict_in & UNIFY_ALLOW_DERIVED)
20091 {
20092 /* First, we try to unify the PARM and ARG directly. */
20093 t = try_class_unification (tparms, targs,
20094 parm, arg, explain_p);
20095
20096 if (!t)
20097 {
20098 /* Fallback to the special case allowed in
20099 [temp.deduct.call]:
20100
20101 If P is a class, and P has the form
20102 template-id, then A can be a derived class of
20103 the deduced A. Likewise, if P is a pointer to
20104 a class of the form template-id, A can be a
20105 pointer to a derived class pointed to by the
20106 deduced A. */
20107 enum template_base_result r;
20108 r = get_template_base (tparms, targs, parm, arg,
20109 explain_p, &t);
20110
20111 if (!t)
20112 {
20113 /* Don't give the derived diagnostic if we're
20114 already dealing with the same template. */
20115 bool same_template
20116 = (CLASSTYPE_TEMPLATE_INFO (arg)
20117 && (CLASSTYPE_TI_TEMPLATE (parm)
20118 == CLASSTYPE_TI_TEMPLATE (arg)));
20119 return unify_no_common_base (explain_p && !same_template,
20120 r, parm, arg);
20121 }
20122 }
20123 }
20124 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20125 && (CLASSTYPE_TI_TEMPLATE (parm)
20126 == CLASSTYPE_TI_TEMPLATE (arg)))
20127 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20128 Then, we should unify `int' and `U'. */
20129 t = arg;
20130 else
20131 /* There's no chance of unification succeeding. */
20132 return unify_type_mismatch (explain_p, parm, arg);
20133
20134 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20135 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20136 }
20137 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20138 return unify_type_mismatch (explain_p, parm, arg);
20139 return unify_success (explain_p);
20140
20141 case METHOD_TYPE:
20142 case FUNCTION_TYPE:
20143 {
20144 unsigned int nargs;
20145 tree *args;
20146 tree a;
20147 unsigned int i;
20148
20149 if (TREE_CODE (arg) != TREE_CODE (parm))
20150 return unify_type_mismatch (explain_p, parm, arg);
20151
20152 /* CV qualifications for methods can never be deduced, they must
20153 match exactly. We need to check them explicitly here,
20154 because type_unification_real treats them as any other
20155 cv-qualified parameter. */
20156 if (TREE_CODE (parm) == METHOD_TYPE
20157 && (!check_cv_quals_for_unify
20158 (UNIFY_ALLOW_NONE,
20159 class_of_this_parm (arg),
20160 class_of_this_parm (parm))))
20161 return unify_cv_qual_mismatch (explain_p, parm, arg);
20162
20163 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20164 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20165
20166 nargs = list_length (TYPE_ARG_TYPES (arg));
20167 args = XALLOCAVEC (tree, nargs);
20168 for (a = TYPE_ARG_TYPES (arg), i = 0;
20169 a != NULL_TREE && a != void_list_node;
20170 a = TREE_CHAIN (a), ++i)
20171 args[i] = TREE_VALUE (a);
20172 nargs = i;
20173
20174 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20175 args, nargs, 1, DEDUCE_EXACT,
20176 LOOKUP_NORMAL, NULL, explain_p);
20177 }
20178
20179 case OFFSET_TYPE:
20180 /* Unify a pointer to member with a pointer to member function, which
20181 deduces the type of the member as a function type. */
20182 if (TYPE_PTRMEMFUNC_P (arg))
20183 {
20184 /* Check top-level cv qualifiers */
20185 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20186 return unify_cv_qual_mismatch (explain_p, parm, arg);
20187
20188 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20189 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20190 UNIFY_ALLOW_NONE, explain_p);
20191
20192 /* Determine the type of the function we are unifying against. */
20193 tree fntype = static_fn_type (arg);
20194
20195 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20196 }
20197
20198 if (TREE_CODE (arg) != OFFSET_TYPE)
20199 return unify_type_mismatch (explain_p, parm, arg);
20200 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20201 TYPE_OFFSET_BASETYPE (arg),
20202 UNIFY_ALLOW_NONE, explain_p);
20203 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20204 strict, explain_p);
20205
20206 case CONST_DECL:
20207 if (DECL_TEMPLATE_PARM_P (parm))
20208 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20209 if (arg != scalar_constant_value (parm))
20210 return unify_template_argument_mismatch (explain_p, parm, arg);
20211 return unify_success (explain_p);
20212
20213 case FIELD_DECL:
20214 case TEMPLATE_DECL:
20215 /* Matched cases are handled by the ARG == PARM test above. */
20216 return unify_template_argument_mismatch (explain_p, parm, arg);
20217
20218 case VAR_DECL:
20219 /* We might get a variable as a non-type template argument in parm if the
20220 corresponding parameter is type-dependent. Make any necessary
20221 adjustments based on whether arg is a reference. */
20222 if (CONSTANT_CLASS_P (arg))
20223 parm = fold_non_dependent_expr (parm);
20224 else if (REFERENCE_REF_P (arg))
20225 {
20226 tree sub = TREE_OPERAND (arg, 0);
20227 STRIP_NOPS (sub);
20228 if (TREE_CODE (sub) == ADDR_EXPR)
20229 arg = TREE_OPERAND (sub, 0);
20230 }
20231 /* Now use the normal expression code to check whether they match. */
20232 goto expr;
20233
20234 case TYPE_ARGUMENT_PACK:
20235 case NONTYPE_ARGUMENT_PACK:
20236 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20237 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20238
20239 case TYPEOF_TYPE:
20240 case DECLTYPE_TYPE:
20241 case UNDERLYING_TYPE:
20242 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20243 or UNDERLYING_TYPE nodes. */
20244 return unify_success (explain_p);
20245
20246 case ERROR_MARK:
20247 /* Unification fails if we hit an error node. */
20248 return unify_invalid (explain_p);
20249
20250 case INDIRECT_REF:
20251 if (REFERENCE_REF_P (parm))
20252 {
20253 if (REFERENCE_REF_P (arg))
20254 arg = TREE_OPERAND (arg, 0);
20255 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20256 strict, explain_p);
20257 }
20258 /* FALLTHRU */
20259
20260 default:
20261 /* An unresolved overload is a nondeduced context. */
20262 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20263 return unify_success (explain_p);
20264 gcc_assert (EXPR_P (parm));
20265 expr:
20266 /* We must be looking at an expression. This can happen with
20267 something like:
20268
20269 template <int I>
20270 void foo(S<I>, S<I + 2>);
20271
20272 This is a "nondeduced context":
20273
20274 [deduct.type]
20275
20276 The nondeduced contexts are:
20277
20278 --A type that is a template-id in which one or more of
20279 the template-arguments is an expression that references
20280 a template-parameter.
20281
20282 In these cases, we assume deduction succeeded, but don't
20283 actually infer any unifications. */
20284
20285 if (!uses_template_parms (parm)
20286 && !template_args_equal (parm, arg))
20287 return unify_expression_unequal (explain_p, parm, arg);
20288 else
20289 return unify_success (explain_p);
20290 }
20291 }
20292 #undef RECUR_AND_CHECK_FAILURE
20293 \f
20294 /* Note that DECL can be defined in this translation unit, if
20295 required. */
20296
20297 static void
20298 mark_definable (tree decl)
20299 {
20300 tree clone;
20301 DECL_NOT_REALLY_EXTERN (decl) = 1;
20302 FOR_EACH_CLONE (clone, decl)
20303 DECL_NOT_REALLY_EXTERN (clone) = 1;
20304 }
20305
20306 /* Called if RESULT is explicitly instantiated, or is a member of an
20307 explicitly instantiated class. */
20308
20309 void
20310 mark_decl_instantiated (tree result, int extern_p)
20311 {
20312 SET_DECL_EXPLICIT_INSTANTIATION (result);
20313
20314 /* If this entity has already been written out, it's too late to
20315 make any modifications. */
20316 if (TREE_ASM_WRITTEN (result))
20317 return;
20318
20319 /* For anonymous namespace we don't need to do anything. */
20320 if (decl_anon_ns_mem_p (result))
20321 {
20322 gcc_assert (!TREE_PUBLIC (result));
20323 return;
20324 }
20325
20326 if (TREE_CODE (result) != FUNCTION_DECL)
20327 /* The TREE_PUBLIC flag for function declarations will have been
20328 set correctly by tsubst. */
20329 TREE_PUBLIC (result) = 1;
20330
20331 /* This might have been set by an earlier implicit instantiation. */
20332 DECL_COMDAT (result) = 0;
20333
20334 if (extern_p)
20335 DECL_NOT_REALLY_EXTERN (result) = 0;
20336 else
20337 {
20338 mark_definable (result);
20339 mark_needed (result);
20340 /* Always make artificials weak. */
20341 if (DECL_ARTIFICIAL (result) && flag_weak)
20342 comdat_linkage (result);
20343 /* For WIN32 we also want to put explicit instantiations in
20344 linkonce sections. */
20345 else if (TREE_PUBLIC (result))
20346 maybe_make_one_only (result);
20347 }
20348
20349 /* If EXTERN_P, then this function will not be emitted -- unless
20350 followed by an explicit instantiation, at which point its linkage
20351 will be adjusted. If !EXTERN_P, then this function will be
20352 emitted here. In neither circumstance do we want
20353 import_export_decl to adjust the linkage. */
20354 DECL_INTERFACE_KNOWN (result) = 1;
20355 }
20356
20357 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20358 important template arguments. If any are missing, we check whether
20359 they're important by using error_mark_node for substituting into any
20360 args that were used for partial ordering (the ones between ARGS and END)
20361 and seeing if it bubbles up. */
20362
20363 static bool
20364 check_undeduced_parms (tree targs, tree args, tree end)
20365 {
20366 bool found = false;
20367 int i;
20368 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
20369 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
20370 {
20371 found = true;
20372 TREE_VEC_ELT (targs, i) = error_mark_node;
20373 }
20374 if (found)
20375 {
20376 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
20377 if (substed == error_mark_node)
20378 return true;
20379 }
20380 return false;
20381 }
20382
20383 /* Given two function templates PAT1 and PAT2, return:
20384
20385 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20386 -1 if PAT2 is more specialized than PAT1.
20387 0 if neither is more specialized.
20388
20389 LEN indicates the number of parameters we should consider
20390 (defaulted parameters should not be considered).
20391
20392 The 1998 std underspecified function template partial ordering, and
20393 DR214 addresses the issue. We take pairs of arguments, one from
20394 each of the templates, and deduce them against each other. One of
20395 the templates will be more specialized if all the *other*
20396 template's arguments deduce against its arguments and at least one
20397 of its arguments *does* *not* deduce against the other template's
20398 corresponding argument. Deduction is done as for class templates.
20399 The arguments used in deduction have reference and top level cv
20400 qualifiers removed. Iff both arguments were originally reference
20401 types *and* deduction succeeds in both directions, an lvalue reference
20402 wins against an rvalue reference and otherwise the template
20403 with the more cv-qualified argument wins for that pairing (if
20404 neither is more cv-qualified, they both are equal). Unlike regular
20405 deduction, after all the arguments have been deduced in this way,
20406 we do *not* verify the deduced template argument values can be
20407 substituted into non-deduced contexts.
20408
20409 The logic can be a bit confusing here, because we look at deduce1 and
20410 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20411 can find template arguments for pat1 to make arg1 look like arg2, that
20412 means that arg2 is at least as specialized as arg1. */
20413
20414 int
20415 more_specialized_fn (tree pat1, tree pat2, int len)
20416 {
20417 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
20418 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
20419 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
20420 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
20421 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
20422 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
20423 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
20424 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
20425 tree origs1, origs2;
20426 bool lose1 = false;
20427 bool lose2 = false;
20428
20429 /* Remove the this parameter from non-static member functions. If
20430 one is a non-static member function and the other is not a static
20431 member function, remove the first parameter from that function
20432 also. This situation occurs for operator functions where we
20433 locate both a member function (with this pointer) and non-member
20434 operator (with explicit first operand). */
20435 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
20436 {
20437 len--; /* LEN is the number of significant arguments for DECL1 */
20438 args1 = TREE_CHAIN (args1);
20439 if (!DECL_STATIC_FUNCTION_P (decl2))
20440 args2 = TREE_CHAIN (args2);
20441 }
20442 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
20443 {
20444 args2 = TREE_CHAIN (args2);
20445 if (!DECL_STATIC_FUNCTION_P (decl1))
20446 {
20447 len--;
20448 args1 = TREE_CHAIN (args1);
20449 }
20450 }
20451
20452 /* If only one is a conversion operator, they are unordered. */
20453 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
20454 return 0;
20455
20456 /* Consider the return type for a conversion function */
20457 if (DECL_CONV_FN_P (decl1))
20458 {
20459 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
20460 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
20461 len++;
20462 }
20463
20464 processing_template_decl++;
20465
20466 origs1 = args1;
20467 origs2 = args2;
20468
20469 while (len--
20470 /* Stop when an ellipsis is seen. */
20471 && args1 != NULL_TREE && args2 != NULL_TREE)
20472 {
20473 tree arg1 = TREE_VALUE (args1);
20474 tree arg2 = TREE_VALUE (args2);
20475 int deduce1, deduce2;
20476 int quals1 = -1;
20477 int quals2 = -1;
20478 int ref1 = 0;
20479 int ref2 = 0;
20480
20481 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20482 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20483 {
20484 /* When both arguments are pack expansions, we need only
20485 unify the patterns themselves. */
20486 arg1 = PACK_EXPANSION_PATTERN (arg1);
20487 arg2 = PACK_EXPANSION_PATTERN (arg2);
20488
20489 /* This is the last comparison we need to do. */
20490 len = 0;
20491 }
20492
20493 if (TREE_CODE (arg1) == REFERENCE_TYPE)
20494 {
20495 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
20496 arg1 = TREE_TYPE (arg1);
20497 quals1 = cp_type_quals (arg1);
20498 }
20499
20500 if (TREE_CODE (arg2) == REFERENCE_TYPE)
20501 {
20502 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
20503 arg2 = TREE_TYPE (arg2);
20504 quals2 = cp_type_quals (arg2);
20505 }
20506
20507 arg1 = TYPE_MAIN_VARIANT (arg1);
20508 arg2 = TYPE_MAIN_VARIANT (arg2);
20509
20510 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
20511 {
20512 int i, len2 = remaining_arguments (args2);
20513 tree parmvec = make_tree_vec (1);
20514 tree argvec = make_tree_vec (len2);
20515 tree ta = args2;
20516
20517 /* Setup the parameter vector, which contains only ARG1. */
20518 TREE_VEC_ELT (parmvec, 0) = arg1;
20519
20520 /* Setup the argument vector, which contains the remaining
20521 arguments. */
20522 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
20523 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20524
20525 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
20526 argvec, DEDUCE_EXACT,
20527 /*subr=*/true, /*explain_p=*/false)
20528 == 0);
20529
20530 /* We cannot deduce in the other direction, because ARG1 is
20531 a pack expansion but ARG2 is not. */
20532 deduce2 = 0;
20533 }
20534 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20535 {
20536 int i, len1 = remaining_arguments (args1);
20537 tree parmvec = make_tree_vec (1);
20538 tree argvec = make_tree_vec (len1);
20539 tree ta = args1;
20540
20541 /* Setup the parameter vector, which contains only ARG1. */
20542 TREE_VEC_ELT (parmvec, 0) = arg2;
20543
20544 /* Setup the argument vector, which contains the remaining
20545 arguments. */
20546 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
20547 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20548
20549 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
20550 argvec, DEDUCE_EXACT,
20551 /*subr=*/true, /*explain_p=*/false)
20552 == 0);
20553
20554 /* We cannot deduce in the other direction, because ARG2 is
20555 a pack expansion but ARG1 is not.*/
20556 deduce1 = 0;
20557 }
20558
20559 else
20560 {
20561 /* The normal case, where neither argument is a pack
20562 expansion. */
20563 deduce1 = (unify (tparms1, targs1, arg1, arg2,
20564 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20565 == 0);
20566 deduce2 = (unify (tparms2, targs2, arg2, arg1,
20567 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20568 == 0);
20569 }
20570
20571 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20572 arg2, then arg2 is not as specialized as arg1. */
20573 if (!deduce1)
20574 lose2 = true;
20575 if (!deduce2)
20576 lose1 = true;
20577
20578 /* "If, for a given type, deduction succeeds in both directions
20579 (i.e., the types are identical after the transformations above)
20580 and both P and A were reference types (before being replaced with
20581 the type referred to above):
20582 - if the type from the argument template was an lvalue reference and
20583 the type from the parameter template was not, the argument type is
20584 considered to be more specialized than the other; otherwise,
20585 - if the type from the argument template is more cv-qualified
20586 than the type from the parameter template (as described above),
20587 the argument type is considered to be more specialized than the other;
20588 otherwise,
20589 - neither type is more specialized than the other." */
20590
20591 if (deduce1 && deduce2)
20592 {
20593 if (ref1 && ref2 && ref1 != ref2)
20594 {
20595 if (ref1 > ref2)
20596 lose1 = true;
20597 else
20598 lose2 = true;
20599 }
20600 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
20601 {
20602 if ((quals1 & quals2) == quals2)
20603 lose2 = true;
20604 if ((quals1 & quals2) == quals1)
20605 lose1 = true;
20606 }
20607 }
20608
20609 if (lose1 && lose2)
20610 /* We've failed to deduce something in either direction.
20611 These must be unordered. */
20612 break;
20613
20614 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20615 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20616 /* We have already processed all of the arguments in our
20617 handing of the pack expansion type. */
20618 len = 0;
20619
20620 args1 = TREE_CHAIN (args1);
20621 args2 = TREE_CHAIN (args2);
20622 }
20623
20624 /* "In most cases, all template parameters must have values in order for
20625 deduction to succeed, but for partial ordering purposes a template
20626 parameter may remain without a value provided it is not used in the
20627 types being used for partial ordering."
20628
20629 Thus, if we are missing any of the targs1 we need to substitute into
20630 origs1, then pat2 is not as specialized as pat1. This can happen when
20631 there is a nondeduced context. */
20632 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
20633 lose2 = true;
20634 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
20635 lose1 = true;
20636
20637 processing_template_decl--;
20638
20639 /* If both deductions succeed, the partial ordering selects the more
20640 constrained template. */
20641 if (!lose1 && !lose2)
20642 {
20643 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
20644 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
20645 lose1 = !subsumes_constraints (c1, c2);
20646 lose2 = !subsumes_constraints (c2, c1);
20647 }
20648
20649 /* All things being equal, if the next argument is a pack expansion
20650 for one function but not for the other, prefer the
20651 non-variadic function. FIXME this is bogus; see c++/41958. */
20652 if (lose1 == lose2
20653 && args1 && TREE_VALUE (args1)
20654 && args2 && TREE_VALUE (args2))
20655 {
20656 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
20657 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
20658 }
20659
20660 if (lose1 == lose2)
20661 return 0;
20662 else if (!lose1)
20663 return 1;
20664 else
20665 return -1;
20666 }
20667
20668 /* Determine which of two partial specializations of TMPL is more
20669 specialized.
20670
20671 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20672 to the first partial specialization. The TREE_PURPOSE is the
20673 innermost set of template parameters for the partial
20674 specialization. PAT2 is similar, but for the second template.
20675
20676 Return 1 if the first partial specialization is more specialized;
20677 -1 if the second is more specialized; 0 if neither is more
20678 specialized.
20679
20680 See [temp.class.order] for information about determining which of
20681 two templates is more specialized. */
20682
20683 static int
20684 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
20685 {
20686 tree targs;
20687 int winner = 0;
20688 bool any_deductions = false;
20689
20690 tree tmpl1 = TREE_VALUE (pat1);
20691 tree tmpl2 = TREE_VALUE (pat2);
20692 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
20693 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
20694
20695 /* Just like what happens for functions, if we are ordering between
20696 different template specializations, we may encounter dependent
20697 types in the arguments, and we need our dependency check functions
20698 to behave correctly. */
20699 ++processing_template_decl;
20700 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
20701 if (targs)
20702 {
20703 --winner;
20704 any_deductions = true;
20705 }
20706
20707 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
20708 if (targs)
20709 {
20710 ++winner;
20711 any_deductions = true;
20712 }
20713 --processing_template_decl;
20714
20715 /* If both deductions succeed, the partial ordering selects the more
20716 constrained template. */
20717 if (!winner && any_deductions)
20718 return more_constrained (tmpl1, tmpl2);
20719
20720 /* In the case of a tie where at least one of the templates
20721 has a parameter pack at the end, the template with the most
20722 non-packed parameters wins. */
20723 if (winner == 0
20724 && any_deductions
20725 && (template_args_variadic_p (TREE_PURPOSE (pat1))
20726 || template_args_variadic_p (TREE_PURPOSE (pat2))))
20727 {
20728 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
20729 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
20730 int len1 = TREE_VEC_LENGTH (args1);
20731 int len2 = TREE_VEC_LENGTH (args2);
20732
20733 /* We don't count the pack expansion at the end. */
20734 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
20735 --len1;
20736 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
20737 --len2;
20738
20739 if (len1 > len2)
20740 return 1;
20741 else if (len1 < len2)
20742 return -1;
20743 }
20744
20745 return winner;
20746 }
20747
20748 /* Return the template arguments that will produce the function signature
20749 DECL from the function template FN, with the explicit template
20750 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20751 also match. Return NULL_TREE if no satisfactory arguments could be
20752 found. */
20753
20754 static tree
20755 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
20756 {
20757 int ntparms = DECL_NTPARMS (fn);
20758 tree targs = make_tree_vec (ntparms);
20759 tree decl_type = TREE_TYPE (decl);
20760 tree decl_arg_types;
20761 tree *args;
20762 unsigned int nargs, ix;
20763 tree arg;
20764
20765 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
20766
20767 /* Never do unification on the 'this' parameter. */
20768 decl_arg_types = skip_artificial_parms_for (decl,
20769 TYPE_ARG_TYPES (decl_type));
20770
20771 nargs = list_length (decl_arg_types);
20772 args = XALLOCAVEC (tree, nargs);
20773 for (arg = decl_arg_types, ix = 0;
20774 arg != NULL_TREE && arg != void_list_node;
20775 arg = TREE_CHAIN (arg), ++ix)
20776 args[ix] = TREE_VALUE (arg);
20777
20778 if (fn_type_unification (fn, explicit_args, targs,
20779 args, ix,
20780 (check_rettype || DECL_CONV_FN_P (fn)
20781 ? TREE_TYPE (decl_type) : NULL_TREE),
20782 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
20783 /*decltype*/false)
20784 == error_mark_node)
20785 return NULL_TREE;
20786
20787 return targs;
20788 }
20789
20790 /* Return the innermost template arguments that, when applied to a partial
20791 specialization SPEC_TMPL of TMPL, yield the ARGS.
20792
20793 For example, suppose we have:
20794
20795 template <class T, class U> struct S {};
20796 template <class T> struct S<T*, int> {};
20797
20798 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
20799 partial specialization and the ARGS will be {double*, int}. The resulting
20800 vector will be {double}, indicating that `T' is bound to `double'. */
20801
20802 static tree
20803 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
20804 {
20805 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
20806 tree spec_args
20807 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
20808 int i, ntparms = TREE_VEC_LENGTH (tparms);
20809 tree deduced_args;
20810 tree innermost_deduced_args;
20811
20812 innermost_deduced_args = make_tree_vec (ntparms);
20813 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20814 {
20815 deduced_args = copy_node (args);
20816 SET_TMPL_ARGS_LEVEL (deduced_args,
20817 TMPL_ARGS_DEPTH (deduced_args),
20818 innermost_deduced_args);
20819 }
20820 else
20821 deduced_args = innermost_deduced_args;
20822
20823 if (unify (tparms, deduced_args,
20824 INNERMOST_TEMPLATE_ARGS (spec_args),
20825 INNERMOST_TEMPLATE_ARGS (args),
20826 UNIFY_ALLOW_NONE, /*explain_p=*/false))
20827 return NULL_TREE;
20828
20829 for (i = 0; i < ntparms; ++i)
20830 if (! TREE_VEC_ELT (innermost_deduced_args, i))
20831 return NULL_TREE;
20832
20833 tree tinst = build_tree_list (spec_tmpl, deduced_args);
20834 if (!push_tinst_level (tinst))
20835 {
20836 excessive_deduction_depth = true;
20837 return NULL_TREE;
20838 }
20839
20840 /* Verify that nondeduced template arguments agree with the type
20841 obtained from argument deduction.
20842
20843 For example:
20844
20845 struct A { typedef int X; };
20846 template <class T, class U> struct C {};
20847 template <class T> struct C<T, typename T::X> {};
20848
20849 Then with the instantiation `C<A, int>', we can deduce that
20850 `T' is `A' but unify () does not check whether `typename T::X'
20851 is `int'. */
20852 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
20853 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20854 spec_args, tmpl,
20855 tf_none, false, false);
20856
20857 pop_tinst_level ();
20858
20859 if (spec_args == error_mark_node
20860 /* We only need to check the innermost arguments; the other
20861 arguments will always agree. */
20862 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
20863 INNERMOST_TEMPLATE_ARGS (args)))
20864 return NULL_TREE;
20865
20866 /* Now that we have bindings for all of the template arguments,
20867 ensure that the arguments deduced for the template template
20868 parameters have compatible template parameter lists. See the use
20869 of template_template_parm_bindings_ok_p in fn_type_unification
20870 for more information. */
20871 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
20872 return NULL_TREE;
20873
20874 return deduced_args;
20875 }
20876
20877 // Compare two function templates T1 and T2 by deducing bindings
20878 // from one against the other. If both deductions succeed, compare
20879 // constraints to see which is more constrained.
20880 static int
20881 more_specialized_inst (tree t1, tree t2)
20882 {
20883 int fate = 0;
20884 int count = 0;
20885
20886 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
20887 {
20888 --fate;
20889 ++count;
20890 }
20891
20892 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
20893 {
20894 ++fate;
20895 ++count;
20896 }
20897
20898 // If both deductions succeed, then one may be more constrained.
20899 if (count == 2 && fate == 0)
20900 fate = more_constrained (t1, t2);
20901
20902 return fate;
20903 }
20904
20905 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20906 Return the TREE_LIST node with the most specialized template, if
20907 any. If there is no most specialized template, the error_mark_node
20908 is returned.
20909
20910 Note that this function does not look at, or modify, the
20911 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20912 returned is one of the elements of INSTANTIATIONS, callers may
20913 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20914 and retrieve it from the value returned. */
20915
20916 tree
20917 most_specialized_instantiation (tree templates)
20918 {
20919 tree fn, champ;
20920
20921 ++processing_template_decl;
20922
20923 champ = templates;
20924 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
20925 {
20926 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
20927 if (fate == -1)
20928 champ = fn;
20929 else if (!fate)
20930 {
20931 /* Equally specialized, move to next function. If there
20932 is no next function, nothing's most specialized. */
20933 fn = TREE_CHAIN (fn);
20934 champ = fn;
20935 if (!fn)
20936 break;
20937 }
20938 }
20939
20940 if (champ)
20941 /* Now verify that champ is better than everything earlier in the
20942 instantiation list. */
20943 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
20944 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
20945 {
20946 champ = NULL_TREE;
20947 break;
20948 }
20949 }
20950
20951 processing_template_decl--;
20952
20953 if (!champ)
20954 return error_mark_node;
20955
20956 return champ;
20957 }
20958
20959 /* If DECL is a specialization of some template, return the most
20960 general such template. Otherwise, returns NULL_TREE.
20961
20962 For example, given:
20963
20964 template <class T> struct S { template <class U> void f(U); };
20965
20966 if TMPL is `template <class U> void S<int>::f(U)' this will return
20967 the full template. This function will not trace past partial
20968 specializations, however. For example, given in addition:
20969
20970 template <class T> struct S<T*> { template <class U> void f(U); };
20971
20972 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20973 `template <class T> template <class U> S<T*>::f(U)'. */
20974
20975 tree
20976 most_general_template (tree decl)
20977 {
20978 if (TREE_CODE (decl) != TEMPLATE_DECL)
20979 {
20980 if (tree tinfo = get_template_info (decl))
20981 decl = TI_TEMPLATE (tinfo);
20982 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20983 template friend, or a FIELD_DECL for a capture pack. */
20984 if (TREE_CODE (decl) != TEMPLATE_DECL)
20985 return NULL_TREE;
20986 }
20987
20988 /* Look for more and more general templates. */
20989 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
20990 {
20991 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20992 (See cp-tree.h for details.) */
20993 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
20994 break;
20995
20996 if (CLASS_TYPE_P (TREE_TYPE (decl))
20997 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
20998 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
20999 break;
21000
21001 /* Stop if we run into an explicitly specialized class template. */
21002 if (!DECL_NAMESPACE_SCOPE_P (decl)
21003 && DECL_CONTEXT (decl)
21004 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21005 break;
21006
21007 decl = DECL_TI_TEMPLATE (decl);
21008 }
21009
21010 return decl;
21011 }
21012
21013 /* Return the most specialized of the template partial specializations
21014 which can produce TARGET, a specialization of some class or variable
21015 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21016 a TEMPLATE_DECL node corresponding to the partial specialization, while
21017 the TREE_PURPOSE is the set of template arguments that must be
21018 substituted into the template pattern in order to generate TARGET.
21019
21020 If the choice of partial specialization is ambiguous, a diagnostic
21021 is issued, and the error_mark_node is returned. If there are no
21022 partial specializations matching TARGET, then NULL_TREE is
21023 returned, indicating that the primary template should be used. */
21024
21025 static tree
21026 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21027 {
21028 tree list = NULL_TREE;
21029 tree t;
21030 tree champ;
21031 int fate;
21032 bool ambiguous_p;
21033 tree outer_args = NULL_TREE;
21034 tree tmpl, args;
21035
21036 if (TYPE_P (target))
21037 {
21038 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21039 tmpl = TI_TEMPLATE (tinfo);
21040 args = TI_ARGS (tinfo);
21041 }
21042 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21043 {
21044 tmpl = TREE_OPERAND (target, 0);
21045 args = TREE_OPERAND (target, 1);
21046 }
21047 else if (VAR_P (target))
21048 {
21049 tree tinfo = DECL_TEMPLATE_INFO (target);
21050 tmpl = TI_TEMPLATE (tinfo);
21051 args = TI_ARGS (tinfo);
21052 }
21053 else
21054 gcc_unreachable ();
21055
21056 tree main_tmpl = most_general_template (tmpl);
21057
21058 /* For determining which partial specialization to use, only the
21059 innermost args are interesting. */
21060 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21061 {
21062 outer_args = strip_innermost_template_args (args, 1);
21063 args = INNERMOST_TEMPLATE_ARGS (args);
21064 }
21065
21066 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21067 {
21068 tree spec_args;
21069 tree spec_tmpl = TREE_VALUE (t);
21070
21071 if (outer_args)
21072 {
21073 /* Substitute in the template args from the enclosing class. */
21074 ++processing_template_decl;
21075 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21076 --processing_template_decl;
21077 }
21078
21079 if (spec_tmpl == error_mark_node)
21080 return error_mark_node;
21081
21082 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21083 if (spec_args)
21084 {
21085 if (outer_args)
21086 spec_args = add_to_template_args (outer_args, spec_args);
21087
21088 /* Keep the candidate only if the constraints are satisfied,
21089 or if we're not compiling with concepts. */
21090 if (!flag_concepts
21091 || constraints_satisfied_p (spec_tmpl, spec_args))
21092 {
21093 list = tree_cons (spec_args, TREE_VALUE (t), list);
21094 TREE_TYPE (list) = TREE_TYPE (t);
21095 }
21096 }
21097 }
21098
21099 if (! list)
21100 return NULL_TREE;
21101
21102 ambiguous_p = false;
21103 t = list;
21104 champ = t;
21105 t = TREE_CHAIN (t);
21106 for (; t; t = TREE_CHAIN (t))
21107 {
21108 fate = more_specialized_partial_spec (tmpl, champ, t);
21109 if (fate == 1)
21110 ;
21111 else
21112 {
21113 if (fate == 0)
21114 {
21115 t = TREE_CHAIN (t);
21116 if (! t)
21117 {
21118 ambiguous_p = true;
21119 break;
21120 }
21121 }
21122 champ = t;
21123 }
21124 }
21125
21126 if (!ambiguous_p)
21127 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21128 {
21129 fate = more_specialized_partial_spec (tmpl, champ, t);
21130 if (fate != 1)
21131 {
21132 ambiguous_p = true;
21133 break;
21134 }
21135 }
21136
21137 if (ambiguous_p)
21138 {
21139 const char *str;
21140 char *spaces = NULL;
21141 if (!(complain & tf_error))
21142 return error_mark_node;
21143 if (TYPE_P (target))
21144 error ("ambiguous template instantiation for %q#T", target);
21145 else
21146 error ("ambiguous template instantiation for %q#D", target);
21147 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21148 for (t = list; t; t = TREE_CHAIN (t))
21149 {
21150 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21151 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21152 "%s %#S", spaces ? spaces : str, subst);
21153 spaces = spaces ? spaces : get_spaces (str);
21154 }
21155 free (spaces);
21156 return error_mark_node;
21157 }
21158
21159 return champ;
21160 }
21161
21162 /* Explicitly instantiate DECL. */
21163
21164 void
21165 do_decl_instantiation (tree decl, tree storage)
21166 {
21167 tree result = NULL_TREE;
21168 int extern_p = 0;
21169
21170 if (!decl || decl == error_mark_node)
21171 /* An error occurred, for which grokdeclarator has already issued
21172 an appropriate message. */
21173 return;
21174 else if (! DECL_LANG_SPECIFIC (decl))
21175 {
21176 error ("explicit instantiation of non-template %q#D", decl);
21177 return;
21178 }
21179
21180 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21181 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21182
21183 if (VAR_P (decl) && !var_templ)
21184 {
21185 /* There is an asymmetry here in the way VAR_DECLs and
21186 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21187 the latter, the DECL we get back will be marked as a
21188 template instantiation, and the appropriate
21189 DECL_TEMPLATE_INFO will be set up. This does not happen for
21190 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21191 should handle VAR_DECLs as it currently handles
21192 FUNCTION_DECLs. */
21193 if (!DECL_CLASS_SCOPE_P (decl))
21194 {
21195 error ("%qD is not a static data member of a class template", decl);
21196 return;
21197 }
21198 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21199 if (!result || !VAR_P (result))
21200 {
21201 error ("no matching template for %qD found", decl);
21202 return;
21203 }
21204 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21205 {
21206 error ("type %qT for explicit instantiation %qD does not match "
21207 "declared type %qT", TREE_TYPE (result), decl,
21208 TREE_TYPE (decl));
21209 return;
21210 }
21211 }
21212 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21213 {
21214 error ("explicit instantiation of %q#D", decl);
21215 return;
21216 }
21217 else
21218 result = decl;
21219
21220 /* Check for various error cases. Note that if the explicit
21221 instantiation is valid the RESULT will currently be marked as an
21222 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21223 until we get here. */
21224
21225 if (DECL_TEMPLATE_SPECIALIZATION (result))
21226 {
21227 /* DR 259 [temp.spec].
21228
21229 Both an explicit instantiation and a declaration of an explicit
21230 specialization shall not appear in a program unless the explicit
21231 instantiation follows a declaration of the explicit specialization.
21232
21233 For a given set of template parameters, if an explicit
21234 instantiation of a template appears after a declaration of an
21235 explicit specialization for that template, the explicit
21236 instantiation has no effect. */
21237 return;
21238 }
21239 else if (DECL_EXPLICIT_INSTANTIATION (result))
21240 {
21241 /* [temp.spec]
21242
21243 No program shall explicitly instantiate any template more
21244 than once.
21245
21246 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21247 the first instantiation was `extern' and the second is not,
21248 and EXTERN_P for the opposite case. */
21249 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21250 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21251 /* If an "extern" explicit instantiation follows an ordinary
21252 explicit instantiation, the template is instantiated. */
21253 if (extern_p)
21254 return;
21255 }
21256 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21257 {
21258 error ("no matching template for %qD found", result);
21259 return;
21260 }
21261 else if (!DECL_TEMPLATE_INFO (result))
21262 {
21263 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21264 return;
21265 }
21266
21267 if (storage == NULL_TREE)
21268 ;
21269 else if (storage == ridpointers[(int) RID_EXTERN])
21270 {
21271 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21272 pedwarn (input_location, OPT_Wpedantic,
21273 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21274 "instantiations");
21275 extern_p = 1;
21276 }
21277 else
21278 error ("storage class %qD applied to template instantiation", storage);
21279
21280 check_explicit_instantiation_namespace (result);
21281 mark_decl_instantiated (result, extern_p);
21282 if (! extern_p)
21283 instantiate_decl (result, /*defer_ok=*/1,
21284 /*expl_inst_class_mem_p=*/false);
21285 }
21286
21287 static void
21288 mark_class_instantiated (tree t, int extern_p)
21289 {
21290 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21291 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21292 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21293 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21294 if (! extern_p)
21295 {
21296 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21297 rest_of_type_compilation (t, 1);
21298 }
21299 }
21300
21301 /* Called from do_type_instantiation through binding_table_foreach to
21302 do recursive instantiation for the type bound in ENTRY. */
21303 static void
21304 bt_instantiate_type_proc (binding_entry entry, void *data)
21305 {
21306 tree storage = *(tree *) data;
21307
21308 if (MAYBE_CLASS_TYPE_P (entry->type)
21309 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
21310 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
21311 }
21312
21313 /* Called from do_type_instantiation to instantiate a member
21314 (a member function or a static member variable) of an
21315 explicitly instantiated class template. */
21316 static void
21317 instantiate_class_member (tree decl, int extern_p)
21318 {
21319 mark_decl_instantiated (decl, extern_p);
21320 if (! extern_p)
21321 instantiate_decl (decl, /*defer_ok=*/1,
21322 /*expl_inst_class_mem_p=*/true);
21323 }
21324
21325 /* Perform an explicit instantiation of template class T. STORAGE, if
21326 non-null, is the RID for extern, inline or static. COMPLAIN is
21327 nonzero if this is called from the parser, zero if called recursively,
21328 since the standard is unclear (as detailed below). */
21329
21330 void
21331 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
21332 {
21333 int extern_p = 0;
21334 int nomem_p = 0;
21335 int static_p = 0;
21336 int previous_instantiation_extern_p = 0;
21337
21338 if (TREE_CODE (t) == TYPE_DECL)
21339 t = TREE_TYPE (t);
21340
21341 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
21342 {
21343 tree tmpl =
21344 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
21345 if (tmpl)
21346 error ("explicit instantiation of non-class template %qD", tmpl);
21347 else
21348 error ("explicit instantiation of non-template type %qT", t);
21349 return;
21350 }
21351
21352 complete_type (t);
21353
21354 if (!COMPLETE_TYPE_P (t))
21355 {
21356 if (complain & tf_error)
21357 error ("explicit instantiation of %q#T before definition of template",
21358 t);
21359 return;
21360 }
21361
21362 if (storage != NULL_TREE)
21363 {
21364 if (!in_system_header_at (input_location))
21365 {
21366 if (storage == ridpointers[(int) RID_EXTERN])
21367 {
21368 if (cxx_dialect == cxx98)
21369 pedwarn (input_location, OPT_Wpedantic,
21370 "ISO C++ 1998 forbids the use of %<extern%> on "
21371 "explicit instantiations");
21372 }
21373 else
21374 pedwarn (input_location, OPT_Wpedantic,
21375 "ISO C++ forbids the use of %qE"
21376 " on explicit instantiations", storage);
21377 }
21378
21379 if (storage == ridpointers[(int) RID_INLINE])
21380 nomem_p = 1;
21381 else if (storage == ridpointers[(int) RID_EXTERN])
21382 extern_p = 1;
21383 else if (storage == ridpointers[(int) RID_STATIC])
21384 static_p = 1;
21385 else
21386 {
21387 error ("storage class %qD applied to template instantiation",
21388 storage);
21389 extern_p = 0;
21390 }
21391 }
21392
21393 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
21394 {
21395 /* DR 259 [temp.spec].
21396
21397 Both an explicit instantiation and a declaration of an explicit
21398 specialization shall not appear in a program unless the explicit
21399 instantiation follows a declaration of the explicit specialization.
21400
21401 For a given set of template parameters, if an explicit
21402 instantiation of a template appears after a declaration of an
21403 explicit specialization for that template, the explicit
21404 instantiation has no effect. */
21405 return;
21406 }
21407 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
21408 {
21409 /* [temp.spec]
21410
21411 No program shall explicitly instantiate any template more
21412 than once.
21413
21414 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21415 instantiation was `extern'. If EXTERN_P then the second is.
21416 These cases are OK. */
21417 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
21418
21419 if (!previous_instantiation_extern_p && !extern_p
21420 && (complain & tf_error))
21421 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
21422
21423 /* If we've already instantiated the template, just return now. */
21424 if (!CLASSTYPE_INTERFACE_ONLY (t))
21425 return;
21426 }
21427
21428 check_explicit_instantiation_namespace (TYPE_NAME (t));
21429 mark_class_instantiated (t, extern_p);
21430
21431 if (nomem_p)
21432 return;
21433
21434 {
21435 tree tmp;
21436
21437 /* In contrast to implicit instantiation, where only the
21438 declarations, and not the definitions, of members are
21439 instantiated, we have here:
21440
21441 [temp.explicit]
21442
21443 The explicit instantiation of a class template specialization
21444 implies the instantiation of all of its members not
21445 previously explicitly specialized in the translation unit
21446 containing the explicit instantiation.
21447
21448 Of course, we can't instantiate member template classes, since
21449 we don't have any arguments for them. Note that the standard
21450 is unclear on whether the instantiation of the members are
21451 *explicit* instantiations or not. However, the most natural
21452 interpretation is that it should be an explicit instantiation. */
21453
21454 if (! static_p)
21455 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
21456 if (TREE_CODE (tmp) == FUNCTION_DECL
21457 && DECL_TEMPLATE_INSTANTIATION (tmp))
21458 instantiate_class_member (tmp, extern_p);
21459
21460 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
21461 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
21462 instantiate_class_member (tmp, extern_p);
21463
21464 if (CLASSTYPE_NESTED_UTDS (t))
21465 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
21466 bt_instantiate_type_proc, &storage);
21467 }
21468 }
21469
21470 /* Given a function DECL, which is a specialization of TMPL, modify
21471 DECL to be a re-instantiation of TMPL with the same template
21472 arguments. TMPL should be the template into which tsubst'ing
21473 should occur for DECL, not the most general template.
21474
21475 One reason for doing this is a scenario like this:
21476
21477 template <class T>
21478 void f(const T&, int i);
21479
21480 void g() { f(3, 7); }
21481
21482 template <class T>
21483 void f(const T& t, const int i) { }
21484
21485 Note that when the template is first instantiated, with
21486 instantiate_template, the resulting DECL will have no name for the
21487 first parameter, and the wrong type for the second. So, when we go
21488 to instantiate the DECL, we regenerate it. */
21489
21490 static void
21491 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
21492 {
21493 /* The arguments used to instantiate DECL, from the most general
21494 template. */
21495 tree code_pattern;
21496
21497 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
21498
21499 /* Make sure that we can see identifiers, and compute access
21500 correctly. */
21501 push_access_scope (decl);
21502
21503 if (TREE_CODE (decl) == FUNCTION_DECL)
21504 {
21505 tree decl_parm;
21506 tree pattern_parm;
21507 tree specs;
21508 int args_depth;
21509 int parms_depth;
21510
21511 args_depth = TMPL_ARGS_DEPTH (args);
21512 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
21513 if (args_depth > parms_depth)
21514 args = get_innermost_template_args (args, parms_depth);
21515
21516 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
21517 args, tf_error, NULL_TREE,
21518 /*defer_ok*/false);
21519 if (specs && specs != error_mark_node)
21520 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
21521 specs);
21522
21523 /* Merge parameter declarations. */
21524 decl_parm = skip_artificial_parms_for (decl,
21525 DECL_ARGUMENTS (decl));
21526 pattern_parm
21527 = skip_artificial_parms_for (code_pattern,
21528 DECL_ARGUMENTS (code_pattern));
21529 while (decl_parm && !DECL_PACK_P (pattern_parm))
21530 {
21531 tree parm_type;
21532 tree attributes;
21533
21534 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21535 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
21536 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
21537 NULL_TREE);
21538 parm_type = type_decays_to (parm_type);
21539 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21540 TREE_TYPE (decl_parm) = parm_type;
21541 attributes = DECL_ATTRIBUTES (pattern_parm);
21542 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21543 {
21544 DECL_ATTRIBUTES (decl_parm) = attributes;
21545 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21546 }
21547 decl_parm = DECL_CHAIN (decl_parm);
21548 pattern_parm = DECL_CHAIN (pattern_parm);
21549 }
21550 /* Merge any parameters that match with the function parameter
21551 pack. */
21552 if (pattern_parm && DECL_PACK_P (pattern_parm))
21553 {
21554 int i, len;
21555 tree expanded_types;
21556 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21557 the parameters in this function parameter pack. */
21558 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
21559 args, tf_error, NULL_TREE);
21560 len = TREE_VEC_LENGTH (expanded_types);
21561 for (i = 0; i < len; i++)
21562 {
21563 tree parm_type;
21564 tree attributes;
21565
21566 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21567 /* Rename the parameter to include the index. */
21568 DECL_NAME (decl_parm) =
21569 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
21570 parm_type = TREE_VEC_ELT (expanded_types, i);
21571 parm_type = type_decays_to (parm_type);
21572 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21573 TREE_TYPE (decl_parm) = parm_type;
21574 attributes = DECL_ATTRIBUTES (pattern_parm);
21575 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21576 {
21577 DECL_ATTRIBUTES (decl_parm) = attributes;
21578 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21579 }
21580 decl_parm = DECL_CHAIN (decl_parm);
21581 }
21582 }
21583 /* Merge additional specifiers from the CODE_PATTERN. */
21584 if (DECL_DECLARED_INLINE_P (code_pattern)
21585 && !DECL_DECLARED_INLINE_P (decl))
21586 DECL_DECLARED_INLINE_P (decl) = 1;
21587 }
21588 else if (VAR_P (decl))
21589 {
21590 DECL_INITIAL (decl) =
21591 tsubst_expr (DECL_INITIAL (code_pattern), args,
21592 tf_error, DECL_TI_TEMPLATE (decl),
21593 /*integral_constant_expression_p=*/false);
21594 if (VAR_HAD_UNKNOWN_BOUND (decl))
21595 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
21596 tf_error, DECL_TI_TEMPLATE (decl));
21597 }
21598 else
21599 gcc_unreachable ();
21600
21601 pop_access_scope (decl);
21602 }
21603
21604 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21605 substituted to get DECL. */
21606
21607 tree
21608 template_for_substitution (tree decl)
21609 {
21610 tree tmpl = DECL_TI_TEMPLATE (decl);
21611
21612 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21613 for the instantiation. This is not always the most general
21614 template. Consider, for example:
21615
21616 template <class T>
21617 struct S { template <class U> void f();
21618 template <> void f<int>(); };
21619
21620 and an instantiation of S<double>::f<int>. We want TD to be the
21621 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21622 while (/* An instantiation cannot have a definition, so we need a
21623 more general template. */
21624 DECL_TEMPLATE_INSTANTIATION (tmpl)
21625 /* We must also deal with friend templates. Given:
21626
21627 template <class T> struct S {
21628 template <class U> friend void f() {};
21629 };
21630
21631 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21632 so far as the language is concerned, but that's still
21633 where we get the pattern for the instantiation from. On
21634 other hand, if the definition comes outside the class, say:
21635
21636 template <class T> struct S {
21637 template <class U> friend void f();
21638 };
21639 template <class U> friend void f() {}
21640
21641 we don't need to look any further. That's what the check for
21642 DECL_INITIAL is for. */
21643 || (TREE_CODE (decl) == FUNCTION_DECL
21644 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
21645 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
21646 {
21647 /* The present template, TD, should not be a definition. If it
21648 were a definition, we should be using it! Note that we
21649 cannot restructure the loop to just keep going until we find
21650 a template with a definition, since that might go too far if
21651 a specialization was declared, but not defined. */
21652
21653 /* Fetch the more general template. */
21654 tmpl = DECL_TI_TEMPLATE (tmpl);
21655 }
21656
21657 return tmpl;
21658 }
21659
21660 /* Returns true if we need to instantiate this template instance even if we
21661 know we aren't going to emit it. */
21662
21663 bool
21664 always_instantiate_p (tree decl)
21665 {
21666 /* We always instantiate inline functions so that we can inline them. An
21667 explicit instantiation declaration prohibits implicit instantiation of
21668 non-inline functions. With high levels of optimization, we would
21669 normally inline non-inline functions -- but we're not allowed to do
21670 that for "extern template" functions. Therefore, we check
21671 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21672 return ((TREE_CODE (decl) == FUNCTION_DECL
21673 && (DECL_DECLARED_INLINE_P (decl)
21674 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
21675 /* And we need to instantiate static data members so that
21676 their initializers are available in integral constant
21677 expressions. */
21678 || (VAR_P (decl)
21679 && decl_maybe_constant_var_p (decl)));
21680 }
21681
21682 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21683 instantiate it now, modifying TREE_TYPE (fn). */
21684
21685 void
21686 maybe_instantiate_noexcept (tree fn)
21687 {
21688 tree fntype, spec, noex, clone;
21689
21690 /* Don't instantiate a noexcept-specification from template context. */
21691 if (processing_template_decl)
21692 return;
21693
21694 if (DECL_CLONED_FUNCTION_P (fn))
21695 fn = DECL_CLONED_FUNCTION (fn);
21696 fntype = TREE_TYPE (fn);
21697 spec = TYPE_RAISES_EXCEPTIONS (fntype);
21698
21699 if (!spec || !TREE_PURPOSE (spec))
21700 return;
21701
21702 noex = TREE_PURPOSE (spec);
21703
21704 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
21705 {
21706 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
21707 spec = get_defaulted_eh_spec (fn);
21708 else if (push_tinst_level (fn))
21709 {
21710 push_access_scope (fn);
21711 push_deferring_access_checks (dk_no_deferred);
21712 input_location = DECL_SOURCE_LOCATION (fn);
21713 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
21714 DEFERRED_NOEXCEPT_ARGS (noex),
21715 tf_warning_or_error, fn,
21716 /*function_p=*/false,
21717 /*integral_constant_expression_p=*/true);
21718 pop_deferring_access_checks ();
21719 pop_access_scope (fn);
21720 pop_tinst_level ();
21721 spec = build_noexcept_spec (noex, tf_warning_or_error);
21722 if (spec == error_mark_node)
21723 spec = noexcept_false_spec;
21724 }
21725 else
21726 spec = noexcept_false_spec;
21727
21728 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
21729 }
21730
21731 FOR_EACH_CLONE (clone, fn)
21732 {
21733 if (TREE_TYPE (clone) == fntype)
21734 TREE_TYPE (clone) = TREE_TYPE (fn);
21735 else
21736 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
21737 }
21738 }
21739
21740 /* Produce the definition of D, a _DECL generated from a template. If
21741 DEFER_OK is nonzero, then we don't have to actually do the
21742 instantiation now; we just have to do it sometime. Normally it is
21743 an error if this is an explicit instantiation but D is undefined.
21744 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21745 explicitly instantiated class template. */
21746
21747 tree
21748 instantiate_decl (tree d, int defer_ok,
21749 bool expl_inst_class_mem_p)
21750 {
21751 tree tmpl = DECL_TI_TEMPLATE (d);
21752 tree gen_args;
21753 tree args;
21754 tree td;
21755 tree code_pattern;
21756 tree spec;
21757 tree gen_tmpl;
21758 bool pattern_defined;
21759 location_t saved_loc = input_location;
21760 int saved_unevaluated_operand = cp_unevaluated_operand;
21761 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21762 bool external_p;
21763 bool deleted_p;
21764 tree fn_context;
21765 bool nested = false;
21766
21767 /* This function should only be used to instantiate templates for
21768 functions and static member variables. */
21769 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
21770
21771 /* A concept is never instantiated. */
21772 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
21773
21774 /* Variables are never deferred; if instantiation is required, they
21775 are instantiated right away. That allows for better code in the
21776 case that an expression refers to the value of the variable --
21777 if the variable has a constant value the referring expression can
21778 take advantage of that fact. */
21779 if (VAR_P (d)
21780 || DECL_DECLARED_CONSTEXPR_P (d))
21781 defer_ok = 0;
21782
21783 /* Don't instantiate cloned functions. Instead, instantiate the
21784 functions they cloned. */
21785 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
21786 d = DECL_CLONED_FUNCTION (d);
21787
21788 if (DECL_TEMPLATE_INSTANTIATED (d)
21789 || (TREE_CODE (d) == FUNCTION_DECL
21790 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
21791 || DECL_TEMPLATE_SPECIALIZATION (d))
21792 /* D has already been instantiated or explicitly specialized, so
21793 there's nothing for us to do here.
21794
21795 It might seem reasonable to check whether or not D is an explicit
21796 instantiation, and, if so, stop here. But when an explicit
21797 instantiation is deferred until the end of the compilation,
21798 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21799 the instantiation. */
21800 return d;
21801
21802 /* Check to see whether we know that this template will be
21803 instantiated in some other file, as with "extern template"
21804 extension. */
21805 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
21806
21807 /* In general, we do not instantiate such templates. */
21808 if (external_p && !always_instantiate_p (d))
21809 return d;
21810
21811 gen_tmpl = most_general_template (tmpl);
21812 gen_args = DECL_TI_ARGS (d);
21813
21814 if (tmpl != gen_tmpl)
21815 /* We should already have the extra args. */
21816 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
21817 == TMPL_ARGS_DEPTH (gen_args));
21818 /* And what's in the hash table should match D. */
21819 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
21820 || spec == NULL_TREE);
21821
21822 /* This needs to happen before any tsubsting. */
21823 if (! push_tinst_level (d))
21824 return d;
21825
21826 timevar_push (TV_TEMPLATE_INST);
21827
21828 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21829 for the instantiation. */
21830 td = template_for_substitution (d);
21831 args = gen_args;
21832
21833 if (VAR_P (d))
21834 {
21835 /* Look up an explicit specialization, if any. */
21836 tree tid = lookup_template_variable (gen_tmpl, gen_args);
21837 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
21838 if (elt && elt != error_mark_node)
21839 {
21840 td = TREE_VALUE (elt);
21841 args = TREE_PURPOSE (elt);
21842 }
21843 }
21844
21845 code_pattern = DECL_TEMPLATE_RESULT (td);
21846
21847 /* We should never be trying to instantiate a member of a class
21848 template or partial specialization. */
21849 gcc_assert (d != code_pattern);
21850
21851 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
21852 || DECL_TEMPLATE_SPECIALIZATION (td))
21853 /* In the case of a friend template whose definition is provided
21854 outside the class, we may have too many arguments. Drop the
21855 ones we don't need. The same is true for specializations. */
21856 args = get_innermost_template_args
21857 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
21858
21859 if (TREE_CODE (d) == FUNCTION_DECL)
21860 {
21861 deleted_p = DECL_DELETED_FN (code_pattern);
21862 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
21863 && DECL_INITIAL (code_pattern) != error_mark_node)
21864 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
21865 || deleted_p);
21866 }
21867 else
21868 {
21869 deleted_p = false;
21870 if (DECL_CLASS_SCOPE_P (code_pattern))
21871 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
21872 else
21873 pattern_defined = ! DECL_EXTERNAL (code_pattern);
21874 }
21875
21876 /* We may be in the middle of deferred access check. Disable it now. */
21877 push_deferring_access_checks (dk_no_deferred);
21878
21879 /* Unless an explicit instantiation directive has already determined
21880 the linkage of D, remember that a definition is available for
21881 this entity. */
21882 if (pattern_defined
21883 && !DECL_INTERFACE_KNOWN (d)
21884 && !DECL_NOT_REALLY_EXTERN (d))
21885 mark_definable (d);
21886
21887 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
21888 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
21889 input_location = DECL_SOURCE_LOCATION (d);
21890
21891 /* If D is a member of an explicitly instantiated class template,
21892 and no definition is available, treat it like an implicit
21893 instantiation. */
21894 if (!pattern_defined && expl_inst_class_mem_p
21895 && DECL_EXPLICIT_INSTANTIATION (d))
21896 {
21897 /* Leave linkage flags alone on instantiations with anonymous
21898 visibility. */
21899 if (TREE_PUBLIC (d))
21900 {
21901 DECL_NOT_REALLY_EXTERN (d) = 0;
21902 DECL_INTERFACE_KNOWN (d) = 0;
21903 }
21904 SET_DECL_IMPLICIT_INSTANTIATION (d);
21905 }
21906
21907 /* Defer all other templates, unless we have been explicitly
21908 forbidden from doing so. */
21909 if (/* If there is no definition, we cannot instantiate the
21910 template. */
21911 ! pattern_defined
21912 /* If it's OK to postpone instantiation, do so. */
21913 || defer_ok
21914 /* If this is a static data member that will be defined
21915 elsewhere, we don't want to instantiate the entire data
21916 member, but we do want to instantiate the initializer so that
21917 we can substitute that elsewhere. */
21918 || (external_p && VAR_P (d))
21919 /* Handle here a deleted function too, avoid generating
21920 its body (c++/61080). */
21921 || deleted_p)
21922 {
21923 /* The definition of the static data member is now required so
21924 we must substitute the initializer. */
21925 if (VAR_P (d)
21926 && !DECL_INITIAL (d)
21927 && DECL_INITIAL (code_pattern))
21928 {
21929 tree ns;
21930 tree init;
21931 bool const_init = false;
21932 bool enter_context = DECL_CLASS_SCOPE_P (d);
21933
21934 ns = decl_namespace_context (d);
21935 push_nested_namespace (ns);
21936 if (enter_context)
21937 push_nested_class (DECL_CONTEXT (d));
21938 init = tsubst_expr (DECL_INITIAL (code_pattern),
21939 args,
21940 tf_warning_or_error, NULL_TREE,
21941 /*integral_constant_expression_p=*/false);
21942 /* If instantiating the initializer involved instantiating this
21943 again, don't call cp_finish_decl twice. */
21944 if (!DECL_INITIAL (d))
21945 {
21946 /* Make sure the initializer is still constant, in case of
21947 circular dependency (template/instantiate6.C). */
21948 const_init
21949 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21950 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
21951 /*asmspec_tree=*/NULL_TREE,
21952 LOOKUP_ONLYCONVERTING);
21953 }
21954 if (enter_context)
21955 pop_nested_class ();
21956 pop_nested_namespace (ns);
21957 }
21958
21959 /* We restore the source position here because it's used by
21960 add_pending_template. */
21961 input_location = saved_loc;
21962
21963 if (at_eof && !pattern_defined
21964 && DECL_EXPLICIT_INSTANTIATION (d)
21965 && DECL_NOT_REALLY_EXTERN (d))
21966 /* [temp.explicit]
21967
21968 The definition of a non-exported function template, a
21969 non-exported member function template, or a non-exported
21970 member function or static data member of a class template
21971 shall be present in every translation unit in which it is
21972 explicitly instantiated. */
21973 permerror (input_location, "explicit instantiation of %qD "
21974 "but no definition available", d);
21975
21976 /* If we're in unevaluated context, we just wanted to get the
21977 constant value; this isn't an odr use, so don't queue
21978 a full instantiation. */
21979 if (cp_unevaluated_operand != 0)
21980 goto out;
21981 /* ??? Historically, we have instantiated inline functions, even
21982 when marked as "extern template". */
21983 if (!(external_p && VAR_P (d)))
21984 add_pending_template (d);
21985 goto out;
21986 }
21987 /* Tell the repository that D is available in this translation unit
21988 -- and see if it is supposed to be instantiated here. */
21989 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
21990 {
21991 /* In a PCH file, despite the fact that the repository hasn't
21992 requested instantiation in the PCH it is still possible that
21993 an instantiation will be required in a file that includes the
21994 PCH. */
21995 if (pch_file)
21996 add_pending_template (d);
21997 /* Instantiate inline functions so that the inliner can do its
21998 job, even though we'll not be emitting a copy of this
21999 function. */
22000 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22001 goto out;
22002 }
22003
22004 fn_context = decl_function_context (d);
22005 nested = (current_function_decl != NULL_TREE);
22006 vec<tree> omp_privatization_save;
22007 if (nested)
22008 save_omp_privatization_clauses (omp_privatization_save);
22009
22010 if (!fn_context)
22011 push_to_top_level ();
22012 else
22013 {
22014 if (nested)
22015 push_function_context ();
22016 cp_unevaluated_operand = 0;
22017 c_inhibit_evaluation_warnings = 0;
22018 }
22019
22020 /* Mark D as instantiated so that recursive calls to
22021 instantiate_decl do not try to instantiate it again. */
22022 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22023
22024 /* Regenerate the declaration in case the template has been modified
22025 by a subsequent redeclaration. */
22026 regenerate_decl_from_template (d, td, args);
22027
22028 /* We already set the file and line above. Reset them now in case
22029 they changed as a result of calling regenerate_decl_from_template. */
22030 input_location = DECL_SOURCE_LOCATION (d);
22031
22032 if (VAR_P (d))
22033 {
22034 tree init;
22035 bool const_init = false;
22036
22037 /* Clear out DECL_RTL; whatever was there before may not be right
22038 since we've reset the type of the declaration. */
22039 SET_DECL_RTL (d, NULL);
22040 DECL_IN_AGGR_P (d) = 0;
22041
22042 /* The initializer is placed in DECL_INITIAL by
22043 regenerate_decl_from_template so we don't need to
22044 push/pop_access_scope again here. Pull it out so that
22045 cp_finish_decl can process it. */
22046 init = DECL_INITIAL (d);
22047 DECL_INITIAL (d) = NULL_TREE;
22048 DECL_INITIALIZED_P (d) = 0;
22049
22050 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22051 initializer. That function will defer actual emission until
22052 we have a chance to determine linkage. */
22053 DECL_EXTERNAL (d) = 0;
22054
22055 /* Enter the scope of D so that access-checking works correctly. */
22056 bool enter_context = DECL_CLASS_SCOPE_P (d);
22057 if (enter_context)
22058 push_nested_class (DECL_CONTEXT (d));
22059
22060 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22061 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22062
22063 if (enter_context)
22064 pop_nested_class ();
22065
22066 if (variable_template_p (gen_tmpl))
22067 note_variable_template_instantiation (d);
22068 }
22069 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22070 synthesize_method (d);
22071 else if (TREE_CODE (d) == FUNCTION_DECL)
22072 {
22073 hash_map<tree, tree> *saved_local_specializations;
22074 tree tmpl_parm;
22075 tree spec_parm;
22076 tree block = NULL_TREE;
22077
22078 /* Save away the current list, in case we are instantiating one
22079 template from within the body of another. */
22080 saved_local_specializations = local_specializations;
22081
22082 /* Set up the list of local specializations. */
22083 local_specializations = new hash_map<tree, tree>;
22084
22085 /* Set up context. */
22086 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22087 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22088 block = push_stmt_list ();
22089 else
22090 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22091
22092 /* Some typedefs referenced from within the template code need to be
22093 access checked at template instantiation time, i.e now. These
22094 types were added to the template at parsing time. Let's get those
22095 and perform the access checks then. */
22096 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22097 args);
22098
22099 /* Create substitution entries for the parameters. */
22100 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22101 spec_parm = DECL_ARGUMENTS (d);
22102 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22103 {
22104 register_local_specialization (spec_parm, tmpl_parm);
22105 spec_parm = skip_artificial_parms_for (d, spec_parm);
22106 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22107 }
22108 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22109 {
22110 if (!DECL_PACK_P (tmpl_parm))
22111 {
22112 register_local_specialization (spec_parm, tmpl_parm);
22113 spec_parm = DECL_CHAIN (spec_parm);
22114 }
22115 else
22116 {
22117 /* Register the (value) argument pack as a specialization of
22118 TMPL_PARM, then move on. */
22119 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22120 register_local_specialization (argpack, tmpl_parm);
22121 }
22122 }
22123 gcc_assert (!spec_parm);
22124
22125 /* Substitute into the body of the function. */
22126 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22127 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22128 tf_warning_or_error, tmpl);
22129 else
22130 {
22131 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22132 tf_warning_or_error, tmpl,
22133 /*integral_constant_expression_p=*/false);
22134
22135 /* Set the current input_location to the end of the function
22136 so that finish_function knows where we are. */
22137 input_location
22138 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22139
22140 /* Remember if we saw an infinite loop in the template. */
22141 current_function_infinite_loop
22142 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22143 }
22144
22145 /* We don't need the local specializations any more. */
22146 delete local_specializations;
22147 local_specializations = saved_local_specializations;
22148
22149 /* Finish the function. */
22150 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22151 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22152 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22153 else
22154 {
22155 d = finish_function (0);
22156 expand_or_defer_fn (d);
22157 }
22158
22159 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22160 cp_check_omp_declare_reduction (d);
22161 }
22162
22163 /* We're not deferring instantiation any more. */
22164 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22165
22166 if (!fn_context)
22167 pop_from_top_level ();
22168 else if (nested)
22169 pop_function_context ();
22170
22171 out:
22172 input_location = saved_loc;
22173 cp_unevaluated_operand = saved_unevaluated_operand;
22174 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22175 pop_deferring_access_checks ();
22176 pop_tinst_level ();
22177 if (nested)
22178 restore_omp_privatization_clauses (omp_privatization_save);
22179
22180 timevar_pop (TV_TEMPLATE_INST);
22181
22182 return d;
22183 }
22184
22185 /* Run through the list of templates that we wish we could
22186 instantiate, and instantiate any we can. RETRIES is the
22187 number of times we retry pending template instantiation. */
22188
22189 void
22190 instantiate_pending_templates (int retries)
22191 {
22192 int reconsider;
22193 location_t saved_loc = input_location;
22194
22195 /* Instantiating templates may trigger vtable generation. This in turn
22196 may require further template instantiations. We place a limit here
22197 to avoid infinite loop. */
22198 if (pending_templates && retries >= max_tinst_depth)
22199 {
22200 tree decl = pending_templates->tinst->decl;
22201
22202 fatal_error (input_location,
22203 "template instantiation depth exceeds maximum of %d"
22204 " instantiating %q+D, possibly from virtual table generation"
22205 " (use -ftemplate-depth= to increase the maximum)",
22206 max_tinst_depth, decl);
22207 if (TREE_CODE (decl) == FUNCTION_DECL)
22208 /* Pretend that we defined it. */
22209 DECL_INITIAL (decl) = error_mark_node;
22210 return;
22211 }
22212
22213 do
22214 {
22215 struct pending_template **t = &pending_templates;
22216 struct pending_template *last = NULL;
22217 reconsider = 0;
22218 while (*t)
22219 {
22220 tree instantiation = reopen_tinst_level ((*t)->tinst);
22221 bool complete = false;
22222
22223 if (TYPE_P (instantiation))
22224 {
22225 tree fn;
22226
22227 if (!COMPLETE_TYPE_P (instantiation))
22228 {
22229 instantiate_class_template (instantiation);
22230 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22231 for (fn = TYPE_METHODS (instantiation);
22232 fn;
22233 fn = TREE_CHAIN (fn))
22234 if (! DECL_ARTIFICIAL (fn))
22235 instantiate_decl (fn,
22236 /*defer_ok=*/0,
22237 /*expl_inst_class_mem_p=*/false);
22238 if (COMPLETE_TYPE_P (instantiation))
22239 reconsider = 1;
22240 }
22241
22242 complete = COMPLETE_TYPE_P (instantiation);
22243 }
22244 else
22245 {
22246 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22247 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22248 {
22249 instantiation
22250 = instantiate_decl (instantiation,
22251 /*defer_ok=*/0,
22252 /*expl_inst_class_mem_p=*/false);
22253 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22254 reconsider = 1;
22255 }
22256
22257 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22258 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22259 }
22260
22261 if (complete)
22262 /* If INSTANTIATION has been instantiated, then we don't
22263 need to consider it again in the future. */
22264 *t = (*t)->next;
22265 else
22266 {
22267 last = *t;
22268 t = &(*t)->next;
22269 }
22270 tinst_depth = 0;
22271 current_tinst_level = NULL;
22272 }
22273 last_pending_template = last;
22274 }
22275 while (reconsider);
22276
22277 input_location = saved_loc;
22278 }
22279
22280 /* Substitute ARGVEC into T, which is a list of initializers for
22281 either base class or a non-static data member. The TREE_PURPOSEs
22282 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22283 instantiate_decl. */
22284
22285 static tree
22286 tsubst_initializer_list (tree t, tree argvec)
22287 {
22288 tree inits = NULL_TREE;
22289
22290 for (; t; t = TREE_CHAIN (t))
22291 {
22292 tree decl;
22293 tree init;
22294 tree expanded_bases = NULL_TREE;
22295 tree expanded_arguments = NULL_TREE;
22296 int i, len = 1;
22297
22298 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
22299 {
22300 tree expr;
22301 tree arg;
22302
22303 /* Expand the base class expansion type into separate base
22304 classes. */
22305 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
22306 tf_warning_or_error,
22307 NULL_TREE);
22308 if (expanded_bases == error_mark_node)
22309 continue;
22310
22311 /* We'll be building separate TREE_LISTs of arguments for
22312 each base. */
22313 len = TREE_VEC_LENGTH (expanded_bases);
22314 expanded_arguments = make_tree_vec (len);
22315 for (i = 0; i < len; i++)
22316 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
22317
22318 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22319 expand each argument in the TREE_VALUE of t. */
22320 expr = make_node (EXPR_PACK_EXPANSION);
22321 PACK_EXPANSION_LOCAL_P (expr) = true;
22322 PACK_EXPANSION_PARAMETER_PACKS (expr) =
22323 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
22324
22325 if (TREE_VALUE (t) == void_type_node)
22326 /* VOID_TYPE_NODE is used to indicate
22327 value-initialization. */
22328 {
22329 for (i = 0; i < len; i++)
22330 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
22331 }
22332 else
22333 {
22334 /* Substitute parameter packs into each argument in the
22335 TREE_LIST. */
22336 in_base_initializer = 1;
22337 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
22338 {
22339 tree expanded_exprs;
22340
22341 /* Expand the argument. */
22342 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
22343 expanded_exprs
22344 = tsubst_pack_expansion (expr, argvec,
22345 tf_warning_or_error,
22346 NULL_TREE);
22347 if (expanded_exprs == error_mark_node)
22348 continue;
22349
22350 /* Prepend each of the expanded expressions to the
22351 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22352 for (i = 0; i < len; i++)
22353 {
22354 TREE_VEC_ELT (expanded_arguments, i) =
22355 tree_cons (NULL_TREE,
22356 TREE_VEC_ELT (expanded_exprs, i),
22357 TREE_VEC_ELT (expanded_arguments, i));
22358 }
22359 }
22360 in_base_initializer = 0;
22361
22362 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22363 since we built them backwards. */
22364 for (i = 0; i < len; i++)
22365 {
22366 TREE_VEC_ELT (expanded_arguments, i) =
22367 nreverse (TREE_VEC_ELT (expanded_arguments, i));
22368 }
22369 }
22370 }
22371
22372 for (i = 0; i < len; ++i)
22373 {
22374 if (expanded_bases)
22375 {
22376 decl = TREE_VEC_ELT (expanded_bases, i);
22377 decl = expand_member_init (decl);
22378 init = TREE_VEC_ELT (expanded_arguments, i);
22379 }
22380 else
22381 {
22382 tree tmp;
22383 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
22384 tf_warning_or_error, NULL_TREE);
22385
22386 decl = expand_member_init (decl);
22387 if (decl && !DECL_P (decl))
22388 in_base_initializer = 1;
22389
22390 init = TREE_VALUE (t);
22391 tmp = init;
22392 if (init != void_type_node)
22393 init = tsubst_expr (init, argvec,
22394 tf_warning_or_error, NULL_TREE,
22395 /*integral_constant_expression_p=*/false);
22396 if (init == NULL_TREE && tmp != NULL_TREE)
22397 /* If we had an initializer but it instantiated to nothing,
22398 value-initialize the object. This will only occur when
22399 the initializer was a pack expansion where the parameter
22400 packs used in that expansion were of length zero. */
22401 init = void_type_node;
22402 in_base_initializer = 0;
22403 }
22404
22405 if (decl)
22406 {
22407 init = build_tree_list (decl, init);
22408 TREE_CHAIN (init) = inits;
22409 inits = init;
22410 }
22411 }
22412 }
22413 return inits;
22414 }
22415
22416 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22417
22418 static void
22419 set_current_access_from_decl (tree decl)
22420 {
22421 if (TREE_PRIVATE (decl))
22422 current_access_specifier = access_private_node;
22423 else if (TREE_PROTECTED (decl))
22424 current_access_specifier = access_protected_node;
22425 else
22426 current_access_specifier = access_public_node;
22427 }
22428
22429 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22430 is the instantiation (which should have been created with
22431 start_enum) and ARGS are the template arguments to use. */
22432
22433 static void
22434 tsubst_enum (tree tag, tree newtag, tree args)
22435 {
22436 tree e;
22437
22438 if (SCOPED_ENUM_P (newtag))
22439 begin_scope (sk_scoped_enum, newtag);
22440
22441 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
22442 {
22443 tree value;
22444 tree decl;
22445
22446 decl = TREE_VALUE (e);
22447 /* Note that in a template enum, the TREE_VALUE is the
22448 CONST_DECL, not the corresponding INTEGER_CST. */
22449 value = tsubst_expr (DECL_INITIAL (decl),
22450 args, tf_warning_or_error, NULL_TREE,
22451 /*integral_constant_expression_p=*/true);
22452
22453 /* Give this enumeration constant the correct access. */
22454 set_current_access_from_decl (decl);
22455
22456 /* Actually build the enumerator itself. Here we're assuming that
22457 enumerators can't have dependent attributes. */
22458 build_enumerator (DECL_NAME (decl), value, newtag,
22459 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
22460 }
22461
22462 if (SCOPED_ENUM_P (newtag))
22463 finish_scope ();
22464
22465 finish_enum_value_list (newtag);
22466 finish_enum (newtag);
22467
22468 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
22469 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
22470 }
22471
22472 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22473 its type -- but without substituting the innermost set of template
22474 arguments. So, innermost set of template parameters will appear in
22475 the type. */
22476
22477 tree
22478 get_mostly_instantiated_function_type (tree decl)
22479 {
22480 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22481 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
22482 }
22483
22484 /* Return truthvalue if we're processing a template different from
22485 the last one involved in diagnostics. */
22486 bool
22487 problematic_instantiation_changed (void)
22488 {
22489 return current_tinst_level != last_error_tinst_level;
22490 }
22491
22492 /* Remember current template involved in diagnostics. */
22493 void
22494 record_last_problematic_instantiation (void)
22495 {
22496 last_error_tinst_level = current_tinst_level;
22497 }
22498
22499 struct tinst_level *
22500 current_instantiation (void)
22501 {
22502 return current_tinst_level;
22503 }
22504
22505 /* Return TRUE if current_function_decl is being instantiated, false
22506 otherwise. */
22507
22508 bool
22509 instantiating_current_function_p (void)
22510 {
22511 return (current_instantiation ()
22512 && current_instantiation ()->decl == current_function_decl);
22513 }
22514
22515 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22516 type. Return zero for ok, nonzero for disallowed. Issue error and
22517 warning messages under control of COMPLAIN. */
22518
22519 static int
22520 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
22521 {
22522 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
22523 return 0;
22524 else if (POINTER_TYPE_P (type))
22525 return 0;
22526 else if (TYPE_PTRMEM_P (type))
22527 return 0;
22528 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
22529 return 0;
22530 else if (TREE_CODE (type) == TYPENAME_TYPE)
22531 return 0;
22532 else if (TREE_CODE (type) == DECLTYPE_TYPE)
22533 return 0;
22534 else if (TREE_CODE (type) == NULLPTR_TYPE)
22535 return 0;
22536 /* A bound template template parm could later be instantiated to have a valid
22537 nontype parm type via an alias template. */
22538 else if (cxx_dialect >= cxx11
22539 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22540 return 0;
22541
22542 if (complain & tf_error)
22543 {
22544 if (type == error_mark_node)
22545 inform (input_location, "invalid template non-type parameter");
22546 else
22547 error ("%q#T is not a valid type for a template non-type parameter",
22548 type);
22549 }
22550 return 1;
22551 }
22552
22553 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22554 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22555
22556 static bool
22557 dependent_type_p_r (tree type)
22558 {
22559 tree scope;
22560
22561 /* [temp.dep.type]
22562
22563 A type is dependent if it is:
22564
22565 -- a template parameter. Template template parameters are types
22566 for us (since TYPE_P holds true for them) so we handle
22567 them here. */
22568 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22569 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
22570 return true;
22571 /* -- a qualified-id with a nested-name-specifier which contains a
22572 class-name that names a dependent type or whose unqualified-id
22573 names a dependent type. */
22574 if (TREE_CODE (type) == TYPENAME_TYPE)
22575 return true;
22576
22577 /* An alias template specialization can be dependent even if the
22578 resulting type is not. */
22579 if (dependent_alias_template_spec_p (type))
22580 return true;
22581
22582 /* -- a cv-qualified type where the cv-unqualified type is
22583 dependent.
22584 No code is necessary for this bullet; the code below handles
22585 cv-qualified types, and we don't want to strip aliases with
22586 TYPE_MAIN_VARIANT because of DR 1558. */
22587 /* -- a compound type constructed from any dependent type. */
22588 if (TYPE_PTRMEM_P (type))
22589 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
22590 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22591 (type)));
22592 else if (TYPE_PTR_P (type)
22593 || TREE_CODE (type) == REFERENCE_TYPE)
22594 return dependent_type_p (TREE_TYPE (type));
22595 else if (TREE_CODE (type) == FUNCTION_TYPE
22596 || TREE_CODE (type) == METHOD_TYPE)
22597 {
22598 tree arg_type;
22599
22600 if (dependent_type_p (TREE_TYPE (type)))
22601 return true;
22602 for (arg_type = TYPE_ARG_TYPES (type);
22603 arg_type;
22604 arg_type = TREE_CHAIN (arg_type))
22605 if (dependent_type_p (TREE_VALUE (arg_type)))
22606 return true;
22607 return false;
22608 }
22609 /* -- an array type constructed from any dependent type or whose
22610 size is specified by a constant expression that is
22611 value-dependent.
22612
22613 We checked for type- and value-dependence of the bounds in
22614 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22615 if (TREE_CODE (type) == ARRAY_TYPE)
22616 {
22617 if (TYPE_DOMAIN (type)
22618 && dependent_type_p (TYPE_DOMAIN (type)))
22619 return true;
22620 return dependent_type_p (TREE_TYPE (type));
22621 }
22622
22623 /* -- a template-id in which either the template name is a template
22624 parameter ... */
22625 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22626 return true;
22627 /* ... or any of the template arguments is a dependent type or
22628 an expression that is type-dependent or value-dependent. */
22629 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
22630 && (any_dependent_template_arguments_p
22631 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
22632 return true;
22633
22634 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22635 dependent; if the argument of the `typeof' expression is not
22636 type-dependent, then it should already been have resolved. */
22637 if (TREE_CODE (type) == TYPEOF_TYPE
22638 || TREE_CODE (type) == DECLTYPE_TYPE
22639 || TREE_CODE (type) == UNDERLYING_TYPE)
22640 return true;
22641
22642 /* A template argument pack is dependent if any of its packed
22643 arguments are. */
22644 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
22645 {
22646 tree args = ARGUMENT_PACK_ARGS (type);
22647 int i, len = TREE_VEC_LENGTH (args);
22648 for (i = 0; i < len; ++i)
22649 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22650 return true;
22651 }
22652
22653 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22654 be template parameters. */
22655 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
22656 return true;
22657
22658 /* The standard does not specifically mention types that are local
22659 to template functions or local classes, but they should be
22660 considered dependent too. For example:
22661
22662 template <int I> void f() {
22663 enum E { a = I };
22664 S<sizeof (E)> s;
22665 }
22666
22667 The size of `E' cannot be known until the value of `I' has been
22668 determined. Therefore, `E' must be considered dependent. */
22669 scope = TYPE_CONTEXT (type);
22670 if (scope && TYPE_P (scope))
22671 return dependent_type_p (scope);
22672 /* Don't use type_dependent_expression_p here, as it can lead
22673 to infinite recursion trying to determine whether a lambda
22674 nested in a lambda is dependent (c++/47687). */
22675 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
22676 && DECL_LANG_SPECIFIC (scope)
22677 && DECL_TEMPLATE_INFO (scope)
22678 && (any_dependent_template_arguments_p
22679 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
22680 return true;
22681
22682 /* Other types are non-dependent. */
22683 return false;
22684 }
22685
22686 /* Returns TRUE if TYPE is dependent, in the sense of
22687 [temp.dep.type]. Note that a NULL type is considered dependent. */
22688
22689 bool
22690 dependent_type_p (tree type)
22691 {
22692 /* If there are no template parameters in scope, then there can't be
22693 any dependent types. */
22694 if (!processing_template_decl)
22695 {
22696 /* If we are not processing a template, then nobody should be
22697 providing us with a dependent type. */
22698 gcc_assert (type);
22699 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
22700 return false;
22701 }
22702
22703 /* If the type is NULL, we have not computed a type for the entity
22704 in question; in that case, the type is dependent. */
22705 if (!type)
22706 return true;
22707
22708 /* Erroneous types can be considered non-dependent. */
22709 if (type == error_mark_node)
22710 return false;
22711
22712 /* If we have not already computed the appropriate value for TYPE,
22713 do so now. */
22714 if (!TYPE_DEPENDENT_P_VALID (type))
22715 {
22716 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
22717 TYPE_DEPENDENT_P_VALID (type) = 1;
22718 }
22719
22720 return TYPE_DEPENDENT_P (type);
22721 }
22722
22723 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22724 lookup. In other words, a dependent type that is not the current
22725 instantiation. */
22726
22727 bool
22728 dependent_scope_p (tree scope)
22729 {
22730 return (scope && TYPE_P (scope) && dependent_type_p (scope)
22731 && !currently_open_class (scope));
22732 }
22733
22734 /* T is a SCOPE_REF; return whether we need to consider it
22735 instantiation-dependent so that we can check access at instantiation
22736 time even though we know which member it resolves to. */
22737
22738 static bool
22739 instantiation_dependent_scope_ref_p (tree t)
22740 {
22741 if (DECL_P (TREE_OPERAND (t, 1))
22742 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
22743 && accessible_in_template_p (TREE_OPERAND (t, 0),
22744 TREE_OPERAND (t, 1)))
22745 return false;
22746 else
22747 return true;
22748 }
22749
22750 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22751 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22752 expression. */
22753
22754 /* Note that this predicate is not appropriate for general expressions;
22755 only constant expressions (that satisfy potential_constant_expression)
22756 can be tested for value dependence. */
22757
22758 bool
22759 value_dependent_expression_p (tree expression)
22760 {
22761 if (!processing_template_decl)
22762 return false;
22763
22764 /* A name declared with a dependent type. */
22765 if (DECL_P (expression) && type_dependent_expression_p (expression))
22766 return true;
22767
22768 switch (TREE_CODE (expression))
22769 {
22770 case BASELINK:
22771 /* A dependent member function of the current instantiation. */
22772 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
22773
22774 case FUNCTION_DECL:
22775 /* A dependent member function of the current instantiation. */
22776 if (DECL_CLASS_SCOPE_P (expression)
22777 && dependent_type_p (DECL_CONTEXT (expression)))
22778 return true;
22779 break;
22780
22781 case IDENTIFIER_NODE:
22782 /* A name that has not been looked up -- must be dependent. */
22783 return true;
22784
22785 case TEMPLATE_PARM_INDEX:
22786 /* A non-type template parm. */
22787 return true;
22788
22789 case CONST_DECL:
22790 /* A non-type template parm. */
22791 if (DECL_TEMPLATE_PARM_P (expression))
22792 return true;
22793 return value_dependent_expression_p (DECL_INITIAL (expression));
22794
22795 case VAR_DECL:
22796 /* A constant with literal type and is initialized
22797 with an expression that is value-dependent.
22798
22799 Note that a non-dependent parenthesized initializer will have
22800 already been replaced with its constant value, so if we see
22801 a TREE_LIST it must be dependent. */
22802 if (DECL_INITIAL (expression)
22803 && decl_constant_var_p (expression)
22804 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
22805 /* cp_finish_decl doesn't fold reference initializers. */
22806 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
22807 || type_dependent_expression_p (DECL_INITIAL (expression))
22808 || value_dependent_expression_p (DECL_INITIAL (expression))))
22809 return true;
22810 return false;
22811
22812 case DYNAMIC_CAST_EXPR:
22813 case STATIC_CAST_EXPR:
22814 case CONST_CAST_EXPR:
22815 case REINTERPRET_CAST_EXPR:
22816 case CAST_EXPR:
22817 /* These expressions are value-dependent if the type to which
22818 the cast occurs is dependent or the expression being casted
22819 is value-dependent. */
22820 {
22821 tree type = TREE_TYPE (expression);
22822
22823 if (dependent_type_p (type))
22824 return true;
22825
22826 /* A functional cast has a list of operands. */
22827 expression = TREE_OPERAND (expression, 0);
22828 if (!expression)
22829 {
22830 /* If there are no operands, it must be an expression such
22831 as "int()". This should not happen for aggregate types
22832 because it would form non-constant expressions. */
22833 gcc_assert (cxx_dialect >= cxx11
22834 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
22835
22836 return false;
22837 }
22838
22839 if (TREE_CODE (expression) == TREE_LIST)
22840 return any_value_dependent_elements_p (expression);
22841
22842 return value_dependent_expression_p (expression);
22843 }
22844
22845 case SIZEOF_EXPR:
22846 if (SIZEOF_EXPR_TYPE_P (expression))
22847 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
22848 /* FALLTHRU */
22849 case ALIGNOF_EXPR:
22850 case TYPEID_EXPR:
22851 /* A `sizeof' expression is value-dependent if the operand is
22852 type-dependent or is a pack expansion. */
22853 expression = TREE_OPERAND (expression, 0);
22854 if (PACK_EXPANSION_P (expression))
22855 return true;
22856 else if (TYPE_P (expression))
22857 return dependent_type_p (expression);
22858 return instantiation_dependent_uneval_expression_p (expression);
22859
22860 case AT_ENCODE_EXPR:
22861 /* An 'encode' expression is value-dependent if the operand is
22862 type-dependent. */
22863 expression = TREE_OPERAND (expression, 0);
22864 return dependent_type_p (expression);
22865
22866 case NOEXCEPT_EXPR:
22867 expression = TREE_OPERAND (expression, 0);
22868 return instantiation_dependent_uneval_expression_p (expression);
22869
22870 case SCOPE_REF:
22871 /* All instantiation-dependent expressions should also be considered
22872 value-dependent. */
22873 return instantiation_dependent_scope_ref_p (expression);
22874
22875 case COMPONENT_REF:
22876 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
22877 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
22878
22879 case NONTYPE_ARGUMENT_PACK:
22880 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22881 is value-dependent. */
22882 {
22883 tree values = ARGUMENT_PACK_ARGS (expression);
22884 int i, len = TREE_VEC_LENGTH (values);
22885
22886 for (i = 0; i < len; ++i)
22887 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
22888 return true;
22889
22890 return false;
22891 }
22892
22893 case TRAIT_EXPR:
22894 {
22895 tree type2 = TRAIT_EXPR_TYPE2 (expression);
22896 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
22897 || (type2 ? dependent_type_p (type2) : false));
22898 }
22899
22900 case MODOP_EXPR:
22901 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22902 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
22903
22904 case ARRAY_REF:
22905 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22906 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
22907
22908 case ADDR_EXPR:
22909 {
22910 tree op = TREE_OPERAND (expression, 0);
22911 return (value_dependent_expression_p (op)
22912 || has_value_dependent_address (op));
22913 }
22914
22915 case REQUIRES_EXPR:
22916 /* Treat all requires-expressions as value-dependent so
22917 we don't try to fold them. */
22918 return true;
22919
22920 case TYPE_REQ:
22921 return dependent_type_p (TREE_OPERAND (expression, 0));
22922
22923 case CALL_EXPR:
22924 {
22925 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
22926 return true;
22927 tree fn = get_callee_fndecl (expression);
22928 int i, nargs;
22929 nargs = call_expr_nargs (expression);
22930 for (i = 0; i < nargs; ++i)
22931 {
22932 tree op = CALL_EXPR_ARG (expression, i);
22933 /* In a call to a constexpr member function, look through the
22934 implicit ADDR_EXPR on the object argument so that it doesn't
22935 cause the call to be considered value-dependent. We also
22936 look through it in potential_constant_expression. */
22937 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
22938 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
22939 && TREE_CODE (op) == ADDR_EXPR)
22940 op = TREE_OPERAND (op, 0);
22941 if (value_dependent_expression_p (op))
22942 return true;
22943 }
22944 return false;
22945 }
22946
22947 case TEMPLATE_ID_EXPR:
22948 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22949 type-dependent. */
22950 return type_dependent_expression_p (expression)
22951 || variable_concept_p (TREE_OPERAND (expression, 0));
22952
22953 case CONSTRUCTOR:
22954 {
22955 unsigned ix;
22956 tree val;
22957 if (dependent_type_p (TREE_TYPE (expression)))
22958 return true;
22959 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
22960 if (value_dependent_expression_p (val))
22961 return true;
22962 return false;
22963 }
22964
22965 case STMT_EXPR:
22966 /* Treat a GNU statement expression as dependent to avoid crashing
22967 under instantiate_non_dependent_expr; it can't be constant. */
22968 return true;
22969
22970 default:
22971 /* A constant expression is value-dependent if any subexpression is
22972 value-dependent. */
22973 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
22974 {
22975 case tcc_reference:
22976 case tcc_unary:
22977 case tcc_comparison:
22978 case tcc_binary:
22979 case tcc_expression:
22980 case tcc_vl_exp:
22981 {
22982 int i, len = cp_tree_operand_length (expression);
22983
22984 for (i = 0; i < len; i++)
22985 {
22986 tree t = TREE_OPERAND (expression, i);
22987
22988 /* In some cases, some of the operands may be missing.l
22989 (For example, in the case of PREDECREMENT_EXPR, the
22990 amount to increment by may be missing.) That doesn't
22991 make the expression dependent. */
22992 if (t && value_dependent_expression_p (t))
22993 return true;
22994 }
22995 }
22996 break;
22997 default:
22998 break;
22999 }
23000 break;
23001 }
23002
23003 /* The expression is not value-dependent. */
23004 return false;
23005 }
23006
23007 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23008 [temp.dep.expr]. Note that an expression with no type is
23009 considered dependent. Other parts of the compiler arrange for an
23010 expression with type-dependent subexpressions to have no type, so
23011 this function doesn't have to be fully recursive. */
23012
23013 bool
23014 type_dependent_expression_p (tree expression)
23015 {
23016 if (!processing_template_decl)
23017 return false;
23018
23019 if (expression == NULL_TREE || expression == error_mark_node)
23020 return false;
23021
23022 /* An unresolved name is always dependent. */
23023 if (identifier_p (expression)
23024 || TREE_CODE (expression) == USING_DECL
23025 || TREE_CODE (expression) == WILDCARD_DECL)
23026 return true;
23027
23028 /* A fold expression is type-dependent. */
23029 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23030 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23031 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23032 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23033 return true;
23034
23035 /* Some expression forms are never type-dependent. */
23036 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23037 || TREE_CODE (expression) == SIZEOF_EXPR
23038 || TREE_CODE (expression) == ALIGNOF_EXPR
23039 || TREE_CODE (expression) == AT_ENCODE_EXPR
23040 || TREE_CODE (expression) == NOEXCEPT_EXPR
23041 || TREE_CODE (expression) == TRAIT_EXPR
23042 || TREE_CODE (expression) == TYPEID_EXPR
23043 || TREE_CODE (expression) == DELETE_EXPR
23044 || TREE_CODE (expression) == VEC_DELETE_EXPR
23045 || TREE_CODE (expression) == THROW_EXPR
23046 || TREE_CODE (expression) == REQUIRES_EXPR)
23047 return false;
23048
23049 /* The types of these expressions depends only on the type to which
23050 the cast occurs. */
23051 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23052 || TREE_CODE (expression) == STATIC_CAST_EXPR
23053 || TREE_CODE (expression) == CONST_CAST_EXPR
23054 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23055 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23056 || TREE_CODE (expression) == CAST_EXPR)
23057 return dependent_type_p (TREE_TYPE (expression));
23058
23059 /* The types of these expressions depends only on the type created
23060 by the expression. */
23061 if (TREE_CODE (expression) == NEW_EXPR
23062 || TREE_CODE (expression) == VEC_NEW_EXPR)
23063 {
23064 /* For NEW_EXPR tree nodes created inside a template, either
23065 the object type itself or a TREE_LIST may appear as the
23066 operand 1. */
23067 tree type = TREE_OPERAND (expression, 1);
23068 if (TREE_CODE (type) == TREE_LIST)
23069 /* This is an array type. We need to check array dimensions
23070 as well. */
23071 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23072 || value_dependent_expression_p
23073 (TREE_OPERAND (TREE_VALUE (type), 1));
23074 else
23075 return dependent_type_p (type);
23076 }
23077
23078 if (TREE_CODE (expression) == SCOPE_REF)
23079 {
23080 tree scope = TREE_OPERAND (expression, 0);
23081 tree name = TREE_OPERAND (expression, 1);
23082
23083 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23084 contains an identifier associated by name lookup with one or more
23085 declarations declared with a dependent type, or...a
23086 nested-name-specifier or qualified-id that names a member of an
23087 unknown specialization. */
23088 return (type_dependent_expression_p (name)
23089 || dependent_scope_p (scope));
23090 }
23091
23092 if (TREE_CODE (expression) == TEMPLATE_DECL
23093 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23094 return uses_outer_template_parms (expression);
23095
23096 if (TREE_CODE (expression) == STMT_EXPR)
23097 expression = stmt_expr_value_expr (expression);
23098
23099 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23100 {
23101 tree elt;
23102 unsigned i;
23103
23104 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23105 {
23106 if (type_dependent_expression_p (elt))
23107 return true;
23108 }
23109 return false;
23110 }
23111
23112 /* A static data member of the current instantiation with incomplete
23113 array type is type-dependent, as the definition and specializations
23114 can have different bounds. */
23115 if (VAR_P (expression)
23116 && DECL_CLASS_SCOPE_P (expression)
23117 && dependent_type_p (DECL_CONTEXT (expression))
23118 && VAR_HAD_UNKNOWN_BOUND (expression))
23119 return true;
23120
23121 /* An array of unknown bound depending on a variadic parameter, eg:
23122
23123 template<typename... Args>
23124 void foo (Args... args)
23125 {
23126 int arr[] = { args... };
23127 }
23128
23129 template<int... vals>
23130 void bar ()
23131 {
23132 int arr[] = { vals... };
23133 }
23134
23135 If the array has no length and has an initializer, it must be that
23136 we couldn't determine its length in cp_complete_array_type because
23137 it is dependent. */
23138 if (VAR_P (expression)
23139 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23140 && !TYPE_DOMAIN (TREE_TYPE (expression))
23141 && DECL_INITIAL (expression))
23142 return true;
23143
23144 /* A function or variable template-id is type-dependent if it has any
23145 dependent template arguments. Note that we only consider the innermost
23146 template arguments here, since those are the ones that come from the
23147 template-id; the template arguments for the enclosing class do not make it
23148 type-dependent, they only make a member function value-dependent. */
23149 if (VAR_OR_FUNCTION_DECL_P (expression)
23150 && DECL_LANG_SPECIFIC (expression)
23151 && DECL_TEMPLATE_INFO (expression)
23152 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23153 && (any_dependent_template_arguments_p
23154 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23155 return true;
23156
23157 /* Always dependent, on the number of arguments if nothing else. */
23158 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23159 return true;
23160
23161 if (TREE_TYPE (expression) == unknown_type_node)
23162 {
23163 if (TREE_CODE (expression) == ADDR_EXPR)
23164 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23165 if (TREE_CODE (expression) == COMPONENT_REF
23166 || TREE_CODE (expression) == OFFSET_REF)
23167 {
23168 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23169 return true;
23170 expression = TREE_OPERAND (expression, 1);
23171 if (identifier_p (expression))
23172 return false;
23173 }
23174 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23175 if (TREE_CODE (expression) == SCOPE_REF)
23176 return false;
23177
23178 if (BASELINK_P (expression))
23179 {
23180 if (BASELINK_OPTYPE (expression)
23181 && dependent_type_p (BASELINK_OPTYPE (expression)))
23182 return true;
23183 expression = BASELINK_FUNCTIONS (expression);
23184 }
23185
23186 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23187 {
23188 if (any_dependent_template_arguments_p
23189 (TREE_OPERAND (expression, 1)))
23190 return true;
23191 expression = TREE_OPERAND (expression, 0);
23192 if (identifier_p (expression))
23193 return true;
23194 }
23195
23196 gcc_assert (TREE_CODE (expression) == OVERLOAD
23197 || TREE_CODE (expression) == FUNCTION_DECL);
23198
23199 while (expression)
23200 {
23201 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23202 return true;
23203 expression = OVL_NEXT (expression);
23204 }
23205 return false;
23206 }
23207
23208 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23209
23210 return (dependent_type_p (TREE_TYPE (expression)));
23211 }
23212
23213 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23214 type-dependent if the expression refers to a member of the current
23215 instantiation and the type of the referenced member is dependent, or the
23216 class member access expression refers to a member of an unknown
23217 specialization.
23218
23219 This function returns true if the OBJECT in such a class member access
23220 expression is of an unknown specialization. */
23221
23222 bool
23223 type_dependent_object_expression_p (tree object)
23224 {
23225 tree scope = TREE_TYPE (object);
23226 return (!scope || dependent_scope_p (scope));
23227 }
23228
23229 /* walk_tree callback function for instantiation_dependent_expression_p,
23230 below. Returns non-zero if a dependent subexpression is found. */
23231
23232 static tree
23233 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23234 void * /*data*/)
23235 {
23236 if (TYPE_P (*tp))
23237 {
23238 /* We don't have to worry about decltype currently because decltype
23239 of an instantiation-dependent expr is a dependent type. This
23240 might change depending on the resolution of DR 1172. */
23241 *walk_subtrees = false;
23242 return NULL_TREE;
23243 }
23244 enum tree_code code = TREE_CODE (*tp);
23245 switch (code)
23246 {
23247 /* Don't treat an argument list as dependent just because it has no
23248 TREE_TYPE. */
23249 case TREE_LIST:
23250 case TREE_VEC:
23251 return NULL_TREE;
23252
23253 case TEMPLATE_PARM_INDEX:
23254 return *tp;
23255
23256 /* Handle expressions with type operands. */
23257 case SIZEOF_EXPR:
23258 case ALIGNOF_EXPR:
23259 case TYPEID_EXPR:
23260 case AT_ENCODE_EXPR:
23261 {
23262 tree op = TREE_OPERAND (*tp, 0);
23263 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
23264 op = TREE_TYPE (op);
23265 if (TYPE_P (op))
23266 {
23267 if (dependent_type_p (op))
23268 return *tp;
23269 else
23270 {
23271 *walk_subtrees = false;
23272 return NULL_TREE;
23273 }
23274 }
23275 break;
23276 }
23277
23278 case COMPONENT_REF:
23279 if (identifier_p (TREE_OPERAND (*tp, 1)))
23280 /* In a template, finish_class_member_access_expr creates a
23281 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
23282 type-dependent, so that we can check access control at
23283 instantiation time (PR 42277). See also Core issue 1273. */
23284 return *tp;
23285 break;
23286
23287 case SCOPE_REF:
23288 if (instantiation_dependent_scope_ref_p (*tp))
23289 return *tp;
23290 else
23291 break;
23292
23293 /* Treat statement-expressions as dependent. */
23294 case BIND_EXPR:
23295 return *tp;
23296
23297 /* Treat requires-expressions as dependent. */
23298 case REQUIRES_EXPR:
23299 return *tp;
23300
23301 case CALL_EXPR:
23302 /* Treat calls to function concepts as dependent. */
23303 if (function_concept_check_p (*tp))
23304 return *tp;
23305 break;
23306
23307 case TEMPLATE_ID_EXPR:
23308 /* And variable concepts. */
23309 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
23310 return *tp;
23311 break;
23312
23313 default:
23314 break;
23315 }
23316
23317 if (type_dependent_expression_p (*tp))
23318 return *tp;
23319 else
23320 return NULL_TREE;
23321 }
23322
23323 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23324 sense defined by the ABI:
23325
23326 "An expression is instantiation-dependent if it is type-dependent
23327 or value-dependent, or it has a subexpression that is type-dependent
23328 or value-dependent."
23329
23330 Except don't actually check value-dependence for unevaluated expressions,
23331 because in sizeof(i) we don't care about the value of i. Checking
23332 type-dependence will in turn check value-dependence of array bounds/template
23333 arguments as needed. */
23334
23335 bool
23336 instantiation_dependent_uneval_expression_p (tree expression)
23337 {
23338 tree result;
23339
23340 if (!processing_template_decl)
23341 return false;
23342
23343 if (expression == error_mark_node)
23344 return false;
23345
23346 result = cp_walk_tree_without_duplicates (&expression,
23347 instantiation_dependent_r, NULL);
23348 return result != NULL_TREE;
23349 }
23350
23351 /* As above, but also check value-dependence of the expression as a whole. */
23352
23353 bool
23354 instantiation_dependent_expression_p (tree expression)
23355 {
23356 return (instantiation_dependent_uneval_expression_p (expression)
23357 || value_dependent_expression_p (expression));
23358 }
23359
23360 /* Like type_dependent_expression_p, but it also works while not processing
23361 a template definition, i.e. during substitution or mangling. */
23362
23363 bool
23364 type_dependent_expression_p_push (tree expr)
23365 {
23366 bool b;
23367 ++processing_template_decl;
23368 b = type_dependent_expression_p (expr);
23369 --processing_template_decl;
23370 return b;
23371 }
23372
23373 /* Returns TRUE if ARGS contains a type-dependent expression. */
23374
23375 bool
23376 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
23377 {
23378 unsigned int i;
23379 tree arg;
23380
23381 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
23382 {
23383 if (type_dependent_expression_p (arg))
23384 return true;
23385 }
23386 return false;
23387 }
23388
23389 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23390 expressions) contains any type-dependent expressions. */
23391
23392 bool
23393 any_type_dependent_elements_p (const_tree list)
23394 {
23395 for (; list; list = TREE_CHAIN (list))
23396 if (type_dependent_expression_p (TREE_VALUE (list)))
23397 return true;
23398
23399 return false;
23400 }
23401
23402 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23403 expressions) contains any value-dependent expressions. */
23404
23405 bool
23406 any_value_dependent_elements_p (const_tree list)
23407 {
23408 for (; list; list = TREE_CHAIN (list))
23409 if (value_dependent_expression_p (TREE_VALUE (list)))
23410 return true;
23411
23412 return false;
23413 }
23414
23415 /* Returns TRUE if the ARG (a template argument) is dependent. */
23416
23417 bool
23418 dependent_template_arg_p (tree arg)
23419 {
23420 if (!processing_template_decl)
23421 return false;
23422
23423 /* Assume a template argument that was wrongly written by the user
23424 is dependent. This is consistent with what
23425 any_dependent_template_arguments_p [that calls this function]
23426 does. */
23427 if (!arg || arg == error_mark_node)
23428 return true;
23429
23430 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
23431 arg = ARGUMENT_PACK_SELECT_ARG (arg);
23432
23433 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
23434 return true;
23435 if (TREE_CODE (arg) == TEMPLATE_DECL)
23436 {
23437 if (DECL_TEMPLATE_PARM_P (arg))
23438 return true;
23439 /* A member template of a dependent class is not necessarily
23440 type-dependent, but it is a dependent template argument because it
23441 will be a member of an unknown specialization to that template. */
23442 tree scope = CP_DECL_CONTEXT (arg);
23443 return TYPE_P (scope) && dependent_type_p (scope);
23444 }
23445 else if (ARGUMENT_PACK_P (arg))
23446 {
23447 tree args = ARGUMENT_PACK_ARGS (arg);
23448 int i, len = TREE_VEC_LENGTH (args);
23449 for (i = 0; i < len; ++i)
23450 {
23451 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23452 return true;
23453 }
23454
23455 return false;
23456 }
23457 else if (TYPE_P (arg))
23458 return dependent_type_p (arg);
23459 else
23460 return (type_dependent_expression_p (arg)
23461 || value_dependent_expression_p (arg));
23462 }
23463
23464 /* Returns true if ARGS (a collection of template arguments) contains
23465 any types that require structural equality testing. */
23466
23467 bool
23468 any_template_arguments_need_structural_equality_p (tree args)
23469 {
23470 int i;
23471 int j;
23472
23473 if (!args)
23474 return false;
23475 if (args == error_mark_node)
23476 return true;
23477
23478 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23479 {
23480 tree level = TMPL_ARGS_LEVEL (args, i + 1);
23481 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23482 {
23483 tree arg = TREE_VEC_ELT (level, j);
23484 tree packed_args = NULL_TREE;
23485 int k, len = 1;
23486
23487 if (ARGUMENT_PACK_P (arg))
23488 {
23489 /* Look inside the argument pack. */
23490 packed_args = ARGUMENT_PACK_ARGS (arg);
23491 len = TREE_VEC_LENGTH (packed_args);
23492 }
23493
23494 for (k = 0; k < len; ++k)
23495 {
23496 if (packed_args)
23497 arg = TREE_VEC_ELT (packed_args, k);
23498
23499 if (error_operand_p (arg))
23500 return true;
23501 else if (TREE_CODE (arg) == TEMPLATE_DECL)
23502 continue;
23503 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
23504 return true;
23505 else if (!TYPE_P (arg) && TREE_TYPE (arg)
23506 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
23507 return true;
23508 }
23509 }
23510 }
23511
23512 return false;
23513 }
23514
23515 /* Returns true if ARGS (a collection of template arguments) contains
23516 any dependent arguments. */
23517
23518 bool
23519 any_dependent_template_arguments_p (const_tree args)
23520 {
23521 int i;
23522 int j;
23523
23524 if (!args)
23525 return false;
23526 if (args == error_mark_node)
23527 return true;
23528
23529 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23530 {
23531 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
23532 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23533 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
23534 return true;
23535 }
23536
23537 return false;
23538 }
23539
23540 /* Returns TRUE if the template TMPL is type-dependent. */
23541
23542 bool
23543 dependent_template_p (tree tmpl)
23544 {
23545 if (TREE_CODE (tmpl) == OVERLOAD)
23546 {
23547 while (tmpl)
23548 {
23549 if (dependent_template_p (OVL_CURRENT (tmpl)))
23550 return true;
23551 tmpl = OVL_NEXT (tmpl);
23552 }
23553 return false;
23554 }
23555
23556 /* Template template parameters are dependent. */
23557 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
23558 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
23559 return true;
23560 /* So are names that have not been looked up. */
23561 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
23562 return true;
23563 return false;
23564 }
23565
23566 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23567
23568 bool
23569 dependent_template_id_p (tree tmpl, tree args)
23570 {
23571 return (dependent_template_p (tmpl)
23572 || any_dependent_template_arguments_p (args));
23573 }
23574
23575 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23576 are dependent. */
23577
23578 bool
23579 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
23580 {
23581 int i;
23582
23583 if (!processing_template_decl)
23584 return false;
23585
23586 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
23587 {
23588 tree decl = TREE_VEC_ELT (declv, i);
23589 tree init = TREE_VEC_ELT (initv, i);
23590 tree cond = TREE_VEC_ELT (condv, i);
23591 tree incr = TREE_VEC_ELT (incrv, i);
23592
23593 if (type_dependent_expression_p (decl)
23594 || TREE_CODE (decl) == SCOPE_REF)
23595 return true;
23596
23597 if (init && type_dependent_expression_p (init))
23598 return true;
23599
23600 if (type_dependent_expression_p (cond))
23601 return true;
23602
23603 if (COMPARISON_CLASS_P (cond)
23604 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
23605 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
23606 return true;
23607
23608 if (TREE_CODE (incr) == MODOP_EXPR)
23609 {
23610 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
23611 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
23612 return true;
23613 }
23614 else if (type_dependent_expression_p (incr))
23615 return true;
23616 else if (TREE_CODE (incr) == MODIFY_EXPR)
23617 {
23618 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
23619 return true;
23620 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
23621 {
23622 tree t = TREE_OPERAND (incr, 1);
23623 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
23624 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
23625 return true;
23626 }
23627 }
23628 }
23629
23630 return false;
23631 }
23632
23633 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23634 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23635 no such TYPE can be found. Note that this function peers inside
23636 uninstantiated templates and therefore should be used only in
23637 extremely limited situations. ONLY_CURRENT_P restricts this
23638 peering to the currently open classes hierarchy (which is required
23639 when comparing types). */
23640
23641 tree
23642 resolve_typename_type (tree type, bool only_current_p)
23643 {
23644 tree scope;
23645 tree name;
23646 tree decl;
23647 int quals;
23648 tree pushed_scope;
23649 tree result;
23650
23651 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
23652
23653 scope = TYPE_CONTEXT (type);
23654 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23655 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23656 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23657 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23658 identifier of the TYPENAME_TYPE anymore.
23659 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23660 TYPENAME_TYPE instead, we avoid messing up with a possible
23661 typedef variant case. */
23662 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
23663
23664 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23665 it first before we can figure out what NAME refers to. */
23666 if (TREE_CODE (scope) == TYPENAME_TYPE)
23667 {
23668 if (TYPENAME_IS_RESOLVING_P (scope))
23669 /* Given a class template A with a dependent base with nested type C,
23670 typedef typename A::C::C C will land us here, as trying to resolve
23671 the initial A::C leads to the local C typedef, which leads back to
23672 A::C::C. So we break the recursion now. */
23673 return type;
23674 else
23675 scope = resolve_typename_type (scope, only_current_p);
23676 }
23677 /* If we don't know what SCOPE refers to, then we cannot resolve the
23678 TYPENAME_TYPE. */
23679 if (!CLASS_TYPE_P (scope))
23680 return type;
23681 /* If this is a typedef, we don't want to look inside (c++/11987). */
23682 if (typedef_variant_p (type))
23683 return type;
23684 /* If SCOPE isn't the template itself, it will not have a valid
23685 TYPE_FIELDS list. */
23686 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
23687 /* scope is either the template itself or a compatible instantiation
23688 like X<T>, so look up the name in the original template. */
23689 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
23690 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
23691 gcc_checking_assert (uses_template_parms (scope));
23692 /* If scope has no fields, it can't be a current instantiation. Check this
23693 before currently_open_class to avoid infinite recursion (71515). */
23694 if (!TYPE_FIELDS (scope))
23695 return type;
23696 /* If the SCOPE is not the current instantiation, there's no reason
23697 to look inside it. */
23698 if (only_current_p && !currently_open_class (scope))
23699 return type;
23700 /* Enter the SCOPE so that name lookup will be resolved as if we
23701 were in the class definition. In particular, SCOPE will no
23702 longer be considered a dependent type. */
23703 pushed_scope = push_scope (scope);
23704 /* Look up the declaration. */
23705 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
23706 tf_warning_or_error);
23707
23708 result = NULL_TREE;
23709
23710 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23711 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23712 if (!decl)
23713 /*nop*/;
23714 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
23715 && TREE_CODE (decl) == TYPE_DECL)
23716 {
23717 result = TREE_TYPE (decl);
23718 if (result == error_mark_node)
23719 result = NULL_TREE;
23720 }
23721 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
23722 && DECL_CLASS_TEMPLATE_P (decl))
23723 {
23724 tree tmpl;
23725 tree args;
23726 /* Obtain the template and the arguments. */
23727 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
23728 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
23729 /* Instantiate the template. */
23730 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
23731 /*entering_scope=*/0,
23732 tf_error | tf_user);
23733 if (result == error_mark_node)
23734 result = NULL_TREE;
23735 }
23736
23737 /* Leave the SCOPE. */
23738 if (pushed_scope)
23739 pop_scope (pushed_scope);
23740
23741 /* If we failed to resolve it, return the original typename. */
23742 if (!result)
23743 return type;
23744
23745 /* If lookup found a typename type, resolve that too. */
23746 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
23747 {
23748 /* Ill-formed programs can cause infinite recursion here, so we
23749 must catch that. */
23750 TYPENAME_IS_RESOLVING_P (result) = 1;
23751 result = resolve_typename_type (result, only_current_p);
23752 TYPENAME_IS_RESOLVING_P (result) = 0;
23753 }
23754
23755 /* Qualify the resulting type. */
23756 quals = cp_type_quals (type);
23757 if (quals)
23758 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
23759
23760 return result;
23761 }
23762
23763 /* EXPR is an expression which is not type-dependent. Return a proxy
23764 for EXPR that can be used to compute the types of larger
23765 expressions containing EXPR. */
23766
23767 tree
23768 build_non_dependent_expr (tree expr)
23769 {
23770 tree inner_expr;
23771
23772 /* When checking, try to get a constant value for all non-dependent
23773 expressions in order to expose bugs in *_dependent_expression_p
23774 and constexpr. This can affect code generation, see PR70704, so
23775 only do this for -fchecking=2. */
23776 if (flag_checking > 1
23777 && cxx_dialect >= cxx11
23778 /* Don't do this during nsdmi parsing as it can lead to
23779 unexpected recursive instantiations. */
23780 && !parsing_nsdmi ()
23781 /* Don't do this during concept expansion either and for
23782 the same reason. */
23783 && !expanding_concept ())
23784 fold_non_dependent_expr (expr);
23785
23786 /* Preserve OVERLOADs; the functions must be available to resolve
23787 types. */
23788 inner_expr = expr;
23789 if (TREE_CODE (inner_expr) == STMT_EXPR)
23790 inner_expr = stmt_expr_value_expr (inner_expr);
23791 if (TREE_CODE (inner_expr) == ADDR_EXPR)
23792 inner_expr = TREE_OPERAND (inner_expr, 0);
23793 if (TREE_CODE (inner_expr) == COMPONENT_REF)
23794 inner_expr = TREE_OPERAND (inner_expr, 1);
23795 if (is_overloaded_fn (inner_expr)
23796 || TREE_CODE (inner_expr) == OFFSET_REF)
23797 return expr;
23798 /* There is no need to return a proxy for a variable. */
23799 if (VAR_P (expr))
23800 return expr;
23801 /* Preserve string constants; conversions from string constants to
23802 "char *" are allowed, even though normally a "const char *"
23803 cannot be used to initialize a "char *". */
23804 if (TREE_CODE (expr) == STRING_CST)
23805 return expr;
23806 /* Preserve void and arithmetic constants, as an optimization -- there is no
23807 reason to create a new node. */
23808 if (TREE_CODE (expr) == VOID_CST
23809 || TREE_CODE (expr) == INTEGER_CST
23810 || TREE_CODE (expr) == REAL_CST)
23811 return expr;
23812 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23813 There is at least one place where we want to know that a
23814 particular expression is a throw-expression: when checking a ?:
23815 expression, there are special rules if the second or third
23816 argument is a throw-expression. */
23817 if (TREE_CODE (expr) == THROW_EXPR)
23818 return expr;
23819
23820 /* Don't wrap an initializer list, we need to be able to look inside. */
23821 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
23822 return expr;
23823
23824 /* Don't wrap a dummy object, we need to be able to test for it. */
23825 if (is_dummy_object (expr))
23826 return expr;
23827
23828 if (TREE_CODE (expr) == COND_EXPR)
23829 return build3 (COND_EXPR,
23830 TREE_TYPE (expr),
23831 TREE_OPERAND (expr, 0),
23832 (TREE_OPERAND (expr, 1)
23833 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
23834 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
23835 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
23836 if (TREE_CODE (expr) == COMPOUND_EXPR
23837 && !COMPOUND_EXPR_OVERLOADED (expr))
23838 return build2 (COMPOUND_EXPR,
23839 TREE_TYPE (expr),
23840 TREE_OPERAND (expr, 0),
23841 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
23842
23843 /* If the type is unknown, it can't really be non-dependent */
23844 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
23845
23846 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23847 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
23848 }
23849
23850 /* ARGS is a vector of expressions as arguments to a function call.
23851 Replace the arguments with equivalent non-dependent expressions.
23852 This modifies ARGS in place. */
23853
23854 void
23855 make_args_non_dependent (vec<tree, va_gc> *args)
23856 {
23857 unsigned int ix;
23858 tree arg;
23859
23860 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
23861 {
23862 tree newarg = build_non_dependent_expr (arg);
23863 if (newarg != arg)
23864 (*args)[ix] = newarg;
23865 }
23866 }
23867
23868 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23869 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23870 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23871
23872 static tree
23873 make_auto_1 (tree name, bool set_canonical)
23874 {
23875 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
23876 TYPE_NAME (au) = build_decl (input_location,
23877 TYPE_DECL, name, au);
23878 TYPE_STUB_DECL (au) = TYPE_NAME (au);
23879 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
23880 (0, processing_template_decl + 1, processing_template_decl + 1,
23881 TYPE_NAME (au), NULL_TREE);
23882 if (set_canonical)
23883 TYPE_CANONICAL (au) = canonical_type_parameter (au);
23884 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
23885 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
23886
23887 return au;
23888 }
23889
23890 tree
23891 make_decltype_auto (void)
23892 {
23893 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23894 }
23895
23896 tree
23897 make_auto (void)
23898 {
23899 return make_auto_1 (get_identifier ("auto"), true);
23900 }
23901
23902 /* Make a "constrained auto" type-specifier. This is an
23903 auto type with constraints that must be associated after
23904 deduction. The constraint is formed from the given
23905 CONC and its optional sequence of arguments, which are
23906 non-null if written as partial-concept-id. */
23907
23908 tree
23909 make_constrained_auto (tree con, tree args)
23910 {
23911 tree type = make_auto_1 (get_identifier ("auto"), false);
23912
23913 /* Build the constraint. */
23914 tree tmpl = DECL_TI_TEMPLATE (con);
23915 tree expr;
23916 if (VAR_P (con))
23917 expr = build_concept_check (tmpl, type, args);
23918 else
23919 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
23920
23921 tree constr = normalize_expression (expr);
23922 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
23923
23924 /* Our canonical type depends on the constraint. */
23925 TYPE_CANONICAL (type) = canonical_type_parameter (type);
23926
23927 /* Attach the constraint to the type declaration. */
23928 tree decl = TYPE_NAME (type);
23929 return decl;
23930 }
23931
23932 /* Given type ARG, return std::initializer_list<ARG>. */
23933
23934 static tree
23935 listify (tree arg)
23936 {
23937 tree std_init_list = namespace_binding
23938 (get_identifier ("initializer_list"), std_node);
23939 tree argvec;
23940 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
23941 {
23942 error ("deducing from brace-enclosed initializer list requires "
23943 "#include <initializer_list>");
23944 return error_mark_node;
23945 }
23946 argvec = make_tree_vec (1);
23947 TREE_VEC_ELT (argvec, 0) = arg;
23948 return lookup_template_class (std_init_list, argvec, NULL_TREE,
23949 NULL_TREE, 0, tf_warning_or_error);
23950 }
23951
23952 /* Replace auto in TYPE with std::initializer_list<auto>. */
23953
23954 static tree
23955 listify_autos (tree type, tree auto_node)
23956 {
23957 tree init_auto = listify (auto_node);
23958 tree argvec = make_tree_vec (1);
23959 TREE_VEC_ELT (argvec, 0) = init_auto;
23960 if (processing_template_decl)
23961 argvec = add_to_template_args (current_template_args (), argvec);
23962 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
23963 }
23964
23965 /* Hash traits for hashing possibly constrained 'auto'
23966 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23967
23968 struct auto_hash : default_hash_traits<tree>
23969 {
23970 static inline hashval_t hash (tree);
23971 static inline bool equal (tree, tree);
23972 };
23973
23974 /* Hash the 'auto' T. */
23975
23976 inline hashval_t
23977 auto_hash::hash (tree t)
23978 {
23979 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
23980 /* Matching constrained-type-specifiers denote the same template
23981 parameter, so hash the constraint. */
23982 return hash_placeholder_constraint (c);
23983 else
23984 /* But unconstrained autos are all separate, so just hash the pointer. */
23985 return iterative_hash_object (t, 0);
23986 }
23987
23988 /* Compare two 'auto's. */
23989
23990 inline bool
23991 auto_hash::equal (tree t1, tree t2)
23992 {
23993 if (t1 == t2)
23994 return true;
23995
23996 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
23997 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
23998
23999 /* Two unconstrained autos are distinct. */
24000 if (!c1 || !c2)
24001 return false;
24002
24003 return equivalent_placeholder_constraints (c1, c2);
24004 }
24005
24006 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24007 constrained) auto, add it to the vector. */
24008
24009 static int
24010 extract_autos_r (tree t, void *data)
24011 {
24012 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24013 if (is_auto_or_concept (t))
24014 {
24015 /* All the autos were built with index 0; fix that up now. */
24016 tree *p = hash.find_slot (t, INSERT);
24017 unsigned idx;
24018 if (*p)
24019 /* If this is a repeated constrained-type-specifier, use the index we
24020 chose before. */
24021 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24022 else
24023 {
24024 /* Otherwise this is new, so use the current count. */
24025 *p = t;
24026 idx = hash.elements () - 1;
24027 }
24028 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24029 }
24030
24031 /* Always keep walking. */
24032 return 0;
24033 }
24034
24035 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24036 says they can appear anywhere in the type. */
24037
24038 static tree
24039 extract_autos (tree type)
24040 {
24041 hash_set<tree> visited;
24042 hash_table<auto_hash> hash (2);
24043
24044 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24045
24046 tree tree_vec = make_tree_vec (hash.elements());
24047 for (hash_table<auto_hash>::iterator iter = hash.begin();
24048 iter != hash.end(); ++iter)
24049 {
24050 tree elt = *iter;
24051 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24052 TREE_VEC_ELT (tree_vec, i)
24053 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24054 }
24055
24056 return tree_vec;
24057 }
24058
24059 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24060 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
24061
24062 tree
24063 do_auto_deduction (tree type, tree init, tree auto_node)
24064 {
24065 return do_auto_deduction (type, init, auto_node,
24066 tf_warning_or_error,
24067 adc_unspecified);
24068 }
24069
24070 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24071 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
24072 The CONTEXT determines the context in which auto deduction is performed
24073 and is used to control error diagnostics.
24074
24075 For partial-concept-ids, extra args may be appended to the list of deduced
24076 template arguments prior to determining constraint satisfaction. */
24077
24078 tree
24079 do_auto_deduction (tree type, tree init, tree auto_node,
24080 tsubst_flags_t complain, auto_deduction_context context)
24081 {
24082 tree targs;
24083
24084 if (init == error_mark_node)
24085 return error_mark_node;
24086
24087 if (type_dependent_expression_p (init))
24088 /* Defining a subset of type-dependent expressions that we can deduce
24089 from ahead of time isn't worth the trouble. */
24090 return type;
24091
24092 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
24093 with either a new invented type template parameter U or, if the
24094 initializer is a braced-init-list (8.5.4), with
24095 std::initializer_list<U>. */
24096 if (BRACE_ENCLOSED_INITIALIZER_P (init))
24097 {
24098 if (!DIRECT_LIST_INIT_P (init))
24099 type = listify_autos (type, auto_node);
24100 else if (CONSTRUCTOR_NELTS (init) == 1)
24101 init = CONSTRUCTOR_ELT (init, 0)->value;
24102 else
24103 {
24104 if (complain & tf_warning_or_error)
24105 {
24106 if (permerror (input_location, "direct-list-initialization of "
24107 "%<auto%> requires exactly one element"))
24108 inform (input_location,
24109 "for deduction to %<std::initializer_list%>, use copy-"
24110 "list-initialization (i.e. add %<=%> before the %<{%>)");
24111 }
24112 type = listify_autos (type, auto_node);
24113 }
24114 }
24115
24116 if (type == error_mark_node)
24117 return error_mark_node;
24118
24119 init = resolve_nondeduced_context (init, complain);
24120
24121 if (AUTO_IS_DECLTYPE (auto_node))
24122 {
24123 bool id = (DECL_P (init)
24124 || ((TREE_CODE (init) == COMPONENT_REF
24125 || TREE_CODE (init) == SCOPE_REF)
24126 && !REF_PARENTHESIZED_P (init)));
24127 targs = make_tree_vec (1);
24128 TREE_VEC_ELT (targs, 0)
24129 = finish_decltype_type (init, id, tf_warning_or_error);
24130 if (type != auto_node)
24131 {
24132 if (complain & tf_error)
24133 error ("%qT as type rather than plain %<decltype(auto)%>", type);
24134 return error_mark_node;
24135 }
24136 }
24137 else
24138 {
24139 tree parms = build_tree_list (NULL_TREE, type);
24140 tree tparms;
24141
24142 if (flag_concepts)
24143 tparms = extract_autos (type);
24144 else
24145 {
24146 tparms = make_tree_vec (1);
24147 TREE_VEC_ELT (tparms, 0)
24148 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
24149 }
24150
24151 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
24152 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
24153 DEDUCE_CALL, LOOKUP_NORMAL,
24154 NULL, /*explain_p=*/false);
24155 if (val > 0)
24156 {
24157 if (processing_template_decl)
24158 /* Try again at instantiation time. */
24159 return type;
24160 if (type && type != error_mark_node
24161 && (complain & tf_error))
24162 /* If type is error_mark_node a diagnostic must have been
24163 emitted by now. Also, having a mention to '<type error>'
24164 in the diagnostic is not really useful to the user. */
24165 {
24166 if (cfun && auto_node == current_function_auto_return_pattern
24167 && LAMBDA_FUNCTION_P (current_function_decl))
24168 error ("unable to deduce lambda return type from %qE", init);
24169 else
24170 error ("unable to deduce %qT from %qE", type, init);
24171 type_unification_real (tparms, targs, parms, &init, 1, 0,
24172 DEDUCE_CALL, LOOKUP_NORMAL,
24173 NULL, /*explain_p=*/true);
24174 }
24175 return error_mark_node;
24176 }
24177 }
24178
24179 /* Check any placeholder constraints against the deduced type. */
24180 if (flag_concepts && !processing_template_decl)
24181 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
24182 {
24183 /* Use the deduced type to check the associated constraints. If we
24184 have a partial-concept-id, rebuild the argument list so that
24185 we check using the extra arguments. */
24186 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
24187 tree cargs = CHECK_CONSTR_ARGS (constr);
24188 if (TREE_VEC_LENGTH (cargs) > 1)
24189 {
24190 cargs = copy_node (cargs);
24191 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
24192 }
24193 else
24194 cargs = targs;
24195 if (!constraints_satisfied_p (constr, cargs))
24196 {
24197 if (complain & tf_warning_or_error)
24198 {
24199 switch (context)
24200 {
24201 case adc_unspecified:
24202 error("placeholder constraints not satisfied");
24203 break;
24204 case adc_variable_type:
24205 error ("deduced initializer does not satisfy "
24206 "placeholder constraints");
24207 break;
24208 case adc_return_type:
24209 error ("deduced return type does not satisfy "
24210 "placeholder constraints");
24211 break;
24212 case adc_requirement:
24213 error ("deduced expression type does not saatisy "
24214 "placeholder constraints");
24215 break;
24216 }
24217 diagnose_constraints (input_location, constr, targs);
24218 }
24219 return error_mark_node;
24220 }
24221 }
24222
24223 if (processing_template_decl)
24224 targs = add_to_template_args (current_template_args (), targs);
24225 return tsubst (type, targs, complain, NULL_TREE);
24226 }
24227
24228 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
24229 result. */
24230
24231 tree
24232 splice_late_return_type (tree type, tree late_return_type)
24233 {
24234 if (is_auto (type))
24235 {
24236 if (late_return_type)
24237 return late_return_type;
24238
24239 tree idx = get_template_parm_index (type);
24240 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
24241 /* In an abbreviated function template we didn't know we were dealing
24242 with a function template when we saw the auto return type, so update
24243 it to have the correct level. */
24244 return make_auto_1 (TYPE_IDENTIFIER (type), true);
24245 }
24246 return type;
24247 }
24248
24249 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
24250 'decltype(auto)'. */
24251
24252 bool
24253 is_auto (const_tree type)
24254 {
24255 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24256 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
24257 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
24258 return true;
24259 else
24260 return false;
24261 }
24262
24263 /* for_each_template_parm callback for type_uses_auto. */
24264
24265 int
24266 is_auto_r (tree tp, void */*data*/)
24267 {
24268 return is_auto_or_concept (tp);
24269 }
24270
24271 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
24272 a use of `auto'. Returns NULL_TREE otherwise. */
24273
24274 tree
24275 type_uses_auto (tree type)
24276 {
24277 if (type == NULL_TREE)
24278 return NULL_TREE;
24279 else if (flag_concepts)
24280 {
24281 /* The Concepts TS allows multiple autos in one type-specifier; just
24282 return the first one we find, do_auto_deduction will collect all of
24283 them. */
24284 if (uses_template_parms (type))
24285 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
24286 /*visited*/NULL, /*nondeduced*/true);
24287 else
24288 return NULL_TREE;
24289 }
24290 else
24291 return find_type_usage (type, is_auto);
24292 }
24293
24294 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
24295 'decltype(auto)' or a concept. */
24296
24297 bool
24298 is_auto_or_concept (const_tree type)
24299 {
24300 return is_auto (type); // or concept
24301 }
24302
24303 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
24304 a concept identifier) iff TYPE contains a use of a generic type. Returns
24305 NULL_TREE otherwise. */
24306
24307 tree
24308 type_uses_auto_or_concept (tree type)
24309 {
24310 return find_type_usage (type, is_auto_or_concept);
24311 }
24312
24313
24314 /* For a given template T, return the vector of typedefs referenced
24315 in T for which access check is needed at T instantiation time.
24316 T is either a FUNCTION_DECL or a RECORD_TYPE.
24317 Those typedefs were added to T by the function
24318 append_type_to_template_for_access_check. */
24319
24320 vec<qualified_typedef_usage_t, va_gc> *
24321 get_types_needing_access_check (tree t)
24322 {
24323 tree ti;
24324 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
24325
24326 if (!t || t == error_mark_node)
24327 return NULL;
24328
24329 if (!(ti = get_template_info (t)))
24330 return NULL;
24331
24332 if (CLASS_TYPE_P (t)
24333 || TREE_CODE (t) == FUNCTION_DECL)
24334 {
24335 if (!TI_TEMPLATE (ti))
24336 return NULL;
24337
24338 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
24339 }
24340
24341 return result;
24342 }
24343
24344 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
24345 tied to T. That list of typedefs will be access checked at
24346 T instantiation time.
24347 T is either a FUNCTION_DECL or a RECORD_TYPE.
24348 TYPE_DECL is a TYPE_DECL node representing a typedef.
24349 SCOPE is the scope through which TYPE_DECL is accessed.
24350 LOCATION is the location of the usage point of TYPE_DECL.
24351
24352 This function is a subroutine of
24353 append_type_to_template_for_access_check. */
24354
24355 static void
24356 append_type_to_template_for_access_check_1 (tree t,
24357 tree type_decl,
24358 tree scope,
24359 location_t location)
24360 {
24361 qualified_typedef_usage_t typedef_usage;
24362 tree ti;
24363
24364 if (!t || t == error_mark_node)
24365 return;
24366
24367 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
24368 || CLASS_TYPE_P (t))
24369 && type_decl
24370 && TREE_CODE (type_decl) == TYPE_DECL
24371 && scope);
24372
24373 if (!(ti = get_template_info (t)))
24374 return;
24375
24376 gcc_assert (TI_TEMPLATE (ti));
24377
24378 typedef_usage.typedef_decl = type_decl;
24379 typedef_usage.context = scope;
24380 typedef_usage.locus = location;
24381
24382 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
24383 }
24384
24385 /* Append TYPE_DECL to the template TEMPL.
24386 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24387 At TEMPL instanciation time, TYPE_DECL will be checked to see
24388 if it can be accessed through SCOPE.
24389 LOCATION is the location of the usage point of TYPE_DECL.
24390
24391 e.g. consider the following code snippet:
24392
24393 class C
24394 {
24395 typedef int myint;
24396 };
24397
24398 template<class U> struct S
24399 {
24400 C::myint mi; // <-- usage point of the typedef C::myint
24401 };
24402
24403 S<char> s;
24404
24405 At S<char> instantiation time, we need to check the access of C::myint
24406 In other words, we need to check the access of the myint typedef through
24407 the C scope. For that purpose, this function will add the myint typedef
24408 and the scope C through which its being accessed to a list of typedefs
24409 tied to the template S. That list will be walked at template instantiation
24410 time and access check performed on each typedefs it contains.
24411 Note that this particular code snippet should yield an error because
24412 myint is private to C. */
24413
24414 void
24415 append_type_to_template_for_access_check (tree templ,
24416 tree type_decl,
24417 tree scope,
24418 location_t location)
24419 {
24420 qualified_typedef_usage_t *iter;
24421 unsigned i;
24422
24423 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
24424
24425 /* Make sure we don't append the type to the template twice. */
24426 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
24427 if (iter->typedef_decl == type_decl && scope == iter->context)
24428 return;
24429
24430 append_type_to_template_for_access_check_1 (templ, type_decl,
24431 scope, location);
24432 }
24433
24434 /* Convert the generic type parameters in PARM that match the types given in the
24435 range [START_IDX, END_IDX) from the current_template_parms into generic type
24436 packs. */
24437
24438 tree
24439 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
24440 {
24441 tree current = current_template_parms;
24442 int depth = TMPL_PARMS_DEPTH (current);
24443 current = INNERMOST_TEMPLATE_PARMS (current);
24444 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
24445
24446 for (int i = 0; i < start_idx; ++i)
24447 TREE_VEC_ELT (replacement, i)
24448 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24449
24450 for (int i = start_idx; i < end_idx; ++i)
24451 {
24452 /* Create a distinct parameter pack type from the current parm and add it
24453 to the replacement args to tsubst below into the generic function
24454 parameter. */
24455
24456 tree o = TREE_TYPE (TREE_VALUE
24457 (TREE_VEC_ELT (current, i)));
24458 tree t = copy_type (o);
24459 TEMPLATE_TYPE_PARM_INDEX (t)
24460 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
24461 o, 0, 0, tf_none);
24462 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
24463 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
24464 TYPE_MAIN_VARIANT (t) = t;
24465 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
24466 TYPE_CANONICAL (t) = canonical_type_parameter (t);
24467 TREE_VEC_ELT (replacement, i) = t;
24468 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
24469 }
24470
24471 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
24472 TREE_VEC_ELT (replacement, i)
24473 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24474
24475 /* If there are more levels then build up the replacement with the outer
24476 template parms. */
24477 if (depth > 1)
24478 replacement = add_to_template_args (template_parms_to_args
24479 (TREE_CHAIN (current_template_parms)),
24480 replacement);
24481
24482 return tsubst (parm, replacement, tf_none, NULL_TREE);
24483 }
24484
24485 /* Entries in the decl_constraint hash table. */
24486 struct GTY((for_user)) constr_entry
24487 {
24488 tree decl;
24489 tree ci;
24490 };
24491
24492 /* Hashing function and equality for constraint entries. */
24493 struct constr_hasher : ggc_ptr_hash<constr_entry>
24494 {
24495 static hashval_t hash (constr_entry *e)
24496 {
24497 return (hashval_t)DECL_UID (e->decl);
24498 }
24499
24500 static bool equal (constr_entry *e1, constr_entry *e2)
24501 {
24502 return e1->decl == e2->decl;
24503 }
24504 };
24505
24506 /* A mapping from declarations to constraint information. Note that
24507 both templates and their underlying declarations are mapped to the
24508 same constraint information.
24509
24510 FIXME: This is defined in pt.c because garbage collection
24511 code is not being generated for constraint.cc. */
24512
24513 static GTY (()) hash_table<constr_hasher> *decl_constraints;
24514
24515 /* Returns the template constraints of declaration T. If T is not
24516 constrained, return NULL_TREE. Note that T must be non-null. */
24517
24518 tree
24519 get_constraints (tree t)
24520 {
24521 if (!flag_concepts)
24522 return NULL_TREE;
24523
24524 gcc_assert (DECL_P (t));
24525 if (TREE_CODE (t) == TEMPLATE_DECL)
24526 t = DECL_TEMPLATE_RESULT (t);
24527 constr_entry elt = { t, NULL_TREE };
24528 constr_entry* found = decl_constraints->find (&elt);
24529 if (found)
24530 return found->ci;
24531 else
24532 return NULL_TREE;
24533 }
24534
24535 /* Associate the given constraint information CI with the declaration
24536 T. If T is a template, then the constraints are associated with
24537 its underlying declaration. Don't build associations if CI is
24538 NULL_TREE. */
24539
24540 void
24541 set_constraints (tree t, tree ci)
24542 {
24543 if (!ci)
24544 return;
24545 gcc_assert (t && flag_concepts);
24546 if (TREE_CODE (t) == TEMPLATE_DECL)
24547 t = DECL_TEMPLATE_RESULT (t);
24548 gcc_assert (!get_constraints (t));
24549 constr_entry elt = {t, ci};
24550 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
24551 constr_entry* entry = ggc_alloc<constr_entry> ();
24552 *entry = elt;
24553 *slot = entry;
24554 }
24555
24556 /* Remove the associated constraints of the declaration T. */
24557
24558 void
24559 remove_constraints (tree t)
24560 {
24561 gcc_assert (DECL_P (t));
24562 if (TREE_CODE (t) == TEMPLATE_DECL)
24563 t = DECL_TEMPLATE_RESULT (t);
24564
24565 constr_entry elt = {t, NULL_TREE};
24566 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
24567 if (slot)
24568 decl_constraints->clear_slot (slot);
24569 }
24570
24571 /* Memoized satisfaction results for declarations. This
24572 maps the pair (constraint_info, arguments) to the result computed
24573 by constraints_satisfied_p. */
24574
24575 struct GTY((for_user)) constraint_sat_entry
24576 {
24577 tree ci;
24578 tree args;
24579 tree result;
24580 };
24581
24582 /* Hashing function and equality for constraint entries. */
24583
24584 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
24585 {
24586 static hashval_t hash (constraint_sat_entry *e)
24587 {
24588 hashval_t val = iterative_hash_object(e->ci, 0);
24589 return iterative_hash_template_arg (e->args, val);
24590 }
24591
24592 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
24593 {
24594 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
24595 }
24596 };
24597
24598 /* Memoized satisfaction results for concept checks. */
24599
24600 struct GTY((for_user)) concept_spec_entry
24601 {
24602 tree tmpl;
24603 tree args;
24604 tree result;
24605 };
24606
24607 /* Hashing function and equality for constraint entries. */
24608
24609 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
24610 {
24611 static hashval_t hash (concept_spec_entry *e)
24612 {
24613 return hash_tmpl_and_args (e->tmpl, e->args);
24614 }
24615
24616 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
24617 {
24618 ++comparing_specializations;
24619 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
24620 --comparing_specializations;
24621 return eq;
24622 }
24623 };
24624
24625 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
24626 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
24627
24628 /* Search for a memoized satisfaction result. Returns one of the
24629 truth value nodes if previously memoized, or NULL_TREE otherwise. */
24630
24631 tree
24632 lookup_constraint_satisfaction (tree ci, tree args)
24633 {
24634 constraint_sat_entry elt = { ci, args, NULL_TREE };
24635 constraint_sat_entry* found = constraint_memos->find (&elt);
24636 if (found)
24637 return found->result;
24638 else
24639 return NULL_TREE;
24640 }
24641
24642 /* Memoize the result of a satisfication test. Returns the saved result. */
24643
24644 tree
24645 memoize_constraint_satisfaction (tree ci, tree args, tree result)
24646 {
24647 constraint_sat_entry elt = {ci, args, result};
24648 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
24649 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
24650 *entry = elt;
24651 *slot = entry;
24652 return result;
24653 }
24654
24655 /* Search for a memoized satisfaction result for a concept. */
24656
24657 tree
24658 lookup_concept_satisfaction (tree tmpl, tree args)
24659 {
24660 concept_spec_entry elt = { tmpl, args, NULL_TREE };
24661 concept_spec_entry* found = concept_memos->find (&elt);
24662 if (found)
24663 return found->result;
24664 else
24665 return NULL_TREE;
24666 }
24667
24668 /* Memoize the result of a concept check. Returns the saved result. */
24669
24670 tree
24671 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
24672 {
24673 concept_spec_entry elt = {tmpl, args, result};
24674 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
24675 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
24676 *entry = elt;
24677 *slot = entry;
24678 return result;
24679 }
24680
24681 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
24682
24683 /* Returns a prior concept specialization. This returns the substituted
24684 and normalized constraints defined by the concept. */
24685
24686 tree
24687 get_concept_expansion (tree tmpl, tree args)
24688 {
24689 concept_spec_entry elt = { tmpl, args, NULL_TREE };
24690 concept_spec_entry* found = concept_expansions->find (&elt);
24691 if (found)
24692 return found->result;
24693 else
24694 return NULL_TREE;
24695 }
24696
24697 /* Save a concept expansion for later. */
24698
24699 tree
24700 save_concept_expansion (tree tmpl, tree args, tree def)
24701 {
24702 concept_spec_entry elt = {tmpl, args, def};
24703 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
24704 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
24705 *entry = elt;
24706 *slot = entry;
24707 return def;
24708 }
24709
24710 static hashval_t
24711 hash_subsumption_args (tree t1, tree t2)
24712 {
24713 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
24714 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
24715 int val = 0;
24716 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
24717 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
24718 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
24719 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
24720 return val;
24721 }
24722
24723 /* Compare the constraints of two subsumption entries. The LEFT1 and
24724 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
24725 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
24726
24727 static bool
24728 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
24729 {
24730 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
24731 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
24732 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
24733 CHECK_CONSTR_ARGS (right1)))
24734 return comp_template_args (CHECK_CONSTR_ARGS (left2),
24735 CHECK_CONSTR_ARGS (right2));
24736 return false;
24737 }
24738
24739 /* Key/value pair for learning and memoizing subsumption results. This
24740 associates a pair of check constraints (including arguments) with
24741 a boolean value indicating the result. */
24742
24743 struct GTY((for_user)) subsumption_entry
24744 {
24745 tree t1;
24746 tree t2;
24747 bool result;
24748 };
24749
24750 /* Hashing function and equality for constraint entries. */
24751
24752 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
24753 {
24754 static hashval_t hash (subsumption_entry *e)
24755 {
24756 return hash_subsumption_args (e->t1, e->t2);
24757 }
24758
24759 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
24760 {
24761 ++comparing_specializations;
24762 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
24763 --comparing_specializations;
24764 return eq;
24765 }
24766 };
24767
24768 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
24769
24770 /* Search for a previously cached subsumption result. */
24771
24772 bool*
24773 lookup_subsumption_result (tree t1, tree t2)
24774 {
24775 subsumption_entry elt = { t1, t2, false };
24776 subsumption_entry* found = subsumption_table->find (&elt);
24777 if (found)
24778 return &found->result;
24779 else
24780 return 0;
24781 }
24782
24783 /* Save a subsumption result. */
24784
24785 bool
24786 save_subsumption_result (tree t1, tree t2, bool result)
24787 {
24788 subsumption_entry elt = {t1, t2, result};
24789 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
24790 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
24791 *entry = elt;
24792 *slot = entry;
24793 return result;
24794 }
24795
24796 /* Set up the hash table for constraint association. */
24797
24798 void
24799 init_constraint_processing (void)
24800 {
24801 if (!flag_concepts)
24802 return;
24803
24804 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
24805 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
24806 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
24807 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
24808 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
24809 }
24810
24811 /* Set up the hash tables for template instantiations. */
24812
24813 void
24814 init_template_processing (void)
24815 {
24816 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
24817 type_specializations = hash_table<spec_hasher>::create_ggc (37);
24818 }
24819
24820 /* Print stats about the template hash tables for -fstats. */
24821
24822 void
24823 print_template_statistics (void)
24824 {
24825 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
24826 "%f collisions\n", (long) decl_specializations->size (),
24827 (long) decl_specializations->elements (),
24828 decl_specializations->collisions ());
24829 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
24830 "%f collisions\n", (long) type_specializations->size (),
24831 (long) type_specializations->elements (),
24832 type_specializations->collisions ());
24833 }
24834
24835 #include "gt-cp-pt.h"