]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
a0cbb2e8c0ea1492a6d3dcf45070fcb3b838d6a2
[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 /* FALLTHRU */
3454 case TEMPLATE_TEMPLATE_PARM:
3455 /* If the placeholder appears in the decl-specifier-seq of a function
3456 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3457 is a pack expansion, the invented template parameter is a template
3458 parameter pack. */
3459 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3460 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3461 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3462 parameter_pack_p = true;
3463 break;
3464
3465 case FIELD_DECL:
3466 case PARM_DECL:
3467 if (DECL_PACK_P (t))
3468 {
3469 /* We don't want to walk into the type of a PARM_DECL,
3470 because we don't want to see the type parameter pack. */
3471 *walk_subtrees = 0;
3472 parameter_pack_p = true;
3473 }
3474 break;
3475
3476 /* Look through a lambda capture proxy to the field pack. */
3477 case VAR_DECL:
3478 if (DECL_HAS_VALUE_EXPR_P (t))
3479 {
3480 tree v = DECL_VALUE_EXPR (t);
3481 cp_walk_tree (&v,
3482 &find_parameter_packs_r,
3483 ppd, ppd->visited);
3484 *walk_subtrees = 0;
3485 }
3486 else if (variable_template_specialization_p (t))
3487 {
3488 cp_walk_tree (&DECL_TI_ARGS (t),
3489 find_parameter_packs_r,
3490 ppd, ppd->visited);
3491 *walk_subtrees = 0;
3492 }
3493 break;
3494
3495 case BASES:
3496 parameter_pack_p = true;
3497 break;
3498 default:
3499 /* Not a parameter pack. */
3500 break;
3501 }
3502
3503 if (parameter_pack_p)
3504 {
3505 /* Add this parameter pack to the list. */
3506 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3507 }
3508
3509 if (TYPE_P (t))
3510 cp_walk_tree (&TYPE_CONTEXT (t),
3511 &find_parameter_packs_r, ppd, ppd->visited);
3512
3513 /* This switch statement will return immediately if we don't find a
3514 parameter pack. */
3515 switch (TREE_CODE (t))
3516 {
3517 case TEMPLATE_PARM_INDEX:
3518 return NULL_TREE;
3519
3520 case BOUND_TEMPLATE_TEMPLATE_PARM:
3521 /* Check the template itself. */
3522 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3523 &find_parameter_packs_r, ppd, ppd->visited);
3524 /* Check the template arguments. */
3525 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3526 ppd->visited);
3527 *walk_subtrees = 0;
3528 return NULL_TREE;
3529
3530 case TEMPLATE_TYPE_PARM:
3531 case TEMPLATE_TEMPLATE_PARM:
3532 return NULL_TREE;
3533
3534 case PARM_DECL:
3535 return NULL_TREE;
3536
3537 case RECORD_TYPE:
3538 if (TYPE_PTRMEMFUNC_P (t))
3539 return NULL_TREE;
3540 /* Fall through. */
3541
3542 case UNION_TYPE:
3543 case ENUMERAL_TYPE:
3544 if (TYPE_TEMPLATE_INFO (t))
3545 cp_walk_tree (&TYPE_TI_ARGS (t),
3546 &find_parameter_packs_r, ppd, ppd->visited);
3547
3548 *walk_subtrees = 0;
3549 return NULL_TREE;
3550
3551 case CONSTRUCTOR:
3552 case TEMPLATE_DECL:
3553 cp_walk_tree (&TREE_TYPE (t),
3554 &find_parameter_packs_r, ppd, ppd->visited);
3555 return NULL_TREE;
3556
3557 case TYPENAME_TYPE:
3558 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3559 ppd, ppd->visited);
3560 *walk_subtrees = 0;
3561 return NULL_TREE;
3562
3563 case TYPE_PACK_EXPANSION:
3564 case EXPR_PACK_EXPANSION:
3565 *walk_subtrees = 0;
3566 return NULL_TREE;
3567
3568 case INTEGER_TYPE:
3569 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3570 ppd, ppd->visited);
3571 *walk_subtrees = 0;
3572 return NULL_TREE;
3573
3574 case IDENTIFIER_NODE:
3575 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3576 ppd->visited);
3577 *walk_subtrees = 0;
3578 return NULL_TREE;
3579
3580 case DECLTYPE_TYPE:
3581 {
3582 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3583 type_pack_expansion_p to false so that any placeholders
3584 within the expression don't get marked as parameter packs. */
3585 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3586 ppd->type_pack_expansion_p = false;
3587 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3588 ppd, ppd->visited);
3589 ppd->type_pack_expansion_p = type_pack_expansion_p;
3590 *walk_subtrees = 0;
3591 return NULL_TREE;
3592 }
3593
3594 default:
3595 return NULL_TREE;
3596 }
3597
3598 return NULL_TREE;
3599 }
3600
3601 /* Determines if the expression or type T uses any parameter packs. */
3602 bool
3603 uses_parameter_packs (tree t)
3604 {
3605 tree parameter_packs = NULL_TREE;
3606 struct find_parameter_pack_data ppd;
3607 ppd.parameter_packs = &parameter_packs;
3608 ppd.visited = new hash_set<tree>;
3609 ppd.type_pack_expansion_p = false;
3610 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3611 delete ppd.visited;
3612 return parameter_packs != NULL_TREE;
3613 }
3614
3615 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3616 representation a base-class initializer into a parameter pack
3617 expansion. If all goes well, the resulting node will be an
3618 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3619 respectively. */
3620 tree
3621 make_pack_expansion (tree arg)
3622 {
3623 tree result;
3624 tree parameter_packs = NULL_TREE;
3625 bool for_types = false;
3626 struct find_parameter_pack_data ppd;
3627
3628 if (!arg || arg == error_mark_node)
3629 return arg;
3630
3631 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3632 {
3633 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3634 class initializer. In this case, the TREE_PURPOSE will be a
3635 _TYPE node (representing the base class expansion we're
3636 initializing) and the TREE_VALUE will be a TREE_LIST
3637 containing the initialization arguments.
3638
3639 The resulting expansion looks somewhat different from most
3640 expansions. Rather than returning just one _EXPANSION, we
3641 return a TREE_LIST whose TREE_PURPOSE is a
3642 TYPE_PACK_EXPANSION containing the bases that will be
3643 initialized. The TREE_VALUE will be identical to the
3644 original TREE_VALUE, which is a list of arguments that will
3645 be passed to each base. We do not introduce any new pack
3646 expansion nodes into the TREE_VALUE (although it is possible
3647 that some already exist), because the TREE_PURPOSE and
3648 TREE_VALUE all need to be expanded together with the same
3649 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3650 resulting TREE_PURPOSE will mention the parameter packs in
3651 both the bases and the arguments to the bases. */
3652 tree purpose;
3653 tree value;
3654 tree parameter_packs = NULL_TREE;
3655
3656 /* Determine which parameter packs will be used by the base
3657 class expansion. */
3658 ppd.visited = new hash_set<tree>;
3659 ppd.parameter_packs = &parameter_packs;
3660 ppd.type_pack_expansion_p = true;
3661 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3662 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3663 &ppd, ppd.visited);
3664
3665 if (parameter_packs == NULL_TREE)
3666 {
3667 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3668 delete ppd.visited;
3669 return error_mark_node;
3670 }
3671
3672 if (TREE_VALUE (arg) != void_type_node)
3673 {
3674 /* Collect the sets of parameter packs used in each of the
3675 initialization arguments. */
3676 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3677 {
3678 /* Determine which parameter packs will be expanded in this
3679 argument. */
3680 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3681 &ppd, ppd.visited);
3682 }
3683 }
3684
3685 delete ppd.visited;
3686
3687 /* Create the pack expansion type for the base type. */
3688 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3689 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3690 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3691
3692 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3693 they will rarely be compared to anything. */
3694 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3695
3696 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3697 }
3698
3699 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3700 for_types = true;
3701
3702 /* Build the PACK_EXPANSION_* node. */
3703 result = for_types
3704 ? cxx_make_type (TYPE_PACK_EXPANSION)
3705 : make_node (EXPR_PACK_EXPANSION);
3706 SET_PACK_EXPANSION_PATTERN (result, arg);
3707 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3708 {
3709 /* Propagate type and const-expression information. */
3710 TREE_TYPE (result) = TREE_TYPE (arg);
3711 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3712 /* Mark this read now, since the expansion might be length 0. */
3713 mark_exp_read (arg);
3714 }
3715 else
3716 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3717 they will rarely be compared to anything. */
3718 SET_TYPE_STRUCTURAL_EQUALITY (result);
3719
3720 /* Determine which parameter packs will be expanded. */
3721 ppd.parameter_packs = &parameter_packs;
3722 ppd.visited = new hash_set<tree>;
3723 ppd.type_pack_expansion_p = TYPE_P (arg);
3724 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3725 delete ppd.visited;
3726
3727 /* Make sure we found some parameter packs. */
3728 if (parameter_packs == NULL_TREE)
3729 {
3730 if (TYPE_P (arg))
3731 error ("expansion pattern %<%T%> contains no argument packs", arg);
3732 else
3733 error ("expansion pattern %<%E%> contains no argument packs", arg);
3734 return error_mark_node;
3735 }
3736 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3737
3738 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3739
3740 return result;
3741 }
3742
3743 /* Checks T for any "bare" parameter packs, which have not yet been
3744 expanded, and issues an error if any are found. This operation can
3745 only be done on full expressions or types (e.g., an expression
3746 statement, "if" condition, etc.), because we could have expressions like:
3747
3748 foo(f(g(h(args)))...)
3749
3750 where "args" is a parameter pack. check_for_bare_parameter_packs
3751 should not be called for the subexpressions args, h(args),
3752 g(h(args)), or f(g(h(args))), because we would produce erroneous
3753 error messages.
3754
3755 Returns TRUE and emits an error if there were bare parameter packs,
3756 returns FALSE otherwise. */
3757 bool
3758 check_for_bare_parameter_packs (tree t)
3759 {
3760 tree parameter_packs = NULL_TREE;
3761 struct find_parameter_pack_data ppd;
3762
3763 if (!processing_template_decl || !t || t == error_mark_node)
3764 return false;
3765
3766 if (TREE_CODE (t) == TYPE_DECL)
3767 t = TREE_TYPE (t);
3768
3769 ppd.parameter_packs = &parameter_packs;
3770 ppd.visited = new hash_set<tree>;
3771 ppd.type_pack_expansion_p = false;
3772 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3773 delete ppd.visited;
3774
3775 if (parameter_packs)
3776 {
3777 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3778 error_at (loc, "parameter packs not expanded with %<...%>:");
3779 while (parameter_packs)
3780 {
3781 tree pack = TREE_VALUE (parameter_packs);
3782 tree name = NULL_TREE;
3783
3784 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3785 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3786 name = TYPE_NAME (pack);
3787 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3788 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3789 else
3790 name = DECL_NAME (pack);
3791
3792 if (name)
3793 inform (loc, " %qD", name);
3794 else
3795 inform (loc, " <anonymous>");
3796
3797 parameter_packs = TREE_CHAIN (parameter_packs);
3798 }
3799
3800 return true;
3801 }
3802
3803 return false;
3804 }
3805
3806 /* Expand any parameter packs that occur in the template arguments in
3807 ARGS. */
3808 tree
3809 expand_template_argument_pack (tree args)
3810 {
3811 tree result_args = NULL_TREE;
3812 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3813 int num_result_args = -1;
3814 int non_default_args_count = -1;
3815
3816 /* First, determine if we need to expand anything, and the number of
3817 slots we'll need. */
3818 for (in_arg = 0; in_arg < nargs; ++in_arg)
3819 {
3820 tree arg = TREE_VEC_ELT (args, in_arg);
3821 if (arg == NULL_TREE)
3822 return args;
3823 if (ARGUMENT_PACK_P (arg))
3824 {
3825 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3826 if (num_result_args < 0)
3827 num_result_args = in_arg + num_packed;
3828 else
3829 num_result_args += num_packed;
3830 }
3831 else
3832 {
3833 if (num_result_args >= 0)
3834 num_result_args++;
3835 }
3836 }
3837
3838 /* If no expansion is necessary, we're done. */
3839 if (num_result_args < 0)
3840 return args;
3841
3842 /* Expand arguments. */
3843 result_args = make_tree_vec (num_result_args);
3844 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3845 non_default_args_count =
3846 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3847 for (in_arg = 0; in_arg < nargs; ++in_arg)
3848 {
3849 tree arg = TREE_VEC_ELT (args, in_arg);
3850 if (ARGUMENT_PACK_P (arg))
3851 {
3852 tree packed = ARGUMENT_PACK_ARGS (arg);
3853 int i, num_packed = TREE_VEC_LENGTH (packed);
3854 for (i = 0; i < num_packed; ++i, ++out_arg)
3855 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3856 if (non_default_args_count > 0)
3857 non_default_args_count += num_packed - 1;
3858 }
3859 else
3860 {
3861 TREE_VEC_ELT (result_args, out_arg) = arg;
3862 ++out_arg;
3863 }
3864 }
3865 if (non_default_args_count >= 0)
3866 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3867 return result_args;
3868 }
3869
3870 /* Checks if DECL shadows a template parameter.
3871
3872 [temp.local]: A template-parameter shall not be redeclared within its
3873 scope (including nested scopes).
3874
3875 Emits an error and returns TRUE if the DECL shadows a parameter,
3876 returns FALSE otherwise. */
3877
3878 bool
3879 check_template_shadow (tree decl)
3880 {
3881 tree olddecl;
3882
3883 /* If we're not in a template, we can't possibly shadow a template
3884 parameter. */
3885 if (!current_template_parms)
3886 return true;
3887
3888 /* Figure out what we're shadowing. */
3889 if (TREE_CODE (decl) == OVERLOAD)
3890 decl = OVL_CURRENT (decl);
3891 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3892
3893 /* If there's no previous binding for this name, we're not shadowing
3894 anything, let alone a template parameter. */
3895 if (!olddecl)
3896 return true;
3897
3898 /* If we're not shadowing a template parameter, we're done. Note
3899 that OLDDECL might be an OVERLOAD (or perhaps even an
3900 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3901 node. */
3902 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3903 return true;
3904
3905 /* We check for decl != olddecl to avoid bogus errors for using a
3906 name inside a class. We check TPFI to avoid duplicate errors for
3907 inline member templates. */
3908 if (decl == olddecl
3909 || (DECL_TEMPLATE_PARM_P (decl)
3910 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3911 return true;
3912
3913 /* Don't complain about the injected class name, as we've already
3914 complained about the class itself. */
3915 if (DECL_SELF_REFERENCE_P (decl))
3916 return false;
3917
3918 if (DECL_TEMPLATE_PARM_P (decl))
3919 error ("declaration of template parameter %q+D shadows "
3920 "template parameter", decl);
3921 else
3922 error ("declaration of %q+#D shadows template parameter", decl);
3923 inform (DECL_SOURCE_LOCATION (olddecl),
3924 "template parameter %qD declared here", olddecl);
3925 return false;
3926 }
3927
3928 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3929 ORIG_LEVEL, DECL, and TYPE. */
3930
3931 static tree
3932 build_template_parm_index (int index,
3933 int level,
3934 int orig_level,
3935 tree decl,
3936 tree type)
3937 {
3938 tree t = make_node (TEMPLATE_PARM_INDEX);
3939 TEMPLATE_PARM_IDX (t) = index;
3940 TEMPLATE_PARM_LEVEL (t) = level;
3941 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3942 TEMPLATE_PARM_DECL (t) = decl;
3943 TREE_TYPE (t) = type;
3944 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3945 TREE_READONLY (t) = TREE_READONLY (decl);
3946
3947 return t;
3948 }
3949
3950 /* Find the canonical type parameter for the given template type
3951 parameter. Returns the canonical type parameter, which may be TYPE
3952 if no such parameter existed. */
3953
3954 static tree
3955 canonical_type_parameter (tree type)
3956 {
3957 tree list;
3958 int idx = TEMPLATE_TYPE_IDX (type);
3959 if (!canonical_template_parms)
3960 vec_alloc (canonical_template_parms, idx+1);
3961
3962 while (canonical_template_parms->length () <= (unsigned)idx)
3963 vec_safe_push (canonical_template_parms, NULL_TREE);
3964
3965 list = (*canonical_template_parms)[idx];
3966 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3967 list = TREE_CHAIN (list);
3968
3969 if (list)
3970 return TREE_VALUE (list);
3971 else
3972 {
3973 (*canonical_template_parms)[idx]
3974 = tree_cons (NULL_TREE, type,
3975 (*canonical_template_parms)[idx]);
3976 return type;
3977 }
3978 }
3979
3980 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3981 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3982 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3983 new one is created. */
3984
3985 static tree
3986 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3987 tsubst_flags_t complain)
3988 {
3989 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3990 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3991 != TEMPLATE_PARM_LEVEL (index) - levels)
3992 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3993 {
3994 tree orig_decl = TEMPLATE_PARM_DECL (index);
3995 tree decl, t;
3996
3997 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3998 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3999 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4000 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4001 DECL_ARTIFICIAL (decl) = 1;
4002 SET_DECL_TEMPLATE_PARM_P (decl);
4003
4004 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4005 TEMPLATE_PARM_LEVEL (index) - levels,
4006 TEMPLATE_PARM_ORIG_LEVEL (index),
4007 decl, type);
4008 TEMPLATE_PARM_DESCENDANTS (index) = t;
4009 TEMPLATE_PARM_PARAMETER_PACK (t)
4010 = TEMPLATE_PARM_PARAMETER_PACK (index);
4011
4012 /* Template template parameters need this. */
4013 if (TREE_CODE (decl) == TEMPLATE_DECL)
4014 {
4015 DECL_TEMPLATE_RESULT (decl)
4016 = build_decl (DECL_SOURCE_LOCATION (decl),
4017 TYPE_DECL, DECL_NAME (decl), type);
4018 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4019 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4020 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4021 }
4022 }
4023
4024 return TEMPLATE_PARM_DESCENDANTS (index);
4025 }
4026
4027 /* Process information from new template parameter PARM and append it
4028 to the LIST being built. This new parameter is a non-type
4029 parameter iff IS_NON_TYPE is true. This new parameter is a
4030 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4031 is in PARM_LOC. */
4032
4033 tree
4034 process_template_parm (tree list, location_t parm_loc, tree parm,
4035 bool is_non_type, bool is_parameter_pack)
4036 {
4037 tree decl = 0;
4038 int idx = 0;
4039
4040 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4041 tree defval = TREE_PURPOSE (parm);
4042 tree constr = TREE_TYPE (parm);
4043
4044 if (list)
4045 {
4046 tree p = tree_last (list);
4047
4048 if (p && TREE_VALUE (p) != error_mark_node)
4049 {
4050 p = TREE_VALUE (p);
4051 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4052 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4053 else
4054 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4055 }
4056
4057 ++idx;
4058 }
4059
4060 if (is_non_type)
4061 {
4062 parm = TREE_VALUE (parm);
4063
4064 SET_DECL_TEMPLATE_PARM_P (parm);
4065
4066 if (TREE_TYPE (parm) != error_mark_node)
4067 {
4068 /* [temp.param]
4069
4070 The top-level cv-qualifiers on the template-parameter are
4071 ignored when determining its type. */
4072 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4073 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4074 TREE_TYPE (parm) = error_mark_node;
4075 else if (uses_parameter_packs (TREE_TYPE (parm))
4076 && !is_parameter_pack
4077 /* If we're in a nested template parameter list, the template
4078 template parameter could be a parameter pack. */
4079 && processing_template_parmlist == 1)
4080 {
4081 /* This template parameter is not a parameter pack, but it
4082 should be. Complain about "bare" parameter packs. */
4083 check_for_bare_parameter_packs (TREE_TYPE (parm));
4084
4085 /* Recover by calling this a parameter pack. */
4086 is_parameter_pack = true;
4087 }
4088 }
4089
4090 /* A template parameter is not modifiable. */
4091 TREE_CONSTANT (parm) = 1;
4092 TREE_READONLY (parm) = 1;
4093 decl = build_decl (parm_loc,
4094 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4095 TREE_CONSTANT (decl) = 1;
4096 TREE_READONLY (decl) = 1;
4097 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4098 = build_template_parm_index (idx, processing_template_decl,
4099 processing_template_decl,
4100 decl, TREE_TYPE (parm));
4101
4102 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4103 = is_parameter_pack;
4104 }
4105 else
4106 {
4107 tree t;
4108 parm = TREE_VALUE (TREE_VALUE (parm));
4109
4110 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4111 {
4112 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4113 /* This is for distinguishing between real templates and template
4114 template parameters */
4115 TREE_TYPE (parm) = t;
4116 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4117 decl = parm;
4118 }
4119 else
4120 {
4121 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4122 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4123 decl = build_decl (parm_loc,
4124 TYPE_DECL, parm, t);
4125 }
4126
4127 TYPE_NAME (t) = decl;
4128 TYPE_STUB_DECL (t) = decl;
4129 parm = decl;
4130 TEMPLATE_TYPE_PARM_INDEX (t)
4131 = build_template_parm_index (idx, processing_template_decl,
4132 processing_template_decl,
4133 decl, TREE_TYPE (parm));
4134 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4135 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4136 }
4137 DECL_ARTIFICIAL (decl) = 1;
4138 SET_DECL_TEMPLATE_PARM_P (decl);
4139
4140 /* Build requirements for the type/template parameter.
4141 This must be done after SET_DECL_TEMPLATE_PARM_P or
4142 process_template_parm could fail. */
4143 tree reqs = finish_shorthand_constraint (parm, constr);
4144
4145 pushdecl (decl);
4146
4147 /* Build the parameter node linking the parameter declaration,
4148 its default argument (if any), and its constraints (if any). */
4149 parm = build_tree_list (defval, parm);
4150 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4151
4152 return chainon (list, parm);
4153 }
4154
4155 /* The end of a template parameter list has been reached. Process the
4156 tree list into a parameter vector, converting each parameter into a more
4157 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4158 as PARM_DECLs. */
4159
4160 tree
4161 end_template_parm_list (tree parms)
4162 {
4163 int nparms;
4164 tree parm, next;
4165 tree saved_parmlist = make_tree_vec (list_length (parms));
4166
4167 /* Pop the dummy parameter level and add the real one. */
4168 current_template_parms = TREE_CHAIN (current_template_parms);
4169
4170 current_template_parms
4171 = tree_cons (size_int (processing_template_decl),
4172 saved_parmlist, current_template_parms);
4173
4174 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4175 {
4176 next = TREE_CHAIN (parm);
4177 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4178 TREE_CHAIN (parm) = NULL_TREE;
4179 }
4180
4181 --processing_template_parmlist;
4182
4183 return saved_parmlist;
4184 }
4185
4186 // Explicitly indicate the end of the template parameter list. We assume
4187 // that the current template parameters have been constructed and/or
4188 // managed explicitly, as when creating new template template parameters
4189 // from a shorthand constraint.
4190 void
4191 end_template_parm_list ()
4192 {
4193 --processing_template_parmlist;
4194 }
4195
4196 /* end_template_decl is called after a template declaration is seen. */
4197
4198 void
4199 end_template_decl (void)
4200 {
4201 reset_specialization ();
4202
4203 if (! processing_template_decl)
4204 return;
4205
4206 /* This matches the pushlevel in begin_template_parm_list. */
4207 finish_scope ();
4208
4209 --processing_template_decl;
4210 current_template_parms = TREE_CHAIN (current_template_parms);
4211 }
4212
4213 /* Takes a TREE_LIST representing a template parameter and convert it
4214 into an argument suitable to be passed to the type substitution
4215 functions. Note that If the TREE_LIST contains an error_mark
4216 node, the returned argument is error_mark_node. */
4217
4218 tree
4219 template_parm_to_arg (tree t)
4220 {
4221
4222 if (t == NULL_TREE
4223 || TREE_CODE (t) != TREE_LIST)
4224 return t;
4225
4226 if (error_operand_p (TREE_VALUE (t)))
4227 return error_mark_node;
4228
4229 t = TREE_VALUE (t);
4230
4231 if (TREE_CODE (t) == TYPE_DECL
4232 || TREE_CODE (t) == TEMPLATE_DECL)
4233 {
4234 t = TREE_TYPE (t);
4235
4236 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4237 {
4238 /* Turn this argument into a TYPE_ARGUMENT_PACK
4239 with a single element, which expands T. */
4240 tree vec = make_tree_vec (1);
4241 if (CHECKING_P)
4242 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4243
4244 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4245
4246 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4247 SET_ARGUMENT_PACK_ARGS (t, vec);
4248 }
4249 }
4250 else
4251 {
4252 t = DECL_INITIAL (t);
4253
4254 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4255 {
4256 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4257 with a single element, which expands T. */
4258 tree vec = make_tree_vec (1);
4259 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4260 if (CHECKING_P)
4261 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4262
4263 t = convert_from_reference (t);
4264 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4265
4266 t = make_node (NONTYPE_ARGUMENT_PACK);
4267 SET_ARGUMENT_PACK_ARGS (t, vec);
4268 TREE_TYPE (t) = type;
4269 }
4270 else
4271 t = convert_from_reference (t);
4272 }
4273 return t;
4274 }
4275
4276 /* Given a set of template parameters, return them as a set of template
4277 arguments. The template parameters are represented as a TREE_VEC, in
4278 the form documented in cp-tree.h for template arguments. */
4279
4280 static tree
4281 template_parms_to_args (tree parms)
4282 {
4283 tree header;
4284 tree args = NULL_TREE;
4285 int length = TMPL_PARMS_DEPTH (parms);
4286 int l = length;
4287
4288 /* If there is only one level of template parameters, we do not
4289 create a TREE_VEC of TREE_VECs. Instead, we return a single
4290 TREE_VEC containing the arguments. */
4291 if (length > 1)
4292 args = make_tree_vec (length);
4293
4294 for (header = parms; header; header = TREE_CHAIN (header))
4295 {
4296 tree a = copy_node (TREE_VALUE (header));
4297 int i;
4298
4299 TREE_TYPE (a) = NULL_TREE;
4300 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4301 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4302
4303 if (CHECKING_P)
4304 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4305
4306 if (length > 1)
4307 TREE_VEC_ELT (args, --l) = a;
4308 else
4309 args = a;
4310 }
4311
4312 return args;
4313 }
4314
4315 /* Within the declaration of a template, return the currently active
4316 template parameters as an argument TREE_VEC. */
4317
4318 static tree
4319 current_template_args (void)
4320 {
4321 return template_parms_to_args (current_template_parms);
4322 }
4323
4324 /* Update the declared TYPE by doing any lookups which were thought to be
4325 dependent, but are not now that we know the SCOPE of the declarator. */
4326
4327 tree
4328 maybe_update_decl_type (tree orig_type, tree scope)
4329 {
4330 tree type = orig_type;
4331
4332 if (type == NULL_TREE)
4333 return type;
4334
4335 if (TREE_CODE (orig_type) == TYPE_DECL)
4336 type = TREE_TYPE (type);
4337
4338 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4339 && dependent_type_p (type)
4340 /* Don't bother building up the args in this case. */
4341 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4342 {
4343 /* tsubst in the args corresponding to the template parameters,
4344 including auto if present. Most things will be unchanged, but
4345 make_typename_type and tsubst_qualified_id will resolve
4346 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4347 tree args = current_template_args ();
4348 tree auto_node = type_uses_auto (type);
4349 tree pushed;
4350 if (auto_node)
4351 {
4352 tree auto_vec = make_tree_vec (1);
4353 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4354 args = add_to_template_args (args, auto_vec);
4355 }
4356 pushed = push_scope (scope);
4357 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4358 if (pushed)
4359 pop_scope (scope);
4360 }
4361
4362 if (type == error_mark_node)
4363 return orig_type;
4364
4365 if (TREE_CODE (orig_type) == TYPE_DECL)
4366 {
4367 if (same_type_p (type, TREE_TYPE (orig_type)))
4368 type = orig_type;
4369 else
4370 type = TYPE_NAME (type);
4371 }
4372 return type;
4373 }
4374
4375 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4376 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4377 the new template is a member template. */
4378
4379 tree
4380 build_template_decl (tree decl, tree parms, bool member_template_p)
4381 {
4382 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4383 DECL_TEMPLATE_PARMS (tmpl) = parms;
4384 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4385 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4386 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4387
4388 return tmpl;
4389 }
4390
4391 struct template_parm_data
4392 {
4393 /* The level of the template parameters we are currently
4394 processing. */
4395 int level;
4396
4397 /* The index of the specialization argument we are currently
4398 processing. */
4399 int current_arg;
4400
4401 /* An array whose size is the number of template parameters. The
4402 elements are nonzero if the parameter has been used in any one
4403 of the arguments processed so far. */
4404 int* parms;
4405
4406 /* An array whose size is the number of template arguments. The
4407 elements are nonzero if the argument makes use of template
4408 parameters of this level. */
4409 int* arg_uses_template_parms;
4410 };
4411
4412 /* Subroutine of push_template_decl used to see if each template
4413 parameter in a partial specialization is used in the explicit
4414 argument list. If T is of the LEVEL given in DATA (which is
4415 treated as a template_parm_data*), then DATA->PARMS is marked
4416 appropriately. */
4417
4418 static int
4419 mark_template_parm (tree t, void* data)
4420 {
4421 int level;
4422 int idx;
4423 struct template_parm_data* tpd = (struct template_parm_data*) data;
4424
4425 template_parm_level_and_index (t, &level, &idx);
4426
4427 if (level == tpd->level)
4428 {
4429 tpd->parms[idx] = 1;
4430 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4431 }
4432
4433 /* Return zero so that for_each_template_parm will continue the
4434 traversal of the tree; we want to mark *every* template parm. */
4435 return 0;
4436 }
4437
4438 /* Process the partial specialization DECL. */
4439
4440 static tree
4441 process_partial_specialization (tree decl)
4442 {
4443 tree type = TREE_TYPE (decl);
4444 tree tinfo = get_template_info (decl);
4445 tree maintmpl = TI_TEMPLATE (tinfo);
4446 tree specargs = TI_ARGS (tinfo);
4447 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4448 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4449 tree inner_parms;
4450 tree inst;
4451 int nargs = TREE_VEC_LENGTH (inner_args);
4452 int ntparms;
4453 int i;
4454 bool did_error_intro = false;
4455 struct template_parm_data tpd;
4456 struct template_parm_data tpd2;
4457
4458 gcc_assert (current_template_parms);
4459
4460 /* A concept cannot be specialized. */
4461 if (flag_concepts && variable_concept_p (maintmpl))
4462 {
4463 error ("specialization of variable concept %q#D", maintmpl);
4464 return error_mark_node;
4465 }
4466
4467 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4468 ntparms = TREE_VEC_LENGTH (inner_parms);
4469
4470 /* We check that each of the template parameters given in the
4471 partial specialization is used in the argument list to the
4472 specialization. For example:
4473
4474 template <class T> struct S;
4475 template <class T> struct S<T*>;
4476
4477 The second declaration is OK because `T*' uses the template
4478 parameter T, whereas
4479
4480 template <class T> struct S<int>;
4481
4482 is no good. Even trickier is:
4483
4484 template <class T>
4485 struct S1
4486 {
4487 template <class U>
4488 struct S2;
4489 template <class U>
4490 struct S2<T>;
4491 };
4492
4493 The S2<T> declaration is actually invalid; it is a
4494 full-specialization. Of course,
4495
4496 template <class U>
4497 struct S2<T (*)(U)>;
4498
4499 or some such would have been OK. */
4500 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4501 tpd.parms = XALLOCAVEC (int, ntparms);
4502 memset (tpd.parms, 0, sizeof (int) * ntparms);
4503
4504 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4505 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4506 for (i = 0; i < nargs; ++i)
4507 {
4508 tpd.current_arg = i;
4509 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4510 &mark_template_parm,
4511 &tpd,
4512 NULL,
4513 /*include_nondeduced_p=*/false);
4514 }
4515 for (i = 0; i < ntparms; ++i)
4516 if (tpd.parms[i] == 0)
4517 {
4518 /* One of the template parms was not used in a deduced context in the
4519 specialization. */
4520 if (!did_error_intro)
4521 {
4522 error ("template parameters not deducible in "
4523 "partial specialization:");
4524 did_error_intro = true;
4525 }
4526
4527 inform (input_location, " %qD",
4528 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4529 }
4530
4531 if (did_error_intro)
4532 return error_mark_node;
4533
4534 /* [temp.class.spec]
4535
4536 The argument list of the specialization shall not be identical to
4537 the implicit argument list of the primary template. */
4538 tree main_args
4539 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4540 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4541 && (!flag_concepts
4542 || !strictly_subsumes (current_template_constraints (),
4543 get_constraints (maintmpl))))
4544 {
4545 if (!flag_concepts)
4546 error ("partial specialization %q+D does not specialize "
4547 "any template arguments", decl);
4548 else
4549 error ("partial specialization %q+D does not specialize any "
4550 "template arguments and is not more constrained than", decl);
4551 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4552 }
4553
4554 /* A partial specialization that replaces multiple parameters of the
4555 primary template with a pack expansion is less specialized for those
4556 parameters. */
4557 if (nargs < DECL_NTPARMS (maintmpl))
4558 {
4559 error ("partial specialization is not more specialized than the "
4560 "primary template because it replaces multiple parameters "
4561 "with a pack expansion");
4562 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4563 return decl;
4564 }
4565
4566 /* [temp.class.spec]
4567
4568 A partially specialized non-type argument expression shall not
4569 involve template parameters of the partial specialization except
4570 when the argument expression is a simple identifier.
4571
4572 The type of a template parameter corresponding to a specialized
4573 non-type argument shall not be dependent on a parameter of the
4574 specialization.
4575
4576 Also, we verify that pack expansions only occur at the
4577 end of the argument list. */
4578 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4579 tpd2.parms = 0;
4580 for (i = 0; i < nargs; ++i)
4581 {
4582 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4583 tree arg = TREE_VEC_ELT (inner_args, i);
4584 tree packed_args = NULL_TREE;
4585 int j, len = 1;
4586
4587 if (ARGUMENT_PACK_P (arg))
4588 {
4589 /* Extract the arguments from the argument pack. We'll be
4590 iterating over these in the following loop. */
4591 packed_args = ARGUMENT_PACK_ARGS (arg);
4592 len = TREE_VEC_LENGTH (packed_args);
4593 }
4594
4595 for (j = 0; j < len; j++)
4596 {
4597 if (packed_args)
4598 /* Get the Jth argument in the parameter pack. */
4599 arg = TREE_VEC_ELT (packed_args, j);
4600
4601 if (PACK_EXPANSION_P (arg))
4602 {
4603 /* Pack expansions must come at the end of the
4604 argument list. */
4605 if ((packed_args && j < len - 1)
4606 || (!packed_args && i < nargs - 1))
4607 {
4608 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4609 error ("parameter pack argument %qE must be at the "
4610 "end of the template argument list", arg);
4611 else
4612 error ("parameter pack argument %qT must be at the "
4613 "end of the template argument list", arg);
4614 }
4615 }
4616
4617 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4618 /* We only care about the pattern. */
4619 arg = PACK_EXPANSION_PATTERN (arg);
4620
4621 if (/* These first two lines are the `non-type' bit. */
4622 !TYPE_P (arg)
4623 && TREE_CODE (arg) != TEMPLATE_DECL
4624 /* This next two lines are the `argument expression is not just a
4625 simple identifier' condition and also the `specialized
4626 non-type argument' bit. */
4627 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4628 && !(REFERENCE_REF_P (arg)
4629 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4630 {
4631 if ((!packed_args && tpd.arg_uses_template_parms[i])
4632 || (packed_args && uses_template_parms (arg)))
4633 error ("template argument %qE involves template parameter(s)",
4634 arg);
4635 else
4636 {
4637 /* Look at the corresponding template parameter,
4638 marking which template parameters its type depends
4639 upon. */
4640 tree type = TREE_TYPE (parm);
4641
4642 if (!tpd2.parms)
4643 {
4644 /* We haven't yet initialized TPD2. Do so now. */
4645 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4646 /* The number of parameters here is the number in the
4647 main template, which, as checked in the assertion
4648 above, is NARGS. */
4649 tpd2.parms = XALLOCAVEC (int, nargs);
4650 tpd2.level =
4651 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4652 }
4653
4654 /* Mark the template parameters. But this time, we're
4655 looking for the template parameters of the main
4656 template, not in the specialization. */
4657 tpd2.current_arg = i;
4658 tpd2.arg_uses_template_parms[i] = 0;
4659 memset (tpd2.parms, 0, sizeof (int) * nargs);
4660 for_each_template_parm (type,
4661 &mark_template_parm,
4662 &tpd2,
4663 NULL,
4664 /*include_nondeduced_p=*/false);
4665
4666 if (tpd2.arg_uses_template_parms [i])
4667 {
4668 /* The type depended on some template parameters.
4669 If they are fully specialized in the
4670 specialization, that's OK. */
4671 int j;
4672 int count = 0;
4673 for (j = 0; j < nargs; ++j)
4674 if (tpd2.parms[j] != 0
4675 && tpd.arg_uses_template_parms [j])
4676 ++count;
4677 if (count != 0)
4678 error_n (input_location, count,
4679 "type %qT of template argument %qE depends "
4680 "on a template parameter",
4681 "type %qT of template argument %qE depends "
4682 "on template parameters",
4683 type,
4684 arg);
4685 }
4686 }
4687 }
4688 }
4689 }
4690
4691 /* We should only get here once. */
4692 if (TREE_CODE (decl) == TYPE_DECL)
4693 gcc_assert (!COMPLETE_TYPE_P (type));
4694
4695 // Build the template decl.
4696 tree tmpl = build_template_decl (decl, current_template_parms,
4697 DECL_MEMBER_TEMPLATE_P (maintmpl));
4698 TREE_TYPE (tmpl) = type;
4699 DECL_TEMPLATE_RESULT (tmpl) = decl;
4700 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4701 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4702 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4703
4704 if (VAR_P (decl))
4705 /* We didn't register this in check_explicit_specialization so we could
4706 wait until the constraints were set. */
4707 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4708 else
4709 associate_classtype_constraints (type);
4710
4711 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4712 = tree_cons (specargs, tmpl,
4713 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4714 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4715
4716 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4717 inst = TREE_CHAIN (inst))
4718 {
4719 tree instance = TREE_VALUE (inst);
4720 if (TYPE_P (instance)
4721 ? (COMPLETE_TYPE_P (instance)
4722 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4723 : DECL_TEMPLATE_INSTANTIATION (instance))
4724 {
4725 tree spec = most_specialized_partial_spec (instance, tf_none);
4726 tree inst_decl = (DECL_P (instance)
4727 ? instance : TYPE_NAME (instance));
4728 if (!spec)
4729 /* OK */;
4730 else if (spec == error_mark_node)
4731 permerror (input_location,
4732 "declaration of %qD ambiguates earlier template "
4733 "instantiation for %qD", decl, inst_decl);
4734 else if (TREE_VALUE (spec) == tmpl)
4735 permerror (input_location,
4736 "partial specialization of %qD after instantiation "
4737 "of %qD", decl, inst_decl);
4738 }
4739 }
4740
4741 return decl;
4742 }
4743
4744 /* PARM is a template parameter of some form; return the corresponding
4745 TEMPLATE_PARM_INDEX. */
4746
4747 static tree
4748 get_template_parm_index (tree parm)
4749 {
4750 if (TREE_CODE (parm) == PARM_DECL
4751 || TREE_CODE (parm) == CONST_DECL)
4752 parm = DECL_INITIAL (parm);
4753 else if (TREE_CODE (parm) == TYPE_DECL
4754 || TREE_CODE (parm) == TEMPLATE_DECL)
4755 parm = TREE_TYPE (parm);
4756 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4757 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4758 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4759 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4760 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4761 return parm;
4762 }
4763
4764 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4765 parameter packs used by the template parameter PARM. */
4766
4767 static void
4768 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4769 {
4770 /* A type parm can't refer to another parm. */
4771 if (TREE_CODE (parm) == TYPE_DECL)
4772 return;
4773 else if (TREE_CODE (parm) == PARM_DECL)
4774 {
4775 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4776 ppd, ppd->visited);
4777 return;
4778 }
4779
4780 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4781
4782 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4783 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4784 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4785 }
4786
4787 /* PARM is a template parameter pack. Return any parameter packs used in
4788 its type or the type of any of its template parameters. If there are
4789 any such packs, it will be instantiated into a fixed template parameter
4790 list by partial instantiation rather than be fully deduced. */
4791
4792 tree
4793 fixed_parameter_pack_p (tree parm)
4794 {
4795 /* This can only be true in a member template. */
4796 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4797 return NULL_TREE;
4798 /* This can only be true for a parameter pack. */
4799 if (!template_parameter_pack_p (parm))
4800 return NULL_TREE;
4801 /* A type parm can't refer to another parm. */
4802 if (TREE_CODE (parm) == TYPE_DECL)
4803 return NULL_TREE;
4804
4805 tree parameter_packs = NULL_TREE;
4806 struct find_parameter_pack_data ppd;
4807 ppd.parameter_packs = &parameter_packs;
4808 ppd.visited = new hash_set<tree>;
4809 ppd.type_pack_expansion_p = false;
4810
4811 fixed_parameter_pack_p_1 (parm, &ppd);
4812
4813 delete ppd.visited;
4814 return parameter_packs;
4815 }
4816
4817 /* Check that a template declaration's use of default arguments and
4818 parameter packs is not invalid. Here, PARMS are the template
4819 parameters. IS_PRIMARY is true if DECL is the thing declared by
4820 a primary template. IS_PARTIAL is true if DECL is a partial
4821 specialization.
4822
4823 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4824 declaration (but not a definition); 1 indicates a declaration, 2
4825 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4826 emitted for extraneous default arguments.
4827
4828 Returns TRUE if there were no errors found, FALSE otherwise. */
4829
4830 bool
4831 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4832 bool is_partial, int is_friend_decl)
4833 {
4834 const char *msg;
4835 int last_level_to_check;
4836 tree parm_level;
4837 bool no_errors = true;
4838
4839 /* [temp.param]
4840
4841 A default template-argument shall not be specified in a
4842 function template declaration or a function template definition, nor
4843 in the template-parameter-list of the definition of a member of a
4844 class template. */
4845
4846 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4847 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4848 /* You can't have a function template declaration in a local
4849 scope, nor you can you define a member of a class template in a
4850 local scope. */
4851 return true;
4852
4853 if ((TREE_CODE (decl) == TYPE_DECL
4854 && TREE_TYPE (decl)
4855 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4856 || (TREE_CODE (decl) == FUNCTION_DECL
4857 && LAMBDA_FUNCTION_P (decl)))
4858 /* A lambda doesn't have an explicit declaration; don't complain
4859 about the parms of the enclosing class. */
4860 return true;
4861
4862 if (current_class_type
4863 && !TYPE_BEING_DEFINED (current_class_type)
4864 && DECL_LANG_SPECIFIC (decl)
4865 && DECL_DECLARES_FUNCTION_P (decl)
4866 /* If this is either a friend defined in the scope of the class
4867 or a member function. */
4868 && (DECL_FUNCTION_MEMBER_P (decl)
4869 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4870 : DECL_FRIEND_CONTEXT (decl)
4871 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4872 : false)
4873 /* And, if it was a member function, it really was defined in
4874 the scope of the class. */
4875 && (!DECL_FUNCTION_MEMBER_P (decl)
4876 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4877 /* We already checked these parameters when the template was
4878 declared, so there's no need to do it again now. This function
4879 was defined in class scope, but we're processing its body now
4880 that the class is complete. */
4881 return true;
4882
4883 /* Core issue 226 (C++0x only): the following only applies to class
4884 templates. */
4885 if (is_primary
4886 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4887 {
4888 /* [temp.param]
4889
4890 If a template-parameter has a default template-argument, all
4891 subsequent template-parameters shall have a default
4892 template-argument supplied. */
4893 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4894 {
4895 tree inner_parms = TREE_VALUE (parm_level);
4896 int ntparms = TREE_VEC_LENGTH (inner_parms);
4897 int seen_def_arg_p = 0;
4898 int i;
4899
4900 for (i = 0; i < ntparms; ++i)
4901 {
4902 tree parm = TREE_VEC_ELT (inner_parms, i);
4903
4904 if (parm == error_mark_node)
4905 continue;
4906
4907 if (TREE_PURPOSE (parm))
4908 seen_def_arg_p = 1;
4909 else if (seen_def_arg_p
4910 && !template_parameter_pack_p (TREE_VALUE (parm)))
4911 {
4912 error ("no default argument for %qD", TREE_VALUE (parm));
4913 /* For better subsequent error-recovery, we indicate that
4914 there should have been a default argument. */
4915 TREE_PURPOSE (parm) = error_mark_node;
4916 no_errors = false;
4917 }
4918 else if (!is_partial
4919 && !is_friend_decl
4920 /* Don't complain about an enclosing partial
4921 specialization. */
4922 && parm_level == parms
4923 && TREE_CODE (decl) == TYPE_DECL
4924 && i < ntparms - 1
4925 && template_parameter_pack_p (TREE_VALUE (parm))
4926 /* A fixed parameter pack will be partially
4927 instantiated into a fixed length list. */
4928 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4929 {
4930 /* A primary class template can only have one
4931 parameter pack, at the end of the template
4932 parameter list. */
4933
4934 error ("parameter pack %q+D must be at the end of the"
4935 " template parameter list", TREE_VALUE (parm));
4936
4937 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4938 = error_mark_node;
4939 no_errors = false;
4940 }
4941 }
4942 }
4943 }
4944
4945 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4946 || is_partial
4947 || !is_primary
4948 || is_friend_decl)
4949 /* For an ordinary class template, default template arguments are
4950 allowed at the innermost level, e.g.:
4951 template <class T = int>
4952 struct S {};
4953 but, in a partial specialization, they're not allowed even
4954 there, as we have in [temp.class.spec]:
4955
4956 The template parameter list of a specialization shall not
4957 contain default template argument values.
4958
4959 So, for a partial specialization, or for a function template
4960 (in C++98/C++03), we look at all of them. */
4961 ;
4962 else
4963 /* But, for a primary class template that is not a partial
4964 specialization we look at all template parameters except the
4965 innermost ones. */
4966 parms = TREE_CHAIN (parms);
4967
4968 /* Figure out what error message to issue. */
4969 if (is_friend_decl == 2)
4970 msg = G_("default template arguments may not be used in function template "
4971 "friend re-declaration");
4972 else if (is_friend_decl)
4973 msg = G_("default template arguments may not be used in function template "
4974 "friend declarations");
4975 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4976 msg = G_("default template arguments may not be used in function templates "
4977 "without -std=c++11 or -std=gnu++11");
4978 else if (is_partial)
4979 msg = G_("default template arguments may not be used in "
4980 "partial specializations");
4981 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
4982 msg = G_("default argument for template parameter for class enclosing %qD");
4983 else
4984 /* Per [temp.param]/9, "A default template-argument shall not be
4985 specified in the template-parameter-lists of the definition of
4986 a member of a class template that appears outside of the member's
4987 class.", thus if we aren't handling a member of a class template
4988 there is no need to examine the parameters. */
4989 return true;
4990
4991 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4992 /* If we're inside a class definition, there's no need to
4993 examine the parameters to the class itself. On the one
4994 hand, they will be checked when the class is defined, and,
4995 on the other, default arguments are valid in things like:
4996 template <class T = double>
4997 struct S { template <class U> void f(U); };
4998 Here the default argument for `S' has no bearing on the
4999 declaration of `f'. */
5000 last_level_to_check = template_class_depth (current_class_type) + 1;
5001 else
5002 /* Check everything. */
5003 last_level_to_check = 0;
5004
5005 for (parm_level = parms;
5006 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5007 parm_level = TREE_CHAIN (parm_level))
5008 {
5009 tree inner_parms = TREE_VALUE (parm_level);
5010 int i;
5011 int ntparms;
5012
5013 ntparms = TREE_VEC_LENGTH (inner_parms);
5014 for (i = 0; i < ntparms; ++i)
5015 {
5016 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5017 continue;
5018
5019 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5020 {
5021 if (msg)
5022 {
5023 no_errors = false;
5024 if (is_friend_decl == 2)
5025 return no_errors;
5026
5027 error (msg, decl);
5028 msg = 0;
5029 }
5030
5031 /* Clear out the default argument so that we are not
5032 confused later. */
5033 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5034 }
5035 }
5036
5037 /* At this point, if we're still interested in issuing messages,
5038 they must apply to classes surrounding the object declared. */
5039 if (msg)
5040 msg = G_("default argument for template parameter for class "
5041 "enclosing %qD");
5042 }
5043
5044 return no_errors;
5045 }
5046
5047 /* Worker for push_template_decl_real, called via
5048 for_each_template_parm. DATA is really an int, indicating the
5049 level of the parameters we are interested in. If T is a template
5050 parameter of that level, return nonzero. */
5051
5052 static int
5053 template_parm_this_level_p (tree t, void* data)
5054 {
5055 int this_level = *(int *)data;
5056 int level;
5057
5058 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5059 level = TEMPLATE_PARM_LEVEL (t);
5060 else
5061 level = TEMPLATE_TYPE_LEVEL (t);
5062 return level == this_level;
5063 }
5064
5065 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5066 DATA is really an int, indicating the innermost outer level of parameters.
5067 If T is a template parameter of that level or further out, return
5068 nonzero. */
5069
5070 static int
5071 template_parm_outer_level (tree t, void *data)
5072 {
5073 int this_level = *(int *)data;
5074 int level;
5075
5076 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5077 level = TEMPLATE_PARM_LEVEL (t);
5078 else
5079 level = TEMPLATE_TYPE_LEVEL (t);
5080 return level <= this_level;
5081 }
5082
5083 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5084 parameters given by current_template_args, or reuses a
5085 previously existing one, if appropriate. Returns the DECL, or an
5086 equivalent one, if it is replaced via a call to duplicate_decls.
5087
5088 If IS_FRIEND is true, DECL is a friend declaration. */
5089
5090 tree
5091 push_template_decl_real (tree decl, bool is_friend)
5092 {
5093 tree tmpl;
5094 tree args;
5095 tree info;
5096 tree ctx;
5097 bool is_primary;
5098 bool is_partial;
5099 int new_template_p = 0;
5100 /* True if the template is a member template, in the sense of
5101 [temp.mem]. */
5102 bool member_template_p = false;
5103
5104 if (decl == error_mark_node || !current_template_parms)
5105 return error_mark_node;
5106
5107 /* See if this is a partial specialization. */
5108 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5109 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5110 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5111 || (VAR_P (decl)
5112 && DECL_LANG_SPECIFIC (decl)
5113 && DECL_TEMPLATE_SPECIALIZATION (decl)
5114 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5115
5116 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5117 is_friend = true;
5118
5119 if (is_friend)
5120 /* For a friend, we want the context of the friend function, not
5121 the type of which it is a friend. */
5122 ctx = CP_DECL_CONTEXT (decl);
5123 else if (CP_DECL_CONTEXT (decl)
5124 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5125 /* In the case of a virtual function, we want the class in which
5126 it is defined. */
5127 ctx = CP_DECL_CONTEXT (decl);
5128 else
5129 /* Otherwise, if we're currently defining some class, the DECL
5130 is assumed to be a member of the class. */
5131 ctx = current_scope ();
5132
5133 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5134 ctx = NULL_TREE;
5135
5136 if (!DECL_CONTEXT (decl))
5137 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5138
5139 /* See if this is a primary template. */
5140 if (is_friend && ctx
5141 && uses_template_parms_level (ctx, processing_template_decl))
5142 /* A friend template that specifies a class context, i.e.
5143 template <typename T> friend void A<T>::f();
5144 is not primary. */
5145 is_primary = false;
5146 else if (TREE_CODE (decl) == TYPE_DECL
5147 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5148 is_primary = false;
5149 else
5150 is_primary = template_parm_scope_p ();
5151
5152 if (is_primary)
5153 {
5154 warning (OPT_Wtemplates, "template %qD declared", decl);
5155
5156 if (DECL_CLASS_SCOPE_P (decl))
5157 member_template_p = true;
5158 if (TREE_CODE (decl) == TYPE_DECL
5159 && anon_aggrname_p (DECL_NAME (decl)))
5160 {
5161 error ("template class without a name");
5162 return error_mark_node;
5163 }
5164 else if (TREE_CODE (decl) == FUNCTION_DECL)
5165 {
5166 if (member_template_p)
5167 {
5168 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5169 error ("member template %qD may not have virt-specifiers", decl);
5170 }
5171 if (DECL_DESTRUCTOR_P (decl))
5172 {
5173 /* [temp.mem]
5174
5175 A destructor shall not be a member template. */
5176 error ("destructor %qD declared as member template", decl);
5177 return error_mark_node;
5178 }
5179 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5180 && (!prototype_p (TREE_TYPE (decl))
5181 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5182 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5183 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5184 == void_list_node)))
5185 {
5186 /* [basic.stc.dynamic.allocation]
5187
5188 An allocation function can be a function
5189 template. ... Template allocation functions shall
5190 have two or more parameters. */
5191 error ("invalid template declaration of %qD", decl);
5192 return error_mark_node;
5193 }
5194 }
5195 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5196 && CLASS_TYPE_P (TREE_TYPE (decl)))
5197 /* OK */;
5198 else if (TREE_CODE (decl) == TYPE_DECL
5199 && TYPE_DECL_ALIAS_P (decl))
5200 /* alias-declaration */
5201 gcc_assert (!DECL_ARTIFICIAL (decl));
5202 else if (VAR_P (decl))
5203 /* C++14 variable template. */;
5204 else
5205 {
5206 error ("template declaration of %q#D", decl);
5207 return error_mark_node;
5208 }
5209 }
5210
5211 /* Check to see that the rules regarding the use of default
5212 arguments are not being violated. */
5213 check_default_tmpl_args (decl, current_template_parms,
5214 is_primary, is_partial, /*is_friend_decl=*/0);
5215
5216 /* Ensure that there are no parameter packs in the type of this
5217 declaration that have not been expanded. */
5218 if (TREE_CODE (decl) == FUNCTION_DECL)
5219 {
5220 /* Check each of the arguments individually to see if there are
5221 any bare parameter packs. */
5222 tree type = TREE_TYPE (decl);
5223 tree arg = DECL_ARGUMENTS (decl);
5224 tree argtype = TYPE_ARG_TYPES (type);
5225
5226 while (arg && argtype)
5227 {
5228 if (!DECL_PACK_P (arg)
5229 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5230 {
5231 /* This is a PARM_DECL that contains unexpanded parameter
5232 packs. We have already complained about this in the
5233 check_for_bare_parameter_packs call, so just replace
5234 these types with ERROR_MARK_NODE. */
5235 TREE_TYPE (arg) = error_mark_node;
5236 TREE_VALUE (argtype) = error_mark_node;
5237 }
5238
5239 arg = DECL_CHAIN (arg);
5240 argtype = TREE_CHAIN (argtype);
5241 }
5242
5243 /* Check for bare parameter packs in the return type and the
5244 exception specifiers. */
5245 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5246 /* Errors were already issued, set return type to int
5247 as the frontend doesn't expect error_mark_node as
5248 the return type. */
5249 TREE_TYPE (type) = integer_type_node;
5250 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5251 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5252 }
5253 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5254 && TYPE_DECL_ALIAS_P (decl))
5255 ? DECL_ORIGINAL_TYPE (decl)
5256 : TREE_TYPE (decl)))
5257 {
5258 TREE_TYPE (decl) = error_mark_node;
5259 return error_mark_node;
5260 }
5261
5262 if (is_partial)
5263 return process_partial_specialization (decl);
5264
5265 args = current_template_args ();
5266
5267 if (!ctx
5268 || TREE_CODE (ctx) == FUNCTION_DECL
5269 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5270 || (TREE_CODE (decl) == TYPE_DECL
5271 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5272 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5273 {
5274 if (DECL_LANG_SPECIFIC (decl)
5275 && DECL_TEMPLATE_INFO (decl)
5276 && DECL_TI_TEMPLATE (decl))
5277 tmpl = DECL_TI_TEMPLATE (decl);
5278 /* If DECL is a TYPE_DECL for a class-template, then there won't
5279 be DECL_LANG_SPECIFIC. The information equivalent to
5280 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5281 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5282 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5283 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5284 {
5285 /* Since a template declaration already existed for this
5286 class-type, we must be redeclaring it here. Make sure
5287 that the redeclaration is valid. */
5288 redeclare_class_template (TREE_TYPE (decl),
5289 current_template_parms,
5290 current_template_constraints ());
5291 /* We don't need to create a new TEMPLATE_DECL; just use the
5292 one we already had. */
5293 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5294 }
5295 else
5296 {
5297 tmpl = build_template_decl (decl, current_template_parms,
5298 member_template_p);
5299 new_template_p = 1;
5300
5301 if (DECL_LANG_SPECIFIC (decl)
5302 && DECL_TEMPLATE_SPECIALIZATION (decl))
5303 {
5304 /* A specialization of a member template of a template
5305 class. */
5306 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5307 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5308 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5309 }
5310 }
5311 }
5312 else
5313 {
5314 tree a, t, current, parms;
5315 int i;
5316 tree tinfo = get_template_info (decl);
5317
5318 if (!tinfo)
5319 {
5320 error ("template definition of non-template %q#D", decl);
5321 return error_mark_node;
5322 }
5323
5324 tmpl = TI_TEMPLATE (tinfo);
5325
5326 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5327 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5328 && DECL_TEMPLATE_SPECIALIZATION (decl)
5329 && DECL_MEMBER_TEMPLATE_P (tmpl))
5330 {
5331 tree new_tmpl;
5332
5333 /* The declaration is a specialization of a member
5334 template, declared outside the class. Therefore, the
5335 innermost template arguments will be NULL, so we
5336 replace them with the arguments determined by the
5337 earlier call to check_explicit_specialization. */
5338 args = DECL_TI_ARGS (decl);
5339
5340 new_tmpl
5341 = build_template_decl (decl, current_template_parms,
5342 member_template_p);
5343 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5344 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5345 DECL_TI_TEMPLATE (decl) = new_tmpl;
5346 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5347 DECL_TEMPLATE_INFO (new_tmpl)
5348 = build_template_info (tmpl, args);
5349
5350 register_specialization (new_tmpl,
5351 most_general_template (tmpl),
5352 args,
5353 is_friend, 0);
5354 return decl;
5355 }
5356
5357 /* Make sure the template headers we got make sense. */
5358
5359 parms = DECL_TEMPLATE_PARMS (tmpl);
5360 i = TMPL_PARMS_DEPTH (parms);
5361 if (TMPL_ARGS_DEPTH (args) != i)
5362 {
5363 error ("expected %d levels of template parms for %q#D, got %d",
5364 i, decl, TMPL_ARGS_DEPTH (args));
5365 DECL_INTERFACE_KNOWN (decl) = 1;
5366 return error_mark_node;
5367 }
5368 else
5369 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5370 {
5371 a = TMPL_ARGS_LEVEL (args, i);
5372 t = INNERMOST_TEMPLATE_PARMS (parms);
5373
5374 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5375 {
5376 if (current == decl)
5377 error ("got %d template parameters for %q#D",
5378 TREE_VEC_LENGTH (a), decl);
5379 else
5380 error ("got %d template parameters for %q#T",
5381 TREE_VEC_LENGTH (a), current);
5382 error (" but %d required", TREE_VEC_LENGTH (t));
5383 /* Avoid crash in import_export_decl. */
5384 DECL_INTERFACE_KNOWN (decl) = 1;
5385 return error_mark_node;
5386 }
5387
5388 if (current == decl)
5389 current = ctx;
5390 else if (current == NULL_TREE)
5391 /* Can happen in erroneous input. */
5392 break;
5393 else
5394 current = get_containing_scope (current);
5395 }
5396
5397 /* Check that the parms are used in the appropriate qualifying scopes
5398 in the declarator. */
5399 if (!comp_template_args
5400 (TI_ARGS (tinfo),
5401 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5402 {
5403 error ("\
5404 template arguments to %qD do not match original template %qD",
5405 decl, DECL_TEMPLATE_RESULT (tmpl));
5406 if (!uses_template_parms (TI_ARGS (tinfo)))
5407 inform (input_location, "use template<> for an explicit specialization");
5408 /* Avoid crash in import_export_decl. */
5409 DECL_INTERFACE_KNOWN (decl) = 1;
5410 return error_mark_node;
5411 }
5412 }
5413
5414 DECL_TEMPLATE_RESULT (tmpl) = decl;
5415 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5416
5417 /* Push template declarations for global functions and types. Note
5418 that we do not try to push a global template friend declared in a
5419 template class; such a thing may well depend on the template
5420 parameters of the class. */
5421 if (new_template_p && !ctx
5422 && !(is_friend && template_class_depth (current_class_type) > 0))
5423 {
5424 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5425 if (tmpl == error_mark_node)
5426 return error_mark_node;
5427
5428 /* Hide template friend classes that haven't been declared yet. */
5429 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5430 {
5431 DECL_ANTICIPATED (tmpl) = 1;
5432 DECL_FRIEND_P (tmpl) = 1;
5433 }
5434 }
5435
5436 if (is_primary)
5437 {
5438 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5439 int i;
5440
5441 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5442 if (DECL_CONV_FN_P (tmpl))
5443 {
5444 int depth = TMPL_PARMS_DEPTH (parms);
5445
5446 /* It is a conversion operator. See if the type converted to
5447 depends on innermost template operands. */
5448
5449 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5450 depth))
5451 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5452 }
5453
5454 /* Give template template parms a DECL_CONTEXT of the template
5455 for which they are a parameter. */
5456 parms = INNERMOST_TEMPLATE_PARMS (parms);
5457 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5458 {
5459 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5460 if (TREE_CODE (parm) == TEMPLATE_DECL)
5461 DECL_CONTEXT (parm) = tmpl;
5462 }
5463
5464 if (TREE_CODE (decl) == TYPE_DECL
5465 && TYPE_DECL_ALIAS_P (decl)
5466 && complex_alias_template_p (tmpl))
5467 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5468 }
5469
5470 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5471 back to its most general template. If TMPL is a specialization,
5472 ARGS may only have the innermost set of arguments. Add the missing
5473 argument levels if necessary. */
5474 if (DECL_TEMPLATE_INFO (tmpl))
5475 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5476
5477 info = build_template_info (tmpl, args);
5478
5479 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5480 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5481 else
5482 {
5483 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5484 retrofit_lang_decl (decl);
5485 if (DECL_LANG_SPECIFIC (decl))
5486 DECL_TEMPLATE_INFO (decl) = info;
5487 }
5488
5489 if (flag_implicit_templates
5490 && !is_friend
5491 && TREE_PUBLIC (decl)
5492 && VAR_OR_FUNCTION_DECL_P (decl))
5493 /* Set DECL_COMDAT on template instantiations; if we force
5494 them to be emitted by explicit instantiation or -frepo,
5495 mark_needed will tell cgraph to do the right thing. */
5496 DECL_COMDAT (decl) = true;
5497
5498 return DECL_TEMPLATE_RESULT (tmpl);
5499 }
5500
5501 tree
5502 push_template_decl (tree decl)
5503 {
5504 return push_template_decl_real (decl, false);
5505 }
5506
5507 /* FN is an inheriting constructor that inherits from the constructor
5508 template INHERITED; turn FN into a constructor template with a matching
5509 template header. */
5510
5511 tree
5512 add_inherited_template_parms (tree fn, tree inherited)
5513 {
5514 tree inner_parms
5515 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5516 inner_parms = copy_node (inner_parms);
5517 tree parms
5518 = tree_cons (size_int (processing_template_decl + 1),
5519 inner_parms, current_template_parms);
5520 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5521 tree args = template_parms_to_args (parms);
5522 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5523 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5524 DECL_TEMPLATE_RESULT (tmpl) = fn;
5525 DECL_ARTIFICIAL (tmpl) = true;
5526 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5527 return tmpl;
5528 }
5529
5530 /* Called when a class template TYPE is redeclared with the indicated
5531 template PARMS, e.g.:
5532
5533 template <class T> struct S;
5534 template <class T> struct S {}; */
5535
5536 bool
5537 redeclare_class_template (tree type, tree parms, tree cons)
5538 {
5539 tree tmpl;
5540 tree tmpl_parms;
5541 int i;
5542
5543 if (!TYPE_TEMPLATE_INFO (type))
5544 {
5545 error ("%qT is not a template type", type);
5546 return false;
5547 }
5548
5549 tmpl = TYPE_TI_TEMPLATE (type);
5550 if (!PRIMARY_TEMPLATE_P (tmpl))
5551 /* The type is nested in some template class. Nothing to worry
5552 about here; there are no new template parameters for the nested
5553 type. */
5554 return true;
5555
5556 if (!parms)
5557 {
5558 error ("template specifiers not specified in declaration of %qD",
5559 tmpl);
5560 return false;
5561 }
5562
5563 parms = INNERMOST_TEMPLATE_PARMS (parms);
5564 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5565
5566 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5567 {
5568 error_n (input_location, TREE_VEC_LENGTH (parms),
5569 "redeclared with %d template parameter",
5570 "redeclared with %d template parameters",
5571 TREE_VEC_LENGTH (parms));
5572 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5573 "previous declaration %qD used %d template parameter",
5574 "previous declaration %qD used %d template parameters",
5575 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5576 return false;
5577 }
5578
5579 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5580 {
5581 tree tmpl_parm;
5582 tree parm;
5583 tree tmpl_default;
5584 tree parm_default;
5585
5586 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5587 || TREE_VEC_ELT (parms, i) == error_mark_node)
5588 continue;
5589
5590 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5591 if (error_operand_p (tmpl_parm))
5592 return false;
5593
5594 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5595 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5596 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5597
5598 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5599 TEMPLATE_DECL. */
5600 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5601 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5602 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5603 || (TREE_CODE (tmpl_parm) != PARM_DECL
5604 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5605 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5606 || (TREE_CODE (tmpl_parm) == PARM_DECL
5607 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5608 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5609 {
5610 error ("template parameter %q+#D", tmpl_parm);
5611 error ("redeclared here as %q#D", parm);
5612 return false;
5613 }
5614
5615 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5616 {
5617 /* We have in [temp.param]:
5618
5619 A template-parameter may not be given default arguments
5620 by two different declarations in the same scope. */
5621 error_at (input_location, "redefinition of default argument for %q#D", parm);
5622 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5623 "original definition appeared here");
5624 return false;
5625 }
5626
5627 if (parm_default != NULL_TREE)
5628 /* Update the previous template parameters (which are the ones
5629 that will really count) with the new default value. */
5630 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5631 else if (tmpl_default != NULL_TREE)
5632 /* Update the new parameters, too; they'll be used as the
5633 parameters for any members. */
5634 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5635
5636 /* Give each template template parm in this redeclaration a
5637 DECL_CONTEXT of the template for which they are a parameter. */
5638 if (TREE_CODE (parm) == TEMPLATE_DECL)
5639 {
5640 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5641 DECL_CONTEXT (parm) = tmpl;
5642 }
5643 }
5644
5645 // Cannot redeclare a class template with a different set of constraints.
5646 if (!equivalent_constraints (get_constraints (tmpl), cons))
5647 {
5648 error_at (input_location, "redeclaration %q#D with different "
5649 "constraints", tmpl);
5650 inform (DECL_SOURCE_LOCATION (tmpl),
5651 "original declaration appeared here");
5652 }
5653
5654 return true;
5655 }
5656
5657 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5658 to be used when the caller has already checked
5659 (processing_template_decl
5660 && !instantiation_dependent_expression_p (expr)
5661 && potential_constant_expression (expr))
5662 and cleared processing_template_decl. */
5663
5664 tree
5665 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5666 {
5667 return tsubst_copy_and_build (expr,
5668 /*args=*/NULL_TREE,
5669 complain,
5670 /*in_decl=*/NULL_TREE,
5671 /*function_p=*/false,
5672 /*integral_constant_expression_p=*/true);
5673 }
5674
5675 /* Simplify EXPR if it is a non-dependent expression. Returns the
5676 (possibly simplified) expression. */
5677
5678 tree
5679 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5680 {
5681 if (expr == NULL_TREE)
5682 return NULL_TREE;
5683
5684 /* If we're in a template, but EXPR isn't value dependent, simplify
5685 it. We're supposed to treat:
5686
5687 template <typename T> void f(T[1 + 1]);
5688 template <typename T> void f(T[2]);
5689
5690 as two declarations of the same function, for example. */
5691 if (processing_template_decl
5692 && potential_nondependent_constant_expression (expr))
5693 {
5694 processing_template_decl_sentinel s;
5695 expr = instantiate_non_dependent_expr_internal (expr, complain);
5696 }
5697 return expr;
5698 }
5699
5700 tree
5701 instantiate_non_dependent_expr (tree expr)
5702 {
5703 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5704 }
5705
5706 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5707 an uninstantiated expression. */
5708
5709 tree
5710 instantiate_non_dependent_or_null (tree expr)
5711 {
5712 if (expr == NULL_TREE)
5713 return NULL_TREE;
5714 if (processing_template_decl)
5715 {
5716 if (!potential_nondependent_constant_expression (expr))
5717 expr = NULL_TREE;
5718 else
5719 {
5720 processing_template_decl_sentinel s;
5721 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5722 }
5723 }
5724 return expr;
5725 }
5726
5727 /* True iff T is a specialization of a variable template. */
5728
5729 bool
5730 variable_template_specialization_p (tree t)
5731 {
5732 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5733 return false;
5734 tree tmpl = DECL_TI_TEMPLATE (t);
5735 return variable_template_p (tmpl);
5736 }
5737
5738 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5739 template declaration, or a TYPE_DECL for an alias declaration. */
5740
5741 bool
5742 alias_type_or_template_p (tree t)
5743 {
5744 if (t == NULL_TREE)
5745 return false;
5746 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5747 || (TYPE_P (t)
5748 && TYPE_NAME (t)
5749 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5750 || DECL_ALIAS_TEMPLATE_P (t));
5751 }
5752
5753 /* Return TRUE iff T is a specialization of an alias template. */
5754
5755 bool
5756 alias_template_specialization_p (const_tree t)
5757 {
5758 /* It's an alias template specialization if it's an alias and its
5759 TYPE_NAME is a specialization of a primary template. */
5760 if (TYPE_ALIAS_P (t))
5761 {
5762 tree name = TYPE_NAME (t);
5763 if (DECL_LANG_SPECIFIC (name))
5764 if (tree ti = DECL_TEMPLATE_INFO (name))
5765 {
5766 tree tmpl = TI_TEMPLATE (ti);
5767 return PRIMARY_TEMPLATE_P (tmpl);
5768 }
5769 }
5770 return false;
5771 }
5772
5773 /* An alias template is complex from a SFINAE perspective if a template-id
5774 using that alias can be ill-formed when the expansion is not, as with
5775 the void_t template. We determine this by checking whether the
5776 expansion for the alias template uses all its template parameters. */
5777
5778 struct uses_all_template_parms_data
5779 {
5780 int level;
5781 bool *seen;
5782 };
5783
5784 static int
5785 uses_all_template_parms_r (tree t, void *data_)
5786 {
5787 struct uses_all_template_parms_data &data
5788 = *(struct uses_all_template_parms_data*)data_;
5789 tree idx = get_template_parm_index (t);
5790
5791 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5792 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5793 return 0;
5794 }
5795
5796 static bool
5797 complex_alias_template_p (const_tree tmpl)
5798 {
5799 struct uses_all_template_parms_data data;
5800 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5801 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5802 data.level = TMPL_PARMS_DEPTH (parms);
5803 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5804 data.seen = XALLOCAVEC (bool, len);
5805 for (int i = 0; i < len; ++i)
5806 data.seen[i] = false;
5807
5808 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5809 for (int i = 0; i < len; ++i)
5810 if (!data.seen[i])
5811 return true;
5812 return false;
5813 }
5814
5815 /* Return TRUE iff T is a specialization of a complex alias template with
5816 dependent template-arguments. */
5817
5818 bool
5819 dependent_alias_template_spec_p (const_tree t)
5820 {
5821 return (alias_template_specialization_p (t)
5822 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5823 && (any_dependent_template_arguments_p
5824 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5825 }
5826
5827 /* Return the number of innermost template parameters in TMPL. */
5828
5829 static int
5830 num_innermost_template_parms (tree tmpl)
5831 {
5832 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5833 return TREE_VEC_LENGTH (parms);
5834 }
5835
5836 /* Return either TMPL or another template that it is equivalent to under DR
5837 1286: An alias that just changes the name of a template is equivalent to
5838 the other template. */
5839
5840 static tree
5841 get_underlying_template (tree tmpl)
5842 {
5843 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5844 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5845 {
5846 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5847 if (TYPE_TEMPLATE_INFO (result))
5848 {
5849 tree sub = TYPE_TI_TEMPLATE (result);
5850 if (PRIMARY_TEMPLATE_P (sub)
5851 && (num_innermost_template_parms (tmpl)
5852 == num_innermost_template_parms (sub)))
5853 {
5854 tree alias_args = INNERMOST_TEMPLATE_ARGS
5855 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5856 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5857 break;
5858 /* The alias type is equivalent to the pattern of the
5859 underlying template, so strip the alias. */
5860 tmpl = sub;
5861 continue;
5862 }
5863 }
5864 break;
5865 }
5866 return tmpl;
5867 }
5868
5869 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5870 must be a function or a pointer-to-function type, as specified
5871 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5872 and check that the resulting function has external linkage. */
5873
5874 static tree
5875 convert_nontype_argument_function (tree type, tree expr,
5876 tsubst_flags_t complain)
5877 {
5878 tree fns = expr;
5879 tree fn, fn_no_ptr;
5880 linkage_kind linkage;
5881
5882 fn = instantiate_type (type, fns, tf_none);
5883 if (fn == error_mark_node)
5884 return error_mark_node;
5885
5886 fn_no_ptr = fn;
5887 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5888 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5889 if (BASELINK_P (fn_no_ptr))
5890 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5891
5892 /* [temp.arg.nontype]/1
5893
5894 A template-argument for a non-type, non-template template-parameter
5895 shall be one of:
5896 [...]
5897 -- the address of an object or function with external [C++11: or
5898 internal] linkage. */
5899
5900 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5901 {
5902 if (complain & tf_error)
5903 {
5904 error ("%qE is not a valid template argument for type %qT",
5905 expr, type);
5906 if (TYPE_PTR_P (type))
5907 error ("it must be the address of a function with "
5908 "external linkage");
5909 else
5910 error ("it must be the name of a function with "
5911 "external linkage");
5912 }
5913 return NULL_TREE;
5914 }
5915
5916 linkage = decl_linkage (fn_no_ptr);
5917 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5918 {
5919 if (complain & tf_error)
5920 {
5921 if (cxx_dialect >= cxx11)
5922 error ("%qE is not a valid template argument for type %qT "
5923 "because %qD has no linkage",
5924 expr, type, fn_no_ptr);
5925 else
5926 error ("%qE is not a valid template argument for type %qT "
5927 "because %qD does not have external linkage",
5928 expr, type, fn_no_ptr);
5929 }
5930 return NULL_TREE;
5931 }
5932
5933 return fn;
5934 }
5935
5936 /* Subroutine of convert_nontype_argument.
5937 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5938 Emit an error otherwise. */
5939
5940 static bool
5941 check_valid_ptrmem_cst_expr (tree type, tree expr,
5942 tsubst_flags_t complain)
5943 {
5944 STRIP_NOPS (expr);
5945 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5946 return true;
5947 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5948 return true;
5949 if (processing_template_decl
5950 && TREE_CODE (expr) == ADDR_EXPR
5951 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5952 return true;
5953 if (complain & tf_error)
5954 {
5955 error ("%qE is not a valid template argument for type %qT",
5956 expr, type);
5957 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5958 }
5959 return false;
5960 }
5961
5962 /* Returns TRUE iff the address of OP is value-dependent.
5963
5964 14.6.2.4 [temp.dep.temp]:
5965 A non-integral non-type template-argument is dependent if its type is
5966 dependent or it has either of the following forms
5967 qualified-id
5968 & qualified-id
5969 and contains a nested-name-specifier which specifies a class-name that
5970 names a dependent type.
5971
5972 We generalize this to just say that the address of a member of a
5973 dependent class is value-dependent; the above doesn't cover the
5974 address of a static data member named with an unqualified-id. */
5975
5976 static bool
5977 has_value_dependent_address (tree op)
5978 {
5979 /* We could use get_inner_reference here, but there's no need;
5980 this is only relevant for template non-type arguments, which
5981 can only be expressed as &id-expression. */
5982 if (DECL_P (op))
5983 {
5984 tree ctx = CP_DECL_CONTEXT (op);
5985 if (TYPE_P (ctx) && dependent_type_p (ctx))
5986 return true;
5987 }
5988
5989 return false;
5990 }
5991
5992 /* The next set of functions are used for providing helpful explanatory
5993 diagnostics for failed overload resolution. Their messages should be
5994 indented by two spaces for consistency with the messages in
5995 call.c */
5996
5997 static int
5998 unify_success (bool /*explain_p*/)
5999 {
6000 return 0;
6001 }
6002
6003 static int
6004 unify_parameter_deduction_failure (bool explain_p, tree parm)
6005 {
6006 if (explain_p)
6007 inform (input_location,
6008 " couldn't deduce template parameter %qD", parm);
6009 return 1;
6010 }
6011
6012 static int
6013 unify_invalid (bool /*explain_p*/)
6014 {
6015 return 1;
6016 }
6017
6018 static int
6019 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6020 {
6021 if (explain_p)
6022 inform (input_location,
6023 " types %qT and %qT have incompatible cv-qualifiers",
6024 parm, arg);
6025 return 1;
6026 }
6027
6028 static int
6029 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6030 {
6031 if (explain_p)
6032 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6033 return 1;
6034 }
6035
6036 static int
6037 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6038 {
6039 if (explain_p)
6040 inform (input_location,
6041 " template parameter %qD is not a parameter pack, but "
6042 "argument %qD is",
6043 parm, arg);
6044 return 1;
6045 }
6046
6047 static int
6048 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6049 {
6050 if (explain_p)
6051 inform (input_location,
6052 " template argument %qE does not match "
6053 "pointer-to-member constant %qE",
6054 arg, parm);
6055 return 1;
6056 }
6057
6058 static int
6059 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6060 {
6061 if (explain_p)
6062 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6063 return 1;
6064 }
6065
6066 static int
6067 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6068 {
6069 if (explain_p)
6070 inform (input_location,
6071 " inconsistent parameter pack deduction with %qT and %qT",
6072 old_arg, new_arg);
6073 return 1;
6074 }
6075
6076 static int
6077 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6078 {
6079 if (explain_p)
6080 {
6081 if (TYPE_P (parm))
6082 inform (input_location,
6083 " deduced conflicting types for parameter %qT (%qT and %qT)",
6084 parm, first, second);
6085 else
6086 inform (input_location,
6087 " deduced conflicting values for non-type parameter "
6088 "%qE (%qE and %qE)", parm, first, second);
6089 }
6090 return 1;
6091 }
6092
6093 static int
6094 unify_vla_arg (bool explain_p, tree arg)
6095 {
6096 if (explain_p)
6097 inform (input_location,
6098 " variable-sized array type %qT is not "
6099 "a valid template argument",
6100 arg);
6101 return 1;
6102 }
6103
6104 static int
6105 unify_method_type_error (bool explain_p, tree arg)
6106 {
6107 if (explain_p)
6108 inform (input_location,
6109 " member function type %qT is not a valid template argument",
6110 arg);
6111 return 1;
6112 }
6113
6114 static int
6115 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6116 {
6117 if (explain_p)
6118 {
6119 if (least_p)
6120 inform_n (input_location, wanted,
6121 " candidate expects at least %d argument, %d provided",
6122 " candidate expects at least %d arguments, %d provided",
6123 wanted, have);
6124 else
6125 inform_n (input_location, wanted,
6126 " candidate expects %d argument, %d provided",
6127 " candidate expects %d arguments, %d provided",
6128 wanted, have);
6129 }
6130 return 1;
6131 }
6132
6133 static int
6134 unify_too_many_arguments (bool explain_p, int have, int wanted)
6135 {
6136 return unify_arity (explain_p, have, wanted);
6137 }
6138
6139 static int
6140 unify_too_few_arguments (bool explain_p, int have, int wanted,
6141 bool least_p = false)
6142 {
6143 return unify_arity (explain_p, have, wanted, least_p);
6144 }
6145
6146 static int
6147 unify_arg_conversion (bool explain_p, tree to_type,
6148 tree from_type, tree arg)
6149 {
6150 if (explain_p)
6151 inform (EXPR_LOC_OR_LOC (arg, input_location),
6152 " cannot convert %qE (type %qT) to type %qT",
6153 arg, from_type, to_type);
6154 return 1;
6155 }
6156
6157 static int
6158 unify_no_common_base (bool explain_p, enum template_base_result r,
6159 tree parm, tree arg)
6160 {
6161 if (explain_p)
6162 switch (r)
6163 {
6164 case tbr_ambiguous_baseclass:
6165 inform (input_location, " %qT is an ambiguous base class of %qT",
6166 parm, arg);
6167 break;
6168 default:
6169 inform (input_location, " %qT is not derived from %qT", arg, parm);
6170 break;
6171 }
6172 return 1;
6173 }
6174
6175 static int
6176 unify_inconsistent_template_template_parameters (bool explain_p)
6177 {
6178 if (explain_p)
6179 inform (input_location,
6180 " template parameters of a template template argument are "
6181 "inconsistent with other deduced template arguments");
6182 return 1;
6183 }
6184
6185 static int
6186 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6187 {
6188 if (explain_p)
6189 inform (input_location,
6190 " can't deduce a template for %qT from non-template type %qT",
6191 parm, arg);
6192 return 1;
6193 }
6194
6195 static int
6196 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6197 {
6198 if (explain_p)
6199 inform (input_location,
6200 " template argument %qE does not match %qE", arg, parm);
6201 return 1;
6202 }
6203
6204 static int
6205 unify_overload_resolution_failure (bool explain_p, tree arg)
6206 {
6207 if (explain_p)
6208 inform (input_location,
6209 " could not resolve address from overloaded function %qE",
6210 arg);
6211 return 1;
6212 }
6213
6214 /* Attempt to convert the non-type template parameter EXPR to the
6215 indicated TYPE. If the conversion is successful, return the
6216 converted value. If the conversion is unsuccessful, return
6217 NULL_TREE if we issued an error message, or error_mark_node if we
6218 did not. We issue error messages for out-and-out bad template
6219 parameters, but not simply because the conversion failed, since we
6220 might be just trying to do argument deduction. Both TYPE and EXPR
6221 must be non-dependent.
6222
6223 The conversion follows the special rules described in
6224 [temp.arg.nontype], and it is much more strict than an implicit
6225 conversion.
6226
6227 This function is called twice for each template argument (see
6228 lookup_template_class for a more accurate description of this
6229 problem). This means that we need to handle expressions which
6230 are not valid in a C++ source, but can be created from the
6231 first call (for instance, casts to perform conversions). These
6232 hacks can go away after we fix the double coercion problem. */
6233
6234 static tree
6235 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6236 {
6237 tree expr_type;
6238
6239 /* Detect immediately string literals as invalid non-type argument.
6240 This special-case is not needed for correctness (we would easily
6241 catch this later), but only to provide better diagnostic for this
6242 common user mistake. As suggested by DR 100, we do not mention
6243 linkage issues in the diagnostic as this is not the point. */
6244 /* FIXME we're making this OK. */
6245 if (TREE_CODE (expr) == STRING_CST)
6246 {
6247 if (complain & tf_error)
6248 error ("%qE is not a valid template argument for type %qT "
6249 "because string literals can never be used in this context",
6250 expr, type);
6251 return NULL_TREE;
6252 }
6253
6254 /* Add the ADDR_EXPR now for the benefit of
6255 value_dependent_expression_p. */
6256 if (TYPE_PTROBV_P (type)
6257 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6258 {
6259 expr = decay_conversion (expr, complain);
6260 if (expr == error_mark_node)
6261 return error_mark_node;
6262 }
6263
6264 /* If we are in a template, EXPR may be non-dependent, but still
6265 have a syntactic, rather than semantic, form. For example, EXPR
6266 might be a SCOPE_REF, rather than the VAR_DECL to which the
6267 SCOPE_REF refers. Preserving the qualifying scope is necessary
6268 so that access checking can be performed when the template is
6269 instantiated -- but here we need the resolved form so that we can
6270 convert the argument. */
6271 bool non_dep = false;
6272 if (TYPE_REF_OBJ_P (type)
6273 && has_value_dependent_address (expr))
6274 /* If we want the address and it's value-dependent, don't fold. */;
6275 else if (processing_template_decl
6276 && potential_nondependent_constant_expression (expr))
6277 non_dep = true;
6278 if (error_operand_p (expr))
6279 return error_mark_node;
6280 expr_type = TREE_TYPE (expr);
6281 if (TREE_CODE (type) == REFERENCE_TYPE)
6282 expr = mark_lvalue_use (expr);
6283 else
6284 expr = mark_rvalue_use (expr);
6285
6286 /* If the argument is non-dependent, perform any conversions in
6287 non-dependent context as well. */
6288 processing_template_decl_sentinel s (non_dep);
6289 if (non_dep)
6290 expr = instantiate_non_dependent_expr_internal (expr, complain);
6291
6292 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6293 to a non-type argument of "nullptr". */
6294 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6295 expr = fold_simple (convert (type, expr));
6296
6297 /* In C++11, integral or enumeration non-type template arguments can be
6298 arbitrary constant expressions. Pointer and pointer to
6299 member arguments can be general constant expressions that evaluate
6300 to a null value, but otherwise still need to be of a specific form. */
6301 if (cxx_dialect >= cxx11)
6302 {
6303 if (TREE_CODE (expr) == PTRMEM_CST)
6304 /* A PTRMEM_CST is already constant, and a valid template
6305 argument for a parameter of pointer to member type, we just want
6306 to leave it in that form rather than lower it to a
6307 CONSTRUCTOR. */;
6308 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6309 expr = maybe_constant_value (expr);
6310 else if (cxx_dialect >= cxx1z)
6311 {
6312 if (TREE_CODE (type) != REFERENCE_TYPE)
6313 expr = maybe_constant_value (expr);
6314 else if (REFERENCE_REF_P (expr))
6315 {
6316 expr = TREE_OPERAND (expr, 0);
6317 expr = maybe_constant_value (expr);
6318 expr = convert_from_reference (expr);
6319 }
6320 }
6321 else if (TYPE_PTR_OR_PTRMEM_P (type))
6322 {
6323 tree folded = maybe_constant_value (expr);
6324 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6325 : null_member_pointer_value_p (folded))
6326 expr = folded;
6327 }
6328 }
6329
6330 /* HACK: Due to double coercion, we can get a
6331 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6332 which is the tree that we built on the first call (see
6333 below when coercing to reference to object or to reference to
6334 function). We just strip everything and get to the arg.
6335 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6336 for examples. */
6337 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6338 {
6339 tree probe_type, probe = expr;
6340 if (REFERENCE_REF_P (probe))
6341 probe = TREE_OPERAND (probe, 0);
6342 probe_type = TREE_TYPE (probe);
6343 if (TREE_CODE (probe) == NOP_EXPR)
6344 {
6345 /* ??? Maybe we could use convert_from_reference here, but we
6346 would need to relax its constraints because the NOP_EXPR
6347 could actually change the type to something more cv-qualified,
6348 and this is not folded by convert_from_reference. */
6349 tree addr = TREE_OPERAND (probe, 0);
6350 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6351 && TREE_CODE (addr) == ADDR_EXPR
6352 && TYPE_PTR_P (TREE_TYPE (addr))
6353 && (same_type_ignoring_top_level_qualifiers_p
6354 (TREE_TYPE (probe_type),
6355 TREE_TYPE (TREE_TYPE (addr)))))
6356 {
6357 expr = TREE_OPERAND (addr, 0);
6358 expr_type = TREE_TYPE (probe_type);
6359 }
6360 }
6361 }
6362
6363 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6364 parameter is a pointer to object, through decay and
6365 qualification conversion. Let's strip everything. */
6366 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6367 {
6368 tree probe = expr;
6369 STRIP_NOPS (probe);
6370 if (TREE_CODE (probe) == ADDR_EXPR
6371 && TYPE_PTR_P (TREE_TYPE (probe)))
6372 {
6373 /* Skip the ADDR_EXPR only if it is part of the decay for
6374 an array. Otherwise, it is part of the original argument
6375 in the source code. */
6376 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6377 probe = TREE_OPERAND (probe, 0);
6378 expr = probe;
6379 expr_type = TREE_TYPE (expr);
6380 }
6381 }
6382
6383 /* [temp.arg.nontype]/5, bullet 1
6384
6385 For a non-type template-parameter of integral or enumeration type,
6386 integral promotions (_conv.prom_) and integral conversions
6387 (_conv.integral_) are applied. */
6388 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6389 {
6390 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6391 t = maybe_constant_value (t);
6392 if (t != error_mark_node)
6393 expr = t;
6394
6395 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6396 return error_mark_node;
6397
6398 /* Notice that there are constant expressions like '4 % 0' which
6399 do not fold into integer constants. */
6400 if (TREE_CODE (expr) != INTEGER_CST)
6401 {
6402 if (complain & tf_error)
6403 {
6404 int errs = errorcount, warns = warningcount + werrorcount;
6405 if (processing_template_decl
6406 && !require_potential_constant_expression (expr))
6407 return NULL_TREE;
6408 expr = cxx_constant_value (expr);
6409 if (errorcount > errs || warningcount + werrorcount > warns)
6410 inform (EXPR_LOC_OR_LOC (expr, input_location),
6411 "in template argument for type %qT ", type);
6412 if (expr == error_mark_node)
6413 return NULL_TREE;
6414 /* else cxx_constant_value complained but gave us
6415 a real constant, so go ahead. */
6416 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6417 }
6418 else
6419 return NULL_TREE;
6420 }
6421
6422 /* Avoid typedef problems. */
6423 if (TREE_TYPE (expr) != type)
6424 expr = fold_convert (type, expr);
6425 }
6426 /* [temp.arg.nontype]/5, bullet 2
6427
6428 For a non-type template-parameter of type pointer to object,
6429 qualification conversions (_conv.qual_) and the array-to-pointer
6430 conversion (_conv.array_) are applied. */
6431 else if (TYPE_PTROBV_P (type))
6432 {
6433 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6434
6435 A template-argument for a non-type, non-template template-parameter
6436 shall be one of: [...]
6437
6438 -- the name of a non-type template-parameter;
6439 -- the address of an object or function with external linkage, [...]
6440 expressed as "& id-expression" where the & is optional if the name
6441 refers to a function or array, or if the corresponding
6442 template-parameter is a reference.
6443
6444 Here, we do not care about functions, as they are invalid anyway
6445 for a parameter of type pointer-to-object. */
6446
6447 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6448 /* Non-type template parameters are OK. */
6449 ;
6450 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6451 /* Null pointer values are OK in C++11. */;
6452 else if (TREE_CODE (expr) != ADDR_EXPR
6453 && TREE_CODE (expr_type) != ARRAY_TYPE)
6454 {
6455 if (VAR_P (expr))
6456 {
6457 if (complain & tf_error)
6458 error ("%qD is not a valid template argument "
6459 "because %qD is a variable, not the address of "
6460 "a variable", expr, expr);
6461 return NULL_TREE;
6462 }
6463 if (POINTER_TYPE_P (expr_type))
6464 {
6465 if (complain & tf_error)
6466 error ("%qE is not a valid template argument for %qT "
6467 "because it is not the address of a variable",
6468 expr, type);
6469 return NULL_TREE;
6470 }
6471 /* Other values, like integer constants, might be valid
6472 non-type arguments of some other type. */
6473 return error_mark_node;
6474 }
6475 else
6476 {
6477 tree decl;
6478
6479 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6480 ? TREE_OPERAND (expr, 0) : expr);
6481 if (!VAR_P (decl))
6482 {
6483 if (complain & tf_error)
6484 error ("%qE is not a valid template argument of type %qT "
6485 "because %qE is not a variable", expr, type, decl);
6486 return NULL_TREE;
6487 }
6488 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6489 {
6490 if (complain & tf_error)
6491 error ("%qE is not a valid template argument of type %qT "
6492 "because %qD does not have external linkage",
6493 expr, type, decl);
6494 return NULL_TREE;
6495 }
6496 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6497 {
6498 if (complain & tf_error)
6499 error ("%qE is not a valid template argument of type %qT "
6500 "because %qD has no linkage", expr, type, decl);
6501 return NULL_TREE;
6502 }
6503 }
6504
6505 expr = decay_conversion (expr, complain);
6506 if (expr == error_mark_node)
6507 return error_mark_node;
6508
6509 expr = perform_qualification_conversions (type, expr);
6510 if (expr == error_mark_node)
6511 return error_mark_node;
6512 }
6513 /* [temp.arg.nontype]/5, bullet 3
6514
6515 For a non-type template-parameter of type reference to object, no
6516 conversions apply. The type referred to by the reference may be more
6517 cv-qualified than the (otherwise identical) type of the
6518 template-argument. The template-parameter is bound directly to the
6519 template-argument, which must be an lvalue. */
6520 else if (TYPE_REF_OBJ_P (type))
6521 {
6522 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6523 expr_type))
6524 return error_mark_node;
6525
6526 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6527 {
6528 if (complain & tf_error)
6529 error ("%qE is not a valid template argument for type %qT "
6530 "because of conflicts in cv-qualification", expr, type);
6531 return NULL_TREE;
6532 }
6533
6534 if (!lvalue_p (expr))
6535 {
6536 if (complain & tf_error)
6537 error ("%qE is not a valid template argument for type %qT "
6538 "because it is not an lvalue", expr, type);
6539 return NULL_TREE;
6540 }
6541
6542 /* [temp.arg.nontype]/1
6543
6544 A template-argument for a non-type, non-template template-parameter
6545 shall be one of: [...]
6546
6547 -- the address of an object or function with external linkage. */
6548 if (INDIRECT_REF_P (expr)
6549 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6550 {
6551 expr = TREE_OPERAND (expr, 0);
6552 if (DECL_P (expr))
6553 {
6554 if (complain & tf_error)
6555 error ("%q#D is not a valid template argument for type %qT "
6556 "because a reference variable does not have a constant "
6557 "address", expr, type);
6558 return NULL_TREE;
6559 }
6560 }
6561
6562 if (!DECL_P (expr))
6563 {
6564 if (complain & tf_error)
6565 error ("%qE is not a valid template argument for type %qT "
6566 "because it is not an object with linkage",
6567 expr, type);
6568 return NULL_TREE;
6569 }
6570
6571 /* DR 1155 allows internal linkage in C++11 and up. */
6572 linkage_kind linkage = decl_linkage (expr);
6573 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6574 {
6575 if (complain & tf_error)
6576 error ("%qE is not a valid template argument for type %qT "
6577 "because object %qD does not have linkage",
6578 expr, type, expr);
6579 return NULL_TREE;
6580 }
6581
6582 expr = build_nop (type, build_address (expr));
6583 }
6584 /* [temp.arg.nontype]/5, bullet 4
6585
6586 For a non-type template-parameter of type pointer to function, only
6587 the function-to-pointer conversion (_conv.func_) is applied. If the
6588 template-argument represents a set of overloaded functions (or a
6589 pointer to such), the matching function is selected from the set
6590 (_over.over_). */
6591 else if (TYPE_PTRFN_P (type))
6592 {
6593 /* If the argument is a template-id, we might not have enough
6594 context information to decay the pointer. */
6595 if (!type_unknown_p (expr_type))
6596 {
6597 expr = decay_conversion (expr, complain);
6598 if (expr == error_mark_node)
6599 return error_mark_node;
6600 }
6601
6602 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6603 /* Null pointer values are OK in C++11. */
6604 return perform_qualification_conversions (type, expr);
6605
6606 expr = convert_nontype_argument_function (type, expr, complain);
6607 if (!expr || expr == error_mark_node)
6608 return expr;
6609 }
6610 /* [temp.arg.nontype]/5, bullet 5
6611
6612 For a non-type template-parameter of type reference to function, no
6613 conversions apply. If the template-argument represents a set of
6614 overloaded functions, the matching function is selected from the set
6615 (_over.over_). */
6616 else if (TYPE_REFFN_P (type))
6617 {
6618 if (TREE_CODE (expr) == ADDR_EXPR)
6619 {
6620 if (complain & tf_error)
6621 {
6622 error ("%qE is not a valid template argument for type %qT "
6623 "because it is a pointer", expr, type);
6624 inform (input_location, "try using %qE instead",
6625 TREE_OPERAND (expr, 0));
6626 }
6627 return NULL_TREE;
6628 }
6629
6630 expr = convert_nontype_argument_function (type, expr, complain);
6631 if (!expr || expr == error_mark_node)
6632 return expr;
6633
6634 expr = build_nop (type, build_address (expr));
6635 }
6636 /* [temp.arg.nontype]/5, bullet 6
6637
6638 For a non-type template-parameter of type pointer to member function,
6639 no conversions apply. If the template-argument represents a set of
6640 overloaded member functions, the matching member function is selected
6641 from the set (_over.over_). */
6642 else if (TYPE_PTRMEMFUNC_P (type))
6643 {
6644 expr = instantiate_type (type, expr, tf_none);
6645 if (expr == error_mark_node)
6646 return error_mark_node;
6647
6648 /* [temp.arg.nontype] bullet 1 says the pointer to member
6649 expression must be a pointer-to-member constant. */
6650 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6651 return error_mark_node;
6652
6653 /* There is no way to disable standard conversions in
6654 resolve_address_of_overloaded_function (called by
6655 instantiate_type). It is possible that the call succeeded by
6656 converting &B::I to &D::I (where B is a base of D), so we need
6657 to reject this conversion here.
6658
6659 Actually, even if there was a way to disable standard conversions,
6660 it would still be better to reject them here so that we can
6661 provide a superior diagnostic. */
6662 if (!same_type_p (TREE_TYPE (expr), type))
6663 {
6664 if (complain & tf_error)
6665 {
6666 error ("%qE is not a valid template argument for type %qT "
6667 "because it is of type %qT", expr, type,
6668 TREE_TYPE (expr));
6669 /* If we are just one standard conversion off, explain. */
6670 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6671 inform (input_location,
6672 "standard conversions are not allowed in this context");
6673 }
6674 return NULL_TREE;
6675 }
6676 }
6677 /* [temp.arg.nontype]/5, bullet 7
6678
6679 For a non-type template-parameter of type pointer to data member,
6680 qualification conversions (_conv.qual_) are applied. */
6681 else if (TYPE_PTRDATAMEM_P (type))
6682 {
6683 /* [temp.arg.nontype] bullet 1 says the pointer to member
6684 expression must be a pointer-to-member constant. */
6685 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6686 return error_mark_node;
6687
6688 expr = perform_qualification_conversions (type, expr);
6689 if (expr == error_mark_node)
6690 return expr;
6691 }
6692 else if (NULLPTR_TYPE_P (type))
6693 {
6694 if (expr != nullptr_node)
6695 {
6696 if (complain & tf_error)
6697 error ("%qE is not a valid template argument for type %qT "
6698 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6699 return NULL_TREE;
6700 }
6701 return expr;
6702 }
6703 /* A template non-type parameter must be one of the above. */
6704 else
6705 gcc_unreachable ();
6706
6707 /* Sanity check: did we actually convert the argument to the
6708 right type? */
6709 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6710 (type, TREE_TYPE (expr)));
6711 return convert_from_reference (expr);
6712 }
6713
6714 /* Subroutine of coerce_template_template_parms, which returns 1 if
6715 PARM_PARM and ARG_PARM match using the rule for the template
6716 parameters of template template parameters. Both PARM and ARG are
6717 template parameters; the rest of the arguments are the same as for
6718 coerce_template_template_parms.
6719 */
6720 static int
6721 coerce_template_template_parm (tree parm,
6722 tree arg,
6723 tsubst_flags_t complain,
6724 tree in_decl,
6725 tree outer_args)
6726 {
6727 if (arg == NULL_TREE || error_operand_p (arg)
6728 || parm == NULL_TREE || error_operand_p (parm))
6729 return 0;
6730
6731 if (TREE_CODE (arg) != TREE_CODE (parm))
6732 return 0;
6733
6734 switch (TREE_CODE (parm))
6735 {
6736 case TEMPLATE_DECL:
6737 /* We encounter instantiations of templates like
6738 template <template <template <class> class> class TT>
6739 class C; */
6740 {
6741 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6742 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6743
6744 if (!coerce_template_template_parms
6745 (parmparm, argparm, complain, in_decl, outer_args))
6746 return 0;
6747 }
6748 /* Fall through. */
6749
6750 case TYPE_DECL:
6751 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6752 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6753 /* Argument is a parameter pack but parameter is not. */
6754 return 0;
6755 break;
6756
6757 case PARM_DECL:
6758 /* The tsubst call is used to handle cases such as
6759
6760 template <int> class C {};
6761 template <class T, template <T> class TT> class D {};
6762 D<int, C> d;
6763
6764 i.e. the parameter list of TT depends on earlier parameters. */
6765 if (!uses_template_parms (TREE_TYPE (arg)))
6766 {
6767 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6768 if (!uses_template_parms (t)
6769 && !same_type_p (t, TREE_TYPE (arg)))
6770 return 0;
6771 }
6772
6773 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6774 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6775 /* Argument is a parameter pack but parameter is not. */
6776 return 0;
6777
6778 break;
6779
6780 default:
6781 gcc_unreachable ();
6782 }
6783
6784 return 1;
6785 }
6786
6787
6788 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6789 template template parameters. Both PARM_PARMS and ARG_PARMS are
6790 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6791 or PARM_DECL.
6792
6793 Consider the example:
6794 template <class T> class A;
6795 template<template <class U> class TT> class B;
6796
6797 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6798 the parameters to A, and OUTER_ARGS contains A. */
6799
6800 static int
6801 coerce_template_template_parms (tree parm_parms,
6802 tree arg_parms,
6803 tsubst_flags_t complain,
6804 tree in_decl,
6805 tree outer_args)
6806 {
6807 int nparms, nargs, i;
6808 tree parm, arg;
6809 int variadic_p = 0;
6810
6811 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6812 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6813
6814 nparms = TREE_VEC_LENGTH (parm_parms);
6815 nargs = TREE_VEC_LENGTH (arg_parms);
6816
6817 /* Determine whether we have a parameter pack at the end of the
6818 template template parameter's template parameter list. */
6819 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6820 {
6821 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6822
6823 if (error_operand_p (parm))
6824 return 0;
6825
6826 switch (TREE_CODE (parm))
6827 {
6828 case TEMPLATE_DECL:
6829 case TYPE_DECL:
6830 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6831 variadic_p = 1;
6832 break;
6833
6834 case PARM_DECL:
6835 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6836 variadic_p = 1;
6837 break;
6838
6839 default:
6840 gcc_unreachable ();
6841 }
6842 }
6843
6844 if (nargs != nparms
6845 && !(variadic_p && nargs >= nparms - 1))
6846 return 0;
6847
6848 /* Check all of the template parameters except the parameter pack at
6849 the end (if any). */
6850 for (i = 0; i < nparms - variadic_p; ++i)
6851 {
6852 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6853 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6854 continue;
6855
6856 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6857 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6858
6859 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6860 outer_args))
6861 return 0;
6862
6863 }
6864
6865 if (variadic_p)
6866 {
6867 /* Check each of the template parameters in the template
6868 argument against the template parameter pack at the end of
6869 the template template parameter. */
6870 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6871 return 0;
6872
6873 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6874
6875 for (; i < nargs; ++i)
6876 {
6877 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6878 continue;
6879
6880 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6881
6882 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6883 outer_args))
6884 return 0;
6885 }
6886 }
6887
6888 return 1;
6889 }
6890
6891 /* Verifies that the deduced template arguments (in TARGS) for the
6892 template template parameters (in TPARMS) represent valid bindings,
6893 by comparing the template parameter list of each template argument
6894 to the template parameter list of its corresponding template
6895 template parameter, in accordance with DR150. This
6896 routine can only be called after all template arguments have been
6897 deduced. It will return TRUE if all of the template template
6898 parameter bindings are okay, FALSE otherwise. */
6899 bool
6900 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6901 {
6902 int i, ntparms = TREE_VEC_LENGTH (tparms);
6903 bool ret = true;
6904
6905 /* We're dealing with template parms in this process. */
6906 ++processing_template_decl;
6907
6908 targs = INNERMOST_TEMPLATE_ARGS (targs);
6909
6910 for (i = 0; i < ntparms; ++i)
6911 {
6912 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6913 tree targ = TREE_VEC_ELT (targs, i);
6914
6915 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6916 {
6917 tree packed_args = NULL_TREE;
6918 int idx, len = 1;
6919
6920 if (ARGUMENT_PACK_P (targ))
6921 {
6922 /* Look inside the argument pack. */
6923 packed_args = ARGUMENT_PACK_ARGS (targ);
6924 len = TREE_VEC_LENGTH (packed_args);
6925 }
6926
6927 for (idx = 0; idx < len; ++idx)
6928 {
6929 tree targ_parms = NULL_TREE;
6930
6931 if (packed_args)
6932 /* Extract the next argument from the argument
6933 pack. */
6934 targ = TREE_VEC_ELT (packed_args, idx);
6935
6936 if (PACK_EXPANSION_P (targ))
6937 /* Look at the pattern of the pack expansion. */
6938 targ = PACK_EXPANSION_PATTERN (targ);
6939
6940 /* Extract the template parameters from the template
6941 argument. */
6942 if (TREE_CODE (targ) == TEMPLATE_DECL)
6943 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6944 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6945 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6946
6947 /* Verify that we can coerce the template template
6948 parameters from the template argument to the template
6949 parameter. This requires an exact match. */
6950 if (targ_parms
6951 && !coerce_template_template_parms
6952 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6953 targ_parms,
6954 tf_none,
6955 tparm,
6956 targs))
6957 {
6958 ret = false;
6959 goto out;
6960 }
6961 }
6962 }
6963 }
6964
6965 out:
6966
6967 --processing_template_decl;
6968 return ret;
6969 }
6970
6971 /* Since type attributes aren't mangled, we need to strip them from
6972 template type arguments. */
6973
6974 static tree
6975 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6976 {
6977 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6978 return arg;
6979 bool removed_attributes = false;
6980 tree canon = strip_typedefs (arg, &removed_attributes);
6981 if (removed_attributes
6982 && (complain & tf_warning))
6983 warning (OPT_Wignored_attributes,
6984 "ignoring attributes on template argument %qT", arg);
6985 return canon;
6986 }
6987
6988 // A template declaration can be substituted for a constrained
6989 // template template parameter only when the argument is more
6990 // constrained than the parameter.
6991 static bool
6992 is_compatible_template_arg (tree parm, tree arg)
6993 {
6994 tree parm_cons = get_constraints (parm);
6995
6996 /* For now, allow constrained template template arguments
6997 and unconstrained template template parameters. */
6998 if (parm_cons == NULL_TREE)
6999 return true;
7000
7001 tree arg_cons = get_constraints (arg);
7002
7003 // If the template parameter is constrained, we need to rewrite its
7004 // constraints in terms of the ARG's template parameters. This ensures
7005 // that all of the template parameter types will have the same depth.
7006 //
7007 // Note that this is only valid when coerce_template_template_parm is
7008 // true for the innermost template parameters of PARM and ARG. In other
7009 // words, because coercion is successful, this conversion will be valid.
7010 if (parm_cons)
7011 {
7012 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7013 parm_cons = tsubst_constraint_info (parm_cons,
7014 INNERMOST_TEMPLATE_ARGS (args),
7015 tf_none, NULL_TREE);
7016 if (parm_cons == error_mark_node)
7017 return false;
7018 }
7019
7020 return subsumes (parm_cons, arg_cons);
7021 }
7022
7023 // Convert a placeholder argument into a binding to the original
7024 // parameter. The original parameter is saved as the TREE_TYPE of
7025 // ARG.
7026 static inline tree
7027 convert_wildcard_argument (tree parm, tree arg)
7028 {
7029 TREE_TYPE (arg) = parm;
7030 return arg;
7031 }
7032
7033 /* Convert the indicated template ARG as necessary to match the
7034 indicated template PARM. Returns the converted ARG, or
7035 error_mark_node if the conversion was unsuccessful. Error and
7036 warning messages are issued under control of COMPLAIN. This
7037 conversion is for the Ith parameter in the parameter list. ARGS is
7038 the full set of template arguments deduced so far. */
7039
7040 static tree
7041 convert_template_argument (tree parm,
7042 tree arg,
7043 tree args,
7044 tsubst_flags_t complain,
7045 int i,
7046 tree in_decl)
7047 {
7048 tree orig_arg;
7049 tree val;
7050 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7051
7052 if (parm == error_mark_node)
7053 return error_mark_node;
7054
7055 /* Trivially convert placeholders. */
7056 if (TREE_CODE (arg) == WILDCARD_DECL)
7057 return convert_wildcard_argument (parm, arg);
7058
7059 if (TREE_CODE (arg) == TREE_LIST
7060 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7061 {
7062 /* The template argument was the name of some
7063 member function. That's usually
7064 invalid, but static members are OK. In any
7065 case, grab the underlying fields/functions
7066 and issue an error later if required. */
7067 orig_arg = TREE_VALUE (arg);
7068 TREE_TYPE (arg) = unknown_type_node;
7069 }
7070
7071 orig_arg = arg;
7072
7073 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7074 requires_type = (TREE_CODE (parm) == TYPE_DECL
7075 || requires_tmpl_type);
7076
7077 /* When determining whether an argument pack expansion is a template,
7078 look at the pattern. */
7079 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7080 arg = PACK_EXPANSION_PATTERN (arg);
7081
7082 /* Deal with an injected-class-name used as a template template arg. */
7083 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7084 {
7085 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7086 if (TREE_CODE (t) == TEMPLATE_DECL)
7087 {
7088 if (cxx_dialect >= cxx11)
7089 /* OK under DR 1004. */;
7090 else if (complain & tf_warning_or_error)
7091 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7092 " used as template template argument", TYPE_NAME (arg));
7093 else if (flag_pedantic_errors)
7094 t = arg;
7095
7096 arg = t;
7097 }
7098 }
7099
7100 is_tmpl_type =
7101 ((TREE_CODE (arg) == TEMPLATE_DECL
7102 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7103 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7104 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7105 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7106
7107 if (is_tmpl_type
7108 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7109 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7110 arg = TYPE_STUB_DECL (arg);
7111
7112 is_type = TYPE_P (arg) || is_tmpl_type;
7113
7114 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7115 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7116 {
7117 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7118 {
7119 if (complain & tf_error)
7120 error ("invalid use of destructor %qE as a type", orig_arg);
7121 return error_mark_node;
7122 }
7123
7124 permerror (input_location,
7125 "to refer to a type member of a template parameter, "
7126 "use %<typename %E%>", orig_arg);
7127
7128 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7129 TREE_OPERAND (arg, 1),
7130 typename_type,
7131 complain);
7132 arg = orig_arg;
7133 is_type = 1;
7134 }
7135 if (is_type != requires_type)
7136 {
7137 if (in_decl)
7138 {
7139 if (complain & tf_error)
7140 {
7141 error ("type/value mismatch at argument %d in template "
7142 "parameter list for %qD",
7143 i + 1, in_decl);
7144 if (is_type)
7145 inform (input_location,
7146 " expected a constant of type %qT, got %qT",
7147 TREE_TYPE (parm),
7148 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7149 else if (requires_tmpl_type)
7150 inform (input_location,
7151 " expected a class template, got %qE", orig_arg);
7152 else
7153 inform (input_location,
7154 " expected a type, got %qE", orig_arg);
7155 }
7156 }
7157 return error_mark_node;
7158 }
7159 if (is_tmpl_type ^ requires_tmpl_type)
7160 {
7161 if (in_decl && (complain & tf_error))
7162 {
7163 error ("type/value mismatch at argument %d in template "
7164 "parameter list for %qD",
7165 i + 1, in_decl);
7166 if (is_tmpl_type)
7167 inform (input_location,
7168 " expected a type, got %qT", DECL_NAME (arg));
7169 else
7170 inform (input_location,
7171 " expected a class template, got %qT", orig_arg);
7172 }
7173 return error_mark_node;
7174 }
7175
7176 if (is_type)
7177 {
7178 if (requires_tmpl_type)
7179 {
7180 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7181 val = orig_arg;
7182 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7183 /* The number of argument required is not known yet.
7184 Just accept it for now. */
7185 val = TREE_TYPE (arg);
7186 else
7187 {
7188 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7189 tree argparm;
7190
7191 /* Strip alias templates that are equivalent to another
7192 template. */
7193 arg = get_underlying_template (arg);
7194 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7195
7196 if (coerce_template_template_parms (parmparm, argparm,
7197 complain, in_decl,
7198 args))
7199 {
7200 val = arg;
7201
7202 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7203 TEMPLATE_DECL. */
7204 if (val != error_mark_node)
7205 {
7206 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7207 val = TREE_TYPE (val);
7208 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7209 val = make_pack_expansion (val);
7210 }
7211 }
7212 else
7213 {
7214 if (in_decl && (complain & tf_error))
7215 {
7216 error ("type/value mismatch at argument %d in "
7217 "template parameter list for %qD",
7218 i + 1, in_decl);
7219 inform (input_location,
7220 " expected a template of type %qD, got %qT",
7221 parm, orig_arg);
7222 }
7223
7224 val = error_mark_node;
7225 }
7226
7227 // Check that the constraints are compatible before allowing the
7228 // substitution.
7229 if (val != error_mark_node)
7230 if (!is_compatible_template_arg (parm, arg))
7231 {
7232 if (in_decl && (complain & tf_error))
7233 {
7234 error ("constraint mismatch at argument %d in "
7235 "template parameter list for %qD",
7236 i + 1, in_decl);
7237 inform (input_location, " expected %qD but got %qD",
7238 parm, arg);
7239 }
7240 val = error_mark_node;
7241 }
7242 }
7243 }
7244 else
7245 val = orig_arg;
7246 /* We only form one instance of each template specialization.
7247 Therefore, if we use a non-canonical variant (i.e., a
7248 typedef), any future messages referring to the type will use
7249 the typedef, which is confusing if those future uses do not
7250 themselves also use the typedef. */
7251 if (TYPE_P (val))
7252 val = canonicalize_type_argument (val, complain);
7253 }
7254 else
7255 {
7256 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7257
7258 if (invalid_nontype_parm_type_p (t, complain))
7259 return error_mark_node;
7260
7261 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7262 {
7263 if (same_type_p (t, TREE_TYPE (orig_arg)))
7264 val = orig_arg;
7265 else
7266 {
7267 /* Not sure if this is reachable, but it doesn't hurt
7268 to be robust. */
7269 error ("type mismatch in nontype parameter pack");
7270 val = error_mark_node;
7271 }
7272 }
7273 else if (!dependent_template_arg_p (orig_arg)
7274 && !uses_template_parms (t))
7275 /* We used to call digest_init here. However, digest_init
7276 will report errors, which we don't want when complain
7277 is zero. More importantly, digest_init will try too
7278 hard to convert things: for example, `0' should not be
7279 converted to pointer type at this point according to
7280 the standard. Accepting this is not merely an
7281 extension, since deciding whether or not these
7282 conversions can occur is part of determining which
7283 function template to call, or whether a given explicit
7284 argument specification is valid. */
7285 val = convert_nontype_argument (t, orig_arg, complain);
7286 else
7287 {
7288 bool removed_attr = false;
7289 val = strip_typedefs_expr (orig_arg, &removed_attr);
7290 }
7291
7292 if (val == NULL_TREE)
7293 val = error_mark_node;
7294 else if (val == error_mark_node && (complain & tf_error))
7295 error ("could not convert template argument %qE to %qT", orig_arg, t);
7296
7297 if (INDIRECT_REF_P (val))
7298 {
7299 /* Reject template arguments that are references to built-in
7300 functions with no library fallbacks. */
7301 const_tree inner = TREE_OPERAND (val, 0);
7302 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7303 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner))) == FUNCTION_TYPE
7304 && TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7305 && 0 < TREE_OPERAND_LENGTH (inner)
7306 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7307 return error_mark_node;
7308 }
7309
7310 if (TREE_CODE (val) == SCOPE_REF)
7311 {
7312 /* Strip typedefs from the SCOPE_REF. */
7313 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7314 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7315 complain);
7316 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7317 QUALIFIED_NAME_IS_TEMPLATE (val));
7318 }
7319 }
7320
7321 return val;
7322 }
7323
7324 /* Coerces the remaining template arguments in INNER_ARGS (from
7325 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7326 Returns the coerced argument pack. PARM_IDX is the position of this
7327 parameter in the template parameter list. ARGS is the original
7328 template argument list. */
7329 static tree
7330 coerce_template_parameter_pack (tree parms,
7331 int parm_idx,
7332 tree args,
7333 tree inner_args,
7334 int arg_idx,
7335 tree new_args,
7336 int* lost,
7337 tree in_decl,
7338 tsubst_flags_t complain)
7339 {
7340 tree parm = TREE_VEC_ELT (parms, parm_idx);
7341 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7342 tree packed_args;
7343 tree argument_pack;
7344 tree packed_parms = NULL_TREE;
7345
7346 if (arg_idx > nargs)
7347 arg_idx = nargs;
7348
7349 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7350 {
7351 /* When the template parameter is a non-type template parameter pack
7352 or template template parameter pack whose type or template
7353 parameters use parameter packs, we know exactly how many arguments
7354 we are looking for. Build a vector of the instantiated decls for
7355 these template parameters in PACKED_PARMS. */
7356 /* We can't use make_pack_expansion here because it would interpret a
7357 _DECL as a use rather than a declaration. */
7358 tree decl = TREE_VALUE (parm);
7359 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7360 SET_PACK_EXPANSION_PATTERN (exp, decl);
7361 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7362 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7363
7364 TREE_VEC_LENGTH (args)--;
7365 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7366 TREE_VEC_LENGTH (args)++;
7367
7368 if (packed_parms == error_mark_node)
7369 return error_mark_node;
7370
7371 /* If we're doing a partial instantiation of a member template,
7372 verify that all of the types used for the non-type
7373 template parameter pack are, in fact, valid for non-type
7374 template parameters. */
7375 if (arg_idx < nargs
7376 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7377 {
7378 int j, len = TREE_VEC_LENGTH (packed_parms);
7379 for (j = 0; j < len; ++j)
7380 {
7381 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7382 if (invalid_nontype_parm_type_p (t, complain))
7383 return error_mark_node;
7384 }
7385 /* We don't know how many args we have yet, just
7386 use the unconverted ones for now. */
7387 return NULL_TREE;
7388 }
7389
7390 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7391 }
7392 /* Check if we have a placeholder pack, which indicates we're
7393 in the context of a introduction list. In that case we want
7394 to match this pack to the single placeholder. */
7395 else if (arg_idx < nargs
7396 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7397 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7398 {
7399 nargs = arg_idx + 1;
7400 packed_args = make_tree_vec (1);
7401 }
7402 else
7403 packed_args = make_tree_vec (nargs - arg_idx);
7404
7405 /* Convert the remaining arguments, which will be a part of the
7406 parameter pack "parm". */
7407 int first_pack_arg = arg_idx;
7408 for (; arg_idx < nargs; ++arg_idx)
7409 {
7410 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7411 tree actual_parm = TREE_VALUE (parm);
7412 int pack_idx = arg_idx - first_pack_arg;
7413
7414 if (packed_parms)
7415 {
7416 /* Once we've packed as many args as we have types, stop. */
7417 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7418 break;
7419 else if (PACK_EXPANSION_P (arg))
7420 /* We don't know how many args we have yet, just
7421 use the unconverted ones for now. */
7422 return NULL_TREE;
7423 else
7424 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7425 }
7426
7427 if (arg == error_mark_node)
7428 {
7429 if (complain & tf_error)
7430 error ("template argument %d is invalid", arg_idx + 1);
7431 }
7432 else
7433 arg = convert_template_argument (actual_parm,
7434 arg, new_args, complain, parm_idx,
7435 in_decl);
7436 if (arg == error_mark_node)
7437 (*lost)++;
7438 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7439 }
7440
7441 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7442 && TREE_VEC_LENGTH (packed_args) > 0)
7443 {
7444 if (complain & tf_error)
7445 error ("wrong number of template arguments (%d, should be %d)",
7446 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7447 return error_mark_node;
7448 }
7449
7450 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7451 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7452 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7453 else
7454 {
7455 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7456 TREE_TYPE (argument_pack)
7457 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7458 TREE_CONSTANT (argument_pack) = 1;
7459 }
7460
7461 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7462 if (CHECKING_P)
7463 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7464 TREE_VEC_LENGTH (packed_args));
7465 return argument_pack;
7466 }
7467
7468 /* Returns the number of pack expansions in the template argument vector
7469 ARGS. */
7470
7471 static int
7472 pack_expansion_args_count (tree args)
7473 {
7474 int i;
7475 int count = 0;
7476 if (args)
7477 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7478 {
7479 tree elt = TREE_VEC_ELT (args, i);
7480 if (elt && PACK_EXPANSION_P (elt))
7481 ++count;
7482 }
7483 return count;
7484 }
7485
7486 /* Convert all template arguments to their appropriate types, and
7487 return a vector containing the innermost resulting template
7488 arguments. If any error occurs, return error_mark_node. Error and
7489 warning messages are issued under control of COMPLAIN.
7490
7491 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7492 for arguments not specified in ARGS. Otherwise, if
7493 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7494 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7495 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7496 ARGS. */
7497
7498 static tree
7499 coerce_template_parms (tree parms,
7500 tree args,
7501 tree in_decl,
7502 tsubst_flags_t complain,
7503 bool require_all_args,
7504 bool use_default_args)
7505 {
7506 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7507 tree orig_inner_args;
7508 tree inner_args;
7509 tree new_args;
7510 tree new_inner_args;
7511 int saved_unevaluated_operand;
7512 int saved_inhibit_evaluation_warnings;
7513
7514 /* When used as a boolean value, indicates whether this is a
7515 variadic template parameter list. Since it's an int, we can also
7516 subtract it from nparms to get the number of non-variadic
7517 parameters. */
7518 int variadic_p = 0;
7519 int variadic_args_p = 0;
7520 int post_variadic_parms = 0;
7521
7522 /* Likewise for parameters with default arguments. */
7523 int default_p = 0;
7524
7525 if (args == error_mark_node)
7526 return error_mark_node;
7527
7528 nparms = TREE_VEC_LENGTH (parms);
7529
7530 /* Determine if there are any parameter packs or default arguments. */
7531 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7532 {
7533 tree parm = TREE_VEC_ELT (parms, parm_idx);
7534 if (variadic_p)
7535 ++post_variadic_parms;
7536 if (template_parameter_pack_p (TREE_VALUE (parm)))
7537 ++variadic_p;
7538 if (TREE_PURPOSE (parm))
7539 ++default_p;
7540 }
7541
7542 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7543 /* If there are no parameters that follow a parameter pack, we need to
7544 expand any argument packs so that we can deduce a parameter pack from
7545 some non-packed args followed by an argument pack, as in variadic85.C.
7546 If there are such parameters, we need to leave argument packs intact
7547 so the arguments are assigned properly. This can happen when dealing
7548 with a nested class inside a partial specialization of a class
7549 template, as in variadic92.C, or when deducing a template parameter pack
7550 from a sub-declarator, as in variadic114.C. */
7551 if (!post_variadic_parms)
7552 inner_args = expand_template_argument_pack (inner_args);
7553
7554 /* Count any pack expansion args. */
7555 variadic_args_p = pack_expansion_args_count (inner_args);
7556
7557 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7558 if ((nargs > nparms && !variadic_p)
7559 || (nargs < nparms - variadic_p
7560 && require_all_args
7561 && !variadic_args_p
7562 && (!use_default_args
7563 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7564 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7565 {
7566 if (complain & tf_error)
7567 {
7568 if (variadic_p || default_p)
7569 {
7570 nparms -= variadic_p + default_p;
7571 error ("wrong number of template arguments "
7572 "(%d, should be at least %d)", nargs, nparms);
7573 }
7574 else
7575 error ("wrong number of template arguments "
7576 "(%d, should be %d)", nargs, nparms);
7577
7578 if (in_decl)
7579 inform (DECL_SOURCE_LOCATION (in_decl),
7580 "provided for %qD", in_decl);
7581 }
7582
7583 return error_mark_node;
7584 }
7585 /* We can't pass a pack expansion to a non-pack parameter of an alias
7586 template (DR 1430). */
7587 else if (in_decl
7588 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7589 || concept_template_p (in_decl))
7590 && variadic_args_p
7591 && nargs - variadic_args_p < nparms - variadic_p)
7592 {
7593 if (complain & tf_error)
7594 {
7595 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7596 {
7597 tree arg = TREE_VEC_ELT (inner_args, i);
7598 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7599
7600 if (PACK_EXPANSION_P (arg)
7601 && !template_parameter_pack_p (parm))
7602 {
7603 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7604 error_at (location_of (arg),
7605 "pack expansion argument for non-pack parameter "
7606 "%qD of alias template %qD", parm, in_decl);
7607 else
7608 error_at (location_of (arg),
7609 "pack expansion argument for non-pack parameter "
7610 "%qD of concept %qD", parm, in_decl);
7611 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7612 goto found;
7613 }
7614 }
7615 gcc_unreachable ();
7616 found:;
7617 }
7618 return error_mark_node;
7619 }
7620
7621 /* We need to evaluate the template arguments, even though this
7622 template-id may be nested within a "sizeof". */
7623 saved_unevaluated_operand = cp_unevaluated_operand;
7624 cp_unevaluated_operand = 0;
7625 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7626 c_inhibit_evaluation_warnings = 0;
7627 new_inner_args = make_tree_vec (nparms);
7628 new_args = add_outermost_template_args (args, new_inner_args);
7629 int pack_adjust = 0;
7630 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7631 {
7632 tree arg;
7633 tree parm;
7634
7635 /* Get the Ith template parameter. */
7636 parm = TREE_VEC_ELT (parms, parm_idx);
7637
7638 if (parm == error_mark_node)
7639 {
7640 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7641 continue;
7642 }
7643
7644 /* Calculate the next argument. */
7645 if (arg_idx < nargs)
7646 arg = TREE_VEC_ELT (inner_args, arg_idx);
7647 else
7648 arg = NULL_TREE;
7649
7650 if (template_parameter_pack_p (TREE_VALUE (parm))
7651 && !(arg && ARGUMENT_PACK_P (arg)))
7652 {
7653 /* Some arguments will be placed in the
7654 template parameter pack PARM. */
7655 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7656 inner_args, arg_idx,
7657 new_args, &lost,
7658 in_decl, complain);
7659
7660 if (arg == NULL_TREE)
7661 {
7662 /* We don't know how many args we have yet, just use the
7663 unconverted (and still packed) ones for now. */
7664 new_inner_args = orig_inner_args;
7665 arg_idx = nargs;
7666 break;
7667 }
7668
7669 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7670
7671 /* Store this argument. */
7672 if (arg == error_mark_node)
7673 {
7674 lost++;
7675 /* We are done with all of the arguments. */
7676 arg_idx = nargs;
7677 }
7678 else
7679 {
7680 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7681 arg_idx += pack_adjust;
7682 }
7683
7684 continue;
7685 }
7686 else if (arg)
7687 {
7688 if (PACK_EXPANSION_P (arg))
7689 {
7690 /* "If every valid specialization of a variadic template
7691 requires an empty template parameter pack, the template is
7692 ill-formed, no diagnostic required." So check that the
7693 pattern works with this parameter. */
7694 tree pattern = PACK_EXPANSION_PATTERN (arg);
7695 tree conv = convert_template_argument (TREE_VALUE (parm),
7696 pattern, new_args,
7697 complain, parm_idx,
7698 in_decl);
7699 if (conv == error_mark_node)
7700 {
7701 inform (input_location, "so any instantiation with a "
7702 "non-empty parameter pack would be ill-formed");
7703 ++lost;
7704 }
7705 else if (TYPE_P (conv) && !TYPE_P (pattern))
7706 /* Recover from missing typename. */
7707 TREE_VEC_ELT (inner_args, arg_idx)
7708 = make_pack_expansion (conv);
7709
7710 /* We don't know how many args we have yet, just
7711 use the unconverted ones for now. */
7712 new_inner_args = inner_args;
7713 arg_idx = nargs;
7714 break;
7715 }
7716 }
7717 else if (require_all_args)
7718 {
7719 /* There must be a default arg in this case. */
7720 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7721 complain, in_decl);
7722 /* The position of the first default template argument,
7723 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7724 Record that. */
7725 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7726 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7727 arg_idx - pack_adjust);
7728 }
7729 else
7730 break;
7731
7732 if (arg == error_mark_node)
7733 {
7734 if (complain & tf_error)
7735 error ("template argument %d is invalid", arg_idx + 1);
7736 }
7737 else if (!arg)
7738 /* This only occurs if there was an error in the template
7739 parameter list itself (which we would already have
7740 reported) that we are trying to recover from, e.g., a class
7741 template with a parameter list such as
7742 template<typename..., typename>. */
7743 ++lost;
7744 else
7745 arg = convert_template_argument (TREE_VALUE (parm),
7746 arg, new_args, complain,
7747 parm_idx, in_decl);
7748
7749 if (arg == error_mark_node)
7750 lost++;
7751 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7752 }
7753 cp_unevaluated_operand = saved_unevaluated_operand;
7754 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7755
7756 if (variadic_p && arg_idx < nargs)
7757 {
7758 if (complain & tf_error)
7759 {
7760 error ("wrong number of template arguments "
7761 "(%d, should be %d)", nargs, arg_idx);
7762 if (in_decl)
7763 error ("provided for %q+D", in_decl);
7764 }
7765 return error_mark_node;
7766 }
7767
7768 if (lost)
7769 return error_mark_node;
7770
7771 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7772 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7773 TREE_VEC_LENGTH (new_inner_args));
7774
7775 return new_inner_args;
7776 }
7777
7778 /* Convert all template arguments to their appropriate types, and
7779 return a vector containing the innermost resulting template
7780 arguments. If any error occurs, return error_mark_node. Error and
7781 warning messages are not issued.
7782
7783 Note that no function argument deduction is performed, and default
7784 arguments are used to fill in unspecified arguments. */
7785 tree
7786 coerce_template_parms (tree parms, tree args, tree in_decl)
7787 {
7788 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7789 }
7790
7791 /* Convert all template arguments to their appropriate type, and
7792 instantiate default arguments as needed. This returns a vector
7793 containing the innermost resulting template arguments, or
7794 error_mark_node if unsuccessful. */
7795 tree
7796 coerce_template_parms (tree parms, tree args, tree in_decl,
7797 tsubst_flags_t complain)
7798 {
7799 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7800 }
7801
7802 /* Like coerce_template_parms. If PARMS represents all template
7803 parameters levels, this function returns a vector of vectors
7804 representing all the resulting argument levels. Note that in this
7805 case, only the innermost arguments are coerced because the
7806 outermost ones are supposed to have been coerced already.
7807
7808 Otherwise, if PARMS represents only (the innermost) vector of
7809 parameters, this function returns a vector containing just the
7810 innermost resulting arguments. */
7811
7812 static tree
7813 coerce_innermost_template_parms (tree parms,
7814 tree args,
7815 tree in_decl,
7816 tsubst_flags_t complain,
7817 bool require_all_args,
7818 bool use_default_args)
7819 {
7820 int parms_depth = TMPL_PARMS_DEPTH (parms);
7821 int args_depth = TMPL_ARGS_DEPTH (args);
7822 tree coerced_args;
7823
7824 if (parms_depth > 1)
7825 {
7826 coerced_args = make_tree_vec (parms_depth);
7827 tree level;
7828 int cur_depth;
7829
7830 for (level = parms, cur_depth = parms_depth;
7831 parms_depth > 0 && level != NULL_TREE;
7832 level = TREE_CHAIN (level), --cur_depth)
7833 {
7834 tree l;
7835 if (cur_depth == args_depth)
7836 l = coerce_template_parms (TREE_VALUE (level),
7837 args, in_decl, complain,
7838 require_all_args,
7839 use_default_args);
7840 else
7841 l = TMPL_ARGS_LEVEL (args, cur_depth);
7842
7843 if (l == error_mark_node)
7844 return error_mark_node;
7845
7846 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7847 }
7848 }
7849 else
7850 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7851 args, in_decl, complain,
7852 require_all_args,
7853 use_default_args);
7854 return coerced_args;
7855 }
7856
7857 /* Returns 1 if template args OT and NT are equivalent. */
7858
7859 int
7860 template_args_equal (tree ot, tree nt)
7861 {
7862 if (nt == ot)
7863 return 1;
7864 if (nt == NULL_TREE || ot == NULL_TREE)
7865 return false;
7866
7867 if (TREE_CODE (nt) == TREE_VEC)
7868 /* For member templates */
7869 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7870 else if (PACK_EXPANSION_P (ot))
7871 return (PACK_EXPANSION_P (nt)
7872 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7873 PACK_EXPANSION_PATTERN (nt))
7874 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7875 PACK_EXPANSION_EXTRA_ARGS (nt)));
7876 else if (ARGUMENT_PACK_P (ot))
7877 {
7878 int i, len;
7879 tree opack, npack;
7880
7881 if (!ARGUMENT_PACK_P (nt))
7882 return 0;
7883
7884 opack = ARGUMENT_PACK_ARGS (ot);
7885 npack = ARGUMENT_PACK_ARGS (nt);
7886 len = TREE_VEC_LENGTH (opack);
7887 if (TREE_VEC_LENGTH (npack) != len)
7888 return 0;
7889 for (i = 0; i < len; ++i)
7890 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7891 TREE_VEC_ELT (npack, i)))
7892 return 0;
7893 return 1;
7894 }
7895 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7896 gcc_unreachable ();
7897 else if (TYPE_P (nt))
7898 {
7899 if (!TYPE_P (ot))
7900 return false;
7901 /* Don't treat an alias template specialization with dependent
7902 arguments as equivalent to its underlying type when used as a
7903 template argument; we need them to be distinct so that we
7904 substitute into the specialization arguments at instantiation
7905 time. And aliases can't be equivalent without being ==, so
7906 we don't need to look any deeper. */
7907 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7908 return false;
7909 else
7910 return same_type_p (ot, nt);
7911 }
7912 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7913 return 0;
7914 else
7915 {
7916 /* Try to treat a template non-type argument that has been converted
7917 to the parameter type as equivalent to one that hasn't yet. */
7918 for (enum tree_code code1 = TREE_CODE (ot);
7919 CONVERT_EXPR_CODE_P (code1)
7920 || code1 == NON_LVALUE_EXPR;
7921 code1 = TREE_CODE (ot))
7922 ot = TREE_OPERAND (ot, 0);
7923 for (enum tree_code code2 = TREE_CODE (nt);
7924 CONVERT_EXPR_CODE_P (code2)
7925 || code2 == NON_LVALUE_EXPR;
7926 code2 = TREE_CODE (nt))
7927 nt = TREE_OPERAND (nt, 0);
7928
7929 return cp_tree_equal (ot, nt);
7930 }
7931 }
7932
7933 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7934 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7935 NEWARG_PTR with the offending arguments if they are non-NULL. */
7936
7937 int
7938 comp_template_args (tree oldargs, tree newargs,
7939 tree *oldarg_ptr, tree *newarg_ptr)
7940 {
7941 int i;
7942
7943 if (oldargs == newargs)
7944 return 1;
7945
7946 if (!oldargs || !newargs)
7947 return 0;
7948
7949 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7950 return 0;
7951
7952 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7953 {
7954 tree nt = TREE_VEC_ELT (newargs, i);
7955 tree ot = TREE_VEC_ELT (oldargs, i);
7956
7957 if (! template_args_equal (ot, nt))
7958 {
7959 if (oldarg_ptr != NULL)
7960 *oldarg_ptr = ot;
7961 if (newarg_ptr != NULL)
7962 *newarg_ptr = nt;
7963 return 0;
7964 }
7965 }
7966 return 1;
7967 }
7968
7969 static void
7970 add_pending_template (tree d)
7971 {
7972 tree ti = (TYPE_P (d)
7973 ? CLASSTYPE_TEMPLATE_INFO (d)
7974 : DECL_TEMPLATE_INFO (d));
7975 struct pending_template *pt;
7976 int level;
7977
7978 if (TI_PENDING_TEMPLATE_FLAG (ti))
7979 return;
7980
7981 /* We are called both from instantiate_decl, where we've already had a
7982 tinst_level pushed, and instantiate_template, where we haven't.
7983 Compensate. */
7984 level = !current_tinst_level || current_tinst_level->decl != d;
7985
7986 if (level)
7987 push_tinst_level (d);
7988
7989 pt = ggc_alloc<pending_template> ();
7990 pt->next = NULL;
7991 pt->tinst = current_tinst_level;
7992 if (last_pending_template)
7993 last_pending_template->next = pt;
7994 else
7995 pending_templates = pt;
7996
7997 last_pending_template = pt;
7998
7999 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8000
8001 if (level)
8002 pop_tinst_level ();
8003 }
8004
8005
8006 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8007 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8008 documentation for TEMPLATE_ID_EXPR. */
8009
8010 tree
8011 lookup_template_function (tree fns, tree arglist)
8012 {
8013 tree type;
8014
8015 if (fns == error_mark_node || arglist == error_mark_node)
8016 return error_mark_node;
8017
8018 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8019
8020 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8021 {
8022 error ("%q#D is not a function template", fns);
8023 return error_mark_node;
8024 }
8025
8026 if (BASELINK_P (fns))
8027 {
8028 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8029 unknown_type_node,
8030 BASELINK_FUNCTIONS (fns),
8031 arglist);
8032 return fns;
8033 }
8034
8035 type = TREE_TYPE (fns);
8036 if (TREE_CODE (fns) == OVERLOAD || !type)
8037 type = unknown_type_node;
8038
8039 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8040 }
8041
8042 /* Within the scope of a template class S<T>, the name S gets bound
8043 (in build_self_reference) to a TYPE_DECL for the class, not a
8044 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8045 or one of its enclosing classes, and that type is a template,
8046 return the associated TEMPLATE_DECL. Otherwise, the original
8047 DECL is returned.
8048
8049 Also handle the case when DECL is a TREE_LIST of ambiguous
8050 injected-class-names from different bases. */
8051
8052 tree
8053 maybe_get_template_decl_from_type_decl (tree decl)
8054 {
8055 if (decl == NULL_TREE)
8056 return decl;
8057
8058 /* DR 176: A lookup that finds an injected-class-name (10.2
8059 [class.member.lookup]) can result in an ambiguity in certain cases
8060 (for example, if it is found in more than one base class). If all of
8061 the injected-class-names that are found refer to specializations of
8062 the same class template, and if the name is followed by a
8063 template-argument-list, the reference refers to the class template
8064 itself and not a specialization thereof, and is not ambiguous. */
8065 if (TREE_CODE (decl) == TREE_LIST)
8066 {
8067 tree t, tmpl = NULL_TREE;
8068 for (t = decl; t; t = TREE_CHAIN (t))
8069 {
8070 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8071 if (!tmpl)
8072 tmpl = elt;
8073 else if (tmpl != elt)
8074 break;
8075 }
8076 if (tmpl && t == NULL_TREE)
8077 return tmpl;
8078 else
8079 return decl;
8080 }
8081
8082 return (decl != NULL_TREE
8083 && DECL_SELF_REFERENCE_P (decl)
8084 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8085 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8086 }
8087
8088 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8089 parameters, find the desired type.
8090
8091 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8092
8093 IN_DECL, if non-NULL, is the template declaration we are trying to
8094 instantiate.
8095
8096 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8097 the class we are looking up.
8098
8099 Issue error and warning messages under control of COMPLAIN.
8100
8101 If the template class is really a local class in a template
8102 function, then the FUNCTION_CONTEXT is the function in which it is
8103 being instantiated.
8104
8105 ??? Note that this function is currently called *twice* for each
8106 template-id: the first time from the parser, while creating the
8107 incomplete type (finish_template_type), and the second type during the
8108 real instantiation (instantiate_template_class). This is surely something
8109 that we want to avoid. It also causes some problems with argument
8110 coercion (see convert_nontype_argument for more information on this). */
8111
8112 static tree
8113 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8114 int entering_scope, tsubst_flags_t complain)
8115 {
8116 tree templ = NULL_TREE, parmlist;
8117 tree t;
8118 spec_entry **slot;
8119 spec_entry *entry;
8120 spec_entry elt;
8121 hashval_t hash;
8122
8123 if (identifier_p (d1))
8124 {
8125 tree value = innermost_non_namespace_value (d1);
8126 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8127 templ = value;
8128 else
8129 {
8130 if (context)
8131 push_decl_namespace (context);
8132 templ = lookup_name (d1);
8133 templ = maybe_get_template_decl_from_type_decl (templ);
8134 if (context)
8135 pop_decl_namespace ();
8136 }
8137 if (templ)
8138 context = DECL_CONTEXT (templ);
8139 }
8140 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8141 {
8142 tree type = TREE_TYPE (d1);
8143
8144 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8145 an implicit typename for the second A. Deal with it. */
8146 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8147 type = TREE_TYPE (type);
8148
8149 if (CLASSTYPE_TEMPLATE_INFO (type))
8150 {
8151 templ = CLASSTYPE_TI_TEMPLATE (type);
8152 d1 = DECL_NAME (templ);
8153 }
8154 }
8155 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8156 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8157 {
8158 templ = TYPE_TI_TEMPLATE (d1);
8159 d1 = DECL_NAME (templ);
8160 }
8161 else if (DECL_TYPE_TEMPLATE_P (d1))
8162 {
8163 templ = d1;
8164 d1 = DECL_NAME (templ);
8165 context = DECL_CONTEXT (templ);
8166 }
8167 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8168 {
8169 templ = d1;
8170 d1 = DECL_NAME (templ);
8171 }
8172
8173 /* Issue an error message if we didn't find a template. */
8174 if (! templ)
8175 {
8176 if (complain & tf_error)
8177 error ("%qT is not a template", d1);
8178 return error_mark_node;
8179 }
8180
8181 if (TREE_CODE (templ) != TEMPLATE_DECL
8182 /* Make sure it's a user visible template, if it was named by
8183 the user. */
8184 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8185 && !PRIMARY_TEMPLATE_P (templ)))
8186 {
8187 if (complain & tf_error)
8188 {
8189 error ("non-template type %qT used as a template", d1);
8190 if (in_decl)
8191 error ("for template declaration %q+D", in_decl);
8192 }
8193 return error_mark_node;
8194 }
8195
8196 complain &= ~tf_user;
8197
8198 /* An alias that just changes the name of a template is equivalent to the
8199 other template, so if any of the arguments are pack expansions, strip
8200 the alias to avoid problems with a pack expansion passed to a non-pack
8201 alias template parameter (DR 1430). */
8202 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8203 templ = get_underlying_template (templ);
8204
8205 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8206 {
8207 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8208 template arguments */
8209
8210 tree parm;
8211 tree arglist2;
8212 tree outer;
8213
8214 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8215
8216 /* Consider an example where a template template parameter declared as
8217
8218 template <class T, class U = std::allocator<T> > class TT
8219
8220 The template parameter level of T and U are one level larger than
8221 of TT. To proper process the default argument of U, say when an
8222 instantiation `TT<int>' is seen, we need to build the full
8223 arguments containing {int} as the innermost level. Outer levels,
8224 available when not appearing as default template argument, can be
8225 obtained from the arguments of the enclosing template.
8226
8227 Suppose that TT is later substituted with std::vector. The above
8228 instantiation is `TT<int, std::allocator<T> >' with TT at
8229 level 1, and T at level 2, while the template arguments at level 1
8230 becomes {std::vector} and the inner level 2 is {int}. */
8231
8232 outer = DECL_CONTEXT (templ);
8233 if (outer)
8234 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8235 else if (current_template_parms)
8236 {
8237 /* This is an argument of the current template, so we haven't set
8238 DECL_CONTEXT yet. */
8239 tree relevant_template_parms;
8240
8241 /* Parameter levels that are greater than the level of the given
8242 template template parm are irrelevant. */
8243 relevant_template_parms = current_template_parms;
8244 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8245 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8246 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8247
8248 outer = template_parms_to_args (relevant_template_parms);
8249 }
8250
8251 if (outer)
8252 arglist = add_to_template_args (outer, arglist);
8253
8254 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8255 complain,
8256 /*require_all_args=*/true,
8257 /*use_default_args=*/true);
8258 if (arglist2 == error_mark_node
8259 || (!uses_template_parms (arglist2)
8260 && check_instantiated_args (templ, arglist2, complain)))
8261 return error_mark_node;
8262
8263 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8264 return parm;
8265 }
8266 else
8267 {
8268 tree template_type = TREE_TYPE (templ);
8269 tree gen_tmpl;
8270 tree type_decl;
8271 tree found = NULL_TREE;
8272 int arg_depth;
8273 int parm_depth;
8274 int is_dependent_type;
8275 int use_partial_inst_tmpl = false;
8276
8277 if (template_type == error_mark_node)
8278 /* An error occurred while building the template TEMPL, and a
8279 diagnostic has most certainly been emitted for that
8280 already. Let's propagate that error. */
8281 return error_mark_node;
8282
8283 gen_tmpl = most_general_template (templ);
8284 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8285 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8286 arg_depth = TMPL_ARGS_DEPTH (arglist);
8287
8288 if (arg_depth == 1 && parm_depth > 1)
8289 {
8290 /* We've been given an incomplete set of template arguments.
8291 For example, given:
8292
8293 template <class T> struct S1 {
8294 template <class U> struct S2 {};
8295 template <class U> struct S2<U*> {};
8296 };
8297
8298 we will be called with an ARGLIST of `U*', but the
8299 TEMPLATE will be `template <class T> template
8300 <class U> struct S1<T>::S2'. We must fill in the missing
8301 arguments. */
8302 arglist
8303 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8304 arglist);
8305 arg_depth = TMPL_ARGS_DEPTH (arglist);
8306 }
8307
8308 /* Now we should have enough arguments. */
8309 gcc_assert (parm_depth == arg_depth);
8310
8311 /* From here on, we're only interested in the most general
8312 template. */
8313
8314 /* Calculate the BOUND_ARGS. These will be the args that are
8315 actually tsubst'd into the definition to create the
8316 instantiation. */
8317 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8318 complain,
8319 /*require_all_args=*/true,
8320 /*use_default_args=*/true);
8321
8322 if (arglist == error_mark_node)
8323 /* We were unable to bind the arguments. */
8324 return error_mark_node;
8325
8326 /* In the scope of a template class, explicit references to the
8327 template class refer to the type of the template, not any
8328 instantiation of it. For example, in:
8329
8330 template <class T> class C { void f(C<T>); }
8331
8332 the `C<T>' is just the same as `C'. Outside of the
8333 class, however, such a reference is an instantiation. */
8334 if ((entering_scope
8335 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8336 || currently_open_class (template_type))
8337 /* comp_template_args is expensive, check it last. */
8338 && comp_template_args (TYPE_TI_ARGS (template_type),
8339 arglist))
8340 return template_type;
8341
8342 /* If we already have this specialization, return it. */
8343 elt.tmpl = gen_tmpl;
8344 elt.args = arglist;
8345 elt.spec = NULL_TREE;
8346 hash = spec_hasher::hash (&elt);
8347 entry = type_specializations->find_with_hash (&elt, hash);
8348
8349 if (entry)
8350 return entry->spec;
8351
8352 /* If the the template's constraints are not satisfied,
8353 then we cannot form a valid type.
8354
8355 Note that the check is deferred until after the hash
8356 lookup. This prevents redundant checks on previously
8357 instantiated specializations. */
8358 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8359 {
8360 if (complain & tf_error)
8361 {
8362 error ("template constraint failure");
8363 diagnose_constraints (input_location, gen_tmpl, arglist);
8364 }
8365 return error_mark_node;
8366 }
8367
8368 is_dependent_type = uses_template_parms (arglist);
8369
8370 /* If the deduced arguments are invalid, then the binding
8371 failed. */
8372 if (!is_dependent_type
8373 && check_instantiated_args (gen_tmpl,
8374 INNERMOST_TEMPLATE_ARGS (arglist),
8375 complain))
8376 return error_mark_node;
8377
8378 if (!is_dependent_type
8379 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8380 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8381 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8382 {
8383 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8384 DECL_NAME (gen_tmpl),
8385 /*tag_scope=*/ts_global);
8386 return found;
8387 }
8388
8389 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8390 complain, in_decl);
8391 if (context == error_mark_node)
8392 return error_mark_node;
8393
8394 if (!context)
8395 context = global_namespace;
8396
8397 /* Create the type. */
8398 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8399 {
8400 /* The user referred to a specialization of an alias
8401 template represented by GEN_TMPL.
8402
8403 [temp.alias]/2 says:
8404
8405 When a template-id refers to the specialization of an
8406 alias template, it is equivalent to the associated
8407 type obtained by substitution of its
8408 template-arguments for the template-parameters in the
8409 type-id of the alias template. */
8410
8411 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8412 /* Note that the call above (by indirectly calling
8413 register_specialization in tsubst_decl) registers the
8414 TYPE_DECL representing the specialization of the alias
8415 template. So next time someone substitutes ARGLIST for
8416 the template parms into the alias template (GEN_TMPL),
8417 she'll get that TYPE_DECL back. */
8418
8419 if (t == error_mark_node)
8420 return t;
8421 }
8422 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8423 {
8424 if (!is_dependent_type)
8425 {
8426 set_current_access_from_decl (TYPE_NAME (template_type));
8427 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8428 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8429 arglist, complain, in_decl),
8430 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8431 arglist, complain, in_decl),
8432 SCOPED_ENUM_P (template_type), NULL);
8433
8434 if (t == error_mark_node)
8435 return t;
8436 }
8437 else
8438 {
8439 /* We don't want to call start_enum for this type, since
8440 the values for the enumeration constants may involve
8441 template parameters. And, no one should be interested
8442 in the enumeration constants for such a type. */
8443 t = cxx_make_type (ENUMERAL_TYPE);
8444 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8445 }
8446 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8447 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8448 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8449 }
8450 else if (CLASS_TYPE_P (template_type))
8451 {
8452 t = make_class_type (TREE_CODE (template_type));
8453 CLASSTYPE_DECLARED_CLASS (t)
8454 = CLASSTYPE_DECLARED_CLASS (template_type);
8455 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8456 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
8457
8458 /* A local class. Make sure the decl gets registered properly. */
8459 if (context == current_function_decl)
8460 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8461
8462 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8463 /* This instantiation is another name for the primary
8464 template type. Set the TYPE_CANONICAL field
8465 appropriately. */
8466 TYPE_CANONICAL (t) = template_type;
8467 else if (any_template_arguments_need_structural_equality_p (arglist))
8468 /* Some of the template arguments require structural
8469 equality testing, so this template class requires
8470 structural equality testing. */
8471 SET_TYPE_STRUCTURAL_EQUALITY (t);
8472 }
8473 else
8474 gcc_unreachable ();
8475
8476 /* If we called start_enum or pushtag above, this information
8477 will already be set up. */
8478 if (!TYPE_NAME (t))
8479 {
8480 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8481
8482 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8483 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8484 DECL_SOURCE_LOCATION (type_decl)
8485 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8486 }
8487 else
8488 type_decl = TYPE_NAME (t);
8489
8490 if (CLASS_TYPE_P (template_type))
8491 {
8492 TREE_PRIVATE (type_decl)
8493 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8494 TREE_PROTECTED (type_decl)
8495 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8496 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8497 {
8498 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8499 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8500 }
8501 }
8502
8503 if (OVERLOAD_TYPE_P (t)
8504 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8505 {
8506 static const char *tags[] = {"abi_tag", "may_alias"};
8507
8508 for (unsigned ix = 0; ix != 2; ix++)
8509 {
8510 tree attributes
8511 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8512
8513 if (attributes)
8514 TYPE_ATTRIBUTES (t)
8515 = tree_cons (TREE_PURPOSE (attributes),
8516 TREE_VALUE (attributes),
8517 TYPE_ATTRIBUTES (t));
8518 }
8519 }
8520
8521 /* Let's consider the explicit specialization of a member
8522 of a class template specialization that is implicitly instantiated,
8523 e.g.:
8524 template<class T>
8525 struct S
8526 {
8527 template<class U> struct M {}; //#0
8528 };
8529
8530 template<>
8531 template<>
8532 struct S<int>::M<char> //#1
8533 {
8534 int i;
8535 };
8536 [temp.expl.spec]/4 says this is valid.
8537
8538 In this case, when we write:
8539 S<int>::M<char> m;
8540
8541 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8542 the one of #0.
8543
8544 When we encounter #1, we want to store the partial instantiation
8545 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8546
8547 For all cases other than this "explicit specialization of member of a
8548 class template", we just want to store the most general template into
8549 the CLASSTYPE_TI_TEMPLATE of M.
8550
8551 This case of "explicit specialization of member of a class template"
8552 only happens when:
8553 1/ the enclosing class is an instantiation of, and therefore not
8554 the same as, the context of the most general template, and
8555 2/ we aren't looking at the partial instantiation itself, i.e.
8556 the innermost arguments are not the same as the innermost parms of
8557 the most general template.
8558
8559 So it's only when 1/ and 2/ happens that we want to use the partial
8560 instantiation of the member template in lieu of its most general
8561 template. */
8562
8563 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8564 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8565 /* the enclosing class must be an instantiation... */
8566 && CLASS_TYPE_P (context)
8567 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8568 {
8569 tree partial_inst_args;
8570 TREE_VEC_LENGTH (arglist)--;
8571 ++processing_template_decl;
8572 partial_inst_args =
8573 tsubst (INNERMOST_TEMPLATE_ARGS
8574 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8575 arglist, complain, NULL_TREE);
8576 --processing_template_decl;
8577 TREE_VEC_LENGTH (arglist)++;
8578 if (partial_inst_args == error_mark_node)
8579 return error_mark_node;
8580 use_partial_inst_tmpl =
8581 /*...and we must not be looking at the partial instantiation
8582 itself. */
8583 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8584 partial_inst_args);
8585 }
8586
8587 if (!use_partial_inst_tmpl)
8588 /* This case is easy; there are no member templates involved. */
8589 found = gen_tmpl;
8590 else
8591 {
8592 /* This is a full instantiation of a member template. Find
8593 the partial instantiation of which this is an instance. */
8594
8595 /* Temporarily reduce by one the number of levels in the ARGLIST
8596 so as to avoid comparing the last set of arguments. */
8597 TREE_VEC_LENGTH (arglist)--;
8598 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8599 TREE_VEC_LENGTH (arglist)++;
8600 /* FOUND is either a proper class type, or an alias
8601 template specialization. In the later case, it's a
8602 TYPE_DECL, resulting from the substituting of arguments
8603 for parameters in the TYPE_DECL of the alias template
8604 done earlier. So be careful while getting the template
8605 of FOUND. */
8606 found = TREE_CODE (found) == TEMPLATE_DECL
8607 ? found
8608 : TREE_CODE (found) == TYPE_DECL
8609 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8610 : CLASSTYPE_TI_TEMPLATE (found);
8611 }
8612
8613 // Build template info for the new specialization.
8614 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8615
8616 elt.spec = t;
8617 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8618 entry = ggc_alloc<spec_entry> ();
8619 *entry = elt;
8620 *slot = entry;
8621
8622 /* Note this use of the partial instantiation so we can check it
8623 later in maybe_process_partial_specialization. */
8624 DECL_TEMPLATE_INSTANTIATIONS (found)
8625 = tree_cons (arglist, t,
8626 DECL_TEMPLATE_INSTANTIATIONS (found));
8627
8628 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8629 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8630 /* Now that the type has been registered on the instantiations
8631 list, we set up the enumerators. Because the enumeration
8632 constants may involve the enumeration type itself, we make
8633 sure to register the type first, and then create the
8634 constants. That way, doing tsubst_expr for the enumeration
8635 constants won't result in recursive calls here; we'll find
8636 the instantiation and exit above. */
8637 tsubst_enum (template_type, t, arglist);
8638
8639 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8640 /* If the type makes use of template parameters, the
8641 code that generates debugging information will crash. */
8642 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8643
8644 /* Possibly limit visibility based on template args. */
8645 TREE_PUBLIC (type_decl) = 1;
8646 determine_visibility (type_decl);
8647
8648 inherit_targ_abi_tags (t);
8649
8650 return t;
8651 }
8652 }
8653
8654 /* Wrapper for lookup_template_class_1. */
8655
8656 tree
8657 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8658 int entering_scope, tsubst_flags_t complain)
8659 {
8660 tree ret;
8661 timevar_push (TV_TEMPLATE_INST);
8662 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8663 entering_scope, complain);
8664 timevar_pop (TV_TEMPLATE_INST);
8665 return ret;
8666 }
8667
8668 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8669
8670 tree
8671 lookup_template_variable (tree templ, tree arglist)
8672 {
8673 /* The type of the expression is NULL_TREE since the template-id could refer
8674 to an explicit or partial specialization. */
8675 tree type = NULL_TREE;
8676 if (flag_concepts && variable_concept_p (templ))
8677 /* Except that concepts are always bool. */
8678 type = boolean_type_node;
8679 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8680 }
8681
8682 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8683
8684 tree
8685 finish_template_variable (tree var, tsubst_flags_t complain)
8686 {
8687 tree templ = TREE_OPERAND (var, 0);
8688 tree arglist = TREE_OPERAND (var, 1);
8689
8690 /* We never want to return a VAR_DECL for a variable concept, since they
8691 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8692 bool concept_p = flag_concepts && variable_concept_p (templ);
8693 if (concept_p && processing_template_decl)
8694 return var;
8695
8696 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8697 arglist = add_outermost_template_args (tmpl_args, arglist);
8698
8699 tree parms = DECL_TEMPLATE_PARMS (templ);
8700 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8701 /*req_all*/true,
8702 /*use_default*/true);
8703
8704 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
8705 {
8706 if (complain & tf_error)
8707 {
8708 error ("use of invalid variable template %qE", var);
8709 diagnose_constraints (location_of (var), templ, arglist);
8710 }
8711 return error_mark_node;
8712 }
8713
8714 /* If a template-id refers to a specialization of a variable
8715 concept, then the expression is true if and only if the
8716 concept's constraints are satisfied by the given template
8717 arguments.
8718
8719 NOTE: This is an extension of Concepts Lite TS that
8720 allows constraints to be used in expressions. */
8721 if (concept_p)
8722 {
8723 tree decl = DECL_TEMPLATE_RESULT (templ);
8724 return evaluate_variable_concept (decl, arglist);
8725 }
8726
8727 return instantiate_template (templ, arglist, complain);
8728 }
8729
8730 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
8731 TARGS template args, and instantiate it if it's not dependent. */
8732
8733 static tree
8734 lookup_and_finish_template_variable (tree templ, tree targs,
8735 tsubst_flags_t complain)
8736 {
8737 templ = lookup_template_variable (templ, targs);
8738 if (!any_dependent_template_arguments_p (targs))
8739 {
8740 templ = finish_template_variable (templ, complain);
8741 mark_used (templ);
8742 }
8743
8744 return convert_from_reference (templ);
8745 }
8746
8747 \f
8748 struct pair_fn_data
8749 {
8750 tree_fn_t fn;
8751 void *data;
8752 /* True when we should also visit template parameters that occur in
8753 non-deduced contexts. */
8754 bool include_nondeduced_p;
8755 hash_set<tree> *visited;
8756 };
8757
8758 /* Called from for_each_template_parm via walk_tree. */
8759
8760 static tree
8761 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8762 {
8763 tree t = *tp;
8764 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8765 tree_fn_t fn = pfd->fn;
8766 void *data = pfd->data;
8767 tree result = NULL_TREE;
8768
8769 #define WALK_SUBTREE(NODE) \
8770 do \
8771 { \
8772 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8773 pfd->include_nondeduced_p); \
8774 if (result) goto out; \
8775 } \
8776 while (0)
8777
8778 if (TYPE_P (t)
8779 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
8780 WALK_SUBTREE (TYPE_CONTEXT (t));
8781
8782 switch (TREE_CODE (t))
8783 {
8784 case RECORD_TYPE:
8785 if (TYPE_PTRMEMFUNC_P (t))
8786 break;
8787 /* Fall through. */
8788
8789 case UNION_TYPE:
8790 case ENUMERAL_TYPE:
8791 if (!TYPE_TEMPLATE_INFO (t))
8792 *walk_subtrees = 0;
8793 else
8794 WALK_SUBTREE (TYPE_TI_ARGS (t));
8795 break;
8796
8797 case INTEGER_TYPE:
8798 WALK_SUBTREE (TYPE_MIN_VALUE (t));
8799 WALK_SUBTREE (TYPE_MAX_VALUE (t));
8800 break;
8801
8802 case METHOD_TYPE:
8803 /* Since we're not going to walk subtrees, we have to do this
8804 explicitly here. */
8805 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
8806 /* Fall through. */
8807
8808 case FUNCTION_TYPE:
8809 /* Check the return type. */
8810 WALK_SUBTREE (TREE_TYPE (t));
8811
8812 /* Check the parameter types. Since default arguments are not
8813 instantiated until they are needed, the TYPE_ARG_TYPES may
8814 contain expressions that involve template parameters. But,
8815 no-one should be looking at them yet. And, once they're
8816 instantiated, they don't contain template parameters, so
8817 there's no point in looking at them then, either. */
8818 {
8819 tree parm;
8820
8821 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8822 WALK_SUBTREE (TREE_VALUE (parm));
8823
8824 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8825 want walk_tree walking into them itself. */
8826 *walk_subtrees = 0;
8827 }
8828 break;
8829
8830 case TYPEOF_TYPE:
8831 case UNDERLYING_TYPE:
8832 if (pfd->include_nondeduced_p
8833 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8834 pfd->visited,
8835 pfd->include_nondeduced_p))
8836 return error_mark_node;
8837 break;
8838
8839 case FUNCTION_DECL:
8840 case VAR_DECL:
8841 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8842 WALK_SUBTREE (DECL_TI_ARGS (t));
8843 /* Fall through. */
8844
8845 case PARM_DECL:
8846 case CONST_DECL:
8847 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
8848 WALK_SUBTREE (DECL_INITIAL (t));
8849 if (DECL_CONTEXT (t)
8850 && pfd->include_nondeduced_p)
8851 WALK_SUBTREE (DECL_CONTEXT (t));
8852 break;
8853
8854 case BOUND_TEMPLATE_TEMPLATE_PARM:
8855 /* Record template parameters such as `T' inside `TT<T>'. */
8856 WALK_SUBTREE (TYPE_TI_ARGS (t));
8857 /* Fall through. */
8858
8859 case TEMPLATE_TEMPLATE_PARM:
8860 case TEMPLATE_TYPE_PARM:
8861 case TEMPLATE_PARM_INDEX:
8862 if (fn && (*fn)(t, data))
8863 return t;
8864 else if (!fn)
8865 return t;
8866 break;
8867
8868 case TEMPLATE_DECL:
8869 /* A template template parameter is encountered. */
8870 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8871 WALK_SUBTREE (TREE_TYPE (t));
8872
8873 /* Already substituted template template parameter */
8874 *walk_subtrees = 0;
8875 break;
8876
8877 case TYPENAME_TYPE:
8878 /* A template-id in a TYPENAME_TYPE might be a deduced context after
8879 partial instantiation. */
8880 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
8881 break;
8882
8883 case CONSTRUCTOR:
8884 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8885 && pfd->include_nondeduced_p)
8886 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
8887 break;
8888
8889 case INDIRECT_REF:
8890 case COMPONENT_REF:
8891 /* If there's no type, then this thing must be some expression
8892 involving template parameters. */
8893 if (!fn && !TREE_TYPE (t))
8894 return error_mark_node;
8895 break;
8896
8897 case MODOP_EXPR:
8898 case CAST_EXPR:
8899 case IMPLICIT_CONV_EXPR:
8900 case REINTERPRET_CAST_EXPR:
8901 case CONST_CAST_EXPR:
8902 case STATIC_CAST_EXPR:
8903 case DYNAMIC_CAST_EXPR:
8904 case ARROW_EXPR:
8905 case DOTSTAR_EXPR:
8906 case TYPEID_EXPR:
8907 case PSEUDO_DTOR_EXPR:
8908 if (!fn)
8909 return error_mark_node;
8910 break;
8911
8912 default:
8913 break;
8914 }
8915
8916 #undef WALK_SUBTREE
8917
8918 /* We didn't find any template parameters we liked. */
8919 out:
8920 return result;
8921 }
8922
8923 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8924 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8925 call FN with the parameter and the DATA.
8926 If FN returns nonzero, the iteration is terminated, and
8927 for_each_template_parm returns 1. Otherwise, the iteration
8928 continues. If FN never returns a nonzero value, the value
8929 returned by for_each_template_parm is 0. If FN is NULL, it is
8930 considered to be the function which always returns 1.
8931
8932 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8933 parameters that occur in non-deduced contexts. When false, only
8934 visits those template parameters that can be deduced. */
8935
8936 static tree
8937 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8938 hash_set<tree> *visited,
8939 bool include_nondeduced_p)
8940 {
8941 struct pair_fn_data pfd;
8942 tree result;
8943
8944 /* Set up. */
8945 pfd.fn = fn;
8946 pfd.data = data;
8947 pfd.include_nondeduced_p = include_nondeduced_p;
8948
8949 /* Walk the tree. (Conceptually, we would like to walk without
8950 duplicates, but for_each_template_parm_r recursively calls
8951 for_each_template_parm, so we would need to reorganize a fair
8952 bit to use walk_tree_without_duplicates, so we keep our own
8953 visited list.) */
8954 if (visited)
8955 pfd.visited = visited;
8956 else
8957 pfd.visited = new hash_set<tree>;
8958 result = cp_walk_tree (&t,
8959 for_each_template_parm_r,
8960 &pfd,
8961 pfd.visited);
8962
8963 /* Clean up. */
8964 if (!visited)
8965 {
8966 delete pfd.visited;
8967 pfd.visited = 0;
8968 }
8969
8970 return result;
8971 }
8972
8973 /* Returns true if T depends on any template parameter. */
8974
8975 int
8976 uses_template_parms (tree t)
8977 {
8978 if (t == NULL_TREE)
8979 return false;
8980
8981 bool dependent_p;
8982 int saved_processing_template_decl;
8983
8984 saved_processing_template_decl = processing_template_decl;
8985 if (!saved_processing_template_decl)
8986 processing_template_decl = 1;
8987 if (TYPE_P (t))
8988 dependent_p = dependent_type_p (t);
8989 else if (TREE_CODE (t) == TREE_VEC)
8990 dependent_p = any_dependent_template_arguments_p (t);
8991 else if (TREE_CODE (t) == TREE_LIST)
8992 dependent_p = (uses_template_parms (TREE_VALUE (t))
8993 || uses_template_parms (TREE_CHAIN (t)));
8994 else if (TREE_CODE (t) == TYPE_DECL)
8995 dependent_p = dependent_type_p (TREE_TYPE (t));
8996 else if (DECL_P (t)
8997 || EXPR_P (t)
8998 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8999 || TREE_CODE (t) == OVERLOAD
9000 || BASELINK_P (t)
9001 || identifier_p (t)
9002 || TREE_CODE (t) == TRAIT_EXPR
9003 || TREE_CODE (t) == CONSTRUCTOR
9004 || CONSTANT_CLASS_P (t))
9005 dependent_p = (type_dependent_expression_p (t)
9006 || value_dependent_expression_p (t));
9007 else
9008 {
9009 gcc_assert (t == error_mark_node);
9010 dependent_p = false;
9011 }
9012
9013 processing_template_decl = saved_processing_template_decl;
9014
9015 return dependent_p;
9016 }
9017
9018 /* Returns true iff current_function_decl is an incompletely instantiated
9019 template. Useful instead of processing_template_decl because the latter
9020 is set to 0 during instantiate_non_dependent_expr. */
9021
9022 bool
9023 in_template_function (void)
9024 {
9025 tree fn = current_function_decl;
9026 bool ret;
9027 ++processing_template_decl;
9028 ret = (fn && DECL_LANG_SPECIFIC (fn)
9029 && DECL_TEMPLATE_INFO (fn)
9030 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9031 --processing_template_decl;
9032 return ret;
9033 }
9034
9035 /* Returns true if T depends on any template parameter with level LEVEL. */
9036
9037 bool
9038 uses_template_parms_level (tree t, int level)
9039 {
9040 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9041 /*include_nondeduced_p=*/true);
9042 }
9043
9044 /* Returns true if the signature of DECL depends on any template parameter from
9045 its enclosing class. */
9046
9047 bool
9048 uses_outer_template_parms (tree decl)
9049 {
9050 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9051 if (depth == 0)
9052 return false;
9053 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9054 &depth, NULL, /*include_nondeduced_p=*/true))
9055 return true;
9056 if (PRIMARY_TEMPLATE_P (decl)
9057 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9058 (DECL_TEMPLATE_PARMS (decl)),
9059 template_parm_outer_level,
9060 &depth, NULL, /*include_nondeduced_p=*/true))
9061 return true;
9062 tree ci = get_constraints (decl);
9063 if (ci)
9064 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9065 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9066 &depth, NULL, /*nondeduced*/true))
9067 return true;
9068 return false;
9069 }
9070
9071 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9072 ill-formed translation unit, i.e. a variable or function that isn't
9073 usable in a constant expression. */
9074
9075 static inline bool
9076 neglectable_inst_p (tree d)
9077 {
9078 return (DECL_P (d)
9079 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9080 : decl_maybe_constant_var_p (d)));
9081 }
9082
9083 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9084 neglectable and instantiated from within an erroneous instantiation. */
9085
9086 static bool
9087 limit_bad_template_recursion (tree decl)
9088 {
9089 struct tinst_level *lev = current_tinst_level;
9090 int errs = errorcount + sorrycount;
9091 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9092 return false;
9093
9094 for (; lev; lev = lev->next)
9095 if (neglectable_inst_p (lev->decl))
9096 break;
9097
9098 return (lev && errs > lev->errors);
9099 }
9100
9101 static int tinst_depth;
9102 extern int max_tinst_depth;
9103 int depth_reached;
9104
9105 static GTY(()) struct tinst_level *last_error_tinst_level;
9106
9107 /* We're starting to instantiate D; record the template instantiation context
9108 for diagnostics and to restore it later. */
9109
9110 bool
9111 push_tinst_level (tree d)
9112 {
9113 return push_tinst_level_loc (d, input_location);
9114 }
9115
9116 /* We're starting to instantiate D; record the template instantiation context
9117 at LOC for diagnostics and to restore it later. */
9118
9119 bool
9120 push_tinst_level_loc (tree d, location_t loc)
9121 {
9122 struct tinst_level *new_level;
9123
9124 if (tinst_depth >= max_tinst_depth)
9125 {
9126 /* Tell error.c not to try to instantiate any templates. */
9127 at_eof = 2;
9128 fatal_error (input_location,
9129 "template instantiation depth exceeds maximum of %d"
9130 " (use -ftemplate-depth= to increase the maximum)",
9131 max_tinst_depth);
9132 return false;
9133 }
9134
9135 /* If the current instantiation caused problems, don't let it instantiate
9136 anything else. Do allow deduction substitution and decls usable in
9137 constant expressions. */
9138 if (limit_bad_template_recursion (d))
9139 return false;
9140
9141 new_level = ggc_alloc<tinst_level> ();
9142 new_level->decl = d;
9143 new_level->locus = loc;
9144 new_level->errors = errorcount+sorrycount;
9145 new_level->in_system_header_p = in_system_header_at (input_location);
9146 new_level->next = current_tinst_level;
9147 current_tinst_level = new_level;
9148
9149 ++tinst_depth;
9150 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9151 depth_reached = tinst_depth;
9152
9153 return true;
9154 }
9155
9156 /* We're done instantiating this template; return to the instantiation
9157 context. */
9158
9159 void
9160 pop_tinst_level (void)
9161 {
9162 /* Restore the filename and line number stashed away when we started
9163 this instantiation. */
9164 input_location = current_tinst_level->locus;
9165 current_tinst_level = current_tinst_level->next;
9166 --tinst_depth;
9167 }
9168
9169 /* We're instantiating a deferred template; restore the template
9170 instantiation context in which the instantiation was requested, which
9171 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9172
9173 static tree
9174 reopen_tinst_level (struct tinst_level *level)
9175 {
9176 struct tinst_level *t;
9177
9178 tinst_depth = 0;
9179 for (t = level; t; t = t->next)
9180 ++tinst_depth;
9181
9182 current_tinst_level = level;
9183 pop_tinst_level ();
9184 if (current_tinst_level)
9185 current_tinst_level->errors = errorcount+sorrycount;
9186 return level->decl;
9187 }
9188
9189 /* Returns the TINST_LEVEL which gives the original instantiation
9190 context. */
9191
9192 struct tinst_level *
9193 outermost_tinst_level (void)
9194 {
9195 struct tinst_level *level = current_tinst_level;
9196 if (level)
9197 while (level->next)
9198 level = level->next;
9199 return level;
9200 }
9201
9202 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9203 vector of template arguments, as for tsubst.
9204
9205 Returns an appropriate tsubst'd friend declaration. */
9206
9207 static tree
9208 tsubst_friend_function (tree decl, tree args)
9209 {
9210 tree new_friend;
9211
9212 if (TREE_CODE (decl) == FUNCTION_DECL
9213 && DECL_TEMPLATE_INSTANTIATION (decl)
9214 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9215 /* This was a friend declared with an explicit template
9216 argument list, e.g.:
9217
9218 friend void f<>(T);
9219
9220 to indicate that f was a template instantiation, not a new
9221 function declaration. Now, we have to figure out what
9222 instantiation of what template. */
9223 {
9224 tree template_id, arglist, fns;
9225 tree new_args;
9226 tree tmpl;
9227 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9228
9229 /* Friend functions are looked up in the containing namespace scope.
9230 We must enter that scope, to avoid finding member functions of the
9231 current class with same name. */
9232 push_nested_namespace (ns);
9233 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9234 tf_warning_or_error, NULL_TREE,
9235 /*integral_constant_expression_p=*/false);
9236 pop_nested_namespace (ns);
9237 arglist = tsubst (DECL_TI_ARGS (decl), args,
9238 tf_warning_or_error, NULL_TREE);
9239 template_id = lookup_template_function (fns, arglist);
9240
9241 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9242 tmpl = determine_specialization (template_id, new_friend,
9243 &new_args,
9244 /*need_member_template=*/0,
9245 TREE_VEC_LENGTH (args),
9246 tsk_none);
9247 return instantiate_template (tmpl, new_args, tf_error);
9248 }
9249
9250 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9251
9252 /* The NEW_FRIEND will look like an instantiation, to the
9253 compiler, but is not an instantiation from the point of view of
9254 the language. For example, we might have had:
9255
9256 template <class T> struct S {
9257 template <class U> friend void f(T, U);
9258 };
9259
9260 Then, in S<int>, template <class U> void f(int, U) is not an
9261 instantiation of anything. */
9262 if (new_friend == error_mark_node)
9263 return error_mark_node;
9264
9265 DECL_USE_TEMPLATE (new_friend) = 0;
9266 if (TREE_CODE (decl) == TEMPLATE_DECL)
9267 {
9268 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9269 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9270 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9271 }
9272
9273 /* The mangled name for the NEW_FRIEND is incorrect. The function
9274 is not a template instantiation and should not be mangled like
9275 one. Therefore, we forget the mangling here; we'll recompute it
9276 later if we need it. */
9277 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9278 {
9279 SET_DECL_RTL (new_friend, NULL);
9280 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9281 }
9282
9283 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9284 {
9285 tree old_decl;
9286 tree new_friend_template_info;
9287 tree new_friend_result_template_info;
9288 tree ns;
9289 int new_friend_is_defn;
9290
9291 /* We must save some information from NEW_FRIEND before calling
9292 duplicate decls since that function will free NEW_FRIEND if
9293 possible. */
9294 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9295 new_friend_is_defn =
9296 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9297 (template_for_substitution (new_friend)))
9298 != NULL_TREE);
9299 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9300 {
9301 /* This declaration is a `primary' template. */
9302 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9303
9304 new_friend_result_template_info
9305 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9306 }
9307 else
9308 new_friend_result_template_info = NULL_TREE;
9309
9310 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9311 if (new_friend_is_defn)
9312 DECL_INITIAL (new_friend) = error_mark_node;
9313
9314 /* Inside pushdecl_namespace_level, we will push into the
9315 current namespace. However, the friend function should go
9316 into the namespace of the template. */
9317 ns = decl_namespace_context (new_friend);
9318 push_nested_namespace (ns);
9319 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9320 pop_nested_namespace (ns);
9321
9322 if (old_decl == error_mark_node)
9323 return error_mark_node;
9324
9325 if (old_decl != new_friend)
9326 {
9327 /* This new friend declaration matched an existing
9328 declaration. For example, given:
9329
9330 template <class T> void f(T);
9331 template <class U> class C {
9332 template <class T> friend void f(T) {}
9333 };
9334
9335 the friend declaration actually provides the definition
9336 of `f', once C has been instantiated for some type. So,
9337 old_decl will be the out-of-class template declaration,
9338 while new_friend is the in-class definition.
9339
9340 But, if `f' was called before this point, the
9341 instantiation of `f' will have DECL_TI_ARGS corresponding
9342 to `T' but not to `U', references to which might appear
9343 in the definition of `f'. Previously, the most general
9344 template for an instantiation of `f' was the out-of-class
9345 version; now it is the in-class version. Therefore, we
9346 run through all specialization of `f', adding to their
9347 DECL_TI_ARGS appropriately. In particular, they need a
9348 new set of outer arguments, corresponding to the
9349 arguments for this class instantiation.
9350
9351 The same situation can arise with something like this:
9352
9353 friend void f(int);
9354 template <class T> class C {
9355 friend void f(T) {}
9356 };
9357
9358 when `C<int>' is instantiated. Now, `f(int)' is defined
9359 in the class. */
9360
9361 if (!new_friend_is_defn)
9362 /* On the other hand, if the in-class declaration does
9363 *not* provide a definition, then we don't want to alter
9364 existing definitions. We can just leave everything
9365 alone. */
9366 ;
9367 else
9368 {
9369 tree new_template = TI_TEMPLATE (new_friend_template_info);
9370 tree new_args = TI_ARGS (new_friend_template_info);
9371
9372 /* Overwrite whatever template info was there before, if
9373 any, with the new template information pertaining to
9374 the declaration. */
9375 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9376
9377 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9378 {
9379 /* We should have called reregister_specialization in
9380 duplicate_decls. */
9381 gcc_assert (retrieve_specialization (new_template,
9382 new_args, 0)
9383 == old_decl);
9384
9385 /* Instantiate it if the global has already been used. */
9386 if (DECL_ODR_USED (old_decl))
9387 instantiate_decl (old_decl, /*defer_ok=*/true,
9388 /*expl_inst_class_mem_p=*/false);
9389 }
9390 else
9391 {
9392 tree t;
9393
9394 /* Indicate that the old function template is a partial
9395 instantiation. */
9396 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9397 = new_friend_result_template_info;
9398
9399 gcc_assert (new_template
9400 == most_general_template (new_template));
9401 gcc_assert (new_template != old_decl);
9402
9403 /* Reassign any specializations already in the hash table
9404 to the new more general template, and add the
9405 additional template args. */
9406 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9407 t != NULL_TREE;
9408 t = TREE_CHAIN (t))
9409 {
9410 tree spec = TREE_VALUE (t);
9411 spec_entry elt;
9412
9413 elt.tmpl = old_decl;
9414 elt.args = DECL_TI_ARGS (spec);
9415 elt.spec = NULL_TREE;
9416
9417 decl_specializations->remove_elt (&elt);
9418
9419 DECL_TI_ARGS (spec)
9420 = add_outermost_template_args (new_args,
9421 DECL_TI_ARGS (spec));
9422
9423 register_specialization
9424 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9425
9426 }
9427 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9428 }
9429 }
9430
9431 /* The information from NEW_FRIEND has been merged into OLD_DECL
9432 by duplicate_decls. */
9433 new_friend = old_decl;
9434 }
9435 }
9436 else
9437 {
9438 tree context = DECL_CONTEXT (new_friend);
9439 bool dependent_p;
9440
9441 /* In the code
9442 template <class T> class C {
9443 template <class U> friend void C1<U>::f (); // case 1
9444 friend void C2<T>::f (); // case 2
9445 };
9446 we only need to make sure CONTEXT is a complete type for
9447 case 2. To distinguish between the two cases, we note that
9448 CONTEXT of case 1 remains dependent type after tsubst while
9449 this isn't true for case 2. */
9450 ++processing_template_decl;
9451 dependent_p = dependent_type_p (context);
9452 --processing_template_decl;
9453
9454 if (!dependent_p
9455 && !complete_type_or_else (context, NULL_TREE))
9456 return error_mark_node;
9457
9458 if (COMPLETE_TYPE_P (context))
9459 {
9460 tree fn = new_friend;
9461 /* do_friend adds the TEMPLATE_DECL for any member friend
9462 template even if it isn't a member template, i.e.
9463 template <class T> friend A<T>::f();
9464 Look through it in that case. */
9465 if (TREE_CODE (fn) == TEMPLATE_DECL
9466 && !PRIMARY_TEMPLATE_P (fn))
9467 fn = DECL_TEMPLATE_RESULT (fn);
9468 /* Check to see that the declaration is really present, and,
9469 possibly obtain an improved declaration. */
9470 fn = check_classfn (context, fn, NULL_TREE);
9471
9472 if (fn)
9473 new_friend = fn;
9474 }
9475 }
9476
9477 return new_friend;
9478 }
9479
9480 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9481 template arguments, as for tsubst.
9482
9483 Returns an appropriate tsubst'd friend type or error_mark_node on
9484 failure. */
9485
9486 static tree
9487 tsubst_friend_class (tree friend_tmpl, tree args)
9488 {
9489 tree friend_type;
9490 tree tmpl;
9491 tree context;
9492
9493 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9494 {
9495 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9496 return TREE_TYPE (t);
9497 }
9498
9499 context = CP_DECL_CONTEXT (friend_tmpl);
9500
9501 if (context != global_namespace)
9502 {
9503 if (TREE_CODE (context) == NAMESPACE_DECL)
9504 push_nested_namespace (context);
9505 else
9506 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9507 }
9508
9509 /* Look for a class template declaration. We look for hidden names
9510 because two friend declarations of the same template are the
9511 same. For example, in:
9512
9513 struct A {
9514 template <typename> friend class F;
9515 };
9516 template <typename> struct B {
9517 template <typename> friend class F;
9518 };
9519
9520 both F templates are the same. */
9521 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9522 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9523
9524 /* But, if we don't find one, it might be because we're in a
9525 situation like this:
9526
9527 template <class T>
9528 struct S {
9529 template <class U>
9530 friend struct S;
9531 };
9532
9533 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9534 for `S<int>', not the TEMPLATE_DECL. */
9535 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9536 {
9537 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9538 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9539 }
9540
9541 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9542 {
9543 /* The friend template has already been declared. Just
9544 check to see that the declarations match, and install any new
9545 default parameters. We must tsubst the default parameters,
9546 of course. We only need the innermost template parameters
9547 because that is all that redeclare_class_template will look
9548 at. */
9549 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9550 > TMPL_ARGS_DEPTH (args))
9551 {
9552 tree parms;
9553 location_t saved_input_location;
9554 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9555 args, tf_warning_or_error);
9556
9557 saved_input_location = input_location;
9558 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9559 tree cons = get_constraints (tmpl);
9560 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9561 input_location = saved_input_location;
9562
9563 }
9564
9565 friend_type = TREE_TYPE (tmpl);
9566 }
9567 else
9568 {
9569 /* The friend template has not already been declared. In this
9570 case, the instantiation of the template class will cause the
9571 injection of this template into the global scope. */
9572 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9573 if (tmpl == error_mark_node)
9574 return error_mark_node;
9575
9576 /* The new TMPL is not an instantiation of anything, so we
9577 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9578 the new type because that is supposed to be the corresponding
9579 template decl, i.e., TMPL. */
9580 DECL_USE_TEMPLATE (tmpl) = 0;
9581 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9582 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9583 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9584 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9585
9586 /* Inject this template into the global scope. */
9587 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9588 }
9589
9590 if (context != global_namespace)
9591 {
9592 if (TREE_CODE (context) == NAMESPACE_DECL)
9593 pop_nested_namespace (context);
9594 else
9595 pop_nested_class ();
9596 }
9597
9598 return friend_type;
9599 }
9600
9601 /* Returns zero if TYPE cannot be completed later due to circularity.
9602 Otherwise returns one. */
9603
9604 static int
9605 can_complete_type_without_circularity (tree type)
9606 {
9607 if (type == NULL_TREE || type == error_mark_node)
9608 return 0;
9609 else if (COMPLETE_TYPE_P (type))
9610 return 1;
9611 else if (TREE_CODE (type) == ARRAY_TYPE)
9612 return can_complete_type_without_circularity (TREE_TYPE (type));
9613 else if (CLASS_TYPE_P (type)
9614 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9615 return 0;
9616 else
9617 return 1;
9618 }
9619
9620 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9621 tsubst_flags_t, tree);
9622
9623 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9624 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9625
9626 static tree
9627 tsubst_attribute (tree t, tree *decl_p, tree args,
9628 tsubst_flags_t complain, tree in_decl)
9629 {
9630 gcc_assert (ATTR_IS_DEPENDENT (t));
9631
9632 tree val = TREE_VALUE (t);
9633 if (val == NULL_TREE)
9634 /* Nothing to do. */;
9635 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9636 && is_attribute_p ("omp declare simd",
9637 get_attribute_name (t)))
9638 {
9639 tree clauses = TREE_VALUE (val);
9640 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9641 complain, in_decl);
9642 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9643 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9644 tree parms = DECL_ARGUMENTS (*decl_p);
9645 clauses
9646 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9647 if (clauses)
9648 val = build_tree_list (NULL_TREE, clauses);
9649 else
9650 val = NULL_TREE;
9651 }
9652 /* If the first attribute argument is an identifier, don't
9653 pass it through tsubst. Attributes like mode, format,
9654 cleanup and several target specific attributes expect it
9655 unmodified. */
9656 else if (attribute_takes_identifier_p (get_attribute_name (t)))
9657 {
9658 tree chain
9659 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
9660 /*integral_constant_expression_p=*/false);
9661 if (chain != TREE_CHAIN (val))
9662 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
9663 }
9664 else if (PACK_EXPANSION_P (val))
9665 {
9666 /* An attribute pack expansion. */
9667 tree purp = TREE_PURPOSE (t);
9668 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
9669 int len = TREE_VEC_LENGTH (pack);
9670 tree list = NULL_TREE;
9671 tree *q = &list;
9672 for (int i = 0; i < len; ++i)
9673 {
9674 tree elt = TREE_VEC_ELT (pack, i);
9675 *q = build_tree_list (purp, elt);
9676 q = &TREE_CHAIN (*q);
9677 }
9678 return list;
9679 }
9680 else
9681 val = tsubst_expr (val, args, complain, in_decl,
9682 /*integral_constant_expression_p=*/false);
9683
9684 if (val != TREE_VALUE (t))
9685 return build_tree_list (TREE_PURPOSE (t), val);
9686 return t;
9687 }
9688
9689 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
9690 unchanged or a new TREE_LIST chain. */
9691
9692 static tree
9693 tsubst_attributes (tree attributes, tree args,
9694 tsubst_flags_t complain, tree in_decl)
9695 {
9696 tree last_dep = NULL_TREE;
9697
9698 for (tree t = attributes; t; t = TREE_CHAIN (t))
9699 if (ATTR_IS_DEPENDENT (t))
9700 {
9701 last_dep = t;
9702 attributes = copy_list (attributes);
9703 break;
9704 }
9705
9706 if (last_dep)
9707 for (tree *p = &attributes; *p; )
9708 {
9709 tree t = *p;
9710 if (ATTR_IS_DEPENDENT (t))
9711 {
9712 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
9713 if (subst != t)
9714 {
9715 *p = subst;
9716 do
9717 p = &TREE_CHAIN (*p);
9718 while (*p);
9719 *p = TREE_CHAIN (t);
9720 continue;
9721 }
9722 }
9723 p = &TREE_CHAIN (*p);
9724 }
9725
9726 return attributes;
9727 }
9728
9729 /* Apply any attributes which had to be deferred until instantiation
9730 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9731 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9732
9733 static void
9734 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9735 tree args, tsubst_flags_t complain, tree in_decl)
9736 {
9737 tree last_dep = NULL_TREE;
9738 tree t;
9739 tree *p;
9740
9741 for (t = attributes; t; t = TREE_CHAIN (t))
9742 if (ATTR_IS_DEPENDENT (t))
9743 {
9744 last_dep = t;
9745 attributes = copy_list (attributes);
9746 break;
9747 }
9748
9749 if (DECL_P (*decl_p))
9750 {
9751 if (TREE_TYPE (*decl_p) == error_mark_node)
9752 return;
9753 p = &DECL_ATTRIBUTES (*decl_p);
9754 }
9755 else
9756 p = &TYPE_ATTRIBUTES (*decl_p);
9757
9758 if (last_dep)
9759 {
9760 tree late_attrs = NULL_TREE;
9761 tree *q = &late_attrs;
9762
9763 for (*p = attributes; *p; )
9764 {
9765 t = *p;
9766 if (ATTR_IS_DEPENDENT (t))
9767 {
9768 *p = TREE_CHAIN (t);
9769 TREE_CHAIN (t) = NULL_TREE;
9770 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
9771 do
9772 q = &TREE_CHAIN (*q);
9773 while (*q);
9774 }
9775 else
9776 p = &TREE_CHAIN (t);
9777 }
9778
9779 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9780 }
9781 }
9782
9783 /* Perform (or defer) access check for typedefs that were referenced
9784 from within the template TMPL code.
9785 This is a subroutine of instantiate_decl and instantiate_class_template.
9786 TMPL is the template to consider and TARGS is the list of arguments of
9787 that template. */
9788
9789 static void
9790 perform_typedefs_access_check (tree tmpl, tree targs)
9791 {
9792 location_t saved_location;
9793 unsigned i;
9794 qualified_typedef_usage_t *iter;
9795
9796 if (!tmpl
9797 || (!CLASS_TYPE_P (tmpl)
9798 && TREE_CODE (tmpl) != FUNCTION_DECL))
9799 return;
9800
9801 saved_location = input_location;
9802 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9803 {
9804 tree type_decl = iter->typedef_decl;
9805 tree type_scope = iter->context;
9806
9807 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9808 continue;
9809
9810 if (uses_template_parms (type_decl))
9811 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9812 if (uses_template_parms (type_scope))
9813 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9814
9815 /* Make access check error messages point to the location
9816 of the use of the typedef. */
9817 input_location = iter->locus;
9818 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9819 type_decl, type_decl,
9820 tf_warning_or_error);
9821 }
9822 input_location = saved_location;
9823 }
9824
9825 static tree
9826 instantiate_class_template_1 (tree type)
9827 {
9828 tree templ, args, pattern, t, member;
9829 tree typedecl;
9830 tree pbinfo;
9831 tree base_list;
9832 unsigned int saved_maximum_field_alignment;
9833 tree fn_context;
9834
9835 if (type == error_mark_node)
9836 return error_mark_node;
9837
9838 if (COMPLETE_OR_OPEN_TYPE_P (type)
9839 || uses_template_parms (type))
9840 return type;
9841
9842 /* Figure out which template is being instantiated. */
9843 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9844 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9845
9846 /* Determine what specialization of the original template to
9847 instantiate. */
9848 t = most_specialized_partial_spec (type, tf_warning_or_error);
9849 if (t == error_mark_node)
9850 {
9851 TYPE_BEING_DEFINED (type) = 1;
9852 return error_mark_node;
9853 }
9854 else if (t)
9855 {
9856 /* This TYPE is actually an instantiation of a partial
9857 specialization. We replace the innermost set of ARGS with
9858 the arguments appropriate for substitution. For example,
9859 given:
9860
9861 template <class T> struct S {};
9862 template <class T> struct S<T*> {};
9863
9864 and supposing that we are instantiating S<int*>, ARGS will
9865 presently be {int*} -- but we need {int}. */
9866 pattern = TREE_TYPE (t);
9867 args = TREE_PURPOSE (t);
9868 }
9869 else
9870 {
9871 pattern = TREE_TYPE (templ);
9872 args = CLASSTYPE_TI_ARGS (type);
9873 }
9874
9875 /* If the template we're instantiating is incomplete, then clearly
9876 there's nothing we can do. */
9877 if (!COMPLETE_TYPE_P (pattern))
9878 return type;
9879
9880 /* If we've recursively instantiated too many templates, stop. */
9881 if (! push_tinst_level (type))
9882 return type;
9883
9884 /* Now we're really doing the instantiation. Mark the type as in
9885 the process of being defined. */
9886 TYPE_BEING_DEFINED (type) = 1;
9887
9888 /* We may be in the middle of deferred access check. Disable
9889 it now. */
9890 push_deferring_access_checks (dk_no_deferred);
9891
9892 int saved_unevaluated_operand = cp_unevaluated_operand;
9893 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9894
9895 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9896 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9897 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9898 fn_context = error_mark_node;
9899 if (!fn_context)
9900 push_to_top_level ();
9901 else
9902 {
9903 cp_unevaluated_operand = 0;
9904 c_inhibit_evaluation_warnings = 0;
9905 }
9906 /* Use #pragma pack from the template context. */
9907 saved_maximum_field_alignment = maximum_field_alignment;
9908 maximum_field_alignment = TYPE_PRECISION (pattern);
9909
9910 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9911
9912 /* Set the input location to the most specialized template definition.
9913 This is needed if tsubsting causes an error. */
9914 typedecl = TYPE_MAIN_DECL (pattern);
9915 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9916 DECL_SOURCE_LOCATION (typedecl);
9917
9918 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9919 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
9920 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9921 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9922 if (ANON_AGGR_TYPE_P (pattern))
9923 SET_ANON_AGGR_TYPE_P (type);
9924 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9925 {
9926 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9927 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9928 /* Adjust visibility for template arguments. */
9929 determine_visibility (TYPE_MAIN_DECL (type));
9930 }
9931 if (CLASS_TYPE_P (type))
9932 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9933
9934 pbinfo = TYPE_BINFO (pattern);
9935
9936 /* We should never instantiate a nested class before its enclosing
9937 class; we need to look up the nested class by name before we can
9938 instantiate it, and that lookup should instantiate the enclosing
9939 class. */
9940 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9941 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9942
9943 base_list = NULL_TREE;
9944 if (BINFO_N_BASE_BINFOS (pbinfo))
9945 {
9946 tree pbase_binfo;
9947 tree pushed_scope;
9948 int i;
9949
9950 /* We must enter the scope containing the type, as that is where
9951 the accessibility of types named in dependent bases are
9952 looked up from. */
9953 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9954
9955 /* Substitute into each of the bases to determine the actual
9956 basetypes. */
9957 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9958 {
9959 tree base;
9960 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9961 tree expanded_bases = NULL_TREE;
9962 int idx, len = 1;
9963
9964 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9965 {
9966 expanded_bases =
9967 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9968 args, tf_error, NULL_TREE);
9969 if (expanded_bases == error_mark_node)
9970 continue;
9971
9972 len = TREE_VEC_LENGTH (expanded_bases);
9973 }
9974
9975 for (idx = 0; idx < len; idx++)
9976 {
9977 if (expanded_bases)
9978 /* Extract the already-expanded base class. */
9979 base = TREE_VEC_ELT (expanded_bases, idx);
9980 else
9981 /* Substitute to figure out the base class. */
9982 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9983 NULL_TREE);
9984
9985 if (base == error_mark_node)
9986 continue;
9987
9988 base_list = tree_cons (access, base, base_list);
9989 if (BINFO_VIRTUAL_P (pbase_binfo))
9990 TREE_TYPE (base_list) = integer_type_node;
9991 }
9992 }
9993
9994 /* The list is now in reverse order; correct that. */
9995 base_list = nreverse (base_list);
9996
9997 if (pushed_scope)
9998 pop_scope (pushed_scope);
9999 }
10000 /* Now call xref_basetypes to set up all the base-class
10001 information. */
10002 xref_basetypes (type, base_list);
10003
10004 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10005 (int) ATTR_FLAG_TYPE_IN_PLACE,
10006 args, tf_error, NULL_TREE);
10007 fixup_attribute_variants (type);
10008
10009 /* Now that our base classes are set up, enter the scope of the
10010 class, so that name lookups into base classes, etc. will work
10011 correctly. This is precisely analogous to what we do in
10012 begin_class_definition when defining an ordinary non-template
10013 class, except we also need to push the enclosing classes. */
10014 push_nested_class (type);
10015
10016 /* Now members are processed in the order of declaration. */
10017 for (member = CLASSTYPE_DECL_LIST (pattern);
10018 member; member = TREE_CHAIN (member))
10019 {
10020 tree t = TREE_VALUE (member);
10021
10022 if (TREE_PURPOSE (member))
10023 {
10024 if (TYPE_P (t))
10025 {
10026 /* Build new CLASSTYPE_NESTED_UTDS. */
10027
10028 tree newtag;
10029 bool class_template_p;
10030
10031 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10032 && TYPE_LANG_SPECIFIC (t)
10033 && CLASSTYPE_IS_TEMPLATE (t));
10034 /* If the member is a class template, then -- even after
10035 substitution -- there may be dependent types in the
10036 template argument list for the class. We increment
10037 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10038 that function will assume that no types are dependent
10039 when outside of a template. */
10040 if (class_template_p)
10041 ++processing_template_decl;
10042 newtag = tsubst (t, args, tf_error, NULL_TREE);
10043 if (class_template_p)
10044 --processing_template_decl;
10045 if (newtag == error_mark_node)
10046 continue;
10047
10048 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10049 {
10050 tree name = TYPE_IDENTIFIER (t);
10051
10052 if (class_template_p)
10053 /* Unfortunately, lookup_template_class sets
10054 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10055 instantiation (i.e., for the type of a member
10056 template class nested within a template class.)
10057 This behavior is required for
10058 maybe_process_partial_specialization to work
10059 correctly, but is not accurate in this case;
10060 the TAG is not an instantiation of anything.
10061 (The corresponding TEMPLATE_DECL is an
10062 instantiation, but the TYPE is not.) */
10063 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10064
10065 /* Now, we call pushtag to put this NEWTAG into the scope of
10066 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10067 pushtag calling push_template_decl. We don't have to do
10068 this for enums because it will already have been done in
10069 tsubst_enum. */
10070 if (name)
10071 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10072 pushtag (name, newtag, /*tag_scope=*/ts_current);
10073 }
10074 }
10075 else if (DECL_DECLARES_FUNCTION_P (t))
10076 {
10077 /* Build new TYPE_METHODS. */
10078 tree r;
10079
10080 if (TREE_CODE (t) == TEMPLATE_DECL)
10081 ++processing_template_decl;
10082 r = tsubst (t, args, tf_error, NULL_TREE);
10083 if (TREE_CODE (t) == TEMPLATE_DECL)
10084 --processing_template_decl;
10085 set_current_access_from_decl (r);
10086 finish_member_declaration (r);
10087 /* Instantiate members marked with attribute used. */
10088 if (r != error_mark_node && DECL_PRESERVE_P (r))
10089 mark_used (r);
10090 if (TREE_CODE (r) == FUNCTION_DECL
10091 && DECL_OMP_DECLARE_REDUCTION_P (r))
10092 cp_check_omp_declare_reduction (r);
10093 }
10094 else if (DECL_CLASS_TEMPLATE_P (t)
10095 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10096 /* A closure type for a lambda in a default argument for a
10097 member template. Ignore it; it will be instantiated with
10098 the default argument. */;
10099 else
10100 {
10101 /* Build new TYPE_FIELDS. */
10102 if (TREE_CODE (t) == STATIC_ASSERT)
10103 {
10104 tree condition;
10105
10106 ++c_inhibit_evaluation_warnings;
10107 condition =
10108 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10109 tf_warning_or_error, NULL_TREE,
10110 /*integral_constant_expression_p=*/true);
10111 --c_inhibit_evaluation_warnings;
10112
10113 finish_static_assert (condition,
10114 STATIC_ASSERT_MESSAGE (t),
10115 STATIC_ASSERT_SOURCE_LOCATION (t),
10116 /*member_p=*/true);
10117 }
10118 else if (TREE_CODE (t) != CONST_DECL)
10119 {
10120 tree r;
10121 tree vec = NULL_TREE;
10122 int len = 1;
10123
10124 /* The file and line for this declaration, to
10125 assist in error message reporting. Since we
10126 called push_tinst_level above, we don't need to
10127 restore these. */
10128 input_location = DECL_SOURCE_LOCATION (t);
10129
10130 if (TREE_CODE (t) == TEMPLATE_DECL)
10131 ++processing_template_decl;
10132 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10133 if (TREE_CODE (t) == TEMPLATE_DECL)
10134 --processing_template_decl;
10135
10136 if (TREE_CODE (r) == TREE_VEC)
10137 {
10138 /* A capture pack became multiple fields. */
10139 vec = r;
10140 len = TREE_VEC_LENGTH (vec);
10141 }
10142
10143 for (int i = 0; i < len; ++i)
10144 {
10145 if (vec)
10146 r = TREE_VEC_ELT (vec, i);
10147 if (VAR_P (r))
10148 {
10149 /* In [temp.inst]:
10150
10151 [t]he initialization (and any associated
10152 side-effects) of a static data member does
10153 not occur unless the static data member is
10154 itself used in a way that requires the
10155 definition of the static data member to
10156 exist.
10157
10158 Therefore, we do not substitute into the
10159 initialized for the static data member here. */
10160 finish_static_data_member_decl
10161 (r,
10162 /*init=*/NULL_TREE,
10163 /*init_const_expr_p=*/false,
10164 /*asmspec_tree=*/NULL_TREE,
10165 /*flags=*/0);
10166 /* Instantiate members marked with attribute used. */
10167 if (r != error_mark_node && DECL_PRESERVE_P (r))
10168 mark_used (r);
10169 }
10170 else if (TREE_CODE (r) == FIELD_DECL)
10171 {
10172 /* Determine whether R has a valid type and can be
10173 completed later. If R is invalid, then its type
10174 is replaced by error_mark_node. */
10175 tree rtype = TREE_TYPE (r);
10176 if (can_complete_type_without_circularity (rtype))
10177 complete_type (rtype);
10178
10179 if (!complete_or_array_type_p (rtype))
10180 {
10181 /* If R's type couldn't be completed and
10182 it isn't a flexible array member (whose
10183 type is incomplete by definition) give
10184 an error. */
10185 cxx_incomplete_type_error (r, rtype);
10186 TREE_TYPE (r) = error_mark_node;
10187 }
10188 }
10189
10190 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10191 such a thing will already have been added to the field
10192 list by tsubst_enum in finish_member_declaration in the
10193 CLASSTYPE_NESTED_UTDS case above. */
10194 if (!(TREE_CODE (r) == TYPE_DECL
10195 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10196 && DECL_ARTIFICIAL (r)))
10197 {
10198 set_current_access_from_decl (r);
10199 finish_member_declaration (r);
10200 }
10201 }
10202 }
10203 }
10204 }
10205 else
10206 {
10207 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10208 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10209 {
10210 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10211
10212 tree friend_type = t;
10213 bool adjust_processing_template_decl = false;
10214
10215 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10216 {
10217 /* template <class T> friend class C; */
10218 friend_type = tsubst_friend_class (friend_type, args);
10219 adjust_processing_template_decl = true;
10220 }
10221 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10222 {
10223 /* template <class T> friend class C::D; */
10224 friend_type = tsubst (friend_type, args,
10225 tf_warning_or_error, NULL_TREE);
10226 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10227 friend_type = TREE_TYPE (friend_type);
10228 adjust_processing_template_decl = true;
10229 }
10230 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10231 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10232 {
10233 /* This could be either
10234
10235 friend class T::C;
10236
10237 when dependent_type_p is false or
10238
10239 template <class U> friend class T::C;
10240
10241 otherwise. */
10242 /* Bump processing_template_decl in case this is something like
10243 template <class T> friend struct A<T>::B. */
10244 ++processing_template_decl;
10245 friend_type = tsubst (friend_type, args,
10246 tf_warning_or_error, NULL_TREE);
10247 if (dependent_type_p (friend_type))
10248 adjust_processing_template_decl = true;
10249 --processing_template_decl;
10250 }
10251 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10252 && hidden_name_p (TYPE_NAME (friend_type)))
10253 {
10254 /* friend class C;
10255
10256 where C hasn't been declared yet. Let's lookup name
10257 from namespace scope directly, bypassing any name that
10258 come from dependent base class. */
10259 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10260
10261 /* The call to xref_tag_from_type does injection for friend
10262 classes. */
10263 push_nested_namespace (ns);
10264 friend_type =
10265 xref_tag_from_type (friend_type, NULL_TREE,
10266 /*tag_scope=*/ts_current);
10267 pop_nested_namespace (ns);
10268 }
10269 else if (uses_template_parms (friend_type))
10270 /* friend class C<T>; */
10271 friend_type = tsubst (friend_type, args,
10272 tf_warning_or_error, NULL_TREE);
10273 /* Otherwise it's
10274
10275 friend class C;
10276
10277 where C is already declared or
10278
10279 friend class C<int>;
10280
10281 We don't have to do anything in these cases. */
10282
10283 if (adjust_processing_template_decl)
10284 /* Trick make_friend_class into realizing that the friend
10285 we're adding is a template, not an ordinary class. It's
10286 important that we use make_friend_class since it will
10287 perform some error-checking and output cross-reference
10288 information. */
10289 ++processing_template_decl;
10290
10291 if (friend_type != error_mark_node)
10292 make_friend_class (type, friend_type, /*complain=*/false);
10293
10294 if (adjust_processing_template_decl)
10295 --processing_template_decl;
10296 }
10297 else
10298 {
10299 /* Build new DECL_FRIENDLIST. */
10300 tree r;
10301
10302 /* The file and line for this declaration, to
10303 assist in error message reporting. Since we
10304 called push_tinst_level above, we don't need to
10305 restore these. */
10306 input_location = DECL_SOURCE_LOCATION (t);
10307
10308 if (TREE_CODE (t) == TEMPLATE_DECL)
10309 {
10310 ++processing_template_decl;
10311 push_deferring_access_checks (dk_no_check);
10312 }
10313
10314 r = tsubst_friend_function (t, args);
10315 add_friend (type, r, /*complain=*/false);
10316 if (TREE_CODE (t) == TEMPLATE_DECL)
10317 {
10318 pop_deferring_access_checks ();
10319 --processing_template_decl;
10320 }
10321 }
10322 }
10323 }
10324
10325 if (fn_context)
10326 {
10327 /* Restore these before substituting into the lambda capture
10328 initializers. */
10329 cp_unevaluated_operand = saved_unevaluated_operand;
10330 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10331 }
10332
10333 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10334 {
10335 tree decl = lambda_function (type);
10336 if (decl)
10337 {
10338 if (cxx_dialect >= cxx1z)
10339 CLASSTYPE_LITERAL_P (type) = true;
10340
10341 if (!DECL_TEMPLATE_INFO (decl)
10342 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10343 {
10344 /* Set function_depth to avoid garbage collection. */
10345 ++function_depth;
10346 instantiate_decl (decl, false, false);
10347 --function_depth;
10348 }
10349
10350 /* We need to instantiate the capture list from the template
10351 after we've instantiated the closure members, but before we
10352 consider adding the conversion op. Also keep any captures
10353 that may have been added during instantiation of the op(). */
10354 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10355 tree tmpl_cap
10356 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10357 args, tf_warning_or_error, NULL_TREE,
10358 false, false);
10359
10360 LAMBDA_EXPR_CAPTURE_LIST (expr)
10361 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10362
10363 maybe_add_lambda_conv_op (type);
10364 }
10365 else
10366 gcc_assert (errorcount);
10367 }
10368
10369 /* Set the file and line number information to whatever is given for
10370 the class itself. This puts error messages involving generated
10371 implicit functions at a predictable point, and the same point
10372 that would be used for non-template classes. */
10373 input_location = DECL_SOURCE_LOCATION (typedecl);
10374
10375 unreverse_member_declarations (type);
10376 finish_struct_1 (type);
10377 TYPE_BEING_DEFINED (type) = 0;
10378
10379 /* We don't instantiate default arguments for member functions. 14.7.1:
10380
10381 The implicit instantiation of a class template specialization causes
10382 the implicit instantiation of the declarations, but not of the
10383 definitions or default arguments, of the class member functions,
10384 member classes, static data members and member templates.... */
10385
10386 /* Some typedefs referenced from within the template code need to be access
10387 checked at template instantiation time, i.e now. These types were
10388 added to the template at parsing time. Let's get those and perform
10389 the access checks then. */
10390 perform_typedefs_access_check (pattern, args);
10391 perform_deferred_access_checks (tf_warning_or_error);
10392 pop_nested_class ();
10393 maximum_field_alignment = saved_maximum_field_alignment;
10394 if (!fn_context)
10395 pop_from_top_level ();
10396 pop_deferring_access_checks ();
10397 pop_tinst_level ();
10398
10399 /* The vtable for a template class can be emitted in any translation
10400 unit in which the class is instantiated. When there is no key
10401 method, however, finish_struct_1 will already have added TYPE to
10402 the keyed_classes list. */
10403 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10404 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10405
10406 return type;
10407 }
10408
10409 /* Wrapper for instantiate_class_template_1. */
10410
10411 tree
10412 instantiate_class_template (tree type)
10413 {
10414 tree ret;
10415 timevar_push (TV_TEMPLATE_INST);
10416 ret = instantiate_class_template_1 (type);
10417 timevar_pop (TV_TEMPLATE_INST);
10418 return ret;
10419 }
10420
10421 static tree
10422 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10423 {
10424 tree r;
10425
10426 if (!t)
10427 r = t;
10428 else if (TYPE_P (t))
10429 r = tsubst (t, args, complain, in_decl);
10430 else
10431 {
10432 if (!(complain & tf_warning))
10433 ++c_inhibit_evaluation_warnings;
10434 r = tsubst_expr (t, args, complain, in_decl,
10435 /*integral_constant_expression_p=*/true);
10436 if (!(complain & tf_warning))
10437 --c_inhibit_evaluation_warnings;
10438 }
10439 return r;
10440 }
10441
10442 /* Given a function parameter pack TMPL_PARM and some function parameters
10443 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10444 and set *SPEC_P to point at the next point in the list. */
10445
10446 tree
10447 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10448 {
10449 /* Collect all of the extra "packed" parameters into an
10450 argument pack. */
10451 tree parmvec;
10452 tree parmtypevec;
10453 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10454 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10455 tree spec_parm = *spec_p;
10456 int i, len;
10457
10458 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10459 if (tmpl_parm
10460 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10461 break;
10462
10463 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10464 parmvec = make_tree_vec (len);
10465 parmtypevec = make_tree_vec (len);
10466 spec_parm = *spec_p;
10467 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10468 {
10469 TREE_VEC_ELT (parmvec, i) = spec_parm;
10470 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10471 }
10472
10473 /* Build the argument packs. */
10474 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10475 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10476 TREE_TYPE (argpack) = argtypepack;
10477 *spec_p = spec_parm;
10478
10479 return argpack;
10480 }
10481
10482 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10483 NONTYPE_ARGUMENT_PACK. */
10484
10485 static tree
10486 make_fnparm_pack (tree spec_parm)
10487 {
10488 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10489 }
10490
10491 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10492 pack expansion with no extra args, 2 if it has extra args, or 0
10493 if it is not a pack expansion. */
10494
10495 static int
10496 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10497 {
10498 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10499 if (i >= TREE_VEC_LENGTH (vec))
10500 return 0;
10501 tree elt = TREE_VEC_ELT (vec, i);
10502 if (DECL_P (elt))
10503 /* A decl pack is itself an expansion. */
10504 elt = TREE_TYPE (elt);
10505 if (!PACK_EXPANSION_P (elt))
10506 return 0;
10507 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10508 return 2;
10509 return 1;
10510 }
10511
10512
10513 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10514
10515 static tree
10516 make_argument_pack_select (tree arg_pack, unsigned index)
10517 {
10518 tree aps = make_node (ARGUMENT_PACK_SELECT);
10519
10520 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10521 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10522
10523 return aps;
10524 }
10525
10526 /* This is a subroutine of tsubst_pack_expansion.
10527
10528 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10529 mechanism to store the (non complete list of) arguments of the
10530 substitution and return a non substituted pack expansion, in order
10531 to wait for when we have enough arguments to really perform the
10532 substitution. */
10533
10534 static bool
10535 use_pack_expansion_extra_args_p (tree parm_packs,
10536 int arg_pack_len,
10537 bool has_empty_arg)
10538 {
10539 /* If one pack has an expansion and another pack has a normal
10540 argument or if one pack has an empty argument and an another
10541 one hasn't then tsubst_pack_expansion cannot perform the
10542 substitution and need to fall back on the
10543 PACK_EXPANSION_EXTRA mechanism. */
10544 if (parm_packs == NULL_TREE)
10545 return false;
10546 else if (has_empty_arg)
10547 return true;
10548
10549 bool has_expansion_arg = false;
10550 for (int i = 0 ; i < arg_pack_len; ++i)
10551 {
10552 bool has_non_expansion_arg = false;
10553 for (tree parm_pack = parm_packs;
10554 parm_pack;
10555 parm_pack = TREE_CHAIN (parm_pack))
10556 {
10557 tree arg = TREE_VALUE (parm_pack);
10558
10559 int exp = argument_pack_element_is_expansion_p (arg, i);
10560 if (exp == 2)
10561 /* We can't substitute a pack expansion with extra args into
10562 our pattern. */
10563 return true;
10564 else if (exp)
10565 has_expansion_arg = true;
10566 else
10567 has_non_expansion_arg = true;
10568 }
10569
10570 if (has_expansion_arg && has_non_expansion_arg)
10571 return true;
10572 }
10573 return false;
10574 }
10575
10576 /* [temp.variadic]/6 says that:
10577
10578 The instantiation of a pack expansion [...]
10579 produces a list E1,E2, ..., En, where N is the number of elements
10580 in the pack expansion parameters.
10581
10582 This subroutine of tsubst_pack_expansion produces one of these Ei.
10583
10584 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10585 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10586 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10587 INDEX is the index 'i' of the element Ei to produce. ARGS,
10588 COMPLAIN, and IN_DECL are the same parameters as for the
10589 tsubst_pack_expansion function.
10590
10591 The function returns the resulting Ei upon successful completion,
10592 or error_mark_node.
10593
10594 Note that this function possibly modifies the ARGS parameter, so
10595 it's the responsibility of the caller to restore it. */
10596
10597 static tree
10598 gen_elem_of_pack_expansion_instantiation (tree pattern,
10599 tree parm_packs,
10600 unsigned index,
10601 tree args /* This parm gets
10602 modified. */,
10603 tsubst_flags_t complain,
10604 tree in_decl)
10605 {
10606 tree t;
10607 bool ith_elem_is_expansion = false;
10608
10609 /* For each parameter pack, change the substitution of the parameter
10610 pack to the ith argument in its argument pack, then expand the
10611 pattern. */
10612 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10613 {
10614 tree parm = TREE_PURPOSE (pack);
10615 tree arg_pack = TREE_VALUE (pack);
10616 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10617
10618 ith_elem_is_expansion |=
10619 argument_pack_element_is_expansion_p (arg_pack, index);
10620
10621 /* Select the Ith argument from the pack. */
10622 if (TREE_CODE (parm) == PARM_DECL
10623 || TREE_CODE (parm) == FIELD_DECL)
10624 {
10625 if (index == 0)
10626 {
10627 aps = make_argument_pack_select (arg_pack, index);
10628 if (!mark_used (parm, complain) && !(complain & tf_error))
10629 return error_mark_node;
10630 register_local_specialization (aps, parm);
10631 }
10632 else
10633 aps = retrieve_local_specialization (parm);
10634 }
10635 else
10636 {
10637 int idx, level;
10638 template_parm_level_and_index (parm, &level, &idx);
10639
10640 if (index == 0)
10641 {
10642 aps = make_argument_pack_select (arg_pack, index);
10643 /* Update the corresponding argument. */
10644 TMPL_ARG (args, level, idx) = aps;
10645 }
10646 else
10647 /* Re-use the ARGUMENT_PACK_SELECT. */
10648 aps = TMPL_ARG (args, level, idx);
10649 }
10650 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10651 }
10652
10653 /* Substitute into the PATTERN with the (possibly altered)
10654 arguments. */
10655 if (pattern == in_decl)
10656 /* Expanding a fixed parameter pack from
10657 coerce_template_parameter_pack. */
10658 t = tsubst_decl (pattern, args, complain);
10659 else if (pattern == error_mark_node)
10660 t = error_mark_node;
10661 else if (constraint_p (pattern))
10662 {
10663 if (processing_template_decl)
10664 t = tsubst_constraint (pattern, args, complain, in_decl);
10665 else
10666 t = (constraints_satisfied_p (pattern, args)
10667 ? boolean_true_node : boolean_false_node);
10668 }
10669 else if (!TYPE_P (pattern))
10670 t = tsubst_expr (pattern, args, complain, in_decl,
10671 /*integral_constant_expression_p=*/false);
10672 else
10673 t = tsubst (pattern, args, complain, in_decl);
10674
10675 /* If the Ith argument pack element is a pack expansion, then
10676 the Ith element resulting from the substituting is going to
10677 be a pack expansion as well. */
10678 if (ith_elem_is_expansion)
10679 t = make_pack_expansion (t);
10680
10681 return t;
10682 }
10683
10684 /* When the unexpanded parameter pack in a fold expression expands to an empty
10685 sequence, the value of the expression is as follows; the program is
10686 ill-formed if the operator is not listed in this table.
10687
10688 && true
10689 || false
10690 , void() */
10691
10692 tree
10693 expand_empty_fold (tree t, tsubst_flags_t complain)
10694 {
10695 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
10696 if (!FOLD_EXPR_MODIFY_P (t))
10697 switch (code)
10698 {
10699 case TRUTH_ANDIF_EXPR:
10700 return boolean_true_node;
10701 case TRUTH_ORIF_EXPR:
10702 return boolean_false_node;
10703 case COMPOUND_EXPR:
10704 return void_node;
10705 default:
10706 break;
10707 }
10708
10709 if (complain & tf_error)
10710 error_at (location_of (t),
10711 "fold of empty expansion over %O", code);
10712 return error_mark_node;
10713 }
10714
10715 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10716 form an expression that combines the two terms using the
10717 operator of T. */
10718
10719 static tree
10720 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
10721 {
10722 tree op = FOLD_EXPR_OP (t);
10723 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
10724
10725 // Handle compound assignment operators.
10726 if (FOLD_EXPR_MODIFY_P (t))
10727 return build_x_modify_expr (input_location, left, code, right, complain);
10728
10729 switch (code)
10730 {
10731 case COMPOUND_EXPR:
10732 return build_x_compound_expr (input_location, left, right, complain);
10733 case DOTSTAR_EXPR:
10734 return build_m_component_ref (left, right, complain);
10735 default:
10736 return build_x_binary_op (input_location, code,
10737 left, TREE_CODE (left),
10738 right, TREE_CODE (right),
10739 /*overload=*/NULL,
10740 complain);
10741 }
10742 }
10743
10744 /* Substitute ARGS into the pack of a fold expression T. */
10745
10746 static inline tree
10747 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10748 {
10749 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
10750 }
10751
10752 /* Substitute ARGS into the pack of a fold expression T. */
10753
10754 static inline tree
10755 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10756 {
10757 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
10758 }
10759
10760 /* Expand a PACK of arguments into a grouped as left fold.
10761 Given a pack containing elements A0, A1, ..., An and an
10762 operator @, this builds the expression:
10763
10764 ((A0 @ A1) @ A2) ... @ An
10765
10766 Note that PACK must not be empty.
10767
10768 The operator is defined by the original fold expression T. */
10769
10770 static tree
10771 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
10772 {
10773 tree left = TREE_VEC_ELT (pack, 0);
10774 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
10775 {
10776 tree right = TREE_VEC_ELT (pack, i);
10777 left = fold_expression (t, left, right, complain);
10778 }
10779 return left;
10780 }
10781
10782 /* Substitute into a unary left fold expression. */
10783
10784 static tree
10785 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
10786 tree in_decl)
10787 {
10788 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10789 if (pack == error_mark_node)
10790 return error_mark_node;
10791 if (PACK_EXPANSION_P (pack))
10792 {
10793 tree r = copy_node (t);
10794 FOLD_EXPR_PACK (r) = pack;
10795 return r;
10796 }
10797 if (TREE_VEC_LENGTH (pack) == 0)
10798 return expand_empty_fold (t, complain);
10799 else
10800 return expand_left_fold (t, pack, complain);
10801 }
10802
10803 /* Substitute into a binary left fold expression.
10804
10805 Do ths by building a single (non-empty) vector of argumnts and
10806 building the expression from those elements. */
10807
10808 static tree
10809 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
10810 tree in_decl)
10811 {
10812 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10813 if (pack == error_mark_node)
10814 return error_mark_node;
10815 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10816 if (init == error_mark_node)
10817 return error_mark_node;
10818
10819 if (PACK_EXPANSION_P (pack))
10820 {
10821 tree r = copy_node (t);
10822 FOLD_EXPR_PACK (r) = pack;
10823 FOLD_EXPR_INIT (r) = init;
10824 return r;
10825 }
10826
10827 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
10828 TREE_VEC_ELT (vec, 0) = init;
10829 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
10830 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
10831
10832 return expand_left_fold (t, vec, complain);
10833 }
10834
10835 /* Expand a PACK of arguments into a grouped as right fold.
10836 Given a pack containing elementns A0, A1, ..., and an
10837 operator @, this builds the expression:
10838
10839 A0@ ... (An-2 @ (An-1 @ An))
10840
10841 Note that PACK must not be empty.
10842
10843 The operator is defined by the original fold expression T. */
10844
10845 tree
10846 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
10847 {
10848 // Build the expression.
10849 int n = TREE_VEC_LENGTH (pack);
10850 tree right = TREE_VEC_ELT (pack, n - 1);
10851 for (--n; n != 0; --n)
10852 {
10853 tree left = TREE_VEC_ELT (pack, n - 1);
10854 right = fold_expression (t, left, right, complain);
10855 }
10856 return right;
10857 }
10858
10859 /* Substitute into a unary right fold expression. */
10860
10861 static tree
10862 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
10863 tree in_decl)
10864 {
10865 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10866 if (pack == error_mark_node)
10867 return error_mark_node;
10868 if (PACK_EXPANSION_P (pack))
10869 {
10870 tree r = copy_node (t);
10871 FOLD_EXPR_PACK (r) = pack;
10872 return r;
10873 }
10874 if (TREE_VEC_LENGTH (pack) == 0)
10875 return expand_empty_fold (t, complain);
10876 else
10877 return expand_right_fold (t, pack, complain);
10878 }
10879
10880 /* Substitute into a binary right fold expression.
10881
10882 Do ths by building a single (non-empty) vector of arguments and
10883 building the expression from those elements. */
10884
10885 static tree
10886 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
10887 tree in_decl)
10888 {
10889 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10890 if (pack == error_mark_node)
10891 return error_mark_node;
10892 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10893 if (init == error_mark_node)
10894 return error_mark_node;
10895
10896 if (PACK_EXPANSION_P (pack))
10897 {
10898 tree r = copy_node (t);
10899 FOLD_EXPR_PACK (r) = pack;
10900 FOLD_EXPR_INIT (r) = init;
10901 return r;
10902 }
10903
10904 int n = TREE_VEC_LENGTH (pack);
10905 tree vec = make_tree_vec (n + 1);
10906 for (int i = 0; i < n; ++i)
10907 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
10908 TREE_VEC_ELT (vec, n) = init;
10909
10910 return expand_right_fold (t, vec, complain);
10911 }
10912
10913
10914 /* Substitute ARGS into T, which is an pack expansion
10915 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10916 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10917 (if only a partial substitution could be performed) or
10918 ERROR_MARK_NODE if there was an error. */
10919 tree
10920 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10921 tree in_decl)
10922 {
10923 tree pattern;
10924 tree pack, packs = NULL_TREE;
10925 bool unsubstituted_packs = false;
10926 int i, len = -1;
10927 tree result;
10928 hash_map<tree, tree> *saved_local_specializations = NULL;
10929 bool need_local_specializations = false;
10930 int levels;
10931
10932 gcc_assert (PACK_EXPANSION_P (t));
10933 pattern = PACK_EXPANSION_PATTERN (t);
10934
10935 /* Add in any args remembered from an earlier partial instantiation. */
10936 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10937
10938 levels = TMPL_ARGS_DEPTH (args);
10939
10940 /* Determine the argument packs that will instantiate the parameter
10941 packs used in the expansion expression. While we're at it,
10942 compute the number of arguments to be expanded and make sure it
10943 is consistent. */
10944 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10945 pack = TREE_CHAIN (pack))
10946 {
10947 tree parm_pack = TREE_VALUE (pack);
10948 tree arg_pack = NULL_TREE;
10949 tree orig_arg = NULL_TREE;
10950 int level = 0;
10951
10952 if (TREE_CODE (parm_pack) == BASES)
10953 {
10954 if (BASES_DIRECT (parm_pack))
10955 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10956 args, complain, in_decl, false));
10957 else
10958 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10959 args, complain, in_decl, false));
10960 }
10961 if (TREE_CODE (parm_pack) == PARM_DECL)
10962 {
10963 /* We know we have correct local_specializations if this
10964 expansion is at function scope, or if we're dealing with a
10965 local parameter in a requires expression; for the latter,
10966 tsubst_requires_expr set it up appropriately. */
10967 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
10968 arg_pack = retrieve_local_specialization (parm_pack);
10969 else
10970 /* We can't rely on local_specializations for a parameter
10971 name used later in a function declaration (such as in a
10972 late-specified return type). Even if it exists, it might
10973 have the wrong value for a recursive call. */
10974 need_local_specializations = true;
10975
10976 if (!arg_pack)
10977 {
10978 /* This parameter pack was used in an unevaluated context. Just
10979 make a dummy decl, since it's only used for its type. */
10980 arg_pack = tsubst_decl (parm_pack, args, complain);
10981 if (arg_pack && DECL_PACK_P (arg_pack))
10982 /* Partial instantiation of the parm_pack, we can't build
10983 up an argument pack yet. */
10984 arg_pack = NULL_TREE;
10985 else
10986 arg_pack = make_fnparm_pack (arg_pack);
10987 }
10988 }
10989 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10990 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10991 else
10992 {
10993 int idx;
10994 template_parm_level_and_index (parm_pack, &level, &idx);
10995
10996 if (level <= levels)
10997 arg_pack = TMPL_ARG (args, level, idx);
10998 }
10999
11000 orig_arg = arg_pack;
11001 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11002 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11003
11004 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11005 /* This can only happen if we forget to expand an argument
11006 pack somewhere else. Just return an error, silently. */
11007 {
11008 result = make_tree_vec (1);
11009 TREE_VEC_ELT (result, 0) = error_mark_node;
11010 return result;
11011 }
11012
11013 if (arg_pack)
11014 {
11015 int my_len =
11016 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11017
11018 /* Don't bother trying to do a partial substitution with
11019 incomplete packs; we'll try again after deduction. */
11020 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11021 return t;
11022
11023 if (len < 0)
11024 len = my_len;
11025 else if (len != my_len)
11026 {
11027 if (!(complain & tf_error))
11028 /* Fail quietly. */;
11029 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11030 error ("mismatched argument pack lengths while expanding "
11031 "%<%T%>",
11032 pattern);
11033 else
11034 error ("mismatched argument pack lengths while expanding "
11035 "%<%E%>",
11036 pattern);
11037 return error_mark_node;
11038 }
11039
11040 /* Keep track of the parameter packs and their corresponding
11041 argument packs. */
11042 packs = tree_cons (parm_pack, arg_pack, packs);
11043 TREE_TYPE (packs) = orig_arg;
11044 }
11045 else
11046 {
11047 /* We can't substitute for this parameter pack. We use a flag as
11048 well as the missing_level counter because function parameter
11049 packs don't have a level. */
11050 gcc_assert (processing_template_decl);
11051 unsubstituted_packs = true;
11052 }
11053 }
11054
11055 /* If the expansion is just T..., return the matching argument pack, unless
11056 we need to call convert_from_reference on all the elements. This is an
11057 important optimization; see c++/68422. */
11058 if (!unsubstituted_packs
11059 && TREE_PURPOSE (packs) == pattern)
11060 {
11061 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11062 /* Types need no adjustment, nor does sizeof..., and if we still have
11063 some pack expansion args we won't do anything yet. */
11064 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11065 || PACK_EXPANSION_SIZEOF_P (t)
11066 || pack_expansion_args_count (args))
11067 return args;
11068 /* Also optimize expression pack expansions if we can tell that the
11069 elements won't have reference type. */
11070 tree type = TREE_TYPE (pattern);
11071 if (type && TREE_CODE (type) != REFERENCE_TYPE
11072 && !PACK_EXPANSION_P (type)
11073 && !WILDCARD_TYPE_P (type))
11074 return args;
11075 /* Otherwise use the normal path so we get convert_from_reference. */
11076 }
11077
11078 /* We cannot expand this expansion expression, because we don't have
11079 all of the argument packs we need. */
11080 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11081 {
11082 /* We got some full packs, but we can't substitute them in until we
11083 have values for all the packs. So remember these until then. */
11084
11085 t = make_pack_expansion (pattern);
11086 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11087 return t;
11088 }
11089 else if (unsubstituted_packs)
11090 {
11091 /* There were no real arguments, we're just replacing a parameter
11092 pack with another version of itself. Substitute into the
11093 pattern and return a PACK_EXPANSION_*. The caller will need to
11094 deal with that. */
11095 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11096 t = tsubst_expr (pattern, args, complain, in_decl,
11097 /*integral_constant_expression_p=*/false);
11098 else
11099 t = tsubst (pattern, args, complain, in_decl);
11100 t = make_pack_expansion (t);
11101 return t;
11102 }
11103
11104 gcc_assert (len >= 0);
11105
11106 if (need_local_specializations)
11107 {
11108 /* We're in a late-specified return type, so create our own local
11109 specializations map; the current map is either NULL or (in the
11110 case of recursive unification) might have bindings that we don't
11111 want to use or alter. */
11112 saved_local_specializations = local_specializations;
11113 local_specializations = new hash_map<tree, tree>;
11114 }
11115
11116 /* For each argument in each argument pack, substitute into the
11117 pattern. */
11118 result = make_tree_vec (len);
11119 tree elem_args = copy_template_args (args);
11120 for (i = 0; i < len; ++i)
11121 {
11122 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11123 i,
11124 elem_args, complain,
11125 in_decl);
11126 TREE_VEC_ELT (result, i) = t;
11127 if (t == error_mark_node)
11128 {
11129 result = error_mark_node;
11130 break;
11131 }
11132 }
11133
11134 /* Update ARGS to restore the substitution from parameter packs to
11135 their argument packs. */
11136 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11137 {
11138 tree parm = TREE_PURPOSE (pack);
11139
11140 if (TREE_CODE (parm) == PARM_DECL
11141 || TREE_CODE (parm) == FIELD_DECL)
11142 register_local_specialization (TREE_TYPE (pack), parm);
11143 else
11144 {
11145 int idx, level;
11146
11147 if (TREE_VALUE (pack) == NULL_TREE)
11148 continue;
11149
11150 template_parm_level_and_index (parm, &level, &idx);
11151
11152 /* Update the corresponding argument. */
11153 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11154 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11155 TREE_TYPE (pack);
11156 else
11157 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11158 }
11159 }
11160
11161 if (need_local_specializations)
11162 {
11163 delete local_specializations;
11164 local_specializations = saved_local_specializations;
11165 }
11166
11167 /* If the dependent pack arguments were such that we end up with only a
11168 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11169 if (len == 1 && TREE_CODE (result) == TREE_VEC
11170 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11171 return TREE_VEC_ELT (result, 0);
11172
11173 return result;
11174 }
11175
11176 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11177 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11178 parameter packs; all parms generated from a function parameter pack will
11179 have the same DECL_PARM_INDEX. */
11180
11181 tree
11182 get_pattern_parm (tree parm, tree tmpl)
11183 {
11184 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11185 tree patparm;
11186
11187 if (DECL_ARTIFICIAL (parm))
11188 {
11189 for (patparm = DECL_ARGUMENTS (pattern);
11190 patparm; patparm = DECL_CHAIN (patparm))
11191 if (DECL_ARTIFICIAL (patparm)
11192 && DECL_NAME (parm) == DECL_NAME (patparm))
11193 break;
11194 }
11195 else
11196 {
11197 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11198 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11199 gcc_assert (DECL_PARM_INDEX (patparm)
11200 == DECL_PARM_INDEX (parm));
11201 }
11202
11203 return patparm;
11204 }
11205
11206 /* Make an argument pack out of the TREE_VEC VEC. */
11207
11208 static tree
11209 make_argument_pack (tree vec)
11210 {
11211 tree pack;
11212 tree elt = TREE_VEC_ELT (vec, 0);
11213 if (TYPE_P (elt))
11214 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11215 else
11216 {
11217 pack = make_node (NONTYPE_ARGUMENT_PACK);
11218 TREE_TYPE (pack) = TREE_TYPE (elt);
11219 TREE_CONSTANT (pack) = 1;
11220 }
11221 SET_ARGUMENT_PACK_ARGS (pack, vec);
11222 return pack;
11223 }
11224
11225 /* Return an exact copy of template args T that can be modified
11226 independently. */
11227
11228 static tree
11229 copy_template_args (tree t)
11230 {
11231 if (t == error_mark_node)
11232 return t;
11233
11234 int len = TREE_VEC_LENGTH (t);
11235 tree new_vec = make_tree_vec (len);
11236
11237 for (int i = 0; i < len; ++i)
11238 {
11239 tree elt = TREE_VEC_ELT (t, i);
11240 if (elt && TREE_CODE (elt) == TREE_VEC)
11241 elt = copy_template_args (elt);
11242 TREE_VEC_ELT (new_vec, i) = elt;
11243 }
11244
11245 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11246 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11247
11248 return new_vec;
11249 }
11250
11251 /* Substitute ARGS into the vector or list of template arguments T. */
11252
11253 static tree
11254 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11255 {
11256 tree orig_t = t;
11257 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11258 tree *elts;
11259
11260 if (t == error_mark_node)
11261 return error_mark_node;
11262
11263 len = TREE_VEC_LENGTH (t);
11264 elts = XALLOCAVEC (tree, len);
11265
11266 for (i = 0; i < len; i++)
11267 {
11268 tree orig_arg = TREE_VEC_ELT (t, i);
11269 tree new_arg;
11270
11271 if (TREE_CODE (orig_arg) == TREE_VEC)
11272 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11273 else if (PACK_EXPANSION_P (orig_arg))
11274 {
11275 /* Substitute into an expansion expression. */
11276 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11277
11278 if (TREE_CODE (new_arg) == TREE_VEC)
11279 /* Add to the expanded length adjustment the number of
11280 expanded arguments. We subtract one from this
11281 measurement, because the argument pack expression
11282 itself is already counted as 1 in
11283 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11284 the argument pack is empty. */
11285 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11286 }
11287 else if (ARGUMENT_PACK_P (orig_arg))
11288 {
11289 /* Substitute into each of the arguments. */
11290 new_arg = TYPE_P (orig_arg)
11291 ? cxx_make_type (TREE_CODE (orig_arg))
11292 : make_node (TREE_CODE (orig_arg));
11293
11294 SET_ARGUMENT_PACK_ARGS (
11295 new_arg,
11296 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11297 args, complain, in_decl));
11298
11299 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11300 new_arg = error_mark_node;
11301
11302 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11303 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11304 complain, in_decl);
11305 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11306
11307 if (TREE_TYPE (new_arg) == error_mark_node)
11308 new_arg = error_mark_node;
11309 }
11310 }
11311 else
11312 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11313
11314 if (new_arg == error_mark_node)
11315 return error_mark_node;
11316
11317 elts[i] = new_arg;
11318 if (new_arg != orig_arg)
11319 need_new = 1;
11320 }
11321
11322 if (!need_new)
11323 return t;
11324
11325 /* Make space for the expanded arguments coming from template
11326 argument packs. */
11327 t = make_tree_vec (len + expanded_len_adjust);
11328 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11329 arguments for a member template.
11330 In that case each TREE_VEC in ORIG_T represents a level of template
11331 arguments, and ORIG_T won't carry any non defaulted argument count.
11332 It will rather be the nested TREE_VECs that will carry one.
11333 In other words, ORIG_T carries a non defaulted argument count only
11334 if it doesn't contain any nested TREE_VEC. */
11335 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11336 {
11337 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11338 count += expanded_len_adjust;
11339 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11340 }
11341 for (i = 0, out = 0; i < len; i++)
11342 {
11343 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11344 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11345 && TREE_CODE (elts[i]) == TREE_VEC)
11346 {
11347 int idx;
11348
11349 /* Now expand the template argument pack "in place". */
11350 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11351 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11352 }
11353 else
11354 {
11355 TREE_VEC_ELT (t, out) = elts[i];
11356 out++;
11357 }
11358 }
11359
11360 return t;
11361 }
11362
11363 /* Return the result of substituting ARGS into the template parameters
11364 given by PARMS. If there are m levels of ARGS and m + n levels of
11365 PARMS, then the result will contain n levels of PARMS. For
11366 example, if PARMS is `template <class T> template <class U>
11367 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11368 result will be `template <int*, double, class V>'. */
11369
11370 static tree
11371 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11372 {
11373 tree r = NULL_TREE;
11374 tree* new_parms;
11375
11376 /* When substituting into a template, we must set
11377 PROCESSING_TEMPLATE_DECL as the template parameters may be
11378 dependent if they are based on one-another, and the dependency
11379 predicates are short-circuit outside of templates. */
11380 ++processing_template_decl;
11381
11382 for (new_parms = &r;
11383 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11384 new_parms = &(TREE_CHAIN (*new_parms)),
11385 parms = TREE_CHAIN (parms))
11386 {
11387 tree new_vec =
11388 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
11389 int i;
11390
11391 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11392 {
11393 tree tuple;
11394
11395 if (parms == error_mark_node)
11396 continue;
11397
11398 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
11399
11400 if (tuple == error_mark_node)
11401 continue;
11402
11403 TREE_VEC_ELT (new_vec, i) =
11404 tsubst_template_parm (tuple, args, complain);
11405 }
11406
11407 *new_parms =
11408 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11409 - TMPL_ARGS_DEPTH (args)),
11410 new_vec, NULL_TREE);
11411 }
11412
11413 --processing_template_decl;
11414
11415 return r;
11416 }
11417
11418 /* Return the result of substituting ARGS into one template parameter
11419 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11420 parameter and which TREE_PURPOSE is the default argument of the
11421 template parameter. */
11422
11423 static tree
11424 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11425 {
11426 tree default_value, parm_decl;
11427
11428 if (args == NULL_TREE
11429 || t == NULL_TREE
11430 || t == error_mark_node)
11431 return t;
11432
11433 gcc_assert (TREE_CODE (t) == TREE_LIST);
11434
11435 default_value = TREE_PURPOSE (t);
11436 parm_decl = TREE_VALUE (t);
11437
11438 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11439 if (TREE_CODE (parm_decl) == PARM_DECL
11440 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11441 parm_decl = error_mark_node;
11442 default_value = tsubst_template_arg (default_value, args,
11443 complain, NULL_TREE);
11444
11445 return build_tree_list (default_value, parm_decl);
11446 }
11447
11448 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11449 type T. If T is not an aggregate or enumeration type, it is
11450 handled as if by tsubst. IN_DECL is as for tsubst. If
11451 ENTERING_SCOPE is nonzero, T is the context for a template which
11452 we are presently tsubst'ing. Return the substituted value. */
11453
11454 static tree
11455 tsubst_aggr_type (tree t,
11456 tree args,
11457 tsubst_flags_t complain,
11458 tree in_decl,
11459 int entering_scope)
11460 {
11461 if (t == NULL_TREE)
11462 return NULL_TREE;
11463
11464 switch (TREE_CODE (t))
11465 {
11466 case RECORD_TYPE:
11467 if (TYPE_PTRMEMFUNC_P (t))
11468 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11469
11470 /* Fall through. */
11471 case ENUMERAL_TYPE:
11472 case UNION_TYPE:
11473 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11474 {
11475 tree argvec;
11476 tree context;
11477 tree r;
11478 int saved_unevaluated_operand;
11479 int saved_inhibit_evaluation_warnings;
11480
11481 /* In "sizeof(X<I>)" we need to evaluate "I". */
11482 saved_unevaluated_operand = cp_unevaluated_operand;
11483 cp_unevaluated_operand = 0;
11484 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11485 c_inhibit_evaluation_warnings = 0;
11486
11487 /* First, determine the context for the type we are looking
11488 up. */
11489 context = TYPE_CONTEXT (t);
11490 if (context && TYPE_P (context))
11491 {
11492 context = tsubst_aggr_type (context, args, complain,
11493 in_decl, /*entering_scope=*/1);
11494 /* If context is a nested class inside a class template,
11495 it may still need to be instantiated (c++/33959). */
11496 context = complete_type (context);
11497 }
11498
11499 /* Then, figure out what arguments are appropriate for the
11500 type we are trying to find. For example, given:
11501
11502 template <class T> struct S;
11503 template <class T, class U> void f(T, U) { S<U> su; }
11504
11505 and supposing that we are instantiating f<int, double>,
11506 then our ARGS will be {int, double}, but, when looking up
11507 S we only want {double}. */
11508 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11509 complain, in_decl);
11510 if (argvec == error_mark_node)
11511 r = error_mark_node;
11512 else
11513 {
11514 r = lookup_template_class (t, argvec, in_decl, context,
11515 entering_scope, complain);
11516 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11517 }
11518
11519 cp_unevaluated_operand = saved_unevaluated_operand;
11520 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11521
11522 return r;
11523 }
11524 else
11525 /* This is not a template type, so there's nothing to do. */
11526 return t;
11527
11528 default:
11529 return tsubst (t, args, complain, in_decl);
11530 }
11531 }
11532
11533 /* Substitute into the default argument ARG (a default argument for
11534 FN), which has the indicated TYPE. */
11535
11536 tree
11537 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11538 {
11539 tree saved_class_ptr = NULL_TREE;
11540 tree saved_class_ref = NULL_TREE;
11541 int errs = errorcount + sorrycount;
11542
11543 /* This can happen in invalid code. */
11544 if (TREE_CODE (arg) == DEFAULT_ARG)
11545 return arg;
11546
11547 /* This default argument came from a template. Instantiate the
11548 default argument here, not in tsubst. In the case of
11549 something like:
11550
11551 template <class T>
11552 struct S {
11553 static T t();
11554 void f(T = t());
11555 };
11556
11557 we must be careful to do name lookup in the scope of S<T>,
11558 rather than in the current class. */
11559 push_access_scope (fn);
11560 /* The "this" pointer is not valid in a default argument. */
11561 if (cfun)
11562 {
11563 saved_class_ptr = current_class_ptr;
11564 cp_function_chain->x_current_class_ptr = NULL_TREE;
11565 saved_class_ref = current_class_ref;
11566 cp_function_chain->x_current_class_ref = NULL_TREE;
11567 }
11568
11569 push_deferring_access_checks(dk_no_deferred);
11570 /* The default argument expression may cause implicitly defined
11571 member functions to be synthesized, which will result in garbage
11572 collection. We must treat this situation as if we were within
11573 the body of function so as to avoid collecting live data on the
11574 stack. */
11575 ++function_depth;
11576 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11577 complain, NULL_TREE,
11578 /*integral_constant_expression_p=*/false);
11579 --function_depth;
11580 pop_deferring_access_checks();
11581
11582 /* Restore the "this" pointer. */
11583 if (cfun)
11584 {
11585 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11586 cp_function_chain->x_current_class_ref = saved_class_ref;
11587 }
11588
11589 if (errorcount+sorrycount > errs
11590 && (complain & tf_warning_or_error))
11591 inform (input_location,
11592 " when instantiating default argument for call to %D", fn);
11593
11594 /* Make sure the default argument is reasonable. */
11595 arg = check_default_argument (type, arg, complain);
11596
11597 pop_access_scope (fn);
11598
11599 return arg;
11600 }
11601
11602 /* Substitute into all the default arguments for FN. */
11603
11604 static void
11605 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11606 {
11607 tree arg;
11608 tree tmpl_args;
11609
11610 tmpl_args = DECL_TI_ARGS (fn);
11611
11612 /* If this function is not yet instantiated, we certainly don't need
11613 its default arguments. */
11614 if (uses_template_parms (tmpl_args))
11615 return;
11616 /* Don't do this again for clones. */
11617 if (DECL_CLONED_FUNCTION_P (fn))
11618 return;
11619
11620 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11621 arg;
11622 arg = TREE_CHAIN (arg))
11623 if (TREE_PURPOSE (arg))
11624 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11625 TREE_VALUE (arg),
11626 TREE_PURPOSE (arg),
11627 complain);
11628 }
11629
11630 /* Substitute the ARGS into the T, which is a _DECL. Return the
11631 result of the substitution. Issue error and warning messages under
11632 control of COMPLAIN. */
11633
11634 static tree
11635 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11636 {
11637 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11638 location_t saved_loc;
11639 tree r = NULL_TREE;
11640 tree in_decl = t;
11641 hashval_t hash = 0;
11642
11643 /* Set the filename and linenumber to improve error-reporting. */
11644 saved_loc = input_location;
11645 input_location = DECL_SOURCE_LOCATION (t);
11646
11647 switch (TREE_CODE (t))
11648 {
11649 case TEMPLATE_DECL:
11650 {
11651 /* We can get here when processing a member function template,
11652 member class template, or template template parameter. */
11653 tree decl = DECL_TEMPLATE_RESULT (t);
11654 tree spec;
11655 tree tmpl_args;
11656 tree full_args;
11657
11658 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11659 {
11660 /* Template template parameter is treated here. */
11661 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11662 if (new_type == error_mark_node)
11663 r = error_mark_node;
11664 /* If we get a real template back, return it. This can happen in
11665 the context of most_specialized_partial_spec. */
11666 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11667 r = new_type;
11668 else
11669 /* The new TEMPLATE_DECL was built in
11670 reduce_template_parm_level. */
11671 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
11672 break;
11673 }
11674
11675 /* We might already have an instance of this template.
11676 The ARGS are for the surrounding class type, so the
11677 full args contain the tsubst'd args for the context,
11678 plus the innermost args from the template decl. */
11679 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11680 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11681 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11682 /* Because this is a template, the arguments will still be
11683 dependent, even after substitution. If
11684 PROCESSING_TEMPLATE_DECL is not set, the dependency
11685 predicates will short-circuit. */
11686 ++processing_template_decl;
11687 full_args = tsubst_template_args (tmpl_args, args,
11688 complain, in_decl);
11689 --processing_template_decl;
11690 if (full_args == error_mark_node)
11691 RETURN (error_mark_node);
11692
11693 /* If this is a default template template argument,
11694 tsubst might not have changed anything. */
11695 if (full_args == tmpl_args)
11696 RETURN (t);
11697
11698 hash = hash_tmpl_and_args (t, full_args);
11699 spec = retrieve_specialization (t, full_args, hash);
11700 if (spec != NULL_TREE)
11701 {
11702 r = spec;
11703 break;
11704 }
11705
11706 /* Make a new template decl. It will be similar to the
11707 original, but will record the current template arguments.
11708 We also create a new function declaration, which is just
11709 like the old one, but points to this new template, rather
11710 than the old one. */
11711 r = copy_decl (t);
11712 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11713 DECL_CHAIN (r) = NULL_TREE;
11714
11715 // Build new template info linking to the original template decl.
11716 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11717
11718 if (TREE_CODE (decl) == TYPE_DECL
11719 && !TYPE_DECL_ALIAS_P (decl))
11720 {
11721 tree new_type;
11722 ++processing_template_decl;
11723 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11724 --processing_template_decl;
11725 if (new_type == error_mark_node)
11726 RETURN (error_mark_node);
11727
11728 TREE_TYPE (r) = new_type;
11729 /* For a partial specialization, we need to keep pointing to
11730 the primary template. */
11731 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11732 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11733 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11734 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11735 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11736 }
11737 else
11738 {
11739 tree new_decl;
11740 ++processing_template_decl;
11741 new_decl = tsubst (decl, args, complain, in_decl);
11742 --processing_template_decl;
11743 if (new_decl == error_mark_node)
11744 RETURN (error_mark_node);
11745
11746 DECL_TEMPLATE_RESULT (r) = new_decl;
11747 DECL_TI_TEMPLATE (new_decl) = r;
11748 TREE_TYPE (r) = TREE_TYPE (new_decl);
11749 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11750 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11751 }
11752
11753 SET_DECL_IMPLICIT_INSTANTIATION (r);
11754 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11755 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11756
11757 /* The template parameters for this new template are all the
11758 template parameters for the old template, except the
11759 outermost level of parameters. */
11760 DECL_TEMPLATE_PARMS (r)
11761 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11762 complain);
11763
11764 if (PRIMARY_TEMPLATE_P (t))
11765 DECL_PRIMARY_TEMPLATE (r) = r;
11766
11767 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
11768 /* Record this non-type partial instantiation. */
11769 register_specialization (r, t,
11770 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11771 false, hash);
11772 }
11773 break;
11774
11775 case FUNCTION_DECL:
11776 {
11777 tree ctx;
11778 tree argvec = NULL_TREE;
11779 tree *friends;
11780 tree gen_tmpl;
11781 tree type;
11782 int member;
11783 int args_depth;
11784 int parms_depth;
11785
11786 /* Nobody should be tsubst'ing into non-template functions. */
11787 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11788
11789 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11790 {
11791 tree spec;
11792
11793 /* If T is not dependent, just return it. */
11794 if (!uses_template_parms (DECL_TI_ARGS (t)))
11795 RETURN (t);
11796
11797 /* Calculate the most general template of which R is a
11798 specialization, and the complete set of arguments used to
11799 specialize R. */
11800 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11801 argvec = tsubst_template_args (DECL_TI_ARGS
11802 (DECL_TEMPLATE_RESULT
11803 (DECL_TI_TEMPLATE (t))),
11804 args, complain, in_decl);
11805 if (argvec == error_mark_node)
11806 RETURN (error_mark_node);
11807
11808 /* Check to see if we already have this specialization. */
11809 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11810 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11811
11812 if (spec)
11813 {
11814 r = spec;
11815 break;
11816 }
11817
11818 /* We can see more levels of arguments than parameters if
11819 there was a specialization of a member template, like
11820 this:
11821
11822 template <class T> struct S { template <class U> void f(); }
11823 template <> template <class U> void S<int>::f(U);
11824
11825 Here, we'll be substituting into the specialization,
11826 because that's where we can find the code we actually
11827 want to generate, but we'll have enough arguments for
11828 the most general template.
11829
11830 We also deal with the peculiar case:
11831
11832 template <class T> struct S {
11833 template <class U> friend void f();
11834 };
11835 template <class U> void f() {}
11836 template S<int>;
11837 template void f<double>();
11838
11839 Here, the ARGS for the instantiation of will be {int,
11840 double}. But, we only need as many ARGS as there are
11841 levels of template parameters in CODE_PATTERN. We are
11842 careful not to get fooled into reducing the ARGS in
11843 situations like:
11844
11845 template <class T> struct S { template <class U> void f(U); }
11846 template <class T> template <> void S<T>::f(int) {}
11847
11848 which we can spot because the pattern will be a
11849 specialization in this case. */
11850 args_depth = TMPL_ARGS_DEPTH (args);
11851 parms_depth =
11852 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11853 if (args_depth > parms_depth
11854 && !DECL_TEMPLATE_SPECIALIZATION (t))
11855 args = get_innermost_template_args (args, parms_depth);
11856 }
11857 else
11858 {
11859 /* This special case arises when we have something like this:
11860
11861 template <class T> struct S {
11862 friend void f<int>(int, double);
11863 };
11864
11865 Here, the DECL_TI_TEMPLATE for the friend declaration
11866 will be an IDENTIFIER_NODE. We are being called from
11867 tsubst_friend_function, and we want only to create a
11868 new decl (R) with appropriate types so that we can call
11869 determine_specialization. */
11870 gen_tmpl = NULL_TREE;
11871 }
11872
11873 if (DECL_CLASS_SCOPE_P (t))
11874 {
11875 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11876 member = 2;
11877 else
11878 member = 1;
11879 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11880 complain, t, /*entering_scope=*/1);
11881 }
11882 else
11883 {
11884 member = 0;
11885 ctx = DECL_CONTEXT (t);
11886 }
11887 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11888 if (type == error_mark_node)
11889 RETURN (error_mark_node);
11890
11891 /* If we hit excessive deduction depth, the type is bogus even if
11892 it isn't error_mark_node, so don't build a decl. */
11893 if (excessive_deduction_depth)
11894 RETURN (error_mark_node);
11895
11896 /* We do NOT check for matching decls pushed separately at this
11897 point, as they may not represent instantiations of this
11898 template, and in any case are considered separate under the
11899 discrete model. */
11900 r = copy_decl (t);
11901 DECL_USE_TEMPLATE (r) = 0;
11902 TREE_TYPE (r) = type;
11903 /* Clear out the mangled name and RTL for the instantiation. */
11904 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11905 SET_DECL_RTL (r, NULL);
11906 /* Leave DECL_INITIAL set on deleted instantiations. */
11907 if (!DECL_DELETED_FN (r))
11908 DECL_INITIAL (r) = NULL_TREE;
11909 DECL_CONTEXT (r) = ctx;
11910
11911 /* OpenMP UDRs have the only argument a reference to the declared
11912 type. We want to diagnose if the declared type is a reference,
11913 which is invalid, but as references to references are usually
11914 quietly merged, diagnose it here. */
11915 if (DECL_OMP_DECLARE_REDUCTION_P (t))
11916 {
11917 tree argtype
11918 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11919 argtype = tsubst (argtype, args, complain, in_decl);
11920 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11921 error_at (DECL_SOURCE_LOCATION (t),
11922 "reference type %qT in "
11923 "%<#pragma omp declare reduction%>", argtype);
11924 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11925 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11926 argtype);
11927 }
11928
11929 if (member && DECL_CONV_FN_P (r))
11930 /* Type-conversion operator. Reconstruct the name, in
11931 case it's the name of one of the template's parameters. */
11932 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11933
11934 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11935 complain, t);
11936 DECL_RESULT (r) = NULL_TREE;
11937
11938 TREE_STATIC (r) = 0;
11939 TREE_PUBLIC (r) = TREE_PUBLIC (t);
11940 DECL_EXTERNAL (r) = 1;
11941 /* If this is an instantiation of a function with internal
11942 linkage, we already know what object file linkage will be
11943 assigned to the instantiation. */
11944 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
11945 DECL_DEFER_OUTPUT (r) = 0;
11946 DECL_CHAIN (r) = NULL_TREE;
11947 DECL_PENDING_INLINE_INFO (r) = 0;
11948 DECL_PENDING_INLINE_P (r) = 0;
11949 DECL_SAVED_TREE (r) = NULL_TREE;
11950 DECL_STRUCT_FUNCTION (r) = NULL;
11951 TREE_USED (r) = 0;
11952 /* We'll re-clone as appropriate in instantiate_template. */
11953 DECL_CLONED_FUNCTION (r) = NULL_TREE;
11954
11955 /* If we aren't complaining now, return on error before we register
11956 the specialization so that we'll complain eventually. */
11957 if ((complain & tf_error) == 0
11958 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11959 && !grok_op_properties (r, /*complain=*/false))
11960 RETURN (error_mark_node);
11961
11962 /* When instantiating a constrained member, substitute
11963 into the constraints to create a new constraint. */
11964 if (tree ci = get_constraints (t))
11965 if (member)
11966 {
11967 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
11968 set_constraints (r, ci);
11969 }
11970
11971 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11972 this in the special friend case mentioned above where
11973 GEN_TMPL is NULL. */
11974 if (gen_tmpl)
11975 {
11976 DECL_TEMPLATE_INFO (r)
11977 = build_template_info (gen_tmpl, argvec);
11978 SET_DECL_IMPLICIT_INSTANTIATION (r);
11979
11980 tree new_r
11981 = register_specialization (r, gen_tmpl, argvec, false, hash);
11982 if (new_r != r)
11983 /* We instantiated this while substituting into
11984 the type earlier (template/friend54.C). */
11985 RETURN (new_r);
11986
11987 /* We're not supposed to instantiate default arguments
11988 until they are called, for a template. But, for a
11989 declaration like:
11990
11991 template <class T> void f ()
11992 { extern void g(int i = T()); }
11993
11994 we should do the substitution when the template is
11995 instantiated. We handle the member function case in
11996 instantiate_class_template since the default arguments
11997 might refer to other members of the class. */
11998 if (!member
11999 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12000 && !uses_template_parms (argvec))
12001 tsubst_default_arguments (r, complain);
12002 }
12003 else
12004 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12005
12006 /* Copy the list of befriending classes. */
12007 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12008 *friends;
12009 friends = &TREE_CHAIN (*friends))
12010 {
12011 *friends = copy_node (*friends);
12012 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12013 args, complain,
12014 in_decl);
12015 }
12016
12017 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12018 {
12019 maybe_retrofit_in_chrg (r);
12020 if (DECL_CONSTRUCTOR_P (r))
12021 grok_ctor_properties (ctx, r);
12022 if (DECL_INHERITED_CTOR_BASE (r))
12023 deduce_inheriting_ctor (r);
12024 /* If this is an instantiation of a member template, clone it.
12025 If it isn't, that'll be handled by
12026 clone_constructors_and_destructors. */
12027 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12028 clone_function_decl (r, /*update_method_vec_p=*/0);
12029 }
12030 else if ((complain & tf_error) != 0
12031 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12032 && !grok_op_properties (r, /*complain=*/true))
12033 RETURN (error_mark_node);
12034
12035 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12036 SET_DECL_FRIEND_CONTEXT (r,
12037 tsubst (DECL_FRIEND_CONTEXT (t),
12038 args, complain, in_decl));
12039
12040 /* Possibly limit visibility based on template args. */
12041 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12042 if (DECL_VISIBILITY_SPECIFIED (t))
12043 {
12044 DECL_VISIBILITY_SPECIFIED (r) = 0;
12045 DECL_ATTRIBUTES (r)
12046 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12047 }
12048 determine_visibility (r);
12049 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12050 && !processing_template_decl)
12051 defaulted_late_check (r);
12052
12053 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12054 args, complain, in_decl);
12055 }
12056 break;
12057
12058 case PARM_DECL:
12059 {
12060 tree type = NULL_TREE;
12061 int i, len = 1;
12062 tree expanded_types = NULL_TREE;
12063 tree prev_r = NULL_TREE;
12064 tree first_r = NULL_TREE;
12065
12066 if (DECL_PACK_P (t))
12067 {
12068 /* If there is a local specialization that isn't a
12069 parameter pack, it means that we're doing a "simple"
12070 substitution from inside tsubst_pack_expansion. Just
12071 return the local specialization (which will be a single
12072 parm). */
12073 tree spec = retrieve_local_specialization (t);
12074 if (spec
12075 && TREE_CODE (spec) == PARM_DECL
12076 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12077 RETURN (spec);
12078
12079 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12080 the parameters in this function parameter pack. */
12081 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12082 complain, in_decl);
12083 if (TREE_CODE (expanded_types) == TREE_VEC)
12084 {
12085 len = TREE_VEC_LENGTH (expanded_types);
12086
12087 /* Zero-length parameter packs are boring. Just substitute
12088 into the chain. */
12089 if (len == 0)
12090 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12091 TREE_CHAIN (t)));
12092 }
12093 else
12094 {
12095 /* All we did was update the type. Make a note of that. */
12096 type = expanded_types;
12097 expanded_types = NULL_TREE;
12098 }
12099 }
12100
12101 /* Loop through all of the parameters we'll build. When T is
12102 a function parameter pack, LEN is the number of expanded
12103 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12104 r = NULL_TREE;
12105 for (i = 0; i < len; ++i)
12106 {
12107 prev_r = r;
12108 r = copy_node (t);
12109 if (DECL_TEMPLATE_PARM_P (t))
12110 SET_DECL_TEMPLATE_PARM_P (r);
12111
12112 if (expanded_types)
12113 /* We're on the Ith parameter of the function parameter
12114 pack. */
12115 {
12116 /* Get the Ith type. */
12117 type = TREE_VEC_ELT (expanded_types, i);
12118
12119 /* Rename the parameter to include the index. */
12120 DECL_NAME (r)
12121 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12122 }
12123 else if (!type)
12124 /* We're dealing with a normal parameter. */
12125 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12126
12127 type = type_decays_to (type);
12128 TREE_TYPE (r) = type;
12129 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12130
12131 if (DECL_INITIAL (r))
12132 {
12133 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12134 DECL_INITIAL (r) = TREE_TYPE (r);
12135 else
12136 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12137 complain, in_decl);
12138 }
12139
12140 DECL_CONTEXT (r) = NULL_TREE;
12141
12142 if (!DECL_TEMPLATE_PARM_P (r))
12143 DECL_ARG_TYPE (r) = type_passed_as (type);
12144
12145 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12146 args, complain, in_decl);
12147
12148 /* Keep track of the first new parameter we
12149 generate. That's what will be returned to the
12150 caller. */
12151 if (!first_r)
12152 first_r = r;
12153
12154 /* Build a proper chain of parameters when substituting
12155 into a function parameter pack. */
12156 if (prev_r)
12157 DECL_CHAIN (prev_r) = r;
12158 }
12159
12160 /* If cp_unevaluated_operand is set, we're just looking for a
12161 single dummy parameter, so don't keep going. */
12162 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12163 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12164 complain, DECL_CHAIN (t));
12165
12166 /* FIRST_R contains the start of the chain we've built. */
12167 r = first_r;
12168 }
12169 break;
12170
12171 case FIELD_DECL:
12172 {
12173 tree type = NULL_TREE;
12174 tree vec = NULL_TREE;
12175 tree expanded_types = NULL_TREE;
12176 int len = 1;
12177
12178 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12179 {
12180 /* This field is a lambda capture pack. Return a TREE_VEC of
12181 the expanded fields to instantiate_class_template_1 and
12182 store them in the specializations hash table as a
12183 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12184 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12185 complain, in_decl);
12186 if (TREE_CODE (expanded_types) == TREE_VEC)
12187 {
12188 len = TREE_VEC_LENGTH (expanded_types);
12189 vec = make_tree_vec (len);
12190 }
12191 else
12192 {
12193 /* All we did was update the type. Make a note of that. */
12194 type = expanded_types;
12195 expanded_types = NULL_TREE;
12196 }
12197 }
12198
12199 for (int i = 0; i < len; ++i)
12200 {
12201 r = copy_decl (t);
12202 if (expanded_types)
12203 {
12204 type = TREE_VEC_ELT (expanded_types, i);
12205 DECL_NAME (r)
12206 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12207 }
12208 else if (!type)
12209 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12210
12211 if (type == error_mark_node)
12212 RETURN (error_mark_node);
12213 TREE_TYPE (r) = type;
12214 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12215
12216 if (DECL_C_BIT_FIELD (r))
12217 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12218 non-bit-fields DECL_INITIAL is a non-static data member
12219 initializer, which gets deferred instantiation. */
12220 DECL_INITIAL (r)
12221 = tsubst_expr (DECL_INITIAL (t), args,
12222 complain, in_decl,
12223 /*integral_constant_expression_p=*/true);
12224 else if (DECL_INITIAL (t))
12225 {
12226 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12227 NSDMI in perform_member_init. Still set DECL_INITIAL
12228 so that we know there is one. */
12229 DECL_INITIAL (r) = void_node;
12230 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12231 retrofit_lang_decl (r);
12232 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12233 }
12234 /* We don't have to set DECL_CONTEXT here; it is set by
12235 finish_member_declaration. */
12236 DECL_CHAIN (r) = NULL_TREE;
12237
12238 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12239 args, complain, in_decl);
12240
12241 if (vec)
12242 TREE_VEC_ELT (vec, i) = r;
12243 }
12244
12245 if (vec)
12246 {
12247 r = vec;
12248 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12249 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12250 SET_ARGUMENT_PACK_ARGS (pack, vec);
12251 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12252 TREE_TYPE (pack) = tpack;
12253 register_specialization (pack, t, args, false, 0);
12254 }
12255 }
12256 break;
12257
12258 case USING_DECL:
12259 /* We reach here only for member using decls. We also need to check
12260 uses_template_parms because DECL_DEPENDENT_P is not set for a
12261 using-declaration that designates a member of the current
12262 instantiation (c++/53549). */
12263 if (DECL_DEPENDENT_P (t)
12264 || uses_template_parms (USING_DECL_SCOPE (t)))
12265 {
12266 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12267 complain, in_decl);
12268 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12269 r = do_class_using_decl (inst_scope, name);
12270 if (!r)
12271 r = error_mark_node;
12272 else
12273 {
12274 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12275 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12276 }
12277 }
12278 else
12279 {
12280 r = copy_node (t);
12281 DECL_CHAIN (r) = NULL_TREE;
12282 }
12283 break;
12284
12285 case TYPE_DECL:
12286 case VAR_DECL:
12287 {
12288 tree argvec = NULL_TREE;
12289 tree gen_tmpl = NULL_TREE;
12290 tree spec;
12291 tree tmpl = NULL_TREE;
12292 tree ctx;
12293 tree type = NULL_TREE;
12294 bool local_p;
12295
12296 if (TREE_TYPE (t) == error_mark_node)
12297 RETURN (error_mark_node);
12298
12299 if (TREE_CODE (t) == TYPE_DECL
12300 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12301 {
12302 /* If this is the canonical decl, we don't have to
12303 mess with instantiations, and often we can't (for
12304 typename, template type parms and such). Note that
12305 TYPE_NAME is not correct for the above test if
12306 we've copied the type for a typedef. */
12307 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12308 if (type == error_mark_node)
12309 RETURN (error_mark_node);
12310 r = TYPE_NAME (type);
12311 break;
12312 }
12313
12314 /* Check to see if we already have the specialization we
12315 need. */
12316 spec = NULL_TREE;
12317 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12318 {
12319 /* T is a static data member or namespace-scope entity.
12320 We have to substitute into namespace-scope variables
12321 (not just variable templates) because of cases like:
12322
12323 template <class T> void f() { extern T t; }
12324
12325 where the entity referenced is not known until
12326 instantiation time. */
12327 local_p = false;
12328 ctx = DECL_CONTEXT (t);
12329 if (DECL_CLASS_SCOPE_P (t))
12330 {
12331 ctx = tsubst_aggr_type (ctx, args,
12332 complain,
12333 in_decl, /*entering_scope=*/1);
12334 /* If CTX is unchanged, then T is in fact the
12335 specialization we want. That situation occurs when
12336 referencing a static data member within in its own
12337 class. We can use pointer equality, rather than
12338 same_type_p, because DECL_CONTEXT is always
12339 canonical... */
12340 if (ctx == DECL_CONTEXT (t)
12341 /* ... unless T is a member template; in which
12342 case our caller can be willing to create a
12343 specialization of that template represented
12344 by T. */
12345 && !(DECL_TI_TEMPLATE (t)
12346 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12347 spec = t;
12348 }
12349
12350 if (!spec)
12351 {
12352 tmpl = DECL_TI_TEMPLATE (t);
12353 gen_tmpl = most_general_template (tmpl);
12354 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12355 if (argvec != error_mark_node)
12356 argvec = (coerce_innermost_template_parms
12357 (DECL_TEMPLATE_PARMS (gen_tmpl),
12358 argvec, t, complain,
12359 /*all*/true, /*defarg*/true));
12360 if (argvec == error_mark_node)
12361 RETURN (error_mark_node);
12362 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12363 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12364 }
12365 }
12366 else
12367 {
12368 /* A local variable. */
12369 local_p = true;
12370 /* Subsequent calls to pushdecl will fill this in. */
12371 ctx = NULL_TREE;
12372 /* Unless this is a reference to a static variable from an
12373 enclosing function, in which case we need to fill it in now. */
12374 if (TREE_STATIC (t))
12375 {
12376 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12377 if (fn != current_function_decl)
12378 ctx = fn;
12379 }
12380 spec = retrieve_local_specialization (t);
12381 }
12382 /* If we already have the specialization we need, there is
12383 nothing more to do. */
12384 if (spec)
12385 {
12386 r = spec;
12387 break;
12388 }
12389
12390 /* Create a new node for the specialization we need. */
12391 r = copy_decl (t);
12392 if (type == NULL_TREE)
12393 {
12394 if (is_typedef_decl (t))
12395 type = DECL_ORIGINAL_TYPE (t);
12396 else
12397 type = TREE_TYPE (t);
12398 if (VAR_P (t)
12399 && VAR_HAD_UNKNOWN_BOUND (t)
12400 && type != error_mark_node)
12401 type = strip_array_domain (type);
12402 type = tsubst (type, args, complain, in_decl);
12403 }
12404 if (VAR_P (r))
12405 {
12406 /* Even if the original location is out of scope, the
12407 newly substituted one is not. */
12408 DECL_DEAD_FOR_LOCAL (r) = 0;
12409 DECL_INITIALIZED_P (r) = 0;
12410 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12411 if (type == error_mark_node)
12412 RETURN (error_mark_node);
12413 if (TREE_CODE (type) == FUNCTION_TYPE)
12414 {
12415 /* It may seem that this case cannot occur, since:
12416
12417 typedef void f();
12418 void g() { f x; }
12419
12420 declares a function, not a variable. However:
12421
12422 typedef void f();
12423 template <typename T> void g() { T t; }
12424 template void g<f>();
12425
12426 is an attempt to declare a variable with function
12427 type. */
12428 error ("variable %qD has function type",
12429 /* R is not yet sufficiently initialized, so we
12430 just use its name. */
12431 DECL_NAME (r));
12432 RETURN (error_mark_node);
12433 }
12434 type = complete_type (type);
12435 /* Wait until cp_finish_decl to set this again, to handle
12436 circular dependency (template/instantiate6.C). */
12437 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12438 type = check_var_type (DECL_NAME (r), type);
12439
12440 if (DECL_HAS_VALUE_EXPR_P (t))
12441 {
12442 tree ve = DECL_VALUE_EXPR (t);
12443 ve = tsubst_expr (ve, args, complain, in_decl,
12444 /*constant_expression_p=*/false);
12445 if (REFERENCE_REF_P (ve))
12446 {
12447 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12448 ve = TREE_OPERAND (ve, 0);
12449 }
12450 SET_DECL_VALUE_EXPR (r, ve);
12451 }
12452 if (CP_DECL_THREAD_LOCAL_P (r)
12453 && !processing_template_decl)
12454 set_decl_tls_model (r, decl_default_tls_model (r));
12455 }
12456 else if (DECL_SELF_REFERENCE_P (t))
12457 SET_DECL_SELF_REFERENCE_P (r);
12458 TREE_TYPE (r) = type;
12459 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12460 DECL_CONTEXT (r) = ctx;
12461 /* Clear out the mangled name and RTL for the instantiation. */
12462 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12463 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12464 SET_DECL_RTL (r, NULL);
12465 /* The initializer must not be expanded until it is required;
12466 see [temp.inst]. */
12467 DECL_INITIAL (r) = NULL_TREE;
12468 if (VAR_P (r))
12469 DECL_MODE (r) = VOIDmode;
12470 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12471 SET_DECL_RTL (r, NULL);
12472 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12473 if (VAR_P (r))
12474 {
12475 /* Possibly limit visibility based on template args. */
12476 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12477 if (DECL_VISIBILITY_SPECIFIED (t))
12478 {
12479 DECL_VISIBILITY_SPECIFIED (r) = 0;
12480 DECL_ATTRIBUTES (r)
12481 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12482 }
12483 determine_visibility (r);
12484 }
12485
12486 if (!local_p)
12487 {
12488 /* A static data member declaration is always marked
12489 external when it is declared in-class, even if an
12490 initializer is present. We mimic the non-template
12491 processing here. */
12492 DECL_EXTERNAL (r) = 1;
12493 if (DECL_NAMESPACE_SCOPE_P (t))
12494 DECL_NOT_REALLY_EXTERN (r) = 1;
12495
12496 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12497 SET_DECL_IMPLICIT_INSTANTIATION (r);
12498 register_specialization (r, gen_tmpl, argvec, false, hash);
12499 }
12500 else
12501 {
12502 if (DECL_LANG_SPECIFIC (r))
12503 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12504 if (!cp_unevaluated_operand)
12505 register_local_specialization (r, t);
12506 }
12507
12508 DECL_CHAIN (r) = NULL_TREE;
12509
12510 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12511 /*flags=*/0,
12512 args, complain, in_decl);
12513
12514 /* Preserve a typedef that names a type. */
12515 if (is_typedef_decl (r))
12516 {
12517 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12518 set_underlying_type (r);
12519 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12520 /* An alias template specialization can be dependent
12521 even if its underlying type is not. */
12522 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12523 }
12524
12525 layout_decl (r, 0);
12526 }
12527 break;
12528
12529 default:
12530 gcc_unreachable ();
12531 }
12532 #undef RETURN
12533
12534 out:
12535 /* Restore the file and line information. */
12536 input_location = saved_loc;
12537
12538 return r;
12539 }
12540
12541 /* Substitute into the ARG_TYPES of a function type.
12542 If END is a TREE_CHAIN, leave it and any following types
12543 un-substituted. */
12544
12545 static tree
12546 tsubst_arg_types (tree arg_types,
12547 tree args,
12548 tree end,
12549 tsubst_flags_t complain,
12550 tree in_decl)
12551 {
12552 tree remaining_arg_types;
12553 tree type = NULL_TREE;
12554 int i = 1;
12555 tree expanded_args = NULL_TREE;
12556 tree default_arg;
12557
12558 if (!arg_types || arg_types == void_list_node || arg_types == end)
12559 return arg_types;
12560
12561 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12562 args, end, complain, in_decl);
12563 if (remaining_arg_types == error_mark_node)
12564 return error_mark_node;
12565
12566 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12567 {
12568 /* For a pack expansion, perform substitution on the
12569 entire expression. Later on, we'll handle the arguments
12570 one-by-one. */
12571 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12572 args, complain, in_decl);
12573
12574 if (TREE_CODE (expanded_args) == TREE_VEC)
12575 /* So that we'll spin through the parameters, one by one. */
12576 i = TREE_VEC_LENGTH (expanded_args);
12577 else
12578 {
12579 /* We only partially substituted into the parameter
12580 pack. Our type is TYPE_PACK_EXPANSION. */
12581 type = expanded_args;
12582 expanded_args = NULL_TREE;
12583 }
12584 }
12585
12586 while (i > 0) {
12587 --i;
12588
12589 if (expanded_args)
12590 type = TREE_VEC_ELT (expanded_args, i);
12591 else if (!type)
12592 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12593
12594 if (type == error_mark_node)
12595 return error_mark_node;
12596 if (VOID_TYPE_P (type))
12597 {
12598 if (complain & tf_error)
12599 {
12600 error ("invalid parameter type %qT", type);
12601 if (in_decl)
12602 error ("in declaration %q+D", in_decl);
12603 }
12604 return error_mark_node;
12605 }
12606 /* DR 657. */
12607 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12608 return error_mark_node;
12609
12610 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12611 top-level qualifiers as required. */
12612 type = cv_unqualified (type_decays_to (type));
12613
12614 /* We do not substitute into default arguments here. The standard
12615 mandates that they be instantiated only when needed, which is
12616 done in build_over_call. */
12617 default_arg = TREE_PURPOSE (arg_types);
12618
12619 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12620 {
12621 /* We've instantiated a template before its default arguments
12622 have been parsed. This can happen for a nested template
12623 class, and is not an error unless we require the default
12624 argument in a call of this function. */
12625 remaining_arg_types =
12626 tree_cons (default_arg, type, remaining_arg_types);
12627 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12628 }
12629 else
12630 remaining_arg_types =
12631 hash_tree_cons (default_arg, type, remaining_arg_types);
12632 }
12633
12634 return remaining_arg_types;
12635 }
12636
12637 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12638 *not* handle the exception-specification for FNTYPE, because the
12639 initial substitution of explicitly provided template parameters
12640 during argument deduction forbids substitution into the
12641 exception-specification:
12642
12643 [temp.deduct]
12644
12645 All references in the function type of the function template to the
12646 corresponding template parameters are replaced by the specified tem-
12647 plate argument values. If a substitution in a template parameter or
12648 in the function type of the function template results in an invalid
12649 type, type deduction fails. [Note: The equivalent substitution in
12650 exception specifications is done only when the function is instanti-
12651 ated, at which point a program is ill-formed if the substitution
12652 results in an invalid type.] */
12653
12654 static tree
12655 tsubst_function_type (tree t,
12656 tree args,
12657 tsubst_flags_t complain,
12658 tree in_decl)
12659 {
12660 tree return_type;
12661 tree arg_types = NULL_TREE;
12662 tree fntype;
12663
12664 /* The TYPE_CONTEXT is not used for function/method types. */
12665 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12666
12667 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12668 failure. */
12669 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12670
12671 if (late_return_type_p)
12672 {
12673 /* Substitute the argument types. */
12674 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12675 complain, in_decl);
12676 if (arg_types == error_mark_node)
12677 return error_mark_node;
12678
12679 tree save_ccp = current_class_ptr;
12680 tree save_ccr = current_class_ref;
12681 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12682 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12683 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12684 if (do_inject)
12685 {
12686 /* DR 1207: 'this' is in scope in the trailing return type. */
12687 inject_this_parameter (this_type, cp_type_quals (this_type));
12688 }
12689
12690 /* Substitute the return type. */
12691 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12692
12693 if (do_inject)
12694 {
12695 current_class_ptr = save_ccp;
12696 current_class_ref = save_ccr;
12697 }
12698 }
12699 else
12700 /* Substitute the return type. */
12701 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12702
12703 if (return_type == error_mark_node)
12704 return error_mark_node;
12705 /* DR 486 clarifies that creation of a function type with an
12706 invalid return type is a deduction failure. */
12707 if (TREE_CODE (return_type) == ARRAY_TYPE
12708 || TREE_CODE (return_type) == FUNCTION_TYPE)
12709 {
12710 if (complain & tf_error)
12711 {
12712 if (TREE_CODE (return_type) == ARRAY_TYPE)
12713 error ("function returning an array");
12714 else
12715 error ("function returning a function");
12716 }
12717 return error_mark_node;
12718 }
12719 /* And DR 657. */
12720 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12721 return error_mark_node;
12722
12723 if (!late_return_type_p)
12724 {
12725 /* Substitute the argument types. */
12726 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12727 complain, in_decl);
12728 if (arg_types == error_mark_node)
12729 return error_mark_node;
12730 }
12731
12732 /* Construct a new type node and return it. */
12733 if (TREE_CODE (t) == FUNCTION_TYPE)
12734 {
12735 fntype = build_function_type (return_type, arg_types);
12736 fntype = apply_memfn_quals (fntype,
12737 type_memfn_quals (t),
12738 type_memfn_rqual (t));
12739 }
12740 else
12741 {
12742 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12743 /* Don't pick up extra function qualifiers from the basetype. */
12744 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12745 if (! MAYBE_CLASS_TYPE_P (r))
12746 {
12747 /* [temp.deduct]
12748
12749 Type deduction may fail for any of the following
12750 reasons:
12751
12752 -- Attempting to create "pointer to member of T" when T
12753 is not a class type. */
12754 if (complain & tf_error)
12755 error ("creating pointer to member function of non-class type %qT",
12756 r);
12757 return error_mark_node;
12758 }
12759
12760 fntype = build_method_type_directly (r, return_type,
12761 TREE_CHAIN (arg_types));
12762 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12763 }
12764 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12765
12766 if (late_return_type_p)
12767 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12768
12769 return fntype;
12770 }
12771
12772 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12773 ARGS into that specification, and return the substituted
12774 specification. If there is no specification, return NULL_TREE. */
12775
12776 static tree
12777 tsubst_exception_specification (tree fntype,
12778 tree args,
12779 tsubst_flags_t complain,
12780 tree in_decl,
12781 bool defer_ok)
12782 {
12783 tree specs;
12784 tree new_specs;
12785
12786 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12787 new_specs = NULL_TREE;
12788 if (specs && TREE_PURPOSE (specs))
12789 {
12790 /* A noexcept-specifier. */
12791 tree expr = TREE_PURPOSE (specs);
12792 if (TREE_CODE (expr) == INTEGER_CST)
12793 new_specs = expr;
12794 else if (defer_ok)
12795 {
12796 /* Defer instantiation of noexcept-specifiers to avoid
12797 excessive instantiations (c++/49107). */
12798 new_specs = make_node (DEFERRED_NOEXCEPT);
12799 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12800 {
12801 /* We already partially instantiated this member template,
12802 so combine the new args with the old. */
12803 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12804 = DEFERRED_NOEXCEPT_PATTERN (expr);
12805 DEFERRED_NOEXCEPT_ARGS (new_specs)
12806 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12807 }
12808 else
12809 {
12810 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12811 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12812 }
12813 }
12814 else
12815 new_specs = tsubst_copy_and_build
12816 (expr, args, complain, in_decl, /*function_p=*/false,
12817 /*integral_constant_expression_p=*/true);
12818 new_specs = build_noexcept_spec (new_specs, complain);
12819 }
12820 else if (specs)
12821 {
12822 if (! TREE_VALUE (specs))
12823 new_specs = specs;
12824 else
12825 while (specs)
12826 {
12827 tree spec;
12828 int i, len = 1;
12829 tree expanded_specs = NULL_TREE;
12830
12831 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12832 {
12833 /* Expand the pack expansion type. */
12834 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12835 args, complain,
12836 in_decl);
12837
12838 if (expanded_specs == error_mark_node)
12839 return error_mark_node;
12840 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12841 len = TREE_VEC_LENGTH (expanded_specs);
12842 else
12843 {
12844 /* We're substituting into a member template, so
12845 we got a TYPE_PACK_EXPANSION back. Add that
12846 expansion and move on. */
12847 gcc_assert (TREE_CODE (expanded_specs)
12848 == TYPE_PACK_EXPANSION);
12849 new_specs = add_exception_specifier (new_specs,
12850 expanded_specs,
12851 complain);
12852 specs = TREE_CHAIN (specs);
12853 continue;
12854 }
12855 }
12856
12857 for (i = 0; i < len; ++i)
12858 {
12859 if (expanded_specs)
12860 spec = TREE_VEC_ELT (expanded_specs, i);
12861 else
12862 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12863 if (spec == error_mark_node)
12864 return spec;
12865 new_specs = add_exception_specifier (new_specs, spec,
12866 complain);
12867 }
12868
12869 specs = TREE_CHAIN (specs);
12870 }
12871 }
12872 return new_specs;
12873 }
12874
12875 /* Take the tree structure T and replace template parameters used
12876 therein with the argument vector ARGS. IN_DECL is an associated
12877 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12878 Issue error and warning messages under control of COMPLAIN. Note
12879 that we must be relatively non-tolerant of extensions here, in
12880 order to preserve conformance; if we allow substitutions that
12881 should not be allowed, we may allow argument deductions that should
12882 not succeed, and therefore report ambiguous overload situations
12883 where there are none. In theory, we could allow the substitution,
12884 but indicate that it should have failed, and allow our caller to
12885 make sure that the right thing happens, but we don't try to do this
12886 yet.
12887
12888 This function is used for dealing with types, decls and the like;
12889 for expressions, use tsubst_expr or tsubst_copy. */
12890
12891 tree
12892 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12893 {
12894 enum tree_code code;
12895 tree type, r = NULL_TREE;
12896
12897 if (t == NULL_TREE || t == error_mark_node
12898 || t == integer_type_node
12899 || t == void_type_node
12900 || t == char_type_node
12901 || t == unknown_type_node
12902 || TREE_CODE (t) == NAMESPACE_DECL
12903 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12904 return t;
12905
12906 if (DECL_P (t))
12907 return tsubst_decl (t, args, complain);
12908
12909 if (args == NULL_TREE)
12910 return t;
12911
12912 code = TREE_CODE (t);
12913
12914 if (code == IDENTIFIER_NODE)
12915 type = IDENTIFIER_TYPE_VALUE (t);
12916 else
12917 type = TREE_TYPE (t);
12918
12919 gcc_assert (type != unknown_type_node);
12920
12921 /* Reuse typedefs. We need to do this to handle dependent attributes,
12922 such as attribute aligned. */
12923 if (TYPE_P (t)
12924 && typedef_variant_p (t))
12925 {
12926 tree decl = TYPE_NAME (t);
12927
12928 if (alias_template_specialization_p (t))
12929 {
12930 /* DECL represents an alias template and we want to
12931 instantiate it. */
12932 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12933 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12934 r = instantiate_alias_template (tmpl, gen_args, complain);
12935 }
12936 else if (DECL_CLASS_SCOPE_P (decl)
12937 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12938 && uses_template_parms (DECL_CONTEXT (decl)))
12939 {
12940 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12941 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12942 r = retrieve_specialization (tmpl, gen_args, 0);
12943 }
12944 else if (DECL_FUNCTION_SCOPE_P (decl)
12945 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12946 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12947 r = retrieve_local_specialization (decl);
12948 else
12949 /* The typedef is from a non-template context. */
12950 return t;
12951
12952 if (r)
12953 {
12954 r = TREE_TYPE (r);
12955 r = cp_build_qualified_type_real
12956 (r, cp_type_quals (t) | cp_type_quals (r),
12957 complain | tf_ignore_bad_quals);
12958 return r;
12959 }
12960 else
12961 {
12962 /* We don't have an instantiation yet, so drop the typedef. */
12963 int quals = cp_type_quals (t);
12964 t = DECL_ORIGINAL_TYPE (decl);
12965 t = cp_build_qualified_type_real (t, quals,
12966 complain | tf_ignore_bad_quals);
12967 }
12968 }
12969
12970 if (type
12971 && code != TYPENAME_TYPE
12972 && code != TEMPLATE_TYPE_PARM
12973 && code != IDENTIFIER_NODE
12974 && code != FUNCTION_TYPE
12975 && code != METHOD_TYPE)
12976 type = tsubst (type, args, complain, in_decl);
12977 if (type == error_mark_node)
12978 return error_mark_node;
12979
12980 switch (code)
12981 {
12982 case RECORD_TYPE:
12983 case UNION_TYPE:
12984 case ENUMERAL_TYPE:
12985 return tsubst_aggr_type (t, args, complain, in_decl,
12986 /*entering_scope=*/0);
12987
12988 case ERROR_MARK:
12989 case IDENTIFIER_NODE:
12990 case VOID_TYPE:
12991 case REAL_TYPE:
12992 case COMPLEX_TYPE:
12993 case VECTOR_TYPE:
12994 case BOOLEAN_TYPE:
12995 case NULLPTR_TYPE:
12996 case LANG_TYPE:
12997 return t;
12998
12999 case INTEGER_TYPE:
13000 if (t == integer_type_node)
13001 return t;
13002
13003 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13004 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13005 return t;
13006
13007 {
13008 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13009
13010 max = tsubst_expr (omax, args, complain, in_decl,
13011 /*integral_constant_expression_p=*/false);
13012
13013 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13014 needed. */
13015 if (TREE_CODE (max) == NOP_EXPR
13016 && TREE_SIDE_EFFECTS (omax)
13017 && !TREE_TYPE (max))
13018 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13019
13020 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13021 with TREE_SIDE_EFFECTS that indicates this is not an integral
13022 constant expression. */
13023 if (processing_template_decl
13024 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13025 {
13026 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13027 TREE_SIDE_EFFECTS (max) = 1;
13028 }
13029
13030 return compute_array_index_type (NULL_TREE, max, complain);
13031 }
13032
13033 case TEMPLATE_TYPE_PARM:
13034 case TEMPLATE_TEMPLATE_PARM:
13035 case BOUND_TEMPLATE_TEMPLATE_PARM:
13036 case TEMPLATE_PARM_INDEX:
13037 {
13038 int idx;
13039 int level;
13040 int levels;
13041 tree arg = NULL_TREE;
13042
13043 /* Early in template argument deduction substitution, we don't
13044 want to reduce the level of 'auto', or it will be confused
13045 with a normal template parm in subsequent deduction. */
13046 if (is_auto (t) && (complain & tf_partial))
13047 return t;
13048
13049 r = NULL_TREE;
13050
13051 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13052 template_parm_level_and_index (t, &level, &idx);
13053
13054 levels = TMPL_ARGS_DEPTH (args);
13055 if (level <= levels
13056 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13057 {
13058 arg = TMPL_ARG (args, level, idx);
13059
13060 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13061 {
13062 /* See through ARGUMENT_PACK_SELECT arguments. */
13063 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13064 /* If the selected argument is an expansion E, that most
13065 likely means we were called from
13066 gen_elem_of_pack_expansion_instantiation during the
13067 substituting of pack an argument pack (which Ith
13068 element is a pack expansion, where I is
13069 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13070 In this case, the Ith element resulting from this
13071 substituting is going to be a pack expansion, which
13072 pattern is the pattern of E. Let's return the
13073 pattern of E, and
13074 gen_elem_of_pack_expansion_instantiation will
13075 build the resulting pack expansion from it. */
13076 if (PACK_EXPANSION_P (arg))
13077 {
13078 /* Make sure we aren't throwing away arg info. */
13079 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13080 arg = PACK_EXPANSION_PATTERN (arg);
13081 }
13082 }
13083 }
13084
13085 if (arg == error_mark_node)
13086 return error_mark_node;
13087 else if (arg != NULL_TREE)
13088 {
13089 if (ARGUMENT_PACK_P (arg))
13090 /* If ARG is an argument pack, we don't actually want to
13091 perform a substitution here, because substitutions
13092 for argument packs are only done
13093 element-by-element. We can get to this point when
13094 substituting the type of a non-type template
13095 parameter pack, when that type actually contains
13096 template parameter packs from an outer template, e.g.,
13097
13098 template<typename... Types> struct A {
13099 template<Types... Values> struct B { };
13100 }; */
13101 return t;
13102
13103 if (code == TEMPLATE_TYPE_PARM)
13104 {
13105 int quals;
13106 gcc_assert (TYPE_P (arg));
13107
13108 quals = cp_type_quals (arg) | cp_type_quals (t);
13109
13110 return cp_build_qualified_type_real
13111 (arg, quals, complain | tf_ignore_bad_quals);
13112 }
13113 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13114 {
13115 /* We are processing a type constructed from a
13116 template template parameter. */
13117 tree argvec = tsubst (TYPE_TI_ARGS (t),
13118 args, complain, in_decl);
13119 if (argvec == error_mark_node)
13120 return error_mark_node;
13121
13122 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13123 || TREE_CODE (arg) == TEMPLATE_DECL
13124 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13125
13126 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13127 /* Consider this code:
13128
13129 template <template <class> class Template>
13130 struct Internal {
13131 template <class Arg> using Bind = Template<Arg>;
13132 };
13133
13134 template <template <class> class Template, class Arg>
13135 using Instantiate = Template<Arg>; //#0
13136
13137 template <template <class> class Template,
13138 class Argument>
13139 using Bind =
13140 Instantiate<Internal<Template>::template Bind,
13141 Argument>; //#1
13142
13143 When #1 is parsed, the
13144 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13145 parameter `Template' in #0 matches the
13146 UNBOUND_CLASS_TEMPLATE representing the argument
13147 `Internal<Template>::template Bind'; We then want
13148 to assemble the type `Bind<Argument>' that can't
13149 be fully created right now, because
13150 `Internal<Template>' not being complete, the Bind
13151 template cannot be looked up in that context. So
13152 we need to "store" `Bind<Argument>' for later
13153 when the context of Bind becomes complete. Let's
13154 store that in a TYPENAME_TYPE. */
13155 return make_typename_type (TYPE_CONTEXT (arg),
13156 build_nt (TEMPLATE_ID_EXPR,
13157 TYPE_IDENTIFIER (arg),
13158 argvec),
13159 typename_type,
13160 complain);
13161
13162 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13163 are resolving nested-types in the signature of a
13164 member function templates. Otherwise ARG is a
13165 TEMPLATE_DECL and is the real template to be
13166 instantiated. */
13167 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13168 arg = TYPE_NAME (arg);
13169
13170 r = lookup_template_class (arg,
13171 argvec, in_decl,
13172 DECL_CONTEXT (arg),
13173 /*entering_scope=*/0,
13174 complain);
13175 return cp_build_qualified_type_real
13176 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13177 }
13178 else
13179 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
13180 return convert_from_reference (unshare_expr (arg));
13181 }
13182
13183 if (level == 1)
13184 /* This can happen during the attempted tsubst'ing in
13185 unify. This means that we don't yet have any information
13186 about the template parameter in question. */
13187 return t;
13188
13189 /* If we get here, we must have been looking at a parm for a
13190 more deeply nested template. Make a new version of this
13191 template parameter, but with a lower level. */
13192 switch (code)
13193 {
13194 case TEMPLATE_TYPE_PARM:
13195 case TEMPLATE_TEMPLATE_PARM:
13196 case BOUND_TEMPLATE_TEMPLATE_PARM:
13197 if (cp_type_quals (t))
13198 {
13199 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13200 r = cp_build_qualified_type_real
13201 (r, cp_type_quals (t),
13202 complain | (code == TEMPLATE_TYPE_PARM
13203 ? tf_ignore_bad_quals : 0));
13204 }
13205 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13206 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13207 && (r = (TEMPLATE_PARM_DESCENDANTS
13208 (TEMPLATE_TYPE_PARM_INDEX (t))))
13209 && (r = TREE_TYPE (r))
13210 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13211 /* Break infinite recursion when substituting the constraints
13212 of a constrained placeholder. */;
13213 else
13214 {
13215 r = copy_type (t);
13216 TEMPLATE_TYPE_PARM_INDEX (r)
13217 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13218 r, levels, args, complain);
13219 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13220 TYPE_MAIN_VARIANT (r) = r;
13221 TYPE_POINTER_TO (r) = NULL_TREE;
13222 TYPE_REFERENCE_TO (r) = NULL_TREE;
13223
13224 /* Propagate constraints on placeholders. */
13225 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13226 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13227 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13228 = tsubst_constraint (constr, args, complain, in_decl);
13229
13230 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13231 /* We have reduced the level of the template
13232 template parameter, but not the levels of its
13233 template parameters, so canonical_type_parameter
13234 will not be able to find the canonical template
13235 template parameter for this level. Thus, we
13236 require structural equality checking to compare
13237 TEMPLATE_TEMPLATE_PARMs. */
13238 SET_TYPE_STRUCTURAL_EQUALITY (r);
13239 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13240 SET_TYPE_STRUCTURAL_EQUALITY (r);
13241 else
13242 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13243
13244 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13245 {
13246 tree tinfo = TYPE_TEMPLATE_INFO (t);
13247 /* We might need to substitute into the types of non-type
13248 template parameters. */
13249 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13250 complain, in_decl);
13251 if (tmpl == error_mark_node)
13252 return error_mark_node;
13253 tree argvec = tsubst (TI_ARGS (tinfo), args,
13254 complain, in_decl);
13255 if (argvec == error_mark_node)
13256 return error_mark_node;
13257
13258 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13259 = build_template_info (tmpl, argvec);
13260 }
13261 }
13262 break;
13263
13264 case TEMPLATE_PARM_INDEX:
13265 r = reduce_template_parm_level (t, type, levels, args, complain);
13266 break;
13267
13268 default:
13269 gcc_unreachable ();
13270 }
13271
13272 return r;
13273 }
13274
13275 case TREE_LIST:
13276 {
13277 tree purpose, value, chain;
13278
13279 if (t == void_list_node)
13280 return t;
13281
13282 purpose = TREE_PURPOSE (t);
13283 if (purpose)
13284 {
13285 purpose = tsubst (purpose, args, complain, in_decl);
13286 if (purpose == error_mark_node)
13287 return error_mark_node;
13288 }
13289 value = TREE_VALUE (t);
13290 if (value)
13291 {
13292 value = tsubst (value, args, complain, in_decl);
13293 if (value == error_mark_node)
13294 return error_mark_node;
13295 }
13296 chain = TREE_CHAIN (t);
13297 if (chain && chain != void_type_node)
13298 {
13299 chain = tsubst (chain, args, complain, in_decl);
13300 if (chain == error_mark_node)
13301 return error_mark_node;
13302 }
13303 if (purpose == TREE_PURPOSE (t)
13304 && value == TREE_VALUE (t)
13305 && chain == TREE_CHAIN (t))
13306 return t;
13307 return hash_tree_cons (purpose, value, chain);
13308 }
13309
13310 case TREE_BINFO:
13311 /* We should never be tsubsting a binfo. */
13312 gcc_unreachable ();
13313
13314 case TREE_VEC:
13315 /* A vector of template arguments. */
13316 gcc_assert (!type);
13317 return tsubst_template_args (t, args, complain, in_decl);
13318
13319 case POINTER_TYPE:
13320 case REFERENCE_TYPE:
13321 {
13322 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13323 return t;
13324
13325 /* [temp.deduct]
13326
13327 Type deduction may fail for any of the following
13328 reasons:
13329
13330 -- Attempting to create a pointer to reference type.
13331 -- Attempting to create a reference to a reference type or
13332 a reference to void.
13333
13334 Core issue 106 says that creating a reference to a reference
13335 during instantiation is no longer a cause for failure. We
13336 only enforce this check in strict C++98 mode. */
13337 if ((TREE_CODE (type) == REFERENCE_TYPE
13338 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13339 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13340 {
13341 static location_t last_loc;
13342
13343 /* We keep track of the last time we issued this error
13344 message to avoid spewing a ton of messages during a
13345 single bad template instantiation. */
13346 if (complain & tf_error
13347 && last_loc != input_location)
13348 {
13349 if (VOID_TYPE_P (type))
13350 error ("forming reference to void");
13351 else if (code == POINTER_TYPE)
13352 error ("forming pointer to reference type %qT", type);
13353 else
13354 error ("forming reference to reference type %qT", type);
13355 last_loc = input_location;
13356 }
13357
13358 return error_mark_node;
13359 }
13360 else if (TREE_CODE (type) == FUNCTION_TYPE
13361 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13362 || type_memfn_rqual (type) != REF_QUAL_NONE))
13363 {
13364 if (complain & tf_error)
13365 {
13366 if (code == POINTER_TYPE)
13367 error ("forming pointer to qualified function type %qT",
13368 type);
13369 else
13370 error ("forming reference to qualified function type %qT",
13371 type);
13372 }
13373 return error_mark_node;
13374 }
13375 else if (code == POINTER_TYPE)
13376 {
13377 r = build_pointer_type (type);
13378 if (TREE_CODE (type) == METHOD_TYPE)
13379 r = build_ptrmemfunc_type (r);
13380 }
13381 else if (TREE_CODE (type) == REFERENCE_TYPE)
13382 /* In C++0x, during template argument substitution, when there is an
13383 attempt to create a reference to a reference type, reference
13384 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13385
13386 "If a template-argument for a template-parameter T names a type
13387 that is a reference to a type A, an attempt to create the type
13388 'lvalue reference to cv T' creates the type 'lvalue reference to
13389 A,' while an attempt to create the type type rvalue reference to
13390 cv T' creates the type T"
13391 */
13392 r = cp_build_reference_type
13393 (TREE_TYPE (type),
13394 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13395 else
13396 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13397 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13398
13399 if (r != error_mark_node)
13400 /* Will this ever be needed for TYPE_..._TO values? */
13401 layout_type (r);
13402
13403 return r;
13404 }
13405 case OFFSET_TYPE:
13406 {
13407 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13408 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13409 {
13410 /* [temp.deduct]
13411
13412 Type deduction may fail for any of the following
13413 reasons:
13414
13415 -- Attempting to create "pointer to member of T" when T
13416 is not a class type. */
13417 if (complain & tf_error)
13418 error ("creating pointer to member of non-class type %qT", r);
13419 return error_mark_node;
13420 }
13421 if (TREE_CODE (type) == REFERENCE_TYPE)
13422 {
13423 if (complain & tf_error)
13424 error ("creating pointer to member reference type %qT", type);
13425 return error_mark_node;
13426 }
13427 if (VOID_TYPE_P (type))
13428 {
13429 if (complain & tf_error)
13430 error ("creating pointer to member of type void");
13431 return error_mark_node;
13432 }
13433 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13434 if (TREE_CODE (type) == FUNCTION_TYPE)
13435 {
13436 /* The type of the implicit object parameter gets its
13437 cv-qualifiers from the FUNCTION_TYPE. */
13438 tree memptr;
13439 tree method_type
13440 = build_memfn_type (type, r, type_memfn_quals (type),
13441 type_memfn_rqual (type));
13442 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13443 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13444 complain);
13445 }
13446 else
13447 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13448 cp_type_quals (t),
13449 complain);
13450 }
13451 case FUNCTION_TYPE:
13452 case METHOD_TYPE:
13453 {
13454 tree fntype;
13455 tree specs;
13456 fntype = tsubst_function_type (t, args, complain, in_decl);
13457 if (fntype == error_mark_node)
13458 return error_mark_node;
13459
13460 /* Substitute the exception specification. */
13461 specs = tsubst_exception_specification (t, args, complain,
13462 in_decl, /*defer_ok*/true);
13463 if (specs == error_mark_node)
13464 return error_mark_node;
13465 if (specs)
13466 fntype = build_exception_variant (fntype, specs);
13467 return fntype;
13468 }
13469 case ARRAY_TYPE:
13470 {
13471 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13472 if (domain == error_mark_node)
13473 return error_mark_node;
13474
13475 /* As an optimization, we avoid regenerating the array type if
13476 it will obviously be the same as T. */
13477 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13478 return t;
13479
13480 /* These checks should match the ones in create_array_type_for_decl.
13481
13482 [temp.deduct]
13483
13484 The deduction may fail for any of the following reasons:
13485
13486 -- Attempting to create an array with an element type that
13487 is void, a function type, or a reference type, or [DR337]
13488 an abstract class type. */
13489 if (VOID_TYPE_P (type)
13490 || TREE_CODE (type) == FUNCTION_TYPE
13491 || (TREE_CODE (type) == ARRAY_TYPE
13492 && TYPE_DOMAIN (type) == NULL_TREE)
13493 || TREE_CODE (type) == REFERENCE_TYPE)
13494 {
13495 if (complain & tf_error)
13496 error ("creating array of %qT", type);
13497 return error_mark_node;
13498 }
13499
13500 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13501 return error_mark_node;
13502
13503 r = build_cplus_array_type (type, domain);
13504
13505 if (TYPE_USER_ALIGN (t))
13506 {
13507 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13508 TYPE_USER_ALIGN (r) = 1;
13509 }
13510
13511 return r;
13512 }
13513
13514 case TYPENAME_TYPE:
13515 {
13516 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13517 in_decl, /*entering_scope=*/1);
13518 if (ctx == error_mark_node)
13519 return error_mark_node;
13520
13521 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13522 complain, in_decl);
13523 if (f == error_mark_node)
13524 return error_mark_node;
13525
13526 if (!MAYBE_CLASS_TYPE_P (ctx))
13527 {
13528 if (complain & tf_error)
13529 error ("%qT is not a class, struct, or union type", ctx);
13530 return error_mark_node;
13531 }
13532 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13533 {
13534 /* Normally, make_typename_type does not require that the CTX
13535 have complete type in order to allow things like:
13536
13537 template <class T> struct S { typename S<T>::X Y; };
13538
13539 But, such constructs have already been resolved by this
13540 point, so here CTX really should have complete type, unless
13541 it's a partial instantiation. */
13542 ctx = complete_type (ctx);
13543 if (!COMPLETE_TYPE_P (ctx))
13544 {
13545 if (complain & tf_error)
13546 cxx_incomplete_type_error (NULL_TREE, ctx);
13547 return error_mark_node;
13548 }
13549 }
13550
13551 f = make_typename_type (ctx, f, typename_type,
13552 complain | tf_keep_type_decl);
13553 if (f == error_mark_node)
13554 return f;
13555 if (TREE_CODE (f) == TYPE_DECL)
13556 {
13557 complain |= tf_ignore_bad_quals;
13558 f = TREE_TYPE (f);
13559 }
13560
13561 if (TREE_CODE (f) != TYPENAME_TYPE)
13562 {
13563 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13564 {
13565 if (complain & tf_error)
13566 error ("%qT resolves to %qT, which is not an enumeration type",
13567 t, f);
13568 else
13569 return error_mark_node;
13570 }
13571 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13572 {
13573 if (complain & tf_error)
13574 error ("%qT resolves to %qT, which is is not a class type",
13575 t, f);
13576 else
13577 return error_mark_node;
13578 }
13579 }
13580
13581 return cp_build_qualified_type_real
13582 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13583 }
13584
13585 case UNBOUND_CLASS_TEMPLATE:
13586 {
13587 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13588 in_decl, /*entering_scope=*/1);
13589 tree name = TYPE_IDENTIFIER (t);
13590 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13591
13592 if (ctx == error_mark_node || name == error_mark_node)
13593 return error_mark_node;
13594
13595 if (parm_list)
13596 parm_list = tsubst_template_parms (parm_list, args, complain);
13597 return make_unbound_class_template (ctx, name, parm_list, complain);
13598 }
13599
13600 case TYPEOF_TYPE:
13601 {
13602 tree type;
13603
13604 ++cp_unevaluated_operand;
13605 ++c_inhibit_evaluation_warnings;
13606
13607 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13608 complain, in_decl,
13609 /*integral_constant_expression_p=*/false);
13610
13611 --cp_unevaluated_operand;
13612 --c_inhibit_evaluation_warnings;
13613
13614 type = finish_typeof (type);
13615 return cp_build_qualified_type_real (type,
13616 cp_type_quals (t)
13617 | cp_type_quals (type),
13618 complain);
13619 }
13620
13621 case DECLTYPE_TYPE:
13622 {
13623 tree type;
13624
13625 ++cp_unevaluated_operand;
13626 ++c_inhibit_evaluation_warnings;
13627
13628 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13629 complain|tf_decltype, in_decl,
13630 /*function_p*/false,
13631 /*integral_constant_expression*/false);
13632
13633 if (DECLTYPE_FOR_INIT_CAPTURE (t))
13634 {
13635 if (type == NULL_TREE)
13636 {
13637 if (complain & tf_error)
13638 error ("empty initializer in lambda init-capture");
13639 type = error_mark_node;
13640 }
13641 else if (TREE_CODE (type) == TREE_LIST)
13642 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
13643 }
13644
13645 --cp_unevaluated_operand;
13646 --c_inhibit_evaluation_warnings;
13647
13648 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13649 type = lambda_capture_field_type (type,
13650 DECLTYPE_FOR_INIT_CAPTURE (t));
13651 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13652 type = lambda_proxy_type (type);
13653 else
13654 {
13655 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13656 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13657 && EXPR_P (type))
13658 /* In a template ~id could be either a complement expression
13659 or an unqualified-id naming a destructor; if instantiating
13660 it produces an expression, it's not an id-expression or
13661 member access. */
13662 id = false;
13663 type = finish_decltype_type (type, id, complain);
13664 }
13665 return cp_build_qualified_type_real (type,
13666 cp_type_quals (t)
13667 | cp_type_quals (type),
13668 complain | tf_ignore_bad_quals);
13669 }
13670
13671 case UNDERLYING_TYPE:
13672 {
13673 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13674 complain, in_decl);
13675 return finish_underlying_type (type);
13676 }
13677
13678 case TYPE_ARGUMENT_PACK:
13679 case NONTYPE_ARGUMENT_PACK:
13680 {
13681 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13682 tree packed_out =
13683 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13684 args,
13685 complain,
13686 in_decl);
13687 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13688
13689 /* For template nontype argument packs, also substitute into
13690 the type. */
13691 if (code == NONTYPE_ARGUMENT_PACK)
13692 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13693
13694 return r;
13695 }
13696 break;
13697
13698 case VOID_CST:
13699 case INTEGER_CST:
13700 case REAL_CST:
13701 case STRING_CST:
13702 case PLUS_EXPR:
13703 case MINUS_EXPR:
13704 case NEGATE_EXPR:
13705 case NOP_EXPR:
13706 case INDIRECT_REF:
13707 case ADDR_EXPR:
13708 case CALL_EXPR:
13709 case ARRAY_REF:
13710 case SCOPE_REF:
13711 /* We should use one of the expression tsubsts for these codes. */
13712 gcc_unreachable ();
13713
13714 default:
13715 sorry ("use of %qs in template", get_tree_code_name (code));
13716 return error_mark_node;
13717 }
13718 }
13719
13720 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13721 type of the expression on the left-hand side of the "." or "->"
13722 operator. */
13723
13724 static tree
13725 tsubst_baselink (tree baselink, tree object_type,
13726 tree args, tsubst_flags_t complain, tree in_decl)
13727 {
13728 tree name;
13729 tree qualifying_scope;
13730 tree fns;
13731 tree optype;
13732 tree template_args = 0;
13733 bool template_id_p = false;
13734 bool qualified = BASELINK_QUALIFIED_P (baselink);
13735
13736 /* A baselink indicates a function from a base class. Both the
13737 BASELINK_ACCESS_BINFO and the base class referenced may
13738 indicate bases of the template class, rather than the
13739 instantiated class. In addition, lookups that were not
13740 ambiguous before may be ambiguous now. Therefore, we perform
13741 the lookup again. */
13742 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13743 qualifying_scope = tsubst (qualifying_scope, args,
13744 complain, in_decl);
13745 fns = BASELINK_FUNCTIONS (baselink);
13746 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13747 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13748 {
13749 template_id_p = true;
13750 template_args = TREE_OPERAND (fns, 1);
13751 fns = TREE_OPERAND (fns, 0);
13752 if (template_args)
13753 template_args = tsubst_template_args (template_args, args,
13754 complain, in_decl);
13755 }
13756 name = DECL_NAME (get_first_fn (fns));
13757 if (IDENTIFIER_TYPENAME_P (name))
13758 name = mangle_conv_op_name_for_type (optype);
13759 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13760 if (!baselink)
13761 {
13762 if (constructor_name_p (name, qualifying_scope))
13763 {
13764 if (complain & tf_error)
13765 error ("cannot call constructor %<%T::%D%> directly",
13766 qualifying_scope, name);
13767 }
13768 return error_mark_node;
13769 }
13770
13771 /* If lookup found a single function, mark it as used at this
13772 point. (If it lookup found multiple functions the one selected
13773 later by overload resolution will be marked as used at that
13774 point.) */
13775 if (BASELINK_P (baselink))
13776 fns = BASELINK_FUNCTIONS (baselink);
13777 if (!template_id_p && !really_overloaded_fn (fns)
13778 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13779 return error_mark_node;
13780
13781 /* Add back the template arguments, if present. */
13782 if (BASELINK_P (baselink) && template_id_p)
13783 BASELINK_FUNCTIONS (baselink)
13784 = build2 (TEMPLATE_ID_EXPR,
13785 unknown_type_node,
13786 BASELINK_FUNCTIONS (baselink),
13787 template_args);
13788 /* Update the conversion operator type. */
13789 if (BASELINK_P (baselink))
13790 BASELINK_OPTYPE (baselink) = optype;
13791
13792 if (!object_type)
13793 object_type = current_class_type;
13794
13795 if (qualified || name == complete_dtor_identifier)
13796 {
13797 baselink = adjust_result_of_qualified_name_lookup (baselink,
13798 qualifying_scope,
13799 object_type);
13800 if (!qualified)
13801 /* We need to call adjust_result_of_qualified_name_lookup in case the
13802 destructor names a base class, but we unset BASELINK_QUALIFIED_P
13803 so that we still get virtual function binding. */
13804 BASELINK_QUALIFIED_P (baselink) = false;
13805 }
13806 return baselink;
13807 }
13808
13809 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13810 true if the qualified-id will be a postfix-expression in-and-of
13811 itself; false if more of the postfix-expression follows the
13812 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13813 of "&". */
13814
13815 static tree
13816 tsubst_qualified_id (tree qualified_id, tree args,
13817 tsubst_flags_t complain, tree in_decl,
13818 bool done, bool address_p)
13819 {
13820 tree expr;
13821 tree scope;
13822 tree name;
13823 bool is_template;
13824 tree template_args;
13825 location_t loc = UNKNOWN_LOCATION;
13826
13827 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13828
13829 /* Figure out what name to look up. */
13830 name = TREE_OPERAND (qualified_id, 1);
13831 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13832 {
13833 is_template = true;
13834 loc = EXPR_LOCATION (name);
13835 template_args = TREE_OPERAND (name, 1);
13836 if (template_args)
13837 template_args = tsubst_template_args (template_args, args,
13838 complain, in_decl);
13839 if (template_args == error_mark_node)
13840 return error_mark_node;
13841 name = TREE_OPERAND (name, 0);
13842 }
13843 else
13844 {
13845 is_template = false;
13846 template_args = NULL_TREE;
13847 }
13848
13849 /* Substitute into the qualifying scope. When there are no ARGS, we
13850 are just trying to simplify a non-dependent expression. In that
13851 case the qualifying scope may be dependent, and, in any case,
13852 substituting will not help. */
13853 scope = TREE_OPERAND (qualified_id, 0);
13854 if (args)
13855 {
13856 scope = tsubst (scope, args, complain, in_decl);
13857 expr = tsubst_copy (name, args, complain, in_decl);
13858 }
13859 else
13860 expr = name;
13861
13862 if (dependent_scope_p (scope))
13863 {
13864 if (is_template)
13865 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13866 tree r = build_qualified_name (NULL_TREE, scope, expr,
13867 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13868 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
13869 return r;
13870 }
13871
13872 if (!BASELINK_P (name) && !DECL_P (expr))
13873 {
13874 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13875 {
13876 /* A BIT_NOT_EXPR is used to represent a destructor. */
13877 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13878 {
13879 error ("qualifying type %qT does not match destructor name ~%qT",
13880 scope, TREE_OPERAND (expr, 0));
13881 expr = error_mark_node;
13882 }
13883 else
13884 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13885 /*is_type_p=*/0, false);
13886 }
13887 else
13888 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13889 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13890 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13891 {
13892 if (complain & tf_error)
13893 {
13894 error ("dependent-name %qE is parsed as a non-type, but "
13895 "instantiation yields a type", qualified_id);
13896 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13897 }
13898 return error_mark_node;
13899 }
13900 }
13901
13902 if (DECL_P (expr))
13903 {
13904 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13905 scope);
13906 /* Remember that there was a reference to this entity. */
13907 if (!mark_used (expr, complain) && !(complain & tf_error))
13908 return error_mark_node;
13909 }
13910
13911 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13912 {
13913 if (complain & tf_error)
13914 qualified_name_lookup_error (scope,
13915 TREE_OPERAND (qualified_id, 1),
13916 expr, input_location);
13917 return error_mark_node;
13918 }
13919
13920 if (is_template)
13921 {
13922 if (variable_template_p (expr))
13923 expr = lookup_and_finish_template_variable (expr, template_args,
13924 complain);
13925 else
13926 expr = lookup_template_function (expr, template_args);
13927 }
13928
13929 if (expr == error_mark_node && complain & tf_error)
13930 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
13931 expr, input_location);
13932 else if (TYPE_P (scope))
13933 {
13934 expr = (adjust_result_of_qualified_name_lookup
13935 (expr, scope, current_nonlambda_class_type ()));
13936 expr = (finish_qualified_id_expr
13937 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
13938 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
13939 /*template_arg_p=*/false, complain));
13940 }
13941
13942 /* Expressions do not generally have reference type. */
13943 if (TREE_CODE (expr) != SCOPE_REF
13944 /* However, if we're about to form a pointer-to-member, we just
13945 want the referenced member referenced. */
13946 && TREE_CODE (expr) != OFFSET_REF)
13947 expr = convert_from_reference (expr);
13948
13949 if (REF_PARENTHESIZED_P (qualified_id))
13950 expr = force_paren_expr (expr);
13951
13952 return expr;
13953 }
13954
13955 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13956 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13957 for tsubst. */
13958
13959 static tree
13960 tsubst_init (tree init, tree decl, tree args,
13961 tsubst_flags_t complain, tree in_decl)
13962 {
13963 if (!init)
13964 return NULL_TREE;
13965
13966 init = tsubst_expr (init, args, complain, in_decl, false);
13967
13968 if (!init)
13969 {
13970 /* If we had an initializer but it
13971 instantiated to nothing,
13972 value-initialize the object. This will
13973 only occur when the initializer was a
13974 pack expansion where the parameter packs
13975 used in that expansion were of length
13976 zero. */
13977 init = build_value_init (TREE_TYPE (decl),
13978 complain);
13979 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13980 init = get_target_expr_sfinae (init, complain);
13981 }
13982
13983 return init;
13984 }
13985
13986 /* Like tsubst, but deals with expressions. This function just replaces
13987 template parms; to finish processing the resultant expression, use
13988 tsubst_copy_and_build or tsubst_expr. */
13989
13990 static tree
13991 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13992 {
13993 enum tree_code code;
13994 tree r;
13995
13996 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
13997 return t;
13998
13999 code = TREE_CODE (t);
14000
14001 switch (code)
14002 {
14003 case PARM_DECL:
14004 r = retrieve_local_specialization (t);
14005
14006 if (r == NULL_TREE)
14007 {
14008 /* We get here for a use of 'this' in an NSDMI as part of a
14009 constructor call or as part of an aggregate initialization. */
14010 if (DECL_NAME (t) == this_identifier
14011 && ((current_function_decl
14012 && DECL_CONSTRUCTOR_P (current_function_decl))
14013 || (current_class_ref
14014 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14015 return current_class_ptr;
14016
14017 /* This can happen for a parameter name used later in a function
14018 declaration (such as in a late-specified return type). Just
14019 make a dummy decl, since it's only used for its type. */
14020 gcc_assert (cp_unevaluated_operand != 0);
14021 r = tsubst_decl (t, args, complain);
14022 /* Give it the template pattern as its context; its true context
14023 hasn't been instantiated yet and this is good enough for
14024 mangling. */
14025 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14026 }
14027
14028 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14029 r = ARGUMENT_PACK_SELECT_ARG (r);
14030 if (!mark_used (r, complain) && !(complain & tf_error))
14031 return error_mark_node;
14032 return r;
14033
14034 case CONST_DECL:
14035 {
14036 tree enum_type;
14037 tree v;
14038
14039 if (DECL_TEMPLATE_PARM_P (t))
14040 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14041 /* There is no need to substitute into namespace-scope
14042 enumerators. */
14043 if (DECL_NAMESPACE_SCOPE_P (t))
14044 return t;
14045 /* If ARGS is NULL, then T is known to be non-dependent. */
14046 if (args == NULL_TREE)
14047 return scalar_constant_value (t);
14048
14049 /* Unfortunately, we cannot just call lookup_name here.
14050 Consider:
14051
14052 template <int I> int f() {
14053 enum E { a = I };
14054 struct S { void g() { E e = a; } };
14055 };
14056
14057 When we instantiate f<7>::S::g(), say, lookup_name is not
14058 clever enough to find f<7>::a. */
14059 enum_type
14060 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14061 /*entering_scope=*/0);
14062
14063 for (v = TYPE_VALUES (enum_type);
14064 v != NULL_TREE;
14065 v = TREE_CHAIN (v))
14066 if (TREE_PURPOSE (v) == DECL_NAME (t))
14067 return TREE_VALUE (v);
14068
14069 /* We didn't find the name. That should never happen; if
14070 name-lookup found it during preliminary parsing, we
14071 should find it again here during instantiation. */
14072 gcc_unreachable ();
14073 }
14074 return t;
14075
14076 case FIELD_DECL:
14077 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14078 {
14079 /* Check for a local specialization set up by
14080 tsubst_pack_expansion. */
14081 if (tree r = retrieve_local_specialization (t))
14082 {
14083 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14084 r = ARGUMENT_PACK_SELECT_ARG (r);
14085 return r;
14086 }
14087
14088 /* When retrieving a capture pack from a generic lambda, remove the
14089 lambda call op's own template argument list from ARGS. Only the
14090 template arguments active for the closure type should be used to
14091 retrieve the pack specialization. */
14092 if (LAMBDA_FUNCTION_P (current_function_decl)
14093 && (template_class_depth (DECL_CONTEXT (t))
14094 != TMPL_ARGS_DEPTH (args)))
14095 args = strip_innermost_template_args (args, 1);
14096
14097 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14098 tsubst_decl put in the hash table. */
14099 return retrieve_specialization (t, args, 0);
14100 }
14101
14102 if (DECL_CONTEXT (t))
14103 {
14104 tree ctx;
14105
14106 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14107 /*entering_scope=*/1);
14108 if (ctx != DECL_CONTEXT (t))
14109 {
14110 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14111 if (!r)
14112 {
14113 if (complain & tf_error)
14114 error ("using invalid field %qD", t);
14115 return error_mark_node;
14116 }
14117 return r;
14118 }
14119 }
14120
14121 return t;
14122
14123 case VAR_DECL:
14124 case FUNCTION_DECL:
14125 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14126 r = tsubst (t, args, complain, in_decl);
14127 else if (local_variable_p (t)
14128 && uses_template_parms (DECL_CONTEXT (t)))
14129 {
14130 r = retrieve_local_specialization (t);
14131 if (r == NULL_TREE)
14132 {
14133 /* First try name lookup to find the instantiation. */
14134 r = lookup_name (DECL_NAME (t));
14135 if (r)
14136 {
14137 /* Make sure that the one we found is the one we want. */
14138 tree ctx = DECL_CONTEXT (t);
14139 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14140 ctx = tsubst (ctx, args, complain, in_decl);
14141 if (ctx != DECL_CONTEXT (r))
14142 r = NULL_TREE;
14143 }
14144
14145 if (r)
14146 /* OK */;
14147 else
14148 {
14149 /* This can happen for a variable used in a
14150 late-specified return type of a local lambda, or for a
14151 local static or constant. Building a new VAR_DECL
14152 should be OK in all those cases. */
14153 r = tsubst_decl (t, args, complain);
14154 if (decl_maybe_constant_var_p (r))
14155 {
14156 /* We can't call cp_finish_decl, so handle the
14157 initializer by hand. */
14158 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14159 complain, in_decl);
14160 if (!processing_template_decl)
14161 init = maybe_constant_init (init);
14162 if (processing_template_decl
14163 ? potential_constant_expression (init)
14164 : reduced_constant_expression_p (init))
14165 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14166 = TREE_CONSTANT (r) = true;
14167 DECL_INITIAL (r) = init;
14168 }
14169 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14170 || decl_constant_var_p (r)
14171 || errorcount || sorrycount);
14172 if (!processing_template_decl
14173 && !TREE_STATIC (r))
14174 r = process_outer_var_ref (r, complain);
14175 }
14176 /* Remember this for subsequent uses. */
14177 if (local_specializations)
14178 register_local_specialization (r, t);
14179 }
14180 }
14181 else
14182 r = t;
14183 if (!mark_used (r, complain) && !(complain & tf_error))
14184 return error_mark_node;
14185 return r;
14186
14187 case NAMESPACE_DECL:
14188 return t;
14189
14190 case OVERLOAD:
14191 /* An OVERLOAD will always be a non-dependent overload set; an
14192 overload set from function scope will just be represented with an
14193 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14194 gcc_assert (!uses_template_parms (t));
14195 return t;
14196
14197 case BASELINK:
14198 return tsubst_baselink (t, current_nonlambda_class_type (),
14199 args, complain, in_decl);
14200
14201 case TEMPLATE_DECL:
14202 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14203 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14204 args, complain, in_decl);
14205 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14206 return tsubst (t, args, complain, in_decl);
14207 else if (DECL_CLASS_SCOPE_P (t)
14208 && uses_template_parms (DECL_CONTEXT (t)))
14209 {
14210 /* Template template argument like the following example need
14211 special treatment:
14212
14213 template <template <class> class TT> struct C {};
14214 template <class T> struct D {
14215 template <class U> struct E {};
14216 C<E> c; // #1
14217 };
14218 D<int> d; // #2
14219
14220 We are processing the template argument `E' in #1 for
14221 the template instantiation #2. Originally, `E' is a
14222 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14223 have to substitute this with one having context `D<int>'. */
14224
14225 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14226 return lookup_field (context, DECL_NAME(t), 0, false);
14227 }
14228 else
14229 /* Ordinary template template argument. */
14230 return t;
14231
14232 case CAST_EXPR:
14233 case REINTERPRET_CAST_EXPR:
14234 case CONST_CAST_EXPR:
14235 case STATIC_CAST_EXPR:
14236 case DYNAMIC_CAST_EXPR:
14237 case IMPLICIT_CONV_EXPR:
14238 case CONVERT_EXPR:
14239 case NOP_EXPR:
14240 {
14241 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14242 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14243 return build1 (code, type, op0);
14244 }
14245
14246 case SIZEOF_EXPR:
14247 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14248 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14249 {
14250 tree expanded, op = TREE_OPERAND (t, 0);
14251 int len = 0;
14252
14253 if (SIZEOF_EXPR_TYPE_P (t))
14254 op = TREE_TYPE (op);
14255
14256 ++cp_unevaluated_operand;
14257 ++c_inhibit_evaluation_warnings;
14258 /* We only want to compute the number of arguments. */
14259 if (PACK_EXPANSION_P (op))
14260 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14261 else
14262 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14263 args, complain, in_decl);
14264 --cp_unevaluated_operand;
14265 --c_inhibit_evaluation_warnings;
14266
14267 if (TREE_CODE (expanded) == TREE_VEC)
14268 {
14269 len = TREE_VEC_LENGTH (expanded);
14270 /* Set TREE_USED for the benefit of -Wunused. */
14271 for (int i = 0; i < len; i++)
14272 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14273 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14274 }
14275
14276 if (expanded == error_mark_node)
14277 return error_mark_node;
14278 else if (PACK_EXPANSION_P (expanded)
14279 || (TREE_CODE (expanded) == TREE_VEC
14280 && pack_expansion_args_count (expanded)))
14281
14282 {
14283 if (PACK_EXPANSION_P (expanded))
14284 /* OK. */;
14285 else if (TREE_VEC_LENGTH (expanded) == 1)
14286 expanded = TREE_VEC_ELT (expanded, 0);
14287 else
14288 expanded = make_argument_pack (expanded);
14289
14290 if (TYPE_P (expanded))
14291 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14292 complain & tf_error);
14293 else
14294 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14295 complain & tf_error);
14296 }
14297 else
14298 return build_int_cst (size_type_node, len);
14299 }
14300 if (SIZEOF_EXPR_TYPE_P (t))
14301 {
14302 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14303 args, complain, in_decl);
14304 r = build1 (NOP_EXPR, r, error_mark_node);
14305 r = build1 (SIZEOF_EXPR,
14306 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14307 SIZEOF_EXPR_TYPE_P (r) = 1;
14308 return r;
14309 }
14310 /* Fall through */
14311
14312 case INDIRECT_REF:
14313 case NEGATE_EXPR:
14314 case TRUTH_NOT_EXPR:
14315 case BIT_NOT_EXPR:
14316 case ADDR_EXPR:
14317 case UNARY_PLUS_EXPR: /* Unary + */
14318 case ALIGNOF_EXPR:
14319 case AT_ENCODE_EXPR:
14320 case ARROW_EXPR:
14321 case THROW_EXPR:
14322 case TYPEID_EXPR:
14323 case REALPART_EXPR:
14324 case IMAGPART_EXPR:
14325 case PAREN_EXPR:
14326 {
14327 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14328 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14329 return build1 (code, type, op0);
14330 }
14331
14332 case COMPONENT_REF:
14333 {
14334 tree object;
14335 tree name;
14336
14337 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14338 name = TREE_OPERAND (t, 1);
14339 if (TREE_CODE (name) == BIT_NOT_EXPR)
14340 {
14341 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14342 complain, in_decl);
14343 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14344 }
14345 else if (TREE_CODE (name) == SCOPE_REF
14346 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14347 {
14348 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14349 complain, in_decl);
14350 name = TREE_OPERAND (name, 1);
14351 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14352 complain, in_decl);
14353 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14354 name = build_qualified_name (/*type=*/NULL_TREE,
14355 base, name,
14356 /*template_p=*/false);
14357 }
14358 else if (BASELINK_P (name))
14359 name = tsubst_baselink (name,
14360 non_reference (TREE_TYPE (object)),
14361 args, complain,
14362 in_decl);
14363 else
14364 name = tsubst_copy (name, args, complain, in_decl);
14365 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14366 }
14367
14368 case PLUS_EXPR:
14369 case MINUS_EXPR:
14370 case MULT_EXPR:
14371 case TRUNC_DIV_EXPR:
14372 case CEIL_DIV_EXPR:
14373 case FLOOR_DIV_EXPR:
14374 case ROUND_DIV_EXPR:
14375 case EXACT_DIV_EXPR:
14376 case BIT_AND_EXPR:
14377 case BIT_IOR_EXPR:
14378 case BIT_XOR_EXPR:
14379 case TRUNC_MOD_EXPR:
14380 case FLOOR_MOD_EXPR:
14381 case TRUTH_ANDIF_EXPR:
14382 case TRUTH_ORIF_EXPR:
14383 case TRUTH_AND_EXPR:
14384 case TRUTH_OR_EXPR:
14385 case RSHIFT_EXPR:
14386 case LSHIFT_EXPR:
14387 case RROTATE_EXPR:
14388 case LROTATE_EXPR:
14389 case EQ_EXPR:
14390 case NE_EXPR:
14391 case MAX_EXPR:
14392 case MIN_EXPR:
14393 case LE_EXPR:
14394 case GE_EXPR:
14395 case LT_EXPR:
14396 case GT_EXPR:
14397 case COMPOUND_EXPR:
14398 case DOTSTAR_EXPR:
14399 case MEMBER_REF:
14400 case PREDECREMENT_EXPR:
14401 case PREINCREMENT_EXPR:
14402 case POSTDECREMENT_EXPR:
14403 case POSTINCREMENT_EXPR:
14404 {
14405 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14406 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14407 return build_nt (code, op0, op1);
14408 }
14409
14410 case SCOPE_REF:
14411 {
14412 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14413 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14414 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14415 QUALIFIED_NAME_IS_TEMPLATE (t));
14416 }
14417
14418 case ARRAY_REF:
14419 {
14420 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14421 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14422 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14423 }
14424
14425 case CALL_EXPR:
14426 {
14427 int n = VL_EXP_OPERAND_LENGTH (t);
14428 tree result = build_vl_exp (CALL_EXPR, n);
14429 int i;
14430 for (i = 0; i < n; i++)
14431 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14432 complain, in_decl);
14433 return result;
14434 }
14435
14436 case COND_EXPR:
14437 case MODOP_EXPR:
14438 case PSEUDO_DTOR_EXPR:
14439 case VEC_PERM_EXPR:
14440 {
14441 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14442 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14443 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14444 r = build_nt (code, op0, op1, op2);
14445 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14446 return r;
14447 }
14448
14449 case NEW_EXPR:
14450 {
14451 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14452 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14453 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14454 r = build_nt (code, op0, op1, op2);
14455 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14456 return r;
14457 }
14458
14459 case DELETE_EXPR:
14460 {
14461 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14462 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14463 r = build_nt (code, op0, op1);
14464 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14465 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14466 return r;
14467 }
14468
14469 case TEMPLATE_ID_EXPR:
14470 {
14471 /* Substituted template arguments */
14472 tree fn = TREE_OPERAND (t, 0);
14473 tree targs = TREE_OPERAND (t, 1);
14474
14475 fn = tsubst_copy (fn, args, complain, in_decl);
14476 if (targs)
14477 targs = tsubst_template_args (targs, args, complain, in_decl);
14478
14479 return lookup_template_function (fn, targs);
14480 }
14481
14482 case TREE_LIST:
14483 {
14484 tree purpose, value, chain;
14485
14486 if (t == void_list_node)
14487 return t;
14488
14489 purpose = TREE_PURPOSE (t);
14490 if (purpose)
14491 purpose = tsubst_copy (purpose, args, complain, in_decl);
14492 value = TREE_VALUE (t);
14493 if (value)
14494 value = tsubst_copy (value, args, complain, in_decl);
14495 chain = TREE_CHAIN (t);
14496 if (chain && chain != void_type_node)
14497 chain = tsubst_copy (chain, args, complain, in_decl);
14498 if (purpose == TREE_PURPOSE (t)
14499 && value == TREE_VALUE (t)
14500 && chain == TREE_CHAIN (t))
14501 return t;
14502 return tree_cons (purpose, value, chain);
14503 }
14504
14505 case RECORD_TYPE:
14506 case UNION_TYPE:
14507 case ENUMERAL_TYPE:
14508 case INTEGER_TYPE:
14509 case TEMPLATE_TYPE_PARM:
14510 case TEMPLATE_TEMPLATE_PARM:
14511 case BOUND_TEMPLATE_TEMPLATE_PARM:
14512 case TEMPLATE_PARM_INDEX:
14513 case POINTER_TYPE:
14514 case REFERENCE_TYPE:
14515 case OFFSET_TYPE:
14516 case FUNCTION_TYPE:
14517 case METHOD_TYPE:
14518 case ARRAY_TYPE:
14519 case TYPENAME_TYPE:
14520 case UNBOUND_CLASS_TEMPLATE:
14521 case TYPEOF_TYPE:
14522 case DECLTYPE_TYPE:
14523 case TYPE_DECL:
14524 return tsubst (t, args, complain, in_decl);
14525
14526 case USING_DECL:
14527 t = DECL_NAME (t);
14528 /* Fall through. */
14529 case IDENTIFIER_NODE:
14530 if (IDENTIFIER_TYPENAME_P (t))
14531 {
14532 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14533 return mangle_conv_op_name_for_type (new_type);
14534 }
14535 else
14536 return t;
14537
14538 case CONSTRUCTOR:
14539 /* This is handled by tsubst_copy_and_build. */
14540 gcc_unreachable ();
14541
14542 case VA_ARG_EXPR:
14543 {
14544 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14545 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14546 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14547 }
14548
14549 case CLEANUP_POINT_EXPR:
14550 /* We shouldn't have built any of these during initial template
14551 generation. Instead, they should be built during instantiation
14552 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14553 gcc_unreachable ();
14554
14555 case OFFSET_REF:
14556 {
14557 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14558 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14559 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14560 r = build2 (code, type, op0, op1);
14561 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14562 if (!mark_used (TREE_OPERAND (r, 1), complain)
14563 && !(complain & tf_error))
14564 return error_mark_node;
14565 return r;
14566 }
14567
14568 case EXPR_PACK_EXPANSION:
14569 error ("invalid use of pack expansion expression");
14570 return error_mark_node;
14571
14572 case NONTYPE_ARGUMENT_PACK:
14573 error ("use %<...%> to expand argument pack");
14574 return error_mark_node;
14575
14576 case VOID_CST:
14577 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14578 return t;
14579
14580 case INTEGER_CST:
14581 case REAL_CST:
14582 case STRING_CST:
14583 case COMPLEX_CST:
14584 {
14585 /* Instantiate any typedefs in the type. */
14586 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14587 r = fold_convert (type, t);
14588 gcc_assert (TREE_CODE (r) == code);
14589 return r;
14590 }
14591
14592 case PTRMEM_CST:
14593 /* These can sometimes show up in a partial instantiation, but never
14594 involve template parms. */
14595 gcc_assert (!uses_template_parms (t));
14596 return t;
14597
14598 case UNARY_LEFT_FOLD_EXPR:
14599 return tsubst_unary_left_fold (t, args, complain, in_decl);
14600 case UNARY_RIGHT_FOLD_EXPR:
14601 return tsubst_unary_right_fold (t, args, complain, in_decl);
14602 case BINARY_LEFT_FOLD_EXPR:
14603 return tsubst_binary_left_fold (t, args, complain, in_decl);
14604 case BINARY_RIGHT_FOLD_EXPR:
14605 return tsubst_binary_right_fold (t, args, complain, in_decl);
14606
14607 default:
14608 /* We shouldn't get here, but keep going if !flag_checking. */
14609 if (flag_checking)
14610 gcc_unreachable ();
14611 return t;
14612 }
14613 }
14614
14615 /* Helper function for tsubst_omp_clauses, used for instantiation of
14616 OMP_CLAUSE_DECL of clauses. */
14617
14618 static tree
14619 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14620 tree in_decl)
14621 {
14622 if (decl == NULL_TREE)
14623 return NULL_TREE;
14624
14625 /* Handle an OpenMP array section represented as a TREE_LIST (or
14626 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14627 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14628 TREE_LIST. We can handle it exactly the same as an array section
14629 (purpose, value, and a chain), even though the nomenclature
14630 (low_bound, length, etc) is different. */
14631 if (TREE_CODE (decl) == TREE_LIST)
14632 {
14633 tree low_bound
14634 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14635 /*integral_constant_expression_p=*/false);
14636 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14637 /*integral_constant_expression_p=*/false);
14638 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14639 in_decl);
14640 if (TREE_PURPOSE (decl) == low_bound
14641 && TREE_VALUE (decl) == length
14642 && TREE_CHAIN (decl) == chain)
14643 return decl;
14644 tree ret = tree_cons (low_bound, length, chain);
14645 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
14646 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
14647 return ret;
14648 }
14649 tree ret = tsubst_expr (decl, args, complain, in_decl,
14650 /*integral_constant_expression_p=*/false);
14651 /* Undo convert_from_reference tsubst_expr could have called. */
14652 if (decl
14653 && REFERENCE_REF_P (ret)
14654 && !REFERENCE_REF_P (decl))
14655 ret = TREE_OPERAND (ret, 0);
14656 return ret;
14657 }
14658
14659 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14660
14661 static tree
14662 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
14663 tree args, tsubst_flags_t complain, tree in_decl)
14664 {
14665 tree new_clauses = NULL_TREE, nc, oc;
14666 tree linear_no_step = NULL_TREE;
14667
14668 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14669 {
14670 nc = copy_node (oc);
14671 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14672 new_clauses = nc;
14673
14674 switch (OMP_CLAUSE_CODE (nc))
14675 {
14676 case OMP_CLAUSE_LASTPRIVATE:
14677 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14678 {
14679 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14680 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14681 in_decl, /*integral_constant_expression_p=*/false);
14682 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14683 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14684 }
14685 /* FALLTHRU */
14686 case OMP_CLAUSE_PRIVATE:
14687 case OMP_CLAUSE_SHARED:
14688 case OMP_CLAUSE_FIRSTPRIVATE:
14689 case OMP_CLAUSE_COPYIN:
14690 case OMP_CLAUSE_COPYPRIVATE:
14691 case OMP_CLAUSE_UNIFORM:
14692 case OMP_CLAUSE_DEPEND:
14693 case OMP_CLAUSE_FROM:
14694 case OMP_CLAUSE_TO:
14695 case OMP_CLAUSE_MAP:
14696 case OMP_CLAUSE_USE_DEVICE_PTR:
14697 case OMP_CLAUSE_IS_DEVICE_PTR:
14698 OMP_CLAUSE_DECL (nc)
14699 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14700 in_decl);
14701 break;
14702 case OMP_CLAUSE_IF:
14703 case OMP_CLAUSE_NUM_THREADS:
14704 case OMP_CLAUSE_SCHEDULE:
14705 case OMP_CLAUSE_COLLAPSE:
14706 case OMP_CLAUSE_FINAL:
14707 case OMP_CLAUSE_DEVICE:
14708 case OMP_CLAUSE_DIST_SCHEDULE:
14709 case OMP_CLAUSE_NUM_TEAMS:
14710 case OMP_CLAUSE_THREAD_LIMIT:
14711 case OMP_CLAUSE_SAFELEN:
14712 case OMP_CLAUSE_SIMDLEN:
14713 case OMP_CLAUSE_NUM_TASKS:
14714 case OMP_CLAUSE_GRAINSIZE:
14715 case OMP_CLAUSE_PRIORITY:
14716 case OMP_CLAUSE_ORDERED:
14717 case OMP_CLAUSE_HINT:
14718 case OMP_CLAUSE_NUM_GANGS:
14719 case OMP_CLAUSE_NUM_WORKERS:
14720 case OMP_CLAUSE_VECTOR_LENGTH:
14721 case OMP_CLAUSE_WORKER:
14722 case OMP_CLAUSE_VECTOR:
14723 case OMP_CLAUSE_ASYNC:
14724 case OMP_CLAUSE_WAIT:
14725 OMP_CLAUSE_OPERAND (nc, 0)
14726 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14727 in_decl, /*integral_constant_expression_p=*/false);
14728 break;
14729 case OMP_CLAUSE_REDUCTION:
14730 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14731 {
14732 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14733 if (TREE_CODE (placeholder) == SCOPE_REF)
14734 {
14735 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14736 complain, in_decl);
14737 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14738 = build_qualified_name (NULL_TREE, scope,
14739 TREE_OPERAND (placeholder, 1),
14740 false);
14741 }
14742 else
14743 gcc_assert (identifier_p (placeholder));
14744 }
14745 OMP_CLAUSE_DECL (nc)
14746 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14747 in_decl);
14748 break;
14749 case OMP_CLAUSE_GANG:
14750 case OMP_CLAUSE_ALIGNED:
14751 OMP_CLAUSE_DECL (nc)
14752 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14753 in_decl);
14754 OMP_CLAUSE_OPERAND (nc, 1)
14755 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14756 in_decl, /*integral_constant_expression_p=*/false);
14757 break;
14758 case OMP_CLAUSE_LINEAR:
14759 OMP_CLAUSE_DECL (nc)
14760 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14761 in_decl);
14762 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
14763 {
14764 gcc_assert (!linear_no_step);
14765 linear_no_step = nc;
14766 }
14767 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
14768 OMP_CLAUSE_LINEAR_STEP (nc)
14769 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
14770 complain, in_decl);
14771 else
14772 OMP_CLAUSE_LINEAR_STEP (nc)
14773 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
14774 in_decl,
14775 /*integral_constant_expression_p=*/false);
14776 break;
14777 case OMP_CLAUSE_NOWAIT:
14778 case OMP_CLAUSE_DEFAULT:
14779 case OMP_CLAUSE_UNTIED:
14780 case OMP_CLAUSE_MERGEABLE:
14781 case OMP_CLAUSE_INBRANCH:
14782 case OMP_CLAUSE_NOTINBRANCH:
14783 case OMP_CLAUSE_PROC_BIND:
14784 case OMP_CLAUSE_FOR:
14785 case OMP_CLAUSE_PARALLEL:
14786 case OMP_CLAUSE_SECTIONS:
14787 case OMP_CLAUSE_TASKGROUP:
14788 case OMP_CLAUSE_NOGROUP:
14789 case OMP_CLAUSE_THREADS:
14790 case OMP_CLAUSE_SIMD:
14791 case OMP_CLAUSE_DEFAULTMAP:
14792 case OMP_CLAUSE_INDEPENDENT:
14793 case OMP_CLAUSE_AUTO:
14794 case OMP_CLAUSE_SEQ:
14795 break;
14796 case OMP_CLAUSE_TILE:
14797 {
14798 tree lnc, loc;
14799 for (lnc = OMP_CLAUSE_TILE_LIST (nc),
14800 loc = OMP_CLAUSE_TILE_LIST (oc);
14801 loc;
14802 loc = TREE_CHAIN (loc), lnc = TREE_CHAIN (lnc))
14803 {
14804 TREE_VALUE (lnc) = tsubst_expr (TREE_VALUE (loc), args,
14805 complain, in_decl, false);
14806 }
14807 }
14808 break;
14809 default:
14810 gcc_unreachable ();
14811 }
14812 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
14813 switch (OMP_CLAUSE_CODE (nc))
14814 {
14815 case OMP_CLAUSE_SHARED:
14816 case OMP_CLAUSE_PRIVATE:
14817 case OMP_CLAUSE_FIRSTPRIVATE:
14818 case OMP_CLAUSE_LASTPRIVATE:
14819 case OMP_CLAUSE_COPYPRIVATE:
14820 case OMP_CLAUSE_LINEAR:
14821 case OMP_CLAUSE_REDUCTION:
14822 case OMP_CLAUSE_USE_DEVICE_PTR:
14823 case OMP_CLAUSE_IS_DEVICE_PTR:
14824 /* tsubst_expr on SCOPE_REF results in returning
14825 finish_non_static_data_member result. Undo that here. */
14826 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
14827 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
14828 == IDENTIFIER_NODE))
14829 {
14830 tree t = OMP_CLAUSE_DECL (nc);
14831 tree v = t;
14832 while (v)
14833 switch (TREE_CODE (v))
14834 {
14835 case COMPONENT_REF:
14836 case MEM_REF:
14837 case INDIRECT_REF:
14838 CASE_CONVERT:
14839 case POINTER_PLUS_EXPR:
14840 v = TREE_OPERAND (v, 0);
14841 continue;
14842 case PARM_DECL:
14843 if (DECL_CONTEXT (v) == current_function_decl
14844 && DECL_ARTIFICIAL (v)
14845 && DECL_NAME (v) == this_identifier)
14846 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
14847 /* FALLTHRU */
14848 default:
14849 v = NULL_TREE;
14850 break;
14851 }
14852 }
14853 else if (VAR_P (OMP_CLAUSE_DECL (oc))
14854 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
14855 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
14856 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
14857 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
14858 {
14859 tree decl = OMP_CLAUSE_DECL (nc);
14860 if (VAR_P (decl))
14861 {
14862 if (!DECL_LANG_SPECIFIC (decl))
14863 retrofit_lang_decl (decl);
14864 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
14865 }
14866 }
14867 break;
14868 default:
14869 break;
14870 }
14871 }
14872
14873 new_clauses = nreverse (new_clauses);
14874 if (ort != C_ORT_OMP_DECLARE_SIMD)
14875 {
14876 new_clauses = finish_omp_clauses (new_clauses, ort);
14877 if (linear_no_step)
14878 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
14879 if (nc == linear_no_step)
14880 {
14881 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
14882 break;
14883 }
14884 }
14885 return new_clauses;
14886 }
14887
14888 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14889
14890 static tree
14891 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14892 tree in_decl)
14893 {
14894 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14895
14896 tree purpose, value, chain;
14897
14898 if (t == NULL)
14899 return t;
14900
14901 if (TREE_CODE (t) != TREE_LIST)
14902 return tsubst_copy_and_build (t, args, complain, in_decl,
14903 /*function_p=*/false,
14904 /*integral_constant_expression_p=*/false);
14905
14906 if (t == void_list_node)
14907 return t;
14908
14909 purpose = TREE_PURPOSE (t);
14910 if (purpose)
14911 purpose = RECUR (purpose);
14912 value = TREE_VALUE (t);
14913 if (value)
14914 {
14915 if (TREE_CODE (value) != LABEL_DECL)
14916 value = RECUR (value);
14917 else
14918 {
14919 value = lookup_label (DECL_NAME (value));
14920 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14921 TREE_USED (value) = 1;
14922 }
14923 }
14924 chain = TREE_CHAIN (t);
14925 if (chain && chain != void_type_node)
14926 chain = RECUR (chain);
14927 return tree_cons (purpose, value, chain);
14928 #undef RECUR
14929 }
14930
14931 /* Used to temporarily communicate the list of #pragma omp parallel
14932 clauses to #pragma omp for instantiation if they are combined
14933 together. */
14934
14935 static tree *omp_parallel_combined_clauses;
14936
14937 /* Substitute one OMP_FOR iterator. */
14938
14939 static void
14940 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
14941 tree initv, tree condv, tree incrv, tree *clauses,
14942 tree args, tsubst_flags_t complain, tree in_decl,
14943 bool integral_constant_expression_p)
14944 {
14945 #define RECUR(NODE) \
14946 tsubst_expr ((NODE), args, complain, in_decl, \
14947 integral_constant_expression_p)
14948 tree decl, init, cond, incr;
14949
14950 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
14951 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
14952
14953 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
14954 {
14955 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
14956 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
14957 }
14958
14959 decl = TREE_OPERAND (init, 0);
14960 init = TREE_OPERAND (init, 1);
14961 tree decl_expr = NULL_TREE;
14962 if (init && TREE_CODE (init) == DECL_EXPR)
14963 {
14964 /* We need to jump through some hoops to handle declarations in the
14965 for-init-statement, since we might need to handle auto deduction,
14966 but we need to keep control of initialization. */
14967 decl_expr = init;
14968 init = DECL_INITIAL (DECL_EXPR_DECL (init));
14969 decl = tsubst_decl (decl, args, complain);
14970 }
14971 else
14972 {
14973 if (TREE_CODE (decl) == SCOPE_REF)
14974 {
14975 decl = RECUR (decl);
14976 if (TREE_CODE (decl) == COMPONENT_REF)
14977 {
14978 tree v = decl;
14979 while (v)
14980 switch (TREE_CODE (v))
14981 {
14982 case COMPONENT_REF:
14983 case MEM_REF:
14984 case INDIRECT_REF:
14985 CASE_CONVERT:
14986 case POINTER_PLUS_EXPR:
14987 v = TREE_OPERAND (v, 0);
14988 continue;
14989 case PARM_DECL:
14990 if (DECL_CONTEXT (v) == current_function_decl
14991 && DECL_ARTIFICIAL (v)
14992 && DECL_NAME (v) == this_identifier)
14993 {
14994 decl = TREE_OPERAND (decl, 1);
14995 decl = omp_privatize_field (decl, false);
14996 }
14997 /* FALLTHRU */
14998 default:
14999 v = NULL_TREE;
15000 break;
15001 }
15002 }
15003 }
15004 else
15005 decl = RECUR (decl);
15006 }
15007 init = RECUR (init);
15008
15009 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15010 if (auto_node && init)
15011 TREE_TYPE (decl)
15012 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15013
15014 gcc_assert (!type_dependent_expression_p (decl));
15015
15016 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15017 {
15018 if (decl_expr)
15019 {
15020 /* Declare the variable, but don't let that initialize it. */
15021 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15022 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15023 RECUR (decl_expr);
15024 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15025 }
15026
15027 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15028 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15029 if (TREE_CODE (incr) == MODIFY_EXPR)
15030 {
15031 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15032 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15033 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15034 NOP_EXPR, rhs, complain);
15035 }
15036 else
15037 incr = RECUR (incr);
15038 TREE_VEC_ELT (declv, i) = decl;
15039 TREE_VEC_ELT (initv, i) = init;
15040 TREE_VEC_ELT (condv, i) = cond;
15041 TREE_VEC_ELT (incrv, i) = incr;
15042 return;
15043 }
15044
15045 if (decl_expr)
15046 {
15047 /* Declare and initialize the variable. */
15048 RECUR (decl_expr);
15049 init = NULL_TREE;
15050 }
15051 else if (init)
15052 {
15053 tree *pc;
15054 int j;
15055 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15056 {
15057 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15058 {
15059 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15060 && OMP_CLAUSE_DECL (*pc) == decl)
15061 break;
15062 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15063 && OMP_CLAUSE_DECL (*pc) == decl)
15064 {
15065 if (j)
15066 break;
15067 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15068 tree c = *pc;
15069 *pc = OMP_CLAUSE_CHAIN (c);
15070 OMP_CLAUSE_CHAIN (c) = *clauses;
15071 *clauses = c;
15072 }
15073 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15074 && OMP_CLAUSE_DECL (*pc) == decl)
15075 {
15076 error ("iteration variable %qD should not be firstprivate",
15077 decl);
15078 *pc = OMP_CLAUSE_CHAIN (*pc);
15079 }
15080 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15081 && OMP_CLAUSE_DECL (*pc) == decl)
15082 {
15083 error ("iteration variable %qD should not be reduction",
15084 decl);
15085 *pc = OMP_CLAUSE_CHAIN (*pc);
15086 }
15087 else
15088 pc = &OMP_CLAUSE_CHAIN (*pc);
15089 }
15090 if (*pc)
15091 break;
15092 }
15093 if (*pc == NULL_TREE)
15094 {
15095 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15096 OMP_CLAUSE_DECL (c) = decl;
15097 c = finish_omp_clauses (c, C_ORT_OMP);
15098 if (c)
15099 {
15100 OMP_CLAUSE_CHAIN (c) = *clauses;
15101 *clauses = c;
15102 }
15103 }
15104 }
15105 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15106 if (COMPARISON_CLASS_P (cond))
15107 {
15108 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15109 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15110 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15111 }
15112 else
15113 cond = RECUR (cond);
15114 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15115 switch (TREE_CODE (incr))
15116 {
15117 case PREINCREMENT_EXPR:
15118 case PREDECREMENT_EXPR:
15119 case POSTINCREMENT_EXPR:
15120 case POSTDECREMENT_EXPR:
15121 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15122 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15123 break;
15124 case MODIFY_EXPR:
15125 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15126 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15127 {
15128 tree rhs = TREE_OPERAND (incr, 1);
15129 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15130 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15131 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15132 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15133 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15134 rhs0, rhs1));
15135 }
15136 else
15137 incr = RECUR (incr);
15138 break;
15139 case MODOP_EXPR:
15140 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15141 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15142 {
15143 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15144 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15145 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15146 TREE_TYPE (decl), lhs,
15147 RECUR (TREE_OPERAND (incr, 2))));
15148 }
15149 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15150 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15151 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15152 {
15153 tree rhs = TREE_OPERAND (incr, 2);
15154 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15155 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15156 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15157 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15158 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15159 rhs0, rhs1));
15160 }
15161 else
15162 incr = RECUR (incr);
15163 break;
15164 default:
15165 incr = RECUR (incr);
15166 break;
15167 }
15168
15169 TREE_VEC_ELT (declv, i) = decl;
15170 TREE_VEC_ELT (initv, i) = init;
15171 TREE_VEC_ELT (condv, i) = cond;
15172 TREE_VEC_ELT (incrv, i) = incr;
15173 #undef RECUR
15174 }
15175
15176 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15177 of OMP_TARGET's body. */
15178
15179 static tree
15180 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15181 {
15182 *walk_subtrees = 0;
15183 switch (TREE_CODE (*tp))
15184 {
15185 case OMP_TEAMS:
15186 return *tp;
15187 case BIND_EXPR:
15188 case STATEMENT_LIST:
15189 *walk_subtrees = 1;
15190 break;
15191 default:
15192 break;
15193 }
15194 return NULL_TREE;
15195 }
15196
15197 /* Like tsubst_copy for expressions, etc. but also does semantic
15198 processing. */
15199
15200 tree
15201 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15202 bool integral_constant_expression_p)
15203 {
15204 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15205 #define RECUR(NODE) \
15206 tsubst_expr ((NODE), args, complain, in_decl, \
15207 integral_constant_expression_p)
15208
15209 tree stmt, tmp;
15210 tree r;
15211 location_t loc;
15212
15213 if (t == NULL_TREE || t == error_mark_node)
15214 return t;
15215
15216 loc = input_location;
15217 if (EXPR_HAS_LOCATION (t))
15218 input_location = EXPR_LOCATION (t);
15219 if (STATEMENT_CODE_P (TREE_CODE (t)))
15220 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15221
15222 switch (TREE_CODE (t))
15223 {
15224 case STATEMENT_LIST:
15225 {
15226 tree_stmt_iterator i;
15227 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15228 RECUR (tsi_stmt (i));
15229 break;
15230 }
15231
15232 case CTOR_INITIALIZER:
15233 finish_mem_initializers (tsubst_initializer_list
15234 (TREE_OPERAND (t, 0), args));
15235 break;
15236
15237 case RETURN_EXPR:
15238 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15239 break;
15240
15241 case EXPR_STMT:
15242 tmp = RECUR (EXPR_STMT_EXPR (t));
15243 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15244 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15245 else
15246 finish_expr_stmt (tmp);
15247 break;
15248
15249 case USING_STMT:
15250 do_using_directive (USING_STMT_NAMESPACE (t));
15251 break;
15252
15253 case DECL_EXPR:
15254 {
15255 tree decl, pattern_decl;
15256 tree init;
15257
15258 pattern_decl = decl = DECL_EXPR_DECL (t);
15259 if (TREE_CODE (decl) == LABEL_DECL)
15260 finish_label_decl (DECL_NAME (decl));
15261 else if (TREE_CODE (decl) == USING_DECL)
15262 {
15263 tree scope = USING_DECL_SCOPE (decl);
15264 tree name = DECL_NAME (decl);
15265
15266 scope = tsubst (scope, args, complain, in_decl);
15267 decl = lookup_qualified_name (scope, name,
15268 /*is_type_p=*/false,
15269 /*complain=*/false);
15270 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15271 qualified_name_lookup_error (scope, name, decl, input_location);
15272 else
15273 do_local_using_decl (decl, scope, name);
15274 }
15275 else if (DECL_PACK_P (decl))
15276 {
15277 /* Don't build up decls for a variadic capture proxy, we'll
15278 instantiate the elements directly as needed. */
15279 break;
15280 }
15281 else
15282 {
15283 init = DECL_INITIAL (decl);
15284 decl = tsubst (decl, args, complain, in_decl);
15285 if (decl != error_mark_node)
15286 {
15287 /* By marking the declaration as instantiated, we avoid
15288 trying to instantiate it. Since instantiate_decl can't
15289 handle local variables, and since we've already done
15290 all that needs to be done, that's the right thing to
15291 do. */
15292 if (VAR_P (decl))
15293 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15294 if (VAR_P (decl)
15295 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15296 /* Anonymous aggregates are a special case. */
15297 finish_anon_union (decl);
15298 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15299 {
15300 DECL_CONTEXT (decl) = current_function_decl;
15301 if (DECL_NAME (decl) == this_identifier)
15302 {
15303 tree lam = DECL_CONTEXT (current_function_decl);
15304 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15305 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15306 }
15307 insert_capture_proxy (decl);
15308 }
15309 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15310 /* We already did a pushtag. */;
15311 else if (TREE_CODE (decl) == FUNCTION_DECL
15312 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15313 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15314 {
15315 DECL_CONTEXT (decl) = NULL_TREE;
15316 pushdecl (decl);
15317 DECL_CONTEXT (decl) = current_function_decl;
15318 cp_check_omp_declare_reduction (decl);
15319 }
15320 else
15321 {
15322 int const_init = false;
15323 maybe_push_decl (decl);
15324 if (VAR_P (decl)
15325 && DECL_PRETTY_FUNCTION_P (decl))
15326 {
15327 /* For __PRETTY_FUNCTION__ we have to adjust the
15328 initializer. */
15329 const char *const name
15330 = cxx_printable_name (current_function_decl, 2);
15331 init = cp_fname_init (name, &TREE_TYPE (decl));
15332 }
15333 else
15334 init = tsubst_init (init, decl, args, complain, in_decl);
15335
15336 if (VAR_P (decl))
15337 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15338 (pattern_decl));
15339 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15340 }
15341 }
15342 }
15343
15344 break;
15345 }
15346
15347 case FOR_STMT:
15348 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15349 RECUR (FOR_INIT_STMT (t));
15350 finish_for_init_stmt (stmt);
15351 tmp = RECUR (FOR_COND (t));
15352 finish_for_cond (tmp, stmt, false);
15353 tmp = RECUR (FOR_EXPR (t));
15354 finish_for_expr (tmp, stmt);
15355 RECUR (FOR_BODY (t));
15356 finish_for_stmt (stmt);
15357 break;
15358
15359 case RANGE_FOR_STMT:
15360 {
15361 tree decl, expr;
15362 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15363 decl = RANGE_FOR_DECL (t);
15364 decl = tsubst (decl, args, complain, in_decl);
15365 maybe_push_decl (decl);
15366 expr = RECUR (RANGE_FOR_EXPR (t));
15367 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
15368 RECUR (RANGE_FOR_BODY (t));
15369 finish_for_stmt (stmt);
15370 }
15371 break;
15372
15373 case WHILE_STMT:
15374 stmt = begin_while_stmt ();
15375 tmp = RECUR (WHILE_COND (t));
15376 finish_while_stmt_cond (tmp, stmt, false);
15377 RECUR (WHILE_BODY (t));
15378 finish_while_stmt (stmt);
15379 break;
15380
15381 case DO_STMT:
15382 stmt = begin_do_stmt ();
15383 RECUR (DO_BODY (t));
15384 finish_do_body (stmt);
15385 tmp = RECUR (DO_COND (t));
15386 finish_do_stmt (tmp, stmt, false);
15387 break;
15388
15389 case IF_STMT:
15390 stmt = begin_if_stmt ();
15391 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15392 tmp = RECUR (IF_COND (t));
15393 tmp = finish_if_stmt_cond (tmp, stmt);
15394 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15395 /* Don't instantiate the THEN_CLAUSE. */;
15396 else
15397 RECUR (THEN_CLAUSE (t));
15398 finish_then_clause (stmt);
15399
15400 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15401 /* Don't instantiate the ELSE_CLAUSE. */;
15402 else if (ELSE_CLAUSE (t))
15403 {
15404 begin_else_clause (stmt);
15405 RECUR (ELSE_CLAUSE (t));
15406 finish_else_clause (stmt);
15407 }
15408
15409 finish_if_stmt (stmt);
15410 break;
15411
15412 case BIND_EXPR:
15413 if (BIND_EXPR_BODY_BLOCK (t))
15414 stmt = begin_function_body ();
15415 else
15416 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15417 ? BCS_TRY_BLOCK : 0);
15418
15419 RECUR (BIND_EXPR_BODY (t));
15420
15421 if (BIND_EXPR_BODY_BLOCK (t))
15422 finish_function_body (stmt);
15423 else
15424 finish_compound_stmt (stmt);
15425 break;
15426
15427 case BREAK_STMT:
15428 finish_break_stmt ();
15429 break;
15430
15431 case CONTINUE_STMT:
15432 finish_continue_stmt ();
15433 break;
15434
15435 case SWITCH_STMT:
15436 stmt = begin_switch_stmt ();
15437 tmp = RECUR (SWITCH_STMT_COND (t));
15438 finish_switch_cond (tmp, stmt);
15439 RECUR (SWITCH_STMT_BODY (t));
15440 finish_switch_stmt (stmt);
15441 break;
15442
15443 case CASE_LABEL_EXPR:
15444 {
15445 tree low = RECUR (CASE_LOW (t));
15446 tree high = RECUR (CASE_HIGH (t));
15447 finish_case_label (EXPR_LOCATION (t), low, high);
15448 }
15449 break;
15450
15451 case LABEL_EXPR:
15452 {
15453 tree decl = LABEL_EXPR_LABEL (t);
15454 tree label;
15455
15456 label = finish_label_stmt (DECL_NAME (decl));
15457 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15458 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15459 }
15460 break;
15461
15462 case GOTO_EXPR:
15463 tmp = GOTO_DESTINATION (t);
15464 if (TREE_CODE (tmp) != LABEL_DECL)
15465 /* Computed goto's must be tsubst'd into. On the other hand,
15466 non-computed gotos must not be; the identifier in question
15467 will have no binding. */
15468 tmp = RECUR (tmp);
15469 else
15470 tmp = DECL_NAME (tmp);
15471 finish_goto_stmt (tmp);
15472 break;
15473
15474 case ASM_EXPR:
15475 {
15476 tree string = RECUR (ASM_STRING (t));
15477 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15478 complain, in_decl);
15479 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15480 complain, in_decl);
15481 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15482 complain, in_decl);
15483 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15484 complain, in_decl);
15485 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15486 clobbers, labels);
15487 tree asm_expr = tmp;
15488 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15489 asm_expr = TREE_OPERAND (asm_expr, 0);
15490 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15491 }
15492 break;
15493
15494 case TRY_BLOCK:
15495 if (CLEANUP_P (t))
15496 {
15497 stmt = begin_try_block ();
15498 RECUR (TRY_STMTS (t));
15499 finish_cleanup_try_block (stmt);
15500 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15501 }
15502 else
15503 {
15504 tree compound_stmt = NULL_TREE;
15505
15506 if (FN_TRY_BLOCK_P (t))
15507 stmt = begin_function_try_block (&compound_stmt);
15508 else
15509 stmt = begin_try_block ();
15510
15511 RECUR (TRY_STMTS (t));
15512
15513 if (FN_TRY_BLOCK_P (t))
15514 finish_function_try_block (stmt);
15515 else
15516 finish_try_block (stmt);
15517
15518 RECUR (TRY_HANDLERS (t));
15519 if (FN_TRY_BLOCK_P (t))
15520 finish_function_handler_sequence (stmt, compound_stmt);
15521 else
15522 finish_handler_sequence (stmt);
15523 }
15524 break;
15525
15526 case HANDLER:
15527 {
15528 tree decl = HANDLER_PARMS (t);
15529
15530 if (decl)
15531 {
15532 decl = tsubst (decl, args, complain, in_decl);
15533 /* Prevent instantiate_decl from trying to instantiate
15534 this variable. We've already done all that needs to be
15535 done. */
15536 if (decl != error_mark_node)
15537 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15538 }
15539 stmt = begin_handler ();
15540 finish_handler_parms (decl, stmt);
15541 RECUR (HANDLER_BODY (t));
15542 finish_handler (stmt);
15543 }
15544 break;
15545
15546 case TAG_DEFN:
15547 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
15548 if (CLASS_TYPE_P (tmp))
15549 {
15550 /* Local classes are not independent templates; they are
15551 instantiated along with their containing function. And this
15552 way we don't have to deal with pushing out of one local class
15553 to instantiate a member of another local class. */
15554 tree fn;
15555 /* Closures are handled by the LAMBDA_EXPR. */
15556 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
15557 complete_type (tmp);
15558 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
15559 if (!DECL_ARTIFICIAL (fn))
15560 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
15561 }
15562 break;
15563
15564 case STATIC_ASSERT:
15565 {
15566 tree condition;
15567
15568 ++c_inhibit_evaluation_warnings;
15569 condition =
15570 tsubst_expr (STATIC_ASSERT_CONDITION (t),
15571 args,
15572 complain, in_decl,
15573 /*integral_constant_expression_p=*/true);
15574 --c_inhibit_evaluation_warnings;
15575
15576 finish_static_assert (condition,
15577 STATIC_ASSERT_MESSAGE (t),
15578 STATIC_ASSERT_SOURCE_LOCATION (t),
15579 /*member_p=*/false);
15580 }
15581 break;
15582
15583 case OACC_KERNELS:
15584 case OACC_PARALLEL:
15585 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
15586 in_decl);
15587 stmt = begin_omp_parallel ();
15588 RECUR (OMP_BODY (t));
15589 finish_omp_construct (TREE_CODE (t), stmt, tmp);
15590 break;
15591
15592 case OMP_PARALLEL:
15593 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
15594 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
15595 complain, in_decl);
15596 if (OMP_PARALLEL_COMBINED (t))
15597 omp_parallel_combined_clauses = &tmp;
15598 stmt = begin_omp_parallel ();
15599 RECUR (OMP_PARALLEL_BODY (t));
15600 gcc_assert (omp_parallel_combined_clauses == NULL);
15601 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15602 = OMP_PARALLEL_COMBINED (t);
15603 pop_omp_privatization_clauses (r);
15604 break;
15605
15606 case OMP_TASK:
15607 r = push_omp_privatization_clauses (false);
15608 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
15609 complain, in_decl);
15610 stmt = begin_omp_task ();
15611 RECUR (OMP_TASK_BODY (t));
15612 finish_omp_task (tmp, stmt);
15613 pop_omp_privatization_clauses (r);
15614 break;
15615
15616 case OMP_FOR:
15617 case OMP_SIMD:
15618 case CILK_SIMD:
15619 case CILK_FOR:
15620 case OMP_DISTRIBUTE:
15621 case OMP_TASKLOOP:
15622 case OACC_LOOP:
15623 {
15624 tree clauses, body, pre_body;
15625 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15626 tree orig_declv = NULL_TREE;
15627 tree incrv = NULL_TREE;
15628 enum c_omp_region_type ort = C_ORT_OMP;
15629 int i;
15630
15631 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
15632 ort = C_ORT_CILK;
15633 else if (TREE_CODE (t) == OACC_LOOP)
15634 ort = C_ORT_ACC;
15635
15636 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
15637 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
15638 in_decl);
15639 if (OMP_FOR_INIT (t) != NULL_TREE)
15640 {
15641 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15642 if (OMP_FOR_ORIG_DECLS (t))
15643 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15644 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15645 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15646 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15647 }
15648
15649 stmt = begin_omp_structured_block ();
15650
15651 pre_body = push_stmt_list ();
15652 RECUR (OMP_FOR_PRE_BODY (t));
15653 pre_body = pop_stmt_list (pre_body);
15654
15655 if (OMP_FOR_INIT (t) != NULL_TREE)
15656 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15657 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
15658 incrv, &clauses, args, complain, in_decl,
15659 integral_constant_expression_p);
15660 omp_parallel_combined_clauses = NULL;
15661
15662 body = push_stmt_list ();
15663 RECUR (OMP_FOR_BODY (t));
15664 body = pop_stmt_list (body);
15665
15666 if (OMP_FOR_INIT (t) != NULL_TREE)
15667 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
15668 orig_declv, initv, condv, incrv, body, pre_body,
15669 NULL, clauses);
15670 else
15671 {
15672 t = make_node (TREE_CODE (t));
15673 TREE_TYPE (t) = void_type_node;
15674 OMP_FOR_BODY (t) = body;
15675 OMP_FOR_PRE_BODY (t) = pre_body;
15676 OMP_FOR_CLAUSES (t) = clauses;
15677 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15678 add_stmt (t);
15679 }
15680
15681 add_stmt (finish_omp_structured_block (stmt));
15682 pop_omp_privatization_clauses (r);
15683 }
15684 break;
15685
15686 case OMP_SECTIONS:
15687 omp_parallel_combined_clauses = NULL;
15688 /* FALLTHRU */
15689 case OMP_SINGLE:
15690 case OMP_TEAMS:
15691 case OMP_CRITICAL:
15692 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
15693 && OMP_TEAMS_COMBINED (t));
15694 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
15695 in_decl);
15696 stmt = push_stmt_list ();
15697 RECUR (OMP_BODY (t));
15698 stmt = pop_stmt_list (stmt);
15699
15700 t = copy_node (t);
15701 OMP_BODY (t) = stmt;
15702 OMP_CLAUSES (t) = tmp;
15703 add_stmt (t);
15704 pop_omp_privatization_clauses (r);
15705 break;
15706
15707 case OACC_DATA:
15708 case OMP_TARGET_DATA:
15709 case OMP_TARGET:
15710 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
15711 ? C_ORT_ACC : C_ORT_OMP, args, complain,
15712 in_decl);
15713 keep_next_level (true);
15714 stmt = begin_omp_structured_block ();
15715
15716 RECUR (OMP_BODY (t));
15717 stmt = finish_omp_structured_block (stmt);
15718
15719 t = copy_node (t);
15720 OMP_BODY (t) = stmt;
15721 OMP_CLAUSES (t) = tmp;
15722 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
15723 {
15724 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
15725 if (teams)
15726 {
15727 /* For combined target teams, ensure the num_teams and
15728 thread_limit clause expressions are evaluated on the host,
15729 before entering the target construct. */
15730 tree c;
15731 for (c = OMP_TEAMS_CLAUSES (teams);
15732 c; c = OMP_CLAUSE_CHAIN (c))
15733 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15734 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15735 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15736 {
15737 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15738 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
15739 if (expr == error_mark_node)
15740 continue;
15741 tmp = TARGET_EXPR_SLOT (expr);
15742 add_stmt (expr);
15743 OMP_CLAUSE_OPERAND (c, 0) = expr;
15744 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15745 OMP_CLAUSE_FIRSTPRIVATE);
15746 OMP_CLAUSE_DECL (tc) = tmp;
15747 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
15748 OMP_TARGET_CLAUSES (t) = tc;
15749 }
15750 }
15751 }
15752 add_stmt (t);
15753 break;
15754
15755 case OACC_DECLARE:
15756 t = copy_node (t);
15757 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
15758 complain, in_decl);
15759 OACC_DECLARE_CLAUSES (t) = tmp;
15760 add_stmt (t);
15761 break;
15762
15763 case OMP_TARGET_UPDATE:
15764 case OMP_TARGET_ENTER_DATA:
15765 case OMP_TARGET_EXIT_DATA:
15766 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
15767 complain, in_decl);
15768 t = copy_node (t);
15769 OMP_STANDALONE_CLAUSES (t) = tmp;
15770 add_stmt (t);
15771 break;
15772
15773 case OACC_ENTER_DATA:
15774 case OACC_EXIT_DATA:
15775 case OACC_UPDATE:
15776 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
15777 complain, in_decl);
15778 t = copy_node (t);
15779 OMP_STANDALONE_CLAUSES (t) = tmp;
15780 add_stmt (t);
15781 break;
15782
15783 case OMP_ORDERED:
15784 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
15785 complain, in_decl);
15786 stmt = push_stmt_list ();
15787 RECUR (OMP_BODY (t));
15788 stmt = pop_stmt_list (stmt);
15789
15790 t = copy_node (t);
15791 OMP_BODY (t) = stmt;
15792 OMP_ORDERED_CLAUSES (t) = tmp;
15793 add_stmt (t);
15794 break;
15795
15796 case OMP_SECTION:
15797 case OMP_MASTER:
15798 case OMP_TASKGROUP:
15799 stmt = push_stmt_list ();
15800 RECUR (OMP_BODY (t));
15801 stmt = pop_stmt_list (stmt);
15802
15803 t = copy_node (t);
15804 OMP_BODY (t) = stmt;
15805 add_stmt (t);
15806 break;
15807
15808 case OMP_ATOMIC:
15809 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
15810 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
15811 {
15812 tree op1 = TREE_OPERAND (t, 1);
15813 tree rhs1 = NULL_TREE;
15814 tree lhs, rhs;
15815 if (TREE_CODE (op1) == COMPOUND_EXPR)
15816 {
15817 rhs1 = RECUR (TREE_OPERAND (op1, 0));
15818 op1 = TREE_OPERAND (op1, 1);
15819 }
15820 lhs = RECUR (TREE_OPERAND (op1, 0));
15821 rhs = RECUR (TREE_OPERAND (op1, 1));
15822 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
15823 NULL_TREE, NULL_TREE, rhs1,
15824 OMP_ATOMIC_SEQ_CST (t));
15825 }
15826 else
15827 {
15828 tree op1 = TREE_OPERAND (t, 1);
15829 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
15830 tree rhs1 = NULL_TREE;
15831 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
15832 enum tree_code opcode = NOP_EXPR;
15833 if (code == OMP_ATOMIC_READ)
15834 {
15835 v = RECUR (TREE_OPERAND (op1, 0));
15836 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15837 }
15838 else if (code == OMP_ATOMIC_CAPTURE_OLD
15839 || code == OMP_ATOMIC_CAPTURE_NEW)
15840 {
15841 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
15842 v = RECUR (TREE_OPERAND (op1, 0));
15843 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15844 if (TREE_CODE (op11) == COMPOUND_EXPR)
15845 {
15846 rhs1 = RECUR (TREE_OPERAND (op11, 0));
15847 op11 = TREE_OPERAND (op11, 1);
15848 }
15849 lhs = RECUR (TREE_OPERAND (op11, 0));
15850 rhs = RECUR (TREE_OPERAND (op11, 1));
15851 opcode = TREE_CODE (op11);
15852 if (opcode == MODIFY_EXPR)
15853 opcode = NOP_EXPR;
15854 }
15855 else
15856 {
15857 code = OMP_ATOMIC;
15858 lhs = RECUR (TREE_OPERAND (op1, 0));
15859 rhs = RECUR (TREE_OPERAND (op1, 1));
15860 }
15861 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
15862 OMP_ATOMIC_SEQ_CST (t));
15863 }
15864 break;
15865
15866 case TRANSACTION_EXPR:
15867 {
15868 int flags = 0;
15869 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
15870 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
15871
15872 if (TRANSACTION_EXPR_IS_STMT (t))
15873 {
15874 tree body = TRANSACTION_EXPR_BODY (t);
15875 tree noex = NULL_TREE;
15876 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
15877 {
15878 noex = MUST_NOT_THROW_COND (body);
15879 if (noex == NULL_TREE)
15880 noex = boolean_true_node;
15881 body = TREE_OPERAND (body, 0);
15882 }
15883 stmt = begin_transaction_stmt (input_location, NULL, flags);
15884 RECUR (body);
15885 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
15886 }
15887 else
15888 {
15889 stmt = build_transaction_expr (EXPR_LOCATION (t),
15890 RECUR (TRANSACTION_EXPR_BODY (t)),
15891 flags, NULL_TREE);
15892 RETURN (stmt);
15893 }
15894 }
15895 break;
15896
15897 case MUST_NOT_THROW_EXPR:
15898 {
15899 tree op0 = RECUR (TREE_OPERAND (t, 0));
15900 tree cond = RECUR (MUST_NOT_THROW_COND (t));
15901 RETURN (build_must_not_throw_expr (op0, cond));
15902 }
15903
15904 case EXPR_PACK_EXPANSION:
15905 error ("invalid use of pack expansion expression");
15906 RETURN (error_mark_node);
15907
15908 case NONTYPE_ARGUMENT_PACK:
15909 error ("use %<...%> to expand argument pack");
15910 RETURN (error_mark_node);
15911
15912 case CILK_SPAWN_STMT:
15913 cfun->calls_cilk_spawn = 1;
15914 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
15915
15916 case CILK_SYNC_STMT:
15917 RETURN (build_cilk_sync ());
15918
15919 case COMPOUND_EXPR:
15920 tmp = RECUR (TREE_OPERAND (t, 0));
15921 if (tmp == NULL_TREE)
15922 /* If the first operand was a statement, we're done with it. */
15923 RETURN (RECUR (TREE_OPERAND (t, 1)));
15924 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
15925 RECUR (TREE_OPERAND (t, 1)),
15926 complain));
15927
15928 case ANNOTATE_EXPR:
15929 tmp = RECUR (TREE_OPERAND (t, 0));
15930 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
15931 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
15932
15933 default:
15934 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
15935
15936 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
15937 /*function_p=*/false,
15938 integral_constant_expression_p));
15939 }
15940
15941 RETURN (NULL_TREE);
15942 out:
15943 input_location = loc;
15944 return r;
15945 #undef RECUR
15946 #undef RETURN
15947 }
15948
15949 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15950 function. For description of the body see comment above
15951 cp_parser_omp_declare_reduction_exprs. */
15952
15953 static void
15954 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15955 {
15956 if (t == NULL_TREE || t == error_mark_node)
15957 return;
15958
15959 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
15960
15961 tree_stmt_iterator tsi;
15962 int i;
15963 tree stmts[7];
15964 memset (stmts, 0, sizeof stmts);
15965 for (i = 0, tsi = tsi_start (t);
15966 i < 7 && !tsi_end_p (tsi);
15967 i++, tsi_next (&tsi))
15968 stmts[i] = tsi_stmt (tsi);
15969 gcc_assert (tsi_end_p (tsi));
15970
15971 if (i >= 3)
15972 {
15973 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
15974 && TREE_CODE (stmts[1]) == DECL_EXPR);
15975 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
15976 args, complain, in_decl);
15977 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
15978 args, complain, in_decl);
15979 DECL_CONTEXT (omp_out) = current_function_decl;
15980 DECL_CONTEXT (omp_in) = current_function_decl;
15981 keep_next_level (true);
15982 tree block = begin_omp_structured_block ();
15983 tsubst_expr (stmts[2], args, complain, in_decl, false);
15984 block = finish_omp_structured_block (block);
15985 block = maybe_cleanup_point_expr_void (block);
15986 add_decl_expr (omp_out);
15987 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
15988 TREE_NO_WARNING (omp_out) = 1;
15989 add_decl_expr (omp_in);
15990 finish_expr_stmt (block);
15991 }
15992 if (i >= 6)
15993 {
15994 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
15995 && TREE_CODE (stmts[4]) == DECL_EXPR);
15996 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
15997 args, complain, in_decl);
15998 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
15999 args, complain, in_decl);
16000 DECL_CONTEXT (omp_priv) = current_function_decl;
16001 DECL_CONTEXT (omp_orig) = current_function_decl;
16002 keep_next_level (true);
16003 tree block = begin_omp_structured_block ();
16004 tsubst_expr (stmts[5], args, complain, in_decl, false);
16005 block = finish_omp_structured_block (block);
16006 block = maybe_cleanup_point_expr_void (block);
16007 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16008 add_decl_expr (omp_priv);
16009 add_decl_expr (omp_orig);
16010 finish_expr_stmt (block);
16011 if (i == 7)
16012 add_decl_expr (omp_orig);
16013 }
16014 }
16015
16016 /* T is a postfix-expression that is not being used in a function
16017 call. Return the substituted version of T. */
16018
16019 static tree
16020 tsubst_non_call_postfix_expression (tree t, tree args,
16021 tsubst_flags_t complain,
16022 tree in_decl)
16023 {
16024 if (TREE_CODE (t) == SCOPE_REF)
16025 t = tsubst_qualified_id (t, args, complain, in_decl,
16026 /*done=*/false, /*address_p=*/false);
16027 else
16028 t = tsubst_copy_and_build (t, args, complain, in_decl,
16029 /*function_p=*/false,
16030 /*integral_constant_expression_p=*/false);
16031
16032 return t;
16033 }
16034
16035 /* Like tsubst but deals with expressions and performs semantic
16036 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16037
16038 tree
16039 tsubst_copy_and_build (tree t,
16040 tree args,
16041 tsubst_flags_t complain,
16042 tree in_decl,
16043 bool function_p,
16044 bool integral_constant_expression_p)
16045 {
16046 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16047 #define RECUR(NODE) \
16048 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16049 /*function_p=*/false, \
16050 integral_constant_expression_p)
16051
16052 tree retval, op1;
16053 location_t loc;
16054
16055 if (t == NULL_TREE || t == error_mark_node)
16056 return t;
16057
16058 loc = input_location;
16059 if (EXPR_HAS_LOCATION (t))
16060 input_location = EXPR_LOCATION (t);
16061
16062 /* N3276 decltype magic only applies to calls at the top level or on the
16063 right side of a comma. */
16064 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16065 complain &= ~tf_decltype;
16066
16067 switch (TREE_CODE (t))
16068 {
16069 case USING_DECL:
16070 t = DECL_NAME (t);
16071 /* Fall through. */
16072 case IDENTIFIER_NODE:
16073 {
16074 tree decl;
16075 cp_id_kind idk;
16076 bool non_integral_constant_expression_p;
16077 const char *error_msg;
16078
16079 if (IDENTIFIER_TYPENAME_P (t))
16080 {
16081 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16082 t = mangle_conv_op_name_for_type (new_type);
16083 }
16084
16085 /* Look up the name. */
16086 decl = lookup_name (t);
16087
16088 /* By convention, expressions use ERROR_MARK_NODE to indicate
16089 failure, not NULL_TREE. */
16090 if (decl == NULL_TREE)
16091 decl = error_mark_node;
16092
16093 decl = finish_id_expression (t, decl, NULL_TREE,
16094 &idk,
16095 integral_constant_expression_p,
16096 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16097 &non_integral_constant_expression_p,
16098 /*template_p=*/false,
16099 /*done=*/true,
16100 /*address_p=*/false,
16101 /*template_arg_p=*/false,
16102 &error_msg,
16103 input_location);
16104 if (error_msg)
16105 error (error_msg);
16106 if (!function_p && identifier_p (decl))
16107 {
16108 if (complain & tf_error)
16109 unqualified_name_lookup_error (decl);
16110 decl = error_mark_node;
16111 }
16112 RETURN (decl);
16113 }
16114
16115 case TEMPLATE_ID_EXPR:
16116 {
16117 tree object;
16118 tree templ = RECUR (TREE_OPERAND (t, 0));
16119 tree targs = TREE_OPERAND (t, 1);
16120
16121 if (targs)
16122 targs = tsubst_template_args (targs, args, complain, in_decl);
16123 if (targs == error_mark_node)
16124 return error_mark_node;
16125
16126 if (variable_template_p (templ))
16127 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16128
16129 if (TREE_CODE (templ) == COMPONENT_REF)
16130 {
16131 object = TREE_OPERAND (templ, 0);
16132 templ = TREE_OPERAND (templ, 1);
16133 }
16134 else
16135 object = NULL_TREE;
16136 templ = lookup_template_function (templ, targs);
16137
16138 if (object)
16139 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16140 object, templ, NULL_TREE));
16141 else
16142 RETURN (baselink_for_fns (templ));
16143 }
16144
16145 case INDIRECT_REF:
16146 {
16147 tree r = RECUR (TREE_OPERAND (t, 0));
16148
16149 if (REFERENCE_REF_P (t))
16150 {
16151 /* A type conversion to reference type will be enclosed in
16152 such an indirect ref, but the substitution of the cast
16153 will have also added such an indirect ref. */
16154 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16155 r = convert_from_reference (r);
16156 }
16157 else
16158 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16159 complain|decltype_flag);
16160
16161 if (TREE_CODE (r) == INDIRECT_REF)
16162 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16163
16164 RETURN (r);
16165 }
16166
16167 case NOP_EXPR:
16168 {
16169 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16170 tree op0 = RECUR (TREE_OPERAND (t, 0));
16171 RETURN (build_nop (type, op0));
16172 }
16173
16174 case IMPLICIT_CONV_EXPR:
16175 {
16176 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16177 tree expr = RECUR (TREE_OPERAND (t, 0));
16178 int flags = LOOKUP_IMPLICIT;
16179 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16180 flags = LOOKUP_NORMAL;
16181 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16182 flags));
16183 }
16184
16185 case CONVERT_EXPR:
16186 {
16187 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16188 tree op0 = RECUR (TREE_OPERAND (t, 0));
16189 RETURN (build1 (CONVERT_EXPR, type, op0));
16190 }
16191
16192 case CAST_EXPR:
16193 case REINTERPRET_CAST_EXPR:
16194 case CONST_CAST_EXPR:
16195 case DYNAMIC_CAST_EXPR:
16196 case STATIC_CAST_EXPR:
16197 {
16198 tree type;
16199 tree op, r = NULL_TREE;
16200
16201 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16202 if (integral_constant_expression_p
16203 && !cast_valid_in_integral_constant_expression_p (type))
16204 {
16205 if (complain & tf_error)
16206 error ("a cast to a type other than an integral or "
16207 "enumeration type cannot appear in a constant-expression");
16208 RETURN (error_mark_node);
16209 }
16210
16211 op = RECUR (TREE_OPERAND (t, 0));
16212
16213 warning_sentinel s(warn_useless_cast);
16214 switch (TREE_CODE (t))
16215 {
16216 case CAST_EXPR:
16217 r = build_functional_cast (type, op, complain);
16218 break;
16219 case REINTERPRET_CAST_EXPR:
16220 r = build_reinterpret_cast (type, op, complain);
16221 break;
16222 case CONST_CAST_EXPR:
16223 r = build_const_cast (type, op, complain);
16224 break;
16225 case DYNAMIC_CAST_EXPR:
16226 r = build_dynamic_cast (type, op, complain);
16227 break;
16228 case STATIC_CAST_EXPR:
16229 r = build_static_cast (type, op, complain);
16230 break;
16231 default:
16232 gcc_unreachable ();
16233 }
16234
16235 RETURN (r);
16236 }
16237
16238 case POSTDECREMENT_EXPR:
16239 case POSTINCREMENT_EXPR:
16240 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16241 args, complain, in_decl);
16242 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16243 complain|decltype_flag));
16244
16245 case PREDECREMENT_EXPR:
16246 case PREINCREMENT_EXPR:
16247 case NEGATE_EXPR:
16248 case BIT_NOT_EXPR:
16249 case ABS_EXPR:
16250 case TRUTH_NOT_EXPR:
16251 case UNARY_PLUS_EXPR: /* Unary + */
16252 case REALPART_EXPR:
16253 case IMAGPART_EXPR:
16254 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16255 RECUR (TREE_OPERAND (t, 0)),
16256 complain|decltype_flag));
16257
16258 case FIX_TRUNC_EXPR:
16259 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16260 false, complain));
16261
16262 case ADDR_EXPR:
16263 op1 = TREE_OPERAND (t, 0);
16264 if (TREE_CODE (op1) == LABEL_DECL)
16265 RETURN (finish_label_address_expr (DECL_NAME (op1),
16266 EXPR_LOCATION (op1)));
16267 if (TREE_CODE (op1) == SCOPE_REF)
16268 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16269 /*done=*/true, /*address_p=*/true);
16270 else
16271 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16272 in_decl);
16273 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16274 complain|decltype_flag));
16275
16276 case PLUS_EXPR:
16277 case MINUS_EXPR:
16278 case MULT_EXPR:
16279 case TRUNC_DIV_EXPR:
16280 case CEIL_DIV_EXPR:
16281 case FLOOR_DIV_EXPR:
16282 case ROUND_DIV_EXPR:
16283 case EXACT_DIV_EXPR:
16284 case BIT_AND_EXPR:
16285 case BIT_IOR_EXPR:
16286 case BIT_XOR_EXPR:
16287 case TRUNC_MOD_EXPR:
16288 case FLOOR_MOD_EXPR:
16289 case TRUTH_ANDIF_EXPR:
16290 case TRUTH_ORIF_EXPR:
16291 case TRUTH_AND_EXPR:
16292 case TRUTH_OR_EXPR:
16293 case RSHIFT_EXPR:
16294 case LSHIFT_EXPR:
16295 case RROTATE_EXPR:
16296 case LROTATE_EXPR:
16297 case EQ_EXPR:
16298 case NE_EXPR:
16299 case MAX_EXPR:
16300 case MIN_EXPR:
16301 case LE_EXPR:
16302 case GE_EXPR:
16303 case LT_EXPR:
16304 case GT_EXPR:
16305 case MEMBER_REF:
16306 case DOTSTAR_EXPR:
16307 {
16308 warning_sentinel s1(warn_type_limits);
16309 warning_sentinel s2(warn_div_by_zero);
16310 warning_sentinel s3(warn_logical_op);
16311 warning_sentinel s4(warn_tautological_compare);
16312 tree op0 = RECUR (TREE_OPERAND (t, 0));
16313 tree op1 = RECUR (TREE_OPERAND (t, 1));
16314 tree r = build_x_binary_op
16315 (input_location, TREE_CODE (t),
16316 op0,
16317 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16318 ? ERROR_MARK
16319 : TREE_CODE (TREE_OPERAND (t, 0))),
16320 op1,
16321 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16322 ? ERROR_MARK
16323 : TREE_CODE (TREE_OPERAND (t, 1))),
16324 /*overload=*/NULL,
16325 complain|decltype_flag);
16326 if (EXPR_P (r) && TREE_NO_WARNING (t))
16327 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16328
16329 RETURN (r);
16330 }
16331
16332 case POINTER_PLUS_EXPR:
16333 {
16334 tree op0 = RECUR (TREE_OPERAND (t, 0));
16335 tree op1 = RECUR (TREE_OPERAND (t, 1));
16336 return fold_build_pointer_plus (op0, op1);
16337 }
16338
16339 case SCOPE_REF:
16340 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16341 /*address_p=*/false));
16342 case ARRAY_REF:
16343 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16344 args, complain, in_decl);
16345 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16346 RECUR (TREE_OPERAND (t, 1)),
16347 complain|decltype_flag));
16348
16349 case ARRAY_NOTATION_REF:
16350 {
16351 tree start_index, length, stride;
16352 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16353 args, complain, in_decl);
16354 start_index = RECUR (ARRAY_NOTATION_START (t));
16355 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16356 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16357 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16358 length, stride, TREE_TYPE (op1)));
16359 }
16360 case SIZEOF_EXPR:
16361 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16362 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16363 RETURN (tsubst_copy (t, args, complain, in_decl));
16364 /* Fall through */
16365
16366 case ALIGNOF_EXPR:
16367 {
16368 tree r;
16369
16370 op1 = TREE_OPERAND (t, 0);
16371 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16372 op1 = TREE_TYPE (op1);
16373 if (!args)
16374 {
16375 /* When there are no ARGS, we are trying to evaluate a
16376 non-dependent expression from the parser. Trying to do
16377 the substitutions may not work. */
16378 if (!TYPE_P (op1))
16379 op1 = TREE_TYPE (op1);
16380 }
16381 else
16382 {
16383 ++cp_unevaluated_operand;
16384 ++c_inhibit_evaluation_warnings;
16385 if (TYPE_P (op1))
16386 op1 = tsubst (op1, args, complain, in_decl);
16387 else
16388 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16389 /*function_p=*/false,
16390 /*integral_constant_expression_p=*/
16391 false);
16392 --cp_unevaluated_operand;
16393 --c_inhibit_evaluation_warnings;
16394 }
16395 if (TYPE_P (op1))
16396 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16397 complain & tf_error);
16398 else
16399 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16400 complain & tf_error);
16401 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16402 {
16403 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16404 {
16405 if (!processing_template_decl && TYPE_P (op1))
16406 {
16407 r = build_min (SIZEOF_EXPR, size_type_node,
16408 build1 (NOP_EXPR, op1, error_mark_node));
16409 SIZEOF_EXPR_TYPE_P (r) = 1;
16410 }
16411 else
16412 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16413 TREE_SIDE_EFFECTS (r) = 0;
16414 TREE_READONLY (r) = 1;
16415 }
16416 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16417 }
16418 RETURN (r);
16419 }
16420
16421 case AT_ENCODE_EXPR:
16422 {
16423 op1 = TREE_OPERAND (t, 0);
16424 ++cp_unevaluated_operand;
16425 ++c_inhibit_evaluation_warnings;
16426 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16427 /*function_p=*/false,
16428 /*integral_constant_expression_p=*/false);
16429 --cp_unevaluated_operand;
16430 --c_inhibit_evaluation_warnings;
16431 RETURN (objc_build_encode_expr (op1));
16432 }
16433
16434 case NOEXCEPT_EXPR:
16435 op1 = TREE_OPERAND (t, 0);
16436 ++cp_unevaluated_operand;
16437 ++c_inhibit_evaluation_warnings;
16438 ++cp_noexcept_operand;
16439 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16440 /*function_p=*/false,
16441 /*integral_constant_expression_p=*/false);
16442 --cp_unevaluated_operand;
16443 --c_inhibit_evaluation_warnings;
16444 --cp_noexcept_operand;
16445 RETURN (finish_noexcept_expr (op1, complain));
16446
16447 case MODOP_EXPR:
16448 {
16449 warning_sentinel s(warn_div_by_zero);
16450 tree lhs = RECUR (TREE_OPERAND (t, 0));
16451 tree rhs = RECUR (TREE_OPERAND (t, 2));
16452 tree r = build_x_modify_expr
16453 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16454 complain|decltype_flag);
16455 /* TREE_NO_WARNING must be set if either the expression was
16456 parenthesized or it uses an operator such as >>= rather
16457 than plain assignment. In the former case, it was already
16458 set and must be copied. In the latter case,
16459 build_x_modify_expr sets it and it must not be reset
16460 here. */
16461 if (TREE_NO_WARNING (t))
16462 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16463
16464 RETURN (r);
16465 }
16466
16467 case ARROW_EXPR:
16468 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16469 args, complain, in_decl);
16470 /* Remember that there was a reference to this entity. */
16471 if (DECL_P (op1)
16472 && !mark_used (op1, complain) && !(complain & tf_error))
16473 RETURN (error_mark_node);
16474 RETURN (build_x_arrow (input_location, op1, complain));
16475
16476 case NEW_EXPR:
16477 {
16478 tree placement = RECUR (TREE_OPERAND (t, 0));
16479 tree init = RECUR (TREE_OPERAND (t, 3));
16480 vec<tree, va_gc> *placement_vec;
16481 vec<tree, va_gc> *init_vec;
16482 tree ret;
16483
16484 if (placement == NULL_TREE)
16485 placement_vec = NULL;
16486 else
16487 {
16488 placement_vec = make_tree_vector ();
16489 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16490 vec_safe_push (placement_vec, TREE_VALUE (placement));
16491 }
16492
16493 /* If there was an initializer in the original tree, but it
16494 instantiated to an empty list, then we should pass a
16495 non-NULL empty vector to tell build_new that it was an
16496 empty initializer() rather than no initializer. This can
16497 only happen when the initializer is a pack expansion whose
16498 parameter packs are of length zero. */
16499 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16500 init_vec = NULL;
16501 else
16502 {
16503 init_vec = make_tree_vector ();
16504 if (init == void_node)
16505 gcc_assert (init_vec != NULL);
16506 else
16507 {
16508 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16509 vec_safe_push (init_vec, TREE_VALUE (init));
16510 }
16511 }
16512
16513 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16514 tree op2 = RECUR (TREE_OPERAND (t, 2));
16515 ret = build_new (&placement_vec, op1, op2, &init_vec,
16516 NEW_EXPR_USE_GLOBAL (t),
16517 complain);
16518
16519 if (placement_vec != NULL)
16520 release_tree_vector (placement_vec);
16521 if (init_vec != NULL)
16522 release_tree_vector (init_vec);
16523
16524 RETURN (ret);
16525 }
16526
16527 case DELETE_EXPR:
16528 {
16529 tree op0 = RECUR (TREE_OPERAND (t, 0));
16530 tree op1 = RECUR (TREE_OPERAND (t, 1));
16531 RETURN (delete_sanity (op0, op1,
16532 DELETE_EXPR_USE_VEC (t),
16533 DELETE_EXPR_USE_GLOBAL (t),
16534 complain));
16535 }
16536
16537 case COMPOUND_EXPR:
16538 {
16539 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
16540 complain & ~tf_decltype, in_decl,
16541 /*function_p=*/false,
16542 integral_constant_expression_p);
16543 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
16544 op0,
16545 RECUR (TREE_OPERAND (t, 1)),
16546 complain|decltype_flag));
16547 }
16548
16549 case CALL_EXPR:
16550 {
16551 tree function;
16552 vec<tree, va_gc> *call_args;
16553 unsigned int nargs, i;
16554 bool qualified_p;
16555 bool koenig_p;
16556 tree ret;
16557
16558 function = CALL_EXPR_FN (t);
16559 if (function == NULL_TREE)
16560 {
16561 /* If you hit this assert, it means that you're trying to tsubst
16562 an internal function with arguments. This isn't yet supported,
16563 so you need to build another internal call with the tsubsted
16564 arguments after the arguments have been tsubsted down below. */
16565 gcc_assert (call_expr_nargs (t) == 0);
16566 RETURN (t);
16567 }
16568 /* When we parsed the expression, we determined whether or
16569 not Koenig lookup should be performed. */
16570 koenig_p = KOENIG_LOOKUP_P (t);
16571 if (TREE_CODE (function) == SCOPE_REF)
16572 {
16573 qualified_p = true;
16574 function = tsubst_qualified_id (function, args, complain, in_decl,
16575 /*done=*/false,
16576 /*address_p=*/false);
16577 }
16578 else if (koenig_p && identifier_p (function))
16579 {
16580 /* Do nothing; calling tsubst_copy_and_build on an identifier
16581 would incorrectly perform unqualified lookup again.
16582
16583 Note that we can also have an IDENTIFIER_NODE if the earlier
16584 unqualified lookup found a member function; in that case
16585 koenig_p will be false and we do want to do the lookup
16586 again to find the instantiated member function.
16587
16588 FIXME but doing that causes c++/15272, so we need to stop
16589 using IDENTIFIER_NODE in that situation. */
16590 qualified_p = false;
16591 }
16592 else
16593 {
16594 if (TREE_CODE (function) == COMPONENT_REF)
16595 {
16596 tree op = TREE_OPERAND (function, 1);
16597
16598 qualified_p = (TREE_CODE (op) == SCOPE_REF
16599 || (BASELINK_P (op)
16600 && BASELINK_QUALIFIED_P (op)));
16601 }
16602 else
16603 qualified_p = false;
16604
16605 if (TREE_CODE (function) == ADDR_EXPR
16606 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
16607 /* Avoid error about taking the address of a constructor. */
16608 function = TREE_OPERAND (function, 0);
16609
16610 function = tsubst_copy_and_build (function, args, complain,
16611 in_decl,
16612 !qualified_p,
16613 integral_constant_expression_p);
16614
16615 if (BASELINK_P (function))
16616 qualified_p = true;
16617 }
16618
16619 nargs = call_expr_nargs (t);
16620 call_args = make_tree_vector ();
16621 for (i = 0; i < nargs; ++i)
16622 {
16623 tree arg = CALL_EXPR_ARG (t, i);
16624
16625 if (!PACK_EXPANSION_P (arg))
16626 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
16627 else
16628 {
16629 /* Expand the pack expansion and push each entry onto
16630 CALL_ARGS. */
16631 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
16632 if (TREE_CODE (arg) == TREE_VEC)
16633 {
16634 unsigned int len, j;
16635
16636 len = TREE_VEC_LENGTH (arg);
16637 for (j = 0; j < len; ++j)
16638 {
16639 tree value = TREE_VEC_ELT (arg, j);
16640 if (value != NULL_TREE)
16641 value = convert_from_reference (value);
16642 vec_safe_push (call_args, value);
16643 }
16644 }
16645 else
16646 {
16647 /* A partial substitution. Add one entry. */
16648 vec_safe_push (call_args, arg);
16649 }
16650 }
16651 }
16652
16653 /* We do not perform argument-dependent lookup if normal
16654 lookup finds a non-function, in accordance with the
16655 expected resolution of DR 218. */
16656 if (koenig_p
16657 && ((is_overloaded_fn (function)
16658 /* If lookup found a member function, the Koenig lookup is
16659 not appropriate, even if an unqualified-name was used
16660 to denote the function. */
16661 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
16662 || identifier_p (function))
16663 /* Only do this when substitution turns a dependent call
16664 into a non-dependent call. */
16665 && type_dependent_expression_p_push (t)
16666 && !any_type_dependent_arguments_p (call_args))
16667 function = perform_koenig_lookup (function, call_args, tf_none);
16668
16669 if (identifier_p (function)
16670 && !any_type_dependent_arguments_p (call_args))
16671 {
16672 if (koenig_p && (complain & tf_warning_or_error))
16673 {
16674 /* For backwards compatibility and good diagnostics, try
16675 the unqualified lookup again if we aren't in SFINAE
16676 context. */
16677 tree unq = (tsubst_copy_and_build
16678 (function, args, complain, in_decl, true,
16679 integral_constant_expression_p));
16680 if (unq == error_mark_node)
16681 RETURN (error_mark_node);
16682
16683 if (unq != function)
16684 {
16685 tree fn = unq;
16686 if (INDIRECT_REF_P (fn))
16687 fn = TREE_OPERAND (fn, 0);
16688 if (TREE_CODE (fn) == COMPONENT_REF)
16689 fn = TREE_OPERAND (fn, 1);
16690 if (is_overloaded_fn (fn))
16691 fn = get_first_fn (fn);
16692 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16693 "%qD was not declared in this scope, "
16694 "and no declarations were found by "
16695 "argument-dependent lookup at the point "
16696 "of instantiation", function))
16697 {
16698 if (!DECL_P (fn))
16699 /* Can't say anything more. */;
16700 else if (DECL_CLASS_SCOPE_P (fn))
16701 {
16702 location_t loc = EXPR_LOC_OR_LOC (t,
16703 input_location);
16704 inform (loc,
16705 "declarations in dependent base %qT are "
16706 "not found by unqualified lookup",
16707 DECL_CLASS_CONTEXT (fn));
16708 if (current_class_ptr)
16709 inform (loc,
16710 "use %<this->%D%> instead", function);
16711 else
16712 inform (loc,
16713 "use %<%T::%D%> instead",
16714 current_class_name, function);
16715 }
16716 else
16717 inform (DECL_SOURCE_LOCATION (fn),
16718 "%qD declared here, later in the "
16719 "translation unit", fn);
16720 }
16721 function = unq;
16722 }
16723 }
16724 if (identifier_p (function))
16725 {
16726 if (complain & tf_error)
16727 unqualified_name_lookup_error (function);
16728 release_tree_vector (call_args);
16729 RETURN (error_mark_node);
16730 }
16731 }
16732
16733 /* Remember that there was a reference to this entity. */
16734 if (DECL_P (function)
16735 && !mark_used (function, complain) && !(complain & tf_error))
16736 RETURN (error_mark_node);
16737
16738 /* Put back tf_decltype for the actual call. */
16739 complain |= decltype_flag;
16740
16741 if (TREE_CODE (function) == OFFSET_REF)
16742 ret = build_offset_ref_call_from_tree (function, &call_args,
16743 complain);
16744 else if (TREE_CODE (function) == COMPONENT_REF)
16745 {
16746 tree instance = TREE_OPERAND (function, 0);
16747 tree fn = TREE_OPERAND (function, 1);
16748
16749 if (processing_template_decl
16750 && (type_dependent_expression_p (instance)
16751 || (!BASELINK_P (fn)
16752 && TREE_CODE (fn) != FIELD_DECL)
16753 || type_dependent_expression_p (fn)
16754 || any_type_dependent_arguments_p (call_args)))
16755 ret = build_nt_call_vec (function, call_args);
16756 else if (!BASELINK_P (fn))
16757 ret = finish_call_expr (function, &call_args,
16758 /*disallow_virtual=*/false,
16759 /*koenig_p=*/false,
16760 complain);
16761 else
16762 ret = (build_new_method_call
16763 (instance, fn,
16764 &call_args, NULL_TREE,
16765 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
16766 /*fn_p=*/NULL,
16767 complain));
16768 }
16769 else
16770 ret = finish_call_expr (function, &call_args,
16771 /*disallow_virtual=*/qualified_p,
16772 koenig_p,
16773 complain);
16774
16775 release_tree_vector (call_args);
16776
16777 if (ret != error_mark_node)
16778 {
16779 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
16780 bool ord = CALL_EXPR_ORDERED_ARGS (t);
16781 bool rev = CALL_EXPR_REVERSE_ARGS (t);
16782 bool thk = CALL_FROM_THUNK_P (t);
16783 if (op || ord || rev || thk)
16784 {
16785 function = extract_call_expr (ret);
16786 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
16787 CALL_EXPR_ORDERED_ARGS (function) = ord;
16788 CALL_EXPR_REVERSE_ARGS (function) = rev;
16789 if (thk)
16790 {
16791 CALL_FROM_THUNK_P (function) = true;
16792 /* The thunk location is not interesting. */
16793 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
16794 }
16795 }
16796 }
16797
16798 RETURN (ret);
16799 }
16800
16801 case COND_EXPR:
16802 {
16803 tree cond = RECUR (TREE_OPERAND (t, 0));
16804 tree folded_cond = fold_non_dependent_expr (cond);
16805 tree exp1, exp2;
16806
16807 if (TREE_CODE (folded_cond) == INTEGER_CST)
16808 {
16809 if (integer_zerop (folded_cond))
16810 {
16811 ++c_inhibit_evaluation_warnings;
16812 exp1 = RECUR (TREE_OPERAND (t, 1));
16813 --c_inhibit_evaluation_warnings;
16814 exp2 = RECUR (TREE_OPERAND (t, 2));
16815 }
16816 else
16817 {
16818 exp1 = RECUR (TREE_OPERAND (t, 1));
16819 ++c_inhibit_evaluation_warnings;
16820 exp2 = RECUR (TREE_OPERAND (t, 2));
16821 --c_inhibit_evaluation_warnings;
16822 }
16823 cond = folded_cond;
16824 }
16825 else
16826 {
16827 exp1 = RECUR (TREE_OPERAND (t, 1));
16828 exp2 = RECUR (TREE_OPERAND (t, 2));
16829 }
16830
16831 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
16832 cond, exp1, exp2, complain));
16833 }
16834
16835 case PSEUDO_DTOR_EXPR:
16836 {
16837 tree op0 = RECUR (TREE_OPERAND (t, 0));
16838 tree op1 = RECUR (TREE_OPERAND (t, 1));
16839 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
16840 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
16841 input_location));
16842 }
16843
16844 case TREE_LIST:
16845 {
16846 tree purpose, value, chain;
16847
16848 if (t == void_list_node)
16849 RETURN (t);
16850
16851 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
16852 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
16853 {
16854 /* We have pack expansions, so expand those and
16855 create a new list out of it. */
16856 tree purposevec = NULL_TREE;
16857 tree valuevec = NULL_TREE;
16858 tree chain;
16859 int i, len = -1;
16860
16861 /* Expand the argument expressions. */
16862 if (TREE_PURPOSE (t))
16863 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
16864 complain, in_decl);
16865 if (TREE_VALUE (t))
16866 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
16867 complain, in_decl);
16868
16869 /* Build the rest of the list. */
16870 chain = TREE_CHAIN (t);
16871 if (chain && chain != void_type_node)
16872 chain = RECUR (chain);
16873
16874 /* Determine the number of arguments. */
16875 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
16876 {
16877 len = TREE_VEC_LENGTH (purposevec);
16878 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16879 }
16880 else if (TREE_CODE (valuevec) == TREE_VEC)
16881 len = TREE_VEC_LENGTH (valuevec);
16882 else
16883 {
16884 /* Since we only performed a partial substitution into
16885 the argument pack, we only RETURN (a single list
16886 node. */
16887 if (purposevec == TREE_PURPOSE (t)
16888 && valuevec == TREE_VALUE (t)
16889 && chain == TREE_CHAIN (t))
16890 RETURN (t);
16891
16892 RETURN (tree_cons (purposevec, valuevec, chain));
16893 }
16894
16895 /* Convert the argument vectors into a TREE_LIST */
16896 i = len;
16897 while (i > 0)
16898 {
16899 /* Grab the Ith values. */
16900 i--;
16901 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
16902 : NULL_TREE;
16903 value
16904 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
16905 : NULL_TREE;
16906
16907 /* Build the list (backwards). */
16908 chain = tree_cons (purpose, value, chain);
16909 }
16910
16911 RETURN (chain);
16912 }
16913
16914 purpose = TREE_PURPOSE (t);
16915 if (purpose)
16916 purpose = RECUR (purpose);
16917 value = TREE_VALUE (t);
16918 if (value)
16919 value = RECUR (value);
16920 chain = TREE_CHAIN (t);
16921 if (chain && chain != void_type_node)
16922 chain = RECUR (chain);
16923 if (purpose == TREE_PURPOSE (t)
16924 && value == TREE_VALUE (t)
16925 && chain == TREE_CHAIN (t))
16926 RETURN (t);
16927 RETURN (tree_cons (purpose, value, chain));
16928 }
16929
16930 case COMPONENT_REF:
16931 {
16932 tree object;
16933 tree object_type;
16934 tree member;
16935 tree r;
16936
16937 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16938 args, complain, in_decl);
16939 /* Remember that there was a reference to this entity. */
16940 if (DECL_P (object)
16941 && !mark_used (object, complain) && !(complain & tf_error))
16942 RETURN (error_mark_node);
16943 object_type = TREE_TYPE (object);
16944
16945 member = TREE_OPERAND (t, 1);
16946 if (BASELINK_P (member))
16947 member = tsubst_baselink (member,
16948 non_reference (TREE_TYPE (object)),
16949 args, complain, in_decl);
16950 else
16951 member = tsubst_copy (member, args, complain, in_decl);
16952 if (member == error_mark_node)
16953 RETURN (error_mark_node);
16954
16955 if (type_dependent_expression_p (object))
16956 /* We can't do much here. */;
16957 else if (!CLASS_TYPE_P (object_type))
16958 {
16959 if (scalarish_type_p (object_type))
16960 {
16961 tree s = NULL_TREE;
16962 tree dtor = member;
16963
16964 if (TREE_CODE (dtor) == SCOPE_REF)
16965 {
16966 s = TREE_OPERAND (dtor, 0);
16967 dtor = TREE_OPERAND (dtor, 1);
16968 }
16969 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
16970 {
16971 dtor = TREE_OPERAND (dtor, 0);
16972 if (TYPE_P (dtor))
16973 RETURN (finish_pseudo_destructor_expr
16974 (object, s, dtor, input_location));
16975 }
16976 }
16977 }
16978 else if (TREE_CODE (member) == SCOPE_REF
16979 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
16980 {
16981 /* Lookup the template functions now that we know what the
16982 scope is. */
16983 tree scope = TREE_OPERAND (member, 0);
16984 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
16985 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
16986 member = lookup_qualified_name (scope, tmpl,
16987 /*is_type_p=*/false,
16988 /*complain=*/false);
16989 if (BASELINK_P (member))
16990 {
16991 BASELINK_FUNCTIONS (member)
16992 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
16993 args);
16994 member = (adjust_result_of_qualified_name_lookup
16995 (member, BINFO_TYPE (BASELINK_BINFO (member)),
16996 object_type));
16997 }
16998 else
16999 {
17000 qualified_name_lookup_error (scope, tmpl, member,
17001 input_location);
17002 RETURN (error_mark_node);
17003 }
17004 }
17005 else if (TREE_CODE (member) == SCOPE_REF
17006 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17007 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17008 {
17009 if (complain & tf_error)
17010 {
17011 if (TYPE_P (TREE_OPERAND (member, 0)))
17012 error ("%qT is not a class or namespace",
17013 TREE_OPERAND (member, 0));
17014 else
17015 error ("%qD is not a class or namespace",
17016 TREE_OPERAND (member, 0));
17017 }
17018 RETURN (error_mark_node);
17019 }
17020 else if (TREE_CODE (member) == FIELD_DECL)
17021 {
17022 r = finish_non_static_data_member (member, object, NULL_TREE);
17023 if (TREE_CODE (r) == COMPONENT_REF)
17024 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17025 RETURN (r);
17026 }
17027
17028 r = finish_class_member_access_expr (object, member,
17029 /*template_p=*/false,
17030 complain);
17031 if (TREE_CODE (r) == COMPONENT_REF)
17032 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17033 RETURN (r);
17034 }
17035
17036 case THROW_EXPR:
17037 RETURN (build_throw
17038 (RECUR (TREE_OPERAND (t, 0))));
17039
17040 case CONSTRUCTOR:
17041 {
17042 vec<constructor_elt, va_gc> *n;
17043 constructor_elt *ce;
17044 unsigned HOST_WIDE_INT idx;
17045 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17046 bool process_index_p;
17047 int newlen;
17048 bool need_copy_p = false;
17049 tree r;
17050
17051 if (type == error_mark_node)
17052 RETURN (error_mark_node);
17053
17054 /* digest_init will do the wrong thing if we let it. */
17055 if (type && TYPE_PTRMEMFUNC_P (type))
17056 RETURN (t);
17057
17058 /* We do not want to process the index of aggregate
17059 initializers as they are identifier nodes which will be
17060 looked up by digest_init. */
17061 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17062
17063 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17064 newlen = vec_safe_length (n);
17065 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17066 {
17067 if (ce->index && process_index_p
17068 /* An identifier index is looked up in the type
17069 being initialized, not the current scope. */
17070 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17071 ce->index = RECUR (ce->index);
17072
17073 if (PACK_EXPANSION_P (ce->value))
17074 {
17075 /* Substitute into the pack expansion. */
17076 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17077 in_decl);
17078
17079 if (ce->value == error_mark_node
17080 || PACK_EXPANSION_P (ce->value))
17081 ;
17082 else if (TREE_VEC_LENGTH (ce->value) == 1)
17083 /* Just move the argument into place. */
17084 ce->value = TREE_VEC_ELT (ce->value, 0);
17085 else
17086 {
17087 /* Update the length of the final CONSTRUCTOR
17088 arguments vector, and note that we will need to
17089 copy.*/
17090 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17091 need_copy_p = true;
17092 }
17093 }
17094 else
17095 ce->value = RECUR (ce->value);
17096 }
17097
17098 if (need_copy_p)
17099 {
17100 vec<constructor_elt, va_gc> *old_n = n;
17101
17102 vec_alloc (n, newlen);
17103 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17104 {
17105 if (TREE_CODE (ce->value) == TREE_VEC)
17106 {
17107 int i, len = TREE_VEC_LENGTH (ce->value);
17108 for (i = 0; i < len; ++i)
17109 CONSTRUCTOR_APPEND_ELT (n, 0,
17110 TREE_VEC_ELT (ce->value, i));
17111 }
17112 else
17113 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17114 }
17115 }
17116
17117 r = build_constructor (init_list_type_node, n);
17118 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17119
17120 if (TREE_HAS_CONSTRUCTOR (t))
17121 RETURN (finish_compound_literal (type, r, complain));
17122
17123 TREE_TYPE (r) = type;
17124 RETURN (r);
17125 }
17126
17127 case TYPEID_EXPR:
17128 {
17129 tree operand_0 = TREE_OPERAND (t, 0);
17130 if (TYPE_P (operand_0))
17131 {
17132 operand_0 = tsubst (operand_0, args, complain, in_decl);
17133 RETURN (get_typeid (operand_0, complain));
17134 }
17135 else
17136 {
17137 operand_0 = RECUR (operand_0);
17138 RETURN (build_typeid (operand_0, complain));
17139 }
17140 }
17141
17142 case VAR_DECL:
17143 if (!args)
17144 RETURN (t);
17145 else if (DECL_PACK_P (t))
17146 {
17147 /* We don't build decls for an instantiation of a
17148 variadic capture proxy, we instantiate the elements
17149 when needed. */
17150 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17151 return RECUR (DECL_VALUE_EXPR (t));
17152 }
17153 /* Fall through */
17154
17155 case PARM_DECL:
17156 {
17157 tree r = tsubst_copy (t, args, complain, in_decl);
17158 /* ??? We're doing a subset of finish_id_expression here. */
17159 if (VAR_P (r)
17160 && !processing_template_decl
17161 && !cp_unevaluated_operand
17162 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17163 && CP_DECL_THREAD_LOCAL_P (r))
17164 {
17165 if (tree wrap = get_tls_wrapper_fn (r))
17166 /* Replace an evaluated use of the thread_local variable with
17167 a call to its wrapper. */
17168 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17169 }
17170 else if (outer_automatic_var_p (r))
17171 {
17172 r = process_outer_var_ref (r, complain);
17173 if (is_capture_proxy (r))
17174 register_local_specialization (r, t);
17175 }
17176
17177 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17178 /* If the original type was a reference, we'll be wrapped in
17179 the appropriate INDIRECT_REF. */
17180 r = convert_from_reference (r);
17181 RETURN (r);
17182 }
17183
17184 case VA_ARG_EXPR:
17185 {
17186 tree op0 = RECUR (TREE_OPERAND (t, 0));
17187 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17188 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17189 }
17190
17191 case OFFSETOF_EXPR:
17192 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
17193 EXPR_LOCATION (t)));
17194
17195 case TRAIT_EXPR:
17196 {
17197 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17198 complain, in_decl);
17199
17200 tree type2 = TRAIT_EXPR_TYPE2 (t);
17201 if (type2 && TREE_CODE (type2) == TREE_LIST)
17202 type2 = RECUR (type2);
17203 else if (type2)
17204 type2 = tsubst (type2, args, complain, in_decl);
17205
17206 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17207 }
17208
17209 case STMT_EXPR:
17210 {
17211 tree old_stmt_expr = cur_stmt_expr;
17212 tree stmt_expr = begin_stmt_expr ();
17213
17214 cur_stmt_expr = stmt_expr;
17215 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17216 integral_constant_expression_p);
17217 stmt_expr = finish_stmt_expr (stmt_expr, false);
17218 cur_stmt_expr = old_stmt_expr;
17219
17220 /* If the resulting list of expression statement is empty,
17221 fold it further into void_node. */
17222 if (empty_expr_stmt_p (stmt_expr))
17223 stmt_expr = void_node;
17224
17225 RETURN (stmt_expr);
17226 }
17227
17228 case LAMBDA_EXPR:
17229 {
17230 tree r = build_lambda_expr ();
17231
17232 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17233 LAMBDA_EXPR_CLOSURE (r) = type;
17234 CLASSTYPE_LAMBDA_EXPR (type) = r;
17235
17236 LAMBDA_EXPR_LOCATION (r)
17237 = LAMBDA_EXPR_LOCATION (t);
17238 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17239 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17240 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17241 LAMBDA_EXPR_DISCRIMINATOR (r)
17242 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17243 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17244 if (!scope)
17245 /* No substitution needed. */;
17246 else if (VAR_OR_FUNCTION_DECL_P (scope))
17247 /* For a function or variable scope, we want to use tsubst so that we
17248 don't complain about referring to an auto before deduction. */
17249 scope = tsubst (scope, args, complain, in_decl);
17250 else if (TREE_CODE (scope) == PARM_DECL)
17251 {
17252 /* Look up the parameter we want directly, as tsubst_copy
17253 doesn't do what we need. */
17254 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17255 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17256 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17257 parm = DECL_CHAIN (parm);
17258 scope = parm;
17259 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17260 if (DECL_CONTEXT (scope) == NULL_TREE)
17261 DECL_CONTEXT (scope) = fn;
17262 }
17263 else if (TREE_CODE (scope) == FIELD_DECL)
17264 /* For a field, use tsubst_copy so that we look up the existing field
17265 rather than build a new one. */
17266 scope = RECUR (scope);
17267 else
17268 gcc_unreachable ();
17269 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17270
17271 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17272 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17273
17274 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17275 determine_visibility (TYPE_NAME (type));
17276 /* Now that we know visibility, instantiate the type so we have a
17277 declaration of the op() for later calls to lambda_function. */
17278 complete_type (type);
17279
17280 if (tree fn = lambda_function (type))
17281 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17282
17283 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17284
17285 insert_pending_capture_proxies ();
17286
17287 RETURN (build_lambda_object (r));
17288 }
17289
17290 case TARGET_EXPR:
17291 /* We can get here for a constant initializer of non-dependent type.
17292 FIXME stop folding in cp_parser_initializer_clause. */
17293 {
17294 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17295 complain);
17296 RETURN (r);
17297 }
17298
17299 case TRANSACTION_EXPR:
17300 RETURN (tsubst_expr(t, args, complain, in_decl,
17301 integral_constant_expression_p));
17302
17303 case PAREN_EXPR:
17304 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17305
17306 case VEC_PERM_EXPR:
17307 {
17308 tree op0 = RECUR (TREE_OPERAND (t, 0));
17309 tree op1 = RECUR (TREE_OPERAND (t, 1));
17310 tree op2 = RECUR (TREE_OPERAND (t, 2));
17311 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17312 complain));
17313 }
17314
17315 case REQUIRES_EXPR:
17316 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17317
17318 default:
17319 /* Handle Objective-C++ constructs, if appropriate. */
17320 {
17321 tree subst
17322 = objcp_tsubst_copy_and_build (t, args, complain,
17323 in_decl, /*function_p=*/false);
17324 if (subst)
17325 RETURN (subst);
17326 }
17327 RETURN (tsubst_copy (t, args, complain, in_decl));
17328 }
17329
17330 #undef RECUR
17331 #undef RETURN
17332 out:
17333 input_location = loc;
17334 return retval;
17335 }
17336
17337 /* Verify that the instantiated ARGS are valid. For type arguments,
17338 make sure that the type's linkage is ok. For non-type arguments,
17339 make sure they are constants if they are integral or enumerations.
17340 Emit an error under control of COMPLAIN, and return TRUE on error. */
17341
17342 static bool
17343 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17344 {
17345 if (dependent_template_arg_p (t))
17346 return false;
17347 if (ARGUMENT_PACK_P (t))
17348 {
17349 tree vec = ARGUMENT_PACK_ARGS (t);
17350 int len = TREE_VEC_LENGTH (vec);
17351 bool result = false;
17352 int i;
17353
17354 for (i = 0; i < len; ++i)
17355 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17356 result = true;
17357 return result;
17358 }
17359 else if (TYPE_P (t))
17360 {
17361 /* [basic.link]: A name with no linkage (notably, the name
17362 of a class or enumeration declared in a local scope)
17363 shall not be used to declare an entity with linkage.
17364 This implies that names with no linkage cannot be used as
17365 template arguments
17366
17367 DR 757 relaxes this restriction for C++0x. */
17368 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17369 : no_linkage_check (t, /*relaxed_p=*/false));
17370
17371 if (nt)
17372 {
17373 /* DR 488 makes use of a type with no linkage cause
17374 type deduction to fail. */
17375 if (complain & tf_error)
17376 {
17377 if (TYPE_UNNAMED_P (nt))
17378 error ("%qT is/uses unnamed type", t);
17379 else
17380 error ("template argument for %qD uses local type %qT",
17381 tmpl, t);
17382 }
17383 return true;
17384 }
17385 /* In order to avoid all sorts of complications, we do not
17386 allow variably-modified types as template arguments. */
17387 else if (variably_modified_type_p (t, NULL_TREE))
17388 {
17389 if (complain & tf_error)
17390 error ("%qT is a variably modified type", t);
17391 return true;
17392 }
17393 }
17394 /* Class template and alias template arguments should be OK. */
17395 else if (DECL_TYPE_TEMPLATE_P (t))
17396 ;
17397 /* A non-type argument of integral or enumerated type must be a
17398 constant. */
17399 else if (TREE_TYPE (t)
17400 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17401 && !REFERENCE_REF_P (t)
17402 && !TREE_CONSTANT (t))
17403 {
17404 if (complain & tf_error)
17405 error ("integral expression %qE is not constant", t);
17406 return true;
17407 }
17408 return false;
17409 }
17410
17411 static bool
17412 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17413 {
17414 int ix, len = DECL_NTPARMS (tmpl);
17415 bool result = false;
17416
17417 for (ix = 0; ix != len; ix++)
17418 {
17419 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17420 result = true;
17421 }
17422 if (result && (complain & tf_error))
17423 error (" trying to instantiate %qD", tmpl);
17424 return result;
17425 }
17426
17427 /* We're out of SFINAE context now, so generate diagnostics for the access
17428 errors we saw earlier when instantiating D from TMPL and ARGS. */
17429
17430 static void
17431 recheck_decl_substitution (tree d, tree tmpl, tree args)
17432 {
17433 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17434 tree type = TREE_TYPE (pattern);
17435 location_t loc = input_location;
17436
17437 push_access_scope (d);
17438 push_deferring_access_checks (dk_no_deferred);
17439 input_location = DECL_SOURCE_LOCATION (pattern);
17440 tsubst (type, args, tf_warning_or_error, d);
17441 input_location = loc;
17442 pop_deferring_access_checks ();
17443 pop_access_scope (d);
17444 }
17445
17446 /* Instantiate the indicated variable, function, or alias template TMPL with
17447 the template arguments in TARG_PTR. */
17448
17449 static tree
17450 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17451 {
17452 tree targ_ptr = orig_args;
17453 tree fndecl;
17454 tree gen_tmpl;
17455 tree spec;
17456 bool access_ok = true;
17457
17458 if (tmpl == error_mark_node)
17459 return error_mark_node;
17460
17461 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
17462
17463 /* If this function is a clone, handle it specially. */
17464 if (DECL_CLONED_FUNCTION_P (tmpl))
17465 {
17466 tree spec;
17467 tree clone;
17468
17469 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17470 DECL_CLONED_FUNCTION. */
17471 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
17472 targ_ptr, complain);
17473 if (spec == error_mark_node)
17474 return error_mark_node;
17475
17476 /* Look for the clone. */
17477 FOR_EACH_CLONE (clone, spec)
17478 if (DECL_NAME (clone) == DECL_NAME (tmpl))
17479 return clone;
17480 /* We should always have found the clone by now. */
17481 gcc_unreachable ();
17482 return NULL_TREE;
17483 }
17484
17485 if (targ_ptr == error_mark_node)
17486 return error_mark_node;
17487
17488 /* Check to see if we already have this specialization. */
17489 gen_tmpl = most_general_template (tmpl);
17490 if (TMPL_ARGS_DEPTH (targ_ptr)
17491 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
17492 /* targ_ptr only has the innermost template args, so add the outer ones
17493 from tmpl, which could be either a partial instantiation or gen_tmpl (in
17494 the case of a non-dependent call within a template definition). */
17495 targ_ptr = (add_outermost_template_args
17496 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
17497 targ_ptr));
17498
17499 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17500 but it doesn't seem to be on the hot path. */
17501 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
17502
17503 gcc_assert (tmpl == gen_tmpl
17504 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
17505 == spec)
17506 || fndecl == NULL_TREE);
17507
17508 if (spec != NULL_TREE)
17509 {
17510 if (FNDECL_HAS_ACCESS_ERRORS (spec))
17511 {
17512 if (complain & tf_error)
17513 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
17514 return error_mark_node;
17515 }
17516 return spec;
17517 }
17518
17519 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
17520 complain))
17521 return error_mark_node;
17522
17523 /* We are building a FUNCTION_DECL, during which the access of its
17524 parameters and return types have to be checked. However this
17525 FUNCTION_DECL which is the desired context for access checking
17526 is not built yet. We solve this chicken-and-egg problem by
17527 deferring all checks until we have the FUNCTION_DECL. */
17528 push_deferring_access_checks (dk_deferred);
17529
17530 /* Instantiation of the function happens in the context of the function
17531 template, not the context of the overload resolution we're doing. */
17532 push_to_top_level ();
17533 /* If there are dependent arguments, e.g. because we're doing partial
17534 ordering, make sure processing_template_decl stays set. */
17535 if (uses_template_parms (targ_ptr))
17536 ++processing_template_decl;
17537 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17538 {
17539 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
17540 complain, gen_tmpl, true);
17541 push_nested_class (ctx);
17542 }
17543
17544 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
17545
17546 fndecl = NULL_TREE;
17547 if (VAR_P (pattern))
17548 {
17549 /* We need to determine if we're using a partial or explicit
17550 specialization now, because the type of the variable could be
17551 different. */
17552 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
17553 tree elt = most_specialized_partial_spec (tid, complain);
17554 if (elt == error_mark_node)
17555 pattern = error_mark_node;
17556 else if (elt)
17557 {
17558 tree partial_tmpl = TREE_VALUE (elt);
17559 tree partial_args = TREE_PURPOSE (elt);
17560 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
17561 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
17562 }
17563 }
17564
17565 /* Substitute template parameters to obtain the specialization. */
17566 if (fndecl == NULL_TREE)
17567 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
17568 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17569 pop_nested_class ();
17570 pop_from_top_level ();
17571
17572 if (fndecl == error_mark_node)
17573 {
17574 pop_deferring_access_checks ();
17575 return error_mark_node;
17576 }
17577
17578 /* The DECL_TI_TEMPLATE should always be the immediate parent
17579 template, not the most general template. */
17580 DECL_TI_TEMPLATE (fndecl) = tmpl;
17581 DECL_TI_ARGS (fndecl) = targ_ptr;
17582
17583 /* Now we know the specialization, compute access previously
17584 deferred. */
17585 push_access_scope (fndecl);
17586 if (!perform_deferred_access_checks (complain))
17587 access_ok = false;
17588 pop_access_scope (fndecl);
17589 pop_deferring_access_checks ();
17590
17591 /* If we've just instantiated the main entry point for a function,
17592 instantiate all the alternate entry points as well. We do this
17593 by cloning the instantiation of the main entry point, not by
17594 instantiating the template clones. */
17595 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
17596 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
17597
17598 if (!access_ok)
17599 {
17600 if (!(complain & tf_error))
17601 {
17602 /* Remember to reinstantiate when we're out of SFINAE so the user
17603 can see the errors. */
17604 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
17605 }
17606 return error_mark_node;
17607 }
17608 return fndecl;
17609 }
17610
17611 /* Wrapper for instantiate_template_1. */
17612
17613 tree
17614 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
17615 {
17616 tree ret;
17617 timevar_push (TV_TEMPLATE_INST);
17618 ret = instantiate_template_1 (tmpl, orig_args, complain);
17619 timevar_pop (TV_TEMPLATE_INST);
17620 return ret;
17621 }
17622
17623 /* Instantiate the alias template TMPL with ARGS. Also push a template
17624 instantiation level, which instantiate_template doesn't do because
17625 functions and variables have sufficient context established by the
17626 callers. */
17627
17628 static tree
17629 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
17630 {
17631 struct pending_template *old_last_pend = last_pending_template;
17632 struct tinst_level *old_error_tinst = last_error_tinst_level;
17633 if (tmpl == error_mark_node || args == error_mark_node)
17634 return error_mark_node;
17635 tree tinst = build_tree_list (tmpl, args);
17636 if (!push_tinst_level (tinst))
17637 {
17638 ggc_free (tinst);
17639 return error_mark_node;
17640 }
17641
17642 args =
17643 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
17644 args, tmpl, complain,
17645 /*require_all_args=*/true,
17646 /*use_default_args=*/true);
17647
17648 tree r = instantiate_template (tmpl, args, complain);
17649 pop_tinst_level ();
17650 /* We can't free this if a pending_template entry or last_error_tinst_level
17651 is pointing at it. */
17652 if (last_pending_template == old_last_pend
17653 && last_error_tinst_level == old_error_tinst)
17654 ggc_free (tinst);
17655
17656 return r;
17657 }
17658
17659 /* PARM is a template parameter pack for FN. Returns true iff
17660 PARM is used in a deducible way in the argument list of FN. */
17661
17662 static bool
17663 pack_deducible_p (tree parm, tree fn)
17664 {
17665 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
17666 for (; t; t = TREE_CHAIN (t))
17667 {
17668 tree type = TREE_VALUE (t);
17669 tree packs;
17670 if (!PACK_EXPANSION_P (type))
17671 continue;
17672 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
17673 packs; packs = TREE_CHAIN (packs))
17674 if (template_args_equal (TREE_VALUE (packs), parm))
17675 {
17676 /* The template parameter pack is used in a function parameter
17677 pack. If this is the end of the parameter list, the
17678 template parameter pack is deducible. */
17679 if (TREE_CHAIN (t) == void_list_node)
17680 return true;
17681 else
17682 /* Otherwise, not. Well, it could be deduced from
17683 a non-pack parameter, but doing so would end up with
17684 a deduction mismatch, so don't bother. */
17685 return false;
17686 }
17687 }
17688 /* The template parameter pack isn't used in any function parameter
17689 packs, but it might be used deeper, e.g. tuple<Args...>. */
17690 return true;
17691 }
17692
17693 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17694 NARGS elements of the arguments that are being used when calling
17695 it. TARGS is a vector into which the deduced template arguments
17696 are placed.
17697
17698 Returns either a FUNCTION_DECL for the matching specialization of FN or
17699 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17700 true, diagnostics will be printed to explain why it failed.
17701
17702 If FN is a conversion operator, or we are trying to produce a specific
17703 specialization, RETURN_TYPE is the return type desired.
17704
17705 The EXPLICIT_TARGS are explicit template arguments provided via a
17706 template-id.
17707
17708 The parameter STRICT is one of:
17709
17710 DEDUCE_CALL:
17711 We are deducing arguments for a function call, as in
17712 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17713 deducing arguments for a call to the result of a conversion
17714 function template, as in [over.call.object].
17715
17716 DEDUCE_CONV:
17717 We are deducing arguments for a conversion function, as in
17718 [temp.deduct.conv].
17719
17720 DEDUCE_EXACT:
17721 We are deducing arguments when doing an explicit instantiation
17722 as in [temp.explicit], when determining an explicit specialization
17723 as in [temp.expl.spec], or when taking the address of a function
17724 template, as in [temp.deduct.funcaddr]. */
17725
17726 tree
17727 fn_type_unification (tree fn,
17728 tree explicit_targs,
17729 tree targs,
17730 const tree *args,
17731 unsigned int nargs,
17732 tree return_type,
17733 unification_kind_t strict,
17734 int flags,
17735 bool explain_p,
17736 bool decltype_p)
17737 {
17738 tree parms;
17739 tree fntype;
17740 tree decl = NULL_TREE;
17741 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17742 bool ok;
17743 static int deduction_depth;
17744 struct pending_template *old_last_pend = last_pending_template;
17745 struct tinst_level *old_error_tinst = last_error_tinst_level;
17746 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17747 tree tinst;
17748 tree r = error_mark_node;
17749
17750 tree full_targs = targs;
17751 if (TMPL_ARGS_DEPTH (targs)
17752 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
17753 full_targs = (add_outermost_template_args
17754 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
17755 targs));
17756
17757 if (decltype_p)
17758 complain |= tf_decltype;
17759
17760 /* In C++0x, it's possible to have a function template whose type depends
17761 on itself recursively. This is most obvious with decltype, but can also
17762 occur with enumeration scope (c++/48969). So we need to catch infinite
17763 recursion and reject the substitution at deduction time; this function
17764 will return error_mark_node for any repeated substitution.
17765
17766 This also catches excessive recursion such as when f<N> depends on
17767 f<N-1> across all integers, and returns error_mark_node for all the
17768 substitutions back up to the initial one.
17769
17770 This is, of course, not reentrant. */
17771 if (excessive_deduction_depth)
17772 return error_mark_node;
17773 tinst = build_tree_list (fn, NULL_TREE);
17774 ++deduction_depth;
17775
17776 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
17777
17778 fntype = TREE_TYPE (fn);
17779 if (explicit_targs)
17780 {
17781 /* [temp.deduct]
17782
17783 The specified template arguments must match the template
17784 parameters in kind (i.e., type, nontype, template), and there
17785 must not be more arguments than there are parameters;
17786 otherwise type deduction fails.
17787
17788 Nontype arguments must match the types of the corresponding
17789 nontype template parameters, or must be convertible to the
17790 types of the corresponding nontype parameters as specified in
17791 _temp.arg.nontype_, otherwise type deduction fails.
17792
17793 All references in the function type of the function template
17794 to the corresponding template parameters are replaced by the
17795 specified template argument values. If a substitution in a
17796 template parameter or in the function type of the function
17797 template results in an invalid type, type deduction fails. */
17798 int i, len = TREE_VEC_LENGTH (tparms);
17799 location_t loc = input_location;
17800 bool incomplete = false;
17801
17802 if (explicit_targs == error_mark_node)
17803 goto fail;
17804
17805 if (TMPL_ARGS_DEPTH (explicit_targs)
17806 < TMPL_ARGS_DEPTH (full_targs))
17807 explicit_targs = add_outermost_template_args (full_targs,
17808 explicit_targs);
17809
17810 /* Adjust any explicit template arguments before entering the
17811 substitution context. */
17812 explicit_targs
17813 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
17814 complain,
17815 /*require_all_args=*/false,
17816 /*use_default_args=*/false));
17817 if (explicit_targs == error_mark_node)
17818 goto fail;
17819
17820 /* Substitute the explicit args into the function type. This is
17821 necessary so that, for instance, explicitly declared function
17822 arguments can match null pointed constants. If we were given
17823 an incomplete set of explicit args, we must not do semantic
17824 processing during substitution as we could create partial
17825 instantiations. */
17826 for (i = 0; i < len; i++)
17827 {
17828 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17829 bool parameter_pack = false;
17830 tree targ = TREE_VEC_ELT (explicit_targs, i);
17831
17832 /* Dig out the actual parm. */
17833 if (TREE_CODE (parm) == TYPE_DECL
17834 || TREE_CODE (parm) == TEMPLATE_DECL)
17835 {
17836 parm = TREE_TYPE (parm);
17837 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
17838 }
17839 else if (TREE_CODE (parm) == PARM_DECL)
17840 {
17841 parm = DECL_INITIAL (parm);
17842 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
17843 }
17844
17845 if (!parameter_pack && targ == NULL_TREE)
17846 /* No explicit argument for this template parameter. */
17847 incomplete = true;
17848
17849 if (parameter_pack && pack_deducible_p (parm, fn))
17850 {
17851 /* Mark the argument pack as "incomplete". We could
17852 still deduce more arguments during unification.
17853 We remove this mark in type_unification_real. */
17854 if (targ)
17855 {
17856 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
17857 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
17858 = ARGUMENT_PACK_ARGS (targ);
17859 }
17860
17861 /* We have some incomplete argument packs. */
17862 incomplete = true;
17863 }
17864 }
17865
17866 TREE_VALUE (tinst) = explicit_targs;
17867 if (!push_tinst_level (tinst))
17868 {
17869 excessive_deduction_depth = true;
17870 goto fail;
17871 }
17872 processing_template_decl += incomplete;
17873 input_location = DECL_SOURCE_LOCATION (fn);
17874 /* Ignore any access checks; we'll see them again in
17875 instantiate_template and they might have the wrong
17876 access path at this point. */
17877 push_deferring_access_checks (dk_deferred);
17878 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
17879 complain | tf_partial, NULL_TREE);
17880 pop_deferring_access_checks ();
17881 input_location = loc;
17882 processing_template_decl -= incomplete;
17883 pop_tinst_level ();
17884
17885 if (fntype == error_mark_node)
17886 goto fail;
17887
17888 /* Place the explicitly specified arguments in TARGS. */
17889 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
17890 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
17891 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
17892 }
17893
17894 /* Never do unification on the 'this' parameter. */
17895 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
17896
17897 if (return_type && strict == DEDUCE_CALL)
17898 {
17899 /* We're deducing for a call to the result of a template conversion
17900 function. The parms we really want are in return_type. */
17901 if (POINTER_TYPE_P (return_type))
17902 return_type = TREE_TYPE (return_type);
17903 parms = TYPE_ARG_TYPES (return_type);
17904 }
17905 else if (return_type)
17906 {
17907 tree *new_args;
17908
17909 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
17910 new_args = XALLOCAVEC (tree, nargs + 1);
17911 new_args[0] = return_type;
17912 memcpy (new_args + 1, args, nargs * sizeof (tree));
17913 args = new_args;
17914 ++nargs;
17915 }
17916
17917 /* We allow incomplete unification without an error message here
17918 because the standard doesn't seem to explicitly prohibit it. Our
17919 callers must be ready to deal with unification failures in any
17920 event. */
17921
17922 TREE_VALUE (tinst) = targs;
17923 /* If we aren't explaining yet, push tinst context so we can see where
17924 any errors (e.g. from class instantiations triggered by instantiation
17925 of default template arguments) come from. If we are explaining, this
17926 context is redundant. */
17927 if (!explain_p && !push_tinst_level (tinst))
17928 {
17929 excessive_deduction_depth = true;
17930 goto fail;
17931 }
17932
17933 /* type_unification_real will pass back any access checks from default
17934 template argument substitution. */
17935 vec<deferred_access_check, va_gc> *checks;
17936 checks = NULL;
17937
17938 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17939 full_targs, parms, args, nargs, /*subr=*/0,
17940 strict, flags, &checks, explain_p);
17941 if (!explain_p)
17942 pop_tinst_level ();
17943 if (!ok)
17944 goto fail;
17945
17946 /* Now that we have bindings for all of the template arguments,
17947 ensure that the arguments deduced for the template template
17948 parameters have compatible template parameter lists. We cannot
17949 check this property before we have deduced all template
17950 arguments, because the template parameter types of a template
17951 template parameter might depend on prior template parameters
17952 deduced after the template template parameter. The following
17953 ill-formed example illustrates this issue:
17954
17955 template<typename T, template<T> class C> void f(C<5>, T);
17956
17957 template<int N> struct X {};
17958
17959 void g() {
17960 f(X<5>(), 5l); // error: template argument deduction fails
17961 }
17962
17963 The template parameter list of 'C' depends on the template type
17964 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17965 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17966 time that we deduce 'C'. */
17967 if (!template_template_parm_bindings_ok_p
17968 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
17969 {
17970 unify_inconsistent_template_template_parameters (explain_p);
17971 goto fail;
17972 }
17973
17974 /* All is well so far. Now, check:
17975
17976 [temp.deduct]
17977
17978 When all template arguments have been deduced, all uses of
17979 template parameters in nondeduced contexts are replaced with
17980 the corresponding deduced argument values. If the
17981 substitution results in an invalid type, as described above,
17982 type deduction fails. */
17983 TREE_VALUE (tinst) = targs;
17984 if (!push_tinst_level (tinst))
17985 {
17986 excessive_deduction_depth = true;
17987 goto fail;
17988 }
17989
17990 /* Also collect access checks from the instantiation. */
17991 reopen_deferring_access_checks (checks);
17992
17993 decl = instantiate_template (fn, targs, complain);
17994
17995 checks = get_deferred_access_checks ();
17996 pop_deferring_access_checks ();
17997
17998 pop_tinst_level ();
17999
18000 if (decl == error_mark_node)
18001 goto fail;
18002
18003 /* Now perform any access checks encountered during substitution. */
18004 push_access_scope (decl);
18005 ok = perform_access_checks (checks, complain);
18006 pop_access_scope (decl);
18007 if (!ok)
18008 goto fail;
18009
18010 /* If we're looking for an exact match, check that what we got
18011 is indeed an exact match. It might not be if some template
18012 parameters are used in non-deduced contexts. But don't check
18013 for an exact match if we have dependent template arguments;
18014 in that case we're doing partial ordering, and we already know
18015 that we have two candidates that will provide the actual type. */
18016 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18017 {
18018 tree substed = TREE_TYPE (decl);
18019 unsigned int i;
18020
18021 tree sarg
18022 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18023 if (return_type)
18024 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18025 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18026 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18027 {
18028 unify_type_mismatch (explain_p, args[i],
18029 TREE_VALUE (sarg));
18030 goto fail;
18031 }
18032 }
18033
18034 r = decl;
18035
18036 fail:
18037 --deduction_depth;
18038 if (excessive_deduction_depth)
18039 {
18040 if (deduction_depth == 0)
18041 /* Reset once we're all the way out. */
18042 excessive_deduction_depth = false;
18043 }
18044
18045 /* We can't free this if a pending_template entry or last_error_tinst_level
18046 is pointing at it. */
18047 if (last_pending_template == old_last_pend
18048 && last_error_tinst_level == old_error_tinst)
18049 ggc_free (tinst);
18050
18051 return r;
18052 }
18053
18054 /* Adjust types before performing type deduction, as described in
18055 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18056 sections are symmetric. PARM is the type of a function parameter
18057 or the return type of the conversion function. ARG is the type of
18058 the argument passed to the call, or the type of the value
18059 initialized with the result of the conversion function.
18060 ARG_EXPR is the original argument expression, which may be null. */
18061
18062 static int
18063 maybe_adjust_types_for_deduction (unification_kind_t strict,
18064 tree* parm,
18065 tree* arg,
18066 tree arg_expr)
18067 {
18068 int result = 0;
18069
18070 switch (strict)
18071 {
18072 case DEDUCE_CALL:
18073 break;
18074
18075 case DEDUCE_CONV:
18076 /* Swap PARM and ARG throughout the remainder of this
18077 function; the handling is precisely symmetric since PARM
18078 will initialize ARG rather than vice versa. */
18079 std::swap (parm, arg);
18080 break;
18081
18082 case DEDUCE_EXACT:
18083 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18084 too, but here handle it by stripping the reference from PARM
18085 rather than by adding it to ARG. */
18086 if (TREE_CODE (*parm) == REFERENCE_TYPE
18087 && TYPE_REF_IS_RVALUE (*parm)
18088 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18089 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18090 && TREE_CODE (*arg) == REFERENCE_TYPE
18091 && !TYPE_REF_IS_RVALUE (*arg))
18092 *parm = TREE_TYPE (*parm);
18093 /* Nothing else to do in this case. */
18094 return 0;
18095
18096 default:
18097 gcc_unreachable ();
18098 }
18099
18100 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18101 {
18102 /* [temp.deduct.call]
18103
18104 If P is not a reference type:
18105
18106 --If A is an array type, the pointer type produced by the
18107 array-to-pointer standard conversion (_conv.array_) is
18108 used in place of A for type deduction; otherwise,
18109
18110 --If A is a function type, the pointer type produced by
18111 the function-to-pointer standard conversion
18112 (_conv.func_) is used in place of A for type deduction;
18113 otherwise,
18114
18115 --If A is a cv-qualified type, the top level
18116 cv-qualifiers of A's type are ignored for type
18117 deduction. */
18118 if (TREE_CODE (*arg) == ARRAY_TYPE)
18119 *arg = build_pointer_type (TREE_TYPE (*arg));
18120 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18121 *arg = build_pointer_type (*arg);
18122 else
18123 *arg = TYPE_MAIN_VARIANT (*arg);
18124 }
18125
18126 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
18127 of the form T&&, where T is a template parameter, and the argument
18128 is an lvalue, T is deduced as A& */
18129 if (TREE_CODE (*parm) == REFERENCE_TYPE
18130 && TYPE_REF_IS_RVALUE (*parm)
18131 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18132 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18133 && (arg_expr ? lvalue_p (arg_expr)
18134 /* try_one_overload doesn't provide an arg_expr, but
18135 functions are always lvalues. */
18136 : TREE_CODE (*arg) == FUNCTION_TYPE))
18137 *arg = build_reference_type (*arg);
18138
18139 /* [temp.deduct.call]
18140
18141 If P is a cv-qualified type, the top level cv-qualifiers
18142 of P's type are ignored for type deduction. If P is a
18143 reference type, the type referred to by P is used for
18144 type deduction. */
18145 *parm = TYPE_MAIN_VARIANT (*parm);
18146 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18147 {
18148 *parm = TREE_TYPE (*parm);
18149 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18150 }
18151
18152 /* DR 322. For conversion deduction, remove a reference type on parm
18153 too (which has been swapped into ARG). */
18154 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18155 *arg = TREE_TYPE (*arg);
18156
18157 return result;
18158 }
18159
18160 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18161 template which does contain any deducible template parameters; check if
18162 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18163 unify_one_argument. */
18164
18165 static int
18166 check_non_deducible_conversion (tree parm, tree arg, int strict,
18167 int flags, bool explain_p)
18168 {
18169 tree type;
18170
18171 if (!TYPE_P (arg))
18172 type = TREE_TYPE (arg);
18173 else
18174 type = arg;
18175
18176 if (same_type_p (parm, type))
18177 return unify_success (explain_p);
18178
18179 if (strict == DEDUCE_CONV)
18180 {
18181 if (can_convert_arg (type, parm, NULL_TREE, flags,
18182 explain_p ? tf_warning_or_error : tf_none))
18183 return unify_success (explain_p);
18184 }
18185 else if (strict != DEDUCE_EXACT)
18186 {
18187 if (can_convert_arg (parm, type,
18188 TYPE_P (arg) ? NULL_TREE : arg,
18189 flags, explain_p ? tf_warning_or_error : tf_none))
18190 return unify_success (explain_p);
18191 }
18192
18193 if (strict == DEDUCE_EXACT)
18194 return unify_type_mismatch (explain_p, parm, arg);
18195 else
18196 return unify_arg_conversion (explain_p, parm, type, arg);
18197 }
18198
18199 static bool uses_deducible_template_parms (tree type);
18200
18201 /* Returns true iff the expression EXPR is one from which a template
18202 argument can be deduced. In other words, if it's an undecorated
18203 use of a template non-type parameter. */
18204
18205 static bool
18206 deducible_expression (tree expr)
18207 {
18208 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18209 }
18210
18211 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18212 deducible way; that is, if it has a max value of <PARM> - 1. */
18213
18214 static bool
18215 deducible_array_bound (tree domain)
18216 {
18217 if (domain == NULL_TREE)
18218 return false;
18219
18220 tree max = TYPE_MAX_VALUE (domain);
18221 if (TREE_CODE (max) != MINUS_EXPR)
18222 return false;
18223
18224 return deducible_expression (TREE_OPERAND (max, 0));
18225 }
18226
18227 /* Returns true iff the template arguments ARGS use a template parameter
18228 in a deducible way. */
18229
18230 static bool
18231 deducible_template_args (tree args)
18232 {
18233 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18234 {
18235 bool deducible;
18236 tree elt = TREE_VEC_ELT (args, i);
18237 if (ARGUMENT_PACK_P (elt))
18238 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18239 else
18240 {
18241 if (PACK_EXPANSION_P (elt))
18242 elt = PACK_EXPANSION_PATTERN (elt);
18243 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18244 deducible = true;
18245 else if (TYPE_P (elt))
18246 deducible = uses_deducible_template_parms (elt);
18247 else
18248 deducible = deducible_expression (elt);
18249 }
18250 if (deducible)
18251 return true;
18252 }
18253 return false;
18254 }
18255
18256 /* Returns true iff TYPE contains any deducible references to template
18257 parameters, as per 14.8.2.5. */
18258
18259 static bool
18260 uses_deducible_template_parms (tree type)
18261 {
18262 if (PACK_EXPANSION_P (type))
18263 type = PACK_EXPANSION_PATTERN (type);
18264
18265 /* T
18266 cv-list T
18267 TT<T>
18268 TT<i>
18269 TT<> */
18270 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18271 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18272 return true;
18273
18274 /* T*
18275 T&
18276 T&& */
18277 if (POINTER_TYPE_P (type))
18278 return uses_deducible_template_parms (TREE_TYPE (type));
18279
18280 /* T[integer-constant ]
18281 type [i] */
18282 if (TREE_CODE (type) == ARRAY_TYPE)
18283 return (uses_deducible_template_parms (TREE_TYPE (type))
18284 || deducible_array_bound (TYPE_DOMAIN (type)));
18285
18286 /* T type ::*
18287 type T::*
18288 T T::*
18289 T (type ::*)()
18290 type (T::*)()
18291 type (type ::*)(T)
18292 type (T::*)(T)
18293 T (type ::*)(T)
18294 T (T::*)()
18295 T (T::*)(T) */
18296 if (TYPE_PTRMEM_P (type))
18297 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18298 || (uses_deducible_template_parms
18299 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18300
18301 /* template-name <T> (where template-name refers to a class template)
18302 template-name <i> (where template-name refers to a class template) */
18303 if (CLASS_TYPE_P (type)
18304 && CLASSTYPE_TEMPLATE_INFO (type)
18305 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18306 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18307 (CLASSTYPE_TI_ARGS (type)));
18308
18309 /* type (T)
18310 T()
18311 T(T) */
18312 if (TREE_CODE (type) == FUNCTION_TYPE
18313 || TREE_CODE (type) == METHOD_TYPE)
18314 {
18315 if (uses_deducible_template_parms (TREE_TYPE (type)))
18316 return true;
18317 tree parm = TYPE_ARG_TYPES (type);
18318 if (TREE_CODE (type) == METHOD_TYPE)
18319 parm = TREE_CHAIN (parm);
18320 for (; parm; parm = TREE_CHAIN (parm))
18321 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18322 return true;
18323 }
18324
18325 return false;
18326 }
18327
18328 /* Subroutine of type_unification_real and unify_pack_expansion to
18329 handle unification of a single P/A pair. Parameters are as
18330 for those functions. */
18331
18332 static int
18333 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18334 int subr, unification_kind_t strict,
18335 bool explain_p)
18336 {
18337 tree arg_expr = NULL_TREE;
18338 int arg_strict;
18339
18340 if (arg == error_mark_node || parm == error_mark_node)
18341 return unify_invalid (explain_p);
18342 if (arg == unknown_type_node)
18343 /* We can't deduce anything from this, but we might get all the
18344 template args from other function args. */
18345 return unify_success (explain_p);
18346
18347 /* Implicit conversions (Clause 4) will be performed on a function
18348 argument to convert it to the type of the corresponding function
18349 parameter if the parameter type contains no template-parameters that
18350 participate in template argument deduction. */
18351 if (strict != DEDUCE_EXACT
18352 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18353 /* For function parameters with no deducible template parameters,
18354 just return. We'll check non-dependent conversions later. */
18355 return unify_success (explain_p);
18356
18357 switch (strict)
18358 {
18359 case DEDUCE_CALL:
18360 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18361 | UNIFY_ALLOW_MORE_CV_QUAL
18362 | UNIFY_ALLOW_DERIVED);
18363 break;
18364
18365 case DEDUCE_CONV:
18366 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18367 break;
18368
18369 case DEDUCE_EXACT:
18370 arg_strict = UNIFY_ALLOW_NONE;
18371 break;
18372
18373 default:
18374 gcc_unreachable ();
18375 }
18376
18377 /* We only do these transformations if this is the top-level
18378 parameter_type_list in a call or declaration matching; in other
18379 situations (nested function declarators, template argument lists) we
18380 won't be comparing a type to an expression, and we don't do any type
18381 adjustments. */
18382 if (!subr)
18383 {
18384 if (!TYPE_P (arg))
18385 {
18386 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18387 if (type_unknown_p (arg))
18388 {
18389 /* [temp.deduct.type] A template-argument can be
18390 deduced from a pointer to function or pointer
18391 to member function argument if the set of
18392 overloaded functions does not contain function
18393 templates and at most one of a set of
18394 overloaded functions provides a unique
18395 match. */
18396
18397 if (resolve_overloaded_unification
18398 (tparms, targs, parm, arg, strict,
18399 arg_strict, explain_p))
18400 return unify_success (explain_p);
18401 return unify_overload_resolution_failure (explain_p, arg);
18402 }
18403
18404 arg_expr = arg;
18405 arg = unlowered_expr_type (arg);
18406 if (arg == error_mark_node)
18407 return unify_invalid (explain_p);
18408 }
18409
18410 arg_strict |=
18411 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18412 }
18413 else
18414 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18415 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18416 return unify_template_argument_mismatch (explain_p, parm, arg);
18417
18418 /* For deduction from an init-list we need the actual list. */
18419 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18420 arg = arg_expr;
18421 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18422 }
18423
18424 /* Most parms like fn_type_unification.
18425
18426 If SUBR is 1, we're being called recursively (to unify the
18427 arguments of a function or method parameter of a function
18428 template).
18429
18430 CHECKS is a pointer to a vector of access checks encountered while
18431 substituting default template arguments. */
18432
18433 static int
18434 type_unification_real (tree tparms,
18435 tree full_targs,
18436 tree xparms,
18437 const tree *xargs,
18438 unsigned int xnargs,
18439 int subr,
18440 unification_kind_t strict,
18441 int flags,
18442 vec<deferred_access_check, va_gc> **checks,
18443 bool explain_p)
18444 {
18445 tree parm, arg;
18446 int i;
18447 int ntparms = TREE_VEC_LENGTH (tparms);
18448 int saw_undeduced = 0;
18449 tree parms;
18450 const tree *args;
18451 unsigned int nargs;
18452 unsigned int ia;
18453
18454 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
18455 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
18456 gcc_assert (ntparms > 0);
18457
18458 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
18459
18460 /* Reset the number of non-defaulted template arguments contained
18461 in TARGS. */
18462 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
18463
18464 again:
18465 parms = xparms;
18466 args = xargs;
18467 nargs = xnargs;
18468
18469 ia = 0;
18470 while (parms && parms != void_list_node
18471 && ia < nargs)
18472 {
18473 parm = TREE_VALUE (parms);
18474
18475 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18476 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
18477 /* For a function parameter pack that occurs at the end of the
18478 parameter-declaration-list, the type A of each remaining
18479 argument of the call is compared with the type P of the
18480 declarator-id of the function parameter pack. */
18481 break;
18482
18483 parms = TREE_CHAIN (parms);
18484
18485 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18486 /* For a function parameter pack that does not occur at the
18487 end of the parameter-declaration-list, the type of the
18488 parameter pack is a non-deduced context. */
18489 continue;
18490
18491 arg = args[ia];
18492 ++ia;
18493
18494 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
18495 explain_p))
18496 return 1;
18497 }
18498
18499 if (parms
18500 && parms != void_list_node
18501 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
18502 {
18503 /* Unify the remaining arguments with the pack expansion type. */
18504 tree argvec;
18505 tree parmvec = make_tree_vec (1);
18506
18507 /* Allocate a TREE_VEC and copy in all of the arguments */
18508 argvec = make_tree_vec (nargs - ia);
18509 for (i = 0; ia < nargs; ++ia, ++i)
18510 TREE_VEC_ELT (argvec, i) = args[ia];
18511
18512 /* Copy the parameter into parmvec. */
18513 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
18514 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
18515 /*subr=*/subr, explain_p))
18516 return 1;
18517
18518 /* Advance to the end of the list of parameters. */
18519 parms = TREE_CHAIN (parms);
18520 }
18521
18522 /* Fail if we've reached the end of the parm list, and more args
18523 are present, and the parm list isn't variadic. */
18524 if (ia < nargs && parms == void_list_node)
18525 return unify_too_many_arguments (explain_p, nargs, ia);
18526 /* Fail if parms are left and they don't have default values and
18527 they aren't all deduced as empty packs (c++/57397). This is
18528 consistent with sufficient_parms_p. */
18529 if (parms && parms != void_list_node
18530 && TREE_PURPOSE (parms) == NULL_TREE)
18531 {
18532 unsigned int count = nargs;
18533 tree p = parms;
18534 bool type_pack_p;
18535 do
18536 {
18537 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
18538 if (!type_pack_p)
18539 count++;
18540 p = TREE_CHAIN (p);
18541 }
18542 while (p && p != void_list_node);
18543 if (count != nargs)
18544 return unify_too_few_arguments (explain_p, ia, count,
18545 type_pack_p);
18546 }
18547
18548 if (!subr)
18549 {
18550 tsubst_flags_t complain = (explain_p
18551 ? tf_warning_or_error
18552 : tf_none);
18553
18554 for (i = 0; i < ntparms; i++)
18555 {
18556 tree targ = TREE_VEC_ELT (targs, i);
18557 tree tparm = TREE_VEC_ELT (tparms, i);
18558
18559 /* Clear the "incomplete" flags on all argument packs now so that
18560 substituting them into later default arguments works. */
18561 if (targ && ARGUMENT_PACK_P (targ))
18562 {
18563 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
18564 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
18565 }
18566
18567 if (targ || tparm == error_mark_node)
18568 continue;
18569 tparm = TREE_VALUE (tparm);
18570
18571 /* If this is an undeduced nontype parameter that depends on
18572 a type parameter, try another pass; its type may have been
18573 deduced from a later argument than the one from which
18574 this parameter can be deduced. */
18575 if (TREE_CODE (tparm) == PARM_DECL
18576 && uses_template_parms (TREE_TYPE (tparm))
18577 && saw_undeduced < 2)
18578 {
18579 saw_undeduced = 1;
18580 continue;
18581 }
18582
18583 /* Core issue #226 (C++0x) [temp.deduct]:
18584
18585 If a template argument has not been deduced, its
18586 default template argument, if any, is used.
18587
18588 When we are in C++98 mode, TREE_PURPOSE will either
18589 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18590 to explicitly check cxx_dialect here. */
18591 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
18592 /* OK, there is a default argument. Wait until after the
18593 conversion check to do substitution. */
18594 continue;
18595
18596 /* If the type parameter is a parameter pack, then it will
18597 be deduced to an empty parameter pack. */
18598 if (template_parameter_pack_p (tparm))
18599 {
18600 tree arg;
18601
18602 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
18603 {
18604 arg = make_node (NONTYPE_ARGUMENT_PACK);
18605 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
18606 TREE_CONSTANT (arg) = 1;
18607 }
18608 else
18609 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
18610
18611 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
18612
18613 TREE_VEC_ELT (targs, i) = arg;
18614 continue;
18615 }
18616
18617 return unify_parameter_deduction_failure (explain_p, tparm);
18618 }
18619
18620 /* DR 1391: All parameters have args, now check non-dependent parms for
18621 convertibility. */
18622 if (saw_undeduced < 2)
18623 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
18624 parms && parms != void_list_node && ia < nargs; )
18625 {
18626 parm = TREE_VALUE (parms);
18627
18628 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18629 && (!TREE_CHAIN (parms)
18630 || TREE_CHAIN (parms) == void_list_node))
18631 /* For a function parameter pack that occurs at the end of the
18632 parameter-declaration-list, the type A of each remaining
18633 argument of the call is compared with the type P of the
18634 declarator-id of the function parameter pack. */
18635 break;
18636
18637 parms = TREE_CHAIN (parms);
18638
18639 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18640 /* For a function parameter pack that does not occur at the
18641 end of the parameter-declaration-list, the type of the
18642 parameter pack is a non-deduced context. */
18643 continue;
18644
18645 arg = args[ia];
18646 ++ia;
18647
18648 if (uses_template_parms (parm))
18649 continue;
18650 if (check_non_deducible_conversion (parm, arg, strict, flags,
18651 explain_p))
18652 return 1;
18653 }
18654
18655 /* Now substitute into the default template arguments. */
18656 for (i = 0; i < ntparms; i++)
18657 {
18658 tree targ = TREE_VEC_ELT (targs, i);
18659 tree tparm = TREE_VEC_ELT (tparms, i);
18660
18661 if (targ || tparm == error_mark_node)
18662 continue;
18663 tree parm = TREE_VALUE (tparm);
18664
18665 if (TREE_CODE (parm) == PARM_DECL
18666 && uses_template_parms (TREE_TYPE (parm))
18667 && saw_undeduced < 2)
18668 continue;
18669
18670 tree arg = TREE_PURPOSE (tparm);
18671 reopen_deferring_access_checks (*checks);
18672 location_t save_loc = input_location;
18673 if (DECL_P (parm))
18674 input_location = DECL_SOURCE_LOCATION (parm);
18675 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
18676 arg = convert_template_argument (parm, arg, full_targs, complain,
18677 i, NULL_TREE);
18678 input_location = save_loc;
18679 *checks = get_deferred_access_checks ();
18680 pop_deferring_access_checks ();
18681 if (arg == error_mark_node)
18682 return 1;
18683 else
18684 {
18685 TREE_VEC_ELT (targs, i) = arg;
18686 /* The position of the first default template argument,
18687 is also the number of non-defaulted arguments in TARGS.
18688 Record that. */
18689 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18690 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
18691 continue;
18692 }
18693 }
18694
18695 if (saw_undeduced++ == 1)
18696 goto again;
18697 }
18698
18699 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18700 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
18701
18702 return unify_success (explain_p);
18703 }
18704
18705 /* Subroutine of type_unification_real. Args are like the variables
18706 at the call site. ARG is an overloaded function (or template-id);
18707 we try deducing template args from each of the overloads, and if
18708 only one succeeds, we go with that. Modifies TARGS and returns
18709 true on success. */
18710
18711 static bool
18712 resolve_overloaded_unification (tree tparms,
18713 tree targs,
18714 tree parm,
18715 tree arg,
18716 unification_kind_t strict,
18717 int sub_strict,
18718 bool explain_p)
18719 {
18720 tree tempargs = copy_node (targs);
18721 int good = 0;
18722 tree goodfn = NULL_TREE;
18723 bool addr_p;
18724
18725 if (TREE_CODE (arg) == ADDR_EXPR)
18726 {
18727 arg = TREE_OPERAND (arg, 0);
18728 addr_p = true;
18729 }
18730 else
18731 addr_p = false;
18732
18733 if (TREE_CODE (arg) == COMPONENT_REF)
18734 /* Handle `&x' where `x' is some static or non-static member
18735 function name. */
18736 arg = TREE_OPERAND (arg, 1);
18737
18738 if (TREE_CODE (arg) == OFFSET_REF)
18739 arg = TREE_OPERAND (arg, 1);
18740
18741 /* Strip baselink information. */
18742 if (BASELINK_P (arg))
18743 arg = BASELINK_FUNCTIONS (arg);
18744
18745 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
18746 {
18747 /* If we got some explicit template args, we need to plug them into
18748 the affected templates before we try to unify, in case the
18749 explicit args will completely resolve the templates in question. */
18750
18751 int ok = 0;
18752 tree expl_subargs = TREE_OPERAND (arg, 1);
18753 arg = TREE_OPERAND (arg, 0);
18754
18755 for (; arg; arg = OVL_NEXT (arg))
18756 {
18757 tree fn = OVL_CURRENT (arg);
18758 tree subargs, elem;
18759
18760 if (TREE_CODE (fn) != TEMPLATE_DECL)
18761 continue;
18762
18763 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18764 expl_subargs, NULL_TREE, tf_none,
18765 /*require_all_args=*/true,
18766 /*use_default_args=*/true);
18767 if (subargs != error_mark_node
18768 && !any_dependent_template_arguments_p (subargs))
18769 {
18770 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
18771 if (try_one_overload (tparms, targs, tempargs, parm,
18772 elem, strict, sub_strict, addr_p, explain_p)
18773 && (!goodfn || !same_type_p (goodfn, elem)))
18774 {
18775 goodfn = elem;
18776 ++good;
18777 }
18778 }
18779 else if (subargs)
18780 ++ok;
18781 }
18782 /* If no templates (or more than one) are fully resolved by the
18783 explicit arguments, this template-id is a non-deduced context; it
18784 could still be OK if we deduce all template arguments for the
18785 enclosing call through other arguments. */
18786 if (good != 1)
18787 good = ok;
18788 }
18789 else if (TREE_CODE (arg) != OVERLOAD
18790 && TREE_CODE (arg) != FUNCTION_DECL)
18791 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18792 -- but the deduction does not succeed because the expression is
18793 not just the function on its own. */
18794 return false;
18795 else
18796 for (; arg; arg = OVL_NEXT (arg))
18797 if (try_one_overload (tparms, targs, tempargs, parm,
18798 TREE_TYPE (OVL_CURRENT (arg)),
18799 strict, sub_strict, addr_p, explain_p)
18800 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
18801 {
18802 goodfn = OVL_CURRENT (arg);
18803 ++good;
18804 }
18805
18806 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18807 to function or pointer to member function argument if the set of
18808 overloaded functions does not contain function templates and at most
18809 one of a set of overloaded functions provides a unique match.
18810
18811 So if we found multiple possibilities, we return success but don't
18812 deduce anything. */
18813
18814 if (good == 1)
18815 {
18816 int i = TREE_VEC_LENGTH (targs);
18817 for (; i--; )
18818 if (TREE_VEC_ELT (tempargs, i))
18819 {
18820 tree old = TREE_VEC_ELT (targs, i);
18821 tree new_ = TREE_VEC_ELT (tempargs, i);
18822 if (new_ && old && ARGUMENT_PACK_P (old)
18823 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
18824 /* Don't forget explicit template arguments in a pack. */
18825 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
18826 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
18827 TREE_VEC_ELT (targs, i) = new_;
18828 }
18829 }
18830 if (good)
18831 return true;
18832
18833 return false;
18834 }
18835
18836 /* Core DR 115: In contexts where deduction is done and fails, or in
18837 contexts where deduction is not done, if a template argument list is
18838 specified and it, along with any default template arguments, identifies
18839 a single function template specialization, then the template-id is an
18840 lvalue for the function template specialization. */
18841
18842 tree
18843 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
18844 {
18845 tree expr, offset, baselink;
18846 bool addr;
18847
18848 if (!type_unknown_p (orig_expr))
18849 return orig_expr;
18850
18851 expr = orig_expr;
18852 addr = false;
18853 offset = NULL_TREE;
18854 baselink = NULL_TREE;
18855
18856 if (TREE_CODE (expr) == ADDR_EXPR)
18857 {
18858 expr = TREE_OPERAND (expr, 0);
18859 addr = true;
18860 }
18861 if (TREE_CODE (expr) == OFFSET_REF)
18862 {
18863 offset = expr;
18864 expr = TREE_OPERAND (expr, 1);
18865 }
18866 if (BASELINK_P (expr))
18867 {
18868 baselink = expr;
18869 expr = BASELINK_FUNCTIONS (expr);
18870 }
18871
18872 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
18873 {
18874 int good = 0;
18875 tree goodfn = NULL_TREE;
18876
18877 /* If we got some explicit template args, we need to plug them into
18878 the affected templates before we try to unify, in case the
18879 explicit args will completely resolve the templates in question. */
18880
18881 tree expl_subargs = TREE_OPERAND (expr, 1);
18882 tree arg = TREE_OPERAND (expr, 0);
18883 tree badfn = NULL_TREE;
18884 tree badargs = NULL_TREE;
18885
18886 for (; arg; arg = OVL_NEXT (arg))
18887 {
18888 tree fn = OVL_CURRENT (arg);
18889 tree subargs, elem;
18890
18891 if (TREE_CODE (fn) != TEMPLATE_DECL)
18892 continue;
18893
18894 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18895 expl_subargs, NULL_TREE, tf_none,
18896 /*require_all_args=*/true,
18897 /*use_default_args=*/true);
18898 if (subargs != error_mark_node
18899 && !any_dependent_template_arguments_p (subargs))
18900 {
18901 elem = instantiate_template (fn, subargs, tf_none);
18902 if (elem == error_mark_node)
18903 {
18904 badfn = fn;
18905 badargs = subargs;
18906 }
18907 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
18908 {
18909 goodfn = elem;
18910 ++good;
18911 }
18912 }
18913 }
18914 if (good == 1)
18915 {
18916 mark_used (goodfn);
18917 expr = goodfn;
18918 if (baselink)
18919 expr = build_baselink (BASELINK_BINFO (baselink),
18920 BASELINK_ACCESS_BINFO (baselink),
18921 expr, BASELINK_OPTYPE (baselink));
18922 if (offset)
18923 {
18924 tree base
18925 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
18926 expr = build_offset_ref (base, expr, addr, complain);
18927 }
18928 if (addr)
18929 expr = cp_build_addr_expr (expr, complain);
18930 return expr;
18931 }
18932 else if (good == 0 && badargs && (complain & tf_error))
18933 /* There were no good options and at least one bad one, so let the
18934 user know what the problem is. */
18935 instantiate_template (badfn, badargs, complain);
18936 }
18937 return orig_expr;
18938 }
18939
18940 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18941 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18942 different overloads deduce different arguments for a given parm.
18943 ADDR_P is true if the expression for which deduction is being
18944 performed was of the form "& fn" rather than simply "fn".
18945
18946 Returns 1 on success. */
18947
18948 static int
18949 try_one_overload (tree tparms,
18950 tree orig_targs,
18951 tree targs,
18952 tree parm,
18953 tree arg,
18954 unification_kind_t strict,
18955 int sub_strict,
18956 bool addr_p,
18957 bool explain_p)
18958 {
18959 int nargs;
18960 tree tempargs;
18961 int i;
18962
18963 if (arg == error_mark_node)
18964 return 0;
18965
18966 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18967 to function or pointer to member function argument if the set of
18968 overloaded functions does not contain function templates and at most
18969 one of a set of overloaded functions provides a unique match.
18970
18971 So if this is a template, just return success. */
18972
18973 if (uses_template_parms (arg))
18974 return 1;
18975
18976 if (TREE_CODE (arg) == METHOD_TYPE)
18977 arg = build_ptrmemfunc_type (build_pointer_type (arg));
18978 else if (addr_p)
18979 arg = build_pointer_type (arg);
18980
18981 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
18982
18983 /* We don't copy orig_targs for this because if we have already deduced
18984 some template args from previous args, unify would complain when we
18985 try to deduce a template parameter for the same argument, even though
18986 there isn't really a conflict. */
18987 nargs = TREE_VEC_LENGTH (targs);
18988 tempargs = make_tree_vec (nargs);
18989
18990 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
18991 return 0;
18992
18993 /* First make sure we didn't deduce anything that conflicts with
18994 explicitly specified args. */
18995 for (i = nargs; i--; )
18996 {
18997 tree elt = TREE_VEC_ELT (tempargs, i);
18998 tree oldelt = TREE_VEC_ELT (orig_targs, i);
18999
19000 if (!elt)
19001 /*NOP*/;
19002 else if (uses_template_parms (elt))
19003 /* Since we're unifying against ourselves, we will fill in
19004 template args used in the function parm list with our own
19005 template parms. Discard them. */
19006 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19007 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19008 {
19009 /* Check that the argument at each index of the deduced argument pack
19010 is equivalent to the corresponding explicitly specified argument.
19011 We may have deduced more arguments than were explicitly specified,
19012 and that's OK. */
19013 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
19014 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
19015 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
19016
19017 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19018 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19019
19020 if (TREE_VEC_LENGTH (deduced_pack)
19021 < TREE_VEC_LENGTH (explicit_pack))
19022 return 0;
19023
19024 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19025 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19026 TREE_VEC_ELT (deduced_pack, j)))
19027 return 0;
19028 }
19029 else if (oldelt && !template_args_equal (oldelt, elt))
19030 return 0;
19031 }
19032
19033 for (i = nargs; i--; )
19034 {
19035 tree elt = TREE_VEC_ELT (tempargs, i);
19036
19037 if (elt)
19038 TREE_VEC_ELT (targs, i) = elt;
19039 }
19040
19041 return 1;
19042 }
19043
19044 /* PARM is a template class (perhaps with unbound template
19045 parameters). ARG is a fully instantiated type. If ARG can be
19046 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19047 TARGS are as for unify. */
19048
19049 static tree
19050 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19051 bool explain_p)
19052 {
19053 tree copy_of_targs;
19054
19055 if (!CLASSTYPE_TEMPLATE_INFO (arg)
19056 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19057 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
19058 return NULL_TREE;
19059
19060 /* We need to make a new template argument vector for the call to
19061 unify. If we used TARGS, we'd clutter it up with the result of
19062 the attempted unification, even if this class didn't work out.
19063 We also don't want to commit ourselves to all the unifications
19064 we've already done, since unification is supposed to be done on
19065 an argument-by-argument basis. In other words, consider the
19066 following pathological case:
19067
19068 template <int I, int J, int K>
19069 struct S {};
19070
19071 template <int I, int J>
19072 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19073
19074 template <int I, int J, int K>
19075 void f(S<I, J, K>, S<I, I, I>);
19076
19077 void g() {
19078 S<0, 0, 0> s0;
19079 S<0, 1, 2> s2;
19080
19081 f(s0, s2);
19082 }
19083
19084 Now, by the time we consider the unification involving `s2', we
19085 already know that we must have `f<0, 0, 0>'. But, even though
19086 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19087 because there are two ways to unify base classes of S<0, 1, 2>
19088 with S<I, I, I>. If we kept the already deduced knowledge, we
19089 would reject the possibility I=1. */
19090 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19091
19092 /* If unification failed, we're done. */
19093 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19094 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19095 return NULL_TREE;
19096
19097 return arg;
19098 }
19099
19100 /* Given a template type PARM and a class type ARG, find the unique
19101 base type in ARG that is an instance of PARM. We do not examine
19102 ARG itself; only its base-classes. If there is not exactly one
19103 appropriate base class, return NULL_TREE. PARM may be the type of
19104 a partial specialization, as well as a plain template type. Used
19105 by unify. */
19106
19107 static enum template_base_result
19108 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19109 bool explain_p, tree *result)
19110 {
19111 tree rval = NULL_TREE;
19112 tree binfo;
19113
19114 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19115
19116 binfo = TYPE_BINFO (complete_type (arg));
19117 if (!binfo)
19118 {
19119 /* The type could not be completed. */
19120 *result = NULL_TREE;
19121 return tbr_incomplete_type;
19122 }
19123
19124 /* Walk in inheritance graph order. The search order is not
19125 important, and this avoids multiple walks of virtual bases. */
19126 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19127 {
19128 tree r = try_class_unification (tparms, targs, parm,
19129 BINFO_TYPE (binfo), explain_p);
19130
19131 if (r)
19132 {
19133 /* If there is more than one satisfactory baseclass, then:
19134
19135 [temp.deduct.call]
19136
19137 If they yield more than one possible deduced A, the type
19138 deduction fails.
19139
19140 applies. */
19141 if (rval && !same_type_p (r, rval))
19142 {
19143 *result = NULL_TREE;
19144 return tbr_ambiguous_baseclass;
19145 }
19146
19147 rval = r;
19148 }
19149 }
19150
19151 *result = rval;
19152 return tbr_success;
19153 }
19154
19155 /* Returns the level of DECL, which declares a template parameter. */
19156
19157 static int
19158 template_decl_level (tree decl)
19159 {
19160 switch (TREE_CODE (decl))
19161 {
19162 case TYPE_DECL:
19163 case TEMPLATE_DECL:
19164 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19165
19166 case PARM_DECL:
19167 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19168
19169 default:
19170 gcc_unreachable ();
19171 }
19172 return 0;
19173 }
19174
19175 /* Decide whether ARG can be unified with PARM, considering only the
19176 cv-qualifiers of each type, given STRICT as documented for unify.
19177 Returns nonzero iff the unification is OK on that basis. */
19178
19179 static int
19180 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19181 {
19182 int arg_quals = cp_type_quals (arg);
19183 int parm_quals = cp_type_quals (parm);
19184
19185 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19186 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19187 {
19188 /* Although a CVR qualifier is ignored when being applied to a
19189 substituted template parameter ([8.3.2]/1 for example), that
19190 does not allow us to unify "const T" with "int&" because both
19191 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19192 It is ok when we're allowing additional CV qualifiers
19193 at the outer level [14.8.2.1]/3,1st bullet. */
19194 if ((TREE_CODE (arg) == REFERENCE_TYPE
19195 || TREE_CODE (arg) == FUNCTION_TYPE
19196 || TREE_CODE (arg) == METHOD_TYPE)
19197 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19198 return 0;
19199
19200 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19201 && (parm_quals & TYPE_QUAL_RESTRICT))
19202 return 0;
19203 }
19204
19205 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19206 && (arg_quals & parm_quals) != parm_quals)
19207 return 0;
19208
19209 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19210 && (parm_quals & arg_quals) != arg_quals)
19211 return 0;
19212
19213 return 1;
19214 }
19215
19216 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19217 void
19218 template_parm_level_and_index (tree parm, int* level, int* index)
19219 {
19220 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19221 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19222 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19223 {
19224 *index = TEMPLATE_TYPE_IDX (parm);
19225 *level = TEMPLATE_TYPE_LEVEL (parm);
19226 }
19227 else
19228 {
19229 *index = TEMPLATE_PARM_IDX (parm);
19230 *level = TEMPLATE_PARM_LEVEL (parm);
19231 }
19232 }
19233
19234 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19235 do { \
19236 if (unify (TP, TA, P, A, S, EP)) \
19237 return 1; \
19238 } while (0);
19239
19240 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19241 expansion at the end of PACKED_PARMS. Returns 0 if the type
19242 deduction succeeds, 1 otherwise. STRICT is the same as in
19243 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
19244 call argument list. We'll need to adjust the arguments to make them
19245 types. SUBR tells us if this is from a recursive call to
19246 type_unification_real, or for comparing two template argument
19247 lists. */
19248
19249 static int
19250 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19251 tree packed_args, unification_kind_t strict,
19252 bool subr, bool explain_p)
19253 {
19254 tree parm
19255 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19256 tree pattern = PACK_EXPANSION_PATTERN (parm);
19257 tree pack, packs = NULL_TREE;
19258 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19259
19260 packed_args = expand_template_argument_pack (packed_args);
19261
19262 int len = TREE_VEC_LENGTH (packed_args);
19263
19264 /* Determine the parameter packs we will be deducing from the
19265 pattern, and record their current deductions. */
19266 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19267 pack; pack = TREE_CHAIN (pack))
19268 {
19269 tree parm_pack = TREE_VALUE (pack);
19270 int idx, level;
19271
19272 /* Determine the index and level of this parameter pack. */
19273 template_parm_level_and_index (parm_pack, &level, &idx);
19274
19275 /* Keep track of the parameter packs and their corresponding
19276 argument packs. */
19277 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19278 TREE_TYPE (packs) = make_tree_vec (len - start);
19279 }
19280
19281 /* Loop through all of the arguments that have not yet been
19282 unified and unify each with the pattern. */
19283 for (i = start; i < len; i++)
19284 {
19285 tree parm;
19286 bool any_explicit = false;
19287 tree arg = TREE_VEC_ELT (packed_args, i);
19288
19289 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19290 or the element of its argument pack at the current index if
19291 this argument was explicitly specified. */
19292 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19293 {
19294 int idx, level;
19295 tree arg, pargs;
19296 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19297
19298 arg = NULL_TREE;
19299 if (TREE_VALUE (pack)
19300 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19301 && (i - start < TREE_VEC_LENGTH (pargs)))
19302 {
19303 any_explicit = true;
19304 arg = TREE_VEC_ELT (pargs, i - start);
19305 }
19306 TMPL_ARG (targs, level, idx) = arg;
19307 }
19308
19309 /* If we had explicit template arguments, substitute them into the
19310 pattern before deduction. */
19311 if (any_explicit)
19312 {
19313 /* Some arguments might still be unspecified or dependent. */
19314 bool dependent;
19315 ++processing_template_decl;
19316 dependent = any_dependent_template_arguments_p (targs);
19317 if (!dependent)
19318 --processing_template_decl;
19319 parm = tsubst (pattern, targs,
19320 explain_p ? tf_warning_or_error : tf_none,
19321 NULL_TREE);
19322 if (dependent)
19323 --processing_template_decl;
19324 if (parm == error_mark_node)
19325 return 1;
19326 }
19327 else
19328 parm = pattern;
19329
19330 /* Unify the pattern with the current argument. */
19331 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19332 explain_p))
19333 return 1;
19334
19335 /* For each parameter pack, collect the deduced value. */
19336 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19337 {
19338 int idx, level;
19339 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19340
19341 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19342 TMPL_ARG (targs, level, idx);
19343 }
19344 }
19345
19346 /* Verify that the results of unification with the parameter packs
19347 produce results consistent with what we've seen before, and make
19348 the deduced argument packs available. */
19349 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19350 {
19351 tree old_pack = TREE_VALUE (pack);
19352 tree new_args = TREE_TYPE (pack);
19353 int i, len = TREE_VEC_LENGTH (new_args);
19354 int idx, level;
19355 bool nondeduced_p = false;
19356
19357 /* By default keep the original deduced argument pack.
19358 If necessary, more specific code is going to update the
19359 resulting deduced argument later down in this function. */
19360 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19361 TMPL_ARG (targs, level, idx) = old_pack;
19362
19363 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19364 actually deduce anything. */
19365 for (i = 0; i < len && !nondeduced_p; ++i)
19366 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
19367 nondeduced_p = true;
19368 if (nondeduced_p)
19369 continue;
19370
19371 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
19372 {
19373 /* If we had fewer function args than explicit template args,
19374 just use the explicits. */
19375 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19376 int explicit_len = TREE_VEC_LENGTH (explicit_args);
19377 if (len < explicit_len)
19378 new_args = explicit_args;
19379 }
19380
19381 if (!old_pack)
19382 {
19383 tree result;
19384 /* Build the deduced *_ARGUMENT_PACK. */
19385 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
19386 {
19387 result = make_node (NONTYPE_ARGUMENT_PACK);
19388 TREE_TYPE (result) =
19389 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
19390 TREE_CONSTANT (result) = 1;
19391 }
19392 else
19393 result = cxx_make_type (TYPE_ARGUMENT_PACK);
19394
19395 SET_ARGUMENT_PACK_ARGS (result, new_args);
19396
19397 /* Note the deduced argument packs for this parameter
19398 pack. */
19399 TMPL_ARG (targs, level, idx) = result;
19400 }
19401 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
19402 && (ARGUMENT_PACK_ARGS (old_pack)
19403 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
19404 {
19405 /* We only had the explicitly-provided arguments before, but
19406 now we have a complete set of arguments. */
19407 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19408
19409 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
19410 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
19411 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
19412 }
19413 else
19414 {
19415 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
19416 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
19417
19418 if (!comp_template_args (old_args, new_args,
19419 &bad_old_arg, &bad_new_arg))
19420 /* Inconsistent unification of this parameter pack. */
19421 return unify_parameter_pack_inconsistent (explain_p,
19422 bad_old_arg,
19423 bad_new_arg);
19424 }
19425 }
19426
19427 return unify_success (explain_p);
19428 }
19429
19430 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19431 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19432 parameters and return value are as for unify. */
19433
19434 static int
19435 unify_array_domain (tree tparms, tree targs,
19436 tree parm_dom, tree arg_dom,
19437 bool explain_p)
19438 {
19439 tree parm_max;
19440 tree arg_max;
19441 bool parm_cst;
19442 bool arg_cst;
19443
19444 /* Our representation of array types uses "N - 1" as the
19445 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19446 not an integer constant. We cannot unify arbitrarily
19447 complex expressions, so we eliminate the MINUS_EXPRs
19448 here. */
19449 parm_max = TYPE_MAX_VALUE (parm_dom);
19450 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
19451 if (!parm_cst)
19452 {
19453 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
19454 parm_max = TREE_OPERAND (parm_max, 0);
19455 }
19456 arg_max = TYPE_MAX_VALUE (arg_dom);
19457 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
19458 if (!arg_cst)
19459 {
19460 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19461 trying to unify the type of a variable with the type
19462 of a template parameter. For example:
19463
19464 template <unsigned int N>
19465 void f (char (&) [N]);
19466 int g();
19467 void h(int i) {
19468 char a[g(i)];
19469 f(a);
19470 }
19471
19472 Here, the type of the ARG will be "int [g(i)]", and
19473 may be a SAVE_EXPR, etc. */
19474 if (TREE_CODE (arg_max) != MINUS_EXPR)
19475 return unify_vla_arg (explain_p, arg_dom);
19476 arg_max = TREE_OPERAND (arg_max, 0);
19477 }
19478
19479 /* If only one of the bounds used a MINUS_EXPR, compensate
19480 by adding one to the other bound. */
19481 if (parm_cst && !arg_cst)
19482 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
19483 integer_type_node,
19484 parm_max,
19485 integer_one_node);
19486 else if (arg_cst && !parm_cst)
19487 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
19488 integer_type_node,
19489 arg_max,
19490 integer_one_node);
19491
19492 return unify (tparms, targs, parm_max, arg_max,
19493 UNIFY_ALLOW_INTEGER, explain_p);
19494 }
19495
19496 /* Deduce the value of template parameters. TPARMS is the (innermost)
19497 set of template parameters to a template. TARGS is the bindings
19498 for those template parameters, as determined thus far; TARGS may
19499 include template arguments for outer levels of template parameters
19500 as well. PARM is a parameter to a template function, or a
19501 subcomponent of that parameter; ARG is the corresponding argument.
19502 This function attempts to match PARM with ARG in a manner
19503 consistent with the existing assignments in TARGS. If more values
19504 are deduced, then TARGS is updated.
19505
19506 Returns 0 if the type deduction succeeds, 1 otherwise. The
19507 parameter STRICT is a bitwise or of the following flags:
19508
19509 UNIFY_ALLOW_NONE:
19510 Require an exact match between PARM and ARG.
19511 UNIFY_ALLOW_MORE_CV_QUAL:
19512 Allow the deduced ARG to be more cv-qualified (by qualification
19513 conversion) than ARG.
19514 UNIFY_ALLOW_LESS_CV_QUAL:
19515 Allow the deduced ARG to be less cv-qualified than ARG.
19516 UNIFY_ALLOW_DERIVED:
19517 Allow the deduced ARG to be a template base class of ARG,
19518 or a pointer to a template base class of the type pointed to by
19519 ARG.
19520 UNIFY_ALLOW_INTEGER:
19521 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19522 case for more information.
19523 UNIFY_ALLOW_OUTER_LEVEL:
19524 This is the outermost level of a deduction. Used to determine validity
19525 of qualification conversions. A valid qualification conversion must
19526 have const qualified pointers leading up to the inner type which
19527 requires additional CV quals, except at the outer level, where const
19528 is not required [conv.qual]. It would be normal to set this flag in
19529 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19530 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19531 This is the outermost level of a deduction, and PARM can be more CV
19532 qualified at this point.
19533 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19534 This is the outermost level of a deduction, and PARM can be less CV
19535 qualified at this point. */
19536
19537 static int
19538 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
19539 bool explain_p)
19540 {
19541 int idx;
19542 tree targ;
19543 tree tparm;
19544 int strict_in = strict;
19545
19546 /* I don't think this will do the right thing with respect to types.
19547 But the only case I've seen it in so far has been array bounds, where
19548 signedness is the only information lost, and I think that will be
19549 okay. */
19550 while (TREE_CODE (parm) == NOP_EXPR)
19551 parm = TREE_OPERAND (parm, 0);
19552
19553 if (arg == error_mark_node)
19554 return unify_invalid (explain_p);
19555 if (arg == unknown_type_node
19556 || arg == init_list_type_node)
19557 /* We can't deduce anything from this, but we might get all the
19558 template args from other function args. */
19559 return unify_success (explain_p);
19560
19561 /* If PARM uses template parameters, then we can't bail out here,
19562 even if ARG == PARM, since we won't record unifications for the
19563 template parameters. We might need them if we're trying to
19564 figure out which of two things is more specialized. */
19565 if (arg == parm && !uses_template_parms (parm))
19566 return unify_success (explain_p);
19567
19568 /* Handle init lists early, so the rest of the function can assume
19569 we're dealing with a type. */
19570 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
19571 {
19572 tree elt, elttype;
19573 unsigned i;
19574 tree orig_parm = parm;
19575
19576 /* Replace T with std::initializer_list<T> for deduction. */
19577 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19578 && flag_deduce_init_list)
19579 parm = listify (parm);
19580
19581 if (!is_std_init_list (parm)
19582 && TREE_CODE (parm) != ARRAY_TYPE)
19583 /* We can only deduce from an initializer list argument if the
19584 parameter is std::initializer_list or an array; otherwise this
19585 is a non-deduced context. */
19586 return unify_success (explain_p);
19587
19588 if (TREE_CODE (parm) == ARRAY_TYPE)
19589 elttype = TREE_TYPE (parm);
19590 else
19591 {
19592 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
19593 /* Deduction is defined in terms of a single type, so just punt
19594 on the (bizarre) std::initializer_list<T...>. */
19595 if (PACK_EXPANSION_P (elttype))
19596 return unify_success (explain_p);
19597 }
19598
19599 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
19600 {
19601 int elt_strict = strict;
19602
19603 if (elt == error_mark_node)
19604 return unify_invalid (explain_p);
19605
19606 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
19607 {
19608 tree type = TREE_TYPE (elt);
19609 if (type == error_mark_node)
19610 return unify_invalid (explain_p);
19611 /* It should only be possible to get here for a call. */
19612 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
19613 elt_strict |= maybe_adjust_types_for_deduction
19614 (DEDUCE_CALL, &elttype, &type, elt);
19615 elt = type;
19616 }
19617
19618 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
19619 explain_p);
19620 }
19621
19622 if (TREE_CODE (parm) == ARRAY_TYPE
19623 && deducible_array_bound (TYPE_DOMAIN (parm)))
19624 {
19625 /* Also deduce from the length of the initializer list. */
19626 tree max = size_int (CONSTRUCTOR_NELTS (arg));
19627 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
19628 if (idx == error_mark_node)
19629 return unify_invalid (explain_p);
19630 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19631 idx, explain_p);
19632 }
19633
19634 /* If the std::initializer_list<T> deduction worked, replace the
19635 deduced A with std::initializer_list<A>. */
19636 if (orig_parm != parm)
19637 {
19638 idx = TEMPLATE_TYPE_IDX (orig_parm);
19639 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19640 targ = listify (targ);
19641 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
19642 }
19643 return unify_success (explain_p);
19644 }
19645
19646 /* Immediately reject some pairs that won't unify because of
19647 cv-qualification mismatches. */
19648 if (TREE_CODE (arg) == TREE_CODE (parm)
19649 && TYPE_P (arg)
19650 /* It is the elements of the array which hold the cv quals of an array
19651 type, and the elements might be template type parms. We'll check
19652 when we recurse. */
19653 && TREE_CODE (arg) != ARRAY_TYPE
19654 /* We check the cv-qualifiers when unifying with template type
19655 parameters below. We want to allow ARG `const T' to unify with
19656 PARM `T' for example, when computing which of two templates
19657 is more specialized, for example. */
19658 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
19659 && !check_cv_quals_for_unify (strict_in, arg, parm))
19660 return unify_cv_qual_mismatch (explain_p, parm, arg);
19661
19662 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
19663 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
19664 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
19665 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
19666 strict &= ~UNIFY_ALLOW_DERIVED;
19667 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19668 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
19669
19670 switch (TREE_CODE (parm))
19671 {
19672 case TYPENAME_TYPE:
19673 case SCOPE_REF:
19674 case UNBOUND_CLASS_TEMPLATE:
19675 /* In a type which contains a nested-name-specifier, template
19676 argument values cannot be deduced for template parameters used
19677 within the nested-name-specifier. */
19678 return unify_success (explain_p);
19679
19680 case TEMPLATE_TYPE_PARM:
19681 case TEMPLATE_TEMPLATE_PARM:
19682 case BOUND_TEMPLATE_TEMPLATE_PARM:
19683 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19684 if (error_operand_p (tparm))
19685 return unify_invalid (explain_p);
19686
19687 if (TEMPLATE_TYPE_LEVEL (parm)
19688 != template_decl_level (tparm))
19689 /* The PARM is not one we're trying to unify. Just check
19690 to see if it matches ARG. */
19691 {
19692 if (TREE_CODE (arg) == TREE_CODE (parm)
19693 && (is_auto (parm) ? is_auto (arg)
19694 : same_type_p (parm, arg)))
19695 return unify_success (explain_p);
19696 else
19697 return unify_type_mismatch (explain_p, parm, arg);
19698 }
19699 idx = TEMPLATE_TYPE_IDX (parm);
19700 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19701 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
19702 if (error_operand_p (tparm))
19703 return unify_invalid (explain_p);
19704
19705 /* Check for mixed types and values. */
19706 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19707 && TREE_CODE (tparm) != TYPE_DECL)
19708 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19709 && TREE_CODE (tparm) != TEMPLATE_DECL))
19710 gcc_unreachable ();
19711
19712 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19713 {
19714 /* ARG must be constructed from a template class or a template
19715 template parameter. */
19716 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
19717 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19718 return unify_template_deduction_failure (explain_p, parm, arg);
19719 {
19720 tree parmvec = TYPE_TI_ARGS (parm);
19721 /* An alias template name is never deduced. */
19722 if (TYPE_ALIAS_P (arg))
19723 arg = strip_typedefs (arg);
19724 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
19725 tree full_argvec = add_to_template_args (targs, argvec);
19726 tree parm_parms
19727 = DECL_INNERMOST_TEMPLATE_PARMS
19728 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
19729 int i, len;
19730 int parm_variadic_p = 0;
19731
19732 /* The resolution to DR150 makes clear that default
19733 arguments for an N-argument may not be used to bind T
19734 to a template template parameter with fewer than N
19735 parameters. It is not safe to permit the binding of
19736 default arguments as an extension, as that may change
19737 the meaning of a conforming program. Consider:
19738
19739 struct Dense { static const unsigned int dim = 1; };
19740
19741 template <template <typename> class View,
19742 typename Block>
19743 void operator+(float, View<Block> const&);
19744
19745 template <typename Block,
19746 unsigned int Dim = Block::dim>
19747 struct Lvalue_proxy { operator float() const; };
19748
19749 void
19750 test_1d (void) {
19751 Lvalue_proxy<Dense> p;
19752 float b;
19753 b + p;
19754 }
19755
19756 Here, if Lvalue_proxy is permitted to bind to View, then
19757 the global operator+ will be used; if they are not, the
19758 Lvalue_proxy will be converted to float. */
19759 if (coerce_template_parms (parm_parms,
19760 full_argvec,
19761 TYPE_TI_TEMPLATE (parm),
19762 (explain_p
19763 ? tf_warning_or_error
19764 : tf_none),
19765 /*require_all_args=*/true,
19766 /*use_default_args=*/false)
19767 == error_mark_node)
19768 return 1;
19769
19770 /* Deduce arguments T, i from TT<T> or TT<i>.
19771 We check each element of PARMVEC and ARGVEC individually
19772 rather than the whole TREE_VEC since they can have
19773 different number of elements. */
19774
19775 parmvec = expand_template_argument_pack (parmvec);
19776 argvec = expand_template_argument_pack (argvec);
19777
19778 len = TREE_VEC_LENGTH (parmvec);
19779
19780 /* Check if the parameters end in a pack, making them
19781 variadic. */
19782 if (len > 0
19783 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
19784 parm_variadic_p = 1;
19785
19786 for (i = 0; i < len - parm_variadic_p; ++i)
19787 /* If the template argument list of P contains a pack
19788 expansion that is not the last template argument, the
19789 entire template argument list is a non-deduced
19790 context. */
19791 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
19792 return unify_success (explain_p);
19793
19794 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
19795 return unify_too_few_arguments (explain_p,
19796 TREE_VEC_LENGTH (argvec), len);
19797
19798 for (i = 0; i < len - parm_variadic_p; ++i)
19799 {
19800 RECUR_AND_CHECK_FAILURE (tparms, targs,
19801 TREE_VEC_ELT (parmvec, i),
19802 TREE_VEC_ELT (argvec, i),
19803 UNIFY_ALLOW_NONE, explain_p);
19804 }
19805
19806 if (parm_variadic_p
19807 && unify_pack_expansion (tparms, targs,
19808 parmvec, argvec,
19809 DEDUCE_EXACT,
19810 /*subr=*/true, explain_p))
19811 return 1;
19812 }
19813 arg = TYPE_TI_TEMPLATE (arg);
19814
19815 /* Fall through to deduce template name. */
19816 }
19817
19818 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19819 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19820 {
19821 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19822
19823 /* Simple cases: Value already set, does match or doesn't. */
19824 if (targ != NULL_TREE && template_args_equal (targ, arg))
19825 return unify_success (explain_p);
19826 else if (targ)
19827 return unify_inconsistency (explain_p, parm, targ, arg);
19828 }
19829 else
19830 {
19831 /* If PARM is `const T' and ARG is only `int', we don't have
19832 a match unless we are allowing additional qualification.
19833 If ARG is `const int' and PARM is just `T' that's OK;
19834 that binds `const int' to `T'. */
19835 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
19836 arg, parm))
19837 return unify_cv_qual_mismatch (explain_p, parm, arg);
19838
19839 /* Consider the case where ARG is `const volatile int' and
19840 PARM is `const T'. Then, T should be `volatile int'. */
19841 arg = cp_build_qualified_type_real
19842 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
19843 if (arg == error_mark_node)
19844 return unify_invalid (explain_p);
19845
19846 /* Simple cases: Value already set, does match or doesn't. */
19847 if (targ != NULL_TREE && same_type_p (targ, arg))
19848 return unify_success (explain_p);
19849 else if (targ)
19850 return unify_inconsistency (explain_p, parm, targ, arg);
19851
19852 /* Make sure that ARG is not a variable-sized array. (Note
19853 that were talking about variable-sized arrays (like
19854 `int[n]'), rather than arrays of unknown size (like
19855 `int[]').) We'll get very confused by such a type since
19856 the bound of the array is not constant, and therefore
19857 not mangleable. Besides, such types are not allowed in
19858 ISO C++, so we can do as we please here. We do allow
19859 them for 'auto' deduction, since that isn't ABI-exposed. */
19860 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
19861 return unify_vla_arg (explain_p, arg);
19862
19863 /* Strip typedefs as in convert_template_argument. */
19864 arg = canonicalize_type_argument (arg, tf_none);
19865 }
19866
19867 /* If ARG is a parameter pack or an expansion, we cannot unify
19868 against it unless PARM is also a parameter pack. */
19869 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19870 && !template_parameter_pack_p (parm))
19871 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19872
19873 /* If the argument deduction results is a METHOD_TYPE,
19874 then there is a problem.
19875 METHOD_TYPE doesn't map to any real C++ type the result of
19876 the deduction can not be of that type. */
19877 if (TREE_CODE (arg) == METHOD_TYPE)
19878 return unify_method_type_error (explain_p, arg);
19879
19880 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19881 return unify_success (explain_p);
19882
19883 case TEMPLATE_PARM_INDEX:
19884 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19885 if (error_operand_p (tparm))
19886 return unify_invalid (explain_p);
19887
19888 if (TEMPLATE_PARM_LEVEL (parm)
19889 != template_decl_level (tparm))
19890 {
19891 /* The PARM is not one we're trying to unify. Just check
19892 to see if it matches ARG. */
19893 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
19894 && cp_tree_equal (parm, arg));
19895 if (result)
19896 unify_expression_unequal (explain_p, parm, arg);
19897 return result;
19898 }
19899
19900 idx = TEMPLATE_PARM_IDX (parm);
19901 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19902
19903 if (targ)
19904 {
19905 int x = !cp_tree_equal (targ, arg);
19906 if (x)
19907 unify_inconsistency (explain_p, parm, targ, arg);
19908 return x;
19909 }
19910
19911 /* [temp.deduct.type] If, in the declaration of a function template
19912 with a non-type template-parameter, the non-type
19913 template-parameter is used in an expression in the function
19914 parameter-list and, if the corresponding template-argument is
19915 deduced, the template-argument type shall match the type of the
19916 template-parameter exactly, except that a template-argument
19917 deduced from an array bound may be of any integral type.
19918 The non-type parameter might use already deduced type parameters. */
19919 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
19920 if (!TREE_TYPE (arg))
19921 /* Template-parameter dependent expression. Just accept it for now.
19922 It will later be processed in convert_template_argument. */
19923 ;
19924 else if (same_type_p (TREE_TYPE (arg), tparm))
19925 /* OK */;
19926 else if ((strict & UNIFY_ALLOW_INTEGER)
19927 && CP_INTEGRAL_TYPE_P (tparm))
19928 /* Convert the ARG to the type of PARM; the deduced non-type
19929 template argument must exactly match the types of the
19930 corresponding parameter. */
19931 arg = fold (build_nop (tparm, arg));
19932 else if (uses_template_parms (tparm))
19933 /* We haven't deduced the type of this parameter yet. Try again
19934 later. */
19935 return unify_success (explain_p);
19936 else
19937 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
19938
19939 /* If ARG is a parameter pack or an expansion, we cannot unify
19940 against it unless PARM is also a parameter pack. */
19941 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19942 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
19943 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19944
19945 {
19946 bool removed_attr = false;
19947 arg = strip_typedefs_expr (arg, &removed_attr);
19948 }
19949 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19950 return unify_success (explain_p);
19951
19952 case PTRMEM_CST:
19953 {
19954 /* A pointer-to-member constant can be unified only with
19955 another constant. */
19956 if (TREE_CODE (arg) != PTRMEM_CST)
19957 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
19958
19959 /* Just unify the class member. It would be useless (and possibly
19960 wrong, depending on the strict flags) to unify also
19961 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19962 arg refer to the same variable, even if through different
19963 classes. For instance:
19964
19965 struct A { int x; };
19966 struct B : A { };
19967
19968 Unification of &A::x and &B::x must succeed. */
19969 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
19970 PTRMEM_CST_MEMBER (arg), strict, explain_p);
19971 }
19972
19973 case POINTER_TYPE:
19974 {
19975 if (!TYPE_PTR_P (arg))
19976 return unify_type_mismatch (explain_p, parm, arg);
19977
19978 /* [temp.deduct.call]
19979
19980 A can be another pointer or pointer to member type that can
19981 be converted to the deduced A via a qualification
19982 conversion (_conv.qual_).
19983
19984 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19985 This will allow for additional cv-qualification of the
19986 pointed-to types if appropriate. */
19987
19988 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
19989 /* The derived-to-base conversion only persists through one
19990 level of pointers. */
19991 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
19992
19993 return unify (tparms, targs, TREE_TYPE (parm),
19994 TREE_TYPE (arg), strict, explain_p);
19995 }
19996
19997 case REFERENCE_TYPE:
19998 if (TREE_CODE (arg) != REFERENCE_TYPE)
19999 return unify_type_mismatch (explain_p, parm, arg);
20000 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20001 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20002
20003 case ARRAY_TYPE:
20004 if (TREE_CODE (arg) != ARRAY_TYPE)
20005 return unify_type_mismatch (explain_p, parm, arg);
20006 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20007 != (TYPE_DOMAIN (arg) == NULL_TREE))
20008 return unify_type_mismatch (explain_p, parm, arg);
20009 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20010 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20011 if (TYPE_DOMAIN (parm) != NULL_TREE)
20012 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20013 TYPE_DOMAIN (arg), explain_p);
20014 return unify_success (explain_p);
20015
20016 case REAL_TYPE:
20017 case COMPLEX_TYPE:
20018 case VECTOR_TYPE:
20019 case INTEGER_TYPE:
20020 case BOOLEAN_TYPE:
20021 case ENUMERAL_TYPE:
20022 case VOID_TYPE:
20023 case NULLPTR_TYPE:
20024 if (TREE_CODE (arg) != TREE_CODE (parm))
20025 return unify_type_mismatch (explain_p, parm, arg);
20026
20027 /* We have already checked cv-qualification at the top of the
20028 function. */
20029 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20030 return unify_type_mismatch (explain_p, parm, arg);
20031
20032 /* As far as unification is concerned, this wins. Later checks
20033 will invalidate it if necessary. */
20034 return unify_success (explain_p);
20035
20036 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20037 /* Type INTEGER_CST can come from ordinary constant template args. */
20038 case INTEGER_CST:
20039 while (TREE_CODE (arg) == NOP_EXPR)
20040 arg = TREE_OPERAND (arg, 0);
20041
20042 if (TREE_CODE (arg) != INTEGER_CST)
20043 return unify_template_argument_mismatch (explain_p, parm, arg);
20044 return (tree_int_cst_equal (parm, arg)
20045 ? unify_success (explain_p)
20046 : unify_template_argument_mismatch (explain_p, parm, arg));
20047
20048 case TREE_VEC:
20049 {
20050 int i, len, argslen;
20051 int parm_variadic_p = 0;
20052
20053 if (TREE_CODE (arg) != TREE_VEC)
20054 return unify_template_argument_mismatch (explain_p, parm, arg);
20055
20056 len = TREE_VEC_LENGTH (parm);
20057 argslen = TREE_VEC_LENGTH (arg);
20058
20059 /* Check for pack expansions in the parameters. */
20060 for (i = 0; i < len; ++i)
20061 {
20062 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20063 {
20064 if (i == len - 1)
20065 /* We can unify against something with a trailing
20066 parameter pack. */
20067 parm_variadic_p = 1;
20068 else
20069 /* [temp.deduct.type]/9: If the template argument list of
20070 P contains a pack expansion that is not the last
20071 template argument, the entire template argument list
20072 is a non-deduced context. */
20073 return unify_success (explain_p);
20074 }
20075 }
20076
20077 /* If we don't have enough arguments to satisfy the parameters
20078 (not counting the pack expression at the end), or we have
20079 too many arguments for a parameter list that doesn't end in
20080 a pack expression, we can't unify. */
20081 if (parm_variadic_p
20082 ? argslen < len - parm_variadic_p
20083 : argslen != len)
20084 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20085
20086 /* Unify all of the parameters that precede the (optional)
20087 pack expression. */
20088 for (i = 0; i < len - parm_variadic_p; ++i)
20089 {
20090 RECUR_AND_CHECK_FAILURE (tparms, targs,
20091 TREE_VEC_ELT (parm, i),
20092 TREE_VEC_ELT (arg, i),
20093 UNIFY_ALLOW_NONE, explain_p);
20094 }
20095 if (parm_variadic_p)
20096 return unify_pack_expansion (tparms, targs, parm, arg,
20097 DEDUCE_EXACT,
20098 /*subr=*/true, explain_p);
20099 return unify_success (explain_p);
20100 }
20101
20102 case RECORD_TYPE:
20103 case UNION_TYPE:
20104 if (TREE_CODE (arg) != TREE_CODE (parm))
20105 return unify_type_mismatch (explain_p, parm, arg);
20106
20107 if (TYPE_PTRMEMFUNC_P (parm))
20108 {
20109 if (!TYPE_PTRMEMFUNC_P (arg))
20110 return unify_type_mismatch (explain_p, parm, arg);
20111
20112 return unify (tparms, targs,
20113 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20114 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20115 strict, explain_p);
20116 }
20117 else if (TYPE_PTRMEMFUNC_P (arg))
20118 return unify_type_mismatch (explain_p, parm, arg);
20119
20120 if (CLASSTYPE_TEMPLATE_INFO (parm))
20121 {
20122 tree t = NULL_TREE;
20123
20124 if (strict_in & UNIFY_ALLOW_DERIVED)
20125 {
20126 /* First, we try to unify the PARM and ARG directly. */
20127 t = try_class_unification (tparms, targs,
20128 parm, arg, explain_p);
20129
20130 if (!t)
20131 {
20132 /* Fallback to the special case allowed in
20133 [temp.deduct.call]:
20134
20135 If P is a class, and P has the form
20136 template-id, then A can be a derived class of
20137 the deduced A. Likewise, if P is a pointer to
20138 a class of the form template-id, A can be a
20139 pointer to a derived class pointed to by the
20140 deduced A. */
20141 enum template_base_result r;
20142 r = get_template_base (tparms, targs, parm, arg,
20143 explain_p, &t);
20144
20145 if (!t)
20146 {
20147 /* Don't give the derived diagnostic if we're
20148 already dealing with the same template. */
20149 bool same_template
20150 = (CLASSTYPE_TEMPLATE_INFO (arg)
20151 && (CLASSTYPE_TI_TEMPLATE (parm)
20152 == CLASSTYPE_TI_TEMPLATE (arg)));
20153 return unify_no_common_base (explain_p && !same_template,
20154 r, parm, arg);
20155 }
20156 }
20157 }
20158 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20159 && (CLASSTYPE_TI_TEMPLATE (parm)
20160 == CLASSTYPE_TI_TEMPLATE (arg)))
20161 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20162 Then, we should unify `int' and `U'. */
20163 t = arg;
20164 else
20165 /* There's no chance of unification succeeding. */
20166 return unify_type_mismatch (explain_p, parm, arg);
20167
20168 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20169 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20170 }
20171 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20172 return unify_type_mismatch (explain_p, parm, arg);
20173 return unify_success (explain_p);
20174
20175 case METHOD_TYPE:
20176 case FUNCTION_TYPE:
20177 {
20178 unsigned int nargs;
20179 tree *args;
20180 tree a;
20181 unsigned int i;
20182
20183 if (TREE_CODE (arg) != TREE_CODE (parm))
20184 return unify_type_mismatch (explain_p, parm, arg);
20185
20186 /* CV qualifications for methods can never be deduced, they must
20187 match exactly. We need to check them explicitly here,
20188 because type_unification_real treats them as any other
20189 cv-qualified parameter. */
20190 if (TREE_CODE (parm) == METHOD_TYPE
20191 && (!check_cv_quals_for_unify
20192 (UNIFY_ALLOW_NONE,
20193 class_of_this_parm (arg),
20194 class_of_this_parm (parm))))
20195 return unify_cv_qual_mismatch (explain_p, parm, arg);
20196
20197 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20198 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20199
20200 nargs = list_length (TYPE_ARG_TYPES (arg));
20201 args = XALLOCAVEC (tree, nargs);
20202 for (a = TYPE_ARG_TYPES (arg), i = 0;
20203 a != NULL_TREE && a != void_list_node;
20204 a = TREE_CHAIN (a), ++i)
20205 args[i] = TREE_VALUE (a);
20206 nargs = i;
20207
20208 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20209 args, nargs, 1, DEDUCE_EXACT,
20210 LOOKUP_NORMAL, NULL, explain_p);
20211 }
20212
20213 case OFFSET_TYPE:
20214 /* Unify a pointer to member with a pointer to member function, which
20215 deduces the type of the member as a function type. */
20216 if (TYPE_PTRMEMFUNC_P (arg))
20217 {
20218 /* Check top-level cv qualifiers */
20219 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20220 return unify_cv_qual_mismatch (explain_p, parm, arg);
20221
20222 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20223 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20224 UNIFY_ALLOW_NONE, explain_p);
20225
20226 /* Determine the type of the function we are unifying against. */
20227 tree fntype = static_fn_type (arg);
20228
20229 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20230 }
20231
20232 if (TREE_CODE (arg) != OFFSET_TYPE)
20233 return unify_type_mismatch (explain_p, parm, arg);
20234 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20235 TYPE_OFFSET_BASETYPE (arg),
20236 UNIFY_ALLOW_NONE, explain_p);
20237 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20238 strict, explain_p);
20239
20240 case CONST_DECL:
20241 if (DECL_TEMPLATE_PARM_P (parm))
20242 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20243 if (arg != scalar_constant_value (parm))
20244 return unify_template_argument_mismatch (explain_p, parm, arg);
20245 return unify_success (explain_p);
20246
20247 case FIELD_DECL:
20248 case TEMPLATE_DECL:
20249 /* Matched cases are handled by the ARG == PARM test above. */
20250 return unify_template_argument_mismatch (explain_p, parm, arg);
20251
20252 case VAR_DECL:
20253 /* We might get a variable as a non-type template argument in parm if the
20254 corresponding parameter is type-dependent. Make any necessary
20255 adjustments based on whether arg is a reference. */
20256 if (CONSTANT_CLASS_P (arg))
20257 parm = fold_non_dependent_expr (parm);
20258 else if (REFERENCE_REF_P (arg))
20259 {
20260 tree sub = TREE_OPERAND (arg, 0);
20261 STRIP_NOPS (sub);
20262 if (TREE_CODE (sub) == ADDR_EXPR)
20263 arg = TREE_OPERAND (sub, 0);
20264 }
20265 /* Now use the normal expression code to check whether they match. */
20266 goto expr;
20267
20268 case TYPE_ARGUMENT_PACK:
20269 case NONTYPE_ARGUMENT_PACK:
20270 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20271 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20272
20273 case TYPEOF_TYPE:
20274 case DECLTYPE_TYPE:
20275 case UNDERLYING_TYPE:
20276 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20277 or UNDERLYING_TYPE nodes. */
20278 return unify_success (explain_p);
20279
20280 case ERROR_MARK:
20281 /* Unification fails if we hit an error node. */
20282 return unify_invalid (explain_p);
20283
20284 case INDIRECT_REF:
20285 if (REFERENCE_REF_P (parm))
20286 {
20287 if (REFERENCE_REF_P (arg))
20288 arg = TREE_OPERAND (arg, 0);
20289 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20290 strict, explain_p);
20291 }
20292 /* FALLTHRU */
20293
20294 default:
20295 /* An unresolved overload is a nondeduced context. */
20296 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20297 return unify_success (explain_p);
20298 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20299 expr:
20300 /* We must be looking at an expression. This can happen with
20301 something like:
20302
20303 template <int I>
20304 void foo(S<I>, S<I + 2>);
20305
20306 This is a "nondeduced context":
20307
20308 [deduct.type]
20309
20310 The nondeduced contexts are:
20311
20312 --A type that is a template-id in which one or more of
20313 the template-arguments is an expression that references
20314 a template-parameter.
20315
20316 In these cases, we assume deduction succeeded, but don't
20317 actually infer any unifications. */
20318
20319 if (!uses_template_parms (parm)
20320 && !template_args_equal (parm, arg))
20321 return unify_expression_unequal (explain_p, parm, arg);
20322 else
20323 return unify_success (explain_p);
20324 }
20325 }
20326 #undef RECUR_AND_CHECK_FAILURE
20327 \f
20328 /* Note that DECL can be defined in this translation unit, if
20329 required. */
20330
20331 static void
20332 mark_definable (tree decl)
20333 {
20334 tree clone;
20335 DECL_NOT_REALLY_EXTERN (decl) = 1;
20336 FOR_EACH_CLONE (clone, decl)
20337 DECL_NOT_REALLY_EXTERN (clone) = 1;
20338 }
20339
20340 /* Called if RESULT is explicitly instantiated, or is a member of an
20341 explicitly instantiated class. */
20342
20343 void
20344 mark_decl_instantiated (tree result, int extern_p)
20345 {
20346 SET_DECL_EXPLICIT_INSTANTIATION (result);
20347
20348 /* If this entity has already been written out, it's too late to
20349 make any modifications. */
20350 if (TREE_ASM_WRITTEN (result))
20351 return;
20352
20353 /* For anonymous namespace we don't need to do anything. */
20354 if (decl_anon_ns_mem_p (result))
20355 {
20356 gcc_assert (!TREE_PUBLIC (result));
20357 return;
20358 }
20359
20360 if (TREE_CODE (result) != FUNCTION_DECL)
20361 /* The TREE_PUBLIC flag for function declarations will have been
20362 set correctly by tsubst. */
20363 TREE_PUBLIC (result) = 1;
20364
20365 /* This might have been set by an earlier implicit instantiation. */
20366 DECL_COMDAT (result) = 0;
20367
20368 if (extern_p)
20369 DECL_NOT_REALLY_EXTERN (result) = 0;
20370 else
20371 {
20372 mark_definable (result);
20373 mark_needed (result);
20374 /* Always make artificials weak. */
20375 if (DECL_ARTIFICIAL (result) && flag_weak)
20376 comdat_linkage (result);
20377 /* For WIN32 we also want to put explicit instantiations in
20378 linkonce sections. */
20379 else if (TREE_PUBLIC (result))
20380 maybe_make_one_only (result);
20381 }
20382
20383 /* If EXTERN_P, then this function will not be emitted -- unless
20384 followed by an explicit instantiation, at which point its linkage
20385 will be adjusted. If !EXTERN_P, then this function will be
20386 emitted here. In neither circumstance do we want
20387 import_export_decl to adjust the linkage. */
20388 DECL_INTERFACE_KNOWN (result) = 1;
20389 }
20390
20391 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20392 important template arguments. If any are missing, we check whether
20393 they're important by using error_mark_node for substituting into any
20394 args that were used for partial ordering (the ones between ARGS and END)
20395 and seeing if it bubbles up. */
20396
20397 static bool
20398 check_undeduced_parms (tree targs, tree args, tree end)
20399 {
20400 bool found = false;
20401 int i;
20402 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
20403 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
20404 {
20405 found = true;
20406 TREE_VEC_ELT (targs, i) = error_mark_node;
20407 }
20408 if (found)
20409 {
20410 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
20411 if (substed == error_mark_node)
20412 return true;
20413 }
20414 return false;
20415 }
20416
20417 /* Given two function templates PAT1 and PAT2, return:
20418
20419 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20420 -1 if PAT2 is more specialized than PAT1.
20421 0 if neither is more specialized.
20422
20423 LEN indicates the number of parameters we should consider
20424 (defaulted parameters should not be considered).
20425
20426 The 1998 std underspecified function template partial ordering, and
20427 DR214 addresses the issue. We take pairs of arguments, one from
20428 each of the templates, and deduce them against each other. One of
20429 the templates will be more specialized if all the *other*
20430 template's arguments deduce against its arguments and at least one
20431 of its arguments *does* *not* deduce against the other template's
20432 corresponding argument. Deduction is done as for class templates.
20433 The arguments used in deduction have reference and top level cv
20434 qualifiers removed. Iff both arguments were originally reference
20435 types *and* deduction succeeds in both directions, an lvalue reference
20436 wins against an rvalue reference and otherwise the template
20437 with the more cv-qualified argument wins for that pairing (if
20438 neither is more cv-qualified, they both are equal). Unlike regular
20439 deduction, after all the arguments have been deduced in this way,
20440 we do *not* verify the deduced template argument values can be
20441 substituted into non-deduced contexts.
20442
20443 The logic can be a bit confusing here, because we look at deduce1 and
20444 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20445 can find template arguments for pat1 to make arg1 look like arg2, that
20446 means that arg2 is at least as specialized as arg1. */
20447
20448 int
20449 more_specialized_fn (tree pat1, tree pat2, int len)
20450 {
20451 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
20452 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
20453 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
20454 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
20455 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
20456 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
20457 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
20458 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
20459 tree origs1, origs2;
20460 bool lose1 = false;
20461 bool lose2 = false;
20462
20463 /* Remove the this parameter from non-static member functions. If
20464 one is a non-static member function and the other is not a static
20465 member function, remove the first parameter from that function
20466 also. This situation occurs for operator functions where we
20467 locate both a member function (with this pointer) and non-member
20468 operator (with explicit first operand). */
20469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
20470 {
20471 len--; /* LEN is the number of significant arguments for DECL1 */
20472 args1 = TREE_CHAIN (args1);
20473 if (!DECL_STATIC_FUNCTION_P (decl2))
20474 args2 = TREE_CHAIN (args2);
20475 }
20476 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
20477 {
20478 args2 = TREE_CHAIN (args2);
20479 if (!DECL_STATIC_FUNCTION_P (decl1))
20480 {
20481 len--;
20482 args1 = TREE_CHAIN (args1);
20483 }
20484 }
20485
20486 /* If only one is a conversion operator, they are unordered. */
20487 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
20488 return 0;
20489
20490 /* Consider the return type for a conversion function */
20491 if (DECL_CONV_FN_P (decl1))
20492 {
20493 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
20494 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
20495 len++;
20496 }
20497
20498 processing_template_decl++;
20499
20500 origs1 = args1;
20501 origs2 = args2;
20502
20503 while (len--
20504 /* Stop when an ellipsis is seen. */
20505 && args1 != NULL_TREE && args2 != NULL_TREE)
20506 {
20507 tree arg1 = TREE_VALUE (args1);
20508 tree arg2 = TREE_VALUE (args2);
20509 int deduce1, deduce2;
20510 int quals1 = -1;
20511 int quals2 = -1;
20512 int ref1 = 0;
20513 int ref2 = 0;
20514
20515 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20516 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20517 {
20518 /* When both arguments are pack expansions, we need only
20519 unify the patterns themselves. */
20520 arg1 = PACK_EXPANSION_PATTERN (arg1);
20521 arg2 = PACK_EXPANSION_PATTERN (arg2);
20522
20523 /* This is the last comparison we need to do. */
20524 len = 0;
20525 }
20526
20527 if (TREE_CODE (arg1) == REFERENCE_TYPE)
20528 {
20529 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
20530 arg1 = TREE_TYPE (arg1);
20531 quals1 = cp_type_quals (arg1);
20532 }
20533
20534 if (TREE_CODE (arg2) == REFERENCE_TYPE)
20535 {
20536 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
20537 arg2 = TREE_TYPE (arg2);
20538 quals2 = cp_type_quals (arg2);
20539 }
20540
20541 arg1 = TYPE_MAIN_VARIANT (arg1);
20542 arg2 = TYPE_MAIN_VARIANT (arg2);
20543
20544 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
20545 {
20546 int i, len2 = remaining_arguments (args2);
20547 tree parmvec = make_tree_vec (1);
20548 tree argvec = make_tree_vec (len2);
20549 tree ta = args2;
20550
20551 /* Setup the parameter vector, which contains only ARG1. */
20552 TREE_VEC_ELT (parmvec, 0) = arg1;
20553
20554 /* Setup the argument vector, which contains the remaining
20555 arguments. */
20556 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
20557 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20558
20559 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
20560 argvec, DEDUCE_EXACT,
20561 /*subr=*/true, /*explain_p=*/false)
20562 == 0);
20563
20564 /* We cannot deduce in the other direction, because ARG1 is
20565 a pack expansion but ARG2 is not. */
20566 deduce2 = 0;
20567 }
20568 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20569 {
20570 int i, len1 = remaining_arguments (args1);
20571 tree parmvec = make_tree_vec (1);
20572 tree argvec = make_tree_vec (len1);
20573 tree ta = args1;
20574
20575 /* Setup the parameter vector, which contains only ARG1. */
20576 TREE_VEC_ELT (parmvec, 0) = arg2;
20577
20578 /* Setup the argument vector, which contains the remaining
20579 arguments. */
20580 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
20581 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20582
20583 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
20584 argvec, DEDUCE_EXACT,
20585 /*subr=*/true, /*explain_p=*/false)
20586 == 0);
20587
20588 /* We cannot deduce in the other direction, because ARG2 is
20589 a pack expansion but ARG1 is not.*/
20590 deduce1 = 0;
20591 }
20592
20593 else
20594 {
20595 /* The normal case, where neither argument is a pack
20596 expansion. */
20597 deduce1 = (unify (tparms1, targs1, arg1, arg2,
20598 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20599 == 0);
20600 deduce2 = (unify (tparms2, targs2, arg2, arg1,
20601 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20602 == 0);
20603 }
20604
20605 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20606 arg2, then arg2 is not as specialized as arg1. */
20607 if (!deduce1)
20608 lose2 = true;
20609 if (!deduce2)
20610 lose1 = true;
20611
20612 /* "If, for a given type, deduction succeeds in both directions
20613 (i.e., the types are identical after the transformations above)
20614 and both P and A were reference types (before being replaced with
20615 the type referred to above):
20616 - if the type from the argument template was an lvalue reference and
20617 the type from the parameter template was not, the argument type is
20618 considered to be more specialized than the other; otherwise,
20619 - if the type from the argument template is more cv-qualified
20620 than the type from the parameter template (as described above),
20621 the argument type is considered to be more specialized than the other;
20622 otherwise,
20623 - neither type is more specialized than the other." */
20624
20625 if (deduce1 && deduce2)
20626 {
20627 if (ref1 && ref2 && ref1 != ref2)
20628 {
20629 if (ref1 > ref2)
20630 lose1 = true;
20631 else
20632 lose2 = true;
20633 }
20634 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
20635 {
20636 if ((quals1 & quals2) == quals2)
20637 lose2 = true;
20638 if ((quals1 & quals2) == quals1)
20639 lose1 = true;
20640 }
20641 }
20642
20643 if (lose1 && lose2)
20644 /* We've failed to deduce something in either direction.
20645 These must be unordered. */
20646 break;
20647
20648 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20649 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20650 /* We have already processed all of the arguments in our
20651 handing of the pack expansion type. */
20652 len = 0;
20653
20654 args1 = TREE_CHAIN (args1);
20655 args2 = TREE_CHAIN (args2);
20656 }
20657
20658 /* "In most cases, all template parameters must have values in order for
20659 deduction to succeed, but for partial ordering purposes a template
20660 parameter may remain without a value provided it is not used in the
20661 types being used for partial ordering."
20662
20663 Thus, if we are missing any of the targs1 we need to substitute into
20664 origs1, then pat2 is not as specialized as pat1. This can happen when
20665 there is a nondeduced context. */
20666 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
20667 lose2 = true;
20668 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
20669 lose1 = true;
20670
20671 processing_template_decl--;
20672
20673 /* If both deductions succeed, the partial ordering selects the more
20674 constrained template. */
20675 if (!lose1 && !lose2)
20676 {
20677 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
20678 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
20679 lose1 = !subsumes_constraints (c1, c2);
20680 lose2 = !subsumes_constraints (c2, c1);
20681 }
20682
20683 /* All things being equal, if the next argument is a pack expansion
20684 for one function but not for the other, prefer the
20685 non-variadic function. FIXME this is bogus; see c++/41958. */
20686 if (lose1 == lose2
20687 && args1 && TREE_VALUE (args1)
20688 && args2 && TREE_VALUE (args2))
20689 {
20690 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
20691 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
20692 }
20693
20694 if (lose1 == lose2)
20695 return 0;
20696 else if (!lose1)
20697 return 1;
20698 else
20699 return -1;
20700 }
20701
20702 /* Determine which of two partial specializations of TMPL is more
20703 specialized.
20704
20705 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20706 to the first partial specialization. The TREE_PURPOSE is the
20707 innermost set of template parameters for the partial
20708 specialization. PAT2 is similar, but for the second template.
20709
20710 Return 1 if the first partial specialization is more specialized;
20711 -1 if the second is more specialized; 0 if neither is more
20712 specialized.
20713
20714 See [temp.class.order] for information about determining which of
20715 two templates is more specialized. */
20716
20717 static int
20718 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
20719 {
20720 tree targs;
20721 int winner = 0;
20722 bool any_deductions = false;
20723
20724 tree tmpl1 = TREE_VALUE (pat1);
20725 tree tmpl2 = TREE_VALUE (pat2);
20726 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
20727 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
20728
20729 /* Just like what happens for functions, if we are ordering between
20730 different template specializations, we may encounter dependent
20731 types in the arguments, and we need our dependency check functions
20732 to behave correctly. */
20733 ++processing_template_decl;
20734 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
20735 if (targs)
20736 {
20737 --winner;
20738 any_deductions = true;
20739 }
20740
20741 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
20742 if (targs)
20743 {
20744 ++winner;
20745 any_deductions = true;
20746 }
20747 --processing_template_decl;
20748
20749 /* If both deductions succeed, the partial ordering selects the more
20750 constrained template. */
20751 if (!winner && any_deductions)
20752 return more_constrained (tmpl1, tmpl2);
20753
20754 /* In the case of a tie where at least one of the templates
20755 has a parameter pack at the end, the template with the most
20756 non-packed parameters wins. */
20757 if (winner == 0
20758 && any_deductions
20759 && (template_args_variadic_p (TREE_PURPOSE (pat1))
20760 || template_args_variadic_p (TREE_PURPOSE (pat2))))
20761 {
20762 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
20763 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
20764 int len1 = TREE_VEC_LENGTH (args1);
20765 int len2 = TREE_VEC_LENGTH (args2);
20766
20767 /* We don't count the pack expansion at the end. */
20768 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
20769 --len1;
20770 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
20771 --len2;
20772
20773 if (len1 > len2)
20774 return 1;
20775 else if (len1 < len2)
20776 return -1;
20777 }
20778
20779 return winner;
20780 }
20781
20782 /* Return the template arguments that will produce the function signature
20783 DECL from the function template FN, with the explicit template
20784 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20785 also match. Return NULL_TREE if no satisfactory arguments could be
20786 found. */
20787
20788 static tree
20789 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
20790 {
20791 int ntparms = DECL_NTPARMS (fn);
20792 tree targs = make_tree_vec (ntparms);
20793 tree decl_type = TREE_TYPE (decl);
20794 tree decl_arg_types;
20795 tree *args;
20796 unsigned int nargs, ix;
20797 tree arg;
20798
20799 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
20800
20801 /* Never do unification on the 'this' parameter. */
20802 decl_arg_types = skip_artificial_parms_for (decl,
20803 TYPE_ARG_TYPES (decl_type));
20804
20805 nargs = list_length (decl_arg_types);
20806 args = XALLOCAVEC (tree, nargs);
20807 for (arg = decl_arg_types, ix = 0;
20808 arg != NULL_TREE && arg != void_list_node;
20809 arg = TREE_CHAIN (arg), ++ix)
20810 args[ix] = TREE_VALUE (arg);
20811
20812 if (fn_type_unification (fn, explicit_args, targs,
20813 args, ix,
20814 (check_rettype || DECL_CONV_FN_P (fn)
20815 ? TREE_TYPE (decl_type) : NULL_TREE),
20816 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
20817 /*decltype*/false)
20818 == error_mark_node)
20819 return NULL_TREE;
20820
20821 return targs;
20822 }
20823
20824 /* Return the innermost template arguments that, when applied to a partial
20825 specialization SPEC_TMPL of TMPL, yield the ARGS.
20826
20827 For example, suppose we have:
20828
20829 template <class T, class U> struct S {};
20830 template <class T> struct S<T*, int> {};
20831
20832 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
20833 partial specialization and the ARGS will be {double*, int}. The resulting
20834 vector will be {double}, indicating that `T' is bound to `double'. */
20835
20836 static tree
20837 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
20838 {
20839 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
20840 tree spec_args
20841 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
20842 int i, ntparms = TREE_VEC_LENGTH (tparms);
20843 tree deduced_args;
20844 tree innermost_deduced_args;
20845
20846 innermost_deduced_args = make_tree_vec (ntparms);
20847 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20848 {
20849 deduced_args = copy_node (args);
20850 SET_TMPL_ARGS_LEVEL (deduced_args,
20851 TMPL_ARGS_DEPTH (deduced_args),
20852 innermost_deduced_args);
20853 }
20854 else
20855 deduced_args = innermost_deduced_args;
20856
20857 if (unify (tparms, deduced_args,
20858 INNERMOST_TEMPLATE_ARGS (spec_args),
20859 INNERMOST_TEMPLATE_ARGS (args),
20860 UNIFY_ALLOW_NONE, /*explain_p=*/false))
20861 return NULL_TREE;
20862
20863 for (i = 0; i < ntparms; ++i)
20864 if (! TREE_VEC_ELT (innermost_deduced_args, i))
20865 return NULL_TREE;
20866
20867 tree tinst = build_tree_list (spec_tmpl, deduced_args);
20868 if (!push_tinst_level (tinst))
20869 {
20870 excessive_deduction_depth = true;
20871 return NULL_TREE;
20872 }
20873
20874 /* Verify that nondeduced template arguments agree with the type
20875 obtained from argument deduction.
20876
20877 For example:
20878
20879 struct A { typedef int X; };
20880 template <class T, class U> struct C {};
20881 template <class T> struct C<T, typename T::X> {};
20882
20883 Then with the instantiation `C<A, int>', we can deduce that
20884 `T' is `A' but unify () does not check whether `typename T::X'
20885 is `int'. */
20886 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
20887 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20888 spec_args, tmpl,
20889 tf_none, false, false);
20890
20891 pop_tinst_level ();
20892
20893 if (spec_args == error_mark_node
20894 /* We only need to check the innermost arguments; the other
20895 arguments will always agree. */
20896 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
20897 INNERMOST_TEMPLATE_ARGS (args)))
20898 return NULL_TREE;
20899
20900 /* Now that we have bindings for all of the template arguments,
20901 ensure that the arguments deduced for the template template
20902 parameters have compatible template parameter lists. See the use
20903 of template_template_parm_bindings_ok_p in fn_type_unification
20904 for more information. */
20905 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
20906 return NULL_TREE;
20907
20908 return deduced_args;
20909 }
20910
20911 // Compare two function templates T1 and T2 by deducing bindings
20912 // from one against the other. If both deductions succeed, compare
20913 // constraints to see which is more constrained.
20914 static int
20915 more_specialized_inst (tree t1, tree t2)
20916 {
20917 int fate = 0;
20918 int count = 0;
20919
20920 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
20921 {
20922 --fate;
20923 ++count;
20924 }
20925
20926 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
20927 {
20928 ++fate;
20929 ++count;
20930 }
20931
20932 // If both deductions succeed, then one may be more constrained.
20933 if (count == 2 && fate == 0)
20934 fate = more_constrained (t1, t2);
20935
20936 return fate;
20937 }
20938
20939 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20940 Return the TREE_LIST node with the most specialized template, if
20941 any. If there is no most specialized template, the error_mark_node
20942 is returned.
20943
20944 Note that this function does not look at, or modify, the
20945 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20946 returned is one of the elements of INSTANTIATIONS, callers may
20947 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20948 and retrieve it from the value returned. */
20949
20950 tree
20951 most_specialized_instantiation (tree templates)
20952 {
20953 tree fn, champ;
20954
20955 ++processing_template_decl;
20956
20957 champ = templates;
20958 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
20959 {
20960 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
20961 if (fate == -1)
20962 champ = fn;
20963 else if (!fate)
20964 {
20965 /* Equally specialized, move to next function. If there
20966 is no next function, nothing's most specialized. */
20967 fn = TREE_CHAIN (fn);
20968 champ = fn;
20969 if (!fn)
20970 break;
20971 }
20972 }
20973
20974 if (champ)
20975 /* Now verify that champ is better than everything earlier in the
20976 instantiation list. */
20977 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
20978 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
20979 {
20980 champ = NULL_TREE;
20981 break;
20982 }
20983 }
20984
20985 processing_template_decl--;
20986
20987 if (!champ)
20988 return error_mark_node;
20989
20990 return champ;
20991 }
20992
20993 /* If DECL is a specialization of some template, return the most
20994 general such template. Otherwise, returns NULL_TREE.
20995
20996 For example, given:
20997
20998 template <class T> struct S { template <class U> void f(U); };
20999
21000 if TMPL is `template <class U> void S<int>::f(U)' this will return
21001 the full template. This function will not trace past partial
21002 specializations, however. For example, given in addition:
21003
21004 template <class T> struct S<T*> { template <class U> void f(U); };
21005
21006 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21007 `template <class T> template <class U> S<T*>::f(U)'. */
21008
21009 tree
21010 most_general_template (tree decl)
21011 {
21012 if (TREE_CODE (decl) != TEMPLATE_DECL)
21013 {
21014 if (tree tinfo = get_template_info (decl))
21015 decl = TI_TEMPLATE (tinfo);
21016 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21017 template friend, or a FIELD_DECL for a capture pack. */
21018 if (TREE_CODE (decl) != TEMPLATE_DECL)
21019 return NULL_TREE;
21020 }
21021
21022 /* Look for more and more general templates. */
21023 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21024 {
21025 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21026 (See cp-tree.h for details.) */
21027 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21028 break;
21029
21030 if (CLASS_TYPE_P (TREE_TYPE (decl))
21031 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21032 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21033 break;
21034
21035 /* Stop if we run into an explicitly specialized class template. */
21036 if (!DECL_NAMESPACE_SCOPE_P (decl)
21037 && DECL_CONTEXT (decl)
21038 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21039 break;
21040
21041 decl = DECL_TI_TEMPLATE (decl);
21042 }
21043
21044 return decl;
21045 }
21046
21047 /* Return the most specialized of the template partial specializations
21048 which can produce TARGET, a specialization of some class or variable
21049 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21050 a TEMPLATE_DECL node corresponding to the partial specialization, while
21051 the TREE_PURPOSE is the set of template arguments that must be
21052 substituted into the template pattern in order to generate TARGET.
21053
21054 If the choice of partial specialization is ambiguous, a diagnostic
21055 is issued, and the error_mark_node is returned. If there are no
21056 partial specializations matching TARGET, then NULL_TREE is
21057 returned, indicating that the primary template should be used. */
21058
21059 static tree
21060 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21061 {
21062 tree list = NULL_TREE;
21063 tree t;
21064 tree champ;
21065 int fate;
21066 bool ambiguous_p;
21067 tree outer_args = NULL_TREE;
21068 tree tmpl, args;
21069
21070 if (TYPE_P (target))
21071 {
21072 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21073 tmpl = TI_TEMPLATE (tinfo);
21074 args = TI_ARGS (tinfo);
21075 }
21076 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21077 {
21078 tmpl = TREE_OPERAND (target, 0);
21079 args = TREE_OPERAND (target, 1);
21080 }
21081 else if (VAR_P (target))
21082 {
21083 tree tinfo = DECL_TEMPLATE_INFO (target);
21084 tmpl = TI_TEMPLATE (tinfo);
21085 args = TI_ARGS (tinfo);
21086 }
21087 else
21088 gcc_unreachable ();
21089
21090 tree main_tmpl = most_general_template (tmpl);
21091
21092 /* For determining which partial specialization to use, only the
21093 innermost args are interesting. */
21094 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21095 {
21096 outer_args = strip_innermost_template_args (args, 1);
21097 args = INNERMOST_TEMPLATE_ARGS (args);
21098 }
21099
21100 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21101 {
21102 tree spec_args;
21103 tree spec_tmpl = TREE_VALUE (t);
21104
21105 if (outer_args)
21106 {
21107 /* Substitute in the template args from the enclosing class. */
21108 ++processing_template_decl;
21109 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21110 --processing_template_decl;
21111 }
21112
21113 if (spec_tmpl == error_mark_node)
21114 return error_mark_node;
21115
21116 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21117 if (spec_args)
21118 {
21119 if (outer_args)
21120 spec_args = add_to_template_args (outer_args, spec_args);
21121
21122 /* Keep the candidate only if the constraints are satisfied,
21123 or if we're not compiling with concepts. */
21124 if (!flag_concepts
21125 || constraints_satisfied_p (spec_tmpl, spec_args))
21126 {
21127 list = tree_cons (spec_args, TREE_VALUE (t), list);
21128 TREE_TYPE (list) = TREE_TYPE (t);
21129 }
21130 }
21131 }
21132
21133 if (! list)
21134 return NULL_TREE;
21135
21136 ambiguous_p = false;
21137 t = list;
21138 champ = t;
21139 t = TREE_CHAIN (t);
21140 for (; t; t = TREE_CHAIN (t))
21141 {
21142 fate = more_specialized_partial_spec (tmpl, champ, t);
21143 if (fate == 1)
21144 ;
21145 else
21146 {
21147 if (fate == 0)
21148 {
21149 t = TREE_CHAIN (t);
21150 if (! t)
21151 {
21152 ambiguous_p = true;
21153 break;
21154 }
21155 }
21156 champ = t;
21157 }
21158 }
21159
21160 if (!ambiguous_p)
21161 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21162 {
21163 fate = more_specialized_partial_spec (tmpl, champ, t);
21164 if (fate != 1)
21165 {
21166 ambiguous_p = true;
21167 break;
21168 }
21169 }
21170
21171 if (ambiguous_p)
21172 {
21173 const char *str;
21174 char *spaces = NULL;
21175 if (!(complain & tf_error))
21176 return error_mark_node;
21177 if (TYPE_P (target))
21178 error ("ambiguous template instantiation for %q#T", target);
21179 else
21180 error ("ambiguous template instantiation for %q#D", target);
21181 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21182 for (t = list; t; t = TREE_CHAIN (t))
21183 {
21184 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21185 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21186 "%s %#S", spaces ? spaces : str, subst);
21187 spaces = spaces ? spaces : get_spaces (str);
21188 }
21189 free (spaces);
21190 return error_mark_node;
21191 }
21192
21193 return champ;
21194 }
21195
21196 /* Explicitly instantiate DECL. */
21197
21198 void
21199 do_decl_instantiation (tree decl, tree storage)
21200 {
21201 tree result = NULL_TREE;
21202 int extern_p = 0;
21203
21204 if (!decl || decl == error_mark_node)
21205 /* An error occurred, for which grokdeclarator has already issued
21206 an appropriate message. */
21207 return;
21208 else if (! DECL_LANG_SPECIFIC (decl))
21209 {
21210 error ("explicit instantiation of non-template %q#D", decl);
21211 return;
21212 }
21213
21214 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21215 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21216
21217 if (VAR_P (decl) && !var_templ)
21218 {
21219 /* There is an asymmetry here in the way VAR_DECLs and
21220 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21221 the latter, the DECL we get back will be marked as a
21222 template instantiation, and the appropriate
21223 DECL_TEMPLATE_INFO will be set up. This does not happen for
21224 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21225 should handle VAR_DECLs as it currently handles
21226 FUNCTION_DECLs. */
21227 if (!DECL_CLASS_SCOPE_P (decl))
21228 {
21229 error ("%qD is not a static data member of a class template", decl);
21230 return;
21231 }
21232 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21233 if (!result || !VAR_P (result))
21234 {
21235 error ("no matching template for %qD found", decl);
21236 return;
21237 }
21238 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21239 {
21240 error ("type %qT for explicit instantiation %qD does not match "
21241 "declared type %qT", TREE_TYPE (result), decl,
21242 TREE_TYPE (decl));
21243 return;
21244 }
21245 }
21246 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21247 {
21248 error ("explicit instantiation of %q#D", decl);
21249 return;
21250 }
21251 else
21252 result = decl;
21253
21254 /* Check for various error cases. Note that if the explicit
21255 instantiation is valid the RESULT will currently be marked as an
21256 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21257 until we get here. */
21258
21259 if (DECL_TEMPLATE_SPECIALIZATION (result))
21260 {
21261 /* DR 259 [temp.spec].
21262
21263 Both an explicit instantiation and a declaration of an explicit
21264 specialization shall not appear in a program unless the explicit
21265 instantiation follows a declaration of the explicit specialization.
21266
21267 For a given set of template parameters, if an explicit
21268 instantiation of a template appears after a declaration of an
21269 explicit specialization for that template, the explicit
21270 instantiation has no effect. */
21271 return;
21272 }
21273 else if (DECL_EXPLICIT_INSTANTIATION (result))
21274 {
21275 /* [temp.spec]
21276
21277 No program shall explicitly instantiate any template more
21278 than once.
21279
21280 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21281 the first instantiation was `extern' and the second is not,
21282 and EXTERN_P for the opposite case. */
21283 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21284 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21285 /* If an "extern" explicit instantiation follows an ordinary
21286 explicit instantiation, the template is instantiated. */
21287 if (extern_p)
21288 return;
21289 }
21290 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21291 {
21292 error ("no matching template for %qD found", result);
21293 return;
21294 }
21295 else if (!DECL_TEMPLATE_INFO (result))
21296 {
21297 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21298 return;
21299 }
21300
21301 if (storage == NULL_TREE)
21302 ;
21303 else if (storage == ridpointers[(int) RID_EXTERN])
21304 {
21305 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21306 pedwarn (input_location, OPT_Wpedantic,
21307 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21308 "instantiations");
21309 extern_p = 1;
21310 }
21311 else
21312 error ("storage class %qD applied to template instantiation", storage);
21313
21314 check_explicit_instantiation_namespace (result);
21315 mark_decl_instantiated (result, extern_p);
21316 if (! extern_p)
21317 instantiate_decl (result, /*defer_ok=*/1,
21318 /*expl_inst_class_mem_p=*/false);
21319 }
21320
21321 static void
21322 mark_class_instantiated (tree t, int extern_p)
21323 {
21324 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21325 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21326 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21327 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21328 if (! extern_p)
21329 {
21330 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21331 rest_of_type_compilation (t, 1);
21332 }
21333 }
21334
21335 /* Called from do_type_instantiation through binding_table_foreach to
21336 do recursive instantiation for the type bound in ENTRY. */
21337 static void
21338 bt_instantiate_type_proc (binding_entry entry, void *data)
21339 {
21340 tree storage = *(tree *) data;
21341
21342 if (MAYBE_CLASS_TYPE_P (entry->type)
21343 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
21344 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
21345 }
21346
21347 /* Called from do_type_instantiation to instantiate a member
21348 (a member function or a static member variable) of an
21349 explicitly instantiated class template. */
21350 static void
21351 instantiate_class_member (tree decl, int extern_p)
21352 {
21353 mark_decl_instantiated (decl, extern_p);
21354 if (! extern_p)
21355 instantiate_decl (decl, /*defer_ok=*/1,
21356 /*expl_inst_class_mem_p=*/true);
21357 }
21358
21359 /* Perform an explicit instantiation of template class T. STORAGE, if
21360 non-null, is the RID for extern, inline or static. COMPLAIN is
21361 nonzero if this is called from the parser, zero if called recursively,
21362 since the standard is unclear (as detailed below). */
21363
21364 void
21365 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
21366 {
21367 int extern_p = 0;
21368 int nomem_p = 0;
21369 int static_p = 0;
21370 int previous_instantiation_extern_p = 0;
21371
21372 if (TREE_CODE (t) == TYPE_DECL)
21373 t = TREE_TYPE (t);
21374
21375 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
21376 {
21377 tree tmpl =
21378 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
21379 if (tmpl)
21380 error ("explicit instantiation of non-class template %qD", tmpl);
21381 else
21382 error ("explicit instantiation of non-template type %qT", t);
21383 return;
21384 }
21385
21386 complete_type (t);
21387
21388 if (!COMPLETE_TYPE_P (t))
21389 {
21390 if (complain & tf_error)
21391 error ("explicit instantiation of %q#T before definition of template",
21392 t);
21393 return;
21394 }
21395
21396 if (storage != NULL_TREE)
21397 {
21398 if (!in_system_header_at (input_location))
21399 {
21400 if (storage == ridpointers[(int) RID_EXTERN])
21401 {
21402 if (cxx_dialect == cxx98)
21403 pedwarn (input_location, OPT_Wpedantic,
21404 "ISO C++ 1998 forbids the use of %<extern%> on "
21405 "explicit instantiations");
21406 }
21407 else
21408 pedwarn (input_location, OPT_Wpedantic,
21409 "ISO C++ forbids the use of %qE"
21410 " on explicit instantiations", storage);
21411 }
21412
21413 if (storage == ridpointers[(int) RID_INLINE])
21414 nomem_p = 1;
21415 else if (storage == ridpointers[(int) RID_EXTERN])
21416 extern_p = 1;
21417 else if (storage == ridpointers[(int) RID_STATIC])
21418 static_p = 1;
21419 else
21420 {
21421 error ("storage class %qD applied to template instantiation",
21422 storage);
21423 extern_p = 0;
21424 }
21425 }
21426
21427 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
21428 {
21429 /* DR 259 [temp.spec].
21430
21431 Both an explicit instantiation and a declaration of an explicit
21432 specialization shall not appear in a program unless the explicit
21433 instantiation follows a declaration of the explicit specialization.
21434
21435 For a given set of template parameters, if an explicit
21436 instantiation of a template appears after a declaration of an
21437 explicit specialization for that template, the explicit
21438 instantiation has no effect. */
21439 return;
21440 }
21441 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
21442 {
21443 /* [temp.spec]
21444
21445 No program shall explicitly instantiate any template more
21446 than once.
21447
21448 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21449 instantiation was `extern'. If EXTERN_P then the second is.
21450 These cases are OK. */
21451 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
21452
21453 if (!previous_instantiation_extern_p && !extern_p
21454 && (complain & tf_error))
21455 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
21456
21457 /* If we've already instantiated the template, just return now. */
21458 if (!CLASSTYPE_INTERFACE_ONLY (t))
21459 return;
21460 }
21461
21462 check_explicit_instantiation_namespace (TYPE_NAME (t));
21463 mark_class_instantiated (t, extern_p);
21464
21465 if (nomem_p)
21466 return;
21467
21468 {
21469 tree tmp;
21470
21471 /* In contrast to implicit instantiation, where only the
21472 declarations, and not the definitions, of members are
21473 instantiated, we have here:
21474
21475 [temp.explicit]
21476
21477 The explicit instantiation of a class template specialization
21478 implies the instantiation of all of its members not
21479 previously explicitly specialized in the translation unit
21480 containing the explicit instantiation.
21481
21482 Of course, we can't instantiate member template classes, since
21483 we don't have any arguments for them. Note that the standard
21484 is unclear on whether the instantiation of the members are
21485 *explicit* instantiations or not. However, the most natural
21486 interpretation is that it should be an explicit instantiation. */
21487
21488 if (! static_p)
21489 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
21490 if (TREE_CODE (tmp) == FUNCTION_DECL
21491 && DECL_TEMPLATE_INSTANTIATION (tmp)
21492 && user_provided_p (tmp))
21493 instantiate_class_member (tmp, extern_p);
21494
21495 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
21496 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
21497 instantiate_class_member (tmp, extern_p);
21498
21499 if (CLASSTYPE_NESTED_UTDS (t))
21500 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
21501 bt_instantiate_type_proc, &storage);
21502 }
21503 }
21504
21505 /* Given a function DECL, which is a specialization of TMPL, modify
21506 DECL to be a re-instantiation of TMPL with the same template
21507 arguments. TMPL should be the template into which tsubst'ing
21508 should occur for DECL, not the most general template.
21509
21510 One reason for doing this is a scenario like this:
21511
21512 template <class T>
21513 void f(const T&, int i);
21514
21515 void g() { f(3, 7); }
21516
21517 template <class T>
21518 void f(const T& t, const int i) { }
21519
21520 Note that when the template is first instantiated, with
21521 instantiate_template, the resulting DECL will have no name for the
21522 first parameter, and the wrong type for the second. So, when we go
21523 to instantiate the DECL, we regenerate it. */
21524
21525 static void
21526 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
21527 {
21528 /* The arguments used to instantiate DECL, from the most general
21529 template. */
21530 tree code_pattern;
21531
21532 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
21533
21534 /* Make sure that we can see identifiers, and compute access
21535 correctly. */
21536 push_access_scope (decl);
21537
21538 if (TREE_CODE (decl) == FUNCTION_DECL)
21539 {
21540 tree decl_parm;
21541 tree pattern_parm;
21542 tree specs;
21543 int args_depth;
21544 int parms_depth;
21545
21546 args_depth = TMPL_ARGS_DEPTH (args);
21547 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
21548 if (args_depth > parms_depth)
21549 args = get_innermost_template_args (args, parms_depth);
21550
21551 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
21552 args, tf_error, NULL_TREE,
21553 /*defer_ok*/false);
21554 if (specs && specs != error_mark_node)
21555 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
21556 specs);
21557
21558 /* Merge parameter declarations. */
21559 decl_parm = skip_artificial_parms_for (decl,
21560 DECL_ARGUMENTS (decl));
21561 pattern_parm
21562 = skip_artificial_parms_for (code_pattern,
21563 DECL_ARGUMENTS (code_pattern));
21564 while (decl_parm && !DECL_PACK_P (pattern_parm))
21565 {
21566 tree parm_type;
21567 tree attributes;
21568
21569 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21570 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
21571 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
21572 NULL_TREE);
21573 parm_type = type_decays_to (parm_type);
21574 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21575 TREE_TYPE (decl_parm) = parm_type;
21576 attributes = DECL_ATTRIBUTES (pattern_parm);
21577 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21578 {
21579 DECL_ATTRIBUTES (decl_parm) = attributes;
21580 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21581 }
21582 decl_parm = DECL_CHAIN (decl_parm);
21583 pattern_parm = DECL_CHAIN (pattern_parm);
21584 }
21585 /* Merge any parameters that match with the function parameter
21586 pack. */
21587 if (pattern_parm && DECL_PACK_P (pattern_parm))
21588 {
21589 int i, len;
21590 tree expanded_types;
21591 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21592 the parameters in this function parameter pack. */
21593 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
21594 args, tf_error, NULL_TREE);
21595 len = TREE_VEC_LENGTH (expanded_types);
21596 for (i = 0; i < len; i++)
21597 {
21598 tree parm_type;
21599 tree attributes;
21600
21601 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21602 /* Rename the parameter to include the index. */
21603 DECL_NAME (decl_parm) =
21604 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
21605 parm_type = TREE_VEC_ELT (expanded_types, i);
21606 parm_type = type_decays_to (parm_type);
21607 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21608 TREE_TYPE (decl_parm) = parm_type;
21609 attributes = DECL_ATTRIBUTES (pattern_parm);
21610 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21611 {
21612 DECL_ATTRIBUTES (decl_parm) = attributes;
21613 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21614 }
21615 decl_parm = DECL_CHAIN (decl_parm);
21616 }
21617 }
21618 /* Merge additional specifiers from the CODE_PATTERN. */
21619 if (DECL_DECLARED_INLINE_P (code_pattern)
21620 && !DECL_DECLARED_INLINE_P (decl))
21621 DECL_DECLARED_INLINE_P (decl) = 1;
21622 }
21623 else if (VAR_P (decl))
21624 {
21625 DECL_INITIAL (decl) =
21626 tsubst_expr (DECL_INITIAL (code_pattern), args,
21627 tf_error, DECL_TI_TEMPLATE (decl),
21628 /*integral_constant_expression_p=*/false);
21629 if (VAR_HAD_UNKNOWN_BOUND (decl))
21630 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
21631 tf_error, DECL_TI_TEMPLATE (decl));
21632 }
21633 else
21634 gcc_unreachable ();
21635
21636 pop_access_scope (decl);
21637 }
21638
21639 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21640 substituted to get DECL. */
21641
21642 tree
21643 template_for_substitution (tree decl)
21644 {
21645 tree tmpl = DECL_TI_TEMPLATE (decl);
21646
21647 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21648 for the instantiation. This is not always the most general
21649 template. Consider, for example:
21650
21651 template <class T>
21652 struct S { template <class U> void f();
21653 template <> void f<int>(); };
21654
21655 and an instantiation of S<double>::f<int>. We want TD to be the
21656 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21657 while (/* An instantiation cannot have a definition, so we need a
21658 more general template. */
21659 DECL_TEMPLATE_INSTANTIATION (tmpl)
21660 /* We must also deal with friend templates. Given:
21661
21662 template <class T> struct S {
21663 template <class U> friend void f() {};
21664 };
21665
21666 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21667 so far as the language is concerned, but that's still
21668 where we get the pattern for the instantiation from. On
21669 other hand, if the definition comes outside the class, say:
21670
21671 template <class T> struct S {
21672 template <class U> friend void f();
21673 };
21674 template <class U> friend void f() {}
21675
21676 we don't need to look any further. That's what the check for
21677 DECL_INITIAL is for. */
21678 || (TREE_CODE (decl) == FUNCTION_DECL
21679 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
21680 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
21681 {
21682 /* The present template, TD, should not be a definition. If it
21683 were a definition, we should be using it! Note that we
21684 cannot restructure the loop to just keep going until we find
21685 a template with a definition, since that might go too far if
21686 a specialization was declared, but not defined. */
21687
21688 /* Fetch the more general template. */
21689 tmpl = DECL_TI_TEMPLATE (tmpl);
21690 }
21691
21692 return tmpl;
21693 }
21694
21695 /* Returns true if we need to instantiate this template instance even if we
21696 know we aren't going to emit it. */
21697
21698 bool
21699 always_instantiate_p (tree decl)
21700 {
21701 /* We always instantiate inline functions so that we can inline them. An
21702 explicit instantiation declaration prohibits implicit instantiation of
21703 non-inline functions. With high levels of optimization, we would
21704 normally inline non-inline functions -- but we're not allowed to do
21705 that for "extern template" functions. Therefore, we check
21706 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21707 return ((TREE_CODE (decl) == FUNCTION_DECL
21708 && (DECL_DECLARED_INLINE_P (decl)
21709 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
21710 /* And we need to instantiate static data members so that
21711 their initializers are available in integral constant
21712 expressions. */
21713 || (VAR_P (decl)
21714 && decl_maybe_constant_var_p (decl)));
21715 }
21716
21717 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21718 instantiate it now, modifying TREE_TYPE (fn). */
21719
21720 void
21721 maybe_instantiate_noexcept (tree fn)
21722 {
21723 tree fntype, spec, noex, clone;
21724
21725 /* Don't instantiate a noexcept-specification from template context. */
21726 if (processing_template_decl)
21727 return;
21728
21729 if (DECL_CLONED_FUNCTION_P (fn))
21730 fn = DECL_CLONED_FUNCTION (fn);
21731 fntype = TREE_TYPE (fn);
21732 spec = TYPE_RAISES_EXCEPTIONS (fntype);
21733
21734 if (!spec || !TREE_PURPOSE (spec))
21735 return;
21736
21737 noex = TREE_PURPOSE (spec);
21738
21739 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
21740 {
21741 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
21742 spec = get_defaulted_eh_spec (fn);
21743 else if (push_tinst_level (fn))
21744 {
21745 push_access_scope (fn);
21746 push_deferring_access_checks (dk_no_deferred);
21747 input_location = DECL_SOURCE_LOCATION (fn);
21748 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
21749 DEFERRED_NOEXCEPT_ARGS (noex),
21750 tf_warning_or_error, fn,
21751 /*function_p=*/false,
21752 /*integral_constant_expression_p=*/true);
21753 pop_deferring_access_checks ();
21754 pop_access_scope (fn);
21755 pop_tinst_level ();
21756 spec = build_noexcept_spec (noex, tf_warning_or_error);
21757 if (spec == error_mark_node)
21758 spec = noexcept_false_spec;
21759 }
21760 else
21761 spec = noexcept_false_spec;
21762
21763 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
21764 }
21765
21766 FOR_EACH_CLONE (clone, fn)
21767 {
21768 if (TREE_TYPE (clone) == fntype)
21769 TREE_TYPE (clone) = TREE_TYPE (fn);
21770 else
21771 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
21772 }
21773 }
21774
21775 /* Produce the definition of D, a _DECL generated from a template. If
21776 DEFER_OK is nonzero, then we don't have to actually do the
21777 instantiation now; we just have to do it sometime. Normally it is
21778 an error if this is an explicit instantiation but D is undefined.
21779 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21780 explicitly instantiated class template. */
21781
21782 tree
21783 instantiate_decl (tree d, int defer_ok,
21784 bool expl_inst_class_mem_p)
21785 {
21786 tree tmpl = DECL_TI_TEMPLATE (d);
21787 tree gen_args;
21788 tree args;
21789 tree td;
21790 tree code_pattern;
21791 tree spec;
21792 tree gen_tmpl;
21793 bool pattern_defined;
21794 location_t saved_loc = input_location;
21795 int saved_unevaluated_operand = cp_unevaluated_operand;
21796 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21797 bool external_p;
21798 bool deleted_p;
21799 tree fn_context;
21800 bool nested = false;
21801
21802 /* This function should only be used to instantiate templates for
21803 functions and static member variables. */
21804 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
21805
21806 /* A concept is never instantiated. */
21807 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
21808
21809 /* Variables are never deferred; if instantiation is required, they
21810 are instantiated right away. That allows for better code in the
21811 case that an expression refers to the value of the variable --
21812 if the variable has a constant value the referring expression can
21813 take advantage of that fact. */
21814 if (VAR_P (d)
21815 || DECL_DECLARED_CONSTEXPR_P (d))
21816 defer_ok = 0;
21817
21818 /* Don't instantiate cloned functions. Instead, instantiate the
21819 functions they cloned. */
21820 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
21821 d = DECL_CLONED_FUNCTION (d);
21822
21823 if (DECL_TEMPLATE_INSTANTIATED (d)
21824 || (TREE_CODE (d) == FUNCTION_DECL
21825 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
21826 || DECL_TEMPLATE_SPECIALIZATION (d))
21827 /* D has already been instantiated or explicitly specialized, so
21828 there's nothing for us to do here.
21829
21830 It might seem reasonable to check whether or not D is an explicit
21831 instantiation, and, if so, stop here. But when an explicit
21832 instantiation is deferred until the end of the compilation,
21833 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21834 the instantiation. */
21835 return d;
21836
21837 /* Check to see whether we know that this template will be
21838 instantiated in some other file, as with "extern template"
21839 extension. */
21840 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
21841
21842 /* In general, we do not instantiate such templates. */
21843 if (external_p && !always_instantiate_p (d))
21844 return d;
21845
21846 gen_tmpl = most_general_template (tmpl);
21847 gen_args = DECL_TI_ARGS (d);
21848
21849 if (tmpl != gen_tmpl)
21850 /* We should already have the extra args. */
21851 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
21852 == TMPL_ARGS_DEPTH (gen_args));
21853 /* And what's in the hash table should match D. */
21854 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
21855 || spec == NULL_TREE);
21856
21857 /* This needs to happen before any tsubsting. */
21858 if (! push_tinst_level (d))
21859 return d;
21860
21861 timevar_push (TV_TEMPLATE_INST);
21862
21863 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21864 for the instantiation. */
21865 td = template_for_substitution (d);
21866 args = gen_args;
21867
21868 if (VAR_P (d))
21869 {
21870 /* Look up an explicit specialization, if any. */
21871 tree tid = lookup_template_variable (gen_tmpl, gen_args);
21872 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
21873 if (elt && elt != error_mark_node)
21874 {
21875 td = TREE_VALUE (elt);
21876 args = TREE_PURPOSE (elt);
21877 }
21878 }
21879
21880 code_pattern = DECL_TEMPLATE_RESULT (td);
21881
21882 /* We should never be trying to instantiate a member of a class
21883 template or partial specialization. */
21884 gcc_assert (d != code_pattern);
21885
21886 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
21887 || DECL_TEMPLATE_SPECIALIZATION (td))
21888 /* In the case of a friend template whose definition is provided
21889 outside the class, we may have too many arguments. Drop the
21890 ones we don't need. The same is true for specializations. */
21891 args = get_innermost_template_args
21892 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
21893
21894 if (TREE_CODE (d) == FUNCTION_DECL)
21895 {
21896 deleted_p = DECL_DELETED_FN (code_pattern);
21897 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
21898 && DECL_INITIAL (code_pattern) != error_mark_node)
21899 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
21900 || deleted_p);
21901 }
21902 else
21903 {
21904 deleted_p = false;
21905 if (DECL_CLASS_SCOPE_P (code_pattern))
21906 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
21907 else
21908 pattern_defined = ! DECL_EXTERNAL (code_pattern);
21909 }
21910
21911 /* We may be in the middle of deferred access check. Disable it now. */
21912 push_deferring_access_checks (dk_no_deferred);
21913
21914 /* Unless an explicit instantiation directive has already determined
21915 the linkage of D, remember that a definition is available for
21916 this entity. */
21917 if (pattern_defined
21918 && !DECL_INTERFACE_KNOWN (d)
21919 && !DECL_NOT_REALLY_EXTERN (d))
21920 mark_definable (d);
21921
21922 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
21923 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
21924 input_location = DECL_SOURCE_LOCATION (d);
21925
21926 /* If D is a member of an explicitly instantiated class template,
21927 and no definition is available, treat it like an implicit
21928 instantiation. */
21929 if (!pattern_defined && expl_inst_class_mem_p
21930 && DECL_EXPLICIT_INSTANTIATION (d))
21931 {
21932 /* Leave linkage flags alone on instantiations with anonymous
21933 visibility. */
21934 if (TREE_PUBLIC (d))
21935 {
21936 DECL_NOT_REALLY_EXTERN (d) = 0;
21937 DECL_INTERFACE_KNOWN (d) = 0;
21938 }
21939 SET_DECL_IMPLICIT_INSTANTIATION (d);
21940 }
21941
21942 /* Defer all other templates, unless we have been explicitly
21943 forbidden from doing so. */
21944 if (/* If there is no definition, we cannot instantiate the
21945 template. */
21946 ! pattern_defined
21947 /* If it's OK to postpone instantiation, do so. */
21948 || defer_ok
21949 /* If this is a static data member that will be defined
21950 elsewhere, we don't want to instantiate the entire data
21951 member, but we do want to instantiate the initializer so that
21952 we can substitute that elsewhere. */
21953 || (external_p && VAR_P (d))
21954 /* Handle here a deleted function too, avoid generating
21955 its body (c++/61080). */
21956 || deleted_p)
21957 {
21958 /* The definition of the static data member is now required so
21959 we must substitute the initializer. */
21960 if (VAR_P (d)
21961 && !DECL_INITIAL (d)
21962 && DECL_INITIAL (code_pattern))
21963 {
21964 tree ns;
21965 tree init;
21966 bool const_init = false;
21967 bool enter_context = DECL_CLASS_SCOPE_P (d);
21968
21969 ns = decl_namespace_context (d);
21970 push_nested_namespace (ns);
21971 if (enter_context)
21972 push_nested_class (DECL_CONTEXT (d));
21973 init = tsubst_expr (DECL_INITIAL (code_pattern),
21974 args,
21975 tf_warning_or_error, NULL_TREE,
21976 /*integral_constant_expression_p=*/false);
21977 /* If instantiating the initializer involved instantiating this
21978 again, don't call cp_finish_decl twice. */
21979 if (!DECL_INITIAL (d))
21980 {
21981 /* Make sure the initializer is still constant, in case of
21982 circular dependency (template/instantiate6.C). */
21983 const_init
21984 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21985 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
21986 /*asmspec_tree=*/NULL_TREE,
21987 LOOKUP_ONLYCONVERTING);
21988 }
21989 if (enter_context)
21990 pop_nested_class ();
21991 pop_nested_namespace (ns);
21992 }
21993
21994 /* We restore the source position here because it's used by
21995 add_pending_template. */
21996 input_location = saved_loc;
21997
21998 if (at_eof && !pattern_defined
21999 && DECL_EXPLICIT_INSTANTIATION (d)
22000 && DECL_NOT_REALLY_EXTERN (d))
22001 /* [temp.explicit]
22002
22003 The definition of a non-exported function template, a
22004 non-exported member function template, or a non-exported
22005 member function or static data member of a class template
22006 shall be present in every translation unit in which it is
22007 explicitly instantiated. */
22008 permerror (input_location, "explicit instantiation of %qD "
22009 "but no definition available", d);
22010
22011 /* If we're in unevaluated context, we just wanted to get the
22012 constant value; this isn't an odr use, so don't queue
22013 a full instantiation. */
22014 if (cp_unevaluated_operand != 0)
22015 goto out;
22016 /* ??? Historically, we have instantiated inline functions, even
22017 when marked as "extern template". */
22018 if (!(external_p && VAR_P (d)))
22019 add_pending_template (d);
22020 goto out;
22021 }
22022 /* Tell the repository that D is available in this translation unit
22023 -- and see if it is supposed to be instantiated here. */
22024 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22025 {
22026 /* In a PCH file, despite the fact that the repository hasn't
22027 requested instantiation in the PCH it is still possible that
22028 an instantiation will be required in a file that includes the
22029 PCH. */
22030 if (pch_file)
22031 add_pending_template (d);
22032 /* Instantiate inline functions so that the inliner can do its
22033 job, even though we'll not be emitting a copy of this
22034 function. */
22035 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22036 goto out;
22037 }
22038
22039 fn_context = decl_function_context (d);
22040 nested = (current_function_decl != NULL_TREE);
22041 vec<tree> omp_privatization_save;
22042 if (nested)
22043 save_omp_privatization_clauses (omp_privatization_save);
22044
22045 if (!fn_context)
22046 push_to_top_level ();
22047 else
22048 {
22049 if (nested)
22050 push_function_context ();
22051 cp_unevaluated_operand = 0;
22052 c_inhibit_evaluation_warnings = 0;
22053 }
22054
22055 /* Mark D as instantiated so that recursive calls to
22056 instantiate_decl do not try to instantiate it again. */
22057 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22058
22059 /* Regenerate the declaration in case the template has been modified
22060 by a subsequent redeclaration. */
22061 regenerate_decl_from_template (d, td, args);
22062
22063 /* We already set the file and line above. Reset them now in case
22064 they changed as a result of calling regenerate_decl_from_template. */
22065 input_location = DECL_SOURCE_LOCATION (d);
22066
22067 if (VAR_P (d))
22068 {
22069 tree init;
22070 bool const_init = false;
22071
22072 /* Clear out DECL_RTL; whatever was there before may not be right
22073 since we've reset the type of the declaration. */
22074 SET_DECL_RTL (d, NULL);
22075 DECL_IN_AGGR_P (d) = 0;
22076
22077 /* The initializer is placed in DECL_INITIAL by
22078 regenerate_decl_from_template so we don't need to
22079 push/pop_access_scope again here. Pull it out so that
22080 cp_finish_decl can process it. */
22081 init = DECL_INITIAL (d);
22082 DECL_INITIAL (d) = NULL_TREE;
22083 DECL_INITIALIZED_P (d) = 0;
22084
22085 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22086 initializer. That function will defer actual emission until
22087 we have a chance to determine linkage. */
22088 DECL_EXTERNAL (d) = 0;
22089
22090 /* Enter the scope of D so that access-checking works correctly. */
22091 bool enter_context = DECL_CLASS_SCOPE_P (d);
22092 if (enter_context)
22093 push_nested_class (DECL_CONTEXT (d));
22094
22095 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22096 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22097
22098 if (enter_context)
22099 pop_nested_class ();
22100
22101 if (variable_template_p (gen_tmpl))
22102 note_variable_template_instantiation (d);
22103 }
22104 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22105 synthesize_method (d);
22106 else if (TREE_CODE (d) == FUNCTION_DECL)
22107 {
22108 hash_map<tree, tree> *saved_local_specializations;
22109 tree tmpl_parm;
22110 tree spec_parm;
22111 tree block = NULL_TREE;
22112
22113 /* Save away the current list, in case we are instantiating one
22114 template from within the body of another. */
22115 saved_local_specializations = local_specializations;
22116
22117 /* Set up the list of local specializations. */
22118 local_specializations = new hash_map<tree, tree>;
22119
22120 /* Set up context. */
22121 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22122 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22123 block = push_stmt_list ();
22124 else
22125 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22126
22127 /* Some typedefs referenced from within the template code need to be
22128 access checked at template instantiation time, i.e now. These
22129 types were added to the template at parsing time. Let's get those
22130 and perform the access checks then. */
22131 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22132 args);
22133
22134 /* Create substitution entries for the parameters. */
22135 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22136 spec_parm = DECL_ARGUMENTS (d);
22137 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22138 {
22139 register_local_specialization (spec_parm, tmpl_parm);
22140 spec_parm = skip_artificial_parms_for (d, spec_parm);
22141 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22142 }
22143 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22144 {
22145 if (!DECL_PACK_P (tmpl_parm))
22146 {
22147 register_local_specialization (spec_parm, tmpl_parm);
22148 spec_parm = DECL_CHAIN (spec_parm);
22149 }
22150 else
22151 {
22152 /* Register the (value) argument pack as a specialization of
22153 TMPL_PARM, then move on. */
22154 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22155 register_local_specialization (argpack, tmpl_parm);
22156 }
22157 }
22158 gcc_assert (!spec_parm);
22159
22160 /* Substitute into the body of the function. */
22161 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22162 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22163 tf_warning_or_error, tmpl);
22164 else
22165 {
22166 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22167 tf_warning_or_error, tmpl,
22168 /*integral_constant_expression_p=*/false);
22169
22170 /* Set the current input_location to the end of the function
22171 so that finish_function knows where we are. */
22172 input_location
22173 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22174
22175 /* Remember if we saw an infinite loop in the template. */
22176 current_function_infinite_loop
22177 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22178 }
22179
22180 /* We don't need the local specializations any more. */
22181 delete local_specializations;
22182 local_specializations = saved_local_specializations;
22183
22184 /* Finish the function. */
22185 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22186 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22187 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22188 else
22189 {
22190 d = finish_function (0);
22191 expand_or_defer_fn (d);
22192 }
22193
22194 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22195 cp_check_omp_declare_reduction (d);
22196 }
22197
22198 /* We're not deferring instantiation any more. */
22199 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22200
22201 if (!fn_context)
22202 pop_from_top_level ();
22203 else if (nested)
22204 pop_function_context ();
22205
22206 out:
22207 input_location = saved_loc;
22208 cp_unevaluated_operand = saved_unevaluated_operand;
22209 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22210 pop_deferring_access_checks ();
22211 pop_tinst_level ();
22212 if (nested)
22213 restore_omp_privatization_clauses (omp_privatization_save);
22214
22215 timevar_pop (TV_TEMPLATE_INST);
22216
22217 return d;
22218 }
22219
22220 /* Run through the list of templates that we wish we could
22221 instantiate, and instantiate any we can. RETRIES is the
22222 number of times we retry pending template instantiation. */
22223
22224 void
22225 instantiate_pending_templates (int retries)
22226 {
22227 int reconsider;
22228 location_t saved_loc = input_location;
22229
22230 /* Instantiating templates may trigger vtable generation. This in turn
22231 may require further template instantiations. We place a limit here
22232 to avoid infinite loop. */
22233 if (pending_templates && retries >= max_tinst_depth)
22234 {
22235 tree decl = pending_templates->tinst->decl;
22236
22237 fatal_error (input_location,
22238 "template instantiation depth exceeds maximum of %d"
22239 " instantiating %q+D, possibly from virtual table generation"
22240 " (use -ftemplate-depth= to increase the maximum)",
22241 max_tinst_depth, decl);
22242 if (TREE_CODE (decl) == FUNCTION_DECL)
22243 /* Pretend that we defined it. */
22244 DECL_INITIAL (decl) = error_mark_node;
22245 return;
22246 }
22247
22248 do
22249 {
22250 struct pending_template **t = &pending_templates;
22251 struct pending_template *last = NULL;
22252 reconsider = 0;
22253 while (*t)
22254 {
22255 tree instantiation = reopen_tinst_level ((*t)->tinst);
22256 bool complete = false;
22257
22258 if (TYPE_P (instantiation))
22259 {
22260 tree fn;
22261
22262 if (!COMPLETE_TYPE_P (instantiation))
22263 {
22264 instantiate_class_template (instantiation);
22265 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22266 for (fn = TYPE_METHODS (instantiation);
22267 fn;
22268 fn = TREE_CHAIN (fn))
22269 if (! DECL_ARTIFICIAL (fn))
22270 instantiate_decl (fn,
22271 /*defer_ok=*/0,
22272 /*expl_inst_class_mem_p=*/false);
22273 if (COMPLETE_TYPE_P (instantiation))
22274 reconsider = 1;
22275 }
22276
22277 complete = COMPLETE_TYPE_P (instantiation);
22278 }
22279 else
22280 {
22281 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22282 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22283 {
22284 instantiation
22285 = instantiate_decl (instantiation,
22286 /*defer_ok=*/0,
22287 /*expl_inst_class_mem_p=*/false);
22288 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22289 reconsider = 1;
22290 }
22291
22292 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22293 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22294 }
22295
22296 if (complete)
22297 /* If INSTANTIATION has been instantiated, then we don't
22298 need to consider it again in the future. */
22299 *t = (*t)->next;
22300 else
22301 {
22302 last = *t;
22303 t = &(*t)->next;
22304 }
22305 tinst_depth = 0;
22306 current_tinst_level = NULL;
22307 }
22308 last_pending_template = last;
22309 }
22310 while (reconsider);
22311
22312 input_location = saved_loc;
22313 }
22314
22315 /* Substitute ARGVEC into T, which is a list of initializers for
22316 either base class or a non-static data member. The TREE_PURPOSEs
22317 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22318 instantiate_decl. */
22319
22320 static tree
22321 tsubst_initializer_list (tree t, tree argvec)
22322 {
22323 tree inits = NULL_TREE;
22324
22325 for (; t; t = TREE_CHAIN (t))
22326 {
22327 tree decl;
22328 tree init;
22329 tree expanded_bases = NULL_TREE;
22330 tree expanded_arguments = NULL_TREE;
22331 int i, len = 1;
22332
22333 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
22334 {
22335 tree expr;
22336 tree arg;
22337
22338 /* Expand the base class expansion type into separate base
22339 classes. */
22340 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
22341 tf_warning_or_error,
22342 NULL_TREE);
22343 if (expanded_bases == error_mark_node)
22344 continue;
22345
22346 /* We'll be building separate TREE_LISTs of arguments for
22347 each base. */
22348 len = TREE_VEC_LENGTH (expanded_bases);
22349 expanded_arguments = make_tree_vec (len);
22350 for (i = 0; i < len; i++)
22351 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
22352
22353 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22354 expand each argument in the TREE_VALUE of t. */
22355 expr = make_node (EXPR_PACK_EXPANSION);
22356 PACK_EXPANSION_LOCAL_P (expr) = true;
22357 PACK_EXPANSION_PARAMETER_PACKS (expr) =
22358 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
22359
22360 if (TREE_VALUE (t) == void_type_node)
22361 /* VOID_TYPE_NODE is used to indicate
22362 value-initialization. */
22363 {
22364 for (i = 0; i < len; i++)
22365 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
22366 }
22367 else
22368 {
22369 /* Substitute parameter packs into each argument in the
22370 TREE_LIST. */
22371 in_base_initializer = 1;
22372 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
22373 {
22374 tree expanded_exprs;
22375
22376 /* Expand the argument. */
22377 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
22378 expanded_exprs
22379 = tsubst_pack_expansion (expr, argvec,
22380 tf_warning_or_error,
22381 NULL_TREE);
22382 if (expanded_exprs == error_mark_node)
22383 continue;
22384
22385 /* Prepend each of the expanded expressions to the
22386 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22387 for (i = 0; i < len; i++)
22388 {
22389 TREE_VEC_ELT (expanded_arguments, i) =
22390 tree_cons (NULL_TREE,
22391 TREE_VEC_ELT (expanded_exprs, i),
22392 TREE_VEC_ELT (expanded_arguments, i));
22393 }
22394 }
22395 in_base_initializer = 0;
22396
22397 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22398 since we built them backwards. */
22399 for (i = 0; i < len; i++)
22400 {
22401 TREE_VEC_ELT (expanded_arguments, i) =
22402 nreverse (TREE_VEC_ELT (expanded_arguments, i));
22403 }
22404 }
22405 }
22406
22407 for (i = 0; i < len; ++i)
22408 {
22409 if (expanded_bases)
22410 {
22411 decl = TREE_VEC_ELT (expanded_bases, i);
22412 decl = expand_member_init (decl);
22413 init = TREE_VEC_ELT (expanded_arguments, i);
22414 }
22415 else
22416 {
22417 tree tmp;
22418 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
22419 tf_warning_or_error, NULL_TREE);
22420
22421 decl = expand_member_init (decl);
22422 if (decl && !DECL_P (decl))
22423 in_base_initializer = 1;
22424
22425 init = TREE_VALUE (t);
22426 tmp = init;
22427 if (init != void_type_node)
22428 init = tsubst_expr (init, argvec,
22429 tf_warning_or_error, NULL_TREE,
22430 /*integral_constant_expression_p=*/false);
22431 if (init == NULL_TREE && tmp != NULL_TREE)
22432 /* If we had an initializer but it instantiated to nothing,
22433 value-initialize the object. This will only occur when
22434 the initializer was a pack expansion where the parameter
22435 packs used in that expansion were of length zero. */
22436 init = void_type_node;
22437 in_base_initializer = 0;
22438 }
22439
22440 if (decl)
22441 {
22442 init = build_tree_list (decl, init);
22443 TREE_CHAIN (init) = inits;
22444 inits = init;
22445 }
22446 }
22447 }
22448 return inits;
22449 }
22450
22451 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22452
22453 static void
22454 set_current_access_from_decl (tree decl)
22455 {
22456 if (TREE_PRIVATE (decl))
22457 current_access_specifier = access_private_node;
22458 else if (TREE_PROTECTED (decl))
22459 current_access_specifier = access_protected_node;
22460 else
22461 current_access_specifier = access_public_node;
22462 }
22463
22464 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22465 is the instantiation (which should have been created with
22466 start_enum) and ARGS are the template arguments to use. */
22467
22468 static void
22469 tsubst_enum (tree tag, tree newtag, tree args)
22470 {
22471 tree e;
22472
22473 if (SCOPED_ENUM_P (newtag))
22474 begin_scope (sk_scoped_enum, newtag);
22475
22476 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
22477 {
22478 tree value;
22479 tree decl;
22480
22481 decl = TREE_VALUE (e);
22482 /* Note that in a template enum, the TREE_VALUE is the
22483 CONST_DECL, not the corresponding INTEGER_CST. */
22484 value = tsubst_expr (DECL_INITIAL (decl),
22485 args, tf_warning_or_error, NULL_TREE,
22486 /*integral_constant_expression_p=*/true);
22487
22488 /* Give this enumeration constant the correct access. */
22489 set_current_access_from_decl (decl);
22490
22491 /* Actually build the enumerator itself. Here we're assuming that
22492 enumerators can't have dependent attributes. */
22493 build_enumerator (DECL_NAME (decl), value, newtag,
22494 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
22495 }
22496
22497 if (SCOPED_ENUM_P (newtag))
22498 finish_scope ();
22499
22500 finish_enum_value_list (newtag);
22501 finish_enum (newtag);
22502
22503 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
22504 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
22505 }
22506
22507 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22508 its type -- but without substituting the innermost set of template
22509 arguments. So, innermost set of template parameters will appear in
22510 the type. */
22511
22512 tree
22513 get_mostly_instantiated_function_type (tree decl)
22514 {
22515 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22516 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
22517 }
22518
22519 /* Return truthvalue if we're processing a template different from
22520 the last one involved in diagnostics. */
22521 bool
22522 problematic_instantiation_changed (void)
22523 {
22524 return current_tinst_level != last_error_tinst_level;
22525 }
22526
22527 /* Remember current template involved in diagnostics. */
22528 void
22529 record_last_problematic_instantiation (void)
22530 {
22531 last_error_tinst_level = current_tinst_level;
22532 }
22533
22534 struct tinst_level *
22535 current_instantiation (void)
22536 {
22537 return current_tinst_level;
22538 }
22539
22540 /* Return TRUE if current_function_decl is being instantiated, false
22541 otherwise. */
22542
22543 bool
22544 instantiating_current_function_p (void)
22545 {
22546 return (current_instantiation ()
22547 && current_instantiation ()->decl == current_function_decl);
22548 }
22549
22550 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22551 type. Return zero for ok, nonzero for disallowed. Issue error and
22552 warning messages under control of COMPLAIN. */
22553
22554 static int
22555 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
22556 {
22557 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
22558 return 0;
22559 else if (POINTER_TYPE_P (type))
22560 return 0;
22561 else if (TYPE_PTRMEM_P (type))
22562 return 0;
22563 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
22564 return 0;
22565 else if (TREE_CODE (type) == TYPENAME_TYPE)
22566 return 0;
22567 else if (TREE_CODE (type) == DECLTYPE_TYPE)
22568 return 0;
22569 else if (TREE_CODE (type) == NULLPTR_TYPE)
22570 return 0;
22571 /* A bound template template parm could later be instantiated to have a valid
22572 nontype parm type via an alias template. */
22573 else if (cxx_dialect >= cxx11
22574 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22575 return 0;
22576
22577 if (complain & tf_error)
22578 {
22579 if (type == error_mark_node)
22580 inform (input_location, "invalid template non-type parameter");
22581 else
22582 error ("%q#T is not a valid type for a template non-type parameter",
22583 type);
22584 }
22585 return 1;
22586 }
22587
22588 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22589 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22590
22591 static bool
22592 dependent_type_p_r (tree type)
22593 {
22594 tree scope;
22595
22596 /* [temp.dep.type]
22597
22598 A type is dependent if it is:
22599
22600 -- a template parameter. Template template parameters are types
22601 for us (since TYPE_P holds true for them) so we handle
22602 them here. */
22603 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22604 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
22605 return true;
22606 /* -- a qualified-id with a nested-name-specifier which contains a
22607 class-name that names a dependent type or whose unqualified-id
22608 names a dependent type. */
22609 if (TREE_CODE (type) == TYPENAME_TYPE)
22610 return true;
22611
22612 /* An alias template specialization can be dependent even if the
22613 resulting type is not. */
22614 if (dependent_alias_template_spec_p (type))
22615 return true;
22616
22617 /* -- a cv-qualified type where the cv-unqualified type is
22618 dependent.
22619 No code is necessary for this bullet; the code below handles
22620 cv-qualified types, and we don't want to strip aliases with
22621 TYPE_MAIN_VARIANT because of DR 1558. */
22622 /* -- a compound type constructed from any dependent type. */
22623 if (TYPE_PTRMEM_P (type))
22624 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
22625 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22626 (type)));
22627 else if (TYPE_PTR_P (type)
22628 || TREE_CODE (type) == REFERENCE_TYPE)
22629 return dependent_type_p (TREE_TYPE (type));
22630 else if (TREE_CODE (type) == FUNCTION_TYPE
22631 || TREE_CODE (type) == METHOD_TYPE)
22632 {
22633 tree arg_type;
22634
22635 if (dependent_type_p (TREE_TYPE (type)))
22636 return true;
22637 for (arg_type = TYPE_ARG_TYPES (type);
22638 arg_type;
22639 arg_type = TREE_CHAIN (arg_type))
22640 if (dependent_type_p (TREE_VALUE (arg_type)))
22641 return true;
22642 return false;
22643 }
22644 /* -- an array type constructed from any dependent type or whose
22645 size is specified by a constant expression that is
22646 value-dependent.
22647
22648 We checked for type- and value-dependence of the bounds in
22649 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22650 if (TREE_CODE (type) == ARRAY_TYPE)
22651 {
22652 if (TYPE_DOMAIN (type)
22653 && dependent_type_p (TYPE_DOMAIN (type)))
22654 return true;
22655 return dependent_type_p (TREE_TYPE (type));
22656 }
22657
22658 /* -- a template-id in which either the template name is a template
22659 parameter ... */
22660 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22661 return true;
22662 /* ... or any of the template arguments is a dependent type or
22663 an expression that is type-dependent or value-dependent. */
22664 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
22665 && (any_dependent_template_arguments_p
22666 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
22667 return true;
22668
22669 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22670 dependent; if the argument of the `typeof' expression is not
22671 type-dependent, then it should already been have resolved. */
22672 if (TREE_CODE (type) == TYPEOF_TYPE
22673 || TREE_CODE (type) == DECLTYPE_TYPE
22674 || TREE_CODE (type) == UNDERLYING_TYPE)
22675 return true;
22676
22677 /* A template argument pack is dependent if any of its packed
22678 arguments are. */
22679 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
22680 {
22681 tree args = ARGUMENT_PACK_ARGS (type);
22682 int i, len = TREE_VEC_LENGTH (args);
22683 for (i = 0; i < len; ++i)
22684 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22685 return true;
22686 }
22687
22688 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22689 be template parameters. */
22690 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
22691 return true;
22692
22693 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
22694 return true;
22695
22696 /* The standard does not specifically mention types that are local
22697 to template functions or local classes, but they should be
22698 considered dependent too. For example:
22699
22700 template <int I> void f() {
22701 enum E { a = I };
22702 S<sizeof (E)> s;
22703 }
22704
22705 The size of `E' cannot be known until the value of `I' has been
22706 determined. Therefore, `E' must be considered dependent. */
22707 scope = TYPE_CONTEXT (type);
22708 if (scope && TYPE_P (scope))
22709 return dependent_type_p (scope);
22710 /* Don't use type_dependent_expression_p here, as it can lead
22711 to infinite recursion trying to determine whether a lambda
22712 nested in a lambda is dependent (c++/47687). */
22713 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
22714 && DECL_LANG_SPECIFIC (scope)
22715 && DECL_TEMPLATE_INFO (scope)
22716 && (any_dependent_template_arguments_p
22717 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
22718 return true;
22719
22720 /* Other types are non-dependent. */
22721 return false;
22722 }
22723
22724 /* Returns TRUE if TYPE is dependent, in the sense of
22725 [temp.dep.type]. Note that a NULL type is considered dependent. */
22726
22727 bool
22728 dependent_type_p (tree type)
22729 {
22730 /* If there are no template parameters in scope, then there can't be
22731 any dependent types. */
22732 if (!processing_template_decl)
22733 {
22734 /* If we are not processing a template, then nobody should be
22735 providing us with a dependent type. */
22736 gcc_assert (type);
22737 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
22738 return false;
22739 }
22740
22741 /* If the type is NULL, we have not computed a type for the entity
22742 in question; in that case, the type is dependent. */
22743 if (!type)
22744 return true;
22745
22746 /* Erroneous types can be considered non-dependent. */
22747 if (type == error_mark_node)
22748 return false;
22749
22750 /* If we have not already computed the appropriate value for TYPE,
22751 do so now. */
22752 if (!TYPE_DEPENDENT_P_VALID (type))
22753 {
22754 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
22755 TYPE_DEPENDENT_P_VALID (type) = 1;
22756 }
22757
22758 return TYPE_DEPENDENT_P (type);
22759 }
22760
22761 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22762 lookup. In other words, a dependent type that is not the current
22763 instantiation. */
22764
22765 bool
22766 dependent_scope_p (tree scope)
22767 {
22768 return (scope && TYPE_P (scope) && dependent_type_p (scope)
22769 && !currently_open_class (scope));
22770 }
22771
22772 /* T is a SCOPE_REF; return whether we need to consider it
22773 instantiation-dependent so that we can check access at instantiation
22774 time even though we know which member it resolves to. */
22775
22776 static bool
22777 instantiation_dependent_scope_ref_p (tree t)
22778 {
22779 if (DECL_P (TREE_OPERAND (t, 1))
22780 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
22781 && accessible_in_template_p (TREE_OPERAND (t, 0),
22782 TREE_OPERAND (t, 1)))
22783 return false;
22784 else
22785 return true;
22786 }
22787
22788 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22789 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22790 expression. */
22791
22792 /* Note that this predicate is not appropriate for general expressions;
22793 only constant expressions (that satisfy potential_constant_expression)
22794 can be tested for value dependence. */
22795
22796 bool
22797 value_dependent_expression_p (tree expression)
22798 {
22799 if (!processing_template_decl || expression == NULL_TREE)
22800 return false;
22801
22802 /* A name declared with a dependent type. */
22803 if (DECL_P (expression) && type_dependent_expression_p (expression))
22804 return true;
22805
22806 switch (TREE_CODE (expression))
22807 {
22808 case BASELINK:
22809 /* A dependent member function of the current instantiation. */
22810 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
22811
22812 case FUNCTION_DECL:
22813 /* A dependent member function of the current instantiation. */
22814 if (DECL_CLASS_SCOPE_P (expression)
22815 && dependent_type_p (DECL_CONTEXT (expression)))
22816 return true;
22817 break;
22818
22819 case IDENTIFIER_NODE:
22820 /* A name that has not been looked up -- must be dependent. */
22821 return true;
22822
22823 case TEMPLATE_PARM_INDEX:
22824 /* A non-type template parm. */
22825 return true;
22826
22827 case CONST_DECL:
22828 /* A non-type template parm. */
22829 if (DECL_TEMPLATE_PARM_P (expression))
22830 return true;
22831 return value_dependent_expression_p (DECL_INITIAL (expression));
22832
22833 case VAR_DECL:
22834 /* A constant with literal type and is initialized
22835 with an expression that is value-dependent.
22836
22837 Note that a non-dependent parenthesized initializer will have
22838 already been replaced with its constant value, so if we see
22839 a TREE_LIST it must be dependent. */
22840 if (DECL_INITIAL (expression)
22841 && decl_constant_var_p (expression)
22842 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
22843 /* cp_finish_decl doesn't fold reference initializers. */
22844 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
22845 || type_dependent_expression_p (DECL_INITIAL (expression))
22846 || value_dependent_expression_p (DECL_INITIAL (expression))))
22847 return true;
22848 return false;
22849
22850 case DYNAMIC_CAST_EXPR:
22851 case STATIC_CAST_EXPR:
22852 case CONST_CAST_EXPR:
22853 case REINTERPRET_CAST_EXPR:
22854 case CAST_EXPR:
22855 /* These expressions are value-dependent if the type to which
22856 the cast occurs is dependent or the expression being casted
22857 is value-dependent. */
22858 {
22859 tree type = TREE_TYPE (expression);
22860
22861 if (dependent_type_p (type))
22862 return true;
22863
22864 /* A functional cast has a list of operands. */
22865 expression = TREE_OPERAND (expression, 0);
22866 if (!expression)
22867 {
22868 /* If there are no operands, it must be an expression such
22869 as "int()". This should not happen for aggregate types
22870 because it would form non-constant expressions. */
22871 gcc_assert (cxx_dialect >= cxx11
22872 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
22873
22874 return false;
22875 }
22876
22877 if (TREE_CODE (expression) == TREE_LIST)
22878 return any_value_dependent_elements_p (expression);
22879
22880 return value_dependent_expression_p (expression);
22881 }
22882
22883 case SIZEOF_EXPR:
22884 if (SIZEOF_EXPR_TYPE_P (expression))
22885 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
22886 /* FALLTHRU */
22887 case ALIGNOF_EXPR:
22888 case TYPEID_EXPR:
22889 /* A `sizeof' expression is value-dependent if the operand is
22890 type-dependent or is a pack expansion. */
22891 expression = TREE_OPERAND (expression, 0);
22892 if (PACK_EXPANSION_P (expression))
22893 return true;
22894 else if (TYPE_P (expression))
22895 return dependent_type_p (expression);
22896 return instantiation_dependent_uneval_expression_p (expression);
22897
22898 case AT_ENCODE_EXPR:
22899 /* An 'encode' expression is value-dependent if the operand is
22900 type-dependent. */
22901 expression = TREE_OPERAND (expression, 0);
22902 return dependent_type_p (expression);
22903
22904 case NOEXCEPT_EXPR:
22905 expression = TREE_OPERAND (expression, 0);
22906 return instantiation_dependent_uneval_expression_p (expression);
22907
22908 case SCOPE_REF:
22909 /* All instantiation-dependent expressions should also be considered
22910 value-dependent. */
22911 return instantiation_dependent_scope_ref_p (expression);
22912
22913 case COMPONENT_REF:
22914 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
22915 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
22916
22917 case NONTYPE_ARGUMENT_PACK:
22918 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22919 is value-dependent. */
22920 {
22921 tree values = ARGUMENT_PACK_ARGS (expression);
22922 int i, len = TREE_VEC_LENGTH (values);
22923
22924 for (i = 0; i < len; ++i)
22925 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
22926 return true;
22927
22928 return false;
22929 }
22930
22931 case TRAIT_EXPR:
22932 {
22933 tree type2 = TRAIT_EXPR_TYPE2 (expression);
22934 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
22935 || (type2 ? dependent_type_p (type2) : false));
22936 }
22937
22938 case MODOP_EXPR:
22939 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22940 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
22941
22942 case ARRAY_REF:
22943 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22944 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
22945
22946 case ADDR_EXPR:
22947 {
22948 tree op = TREE_OPERAND (expression, 0);
22949 return (value_dependent_expression_p (op)
22950 || has_value_dependent_address (op));
22951 }
22952
22953 case REQUIRES_EXPR:
22954 /* Treat all requires-expressions as value-dependent so
22955 we don't try to fold them. */
22956 return true;
22957
22958 case TYPE_REQ:
22959 return dependent_type_p (TREE_OPERAND (expression, 0));
22960
22961 case CALL_EXPR:
22962 {
22963 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
22964 return true;
22965 tree fn = get_callee_fndecl (expression);
22966 int i, nargs;
22967 nargs = call_expr_nargs (expression);
22968 for (i = 0; i < nargs; ++i)
22969 {
22970 tree op = CALL_EXPR_ARG (expression, i);
22971 /* In a call to a constexpr member function, look through the
22972 implicit ADDR_EXPR on the object argument so that it doesn't
22973 cause the call to be considered value-dependent. We also
22974 look through it in potential_constant_expression. */
22975 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
22976 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
22977 && TREE_CODE (op) == ADDR_EXPR)
22978 op = TREE_OPERAND (op, 0);
22979 if (value_dependent_expression_p (op))
22980 return true;
22981 }
22982 return false;
22983 }
22984
22985 case TEMPLATE_ID_EXPR:
22986 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22987 type-dependent. */
22988 return type_dependent_expression_p (expression)
22989 || variable_concept_p (TREE_OPERAND (expression, 0));
22990
22991 case CONSTRUCTOR:
22992 {
22993 unsigned ix;
22994 tree val;
22995 if (dependent_type_p (TREE_TYPE (expression)))
22996 return true;
22997 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
22998 if (value_dependent_expression_p (val))
22999 return true;
23000 return false;
23001 }
23002
23003 case STMT_EXPR:
23004 /* Treat a GNU statement expression as dependent to avoid crashing
23005 under instantiate_non_dependent_expr; it can't be constant. */
23006 return true;
23007
23008 default:
23009 /* A constant expression is value-dependent if any subexpression is
23010 value-dependent. */
23011 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23012 {
23013 case tcc_reference:
23014 case tcc_unary:
23015 case tcc_comparison:
23016 case tcc_binary:
23017 case tcc_expression:
23018 case tcc_vl_exp:
23019 {
23020 int i, len = cp_tree_operand_length (expression);
23021
23022 for (i = 0; i < len; i++)
23023 {
23024 tree t = TREE_OPERAND (expression, i);
23025
23026 /* In some cases, some of the operands may be missing.l
23027 (For example, in the case of PREDECREMENT_EXPR, the
23028 amount to increment by may be missing.) That doesn't
23029 make the expression dependent. */
23030 if (t && value_dependent_expression_p (t))
23031 return true;
23032 }
23033 }
23034 break;
23035 default:
23036 break;
23037 }
23038 break;
23039 }
23040
23041 /* The expression is not value-dependent. */
23042 return false;
23043 }
23044
23045 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23046 [temp.dep.expr]. Note that an expression with no type is
23047 considered dependent. Other parts of the compiler arrange for an
23048 expression with type-dependent subexpressions to have no type, so
23049 this function doesn't have to be fully recursive. */
23050
23051 bool
23052 type_dependent_expression_p (tree expression)
23053 {
23054 if (!processing_template_decl)
23055 return false;
23056
23057 if (expression == NULL_TREE || expression == error_mark_node)
23058 return false;
23059
23060 /* An unresolved name is always dependent. */
23061 if (identifier_p (expression)
23062 || TREE_CODE (expression) == USING_DECL
23063 || TREE_CODE (expression) == WILDCARD_DECL)
23064 return true;
23065
23066 /* A fold expression is type-dependent. */
23067 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23068 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23069 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23070 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23071 return true;
23072
23073 /* Some expression forms are never type-dependent. */
23074 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23075 || TREE_CODE (expression) == SIZEOF_EXPR
23076 || TREE_CODE (expression) == ALIGNOF_EXPR
23077 || TREE_CODE (expression) == AT_ENCODE_EXPR
23078 || TREE_CODE (expression) == NOEXCEPT_EXPR
23079 || TREE_CODE (expression) == TRAIT_EXPR
23080 || TREE_CODE (expression) == TYPEID_EXPR
23081 || TREE_CODE (expression) == DELETE_EXPR
23082 || TREE_CODE (expression) == VEC_DELETE_EXPR
23083 || TREE_CODE (expression) == THROW_EXPR
23084 || TREE_CODE (expression) == REQUIRES_EXPR)
23085 return false;
23086
23087 /* The types of these expressions depends only on the type to which
23088 the cast occurs. */
23089 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23090 || TREE_CODE (expression) == STATIC_CAST_EXPR
23091 || TREE_CODE (expression) == CONST_CAST_EXPR
23092 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23093 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23094 || TREE_CODE (expression) == CAST_EXPR)
23095 return dependent_type_p (TREE_TYPE (expression));
23096
23097 /* The types of these expressions depends only on the type created
23098 by the expression. */
23099 if (TREE_CODE (expression) == NEW_EXPR
23100 || TREE_CODE (expression) == VEC_NEW_EXPR)
23101 {
23102 /* For NEW_EXPR tree nodes created inside a template, either
23103 the object type itself or a TREE_LIST may appear as the
23104 operand 1. */
23105 tree type = TREE_OPERAND (expression, 1);
23106 if (TREE_CODE (type) == TREE_LIST)
23107 /* This is an array type. We need to check array dimensions
23108 as well. */
23109 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23110 || value_dependent_expression_p
23111 (TREE_OPERAND (TREE_VALUE (type), 1));
23112 else
23113 return dependent_type_p (type);
23114 }
23115
23116 if (TREE_CODE (expression) == SCOPE_REF)
23117 {
23118 tree scope = TREE_OPERAND (expression, 0);
23119 tree name = TREE_OPERAND (expression, 1);
23120
23121 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23122 contains an identifier associated by name lookup with one or more
23123 declarations declared with a dependent type, or...a
23124 nested-name-specifier or qualified-id that names a member of an
23125 unknown specialization. */
23126 return (type_dependent_expression_p (name)
23127 || dependent_scope_p (scope));
23128 }
23129
23130 if (TREE_CODE (expression) == TEMPLATE_DECL
23131 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23132 return uses_outer_template_parms (expression);
23133
23134 if (TREE_CODE (expression) == STMT_EXPR)
23135 expression = stmt_expr_value_expr (expression);
23136
23137 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23138 {
23139 tree elt;
23140 unsigned i;
23141
23142 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23143 {
23144 if (type_dependent_expression_p (elt))
23145 return true;
23146 }
23147 return false;
23148 }
23149
23150 /* A static data member of the current instantiation with incomplete
23151 array type is type-dependent, as the definition and specializations
23152 can have different bounds. */
23153 if (VAR_P (expression)
23154 && DECL_CLASS_SCOPE_P (expression)
23155 && dependent_type_p (DECL_CONTEXT (expression))
23156 && VAR_HAD_UNKNOWN_BOUND (expression))
23157 return true;
23158
23159 /* An array of unknown bound depending on a variadic parameter, eg:
23160
23161 template<typename... Args>
23162 void foo (Args... args)
23163 {
23164 int arr[] = { args... };
23165 }
23166
23167 template<int... vals>
23168 void bar ()
23169 {
23170 int arr[] = { vals... };
23171 }
23172
23173 If the array has no length and has an initializer, it must be that
23174 we couldn't determine its length in cp_complete_array_type because
23175 it is dependent. */
23176 if (VAR_P (expression)
23177 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23178 && !TYPE_DOMAIN (TREE_TYPE (expression))
23179 && DECL_INITIAL (expression))
23180 return true;
23181
23182 /* A function or variable template-id is type-dependent if it has any
23183 dependent template arguments. Note that we only consider the innermost
23184 template arguments here, since those are the ones that come from the
23185 template-id; the template arguments for the enclosing class do not make it
23186 type-dependent, they only make a member function value-dependent. */
23187 if (VAR_OR_FUNCTION_DECL_P (expression)
23188 && DECL_LANG_SPECIFIC (expression)
23189 && DECL_TEMPLATE_INFO (expression)
23190 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23191 && (any_dependent_template_arguments_p
23192 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23193 return true;
23194
23195 /* Always dependent, on the number of arguments if nothing else. */
23196 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23197 return true;
23198
23199 if (TREE_TYPE (expression) == unknown_type_node)
23200 {
23201 if (TREE_CODE (expression) == ADDR_EXPR)
23202 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23203 if (TREE_CODE (expression) == COMPONENT_REF
23204 || TREE_CODE (expression) == OFFSET_REF)
23205 {
23206 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23207 return true;
23208 expression = TREE_OPERAND (expression, 1);
23209 if (identifier_p (expression))
23210 return false;
23211 }
23212 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23213 if (TREE_CODE (expression) == SCOPE_REF)
23214 return false;
23215
23216 if (BASELINK_P (expression))
23217 {
23218 if (BASELINK_OPTYPE (expression)
23219 && dependent_type_p (BASELINK_OPTYPE (expression)))
23220 return true;
23221 expression = BASELINK_FUNCTIONS (expression);
23222 }
23223
23224 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23225 {
23226 if (any_dependent_template_arguments_p
23227 (TREE_OPERAND (expression, 1)))
23228 return true;
23229 expression = TREE_OPERAND (expression, 0);
23230 if (identifier_p (expression))
23231 return true;
23232 }
23233
23234 gcc_assert (TREE_CODE (expression) == OVERLOAD
23235 || TREE_CODE (expression) == FUNCTION_DECL);
23236
23237 while (expression)
23238 {
23239 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23240 return true;
23241 expression = OVL_NEXT (expression);
23242 }
23243 return false;
23244 }
23245
23246 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23247
23248 /* Dependent type attributes might not have made it from the decl to
23249 the type yet. */
23250 if (DECL_P (expression)
23251 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23252 return true;
23253
23254 return (dependent_type_p (TREE_TYPE (expression)));
23255 }
23256
23257 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23258 type-dependent if the expression refers to a member of the current
23259 instantiation and the type of the referenced member is dependent, or the
23260 class member access expression refers to a member of an unknown
23261 specialization.
23262
23263 This function returns true if the OBJECT in such a class member access
23264 expression is of an unknown specialization. */
23265
23266 bool
23267 type_dependent_object_expression_p (tree object)
23268 {
23269 tree scope = TREE_TYPE (object);
23270 return (!scope || dependent_scope_p (scope));
23271 }
23272
23273 /* walk_tree callback function for instantiation_dependent_expression_p,
23274 below. Returns non-zero if a dependent subexpression is found. */
23275
23276 static tree
23277 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23278 void * /*data*/)
23279 {
23280 if (TYPE_P (*tp))
23281 {
23282 /* We don't have to worry about decltype currently because decltype
23283 of an instantiation-dependent expr is a dependent type. This
23284 might change depending on the resolution of DR 1172. */
23285 *walk_subtrees = false;
23286 return NULL_TREE;
23287 }
23288 enum tree_code code = TREE_CODE (*tp);
23289 switch (code)
23290 {
23291 /* Don't treat an argument list as dependent just because it has no
23292 TREE_TYPE. */
23293 case TREE_LIST:
23294 case TREE_VEC:
23295 return NULL_TREE;
23296
23297 case TEMPLATE_PARM_INDEX:
23298 return *tp;
23299
23300 /* Handle expressions with type operands. */
23301 case SIZEOF_EXPR:
23302 case ALIGNOF_EXPR:
23303 case TYPEID_EXPR:
23304 case AT_ENCODE_EXPR:
23305 {
23306 tree op = TREE_OPERAND (*tp, 0);
23307 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
23308 op = TREE_TYPE (op);
23309 if (TYPE_P (op))
23310 {
23311 if (dependent_type_p (op))
23312 return *tp;
23313 else
23314 {
23315 *walk_subtrees = false;
23316 return NULL_TREE;
23317 }
23318 }
23319 break;
23320 }
23321
23322 case COMPONENT_REF:
23323 if (identifier_p (TREE_OPERAND (*tp, 1)))
23324 /* In a template, finish_class_member_access_expr creates a
23325 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
23326 type-dependent, so that we can check access control at
23327 instantiation time (PR 42277). See also Core issue 1273. */
23328 return *tp;
23329 break;
23330
23331 case SCOPE_REF:
23332 if (instantiation_dependent_scope_ref_p (*tp))
23333 return *tp;
23334 else
23335 break;
23336
23337 /* Treat statement-expressions as dependent. */
23338 case BIND_EXPR:
23339 return *tp;
23340
23341 /* Treat requires-expressions as dependent. */
23342 case REQUIRES_EXPR:
23343 return *tp;
23344
23345 case CALL_EXPR:
23346 /* Treat calls to function concepts as dependent. */
23347 if (function_concept_check_p (*tp))
23348 return *tp;
23349 break;
23350
23351 case TEMPLATE_ID_EXPR:
23352 /* And variable concepts. */
23353 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
23354 return *tp;
23355 break;
23356
23357 default:
23358 break;
23359 }
23360
23361 if (type_dependent_expression_p (*tp))
23362 return *tp;
23363 else
23364 return NULL_TREE;
23365 }
23366
23367 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23368 sense defined by the ABI:
23369
23370 "An expression is instantiation-dependent if it is type-dependent
23371 or value-dependent, or it has a subexpression that is type-dependent
23372 or value-dependent."
23373
23374 Except don't actually check value-dependence for unevaluated expressions,
23375 because in sizeof(i) we don't care about the value of i. Checking
23376 type-dependence will in turn check value-dependence of array bounds/template
23377 arguments as needed. */
23378
23379 bool
23380 instantiation_dependent_uneval_expression_p (tree expression)
23381 {
23382 tree result;
23383
23384 if (!processing_template_decl)
23385 return false;
23386
23387 if (expression == error_mark_node)
23388 return false;
23389
23390 result = cp_walk_tree_without_duplicates (&expression,
23391 instantiation_dependent_r, NULL);
23392 return result != NULL_TREE;
23393 }
23394
23395 /* As above, but also check value-dependence of the expression as a whole. */
23396
23397 bool
23398 instantiation_dependent_expression_p (tree expression)
23399 {
23400 return (instantiation_dependent_uneval_expression_p (expression)
23401 || value_dependent_expression_p (expression));
23402 }
23403
23404 /* Like type_dependent_expression_p, but it also works while not processing
23405 a template definition, i.e. during substitution or mangling. */
23406
23407 bool
23408 type_dependent_expression_p_push (tree expr)
23409 {
23410 bool b;
23411 ++processing_template_decl;
23412 b = type_dependent_expression_p (expr);
23413 --processing_template_decl;
23414 return b;
23415 }
23416
23417 /* Returns TRUE if ARGS contains a type-dependent expression. */
23418
23419 bool
23420 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
23421 {
23422 unsigned int i;
23423 tree arg;
23424
23425 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
23426 {
23427 if (type_dependent_expression_p (arg))
23428 return true;
23429 }
23430 return false;
23431 }
23432
23433 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23434 expressions) contains any type-dependent expressions. */
23435
23436 bool
23437 any_type_dependent_elements_p (const_tree list)
23438 {
23439 for (; list; list = TREE_CHAIN (list))
23440 if (type_dependent_expression_p (TREE_VALUE (list)))
23441 return true;
23442
23443 return false;
23444 }
23445
23446 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23447 expressions) contains any value-dependent expressions. */
23448
23449 bool
23450 any_value_dependent_elements_p (const_tree list)
23451 {
23452 for (; list; list = TREE_CHAIN (list))
23453 if (value_dependent_expression_p (TREE_VALUE (list)))
23454 return true;
23455
23456 return false;
23457 }
23458
23459 /* Returns TRUE if the ARG (a template argument) is dependent. */
23460
23461 bool
23462 dependent_template_arg_p (tree arg)
23463 {
23464 if (!processing_template_decl)
23465 return false;
23466
23467 /* Assume a template argument that was wrongly written by the user
23468 is dependent. This is consistent with what
23469 any_dependent_template_arguments_p [that calls this function]
23470 does. */
23471 if (!arg || arg == error_mark_node)
23472 return true;
23473
23474 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
23475 arg = ARGUMENT_PACK_SELECT_ARG (arg);
23476
23477 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
23478 return true;
23479 if (TREE_CODE (arg) == TEMPLATE_DECL)
23480 {
23481 if (DECL_TEMPLATE_PARM_P (arg))
23482 return true;
23483 /* A member template of a dependent class is not necessarily
23484 type-dependent, but it is a dependent template argument because it
23485 will be a member of an unknown specialization to that template. */
23486 tree scope = CP_DECL_CONTEXT (arg);
23487 return TYPE_P (scope) && dependent_type_p (scope);
23488 }
23489 else if (ARGUMENT_PACK_P (arg))
23490 {
23491 tree args = ARGUMENT_PACK_ARGS (arg);
23492 int i, len = TREE_VEC_LENGTH (args);
23493 for (i = 0; i < len; ++i)
23494 {
23495 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23496 return true;
23497 }
23498
23499 return false;
23500 }
23501 else if (TYPE_P (arg))
23502 return dependent_type_p (arg);
23503 else
23504 return (type_dependent_expression_p (arg)
23505 || value_dependent_expression_p (arg));
23506 }
23507
23508 /* Returns true if ARGS (a collection of template arguments) contains
23509 any types that require structural equality testing. */
23510
23511 bool
23512 any_template_arguments_need_structural_equality_p (tree args)
23513 {
23514 int i;
23515 int j;
23516
23517 if (!args)
23518 return false;
23519 if (args == error_mark_node)
23520 return true;
23521
23522 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23523 {
23524 tree level = TMPL_ARGS_LEVEL (args, i + 1);
23525 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23526 {
23527 tree arg = TREE_VEC_ELT (level, j);
23528 tree packed_args = NULL_TREE;
23529 int k, len = 1;
23530
23531 if (ARGUMENT_PACK_P (arg))
23532 {
23533 /* Look inside the argument pack. */
23534 packed_args = ARGUMENT_PACK_ARGS (arg);
23535 len = TREE_VEC_LENGTH (packed_args);
23536 }
23537
23538 for (k = 0; k < len; ++k)
23539 {
23540 if (packed_args)
23541 arg = TREE_VEC_ELT (packed_args, k);
23542
23543 if (error_operand_p (arg))
23544 return true;
23545 else if (TREE_CODE (arg) == TEMPLATE_DECL)
23546 continue;
23547 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
23548 return true;
23549 else if (!TYPE_P (arg) && TREE_TYPE (arg)
23550 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
23551 return true;
23552 }
23553 }
23554 }
23555
23556 return false;
23557 }
23558
23559 /* Returns true if ARGS (a collection of template arguments) contains
23560 any dependent arguments. */
23561
23562 bool
23563 any_dependent_template_arguments_p (const_tree args)
23564 {
23565 int i;
23566 int j;
23567
23568 if (!args)
23569 return false;
23570 if (args == error_mark_node)
23571 return true;
23572
23573 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23574 {
23575 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
23576 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23577 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
23578 return true;
23579 }
23580
23581 return false;
23582 }
23583
23584 /* Returns TRUE if the template TMPL is type-dependent. */
23585
23586 bool
23587 dependent_template_p (tree tmpl)
23588 {
23589 if (TREE_CODE (tmpl) == OVERLOAD)
23590 {
23591 while (tmpl)
23592 {
23593 if (dependent_template_p (OVL_CURRENT (tmpl)))
23594 return true;
23595 tmpl = OVL_NEXT (tmpl);
23596 }
23597 return false;
23598 }
23599
23600 /* Template template parameters are dependent. */
23601 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
23602 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
23603 return true;
23604 /* So are names that have not been looked up. */
23605 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
23606 return true;
23607 return false;
23608 }
23609
23610 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23611
23612 bool
23613 dependent_template_id_p (tree tmpl, tree args)
23614 {
23615 return (dependent_template_p (tmpl)
23616 || any_dependent_template_arguments_p (args));
23617 }
23618
23619 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23620 are dependent. */
23621
23622 bool
23623 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
23624 {
23625 int i;
23626
23627 if (!processing_template_decl)
23628 return false;
23629
23630 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
23631 {
23632 tree decl = TREE_VEC_ELT (declv, i);
23633 tree init = TREE_VEC_ELT (initv, i);
23634 tree cond = TREE_VEC_ELT (condv, i);
23635 tree incr = TREE_VEC_ELT (incrv, i);
23636
23637 if (type_dependent_expression_p (decl)
23638 || TREE_CODE (decl) == SCOPE_REF)
23639 return true;
23640
23641 if (init && type_dependent_expression_p (init))
23642 return true;
23643
23644 if (type_dependent_expression_p (cond))
23645 return true;
23646
23647 if (COMPARISON_CLASS_P (cond)
23648 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
23649 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
23650 return true;
23651
23652 if (TREE_CODE (incr) == MODOP_EXPR)
23653 {
23654 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
23655 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
23656 return true;
23657 }
23658 else if (type_dependent_expression_p (incr))
23659 return true;
23660 else if (TREE_CODE (incr) == MODIFY_EXPR)
23661 {
23662 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
23663 return true;
23664 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
23665 {
23666 tree t = TREE_OPERAND (incr, 1);
23667 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
23668 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
23669 return true;
23670 }
23671 }
23672 }
23673
23674 return false;
23675 }
23676
23677 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23678 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23679 no such TYPE can be found. Note that this function peers inside
23680 uninstantiated templates and therefore should be used only in
23681 extremely limited situations. ONLY_CURRENT_P restricts this
23682 peering to the currently open classes hierarchy (which is required
23683 when comparing types). */
23684
23685 tree
23686 resolve_typename_type (tree type, bool only_current_p)
23687 {
23688 tree scope;
23689 tree name;
23690 tree decl;
23691 int quals;
23692 tree pushed_scope;
23693 tree result;
23694
23695 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
23696
23697 scope = TYPE_CONTEXT (type);
23698 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23699 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23700 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23701 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23702 identifier of the TYPENAME_TYPE anymore.
23703 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23704 TYPENAME_TYPE instead, we avoid messing up with a possible
23705 typedef variant case. */
23706 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
23707
23708 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23709 it first before we can figure out what NAME refers to. */
23710 if (TREE_CODE (scope) == TYPENAME_TYPE)
23711 {
23712 if (TYPENAME_IS_RESOLVING_P (scope))
23713 /* Given a class template A with a dependent base with nested type C,
23714 typedef typename A::C::C C will land us here, as trying to resolve
23715 the initial A::C leads to the local C typedef, which leads back to
23716 A::C::C. So we break the recursion now. */
23717 return type;
23718 else
23719 scope = resolve_typename_type (scope, only_current_p);
23720 }
23721 /* If we don't know what SCOPE refers to, then we cannot resolve the
23722 TYPENAME_TYPE. */
23723 if (!CLASS_TYPE_P (scope))
23724 return type;
23725 /* If this is a typedef, we don't want to look inside (c++/11987). */
23726 if (typedef_variant_p (type))
23727 return type;
23728 /* If SCOPE isn't the template itself, it will not have a valid
23729 TYPE_FIELDS list. */
23730 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
23731 /* scope is either the template itself or a compatible instantiation
23732 like X<T>, so look up the name in the original template. */
23733 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
23734 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
23735 gcc_checking_assert (uses_template_parms (scope));
23736 /* If scope has no fields, it can't be a current instantiation. Check this
23737 before currently_open_class to avoid infinite recursion (71515). */
23738 if (!TYPE_FIELDS (scope))
23739 return type;
23740 /* If the SCOPE is not the current instantiation, there's no reason
23741 to look inside it. */
23742 if (only_current_p && !currently_open_class (scope))
23743 return type;
23744 /* Enter the SCOPE so that name lookup will be resolved as if we
23745 were in the class definition. In particular, SCOPE will no
23746 longer be considered a dependent type. */
23747 pushed_scope = push_scope (scope);
23748 /* Look up the declaration. */
23749 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
23750 tf_warning_or_error);
23751
23752 result = NULL_TREE;
23753
23754 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23755 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23756 if (!decl)
23757 /*nop*/;
23758 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
23759 && TREE_CODE (decl) == TYPE_DECL)
23760 {
23761 result = TREE_TYPE (decl);
23762 if (result == error_mark_node)
23763 result = NULL_TREE;
23764 }
23765 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
23766 && DECL_CLASS_TEMPLATE_P (decl))
23767 {
23768 tree tmpl;
23769 tree args;
23770 /* Obtain the template and the arguments. */
23771 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
23772 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
23773 /* Instantiate the template. */
23774 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
23775 /*entering_scope=*/0,
23776 tf_error | tf_user);
23777 if (result == error_mark_node)
23778 result = NULL_TREE;
23779 }
23780
23781 /* Leave the SCOPE. */
23782 if (pushed_scope)
23783 pop_scope (pushed_scope);
23784
23785 /* If we failed to resolve it, return the original typename. */
23786 if (!result)
23787 return type;
23788
23789 /* If lookup found a typename type, resolve that too. */
23790 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
23791 {
23792 /* Ill-formed programs can cause infinite recursion here, so we
23793 must catch that. */
23794 TYPENAME_IS_RESOLVING_P (result) = 1;
23795 result = resolve_typename_type (result, only_current_p);
23796 TYPENAME_IS_RESOLVING_P (result) = 0;
23797 }
23798
23799 /* Qualify the resulting type. */
23800 quals = cp_type_quals (type);
23801 if (quals)
23802 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
23803
23804 return result;
23805 }
23806
23807 /* EXPR is an expression which is not type-dependent. Return a proxy
23808 for EXPR that can be used to compute the types of larger
23809 expressions containing EXPR. */
23810
23811 tree
23812 build_non_dependent_expr (tree expr)
23813 {
23814 tree inner_expr;
23815
23816 /* When checking, try to get a constant value for all non-dependent
23817 expressions in order to expose bugs in *_dependent_expression_p
23818 and constexpr. This can affect code generation, see PR70704, so
23819 only do this for -fchecking=2. */
23820 if (flag_checking > 1
23821 && cxx_dialect >= cxx11
23822 /* Don't do this during nsdmi parsing as it can lead to
23823 unexpected recursive instantiations. */
23824 && !parsing_nsdmi ()
23825 /* Don't do this during concept expansion either and for
23826 the same reason. */
23827 && !expanding_concept ())
23828 fold_non_dependent_expr (expr);
23829
23830 /* Preserve OVERLOADs; the functions must be available to resolve
23831 types. */
23832 inner_expr = expr;
23833 if (TREE_CODE (inner_expr) == STMT_EXPR)
23834 inner_expr = stmt_expr_value_expr (inner_expr);
23835 if (TREE_CODE (inner_expr) == ADDR_EXPR)
23836 inner_expr = TREE_OPERAND (inner_expr, 0);
23837 if (TREE_CODE (inner_expr) == COMPONENT_REF)
23838 inner_expr = TREE_OPERAND (inner_expr, 1);
23839 if (is_overloaded_fn (inner_expr)
23840 || TREE_CODE (inner_expr) == OFFSET_REF)
23841 return expr;
23842 /* There is no need to return a proxy for a variable. */
23843 if (VAR_P (expr))
23844 return expr;
23845 /* Preserve string constants; conversions from string constants to
23846 "char *" are allowed, even though normally a "const char *"
23847 cannot be used to initialize a "char *". */
23848 if (TREE_CODE (expr) == STRING_CST)
23849 return expr;
23850 /* Preserve void and arithmetic constants, as an optimization -- there is no
23851 reason to create a new node. */
23852 if (TREE_CODE (expr) == VOID_CST
23853 || TREE_CODE (expr) == INTEGER_CST
23854 || TREE_CODE (expr) == REAL_CST)
23855 return expr;
23856 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23857 There is at least one place where we want to know that a
23858 particular expression is a throw-expression: when checking a ?:
23859 expression, there are special rules if the second or third
23860 argument is a throw-expression. */
23861 if (TREE_CODE (expr) == THROW_EXPR)
23862 return expr;
23863
23864 /* Don't wrap an initializer list, we need to be able to look inside. */
23865 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
23866 return expr;
23867
23868 /* Don't wrap a dummy object, we need to be able to test for it. */
23869 if (is_dummy_object (expr))
23870 return expr;
23871
23872 if (TREE_CODE (expr) == COND_EXPR)
23873 return build3 (COND_EXPR,
23874 TREE_TYPE (expr),
23875 TREE_OPERAND (expr, 0),
23876 (TREE_OPERAND (expr, 1)
23877 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
23878 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
23879 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
23880 if (TREE_CODE (expr) == COMPOUND_EXPR
23881 && !COMPOUND_EXPR_OVERLOADED (expr))
23882 return build2 (COMPOUND_EXPR,
23883 TREE_TYPE (expr),
23884 TREE_OPERAND (expr, 0),
23885 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
23886
23887 /* If the type is unknown, it can't really be non-dependent */
23888 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
23889
23890 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23891 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
23892 }
23893
23894 /* ARGS is a vector of expressions as arguments to a function call.
23895 Replace the arguments with equivalent non-dependent expressions.
23896 This modifies ARGS in place. */
23897
23898 void
23899 make_args_non_dependent (vec<tree, va_gc> *args)
23900 {
23901 unsigned int ix;
23902 tree arg;
23903
23904 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
23905 {
23906 tree newarg = build_non_dependent_expr (arg);
23907 if (newarg != arg)
23908 (*args)[ix] = newarg;
23909 }
23910 }
23911
23912 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23913 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23914 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23915
23916 static tree
23917 make_auto_1 (tree name, bool set_canonical)
23918 {
23919 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
23920 TYPE_NAME (au) = build_decl (input_location,
23921 TYPE_DECL, name, au);
23922 TYPE_STUB_DECL (au) = TYPE_NAME (au);
23923 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
23924 (0, processing_template_decl + 1, processing_template_decl + 1,
23925 TYPE_NAME (au), NULL_TREE);
23926 if (set_canonical)
23927 TYPE_CANONICAL (au) = canonical_type_parameter (au);
23928 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
23929 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
23930
23931 return au;
23932 }
23933
23934 tree
23935 make_decltype_auto (void)
23936 {
23937 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23938 }
23939
23940 tree
23941 make_auto (void)
23942 {
23943 return make_auto_1 (get_identifier ("auto"), true);
23944 }
23945
23946 /* Make a "constrained auto" type-specifier. This is an
23947 auto type with constraints that must be associated after
23948 deduction. The constraint is formed from the given
23949 CONC and its optional sequence of arguments, which are
23950 non-null if written as partial-concept-id. */
23951
23952 tree
23953 make_constrained_auto (tree con, tree args)
23954 {
23955 tree type = make_auto_1 (get_identifier ("auto"), false);
23956
23957 /* Build the constraint. */
23958 tree tmpl = DECL_TI_TEMPLATE (con);
23959 tree expr;
23960 if (VAR_P (con))
23961 expr = build_concept_check (tmpl, type, args);
23962 else
23963 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
23964
23965 tree constr = normalize_expression (expr);
23966 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
23967
23968 /* Our canonical type depends on the constraint. */
23969 TYPE_CANONICAL (type) = canonical_type_parameter (type);
23970
23971 /* Attach the constraint to the type declaration. */
23972 tree decl = TYPE_NAME (type);
23973 return decl;
23974 }
23975
23976 /* Given type ARG, return std::initializer_list<ARG>. */
23977
23978 static tree
23979 listify (tree arg)
23980 {
23981 tree std_init_list = namespace_binding
23982 (get_identifier ("initializer_list"), std_node);
23983 tree argvec;
23984 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
23985 {
23986 error ("deducing from brace-enclosed initializer list requires "
23987 "#include <initializer_list>");
23988 return error_mark_node;
23989 }
23990 argvec = make_tree_vec (1);
23991 TREE_VEC_ELT (argvec, 0) = arg;
23992 return lookup_template_class (std_init_list, argvec, NULL_TREE,
23993 NULL_TREE, 0, tf_warning_or_error);
23994 }
23995
23996 /* Replace auto in TYPE with std::initializer_list<auto>. */
23997
23998 static tree
23999 listify_autos (tree type, tree auto_node)
24000 {
24001 tree init_auto = listify (auto_node);
24002 tree argvec = make_tree_vec (1);
24003 TREE_VEC_ELT (argvec, 0) = init_auto;
24004 if (processing_template_decl)
24005 argvec = add_to_template_args (current_template_args (), argvec);
24006 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24007 }
24008
24009 /* Hash traits for hashing possibly constrained 'auto'
24010 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24011
24012 struct auto_hash : default_hash_traits<tree>
24013 {
24014 static inline hashval_t hash (tree);
24015 static inline bool equal (tree, tree);
24016 };
24017
24018 /* Hash the 'auto' T. */
24019
24020 inline hashval_t
24021 auto_hash::hash (tree t)
24022 {
24023 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24024 /* Matching constrained-type-specifiers denote the same template
24025 parameter, so hash the constraint. */
24026 return hash_placeholder_constraint (c);
24027 else
24028 /* But unconstrained autos are all separate, so just hash the pointer. */
24029 return iterative_hash_object (t, 0);
24030 }
24031
24032 /* Compare two 'auto's. */
24033
24034 inline bool
24035 auto_hash::equal (tree t1, tree t2)
24036 {
24037 if (t1 == t2)
24038 return true;
24039
24040 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24041 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24042
24043 /* Two unconstrained autos are distinct. */
24044 if (!c1 || !c2)
24045 return false;
24046
24047 return equivalent_placeholder_constraints (c1, c2);
24048 }
24049
24050 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24051 constrained) auto, add it to the vector. */
24052
24053 static int
24054 extract_autos_r (tree t, void *data)
24055 {
24056 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24057 if (is_auto_or_concept (t))
24058 {
24059 /* All the autos were built with index 0; fix that up now. */
24060 tree *p = hash.find_slot (t, INSERT);
24061 unsigned idx;
24062 if (*p)
24063 /* If this is a repeated constrained-type-specifier, use the index we
24064 chose before. */
24065 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24066 else
24067 {
24068 /* Otherwise this is new, so use the current count. */
24069 *p = t;
24070 idx = hash.elements () - 1;
24071 }
24072 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24073 }
24074
24075 /* Always keep walking. */
24076 return 0;
24077 }
24078
24079 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24080 says they can appear anywhere in the type. */
24081
24082 static tree
24083 extract_autos (tree type)
24084 {
24085 hash_set<tree> visited;
24086 hash_table<auto_hash> hash (2);
24087
24088 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24089
24090 tree tree_vec = make_tree_vec (hash.elements());
24091 for (hash_table<auto_hash>::iterator iter = hash.begin();
24092 iter != hash.end(); ++iter)
24093 {
24094 tree elt = *iter;
24095 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24096 TREE_VEC_ELT (tree_vec, i)
24097 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24098 }
24099
24100 return tree_vec;
24101 }
24102
24103 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24104 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
24105
24106 tree
24107 do_auto_deduction (tree type, tree init, tree auto_node)
24108 {
24109 return do_auto_deduction (type, init, auto_node,
24110 tf_warning_or_error,
24111 adc_unspecified);
24112 }
24113
24114 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24115 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
24116 The CONTEXT determines the context in which auto deduction is performed
24117 and is used to control error diagnostics.
24118
24119 For partial-concept-ids, extra args may be appended to the list of deduced
24120 template arguments prior to determining constraint satisfaction. */
24121
24122 tree
24123 do_auto_deduction (tree type, tree init, tree auto_node,
24124 tsubst_flags_t complain, auto_deduction_context context)
24125 {
24126 tree targs;
24127
24128 if (init == error_mark_node)
24129 return error_mark_node;
24130
24131 if (type_dependent_expression_p (init))
24132 /* Defining a subset of type-dependent expressions that we can deduce
24133 from ahead of time isn't worth the trouble. */
24134 return type;
24135
24136 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
24137 with either a new invented type template parameter U or, if the
24138 initializer is a braced-init-list (8.5.4), with
24139 std::initializer_list<U>. */
24140 if (BRACE_ENCLOSED_INITIALIZER_P (init))
24141 {
24142 if (!DIRECT_LIST_INIT_P (init))
24143 type = listify_autos (type, auto_node);
24144 else if (CONSTRUCTOR_NELTS (init) == 1)
24145 init = CONSTRUCTOR_ELT (init, 0)->value;
24146 else
24147 {
24148 if (complain & tf_warning_or_error)
24149 {
24150 if (permerror (input_location, "direct-list-initialization of "
24151 "%<auto%> requires exactly one element"))
24152 inform (input_location,
24153 "for deduction to %<std::initializer_list%>, use copy-"
24154 "list-initialization (i.e. add %<=%> before the %<{%>)");
24155 }
24156 type = listify_autos (type, auto_node);
24157 }
24158 }
24159
24160 if (type == error_mark_node)
24161 return error_mark_node;
24162
24163 init = resolve_nondeduced_context (init, complain);
24164
24165 if (AUTO_IS_DECLTYPE (auto_node))
24166 {
24167 bool id = (DECL_P (init)
24168 || ((TREE_CODE (init) == COMPONENT_REF
24169 || TREE_CODE (init) == SCOPE_REF)
24170 && !REF_PARENTHESIZED_P (init)));
24171 targs = make_tree_vec (1);
24172 TREE_VEC_ELT (targs, 0)
24173 = finish_decltype_type (init, id, tf_warning_or_error);
24174 if (type != auto_node)
24175 {
24176 if (complain & tf_error)
24177 error ("%qT as type rather than plain %<decltype(auto)%>", type);
24178 return error_mark_node;
24179 }
24180 }
24181 else
24182 {
24183 tree parms = build_tree_list (NULL_TREE, type);
24184 tree tparms;
24185
24186 if (flag_concepts)
24187 tparms = extract_autos (type);
24188 else
24189 {
24190 tparms = make_tree_vec (1);
24191 TREE_VEC_ELT (tparms, 0)
24192 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
24193 }
24194
24195 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
24196 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
24197 DEDUCE_CALL, LOOKUP_NORMAL,
24198 NULL, /*explain_p=*/false);
24199 if (val > 0)
24200 {
24201 if (processing_template_decl)
24202 /* Try again at instantiation time. */
24203 return type;
24204 if (type && type != error_mark_node
24205 && (complain & tf_error))
24206 /* If type is error_mark_node a diagnostic must have been
24207 emitted by now. Also, having a mention to '<type error>'
24208 in the diagnostic is not really useful to the user. */
24209 {
24210 if (cfun && auto_node == current_function_auto_return_pattern
24211 && LAMBDA_FUNCTION_P (current_function_decl))
24212 error ("unable to deduce lambda return type from %qE", init);
24213 else
24214 error ("unable to deduce %qT from %qE", type, init);
24215 type_unification_real (tparms, targs, parms, &init, 1, 0,
24216 DEDUCE_CALL, LOOKUP_NORMAL,
24217 NULL, /*explain_p=*/true);
24218 }
24219 return error_mark_node;
24220 }
24221 }
24222
24223 /* Check any placeholder constraints against the deduced type. */
24224 if (flag_concepts && !processing_template_decl)
24225 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
24226 {
24227 /* Use the deduced type to check the associated constraints. If we
24228 have a partial-concept-id, rebuild the argument list so that
24229 we check using the extra arguments. */
24230 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
24231 tree cargs = CHECK_CONSTR_ARGS (constr);
24232 if (TREE_VEC_LENGTH (cargs) > 1)
24233 {
24234 cargs = copy_node (cargs);
24235 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
24236 }
24237 else
24238 cargs = targs;
24239 if (!constraints_satisfied_p (constr, cargs))
24240 {
24241 if (complain & tf_warning_or_error)
24242 {
24243 switch (context)
24244 {
24245 case adc_unspecified:
24246 error("placeholder constraints not satisfied");
24247 break;
24248 case adc_variable_type:
24249 error ("deduced initializer does not satisfy "
24250 "placeholder constraints");
24251 break;
24252 case adc_return_type:
24253 error ("deduced return type does not satisfy "
24254 "placeholder constraints");
24255 break;
24256 case adc_requirement:
24257 error ("deduced expression type does not saatisy "
24258 "placeholder constraints");
24259 break;
24260 }
24261 diagnose_constraints (input_location, constr, targs);
24262 }
24263 return error_mark_node;
24264 }
24265 }
24266
24267 if (processing_template_decl)
24268 targs = add_to_template_args (current_template_args (), targs);
24269 return tsubst (type, targs, complain, NULL_TREE);
24270 }
24271
24272 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
24273 result. */
24274
24275 tree
24276 splice_late_return_type (tree type, tree late_return_type)
24277 {
24278 if (is_auto (type))
24279 {
24280 if (late_return_type)
24281 return late_return_type;
24282
24283 tree idx = get_template_parm_index (type);
24284 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
24285 /* In an abbreviated function template we didn't know we were dealing
24286 with a function template when we saw the auto return type, so update
24287 it to have the correct level. */
24288 return make_auto_1 (TYPE_IDENTIFIER (type), true);
24289 }
24290 return type;
24291 }
24292
24293 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
24294 'decltype(auto)'. */
24295
24296 bool
24297 is_auto (const_tree type)
24298 {
24299 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24300 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
24301 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
24302 return true;
24303 else
24304 return false;
24305 }
24306
24307 /* for_each_template_parm callback for type_uses_auto. */
24308
24309 int
24310 is_auto_r (tree tp, void */*data*/)
24311 {
24312 return is_auto_or_concept (tp);
24313 }
24314
24315 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
24316 a use of `auto'. Returns NULL_TREE otherwise. */
24317
24318 tree
24319 type_uses_auto (tree type)
24320 {
24321 if (type == NULL_TREE)
24322 return NULL_TREE;
24323 else if (flag_concepts)
24324 {
24325 /* The Concepts TS allows multiple autos in one type-specifier; just
24326 return the first one we find, do_auto_deduction will collect all of
24327 them. */
24328 if (uses_template_parms (type))
24329 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
24330 /*visited*/NULL, /*nondeduced*/true);
24331 else
24332 return NULL_TREE;
24333 }
24334 else
24335 return find_type_usage (type, is_auto);
24336 }
24337
24338 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
24339 'decltype(auto)' or a concept. */
24340
24341 bool
24342 is_auto_or_concept (const_tree type)
24343 {
24344 return is_auto (type); // or concept
24345 }
24346
24347 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
24348 a concept identifier) iff TYPE contains a use of a generic type. Returns
24349 NULL_TREE otherwise. */
24350
24351 tree
24352 type_uses_auto_or_concept (tree type)
24353 {
24354 return find_type_usage (type, is_auto_or_concept);
24355 }
24356
24357
24358 /* For a given template T, return the vector of typedefs referenced
24359 in T for which access check is needed at T instantiation time.
24360 T is either a FUNCTION_DECL or a RECORD_TYPE.
24361 Those typedefs were added to T by the function
24362 append_type_to_template_for_access_check. */
24363
24364 vec<qualified_typedef_usage_t, va_gc> *
24365 get_types_needing_access_check (tree t)
24366 {
24367 tree ti;
24368 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
24369
24370 if (!t || t == error_mark_node)
24371 return NULL;
24372
24373 if (!(ti = get_template_info (t)))
24374 return NULL;
24375
24376 if (CLASS_TYPE_P (t)
24377 || TREE_CODE (t) == FUNCTION_DECL)
24378 {
24379 if (!TI_TEMPLATE (ti))
24380 return NULL;
24381
24382 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
24383 }
24384
24385 return result;
24386 }
24387
24388 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
24389 tied to T. That list of typedefs will be access checked at
24390 T instantiation time.
24391 T is either a FUNCTION_DECL or a RECORD_TYPE.
24392 TYPE_DECL is a TYPE_DECL node representing a typedef.
24393 SCOPE is the scope through which TYPE_DECL is accessed.
24394 LOCATION is the location of the usage point of TYPE_DECL.
24395
24396 This function is a subroutine of
24397 append_type_to_template_for_access_check. */
24398
24399 static void
24400 append_type_to_template_for_access_check_1 (tree t,
24401 tree type_decl,
24402 tree scope,
24403 location_t location)
24404 {
24405 qualified_typedef_usage_t typedef_usage;
24406 tree ti;
24407
24408 if (!t || t == error_mark_node)
24409 return;
24410
24411 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
24412 || CLASS_TYPE_P (t))
24413 && type_decl
24414 && TREE_CODE (type_decl) == TYPE_DECL
24415 && scope);
24416
24417 if (!(ti = get_template_info (t)))
24418 return;
24419
24420 gcc_assert (TI_TEMPLATE (ti));
24421
24422 typedef_usage.typedef_decl = type_decl;
24423 typedef_usage.context = scope;
24424 typedef_usage.locus = location;
24425
24426 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
24427 }
24428
24429 /* Append TYPE_DECL to the template TEMPL.
24430 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24431 At TEMPL instanciation time, TYPE_DECL will be checked to see
24432 if it can be accessed through SCOPE.
24433 LOCATION is the location of the usage point of TYPE_DECL.
24434
24435 e.g. consider the following code snippet:
24436
24437 class C
24438 {
24439 typedef int myint;
24440 };
24441
24442 template<class U> struct S
24443 {
24444 C::myint mi; // <-- usage point of the typedef C::myint
24445 };
24446
24447 S<char> s;
24448
24449 At S<char> instantiation time, we need to check the access of C::myint
24450 In other words, we need to check the access of the myint typedef through
24451 the C scope. For that purpose, this function will add the myint typedef
24452 and the scope C through which its being accessed to a list of typedefs
24453 tied to the template S. That list will be walked at template instantiation
24454 time and access check performed on each typedefs it contains.
24455 Note that this particular code snippet should yield an error because
24456 myint is private to C. */
24457
24458 void
24459 append_type_to_template_for_access_check (tree templ,
24460 tree type_decl,
24461 tree scope,
24462 location_t location)
24463 {
24464 qualified_typedef_usage_t *iter;
24465 unsigned i;
24466
24467 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
24468
24469 /* Make sure we don't append the type to the template twice. */
24470 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
24471 if (iter->typedef_decl == type_decl && scope == iter->context)
24472 return;
24473
24474 append_type_to_template_for_access_check_1 (templ, type_decl,
24475 scope, location);
24476 }
24477
24478 /* Convert the generic type parameters in PARM that match the types given in the
24479 range [START_IDX, END_IDX) from the current_template_parms into generic type
24480 packs. */
24481
24482 tree
24483 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
24484 {
24485 tree current = current_template_parms;
24486 int depth = TMPL_PARMS_DEPTH (current);
24487 current = INNERMOST_TEMPLATE_PARMS (current);
24488 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
24489
24490 for (int i = 0; i < start_idx; ++i)
24491 TREE_VEC_ELT (replacement, i)
24492 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24493
24494 for (int i = start_idx; i < end_idx; ++i)
24495 {
24496 /* Create a distinct parameter pack type from the current parm and add it
24497 to the replacement args to tsubst below into the generic function
24498 parameter. */
24499
24500 tree o = TREE_TYPE (TREE_VALUE
24501 (TREE_VEC_ELT (current, i)));
24502 tree t = copy_type (o);
24503 TEMPLATE_TYPE_PARM_INDEX (t)
24504 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
24505 o, 0, 0, tf_none);
24506 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
24507 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
24508 TYPE_MAIN_VARIANT (t) = t;
24509 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
24510 TYPE_CANONICAL (t) = canonical_type_parameter (t);
24511 TREE_VEC_ELT (replacement, i) = t;
24512 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
24513 }
24514
24515 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
24516 TREE_VEC_ELT (replacement, i)
24517 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24518
24519 /* If there are more levels then build up the replacement with the outer
24520 template parms. */
24521 if (depth > 1)
24522 replacement = add_to_template_args (template_parms_to_args
24523 (TREE_CHAIN (current_template_parms)),
24524 replacement);
24525
24526 return tsubst (parm, replacement, tf_none, NULL_TREE);
24527 }
24528
24529 /* Entries in the decl_constraint hash table. */
24530 struct GTY((for_user)) constr_entry
24531 {
24532 tree decl;
24533 tree ci;
24534 };
24535
24536 /* Hashing function and equality for constraint entries. */
24537 struct constr_hasher : ggc_ptr_hash<constr_entry>
24538 {
24539 static hashval_t hash (constr_entry *e)
24540 {
24541 return (hashval_t)DECL_UID (e->decl);
24542 }
24543
24544 static bool equal (constr_entry *e1, constr_entry *e2)
24545 {
24546 return e1->decl == e2->decl;
24547 }
24548 };
24549
24550 /* A mapping from declarations to constraint information. Note that
24551 both templates and their underlying declarations are mapped to the
24552 same constraint information.
24553
24554 FIXME: This is defined in pt.c because garbage collection
24555 code is not being generated for constraint.cc. */
24556
24557 static GTY (()) hash_table<constr_hasher> *decl_constraints;
24558
24559 /* Returns the template constraints of declaration T. If T is not
24560 constrained, return NULL_TREE. Note that T must be non-null. */
24561
24562 tree
24563 get_constraints (tree t)
24564 {
24565 if (!flag_concepts)
24566 return NULL_TREE;
24567
24568 gcc_assert (DECL_P (t));
24569 if (TREE_CODE (t) == TEMPLATE_DECL)
24570 t = DECL_TEMPLATE_RESULT (t);
24571 constr_entry elt = { t, NULL_TREE };
24572 constr_entry* found = decl_constraints->find (&elt);
24573 if (found)
24574 return found->ci;
24575 else
24576 return NULL_TREE;
24577 }
24578
24579 /* Associate the given constraint information CI with the declaration
24580 T. If T is a template, then the constraints are associated with
24581 its underlying declaration. Don't build associations if CI is
24582 NULL_TREE. */
24583
24584 void
24585 set_constraints (tree t, tree ci)
24586 {
24587 if (!ci)
24588 return;
24589 gcc_assert (t && flag_concepts);
24590 if (TREE_CODE (t) == TEMPLATE_DECL)
24591 t = DECL_TEMPLATE_RESULT (t);
24592 gcc_assert (!get_constraints (t));
24593 constr_entry elt = {t, ci};
24594 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
24595 constr_entry* entry = ggc_alloc<constr_entry> ();
24596 *entry = elt;
24597 *slot = entry;
24598 }
24599
24600 /* Remove the associated constraints of the declaration T. */
24601
24602 void
24603 remove_constraints (tree t)
24604 {
24605 gcc_assert (DECL_P (t));
24606 if (TREE_CODE (t) == TEMPLATE_DECL)
24607 t = DECL_TEMPLATE_RESULT (t);
24608
24609 constr_entry elt = {t, NULL_TREE};
24610 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
24611 if (slot)
24612 decl_constraints->clear_slot (slot);
24613 }
24614
24615 /* Memoized satisfaction results for declarations. This
24616 maps the pair (constraint_info, arguments) to the result computed
24617 by constraints_satisfied_p. */
24618
24619 struct GTY((for_user)) constraint_sat_entry
24620 {
24621 tree ci;
24622 tree args;
24623 tree result;
24624 };
24625
24626 /* Hashing function and equality for constraint entries. */
24627
24628 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
24629 {
24630 static hashval_t hash (constraint_sat_entry *e)
24631 {
24632 hashval_t val = iterative_hash_object(e->ci, 0);
24633 return iterative_hash_template_arg (e->args, val);
24634 }
24635
24636 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
24637 {
24638 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
24639 }
24640 };
24641
24642 /* Memoized satisfaction results for concept checks. */
24643
24644 struct GTY((for_user)) concept_spec_entry
24645 {
24646 tree tmpl;
24647 tree args;
24648 tree result;
24649 };
24650
24651 /* Hashing function and equality for constraint entries. */
24652
24653 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
24654 {
24655 static hashval_t hash (concept_spec_entry *e)
24656 {
24657 return hash_tmpl_and_args (e->tmpl, e->args);
24658 }
24659
24660 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
24661 {
24662 ++comparing_specializations;
24663 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
24664 --comparing_specializations;
24665 return eq;
24666 }
24667 };
24668
24669 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
24670 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
24671
24672 /* Search for a memoized satisfaction result. Returns one of the
24673 truth value nodes if previously memoized, or NULL_TREE otherwise. */
24674
24675 tree
24676 lookup_constraint_satisfaction (tree ci, tree args)
24677 {
24678 constraint_sat_entry elt = { ci, args, NULL_TREE };
24679 constraint_sat_entry* found = constraint_memos->find (&elt);
24680 if (found)
24681 return found->result;
24682 else
24683 return NULL_TREE;
24684 }
24685
24686 /* Memoize the result of a satisfication test. Returns the saved result. */
24687
24688 tree
24689 memoize_constraint_satisfaction (tree ci, tree args, tree result)
24690 {
24691 constraint_sat_entry elt = {ci, args, result};
24692 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
24693 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
24694 *entry = elt;
24695 *slot = entry;
24696 return result;
24697 }
24698
24699 /* Search for a memoized satisfaction result for a concept. */
24700
24701 tree
24702 lookup_concept_satisfaction (tree tmpl, tree args)
24703 {
24704 concept_spec_entry elt = { tmpl, args, NULL_TREE };
24705 concept_spec_entry* found = concept_memos->find (&elt);
24706 if (found)
24707 return found->result;
24708 else
24709 return NULL_TREE;
24710 }
24711
24712 /* Memoize the result of a concept check. Returns the saved result. */
24713
24714 tree
24715 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
24716 {
24717 concept_spec_entry elt = {tmpl, args, result};
24718 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
24719 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
24720 *entry = elt;
24721 *slot = entry;
24722 return result;
24723 }
24724
24725 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
24726
24727 /* Returns a prior concept specialization. This returns the substituted
24728 and normalized constraints defined by the concept. */
24729
24730 tree
24731 get_concept_expansion (tree tmpl, tree args)
24732 {
24733 concept_spec_entry elt = { tmpl, args, NULL_TREE };
24734 concept_spec_entry* found = concept_expansions->find (&elt);
24735 if (found)
24736 return found->result;
24737 else
24738 return NULL_TREE;
24739 }
24740
24741 /* Save a concept expansion for later. */
24742
24743 tree
24744 save_concept_expansion (tree tmpl, tree args, tree def)
24745 {
24746 concept_spec_entry elt = {tmpl, args, def};
24747 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
24748 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
24749 *entry = elt;
24750 *slot = entry;
24751 return def;
24752 }
24753
24754 static hashval_t
24755 hash_subsumption_args (tree t1, tree t2)
24756 {
24757 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
24758 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
24759 int val = 0;
24760 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
24761 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
24762 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
24763 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
24764 return val;
24765 }
24766
24767 /* Compare the constraints of two subsumption entries. The LEFT1 and
24768 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
24769 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
24770
24771 static bool
24772 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
24773 {
24774 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
24775 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
24776 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
24777 CHECK_CONSTR_ARGS (right1)))
24778 return comp_template_args (CHECK_CONSTR_ARGS (left2),
24779 CHECK_CONSTR_ARGS (right2));
24780 return false;
24781 }
24782
24783 /* Key/value pair for learning and memoizing subsumption results. This
24784 associates a pair of check constraints (including arguments) with
24785 a boolean value indicating the result. */
24786
24787 struct GTY((for_user)) subsumption_entry
24788 {
24789 tree t1;
24790 tree t2;
24791 bool result;
24792 };
24793
24794 /* Hashing function and equality for constraint entries. */
24795
24796 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
24797 {
24798 static hashval_t hash (subsumption_entry *e)
24799 {
24800 return hash_subsumption_args (e->t1, e->t2);
24801 }
24802
24803 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
24804 {
24805 ++comparing_specializations;
24806 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
24807 --comparing_specializations;
24808 return eq;
24809 }
24810 };
24811
24812 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
24813
24814 /* Search for a previously cached subsumption result. */
24815
24816 bool*
24817 lookup_subsumption_result (tree t1, tree t2)
24818 {
24819 subsumption_entry elt = { t1, t2, false };
24820 subsumption_entry* found = subsumption_table->find (&elt);
24821 if (found)
24822 return &found->result;
24823 else
24824 return 0;
24825 }
24826
24827 /* Save a subsumption result. */
24828
24829 bool
24830 save_subsumption_result (tree t1, tree t2, bool result)
24831 {
24832 subsumption_entry elt = {t1, t2, result};
24833 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
24834 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
24835 *entry = elt;
24836 *slot = entry;
24837 return result;
24838 }
24839
24840 /* Set up the hash table for constraint association. */
24841
24842 void
24843 init_constraint_processing (void)
24844 {
24845 if (!flag_concepts)
24846 return;
24847
24848 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
24849 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
24850 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
24851 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
24852 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
24853 }
24854
24855 /* Set up the hash tables for template instantiations. */
24856
24857 void
24858 init_template_processing (void)
24859 {
24860 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
24861 type_specializations = hash_table<spec_hasher>::create_ggc (37);
24862 }
24863
24864 /* Print stats about the template hash tables for -fstats. */
24865
24866 void
24867 print_template_statistics (void)
24868 {
24869 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
24870 "%f collisions\n", (long) decl_specializations->size (),
24871 (long) decl_specializations->elements (),
24872 decl_specializations->collisions ());
24873 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
24874 "%f collisions\n", (long) type_specializations->size (),
24875 (long) type_specializations->elements (),
24876 type_specializations->collisions ());
24877 }
24878
24879 #include "gt-cp-pt.h"