re PR c++/68965 (`-Wunused-parameter` is reported in variadic lambda or function...
[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, 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 tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
182 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
184 static void regenerate_decl_from_template (tree, tree);
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 int template_args_equal (tree, tree);
199 static void tsubst_default_arguments (tree, tsubst_flags_t);
200 static tree for_each_template_parm_r (tree *, int *, void *);
201 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
202 static void copy_default_args_to_explicit_spec (tree);
203 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
204 static bool dependent_template_arg_p (tree);
205 static bool any_template_arguments_need_structural_equality_p (tree);
206 static bool dependent_type_p_r (tree);
207 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
208 static tree tsubst_decl (tree, tree, tsubst_flags_t);
209 static void perform_typedefs_access_check (tree tmpl, tree targs);
210 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
211 location_t);
212 static tree listify (tree);
213 static tree listify_autos (tree, tree);
214 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
215 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
216 static bool complex_alias_template_p (const_tree tmpl);
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 return NULL;
333
334 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
335 tinfo = DECL_TEMPLATE_INFO (t);
336
337 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
338 t = TREE_TYPE (t);
339
340 if (OVERLOAD_TYPE_P (t))
341 tinfo = TYPE_TEMPLATE_INFO (t);
342 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
343 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
344
345 return tinfo;
346 }
347
348 /* Returns the template nesting level of the indicated class TYPE.
349
350 For example, in:
351 template <class T>
352 struct A
353 {
354 template <class U>
355 struct B {};
356 };
357
358 A<T>::B<U> has depth two, while A<T> has depth one.
359 Both A<T>::B<int> and A<int>::B<U> have depth one, if
360 they are instantiations, not specializations.
361
362 This function is guaranteed to return 0 if passed NULL_TREE so
363 that, for example, `template_class_depth (current_class_type)' is
364 always safe. */
365
366 int
367 template_class_depth (tree type)
368 {
369 int depth;
370
371 for (depth = 0;
372 type && TREE_CODE (type) != NAMESPACE_DECL;
373 type = (TREE_CODE (type) == FUNCTION_DECL)
374 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
375 {
376 tree tinfo = get_template_info (type);
377
378 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
379 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
380 ++depth;
381 }
382
383 return depth;
384 }
385
386 /* Subroutine of maybe_begin_member_template_processing.
387 Returns true if processing DECL needs us to push template parms. */
388
389 static bool
390 inline_needs_template_parms (tree decl, bool nsdmi)
391 {
392 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
393 return false;
394
395 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
396 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
397 }
398
399 /* Subroutine of maybe_begin_member_template_processing.
400 Push the template parms in PARMS, starting from LEVELS steps into the
401 chain, and ending at the beginning, since template parms are listed
402 innermost first. */
403
404 static void
405 push_inline_template_parms_recursive (tree parmlist, int levels)
406 {
407 tree parms = TREE_VALUE (parmlist);
408 int i;
409
410 if (levels > 1)
411 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
412
413 ++processing_template_decl;
414 current_template_parms
415 = tree_cons (size_int (processing_template_decl),
416 parms, current_template_parms);
417 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
418
419 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
420 NULL);
421 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
422 {
423 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
424
425 if (error_operand_p (parm))
426 continue;
427
428 gcc_assert (DECL_P (parm));
429
430 switch (TREE_CODE (parm))
431 {
432 case TYPE_DECL:
433 case TEMPLATE_DECL:
434 pushdecl (parm);
435 break;
436
437 case PARM_DECL:
438 /* Push the CONST_DECL. */
439 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
440 break;
441
442 default:
443 gcc_unreachable ();
444 }
445 }
446 }
447
448 /* Restore the template parameter context for a member template, a
449 friend template defined in a class definition, or a non-template
450 member of template class. */
451
452 void
453 maybe_begin_member_template_processing (tree decl)
454 {
455 tree parms;
456 int levels = 0;
457 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
458
459 if (nsdmi)
460 {
461 tree ctx = DECL_CONTEXT (decl);
462 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
463 /* Disregard full specializations (c++/60999). */
464 && uses_template_parms (ctx)
465 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
466 }
467
468 if (inline_needs_template_parms (decl, nsdmi))
469 {
470 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
471 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
472
473 if (DECL_TEMPLATE_SPECIALIZATION (decl))
474 {
475 --levels;
476 parms = TREE_CHAIN (parms);
477 }
478
479 push_inline_template_parms_recursive (parms, levels);
480 }
481
482 /* Remember how many levels of template parameters we pushed so that
483 we can pop them later. */
484 inline_parm_levels.safe_push (levels);
485 }
486
487 /* Undo the effects of maybe_begin_member_template_processing. */
488
489 void
490 maybe_end_member_template_processing (void)
491 {
492 int i;
493 int last;
494
495 if (inline_parm_levels.length () == 0)
496 return;
497
498 last = inline_parm_levels.pop ();
499 for (i = 0; i < last; ++i)
500 {
501 --processing_template_decl;
502 current_template_parms = TREE_CHAIN (current_template_parms);
503 poplevel (0, 0, 0);
504 }
505 }
506
507 /* Return a new template argument vector which contains all of ARGS,
508 but has as its innermost set of arguments the EXTRA_ARGS. */
509
510 static tree
511 add_to_template_args (tree args, tree extra_args)
512 {
513 tree new_args;
514 int extra_depth;
515 int i;
516 int j;
517
518 if (args == NULL_TREE || extra_args == error_mark_node)
519 return extra_args;
520
521 extra_depth = TMPL_ARGS_DEPTH (extra_args);
522 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
523
524 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
525 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
526
527 for (j = 1; j <= extra_depth; ++j, ++i)
528 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
529
530 return new_args;
531 }
532
533 /* Like add_to_template_args, but only the outermost ARGS are added to
534 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
535 (EXTRA_ARGS) levels are added. This function is used to combine
536 the template arguments from a partial instantiation with the
537 template arguments used to attain the full instantiation from the
538 partial instantiation. */
539
540 static tree
541 add_outermost_template_args (tree args, tree extra_args)
542 {
543 tree new_args;
544
545 /* If there are more levels of EXTRA_ARGS than there are ARGS,
546 something very fishy is going on. */
547 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
548
549 /* If *all* the new arguments will be the EXTRA_ARGS, just return
550 them. */
551 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
552 return extra_args;
553
554 /* For the moment, we make ARGS look like it contains fewer levels. */
555 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
556
557 new_args = add_to_template_args (args, extra_args);
558
559 /* Now, we restore ARGS to its full dimensions. */
560 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
561
562 return new_args;
563 }
564
565 /* Return the N levels of innermost template arguments from the ARGS. */
566
567 tree
568 get_innermost_template_args (tree args, int n)
569 {
570 tree new_args;
571 int extra_levels;
572 int i;
573
574 gcc_assert (n >= 0);
575
576 /* If N is 1, just return the innermost set of template arguments. */
577 if (n == 1)
578 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
579
580 /* If we're not removing anything, just return the arguments we were
581 given. */
582 extra_levels = TMPL_ARGS_DEPTH (args) - n;
583 gcc_assert (extra_levels >= 0);
584 if (extra_levels == 0)
585 return args;
586
587 /* Make a new set of arguments, not containing the outer arguments. */
588 new_args = make_tree_vec (n);
589 for (i = 1; i <= n; ++i)
590 SET_TMPL_ARGS_LEVEL (new_args, i,
591 TMPL_ARGS_LEVEL (args, i + extra_levels));
592
593 return new_args;
594 }
595
596 /* The inverse of get_innermost_template_args: Return all but the innermost
597 EXTRA_LEVELS levels of template arguments from the ARGS. */
598
599 static tree
600 strip_innermost_template_args (tree args, int extra_levels)
601 {
602 tree new_args;
603 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
604 int i;
605
606 gcc_assert (n >= 0);
607
608 /* If N is 1, just return the outermost set of template arguments. */
609 if (n == 1)
610 return TMPL_ARGS_LEVEL (args, 1);
611
612 /* If we're not removing anything, just return the arguments we were
613 given. */
614 gcc_assert (extra_levels >= 0);
615 if (extra_levels == 0)
616 return args;
617
618 /* Make a new set of arguments, not containing the inner arguments. */
619 new_args = make_tree_vec (n);
620 for (i = 1; i <= n; ++i)
621 SET_TMPL_ARGS_LEVEL (new_args, i,
622 TMPL_ARGS_LEVEL (args, i));
623
624 return new_args;
625 }
626
627 /* We've got a template header coming up; push to a new level for storing
628 the parms. */
629
630 void
631 begin_template_parm_list (void)
632 {
633 /* We use a non-tag-transparent scope here, which causes pushtag to
634 put tags in this scope, rather than in the enclosing class or
635 namespace scope. This is the right thing, since we want
636 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
637 global template class, push_template_decl handles putting the
638 TEMPLATE_DECL into top-level scope. For a nested template class,
639 e.g.:
640
641 template <class T> struct S1 {
642 template <class T> struct S2 {};
643 };
644
645 pushtag contains special code to call pushdecl_with_scope on the
646 TEMPLATE_DECL for S2. */
647 begin_scope (sk_template_parms, NULL);
648 ++processing_template_decl;
649 ++processing_template_parmlist;
650 note_template_header (0);
651
652 /* Add a dummy parameter level while we process the parameter list. */
653 current_template_parms
654 = tree_cons (size_int (processing_template_decl),
655 make_tree_vec (0),
656 current_template_parms);
657 }
658
659 /* This routine is called when a specialization is declared. If it is
660 invalid to declare a specialization here, an error is reported and
661 false is returned, otherwise this routine will return true. */
662
663 static bool
664 check_specialization_scope (void)
665 {
666 tree scope = current_scope ();
667
668 /* [temp.expl.spec]
669
670 An explicit specialization shall be declared in the namespace of
671 which the template is a member, or, for member templates, in the
672 namespace of which the enclosing class or enclosing class
673 template is a member. An explicit specialization of a member
674 function, member class or static data member of a class template
675 shall be declared in the namespace of which the class template
676 is a member. */
677 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
678 {
679 error ("explicit specialization in non-namespace scope %qD", scope);
680 return false;
681 }
682
683 /* [temp.expl.spec]
684
685 In an explicit specialization declaration for a member of a class
686 template or a member template that appears in namespace scope,
687 the member template and some of its enclosing class templates may
688 remain unspecialized, except that the declaration shall not
689 explicitly specialize a class member template if its enclosing
690 class templates are not explicitly specialized as well. */
691 if (current_template_parms)
692 {
693 error ("enclosing class templates are not explicitly specialized");
694 return false;
695 }
696
697 return true;
698 }
699
700 /* We've just seen template <>. */
701
702 bool
703 begin_specialization (void)
704 {
705 begin_scope (sk_template_spec, NULL);
706 note_template_header (1);
707 return check_specialization_scope ();
708 }
709
710 /* Called at then end of processing a declaration preceded by
711 template<>. */
712
713 void
714 end_specialization (void)
715 {
716 finish_scope ();
717 reset_specialization ();
718 }
719
720 /* Any template <>'s that we have seen thus far are not referring to a
721 function specialization. */
722
723 void
724 reset_specialization (void)
725 {
726 processing_specialization = 0;
727 template_header_count = 0;
728 }
729
730 /* We've just seen a template header. If SPECIALIZATION is nonzero,
731 it was of the form template <>. */
732
733 static void
734 note_template_header (int specialization)
735 {
736 processing_specialization = specialization;
737 template_header_count++;
738 }
739
740 /* We're beginning an explicit instantiation. */
741
742 void
743 begin_explicit_instantiation (void)
744 {
745 gcc_assert (!processing_explicit_instantiation);
746 processing_explicit_instantiation = true;
747 }
748
749
750 void
751 end_explicit_instantiation (void)
752 {
753 gcc_assert (processing_explicit_instantiation);
754 processing_explicit_instantiation = false;
755 }
756
757 /* An explicit specialization or partial specialization of TMPL is being
758 declared. Check that the namespace in which the specialization is
759 occurring is permissible. Returns false iff it is invalid to
760 specialize TMPL in the current namespace. */
761
762 static bool
763 check_specialization_namespace (tree tmpl)
764 {
765 tree tpl_ns = decl_namespace_context (tmpl);
766
767 /* [tmpl.expl.spec]
768
769 An explicit specialization shall be declared in the namespace of
770 which the template is a member, or, for member templates, in the
771 namespace of which the enclosing class or enclosing class
772 template is a member. An explicit specialization of a member
773 function, member class or static data member of a class template
774 shall be declared in the namespace of which the class template is
775 a member. */
776 if (current_scope() != DECL_CONTEXT (tmpl)
777 && !at_namespace_scope_p ())
778 {
779 error ("specialization of %qD must appear at namespace scope", tmpl);
780 return false;
781 }
782 if (is_associated_namespace (current_namespace, tpl_ns))
783 /* Same or super-using namespace. */
784 return true;
785 else
786 {
787 permerror (input_location,
788 "specialization of %qD in different namespace", tmpl);
789 permerror (DECL_SOURCE_LOCATION (tmpl),
790 " from definition of %q#D", tmpl);
791 return false;
792 }
793 }
794
795 /* SPEC is an explicit instantiation. Check that it is valid to
796 perform this explicit instantiation in the current namespace. */
797
798 static void
799 check_explicit_instantiation_namespace (tree spec)
800 {
801 tree ns;
802
803 /* DR 275: An explicit instantiation shall appear in an enclosing
804 namespace of its template. */
805 ns = decl_namespace_context (spec);
806 if (!is_ancestor (current_namespace, ns))
807 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
808 "(which does not enclose namespace %qD)",
809 spec, current_namespace, ns);
810 }
811
812 // Returns the type of a template specialization only if that
813 // specialization needs to be defined. Otherwise (e.g., if the type has
814 // already been defined), the function returns NULL_TREE.
815 static tree
816 maybe_new_partial_specialization (tree type)
817 {
818 // An implicit instantiation of an incomplete type implies
819 // the definition of a new class template.
820 //
821 // template<typename T>
822 // struct S;
823 //
824 // template<typename T>
825 // struct S<T*>;
826 //
827 // Here, S<T*> is an implicit instantiation of S whose type
828 // is incomplete.
829 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
830 return type;
831
832 // It can also be the case that TYPE is a completed specialization.
833 // Continuing the previous example, suppose we also declare:
834 //
835 // template<typename T>
836 // requires Integral<T>
837 // struct S<T*>;
838 //
839 // Here, S<T*> refers to the specialization S<T*> defined
840 // above. However, we need to differentiate definitions because
841 // we intend to define a new partial specialization. In this case,
842 // we rely on the fact that the constraints are different for
843 // this declaration than that above.
844 //
845 // Note that we also get here for injected class names and
846 // late-parsed template definitions. We must ensure that we
847 // do not create new type declarations for those cases.
848 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
849 {
850 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
851 tree args = CLASSTYPE_TI_ARGS (type);
852
853 // If there are no template parameters, this cannot be a new
854 // partial template specializtion?
855 if (!current_template_parms)
856 return NULL_TREE;
857
858 // The injected-class-name is not a new partial specialization.
859 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
860 return NULL_TREE;
861
862 // If the constraints are not the same as those of the primary
863 // then, we can probably create a new specialization.
864 tree type_constr = current_template_constraints ();
865
866 if (type == TREE_TYPE (tmpl))
867 {
868 tree main_constr = get_constraints (tmpl);
869 if (equivalent_constraints (type_constr, main_constr))
870 return NULL_TREE;
871 }
872
873 // Also, if there's a pre-existing specialization with matching
874 // constraints, then this also isn't new.
875 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
876 while (specs)
877 {
878 tree spec_tmpl = TREE_VALUE (specs);
879 tree spec_args = TREE_PURPOSE (specs);
880 tree spec_constr = get_constraints (spec_tmpl);
881 if (comp_template_args (args, spec_args)
882 && equivalent_constraints (type_constr, spec_constr))
883 return NULL_TREE;
884 specs = TREE_CHAIN (specs);
885 }
886
887 // Create a new type node (and corresponding type decl)
888 // for the newly declared specialization.
889 tree t = make_class_type (TREE_CODE (type));
890 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
891 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
892 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
893
894 /* We only need a separate type node for storing the definition of this
895 partial specialization; uses of S<T*> are unconstrained, so all are
896 equivalent. So keep TYPE_CANONICAL the same. */
897 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
898
899 // Build the corresponding type decl.
900 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
901 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
902 DECL_SOURCE_LOCATION (d) = input_location;
903
904 return t;
905 }
906
907 return NULL_TREE;
908 }
909
910 /* The TYPE is being declared. If it is a template type, that means it
911 is a partial specialization. Do appropriate error-checking. */
912
913 tree
914 maybe_process_partial_specialization (tree type)
915 {
916 tree context;
917
918 if (type == error_mark_node)
919 return error_mark_node;
920
921 /* A lambda that appears in specialization context is not itself a
922 specialization. */
923 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
924 return type;
925
926 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
927 {
928 error ("name of class shadows template template parameter %qD",
929 TYPE_NAME (type));
930 return error_mark_node;
931 }
932
933 context = TYPE_CONTEXT (type);
934
935 if (TYPE_ALIAS_P (type))
936 {
937 if (TYPE_TEMPLATE_INFO (type)
938 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
939 error ("specialization of alias template %qD",
940 TYPE_TI_TEMPLATE (type));
941 else
942 error ("explicit specialization of non-template %qT", type);
943 return error_mark_node;
944 }
945 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
946 {
947 /* This is for ordinary explicit specialization and partial
948 specialization of a template class such as:
949
950 template <> class C<int>;
951
952 or:
953
954 template <class T> class C<T*>;
955
956 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
957
958 if (tree t = maybe_new_partial_specialization (type))
959 {
960 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
961 && !at_namespace_scope_p ())
962 return error_mark_node;
963 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
964 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
965 if (processing_template_decl)
966 {
967 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
968 if (decl == error_mark_node)
969 return error_mark_node;
970 return TREE_TYPE (decl);
971 }
972 }
973 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
974 error ("specialization of %qT after instantiation", type);
975 else if (errorcount && !processing_specialization
976 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
977 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
978 /* Trying to define a specialization either without a template<> header
979 or in an inappropriate place. We've already given an error, so just
980 bail now so we don't actually define the specialization. */
981 return error_mark_node;
982 }
983 else if (CLASS_TYPE_P (type)
984 && !CLASSTYPE_USE_TEMPLATE (type)
985 && CLASSTYPE_TEMPLATE_INFO (type)
986 && context && CLASS_TYPE_P (context)
987 && CLASSTYPE_TEMPLATE_INFO (context))
988 {
989 /* This is for an explicit specialization of member class
990 template according to [temp.expl.spec/18]:
991
992 template <> template <class U> class C<int>::D;
993
994 The context `C<int>' must be an implicit instantiation.
995 Otherwise this is just a member class template declared
996 earlier like:
997
998 template <> class C<int> { template <class U> class D; };
999 template <> template <class U> class C<int>::D;
1000
1001 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1002 while in the second case, `C<int>::D' is a primary template
1003 and `C<T>::D' may not exist. */
1004
1005 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1006 && !COMPLETE_TYPE_P (type))
1007 {
1008 tree t;
1009 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1010
1011 if (current_namespace
1012 != decl_namespace_context (tmpl))
1013 {
1014 permerror (input_location,
1015 "specializing %q#T in different namespace", type);
1016 permerror (DECL_SOURCE_LOCATION (tmpl),
1017 " from definition of %q#D", tmpl);
1018 }
1019
1020 /* Check for invalid specialization after instantiation:
1021
1022 template <> template <> class C<int>::D<int>;
1023 template <> template <class U> class C<int>::D; */
1024
1025 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1026 t; t = TREE_CHAIN (t))
1027 {
1028 tree inst = TREE_VALUE (t);
1029 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1030 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1031 {
1032 /* We already have a full specialization of this partial
1033 instantiation, or a full specialization has been
1034 looked up but not instantiated. Reassign it to the
1035 new member specialization template. */
1036 spec_entry elt;
1037 spec_entry *entry;
1038
1039 elt.tmpl = most_general_template (tmpl);
1040 elt.args = CLASSTYPE_TI_ARGS (inst);
1041 elt.spec = inst;
1042
1043 type_specializations->remove_elt (&elt);
1044
1045 elt.tmpl = tmpl;
1046 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1047
1048 spec_entry **slot
1049 = type_specializations->find_slot (&elt, INSERT);
1050 entry = ggc_alloc<spec_entry> ();
1051 *entry = elt;
1052 *slot = entry;
1053 }
1054 else
1055 /* But if we've had an implicit instantiation, that's a
1056 problem ([temp.expl.spec]/6). */
1057 error ("specialization %qT after instantiation %qT",
1058 type, inst);
1059 }
1060
1061 /* Mark TYPE as a specialization. And as a result, we only
1062 have one level of template argument for the innermost
1063 class template. */
1064 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1065 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1066 CLASSTYPE_TI_ARGS (type)
1067 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1068 }
1069 }
1070 else if (processing_specialization)
1071 {
1072 /* Someday C++0x may allow for enum template specialization. */
1073 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1074 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1075 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1076 "of %qD not allowed by ISO C++", type);
1077 else
1078 {
1079 error ("explicit specialization of non-template %qT", type);
1080 return error_mark_node;
1081 }
1082 }
1083
1084 return type;
1085 }
1086
1087 /* Returns nonzero if we can optimize the retrieval of specializations
1088 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1089 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1090
1091 static inline bool
1092 optimize_specialization_lookup_p (tree tmpl)
1093 {
1094 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1095 && DECL_CLASS_SCOPE_P (tmpl)
1096 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1097 parameter. */
1098 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1099 /* The optimized lookup depends on the fact that the
1100 template arguments for the member function template apply
1101 purely to the containing class, which is not true if the
1102 containing class is an explicit or partial
1103 specialization. */
1104 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1105 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1106 && !DECL_CONV_FN_P (tmpl)
1107 /* It is possible to have a template that is not a member
1108 template and is not a member of a template class:
1109
1110 template <typename T>
1111 struct S { friend A::f(); };
1112
1113 Here, the friend function is a template, but the context does
1114 not have template information. The optimized lookup relies
1115 on having ARGS be the template arguments for both the class
1116 and the function template. */
1117 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1118 }
1119
1120 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1121 gone through coerce_template_parms by now. */
1122
1123 static void
1124 verify_unstripped_args (tree args)
1125 {
1126 ++processing_template_decl;
1127 if (!any_dependent_template_arguments_p (args))
1128 {
1129 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1130 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1131 {
1132 tree arg = TREE_VEC_ELT (inner, i);
1133 if (TREE_CODE (arg) == TEMPLATE_DECL)
1134 /* OK */;
1135 else if (TYPE_P (arg))
1136 gcc_assert (strip_typedefs (arg, NULL) == arg);
1137 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1138 /* Allow typedefs on the type of a non-type argument, since a
1139 parameter can have them. */;
1140 else
1141 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1142 }
1143 }
1144 --processing_template_decl;
1145 }
1146
1147 /* Retrieve the specialization (in the sense of [temp.spec] - a
1148 specialization is either an instantiation or an explicit
1149 specialization) of TMPL for the given template ARGS. If there is
1150 no such specialization, return NULL_TREE. The ARGS are a vector of
1151 arguments, or a vector of vectors of arguments, in the case of
1152 templates with more than one level of parameters.
1153
1154 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1155 then we search for a partial specialization matching ARGS. This
1156 parameter is ignored if TMPL is not a class template.
1157
1158 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1159 result is a NONTYPE_ARGUMENT_PACK. */
1160
1161 static tree
1162 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1163 {
1164 if (tmpl == NULL_TREE)
1165 return NULL_TREE;
1166
1167 if (args == error_mark_node)
1168 return NULL_TREE;
1169
1170 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1171 || TREE_CODE (tmpl) == FIELD_DECL);
1172
1173 /* There should be as many levels of arguments as there are
1174 levels of parameters. */
1175 gcc_assert (TMPL_ARGS_DEPTH (args)
1176 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1177 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1178 : template_class_depth (DECL_CONTEXT (tmpl))));
1179
1180 if (flag_checking)
1181 verify_unstripped_args (args);
1182
1183 if (optimize_specialization_lookup_p (tmpl))
1184 {
1185 tree class_template;
1186 tree class_specialization;
1187 vec<tree, va_gc> *methods;
1188 tree fns;
1189 int idx;
1190
1191 /* The template arguments actually apply to the containing
1192 class. Find the class specialization with those
1193 arguments. */
1194 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1195 class_specialization
1196 = retrieve_specialization (class_template, args, 0);
1197 if (!class_specialization)
1198 return NULL_TREE;
1199 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1200 for the specialization. */
1201 idx = class_method_index_for_fn (class_specialization, tmpl);
1202 if (idx == -1)
1203 return NULL_TREE;
1204 /* Iterate through the methods with the indicated name, looking
1205 for the one that has an instance of TMPL. */
1206 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1207 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1208 {
1209 tree fn = OVL_CURRENT (fns);
1210 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1211 /* using-declarations can add base methods to the method vec,
1212 and we don't want those here. */
1213 && DECL_CONTEXT (fn) == class_specialization)
1214 return fn;
1215 }
1216 return NULL_TREE;
1217 }
1218 else
1219 {
1220 spec_entry *found;
1221 spec_entry elt;
1222 hash_table<spec_hasher> *specializations;
1223
1224 elt.tmpl = tmpl;
1225 elt.args = args;
1226 elt.spec = NULL_TREE;
1227
1228 if (DECL_CLASS_TEMPLATE_P (tmpl))
1229 specializations = type_specializations;
1230 else
1231 specializations = decl_specializations;
1232
1233 if (hash == 0)
1234 hash = spec_hasher::hash (&elt);
1235 found = specializations->find_with_hash (&elt, hash);
1236 if (found)
1237 return found->spec;
1238 }
1239
1240 return NULL_TREE;
1241 }
1242
1243 /* Like retrieve_specialization, but for local declarations. */
1244
1245 tree
1246 retrieve_local_specialization (tree tmpl)
1247 {
1248 if (local_specializations == NULL)
1249 return NULL_TREE;
1250
1251 tree *slot = local_specializations->get (tmpl);
1252 return slot ? *slot : NULL_TREE;
1253 }
1254
1255 /* Returns nonzero iff DECL is a specialization of TMPL. */
1256
1257 int
1258 is_specialization_of (tree decl, tree tmpl)
1259 {
1260 tree t;
1261
1262 if (TREE_CODE (decl) == FUNCTION_DECL)
1263 {
1264 for (t = decl;
1265 t != NULL_TREE;
1266 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1267 if (t == tmpl)
1268 return 1;
1269 }
1270 else
1271 {
1272 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1273
1274 for (t = TREE_TYPE (decl);
1275 t != NULL_TREE;
1276 t = CLASSTYPE_USE_TEMPLATE (t)
1277 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1278 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1279 return 1;
1280 }
1281
1282 return 0;
1283 }
1284
1285 /* Returns nonzero iff DECL is a specialization of friend declaration
1286 FRIEND_DECL according to [temp.friend]. */
1287
1288 bool
1289 is_specialization_of_friend (tree decl, tree friend_decl)
1290 {
1291 bool need_template = true;
1292 int template_depth;
1293
1294 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1295 || TREE_CODE (decl) == TYPE_DECL);
1296
1297 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1298 of a template class, we want to check if DECL is a specialization
1299 if this. */
1300 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1301 && DECL_TEMPLATE_INFO (friend_decl)
1302 && !DECL_USE_TEMPLATE (friend_decl))
1303 {
1304 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1305 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1306 need_template = false;
1307 }
1308 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1309 && !PRIMARY_TEMPLATE_P (friend_decl))
1310 need_template = false;
1311
1312 /* There is nothing to do if this is not a template friend. */
1313 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1314 return false;
1315
1316 if (is_specialization_of (decl, friend_decl))
1317 return true;
1318
1319 /* [temp.friend/6]
1320 A member of a class template may be declared to be a friend of a
1321 non-template class. In this case, the corresponding member of
1322 every specialization of the class template is a friend of the
1323 class granting friendship.
1324
1325 For example, given a template friend declaration
1326
1327 template <class T> friend void A<T>::f();
1328
1329 the member function below is considered a friend
1330
1331 template <> struct A<int> {
1332 void f();
1333 };
1334
1335 For this type of template friend, TEMPLATE_DEPTH below will be
1336 nonzero. To determine if DECL is a friend of FRIEND, we first
1337 check if the enclosing class is a specialization of another. */
1338
1339 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1340 if (template_depth
1341 && DECL_CLASS_SCOPE_P (decl)
1342 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1343 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1344 {
1345 /* Next, we check the members themselves. In order to handle
1346 a few tricky cases, such as when FRIEND_DECL's are
1347
1348 template <class T> friend void A<T>::g(T t);
1349 template <class T> template <T t> friend void A<T>::h();
1350
1351 and DECL's are
1352
1353 void A<int>::g(int);
1354 template <int> void A<int>::h();
1355
1356 we need to figure out ARGS, the template arguments from
1357 the context of DECL. This is required for template substitution
1358 of `T' in the function parameter of `g' and template parameter
1359 of `h' in the above examples. Here ARGS corresponds to `int'. */
1360
1361 tree context = DECL_CONTEXT (decl);
1362 tree args = NULL_TREE;
1363 int current_depth = 0;
1364
1365 while (current_depth < template_depth)
1366 {
1367 if (CLASSTYPE_TEMPLATE_INFO (context))
1368 {
1369 if (current_depth == 0)
1370 args = TYPE_TI_ARGS (context);
1371 else
1372 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1373 current_depth++;
1374 }
1375 context = TYPE_CONTEXT (context);
1376 }
1377
1378 if (TREE_CODE (decl) == FUNCTION_DECL)
1379 {
1380 bool is_template;
1381 tree friend_type;
1382 tree decl_type;
1383 tree friend_args_type;
1384 tree decl_args_type;
1385
1386 /* Make sure that both DECL and FRIEND_DECL are templates or
1387 non-templates. */
1388 is_template = DECL_TEMPLATE_INFO (decl)
1389 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1390 if (need_template ^ is_template)
1391 return false;
1392 else if (is_template)
1393 {
1394 /* If both are templates, check template parameter list. */
1395 tree friend_parms
1396 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1397 args, tf_none);
1398 if (!comp_template_parms
1399 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1400 friend_parms))
1401 return false;
1402
1403 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1404 }
1405 else
1406 decl_type = TREE_TYPE (decl);
1407
1408 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1409 tf_none, NULL_TREE);
1410 if (friend_type == error_mark_node)
1411 return false;
1412
1413 /* Check if return types match. */
1414 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1415 return false;
1416
1417 /* Check if function parameter types match, ignoring the
1418 `this' parameter. */
1419 friend_args_type = TYPE_ARG_TYPES (friend_type);
1420 decl_args_type = TYPE_ARG_TYPES (decl_type);
1421 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1422 friend_args_type = TREE_CHAIN (friend_args_type);
1423 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1424 decl_args_type = TREE_CHAIN (decl_args_type);
1425
1426 return compparms (decl_args_type, friend_args_type);
1427 }
1428 else
1429 {
1430 /* DECL is a TYPE_DECL */
1431 bool is_template;
1432 tree decl_type = TREE_TYPE (decl);
1433
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1435 non-templates. */
1436 is_template
1437 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1439
1440 if (need_template ^ is_template)
1441 return false;
1442 else if (is_template)
1443 {
1444 tree friend_parms;
1445 /* If both are templates, check the name of the two
1446 TEMPLATE_DECL's first because is_friend didn't. */
1447 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1448 != DECL_NAME (friend_decl))
1449 return false;
1450
1451 /* Now check template parameter list. */
1452 friend_parms
1453 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1454 args, tf_none);
1455 return comp_template_parms
1456 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1457 friend_parms);
1458 }
1459 else
1460 return (DECL_NAME (decl)
1461 == DECL_NAME (friend_decl));
1462 }
1463 }
1464 return false;
1465 }
1466
1467 /* Register the specialization SPEC as a specialization of TMPL with
1468 the indicated ARGS. IS_FRIEND indicates whether the specialization
1469 is actually just a friend declaration. Returns SPEC, or an
1470 equivalent prior declaration, if available.
1471
1472 We also store instantiations of field packs in the hash table, even
1473 though they are not themselves templates, to make lookup easier. */
1474
1475 static tree
1476 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1477 hashval_t hash)
1478 {
1479 tree fn;
1480 spec_entry **slot = NULL;
1481 spec_entry elt;
1482
1483 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1484 || (TREE_CODE (tmpl) == FIELD_DECL
1485 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1486
1487 if (TREE_CODE (spec) == FUNCTION_DECL
1488 && uses_template_parms (DECL_TI_ARGS (spec)))
1489 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1490 register it; we want the corresponding TEMPLATE_DECL instead.
1491 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1492 the more obvious `uses_template_parms (spec)' to avoid problems
1493 with default function arguments. In particular, given
1494 something like this:
1495
1496 template <class T> void f(T t1, T t = T())
1497
1498 the default argument expression is not substituted for in an
1499 instantiation unless and until it is actually needed. */
1500 return spec;
1501
1502 if (optimize_specialization_lookup_p (tmpl))
1503 /* We don't put these specializations in the hash table, but we might
1504 want to give an error about a mismatch. */
1505 fn = retrieve_specialization (tmpl, args, 0);
1506 else
1507 {
1508 elt.tmpl = tmpl;
1509 elt.args = args;
1510 elt.spec = spec;
1511
1512 if (hash == 0)
1513 hash = spec_hasher::hash (&elt);
1514
1515 slot =
1516 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1517 if (*slot)
1518 fn = ((spec_entry *) *slot)->spec;
1519 else
1520 fn = NULL_TREE;
1521 }
1522
1523 /* We can sometimes try to re-register a specialization that we've
1524 already got. In particular, regenerate_decl_from_template calls
1525 duplicate_decls which will update the specialization list. But,
1526 we'll still get called again here anyhow. It's more convenient
1527 to simply allow this than to try to prevent it. */
1528 if (fn == spec)
1529 return spec;
1530 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1531 {
1532 if (DECL_TEMPLATE_INSTANTIATION (fn))
1533 {
1534 if (DECL_ODR_USED (fn)
1535 || DECL_EXPLICIT_INSTANTIATION (fn))
1536 {
1537 error ("specialization of %qD after instantiation",
1538 fn);
1539 return error_mark_node;
1540 }
1541 else
1542 {
1543 tree clone;
1544 /* This situation should occur only if the first
1545 specialization is an implicit instantiation, the
1546 second is an explicit specialization, and the
1547 implicit instantiation has not yet been used. That
1548 situation can occur if we have implicitly
1549 instantiated a member function and then specialized
1550 it later.
1551
1552 We can also wind up here if a friend declaration that
1553 looked like an instantiation turns out to be a
1554 specialization:
1555
1556 template <class T> void foo(T);
1557 class S { friend void foo<>(int) };
1558 template <> void foo(int);
1559
1560 We transform the existing DECL in place so that any
1561 pointers to it become pointers to the updated
1562 declaration.
1563
1564 If there was a definition for the template, but not
1565 for the specialization, we want this to look as if
1566 there were no definition, and vice versa. */
1567 DECL_INITIAL (fn) = NULL_TREE;
1568 duplicate_decls (spec, fn, is_friend);
1569 /* The call to duplicate_decls will have applied
1570 [temp.expl.spec]:
1571
1572 An explicit specialization of a function template
1573 is inline only if it is explicitly declared to be,
1574 and independently of whether its function template
1575 is.
1576
1577 to the primary function; now copy the inline bits to
1578 the various clones. */
1579 FOR_EACH_CLONE (clone, fn)
1580 {
1581 DECL_DECLARED_INLINE_P (clone)
1582 = DECL_DECLARED_INLINE_P (fn);
1583 DECL_SOURCE_LOCATION (clone)
1584 = DECL_SOURCE_LOCATION (fn);
1585 DECL_DELETED_FN (clone)
1586 = DECL_DELETED_FN (fn);
1587 }
1588 check_specialization_namespace (tmpl);
1589
1590 return fn;
1591 }
1592 }
1593 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1594 {
1595 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1596 /* Dup decl failed, but this is a new definition. Set the
1597 line number so any errors match this new
1598 definition. */
1599 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1600
1601 return fn;
1602 }
1603 }
1604 else if (fn)
1605 return duplicate_decls (spec, fn, is_friend);
1606
1607 /* A specialization must be declared in the same namespace as the
1608 template it is specializing. */
1609 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1610 && !check_specialization_namespace (tmpl))
1611 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1612
1613 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1614 {
1615 spec_entry *entry = ggc_alloc<spec_entry> ();
1616 gcc_assert (tmpl && args && spec);
1617 *entry = elt;
1618 *slot = entry;
1619 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1620 && PRIMARY_TEMPLATE_P (tmpl)
1621 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1622 || variable_template_p (tmpl))
1623 /* If TMPL is a forward declaration of a template function, keep a list
1624 of all specializations in case we need to reassign them to a friend
1625 template later in tsubst_friend_function.
1626
1627 Also keep a list of all variable template instantiations so that
1628 process_partial_specialization can check whether a later partial
1629 specialization would have used it. */
1630 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1631 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1632 }
1633
1634 return spec;
1635 }
1636
1637 /* Returns true iff two spec_entry nodes are equivalent. */
1638
1639 int comparing_specializations;
1640
1641 bool
1642 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1643 {
1644 int equal;
1645
1646 ++comparing_specializations;
1647 equal = (e1->tmpl == e2->tmpl
1648 && comp_template_args (e1->args, e2->args));
1649 if (equal && flag_concepts
1650 /* tmpl could be a FIELD_DECL for a capture pack. */
1651 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1652 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1653 && uses_template_parms (e1->args))
1654 {
1655 /* Partial specializations of a variable template can be distinguished by
1656 constraints. */
1657 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1658 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1659 equal = equivalent_constraints (c1, c2);
1660 }
1661 --comparing_specializations;
1662
1663 return equal;
1664 }
1665
1666 /* Returns a hash for a template TMPL and template arguments ARGS. */
1667
1668 static hashval_t
1669 hash_tmpl_and_args (tree tmpl, tree args)
1670 {
1671 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1672 return iterative_hash_template_arg (args, val);
1673 }
1674
1675 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1676 ignoring SPEC. */
1677
1678 hashval_t
1679 spec_hasher::hash (spec_entry *e)
1680 {
1681 return hash_tmpl_and_args (e->tmpl, e->args);
1682 }
1683
1684 /* Recursively calculate a hash value for a template argument ARG, for use
1685 in the hash tables of template specializations. */
1686
1687 hashval_t
1688 iterative_hash_template_arg (tree arg, hashval_t val)
1689 {
1690 unsigned HOST_WIDE_INT i;
1691 enum tree_code code;
1692 char tclass;
1693
1694 if (arg == NULL_TREE)
1695 return iterative_hash_object (arg, val);
1696
1697 if (!TYPE_P (arg))
1698 STRIP_NOPS (arg);
1699
1700 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1701 /* We can get one of these when re-hashing a previous entry in the middle
1702 of substituting into a pack expansion. Just look through it. */
1703 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1704
1705 code = TREE_CODE (arg);
1706 tclass = TREE_CODE_CLASS (code);
1707
1708 val = iterative_hash_object (code, val);
1709
1710 switch (code)
1711 {
1712 case ERROR_MARK:
1713 return val;
1714
1715 case IDENTIFIER_NODE:
1716 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1717
1718 case TREE_VEC:
1719 {
1720 int i, len = TREE_VEC_LENGTH (arg);
1721 for (i = 0; i < len; ++i)
1722 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1723 return val;
1724 }
1725
1726 case TYPE_PACK_EXPANSION:
1727 case EXPR_PACK_EXPANSION:
1728 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1729 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1730
1731 case TYPE_ARGUMENT_PACK:
1732 case NONTYPE_ARGUMENT_PACK:
1733 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1734
1735 case TREE_LIST:
1736 for (; arg; arg = TREE_CHAIN (arg))
1737 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1738 return val;
1739
1740 case OVERLOAD:
1741 for (; arg; arg = OVL_NEXT (arg))
1742 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1743 return val;
1744
1745 case CONSTRUCTOR:
1746 {
1747 tree field, value;
1748 iterative_hash_template_arg (TREE_TYPE (arg), val);
1749 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1750 {
1751 val = iterative_hash_template_arg (field, val);
1752 val = iterative_hash_template_arg (value, val);
1753 }
1754 return val;
1755 }
1756
1757 case PARM_DECL:
1758 if (!DECL_ARTIFICIAL (arg))
1759 {
1760 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1761 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1762 }
1763 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1764
1765 case TARGET_EXPR:
1766 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1767
1768 case PTRMEM_CST:
1769 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1770 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1771
1772 case TEMPLATE_PARM_INDEX:
1773 val = iterative_hash_template_arg
1774 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1775 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1776 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1777
1778 case TRAIT_EXPR:
1779 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1780 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1781 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1782
1783 case BASELINK:
1784 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1785 val);
1786 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1787 val);
1788
1789 case MODOP_EXPR:
1790 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1791 code = TREE_CODE (TREE_OPERAND (arg, 1));
1792 val = iterative_hash_object (code, val);
1793 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1794
1795 case LAMBDA_EXPR:
1796 /* A lambda can't appear in a template arg, but don't crash on
1797 erroneous input. */
1798 gcc_assert (seen_error ());
1799 return val;
1800
1801 case CAST_EXPR:
1802 case IMPLICIT_CONV_EXPR:
1803 case STATIC_CAST_EXPR:
1804 case REINTERPRET_CAST_EXPR:
1805 case CONST_CAST_EXPR:
1806 case DYNAMIC_CAST_EXPR:
1807 case NEW_EXPR:
1808 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1809 /* Now hash operands as usual. */
1810 break;
1811
1812 default:
1813 break;
1814 }
1815
1816 switch (tclass)
1817 {
1818 case tcc_type:
1819 if (alias_template_specialization_p (arg))
1820 {
1821 // We want an alias specialization that survived strip_typedefs
1822 // to hash differently from its TYPE_CANONICAL, to avoid hash
1823 // collisions that compare as different in template_args_equal.
1824 // These could be dependent specializations that strip_typedefs
1825 // left alone, or untouched specializations because
1826 // coerce_template_parms returns the unconverted template
1827 // arguments if it sees incomplete argument packs.
1828 tree ti = TYPE_TEMPLATE_INFO (arg);
1829 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1830 }
1831 if (TYPE_CANONICAL (arg))
1832 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1833 val);
1834 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1835 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1836 /* Otherwise just compare the types during lookup. */
1837 return val;
1838
1839 case tcc_declaration:
1840 case tcc_constant:
1841 return iterative_hash_expr (arg, val);
1842
1843 default:
1844 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1845 {
1846 unsigned n = cp_tree_operand_length (arg);
1847 for (i = 0; i < n; ++i)
1848 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1849 return val;
1850 }
1851 }
1852 gcc_unreachable ();
1853 return 0;
1854 }
1855
1856 /* Unregister the specialization SPEC as a specialization of TMPL.
1857 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1858 if the SPEC was listed as a specialization of TMPL.
1859
1860 Note that SPEC has been ggc_freed, so we can't look inside it. */
1861
1862 bool
1863 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1864 {
1865 spec_entry *entry;
1866 spec_entry elt;
1867
1868 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1869 elt.args = TI_ARGS (tinfo);
1870 elt.spec = NULL_TREE;
1871
1872 entry = decl_specializations->find (&elt);
1873 if (entry != NULL)
1874 {
1875 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1876 gcc_assert (new_spec != NULL_TREE);
1877 entry->spec = new_spec;
1878 return 1;
1879 }
1880
1881 return 0;
1882 }
1883
1884 /* Like register_specialization, but for local declarations. We are
1885 registering SPEC, an instantiation of TMPL. */
1886
1887 void
1888 register_local_specialization (tree spec, tree tmpl)
1889 {
1890 local_specializations->put (tmpl, spec);
1891 }
1892
1893 /* TYPE is a class type. Returns true if TYPE is an explicitly
1894 specialized class. */
1895
1896 bool
1897 explicit_class_specialization_p (tree type)
1898 {
1899 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1900 return false;
1901 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1902 }
1903
1904 /* Print the list of functions at FNS, going through all the overloads
1905 for each element of the list. Alternatively, FNS can not be a
1906 TREE_LIST, in which case it will be printed together with all the
1907 overloads.
1908
1909 MORE and *STR should respectively be FALSE and NULL when the function
1910 is called from the outside. They are used internally on recursive
1911 calls. print_candidates manages the two parameters and leaves NULL
1912 in *STR when it ends. */
1913
1914 static void
1915 print_candidates_1 (tree fns, bool more, const char **str)
1916 {
1917 tree fn, fn2;
1918 char *spaces = NULL;
1919
1920 for (fn = fns; fn; fn = OVL_NEXT (fn))
1921 if (TREE_CODE (fn) == TREE_LIST)
1922 {
1923 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1924 print_candidates_1 (TREE_VALUE (fn2),
1925 TREE_CHAIN (fn2) || more, str);
1926 }
1927 else
1928 {
1929 tree cand = OVL_CURRENT (fn);
1930 if (!*str)
1931 {
1932 /* Pick the prefix string. */
1933 if (!more && !OVL_NEXT (fns))
1934 {
1935 inform (DECL_SOURCE_LOCATION (cand),
1936 "candidate is: %#D", cand);
1937 continue;
1938 }
1939
1940 *str = _("candidates are:");
1941 spaces = get_spaces (*str);
1942 }
1943 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1944 *str = spaces ? spaces : *str;
1945 }
1946
1947 if (!more)
1948 {
1949 free (spaces);
1950 *str = NULL;
1951 }
1952 }
1953
1954 /* Print the list of candidate FNS in an error message. FNS can also
1955 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1956
1957 void
1958 print_candidates (tree fns)
1959 {
1960 const char *str = NULL;
1961 print_candidates_1 (fns, false, &str);
1962 gcc_assert (str == NULL);
1963 }
1964
1965 /* Get a (possibly) constrained template declaration for the
1966 purpose of ordering candidates. */
1967 static tree
1968 get_template_for_ordering (tree list)
1969 {
1970 gcc_assert (TREE_CODE (list) == TREE_LIST);
1971 tree f = TREE_VALUE (list);
1972 if (tree ti = DECL_TEMPLATE_INFO (f))
1973 return TI_TEMPLATE (ti);
1974 return f;
1975 }
1976
1977 /* Among candidates having the same signature, return the
1978 most constrained or NULL_TREE if there is no best candidate.
1979 If the signatures of candidates vary (e.g., template
1980 specialization vs. member function), then there can be no
1981 most constrained.
1982
1983 Note that we don't compare constraints on the functions
1984 themselves, but rather those of their templates. */
1985 static tree
1986 most_constrained_function (tree candidates)
1987 {
1988 // Try to find the best candidate in a first pass.
1989 tree champ = candidates;
1990 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1991 {
1992 int winner = more_constrained (get_template_for_ordering (champ),
1993 get_template_for_ordering (c));
1994 if (winner == -1)
1995 champ = c; // The candidate is more constrained
1996 else if (winner == 0)
1997 return NULL_TREE; // Neither is more constrained
1998 }
1999
2000 // Verify that the champ is better than previous candidates.
2001 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2002 if (!more_constrained (get_template_for_ordering (champ),
2003 get_template_for_ordering (c)))
2004 return NULL_TREE;
2005 }
2006
2007 return champ;
2008 }
2009
2010
2011 /* Returns the template (one of the functions given by TEMPLATE_ID)
2012 which can be specialized to match the indicated DECL with the
2013 explicit template args given in TEMPLATE_ID. The DECL may be
2014 NULL_TREE if none is available. In that case, the functions in
2015 TEMPLATE_ID are non-members.
2016
2017 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2018 specialization of a member template.
2019
2020 The TEMPLATE_COUNT is the number of references to qualifying
2021 template classes that appeared in the name of the function. See
2022 check_explicit_specialization for a more accurate description.
2023
2024 TSK indicates what kind of template declaration (if any) is being
2025 declared. TSK_TEMPLATE indicates that the declaration given by
2026 DECL, though a FUNCTION_DECL, has template parameters, and is
2027 therefore a template function.
2028
2029 The template args (those explicitly specified and those deduced)
2030 are output in a newly created vector *TARGS_OUT.
2031
2032 If it is impossible to determine the result, an error message is
2033 issued. The error_mark_node is returned to indicate failure. */
2034
2035 static tree
2036 determine_specialization (tree template_id,
2037 tree decl,
2038 tree* targs_out,
2039 int need_member_template,
2040 int template_count,
2041 tmpl_spec_kind tsk)
2042 {
2043 tree fns;
2044 tree targs;
2045 tree explicit_targs;
2046 tree candidates = NULL_TREE;
2047
2048 /* A TREE_LIST of templates of which DECL may be a specialization.
2049 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2050 corresponding TREE_PURPOSE is the set of template arguments that,
2051 when used to instantiate the template, would produce a function
2052 with the signature of DECL. */
2053 tree templates = NULL_TREE;
2054 int header_count;
2055 cp_binding_level *b;
2056
2057 *targs_out = NULL_TREE;
2058
2059 if (template_id == error_mark_node || decl == error_mark_node)
2060 return error_mark_node;
2061
2062 /* We shouldn't be specializing a member template of an
2063 unspecialized class template; we already gave an error in
2064 check_specialization_scope, now avoid crashing. */
2065 if (template_count && DECL_CLASS_SCOPE_P (decl)
2066 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2067 {
2068 gcc_assert (errorcount);
2069 return error_mark_node;
2070 }
2071
2072 fns = TREE_OPERAND (template_id, 0);
2073 explicit_targs = TREE_OPERAND (template_id, 1);
2074
2075 if (fns == error_mark_node)
2076 return error_mark_node;
2077
2078 /* Check for baselinks. */
2079 if (BASELINK_P (fns))
2080 fns = BASELINK_FUNCTIONS (fns);
2081
2082 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2083 {
2084 error ("%qD is not a function template", fns);
2085 return error_mark_node;
2086 }
2087 else if (VAR_P (decl) && !variable_template_p (fns))
2088 {
2089 error ("%qD is not a variable template", fns);
2090 return error_mark_node;
2091 }
2092
2093 /* Count the number of template headers specified for this
2094 specialization. */
2095 header_count = 0;
2096 for (b = current_binding_level;
2097 b->kind == sk_template_parms;
2098 b = b->level_chain)
2099 ++header_count;
2100
2101 tree orig_fns = fns;
2102
2103 if (variable_template_p (fns))
2104 {
2105 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2106 targs = coerce_template_parms (parms, explicit_targs, fns,
2107 tf_warning_or_error,
2108 /*req_all*/true, /*use_defarg*/true);
2109 if (targs != error_mark_node)
2110 templates = tree_cons (targs, fns, templates);
2111 }
2112 else for (; fns; fns = OVL_NEXT (fns))
2113 {
2114 tree fn = OVL_CURRENT (fns);
2115
2116 if (TREE_CODE (fn) == TEMPLATE_DECL)
2117 {
2118 tree decl_arg_types;
2119 tree fn_arg_types;
2120 tree insttype;
2121
2122 /* In case of explicit specialization, we need to check if
2123 the number of template headers appearing in the specialization
2124 is correct. This is usually done in check_explicit_specialization,
2125 but the check done there cannot be exhaustive when specializing
2126 member functions. Consider the following code:
2127
2128 template <> void A<int>::f(int);
2129 template <> template <> void A<int>::f(int);
2130
2131 Assuming that A<int> is not itself an explicit specialization
2132 already, the first line specializes "f" which is a non-template
2133 member function, whilst the second line specializes "f" which
2134 is a template member function. So both lines are syntactically
2135 correct, and check_explicit_specialization does not reject
2136 them.
2137
2138 Here, we can do better, as we are matching the specialization
2139 against the declarations. We count the number of template
2140 headers, and we check if they match TEMPLATE_COUNT + 1
2141 (TEMPLATE_COUNT is the number of qualifying template classes,
2142 plus there must be another header for the member template
2143 itself).
2144
2145 Notice that if header_count is zero, this is not a
2146 specialization but rather a template instantiation, so there
2147 is no check we can perform here. */
2148 if (header_count && header_count != template_count + 1)
2149 continue;
2150
2151 /* Check that the number of template arguments at the
2152 innermost level for DECL is the same as for FN. */
2153 if (current_binding_level->kind == sk_template_parms
2154 && !current_binding_level->explicit_spec_p
2155 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2156 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2157 (current_template_parms))))
2158 continue;
2159
2160 /* DECL might be a specialization of FN. */
2161 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2162 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2163
2164 /* For a non-static member function, we need to make sure
2165 that the const qualification is the same. Since
2166 get_bindings does not try to merge the "this" parameter,
2167 we must do the comparison explicitly. */
2168 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2169 && !same_type_p (TREE_VALUE (fn_arg_types),
2170 TREE_VALUE (decl_arg_types)))
2171 continue;
2172
2173 /* Skip the "this" parameter and, for constructors of
2174 classes with virtual bases, the VTT parameter. A
2175 full specialization of a constructor will have a VTT
2176 parameter, but a template never will. */
2177 decl_arg_types
2178 = skip_artificial_parms_for (decl, decl_arg_types);
2179 fn_arg_types
2180 = skip_artificial_parms_for (fn, fn_arg_types);
2181
2182 /* Function templates cannot be specializations; there are
2183 no partial specializations of functions. Therefore, if
2184 the type of DECL does not match FN, there is no
2185 match.
2186
2187 Note that it should never be the case that we have both
2188 candidates added here, and for regular member functions
2189 below. */
2190 if (tsk == tsk_template)
2191 {
2192 if (compparms (fn_arg_types, decl_arg_types))
2193 candidates = tree_cons (NULL_TREE, fn, candidates);
2194 continue;
2195 }
2196
2197 /* See whether this function might be a specialization of this
2198 template. Suppress access control because we might be trying
2199 to make this specialization a friend, and we have already done
2200 access control for the declaration of the specialization. */
2201 push_deferring_access_checks (dk_no_check);
2202 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2203 pop_deferring_access_checks ();
2204
2205 if (!targs)
2206 /* We cannot deduce template arguments that when used to
2207 specialize TMPL will produce DECL. */
2208 continue;
2209
2210 /* Remove, from the set of candidates, all those functions
2211 whose constraints are not satisfied. */
2212 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2213 continue;
2214
2215 // Then, try to form the new function type.
2216 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2217 if (insttype == error_mark_node)
2218 continue;
2219 fn_arg_types
2220 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2221 if (!compparms (fn_arg_types, decl_arg_types))
2222 continue;
2223
2224 /* Save this template, and the arguments deduced. */
2225 templates = tree_cons (targs, fn, templates);
2226 }
2227 else if (need_member_template)
2228 /* FN is an ordinary member function, and we need a
2229 specialization of a member template. */
2230 ;
2231 else if (TREE_CODE (fn) != FUNCTION_DECL)
2232 /* We can get IDENTIFIER_NODEs here in certain erroneous
2233 cases. */
2234 ;
2235 else if (!DECL_FUNCTION_MEMBER_P (fn))
2236 /* This is just an ordinary non-member function. Nothing can
2237 be a specialization of that. */
2238 ;
2239 else if (DECL_ARTIFICIAL (fn))
2240 /* Cannot specialize functions that are created implicitly. */
2241 ;
2242 else
2243 {
2244 tree decl_arg_types;
2245
2246 /* This is an ordinary member function. However, since
2247 we're here, we can assume its enclosing class is a
2248 template class. For example,
2249
2250 template <typename T> struct S { void f(); };
2251 template <> void S<int>::f() {}
2252
2253 Here, S<int>::f is a non-template, but S<int> is a
2254 template class. If FN has the same type as DECL, we
2255 might be in business. */
2256
2257 if (!DECL_TEMPLATE_INFO (fn))
2258 /* Its enclosing class is an explicit specialization
2259 of a template class. This is not a candidate. */
2260 continue;
2261
2262 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2263 TREE_TYPE (TREE_TYPE (fn))))
2264 /* The return types differ. */
2265 continue;
2266
2267 /* Adjust the type of DECL in case FN is a static member. */
2268 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2269 if (DECL_STATIC_FUNCTION_P (fn)
2270 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2271 decl_arg_types = TREE_CHAIN (decl_arg_types);
2272
2273 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2274 decl_arg_types))
2275 continue;
2276
2277 // If the deduced arguments do not satisfy the constraints,
2278 // this is not a candidate.
2279 if (flag_concepts && !constraints_satisfied_p (fn))
2280 continue;
2281
2282 // Add the candidate.
2283 candidates = tree_cons (NULL_TREE, fn, candidates);
2284 }
2285 }
2286
2287 if (templates && TREE_CHAIN (templates))
2288 {
2289 /* We have:
2290
2291 [temp.expl.spec]
2292
2293 It is possible for a specialization with a given function
2294 signature to be instantiated from more than one function
2295 template. In such cases, explicit specification of the
2296 template arguments must be used to uniquely identify the
2297 function template specialization being specialized.
2298
2299 Note that here, there's no suggestion that we're supposed to
2300 determine which of the candidate templates is most
2301 specialized. However, we, also have:
2302
2303 [temp.func.order]
2304
2305 Partial ordering of overloaded function template
2306 declarations is used in the following contexts to select
2307 the function template to which a function template
2308 specialization refers:
2309
2310 -- when an explicit specialization refers to a function
2311 template.
2312
2313 So, we do use the partial ordering rules, at least for now.
2314 This extension can only serve to make invalid programs valid,
2315 so it's safe. And, there is strong anecdotal evidence that
2316 the committee intended the partial ordering rules to apply;
2317 the EDG front end has that behavior, and John Spicer claims
2318 that the committee simply forgot to delete the wording in
2319 [temp.expl.spec]. */
2320 tree tmpl = most_specialized_instantiation (templates);
2321 if (tmpl != error_mark_node)
2322 {
2323 templates = tmpl;
2324 TREE_CHAIN (templates) = NULL_TREE;
2325 }
2326 }
2327
2328 // Concepts allows multiple declarations of member functions
2329 // with the same signature. Like above, we need to rely on
2330 // on the partial ordering of those candidates to determine which
2331 // is the best.
2332 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2333 {
2334 if (tree cand = most_constrained_function (candidates))
2335 {
2336 candidates = cand;
2337 TREE_CHAIN (cand) = NULL_TREE;
2338 }
2339 }
2340
2341 if (templates == NULL_TREE && candidates == NULL_TREE)
2342 {
2343 error ("template-id %qD for %q+D does not match any template "
2344 "declaration", template_id, decl);
2345 if (header_count && header_count != template_count + 1)
2346 inform (input_location, "saw %d %<template<>%>, need %d for "
2347 "specializing a member function template",
2348 header_count, template_count + 1);
2349 else
2350 print_candidates (orig_fns);
2351 return error_mark_node;
2352 }
2353 else if ((templates && TREE_CHAIN (templates))
2354 || (candidates && TREE_CHAIN (candidates))
2355 || (templates && candidates))
2356 {
2357 error ("ambiguous template specialization %qD for %q+D",
2358 template_id, decl);
2359 candidates = chainon (candidates, templates);
2360 print_candidates (candidates);
2361 return error_mark_node;
2362 }
2363
2364 /* We have one, and exactly one, match. */
2365 if (candidates)
2366 {
2367 tree fn = TREE_VALUE (candidates);
2368 *targs_out = copy_node (DECL_TI_ARGS (fn));
2369
2370 // Propagate the candidate's constraints to the declaration.
2371 set_constraints (decl, get_constraints (fn));
2372
2373 /* DECL is a re-declaration or partial instantiation of a template
2374 function. */
2375 if (TREE_CODE (fn) == TEMPLATE_DECL)
2376 return fn;
2377 /* It was a specialization of an ordinary member function in a
2378 template class. */
2379 return DECL_TI_TEMPLATE (fn);
2380 }
2381
2382 /* It was a specialization of a template. */
2383 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2384 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2385 {
2386 *targs_out = copy_node (targs);
2387 SET_TMPL_ARGS_LEVEL (*targs_out,
2388 TMPL_ARGS_DEPTH (*targs_out),
2389 TREE_PURPOSE (templates));
2390 }
2391 else
2392 *targs_out = TREE_PURPOSE (templates);
2393 return TREE_VALUE (templates);
2394 }
2395
2396 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2397 but with the default argument values filled in from those in the
2398 TMPL_TYPES. */
2399
2400 static tree
2401 copy_default_args_to_explicit_spec_1 (tree spec_types,
2402 tree tmpl_types)
2403 {
2404 tree new_spec_types;
2405
2406 if (!spec_types)
2407 return NULL_TREE;
2408
2409 if (spec_types == void_list_node)
2410 return void_list_node;
2411
2412 /* Substitute into the rest of the list. */
2413 new_spec_types =
2414 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2415 TREE_CHAIN (tmpl_types));
2416
2417 /* Add the default argument for this parameter. */
2418 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2419 TREE_VALUE (spec_types),
2420 new_spec_types);
2421 }
2422
2423 /* DECL is an explicit specialization. Replicate default arguments
2424 from the template it specializes. (That way, code like:
2425
2426 template <class T> void f(T = 3);
2427 template <> void f(double);
2428 void g () { f (); }
2429
2430 works, as required.) An alternative approach would be to look up
2431 the correct default arguments at the call-site, but this approach
2432 is consistent with how implicit instantiations are handled. */
2433
2434 static void
2435 copy_default_args_to_explicit_spec (tree decl)
2436 {
2437 tree tmpl;
2438 tree spec_types;
2439 tree tmpl_types;
2440 tree new_spec_types;
2441 tree old_type;
2442 tree new_type;
2443 tree t;
2444 tree object_type = NULL_TREE;
2445 tree in_charge = NULL_TREE;
2446 tree vtt = NULL_TREE;
2447
2448 /* See if there's anything we need to do. */
2449 tmpl = DECL_TI_TEMPLATE (decl);
2450 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2451 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2452 if (TREE_PURPOSE (t))
2453 break;
2454 if (!t)
2455 return;
2456
2457 old_type = TREE_TYPE (decl);
2458 spec_types = TYPE_ARG_TYPES (old_type);
2459
2460 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2461 {
2462 /* Remove the this pointer, but remember the object's type for
2463 CV quals. */
2464 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2465 spec_types = TREE_CHAIN (spec_types);
2466 tmpl_types = TREE_CHAIN (tmpl_types);
2467
2468 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2469 {
2470 /* DECL may contain more parameters than TMPL due to the extra
2471 in-charge parameter in constructors and destructors. */
2472 in_charge = spec_types;
2473 spec_types = TREE_CHAIN (spec_types);
2474 }
2475 if (DECL_HAS_VTT_PARM_P (decl))
2476 {
2477 vtt = spec_types;
2478 spec_types = TREE_CHAIN (spec_types);
2479 }
2480 }
2481
2482 /* Compute the merged default arguments. */
2483 new_spec_types =
2484 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2485
2486 /* Compute the new FUNCTION_TYPE. */
2487 if (object_type)
2488 {
2489 if (vtt)
2490 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2491 TREE_VALUE (vtt),
2492 new_spec_types);
2493
2494 if (in_charge)
2495 /* Put the in-charge parameter back. */
2496 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2497 TREE_VALUE (in_charge),
2498 new_spec_types);
2499
2500 new_type = build_method_type_directly (object_type,
2501 TREE_TYPE (old_type),
2502 new_spec_types);
2503 }
2504 else
2505 new_type = build_function_type (TREE_TYPE (old_type),
2506 new_spec_types);
2507 new_type = cp_build_type_attribute_variant (new_type,
2508 TYPE_ATTRIBUTES (old_type));
2509 new_type = build_exception_variant (new_type,
2510 TYPE_RAISES_EXCEPTIONS (old_type));
2511
2512 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2513 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2514
2515 TREE_TYPE (decl) = new_type;
2516 }
2517
2518 /* Return the number of template headers we expect to see for a definition
2519 or specialization of CTYPE or one of its non-template members. */
2520
2521 int
2522 num_template_headers_for_class (tree ctype)
2523 {
2524 int num_templates = 0;
2525
2526 while (ctype && CLASS_TYPE_P (ctype))
2527 {
2528 /* You're supposed to have one `template <...>' for every
2529 template class, but you don't need one for a full
2530 specialization. For example:
2531
2532 template <class T> struct S{};
2533 template <> struct S<int> { void f(); };
2534 void S<int>::f () {}
2535
2536 is correct; there shouldn't be a `template <>' for the
2537 definition of `S<int>::f'. */
2538 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2539 /* If CTYPE does not have template information of any
2540 kind, then it is not a template, nor is it nested
2541 within a template. */
2542 break;
2543 if (explicit_class_specialization_p (ctype))
2544 break;
2545 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2546 ++num_templates;
2547
2548 ctype = TYPE_CONTEXT (ctype);
2549 }
2550
2551 return num_templates;
2552 }
2553
2554 /* Do a simple sanity check on the template headers that precede the
2555 variable declaration DECL. */
2556
2557 void
2558 check_template_variable (tree decl)
2559 {
2560 tree ctx = CP_DECL_CONTEXT (decl);
2561 int wanted = num_template_headers_for_class (ctx);
2562 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2563 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2564 {
2565 if (cxx_dialect < cxx14)
2566 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2567 "variable templates only available with "
2568 "-std=c++14 or -std=gnu++14");
2569
2570 // Namespace-scope variable templates should have a template header.
2571 ++wanted;
2572 }
2573 if (template_header_count > wanted)
2574 {
2575 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2576 "too many template headers for %D (should be %d)",
2577 decl, wanted);
2578 if (warned && CLASS_TYPE_P (ctx)
2579 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2580 inform (DECL_SOURCE_LOCATION (decl),
2581 "members of an explicitly specialized class are defined "
2582 "without a template header");
2583 }
2584 }
2585
2586 /* Check to see if the function just declared, as indicated in
2587 DECLARATOR, and in DECL, is a specialization of a function
2588 template. We may also discover that the declaration is an explicit
2589 instantiation at this point.
2590
2591 Returns DECL, or an equivalent declaration that should be used
2592 instead if all goes well. Issues an error message if something is
2593 amiss. Returns error_mark_node if the error is not easily
2594 recoverable.
2595
2596 FLAGS is a bitmask consisting of the following flags:
2597
2598 2: The function has a definition.
2599 4: The function is a friend.
2600
2601 The TEMPLATE_COUNT is the number of references to qualifying
2602 template classes that appeared in the name of the function. For
2603 example, in
2604
2605 template <class T> struct S { void f(); };
2606 void S<int>::f();
2607
2608 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2609 classes are not counted in the TEMPLATE_COUNT, so that in
2610
2611 template <class T> struct S {};
2612 template <> struct S<int> { void f(); }
2613 template <> void S<int>::f();
2614
2615 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2616 invalid; there should be no template <>.)
2617
2618 If the function is a specialization, it is marked as such via
2619 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2620 is set up correctly, and it is added to the list of specializations
2621 for that template. */
2622
2623 tree
2624 check_explicit_specialization (tree declarator,
2625 tree decl,
2626 int template_count,
2627 int flags)
2628 {
2629 int have_def = flags & 2;
2630 int is_friend = flags & 4;
2631 bool is_concept = flags & 8;
2632 int specialization = 0;
2633 int explicit_instantiation = 0;
2634 int member_specialization = 0;
2635 tree ctype = DECL_CLASS_CONTEXT (decl);
2636 tree dname = DECL_NAME (decl);
2637 tmpl_spec_kind tsk;
2638
2639 if (is_friend)
2640 {
2641 if (!processing_specialization)
2642 tsk = tsk_none;
2643 else
2644 tsk = tsk_excessive_parms;
2645 }
2646 else
2647 tsk = current_tmpl_spec_kind (template_count);
2648
2649 switch (tsk)
2650 {
2651 case tsk_none:
2652 if (processing_specialization && !VAR_P (decl))
2653 {
2654 specialization = 1;
2655 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2656 }
2657 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2658 {
2659 if (is_friend)
2660 /* This could be something like:
2661
2662 template <class T> void f(T);
2663 class S { friend void f<>(int); } */
2664 specialization = 1;
2665 else
2666 {
2667 /* This case handles bogus declarations like template <>
2668 template <class T> void f<int>(); */
2669
2670 error ("template-id %qD in declaration of primary template",
2671 declarator);
2672 return decl;
2673 }
2674 }
2675 break;
2676
2677 case tsk_invalid_member_spec:
2678 /* The error has already been reported in
2679 check_specialization_scope. */
2680 return error_mark_node;
2681
2682 case tsk_invalid_expl_inst:
2683 error ("template parameter list used in explicit instantiation");
2684
2685 /* Fall through. */
2686
2687 case tsk_expl_inst:
2688 if (have_def)
2689 error ("definition provided for explicit instantiation");
2690
2691 explicit_instantiation = 1;
2692 break;
2693
2694 case tsk_excessive_parms:
2695 case tsk_insufficient_parms:
2696 if (tsk == tsk_excessive_parms)
2697 error ("too many template parameter lists in declaration of %qD",
2698 decl);
2699 else if (template_header_count)
2700 error("too few template parameter lists in declaration of %qD", decl);
2701 else
2702 error("explicit specialization of %qD must be introduced by "
2703 "%<template <>%>", decl);
2704
2705 /* Fall through. */
2706 case tsk_expl_spec:
2707 if (is_concept)
2708 error ("explicit specialization declared %<concept%>");
2709
2710 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2711 /* In cases like template<> constexpr bool v = true;
2712 We'll give an error in check_template_variable. */
2713 break;
2714
2715 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2716 if (ctype)
2717 member_specialization = 1;
2718 else
2719 specialization = 1;
2720 break;
2721
2722 case tsk_template:
2723 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2724 {
2725 /* This case handles bogus declarations like template <>
2726 template <class T> void f<int>(); */
2727
2728 if (!uses_template_parms (declarator))
2729 error ("template-id %qD in declaration of primary template",
2730 declarator);
2731 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2732 {
2733 /* Partial specialization of variable template. */
2734 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2735 specialization = 1;
2736 goto ok;
2737 }
2738 else if (cxx_dialect < cxx14)
2739 error ("non-type partial specialization %qD "
2740 "is not allowed", declarator);
2741 else
2742 error ("non-class, non-variable partial specialization %qD "
2743 "is not allowed", declarator);
2744 return decl;
2745 ok:;
2746 }
2747
2748 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2749 /* This is a specialization of a member template, without
2750 specialization the containing class. Something like:
2751
2752 template <class T> struct S {
2753 template <class U> void f (U);
2754 };
2755 template <> template <class U> void S<int>::f(U) {}
2756
2757 That's a specialization -- but of the entire template. */
2758 specialization = 1;
2759 break;
2760
2761 default:
2762 gcc_unreachable ();
2763 }
2764
2765 if ((specialization || member_specialization)
2766 /* This doesn't apply to variable templates. */
2767 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2768 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2769 {
2770 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2771 for (; t; t = TREE_CHAIN (t))
2772 if (TREE_PURPOSE (t))
2773 {
2774 permerror (input_location,
2775 "default argument specified in explicit specialization");
2776 break;
2777 }
2778 }
2779
2780 if (specialization || member_specialization || explicit_instantiation)
2781 {
2782 tree tmpl = NULL_TREE;
2783 tree targs = NULL_TREE;
2784 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2785
2786 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2787 if (!was_template_id)
2788 {
2789 tree fns;
2790
2791 gcc_assert (identifier_p (declarator));
2792 if (ctype)
2793 fns = dname;
2794 else
2795 {
2796 /* If there is no class context, the explicit instantiation
2797 must be at namespace scope. */
2798 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2799
2800 /* Find the namespace binding, using the declaration
2801 context. */
2802 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2803 false, true);
2804 if (fns == error_mark_node || !is_overloaded_fn (fns))
2805 {
2806 error ("%qD is not a template function", dname);
2807 fns = error_mark_node;
2808 }
2809 }
2810
2811 declarator = lookup_template_function (fns, NULL_TREE);
2812 }
2813
2814 if (declarator == error_mark_node)
2815 return error_mark_node;
2816
2817 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2818 {
2819 if (!explicit_instantiation)
2820 /* A specialization in class scope. This is invalid,
2821 but the error will already have been flagged by
2822 check_specialization_scope. */
2823 return error_mark_node;
2824 else
2825 {
2826 /* It's not valid to write an explicit instantiation in
2827 class scope, e.g.:
2828
2829 class C { template void f(); }
2830
2831 This case is caught by the parser. However, on
2832 something like:
2833
2834 template class C { void f(); };
2835
2836 (which is invalid) we can get here. The error will be
2837 issued later. */
2838 ;
2839 }
2840
2841 return decl;
2842 }
2843 else if (ctype != NULL_TREE
2844 && (identifier_p (TREE_OPERAND (declarator, 0))))
2845 {
2846 // We'll match variable templates in start_decl.
2847 if (VAR_P (decl))
2848 return decl;
2849
2850 /* Find the list of functions in ctype that have the same
2851 name as the declared function. */
2852 tree name = TREE_OPERAND (declarator, 0);
2853 tree fns = NULL_TREE;
2854 int idx;
2855
2856 if (constructor_name_p (name, ctype))
2857 {
2858 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2859
2860 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2861 : !CLASSTYPE_DESTRUCTORS (ctype))
2862 {
2863 /* From [temp.expl.spec]:
2864
2865 If such an explicit specialization for the member
2866 of a class template names an implicitly-declared
2867 special member function (clause _special_), the
2868 program is ill-formed.
2869
2870 Similar language is found in [temp.explicit]. */
2871 error ("specialization of implicitly-declared special member function");
2872 return error_mark_node;
2873 }
2874
2875 name = is_constructor ? ctor_identifier : dtor_identifier;
2876 }
2877
2878 if (!DECL_CONV_FN_P (decl))
2879 {
2880 idx = lookup_fnfields_1 (ctype, name);
2881 if (idx >= 0)
2882 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2883 }
2884 else
2885 {
2886 vec<tree, va_gc> *methods;
2887 tree ovl;
2888
2889 /* For a type-conversion operator, we cannot do a
2890 name-based lookup. We might be looking for `operator
2891 int' which will be a specialization of `operator T'.
2892 So, we find *all* the conversion operators, and then
2893 select from them. */
2894 fns = NULL_TREE;
2895
2896 methods = CLASSTYPE_METHOD_VEC (ctype);
2897 if (methods)
2898 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2899 methods->iterate (idx, &ovl);
2900 ++idx)
2901 {
2902 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2903 /* There are no more conversion functions. */
2904 break;
2905
2906 /* Glue all these conversion functions together
2907 with those we already have. */
2908 for (; ovl; ovl = OVL_NEXT (ovl))
2909 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2910 }
2911 }
2912
2913 if (fns == NULL_TREE)
2914 {
2915 error ("no member function %qD declared in %qT", name, ctype);
2916 return error_mark_node;
2917 }
2918 else
2919 TREE_OPERAND (declarator, 0) = fns;
2920 }
2921
2922 /* Figure out what exactly is being specialized at this point.
2923 Note that for an explicit instantiation, even one for a
2924 member function, we cannot tell apriori whether the
2925 instantiation is for a member template, or just a member
2926 function of a template class. Even if a member template is
2927 being instantiated, the member template arguments may be
2928 elided if they can be deduced from the rest of the
2929 declaration. */
2930 tmpl = determine_specialization (declarator, decl,
2931 &targs,
2932 member_specialization,
2933 template_count,
2934 tsk);
2935
2936 if (!tmpl || tmpl == error_mark_node)
2937 /* We couldn't figure out what this declaration was
2938 specializing. */
2939 return error_mark_node;
2940 else
2941 {
2942 if (!ctype && !was_template_id
2943 && (specialization || member_specialization
2944 || explicit_instantiation)
2945 && !is_associated_namespace (CP_DECL_CONTEXT (decl),
2946 CP_DECL_CONTEXT (tmpl)))
2947 error ("%qD is not declared in %qD",
2948 tmpl, current_namespace);
2949
2950 tree gen_tmpl = most_general_template (tmpl);
2951
2952 if (explicit_instantiation)
2953 {
2954 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2955 is done by do_decl_instantiation later. */
2956
2957 int arg_depth = TMPL_ARGS_DEPTH (targs);
2958 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2959
2960 if (arg_depth > parm_depth)
2961 {
2962 /* If TMPL is not the most general template (for
2963 example, if TMPL is a friend template that is
2964 injected into namespace scope), then there will
2965 be too many levels of TARGS. Remove some of them
2966 here. */
2967 int i;
2968 tree new_targs;
2969
2970 new_targs = make_tree_vec (parm_depth);
2971 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2972 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2973 = TREE_VEC_ELT (targs, i);
2974 targs = new_targs;
2975 }
2976
2977 return instantiate_template (tmpl, targs, tf_error);
2978 }
2979
2980 /* If we thought that the DECL was a member function, but it
2981 turns out to be specializing a static member function,
2982 make DECL a static member function as well. */
2983 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2984 && DECL_STATIC_FUNCTION_P (tmpl)
2985 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2986 revert_static_member_fn (decl);
2987
2988 /* If this is a specialization of a member template of a
2989 template class, we want to return the TEMPLATE_DECL, not
2990 the specialization of it. */
2991 if (tsk == tsk_template && !was_template_id)
2992 {
2993 tree result = DECL_TEMPLATE_RESULT (tmpl);
2994 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2995 DECL_INITIAL (result) = NULL_TREE;
2996 if (have_def)
2997 {
2998 tree parm;
2999 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3000 DECL_SOURCE_LOCATION (result)
3001 = DECL_SOURCE_LOCATION (decl);
3002 /* We want to use the argument list specified in the
3003 definition, not in the original declaration. */
3004 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3005 for (parm = DECL_ARGUMENTS (result); parm;
3006 parm = DECL_CHAIN (parm))
3007 DECL_CONTEXT (parm) = result;
3008 }
3009 return register_specialization (tmpl, gen_tmpl, targs,
3010 is_friend, 0);
3011 }
3012
3013 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3014 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3015
3016 if (was_template_id)
3017 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3018
3019 /* Inherit default function arguments from the template
3020 DECL is specializing. */
3021 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3022 copy_default_args_to_explicit_spec (decl);
3023
3024 /* This specialization has the same protection as the
3025 template it specializes. */
3026 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3027 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3028
3029 /* 7.1.1-1 [dcl.stc]
3030
3031 A storage-class-specifier shall not be specified in an
3032 explicit specialization...
3033
3034 The parser rejects these, so unless action is taken here,
3035 explicit function specializations will always appear with
3036 global linkage.
3037
3038 The action recommended by the C++ CWG in response to C++
3039 defect report 605 is to make the storage class and linkage
3040 of the explicit specialization match the templated function:
3041
3042 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3043 */
3044 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3045 {
3046 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3047 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3048
3049 /* A concept cannot be specialized. */
3050 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3051 {
3052 error ("explicit specialization of function concept %qD",
3053 gen_tmpl);
3054 return error_mark_node;
3055 }
3056
3057 /* This specialization has the same linkage and visibility as
3058 the function template it specializes. */
3059 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3060 if (! TREE_PUBLIC (decl))
3061 {
3062 DECL_INTERFACE_KNOWN (decl) = 1;
3063 DECL_NOT_REALLY_EXTERN (decl) = 1;
3064 }
3065 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3066 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3067 {
3068 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3069 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3070 }
3071 }
3072
3073 /* If DECL is a friend declaration, declared using an
3074 unqualified name, the namespace associated with DECL may
3075 have been set incorrectly. For example, in:
3076
3077 template <typename T> void f(T);
3078 namespace N {
3079 struct S { friend void f<int>(int); }
3080 }
3081
3082 we will have set the DECL_CONTEXT for the friend
3083 declaration to N, rather than to the global namespace. */
3084 if (DECL_NAMESPACE_SCOPE_P (decl))
3085 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3086
3087 if (is_friend && !have_def)
3088 /* This is not really a declaration of a specialization.
3089 It's just the name of an instantiation. But, it's not
3090 a request for an instantiation, either. */
3091 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3092 else if (TREE_CODE (decl) == FUNCTION_DECL)
3093 /* A specialization is not necessarily COMDAT. */
3094 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3095 && DECL_DECLARED_INLINE_P (decl));
3096 else if (VAR_P (decl))
3097 DECL_COMDAT (decl) = false;
3098
3099 /* If this is a full specialization, register it so that we can find
3100 it again. Partial specializations will be registered in
3101 process_partial_specialization. */
3102 if (!processing_template_decl)
3103 decl = register_specialization (decl, gen_tmpl, targs,
3104 is_friend, 0);
3105
3106 /* A 'structor should already have clones. */
3107 gcc_assert (decl == error_mark_node
3108 || variable_template_p (tmpl)
3109 || !(DECL_CONSTRUCTOR_P (decl)
3110 || DECL_DESTRUCTOR_P (decl))
3111 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3112 }
3113 }
3114
3115 return decl;
3116 }
3117
3118 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3119 parameters. These are represented in the same format used for
3120 DECL_TEMPLATE_PARMS. */
3121
3122 int
3123 comp_template_parms (const_tree parms1, const_tree parms2)
3124 {
3125 const_tree p1;
3126 const_tree p2;
3127
3128 if (parms1 == parms2)
3129 return 1;
3130
3131 for (p1 = parms1, p2 = parms2;
3132 p1 != NULL_TREE && p2 != NULL_TREE;
3133 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3134 {
3135 tree t1 = TREE_VALUE (p1);
3136 tree t2 = TREE_VALUE (p2);
3137 int i;
3138
3139 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3140 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3141
3142 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3143 return 0;
3144
3145 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3146 {
3147 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3148 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3149
3150 /* If either of the template parameters are invalid, assume
3151 they match for the sake of error recovery. */
3152 if (error_operand_p (parm1) || error_operand_p (parm2))
3153 return 1;
3154
3155 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3156 return 0;
3157
3158 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3159 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3160 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3161 continue;
3162 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3163 return 0;
3164 }
3165 }
3166
3167 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3168 /* One set of parameters has more parameters lists than the
3169 other. */
3170 return 0;
3171
3172 return 1;
3173 }
3174
3175 /* Determine whether PARM is a parameter pack. */
3176
3177 bool
3178 template_parameter_pack_p (const_tree parm)
3179 {
3180 /* Determine if we have a non-type template parameter pack. */
3181 if (TREE_CODE (parm) == PARM_DECL)
3182 return (DECL_TEMPLATE_PARM_P (parm)
3183 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3184 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3185 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3186
3187 /* If this is a list of template parameters, we could get a
3188 TYPE_DECL or a TEMPLATE_DECL. */
3189 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3190 parm = TREE_TYPE (parm);
3191
3192 /* Otherwise it must be a type template parameter. */
3193 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3194 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3195 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3196 }
3197
3198 /* Determine if T is a function parameter pack. */
3199
3200 bool
3201 function_parameter_pack_p (const_tree t)
3202 {
3203 if (t && TREE_CODE (t) == PARM_DECL)
3204 return DECL_PACK_P (t);
3205 return false;
3206 }
3207
3208 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3209 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3210
3211 tree
3212 get_function_template_decl (const_tree primary_func_tmpl_inst)
3213 {
3214 if (! primary_func_tmpl_inst
3215 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3216 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3217 return NULL;
3218
3219 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3220 }
3221
3222 /* Return true iff the function parameter PARAM_DECL was expanded
3223 from the function parameter pack PACK. */
3224
3225 bool
3226 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3227 {
3228 if (DECL_ARTIFICIAL (param_decl)
3229 || !function_parameter_pack_p (pack))
3230 return false;
3231
3232 /* The parameter pack and its pack arguments have the same
3233 DECL_PARM_INDEX. */
3234 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3235 }
3236
3237 /* Determine whether ARGS describes a variadic template args list,
3238 i.e., one that is terminated by a template argument pack. */
3239
3240 static bool
3241 template_args_variadic_p (tree args)
3242 {
3243 int nargs;
3244 tree last_parm;
3245
3246 if (args == NULL_TREE)
3247 return false;
3248
3249 args = INNERMOST_TEMPLATE_ARGS (args);
3250 nargs = TREE_VEC_LENGTH (args);
3251
3252 if (nargs == 0)
3253 return false;
3254
3255 last_parm = TREE_VEC_ELT (args, nargs - 1);
3256
3257 return ARGUMENT_PACK_P (last_parm);
3258 }
3259
3260 /* Generate a new name for the parameter pack name NAME (an
3261 IDENTIFIER_NODE) that incorporates its */
3262
3263 static tree
3264 make_ith_pack_parameter_name (tree name, int i)
3265 {
3266 /* Munge the name to include the parameter index. */
3267 #define NUMBUF_LEN 128
3268 char numbuf[NUMBUF_LEN];
3269 char* newname;
3270 int newname_len;
3271
3272 if (name == NULL_TREE)
3273 return name;
3274 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3275 newname_len = IDENTIFIER_LENGTH (name)
3276 + strlen (numbuf) + 2;
3277 newname = (char*)alloca (newname_len);
3278 snprintf (newname, newname_len,
3279 "%s#%i", IDENTIFIER_POINTER (name), i);
3280 return get_identifier (newname);
3281 }
3282
3283 /* Return true if T is a primary function, class or alias template
3284 instantiation. */
3285
3286 bool
3287 primary_template_instantiation_p (const_tree t)
3288 {
3289 if (!t)
3290 return false;
3291
3292 if (TREE_CODE (t) == FUNCTION_DECL)
3293 return DECL_LANG_SPECIFIC (t)
3294 && DECL_TEMPLATE_INSTANTIATION (t)
3295 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3296 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3297 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3299 else if (alias_template_specialization_p (t))
3300 return true;
3301 return false;
3302 }
3303
3304 /* Return true if PARM is a template template parameter. */
3305
3306 bool
3307 template_template_parameter_p (const_tree parm)
3308 {
3309 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3310 }
3311
3312 /* Return true iff PARM is a DECL representing a type template
3313 parameter. */
3314
3315 bool
3316 template_type_parameter_p (const_tree parm)
3317 {
3318 return (parm
3319 && (TREE_CODE (parm) == TYPE_DECL
3320 || TREE_CODE (parm) == TEMPLATE_DECL)
3321 && DECL_TEMPLATE_PARM_P (parm));
3322 }
3323
3324 /* Return the template parameters of T if T is a
3325 primary template instantiation, NULL otherwise. */
3326
3327 tree
3328 get_primary_template_innermost_parameters (const_tree t)
3329 {
3330 tree parms = NULL, template_info = NULL;
3331
3332 if ((template_info = get_template_info (t))
3333 && primary_template_instantiation_p (t))
3334 parms = INNERMOST_TEMPLATE_PARMS
3335 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3336
3337 return parms;
3338 }
3339
3340 /* Return the template parameters of the LEVELth level from the full list
3341 of template parameters PARMS. */
3342
3343 tree
3344 get_template_parms_at_level (tree parms, int level)
3345 {
3346 tree p;
3347 if (!parms
3348 || TREE_CODE (parms) != TREE_LIST
3349 || level > TMPL_PARMS_DEPTH (parms))
3350 return NULL_TREE;
3351
3352 for (p = parms; p; p = TREE_CHAIN (p))
3353 if (TMPL_PARMS_DEPTH (p) == level)
3354 return p;
3355
3356 return NULL_TREE;
3357 }
3358
3359 /* Returns the template arguments of T if T is a template instantiation,
3360 NULL otherwise. */
3361
3362 tree
3363 get_template_innermost_arguments (const_tree t)
3364 {
3365 tree args = NULL, template_info = NULL;
3366
3367 if ((template_info = get_template_info (t))
3368 && TI_ARGS (template_info))
3369 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3370
3371 return args;
3372 }
3373
3374 /* Return the argument pack elements of T if T is a template argument pack,
3375 NULL otherwise. */
3376
3377 tree
3378 get_template_argument_pack_elems (const_tree t)
3379 {
3380 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3381 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3382 return NULL;
3383
3384 return ARGUMENT_PACK_ARGS (t);
3385 }
3386
3387 /* Structure used to track the progress of find_parameter_packs_r. */
3388 struct find_parameter_pack_data
3389 {
3390 /* TREE_LIST that will contain all of the parameter packs found by
3391 the traversal. */
3392 tree* parameter_packs;
3393
3394 /* Set of AST nodes that have been visited by the traversal. */
3395 hash_set<tree> *visited;
3396
3397 /* True iff we're making a type pack expansion. */
3398 bool type_pack_expansion_p;
3399 };
3400
3401 /* Identifies all of the argument packs that occur in a template
3402 argument and appends them to the TREE_LIST inside DATA, which is a
3403 find_parameter_pack_data structure. This is a subroutine of
3404 make_pack_expansion and uses_parameter_packs. */
3405 static tree
3406 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3407 {
3408 tree t = *tp;
3409 struct find_parameter_pack_data* ppd =
3410 (struct find_parameter_pack_data*)data;
3411 bool parameter_pack_p = false;
3412
3413 /* Handle type aliases/typedefs. */
3414 if (TYPE_ALIAS_P (t))
3415 {
3416 if (TYPE_TEMPLATE_INFO (t))
3417 cp_walk_tree (&TYPE_TI_ARGS (t),
3418 &find_parameter_packs_r,
3419 ppd, ppd->visited);
3420 *walk_subtrees = 0;
3421 return NULL_TREE;
3422 }
3423
3424 /* Identify whether this is a parameter pack or not. */
3425 switch (TREE_CODE (t))
3426 {
3427 case TEMPLATE_PARM_INDEX:
3428 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3429 parameter_pack_p = true;
3430 break;
3431
3432 case TEMPLATE_TYPE_PARM:
3433 t = TYPE_MAIN_VARIANT (t);
3434 case TEMPLATE_TEMPLATE_PARM:
3435 /* If the placeholder appears in the decl-specifier-seq of a function
3436 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3437 is a pack expansion, the invented template parameter is a template
3438 parameter pack. */
3439 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3440 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3441 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3442 parameter_pack_p = true;
3443 break;
3444
3445 case FIELD_DECL:
3446 case PARM_DECL:
3447 if (DECL_PACK_P (t))
3448 {
3449 /* We don't want to walk into the type of a PARM_DECL,
3450 because we don't want to see the type parameter pack. */
3451 *walk_subtrees = 0;
3452 parameter_pack_p = true;
3453 }
3454 break;
3455
3456 /* Look through a lambda capture proxy to the field pack. */
3457 case VAR_DECL:
3458 if (DECL_HAS_VALUE_EXPR_P (t))
3459 {
3460 tree v = DECL_VALUE_EXPR (t);
3461 cp_walk_tree (&v,
3462 &find_parameter_packs_r,
3463 ppd, ppd->visited);
3464 *walk_subtrees = 0;
3465 }
3466 else if (variable_template_specialization_p (t))
3467 {
3468 cp_walk_tree (&DECL_TI_ARGS (t),
3469 find_parameter_packs_r,
3470 ppd, ppd->visited);
3471 *walk_subtrees = 0;
3472 }
3473 break;
3474
3475 case BASES:
3476 parameter_pack_p = true;
3477 break;
3478 default:
3479 /* Not a parameter pack. */
3480 break;
3481 }
3482
3483 if (parameter_pack_p)
3484 {
3485 /* Add this parameter pack to the list. */
3486 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3487 }
3488
3489 if (TYPE_P (t))
3490 cp_walk_tree (&TYPE_CONTEXT (t),
3491 &find_parameter_packs_r, ppd, ppd->visited);
3492
3493 /* This switch statement will return immediately if we don't find a
3494 parameter pack. */
3495 switch (TREE_CODE (t))
3496 {
3497 case TEMPLATE_PARM_INDEX:
3498 return NULL_TREE;
3499
3500 case BOUND_TEMPLATE_TEMPLATE_PARM:
3501 /* Check the template itself. */
3502 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3503 &find_parameter_packs_r, ppd, ppd->visited);
3504 /* Check the template arguments. */
3505 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3506 ppd->visited);
3507 *walk_subtrees = 0;
3508 return NULL_TREE;
3509
3510 case TEMPLATE_TYPE_PARM:
3511 case TEMPLATE_TEMPLATE_PARM:
3512 return NULL_TREE;
3513
3514 case PARM_DECL:
3515 return NULL_TREE;
3516
3517 case RECORD_TYPE:
3518 if (TYPE_PTRMEMFUNC_P (t))
3519 return NULL_TREE;
3520 /* Fall through. */
3521
3522 case UNION_TYPE:
3523 case ENUMERAL_TYPE:
3524 if (TYPE_TEMPLATE_INFO (t))
3525 cp_walk_tree (&TYPE_TI_ARGS (t),
3526 &find_parameter_packs_r, ppd, ppd->visited);
3527
3528 *walk_subtrees = 0;
3529 return NULL_TREE;
3530
3531 case CONSTRUCTOR:
3532 case TEMPLATE_DECL:
3533 cp_walk_tree (&TREE_TYPE (t),
3534 &find_parameter_packs_r, ppd, ppd->visited);
3535 return NULL_TREE;
3536
3537 case TYPENAME_TYPE:
3538 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3539 ppd, ppd->visited);
3540 *walk_subtrees = 0;
3541 return NULL_TREE;
3542
3543 case TYPE_PACK_EXPANSION:
3544 case EXPR_PACK_EXPANSION:
3545 *walk_subtrees = 0;
3546 return NULL_TREE;
3547
3548 case INTEGER_TYPE:
3549 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3550 ppd, ppd->visited);
3551 *walk_subtrees = 0;
3552 return NULL_TREE;
3553
3554 case IDENTIFIER_NODE:
3555 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3556 ppd->visited);
3557 *walk_subtrees = 0;
3558 return NULL_TREE;
3559
3560 case DECLTYPE_TYPE:
3561 {
3562 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3563 type_pack_expansion_p to false so that any placeholders
3564 within the expression don't get marked as parameter packs. */
3565 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3566 ppd->type_pack_expansion_p = false;
3567 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3568 ppd, ppd->visited);
3569 ppd->type_pack_expansion_p = type_pack_expansion_p;
3570 *walk_subtrees = 0;
3571 return NULL_TREE;
3572 }
3573
3574 default:
3575 return NULL_TREE;
3576 }
3577
3578 return NULL_TREE;
3579 }
3580
3581 /* Determines if the expression or type T uses any parameter packs. */
3582 bool
3583 uses_parameter_packs (tree t)
3584 {
3585 tree parameter_packs = NULL_TREE;
3586 struct find_parameter_pack_data ppd;
3587 ppd.parameter_packs = &parameter_packs;
3588 ppd.visited = new hash_set<tree>;
3589 ppd.type_pack_expansion_p = false;
3590 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3591 delete ppd.visited;
3592 return parameter_packs != NULL_TREE;
3593 }
3594
3595 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3596 representation a base-class initializer into a parameter pack
3597 expansion. If all goes well, the resulting node will be an
3598 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3599 respectively. */
3600 tree
3601 make_pack_expansion (tree arg)
3602 {
3603 tree result;
3604 tree parameter_packs = NULL_TREE;
3605 bool for_types = false;
3606 struct find_parameter_pack_data ppd;
3607
3608 if (!arg || arg == error_mark_node)
3609 return arg;
3610
3611 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3612 {
3613 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3614 class initializer. In this case, the TREE_PURPOSE will be a
3615 _TYPE node (representing the base class expansion we're
3616 initializing) and the TREE_VALUE will be a TREE_LIST
3617 containing the initialization arguments.
3618
3619 The resulting expansion looks somewhat different from most
3620 expansions. Rather than returning just one _EXPANSION, we
3621 return a TREE_LIST whose TREE_PURPOSE is a
3622 TYPE_PACK_EXPANSION containing the bases that will be
3623 initialized. The TREE_VALUE will be identical to the
3624 original TREE_VALUE, which is a list of arguments that will
3625 be passed to each base. We do not introduce any new pack
3626 expansion nodes into the TREE_VALUE (although it is possible
3627 that some already exist), because the TREE_PURPOSE and
3628 TREE_VALUE all need to be expanded together with the same
3629 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3630 resulting TREE_PURPOSE will mention the parameter packs in
3631 both the bases and the arguments to the bases. */
3632 tree purpose;
3633 tree value;
3634 tree parameter_packs = NULL_TREE;
3635
3636 /* Determine which parameter packs will be used by the base
3637 class expansion. */
3638 ppd.visited = new hash_set<tree>;
3639 ppd.parameter_packs = &parameter_packs;
3640 ppd.type_pack_expansion_p = true;
3641 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3642 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3643 &ppd, ppd.visited);
3644
3645 if (parameter_packs == NULL_TREE)
3646 {
3647 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3648 delete ppd.visited;
3649 return error_mark_node;
3650 }
3651
3652 if (TREE_VALUE (arg) != void_type_node)
3653 {
3654 /* Collect the sets of parameter packs used in each of the
3655 initialization arguments. */
3656 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3657 {
3658 /* Determine which parameter packs will be expanded in this
3659 argument. */
3660 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3661 &ppd, ppd.visited);
3662 }
3663 }
3664
3665 delete ppd.visited;
3666
3667 /* Create the pack expansion type for the base type. */
3668 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3669 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3670 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3671
3672 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3673 they will rarely be compared to anything. */
3674 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3675
3676 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3677 }
3678
3679 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3680 for_types = true;
3681
3682 /* Build the PACK_EXPANSION_* node. */
3683 result = for_types
3684 ? cxx_make_type (TYPE_PACK_EXPANSION)
3685 : make_node (EXPR_PACK_EXPANSION);
3686 SET_PACK_EXPANSION_PATTERN (result, arg);
3687 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3688 {
3689 /* Propagate type and const-expression information. */
3690 TREE_TYPE (result) = TREE_TYPE (arg);
3691 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3692 }
3693 else
3694 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3695 they will rarely be compared to anything. */
3696 SET_TYPE_STRUCTURAL_EQUALITY (result);
3697
3698 /* Determine which parameter packs will be expanded. */
3699 ppd.parameter_packs = &parameter_packs;
3700 ppd.visited = new hash_set<tree>;
3701 ppd.type_pack_expansion_p = TYPE_P (arg);
3702 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3703 delete ppd.visited;
3704
3705 /* Make sure we found some parameter packs. */
3706 if (parameter_packs == NULL_TREE)
3707 {
3708 if (TYPE_P (arg))
3709 error ("expansion pattern %<%T%> contains no argument packs", arg);
3710 else
3711 error ("expansion pattern %<%E%> contains no argument packs", arg);
3712 return error_mark_node;
3713 }
3714 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3715
3716 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3717
3718 return result;
3719 }
3720
3721 /* Checks T for any "bare" parameter packs, which have not yet been
3722 expanded, and issues an error if any are found. This operation can
3723 only be done on full expressions or types (e.g., an expression
3724 statement, "if" condition, etc.), because we could have expressions like:
3725
3726 foo(f(g(h(args)))...)
3727
3728 where "args" is a parameter pack. check_for_bare_parameter_packs
3729 should not be called for the subexpressions args, h(args),
3730 g(h(args)), or f(g(h(args))), because we would produce erroneous
3731 error messages.
3732
3733 Returns TRUE and emits an error if there were bare parameter packs,
3734 returns FALSE otherwise. */
3735 bool
3736 check_for_bare_parameter_packs (tree t)
3737 {
3738 tree parameter_packs = NULL_TREE;
3739 struct find_parameter_pack_data ppd;
3740
3741 if (!processing_template_decl || !t || t == error_mark_node)
3742 return false;
3743
3744 if (TREE_CODE (t) == TYPE_DECL)
3745 t = TREE_TYPE (t);
3746
3747 ppd.parameter_packs = &parameter_packs;
3748 ppd.visited = new hash_set<tree>;
3749 ppd.type_pack_expansion_p = false;
3750 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3751 delete ppd.visited;
3752
3753 if (parameter_packs)
3754 {
3755 error ("parameter packs not expanded with %<...%>:");
3756 while (parameter_packs)
3757 {
3758 tree pack = TREE_VALUE (parameter_packs);
3759 tree name = NULL_TREE;
3760
3761 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3762 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3763 name = TYPE_NAME (pack);
3764 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3765 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3766 else
3767 name = DECL_NAME (pack);
3768
3769 if (name)
3770 inform (input_location, " %qD", name);
3771 else
3772 inform (input_location, " <anonymous>");
3773
3774 parameter_packs = TREE_CHAIN (parameter_packs);
3775 }
3776
3777 return true;
3778 }
3779
3780 return false;
3781 }
3782
3783 /* Expand any parameter packs that occur in the template arguments in
3784 ARGS. */
3785 tree
3786 expand_template_argument_pack (tree args)
3787 {
3788 tree result_args = NULL_TREE;
3789 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3790 int num_result_args = -1;
3791 int non_default_args_count = -1;
3792
3793 /* First, determine if we need to expand anything, and the number of
3794 slots we'll need. */
3795 for (in_arg = 0; in_arg < nargs; ++in_arg)
3796 {
3797 tree arg = TREE_VEC_ELT (args, in_arg);
3798 if (arg == NULL_TREE)
3799 return args;
3800 if (ARGUMENT_PACK_P (arg))
3801 {
3802 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3803 if (num_result_args < 0)
3804 num_result_args = in_arg + num_packed;
3805 else
3806 num_result_args += num_packed;
3807 }
3808 else
3809 {
3810 if (num_result_args >= 0)
3811 num_result_args++;
3812 }
3813 }
3814
3815 /* If no expansion is necessary, we're done. */
3816 if (num_result_args < 0)
3817 return args;
3818
3819 /* Expand arguments. */
3820 result_args = make_tree_vec (num_result_args);
3821 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3822 non_default_args_count =
3823 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3824 for (in_arg = 0; in_arg < nargs; ++in_arg)
3825 {
3826 tree arg = TREE_VEC_ELT (args, in_arg);
3827 if (ARGUMENT_PACK_P (arg))
3828 {
3829 tree packed = ARGUMENT_PACK_ARGS (arg);
3830 int i, num_packed = TREE_VEC_LENGTH (packed);
3831 for (i = 0; i < num_packed; ++i, ++out_arg)
3832 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3833 if (non_default_args_count > 0)
3834 non_default_args_count += num_packed - 1;
3835 }
3836 else
3837 {
3838 TREE_VEC_ELT (result_args, out_arg) = arg;
3839 ++out_arg;
3840 }
3841 }
3842 if (non_default_args_count >= 0)
3843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3844 return result_args;
3845 }
3846
3847 /* Checks if DECL shadows a template parameter.
3848
3849 [temp.local]: A template-parameter shall not be redeclared within its
3850 scope (including nested scopes).
3851
3852 Emits an error and returns TRUE if the DECL shadows a parameter,
3853 returns FALSE otherwise. */
3854
3855 bool
3856 check_template_shadow (tree decl)
3857 {
3858 tree olddecl;
3859
3860 /* If we're not in a template, we can't possibly shadow a template
3861 parameter. */
3862 if (!current_template_parms)
3863 return true;
3864
3865 /* Figure out what we're shadowing. */
3866 if (TREE_CODE (decl) == OVERLOAD)
3867 decl = OVL_CURRENT (decl);
3868 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3869
3870 /* If there's no previous binding for this name, we're not shadowing
3871 anything, let alone a template parameter. */
3872 if (!olddecl)
3873 return true;
3874
3875 /* If we're not shadowing a template parameter, we're done. Note
3876 that OLDDECL might be an OVERLOAD (or perhaps even an
3877 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3878 node. */
3879 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3880 return true;
3881
3882 /* We check for decl != olddecl to avoid bogus errors for using a
3883 name inside a class. We check TPFI to avoid duplicate errors for
3884 inline member templates. */
3885 if (decl == olddecl
3886 || (DECL_TEMPLATE_PARM_P (decl)
3887 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3888 return true;
3889
3890 /* Don't complain about the injected class name, as we've already
3891 complained about the class itself. */
3892 if (DECL_SELF_REFERENCE_P (decl))
3893 return false;
3894
3895 if (DECL_TEMPLATE_PARM_P (decl))
3896 error ("declaration of template parameter %q+D shadows "
3897 "template parameter", decl);
3898 else
3899 error ("declaration of %q+#D shadows template parameter", decl);
3900 inform (DECL_SOURCE_LOCATION (olddecl),
3901 "template parameter %qD declared here", olddecl);
3902 return false;
3903 }
3904
3905 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3906 ORIG_LEVEL, DECL, and TYPE. */
3907
3908 static tree
3909 build_template_parm_index (int index,
3910 int level,
3911 int orig_level,
3912 tree decl,
3913 tree type)
3914 {
3915 tree t = make_node (TEMPLATE_PARM_INDEX);
3916 TEMPLATE_PARM_IDX (t) = index;
3917 TEMPLATE_PARM_LEVEL (t) = level;
3918 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3919 TEMPLATE_PARM_DECL (t) = decl;
3920 TREE_TYPE (t) = type;
3921 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3922 TREE_READONLY (t) = TREE_READONLY (decl);
3923
3924 return t;
3925 }
3926
3927 /* Find the canonical type parameter for the given template type
3928 parameter. Returns the canonical type parameter, which may be TYPE
3929 if no such parameter existed. */
3930
3931 static tree
3932 canonical_type_parameter (tree type)
3933 {
3934 tree list;
3935 int idx = TEMPLATE_TYPE_IDX (type);
3936 if (!canonical_template_parms)
3937 vec_alloc (canonical_template_parms, idx+1);
3938
3939 while (canonical_template_parms->length () <= (unsigned)idx)
3940 vec_safe_push (canonical_template_parms, NULL_TREE);
3941
3942 list = (*canonical_template_parms)[idx];
3943 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3944 list = TREE_CHAIN (list);
3945
3946 if (list)
3947 return TREE_VALUE (list);
3948 else
3949 {
3950 (*canonical_template_parms)[idx]
3951 = tree_cons (NULL_TREE, type,
3952 (*canonical_template_parms)[idx]);
3953 return type;
3954 }
3955 }
3956
3957 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3958 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3959 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3960 new one is created. */
3961
3962 static tree
3963 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3964 tsubst_flags_t complain)
3965 {
3966 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3967 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3968 != TEMPLATE_PARM_LEVEL (index) - levels)
3969 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3970 {
3971 tree orig_decl = TEMPLATE_PARM_DECL (index);
3972 tree decl, t;
3973
3974 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3975 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3976 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3977 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3978 DECL_ARTIFICIAL (decl) = 1;
3979 SET_DECL_TEMPLATE_PARM_P (decl);
3980
3981 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3982 TEMPLATE_PARM_LEVEL (index) - levels,
3983 TEMPLATE_PARM_ORIG_LEVEL (index),
3984 decl, type);
3985 TEMPLATE_PARM_DESCENDANTS (index) = t;
3986 TEMPLATE_PARM_PARAMETER_PACK (t)
3987 = TEMPLATE_PARM_PARAMETER_PACK (index);
3988
3989 /* Template template parameters need this. */
3990 if (TREE_CODE (decl) == TEMPLATE_DECL)
3991 {
3992 DECL_TEMPLATE_RESULT (decl)
3993 = build_decl (DECL_SOURCE_LOCATION (decl),
3994 TYPE_DECL, DECL_NAME (decl), type);
3995 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
3996 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3997 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
3998 }
3999 }
4000
4001 return TEMPLATE_PARM_DESCENDANTS (index);
4002 }
4003
4004 /* Process information from new template parameter PARM and append it
4005 to the LIST being built. This new parameter is a non-type
4006 parameter iff IS_NON_TYPE is true. This new parameter is a
4007 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4008 is in PARM_LOC. */
4009
4010 tree
4011 process_template_parm (tree list, location_t parm_loc, tree parm,
4012 bool is_non_type, bool is_parameter_pack)
4013 {
4014 tree decl = 0;
4015 int idx = 0;
4016
4017 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4018 tree defval = TREE_PURPOSE (parm);
4019 tree constr = TREE_TYPE (parm);
4020
4021 if (list)
4022 {
4023 tree p = tree_last (list);
4024
4025 if (p && TREE_VALUE (p) != error_mark_node)
4026 {
4027 p = TREE_VALUE (p);
4028 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4029 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4030 else
4031 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4032 }
4033
4034 ++idx;
4035 }
4036
4037 if (is_non_type)
4038 {
4039 parm = TREE_VALUE (parm);
4040
4041 SET_DECL_TEMPLATE_PARM_P (parm);
4042
4043 if (TREE_TYPE (parm) != error_mark_node)
4044 {
4045 /* [temp.param]
4046
4047 The top-level cv-qualifiers on the template-parameter are
4048 ignored when determining its type. */
4049 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4050 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4051 TREE_TYPE (parm) = error_mark_node;
4052 else if (uses_parameter_packs (TREE_TYPE (parm))
4053 && !is_parameter_pack
4054 /* If we're in a nested template parameter list, the template
4055 template parameter could be a parameter pack. */
4056 && processing_template_parmlist == 1)
4057 {
4058 /* This template parameter is not a parameter pack, but it
4059 should be. Complain about "bare" parameter packs. */
4060 check_for_bare_parameter_packs (TREE_TYPE (parm));
4061
4062 /* Recover by calling this a parameter pack. */
4063 is_parameter_pack = true;
4064 }
4065 }
4066
4067 /* A template parameter is not modifiable. */
4068 TREE_CONSTANT (parm) = 1;
4069 TREE_READONLY (parm) = 1;
4070 decl = build_decl (parm_loc,
4071 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4072 TREE_CONSTANT (decl) = 1;
4073 TREE_READONLY (decl) = 1;
4074 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4075 = build_template_parm_index (idx, processing_template_decl,
4076 processing_template_decl,
4077 decl, TREE_TYPE (parm));
4078
4079 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4080 = is_parameter_pack;
4081 }
4082 else
4083 {
4084 tree t;
4085 parm = TREE_VALUE (TREE_VALUE (parm));
4086
4087 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4088 {
4089 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4090 /* This is for distinguishing between real templates and template
4091 template parameters */
4092 TREE_TYPE (parm) = t;
4093 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4094 decl = parm;
4095 }
4096 else
4097 {
4098 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4099 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4100 decl = build_decl (parm_loc,
4101 TYPE_DECL, parm, t);
4102 }
4103
4104 TYPE_NAME (t) = decl;
4105 TYPE_STUB_DECL (t) = decl;
4106 parm = decl;
4107 TEMPLATE_TYPE_PARM_INDEX (t)
4108 = build_template_parm_index (idx, processing_template_decl,
4109 processing_template_decl,
4110 decl, TREE_TYPE (parm));
4111 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4112 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4113 }
4114 DECL_ARTIFICIAL (decl) = 1;
4115 SET_DECL_TEMPLATE_PARM_P (decl);
4116
4117 /* Build requirements for the type/template parameter.
4118 This must be done after SET_DECL_TEMPLATE_PARM_P or
4119 process_template_parm could fail. */
4120 tree reqs = finish_shorthand_constraint (parm, constr);
4121
4122 pushdecl (decl);
4123
4124 /* Build the parameter node linking the parameter declaration,
4125 its default argument (if any), and its constraints (if any). */
4126 parm = build_tree_list (defval, parm);
4127 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4128
4129 return chainon (list, parm);
4130 }
4131
4132 /* The end of a template parameter list has been reached. Process the
4133 tree list into a parameter vector, converting each parameter into a more
4134 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4135 as PARM_DECLs. */
4136
4137 tree
4138 end_template_parm_list (tree parms)
4139 {
4140 int nparms;
4141 tree parm, next;
4142 tree saved_parmlist = make_tree_vec (list_length (parms));
4143
4144 /* Pop the dummy parameter level and add the real one. */
4145 current_template_parms = TREE_CHAIN (current_template_parms);
4146
4147 current_template_parms
4148 = tree_cons (size_int (processing_template_decl),
4149 saved_parmlist, current_template_parms);
4150
4151 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4152 {
4153 next = TREE_CHAIN (parm);
4154 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4155 TREE_CHAIN (parm) = NULL_TREE;
4156 }
4157
4158 --processing_template_parmlist;
4159
4160 return saved_parmlist;
4161 }
4162
4163 // Explicitly indicate the end of the template parameter list. We assume
4164 // that the current template parameters have been constructed and/or
4165 // managed explicitly, as when creating new template template parameters
4166 // from a shorthand constraint.
4167 void
4168 end_template_parm_list ()
4169 {
4170 --processing_template_parmlist;
4171 }
4172
4173 /* end_template_decl is called after a template declaration is seen. */
4174
4175 void
4176 end_template_decl (void)
4177 {
4178 reset_specialization ();
4179
4180 if (! processing_template_decl)
4181 return;
4182
4183 /* This matches the pushlevel in begin_template_parm_list. */
4184 finish_scope ();
4185
4186 --processing_template_decl;
4187 current_template_parms = TREE_CHAIN (current_template_parms);
4188 }
4189
4190 /* Takes a TREE_LIST representing a template parameter and convert it
4191 into an argument suitable to be passed to the type substitution
4192 functions. Note that If the TREE_LIST contains an error_mark
4193 node, the returned argument is error_mark_node. */
4194
4195 tree
4196 template_parm_to_arg (tree t)
4197 {
4198
4199 if (t == NULL_TREE
4200 || TREE_CODE (t) != TREE_LIST)
4201 return t;
4202
4203 if (error_operand_p (TREE_VALUE (t)))
4204 return error_mark_node;
4205
4206 t = TREE_VALUE (t);
4207
4208 if (TREE_CODE (t) == TYPE_DECL
4209 || TREE_CODE (t) == TEMPLATE_DECL)
4210 {
4211 t = TREE_TYPE (t);
4212
4213 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4214 {
4215 /* Turn this argument into a TYPE_ARGUMENT_PACK
4216 with a single element, which expands T. */
4217 tree vec = make_tree_vec (1);
4218 if (CHECKING_P)
4219 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4220
4221 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4222
4223 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4224 SET_ARGUMENT_PACK_ARGS (t, vec);
4225 }
4226 }
4227 else
4228 {
4229 t = DECL_INITIAL (t);
4230
4231 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4232 {
4233 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4234 with a single element, which expands T. */
4235 tree vec = make_tree_vec (1);
4236 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4237 if (CHECKING_P)
4238 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4239
4240 t = convert_from_reference (t);
4241 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4242
4243 t = make_node (NONTYPE_ARGUMENT_PACK);
4244 SET_ARGUMENT_PACK_ARGS (t, vec);
4245 TREE_TYPE (t) = type;
4246 }
4247 else
4248 t = convert_from_reference (t);
4249 }
4250 return t;
4251 }
4252
4253 /* Given a set of template parameters, return them as a set of template
4254 arguments. The template parameters are represented as a TREE_VEC, in
4255 the form documented in cp-tree.h for template arguments. */
4256
4257 static tree
4258 template_parms_to_args (tree parms)
4259 {
4260 tree header;
4261 tree args = NULL_TREE;
4262 int length = TMPL_PARMS_DEPTH (parms);
4263 int l = length;
4264
4265 /* If there is only one level of template parameters, we do not
4266 create a TREE_VEC of TREE_VECs. Instead, we return a single
4267 TREE_VEC containing the arguments. */
4268 if (length > 1)
4269 args = make_tree_vec (length);
4270
4271 for (header = parms; header; header = TREE_CHAIN (header))
4272 {
4273 tree a = copy_node (TREE_VALUE (header));
4274 int i;
4275
4276 TREE_TYPE (a) = NULL_TREE;
4277 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4278 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4279
4280 if (CHECKING_P)
4281 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4282
4283 if (length > 1)
4284 TREE_VEC_ELT (args, --l) = a;
4285 else
4286 args = a;
4287 }
4288
4289 return args;
4290 }
4291
4292 /* Within the declaration of a template, return the currently active
4293 template parameters as an argument TREE_VEC. */
4294
4295 static tree
4296 current_template_args (void)
4297 {
4298 return template_parms_to_args (current_template_parms);
4299 }
4300
4301 /* Update the declared TYPE by doing any lookups which were thought to be
4302 dependent, but are not now that we know the SCOPE of the declarator. */
4303
4304 tree
4305 maybe_update_decl_type (tree orig_type, tree scope)
4306 {
4307 tree type = orig_type;
4308
4309 if (type == NULL_TREE)
4310 return type;
4311
4312 if (TREE_CODE (orig_type) == TYPE_DECL)
4313 type = TREE_TYPE (type);
4314
4315 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4316 && dependent_type_p (type)
4317 /* Don't bother building up the args in this case. */
4318 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4319 {
4320 /* tsubst in the args corresponding to the template parameters,
4321 including auto if present. Most things will be unchanged, but
4322 make_typename_type and tsubst_qualified_id will resolve
4323 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4324 tree args = current_template_args ();
4325 tree auto_node = type_uses_auto (type);
4326 tree pushed;
4327 if (auto_node)
4328 {
4329 tree auto_vec = make_tree_vec (1);
4330 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4331 args = add_to_template_args (args, auto_vec);
4332 }
4333 pushed = push_scope (scope);
4334 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4335 if (pushed)
4336 pop_scope (scope);
4337 }
4338
4339 if (type == error_mark_node)
4340 return orig_type;
4341
4342 if (TREE_CODE (orig_type) == TYPE_DECL)
4343 {
4344 if (same_type_p (type, TREE_TYPE (orig_type)))
4345 type = orig_type;
4346 else
4347 type = TYPE_NAME (type);
4348 }
4349 return type;
4350 }
4351
4352 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4353 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4354 the new template is a member template. */
4355
4356 tree
4357 build_template_decl (tree decl, tree parms, bool member_template_p)
4358 {
4359 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4360 DECL_TEMPLATE_PARMS (tmpl) = parms;
4361 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4362 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4363 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4364
4365 return tmpl;
4366 }
4367
4368 struct template_parm_data
4369 {
4370 /* The level of the template parameters we are currently
4371 processing. */
4372 int level;
4373
4374 /* The index of the specialization argument we are currently
4375 processing. */
4376 int current_arg;
4377
4378 /* An array whose size is the number of template parameters. The
4379 elements are nonzero if the parameter has been used in any one
4380 of the arguments processed so far. */
4381 int* parms;
4382
4383 /* An array whose size is the number of template arguments. The
4384 elements are nonzero if the argument makes use of template
4385 parameters of this level. */
4386 int* arg_uses_template_parms;
4387 };
4388
4389 /* Subroutine of push_template_decl used to see if each template
4390 parameter in a partial specialization is used in the explicit
4391 argument list. If T is of the LEVEL given in DATA (which is
4392 treated as a template_parm_data*), then DATA->PARMS is marked
4393 appropriately. */
4394
4395 static int
4396 mark_template_parm (tree t, void* data)
4397 {
4398 int level;
4399 int idx;
4400 struct template_parm_data* tpd = (struct template_parm_data*) data;
4401
4402 template_parm_level_and_index (t, &level, &idx);
4403
4404 if (level == tpd->level)
4405 {
4406 tpd->parms[idx] = 1;
4407 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4408 }
4409
4410 /* Return zero so that for_each_template_parm will continue the
4411 traversal of the tree; we want to mark *every* template parm. */
4412 return 0;
4413 }
4414
4415 /* Process the partial specialization DECL. */
4416
4417 static tree
4418 process_partial_specialization (tree decl)
4419 {
4420 tree type = TREE_TYPE (decl);
4421 tree tinfo = get_template_info (decl);
4422 tree maintmpl = TI_TEMPLATE (tinfo);
4423 tree specargs = TI_ARGS (tinfo);
4424 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4425 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4426 tree inner_parms;
4427 tree inst;
4428 int nargs = TREE_VEC_LENGTH (inner_args);
4429 int ntparms;
4430 int i;
4431 bool did_error_intro = false;
4432 struct template_parm_data tpd;
4433 struct template_parm_data tpd2;
4434
4435 gcc_assert (current_template_parms);
4436
4437 /* A concept cannot be specialized. */
4438 if (flag_concepts && variable_concept_p (maintmpl))
4439 {
4440 error ("specialization of variable concept %q#D", maintmpl);
4441 return error_mark_node;
4442 }
4443
4444 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4445 ntparms = TREE_VEC_LENGTH (inner_parms);
4446
4447 /* We check that each of the template parameters given in the
4448 partial specialization is used in the argument list to the
4449 specialization. For example:
4450
4451 template <class T> struct S;
4452 template <class T> struct S<T*>;
4453
4454 The second declaration is OK because `T*' uses the template
4455 parameter T, whereas
4456
4457 template <class T> struct S<int>;
4458
4459 is no good. Even trickier is:
4460
4461 template <class T>
4462 struct S1
4463 {
4464 template <class U>
4465 struct S2;
4466 template <class U>
4467 struct S2<T>;
4468 };
4469
4470 The S2<T> declaration is actually invalid; it is a
4471 full-specialization. Of course,
4472
4473 template <class U>
4474 struct S2<T (*)(U)>;
4475
4476 or some such would have been OK. */
4477 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4478 tpd.parms = XALLOCAVEC (int, ntparms);
4479 memset (tpd.parms, 0, sizeof (int) * ntparms);
4480
4481 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4482 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4483 for (i = 0; i < nargs; ++i)
4484 {
4485 tpd.current_arg = i;
4486 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4487 &mark_template_parm,
4488 &tpd,
4489 NULL,
4490 /*include_nondeduced_p=*/false);
4491 }
4492 for (i = 0; i < ntparms; ++i)
4493 if (tpd.parms[i] == 0)
4494 {
4495 /* One of the template parms was not used in a deduced context in the
4496 specialization. */
4497 if (!did_error_intro)
4498 {
4499 error ("template parameters not deducible in "
4500 "partial specialization:");
4501 did_error_intro = true;
4502 }
4503
4504 inform (input_location, " %qD",
4505 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4506 }
4507
4508 if (did_error_intro)
4509 return error_mark_node;
4510
4511 /* [temp.class.spec]
4512
4513 The argument list of the specialization shall not be identical to
4514 the implicit argument list of the primary template. */
4515 tree main_args
4516 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4517 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4518 && (!flag_concepts
4519 || !strictly_subsumes (current_template_constraints (),
4520 get_constraints (maintmpl))))
4521 {
4522 if (!flag_concepts)
4523 error ("partial specialization %q+D does not specialize "
4524 "any template arguments", decl);
4525 else
4526 error ("partial specialization %q+D does not specialize any "
4527 "template arguments and is not more constrained than", decl);
4528 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4529 }
4530
4531 /* A partial specialization that replaces multiple parameters of the
4532 primary template with a pack expansion is less specialized for those
4533 parameters. */
4534 if (nargs < DECL_NTPARMS (maintmpl))
4535 {
4536 error ("partial specialization is not more specialized than the "
4537 "primary template because it replaces multiple parameters "
4538 "with a pack expansion");
4539 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4540 return decl;
4541 }
4542
4543 /* [temp.class.spec]
4544
4545 A partially specialized non-type argument expression shall not
4546 involve template parameters of the partial specialization except
4547 when the argument expression is a simple identifier.
4548
4549 The type of a template parameter corresponding to a specialized
4550 non-type argument shall not be dependent on a parameter of the
4551 specialization.
4552
4553 Also, we verify that pack expansions only occur at the
4554 end of the argument list. */
4555 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4556 tpd2.parms = 0;
4557 for (i = 0; i < nargs; ++i)
4558 {
4559 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4560 tree arg = TREE_VEC_ELT (inner_args, i);
4561 tree packed_args = NULL_TREE;
4562 int j, len = 1;
4563
4564 if (ARGUMENT_PACK_P (arg))
4565 {
4566 /* Extract the arguments from the argument pack. We'll be
4567 iterating over these in the following loop. */
4568 packed_args = ARGUMENT_PACK_ARGS (arg);
4569 len = TREE_VEC_LENGTH (packed_args);
4570 }
4571
4572 for (j = 0; j < len; j++)
4573 {
4574 if (packed_args)
4575 /* Get the Jth argument in the parameter pack. */
4576 arg = TREE_VEC_ELT (packed_args, j);
4577
4578 if (PACK_EXPANSION_P (arg))
4579 {
4580 /* Pack expansions must come at the end of the
4581 argument list. */
4582 if ((packed_args && j < len - 1)
4583 || (!packed_args && i < nargs - 1))
4584 {
4585 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4586 error ("parameter pack argument %qE must be at the "
4587 "end of the template argument list", arg);
4588 else
4589 error ("parameter pack argument %qT must be at the "
4590 "end of the template argument list", arg);
4591 }
4592 }
4593
4594 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4595 /* We only care about the pattern. */
4596 arg = PACK_EXPANSION_PATTERN (arg);
4597
4598 if (/* These first two lines are the `non-type' bit. */
4599 !TYPE_P (arg)
4600 && TREE_CODE (arg) != TEMPLATE_DECL
4601 /* This next two lines are the `argument expression is not just a
4602 simple identifier' condition and also the `specialized
4603 non-type argument' bit. */
4604 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4605 && !(REFERENCE_REF_P (arg)
4606 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4607 {
4608 if ((!packed_args && tpd.arg_uses_template_parms[i])
4609 || (packed_args && uses_template_parms (arg)))
4610 error ("template argument %qE involves template parameter(s)",
4611 arg);
4612 else
4613 {
4614 /* Look at the corresponding template parameter,
4615 marking which template parameters its type depends
4616 upon. */
4617 tree type = TREE_TYPE (parm);
4618
4619 if (!tpd2.parms)
4620 {
4621 /* We haven't yet initialized TPD2. Do so now. */
4622 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4623 /* The number of parameters here is the number in the
4624 main template, which, as checked in the assertion
4625 above, is NARGS. */
4626 tpd2.parms = XALLOCAVEC (int, nargs);
4627 tpd2.level =
4628 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4629 }
4630
4631 /* Mark the template parameters. But this time, we're
4632 looking for the template parameters of the main
4633 template, not in the specialization. */
4634 tpd2.current_arg = i;
4635 tpd2.arg_uses_template_parms[i] = 0;
4636 memset (tpd2.parms, 0, sizeof (int) * nargs);
4637 for_each_template_parm (type,
4638 &mark_template_parm,
4639 &tpd2,
4640 NULL,
4641 /*include_nondeduced_p=*/false);
4642
4643 if (tpd2.arg_uses_template_parms [i])
4644 {
4645 /* The type depended on some template parameters.
4646 If they are fully specialized in the
4647 specialization, that's OK. */
4648 int j;
4649 int count = 0;
4650 for (j = 0; j < nargs; ++j)
4651 if (tpd2.parms[j] != 0
4652 && tpd.arg_uses_template_parms [j])
4653 ++count;
4654 if (count != 0)
4655 error_n (input_location, count,
4656 "type %qT of template argument %qE depends "
4657 "on a template parameter",
4658 "type %qT of template argument %qE depends "
4659 "on template parameters",
4660 type,
4661 arg);
4662 }
4663 }
4664 }
4665 }
4666 }
4667
4668 /* We should only get here once. */
4669 if (TREE_CODE (decl) == TYPE_DECL)
4670 gcc_assert (!COMPLETE_TYPE_P (type));
4671
4672 // Build the template decl.
4673 tree tmpl = build_template_decl (decl, current_template_parms,
4674 DECL_MEMBER_TEMPLATE_P (maintmpl));
4675 TREE_TYPE (tmpl) = type;
4676 DECL_TEMPLATE_RESULT (tmpl) = decl;
4677 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4678 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4679 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4680
4681 if (VAR_P (decl))
4682 /* We didn't register this in check_explicit_specialization so we could
4683 wait until the constraints were set. */
4684 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4685 else
4686 associate_classtype_constraints (type);
4687
4688 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4689 = tree_cons (specargs, tmpl,
4690 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4691 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4692
4693 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4694 inst = TREE_CHAIN (inst))
4695 {
4696 tree instance = TREE_VALUE (inst);
4697 if (TYPE_P (instance)
4698 ? (COMPLETE_TYPE_P (instance)
4699 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4700 : DECL_TEMPLATE_INSTANTIATION (instance))
4701 {
4702 tree spec = most_specialized_partial_spec (instance, tf_none);
4703 tree inst_decl = (DECL_P (instance)
4704 ? instance : TYPE_NAME (instance));
4705 if (!spec)
4706 /* OK */;
4707 else if (spec == error_mark_node)
4708 permerror (input_location,
4709 "declaration of %qD ambiguates earlier template "
4710 "instantiation for %qD", decl, inst_decl);
4711 else if (TREE_VALUE (spec) == tmpl)
4712 permerror (input_location,
4713 "partial specialization of %qD after instantiation "
4714 "of %qD", decl, inst_decl);
4715 }
4716 }
4717
4718 return decl;
4719 }
4720
4721 /* PARM is a template parameter of some form; return the corresponding
4722 TEMPLATE_PARM_INDEX. */
4723
4724 static tree
4725 get_template_parm_index (tree parm)
4726 {
4727 if (TREE_CODE (parm) == PARM_DECL
4728 || TREE_CODE (parm) == CONST_DECL)
4729 parm = DECL_INITIAL (parm);
4730 else if (TREE_CODE (parm) == TYPE_DECL
4731 || TREE_CODE (parm) == TEMPLATE_DECL)
4732 parm = TREE_TYPE (parm);
4733 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4734 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4735 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4736 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4737 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4738 return parm;
4739 }
4740
4741 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4742 parameter packs used by the template parameter PARM. */
4743
4744 static void
4745 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4746 {
4747 /* A type parm can't refer to another parm. */
4748 if (TREE_CODE (parm) == TYPE_DECL)
4749 return;
4750 else if (TREE_CODE (parm) == PARM_DECL)
4751 {
4752 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4753 ppd, ppd->visited);
4754 return;
4755 }
4756
4757 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4758
4759 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4760 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4761 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4762 }
4763
4764 /* PARM is a template parameter pack. Return any parameter packs used in
4765 its type or the type of any of its template parameters. If there are
4766 any such packs, it will be instantiated into a fixed template parameter
4767 list by partial instantiation rather than be fully deduced. */
4768
4769 tree
4770 fixed_parameter_pack_p (tree parm)
4771 {
4772 /* This can only be true in a member template. */
4773 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4774 return NULL_TREE;
4775 /* This can only be true for a parameter pack. */
4776 if (!template_parameter_pack_p (parm))
4777 return NULL_TREE;
4778 /* A type parm can't refer to another parm. */
4779 if (TREE_CODE (parm) == TYPE_DECL)
4780 return NULL_TREE;
4781
4782 tree parameter_packs = NULL_TREE;
4783 struct find_parameter_pack_data ppd;
4784 ppd.parameter_packs = &parameter_packs;
4785 ppd.visited = new hash_set<tree>;
4786 ppd.type_pack_expansion_p = false;
4787
4788 fixed_parameter_pack_p_1 (parm, &ppd);
4789
4790 delete ppd.visited;
4791 return parameter_packs;
4792 }
4793
4794 /* Check that a template declaration's use of default arguments and
4795 parameter packs is not invalid. Here, PARMS are the template
4796 parameters. IS_PRIMARY is true if DECL is the thing declared by
4797 a primary template. IS_PARTIAL is true if DECL is a partial
4798 specialization.
4799
4800 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4801 declaration (but not a definition); 1 indicates a declaration, 2
4802 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4803 emitted for extraneous default arguments.
4804
4805 Returns TRUE if there were no errors found, FALSE otherwise. */
4806
4807 bool
4808 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4809 bool is_partial, int is_friend_decl)
4810 {
4811 const char *msg;
4812 int last_level_to_check;
4813 tree parm_level;
4814 bool no_errors = true;
4815
4816 /* [temp.param]
4817
4818 A default template-argument shall not be specified in a
4819 function template declaration or a function template definition, nor
4820 in the template-parameter-list of the definition of a member of a
4821 class template. */
4822
4823 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4824 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4825 /* You can't have a function template declaration in a local
4826 scope, nor you can you define a member of a class template in a
4827 local scope. */
4828 return true;
4829
4830 if ((TREE_CODE (decl) == TYPE_DECL
4831 && TREE_TYPE (decl)
4832 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4833 || (TREE_CODE (decl) == FUNCTION_DECL
4834 && LAMBDA_FUNCTION_P (decl)))
4835 /* A lambda doesn't have an explicit declaration; don't complain
4836 about the parms of the enclosing class. */
4837 return true;
4838
4839 if (current_class_type
4840 && !TYPE_BEING_DEFINED (current_class_type)
4841 && DECL_LANG_SPECIFIC (decl)
4842 && DECL_DECLARES_FUNCTION_P (decl)
4843 /* If this is either a friend defined in the scope of the class
4844 or a member function. */
4845 && (DECL_FUNCTION_MEMBER_P (decl)
4846 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4847 : DECL_FRIEND_CONTEXT (decl)
4848 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4849 : false)
4850 /* And, if it was a member function, it really was defined in
4851 the scope of the class. */
4852 && (!DECL_FUNCTION_MEMBER_P (decl)
4853 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4854 /* We already checked these parameters when the template was
4855 declared, so there's no need to do it again now. This function
4856 was defined in class scope, but we're processing its body now
4857 that the class is complete. */
4858 return true;
4859
4860 /* Core issue 226 (C++0x only): the following only applies to class
4861 templates. */
4862 if (is_primary
4863 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4864 {
4865 /* [temp.param]
4866
4867 If a template-parameter has a default template-argument, all
4868 subsequent template-parameters shall have a default
4869 template-argument supplied. */
4870 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4871 {
4872 tree inner_parms = TREE_VALUE (parm_level);
4873 int ntparms = TREE_VEC_LENGTH (inner_parms);
4874 int seen_def_arg_p = 0;
4875 int i;
4876
4877 for (i = 0; i < ntparms; ++i)
4878 {
4879 tree parm = TREE_VEC_ELT (inner_parms, i);
4880
4881 if (parm == error_mark_node)
4882 continue;
4883
4884 if (TREE_PURPOSE (parm))
4885 seen_def_arg_p = 1;
4886 else if (seen_def_arg_p
4887 && !template_parameter_pack_p (TREE_VALUE (parm)))
4888 {
4889 error ("no default argument for %qD", TREE_VALUE (parm));
4890 /* For better subsequent error-recovery, we indicate that
4891 there should have been a default argument. */
4892 TREE_PURPOSE (parm) = error_mark_node;
4893 no_errors = false;
4894 }
4895 else if (!is_partial
4896 && !is_friend_decl
4897 /* Don't complain about an enclosing partial
4898 specialization. */
4899 && parm_level == parms
4900 && TREE_CODE (decl) == TYPE_DECL
4901 && i < ntparms - 1
4902 && template_parameter_pack_p (TREE_VALUE (parm))
4903 /* A fixed parameter pack will be partially
4904 instantiated into a fixed length list. */
4905 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4906 {
4907 /* A primary class template can only have one
4908 parameter pack, at the end of the template
4909 parameter list. */
4910
4911 error ("parameter pack %q+D must be at the end of the"
4912 " template parameter list", TREE_VALUE (parm));
4913
4914 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4915 = error_mark_node;
4916 no_errors = false;
4917 }
4918 }
4919 }
4920 }
4921
4922 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4923 || is_partial
4924 || !is_primary
4925 || is_friend_decl)
4926 /* For an ordinary class template, default template arguments are
4927 allowed at the innermost level, e.g.:
4928 template <class T = int>
4929 struct S {};
4930 but, in a partial specialization, they're not allowed even
4931 there, as we have in [temp.class.spec]:
4932
4933 The template parameter list of a specialization shall not
4934 contain default template argument values.
4935
4936 So, for a partial specialization, or for a function template
4937 (in C++98/C++03), we look at all of them. */
4938 ;
4939 else
4940 /* But, for a primary class template that is not a partial
4941 specialization we look at all template parameters except the
4942 innermost ones. */
4943 parms = TREE_CHAIN (parms);
4944
4945 /* Figure out what error message to issue. */
4946 if (is_friend_decl == 2)
4947 msg = G_("default template arguments may not be used in function template "
4948 "friend re-declaration");
4949 else if (is_friend_decl)
4950 msg = G_("default template arguments may not be used in function template "
4951 "friend declarations");
4952 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4953 msg = G_("default template arguments may not be used in function templates "
4954 "without -std=c++11 or -std=gnu++11");
4955 else if (is_partial)
4956 msg = G_("default template arguments may not be used in "
4957 "partial specializations");
4958 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
4959 msg = G_("default argument for template parameter for class enclosing %qD");
4960 else
4961 /* Per [temp.param]/9, "A default template-argument shall not be
4962 specified in the template-parameter-lists of the definition of
4963 a member of a class template that appears outside of the member's
4964 class.", thus if we aren't handling a member of a class template
4965 there is no need to examine the parameters. */
4966 return true;
4967
4968 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4969 /* If we're inside a class definition, there's no need to
4970 examine the parameters to the class itself. On the one
4971 hand, they will be checked when the class is defined, and,
4972 on the other, default arguments are valid in things like:
4973 template <class T = double>
4974 struct S { template <class U> void f(U); };
4975 Here the default argument for `S' has no bearing on the
4976 declaration of `f'. */
4977 last_level_to_check = template_class_depth (current_class_type) + 1;
4978 else
4979 /* Check everything. */
4980 last_level_to_check = 0;
4981
4982 for (parm_level = parms;
4983 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4984 parm_level = TREE_CHAIN (parm_level))
4985 {
4986 tree inner_parms = TREE_VALUE (parm_level);
4987 int i;
4988 int ntparms;
4989
4990 ntparms = TREE_VEC_LENGTH (inner_parms);
4991 for (i = 0; i < ntparms; ++i)
4992 {
4993 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4994 continue;
4995
4996 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4997 {
4998 if (msg)
4999 {
5000 no_errors = false;
5001 if (is_friend_decl == 2)
5002 return no_errors;
5003
5004 error (msg, decl);
5005 msg = 0;
5006 }
5007
5008 /* Clear out the default argument so that we are not
5009 confused later. */
5010 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5011 }
5012 }
5013
5014 /* At this point, if we're still interested in issuing messages,
5015 they must apply to classes surrounding the object declared. */
5016 if (msg)
5017 msg = G_("default argument for template parameter for class "
5018 "enclosing %qD");
5019 }
5020
5021 return no_errors;
5022 }
5023
5024 /* Worker for push_template_decl_real, called via
5025 for_each_template_parm. DATA is really an int, indicating the
5026 level of the parameters we are interested in. If T is a template
5027 parameter of that level, return nonzero. */
5028
5029 static int
5030 template_parm_this_level_p (tree t, void* data)
5031 {
5032 int this_level = *(int *)data;
5033 int level;
5034
5035 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5036 level = TEMPLATE_PARM_LEVEL (t);
5037 else
5038 level = TEMPLATE_TYPE_LEVEL (t);
5039 return level == this_level;
5040 }
5041
5042 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5043 parameters given by current_template_args, or reuses a
5044 previously existing one, if appropriate. Returns the DECL, or an
5045 equivalent one, if it is replaced via a call to duplicate_decls.
5046
5047 If IS_FRIEND is true, DECL is a friend declaration. */
5048
5049 tree
5050 push_template_decl_real (tree decl, bool is_friend)
5051 {
5052 tree tmpl;
5053 tree args;
5054 tree info;
5055 tree ctx;
5056 bool is_primary;
5057 bool is_partial;
5058 int new_template_p = 0;
5059 /* True if the template is a member template, in the sense of
5060 [temp.mem]. */
5061 bool member_template_p = false;
5062
5063 if (decl == error_mark_node || !current_template_parms)
5064 return error_mark_node;
5065
5066 /* See if this is a partial specialization. */
5067 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5068 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5069 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5070 || (VAR_P (decl)
5071 && DECL_LANG_SPECIFIC (decl)
5072 && DECL_TEMPLATE_SPECIALIZATION (decl)
5073 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5074
5075 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5076 is_friend = true;
5077
5078 if (is_friend)
5079 /* For a friend, we want the context of the friend function, not
5080 the type of which it is a friend. */
5081 ctx = CP_DECL_CONTEXT (decl);
5082 else if (CP_DECL_CONTEXT (decl)
5083 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5084 /* In the case of a virtual function, we want the class in which
5085 it is defined. */
5086 ctx = CP_DECL_CONTEXT (decl);
5087 else
5088 /* Otherwise, if we're currently defining some class, the DECL
5089 is assumed to be a member of the class. */
5090 ctx = current_scope ();
5091
5092 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5093 ctx = NULL_TREE;
5094
5095 if (!DECL_CONTEXT (decl))
5096 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5097
5098 /* See if this is a primary template. */
5099 if (is_friend && ctx
5100 && uses_template_parms_level (ctx, processing_template_decl))
5101 /* A friend template that specifies a class context, i.e.
5102 template <typename T> friend void A<T>::f();
5103 is not primary. */
5104 is_primary = false;
5105 else if (TREE_CODE (decl) == TYPE_DECL
5106 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5107 is_primary = false;
5108 else
5109 is_primary = template_parm_scope_p ();
5110
5111 if (is_primary)
5112 {
5113 warning (OPT_Wtemplates, "template %qD declared", decl);
5114
5115 if (DECL_CLASS_SCOPE_P (decl))
5116 member_template_p = true;
5117 if (TREE_CODE (decl) == TYPE_DECL
5118 && anon_aggrname_p (DECL_NAME (decl)))
5119 {
5120 error ("template class without a name");
5121 return error_mark_node;
5122 }
5123 else if (TREE_CODE (decl) == FUNCTION_DECL)
5124 {
5125 if (member_template_p)
5126 {
5127 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5128 error ("member template %qD may not have virt-specifiers", decl);
5129 }
5130 if (DECL_DESTRUCTOR_P (decl))
5131 {
5132 /* [temp.mem]
5133
5134 A destructor shall not be a member template. */
5135 error ("destructor %qD declared as member template", decl);
5136 return error_mark_node;
5137 }
5138 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5139 && (!prototype_p (TREE_TYPE (decl))
5140 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5141 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5142 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5143 == void_list_node)))
5144 {
5145 /* [basic.stc.dynamic.allocation]
5146
5147 An allocation function can be a function
5148 template. ... Template allocation functions shall
5149 have two or more parameters. */
5150 error ("invalid template declaration of %qD", decl);
5151 return error_mark_node;
5152 }
5153 }
5154 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5155 && CLASS_TYPE_P (TREE_TYPE (decl)))
5156 /* OK */;
5157 else if (TREE_CODE (decl) == TYPE_DECL
5158 && TYPE_DECL_ALIAS_P (decl))
5159 /* alias-declaration */
5160 gcc_assert (!DECL_ARTIFICIAL (decl));
5161 else if (VAR_P (decl))
5162 /* C++14 variable template. */;
5163 else
5164 {
5165 error ("template declaration of %q#D", decl);
5166 return error_mark_node;
5167 }
5168 }
5169
5170 /* Check to see that the rules regarding the use of default
5171 arguments are not being violated. */
5172 check_default_tmpl_args (decl, current_template_parms,
5173 is_primary, is_partial, /*is_friend_decl=*/0);
5174
5175 /* Ensure that there are no parameter packs in the type of this
5176 declaration that have not been expanded. */
5177 if (TREE_CODE (decl) == FUNCTION_DECL)
5178 {
5179 /* Check each of the arguments individually to see if there are
5180 any bare parameter packs. */
5181 tree type = TREE_TYPE (decl);
5182 tree arg = DECL_ARGUMENTS (decl);
5183 tree argtype = TYPE_ARG_TYPES (type);
5184
5185 while (arg && argtype)
5186 {
5187 if (!DECL_PACK_P (arg)
5188 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5189 {
5190 /* This is a PARM_DECL that contains unexpanded parameter
5191 packs. We have already complained about this in the
5192 check_for_bare_parameter_packs call, so just replace
5193 these types with ERROR_MARK_NODE. */
5194 TREE_TYPE (arg) = error_mark_node;
5195 TREE_VALUE (argtype) = error_mark_node;
5196 }
5197
5198 arg = DECL_CHAIN (arg);
5199 argtype = TREE_CHAIN (argtype);
5200 }
5201
5202 /* Check for bare parameter packs in the return type and the
5203 exception specifiers. */
5204 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5205 /* Errors were already issued, set return type to int
5206 as the frontend doesn't expect error_mark_node as
5207 the return type. */
5208 TREE_TYPE (type) = integer_type_node;
5209 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5210 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5211 }
5212 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5213 && TYPE_DECL_ALIAS_P (decl))
5214 ? DECL_ORIGINAL_TYPE (decl)
5215 : TREE_TYPE (decl)))
5216 {
5217 TREE_TYPE (decl) = error_mark_node;
5218 return error_mark_node;
5219 }
5220
5221 if (is_partial)
5222 return process_partial_specialization (decl);
5223
5224 args = current_template_args ();
5225
5226 if (!ctx
5227 || TREE_CODE (ctx) == FUNCTION_DECL
5228 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5229 || (TREE_CODE (decl) == TYPE_DECL
5230 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5231 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5232 {
5233 if (DECL_LANG_SPECIFIC (decl)
5234 && DECL_TEMPLATE_INFO (decl)
5235 && DECL_TI_TEMPLATE (decl))
5236 tmpl = DECL_TI_TEMPLATE (decl);
5237 /* If DECL is a TYPE_DECL for a class-template, then there won't
5238 be DECL_LANG_SPECIFIC. The information equivalent to
5239 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5240 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5241 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5242 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5243 {
5244 /* Since a template declaration already existed for this
5245 class-type, we must be redeclaring it here. Make sure
5246 that the redeclaration is valid. */
5247 redeclare_class_template (TREE_TYPE (decl),
5248 current_template_parms,
5249 current_template_constraints ());
5250 /* We don't need to create a new TEMPLATE_DECL; just use the
5251 one we already had. */
5252 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5253 }
5254 else
5255 {
5256 tmpl = build_template_decl (decl, current_template_parms,
5257 member_template_p);
5258 new_template_p = 1;
5259
5260 if (DECL_LANG_SPECIFIC (decl)
5261 && DECL_TEMPLATE_SPECIALIZATION (decl))
5262 {
5263 /* A specialization of a member template of a template
5264 class. */
5265 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5266 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5267 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5268 }
5269 }
5270 }
5271 else
5272 {
5273 tree a, t, current, parms;
5274 int i;
5275 tree tinfo = get_template_info (decl);
5276
5277 if (!tinfo)
5278 {
5279 error ("template definition of non-template %q#D", decl);
5280 return error_mark_node;
5281 }
5282
5283 tmpl = TI_TEMPLATE (tinfo);
5284
5285 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5286 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5287 && DECL_TEMPLATE_SPECIALIZATION (decl)
5288 && DECL_MEMBER_TEMPLATE_P (tmpl))
5289 {
5290 tree new_tmpl;
5291
5292 /* The declaration is a specialization of a member
5293 template, declared outside the class. Therefore, the
5294 innermost template arguments will be NULL, so we
5295 replace them with the arguments determined by the
5296 earlier call to check_explicit_specialization. */
5297 args = DECL_TI_ARGS (decl);
5298
5299 new_tmpl
5300 = build_template_decl (decl, current_template_parms,
5301 member_template_p);
5302 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5303 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5304 DECL_TI_TEMPLATE (decl) = new_tmpl;
5305 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5306 DECL_TEMPLATE_INFO (new_tmpl)
5307 = build_template_info (tmpl, args);
5308
5309 register_specialization (new_tmpl,
5310 most_general_template (tmpl),
5311 args,
5312 is_friend, 0);
5313 return decl;
5314 }
5315
5316 /* Make sure the template headers we got make sense. */
5317
5318 parms = DECL_TEMPLATE_PARMS (tmpl);
5319 i = TMPL_PARMS_DEPTH (parms);
5320 if (TMPL_ARGS_DEPTH (args) != i)
5321 {
5322 error ("expected %d levels of template parms for %q#D, got %d",
5323 i, decl, TMPL_ARGS_DEPTH (args));
5324 DECL_INTERFACE_KNOWN (decl) = 1;
5325 return error_mark_node;
5326 }
5327 else
5328 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5329 {
5330 a = TMPL_ARGS_LEVEL (args, i);
5331 t = INNERMOST_TEMPLATE_PARMS (parms);
5332
5333 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5334 {
5335 if (current == decl)
5336 error ("got %d template parameters for %q#D",
5337 TREE_VEC_LENGTH (a), decl);
5338 else
5339 error ("got %d template parameters for %q#T",
5340 TREE_VEC_LENGTH (a), current);
5341 error (" but %d required", TREE_VEC_LENGTH (t));
5342 /* Avoid crash in import_export_decl. */
5343 DECL_INTERFACE_KNOWN (decl) = 1;
5344 return error_mark_node;
5345 }
5346
5347 if (current == decl)
5348 current = ctx;
5349 else if (current == NULL_TREE)
5350 /* Can happen in erroneous input. */
5351 break;
5352 else
5353 current = get_containing_scope (current);
5354 }
5355
5356 /* Check that the parms are used in the appropriate qualifying scopes
5357 in the declarator. */
5358 if (!comp_template_args
5359 (TI_ARGS (tinfo),
5360 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5361 {
5362 error ("\
5363 template arguments to %qD do not match original template %qD",
5364 decl, DECL_TEMPLATE_RESULT (tmpl));
5365 if (!uses_template_parms (TI_ARGS (tinfo)))
5366 inform (input_location, "use template<> for an explicit specialization");
5367 /* Avoid crash in import_export_decl. */
5368 DECL_INTERFACE_KNOWN (decl) = 1;
5369 return error_mark_node;
5370 }
5371 }
5372
5373 DECL_TEMPLATE_RESULT (tmpl) = decl;
5374 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5375
5376 /* Push template declarations for global functions and types. Note
5377 that we do not try to push a global template friend declared in a
5378 template class; such a thing may well depend on the template
5379 parameters of the class. */
5380 if (new_template_p && !ctx
5381 && !(is_friend && template_class_depth (current_class_type) > 0))
5382 {
5383 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5384 if (tmpl == error_mark_node)
5385 return error_mark_node;
5386
5387 /* Hide template friend classes that haven't been declared yet. */
5388 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5389 {
5390 DECL_ANTICIPATED (tmpl) = 1;
5391 DECL_FRIEND_P (tmpl) = 1;
5392 }
5393 }
5394
5395 if (is_primary)
5396 {
5397 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5398 int i;
5399
5400 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5401 if (DECL_CONV_FN_P (tmpl))
5402 {
5403 int depth = TMPL_PARMS_DEPTH (parms);
5404
5405 /* It is a conversion operator. See if the type converted to
5406 depends on innermost template operands. */
5407
5408 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5409 depth))
5410 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5411 }
5412
5413 /* Give template template parms a DECL_CONTEXT of the template
5414 for which they are a parameter. */
5415 parms = INNERMOST_TEMPLATE_PARMS (parms);
5416 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5417 {
5418 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5419 if (TREE_CODE (parm) == TEMPLATE_DECL)
5420 DECL_CONTEXT (parm) = tmpl;
5421 }
5422
5423 if (TREE_CODE (decl) == TYPE_DECL
5424 && TYPE_DECL_ALIAS_P (decl)
5425 && complex_alias_template_p (tmpl))
5426 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5427 }
5428
5429 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5430 back to its most general template. If TMPL is a specialization,
5431 ARGS may only have the innermost set of arguments. Add the missing
5432 argument levels if necessary. */
5433 if (DECL_TEMPLATE_INFO (tmpl))
5434 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5435
5436 info = build_template_info (tmpl, args);
5437
5438 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5439 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5440 else
5441 {
5442 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5443 retrofit_lang_decl (decl);
5444 if (DECL_LANG_SPECIFIC (decl))
5445 DECL_TEMPLATE_INFO (decl) = info;
5446 }
5447
5448 if (flag_implicit_templates
5449 && !is_friend
5450 && TREE_PUBLIC (decl)
5451 && VAR_OR_FUNCTION_DECL_P (decl))
5452 /* Set DECL_COMDAT on template instantiations; if we force
5453 them to be emitted by explicit instantiation or -frepo,
5454 mark_needed will tell cgraph to do the right thing. */
5455 DECL_COMDAT (decl) = true;
5456
5457 return DECL_TEMPLATE_RESULT (tmpl);
5458 }
5459
5460 tree
5461 push_template_decl (tree decl)
5462 {
5463 return push_template_decl_real (decl, false);
5464 }
5465
5466 /* FN is an inheriting constructor that inherits from the constructor
5467 template INHERITED; turn FN into a constructor template with a matching
5468 template header. */
5469
5470 tree
5471 add_inherited_template_parms (tree fn, tree inherited)
5472 {
5473 tree inner_parms
5474 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5475 inner_parms = copy_node (inner_parms);
5476 tree parms
5477 = tree_cons (size_int (processing_template_decl + 1),
5478 inner_parms, current_template_parms);
5479 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5480 tree args = template_parms_to_args (parms);
5481 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5482 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5483 DECL_TEMPLATE_RESULT (tmpl) = fn;
5484 DECL_ARTIFICIAL (tmpl) = true;
5485 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5486 return tmpl;
5487 }
5488
5489 /* Called when a class template TYPE is redeclared with the indicated
5490 template PARMS, e.g.:
5491
5492 template <class T> struct S;
5493 template <class T> struct S {}; */
5494
5495 bool
5496 redeclare_class_template (tree type, tree parms, tree cons)
5497 {
5498 tree tmpl;
5499 tree tmpl_parms;
5500 int i;
5501
5502 if (!TYPE_TEMPLATE_INFO (type))
5503 {
5504 error ("%qT is not a template type", type);
5505 return false;
5506 }
5507
5508 tmpl = TYPE_TI_TEMPLATE (type);
5509 if (!PRIMARY_TEMPLATE_P (tmpl))
5510 /* The type is nested in some template class. Nothing to worry
5511 about here; there are no new template parameters for the nested
5512 type. */
5513 return true;
5514
5515 if (!parms)
5516 {
5517 error ("template specifiers not specified in declaration of %qD",
5518 tmpl);
5519 return false;
5520 }
5521
5522 parms = INNERMOST_TEMPLATE_PARMS (parms);
5523 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5524
5525 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5526 {
5527 error_n (input_location, TREE_VEC_LENGTH (parms),
5528 "redeclared with %d template parameter",
5529 "redeclared with %d template parameters",
5530 TREE_VEC_LENGTH (parms));
5531 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5532 "previous declaration %qD used %d template parameter",
5533 "previous declaration %qD used %d template parameters",
5534 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5535 return false;
5536 }
5537
5538 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5539 {
5540 tree tmpl_parm;
5541 tree parm;
5542 tree tmpl_default;
5543 tree parm_default;
5544
5545 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5546 || TREE_VEC_ELT (parms, i) == error_mark_node)
5547 continue;
5548
5549 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5550 if (error_operand_p (tmpl_parm))
5551 return false;
5552
5553 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5554 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5555 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5556
5557 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5558 TEMPLATE_DECL. */
5559 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5560 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5561 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5562 || (TREE_CODE (tmpl_parm) != PARM_DECL
5563 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5564 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5565 || (TREE_CODE (tmpl_parm) == PARM_DECL
5566 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5567 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5568 {
5569 error ("template parameter %q+#D", tmpl_parm);
5570 error ("redeclared here as %q#D", parm);
5571 return false;
5572 }
5573
5574 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5575 {
5576 /* We have in [temp.param]:
5577
5578 A template-parameter may not be given default arguments
5579 by two different declarations in the same scope. */
5580 error_at (input_location, "redefinition of default argument for %q#D", parm);
5581 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5582 "original definition appeared here");
5583 return false;
5584 }
5585
5586 if (parm_default != NULL_TREE)
5587 /* Update the previous template parameters (which are the ones
5588 that will really count) with the new default value. */
5589 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5590 else if (tmpl_default != NULL_TREE)
5591 /* Update the new parameters, too; they'll be used as the
5592 parameters for any members. */
5593 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5594
5595 /* Give each template template parm in this redeclaration a
5596 DECL_CONTEXT of the template for which they are a parameter. */
5597 if (TREE_CODE (parm) == TEMPLATE_DECL)
5598 {
5599 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5600 DECL_CONTEXT (parm) = tmpl;
5601 }
5602 }
5603
5604 // Cannot redeclare a class template with a different set of constraints.
5605 if (!equivalent_constraints (get_constraints (tmpl), cons))
5606 {
5607 error_at (input_location, "redeclaration %q#D with different "
5608 "constraints", tmpl);
5609 inform (DECL_SOURCE_LOCATION (tmpl),
5610 "original declaration appeared here");
5611 }
5612
5613 return true;
5614 }
5615
5616 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5617 to be used when the caller has already checked
5618 (processing_template_decl
5619 && !instantiation_dependent_expression_p (expr)
5620 && potential_constant_expression (expr))
5621 and cleared processing_template_decl. */
5622
5623 tree
5624 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5625 {
5626 return tsubst_copy_and_build (expr,
5627 /*args=*/NULL_TREE,
5628 complain,
5629 /*in_decl=*/NULL_TREE,
5630 /*function_p=*/false,
5631 /*integral_constant_expression_p=*/true);
5632 }
5633
5634 /* Simplify EXPR if it is a non-dependent expression. Returns the
5635 (possibly simplified) expression. */
5636
5637 tree
5638 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5639 {
5640 if (expr == NULL_TREE)
5641 return NULL_TREE;
5642
5643 /* If we're in a template, but EXPR isn't value dependent, simplify
5644 it. We're supposed to treat:
5645
5646 template <typename T> void f(T[1 + 1]);
5647 template <typename T> void f(T[2]);
5648
5649 as two declarations of the same function, for example. */
5650 if (processing_template_decl
5651 && !instantiation_dependent_expression_p (expr)
5652 && potential_constant_expression (expr))
5653 {
5654 processing_template_decl_sentinel s;
5655 expr = instantiate_non_dependent_expr_internal (expr, complain);
5656 }
5657 return expr;
5658 }
5659
5660 tree
5661 instantiate_non_dependent_expr (tree expr)
5662 {
5663 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5664 }
5665
5666 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5667 an uninstantiated expression. */
5668
5669 tree
5670 instantiate_non_dependent_or_null (tree expr)
5671 {
5672 if (expr == NULL_TREE)
5673 return NULL_TREE;
5674 if (processing_template_decl)
5675 {
5676 if (instantiation_dependent_expression_p (expr)
5677 || !potential_constant_expression (expr))
5678 expr = NULL_TREE;
5679 else
5680 {
5681 processing_template_decl_sentinel s;
5682 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5683 }
5684 }
5685 return expr;
5686 }
5687
5688 /* True iff T is a specialization of a variable template. */
5689
5690 bool
5691 variable_template_specialization_p (tree t)
5692 {
5693 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5694 return false;
5695 tree tmpl = DECL_TI_TEMPLATE (t);
5696 return variable_template_p (tmpl);
5697 }
5698
5699 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5700 template declaration, or a TYPE_DECL for an alias declaration. */
5701
5702 bool
5703 alias_type_or_template_p (tree t)
5704 {
5705 if (t == NULL_TREE)
5706 return false;
5707 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5708 || (TYPE_P (t)
5709 && TYPE_NAME (t)
5710 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5711 || DECL_ALIAS_TEMPLATE_P (t));
5712 }
5713
5714 /* Return TRUE iff T is a specialization of an alias template. */
5715
5716 bool
5717 alias_template_specialization_p (const_tree t)
5718 {
5719 /* It's an alias template specialization if it's an alias and its
5720 TYPE_NAME is a specialization of a primary template. */
5721 if (TYPE_ALIAS_P (t))
5722 {
5723 tree name = TYPE_NAME (t);
5724 if (DECL_LANG_SPECIFIC (name))
5725 if (tree ti = DECL_TEMPLATE_INFO (name))
5726 {
5727 tree tmpl = TI_TEMPLATE (ti);
5728 return PRIMARY_TEMPLATE_P (tmpl);
5729 }
5730 }
5731 return false;
5732 }
5733
5734 /* An alias template is complex from a SFINAE perspective if a template-id
5735 using that alias can be ill-formed when the expansion is not, as with
5736 the void_t template. We determine this by checking whether the
5737 expansion for the alias template uses all its template parameters. */
5738
5739 struct uses_all_template_parms_data
5740 {
5741 int level;
5742 bool *seen;
5743 };
5744
5745 static int
5746 uses_all_template_parms_r (tree t, void *data_)
5747 {
5748 struct uses_all_template_parms_data &data
5749 = *(struct uses_all_template_parms_data*)data_;
5750 tree idx = get_template_parm_index (t);
5751
5752 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5753 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5754 return 0;
5755 }
5756
5757 static bool
5758 complex_alias_template_p (const_tree tmpl)
5759 {
5760 struct uses_all_template_parms_data data;
5761 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5762 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5763 data.level = TMPL_PARMS_DEPTH (parms);
5764 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5765 data.seen = XALLOCAVEC (bool, len);
5766 for (int i = 0; i < len; ++i)
5767 data.seen[i] = false;
5768
5769 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5770 for (int i = 0; i < len; ++i)
5771 if (!data.seen[i])
5772 return true;
5773 return false;
5774 }
5775
5776 /* Return TRUE iff T is a specialization of a complex alias template with
5777 dependent template-arguments. */
5778
5779 bool
5780 dependent_alias_template_spec_p (const_tree t)
5781 {
5782 return (alias_template_specialization_p (t)
5783 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5784 && (any_dependent_template_arguments_p
5785 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5786 }
5787
5788 /* Return the number of innermost template parameters in TMPL. */
5789
5790 static int
5791 num_innermost_template_parms (tree tmpl)
5792 {
5793 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5794 return TREE_VEC_LENGTH (parms);
5795 }
5796
5797 /* Return either TMPL or another template that it is equivalent to under DR
5798 1286: An alias that just changes the name of a template is equivalent to
5799 the other template. */
5800
5801 static tree
5802 get_underlying_template (tree tmpl)
5803 {
5804 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5805 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5806 {
5807 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5808 if (TYPE_TEMPLATE_INFO (result))
5809 {
5810 tree sub = TYPE_TI_TEMPLATE (result);
5811 if (PRIMARY_TEMPLATE_P (sub)
5812 && (num_innermost_template_parms (tmpl)
5813 == num_innermost_template_parms (sub)))
5814 {
5815 tree alias_args = INNERMOST_TEMPLATE_ARGS
5816 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5817 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5818 break;
5819 /* The alias type is equivalent to the pattern of the
5820 underlying template, so strip the alias. */
5821 tmpl = sub;
5822 continue;
5823 }
5824 }
5825 break;
5826 }
5827 return tmpl;
5828 }
5829
5830 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5831 must be a function or a pointer-to-function type, as specified
5832 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5833 and check that the resulting function has external linkage. */
5834
5835 static tree
5836 convert_nontype_argument_function (tree type, tree expr,
5837 tsubst_flags_t complain)
5838 {
5839 tree fns = expr;
5840 tree fn, fn_no_ptr;
5841 linkage_kind linkage;
5842
5843 fn = instantiate_type (type, fns, tf_none);
5844 if (fn == error_mark_node)
5845 return error_mark_node;
5846
5847 fn_no_ptr = fn;
5848 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5849 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5850 if (BASELINK_P (fn_no_ptr))
5851 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5852
5853 /* [temp.arg.nontype]/1
5854
5855 A template-argument for a non-type, non-template template-parameter
5856 shall be one of:
5857 [...]
5858 -- the address of an object or function with external [C++11: or
5859 internal] linkage. */
5860
5861 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5862 {
5863 if (complain & tf_error)
5864 {
5865 error ("%qE is not a valid template argument for type %qT",
5866 expr, type);
5867 if (TYPE_PTR_P (type))
5868 error ("it must be the address of a function with "
5869 "external linkage");
5870 else
5871 error ("it must be the name of a function with "
5872 "external linkage");
5873 }
5874 return NULL_TREE;
5875 }
5876
5877 linkage = decl_linkage (fn_no_ptr);
5878 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5879 {
5880 if (complain & tf_error)
5881 {
5882 if (cxx_dialect >= cxx11)
5883 error ("%qE is not a valid template argument for type %qT "
5884 "because %qD has no linkage",
5885 expr, type, fn_no_ptr);
5886 else
5887 error ("%qE is not a valid template argument for type %qT "
5888 "because %qD does not have external linkage",
5889 expr, type, fn_no_ptr);
5890 }
5891 return NULL_TREE;
5892 }
5893
5894 return fn;
5895 }
5896
5897 /* Subroutine of convert_nontype_argument.
5898 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5899 Emit an error otherwise. */
5900
5901 static bool
5902 check_valid_ptrmem_cst_expr (tree type, tree expr,
5903 tsubst_flags_t complain)
5904 {
5905 STRIP_NOPS (expr);
5906 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5907 return true;
5908 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5909 return true;
5910 if (processing_template_decl
5911 && TREE_CODE (expr) == ADDR_EXPR
5912 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5913 return true;
5914 if (complain & tf_error)
5915 {
5916 error ("%qE is not a valid template argument for type %qT",
5917 expr, type);
5918 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5919 }
5920 return false;
5921 }
5922
5923 /* Returns TRUE iff the address of OP is value-dependent.
5924
5925 14.6.2.4 [temp.dep.temp]:
5926 A non-integral non-type template-argument is dependent if its type is
5927 dependent or it has either of the following forms
5928 qualified-id
5929 & qualified-id
5930 and contains a nested-name-specifier which specifies a class-name that
5931 names a dependent type.
5932
5933 We generalize this to just say that the address of a member of a
5934 dependent class is value-dependent; the above doesn't cover the
5935 address of a static data member named with an unqualified-id. */
5936
5937 static bool
5938 has_value_dependent_address (tree op)
5939 {
5940 /* We could use get_inner_reference here, but there's no need;
5941 this is only relevant for template non-type arguments, which
5942 can only be expressed as &id-expression. */
5943 if (DECL_P (op))
5944 {
5945 tree ctx = CP_DECL_CONTEXT (op);
5946 if (TYPE_P (ctx) && dependent_type_p (ctx))
5947 return true;
5948 }
5949
5950 return false;
5951 }
5952
5953 /* The next set of functions are used for providing helpful explanatory
5954 diagnostics for failed overload resolution. Their messages should be
5955 indented by two spaces for consistency with the messages in
5956 call.c */
5957
5958 static int
5959 unify_success (bool /*explain_p*/)
5960 {
5961 return 0;
5962 }
5963
5964 static int
5965 unify_parameter_deduction_failure (bool explain_p, tree parm)
5966 {
5967 if (explain_p)
5968 inform (input_location,
5969 " couldn't deduce template parameter %qD", parm);
5970 return 1;
5971 }
5972
5973 static int
5974 unify_invalid (bool /*explain_p*/)
5975 {
5976 return 1;
5977 }
5978
5979 static int
5980 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5981 {
5982 if (explain_p)
5983 inform (input_location,
5984 " types %qT and %qT have incompatible cv-qualifiers",
5985 parm, arg);
5986 return 1;
5987 }
5988
5989 static int
5990 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5991 {
5992 if (explain_p)
5993 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5994 return 1;
5995 }
5996
5997 static int
5998 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5999 {
6000 if (explain_p)
6001 inform (input_location,
6002 " template parameter %qD is not a parameter pack, but "
6003 "argument %qD is",
6004 parm, arg);
6005 return 1;
6006 }
6007
6008 static int
6009 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6010 {
6011 if (explain_p)
6012 inform (input_location,
6013 " template argument %qE does not match "
6014 "pointer-to-member constant %qE",
6015 arg, parm);
6016 return 1;
6017 }
6018
6019 static int
6020 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6021 {
6022 if (explain_p)
6023 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6024 return 1;
6025 }
6026
6027 static int
6028 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6029 {
6030 if (explain_p)
6031 inform (input_location,
6032 " inconsistent parameter pack deduction with %qT and %qT",
6033 old_arg, new_arg);
6034 return 1;
6035 }
6036
6037 static int
6038 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6039 {
6040 if (explain_p)
6041 {
6042 if (TYPE_P (parm))
6043 inform (input_location,
6044 " deduced conflicting types for parameter %qT (%qT and %qT)",
6045 parm, first, second);
6046 else
6047 inform (input_location,
6048 " deduced conflicting values for non-type parameter "
6049 "%qE (%qE and %qE)", parm, first, second);
6050 }
6051 return 1;
6052 }
6053
6054 static int
6055 unify_vla_arg (bool explain_p, tree arg)
6056 {
6057 if (explain_p)
6058 inform (input_location,
6059 " variable-sized array type %qT is not "
6060 "a valid template argument",
6061 arg);
6062 return 1;
6063 }
6064
6065 static int
6066 unify_method_type_error (bool explain_p, tree arg)
6067 {
6068 if (explain_p)
6069 inform (input_location,
6070 " member function type %qT is not a valid template argument",
6071 arg);
6072 return 1;
6073 }
6074
6075 static int
6076 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6077 {
6078 if (explain_p)
6079 {
6080 if (least_p)
6081 inform_n (input_location, wanted,
6082 " candidate expects at least %d argument, %d provided",
6083 " candidate expects at least %d arguments, %d provided",
6084 wanted, have);
6085 else
6086 inform_n (input_location, wanted,
6087 " candidate expects %d argument, %d provided",
6088 " candidate expects %d arguments, %d provided",
6089 wanted, have);
6090 }
6091 return 1;
6092 }
6093
6094 static int
6095 unify_too_many_arguments (bool explain_p, int have, int wanted)
6096 {
6097 return unify_arity (explain_p, have, wanted);
6098 }
6099
6100 static int
6101 unify_too_few_arguments (bool explain_p, int have, int wanted,
6102 bool least_p = false)
6103 {
6104 return unify_arity (explain_p, have, wanted, least_p);
6105 }
6106
6107 static int
6108 unify_arg_conversion (bool explain_p, tree to_type,
6109 tree from_type, tree arg)
6110 {
6111 if (explain_p)
6112 inform (EXPR_LOC_OR_LOC (arg, input_location),
6113 " cannot convert %qE (type %qT) to type %qT",
6114 arg, from_type, to_type);
6115 return 1;
6116 }
6117
6118 static int
6119 unify_no_common_base (bool explain_p, enum template_base_result r,
6120 tree parm, tree arg)
6121 {
6122 if (explain_p)
6123 switch (r)
6124 {
6125 case tbr_ambiguous_baseclass:
6126 inform (input_location, " %qT is an ambiguous base class of %qT",
6127 parm, arg);
6128 break;
6129 default:
6130 inform (input_location, " %qT is not derived from %qT", arg, parm);
6131 break;
6132 }
6133 return 1;
6134 }
6135
6136 static int
6137 unify_inconsistent_template_template_parameters (bool explain_p)
6138 {
6139 if (explain_p)
6140 inform (input_location,
6141 " template parameters of a template template argument are "
6142 "inconsistent with other deduced template arguments");
6143 return 1;
6144 }
6145
6146 static int
6147 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6148 {
6149 if (explain_p)
6150 inform (input_location,
6151 " can't deduce a template for %qT from non-template type %qT",
6152 parm, arg);
6153 return 1;
6154 }
6155
6156 static int
6157 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6158 {
6159 if (explain_p)
6160 inform (input_location,
6161 " template argument %qE does not match %qD", arg, parm);
6162 return 1;
6163 }
6164
6165 static int
6166 unify_overload_resolution_failure (bool explain_p, tree arg)
6167 {
6168 if (explain_p)
6169 inform (input_location,
6170 " could not resolve address from overloaded function %qE",
6171 arg);
6172 return 1;
6173 }
6174
6175 /* Attempt to convert the non-type template parameter EXPR to the
6176 indicated TYPE. If the conversion is successful, return the
6177 converted value. If the conversion is unsuccessful, return
6178 NULL_TREE if we issued an error message, or error_mark_node if we
6179 did not. We issue error messages for out-and-out bad template
6180 parameters, but not simply because the conversion failed, since we
6181 might be just trying to do argument deduction. Both TYPE and EXPR
6182 must be non-dependent.
6183
6184 The conversion follows the special rules described in
6185 [temp.arg.nontype], and it is much more strict than an implicit
6186 conversion.
6187
6188 This function is called twice for each template argument (see
6189 lookup_template_class for a more accurate description of this
6190 problem). This means that we need to handle expressions which
6191 are not valid in a C++ source, but can be created from the
6192 first call (for instance, casts to perform conversions). These
6193 hacks can go away after we fix the double coercion problem. */
6194
6195 static tree
6196 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6197 {
6198 tree expr_type;
6199
6200 /* Detect immediately string literals as invalid non-type argument.
6201 This special-case is not needed for correctness (we would easily
6202 catch this later), but only to provide better diagnostic for this
6203 common user mistake. As suggested by DR 100, we do not mention
6204 linkage issues in the diagnostic as this is not the point. */
6205 /* FIXME we're making this OK. */
6206 if (TREE_CODE (expr) == STRING_CST)
6207 {
6208 if (complain & tf_error)
6209 error ("%qE is not a valid template argument for type %qT "
6210 "because string literals can never be used in this context",
6211 expr, type);
6212 return NULL_TREE;
6213 }
6214
6215 /* Add the ADDR_EXPR now for the benefit of
6216 value_dependent_expression_p. */
6217 if (TYPE_PTROBV_P (type)
6218 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6219 {
6220 expr = decay_conversion (expr, complain);
6221 if (expr == error_mark_node)
6222 return error_mark_node;
6223 }
6224
6225 /* If we are in a template, EXPR may be non-dependent, but still
6226 have a syntactic, rather than semantic, form. For example, EXPR
6227 might be a SCOPE_REF, rather than the VAR_DECL to which the
6228 SCOPE_REF refers. Preserving the qualifying scope is necessary
6229 so that access checking can be performed when the template is
6230 instantiated -- but here we need the resolved form so that we can
6231 convert the argument. */
6232 bool non_dep = false;
6233 if (TYPE_REF_OBJ_P (type)
6234 && has_value_dependent_address (expr))
6235 /* If we want the address and it's value-dependent, don't fold. */;
6236 else if (!type_unknown_p (expr)
6237 && processing_template_decl
6238 && !instantiation_dependent_expression_p (expr)
6239 && potential_constant_expression (expr))
6240 non_dep = true;
6241 if (error_operand_p (expr))
6242 return error_mark_node;
6243 expr_type = TREE_TYPE (expr);
6244 if (TREE_CODE (type) == REFERENCE_TYPE)
6245 expr = mark_lvalue_use (expr);
6246 else
6247 expr = mark_rvalue_use (expr);
6248
6249 /* If the argument is non-dependent, perform any conversions in
6250 non-dependent context as well. */
6251 processing_template_decl_sentinel s (non_dep);
6252 if (non_dep)
6253 expr = instantiate_non_dependent_expr_internal (expr, complain);
6254
6255 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6256 to a non-type argument of "nullptr". */
6257 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6258 expr = fold_simple (convert (type, expr));
6259
6260 /* In C++11, integral or enumeration non-type template arguments can be
6261 arbitrary constant expressions. Pointer and pointer to
6262 member arguments can be general constant expressions that evaluate
6263 to a null value, but otherwise still need to be of a specific form. */
6264 if (cxx_dialect >= cxx11)
6265 {
6266 if (TREE_CODE (expr) == PTRMEM_CST)
6267 /* A PTRMEM_CST is already constant, and a valid template
6268 argument for a parameter of pointer to member type, we just want
6269 to leave it in that form rather than lower it to a
6270 CONSTRUCTOR. */;
6271 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6272 expr = maybe_constant_value (expr);
6273 else if (cxx_dialect >= cxx1z)
6274 {
6275 if (TREE_CODE (type) != REFERENCE_TYPE)
6276 expr = maybe_constant_value (expr);
6277 else if (REFERENCE_REF_P (expr))
6278 {
6279 expr = TREE_OPERAND (expr, 0);
6280 expr = maybe_constant_value (expr);
6281 expr = convert_from_reference (expr);
6282 }
6283 }
6284 else if (TYPE_PTR_OR_PTRMEM_P (type))
6285 {
6286 tree folded = maybe_constant_value (expr);
6287 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6288 : null_member_pointer_value_p (folded))
6289 expr = folded;
6290 }
6291 }
6292
6293 /* HACK: Due to double coercion, we can get a
6294 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6295 which is the tree that we built on the first call (see
6296 below when coercing to reference to object or to reference to
6297 function). We just strip everything and get to the arg.
6298 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6299 for examples. */
6300 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6301 {
6302 tree probe_type, probe = expr;
6303 if (REFERENCE_REF_P (probe))
6304 probe = TREE_OPERAND (probe, 0);
6305 probe_type = TREE_TYPE (probe);
6306 if (TREE_CODE (probe) == NOP_EXPR)
6307 {
6308 /* ??? Maybe we could use convert_from_reference here, but we
6309 would need to relax its constraints because the NOP_EXPR
6310 could actually change the type to something more cv-qualified,
6311 and this is not folded by convert_from_reference. */
6312 tree addr = TREE_OPERAND (probe, 0);
6313 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6314 && TREE_CODE (addr) == ADDR_EXPR
6315 && TYPE_PTR_P (TREE_TYPE (addr))
6316 && (same_type_ignoring_top_level_qualifiers_p
6317 (TREE_TYPE (probe_type),
6318 TREE_TYPE (TREE_TYPE (addr)))))
6319 {
6320 expr = TREE_OPERAND (addr, 0);
6321 expr_type = TREE_TYPE (probe_type);
6322 }
6323 }
6324 }
6325
6326 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6327 parameter is a pointer to object, through decay and
6328 qualification conversion. Let's strip everything. */
6329 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6330 {
6331 tree probe = expr;
6332 STRIP_NOPS (probe);
6333 if (TREE_CODE (probe) == ADDR_EXPR
6334 && TYPE_PTR_P (TREE_TYPE (probe)))
6335 {
6336 /* Skip the ADDR_EXPR only if it is part of the decay for
6337 an array. Otherwise, it is part of the original argument
6338 in the source code. */
6339 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6340 probe = TREE_OPERAND (probe, 0);
6341 expr = probe;
6342 expr_type = TREE_TYPE (expr);
6343 }
6344 }
6345
6346 /* [temp.arg.nontype]/5, bullet 1
6347
6348 For a non-type template-parameter of integral or enumeration type,
6349 integral promotions (_conv.prom_) and integral conversions
6350 (_conv.integral_) are applied. */
6351 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6352 {
6353 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6354 t = maybe_constant_value (t);
6355 if (t != error_mark_node)
6356 expr = t;
6357
6358 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6359 return error_mark_node;
6360
6361 /* Notice that there are constant expressions like '4 % 0' which
6362 do not fold into integer constants. */
6363 if (TREE_CODE (expr) != INTEGER_CST)
6364 {
6365 if (complain & tf_error)
6366 {
6367 int errs = errorcount, warns = warningcount + werrorcount;
6368 if (processing_template_decl
6369 && !require_potential_constant_expression (expr))
6370 return NULL_TREE;
6371 expr = cxx_constant_value (expr);
6372 if (errorcount > errs || warningcount + werrorcount > warns)
6373 inform (EXPR_LOC_OR_LOC (expr, input_location),
6374 "in template argument for type %qT ", type);
6375 if (expr == error_mark_node)
6376 return NULL_TREE;
6377 /* else cxx_constant_value complained but gave us
6378 a real constant, so go ahead. */
6379 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6380 }
6381 else
6382 return NULL_TREE;
6383 }
6384
6385 /* Avoid typedef problems. */
6386 if (TREE_TYPE (expr) != type)
6387 expr = fold_convert (type, expr);
6388 }
6389 /* [temp.arg.nontype]/5, bullet 2
6390
6391 For a non-type template-parameter of type pointer to object,
6392 qualification conversions (_conv.qual_) and the array-to-pointer
6393 conversion (_conv.array_) are applied. */
6394 else if (TYPE_PTROBV_P (type))
6395 {
6396 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6397
6398 A template-argument for a non-type, non-template template-parameter
6399 shall be one of: [...]
6400
6401 -- the name of a non-type template-parameter;
6402 -- the address of an object or function with external linkage, [...]
6403 expressed as "& id-expression" where the & is optional if the name
6404 refers to a function or array, or if the corresponding
6405 template-parameter is a reference.
6406
6407 Here, we do not care about functions, as they are invalid anyway
6408 for a parameter of type pointer-to-object. */
6409
6410 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6411 /* Non-type template parameters are OK. */
6412 ;
6413 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6414 /* Null pointer values are OK in C++11. */;
6415 else if (TREE_CODE (expr) != ADDR_EXPR
6416 && TREE_CODE (expr_type) != ARRAY_TYPE)
6417 {
6418 if (VAR_P (expr))
6419 {
6420 if (complain & tf_error)
6421 error ("%qD is not a valid template argument "
6422 "because %qD is a variable, not the address of "
6423 "a variable", expr, expr);
6424 return NULL_TREE;
6425 }
6426 if (POINTER_TYPE_P (expr_type))
6427 {
6428 if (complain & tf_error)
6429 error ("%qE is not a valid template argument for %qT "
6430 "because it is not the address of a variable",
6431 expr, type);
6432 return NULL_TREE;
6433 }
6434 /* Other values, like integer constants, might be valid
6435 non-type arguments of some other type. */
6436 return error_mark_node;
6437 }
6438 else
6439 {
6440 tree decl;
6441
6442 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6443 ? TREE_OPERAND (expr, 0) : expr);
6444 if (!VAR_P (decl))
6445 {
6446 if (complain & tf_error)
6447 error ("%qE is not a valid template argument of type %qT "
6448 "because %qE is not a variable", expr, type, decl);
6449 return NULL_TREE;
6450 }
6451 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6452 {
6453 if (complain & tf_error)
6454 error ("%qE is not a valid template argument of type %qT "
6455 "because %qD does not have external linkage",
6456 expr, type, decl);
6457 return NULL_TREE;
6458 }
6459 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6460 {
6461 if (complain & tf_error)
6462 error ("%qE is not a valid template argument of type %qT "
6463 "because %qD has no linkage", expr, type, decl);
6464 return NULL_TREE;
6465 }
6466 }
6467
6468 expr = decay_conversion (expr, complain);
6469 if (expr == error_mark_node)
6470 return error_mark_node;
6471
6472 expr = perform_qualification_conversions (type, expr);
6473 if (expr == error_mark_node)
6474 return error_mark_node;
6475 }
6476 /* [temp.arg.nontype]/5, bullet 3
6477
6478 For a non-type template-parameter of type reference to object, no
6479 conversions apply. The type referred to by the reference may be more
6480 cv-qualified than the (otherwise identical) type of the
6481 template-argument. The template-parameter is bound directly to the
6482 template-argument, which must be an lvalue. */
6483 else if (TYPE_REF_OBJ_P (type))
6484 {
6485 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6486 expr_type))
6487 return error_mark_node;
6488
6489 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6490 {
6491 if (complain & tf_error)
6492 error ("%qE is not a valid template argument for type %qT "
6493 "because of conflicts in cv-qualification", expr, type);
6494 return NULL_TREE;
6495 }
6496
6497 if (!real_lvalue_p (expr))
6498 {
6499 if (complain & tf_error)
6500 error ("%qE is not a valid template argument for type %qT "
6501 "because it is not an lvalue", expr, type);
6502 return NULL_TREE;
6503 }
6504
6505 /* [temp.arg.nontype]/1
6506
6507 A template-argument for a non-type, non-template template-parameter
6508 shall be one of: [...]
6509
6510 -- the address of an object or function with external linkage. */
6511 if (INDIRECT_REF_P (expr)
6512 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6513 {
6514 expr = TREE_OPERAND (expr, 0);
6515 if (DECL_P (expr))
6516 {
6517 if (complain & tf_error)
6518 error ("%q#D is not a valid template argument for type %qT "
6519 "because a reference variable does not have a constant "
6520 "address", expr, type);
6521 return NULL_TREE;
6522 }
6523 }
6524
6525 if (!DECL_P (expr))
6526 {
6527 if (complain & tf_error)
6528 error ("%qE is not a valid template argument for type %qT "
6529 "because it is not an object with linkage",
6530 expr, type);
6531 return NULL_TREE;
6532 }
6533
6534 /* DR 1155 allows internal linkage in C++11 and up. */
6535 linkage_kind linkage = decl_linkage (expr);
6536 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6537 {
6538 if (complain & tf_error)
6539 error ("%qE is not a valid template argument for type %qT "
6540 "because object %qD does not have linkage",
6541 expr, type, expr);
6542 return NULL_TREE;
6543 }
6544
6545 expr = build_nop (type, build_address (expr));
6546 }
6547 /* [temp.arg.nontype]/5, bullet 4
6548
6549 For a non-type template-parameter of type pointer to function, only
6550 the function-to-pointer conversion (_conv.func_) is applied. If the
6551 template-argument represents a set of overloaded functions (or a
6552 pointer to such), the matching function is selected from the set
6553 (_over.over_). */
6554 else if (TYPE_PTRFN_P (type))
6555 {
6556 /* If the argument is a template-id, we might not have enough
6557 context information to decay the pointer. */
6558 if (!type_unknown_p (expr_type))
6559 {
6560 expr = decay_conversion (expr, complain);
6561 if (expr == error_mark_node)
6562 return error_mark_node;
6563 }
6564
6565 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6566 /* Null pointer values are OK in C++11. */
6567 return perform_qualification_conversions (type, expr);
6568
6569 expr = convert_nontype_argument_function (type, expr, complain);
6570 if (!expr || expr == error_mark_node)
6571 return expr;
6572 }
6573 /* [temp.arg.nontype]/5, bullet 5
6574
6575 For a non-type template-parameter of type reference to function, no
6576 conversions apply. If the template-argument represents a set of
6577 overloaded functions, the matching function is selected from the set
6578 (_over.over_). */
6579 else if (TYPE_REFFN_P (type))
6580 {
6581 if (TREE_CODE (expr) == ADDR_EXPR)
6582 {
6583 if (complain & tf_error)
6584 {
6585 error ("%qE is not a valid template argument for type %qT "
6586 "because it is a pointer", expr, type);
6587 inform (input_location, "try using %qE instead",
6588 TREE_OPERAND (expr, 0));
6589 }
6590 return NULL_TREE;
6591 }
6592
6593 expr = convert_nontype_argument_function (type, expr, complain);
6594 if (!expr || expr == error_mark_node)
6595 return expr;
6596
6597 expr = build_nop (type, build_address (expr));
6598 }
6599 /* [temp.arg.nontype]/5, bullet 6
6600
6601 For a non-type template-parameter of type pointer to member function,
6602 no conversions apply. If the template-argument represents a set of
6603 overloaded member functions, the matching member function is selected
6604 from the set (_over.over_). */
6605 else if (TYPE_PTRMEMFUNC_P (type))
6606 {
6607 expr = instantiate_type (type, expr, tf_none);
6608 if (expr == error_mark_node)
6609 return error_mark_node;
6610
6611 /* [temp.arg.nontype] bullet 1 says the pointer to member
6612 expression must be a pointer-to-member constant. */
6613 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6614 return error_mark_node;
6615
6616 /* There is no way to disable standard conversions in
6617 resolve_address_of_overloaded_function (called by
6618 instantiate_type). It is possible that the call succeeded by
6619 converting &B::I to &D::I (where B is a base of D), so we need
6620 to reject this conversion here.
6621
6622 Actually, even if there was a way to disable standard conversions,
6623 it would still be better to reject them here so that we can
6624 provide a superior diagnostic. */
6625 if (!same_type_p (TREE_TYPE (expr), type))
6626 {
6627 if (complain & tf_error)
6628 {
6629 error ("%qE is not a valid template argument for type %qT "
6630 "because it is of type %qT", expr, type,
6631 TREE_TYPE (expr));
6632 /* If we are just one standard conversion off, explain. */
6633 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6634 inform (input_location,
6635 "standard conversions are not allowed in this context");
6636 }
6637 return NULL_TREE;
6638 }
6639 }
6640 /* [temp.arg.nontype]/5, bullet 7
6641
6642 For a non-type template-parameter of type pointer to data member,
6643 qualification conversions (_conv.qual_) are applied. */
6644 else if (TYPE_PTRDATAMEM_P (type))
6645 {
6646 /* [temp.arg.nontype] bullet 1 says the pointer to member
6647 expression must be a pointer-to-member constant. */
6648 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6649 return error_mark_node;
6650
6651 expr = perform_qualification_conversions (type, expr);
6652 if (expr == error_mark_node)
6653 return expr;
6654 }
6655 else if (NULLPTR_TYPE_P (type))
6656 {
6657 if (expr != nullptr_node)
6658 {
6659 if (complain & tf_error)
6660 error ("%qE is not a valid template argument for type %qT "
6661 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6662 return NULL_TREE;
6663 }
6664 return expr;
6665 }
6666 /* A template non-type parameter must be one of the above. */
6667 else
6668 gcc_unreachable ();
6669
6670 /* Sanity check: did we actually convert the argument to the
6671 right type? */
6672 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6673 (type, TREE_TYPE (expr)));
6674 return convert_from_reference (expr);
6675 }
6676
6677 /* Subroutine of coerce_template_template_parms, which returns 1 if
6678 PARM_PARM and ARG_PARM match using the rule for the template
6679 parameters of template template parameters. Both PARM and ARG are
6680 template parameters; the rest of the arguments are the same as for
6681 coerce_template_template_parms.
6682 */
6683 static int
6684 coerce_template_template_parm (tree parm,
6685 tree arg,
6686 tsubst_flags_t complain,
6687 tree in_decl,
6688 tree outer_args)
6689 {
6690 if (arg == NULL_TREE || error_operand_p (arg)
6691 || parm == NULL_TREE || error_operand_p (parm))
6692 return 0;
6693
6694 if (TREE_CODE (arg) != TREE_CODE (parm))
6695 return 0;
6696
6697 switch (TREE_CODE (parm))
6698 {
6699 case TEMPLATE_DECL:
6700 /* We encounter instantiations of templates like
6701 template <template <template <class> class> class TT>
6702 class C; */
6703 {
6704 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6705 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6706
6707 if (!coerce_template_template_parms
6708 (parmparm, argparm, complain, in_decl, outer_args))
6709 return 0;
6710 }
6711 /* Fall through. */
6712
6713 case TYPE_DECL:
6714 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6715 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6716 /* Argument is a parameter pack but parameter is not. */
6717 return 0;
6718 break;
6719
6720 case PARM_DECL:
6721 /* The tsubst call is used to handle cases such as
6722
6723 template <int> class C {};
6724 template <class T, template <T> class TT> class D {};
6725 D<int, C> d;
6726
6727 i.e. the parameter list of TT depends on earlier parameters. */
6728 if (!uses_template_parms (TREE_TYPE (arg)))
6729 {
6730 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6731 if (!uses_template_parms (t)
6732 && !same_type_p (t, TREE_TYPE (arg)))
6733 return 0;
6734 }
6735
6736 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6737 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6738 /* Argument is a parameter pack but parameter is not. */
6739 return 0;
6740
6741 break;
6742
6743 default:
6744 gcc_unreachable ();
6745 }
6746
6747 return 1;
6748 }
6749
6750
6751 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6752 template template parameters. Both PARM_PARMS and ARG_PARMS are
6753 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6754 or PARM_DECL.
6755
6756 Consider the example:
6757 template <class T> class A;
6758 template<template <class U> class TT> class B;
6759
6760 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6761 the parameters to A, and OUTER_ARGS contains A. */
6762
6763 static int
6764 coerce_template_template_parms (tree parm_parms,
6765 tree arg_parms,
6766 tsubst_flags_t complain,
6767 tree in_decl,
6768 tree outer_args)
6769 {
6770 int nparms, nargs, i;
6771 tree parm, arg;
6772 int variadic_p = 0;
6773
6774 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6775 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6776
6777 nparms = TREE_VEC_LENGTH (parm_parms);
6778 nargs = TREE_VEC_LENGTH (arg_parms);
6779
6780 /* Determine whether we have a parameter pack at the end of the
6781 template template parameter's template parameter list. */
6782 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6783 {
6784 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6785
6786 if (error_operand_p (parm))
6787 return 0;
6788
6789 switch (TREE_CODE (parm))
6790 {
6791 case TEMPLATE_DECL:
6792 case TYPE_DECL:
6793 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6794 variadic_p = 1;
6795 break;
6796
6797 case PARM_DECL:
6798 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6799 variadic_p = 1;
6800 break;
6801
6802 default:
6803 gcc_unreachable ();
6804 }
6805 }
6806
6807 if (nargs != nparms
6808 && !(variadic_p && nargs >= nparms - 1))
6809 return 0;
6810
6811 /* Check all of the template parameters except the parameter pack at
6812 the end (if any). */
6813 for (i = 0; i < nparms - variadic_p; ++i)
6814 {
6815 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6816 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6817 continue;
6818
6819 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6820 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6821
6822 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6823 outer_args))
6824 return 0;
6825
6826 }
6827
6828 if (variadic_p)
6829 {
6830 /* Check each of the template parameters in the template
6831 argument against the template parameter pack at the end of
6832 the template template parameter. */
6833 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6834 return 0;
6835
6836 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6837
6838 for (; i < nargs; ++i)
6839 {
6840 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6841 continue;
6842
6843 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6844
6845 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6846 outer_args))
6847 return 0;
6848 }
6849 }
6850
6851 return 1;
6852 }
6853
6854 /* Verifies that the deduced template arguments (in TARGS) for the
6855 template template parameters (in TPARMS) represent valid bindings,
6856 by comparing the template parameter list of each template argument
6857 to the template parameter list of its corresponding template
6858 template parameter, in accordance with DR150. This
6859 routine can only be called after all template arguments have been
6860 deduced. It will return TRUE if all of the template template
6861 parameter bindings are okay, FALSE otherwise. */
6862 bool
6863 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6864 {
6865 int i, ntparms = TREE_VEC_LENGTH (tparms);
6866 bool ret = true;
6867
6868 /* We're dealing with template parms in this process. */
6869 ++processing_template_decl;
6870
6871 targs = INNERMOST_TEMPLATE_ARGS (targs);
6872
6873 for (i = 0; i < ntparms; ++i)
6874 {
6875 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6876 tree targ = TREE_VEC_ELT (targs, i);
6877
6878 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6879 {
6880 tree packed_args = NULL_TREE;
6881 int idx, len = 1;
6882
6883 if (ARGUMENT_PACK_P (targ))
6884 {
6885 /* Look inside the argument pack. */
6886 packed_args = ARGUMENT_PACK_ARGS (targ);
6887 len = TREE_VEC_LENGTH (packed_args);
6888 }
6889
6890 for (idx = 0; idx < len; ++idx)
6891 {
6892 tree targ_parms = NULL_TREE;
6893
6894 if (packed_args)
6895 /* Extract the next argument from the argument
6896 pack. */
6897 targ = TREE_VEC_ELT (packed_args, idx);
6898
6899 if (PACK_EXPANSION_P (targ))
6900 /* Look at the pattern of the pack expansion. */
6901 targ = PACK_EXPANSION_PATTERN (targ);
6902
6903 /* Extract the template parameters from the template
6904 argument. */
6905 if (TREE_CODE (targ) == TEMPLATE_DECL)
6906 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6907 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6908 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6909
6910 /* Verify that we can coerce the template template
6911 parameters from the template argument to the template
6912 parameter. This requires an exact match. */
6913 if (targ_parms
6914 && !coerce_template_template_parms
6915 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6916 targ_parms,
6917 tf_none,
6918 tparm,
6919 targs))
6920 {
6921 ret = false;
6922 goto out;
6923 }
6924 }
6925 }
6926 }
6927
6928 out:
6929
6930 --processing_template_decl;
6931 return ret;
6932 }
6933
6934 /* Since type attributes aren't mangled, we need to strip them from
6935 template type arguments. */
6936
6937 static tree
6938 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6939 {
6940 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6941 return arg;
6942 bool removed_attributes = false;
6943 tree canon = strip_typedefs (arg, &removed_attributes);
6944 if (removed_attributes
6945 && (complain & tf_warning))
6946 warning (0, "ignoring attributes on template argument %qT", arg);
6947 return canon;
6948 }
6949
6950 // A template declaration can be substituted for a constrained
6951 // template template parameter only when the argument is more
6952 // constrained than the parameter.
6953 static bool
6954 is_compatible_template_arg (tree parm, tree arg)
6955 {
6956 tree parm_cons = get_constraints (parm);
6957
6958 /* For now, allow constrained template template arguments
6959 and unconstrained template template parameters. */
6960 if (parm_cons == NULL_TREE)
6961 return true;
6962
6963 tree arg_cons = get_constraints (arg);
6964
6965 // If the template parameter is constrained, we need to rewrite its
6966 // constraints in terms of the ARG's template parameters. This ensures
6967 // that all of the template parameter types will have the same depth.
6968 //
6969 // Note that this is only valid when coerce_template_template_parm is
6970 // true for the innermost template parameters of PARM and ARG. In other
6971 // words, because coercion is successful, this conversion will be valid.
6972 if (parm_cons)
6973 {
6974 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
6975 parm_cons = tsubst_constraint_info (parm_cons,
6976 INNERMOST_TEMPLATE_ARGS (args),
6977 tf_none, NULL_TREE);
6978 if (parm_cons == error_mark_node)
6979 return false;
6980 }
6981
6982 return subsumes (parm_cons, arg_cons);
6983 }
6984
6985 // Convert a placeholder argument into a binding to the original
6986 // parameter. The original parameter is saved as the TREE_TYPE of
6987 // ARG.
6988 static inline tree
6989 convert_wildcard_argument (tree parm, tree arg)
6990 {
6991 TREE_TYPE (arg) = parm;
6992 return arg;
6993 }
6994
6995 /* Convert the indicated template ARG as necessary to match the
6996 indicated template PARM. Returns the converted ARG, or
6997 error_mark_node if the conversion was unsuccessful. Error and
6998 warning messages are issued under control of COMPLAIN. This
6999 conversion is for the Ith parameter in the parameter list. ARGS is
7000 the full set of template arguments deduced so far. */
7001
7002 static tree
7003 convert_template_argument (tree parm,
7004 tree arg,
7005 tree args,
7006 tsubst_flags_t complain,
7007 int i,
7008 tree in_decl)
7009 {
7010 tree orig_arg;
7011 tree val;
7012 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7013
7014 if (parm == error_mark_node)
7015 return error_mark_node;
7016
7017 /* Trivially convert placeholders. */
7018 if (TREE_CODE (arg) == WILDCARD_DECL)
7019 return convert_wildcard_argument (parm, arg);
7020
7021 if (TREE_CODE (arg) == TREE_LIST
7022 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7023 {
7024 /* The template argument was the name of some
7025 member function. That's usually
7026 invalid, but static members are OK. In any
7027 case, grab the underlying fields/functions
7028 and issue an error later if required. */
7029 orig_arg = TREE_VALUE (arg);
7030 TREE_TYPE (arg) = unknown_type_node;
7031 }
7032
7033 orig_arg = arg;
7034
7035 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7036 requires_type = (TREE_CODE (parm) == TYPE_DECL
7037 || requires_tmpl_type);
7038
7039 /* When determining whether an argument pack expansion is a template,
7040 look at the pattern. */
7041 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7042 arg = PACK_EXPANSION_PATTERN (arg);
7043
7044 /* Deal with an injected-class-name used as a template template arg. */
7045 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7046 {
7047 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7048 if (TREE_CODE (t) == TEMPLATE_DECL)
7049 {
7050 if (cxx_dialect >= cxx11)
7051 /* OK under DR 1004. */;
7052 else if (complain & tf_warning_or_error)
7053 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7054 " used as template template argument", TYPE_NAME (arg));
7055 else if (flag_pedantic_errors)
7056 t = arg;
7057
7058 arg = t;
7059 }
7060 }
7061
7062 is_tmpl_type =
7063 ((TREE_CODE (arg) == TEMPLATE_DECL
7064 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7065 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7066 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7067 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7068
7069 if (is_tmpl_type
7070 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7071 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7072 arg = TYPE_STUB_DECL (arg);
7073
7074 is_type = TYPE_P (arg) || is_tmpl_type;
7075
7076 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7077 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7078 {
7079 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7080 {
7081 if (complain & tf_error)
7082 error ("invalid use of destructor %qE as a type", orig_arg);
7083 return error_mark_node;
7084 }
7085
7086 permerror (input_location,
7087 "to refer to a type member of a template parameter, "
7088 "use %<typename %E%>", orig_arg);
7089
7090 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7091 TREE_OPERAND (arg, 1),
7092 typename_type,
7093 complain);
7094 arg = orig_arg;
7095 is_type = 1;
7096 }
7097 if (is_type != requires_type)
7098 {
7099 if (in_decl)
7100 {
7101 if (complain & tf_error)
7102 {
7103 error ("type/value mismatch at argument %d in template "
7104 "parameter list for %qD",
7105 i + 1, in_decl);
7106 if (is_type)
7107 inform (input_location,
7108 " expected a constant of type %qT, got %qT",
7109 TREE_TYPE (parm),
7110 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7111 else if (requires_tmpl_type)
7112 inform (input_location,
7113 " expected a class template, got %qE", orig_arg);
7114 else
7115 inform (input_location,
7116 " expected a type, got %qE", orig_arg);
7117 }
7118 }
7119 return error_mark_node;
7120 }
7121 if (is_tmpl_type ^ requires_tmpl_type)
7122 {
7123 if (in_decl && (complain & tf_error))
7124 {
7125 error ("type/value mismatch at argument %d in template "
7126 "parameter list for %qD",
7127 i + 1, in_decl);
7128 if (is_tmpl_type)
7129 inform (input_location,
7130 " expected a type, got %qT", DECL_NAME (arg));
7131 else
7132 inform (input_location,
7133 " expected a class template, got %qT", orig_arg);
7134 }
7135 return error_mark_node;
7136 }
7137
7138 if (is_type)
7139 {
7140 if (requires_tmpl_type)
7141 {
7142 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7143 val = orig_arg;
7144 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7145 /* The number of argument required is not known yet.
7146 Just accept it for now. */
7147 val = TREE_TYPE (arg);
7148 else
7149 {
7150 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7151 tree argparm;
7152
7153 /* Strip alias templates that are equivalent to another
7154 template. */
7155 arg = get_underlying_template (arg);
7156 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7157
7158 if (coerce_template_template_parms (parmparm, argparm,
7159 complain, in_decl,
7160 args))
7161 {
7162 val = arg;
7163
7164 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7165 TEMPLATE_DECL. */
7166 if (val != error_mark_node)
7167 {
7168 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7169 val = TREE_TYPE (val);
7170 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7171 val = make_pack_expansion (val);
7172 }
7173 }
7174 else
7175 {
7176 if (in_decl && (complain & tf_error))
7177 {
7178 error ("type/value mismatch at argument %d in "
7179 "template parameter list for %qD",
7180 i + 1, in_decl);
7181 inform (input_location,
7182 " expected a template of type %qD, got %qT",
7183 parm, orig_arg);
7184 }
7185
7186 val = error_mark_node;
7187 }
7188
7189 // Check that the constraints are compatible before allowing the
7190 // substitution.
7191 if (val != error_mark_node)
7192 if (!is_compatible_template_arg (parm, arg))
7193 {
7194 if (in_decl && (complain & tf_error))
7195 {
7196 error ("constraint mismatch at argument %d in "
7197 "template parameter list for %qD",
7198 i + 1, in_decl);
7199 inform (input_location, " expected %qD but got %qD",
7200 parm, arg);
7201 }
7202 val = error_mark_node;
7203 }
7204 }
7205 }
7206 else
7207 val = orig_arg;
7208 /* We only form one instance of each template specialization.
7209 Therefore, if we use a non-canonical variant (i.e., a
7210 typedef), any future messages referring to the type will use
7211 the typedef, which is confusing if those future uses do not
7212 themselves also use the typedef. */
7213 if (TYPE_P (val))
7214 val = canonicalize_type_argument (val, complain);
7215 }
7216 else
7217 {
7218 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7219
7220 if (invalid_nontype_parm_type_p (t, complain))
7221 return error_mark_node;
7222
7223 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7224 {
7225 if (same_type_p (t, TREE_TYPE (orig_arg)))
7226 val = orig_arg;
7227 else
7228 {
7229 /* Not sure if this is reachable, but it doesn't hurt
7230 to be robust. */
7231 error ("type mismatch in nontype parameter pack");
7232 val = error_mark_node;
7233 }
7234 }
7235 else if (!dependent_template_arg_p (orig_arg)
7236 && !uses_template_parms (t))
7237 /* We used to call digest_init here. However, digest_init
7238 will report errors, which we don't want when complain
7239 is zero. More importantly, digest_init will try too
7240 hard to convert things: for example, `0' should not be
7241 converted to pointer type at this point according to
7242 the standard. Accepting this is not merely an
7243 extension, since deciding whether or not these
7244 conversions can occur is part of determining which
7245 function template to call, or whether a given explicit
7246 argument specification is valid. */
7247 val = convert_nontype_argument (t, orig_arg, complain);
7248 else
7249 {
7250 bool removed_attr = false;
7251 val = strip_typedefs_expr (orig_arg, &removed_attr);
7252 }
7253
7254 if (val == NULL_TREE)
7255 val = error_mark_node;
7256 else if (val == error_mark_node && (complain & tf_error))
7257 error ("could not convert template argument %qE to %qT", orig_arg, t);
7258
7259 if (INDIRECT_REF_P (val))
7260 {
7261 /* Reject template arguments that are references to built-in
7262 functions with no library fallbacks. */
7263 const_tree inner = TREE_OPERAND (val, 0);
7264 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7265 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner))) == FUNCTION_TYPE
7266 && TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7267 && 0 < TREE_OPERAND_LENGTH (inner)
7268 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7269 return error_mark_node;
7270 }
7271
7272 if (TREE_CODE (val) == SCOPE_REF)
7273 {
7274 /* Strip typedefs from the SCOPE_REF. */
7275 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7276 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7277 complain);
7278 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7279 QUALIFIED_NAME_IS_TEMPLATE (val));
7280 }
7281 }
7282
7283 return val;
7284 }
7285
7286 /* Coerces the remaining template arguments in INNER_ARGS (from
7287 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7288 Returns the coerced argument pack. PARM_IDX is the position of this
7289 parameter in the template parameter list. ARGS is the original
7290 template argument list. */
7291 static tree
7292 coerce_template_parameter_pack (tree parms,
7293 int parm_idx,
7294 tree args,
7295 tree inner_args,
7296 int arg_idx,
7297 tree new_args,
7298 int* lost,
7299 tree in_decl,
7300 tsubst_flags_t complain)
7301 {
7302 tree parm = TREE_VEC_ELT (parms, parm_idx);
7303 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7304 tree packed_args;
7305 tree argument_pack;
7306 tree packed_parms = NULL_TREE;
7307
7308 if (arg_idx > nargs)
7309 arg_idx = nargs;
7310
7311 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7312 {
7313 /* When the template parameter is a non-type template parameter pack
7314 or template template parameter pack whose type or template
7315 parameters use parameter packs, we know exactly how many arguments
7316 we are looking for. Build a vector of the instantiated decls for
7317 these template parameters in PACKED_PARMS. */
7318 /* We can't use make_pack_expansion here because it would interpret a
7319 _DECL as a use rather than a declaration. */
7320 tree decl = TREE_VALUE (parm);
7321 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7322 SET_PACK_EXPANSION_PATTERN (exp, decl);
7323 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7324 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7325
7326 TREE_VEC_LENGTH (args)--;
7327 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7328 TREE_VEC_LENGTH (args)++;
7329
7330 if (packed_parms == error_mark_node)
7331 return error_mark_node;
7332
7333 /* If we're doing a partial instantiation of a member template,
7334 verify that all of the types used for the non-type
7335 template parameter pack are, in fact, valid for non-type
7336 template parameters. */
7337 if (arg_idx < nargs
7338 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7339 {
7340 int j, len = TREE_VEC_LENGTH (packed_parms);
7341 for (j = 0; j < len; ++j)
7342 {
7343 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7344 if (invalid_nontype_parm_type_p (t, complain))
7345 return error_mark_node;
7346 }
7347 /* We don't know how many args we have yet, just
7348 use the unconverted ones for now. */
7349 return NULL_TREE;
7350 }
7351
7352 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7353 }
7354 /* Check if we have a placeholder pack, which indicates we're
7355 in the context of a introduction list. In that case we want
7356 to match this pack to the single placeholder. */
7357 else if (arg_idx < nargs
7358 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7359 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7360 {
7361 nargs = arg_idx + 1;
7362 packed_args = make_tree_vec (1);
7363 }
7364 else
7365 packed_args = make_tree_vec (nargs - arg_idx);
7366
7367 /* Convert the remaining arguments, which will be a part of the
7368 parameter pack "parm". */
7369 for (; arg_idx < nargs; ++arg_idx)
7370 {
7371 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7372 tree actual_parm = TREE_VALUE (parm);
7373 int pack_idx = arg_idx - parm_idx;
7374
7375 if (packed_parms)
7376 {
7377 /* Once we've packed as many args as we have types, stop. */
7378 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7379 break;
7380 else if (PACK_EXPANSION_P (arg))
7381 /* We don't know how many args we have yet, just
7382 use the unconverted ones for now. */
7383 return NULL_TREE;
7384 else
7385 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7386 }
7387
7388 if (arg == error_mark_node)
7389 {
7390 if (complain & tf_error)
7391 error ("template argument %d is invalid", arg_idx + 1);
7392 }
7393 else
7394 arg = convert_template_argument (actual_parm,
7395 arg, new_args, complain, parm_idx,
7396 in_decl);
7397 if (arg == error_mark_node)
7398 (*lost)++;
7399 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7400 }
7401
7402 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
7403 && TREE_VEC_LENGTH (packed_args) > 0)
7404 {
7405 if (complain & tf_error)
7406 error ("wrong number of template arguments (%d, should be %d)",
7407 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
7408 return error_mark_node;
7409 }
7410
7411 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7412 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7413 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7414 else
7415 {
7416 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7417 TREE_TYPE (argument_pack)
7418 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7419 TREE_CONSTANT (argument_pack) = 1;
7420 }
7421
7422 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7423 if (CHECKING_P)
7424 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7425 TREE_VEC_LENGTH (packed_args));
7426 return argument_pack;
7427 }
7428
7429 /* Returns the number of pack expansions in the template argument vector
7430 ARGS. */
7431
7432 static int
7433 pack_expansion_args_count (tree args)
7434 {
7435 int i;
7436 int count = 0;
7437 if (args)
7438 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7439 {
7440 tree elt = TREE_VEC_ELT (args, i);
7441 if (elt && PACK_EXPANSION_P (elt))
7442 ++count;
7443 }
7444 return count;
7445 }
7446
7447 /* Convert all template arguments to their appropriate types, and
7448 return a vector containing the innermost resulting template
7449 arguments. If any error occurs, return error_mark_node. Error and
7450 warning messages are issued under control of COMPLAIN.
7451
7452 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7453 for arguments not specified in ARGS. Otherwise, if
7454 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7455 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7456 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7457 ARGS. */
7458
7459 static tree
7460 coerce_template_parms (tree parms,
7461 tree args,
7462 tree in_decl,
7463 tsubst_flags_t complain,
7464 bool require_all_args,
7465 bool use_default_args)
7466 {
7467 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7468 tree orig_inner_args;
7469 tree inner_args;
7470 tree new_args;
7471 tree new_inner_args;
7472 int saved_unevaluated_operand;
7473 int saved_inhibit_evaluation_warnings;
7474
7475 /* When used as a boolean value, indicates whether this is a
7476 variadic template parameter list. Since it's an int, we can also
7477 subtract it from nparms to get the number of non-variadic
7478 parameters. */
7479 int variadic_p = 0;
7480 int variadic_args_p = 0;
7481 int post_variadic_parms = 0;
7482
7483 /* Likewise for parameters with default arguments. */
7484 int default_p = 0;
7485
7486 if (args == error_mark_node)
7487 return error_mark_node;
7488
7489 nparms = TREE_VEC_LENGTH (parms);
7490
7491 /* Determine if there are any parameter packs or default arguments. */
7492 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7493 {
7494 tree parm = TREE_VEC_ELT (parms, parm_idx);
7495 if (variadic_p)
7496 ++post_variadic_parms;
7497 if (template_parameter_pack_p (TREE_VALUE (parm)))
7498 ++variadic_p;
7499 if (TREE_PURPOSE (parm))
7500 ++default_p;
7501 }
7502
7503 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7504 /* If there are no parameters that follow a parameter pack, we need to
7505 expand any argument packs so that we can deduce a parameter pack from
7506 some non-packed args followed by an argument pack, as in variadic85.C.
7507 If there are such parameters, we need to leave argument packs intact
7508 so the arguments are assigned properly. This can happen when dealing
7509 with a nested class inside a partial specialization of a class
7510 template, as in variadic92.C, or when deducing a template parameter pack
7511 from a sub-declarator, as in variadic114.C. */
7512 if (!post_variadic_parms)
7513 inner_args = expand_template_argument_pack (inner_args);
7514
7515 /* Count any pack expansion args. */
7516 variadic_args_p = pack_expansion_args_count (inner_args);
7517
7518 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7519 if ((nargs > nparms && !variadic_p)
7520 || (nargs < nparms - variadic_p
7521 && require_all_args
7522 && !variadic_args_p
7523 && (!use_default_args
7524 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7525 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7526 {
7527 if (complain & tf_error)
7528 {
7529 if (variadic_p || default_p)
7530 {
7531 nparms -= variadic_p + default_p;
7532 error ("wrong number of template arguments "
7533 "(%d, should be at least %d)", nargs, nparms);
7534 }
7535 else
7536 error ("wrong number of template arguments "
7537 "(%d, should be %d)", nargs, nparms);
7538
7539 if (in_decl)
7540 inform (DECL_SOURCE_LOCATION (in_decl),
7541 "provided for %qD", in_decl);
7542 }
7543
7544 return error_mark_node;
7545 }
7546 /* We can't pass a pack expansion to a non-pack parameter of an alias
7547 template (DR 1430). */
7548 else if (in_decl
7549 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7550 || concept_template_p (in_decl))
7551 && variadic_args_p
7552 && nargs - variadic_args_p < nparms - variadic_p)
7553 {
7554 if (complain & tf_error)
7555 {
7556 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7557 {
7558 tree arg = TREE_VEC_ELT (inner_args, i);
7559 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7560
7561 if (PACK_EXPANSION_P (arg)
7562 && !template_parameter_pack_p (parm))
7563 {
7564 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7565 error_at (location_of (arg),
7566 "pack expansion argument for non-pack parameter "
7567 "%qD of alias template %qD", parm, in_decl);
7568 else
7569 error_at (location_of (arg),
7570 "pack expansion argument for non-pack parameter "
7571 "%qD of concept %qD", parm, in_decl);
7572 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7573 goto found;
7574 }
7575 }
7576 gcc_unreachable ();
7577 found:;
7578 }
7579 return error_mark_node;
7580 }
7581
7582 /* We need to evaluate the template arguments, even though this
7583 template-id may be nested within a "sizeof". */
7584 saved_unevaluated_operand = cp_unevaluated_operand;
7585 cp_unevaluated_operand = 0;
7586 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7587 c_inhibit_evaluation_warnings = 0;
7588 new_inner_args = make_tree_vec (nparms);
7589 new_args = add_outermost_template_args (args, new_inner_args);
7590 int pack_adjust = 0;
7591 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7592 {
7593 tree arg;
7594 tree parm;
7595
7596 /* Get the Ith template parameter. */
7597 parm = TREE_VEC_ELT (parms, parm_idx);
7598
7599 if (parm == error_mark_node)
7600 {
7601 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7602 continue;
7603 }
7604
7605 /* Calculate the next argument. */
7606 if (arg_idx < nargs)
7607 arg = TREE_VEC_ELT (inner_args, arg_idx);
7608 else
7609 arg = NULL_TREE;
7610
7611 if (template_parameter_pack_p (TREE_VALUE (parm))
7612 && !(arg && ARGUMENT_PACK_P (arg)))
7613 {
7614 /* Some arguments will be placed in the
7615 template parameter pack PARM. */
7616 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7617 inner_args, arg_idx,
7618 new_args, &lost,
7619 in_decl, complain);
7620
7621 if (arg == NULL_TREE)
7622 {
7623 /* We don't know how many args we have yet, just use the
7624 unconverted (and still packed) ones for now. */
7625 new_inner_args = orig_inner_args;
7626 arg_idx = nargs;
7627 break;
7628 }
7629
7630 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7631
7632 /* Store this argument. */
7633 if (arg == error_mark_node)
7634 {
7635 lost++;
7636 /* We are done with all of the arguments. */
7637 arg_idx = nargs;
7638 }
7639 else
7640 {
7641 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7642 arg_idx += pack_adjust;
7643 }
7644
7645 continue;
7646 }
7647 else if (arg)
7648 {
7649 if (PACK_EXPANSION_P (arg))
7650 {
7651 /* "If every valid specialization of a variadic template
7652 requires an empty template parameter pack, the template is
7653 ill-formed, no diagnostic required." So check that the
7654 pattern works with this parameter. */
7655 tree pattern = PACK_EXPANSION_PATTERN (arg);
7656 tree conv = convert_template_argument (TREE_VALUE (parm),
7657 pattern, new_args,
7658 complain, parm_idx,
7659 in_decl);
7660 if (conv == error_mark_node)
7661 {
7662 inform (input_location, "so any instantiation with a "
7663 "non-empty parameter pack would be ill-formed");
7664 ++lost;
7665 }
7666 else if (TYPE_P (conv) && !TYPE_P (pattern))
7667 /* Recover from missing typename. */
7668 TREE_VEC_ELT (inner_args, arg_idx)
7669 = make_pack_expansion (conv);
7670
7671 /* We don't know how many args we have yet, just
7672 use the unconverted ones for now. */
7673 new_inner_args = inner_args;
7674 arg_idx = nargs;
7675 break;
7676 }
7677 }
7678 else if (require_all_args)
7679 {
7680 /* There must be a default arg in this case. */
7681 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7682 complain, in_decl);
7683 /* The position of the first default template argument,
7684 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7685 Record that. */
7686 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7687 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7688 arg_idx - pack_adjust);
7689 }
7690 else
7691 break;
7692
7693 if (arg == error_mark_node)
7694 {
7695 if (complain & tf_error)
7696 error ("template argument %d is invalid", arg_idx + 1);
7697 }
7698 else if (!arg)
7699 /* This only occurs if there was an error in the template
7700 parameter list itself (which we would already have
7701 reported) that we are trying to recover from, e.g., a class
7702 template with a parameter list such as
7703 template<typename..., typename>. */
7704 ++lost;
7705 else
7706 arg = convert_template_argument (TREE_VALUE (parm),
7707 arg, new_args, complain,
7708 parm_idx, in_decl);
7709
7710 if (arg == error_mark_node)
7711 lost++;
7712 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7713 }
7714 cp_unevaluated_operand = saved_unevaluated_operand;
7715 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7716
7717 if (variadic_p && arg_idx < nargs)
7718 {
7719 if (complain & tf_error)
7720 {
7721 error ("wrong number of template arguments "
7722 "(%d, should be %d)", nargs, arg_idx);
7723 if (in_decl)
7724 error ("provided for %q+D", in_decl);
7725 }
7726 return error_mark_node;
7727 }
7728
7729 if (lost)
7730 return error_mark_node;
7731
7732 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7733 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7734 TREE_VEC_LENGTH (new_inner_args));
7735
7736 return new_inner_args;
7737 }
7738
7739 /* Convert all template arguments to their appropriate types, and
7740 return a vector containing the innermost resulting template
7741 arguments. If any error occurs, return error_mark_node. Error and
7742 warning messages are not issued.
7743
7744 Note that no function argument deduction is performed, and default
7745 arguments are used to fill in unspecified arguments. */
7746 tree
7747 coerce_template_parms (tree parms, tree args, tree in_decl)
7748 {
7749 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7750 }
7751
7752 /* Convert all template arguments to their appropriate type, and
7753 instantiate default arguments as needed. This returns a vector
7754 containing the innermost resulting template arguments, or
7755 error_mark_node if unsuccessful. */
7756 tree
7757 coerce_template_parms (tree parms, tree args, tree in_decl,
7758 tsubst_flags_t complain)
7759 {
7760 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7761 }
7762
7763 /* Like coerce_template_parms. If PARMS represents all template
7764 parameters levels, this function returns a vector of vectors
7765 representing all the resulting argument levels. Note that in this
7766 case, only the innermost arguments are coerced because the
7767 outermost ones are supposed to have been coerced already.
7768
7769 Otherwise, if PARMS represents only (the innermost) vector of
7770 parameters, this function returns a vector containing just the
7771 innermost resulting arguments. */
7772
7773 static tree
7774 coerce_innermost_template_parms (tree parms,
7775 tree args,
7776 tree in_decl,
7777 tsubst_flags_t complain,
7778 bool require_all_args,
7779 bool use_default_args)
7780 {
7781 int parms_depth = TMPL_PARMS_DEPTH (parms);
7782 int args_depth = TMPL_ARGS_DEPTH (args);
7783 tree coerced_args;
7784
7785 if (parms_depth > 1)
7786 {
7787 coerced_args = make_tree_vec (parms_depth);
7788 tree level;
7789 int cur_depth;
7790
7791 for (level = parms, cur_depth = parms_depth;
7792 parms_depth > 0 && level != NULL_TREE;
7793 level = TREE_CHAIN (level), --cur_depth)
7794 {
7795 tree l;
7796 if (cur_depth == args_depth)
7797 l = coerce_template_parms (TREE_VALUE (level),
7798 args, in_decl, complain,
7799 require_all_args,
7800 use_default_args);
7801 else
7802 l = TMPL_ARGS_LEVEL (args, cur_depth);
7803
7804 if (l == error_mark_node)
7805 return error_mark_node;
7806
7807 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7808 }
7809 }
7810 else
7811 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7812 args, in_decl, complain,
7813 require_all_args,
7814 use_default_args);
7815 return coerced_args;
7816 }
7817
7818 /* Returns 1 if template args OT and NT are equivalent. */
7819
7820 static int
7821 template_args_equal (tree ot, tree nt)
7822 {
7823 if (nt == ot)
7824 return 1;
7825 if (nt == NULL_TREE || ot == NULL_TREE)
7826 return false;
7827
7828 if (TREE_CODE (nt) == TREE_VEC)
7829 /* For member templates */
7830 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7831 else if (PACK_EXPANSION_P (ot))
7832 return (PACK_EXPANSION_P (nt)
7833 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7834 PACK_EXPANSION_PATTERN (nt))
7835 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7836 PACK_EXPANSION_EXTRA_ARGS (nt)));
7837 else if (ARGUMENT_PACK_P (ot))
7838 {
7839 int i, len;
7840 tree opack, npack;
7841
7842 if (!ARGUMENT_PACK_P (nt))
7843 return 0;
7844
7845 opack = ARGUMENT_PACK_ARGS (ot);
7846 npack = ARGUMENT_PACK_ARGS (nt);
7847 len = TREE_VEC_LENGTH (opack);
7848 if (TREE_VEC_LENGTH (npack) != len)
7849 return 0;
7850 for (i = 0; i < len; ++i)
7851 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7852 TREE_VEC_ELT (npack, i)))
7853 return 0;
7854 return 1;
7855 }
7856 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7857 {
7858 /* We get here probably because we are in the middle of substituting
7859 into the pattern of a pack expansion. In that case the
7860 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7861 interested in. So we want to use the initial pack argument for
7862 the comparison. */
7863 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7864 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7865 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7866 return template_args_equal (ot, nt);
7867 }
7868 else if (TYPE_P (nt))
7869 {
7870 if (!TYPE_P (ot))
7871 return false;
7872 /* Don't treat an alias template specialization with dependent
7873 arguments as equivalent to its underlying type when used as a
7874 template argument; we need them to be distinct so that we
7875 substitute into the specialization arguments at instantiation
7876 time. And aliases can't be equivalent without being ==, so
7877 we don't need to look any deeper. */
7878 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7879 return false;
7880 else
7881 return same_type_p (ot, nt);
7882 }
7883 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7884 return 0;
7885 else
7886 {
7887 /* Try to treat a template non-type argument that has been converted
7888 to the parameter type as equivalent to one that hasn't yet. */
7889 for (enum tree_code code1 = TREE_CODE (ot);
7890 CONVERT_EXPR_CODE_P (code1)
7891 || code1 == NON_LVALUE_EXPR;
7892 code1 = TREE_CODE (ot))
7893 ot = TREE_OPERAND (ot, 0);
7894 for (enum tree_code code2 = TREE_CODE (nt);
7895 CONVERT_EXPR_CODE_P (code2)
7896 || code2 == NON_LVALUE_EXPR;
7897 code2 = TREE_CODE (nt))
7898 nt = TREE_OPERAND (nt, 0);
7899
7900 return cp_tree_equal (ot, nt);
7901 }
7902 }
7903
7904 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7905 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7906 NEWARG_PTR with the offending arguments if they are non-NULL. */
7907
7908 int
7909 comp_template_args (tree oldargs, tree newargs,
7910 tree *oldarg_ptr, tree *newarg_ptr)
7911 {
7912 int i;
7913
7914 if (oldargs == newargs)
7915 return 1;
7916
7917 if (!oldargs || !newargs)
7918 return 0;
7919
7920 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7921 return 0;
7922
7923 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7924 {
7925 tree nt = TREE_VEC_ELT (newargs, i);
7926 tree ot = TREE_VEC_ELT (oldargs, i);
7927
7928 if (! template_args_equal (ot, nt))
7929 {
7930 if (oldarg_ptr != NULL)
7931 *oldarg_ptr = ot;
7932 if (newarg_ptr != NULL)
7933 *newarg_ptr = nt;
7934 return 0;
7935 }
7936 }
7937 return 1;
7938 }
7939
7940 static void
7941 add_pending_template (tree d)
7942 {
7943 tree ti = (TYPE_P (d)
7944 ? CLASSTYPE_TEMPLATE_INFO (d)
7945 : DECL_TEMPLATE_INFO (d));
7946 struct pending_template *pt;
7947 int level;
7948
7949 if (TI_PENDING_TEMPLATE_FLAG (ti))
7950 return;
7951
7952 /* We are called both from instantiate_decl, where we've already had a
7953 tinst_level pushed, and instantiate_template, where we haven't.
7954 Compensate. */
7955 level = !current_tinst_level || current_tinst_level->decl != d;
7956
7957 if (level)
7958 push_tinst_level (d);
7959
7960 pt = ggc_alloc<pending_template> ();
7961 pt->next = NULL;
7962 pt->tinst = current_tinst_level;
7963 if (last_pending_template)
7964 last_pending_template->next = pt;
7965 else
7966 pending_templates = pt;
7967
7968 last_pending_template = pt;
7969
7970 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7971
7972 if (level)
7973 pop_tinst_level ();
7974 }
7975
7976
7977 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7978 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7979 documentation for TEMPLATE_ID_EXPR. */
7980
7981 tree
7982 lookup_template_function (tree fns, tree arglist)
7983 {
7984 tree type;
7985
7986 if (fns == error_mark_node || arglist == error_mark_node)
7987 return error_mark_node;
7988
7989 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7990
7991 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7992 {
7993 error ("%q#D is not a function template", fns);
7994 return error_mark_node;
7995 }
7996
7997 if (BASELINK_P (fns))
7998 {
7999 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8000 unknown_type_node,
8001 BASELINK_FUNCTIONS (fns),
8002 arglist);
8003 return fns;
8004 }
8005
8006 type = TREE_TYPE (fns);
8007 if (TREE_CODE (fns) == OVERLOAD || !type)
8008 type = unknown_type_node;
8009
8010 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8011 }
8012
8013 /* Within the scope of a template class S<T>, the name S gets bound
8014 (in build_self_reference) to a TYPE_DECL for the class, not a
8015 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8016 or one of its enclosing classes, and that type is a template,
8017 return the associated TEMPLATE_DECL. Otherwise, the original
8018 DECL is returned.
8019
8020 Also handle the case when DECL is a TREE_LIST of ambiguous
8021 injected-class-names from different bases. */
8022
8023 tree
8024 maybe_get_template_decl_from_type_decl (tree decl)
8025 {
8026 if (decl == NULL_TREE)
8027 return decl;
8028
8029 /* DR 176: A lookup that finds an injected-class-name (10.2
8030 [class.member.lookup]) can result in an ambiguity in certain cases
8031 (for example, if it is found in more than one base class). If all of
8032 the injected-class-names that are found refer to specializations of
8033 the same class template, and if the name is followed by a
8034 template-argument-list, the reference refers to the class template
8035 itself and not a specialization thereof, and is not ambiguous. */
8036 if (TREE_CODE (decl) == TREE_LIST)
8037 {
8038 tree t, tmpl = NULL_TREE;
8039 for (t = decl; t; t = TREE_CHAIN (t))
8040 {
8041 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8042 if (!tmpl)
8043 tmpl = elt;
8044 else if (tmpl != elt)
8045 break;
8046 }
8047 if (tmpl && t == NULL_TREE)
8048 return tmpl;
8049 else
8050 return decl;
8051 }
8052
8053 return (decl != NULL_TREE
8054 && DECL_SELF_REFERENCE_P (decl)
8055 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8056 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8057 }
8058
8059 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8060 parameters, find the desired type.
8061
8062 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8063
8064 IN_DECL, if non-NULL, is the template declaration we are trying to
8065 instantiate.
8066
8067 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8068 the class we are looking up.
8069
8070 Issue error and warning messages under control of COMPLAIN.
8071
8072 If the template class is really a local class in a template
8073 function, then the FUNCTION_CONTEXT is the function in which it is
8074 being instantiated.
8075
8076 ??? Note that this function is currently called *twice* for each
8077 template-id: the first time from the parser, while creating the
8078 incomplete type (finish_template_type), and the second type during the
8079 real instantiation (instantiate_template_class). This is surely something
8080 that we want to avoid. It also causes some problems with argument
8081 coercion (see convert_nontype_argument for more information on this). */
8082
8083 static tree
8084 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8085 int entering_scope, tsubst_flags_t complain)
8086 {
8087 tree templ = NULL_TREE, parmlist;
8088 tree t;
8089 spec_entry **slot;
8090 spec_entry *entry;
8091 spec_entry elt;
8092 hashval_t hash;
8093
8094 if (identifier_p (d1))
8095 {
8096 tree value = innermost_non_namespace_value (d1);
8097 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8098 templ = value;
8099 else
8100 {
8101 if (context)
8102 push_decl_namespace (context);
8103 templ = lookup_name (d1);
8104 templ = maybe_get_template_decl_from_type_decl (templ);
8105 if (context)
8106 pop_decl_namespace ();
8107 }
8108 if (templ)
8109 context = DECL_CONTEXT (templ);
8110 }
8111 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8112 {
8113 tree type = TREE_TYPE (d1);
8114
8115 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8116 an implicit typename for the second A. Deal with it. */
8117 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8118 type = TREE_TYPE (type);
8119
8120 if (CLASSTYPE_TEMPLATE_INFO (type))
8121 {
8122 templ = CLASSTYPE_TI_TEMPLATE (type);
8123 d1 = DECL_NAME (templ);
8124 }
8125 }
8126 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8127 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8128 {
8129 templ = TYPE_TI_TEMPLATE (d1);
8130 d1 = DECL_NAME (templ);
8131 }
8132 else if (DECL_TYPE_TEMPLATE_P (d1))
8133 {
8134 templ = d1;
8135 d1 = DECL_NAME (templ);
8136 context = DECL_CONTEXT (templ);
8137 }
8138 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8139 {
8140 templ = d1;
8141 d1 = DECL_NAME (templ);
8142 }
8143
8144 /* Issue an error message if we didn't find a template. */
8145 if (! templ)
8146 {
8147 if (complain & tf_error)
8148 error ("%qT is not a template", d1);
8149 return error_mark_node;
8150 }
8151
8152 if (TREE_CODE (templ) != TEMPLATE_DECL
8153 /* Make sure it's a user visible template, if it was named by
8154 the user. */
8155 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8156 && !PRIMARY_TEMPLATE_P (templ)))
8157 {
8158 if (complain & tf_error)
8159 {
8160 error ("non-template type %qT used as a template", d1);
8161 if (in_decl)
8162 error ("for template declaration %q+D", in_decl);
8163 }
8164 return error_mark_node;
8165 }
8166
8167 complain &= ~tf_user;
8168
8169 /* An alias that just changes the name of a template is equivalent to the
8170 other template, so if any of the arguments are pack expansions, strip
8171 the alias to avoid problems with a pack expansion passed to a non-pack
8172 alias template parameter (DR 1430). */
8173 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8174 templ = get_underlying_template (templ);
8175
8176 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8177 {
8178 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8179 template arguments */
8180
8181 tree parm;
8182 tree arglist2;
8183 tree outer;
8184
8185 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8186
8187 /* Consider an example where a template template parameter declared as
8188
8189 template <class T, class U = std::allocator<T> > class TT
8190
8191 The template parameter level of T and U are one level larger than
8192 of TT. To proper process the default argument of U, say when an
8193 instantiation `TT<int>' is seen, we need to build the full
8194 arguments containing {int} as the innermost level. Outer levels,
8195 available when not appearing as default template argument, can be
8196 obtained from the arguments of the enclosing template.
8197
8198 Suppose that TT is later substituted with std::vector. The above
8199 instantiation is `TT<int, std::allocator<T> >' with TT at
8200 level 1, and T at level 2, while the template arguments at level 1
8201 becomes {std::vector} and the inner level 2 is {int}. */
8202
8203 outer = DECL_CONTEXT (templ);
8204 if (outer)
8205 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8206 else if (current_template_parms)
8207 {
8208 /* This is an argument of the current template, so we haven't set
8209 DECL_CONTEXT yet. */
8210 tree relevant_template_parms;
8211
8212 /* Parameter levels that are greater than the level of the given
8213 template template parm are irrelevant. */
8214 relevant_template_parms = current_template_parms;
8215 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8216 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8217 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8218
8219 outer = template_parms_to_args (relevant_template_parms);
8220 }
8221
8222 if (outer)
8223 arglist = add_to_template_args (outer, arglist);
8224
8225 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8226 complain,
8227 /*require_all_args=*/true,
8228 /*use_default_args=*/true);
8229 if (arglist2 == error_mark_node
8230 || (!uses_template_parms (arglist2)
8231 && check_instantiated_args (templ, arglist2, complain)))
8232 return error_mark_node;
8233
8234 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8235 return parm;
8236 }
8237 else
8238 {
8239 tree template_type = TREE_TYPE (templ);
8240 tree gen_tmpl;
8241 tree type_decl;
8242 tree found = NULL_TREE;
8243 int arg_depth;
8244 int parm_depth;
8245 int is_dependent_type;
8246 int use_partial_inst_tmpl = false;
8247
8248 if (template_type == error_mark_node)
8249 /* An error occurred while building the template TEMPL, and a
8250 diagnostic has most certainly been emitted for that
8251 already. Let's propagate that error. */
8252 return error_mark_node;
8253
8254 gen_tmpl = most_general_template (templ);
8255 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8256 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8257 arg_depth = TMPL_ARGS_DEPTH (arglist);
8258
8259 if (arg_depth == 1 && parm_depth > 1)
8260 {
8261 /* We've been given an incomplete set of template arguments.
8262 For example, given:
8263
8264 template <class T> struct S1 {
8265 template <class U> struct S2 {};
8266 template <class U> struct S2<U*> {};
8267 };
8268
8269 we will be called with an ARGLIST of `U*', but the
8270 TEMPLATE will be `template <class T> template
8271 <class U> struct S1<T>::S2'. We must fill in the missing
8272 arguments. */
8273 arglist
8274 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8275 arglist);
8276 arg_depth = TMPL_ARGS_DEPTH (arglist);
8277 }
8278
8279 /* Now we should have enough arguments. */
8280 gcc_assert (parm_depth == arg_depth);
8281
8282 /* From here on, we're only interested in the most general
8283 template. */
8284
8285 /* Calculate the BOUND_ARGS. These will be the args that are
8286 actually tsubst'd into the definition to create the
8287 instantiation. */
8288 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8289 complain,
8290 /*require_all_args=*/true,
8291 /*use_default_args=*/true);
8292
8293 if (arglist == error_mark_node)
8294 /* We were unable to bind the arguments. */
8295 return error_mark_node;
8296
8297 /* In the scope of a template class, explicit references to the
8298 template class refer to the type of the template, not any
8299 instantiation of it. For example, in:
8300
8301 template <class T> class C { void f(C<T>); }
8302
8303 the `C<T>' is just the same as `C'. Outside of the
8304 class, however, such a reference is an instantiation. */
8305 if ((entering_scope
8306 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8307 || currently_open_class (template_type))
8308 /* comp_template_args is expensive, check it last. */
8309 && comp_template_args (TYPE_TI_ARGS (template_type),
8310 arglist))
8311 return template_type;
8312
8313 /* If we already have this specialization, return it. */
8314 elt.tmpl = gen_tmpl;
8315 elt.args = arglist;
8316 elt.spec = NULL_TREE;
8317 hash = spec_hasher::hash (&elt);
8318 entry = type_specializations->find_with_hash (&elt, hash);
8319
8320 if (entry)
8321 return entry->spec;
8322
8323 /* If the the template's constraints are not satisfied,
8324 then we cannot form a valid type.
8325
8326 Note that the check is deferred until after the hash
8327 lookup. This prevents redundant checks on previously
8328 instantiated specializations. */
8329 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8330 {
8331 if (complain & tf_error)
8332 {
8333 error ("template constraint failure");
8334 diagnose_constraints (input_location, gen_tmpl, arglist);
8335 }
8336 return error_mark_node;
8337 }
8338
8339 is_dependent_type = uses_template_parms (arglist);
8340
8341 /* If the deduced arguments are invalid, then the binding
8342 failed. */
8343 if (!is_dependent_type
8344 && check_instantiated_args (gen_tmpl,
8345 INNERMOST_TEMPLATE_ARGS (arglist),
8346 complain))
8347 return error_mark_node;
8348
8349 if (!is_dependent_type
8350 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8351 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8352 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8353 {
8354 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8355 DECL_NAME (gen_tmpl),
8356 /*tag_scope=*/ts_global);
8357 return found;
8358 }
8359
8360 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8361 complain, in_decl);
8362 if (context == error_mark_node)
8363 return error_mark_node;
8364
8365 if (!context)
8366 context = global_namespace;
8367
8368 /* Create the type. */
8369 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8370 {
8371 /* The user referred to a specialization of an alias
8372 template represented by GEN_TMPL.
8373
8374 [temp.alias]/2 says:
8375
8376 When a template-id refers to the specialization of an
8377 alias template, it is equivalent to the associated
8378 type obtained by substitution of its
8379 template-arguments for the template-parameters in the
8380 type-id of the alias template. */
8381
8382 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8383 /* Note that the call above (by indirectly calling
8384 register_specialization in tsubst_decl) registers the
8385 TYPE_DECL representing the specialization of the alias
8386 template. So next time someone substitutes ARGLIST for
8387 the template parms into the alias template (GEN_TMPL),
8388 she'll get that TYPE_DECL back. */
8389
8390 if (t == error_mark_node)
8391 return t;
8392 }
8393 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8394 {
8395 if (!is_dependent_type)
8396 {
8397 set_current_access_from_decl (TYPE_NAME (template_type));
8398 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8399 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8400 arglist, complain, in_decl),
8401 SCOPED_ENUM_P (template_type), NULL);
8402
8403 if (t == error_mark_node)
8404 return t;
8405 }
8406 else
8407 {
8408 /* We don't want to call start_enum for this type, since
8409 the values for the enumeration constants may involve
8410 template parameters. And, no one should be interested
8411 in the enumeration constants for such a type. */
8412 t = cxx_make_type (ENUMERAL_TYPE);
8413 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8414 }
8415 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8416 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8417 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8418 }
8419 else if (CLASS_TYPE_P (template_type))
8420 {
8421 t = make_class_type (TREE_CODE (template_type));
8422 CLASSTYPE_DECLARED_CLASS (t)
8423 = CLASSTYPE_DECLARED_CLASS (template_type);
8424 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8425 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
8426
8427 /* A local class. Make sure the decl gets registered properly. */
8428 if (context == current_function_decl)
8429 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8430
8431 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8432 /* This instantiation is another name for the primary
8433 template type. Set the TYPE_CANONICAL field
8434 appropriately. */
8435 TYPE_CANONICAL (t) = template_type;
8436 else if (any_template_arguments_need_structural_equality_p (arglist))
8437 /* Some of the template arguments require structural
8438 equality testing, so this template class requires
8439 structural equality testing. */
8440 SET_TYPE_STRUCTURAL_EQUALITY (t);
8441 }
8442 else
8443 gcc_unreachable ();
8444
8445 /* If we called start_enum or pushtag above, this information
8446 will already be set up. */
8447 if (!TYPE_NAME (t))
8448 {
8449 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8450
8451 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8452 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8453 DECL_SOURCE_LOCATION (type_decl)
8454 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8455 }
8456 else
8457 type_decl = TYPE_NAME (t);
8458
8459 if (CLASS_TYPE_P (template_type))
8460 {
8461 TREE_PRIVATE (type_decl)
8462 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8463 TREE_PROTECTED (type_decl)
8464 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8465 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8466 {
8467 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8468 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8469 }
8470 }
8471
8472 if (OVERLOAD_TYPE_P (t)
8473 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8474 {
8475 static const char *tags[] = {"abi_tag", "may_alias"};
8476
8477 for (unsigned ix = 0; ix != 2; ix++)
8478 {
8479 tree attributes
8480 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8481
8482 if (!attributes)
8483 ;
8484 else if (!TREE_CHAIN (attributes) && !TYPE_ATTRIBUTES (t))
8485 TYPE_ATTRIBUTES (t) = attributes;
8486 else
8487 TYPE_ATTRIBUTES (t)
8488 = tree_cons (TREE_PURPOSE (attributes),
8489 TREE_VALUE (attributes),
8490 TYPE_ATTRIBUTES (t));
8491 }
8492 }
8493
8494 /* Let's consider the explicit specialization of a member
8495 of a class template specialization that is implicitly instantiated,
8496 e.g.:
8497 template<class T>
8498 struct S
8499 {
8500 template<class U> struct M {}; //#0
8501 };
8502
8503 template<>
8504 template<>
8505 struct S<int>::M<char> //#1
8506 {
8507 int i;
8508 };
8509 [temp.expl.spec]/4 says this is valid.
8510
8511 In this case, when we write:
8512 S<int>::M<char> m;
8513
8514 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8515 the one of #0.
8516
8517 When we encounter #1, we want to store the partial instantiation
8518 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8519
8520 For all cases other than this "explicit specialization of member of a
8521 class template", we just want to store the most general template into
8522 the CLASSTYPE_TI_TEMPLATE of M.
8523
8524 This case of "explicit specialization of member of a class template"
8525 only happens when:
8526 1/ the enclosing class is an instantiation of, and therefore not
8527 the same as, the context of the most general template, and
8528 2/ we aren't looking at the partial instantiation itself, i.e.
8529 the innermost arguments are not the same as the innermost parms of
8530 the most general template.
8531
8532 So it's only when 1/ and 2/ happens that we want to use the partial
8533 instantiation of the member template in lieu of its most general
8534 template. */
8535
8536 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8537 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8538 /* the enclosing class must be an instantiation... */
8539 && CLASS_TYPE_P (context)
8540 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8541 {
8542 tree partial_inst_args;
8543 TREE_VEC_LENGTH (arglist)--;
8544 ++processing_template_decl;
8545 partial_inst_args =
8546 tsubst (INNERMOST_TEMPLATE_ARGS
8547 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8548 arglist, complain, NULL_TREE);
8549 --processing_template_decl;
8550 TREE_VEC_LENGTH (arglist)++;
8551 use_partial_inst_tmpl =
8552 /*...and we must not be looking at the partial instantiation
8553 itself. */
8554 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8555 partial_inst_args);
8556 }
8557
8558 if (!use_partial_inst_tmpl)
8559 /* This case is easy; there are no member templates involved. */
8560 found = gen_tmpl;
8561 else
8562 {
8563 /* This is a full instantiation of a member template. Find
8564 the partial instantiation of which this is an instance. */
8565
8566 /* Temporarily reduce by one the number of levels in the ARGLIST
8567 so as to avoid comparing the last set of arguments. */
8568 TREE_VEC_LENGTH (arglist)--;
8569 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8570 TREE_VEC_LENGTH (arglist)++;
8571 /* FOUND is either a proper class type, or an alias
8572 template specialization. In the later case, it's a
8573 TYPE_DECL, resulting from the substituting of arguments
8574 for parameters in the TYPE_DECL of the alias template
8575 done earlier. So be careful while getting the template
8576 of FOUND. */
8577 found = TREE_CODE (found) == TYPE_DECL
8578 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8579 : CLASSTYPE_TI_TEMPLATE (found);
8580 }
8581
8582 // Build template info for the new specialization.
8583 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8584
8585 elt.spec = t;
8586 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8587 entry = ggc_alloc<spec_entry> ();
8588 *entry = elt;
8589 *slot = entry;
8590
8591 /* Note this use of the partial instantiation so we can check it
8592 later in maybe_process_partial_specialization. */
8593 DECL_TEMPLATE_INSTANTIATIONS (found)
8594 = tree_cons (arglist, t,
8595 DECL_TEMPLATE_INSTANTIATIONS (found));
8596
8597 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8598 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8599 /* Now that the type has been registered on the instantiations
8600 list, we set up the enumerators. Because the enumeration
8601 constants may involve the enumeration type itself, we make
8602 sure to register the type first, and then create the
8603 constants. That way, doing tsubst_expr for the enumeration
8604 constants won't result in recursive calls here; we'll find
8605 the instantiation and exit above. */
8606 tsubst_enum (template_type, t, arglist);
8607
8608 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8609 /* If the type makes use of template parameters, the
8610 code that generates debugging information will crash. */
8611 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8612
8613 /* Possibly limit visibility based on template args. */
8614 TREE_PUBLIC (type_decl) = 1;
8615 determine_visibility (type_decl);
8616
8617 inherit_targ_abi_tags (t);
8618
8619 return t;
8620 }
8621 }
8622
8623 /* Wrapper for lookup_template_class_1. */
8624
8625 tree
8626 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8627 int entering_scope, tsubst_flags_t complain)
8628 {
8629 tree ret;
8630 timevar_push (TV_TEMPLATE_INST);
8631 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8632 entering_scope, complain);
8633 timevar_pop (TV_TEMPLATE_INST);
8634 return ret;
8635 }
8636
8637 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8638
8639 tree
8640 lookup_template_variable (tree templ, tree arglist)
8641 {
8642 /* The type of the expression is NULL_TREE since the template-id could refer
8643 to an explicit or partial specialization. */
8644 tree type = NULL_TREE;
8645 if (flag_concepts && variable_concept_p (templ))
8646 /* Except that concepts are always bool. */
8647 type = boolean_type_node;
8648 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8649 }
8650
8651 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8652
8653 tree
8654 finish_template_variable (tree var, tsubst_flags_t complain)
8655 {
8656 tree templ = TREE_OPERAND (var, 0);
8657 tree arglist = TREE_OPERAND (var, 1);
8658
8659 /* We never want to return a VAR_DECL for a variable concept, since they
8660 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8661 bool concept_p = flag_concepts && variable_concept_p (templ);
8662 if (concept_p && processing_template_decl)
8663 return var;
8664
8665 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8666 arglist = add_outermost_template_args (tmpl_args, arglist);
8667
8668 tree parms = DECL_TEMPLATE_PARMS (templ);
8669 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8670 /*req_all*/true,
8671 /*use_default*/true);
8672
8673 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
8674 {
8675 if (complain & tf_error)
8676 {
8677 error ("constraints for %qD not satisfied", templ);
8678 diagnose_constraints (location_of (var), templ, arglist);
8679 }
8680 return error_mark_node;
8681 }
8682
8683 /* If a template-id refers to a specialization of a variable
8684 concept, then the expression is true if and only if the
8685 concept's constraints are satisfied by the given template
8686 arguments.
8687
8688 NOTE: This is an extension of Concepts Lite TS that
8689 allows constraints to be used in expressions. */
8690 if (concept_p)
8691 {
8692 tree decl = DECL_TEMPLATE_RESULT (templ);
8693 return evaluate_variable_concept (decl, arglist);
8694 }
8695
8696 return instantiate_template (templ, arglist, complain);
8697 }
8698 \f
8699 struct pair_fn_data
8700 {
8701 tree_fn_t fn;
8702 void *data;
8703 /* True when we should also visit template parameters that occur in
8704 non-deduced contexts. */
8705 bool include_nondeduced_p;
8706 hash_set<tree> *visited;
8707 };
8708
8709 /* Called from for_each_template_parm via walk_tree. */
8710
8711 static tree
8712 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8713 {
8714 tree t = *tp;
8715 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8716 tree_fn_t fn = pfd->fn;
8717 void *data = pfd->data;
8718 tree result = NULL_TREE;
8719
8720 #define WALK_SUBTREE(NODE) \
8721 do \
8722 { \
8723 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8724 pfd->include_nondeduced_p); \
8725 if (result) goto out; \
8726 } \
8727 while (0)
8728
8729 if (TYPE_P (t)
8730 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
8731 WALK_SUBTREE (TYPE_CONTEXT (t));
8732
8733 switch (TREE_CODE (t))
8734 {
8735 case RECORD_TYPE:
8736 if (TYPE_PTRMEMFUNC_P (t))
8737 break;
8738 /* Fall through. */
8739
8740 case UNION_TYPE:
8741 case ENUMERAL_TYPE:
8742 if (!TYPE_TEMPLATE_INFO (t))
8743 *walk_subtrees = 0;
8744 else
8745 WALK_SUBTREE (TYPE_TI_ARGS (t));
8746 break;
8747
8748 case INTEGER_TYPE:
8749 WALK_SUBTREE (TYPE_MIN_VALUE (t));
8750 WALK_SUBTREE (TYPE_MAX_VALUE (t));
8751 break;
8752
8753 case METHOD_TYPE:
8754 /* Since we're not going to walk subtrees, we have to do this
8755 explicitly here. */
8756 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
8757 /* Fall through. */
8758
8759 case FUNCTION_TYPE:
8760 /* Check the return type. */
8761 WALK_SUBTREE (TREE_TYPE (t));
8762
8763 /* Check the parameter types. Since default arguments are not
8764 instantiated until they are needed, the TYPE_ARG_TYPES may
8765 contain expressions that involve template parameters. But,
8766 no-one should be looking at them yet. And, once they're
8767 instantiated, they don't contain template parameters, so
8768 there's no point in looking at them then, either. */
8769 {
8770 tree parm;
8771
8772 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8773 WALK_SUBTREE (TREE_VALUE (parm));
8774
8775 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8776 want walk_tree walking into them itself. */
8777 *walk_subtrees = 0;
8778 }
8779 break;
8780
8781 case TYPEOF_TYPE:
8782 case UNDERLYING_TYPE:
8783 if (pfd->include_nondeduced_p
8784 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8785 pfd->visited,
8786 pfd->include_nondeduced_p))
8787 return error_mark_node;
8788 break;
8789
8790 case FUNCTION_DECL:
8791 case VAR_DECL:
8792 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8793 WALK_SUBTREE (DECL_TI_ARGS (t));
8794 /* Fall through. */
8795
8796 case PARM_DECL:
8797 case CONST_DECL:
8798 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
8799 WALK_SUBTREE (DECL_INITIAL (t));
8800 if (DECL_CONTEXT (t)
8801 && pfd->include_nondeduced_p)
8802 WALK_SUBTREE (DECL_CONTEXT (t));
8803 break;
8804
8805 case BOUND_TEMPLATE_TEMPLATE_PARM:
8806 /* Record template parameters such as `T' inside `TT<T>'. */
8807 WALK_SUBTREE (TYPE_TI_ARGS (t));
8808 /* Fall through. */
8809
8810 case TEMPLATE_TEMPLATE_PARM:
8811 case TEMPLATE_TYPE_PARM:
8812 case TEMPLATE_PARM_INDEX:
8813 if (fn && (*fn)(t, data))
8814 return t;
8815 else if (!fn)
8816 return t;
8817 break;
8818
8819 case TEMPLATE_DECL:
8820 /* A template template parameter is encountered. */
8821 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8822 WALK_SUBTREE (TREE_TYPE (t));
8823
8824 /* Already substituted template template parameter */
8825 *walk_subtrees = 0;
8826 break;
8827
8828 case TYPENAME_TYPE:
8829 if (!fn)
8830 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
8831 break;
8832
8833 case CONSTRUCTOR:
8834 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8835 && pfd->include_nondeduced_p)
8836 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
8837 break;
8838
8839 case INDIRECT_REF:
8840 case COMPONENT_REF:
8841 /* If there's no type, then this thing must be some expression
8842 involving template parameters. */
8843 if (!fn && !TREE_TYPE (t))
8844 return error_mark_node;
8845 break;
8846
8847 case MODOP_EXPR:
8848 case CAST_EXPR:
8849 case IMPLICIT_CONV_EXPR:
8850 case REINTERPRET_CAST_EXPR:
8851 case CONST_CAST_EXPR:
8852 case STATIC_CAST_EXPR:
8853 case DYNAMIC_CAST_EXPR:
8854 case ARROW_EXPR:
8855 case DOTSTAR_EXPR:
8856 case TYPEID_EXPR:
8857 case PSEUDO_DTOR_EXPR:
8858 if (!fn)
8859 return error_mark_node;
8860 break;
8861
8862 default:
8863 break;
8864 }
8865
8866 #undef WALK_SUBTREE
8867
8868 /* We didn't find any template parameters we liked. */
8869 out:
8870 return result;
8871 }
8872
8873 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8874 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8875 call FN with the parameter and the DATA.
8876 If FN returns nonzero, the iteration is terminated, and
8877 for_each_template_parm returns 1. Otherwise, the iteration
8878 continues. If FN never returns a nonzero value, the value
8879 returned by for_each_template_parm is 0. If FN is NULL, it is
8880 considered to be the function which always returns 1.
8881
8882 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8883 parameters that occur in non-deduced contexts. When false, only
8884 visits those template parameters that can be deduced. */
8885
8886 static tree
8887 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8888 hash_set<tree> *visited,
8889 bool include_nondeduced_p)
8890 {
8891 struct pair_fn_data pfd;
8892 tree result;
8893
8894 /* Set up. */
8895 pfd.fn = fn;
8896 pfd.data = data;
8897 pfd.include_nondeduced_p = include_nondeduced_p;
8898
8899 /* Walk the tree. (Conceptually, we would like to walk without
8900 duplicates, but for_each_template_parm_r recursively calls
8901 for_each_template_parm, so we would need to reorganize a fair
8902 bit to use walk_tree_without_duplicates, so we keep our own
8903 visited list.) */
8904 if (visited)
8905 pfd.visited = visited;
8906 else
8907 pfd.visited = new hash_set<tree>;
8908 result = cp_walk_tree (&t,
8909 for_each_template_parm_r,
8910 &pfd,
8911 pfd.visited);
8912
8913 /* Clean up. */
8914 if (!visited)
8915 {
8916 delete pfd.visited;
8917 pfd.visited = 0;
8918 }
8919
8920 return result;
8921 }
8922
8923 /* Returns true if T depends on any template parameter. */
8924
8925 int
8926 uses_template_parms (tree t)
8927 {
8928 if (t == NULL_TREE)
8929 return false;
8930
8931 bool dependent_p;
8932 int saved_processing_template_decl;
8933
8934 saved_processing_template_decl = processing_template_decl;
8935 if (!saved_processing_template_decl)
8936 processing_template_decl = 1;
8937 if (TYPE_P (t))
8938 dependent_p = dependent_type_p (t);
8939 else if (TREE_CODE (t) == TREE_VEC)
8940 dependent_p = any_dependent_template_arguments_p (t);
8941 else if (TREE_CODE (t) == TREE_LIST)
8942 dependent_p = (uses_template_parms (TREE_VALUE (t))
8943 || uses_template_parms (TREE_CHAIN (t)));
8944 else if (TREE_CODE (t) == TYPE_DECL)
8945 dependent_p = dependent_type_p (TREE_TYPE (t));
8946 else if (DECL_P (t)
8947 || EXPR_P (t)
8948 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8949 || TREE_CODE (t) == OVERLOAD
8950 || BASELINK_P (t)
8951 || identifier_p (t)
8952 || TREE_CODE (t) == TRAIT_EXPR
8953 || TREE_CODE (t) == CONSTRUCTOR
8954 || CONSTANT_CLASS_P (t))
8955 dependent_p = (type_dependent_expression_p (t)
8956 || value_dependent_expression_p (t));
8957 else
8958 {
8959 gcc_assert (t == error_mark_node);
8960 dependent_p = false;
8961 }
8962
8963 processing_template_decl = saved_processing_template_decl;
8964
8965 return dependent_p;
8966 }
8967
8968 /* Returns true iff current_function_decl is an incompletely instantiated
8969 template. Useful instead of processing_template_decl because the latter
8970 is set to 0 during instantiate_non_dependent_expr. */
8971
8972 bool
8973 in_template_function (void)
8974 {
8975 tree fn = current_function_decl;
8976 bool ret;
8977 ++processing_template_decl;
8978 ret = (fn && DECL_LANG_SPECIFIC (fn)
8979 && DECL_TEMPLATE_INFO (fn)
8980 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8981 --processing_template_decl;
8982 return ret;
8983 }
8984
8985 /* Returns true if T depends on any template parameter with level LEVEL. */
8986
8987 bool
8988 uses_template_parms_level (tree t, int level)
8989 {
8990 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8991 /*include_nondeduced_p=*/true);
8992 }
8993
8994 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8995 ill-formed translation unit, i.e. a variable or function that isn't
8996 usable in a constant expression. */
8997
8998 static inline bool
8999 neglectable_inst_p (tree d)
9000 {
9001 return (DECL_P (d)
9002 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9003 : decl_maybe_constant_var_p (d)));
9004 }
9005
9006 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9007 neglectable and instantiated from within an erroneous instantiation. */
9008
9009 static bool
9010 limit_bad_template_recursion (tree decl)
9011 {
9012 struct tinst_level *lev = current_tinst_level;
9013 int errs = errorcount + sorrycount;
9014 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9015 return false;
9016
9017 for (; lev; lev = lev->next)
9018 if (neglectable_inst_p (lev->decl))
9019 break;
9020
9021 return (lev && errs > lev->errors);
9022 }
9023
9024 static int tinst_depth;
9025 extern int max_tinst_depth;
9026 int depth_reached;
9027
9028 static GTY(()) struct tinst_level *last_error_tinst_level;
9029
9030 /* We're starting to instantiate D; record the template instantiation context
9031 for diagnostics and to restore it later. */
9032
9033 bool
9034 push_tinst_level (tree d)
9035 {
9036 return push_tinst_level_loc (d, input_location);
9037 }
9038
9039 /* We're starting to instantiate D; record the template instantiation context
9040 at LOC for diagnostics and to restore it later. */
9041
9042 bool
9043 push_tinst_level_loc (tree d, location_t loc)
9044 {
9045 struct tinst_level *new_level;
9046
9047 if (tinst_depth >= max_tinst_depth)
9048 {
9049 fatal_error (input_location,
9050 "template instantiation depth exceeds maximum of %d"
9051 " (use -ftemplate-depth= to increase the maximum)",
9052 max_tinst_depth);
9053 return false;
9054 }
9055
9056 /* If the current instantiation caused problems, don't let it instantiate
9057 anything else. Do allow deduction substitution and decls usable in
9058 constant expressions. */
9059 if (limit_bad_template_recursion (d))
9060 return false;
9061
9062 new_level = ggc_alloc<tinst_level> ();
9063 new_level->decl = d;
9064 new_level->locus = loc;
9065 new_level->errors = errorcount+sorrycount;
9066 new_level->in_system_header_p = in_system_header_at (input_location);
9067 new_level->next = current_tinst_level;
9068 current_tinst_level = new_level;
9069
9070 ++tinst_depth;
9071 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9072 depth_reached = tinst_depth;
9073
9074 return true;
9075 }
9076
9077 /* We're done instantiating this template; return to the instantiation
9078 context. */
9079
9080 void
9081 pop_tinst_level (void)
9082 {
9083 /* Restore the filename and line number stashed away when we started
9084 this instantiation. */
9085 input_location = current_tinst_level->locus;
9086 current_tinst_level = current_tinst_level->next;
9087 --tinst_depth;
9088 }
9089
9090 /* We're instantiating a deferred template; restore the template
9091 instantiation context in which the instantiation was requested, which
9092 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9093
9094 static tree
9095 reopen_tinst_level (struct tinst_level *level)
9096 {
9097 struct tinst_level *t;
9098
9099 tinst_depth = 0;
9100 for (t = level; t; t = t->next)
9101 ++tinst_depth;
9102
9103 current_tinst_level = level;
9104 pop_tinst_level ();
9105 if (current_tinst_level)
9106 current_tinst_level->errors = errorcount+sorrycount;
9107 return level->decl;
9108 }
9109
9110 /* Returns the TINST_LEVEL which gives the original instantiation
9111 context. */
9112
9113 struct tinst_level *
9114 outermost_tinst_level (void)
9115 {
9116 struct tinst_level *level = current_tinst_level;
9117 if (level)
9118 while (level->next)
9119 level = level->next;
9120 return level;
9121 }
9122
9123 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9124 vector of template arguments, as for tsubst.
9125
9126 Returns an appropriate tsubst'd friend declaration. */
9127
9128 static tree
9129 tsubst_friend_function (tree decl, tree args)
9130 {
9131 tree new_friend;
9132
9133 if (TREE_CODE (decl) == FUNCTION_DECL
9134 && DECL_TEMPLATE_INSTANTIATION (decl)
9135 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9136 /* This was a friend declared with an explicit template
9137 argument list, e.g.:
9138
9139 friend void f<>(T);
9140
9141 to indicate that f was a template instantiation, not a new
9142 function declaration. Now, we have to figure out what
9143 instantiation of what template. */
9144 {
9145 tree template_id, arglist, fns;
9146 tree new_args;
9147 tree tmpl;
9148 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9149
9150 /* Friend functions are looked up in the containing namespace scope.
9151 We must enter that scope, to avoid finding member functions of the
9152 current class with same name. */
9153 push_nested_namespace (ns);
9154 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9155 tf_warning_or_error, NULL_TREE,
9156 /*integral_constant_expression_p=*/false);
9157 pop_nested_namespace (ns);
9158 arglist = tsubst (DECL_TI_ARGS (decl), args,
9159 tf_warning_or_error, NULL_TREE);
9160 template_id = lookup_template_function (fns, arglist);
9161
9162 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9163 tmpl = determine_specialization (template_id, new_friend,
9164 &new_args,
9165 /*need_member_template=*/0,
9166 TREE_VEC_LENGTH (args),
9167 tsk_none);
9168 return instantiate_template (tmpl, new_args, tf_error);
9169 }
9170
9171 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9172
9173 /* The NEW_FRIEND will look like an instantiation, to the
9174 compiler, but is not an instantiation from the point of view of
9175 the language. For example, we might have had:
9176
9177 template <class T> struct S {
9178 template <class U> friend void f(T, U);
9179 };
9180
9181 Then, in S<int>, template <class U> void f(int, U) is not an
9182 instantiation of anything. */
9183 if (new_friend == error_mark_node)
9184 return error_mark_node;
9185
9186 DECL_USE_TEMPLATE (new_friend) = 0;
9187 if (TREE_CODE (decl) == TEMPLATE_DECL)
9188 {
9189 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9190 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9191 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9192 }
9193
9194 /* The mangled name for the NEW_FRIEND is incorrect. The function
9195 is not a template instantiation and should not be mangled like
9196 one. Therefore, we forget the mangling here; we'll recompute it
9197 later if we need it. */
9198 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9199 {
9200 SET_DECL_RTL (new_friend, NULL);
9201 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9202 }
9203
9204 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9205 {
9206 tree old_decl;
9207 tree new_friend_template_info;
9208 tree new_friend_result_template_info;
9209 tree ns;
9210 int new_friend_is_defn;
9211
9212 /* We must save some information from NEW_FRIEND before calling
9213 duplicate decls since that function will free NEW_FRIEND if
9214 possible. */
9215 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9216 new_friend_is_defn =
9217 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9218 (template_for_substitution (new_friend)))
9219 != NULL_TREE);
9220 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9221 {
9222 /* This declaration is a `primary' template. */
9223 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9224
9225 new_friend_result_template_info
9226 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9227 }
9228 else
9229 new_friend_result_template_info = NULL_TREE;
9230
9231 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9232 if (new_friend_is_defn)
9233 DECL_INITIAL (new_friend) = error_mark_node;
9234
9235 /* Inside pushdecl_namespace_level, we will push into the
9236 current namespace. However, the friend function should go
9237 into the namespace of the template. */
9238 ns = decl_namespace_context (new_friend);
9239 push_nested_namespace (ns);
9240 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9241 pop_nested_namespace (ns);
9242
9243 if (old_decl == error_mark_node)
9244 return error_mark_node;
9245
9246 if (old_decl != new_friend)
9247 {
9248 /* This new friend declaration matched an existing
9249 declaration. For example, given:
9250
9251 template <class T> void f(T);
9252 template <class U> class C {
9253 template <class T> friend void f(T) {}
9254 };
9255
9256 the friend declaration actually provides the definition
9257 of `f', once C has been instantiated for some type. So,
9258 old_decl will be the out-of-class template declaration,
9259 while new_friend is the in-class definition.
9260
9261 But, if `f' was called before this point, the
9262 instantiation of `f' will have DECL_TI_ARGS corresponding
9263 to `T' but not to `U', references to which might appear
9264 in the definition of `f'. Previously, the most general
9265 template for an instantiation of `f' was the out-of-class
9266 version; now it is the in-class version. Therefore, we
9267 run through all specialization of `f', adding to their
9268 DECL_TI_ARGS appropriately. In particular, they need a
9269 new set of outer arguments, corresponding to the
9270 arguments for this class instantiation.
9271
9272 The same situation can arise with something like this:
9273
9274 friend void f(int);
9275 template <class T> class C {
9276 friend void f(T) {}
9277 };
9278
9279 when `C<int>' is instantiated. Now, `f(int)' is defined
9280 in the class. */
9281
9282 if (!new_friend_is_defn)
9283 /* On the other hand, if the in-class declaration does
9284 *not* provide a definition, then we don't want to alter
9285 existing definitions. We can just leave everything
9286 alone. */
9287 ;
9288 else
9289 {
9290 tree new_template = TI_TEMPLATE (new_friend_template_info);
9291 tree new_args = TI_ARGS (new_friend_template_info);
9292
9293 /* Overwrite whatever template info was there before, if
9294 any, with the new template information pertaining to
9295 the declaration. */
9296 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9297
9298 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9299 {
9300 /* We should have called reregister_specialization in
9301 duplicate_decls. */
9302 gcc_assert (retrieve_specialization (new_template,
9303 new_args, 0)
9304 == old_decl);
9305
9306 /* Instantiate it if the global has already been used. */
9307 if (DECL_ODR_USED (old_decl))
9308 instantiate_decl (old_decl, /*defer_ok=*/true,
9309 /*expl_inst_class_mem_p=*/false);
9310 }
9311 else
9312 {
9313 tree t;
9314
9315 /* Indicate that the old function template is a partial
9316 instantiation. */
9317 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9318 = new_friend_result_template_info;
9319
9320 gcc_assert (new_template
9321 == most_general_template (new_template));
9322 gcc_assert (new_template != old_decl);
9323
9324 /* Reassign any specializations already in the hash table
9325 to the new more general template, and add the
9326 additional template args. */
9327 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9328 t != NULL_TREE;
9329 t = TREE_CHAIN (t))
9330 {
9331 tree spec = TREE_VALUE (t);
9332 spec_entry elt;
9333
9334 elt.tmpl = old_decl;
9335 elt.args = DECL_TI_ARGS (spec);
9336 elt.spec = NULL_TREE;
9337
9338 decl_specializations->remove_elt (&elt);
9339
9340 DECL_TI_ARGS (spec)
9341 = add_outermost_template_args (new_args,
9342 DECL_TI_ARGS (spec));
9343
9344 register_specialization
9345 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9346
9347 }
9348 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9349 }
9350 }
9351
9352 /* The information from NEW_FRIEND has been merged into OLD_DECL
9353 by duplicate_decls. */
9354 new_friend = old_decl;
9355 }
9356 }
9357 else
9358 {
9359 tree context = DECL_CONTEXT (new_friend);
9360 bool dependent_p;
9361
9362 /* In the code
9363 template <class T> class C {
9364 template <class U> friend void C1<U>::f (); // case 1
9365 friend void C2<T>::f (); // case 2
9366 };
9367 we only need to make sure CONTEXT is a complete type for
9368 case 2. To distinguish between the two cases, we note that
9369 CONTEXT of case 1 remains dependent type after tsubst while
9370 this isn't true for case 2. */
9371 ++processing_template_decl;
9372 dependent_p = dependent_type_p (context);
9373 --processing_template_decl;
9374
9375 if (!dependent_p
9376 && !complete_type_or_else (context, NULL_TREE))
9377 return error_mark_node;
9378
9379 if (COMPLETE_TYPE_P (context))
9380 {
9381 tree fn = new_friend;
9382 /* do_friend adds the TEMPLATE_DECL for any member friend
9383 template even if it isn't a member template, i.e.
9384 template <class T> friend A<T>::f();
9385 Look through it in that case. */
9386 if (TREE_CODE (fn) == TEMPLATE_DECL
9387 && !PRIMARY_TEMPLATE_P (fn))
9388 fn = DECL_TEMPLATE_RESULT (fn);
9389 /* Check to see that the declaration is really present, and,
9390 possibly obtain an improved declaration. */
9391 fn = check_classfn (context, fn, NULL_TREE);
9392
9393 if (fn)
9394 new_friend = fn;
9395 }
9396 }
9397
9398 return new_friend;
9399 }
9400
9401 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9402 template arguments, as for tsubst.
9403
9404 Returns an appropriate tsubst'd friend type or error_mark_node on
9405 failure. */
9406
9407 static tree
9408 tsubst_friend_class (tree friend_tmpl, tree args)
9409 {
9410 tree friend_type;
9411 tree tmpl;
9412 tree context;
9413
9414 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9415 {
9416 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9417 return TREE_TYPE (t);
9418 }
9419
9420 context = CP_DECL_CONTEXT (friend_tmpl);
9421
9422 if (context != global_namespace)
9423 {
9424 if (TREE_CODE (context) == NAMESPACE_DECL)
9425 push_nested_namespace (context);
9426 else
9427 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9428 }
9429
9430 /* Look for a class template declaration. We look for hidden names
9431 because two friend declarations of the same template are the
9432 same. For example, in:
9433
9434 struct A {
9435 template <typename> friend class F;
9436 };
9437 template <typename> struct B {
9438 template <typename> friend class F;
9439 };
9440
9441 both F templates are the same. */
9442 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9443 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9444
9445 /* But, if we don't find one, it might be because we're in a
9446 situation like this:
9447
9448 template <class T>
9449 struct S {
9450 template <class U>
9451 friend struct S;
9452 };
9453
9454 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9455 for `S<int>', not the TEMPLATE_DECL. */
9456 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9457 {
9458 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9459 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9460 }
9461
9462 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9463 {
9464 /* The friend template has already been declared. Just
9465 check to see that the declarations match, and install any new
9466 default parameters. We must tsubst the default parameters,
9467 of course. We only need the innermost template parameters
9468 because that is all that redeclare_class_template will look
9469 at. */
9470 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9471 > TMPL_ARGS_DEPTH (args))
9472 {
9473 tree parms;
9474 location_t saved_input_location;
9475 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9476 args, tf_warning_or_error);
9477
9478 saved_input_location = input_location;
9479 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9480 tree cons = get_constraints (tmpl);
9481 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9482 input_location = saved_input_location;
9483
9484 }
9485
9486 friend_type = TREE_TYPE (tmpl);
9487 }
9488 else
9489 {
9490 /* The friend template has not already been declared. In this
9491 case, the instantiation of the template class will cause the
9492 injection of this template into the global scope. */
9493 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9494 if (tmpl == error_mark_node)
9495 return error_mark_node;
9496
9497 /* The new TMPL is not an instantiation of anything, so we
9498 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9499 the new type because that is supposed to be the corresponding
9500 template decl, i.e., TMPL. */
9501 DECL_USE_TEMPLATE (tmpl) = 0;
9502 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9503 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9504 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9505 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9506
9507 /* Inject this template into the global scope. */
9508 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9509 }
9510
9511 if (context != global_namespace)
9512 {
9513 if (TREE_CODE (context) == NAMESPACE_DECL)
9514 pop_nested_namespace (context);
9515 else
9516 pop_nested_class ();
9517 }
9518
9519 return friend_type;
9520 }
9521
9522 /* Returns zero if TYPE cannot be completed later due to circularity.
9523 Otherwise returns one. */
9524
9525 static int
9526 can_complete_type_without_circularity (tree type)
9527 {
9528 if (type == NULL_TREE || type == error_mark_node)
9529 return 0;
9530 else if (COMPLETE_TYPE_P (type))
9531 return 1;
9532 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
9533 return can_complete_type_without_circularity (TREE_TYPE (type));
9534 else if (CLASS_TYPE_P (type)
9535 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9536 return 0;
9537 else
9538 return 1;
9539 }
9540
9541 static tree tsubst_omp_clauses (tree, bool, bool, tree, tsubst_flags_t, tree);
9542
9543 /* Apply any attributes which had to be deferred until instantiation
9544 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9545 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9546
9547 static void
9548 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9549 tree args, tsubst_flags_t complain, tree in_decl)
9550 {
9551 tree last_dep = NULL_TREE;
9552 tree t;
9553 tree *p;
9554
9555 for (t = attributes; t; t = TREE_CHAIN (t))
9556 if (ATTR_IS_DEPENDENT (t))
9557 {
9558 last_dep = t;
9559 attributes = copy_list (attributes);
9560 break;
9561 }
9562
9563 if (DECL_P (*decl_p))
9564 {
9565 if (TREE_TYPE (*decl_p) == error_mark_node)
9566 return;
9567 p = &DECL_ATTRIBUTES (*decl_p);
9568 }
9569 else
9570 p = &TYPE_ATTRIBUTES (*decl_p);
9571
9572 if (last_dep)
9573 {
9574 tree late_attrs = NULL_TREE;
9575 tree *q = &late_attrs;
9576
9577 for (*p = attributes; *p; )
9578 {
9579 t = *p;
9580 if (ATTR_IS_DEPENDENT (t))
9581 {
9582 *p = TREE_CHAIN (t);
9583 TREE_CHAIN (t) = NULL_TREE;
9584 if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9585 && is_attribute_p ("omp declare simd",
9586 get_attribute_name (t))
9587 && TREE_VALUE (t))
9588 {
9589 tree clauses = TREE_VALUE (TREE_VALUE (t));
9590 clauses = tsubst_omp_clauses (clauses, true, false, args,
9591 complain, in_decl);
9592 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9593 clauses = finish_omp_clauses (clauses, false, true);
9594 tree parms = DECL_ARGUMENTS (*decl_p);
9595 clauses
9596 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9597 if (clauses)
9598 TREE_VALUE (TREE_VALUE (t)) = clauses;
9599 else
9600 TREE_VALUE (t) = NULL_TREE;
9601 }
9602 /* If the first attribute argument is an identifier, don't
9603 pass it through tsubst. Attributes like mode, format,
9604 cleanup and several target specific attributes expect it
9605 unmodified. */
9606 else if (attribute_takes_identifier_p (get_attribute_name (t))
9607 && TREE_VALUE (t))
9608 {
9609 tree chain
9610 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9611 in_decl,
9612 /*integral_constant_expression_p=*/false);
9613 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9614 TREE_VALUE (t)
9615 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9616 chain);
9617 }
9618 else if (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))
9619 {
9620 /* An attribute pack expansion. */
9621 tree purp = TREE_PURPOSE (t);
9622 tree pack = (tsubst_pack_expansion
9623 (TREE_VALUE (t), args, complain, in_decl));
9624 int len = TREE_VEC_LENGTH (pack);
9625 for (int i = 0; i < len; ++i)
9626 {
9627 tree elt = TREE_VEC_ELT (pack, i);
9628 *q = build_tree_list (purp, elt);
9629 q = &TREE_CHAIN (*q);
9630 }
9631 continue;
9632 }
9633 else
9634 TREE_VALUE (t)
9635 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9636 /*integral_constant_expression_p=*/false);
9637 *q = t;
9638 q = &TREE_CHAIN (t);
9639 }
9640 else
9641 p = &TREE_CHAIN (t);
9642 }
9643
9644 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9645 }
9646 }
9647
9648 /* Perform (or defer) access check for typedefs that were referenced
9649 from within the template TMPL code.
9650 This is a subroutine of instantiate_decl and instantiate_class_template.
9651 TMPL is the template to consider and TARGS is the list of arguments of
9652 that template. */
9653
9654 static void
9655 perform_typedefs_access_check (tree tmpl, tree targs)
9656 {
9657 location_t saved_location;
9658 unsigned i;
9659 qualified_typedef_usage_t *iter;
9660
9661 if (!tmpl
9662 || (!CLASS_TYPE_P (tmpl)
9663 && TREE_CODE (tmpl) != FUNCTION_DECL))
9664 return;
9665
9666 saved_location = input_location;
9667 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9668 {
9669 tree type_decl = iter->typedef_decl;
9670 tree type_scope = iter->context;
9671
9672 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9673 continue;
9674
9675 if (uses_template_parms (type_decl))
9676 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9677 if (uses_template_parms (type_scope))
9678 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9679
9680 /* Make access check error messages point to the location
9681 of the use of the typedef. */
9682 input_location = iter->locus;
9683 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9684 type_decl, type_decl,
9685 tf_warning_or_error);
9686 }
9687 input_location = saved_location;
9688 }
9689
9690 static tree
9691 instantiate_class_template_1 (tree type)
9692 {
9693 tree templ, args, pattern, t, member;
9694 tree typedecl;
9695 tree pbinfo;
9696 tree base_list;
9697 unsigned int saved_maximum_field_alignment;
9698 tree fn_context;
9699
9700 if (type == error_mark_node)
9701 return error_mark_node;
9702
9703 if (COMPLETE_OR_OPEN_TYPE_P (type)
9704 || uses_template_parms (type))
9705 return type;
9706
9707 /* Figure out which template is being instantiated. */
9708 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9709 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9710
9711 /* Determine what specialization of the original template to
9712 instantiate. */
9713 t = most_specialized_partial_spec (type, tf_warning_or_error);
9714 if (t == error_mark_node)
9715 {
9716 TYPE_BEING_DEFINED (type) = 1;
9717 return error_mark_node;
9718 }
9719 else if (t)
9720 {
9721 /* This TYPE is actually an instantiation of a partial
9722 specialization. We replace the innermost set of ARGS with
9723 the arguments appropriate for substitution. For example,
9724 given:
9725
9726 template <class T> struct S {};
9727 template <class T> struct S<T*> {};
9728
9729 and supposing that we are instantiating S<int*>, ARGS will
9730 presently be {int*} -- but we need {int}. */
9731 pattern = TREE_TYPE (t);
9732 args = TREE_PURPOSE (t);
9733 }
9734 else
9735 {
9736 pattern = TREE_TYPE (templ);
9737 args = CLASSTYPE_TI_ARGS (type);
9738 }
9739
9740 /* If the template we're instantiating is incomplete, then clearly
9741 there's nothing we can do. */
9742 if (!COMPLETE_TYPE_P (pattern))
9743 return type;
9744
9745 /* If we've recursively instantiated too many templates, stop. */
9746 if (! push_tinst_level (type))
9747 return type;
9748
9749 /* Now we're really doing the instantiation. Mark the type as in
9750 the process of being defined. */
9751 TYPE_BEING_DEFINED (type) = 1;
9752
9753 /* We may be in the middle of deferred access check. Disable
9754 it now. */
9755 push_deferring_access_checks (dk_no_deferred);
9756
9757 int saved_unevaluated_operand = cp_unevaluated_operand;
9758 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9759
9760 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9761 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9762 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9763 fn_context = error_mark_node;
9764 if (!fn_context)
9765 push_to_top_level ();
9766 else
9767 {
9768 cp_unevaluated_operand = 0;
9769 c_inhibit_evaluation_warnings = 0;
9770 }
9771 /* Use #pragma pack from the template context. */
9772 saved_maximum_field_alignment = maximum_field_alignment;
9773 maximum_field_alignment = TYPE_PRECISION (pattern);
9774
9775 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9776
9777 /* Set the input location to the most specialized template definition.
9778 This is needed if tsubsting causes an error. */
9779 typedecl = TYPE_MAIN_DECL (pattern);
9780 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9781 DECL_SOURCE_LOCATION (typedecl);
9782
9783 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9784 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9785 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9786 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9787 if (ANON_AGGR_TYPE_P (pattern))
9788 SET_ANON_AGGR_TYPE_P (type);
9789 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9790 {
9791 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9792 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9793 /* Adjust visibility for template arguments. */
9794 determine_visibility (TYPE_MAIN_DECL (type));
9795 }
9796 if (CLASS_TYPE_P (type))
9797 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9798
9799 pbinfo = TYPE_BINFO (pattern);
9800
9801 /* We should never instantiate a nested class before its enclosing
9802 class; we need to look up the nested class by name before we can
9803 instantiate it, and that lookup should instantiate the enclosing
9804 class. */
9805 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9806 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9807
9808 base_list = NULL_TREE;
9809 if (BINFO_N_BASE_BINFOS (pbinfo))
9810 {
9811 tree pbase_binfo;
9812 tree pushed_scope;
9813 int i;
9814
9815 /* We must enter the scope containing the type, as that is where
9816 the accessibility of types named in dependent bases are
9817 looked up from. */
9818 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9819
9820 /* Substitute into each of the bases to determine the actual
9821 basetypes. */
9822 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9823 {
9824 tree base;
9825 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9826 tree expanded_bases = NULL_TREE;
9827 int idx, len = 1;
9828
9829 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9830 {
9831 expanded_bases =
9832 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9833 args, tf_error, NULL_TREE);
9834 if (expanded_bases == error_mark_node)
9835 continue;
9836
9837 len = TREE_VEC_LENGTH (expanded_bases);
9838 }
9839
9840 for (idx = 0; idx < len; idx++)
9841 {
9842 if (expanded_bases)
9843 /* Extract the already-expanded base class. */
9844 base = TREE_VEC_ELT (expanded_bases, idx);
9845 else
9846 /* Substitute to figure out the base class. */
9847 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9848 NULL_TREE);
9849
9850 if (base == error_mark_node)
9851 continue;
9852
9853 base_list = tree_cons (access, base, base_list);
9854 if (BINFO_VIRTUAL_P (pbase_binfo))
9855 TREE_TYPE (base_list) = integer_type_node;
9856 }
9857 }
9858
9859 /* The list is now in reverse order; correct that. */
9860 base_list = nreverse (base_list);
9861
9862 if (pushed_scope)
9863 pop_scope (pushed_scope);
9864 }
9865 /* Now call xref_basetypes to set up all the base-class
9866 information. */
9867 xref_basetypes (type, base_list);
9868
9869 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9870 (int) ATTR_FLAG_TYPE_IN_PLACE,
9871 args, tf_error, NULL_TREE);
9872 fixup_attribute_variants (type);
9873
9874 /* Now that our base classes are set up, enter the scope of the
9875 class, so that name lookups into base classes, etc. will work
9876 correctly. This is precisely analogous to what we do in
9877 begin_class_definition when defining an ordinary non-template
9878 class, except we also need to push the enclosing classes. */
9879 push_nested_class (type);
9880
9881 /* Now members are processed in the order of declaration. */
9882 for (member = CLASSTYPE_DECL_LIST (pattern);
9883 member; member = TREE_CHAIN (member))
9884 {
9885 tree t = TREE_VALUE (member);
9886
9887 if (TREE_PURPOSE (member))
9888 {
9889 if (TYPE_P (t))
9890 {
9891 /* Build new CLASSTYPE_NESTED_UTDS. */
9892
9893 tree newtag;
9894 bool class_template_p;
9895
9896 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9897 && TYPE_LANG_SPECIFIC (t)
9898 && CLASSTYPE_IS_TEMPLATE (t));
9899 /* If the member is a class template, then -- even after
9900 substitution -- there may be dependent types in the
9901 template argument list for the class. We increment
9902 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9903 that function will assume that no types are dependent
9904 when outside of a template. */
9905 if (class_template_p)
9906 ++processing_template_decl;
9907 newtag = tsubst (t, args, tf_error, NULL_TREE);
9908 if (class_template_p)
9909 --processing_template_decl;
9910 if (newtag == error_mark_node)
9911 continue;
9912
9913 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9914 {
9915 tree name = TYPE_IDENTIFIER (t);
9916
9917 if (class_template_p)
9918 /* Unfortunately, lookup_template_class sets
9919 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9920 instantiation (i.e., for the type of a member
9921 template class nested within a template class.)
9922 This behavior is required for
9923 maybe_process_partial_specialization to work
9924 correctly, but is not accurate in this case;
9925 the TAG is not an instantiation of anything.
9926 (The corresponding TEMPLATE_DECL is an
9927 instantiation, but the TYPE is not.) */
9928 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9929
9930 /* Now, we call pushtag to put this NEWTAG into the scope of
9931 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9932 pushtag calling push_template_decl. We don't have to do
9933 this for enums because it will already have been done in
9934 tsubst_enum. */
9935 if (name)
9936 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9937 pushtag (name, newtag, /*tag_scope=*/ts_current);
9938 }
9939 }
9940 else if (DECL_DECLARES_FUNCTION_P (t))
9941 {
9942 /* Build new TYPE_METHODS. */
9943 tree r;
9944
9945 if (TREE_CODE (t) == TEMPLATE_DECL)
9946 ++processing_template_decl;
9947 r = tsubst (t, args, tf_error, NULL_TREE);
9948 if (TREE_CODE (t) == TEMPLATE_DECL)
9949 --processing_template_decl;
9950 set_current_access_from_decl (r);
9951 finish_member_declaration (r);
9952 /* Instantiate members marked with attribute used. */
9953 if (r != error_mark_node && DECL_PRESERVE_P (r))
9954 mark_used (r);
9955 if (TREE_CODE (r) == FUNCTION_DECL
9956 && DECL_OMP_DECLARE_REDUCTION_P (r))
9957 cp_check_omp_declare_reduction (r);
9958 }
9959 else if (DECL_CLASS_TEMPLATE_P (t)
9960 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9961 /* A closure type for a lambda in a default argument for a
9962 member template. Ignore it; it will be instantiated with
9963 the default argument. */;
9964 else
9965 {
9966 /* Build new TYPE_FIELDS. */
9967 if (TREE_CODE (t) == STATIC_ASSERT)
9968 {
9969 tree condition;
9970
9971 ++c_inhibit_evaluation_warnings;
9972 condition =
9973 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9974 tf_warning_or_error, NULL_TREE,
9975 /*integral_constant_expression_p=*/true);
9976 --c_inhibit_evaluation_warnings;
9977
9978 finish_static_assert (condition,
9979 STATIC_ASSERT_MESSAGE (t),
9980 STATIC_ASSERT_SOURCE_LOCATION (t),
9981 /*member_p=*/true);
9982 }
9983 else if (TREE_CODE (t) != CONST_DECL)
9984 {
9985 tree r;
9986 tree vec = NULL_TREE;
9987 int len = 1;
9988
9989 /* The file and line for this declaration, to
9990 assist in error message reporting. Since we
9991 called push_tinst_level above, we don't need to
9992 restore these. */
9993 input_location = DECL_SOURCE_LOCATION (t);
9994
9995 if (TREE_CODE (t) == TEMPLATE_DECL)
9996 ++processing_template_decl;
9997 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9998 if (TREE_CODE (t) == TEMPLATE_DECL)
9999 --processing_template_decl;
10000
10001 if (TREE_CODE (r) == TREE_VEC)
10002 {
10003 /* A capture pack became multiple fields. */
10004 vec = r;
10005 len = TREE_VEC_LENGTH (vec);
10006 }
10007
10008 for (int i = 0; i < len; ++i)
10009 {
10010 if (vec)
10011 r = TREE_VEC_ELT (vec, i);
10012 if (VAR_P (r))
10013 {
10014 /* In [temp.inst]:
10015
10016 [t]he initialization (and any associated
10017 side-effects) of a static data member does
10018 not occur unless the static data member is
10019 itself used in a way that requires the
10020 definition of the static data member to
10021 exist.
10022
10023 Therefore, we do not substitute into the
10024 initialized for the static data member here. */
10025 finish_static_data_member_decl
10026 (r,
10027 /*init=*/NULL_TREE,
10028 /*init_const_expr_p=*/false,
10029 /*asmspec_tree=*/NULL_TREE,
10030 /*flags=*/0);
10031 /* Instantiate members marked with attribute used. */
10032 if (r != error_mark_node && DECL_PRESERVE_P (r))
10033 mark_used (r);
10034 }
10035 else if (TREE_CODE (r) == FIELD_DECL)
10036 {
10037 /* Determine whether R has a valid type and can be
10038 completed later. If R is invalid, then its type
10039 is replaced by error_mark_node. */
10040 tree rtype = TREE_TYPE (r);
10041 if (can_complete_type_without_circularity (rtype))
10042 complete_type (rtype);
10043
10044 if (TREE_CODE (r) == FIELD_DECL
10045 && TREE_CODE (rtype) == ARRAY_TYPE
10046 && COMPLETE_TYPE_P (TREE_TYPE (rtype))
10047 && !COMPLETE_TYPE_P (rtype))
10048 {
10049 /* Flexible array mmembers of elements
10050 of complete type have an incomplete type
10051 and that's okay. */
10052 }
10053 else if (!COMPLETE_TYPE_P (rtype))
10054 {
10055 cxx_incomplete_type_error (r, rtype);
10056 TREE_TYPE (r) = error_mark_node;
10057 }
10058 }
10059
10060 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10061 such a thing will already have been added to the field
10062 list by tsubst_enum in finish_member_declaration in the
10063 CLASSTYPE_NESTED_UTDS case above. */
10064 if (!(TREE_CODE (r) == TYPE_DECL
10065 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10066 && DECL_ARTIFICIAL (r)))
10067 {
10068 set_current_access_from_decl (r);
10069 finish_member_declaration (r);
10070 }
10071 }
10072 }
10073 }
10074 }
10075 else
10076 {
10077 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10078 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10079 {
10080 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10081
10082 tree friend_type = t;
10083 bool adjust_processing_template_decl = false;
10084
10085 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10086 {
10087 /* template <class T> friend class C; */
10088 friend_type = tsubst_friend_class (friend_type, args);
10089 adjust_processing_template_decl = true;
10090 }
10091 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10092 {
10093 /* template <class T> friend class C::D; */
10094 friend_type = tsubst (friend_type, args,
10095 tf_warning_or_error, NULL_TREE);
10096 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10097 friend_type = TREE_TYPE (friend_type);
10098 adjust_processing_template_decl = true;
10099 }
10100 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10101 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10102 {
10103 /* This could be either
10104
10105 friend class T::C;
10106
10107 when dependent_type_p is false or
10108
10109 template <class U> friend class T::C;
10110
10111 otherwise. */
10112 friend_type = tsubst (friend_type, args,
10113 tf_warning_or_error, NULL_TREE);
10114 /* Bump processing_template_decl for correct
10115 dependent_type_p calculation. */
10116 ++processing_template_decl;
10117 if (dependent_type_p (friend_type))
10118 adjust_processing_template_decl = true;
10119 --processing_template_decl;
10120 }
10121 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10122 && hidden_name_p (TYPE_NAME (friend_type)))
10123 {
10124 /* friend class C;
10125
10126 where C hasn't been declared yet. Let's lookup name
10127 from namespace scope directly, bypassing any name that
10128 come from dependent base class. */
10129 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10130
10131 /* The call to xref_tag_from_type does injection for friend
10132 classes. */
10133 push_nested_namespace (ns);
10134 friend_type =
10135 xref_tag_from_type (friend_type, NULL_TREE,
10136 /*tag_scope=*/ts_current);
10137 pop_nested_namespace (ns);
10138 }
10139 else if (uses_template_parms (friend_type))
10140 /* friend class C<T>; */
10141 friend_type = tsubst (friend_type, args,
10142 tf_warning_or_error, NULL_TREE);
10143 /* Otherwise it's
10144
10145 friend class C;
10146
10147 where C is already declared or
10148
10149 friend class C<int>;
10150
10151 We don't have to do anything in these cases. */
10152
10153 if (adjust_processing_template_decl)
10154 /* Trick make_friend_class into realizing that the friend
10155 we're adding is a template, not an ordinary class. It's
10156 important that we use make_friend_class since it will
10157 perform some error-checking and output cross-reference
10158 information. */
10159 ++processing_template_decl;
10160
10161 if (friend_type != error_mark_node)
10162 make_friend_class (type, friend_type, /*complain=*/false);
10163
10164 if (adjust_processing_template_decl)
10165 --processing_template_decl;
10166 }
10167 else
10168 {
10169 /* Build new DECL_FRIENDLIST. */
10170 tree r;
10171
10172 /* The file and line for this declaration, to
10173 assist in error message reporting. Since we
10174 called push_tinst_level above, we don't need to
10175 restore these. */
10176 input_location = DECL_SOURCE_LOCATION (t);
10177
10178 if (TREE_CODE (t) == TEMPLATE_DECL)
10179 {
10180 ++processing_template_decl;
10181 push_deferring_access_checks (dk_no_check);
10182 }
10183
10184 r = tsubst_friend_function (t, args);
10185 add_friend (type, r, /*complain=*/false);
10186 if (TREE_CODE (t) == TEMPLATE_DECL)
10187 {
10188 pop_deferring_access_checks ();
10189 --processing_template_decl;
10190 }
10191 }
10192 }
10193 }
10194
10195 if (fn_context)
10196 {
10197 /* Restore these before substituting into the lambda capture
10198 initializers. */
10199 cp_unevaluated_operand = saved_unevaluated_operand;
10200 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10201 }
10202
10203 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10204 {
10205 tree decl = lambda_function (type);
10206 if (decl)
10207 {
10208 if (!DECL_TEMPLATE_INFO (decl)
10209 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10210 {
10211 /* Set function_depth to avoid garbage collection. */
10212 ++function_depth;
10213 instantiate_decl (decl, false, false);
10214 --function_depth;
10215 }
10216
10217 /* We need to instantiate the capture list from the template
10218 after we've instantiated the closure members, but before we
10219 consider adding the conversion op. Also keep any captures
10220 that may have been added during instantiation of the op(). */
10221 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10222 tree tmpl_cap
10223 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10224 args, tf_warning_or_error, NULL_TREE,
10225 false, false);
10226
10227 LAMBDA_EXPR_CAPTURE_LIST (expr)
10228 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10229
10230 maybe_add_lambda_conv_op (type);
10231 }
10232 else
10233 gcc_assert (errorcount);
10234 }
10235
10236 /* Set the file and line number information to whatever is given for
10237 the class itself. This puts error messages involving generated
10238 implicit functions at a predictable point, and the same point
10239 that would be used for non-template classes. */
10240 input_location = DECL_SOURCE_LOCATION (typedecl);
10241
10242 unreverse_member_declarations (type);
10243 finish_struct_1 (type);
10244 TYPE_BEING_DEFINED (type) = 0;
10245
10246 /* We don't instantiate default arguments for member functions. 14.7.1:
10247
10248 The implicit instantiation of a class template specialization causes
10249 the implicit instantiation of the declarations, but not of the
10250 definitions or default arguments, of the class member functions,
10251 member classes, static data members and member templates.... */
10252
10253 /* Some typedefs referenced from within the template code need to be access
10254 checked at template instantiation time, i.e now. These types were
10255 added to the template at parsing time. Let's get those and perform
10256 the access checks then. */
10257 perform_typedefs_access_check (pattern, args);
10258 perform_deferred_access_checks (tf_warning_or_error);
10259 pop_nested_class ();
10260 maximum_field_alignment = saved_maximum_field_alignment;
10261 if (!fn_context)
10262 pop_from_top_level ();
10263 pop_deferring_access_checks ();
10264 pop_tinst_level ();
10265
10266 /* The vtable for a template class can be emitted in any translation
10267 unit in which the class is instantiated. When there is no key
10268 method, however, finish_struct_1 will already have added TYPE to
10269 the keyed_classes list. */
10270 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10271 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10272
10273 return type;
10274 }
10275
10276 /* Wrapper for instantiate_class_template_1. */
10277
10278 tree
10279 instantiate_class_template (tree type)
10280 {
10281 tree ret;
10282 timevar_push (TV_TEMPLATE_INST);
10283 ret = instantiate_class_template_1 (type);
10284 timevar_pop (TV_TEMPLATE_INST);
10285 return ret;
10286 }
10287
10288 static tree
10289 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10290 {
10291 tree r;
10292
10293 if (!t)
10294 r = t;
10295 else if (TYPE_P (t))
10296 r = tsubst (t, args, complain, in_decl);
10297 else
10298 {
10299 if (!(complain & tf_warning))
10300 ++c_inhibit_evaluation_warnings;
10301 r = tsubst_expr (t, args, complain, in_decl,
10302 /*integral_constant_expression_p=*/true);
10303 if (!(complain & tf_warning))
10304 --c_inhibit_evaluation_warnings;
10305 }
10306 return r;
10307 }
10308
10309 /* Given a function parameter pack TMPL_PARM and some function parameters
10310 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10311 and set *SPEC_P to point at the next point in the list. */
10312
10313 tree
10314 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10315 {
10316 /* Collect all of the extra "packed" parameters into an
10317 argument pack. */
10318 tree parmvec;
10319 tree parmtypevec;
10320 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10321 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10322 tree spec_parm = *spec_p;
10323 int i, len;
10324
10325 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10326 if (tmpl_parm
10327 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10328 break;
10329
10330 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10331 parmvec = make_tree_vec (len);
10332 parmtypevec = make_tree_vec (len);
10333 spec_parm = *spec_p;
10334 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10335 {
10336 TREE_VEC_ELT (parmvec, i) = spec_parm;
10337 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10338 }
10339
10340 /* Build the argument packs. */
10341 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10342 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10343 TREE_TYPE (argpack) = argtypepack;
10344 *spec_p = spec_parm;
10345
10346 return argpack;
10347 }
10348
10349 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10350 NONTYPE_ARGUMENT_PACK. */
10351
10352 static tree
10353 make_fnparm_pack (tree spec_parm)
10354 {
10355 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10356 }
10357
10358 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10359 pack expansion with no extra args, 2 if it has extra args, or 0
10360 if it is not a pack expansion. */
10361
10362 static int
10363 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10364 {
10365 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10366 if (i >= TREE_VEC_LENGTH (vec))
10367 return 0;
10368 tree elt = TREE_VEC_ELT (vec, i);
10369 if (DECL_P (elt))
10370 /* A decl pack is itself an expansion. */
10371 elt = TREE_TYPE (elt);
10372 if (!PACK_EXPANSION_P (elt))
10373 return 0;
10374 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10375 return 2;
10376 return 1;
10377 }
10378
10379
10380 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10381
10382 static tree
10383 make_argument_pack_select (tree arg_pack, unsigned index)
10384 {
10385 tree aps = make_node (ARGUMENT_PACK_SELECT);
10386
10387 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10388 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10389
10390 return aps;
10391 }
10392
10393 /* This is a subroutine of tsubst_pack_expansion.
10394
10395 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10396 mechanism to store the (non complete list of) arguments of the
10397 substitution and return a non substituted pack expansion, in order
10398 to wait for when we have enough arguments to really perform the
10399 substitution. */
10400
10401 static bool
10402 use_pack_expansion_extra_args_p (tree parm_packs,
10403 int arg_pack_len,
10404 bool has_empty_arg)
10405 {
10406 /* If one pack has an expansion and another pack has a normal
10407 argument or if one pack has an empty argument and an another
10408 one hasn't then tsubst_pack_expansion cannot perform the
10409 substitution and need to fall back on the
10410 PACK_EXPANSION_EXTRA mechanism. */
10411 if (parm_packs == NULL_TREE)
10412 return false;
10413 else if (has_empty_arg)
10414 return true;
10415
10416 bool has_expansion_arg = false;
10417 for (int i = 0 ; i < arg_pack_len; ++i)
10418 {
10419 bool has_non_expansion_arg = false;
10420 for (tree parm_pack = parm_packs;
10421 parm_pack;
10422 parm_pack = TREE_CHAIN (parm_pack))
10423 {
10424 tree arg = TREE_VALUE (parm_pack);
10425
10426 int exp = argument_pack_element_is_expansion_p (arg, i);
10427 if (exp == 2)
10428 /* We can't substitute a pack expansion with extra args into
10429 our pattern. */
10430 return true;
10431 else if (exp)
10432 has_expansion_arg = true;
10433 else
10434 has_non_expansion_arg = true;
10435 }
10436
10437 if (has_expansion_arg && has_non_expansion_arg)
10438 return true;
10439 }
10440 return false;
10441 }
10442
10443 /* [temp.variadic]/6 says that:
10444
10445 The instantiation of a pack expansion [...]
10446 produces a list E1,E2, ..., En, where N is the number of elements
10447 in the pack expansion parameters.
10448
10449 This subroutine of tsubst_pack_expansion produces one of these Ei.
10450
10451 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10452 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10453 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10454 INDEX is the index 'i' of the element Ei to produce. ARGS,
10455 COMPLAIN, and IN_DECL are the same parameters as for the
10456 tsubst_pack_expansion function.
10457
10458 The function returns the resulting Ei upon successful completion,
10459 or error_mark_node.
10460
10461 Note that this function possibly modifies the ARGS parameter, so
10462 it's the responsibility of the caller to restore it. */
10463
10464 static tree
10465 gen_elem_of_pack_expansion_instantiation (tree pattern,
10466 tree parm_packs,
10467 unsigned index,
10468 tree args /* This parm gets
10469 modified. */,
10470 tsubst_flags_t complain,
10471 tree in_decl)
10472 {
10473 tree t;
10474 bool ith_elem_is_expansion = false;
10475
10476 /* For each parameter pack, change the substitution of the parameter
10477 pack to the ith argument in its argument pack, then expand the
10478 pattern. */
10479 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10480 {
10481 tree parm = TREE_PURPOSE (pack);
10482 tree arg_pack = TREE_VALUE (pack);
10483 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10484
10485 ith_elem_is_expansion |=
10486 argument_pack_element_is_expansion_p (arg_pack, index);
10487
10488 /* Select the Ith argument from the pack. */
10489 if (TREE_CODE (parm) == PARM_DECL
10490 || TREE_CODE (parm) == FIELD_DECL)
10491 {
10492 if (index == 0)
10493 {
10494 aps = make_argument_pack_select (arg_pack, index);
10495 if (!mark_used (parm, complain) && !(complain & tf_error))
10496 return error_mark_node;
10497 register_local_specialization (aps, parm);
10498 }
10499 else
10500 aps = retrieve_local_specialization (parm);
10501 }
10502 else
10503 {
10504 int idx, level;
10505 template_parm_level_and_index (parm, &level, &idx);
10506
10507 if (index == 0)
10508 {
10509 aps = make_argument_pack_select (arg_pack, index);
10510 /* Update the corresponding argument. */
10511 TMPL_ARG (args, level, idx) = aps;
10512 }
10513 else
10514 /* Re-use the ARGUMENT_PACK_SELECT. */
10515 aps = TMPL_ARG (args, level, idx);
10516 }
10517 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10518 }
10519
10520 /* Substitute into the PATTERN with the (possibly altered)
10521 arguments. */
10522 if (pattern == in_decl)
10523 /* Expanding a fixed parameter pack from
10524 coerce_template_parameter_pack. */
10525 t = tsubst_decl (pattern, args, complain);
10526 else if (pattern == error_mark_node)
10527 t = error_mark_node;
10528 else if (constraint_p (pattern))
10529 {
10530 if (processing_template_decl)
10531 t = tsubst_constraint (pattern, args, complain, in_decl);
10532 else
10533 t = (constraints_satisfied_p (pattern, args)
10534 ? boolean_true_node : boolean_false_node);
10535 }
10536 else if (!TYPE_P (pattern))
10537 t = tsubst_expr (pattern, args, complain, in_decl,
10538 /*integral_constant_expression_p=*/false);
10539 else
10540 t = tsubst (pattern, args, complain, in_decl);
10541
10542 /* If the Ith argument pack element is a pack expansion, then
10543 the Ith element resulting from the substituting is going to
10544 be a pack expansion as well. */
10545 if (ith_elem_is_expansion)
10546 t = make_pack_expansion (t);
10547
10548 return t;
10549 }
10550
10551 /* When the unexpanded parameter pack in a fold expression expands to an empty
10552 sequence, the value of the expression is as follows; the program is
10553 ill-formed if the operator is not listed in this table.
10554
10555 * 1
10556 + 0
10557 & -1
10558 | 0
10559 && true
10560 || false
10561 , void() */
10562
10563 tree
10564 expand_empty_fold (tree t, tsubst_flags_t complain)
10565 {
10566 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
10567 if (!FOLD_EXPR_MODIFY_P (t))
10568 switch (code)
10569 {
10570 case MULT_EXPR:
10571 return integer_one_node;
10572 case PLUS_EXPR:
10573 return integer_zero_node;
10574 case BIT_AND_EXPR:
10575 return integer_minus_one_node;
10576 case BIT_IOR_EXPR:
10577 return integer_zero_node;
10578 case TRUTH_ANDIF_EXPR:
10579 return boolean_true_node;
10580 case TRUTH_ORIF_EXPR:
10581 return boolean_false_node;
10582 case COMPOUND_EXPR:
10583 return void_node;
10584 default:
10585 break;
10586 }
10587
10588 if (complain & tf_error)
10589 error_at (location_of (t),
10590 "fold of empty expansion over %O", code);
10591 return error_mark_node;
10592 }
10593
10594 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10595 form an expression that combines the two terms using the
10596 operator of T. */
10597
10598 static tree
10599 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
10600 {
10601 tree op = FOLD_EXPR_OP (t);
10602 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
10603
10604 // Handle compound assignment operators.
10605 if (FOLD_EXPR_MODIFY_P (t))
10606 return build_x_modify_expr (input_location, left, code, right, complain);
10607
10608 switch (code)
10609 {
10610 case COMPOUND_EXPR:
10611 return build_x_compound_expr (input_location, left, right, complain);
10612 case DOTSTAR_EXPR:
10613 return build_m_component_ref (left, right, complain);
10614 default:
10615 return build_x_binary_op (input_location, code,
10616 left, TREE_CODE (left),
10617 right, TREE_CODE (right),
10618 /*overload=*/NULL,
10619 complain);
10620 }
10621 }
10622
10623 /* Substitute ARGS into the pack of a fold expression T. */
10624
10625 static inline tree
10626 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10627 {
10628 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
10629 }
10630
10631 /* Substitute ARGS into the pack of a fold expression T. */
10632
10633 static inline tree
10634 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10635 {
10636 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
10637 }
10638
10639 /* Expand a PACK of arguments into a grouped as left fold.
10640 Given a pack containing elements A0, A1, ..., An and an
10641 operator @, this builds the expression:
10642
10643 ((A0 @ A1) @ A2) ... @ An
10644
10645 Note that PACK must not be empty.
10646
10647 The operator is defined by the original fold expression T. */
10648
10649 static tree
10650 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
10651 {
10652 tree left = TREE_VEC_ELT (pack, 0);
10653 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
10654 {
10655 tree right = TREE_VEC_ELT (pack, i);
10656 left = fold_expression (t, left, right, complain);
10657 }
10658 return left;
10659 }
10660
10661 /* Substitute into a unary left fold expression. */
10662
10663 static tree
10664 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
10665 tree in_decl)
10666 {
10667 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10668 if (pack == error_mark_node)
10669 return error_mark_node;
10670 if (TREE_VEC_LENGTH (pack) == 0)
10671 return expand_empty_fold (t, complain);
10672 else
10673 return expand_left_fold (t, pack, complain);
10674 }
10675
10676 /* Substitute into a binary left fold expression.
10677
10678 Do ths by building a single (non-empty) vector of argumnts and
10679 building the expression from those elements. */
10680
10681 static tree
10682 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
10683 tree in_decl)
10684 {
10685 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10686 if (pack == error_mark_node)
10687 return error_mark_node;
10688 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10689 if (init == error_mark_node)
10690 return error_mark_node;
10691
10692 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
10693 TREE_VEC_ELT (vec, 0) = init;
10694 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
10695 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
10696
10697 return expand_left_fold (t, vec, complain);
10698 }
10699
10700 /* Expand a PACK of arguments into a grouped as right fold.
10701 Given a pack containing elementns A0, A1, ..., and an
10702 operator @, this builds the expression:
10703
10704 A0@ ... (An-2 @ (An-1 @ An))
10705
10706 Note that PACK must not be empty.
10707
10708 The operator is defined by the original fold expression T. */
10709
10710 tree
10711 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
10712 {
10713 // Build the expression.
10714 int n = TREE_VEC_LENGTH (pack);
10715 tree right = TREE_VEC_ELT (pack, n - 1);
10716 for (--n; n != 0; --n)
10717 {
10718 tree left = TREE_VEC_ELT (pack, n - 1);
10719 right = fold_expression (t, left, right, complain);
10720 }
10721 return right;
10722 }
10723
10724 /* Substitute into a unary right fold expression. */
10725
10726 static tree
10727 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
10728 tree in_decl)
10729 {
10730 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10731 if (pack == error_mark_node)
10732 return error_mark_node;
10733 if (TREE_VEC_LENGTH (pack) == 0)
10734 return expand_empty_fold (t, complain);
10735 else
10736 return expand_right_fold (t, pack, complain);
10737 }
10738
10739 /* Substitute into a binary right fold expression.
10740
10741 Do ths by building a single (non-empty) vector of arguments and
10742 building the expression from those elements. */
10743
10744 static tree
10745 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
10746 tree in_decl)
10747 {
10748 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10749 if (pack == error_mark_node)
10750 return error_mark_node;
10751 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10752 if (init == error_mark_node)
10753 return error_mark_node;
10754
10755 int n = TREE_VEC_LENGTH (pack);
10756 tree vec = make_tree_vec (n + 1);
10757 for (int i = 0; i < n; ++i)
10758 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
10759 TREE_VEC_ELT (vec, n) = init;
10760
10761 return expand_right_fold (t, vec, complain);
10762 }
10763
10764
10765 /* Substitute ARGS into T, which is an pack expansion
10766 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10767 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10768 (if only a partial substitution could be performed) or
10769 ERROR_MARK_NODE if there was an error. */
10770 tree
10771 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10772 tree in_decl)
10773 {
10774 tree pattern;
10775 tree pack, packs = NULL_TREE;
10776 bool unsubstituted_packs = false;
10777 int i, len = -1;
10778 tree result;
10779 hash_map<tree, tree> *saved_local_specializations = NULL;
10780 bool need_local_specializations = false;
10781 int levels;
10782
10783 gcc_assert (PACK_EXPANSION_P (t));
10784 pattern = PACK_EXPANSION_PATTERN (t);
10785
10786 /* Add in any args remembered from an earlier partial instantiation. */
10787 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10788
10789 levels = TMPL_ARGS_DEPTH (args);
10790
10791 /* Determine the argument packs that will instantiate the parameter
10792 packs used in the expansion expression. While we're at it,
10793 compute the number of arguments to be expanded and make sure it
10794 is consistent. */
10795 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10796 pack = TREE_CHAIN (pack))
10797 {
10798 tree parm_pack = TREE_VALUE (pack);
10799 tree arg_pack = NULL_TREE;
10800 tree orig_arg = NULL_TREE;
10801 int level = 0;
10802
10803 if (TREE_CODE (parm_pack) == BASES)
10804 {
10805 if (BASES_DIRECT (parm_pack))
10806 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10807 args, complain, in_decl, false));
10808 else
10809 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10810 args, complain, in_decl, false));
10811 }
10812 if (TREE_CODE (parm_pack) == PARM_DECL)
10813 {
10814 /* We know we have correct local_specializations if this
10815 expansion is at function scope, or if we're dealing with a
10816 local parameter in a requires expression; for the latter,
10817 tsubst_requires_expr set it up appropriately. */
10818 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
10819 arg_pack = retrieve_local_specialization (parm_pack);
10820 else
10821 /* We can't rely on local_specializations for a parameter
10822 name used later in a function declaration (such as in a
10823 late-specified return type). Even if it exists, it might
10824 have the wrong value for a recursive call. */
10825 need_local_specializations = true;
10826
10827 if (!arg_pack)
10828 {
10829 /* This parameter pack was used in an unevaluated context. Just
10830 make a dummy decl, since it's only used for its type. */
10831 arg_pack = tsubst_decl (parm_pack, args, complain);
10832 if (arg_pack && DECL_PACK_P (arg_pack))
10833 /* Partial instantiation of the parm_pack, we can't build
10834 up an argument pack yet. */
10835 arg_pack = NULL_TREE;
10836 else
10837 arg_pack = make_fnparm_pack (arg_pack);
10838 }
10839 }
10840 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10841 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10842 else
10843 {
10844 int idx;
10845 template_parm_level_and_index (parm_pack, &level, &idx);
10846
10847 if (level <= levels)
10848 arg_pack = TMPL_ARG (args, level, idx);
10849 }
10850
10851 orig_arg = arg_pack;
10852 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
10853 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
10854
10855 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
10856 /* This can only happen if we forget to expand an argument
10857 pack somewhere else. Just return an error, silently. */
10858 {
10859 result = make_tree_vec (1);
10860 TREE_VEC_ELT (result, 0) = error_mark_node;
10861 return result;
10862 }
10863
10864 if (arg_pack)
10865 {
10866 int my_len =
10867 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10868
10869 /* Don't bother trying to do a partial substitution with
10870 incomplete packs; we'll try again after deduction. */
10871 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10872 return t;
10873
10874 if (len < 0)
10875 len = my_len;
10876 else if (len != my_len)
10877 {
10878 if (!(complain & tf_error))
10879 /* Fail quietly. */;
10880 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10881 error ("mismatched argument pack lengths while expanding "
10882 "%<%T%>",
10883 pattern);
10884 else
10885 error ("mismatched argument pack lengths while expanding "
10886 "%<%E%>",
10887 pattern);
10888 return error_mark_node;
10889 }
10890
10891 /* Keep track of the parameter packs and their corresponding
10892 argument packs. */
10893 packs = tree_cons (parm_pack, arg_pack, packs);
10894 TREE_TYPE (packs) = orig_arg;
10895 }
10896 else
10897 {
10898 /* We can't substitute for this parameter pack. We use a flag as
10899 well as the missing_level counter because function parameter
10900 packs don't have a level. */
10901 unsubstituted_packs = true;
10902 }
10903 }
10904
10905 /* If the expansion is just T..., return the matching argument pack, unless
10906 we need to call convert_from_reference on all the elements. This is an
10907 important optimization; see c++/68422. */
10908 if (!unsubstituted_packs
10909 && TREE_PURPOSE (packs) == pattern)
10910 {
10911 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10912 /* Types need no adjustment, nor does sizeof..., and if we still have
10913 some pack expansion args we won't do anything yet. */
10914 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10915 || PACK_EXPANSION_SIZEOF_P (t)
10916 || pack_expansion_args_count (args))
10917 return args;
10918 /* Also optimize expression pack expansions if we can tell that the
10919 elements won't have reference type. */
10920 tree type = TREE_TYPE (pattern);
10921 if (type && TREE_CODE (type) != REFERENCE_TYPE
10922 && !PACK_EXPANSION_P (type)
10923 && !WILDCARD_TYPE_P (type))
10924 return args;
10925 /* Otherwise use the normal path so we get convert_from_reference. */
10926 }
10927
10928 /* We cannot expand this expansion expression, because we don't have
10929 all of the argument packs we need. */
10930 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10931 {
10932 /* We got some full packs, but we can't substitute them in until we
10933 have values for all the packs. So remember these until then. */
10934
10935 t = make_pack_expansion (pattern);
10936 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10937 return t;
10938 }
10939 else if (unsubstituted_packs)
10940 {
10941 /* There were no real arguments, we're just replacing a parameter
10942 pack with another version of itself. Substitute into the
10943 pattern and return a PACK_EXPANSION_*. The caller will need to
10944 deal with that. */
10945 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10946 t = tsubst_expr (pattern, args, complain, in_decl,
10947 /*integral_constant_expression_p=*/false);
10948 else
10949 t = tsubst (pattern, args, complain, in_decl);
10950 t = make_pack_expansion (t);
10951 return t;
10952 }
10953
10954 gcc_assert (len >= 0);
10955
10956 if (need_local_specializations)
10957 {
10958 /* We're in a late-specified return type, so create our own local
10959 specializations map; the current map is either NULL or (in the
10960 case of recursive unification) might have bindings that we don't
10961 want to use or alter. */
10962 saved_local_specializations = local_specializations;
10963 local_specializations = new hash_map<tree, tree>;
10964 }
10965
10966 /* For each argument in each argument pack, substitute into the
10967 pattern. */
10968 result = make_tree_vec (len);
10969 for (i = 0; i < len; ++i)
10970 {
10971 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10972 i,
10973 args, complain,
10974 in_decl);
10975 TREE_VEC_ELT (result, i) = t;
10976 if (t == error_mark_node)
10977 {
10978 result = error_mark_node;
10979 break;
10980 }
10981 }
10982
10983 /* Update ARGS to restore the substitution from parameter packs to
10984 their argument packs. */
10985 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10986 {
10987 tree parm = TREE_PURPOSE (pack);
10988
10989 if (TREE_CODE (parm) == PARM_DECL
10990 || TREE_CODE (parm) == FIELD_DECL)
10991 register_local_specialization (TREE_TYPE (pack), parm);
10992 else
10993 {
10994 int idx, level;
10995
10996 if (TREE_VALUE (pack) == NULL_TREE)
10997 continue;
10998
10999 template_parm_level_and_index (parm, &level, &idx);
11000
11001 /* Update the corresponding argument. */
11002 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11003 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11004 TREE_TYPE (pack);
11005 else
11006 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11007 }
11008 }
11009
11010 if (need_local_specializations)
11011 {
11012 delete local_specializations;
11013 local_specializations = saved_local_specializations;
11014 }
11015
11016 return result;
11017 }
11018
11019 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11020 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11021 parameter packs; all parms generated from a function parameter pack will
11022 have the same DECL_PARM_INDEX. */
11023
11024 tree
11025 get_pattern_parm (tree parm, tree tmpl)
11026 {
11027 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11028 tree patparm;
11029
11030 if (DECL_ARTIFICIAL (parm))
11031 {
11032 for (patparm = DECL_ARGUMENTS (pattern);
11033 patparm; patparm = DECL_CHAIN (patparm))
11034 if (DECL_ARTIFICIAL (patparm)
11035 && DECL_NAME (parm) == DECL_NAME (patparm))
11036 break;
11037 }
11038 else
11039 {
11040 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11041 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11042 gcc_assert (DECL_PARM_INDEX (patparm)
11043 == DECL_PARM_INDEX (parm));
11044 }
11045
11046 return patparm;
11047 }
11048
11049 /* Substitute ARGS into the vector or list of template arguments T. */
11050
11051 static tree
11052 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11053 {
11054 tree orig_t = t;
11055 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11056 tree *elts;
11057
11058 if (t == error_mark_node)
11059 return error_mark_node;
11060
11061 len = TREE_VEC_LENGTH (t);
11062 elts = XALLOCAVEC (tree, len);
11063
11064 for (i = 0; i < len; i++)
11065 {
11066 tree orig_arg = TREE_VEC_ELT (t, i);
11067 tree new_arg;
11068
11069 if (TREE_CODE (orig_arg) == TREE_VEC)
11070 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11071 else if (PACK_EXPANSION_P (orig_arg))
11072 {
11073 /* Substitute into an expansion expression. */
11074 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11075
11076 if (TREE_CODE (new_arg) == TREE_VEC)
11077 /* Add to the expanded length adjustment the number of
11078 expanded arguments. We subtract one from this
11079 measurement, because the argument pack expression
11080 itself is already counted as 1 in
11081 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11082 the argument pack is empty. */
11083 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11084 }
11085 else if (ARGUMENT_PACK_P (orig_arg))
11086 {
11087 /* Substitute into each of the arguments. */
11088 new_arg = TYPE_P (orig_arg)
11089 ? cxx_make_type (TREE_CODE (orig_arg))
11090 : make_node (TREE_CODE (orig_arg));
11091
11092 SET_ARGUMENT_PACK_ARGS (
11093 new_arg,
11094 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11095 args, complain, in_decl));
11096
11097 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11098 new_arg = error_mark_node;
11099
11100 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11101 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11102 complain, in_decl);
11103 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11104
11105 if (TREE_TYPE (new_arg) == error_mark_node)
11106 new_arg = error_mark_node;
11107 }
11108 }
11109 else
11110 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11111
11112 if (new_arg == error_mark_node)
11113 return error_mark_node;
11114
11115 elts[i] = new_arg;
11116 if (new_arg != orig_arg)
11117 need_new = 1;
11118 }
11119
11120 if (!need_new)
11121 return t;
11122
11123 /* Make space for the expanded arguments coming from template
11124 argument packs. */
11125 t = make_tree_vec (len + expanded_len_adjust);
11126 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11127 arguments for a member template.
11128 In that case each TREE_VEC in ORIG_T represents a level of template
11129 arguments, and ORIG_T won't carry any non defaulted argument count.
11130 It will rather be the nested TREE_VECs that will carry one.
11131 In other words, ORIG_T carries a non defaulted argument count only
11132 if it doesn't contain any nested TREE_VEC. */
11133 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11134 {
11135 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11136 count += expanded_len_adjust;
11137 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11138 }
11139 for (i = 0, out = 0; i < len; i++)
11140 {
11141 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11142 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11143 && TREE_CODE (elts[i]) == TREE_VEC)
11144 {
11145 int idx;
11146
11147 /* Now expand the template argument pack "in place". */
11148 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11149 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11150 }
11151 else
11152 {
11153 TREE_VEC_ELT (t, out) = elts[i];
11154 out++;
11155 }
11156 }
11157
11158 return t;
11159 }
11160
11161 /* Return the result of substituting ARGS into the template parameters
11162 given by PARMS. If there are m levels of ARGS and m + n levels of
11163 PARMS, then the result will contain n levels of PARMS. For
11164 example, if PARMS is `template <class T> template <class U>
11165 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11166 result will be `template <int*, double, class V>'. */
11167
11168 static tree
11169 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11170 {
11171 tree r = NULL_TREE;
11172 tree* new_parms;
11173
11174 /* When substituting into a template, we must set
11175 PROCESSING_TEMPLATE_DECL as the template parameters may be
11176 dependent if they are based on one-another, and the dependency
11177 predicates are short-circuit outside of templates. */
11178 ++processing_template_decl;
11179
11180 for (new_parms = &r;
11181 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11182 new_parms = &(TREE_CHAIN (*new_parms)),
11183 parms = TREE_CHAIN (parms))
11184 {
11185 tree new_vec =
11186 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
11187 int i;
11188
11189 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11190 {
11191 tree tuple;
11192
11193 if (parms == error_mark_node)
11194 continue;
11195
11196 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
11197
11198 if (tuple == error_mark_node)
11199 continue;
11200
11201 TREE_VEC_ELT (new_vec, i) =
11202 tsubst_template_parm (tuple, args, complain);
11203 }
11204
11205 *new_parms =
11206 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11207 - TMPL_ARGS_DEPTH (args)),
11208 new_vec, NULL_TREE);
11209 }
11210
11211 --processing_template_decl;
11212
11213 return r;
11214 }
11215
11216 /* Return the result of substituting ARGS into one template parameter
11217 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11218 parameter and which TREE_PURPOSE is the default argument of the
11219 template parameter. */
11220
11221 static tree
11222 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11223 {
11224 tree default_value, parm_decl;
11225
11226 if (args == NULL_TREE
11227 || t == NULL_TREE
11228 || t == error_mark_node)
11229 return t;
11230
11231 gcc_assert (TREE_CODE (t) == TREE_LIST);
11232
11233 default_value = TREE_PURPOSE (t);
11234 parm_decl = TREE_VALUE (t);
11235
11236 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11237 if (TREE_CODE (parm_decl) == PARM_DECL
11238 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11239 parm_decl = error_mark_node;
11240 default_value = tsubst_template_arg (default_value, args,
11241 complain, NULL_TREE);
11242
11243 return build_tree_list (default_value, parm_decl);
11244 }
11245
11246 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11247 type T. If T is not an aggregate or enumeration type, it is
11248 handled as if by tsubst. IN_DECL is as for tsubst. If
11249 ENTERING_SCOPE is nonzero, T is the context for a template which
11250 we are presently tsubst'ing. Return the substituted value. */
11251
11252 static tree
11253 tsubst_aggr_type (tree t,
11254 tree args,
11255 tsubst_flags_t complain,
11256 tree in_decl,
11257 int entering_scope)
11258 {
11259 if (t == NULL_TREE)
11260 return NULL_TREE;
11261
11262 switch (TREE_CODE (t))
11263 {
11264 case RECORD_TYPE:
11265 if (TYPE_PTRMEMFUNC_P (t))
11266 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11267
11268 /* Else fall through. */
11269 case ENUMERAL_TYPE:
11270 case UNION_TYPE:
11271 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11272 {
11273 tree argvec;
11274 tree context;
11275 tree r;
11276 int saved_unevaluated_operand;
11277 int saved_inhibit_evaluation_warnings;
11278
11279 /* In "sizeof(X<I>)" we need to evaluate "I". */
11280 saved_unevaluated_operand = cp_unevaluated_operand;
11281 cp_unevaluated_operand = 0;
11282 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11283 c_inhibit_evaluation_warnings = 0;
11284
11285 /* First, determine the context for the type we are looking
11286 up. */
11287 context = TYPE_CONTEXT (t);
11288 if (context && TYPE_P (context))
11289 {
11290 context = tsubst_aggr_type (context, args, complain,
11291 in_decl, /*entering_scope=*/1);
11292 /* If context is a nested class inside a class template,
11293 it may still need to be instantiated (c++/33959). */
11294 context = complete_type (context);
11295 }
11296
11297 /* Then, figure out what arguments are appropriate for the
11298 type we are trying to find. For example, given:
11299
11300 template <class T> struct S;
11301 template <class T, class U> void f(T, U) { S<U> su; }
11302
11303 and supposing that we are instantiating f<int, double>,
11304 then our ARGS will be {int, double}, but, when looking up
11305 S we only want {double}. */
11306 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11307 complain, in_decl);
11308 if (argvec == error_mark_node)
11309 r = error_mark_node;
11310 else
11311 {
11312 r = lookup_template_class (t, argvec, in_decl, context,
11313 entering_scope, complain);
11314 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11315 }
11316
11317 cp_unevaluated_operand = saved_unevaluated_operand;
11318 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11319
11320 return r;
11321 }
11322 else
11323 /* This is not a template type, so there's nothing to do. */
11324 return t;
11325
11326 default:
11327 return tsubst (t, args, complain, in_decl);
11328 }
11329 }
11330
11331 /* Substitute into the default argument ARG (a default argument for
11332 FN), which has the indicated TYPE. */
11333
11334 tree
11335 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11336 {
11337 tree saved_class_ptr = NULL_TREE;
11338 tree saved_class_ref = NULL_TREE;
11339 int errs = errorcount + sorrycount;
11340
11341 /* This can happen in invalid code. */
11342 if (TREE_CODE (arg) == DEFAULT_ARG)
11343 return arg;
11344
11345 /* This default argument came from a template. Instantiate the
11346 default argument here, not in tsubst. In the case of
11347 something like:
11348
11349 template <class T>
11350 struct S {
11351 static T t();
11352 void f(T = t());
11353 };
11354
11355 we must be careful to do name lookup in the scope of S<T>,
11356 rather than in the current class. */
11357 push_access_scope (fn);
11358 /* The "this" pointer is not valid in a default argument. */
11359 if (cfun)
11360 {
11361 saved_class_ptr = current_class_ptr;
11362 cp_function_chain->x_current_class_ptr = NULL_TREE;
11363 saved_class_ref = current_class_ref;
11364 cp_function_chain->x_current_class_ref = NULL_TREE;
11365 }
11366
11367 push_deferring_access_checks(dk_no_deferred);
11368 /* The default argument expression may cause implicitly defined
11369 member functions to be synthesized, which will result in garbage
11370 collection. We must treat this situation as if we were within
11371 the body of function so as to avoid collecting live data on the
11372 stack. */
11373 ++function_depth;
11374 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11375 complain, NULL_TREE,
11376 /*integral_constant_expression_p=*/false);
11377 --function_depth;
11378 pop_deferring_access_checks();
11379
11380 /* Restore the "this" pointer. */
11381 if (cfun)
11382 {
11383 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11384 cp_function_chain->x_current_class_ref = saved_class_ref;
11385 }
11386
11387 if (errorcount+sorrycount > errs
11388 && (complain & tf_warning_or_error))
11389 inform (input_location,
11390 " when instantiating default argument for call to %D", fn);
11391
11392 /* Make sure the default argument is reasonable. */
11393 arg = check_default_argument (type, arg, complain);
11394
11395 pop_access_scope (fn);
11396
11397 return arg;
11398 }
11399
11400 /* Substitute into all the default arguments for FN. */
11401
11402 static void
11403 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11404 {
11405 tree arg;
11406 tree tmpl_args;
11407
11408 tmpl_args = DECL_TI_ARGS (fn);
11409
11410 /* If this function is not yet instantiated, we certainly don't need
11411 its default arguments. */
11412 if (uses_template_parms (tmpl_args))
11413 return;
11414 /* Don't do this again for clones. */
11415 if (DECL_CLONED_FUNCTION_P (fn))
11416 return;
11417
11418 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11419 arg;
11420 arg = TREE_CHAIN (arg))
11421 if (TREE_PURPOSE (arg))
11422 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11423 TREE_VALUE (arg),
11424 TREE_PURPOSE (arg),
11425 complain);
11426 }
11427
11428 /* Substitute the ARGS into the T, which is a _DECL. Return the
11429 result of the substitution. Issue error and warning messages under
11430 control of COMPLAIN. */
11431
11432 static tree
11433 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11434 {
11435 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11436 location_t saved_loc;
11437 tree r = NULL_TREE;
11438 tree in_decl = t;
11439 hashval_t hash = 0;
11440
11441 /* Set the filename and linenumber to improve error-reporting. */
11442 saved_loc = input_location;
11443 input_location = DECL_SOURCE_LOCATION (t);
11444
11445 switch (TREE_CODE (t))
11446 {
11447 case TEMPLATE_DECL:
11448 {
11449 /* We can get here when processing a member function template,
11450 member class template, or template template parameter. */
11451 tree decl = DECL_TEMPLATE_RESULT (t);
11452 tree spec;
11453 tree tmpl_args;
11454 tree full_args;
11455
11456 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11457 {
11458 /* Template template parameter is treated here. */
11459 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11460 if (new_type == error_mark_node)
11461 r = error_mark_node;
11462 /* If we get a real template back, return it. This can happen in
11463 the context of most_specialized_partial_spec. */
11464 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11465 r = new_type;
11466 else
11467 /* The new TEMPLATE_DECL was built in
11468 reduce_template_parm_level. */
11469 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
11470 break;
11471 }
11472
11473 /* We might already have an instance of this template.
11474 The ARGS are for the surrounding class type, so the
11475 full args contain the tsubst'd args for the context,
11476 plus the innermost args from the template decl. */
11477 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11478 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11479 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11480 /* Because this is a template, the arguments will still be
11481 dependent, even after substitution. If
11482 PROCESSING_TEMPLATE_DECL is not set, the dependency
11483 predicates will short-circuit. */
11484 ++processing_template_decl;
11485 full_args = tsubst_template_args (tmpl_args, args,
11486 complain, in_decl);
11487 --processing_template_decl;
11488 if (full_args == error_mark_node)
11489 RETURN (error_mark_node);
11490
11491 /* If this is a default template template argument,
11492 tsubst might not have changed anything. */
11493 if (full_args == tmpl_args)
11494 RETURN (t);
11495
11496 hash = hash_tmpl_and_args (t, full_args);
11497 spec = retrieve_specialization (t, full_args, hash);
11498 if (spec != NULL_TREE)
11499 {
11500 r = spec;
11501 break;
11502 }
11503
11504 /* Make a new template decl. It will be similar to the
11505 original, but will record the current template arguments.
11506 We also create a new function declaration, which is just
11507 like the old one, but points to this new template, rather
11508 than the old one. */
11509 r = copy_decl (t);
11510 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11511 DECL_CHAIN (r) = NULL_TREE;
11512
11513 // Build new template info linking to the original template decl.
11514 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11515
11516 if (TREE_CODE (decl) == TYPE_DECL
11517 && !TYPE_DECL_ALIAS_P (decl))
11518 {
11519 tree new_type;
11520 ++processing_template_decl;
11521 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11522 --processing_template_decl;
11523 if (new_type == error_mark_node)
11524 RETURN (error_mark_node);
11525
11526 TREE_TYPE (r) = new_type;
11527 /* For a partial specialization, we need to keep pointing to
11528 the primary template. */
11529 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11530 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11531 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11532 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11533 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11534 }
11535 else
11536 {
11537 tree new_decl;
11538 ++processing_template_decl;
11539 new_decl = tsubst (decl, args, complain, in_decl);
11540 --processing_template_decl;
11541 if (new_decl == error_mark_node)
11542 RETURN (error_mark_node);
11543
11544 DECL_TEMPLATE_RESULT (r) = new_decl;
11545 DECL_TI_TEMPLATE (new_decl) = r;
11546 TREE_TYPE (r) = TREE_TYPE (new_decl);
11547 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11548 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11549 }
11550
11551 SET_DECL_IMPLICIT_INSTANTIATION (r);
11552 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11553 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11554
11555 /* The template parameters for this new template are all the
11556 template parameters for the old template, except the
11557 outermost level of parameters. */
11558 DECL_TEMPLATE_PARMS (r)
11559 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11560 complain);
11561
11562 if (PRIMARY_TEMPLATE_P (t))
11563 DECL_PRIMARY_TEMPLATE (r) = r;
11564
11565 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
11566 /* Record this non-type partial instantiation. */
11567 register_specialization (r, t,
11568 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11569 false, hash);
11570 }
11571 break;
11572
11573 case FUNCTION_DECL:
11574 {
11575 tree ctx;
11576 tree argvec = NULL_TREE;
11577 tree *friends;
11578 tree gen_tmpl;
11579 tree type;
11580 int member;
11581 int args_depth;
11582 int parms_depth;
11583
11584 /* Nobody should be tsubst'ing into non-template functions. */
11585 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11586
11587 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11588 {
11589 tree spec;
11590 bool dependent_p;
11591
11592 /* If T is not dependent, just return it. We have to
11593 increment PROCESSING_TEMPLATE_DECL because
11594 value_dependent_expression_p assumes that nothing is
11595 dependent when PROCESSING_TEMPLATE_DECL is zero. */
11596 ++processing_template_decl;
11597 dependent_p = value_dependent_expression_p (t);
11598 --processing_template_decl;
11599 if (!dependent_p)
11600 RETURN (t);
11601
11602 /* Calculate the most general template of which R is a
11603 specialization, and the complete set of arguments used to
11604 specialize R. */
11605 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11606 argvec = tsubst_template_args (DECL_TI_ARGS
11607 (DECL_TEMPLATE_RESULT
11608 (DECL_TI_TEMPLATE (t))),
11609 args, complain, in_decl);
11610 if (argvec == error_mark_node)
11611 RETURN (error_mark_node);
11612
11613 /* Check to see if we already have this specialization. */
11614 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11615 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11616
11617 if (spec)
11618 {
11619 r = spec;
11620 break;
11621 }
11622
11623 /* We can see more levels of arguments than parameters if
11624 there was a specialization of a member template, like
11625 this:
11626
11627 template <class T> struct S { template <class U> void f(); }
11628 template <> template <class U> void S<int>::f(U);
11629
11630 Here, we'll be substituting into the specialization,
11631 because that's where we can find the code we actually
11632 want to generate, but we'll have enough arguments for
11633 the most general template.
11634
11635 We also deal with the peculiar case:
11636
11637 template <class T> struct S {
11638 template <class U> friend void f();
11639 };
11640 template <class U> void f() {}
11641 template S<int>;
11642 template void f<double>();
11643
11644 Here, the ARGS for the instantiation of will be {int,
11645 double}. But, we only need as many ARGS as there are
11646 levels of template parameters in CODE_PATTERN. We are
11647 careful not to get fooled into reducing the ARGS in
11648 situations like:
11649
11650 template <class T> struct S { template <class U> void f(U); }
11651 template <class T> template <> void S<T>::f(int) {}
11652
11653 which we can spot because the pattern will be a
11654 specialization in this case. */
11655 args_depth = TMPL_ARGS_DEPTH (args);
11656 parms_depth =
11657 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11658 if (args_depth > parms_depth
11659 && !DECL_TEMPLATE_SPECIALIZATION (t))
11660 args = get_innermost_template_args (args, parms_depth);
11661 }
11662 else
11663 {
11664 /* This special case arises when we have something like this:
11665
11666 template <class T> struct S {
11667 friend void f<int>(int, double);
11668 };
11669
11670 Here, the DECL_TI_TEMPLATE for the friend declaration
11671 will be an IDENTIFIER_NODE. We are being called from
11672 tsubst_friend_function, and we want only to create a
11673 new decl (R) with appropriate types so that we can call
11674 determine_specialization. */
11675 gen_tmpl = NULL_TREE;
11676 }
11677
11678 if (DECL_CLASS_SCOPE_P (t))
11679 {
11680 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11681 member = 2;
11682 else
11683 member = 1;
11684 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11685 complain, t, /*entering_scope=*/1);
11686 }
11687 else
11688 {
11689 member = 0;
11690 ctx = DECL_CONTEXT (t);
11691 }
11692 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11693 if (type == error_mark_node)
11694 RETURN (error_mark_node);
11695
11696 /* If we hit excessive deduction depth, the type is bogus even if
11697 it isn't error_mark_node, so don't build a decl. */
11698 if (excessive_deduction_depth)
11699 RETURN (error_mark_node);
11700
11701 /* We do NOT check for matching decls pushed separately at this
11702 point, as they may not represent instantiations of this
11703 template, and in any case are considered separate under the
11704 discrete model. */
11705 r = copy_decl (t);
11706 DECL_USE_TEMPLATE (r) = 0;
11707 TREE_TYPE (r) = type;
11708 /* Clear out the mangled name and RTL for the instantiation. */
11709 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11710 SET_DECL_RTL (r, NULL);
11711 /* Leave DECL_INITIAL set on deleted instantiations. */
11712 if (!DECL_DELETED_FN (r))
11713 DECL_INITIAL (r) = NULL_TREE;
11714 DECL_CONTEXT (r) = ctx;
11715
11716 /* OpenMP UDRs have the only argument a reference to the declared
11717 type. We want to diagnose if the declared type is a reference,
11718 which is invalid, but as references to references are usually
11719 quietly merged, diagnose it here. */
11720 if (DECL_OMP_DECLARE_REDUCTION_P (t))
11721 {
11722 tree argtype
11723 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11724 argtype = tsubst (argtype, args, complain, in_decl);
11725 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11726 error_at (DECL_SOURCE_LOCATION (t),
11727 "reference type %qT in "
11728 "%<#pragma omp declare reduction%>", argtype);
11729 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11730 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11731 argtype);
11732 }
11733
11734 if (member && DECL_CONV_FN_P (r))
11735 /* Type-conversion operator. Reconstruct the name, in
11736 case it's the name of one of the template's parameters. */
11737 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11738
11739 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11740 complain, t);
11741 DECL_RESULT (r) = NULL_TREE;
11742
11743 TREE_STATIC (r) = 0;
11744 TREE_PUBLIC (r) = TREE_PUBLIC (t);
11745 DECL_EXTERNAL (r) = 1;
11746 /* If this is an instantiation of a function with internal
11747 linkage, we already know what object file linkage will be
11748 assigned to the instantiation. */
11749 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
11750 DECL_DEFER_OUTPUT (r) = 0;
11751 DECL_CHAIN (r) = NULL_TREE;
11752 DECL_PENDING_INLINE_INFO (r) = 0;
11753 DECL_PENDING_INLINE_P (r) = 0;
11754 DECL_SAVED_TREE (r) = NULL_TREE;
11755 DECL_STRUCT_FUNCTION (r) = NULL;
11756 TREE_USED (r) = 0;
11757 /* We'll re-clone as appropriate in instantiate_template. */
11758 DECL_CLONED_FUNCTION (r) = NULL_TREE;
11759
11760 /* If we aren't complaining now, return on error before we register
11761 the specialization so that we'll complain eventually. */
11762 if ((complain & tf_error) == 0
11763 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11764 && !grok_op_properties (r, /*complain=*/false))
11765 RETURN (error_mark_node);
11766
11767 /* When instantiating a constrained member, substitute
11768 into the constraints to create a new constraint. */
11769 if (tree ci = get_constraints (t))
11770 if (member)
11771 {
11772 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
11773 set_constraints (r, ci);
11774 }
11775
11776 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11777 this in the special friend case mentioned above where
11778 GEN_TMPL is NULL. */
11779 if (gen_tmpl)
11780 {
11781 DECL_TEMPLATE_INFO (r)
11782 = build_template_info (gen_tmpl, argvec);
11783 SET_DECL_IMPLICIT_INSTANTIATION (r);
11784
11785 tree new_r
11786 = register_specialization (r, gen_tmpl, argvec, false, hash);
11787 if (new_r != r)
11788 /* We instantiated this while substituting into
11789 the type earlier (template/friend54.C). */
11790 RETURN (new_r);
11791
11792 /* We're not supposed to instantiate default arguments
11793 until they are called, for a template. But, for a
11794 declaration like:
11795
11796 template <class T> void f ()
11797 { extern void g(int i = T()); }
11798
11799 we should do the substitution when the template is
11800 instantiated. We handle the member function case in
11801 instantiate_class_template since the default arguments
11802 might refer to other members of the class. */
11803 if (!member
11804 && !PRIMARY_TEMPLATE_P (gen_tmpl)
11805 && !uses_template_parms (argvec))
11806 tsubst_default_arguments (r, complain);
11807 }
11808 else
11809 DECL_TEMPLATE_INFO (r) = NULL_TREE;
11810
11811 /* Copy the list of befriending classes. */
11812 for (friends = &DECL_BEFRIENDING_CLASSES (r);
11813 *friends;
11814 friends = &TREE_CHAIN (*friends))
11815 {
11816 *friends = copy_node (*friends);
11817 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
11818 args, complain,
11819 in_decl);
11820 }
11821
11822 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
11823 {
11824 maybe_retrofit_in_chrg (r);
11825 if (DECL_CONSTRUCTOR_P (r))
11826 grok_ctor_properties (ctx, r);
11827 if (DECL_INHERITED_CTOR_BASE (r))
11828 deduce_inheriting_ctor (r);
11829 /* If this is an instantiation of a member template, clone it.
11830 If it isn't, that'll be handled by
11831 clone_constructors_and_destructors. */
11832 if (PRIMARY_TEMPLATE_P (gen_tmpl))
11833 clone_function_decl (r, /*update_method_vec_p=*/0);
11834 }
11835 else if ((complain & tf_error) != 0
11836 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11837 && !grok_op_properties (r, /*complain=*/true))
11838 RETURN (error_mark_node);
11839
11840 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
11841 SET_DECL_FRIEND_CONTEXT (r,
11842 tsubst (DECL_FRIEND_CONTEXT (t),
11843 args, complain, in_decl));
11844
11845 /* Possibly limit visibility based on template args. */
11846 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11847 if (DECL_VISIBILITY_SPECIFIED (t))
11848 {
11849 DECL_VISIBILITY_SPECIFIED (r) = 0;
11850 DECL_ATTRIBUTES (r)
11851 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11852 }
11853 determine_visibility (r);
11854 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
11855 && !processing_template_decl)
11856 defaulted_late_check (r);
11857
11858 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11859 args, complain, in_decl);
11860 }
11861 break;
11862
11863 case PARM_DECL:
11864 {
11865 tree type = NULL_TREE;
11866 int i, len = 1;
11867 tree expanded_types = NULL_TREE;
11868 tree prev_r = NULL_TREE;
11869 tree first_r = NULL_TREE;
11870
11871 if (DECL_PACK_P (t))
11872 {
11873 /* If there is a local specialization that isn't a
11874 parameter pack, it means that we're doing a "simple"
11875 substitution from inside tsubst_pack_expansion. Just
11876 return the local specialization (which will be a single
11877 parm). */
11878 tree spec = retrieve_local_specialization (t);
11879 if (spec
11880 && TREE_CODE (spec) == PARM_DECL
11881 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11882 RETURN (spec);
11883
11884 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11885 the parameters in this function parameter pack. */
11886 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11887 complain, in_decl);
11888 if (TREE_CODE (expanded_types) == TREE_VEC)
11889 {
11890 len = TREE_VEC_LENGTH (expanded_types);
11891
11892 /* Zero-length parameter packs are boring. Just substitute
11893 into the chain. */
11894 if (len == 0)
11895 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11896 TREE_CHAIN (t)));
11897 }
11898 else
11899 {
11900 /* All we did was update the type. Make a note of that. */
11901 type = expanded_types;
11902 expanded_types = NULL_TREE;
11903 }
11904 }
11905
11906 /* Loop through all of the parameters we'll build. When T is
11907 a function parameter pack, LEN is the number of expanded
11908 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11909 r = NULL_TREE;
11910 for (i = 0; i < len; ++i)
11911 {
11912 prev_r = r;
11913 r = copy_node (t);
11914 if (DECL_TEMPLATE_PARM_P (t))
11915 SET_DECL_TEMPLATE_PARM_P (r);
11916
11917 if (expanded_types)
11918 /* We're on the Ith parameter of the function parameter
11919 pack. */
11920 {
11921 /* Get the Ith type. */
11922 type = TREE_VEC_ELT (expanded_types, i);
11923
11924 /* Rename the parameter to include the index. */
11925 DECL_NAME (r)
11926 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11927 }
11928 else if (!type)
11929 /* We're dealing with a normal parameter. */
11930 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11931
11932 type = type_decays_to (type);
11933 TREE_TYPE (r) = type;
11934 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11935
11936 if (DECL_INITIAL (r))
11937 {
11938 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11939 DECL_INITIAL (r) = TREE_TYPE (r);
11940 else
11941 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11942 complain, in_decl);
11943 }
11944
11945 DECL_CONTEXT (r) = NULL_TREE;
11946
11947 if (!DECL_TEMPLATE_PARM_P (r))
11948 DECL_ARG_TYPE (r) = type_passed_as (type);
11949
11950 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11951 args, complain, in_decl);
11952
11953 /* Keep track of the first new parameter we
11954 generate. That's what will be returned to the
11955 caller. */
11956 if (!first_r)
11957 first_r = r;
11958
11959 /* Build a proper chain of parameters when substituting
11960 into a function parameter pack. */
11961 if (prev_r)
11962 DECL_CHAIN (prev_r) = r;
11963 }
11964
11965 /* If cp_unevaluated_operand is set, we're just looking for a
11966 single dummy parameter, so don't keep going. */
11967 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11968 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11969 complain, DECL_CHAIN (t));
11970
11971 /* FIRST_R contains the start of the chain we've built. */
11972 r = first_r;
11973 }
11974 break;
11975
11976 case FIELD_DECL:
11977 {
11978 tree type = NULL_TREE;
11979 tree vec = NULL_TREE;
11980 tree expanded_types = NULL_TREE;
11981 int len = 1;
11982
11983 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11984 {
11985 /* This field is a lambda capture pack. Return a TREE_VEC of
11986 the expanded fields to instantiate_class_template_1 and
11987 store them in the specializations hash table as a
11988 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11989 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11990 complain, in_decl);
11991 if (TREE_CODE (expanded_types) == TREE_VEC)
11992 {
11993 len = TREE_VEC_LENGTH (expanded_types);
11994 vec = make_tree_vec (len);
11995 }
11996 else
11997 {
11998 /* All we did was update the type. Make a note of that. */
11999 type = expanded_types;
12000 expanded_types = NULL_TREE;
12001 }
12002 }
12003
12004 for (int i = 0; i < len; ++i)
12005 {
12006 r = copy_decl (t);
12007 if (expanded_types)
12008 {
12009 type = TREE_VEC_ELT (expanded_types, i);
12010 DECL_NAME (r)
12011 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12012 }
12013 else if (!type)
12014 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12015
12016 if (type == error_mark_node)
12017 RETURN (error_mark_node);
12018 TREE_TYPE (r) = type;
12019 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12020
12021 if (DECL_C_BIT_FIELD (r))
12022 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12023 non-bit-fields DECL_INITIAL is a non-static data member
12024 initializer, which gets deferred instantiation. */
12025 DECL_INITIAL (r)
12026 = tsubst_expr (DECL_INITIAL (t), args,
12027 complain, in_decl,
12028 /*integral_constant_expression_p=*/true);
12029 else if (DECL_INITIAL (t))
12030 {
12031 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12032 NSDMI in perform_member_init. Still set DECL_INITIAL
12033 so that we know there is one. */
12034 DECL_INITIAL (r) = void_node;
12035 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12036 retrofit_lang_decl (r);
12037 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12038 }
12039 /* We don't have to set DECL_CONTEXT here; it is set by
12040 finish_member_declaration. */
12041 DECL_CHAIN (r) = NULL_TREE;
12042
12043 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12044 args, complain, in_decl);
12045
12046 if (vec)
12047 TREE_VEC_ELT (vec, i) = r;
12048 }
12049
12050 if (vec)
12051 {
12052 r = vec;
12053 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12054 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12055 SET_ARGUMENT_PACK_ARGS (pack, vec);
12056 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12057 TREE_TYPE (pack) = tpack;
12058 register_specialization (pack, t, args, false, 0);
12059 }
12060 }
12061 break;
12062
12063 case USING_DECL:
12064 /* We reach here only for member using decls. We also need to check
12065 uses_template_parms because DECL_DEPENDENT_P is not set for a
12066 using-declaration that designates a member of the current
12067 instantiation (c++/53549). */
12068 if (DECL_DEPENDENT_P (t)
12069 || uses_template_parms (USING_DECL_SCOPE (t)))
12070 {
12071 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12072 complain, in_decl);
12073 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12074 r = do_class_using_decl (inst_scope, name);
12075 if (!r)
12076 r = error_mark_node;
12077 else
12078 {
12079 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12080 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12081 }
12082 }
12083 else
12084 {
12085 r = copy_node (t);
12086 DECL_CHAIN (r) = NULL_TREE;
12087 }
12088 break;
12089
12090 case TYPE_DECL:
12091 case VAR_DECL:
12092 {
12093 tree argvec = NULL_TREE;
12094 tree gen_tmpl = NULL_TREE;
12095 tree spec;
12096 tree tmpl = NULL_TREE;
12097 tree ctx;
12098 tree type = NULL_TREE;
12099 bool local_p;
12100
12101 if (TREE_TYPE (t) == error_mark_node)
12102 RETURN (error_mark_node);
12103
12104 if (TREE_CODE (t) == TYPE_DECL
12105 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12106 {
12107 /* If this is the canonical decl, we don't have to
12108 mess with instantiations, and often we can't (for
12109 typename, template type parms and such). Note that
12110 TYPE_NAME is not correct for the above test if
12111 we've copied the type for a typedef. */
12112 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12113 if (type == error_mark_node)
12114 RETURN (error_mark_node);
12115 r = TYPE_NAME (type);
12116 break;
12117 }
12118
12119 /* Check to see if we already have the specialization we
12120 need. */
12121 spec = NULL_TREE;
12122 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12123 {
12124 /* T is a static data member or namespace-scope entity.
12125 We have to substitute into namespace-scope variables
12126 (not just variable templates) because of cases like:
12127
12128 template <class T> void f() { extern T t; }
12129
12130 where the entity referenced is not known until
12131 instantiation time. */
12132 local_p = false;
12133 ctx = DECL_CONTEXT (t);
12134 if (DECL_CLASS_SCOPE_P (t))
12135 {
12136 ctx = tsubst_aggr_type (ctx, args,
12137 complain,
12138 in_decl, /*entering_scope=*/1);
12139 /* If CTX is unchanged, then T is in fact the
12140 specialization we want. That situation occurs when
12141 referencing a static data member within in its own
12142 class. We can use pointer equality, rather than
12143 same_type_p, because DECL_CONTEXT is always
12144 canonical... */
12145 if (ctx == DECL_CONTEXT (t)
12146 /* ... unless T is a member template; in which
12147 case our caller can be willing to create a
12148 specialization of that template represented
12149 by T. */
12150 && !(DECL_TI_TEMPLATE (t)
12151 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12152 spec = t;
12153 }
12154
12155 if (!spec)
12156 {
12157 tmpl = DECL_TI_TEMPLATE (t);
12158 gen_tmpl = most_general_template (tmpl);
12159 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12160 if (argvec != error_mark_node)
12161 argvec = (coerce_innermost_template_parms
12162 (DECL_TEMPLATE_PARMS (gen_tmpl),
12163 argvec, t, complain,
12164 /*all*/true, /*defarg*/true));
12165 if (argvec == error_mark_node)
12166 RETURN (error_mark_node);
12167 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12168 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12169 }
12170 }
12171 else
12172 {
12173 /* A local variable. */
12174 local_p = true;
12175 /* Subsequent calls to pushdecl will fill this in. */
12176 ctx = NULL_TREE;
12177 spec = retrieve_local_specialization (t);
12178 }
12179 /* If we already have the specialization we need, there is
12180 nothing more to do. */
12181 if (spec)
12182 {
12183 r = spec;
12184 break;
12185 }
12186
12187 /* Create a new node for the specialization we need. */
12188 r = copy_decl (t);
12189 if (type == NULL_TREE)
12190 {
12191 if (is_typedef_decl (t))
12192 type = DECL_ORIGINAL_TYPE (t);
12193 else
12194 type = TREE_TYPE (t);
12195 if (VAR_P (t)
12196 && VAR_HAD_UNKNOWN_BOUND (t)
12197 && type != error_mark_node)
12198 type = strip_array_domain (type);
12199 type = tsubst (type, args, complain, in_decl);
12200 }
12201 if (VAR_P (r))
12202 {
12203 /* Even if the original location is out of scope, the
12204 newly substituted one is not. */
12205 DECL_DEAD_FOR_LOCAL (r) = 0;
12206 DECL_INITIALIZED_P (r) = 0;
12207 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12208 if (type == error_mark_node)
12209 RETURN (error_mark_node);
12210 if (TREE_CODE (type) == FUNCTION_TYPE)
12211 {
12212 /* It may seem that this case cannot occur, since:
12213
12214 typedef void f();
12215 void g() { f x; }
12216
12217 declares a function, not a variable. However:
12218
12219 typedef void f();
12220 template <typename T> void g() { T t; }
12221 template void g<f>();
12222
12223 is an attempt to declare a variable with function
12224 type. */
12225 error ("variable %qD has function type",
12226 /* R is not yet sufficiently initialized, so we
12227 just use its name. */
12228 DECL_NAME (r));
12229 RETURN (error_mark_node);
12230 }
12231 type = complete_type (type);
12232 /* Wait until cp_finish_decl to set this again, to handle
12233 circular dependency (template/instantiate6.C). */
12234 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12235 type = check_var_type (DECL_NAME (r), type);
12236
12237 if (DECL_HAS_VALUE_EXPR_P (t))
12238 {
12239 tree ve = DECL_VALUE_EXPR (t);
12240 ve = tsubst_expr (ve, args, complain, in_decl,
12241 /*constant_expression_p=*/false);
12242 if (REFERENCE_REF_P (ve))
12243 {
12244 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12245 ve = TREE_OPERAND (ve, 0);
12246 }
12247 SET_DECL_VALUE_EXPR (r, ve);
12248 }
12249 if (CP_DECL_THREAD_LOCAL_P (r)
12250 && !processing_template_decl)
12251 set_decl_tls_model (r, decl_default_tls_model (r));
12252 }
12253 else if (DECL_SELF_REFERENCE_P (t))
12254 SET_DECL_SELF_REFERENCE_P (r);
12255 TREE_TYPE (r) = type;
12256 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12257 DECL_CONTEXT (r) = ctx;
12258 /* Clear out the mangled name and RTL for the instantiation. */
12259 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12260 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12261 SET_DECL_RTL (r, NULL);
12262 /* The initializer must not be expanded until it is required;
12263 see [temp.inst]. */
12264 DECL_INITIAL (r) = NULL_TREE;
12265 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12266 SET_DECL_RTL (r, NULL);
12267 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12268 if (VAR_P (r))
12269 {
12270 /* Possibly limit visibility based on template args. */
12271 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12272 if (DECL_VISIBILITY_SPECIFIED (t))
12273 {
12274 DECL_VISIBILITY_SPECIFIED (r) = 0;
12275 DECL_ATTRIBUTES (r)
12276 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12277 }
12278 determine_visibility (r);
12279 }
12280
12281 if (!local_p)
12282 {
12283 /* A static data member declaration is always marked
12284 external when it is declared in-class, even if an
12285 initializer is present. We mimic the non-template
12286 processing here. */
12287 DECL_EXTERNAL (r) = 1;
12288 if (DECL_NAMESPACE_SCOPE_P (t))
12289 DECL_NOT_REALLY_EXTERN (r) = 1;
12290
12291 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12292 SET_DECL_IMPLICIT_INSTANTIATION (r);
12293 register_specialization (r, gen_tmpl, argvec, false, hash);
12294 }
12295 else
12296 {
12297 if (DECL_LANG_SPECIFIC (r))
12298 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12299 if (!cp_unevaluated_operand)
12300 register_local_specialization (r, t);
12301 }
12302
12303 DECL_CHAIN (r) = NULL_TREE;
12304
12305 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12306 /*flags=*/0,
12307 args, complain, in_decl);
12308
12309 /* Preserve a typedef that names a type. */
12310 if (is_typedef_decl (r))
12311 {
12312 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12313 set_underlying_type (r);
12314 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12315 /* An alias template specialization can be dependent
12316 even if its underlying type is not. */
12317 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12318 }
12319
12320 layout_decl (r, 0);
12321 }
12322 break;
12323
12324 default:
12325 gcc_unreachable ();
12326 }
12327 #undef RETURN
12328
12329 out:
12330 /* Restore the file and line information. */
12331 input_location = saved_loc;
12332
12333 return r;
12334 }
12335
12336 /* Substitute into the ARG_TYPES of a function type.
12337 If END is a TREE_CHAIN, leave it and any following types
12338 un-substituted. */
12339
12340 static tree
12341 tsubst_arg_types (tree arg_types,
12342 tree args,
12343 tree end,
12344 tsubst_flags_t complain,
12345 tree in_decl)
12346 {
12347 tree remaining_arg_types;
12348 tree type = NULL_TREE;
12349 int i = 1;
12350 tree expanded_args = NULL_TREE;
12351 tree default_arg;
12352
12353 if (!arg_types || arg_types == void_list_node || arg_types == end)
12354 return arg_types;
12355
12356 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12357 args, end, complain, in_decl);
12358 if (remaining_arg_types == error_mark_node)
12359 return error_mark_node;
12360
12361 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12362 {
12363 /* For a pack expansion, perform substitution on the
12364 entire expression. Later on, we'll handle the arguments
12365 one-by-one. */
12366 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12367 args, complain, in_decl);
12368
12369 if (TREE_CODE (expanded_args) == TREE_VEC)
12370 /* So that we'll spin through the parameters, one by one. */
12371 i = TREE_VEC_LENGTH (expanded_args);
12372 else
12373 {
12374 /* We only partially substituted into the parameter
12375 pack. Our type is TYPE_PACK_EXPANSION. */
12376 type = expanded_args;
12377 expanded_args = NULL_TREE;
12378 }
12379 }
12380
12381 while (i > 0) {
12382 --i;
12383
12384 if (expanded_args)
12385 type = TREE_VEC_ELT (expanded_args, i);
12386 else if (!type)
12387 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12388
12389 if (type == error_mark_node)
12390 return error_mark_node;
12391 if (VOID_TYPE_P (type))
12392 {
12393 if (complain & tf_error)
12394 {
12395 error ("invalid parameter type %qT", type);
12396 if (in_decl)
12397 error ("in declaration %q+D", in_decl);
12398 }
12399 return error_mark_node;
12400 }
12401 /* DR 657. */
12402 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12403 return error_mark_node;
12404
12405 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12406 top-level qualifiers as required. */
12407 type = cv_unqualified (type_decays_to (type));
12408
12409 /* We do not substitute into default arguments here. The standard
12410 mandates that they be instantiated only when needed, which is
12411 done in build_over_call. */
12412 default_arg = TREE_PURPOSE (arg_types);
12413
12414 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12415 {
12416 /* We've instantiated a template before its default arguments
12417 have been parsed. This can happen for a nested template
12418 class, and is not an error unless we require the default
12419 argument in a call of this function. */
12420 remaining_arg_types =
12421 tree_cons (default_arg, type, remaining_arg_types);
12422 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12423 }
12424 else
12425 remaining_arg_types =
12426 hash_tree_cons (default_arg, type, remaining_arg_types);
12427 }
12428
12429 return remaining_arg_types;
12430 }
12431
12432 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12433 *not* handle the exception-specification for FNTYPE, because the
12434 initial substitution of explicitly provided template parameters
12435 during argument deduction forbids substitution into the
12436 exception-specification:
12437
12438 [temp.deduct]
12439
12440 All references in the function type of the function template to the
12441 corresponding template parameters are replaced by the specified tem-
12442 plate argument values. If a substitution in a template parameter or
12443 in the function type of the function template results in an invalid
12444 type, type deduction fails. [Note: The equivalent substitution in
12445 exception specifications is done only when the function is instanti-
12446 ated, at which point a program is ill-formed if the substitution
12447 results in an invalid type.] */
12448
12449 static tree
12450 tsubst_function_type (tree t,
12451 tree args,
12452 tsubst_flags_t complain,
12453 tree in_decl)
12454 {
12455 tree return_type;
12456 tree arg_types = NULL_TREE;
12457 tree fntype;
12458
12459 /* The TYPE_CONTEXT is not used for function/method types. */
12460 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12461
12462 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12463 failure. */
12464 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12465
12466 if (late_return_type_p)
12467 {
12468 /* Substitute the argument types. */
12469 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12470 complain, in_decl);
12471 if (arg_types == error_mark_node)
12472 return error_mark_node;
12473
12474 tree save_ccp = current_class_ptr;
12475 tree save_ccr = current_class_ref;
12476 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12477 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12478 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12479 if (do_inject)
12480 {
12481 /* DR 1207: 'this' is in scope in the trailing return type. */
12482 inject_this_parameter (this_type, cp_type_quals (this_type));
12483 }
12484
12485 /* Substitute the return type. */
12486 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12487
12488 if (do_inject)
12489 {
12490 current_class_ptr = save_ccp;
12491 current_class_ref = save_ccr;
12492 }
12493 }
12494 else
12495 /* Substitute the return type. */
12496 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12497
12498 if (return_type == error_mark_node)
12499 return error_mark_node;
12500 /* DR 486 clarifies that creation of a function type with an
12501 invalid return type is a deduction failure. */
12502 if (TREE_CODE (return_type) == ARRAY_TYPE
12503 || TREE_CODE (return_type) == FUNCTION_TYPE)
12504 {
12505 if (complain & tf_error)
12506 {
12507 if (TREE_CODE (return_type) == ARRAY_TYPE)
12508 error ("function returning an array");
12509 else
12510 error ("function returning a function");
12511 }
12512 return error_mark_node;
12513 }
12514 /* And DR 657. */
12515 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12516 return error_mark_node;
12517
12518 if (!late_return_type_p)
12519 {
12520 /* Substitute the argument types. */
12521 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12522 complain, in_decl);
12523 if (arg_types == error_mark_node)
12524 return error_mark_node;
12525 }
12526
12527 /* Construct a new type node and return it. */
12528 if (TREE_CODE (t) == FUNCTION_TYPE)
12529 {
12530 fntype = build_function_type (return_type, arg_types);
12531 fntype = apply_memfn_quals (fntype,
12532 type_memfn_quals (t),
12533 type_memfn_rqual (t));
12534 }
12535 else
12536 {
12537 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12538 /* Don't pick up extra function qualifiers from the basetype. */
12539 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12540 if (! MAYBE_CLASS_TYPE_P (r))
12541 {
12542 /* [temp.deduct]
12543
12544 Type deduction may fail for any of the following
12545 reasons:
12546
12547 -- Attempting to create "pointer to member of T" when T
12548 is not a class type. */
12549 if (complain & tf_error)
12550 error ("creating pointer to member function of non-class type %qT",
12551 r);
12552 return error_mark_node;
12553 }
12554
12555 fntype = build_method_type_directly (r, return_type,
12556 TREE_CHAIN (arg_types));
12557 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12558 }
12559 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12560
12561 if (late_return_type_p)
12562 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12563
12564 return fntype;
12565 }
12566
12567 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12568 ARGS into that specification, and return the substituted
12569 specification. If there is no specification, return NULL_TREE. */
12570
12571 static tree
12572 tsubst_exception_specification (tree fntype,
12573 tree args,
12574 tsubst_flags_t complain,
12575 tree in_decl,
12576 bool defer_ok)
12577 {
12578 tree specs;
12579 tree new_specs;
12580
12581 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12582 new_specs = NULL_TREE;
12583 if (specs && TREE_PURPOSE (specs))
12584 {
12585 /* A noexcept-specifier. */
12586 tree expr = TREE_PURPOSE (specs);
12587 if (TREE_CODE (expr) == INTEGER_CST)
12588 new_specs = expr;
12589 else if (defer_ok)
12590 {
12591 /* Defer instantiation of noexcept-specifiers to avoid
12592 excessive instantiations (c++/49107). */
12593 new_specs = make_node (DEFERRED_NOEXCEPT);
12594 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12595 {
12596 /* We already partially instantiated this member template,
12597 so combine the new args with the old. */
12598 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12599 = DEFERRED_NOEXCEPT_PATTERN (expr);
12600 DEFERRED_NOEXCEPT_ARGS (new_specs)
12601 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12602 }
12603 else
12604 {
12605 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12606 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12607 }
12608 }
12609 else
12610 new_specs = tsubst_copy_and_build
12611 (expr, args, complain, in_decl, /*function_p=*/false,
12612 /*integral_constant_expression_p=*/true);
12613 new_specs = build_noexcept_spec (new_specs, complain);
12614 }
12615 else if (specs)
12616 {
12617 if (! TREE_VALUE (specs))
12618 new_specs = specs;
12619 else
12620 while (specs)
12621 {
12622 tree spec;
12623 int i, len = 1;
12624 tree expanded_specs = NULL_TREE;
12625
12626 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12627 {
12628 /* Expand the pack expansion type. */
12629 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12630 args, complain,
12631 in_decl);
12632
12633 if (expanded_specs == error_mark_node)
12634 return error_mark_node;
12635 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12636 len = TREE_VEC_LENGTH (expanded_specs);
12637 else
12638 {
12639 /* We're substituting into a member template, so
12640 we got a TYPE_PACK_EXPANSION back. Add that
12641 expansion and move on. */
12642 gcc_assert (TREE_CODE (expanded_specs)
12643 == TYPE_PACK_EXPANSION);
12644 new_specs = add_exception_specifier (new_specs,
12645 expanded_specs,
12646 complain);
12647 specs = TREE_CHAIN (specs);
12648 continue;
12649 }
12650 }
12651
12652 for (i = 0; i < len; ++i)
12653 {
12654 if (expanded_specs)
12655 spec = TREE_VEC_ELT (expanded_specs, i);
12656 else
12657 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12658 if (spec == error_mark_node)
12659 return spec;
12660 new_specs = add_exception_specifier (new_specs, spec,
12661 complain);
12662 }
12663
12664 specs = TREE_CHAIN (specs);
12665 }
12666 }
12667 return new_specs;
12668 }
12669
12670 /* Take the tree structure T and replace template parameters used
12671 therein with the argument vector ARGS. IN_DECL is an associated
12672 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12673 Issue error and warning messages under control of COMPLAIN. Note
12674 that we must be relatively non-tolerant of extensions here, in
12675 order to preserve conformance; if we allow substitutions that
12676 should not be allowed, we may allow argument deductions that should
12677 not succeed, and therefore report ambiguous overload situations
12678 where there are none. In theory, we could allow the substitution,
12679 but indicate that it should have failed, and allow our caller to
12680 make sure that the right thing happens, but we don't try to do this
12681 yet.
12682
12683 This function is used for dealing with types, decls and the like;
12684 for expressions, use tsubst_expr or tsubst_copy. */
12685
12686 tree
12687 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12688 {
12689 enum tree_code code;
12690 tree type, r = NULL_TREE;
12691
12692 if (t == NULL_TREE || t == error_mark_node
12693 || t == integer_type_node
12694 || t == void_type_node
12695 || t == char_type_node
12696 || t == unknown_type_node
12697 || TREE_CODE (t) == NAMESPACE_DECL
12698 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12699 return t;
12700
12701 if (DECL_P (t))
12702 return tsubst_decl (t, args, complain);
12703
12704 if (args == NULL_TREE)
12705 return t;
12706
12707 code = TREE_CODE (t);
12708
12709 if (code == IDENTIFIER_NODE)
12710 type = IDENTIFIER_TYPE_VALUE (t);
12711 else
12712 type = TREE_TYPE (t);
12713
12714 gcc_assert (type != unknown_type_node);
12715
12716 /* Reuse typedefs. We need to do this to handle dependent attributes,
12717 such as attribute aligned. */
12718 if (TYPE_P (t)
12719 && typedef_variant_p (t))
12720 {
12721 tree decl = TYPE_NAME (t);
12722
12723 if (alias_template_specialization_p (t))
12724 {
12725 /* DECL represents an alias template and we want to
12726 instantiate it. */
12727 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12728 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12729 r = instantiate_alias_template (tmpl, gen_args, complain);
12730 }
12731 else if (DECL_CLASS_SCOPE_P (decl)
12732 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12733 && uses_template_parms (DECL_CONTEXT (decl)))
12734 {
12735 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12736 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12737 r = retrieve_specialization (tmpl, gen_args, 0);
12738 }
12739 else if (DECL_FUNCTION_SCOPE_P (decl)
12740 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12741 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12742 r = retrieve_local_specialization (decl);
12743 else
12744 /* The typedef is from a non-template context. */
12745 return t;
12746
12747 if (r)
12748 {
12749 r = TREE_TYPE (r);
12750 r = cp_build_qualified_type_real
12751 (r, cp_type_quals (t) | cp_type_quals (r),
12752 complain | tf_ignore_bad_quals);
12753 return r;
12754 }
12755 else
12756 {
12757 /* We don't have an instantiation yet, so drop the typedef. */
12758 int quals = cp_type_quals (t);
12759 t = DECL_ORIGINAL_TYPE (decl);
12760 t = cp_build_qualified_type_real (t, quals,
12761 complain | tf_ignore_bad_quals);
12762 }
12763 }
12764
12765 if (type
12766 && code != TYPENAME_TYPE
12767 && code != TEMPLATE_TYPE_PARM
12768 && code != IDENTIFIER_NODE
12769 && code != FUNCTION_TYPE
12770 && code != METHOD_TYPE)
12771 type = tsubst (type, args, complain, in_decl);
12772 if (type == error_mark_node)
12773 return error_mark_node;
12774
12775 switch (code)
12776 {
12777 case RECORD_TYPE:
12778 case UNION_TYPE:
12779 case ENUMERAL_TYPE:
12780 return tsubst_aggr_type (t, args, complain, in_decl,
12781 /*entering_scope=*/0);
12782
12783 case ERROR_MARK:
12784 case IDENTIFIER_NODE:
12785 case VOID_TYPE:
12786 case REAL_TYPE:
12787 case COMPLEX_TYPE:
12788 case VECTOR_TYPE:
12789 case BOOLEAN_TYPE:
12790 case NULLPTR_TYPE:
12791 case LANG_TYPE:
12792 return t;
12793
12794 case INTEGER_TYPE:
12795 if (t == integer_type_node)
12796 return t;
12797
12798 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST)
12799 {
12800 if (!TYPE_MAX_VALUE (t))
12801 return compute_array_index_type (NULL_TREE, NULL_TREE, complain);
12802
12803 if (TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
12804 return t;
12805 }
12806
12807 {
12808 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
12809
12810 max = tsubst_expr (omax, args, complain, in_decl,
12811 /*integral_constant_expression_p=*/false);
12812
12813 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12814 needed. */
12815 if (TREE_CODE (max) == NOP_EXPR
12816 && TREE_SIDE_EFFECTS (omax)
12817 && !TREE_TYPE (max))
12818 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
12819
12820 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12821 with TREE_SIDE_EFFECTS that indicates this is not an integral
12822 constant expression. */
12823 if (processing_template_decl
12824 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
12825 {
12826 gcc_assert (TREE_CODE (max) == NOP_EXPR);
12827 TREE_SIDE_EFFECTS (max) = 1;
12828 }
12829
12830 return compute_array_index_type (NULL_TREE, max, complain);
12831 }
12832
12833 case TEMPLATE_TYPE_PARM:
12834 case TEMPLATE_TEMPLATE_PARM:
12835 case BOUND_TEMPLATE_TEMPLATE_PARM:
12836 case TEMPLATE_PARM_INDEX:
12837 {
12838 int idx;
12839 int level;
12840 int levels;
12841 tree arg = NULL_TREE;
12842
12843 /* Early in template argument deduction substitution, we don't
12844 want to reduce the level of 'auto', or it will be confused
12845 with a normal template parm in subsequent deduction. */
12846 if (is_auto (t) && (complain & tf_partial))
12847 return t;
12848
12849 r = NULL_TREE;
12850
12851 gcc_assert (TREE_VEC_LENGTH (args) > 0);
12852 template_parm_level_and_index (t, &level, &idx);
12853
12854 levels = TMPL_ARGS_DEPTH (args);
12855 if (level <= levels
12856 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
12857 {
12858 arg = TMPL_ARG (args, level, idx);
12859
12860 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
12861 {
12862 /* See through ARGUMENT_PACK_SELECT arguments. */
12863 arg = ARGUMENT_PACK_SELECT_ARG (arg);
12864 /* If the selected argument is an expansion E, that most
12865 likely means we were called from
12866 gen_elem_of_pack_expansion_instantiation during the
12867 substituting of pack an argument pack (which Ith
12868 element is a pack expansion, where I is
12869 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12870 In this case, the Ith element resulting from this
12871 substituting is going to be a pack expansion, which
12872 pattern is the pattern of E. Let's return the
12873 pattern of E, and
12874 gen_elem_of_pack_expansion_instantiation will
12875 build the resulting pack expansion from it. */
12876 if (PACK_EXPANSION_P (arg))
12877 {
12878 /* Make sure we aren't throwing away arg info. */
12879 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
12880 arg = PACK_EXPANSION_PATTERN (arg);
12881 }
12882 }
12883 }
12884
12885 if (arg == error_mark_node)
12886 return error_mark_node;
12887 else if (arg != NULL_TREE)
12888 {
12889 if (ARGUMENT_PACK_P (arg))
12890 /* If ARG is an argument pack, we don't actually want to
12891 perform a substitution here, because substitutions
12892 for argument packs are only done
12893 element-by-element. We can get to this point when
12894 substituting the type of a non-type template
12895 parameter pack, when that type actually contains
12896 template parameter packs from an outer template, e.g.,
12897
12898 template<typename... Types> struct A {
12899 template<Types... Values> struct B { };
12900 }; */
12901 return t;
12902
12903 if (code == TEMPLATE_TYPE_PARM)
12904 {
12905 int quals;
12906 gcc_assert (TYPE_P (arg));
12907
12908 quals = cp_type_quals (arg) | cp_type_quals (t);
12909
12910 return cp_build_qualified_type_real
12911 (arg, quals, complain | tf_ignore_bad_quals);
12912 }
12913 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12914 {
12915 /* We are processing a type constructed from a
12916 template template parameter. */
12917 tree argvec = tsubst (TYPE_TI_ARGS (t),
12918 args, complain, in_decl);
12919 if (argvec == error_mark_node)
12920 return error_mark_node;
12921
12922 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12923 || TREE_CODE (arg) == TEMPLATE_DECL
12924 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12925
12926 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12927 /* Consider this code:
12928
12929 template <template <class> class Template>
12930 struct Internal {
12931 template <class Arg> using Bind = Template<Arg>;
12932 };
12933
12934 template <template <class> class Template, class Arg>
12935 using Instantiate = Template<Arg>; //#0
12936
12937 template <template <class> class Template,
12938 class Argument>
12939 using Bind =
12940 Instantiate<Internal<Template>::template Bind,
12941 Argument>; //#1
12942
12943 When #1 is parsed, the
12944 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12945 parameter `Template' in #0 matches the
12946 UNBOUND_CLASS_TEMPLATE representing the argument
12947 `Internal<Template>::template Bind'; We then want
12948 to assemble the type `Bind<Argument>' that can't
12949 be fully created right now, because
12950 `Internal<Template>' not being complete, the Bind
12951 template cannot be looked up in that context. So
12952 we need to "store" `Bind<Argument>' for later
12953 when the context of Bind becomes complete. Let's
12954 store that in a TYPENAME_TYPE. */
12955 return make_typename_type (TYPE_CONTEXT (arg),
12956 build_nt (TEMPLATE_ID_EXPR,
12957 TYPE_IDENTIFIER (arg),
12958 argvec),
12959 typename_type,
12960 complain);
12961
12962 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12963 are resolving nested-types in the signature of a
12964 member function templates. Otherwise ARG is a
12965 TEMPLATE_DECL and is the real template to be
12966 instantiated. */
12967 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12968 arg = TYPE_NAME (arg);
12969
12970 r = lookup_template_class (arg,
12971 argvec, in_decl,
12972 DECL_CONTEXT (arg),
12973 /*entering_scope=*/0,
12974 complain);
12975 return cp_build_qualified_type_real
12976 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12977 }
12978 else
12979 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12980 return convert_from_reference (unshare_expr (arg));
12981 }
12982
12983 if (level == 1)
12984 /* This can happen during the attempted tsubst'ing in
12985 unify. This means that we don't yet have any information
12986 about the template parameter in question. */
12987 return t;
12988
12989 /* If we get here, we must have been looking at a parm for a
12990 more deeply nested template. Make a new version of this
12991 template parameter, but with a lower level. */
12992 switch (code)
12993 {
12994 case TEMPLATE_TYPE_PARM:
12995 case TEMPLATE_TEMPLATE_PARM:
12996 case BOUND_TEMPLATE_TEMPLATE_PARM:
12997 if (cp_type_quals (t))
12998 {
12999 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13000 r = cp_build_qualified_type_real
13001 (r, cp_type_quals (t),
13002 complain | (code == TEMPLATE_TYPE_PARM
13003 ? tf_ignore_bad_quals : 0));
13004 }
13005 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13006 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13007 && (r = (TEMPLATE_PARM_DESCENDANTS
13008 (TEMPLATE_TYPE_PARM_INDEX (t))))
13009 && (r = TREE_TYPE (r))
13010 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13011 /* Break infinite recursion when substituting the constraints
13012 of a constrained placeholder. */;
13013 else
13014 {
13015 r = copy_type (t);
13016 TEMPLATE_TYPE_PARM_INDEX (r)
13017 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13018 r, levels, args, complain);
13019 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13020 TYPE_MAIN_VARIANT (r) = r;
13021 TYPE_POINTER_TO (r) = NULL_TREE;
13022 TYPE_REFERENCE_TO (r) = NULL_TREE;
13023
13024 /* Propagate constraints on placeholders. */
13025 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13026 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13027 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13028 = tsubst_constraint (constr, args, complain, in_decl);
13029
13030 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13031 /* We have reduced the level of the template
13032 template parameter, but not the levels of its
13033 template parameters, so canonical_type_parameter
13034 will not be able to find the canonical template
13035 template parameter for this level. Thus, we
13036 require structural equality checking to compare
13037 TEMPLATE_TEMPLATE_PARMs. */
13038 SET_TYPE_STRUCTURAL_EQUALITY (r);
13039 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13040 SET_TYPE_STRUCTURAL_EQUALITY (r);
13041 else
13042 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13043
13044 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13045 {
13046 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
13047 complain, in_decl);
13048 if (argvec == error_mark_node)
13049 return error_mark_node;
13050
13051 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13052 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
13053 }
13054 }
13055 break;
13056
13057 case TEMPLATE_PARM_INDEX:
13058 r = reduce_template_parm_level (t, type, levels, args, complain);
13059 break;
13060
13061 default:
13062 gcc_unreachable ();
13063 }
13064
13065 return r;
13066 }
13067
13068 case TREE_LIST:
13069 {
13070 tree purpose, value, chain;
13071
13072 if (t == void_list_node)
13073 return t;
13074
13075 purpose = TREE_PURPOSE (t);
13076 if (purpose)
13077 {
13078 purpose = tsubst (purpose, args, complain, in_decl);
13079 if (purpose == error_mark_node)
13080 return error_mark_node;
13081 }
13082 value = TREE_VALUE (t);
13083 if (value)
13084 {
13085 value = tsubst (value, args, complain, in_decl);
13086 if (value == error_mark_node)
13087 return error_mark_node;
13088 }
13089 chain = TREE_CHAIN (t);
13090 if (chain && chain != void_type_node)
13091 {
13092 chain = tsubst (chain, args, complain, in_decl);
13093 if (chain == error_mark_node)
13094 return error_mark_node;
13095 }
13096 if (purpose == TREE_PURPOSE (t)
13097 && value == TREE_VALUE (t)
13098 && chain == TREE_CHAIN (t))
13099 return t;
13100 return hash_tree_cons (purpose, value, chain);
13101 }
13102
13103 case TREE_BINFO:
13104 /* We should never be tsubsting a binfo. */
13105 gcc_unreachable ();
13106
13107 case TREE_VEC:
13108 /* A vector of template arguments. */
13109 gcc_assert (!type);
13110 return tsubst_template_args (t, args, complain, in_decl);
13111
13112 case POINTER_TYPE:
13113 case REFERENCE_TYPE:
13114 {
13115 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13116 return t;
13117
13118 /* [temp.deduct]
13119
13120 Type deduction may fail for any of the following
13121 reasons:
13122
13123 -- Attempting to create a pointer to reference type.
13124 -- Attempting to create a reference to a reference type or
13125 a reference to void.
13126
13127 Core issue 106 says that creating a reference to a reference
13128 during instantiation is no longer a cause for failure. We
13129 only enforce this check in strict C++98 mode. */
13130 if ((TREE_CODE (type) == REFERENCE_TYPE
13131 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13132 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13133 {
13134 static location_t last_loc;
13135
13136 /* We keep track of the last time we issued this error
13137 message to avoid spewing a ton of messages during a
13138 single bad template instantiation. */
13139 if (complain & tf_error
13140 && last_loc != input_location)
13141 {
13142 if (VOID_TYPE_P (type))
13143 error ("forming reference to void");
13144 else if (code == POINTER_TYPE)
13145 error ("forming pointer to reference type %qT", type);
13146 else
13147 error ("forming reference to reference type %qT", type);
13148 last_loc = input_location;
13149 }
13150
13151 return error_mark_node;
13152 }
13153 else if (TREE_CODE (type) == FUNCTION_TYPE
13154 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13155 || type_memfn_rqual (type) != REF_QUAL_NONE))
13156 {
13157 if (complain & tf_error)
13158 {
13159 if (code == POINTER_TYPE)
13160 error ("forming pointer to qualified function type %qT",
13161 type);
13162 else
13163 error ("forming reference to qualified function type %qT",
13164 type);
13165 }
13166 return error_mark_node;
13167 }
13168 else if (code == POINTER_TYPE)
13169 {
13170 r = build_pointer_type (type);
13171 if (TREE_CODE (type) == METHOD_TYPE)
13172 r = build_ptrmemfunc_type (r);
13173 }
13174 else if (TREE_CODE (type) == REFERENCE_TYPE)
13175 /* In C++0x, during template argument substitution, when there is an
13176 attempt to create a reference to a reference type, reference
13177 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13178
13179 "If a template-argument for a template-parameter T names a type
13180 that is a reference to a type A, an attempt to create the type
13181 'lvalue reference to cv T' creates the type 'lvalue reference to
13182 A,' while an attempt to create the type type rvalue reference to
13183 cv T' creates the type T"
13184 */
13185 r = cp_build_reference_type
13186 (TREE_TYPE (type),
13187 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13188 else
13189 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13190 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13191
13192 if (r != error_mark_node)
13193 /* Will this ever be needed for TYPE_..._TO values? */
13194 layout_type (r);
13195
13196 return r;
13197 }
13198 case OFFSET_TYPE:
13199 {
13200 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13201 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13202 {
13203 /* [temp.deduct]
13204
13205 Type deduction may fail for any of the following
13206 reasons:
13207
13208 -- Attempting to create "pointer to member of T" when T
13209 is not a class type. */
13210 if (complain & tf_error)
13211 error ("creating pointer to member of non-class type %qT", r);
13212 return error_mark_node;
13213 }
13214 if (TREE_CODE (type) == REFERENCE_TYPE)
13215 {
13216 if (complain & tf_error)
13217 error ("creating pointer to member reference type %qT", type);
13218 return error_mark_node;
13219 }
13220 if (VOID_TYPE_P (type))
13221 {
13222 if (complain & tf_error)
13223 error ("creating pointer to member of type void");
13224 return error_mark_node;
13225 }
13226 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13227 if (TREE_CODE (type) == FUNCTION_TYPE)
13228 {
13229 /* The type of the implicit object parameter gets its
13230 cv-qualifiers from the FUNCTION_TYPE. */
13231 tree memptr;
13232 tree method_type
13233 = build_memfn_type (type, r, type_memfn_quals (type),
13234 type_memfn_rqual (type));
13235 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13236 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13237 complain);
13238 }
13239 else
13240 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13241 cp_type_quals (t),
13242 complain);
13243 }
13244 case FUNCTION_TYPE:
13245 case METHOD_TYPE:
13246 {
13247 tree fntype;
13248 tree specs;
13249 fntype = tsubst_function_type (t, args, complain, in_decl);
13250 if (fntype == error_mark_node)
13251 return error_mark_node;
13252
13253 /* Substitute the exception specification. */
13254 specs = tsubst_exception_specification (t, args, complain,
13255 in_decl, /*defer_ok*/true);
13256 if (specs == error_mark_node)
13257 return error_mark_node;
13258 if (specs)
13259 fntype = build_exception_variant (fntype, specs);
13260 return fntype;
13261 }
13262 case ARRAY_TYPE:
13263 {
13264 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13265 if (domain == error_mark_node)
13266 return error_mark_node;
13267
13268 /* As an optimization, we avoid regenerating the array type if
13269 it will obviously be the same as T. */
13270 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13271 return t;
13272
13273 /* These checks should match the ones in create_array_type_for_decl.
13274
13275 [temp.deduct]
13276
13277 The deduction may fail for any of the following reasons:
13278
13279 -- Attempting to create an array with an element type that
13280 is void, a function type, or a reference type, or [DR337]
13281 an abstract class type. */
13282 if (VOID_TYPE_P (type)
13283 || TREE_CODE (type) == FUNCTION_TYPE
13284 || (TREE_CODE (type) == ARRAY_TYPE
13285 && TYPE_DOMAIN (type) == NULL_TREE)
13286 || TREE_CODE (type) == REFERENCE_TYPE)
13287 {
13288 if (complain & tf_error)
13289 error ("creating array of %qT", type);
13290 return error_mark_node;
13291 }
13292
13293 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13294 return error_mark_node;
13295
13296 r = build_cplus_array_type (type, domain);
13297
13298 if (TYPE_USER_ALIGN (t))
13299 {
13300 TYPE_ALIGN (r) = TYPE_ALIGN (t);
13301 TYPE_USER_ALIGN (r) = 1;
13302 }
13303
13304 return r;
13305 }
13306
13307 case TYPENAME_TYPE:
13308 {
13309 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13310 in_decl, /*entering_scope=*/1);
13311 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13312 complain, in_decl);
13313
13314 if (ctx == error_mark_node || f == error_mark_node)
13315 return error_mark_node;
13316
13317 if (!MAYBE_CLASS_TYPE_P (ctx))
13318 {
13319 if (complain & tf_error)
13320 error ("%qT is not a class, struct, or union type", ctx);
13321 return error_mark_node;
13322 }
13323 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13324 {
13325 /* Normally, make_typename_type does not require that the CTX
13326 have complete type in order to allow things like:
13327
13328 template <class T> struct S { typename S<T>::X Y; };
13329
13330 But, such constructs have already been resolved by this
13331 point, so here CTX really should have complete type, unless
13332 it's a partial instantiation. */
13333 ctx = complete_type (ctx);
13334 if (!COMPLETE_TYPE_P (ctx))
13335 {
13336 if (complain & tf_error)
13337 cxx_incomplete_type_error (NULL_TREE, ctx);
13338 return error_mark_node;
13339 }
13340 }
13341
13342 f = make_typename_type (ctx, f, typename_type,
13343 complain | tf_keep_type_decl);
13344 if (f == error_mark_node)
13345 return f;
13346 if (TREE_CODE (f) == TYPE_DECL)
13347 {
13348 complain |= tf_ignore_bad_quals;
13349 f = TREE_TYPE (f);
13350 }
13351
13352 if (TREE_CODE (f) != TYPENAME_TYPE)
13353 {
13354 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13355 {
13356 if (complain & tf_error)
13357 error ("%qT resolves to %qT, which is not an enumeration type",
13358 t, f);
13359 else
13360 return error_mark_node;
13361 }
13362 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13363 {
13364 if (complain & tf_error)
13365 error ("%qT resolves to %qT, which is is not a class type",
13366 t, f);
13367 else
13368 return error_mark_node;
13369 }
13370 }
13371
13372 return cp_build_qualified_type_real
13373 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13374 }
13375
13376 case UNBOUND_CLASS_TEMPLATE:
13377 {
13378 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13379 in_decl, /*entering_scope=*/1);
13380 tree name = TYPE_IDENTIFIER (t);
13381 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13382
13383 if (ctx == error_mark_node || name == error_mark_node)
13384 return error_mark_node;
13385
13386 if (parm_list)
13387 parm_list = tsubst_template_parms (parm_list, args, complain);
13388 return make_unbound_class_template (ctx, name, parm_list, complain);
13389 }
13390
13391 case TYPEOF_TYPE:
13392 {
13393 tree type;
13394
13395 ++cp_unevaluated_operand;
13396 ++c_inhibit_evaluation_warnings;
13397
13398 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13399 complain, in_decl,
13400 /*integral_constant_expression_p=*/false);
13401
13402 --cp_unevaluated_operand;
13403 --c_inhibit_evaluation_warnings;
13404
13405 type = finish_typeof (type);
13406 return cp_build_qualified_type_real (type,
13407 cp_type_quals (t)
13408 | cp_type_quals (type),
13409 complain);
13410 }
13411
13412 case DECLTYPE_TYPE:
13413 {
13414 tree type;
13415
13416 ++cp_unevaluated_operand;
13417 ++c_inhibit_evaluation_warnings;
13418
13419 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13420 complain|tf_decltype, in_decl,
13421 /*function_p*/false,
13422 /*integral_constant_expression*/false);
13423
13424 --cp_unevaluated_operand;
13425 --c_inhibit_evaluation_warnings;
13426
13427 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13428 type = lambda_capture_field_type (type,
13429 DECLTYPE_FOR_INIT_CAPTURE (t));
13430 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13431 type = lambda_proxy_type (type);
13432 else
13433 {
13434 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13435 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13436 && EXPR_P (type))
13437 /* In a template ~id could be either a complement expression
13438 or an unqualified-id naming a destructor; if instantiating
13439 it produces an expression, it's not an id-expression or
13440 member access. */
13441 id = false;
13442 type = finish_decltype_type (type, id, complain);
13443 }
13444 return cp_build_qualified_type_real (type,
13445 cp_type_quals (t)
13446 | cp_type_quals (type),
13447 complain | tf_ignore_bad_quals);
13448 }
13449
13450 case UNDERLYING_TYPE:
13451 {
13452 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13453 complain, in_decl);
13454 return finish_underlying_type (type);
13455 }
13456
13457 case TYPE_ARGUMENT_PACK:
13458 case NONTYPE_ARGUMENT_PACK:
13459 {
13460 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13461 tree packed_out =
13462 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13463 args,
13464 complain,
13465 in_decl);
13466 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13467
13468 /* For template nontype argument packs, also substitute into
13469 the type. */
13470 if (code == NONTYPE_ARGUMENT_PACK)
13471 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13472
13473 return r;
13474 }
13475 break;
13476
13477 case VOID_CST:
13478 case INTEGER_CST:
13479 case REAL_CST:
13480 case STRING_CST:
13481 case PLUS_EXPR:
13482 case MINUS_EXPR:
13483 case NEGATE_EXPR:
13484 case NOP_EXPR:
13485 case INDIRECT_REF:
13486 case ADDR_EXPR:
13487 case CALL_EXPR:
13488 case ARRAY_REF:
13489 case SCOPE_REF:
13490 /* We should use one of the expression tsubsts for these codes. */
13491 gcc_unreachable ();
13492
13493 default:
13494 sorry ("use of %qs in template", get_tree_code_name (code));
13495 return error_mark_node;
13496 }
13497 }
13498
13499 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13500 type of the expression on the left-hand side of the "." or "->"
13501 operator. */
13502
13503 static tree
13504 tsubst_baselink (tree baselink, tree object_type,
13505 tree args, tsubst_flags_t complain, tree in_decl)
13506 {
13507 tree name;
13508 tree qualifying_scope;
13509 tree fns;
13510 tree optype;
13511 tree template_args = 0;
13512 bool template_id_p = false;
13513 bool qualified = BASELINK_QUALIFIED_P (baselink);
13514
13515 /* A baselink indicates a function from a base class. Both the
13516 BASELINK_ACCESS_BINFO and the base class referenced may
13517 indicate bases of the template class, rather than the
13518 instantiated class. In addition, lookups that were not
13519 ambiguous before may be ambiguous now. Therefore, we perform
13520 the lookup again. */
13521 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13522 qualifying_scope = tsubst (qualifying_scope, args,
13523 complain, in_decl);
13524 fns = BASELINK_FUNCTIONS (baselink);
13525 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13526 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13527 {
13528 template_id_p = true;
13529 template_args = TREE_OPERAND (fns, 1);
13530 fns = TREE_OPERAND (fns, 0);
13531 if (template_args)
13532 template_args = tsubst_template_args (template_args, args,
13533 complain, in_decl);
13534 }
13535 name = DECL_NAME (get_first_fn (fns));
13536 if (IDENTIFIER_TYPENAME_P (name))
13537 name = mangle_conv_op_name_for_type (optype);
13538 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13539 if (!baselink)
13540 return error_mark_node;
13541
13542 /* If lookup found a single function, mark it as used at this
13543 point. (If it lookup found multiple functions the one selected
13544 later by overload resolution will be marked as used at that
13545 point.) */
13546 if (BASELINK_P (baselink))
13547 fns = BASELINK_FUNCTIONS (baselink);
13548 if (!template_id_p && !really_overloaded_fn (fns)
13549 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13550 return error_mark_node;
13551
13552 /* Add back the template arguments, if present. */
13553 if (BASELINK_P (baselink) && template_id_p)
13554 BASELINK_FUNCTIONS (baselink)
13555 = build_nt (TEMPLATE_ID_EXPR,
13556 BASELINK_FUNCTIONS (baselink),
13557 template_args);
13558 /* Update the conversion operator type. */
13559 BASELINK_OPTYPE (baselink) = optype;
13560
13561 if (!object_type)
13562 object_type = current_class_type;
13563
13564 if (qualified)
13565 baselink = adjust_result_of_qualified_name_lookup (baselink,
13566 qualifying_scope,
13567 object_type);
13568 return baselink;
13569 }
13570
13571 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13572 true if the qualified-id will be a postfix-expression in-and-of
13573 itself; false if more of the postfix-expression follows the
13574 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13575 of "&". */
13576
13577 static tree
13578 tsubst_qualified_id (tree qualified_id, tree args,
13579 tsubst_flags_t complain, tree in_decl,
13580 bool done, bool address_p)
13581 {
13582 tree expr;
13583 tree scope;
13584 tree name;
13585 bool is_template;
13586 tree template_args;
13587 location_t loc = UNKNOWN_LOCATION;
13588
13589 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13590
13591 /* Figure out what name to look up. */
13592 name = TREE_OPERAND (qualified_id, 1);
13593 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13594 {
13595 is_template = true;
13596 loc = EXPR_LOCATION (name);
13597 template_args = TREE_OPERAND (name, 1);
13598 if (template_args)
13599 template_args = tsubst_template_args (template_args, args,
13600 complain, in_decl);
13601 name = TREE_OPERAND (name, 0);
13602 }
13603 else
13604 {
13605 is_template = false;
13606 template_args = NULL_TREE;
13607 }
13608
13609 /* Substitute into the qualifying scope. When there are no ARGS, we
13610 are just trying to simplify a non-dependent expression. In that
13611 case the qualifying scope may be dependent, and, in any case,
13612 substituting will not help. */
13613 scope = TREE_OPERAND (qualified_id, 0);
13614 if (args)
13615 {
13616 scope = tsubst (scope, args, complain, in_decl);
13617 expr = tsubst_copy (name, args, complain, in_decl);
13618 }
13619 else
13620 expr = name;
13621
13622 if (dependent_scope_p (scope))
13623 {
13624 if (is_template)
13625 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13626 return build_qualified_name (NULL_TREE, scope, expr,
13627 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13628 }
13629
13630 if (!BASELINK_P (name) && !DECL_P (expr))
13631 {
13632 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13633 {
13634 /* A BIT_NOT_EXPR is used to represent a destructor. */
13635 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13636 {
13637 error ("qualifying type %qT does not match destructor name ~%qT",
13638 scope, TREE_OPERAND (expr, 0));
13639 expr = error_mark_node;
13640 }
13641 else
13642 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13643 /*is_type_p=*/0, false);
13644 }
13645 else
13646 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13647 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13648 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13649 {
13650 if (complain & tf_error)
13651 {
13652 error ("dependent-name %qE is parsed as a non-type, but "
13653 "instantiation yields a type", qualified_id);
13654 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13655 }
13656 return error_mark_node;
13657 }
13658 }
13659
13660 if (DECL_P (expr))
13661 {
13662 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13663 scope);
13664 /* Remember that there was a reference to this entity. */
13665 if (!mark_used (expr, complain) && !(complain & tf_error))
13666 return error_mark_node;
13667 }
13668
13669 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13670 {
13671 if (complain & tf_error)
13672 qualified_name_lookup_error (scope,
13673 TREE_OPERAND (qualified_id, 1),
13674 expr, input_location);
13675 return error_mark_node;
13676 }
13677
13678 if (is_template)
13679 expr = lookup_template_function (expr, template_args);
13680
13681 if (expr == error_mark_node && complain & tf_error)
13682 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
13683 expr, input_location);
13684 else if (TYPE_P (scope))
13685 {
13686 expr = (adjust_result_of_qualified_name_lookup
13687 (expr, scope, current_nonlambda_class_type ()));
13688 expr = (finish_qualified_id_expr
13689 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
13690 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
13691 /*template_arg_p=*/false, complain));
13692 }
13693
13694 /* Expressions do not generally have reference type. */
13695 if (TREE_CODE (expr) != SCOPE_REF
13696 /* However, if we're about to form a pointer-to-member, we just
13697 want the referenced member referenced. */
13698 && TREE_CODE (expr) != OFFSET_REF)
13699 expr = convert_from_reference (expr);
13700
13701 return expr;
13702 }
13703
13704 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13705 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13706 for tsubst. */
13707
13708 static tree
13709 tsubst_init (tree init, tree decl, tree args,
13710 tsubst_flags_t complain, tree in_decl)
13711 {
13712 if (!init)
13713 return NULL_TREE;
13714
13715 init = tsubst_expr (init, args, complain, in_decl, false);
13716
13717 if (!init)
13718 {
13719 /* If we had an initializer but it
13720 instantiated to nothing,
13721 value-initialize the object. This will
13722 only occur when the initializer was a
13723 pack expansion where the parameter packs
13724 used in that expansion were of length
13725 zero. */
13726 init = build_value_init (TREE_TYPE (decl),
13727 complain);
13728 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13729 init = get_target_expr_sfinae (init, complain);
13730 }
13731
13732 return init;
13733 }
13734
13735 /* Like tsubst, but deals with expressions. This function just replaces
13736 template parms; to finish processing the resultant expression, use
13737 tsubst_copy_and_build or tsubst_expr. */
13738
13739 static tree
13740 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13741 {
13742 enum tree_code code;
13743 tree r;
13744
13745 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
13746 return t;
13747
13748 code = TREE_CODE (t);
13749
13750 switch (code)
13751 {
13752 case PARM_DECL:
13753 r = retrieve_local_specialization (t);
13754
13755 if (r == NULL_TREE)
13756 {
13757 /* We get here for a use of 'this' in an NSDMI. */
13758 if (DECL_NAME (t) == this_identifier
13759 && current_function_decl
13760 && DECL_CONSTRUCTOR_P (current_function_decl))
13761 return current_class_ptr;
13762
13763 /* This can happen for a parameter name used later in a function
13764 declaration (such as in a late-specified return type). Just
13765 make a dummy decl, since it's only used for its type. */
13766 gcc_assert (cp_unevaluated_operand != 0);
13767 r = tsubst_decl (t, args, complain);
13768 /* Give it the template pattern as its context; its true context
13769 hasn't been instantiated yet and this is good enough for
13770 mangling. */
13771 DECL_CONTEXT (r) = DECL_CONTEXT (t);
13772 }
13773
13774 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13775 r = ARGUMENT_PACK_SELECT_ARG (r);
13776 if (!mark_used (r, complain) && !(complain & tf_error))
13777 return error_mark_node;
13778 return r;
13779
13780 case CONST_DECL:
13781 {
13782 tree enum_type;
13783 tree v;
13784
13785 if (DECL_TEMPLATE_PARM_P (t))
13786 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
13787 /* There is no need to substitute into namespace-scope
13788 enumerators. */
13789 if (DECL_NAMESPACE_SCOPE_P (t))
13790 return t;
13791 /* If ARGS is NULL, then T is known to be non-dependent. */
13792 if (args == NULL_TREE)
13793 return scalar_constant_value (t);
13794
13795 /* Unfortunately, we cannot just call lookup_name here.
13796 Consider:
13797
13798 template <int I> int f() {
13799 enum E { a = I };
13800 struct S { void g() { E e = a; } };
13801 };
13802
13803 When we instantiate f<7>::S::g(), say, lookup_name is not
13804 clever enough to find f<7>::a. */
13805 enum_type
13806 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13807 /*entering_scope=*/0);
13808
13809 for (v = TYPE_VALUES (enum_type);
13810 v != NULL_TREE;
13811 v = TREE_CHAIN (v))
13812 if (TREE_PURPOSE (v) == DECL_NAME (t))
13813 return TREE_VALUE (v);
13814
13815 /* We didn't find the name. That should never happen; if
13816 name-lookup found it during preliminary parsing, we
13817 should find it again here during instantiation. */
13818 gcc_unreachable ();
13819 }
13820 return t;
13821
13822 case FIELD_DECL:
13823 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13824 {
13825 /* Check for a local specialization set up by
13826 tsubst_pack_expansion. */
13827 if (tree r = retrieve_local_specialization (t))
13828 {
13829 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13830 r = ARGUMENT_PACK_SELECT_ARG (r);
13831 return r;
13832 }
13833
13834 /* When retrieving a capture pack from a generic lambda, remove the
13835 lambda call op's own template argument list from ARGS. Only the
13836 template arguments active for the closure type should be used to
13837 retrieve the pack specialization. */
13838 if (LAMBDA_FUNCTION_P (current_function_decl)
13839 && (template_class_depth (DECL_CONTEXT (t))
13840 != TMPL_ARGS_DEPTH (args)))
13841 args = strip_innermost_template_args (args, 1);
13842
13843 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13844 tsubst_decl put in the hash table. */
13845 return retrieve_specialization (t, args, 0);
13846 }
13847
13848 if (DECL_CONTEXT (t))
13849 {
13850 tree ctx;
13851
13852 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13853 /*entering_scope=*/1);
13854 if (ctx != DECL_CONTEXT (t))
13855 {
13856 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
13857 if (!r)
13858 {
13859 if (complain & tf_error)
13860 error ("using invalid field %qD", t);
13861 return error_mark_node;
13862 }
13863 return r;
13864 }
13865 }
13866
13867 return t;
13868
13869 case VAR_DECL:
13870 case FUNCTION_DECL:
13871 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
13872 r = tsubst (t, args, complain, in_decl);
13873 else if (local_variable_p (t))
13874 {
13875 r = retrieve_local_specialization (t);
13876 if (r == NULL_TREE)
13877 {
13878 /* First try name lookup to find the instantiation. */
13879 r = lookup_name (DECL_NAME (t));
13880 if (r)
13881 {
13882 /* Make sure that the one we found is the one we want. */
13883 tree ctx = DECL_CONTEXT (t);
13884 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
13885 ctx = tsubst (ctx, args, complain, in_decl);
13886 if (ctx != DECL_CONTEXT (r))
13887 r = NULL_TREE;
13888 }
13889
13890 if (r)
13891 /* OK */;
13892 else
13893 {
13894 /* This can happen for a variable used in a
13895 late-specified return type of a local lambda, or for a
13896 local static or constant. Building a new VAR_DECL
13897 should be OK in all those cases. */
13898 r = tsubst_decl (t, args, complain);
13899 if (decl_maybe_constant_var_p (r))
13900 {
13901 /* We can't call cp_finish_decl, so handle the
13902 initializer by hand. */
13903 tree init = tsubst_init (DECL_INITIAL (t), r, args,
13904 complain, in_decl);
13905 if (!processing_template_decl)
13906 init = maybe_constant_init (init);
13907 if (processing_template_decl
13908 ? potential_constant_expression (init)
13909 : reduced_constant_expression_p (init))
13910 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13911 = TREE_CONSTANT (r) = true;
13912 DECL_INITIAL (r) = init;
13913 }
13914 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13915 || decl_constant_var_p (r)
13916 || errorcount || sorrycount);
13917 if (!processing_template_decl)
13918 {
13919 if (TREE_STATIC (r))
13920 rest_of_decl_compilation (r, toplevel_bindings_p (),
13921 at_eof);
13922 else
13923 r = process_outer_var_ref (r, complain);
13924 }
13925 }
13926 /* Remember this for subsequent uses. */
13927 if (local_specializations)
13928 register_local_specialization (r, t);
13929 }
13930 }
13931 else
13932 r = t;
13933 if (!mark_used (r, complain) && !(complain & tf_error))
13934 return error_mark_node;
13935 return r;
13936
13937 case NAMESPACE_DECL:
13938 return t;
13939
13940 case OVERLOAD:
13941 /* An OVERLOAD will always be a non-dependent overload set; an
13942 overload set from function scope will just be represented with an
13943 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13944 gcc_assert (!uses_template_parms (t));
13945 return t;
13946
13947 case BASELINK:
13948 return tsubst_baselink (t, current_nonlambda_class_type (),
13949 args, complain, in_decl);
13950
13951 case TEMPLATE_DECL:
13952 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13953 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13954 args, complain, in_decl);
13955 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13956 return tsubst (t, args, complain, in_decl);
13957 else if (DECL_CLASS_SCOPE_P (t)
13958 && uses_template_parms (DECL_CONTEXT (t)))
13959 {
13960 /* Template template argument like the following example need
13961 special treatment:
13962
13963 template <template <class> class TT> struct C {};
13964 template <class T> struct D {
13965 template <class U> struct E {};
13966 C<E> c; // #1
13967 };
13968 D<int> d; // #2
13969
13970 We are processing the template argument `E' in #1 for
13971 the template instantiation #2. Originally, `E' is a
13972 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13973 have to substitute this with one having context `D<int>'. */
13974
13975 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13976 return lookup_field (context, DECL_NAME(t), 0, false);
13977 }
13978 else
13979 /* Ordinary template template argument. */
13980 return t;
13981
13982 case CAST_EXPR:
13983 case REINTERPRET_CAST_EXPR:
13984 case CONST_CAST_EXPR:
13985 case STATIC_CAST_EXPR:
13986 case DYNAMIC_CAST_EXPR:
13987 case IMPLICIT_CONV_EXPR:
13988 case CONVERT_EXPR:
13989 case NOP_EXPR:
13990 {
13991 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13992 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13993 return build1 (code, type, op0);
13994 }
13995
13996 case SIZEOF_EXPR:
13997 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13998 {
13999 tree expanded, op = TREE_OPERAND (t, 0);
14000 int len = 0;
14001
14002 if (SIZEOF_EXPR_TYPE_P (t))
14003 op = TREE_TYPE (op);
14004
14005 ++cp_unevaluated_operand;
14006 ++c_inhibit_evaluation_warnings;
14007 /* We only want to compute the number of arguments. */
14008 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14009 --cp_unevaluated_operand;
14010 --c_inhibit_evaluation_warnings;
14011
14012 if (TREE_CODE (expanded) == TREE_VEC)
14013 {
14014 len = TREE_VEC_LENGTH (expanded);
14015 /* Set TREE_USED for the benefit of -Wunused. */
14016 for (int i = 0; i < len; i++)
14017 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14018 }
14019
14020 if (expanded == error_mark_node)
14021 return error_mark_node;
14022 else if (PACK_EXPANSION_P (expanded)
14023 || (TREE_CODE (expanded) == TREE_VEC
14024 && len > 0
14025 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
14026 {
14027 if (TREE_CODE (expanded) == TREE_VEC)
14028 expanded = TREE_VEC_ELT (expanded, len - 1);
14029 else
14030 PACK_EXPANSION_SIZEOF_P (expanded) = true;
14031
14032 if (TYPE_P (expanded))
14033 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14034 complain & tf_error);
14035 else
14036 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14037 complain & tf_error);
14038 }
14039 else
14040 return build_int_cst (size_type_node, len);
14041 }
14042 if (SIZEOF_EXPR_TYPE_P (t))
14043 {
14044 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14045 args, complain, in_decl);
14046 r = build1 (NOP_EXPR, r, error_mark_node);
14047 r = build1 (SIZEOF_EXPR,
14048 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14049 SIZEOF_EXPR_TYPE_P (r) = 1;
14050 return r;
14051 }
14052 /* Fall through */
14053
14054 case INDIRECT_REF:
14055 case NEGATE_EXPR:
14056 case TRUTH_NOT_EXPR:
14057 case BIT_NOT_EXPR:
14058 case ADDR_EXPR:
14059 case UNARY_PLUS_EXPR: /* Unary + */
14060 case ALIGNOF_EXPR:
14061 case AT_ENCODE_EXPR:
14062 case ARROW_EXPR:
14063 case THROW_EXPR:
14064 case TYPEID_EXPR:
14065 case REALPART_EXPR:
14066 case IMAGPART_EXPR:
14067 case PAREN_EXPR:
14068 {
14069 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14070 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14071 return build1 (code, type, op0);
14072 }
14073
14074 case COMPONENT_REF:
14075 {
14076 tree object;
14077 tree name;
14078
14079 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14080 name = TREE_OPERAND (t, 1);
14081 if (TREE_CODE (name) == BIT_NOT_EXPR)
14082 {
14083 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14084 complain, in_decl);
14085 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14086 }
14087 else if (TREE_CODE (name) == SCOPE_REF
14088 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14089 {
14090 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14091 complain, in_decl);
14092 name = TREE_OPERAND (name, 1);
14093 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14094 complain, in_decl);
14095 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14096 name = build_qualified_name (/*type=*/NULL_TREE,
14097 base, name,
14098 /*template_p=*/false);
14099 }
14100 else if (BASELINK_P (name))
14101 name = tsubst_baselink (name,
14102 non_reference (TREE_TYPE (object)),
14103 args, complain,
14104 in_decl);
14105 else
14106 name = tsubst_copy (name, args, complain, in_decl);
14107 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14108 }
14109
14110 case PLUS_EXPR:
14111 case MINUS_EXPR:
14112 case MULT_EXPR:
14113 case TRUNC_DIV_EXPR:
14114 case CEIL_DIV_EXPR:
14115 case FLOOR_DIV_EXPR:
14116 case ROUND_DIV_EXPR:
14117 case EXACT_DIV_EXPR:
14118 case BIT_AND_EXPR:
14119 case BIT_IOR_EXPR:
14120 case BIT_XOR_EXPR:
14121 case TRUNC_MOD_EXPR:
14122 case FLOOR_MOD_EXPR:
14123 case TRUTH_ANDIF_EXPR:
14124 case TRUTH_ORIF_EXPR:
14125 case TRUTH_AND_EXPR:
14126 case TRUTH_OR_EXPR:
14127 case RSHIFT_EXPR:
14128 case LSHIFT_EXPR:
14129 case RROTATE_EXPR:
14130 case LROTATE_EXPR:
14131 case EQ_EXPR:
14132 case NE_EXPR:
14133 case MAX_EXPR:
14134 case MIN_EXPR:
14135 case LE_EXPR:
14136 case GE_EXPR:
14137 case LT_EXPR:
14138 case GT_EXPR:
14139 case COMPOUND_EXPR:
14140 case DOTSTAR_EXPR:
14141 case MEMBER_REF:
14142 case PREDECREMENT_EXPR:
14143 case PREINCREMENT_EXPR:
14144 case POSTDECREMENT_EXPR:
14145 case POSTINCREMENT_EXPR:
14146 {
14147 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14148 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14149 return build_nt (code, op0, op1);
14150 }
14151
14152 case SCOPE_REF:
14153 {
14154 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14155 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14156 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14157 QUALIFIED_NAME_IS_TEMPLATE (t));
14158 }
14159
14160 case ARRAY_REF:
14161 {
14162 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14163 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14164 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14165 }
14166
14167 case CALL_EXPR:
14168 {
14169 int n = VL_EXP_OPERAND_LENGTH (t);
14170 tree result = build_vl_exp (CALL_EXPR, n);
14171 int i;
14172 for (i = 0; i < n; i++)
14173 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14174 complain, in_decl);
14175 return result;
14176 }
14177
14178 case COND_EXPR:
14179 case MODOP_EXPR:
14180 case PSEUDO_DTOR_EXPR:
14181 case VEC_PERM_EXPR:
14182 {
14183 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14184 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14185 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14186 r = build_nt (code, op0, op1, op2);
14187 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14188 return r;
14189 }
14190
14191 case NEW_EXPR:
14192 {
14193 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14194 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14195 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14196 r = build_nt (code, op0, op1, op2);
14197 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14198 return r;
14199 }
14200
14201 case DELETE_EXPR:
14202 {
14203 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14204 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14205 r = build_nt (code, op0, op1);
14206 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14207 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14208 return r;
14209 }
14210
14211 case TEMPLATE_ID_EXPR:
14212 {
14213 /* Substituted template arguments */
14214 tree fn = TREE_OPERAND (t, 0);
14215 tree targs = TREE_OPERAND (t, 1);
14216
14217 fn = tsubst_copy (fn, args, complain, in_decl);
14218 if (targs)
14219 targs = tsubst_template_args (targs, args, complain, in_decl);
14220
14221 return lookup_template_function (fn, targs);
14222 }
14223
14224 case TREE_LIST:
14225 {
14226 tree purpose, value, chain;
14227
14228 if (t == void_list_node)
14229 return t;
14230
14231 purpose = TREE_PURPOSE (t);
14232 if (purpose)
14233 purpose = tsubst_copy (purpose, args, complain, in_decl);
14234 value = TREE_VALUE (t);
14235 if (value)
14236 value = tsubst_copy (value, args, complain, in_decl);
14237 chain = TREE_CHAIN (t);
14238 if (chain && chain != void_type_node)
14239 chain = tsubst_copy (chain, args, complain, in_decl);
14240 if (purpose == TREE_PURPOSE (t)
14241 && value == TREE_VALUE (t)
14242 && chain == TREE_CHAIN (t))
14243 return t;
14244 return tree_cons (purpose, value, chain);
14245 }
14246
14247 case RECORD_TYPE:
14248 case UNION_TYPE:
14249 case ENUMERAL_TYPE:
14250 case INTEGER_TYPE:
14251 case TEMPLATE_TYPE_PARM:
14252 case TEMPLATE_TEMPLATE_PARM:
14253 case BOUND_TEMPLATE_TEMPLATE_PARM:
14254 case TEMPLATE_PARM_INDEX:
14255 case POINTER_TYPE:
14256 case REFERENCE_TYPE:
14257 case OFFSET_TYPE:
14258 case FUNCTION_TYPE:
14259 case METHOD_TYPE:
14260 case ARRAY_TYPE:
14261 case TYPENAME_TYPE:
14262 case UNBOUND_CLASS_TEMPLATE:
14263 case TYPEOF_TYPE:
14264 case DECLTYPE_TYPE:
14265 case TYPE_DECL:
14266 return tsubst (t, args, complain, in_decl);
14267
14268 case USING_DECL:
14269 t = DECL_NAME (t);
14270 /* Fall through. */
14271 case IDENTIFIER_NODE:
14272 if (IDENTIFIER_TYPENAME_P (t))
14273 {
14274 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14275 return mangle_conv_op_name_for_type (new_type);
14276 }
14277 else
14278 return t;
14279
14280 case CONSTRUCTOR:
14281 /* This is handled by tsubst_copy_and_build. */
14282 gcc_unreachable ();
14283
14284 case VA_ARG_EXPR:
14285 {
14286 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14287 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14288 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14289 }
14290
14291 case CLEANUP_POINT_EXPR:
14292 /* We shouldn't have built any of these during initial template
14293 generation. Instead, they should be built during instantiation
14294 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14295 gcc_unreachable ();
14296
14297 case OFFSET_REF:
14298 {
14299 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14300 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14301 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14302 r = build2 (code, type, op0, op1);
14303 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14304 if (!mark_used (TREE_OPERAND (r, 1), complain)
14305 && !(complain & tf_error))
14306 return error_mark_node;
14307 return r;
14308 }
14309
14310 case EXPR_PACK_EXPANSION:
14311 error ("invalid use of pack expansion expression");
14312 return error_mark_node;
14313
14314 case NONTYPE_ARGUMENT_PACK:
14315 error ("use %<...%> to expand argument pack");
14316 return error_mark_node;
14317
14318 case VOID_CST:
14319 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14320 return t;
14321
14322 case INTEGER_CST:
14323 case REAL_CST:
14324 case STRING_CST:
14325 case COMPLEX_CST:
14326 {
14327 /* Instantiate any typedefs in the type. */
14328 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14329 r = fold_convert (type, t);
14330 gcc_assert (TREE_CODE (r) == code);
14331 return r;
14332 }
14333
14334 case PTRMEM_CST:
14335 /* These can sometimes show up in a partial instantiation, but never
14336 involve template parms. */
14337 gcc_assert (!uses_template_parms (t));
14338 return t;
14339
14340 case UNARY_LEFT_FOLD_EXPR:
14341 return tsubst_unary_left_fold (t, args, complain, in_decl);
14342 case UNARY_RIGHT_FOLD_EXPR:
14343 return tsubst_unary_right_fold (t, args, complain, in_decl);
14344 case BINARY_LEFT_FOLD_EXPR:
14345 return tsubst_binary_left_fold (t, args, complain, in_decl);
14346 case BINARY_RIGHT_FOLD_EXPR:
14347 return tsubst_binary_right_fold (t, args, complain, in_decl);
14348
14349 default:
14350 /* We shouldn't get here, but keep going if !flag_checking. */
14351 if (flag_checking)
14352 gcc_unreachable ();
14353 return t;
14354 }
14355 }
14356
14357 /* Helper function for tsubst_omp_clauses, used for instantiation of
14358 OMP_CLAUSE_DECL of clauses. */
14359
14360 static tree
14361 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14362 tree in_decl)
14363 {
14364 if (decl == NULL_TREE)
14365 return NULL_TREE;
14366
14367 /* Handle an OpenMP array section represented as a TREE_LIST (or
14368 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14369 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14370 TREE_LIST. We can handle it exactly the same as an array section
14371 (purpose, value, and a chain), even though the nomenclature
14372 (low_bound, length, etc) is different. */
14373 if (TREE_CODE (decl) == TREE_LIST)
14374 {
14375 tree low_bound
14376 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14377 /*integral_constant_expression_p=*/false);
14378 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14379 /*integral_constant_expression_p=*/false);
14380 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14381 in_decl);
14382 if (TREE_PURPOSE (decl) == low_bound
14383 && TREE_VALUE (decl) == length
14384 && TREE_CHAIN (decl) == chain)
14385 return decl;
14386 tree ret = tree_cons (low_bound, length, chain);
14387 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
14388 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
14389 return ret;
14390 }
14391 tree ret = tsubst_expr (decl, args, complain, in_decl,
14392 /*integral_constant_expression_p=*/false);
14393 /* Undo convert_from_reference tsubst_expr could have called. */
14394 if (decl
14395 && REFERENCE_REF_P (ret)
14396 && !REFERENCE_REF_P (decl))
14397 ret = TREE_OPERAND (ret, 0);
14398 return ret;
14399 }
14400
14401 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14402
14403 static tree
14404 tsubst_omp_clauses (tree clauses, bool declare_simd, bool allow_fields,
14405 tree args, tsubst_flags_t complain, tree in_decl)
14406 {
14407 tree new_clauses = NULL_TREE, nc, oc;
14408 tree linear_no_step = NULL_TREE;
14409
14410 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14411 {
14412 nc = copy_node (oc);
14413 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14414 new_clauses = nc;
14415
14416 switch (OMP_CLAUSE_CODE (nc))
14417 {
14418 case OMP_CLAUSE_LASTPRIVATE:
14419 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14420 {
14421 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14422 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14423 in_decl, /*integral_constant_expression_p=*/false);
14424 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14425 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14426 }
14427 /* FALLTHRU */
14428 case OMP_CLAUSE_PRIVATE:
14429 case OMP_CLAUSE_SHARED:
14430 case OMP_CLAUSE_FIRSTPRIVATE:
14431 case OMP_CLAUSE_COPYIN:
14432 case OMP_CLAUSE_COPYPRIVATE:
14433 case OMP_CLAUSE_UNIFORM:
14434 case OMP_CLAUSE_DEPEND:
14435 case OMP_CLAUSE_FROM:
14436 case OMP_CLAUSE_TO:
14437 case OMP_CLAUSE_MAP:
14438 case OMP_CLAUSE_USE_DEVICE_PTR:
14439 case OMP_CLAUSE_IS_DEVICE_PTR:
14440 OMP_CLAUSE_DECL (nc)
14441 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14442 in_decl);
14443 break;
14444 case OMP_CLAUSE_IF:
14445 case OMP_CLAUSE_NUM_THREADS:
14446 case OMP_CLAUSE_SCHEDULE:
14447 case OMP_CLAUSE_COLLAPSE:
14448 case OMP_CLAUSE_FINAL:
14449 case OMP_CLAUSE_DEVICE:
14450 case OMP_CLAUSE_DIST_SCHEDULE:
14451 case OMP_CLAUSE_NUM_TEAMS:
14452 case OMP_CLAUSE_THREAD_LIMIT:
14453 case OMP_CLAUSE_SAFELEN:
14454 case OMP_CLAUSE_SIMDLEN:
14455 case OMP_CLAUSE_NUM_TASKS:
14456 case OMP_CLAUSE_GRAINSIZE:
14457 case OMP_CLAUSE_PRIORITY:
14458 case OMP_CLAUSE_ORDERED:
14459 case OMP_CLAUSE_HINT:
14460 case OMP_CLAUSE_NUM_GANGS:
14461 case OMP_CLAUSE_NUM_WORKERS:
14462 case OMP_CLAUSE_VECTOR_LENGTH:
14463 case OMP_CLAUSE_WORKER:
14464 case OMP_CLAUSE_VECTOR:
14465 case OMP_CLAUSE_ASYNC:
14466 case OMP_CLAUSE_WAIT:
14467 OMP_CLAUSE_OPERAND (nc, 0)
14468 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14469 in_decl, /*integral_constant_expression_p=*/false);
14470 break;
14471 case OMP_CLAUSE_REDUCTION:
14472 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14473 {
14474 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14475 if (TREE_CODE (placeholder) == SCOPE_REF)
14476 {
14477 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14478 complain, in_decl);
14479 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14480 = build_qualified_name (NULL_TREE, scope,
14481 TREE_OPERAND (placeholder, 1),
14482 false);
14483 }
14484 else
14485 gcc_assert (identifier_p (placeholder));
14486 }
14487 OMP_CLAUSE_DECL (nc)
14488 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14489 in_decl);
14490 break;
14491 case OMP_CLAUSE_GANG:
14492 case OMP_CLAUSE_ALIGNED:
14493 OMP_CLAUSE_DECL (nc)
14494 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14495 in_decl);
14496 OMP_CLAUSE_OPERAND (nc, 1)
14497 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14498 in_decl, /*integral_constant_expression_p=*/false);
14499 break;
14500 case OMP_CLAUSE_LINEAR:
14501 OMP_CLAUSE_DECL (nc)
14502 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14503 in_decl);
14504 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
14505 {
14506 gcc_assert (!linear_no_step);
14507 linear_no_step = nc;
14508 }
14509 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
14510 OMP_CLAUSE_LINEAR_STEP (nc)
14511 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
14512 complain, in_decl);
14513 else
14514 OMP_CLAUSE_LINEAR_STEP (nc)
14515 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
14516 in_decl,
14517 /*integral_constant_expression_p=*/false);
14518 break;
14519 case OMP_CLAUSE_NOWAIT:
14520 case OMP_CLAUSE_DEFAULT:
14521 case OMP_CLAUSE_UNTIED:
14522 case OMP_CLAUSE_MERGEABLE:
14523 case OMP_CLAUSE_INBRANCH:
14524 case OMP_CLAUSE_NOTINBRANCH:
14525 case OMP_CLAUSE_PROC_BIND:
14526 case OMP_CLAUSE_FOR:
14527 case OMP_CLAUSE_PARALLEL:
14528 case OMP_CLAUSE_SECTIONS:
14529 case OMP_CLAUSE_TASKGROUP:
14530 case OMP_CLAUSE_NOGROUP:
14531 case OMP_CLAUSE_THREADS:
14532 case OMP_CLAUSE_SIMD:
14533 case OMP_CLAUSE_DEFAULTMAP:
14534 case OMP_CLAUSE_INDEPENDENT:
14535 case OMP_CLAUSE_AUTO:
14536 case OMP_CLAUSE_SEQ:
14537 break;
14538 case OMP_CLAUSE_TILE:
14539 {
14540 tree lnc, loc;
14541 for (lnc = OMP_CLAUSE_TILE_LIST (nc),
14542 loc = OMP_CLAUSE_TILE_LIST (oc);
14543 loc;
14544 loc = TREE_CHAIN (loc), lnc = TREE_CHAIN (lnc))
14545 {
14546 TREE_VALUE (lnc) = tsubst_expr (TREE_VALUE (loc), args,
14547 complain, in_decl, false);
14548 }
14549 }
14550 break;
14551 default:
14552 gcc_unreachable ();
14553 }
14554 if (allow_fields)
14555 switch (OMP_CLAUSE_CODE (nc))
14556 {
14557 case OMP_CLAUSE_SHARED:
14558 case OMP_CLAUSE_PRIVATE:
14559 case OMP_CLAUSE_FIRSTPRIVATE:
14560 case OMP_CLAUSE_LASTPRIVATE:
14561 case OMP_CLAUSE_COPYPRIVATE:
14562 case OMP_CLAUSE_LINEAR:
14563 case OMP_CLAUSE_REDUCTION:
14564 case OMP_CLAUSE_USE_DEVICE_PTR:
14565 case OMP_CLAUSE_IS_DEVICE_PTR:
14566 /* tsubst_expr on SCOPE_REF results in returning
14567 finish_non_static_data_member result. Undo that here. */
14568 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
14569 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
14570 == IDENTIFIER_NODE))
14571 {
14572 tree t = OMP_CLAUSE_DECL (nc);
14573 tree v = t;
14574 while (v)
14575 switch (TREE_CODE (v))
14576 {
14577 case COMPONENT_REF:
14578 case MEM_REF:
14579 case INDIRECT_REF:
14580 CASE_CONVERT:
14581 case POINTER_PLUS_EXPR:
14582 v = TREE_OPERAND (v, 0);
14583 continue;
14584 case PARM_DECL:
14585 if (DECL_CONTEXT (v) == current_function_decl
14586 && DECL_ARTIFICIAL (v)
14587 && DECL_NAME (v) == this_identifier)
14588 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
14589 /* FALLTHRU */
14590 default:
14591 v = NULL_TREE;
14592 break;
14593 }
14594 }
14595 else if (VAR_P (OMP_CLAUSE_DECL (oc))
14596 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
14597 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
14598 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
14599 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
14600 {
14601 tree decl = OMP_CLAUSE_DECL (nc);
14602 if (VAR_P (decl))
14603 {
14604 if (!DECL_LANG_SPECIFIC (decl))
14605 retrofit_lang_decl (decl);
14606 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
14607 }
14608 }
14609 break;
14610 default:
14611 break;
14612 }
14613 }
14614
14615 new_clauses = nreverse (new_clauses);
14616 if (!declare_simd)
14617 {
14618 new_clauses = finish_omp_clauses (new_clauses, allow_fields);
14619 if (linear_no_step)
14620 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
14621 if (nc == linear_no_step)
14622 {
14623 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
14624 break;
14625 }
14626 }
14627 return new_clauses;
14628 }
14629
14630 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14631
14632 static tree
14633 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14634 tree in_decl)
14635 {
14636 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14637
14638 tree purpose, value, chain;
14639
14640 if (t == NULL)
14641 return t;
14642
14643 if (TREE_CODE (t) != TREE_LIST)
14644 return tsubst_copy_and_build (t, args, complain, in_decl,
14645 /*function_p=*/false,
14646 /*integral_constant_expression_p=*/false);
14647
14648 if (t == void_list_node)
14649 return t;
14650
14651 purpose = TREE_PURPOSE (t);
14652 if (purpose)
14653 purpose = RECUR (purpose);
14654 value = TREE_VALUE (t);
14655 if (value)
14656 {
14657 if (TREE_CODE (value) != LABEL_DECL)
14658 value = RECUR (value);
14659 else
14660 {
14661 value = lookup_label (DECL_NAME (value));
14662 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14663 TREE_USED (value) = 1;
14664 }
14665 }
14666 chain = TREE_CHAIN (t);
14667 if (chain && chain != void_type_node)
14668 chain = RECUR (chain);
14669 return tree_cons (purpose, value, chain);
14670 #undef RECUR
14671 }
14672
14673 /* Used to temporarily communicate the list of #pragma omp parallel
14674 clauses to #pragma omp for instantiation if they are combined
14675 together. */
14676
14677 static tree *omp_parallel_combined_clauses;
14678
14679 /* Substitute one OMP_FOR iterator. */
14680
14681 static void
14682 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
14683 tree initv, tree condv, tree incrv, tree *clauses,
14684 tree args, tsubst_flags_t complain, tree in_decl,
14685 bool integral_constant_expression_p)
14686 {
14687 #define RECUR(NODE) \
14688 tsubst_expr ((NODE), args, complain, in_decl, \
14689 integral_constant_expression_p)
14690 tree decl, init, cond, incr;
14691
14692 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
14693 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
14694
14695 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
14696 {
14697 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
14698 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
14699 }
14700
14701 decl = TREE_OPERAND (init, 0);
14702 init = TREE_OPERAND (init, 1);
14703 tree decl_expr = NULL_TREE;
14704 if (init && TREE_CODE (init) == DECL_EXPR)
14705 {
14706 /* We need to jump through some hoops to handle declarations in the
14707 for-init-statement, since we might need to handle auto deduction,
14708 but we need to keep control of initialization. */
14709 decl_expr = init;
14710 init = DECL_INITIAL (DECL_EXPR_DECL (init));
14711 decl = tsubst_decl (decl, args, complain);
14712 }
14713 else
14714 {
14715 if (TREE_CODE (decl) == SCOPE_REF)
14716 {
14717 decl = RECUR (decl);
14718 if (TREE_CODE (decl) == COMPONENT_REF)
14719 {
14720 tree v = decl;
14721 while (v)
14722 switch (TREE_CODE (v))
14723 {
14724 case COMPONENT_REF:
14725 case MEM_REF:
14726 case INDIRECT_REF:
14727 CASE_CONVERT:
14728 case POINTER_PLUS_EXPR:
14729 v = TREE_OPERAND (v, 0);
14730 continue;
14731 case PARM_DECL:
14732 if (DECL_CONTEXT (v) == current_function_decl
14733 && DECL_ARTIFICIAL (v)
14734 && DECL_NAME (v) == this_identifier)
14735 {
14736 decl = TREE_OPERAND (decl, 1);
14737 decl = omp_privatize_field (decl, false);
14738 }
14739 /* FALLTHRU */
14740 default:
14741 v = NULL_TREE;
14742 break;
14743 }
14744 }
14745 }
14746 else
14747 decl = RECUR (decl);
14748 }
14749 init = RECUR (init);
14750
14751 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14752 if (auto_node && init)
14753 TREE_TYPE (decl)
14754 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
14755
14756 gcc_assert (!type_dependent_expression_p (decl));
14757
14758 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14759 {
14760 if (decl_expr)
14761 {
14762 /* Declare the variable, but don't let that initialize it. */
14763 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
14764 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
14765 RECUR (decl_expr);
14766 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
14767 }
14768
14769 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
14770 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14771 if (TREE_CODE (incr) == MODIFY_EXPR)
14772 {
14773 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14774 tree rhs = RECUR (TREE_OPERAND (incr, 1));
14775 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
14776 NOP_EXPR, rhs, complain);
14777 }
14778 else
14779 incr = RECUR (incr);
14780 TREE_VEC_ELT (declv, i) = decl;
14781 TREE_VEC_ELT (initv, i) = init;
14782 TREE_VEC_ELT (condv, i) = cond;
14783 TREE_VEC_ELT (incrv, i) = incr;
14784 return;
14785 }
14786
14787 if (decl_expr)
14788 {
14789 /* Declare and initialize the variable. */
14790 RECUR (decl_expr);
14791 init = NULL_TREE;
14792 }
14793 else if (init)
14794 {
14795 tree *pc;
14796 int j;
14797 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
14798 {
14799 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
14800 {
14801 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
14802 && OMP_CLAUSE_DECL (*pc) == decl)
14803 break;
14804 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
14805 && OMP_CLAUSE_DECL (*pc) == decl)
14806 {
14807 if (j)
14808 break;
14809 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14810 tree c = *pc;
14811 *pc = OMP_CLAUSE_CHAIN (c);
14812 OMP_CLAUSE_CHAIN (c) = *clauses;
14813 *clauses = c;
14814 }
14815 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
14816 && OMP_CLAUSE_DECL (*pc) == decl)
14817 {
14818 error ("iteration variable %qD should not be firstprivate",
14819 decl);
14820 *pc = OMP_CLAUSE_CHAIN (*pc);
14821 }
14822 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
14823 && OMP_CLAUSE_DECL (*pc) == decl)
14824 {
14825 error ("iteration variable %qD should not be reduction",
14826 decl);
14827 *pc = OMP_CLAUSE_CHAIN (*pc);
14828 }
14829 else
14830 pc = &OMP_CLAUSE_CHAIN (*pc);
14831 }
14832 if (*pc)
14833 break;
14834 }
14835 if (*pc == NULL_TREE)
14836 {
14837 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14838 OMP_CLAUSE_DECL (c) = decl;
14839 c = finish_omp_clauses (c, true);
14840 if (c)
14841 {
14842 OMP_CLAUSE_CHAIN (c) = *clauses;
14843 *clauses = c;
14844 }
14845 }
14846 }
14847 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
14848 if (COMPARISON_CLASS_P (cond))
14849 {
14850 tree op0 = RECUR (TREE_OPERAND (cond, 0));
14851 tree op1 = RECUR (TREE_OPERAND (cond, 1));
14852 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
14853 }
14854 else
14855 cond = RECUR (cond);
14856 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14857 switch (TREE_CODE (incr))
14858 {
14859 case PREINCREMENT_EXPR:
14860 case PREDECREMENT_EXPR:
14861 case POSTINCREMENT_EXPR:
14862 case POSTDECREMENT_EXPR:
14863 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
14864 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
14865 break;
14866 case MODIFY_EXPR:
14867 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14868 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14869 {
14870 tree rhs = TREE_OPERAND (incr, 1);
14871 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14872 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14873 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14874 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14875 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14876 rhs0, rhs1));
14877 }
14878 else
14879 incr = RECUR (incr);
14880 break;
14881 case MODOP_EXPR:
14882 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14883 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14884 {
14885 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14886 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14887 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
14888 TREE_TYPE (decl), lhs,
14889 RECUR (TREE_OPERAND (incr, 2))));
14890 }
14891 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
14892 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
14893 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
14894 {
14895 tree rhs = TREE_OPERAND (incr, 2);
14896 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14897 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14898 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14899 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14900 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14901 rhs0, rhs1));
14902 }
14903 else
14904 incr = RECUR (incr);
14905 break;
14906 default:
14907 incr = RECUR (incr);
14908 break;
14909 }
14910
14911 TREE_VEC_ELT (declv, i) = decl;
14912 TREE_VEC_ELT (initv, i) = init;
14913 TREE_VEC_ELT (condv, i) = cond;
14914 TREE_VEC_ELT (incrv, i) = incr;
14915 #undef RECUR
14916 }
14917
14918 /* Helper function of tsubst_expr, find OMP_TEAMS inside
14919 of OMP_TARGET's body. */
14920
14921 static tree
14922 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
14923 {
14924 *walk_subtrees = 0;
14925 switch (TREE_CODE (*tp))
14926 {
14927 case OMP_TEAMS:
14928 return *tp;
14929 case BIND_EXPR:
14930 case STATEMENT_LIST:
14931 *walk_subtrees = 1;
14932 break;
14933 default:
14934 break;
14935 }
14936 return NULL_TREE;
14937 }
14938
14939 /* Like tsubst_copy for expressions, etc. but also does semantic
14940 processing. */
14941
14942 tree
14943 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
14944 bool integral_constant_expression_p)
14945 {
14946 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14947 #define RECUR(NODE) \
14948 tsubst_expr ((NODE), args, complain, in_decl, \
14949 integral_constant_expression_p)
14950
14951 tree stmt, tmp;
14952 tree r;
14953 location_t loc;
14954
14955 if (t == NULL_TREE || t == error_mark_node)
14956 return t;
14957
14958 loc = input_location;
14959 if (EXPR_HAS_LOCATION (t))
14960 input_location = EXPR_LOCATION (t);
14961 if (STATEMENT_CODE_P (TREE_CODE (t)))
14962 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
14963
14964 switch (TREE_CODE (t))
14965 {
14966 case STATEMENT_LIST:
14967 {
14968 tree_stmt_iterator i;
14969 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
14970 RECUR (tsi_stmt (i));
14971 break;
14972 }
14973
14974 case CTOR_INITIALIZER:
14975 finish_mem_initializers (tsubst_initializer_list
14976 (TREE_OPERAND (t, 0), args));
14977 break;
14978
14979 case RETURN_EXPR:
14980 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
14981 break;
14982
14983 case EXPR_STMT:
14984 tmp = RECUR (EXPR_STMT_EXPR (t));
14985 if (EXPR_STMT_STMT_EXPR_RESULT (t))
14986 finish_stmt_expr_expr (tmp, cur_stmt_expr);
14987 else
14988 finish_expr_stmt (tmp);
14989 break;
14990
14991 case USING_STMT:
14992 do_using_directive (USING_STMT_NAMESPACE (t));
14993 break;
14994
14995 case DECL_EXPR:
14996 {
14997 tree decl, pattern_decl;
14998 tree init;
14999
15000 pattern_decl = decl = DECL_EXPR_DECL (t);
15001 if (TREE_CODE (decl) == LABEL_DECL)
15002 finish_label_decl (DECL_NAME (decl));
15003 else if (TREE_CODE (decl) == USING_DECL)
15004 {
15005 tree scope = USING_DECL_SCOPE (decl);
15006 tree name = DECL_NAME (decl);
15007 tree decl;
15008
15009 scope = tsubst (scope, args, complain, in_decl);
15010 decl = lookup_qualified_name (scope, name,
15011 /*is_type_p=*/false,
15012 /*complain=*/false);
15013 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15014 qualified_name_lookup_error (scope, name, decl, input_location);
15015 else
15016 do_local_using_decl (decl, scope, name);
15017 }
15018 else if (DECL_PACK_P (decl))
15019 {
15020 /* Don't build up decls for a variadic capture proxy, we'll
15021 instantiate the elements directly as needed. */
15022 break;
15023 }
15024 else
15025 {
15026 init = DECL_INITIAL (decl);
15027 decl = tsubst (decl, args, complain, in_decl);
15028 if (decl != error_mark_node)
15029 {
15030 /* By marking the declaration as instantiated, we avoid
15031 trying to instantiate it. Since instantiate_decl can't
15032 handle local variables, and since we've already done
15033 all that needs to be done, that's the right thing to
15034 do. */
15035 if (VAR_P (decl))
15036 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15037 if (VAR_P (decl)
15038 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15039 /* Anonymous aggregates are a special case. */
15040 finish_anon_union (decl);
15041 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15042 {
15043 DECL_CONTEXT (decl) = current_function_decl;
15044 if (DECL_NAME (decl) == this_identifier)
15045 {
15046 tree lam = DECL_CONTEXT (current_function_decl);
15047 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15048 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15049 }
15050 insert_capture_proxy (decl);
15051 }
15052 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15053 /* We already did a pushtag. */;
15054 else if (TREE_CODE (decl) == FUNCTION_DECL
15055 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15056 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15057 {
15058 DECL_CONTEXT (decl) = NULL_TREE;
15059 pushdecl (decl);
15060 DECL_CONTEXT (decl) = current_function_decl;
15061 cp_check_omp_declare_reduction (decl);
15062 }
15063 else
15064 {
15065 int const_init = false;
15066 maybe_push_decl (decl);
15067 if (VAR_P (decl)
15068 && DECL_PRETTY_FUNCTION_P (decl))
15069 {
15070 /* For __PRETTY_FUNCTION__ we have to adjust the
15071 initializer. */
15072 const char *const name
15073 = cxx_printable_name (current_function_decl, 2);
15074 init = cp_fname_init (name, &TREE_TYPE (decl));
15075 }
15076 else
15077 init = tsubst_init (init, decl, args, complain, in_decl);
15078
15079 if (VAR_P (decl))
15080 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15081 (pattern_decl));
15082 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15083 }
15084 }
15085 }
15086
15087 break;
15088 }
15089
15090 case FOR_STMT:
15091 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15092 RECUR (FOR_INIT_STMT (t));
15093 finish_for_init_stmt (stmt);
15094 tmp = RECUR (FOR_COND (t));
15095 finish_for_cond (tmp, stmt, false);
15096 tmp = RECUR (FOR_EXPR (t));
15097 finish_for_expr (tmp, stmt);
15098 RECUR (FOR_BODY (t));
15099 finish_for_stmt (stmt);
15100 break;
15101
15102 case RANGE_FOR_STMT:
15103 {
15104 tree decl, expr;
15105 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15106 decl = RANGE_FOR_DECL (t);
15107 decl = tsubst (decl, args, complain, in_decl);
15108 maybe_push_decl (decl);
15109 expr = RECUR (RANGE_FOR_EXPR (t));
15110 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
15111 RECUR (RANGE_FOR_BODY (t));
15112 finish_for_stmt (stmt);
15113 }
15114 break;
15115
15116 case WHILE_STMT:
15117 stmt = begin_while_stmt ();
15118 tmp = RECUR (WHILE_COND (t));
15119 finish_while_stmt_cond (tmp, stmt, false);
15120 RECUR (WHILE_BODY (t));
15121 finish_while_stmt (stmt);
15122 break;
15123
15124 case DO_STMT:
15125 stmt = begin_do_stmt ();
15126 RECUR (DO_BODY (t));
15127 finish_do_body (stmt);
15128 tmp = RECUR (DO_COND (t));
15129 finish_do_stmt (tmp, stmt, false);
15130 break;
15131
15132 case IF_STMT:
15133 stmt = begin_if_stmt ();
15134 tmp = RECUR (IF_COND (t));
15135 finish_if_stmt_cond (tmp, stmt);
15136 RECUR (THEN_CLAUSE (t));
15137 finish_then_clause (stmt);
15138
15139 if (ELSE_CLAUSE (t))
15140 {
15141 begin_else_clause (stmt);
15142 RECUR (ELSE_CLAUSE (t));
15143 finish_else_clause (stmt);
15144 }
15145
15146 finish_if_stmt (stmt);
15147 break;
15148
15149 case BIND_EXPR:
15150 if (BIND_EXPR_BODY_BLOCK (t))
15151 stmt = begin_function_body ();
15152 else
15153 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15154 ? BCS_TRY_BLOCK : 0);
15155
15156 RECUR (BIND_EXPR_BODY (t));
15157
15158 if (BIND_EXPR_BODY_BLOCK (t))
15159 finish_function_body (stmt);
15160 else
15161 finish_compound_stmt (stmt);
15162 break;
15163
15164 case BREAK_STMT:
15165 finish_break_stmt ();
15166 break;
15167
15168 case CONTINUE_STMT:
15169 finish_continue_stmt ();
15170 break;
15171
15172 case SWITCH_STMT:
15173 stmt = begin_switch_stmt ();
15174 tmp = RECUR (SWITCH_STMT_COND (t));
15175 finish_switch_cond (tmp, stmt);
15176 RECUR (SWITCH_STMT_BODY (t));
15177 finish_switch_stmt (stmt);
15178 break;
15179
15180 case CASE_LABEL_EXPR:
15181 {
15182 tree low = RECUR (CASE_LOW (t));
15183 tree high = RECUR (CASE_HIGH (t));
15184 finish_case_label (EXPR_LOCATION (t), low, high);
15185 }
15186 break;
15187
15188 case LABEL_EXPR:
15189 {
15190 tree decl = LABEL_EXPR_LABEL (t);
15191 tree label;
15192
15193 label = finish_label_stmt (DECL_NAME (decl));
15194 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15195 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15196 }
15197 break;
15198
15199 case GOTO_EXPR:
15200 tmp = GOTO_DESTINATION (t);
15201 if (TREE_CODE (tmp) != LABEL_DECL)
15202 /* Computed goto's must be tsubst'd into. On the other hand,
15203 non-computed gotos must not be; the identifier in question
15204 will have no binding. */
15205 tmp = RECUR (tmp);
15206 else
15207 tmp = DECL_NAME (tmp);
15208 finish_goto_stmt (tmp);
15209 break;
15210
15211 case ASM_EXPR:
15212 {
15213 tree string = RECUR (ASM_STRING (t));
15214 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15215 complain, in_decl);
15216 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15217 complain, in_decl);
15218 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15219 complain, in_decl);
15220 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15221 complain, in_decl);
15222 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15223 clobbers, labels);
15224 tree asm_expr = tmp;
15225 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15226 asm_expr = TREE_OPERAND (asm_expr, 0);
15227 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15228 }
15229 break;
15230
15231 case TRY_BLOCK:
15232 if (CLEANUP_P (t))
15233 {
15234 stmt = begin_try_block ();
15235 RECUR (TRY_STMTS (t));
15236 finish_cleanup_try_block (stmt);
15237 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15238 }
15239 else
15240 {
15241 tree compound_stmt = NULL_TREE;
15242
15243 if (FN_TRY_BLOCK_P (t))
15244 stmt = begin_function_try_block (&compound_stmt);
15245 else
15246 stmt = begin_try_block ();
15247
15248 RECUR (TRY_STMTS (t));
15249
15250 if (FN_TRY_BLOCK_P (t))
15251 finish_function_try_block (stmt);
15252 else
15253 finish_try_block (stmt);
15254
15255 RECUR (TRY_HANDLERS (t));
15256 if (FN_TRY_BLOCK_P (t))
15257 finish_function_handler_sequence (stmt, compound_stmt);
15258 else
15259 finish_handler_sequence (stmt);
15260 }
15261 break;
15262
15263 case HANDLER:
15264 {
15265 tree decl = HANDLER_PARMS (t);
15266
15267 if (decl)
15268 {
15269 decl = tsubst (decl, args, complain, in_decl);
15270 /* Prevent instantiate_decl from trying to instantiate
15271 this variable. We've already done all that needs to be
15272 done. */
15273 if (decl != error_mark_node)
15274 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15275 }
15276 stmt = begin_handler ();
15277 finish_handler_parms (decl, stmt);
15278 RECUR (HANDLER_BODY (t));
15279 finish_handler (stmt);
15280 }
15281 break;
15282
15283 case TAG_DEFN:
15284 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
15285 if (CLASS_TYPE_P (tmp))
15286 {
15287 /* Local classes are not independent templates; they are
15288 instantiated along with their containing function. And this
15289 way we don't have to deal with pushing out of one local class
15290 to instantiate a member of another local class. */
15291 tree fn;
15292 /* Closures are handled by the LAMBDA_EXPR. */
15293 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
15294 complete_type (tmp);
15295 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
15296 if (!DECL_ARTIFICIAL (fn))
15297 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
15298 }
15299 break;
15300
15301 case STATIC_ASSERT:
15302 {
15303 tree condition;
15304
15305 ++c_inhibit_evaluation_warnings;
15306 condition =
15307 tsubst_expr (STATIC_ASSERT_CONDITION (t),
15308 args,
15309 complain, in_decl,
15310 /*integral_constant_expression_p=*/true);
15311 --c_inhibit_evaluation_warnings;
15312
15313 finish_static_assert (condition,
15314 STATIC_ASSERT_MESSAGE (t),
15315 STATIC_ASSERT_SOURCE_LOCATION (t),
15316 /*member_p=*/false);
15317 }
15318 break;
15319
15320 case OACC_KERNELS:
15321 case OACC_PARALLEL:
15322 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, false, args, complain,
15323 in_decl);
15324 stmt = begin_omp_parallel ();
15325 RECUR (OMP_BODY (t));
15326 finish_omp_construct (TREE_CODE (t), stmt, tmp);
15327 break;
15328
15329 case OMP_PARALLEL:
15330 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
15331 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false, true,
15332 args, complain, in_decl);
15333 if (OMP_PARALLEL_COMBINED (t))
15334 omp_parallel_combined_clauses = &tmp;
15335 stmt = begin_omp_parallel ();
15336 RECUR (OMP_PARALLEL_BODY (t));
15337 gcc_assert (omp_parallel_combined_clauses == NULL);
15338 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15339 = OMP_PARALLEL_COMBINED (t);
15340 pop_omp_privatization_clauses (r);
15341 break;
15342
15343 case OMP_TASK:
15344 r = push_omp_privatization_clauses (false);
15345 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false, true,
15346 args, complain, in_decl);
15347 stmt = begin_omp_task ();
15348 RECUR (OMP_TASK_BODY (t));
15349 finish_omp_task (tmp, stmt);
15350 pop_omp_privatization_clauses (r);
15351 break;
15352
15353 case OMP_FOR:
15354 case OMP_SIMD:
15355 case CILK_SIMD:
15356 case CILK_FOR:
15357 case OMP_DISTRIBUTE:
15358 case OMP_TASKLOOP:
15359 case OACC_LOOP:
15360 {
15361 tree clauses, body, pre_body;
15362 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15363 tree orig_declv = NULL_TREE;
15364 tree incrv = NULL_TREE;
15365 int i;
15366
15367 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
15368 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
15369 TREE_CODE (t) != OACC_LOOP,
15370 args, complain, in_decl);
15371 if (OMP_FOR_INIT (t) != NULL_TREE)
15372 {
15373 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15374 if (OMP_FOR_ORIG_DECLS (t))
15375 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15376 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15377 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15378 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15379 }
15380
15381 stmt = begin_omp_structured_block ();
15382
15383 pre_body = push_stmt_list ();
15384 RECUR (OMP_FOR_PRE_BODY (t));
15385 pre_body = pop_stmt_list (pre_body);
15386
15387 if (OMP_FOR_INIT (t) != NULL_TREE)
15388 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15389 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
15390 incrv, &clauses, args, complain, in_decl,
15391 integral_constant_expression_p);
15392 omp_parallel_combined_clauses = NULL;
15393
15394 body = push_stmt_list ();
15395 RECUR (OMP_FOR_BODY (t));
15396 body = pop_stmt_list (body);
15397
15398 if (OMP_FOR_INIT (t) != NULL_TREE)
15399 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
15400 orig_declv, initv, condv, incrv, body, pre_body,
15401 NULL, clauses);
15402 else
15403 {
15404 t = make_node (TREE_CODE (t));
15405 TREE_TYPE (t) = void_type_node;
15406 OMP_FOR_BODY (t) = body;
15407 OMP_FOR_PRE_BODY (t) = pre_body;
15408 OMP_FOR_CLAUSES (t) = clauses;
15409 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15410 add_stmt (t);
15411 }
15412
15413 add_stmt (finish_omp_structured_block (stmt));
15414 pop_omp_privatization_clauses (r);
15415 }
15416 break;
15417
15418 case OMP_SECTIONS:
15419 omp_parallel_combined_clauses = NULL;
15420 /* FALLTHRU */
15421 case OMP_SINGLE:
15422 case OMP_TEAMS:
15423 case OMP_CRITICAL:
15424 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
15425 && OMP_TEAMS_COMBINED (t));
15426 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, true,
15427 args, complain, in_decl);
15428 stmt = push_stmt_list ();
15429 RECUR (OMP_BODY (t));
15430 stmt = pop_stmt_list (stmt);
15431
15432 t = copy_node (t);
15433 OMP_BODY (t) = stmt;
15434 OMP_CLAUSES (t) = tmp;
15435 add_stmt (t);
15436 pop_omp_privatization_clauses (r);
15437 break;
15438
15439 case OACC_DATA:
15440 case OMP_TARGET_DATA:
15441 case OMP_TARGET:
15442 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
15443 TREE_CODE (t) != OACC_DATA,
15444 args, complain, in_decl);
15445 keep_next_level (true);
15446 stmt = begin_omp_structured_block ();
15447
15448 RECUR (OMP_BODY (t));
15449 stmt = finish_omp_structured_block (stmt);
15450
15451 t = copy_node (t);
15452 OMP_BODY (t) = stmt;
15453 OMP_CLAUSES (t) = tmp;
15454 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
15455 {
15456 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
15457 if (teams)
15458 {
15459 /* For combined target teams, ensure the num_teams and
15460 thread_limit clause expressions are evaluated on the host,
15461 before entering the target construct. */
15462 tree c;
15463 for (c = OMP_TEAMS_CLAUSES (teams);
15464 c; c = OMP_CLAUSE_CHAIN (c))
15465 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15466 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15467 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15468 {
15469 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15470 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
15471 if (expr == error_mark_node)
15472 continue;
15473 tmp = TARGET_EXPR_SLOT (expr);
15474 add_stmt (expr);
15475 OMP_CLAUSE_OPERAND (c, 0) = expr;
15476 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15477 OMP_CLAUSE_FIRSTPRIVATE);
15478 OMP_CLAUSE_DECL (tc) = tmp;
15479 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
15480 OMP_TARGET_CLAUSES (t) = tc;
15481 }
15482 }
15483 }
15484 add_stmt (t);
15485 break;
15486
15487 case OACC_DECLARE:
15488 t = copy_node (t);
15489 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), false, false,
15490 args, complain, in_decl);
15491 OACC_DECLARE_CLAUSES (t) = tmp;
15492 add_stmt (t);
15493 break;
15494
15495 case OMP_TARGET_UPDATE:
15496 case OMP_TARGET_ENTER_DATA:
15497 case OMP_TARGET_EXIT_DATA:
15498 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, true,
15499 args, complain, in_decl);
15500 t = copy_node (t);
15501 OMP_STANDALONE_CLAUSES (t) = tmp;
15502 add_stmt (t);
15503 break;
15504
15505 case OACC_ENTER_DATA:
15506 case OACC_EXIT_DATA:
15507 case OACC_UPDATE:
15508 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, false,
15509 args, complain, in_decl);
15510 t = copy_node (t);
15511 OMP_STANDALONE_CLAUSES (t) = tmp;
15512 add_stmt (t);
15513 break;
15514
15515 case OMP_ORDERED:
15516 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), false, true,
15517 args, complain, in_decl);
15518 stmt = push_stmt_list ();
15519 RECUR (OMP_BODY (t));
15520 stmt = pop_stmt_list (stmt);
15521
15522 t = copy_node (t);
15523 OMP_BODY (t) = stmt;
15524 OMP_ORDERED_CLAUSES (t) = tmp;
15525 add_stmt (t);
15526 break;
15527
15528 case OMP_SECTION:
15529 case OMP_MASTER:
15530 case OMP_TASKGROUP:
15531 stmt = push_stmt_list ();
15532 RECUR (OMP_BODY (t));
15533 stmt = pop_stmt_list (stmt);
15534
15535 t = copy_node (t);
15536 OMP_BODY (t) = stmt;
15537 add_stmt (t);
15538 break;
15539
15540 case OMP_ATOMIC:
15541 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
15542 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
15543 {
15544 tree op1 = TREE_OPERAND (t, 1);
15545 tree rhs1 = NULL_TREE;
15546 tree lhs, rhs;
15547 if (TREE_CODE (op1) == COMPOUND_EXPR)
15548 {
15549 rhs1 = RECUR (TREE_OPERAND (op1, 0));
15550 op1 = TREE_OPERAND (op1, 1);
15551 }
15552 lhs = RECUR (TREE_OPERAND (op1, 0));
15553 rhs = RECUR (TREE_OPERAND (op1, 1));
15554 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
15555 NULL_TREE, NULL_TREE, rhs1,
15556 OMP_ATOMIC_SEQ_CST (t));
15557 }
15558 else
15559 {
15560 tree op1 = TREE_OPERAND (t, 1);
15561 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
15562 tree rhs1 = NULL_TREE;
15563 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
15564 enum tree_code opcode = NOP_EXPR;
15565 if (code == OMP_ATOMIC_READ)
15566 {
15567 v = RECUR (TREE_OPERAND (op1, 0));
15568 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15569 }
15570 else if (code == OMP_ATOMIC_CAPTURE_OLD
15571 || code == OMP_ATOMIC_CAPTURE_NEW)
15572 {
15573 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
15574 v = RECUR (TREE_OPERAND (op1, 0));
15575 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15576 if (TREE_CODE (op11) == COMPOUND_EXPR)
15577 {
15578 rhs1 = RECUR (TREE_OPERAND (op11, 0));
15579 op11 = TREE_OPERAND (op11, 1);
15580 }
15581 lhs = RECUR (TREE_OPERAND (op11, 0));
15582 rhs = RECUR (TREE_OPERAND (op11, 1));
15583 opcode = TREE_CODE (op11);
15584 if (opcode == MODIFY_EXPR)
15585 opcode = NOP_EXPR;
15586 }
15587 else
15588 {
15589 code = OMP_ATOMIC;
15590 lhs = RECUR (TREE_OPERAND (op1, 0));
15591 rhs = RECUR (TREE_OPERAND (op1, 1));
15592 }
15593 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
15594 OMP_ATOMIC_SEQ_CST (t));
15595 }
15596 break;
15597
15598 case TRANSACTION_EXPR:
15599 {
15600 int flags = 0;
15601 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
15602 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
15603
15604 if (TRANSACTION_EXPR_IS_STMT (t))
15605 {
15606 tree body = TRANSACTION_EXPR_BODY (t);
15607 tree noex = NULL_TREE;
15608 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
15609 {
15610 noex = MUST_NOT_THROW_COND (body);
15611 if (noex == NULL_TREE)
15612 noex = boolean_true_node;
15613 body = TREE_OPERAND (body, 0);
15614 }
15615 stmt = begin_transaction_stmt (input_location, NULL, flags);
15616 RECUR (body);
15617 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
15618 }
15619 else
15620 {
15621 stmt = build_transaction_expr (EXPR_LOCATION (t),
15622 RECUR (TRANSACTION_EXPR_BODY (t)),
15623 flags, NULL_TREE);
15624 RETURN (stmt);
15625 }
15626 }
15627 break;
15628
15629 case MUST_NOT_THROW_EXPR:
15630 {
15631 tree op0 = RECUR (TREE_OPERAND (t, 0));
15632 tree cond = RECUR (MUST_NOT_THROW_COND (t));
15633 RETURN (build_must_not_throw_expr (op0, cond));
15634 }
15635
15636 case EXPR_PACK_EXPANSION:
15637 error ("invalid use of pack expansion expression");
15638 RETURN (error_mark_node);
15639
15640 case NONTYPE_ARGUMENT_PACK:
15641 error ("use %<...%> to expand argument pack");
15642 RETURN (error_mark_node);
15643
15644 case CILK_SPAWN_STMT:
15645 cfun->calls_cilk_spawn = 1;
15646 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
15647
15648 case CILK_SYNC_STMT:
15649 RETURN (build_cilk_sync ());
15650
15651 case COMPOUND_EXPR:
15652 tmp = RECUR (TREE_OPERAND (t, 0));
15653 if (tmp == NULL_TREE)
15654 /* If the first operand was a statement, we're done with it. */
15655 RETURN (RECUR (TREE_OPERAND (t, 1)));
15656 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
15657 RECUR (TREE_OPERAND (t, 1)),
15658 complain));
15659
15660 case ANNOTATE_EXPR:
15661 tmp = RECUR (TREE_OPERAND (t, 0));
15662 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
15663 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
15664
15665 default:
15666 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
15667
15668 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
15669 /*function_p=*/false,
15670 integral_constant_expression_p));
15671 }
15672
15673 RETURN (NULL_TREE);
15674 out:
15675 input_location = loc;
15676 return r;
15677 #undef RECUR
15678 #undef RETURN
15679 }
15680
15681 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15682 function. For description of the body see comment above
15683 cp_parser_omp_declare_reduction_exprs. */
15684
15685 static void
15686 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15687 {
15688 if (t == NULL_TREE || t == error_mark_node)
15689 return;
15690
15691 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
15692
15693 tree_stmt_iterator tsi;
15694 int i;
15695 tree stmts[7];
15696 memset (stmts, 0, sizeof stmts);
15697 for (i = 0, tsi = tsi_start (t);
15698 i < 7 && !tsi_end_p (tsi);
15699 i++, tsi_next (&tsi))
15700 stmts[i] = tsi_stmt (tsi);
15701 gcc_assert (tsi_end_p (tsi));
15702
15703 if (i >= 3)
15704 {
15705 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
15706 && TREE_CODE (stmts[1]) == DECL_EXPR);
15707 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
15708 args, complain, in_decl);
15709 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
15710 args, complain, in_decl);
15711 DECL_CONTEXT (omp_out) = current_function_decl;
15712 DECL_CONTEXT (omp_in) = current_function_decl;
15713 keep_next_level (true);
15714 tree block = begin_omp_structured_block ();
15715 tsubst_expr (stmts[2], args, complain, in_decl, false);
15716 block = finish_omp_structured_block (block);
15717 block = maybe_cleanup_point_expr_void (block);
15718 add_decl_expr (omp_out);
15719 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
15720 TREE_NO_WARNING (omp_out) = 1;
15721 add_decl_expr (omp_in);
15722 finish_expr_stmt (block);
15723 }
15724 if (i >= 6)
15725 {
15726 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
15727 && TREE_CODE (stmts[4]) == DECL_EXPR);
15728 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
15729 args, complain, in_decl);
15730 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
15731 args, complain, in_decl);
15732 DECL_CONTEXT (omp_priv) = current_function_decl;
15733 DECL_CONTEXT (omp_orig) = current_function_decl;
15734 keep_next_level (true);
15735 tree block = begin_omp_structured_block ();
15736 tsubst_expr (stmts[5], args, complain, in_decl, false);
15737 block = finish_omp_structured_block (block);
15738 block = maybe_cleanup_point_expr_void (block);
15739 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
15740 add_decl_expr (omp_priv);
15741 add_decl_expr (omp_orig);
15742 finish_expr_stmt (block);
15743 if (i == 7)
15744 add_decl_expr (omp_orig);
15745 }
15746 }
15747
15748 /* T is a postfix-expression that is not being used in a function
15749 call. Return the substituted version of T. */
15750
15751 static tree
15752 tsubst_non_call_postfix_expression (tree t, tree args,
15753 tsubst_flags_t complain,
15754 tree in_decl)
15755 {
15756 if (TREE_CODE (t) == SCOPE_REF)
15757 t = tsubst_qualified_id (t, args, complain, in_decl,
15758 /*done=*/false, /*address_p=*/false);
15759 else
15760 t = tsubst_copy_and_build (t, args, complain, in_decl,
15761 /*function_p=*/false,
15762 /*integral_constant_expression_p=*/false);
15763
15764 return t;
15765 }
15766
15767 /* Like tsubst but deals with expressions and performs semantic
15768 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15769
15770 tree
15771 tsubst_copy_and_build (tree t,
15772 tree args,
15773 tsubst_flags_t complain,
15774 tree in_decl,
15775 bool function_p,
15776 bool integral_constant_expression_p)
15777 {
15778 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15779 #define RECUR(NODE) \
15780 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15781 /*function_p=*/false, \
15782 integral_constant_expression_p)
15783
15784 tree retval, op1;
15785 location_t loc;
15786
15787 if (t == NULL_TREE || t == error_mark_node)
15788 return t;
15789
15790 loc = input_location;
15791 if (EXPR_HAS_LOCATION (t))
15792 input_location = EXPR_LOCATION (t);
15793
15794 /* N3276 decltype magic only applies to calls at the top level or on the
15795 right side of a comma. */
15796 tsubst_flags_t decltype_flag = (complain & tf_decltype);
15797 complain &= ~tf_decltype;
15798
15799 switch (TREE_CODE (t))
15800 {
15801 case USING_DECL:
15802 t = DECL_NAME (t);
15803 /* Fall through. */
15804 case IDENTIFIER_NODE:
15805 {
15806 tree decl;
15807 cp_id_kind idk;
15808 bool non_integral_constant_expression_p;
15809 const char *error_msg;
15810
15811 if (IDENTIFIER_TYPENAME_P (t))
15812 {
15813 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15814 t = mangle_conv_op_name_for_type (new_type);
15815 }
15816
15817 /* Look up the name. */
15818 decl = lookup_name (t);
15819
15820 /* By convention, expressions use ERROR_MARK_NODE to indicate
15821 failure, not NULL_TREE. */
15822 if (decl == NULL_TREE)
15823 decl = error_mark_node;
15824
15825 decl = finish_id_expression (t, decl, NULL_TREE,
15826 &idk,
15827 integral_constant_expression_p,
15828 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
15829 &non_integral_constant_expression_p,
15830 /*template_p=*/false,
15831 /*done=*/true,
15832 /*address_p=*/false,
15833 /*template_arg_p=*/false,
15834 &error_msg,
15835 input_location);
15836 if (error_msg)
15837 error (error_msg);
15838 if (!function_p && identifier_p (decl))
15839 {
15840 if (complain & tf_error)
15841 unqualified_name_lookup_error (decl);
15842 decl = error_mark_node;
15843 }
15844 RETURN (decl);
15845 }
15846
15847 case TEMPLATE_ID_EXPR:
15848 {
15849 tree object;
15850 tree templ = RECUR (TREE_OPERAND (t, 0));
15851 tree targs = TREE_OPERAND (t, 1);
15852
15853 if (targs)
15854 targs = tsubst_template_args (targs, args, complain, in_decl);
15855 if (targs == error_mark_node)
15856 return error_mark_node;
15857
15858 if (variable_template_p (templ))
15859 {
15860 templ = lookup_template_variable (templ, targs);
15861 if (!any_dependent_template_arguments_p (targs))
15862 {
15863 templ = finish_template_variable (templ, complain);
15864 mark_used (templ);
15865 }
15866 RETURN (convert_from_reference (templ));
15867 }
15868
15869 if (TREE_CODE (templ) == COMPONENT_REF)
15870 {
15871 object = TREE_OPERAND (templ, 0);
15872 templ = TREE_OPERAND (templ, 1);
15873 }
15874 else
15875 object = NULL_TREE;
15876 templ = lookup_template_function (templ, targs);
15877
15878 if (object)
15879 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
15880 object, templ, NULL_TREE));
15881 else
15882 RETURN (baselink_for_fns (templ));
15883 }
15884
15885 case INDIRECT_REF:
15886 {
15887 tree r = RECUR (TREE_OPERAND (t, 0));
15888
15889 if (REFERENCE_REF_P (t))
15890 {
15891 /* A type conversion to reference type will be enclosed in
15892 such an indirect ref, but the substitution of the cast
15893 will have also added such an indirect ref. */
15894 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
15895 r = convert_from_reference (r);
15896 }
15897 else
15898 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
15899 complain|decltype_flag);
15900 RETURN (r);
15901 }
15902
15903 case NOP_EXPR:
15904 {
15905 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15906 tree op0 = RECUR (TREE_OPERAND (t, 0));
15907 RETURN (build_nop (type, op0));
15908 }
15909
15910 case IMPLICIT_CONV_EXPR:
15911 {
15912 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15913 tree expr = RECUR (TREE_OPERAND (t, 0));
15914 int flags = LOOKUP_IMPLICIT;
15915 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
15916 flags = LOOKUP_NORMAL;
15917 RETURN (perform_implicit_conversion_flags (type, expr, complain,
15918 flags));
15919 }
15920
15921 case CONVERT_EXPR:
15922 {
15923 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15924 tree op0 = RECUR (TREE_OPERAND (t, 0));
15925 RETURN (build1 (CONVERT_EXPR, type, op0));
15926 }
15927
15928 case CAST_EXPR:
15929 case REINTERPRET_CAST_EXPR:
15930 case CONST_CAST_EXPR:
15931 case DYNAMIC_CAST_EXPR:
15932 case STATIC_CAST_EXPR:
15933 {
15934 tree type;
15935 tree op, r = NULL_TREE;
15936
15937 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15938 if (integral_constant_expression_p
15939 && !cast_valid_in_integral_constant_expression_p (type))
15940 {
15941 if (complain & tf_error)
15942 error ("a cast to a type other than an integral or "
15943 "enumeration type cannot appear in a constant-expression");
15944 RETURN (error_mark_node);
15945 }
15946
15947 op = RECUR (TREE_OPERAND (t, 0));
15948
15949 warning_sentinel s(warn_useless_cast);
15950 switch (TREE_CODE (t))
15951 {
15952 case CAST_EXPR:
15953 r = build_functional_cast (type, op, complain);
15954 break;
15955 case REINTERPRET_CAST_EXPR:
15956 r = build_reinterpret_cast (type, op, complain);
15957 break;
15958 case CONST_CAST_EXPR:
15959 r = build_const_cast (type, op, complain);
15960 break;
15961 case DYNAMIC_CAST_EXPR:
15962 r = build_dynamic_cast (type, op, complain);
15963 break;
15964 case STATIC_CAST_EXPR:
15965 r = build_static_cast (type, op, complain);
15966 break;
15967 default:
15968 gcc_unreachable ();
15969 }
15970
15971 RETURN (r);
15972 }
15973
15974 case POSTDECREMENT_EXPR:
15975 case POSTINCREMENT_EXPR:
15976 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15977 args, complain, in_decl);
15978 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
15979 complain|decltype_flag));
15980
15981 case PREDECREMENT_EXPR:
15982 case PREINCREMENT_EXPR:
15983 case NEGATE_EXPR:
15984 case BIT_NOT_EXPR:
15985 case ABS_EXPR:
15986 case TRUTH_NOT_EXPR:
15987 case UNARY_PLUS_EXPR: /* Unary + */
15988 case REALPART_EXPR:
15989 case IMAGPART_EXPR:
15990 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
15991 RECUR (TREE_OPERAND (t, 0)),
15992 complain|decltype_flag));
15993
15994 case FIX_TRUNC_EXPR:
15995 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
15996 0, complain));
15997
15998 case ADDR_EXPR:
15999 op1 = TREE_OPERAND (t, 0);
16000 if (TREE_CODE (op1) == LABEL_DECL)
16001 RETURN (finish_label_address_expr (DECL_NAME (op1),
16002 EXPR_LOCATION (op1)));
16003 if (TREE_CODE (op1) == SCOPE_REF)
16004 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16005 /*done=*/true, /*address_p=*/true);
16006 else
16007 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16008 in_decl);
16009 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16010 complain|decltype_flag));
16011
16012 case PLUS_EXPR:
16013 case MINUS_EXPR:
16014 case MULT_EXPR:
16015 case TRUNC_DIV_EXPR:
16016 case CEIL_DIV_EXPR:
16017 case FLOOR_DIV_EXPR:
16018 case ROUND_DIV_EXPR:
16019 case EXACT_DIV_EXPR:
16020 case BIT_AND_EXPR:
16021 case BIT_IOR_EXPR:
16022 case BIT_XOR_EXPR:
16023 case TRUNC_MOD_EXPR:
16024 case FLOOR_MOD_EXPR:
16025 case TRUTH_ANDIF_EXPR:
16026 case TRUTH_ORIF_EXPR:
16027 case TRUTH_AND_EXPR:
16028 case TRUTH_OR_EXPR:
16029 case RSHIFT_EXPR:
16030 case LSHIFT_EXPR:
16031 case RROTATE_EXPR:
16032 case LROTATE_EXPR:
16033 case EQ_EXPR:
16034 case NE_EXPR:
16035 case MAX_EXPR:
16036 case MIN_EXPR:
16037 case LE_EXPR:
16038 case GE_EXPR:
16039 case LT_EXPR:
16040 case GT_EXPR:
16041 case MEMBER_REF:
16042 case DOTSTAR_EXPR:
16043 {
16044 warning_sentinel s1(warn_type_limits);
16045 warning_sentinel s2(warn_div_by_zero);
16046 warning_sentinel s3(warn_logical_op);
16047 warning_sentinel s4(warn_tautological_compare);
16048 tree op0 = RECUR (TREE_OPERAND (t, 0));
16049 tree op1 = RECUR (TREE_OPERAND (t, 1));
16050 tree r = build_x_binary_op
16051 (input_location, TREE_CODE (t),
16052 op0,
16053 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16054 ? ERROR_MARK
16055 : TREE_CODE (TREE_OPERAND (t, 0))),
16056 op1,
16057 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16058 ? ERROR_MARK
16059 : TREE_CODE (TREE_OPERAND (t, 1))),
16060 /*overload=*/NULL,
16061 complain|decltype_flag);
16062 if (EXPR_P (r) && TREE_NO_WARNING (t))
16063 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16064
16065 RETURN (r);
16066 }
16067
16068 case POINTER_PLUS_EXPR:
16069 {
16070 tree op0 = RECUR (TREE_OPERAND (t, 0));
16071 tree op1 = RECUR (TREE_OPERAND (t, 1));
16072 return fold_build_pointer_plus (op0, op1);
16073 }
16074
16075 case SCOPE_REF:
16076 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16077 /*address_p=*/false));
16078 case ARRAY_REF:
16079 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16080 args, complain, in_decl);
16081 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16082 RECUR (TREE_OPERAND (t, 1)),
16083 complain|decltype_flag));
16084
16085 case ARRAY_NOTATION_REF:
16086 {
16087 tree start_index, length, stride;
16088 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16089 args, complain, in_decl);
16090 start_index = RECUR (ARRAY_NOTATION_START (t));
16091 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16092 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16093 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16094 length, stride, TREE_TYPE (op1)));
16095 }
16096 case SIZEOF_EXPR:
16097 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
16098 RETURN (tsubst_copy (t, args, complain, in_decl));
16099 /* Fall through */
16100
16101 case ALIGNOF_EXPR:
16102 {
16103 tree r;
16104
16105 op1 = TREE_OPERAND (t, 0);
16106 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16107 op1 = TREE_TYPE (op1);
16108 if (!args)
16109 {
16110 /* When there are no ARGS, we are trying to evaluate a
16111 non-dependent expression from the parser. Trying to do
16112 the substitutions may not work. */
16113 if (!TYPE_P (op1))
16114 op1 = TREE_TYPE (op1);
16115 }
16116 else
16117 {
16118 ++cp_unevaluated_operand;
16119 ++c_inhibit_evaluation_warnings;
16120 if (TYPE_P (op1))
16121 op1 = tsubst (op1, args, complain, in_decl);
16122 else
16123 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16124 /*function_p=*/false,
16125 /*integral_constant_expression_p=*/
16126 false);
16127 --cp_unevaluated_operand;
16128 --c_inhibit_evaluation_warnings;
16129 }
16130 if (TYPE_P (op1))
16131 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16132 complain & tf_error);
16133 else
16134 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16135 complain & tf_error);
16136 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16137 {
16138 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16139 {
16140 if (!processing_template_decl && TYPE_P (op1))
16141 {
16142 r = build_min (SIZEOF_EXPR, size_type_node,
16143 build1 (NOP_EXPR, op1, error_mark_node));
16144 SIZEOF_EXPR_TYPE_P (r) = 1;
16145 }
16146 else
16147 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16148 TREE_SIDE_EFFECTS (r) = 0;
16149 TREE_READONLY (r) = 1;
16150 }
16151 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16152 }
16153 RETURN (r);
16154 }
16155
16156 case AT_ENCODE_EXPR:
16157 {
16158 op1 = TREE_OPERAND (t, 0);
16159 ++cp_unevaluated_operand;
16160 ++c_inhibit_evaluation_warnings;
16161 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16162 /*function_p=*/false,
16163 /*integral_constant_expression_p=*/false);
16164 --cp_unevaluated_operand;
16165 --c_inhibit_evaluation_warnings;
16166 RETURN (objc_build_encode_expr (op1));
16167 }
16168
16169 case NOEXCEPT_EXPR:
16170 op1 = TREE_OPERAND (t, 0);
16171 ++cp_unevaluated_operand;
16172 ++c_inhibit_evaluation_warnings;
16173 ++cp_noexcept_operand;
16174 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16175 /*function_p=*/false,
16176 /*integral_constant_expression_p=*/false);
16177 --cp_unevaluated_operand;
16178 --c_inhibit_evaluation_warnings;
16179 --cp_noexcept_operand;
16180 RETURN (finish_noexcept_expr (op1, complain));
16181
16182 case MODOP_EXPR:
16183 {
16184 warning_sentinel s(warn_div_by_zero);
16185 tree lhs = RECUR (TREE_OPERAND (t, 0));
16186 tree rhs = RECUR (TREE_OPERAND (t, 2));
16187 tree r = build_x_modify_expr
16188 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16189 complain|decltype_flag);
16190 /* TREE_NO_WARNING must be set if either the expression was
16191 parenthesized or it uses an operator such as >>= rather
16192 than plain assignment. In the former case, it was already
16193 set and must be copied. In the latter case,
16194 build_x_modify_expr sets it and it must not be reset
16195 here. */
16196 if (TREE_NO_WARNING (t))
16197 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16198
16199 RETURN (r);
16200 }
16201
16202 case ARROW_EXPR:
16203 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16204 args, complain, in_decl);
16205 /* Remember that there was a reference to this entity. */
16206 if (DECL_P (op1)
16207 && !mark_used (op1, complain) && !(complain & tf_error))
16208 RETURN (error_mark_node);
16209 RETURN (build_x_arrow (input_location, op1, complain));
16210
16211 case NEW_EXPR:
16212 {
16213 tree placement = RECUR (TREE_OPERAND (t, 0));
16214 tree init = RECUR (TREE_OPERAND (t, 3));
16215 vec<tree, va_gc> *placement_vec;
16216 vec<tree, va_gc> *init_vec;
16217 tree ret;
16218
16219 if (placement == NULL_TREE)
16220 placement_vec = NULL;
16221 else
16222 {
16223 placement_vec = make_tree_vector ();
16224 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16225 vec_safe_push (placement_vec, TREE_VALUE (placement));
16226 }
16227
16228 /* If there was an initializer in the original tree, but it
16229 instantiated to an empty list, then we should pass a
16230 non-NULL empty vector to tell build_new that it was an
16231 empty initializer() rather than no initializer. This can
16232 only happen when the initializer is a pack expansion whose
16233 parameter packs are of length zero. */
16234 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16235 init_vec = NULL;
16236 else
16237 {
16238 init_vec = make_tree_vector ();
16239 if (init == void_node)
16240 gcc_assert (init_vec != NULL);
16241 else
16242 {
16243 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16244 vec_safe_push (init_vec, TREE_VALUE (init));
16245 }
16246 }
16247
16248 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16249 tree op2 = RECUR (TREE_OPERAND (t, 2));
16250 ret = build_new (&placement_vec, op1, op2, &init_vec,
16251 NEW_EXPR_USE_GLOBAL (t),
16252 complain);
16253
16254 if (placement_vec != NULL)
16255 release_tree_vector (placement_vec);
16256 if (init_vec != NULL)
16257 release_tree_vector (init_vec);
16258
16259 RETURN (ret);
16260 }
16261
16262 case DELETE_EXPR:
16263 {
16264 tree op0 = RECUR (TREE_OPERAND (t, 0));
16265 tree op1 = RECUR (TREE_OPERAND (t, 1));
16266 RETURN (delete_sanity (op0, op1,
16267 DELETE_EXPR_USE_VEC (t),
16268 DELETE_EXPR_USE_GLOBAL (t),
16269 complain));
16270 }
16271
16272 case COMPOUND_EXPR:
16273 {
16274 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
16275 complain & ~tf_decltype, in_decl,
16276 /*function_p=*/false,
16277 integral_constant_expression_p);
16278 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
16279 op0,
16280 RECUR (TREE_OPERAND (t, 1)),
16281 complain|decltype_flag));
16282 }
16283
16284 case CALL_EXPR:
16285 {
16286 tree function;
16287 vec<tree, va_gc> *call_args;
16288 unsigned int nargs, i;
16289 bool qualified_p;
16290 bool koenig_p;
16291 tree ret;
16292
16293 function = CALL_EXPR_FN (t);
16294 /* When we parsed the expression, we determined whether or
16295 not Koenig lookup should be performed. */
16296 koenig_p = KOENIG_LOOKUP_P (t);
16297 if (TREE_CODE (function) == SCOPE_REF)
16298 {
16299 qualified_p = true;
16300 function = tsubst_qualified_id (function, args, complain, in_decl,
16301 /*done=*/false,
16302 /*address_p=*/false);
16303 }
16304 else if (koenig_p && identifier_p (function))
16305 {
16306 /* Do nothing; calling tsubst_copy_and_build on an identifier
16307 would incorrectly perform unqualified lookup again.
16308
16309 Note that we can also have an IDENTIFIER_NODE if the earlier
16310 unqualified lookup found a member function; in that case
16311 koenig_p will be false and we do want to do the lookup
16312 again to find the instantiated member function.
16313
16314 FIXME but doing that causes c++/15272, so we need to stop
16315 using IDENTIFIER_NODE in that situation. */
16316 qualified_p = false;
16317 }
16318 else
16319 {
16320 if (TREE_CODE (function) == COMPONENT_REF)
16321 {
16322 tree op = TREE_OPERAND (function, 1);
16323
16324 qualified_p = (TREE_CODE (op) == SCOPE_REF
16325 || (BASELINK_P (op)
16326 && BASELINK_QUALIFIED_P (op)));
16327 }
16328 else
16329 qualified_p = false;
16330
16331 if (TREE_CODE (function) == ADDR_EXPR
16332 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
16333 /* Avoid error about taking the address of a constructor. */
16334 function = TREE_OPERAND (function, 0);
16335
16336 function = tsubst_copy_and_build (function, args, complain,
16337 in_decl,
16338 !qualified_p,
16339 integral_constant_expression_p);
16340
16341 if (BASELINK_P (function))
16342 qualified_p = true;
16343 }
16344
16345 nargs = call_expr_nargs (t);
16346 call_args = make_tree_vector ();
16347 for (i = 0; i < nargs; ++i)
16348 {
16349 tree arg = CALL_EXPR_ARG (t, i);
16350
16351 if (!PACK_EXPANSION_P (arg))
16352 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
16353 else
16354 {
16355 /* Expand the pack expansion and push each entry onto
16356 CALL_ARGS. */
16357 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
16358 if (TREE_CODE (arg) == TREE_VEC)
16359 {
16360 unsigned int len, j;
16361
16362 len = TREE_VEC_LENGTH (arg);
16363 for (j = 0; j < len; ++j)
16364 {
16365 tree value = TREE_VEC_ELT (arg, j);
16366 if (value != NULL_TREE)
16367 value = convert_from_reference (value);
16368 vec_safe_push (call_args, value);
16369 }
16370 }
16371 else
16372 {
16373 /* A partial substitution. Add one entry. */
16374 vec_safe_push (call_args, arg);
16375 }
16376 }
16377 }
16378
16379 /* We do not perform argument-dependent lookup if normal
16380 lookup finds a non-function, in accordance with the
16381 expected resolution of DR 218. */
16382 if (koenig_p
16383 && ((is_overloaded_fn (function)
16384 /* If lookup found a member function, the Koenig lookup is
16385 not appropriate, even if an unqualified-name was used
16386 to denote the function. */
16387 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
16388 || identifier_p (function))
16389 /* Only do this when substitution turns a dependent call
16390 into a non-dependent call. */
16391 && type_dependent_expression_p_push (t)
16392 && !any_type_dependent_arguments_p (call_args))
16393 function = perform_koenig_lookup (function, call_args, tf_none);
16394
16395 if (identifier_p (function)
16396 && !any_type_dependent_arguments_p (call_args))
16397 {
16398 if (koenig_p && (complain & tf_warning_or_error))
16399 {
16400 /* For backwards compatibility and good diagnostics, try
16401 the unqualified lookup again if we aren't in SFINAE
16402 context. */
16403 tree unq = (tsubst_copy_and_build
16404 (function, args, complain, in_decl, true,
16405 integral_constant_expression_p));
16406 if (unq == error_mark_node)
16407 RETURN (error_mark_node);
16408
16409 if (unq != function)
16410 {
16411 tree fn = unq;
16412 if (INDIRECT_REF_P (fn))
16413 fn = TREE_OPERAND (fn, 0);
16414 if (TREE_CODE (fn) == COMPONENT_REF)
16415 fn = TREE_OPERAND (fn, 1);
16416 if (is_overloaded_fn (fn))
16417 fn = get_first_fn (fn);
16418 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16419 "%qD was not declared in this scope, "
16420 "and no declarations were found by "
16421 "argument-dependent lookup at the point "
16422 "of instantiation", function))
16423 {
16424 if (!DECL_P (fn))
16425 /* Can't say anything more. */;
16426 else if (DECL_CLASS_SCOPE_P (fn))
16427 {
16428 location_t loc = EXPR_LOC_OR_LOC (t,
16429 input_location);
16430 inform (loc,
16431 "declarations in dependent base %qT are "
16432 "not found by unqualified lookup",
16433 DECL_CLASS_CONTEXT (fn));
16434 if (current_class_ptr)
16435 inform (loc,
16436 "use %<this->%D%> instead", function);
16437 else
16438 inform (loc,
16439 "use %<%T::%D%> instead",
16440 current_class_name, function);
16441 }
16442 else
16443 inform (DECL_SOURCE_LOCATION (fn),
16444 "%qD declared here, later in the "
16445 "translation unit", fn);
16446 }
16447 function = unq;
16448 }
16449 }
16450 if (identifier_p (function))
16451 {
16452 if (complain & tf_error)
16453 unqualified_name_lookup_error (function);
16454 release_tree_vector (call_args);
16455 RETURN (error_mark_node);
16456 }
16457 }
16458
16459 /* Remember that there was a reference to this entity. */
16460 if (DECL_P (function)
16461 && !mark_used (function, complain) && !(complain & tf_error))
16462 RETURN (error_mark_node);
16463
16464 /* Put back tf_decltype for the actual call. */
16465 complain |= decltype_flag;
16466
16467 if (TREE_CODE (function) == OFFSET_REF)
16468 ret = build_offset_ref_call_from_tree (function, &call_args,
16469 complain);
16470 else if (TREE_CODE (function) == COMPONENT_REF)
16471 {
16472 tree instance = TREE_OPERAND (function, 0);
16473 tree fn = TREE_OPERAND (function, 1);
16474
16475 if (processing_template_decl
16476 && (type_dependent_expression_p (instance)
16477 || (!BASELINK_P (fn)
16478 && TREE_CODE (fn) != FIELD_DECL)
16479 || type_dependent_expression_p (fn)
16480 || any_type_dependent_arguments_p (call_args)))
16481 ret = build_nt_call_vec (function, call_args);
16482 else if (!BASELINK_P (fn))
16483 ret = finish_call_expr (function, &call_args,
16484 /*disallow_virtual=*/false,
16485 /*koenig_p=*/false,
16486 complain);
16487 else
16488 ret = (build_new_method_call
16489 (instance, fn,
16490 &call_args, NULL_TREE,
16491 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
16492 /*fn_p=*/NULL,
16493 complain));
16494 }
16495 else
16496 ret = finish_call_expr (function, &call_args,
16497 /*disallow_virtual=*/qualified_p,
16498 koenig_p,
16499 complain);
16500
16501 release_tree_vector (call_args);
16502
16503 RETURN (ret);
16504 }
16505
16506 case COND_EXPR:
16507 {
16508 tree cond = RECUR (TREE_OPERAND (t, 0));
16509 tree folded_cond = fold_non_dependent_expr (cond);
16510 tree exp1, exp2;
16511
16512 if (TREE_CODE (folded_cond) == INTEGER_CST)
16513 {
16514 if (integer_zerop (folded_cond))
16515 {
16516 ++c_inhibit_evaluation_warnings;
16517 exp1 = RECUR (TREE_OPERAND (t, 1));
16518 --c_inhibit_evaluation_warnings;
16519 exp2 = RECUR (TREE_OPERAND (t, 2));
16520 }
16521 else
16522 {
16523 exp1 = RECUR (TREE_OPERAND (t, 1));
16524 ++c_inhibit_evaluation_warnings;
16525 exp2 = RECUR (TREE_OPERAND (t, 2));
16526 --c_inhibit_evaluation_warnings;
16527 }
16528 cond = folded_cond;
16529 }
16530 else
16531 {
16532 exp1 = RECUR (TREE_OPERAND (t, 1));
16533 exp2 = RECUR (TREE_OPERAND (t, 2));
16534 }
16535
16536 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
16537 cond, exp1, exp2, complain));
16538 }
16539
16540 case PSEUDO_DTOR_EXPR:
16541 {
16542 tree op0 = RECUR (TREE_OPERAND (t, 0));
16543 tree op1 = RECUR (TREE_OPERAND (t, 1));
16544 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
16545 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
16546 input_location));
16547 }
16548
16549 case TREE_LIST:
16550 {
16551 tree purpose, value, chain;
16552
16553 if (t == void_list_node)
16554 RETURN (t);
16555
16556 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
16557 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
16558 {
16559 /* We have pack expansions, so expand those and
16560 create a new list out of it. */
16561 tree purposevec = NULL_TREE;
16562 tree valuevec = NULL_TREE;
16563 tree chain;
16564 int i, len = -1;
16565
16566 /* Expand the argument expressions. */
16567 if (TREE_PURPOSE (t))
16568 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
16569 complain, in_decl);
16570 if (TREE_VALUE (t))
16571 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
16572 complain, in_decl);
16573
16574 /* Build the rest of the list. */
16575 chain = TREE_CHAIN (t);
16576 if (chain && chain != void_type_node)
16577 chain = RECUR (chain);
16578
16579 /* Determine the number of arguments. */
16580 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
16581 {
16582 len = TREE_VEC_LENGTH (purposevec);
16583 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16584 }
16585 else if (TREE_CODE (valuevec) == TREE_VEC)
16586 len = TREE_VEC_LENGTH (valuevec);
16587 else
16588 {
16589 /* Since we only performed a partial substitution into
16590 the argument pack, we only RETURN (a single list
16591 node. */
16592 if (purposevec == TREE_PURPOSE (t)
16593 && valuevec == TREE_VALUE (t)
16594 && chain == TREE_CHAIN (t))
16595 RETURN (t);
16596
16597 RETURN (tree_cons (purposevec, valuevec, chain));
16598 }
16599
16600 /* Convert the argument vectors into a TREE_LIST */
16601 i = len;
16602 while (i > 0)
16603 {
16604 /* Grab the Ith values. */
16605 i--;
16606 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
16607 : NULL_TREE;
16608 value
16609 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
16610 : NULL_TREE;
16611
16612 /* Build the list (backwards). */
16613 chain = tree_cons (purpose, value, chain);
16614 }
16615
16616 RETURN (chain);
16617 }
16618
16619 purpose = TREE_PURPOSE (t);
16620 if (purpose)
16621 purpose = RECUR (purpose);
16622 value = TREE_VALUE (t);
16623 if (value)
16624 value = RECUR (value);
16625 chain = TREE_CHAIN (t);
16626 if (chain && chain != void_type_node)
16627 chain = RECUR (chain);
16628 if (purpose == TREE_PURPOSE (t)
16629 && value == TREE_VALUE (t)
16630 && chain == TREE_CHAIN (t))
16631 RETURN (t);
16632 RETURN (tree_cons (purpose, value, chain));
16633 }
16634
16635 case COMPONENT_REF:
16636 {
16637 tree object;
16638 tree object_type;
16639 tree member;
16640 tree r;
16641
16642 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16643 args, complain, in_decl);
16644 /* Remember that there was a reference to this entity. */
16645 if (DECL_P (object)
16646 && !mark_used (object, complain) && !(complain & tf_error))
16647 RETURN (error_mark_node);
16648 object_type = TREE_TYPE (object);
16649
16650 member = TREE_OPERAND (t, 1);
16651 if (BASELINK_P (member))
16652 member = tsubst_baselink (member,
16653 non_reference (TREE_TYPE (object)),
16654 args, complain, in_decl);
16655 else
16656 member = tsubst_copy (member, args, complain, in_decl);
16657 if (member == error_mark_node)
16658 RETURN (error_mark_node);
16659
16660 if (type_dependent_expression_p (object))
16661 /* We can't do much here. */;
16662 else if (!CLASS_TYPE_P (object_type))
16663 {
16664 if (scalarish_type_p (object_type))
16665 {
16666 tree s = NULL_TREE;
16667 tree dtor = member;
16668
16669 if (TREE_CODE (dtor) == SCOPE_REF)
16670 {
16671 s = TREE_OPERAND (dtor, 0);
16672 dtor = TREE_OPERAND (dtor, 1);
16673 }
16674 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
16675 {
16676 dtor = TREE_OPERAND (dtor, 0);
16677 if (TYPE_P (dtor))
16678 RETURN (finish_pseudo_destructor_expr
16679 (object, s, dtor, input_location));
16680 }
16681 }
16682 }
16683 else if (TREE_CODE (member) == SCOPE_REF
16684 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
16685 {
16686 /* Lookup the template functions now that we know what the
16687 scope is. */
16688 tree scope = TREE_OPERAND (member, 0);
16689 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
16690 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
16691 member = lookup_qualified_name (scope, tmpl,
16692 /*is_type_p=*/false,
16693 /*complain=*/false);
16694 if (BASELINK_P (member))
16695 {
16696 BASELINK_FUNCTIONS (member)
16697 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
16698 args);
16699 member = (adjust_result_of_qualified_name_lookup
16700 (member, BINFO_TYPE (BASELINK_BINFO (member)),
16701 object_type));
16702 }
16703 else
16704 {
16705 qualified_name_lookup_error (scope, tmpl, member,
16706 input_location);
16707 RETURN (error_mark_node);
16708 }
16709 }
16710 else if (TREE_CODE (member) == SCOPE_REF
16711 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
16712 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
16713 {
16714 if (complain & tf_error)
16715 {
16716 if (TYPE_P (TREE_OPERAND (member, 0)))
16717 error ("%qT is not a class or namespace",
16718 TREE_OPERAND (member, 0));
16719 else
16720 error ("%qD is not a class or namespace",
16721 TREE_OPERAND (member, 0));
16722 }
16723 RETURN (error_mark_node);
16724 }
16725 else if (TREE_CODE (member) == FIELD_DECL)
16726 {
16727 r = finish_non_static_data_member (member, object, NULL_TREE);
16728 if (TREE_CODE (r) == COMPONENT_REF)
16729 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16730 RETURN (r);
16731 }
16732
16733 r = finish_class_member_access_expr (object, member,
16734 /*template_p=*/false,
16735 complain);
16736 if (TREE_CODE (r) == COMPONENT_REF)
16737 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16738 RETURN (r);
16739 }
16740
16741 case THROW_EXPR:
16742 RETURN (build_throw
16743 (RECUR (TREE_OPERAND (t, 0))));
16744
16745 case CONSTRUCTOR:
16746 {
16747 vec<constructor_elt, va_gc> *n;
16748 constructor_elt *ce;
16749 unsigned HOST_WIDE_INT idx;
16750 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16751 bool process_index_p;
16752 int newlen;
16753 bool need_copy_p = false;
16754 tree r;
16755
16756 if (type == error_mark_node)
16757 RETURN (error_mark_node);
16758
16759 /* digest_init will do the wrong thing if we let it. */
16760 if (type && TYPE_PTRMEMFUNC_P (type))
16761 RETURN (t);
16762
16763 /* We do not want to process the index of aggregate
16764 initializers as they are identifier nodes which will be
16765 looked up by digest_init. */
16766 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
16767
16768 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
16769 newlen = vec_safe_length (n);
16770 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
16771 {
16772 if (ce->index && process_index_p
16773 /* An identifier index is looked up in the type
16774 being initialized, not the current scope. */
16775 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
16776 ce->index = RECUR (ce->index);
16777
16778 if (PACK_EXPANSION_P (ce->value))
16779 {
16780 /* Substitute into the pack expansion. */
16781 ce->value = tsubst_pack_expansion (ce->value, args, complain,
16782 in_decl);
16783
16784 if (ce->value == error_mark_node
16785 || PACK_EXPANSION_P (ce->value))
16786 ;
16787 else if (TREE_VEC_LENGTH (ce->value) == 1)
16788 /* Just move the argument into place. */
16789 ce->value = TREE_VEC_ELT (ce->value, 0);
16790 else
16791 {
16792 /* Update the length of the final CONSTRUCTOR
16793 arguments vector, and note that we will need to
16794 copy.*/
16795 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
16796 need_copy_p = true;
16797 }
16798 }
16799 else
16800 ce->value = RECUR (ce->value);
16801 }
16802
16803 if (need_copy_p)
16804 {
16805 vec<constructor_elt, va_gc> *old_n = n;
16806
16807 vec_alloc (n, newlen);
16808 FOR_EACH_VEC_ELT (*old_n, idx, ce)
16809 {
16810 if (TREE_CODE (ce->value) == TREE_VEC)
16811 {
16812 int i, len = TREE_VEC_LENGTH (ce->value);
16813 for (i = 0; i < len; ++i)
16814 CONSTRUCTOR_APPEND_ELT (n, 0,
16815 TREE_VEC_ELT (ce->value, i));
16816 }
16817 else
16818 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
16819 }
16820 }
16821
16822 r = build_constructor (init_list_type_node, n);
16823 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
16824
16825 if (TREE_HAS_CONSTRUCTOR (t))
16826 RETURN (finish_compound_literal (type, r, complain));
16827
16828 TREE_TYPE (r) = type;
16829 RETURN (r);
16830 }
16831
16832 case TYPEID_EXPR:
16833 {
16834 tree operand_0 = TREE_OPERAND (t, 0);
16835 if (TYPE_P (operand_0))
16836 {
16837 operand_0 = tsubst (operand_0, args, complain, in_decl);
16838 RETURN (get_typeid (operand_0, complain));
16839 }
16840 else
16841 {
16842 operand_0 = RECUR (operand_0);
16843 RETURN (build_typeid (operand_0, complain));
16844 }
16845 }
16846
16847 case VAR_DECL:
16848 if (!args)
16849 RETURN (t);
16850 else if (DECL_PACK_P (t))
16851 {
16852 /* We don't build decls for an instantiation of a
16853 variadic capture proxy, we instantiate the elements
16854 when needed. */
16855 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
16856 return RECUR (DECL_VALUE_EXPR (t));
16857 }
16858 /* Fall through */
16859
16860 case PARM_DECL:
16861 {
16862 tree r = tsubst_copy (t, args, complain, in_decl);
16863 /* ??? We're doing a subset of finish_id_expression here. */
16864 if (VAR_P (r)
16865 && !processing_template_decl
16866 && !cp_unevaluated_operand
16867 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
16868 && CP_DECL_THREAD_LOCAL_P (r))
16869 {
16870 if (tree wrap = get_tls_wrapper_fn (r))
16871 /* Replace an evaluated use of the thread_local variable with
16872 a call to its wrapper. */
16873 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
16874 }
16875 else if (outer_automatic_var_p (r))
16876 {
16877 r = process_outer_var_ref (r, complain);
16878 if (is_capture_proxy (r))
16879 register_local_specialization (r, t);
16880 }
16881
16882 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
16883 /* If the original type was a reference, we'll be wrapped in
16884 the appropriate INDIRECT_REF. */
16885 r = convert_from_reference (r);
16886 RETURN (r);
16887 }
16888
16889 case VA_ARG_EXPR:
16890 {
16891 tree op0 = RECUR (TREE_OPERAND (t, 0));
16892 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16893 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
16894 }
16895
16896 case OFFSETOF_EXPR:
16897 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
16898 EXPR_LOCATION (t)));
16899
16900 case TRAIT_EXPR:
16901 {
16902 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
16903 complain, in_decl);
16904
16905 tree type2 = TRAIT_EXPR_TYPE2 (t);
16906 if (type2 && TREE_CODE (type2) == TREE_LIST)
16907 type2 = RECUR (type2);
16908 else if (type2)
16909 type2 = tsubst (type2, args, complain, in_decl);
16910
16911 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
16912 }
16913
16914 case STMT_EXPR:
16915 {
16916 tree old_stmt_expr = cur_stmt_expr;
16917 tree stmt_expr = begin_stmt_expr ();
16918
16919 cur_stmt_expr = stmt_expr;
16920 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
16921 integral_constant_expression_p);
16922 stmt_expr = finish_stmt_expr (stmt_expr, false);
16923 cur_stmt_expr = old_stmt_expr;
16924
16925 /* If the resulting list of expression statement is empty,
16926 fold it further into void_node. */
16927 if (empty_expr_stmt_p (stmt_expr))
16928 stmt_expr = void_node;
16929
16930 RETURN (stmt_expr);
16931 }
16932
16933 case LAMBDA_EXPR:
16934 {
16935 tree r = build_lambda_expr ();
16936
16937 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
16938 LAMBDA_EXPR_CLOSURE (r) = type;
16939 CLASSTYPE_LAMBDA_EXPR (type) = r;
16940
16941 LAMBDA_EXPR_LOCATION (r)
16942 = LAMBDA_EXPR_LOCATION (t);
16943 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16944 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16945 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16946 LAMBDA_EXPR_DISCRIMINATOR (r)
16947 = (LAMBDA_EXPR_DISCRIMINATOR (t));
16948 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
16949 if (!scope)
16950 /* No substitution needed. */;
16951 else if (VAR_OR_FUNCTION_DECL_P (scope))
16952 /* For a function or variable scope, we want to use tsubst so that we
16953 don't complain about referring to an auto before deduction. */
16954 scope = tsubst (scope, args, complain, in_decl);
16955 else if (TREE_CODE (scope) == PARM_DECL)
16956 {
16957 /* Look up the parameter we want directly, as tsubst_copy
16958 doesn't do what we need. */
16959 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
16960 tree parm = FUNCTION_FIRST_USER_PARM (fn);
16961 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
16962 parm = DECL_CHAIN (parm);
16963 scope = parm;
16964 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16965 if (DECL_CONTEXT (scope) == NULL_TREE)
16966 DECL_CONTEXT (scope) = fn;
16967 }
16968 else if (TREE_CODE (scope) == FIELD_DECL)
16969 /* For a field, use tsubst_copy so that we look up the existing field
16970 rather than build a new one. */
16971 scope = RECUR (scope);
16972 else
16973 gcc_unreachable ();
16974 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
16975 LAMBDA_EXPR_RETURN_TYPE (r)
16976 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
16977
16978 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16979 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16980
16981 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16982 determine_visibility (TYPE_NAME (type));
16983 /* Now that we know visibility, instantiate the type so we have a
16984 declaration of the op() for later calls to lambda_function. */
16985 complete_type (type);
16986
16987 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16988
16989 insert_pending_capture_proxies ();
16990
16991 RETURN (build_lambda_object (r));
16992 }
16993
16994 case TARGET_EXPR:
16995 /* We can get here for a constant initializer of non-dependent type.
16996 FIXME stop folding in cp_parser_initializer_clause. */
16997 {
16998 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
16999 complain);
17000 RETURN (r);
17001 }
17002
17003 case TRANSACTION_EXPR:
17004 RETURN (tsubst_expr(t, args, complain, in_decl,
17005 integral_constant_expression_p));
17006
17007 case PAREN_EXPR:
17008 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17009
17010 case VEC_PERM_EXPR:
17011 {
17012 tree op0 = RECUR (TREE_OPERAND (t, 0));
17013 tree op1 = RECUR (TREE_OPERAND (t, 1));
17014 tree op2 = RECUR (TREE_OPERAND (t, 2));
17015 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17016 complain));
17017 }
17018
17019 case REQUIRES_EXPR:
17020 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17021
17022 default:
17023 /* Handle Objective-C++ constructs, if appropriate. */
17024 {
17025 tree subst
17026 = objcp_tsubst_copy_and_build (t, args, complain,
17027 in_decl, /*function_p=*/false);
17028 if (subst)
17029 RETURN (subst);
17030 }
17031 RETURN (tsubst_copy (t, args, complain, in_decl));
17032 }
17033
17034 #undef RECUR
17035 #undef RETURN
17036 out:
17037 input_location = loc;
17038 return retval;
17039 }
17040
17041 /* Verify that the instantiated ARGS are valid. For type arguments,
17042 make sure that the type's linkage is ok. For non-type arguments,
17043 make sure they are constants if they are integral or enumerations.
17044 Emit an error under control of COMPLAIN, and return TRUE on error. */
17045
17046 static bool
17047 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17048 {
17049 if (dependent_template_arg_p (t))
17050 return false;
17051 if (ARGUMENT_PACK_P (t))
17052 {
17053 tree vec = ARGUMENT_PACK_ARGS (t);
17054 int len = TREE_VEC_LENGTH (vec);
17055 bool result = false;
17056 int i;
17057
17058 for (i = 0; i < len; ++i)
17059 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17060 result = true;
17061 return result;
17062 }
17063 else if (TYPE_P (t))
17064 {
17065 /* [basic.link]: A name with no linkage (notably, the name
17066 of a class or enumeration declared in a local scope)
17067 shall not be used to declare an entity with linkage.
17068 This implies that names with no linkage cannot be used as
17069 template arguments
17070
17071 DR 757 relaxes this restriction for C++0x. */
17072 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17073 : no_linkage_check (t, /*relaxed_p=*/false));
17074
17075 if (nt)
17076 {
17077 /* DR 488 makes use of a type with no linkage cause
17078 type deduction to fail. */
17079 if (complain & tf_error)
17080 {
17081 if (TYPE_ANONYMOUS_P (nt))
17082 error ("%qT is/uses anonymous type", t);
17083 else
17084 error ("template argument for %qD uses local type %qT",
17085 tmpl, t);
17086 }
17087 return true;
17088 }
17089 /* In order to avoid all sorts of complications, we do not
17090 allow variably-modified types as template arguments. */
17091 else if (variably_modified_type_p (t, NULL_TREE))
17092 {
17093 if (complain & tf_error)
17094 error ("%qT is a variably modified type", t);
17095 return true;
17096 }
17097 }
17098 /* Class template and alias template arguments should be OK. */
17099 else if (DECL_TYPE_TEMPLATE_P (t))
17100 ;
17101 /* A non-type argument of integral or enumerated type must be a
17102 constant. */
17103 else if (TREE_TYPE (t)
17104 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17105 && !REFERENCE_REF_P (t)
17106 && !TREE_CONSTANT (t))
17107 {
17108 if (complain & tf_error)
17109 error ("integral expression %qE is not constant", t);
17110 return true;
17111 }
17112 return false;
17113 }
17114
17115 static bool
17116 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17117 {
17118 int ix, len = DECL_NTPARMS (tmpl);
17119 bool result = false;
17120
17121 for (ix = 0; ix != len; ix++)
17122 {
17123 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17124 result = true;
17125 }
17126 if (result && (complain & tf_error))
17127 error (" trying to instantiate %qD", tmpl);
17128 return result;
17129 }
17130
17131 /* We're out of SFINAE context now, so generate diagnostics for the access
17132 errors we saw earlier when instantiating D from TMPL and ARGS. */
17133
17134 static void
17135 recheck_decl_substitution (tree d, tree tmpl, tree args)
17136 {
17137 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17138 tree type = TREE_TYPE (pattern);
17139 location_t loc = input_location;
17140
17141 push_access_scope (d);
17142 push_deferring_access_checks (dk_no_deferred);
17143 input_location = DECL_SOURCE_LOCATION (pattern);
17144 tsubst (type, args, tf_warning_or_error, d);
17145 input_location = loc;
17146 pop_deferring_access_checks ();
17147 pop_access_scope (d);
17148 }
17149
17150 /* Instantiate the indicated variable, function, or alias template TMPL with
17151 the template arguments in TARG_PTR. */
17152
17153 static tree
17154 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17155 {
17156 tree targ_ptr = orig_args;
17157 tree fndecl;
17158 tree gen_tmpl;
17159 tree spec;
17160 bool access_ok = true;
17161
17162 if (tmpl == error_mark_node)
17163 return error_mark_node;
17164
17165 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
17166
17167 /* If this function is a clone, handle it specially. */
17168 if (DECL_CLONED_FUNCTION_P (tmpl))
17169 {
17170 tree spec;
17171 tree clone;
17172
17173 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17174 DECL_CLONED_FUNCTION. */
17175 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
17176 targ_ptr, complain);
17177 if (spec == error_mark_node)
17178 return error_mark_node;
17179
17180 /* Look for the clone. */
17181 FOR_EACH_CLONE (clone, spec)
17182 if (DECL_NAME (clone) == DECL_NAME (tmpl))
17183 return clone;
17184 /* We should always have found the clone by now. */
17185 gcc_unreachable ();
17186 return NULL_TREE;
17187 }
17188
17189 if (targ_ptr == error_mark_node)
17190 return error_mark_node;
17191
17192 /* Check to see if we already have this specialization. */
17193 gen_tmpl = most_general_template (tmpl);
17194 if (tmpl != gen_tmpl)
17195 /* The TMPL is a partial instantiation. To get a full set of
17196 arguments we must add the arguments used to perform the
17197 partial instantiation. */
17198 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
17199 targ_ptr);
17200
17201 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17202 but it doesn't seem to be on the hot path. */
17203 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
17204
17205 gcc_assert (tmpl == gen_tmpl
17206 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
17207 == spec)
17208 || fndecl == NULL_TREE);
17209
17210 if (spec != NULL_TREE)
17211 {
17212 if (FNDECL_HAS_ACCESS_ERRORS (spec))
17213 {
17214 if (complain & tf_error)
17215 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
17216 return error_mark_node;
17217 }
17218 return spec;
17219 }
17220
17221 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
17222 complain))
17223 return error_mark_node;
17224
17225 /* We are building a FUNCTION_DECL, during which the access of its
17226 parameters and return types have to be checked. However this
17227 FUNCTION_DECL which is the desired context for access checking
17228 is not built yet. We solve this chicken-and-egg problem by
17229 deferring all checks until we have the FUNCTION_DECL. */
17230 push_deferring_access_checks (dk_deferred);
17231
17232 /* Instantiation of the function happens in the context of the function
17233 template, not the context of the overload resolution we're doing. */
17234 push_to_top_level ();
17235 /* If there are dependent arguments, e.g. because we're doing partial
17236 ordering, make sure processing_template_decl stays set. */
17237 if (uses_template_parms (targ_ptr))
17238 ++processing_template_decl;
17239 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17240 {
17241 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
17242 complain, gen_tmpl, true);
17243 push_nested_class (ctx);
17244 }
17245
17246 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
17247
17248 if (VAR_P (pattern))
17249 {
17250 /* We need to determine if we're using a partial or explicit
17251 specialization now, because the type of the variable could be
17252 different. */
17253 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
17254 tree elt = most_specialized_partial_spec (tid, complain);
17255 if (elt == error_mark_node)
17256 pattern = error_mark_node;
17257 else if (elt)
17258 {
17259 tmpl = TREE_VALUE (elt);
17260 pattern = DECL_TEMPLATE_RESULT (tmpl);
17261 targ_ptr = TREE_PURPOSE (elt);
17262 }
17263 }
17264
17265 /* Substitute template parameters to obtain the specialization. */
17266 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
17267 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17268 pop_nested_class ();
17269 pop_from_top_level ();
17270
17271 if (fndecl == error_mark_node)
17272 {
17273 pop_deferring_access_checks ();
17274 return error_mark_node;
17275 }
17276
17277 /* The DECL_TI_TEMPLATE should always be the immediate parent
17278 template, not the most general template. */
17279 DECL_TI_TEMPLATE (fndecl) = tmpl;
17280 DECL_TI_ARGS (fndecl) = targ_ptr;
17281
17282 /* Now we know the specialization, compute access previously
17283 deferred. */
17284 push_access_scope (fndecl);
17285 if (!perform_deferred_access_checks (complain))
17286 access_ok = false;
17287 pop_access_scope (fndecl);
17288 pop_deferring_access_checks ();
17289
17290 /* If we've just instantiated the main entry point for a function,
17291 instantiate all the alternate entry points as well. We do this
17292 by cloning the instantiation of the main entry point, not by
17293 instantiating the template clones. */
17294 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
17295 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
17296
17297 if (!access_ok)
17298 {
17299 if (!(complain & tf_error))
17300 {
17301 /* Remember to reinstantiate when we're out of SFINAE so the user
17302 can see the errors. */
17303 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
17304 }
17305 return error_mark_node;
17306 }
17307 return fndecl;
17308 }
17309
17310 /* Wrapper for instantiate_template_1. */
17311
17312 tree
17313 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
17314 {
17315 tree ret;
17316 timevar_push (TV_TEMPLATE_INST);
17317 ret = instantiate_template_1 (tmpl, orig_args, complain);
17318 timevar_pop (TV_TEMPLATE_INST);
17319 return ret;
17320 }
17321
17322 /* Instantiate the alias template TMPL with ARGS. Also push a template
17323 instantiation level, which instantiate_template doesn't do because
17324 functions and variables have sufficient context established by the
17325 callers. */
17326
17327 static tree
17328 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
17329 {
17330 struct pending_template *old_last_pend = last_pending_template;
17331 struct tinst_level *old_error_tinst = last_error_tinst_level;
17332 if (tmpl == error_mark_node || args == error_mark_node)
17333 return error_mark_node;
17334 tree tinst = build_tree_list (tmpl, args);
17335 if (!push_tinst_level (tinst))
17336 {
17337 ggc_free (tinst);
17338 return error_mark_node;
17339 }
17340
17341 args =
17342 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
17343 args, tmpl, complain,
17344 /*require_all_args=*/true,
17345 /*use_default_args=*/true);
17346
17347 tree r = instantiate_template (tmpl, args, complain);
17348 pop_tinst_level ();
17349 /* We can't free this if a pending_template entry or last_error_tinst_level
17350 is pointing at it. */
17351 if (last_pending_template == old_last_pend
17352 && last_error_tinst_level == old_error_tinst)
17353 ggc_free (tinst);
17354
17355 return r;
17356 }
17357
17358 /* PARM is a template parameter pack for FN. Returns true iff
17359 PARM is used in a deducible way in the argument list of FN. */
17360
17361 static bool
17362 pack_deducible_p (tree parm, tree fn)
17363 {
17364 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
17365 for (; t; t = TREE_CHAIN (t))
17366 {
17367 tree type = TREE_VALUE (t);
17368 tree packs;
17369 if (!PACK_EXPANSION_P (type))
17370 continue;
17371 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
17372 packs; packs = TREE_CHAIN (packs))
17373 if (template_args_equal (TREE_VALUE (packs), parm))
17374 {
17375 /* The template parameter pack is used in a function parameter
17376 pack. If this is the end of the parameter list, the
17377 template parameter pack is deducible. */
17378 if (TREE_CHAIN (t) == void_list_node)
17379 return true;
17380 else
17381 /* Otherwise, not. Well, it could be deduced from
17382 a non-pack parameter, but doing so would end up with
17383 a deduction mismatch, so don't bother. */
17384 return false;
17385 }
17386 }
17387 /* The template parameter pack isn't used in any function parameter
17388 packs, but it might be used deeper, e.g. tuple<Args...>. */
17389 return true;
17390 }
17391
17392 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17393 NARGS elements of the arguments that are being used when calling
17394 it. TARGS is a vector into which the deduced template arguments
17395 are placed.
17396
17397 Returns either a FUNCTION_DECL for the matching specialization of FN or
17398 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17399 true, diagnostics will be printed to explain why it failed.
17400
17401 If FN is a conversion operator, or we are trying to produce a specific
17402 specialization, RETURN_TYPE is the return type desired.
17403
17404 The EXPLICIT_TARGS are explicit template arguments provided via a
17405 template-id.
17406
17407 The parameter STRICT is one of:
17408
17409 DEDUCE_CALL:
17410 We are deducing arguments for a function call, as in
17411 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17412 deducing arguments for a call to the result of a conversion
17413 function template, as in [over.call.object].
17414
17415 DEDUCE_CONV:
17416 We are deducing arguments for a conversion function, as in
17417 [temp.deduct.conv].
17418
17419 DEDUCE_EXACT:
17420 We are deducing arguments when doing an explicit instantiation
17421 as in [temp.explicit], when determining an explicit specialization
17422 as in [temp.expl.spec], or when taking the address of a function
17423 template, as in [temp.deduct.funcaddr]. */
17424
17425 tree
17426 fn_type_unification (tree fn,
17427 tree explicit_targs,
17428 tree targs,
17429 const tree *args,
17430 unsigned int nargs,
17431 tree return_type,
17432 unification_kind_t strict,
17433 int flags,
17434 bool explain_p,
17435 bool decltype_p)
17436 {
17437 tree parms;
17438 tree fntype;
17439 tree decl = NULL_TREE;
17440 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17441 bool ok;
17442 static int deduction_depth;
17443 struct pending_template *old_last_pend = last_pending_template;
17444 struct tinst_level *old_error_tinst = last_error_tinst_level;
17445 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17446 tree tinst;
17447 tree r = error_mark_node;
17448
17449 if (decltype_p)
17450 complain |= tf_decltype;
17451
17452 /* In C++0x, it's possible to have a function template whose type depends
17453 on itself recursively. This is most obvious with decltype, but can also
17454 occur with enumeration scope (c++/48969). So we need to catch infinite
17455 recursion and reject the substitution at deduction time; this function
17456 will return error_mark_node for any repeated substitution.
17457
17458 This also catches excessive recursion such as when f<N> depends on
17459 f<N-1> across all integers, and returns error_mark_node for all the
17460 substitutions back up to the initial one.
17461
17462 This is, of course, not reentrant. */
17463 if (excessive_deduction_depth)
17464 return error_mark_node;
17465 tinst = build_tree_list (fn, NULL_TREE);
17466 ++deduction_depth;
17467
17468 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
17469
17470 fntype = TREE_TYPE (fn);
17471 if (explicit_targs)
17472 {
17473 /* [temp.deduct]
17474
17475 The specified template arguments must match the template
17476 parameters in kind (i.e., type, nontype, template), and there
17477 must not be more arguments than there are parameters;
17478 otherwise type deduction fails.
17479
17480 Nontype arguments must match the types of the corresponding
17481 nontype template parameters, or must be convertible to the
17482 types of the corresponding nontype parameters as specified in
17483 _temp.arg.nontype_, otherwise type deduction fails.
17484
17485 All references in the function type of the function template
17486 to the corresponding template parameters are replaced by the
17487 specified template argument values. If a substitution in a
17488 template parameter or in the function type of the function
17489 template results in an invalid type, type deduction fails. */
17490 int i, len = TREE_VEC_LENGTH (tparms);
17491 location_t loc = input_location;
17492 bool incomplete = false;
17493
17494 /* Adjust any explicit template arguments before entering the
17495 substitution context. */
17496 explicit_targs
17497 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
17498 complain,
17499 /*require_all_args=*/false,
17500 /*use_default_args=*/false));
17501 if (explicit_targs == error_mark_node)
17502 goto fail;
17503
17504 /* Substitute the explicit args into the function type. This is
17505 necessary so that, for instance, explicitly declared function
17506 arguments can match null pointed constants. If we were given
17507 an incomplete set of explicit args, we must not do semantic
17508 processing during substitution as we could create partial
17509 instantiations. */
17510 for (i = 0; i < len; i++)
17511 {
17512 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17513 bool parameter_pack = false;
17514 tree targ = TREE_VEC_ELT (explicit_targs, i);
17515
17516 /* Dig out the actual parm. */
17517 if (TREE_CODE (parm) == TYPE_DECL
17518 || TREE_CODE (parm) == TEMPLATE_DECL)
17519 {
17520 parm = TREE_TYPE (parm);
17521 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
17522 }
17523 else if (TREE_CODE (parm) == PARM_DECL)
17524 {
17525 parm = DECL_INITIAL (parm);
17526 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
17527 }
17528
17529 if (!parameter_pack && targ == NULL_TREE)
17530 /* No explicit argument for this template parameter. */
17531 incomplete = true;
17532
17533 if (parameter_pack && pack_deducible_p (parm, fn))
17534 {
17535 /* Mark the argument pack as "incomplete". We could
17536 still deduce more arguments during unification.
17537 We remove this mark in type_unification_real. */
17538 if (targ)
17539 {
17540 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
17541 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
17542 = ARGUMENT_PACK_ARGS (targ);
17543 }
17544
17545 /* We have some incomplete argument packs. */
17546 incomplete = true;
17547 }
17548 }
17549
17550 TREE_VALUE (tinst) = explicit_targs;
17551 if (!push_tinst_level (tinst))
17552 {
17553 excessive_deduction_depth = true;
17554 goto fail;
17555 }
17556 processing_template_decl += incomplete;
17557 input_location = DECL_SOURCE_LOCATION (fn);
17558 /* Ignore any access checks; we'll see them again in
17559 instantiate_template and they might have the wrong
17560 access path at this point. */
17561 push_deferring_access_checks (dk_deferred);
17562 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
17563 complain | tf_partial, NULL_TREE);
17564 pop_deferring_access_checks ();
17565 input_location = loc;
17566 processing_template_decl -= incomplete;
17567 pop_tinst_level ();
17568
17569 if (fntype == error_mark_node)
17570 goto fail;
17571
17572 /* Place the explicitly specified arguments in TARGS. */
17573 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
17574 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
17575 }
17576
17577 /* Never do unification on the 'this' parameter. */
17578 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
17579
17580 if (return_type && strict == DEDUCE_CALL)
17581 {
17582 /* We're deducing for a call to the result of a template conversion
17583 function. The parms we really want are in return_type. */
17584 if (POINTER_TYPE_P (return_type))
17585 return_type = TREE_TYPE (return_type);
17586 parms = TYPE_ARG_TYPES (return_type);
17587 }
17588 else if (return_type)
17589 {
17590 tree *new_args;
17591
17592 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
17593 new_args = XALLOCAVEC (tree, nargs + 1);
17594 new_args[0] = return_type;
17595 memcpy (new_args + 1, args, nargs * sizeof (tree));
17596 args = new_args;
17597 ++nargs;
17598 }
17599
17600 /* We allow incomplete unification without an error message here
17601 because the standard doesn't seem to explicitly prohibit it. Our
17602 callers must be ready to deal with unification failures in any
17603 event. */
17604
17605 TREE_VALUE (tinst) = targs;
17606 /* If we aren't explaining yet, push tinst context so we can see where
17607 any errors (e.g. from class instantiations triggered by instantiation
17608 of default template arguments) come from. If we are explaining, this
17609 context is redundant. */
17610 if (!explain_p && !push_tinst_level (tinst))
17611 {
17612 excessive_deduction_depth = true;
17613 goto fail;
17614 }
17615
17616 /* type_unification_real will pass back any access checks from default
17617 template argument substitution. */
17618 vec<deferred_access_check, va_gc> *checks;
17619 checks = NULL;
17620
17621 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17622 targs, parms, args, nargs, /*subr=*/0,
17623 strict, flags, &checks, explain_p);
17624 if (!explain_p)
17625 pop_tinst_level ();
17626 if (!ok)
17627 goto fail;
17628
17629 /* Now that we have bindings for all of the template arguments,
17630 ensure that the arguments deduced for the template template
17631 parameters have compatible template parameter lists. We cannot
17632 check this property before we have deduced all template
17633 arguments, because the template parameter types of a template
17634 template parameter might depend on prior template parameters
17635 deduced after the template template parameter. The following
17636 ill-formed example illustrates this issue:
17637
17638 template<typename T, template<T> class C> void f(C<5>, T);
17639
17640 template<int N> struct X {};
17641
17642 void g() {
17643 f(X<5>(), 5l); // error: template argument deduction fails
17644 }
17645
17646 The template parameter list of 'C' depends on the template type
17647 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17648 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17649 time that we deduce 'C'. */
17650 if (!template_template_parm_bindings_ok_p
17651 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
17652 {
17653 unify_inconsistent_template_template_parameters (explain_p);
17654 goto fail;
17655 }
17656
17657 /* All is well so far. Now, check:
17658
17659 [temp.deduct]
17660
17661 When all template arguments have been deduced, all uses of
17662 template parameters in nondeduced contexts are replaced with
17663 the corresponding deduced argument values. If the
17664 substitution results in an invalid type, as described above,
17665 type deduction fails. */
17666 TREE_VALUE (tinst) = targs;
17667 if (!push_tinst_level (tinst))
17668 {
17669 excessive_deduction_depth = true;
17670 goto fail;
17671 }
17672
17673 /* Also collect access checks from the instantiation. */
17674 reopen_deferring_access_checks (checks);
17675
17676 decl = instantiate_template (fn, targs, complain);
17677
17678 checks = get_deferred_access_checks ();
17679 pop_deferring_access_checks ();
17680
17681 pop_tinst_level ();
17682
17683 if (decl == error_mark_node)
17684 goto fail;
17685
17686 /* Now perform any access checks encountered during substitution. */
17687 push_access_scope (decl);
17688 ok = perform_access_checks (checks, complain);
17689 pop_access_scope (decl);
17690 if (!ok)
17691 goto fail;
17692
17693 /* If we're looking for an exact match, check that what we got
17694 is indeed an exact match. It might not be if some template
17695 parameters are used in non-deduced contexts. But don't check
17696 for an exact match if we have dependent template arguments;
17697 in that case we're doing partial ordering, and we already know
17698 that we have two candidates that will provide the actual type. */
17699 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
17700 {
17701 tree substed = TREE_TYPE (decl);
17702 unsigned int i;
17703
17704 tree sarg
17705 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
17706 if (return_type)
17707 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
17708 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
17709 if (!same_type_p (args[i], TREE_VALUE (sarg)))
17710 {
17711 unify_type_mismatch (explain_p, args[i],
17712 TREE_VALUE (sarg));
17713 goto fail;
17714 }
17715 }
17716
17717 r = decl;
17718
17719 fail:
17720 --deduction_depth;
17721 if (excessive_deduction_depth)
17722 {
17723 if (deduction_depth == 0)
17724 /* Reset once we're all the way out. */
17725 excessive_deduction_depth = false;
17726 }
17727
17728 /* We can't free this if a pending_template entry or last_error_tinst_level
17729 is pointing at it. */
17730 if (last_pending_template == old_last_pend
17731 && last_error_tinst_level == old_error_tinst)
17732 ggc_free (tinst);
17733
17734 return r;
17735 }
17736
17737 /* TYPE is the type of a function parameter. If TYPE is a (dependent)
17738 ARRAY_TYPE, return the corresponding POINTER_TYPE to which it decays.
17739 Otherwise return TYPE. (We shouldn't see non-dependent ARRAY_TYPE
17740 parameters because they get decayed as soon as they are declared.) */
17741
17742 static tree
17743 decay_dependent_array_parm_type (tree type)
17744 {
17745 if (TREE_CODE (type) == ARRAY_TYPE)
17746 {
17747 gcc_assert (uses_template_parms (type));
17748 return type_decays_to (type);
17749 }
17750
17751 return type;
17752 }
17753
17754 /* Adjust types before performing type deduction, as described in
17755 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17756 sections are symmetric. PARM is the type of a function parameter
17757 or the return type of the conversion function. ARG is the type of
17758 the argument passed to the call, or the type of the value
17759 initialized with the result of the conversion function.
17760 ARG_EXPR is the original argument expression, which may be null. */
17761
17762 static int
17763 maybe_adjust_types_for_deduction (unification_kind_t strict,
17764 tree* parm,
17765 tree* arg,
17766 tree arg_expr)
17767 {
17768 int result = 0;
17769
17770 switch (strict)
17771 {
17772 case DEDUCE_CALL:
17773 break;
17774
17775 case DEDUCE_CONV:
17776 /* Swap PARM and ARG throughout the remainder of this
17777 function; the handling is precisely symmetric since PARM
17778 will initialize ARG rather than vice versa. */
17779 std::swap (parm, arg);
17780 break;
17781
17782 case DEDUCE_EXACT:
17783 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17784 too, but here handle it by stripping the reference from PARM
17785 rather than by adding it to ARG. */
17786 if (TREE_CODE (*parm) == REFERENCE_TYPE
17787 && TYPE_REF_IS_RVALUE (*parm)
17788 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17789 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17790 && TREE_CODE (*arg) == REFERENCE_TYPE
17791 && !TYPE_REF_IS_RVALUE (*arg))
17792 *parm = TREE_TYPE (*parm);
17793 /* Nothing else to do in this case. */
17794 return 0;
17795
17796 default:
17797 gcc_unreachable ();
17798 }
17799
17800 if (TREE_CODE (*parm) != REFERENCE_TYPE)
17801 {
17802 /* [temp.deduct.call]
17803
17804 If P is not a reference type:
17805
17806 --If A is an array type, the pointer type produced by the
17807 array-to-pointer standard conversion (_conv.array_) is
17808 used in place of A for type deduction; otherwise,
17809
17810 --If A is a function type, the pointer type produced by
17811 the function-to-pointer standard conversion
17812 (_conv.func_) is used in place of A for type deduction;
17813 otherwise,
17814
17815 --If A is a cv-qualified type, the top level
17816 cv-qualifiers of A's type are ignored for type
17817 deduction. */
17818 if (TREE_CODE (*arg) == ARRAY_TYPE)
17819 *arg = build_pointer_type (TREE_TYPE (*arg));
17820 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
17821 *arg = build_pointer_type (*arg);
17822 else
17823 *arg = TYPE_MAIN_VARIANT (*arg);
17824 }
17825
17826 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17827 of the form T&&, where T is a template parameter, and the argument
17828 is an lvalue, T is deduced as A& */
17829 if (TREE_CODE (*parm) == REFERENCE_TYPE
17830 && TYPE_REF_IS_RVALUE (*parm)
17831 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17832 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17833 && (arg_expr ? real_lvalue_p (arg_expr)
17834 /* try_one_overload doesn't provide an arg_expr, but
17835 functions are always lvalues. */
17836 : TREE_CODE (*arg) == FUNCTION_TYPE))
17837 *arg = build_reference_type (*arg);
17838
17839 /* [temp.deduct.call]
17840
17841 If P is a cv-qualified type, the top level cv-qualifiers
17842 of P's type are ignored for type deduction. If P is a
17843 reference type, the type referred to by P is used for
17844 type deduction. */
17845 *parm = TYPE_MAIN_VARIANT (*parm);
17846 if (TREE_CODE (*parm) == REFERENCE_TYPE)
17847 {
17848 *parm = TREE_TYPE (*parm);
17849 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17850 }
17851
17852 /* DR 322. For conversion deduction, remove a reference type on parm
17853 too (which has been swapped into ARG). */
17854 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
17855 *arg = TREE_TYPE (*arg);
17856
17857 return result;
17858 }
17859
17860 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17861 template which does contain any deducible template parameters; check if
17862 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17863 unify_one_argument. */
17864
17865 static int
17866 check_non_deducible_conversion (tree parm, tree arg, int strict,
17867 int flags, bool explain_p)
17868 {
17869 tree type;
17870
17871 if (!TYPE_P (arg))
17872 type = TREE_TYPE (arg);
17873 else
17874 type = arg;
17875
17876 if (same_type_p (parm, type))
17877 return unify_success (explain_p);
17878
17879 if (strict == DEDUCE_CONV)
17880 {
17881 if (can_convert_arg (type, parm, NULL_TREE, flags,
17882 explain_p ? tf_warning_or_error : tf_none))
17883 return unify_success (explain_p);
17884 }
17885 else if (strict != DEDUCE_EXACT)
17886 {
17887 if (can_convert_arg (parm, type,
17888 TYPE_P (arg) ? NULL_TREE : arg,
17889 flags, explain_p ? tf_warning_or_error : tf_none))
17890 return unify_success (explain_p);
17891 }
17892
17893 if (strict == DEDUCE_EXACT)
17894 return unify_type_mismatch (explain_p, parm, arg);
17895 else
17896 return unify_arg_conversion (explain_p, parm, type, arg);
17897 }
17898
17899 static bool uses_deducible_template_parms (tree type);
17900
17901 /* Returns true iff the expression EXPR is one from which a template
17902 argument can be deduced. In other words, if it's an undecorated
17903 use of a template non-type parameter. */
17904
17905 static bool
17906 deducible_expression (tree expr)
17907 {
17908 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
17909 }
17910
17911 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17912 deducible way; that is, if it has a max value of <PARM> - 1. */
17913
17914 static bool
17915 deducible_array_bound (tree domain)
17916 {
17917 if (domain == NULL_TREE)
17918 return false;
17919
17920 tree max = TYPE_MAX_VALUE (domain);
17921 if (TREE_CODE (max) != MINUS_EXPR)
17922 return false;
17923
17924 return deducible_expression (TREE_OPERAND (max, 0));
17925 }
17926
17927 /* Returns true iff the template arguments ARGS use a template parameter
17928 in a deducible way. */
17929
17930 static bool
17931 deducible_template_args (tree args)
17932 {
17933 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
17934 {
17935 bool deducible;
17936 tree elt = TREE_VEC_ELT (args, i);
17937 if (ARGUMENT_PACK_P (elt))
17938 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
17939 else
17940 {
17941 if (PACK_EXPANSION_P (elt))
17942 elt = PACK_EXPANSION_PATTERN (elt);
17943 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
17944 deducible = true;
17945 else if (TYPE_P (elt))
17946 deducible = uses_deducible_template_parms (elt);
17947 else
17948 deducible = deducible_expression (elt);
17949 }
17950 if (deducible)
17951 return true;
17952 }
17953 return false;
17954 }
17955
17956 /* Returns true iff TYPE contains any deducible references to template
17957 parameters, as per 14.8.2.5. */
17958
17959 static bool
17960 uses_deducible_template_parms (tree type)
17961 {
17962 if (PACK_EXPANSION_P (type))
17963 type = PACK_EXPANSION_PATTERN (type);
17964
17965 /* T
17966 cv-list T
17967 TT<T>
17968 TT<i>
17969 TT<> */
17970 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17971 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17972 return true;
17973
17974 /* T*
17975 T&
17976 T&& */
17977 if (POINTER_TYPE_P (type))
17978 return uses_deducible_template_parms (TREE_TYPE (type));
17979
17980 /* T[integer-constant ]
17981 type [i] */
17982 if (TREE_CODE (type) == ARRAY_TYPE)
17983 return (uses_deducible_template_parms (TREE_TYPE (type))
17984 || deducible_array_bound (TYPE_DOMAIN (type)));
17985
17986 /* T type ::*
17987 type T::*
17988 T T::*
17989 T (type ::*)()
17990 type (T::*)()
17991 type (type ::*)(T)
17992 type (T::*)(T)
17993 T (type ::*)(T)
17994 T (T::*)()
17995 T (T::*)(T) */
17996 if (TYPE_PTRMEM_P (type))
17997 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
17998 || (uses_deducible_template_parms
17999 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18000
18001 /* template-name <T> (where template-name refers to a class template)
18002 template-name <i> (where template-name refers to a class template) */
18003 if (CLASS_TYPE_P (type)
18004 && CLASSTYPE_TEMPLATE_INFO (type)
18005 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18006 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18007 (CLASSTYPE_TI_ARGS (type)));
18008
18009 /* type (T)
18010 T()
18011 T(T) */
18012 if (TREE_CODE (type) == FUNCTION_TYPE
18013 || TREE_CODE (type) == METHOD_TYPE)
18014 {
18015 if (uses_deducible_template_parms (TREE_TYPE (type)))
18016 return true;
18017 tree parm = TYPE_ARG_TYPES (type);
18018 if (TREE_CODE (type) == METHOD_TYPE)
18019 parm = TREE_CHAIN (parm);
18020 for (; parm; parm = TREE_CHAIN (parm))
18021 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18022 return true;
18023 }
18024
18025 return false;
18026 }
18027
18028 /* Subroutine of type_unification_real and unify_pack_expansion to
18029 handle unification of a single P/A pair. Parameters are as
18030 for those functions. */
18031
18032 static int
18033 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18034 int subr, unification_kind_t strict,
18035 bool explain_p)
18036 {
18037 tree arg_expr = NULL_TREE;
18038 int arg_strict;
18039
18040 if (arg == error_mark_node || parm == error_mark_node)
18041 return unify_invalid (explain_p);
18042 if (arg == unknown_type_node)
18043 /* We can't deduce anything from this, but we might get all the
18044 template args from other function args. */
18045 return unify_success (explain_p);
18046
18047 /* Implicit conversions (Clause 4) will be performed on a function
18048 argument to convert it to the type of the corresponding function
18049 parameter if the parameter type contains no template-parameters that
18050 participate in template argument deduction. */
18051 if (strict != DEDUCE_EXACT
18052 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18053 /* For function parameters with no deducible template parameters,
18054 just return. We'll check non-dependent conversions later. */
18055 return unify_success (explain_p);
18056
18057 switch (strict)
18058 {
18059 case DEDUCE_CALL:
18060 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18061 | UNIFY_ALLOW_MORE_CV_QUAL
18062 | UNIFY_ALLOW_DERIVED);
18063 break;
18064
18065 case DEDUCE_CONV:
18066 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18067 break;
18068
18069 case DEDUCE_EXACT:
18070 arg_strict = UNIFY_ALLOW_NONE;
18071 break;
18072
18073 default:
18074 gcc_unreachable ();
18075 }
18076
18077 /* We only do these transformations if this is the top-level
18078 parameter_type_list in a call or declaration matching; in other
18079 situations (nested function declarators, template argument lists) we
18080 won't be comparing a type to an expression, and we don't do any type
18081 adjustments. */
18082 if (!subr)
18083 {
18084 if (!TYPE_P (arg))
18085 {
18086 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18087 if (type_unknown_p (arg))
18088 {
18089 /* [temp.deduct.type] A template-argument can be
18090 deduced from a pointer to function or pointer
18091 to member function argument if the set of
18092 overloaded functions does not contain function
18093 templates and at most one of a set of
18094 overloaded functions provides a unique
18095 match. */
18096
18097 if (resolve_overloaded_unification
18098 (tparms, targs, parm, arg, strict,
18099 arg_strict, explain_p))
18100 return unify_success (explain_p);
18101 return unify_overload_resolution_failure (explain_p, arg);
18102 }
18103
18104 arg_expr = arg;
18105 arg = unlowered_expr_type (arg);
18106 if (arg == error_mark_node)
18107 return unify_invalid (explain_p);
18108 }
18109
18110 arg_strict |=
18111 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18112 }
18113 else
18114 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18115 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18116 return unify_template_argument_mismatch (explain_p, parm, arg);
18117
18118 /* For deduction from an init-list we need the actual list. */
18119 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18120 arg = arg_expr;
18121 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18122 }
18123
18124 /* Most parms like fn_type_unification.
18125
18126 If SUBR is 1, we're being called recursively (to unify the
18127 arguments of a function or method parameter of a function
18128 template).
18129
18130 CHECKS is a pointer to a vector of access checks encountered while
18131 substituting default template arguments. */
18132
18133 static int
18134 type_unification_real (tree tparms,
18135 tree targs,
18136 tree xparms,
18137 const tree *xargs,
18138 unsigned int xnargs,
18139 int subr,
18140 unification_kind_t strict,
18141 int flags,
18142 vec<deferred_access_check, va_gc> **checks,
18143 bool explain_p)
18144 {
18145 tree parm, arg;
18146 int i;
18147 int ntparms = TREE_VEC_LENGTH (tparms);
18148 int saw_undeduced = 0;
18149 tree parms;
18150 const tree *args;
18151 unsigned int nargs;
18152 unsigned int ia;
18153
18154 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
18155 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
18156 gcc_assert (ntparms > 0);
18157
18158 /* Reset the number of non-defaulted template arguments contained
18159 in TARGS. */
18160 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
18161
18162 again:
18163 parms = xparms;
18164 args = xargs;
18165 nargs = xnargs;
18166
18167 ia = 0;
18168 while (parms && parms != void_list_node
18169 && ia < nargs)
18170 {
18171 parm = TREE_VALUE (parms);
18172
18173 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18174 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
18175 /* For a function parameter pack that occurs at the end of the
18176 parameter-declaration-list, the type A of each remaining
18177 argument of the call is compared with the type P of the
18178 declarator-id of the function parameter pack. */
18179 break;
18180
18181 parms = TREE_CHAIN (parms);
18182
18183 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18184 /* For a function parameter pack that does not occur at the
18185 end of the parameter-declaration-list, the type of the
18186 parameter pack is a non-deduced context. */
18187 continue;
18188
18189 arg = args[ia];
18190 ++ia;
18191
18192 parm = decay_dependent_array_parm_type (parm);
18193
18194 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
18195 explain_p))
18196 return 1;
18197 }
18198
18199 if (parms
18200 && parms != void_list_node
18201 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
18202 {
18203 /* Unify the remaining arguments with the pack expansion type. */
18204 tree argvec;
18205 tree parmvec = make_tree_vec (1);
18206
18207 /* Allocate a TREE_VEC and copy in all of the arguments */
18208 argvec = make_tree_vec (nargs - ia);
18209 for (i = 0; ia < nargs; ++ia, ++i)
18210 TREE_VEC_ELT (argvec, i) = args[ia];
18211
18212 /* Copy the parameter into parmvec. */
18213 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
18214 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
18215 /*subr=*/subr, explain_p))
18216 return 1;
18217
18218 /* Advance to the end of the list of parameters. */
18219 parms = TREE_CHAIN (parms);
18220 }
18221
18222 /* Fail if we've reached the end of the parm list, and more args
18223 are present, and the parm list isn't variadic. */
18224 if (ia < nargs && parms == void_list_node)
18225 return unify_too_many_arguments (explain_p, nargs, ia);
18226 /* Fail if parms are left and they don't have default values and
18227 they aren't all deduced as empty packs (c++/57397). This is
18228 consistent with sufficient_parms_p. */
18229 if (parms && parms != void_list_node
18230 && TREE_PURPOSE (parms) == NULL_TREE)
18231 {
18232 unsigned int count = nargs;
18233 tree p = parms;
18234 bool type_pack_p;
18235 do
18236 {
18237 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
18238 if (!type_pack_p)
18239 count++;
18240 p = TREE_CHAIN (p);
18241 }
18242 while (p && p != void_list_node);
18243 if (count != nargs)
18244 return unify_too_few_arguments (explain_p, ia, count,
18245 type_pack_p);
18246 }
18247
18248 if (!subr)
18249 {
18250 tsubst_flags_t complain = (explain_p
18251 ? tf_warning_or_error
18252 : tf_none);
18253
18254 for (i = 0; i < ntparms; i++)
18255 {
18256 tree targ = TREE_VEC_ELT (targs, i);
18257 tree tparm = TREE_VEC_ELT (tparms, i);
18258
18259 /* Clear the "incomplete" flags on all argument packs now so that
18260 substituting them into later default arguments works. */
18261 if (targ && ARGUMENT_PACK_P (targ))
18262 {
18263 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
18264 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
18265 }
18266
18267 if (targ || tparm == error_mark_node)
18268 continue;
18269 tparm = TREE_VALUE (tparm);
18270
18271 /* If this is an undeduced nontype parameter that depends on
18272 a type parameter, try another pass; its type may have been
18273 deduced from a later argument than the one from which
18274 this parameter can be deduced. */
18275 if (TREE_CODE (tparm) == PARM_DECL
18276 && uses_template_parms (TREE_TYPE (tparm))
18277 && saw_undeduced < 2)
18278 {
18279 saw_undeduced = 1;
18280 continue;
18281 }
18282
18283 /* Core issue #226 (C++0x) [temp.deduct]:
18284
18285 If a template argument has not been deduced, its
18286 default template argument, if any, is used.
18287
18288 When we are in C++98 mode, TREE_PURPOSE will either
18289 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18290 to explicitly check cxx_dialect here. */
18291 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
18292 /* OK, there is a default argument. Wait until after the
18293 conversion check to do substitution. */
18294 continue;
18295
18296 /* If the type parameter is a parameter pack, then it will
18297 be deduced to an empty parameter pack. */
18298 if (template_parameter_pack_p (tparm))
18299 {
18300 tree arg;
18301
18302 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
18303 {
18304 arg = make_node (NONTYPE_ARGUMENT_PACK);
18305 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
18306 TREE_CONSTANT (arg) = 1;
18307 }
18308 else
18309 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
18310
18311 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
18312
18313 TREE_VEC_ELT (targs, i) = arg;
18314 continue;
18315 }
18316
18317 return unify_parameter_deduction_failure (explain_p, tparm);
18318 }
18319
18320 /* DR 1391: All parameters have args, now check non-dependent parms for
18321 convertibility. */
18322 if (saw_undeduced < 2)
18323 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
18324 parms && parms != void_list_node && ia < nargs; )
18325 {
18326 parm = TREE_VALUE (parms);
18327
18328 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18329 && (!TREE_CHAIN (parms)
18330 || TREE_CHAIN (parms) == void_list_node))
18331 /* For a function parameter pack that occurs at the end of the
18332 parameter-declaration-list, the type A of each remaining
18333 argument of the call is compared with the type P of the
18334 declarator-id of the function parameter pack. */
18335 break;
18336
18337 parms = TREE_CHAIN (parms);
18338
18339 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18340 /* For a function parameter pack that does not occur at the
18341 end of the parameter-declaration-list, the type of the
18342 parameter pack is a non-deduced context. */
18343 continue;
18344
18345 arg = args[ia];
18346 ++ia;
18347
18348 if (uses_template_parms (parm))
18349 continue;
18350 if (check_non_deducible_conversion (parm, arg, strict, flags,
18351 explain_p))
18352 return 1;
18353 }
18354
18355 /* Now substitute into the default template arguments. */
18356 for (i = 0; i < ntparms; i++)
18357 {
18358 tree targ = TREE_VEC_ELT (targs, i);
18359 tree tparm = TREE_VEC_ELT (tparms, i);
18360
18361 if (targ || tparm == error_mark_node)
18362 continue;
18363 tree parm = TREE_VALUE (tparm);
18364
18365 if (TREE_CODE (parm) == PARM_DECL
18366 && uses_template_parms (TREE_TYPE (parm))
18367 && saw_undeduced < 2)
18368 continue;
18369
18370 tree arg = TREE_PURPOSE (tparm);
18371 reopen_deferring_access_checks (*checks);
18372 location_t save_loc = input_location;
18373 if (DECL_P (parm))
18374 input_location = DECL_SOURCE_LOCATION (parm);
18375 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
18376 arg = convert_template_argument (parm, arg, targs, complain,
18377 i, NULL_TREE);
18378 input_location = save_loc;
18379 *checks = get_deferred_access_checks ();
18380 pop_deferring_access_checks ();
18381 if (arg == error_mark_node)
18382 return 1;
18383 else
18384 {
18385 TREE_VEC_ELT (targs, i) = arg;
18386 /* The position of the first default template argument,
18387 is also the number of non-defaulted arguments in TARGS.
18388 Record that. */
18389 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18390 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
18391 continue;
18392 }
18393 }
18394
18395 if (saw_undeduced++ == 1)
18396 goto again;
18397 }
18398
18399 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18400 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
18401
18402 return unify_success (explain_p);
18403 }
18404
18405 /* Subroutine of type_unification_real. Args are like the variables
18406 at the call site. ARG is an overloaded function (or template-id);
18407 we try deducing template args from each of the overloads, and if
18408 only one succeeds, we go with that. Modifies TARGS and returns
18409 true on success. */
18410
18411 static bool
18412 resolve_overloaded_unification (tree tparms,
18413 tree targs,
18414 tree parm,
18415 tree arg,
18416 unification_kind_t strict,
18417 int sub_strict,
18418 bool explain_p)
18419 {
18420 tree tempargs = copy_node (targs);
18421 int good = 0;
18422 tree goodfn = NULL_TREE;
18423 bool addr_p;
18424
18425 if (TREE_CODE (arg) == ADDR_EXPR)
18426 {
18427 arg = TREE_OPERAND (arg, 0);
18428 addr_p = true;
18429 }
18430 else
18431 addr_p = false;
18432
18433 if (TREE_CODE (arg) == COMPONENT_REF)
18434 /* Handle `&x' where `x' is some static or non-static member
18435 function name. */
18436 arg = TREE_OPERAND (arg, 1);
18437
18438 if (TREE_CODE (arg) == OFFSET_REF)
18439 arg = TREE_OPERAND (arg, 1);
18440
18441 /* Strip baselink information. */
18442 if (BASELINK_P (arg))
18443 arg = BASELINK_FUNCTIONS (arg);
18444
18445 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
18446 {
18447 /* If we got some explicit template args, we need to plug them into
18448 the affected templates before we try to unify, in case the
18449 explicit args will completely resolve the templates in question. */
18450
18451 int ok = 0;
18452 tree expl_subargs = TREE_OPERAND (arg, 1);
18453 arg = TREE_OPERAND (arg, 0);
18454
18455 for (; arg; arg = OVL_NEXT (arg))
18456 {
18457 tree fn = OVL_CURRENT (arg);
18458 tree subargs, elem;
18459
18460 if (TREE_CODE (fn) != TEMPLATE_DECL)
18461 continue;
18462
18463 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18464 expl_subargs, NULL_TREE, tf_none,
18465 /*require_all_args=*/true,
18466 /*use_default_args=*/true);
18467 if (subargs != error_mark_node
18468 && !any_dependent_template_arguments_p (subargs))
18469 {
18470 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
18471 if (try_one_overload (tparms, targs, tempargs, parm,
18472 elem, strict, sub_strict, addr_p, explain_p)
18473 && (!goodfn || !same_type_p (goodfn, elem)))
18474 {
18475 goodfn = elem;
18476 ++good;
18477 }
18478 }
18479 else if (subargs)
18480 ++ok;
18481 }
18482 /* If no templates (or more than one) are fully resolved by the
18483 explicit arguments, this template-id is a non-deduced context; it
18484 could still be OK if we deduce all template arguments for the
18485 enclosing call through other arguments. */
18486 if (good != 1)
18487 good = ok;
18488 }
18489 else if (TREE_CODE (arg) != OVERLOAD
18490 && TREE_CODE (arg) != FUNCTION_DECL)
18491 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18492 -- but the deduction does not succeed because the expression is
18493 not just the function on its own. */
18494 return false;
18495 else
18496 for (; arg; arg = OVL_NEXT (arg))
18497 if (try_one_overload (tparms, targs, tempargs, parm,
18498 TREE_TYPE (OVL_CURRENT (arg)),
18499 strict, sub_strict, addr_p, explain_p)
18500 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
18501 {
18502 goodfn = OVL_CURRENT (arg);
18503 ++good;
18504 }
18505
18506 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18507 to function or pointer to member function argument if the set of
18508 overloaded functions does not contain function templates and at most
18509 one of a set of overloaded functions provides a unique match.
18510
18511 So if we found multiple possibilities, we return success but don't
18512 deduce anything. */
18513
18514 if (good == 1)
18515 {
18516 int i = TREE_VEC_LENGTH (targs);
18517 for (; i--; )
18518 if (TREE_VEC_ELT (tempargs, i))
18519 {
18520 tree old = TREE_VEC_ELT (targs, i);
18521 tree new_ = TREE_VEC_ELT (tempargs, i);
18522 if (new_ && old && ARGUMENT_PACK_P (old)
18523 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
18524 /* Don't forget explicit template arguments in a pack. */
18525 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
18526 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
18527 TREE_VEC_ELT (targs, i) = new_;
18528 }
18529 }
18530 if (good)
18531 return true;
18532
18533 return false;
18534 }
18535
18536 /* Core DR 115: In contexts where deduction is done and fails, or in
18537 contexts where deduction is not done, if a template argument list is
18538 specified and it, along with any default template arguments, identifies
18539 a single function template specialization, then the template-id is an
18540 lvalue for the function template specialization. */
18541
18542 tree
18543 resolve_nondeduced_context (tree orig_expr)
18544 {
18545 tree expr, offset, baselink;
18546 bool addr;
18547
18548 if (!type_unknown_p (orig_expr))
18549 return orig_expr;
18550
18551 expr = orig_expr;
18552 addr = false;
18553 offset = NULL_TREE;
18554 baselink = NULL_TREE;
18555
18556 if (TREE_CODE (expr) == ADDR_EXPR)
18557 {
18558 expr = TREE_OPERAND (expr, 0);
18559 addr = true;
18560 }
18561 if (TREE_CODE (expr) == OFFSET_REF)
18562 {
18563 offset = expr;
18564 expr = TREE_OPERAND (expr, 1);
18565 }
18566 if (BASELINK_P (expr))
18567 {
18568 baselink = expr;
18569 expr = BASELINK_FUNCTIONS (expr);
18570 }
18571
18572 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
18573 {
18574 int good = 0;
18575 tree goodfn = NULL_TREE;
18576
18577 /* If we got some explicit template args, we need to plug them into
18578 the affected templates before we try to unify, in case the
18579 explicit args will completely resolve the templates in question. */
18580
18581 tree expl_subargs = TREE_OPERAND (expr, 1);
18582 tree arg = TREE_OPERAND (expr, 0);
18583 tree badfn = NULL_TREE;
18584 tree badargs = NULL_TREE;
18585
18586 for (; arg; arg = OVL_NEXT (arg))
18587 {
18588 tree fn = OVL_CURRENT (arg);
18589 tree subargs, elem;
18590
18591 if (TREE_CODE (fn) != TEMPLATE_DECL)
18592 continue;
18593
18594 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18595 expl_subargs, NULL_TREE, tf_none,
18596 /*require_all_args=*/true,
18597 /*use_default_args=*/true);
18598 if (subargs != error_mark_node
18599 && !any_dependent_template_arguments_p (subargs))
18600 {
18601 elem = instantiate_template (fn, subargs, tf_none);
18602 if (elem == error_mark_node)
18603 {
18604 badfn = fn;
18605 badargs = subargs;
18606 }
18607 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
18608 {
18609 goodfn = elem;
18610 ++good;
18611 }
18612 }
18613 }
18614 if (good == 1)
18615 {
18616 mark_used (goodfn);
18617 expr = goodfn;
18618 if (baselink)
18619 expr = build_baselink (BASELINK_BINFO (baselink),
18620 BASELINK_ACCESS_BINFO (baselink),
18621 expr, BASELINK_OPTYPE (baselink));
18622 if (offset)
18623 {
18624 tree base
18625 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
18626 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
18627 }
18628 if (addr)
18629 expr = cp_build_addr_expr (expr, tf_warning_or_error);
18630 return expr;
18631 }
18632 else if (good == 0 && badargs)
18633 /* There were no good options and at least one bad one, so let the
18634 user know what the problem is. */
18635 instantiate_template (badfn, badargs, tf_warning_or_error);
18636 }
18637 return orig_expr;
18638 }
18639
18640 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18641 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18642 different overloads deduce different arguments for a given parm.
18643 ADDR_P is true if the expression for which deduction is being
18644 performed was of the form "& fn" rather than simply "fn".
18645
18646 Returns 1 on success. */
18647
18648 static int
18649 try_one_overload (tree tparms,
18650 tree orig_targs,
18651 tree targs,
18652 tree parm,
18653 tree arg,
18654 unification_kind_t strict,
18655 int sub_strict,
18656 bool addr_p,
18657 bool explain_p)
18658 {
18659 int nargs;
18660 tree tempargs;
18661 int i;
18662
18663 if (arg == error_mark_node)
18664 return 0;
18665
18666 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18667 to function or pointer to member function argument if the set of
18668 overloaded functions does not contain function templates and at most
18669 one of a set of overloaded functions provides a unique match.
18670
18671 So if this is a template, just return success. */
18672
18673 if (uses_template_parms (arg))
18674 return 1;
18675
18676 if (TREE_CODE (arg) == METHOD_TYPE)
18677 arg = build_ptrmemfunc_type (build_pointer_type (arg));
18678 else if (addr_p)
18679 arg = build_pointer_type (arg);
18680
18681 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
18682
18683 /* We don't copy orig_targs for this because if we have already deduced
18684 some template args from previous args, unify would complain when we
18685 try to deduce a template parameter for the same argument, even though
18686 there isn't really a conflict. */
18687 nargs = TREE_VEC_LENGTH (targs);
18688 tempargs = make_tree_vec (nargs);
18689
18690 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
18691 return 0;
18692
18693 /* First make sure we didn't deduce anything that conflicts with
18694 explicitly specified args. */
18695 for (i = nargs; i--; )
18696 {
18697 tree elt = TREE_VEC_ELT (tempargs, i);
18698 tree oldelt = TREE_VEC_ELT (orig_targs, i);
18699
18700 if (!elt)
18701 /*NOP*/;
18702 else if (uses_template_parms (elt))
18703 /* Since we're unifying against ourselves, we will fill in
18704 template args used in the function parm list with our own
18705 template parms. Discard them. */
18706 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
18707 else if (oldelt && !template_args_equal (oldelt, elt))
18708 return 0;
18709 }
18710
18711 for (i = nargs; i--; )
18712 {
18713 tree elt = TREE_VEC_ELT (tempargs, i);
18714
18715 if (elt)
18716 TREE_VEC_ELT (targs, i) = elt;
18717 }
18718
18719 return 1;
18720 }
18721
18722 /* PARM is a template class (perhaps with unbound template
18723 parameters). ARG is a fully instantiated type. If ARG can be
18724 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
18725 TARGS are as for unify. */
18726
18727 static tree
18728 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
18729 bool explain_p)
18730 {
18731 tree copy_of_targs;
18732
18733 if (!CLASSTYPE_TEMPLATE_INFO (arg)
18734 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
18735 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
18736 return NULL_TREE;
18737
18738 /* We need to make a new template argument vector for the call to
18739 unify. If we used TARGS, we'd clutter it up with the result of
18740 the attempted unification, even if this class didn't work out.
18741 We also don't want to commit ourselves to all the unifications
18742 we've already done, since unification is supposed to be done on
18743 an argument-by-argument basis. In other words, consider the
18744 following pathological case:
18745
18746 template <int I, int J, int K>
18747 struct S {};
18748
18749 template <int I, int J>
18750 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
18751
18752 template <int I, int J, int K>
18753 void f(S<I, J, K>, S<I, I, I>);
18754
18755 void g() {
18756 S<0, 0, 0> s0;
18757 S<0, 1, 2> s2;
18758
18759 f(s0, s2);
18760 }
18761
18762 Now, by the time we consider the unification involving `s2', we
18763 already know that we must have `f<0, 0, 0>'. But, even though
18764 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
18765 because there are two ways to unify base classes of S<0, 1, 2>
18766 with S<I, I, I>. If we kept the already deduced knowledge, we
18767 would reject the possibility I=1. */
18768 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
18769
18770 /* If unification failed, we're done. */
18771 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
18772 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
18773 return NULL_TREE;
18774
18775 return arg;
18776 }
18777
18778 /* Given a template type PARM and a class type ARG, find the unique
18779 base type in ARG that is an instance of PARM. We do not examine
18780 ARG itself; only its base-classes. If there is not exactly one
18781 appropriate base class, return NULL_TREE. PARM may be the type of
18782 a partial specialization, as well as a plain template type. Used
18783 by unify. */
18784
18785 static enum template_base_result
18786 get_template_base (tree tparms, tree targs, tree parm, tree arg,
18787 bool explain_p, tree *result)
18788 {
18789 tree rval = NULL_TREE;
18790 tree binfo;
18791
18792 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
18793
18794 binfo = TYPE_BINFO (complete_type (arg));
18795 if (!binfo)
18796 {
18797 /* The type could not be completed. */
18798 *result = NULL_TREE;
18799 return tbr_incomplete_type;
18800 }
18801
18802 /* Walk in inheritance graph order. The search order is not
18803 important, and this avoids multiple walks of virtual bases. */
18804 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
18805 {
18806 tree r = try_class_unification (tparms, targs, parm,
18807 BINFO_TYPE (binfo), explain_p);
18808
18809 if (r)
18810 {
18811 /* If there is more than one satisfactory baseclass, then:
18812
18813 [temp.deduct.call]
18814
18815 If they yield more than one possible deduced A, the type
18816 deduction fails.
18817
18818 applies. */
18819 if (rval && !same_type_p (r, rval))
18820 {
18821 *result = NULL_TREE;
18822 return tbr_ambiguous_baseclass;
18823 }
18824
18825 rval = r;
18826 }
18827 }
18828
18829 *result = rval;
18830 return tbr_success;
18831 }
18832
18833 /* Returns the level of DECL, which declares a template parameter. */
18834
18835 static int
18836 template_decl_level (tree decl)
18837 {
18838 switch (TREE_CODE (decl))
18839 {
18840 case TYPE_DECL:
18841 case TEMPLATE_DECL:
18842 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
18843
18844 case PARM_DECL:
18845 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
18846
18847 default:
18848 gcc_unreachable ();
18849 }
18850 return 0;
18851 }
18852
18853 /* Decide whether ARG can be unified with PARM, considering only the
18854 cv-qualifiers of each type, given STRICT as documented for unify.
18855 Returns nonzero iff the unification is OK on that basis. */
18856
18857 static int
18858 check_cv_quals_for_unify (int strict, tree arg, tree parm)
18859 {
18860 int arg_quals = cp_type_quals (arg);
18861 int parm_quals = cp_type_quals (parm);
18862
18863 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18864 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18865 {
18866 /* Although a CVR qualifier is ignored when being applied to a
18867 substituted template parameter ([8.3.2]/1 for example), that
18868 does not allow us to unify "const T" with "int&" because both
18869 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18870 It is ok when we're allowing additional CV qualifiers
18871 at the outer level [14.8.2.1]/3,1st bullet. */
18872 if ((TREE_CODE (arg) == REFERENCE_TYPE
18873 || TREE_CODE (arg) == FUNCTION_TYPE
18874 || TREE_CODE (arg) == METHOD_TYPE)
18875 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
18876 return 0;
18877
18878 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
18879 && (parm_quals & TYPE_QUAL_RESTRICT))
18880 return 0;
18881 }
18882
18883 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18884 && (arg_quals & parm_quals) != parm_quals)
18885 return 0;
18886
18887 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
18888 && (parm_quals & arg_quals) != arg_quals)
18889 return 0;
18890
18891 return 1;
18892 }
18893
18894 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18895 void
18896 template_parm_level_and_index (tree parm, int* level, int* index)
18897 {
18898 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18899 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18900 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18901 {
18902 *index = TEMPLATE_TYPE_IDX (parm);
18903 *level = TEMPLATE_TYPE_LEVEL (parm);
18904 }
18905 else
18906 {
18907 *index = TEMPLATE_PARM_IDX (parm);
18908 *level = TEMPLATE_PARM_LEVEL (parm);
18909 }
18910 }
18911
18912 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18913 do { \
18914 if (unify (TP, TA, P, A, S, EP)) \
18915 return 1; \
18916 } while (0);
18917
18918 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18919 expansion at the end of PACKED_PARMS. Returns 0 if the type
18920 deduction succeeds, 1 otherwise. STRICT is the same as in
18921 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18922 call argument list. We'll need to adjust the arguments to make them
18923 types. SUBR tells us if this is from a recursive call to
18924 type_unification_real, or for comparing two template argument
18925 lists. */
18926
18927 static int
18928 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
18929 tree packed_args, unification_kind_t strict,
18930 bool subr, bool explain_p)
18931 {
18932 tree parm
18933 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
18934 tree pattern = PACK_EXPANSION_PATTERN (parm);
18935 tree pack, packs = NULL_TREE;
18936 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
18937
18938 packed_args = expand_template_argument_pack (packed_args);
18939
18940 int len = TREE_VEC_LENGTH (packed_args);
18941
18942 /* Determine the parameter packs we will be deducing from the
18943 pattern, and record their current deductions. */
18944 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
18945 pack; pack = TREE_CHAIN (pack))
18946 {
18947 tree parm_pack = TREE_VALUE (pack);
18948 int idx, level;
18949
18950 /* Determine the index and level of this parameter pack. */
18951 template_parm_level_and_index (parm_pack, &level, &idx);
18952
18953 /* Keep track of the parameter packs and their corresponding
18954 argument packs. */
18955 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
18956 TREE_TYPE (packs) = make_tree_vec (len - start);
18957 }
18958
18959 /* Loop through all of the arguments that have not yet been
18960 unified and unify each with the pattern. */
18961 for (i = start; i < len; i++)
18962 {
18963 tree parm;
18964 bool any_explicit = false;
18965 tree arg = TREE_VEC_ELT (packed_args, i);
18966
18967 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18968 or the element of its argument pack at the current index if
18969 this argument was explicitly specified. */
18970 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18971 {
18972 int idx, level;
18973 tree arg, pargs;
18974 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18975
18976 arg = NULL_TREE;
18977 if (TREE_VALUE (pack)
18978 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
18979 && (i - start < TREE_VEC_LENGTH (pargs)))
18980 {
18981 any_explicit = true;
18982 arg = TREE_VEC_ELT (pargs, i - start);
18983 }
18984 TMPL_ARG (targs, level, idx) = arg;
18985 }
18986
18987 /* If we had explicit template arguments, substitute them into the
18988 pattern before deduction. */
18989 if (any_explicit)
18990 {
18991 /* Some arguments might still be unspecified or dependent. */
18992 bool dependent;
18993 ++processing_template_decl;
18994 dependent = any_dependent_template_arguments_p (targs);
18995 if (!dependent)
18996 --processing_template_decl;
18997 parm = tsubst (pattern, targs,
18998 explain_p ? tf_warning_or_error : tf_none,
18999 NULL_TREE);
19000 if (dependent)
19001 --processing_template_decl;
19002 if (parm == error_mark_node)
19003 return 1;
19004 }
19005 else
19006 parm = pattern;
19007
19008 /* Unify the pattern with the current argument. */
19009 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19010 explain_p))
19011 return 1;
19012
19013 /* For each parameter pack, collect the deduced value. */
19014 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19015 {
19016 int idx, level;
19017 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19018
19019 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19020 TMPL_ARG (targs, level, idx);
19021 }
19022 }
19023
19024 /* Verify that the results of unification with the parameter packs
19025 produce results consistent with what we've seen before, and make
19026 the deduced argument packs available. */
19027 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19028 {
19029 tree old_pack = TREE_VALUE (pack);
19030 tree new_args = TREE_TYPE (pack);
19031 int i, len = TREE_VEC_LENGTH (new_args);
19032 int idx, level;
19033 bool nondeduced_p = false;
19034
19035 /* By default keep the original deduced argument pack.
19036 If necessary, more specific code is going to update the
19037 resulting deduced argument later down in this function. */
19038 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19039 TMPL_ARG (targs, level, idx) = old_pack;
19040
19041 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19042 actually deduce anything. */
19043 for (i = 0; i < len && !nondeduced_p; ++i)
19044 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
19045 nondeduced_p = true;
19046 if (nondeduced_p)
19047 continue;
19048
19049 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
19050 {
19051 /* If we had fewer function args than explicit template args,
19052 just use the explicits. */
19053 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19054 int explicit_len = TREE_VEC_LENGTH (explicit_args);
19055 if (len < explicit_len)
19056 new_args = explicit_args;
19057 }
19058
19059 if (!old_pack)
19060 {
19061 tree result;
19062 /* Build the deduced *_ARGUMENT_PACK. */
19063 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
19064 {
19065 result = make_node (NONTYPE_ARGUMENT_PACK);
19066 TREE_TYPE (result) =
19067 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
19068 TREE_CONSTANT (result) = 1;
19069 }
19070 else
19071 result = cxx_make_type (TYPE_ARGUMENT_PACK);
19072
19073 SET_ARGUMENT_PACK_ARGS (result, new_args);
19074
19075 /* Note the deduced argument packs for this parameter
19076 pack. */
19077 TMPL_ARG (targs, level, idx) = result;
19078 }
19079 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
19080 && (ARGUMENT_PACK_ARGS (old_pack)
19081 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
19082 {
19083 /* We only had the explicitly-provided arguments before, but
19084 now we have a complete set of arguments. */
19085 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19086
19087 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
19088 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
19089 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
19090 }
19091 else
19092 {
19093 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
19094 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
19095
19096 if (!comp_template_args (old_args, new_args,
19097 &bad_old_arg, &bad_new_arg))
19098 /* Inconsistent unification of this parameter pack. */
19099 return unify_parameter_pack_inconsistent (explain_p,
19100 bad_old_arg,
19101 bad_new_arg);
19102 }
19103 }
19104
19105 return unify_success (explain_p);
19106 }
19107
19108 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19109 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19110 parameters and return value are as for unify. */
19111
19112 static int
19113 unify_array_domain (tree tparms, tree targs,
19114 tree parm_dom, tree arg_dom,
19115 bool explain_p)
19116 {
19117 tree parm_max;
19118 tree arg_max;
19119 bool parm_cst;
19120 bool arg_cst;
19121
19122 /* Our representation of array types uses "N - 1" as the
19123 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19124 not an integer constant. We cannot unify arbitrarily
19125 complex expressions, so we eliminate the MINUS_EXPRs
19126 here. */
19127 parm_max = TYPE_MAX_VALUE (parm_dom);
19128 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
19129 if (!parm_cst)
19130 {
19131 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
19132 parm_max = TREE_OPERAND (parm_max, 0);
19133 }
19134 arg_max = TYPE_MAX_VALUE (arg_dom);
19135 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
19136 if (!arg_cst)
19137 {
19138 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19139 trying to unify the type of a variable with the type
19140 of a template parameter. For example:
19141
19142 template <unsigned int N>
19143 void f (char (&) [N]);
19144 int g();
19145 void h(int i) {
19146 char a[g(i)];
19147 f(a);
19148 }
19149
19150 Here, the type of the ARG will be "int [g(i)]", and
19151 may be a SAVE_EXPR, etc. */
19152 if (TREE_CODE (arg_max) != MINUS_EXPR)
19153 return unify_vla_arg (explain_p, arg_dom);
19154 arg_max = TREE_OPERAND (arg_max, 0);
19155 }
19156
19157 /* If only one of the bounds used a MINUS_EXPR, compensate
19158 by adding one to the other bound. */
19159 if (parm_cst && !arg_cst)
19160 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
19161 integer_type_node,
19162 parm_max,
19163 integer_one_node);
19164 else if (arg_cst && !parm_cst)
19165 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
19166 integer_type_node,
19167 arg_max,
19168 integer_one_node);
19169
19170 return unify (tparms, targs, parm_max, arg_max,
19171 UNIFY_ALLOW_INTEGER, explain_p);
19172 }
19173
19174 /* Deduce the value of template parameters. TPARMS is the (innermost)
19175 set of template parameters to a template. TARGS is the bindings
19176 for those template parameters, as determined thus far; TARGS may
19177 include template arguments for outer levels of template parameters
19178 as well. PARM is a parameter to a template function, or a
19179 subcomponent of that parameter; ARG is the corresponding argument.
19180 This function attempts to match PARM with ARG in a manner
19181 consistent with the existing assignments in TARGS. If more values
19182 are deduced, then TARGS is updated.
19183
19184 Returns 0 if the type deduction succeeds, 1 otherwise. The
19185 parameter STRICT is a bitwise or of the following flags:
19186
19187 UNIFY_ALLOW_NONE:
19188 Require an exact match between PARM and ARG.
19189 UNIFY_ALLOW_MORE_CV_QUAL:
19190 Allow the deduced ARG to be more cv-qualified (by qualification
19191 conversion) than ARG.
19192 UNIFY_ALLOW_LESS_CV_QUAL:
19193 Allow the deduced ARG to be less cv-qualified than ARG.
19194 UNIFY_ALLOW_DERIVED:
19195 Allow the deduced ARG to be a template base class of ARG,
19196 or a pointer to a template base class of the type pointed to by
19197 ARG.
19198 UNIFY_ALLOW_INTEGER:
19199 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19200 case for more information.
19201 UNIFY_ALLOW_OUTER_LEVEL:
19202 This is the outermost level of a deduction. Used to determine validity
19203 of qualification conversions. A valid qualification conversion must
19204 have const qualified pointers leading up to the inner type which
19205 requires additional CV quals, except at the outer level, where const
19206 is not required [conv.qual]. It would be normal to set this flag in
19207 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19208 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19209 This is the outermost level of a deduction, and PARM can be more CV
19210 qualified at this point.
19211 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19212 This is the outermost level of a deduction, and PARM can be less CV
19213 qualified at this point. */
19214
19215 static int
19216 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
19217 bool explain_p)
19218 {
19219 int idx;
19220 tree targ;
19221 tree tparm;
19222 int strict_in = strict;
19223
19224 /* I don't think this will do the right thing with respect to types.
19225 But the only case I've seen it in so far has been array bounds, where
19226 signedness is the only information lost, and I think that will be
19227 okay. */
19228 while (TREE_CODE (parm) == NOP_EXPR)
19229 parm = TREE_OPERAND (parm, 0);
19230
19231 if (arg == error_mark_node)
19232 return unify_invalid (explain_p);
19233 if (arg == unknown_type_node
19234 || arg == init_list_type_node)
19235 /* We can't deduce anything from this, but we might get all the
19236 template args from other function args. */
19237 return unify_success (explain_p);
19238
19239 /* If PARM uses template parameters, then we can't bail out here,
19240 even if ARG == PARM, since we won't record unifications for the
19241 template parameters. We might need them if we're trying to
19242 figure out which of two things is more specialized. */
19243 if (arg == parm && !uses_template_parms (parm))
19244 return unify_success (explain_p);
19245
19246 /* Handle init lists early, so the rest of the function can assume
19247 we're dealing with a type. */
19248 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
19249 {
19250 tree elt, elttype;
19251 unsigned i;
19252 tree orig_parm = parm;
19253
19254 /* Replace T with std::initializer_list<T> for deduction. */
19255 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19256 && flag_deduce_init_list)
19257 parm = listify (parm);
19258
19259 if (!is_std_init_list (parm)
19260 && TREE_CODE (parm) != ARRAY_TYPE)
19261 /* We can only deduce from an initializer list argument if the
19262 parameter is std::initializer_list or an array; otherwise this
19263 is a non-deduced context. */
19264 return unify_success (explain_p);
19265
19266 if (TREE_CODE (parm) == ARRAY_TYPE)
19267 elttype = TREE_TYPE (parm);
19268 else
19269 {
19270 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
19271 /* Deduction is defined in terms of a single type, so just punt
19272 on the (bizarre) std::initializer_list<T...>. */
19273 if (PACK_EXPANSION_P (elttype))
19274 return unify_success (explain_p);
19275 }
19276
19277 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
19278 {
19279 int elt_strict = strict;
19280
19281 if (elt == error_mark_node)
19282 return unify_invalid (explain_p);
19283
19284 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
19285 {
19286 tree type = TREE_TYPE (elt);
19287 if (type == error_mark_node)
19288 return unify_invalid (explain_p);
19289 /* It should only be possible to get here for a call. */
19290 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
19291 elt_strict |= maybe_adjust_types_for_deduction
19292 (DEDUCE_CALL, &elttype, &type, elt);
19293 elt = type;
19294 }
19295
19296 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
19297 explain_p);
19298 }
19299
19300 if (TREE_CODE (parm) == ARRAY_TYPE
19301 && deducible_array_bound (TYPE_DOMAIN (parm)))
19302 {
19303 /* Also deduce from the length of the initializer list. */
19304 tree max = size_int (CONSTRUCTOR_NELTS (arg));
19305 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
19306 if (idx == error_mark_node)
19307 return unify_invalid (explain_p);
19308 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19309 idx, explain_p);
19310 }
19311
19312 /* If the std::initializer_list<T> deduction worked, replace the
19313 deduced A with std::initializer_list<A>. */
19314 if (orig_parm != parm)
19315 {
19316 idx = TEMPLATE_TYPE_IDX (orig_parm);
19317 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19318 targ = listify (targ);
19319 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
19320 }
19321 return unify_success (explain_p);
19322 }
19323
19324 /* Immediately reject some pairs that won't unify because of
19325 cv-qualification mismatches. */
19326 if (TREE_CODE (arg) == TREE_CODE (parm)
19327 && TYPE_P (arg)
19328 /* It is the elements of the array which hold the cv quals of an array
19329 type, and the elements might be template type parms. We'll check
19330 when we recurse. */
19331 && TREE_CODE (arg) != ARRAY_TYPE
19332 /* We check the cv-qualifiers when unifying with template type
19333 parameters below. We want to allow ARG `const T' to unify with
19334 PARM `T' for example, when computing which of two templates
19335 is more specialized, for example. */
19336 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
19337 && !check_cv_quals_for_unify (strict_in, arg, parm))
19338 return unify_cv_qual_mismatch (explain_p, parm, arg);
19339
19340 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
19341 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
19342 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
19343 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
19344 strict &= ~UNIFY_ALLOW_DERIVED;
19345 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19346 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
19347
19348 switch (TREE_CODE (parm))
19349 {
19350 case TYPENAME_TYPE:
19351 case SCOPE_REF:
19352 case UNBOUND_CLASS_TEMPLATE:
19353 /* In a type which contains a nested-name-specifier, template
19354 argument values cannot be deduced for template parameters used
19355 within the nested-name-specifier. */
19356 return unify_success (explain_p);
19357
19358 case TEMPLATE_TYPE_PARM:
19359 case TEMPLATE_TEMPLATE_PARM:
19360 case BOUND_TEMPLATE_TEMPLATE_PARM:
19361 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19362 if (error_operand_p (tparm))
19363 return unify_invalid (explain_p);
19364
19365 if (TEMPLATE_TYPE_LEVEL (parm)
19366 != template_decl_level (tparm))
19367 /* The PARM is not one we're trying to unify. Just check
19368 to see if it matches ARG. */
19369 {
19370 if (TREE_CODE (arg) == TREE_CODE (parm)
19371 && (is_auto (parm) ? is_auto (arg)
19372 : same_type_p (parm, arg)))
19373 return unify_success (explain_p);
19374 else
19375 return unify_type_mismatch (explain_p, parm, arg);
19376 }
19377 idx = TEMPLATE_TYPE_IDX (parm);
19378 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19379 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
19380 if (error_operand_p (tparm))
19381 return unify_invalid (explain_p);
19382
19383 /* Check for mixed types and values. */
19384 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19385 && TREE_CODE (tparm) != TYPE_DECL)
19386 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19387 && TREE_CODE (tparm) != TEMPLATE_DECL))
19388 gcc_unreachable ();
19389
19390 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19391 {
19392 /* ARG must be constructed from a template class or a template
19393 template parameter. */
19394 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
19395 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19396 return unify_template_deduction_failure (explain_p, parm, arg);
19397 {
19398 tree parmvec = TYPE_TI_ARGS (parm);
19399 /* An alias template name is never deduced. */
19400 if (TYPE_ALIAS_P (arg))
19401 arg = strip_typedefs (arg);
19402 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
19403 tree full_argvec = add_to_template_args (targs, argvec);
19404 tree parm_parms
19405 = DECL_INNERMOST_TEMPLATE_PARMS
19406 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
19407 int i, len;
19408 int parm_variadic_p = 0;
19409
19410 /* The resolution to DR150 makes clear that default
19411 arguments for an N-argument may not be used to bind T
19412 to a template template parameter with fewer than N
19413 parameters. It is not safe to permit the binding of
19414 default arguments as an extension, as that may change
19415 the meaning of a conforming program. Consider:
19416
19417 struct Dense { static const unsigned int dim = 1; };
19418
19419 template <template <typename> class View,
19420 typename Block>
19421 void operator+(float, View<Block> const&);
19422
19423 template <typename Block,
19424 unsigned int Dim = Block::dim>
19425 struct Lvalue_proxy { operator float() const; };
19426
19427 void
19428 test_1d (void) {
19429 Lvalue_proxy<Dense> p;
19430 float b;
19431 b + p;
19432 }
19433
19434 Here, if Lvalue_proxy is permitted to bind to View, then
19435 the global operator+ will be used; if they are not, the
19436 Lvalue_proxy will be converted to float. */
19437 if (coerce_template_parms (parm_parms,
19438 full_argvec,
19439 TYPE_TI_TEMPLATE (parm),
19440 (explain_p
19441 ? tf_warning_or_error
19442 : tf_none),
19443 /*require_all_args=*/true,
19444 /*use_default_args=*/false)
19445 == error_mark_node)
19446 return 1;
19447
19448 /* Deduce arguments T, i from TT<T> or TT<i>.
19449 We check each element of PARMVEC and ARGVEC individually
19450 rather than the whole TREE_VEC since they can have
19451 different number of elements. */
19452
19453 parmvec = expand_template_argument_pack (parmvec);
19454 argvec = expand_template_argument_pack (argvec);
19455
19456 len = TREE_VEC_LENGTH (parmvec);
19457
19458 /* Check if the parameters end in a pack, making them
19459 variadic. */
19460 if (len > 0
19461 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
19462 parm_variadic_p = 1;
19463
19464 for (i = 0; i < len - parm_variadic_p; ++i)
19465 /* If the template argument list of P contains a pack
19466 expansion that is not the last template argument, the
19467 entire template argument list is a non-deduced
19468 context. */
19469 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
19470 return unify_success (explain_p);
19471
19472 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
19473 return unify_too_few_arguments (explain_p,
19474 TREE_VEC_LENGTH (argvec), len);
19475
19476 for (i = 0; i < len - parm_variadic_p; ++i)
19477 {
19478 RECUR_AND_CHECK_FAILURE (tparms, targs,
19479 TREE_VEC_ELT (parmvec, i),
19480 TREE_VEC_ELT (argvec, i),
19481 UNIFY_ALLOW_NONE, explain_p);
19482 }
19483
19484 if (parm_variadic_p
19485 && unify_pack_expansion (tparms, targs,
19486 parmvec, argvec,
19487 DEDUCE_EXACT,
19488 /*subr=*/true, explain_p))
19489 return 1;
19490 }
19491 arg = TYPE_TI_TEMPLATE (arg);
19492
19493 /* Fall through to deduce template name. */
19494 }
19495
19496 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19497 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19498 {
19499 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19500
19501 /* Simple cases: Value already set, does match or doesn't. */
19502 if (targ != NULL_TREE && template_args_equal (targ, arg))
19503 return unify_success (explain_p);
19504 else if (targ)
19505 return unify_inconsistency (explain_p, parm, targ, arg);
19506 }
19507 else
19508 {
19509 /* If PARM is `const T' and ARG is only `int', we don't have
19510 a match unless we are allowing additional qualification.
19511 If ARG is `const int' and PARM is just `T' that's OK;
19512 that binds `const int' to `T'. */
19513 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
19514 arg, parm))
19515 return unify_cv_qual_mismatch (explain_p, parm, arg);
19516
19517 /* Consider the case where ARG is `const volatile int' and
19518 PARM is `const T'. Then, T should be `volatile int'. */
19519 arg = cp_build_qualified_type_real
19520 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
19521 if (arg == error_mark_node)
19522 return unify_invalid (explain_p);
19523
19524 /* Simple cases: Value already set, does match or doesn't. */
19525 if (targ != NULL_TREE && same_type_p (targ, arg))
19526 return unify_success (explain_p);
19527 else if (targ)
19528 return unify_inconsistency (explain_p, parm, targ, arg);
19529
19530 /* Make sure that ARG is not a variable-sized array. (Note
19531 that were talking about variable-sized arrays (like
19532 `int[n]'), rather than arrays of unknown size (like
19533 `int[]').) We'll get very confused by such a type since
19534 the bound of the array is not constant, and therefore
19535 not mangleable. Besides, such types are not allowed in
19536 ISO C++, so we can do as we please here. We do allow
19537 them for 'auto' deduction, since that isn't ABI-exposed. */
19538 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
19539 return unify_vla_arg (explain_p, arg);
19540
19541 /* Strip typedefs as in convert_template_argument. */
19542 arg = canonicalize_type_argument (arg, tf_none);
19543 }
19544
19545 /* If ARG is a parameter pack or an expansion, we cannot unify
19546 against it unless PARM is also a parameter pack. */
19547 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19548 && !template_parameter_pack_p (parm))
19549 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19550
19551 /* If the argument deduction results is a METHOD_TYPE,
19552 then there is a problem.
19553 METHOD_TYPE doesn't map to any real C++ type the result of
19554 the deduction can not be of that type. */
19555 if (TREE_CODE (arg) == METHOD_TYPE)
19556 return unify_method_type_error (explain_p, arg);
19557
19558 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19559 return unify_success (explain_p);
19560
19561 case TEMPLATE_PARM_INDEX:
19562 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19563 if (error_operand_p (tparm))
19564 return unify_invalid (explain_p);
19565
19566 if (TEMPLATE_PARM_LEVEL (parm)
19567 != template_decl_level (tparm))
19568 {
19569 /* The PARM is not one we're trying to unify. Just check
19570 to see if it matches ARG. */
19571 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
19572 && cp_tree_equal (parm, arg));
19573 if (result)
19574 unify_expression_unequal (explain_p, parm, arg);
19575 return result;
19576 }
19577
19578 idx = TEMPLATE_PARM_IDX (parm);
19579 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19580
19581 if (targ)
19582 {
19583 int x = !cp_tree_equal (targ, arg);
19584 if (x)
19585 unify_inconsistency (explain_p, parm, targ, arg);
19586 return x;
19587 }
19588
19589 /* [temp.deduct.type] If, in the declaration of a function template
19590 with a non-type template-parameter, the non-type
19591 template-parameter is used in an expression in the function
19592 parameter-list and, if the corresponding template-argument is
19593 deduced, the template-argument type shall match the type of the
19594 template-parameter exactly, except that a template-argument
19595 deduced from an array bound may be of any integral type.
19596 The non-type parameter might use already deduced type parameters. */
19597 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
19598 if (!TREE_TYPE (arg))
19599 /* Template-parameter dependent expression. Just accept it for now.
19600 It will later be processed in convert_template_argument. */
19601 ;
19602 else if (same_type_p (TREE_TYPE (arg), tparm))
19603 /* OK */;
19604 else if ((strict & UNIFY_ALLOW_INTEGER)
19605 && CP_INTEGRAL_TYPE_P (tparm))
19606 /* Convert the ARG to the type of PARM; the deduced non-type
19607 template argument must exactly match the types of the
19608 corresponding parameter. */
19609 arg = fold (build_nop (tparm, arg));
19610 else if (uses_template_parms (tparm))
19611 /* We haven't deduced the type of this parameter yet. Try again
19612 later. */
19613 return unify_success (explain_p);
19614 else
19615 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
19616
19617 /* If ARG is a parameter pack or an expansion, we cannot unify
19618 against it unless PARM is also a parameter pack. */
19619 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19620 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
19621 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19622
19623 {
19624 bool removed_attr = false;
19625 arg = strip_typedefs_expr (arg, &removed_attr);
19626 }
19627 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19628 return unify_success (explain_p);
19629
19630 case PTRMEM_CST:
19631 {
19632 /* A pointer-to-member constant can be unified only with
19633 another constant. */
19634 if (TREE_CODE (arg) != PTRMEM_CST)
19635 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
19636
19637 /* Just unify the class member. It would be useless (and possibly
19638 wrong, depending on the strict flags) to unify also
19639 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19640 arg refer to the same variable, even if through different
19641 classes. For instance:
19642
19643 struct A { int x; };
19644 struct B : A { };
19645
19646 Unification of &A::x and &B::x must succeed. */
19647 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
19648 PTRMEM_CST_MEMBER (arg), strict, explain_p);
19649 }
19650
19651 case POINTER_TYPE:
19652 {
19653 if (!TYPE_PTR_P (arg))
19654 return unify_type_mismatch (explain_p, parm, arg);
19655
19656 /* [temp.deduct.call]
19657
19658 A can be another pointer or pointer to member type that can
19659 be converted to the deduced A via a qualification
19660 conversion (_conv.qual_).
19661
19662 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19663 This will allow for additional cv-qualification of the
19664 pointed-to types if appropriate. */
19665
19666 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
19667 /* The derived-to-base conversion only persists through one
19668 level of pointers. */
19669 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
19670
19671 return unify (tparms, targs, TREE_TYPE (parm),
19672 TREE_TYPE (arg), strict, explain_p);
19673 }
19674
19675 case REFERENCE_TYPE:
19676 if (TREE_CODE (arg) != REFERENCE_TYPE)
19677 return unify_type_mismatch (explain_p, parm, arg);
19678 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19679 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19680
19681 case ARRAY_TYPE:
19682 if (TREE_CODE (arg) != ARRAY_TYPE)
19683 return unify_type_mismatch (explain_p, parm, arg);
19684 if ((TYPE_DOMAIN (parm) == NULL_TREE)
19685 != (TYPE_DOMAIN (arg) == NULL_TREE))
19686 return unify_type_mismatch (explain_p, parm, arg);
19687 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19688 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19689 if (TYPE_DOMAIN (parm) != NULL_TREE)
19690 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19691 TYPE_DOMAIN (arg), explain_p);
19692 return unify_success (explain_p);
19693
19694 case REAL_TYPE:
19695 case COMPLEX_TYPE:
19696 case VECTOR_TYPE:
19697 case INTEGER_TYPE:
19698 case BOOLEAN_TYPE:
19699 case ENUMERAL_TYPE:
19700 case VOID_TYPE:
19701 case NULLPTR_TYPE:
19702 if (TREE_CODE (arg) != TREE_CODE (parm))
19703 return unify_type_mismatch (explain_p, parm, arg);
19704
19705 /* We have already checked cv-qualification at the top of the
19706 function. */
19707 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
19708 return unify_type_mismatch (explain_p, parm, arg);
19709
19710 /* As far as unification is concerned, this wins. Later checks
19711 will invalidate it if necessary. */
19712 return unify_success (explain_p);
19713
19714 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
19715 /* Type INTEGER_CST can come from ordinary constant template args. */
19716 case INTEGER_CST:
19717 while (TREE_CODE (arg) == NOP_EXPR)
19718 arg = TREE_OPERAND (arg, 0);
19719
19720 if (TREE_CODE (arg) != INTEGER_CST)
19721 return unify_template_argument_mismatch (explain_p, parm, arg);
19722 return (tree_int_cst_equal (parm, arg)
19723 ? unify_success (explain_p)
19724 : unify_template_argument_mismatch (explain_p, parm, arg));
19725
19726 case TREE_VEC:
19727 {
19728 int i, len, argslen;
19729 int parm_variadic_p = 0;
19730
19731 if (TREE_CODE (arg) != TREE_VEC)
19732 return unify_template_argument_mismatch (explain_p, parm, arg);
19733
19734 len = TREE_VEC_LENGTH (parm);
19735 argslen = TREE_VEC_LENGTH (arg);
19736
19737 /* Check for pack expansions in the parameters. */
19738 for (i = 0; i < len; ++i)
19739 {
19740 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
19741 {
19742 if (i == len - 1)
19743 /* We can unify against something with a trailing
19744 parameter pack. */
19745 parm_variadic_p = 1;
19746 else
19747 /* [temp.deduct.type]/9: If the template argument list of
19748 P contains a pack expansion that is not the last
19749 template argument, the entire template argument list
19750 is a non-deduced context. */
19751 return unify_success (explain_p);
19752 }
19753 }
19754
19755 /* If we don't have enough arguments to satisfy the parameters
19756 (not counting the pack expression at the end), or we have
19757 too many arguments for a parameter list that doesn't end in
19758 a pack expression, we can't unify. */
19759 if (parm_variadic_p
19760 ? argslen < len - parm_variadic_p
19761 : argslen != len)
19762 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
19763
19764 /* Unify all of the parameters that precede the (optional)
19765 pack expression. */
19766 for (i = 0; i < len - parm_variadic_p; ++i)
19767 {
19768 RECUR_AND_CHECK_FAILURE (tparms, targs,
19769 TREE_VEC_ELT (parm, i),
19770 TREE_VEC_ELT (arg, i),
19771 UNIFY_ALLOW_NONE, explain_p);
19772 }
19773 if (parm_variadic_p)
19774 return unify_pack_expansion (tparms, targs, parm, arg,
19775 DEDUCE_EXACT,
19776 /*subr=*/true, explain_p);
19777 return unify_success (explain_p);
19778 }
19779
19780 case RECORD_TYPE:
19781 case UNION_TYPE:
19782 if (TREE_CODE (arg) != TREE_CODE (parm))
19783 return unify_type_mismatch (explain_p, parm, arg);
19784
19785 if (TYPE_PTRMEMFUNC_P (parm))
19786 {
19787 if (!TYPE_PTRMEMFUNC_P (arg))
19788 return unify_type_mismatch (explain_p, parm, arg);
19789
19790 return unify (tparms, targs,
19791 TYPE_PTRMEMFUNC_FN_TYPE (parm),
19792 TYPE_PTRMEMFUNC_FN_TYPE (arg),
19793 strict, explain_p);
19794 }
19795 else if (TYPE_PTRMEMFUNC_P (arg))
19796 return unify_type_mismatch (explain_p, parm, arg);
19797
19798 if (CLASSTYPE_TEMPLATE_INFO (parm))
19799 {
19800 tree t = NULL_TREE;
19801
19802 if (strict_in & UNIFY_ALLOW_DERIVED)
19803 {
19804 /* First, we try to unify the PARM and ARG directly. */
19805 t = try_class_unification (tparms, targs,
19806 parm, arg, explain_p);
19807
19808 if (!t)
19809 {
19810 /* Fallback to the special case allowed in
19811 [temp.deduct.call]:
19812
19813 If P is a class, and P has the form
19814 template-id, then A can be a derived class of
19815 the deduced A. Likewise, if P is a pointer to
19816 a class of the form template-id, A can be a
19817 pointer to a derived class pointed to by the
19818 deduced A. */
19819 enum template_base_result r;
19820 r = get_template_base (tparms, targs, parm, arg,
19821 explain_p, &t);
19822
19823 if (!t)
19824 {
19825 /* Don't give the derived diagnostic if we're
19826 already dealing with the same template. */
19827 bool same_template
19828 = (CLASSTYPE_TEMPLATE_INFO (arg)
19829 && (CLASSTYPE_TI_TEMPLATE (parm)
19830 == CLASSTYPE_TI_TEMPLATE (arg)));
19831 return unify_no_common_base (explain_p && !same_template,
19832 r, parm, arg);
19833 }
19834 }
19835 }
19836 else if (CLASSTYPE_TEMPLATE_INFO (arg)
19837 && (CLASSTYPE_TI_TEMPLATE (parm)
19838 == CLASSTYPE_TI_TEMPLATE (arg)))
19839 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19840 Then, we should unify `int' and `U'. */
19841 t = arg;
19842 else
19843 /* There's no chance of unification succeeding. */
19844 return unify_type_mismatch (explain_p, parm, arg);
19845
19846 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
19847 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
19848 }
19849 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
19850 return unify_type_mismatch (explain_p, parm, arg);
19851 return unify_success (explain_p);
19852
19853 case METHOD_TYPE:
19854 case FUNCTION_TYPE:
19855 {
19856 unsigned int nargs;
19857 tree *args;
19858 tree a;
19859 unsigned int i;
19860
19861 if (TREE_CODE (arg) != TREE_CODE (parm))
19862 return unify_type_mismatch (explain_p, parm, arg);
19863
19864 /* CV qualifications for methods can never be deduced, they must
19865 match exactly. We need to check them explicitly here,
19866 because type_unification_real treats them as any other
19867 cv-qualified parameter. */
19868 if (TREE_CODE (parm) == METHOD_TYPE
19869 && (!check_cv_quals_for_unify
19870 (UNIFY_ALLOW_NONE,
19871 class_of_this_parm (arg),
19872 class_of_this_parm (parm))))
19873 return unify_cv_qual_mismatch (explain_p, parm, arg);
19874
19875 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
19876 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
19877
19878 nargs = list_length (TYPE_ARG_TYPES (arg));
19879 args = XALLOCAVEC (tree, nargs);
19880 for (a = TYPE_ARG_TYPES (arg), i = 0;
19881 a != NULL_TREE && a != void_list_node;
19882 a = TREE_CHAIN (a), ++i)
19883 args[i] = TREE_VALUE (a);
19884 nargs = i;
19885
19886 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
19887 args, nargs, 1, DEDUCE_EXACT,
19888 LOOKUP_NORMAL, NULL, explain_p);
19889 }
19890
19891 case OFFSET_TYPE:
19892 /* Unify a pointer to member with a pointer to member function, which
19893 deduces the type of the member as a function type. */
19894 if (TYPE_PTRMEMFUNC_P (arg))
19895 {
19896 /* Check top-level cv qualifiers */
19897 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
19898 return unify_cv_qual_mismatch (explain_p, parm, arg);
19899
19900 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19901 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
19902 UNIFY_ALLOW_NONE, explain_p);
19903
19904 /* Determine the type of the function we are unifying against. */
19905 tree fntype = static_fn_type (arg);
19906
19907 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
19908 }
19909
19910 if (TREE_CODE (arg) != OFFSET_TYPE)
19911 return unify_type_mismatch (explain_p, parm, arg);
19912 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19913 TYPE_OFFSET_BASETYPE (arg),
19914 UNIFY_ALLOW_NONE, explain_p);
19915 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19916 strict, explain_p);
19917
19918 case CONST_DECL:
19919 if (DECL_TEMPLATE_PARM_P (parm))
19920 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
19921 if (arg != scalar_constant_value (parm))
19922 return unify_template_argument_mismatch (explain_p, parm, arg);
19923 return unify_success (explain_p);
19924
19925 case FIELD_DECL:
19926 case TEMPLATE_DECL:
19927 /* Matched cases are handled by the ARG == PARM test above. */
19928 return unify_template_argument_mismatch (explain_p, parm, arg);
19929
19930 case VAR_DECL:
19931 /* A non-type template parameter that is a variable should be a
19932 an integral constant, in which case, it whould have been
19933 folded into its (constant) value. So we should not be getting
19934 a variable here. */
19935 gcc_unreachable ();
19936
19937 case TYPE_ARGUMENT_PACK:
19938 case NONTYPE_ARGUMENT_PACK:
19939 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
19940 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
19941
19942 case TYPEOF_TYPE:
19943 case DECLTYPE_TYPE:
19944 case UNDERLYING_TYPE:
19945 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19946 or UNDERLYING_TYPE nodes. */
19947 return unify_success (explain_p);
19948
19949 case ERROR_MARK:
19950 /* Unification fails if we hit an error node. */
19951 return unify_invalid (explain_p);
19952
19953 case INDIRECT_REF:
19954 if (REFERENCE_REF_P (parm))
19955 {
19956 if (REFERENCE_REF_P (arg))
19957 arg = TREE_OPERAND (arg, 0);
19958 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
19959 strict, explain_p);
19960 }
19961 /* FALLTHRU */
19962
19963 default:
19964 /* An unresolved overload is a nondeduced context. */
19965 if (is_overloaded_fn (parm) || type_unknown_p (parm))
19966 return unify_success (explain_p);
19967 gcc_assert (EXPR_P (parm));
19968
19969 /* We must be looking at an expression. This can happen with
19970 something like:
19971
19972 template <int I>
19973 void foo(S<I>, S<I + 2>);
19974
19975 This is a "nondeduced context":
19976
19977 [deduct.type]
19978
19979 The nondeduced contexts are:
19980
19981 --A type that is a template-id in which one or more of
19982 the template-arguments is an expression that references
19983 a template-parameter.
19984
19985 In these cases, we assume deduction succeeded, but don't
19986 actually infer any unifications. */
19987
19988 if (!uses_template_parms (parm)
19989 && !template_args_equal (parm, arg))
19990 return unify_expression_unequal (explain_p, parm, arg);
19991 else
19992 return unify_success (explain_p);
19993 }
19994 }
19995 #undef RECUR_AND_CHECK_FAILURE
19996 \f
19997 /* Note that DECL can be defined in this translation unit, if
19998 required. */
19999
20000 static void
20001 mark_definable (tree decl)
20002 {
20003 tree clone;
20004 DECL_NOT_REALLY_EXTERN (decl) = 1;
20005 FOR_EACH_CLONE (clone, decl)
20006 DECL_NOT_REALLY_EXTERN (clone) = 1;
20007 }
20008
20009 /* Called if RESULT is explicitly instantiated, or is a member of an
20010 explicitly instantiated class. */
20011
20012 void
20013 mark_decl_instantiated (tree result, int extern_p)
20014 {
20015 SET_DECL_EXPLICIT_INSTANTIATION (result);
20016
20017 /* If this entity has already been written out, it's too late to
20018 make any modifications. */
20019 if (TREE_ASM_WRITTEN (result))
20020 return;
20021
20022 /* For anonymous namespace we don't need to do anything. */
20023 if (decl_anon_ns_mem_p (result))
20024 {
20025 gcc_assert (!TREE_PUBLIC (result));
20026 return;
20027 }
20028
20029 if (TREE_CODE (result) != FUNCTION_DECL)
20030 /* The TREE_PUBLIC flag for function declarations will have been
20031 set correctly by tsubst. */
20032 TREE_PUBLIC (result) = 1;
20033
20034 /* This might have been set by an earlier implicit instantiation. */
20035 DECL_COMDAT (result) = 0;
20036
20037 if (extern_p)
20038 DECL_NOT_REALLY_EXTERN (result) = 0;
20039 else
20040 {
20041 mark_definable (result);
20042 mark_needed (result);
20043 /* Always make artificials weak. */
20044 if (DECL_ARTIFICIAL (result) && flag_weak)
20045 comdat_linkage (result);
20046 /* For WIN32 we also want to put explicit instantiations in
20047 linkonce sections. */
20048 else if (TREE_PUBLIC (result))
20049 maybe_make_one_only (result);
20050 }
20051
20052 /* If EXTERN_P, then this function will not be emitted -- unless
20053 followed by an explicit instantiation, at which point its linkage
20054 will be adjusted. If !EXTERN_P, then this function will be
20055 emitted here. In neither circumstance do we want
20056 import_export_decl to adjust the linkage. */
20057 DECL_INTERFACE_KNOWN (result) = 1;
20058 }
20059
20060 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20061 important template arguments. If any are missing, we check whether
20062 they're important by using error_mark_node for substituting into any
20063 args that were used for partial ordering (the ones between ARGS and END)
20064 and seeing if it bubbles up. */
20065
20066 static bool
20067 check_undeduced_parms (tree targs, tree args, tree end)
20068 {
20069 bool found = false;
20070 int i;
20071 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
20072 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
20073 {
20074 found = true;
20075 TREE_VEC_ELT (targs, i) = error_mark_node;
20076 }
20077 if (found)
20078 {
20079 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
20080 if (substed == error_mark_node)
20081 return true;
20082 }
20083 return false;
20084 }
20085
20086 /* Given two function templates PAT1 and PAT2, return:
20087
20088 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20089 -1 if PAT2 is more specialized than PAT1.
20090 0 if neither is more specialized.
20091
20092 LEN indicates the number of parameters we should consider
20093 (defaulted parameters should not be considered).
20094
20095 The 1998 std underspecified function template partial ordering, and
20096 DR214 addresses the issue. We take pairs of arguments, one from
20097 each of the templates, and deduce them against each other. One of
20098 the templates will be more specialized if all the *other*
20099 template's arguments deduce against its arguments and at least one
20100 of its arguments *does* *not* deduce against the other template's
20101 corresponding argument. Deduction is done as for class templates.
20102 The arguments used in deduction have reference and top level cv
20103 qualifiers removed. Iff both arguments were originally reference
20104 types *and* deduction succeeds in both directions, an lvalue reference
20105 wins against an rvalue reference and otherwise the template
20106 with the more cv-qualified argument wins for that pairing (if
20107 neither is more cv-qualified, they both are equal). Unlike regular
20108 deduction, after all the arguments have been deduced in this way,
20109 we do *not* verify the deduced template argument values can be
20110 substituted into non-deduced contexts.
20111
20112 The logic can be a bit confusing here, because we look at deduce1 and
20113 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20114 can find template arguments for pat1 to make arg1 look like arg2, that
20115 means that arg2 is at least as specialized as arg1. */
20116
20117 int
20118 more_specialized_fn (tree pat1, tree pat2, int len)
20119 {
20120 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
20121 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
20122 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
20123 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
20124 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
20125 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
20126 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
20127 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
20128 tree origs1, origs2;
20129 bool lose1 = false;
20130 bool lose2 = false;
20131
20132 /* Remove the this parameter from non-static member functions. If
20133 one is a non-static member function and the other is not a static
20134 member function, remove the first parameter from that function
20135 also. This situation occurs for operator functions where we
20136 locate both a member function (with this pointer) and non-member
20137 operator (with explicit first operand). */
20138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
20139 {
20140 len--; /* LEN is the number of significant arguments for DECL1 */
20141 args1 = TREE_CHAIN (args1);
20142 if (!DECL_STATIC_FUNCTION_P (decl2))
20143 args2 = TREE_CHAIN (args2);
20144 }
20145 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
20146 {
20147 args2 = TREE_CHAIN (args2);
20148 if (!DECL_STATIC_FUNCTION_P (decl1))
20149 {
20150 len--;
20151 args1 = TREE_CHAIN (args1);
20152 }
20153 }
20154
20155 /* If only one is a conversion operator, they are unordered. */
20156 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
20157 return 0;
20158
20159 /* Consider the return type for a conversion function */
20160 if (DECL_CONV_FN_P (decl1))
20161 {
20162 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
20163 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
20164 len++;
20165 }
20166
20167 processing_template_decl++;
20168
20169 origs1 = args1;
20170 origs2 = args2;
20171
20172 while (len--
20173 /* Stop when an ellipsis is seen. */
20174 && args1 != NULL_TREE && args2 != NULL_TREE)
20175 {
20176 tree arg1 = TREE_VALUE (args1);
20177 tree arg2 = TREE_VALUE (args2);
20178 int deduce1, deduce2;
20179 int quals1 = -1;
20180 int quals2 = -1;
20181 int ref1 = 0;
20182 int ref2 = 0;
20183
20184 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20185 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20186 {
20187 /* When both arguments are pack expansions, we need only
20188 unify the patterns themselves. */
20189 arg1 = PACK_EXPANSION_PATTERN (arg1);
20190 arg2 = PACK_EXPANSION_PATTERN (arg2);
20191
20192 /* This is the last comparison we need to do. */
20193 len = 0;
20194 }
20195
20196 arg1 = decay_dependent_array_parm_type (arg1);
20197 arg2 = decay_dependent_array_parm_type (arg2);
20198
20199 if (TREE_CODE (arg1) == REFERENCE_TYPE)
20200 {
20201 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
20202 arg1 = TREE_TYPE (arg1);
20203 quals1 = cp_type_quals (arg1);
20204 }
20205
20206 if (TREE_CODE (arg2) == REFERENCE_TYPE)
20207 {
20208 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
20209 arg2 = TREE_TYPE (arg2);
20210 quals2 = cp_type_quals (arg2);
20211 }
20212
20213 arg1 = TYPE_MAIN_VARIANT (arg1);
20214 arg2 = TYPE_MAIN_VARIANT (arg2);
20215
20216 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
20217 {
20218 int i, len2 = list_length (args2);
20219 tree parmvec = make_tree_vec (1);
20220 tree argvec = make_tree_vec (len2);
20221 tree ta = args2;
20222
20223 /* Setup the parameter vector, which contains only ARG1. */
20224 TREE_VEC_ELT (parmvec, 0) = arg1;
20225
20226 /* Setup the argument vector, which contains the remaining
20227 arguments. */
20228 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
20229 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20230
20231 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
20232 argvec, DEDUCE_EXACT,
20233 /*subr=*/true, /*explain_p=*/false)
20234 == 0);
20235
20236 /* We cannot deduce in the other direction, because ARG1 is
20237 a pack expansion but ARG2 is not. */
20238 deduce2 = 0;
20239 }
20240 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20241 {
20242 int i, len1 = list_length (args1);
20243 tree parmvec = make_tree_vec (1);
20244 tree argvec = make_tree_vec (len1);
20245 tree ta = args1;
20246
20247 /* Setup the parameter vector, which contains only ARG1. */
20248 TREE_VEC_ELT (parmvec, 0) = arg2;
20249
20250 /* Setup the argument vector, which contains the remaining
20251 arguments. */
20252 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
20253 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20254
20255 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
20256 argvec, DEDUCE_EXACT,
20257 /*subr=*/true, /*explain_p=*/false)
20258 == 0);
20259
20260 /* We cannot deduce in the other direction, because ARG2 is
20261 a pack expansion but ARG1 is not.*/
20262 deduce1 = 0;
20263 }
20264
20265 else
20266 {
20267 /* The normal case, where neither argument is a pack
20268 expansion. */
20269 deduce1 = (unify (tparms1, targs1, arg1, arg2,
20270 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20271 == 0);
20272 deduce2 = (unify (tparms2, targs2, arg2, arg1,
20273 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20274 == 0);
20275 }
20276
20277 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20278 arg2, then arg2 is not as specialized as arg1. */
20279 if (!deduce1)
20280 lose2 = true;
20281 if (!deduce2)
20282 lose1 = true;
20283
20284 /* "If, for a given type, deduction succeeds in both directions
20285 (i.e., the types are identical after the transformations above)
20286 and both P and A were reference types (before being replaced with
20287 the type referred to above):
20288 - if the type from the argument template was an lvalue reference and
20289 the type from the parameter template was not, the argument type is
20290 considered to be more specialized than the other; otherwise,
20291 - if the type from the argument template is more cv-qualified
20292 than the type from the parameter template (as described above),
20293 the argument type is considered to be more specialized than the other;
20294 otherwise,
20295 - neither type is more specialized than the other." */
20296
20297 if (deduce1 && deduce2)
20298 {
20299 if (ref1 && ref2 && ref1 != ref2)
20300 {
20301 if (ref1 > ref2)
20302 lose1 = true;
20303 else
20304 lose2 = true;
20305 }
20306 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
20307 {
20308 if ((quals1 & quals2) == quals2)
20309 lose2 = true;
20310 if ((quals1 & quals2) == quals1)
20311 lose1 = true;
20312 }
20313 }
20314
20315 if (lose1 && lose2)
20316 /* We've failed to deduce something in either direction.
20317 These must be unordered. */
20318 break;
20319
20320 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20321 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20322 /* We have already processed all of the arguments in our
20323 handing of the pack expansion type. */
20324 len = 0;
20325
20326 args1 = TREE_CHAIN (args1);
20327 args2 = TREE_CHAIN (args2);
20328 }
20329
20330 /* "In most cases, all template parameters must have values in order for
20331 deduction to succeed, but for partial ordering purposes a template
20332 parameter may remain without a value provided it is not used in the
20333 types being used for partial ordering."
20334
20335 Thus, if we are missing any of the targs1 we need to substitute into
20336 origs1, then pat2 is not as specialized as pat1. This can happen when
20337 there is a nondeduced context. */
20338 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
20339 lose2 = true;
20340 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
20341 lose1 = true;
20342
20343 processing_template_decl--;
20344
20345 /* If both deductions succeed, the partial ordering selects the more
20346 constrained template. */
20347 if (!lose1 && !lose2)
20348 {
20349 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
20350 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
20351 lose1 = !subsumes_constraints (c1, c2);
20352 lose2 = !subsumes_constraints (c2, c1);
20353 }
20354
20355 /* All things being equal, if the next argument is a pack expansion
20356 for one function but not for the other, prefer the
20357 non-variadic function. FIXME this is bogus; see c++/41958. */
20358 if (lose1 == lose2
20359 && args1 && TREE_VALUE (args1)
20360 && args2 && TREE_VALUE (args2))
20361 {
20362 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
20363 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
20364 }
20365
20366 if (lose1 == lose2)
20367 return 0;
20368 else if (!lose1)
20369 return 1;
20370 else
20371 return -1;
20372 }
20373
20374 /* Determine which of two partial specializations of TMPL is more
20375 specialized.
20376
20377 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20378 to the first partial specialization. The TREE_PURPOSE is the
20379 innermost set of template parameters for the partial
20380 specialization. PAT2 is similar, but for the second template.
20381
20382 Return 1 if the first partial specialization is more specialized;
20383 -1 if the second is more specialized; 0 if neither is more
20384 specialized.
20385
20386 See [temp.class.order] for information about determining which of
20387 two templates is more specialized. */
20388
20389 static int
20390 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
20391 {
20392 tree targs;
20393 int winner = 0;
20394 bool any_deductions = false;
20395
20396 tree tmpl1 = TREE_VALUE (pat1);
20397 tree tmpl2 = TREE_VALUE (pat2);
20398 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
20399 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
20400 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
20401 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
20402
20403 /* Just like what happens for functions, if we are ordering between
20404 different template specializations, we may encounter dependent
20405 types in the arguments, and we need our dependency check functions
20406 to behave correctly. */
20407 ++processing_template_decl;
20408 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
20409 if (targs)
20410 {
20411 --winner;
20412 any_deductions = true;
20413 }
20414
20415 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
20416 if (targs)
20417 {
20418 ++winner;
20419 any_deductions = true;
20420 }
20421 --processing_template_decl;
20422
20423 /* If both deductions succeed, the partial ordering selects the more
20424 constrained template. */
20425 if (!winner && any_deductions)
20426 return more_constrained (tmpl1, tmpl2);
20427
20428 /* In the case of a tie where at least one of the templates
20429 has a parameter pack at the end, the template with the most
20430 non-packed parameters wins. */
20431 if (winner == 0
20432 && any_deductions
20433 && (template_args_variadic_p (TREE_PURPOSE (pat1))
20434 || template_args_variadic_p (TREE_PURPOSE (pat2))))
20435 {
20436 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
20437 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
20438 int len1 = TREE_VEC_LENGTH (args1);
20439 int len2 = TREE_VEC_LENGTH (args2);
20440
20441 /* We don't count the pack expansion at the end. */
20442 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
20443 --len1;
20444 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
20445 --len2;
20446
20447 if (len1 > len2)
20448 return 1;
20449 else if (len1 < len2)
20450 return -1;
20451 }
20452
20453 return winner;
20454 }
20455
20456 /* Return the template arguments that will produce the function signature
20457 DECL from the function template FN, with the explicit template
20458 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20459 also match. Return NULL_TREE if no satisfactory arguments could be
20460 found. */
20461
20462 static tree
20463 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
20464 {
20465 int ntparms = DECL_NTPARMS (fn);
20466 tree targs = make_tree_vec (ntparms);
20467 tree decl_type = TREE_TYPE (decl);
20468 tree decl_arg_types;
20469 tree *args;
20470 unsigned int nargs, ix;
20471 tree arg;
20472
20473 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
20474
20475 /* Never do unification on the 'this' parameter. */
20476 decl_arg_types = skip_artificial_parms_for (decl,
20477 TYPE_ARG_TYPES (decl_type));
20478
20479 nargs = list_length (decl_arg_types);
20480 args = XALLOCAVEC (tree, nargs);
20481 for (arg = decl_arg_types, ix = 0;
20482 arg != NULL_TREE && arg != void_list_node;
20483 arg = TREE_CHAIN (arg), ++ix)
20484 {
20485 args[ix] = TREE_VALUE (arg);
20486 args[ix] = decay_dependent_array_parm_type (args[ix]);
20487 }
20488
20489 if (fn_type_unification (fn, explicit_args, targs,
20490 args, ix,
20491 (check_rettype || DECL_CONV_FN_P (fn)
20492 ? TREE_TYPE (decl_type) : NULL_TREE),
20493 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
20494 /*decltype*/false)
20495 == error_mark_node)
20496 return NULL_TREE;
20497
20498 return targs;
20499 }
20500
20501 /* Return the innermost template arguments that, when applied to a partial
20502 specialization of TMPL whose innermost template parameters are
20503 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
20504 ARGS.
20505
20506 For example, suppose we have:
20507
20508 template <class T, class U> struct S {};
20509 template <class T> struct S<T*, int> {};
20510
20511 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
20512 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
20513 int}. The resulting vector will be {double}, indicating that `T'
20514 is bound to `double'. */
20515
20516 static tree
20517 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
20518 {
20519 int i, ntparms = TREE_VEC_LENGTH (tparms);
20520 tree deduced_args;
20521 tree innermost_deduced_args;
20522
20523 innermost_deduced_args = make_tree_vec (ntparms);
20524 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20525 {
20526 deduced_args = copy_node (args);
20527 SET_TMPL_ARGS_LEVEL (deduced_args,
20528 TMPL_ARGS_DEPTH (deduced_args),
20529 innermost_deduced_args);
20530 }
20531 else
20532 deduced_args = innermost_deduced_args;
20533
20534 if (unify (tparms, deduced_args,
20535 INNERMOST_TEMPLATE_ARGS (spec_args),
20536 INNERMOST_TEMPLATE_ARGS (args),
20537 UNIFY_ALLOW_NONE, /*explain_p=*/false))
20538 return NULL_TREE;
20539
20540 for (i = 0; i < ntparms; ++i)
20541 if (! TREE_VEC_ELT (innermost_deduced_args, i))
20542 return NULL_TREE;
20543
20544 /* Verify that nondeduced template arguments agree with the type
20545 obtained from argument deduction.
20546
20547 For example:
20548
20549 struct A { typedef int X; };
20550 template <class T, class U> struct C {};
20551 template <class T> struct C<T, typename T::X> {};
20552
20553 Then with the instantiation `C<A, int>', we can deduce that
20554 `T' is `A' but unify () does not check whether `typename T::X'
20555 is `int'. */
20556 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
20557 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20558 spec_args, tmpl,
20559 tf_none, false, false);
20560 if (spec_args == error_mark_node
20561 /* We only need to check the innermost arguments; the other
20562 arguments will always agree. */
20563 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
20564 INNERMOST_TEMPLATE_ARGS (args)))
20565 return NULL_TREE;
20566
20567 /* Now that we have bindings for all of the template arguments,
20568 ensure that the arguments deduced for the template template
20569 parameters have compatible template parameter lists. See the use
20570 of template_template_parm_bindings_ok_p in fn_type_unification
20571 for more information. */
20572 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
20573 return NULL_TREE;
20574
20575 return deduced_args;
20576 }
20577
20578 // Compare two function templates T1 and T2 by deducing bindings
20579 // from one against the other. If both deductions succeed, compare
20580 // constraints to see which is more constrained.
20581 static int
20582 more_specialized_inst (tree t1, tree t2)
20583 {
20584 int fate = 0;
20585 int count = 0;
20586
20587 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
20588 {
20589 --fate;
20590 ++count;
20591 }
20592
20593 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
20594 {
20595 ++fate;
20596 ++count;
20597 }
20598
20599 // If both deductions succeed, then one may be more constrained.
20600 if (count == 2 && fate == 0)
20601 fate = more_constrained (t1, t2);
20602
20603 return fate;
20604 }
20605
20606 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20607 Return the TREE_LIST node with the most specialized template, if
20608 any. If there is no most specialized template, the error_mark_node
20609 is returned.
20610
20611 Note that this function does not look at, or modify, the
20612 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20613 returned is one of the elements of INSTANTIATIONS, callers may
20614 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20615 and retrieve it from the value returned. */
20616
20617 tree
20618 most_specialized_instantiation (tree templates)
20619 {
20620 tree fn, champ;
20621
20622 ++processing_template_decl;
20623
20624 champ = templates;
20625 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
20626 {
20627 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
20628 if (fate == -1)
20629 champ = fn;
20630 else if (!fate)
20631 {
20632 /* Equally specialized, move to next function. If there
20633 is no next function, nothing's most specialized. */
20634 fn = TREE_CHAIN (fn);
20635 champ = fn;
20636 if (!fn)
20637 break;
20638 }
20639 }
20640
20641 if (champ)
20642 /* Now verify that champ is better than everything earlier in the
20643 instantiation list. */
20644 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
20645 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
20646 {
20647 champ = NULL_TREE;
20648 break;
20649 }
20650 }
20651
20652 processing_template_decl--;
20653
20654 if (!champ)
20655 return error_mark_node;
20656
20657 return champ;
20658 }
20659
20660 /* If DECL is a specialization of some template, return the most
20661 general such template. Otherwise, returns NULL_TREE.
20662
20663 For example, given:
20664
20665 template <class T> struct S { template <class U> void f(U); };
20666
20667 if TMPL is `template <class U> void S<int>::f(U)' this will return
20668 the full template. This function will not trace past partial
20669 specializations, however. For example, given in addition:
20670
20671 template <class T> struct S<T*> { template <class U> void f(U); };
20672
20673 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20674 `template <class T> template <class U> S<T*>::f(U)'. */
20675
20676 tree
20677 most_general_template (tree decl)
20678 {
20679 if (TREE_CODE (decl) != TEMPLATE_DECL)
20680 {
20681 if (tree tinfo = get_template_info (decl))
20682 decl = TI_TEMPLATE (tinfo);
20683 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20684 template friend, or a FIELD_DECL for a capture pack. */
20685 if (TREE_CODE (decl) != TEMPLATE_DECL)
20686 return NULL_TREE;
20687 }
20688
20689 /* Look for more and more general templates. */
20690 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
20691 {
20692 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20693 (See cp-tree.h for details.) */
20694 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
20695 break;
20696
20697 if (CLASS_TYPE_P (TREE_TYPE (decl))
20698 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
20699 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
20700 break;
20701
20702 /* Stop if we run into an explicitly specialized class template. */
20703 if (!DECL_NAMESPACE_SCOPE_P (decl)
20704 && DECL_CONTEXT (decl)
20705 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
20706 break;
20707
20708 decl = DECL_TI_TEMPLATE (decl);
20709 }
20710
20711 return decl;
20712 }
20713
20714 /* Return the most specialized of the template partial specializations
20715 which can produce TARGET, a specialization of some class or variable
20716 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
20717 a TEMPLATE_DECL node corresponding to the partial specialization, while
20718 the TREE_PURPOSE is the set of template arguments that must be
20719 substituted into the template pattern in order to generate TARGET.
20720
20721 If the choice of partial specialization is ambiguous, a diagnostic
20722 is issued, and the error_mark_node is returned. If there are no
20723 partial specializations matching TARGET, then NULL_TREE is
20724 returned, indicating that the primary template should be used. */
20725
20726 static tree
20727 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
20728 {
20729 tree list = NULL_TREE;
20730 tree t;
20731 tree champ;
20732 int fate;
20733 bool ambiguous_p;
20734 tree outer_args = NULL_TREE;
20735 tree tmpl, args;
20736
20737 if (TYPE_P (target))
20738 {
20739 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
20740 tmpl = TI_TEMPLATE (tinfo);
20741 args = TI_ARGS (tinfo);
20742 }
20743 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
20744 {
20745 tmpl = TREE_OPERAND (target, 0);
20746 args = TREE_OPERAND (target, 1);
20747 }
20748 else if (VAR_P (target))
20749 {
20750 tree tinfo = DECL_TEMPLATE_INFO (target);
20751 tmpl = TI_TEMPLATE (tinfo);
20752 args = TI_ARGS (tinfo);
20753 }
20754 else
20755 gcc_unreachable ();
20756
20757 tree main_tmpl = most_general_template (tmpl);
20758
20759 /* For determining which partial specialization to use, only the
20760 innermost args are interesting. */
20761 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20762 {
20763 outer_args = strip_innermost_template_args (args, 1);
20764 args = INNERMOST_TEMPLATE_ARGS (args);
20765 }
20766
20767 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
20768 {
20769 tree partial_spec_args;
20770 tree spec_args;
20771 tree spec_tmpl = TREE_VALUE (t);
20772
20773 partial_spec_args = TREE_PURPOSE (t);
20774
20775 ++processing_template_decl;
20776
20777 if (outer_args)
20778 {
20779 /* Discard the outer levels of args, and then substitute in the
20780 template args from the enclosing class. */
20781 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
20782 partial_spec_args = tsubst_template_args
20783 (partial_spec_args, outer_args, tf_none, NULL_TREE);
20784
20785 /* And the same for the partial specialization TEMPLATE_DECL. */
20786 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
20787 }
20788
20789 partial_spec_args =
20790 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20791 partial_spec_args,
20792 tmpl, tf_none,
20793 /*require_all_args=*/true,
20794 /*use_default_args=*/true);
20795
20796 --processing_template_decl;
20797
20798 if (partial_spec_args == error_mark_node)
20799 return error_mark_node;
20800 if (spec_tmpl == error_mark_node)
20801 return error_mark_node;
20802
20803 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
20804 spec_args = get_partial_spec_bindings (tmpl, parms,
20805 partial_spec_args,
20806 args);
20807 if (spec_args)
20808 {
20809 if (outer_args)
20810 spec_args = add_to_template_args (outer_args, spec_args);
20811
20812 /* Keep the candidate only if the constraints are satisfied,
20813 or if we're not compiling with concepts. */
20814 if (!flag_concepts
20815 || constraints_satisfied_p (spec_tmpl, spec_args))
20816 {
20817 list = tree_cons (spec_args, TREE_VALUE (t), list);
20818 TREE_TYPE (list) = TREE_TYPE (t);
20819 }
20820 }
20821 }
20822
20823 if (! list)
20824 return NULL_TREE;
20825
20826 ambiguous_p = false;
20827 t = list;
20828 champ = t;
20829 t = TREE_CHAIN (t);
20830 for (; t; t = TREE_CHAIN (t))
20831 {
20832 fate = more_specialized_partial_spec (tmpl, champ, t);
20833 if (fate == 1)
20834 ;
20835 else
20836 {
20837 if (fate == 0)
20838 {
20839 t = TREE_CHAIN (t);
20840 if (! t)
20841 {
20842 ambiguous_p = true;
20843 break;
20844 }
20845 }
20846 champ = t;
20847 }
20848 }
20849
20850 if (!ambiguous_p)
20851 for (t = list; t && t != champ; t = TREE_CHAIN (t))
20852 {
20853 fate = more_specialized_partial_spec (tmpl, champ, t);
20854 if (fate != 1)
20855 {
20856 ambiguous_p = true;
20857 break;
20858 }
20859 }
20860
20861 if (ambiguous_p)
20862 {
20863 const char *str;
20864 char *spaces = NULL;
20865 if (!(complain & tf_error))
20866 return error_mark_node;
20867 if (TYPE_P (target))
20868 error ("ambiguous template instantiation for %q#T", target);
20869 else
20870 error ("ambiguous template instantiation for %q#D", target);
20871 str = ngettext ("candidate is:", "candidates are:", list_length (list));
20872 for (t = list; t; t = TREE_CHAIN (t))
20873 {
20874 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
20875 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
20876 "%s %#S", spaces ? spaces : str, subst);
20877 spaces = spaces ? spaces : get_spaces (str);
20878 }
20879 free (spaces);
20880 return error_mark_node;
20881 }
20882
20883 return champ;
20884 }
20885
20886 /* Explicitly instantiate DECL. */
20887
20888 void
20889 do_decl_instantiation (tree decl, tree storage)
20890 {
20891 tree result = NULL_TREE;
20892 int extern_p = 0;
20893
20894 if (!decl || decl == error_mark_node)
20895 /* An error occurred, for which grokdeclarator has already issued
20896 an appropriate message. */
20897 return;
20898 else if (! DECL_LANG_SPECIFIC (decl))
20899 {
20900 error ("explicit instantiation of non-template %q#D", decl);
20901 return;
20902 }
20903
20904 bool var_templ = (DECL_TEMPLATE_INFO (decl)
20905 && variable_template_p (DECL_TI_TEMPLATE (decl)));
20906
20907 if (VAR_P (decl) && !var_templ)
20908 {
20909 /* There is an asymmetry here in the way VAR_DECLs and
20910 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20911 the latter, the DECL we get back will be marked as a
20912 template instantiation, and the appropriate
20913 DECL_TEMPLATE_INFO will be set up. This does not happen for
20914 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20915 should handle VAR_DECLs as it currently handles
20916 FUNCTION_DECLs. */
20917 if (!DECL_CLASS_SCOPE_P (decl))
20918 {
20919 error ("%qD is not a static data member of a class template", decl);
20920 return;
20921 }
20922 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
20923 if (!result || !VAR_P (result))
20924 {
20925 error ("no matching template for %qD found", decl);
20926 return;
20927 }
20928 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
20929 {
20930 error ("type %qT for explicit instantiation %qD does not match "
20931 "declared type %qT", TREE_TYPE (result), decl,
20932 TREE_TYPE (decl));
20933 return;
20934 }
20935 }
20936 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
20937 {
20938 error ("explicit instantiation of %q#D", decl);
20939 return;
20940 }
20941 else
20942 result = decl;
20943
20944 /* Check for various error cases. Note that if the explicit
20945 instantiation is valid the RESULT will currently be marked as an
20946 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20947 until we get here. */
20948
20949 if (DECL_TEMPLATE_SPECIALIZATION (result))
20950 {
20951 /* DR 259 [temp.spec].
20952
20953 Both an explicit instantiation and a declaration of an explicit
20954 specialization shall not appear in a program unless the explicit
20955 instantiation follows a declaration of the explicit specialization.
20956
20957 For a given set of template parameters, if an explicit
20958 instantiation of a template appears after a declaration of an
20959 explicit specialization for that template, the explicit
20960 instantiation has no effect. */
20961 return;
20962 }
20963 else if (DECL_EXPLICIT_INSTANTIATION (result))
20964 {
20965 /* [temp.spec]
20966
20967 No program shall explicitly instantiate any template more
20968 than once.
20969
20970 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20971 the first instantiation was `extern' and the second is not,
20972 and EXTERN_P for the opposite case. */
20973 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
20974 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
20975 /* If an "extern" explicit instantiation follows an ordinary
20976 explicit instantiation, the template is instantiated. */
20977 if (extern_p)
20978 return;
20979 }
20980 else if (!DECL_IMPLICIT_INSTANTIATION (result))
20981 {
20982 error ("no matching template for %qD found", result);
20983 return;
20984 }
20985 else if (!DECL_TEMPLATE_INFO (result))
20986 {
20987 permerror (input_location, "explicit instantiation of non-template %q#D", result);
20988 return;
20989 }
20990
20991 if (storage == NULL_TREE)
20992 ;
20993 else if (storage == ridpointers[(int) RID_EXTERN])
20994 {
20995 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
20996 pedwarn (input_location, OPT_Wpedantic,
20997 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20998 "instantiations");
20999 extern_p = 1;
21000 }
21001 else
21002 error ("storage class %qD applied to template instantiation", storage);
21003
21004 check_explicit_instantiation_namespace (result);
21005 mark_decl_instantiated (result, extern_p);
21006 if (! extern_p)
21007 instantiate_decl (result, /*defer_ok=*/1,
21008 /*expl_inst_class_mem_p=*/false);
21009 }
21010
21011 static void
21012 mark_class_instantiated (tree t, int extern_p)
21013 {
21014 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21015 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21016 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21017 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21018 if (! extern_p)
21019 {
21020 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21021 rest_of_type_compilation (t, 1);
21022 }
21023 }
21024
21025 /* Called from do_type_instantiation through binding_table_foreach to
21026 do recursive instantiation for the type bound in ENTRY. */
21027 static void
21028 bt_instantiate_type_proc (binding_entry entry, void *data)
21029 {
21030 tree storage = *(tree *) data;
21031
21032 if (MAYBE_CLASS_TYPE_P (entry->type)
21033 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
21034 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
21035 }
21036
21037 /* Called from do_type_instantiation to instantiate a member
21038 (a member function or a static member variable) of an
21039 explicitly instantiated class template. */
21040 static void
21041 instantiate_class_member (tree decl, int extern_p)
21042 {
21043 mark_decl_instantiated (decl, extern_p);
21044 if (! extern_p)
21045 instantiate_decl (decl, /*defer_ok=*/1,
21046 /*expl_inst_class_mem_p=*/true);
21047 }
21048
21049 /* Perform an explicit instantiation of template class T. STORAGE, if
21050 non-null, is the RID for extern, inline or static. COMPLAIN is
21051 nonzero if this is called from the parser, zero if called recursively,
21052 since the standard is unclear (as detailed below). */
21053
21054 void
21055 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
21056 {
21057 int extern_p = 0;
21058 int nomem_p = 0;
21059 int static_p = 0;
21060 int previous_instantiation_extern_p = 0;
21061
21062 if (TREE_CODE (t) == TYPE_DECL)
21063 t = TREE_TYPE (t);
21064
21065 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
21066 {
21067 tree tmpl =
21068 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
21069 if (tmpl)
21070 error ("explicit instantiation of non-class template %qD", tmpl);
21071 else
21072 error ("explicit instantiation of non-template type %qT", t);
21073 return;
21074 }
21075
21076 complete_type (t);
21077
21078 if (!COMPLETE_TYPE_P (t))
21079 {
21080 if (complain & tf_error)
21081 error ("explicit instantiation of %q#T before definition of template",
21082 t);
21083 return;
21084 }
21085
21086 if (storage != NULL_TREE)
21087 {
21088 if (!in_system_header_at (input_location))
21089 {
21090 if (storage == ridpointers[(int) RID_EXTERN])
21091 {
21092 if (cxx_dialect == cxx98)
21093 pedwarn (input_location, OPT_Wpedantic,
21094 "ISO C++ 1998 forbids the use of %<extern%> on "
21095 "explicit instantiations");
21096 }
21097 else
21098 pedwarn (input_location, OPT_Wpedantic,
21099 "ISO C++ forbids the use of %qE"
21100 " on explicit instantiations", storage);
21101 }
21102
21103 if (storage == ridpointers[(int) RID_INLINE])
21104 nomem_p = 1;
21105 else if (storage == ridpointers[(int) RID_EXTERN])
21106 extern_p = 1;
21107 else if (storage == ridpointers[(int) RID_STATIC])
21108 static_p = 1;
21109 else
21110 {
21111 error ("storage class %qD applied to template instantiation",
21112 storage);
21113 extern_p = 0;
21114 }
21115 }
21116
21117 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
21118 {
21119 /* DR 259 [temp.spec].
21120
21121 Both an explicit instantiation and a declaration of an explicit
21122 specialization shall not appear in a program unless the explicit
21123 instantiation follows a declaration of the explicit specialization.
21124
21125 For a given set of template parameters, if an explicit
21126 instantiation of a template appears after a declaration of an
21127 explicit specialization for that template, the explicit
21128 instantiation has no effect. */
21129 return;
21130 }
21131 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
21132 {
21133 /* [temp.spec]
21134
21135 No program shall explicitly instantiate any template more
21136 than once.
21137
21138 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21139 instantiation was `extern'. If EXTERN_P then the second is.
21140 These cases are OK. */
21141 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
21142
21143 if (!previous_instantiation_extern_p && !extern_p
21144 && (complain & tf_error))
21145 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
21146
21147 /* If we've already instantiated the template, just return now. */
21148 if (!CLASSTYPE_INTERFACE_ONLY (t))
21149 return;
21150 }
21151
21152 check_explicit_instantiation_namespace (TYPE_NAME (t));
21153 mark_class_instantiated (t, extern_p);
21154
21155 if (nomem_p)
21156 return;
21157
21158 {
21159 tree tmp;
21160
21161 /* In contrast to implicit instantiation, where only the
21162 declarations, and not the definitions, of members are
21163 instantiated, we have here:
21164
21165 [temp.explicit]
21166
21167 The explicit instantiation of a class template specialization
21168 implies the instantiation of all of its members not
21169 previously explicitly specialized in the translation unit
21170 containing the explicit instantiation.
21171
21172 Of course, we can't instantiate member template classes, since
21173 we don't have any arguments for them. Note that the standard
21174 is unclear on whether the instantiation of the members are
21175 *explicit* instantiations or not. However, the most natural
21176 interpretation is that it should be an explicit instantiation. */
21177
21178 if (! static_p)
21179 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
21180 if (TREE_CODE (tmp) == FUNCTION_DECL
21181 && DECL_TEMPLATE_INSTANTIATION (tmp))
21182 instantiate_class_member (tmp, extern_p);
21183
21184 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
21185 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
21186 instantiate_class_member (tmp, extern_p);
21187
21188 if (CLASSTYPE_NESTED_UTDS (t))
21189 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
21190 bt_instantiate_type_proc, &storage);
21191 }
21192 }
21193
21194 /* Given a function DECL, which is a specialization of TMPL, modify
21195 DECL to be a re-instantiation of TMPL with the same template
21196 arguments. TMPL should be the template into which tsubst'ing
21197 should occur for DECL, not the most general template.
21198
21199 One reason for doing this is a scenario like this:
21200
21201 template <class T>
21202 void f(const T&, int i);
21203
21204 void g() { f(3, 7); }
21205
21206 template <class T>
21207 void f(const T& t, const int i) { }
21208
21209 Note that when the template is first instantiated, with
21210 instantiate_template, the resulting DECL will have no name for the
21211 first parameter, and the wrong type for the second. So, when we go
21212 to instantiate the DECL, we regenerate it. */
21213
21214 static void
21215 regenerate_decl_from_template (tree decl, tree tmpl)
21216 {
21217 /* The arguments used to instantiate DECL, from the most general
21218 template. */
21219 tree args;
21220 tree code_pattern;
21221
21222 args = DECL_TI_ARGS (decl);
21223 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
21224
21225 /* Make sure that we can see identifiers, and compute access
21226 correctly. */
21227 push_access_scope (decl);
21228
21229 if (TREE_CODE (decl) == FUNCTION_DECL)
21230 {
21231 tree decl_parm;
21232 tree pattern_parm;
21233 tree specs;
21234 int args_depth;
21235 int parms_depth;
21236
21237 args_depth = TMPL_ARGS_DEPTH (args);
21238 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
21239 if (args_depth > parms_depth)
21240 args = get_innermost_template_args (args, parms_depth);
21241
21242 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
21243 args, tf_error, NULL_TREE,
21244 /*defer_ok*/false);
21245 if (specs && specs != error_mark_node)
21246 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
21247 specs);
21248
21249 /* Merge parameter declarations. */
21250 decl_parm = skip_artificial_parms_for (decl,
21251 DECL_ARGUMENTS (decl));
21252 pattern_parm
21253 = skip_artificial_parms_for (code_pattern,
21254 DECL_ARGUMENTS (code_pattern));
21255 while (decl_parm && !DECL_PACK_P (pattern_parm))
21256 {
21257 tree parm_type;
21258 tree attributes;
21259
21260 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21261 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
21262 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
21263 NULL_TREE);
21264 parm_type = type_decays_to (parm_type);
21265 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21266 TREE_TYPE (decl_parm) = parm_type;
21267 attributes = DECL_ATTRIBUTES (pattern_parm);
21268 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21269 {
21270 DECL_ATTRIBUTES (decl_parm) = attributes;
21271 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21272 }
21273 decl_parm = DECL_CHAIN (decl_parm);
21274 pattern_parm = DECL_CHAIN (pattern_parm);
21275 }
21276 /* Merge any parameters that match with the function parameter
21277 pack. */
21278 if (pattern_parm && DECL_PACK_P (pattern_parm))
21279 {
21280 int i, len;
21281 tree expanded_types;
21282 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21283 the parameters in this function parameter pack. */
21284 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
21285 args, tf_error, NULL_TREE);
21286 len = TREE_VEC_LENGTH (expanded_types);
21287 for (i = 0; i < len; i++)
21288 {
21289 tree parm_type;
21290 tree attributes;
21291
21292 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21293 /* Rename the parameter to include the index. */
21294 DECL_NAME (decl_parm) =
21295 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
21296 parm_type = TREE_VEC_ELT (expanded_types, i);
21297 parm_type = type_decays_to (parm_type);
21298 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21299 TREE_TYPE (decl_parm) = parm_type;
21300 attributes = DECL_ATTRIBUTES (pattern_parm);
21301 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21302 {
21303 DECL_ATTRIBUTES (decl_parm) = attributes;
21304 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21305 }
21306 decl_parm = DECL_CHAIN (decl_parm);
21307 }
21308 }
21309 /* Merge additional specifiers from the CODE_PATTERN. */
21310 if (DECL_DECLARED_INLINE_P (code_pattern)
21311 && !DECL_DECLARED_INLINE_P (decl))
21312 DECL_DECLARED_INLINE_P (decl) = 1;
21313 }
21314 else if (VAR_P (decl))
21315 {
21316 DECL_INITIAL (decl) =
21317 tsubst_expr (DECL_INITIAL (code_pattern), args,
21318 tf_error, DECL_TI_TEMPLATE (decl),
21319 /*integral_constant_expression_p=*/false);
21320 if (VAR_HAD_UNKNOWN_BOUND (decl))
21321 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
21322 tf_error, DECL_TI_TEMPLATE (decl));
21323 }
21324 else
21325 gcc_unreachable ();
21326
21327 pop_access_scope (decl);
21328 }
21329
21330 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21331 substituted to get DECL. */
21332
21333 tree
21334 template_for_substitution (tree decl)
21335 {
21336 tree tmpl = DECL_TI_TEMPLATE (decl);
21337
21338 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21339 for the instantiation. This is not always the most general
21340 template. Consider, for example:
21341
21342 template <class T>
21343 struct S { template <class U> void f();
21344 template <> void f<int>(); };
21345
21346 and an instantiation of S<double>::f<int>. We want TD to be the
21347 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21348 while (/* An instantiation cannot have a definition, so we need a
21349 more general template. */
21350 DECL_TEMPLATE_INSTANTIATION (tmpl)
21351 /* We must also deal with friend templates. Given:
21352
21353 template <class T> struct S {
21354 template <class U> friend void f() {};
21355 };
21356
21357 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21358 so far as the language is concerned, but that's still
21359 where we get the pattern for the instantiation from. On
21360 other hand, if the definition comes outside the class, say:
21361
21362 template <class T> struct S {
21363 template <class U> friend void f();
21364 };
21365 template <class U> friend void f() {}
21366
21367 we don't need to look any further. That's what the check for
21368 DECL_INITIAL is for. */
21369 || (TREE_CODE (decl) == FUNCTION_DECL
21370 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
21371 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
21372 {
21373 /* The present template, TD, should not be a definition. If it
21374 were a definition, we should be using it! Note that we
21375 cannot restructure the loop to just keep going until we find
21376 a template with a definition, since that might go too far if
21377 a specialization was declared, but not defined. */
21378
21379 /* Fetch the more general template. */
21380 tmpl = DECL_TI_TEMPLATE (tmpl);
21381 }
21382
21383 return tmpl;
21384 }
21385
21386 /* Returns true if we need to instantiate this template instance even if we
21387 know we aren't going to emit it. */
21388
21389 bool
21390 always_instantiate_p (tree decl)
21391 {
21392 /* We always instantiate inline functions so that we can inline them. An
21393 explicit instantiation declaration prohibits implicit instantiation of
21394 non-inline functions. With high levels of optimization, we would
21395 normally inline non-inline functions -- but we're not allowed to do
21396 that for "extern template" functions. Therefore, we check
21397 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21398 return ((TREE_CODE (decl) == FUNCTION_DECL
21399 && (DECL_DECLARED_INLINE_P (decl)
21400 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
21401 /* And we need to instantiate static data members so that
21402 their initializers are available in integral constant
21403 expressions. */
21404 || (VAR_P (decl)
21405 && decl_maybe_constant_var_p (decl)));
21406 }
21407
21408 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21409 instantiate it now, modifying TREE_TYPE (fn). */
21410
21411 void
21412 maybe_instantiate_noexcept (tree fn)
21413 {
21414 tree fntype, spec, noex, clone;
21415
21416 /* Don't instantiate a noexcept-specification from template context. */
21417 if (processing_template_decl)
21418 return;
21419
21420 if (DECL_CLONED_FUNCTION_P (fn))
21421 fn = DECL_CLONED_FUNCTION (fn);
21422 fntype = TREE_TYPE (fn);
21423 spec = TYPE_RAISES_EXCEPTIONS (fntype);
21424
21425 if (!spec || !TREE_PURPOSE (spec))
21426 return;
21427
21428 noex = TREE_PURPOSE (spec);
21429
21430 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
21431 {
21432 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
21433 spec = get_defaulted_eh_spec (fn);
21434 else if (push_tinst_level (fn))
21435 {
21436 push_access_scope (fn);
21437 push_deferring_access_checks (dk_no_deferred);
21438 input_location = DECL_SOURCE_LOCATION (fn);
21439 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
21440 DEFERRED_NOEXCEPT_ARGS (noex),
21441 tf_warning_or_error, fn,
21442 /*function_p=*/false,
21443 /*integral_constant_expression_p=*/true);
21444 pop_deferring_access_checks ();
21445 pop_access_scope (fn);
21446 pop_tinst_level ();
21447 spec = build_noexcept_spec (noex, tf_warning_or_error);
21448 if (spec == error_mark_node)
21449 spec = noexcept_false_spec;
21450 }
21451 else
21452 spec = noexcept_false_spec;
21453
21454 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
21455 }
21456
21457 FOR_EACH_CLONE (clone, fn)
21458 {
21459 if (TREE_TYPE (clone) == fntype)
21460 TREE_TYPE (clone) = TREE_TYPE (fn);
21461 else
21462 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
21463 }
21464 }
21465
21466 /* Produce the definition of D, a _DECL generated from a template. If
21467 DEFER_OK is nonzero, then we don't have to actually do the
21468 instantiation now; we just have to do it sometime. Normally it is
21469 an error if this is an explicit instantiation but D is undefined.
21470 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21471 explicitly instantiated class template. */
21472
21473 tree
21474 instantiate_decl (tree d, int defer_ok,
21475 bool expl_inst_class_mem_p)
21476 {
21477 tree tmpl = DECL_TI_TEMPLATE (d);
21478 tree gen_args;
21479 tree args;
21480 tree td;
21481 tree code_pattern;
21482 tree spec;
21483 tree gen_tmpl;
21484 bool pattern_defined;
21485 location_t saved_loc = input_location;
21486 int saved_unevaluated_operand = cp_unevaluated_operand;
21487 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21488 bool external_p;
21489 bool deleted_p;
21490 tree fn_context;
21491 bool nested = false;
21492
21493 /* This function should only be used to instantiate templates for
21494 functions and static member variables. */
21495 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
21496
21497 /* A concept is never instantiated. */
21498 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
21499
21500 /* Variables are never deferred; if instantiation is required, they
21501 are instantiated right away. That allows for better code in the
21502 case that an expression refers to the value of the variable --
21503 if the variable has a constant value the referring expression can
21504 take advantage of that fact. */
21505 if (VAR_P (d)
21506 || DECL_DECLARED_CONSTEXPR_P (d))
21507 defer_ok = 0;
21508
21509 /* Don't instantiate cloned functions. Instead, instantiate the
21510 functions they cloned. */
21511 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
21512 d = DECL_CLONED_FUNCTION (d);
21513
21514 if (DECL_TEMPLATE_INSTANTIATED (d)
21515 || (TREE_CODE (d) == FUNCTION_DECL
21516 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
21517 || DECL_TEMPLATE_SPECIALIZATION (d))
21518 /* D has already been instantiated or explicitly specialized, so
21519 there's nothing for us to do here.
21520
21521 It might seem reasonable to check whether or not D is an explicit
21522 instantiation, and, if so, stop here. But when an explicit
21523 instantiation is deferred until the end of the compilation,
21524 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21525 the instantiation. */
21526 return d;
21527
21528 /* Check to see whether we know that this template will be
21529 instantiated in some other file, as with "extern template"
21530 extension. */
21531 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
21532
21533 /* In general, we do not instantiate such templates. */
21534 if (external_p && !always_instantiate_p (d))
21535 return d;
21536
21537 gen_tmpl = most_general_template (tmpl);
21538 gen_args = DECL_TI_ARGS (d);
21539
21540 if (tmpl != gen_tmpl)
21541 /* We should already have the extra args. */
21542 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
21543 == TMPL_ARGS_DEPTH (gen_args));
21544 /* And what's in the hash table should match D. */
21545 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
21546 || spec == NULL_TREE);
21547
21548 /* This needs to happen before any tsubsting. */
21549 if (! push_tinst_level (d))
21550 return d;
21551
21552 timevar_push (TV_TEMPLATE_INST);
21553
21554 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21555 for the instantiation. */
21556 td = template_for_substitution (d);
21557 code_pattern = DECL_TEMPLATE_RESULT (td);
21558
21559 /* We should never be trying to instantiate a member of a class
21560 template or partial specialization. */
21561 gcc_assert (d != code_pattern);
21562
21563 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
21564 || DECL_TEMPLATE_SPECIALIZATION (td))
21565 /* In the case of a friend template whose definition is provided
21566 outside the class, we may have too many arguments. Drop the
21567 ones we don't need. The same is true for specializations. */
21568 args = get_innermost_template_args
21569 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
21570 else
21571 args = gen_args;
21572
21573 if (TREE_CODE (d) == FUNCTION_DECL)
21574 {
21575 deleted_p = DECL_DELETED_FN (code_pattern);
21576 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
21577 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
21578 || deleted_p);
21579 }
21580 else
21581 {
21582 deleted_p = false;
21583 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
21584 }
21585
21586 /* We may be in the middle of deferred access check. Disable it now. */
21587 push_deferring_access_checks (dk_no_deferred);
21588
21589 /* Unless an explicit instantiation directive has already determined
21590 the linkage of D, remember that a definition is available for
21591 this entity. */
21592 if (pattern_defined
21593 && !DECL_INTERFACE_KNOWN (d)
21594 && !DECL_NOT_REALLY_EXTERN (d))
21595 mark_definable (d);
21596
21597 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
21598 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
21599 input_location = DECL_SOURCE_LOCATION (d);
21600
21601 /* If D is a member of an explicitly instantiated class template,
21602 and no definition is available, treat it like an implicit
21603 instantiation. */
21604 if (!pattern_defined && expl_inst_class_mem_p
21605 && DECL_EXPLICIT_INSTANTIATION (d))
21606 {
21607 /* Leave linkage flags alone on instantiations with anonymous
21608 visibility. */
21609 if (TREE_PUBLIC (d))
21610 {
21611 DECL_NOT_REALLY_EXTERN (d) = 0;
21612 DECL_INTERFACE_KNOWN (d) = 0;
21613 }
21614 SET_DECL_IMPLICIT_INSTANTIATION (d);
21615 }
21616
21617 /* Defer all other templates, unless we have been explicitly
21618 forbidden from doing so. */
21619 if (/* If there is no definition, we cannot instantiate the
21620 template. */
21621 ! pattern_defined
21622 /* If it's OK to postpone instantiation, do so. */
21623 || defer_ok
21624 /* If this is a static data member that will be defined
21625 elsewhere, we don't want to instantiate the entire data
21626 member, but we do want to instantiate the initializer so that
21627 we can substitute that elsewhere. */
21628 || (external_p && VAR_P (d))
21629 /* Handle here a deleted function too, avoid generating
21630 its body (c++/61080). */
21631 || deleted_p)
21632 {
21633 /* The definition of the static data member is now required so
21634 we must substitute the initializer. */
21635 if (VAR_P (d)
21636 && !DECL_INITIAL (d)
21637 && DECL_INITIAL (code_pattern))
21638 {
21639 tree ns;
21640 tree init;
21641 bool const_init = false;
21642 bool enter_context = DECL_CLASS_SCOPE_P (d);
21643
21644 ns = decl_namespace_context (d);
21645 push_nested_namespace (ns);
21646 if (enter_context)
21647 push_nested_class (DECL_CONTEXT (d));
21648 init = tsubst_expr (DECL_INITIAL (code_pattern),
21649 args,
21650 tf_warning_or_error, NULL_TREE,
21651 /*integral_constant_expression_p=*/false);
21652 /* If instantiating the initializer involved instantiating this
21653 again, don't call cp_finish_decl twice. */
21654 if (!DECL_INITIAL (d))
21655 {
21656 /* Make sure the initializer is still constant, in case of
21657 circular dependency (template/instantiate6.C). */
21658 const_init
21659 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21660 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
21661 /*asmspec_tree=*/NULL_TREE,
21662 LOOKUP_ONLYCONVERTING);
21663 }
21664 if (enter_context)
21665 pop_nested_class ();
21666 pop_nested_namespace (ns);
21667 }
21668
21669 /* We restore the source position here because it's used by
21670 add_pending_template. */
21671 input_location = saved_loc;
21672
21673 if (at_eof && !pattern_defined
21674 && DECL_EXPLICIT_INSTANTIATION (d)
21675 && DECL_NOT_REALLY_EXTERN (d))
21676 /* [temp.explicit]
21677
21678 The definition of a non-exported function template, a
21679 non-exported member function template, or a non-exported
21680 member function or static data member of a class template
21681 shall be present in every translation unit in which it is
21682 explicitly instantiated. */
21683 permerror (input_location, "explicit instantiation of %qD "
21684 "but no definition available", d);
21685
21686 /* If we're in unevaluated context, we just wanted to get the
21687 constant value; this isn't an odr use, so don't queue
21688 a full instantiation. */
21689 if (cp_unevaluated_operand != 0)
21690 goto out;
21691 /* ??? Historically, we have instantiated inline functions, even
21692 when marked as "extern template". */
21693 if (!(external_p && VAR_P (d)))
21694 add_pending_template (d);
21695 goto out;
21696 }
21697 /* Tell the repository that D is available in this translation unit
21698 -- and see if it is supposed to be instantiated here. */
21699 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
21700 {
21701 /* In a PCH file, despite the fact that the repository hasn't
21702 requested instantiation in the PCH it is still possible that
21703 an instantiation will be required in a file that includes the
21704 PCH. */
21705 if (pch_file)
21706 add_pending_template (d);
21707 /* Instantiate inline functions so that the inliner can do its
21708 job, even though we'll not be emitting a copy of this
21709 function. */
21710 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
21711 goto out;
21712 }
21713
21714 fn_context = decl_function_context (d);
21715 nested = (current_function_decl != NULL_TREE);
21716 vec<tree> omp_privatization_save;
21717 if (nested)
21718 save_omp_privatization_clauses (omp_privatization_save);
21719
21720 if (!fn_context)
21721 push_to_top_level ();
21722 else
21723 {
21724 if (nested)
21725 push_function_context ();
21726 cp_unevaluated_operand = 0;
21727 c_inhibit_evaluation_warnings = 0;
21728 }
21729
21730 /* Mark D as instantiated so that recursive calls to
21731 instantiate_decl do not try to instantiate it again. */
21732 DECL_TEMPLATE_INSTANTIATED (d) = 1;
21733
21734 /* Regenerate the declaration in case the template has been modified
21735 by a subsequent redeclaration. */
21736 regenerate_decl_from_template (d, td);
21737
21738 /* We already set the file and line above. Reset them now in case
21739 they changed as a result of calling regenerate_decl_from_template. */
21740 input_location = DECL_SOURCE_LOCATION (d);
21741
21742 if (VAR_P (d))
21743 {
21744 tree init;
21745 bool const_init = false;
21746
21747 /* Clear out DECL_RTL; whatever was there before may not be right
21748 since we've reset the type of the declaration. */
21749 SET_DECL_RTL (d, NULL);
21750 DECL_IN_AGGR_P (d) = 0;
21751
21752 /* The initializer is placed in DECL_INITIAL by
21753 regenerate_decl_from_template so we don't need to
21754 push/pop_access_scope again here. Pull it out so that
21755 cp_finish_decl can process it. */
21756 init = DECL_INITIAL (d);
21757 DECL_INITIAL (d) = NULL_TREE;
21758 DECL_INITIALIZED_P (d) = 0;
21759
21760 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
21761 initializer. That function will defer actual emission until
21762 we have a chance to determine linkage. */
21763 DECL_EXTERNAL (d) = 0;
21764
21765 /* Enter the scope of D so that access-checking works correctly. */
21766 bool enter_context = DECL_CLASS_SCOPE_P (d);
21767 if (enter_context)
21768 push_nested_class (DECL_CONTEXT (d));
21769
21770 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21771 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
21772
21773 if (enter_context)
21774 pop_nested_class ();
21775
21776 if (variable_template_p (td))
21777 note_variable_template_instantiation (d);
21778 }
21779 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
21780 synthesize_method (d);
21781 else if (TREE_CODE (d) == FUNCTION_DECL)
21782 {
21783 hash_map<tree, tree> *saved_local_specializations;
21784 tree subst_decl;
21785 tree tmpl_parm;
21786 tree spec_parm;
21787 tree block = NULL_TREE;
21788
21789 /* Save away the current list, in case we are instantiating one
21790 template from within the body of another. */
21791 saved_local_specializations = local_specializations;
21792
21793 /* Set up the list of local specializations. */
21794 local_specializations = new hash_map<tree, tree>;
21795
21796 /* Set up context. */
21797 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21798 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21799 block = push_stmt_list ();
21800 else
21801 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
21802
21803 /* Some typedefs referenced from within the template code need to be
21804 access checked at template instantiation time, i.e now. These
21805 types were added to the template at parsing time. Let's get those
21806 and perform the access checks then. */
21807 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
21808 gen_args);
21809
21810 /* Create substitution entries for the parameters. */
21811 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
21812 tmpl_parm = DECL_ARGUMENTS (subst_decl);
21813 spec_parm = DECL_ARGUMENTS (d);
21814 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
21815 {
21816 register_local_specialization (spec_parm, tmpl_parm);
21817 spec_parm = skip_artificial_parms_for (d, spec_parm);
21818 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
21819 }
21820 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
21821 {
21822 if (!DECL_PACK_P (tmpl_parm))
21823 {
21824 register_local_specialization (spec_parm, tmpl_parm);
21825 spec_parm = DECL_CHAIN (spec_parm);
21826 }
21827 else
21828 {
21829 /* Register the (value) argument pack as a specialization of
21830 TMPL_PARM, then move on. */
21831 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
21832 register_local_specialization (argpack, tmpl_parm);
21833 }
21834 }
21835 gcc_assert (!spec_parm);
21836
21837 /* Substitute into the body of the function. */
21838 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21839 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
21840 tf_warning_or_error, tmpl);
21841 else
21842 {
21843 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
21844 tf_warning_or_error, tmpl,
21845 /*integral_constant_expression_p=*/false);
21846
21847 /* Set the current input_location to the end of the function
21848 so that finish_function knows where we are. */
21849 input_location
21850 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
21851
21852 /* Remember if we saw an infinite loop in the template. */
21853 current_function_infinite_loop
21854 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
21855 }
21856
21857 /* We don't need the local specializations any more. */
21858 delete local_specializations;
21859 local_specializations = saved_local_specializations;
21860
21861 /* Finish the function. */
21862 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21863 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21864 DECL_SAVED_TREE (d) = pop_stmt_list (block);
21865 else
21866 {
21867 d = finish_function (0);
21868 expand_or_defer_fn (d);
21869 }
21870
21871 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21872 cp_check_omp_declare_reduction (d);
21873 }
21874
21875 /* We're not deferring instantiation any more. */
21876 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
21877
21878 if (!fn_context)
21879 pop_from_top_level ();
21880 else if (nested)
21881 pop_function_context ();
21882
21883 out:
21884 input_location = saved_loc;
21885 cp_unevaluated_operand = saved_unevaluated_operand;
21886 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21887 pop_deferring_access_checks ();
21888 pop_tinst_level ();
21889 if (nested)
21890 restore_omp_privatization_clauses (omp_privatization_save);
21891
21892 timevar_pop (TV_TEMPLATE_INST);
21893
21894 return d;
21895 }
21896
21897 /* Run through the list of templates that we wish we could
21898 instantiate, and instantiate any we can. RETRIES is the
21899 number of times we retry pending template instantiation. */
21900
21901 void
21902 instantiate_pending_templates (int retries)
21903 {
21904 int reconsider;
21905 location_t saved_loc = input_location;
21906
21907 /* Instantiating templates may trigger vtable generation. This in turn
21908 may require further template instantiations. We place a limit here
21909 to avoid infinite loop. */
21910 if (pending_templates && retries >= max_tinst_depth)
21911 {
21912 tree decl = pending_templates->tinst->decl;
21913
21914 fatal_error (input_location,
21915 "template instantiation depth exceeds maximum of %d"
21916 " instantiating %q+D, possibly from virtual table generation"
21917 " (use -ftemplate-depth= to increase the maximum)",
21918 max_tinst_depth, decl);
21919 if (TREE_CODE (decl) == FUNCTION_DECL)
21920 /* Pretend that we defined it. */
21921 DECL_INITIAL (decl) = error_mark_node;
21922 return;
21923 }
21924
21925 do
21926 {
21927 struct pending_template **t = &pending_templates;
21928 struct pending_template *last = NULL;
21929 reconsider = 0;
21930 while (*t)
21931 {
21932 tree instantiation = reopen_tinst_level ((*t)->tinst);
21933 bool complete = false;
21934
21935 if (TYPE_P (instantiation))
21936 {
21937 tree fn;
21938
21939 if (!COMPLETE_TYPE_P (instantiation))
21940 {
21941 instantiate_class_template (instantiation);
21942 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
21943 for (fn = TYPE_METHODS (instantiation);
21944 fn;
21945 fn = TREE_CHAIN (fn))
21946 if (! DECL_ARTIFICIAL (fn))
21947 instantiate_decl (fn,
21948 /*defer_ok=*/0,
21949 /*expl_inst_class_mem_p=*/false);
21950 if (COMPLETE_TYPE_P (instantiation))
21951 reconsider = 1;
21952 }
21953
21954 complete = COMPLETE_TYPE_P (instantiation);
21955 }
21956 else
21957 {
21958 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
21959 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
21960 {
21961 instantiation
21962 = instantiate_decl (instantiation,
21963 /*defer_ok=*/0,
21964 /*expl_inst_class_mem_p=*/false);
21965 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
21966 reconsider = 1;
21967 }
21968
21969 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
21970 || DECL_TEMPLATE_INSTANTIATED (instantiation));
21971 }
21972
21973 if (complete)
21974 /* If INSTANTIATION has been instantiated, then we don't
21975 need to consider it again in the future. */
21976 *t = (*t)->next;
21977 else
21978 {
21979 last = *t;
21980 t = &(*t)->next;
21981 }
21982 tinst_depth = 0;
21983 current_tinst_level = NULL;
21984 }
21985 last_pending_template = last;
21986 }
21987 while (reconsider);
21988
21989 input_location = saved_loc;
21990 }
21991
21992 /* Substitute ARGVEC into T, which is a list of initializers for
21993 either base class or a non-static data member. The TREE_PURPOSEs
21994 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21995 instantiate_decl. */
21996
21997 static tree
21998 tsubst_initializer_list (tree t, tree argvec)
21999 {
22000 tree inits = NULL_TREE;
22001
22002 for (; t; t = TREE_CHAIN (t))
22003 {
22004 tree decl;
22005 tree init;
22006 tree expanded_bases = NULL_TREE;
22007 tree expanded_arguments = NULL_TREE;
22008 int i, len = 1;
22009
22010 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
22011 {
22012 tree expr;
22013 tree arg;
22014
22015 /* Expand the base class expansion type into separate base
22016 classes. */
22017 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
22018 tf_warning_or_error,
22019 NULL_TREE);
22020 if (expanded_bases == error_mark_node)
22021 continue;
22022
22023 /* We'll be building separate TREE_LISTs of arguments for
22024 each base. */
22025 len = TREE_VEC_LENGTH (expanded_bases);
22026 expanded_arguments = make_tree_vec (len);
22027 for (i = 0; i < len; i++)
22028 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
22029
22030 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22031 expand each argument in the TREE_VALUE of t. */
22032 expr = make_node (EXPR_PACK_EXPANSION);
22033 PACK_EXPANSION_LOCAL_P (expr) = true;
22034 PACK_EXPANSION_PARAMETER_PACKS (expr) =
22035 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
22036
22037 if (TREE_VALUE (t) == void_type_node)
22038 /* VOID_TYPE_NODE is used to indicate
22039 value-initialization. */
22040 {
22041 for (i = 0; i < len; i++)
22042 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
22043 }
22044 else
22045 {
22046 /* Substitute parameter packs into each argument in the
22047 TREE_LIST. */
22048 in_base_initializer = 1;
22049 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
22050 {
22051 tree expanded_exprs;
22052
22053 /* Expand the argument. */
22054 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
22055 expanded_exprs
22056 = tsubst_pack_expansion (expr, argvec,
22057 tf_warning_or_error,
22058 NULL_TREE);
22059 if (expanded_exprs == error_mark_node)
22060 continue;
22061
22062 /* Prepend each of the expanded expressions to the
22063 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22064 for (i = 0; i < len; i++)
22065 {
22066 TREE_VEC_ELT (expanded_arguments, i) =
22067 tree_cons (NULL_TREE,
22068 TREE_VEC_ELT (expanded_exprs, i),
22069 TREE_VEC_ELT (expanded_arguments, i));
22070 }
22071 }
22072 in_base_initializer = 0;
22073
22074 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22075 since we built them backwards. */
22076 for (i = 0; i < len; i++)
22077 {
22078 TREE_VEC_ELT (expanded_arguments, i) =
22079 nreverse (TREE_VEC_ELT (expanded_arguments, i));
22080 }
22081 }
22082 }
22083
22084 for (i = 0; i < len; ++i)
22085 {
22086 if (expanded_bases)
22087 {
22088 decl = TREE_VEC_ELT (expanded_bases, i);
22089 decl = expand_member_init (decl);
22090 init = TREE_VEC_ELT (expanded_arguments, i);
22091 }
22092 else
22093 {
22094 tree tmp;
22095 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
22096 tf_warning_or_error, NULL_TREE);
22097
22098 decl = expand_member_init (decl);
22099 if (decl && !DECL_P (decl))
22100 in_base_initializer = 1;
22101
22102 init = TREE_VALUE (t);
22103 tmp = init;
22104 if (init != void_type_node)
22105 init = tsubst_expr (init, argvec,
22106 tf_warning_or_error, NULL_TREE,
22107 /*integral_constant_expression_p=*/false);
22108 if (init == NULL_TREE && tmp != NULL_TREE)
22109 /* If we had an initializer but it instantiated to nothing,
22110 value-initialize the object. This will only occur when
22111 the initializer was a pack expansion where the parameter
22112 packs used in that expansion were of length zero. */
22113 init = void_type_node;
22114 in_base_initializer = 0;
22115 }
22116
22117 if (decl)
22118 {
22119 init = build_tree_list (decl, init);
22120 TREE_CHAIN (init) = inits;
22121 inits = init;
22122 }
22123 }
22124 }
22125 return inits;
22126 }
22127
22128 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22129
22130 static void
22131 set_current_access_from_decl (tree decl)
22132 {
22133 if (TREE_PRIVATE (decl))
22134 current_access_specifier = access_private_node;
22135 else if (TREE_PROTECTED (decl))
22136 current_access_specifier = access_protected_node;
22137 else
22138 current_access_specifier = access_public_node;
22139 }
22140
22141 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22142 is the instantiation (which should have been created with
22143 start_enum) and ARGS are the template arguments to use. */
22144
22145 static void
22146 tsubst_enum (tree tag, tree newtag, tree args)
22147 {
22148 tree e;
22149
22150 if (SCOPED_ENUM_P (newtag))
22151 begin_scope (sk_scoped_enum, newtag);
22152
22153 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
22154 {
22155 tree value;
22156 tree decl;
22157
22158 decl = TREE_VALUE (e);
22159 /* Note that in a template enum, the TREE_VALUE is the
22160 CONST_DECL, not the corresponding INTEGER_CST. */
22161 value = tsubst_expr (DECL_INITIAL (decl),
22162 args, tf_warning_or_error, NULL_TREE,
22163 /*integral_constant_expression_p=*/true);
22164
22165 /* Give this enumeration constant the correct access. */
22166 set_current_access_from_decl (decl);
22167
22168 /* Actually build the enumerator itself. Here we're assuming that
22169 enumerators can't have dependent attributes. */
22170 build_enumerator (DECL_NAME (decl), value, newtag,
22171 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
22172 }
22173
22174 if (SCOPED_ENUM_P (newtag))
22175 finish_scope ();
22176
22177 finish_enum_value_list (newtag);
22178 finish_enum (newtag);
22179
22180 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
22181 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
22182 }
22183
22184 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22185 its type -- but without substituting the innermost set of template
22186 arguments. So, innermost set of template parameters will appear in
22187 the type. */
22188
22189 tree
22190 get_mostly_instantiated_function_type (tree decl)
22191 {
22192 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22193 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
22194 }
22195
22196 /* Return truthvalue if we're processing a template different from
22197 the last one involved in diagnostics. */
22198 bool
22199 problematic_instantiation_changed (void)
22200 {
22201 return current_tinst_level != last_error_tinst_level;
22202 }
22203
22204 /* Remember current template involved in diagnostics. */
22205 void
22206 record_last_problematic_instantiation (void)
22207 {
22208 last_error_tinst_level = current_tinst_level;
22209 }
22210
22211 struct tinst_level *
22212 current_instantiation (void)
22213 {
22214 return current_tinst_level;
22215 }
22216
22217 /* Return TRUE if current_function_decl is being instantiated, false
22218 otherwise. */
22219
22220 bool
22221 instantiating_current_function_p (void)
22222 {
22223 return (current_instantiation ()
22224 && current_instantiation ()->decl == current_function_decl);
22225 }
22226
22227 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22228 type. Return zero for ok, nonzero for disallowed. Issue error and
22229 warning messages under control of COMPLAIN. */
22230
22231 static int
22232 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
22233 {
22234 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
22235 return 0;
22236 else if (POINTER_TYPE_P (type))
22237 return 0;
22238 else if (TYPE_PTRMEM_P (type))
22239 return 0;
22240 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
22241 return 0;
22242 else if (TREE_CODE (type) == TYPENAME_TYPE)
22243 return 0;
22244 else if (TREE_CODE (type) == DECLTYPE_TYPE)
22245 return 0;
22246 else if (TREE_CODE (type) == NULLPTR_TYPE)
22247 return 0;
22248 /* A bound template template parm could later be instantiated to have a valid
22249 nontype parm type via an alias template. */
22250 else if (cxx_dialect >= cxx11
22251 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22252 return 0;
22253
22254 if (complain & tf_error)
22255 {
22256 if (type == error_mark_node)
22257 inform (input_location, "invalid template non-type parameter");
22258 else
22259 error ("%q#T is not a valid type for a template non-type parameter",
22260 type);
22261 }
22262 return 1;
22263 }
22264
22265 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22266 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22267
22268 static bool
22269 dependent_type_p_r (tree type)
22270 {
22271 tree scope;
22272
22273 /* [temp.dep.type]
22274
22275 A type is dependent if it is:
22276
22277 -- a template parameter. Template template parameters are types
22278 for us (since TYPE_P holds true for them) so we handle
22279 them here. */
22280 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22281 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
22282 return true;
22283 /* -- a qualified-id with a nested-name-specifier which contains a
22284 class-name that names a dependent type or whose unqualified-id
22285 names a dependent type. */
22286 if (TREE_CODE (type) == TYPENAME_TYPE)
22287 return true;
22288
22289 /* An alias template specialization can be dependent even if the
22290 resulting type is not. */
22291 if (dependent_alias_template_spec_p (type))
22292 return true;
22293
22294 /* -- a cv-qualified type where the cv-unqualified type is
22295 dependent.
22296 No code is necessary for this bullet; the code below handles
22297 cv-qualified types, and we don't want to strip aliases with
22298 TYPE_MAIN_VARIANT because of DR 1558. */
22299 /* -- a compound type constructed from any dependent type. */
22300 if (TYPE_PTRMEM_P (type))
22301 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
22302 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22303 (type)));
22304 else if (TYPE_PTR_P (type)
22305 || TREE_CODE (type) == REFERENCE_TYPE)
22306 return dependent_type_p (TREE_TYPE (type));
22307 else if (TREE_CODE (type) == FUNCTION_TYPE
22308 || TREE_CODE (type) == METHOD_TYPE)
22309 {
22310 tree arg_type;
22311
22312 if (dependent_type_p (TREE_TYPE (type)))
22313 return true;
22314 for (arg_type = TYPE_ARG_TYPES (type);
22315 arg_type;
22316 arg_type = TREE_CHAIN (arg_type))
22317 if (dependent_type_p (TREE_VALUE (arg_type)))
22318 return true;
22319 return false;
22320 }
22321 /* -- an array type constructed from any dependent type or whose
22322 size is specified by a constant expression that is
22323 value-dependent.
22324
22325 We checked for type- and value-dependence of the bounds in
22326 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22327 if (TREE_CODE (type) == ARRAY_TYPE)
22328 {
22329 if (TYPE_DOMAIN (type)
22330 && dependent_type_p (TYPE_DOMAIN (type)))
22331 return true;
22332 return dependent_type_p (TREE_TYPE (type));
22333 }
22334
22335 /* -- a template-id in which either the template name is a template
22336 parameter ... */
22337 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22338 return true;
22339 /* ... or any of the template arguments is a dependent type or
22340 an expression that is type-dependent or value-dependent. */
22341 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
22342 && (any_dependent_template_arguments_p
22343 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
22344 return true;
22345
22346 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22347 dependent; if the argument of the `typeof' expression is not
22348 type-dependent, then it should already been have resolved. */
22349 if (TREE_CODE (type) == TYPEOF_TYPE
22350 || TREE_CODE (type) == DECLTYPE_TYPE
22351 || TREE_CODE (type) == UNDERLYING_TYPE)
22352 return true;
22353
22354 /* A template argument pack is dependent if any of its packed
22355 arguments are. */
22356 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
22357 {
22358 tree args = ARGUMENT_PACK_ARGS (type);
22359 int i, len = TREE_VEC_LENGTH (args);
22360 for (i = 0; i < len; ++i)
22361 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22362 return true;
22363 }
22364
22365 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22366 be template parameters. */
22367 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
22368 return true;
22369
22370 /* The standard does not specifically mention types that are local
22371 to template functions or local classes, but they should be
22372 considered dependent too. For example:
22373
22374 template <int I> void f() {
22375 enum E { a = I };
22376 S<sizeof (E)> s;
22377 }
22378
22379 The size of `E' cannot be known until the value of `I' has been
22380 determined. Therefore, `E' must be considered dependent. */
22381 scope = TYPE_CONTEXT (type);
22382 if (scope && TYPE_P (scope))
22383 return dependent_type_p (scope);
22384 /* Don't use type_dependent_expression_p here, as it can lead
22385 to infinite recursion trying to determine whether a lambda
22386 nested in a lambda is dependent (c++/47687). */
22387 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
22388 && DECL_LANG_SPECIFIC (scope)
22389 && DECL_TEMPLATE_INFO (scope)
22390 && (any_dependent_template_arguments_p
22391 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
22392 return true;
22393
22394 /* Other types are non-dependent. */
22395 return false;
22396 }
22397
22398 /* Returns TRUE if TYPE is dependent, in the sense of
22399 [temp.dep.type]. Note that a NULL type is considered dependent. */
22400
22401 bool
22402 dependent_type_p (tree type)
22403 {
22404 /* If there are no template parameters in scope, then there can't be
22405 any dependent types. */
22406 if (!processing_template_decl)
22407 {
22408 /* If we are not processing a template, then nobody should be
22409 providing us with a dependent type. */
22410 gcc_assert (type);
22411 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
22412 return false;
22413 }
22414
22415 /* If the type is NULL, we have not computed a type for the entity
22416 in question; in that case, the type is dependent. */
22417 if (!type)
22418 return true;
22419
22420 /* Erroneous types can be considered non-dependent. */
22421 if (type == error_mark_node)
22422 return false;
22423
22424 /* If we have not already computed the appropriate value for TYPE,
22425 do so now. */
22426 if (!TYPE_DEPENDENT_P_VALID (type))
22427 {
22428 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
22429 TYPE_DEPENDENT_P_VALID (type) = 1;
22430 }
22431
22432 return TYPE_DEPENDENT_P (type);
22433 }
22434
22435 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22436 lookup. In other words, a dependent type that is not the current
22437 instantiation. */
22438
22439 bool
22440 dependent_scope_p (tree scope)
22441 {
22442 return (scope && TYPE_P (scope) && dependent_type_p (scope)
22443 && !currently_open_class (scope));
22444 }
22445
22446 /* T is a SCOPE_REF; return whether we need to consider it
22447 instantiation-dependent so that we can check access at instantiation
22448 time even though we know which member it resolves to. */
22449
22450 static bool
22451 instantiation_dependent_scope_ref_p (tree t)
22452 {
22453 if (DECL_P (TREE_OPERAND (t, 1))
22454 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
22455 && accessible_in_template_p (TREE_OPERAND (t, 0),
22456 TREE_OPERAND (t, 1)))
22457 return false;
22458 else
22459 return true;
22460 }
22461
22462 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22463 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22464 expression. */
22465
22466 /* Note that this predicate is not appropriate for general expressions;
22467 only constant expressions (that satisfy potential_constant_expression)
22468 can be tested for value dependence. */
22469
22470 bool
22471 value_dependent_expression_p (tree expression)
22472 {
22473 if (!processing_template_decl)
22474 return false;
22475
22476 /* A name declared with a dependent type. */
22477 if (DECL_P (expression) && type_dependent_expression_p (expression))
22478 return true;
22479
22480 switch (TREE_CODE (expression))
22481 {
22482 case IDENTIFIER_NODE:
22483 /* A name that has not been looked up -- must be dependent. */
22484 return true;
22485
22486 case TEMPLATE_PARM_INDEX:
22487 /* A non-type template parm. */
22488 return true;
22489
22490 case CONST_DECL:
22491 /* A non-type template parm. */
22492 if (DECL_TEMPLATE_PARM_P (expression))
22493 return true;
22494 return value_dependent_expression_p (DECL_INITIAL (expression));
22495
22496 case VAR_DECL:
22497 /* A constant with literal type and is initialized
22498 with an expression that is value-dependent.
22499
22500 Note that a non-dependent parenthesized initializer will have
22501 already been replaced with its constant value, so if we see
22502 a TREE_LIST it must be dependent. */
22503 if (DECL_INITIAL (expression)
22504 && decl_constant_var_p (expression)
22505 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
22506 /* cp_finish_decl doesn't fold reference initializers. */
22507 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
22508 || value_dependent_expression_p (DECL_INITIAL (expression))))
22509 return true;
22510 return false;
22511
22512 case DYNAMIC_CAST_EXPR:
22513 case STATIC_CAST_EXPR:
22514 case CONST_CAST_EXPR:
22515 case REINTERPRET_CAST_EXPR:
22516 case CAST_EXPR:
22517 /* These expressions are value-dependent if the type to which
22518 the cast occurs is dependent or the expression being casted
22519 is value-dependent. */
22520 {
22521 tree type = TREE_TYPE (expression);
22522
22523 if (dependent_type_p (type))
22524 return true;
22525
22526 /* A functional cast has a list of operands. */
22527 expression = TREE_OPERAND (expression, 0);
22528 if (!expression)
22529 {
22530 /* If there are no operands, it must be an expression such
22531 as "int()". This should not happen for aggregate types
22532 because it would form non-constant expressions. */
22533 gcc_assert (cxx_dialect >= cxx11
22534 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
22535
22536 return false;
22537 }
22538
22539 if (TREE_CODE (expression) == TREE_LIST)
22540 return any_value_dependent_elements_p (expression);
22541
22542 return value_dependent_expression_p (expression);
22543 }
22544
22545 case SIZEOF_EXPR:
22546 if (SIZEOF_EXPR_TYPE_P (expression))
22547 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
22548 /* FALLTHRU */
22549 case ALIGNOF_EXPR:
22550 case TYPEID_EXPR:
22551 /* A `sizeof' expression is value-dependent if the operand is
22552 type-dependent or is a pack expansion. */
22553 expression = TREE_OPERAND (expression, 0);
22554 if (PACK_EXPANSION_P (expression))
22555 return true;
22556 else if (TYPE_P (expression))
22557 return dependent_type_p (expression);
22558 return instantiation_dependent_expression_p (expression);
22559
22560 case AT_ENCODE_EXPR:
22561 /* An 'encode' expression is value-dependent if the operand is
22562 type-dependent. */
22563 expression = TREE_OPERAND (expression, 0);
22564 return dependent_type_p (expression);
22565
22566 case NOEXCEPT_EXPR:
22567 expression = TREE_OPERAND (expression, 0);
22568 return instantiation_dependent_expression_p (expression);
22569
22570 case SCOPE_REF:
22571 /* All instantiation-dependent expressions should also be considered
22572 value-dependent. */
22573 return instantiation_dependent_scope_ref_p (expression);
22574
22575 case COMPONENT_REF:
22576 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
22577 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
22578
22579 case NONTYPE_ARGUMENT_PACK:
22580 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22581 is value-dependent. */
22582 {
22583 tree values = ARGUMENT_PACK_ARGS (expression);
22584 int i, len = TREE_VEC_LENGTH (values);
22585
22586 for (i = 0; i < len; ++i)
22587 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
22588 return true;
22589
22590 return false;
22591 }
22592
22593 case TRAIT_EXPR:
22594 {
22595 tree type2 = TRAIT_EXPR_TYPE2 (expression);
22596 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
22597 || (type2 ? dependent_type_p (type2) : false));
22598 }
22599
22600 case MODOP_EXPR:
22601 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22602 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
22603
22604 case ARRAY_REF:
22605 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22606 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
22607
22608 case ADDR_EXPR:
22609 {
22610 tree op = TREE_OPERAND (expression, 0);
22611 return (value_dependent_expression_p (op)
22612 || has_value_dependent_address (op));
22613 }
22614
22615 case REQUIRES_EXPR:
22616 /* Treat all requires-expressions as value-dependent so
22617 we don't try to fold them. */
22618 return true;
22619
22620 case TYPE_REQ:
22621 return dependent_type_p (TREE_OPERAND (expression, 0));
22622
22623 case CALL_EXPR:
22624 {
22625 tree fn = get_callee_fndecl (expression);
22626 int i, nargs;
22627 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
22628 return true;
22629 nargs = call_expr_nargs (expression);
22630 for (i = 0; i < nargs; ++i)
22631 {
22632 tree op = CALL_EXPR_ARG (expression, i);
22633 /* In a call to a constexpr member function, look through the
22634 implicit ADDR_EXPR on the object argument so that it doesn't
22635 cause the call to be considered value-dependent. We also
22636 look through it in potential_constant_expression. */
22637 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
22638 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
22639 && TREE_CODE (op) == ADDR_EXPR)
22640 op = TREE_OPERAND (op, 0);
22641 if (value_dependent_expression_p (op))
22642 return true;
22643 }
22644 return false;
22645 }
22646
22647 case TEMPLATE_ID_EXPR:
22648 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22649 type-dependent. */
22650 return type_dependent_expression_p (expression)
22651 || variable_concept_p (TREE_OPERAND (expression, 0));
22652
22653 case CONSTRUCTOR:
22654 {
22655 unsigned ix;
22656 tree val;
22657 if (dependent_type_p (TREE_TYPE (expression)))
22658 return true;
22659 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
22660 if (value_dependent_expression_p (val))
22661 return true;
22662 return false;
22663 }
22664
22665 case STMT_EXPR:
22666 /* Treat a GNU statement expression as dependent to avoid crashing
22667 under instantiate_non_dependent_expr; it can't be constant. */
22668 return true;
22669
22670 default:
22671 /* A constant expression is value-dependent if any subexpression is
22672 value-dependent. */
22673 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
22674 {
22675 case tcc_reference:
22676 case tcc_unary:
22677 case tcc_comparison:
22678 case tcc_binary:
22679 case tcc_expression:
22680 case tcc_vl_exp:
22681 {
22682 int i, len = cp_tree_operand_length (expression);
22683
22684 for (i = 0; i < len; i++)
22685 {
22686 tree t = TREE_OPERAND (expression, i);
22687
22688 /* In some cases, some of the operands may be missing.l
22689 (For example, in the case of PREDECREMENT_EXPR, the
22690 amount to increment by may be missing.) That doesn't
22691 make the expression dependent. */
22692 if (t && value_dependent_expression_p (t))
22693 return true;
22694 }
22695 }
22696 break;
22697 default:
22698 break;
22699 }
22700 break;
22701 }
22702
22703 /* The expression is not value-dependent. */
22704 return false;
22705 }
22706
22707 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
22708 [temp.dep.expr]. Note that an expression with no type is
22709 considered dependent. Other parts of the compiler arrange for an
22710 expression with type-dependent subexpressions to have no type, so
22711 this function doesn't have to be fully recursive. */
22712
22713 bool
22714 type_dependent_expression_p (tree expression)
22715 {
22716 if (!processing_template_decl)
22717 return false;
22718
22719 if (expression == NULL_TREE || expression == error_mark_node)
22720 return false;
22721
22722 /* An unresolved name is always dependent. */
22723 if (identifier_p (expression)
22724 || TREE_CODE (expression) == USING_DECL
22725 || TREE_CODE (expression) == WILDCARD_DECL)
22726 return true;
22727
22728 /* A fold expression is type-dependent. */
22729 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
22730 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
22731 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
22732 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
22733 return true;
22734
22735 /* Some expression forms are never type-dependent. */
22736 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
22737 || TREE_CODE (expression) == SIZEOF_EXPR
22738 || TREE_CODE (expression) == ALIGNOF_EXPR
22739 || TREE_CODE (expression) == AT_ENCODE_EXPR
22740 || TREE_CODE (expression) == NOEXCEPT_EXPR
22741 || TREE_CODE (expression) == TRAIT_EXPR
22742 || TREE_CODE (expression) == TYPEID_EXPR
22743 || TREE_CODE (expression) == DELETE_EXPR
22744 || TREE_CODE (expression) == VEC_DELETE_EXPR
22745 || TREE_CODE (expression) == THROW_EXPR
22746 || TREE_CODE (expression) == REQUIRES_EXPR)
22747 return false;
22748
22749 /* The types of these expressions depends only on the type to which
22750 the cast occurs. */
22751 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
22752 || TREE_CODE (expression) == STATIC_CAST_EXPR
22753 || TREE_CODE (expression) == CONST_CAST_EXPR
22754 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
22755 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
22756 || TREE_CODE (expression) == CAST_EXPR)
22757 return dependent_type_p (TREE_TYPE (expression));
22758
22759 /* The types of these expressions depends only on the type created
22760 by the expression. */
22761 if (TREE_CODE (expression) == NEW_EXPR
22762 || TREE_CODE (expression) == VEC_NEW_EXPR)
22763 {
22764 /* For NEW_EXPR tree nodes created inside a template, either
22765 the object type itself or a TREE_LIST may appear as the
22766 operand 1. */
22767 tree type = TREE_OPERAND (expression, 1);
22768 if (TREE_CODE (type) == TREE_LIST)
22769 /* This is an array type. We need to check array dimensions
22770 as well. */
22771 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
22772 || value_dependent_expression_p
22773 (TREE_OPERAND (TREE_VALUE (type), 1));
22774 else
22775 return dependent_type_p (type);
22776 }
22777
22778 if (TREE_CODE (expression) == SCOPE_REF)
22779 {
22780 tree scope = TREE_OPERAND (expression, 0);
22781 tree name = TREE_OPERAND (expression, 1);
22782
22783 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
22784 contains an identifier associated by name lookup with one or more
22785 declarations declared with a dependent type, or...a
22786 nested-name-specifier or qualified-id that names a member of an
22787 unknown specialization. */
22788 return (type_dependent_expression_p (name)
22789 || dependent_scope_p (scope));
22790 }
22791
22792 /* A function template specialization is type-dependent if it has any
22793 dependent template arguments. */
22794 if (TREE_CODE (expression) == FUNCTION_DECL
22795 && DECL_LANG_SPECIFIC (expression)
22796 && DECL_TEMPLATE_INFO (expression))
22797 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
22798
22799 if (TREE_CODE (expression) == TEMPLATE_DECL
22800 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
22801 return false;
22802
22803 if (TREE_CODE (expression) == STMT_EXPR)
22804 expression = stmt_expr_value_expr (expression);
22805
22806 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
22807 {
22808 tree elt;
22809 unsigned i;
22810
22811 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
22812 {
22813 if (type_dependent_expression_p (elt))
22814 return true;
22815 }
22816 return false;
22817 }
22818
22819 /* A static data member of the current instantiation with incomplete
22820 array type is type-dependent, as the definition and specializations
22821 can have different bounds. */
22822 if (VAR_P (expression)
22823 && DECL_CLASS_SCOPE_P (expression)
22824 && dependent_type_p (DECL_CONTEXT (expression))
22825 && VAR_HAD_UNKNOWN_BOUND (expression))
22826 return true;
22827
22828 /* An array of unknown bound depending on a variadic parameter, eg:
22829
22830 template<typename... Args>
22831 void foo (Args... args)
22832 {
22833 int arr[] = { args... };
22834 }
22835
22836 template<int... vals>
22837 void bar ()
22838 {
22839 int arr[] = { vals... };
22840 }
22841
22842 If the array has no length and has an initializer, it must be that
22843 we couldn't determine its length in cp_complete_array_type because
22844 it is dependent. */
22845 if (VAR_P (expression)
22846 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
22847 && !TYPE_DOMAIN (TREE_TYPE (expression))
22848 && DECL_INITIAL (expression))
22849 return true;
22850
22851 /* A variable template specialization is type-dependent if it has any
22852 dependent template arguments. */
22853 if (VAR_P (expression)
22854 && DECL_LANG_SPECIFIC (expression)
22855 && DECL_TEMPLATE_INFO (expression)
22856 && variable_template_p (DECL_TI_TEMPLATE (expression)))
22857 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
22858
22859 /* Always dependent, on the number of arguments if nothing else. */
22860 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
22861 return true;
22862
22863 if (TREE_TYPE (expression) == unknown_type_node)
22864 {
22865 if (TREE_CODE (expression) == ADDR_EXPR)
22866 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
22867 if (TREE_CODE (expression) == COMPONENT_REF
22868 || TREE_CODE (expression) == OFFSET_REF)
22869 {
22870 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
22871 return true;
22872 expression = TREE_OPERAND (expression, 1);
22873 if (identifier_p (expression))
22874 return false;
22875 }
22876 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22877 if (TREE_CODE (expression) == SCOPE_REF)
22878 return false;
22879
22880 if (BASELINK_P (expression))
22881 {
22882 if (BASELINK_OPTYPE (expression)
22883 && dependent_type_p (BASELINK_OPTYPE (expression)))
22884 return true;
22885 expression = BASELINK_FUNCTIONS (expression);
22886 }
22887
22888 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
22889 {
22890 if (any_dependent_template_arguments_p
22891 (TREE_OPERAND (expression, 1)))
22892 return true;
22893 expression = TREE_OPERAND (expression, 0);
22894 if (identifier_p (expression))
22895 return true;
22896 }
22897
22898 gcc_assert (TREE_CODE (expression) == OVERLOAD
22899 || TREE_CODE (expression) == FUNCTION_DECL);
22900
22901 while (expression)
22902 {
22903 if (type_dependent_expression_p (OVL_CURRENT (expression)))
22904 return true;
22905 expression = OVL_NEXT (expression);
22906 }
22907 return false;
22908 }
22909
22910 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
22911
22912 return (dependent_type_p (TREE_TYPE (expression)));
22913 }
22914
22915 /* walk_tree callback function for instantiation_dependent_expression_p,
22916 below. Returns non-zero if a dependent subexpression is found. */
22917
22918 static tree
22919 instantiation_dependent_r (tree *tp, int *walk_subtrees,
22920 void * /*data*/)
22921 {
22922 if (TYPE_P (*tp))
22923 {
22924 /* We don't have to worry about decltype currently because decltype
22925 of an instantiation-dependent expr is a dependent type. This
22926 might change depending on the resolution of DR 1172. */
22927 *walk_subtrees = false;
22928 return NULL_TREE;
22929 }
22930 enum tree_code code = TREE_CODE (*tp);
22931 switch (code)
22932 {
22933 /* Don't treat an argument list as dependent just because it has no
22934 TREE_TYPE. */
22935 case TREE_LIST:
22936 case TREE_VEC:
22937 return NULL_TREE;
22938
22939 case VAR_DECL:
22940 case CONST_DECL:
22941 /* A constant with a dependent initializer is dependent. */
22942 if (value_dependent_expression_p (*tp))
22943 return *tp;
22944 break;
22945
22946 case TEMPLATE_PARM_INDEX:
22947 return *tp;
22948
22949 /* Handle expressions with type operands. */
22950 case SIZEOF_EXPR:
22951 case ALIGNOF_EXPR:
22952 case TYPEID_EXPR:
22953 case AT_ENCODE_EXPR:
22954 {
22955 tree op = TREE_OPERAND (*tp, 0);
22956 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
22957 op = TREE_TYPE (op);
22958 if (TYPE_P (op))
22959 {
22960 if (dependent_type_p (op))
22961 return *tp;
22962 else
22963 {
22964 *walk_subtrees = false;
22965 return NULL_TREE;
22966 }
22967 }
22968 break;
22969 }
22970
22971 case TRAIT_EXPR:
22972 if (value_dependent_expression_p (*tp))
22973 return *tp;
22974 *walk_subtrees = false;
22975 return NULL_TREE;
22976
22977 case COMPONENT_REF:
22978 if (identifier_p (TREE_OPERAND (*tp, 1)))
22979 /* In a template, finish_class_member_access_expr creates a
22980 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22981 type-dependent, so that we can check access control at
22982 instantiation time (PR 42277). See also Core issue 1273. */
22983 return *tp;
22984 break;
22985
22986 case SCOPE_REF:
22987 if (instantiation_dependent_scope_ref_p (*tp))
22988 return *tp;
22989 else
22990 break;
22991
22992 /* Treat statement-expressions as dependent. */
22993 case BIND_EXPR:
22994 return *tp;
22995
22996 /* Treat requires-expressions as dependent. */
22997 case REQUIRES_EXPR:
22998 return *tp;
22999
23000 case CALL_EXPR:
23001 /* Treat calls to function concepts as dependent. */
23002 if (function_concept_check_p (*tp))
23003 return *tp;
23004 break;
23005
23006 case TEMPLATE_ID_EXPR:
23007 /* And variable concepts. */
23008 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
23009 return *tp;
23010 break;
23011
23012 default:
23013 break;
23014 }
23015
23016 if (type_dependent_expression_p (*tp))
23017 return *tp;
23018 else
23019 return NULL_TREE;
23020 }
23021
23022 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23023 sense defined by the ABI:
23024
23025 "An expression is instantiation-dependent if it is type-dependent
23026 or value-dependent, or it has a subexpression that is type-dependent
23027 or value-dependent." */
23028
23029 bool
23030 instantiation_dependent_expression_p (tree expression)
23031 {
23032 tree result;
23033
23034 if (!processing_template_decl)
23035 return false;
23036
23037 if (expression == error_mark_node)
23038 return false;
23039
23040 result = cp_walk_tree_without_duplicates (&expression,
23041 instantiation_dependent_r, NULL);
23042 return result != NULL_TREE;
23043 }
23044
23045 /* Like type_dependent_expression_p, but it also works while not processing
23046 a template definition, i.e. during substitution or mangling. */
23047
23048 bool
23049 type_dependent_expression_p_push (tree expr)
23050 {
23051 bool b;
23052 ++processing_template_decl;
23053 b = type_dependent_expression_p (expr);
23054 --processing_template_decl;
23055 return b;
23056 }
23057
23058 /* Returns TRUE if ARGS contains a type-dependent expression. */
23059
23060 bool
23061 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
23062 {
23063 unsigned int i;
23064 tree arg;
23065
23066 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
23067 {
23068 if (type_dependent_expression_p (arg))
23069 return true;
23070 }
23071 return false;
23072 }
23073
23074 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23075 expressions) contains any type-dependent expressions. */
23076
23077 bool
23078 any_type_dependent_elements_p (const_tree list)
23079 {
23080 for (; list; list = TREE_CHAIN (list))
23081 if (type_dependent_expression_p (TREE_VALUE (list)))
23082 return true;
23083
23084 return false;
23085 }
23086
23087 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23088 expressions) contains any value-dependent expressions. */
23089
23090 bool
23091 any_value_dependent_elements_p (const_tree list)
23092 {
23093 for (; list; list = TREE_CHAIN (list))
23094 if (value_dependent_expression_p (TREE_VALUE (list)))
23095 return true;
23096
23097 return false;
23098 }
23099
23100 /* Returns TRUE if the ARG (a template argument) is dependent. */
23101
23102 bool
23103 dependent_template_arg_p (tree arg)
23104 {
23105 if (!processing_template_decl)
23106 return false;
23107
23108 /* Assume a template argument that was wrongly written by the user
23109 is dependent. This is consistent with what
23110 any_dependent_template_arguments_p [that calls this function]
23111 does. */
23112 if (!arg || arg == error_mark_node)
23113 return true;
23114
23115 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
23116 arg = ARGUMENT_PACK_SELECT_ARG (arg);
23117
23118 if (TREE_CODE (arg) == TEMPLATE_DECL
23119 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
23120 return dependent_template_p (arg);
23121 else if (ARGUMENT_PACK_P (arg))
23122 {
23123 tree args = ARGUMENT_PACK_ARGS (arg);
23124 int i, len = TREE_VEC_LENGTH (args);
23125 for (i = 0; i < len; ++i)
23126 {
23127 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23128 return true;
23129 }
23130
23131 return false;
23132 }
23133 else if (TYPE_P (arg))
23134 return dependent_type_p (arg);
23135 else
23136 return (type_dependent_expression_p (arg)
23137 || value_dependent_expression_p (arg));
23138 }
23139
23140 /* Returns true if ARGS (a collection of template arguments) contains
23141 any types that require structural equality testing. */
23142
23143 bool
23144 any_template_arguments_need_structural_equality_p (tree args)
23145 {
23146 int i;
23147 int j;
23148
23149 if (!args)
23150 return false;
23151 if (args == error_mark_node)
23152 return true;
23153
23154 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23155 {
23156 tree level = TMPL_ARGS_LEVEL (args, i + 1);
23157 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23158 {
23159 tree arg = TREE_VEC_ELT (level, j);
23160 tree packed_args = NULL_TREE;
23161 int k, len = 1;
23162
23163 if (ARGUMENT_PACK_P (arg))
23164 {
23165 /* Look inside the argument pack. */
23166 packed_args = ARGUMENT_PACK_ARGS (arg);
23167 len = TREE_VEC_LENGTH (packed_args);
23168 }
23169
23170 for (k = 0; k < len; ++k)
23171 {
23172 if (packed_args)
23173 arg = TREE_VEC_ELT (packed_args, k);
23174
23175 if (error_operand_p (arg))
23176 return true;
23177 else if (TREE_CODE (arg) == TEMPLATE_DECL)
23178 continue;
23179 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
23180 return true;
23181 else if (!TYPE_P (arg) && TREE_TYPE (arg)
23182 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
23183 return true;
23184 }
23185 }
23186 }
23187
23188 return false;
23189 }
23190
23191 /* Returns true if ARGS (a collection of template arguments) contains
23192 any dependent arguments. */
23193
23194 bool
23195 any_dependent_template_arguments_p (const_tree args)
23196 {
23197 int i;
23198 int j;
23199
23200 if (!args)
23201 return false;
23202 if (args == error_mark_node)
23203 return true;
23204
23205 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23206 {
23207 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
23208 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23209 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
23210 return true;
23211 }
23212
23213 return false;
23214 }
23215
23216 /* Returns TRUE if the template TMPL is dependent. */
23217
23218 bool
23219 dependent_template_p (tree tmpl)
23220 {
23221 if (TREE_CODE (tmpl) == OVERLOAD)
23222 {
23223 while (tmpl)
23224 {
23225 if (dependent_template_p (OVL_CURRENT (tmpl)))
23226 return true;
23227 tmpl = OVL_NEXT (tmpl);
23228 }
23229 return false;
23230 }
23231
23232 /* Template template parameters are dependent. */
23233 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
23234 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
23235 return true;
23236 /* So are names that have not been looked up. */
23237 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
23238 return true;
23239 /* So are member templates of dependent classes. */
23240 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
23241 return dependent_type_p (DECL_CONTEXT (tmpl));
23242 return false;
23243 }
23244
23245 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23246
23247 bool
23248 dependent_template_id_p (tree tmpl, tree args)
23249 {
23250 return (dependent_template_p (tmpl)
23251 || any_dependent_template_arguments_p (args));
23252 }
23253
23254 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23255 are dependent. */
23256
23257 bool
23258 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
23259 {
23260 int i;
23261
23262 if (!processing_template_decl)
23263 return false;
23264
23265 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
23266 {
23267 tree decl = TREE_VEC_ELT (declv, i);
23268 tree init = TREE_VEC_ELT (initv, i);
23269 tree cond = TREE_VEC_ELT (condv, i);
23270 tree incr = TREE_VEC_ELT (incrv, i);
23271
23272 if (type_dependent_expression_p (decl)
23273 || TREE_CODE (decl) == SCOPE_REF)
23274 return true;
23275
23276 if (init && type_dependent_expression_p (init))
23277 return true;
23278
23279 if (type_dependent_expression_p (cond))
23280 return true;
23281
23282 if (COMPARISON_CLASS_P (cond)
23283 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
23284 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
23285 return true;
23286
23287 if (TREE_CODE (incr) == MODOP_EXPR)
23288 {
23289 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
23290 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
23291 return true;
23292 }
23293 else if (type_dependent_expression_p (incr))
23294 return true;
23295 else if (TREE_CODE (incr) == MODIFY_EXPR)
23296 {
23297 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
23298 return true;
23299 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
23300 {
23301 tree t = TREE_OPERAND (incr, 1);
23302 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
23303 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
23304 return true;
23305 }
23306 }
23307 }
23308
23309 return false;
23310 }
23311
23312 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23313 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23314 no such TYPE can be found. Note that this function peers inside
23315 uninstantiated templates and therefore should be used only in
23316 extremely limited situations. ONLY_CURRENT_P restricts this
23317 peering to the currently open classes hierarchy (which is required
23318 when comparing types). */
23319
23320 tree
23321 resolve_typename_type (tree type, bool only_current_p)
23322 {
23323 tree scope;
23324 tree name;
23325 tree decl;
23326 int quals;
23327 tree pushed_scope;
23328 tree result;
23329
23330 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
23331
23332 scope = TYPE_CONTEXT (type);
23333 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23334 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23335 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23336 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23337 identifier of the TYPENAME_TYPE anymore.
23338 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23339 TYPENAME_TYPE instead, we avoid messing up with a possible
23340 typedef variant case. */
23341 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
23342
23343 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23344 it first before we can figure out what NAME refers to. */
23345 if (TREE_CODE (scope) == TYPENAME_TYPE)
23346 {
23347 if (TYPENAME_IS_RESOLVING_P (scope))
23348 /* Given a class template A with a dependent base with nested type C,
23349 typedef typename A::C::C C will land us here, as trying to resolve
23350 the initial A::C leads to the local C typedef, which leads back to
23351 A::C::C. So we break the recursion now. */
23352 return type;
23353 else
23354 scope = resolve_typename_type (scope, only_current_p);
23355 }
23356 /* If we don't know what SCOPE refers to, then we cannot resolve the
23357 TYPENAME_TYPE. */
23358 if (TREE_CODE (scope) == TYPENAME_TYPE)
23359 return type;
23360 /* If the SCOPE is a template type parameter, we have no way of
23361 resolving the name. */
23362 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
23363 return type;
23364 /* If the SCOPE is not the current instantiation, there's no reason
23365 to look inside it. */
23366 if (only_current_p && !currently_open_class (scope))
23367 return type;
23368 /* If this is a typedef, we don't want to look inside (c++/11987). */
23369 if (typedef_variant_p (type))
23370 return type;
23371 /* If SCOPE isn't the template itself, it will not have a valid
23372 TYPE_FIELDS list. */
23373 if (CLASS_TYPE_P (scope)
23374 && same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
23375 /* scope is either the template itself or a compatible instantiation
23376 like X<T>, so look up the name in the original template. */
23377 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
23378 else
23379 /* scope is a partial instantiation, so we can't do the lookup or we
23380 will lose the template arguments. */
23381 return type;
23382 /* Enter the SCOPE so that name lookup will be resolved as if we
23383 were in the class definition. In particular, SCOPE will no
23384 longer be considered a dependent type. */
23385 pushed_scope = push_scope (scope);
23386 /* Look up the declaration. */
23387 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
23388 tf_warning_or_error);
23389
23390 result = NULL_TREE;
23391
23392 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23393 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23394 if (!decl)
23395 /*nop*/;
23396 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
23397 && TREE_CODE (decl) == TYPE_DECL)
23398 {
23399 result = TREE_TYPE (decl);
23400 if (result == error_mark_node)
23401 result = NULL_TREE;
23402 }
23403 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
23404 && DECL_CLASS_TEMPLATE_P (decl))
23405 {
23406 tree tmpl;
23407 tree args;
23408 /* Obtain the template and the arguments. */
23409 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
23410 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
23411 /* Instantiate the template. */
23412 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
23413 /*entering_scope=*/0,
23414 tf_error | tf_user);
23415 if (result == error_mark_node)
23416 result = NULL_TREE;
23417 }
23418
23419 /* Leave the SCOPE. */
23420 if (pushed_scope)
23421 pop_scope (pushed_scope);
23422
23423 /* If we failed to resolve it, return the original typename. */
23424 if (!result)
23425 return type;
23426
23427 /* If lookup found a typename type, resolve that too. */
23428 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
23429 {
23430 /* Ill-formed programs can cause infinite recursion here, so we
23431 must catch that. */
23432 TYPENAME_IS_RESOLVING_P (type) = 1;
23433 result = resolve_typename_type (result, only_current_p);
23434 TYPENAME_IS_RESOLVING_P (type) = 0;
23435 }
23436
23437 /* Qualify the resulting type. */
23438 quals = cp_type_quals (type);
23439 if (quals)
23440 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
23441
23442 return result;
23443 }
23444
23445 /* EXPR is an expression which is not type-dependent. Return a proxy
23446 for EXPR that can be used to compute the types of larger
23447 expressions containing EXPR. */
23448
23449 tree
23450 build_non_dependent_expr (tree expr)
23451 {
23452 tree inner_expr;
23453
23454 /* When checking, try to get a constant value for all non-dependent
23455 expressions in order to expose bugs in *_dependent_expression_p
23456 and constexpr. */
23457 if (flag_checking && cxx_dialect >= cxx11
23458 /* Don't do this during nsdmi parsing as it can lead to
23459 unexpected recursive instantiations. */
23460 && !parsing_nsdmi ())
23461 fold_non_dependent_expr (expr);
23462
23463 /* Preserve OVERLOADs; the functions must be available to resolve
23464 types. */
23465 inner_expr = expr;
23466 if (TREE_CODE (inner_expr) == STMT_EXPR)
23467 inner_expr = stmt_expr_value_expr (inner_expr);
23468 if (TREE_CODE (inner_expr) == ADDR_EXPR)
23469 inner_expr = TREE_OPERAND (inner_expr, 0);
23470 if (TREE_CODE (inner_expr) == COMPONENT_REF)
23471 inner_expr = TREE_OPERAND (inner_expr, 1);
23472 if (is_overloaded_fn (inner_expr)
23473 || TREE_CODE (inner_expr) == OFFSET_REF)
23474 return expr;
23475 /* There is no need to return a proxy for a variable. */
23476 if (VAR_P (expr))
23477 return expr;
23478 /* Preserve string constants; conversions from string constants to
23479 "char *" are allowed, even though normally a "const char *"
23480 cannot be used to initialize a "char *". */
23481 if (TREE_CODE (expr) == STRING_CST)
23482 return expr;
23483 /* Preserve void and arithmetic constants, as an optimization -- there is no
23484 reason to create a new node. */
23485 if (TREE_CODE (expr) == VOID_CST
23486 || TREE_CODE (expr) == INTEGER_CST
23487 || TREE_CODE (expr) == REAL_CST)
23488 return expr;
23489 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23490 There is at least one place where we want to know that a
23491 particular expression is a throw-expression: when checking a ?:
23492 expression, there are special rules if the second or third
23493 argument is a throw-expression. */
23494 if (TREE_CODE (expr) == THROW_EXPR)
23495 return expr;
23496
23497 /* Don't wrap an initializer list, we need to be able to look inside. */
23498 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
23499 return expr;
23500
23501 /* Don't wrap a dummy object, we need to be able to test for it. */
23502 if (is_dummy_object (expr))
23503 return expr;
23504
23505 if (TREE_CODE (expr) == COND_EXPR)
23506 return build3 (COND_EXPR,
23507 TREE_TYPE (expr),
23508 TREE_OPERAND (expr, 0),
23509 (TREE_OPERAND (expr, 1)
23510 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
23511 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
23512 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
23513 if (TREE_CODE (expr) == COMPOUND_EXPR
23514 && !COMPOUND_EXPR_OVERLOADED (expr))
23515 return build2 (COMPOUND_EXPR,
23516 TREE_TYPE (expr),
23517 TREE_OPERAND (expr, 0),
23518 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
23519
23520 /* If the type is unknown, it can't really be non-dependent */
23521 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
23522
23523 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23524 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
23525 }
23526
23527 /* ARGS is a vector of expressions as arguments to a function call.
23528 Replace the arguments with equivalent non-dependent expressions.
23529 This modifies ARGS in place. */
23530
23531 void
23532 make_args_non_dependent (vec<tree, va_gc> *args)
23533 {
23534 unsigned int ix;
23535 tree arg;
23536
23537 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
23538 {
23539 tree newarg = build_non_dependent_expr (arg);
23540 if (newarg != arg)
23541 (*args)[ix] = newarg;
23542 }
23543 }
23544
23545 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23546 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23547 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23548
23549 static tree
23550 make_auto_1 (tree name, bool set_canonical)
23551 {
23552 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
23553 TYPE_NAME (au) = build_decl (input_location,
23554 TYPE_DECL, name, au);
23555 TYPE_STUB_DECL (au) = TYPE_NAME (au);
23556 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
23557 (0, processing_template_decl + 1, processing_template_decl + 1,
23558 TYPE_NAME (au), NULL_TREE);
23559 if (set_canonical)
23560 TYPE_CANONICAL (au) = canonical_type_parameter (au);
23561 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
23562 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
23563
23564 return au;
23565 }
23566
23567 tree
23568 make_decltype_auto (void)
23569 {
23570 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23571 }
23572
23573 tree
23574 make_auto (void)
23575 {
23576 return make_auto_1 (get_identifier ("auto"), true);
23577 }
23578
23579 /* Make a "constrained auto" type-specifier. This is an
23580 auto type with constraints that must be associated after
23581 deduction. The constraint is formed from the given
23582 CONC and its optional sequence of arguments, which are
23583 non-null if written as partial-concept-id. */
23584
23585 tree
23586 make_constrained_auto (tree con, tree args)
23587 {
23588 tree type = make_auto_1 (get_identifier ("auto"), false);
23589
23590 /* Build the constraint. */
23591 tree tmpl = DECL_TI_TEMPLATE (con);
23592 tree expr;
23593 if (VAR_P (con))
23594 expr = build_concept_check (tmpl, type, args);
23595 else
23596 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
23597
23598 tree constr = make_predicate_constraint (expr);
23599 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
23600
23601 /* Our canonical type depends on the constraint. */
23602 TYPE_CANONICAL (type) = canonical_type_parameter (type);
23603
23604 /* Attach the constraint to the type declaration. */
23605 tree decl = TYPE_NAME (type);
23606 return decl;
23607 }
23608
23609 /* Given type ARG, return std::initializer_list<ARG>. */
23610
23611 static tree
23612 listify (tree arg)
23613 {
23614 tree std_init_list = namespace_binding
23615 (get_identifier ("initializer_list"), std_node);
23616 tree argvec;
23617 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
23618 {
23619 error ("deducing from brace-enclosed initializer list requires "
23620 "#include <initializer_list>");
23621 return error_mark_node;
23622 }
23623 argvec = make_tree_vec (1);
23624 TREE_VEC_ELT (argvec, 0) = arg;
23625 return lookup_template_class (std_init_list, argvec, NULL_TREE,
23626 NULL_TREE, 0, tf_warning_or_error);
23627 }
23628
23629 /* Replace auto in TYPE with std::initializer_list<auto>. */
23630
23631 static tree
23632 listify_autos (tree type, tree auto_node)
23633 {
23634 tree init_auto = listify (auto_node);
23635 tree argvec = make_tree_vec (1);
23636 TREE_VEC_ELT (argvec, 0) = init_auto;
23637 if (processing_template_decl)
23638 argvec = add_to_template_args (current_template_args (), argvec);
23639 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
23640 }
23641
23642 /* Hash traits for hashing possibly constrained 'auto'
23643 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23644
23645 struct auto_hash : default_hash_traits<tree>
23646 {
23647 static inline hashval_t hash (tree);
23648 static inline bool equal (tree, tree);
23649 };
23650
23651 /* Hash the 'auto' T. */
23652
23653 inline hashval_t
23654 auto_hash::hash (tree t)
23655 {
23656 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
23657 /* Matching constrained-type-specifiers denote the same template
23658 parameter, so hash the constraint. */
23659 return hash_placeholder_constraint (c);
23660 else
23661 /* But unconstrained autos are all separate, so just hash the pointer. */
23662 return iterative_hash_object (t, 0);
23663 }
23664
23665 /* Compare two 'auto's. */
23666
23667 inline bool
23668 auto_hash::equal (tree t1, tree t2)
23669 {
23670 if (t1 == t2)
23671 return true;
23672
23673 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
23674 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
23675
23676 /* Two unconstrained autos are distinct. */
23677 if (!c1 || !c2)
23678 return false;
23679
23680 return equivalent_placeholder_constraints (c1, c2);
23681 }
23682
23683 /* for_each_template_parm callback for extract_autos: if t is a (possibly
23684 constrained) auto, add it to the vector. */
23685
23686 static int
23687 extract_autos_r (tree t, void *data)
23688 {
23689 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
23690 if (is_auto_or_concept (t))
23691 {
23692 /* All the autos were built with index 0; fix that up now. */
23693 tree *p = hash.find_slot (t, INSERT);
23694 unsigned idx;
23695 if (*p)
23696 /* If this is a repeated constrained-type-specifier, use the index we
23697 chose before. */
23698 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
23699 else
23700 {
23701 /* Otherwise this is new, so use the current count. */
23702 *p = t;
23703 idx = hash.elements () - 1;
23704 }
23705 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
23706 }
23707
23708 /* Always keep walking. */
23709 return 0;
23710 }
23711
23712 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
23713 says they can appear anywhere in the type. */
23714
23715 static tree
23716 extract_autos (tree type)
23717 {
23718 hash_set<tree> visited;
23719 hash_table<auto_hash> hash (2);
23720
23721 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
23722
23723 tree tree_vec = make_tree_vec (hash.elements());
23724 for (hash_table<auto_hash>::iterator iter = hash.begin();
23725 iter != hash.end(); ++iter)
23726 {
23727 tree elt = *iter;
23728 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
23729 TREE_VEC_ELT (tree_vec, i)
23730 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
23731 }
23732
23733 return tree_vec;
23734 }
23735
23736 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23737 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
23738
23739 tree
23740 do_auto_deduction (tree type, tree init, tree auto_node)
23741 {
23742 return do_auto_deduction (type, init, auto_node,
23743 tf_warning_or_error,
23744 adc_unspecified);
23745 }
23746
23747 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23748 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
23749 The CONTEXT determines the context in which auto deduction is performed
23750 and is used to control error diagnostics. */
23751
23752 tree
23753 do_auto_deduction (tree type, tree init, tree auto_node,
23754 tsubst_flags_t complain, auto_deduction_context context)
23755 {
23756 tree targs;
23757
23758 if (init == error_mark_node)
23759 return error_mark_node;
23760
23761 if (type_dependent_expression_p (init))
23762 /* Defining a subset of type-dependent expressions that we can deduce
23763 from ahead of time isn't worth the trouble. */
23764 return type;
23765
23766 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
23767 with either a new invented type template parameter U or, if the
23768 initializer is a braced-init-list (8.5.4), with
23769 std::initializer_list<U>. */
23770 if (BRACE_ENCLOSED_INITIALIZER_P (init))
23771 {
23772 if (!DIRECT_LIST_INIT_P (init))
23773 type = listify_autos (type, auto_node);
23774 else if (CONSTRUCTOR_NELTS (init) == 1)
23775 init = CONSTRUCTOR_ELT (init, 0)->value;
23776 else
23777 {
23778 if (complain & tf_warning_or_error)
23779 {
23780 if (permerror (input_location, "direct-list-initialization of "
23781 "%<auto%> requires exactly one element"))
23782 inform (input_location,
23783 "for deduction to %<std::initializer_list%>, use copy-"
23784 "list-initialization (i.e. add %<=%> before the %<{%>)");
23785 }
23786 type = listify_autos (type, auto_node);
23787 }
23788 }
23789
23790 if (type == error_mark_node)
23791 return error_mark_node;
23792
23793 init = resolve_nondeduced_context (init);
23794
23795 if (AUTO_IS_DECLTYPE (auto_node))
23796 {
23797 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
23798 && !REF_PARENTHESIZED_P (init)));
23799 targs = make_tree_vec (1);
23800 TREE_VEC_ELT (targs, 0)
23801 = finish_decltype_type (init, id, tf_warning_or_error);
23802 if (type != auto_node)
23803 {
23804 if (complain & tf_error)
23805 error ("%qT as type rather than plain %<decltype(auto)%>", type);
23806 return error_mark_node;
23807 }
23808 }
23809 else
23810 {
23811 tree parms = build_tree_list (NULL_TREE, type);
23812 tree tparms;
23813
23814 if (flag_concepts)
23815 tparms = extract_autos (type);
23816 else
23817 {
23818 tparms = make_tree_vec (1);
23819 TREE_VEC_ELT (tparms, 0)
23820 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
23821 }
23822
23823 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
23824 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
23825 DEDUCE_CALL, LOOKUP_NORMAL,
23826 NULL, /*explain_p=*/false);
23827 if (val > 0)
23828 {
23829 if (processing_template_decl)
23830 /* Try again at instantiation time. */
23831 return type;
23832 if (type && type != error_mark_node
23833 && (complain & tf_error))
23834 /* If type is error_mark_node a diagnostic must have been
23835 emitted by now. Also, having a mention to '<type error>'
23836 in the diagnostic is not really useful to the user. */
23837 {
23838 if (cfun && auto_node == current_function_auto_return_pattern
23839 && LAMBDA_FUNCTION_P (current_function_decl))
23840 error ("unable to deduce lambda return type from %qE", init);
23841 else
23842 error ("unable to deduce %qT from %qE", type, init);
23843 type_unification_real (tparms, targs, parms, &init, 1, 0,
23844 DEDUCE_CALL, LOOKUP_NORMAL,
23845 NULL, /*explain_p=*/true);
23846 }
23847 return error_mark_node;
23848 }
23849 }
23850
23851 /* Check any placeholder constraints against the deduced type. */
23852 if (flag_concepts && !processing_template_decl)
23853 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
23854 {
23855 /* Use the deduced type to check the associated constraints. */
23856 if (!constraints_satisfied_p (constr, targs))
23857 {
23858 if (complain & tf_warning_or_error)
23859 {
23860 switch (context)
23861 {
23862 case adc_unspecified:
23863 error("placeholder constraints not satisfied");
23864 break;
23865 case adc_variable_type:
23866 error ("deduced initializer does not satisfy "
23867 "placeholder constraints");
23868 break;
23869 case adc_return_type:
23870 error ("deduced return type does not satisfy "
23871 "placeholder constraints");
23872 break;
23873 case adc_requirement:
23874 error ("deduced expression type does not saatisy "
23875 "placeholder constraints");
23876 break;
23877 }
23878 diagnose_constraints (input_location, constr, targs);
23879 }
23880 return error_mark_node;
23881 }
23882 }
23883
23884 if (processing_template_decl)
23885 targs = add_to_template_args (current_template_args (), targs);
23886 return tsubst (type, targs, complain, NULL_TREE);
23887 }
23888
23889 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
23890 result. */
23891
23892 tree
23893 splice_late_return_type (tree type, tree late_return_type)
23894 {
23895 if (is_auto (type))
23896 {
23897 if (late_return_type)
23898 return late_return_type;
23899
23900 tree idx = get_template_parm_index (type);
23901 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
23902 /* In an abbreviated function template we didn't know we were dealing
23903 with a function template when we saw the auto return type, so update
23904 it to have the correct level. */
23905 return make_auto_1 (TYPE_IDENTIFIER (type), true);
23906 }
23907 return type;
23908 }
23909
23910 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
23911 'decltype(auto)'. */
23912
23913 bool
23914 is_auto (const_tree type)
23915 {
23916 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23917 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
23918 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
23919 return true;
23920 else
23921 return false;
23922 }
23923
23924 /* for_each_template_parm callback for type_uses_auto. */
23925
23926 int
23927 is_auto_r (tree tp, void */*data*/)
23928 {
23929 return is_auto_or_concept (tp);
23930 }
23931
23932 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
23933 a use of `auto'. Returns NULL_TREE otherwise. */
23934
23935 tree
23936 type_uses_auto (tree type)
23937 {
23938 if (type == NULL_TREE)
23939 return NULL_TREE;
23940 else if (flag_concepts)
23941 {
23942 /* The Concepts TS allows multiple autos in one type-specifier; just
23943 return the first one we find, do_auto_deduction will collect all of
23944 them. */
23945 if (uses_template_parms (type))
23946 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
23947 /*visited*/NULL, /*nondeduced*/true);
23948 else
23949 return NULL_TREE;
23950 }
23951 else
23952 return find_type_usage (type, is_auto);
23953 }
23954
23955 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
23956 'decltype(auto)' or a concept. */
23957
23958 bool
23959 is_auto_or_concept (const_tree type)
23960 {
23961 return is_auto (type); // or concept
23962 }
23963
23964 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
23965 a concept identifier) iff TYPE contains a use of a generic type. Returns
23966 NULL_TREE otherwise. */
23967
23968 tree
23969 type_uses_auto_or_concept (tree type)
23970 {
23971 return find_type_usage (type, is_auto_or_concept);
23972 }
23973
23974
23975 /* For a given template T, return the vector of typedefs referenced
23976 in T for which access check is needed at T instantiation time.
23977 T is either a FUNCTION_DECL or a RECORD_TYPE.
23978 Those typedefs were added to T by the function
23979 append_type_to_template_for_access_check. */
23980
23981 vec<qualified_typedef_usage_t, va_gc> *
23982 get_types_needing_access_check (tree t)
23983 {
23984 tree ti;
23985 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
23986
23987 if (!t || t == error_mark_node)
23988 return NULL;
23989
23990 if (!(ti = get_template_info (t)))
23991 return NULL;
23992
23993 if (CLASS_TYPE_P (t)
23994 || TREE_CODE (t) == FUNCTION_DECL)
23995 {
23996 if (!TI_TEMPLATE (ti))
23997 return NULL;
23998
23999 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
24000 }
24001
24002 return result;
24003 }
24004
24005 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
24006 tied to T. That list of typedefs will be access checked at
24007 T instantiation time.
24008 T is either a FUNCTION_DECL or a RECORD_TYPE.
24009 TYPE_DECL is a TYPE_DECL node representing a typedef.
24010 SCOPE is the scope through which TYPE_DECL is accessed.
24011 LOCATION is the location of the usage point of TYPE_DECL.
24012
24013 This function is a subroutine of
24014 append_type_to_template_for_access_check. */
24015
24016 static void
24017 append_type_to_template_for_access_check_1 (tree t,
24018 tree type_decl,
24019 tree scope,
24020 location_t location)
24021 {
24022 qualified_typedef_usage_t typedef_usage;
24023 tree ti;
24024
24025 if (!t || t == error_mark_node)
24026 return;
24027
24028 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
24029 || CLASS_TYPE_P (t))
24030 && type_decl
24031 && TREE_CODE (type_decl) == TYPE_DECL
24032 && scope);
24033
24034 if (!(ti = get_template_info (t)))
24035 return;
24036
24037 gcc_assert (TI_TEMPLATE (ti));
24038
24039 typedef_usage.typedef_decl = type_decl;
24040 typedef_usage.context = scope;
24041 typedef_usage.locus = location;
24042
24043 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
24044 }
24045
24046 /* Append TYPE_DECL to the template TEMPL.
24047 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24048 At TEMPL instanciation time, TYPE_DECL will be checked to see
24049 if it can be accessed through SCOPE.
24050 LOCATION is the location of the usage point of TYPE_DECL.
24051
24052 e.g. consider the following code snippet:
24053
24054 class C
24055 {
24056 typedef int myint;
24057 };
24058
24059 template<class U> struct S
24060 {
24061 C::myint mi; // <-- usage point of the typedef C::myint
24062 };
24063
24064 S<char> s;
24065
24066 At S<char> instantiation time, we need to check the access of C::myint
24067 In other words, we need to check the access of the myint typedef through
24068 the C scope. For that purpose, this function will add the myint typedef
24069 and the scope C through which its being accessed to a list of typedefs
24070 tied to the template S. That list will be walked at template instantiation
24071 time and access check performed on each typedefs it contains.
24072 Note that this particular code snippet should yield an error because
24073 myint is private to C. */
24074
24075 void
24076 append_type_to_template_for_access_check (tree templ,
24077 tree type_decl,
24078 tree scope,
24079 location_t location)
24080 {
24081 qualified_typedef_usage_t *iter;
24082 unsigned i;
24083
24084 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
24085
24086 /* Make sure we don't append the type to the template twice. */
24087 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
24088 if (iter->typedef_decl == type_decl && scope == iter->context)
24089 return;
24090
24091 append_type_to_template_for_access_check_1 (templ, type_decl,
24092 scope, location);
24093 }
24094
24095 /* Convert the generic type parameters in PARM that match the types given in the
24096 range [START_IDX, END_IDX) from the current_template_parms into generic type
24097 packs. */
24098
24099 tree
24100 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
24101 {
24102 tree current = current_template_parms;
24103 int depth = TMPL_PARMS_DEPTH (current);
24104 current = INNERMOST_TEMPLATE_PARMS (current);
24105 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
24106
24107 for (int i = 0; i < start_idx; ++i)
24108 TREE_VEC_ELT (replacement, i)
24109 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24110
24111 for (int i = start_idx; i < end_idx; ++i)
24112 {
24113 /* Create a distinct parameter pack type from the current parm and add it
24114 to the replacement args to tsubst below into the generic function
24115 parameter. */
24116
24117 tree o = TREE_TYPE (TREE_VALUE
24118 (TREE_VEC_ELT (current, i)));
24119 tree t = copy_type (o);
24120 TEMPLATE_TYPE_PARM_INDEX (t)
24121 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
24122 o, 0, 0, tf_none);
24123 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
24124 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
24125 TYPE_MAIN_VARIANT (t) = t;
24126 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
24127 TYPE_CANONICAL (t) = canonical_type_parameter (t);
24128 TREE_VEC_ELT (replacement, i) = t;
24129 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
24130 }
24131
24132 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
24133 TREE_VEC_ELT (replacement, i)
24134 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24135
24136 /* If there are more levels then build up the replacement with the outer
24137 template parms. */
24138 if (depth > 1)
24139 replacement = add_to_template_args (template_parms_to_args
24140 (TREE_CHAIN (current_template_parms)),
24141 replacement);
24142
24143 return tsubst (parm, replacement, tf_none, NULL_TREE);
24144 }
24145
24146 /* Entries in the decl_constraint hash table. */
24147 struct GTY((for_user)) constr_entry
24148 {
24149 tree decl;
24150 tree ci;
24151 };
24152
24153 /* Hashing function and equality for constraint entries. */
24154 struct constr_hasher : ggc_ptr_hash<constr_entry>
24155 {
24156 static hashval_t hash (constr_entry *e)
24157 {
24158 return (hashval_t)DECL_UID (e->decl);
24159 }
24160
24161 static bool equal (constr_entry *e1, constr_entry *e2)
24162 {
24163 return e1->decl == e2->decl;
24164 }
24165 };
24166
24167 /* A mapping from declarations to constraint information. Note that
24168 both templates and their underlying declarations are mapped to the
24169 same constraint information.
24170
24171 FIXME: This is defined in pt.c because garbage collection
24172 code is not being generated for constraint.cc. */
24173
24174 static GTY (()) hash_table<constr_hasher> *decl_constraints;
24175
24176 /* Returns true iff cinfo contains a valid set of constraints.
24177 This is the case when the associated requirements have been
24178 successfully decomposed into lists of atomic constraints.
24179 That is, when the saved assumptions are not error_mark_node. */
24180
24181 bool
24182 valid_constraints_p (tree cinfo)
24183 {
24184 gcc_assert (cinfo);
24185 return CI_ASSUMPTIONS (cinfo) != error_mark_node;
24186 }
24187
24188 /* Returns the template constraints of declaration T. If T is not
24189 constrained, return NULL_TREE. Note that T must be non-null. */
24190
24191 tree
24192 get_constraints (tree t)
24193 {
24194 gcc_assert (DECL_P (t));
24195 if (TREE_CODE (t) == TEMPLATE_DECL)
24196 t = DECL_TEMPLATE_RESULT (t);
24197 constr_entry elt = { t, NULL_TREE };
24198 constr_entry* found = decl_constraints->find (&elt);
24199 if (found)
24200 return found->ci;
24201 else
24202 return NULL_TREE;
24203 }
24204
24205 /* Associate the given constraint information CI with the declaration
24206 T. If T is a template, then the constraints are associated with
24207 its underlying declaration. Don't build associations if CI is
24208 NULL_TREE. */
24209
24210 void
24211 set_constraints (tree t, tree ci)
24212 {
24213 if (!ci)
24214 return;
24215 gcc_assert (t);
24216 if (TREE_CODE (t) == TEMPLATE_DECL)
24217 t = DECL_TEMPLATE_RESULT (t);
24218 gcc_assert (!get_constraints (t));
24219 constr_entry elt = {t, ci};
24220 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
24221 constr_entry* entry = ggc_alloc<constr_entry> ();
24222 *entry = elt;
24223 *slot = entry;
24224 }
24225
24226 /* Remove the associated constraints of the declaration T. */
24227
24228 void
24229 remove_constraints (tree t)
24230 {
24231 gcc_assert (DECL_P (t));
24232 if (TREE_CODE (t) == TEMPLATE_DECL)
24233 t = DECL_TEMPLATE_RESULT (t);
24234
24235 constr_entry elt = {t, NULL_TREE};
24236 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
24237 if (slot)
24238 decl_constraints->clear_slot (slot);
24239 }
24240
24241 /* Set up the hash table for constraint association. */
24242
24243 void
24244 init_constraint_processing (void)
24245 {
24246 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
24247 }
24248
24249 /* Set up the hash tables for template instantiations. */
24250
24251 void
24252 init_template_processing (void)
24253 {
24254 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
24255 type_specializations = hash_table<spec_hasher>::create_ggc (37);
24256 }
24257
24258 /* Print stats about the template hash tables for -fstats. */
24259
24260 void
24261 print_template_statistics (void)
24262 {
24263 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
24264 "%f collisions\n", (long) decl_specializations->size (),
24265 (long) decl_specializations->elements (),
24266 decl_specializations->collisions ());
24267 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
24268 "%f collisions\n", (long) type_specializations->size (),
24269 (long) type_specializations->elements (),
24270 type_specializations->collisions ());
24271 }
24272
24273 #include "gt-cp-pt.h"