]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Update copyright years.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43
44 /* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46 typedef int (*tree_fn_t) (tree, void*);
47
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
54 };
55
56 static GTY(()) struct pending_template *pending_templates;
57 static GTY(()) struct pending_template *last_pending_template;
58
59 int processing_template_parmlist;
60 static int template_header_count;
61
62 static GTY(()) tree saved_trees;
63 static vec<int> inline_parm_levels;
64
65 static GTY(()) struct tinst_level *current_tinst_level;
66
67 static GTY(()) tree saved_access_scope;
68
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr;
73
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
76 //
77 // Implementation of the RAII helper for creating new local
78 // specializations.
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
81 {
82 local_specializations = new hash_map<tree, tree>;
83 }
84
85 local_specialization_stack::~local_specialization_stack ()
86 {
87 delete local_specializations;
88 local_specializations = saved;
89 }
90
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth;
93
94 struct GTY((for_user)) spec_entry
95 {
96 tree tmpl;
97 tree args;
98 tree spec;
99 };
100
101 struct spec_hasher : ggc_ptr_hash<spec_entry>
102 {
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
105 };
106
107 static GTY (()) hash_table<spec_hasher> *decl_specializations;
108
109 static GTY (()) hash_table<spec_hasher> *type_specializations;
110
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
116
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
125
126 enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
130 };
131
132 static void push_access_scope (tree);
133 static void pop_access_scope (tree);
134 static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137 static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139 static int unify (tree, tree, tree, tree, int, bool);
140 static void add_pending_template (tree);
141 static tree reopen_tinst_level (struct tinst_level *);
142 static tree tsubst_initializer_list (tree, tree);
143 static tree get_partial_spec_bindings (tree, tree, tree, 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 if (!cp_unevaluated_operand)
12296 register_local_specialization (r, t);
12297
12298 DECL_CHAIN (r) = NULL_TREE;
12299
12300 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12301 /*flags=*/0,
12302 args, complain, in_decl);
12303
12304 /* Preserve a typedef that names a type. */
12305 if (is_typedef_decl (r))
12306 {
12307 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12308 set_underlying_type (r);
12309 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12310 /* An alias template specialization can be dependent
12311 even if its underlying type is not. */
12312 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12313 }
12314
12315 layout_decl (r, 0);
12316 }
12317 break;
12318
12319 default:
12320 gcc_unreachable ();
12321 }
12322 #undef RETURN
12323
12324 out:
12325 /* Restore the file and line information. */
12326 input_location = saved_loc;
12327
12328 return r;
12329 }
12330
12331 /* Substitute into the ARG_TYPES of a function type.
12332 If END is a TREE_CHAIN, leave it and any following types
12333 un-substituted. */
12334
12335 static tree
12336 tsubst_arg_types (tree arg_types,
12337 tree args,
12338 tree end,
12339 tsubst_flags_t complain,
12340 tree in_decl)
12341 {
12342 tree remaining_arg_types;
12343 tree type = NULL_TREE;
12344 int i = 1;
12345 tree expanded_args = NULL_TREE;
12346 tree default_arg;
12347
12348 if (!arg_types || arg_types == void_list_node || arg_types == end)
12349 return arg_types;
12350
12351 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12352 args, end, complain, in_decl);
12353 if (remaining_arg_types == error_mark_node)
12354 return error_mark_node;
12355
12356 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12357 {
12358 /* For a pack expansion, perform substitution on the
12359 entire expression. Later on, we'll handle the arguments
12360 one-by-one. */
12361 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12362 args, complain, in_decl);
12363
12364 if (TREE_CODE (expanded_args) == TREE_VEC)
12365 /* So that we'll spin through the parameters, one by one. */
12366 i = TREE_VEC_LENGTH (expanded_args);
12367 else
12368 {
12369 /* We only partially substituted into the parameter
12370 pack. Our type is TYPE_PACK_EXPANSION. */
12371 type = expanded_args;
12372 expanded_args = NULL_TREE;
12373 }
12374 }
12375
12376 while (i > 0) {
12377 --i;
12378
12379 if (expanded_args)
12380 type = TREE_VEC_ELT (expanded_args, i);
12381 else if (!type)
12382 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12383
12384 if (type == error_mark_node)
12385 return error_mark_node;
12386 if (VOID_TYPE_P (type))
12387 {
12388 if (complain & tf_error)
12389 {
12390 error ("invalid parameter type %qT", type);
12391 if (in_decl)
12392 error ("in declaration %q+D", in_decl);
12393 }
12394 return error_mark_node;
12395 }
12396 /* DR 657. */
12397 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12398 return error_mark_node;
12399
12400 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12401 top-level qualifiers as required. */
12402 type = cv_unqualified (type_decays_to (type));
12403
12404 /* We do not substitute into default arguments here. The standard
12405 mandates that they be instantiated only when needed, which is
12406 done in build_over_call. */
12407 default_arg = TREE_PURPOSE (arg_types);
12408
12409 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12410 {
12411 /* We've instantiated a template before its default arguments
12412 have been parsed. This can happen for a nested template
12413 class, and is not an error unless we require the default
12414 argument in a call of this function. */
12415 remaining_arg_types =
12416 tree_cons (default_arg, type, remaining_arg_types);
12417 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12418 }
12419 else
12420 remaining_arg_types =
12421 hash_tree_cons (default_arg, type, remaining_arg_types);
12422 }
12423
12424 return remaining_arg_types;
12425 }
12426
12427 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12428 *not* handle the exception-specification for FNTYPE, because the
12429 initial substitution of explicitly provided template parameters
12430 during argument deduction forbids substitution into the
12431 exception-specification:
12432
12433 [temp.deduct]
12434
12435 All references in the function type of the function template to the
12436 corresponding template parameters are replaced by the specified tem-
12437 plate argument values. If a substitution in a template parameter or
12438 in the function type of the function template results in an invalid
12439 type, type deduction fails. [Note: The equivalent substitution in
12440 exception specifications is done only when the function is instanti-
12441 ated, at which point a program is ill-formed if the substitution
12442 results in an invalid type.] */
12443
12444 static tree
12445 tsubst_function_type (tree t,
12446 tree args,
12447 tsubst_flags_t complain,
12448 tree in_decl)
12449 {
12450 tree return_type;
12451 tree arg_types = NULL_TREE;
12452 tree fntype;
12453
12454 /* The TYPE_CONTEXT is not used for function/method types. */
12455 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12456
12457 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12458 failure. */
12459 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12460
12461 if (late_return_type_p)
12462 {
12463 /* Substitute the argument types. */
12464 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12465 complain, in_decl);
12466 if (arg_types == error_mark_node)
12467 return error_mark_node;
12468
12469 tree save_ccp = current_class_ptr;
12470 tree save_ccr = current_class_ref;
12471 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12472 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12473 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12474 if (do_inject)
12475 {
12476 /* DR 1207: 'this' is in scope in the trailing return type. */
12477 inject_this_parameter (this_type, cp_type_quals (this_type));
12478 }
12479
12480 /* Substitute the return type. */
12481 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12482
12483 if (do_inject)
12484 {
12485 current_class_ptr = save_ccp;
12486 current_class_ref = save_ccr;
12487 }
12488 }
12489 else
12490 /* Substitute the return type. */
12491 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12492
12493 if (return_type == error_mark_node)
12494 return error_mark_node;
12495 /* DR 486 clarifies that creation of a function type with an
12496 invalid return type is a deduction failure. */
12497 if (TREE_CODE (return_type) == ARRAY_TYPE
12498 || TREE_CODE (return_type) == FUNCTION_TYPE)
12499 {
12500 if (complain & tf_error)
12501 {
12502 if (TREE_CODE (return_type) == ARRAY_TYPE)
12503 error ("function returning an array");
12504 else
12505 error ("function returning a function");
12506 }
12507 return error_mark_node;
12508 }
12509 /* And DR 657. */
12510 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12511 return error_mark_node;
12512
12513 if (!late_return_type_p)
12514 {
12515 /* Substitute the argument types. */
12516 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12517 complain, in_decl);
12518 if (arg_types == error_mark_node)
12519 return error_mark_node;
12520 }
12521
12522 /* Construct a new type node and return it. */
12523 if (TREE_CODE (t) == FUNCTION_TYPE)
12524 {
12525 fntype = build_function_type (return_type, arg_types);
12526 fntype = apply_memfn_quals (fntype,
12527 type_memfn_quals (t),
12528 type_memfn_rqual (t));
12529 }
12530 else
12531 {
12532 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12533 /* Don't pick up extra function qualifiers from the basetype. */
12534 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12535 if (! MAYBE_CLASS_TYPE_P (r))
12536 {
12537 /* [temp.deduct]
12538
12539 Type deduction may fail for any of the following
12540 reasons:
12541
12542 -- Attempting to create "pointer to member of T" when T
12543 is not a class type. */
12544 if (complain & tf_error)
12545 error ("creating pointer to member function of non-class type %qT",
12546 r);
12547 return error_mark_node;
12548 }
12549
12550 fntype = build_method_type_directly (r, return_type,
12551 TREE_CHAIN (arg_types));
12552 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12553 }
12554 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12555
12556 if (late_return_type_p)
12557 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12558
12559 return fntype;
12560 }
12561
12562 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12563 ARGS into that specification, and return the substituted
12564 specification. If there is no specification, return NULL_TREE. */
12565
12566 static tree
12567 tsubst_exception_specification (tree fntype,
12568 tree args,
12569 tsubst_flags_t complain,
12570 tree in_decl,
12571 bool defer_ok)
12572 {
12573 tree specs;
12574 tree new_specs;
12575
12576 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12577 new_specs = NULL_TREE;
12578 if (specs && TREE_PURPOSE (specs))
12579 {
12580 /* A noexcept-specifier. */
12581 tree expr = TREE_PURPOSE (specs);
12582 if (TREE_CODE (expr) == INTEGER_CST)
12583 new_specs = expr;
12584 else if (defer_ok)
12585 {
12586 /* Defer instantiation of noexcept-specifiers to avoid
12587 excessive instantiations (c++/49107). */
12588 new_specs = make_node (DEFERRED_NOEXCEPT);
12589 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12590 {
12591 /* We already partially instantiated this member template,
12592 so combine the new args with the old. */
12593 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12594 = DEFERRED_NOEXCEPT_PATTERN (expr);
12595 DEFERRED_NOEXCEPT_ARGS (new_specs)
12596 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12597 }
12598 else
12599 {
12600 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12601 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12602 }
12603 }
12604 else
12605 new_specs = tsubst_copy_and_build
12606 (expr, args, complain, in_decl, /*function_p=*/false,
12607 /*integral_constant_expression_p=*/true);
12608 new_specs = build_noexcept_spec (new_specs, complain);
12609 }
12610 else if (specs)
12611 {
12612 if (! TREE_VALUE (specs))
12613 new_specs = specs;
12614 else
12615 while (specs)
12616 {
12617 tree spec;
12618 int i, len = 1;
12619 tree expanded_specs = NULL_TREE;
12620
12621 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12622 {
12623 /* Expand the pack expansion type. */
12624 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12625 args, complain,
12626 in_decl);
12627
12628 if (expanded_specs == error_mark_node)
12629 return error_mark_node;
12630 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12631 len = TREE_VEC_LENGTH (expanded_specs);
12632 else
12633 {
12634 /* We're substituting into a member template, so
12635 we got a TYPE_PACK_EXPANSION back. Add that
12636 expansion and move on. */
12637 gcc_assert (TREE_CODE (expanded_specs)
12638 == TYPE_PACK_EXPANSION);
12639 new_specs = add_exception_specifier (new_specs,
12640 expanded_specs,
12641 complain);
12642 specs = TREE_CHAIN (specs);
12643 continue;
12644 }
12645 }
12646
12647 for (i = 0; i < len; ++i)
12648 {
12649 if (expanded_specs)
12650 spec = TREE_VEC_ELT (expanded_specs, i);
12651 else
12652 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12653 if (spec == error_mark_node)
12654 return spec;
12655 new_specs = add_exception_specifier (new_specs, spec,
12656 complain);
12657 }
12658
12659 specs = TREE_CHAIN (specs);
12660 }
12661 }
12662 return new_specs;
12663 }
12664
12665 /* Take the tree structure T and replace template parameters used
12666 therein with the argument vector ARGS. IN_DECL is an associated
12667 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12668 Issue error and warning messages under control of COMPLAIN. Note
12669 that we must be relatively non-tolerant of extensions here, in
12670 order to preserve conformance; if we allow substitutions that
12671 should not be allowed, we may allow argument deductions that should
12672 not succeed, and therefore report ambiguous overload situations
12673 where there are none. In theory, we could allow the substitution,
12674 but indicate that it should have failed, and allow our caller to
12675 make sure that the right thing happens, but we don't try to do this
12676 yet.
12677
12678 This function is used for dealing with types, decls and the like;
12679 for expressions, use tsubst_expr or tsubst_copy. */
12680
12681 tree
12682 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12683 {
12684 enum tree_code code;
12685 tree type, r = NULL_TREE;
12686
12687 if (t == NULL_TREE || t == error_mark_node
12688 || t == integer_type_node
12689 || t == void_type_node
12690 || t == char_type_node
12691 || t == unknown_type_node
12692 || TREE_CODE (t) == NAMESPACE_DECL
12693 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12694 return t;
12695
12696 if (DECL_P (t))
12697 return tsubst_decl (t, args, complain);
12698
12699 if (args == NULL_TREE)
12700 return t;
12701
12702 code = TREE_CODE (t);
12703
12704 if (code == IDENTIFIER_NODE)
12705 type = IDENTIFIER_TYPE_VALUE (t);
12706 else
12707 type = TREE_TYPE (t);
12708
12709 gcc_assert (type != unknown_type_node);
12710
12711 /* Reuse typedefs. We need to do this to handle dependent attributes,
12712 such as attribute aligned. */
12713 if (TYPE_P (t)
12714 && typedef_variant_p (t))
12715 {
12716 tree decl = TYPE_NAME (t);
12717
12718 if (alias_template_specialization_p (t))
12719 {
12720 /* DECL represents an alias template and we want to
12721 instantiate it. */
12722 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12723 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12724 r = instantiate_alias_template (tmpl, gen_args, complain);
12725 }
12726 else if (DECL_CLASS_SCOPE_P (decl)
12727 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12728 && uses_template_parms (DECL_CONTEXT (decl)))
12729 {
12730 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12731 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12732 r = retrieve_specialization (tmpl, gen_args, 0);
12733 }
12734 else if (DECL_FUNCTION_SCOPE_P (decl)
12735 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12736 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12737 r = retrieve_local_specialization (decl);
12738 else
12739 /* The typedef is from a non-template context. */
12740 return t;
12741
12742 if (r)
12743 {
12744 r = TREE_TYPE (r);
12745 r = cp_build_qualified_type_real
12746 (r, cp_type_quals (t) | cp_type_quals (r),
12747 complain | tf_ignore_bad_quals);
12748 return r;
12749 }
12750 else
12751 {
12752 /* We don't have an instantiation yet, so drop the typedef. */
12753 int quals = cp_type_quals (t);
12754 t = DECL_ORIGINAL_TYPE (decl);
12755 t = cp_build_qualified_type_real (t, quals,
12756 complain | tf_ignore_bad_quals);
12757 }
12758 }
12759
12760 if (type
12761 && code != TYPENAME_TYPE
12762 && code != TEMPLATE_TYPE_PARM
12763 && code != IDENTIFIER_NODE
12764 && code != FUNCTION_TYPE
12765 && code != METHOD_TYPE)
12766 type = tsubst (type, args, complain, in_decl);
12767 if (type == error_mark_node)
12768 return error_mark_node;
12769
12770 switch (code)
12771 {
12772 case RECORD_TYPE:
12773 case UNION_TYPE:
12774 case ENUMERAL_TYPE:
12775 return tsubst_aggr_type (t, args, complain, in_decl,
12776 /*entering_scope=*/0);
12777
12778 case ERROR_MARK:
12779 case IDENTIFIER_NODE:
12780 case VOID_TYPE:
12781 case REAL_TYPE:
12782 case COMPLEX_TYPE:
12783 case VECTOR_TYPE:
12784 case BOOLEAN_TYPE:
12785 case NULLPTR_TYPE:
12786 case LANG_TYPE:
12787 return t;
12788
12789 case INTEGER_TYPE:
12790 if (t == integer_type_node)
12791 return t;
12792
12793 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST)
12794 {
12795 if (!TYPE_MAX_VALUE (t))
12796 return compute_array_index_type (NULL_TREE, NULL_TREE, complain);
12797
12798 if (TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
12799 return t;
12800 }
12801
12802 {
12803 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
12804
12805 max = tsubst_expr (omax, args, complain, in_decl,
12806 /*integral_constant_expression_p=*/false);
12807
12808 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12809 needed. */
12810 if (TREE_CODE (max) == NOP_EXPR
12811 && TREE_SIDE_EFFECTS (omax)
12812 && !TREE_TYPE (max))
12813 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
12814
12815 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12816 with TREE_SIDE_EFFECTS that indicates this is not an integral
12817 constant expression. */
12818 if (processing_template_decl
12819 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
12820 {
12821 gcc_assert (TREE_CODE (max) == NOP_EXPR);
12822 TREE_SIDE_EFFECTS (max) = 1;
12823 }
12824
12825 return compute_array_index_type (NULL_TREE, max, complain);
12826 }
12827
12828 case TEMPLATE_TYPE_PARM:
12829 case TEMPLATE_TEMPLATE_PARM:
12830 case BOUND_TEMPLATE_TEMPLATE_PARM:
12831 case TEMPLATE_PARM_INDEX:
12832 {
12833 int idx;
12834 int level;
12835 int levels;
12836 tree arg = NULL_TREE;
12837
12838 /* Early in template argument deduction substitution, we don't
12839 want to reduce the level of 'auto', or it will be confused
12840 with a normal template parm in subsequent deduction. */
12841 if (is_auto (t) && (complain & tf_partial))
12842 return t;
12843
12844 r = NULL_TREE;
12845
12846 gcc_assert (TREE_VEC_LENGTH (args) > 0);
12847 template_parm_level_and_index (t, &level, &idx);
12848
12849 levels = TMPL_ARGS_DEPTH (args);
12850 if (level <= levels
12851 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
12852 {
12853 arg = TMPL_ARG (args, level, idx);
12854
12855 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
12856 {
12857 /* See through ARGUMENT_PACK_SELECT arguments. */
12858 arg = ARGUMENT_PACK_SELECT_ARG (arg);
12859 /* If the selected argument is an expansion E, that most
12860 likely means we were called from
12861 gen_elem_of_pack_expansion_instantiation during the
12862 substituting of pack an argument pack (which Ith
12863 element is a pack expansion, where I is
12864 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12865 In this case, the Ith element resulting from this
12866 substituting is going to be a pack expansion, which
12867 pattern is the pattern of E. Let's return the
12868 pattern of E, and
12869 gen_elem_of_pack_expansion_instantiation will
12870 build the resulting pack expansion from it. */
12871 if (PACK_EXPANSION_P (arg))
12872 {
12873 /* Make sure we aren't throwing away arg info. */
12874 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
12875 arg = PACK_EXPANSION_PATTERN (arg);
12876 }
12877 }
12878 }
12879
12880 if (arg == error_mark_node)
12881 return error_mark_node;
12882 else if (arg != NULL_TREE)
12883 {
12884 if (ARGUMENT_PACK_P (arg))
12885 /* If ARG is an argument pack, we don't actually want to
12886 perform a substitution here, because substitutions
12887 for argument packs are only done
12888 element-by-element. We can get to this point when
12889 substituting the type of a non-type template
12890 parameter pack, when that type actually contains
12891 template parameter packs from an outer template, e.g.,
12892
12893 template<typename... Types> struct A {
12894 template<Types... Values> struct B { };
12895 }; */
12896 return t;
12897
12898 if (code == TEMPLATE_TYPE_PARM)
12899 {
12900 int quals;
12901 gcc_assert (TYPE_P (arg));
12902
12903 quals = cp_type_quals (arg) | cp_type_quals (t);
12904
12905 return cp_build_qualified_type_real
12906 (arg, quals, complain | tf_ignore_bad_quals);
12907 }
12908 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12909 {
12910 /* We are processing a type constructed from a
12911 template template parameter. */
12912 tree argvec = tsubst (TYPE_TI_ARGS (t),
12913 args, complain, in_decl);
12914 if (argvec == error_mark_node)
12915 return error_mark_node;
12916
12917 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12918 || TREE_CODE (arg) == TEMPLATE_DECL
12919 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12920
12921 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12922 /* Consider this code:
12923
12924 template <template <class> class Template>
12925 struct Internal {
12926 template <class Arg> using Bind = Template<Arg>;
12927 };
12928
12929 template <template <class> class Template, class Arg>
12930 using Instantiate = Template<Arg>; //#0
12931
12932 template <template <class> class Template,
12933 class Argument>
12934 using Bind =
12935 Instantiate<Internal<Template>::template Bind,
12936 Argument>; //#1
12937
12938 When #1 is parsed, the
12939 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12940 parameter `Template' in #0 matches the
12941 UNBOUND_CLASS_TEMPLATE representing the argument
12942 `Internal<Template>::template Bind'; We then want
12943 to assemble the type `Bind<Argument>' that can't
12944 be fully created right now, because
12945 `Internal<Template>' not being complete, the Bind
12946 template cannot be looked up in that context. So
12947 we need to "store" `Bind<Argument>' for later
12948 when the context of Bind becomes complete. Let's
12949 store that in a TYPENAME_TYPE. */
12950 return make_typename_type (TYPE_CONTEXT (arg),
12951 build_nt (TEMPLATE_ID_EXPR,
12952 TYPE_IDENTIFIER (arg),
12953 argvec),
12954 typename_type,
12955 complain);
12956
12957 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12958 are resolving nested-types in the signature of a
12959 member function templates. Otherwise ARG is a
12960 TEMPLATE_DECL and is the real template to be
12961 instantiated. */
12962 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12963 arg = TYPE_NAME (arg);
12964
12965 r = lookup_template_class (arg,
12966 argvec, in_decl,
12967 DECL_CONTEXT (arg),
12968 /*entering_scope=*/0,
12969 complain);
12970 return cp_build_qualified_type_real
12971 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12972 }
12973 else
12974 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12975 return convert_from_reference (unshare_expr (arg));
12976 }
12977
12978 if (level == 1)
12979 /* This can happen during the attempted tsubst'ing in
12980 unify. This means that we don't yet have any information
12981 about the template parameter in question. */
12982 return t;
12983
12984 /* If we get here, we must have been looking at a parm for a
12985 more deeply nested template. Make a new version of this
12986 template parameter, but with a lower level. */
12987 switch (code)
12988 {
12989 case TEMPLATE_TYPE_PARM:
12990 case TEMPLATE_TEMPLATE_PARM:
12991 case BOUND_TEMPLATE_TEMPLATE_PARM:
12992 if (cp_type_quals (t))
12993 {
12994 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12995 r = cp_build_qualified_type_real
12996 (r, cp_type_quals (t),
12997 complain | (code == TEMPLATE_TYPE_PARM
12998 ? tf_ignore_bad_quals : 0));
12999 }
13000 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13001 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13002 && (r = (TEMPLATE_PARM_DESCENDANTS
13003 (TEMPLATE_TYPE_PARM_INDEX (t))))
13004 && (r = TREE_TYPE (r))
13005 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13006 /* Break infinite recursion when substituting the constraints
13007 of a constrained placeholder. */;
13008 else
13009 {
13010 r = copy_type (t);
13011 TEMPLATE_TYPE_PARM_INDEX (r)
13012 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13013 r, levels, args, complain);
13014 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13015 TYPE_MAIN_VARIANT (r) = r;
13016 TYPE_POINTER_TO (r) = NULL_TREE;
13017 TYPE_REFERENCE_TO (r) = NULL_TREE;
13018
13019 /* Propagate constraints on placeholders. */
13020 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13021 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13022 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13023 = tsubst_constraint (constr, args, complain, in_decl);
13024
13025 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13026 /* We have reduced the level of the template
13027 template parameter, but not the levels of its
13028 template parameters, so canonical_type_parameter
13029 will not be able to find the canonical template
13030 template parameter for this level. Thus, we
13031 require structural equality checking to compare
13032 TEMPLATE_TEMPLATE_PARMs. */
13033 SET_TYPE_STRUCTURAL_EQUALITY (r);
13034 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13035 SET_TYPE_STRUCTURAL_EQUALITY (r);
13036 else
13037 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13038
13039 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13040 {
13041 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
13042 complain, in_decl);
13043 if (argvec == error_mark_node)
13044 return error_mark_node;
13045
13046 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13047 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
13048 }
13049 }
13050 break;
13051
13052 case TEMPLATE_PARM_INDEX:
13053 r = reduce_template_parm_level (t, type, levels, args, complain);
13054 break;
13055
13056 default:
13057 gcc_unreachable ();
13058 }
13059
13060 return r;
13061 }
13062
13063 case TREE_LIST:
13064 {
13065 tree purpose, value, chain;
13066
13067 if (t == void_list_node)
13068 return t;
13069
13070 purpose = TREE_PURPOSE (t);
13071 if (purpose)
13072 {
13073 purpose = tsubst (purpose, args, complain, in_decl);
13074 if (purpose == error_mark_node)
13075 return error_mark_node;
13076 }
13077 value = TREE_VALUE (t);
13078 if (value)
13079 {
13080 value = tsubst (value, args, complain, in_decl);
13081 if (value == error_mark_node)
13082 return error_mark_node;
13083 }
13084 chain = TREE_CHAIN (t);
13085 if (chain && chain != void_type_node)
13086 {
13087 chain = tsubst (chain, args, complain, in_decl);
13088 if (chain == error_mark_node)
13089 return error_mark_node;
13090 }
13091 if (purpose == TREE_PURPOSE (t)
13092 && value == TREE_VALUE (t)
13093 && chain == TREE_CHAIN (t))
13094 return t;
13095 return hash_tree_cons (purpose, value, chain);
13096 }
13097
13098 case TREE_BINFO:
13099 /* We should never be tsubsting a binfo. */
13100 gcc_unreachable ();
13101
13102 case TREE_VEC:
13103 /* A vector of template arguments. */
13104 gcc_assert (!type);
13105 return tsubst_template_args (t, args, complain, in_decl);
13106
13107 case POINTER_TYPE:
13108 case REFERENCE_TYPE:
13109 {
13110 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13111 return t;
13112
13113 /* [temp.deduct]
13114
13115 Type deduction may fail for any of the following
13116 reasons:
13117
13118 -- Attempting to create a pointer to reference type.
13119 -- Attempting to create a reference to a reference type or
13120 a reference to void.
13121
13122 Core issue 106 says that creating a reference to a reference
13123 during instantiation is no longer a cause for failure. We
13124 only enforce this check in strict C++98 mode. */
13125 if ((TREE_CODE (type) == REFERENCE_TYPE
13126 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13127 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13128 {
13129 static location_t last_loc;
13130
13131 /* We keep track of the last time we issued this error
13132 message to avoid spewing a ton of messages during a
13133 single bad template instantiation. */
13134 if (complain & tf_error
13135 && last_loc != input_location)
13136 {
13137 if (VOID_TYPE_P (type))
13138 error ("forming reference to void");
13139 else if (code == POINTER_TYPE)
13140 error ("forming pointer to reference type %qT", type);
13141 else
13142 error ("forming reference to reference type %qT", type);
13143 last_loc = input_location;
13144 }
13145
13146 return error_mark_node;
13147 }
13148 else if (TREE_CODE (type) == FUNCTION_TYPE
13149 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13150 || type_memfn_rqual (type) != REF_QUAL_NONE))
13151 {
13152 if (complain & tf_error)
13153 {
13154 if (code == POINTER_TYPE)
13155 error ("forming pointer to qualified function type %qT",
13156 type);
13157 else
13158 error ("forming reference to qualified function type %qT",
13159 type);
13160 }
13161 return error_mark_node;
13162 }
13163 else if (code == POINTER_TYPE)
13164 {
13165 r = build_pointer_type (type);
13166 if (TREE_CODE (type) == METHOD_TYPE)
13167 r = build_ptrmemfunc_type (r);
13168 }
13169 else if (TREE_CODE (type) == REFERENCE_TYPE)
13170 /* In C++0x, during template argument substitution, when there is an
13171 attempt to create a reference to a reference type, reference
13172 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13173
13174 "If a template-argument for a template-parameter T names a type
13175 that is a reference to a type A, an attempt to create the type
13176 'lvalue reference to cv T' creates the type 'lvalue reference to
13177 A,' while an attempt to create the type type rvalue reference to
13178 cv T' creates the type T"
13179 */
13180 r = cp_build_reference_type
13181 (TREE_TYPE (type),
13182 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13183 else
13184 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13185 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13186
13187 if (r != error_mark_node)
13188 /* Will this ever be needed for TYPE_..._TO values? */
13189 layout_type (r);
13190
13191 return r;
13192 }
13193 case OFFSET_TYPE:
13194 {
13195 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13196 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13197 {
13198 /* [temp.deduct]
13199
13200 Type deduction may fail for any of the following
13201 reasons:
13202
13203 -- Attempting to create "pointer to member of T" when T
13204 is not a class type. */
13205 if (complain & tf_error)
13206 error ("creating pointer to member of non-class type %qT", r);
13207 return error_mark_node;
13208 }
13209 if (TREE_CODE (type) == REFERENCE_TYPE)
13210 {
13211 if (complain & tf_error)
13212 error ("creating pointer to member reference type %qT", type);
13213 return error_mark_node;
13214 }
13215 if (VOID_TYPE_P (type))
13216 {
13217 if (complain & tf_error)
13218 error ("creating pointer to member of type void");
13219 return error_mark_node;
13220 }
13221 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13222 if (TREE_CODE (type) == FUNCTION_TYPE)
13223 {
13224 /* The type of the implicit object parameter gets its
13225 cv-qualifiers from the FUNCTION_TYPE. */
13226 tree memptr;
13227 tree method_type
13228 = build_memfn_type (type, r, type_memfn_quals (type),
13229 type_memfn_rqual (type));
13230 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13231 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13232 complain);
13233 }
13234 else
13235 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13236 cp_type_quals (t),
13237 complain);
13238 }
13239 case FUNCTION_TYPE:
13240 case METHOD_TYPE:
13241 {
13242 tree fntype;
13243 tree specs;
13244 fntype = tsubst_function_type (t, args, complain, in_decl);
13245 if (fntype == error_mark_node)
13246 return error_mark_node;
13247
13248 /* Substitute the exception specification. */
13249 specs = tsubst_exception_specification (t, args, complain,
13250 in_decl, /*defer_ok*/true);
13251 if (specs == error_mark_node)
13252 return error_mark_node;
13253 if (specs)
13254 fntype = build_exception_variant (fntype, specs);
13255 return fntype;
13256 }
13257 case ARRAY_TYPE:
13258 {
13259 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13260 if (domain == error_mark_node)
13261 return error_mark_node;
13262
13263 /* As an optimization, we avoid regenerating the array type if
13264 it will obviously be the same as T. */
13265 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13266 return t;
13267
13268 /* These checks should match the ones in create_array_type_for_decl.
13269
13270 [temp.deduct]
13271
13272 The deduction may fail for any of the following reasons:
13273
13274 -- Attempting to create an array with an element type that
13275 is void, a function type, or a reference type, or [DR337]
13276 an abstract class type. */
13277 if (VOID_TYPE_P (type)
13278 || TREE_CODE (type) == FUNCTION_TYPE
13279 || (TREE_CODE (type) == ARRAY_TYPE
13280 && TYPE_DOMAIN (type) == NULL_TREE)
13281 || TREE_CODE (type) == REFERENCE_TYPE)
13282 {
13283 if (complain & tf_error)
13284 error ("creating array of %qT", type);
13285 return error_mark_node;
13286 }
13287
13288 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13289 return error_mark_node;
13290
13291 r = build_cplus_array_type (type, domain);
13292
13293 if (TYPE_USER_ALIGN (t))
13294 {
13295 TYPE_ALIGN (r) = TYPE_ALIGN (t);
13296 TYPE_USER_ALIGN (r) = 1;
13297 }
13298
13299 return r;
13300 }
13301
13302 case TYPENAME_TYPE:
13303 {
13304 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13305 in_decl, /*entering_scope=*/1);
13306 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13307 complain, in_decl);
13308
13309 if (ctx == error_mark_node || f == error_mark_node)
13310 return error_mark_node;
13311
13312 if (!MAYBE_CLASS_TYPE_P (ctx))
13313 {
13314 if (complain & tf_error)
13315 error ("%qT is not a class, struct, or union type", ctx);
13316 return error_mark_node;
13317 }
13318 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13319 {
13320 /* Normally, make_typename_type does not require that the CTX
13321 have complete type in order to allow things like:
13322
13323 template <class T> struct S { typename S<T>::X Y; };
13324
13325 But, such constructs have already been resolved by this
13326 point, so here CTX really should have complete type, unless
13327 it's a partial instantiation. */
13328 ctx = complete_type (ctx);
13329 if (!COMPLETE_TYPE_P (ctx))
13330 {
13331 if (complain & tf_error)
13332 cxx_incomplete_type_error (NULL_TREE, ctx);
13333 return error_mark_node;
13334 }
13335 }
13336
13337 f = make_typename_type (ctx, f, typename_type,
13338 complain | tf_keep_type_decl);
13339 if (f == error_mark_node)
13340 return f;
13341 if (TREE_CODE (f) == TYPE_DECL)
13342 {
13343 complain |= tf_ignore_bad_quals;
13344 f = TREE_TYPE (f);
13345 }
13346
13347 if (TREE_CODE (f) != TYPENAME_TYPE)
13348 {
13349 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13350 {
13351 if (complain & tf_error)
13352 error ("%qT resolves to %qT, which is not an enumeration type",
13353 t, f);
13354 else
13355 return error_mark_node;
13356 }
13357 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13358 {
13359 if (complain & tf_error)
13360 error ("%qT resolves to %qT, which is is not a class type",
13361 t, f);
13362 else
13363 return error_mark_node;
13364 }
13365 }
13366
13367 return cp_build_qualified_type_real
13368 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13369 }
13370
13371 case UNBOUND_CLASS_TEMPLATE:
13372 {
13373 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13374 in_decl, /*entering_scope=*/1);
13375 tree name = TYPE_IDENTIFIER (t);
13376 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13377
13378 if (ctx == error_mark_node || name == error_mark_node)
13379 return error_mark_node;
13380
13381 if (parm_list)
13382 parm_list = tsubst_template_parms (parm_list, args, complain);
13383 return make_unbound_class_template (ctx, name, parm_list, complain);
13384 }
13385
13386 case TYPEOF_TYPE:
13387 {
13388 tree type;
13389
13390 ++cp_unevaluated_operand;
13391 ++c_inhibit_evaluation_warnings;
13392
13393 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13394 complain, in_decl,
13395 /*integral_constant_expression_p=*/false);
13396
13397 --cp_unevaluated_operand;
13398 --c_inhibit_evaluation_warnings;
13399
13400 type = finish_typeof (type);
13401 return cp_build_qualified_type_real (type,
13402 cp_type_quals (t)
13403 | cp_type_quals (type),
13404 complain);
13405 }
13406
13407 case DECLTYPE_TYPE:
13408 {
13409 tree type;
13410
13411 ++cp_unevaluated_operand;
13412 ++c_inhibit_evaluation_warnings;
13413
13414 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13415 complain|tf_decltype, in_decl,
13416 /*function_p*/false,
13417 /*integral_constant_expression*/false);
13418
13419 --cp_unevaluated_operand;
13420 --c_inhibit_evaluation_warnings;
13421
13422 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13423 type = lambda_capture_field_type (type,
13424 DECLTYPE_FOR_INIT_CAPTURE (t));
13425 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13426 type = lambda_proxy_type (type);
13427 else
13428 {
13429 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13430 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13431 && EXPR_P (type))
13432 /* In a template ~id could be either a complement expression
13433 or an unqualified-id naming a destructor; if instantiating
13434 it produces an expression, it's not an id-expression or
13435 member access. */
13436 id = false;
13437 type = finish_decltype_type (type, id, complain);
13438 }
13439 return cp_build_qualified_type_real (type,
13440 cp_type_quals (t)
13441 | cp_type_quals (type),
13442 complain | tf_ignore_bad_quals);
13443 }
13444
13445 case UNDERLYING_TYPE:
13446 {
13447 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13448 complain, in_decl);
13449 return finish_underlying_type (type);
13450 }
13451
13452 case TYPE_ARGUMENT_PACK:
13453 case NONTYPE_ARGUMENT_PACK:
13454 {
13455 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13456 tree packed_out =
13457 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13458 args,
13459 complain,
13460 in_decl);
13461 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13462
13463 /* For template nontype argument packs, also substitute into
13464 the type. */
13465 if (code == NONTYPE_ARGUMENT_PACK)
13466 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13467
13468 return r;
13469 }
13470 break;
13471
13472 case VOID_CST:
13473 case INTEGER_CST:
13474 case REAL_CST:
13475 case STRING_CST:
13476 case PLUS_EXPR:
13477 case MINUS_EXPR:
13478 case NEGATE_EXPR:
13479 case NOP_EXPR:
13480 case INDIRECT_REF:
13481 case ADDR_EXPR:
13482 case CALL_EXPR:
13483 case ARRAY_REF:
13484 case SCOPE_REF:
13485 /* We should use one of the expression tsubsts for these codes. */
13486 gcc_unreachable ();
13487
13488 default:
13489 sorry ("use of %qs in template", get_tree_code_name (code));
13490 return error_mark_node;
13491 }
13492 }
13493
13494 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13495 type of the expression on the left-hand side of the "." or "->"
13496 operator. */
13497
13498 static tree
13499 tsubst_baselink (tree baselink, tree object_type,
13500 tree args, tsubst_flags_t complain, tree in_decl)
13501 {
13502 tree name;
13503 tree qualifying_scope;
13504 tree fns;
13505 tree optype;
13506 tree template_args = 0;
13507 bool template_id_p = false;
13508 bool qualified = BASELINK_QUALIFIED_P (baselink);
13509
13510 /* A baselink indicates a function from a base class. Both the
13511 BASELINK_ACCESS_BINFO and the base class referenced may
13512 indicate bases of the template class, rather than the
13513 instantiated class. In addition, lookups that were not
13514 ambiguous before may be ambiguous now. Therefore, we perform
13515 the lookup again. */
13516 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13517 qualifying_scope = tsubst (qualifying_scope, args,
13518 complain, in_decl);
13519 fns = BASELINK_FUNCTIONS (baselink);
13520 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13521 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13522 {
13523 template_id_p = true;
13524 template_args = TREE_OPERAND (fns, 1);
13525 fns = TREE_OPERAND (fns, 0);
13526 if (template_args)
13527 template_args = tsubst_template_args (template_args, args,
13528 complain, in_decl);
13529 }
13530 name = DECL_NAME (get_first_fn (fns));
13531 if (IDENTIFIER_TYPENAME_P (name))
13532 name = mangle_conv_op_name_for_type (optype);
13533 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13534 if (!baselink)
13535 return error_mark_node;
13536
13537 /* If lookup found a single function, mark it as used at this
13538 point. (If it lookup found multiple functions the one selected
13539 later by overload resolution will be marked as used at that
13540 point.) */
13541 if (BASELINK_P (baselink))
13542 fns = BASELINK_FUNCTIONS (baselink);
13543 if (!template_id_p && !really_overloaded_fn (fns)
13544 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13545 return error_mark_node;
13546
13547 /* Add back the template arguments, if present. */
13548 if (BASELINK_P (baselink) && template_id_p)
13549 BASELINK_FUNCTIONS (baselink)
13550 = build_nt (TEMPLATE_ID_EXPR,
13551 BASELINK_FUNCTIONS (baselink),
13552 template_args);
13553 /* Update the conversion operator type. */
13554 BASELINK_OPTYPE (baselink) = optype;
13555
13556 if (!object_type)
13557 object_type = current_class_type;
13558
13559 if (qualified)
13560 baselink = adjust_result_of_qualified_name_lookup (baselink,
13561 qualifying_scope,
13562 object_type);
13563 return baselink;
13564 }
13565
13566 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13567 true if the qualified-id will be a postfix-expression in-and-of
13568 itself; false if more of the postfix-expression follows the
13569 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13570 of "&". */
13571
13572 static tree
13573 tsubst_qualified_id (tree qualified_id, tree args,
13574 tsubst_flags_t complain, tree in_decl,
13575 bool done, bool address_p)
13576 {
13577 tree expr;
13578 tree scope;
13579 tree name;
13580 bool is_template;
13581 tree template_args;
13582 location_t loc = UNKNOWN_LOCATION;
13583
13584 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13585
13586 /* Figure out what name to look up. */
13587 name = TREE_OPERAND (qualified_id, 1);
13588 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13589 {
13590 is_template = true;
13591 loc = EXPR_LOCATION (name);
13592 template_args = TREE_OPERAND (name, 1);
13593 if (template_args)
13594 template_args = tsubst_template_args (template_args, args,
13595 complain, in_decl);
13596 name = TREE_OPERAND (name, 0);
13597 }
13598 else
13599 {
13600 is_template = false;
13601 template_args = NULL_TREE;
13602 }
13603
13604 /* Substitute into the qualifying scope. When there are no ARGS, we
13605 are just trying to simplify a non-dependent expression. In that
13606 case the qualifying scope may be dependent, and, in any case,
13607 substituting will not help. */
13608 scope = TREE_OPERAND (qualified_id, 0);
13609 if (args)
13610 {
13611 scope = tsubst (scope, args, complain, in_decl);
13612 expr = tsubst_copy (name, args, complain, in_decl);
13613 }
13614 else
13615 expr = name;
13616
13617 if (dependent_scope_p (scope))
13618 {
13619 if (is_template)
13620 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13621 return build_qualified_name (NULL_TREE, scope, expr,
13622 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13623 }
13624
13625 if (!BASELINK_P (name) && !DECL_P (expr))
13626 {
13627 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13628 {
13629 /* A BIT_NOT_EXPR is used to represent a destructor. */
13630 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13631 {
13632 error ("qualifying type %qT does not match destructor name ~%qT",
13633 scope, TREE_OPERAND (expr, 0));
13634 expr = error_mark_node;
13635 }
13636 else
13637 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13638 /*is_type_p=*/0, false);
13639 }
13640 else
13641 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13642 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13643 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13644 {
13645 if (complain & tf_error)
13646 {
13647 error ("dependent-name %qE is parsed as a non-type, but "
13648 "instantiation yields a type", qualified_id);
13649 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13650 }
13651 return error_mark_node;
13652 }
13653 }
13654
13655 if (DECL_P (expr))
13656 {
13657 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13658 scope);
13659 /* Remember that there was a reference to this entity. */
13660 if (!mark_used (expr, complain) && !(complain & tf_error))
13661 return error_mark_node;
13662 }
13663
13664 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13665 {
13666 if (complain & tf_error)
13667 qualified_name_lookup_error (scope,
13668 TREE_OPERAND (qualified_id, 1),
13669 expr, input_location);
13670 return error_mark_node;
13671 }
13672
13673 if (is_template)
13674 expr = lookup_template_function (expr, template_args);
13675
13676 if (expr == error_mark_node && complain & tf_error)
13677 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
13678 expr, input_location);
13679 else if (TYPE_P (scope))
13680 {
13681 expr = (adjust_result_of_qualified_name_lookup
13682 (expr, scope, current_nonlambda_class_type ()));
13683 expr = (finish_qualified_id_expr
13684 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
13685 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
13686 /*template_arg_p=*/false, complain));
13687 }
13688
13689 /* Expressions do not generally have reference type. */
13690 if (TREE_CODE (expr) != SCOPE_REF
13691 /* However, if we're about to form a pointer-to-member, we just
13692 want the referenced member referenced. */
13693 && TREE_CODE (expr) != OFFSET_REF)
13694 expr = convert_from_reference (expr);
13695
13696 return expr;
13697 }
13698
13699 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13700 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13701 for tsubst. */
13702
13703 static tree
13704 tsubst_init (tree init, tree decl, tree args,
13705 tsubst_flags_t complain, tree in_decl)
13706 {
13707 if (!init)
13708 return NULL_TREE;
13709
13710 init = tsubst_expr (init, args, complain, in_decl, false);
13711
13712 if (!init)
13713 {
13714 /* If we had an initializer but it
13715 instantiated to nothing,
13716 value-initialize the object. This will
13717 only occur when the initializer was a
13718 pack expansion where the parameter packs
13719 used in that expansion were of length
13720 zero. */
13721 init = build_value_init (TREE_TYPE (decl),
13722 complain);
13723 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13724 init = get_target_expr_sfinae (init, complain);
13725 }
13726
13727 return init;
13728 }
13729
13730 /* Like tsubst, but deals with expressions. This function just replaces
13731 template parms; to finish processing the resultant expression, use
13732 tsubst_copy_and_build or tsubst_expr. */
13733
13734 static tree
13735 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13736 {
13737 enum tree_code code;
13738 tree r;
13739
13740 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
13741 return t;
13742
13743 code = TREE_CODE (t);
13744
13745 switch (code)
13746 {
13747 case PARM_DECL:
13748 r = retrieve_local_specialization (t);
13749
13750 if (r == NULL_TREE)
13751 {
13752 /* We get here for a use of 'this' in an NSDMI. */
13753 if (DECL_NAME (t) == this_identifier
13754 && current_function_decl
13755 && DECL_CONSTRUCTOR_P (current_function_decl))
13756 return current_class_ptr;
13757
13758 /* This can happen for a parameter name used later in a function
13759 declaration (such as in a late-specified return type). Just
13760 make a dummy decl, since it's only used for its type. */
13761 gcc_assert (cp_unevaluated_operand != 0);
13762 r = tsubst_decl (t, args, complain);
13763 /* Give it the template pattern as its context; its true context
13764 hasn't been instantiated yet and this is good enough for
13765 mangling. */
13766 DECL_CONTEXT (r) = DECL_CONTEXT (t);
13767 }
13768
13769 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13770 r = ARGUMENT_PACK_SELECT_ARG (r);
13771 if (!mark_used (r, complain) && !(complain & tf_error))
13772 return error_mark_node;
13773 return r;
13774
13775 case CONST_DECL:
13776 {
13777 tree enum_type;
13778 tree v;
13779
13780 if (DECL_TEMPLATE_PARM_P (t))
13781 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
13782 /* There is no need to substitute into namespace-scope
13783 enumerators. */
13784 if (DECL_NAMESPACE_SCOPE_P (t))
13785 return t;
13786 /* If ARGS is NULL, then T is known to be non-dependent. */
13787 if (args == NULL_TREE)
13788 return scalar_constant_value (t);
13789
13790 /* Unfortunately, we cannot just call lookup_name here.
13791 Consider:
13792
13793 template <int I> int f() {
13794 enum E { a = I };
13795 struct S { void g() { E e = a; } };
13796 };
13797
13798 When we instantiate f<7>::S::g(), say, lookup_name is not
13799 clever enough to find f<7>::a. */
13800 enum_type
13801 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13802 /*entering_scope=*/0);
13803
13804 for (v = TYPE_VALUES (enum_type);
13805 v != NULL_TREE;
13806 v = TREE_CHAIN (v))
13807 if (TREE_PURPOSE (v) == DECL_NAME (t))
13808 return TREE_VALUE (v);
13809
13810 /* We didn't find the name. That should never happen; if
13811 name-lookup found it during preliminary parsing, we
13812 should find it again here during instantiation. */
13813 gcc_unreachable ();
13814 }
13815 return t;
13816
13817 case FIELD_DECL:
13818 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13819 {
13820 /* Check for a local specialization set up by
13821 tsubst_pack_expansion. */
13822 if (tree r = retrieve_local_specialization (t))
13823 {
13824 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13825 r = ARGUMENT_PACK_SELECT_ARG (r);
13826 return r;
13827 }
13828
13829 /* When retrieving a capture pack from a generic lambda, remove the
13830 lambda call op's own template argument list from ARGS. Only the
13831 template arguments active for the closure type should be used to
13832 retrieve the pack specialization. */
13833 if (LAMBDA_FUNCTION_P (current_function_decl)
13834 && (template_class_depth (DECL_CONTEXT (t))
13835 != TMPL_ARGS_DEPTH (args)))
13836 args = strip_innermost_template_args (args, 1);
13837
13838 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13839 tsubst_decl put in the hash table. */
13840 return retrieve_specialization (t, args, 0);
13841 }
13842
13843 if (DECL_CONTEXT (t))
13844 {
13845 tree ctx;
13846
13847 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13848 /*entering_scope=*/1);
13849 if (ctx != DECL_CONTEXT (t))
13850 {
13851 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
13852 if (!r)
13853 {
13854 if (complain & tf_error)
13855 error ("using invalid field %qD", t);
13856 return error_mark_node;
13857 }
13858 return r;
13859 }
13860 }
13861
13862 return t;
13863
13864 case VAR_DECL:
13865 case FUNCTION_DECL:
13866 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
13867 r = tsubst (t, args, complain, in_decl);
13868 else if (local_variable_p (t))
13869 {
13870 r = retrieve_local_specialization (t);
13871 if (r == NULL_TREE)
13872 {
13873 /* First try name lookup to find the instantiation. */
13874 r = lookup_name (DECL_NAME (t));
13875 if (r)
13876 {
13877 /* Make sure that the one we found is the one we want. */
13878 tree ctx = DECL_CONTEXT (t);
13879 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
13880 ctx = tsubst (ctx, args, complain, in_decl);
13881 if (ctx != DECL_CONTEXT (r))
13882 r = NULL_TREE;
13883 }
13884
13885 if (r)
13886 /* OK */;
13887 else
13888 {
13889 /* This can happen for a variable used in a
13890 late-specified return type of a local lambda, or for a
13891 local static or constant. Building a new VAR_DECL
13892 should be OK in all those cases. */
13893 r = tsubst_decl (t, args, complain);
13894 if (decl_maybe_constant_var_p (r))
13895 {
13896 /* We can't call cp_finish_decl, so handle the
13897 initializer by hand. */
13898 tree init = tsubst_init (DECL_INITIAL (t), r, args,
13899 complain, in_decl);
13900 if (!processing_template_decl)
13901 init = maybe_constant_init (init);
13902 if (processing_template_decl
13903 ? potential_constant_expression (init)
13904 : reduced_constant_expression_p (init))
13905 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13906 = TREE_CONSTANT (r) = true;
13907 DECL_INITIAL (r) = init;
13908 }
13909 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13910 || decl_constant_var_p (r)
13911 || errorcount || sorrycount);
13912 if (!processing_template_decl)
13913 {
13914 if (TREE_STATIC (r))
13915 rest_of_decl_compilation (r, toplevel_bindings_p (),
13916 at_eof);
13917 else
13918 r = process_outer_var_ref (r, complain);
13919 }
13920 }
13921 /* Remember this for subsequent uses. */
13922 if (local_specializations)
13923 register_local_specialization (r, t);
13924 }
13925 }
13926 else
13927 r = t;
13928 if (!mark_used (r, complain) && !(complain & tf_error))
13929 return error_mark_node;
13930 return r;
13931
13932 case NAMESPACE_DECL:
13933 return t;
13934
13935 case OVERLOAD:
13936 /* An OVERLOAD will always be a non-dependent overload set; an
13937 overload set from function scope will just be represented with an
13938 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13939 gcc_assert (!uses_template_parms (t));
13940 return t;
13941
13942 case BASELINK:
13943 return tsubst_baselink (t, current_nonlambda_class_type (),
13944 args, complain, in_decl);
13945
13946 case TEMPLATE_DECL:
13947 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13948 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13949 args, complain, in_decl);
13950 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13951 return tsubst (t, args, complain, in_decl);
13952 else if (DECL_CLASS_SCOPE_P (t)
13953 && uses_template_parms (DECL_CONTEXT (t)))
13954 {
13955 /* Template template argument like the following example need
13956 special treatment:
13957
13958 template <template <class> class TT> struct C {};
13959 template <class T> struct D {
13960 template <class U> struct E {};
13961 C<E> c; // #1
13962 };
13963 D<int> d; // #2
13964
13965 We are processing the template argument `E' in #1 for
13966 the template instantiation #2. Originally, `E' is a
13967 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13968 have to substitute this with one having context `D<int>'. */
13969
13970 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13971 return lookup_field (context, DECL_NAME(t), 0, false);
13972 }
13973 else
13974 /* Ordinary template template argument. */
13975 return t;
13976
13977 case CAST_EXPR:
13978 case REINTERPRET_CAST_EXPR:
13979 case CONST_CAST_EXPR:
13980 case STATIC_CAST_EXPR:
13981 case DYNAMIC_CAST_EXPR:
13982 case IMPLICIT_CONV_EXPR:
13983 case CONVERT_EXPR:
13984 case NOP_EXPR:
13985 {
13986 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13987 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13988 return build1 (code, type, op0);
13989 }
13990
13991 case SIZEOF_EXPR:
13992 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13993 {
13994 tree expanded, op = TREE_OPERAND (t, 0);
13995 int len = 0;
13996
13997 if (SIZEOF_EXPR_TYPE_P (t))
13998 op = TREE_TYPE (op);
13999
14000 ++cp_unevaluated_operand;
14001 ++c_inhibit_evaluation_warnings;
14002 /* We only want to compute the number of arguments. */
14003 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14004 --cp_unevaluated_operand;
14005 --c_inhibit_evaluation_warnings;
14006
14007 if (TREE_CODE (expanded) == TREE_VEC)
14008 len = TREE_VEC_LENGTH (expanded);
14009
14010 if (expanded == error_mark_node)
14011 return error_mark_node;
14012 else if (PACK_EXPANSION_P (expanded)
14013 || (TREE_CODE (expanded) == TREE_VEC
14014 && len > 0
14015 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
14016 {
14017 if (TREE_CODE (expanded) == TREE_VEC)
14018 expanded = TREE_VEC_ELT (expanded, len - 1);
14019 else
14020 PACK_EXPANSION_SIZEOF_P (expanded) = true;
14021
14022 if (TYPE_P (expanded))
14023 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14024 complain & tf_error);
14025 else
14026 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14027 complain & tf_error);
14028 }
14029 else
14030 return build_int_cst (size_type_node, len);
14031 }
14032 if (SIZEOF_EXPR_TYPE_P (t))
14033 {
14034 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14035 args, complain, in_decl);
14036 r = build1 (NOP_EXPR, r, error_mark_node);
14037 r = build1 (SIZEOF_EXPR,
14038 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14039 SIZEOF_EXPR_TYPE_P (r) = 1;
14040 return r;
14041 }
14042 /* Fall through */
14043
14044 case INDIRECT_REF:
14045 case NEGATE_EXPR:
14046 case TRUTH_NOT_EXPR:
14047 case BIT_NOT_EXPR:
14048 case ADDR_EXPR:
14049 case UNARY_PLUS_EXPR: /* Unary + */
14050 case ALIGNOF_EXPR:
14051 case AT_ENCODE_EXPR:
14052 case ARROW_EXPR:
14053 case THROW_EXPR:
14054 case TYPEID_EXPR:
14055 case REALPART_EXPR:
14056 case IMAGPART_EXPR:
14057 case PAREN_EXPR:
14058 {
14059 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14060 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14061 return build1 (code, type, op0);
14062 }
14063
14064 case COMPONENT_REF:
14065 {
14066 tree object;
14067 tree name;
14068
14069 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14070 name = TREE_OPERAND (t, 1);
14071 if (TREE_CODE (name) == BIT_NOT_EXPR)
14072 {
14073 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14074 complain, in_decl);
14075 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14076 }
14077 else if (TREE_CODE (name) == SCOPE_REF
14078 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14079 {
14080 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14081 complain, in_decl);
14082 name = TREE_OPERAND (name, 1);
14083 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14084 complain, in_decl);
14085 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14086 name = build_qualified_name (/*type=*/NULL_TREE,
14087 base, name,
14088 /*template_p=*/false);
14089 }
14090 else if (BASELINK_P (name))
14091 name = tsubst_baselink (name,
14092 non_reference (TREE_TYPE (object)),
14093 args, complain,
14094 in_decl);
14095 else
14096 name = tsubst_copy (name, args, complain, in_decl);
14097 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14098 }
14099
14100 case PLUS_EXPR:
14101 case MINUS_EXPR:
14102 case MULT_EXPR:
14103 case TRUNC_DIV_EXPR:
14104 case CEIL_DIV_EXPR:
14105 case FLOOR_DIV_EXPR:
14106 case ROUND_DIV_EXPR:
14107 case EXACT_DIV_EXPR:
14108 case BIT_AND_EXPR:
14109 case BIT_IOR_EXPR:
14110 case BIT_XOR_EXPR:
14111 case TRUNC_MOD_EXPR:
14112 case FLOOR_MOD_EXPR:
14113 case TRUTH_ANDIF_EXPR:
14114 case TRUTH_ORIF_EXPR:
14115 case TRUTH_AND_EXPR:
14116 case TRUTH_OR_EXPR:
14117 case RSHIFT_EXPR:
14118 case LSHIFT_EXPR:
14119 case RROTATE_EXPR:
14120 case LROTATE_EXPR:
14121 case EQ_EXPR:
14122 case NE_EXPR:
14123 case MAX_EXPR:
14124 case MIN_EXPR:
14125 case LE_EXPR:
14126 case GE_EXPR:
14127 case LT_EXPR:
14128 case GT_EXPR:
14129 case COMPOUND_EXPR:
14130 case DOTSTAR_EXPR:
14131 case MEMBER_REF:
14132 case PREDECREMENT_EXPR:
14133 case PREINCREMENT_EXPR:
14134 case POSTDECREMENT_EXPR:
14135 case POSTINCREMENT_EXPR:
14136 {
14137 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14138 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14139 return build_nt (code, op0, op1);
14140 }
14141
14142 case SCOPE_REF:
14143 {
14144 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14145 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14146 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14147 QUALIFIED_NAME_IS_TEMPLATE (t));
14148 }
14149
14150 case ARRAY_REF:
14151 {
14152 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14153 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14154 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14155 }
14156
14157 case CALL_EXPR:
14158 {
14159 int n = VL_EXP_OPERAND_LENGTH (t);
14160 tree result = build_vl_exp (CALL_EXPR, n);
14161 int i;
14162 for (i = 0; i < n; i++)
14163 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14164 complain, in_decl);
14165 return result;
14166 }
14167
14168 case COND_EXPR:
14169 case MODOP_EXPR:
14170 case PSEUDO_DTOR_EXPR:
14171 case VEC_PERM_EXPR:
14172 {
14173 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14174 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14175 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14176 r = build_nt (code, op0, op1, op2);
14177 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14178 return r;
14179 }
14180
14181 case NEW_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 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14188 return r;
14189 }
14190
14191 case DELETE_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 r = build_nt (code, op0, op1);
14196 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14197 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14198 return r;
14199 }
14200
14201 case TEMPLATE_ID_EXPR:
14202 {
14203 /* Substituted template arguments */
14204 tree fn = TREE_OPERAND (t, 0);
14205 tree targs = TREE_OPERAND (t, 1);
14206
14207 fn = tsubst_copy (fn, args, complain, in_decl);
14208 if (targs)
14209 targs = tsubst_template_args (targs, args, complain, in_decl);
14210
14211 return lookup_template_function (fn, targs);
14212 }
14213
14214 case TREE_LIST:
14215 {
14216 tree purpose, value, chain;
14217
14218 if (t == void_list_node)
14219 return t;
14220
14221 purpose = TREE_PURPOSE (t);
14222 if (purpose)
14223 purpose = tsubst_copy (purpose, args, complain, in_decl);
14224 value = TREE_VALUE (t);
14225 if (value)
14226 value = tsubst_copy (value, args, complain, in_decl);
14227 chain = TREE_CHAIN (t);
14228 if (chain && chain != void_type_node)
14229 chain = tsubst_copy (chain, args, complain, in_decl);
14230 if (purpose == TREE_PURPOSE (t)
14231 && value == TREE_VALUE (t)
14232 && chain == TREE_CHAIN (t))
14233 return t;
14234 return tree_cons (purpose, value, chain);
14235 }
14236
14237 case RECORD_TYPE:
14238 case UNION_TYPE:
14239 case ENUMERAL_TYPE:
14240 case INTEGER_TYPE:
14241 case TEMPLATE_TYPE_PARM:
14242 case TEMPLATE_TEMPLATE_PARM:
14243 case BOUND_TEMPLATE_TEMPLATE_PARM:
14244 case TEMPLATE_PARM_INDEX:
14245 case POINTER_TYPE:
14246 case REFERENCE_TYPE:
14247 case OFFSET_TYPE:
14248 case FUNCTION_TYPE:
14249 case METHOD_TYPE:
14250 case ARRAY_TYPE:
14251 case TYPENAME_TYPE:
14252 case UNBOUND_CLASS_TEMPLATE:
14253 case TYPEOF_TYPE:
14254 case DECLTYPE_TYPE:
14255 case TYPE_DECL:
14256 return tsubst (t, args, complain, in_decl);
14257
14258 case USING_DECL:
14259 t = DECL_NAME (t);
14260 /* Fall through. */
14261 case IDENTIFIER_NODE:
14262 if (IDENTIFIER_TYPENAME_P (t))
14263 {
14264 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14265 return mangle_conv_op_name_for_type (new_type);
14266 }
14267 else
14268 return t;
14269
14270 case CONSTRUCTOR:
14271 /* This is handled by tsubst_copy_and_build. */
14272 gcc_unreachable ();
14273
14274 case VA_ARG_EXPR:
14275 {
14276 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14277 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14278 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14279 }
14280
14281 case CLEANUP_POINT_EXPR:
14282 /* We shouldn't have built any of these during initial template
14283 generation. Instead, they should be built during instantiation
14284 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14285 gcc_unreachable ();
14286
14287 case OFFSET_REF:
14288 {
14289 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14290 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14291 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14292 r = build2 (code, type, op0, op1);
14293 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14294 if (!mark_used (TREE_OPERAND (r, 1), complain)
14295 && !(complain & tf_error))
14296 return error_mark_node;
14297 return r;
14298 }
14299
14300 case EXPR_PACK_EXPANSION:
14301 error ("invalid use of pack expansion expression");
14302 return error_mark_node;
14303
14304 case NONTYPE_ARGUMENT_PACK:
14305 error ("use %<...%> to expand argument pack");
14306 return error_mark_node;
14307
14308 case VOID_CST:
14309 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14310 return t;
14311
14312 case INTEGER_CST:
14313 case REAL_CST:
14314 case STRING_CST:
14315 case COMPLEX_CST:
14316 {
14317 /* Instantiate any typedefs in the type. */
14318 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14319 r = fold_convert (type, t);
14320 gcc_assert (TREE_CODE (r) == code);
14321 return r;
14322 }
14323
14324 case PTRMEM_CST:
14325 /* These can sometimes show up in a partial instantiation, but never
14326 involve template parms. */
14327 gcc_assert (!uses_template_parms (t));
14328 return t;
14329
14330 case UNARY_LEFT_FOLD_EXPR:
14331 return tsubst_unary_left_fold (t, args, complain, in_decl);
14332 case UNARY_RIGHT_FOLD_EXPR:
14333 return tsubst_unary_right_fold (t, args, complain, in_decl);
14334 case BINARY_LEFT_FOLD_EXPR:
14335 return tsubst_binary_left_fold (t, args, complain, in_decl);
14336 case BINARY_RIGHT_FOLD_EXPR:
14337 return tsubst_binary_right_fold (t, args, complain, in_decl);
14338
14339 default:
14340 /* We shouldn't get here, but keep going if !flag_checking. */
14341 if (flag_checking)
14342 gcc_unreachable ();
14343 return t;
14344 }
14345 }
14346
14347 /* Helper function for tsubst_omp_clauses, used for instantiation of
14348 OMP_CLAUSE_DECL of clauses. */
14349
14350 static tree
14351 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14352 tree in_decl)
14353 {
14354 if (decl == NULL_TREE)
14355 return NULL_TREE;
14356
14357 /* Handle an OpenMP array section represented as a TREE_LIST (or
14358 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14359 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14360 TREE_LIST. We can handle it exactly the same as an array section
14361 (purpose, value, and a chain), even though the nomenclature
14362 (low_bound, length, etc) is different. */
14363 if (TREE_CODE (decl) == TREE_LIST)
14364 {
14365 tree low_bound
14366 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14367 /*integral_constant_expression_p=*/false);
14368 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14369 /*integral_constant_expression_p=*/false);
14370 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14371 in_decl);
14372 if (TREE_PURPOSE (decl) == low_bound
14373 && TREE_VALUE (decl) == length
14374 && TREE_CHAIN (decl) == chain)
14375 return decl;
14376 tree ret = tree_cons (low_bound, length, chain);
14377 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
14378 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
14379 return ret;
14380 }
14381 tree ret = tsubst_expr (decl, args, complain, in_decl,
14382 /*integral_constant_expression_p=*/false);
14383 /* Undo convert_from_reference tsubst_expr could have called. */
14384 if (decl
14385 && REFERENCE_REF_P (ret)
14386 && !REFERENCE_REF_P (decl))
14387 ret = TREE_OPERAND (ret, 0);
14388 return ret;
14389 }
14390
14391 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14392
14393 static tree
14394 tsubst_omp_clauses (tree clauses, bool declare_simd, bool allow_fields,
14395 tree args, tsubst_flags_t complain, tree in_decl)
14396 {
14397 tree new_clauses = NULL_TREE, nc, oc;
14398 tree linear_no_step = NULL_TREE;
14399
14400 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14401 {
14402 nc = copy_node (oc);
14403 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14404 new_clauses = nc;
14405
14406 switch (OMP_CLAUSE_CODE (nc))
14407 {
14408 case OMP_CLAUSE_LASTPRIVATE:
14409 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14410 {
14411 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14412 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14413 in_decl, /*integral_constant_expression_p=*/false);
14414 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14415 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14416 }
14417 /* FALLTHRU */
14418 case OMP_CLAUSE_PRIVATE:
14419 case OMP_CLAUSE_SHARED:
14420 case OMP_CLAUSE_FIRSTPRIVATE:
14421 case OMP_CLAUSE_COPYIN:
14422 case OMP_CLAUSE_COPYPRIVATE:
14423 case OMP_CLAUSE_UNIFORM:
14424 case OMP_CLAUSE_DEPEND:
14425 case OMP_CLAUSE_FROM:
14426 case OMP_CLAUSE_TO:
14427 case OMP_CLAUSE_MAP:
14428 case OMP_CLAUSE_USE_DEVICE_PTR:
14429 case OMP_CLAUSE_IS_DEVICE_PTR:
14430 OMP_CLAUSE_DECL (nc)
14431 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14432 in_decl);
14433 break;
14434 case OMP_CLAUSE_IF:
14435 case OMP_CLAUSE_NUM_THREADS:
14436 case OMP_CLAUSE_SCHEDULE:
14437 case OMP_CLAUSE_COLLAPSE:
14438 case OMP_CLAUSE_FINAL:
14439 case OMP_CLAUSE_DEVICE:
14440 case OMP_CLAUSE_DIST_SCHEDULE:
14441 case OMP_CLAUSE_NUM_TEAMS:
14442 case OMP_CLAUSE_THREAD_LIMIT:
14443 case OMP_CLAUSE_SAFELEN:
14444 case OMP_CLAUSE_SIMDLEN:
14445 case OMP_CLAUSE_NUM_TASKS:
14446 case OMP_CLAUSE_GRAINSIZE:
14447 case OMP_CLAUSE_PRIORITY:
14448 case OMP_CLAUSE_ORDERED:
14449 case OMP_CLAUSE_HINT:
14450 case OMP_CLAUSE_NUM_GANGS:
14451 case OMP_CLAUSE_NUM_WORKERS:
14452 case OMP_CLAUSE_VECTOR_LENGTH:
14453 case OMP_CLAUSE_WORKER:
14454 case OMP_CLAUSE_VECTOR:
14455 case OMP_CLAUSE_ASYNC:
14456 case OMP_CLAUSE_WAIT:
14457 OMP_CLAUSE_OPERAND (nc, 0)
14458 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14459 in_decl, /*integral_constant_expression_p=*/false);
14460 break;
14461 case OMP_CLAUSE_REDUCTION:
14462 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14463 {
14464 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14465 if (TREE_CODE (placeholder) == SCOPE_REF)
14466 {
14467 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14468 complain, in_decl);
14469 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14470 = build_qualified_name (NULL_TREE, scope,
14471 TREE_OPERAND (placeholder, 1),
14472 false);
14473 }
14474 else
14475 gcc_assert (identifier_p (placeholder));
14476 }
14477 OMP_CLAUSE_DECL (nc)
14478 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14479 in_decl);
14480 break;
14481 case OMP_CLAUSE_GANG:
14482 case OMP_CLAUSE_ALIGNED:
14483 OMP_CLAUSE_DECL (nc)
14484 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14485 in_decl);
14486 OMP_CLAUSE_OPERAND (nc, 1)
14487 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14488 in_decl, /*integral_constant_expression_p=*/false);
14489 break;
14490 case OMP_CLAUSE_LINEAR:
14491 OMP_CLAUSE_DECL (nc)
14492 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14493 in_decl);
14494 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
14495 {
14496 gcc_assert (!linear_no_step);
14497 linear_no_step = nc;
14498 }
14499 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
14500 OMP_CLAUSE_LINEAR_STEP (nc)
14501 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
14502 complain, in_decl);
14503 else
14504 OMP_CLAUSE_LINEAR_STEP (nc)
14505 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
14506 in_decl,
14507 /*integral_constant_expression_p=*/false);
14508 break;
14509 case OMP_CLAUSE_NOWAIT:
14510 case OMP_CLAUSE_DEFAULT:
14511 case OMP_CLAUSE_UNTIED:
14512 case OMP_CLAUSE_MERGEABLE:
14513 case OMP_CLAUSE_INBRANCH:
14514 case OMP_CLAUSE_NOTINBRANCH:
14515 case OMP_CLAUSE_PROC_BIND:
14516 case OMP_CLAUSE_FOR:
14517 case OMP_CLAUSE_PARALLEL:
14518 case OMP_CLAUSE_SECTIONS:
14519 case OMP_CLAUSE_TASKGROUP:
14520 case OMP_CLAUSE_NOGROUP:
14521 case OMP_CLAUSE_THREADS:
14522 case OMP_CLAUSE_SIMD:
14523 case OMP_CLAUSE_DEFAULTMAP:
14524 case OMP_CLAUSE_INDEPENDENT:
14525 case OMP_CLAUSE_AUTO:
14526 case OMP_CLAUSE_SEQ:
14527 break;
14528 case OMP_CLAUSE_TILE:
14529 {
14530 tree lnc, loc;
14531 for (lnc = OMP_CLAUSE_TILE_LIST (nc),
14532 loc = OMP_CLAUSE_TILE_LIST (oc);
14533 loc;
14534 loc = TREE_CHAIN (loc), lnc = TREE_CHAIN (lnc))
14535 {
14536 TREE_VALUE (lnc) = tsubst_expr (TREE_VALUE (loc), args,
14537 complain, in_decl, false);
14538 }
14539 }
14540 break;
14541 default:
14542 gcc_unreachable ();
14543 }
14544 if (allow_fields)
14545 switch (OMP_CLAUSE_CODE (nc))
14546 {
14547 case OMP_CLAUSE_SHARED:
14548 case OMP_CLAUSE_PRIVATE:
14549 case OMP_CLAUSE_FIRSTPRIVATE:
14550 case OMP_CLAUSE_LASTPRIVATE:
14551 case OMP_CLAUSE_COPYPRIVATE:
14552 case OMP_CLAUSE_LINEAR:
14553 case OMP_CLAUSE_REDUCTION:
14554 case OMP_CLAUSE_USE_DEVICE_PTR:
14555 case OMP_CLAUSE_IS_DEVICE_PTR:
14556 /* tsubst_expr on SCOPE_REF results in returning
14557 finish_non_static_data_member result. Undo that here. */
14558 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
14559 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
14560 == IDENTIFIER_NODE))
14561 {
14562 tree t = OMP_CLAUSE_DECL (nc);
14563 tree v = t;
14564 while (v)
14565 switch (TREE_CODE (v))
14566 {
14567 case COMPONENT_REF:
14568 case MEM_REF:
14569 case INDIRECT_REF:
14570 CASE_CONVERT:
14571 case POINTER_PLUS_EXPR:
14572 v = TREE_OPERAND (v, 0);
14573 continue;
14574 case PARM_DECL:
14575 if (DECL_CONTEXT (v) == current_function_decl
14576 && DECL_ARTIFICIAL (v)
14577 && DECL_NAME (v) == this_identifier)
14578 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
14579 /* FALLTHRU */
14580 default:
14581 v = NULL_TREE;
14582 break;
14583 }
14584 }
14585 else if (VAR_P (OMP_CLAUSE_DECL (oc))
14586 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
14587 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
14588 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
14589 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
14590 {
14591 tree decl = OMP_CLAUSE_DECL (nc);
14592 if (VAR_P (decl))
14593 {
14594 if (!DECL_LANG_SPECIFIC (decl))
14595 retrofit_lang_decl (decl);
14596 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
14597 }
14598 }
14599 break;
14600 default:
14601 break;
14602 }
14603 }
14604
14605 new_clauses = nreverse (new_clauses);
14606 if (!declare_simd)
14607 {
14608 new_clauses = finish_omp_clauses (new_clauses, allow_fields);
14609 if (linear_no_step)
14610 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
14611 if (nc == linear_no_step)
14612 {
14613 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
14614 break;
14615 }
14616 }
14617 return new_clauses;
14618 }
14619
14620 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14621
14622 static tree
14623 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14624 tree in_decl)
14625 {
14626 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14627
14628 tree purpose, value, chain;
14629
14630 if (t == NULL)
14631 return t;
14632
14633 if (TREE_CODE (t) != TREE_LIST)
14634 return tsubst_copy_and_build (t, args, complain, in_decl,
14635 /*function_p=*/false,
14636 /*integral_constant_expression_p=*/false);
14637
14638 if (t == void_list_node)
14639 return t;
14640
14641 purpose = TREE_PURPOSE (t);
14642 if (purpose)
14643 purpose = RECUR (purpose);
14644 value = TREE_VALUE (t);
14645 if (value)
14646 {
14647 if (TREE_CODE (value) != LABEL_DECL)
14648 value = RECUR (value);
14649 else
14650 {
14651 value = lookup_label (DECL_NAME (value));
14652 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14653 TREE_USED (value) = 1;
14654 }
14655 }
14656 chain = TREE_CHAIN (t);
14657 if (chain && chain != void_type_node)
14658 chain = RECUR (chain);
14659 return tree_cons (purpose, value, chain);
14660 #undef RECUR
14661 }
14662
14663 /* Used to temporarily communicate the list of #pragma omp parallel
14664 clauses to #pragma omp for instantiation if they are combined
14665 together. */
14666
14667 static tree *omp_parallel_combined_clauses;
14668
14669 /* Substitute one OMP_FOR iterator. */
14670
14671 static void
14672 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
14673 tree initv, tree condv, tree incrv, tree *clauses,
14674 tree args, tsubst_flags_t complain, tree in_decl,
14675 bool integral_constant_expression_p)
14676 {
14677 #define RECUR(NODE) \
14678 tsubst_expr ((NODE), args, complain, in_decl, \
14679 integral_constant_expression_p)
14680 tree decl, init, cond, incr;
14681
14682 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
14683 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
14684
14685 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
14686 {
14687 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
14688 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
14689 }
14690
14691 decl = TREE_OPERAND (init, 0);
14692 init = TREE_OPERAND (init, 1);
14693 tree decl_expr = NULL_TREE;
14694 if (init && TREE_CODE (init) == DECL_EXPR)
14695 {
14696 /* We need to jump through some hoops to handle declarations in the
14697 for-init-statement, since we might need to handle auto deduction,
14698 but we need to keep control of initialization. */
14699 decl_expr = init;
14700 init = DECL_INITIAL (DECL_EXPR_DECL (init));
14701 decl = tsubst_decl (decl, args, complain);
14702 }
14703 else
14704 {
14705 if (TREE_CODE (decl) == SCOPE_REF)
14706 {
14707 decl = RECUR (decl);
14708 if (TREE_CODE (decl) == COMPONENT_REF)
14709 {
14710 tree v = decl;
14711 while (v)
14712 switch (TREE_CODE (v))
14713 {
14714 case COMPONENT_REF:
14715 case MEM_REF:
14716 case INDIRECT_REF:
14717 CASE_CONVERT:
14718 case POINTER_PLUS_EXPR:
14719 v = TREE_OPERAND (v, 0);
14720 continue;
14721 case PARM_DECL:
14722 if (DECL_CONTEXT (v) == current_function_decl
14723 && DECL_ARTIFICIAL (v)
14724 && DECL_NAME (v) == this_identifier)
14725 {
14726 decl = TREE_OPERAND (decl, 1);
14727 decl = omp_privatize_field (decl, false);
14728 }
14729 /* FALLTHRU */
14730 default:
14731 v = NULL_TREE;
14732 break;
14733 }
14734 }
14735 }
14736 else
14737 decl = RECUR (decl);
14738 }
14739 init = RECUR (init);
14740
14741 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14742 if (auto_node && init)
14743 TREE_TYPE (decl)
14744 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
14745
14746 gcc_assert (!type_dependent_expression_p (decl));
14747
14748 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14749 {
14750 if (decl_expr)
14751 {
14752 /* Declare the variable, but don't let that initialize it. */
14753 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
14754 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
14755 RECUR (decl_expr);
14756 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
14757 }
14758
14759 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
14760 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14761 if (TREE_CODE (incr) == MODIFY_EXPR)
14762 {
14763 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14764 tree rhs = RECUR (TREE_OPERAND (incr, 1));
14765 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
14766 NOP_EXPR, rhs, complain);
14767 }
14768 else
14769 incr = RECUR (incr);
14770 TREE_VEC_ELT (declv, i) = decl;
14771 TREE_VEC_ELT (initv, i) = init;
14772 TREE_VEC_ELT (condv, i) = cond;
14773 TREE_VEC_ELT (incrv, i) = incr;
14774 return;
14775 }
14776
14777 if (decl_expr)
14778 {
14779 /* Declare and initialize the variable. */
14780 RECUR (decl_expr);
14781 init = NULL_TREE;
14782 }
14783 else if (init)
14784 {
14785 tree *pc;
14786 int j;
14787 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
14788 {
14789 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
14790 {
14791 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
14792 && OMP_CLAUSE_DECL (*pc) == decl)
14793 break;
14794 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
14795 && OMP_CLAUSE_DECL (*pc) == decl)
14796 {
14797 if (j)
14798 break;
14799 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14800 tree c = *pc;
14801 *pc = OMP_CLAUSE_CHAIN (c);
14802 OMP_CLAUSE_CHAIN (c) = *clauses;
14803 *clauses = c;
14804 }
14805 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
14806 && OMP_CLAUSE_DECL (*pc) == decl)
14807 {
14808 error ("iteration variable %qD should not be firstprivate",
14809 decl);
14810 *pc = OMP_CLAUSE_CHAIN (*pc);
14811 }
14812 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
14813 && OMP_CLAUSE_DECL (*pc) == decl)
14814 {
14815 error ("iteration variable %qD should not be reduction",
14816 decl);
14817 *pc = OMP_CLAUSE_CHAIN (*pc);
14818 }
14819 else
14820 pc = &OMP_CLAUSE_CHAIN (*pc);
14821 }
14822 if (*pc)
14823 break;
14824 }
14825 if (*pc == NULL_TREE)
14826 {
14827 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14828 OMP_CLAUSE_DECL (c) = decl;
14829 c = finish_omp_clauses (c, true);
14830 if (c)
14831 {
14832 OMP_CLAUSE_CHAIN (c) = *clauses;
14833 *clauses = c;
14834 }
14835 }
14836 }
14837 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
14838 if (COMPARISON_CLASS_P (cond))
14839 {
14840 tree op0 = RECUR (TREE_OPERAND (cond, 0));
14841 tree op1 = RECUR (TREE_OPERAND (cond, 1));
14842 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
14843 }
14844 else
14845 cond = RECUR (cond);
14846 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14847 switch (TREE_CODE (incr))
14848 {
14849 case PREINCREMENT_EXPR:
14850 case PREDECREMENT_EXPR:
14851 case POSTINCREMENT_EXPR:
14852 case POSTDECREMENT_EXPR:
14853 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
14854 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
14855 break;
14856 case MODIFY_EXPR:
14857 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14858 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14859 {
14860 tree rhs = TREE_OPERAND (incr, 1);
14861 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14862 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14863 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14864 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14865 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14866 rhs0, rhs1));
14867 }
14868 else
14869 incr = RECUR (incr);
14870 break;
14871 case MODOP_EXPR:
14872 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14873 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14874 {
14875 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14876 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14877 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
14878 TREE_TYPE (decl), lhs,
14879 RECUR (TREE_OPERAND (incr, 2))));
14880 }
14881 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
14882 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
14883 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
14884 {
14885 tree rhs = TREE_OPERAND (incr, 2);
14886 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14887 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14888 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14889 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14890 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14891 rhs0, rhs1));
14892 }
14893 else
14894 incr = RECUR (incr);
14895 break;
14896 default:
14897 incr = RECUR (incr);
14898 break;
14899 }
14900
14901 TREE_VEC_ELT (declv, i) = decl;
14902 TREE_VEC_ELT (initv, i) = init;
14903 TREE_VEC_ELT (condv, i) = cond;
14904 TREE_VEC_ELT (incrv, i) = incr;
14905 #undef RECUR
14906 }
14907
14908 /* Helper function of tsubst_expr, find OMP_TEAMS inside
14909 of OMP_TARGET's body. */
14910
14911 static tree
14912 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
14913 {
14914 *walk_subtrees = 0;
14915 switch (TREE_CODE (*tp))
14916 {
14917 case OMP_TEAMS:
14918 return *tp;
14919 case BIND_EXPR:
14920 case STATEMENT_LIST:
14921 *walk_subtrees = 1;
14922 break;
14923 default:
14924 break;
14925 }
14926 return NULL_TREE;
14927 }
14928
14929 /* Like tsubst_copy for expressions, etc. but also does semantic
14930 processing. */
14931
14932 tree
14933 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
14934 bool integral_constant_expression_p)
14935 {
14936 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14937 #define RECUR(NODE) \
14938 tsubst_expr ((NODE), args, complain, in_decl, \
14939 integral_constant_expression_p)
14940
14941 tree stmt, tmp;
14942 tree r;
14943 location_t loc;
14944
14945 if (t == NULL_TREE || t == error_mark_node)
14946 return t;
14947
14948 loc = input_location;
14949 if (EXPR_HAS_LOCATION (t))
14950 input_location = EXPR_LOCATION (t);
14951 if (STATEMENT_CODE_P (TREE_CODE (t)))
14952 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
14953
14954 switch (TREE_CODE (t))
14955 {
14956 case STATEMENT_LIST:
14957 {
14958 tree_stmt_iterator i;
14959 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
14960 RECUR (tsi_stmt (i));
14961 break;
14962 }
14963
14964 case CTOR_INITIALIZER:
14965 finish_mem_initializers (tsubst_initializer_list
14966 (TREE_OPERAND (t, 0), args));
14967 break;
14968
14969 case RETURN_EXPR:
14970 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
14971 break;
14972
14973 case EXPR_STMT:
14974 tmp = RECUR (EXPR_STMT_EXPR (t));
14975 if (EXPR_STMT_STMT_EXPR_RESULT (t))
14976 finish_stmt_expr_expr (tmp, cur_stmt_expr);
14977 else
14978 finish_expr_stmt (tmp);
14979 break;
14980
14981 case USING_STMT:
14982 do_using_directive (USING_STMT_NAMESPACE (t));
14983 break;
14984
14985 case DECL_EXPR:
14986 {
14987 tree decl, pattern_decl;
14988 tree init;
14989
14990 pattern_decl = decl = DECL_EXPR_DECL (t);
14991 if (TREE_CODE (decl) == LABEL_DECL)
14992 finish_label_decl (DECL_NAME (decl));
14993 else if (TREE_CODE (decl) == USING_DECL)
14994 {
14995 tree scope = USING_DECL_SCOPE (decl);
14996 tree name = DECL_NAME (decl);
14997 tree decl;
14998
14999 scope = tsubst (scope, args, complain, in_decl);
15000 decl = lookup_qualified_name (scope, name,
15001 /*is_type_p=*/false,
15002 /*complain=*/false);
15003 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15004 qualified_name_lookup_error (scope, name, decl, input_location);
15005 else
15006 do_local_using_decl (decl, scope, name);
15007 }
15008 else if (DECL_PACK_P (decl))
15009 {
15010 /* Don't build up decls for a variadic capture proxy, we'll
15011 instantiate the elements directly as needed. */
15012 break;
15013 }
15014 else
15015 {
15016 init = DECL_INITIAL (decl);
15017 decl = tsubst (decl, args, complain, in_decl);
15018 if (decl != error_mark_node)
15019 {
15020 /* By marking the declaration as instantiated, we avoid
15021 trying to instantiate it. Since instantiate_decl can't
15022 handle local variables, and since we've already done
15023 all that needs to be done, that's the right thing to
15024 do. */
15025 if (VAR_P (decl))
15026 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15027 if (VAR_P (decl)
15028 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15029 /* Anonymous aggregates are a special case. */
15030 finish_anon_union (decl);
15031 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15032 {
15033 DECL_CONTEXT (decl) = current_function_decl;
15034 if (DECL_NAME (decl) == this_identifier)
15035 {
15036 tree lam = DECL_CONTEXT (current_function_decl);
15037 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15038 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15039 }
15040 insert_capture_proxy (decl);
15041 }
15042 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15043 /* We already did a pushtag. */;
15044 else if (TREE_CODE (decl) == FUNCTION_DECL
15045 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15046 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15047 {
15048 DECL_CONTEXT (decl) = NULL_TREE;
15049 pushdecl (decl);
15050 DECL_CONTEXT (decl) = current_function_decl;
15051 cp_check_omp_declare_reduction (decl);
15052 }
15053 else
15054 {
15055 int const_init = false;
15056 maybe_push_decl (decl);
15057 if (VAR_P (decl)
15058 && DECL_PRETTY_FUNCTION_P (decl))
15059 {
15060 /* For __PRETTY_FUNCTION__ we have to adjust the
15061 initializer. */
15062 const char *const name
15063 = cxx_printable_name (current_function_decl, 2);
15064 init = cp_fname_init (name, &TREE_TYPE (decl));
15065 }
15066 else
15067 init = tsubst_init (init, decl, args, complain, in_decl);
15068
15069 if (VAR_P (decl))
15070 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15071 (pattern_decl));
15072 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15073 }
15074 }
15075 }
15076
15077 break;
15078 }
15079
15080 case FOR_STMT:
15081 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15082 RECUR (FOR_INIT_STMT (t));
15083 finish_for_init_stmt (stmt);
15084 tmp = RECUR (FOR_COND (t));
15085 finish_for_cond (tmp, stmt, false);
15086 tmp = RECUR (FOR_EXPR (t));
15087 finish_for_expr (tmp, stmt);
15088 RECUR (FOR_BODY (t));
15089 finish_for_stmt (stmt);
15090 break;
15091
15092 case RANGE_FOR_STMT:
15093 {
15094 tree decl, expr;
15095 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15096 decl = RANGE_FOR_DECL (t);
15097 decl = tsubst (decl, args, complain, in_decl);
15098 maybe_push_decl (decl);
15099 expr = RECUR (RANGE_FOR_EXPR (t));
15100 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
15101 RECUR (RANGE_FOR_BODY (t));
15102 finish_for_stmt (stmt);
15103 }
15104 break;
15105
15106 case WHILE_STMT:
15107 stmt = begin_while_stmt ();
15108 tmp = RECUR (WHILE_COND (t));
15109 finish_while_stmt_cond (tmp, stmt, false);
15110 RECUR (WHILE_BODY (t));
15111 finish_while_stmt (stmt);
15112 break;
15113
15114 case DO_STMT:
15115 stmt = begin_do_stmt ();
15116 RECUR (DO_BODY (t));
15117 finish_do_body (stmt);
15118 tmp = RECUR (DO_COND (t));
15119 finish_do_stmt (tmp, stmt, false);
15120 break;
15121
15122 case IF_STMT:
15123 stmt = begin_if_stmt ();
15124 tmp = RECUR (IF_COND (t));
15125 finish_if_stmt_cond (tmp, stmt);
15126 RECUR (THEN_CLAUSE (t));
15127 finish_then_clause (stmt);
15128
15129 if (ELSE_CLAUSE (t))
15130 {
15131 begin_else_clause (stmt);
15132 RECUR (ELSE_CLAUSE (t));
15133 finish_else_clause (stmt);
15134 }
15135
15136 finish_if_stmt (stmt);
15137 break;
15138
15139 case BIND_EXPR:
15140 if (BIND_EXPR_BODY_BLOCK (t))
15141 stmt = begin_function_body ();
15142 else
15143 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15144 ? BCS_TRY_BLOCK : 0);
15145
15146 RECUR (BIND_EXPR_BODY (t));
15147
15148 if (BIND_EXPR_BODY_BLOCK (t))
15149 finish_function_body (stmt);
15150 else
15151 finish_compound_stmt (stmt);
15152 break;
15153
15154 case BREAK_STMT:
15155 finish_break_stmt ();
15156 break;
15157
15158 case CONTINUE_STMT:
15159 finish_continue_stmt ();
15160 break;
15161
15162 case SWITCH_STMT:
15163 stmt = begin_switch_stmt ();
15164 tmp = RECUR (SWITCH_STMT_COND (t));
15165 finish_switch_cond (tmp, stmt);
15166 RECUR (SWITCH_STMT_BODY (t));
15167 finish_switch_stmt (stmt);
15168 break;
15169
15170 case CASE_LABEL_EXPR:
15171 {
15172 tree low = RECUR (CASE_LOW (t));
15173 tree high = RECUR (CASE_HIGH (t));
15174 finish_case_label (EXPR_LOCATION (t), low, high);
15175 }
15176 break;
15177
15178 case LABEL_EXPR:
15179 {
15180 tree decl = LABEL_EXPR_LABEL (t);
15181 tree label;
15182
15183 label = finish_label_stmt (DECL_NAME (decl));
15184 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15185 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15186 }
15187 break;
15188
15189 case GOTO_EXPR:
15190 tmp = GOTO_DESTINATION (t);
15191 if (TREE_CODE (tmp) != LABEL_DECL)
15192 /* Computed goto's must be tsubst'd into. On the other hand,
15193 non-computed gotos must not be; the identifier in question
15194 will have no binding. */
15195 tmp = RECUR (tmp);
15196 else
15197 tmp = DECL_NAME (tmp);
15198 finish_goto_stmt (tmp);
15199 break;
15200
15201 case ASM_EXPR:
15202 {
15203 tree string = RECUR (ASM_STRING (t));
15204 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15205 complain, in_decl);
15206 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15207 complain, in_decl);
15208 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15209 complain, in_decl);
15210 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15211 complain, in_decl);
15212 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15213 clobbers, labels);
15214 tree asm_expr = tmp;
15215 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15216 asm_expr = TREE_OPERAND (asm_expr, 0);
15217 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15218 }
15219 break;
15220
15221 case TRY_BLOCK:
15222 if (CLEANUP_P (t))
15223 {
15224 stmt = begin_try_block ();
15225 RECUR (TRY_STMTS (t));
15226 finish_cleanup_try_block (stmt);
15227 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15228 }
15229 else
15230 {
15231 tree compound_stmt = NULL_TREE;
15232
15233 if (FN_TRY_BLOCK_P (t))
15234 stmt = begin_function_try_block (&compound_stmt);
15235 else
15236 stmt = begin_try_block ();
15237
15238 RECUR (TRY_STMTS (t));
15239
15240 if (FN_TRY_BLOCK_P (t))
15241 finish_function_try_block (stmt);
15242 else
15243 finish_try_block (stmt);
15244
15245 RECUR (TRY_HANDLERS (t));
15246 if (FN_TRY_BLOCK_P (t))
15247 finish_function_handler_sequence (stmt, compound_stmt);
15248 else
15249 finish_handler_sequence (stmt);
15250 }
15251 break;
15252
15253 case HANDLER:
15254 {
15255 tree decl = HANDLER_PARMS (t);
15256
15257 if (decl)
15258 {
15259 decl = tsubst (decl, args, complain, in_decl);
15260 /* Prevent instantiate_decl from trying to instantiate
15261 this variable. We've already done all that needs to be
15262 done. */
15263 if (decl != error_mark_node)
15264 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15265 }
15266 stmt = begin_handler ();
15267 finish_handler_parms (decl, stmt);
15268 RECUR (HANDLER_BODY (t));
15269 finish_handler (stmt);
15270 }
15271 break;
15272
15273 case TAG_DEFN:
15274 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
15275 if (CLASS_TYPE_P (tmp))
15276 {
15277 /* Local classes are not independent templates; they are
15278 instantiated along with their containing function. And this
15279 way we don't have to deal with pushing out of one local class
15280 to instantiate a member of another local class. */
15281 tree fn;
15282 /* Closures are handled by the LAMBDA_EXPR. */
15283 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
15284 complete_type (tmp);
15285 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
15286 if (!DECL_ARTIFICIAL (fn))
15287 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
15288 }
15289 break;
15290
15291 case STATIC_ASSERT:
15292 {
15293 tree condition;
15294
15295 ++c_inhibit_evaluation_warnings;
15296 condition =
15297 tsubst_expr (STATIC_ASSERT_CONDITION (t),
15298 args,
15299 complain, in_decl,
15300 /*integral_constant_expression_p=*/true);
15301 --c_inhibit_evaluation_warnings;
15302
15303 finish_static_assert (condition,
15304 STATIC_ASSERT_MESSAGE (t),
15305 STATIC_ASSERT_SOURCE_LOCATION (t),
15306 /*member_p=*/false);
15307 }
15308 break;
15309
15310 case OACC_KERNELS:
15311 case OACC_PARALLEL:
15312 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, false, args, complain,
15313 in_decl);
15314 stmt = begin_omp_parallel ();
15315 RECUR (OMP_BODY (t));
15316 finish_omp_construct (TREE_CODE (t), stmt, tmp);
15317 break;
15318
15319 case OMP_PARALLEL:
15320 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
15321 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false, true,
15322 args, complain, in_decl);
15323 if (OMP_PARALLEL_COMBINED (t))
15324 omp_parallel_combined_clauses = &tmp;
15325 stmt = begin_omp_parallel ();
15326 RECUR (OMP_PARALLEL_BODY (t));
15327 gcc_assert (omp_parallel_combined_clauses == NULL);
15328 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15329 = OMP_PARALLEL_COMBINED (t);
15330 pop_omp_privatization_clauses (r);
15331 break;
15332
15333 case OMP_TASK:
15334 r = push_omp_privatization_clauses (false);
15335 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false, true,
15336 args, complain, in_decl);
15337 stmt = begin_omp_task ();
15338 RECUR (OMP_TASK_BODY (t));
15339 finish_omp_task (tmp, stmt);
15340 pop_omp_privatization_clauses (r);
15341 break;
15342
15343 case OMP_FOR:
15344 case OMP_SIMD:
15345 case CILK_SIMD:
15346 case CILK_FOR:
15347 case OMP_DISTRIBUTE:
15348 case OMP_TASKLOOP:
15349 case OACC_LOOP:
15350 {
15351 tree clauses, body, pre_body;
15352 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15353 tree orig_declv = NULL_TREE;
15354 tree incrv = NULL_TREE;
15355 int i;
15356
15357 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
15358 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
15359 TREE_CODE (t) != OACC_LOOP,
15360 args, complain, in_decl);
15361 if (OMP_FOR_INIT (t) != NULL_TREE)
15362 {
15363 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15364 if (OMP_FOR_ORIG_DECLS (t))
15365 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15366 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15367 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15368 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15369 }
15370
15371 stmt = begin_omp_structured_block ();
15372
15373 pre_body = push_stmt_list ();
15374 RECUR (OMP_FOR_PRE_BODY (t));
15375 pre_body = pop_stmt_list (pre_body);
15376
15377 if (OMP_FOR_INIT (t) != NULL_TREE)
15378 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15379 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
15380 incrv, &clauses, args, complain, in_decl,
15381 integral_constant_expression_p);
15382 omp_parallel_combined_clauses = NULL;
15383
15384 body = push_stmt_list ();
15385 RECUR (OMP_FOR_BODY (t));
15386 body = pop_stmt_list (body);
15387
15388 if (OMP_FOR_INIT (t) != NULL_TREE)
15389 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
15390 orig_declv, initv, condv, incrv, body, pre_body,
15391 NULL, clauses);
15392 else
15393 {
15394 t = make_node (TREE_CODE (t));
15395 TREE_TYPE (t) = void_type_node;
15396 OMP_FOR_BODY (t) = body;
15397 OMP_FOR_PRE_BODY (t) = pre_body;
15398 OMP_FOR_CLAUSES (t) = clauses;
15399 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15400 add_stmt (t);
15401 }
15402
15403 add_stmt (finish_omp_structured_block (stmt));
15404 pop_omp_privatization_clauses (r);
15405 }
15406 break;
15407
15408 case OMP_SECTIONS:
15409 omp_parallel_combined_clauses = NULL;
15410 /* FALLTHRU */
15411 case OMP_SINGLE:
15412 case OMP_TEAMS:
15413 case OMP_CRITICAL:
15414 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
15415 && OMP_TEAMS_COMBINED (t));
15416 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, true,
15417 args, complain, in_decl);
15418 stmt = push_stmt_list ();
15419 RECUR (OMP_BODY (t));
15420 stmt = pop_stmt_list (stmt);
15421
15422 t = copy_node (t);
15423 OMP_BODY (t) = stmt;
15424 OMP_CLAUSES (t) = tmp;
15425 add_stmt (t);
15426 pop_omp_privatization_clauses (r);
15427 break;
15428
15429 case OACC_DATA:
15430 case OMP_TARGET_DATA:
15431 case OMP_TARGET:
15432 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
15433 TREE_CODE (t) != OACC_DATA,
15434 args, complain, in_decl);
15435 keep_next_level (true);
15436 stmt = begin_omp_structured_block ();
15437
15438 RECUR (OMP_BODY (t));
15439 stmt = finish_omp_structured_block (stmt);
15440
15441 t = copy_node (t);
15442 OMP_BODY (t) = stmt;
15443 OMP_CLAUSES (t) = tmp;
15444 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
15445 {
15446 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
15447 if (teams)
15448 {
15449 /* For combined target teams, ensure the num_teams and
15450 thread_limit clause expressions are evaluated on the host,
15451 before entering the target construct. */
15452 tree c;
15453 for (c = OMP_TEAMS_CLAUSES (teams);
15454 c; c = OMP_CLAUSE_CHAIN (c))
15455 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15456 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15457 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15458 {
15459 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15460 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
15461 if (expr == error_mark_node)
15462 continue;
15463 tmp = TARGET_EXPR_SLOT (expr);
15464 add_stmt (expr);
15465 OMP_CLAUSE_OPERAND (c, 0) = expr;
15466 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15467 OMP_CLAUSE_FIRSTPRIVATE);
15468 OMP_CLAUSE_DECL (tc) = tmp;
15469 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
15470 OMP_TARGET_CLAUSES (t) = tc;
15471 }
15472 }
15473 }
15474 add_stmt (t);
15475 break;
15476
15477 case OACC_DECLARE:
15478 t = copy_node (t);
15479 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), false, false,
15480 args, complain, in_decl);
15481 OACC_DECLARE_CLAUSES (t) = tmp;
15482 add_stmt (t);
15483 break;
15484
15485 case OMP_TARGET_UPDATE:
15486 case OMP_TARGET_ENTER_DATA:
15487 case OMP_TARGET_EXIT_DATA:
15488 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, true,
15489 args, complain, in_decl);
15490 t = copy_node (t);
15491 OMP_STANDALONE_CLAUSES (t) = tmp;
15492 add_stmt (t);
15493 break;
15494
15495 case OACC_ENTER_DATA:
15496 case OACC_EXIT_DATA:
15497 case OACC_UPDATE:
15498 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, false,
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 OMP_ORDERED:
15506 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), false, true,
15507 args, complain, in_decl);
15508 stmt = push_stmt_list ();
15509 RECUR (OMP_BODY (t));
15510 stmt = pop_stmt_list (stmt);
15511
15512 t = copy_node (t);
15513 OMP_BODY (t) = stmt;
15514 OMP_ORDERED_CLAUSES (t) = tmp;
15515 add_stmt (t);
15516 break;
15517
15518 case OMP_SECTION:
15519 case OMP_MASTER:
15520 case OMP_TASKGROUP:
15521 stmt = push_stmt_list ();
15522 RECUR (OMP_BODY (t));
15523 stmt = pop_stmt_list (stmt);
15524
15525 t = copy_node (t);
15526 OMP_BODY (t) = stmt;
15527 add_stmt (t);
15528 break;
15529
15530 case OMP_ATOMIC:
15531 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
15532 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
15533 {
15534 tree op1 = TREE_OPERAND (t, 1);
15535 tree rhs1 = NULL_TREE;
15536 tree lhs, rhs;
15537 if (TREE_CODE (op1) == COMPOUND_EXPR)
15538 {
15539 rhs1 = RECUR (TREE_OPERAND (op1, 0));
15540 op1 = TREE_OPERAND (op1, 1);
15541 }
15542 lhs = RECUR (TREE_OPERAND (op1, 0));
15543 rhs = RECUR (TREE_OPERAND (op1, 1));
15544 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
15545 NULL_TREE, NULL_TREE, rhs1,
15546 OMP_ATOMIC_SEQ_CST (t));
15547 }
15548 else
15549 {
15550 tree op1 = TREE_OPERAND (t, 1);
15551 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
15552 tree rhs1 = NULL_TREE;
15553 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
15554 enum tree_code opcode = NOP_EXPR;
15555 if (code == OMP_ATOMIC_READ)
15556 {
15557 v = RECUR (TREE_OPERAND (op1, 0));
15558 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15559 }
15560 else if (code == OMP_ATOMIC_CAPTURE_OLD
15561 || code == OMP_ATOMIC_CAPTURE_NEW)
15562 {
15563 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
15564 v = RECUR (TREE_OPERAND (op1, 0));
15565 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15566 if (TREE_CODE (op11) == COMPOUND_EXPR)
15567 {
15568 rhs1 = RECUR (TREE_OPERAND (op11, 0));
15569 op11 = TREE_OPERAND (op11, 1);
15570 }
15571 lhs = RECUR (TREE_OPERAND (op11, 0));
15572 rhs = RECUR (TREE_OPERAND (op11, 1));
15573 opcode = TREE_CODE (op11);
15574 if (opcode == MODIFY_EXPR)
15575 opcode = NOP_EXPR;
15576 }
15577 else
15578 {
15579 code = OMP_ATOMIC;
15580 lhs = RECUR (TREE_OPERAND (op1, 0));
15581 rhs = RECUR (TREE_OPERAND (op1, 1));
15582 }
15583 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
15584 OMP_ATOMIC_SEQ_CST (t));
15585 }
15586 break;
15587
15588 case TRANSACTION_EXPR:
15589 {
15590 int flags = 0;
15591 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
15592 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
15593
15594 if (TRANSACTION_EXPR_IS_STMT (t))
15595 {
15596 tree body = TRANSACTION_EXPR_BODY (t);
15597 tree noex = NULL_TREE;
15598 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
15599 {
15600 noex = MUST_NOT_THROW_COND (body);
15601 if (noex == NULL_TREE)
15602 noex = boolean_true_node;
15603 body = TREE_OPERAND (body, 0);
15604 }
15605 stmt = begin_transaction_stmt (input_location, NULL, flags);
15606 RECUR (body);
15607 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
15608 }
15609 else
15610 {
15611 stmt = build_transaction_expr (EXPR_LOCATION (t),
15612 RECUR (TRANSACTION_EXPR_BODY (t)),
15613 flags, NULL_TREE);
15614 RETURN (stmt);
15615 }
15616 }
15617 break;
15618
15619 case MUST_NOT_THROW_EXPR:
15620 {
15621 tree op0 = RECUR (TREE_OPERAND (t, 0));
15622 tree cond = RECUR (MUST_NOT_THROW_COND (t));
15623 RETURN (build_must_not_throw_expr (op0, cond));
15624 }
15625
15626 case EXPR_PACK_EXPANSION:
15627 error ("invalid use of pack expansion expression");
15628 RETURN (error_mark_node);
15629
15630 case NONTYPE_ARGUMENT_PACK:
15631 error ("use %<...%> to expand argument pack");
15632 RETURN (error_mark_node);
15633
15634 case CILK_SPAWN_STMT:
15635 cfun->calls_cilk_spawn = 1;
15636 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
15637
15638 case CILK_SYNC_STMT:
15639 RETURN (build_cilk_sync ());
15640
15641 case COMPOUND_EXPR:
15642 tmp = RECUR (TREE_OPERAND (t, 0));
15643 if (tmp == NULL_TREE)
15644 /* If the first operand was a statement, we're done with it. */
15645 RETURN (RECUR (TREE_OPERAND (t, 1)));
15646 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
15647 RECUR (TREE_OPERAND (t, 1)),
15648 complain));
15649
15650 case ANNOTATE_EXPR:
15651 tmp = RECUR (TREE_OPERAND (t, 0));
15652 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
15653 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
15654
15655 default:
15656 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
15657
15658 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
15659 /*function_p=*/false,
15660 integral_constant_expression_p));
15661 }
15662
15663 RETURN (NULL_TREE);
15664 out:
15665 input_location = loc;
15666 return r;
15667 #undef RECUR
15668 #undef RETURN
15669 }
15670
15671 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15672 function. For description of the body see comment above
15673 cp_parser_omp_declare_reduction_exprs. */
15674
15675 static void
15676 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15677 {
15678 if (t == NULL_TREE || t == error_mark_node)
15679 return;
15680
15681 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
15682
15683 tree_stmt_iterator tsi;
15684 int i;
15685 tree stmts[7];
15686 memset (stmts, 0, sizeof stmts);
15687 for (i = 0, tsi = tsi_start (t);
15688 i < 7 && !tsi_end_p (tsi);
15689 i++, tsi_next (&tsi))
15690 stmts[i] = tsi_stmt (tsi);
15691 gcc_assert (tsi_end_p (tsi));
15692
15693 if (i >= 3)
15694 {
15695 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
15696 && TREE_CODE (stmts[1]) == DECL_EXPR);
15697 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
15698 args, complain, in_decl);
15699 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
15700 args, complain, in_decl);
15701 DECL_CONTEXT (omp_out) = current_function_decl;
15702 DECL_CONTEXT (omp_in) = current_function_decl;
15703 keep_next_level (true);
15704 tree block = begin_omp_structured_block ();
15705 tsubst_expr (stmts[2], args, complain, in_decl, false);
15706 block = finish_omp_structured_block (block);
15707 block = maybe_cleanup_point_expr_void (block);
15708 add_decl_expr (omp_out);
15709 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
15710 TREE_NO_WARNING (omp_out) = 1;
15711 add_decl_expr (omp_in);
15712 finish_expr_stmt (block);
15713 }
15714 if (i >= 6)
15715 {
15716 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
15717 && TREE_CODE (stmts[4]) == DECL_EXPR);
15718 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
15719 args, complain, in_decl);
15720 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
15721 args, complain, in_decl);
15722 DECL_CONTEXT (omp_priv) = current_function_decl;
15723 DECL_CONTEXT (omp_orig) = current_function_decl;
15724 keep_next_level (true);
15725 tree block = begin_omp_structured_block ();
15726 tsubst_expr (stmts[5], args, complain, in_decl, false);
15727 block = finish_omp_structured_block (block);
15728 block = maybe_cleanup_point_expr_void (block);
15729 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
15730 add_decl_expr (omp_priv);
15731 add_decl_expr (omp_orig);
15732 finish_expr_stmt (block);
15733 if (i == 7)
15734 add_decl_expr (omp_orig);
15735 }
15736 }
15737
15738 /* T is a postfix-expression that is not being used in a function
15739 call. Return the substituted version of T. */
15740
15741 static tree
15742 tsubst_non_call_postfix_expression (tree t, tree args,
15743 tsubst_flags_t complain,
15744 tree in_decl)
15745 {
15746 if (TREE_CODE (t) == SCOPE_REF)
15747 t = tsubst_qualified_id (t, args, complain, in_decl,
15748 /*done=*/false, /*address_p=*/false);
15749 else
15750 t = tsubst_copy_and_build (t, args, complain, in_decl,
15751 /*function_p=*/false,
15752 /*integral_constant_expression_p=*/false);
15753
15754 return t;
15755 }
15756
15757 /* Like tsubst but deals with expressions and performs semantic
15758 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15759
15760 tree
15761 tsubst_copy_and_build (tree t,
15762 tree args,
15763 tsubst_flags_t complain,
15764 tree in_decl,
15765 bool function_p,
15766 bool integral_constant_expression_p)
15767 {
15768 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15769 #define RECUR(NODE) \
15770 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15771 /*function_p=*/false, \
15772 integral_constant_expression_p)
15773
15774 tree retval, op1;
15775 location_t loc;
15776
15777 if (t == NULL_TREE || t == error_mark_node)
15778 return t;
15779
15780 loc = input_location;
15781 if (EXPR_HAS_LOCATION (t))
15782 input_location = EXPR_LOCATION (t);
15783
15784 /* N3276 decltype magic only applies to calls at the top level or on the
15785 right side of a comma. */
15786 tsubst_flags_t decltype_flag = (complain & tf_decltype);
15787 complain &= ~tf_decltype;
15788
15789 switch (TREE_CODE (t))
15790 {
15791 case USING_DECL:
15792 t = DECL_NAME (t);
15793 /* Fall through. */
15794 case IDENTIFIER_NODE:
15795 {
15796 tree decl;
15797 cp_id_kind idk;
15798 bool non_integral_constant_expression_p;
15799 const char *error_msg;
15800
15801 if (IDENTIFIER_TYPENAME_P (t))
15802 {
15803 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15804 t = mangle_conv_op_name_for_type (new_type);
15805 }
15806
15807 /* Look up the name. */
15808 decl = lookup_name (t);
15809
15810 /* By convention, expressions use ERROR_MARK_NODE to indicate
15811 failure, not NULL_TREE. */
15812 if (decl == NULL_TREE)
15813 decl = error_mark_node;
15814
15815 decl = finish_id_expression (t, decl, NULL_TREE,
15816 &idk,
15817 integral_constant_expression_p,
15818 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
15819 &non_integral_constant_expression_p,
15820 /*template_p=*/false,
15821 /*done=*/true,
15822 /*address_p=*/false,
15823 /*template_arg_p=*/false,
15824 &error_msg,
15825 input_location);
15826 if (error_msg)
15827 error (error_msg);
15828 if (!function_p && identifier_p (decl))
15829 {
15830 if (complain & tf_error)
15831 unqualified_name_lookup_error (decl);
15832 decl = error_mark_node;
15833 }
15834 RETURN (decl);
15835 }
15836
15837 case TEMPLATE_ID_EXPR:
15838 {
15839 tree object;
15840 tree templ = RECUR (TREE_OPERAND (t, 0));
15841 tree targs = TREE_OPERAND (t, 1);
15842
15843 if (targs)
15844 targs = tsubst_template_args (targs, args, complain, in_decl);
15845 if (targs == error_mark_node)
15846 return error_mark_node;
15847
15848 if (variable_template_p (templ))
15849 {
15850 templ = lookup_template_variable (templ, targs);
15851 if (!any_dependent_template_arguments_p (targs))
15852 {
15853 templ = finish_template_variable (templ, complain);
15854 mark_used (templ);
15855 }
15856 RETURN (convert_from_reference (templ));
15857 }
15858
15859 if (TREE_CODE (templ) == COMPONENT_REF)
15860 {
15861 object = TREE_OPERAND (templ, 0);
15862 templ = TREE_OPERAND (templ, 1);
15863 }
15864 else
15865 object = NULL_TREE;
15866 templ = lookup_template_function (templ, targs);
15867
15868 if (object)
15869 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
15870 object, templ, NULL_TREE));
15871 else
15872 RETURN (baselink_for_fns (templ));
15873 }
15874
15875 case INDIRECT_REF:
15876 {
15877 tree r = RECUR (TREE_OPERAND (t, 0));
15878
15879 if (REFERENCE_REF_P (t))
15880 {
15881 /* A type conversion to reference type will be enclosed in
15882 such an indirect ref, but the substitution of the cast
15883 will have also added such an indirect ref. */
15884 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
15885 r = convert_from_reference (r);
15886 }
15887 else
15888 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
15889 complain|decltype_flag);
15890 RETURN (r);
15891 }
15892
15893 case NOP_EXPR:
15894 {
15895 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15896 tree op0 = RECUR (TREE_OPERAND (t, 0));
15897 RETURN (build_nop (type, op0));
15898 }
15899
15900 case IMPLICIT_CONV_EXPR:
15901 {
15902 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15903 tree expr = RECUR (TREE_OPERAND (t, 0));
15904 int flags = LOOKUP_IMPLICIT;
15905 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
15906 flags = LOOKUP_NORMAL;
15907 RETURN (perform_implicit_conversion_flags (type, expr, complain,
15908 flags));
15909 }
15910
15911 case CONVERT_EXPR:
15912 {
15913 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15914 tree op0 = RECUR (TREE_OPERAND (t, 0));
15915 RETURN (build1 (CONVERT_EXPR, type, op0));
15916 }
15917
15918 case CAST_EXPR:
15919 case REINTERPRET_CAST_EXPR:
15920 case CONST_CAST_EXPR:
15921 case DYNAMIC_CAST_EXPR:
15922 case STATIC_CAST_EXPR:
15923 {
15924 tree type;
15925 tree op, r = NULL_TREE;
15926
15927 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15928 if (integral_constant_expression_p
15929 && !cast_valid_in_integral_constant_expression_p (type))
15930 {
15931 if (complain & tf_error)
15932 error ("a cast to a type other than an integral or "
15933 "enumeration type cannot appear in a constant-expression");
15934 RETURN (error_mark_node);
15935 }
15936
15937 op = RECUR (TREE_OPERAND (t, 0));
15938
15939 warning_sentinel s(warn_useless_cast);
15940 switch (TREE_CODE (t))
15941 {
15942 case CAST_EXPR:
15943 r = build_functional_cast (type, op, complain);
15944 break;
15945 case REINTERPRET_CAST_EXPR:
15946 r = build_reinterpret_cast (type, op, complain);
15947 break;
15948 case CONST_CAST_EXPR:
15949 r = build_const_cast (type, op, complain);
15950 break;
15951 case DYNAMIC_CAST_EXPR:
15952 r = build_dynamic_cast (type, op, complain);
15953 break;
15954 case STATIC_CAST_EXPR:
15955 r = build_static_cast (type, op, complain);
15956 break;
15957 default:
15958 gcc_unreachable ();
15959 }
15960
15961 RETURN (r);
15962 }
15963
15964 case POSTDECREMENT_EXPR:
15965 case POSTINCREMENT_EXPR:
15966 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15967 args, complain, in_decl);
15968 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
15969 complain|decltype_flag));
15970
15971 case PREDECREMENT_EXPR:
15972 case PREINCREMENT_EXPR:
15973 case NEGATE_EXPR:
15974 case BIT_NOT_EXPR:
15975 case ABS_EXPR:
15976 case TRUTH_NOT_EXPR:
15977 case UNARY_PLUS_EXPR: /* Unary + */
15978 case REALPART_EXPR:
15979 case IMAGPART_EXPR:
15980 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
15981 RECUR (TREE_OPERAND (t, 0)),
15982 complain|decltype_flag));
15983
15984 case FIX_TRUNC_EXPR:
15985 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
15986 0, complain));
15987
15988 case ADDR_EXPR:
15989 op1 = TREE_OPERAND (t, 0);
15990 if (TREE_CODE (op1) == LABEL_DECL)
15991 RETURN (finish_label_address_expr (DECL_NAME (op1),
15992 EXPR_LOCATION (op1)));
15993 if (TREE_CODE (op1) == SCOPE_REF)
15994 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
15995 /*done=*/true, /*address_p=*/true);
15996 else
15997 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
15998 in_decl);
15999 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16000 complain|decltype_flag));
16001
16002 case PLUS_EXPR:
16003 case MINUS_EXPR:
16004 case MULT_EXPR:
16005 case TRUNC_DIV_EXPR:
16006 case CEIL_DIV_EXPR:
16007 case FLOOR_DIV_EXPR:
16008 case ROUND_DIV_EXPR:
16009 case EXACT_DIV_EXPR:
16010 case BIT_AND_EXPR:
16011 case BIT_IOR_EXPR:
16012 case BIT_XOR_EXPR:
16013 case TRUNC_MOD_EXPR:
16014 case FLOOR_MOD_EXPR:
16015 case TRUTH_ANDIF_EXPR:
16016 case TRUTH_ORIF_EXPR:
16017 case TRUTH_AND_EXPR:
16018 case TRUTH_OR_EXPR:
16019 case RSHIFT_EXPR:
16020 case LSHIFT_EXPR:
16021 case RROTATE_EXPR:
16022 case LROTATE_EXPR:
16023 case EQ_EXPR:
16024 case NE_EXPR:
16025 case MAX_EXPR:
16026 case MIN_EXPR:
16027 case LE_EXPR:
16028 case GE_EXPR:
16029 case LT_EXPR:
16030 case GT_EXPR:
16031 case MEMBER_REF:
16032 case DOTSTAR_EXPR:
16033 {
16034 warning_sentinel s1(warn_type_limits);
16035 warning_sentinel s2(warn_div_by_zero);
16036 warning_sentinel s3(warn_logical_op);
16037 warning_sentinel s4(warn_tautological_compare);
16038 tree op0 = RECUR (TREE_OPERAND (t, 0));
16039 tree op1 = RECUR (TREE_OPERAND (t, 1));
16040 tree r = build_x_binary_op
16041 (input_location, TREE_CODE (t),
16042 op0,
16043 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16044 ? ERROR_MARK
16045 : TREE_CODE (TREE_OPERAND (t, 0))),
16046 op1,
16047 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16048 ? ERROR_MARK
16049 : TREE_CODE (TREE_OPERAND (t, 1))),
16050 /*overload=*/NULL,
16051 complain|decltype_flag);
16052 if (EXPR_P (r) && TREE_NO_WARNING (t))
16053 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16054
16055 RETURN (r);
16056 }
16057
16058 case POINTER_PLUS_EXPR:
16059 {
16060 tree op0 = RECUR (TREE_OPERAND (t, 0));
16061 tree op1 = RECUR (TREE_OPERAND (t, 1));
16062 return fold_build_pointer_plus (op0, op1);
16063 }
16064
16065 case SCOPE_REF:
16066 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16067 /*address_p=*/false));
16068 case ARRAY_REF:
16069 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16070 args, complain, in_decl);
16071 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16072 RECUR (TREE_OPERAND (t, 1)),
16073 complain|decltype_flag));
16074
16075 case ARRAY_NOTATION_REF:
16076 {
16077 tree start_index, length, stride;
16078 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16079 args, complain, in_decl);
16080 start_index = RECUR (ARRAY_NOTATION_START (t));
16081 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16082 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16083 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16084 length, stride, TREE_TYPE (op1)));
16085 }
16086 case SIZEOF_EXPR:
16087 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
16088 RETURN (tsubst_copy (t, args, complain, in_decl));
16089 /* Fall through */
16090
16091 case ALIGNOF_EXPR:
16092 {
16093 tree r;
16094
16095 op1 = TREE_OPERAND (t, 0);
16096 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16097 op1 = TREE_TYPE (op1);
16098 if (!args)
16099 {
16100 /* When there are no ARGS, we are trying to evaluate a
16101 non-dependent expression from the parser. Trying to do
16102 the substitutions may not work. */
16103 if (!TYPE_P (op1))
16104 op1 = TREE_TYPE (op1);
16105 }
16106 else
16107 {
16108 ++cp_unevaluated_operand;
16109 ++c_inhibit_evaluation_warnings;
16110 if (TYPE_P (op1))
16111 op1 = tsubst (op1, args, complain, in_decl);
16112 else
16113 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16114 /*function_p=*/false,
16115 /*integral_constant_expression_p=*/
16116 false);
16117 --cp_unevaluated_operand;
16118 --c_inhibit_evaluation_warnings;
16119 }
16120 if (TYPE_P (op1))
16121 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16122 complain & tf_error);
16123 else
16124 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16125 complain & tf_error);
16126 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16127 {
16128 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16129 {
16130 if (!processing_template_decl && TYPE_P (op1))
16131 {
16132 r = build_min (SIZEOF_EXPR, size_type_node,
16133 build1 (NOP_EXPR, op1, error_mark_node));
16134 SIZEOF_EXPR_TYPE_P (r) = 1;
16135 }
16136 else
16137 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16138 TREE_SIDE_EFFECTS (r) = 0;
16139 TREE_READONLY (r) = 1;
16140 }
16141 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16142 }
16143 RETURN (r);
16144 }
16145
16146 case AT_ENCODE_EXPR:
16147 {
16148 op1 = TREE_OPERAND (t, 0);
16149 ++cp_unevaluated_operand;
16150 ++c_inhibit_evaluation_warnings;
16151 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16152 /*function_p=*/false,
16153 /*integral_constant_expression_p=*/false);
16154 --cp_unevaluated_operand;
16155 --c_inhibit_evaluation_warnings;
16156 RETURN (objc_build_encode_expr (op1));
16157 }
16158
16159 case NOEXCEPT_EXPR:
16160 op1 = TREE_OPERAND (t, 0);
16161 ++cp_unevaluated_operand;
16162 ++c_inhibit_evaluation_warnings;
16163 ++cp_noexcept_operand;
16164 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16165 /*function_p=*/false,
16166 /*integral_constant_expression_p=*/false);
16167 --cp_unevaluated_operand;
16168 --c_inhibit_evaluation_warnings;
16169 --cp_noexcept_operand;
16170 RETURN (finish_noexcept_expr (op1, complain));
16171
16172 case MODOP_EXPR:
16173 {
16174 warning_sentinel s(warn_div_by_zero);
16175 tree lhs = RECUR (TREE_OPERAND (t, 0));
16176 tree rhs = RECUR (TREE_OPERAND (t, 2));
16177 tree r = build_x_modify_expr
16178 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16179 complain|decltype_flag);
16180 /* TREE_NO_WARNING must be set if either the expression was
16181 parenthesized or it uses an operator such as >>= rather
16182 than plain assignment. In the former case, it was already
16183 set and must be copied. In the latter case,
16184 build_x_modify_expr sets it and it must not be reset
16185 here. */
16186 if (TREE_NO_WARNING (t))
16187 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16188
16189 RETURN (r);
16190 }
16191
16192 case ARROW_EXPR:
16193 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16194 args, complain, in_decl);
16195 /* Remember that there was a reference to this entity. */
16196 if (DECL_P (op1)
16197 && !mark_used (op1, complain) && !(complain & tf_error))
16198 RETURN (error_mark_node);
16199 RETURN (build_x_arrow (input_location, op1, complain));
16200
16201 case NEW_EXPR:
16202 {
16203 tree placement = RECUR (TREE_OPERAND (t, 0));
16204 tree init = RECUR (TREE_OPERAND (t, 3));
16205 vec<tree, va_gc> *placement_vec;
16206 vec<tree, va_gc> *init_vec;
16207 tree ret;
16208
16209 if (placement == NULL_TREE)
16210 placement_vec = NULL;
16211 else
16212 {
16213 placement_vec = make_tree_vector ();
16214 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16215 vec_safe_push (placement_vec, TREE_VALUE (placement));
16216 }
16217
16218 /* If there was an initializer in the original tree, but it
16219 instantiated to an empty list, then we should pass a
16220 non-NULL empty vector to tell build_new that it was an
16221 empty initializer() rather than no initializer. This can
16222 only happen when the initializer is a pack expansion whose
16223 parameter packs are of length zero. */
16224 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16225 init_vec = NULL;
16226 else
16227 {
16228 init_vec = make_tree_vector ();
16229 if (init == void_node)
16230 gcc_assert (init_vec != NULL);
16231 else
16232 {
16233 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16234 vec_safe_push (init_vec, TREE_VALUE (init));
16235 }
16236 }
16237
16238 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16239 tree op2 = RECUR (TREE_OPERAND (t, 2));
16240 ret = build_new (&placement_vec, op1, op2, &init_vec,
16241 NEW_EXPR_USE_GLOBAL (t),
16242 complain);
16243
16244 if (placement_vec != NULL)
16245 release_tree_vector (placement_vec);
16246 if (init_vec != NULL)
16247 release_tree_vector (init_vec);
16248
16249 RETURN (ret);
16250 }
16251
16252 case DELETE_EXPR:
16253 {
16254 tree op0 = RECUR (TREE_OPERAND (t, 0));
16255 tree op1 = RECUR (TREE_OPERAND (t, 1));
16256 RETURN (delete_sanity (op0, op1,
16257 DELETE_EXPR_USE_VEC (t),
16258 DELETE_EXPR_USE_GLOBAL (t),
16259 complain));
16260 }
16261
16262 case COMPOUND_EXPR:
16263 {
16264 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
16265 complain & ~tf_decltype, in_decl,
16266 /*function_p=*/false,
16267 integral_constant_expression_p);
16268 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
16269 op0,
16270 RECUR (TREE_OPERAND (t, 1)),
16271 complain|decltype_flag));
16272 }
16273
16274 case CALL_EXPR:
16275 {
16276 tree function;
16277 vec<tree, va_gc> *call_args;
16278 unsigned int nargs, i;
16279 bool qualified_p;
16280 bool koenig_p;
16281 tree ret;
16282
16283 function = CALL_EXPR_FN (t);
16284 /* When we parsed the expression, we determined whether or
16285 not Koenig lookup should be performed. */
16286 koenig_p = KOENIG_LOOKUP_P (t);
16287 if (TREE_CODE (function) == SCOPE_REF)
16288 {
16289 qualified_p = true;
16290 function = tsubst_qualified_id (function, args, complain, in_decl,
16291 /*done=*/false,
16292 /*address_p=*/false);
16293 }
16294 else if (koenig_p && identifier_p (function))
16295 {
16296 /* Do nothing; calling tsubst_copy_and_build on an identifier
16297 would incorrectly perform unqualified lookup again.
16298
16299 Note that we can also have an IDENTIFIER_NODE if the earlier
16300 unqualified lookup found a member function; in that case
16301 koenig_p will be false and we do want to do the lookup
16302 again to find the instantiated member function.
16303
16304 FIXME but doing that causes c++/15272, so we need to stop
16305 using IDENTIFIER_NODE in that situation. */
16306 qualified_p = false;
16307 }
16308 else
16309 {
16310 if (TREE_CODE (function) == COMPONENT_REF)
16311 {
16312 tree op = TREE_OPERAND (function, 1);
16313
16314 qualified_p = (TREE_CODE (op) == SCOPE_REF
16315 || (BASELINK_P (op)
16316 && BASELINK_QUALIFIED_P (op)));
16317 }
16318 else
16319 qualified_p = false;
16320
16321 if (TREE_CODE (function) == ADDR_EXPR
16322 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
16323 /* Avoid error about taking the address of a constructor. */
16324 function = TREE_OPERAND (function, 0);
16325
16326 function = tsubst_copy_and_build (function, args, complain,
16327 in_decl,
16328 !qualified_p,
16329 integral_constant_expression_p);
16330
16331 if (BASELINK_P (function))
16332 qualified_p = true;
16333 }
16334
16335 nargs = call_expr_nargs (t);
16336 call_args = make_tree_vector ();
16337 for (i = 0; i < nargs; ++i)
16338 {
16339 tree arg = CALL_EXPR_ARG (t, i);
16340
16341 if (!PACK_EXPANSION_P (arg))
16342 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
16343 else
16344 {
16345 /* Expand the pack expansion and push each entry onto
16346 CALL_ARGS. */
16347 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
16348 if (TREE_CODE (arg) == TREE_VEC)
16349 {
16350 unsigned int len, j;
16351
16352 len = TREE_VEC_LENGTH (arg);
16353 for (j = 0; j < len; ++j)
16354 {
16355 tree value = TREE_VEC_ELT (arg, j);
16356 if (value != NULL_TREE)
16357 value = convert_from_reference (value);
16358 vec_safe_push (call_args, value);
16359 }
16360 }
16361 else
16362 {
16363 /* A partial substitution. Add one entry. */
16364 vec_safe_push (call_args, arg);
16365 }
16366 }
16367 }
16368
16369 /* We do not perform argument-dependent lookup if normal
16370 lookup finds a non-function, in accordance with the
16371 expected resolution of DR 218. */
16372 if (koenig_p
16373 && ((is_overloaded_fn (function)
16374 /* If lookup found a member function, the Koenig lookup is
16375 not appropriate, even if an unqualified-name was used
16376 to denote the function. */
16377 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
16378 || identifier_p (function))
16379 /* Only do this when substitution turns a dependent call
16380 into a non-dependent call. */
16381 && type_dependent_expression_p_push (t)
16382 && !any_type_dependent_arguments_p (call_args))
16383 function = perform_koenig_lookup (function, call_args, tf_none);
16384
16385 if (identifier_p (function)
16386 && !any_type_dependent_arguments_p (call_args))
16387 {
16388 if (koenig_p && (complain & tf_warning_or_error))
16389 {
16390 /* For backwards compatibility and good diagnostics, try
16391 the unqualified lookup again if we aren't in SFINAE
16392 context. */
16393 tree unq = (tsubst_copy_and_build
16394 (function, args, complain, in_decl, true,
16395 integral_constant_expression_p));
16396 if (unq == error_mark_node)
16397 RETURN (error_mark_node);
16398
16399 if (unq != function)
16400 {
16401 tree fn = unq;
16402 if (INDIRECT_REF_P (fn))
16403 fn = TREE_OPERAND (fn, 0);
16404 if (TREE_CODE (fn) == COMPONENT_REF)
16405 fn = TREE_OPERAND (fn, 1);
16406 if (is_overloaded_fn (fn))
16407 fn = get_first_fn (fn);
16408 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16409 "%qD was not declared in this scope, "
16410 "and no declarations were found by "
16411 "argument-dependent lookup at the point "
16412 "of instantiation", function))
16413 {
16414 if (!DECL_P (fn))
16415 /* Can't say anything more. */;
16416 else if (DECL_CLASS_SCOPE_P (fn))
16417 {
16418 location_t loc = EXPR_LOC_OR_LOC (t,
16419 input_location);
16420 inform (loc,
16421 "declarations in dependent base %qT are "
16422 "not found by unqualified lookup",
16423 DECL_CLASS_CONTEXT (fn));
16424 if (current_class_ptr)
16425 inform (loc,
16426 "use %<this->%D%> instead", function);
16427 else
16428 inform (loc,
16429 "use %<%T::%D%> instead",
16430 current_class_name, function);
16431 }
16432 else
16433 inform (DECL_SOURCE_LOCATION (fn),
16434 "%qD declared here, later in the "
16435 "translation unit", fn);
16436 }
16437 function = unq;
16438 }
16439 }
16440 if (identifier_p (function))
16441 {
16442 if (complain & tf_error)
16443 unqualified_name_lookup_error (function);
16444 release_tree_vector (call_args);
16445 RETURN (error_mark_node);
16446 }
16447 }
16448
16449 /* Remember that there was a reference to this entity. */
16450 if (DECL_P (function)
16451 && !mark_used (function, complain) && !(complain & tf_error))
16452 RETURN (error_mark_node);
16453
16454 /* Put back tf_decltype for the actual call. */
16455 complain |= decltype_flag;
16456
16457 if (TREE_CODE (function) == OFFSET_REF)
16458 ret = build_offset_ref_call_from_tree (function, &call_args,
16459 complain);
16460 else if (TREE_CODE (function) == COMPONENT_REF)
16461 {
16462 tree instance = TREE_OPERAND (function, 0);
16463 tree fn = TREE_OPERAND (function, 1);
16464
16465 if (processing_template_decl
16466 && (type_dependent_expression_p (instance)
16467 || (!BASELINK_P (fn)
16468 && TREE_CODE (fn) != FIELD_DECL)
16469 || type_dependent_expression_p (fn)
16470 || any_type_dependent_arguments_p (call_args)))
16471 ret = build_nt_call_vec (function, call_args);
16472 else if (!BASELINK_P (fn))
16473 ret = finish_call_expr (function, &call_args,
16474 /*disallow_virtual=*/false,
16475 /*koenig_p=*/false,
16476 complain);
16477 else
16478 ret = (build_new_method_call
16479 (instance, fn,
16480 &call_args, NULL_TREE,
16481 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
16482 /*fn_p=*/NULL,
16483 complain));
16484 }
16485 else
16486 ret = finish_call_expr (function, &call_args,
16487 /*disallow_virtual=*/qualified_p,
16488 koenig_p,
16489 complain);
16490
16491 release_tree_vector (call_args);
16492
16493 RETURN (ret);
16494 }
16495
16496 case COND_EXPR:
16497 {
16498 tree cond = RECUR (TREE_OPERAND (t, 0));
16499 tree folded_cond = fold_non_dependent_expr (cond);
16500 tree exp1, exp2;
16501
16502 if (TREE_CODE (folded_cond) == INTEGER_CST)
16503 {
16504 if (integer_zerop (folded_cond))
16505 {
16506 ++c_inhibit_evaluation_warnings;
16507 exp1 = RECUR (TREE_OPERAND (t, 1));
16508 --c_inhibit_evaluation_warnings;
16509 exp2 = RECUR (TREE_OPERAND (t, 2));
16510 }
16511 else
16512 {
16513 exp1 = RECUR (TREE_OPERAND (t, 1));
16514 ++c_inhibit_evaluation_warnings;
16515 exp2 = RECUR (TREE_OPERAND (t, 2));
16516 --c_inhibit_evaluation_warnings;
16517 }
16518 cond = folded_cond;
16519 }
16520 else
16521 {
16522 exp1 = RECUR (TREE_OPERAND (t, 1));
16523 exp2 = RECUR (TREE_OPERAND (t, 2));
16524 }
16525
16526 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
16527 cond, exp1, exp2, complain));
16528 }
16529
16530 case PSEUDO_DTOR_EXPR:
16531 {
16532 tree op0 = RECUR (TREE_OPERAND (t, 0));
16533 tree op1 = RECUR (TREE_OPERAND (t, 1));
16534 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
16535 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
16536 input_location));
16537 }
16538
16539 case TREE_LIST:
16540 {
16541 tree purpose, value, chain;
16542
16543 if (t == void_list_node)
16544 RETURN (t);
16545
16546 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
16547 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
16548 {
16549 /* We have pack expansions, so expand those and
16550 create a new list out of it. */
16551 tree purposevec = NULL_TREE;
16552 tree valuevec = NULL_TREE;
16553 tree chain;
16554 int i, len = -1;
16555
16556 /* Expand the argument expressions. */
16557 if (TREE_PURPOSE (t))
16558 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
16559 complain, in_decl);
16560 if (TREE_VALUE (t))
16561 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
16562 complain, in_decl);
16563
16564 /* Build the rest of the list. */
16565 chain = TREE_CHAIN (t);
16566 if (chain && chain != void_type_node)
16567 chain = RECUR (chain);
16568
16569 /* Determine the number of arguments. */
16570 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
16571 {
16572 len = TREE_VEC_LENGTH (purposevec);
16573 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16574 }
16575 else if (TREE_CODE (valuevec) == TREE_VEC)
16576 len = TREE_VEC_LENGTH (valuevec);
16577 else
16578 {
16579 /* Since we only performed a partial substitution into
16580 the argument pack, we only RETURN (a single list
16581 node. */
16582 if (purposevec == TREE_PURPOSE (t)
16583 && valuevec == TREE_VALUE (t)
16584 && chain == TREE_CHAIN (t))
16585 RETURN (t);
16586
16587 RETURN (tree_cons (purposevec, valuevec, chain));
16588 }
16589
16590 /* Convert the argument vectors into a TREE_LIST */
16591 i = len;
16592 while (i > 0)
16593 {
16594 /* Grab the Ith values. */
16595 i--;
16596 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
16597 : NULL_TREE;
16598 value
16599 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
16600 : NULL_TREE;
16601
16602 /* Build the list (backwards). */
16603 chain = tree_cons (purpose, value, chain);
16604 }
16605
16606 RETURN (chain);
16607 }
16608
16609 purpose = TREE_PURPOSE (t);
16610 if (purpose)
16611 purpose = RECUR (purpose);
16612 value = TREE_VALUE (t);
16613 if (value)
16614 value = RECUR (value);
16615 chain = TREE_CHAIN (t);
16616 if (chain && chain != void_type_node)
16617 chain = RECUR (chain);
16618 if (purpose == TREE_PURPOSE (t)
16619 && value == TREE_VALUE (t)
16620 && chain == TREE_CHAIN (t))
16621 RETURN (t);
16622 RETURN (tree_cons (purpose, value, chain));
16623 }
16624
16625 case COMPONENT_REF:
16626 {
16627 tree object;
16628 tree object_type;
16629 tree member;
16630 tree r;
16631
16632 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16633 args, complain, in_decl);
16634 /* Remember that there was a reference to this entity. */
16635 if (DECL_P (object)
16636 && !mark_used (object, complain) && !(complain & tf_error))
16637 RETURN (error_mark_node);
16638 object_type = TREE_TYPE (object);
16639
16640 member = TREE_OPERAND (t, 1);
16641 if (BASELINK_P (member))
16642 member = tsubst_baselink (member,
16643 non_reference (TREE_TYPE (object)),
16644 args, complain, in_decl);
16645 else
16646 member = tsubst_copy (member, args, complain, in_decl);
16647 if (member == error_mark_node)
16648 RETURN (error_mark_node);
16649
16650 if (type_dependent_expression_p (object))
16651 /* We can't do much here. */;
16652 else if (!CLASS_TYPE_P (object_type))
16653 {
16654 if (scalarish_type_p (object_type))
16655 {
16656 tree s = NULL_TREE;
16657 tree dtor = member;
16658
16659 if (TREE_CODE (dtor) == SCOPE_REF)
16660 {
16661 s = TREE_OPERAND (dtor, 0);
16662 dtor = TREE_OPERAND (dtor, 1);
16663 }
16664 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
16665 {
16666 dtor = TREE_OPERAND (dtor, 0);
16667 if (TYPE_P (dtor))
16668 RETURN (finish_pseudo_destructor_expr
16669 (object, s, dtor, input_location));
16670 }
16671 }
16672 }
16673 else if (TREE_CODE (member) == SCOPE_REF
16674 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
16675 {
16676 /* Lookup the template functions now that we know what the
16677 scope is. */
16678 tree scope = TREE_OPERAND (member, 0);
16679 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
16680 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
16681 member = lookup_qualified_name (scope, tmpl,
16682 /*is_type_p=*/false,
16683 /*complain=*/false);
16684 if (BASELINK_P (member))
16685 {
16686 BASELINK_FUNCTIONS (member)
16687 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
16688 args);
16689 member = (adjust_result_of_qualified_name_lookup
16690 (member, BINFO_TYPE (BASELINK_BINFO (member)),
16691 object_type));
16692 }
16693 else
16694 {
16695 qualified_name_lookup_error (scope, tmpl, member,
16696 input_location);
16697 RETURN (error_mark_node);
16698 }
16699 }
16700 else if (TREE_CODE (member) == SCOPE_REF
16701 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
16702 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
16703 {
16704 if (complain & tf_error)
16705 {
16706 if (TYPE_P (TREE_OPERAND (member, 0)))
16707 error ("%qT is not a class or namespace",
16708 TREE_OPERAND (member, 0));
16709 else
16710 error ("%qD is not a class or namespace",
16711 TREE_OPERAND (member, 0));
16712 }
16713 RETURN (error_mark_node);
16714 }
16715 else if (TREE_CODE (member) == FIELD_DECL)
16716 {
16717 r = finish_non_static_data_member (member, object, NULL_TREE);
16718 if (TREE_CODE (r) == COMPONENT_REF)
16719 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16720 RETURN (r);
16721 }
16722
16723 r = finish_class_member_access_expr (object, member,
16724 /*template_p=*/false,
16725 complain);
16726 if (TREE_CODE (r) == COMPONENT_REF)
16727 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16728 RETURN (r);
16729 }
16730
16731 case THROW_EXPR:
16732 RETURN (build_throw
16733 (RECUR (TREE_OPERAND (t, 0))));
16734
16735 case CONSTRUCTOR:
16736 {
16737 vec<constructor_elt, va_gc> *n;
16738 constructor_elt *ce;
16739 unsigned HOST_WIDE_INT idx;
16740 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16741 bool process_index_p;
16742 int newlen;
16743 bool need_copy_p = false;
16744 tree r;
16745
16746 if (type == error_mark_node)
16747 RETURN (error_mark_node);
16748
16749 /* digest_init will do the wrong thing if we let it. */
16750 if (type && TYPE_PTRMEMFUNC_P (type))
16751 RETURN (t);
16752
16753 /* We do not want to process the index of aggregate
16754 initializers as they are identifier nodes which will be
16755 looked up by digest_init. */
16756 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
16757
16758 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
16759 newlen = vec_safe_length (n);
16760 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
16761 {
16762 if (ce->index && process_index_p
16763 /* An identifier index is looked up in the type
16764 being initialized, not the current scope. */
16765 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
16766 ce->index = RECUR (ce->index);
16767
16768 if (PACK_EXPANSION_P (ce->value))
16769 {
16770 /* Substitute into the pack expansion. */
16771 ce->value = tsubst_pack_expansion (ce->value, args, complain,
16772 in_decl);
16773
16774 if (ce->value == error_mark_node
16775 || PACK_EXPANSION_P (ce->value))
16776 ;
16777 else if (TREE_VEC_LENGTH (ce->value) == 1)
16778 /* Just move the argument into place. */
16779 ce->value = TREE_VEC_ELT (ce->value, 0);
16780 else
16781 {
16782 /* Update the length of the final CONSTRUCTOR
16783 arguments vector, and note that we will need to
16784 copy.*/
16785 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
16786 need_copy_p = true;
16787 }
16788 }
16789 else
16790 ce->value = RECUR (ce->value);
16791 }
16792
16793 if (need_copy_p)
16794 {
16795 vec<constructor_elt, va_gc> *old_n = n;
16796
16797 vec_alloc (n, newlen);
16798 FOR_EACH_VEC_ELT (*old_n, idx, ce)
16799 {
16800 if (TREE_CODE (ce->value) == TREE_VEC)
16801 {
16802 int i, len = TREE_VEC_LENGTH (ce->value);
16803 for (i = 0; i < len; ++i)
16804 CONSTRUCTOR_APPEND_ELT (n, 0,
16805 TREE_VEC_ELT (ce->value, i));
16806 }
16807 else
16808 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
16809 }
16810 }
16811
16812 r = build_constructor (init_list_type_node, n);
16813 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
16814
16815 if (TREE_HAS_CONSTRUCTOR (t))
16816 RETURN (finish_compound_literal (type, r, complain));
16817
16818 TREE_TYPE (r) = type;
16819 RETURN (r);
16820 }
16821
16822 case TYPEID_EXPR:
16823 {
16824 tree operand_0 = TREE_OPERAND (t, 0);
16825 if (TYPE_P (operand_0))
16826 {
16827 operand_0 = tsubst (operand_0, args, complain, in_decl);
16828 RETURN (get_typeid (operand_0, complain));
16829 }
16830 else
16831 {
16832 operand_0 = RECUR (operand_0);
16833 RETURN (build_typeid (operand_0, complain));
16834 }
16835 }
16836
16837 case VAR_DECL:
16838 if (!args)
16839 RETURN (t);
16840 else if (DECL_PACK_P (t))
16841 {
16842 /* We don't build decls for an instantiation of a
16843 variadic capture proxy, we instantiate the elements
16844 when needed. */
16845 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
16846 return RECUR (DECL_VALUE_EXPR (t));
16847 }
16848 /* Fall through */
16849
16850 case PARM_DECL:
16851 {
16852 tree r = tsubst_copy (t, args, complain, in_decl);
16853 /* ??? We're doing a subset of finish_id_expression here. */
16854 if (VAR_P (r)
16855 && !processing_template_decl
16856 && !cp_unevaluated_operand
16857 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
16858 && CP_DECL_THREAD_LOCAL_P (r))
16859 {
16860 if (tree wrap = get_tls_wrapper_fn (r))
16861 /* Replace an evaluated use of the thread_local variable with
16862 a call to its wrapper. */
16863 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
16864 }
16865 else if (outer_automatic_var_p (r))
16866 {
16867 r = process_outer_var_ref (r, complain);
16868 if (is_capture_proxy (r))
16869 register_local_specialization (r, t);
16870 }
16871
16872 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
16873 /* If the original type was a reference, we'll be wrapped in
16874 the appropriate INDIRECT_REF. */
16875 r = convert_from_reference (r);
16876 RETURN (r);
16877 }
16878
16879 case VA_ARG_EXPR:
16880 {
16881 tree op0 = RECUR (TREE_OPERAND (t, 0));
16882 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16883 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
16884 }
16885
16886 case OFFSETOF_EXPR:
16887 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
16888 EXPR_LOCATION (t)));
16889
16890 case TRAIT_EXPR:
16891 {
16892 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
16893 complain, in_decl);
16894
16895 tree type2 = TRAIT_EXPR_TYPE2 (t);
16896 if (type2 && TREE_CODE (type2) == TREE_LIST)
16897 type2 = RECUR (type2);
16898 else if (type2)
16899 type2 = tsubst (type2, args, complain, in_decl);
16900
16901 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
16902 }
16903
16904 case STMT_EXPR:
16905 {
16906 tree old_stmt_expr = cur_stmt_expr;
16907 tree stmt_expr = begin_stmt_expr ();
16908
16909 cur_stmt_expr = stmt_expr;
16910 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
16911 integral_constant_expression_p);
16912 stmt_expr = finish_stmt_expr (stmt_expr, false);
16913 cur_stmt_expr = old_stmt_expr;
16914
16915 /* If the resulting list of expression statement is empty,
16916 fold it further into void_node. */
16917 if (empty_expr_stmt_p (stmt_expr))
16918 stmt_expr = void_node;
16919
16920 RETURN (stmt_expr);
16921 }
16922
16923 case LAMBDA_EXPR:
16924 {
16925 tree r = build_lambda_expr ();
16926
16927 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
16928 LAMBDA_EXPR_CLOSURE (r) = type;
16929 CLASSTYPE_LAMBDA_EXPR (type) = r;
16930
16931 LAMBDA_EXPR_LOCATION (r)
16932 = LAMBDA_EXPR_LOCATION (t);
16933 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16934 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16935 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16936 LAMBDA_EXPR_DISCRIMINATOR (r)
16937 = (LAMBDA_EXPR_DISCRIMINATOR (t));
16938 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
16939 if (!scope)
16940 /* No substitution needed. */;
16941 else if (VAR_OR_FUNCTION_DECL_P (scope))
16942 /* For a function or variable scope, we want to use tsubst so that we
16943 don't complain about referring to an auto before deduction. */
16944 scope = tsubst (scope, args, complain, in_decl);
16945 else if (TREE_CODE (scope) == PARM_DECL)
16946 {
16947 /* Look up the parameter we want directly, as tsubst_copy
16948 doesn't do what we need. */
16949 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
16950 tree parm = FUNCTION_FIRST_USER_PARM (fn);
16951 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
16952 parm = DECL_CHAIN (parm);
16953 scope = parm;
16954 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16955 if (DECL_CONTEXT (scope) == NULL_TREE)
16956 DECL_CONTEXT (scope) = fn;
16957 }
16958 else if (TREE_CODE (scope) == FIELD_DECL)
16959 /* For a field, use tsubst_copy so that we look up the existing field
16960 rather than build a new one. */
16961 scope = RECUR (scope);
16962 else
16963 gcc_unreachable ();
16964 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
16965 LAMBDA_EXPR_RETURN_TYPE (r)
16966 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
16967
16968 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16969 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16970
16971 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16972 determine_visibility (TYPE_NAME (type));
16973 /* Now that we know visibility, instantiate the type so we have a
16974 declaration of the op() for later calls to lambda_function. */
16975 complete_type (type);
16976
16977 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16978
16979 insert_pending_capture_proxies ();
16980
16981 RETURN (build_lambda_object (r));
16982 }
16983
16984 case TARGET_EXPR:
16985 /* We can get here for a constant initializer of non-dependent type.
16986 FIXME stop folding in cp_parser_initializer_clause. */
16987 {
16988 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
16989 complain);
16990 RETURN (r);
16991 }
16992
16993 case TRANSACTION_EXPR:
16994 RETURN (tsubst_expr(t, args, complain, in_decl,
16995 integral_constant_expression_p));
16996
16997 case PAREN_EXPR:
16998 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
16999
17000 case VEC_PERM_EXPR:
17001 {
17002 tree op0 = RECUR (TREE_OPERAND (t, 0));
17003 tree op1 = RECUR (TREE_OPERAND (t, 1));
17004 tree op2 = RECUR (TREE_OPERAND (t, 2));
17005 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17006 complain));
17007 }
17008
17009 case REQUIRES_EXPR:
17010 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17011
17012 default:
17013 /* Handle Objective-C++ constructs, if appropriate. */
17014 {
17015 tree subst
17016 = objcp_tsubst_copy_and_build (t, args, complain,
17017 in_decl, /*function_p=*/false);
17018 if (subst)
17019 RETURN (subst);
17020 }
17021 RETURN (tsubst_copy (t, args, complain, in_decl));
17022 }
17023
17024 #undef RECUR
17025 #undef RETURN
17026 out:
17027 input_location = loc;
17028 return retval;
17029 }
17030
17031 /* Verify that the instantiated ARGS are valid. For type arguments,
17032 make sure that the type's linkage is ok. For non-type arguments,
17033 make sure they are constants if they are integral or enumerations.
17034 Emit an error under control of COMPLAIN, and return TRUE on error. */
17035
17036 static bool
17037 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17038 {
17039 if (dependent_template_arg_p (t))
17040 return false;
17041 if (ARGUMENT_PACK_P (t))
17042 {
17043 tree vec = ARGUMENT_PACK_ARGS (t);
17044 int len = TREE_VEC_LENGTH (vec);
17045 bool result = false;
17046 int i;
17047
17048 for (i = 0; i < len; ++i)
17049 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17050 result = true;
17051 return result;
17052 }
17053 else if (TYPE_P (t))
17054 {
17055 /* [basic.link]: A name with no linkage (notably, the name
17056 of a class or enumeration declared in a local scope)
17057 shall not be used to declare an entity with linkage.
17058 This implies that names with no linkage cannot be used as
17059 template arguments
17060
17061 DR 757 relaxes this restriction for C++0x. */
17062 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17063 : no_linkage_check (t, /*relaxed_p=*/false));
17064
17065 if (nt)
17066 {
17067 /* DR 488 makes use of a type with no linkage cause
17068 type deduction to fail. */
17069 if (complain & tf_error)
17070 {
17071 if (TYPE_ANONYMOUS_P (nt))
17072 error ("%qT is/uses anonymous type", t);
17073 else
17074 error ("template argument for %qD uses local type %qT",
17075 tmpl, t);
17076 }
17077 return true;
17078 }
17079 /* In order to avoid all sorts of complications, we do not
17080 allow variably-modified types as template arguments. */
17081 else if (variably_modified_type_p (t, NULL_TREE))
17082 {
17083 if (complain & tf_error)
17084 error ("%qT is a variably modified type", t);
17085 return true;
17086 }
17087 }
17088 /* Class template and alias template arguments should be OK. */
17089 else if (DECL_TYPE_TEMPLATE_P (t))
17090 ;
17091 /* A non-type argument of integral or enumerated type must be a
17092 constant. */
17093 else if (TREE_TYPE (t)
17094 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17095 && !REFERENCE_REF_P (t)
17096 && !TREE_CONSTANT (t))
17097 {
17098 if (complain & tf_error)
17099 error ("integral expression %qE is not constant", t);
17100 return true;
17101 }
17102 return false;
17103 }
17104
17105 static bool
17106 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17107 {
17108 int ix, len = DECL_NTPARMS (tmpl);
17109 bool result = false;
17110
17111 for (ix = 0; ix != len; ix++)
17112 {
17113 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17114 result = true;
17115 }
17116 if (result && (complain & tf_error))
17117 error (" trying to instantiate %qD", tmpl);
17118 return result;
17119 }
17120
17121 /* We're out of SFINAE context now, so generate diagnostics for the access
17122 errors we saw earlier when instantiating D from TMPL and ARGS. */
17123
17124 static void
17125 recheck_decl_substitution (tree d, tree tmpl, tree args)
17126 {
17127 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17128 tree type = TREE_TYPE (pattern);
17129 location_t loc = input_location;
17130
17131 push_access_scope (d);
17132 push_deferring_access_checks (dk_no_deferred);
17133 input_location = DECL_SOURCE_LOCATION (pattern);
17134 tsubst (type, args, tf_warning_or_error, d);
17135 input_location = loc;
17136 pop_deferring_access_checks ();
17137 pop_access_scope (d);
17138 }
17139
17140 /* Instantiate the indicated variable, function, or alias template TMPL with
17141 the template arguments in TARG_PTR. */
17142
17143 static tree
17144 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17145 {
17146 tree targ_ptr = orig_args;
17147 tree fndecl;
17148 tree gen_tmpl;
17149 tree spec;
17150 bool access_ok = true;
17151
17152 if (tmpl == error_mark_node)
17153 return error_mark_node;
17154
17155 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
17156
17157 /* If this function is a clone, handle it specially. */
17158 if (DECL_CLONED_FUNCTION_P (tmpl))
17159 {
17160 tree spec;
17161 tree clone;
17162
17163 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17164 DECL_CLONED_FUNCTION. */
17165 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
17166 targ_ptr, complain);
17167 if (spec == error_mark_node)
17168 return error_mark_node;
17169
17170 /* Look for the clone. */
17171 FOR_EACH_CLONE (clone, spec)
17172 if (DECL_NAME (clone) == DECL_NAME (tmpl))
17173 return clone;
17174 /* We should always have found the clone by now. */
17175 gcc_unreachable ();
17176 return NULL_TREE;
17177 }
17178
17179 if (targ_ptr == error_mark_node)
17180 return error_mark_node;
17181
17182 /* Check to see if we already have this specialization. */
17183 gen_tmpl = most_general_template (tmpl);
17184 if (tmpl != gen_tmpl)
17185 /* The TMPL is a partial instantiation. To get a full set of
17186 arguments we must add the arguments used to perform the
17187 partial instantiation. */
17188 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
17189 targ_ptr);
17190
17191 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17192 but it doesn't seem to be on the hot path. */
17193 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
17194
17195 gcc_assert (tmpl == gen_tmpl
17196 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
17197 == spec)
17198 || fndecl == NULL_TREE);
17199
17200 if (spec != NULL_TREE)
17201 {
17202 if (FNDECL_HAS_ACCESS_ERRORS (spec))
17203 {
17204 if (complain & tf_error)
17205 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
17206 return error_mark_node;
17207 }
17208 return spec;
17209 }
17210
17211 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
17212 complain))
17213 return error_mark_node;
17214
17215 /* We are building a FUNCTION_DECL, during which the access of its
17216 parameters and return types have to be checked. However this
17217 FUNCTION_DECL which is the desired context for access checking
17218 is not built yet. We solve this chicken-and-egg problem by
17219 deferring all checks until we have the FUNCTION_DECL. */
17220 push_deferring_access_checks (dk_deferred);
17221
17222 /* Instantiation of the function happens in the context of the function
17223 template, not the context of the overload resolution we're doing. */
17224 push_to_top_level ();
17225 /* If there are dependent arguments, e.g. because we're doing partial
17226 ordering, make sure processing_template_decl stays set. */
17227 if (uses_template_parms (targ_ptr))
17228 ++processing_template_decl;
17229 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17230 {
17231 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
17232 complain, gen_tmpl, true);
17233 push_nested_class (ctx);
17234 }
17235
17236 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
17237
17238 if (VAR_P (pattern))
17239 {
17240 /* We need to determine if we're using a partial or explicit
17241 specialization now, because the type of the variable could be
17242 different. */
17243 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
17244 tree elt = most_specialized_partial_spec (tid, complain);
17245 if (elt == error_mark_node)
17246 pattern = error_mark_node;
17247 else if (elt)
17248 {
17249 tmpl = TREE_VALUE (elt);
17250 pattern = DECL_TEMPLATE_RESULT (tmpl);
17251 targ_ptr = TREE_PURPOSE (elt);
17252 }
17253 }
17254
17255 /* Substitute template parameters to obtain the specialization. */
17256 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
17257 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17258 pop_nested_class ();
17259 pop_from_top_level ();
17260
17261 if (fndecl == error_mark_node)
17262 {
17263 pop_deferring_access_checks ();
17264 return error_mark_node;
17265 }
17266
17267 /* The DECL_TI_TEMPLATE should always be the immediate parent
17268 template, not the most general template. */
17269 DECL_TI_TEMPLATE (fndecl) = tmpl;
17270 DECL_TI_ARGS (fndecl) = targ_ptr;
17271
17272 /* Now we know the specialization, compute access previously
17273 deferred. */
17274 push_access_scope (fndecl);
17275 if (!perform_deferred_access_checks (complain))
17276 access_ok = false;
17277 pop_access_scope (fndecl);
17278 pop_deferring_access_checks ();
17279
17280 /* If we've just instantiated the main entry point for a function,
17281 instantiate all the alternate entry points as well. We do this
17282 by cloning the instantiation of the main entry point, not by
17283 instantiating the template clones. */
17284 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
17285 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
17286
17287 if (!access_ok)
17288 {
17289 if (!(complain & tf_error))
17290 {
17291 /* Remember to reinstantiate when we're out of SFINAE so the user
17292 can see the errors. */
17293 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
17294 }
17295 return error_mark_node;
17296 }
17297 return fndecl;
17298 }
17299
17300 /* Wrapper for instantiate_template_1. */
17301
17302 tree
17303 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
17304 {
17305 tree ret;
17306 timevar_push (TV_TEMPLATE_INST);
17307 ret = instantiate_template_1 (tmpl, orig_args, complain);
17308 timevar_pop (TV_TEMPLATE_INST);
17309 return ret;
17310 }
17311
17312 /* Instantiate the alias template TMPL with ARGS. Also push a template
17313 instantiation level, which instantiate_template doesn't do because
17314 functions and variables have sufficient context established by the
17315 callers. */
17316
17317 static tree
17318 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
17319 {
17320 struct pending_template *old_last_pend = last_pending_template;
17321 struct tinst_level *old_error_tinst = last_error_tinst_level;
17322 if (tmpl == error_mark_node || args == error_mark_node)
17323 return error_mark_node;
17324 tree tinst = build_tree_list (tmpl, args);
17325 if (!push_tinst_level (tinst))
17326 {
17327 ggc_free (tinst);
17328 return error_mark_node;
17329 }
17330
17331 args =
17332 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
17333 args, tmpl, complain,
17334 /*require_all_args=*/true,
17335 /*use_default_args=*/true);
17336
17337 tree r = instantiate_template (tmpl, args, complain);
17338 pop_tinst_level ();
17339 /* We can't free this if a pending_template entry or last_error_tinst_level
17340 is pointing at it. */
17341 if (last_pending_template == old_last_pend
17342 && last_error_tinst_level == old_error_tinst)
17343 ggc_free (tinst);
17344
17345 return r;
17346 }
17347
17348 /* PARM is a template parameter pack for FN. Returns true iff
17349 PARM is used in a deducible way in the argument list of FN. */
17350
17351 static bool
17352 pack_deducible_p (tree parm, tree fn)
17353 {
17354 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
17355 for (; t; t = TREE_CHAIN (t))
17356 {
17357 tree type = TREE_VALUE (t);
17358 tree packs;
17359 if (!PACK_EXPANSION_P (type))
17360 continue;
17361 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
17362 packs; packs = TREE_CHAIN (packs))
17363 if (template_args_equal (TREE_VALUE (packs), parm))
17364 {
17365 /* The template parameter pack is used in a function parameter
17366 pack. If this is the end of the parameter list, the
17367 template parameter pack is deducible. */
17368 if (TREE_CHAIN (t) == void_list_node)
17369 return true;
17370 else
17371 /* Otherwise, not. Well, it could be deduced from
17372 a non-pack parameter, but doing so would end up with
17373 a deduction mismatch, so don't bother. */
17374 return false;
17375 }
17376 }
17377 /* The template parameter pack isn't used in any function parameter
17378 packs, but it might be used deeper, e.g. tuple<Args...>. */
17379 return true;
17380 }
17381
17382 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17383 NARGS elements of the arguments that are being used when calling
17384 it. TARGS is a vector into which the deduced template arguments
17385 are placed.
17386
17387 Returns either a FUNCTION_DECL for the matching specialization of FN or
17388 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17389 true, diagnostics will be printed to explain why it failed.
17390
17391 If FN is a conversion operator, or we are trying to produce a specific
17392 specialization, RETURN_TYPE is the return type desired.
17393
17394 The EXPLICIT_TARGS are explicit template arguments provided via a
17395 template-id.
17396
17397 The parameter STRICT is one of:
17398
17399 DEDUCE_CALL:
17400 We are deducing arguments for a function call, as in
17401 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17402 deducing arguments for a call to the result of a conversion
17403 function template, as in [over.call.object].
17404
17405 DEDUCE_CONV:
17406 We are deducing arguments for a conversion function, as in
17407 [temp.deduct.conv].
17408
17409 DEDUCE_EXACT:
17410 We are deducing arguments when doing an explicit instantiation
17411 as in [temp.explicit], when determining an explicit specialization
17412 as in [temp.expl.spec], or when taking the address of a function
17413 template, as in [temp.deduct.funcaddr]. */
17414
17415 tree
17416 fn_type_unification (tree fn,
17417 tree explicit_targs,
17418 tree targs,
17419 const tree *args,
17420 unsigned int nargs,
17421 tree return_type,
17422 unification_kind_t strict,
17423 int flags,
17424 bool explain_p,
17425 bool decltype_p)
17426 {
17427 tree parms;
17428 tree fntype;
17429 tree decl = NULL_TREE;
17430 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17431 bool ok;
17432 static int deduction_depth;
17433 struct pending_template *old_last_pend = last_pending_template;
17434 struct tinst_level *old_error_tinst = last_error_tinst_level;
17435 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17436 tree tinst;
17437 tree r = error_mark_node;
17438
17439 if (decltype_p)
17440 complain |= tf_decltype;
17441
17442 /* In C++0x, it's possible to have a function template whose type depends
17443 on itself recursively. This is most obvious with decltype, but can also
17444 occur with enumeration scope (c++/48969). So we need to catch infinite
17445 recursion and reject the substitution at deduction time; this function
17446 will return error_mark_node for any repeated substitution.
17447
17448 This also catches excessive recursion such as when f<N> depends on
17449 f<N-1> across all integers, and returns error_mark_node for all the
17450 substitutions back up to the initial one.
17451
17452 This is, of course, not reentrant. */
17453 if (excessive_deduction_depth)
17454 return error_mark_node;
17455 tinst = build_tree_list (fn, NULL_TREE);
17456 ++deduction_depth;
17457
17458 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
17459
17460 fntype = TREE_TYPE (fn);
17461 if (explicit_targs)
17462 {
17463 /* [temp.deduct]
17464
17465 The specified template arguments must match the template
17466 parameters in kind (i.e., type, nontype, template), and there
17467 must not be more arguments than there are parameters;
17468 otherwise type deduction fails.
17469
17470 Nontype arguments must match the types of the corresponding
17471 nontype template parameters, or must be convertible to the
17472 types of the corresponding nontype parameters as specified in
17473 _temp.arg.nontype_, otherwise type deduction fails.
17474
17475 All references in the function type of the function template
17476 to the corresponding template parameters are replaced by the
17477 specified template argument values. If a substitution in a
17478 template parameter or in the function type of the function
17479 template results in an invalid type, type deduction fails. */
17480 int i, len = TREE_VEC_LENGTH (tparms);
17481 location_t loc = input_location;
17482 bool incomplete = false;
17483
17484 /* Adjust any explicit template arguments before entering the
17485 substitution context. */
17486 explicit_targs
17487 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
17488 complain,
17489 /*require_all_args=*/false,
17490 /*use_default_args=*/false));
17491 if (explicit_targs == error_mark_node)
17492 goto fail;
17493
17494 /* Substitute the explicit args into the function type. This is
17495 necessary so that, for instance, explicitly declared function
17496 arguments can match null pointed constants. If we were given
17497 an incomplete set of explicit args, we must not do semantic
17498 processing during substitution as we could create partial
17499 instantiations. */
17500 for (i = 0; i < len; i++)
17501 {
17502 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17503 bool parameter_pack = false;
17504 tree targ = TREE_VEC_ELT (explicit_targs, i);
17505
17506 /* Dig out the actual parm. */
17507 if (TREE_CODE (parm) == TYPE_DECL
17508 || TREE_CODE (parm) == TEMPLATE_DECL)
17509 {
17510 parm = TREE_TYPE (parm);
17511 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
17512 }
17513 else if (TREE_CODE (parm) == PARM_DECL)
17514 {
17515 parm = DECL_INITIAL (parm);
17516 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
17517 }
17518
17519 if (!parameter_pack && targ == NULL_TREE)
17520 /* No explicit argument for this template parameter. */
17521 incomplete = true;
17522
17523 if (parameter_pack && pack_deducible_p (parm, fn))
17524 {
17525 /* Mark the argument pack as "incomplete". We could
17526 still deduce more arguments during unification.
17527 We remove this mark in type_unification_real. */
17528 if (targ)
17529 {
17530 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
17531 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
17532 = ARGUMENT_PACK_ARGS (targ);
17533 }
17534
17535 /* We have some incomplete argument packs. */
17536 incomplete = true;
17537 }
17538 }
17539
17540 TREE_VALUE (tinst) = explicit_targs;
17541 if (!push_tinst_level (tinst))
17542 {
17543 excessive_deduction_depth = true;
17544 goto fail;
17545 }
17546 processing_template_decl += incomplete;
17547 input_location = DECL_SOURCE_LOCATION (fn);
17548 /* Ignore any access checks; we'll see them again in
17549 instantiate_template and they might have the wrong
17550 access path at this point. */
17551 push_deferring_access_checks (dk_deferred);
17552 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
17553 complain | tf_partial, NULL_TREE);
17554 pop_deferring_access_checks ();
17555 input_location = loc;
17556 processing_template_decl -= incomplete;
17557 pop_tinst_level ();
17558
17559 if (fntype == error_mark_node)
17560 goto fail;
17561
17562 /* Place the explicitly specified arguments in TARGS. */
17563 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
17564 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
17565 }
17566
17567 /* Never do unification on the 'this' parameter. */
17568 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
17569
17570 if (return_type && strict == DEDUCE_CALL)
17571 {
17572 /* We're deducing for a call to the result of a template conversion
17573 function. The parms we really want are in return_type. */
17574 if (POINTER_TYPE_P (return_type))
17575 return_type = TREE_TYPE (return_type);
17576 parms = TYPE_ARG_TYPES (return_type);
17577 }
17578 else if (return_type)
17579 {
17580 tree *new_args;
17581
17582 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
17583 new_args = XALLOCAVEC (tree, nargs + 1);
17584 new_args[0] = return_type;
17585 memcpy (new_args + 1, args, nargs * sizeof (tree));
17586 args = new_args;
17587 ++nargs;
17588 }
17589
17590 /* We allow incomplete unification without an error message here
17591 because the standard doesn't seem to explicitly prohibit it. Our
17592 callers must be ready to deal with unification failures in any
17593 event. */
17594
17595 TREE_VALUE (tinst) = targs;
17596 /* If we aren't explaining yet, push tinst context so we can see where
17597 any errors (e.g. from class instantiations triggered by instantiation
17598 of default template arguments) come from. If we are explaining, this
17599 context is redundant. */
17600 if (!explain_p && !push_tinst_level (tinst))
17601 {
17602 excessive_deduction_depth = true;
17603 goto fail;
17604 }
17605
17606 /* type_unification_real will pass back any access checks from default
17607 template argument substitution. */
17608 vec<deferred_access_check, va_gc> *checks;
17609 checks = NULL;
17610
17611 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17612 targs, parms, args, nargs, /*subr=*/0,
17613 strict, flags, &checks, explain_p);
17614 if (!explain_p)
17615 pop_tinst_level ();
17616 if (!ok)
17617 goto fail;
17618
17619 /* Now that we have bindings for all of the template arguments,
17620 ensure that the arguments deduced for the template template
17621 parameters have compatible template parameter lists. We cannot
17622 check this property before we have deduced all template
17623 arguments, because the template parameter types of a template
17624 template parameter might depend on prior template parameters
17625 deduced after the template template parameter. The following
17626 ill-formed example illustrates this issue:
17627
17628 template<typename T, template<T> class C> void f(C<5>, T);
17629
17630 template<int N> struct X {};
17631
17632 void g() {
17633 f(X<5>(), 5l); // error: template argument deduction fails
17634 }
17635
17636 The template parameter list of 'C' depends on the template type
17637 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17638 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17639 time that we deduce 'C'. */
17640 if (!template_template_parm_bindings_ok_p
17641 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
17642 {
17643 unify_inconsistent_template_template_parameters (explain_p);
17644 goto fail;
17645 }
17646
17647 /* All is well so far. Now, check:
17648
17649 [temp.deduct]
17650
17651 When all template arguments have been deduced, all uses of
17652 template parameters in nondeduced contexts are replaced with
17653 the corresponding deduced argument values. If the
17654 substitution results in an invalid type, as described above,
17655 type deduction fails. */
17656 TREE_VALUE (tinst) = targs;
17657 if (!push_tinst_level (tinst))
17658 {
17659 excessive_deduction_depth = true;
17660 goto fail;
17661 }
17662
17663 /* Also collect access checks from the instantiation. */
17664 reopen_deferring_access_checks (checks);
17665
17666 decl = instantiate_template (fn, targs, complain);
17667
17668 checks = get_deferred_access_checks ();
17669 pop_deferring_access_checks ();
17670
17671 pop_tinst_level ();
17672
17673 if (decl == error_mark_node)
17674 goto fail;
17675
17676 /* Now perform any access checks encountered during substitution. */
17677 push_access_scope (decl);
17678 ok = perform_access_checks (checks, complain);
17679 pop_access_scope (decl);
17680 if (!ok)
17681 goto fail;
17682
17683 /* If we're looking for an exact match, check that what we got
17684 is indeed an exact match. It might not be if some template
17685 parameters are used in non-deduced contexts. But don't check
17686 for an exact match if we have dependent template arguments;
17687 in that case we're doing partial ordering, and we already know
17688 that we have two candidates that will provide the actual type. */
17689 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
17690 {
17691 tree substed = TREE_TYPE (decl);
17692 unsigned int i;
17693
17694 tree sarg
17695 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
17696 if (return_type)
17697 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
17698 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
17699 if (!same_type_p (args[i], TREE_VALUE (sarg)))
17700 {
17701 unify_type_mismatch (explain_p, args[i],
17702 TREE_VALUE (sarg));
17703 goto fail;
17704 }
17705 }
17706
17707 r = decl;
17708
17709 fail:
17710 --deduction_depth;
17711 if (excessive_deduction_depth)
17712 {
17713 if (deduction_depth == 0)
17714 /* Reset once we're all the way out. */
17715 excessive_deduction_depth = false;
17716 }
17717
17718 /* We can't free this if a pending_template entry or last_error_tinst_level
17719 is pointing at it. */
17720 if (last_pending_template == old_last_pend
17721 && last_error_tinst_level == old_error_tinst)
17722 ggc_free (tinst);
17723
17724 return r;
17725 }
17726
17727 /* Adjust types before performing type deduction, as described in
17728 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17729 sections are symmetric. PARM is the type of a function parameter
17730 or the return type of the conversion function. ARG is the type of
17731 the argument passed to the call, or the type of the value
17732 initialized with the result of the conversion function.
17733 ARG_EXPR is the original argument expression, which may be null. */
17734
17735 static int
17736 maybe_adjust_types_for_deduction (unification_kind_t strict,
17737 tree* parm,
17738 tree* arg,
17739 tree arg_expr)
17740 {
17741 int result = 0;
17742
17743 switch (strict)
17744 {
17745 case DEDUCE_CALL:
17746 break;
17747
17748 case DEDUCE_CONV:
17749 /* Swap PARM and ARG throughout the remainder of this
17750 function; the handling is precisely symmetric since PARM
17751 will initialize ARG rather than vice versa. */
17752 std::swap (parm, arg);
17753 break;
17754
17755 case DEDUCE_EXACT:
17756 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17757 too, but here handle it by stripping the reference from PARM
17758 rather than by adding it to ARG. */
17759 if (TREE_CODE (*parm) == REFERENCE_TYPE
17760 && TYPE_REF_IS_RVALUE (*parm)
17761 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17762 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17763 && TREE_CODE (*arg) == REFERENCE_TYPE
17764 && !TYPE_REF_IS_RVALUE (*arg))
17765 *parm = TREE_TYPE (*parm);
17766 /* Nothing else to do in this case. */
17767 return 0;
17768
17769 default:
17770 gcc_unreachable ();
17771 }
17772
17773 if (TREE_CODE (*parm) != REFERENCE_TYPE)
17774 {
17775 /* [temp.deduct.call]
17776
17777 If P is not a reference type:
17778
17779 --If A is an array type, the pointer type produced by the
17780 array-to-pointer standard conversion (_conv.array_) is
17781 used in place of A for type deduction; otherwise,
17782
17783 --If A is a function type, the pointer type produced by
17784 the function-to-pointer standard conversion
17785 (_conv.func_) is used in place of A for type deduction;
17786 otherwise,
17787
17788 --If A is a cv-qualified type, the top level
17789 cv-qualifiers of A's type are ignored for type
17790 deduction. */
17791 if (TREE_CODE (*arg) == ARRAY_TYPE)
17792 *arg = build_pointer_type (TREE_TYPE (*arg));
17793 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
17794 *arg = build_pointer_type (*arg);
17795 else
17796 *arg = TYPE_MAIN_VARIANT (*arg);
17797 }
17798
17799 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17800 of the form T&&, where T is a template parameter, and the argument
17801 is an lvalue, T is deduced as A& */
17802 if (TREE_CODE (*parm) == REFERENCE_TYPE
17803 && TYPE_REF_IS_RVALUE (*parm)
17804 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17805 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17806 && (arg_expr ? real_lvalue_p (arg_expr)
17807 /* try_one_overload doesn't provide an arg_expr, but
17808 functions are always lvalues. */
17809 : TREE_CODE (*arg) == FUNCTION_TYPE))
17810 *arg = build_reference_type (*arg);
17811
17812 /* [temp.deduct.call]
17813
17814 If P is a cv-qualified type, the top level cv-qualifiers
17815 of P's type are ignored for type deduction. If P is a
17816 reference type, the type referred to by P is used for
17817 type deduction. */
17818 *parm = TYPE_MAIN_VARIANT (*parm);
17819 if (TREE_CODE (*parm) == REFERENCE_TYPE)
17820 {
17821 *parm = TREE_TYPE (*parm);
17822 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17823 }
17824
17825 /* DR 322. For conversion deduction, remove a reference type on parm
17826 too (which has been swapped into ARG). */
17827 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
17828 *arg = TREE_TYPE (*arg);
17829
17830 return result;
17831 }
17832
17833 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17834 template which does contain any deducible template parameters; check if
17835 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17836 unify_one_argument. */
17837
17838 static int
17839 check_non_deducible_conversion (tree parm, tree arg, int strict,
17840 int flags, bool explain_p)
17841 {
17842 tree type;
17843
17844 if (!TYPE_P (arg))
17845 type = TREE_TYPE (arg);
17846 else
17847 type = arg;
17848
17849 if (same_type_p (parm, type))
17850 return unify_success (explain_p);
17851
17852 if (strict == DEDUCE_CONV)
17853 {
17854 if (can_convert_arg (type, parm, NULL_TREE, flags,
17855 explain_p ? tf_warning_or_error : tf_none))
17856 return unify_success (explain_p);
17857 }
17858 else if (strict != DEDUCE_EXACT)
17859 {
17860 if (can_convert_arg (parm, type,
17861 TYPE_P (arg) ? NULL_TREE : arg,
17862 flags, explain_p ? tf_warning_or_error : tf_none))
17863 return unify_success (explain_p);
17864 }
17865
17866 if (strict == DEDUCE_EXACT)
17867 return unify_type_mismatch (explain_p, parm, arg);
17868 else
17869 return unify_arg_conversion (explain_p, parm, type, arg);
17870 }
17871
17872 static bool uses_deducible_template_parms (tree type);
17873
17874 /* Returns true iff the expression EXPR is one from which a template
17875 argument can be deduced. In other words, if it's an undecorated
17876 use of a template non-type parameter. */
17877
17878 static bool
17879 deducible_expression (tree expr)
17880 {
17881 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
17882 }
17883
17884 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17885 deducible way; that is, if it has a max value of <PARM> - 1. */
17886
17887 static bool
17888 deducible_array_bound (tree domain)
17889 {
17890 if (domain == NULL_TREE)
17891 return false;
17892
17893 tree max = TYPE_MAX_VALUE (domain);
17894 if (TREE_CODE (max) != MINUS_EXPR)
17895 return false;
17896
17897 return deducible_expression (TREE_OPERAND (max, 0));
17898 }
17899
17900 /* Returns true iff the template arguments ARGS use a template parameter
17901 in a deducible way. */
17902
17903 static bool
17904 deducible_template_args (tree args)
17905 {
17906 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
17907 {
17908 bool deducible;
17909 tree elt = TREE_VEC_ELT (args, i);
17910 if (ARGUMENT_PACK_P (elt))
17911 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
17912 else
17913 {
17914 if (PACK_EXPANSION_P (elt))
17915 elt = PACK_EXPANSION_PATTERN (elt);
17916 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
17917 deducible = true;
17918 else if (TYPE_P (elt))
17919 deducible = uses_deducible_template_parms (elt);
17920 else
17921 deducible = deducible_expression (elt);
17922 }
17923 if (deducible)
17924 return true;
17925 }
17926 return false;
17927 }
17928
17929 /* Returns true iff TYPE contains any deducible references to template
17930 parameters, as per 14.8.2.5. */
17931
17932 static bool
17933 uses_deducible_template_parms (tree type)
17934 {
17935 if (PACK_EXPANSION_P (type))
17936 type = PACK_EXPANSION_PATTERN (type);
17937
17938 /* T
17939 cv-list T
17940 TT<T>
17941 TT<i>
17942 TT<> */
17943 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17944 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17945 return true;
17946
17947 /* T*
17948 T&
17949 T&& */
17950 if (POINTER_TYPE_P (type))
17951 return uses_deducible_template_parms (TREE_TYPE (type));
17952
17953 /* T[integer-constant ]
17954 type [i] */
17955 if (TREE_CODE (type) == ARRAY_TYPE)
17956 return (uses_deducible_template_parms (TREE_TYPE (type))
17957 || deducible_array_bound (TYPE_DOMAIN (type)));
17958
17959 /* T type ::*
17960 type T::*
17961 T T::*
17962 T (type ::*)()
17963 type (T::*)()
17964 type (type ::*)(T)
17965 type (T::*)(T)
17966 T (type ::*)(T)
17967 T (T::*)()
17968 T (T::*)(T) */
17969 if (TYPE_PTRMEM_P (type))
17970 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
17971 || (uses_deducible_template_parms
17972 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
17973
17974 /* template-name <T> (where template-name refers to a class template)
17975 template-name <i> (where template-name refers to a class template) */
17976 if (CLASS_TYPE_P (type)
17977 && CLASSTYPE_TEMPLATE_INFO (type)
17978 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
17979 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
17980 (CLASSTYPE_TI_ARGS (type)));
17981
17982 /* type (T)
17983 T()
17984 T(T) */
17985 if (TREE_CODE (type) == FUNCTION_TYPE
17986 || TREE_CODE (type) == METHOD_TYPE)
17987 {
17988 if (uses_deducible_template_parms (TREE_TYPE (type)))
17989 return true;
17990 tree parm = TYPE_ARG_TYPES (type);
17991 if (TREE_CODE (type) == METHOD_TYPE)
17992 parm = TREE_CHAIN (parm);
17993 for (; parm; parm = TREE_CHAIN (parm))
17994 if (uses_deducible_template_parms (TREE_VALUE (parm)))
17995 return true;
17996 }
17997
17998 return false;
17999 }
18000
18001 /* Subroutine of type_unification_real and unify_pack_expansion to
18002 handle unification of a single P/A pair. Parameters are as
18003 for those functions. */
18004
18005 static int
18006 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18007 int subr, unification_kind_t strict,
18008 bool explain_p)
18009 {
18010 tree arg_expr = NULL_TREE;
18011 int arg_strict;
18012
18013 if (arg == error_mark_node || parm == error_mark_node)
18014 return unify_invalid (explain_p);
18015 if (arg == unknown_type_node)
18016 /* We can't deduce anything from this, but we might get all the
18017 template args from other function args. */
18018 return unify_success (explain_p);
18019
18020 /* Implicit conversions (Clause 4) will be performed on a function
18021 argument to convert it to the type of the corresponding function
18022 parameter if the parameter type contains no template-parameters that
18023 participate in template argument deduction. */
18024 if (strict != DEDUCE_EXACT
18025 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18026 /* For function parameters with no deducible template parameters,
18027 just return. We'll check non-dependent conversions later. */
18028 return unify_success (explain_p);
18029
18030 switch (strict)
18031 {
18032 case DEDUCE_CALL:
18033 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18034 | UNIFY_ALLOW_MORE_CV_QUAL
18035 | UNIFY_ALLOW_DERIVED);
18036 break;
18037
18038 case DEDUCE_CONV:
18039 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18040 break;
18041
18042 case DEDUCE_EXACT:
18043 arg_strict = UNIFY_ALLOW_NONE;
18044 break;
18045
18046 default:
18047 gcc_unreachable ();
18048 }
18049
18050 /* We only do these transformations if this is the top-level
18051 parameter_type_list in a call or declaration matching; in other
18052 situations (nested function declarators, template argument lists) we
18053 won't be comparing a type to an expression, and we don't do any type
18054 adjustments. */
18055 if (!subr)
18056 {
18057 if (!TYPE_P (arg))
18058 {
18059 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18060 if (type_unknown_p (arg))
18061 {
18062 /* [temp.deduct.type] A template-argument can be
18063 deduced from a pointer to function or pointer
18064 to member function argument if the set of
18065 overloaded functions does not contain function
18066 templates and at most one of a set of
18067 overloaded functions provides a unique
18068 match. */
18069
18070 if (resolve_overloaded_unification
18071 (tparms, targs, parm, arg, strict,
18072 arg_strict, explain_p))
18073 return unify_success (explain_p);
18074 return unify_overload_resolution_failure (explain_p, arg);
18075 }
18076
18077 arg_expr = arg;
18078 arg = unlowered_expr_type (arg);
18079 if (arg == error_mark_node)
18080 return unify_invalid (explain_p);
18081 }
18082
18083 arg_strict |=
18084 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18085 }
18086 else
18087 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18088 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18089 return unify_template_argument_mismatch (explain_p, parm, arg);
18090
18091 /* For deduction from an init-list we need the actual list. */
18092 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18093 arg = arg_expr;
18094 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18095 }
18096
18097 /* Most parms like fn_type_unification.
18098
18099 If SUBR is 1, we're being called recursively (to unify the
18100 arguments of a function or method parameter of a function
18101 template).
18102
18103 CHECKS is a pointer to a vector of access checks encountered while
18104 substituting default template arguments. */
18105
18106 static int
18107 type_unification_real (tree tparms,
18108 tree targs,
18109 tree xparms,
18110 const tree *xargs,
18111 unsigned int xnargs,
18112 int subr,
18113 unification_kind_t strict,
18114 int flags,
18115 vec<deferred_access_check, va_gc> **checks,
18116 bool explain_p)
18117 {
18118 tree parm, arg;
18119 int i;
18120 int ntparms = TREE_VEC_LENGTH (tparms);
18121 int saw_undeduced = 0;
18122 tree parms;
18123 const tree *args;
18124 unsigned int nargs;
18125 unsigned int ia;
18126
18127 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
18128 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
18129 gcc_assert (ntparms > 0);
18130
18131 /* Reset the number of non-defaulted template arguments contained
18132 in TARGS. */
18133 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
18134
18135 again:
18136 parms = xparms;
18137 args = xargs;
18138 nargs = xnargs;
18139
18140 ia = 0;
18141 while (parms && parms != void_list_node
18142 && ia < nargs)
18143 {
18144 parm = TREE_VALUE (parms);
18145
18146 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18147 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
18148 /* For a function parameter pack that occurs at the end of the
18149 parameter-declaration-list, the type A of each remaining
18150 argument of the call is compared with the type P of the
18151 declarator-id of the function parameter pack. */
18152 break;
18153
18154 parms = TREE_CHAIN (parms);
18155
18156 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18157 /* For a function parameter pack that does not occur at the
18158 end of the parameter-declaration-list, the type of the
18159 parameter pack is a non-deduced context. */
18160 continue;
18161
18162 arg = args[ia];
18163 ++ia;
18164
18165 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
18166 explain_p))
18167 return 1;
18168 }
18169
18170 if (parms
18171 && parms != void_list_node
18172 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
18173 {
18174 /* Unify the remaining arguments with the pack expansion type. */
18175 tree argvec;
18176 tree parmvec = make_tree_vec (1);
18177
18178 /* Allocate a TREE_VEC and copy in all of the arguments */
18179 argvec = make_tree_vec (nargs - ia);
18180 for (i = 0; ia < nargs; ++ia, ++i)
18181 TREE_VEC_ELT (argvec, i) = args[ia];
18182
18183 /* Copy the parameter into parmvec. */
18184 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
18185 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
18186 /*subr=*/subr, explain_p))
18187 return 1;
18188
18189 /* Advance to the end of the list of parameters. */
18190 parms = TREE_CHAIN (parms);
18191 }
18192
18193 /* Fail if we've reached the end of the parm list, and more args
18194 are present, and the parm list isn't variadic. */
18195 if (ia < nargs && parms == void_list_node)
18196 return unify_too_many_arguments (explain_p, nargs, ia);
18197 /* Fail if parms are left and they don't have default values and
18198 they aren't all deduced as empty packs (c++/57397). This is
18199 consistent with sufficient_parms_p. */
18200 if (parms && parms != void_list_node
18201 && TREE_PURPOSE (parms) == NULL_TREE)
18202 {
18203 unsigned int count = nargs;
18204 tree p = parms;
18205 bool type_pack_p;
18206 do
18207 {
18208 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
18209 if (!type_pack_p)
18210 count++;
18211 p = TREE_CHAIN (p);
18212 }
18213 while (p && p != void_list_node);
18214 if (count != nargs)
18215 return unify_too_few_arguments (explain_p, ia, count,
18216 type_pack_p);
18217 }
18218
18219 if (!subr)
18220 {
18221 tsubst_flags_t complain = (explain_p
18222 ? tf_warning_or_error
18223 : tf_none);
18224
18225 for (i = 0; i < ntparms; i++)
18226 {
18227 tree targ = TREE_VEC_ELT (targs, i);
18228 tree tparm = TREE_VEC_ELT (tparms, i);
18229
18230 /* Clear the "incomplete" flags on all argument packs now so that
18231 substituting them into later default arguments works. */
18232 if (targ && ARGUMENT_PACK_P (targ))
18233 {
18234 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
18235 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
18236 }
18237
18238 if (targ || tparm == error_mark_node)
18239 continue;
18240 tparm = TREE_VALUE (tparm);
18241
18242 /* If this is an undeduced nontype parameter that depends on
18243 a type parameter, try another pass; its type may have been
18244 deduced from a later argument than the one from which
18245 this parameter can be deduced. */
18246 if (TREE_CODE (tparm) == PARM_DECL
18247 && uses_template_parms (TREE_TYPE (tparm))
18248 && saw_undeduced < 2)
18249 {
18250 saw_undeduced = 1;
18251 continue;
18252 }
18253
18254 /* Core issue #226 (C++0x) [temp.deduct]:
18255
18256 If a template argument has not been deduced, its
18257 default template argument, if any, is used.
18258
18259 When we are in C++98 mode, TREE_PURPOSE will either
18260 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18261 to explicitly check cxx_dialect here. */
18262 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
18263 /* OK, there is a default argument. Wait until after the
18264 conversion check to do substitution. */
18265 continue;
18266
18267 /* If the type parameter is a parameter pack, then it will
18268 be deduced to an empty parameter pack. */
18269 if (template_parameter_pack_p (tparm))
18270 {
18271 tree arg;
18272
18273 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
18274 {
18275 arg = make_node (NONTYPE_ARGUMENT_PACK);
18276 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
18277 TREE_CONSTANT (arg) = 1;
18278 }
18279 else
18280 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
18281
18282 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
18283
18284 TREE_VEC_ELT (targs, i) = arg;
18285 continue;
18286 }
18287
18288 return unify_parameter_deduction_failure (explain_p, tparm);
18289 }
18290
18291 /* DR 1391: All parameters have args, now check non-dependent parms for
18292 convertibility. */
18293 if (saw_undeduced < 2)
18294 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
18295 parms && parms != void_list_node && ia < nargs; )
18296 {
18297 parm = TREE_VALUE (parms);
18298
18299 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18300 && (!TREE_CHAIN (parms)
18301 || TREE_CHAIN (parms) == void_list_node))
18302 /* For a function parameter pack that occurs at the end of the
18303 parameter-declaration-list, the type A of each remaining
18304 argument of the call is compared with the type P of the
18305 declarator-id of the function parameter pack. */
18306 break;
18307
18308 parms = TREE_CHAIN (parms);
18309
18310 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18311 /* For a function parameter pack that does not occur at the
18312 end of the parameter-declaration-list, the type of the
18313 parameter pack is a non-deduced context. */
18314 continue;
18315
18316 arg = args[ia];
18317 ++ia;
18318
18319 if (uses_template_parms (parm))
18320 continue;
18321 if (check_non_deducible_conversion (parm, arg, strict, flags,
18322 explain_p))
18323 return 1;
18324 }
18325
18326 /* Now substitute into the default template arguments. */
18327 for (i = 0; i < ntparms; i++)
18328 {
18329 tree targ = TREE_VEC_ELT (targs, i);
18330 tree tparm = TREE_VEC_ELT (tparms, i);
18331
18332 if (targ || tparm == error_mark_node)
18333 continue;
18334 tree parm = TREE_VALUE (tparm);
18335
18336 if (TREE_CODE (parm) == PARM_DECL
18337 && uses_template_parms (TREE_TYPE (parm))
18338 && saw_undeduced < 2)
18339 continue;
18340
18341 tree arg = TREE_PURPOSE (tparm);
18342 reopen_deferring_access_checks (*checks);
18343 location_t save_loc = input_location;
18344 if (DECL_P (parm))
18345 input_location = DECL_SOURCE_LOCATION (parm);
18346 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
18347 arg = convert_template_argument (parm, arg, targs, complain,
18348 i, NULL_TREE);
18349 input_location = save_loc;
18350 *checks = get_deferred_access_checks ();
18351 pop_deferring_access_checks ();
18352 if (arg == error_mark_node)
18353 return 1;
18354 else
18355 {
18356 TREE_VEC_ELT (targs, i) = arg;
18357 /* The position of the first default template argument,
18358 is also the number of non-defaulted arguments in TARGS.
18359 Record that. */
18360 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18361 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
18362 continue;
18363 }
18364 }
18365
18366 if (saw_undeduced++ == 1)
18367 goto again;
18368 }
18369
18370 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18371 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
18372
18373 return unify_success (explain_p);
18374 }
18375
18376 /* Subroutine of type_unification_real. Args are like the variables
18377 at the call site. ARG is an overloaded function (or template-id);
18378 we try deducing template args from each of the overloads, and if
18379 only one succeeds, we go with that. Modifies TARGS and returns
18380 true on success. */
18381
18382 static bool
18383 resolve_overloaded_unification (tree tparms,
18384 tree targs,
18385 tree parm,
18386 tree arg,
18387 unification_kind_t strict,
18388 int sub_strict,
18389 bool explain_p)
18390 {
18391 tree tempargs = copy_node (targs);
18392 int good = 0;
18393 tree goodfn = NULL_TREE;
18394 bool addr_p;
18395
18396 if (TREE_CODE (arg) == ADDR_EXPR)
18397 {
18398 arg = TREE_OPERAND (arg, 0);
18399 addr_p = true;
18400 }
18401 else
18402 addr_p = false;
18403
18404 if (TREE_CODE (arg) == COMPONENT_REF)
18405 /* Handle `&x' where `x' is some static or non-static member
18406 function name. */
18407 arg = TREE_OPERAND (arg, 1);
18408
18409 if (TREE_CODE (arg) == OFFSET_REF)
18410 arg = TREE_OPERAND (arg, 1);
18411
18412 /* Strip baselink information. */
18413 if (BASELINK_P (arg))
18414 arg = BASELINK_FUNCTIONS (arg);
18415
18416 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
18417 {
18418 /* If we got some explicit template args, we need to plug them into
18419 the affected templates before we try to unify, in case the
18420 explicit args will completely resolve the templates in question. */
18421
18422 int ok = 0;
18423 tree expl_subargs = TREE_OPERAND (arg, 1);
18424 arg = TREE_OPERAND (arg, 0);
18425
18426 for (; arg; arg = OVL_NEXT (arg))
18427 {
18428 tree fn = OVL_CURRENT (arg);
18429 tree subargs, elem;
18430
18431 if (TREE_CODE (fn) != TEMPLATE_DECL)
18432 continue;
18433
18434 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18435 expl_subargs, NULL_TREE, tf_none,
18436 /*require_all_args=*/true,
18437 /*use_default_args=*/true);
18438 if (subargs != error_mark_node
18439 && !any_dependent_template_arguments_p (subargs))
18440 {
18441 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
18442 if (try_one_overload (tparms, targs, tempargs, parm,
18443 elem, strict, sub_strict, addr_p, explain_p)
18444 && (!goodfn || !same_type_p (goodfn, elem)))
18445 {
18446 goodfn = elem;
18447 ++good;
18448 }
18449 }
18450 else if (subargs)
18451 ++ok;
18452 }
18453 /* If no templates (or more than one) are fully resolved by the
18454 explicit arguments, this template-id is a non-deduced context; it
18455 could still be OK if we deduce all template arguments for the
18456 enclosing call through other arguments. */
18457 if (good != 1)
18458 good = ok;
18459 }
18460 else if (TREE_CODE (arg) != OVERLOAD
18461 && TREE_CODE (arg) != FUNCTION_DECL)
18462 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18463 -- but the deduction does not succeed because the expression is
18464 not just the function on its own. */
18465 return false;
18466 else
18467 for (; arg; arg = OVL_NEXT (arg))
18468 if (try_one_overload (tparms, targs, tempargs, parm,
18469 TREE_TYPE (OVL_CURRENT (arg)),
18470 strict, sub_strict, addr_p, explain_p)
18471 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
18472 {
18473 goodfn = OVL_CURRENT (arg);
18474 ++good;
18475 }
18476
18477 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18478 to function or pointer to member function argument if the set of
18479 overloaded functions does not contain function templates and at most
18480 one of a set of overloaded functions provides a unique match.
18481
18482 So if we found multiple possibilities, we return success but don't
18483 deduce anything. */
18484
18485 if (good == 1)
18486 {
18487 int i = TREE_VEC_LENGTH (targs);
18488 for (; i--; )
18489 if (TREE_VEC_ELT (tempargs, i))
18490 {
18491 tree old = TREE_VEC_ELT (targs, i);
18492 tree new_ = TREE_VEC_ELT (tempargs, i);
18493 if (new_ && old && ARGUMENT_PACK_P (old)
18494 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
18495 /* Don't forget explicit template arguments in a pack. */
18496 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
18497 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
18498 TREE_VEC_ELT (targs, i) = new_;
18499 }
18500 }
18501 if (good)
18502 return true;
18503
18504 return false;
18505 }
18506
18507 /* Core DR 115: In contexts where deduction is done and fails, or in
18508 contexts where deduction is not done, if a template argument list is
18509 specified and it, along with any default template arguments, identifies
18510 a single function template specialization, then the template-id is an
18511 lvalue for the function template specialization. */
18512
18513 tree
18514 resolve_nondeduced_context (tree orig_expr)
18515 {
18516 tree expr, offset, baselink;
18517 bool addr;
18518
18519 if (!type_unknown_p (orig_expr))
18520 return orig_expr;
18521
18522 expr = orig_expr;
18523 addr = false;
18524 offset = NULL_TREE;
18525 baselink = NULL_TREE;
18526
18527 if (TREE_CODE (expr) == ADDR_EXPR)
18528 {
18529 expr = TREE_OPERAND (expr, 0);
18530 addr = true;
18531 }
18532 if (TREE_CODE (expr) == OFFSET_REF)
18533 {
18534 offset = expr;
18535 expr = TREE_OPERAND (expr, 1);
18536 }
18537 if (BASELINK_P (expr))
18538 {
18539 baselink = expr;
18540 expr = BASELINK_FUNCTIONS (expr);
18541 }
18542
18543 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
18544 {
18545 int good = 0;
18546 tree goodfn = NULL_TREE;
18547
18548 /* If we got some explicit template args, we need to plug them into
18549 the affected templates before we try to unify, in case the
18550 explicit args will completely resolve the templates in question. */
18551
18552 tree expl_subargs = TREE_OPERAND (expr, 1);
18553 tree arg = TREE_OPERAND (expr, 0);
18554 tree badfn = NULL_TREE;
18555 tree badargs = NULL_TREE;
18556
18557 for (; arg; arg = OVL_NEXT (arg))
18558 {
18559 tree fn = OVL_CURRENT (arg);
18560 tree subargs, elem;
18561
18562 if (TREE_CODE (fn) != TEMPLATE_DECL)
18563 continue;
18564
18565 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18566 expl_subargs, NULL_TREE, tf_none,
18567 /*require_all_args=*/true,
18568 /*use_default_args=*/true);
18569 if (subargs != error_mark_node
18570 && !any_dependent_template_arguments_p (subargs))
18571 {
18572 elem = instantiate_template (fn, subargs, tf_none);
18573 if (elem == error_mark_node)
18574 {
18575 badfn = fn;
18576 badargs = subargs;
18577 }
18578 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
18579 {
18580 goodfn = elem;
18581 ++good;
18582 }
18583 }
18584 }
18585 if (good == 1)
18586 {
18587 mark_used (goodfn);
18588 expr = goodfn;
18589 if (baselink)
18590 expr = build_baselink (BASELINK_BINFO (baselink),
18591 BASELINK_ACCESS_BINFO (baselink),
18592 expr, BASELINK_OPTYPE (baselink));
18593 if (offset)
18594 {
18595 tree base
18596 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
18597 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
18598 }
18599 if (addr)
18600 expr = cp_build_addr_expr (expr, tf_warning_or_error);
18601 return expr;
18602 }
18603 else if (good == 0 && badargs)
18604 /* There were no good options and at least one bad one, so let the
18605 user know what the problem is. */
18606 instantiate_template (badfn, badargs, tf_warning_or_error);
18607 }
18608 return orig_expr;
18609 }
18610
18611 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18612 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18613 different overloads deduce different arguments for a given parm.
18614 ADDR_P is true if the expression for which deduction is being
18615 performed was of the form "& fn" rather than simply "fn".
18616
18617 Returns 1 on success. */
18618
18619 static int
18620 try_one_overload (tree tparms,
18621 tree orig_targs,
18622 tree targs,
18623 tree parm,
18624 tree arg,
18625 unification_kind_t strict,
18626 int sub_strict,
18627 bool addr_p,
18628 bool explain_p)
18629 {
18630 int nargs;
18631 tree tempargs;
18632 int i;
18633
18634 if (arg == error_mark_node)
18635 return 0;
18636
18637 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18638 to function or pointer to member function argument if the set of
18639 overloaded functions does not contain function templates and at most
18640 one of a set of overloaded functions provides a unique match.
18641
18642 So if this is a template, just return success. */
18643
18644 if (uses_template_parms (arg))
18645 return 1;
18646
18647 if (TREE_CODE (arg) == METHOD_TYPE)
18648 arg = build_ptrmemfunc_type (build_pointer_type (arg));
18649 else if (addr_p)
18650 arg = build_pointer_type (arg);
18651
18652 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
18653
18654 /* We don't copy orig_targs for this because if we have already deduced
18655 some template args from previous args, unify would complain when we
18656 try to deduce a template parameter for the same argument, even though
18657 there isn't really a conflict. */
18658 nargs = TREE_VEC_LENGTH (targs);
18659 tempargs = make_tree_vec (nargs);
18660
18661 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
18662 return 0;
18663
18664 /* First make sure we didn't deduce anything that conflicts with
18665 explicitly specified args. */
18666 for (i = nargs; i--; )
18667 {
18668 tree elt = TREE_VEC_ELT (tempargs, i);
18669 tree oldelt = TREE_VEC_ELT (orig_targs, i);
18670
18671 if (!elt)
18672 /*NOP*/;
18673 else if (uses_template_parms (elt))
18674 /* Since we're unifying against ourselves, we will fill in
18675 template args used in the function parm list with our own
18676 template parms. Discard them. */
18677 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
18678 else if (oldelt && !template_args_equal (oldelt, elt))
18679 return 0;
18680 }
18681
18682 for (i = nargs; i--; )
18683 {
18684 tree elt = TREE_VEC_ELT (tempargs, i);
18685
18686 if (elt)
18687 TREE_VEC_ELT (targs, i) = elt;
18688 }
18689
18690 return 1;
18691 }
18692
18693 /* PARM is a template class (perhaps with unbound template
18694 parameters). ARG is a fully instantiated type. If ARG can be
18695 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
18696 TARGS are as for unify. */
18697
18698 static tree
18699 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
18700 bool explain_p)
18701 {
18702 tree copy_of_targs;
18703
18704 if (!CLASSTYPE_TEMPLATE_INFO (arg)
18705 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
18706 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
18707 return NULL_TREE;
18708
18709 /* We need to make a new template argument vector for the call to
18710 unify. If we used TARGS, we'd clutter it up with the result of
18711 the attempted unification, even if this class didn't work out.
18712 We also don't want to commit ourselves to all the unifications
18713 we've already done, since unification is supposed to be done on
18714 an argument-by-argument basis. In other words, consider the
18715 following pathological case:
18716
18717 template <int I, int J, int K>
18718 struct S {};
18719
18720 template <int I, int J>
18721 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
18722
18723 template <int I, int J, int K>
18724 void f(S<I, J, K>, S<I, I, I>);
18725
18726 void g() {
18727 S<0, 0, 0> s0;
18728 S<0, 1, 2> s2;
18729
18730 f(s0, s2);
18731 }
18732
18733 Now, by the time we consider the unification involving `s2', we
18734 already know that we must have `f<0, 0, 0>'. But, even though
18735 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
18736 because there are two ways to unify base classes of S<0, 1, 2>
18737 with S<I, I, I>. If we kept the already deduced knowledge, we
18738 would reject the possibility I=1. */
18739 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
18740
18741 /* If unification failed, we're done. */
18742 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
18743 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
18744 return NULL_TREE;
18745
18746 return arg;
18747 }
18748
18749 /* Given a template type PARM and a class type ARG, find the unique
18750 base type in ARG that is an instance of PARM. We do not examine
18751 ARG itself; only its base-classes. If there is not exactly one
18752 appropriate base class, return NULL_TREE. PARM may be the type of
18753 a partial specialization, as well as a plain template type. Used
18754 by unify. */
18755
18756 static enum template_base_result
18757 get_template_base (tree tparms, tree targs, tree parm, tree arg,
18758 bool explain_p, tree *result)
18759 {
18760 tree rval = NULL_TREE;
18761 tree binfo;
18762
18763 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
18764
18765 binfo = TYPE_BINFO (complete_type (arg));
18766 if (!binfo)
18767 {
18768 /* The type could not be completed. */
18769 *result = NULL_TREE;
18770 return tbr_incomplete_type;
18771 }
18772
18773 /* Walk in inheritance graph order. The search order is not
18774 important, and this avoids multiple walks of virtual bases. */
18775 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
18776 {
18777 tree r = try_class_unification (tparms, targs, parm,
18778 BINFO_TYPE (binfo), explain_p);
18779
18780 if (r)
18781 {
18782 /* If there is more than one satisfactory baseclass, then:
18783
18784 [temp.deduct.call]
18785
18786 If they yield more than one possible deduced A, the type
18787 deduction fails.
18788
18789 applies. */
18790 if (rval && !same_type_p (r, rval))
18791 {
18792 *result = NULL_TREE;
18793 return tbr_ambiguous_baseclass;
18794 }
18795
18796 rval = r;
18797 }
18798 }
18799
18800 *result = rval;
18801 return tbr_success;
18802 }
18803
18804 /* Returns the level of DECL, which declares a template parameter. */
18805
18806 static int
18807 template_decl_level (tree decl)
18808 {
18809 switch (TREE_CODE (decl))
18810 {
18811 case TYPE_DECL:
18812 case TEMPLATE_DECL:
18813 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
18814
18815 case PARM_DECL:
18816 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
18817
18818 default:
18819 gcc_unreachable ();
18820 }
18821 return 0;
18822 }
18823
18824 /* Decide whether ARG can be unified with PARM, considering only the
18825 cv-qualifiers of each type, given STRICT as documented for unify.
18826 Returns nonzero iff the unification is OK on that basis. */
18827
18828 static int
18829 check_cv_quals_for_unify (int strict, tree arg, tree parm)
18830 {
18831 int arg_quals = cp_type_quals (arg);
18832 int parm_quals = cp_type_quals (parm);
18833
18834 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18835 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18836 {
18837 /* Although a CVR qualifier is ignored when being applied to a
18838 substituted template parameter ([8.3.2]/1 for example), that
18839 does not allow us to unify "const T" with "int&" because both
18840 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18841 It is ok when we're allowing additional CV qualifiers
18842 at the outer level [14.8.2.1]/3,1st bullet. */
18843 if ((TREE_CODE (arg) == REFERENCE_TYPE
18844 || TREE_CODE (arg) == FUNCTION_TYPE
18845 || TREE_CODE (arg) == METHOD_TYPE)
18846 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
18847 return 0;
18848
18849 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
18850 && (parm_quals & TYPE_QUAL_RESTRICT))
18851 return 0;
18852 }
18853
18854 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18855 && (arg_quals & parm_quals) != parm_quals)
18856 return 0;
18857
18858 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
18859 && (parm_quals & arg_quals) != arg_quals)
18860 return 0;
18861
18862 return 1;
18863 }
18864
18865 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18866 void
18867 template_parm_level_and_index (tree parm, int* level, int* index)
18868 {
18869 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18870 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18871 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18872 {
18873 *index = TEMPLATE_TYPE_IDX (parm);
18874 *level = TEMPLATE_TYPE_LEVEL (parm);
18875 }
18876 else
18877 {
18878 *index = TEMPLATE_PARM_IDX (parm);
18879 *level = TEMPLATE_PARM_LEVEL (parm);
18880 }
18881 }
18882
18883 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18884 do { \
18885 if (unify (TP, TA, P, A, S, EP)) \
18886 return 1; \
18887 } while (0);
18888
18889 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18890 expansion at the end of PACKED_PARMS. Returns 0 if the type
18891 deduction succeeds, 1 otherwise. STRICT is the same as in
18892 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18893 call argument list. We'll need to adjust the arguments to make them
18894 types. SUBR tells us if this is from a recursive call to
18895 type_unification_real, or for comparing two template argument
18896 lists. */
18897
18898 static int
18899 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
18900 tree packed_args, unification_kind_t strict,
18901 bool subr, bool explain_p)
18902 {
18903 tree parm
18904 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
18905 tree pattern = PACK_EXPANSION_PATTERN (parm);
18906 tree pack, packs = NULL_TREE;
18907 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
18908
18909 packed_args = expand_template_argument_pack (packed_args);
18910
18911 int len = TREE_VEC_LENGTH (packed_args);
18912
18913 /* Determine the parameter packs we will be deducing from the
18914 pattern, and record their current deductions. */
18915 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
18916 pack; pack = TREE_CHAIN (pack))
18917 {
18918 tree parm_pack = TREE_VALUE (pack);
18919 int idx, level;
18920
18921 /* Determine the index and level of this parameter pack. */
18922 template_parm_level_and_index (parm_pack, &level, &idx);
18923
18924 /* Keep track of the parameter packs and their corresponding
18925 argument packs. */
18926 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
18927 TREE_TYPE (packs) = make_tree_vec (len - start);
18928 }
18929
18930 /* Loop through all of the arguments that have not yet been
18931 unified and unify each with the pattern. */
18932 for (i = start; i < len; i++)
18933 {
18934 tree parm;
18935 bool any_explicit = false;
18936 tree arg = TREE_VEC_ELT (packed_args, i);
18937
18938 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18939 or the element of its argument pack at the current index if
18940 this argument was explicitly specified. */
18941 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18942 {
18943 int idx, level;
18944 tree arg, pargs;
18945 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18946
18947 arg = NULL_TREE;
18948 if (TREE_VALUE (pack)
18949 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
18950 && (i - start < TREE_VEC_LENGTH (pargs)))
18951 {
18952 any_explicit = true;
18953 arg = TREE_VEC_ELT (pargs, i - start);
18954 }
18955 TMPL_ARG (targs, level, idx) = arg;
18956 }
18957
18958 /* If we had explicit template arguments, substitute them into the
18959 pattern before deduction. */
18960 if (any_explicit)
18961 {
18962 /* Some arguments might still be unspecified or dependent. */
18963 bool dependent;
18964 ++processing_template_decl;
18965 dependent = any_dependent_template_arguments_p (targs);
18966 if (!dependent)
18967 --processing_template_decl;
18968 parm = tsubst (pattern, targs,
18969 explain_p ? tf_warning_or_error : tf_none,
18970 NULL_TREE);
18971 if (dependent)
18972 --processing_template_decl;
18973 if (parm == error_mark_node)
18974 return 1;
18975 }
18976 else
18977 parm = pattern;
18978
18979 /* Unify the pattern with the current argument. */
18980 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
18981 explain_p))
18982 return 1;
18983
18984 /* For each parameter pack, collect the deduced value. */
18985 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18986 {
18987 int idx, level;
18988 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18989
18990 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
18991 TMPL_ARG (targs, level, idx);
18992 }
18993 }
18994
18995 /* Verify that the results of unification with the parameter packs
18996 produce results consistent with what we've seen before, and make
18997 the deduced argument packs available. */
18998 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18999 {
19000 tree old_pack = TREE_VALUE (pack);
19001 tree new_args = TREE_TYPE (pack);
19002 int i, len = TREE_VEC_LENGTH (new_args);
19003 int idx, level;
19004 bool nondeduced_p = false;
19005
19006 /* By default keep the original deduced argument pack.
19007 If necessary, more specific code is going to update the
19008 resulting deduced argument later down in this function. */
19009 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19010 TMPL_ARG (targs, level, idx) = old_pack;
19011
19012 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19013 actually deduce anything. */
19014 for (i = 0; i < len && !nondeduced_p; ++i)
19015 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
19016 nondeduced_p = true;
19017 if (nondeduced_p)
19018 continue;
19019
19020 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
19021 {
19022 /* If we had fewer function args than explicit template args,
19023 just use the explicits. */
19024 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19025 int explicit_len = TREE_VEC_LENGTH (explicit_args);
19026 if (len < explicit_len)
19027 new_args = explicit_args;
19028 }
19029
19030 if (!old_pack)
19031 {
19032 tree result;
19033 /* Build the deduced *_ARGUMENT_PACK. */
19034 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
19035 {
19036 result = make_node (NONTYPE_ARGUMENT_PACK);
19037 TREE_TYPE (result) =
19038 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
19039 TREE_CONSTANT (result) = 1;
19040 }
19041 else
19042 result = cxx_make_type (TYPE_ARGUMENT_PACK);
19043
19044 SET_ARGUMENT_PACK_ARGS (result, new_args);
19045
19046 /* Note the deduced argument packs for this parameter
19047 pack. */
19048 TMPL_ARG (targs, level, idx) = result;
19049 }
19050 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
19051 && (ARGUMENT_PACK_ARGS (old_pack)
19052 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
19053 {
19054 /* We only had the explicitly-provided arguments before, but
19055 now we have a complete set of arguments. */
19056 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19057
19058 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
19059 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
19060 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
19061 }
19062 else
19063 {
19064 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
19065 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
19066
19067 if (!comp_template_args (old_args, new_args,
19068 &bad_old_arg, &bad_new_arg))
19069 /* Inconsistent unification of this parameter pack. */
19070 return unify_parameter_pack_inconsistent (explain_p,
19071 bad_old_arg,
19072 bad_new_arg);
19073 }
19074 }
19075
19076 return unify_success (explain_p);
19077 }
19078
19079 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19080 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19081 parameters and return value are as for unify. */
19082
19083 static int
19084 unify_array_domain (tree tparms, tree targs,
19085 tree parm_dom, tree arg_dom,
19086 bool explain_p)
19087 {
19088 tree parm_max;
19089 tree arg_max;
19090 bool parm_cst;
19091 bool arg_cst;
19092
19093 /* Our representation of array types uses "N - 1" as the
19094 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19095 not an integer constant. We cannot unify arbitrarily
19096 complex expressions, so we eliminate the MINUS_EXPRs
19097 here. */
19098 parm_max = TYPE_MAX_VALUE (parm_dom);
19099 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
19100 if (!parm_cst)
19101 {
19102 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
19103 parm_max = TREE_OPERAND (parm_max, 0);
19104 }
19105 arg_max = TYPE_MAX_VALUE (arg_dom);
19106 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
19107 if (!arg_cst)
19108 {
19109 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19110 trying to unify the type of a variable with the type
19111 of a template parameter. For example:
19112
19113 template <unsigned int N>
19114 void f (char (&) [N]);
19115 int g();
19116 void h(int i) {
19117 char a[g(i)];
19118 f(a);
19119 }
19120
19121 Here, the type of the ARG will be "int [g(i)]", and
19122 may be a SAVE_EXPR, etc. */
19123 if (TREE_CODE (arg_max) != MINUS_EXPR)
19124 return unify_vla_arg (explain_p, arg_dom);
19125 arg_max = TREE_OPERAND (arg_max, 0);
19126 }
19127
19128 /* If only one of the bounds used a MINUS_EXPR, compensate
19129 by adding one to the other bound. */
19130 if (parm_cst && !arg_cst)
19131 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
19132 integer_type_node,
19133 parm_max,
19134 integer_one_node);
19135 else if (arg_cst && !parm_cst)
19136 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
19137 integer_type_node,
19138 arg_max,
19139 integer_one_node);
19140
19141 return unify (tparms, targs, parm_max, arg_max,
19142 UNIFY_ALLOW_INTEGER, explain_p);
19143 }
19144
19145 /* Deduce the value of template parameters. TPARMS is the (innermost)
19146 set of template parameters to a template. TARGS is the bindings
19147 for those template parameters, as determined thus far; TARGS may
19148 include template arguments for outer levels of template parameters
19149 as well. PARM is a parameter to a template function, or a
19150 subcomponent of that parameter; ARG is the corresponding argument.
19151 This function attempts to match PARM with ARG in a manner
19152 consistent with the existing assignments in TARGS. If more values
19153 are deduced, then TARGS is updated.
19154
19155 Returns 0 if the type deduction succeeds, 1 otherwise. The
19156 parameter STRICT is a bitwise or of the following flags:
19157
19158 UNIFY_ALLOW_NONE:
19159 Require an exact match between PARM and ARG.
19160 UNIFY_ALLOW_MORE_CV_QUAL:
19161 Allow the deduced ARG to be more cv-qualified (by qualification
19162 conversion) than ARG.
19163 UNIFY_ALLOW_LESS_CV_QUAL:
19164 Allow the deduced ARG to be less cv-qualified than ARG.
19165 UNIFY_ALLOW_DERIVED:
19166 Allow the deduced ARG to be a template base class of ARG,
19167 or a pointer to a template base class of the type pointed to by
19168 ARG.
19169 UNIFY_ALLOW_INTEGER:
19170 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19171 case for more information.
19172 UNIFY_ALLOW_OUTER_LEVEL:
19173 This is the outermost level of a deduction. Used to determine validity
19174 of qualification conversions. A valid qualification conversion must
19175 have const qualified pointers leading up to the inner type which
19176 requires additional CV quals, except at the outer level, where const
19177 is not required [conv.qual]. It would be normal to set this flag in
19178 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19179 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19180 This is the outermost level of a deduction, and PARM can be more CV
19181 qualified at this point.
19182 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19183 This is the outermost level of a deduction, and PARM can be less CV
19184 qualified at this point. */
19185
19186 static int
19187 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
19188 bool explain_p)
19189 {
19190 int idx;
19191 tree targ;
19192 tree tparm;
19193 int strict_in = strict;
19194
19195 /* I don't think this will do the right thing with respect to types.
19196 But the only case I've seen it in so far has been array bounds, where
19197 signedness is the only information lost, and I think that will be
19198 okay. */
19199 while (TREE_CODE (parm) == NOP_EXPR)
19200 parm = TREE_OPERAND (parm, 0);
19201
19202 if (arg == error_mark_node)
19203 return unify_invalid (explain_p);
19204 if (arg == unknown_type_node
19205 || arg == init_list_type_node)
19206 /* We can't deduce anything from this, but we might get all the
19207 template args from other function args. */
19208 return unify_success (explain_p);
19209
19210 /* If PARM uses template parameters, then we can't bail out here,
19211 even if ARG == PARM, since we won't record unifications for the
19212 template parameters. We might need them if we're trying to
19213 figure out which of two things is more specialized. */
19214 if (arg == parm && !uses_template_parms (parm))
19215 return unify_success (explain_p);
19216
19217 /* Handle init lists early, so the rest of the function can assume
19218 we're dealing with a type. */
19219 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
19220 {
19221 tree elt, elttype;
19222 unsigned i;
19223 tree orig_parm = parm;
19224
19225 /* Replace T with std::initializer_list<T> for deduction. */
19226 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19227 && flag_deduce_init_list)
19228 parm = listify (parm);
19229
19230 if (!is_std_init_list (parm)
19231 && TREE_CODE (parm) != ARRAY_TYPE)
19232 /* We can only deduce from an initializer list argument if the
19233 parameter is std::initializer_list or an array; otherwise this
19234 is a non-deduced context. */
19235 return unify_success (explain_p);
19236
19237 if (TREE_CODE (parm) == ARRAY_TYPE)
19238 elttype = TREE_TYPE (parm);
19239 else
19240 {
19241 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
19242 /* Deduction is defined in terms of a single type, so just punt
19243 on the (bizarre) std::initializer_list<T...>. */
19244 if (PACK_EXPANSION_P (elttype))
19245 return unify_success (explain_p);
19246 }
19247
19248 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
19249 {
19250 int elt_strict = strict;
19251
19252 if (elt == error_mark_node)
19253 return unify_invalid (explain_p);
19254
19255 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
19256 {
19257 tree type = TREE_TYPE (elt);
19258 if (type == error_mark_node)
19259 return unify_invalid (explain_p);
19260 /* It should only be possible to get here for a call. */
19261 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
19262 elt_strict |= maybe_adjust_types_for_deduction
19263 (DEDUCE_CALL, &elttype, &type, elt);
19264 elt = type;
19265 }
19266
19267 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
19268 explain_p);
19269 }
19270
19271 if (TREE_CODE (parm) == ARRAY_TYPE
19272 && deducible_array_bound (TYPE_DOMAIN (parm)))
19273 {
19274 /* Also deduce from the length of the initializer list. */
19275 tree max = size_int (CONSTRUCTOR_NELTS (arg));
19276 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
19277 if (idx == error_mark_node)
19278 return unify_invalid (explain_p);
19279 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19280 idx, explain_p);
19281 }
19282
19283 /* If the std::initializer_list<T> deduction worked, replace the
19284 deduced A with std::initializer_list<A>. */
19285 if (orig_parm != parm)
19286 {
19287 idx = TEMPLATE_TYPE_IDX (orig_parm);
19288 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19289 targ = listify (targ);
19290 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
19291 }
19292 return unify_success (explain_p);
19293 }
19294
19295 /* Immediately reject some pairs that won't unify because of
19296 cv-qualification mismatches. */
19297 if (TREE_CODE (arg) == TREE_CODE (parm)
19298 && TYPE_P (arg)
19299 /* It is the elements of the array which hold the cv quals of an array
19300 type, and the elements might be template type parms. We'll check
19301 when we recurse. */
19302 && TREE_CODE (arg) != ARRAY_TYPE
19303 /* We check the cv-qualifiers when unifying with template type
19304 parameters below. We want to allow ARG `const T' to unify with
19305 PARM `T' for example, when computing which of two templates
19306 is more specialized, for example. */
19307 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
19308 && !check_cv_quals_for_unify (strict_in, arg, parm))
19309 return unify_cv_qual_mismatch (explain_p, parm, arg);
19310
19311 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
19312 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
19313 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
19314 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
19315 strict &= ~UNIFY_ALLOW_DERIVED;
19316 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19317 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
19318
19319 switch (TREE_CODE (parm))
19320 {
19321 case TYPENAME_TYPE:
19322 case SCOPE_REF:
19323 case UNBOUND_CLASS_TEMPLATE:
19324 /* In a type which contains a nested-name-specifier, template
19325 argument values cannot be deduced for template parameters used
19326 within the nested-name-specifier. */
19327 return unify_success (explain_p);
19328
19329 case TEMPLATE_TYPE_PARM:
19330 case TEMPLATE_TEMPLATE_PARM:
19331 case BOUND_TEMPLATE_TEMPLATE_PARM:
19332 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19333 if (error_operand_p (tparm))
19334 return unify_invalid (explain_p);
19335
19336 if (TEMPLATE_TYPE_LEVEL (parm)
19337 != template_decl_level (tparm))
19338 /* The PARM is not one we're trying to unify. Just check
19339 to see if it matches ARG. */
19340 {
19341 if (TREE_CODE (arg) == TREE_CODE (parm)
19342 && (is_auto (parm) ? is_auto (arg)
19343 : same_type_p (parm, arg)))
19344 return unify_success (explain_p);
19345 else
19346 return unify_type_mismatch (explain_p, parm, arg);
19347 }
19348 idx = TEMPLATE_TYPE_IDX (parm);
19349 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19350 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
19351 if (error_operand_p (tparm))
19352 return unify_invalid (explain_p);
19353
19354 /* Check for mixed types and values. */
19355 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19356 && TREE_CODE (tparm) != TYPE_DECL)
19357 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19358 && TREE_CODE (tparm) != TEMPLATE_DECL))
19359 gcc_unreachable ();
19360
19361 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19362 {
19363 /* ARG must be constructed from a template class or a template
19364 template parameter. */
19365 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
19366 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19367 return unify_template_deduction_failure (explain_p, parm, arg);
19368 {
19369 tree parmvec = TYPE_TI_ARGS (parm);
19370 /* An alias template name is never deduced. */
19371 if (TYPE_ALIAS_P (arg))
19372 arg = strip_typedefs (arg);
19373 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
19374 tree full_argvec = add_to_template_args (targs, argvec);
19375 tree parm_parms
19376 = DECL_INNERMOST_TEMPLATE_PARMS
19377 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
19378 int i, len;
19379 int parm_variadic_p = 0;
19380
19381 /* The resolution to DR150 makes clear that default
19382 arguments for an N-argument may not be used to bind T
19383 to a template template parameter with fewer than N
19384 parameters. It is not safe to permit the binding of
19385 default arguments as an extension, as that may change
19386 the meaning of a conforming program. Consider:
19387
19388 struct Dense { static const unsigned int dim = 1; };
19389
19390 template <template <typename> class View,
19391 typename Block>
19392 void operator+(float, View<Block> const&);
19393
19394 template <typename Block,
19395 unsigned int Dim = Block::dim>
19396 struct Lvalue_proxy { operator float() const; };
19397
19398 void
19399 test_1d (void) {
19400 Lvalue_proxy<Dense> p;
19401 float b;
19402 b + p;
19403 }
19404
19405 Here, if Lvalue_proxy is permitted to bind to View, then
19406 the global operator+ will be used; if they are not, the
19407 Lvalue_proxy will be converted to float. */
19408 if (coerce_template_parms (parm_parms,
19409 full_argvec,
19410 TYPE_TI_TEMPLATE (parm),
19411 (explain_p
19412 ? tf_warning_or_error
19413 : tf_none),
19414 /*require_all_args=*/true,
19415 /*use_default_args=*/false)
19416 == error_mark_node)
19417 return 1;
19418
19419 /* Deduce arguments T, i from TT<T> or TT<i>.
19420 We check each element of PARMVEC and ARGVEC individually
19421 rather than the whole TREE_VEC since they can have
19422 different number of elements. */
19423
19424 parmvec = expand_template_argument_pack (parmvec);
19425 argvec = expand_template_argument_pack (argvec);
19426
19427 len = TREE_VEC_LENGTH (parmvec);
19428
19429 /* Check if the parameters end in a pack, making them
19430 variadic. */
19431 if (len > 0
19432 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
19433 parm_variadic_p = 1;
19434
19435 for (i = 0; i < len - parm_variadic_p; ++i)
19436 /* If the template argument list of P contains a pack
19437 expansion that is not the last template argument, the
19438 entire template argument list is a non-deduced
19439 context. */
19440 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
19441 return unify_success (explain_p);
19442
19443 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
19444 return unify_too_few_arguments (explain_p,
19445 TREE_VEC_LENGTH (argvec), len);
19446
19447 for (i = 0; i < len - parm_variadic_p; ++i)
19448 {
19449 RECUR_AND_CHECK_FAILURE (tparms, targs,
19450 TREE_VEC_ELT (parmvec, i),
19451 TREE_VEC_ELT (argvec, i),
19452 UNIFY_ALLOW_NONE, explain_p);
19453 }
19454
19455 if (parm_variadic_p
19456 && unify_pack_expansion (tparms, targs,
19457 parmvec, argvec,
19458 DEDUCE_EXACT,
19459 /*subr=*/true, explain_p))
19460 return 1;
19461 }
19462 arg = TYPE_TI_TEMPLATE (arg);
19463
19464 /* Fall through to deduce template name. */
19465 }
19466
19467 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19468 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19469 {
19470 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19471
19472 /* Simple cases: Value already set, does match or doesn't. */
19473 if (targ != NULL_TREE && template_args_equal (targ, arg))
19474 return unify_success (explain_p);
19475 else if (targ)
19476 return unify_inconsistency (explain_p, parm, targ, arg);
19477 }
19478 else
19479 {
19480 /* If PARM is `const T' and ARG is only `int', we don't have
19481 a match unless we are allowing additional qualification.
19482 If ARG is `const int' and PARM is just `T' that's OK;
19483 that binds `const int' to `T'. */
19484 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
19485 arg, parm))
19486 return unify_cv_qual_mismatch (explain_p, parm, arg);
19487
19488 /* Consider the case where ARG is `const volatile int' and
19489 PARM is `const T'. Then, T should be `volatile int'. */
19490 arg = cp_build_qualified_type_real
19491 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
19492 if (arg == error_mark_node)
19493 return unify_invalid (explain_p);
19494
19495 /* Simple cases: Value already set, does match or doesn't. */
19496 if (targ != NULL_TREE && same_type_p (targ, arg))
19497 return unify_success (explain_p);
19498 else if (targ)
19499 return unify_inconsistency (explain_p, parm, targ, arg);
19500
19501 /* Make sure that ARG is not a variable-sized array. (Note
19502 that were talking about variable-sized arrays (like
19503 `int[n]'), rather than arrays of unknown size (like
19504 `int[]').) We'll get very confused by such a type since
19505 the bound of the array is not constant, and therefore
19506 not mangleable. Besides, such types are not allowed in
19507 ISO C++, so we can do as we please here. We do allow
19508 them for 'auto' deduction, since that isn't ABI-exposed. */
19509 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
19510 return unify_vla_arg (explain_p, arg);
19511
19512 /* Strip typedefs as in convert_template_argument. */
19513 arg = canonicalize_type_argument (arg, tf_none);
19514 }
19515
19516 /* If ARG is a parameter pack or an expansion, we cannot unify
19517 against it unless PARM is also a parameter pack. */
19518 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19519 && !template_parameter_pack_p (parm))
19520 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19521
19522 /* If the argument deduction results is a METHOD_TYPE,
19523 then there is a problem.
19524 METHOD_TYPE doesn't map to any real C++ type the result of
19525 the deduction can not be of that type. */
19526 if (TREE_CODE (arg) == METHOD_TYPE)
19527 return unify_method_type_error (explain_p, arg);
19528
19529 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19530 return unify_success (explain_p);
19531
19532 case TEMPLATE_PARM_INDEX:
19533 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19534 if (error_operand_p (tparm))
19535 return unify_invalid (explain_p);
19536
19537 if (TEMPLATE_PARM_LEVEL (parm)
19538 != template_decl_level (tparm))
19539 {
19540 /* The PARM is not one we're trying to unify. Just check
19541 to see if it matches ARG. */
19542 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
19543 && cp_tree_equal (parm, arg));
19544 if (result)
19545 unify_expression_unequal (explain_p, parm, arg);
19546 return result;
19547 }
19548
19549 idx = TEMPLATE_PARM_IDX (parm);
19550 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19551
19552 if (targ)
19553 {
19554 int x = !cp_tree_equal (targ, arg);
19555 if (x)
19556 unify_inconsistency (explain_p, parm, targ, arg);
19557 return x;
19558 }
19559
19560 /* [temp.deduct.type] If, in the declaration of a function template
19561 with a non-type template-parameter, the non-type
19562 template-parameter is used in an expression in the function
19563 parameter-list and, if the corresponding template-argument is
19564 deduced, the template-argument type shall match the type of the
19565 template-parameter exactly, except that a template-argument
19566 deduced from an array bound may be of any integral type.
19567 The non-type parameter might use already deduced type parameters. */
19568 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
19569 if (!TREE_TYPE (arg))
19570 /* Template-parameter dependent expression. Just accept it for now.
19571 It will later be processed in convert_template_argument. */
19572 ;
19573 else if (same_type_p (TREE_TYPE (arg), tparm))
19574 /* OK */;
19575 else if ((strict & UNIFY_ALLOW_INTEGER)
19576 && CP_INTEGRAL_TYPE_P (tparm))
19577 /* Convert the ARG to the type of PARM; the deduced non-type
19578 template argument must exactly match the types of the
19579 corresponding parameter. */
19580 arg = fold (build_nop (tparm, arg));
19581 else if (uses_template_parms (tparm))
19582 /* We haven't deduced the type of this parameter yet. Try again
19583 later. */
19584 return unify_success (explain_p);
19585 else
19586 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
19587
19588 /* If ARG is a parameter pack or an expansion, we cannot unify
19589 against it unless PARM is also a parameter pack. */
19590 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19591 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
19592 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19593
19594 {
19595 bool removed_attr = false;
19596 arg = strip_typedefs_expr (arg, &removed_attr);
19597 }
19598 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19599 return unify_success (explain_p);
19600
19601 case PTRMEM_CST:
19602 {
19603 /* A pointer-to-member constant can be unified only with
19604 another constant. */
19605 if (TREE_CODE (arg) != PTRMEM_CST)
19606 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
19607
19608 /* Just unify the class member. It would be useless (and possibly
19609 wrong, depending on the strict flags) to unify also
19610 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19611 arg refer to the same variable, even if through different
19612 classes. For instance:
19613
19614 struct A { int x; };
19615 struct B : A { };
19616
19617 Unification of &A::x and &B::x must succeed. */
19618 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
19619 PTRMEM_CST_MEMBER (arg), strict, explain_p);
19620 }
19621
19622 case POINTER_TYPE:
19623 {
19624 if (!TYPE_PTR_P (arg))
19625 return unify_type_mismatch (explain_p, parm, arg);
19626
19627 /* [temp.deduct.call]
19628
19629 A can be another pointer or pointer to member type that can
19630 be converted to the deduced A via a qualification
19631 conversion (_conv.qual_).
19632
19633 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19634 This will allow for additional cv-qualification of the
19635 pointed-to types if appropriate. */
19636
19637 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
19638 /* The derived-to-base conversion only persists through one
19639 level of pointers. */
19640 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
19641
19642 return unify (tparms, targs, TREE_TYPE (parm),
19643 TREE_TYPE (arg), strict, explain_p);
19644 }
19645
19646 case REFERENCE_TYPE:
19647 if (TREE_CODE (arg) != REFERENCE_TYPE)
19648 return unify_type_mismatch (explain_p, parm, arg);
19649 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19650 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19651
19652 case ARRAY_TYPE:
19653 if (TREE_CODE (arg) != ARRAY_TYPE)
19654 return unify_type_mismatch (explain_p, parm, arg);
19655 if ((TYPE_DOMAIN (parm) == NULL_TREE)
19656 != (TYPE_DOMAIN (arg) == NULL_TREE))
19657 return unify_type_mismatch (explain_p, parm, arg);
19658 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19659 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19660 if (TYPE_DOMAIN (parm) != NULL_TREE)
19661 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19662 TYPE_DOMAIN (arg), explain_p);
19663 return unify_success (explain_p);
19664
19665 case REAL_TYPE:
19666 case COMPLEX_TYPE:
19667 case VECTOR_TYPE:
19668 case INTEGER_TYPE:
19669 case BOOLEAN_TYPE:
19670 case ENUMERAL_TYPE:
19671 case VOID_TYPE:
19672 case NULLPTR_TYPE:
19673 if (TREE_CODE (arg) != TREE_CODE (parm))
19674 return unify_type_mismatch (explain_p, parm, arg);
19675
19676 /* We have already checked cv-qualification at the top of the
19677 function. */
19678 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
19679 return unify_type_mismatch (explain_p, parm, arg);
19680
19681 /* As far as unification is concerned, this wins. Later checks
19682 will invalidate it if necessary. */
19683 return unify_success (explain_p);
19684
19685 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
19686 /* Type INTEGER_CST can come from ordinary constant template args. */
19687 case INTEGER_CST:
19688 while (TREE_CODE (arg) == NOP_EXPR)
19689 arg = TREE_OPERAND (arg, 0);
19690
19691 if (TREE_CODE (arg) != INTEGER_CST)
19692 return unify_template_argument_mismatch (explain_p, parm, arg);
19693 return (tree_int_cst_equal (parm, arg)
19694 ? unify_success (explain_p)
19695 : unify_template_argument_mismatch (explain_p, parm, arg));
19696
19697 case TREE_VEC:
19698 {
19699 int i, len, argslen;
19700 int parm_variadic_p = 0;
19701
19702 if (TREE_CODE (arg) != TREE_VEC)
19703 return unify_template_argument_mismatch (explain_p, parm, arg);
19704
19705 len = TREE_VEC_LENGTH (parm);
19706 argslen = TREE_VEC_LENGTH (arg);
19707
19708 /* Check for pack expansions in the parameters. */
19709 for (i = 0; i < len; ++i)
19710 {
19711 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
19712 {
19713 if (i == len - 1)
19714 /* We can unify against something with a trailing
19715 parameter pack. */
19716 parm_variadic_p = 1;
19717 else
19718 /* [temp.deduct.type]/9: If the template argument list of
19719 P contains a pack expansion that is not the last
19720 template argument, the entire template argument list
19721 is a non-deduced context. */
19722 return unify_success (explain_p);
19723 }
19724 }
19725
19726 /* If we don't have enough arguments to satisfy the parameters
19727 (not counting the pack expression at the end), or we have
19728 too many arguments for a parameter list that doesn't end in
19729 a pack expression, we can't unify. */
19730 if (parm_variadic_p
19731 ? argslen < len - parm_variadic_p
19732 : argslen != len)
19733 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
19734
19735 /* Unify all of the parameters that precede the (optional)
19736 pack expression. */
19737 for (i = 0; i < len - parm_variadic_p; ++i)
19738 {
19739 RECUR_AND_CHECK_FAILURE (tparms, targs,
19740 TREE_VEC_ELT (parm, i),
19741 TREE_VEC_ELT (arg, i),
19742 UNIFY_ALLOW_NONE, explain_p);
19743 }
19744 if (parm_variadic_p)
19745 return unify_pack_expansion (tparms, targs, parm, arg,
19746 DEDUCE_EXACT,
19747 /*subr=*/true, explain_p);
19748 return unify_success (explain_p);
19749 }
19750
19751 case RECORD_TYPE:
19752 case UNION_TYPE:
19753 if (TREE_CODE (arg) != TREE_CODE (parm))
19754 return unify_type_mismatch (explain_p, parm, arg);
19755
19756 if (TYPE_PTRMEMFUNC_P (parm))
19757 {
19758 if (!TYPE_PTRMEMFUNC_P (arg))
19759 return unify_type_mismatch (explain_p, parm, arg);
19760
19761 return unify (tparms, targs,
19762 TYPE_PTRMEMFUNC_FN_TYPE (parm),
19763 TYPE_PTRMEMFUNC_FN_TYPE (arg),
19764 strict, explain_p);
19765 }
19766 else if (TYPE_PTRMEMFUNC_P (arg))
19767 return unify_type_mismatch (explain_p, parm, arg);
19768
19769 if (CLASSTYPE_TEMPLATE_INFO (parm))
19770 {
19771 tree t = NULL_TREE;
19772
19773 if (strict_in & UNIFY_ALLOW_DERIVED)
19774 {
19775 /* First, we try to unify the PARM and ARG directly. */
19776 t = try_class_unification (tparms, targs,
19777 parm, arg, explain_p);
19778
19779 if (!t)
19780 {
19781 /* Fallback to the special case allowed in
19782 [temp.deduct.call]:
19783
19784 If P is a class, and P has the form
19785 template-id, then A can be a derived class of
19786 the deduced A. Likewise, if P is a pointer to
19787 a class of the form template-id, A can be a
19788 pointer to a derived class pointed to by the
19789 deduced A. */
19790 enum template_base_result r;
19791 r = get_template_base (tparms, targs, parm, arg,
19792 explain_p, &t);
19793
19794 if (!t)
19795 {
19796 /* Don't give the derived diagnostic if we're
19797 already dealing with the same template. */
19798 bool same_template
19799 = (CLASSTYPE_TEMPLATE_INFO (arg)
19800 && (CLASSTYPE_TI_TEMPLATE (parm)
19801 == CLASSTYPE_TI_TEMPLATE (arg)));
19802 return unify_no_common_base (explain_p && !same_template,
19803 r, parm, arg);
19804 }
19805 }
19806 }
19807 else if (CLASSTYPE_TEMPLATE_INFO (arg)
19808 && (CLASSTYPE_TI_TEMPLATE (parm)
19809 == CLASSTYPE_TI_TEMPLATE (arg)))
19810 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19811 Then, we should unify `int' and `U'. */
19812 t = arg;
19813 else
19814 /* There's no chance of unification succeeding. */
19815 return unify_type_mismatch (explain_p, parm, arg);
19816
19817 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
19818 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
19819 }
19820 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
19821 return unify_type_mismatch (explain_p, parm, arg);
19822 return unify_success (explain_p);
19823
19824 case METHOD_TYPE:
19825 case FUNCTION_TYPE:
19826 {
19827 unsigned int nargs;
19828 tree *args;
19829 tree a;
19830 unsigned int i;
19831
19832 if (TREE_CODE (arg) != TREE_CODE (parm))
19833 return unify_type_mismatch (explain_p, parm, arg);
19834
19835 /* CV qualifications for methods can never be deduced, they must
19836 match exactly. We need to check them explicitly here,
19837 because type_unification_real treats them as any other
19838 cv-qualified parameter. */
19839 if (TREE_CODE (parm) == METHOD_TYPE
19840 && (!check_cv_quals_for_unify
19841 (UNIFY_ALLOW_NONE,
19842 class_of_this_parm (arg),
19843 class_of_this_parm (parm))))
19844 return unify_cv_qual_mismatch (explain_p, parm, arg);
19845
19846 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
19847 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
19848
19849 nargs = list_length (TYPE_ARG_TYPES (arg));
19850 args = XALLOCAVEC (tree, nargs);
19851 for (a = TYPE_ARG_TYPES (arg), i = 0;
19852 a != NULL_TREE && a != void_list_node;
19853 a = TREE_CHAIN (a), ++i)
19854 args[i] = TREE_VALUE (a);
19855 nargs = i;
19856
19857 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
19858 args, nargs, 1, DEDUCE_EXACT,
19859 LOOKUP_NORMAL, NULL, explain_p);
19860 }
19861
19862 case OFFSET_TYPE:
19863 /* Unify a pointer to member with a pointer to member function, which
19864 deduces the type of the member as a function type. */
19865 if (TYPE_PTRMEMFUNC_P (arg))
19866 {
19867 /* Check top-level cv qualifiers */
19868 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
19869 return unify_cv_qual_mismatch (explain_p, parm, arg);
19870
19871 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19872 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
19873 UNIFY_ALLOW_NONE, explain_p);
19874
19875 /* Determine the type of the function we are unifying against. */
19876 tree fntype = static_fn_type (arg);
19877
19878 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
19879 }
19880
19881 if (TREE_CODE (arg) != OFFSET_TYPE)
19882 return unify_type_mismatch (explain_p, parm, arg);
19883 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19884 TYPE_OFFSET_BASETYPE (arg),
19885 UNIFY_ALLOW_NONE, explain_p);
19886 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19887 strict, explain_p);
19888
19889 case CONST_DECL:
19890 if (DECL_TEMPLATE_PARM_P (parm))
19891 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
19892 if (arg != scalar_constant_value (parm))
19893 return unify_template_argument_mismatch (explain_p, parm, arg);
19894 return unify_success (explain_p);
19895
19896 case FIELD_DECL:
19897 case TEMPLATE_DECL:
19898 /* Matched cases are handled by the ARG == PARM test above. */
19899 return unify_template_argument_mismatch (explain_p, parm, arg);
19900
19901 case VAR_DECL:
19902 /* A non-type template parameter that is a variable should be a
19903 an integral constant, in which case, it whould have been
19904 folded into its (constant) value. So we should not be getting
19905 a variable here. */
19906 gcc_unreachable ();
19907
19908 case TYPE_ARGUMENT_PACK:
19909 case NONTYPE_ARGUMENT_PACK:
19910 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
19911 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
19912
19913 case TYPEOF_TYPE:
19914 case DECLTYPE_TYPE:
19915 case UNDERLYING_TYPE:
19916 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19917 or UNDERLYING_TYPE nodes. */
19918 return unify_success (explain_p);
19919
19920 case ERROR_MARK:
19921 /* Unification fails if we hit an error node. */
19922 return unify_invalid (explain_p);
19923
19924 case INDIRECT_REF:
19925 if (REFERENCE_REF_P (parm))
19926 {
19927 if (REFERENCE_REF_P (arg))
19928 arg = TREE_OPERAND (arg, 0);
19929 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
19930 strict, explain_p);
19931 }
19932 /* FALLTHRU */
19933
19934 default:
19935 /* An unresolved overload is a nondeduced context. */
19936 if (is_overloaded_fn (parm) || type_unknown_p (parm))
19937 return unify_success (explain_p);
19938 gcc_assert (EXPR_P (parm));
19939
19940 /* We must be looking at an expression. This can happen with
19941 something like:
19942
19943 template <int I>
19944 void foo(S<I>, S<I + 2>);
19945
19946 This is a "nondeduced context":
19947
19948 [deduct.type]
19949
19950 The nondeduced contexts are:
19951
19952 --A type that is a template-id in which one or more of
19953 the template-arguments is an expression that references
19954 a template-parameter.
19955
19956 In these cases, we assume deduction succeeded, but don't
19957 actually infer any unifications. */
19958
19959 if (!uses_template_parms (parm)
19960 && !template_args_equal (parm, arg))
19961 return unify_expression_unequal (explain_p, parm, arg);
19962 else
19963 return unify_success (explain_p);
19964 }
19965 }
19966 #undef RECUR_AND_CHECK_FAILURE
19967 \f
19968 /* Note that DECL can be defined in this translation unit, if
19969 required. */
19970
19971 static void
19972 mark_definable (tree decl)
19973 {
19974 tree clone;
19975 DECL_NOT_REALLY_EXTERN (decl) = 1;
19976 FOR_EACH_CLONE (clone, decl)
19977 DECL_NOT_REALLY_EXTERN (clone) = 1;
19978 }
19979
19980 /* Called if RESULT is explicitly instantiated, or is a member of an
19981 explicitly instantiated class. */
19982
19983 void
19984 mark_decl_instantiated (tree result, int extern_p)
19985 {
19986 SET_DECL_EXPLICIT_INSTANTIATION (result);
19987
19988 /* If this entity has already been written out, it's too late to
19989 make any modifications. */
19990 if (TREE_ASM_WRITTEN (result))
19991 return;
19992
19993 /* For anonymous namespace we don't need to do anything. */
19994 if (decl_anon_ns_mem_p (result))
19995 {
19996 gcc_assert (!TREE_PUBLIC (result));
19997 return;
19998 }
19999
20000 if (TREE_CODE (result) != FUNCTION_DECL)
20001 /* The TREE_PUBLIC flag for function declarations will have been
20002 set correctly by tsubst. */
20003 TREE_PUBLIC (result) = 1;
20004
20005 /* This might have been set by an earlier implicit instantiation. */
20006 DECL_COMDAT (result) = 0;
20007
20008 if (extern_p)
20009 DECL_NOT_REALLY_EXTERN (result) = 0;
20010 else
20011 {
20012 mark_definable (result);
20013 mark_needed (result);
20014 /* Always make artificials weak. */
20015 if (DECL_ARTIFICIAL (result) && flag_weak)
20016 comdat_linkage (result);
20017 /* For WIN32 we also want to put explicit instantiations in
20018 linkonce sections. */
20019 else if (TREE_PUBLIC (result))
20020 maybe_make_one_only (result);
20021 }
20022
20023 /* If EXTERN_P, then this function will not be emitted -- unless
20024 followed by an explicit instantiation, at which point its linkage
20025 will be adjusted. If !EXTERN_P, then this function will be
20026 emitted here. In neither circumstance do we want
20027 import_export_decl to adjust the linkage. */
20028 DECL_INTERFACE_KNOWN (result) = 1;
20029 }
20030
20031 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20032 important template arguments. If any are missing, we check whether
20033 they're important by using error_mark_node for substituting into any
20034 args that were used for partial ordering (the ones between ARGS and END)
20035 and seeing if it bubbles up. */
20036
20037 static bool
20038 check_undeduced_parms (tree targs, tree args, tree end)
20039 {
20040 bool found = false;
20041 int i;
20042 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
20043 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
20044 {
20045 found = true;
20046 TREE_VEC_ELT (targs, i) = error_mark_node;
20047 }
20048 if (found)
20049 {
20050 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
20051 if (substed == error_mark_node)
20052 return true;
20053 }
20054 return false;
20055 }
20056
20057 /* Given two function templates PAT1 and PAT2, return:
20058
20059 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20060 -1 if PAT2 is more specialized than PAT1.
20061 0 if neither is more specialized.
20062
20063 LEN indicates the number of parameters we should consider
20064 (defaulted parameters should not be considered).
20065
20066 The 1998 std underspecified function template partial ordering, and
20067 DR214 addresses the issue. We take pairs of arguments, one from
20068 each of the templates, and deduce them against each other. One of
20069 the templates will be more specialized if all the *other*
20070 template's arguments deduce against its arguments and at least one
20071 of its arguments *does* *not* deduce against the other template's
20072 corresponding argument. Deduction is done as for class templates.
20073 The arguments used in deduction have reference and top level cv
20074 qualifiers removed. Iff both arguments were originally reference
20075 types *and* deduction succeeds in both directions, an lvalue reference
20076 wins against an rvalue reference and otherwise the template
20077 with the more cv-qualified argument wins for that pairing (if
20078 neither is more cv-qualified, they both are equal). Unlike regular
20079 deduction, after all the arguments have been deduced in this way,
20080 we do *not* verify the deduced template argument values can be
20081 substituted into non-deduced contexts.
20082
20083 The logic can be a bit confusing here, because we look at deduce1 and
20084 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20085 can find template arguments for pat1 to make arg1 look like arg2, that
20086 means that arg2 is at least as specialized as arg1. */
20087
20088 int
20089 more_specialized_fn (tree pat1, tree pat2, int len)
20090 {
20091 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
20092 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
20093 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
20094 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
20095 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
20096 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
20097 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
20098 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
20099 tree origs1, origs2;
20100 bool lose1 = false;
20101 bool lose2 = false;
20102
20103 /* Remove the this parameter from non-static member functions. If
20104 one is a non-static member function and the other is not a static
20105 member function, remove the first parameter from that function
20106 also. This situation occurs for operator functions where we
20107 locate both a member function (with this pointer) and non-member
20108 operator (with explicit first operand). */
20109 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
20110 {
20111 len--; /* LEN is the number of significant arguments for DECL1 */
20112 args1 = TREE_CHAIN (args1);
20113 if (!DECL_STATIC_FUNCTION_P (decl2))
20114 args2 = TREE_CHAIN (args2);
20115 }
20116 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
20117 {
20118 args2 = TREE_CHAIN (args2);
20119 if (!DECL_STATIC_FUNCTION_P (decl1))
20120 {
20121 len--;
20122 args1 = TREE_CHAIN (args1);
20123 }
20124 }
20125
20126 /* If only one is a conversion operator, they are unordered. */
20127 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
20128 return 0;
20129
20130 /* Consider the return type for a conversion function */
20131 if (DECL_CONV_FN_P (decl1))
20132 {
20133 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
20134 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
20135 len++;
20136 }
20137
20138 processing_template_decl++;
20139
20140 origs1 = args1;
20141 origs2 = args2;
20142
20143 while (len--
20144 /* Stop when an ellipsis is seen. */
20145 && args1 != NULL_TREE && args2 != NULL_TREE)
20146 {
20147 tree arg1 = TREE_VALUE (args1);
20148 tree arg2 = TREE_VALUE (args2);
20149 int deduce1, deduce2;
20150 int quals1 = -1;
20151 int quals2 = -1;
20152 int ref1 = 0;
20153 int ref2 = 0;
20154
20155 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20156 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20157 {
20158 /* When both arguments are pack expansions, we need only
20159 unify the patterns themselves. */
20160 arg1 = PACK_EXPANSION_PATTERN (arg1);
20161 arg2 = PACK_EXPANSION_PATTERN (arg2);
20162
20163 /* This is the last comparison we need to do. */
20164 len = 0;
20165 }
20166
20167 if (TREE_CODE (arg1) == REFERENCE_TYPE)
20168 {
20169 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
20170 arg1 = TREE_TYPE (arg1);
20171 quals1 = cp_type_quals (arg1);
20172 }
20173
20174 if (TREE_CODE (arg2) == REFERENCE_TYPE)
20175 {
20176 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
20177 arg2 = TREE_TYPE (arg2);
20178 quals2 = cp_type_quals (arg2);
20179 }
20180
20181 arg1 = TYPE_MAIN_VARIANT (arg1);
20182 arg2 = TYPE_MAIN_VARIANT (arg2);
20183
20184 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
20185 {
20186 int i, len2 = list_length (args2);
20187 tree parmvec = make_tree_vec (1);
20188 tree argvec = make_tree_vec (len2);
20189 tree ta = args2;
20190
20191 /* Setup the parameter vector, which contains only ARG1. */
20192 TREE_VEC_ELT (parmvec, 0) = arg1;
20193
20194 /* Setup the argument vector, which contains the remaining
20195 arguments. */
20196 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
20197 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20198
20199 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
20200 argvec, DEDUCE_EXACT,
20201 /*subr=*/true, /*explain_p=*/false)
20202 == 0);
20203
20204 /* We cannot deduce in the other direction, because ARG1 is
20205 a pack expansion but ARG2 is not. */
20206 deduce2 = 0;
20207 }
20208 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20209 {
20210 int i, len1 = list_length (args1);
20211 tree parmvec = make_tree_vec (1);
20212 tree argvec = make_tree_vec (len1);
20213 tree ta = args1;
20214
20215 /* Setup the parameter vector, which contains only ARG1. */
20216 TREE_VEC_ELT (parmvec, 0) = arg2;
20217
20218 /* Setup the argument vector, which contains the remaining
20219 arguments. */
20220 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
20221 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20222
20223 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
20224 argvec, DEDUCE_EXACT,
20225 /*subr=*/true, /*explain_p=*/false)
20226 == 0);
20227
20228 /* We cannot deduce in the other direction, because ARG2 is
20229 a pack expansion but ARG1 is not.*/
20230 deduce1 = 0;
20231 }
20232
20233 else
20234 {
20235 /* The normal case, where neither argument is a pack
20236 expansion. */
20237 deduce1 = (unify (tparms1, targs1, arg1, arg2,
20238 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20239 == 0);
20240 deduce2 = (unify (tparms2, targs2, arg2, arg1,
20241 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20242 == 0);
20243 }
20244
20245 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20246 arg2, then arg2 is not as specialized as arg1. */
20247 if (!deduce1)
20248 lose2 = true;
20249 if (!deduce2)
20250 lose1 = true;
20251
20252 /* "If, for a given type, deduction succeeds in both directions
20253 (i.e., the types are identical after the transformations above)
20254 and both P and A were reference types (before being replaced with
20255 the type referred to above):
20256 - if the type from the argument template was an lvalue reference and
20257 the type from the parameter template was not, the argument type is
20258 considered to be more specialized than the other; otherwise,
20259 - if the type from the argument template is more cv-qualified
20260 than the type from the parameter template (as described above),
20261 the argument type is considered to be more specialized than the other;
20262 otherwise,
20263 - neither type is more specialized than the other." */
20264
20265 if (deduce1 && deduce2)
20266 {
20267 if (ref1 && ref2 && ref1 != ref2)
20268 {
20269 if (ref1 > ref2)
20270 lose1 = true;
20271 else
20272 lose2 = true;
20273 }
20274 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
20275 {
20276 if ((quals1 & quals2) == quals2)
20277 lose2 = true;
20278 if ((quals1 & quals2) == quals1)
20279 lose1 = true;
20280 }
20281 }
20282
20283 if (lose1 && lose2)
20284 /* We've failed to deduce something in either direction.
20285 These must be unordered. */
20286 break;
20287
20288 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20289 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20290 /* We have already processed all of the arguments in our
20291 handing of the pack expansion type. */
20292 len = 0;
20293
20294 args1 = TREE_CHAIN (args1);
20295 args2 = TREE_CHAIN (args2);
20296 }
20297
20298 /* "In most cases, all template parameters must have values in order for
20299 deduction to succeed, but for partial ordering purposes a template
20300 parameter may remain without a value provided it is not used in the
20301 types being used for partial ordering."
20302
20303 Thus, if we are missing any of the targs1 we need to substitute into
20304 origs1, then pat2 is not as specialized as pat1. This can happen when
20305 there is a nondeduced context. */
20306 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
20307 lose2 = true;
20308 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
20309 lose1 = true;
20310
20311 processing_template_decl--;
20312
20313 /* If both deductions succeed, the partial ordering selects the more
20314 constrained template. */
20315 if (!lose1 && !lose2)
20316 {
20317 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
20318 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
20319 lose1 = !subsumes_constraints (c1, c2);
20320 lose2 = !subsumes_constraints (c2, c1);
20321 }
20322
20323 /* All things being equal, if the next argument is a pack expansion
20324 for one function but not for the other, prefer the
20325 non-variadic function. FIXME this is bogus; see c++/41958. */
20326 if (lose1 == lose2
20327 && args1 && TREE_VALUE (args1)
20328 && args2 && TREE_VALUE (args2))
20329 {
20330 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
20331 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
20332 }
20333
20334 if (lose1 == lose2)
20335 return 0;
20336 else if (!lose1)
20337 return 1;
20338 else
20339 return -1;
20340 }
20341
20342 /* Determine which of two partial specializations of TMPL is more
20343 specialized.
20344
20345 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20346 to the first partial specialization. The TREE_PURPOSE is the
20347 innermost set of template parameters for the partial
20348 specialization. PAT2 is similar, but for the second template.
20349
20350 Return 1 if the first partial specialization is more specialized;
20351 -1 if the second is more specialized; 0 if neither is more
20352 specialized.
20353
20354 See [temp.class.order] for information about determining which of
20355 two templates is more specialized. */
20356
20357 static int
20358 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
20359 {
20360 tree targs;
20361 int winner = 0;
20362 bool any_deductions = false;
20363
20364 tree tmpl1 = TREE_VALUE (pat1);
20365 tree tmpl2 = TREE_VALUE (pat2);
20366 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
20367 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
20368 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
20369 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
20370
20371 /* Just like what happens for functions, if we are ordering between
20372 different template specializations, we may encounter dependent
20373 types in the arguments, and we need our dependency check functions
20374 to behave correctly. */
20375 ++processing_template_decl;
20376 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
20377 if (targs)
20378 {
20379 --winner;
20380 any_deductions = true;
20381 }
20382
20383 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
20384 if (targs)
20385 {
20386 ++winner;
20387 any_deductions = true;
20388 }
20389 --processing_template_decl;
20390
20391 /* If both deductions succeed, the partial ordering selects the more
20392 constrained template. */
20393 if (!winner && any_deductions)
20394 return more_constrained (tmpl1, tmpl2);
20395
20396 /* In the case of a tie where at least one of the templates
20397 has a parameter pack at the end, the template with the most
20398 non-packed parameters wins. */
20399 if (winner == 0
20400 && any_deductions
20401 && (template_args_variadic_p (TREE_PURPOSE (pat1))
20402 || template_args_variadic_p (TREE_PURPOSE (pat2))))
20403 {
20404 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
20405 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
20406 int len1 = TREE_VEC_LENGTH (args1);
20407 int len2 = TREE_VEC_LENGTH (args2);
20408
20409 /* We don't count the pack expansion at the end. */
20410 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
20411 --len1;
20412 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
20413 --len2;
20414
20415 if (len1 > len2)
20416 return 1;
20417 else if (len1 < len2)
20418 return -1;
20419 }
20420
20421 return winner;
20422 }
20423
20424 /* Return the template arguments that will produce the function signature
20425 DECL from the function template FN, with the explicit template
20426 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20427 also match. Return NULL_TREE if no satisfactory arguments could be
20428 found. */
20429
20430 static tree
20431 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
20432 {
20433 int ntparms = DECL_NTPARMS (fn);
20434 tree targs = make_tree_vec (ntparms);
20435 tree decl_type = TREE_TYPE (decl);
20436 tree decl_arg_types;
20437 tree *args;
20438 unsigned int nargs, ix;
20439 tree arg;
20440
20441 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
20442
20443 /* Never do unification on the 'this' parameter. */
20444 decl_arg_types = skip_artificial_parms_for (decl,
20445 TYPE_ARG_TYPES (decl_type));
20446
20447 nargs = list_length (decl_arg_types);
20448 args = XALLOCAVEC (tree, nargs);
20449 for (arg = decl_arg_types, ix = 0;
20450 arg != NULL_TREE && arg != void_list_node;
20451 arg = TREE_CHAIN (arg), ++ix)
20452 args[ix] = TREE_VALUE (arg);
20453
20454 if (fn_type_unification (fn, explicit_args, targs,
20455 args, ix,
20456 (check_rettype || DECL_CONV_FN_P (fn)
20457 ? TREE_TYPE (decl_type) : NULL_TREE),
20458 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
20459 /*decltype*/false)
20460 == error_mark_node)
20461 return NULL_TREE;
20462
20463 return targs;
20464 }
20465
20466 /* Return the innermost template arguments that, when applied to a partial
20467 specialization of TMPL whose innermost template parameters are
20468 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
20469 ARGS.
20470
20471 For example, suppose we have:
20472
20473 template <class T, class U> struct S {};
20474 template <class T> struct S<T*, int> {};
20475
20476 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
20477 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
20478 int}. The resulting vector will be {double}, indicating that `T'
20479 is bound to `double'. */
20480
20481 static tree
20482 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
20483 {
20484 int i, ntparms = TREE_VEC_LENGTH (tparms);
20485 tree deduced_args;
20486 tree innermost_deduced_args;
20487
20488 innermost_deduced_args = make_tree_vec (ntparms);
20489 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20490 {
20491 deduced_args = copy_node (args);
20492 SET_TMPL_ARGS_LEVEL (deduced_args,
20493 TMPL_ARGS_DEPTH (deduced_args),
20494 innermost_deduced_args);
20495 }
20496 else
20497 deduced_args = innermost_deduced_args;
20498
20499 if (unify (tparms, deduced_args,
20500 INNERMOST_TEMPLATE_ARGS (spec_args),
20501 INNERMOST_TEMPLATE_ARGS (args),
20502 UNIFY_ALLOW_NONE, /*explain_p=*/false))
20503 return NULL_TREE;
20504
20505 for (i = 0; i < ntparms; ++i)
20506 if (! TREE_VEC_ELT (innermost_deduced_args, i))
20507 return NULL_TREE;
20508
20509 /* Verify that nondeduced template arguments agree with the type
20510 obtained from argument deduction.
20511
20512 For example:
20513
20514 struct A { typedef int X; };
20515 template <class T, class U> struct C {};
20516 template <class T> struct C<T, typename T::X> {};
20517
20518 Then with the instantiation `C<A, int>', we can deduce that
20519 `T' is `A' but unify () does not check whether `typename T::X'
20520 is `int'. */
20521 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
20522 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20523 spec_args, tmpl,
20524 tf_none, false, false);
20525 if (spec_args == error_mark_node
20526 /* We only need to check the innermost arguments; the other
20527 arguments will always agree. */
20528 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
20529 INNERMOST_TEMPLATE_ARGS (args)))
20530 return NULL_TREE;
20531
20532 /* Now that we have bindings for all of the template arguments,
20533 ensure that the arguments deduced for the template template
20534 parameters have compatible template parameter lists. See the use
20535 of template_template_parm_bindings_ok_p in fn_type_unification
20536 for more information. */
20537 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
20538 return NULL_TREE;
20539
20540 return deduced_args;
20541 }
20542
20543 // Compare two function templates T1 and T2 by deducing bindings
20544 // from one against the other. If both deductions succeed, compare
20545 // constraints to see which is more constrained.
20546 static int
20547 more_specialized_inst (tree t1, tree t2)
20548 {
20549 int fate = 0;
20550 int count = 0;
20551
20552 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
20553 {
20554 --fate;
20555 ++count;
20556 }
20557
20558 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
20559 {
20560 ++fate;
20561 ++count;
20562 }
20563
20564 // If both deductions succeed, then one may be more constrained.
20565 if (count == 2 && fate == 0)
20566 fate = more_constrained (t1, t2);
20567
20568 return fate;
20569 }
20570
20571 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20572 Return the TREE_LIST node with the most specialized template, if
20573 any. If there is no most specialized template, the error_mark_node
20574 is returned.
20575
20576 Note that this function does not look at, or modify, the
20577 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20578 returned is one of the elements of INSTANTIATIONS, callers may
20579 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20580 and retrieve it from the value returned. */
20581
20582 tree
20583 most_specialized_instantiation (tree templates)
20584 {
20585 tree fn, champ;
20586
20587 ++processing_template_decl;
20588
20589 champ = templates;
20590 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
20591 {
20592 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
20593 if (fate == -1)
20594 champ = fn;
20595 else if (!fate)
20596 {
20597 /* Equally specialized, move to next function. If there
20598 is no next function, nothing's most specialized. */
20599 fn = TREE_CHAIN (fn);
20600 champ = fn;
20601 if (!fn)
20602 break;
20603 }
20604 }
20605
20606 if (champ)
20607 /* Now verify that champ is better than everything earlier in the
20608 instantiation list. */
20609 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
20610 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
20611 {
20612 champ = NULL_TREE;
20613 break;
20614 }
20615 }
20616
20617 processing_template_decl--;
20618
20619 if (!champ)
20620 return error_mark_node;
20621
20622 return champ;
20623 }
20624
20625 /* If DECL is a specialization of some template, return the most
20626 general such template. Otherwise, returns NULL_TREE.
20627
20628 For example, given:
20629
20630 template <class T> struct S { template <class U> void f(U); };
20631
20632 if TMPL is `template <class U> void S<int>::f(U)' this will return
20633 the full template. This function will not trace past partial
20634 specializations, however. For example, given in addition:
20635
20636 template <class T> struct S<T*> { template <class U> void f(U); };
20637
20638 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20639 `template <class T> template <class U> S<T*>::f(U)'. */
20640
20641 tree
20642 most_general_template (tree decl)
20643 {
20644 if (TREE_CODE (decl) != TEMPLATE_DECL)
20645 {
20646 if (tree tinfo = get_template_info (decl))
20647 decl = TI_TEMPLATE (tinfo);
20648 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20649 template friend, or a FIELD_DECL for a capture pack. */
20650 if (TREE_CODE (decl) != TEMPLATE_DECL)
20651 return NULL_TREE;
20652 }
20653
20654 /* Look for more and more general templates. */
20655 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
20656 {
20657 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20658 (See cp-tree.h for details.) */
20659 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
20660 break;
20661
20662 if (CLASS_TYPE_P (TREE_TYPE (decl))
20663 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
20664 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
20665 break;
20666
20667 /* Stop if we run into an explicitly specialized class template. */
20668 if (!DECL_NAMESPACE_SCOPE_P (decl)
20669 && DECL_CONTEXT (decl)
20670 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
20671 break;
20672
20673 decl = DECL_TI_TEMPLATE (decl);
20674 }
20675
20676 return decl;
20677 }
20678
20679 /* Return the most specialized of the template partial specializations
20680 which can produce TARGET, a specialization of some class or variable
20681 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
20682 a TEMPLATE_DECL node corresponding to the partial specialization, while
20683 the TREE_PURPOSE is the set of template arguments that must be
20684 substituted into the template pattern in order to generate TARGET.
20685
20686 If the choice of partial specialization is ambiguous, a diagnostic
20687 is issued, and the error_mark_node is returned. If there are no
20688 partial specializations matching TARGET, then NULL_TREE is
20689 returned, indicating that the primary template should be used. */
20690
20691 static tree
20692 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
20693 {
20694 tree list = NULL_TREE;
20695 tree t;
20696 tree champ;
20697 int fate;
20698 bool ambiguous_p;
20699 tree outer_args = NULL_TREE;
20700 tree tmpl, args;
20701
20702 if (TYPE_P (target))
20703 {
20704 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
20705 tmpl = TI_TEMPLATE (tinfo);
20706 args = TI_ARGS (tinfo);
20707 }
20708 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
20709 {
20710 tmpl = TREE_OPERAND (target, 0);
20711 args = TREE_OPERAND (target, 1);
20712 }
20713 else if (VAR_P (target))
20714 {
20715 tree tinfo = DECL_TEMPLATE_INFO (target);
20716 tmpl = TI_TEMPLATE (tinfo);
20717 args = TI_ARGS (tinfo);
20718 }
20719 else
20720 gcc_unreachable ();
20721
20722 tree main_tmpl = most_general_template (tmpl);
20723
20724 /* For determining which partial specialization to use, only the
20725 innermost args are interesting. */
20726 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20727 {
20728 outer_args = strip_innermost_template_args (args, 1);
20729 args = INNERMOST_TEMPLATE_ARGS (args);
20730 }
20731
20732 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
20733 {
20734 tree partial_spec_args;
20735 tree spec_args;
20736 tree spec_tmpl = TREE_VALUE (t);
20737
20738 partial_spec_args = TREE_PURPOSE (t);
20739
20740 ++processing_template_decl;
20741
20742 if (outer_args)
20743 {
20744 /* Discard the outer levels of args, and then substitute in the
20745 template args from the enclosing class. */
20746 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
20747 partial_spec_args = tsubst_template_args
20748 (partial_spec_args, outer_args, tf_none, NULL_TREE);
20749
20750 /* And the same for the partial specialization TEMPLATE_DECL. */
20751 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
20752 }
20753
20754 partial_spec_args =
20755 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20756 partial_spec_args,
20757 tmpl, tf_none,
20758 /*require_all_args=*/true,
20759 /*use_default_args=*/true);
20760
20761 --processing_template_decl;
20762
20763 if (partial_spec_args == error_mark_node)
20764 return error_mark_node;
20765 if (spec_tmpl == error_mark_node)
20766 return error_mark_node;
20767
20768 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
20769 spec_args = get_partial_spec_bindings (tmpl, parms,
20770 partial_spec_args,
20771 args);
20772 if (spec_args)
20773 {
20774 if (outer_args)
20775 spec_args = add_to_template_args (outer_args, spec_args);
20776
20777 /* Keep the candidate only if the constraints are satisfied,
20778 or if we're not compiling with concepts. */
20779 if (!flag_concepts
20780 || constraints_satisfied_p (spec_tmpl, spec_args))
20781 {
20782 list = tree_cons (spec_args, TREE_VALUE (t), list);
20783 TREE_TYPE (list) = TREE_TYPE (t);
20784 }
20785 }
20786 }
20787
20788 if (! list)
20789 return NULL_TREE;
20790
20791 ambiguous_p = false;
20792 t = list;
20793 champ = t;
20794 t = TREE_CHAIN (t);
20795 for (; t; t = TREE_CHAIN (t))
20796 {
20797 fate = more_specialized_partial_spec (tmpl, champ, t);
20798 if (fate == 1)
20799 ;
20800 else
20801 {
20802 if (fate == 0)
20803 {
20804 t = TREE_CHAIN (t);
20805 if (! t)
20806 {
20807 ambiguous_p = true;
20808 break;
20809 }
20810 }
20811 champ = t;
20812 }
20813 }
20814
20815 if (!ambiguous_p)
20816 for (t = list; t && t != champ; t = TREE_CHAIN (t))
20817 {
20818 fate = more_specialized_partial_spec (tmpl, champ, t);
20819 if (fate != 1)
20820 {
20821 ambiguous_p = true;
20822 break;
20823 }
20824 }
20825
20826 if (ambiguous_p)
20827 {
20828 const char *str;
20829 char *spaces = NULL;
20830 if (!(complain & tf_error))
20831 return error_mark_node;
20832 if (TYPE_P (target))
20833 error ("ambiguous template instantiation for %q#T", target);
20834 else
20835 error ("ambiguous template instantiation for %q#D", target);
20836 str = ngettext ("candidate is:", "candidates are:", list_length (list));
20837 for (t = list; t; t = TREE_CHAIN (t))
20838 {
20839 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
20840 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
20841 "%s %#S", spaces ? spaces : str, subst);
20842 spaces = spaces ? spaces : get_spaces (str);
20843 }
20844 free (spaces);
20845 return error_mark_node;
20846 }
20847
20848 return champ;
20849 }
20850
20851 /* Explicitly instantiate DECL. */
20852
20853 void
20854 do_decl_instantiation (tree decl, tree storage)
20855 {
20856 tree result = NULL_TREE;
20857 int extern_p = 0;
20858
20859 if (!decl || decl == error_mark_node)
20860 /* An error occurred, for which grokdeclarator has already issued
20861 an appropriate message. */
20862 return;
20863 else if (! DECL_LANG_SPECIFIC (decl))
20864 {
20865 error ("explicit instantiation of non-template %q#D", decl);
20866 return;
20867 }
20868
20869 bool var_templ = (DECL_TEMPLATE_INFO (decl)
20870 && variable_template_p (DECL_TI_TEMPLATE (decl)));
20871
20872 if (VAR_P (decl) && !var_templ)
20873 {
20874 /* There is an asymmetry here in the way VAR_DECLs and
20875 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20876 the latter, the DECL we get back will be marked as a
20877 template instantiation, and the appropriate
20878 DECL_TEMPLATE_INFO will be set up. This does not happen for
20879 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20880 should handle VAR_DECLs as it currently handles
20881 FUNCTION_DECLs. */
20882 if (!DECL_CLASS_SCOPE_P (decl))
20883 {
20884 error ("%qD is not a static data member of a class template", decl);
20885 return;
20886 }
20887 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
20888 if (!result || !VAR_P (result))
20889 {
20890 error ("no matching template for %qD found", decl);
20891 return;
20892 }
20893 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
20894 {
20895 error ("type %qT for explicit instantiation %qD does not match "
20896 "declared type %qT", TREE_TYPE (result), decl,
20897 TREE_TYPE (decl));
20898 return;
20899 }
20900 }
20901 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
20902 {
20903 error ("explicit instantiation of %q#D", decl);
20904 return;
20905 }
20906 else
20907 result = decl;
20908
20909 /* Check for various error cases. Note that if the explicit
20910 instantiation is valid the RESULT will currently be marked as an
20911 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20912 until we get here. */
20913
20914 if (DECL_TEMPLATE_SPECIALIZATION (result))
20915 {
20916 /* DR 259 [temp.spec].
20917
20918 Both an explicit instantiation and a declaration of an explicit
20919 specialization shall not appear in a program unless the explicit
20920 instantiation follows a declaration of the explicit specialization.
20921
20922 For a given set of template parameters, if an explicit
20923 instantiation of a template appears after a declaration of an
20924 explicit specialization for that template, the explicit
20925 instantiation has no effect. */
20926 return;
20927 }
20928 else if (DECL_EXPLICIT_INSTANTIATION (result))
20929 {
20930 /* [temp.spec]
20931
20932 No program shall explicitly instantiate any template more
20933 than once.
20934
20935 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20936 the first instantiation was `extern' and the second is not,
20937 and EXTERN_P for the opposite case. */
20938 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
20939 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
20940 /* If an "extern" explicit instantiation follows an ordinary
20941 explicit instantiation, the template is instantiated. */
20942 if (extern_p)
20943 return;
20944 }
20945 else if (!DECL_IMPLICIT_INSTANTIATION (result))
20946 {
20947 error ("no matching template for %qD found", result);
20948 return;
20949 }
20950 else if (!DECL_TEMPLATE_INFO (result))
20951 {
20952 permerror (input_location, "explicit instantiation of non-template %q#D", result);
20953 return;
20954 }
20955
20956 if (storage == NULL_TREE)
20957 ;
20958 else if (storage == ridpointers[(int) RID_EXTERN])
20959 {
20960 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
20961 pedwarn (input_location, OPT_Wpedantic,
20962 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20963 "instantiations");
20964 extern_p = 1;
20965 }
20966 else
20967 error ("storage class %qD applied to template instantiation", storage);
20968
20969 check_explicit_instantiation_namespace (result);
20970 mark_decl_instantiated (result, extern_p);
20971 if (! extern_p)
20972 instantiate_decl (result, /*defer_ok=*/1,
20973 /*expl_inst_class_mem_p=*/false);
20974 }
20975
20976 static void
20977 mark_class_instantiated (tree t, int extern_p)
20978 {
20979 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
20980 SET_CLASSTYPE_INTERFACE_KNOWN (t);
20981 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
20982 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
20983 if (! extern_p)
20984 {
20985 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
20986 rest_of_type_compilation (t, 1);
20987 }
20988 }
20989
20990 /* Called from do_type_instantiation through binding_table_foreach to
20991 do recursive instantiation for the type bound in ENTRY. */
20992 static void
20993 bt_instantiate_type_proc (binding_entry entry, void *data)
20994 {
20995 tree storage = *(tree *) data;
20996
20997 if (MAYBE_CLASS_TYPE_P (entry->type)
20998 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
20999 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
21000 }
21001
21002 /* Called from do_type_instantiation to instantiate a member
21003 (a member function or a static member variable) of an
21004 explicitly instantiated class template. */
21005 static void
21006 instantiate_class_member (tree decl, int extern_p)
21007 {
21008 mark_decl_instantiated (decl, extern_p);
21009 if (! extern_p)
21010 instantiate_decl (decl, /*defer_ok=*/1,
21011 /*expl_inst_class_mem_p=*/true);
21012 }
21013
21014 /* Perform an explicit instantiation of template class T. STORAGE, if
21015 non-null, is the RID for extern, inline or static. COMPLAIN is
21016 nonzero if this is called from the parser, zero if called recursively,
21017 since the standard is unclear (as detailed below). */
21018
21019 void
21020 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
21021 {
21022 int extern_p = 0;
21023 int nomem_p = 0;
21024 int static_p = 0;
21025 int previous_instantiation_extern_p = 0;
21026
21027 if (TREE_CODE (t) == TYPE_DECL)
21028 t = TREE_TYPE (t);
21029
21030 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
21031 {
21032 tree tmpl =
21033 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
21034 if (tmpl)
21035 error ("explicit instantiation of non-class template %qD", tmpl);
21036 else
21037 error ("explicit instantiation of non-template type %qT", t);
21038 return;
21039 }
21040
21041 complete_type (t);
21042
21043 if (!COMPLETE_TYPE_P (t))
21044 {
21045 if (complain & tf_error)
21046 error ("explicit instantiation of %q#T before definition of template",
21047 t);
21048 return;
21049 }
21050
21051 if (storage != NULL_TREE)
21052 {
21053 if (!in_system_header_at (input_location))
21054 {
21055 if (storage == ridpointers[(int) RID_EXTERN])
21056 {
21057 if (cxx_dialect == cxx98)
21058 pedwarn (input_location, OPT_Wpedantic,
21059 "ISO C++ 1998 forbids the use of %<extern%> on "
21060 "explicit instantiations");
21061 }
21062 else
21063 pedwarn (input_location, OPT_Wpedantic,
21064 "ISO C++ forbids the use of %qE"
21065 " on explicit instantiations", storage);
21066 }
21067
21068 if (storage == ridpointers[(int) RID_INLINE])
21069 nomem_p = 1;
21070 else if (storage == ridpointers[(int) RID_EXTERN])
21071 extern_p = 1;
21072 else if (storage == ridpointers[(int) RID_STATIC])
21073 static_p = 1;
21074 else
21075 {
21076 error ("storage class %qD applied to template instantiation",
21077 storage);
21078 extern_p = 0;
21079 }
21080 }
21081
21082 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
21083 {
21084 /* DR 259 [temp.spec].
21085
21086 Both an explicit instantiation and a declaration of an explicit
21087 specialization shall not appear in a program unless the explicit
21088 instantiation follows a declaration of the explicit specialization.
21089
21090 For a given set of template parameters, if an explicit
21091 instantiation of a template appears after a declaration of an
21092 explicit specialization for that template, the explicit
21093 instantiation has no effect. */
21094 return;
21095 }
21096 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
21097 {
21098 /* [temp.spec]
21099
21100 No program shall explicitly instantiate any template more
21101 than once.
21102
21103 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21104 instantiation was `extern'. If EXTERN_P then the second is.
21105 These cases are OK. */
21106 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
21107
21108 if (!previous_instantiation_extern_p && !extern_p
21109 && (complain & tf_error))
21110 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
21111
21112 /* If we've already instantiated the template, just return now. */
21113 if (!CLASSTYPE_INTERFACE_ONLY (t))
21114 return;
21115 }
21116
21117 check_explicit_instantiation_namespace (TYPE_NAME (t));
21118 mark_class_instantiated (t, extern_p);
21119
21120 if (nomem_p)
21121 return;
21122
21123 {
21124 tree tmp;
21125
21126 /* In contrast to implicit instantiation, where only the
21127 declarations, and not the definitions, of members are
21128 instantiated, we have here:
21129
21130 [temp.explicit]
21131
21132 The explicit instantiation of a class template specialization
21133 implies the instantiation of all of its members not
21134 previously explicitly specialized in the translation unit
21135 containing the explicit instantiation.
21136
21137 Of course, we can't instantiate member template classes, since
21138 we don't have any arguments for them. Note that the standard
21139 is unclear on whether the instantiation of the members are
21140 *explicit* instantiations or not. However, the most natural
21141 interpretation is that it should be an explicit instantiation. */
21142
21143 if (! static_p)
21144 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
21145 if (TREE_CODE (tmp) == FUNCTION_DECL
21146 && DECL_TEMPLATE_INSTANTIATION (tmp))
21147 instantiate_class_member (tmp, extern_p);
21148
21149 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
21150 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
21151 instantiate_class_member (tmp, extern_p);
21152
21153 if (CLASSTYPE_NESTED_UTDS (t))
21154 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
21155 bt_instantiate_type_proc, &storage);
21156 }
21157 }
21158
21159 /* Given a function DECL, which is a specialization of TMPL, modify
21160 DECL to be a re-instantiation of TMPL with the same template
21161 arguments. TMPL should be the template into which tsubst'ing
21162 should occur for DECL, not the most general template.
21163
21164 One reason for doing this is a scenario like this:
21165
21166 template <class T>
21167 void f(const T&, int i);
21168
21169 void g() { f(3, 7); }
21170
21171 template <class T>
21172 void f(const T& t, const int i) { }
21173
21174 Note that when the template is first instantiated, with
21175 instantiate_template, the resulting DECL will have no name for the
21176 first parameter, and the wrong type for the second. So, when we go
21177 to instantiate the DECL, we regenerate it. */
21178
21179 static void
21180 regenerate_decl_from_template (tree decl, tree tmpl)
21181 {
21182 /* The arguments used to instantiate DECL, from the most general
21183 template. */
21184 tree args;
21185 tree code_pattern;
21186
21187 args = DECL_TI_ARGS (decl);
21188 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
21189
21190 /* Make sure that we can see identifiers, and compute access
21191 correctly. */
21192 push_access_scope (decl);
21193
21194 if (TREE_CODE (decl) == FUNCTION_DECL)
21195 {
21196 tree decl_parm;
21197 tree pattern_parm;
21198 tree specs;
21199 int args_depth;
21200 int parms_depth;
21201
21202 args_depth = TMPL_ARGS_DEPTH (args);
21203 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
21204 if (args_depth > parms_depth)
21205 args = get_innermost_template_args (args, parms_depth);
21206
21207 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
21208 args, tf_error, NULL_TREE,
21209 /*defer_ok*/false);
21210 if (specs && specs != error_mark_node)
21211 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
21212 specs);
21213
21214 /* Merge parameter declarations. */
21215 decl_parm = skip_artificial_parms_for (decl,
21216 DECL_ARGUMENTS (decl));
21217 pattern_parm
21218 = skip_artificial_parms_for (code_pattern,
21219 DECL_ARGUMENTS (code_pattern));
21220 while (decl_parm && !DECL_PACK_P (pattern_parm))
21221 {
21222 tree parm_type;
21223 tree attributes;
21224
21225 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21226 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
21227 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
21228 NULL_TREE);
21229 parm_type = type_decays_to (parm_type);
21230 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21231 TREE_TYPE (decl_parm) = parm_type;
21232 attributes = DECL_ATTRIBUTES (pattern_parm);
21233 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21234 {
21235 DECL_ATTRIBUTES (decl_parm) = attributes;
21236 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21237 }
21238 decl_parm = DECL_CHAIN (decl_parm);
21239 pattern_parm = DECL_CHAIN (pattern_parm);
21240 }
21241 /* Merge any parameters that match with the function parameter
21242 pack. */
21243 if (pattern_parm && DECL_PACK_P (pattern_parm))
21244 {
21245 int i, len;
21246 tree expanded_types;
21247 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21248 the parameters in this function parameter pack. */
21249 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
21250 args, tf_error, NULL_TREE);
21251 len = TREE_VEC_LENGTH (expanded_types);
21252 for (i = 0; i < len; i++)
21253 {
21254 tree parm_type;
21255 tree attributes;
21256
21257 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21258 /* Rename the parameter to include the index. */
21259 DECL_NAME (decl_parm) =
21260 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
21261 parm_type = TREE_VEC_ELT (expanded_types, i);
21262 parm_type = type_decays_to (parm_type);
21263 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21264 TREE_TYPE (decl_parm) = parm_type;
21265 attributes = DECL_ATTRIBUTES (pattern_parm);
21266 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21267 {
21268 DECL_ATTRIBUTES (decl_parm) = attributes;
21269 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21270 }
21271 decl_parm = DECL_CHAIN (decl_parm);
21272 }
21273 }
21274 /* Merge additional specifiers from the CODE_PATTERN. */
21275 if (DECL_DECLARED_INLINE_P (code_pattern)
21276 && !DECL_DECLARED_INLINE_P (decl))
21277 DECL_DECLARED_INLINE_P (decl) = 1;
21278 }
21279 else if (VAR_P (decl))
21280 {
21281 DECL_INITIAL (decl) =
21282 tsubst_expr (DECL_INITIAL (code_pattern), args,
21283 tf_error, DECL_TI_TEMPLATE (decl),
21284 /*integral_constant_expression_p=*/false);
21285 if (VAR_HAD_UNKNOWN_BOUND (decl))
21286 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
21287 tf_error, DECL_TI_TEMPLATE (decl));
21288 }
21289 else
21290 gcc_unreachable ();
21291
21292 pop_access_scope (decl);
21293 }
21294
21295 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21296 substituted to get DECL. */
21297
21298 tree
21299 template_for_substitution (tree decl)
21300 {
21301 tree tmpl = DECL_TI_TEMPLATE (decl);
21302
21303 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21304 for the instantiation. This is not always the most general
21305 template. Consider, for example:
21306
21307 template <class T>
21308 struct S { template <class U> void f();
21309 template <> void f<int>(); };
21310
21311 and an instantiation of S<double>::f<int>. We want TD to be the
21312 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21313 while (/* An instantiation cannot have a definition, so we need a
21314 more general template. */
21315 DECL_TEMPLATE_INSTANTIATION (tmpl)
21316 /* We must also deal with friend templates. Given:
21317
21318 template <class T> struct S {
21319 template <class U> friend void f() {};
21320 };
21321
21322 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21323 so far as the language is concerned, but that's still
21324 where we get the pattern for the instantiation from. On
21325 other hand, if the definition comes outside the class, say:
21326
21327 template <class T> struct S {
21328 template <class U> friend void f();
21329 };
21330 template <class U> friend void f() {}
21331
21332 we don't need to look any further. That's what the check for
21333 DECL_INITIAL is for. */
21334 || (TREE_CODE (decl) == FUNCTION_DECL
21335 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
21336 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
21337 {
21338 /* The present template, TD, should not be a definition. If it
21339 were a definition, we should be using it! Note that we
21340 cannot restructure the loop to just keep going until we find
21341 a template with a definition, since that might go too far if
21342 a specialization was declared, but not defined. */
21343
21344 /* Fetch the more general template. */
21345 tmpl = DECL_TI_TEMPLATE (tmpl);
21346 }
21347
21348 return tmpl;
21349 }
21350
21351 /* Returns true if we need to instantiate this template instance even if we
21352 know we aren't going to emit it. */
21353
21354 bool
21355 always_instantiate_p (tree decl)
21356 {
21357 /* We always instantiate inline functions so that we can inline them. An
21358 explicit instantiation declaration prohibits implicit instantiation of
21359 non-inline functions. With high levels of optimization, we would
21360 normally inline non-inline functions -- but we're not allowed to do
21361 that for "extern template" functions. Therefore, we check
21362 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21363 return ((TREE_CODE (decl) == FUNCTION_DECL
21364 && (DECL_DECLARED_INLINE_P (decl)
21365 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
21366 /* And we need to instantiate static data members so that
21367 their initializers are available in integral constant
21368 expressions. */
21369 || (VAR_P (decl)
21370 && decl_maybe_constant_var_p (decl)));
21371 }
21372
21373 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21374 instantiate it now, modifying TREE_TYPE (fn). */
21375
21376 void
21377 maybe_instantiate_noexcept (tree fn)
21378 {
21379 tree fntype, spec, noex, clone;
21380
21381 /* Don't instantiate a noexcept-specification from template context. */
21382 if (processing_template_decl)
21383 return;
21384
21385 if (DECL_CLONED_FUNCTION_P (fn))
21386 fn = DECL_CLONED_FUNCTION (fn);
21387 fntype = TREE_TYPE (fn);
21388 spec = TYPE_RAISES_EXCEPTIONS (fntype);
21389
21390 if (!spec || !TREE_PURPOSE (spec))
21391 return;
21392
21393 noex = TREE_PURPOSE (spec);
21394
21395 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
21396 {
21397 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
21398 spec = get_defaulted_eh_spec (fn);
21399 else if (push_tinst_level (fn))
21400 {
21401 push_access_scope (fn);
21402 push_deferring_access_checks (dk_no_deferred);
21403 input_location = DECL_SOURCE_LOCATION (fn);
21404 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
21405 DEFERRED_NOEXCEPT_ARGS (noex),
21406 tf_warning_or_error, fn,
21407 /*function_p=*/false,
21408 /*integral_constant_expression_p=*/true);
21409 pop_deferring_access_checks ();
21410 pop_access_scope (fn);
21411 pop_tinst_level ();
21412 spec = build_noexcept_spec (noex, tf_warning_or_error);
21413 if (spec == error_mark_node)
21414 spec = noexcept_false_spec;
21415 }
21416 else
21417 spec = noexcept_false_spec;
21418
21419 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
21420 }
21421
21422 FOR_EACH_CLONE (clone, fn)
21423 {
21424 if (TREE_TYPE (clone) == fntype)
21425 TREE_TYPE (clone) = TREE_TYPE (fn);
21426 else
21427 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
21428 }
21429 }
21430
21431 /* Produce the definition of D, a _DECL generated from a template. If
21432 DEFER_OK is nonzero, then we don't have to actually do the
21433 instantiation now; we just have to do it sometime. Normally it is
21434 an error if this is an explicit instantiation but D is undefined.
21435 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21436 explicitly instantiated class template. */
21437
21438 tree
21439 instantiate_decl (tree d, int defer_ok,
21440 bool expl_inst_class_mem_p)
21441 {
21442 tree tmpl = DECL_TI_TEMPLATE (d);
21443 tree gen_args;
21444 tree args;
21445 tree td;
21446 tree code_pattern;
21447 tree spec;
21448 tree gen_tmpl;
21449 bool pattern_defined;
21450 location_t saved_loc = input_location;
21451 int saved_unevaluated_operand = cp_unevaluated_operand;
21452 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21453 bool external_p;
21454 bool deleted_p;
21455 tree fn_context;
21456 bool nested = false;
21457
21458 /* This function should only be used to instantiate templates for
21459 functions and static member variables. */
21460 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
21461
21462 /* A concept is never instantiated. */
21463 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
21464
21465 /* Variables are never deferred; if instantiation is required, they
21466 are instantiated right away. That allows for better code in the
21467 case that an expression refers to the value of the variable --
21468 if the variable has a constant value the referring expression can
21469 take advantage of that fact. */
21470 if (VAR_P (d)
21471 || DECL_DECLARED_CONSTEXPR_P (d))
21472 defer_ok = 0;
21473
21474 /* Don't instantiate cloned functions. Instead, instantiate the
21475 functions they cloned. */
21476 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
21477 d = DECL_CLONED_FUNCTION (d);
21478
21479 if (DECL_TEMPLATE_INSTANTIATED (d)
21480 || (TREE_CODE (d) == FUNCTION_DECL
21481 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
21482 || DECL_TEMPLATE_SPECIALIZATION (d))
21483 /* D has already been instantiated or explicitly specialized, so
21484 there's nothing for us to do here.
21485
21486 It might seem reasonable to check whether or not D is an explicit
21487 instantiation, and, if so, stop here. But when an explicit
21488 instantiation is deferred until the end of the compilation,
21489 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21490 the instantiation. */
21491 return d;
21492
21493 /* Check to see whether we know that this template will be
21494 instantiated in some other file, as with "extern template"
21495 extension. */
21496 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
21497
21498 /* In general, we do not instantiate such templates. */
21499 if (external_p && !always_instantiate_p (d))
21500 return d;
21501
21502 gen_tmpl = most_general_template (tmpl);
21503 gen_args = DECL_TI_ARGS (d);
21504
21505 if (tmpl != gen_tmpl)
21506 /* We should already have the extra args. */
21507 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
21508 == TMPL_ARGS_DEPTH (gen_args));
21509 /* And what's in the hash table should match D. */
21510 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
21511 || spec == NULL_TREE);
21512
21513 /* This needs to happen before any tsubsting. */
21514 if (! push_tinst_level (d))
21515 return d;
21516
21517 timevar_push (TV_TEMPLATE_INST);
21518
21519 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21520 for the instantiation. */
21521 td = template_for_substitution (d);
21522 code_pattern = DECL_TEMPLATE_RESULT (td);
21523
21524 /* We should never be trying to instantiate a member of a class
21525 template or partial specialization. */
21526 gcc_assert (d != code_pattern);
21527
21528 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
21529 || DECL_TEMPLATE_SPECIALIZATION (td))
21530 /* In the case of a friend template whose definition is provided
21531 outside the class, we may have too many arguments. Drop the
21532 ones we don't need. The same is true for specializations. */
21533 args = get_innermost_template_args
21534 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
21535 else
21536 args = gen_args;
21537
21538 if (TREE_CODE (d) == FUNCTION_DECL)
21539 {
21540 deleted_p = DECL_DELETED_FN (code_pattern);
21541 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
21542 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
21543 || deleted_p);
21544 }
21545 else
21546 {
21547 deleted_p = false;
21548 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
21549 }
21550
21551 /* We may be in the middle of deferred access check. Disable it now. */
21552 push_deferring_access_checks (dk_no_deferred);
21553
21554 /* Unless an explicit instantiation directive has already determined
21555 the linkage of D, remember that a definition is available for
21556 this entity. */
21557 if (pattern_defined
21558 && !DECL_INTERFACE_KNOWN (d)
21559 && !DECL_NOT_REALLY_EXTERN (d))
21560 mark_definable (d);
21561
21562 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
21563 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
21564 input_location = DECL_SOURCE_LOCATION (d);
21565
21566 /* If D is a member of an explicitly instantiated class template,
21567 and no definition is available, treat it like an implicit
21568 instantiation. */
21569 if (!pattern_defined && expl_inst_class_mem_p
21570 && DECL_EXPLICIT_INSTANTIATION (d))
21571 {
21572 /* Leave linkage flags alone on instantiations with anonymous
21573 visibility. */
21574 if (TREE_PUBLIC (d))
21575 {
21576 DECL_NOT_REALLY_EXTERN (d) = 0;
21577 DECL_INTERFACE_KNOWN (d) = 0;
21578 }
21579 SET_DECL_IMPLICIT_INSTANTIATION (d);
21580 }
21581
21582 /* Defer all other templates, unless we have been explicitly
21583 forbidden from doing so. */
21584 if (/* If there is no definition, we cannot instantiate the
21585 template. */
21586 ! pattern_defined
21587 /* If it's OK to postpone instantiation, do so. */
21588 || defer_ok
21589 /* If this is a static data member that will be defined
21590 elsewhere, we don't want to instantiate the entire data
21591 member, but we do want to instantiate the initializer so that
21592 we can substitute that elsewhere. */
21593 || (external_p && VAR_P (d))
21594 /* Handle here a deleted function too, avoid generating
21595 its body (c++/61080). */
21596 || deleted_p)
21597 {
21598 /* The definition of the static data member is now required so
21599 we must substitute the initializer. */
21600 if (VAR_P (d)
21601 && !DECL_INITIAL (d)
21602 && DECL_INITIAL (code_pattern))
21603 {
21604 tree ns;
21605 tree init;
21606 bool const_init = false;
21607 bool enter_context = DECL_CLASS_SCOPE_P (d);
21608
21609 ns = decl_namespace_context (d);
21610 push_nested_namespace (ns);
21611 if (enter_context)
21612 push_nested_class (DECL_CONTEXT (d));
21613 init = tsubst_expr (DECL_INITIAL (code_pattern),
21614 args,
21615 tf_warning_or_error, NULL_TREE,
21616 /*integral_constant_expression_p=*/false);
21617 /* If instantiating the initializer involved instantiating this
21618 again, don't call cp_finish_decl twice. */
21619 if (!DECL_INITIAL (d))
21620 {
21621 /* Make sure the initializer is still constant, in case of
21622 circular dependency (template/instantiate6.C). */
21623 const_init
21624 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21625 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
21626 /*asmspec_tree=*/NULL_TREE,
21627 LOOKUP_ONLYCONVERTING);
21628 }
21629 if (enter_context)
21630 pop_nested_class ();
21631 pop_nested_namespace (ns);
21632 }
21633
21634 /* We restore the source position here because it's used by
21635 add_pending_template. */
21636 input_location = saved_loc;
21637
21638 if (at_eof && !pattern_defined
21639 && DECL_EXPLICIT_INSTANTIATION (d)
21640 && DECL_NOT_REALLY_EXTERN (d))
21641 /* [temp.explicit]
21642
21643 The definition of a non-exported function template, a
21644 non-exported member function template, or a non-exported
21645 member function or static data member of a class template
21646 shall be present in every translation unit in which it is
21647 explicitly instantiated. */
21648 permerror (input_location, "explicit instantiation of %qD "
21649 "but no definition available", d);
21650
21651 /* If we're in unevaluated context, we just wanted to get the
21652 constant value; this isn't an odr use, so don't queue
21653 a full instantiation. */
21654 if (cp_unevaluated_operand != 0)
21655 goto out;
21656 /* ??? Historically, we have instantiated inline functions, even
21657 when marked as "extern template". */
21658 if (!(external_p && VAR_P (d)))
21659 add_pending_template (d);
21660 goto out;
21661 }
21662 /* Tell the repository that D is available in this translation unit
21663 -- and see if it is supposed to be instantiated here. */
21664 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
21665 {
21666 /* In a PCH file, despite the fact that the repository hasn't
21667 requested instantiation in the PCH it is still possible that
21668 an instantiation will be required in a file that includes the
21669 PCH. */
21670 if (pch_file)
21671 add_pending_template (d);
21672 /* Instantiate inline functions so that the inliner can do its
21673 job, even though we'll not be emitting a copy of this
21674 function. */
21675 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
21676 goto out;
21677 }
21678
21679 fn_context = decl_function_context (d);
21680 nested = (current_function_decl != NULL_TREE);
21681 vec<tree> omp_privatization_save;
21682 if (nested)
21683 save_omp_privatization_clauses (omp_privatization_save);
21684
21685 if (!fn_context)
21686 push_to_top_level ();
21687 else
21688 {
21689 if (nested)
21690 push_function_context ();
21691 cp_unevaluated_operand = 0;
21692 c_inhibit_evaluation_warnings = 0;
21693 }
21694
21695 /* Mark D as instantiated so that recursive calls to
21696 instantiate_decl do not try to instantiate it again. */
21697 DECL_TEMPLATE_INSTANTIATED (d) = 1;
21698
21699 /* Regenerate the declaration in case the template has been modified
21700 by a subsequent redeclaration. */
21701 regenerate_decl_from_template (d, td);
21702
21703 /* We already set the file and line above. Reset them now in case
21704 they changed as a result of calling regenerate_decl_from_template. */
21705 input_location = DECL_SOURCE_LOCATION (d);
21706
21707 if (VAR_P (d))
21708 {
21709 tree init;
21710 bool const_init = false;
21711
21712 /* Clear out DECL_RTL; whatever was there before may not be right
21713 since we've reset the type of the declaration. */
21714 SET_DECL_RTL (d, NULL);
21715 DECL_IN_AGGR_P (d) = 0;
21716
21717 /* The initializer is placed in DECL_INITIAL by
21718 regenerate_decl_from_template so we don't need to
21719 push/pop_access_scope again here. Pull it out so that
21720 cp_finish_decl can process it. */
21721 init = DECL_INITIAL (d);
21722 DECL_INITIAL (d) = NULL_TREE;
21723 DECL_INITIALIZED_P (d) = 0;
21724
21725 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
21726 initializer. That function will defer actual emission until
21727 we have a chance to determine linkage. */
21728 DECL_EXTERNAL (d) = 0;
21729
21730 /* Enter the scope of D so that access-checking works correctly. */
21731 bool enter_context = DECL_CLASS_SCOPE_P (d);
21732 if (enter_context)
21733 push_nested_class (DECL_CONTEXT (d));
21734
21735 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21736 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
21737
21738 if (enter_context)
21739 pop_nested_class ();
21740
21741 if (variable_template_p (td))
21742 note_variable_template_instantiation (d);
21743 }
21744 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
21745 synthesize_method (d);
21746 else if (TREE_CODE (d) == FUNCTION_DECL)
21747 {
21748 hash_map<tree, tree> *saved_local_specializations;
21749 tree subst_decl;
21750 tree tmpl_parm;
21751 tree spec_parm;
21752 tree block = NULL_TREE;
21753
21754 /* Save away the current list, in case we are instantiating one
21755 template from within the body of another. */
21756 saved_local_specializations = local_specializations;
21757
21758 /* Set up the list of local specializations. */
21759 local_specializations = new hash_map<tree, tree>;
21760
21761 /* Set up context. */
21762 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21763 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21764 block = push_stmt_list ();
21765 else
21766 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
21767
21768 /* Some typedefs referenced from within the template code need to be
21769 access checked at template instantiation time, i.e now. These
21770 types were added to the template at parsing time. Let's get those
21771 and perform the access checks then. */
21772 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
21773 gen_args);
21774
21775 /* Create substitution entries for the parameters. */
21776 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
21777 tmpl_parm = DECL_ARGUMENTS (subst_decl);
21778 spec_parm = DECL_ARGUMENTS (d);
21779 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
21780 {
21781 register_local_specialization (spec_parm, tmpl_parm);
21782 spec_parm = skip_artificial_parms_for (d, spec_parm);
21783 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
21784 }
21785 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
21786 {
21787 if (!DECL_PACK_P (tmpl_parm))
21788 {
21789 register_local_specialization (spec_parm, tmpl_parm);
21790 spec_parm = DECL_CHAIN (spec_parm);
21791 }
21792 else
21793 {
21794 /* Register the (value) argument pack as a specialization of
21795 TMPL_PARM, then move on. */
21796 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
21797 register_local_specialization (argpack, tmpl_parm);
21798 }
21799 }
21800 gcc_assert (!spec_parm);
21801
21802 /* Substitute into the body of the function. */
21803 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21804 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
21805 tf_warning_or_error, tmpl);
21806 else
21807 {
21808 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
21809 tf_warning_or_error, tmpl,
21810 /*integral_constant_expression_p=*/false);
21811
21812 /* Set the current input_location to the end of the function
21813 so that finish_function knows where we are. */
21814 input_location
21815 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
21816
21817 /* Remember if we saw an infinite loop in the template. */
21818 current_function_infinite_loop
21819 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
21820 }
21821
21822 /* We don't need the local specializations any more. */
21823 delete local_specializations;
21824 local_specializations = saved_local_specializations;
21825
21826 /* Finish the function. */
21827 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21828 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21829 DECL_SAVED_TREE (d) = pop_stmt_list (block);
21830 else
21831 {
21832 d = finish_function (0);
21833 expand_or_defer_fn (d);
21834 }
21835
21836 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21837 cp_check_omp_declare_reduction (d);
21838 }
21839
21840 /* We're not deferring instantiation any more. */
21841 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
21842
21843 if (!fn_context)
21844 pop_from_top_level ();
21845 else if (nested)
21846 pop_function_context ();
21847
21848 out:
21849 input_location = saved_loc;
21850 cp_unevaluated_operand = saved_unevaluated_operand;
21851 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21852 pop_deferring_access_checks ();
21853 pop_tinst_level ();
21854 if (nested)
21855 restore_omp_privatization_clauses (omp_privatization_save);
21856
21857 timevar_pop (TV_TEMPLATE_INST);
21858
21859 return d;
21860 }
21861
21862 /* Run through the list of templates that we wish we could
21863 instantiate, and instantiate any we can. RETRIES is the
21864 number of times we retry pending template instantiation. */
21865
21866 void
21867 instantiate_pending_templates (int retries)
21868 {
21869 int reconsider;
21870 location_t saved_loc = input_location;
21871
21872 /* Instantiating templates may trigger vtable generation. This in turn
21873 may require further template instantiations. We place a limit here
21874 to avoid infinite loop. */
21875 if (pending_templates && retries >= max_tinst_depth)
21876 {
21877 tree decl = pending_templates->tinst->decl;
21878
21879 fatal_error (input_location,
21880 "template instantiation depth exceeds maximum of %d"
21881 " instantiating %q+D, possibly from virtual table generation"
21882 " (use -ftemplate-depth= to increase the maximum)",
21883 max_tinst_depth, decl);
21884 if (TREE_CODE (decl) == FUNCTION_DECL)
21885 /* Pretend that we defined it. */
21886 DECL_INITIAL (decl) = error_mark_node;
21887 return;
21888 }
21889
21890 do
21891 {
21892 struct pending_template **t = &pending_templates;
21893 struct pending_template *last = NULL;
21894 reconsider = 0;
21895 while (*t)
21896 {
21897 tree instantiation = reopen_tinst_level ((*t)->tinst);
21898 bool complete = false;
21899
21900 if (TYPE_P (instantiation))
21901 {
21902 tree fn;
21903
21904 if (!COMPLETE_TYPE_P (instantiation))
21905 {
21906 instantiate_class_template (instantiation);
21907 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
21908 for (fn = TYPE_METHODS (instantiation);
21909 fn;
21910 fn = TREE_CHAIN (fn))
21911 if (! DECL_ARTIFICIAL (fn))
21912 instantiate_decl (fn,
21913 /*defer_ok=*/0,
21914 /*expl_inst_class_mem_p=*/false);
21915 if (COMPLETE_TYPE_P (instantiation))
21916 reconsider = 1;
21917 }
21918
21919 complete = COMPLETE_TYPE_P (instantiation);
21920 }
21921 else
21922 {
21923 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
21924 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
21925 {
21926 instantiation
21927 = instantiate_decl (instantiation,
21928 /*defer_ok=*/0,
21929 /*expl_inst_class_mem_p=*/false);
21930 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
21931 reconsider = 1;
21932 }
21933
21934 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
21935 || DECL_TEMPLATE_INSTANTIATED (instantiation));
21936 }
21937
21938 if (complete)
21939 /* If INSTANTIATION has been instantiated, then we don't
21940 need to consider it again in the future. */
21941 *t = (*t)->next;
21942 else
21943 {
21944 last = *t;
21945 t = &(*t)->next;
21946 }
21947 tinst_depth = 0;
21948 current_tinst_level = NULL;
21949 }
21950 last_pending_template = last;
21951 }
21952 while (reconsider);
21953
21954 input_location = saved_loc;
21955 }
21956
21957 /* Substitute ARGVEC into T, which is a list of initializers for
21958 either base class or a non-static data member. The TREE_PURPOSEs
21959 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21960 instantiate_decl. */
21961
21962 static tree
21963 tsubst_initializer_list (tree t, tree argvec)
21964 {
21965 tree inits = NULL_TREE;
21966
21967 for (; t; t = TREE_CHAIN (t))
21968 {
21969 tree decl;
21970 tree init;
21971 tree expanded_bases = NULL_TREE;
21972 tree expanded_arguments = NULL_TREE;
21973 int i, len = 1;
21974
21975 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
21976 {
21977 tree expr;
21978 tree arg;
21979
21980 /* Expand the base class expansion type into separate base
21981 classes. */
21982 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
21983 tf_warning_or_error,
21984 NULL_TREE);
21985 if (expanded_bases == error_mark_node)
21986 continue;
21987
21988 /* We'll be building separate TREE_LISTs of arguments for
21989 each base. */
21990 len = TREE_VEC_LENGTH (expanded_bases);
21991 expanded_arguments = make_tree_vec (len);
21992 for (i = 0; i < len; i++)
21993 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
21994
21995 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
21996 expand each argument in the TREE_VALUE of t. */
21997 expr = make_node (EXPR_PACK_EXPANSION);
21998 PACK_EXPANSION_LOCAL_P (expr) = true;
21999 PACK_EXPANSION_PARAMETER_PACKS (expr) =
22000 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
22001
22002 if (TREE_VALUE (t) == void_type_node)
22003 /* VOID_TYPE_NODE is used to indicate
22004 value-initialization. */
22005 {
22006 for (i = 0; i < len; i++)
22007 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
22008 }
22009 else
22010 {
22011 /* Substitute parameter packs into each argument in the
22012 TREE_LIST. */
22013 in_base_initializer = 1;
22014 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
22015 {
22016 tree expanded_exprs;
22017
22018 /* Expand the argument. */
22019 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
22020 expanded_exprs
22021 = tsubst_pack_expansion (expr, argvec,
22022 tf_warning_or_error,
22023 NULL_TREE);
22024 if (expanded_exprs == error_mark_node)
22025 continue;
22026
22027 /* Prepend each of the expanded expressions to the
22028 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22029 for (i = 0; i < len; i++)
22030 {
22031 TREE_VEC_ELT (expanded_arguments, i) =
22032 tree_cons (NULL_TREE,
22033 TREE_VEC_ELT (expanded_exprs, i),
22034 TREE_VEC_ELT (expanded_arguments, i));
22035 }
22036 }
22037 in_base_initializer = 0;
22038
22039 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22040 since we built them backwards. */
22041 for (i = 0; i < len; i++)
22042 {
22043 TREE_VEC_ELT (expanded_arguments, i) =
22044 nreverse (TREE_VEC_ELT (expanded_arguments, i));
22045 }
22046 }
22047 }
22048
22049 for (i = 0; i < len; ++i)
22050 {
22051 if (expanded_bases)
22052 {
22053 decl = TREE_VEC_ELT (expanded_bases, i);
22054 decl = expand_member_init (decl);
22055 init = TREE_VEC_ELT (expanded_arguments, i);
22056 }
22057 else
22058 {
22059 tree tmp;
22060 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
22061 tf_warning_or_error, NULL_TREE);
22062
22063 decl = expand_member_init (decl);
22064 if (decl && !DECL_P (decl))
22065 in_base_initializer = 1;
22066
22067 init = TREE_VALUE (t);
22068 tmp = init;
22069 if (init != void_type_node)
22070 init = tsubst_expr (init, argvec,
22071 tf_warning_or_error, NULL_TREE,
22072 /*integral_constant_expression_p=*/false);
22073 if (init == NULL_TREE && tmp != NULL_TREE)
22074 /* If we had an initializer but it instantiated to nothing,
22075 value-initialize the object. This will only occur when
22076 the initializer was a pack expansion where the parameter
22077 packs used in that expansion were of length zero. */
22078 init = void_type_node;
22079 in_base_initializer = 0;
22080 }
22081
22082 if (decl)
22083 {
22084 init = build_tree_list (decl, init);
22085 TREE_CHAIN (init) = inits;
22086 inits = init;
22087 }
22088 }
22089 }
22090 return inits;
22091 }
22092
22093 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22094
22095 static void
22096 set_current_access_from_decl (tree decl)
22097 {
22098 if (TREE_PRIVATE (decl))
22099 current_access_specifier = access_private_node;
22100 else if (TREE_PROTECTED (decl))
22101 current_access_specifier = access_protected_node;
22102 else
22103 current_access_specifier = access_public_node;
22104 }
22105
22106 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22107 is the instantiation (which should have been created with
22108 start_enum) and ARGS are the template arguments to use. */
22109
22110 static void
22111 tsubst_enum (tree tag, tree newtag, tree args)
22112 {
22113 tree e;
22114
22115 if (SCOPED_ENUM_P (newtag))
22116 begin_scope (sk_scoped_enum, newtag);
22117
22118 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
22119 {
22120 tree value;
22121 tree decl;
22122
22123 decl = TREE_VALUE (e);
22124 /* Note that in a template enum, the TREE_VALUE is the
22125 CONST_DECL, not the corresponding INTEGER_CST. */
22126 value = tsubst_expr (DECL_INITIAL (decl),
22127 args, tf_warning_or_error, NULL_TREE,
22128 /*integral_constant_expression_p=*/true);
22129
22130 /* Give this enumeration constant the correct access. */
22131 set_current_access_from_decl (decl);
22132
22133 /* Actually build the enumerator itself. Here we're assuming that
22134 enumerators can't have dependent attributes. */
22135 build_enumerator (DECL_NAME (decl), value, newtag,
22136 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
22137 }
22138
22139 if (SCOPED_ENUM_P (newtag))
22140 finish_scope ();
22141
22142 finish_enum_value_list (newtag);
22143 finish_enum (newtag);
22144
22145 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
22146 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
22147 }
22148
22149 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22150 its type -- but without substituting the innermost set of template
22151 arguments. So, innermost set of template parameters will appear in
22152 the type. */
22153
22154 tree
22155 get_mostly_instantiated_function_type (tree decl)
22156 {
22157 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22158 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
22159 }
22160
22161 /* Return truthvalue if we're processing a template different from
22162 the last one involved in diagnostics. */
22163 bool
22164 problematic_instantiation_changed (void)
22165 {
22166 return current_tinst_level != last_error_tinst_level;
22167 }
22168
22169 /* Remember current template involved in diagnostics. */
22170 void
22171 record_last_problematic_instantiation (void)
22172 {
22173 last_error_tinst_level = current_tinst_level;
22174 }
22175
22176 struct tinst_level *
22177 current_instantiation (void)
22178 {
22179 return current_tinst_level;
22180 }
22181
22182 /* Return TRUE if current_function_decl is being instantiated, false
22183 otherwise. */
22184
22185 bool
22186 instantiating_current_function_p (void)
22187 {
22188 return (current_instantiation ()
22189 && current_instantiation ()->decl == current_function_decl);
22190 }
22191
22192 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22193 type. Return zero for ok, nonzero for disallowed. Issue error and
22194 warning messages under control of COMPLAIN. */
22195
22196 static int
22197 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
22198 {
22199 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
22200 return 0;
22201 else if (POINTER_TYPE_P (type))
22202 return 0;
22203 else if (TYPE_PTRMEM_P (type))
22204 return 0;
22205 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
22206 return 0;
22207 else if (TREE_CODE (type) == TYPENAME_TYPE)
22208 return 0;
22209 else if (TREE_CODE (type) == DECLTYPE_TYPE)
22210 return 0;
22211 else if (TREE_CODE (type) == NULLPTR_TYPE)
22212 return 0;
22213 /* A bound template template parm could later be instantiated to have a valid
22214 nontype parm type via an alias template. */
22215 else if (cxx_dialect >= cxx11
22216 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22217 return 0;
22218
22219 if (complain & tf_error)
22220 {
22221 if (type == error_mark_node)
22222 inform (input_location, "invalid template non-type parameter");
22223 else
22224 error ("%q#T is not a valid type for a template non-type parameter",
22225 type);
22226 }
22227 return 1;
22228 }
22229
22230 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22231 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22232
22233 static bool
22234 dependent_type_p_r (tree type)
22235 {
22236 tree scope;
22237
22238 /* [temp.dep.type]
22239
22240 A type is dependent if it is:
22241
22242 -- a template parameter. Template template parameters are types
22243 for us (since TYPE_P holds true for them) so we handle
22244 them here. */
22245 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22246 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
22247 return true;
22248 /* -- a qualified-id with a nested-name-specifier which contains a
22249 class-name that names a dependent type or whose unqualified-id
22250 names a dependent type. */
22251 if (TREE_CODE (type) == TYPENAME_TYPE)
22252 return true;
22253
22254 /* An alias template specialization can be dependent even if the
22255 resulting type is not. */
22256 if (dependent_alias_template_spec_p (type))
22257 return true;
22258
22259 /* -- a cv-qualified type where the cv-unqualified type is
22260 dependent.
22261 No code is necessary for this bullet; the code below handles
22262 cv-qualified types, and we don't want to strip aliases with
22263 TYPE_MAIN_VARIANT because of DR 1558. */
22264 /* -- a compound type constructed from any dependent type. */
22265 if (TYPE_PTRMEM_P (type))
22266 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
22267 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22268 (type)));
22269 else if (TYPE_PTR_P (type)
22270 || TREE_CODE (type) == REFERENCE_TYPE)
22271 return dependent_type_p (TREE_TYPE (type));
22272 else if (TREE_CODE (type) == FUNCTION_TYPE
22273 || TREE_CODE (type) == METHOD_TYPE)
22274 {
22275 tree arg_type;
22276
22277 if (dependent_type_p (TREE_TYPE (type)))
22278 return true;
22279 for (arg_type = TYPE_ARG_TYPES (type);
22280 arg_type;
22281 arg_type = TREE_CHAIN (arg_type))
22282 if (dependent_type_p (TREE_VALUE (arg_type)))
22283 return true;
22284 return false;
22285 }
22286 /* -- an array type constructed from any dependent type or whose
22287 size is specified by a constant expression that is
22288 value-dependent.
22289
22290 We checked for type- and value-dependence of the bounds in
22291 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22292 if (TREE_CODE (type) == ARRAY_TYPE)
22293 {
22294 if (TYPE_DOMAIN (type)
22295 && dependent_type_p (TYPE_DOMAIN (type)))
22296 return true;
22297 return dependent_type_p (TREE_TYPE (type));
22298 }
22299
22300 /* -- a template-id in which either the template name is a template
22301 parameter ... */
22302 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22303 return true;
22304 /* ... or any of the template arguments is a dependent type or
22305 an expression that is type-dependent or value-dependent. */
22306 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
22307 && (any_dependent_template_arguments_p
22308 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
22309 return true;
22310
22311 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22312 dependent; if the argument of the `typeof' expression is not
22313 type-dependent, then it should already been have resolved. */
22314 if (TREE_CODE (type) == TYPEOF_TYPE
22315 || TREE_CODE (type) == DECLTYPE_TYPE
22316 || TREE_CODE (type) == UNDERLYING_TYPE)
22317 return true;
22318
22319 /* A template argument pack is dependent if any of its packed
22320 arguments are. */
22321 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
22322 {
22323 tree args = ARGUMENT_PACK_ARGS (type);
22324 int i, len = TREE_VEC_LENGTH (args);
22325 for (i = 0; i < len; ++i)
22326 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22327 return true;
22328 }
22329
22330 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22331 be template parameters. */
22332 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
22333 return true;
22334
22335 /* The standard does not specifically mention types that are local
22336 to template functions or local classes, but they should be
22337 considered dependent too. For example:
22338
22339 template <int I> void f() {
22340 enum E { a = I };
22341 S<sizeof (E)> s;
22342 }
22343
22344 The size of `E' cannot be known until the value of `I' has been
22345 determined. Therefore, `E' must be considered dependent. */
22346 scope = TYPE_CONTEXT (type);
22347 if (scope && TYPE_P (scope))
22348 return dependent_type_p (scope);
22349 /* Don't use type_dependent_expression_p here, as it can lead
22350 to infinite recursion trying to determine whether a lambda
22351 nested in a lambda is dependent (c++/47687). */
22352 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
22353 && DECL_LANG_SPECIFIC (scope)
22354 && DECL_TEMPLATE_INFO (scope)
22355 && (any_dependent_template_arguments_p
22356 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
22357 return true;
22358
22359 /* Other types are non-dependent. */
22360 return false;
22361 }
22362
22363 /* Returns TRUE if TYPE is dependent, in the sense of
22364 [temp.dep.type]. Note that a NULL type is considered dependent. */
22365
22366 bool
22367 dependent_type_p (tree type)
22368 {
22369 /* If there are no template parameters in scope, then there can't be
22370 any dependent types. */
22371 if (!processing_template_decl)
22372 {
22373 /* If we are not processing a template, then nobody should be
22374 providing us with a dependent type. */
22375 gcc_assert (type);
22376 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
22377 return false;
22378 }
22379
22380 /* If the type is NULL, we have not computed a type for the entity
22381 in question; in that case, the type is dependent. */
22382 if (!type)
22383 return true;
22384
22385 /* Erroneous types can be considered non-dependent. */
22386 if (type == error_mark_node)
22387 return false;
22388
22389 /* If we have not already computed the appropriate value for TYPE,
22390 do so now. */
22391 if (!TYPE_DEPENDENT_P_VALID (type))
22392 {
22393 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
22394 TYPE_DEPENDENT_P_VALID (type) = 1;
22395 }
22396
22397 return TYPE_DEPENDENT_P (type);
22398 }
22399
22400 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22401 lookup. In other words, a dependent type that is not the current
22402 instantiation. */
22403
22404 bool
22405 dependent_scope_p (tree scope)
22406 {
22407 return (scope && TYPE_P (scope) && dependent_type_p (scope)
22408 && !currently_open_class (scope));
22409 }
22410
22411 /* T is a SCOPE_REF; return whether we need to consider it
22412 instantiation-dependent so that we can check access at instantiation
22413 time even though we know which member it resolves to. */
22414
22415 static bool
22416 instantiation_dependent_scope_ref_p (tree t)
22417 {
22418 if (DECL_P (TREE_OPERAND (t, 1))
22419 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
22420 && accessible_in_template_p (TREE_OPERAND (t, 0),
22421 TREE_OPERAND (t, 1)))
22422 return false;
22423 else
22424 return true;
22425 }
22426
22427 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22428 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22429 expression. */
22430
22431 /* Note that this predicate is not appropriate for general expressions;
22432 only constant expressions (that satisfy potential_constant_expression)
22433 can be tested for value dependence. */
22434
22435 bool
22436 value_dependent_expression_p (tree expression)
22437 {
22438 if (!processing_template_decl)
22439 return false;
22440
22441 /* A name declared with a dependent type. */
22442 if (DECL_P (expression) && type_dependent_expression_p (expression))
22443 return true;
22444
22445 switch (TREE_CODE (expression))
22446 {
22447 case IDENTIFIER_NODE:
22448 /* A name that has not been looked up -- must be dependent. */
22449 return true;
22450
22451 case TEMPLATE_PARM_INDEX:
22452 /* A non-type template parm. */
22453 return true;
22454
22455 case CONST_DECL:
22456 /* A non-type template parm. */
22457 if (DECL_TEMPLATE_PARM_P (expression))
22458 return true;
22459 return value_dependent_expression_p (DECL_INITIAL (expression));
22460
22461 case VAR_DECL:
22462 /* A constant with literal type and is initialized
22463 with an expression that is value-dependent.
22464
22465 Note that a non-dependent parenthesized initializer will have
22466 already been replaced with its constant value, so if we see
22467 a TREE_LIST it must be dependent. */
22468 if (DECL_INITIAL (expression)
22469 && decl_constant_var_p (expression)
22470 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
22471 /* cp_finish_decl doesn't fold reference initializers. */
22472 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
22473 || value_dependent_expression_p (DECL_INITIAL (expression))))
22474 return true;
22475 return false;
22476
22477 case DYNAMIC_CAST_EXPR:
22478 case STATIC_CAST_EXPR:
22479 case CONST_CAST_EXPR:
22480 case REINTERPRET_CAST_EXPR:
22481 case CAST_EXPR:
22482 /* These expressions are value-dependent if the type to which
22483 the cast occurs is dependent or the expression being casted
22484 is value-dependent. */
22485 {
22486 tree type = TREE_TYPE (expression);
22487
22488 if (dependent_type_p (type))
22489 return true;
22490
22491 /* A functional cast has a list of operands. */
22492 expression = TREE_OPERAND (expression, 0);
22493 if (!expression)
22494 {
22495 /* If there are no operands, it must be an expression such
22496 as "int()". This should not happen for aggregate types
22497 because it would form non-constant expressions. */
22498 gcc_assert (cxx_dialect >= cxx11
22499 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
22500
22501 return false;
22502 }
22503
22504 if (TREE_CODE (expression) == TREE_LIST)
22505 return any_value_dependent_elements_p (expression);
22506
22507 return value_dependent_expression_p (expression);
22508 }
22509
22510 case SIZEOF_EXPR:
22511 if (SIZEOF_EXPR_TYPE_P (expression))
22512 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
22513 /* FALLTHRU */
22514 case ALIGNOF_EXPR:
22515 case TYPEID_EXPR:
22516 /* A `sizeof' expression is value-dependent if the operand is
22517 type-dependent or is a pack expansion. */
22518 expression = TREE_OPERAND (expression, 0);
22519 if (PACK_EXPANSION_P (expression))
22520 return true;
22521 else if (TYPE_P (expression))
22522 return dependent_type_p (expression);
22523 return instantiation_dependent_expression_p (expression);
22524
22525 case AT_ENCODE_EXPR:
22526 /* An 'encode' expression is value-dependent if the operand is
22527 type-dependent. */
22528 expression = TREE_OPERAND (expression, 0);
22529 return dependent_type_p (expression);
22530
22531 case NOEXCEPT_EXPR:
22532 expression = TREE_OPERAND (expression, 0);
22533 return instantiation_dependent_expression_p (expression);
22534
22535 case SCOPE_REF:
22536 /* All instantiation-dependent expressions should also be considered
22537 value-dependent. */
22538 return instantiation_dependent_scope_ref_p (expression);
22539
22540 case COMPONENT_REF:
22541 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
22542 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
22543
22544 case NONTYPE_ARGUMENT_PACK:
22545 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22546 is value-dependent. */
22547 {
22548 tree values = ARGUMENT_PACK_ARGS (expression);
22549 int i, len = TREE_VEC_LENGTH (values);
22550
22551 for (i = 0; i < len; ++i)
22552 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
22553 return true;
22554
22555 return false;
22556 }
22557
22558 case TRAIT_EXPR:
22559 {
22560 tree type2 = TRAIT_EXPR_TYPE2 (expression);
22561 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
22562 || (type2 ? dependent_type_p (type2) : false));
22563 }
22564
22565 case MODOP_EXPR:
22566 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22567 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
22568
22569 case ARRAY_REF:
22570 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22571 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
22572
22573 case ADDR_EXPR:
22574 {
22575 tree op = TREE_OPERAND (expression, 0);
22576 return (value_dependent_expression_p (op)
22577 || has_value_dependent_address (op));
22578 }
22579
22580 case REQUIRES_EXPR:
22581 /* Treat all requires-expressions as value-dependent so
22582 we don't try to fold them. */
22583 return true;
22584
22585 case TYPE_REQ:
22586 return dependent_type_p (TREE_OPERAND (expression, 0));
22587
22588 case CALL_EXPR:
22589 {
22590 tree fn = get_callee_fndecl (expression);
22591 int i, nargs;
22592 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
22593 return true;
22594 nargs = call_expr_nargs (expression);
22595 for (i = 0; i < nargs; ++i)
22596 {
22597 tree op = CALL_EXPR_ARG (expression, i);
22598 /* In a call to a constexpr member function, look through the
22599 implicit ADDR_EXPR on the object argument so that it doesn't
22600 cause the call to be considered value-dependent. We also
22601 look through it in potential_constant_expression. */
22602 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
22603 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
22604 && TREE_CODE (op) == ADDR_EXPR)
22605 op = TREE_OPERAND (op, 0);
22606 if (value_dependent_expression_p (op))
22607 return true;
22608 }
22609 return false;
22610 }
22611
22612 case TEMPLATE_ID_EXPR:
22613 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22614 type-dependent. */
22615 return type_dependent_expression_p (expression)
22616 || variable_concept_p (TREE_OPERAND (expression, 0));
22617
22618 case CONSTRUCTOR:
22619 {
22620 unsigned ix;
22621 tree val;
22622 if (dependent_type_p (TREE_TYPE (expression)))
22623 return true;
22624 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
22625 if (value_dependent_expression_p (val))
22626 return true;
22627 return false;
22628 }
22629
22630 case STMT_EXPR:
22631 /* Treat a GNU statement expression as dependent to avoid crashing
22632 under instantiate_non_dependent_expr; it can't be constant. */
22633 return true;
22634
22635 default:
22636 /* A constant expression is value-dependent if any subexpression is
22637 value-dependent. */
22638 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
22639 {
22640 case tcc_reference:
22641 case tcc_unary:
22642 case tcc_comparison:
22643 case tcc_binary:
22644 case tcc_expression:
22645 case tcc_vl_exp:
22646 {
22647 int i, len = cp_tree_operand_length (expression);
22648
22649 for (i = 0; i < len; i++)
22650 {
22651 tree t = TREE_OPERAND (expression, i);
22652
22653 /* In some cases, some of the operands may be missing.l
22654 (For example, in the case of PREDECREMENT_EXPR, the
22655 amount to increment by may be missing.) That doesn't
22656 make the expression dependent. */
22657 if (t && value_dependent_expression_p (t))
22658 return true;
22659 }
22660 }
22661 break;
22662 default:
22663 break;
22664 }
22665 break;
22666 }
22667
22668 /* The expression is not value-dependent. */
22669 return false;
22670 }
22671
22672 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
22673 [temp.dep.expr]. Note that an expression with no type is
22674 considered dependent. Other parts of the compiler arrange for an
22675 expression with type-dependent subexpressions to have no type, so
22676 this function doesn't have to be fully recursive. */
22677
22678 bool
22679 type_dependent_expression_p (tree expression)
22680 {
22681 if (!processing_template_decl)
22682 return false;
22683
22684 if (expression == NULL_TREE || expression == error_mark_node)
22685 return false;
22686
22687 /* An unresolved name is always dependent. */
22688 if (identifier_p (expression)
22689 || TREE_CODE (expression) == USING_DECL
22690 || TREE_CODE (expression) == WILDCARD_DECL)
22691 return true;
22692
22693 /* A fold expression is type-dependent. */
22694 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
22695 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
22696 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
22697 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
22698 return true;
22699
22700 /* Some expression forms are never type-dependent. */
22701 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
22702 || TREE_CODE (expression) == SIZEOF_EXPR
22703 || TREE_CODE (expression) == ALIGNOF_EXPR
22704 || TREE_CODE (expression) == AT_ENCODE_EXPR
22705 || TREE_CODE (expression) == NOEXCEPT_EXPR
22706 || TREE_CODE (expression) == TRAIT_EXPR
22707 || TREE_CODE (expression) == TYPEID_EXPR
22708 || TREE_CODE (expression) == DELETE_EXPR
22709 || TREE_CODE (expression) == VEC_DELETE_EXPR
22710 || TREE_CODE (expression) == THROW_EXPR
22711 || TREE_CODE (expression) == REQUIRES_EXPR)
22712 return false;
22713
22714 /* The types of these expressions depends only on the type to which
22715 the cast occurs. */
22716 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
22717 || TREE_CODE (expression) == STATIC_CAST_EXPR
22718 || TREE_CODE (expression) == CONST_CAST_EXPR
22719 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
22720 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
22721 || TREE_CODE (expression) == CAST_EXPR)
22722 return dependent_type_p (TREE_TYPE (expression));
22723
22724 /* The types of these expressions depends only on the type created
22725 by the expression. */
22726 if (TREE_CODE (expression) == NEW_EXPR
22727 || TREE_CODE (expression) == VEC_NEW_EXPR)
22728 {
22729 /* For NEW_EXPR tree nodes created inside a template, either
22730 the object type itself or a TREE_LIST may appear as the
22731 operand 1. */
22732 tree type = TREE_OPERAND (expression, 1);
22733 if (TREE_CODE (type) == TREE_LIST)
22734 /* This is an array type. We need to check array dimensions
22735 as well. */
22736 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
22737 || value_dependent_expression_p
22738 (TREE_OPERAND (TREE_VALUE (type), 1));
22739 else
22740 return dependent_type_p (type);
22741 }
22742
22743 if (TREE_CODE (expression) == SCOPE_REF)
22744 {
22745 tree scope = TREE_OPERAND (expression, 0);
22746 tree name = TREE_OPERAND (expression, 1);
22747
22748 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
22749 contains an identifier associated by name lookup with one or more
22750 declarations declared with a dependent type, or...a
22751 nested-name-specifier or qualified-id that names a member of an
22752 unknown specialization. */
22753 return (type_dependent_expression_p (name)
22754 || dependent_scope_p (scope));
22755 }
22756
22757 if (TREE_CODE (expression) == FUNCTION_DECL
22758 && DECL_LANG_SPECIFIC (expression)
22759 && DECL_TEMPLATE_INFO (expression)
22760 && (any_dependent_template_arguments_p
22761 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
22762 return true;
22763
22764 if (TREE_CODE (expression) == TEMPLATE_DECL
22765 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
22766 return false;
22767
22768 if (TREE_CODE (expression) == STMT_EXPR)
22769 expression = stmt_expr_value_expr (expression);
22770
22771 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
22772 {
22773 tree elt;
22774 unsigned i;
22775
22776 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
22777 {
22778 if (type_dependent_expression_p (elt))
22779 return true;
22780 }
22781 return false;
22782 }
22783
22784 /* A static data member of the current instantiation with incomplete
22785 array type is type-dependent, as the definition and specializations
22786 can have different bounds. */
22787 if (VAR_P (expression)
22788 && DECL_CLASS_SCOPE_P (expression)
22789 && dependent_type_p (DECL_CONTEXT (expression))
22790 && VAR_HAD_UNKNOWN_BOUND (expression))
22791 return true;
22792
22793 /* An array of unknown bound depending on a variadic parameter, eg:
22794
22795 template<typename... Args>
22796 void foo (Args... args)
22797 {
22798 int arr[] = { args... };
22799 }
22800
22801 template<int... vals>
22802 void bar ()
22803 {
22804 int arr[] = { vals... };
22805 }
22806
22807 If the array has no length and has an initializer, it must be that
22808 we couldn't determine its length in cp_complete_array_type because
22809 it is dependent. */
22810 if (VAR_P (expression)
22811 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
22812 && !TYPE_DOMAIN (TREE_TYPE (expression))
22813 && DECL_INITIAL (expression))
22814 return true;
22815
22816 /* A variable template specialization is type-dependent if it has any
22817 dependent template arguments. */
22818 if (VAR_P (expression)
22819 && DECL_LANG_SPECIFIC (expression)
22820 && DECL_TEMPLATE_INFO (expression)
22821 && variable_template_p (DECL_TI_TEMPLATE (expression)))
22822 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
22823
22824 /* Always dependent, on the number of arguments if nothing else. */
22825 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
22826 return true;
22827
22828 if (TREE_TYPE (expression) == unknown_type_node)
22829 {
22830 if (TREE_CODE (expression) == ADDR_EXPR)
22831 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
22832 if (TREE_CODE (expression) == COMPONENT_REF
22833 || TREE_CODE (expression) == OFFSET_REF)
22834 {
22835 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
22836 return true;
22837 expression = TREE_OPERAND (expression, 1);
22838 if (identifier_p (expression))
22839 return false;
22840 }
22841 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22842 if (TREE_CODE (expression) == SCOPE_REF)
22843 return false;
22844
22845 if (BASELINK_P (expression))
22846 {
22847 if (BASELINK_OPTYPE (expression)
22848 && dependent_type_p (BASELINK_OPTYPE (expression)))
22849 return true;
22850 expression = BASELINK_FUNCTIONS (expression);
22851 }
22852
22853 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
22854 {
22855 if (any_dependent_template_arguments_p
22856 (TREE_OPERAND (expression, 1)))
22857 return true;
22858 expression = TREE_OPERAND (expression, 0);
22859 if (identifier_p (expression))
22860 return true;
22861 }
22862
22863 gcc_assert (TREE_CODE (expression) == OVERLOAD
22864 || TREE_CODE (expression) == FUNCTION_DECL);
22865
22866 while (expression)
22867 {
22868 if (type_dependent_expression_p (OVL_CURRENT (expression)))
22869 return true;
22870 expression = OVL_NEXT (expression);
22871 }
22872 return false;
22873 }
22874
22875 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
22876
22877 return (dependent_type_p (TREE_TYPE (expression)));
22878 }
22879
22880 /* walk_tree callback function for instantiation_dependent_expression_p,
22881 below. Returns non-zero if a dependent subexpression is found. */
22882
22883 static tree
22884 instantiation_dependent_r (tree *tp, int *walk_subtrees,
22885 void * /*data*/)
22886 {
22887 if (TYPE_P (*tp))
22888 {
22889 /* We don't have to worry about decltype currently because decltype
22890 of an instantiation-dependent expr is a dependent type. This
22891 might change depending on the resolution of DR 1172. */
22892 *walk_subtrees = false;
22893 return NULL_TREE;
22894 }
22895 enum tree_code code = TREE_CODE (*tp);
22896 switch (code)
22897 {
22898 /* Don't treat an argument list as dependent just because it has no
22899 TREE_TYPE. */
22900 case TREE_LIST:
22901 case TREE_VEC:
22902 return NULL_TREE;
22903
22904 case VAR_DECL:
22905 case CONST_DECL:
22906 /* A constant with a dependent initializer is dependent. */
22907 if (value_dependent_expression_p (*tp))
22908 return *tp;
22909 break;
22910
22911 case TEMPLATE_PARM_INDEX:
22912 return *tp;
22913
22914 /* Handle expressions with type operands. */
22915 case SIZEOF_EXPR:
22916 case ALIGNOF_EXPR:
22917 case TYPEID_EXPR:
22918 case AT_ENCODE_EXPR:
22919 {
22920 tree op = TREE_OPERAND (*tp, 0);
22921 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
22922 op = TREE_TYPE (op);
22923 if (TYPE_P (op))
22924 {
22925 if (dependent_type_p (op))
22926 return *tp;
22927 else
22928 {
22929 *walk_subtrees = false;
22930 return NULL_TREE;
22931 }
22932 }
22933 break;
22934 }
22935
22936 case TRAIT_EXPR:
22937 if (value_dependent_expression_p (*tp))
22938 return *tp;
22939 *walk_subtrees = false;
22940 return NULL_TREE;
22941
22942 case COMPONENT_REF:
22943 if (identifier_p (TREE_OPERAND (*tp, 1)))
22944 /* In a template, finish_class_member_access_expr creates a
22945 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22946 type-dependent, so that we can check access control at
22947 instantiation time (PR 42277). See also Core issue 1273. */
22948 return *tp;
22949 break;
22950
22951 case SCOPE_REF:
22952 if (instantiation_dependent_scope_ref_p (*tp))
22953 return *tp;
22954 else
22955 break;
22956
22957 /* Treat statement-expressions as dependent. */
22958 case BIND_EXPR:
22959 return *tp;
22960
22961 /* Treat requires-expressions as dependent. */
22962 case REQUIRES_EXPR:
22963 return *tp;
22964
22965 case CALL_EXPR:
22966 /* Treat calls to function concepts as dependent. */
22967 if (function_concept_check_p (*tp))
22968 return *tp;
22969 break;
22970
22971 case TEMPLATE_ID_EXPR:
22972 /* And variable concepts. */
22973 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
22974 return *tp;
22975 break;
22976
22977 default:
22978 break;
22979 }
22980
22981 if (type_dependent_expression_p (*tp))
22982 return *tp;
22983 else
22984 return NULL_TREE;
22985 }
22986
22987 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
22988 sense defined by the ABI:
22989
22990 "An expression is instantiation-dependent if it is type-dependent
22991 or value-dependent, or it has a subexpression that is type-dependent
22992 or value-dependent." */
22993
22994 bool
22995 instantiation_dependent_expression_p (tree expression)
22996 {
22997 tree result;
22998
22999 if (!processing_template_decl)
23000 return false;
23001
23002 if (expression == error_mark_node)
23003 return false;
23004
23005 result = cp_walk_tree_without_duplicates (&expression,
23006 instantiation_dependent_r, NULL);
23007 return result != NULL_TREE;
23008 }
23009
23010 /* Like type_dependent_expression_p, but it also works while not processing
23011 a template definition, i.e. during substitution or mangling. */
23012
23013 bool
23014 type_dependent_expression_p_push (tree expr)
23015 {
23016 bool b;
23017 ++processing_template_decl;
23018 b = type_dependent_expression_p (expr);
23019 --processing_template_decl;
23020 return b;
23021 }
23022
23023 /* Returns TRUE if ARGS contains a type-dependent expression. */
23024
23025 bool
23026 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
23027 {
23028 unsigned int i;
23029 tree arg;
23030
23031 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
23032 {
23033 if (type_dependent_expression_p (arg))
23034 return true;
23035 }
23036 return false;
23037 }
23038
23039 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23040 expressions) contains any type-dependent expressions. */
23041
23042 bool
23043 any_type_dependent_elements_p (const_tree list)
23044 {
23045 for (; list; list = TREE_CHAIN (list))
23046 if (type_dependent_expression_p (TREE_VALUE (list)))
23047 return true;
23048
23049 return false;
23050 }
23051
23052 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23053 expressions) contains any value-dependent expressions. */
23054
23055 bool
23056 any_value_dependent_elements_p (const_tree list)
23057 {
23058 for (; list; list = TREE_CHAIN (list))
23059 if (value_dependent_expression_p (TREE_VALUE (list)))
23060 return true;
23061
23062 return false;
23063 }
23064
23065 /* Returns TRUE if the ARG (a template argument) is dependent. */
23066
23067 bool
23068 dependent_template_arg_p (tree arg)
23069 {
23070 if (!processing_template_decl)
23071 return false;
23072
23073 /* Assume a template argument that was wrongly written by the user
23074 is dependent. This is consistent with what
23075 any_dependent_template_arguments_p [that calls this function]
23076 does. */
23077 if (!arg || arg == error_mark_node)
23078 return true;
23079
23080 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
23081 arg = ARGUMENT_PACK_SELECT_ARG (arg);
23082
23083 if (TREE_CODE (arg) == TEMPLATE_DECL
23084 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
23085 return dependent_template_p (arg);
23086 else if (ARGUMENT_PACK_P (arg))
23087 {
23088 tree args = ARGUMENT_PACK_ARGS (arg);
23089 int i, len = TREE_VEC_LENGTH (args);
23090 for (i = 0; i < len; ++i)
23091 {
23092 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23093 return true;
23094 }
23095
23096 return false;
23097 }
23098 else if (TYPE_P (arg))
23099 return dependent_type_p (arg);
23100 else
23101 return (type_dependent_expression_p (arg)
23102 || value_dependent_expression_p (arg));
23103 }
23104
23105 /* Returns true if ARGS (a collection of template arguments) contains
23106 any types that require structural equality testing. */
23107
23108 bool
23109 any_template_arguments_need_structural_equality_p (tree args)
23110 {
23111 int i;
23112 int j;
23113
23114 if (!args)
23115 return false;
23116 if (args == error_mark_node)
23117 return true;
23118
23119 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23120 {
23121 tree level = TMPL_ARGS_LEVEL (args, i + 1);
23122 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23123 {
23124 tree arg = TREE_VEC_ELT (level, j);
23125 tree packed_args = NULL_TREE;
23126 int k, len = 1;
23127
23128 if (ARGUMENT_PACK_P (arg))
23129 {
23130 /* Look inside the argument pack. */
23131 packed_args = ARGUMENT_PACK_ARGS (arg);
23132 len = TREE_VEC_LENGTH (packed_args);
23133 }
23134
23135 for (k = 0; k < len; ++k)
23136 {
23137 if (packed_args)
23138 arg = TREE_VEC_ELT (packed_args, k);
23139
23140 if (error_operand_p (arg))
23141 return true;
23142 else if (TREE_CODE (arg) == TEMPLATE_DECL)
23143 continue;
23144 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
23145 return true;
23146 else if (!TYPE_P (arg) && TREE_TYPE (arg)
23147 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
23148 return true;
23149 }
23150 }
23151 }
23152
23153 return false;
23154 }
23155
23156 /* Returns true if ARGS (a collection of template arguments) contains
23157 any dependent arguments. */
23158
23159 bool
23160 any_dependent_template_arguments_p (const_tree args)
23161 {
23162 int i;
23163 int j;
23164
23165 if (!args)
23166 return false;
23167 if (args == error_mark_node)
23168 return true;
23169
23170 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23171 {
23172 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
23173 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23174 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
23175 return true;
23176 }
23177
23178 return false;
23179 }
23180
23181 /* Returns TRUE if the template TMPL is dependent. */
23182
23183 bool
23184 dependent_template_p (tree tmpl)
23185 {
23186 if (TREE_CODE (tmpl) == OVERLOAD)
23187 {
23188 while (tmpl)
23189 {
23190 if (dependent_template_p (OVL_CURRENT (tmpl)))
23191 return true;
23192 tmpl = OVL_NEXT (tmpl);
23193 }
23194 return false;
23195 }
23196
23197 /* Template template parameters are dependent. */
23198 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
23199 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
23200 return true;
23201 /* So are names that have not been looked up. */
23202 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
23203 return true;
23204 /* So are member templates of dependent classes. */
23205 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
23206 return dependent_type_p (DECL_CONTEXT (tmpl));
23207 return false;
23208 }
23209
23210 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23211
23212 bool
23213 dependent_template_id_p (tree tmpl, tree args)
23214 {
23215 return (dependent_template_p (tmpl)
23216 || any_dependent_template_arguments_p (args));
23217 }
23218
23219 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23220 are dependent. */
23221
23222 bool
23223 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
23224 {
23225 int i;
23226
23227 if (!processing_template_decl)
23228 return false;
23229
23230 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
23231 {
23232 tree decl = TREE_VEC_ELT (declv, i);
23233 tree init = TREE_VEC_ELT (initv, i);
23234 tree cond = TREE_VEC_ELT (condv, i);
23235 tree incr = TREE_VEC_ELT (incrv, i);
23236
23237 if (type_dependent_expression_p (decl)
23238 || TREE_CODE (decl) == SCOPE_REF)
23239 return true;
23240
23241 if (init && type_dependent_expression_p (init))
23242 return true;
23243
23244 if (type_dependent_expression_p (cond))
23245 return true;
23246
23247 if (COMPARISON_CLASS_P (cond)
23248 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
23249 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
23250 return true;
23251
23252 if (TREE_CODE (incr) == MODOP_EXPR)
23253 {
23254 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
23255 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
23256 return true;
23257 }
23258 else if (type_dependent_expression_p (incr))
23259 return true;
23260 else if (TREE_CODE (incr) == MODIFY_EXPR)
23261 {
23262 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
23263 return true;
23264 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
23265 {
23266 tree t = TREE_OPERAND (incr, 1);
23267 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
23268 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
23269 return true;
23270 }
23271 }
23272 }
23273
23274 return false;
23275 }
23276
23277 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23278 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23279 no such TYPE can be found. Note that this function peers inside
23280 uninstantiated templates and therefore should be used only in
23281 extremely limited situations. ONLY_CURRENT_P restricts this
23282 peering to the currently open classes hierarchy (which is required
23283 when comparing types). */
23284
23285 tree
23286 resolve_typename_type (tree type, bool only_current_p)
23287 {
23288 tree scope;
23289 tree name;
23290 tree decl;
23291 int quals;
23292 tree pushed_scope;
23293 tree result;
23294
23295 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
23296
23297 scope = TYPE_CONTEXT (type);
23298 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23299 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23300 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23301 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23302 identifier of the TYPENAME_TYPE anymore.
23303 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23304 TYPENAME_TYPE instead, we avoid messing up with a possible
23305 typedef variant case. */
23306 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
23307
23308 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23309 it first before we can figure out what NAME refers to. */
23310 if (TREE_CODE (scope) == TYPENAME_TYPE)
23311 {
23312 if (TYPENAME_IS_RESOLVING_P (scope))
23313 /* Given a class template A with a dependent base with nested type C,
23314 typedef typename A::C::C C will land us here, as trying to resolve
23315 the initial A::C leads to the local C typedef, which leads back to
23316 A::C::C. So we break the recursion now. */
23317 return type;
23318 else
23319 scope = resolve_typename_type (scope, only_current_p);
23320 }
23321 /* If we don't know what SCOPE refers to, then we cannot resolve the
23322 TYPENAME_TYPE. */
23323 if (TREE_CODE (scope) == TYPENAME_TYPE)
23324 return type;
23325 /* If the SCOPE is a template type parameter, we have no way of
23326 resolving the name. */
23327 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
23328 return type;
23329 /* If the SCOPE is not the current instantiation, there's no reason
23330 to look inside it. */
23331 if (only_current_p && !currently_open_class (scope))
23332 return type;
23333 /* If this is a typedef, we don't want to look inside (c++/11987). */
23334 if (typedef_variant_p (type))
23335 return type;
23336 /* If SCOPE isn't the template itself, it will not have a valid
23337 TYPE_FIELDS list. */
23338 if (CLASS_TYPE_P (scope)
23339 && same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
23340 /* scope is either the template itself or a compatible instantiation
23341 like X<T>, so look up the name in the original template. */
23342 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
23343 else
23344 /* scope is a partial instantiation, so we can't do the lookup or we
23345 will lose the template arguments. */
23346 return type;
23347 /* Enter the SCOPE so that name lookup will be resolved as if we
23348 were in the class definition. In particular, SCOPE will no
23349 longer be considered a dependent type. */
23350 pushed_scope = push_scope (scope);
23351 /* Look up the declaration. */
23352 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
23353 tf_warning_or_error);
23354
23355 result = NULL_TREE;
23356
23357 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23358 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23359 if (!decl)
23360 /*nop*/;
23361 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
23362 && TREE_CODE (decl) == TYPE_DECL)
23363 {
23364 result = TREE_TYPE (decl);
23365 if (result == error_mark_node)
23366 result = NULL_TREE;
23367 }
23368 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
23369 && DECL_CLASS_TEMPLATE_P (decl))
23370 {
23371 tree tmpl;
23372 tree args;
23373 /* Obtain the template and the arguments. */
23374 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
23375 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
23376 /* Instantiate the template. */
23377 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
23378 /*entering_scope=*/0,
23379 tf_error | tf_user);
23380 if (result == error_mark_node)
23381 result = NULL_TREE;
23382 }
23383
23384 /* Leave the SCOPE. */
23385 if (pushed_scope)
23386 pop_scope (pushed_scope);
23387
23388 /* If we failed to resolve it, return the original typename. */
23389 if (!result)
23390 return type;
23391
23392 /* If lookup found a typename type, resolve that too. */
23393 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
23394 {
23395 /* Ill-formed programs can cause infinite recursion here, so we
23396 must catch that. */
23397 TYPENAME_IS_RESOLVING_P (type) = 1;
23398 result = resolve_typename_type (result, only_current_p);
23399 TYPENAME_IS_RESOLVING_P (type) = 0;
23400 }
23401
23402 /* Qualify the resulting type. */
23403 quals = cp_type_quals (type);
23404 if (quals)
23405 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
23406
23407 return result;
23408 }
23409
23410 /* EXPR is an expression which is not type-dependent. Return a proxy
23411 for EXPR that can be used to compute the types of larger
23412 expressions containing EXPR. */
23413
23414 tree
23415 build_non_dependent_expr (tree expr)
23416 {
23417 tree inner_expr;
23418
23419 /* Try to get a constant value for all non-dependent expressions in
23420 order to expose bugs in *_dependent_expression_p and constexpr. */
23421 if (flag_checking && cxx_dialect >= cxx11)
23422 fold_non_dependent_expr (expr);
23423
23424 /* Preserve OVERLOADs; the functions must be available to resolve
23425 types. */
23426 inner_expr = expr;
23427 if (TREE_CODE (inner_expr) == STMT_EXPR)
23428 inner_expr = stmt_expr_value_expr (inner_expr);
23429 if (TREE_CODE (inner_expr) == ADDR_EXPR)
23430 inner_expr = TREE_OPERAND (inner_expr, 0);
23431 if (TREE_CODE (inner_expr) == COMPONENT_REF)
23432 inner_expr = TREE_OPERAND (inner_expr, 1);
23433 if (is_overloaded_fn (inner_expr)
23434 || TREE_CODE (inner_expr) == OFFSET_REF)
23435 return expr;
23436 /* There is no need to return a proxy for a variable. */
23437 if (VAR_P (expr))
23438 return expr;
23439 /* Preserve string constants; conversions from string constants to
23440 "char *" are allowed, even though normally a "const char *"
23441 cannot be used to initialize a "char *". */
23442 if (TREE_CODE (expr) == STRING_CST)
23443 return expr;
23444 /* Preserve void and arithmetic constants, as an optimization -- there is no
23445 reason to create a new node. */
23446 if (TREE_CODE (expr) == VOID_CST
23447 || TREE_CODE (expr) == INTEGER_CST
23448 || TREE_CODE (expr) == REAL_CST)
23449 return expr;
23450 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23451 There is at least one place where we want to know that a
23452 particular expression is a throw-expression: when checking a ?:
23453 expression, there are special rules if the second or third
23454 argument is a throw-expression. */
23455 if (TREE_CODE (expr) == THROW_EXPR)
23456 return expr;
23457
23458 /* Don't wrap an initializer list, we need to be able to look inside. */
23459 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
23460 return expr;
23461
23462 /* Don't wrap a dummy object, we need to be able to test for it. */
23463 if (is_dummy_object (expr))
23464 return expr;
23465
23466 if (TREE_CODE (expr) == COND_EXPR)
23467 return build3 (COND_EXPR,
23468 TREE_TYPE (expr),
23469 TREE_OPERAND (expr, 0),
23470 (TREE_OPERAND (expr, 1)
23471 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
23472 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
23473 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
23474 if (TREE_CODE (expr) == COMPOUND_EXPR
23475 && !COMPOUND_EXPR_OVERLOADED (expr))
23476 return build2 (COMPOUND_EXPR,
23477 TREE_TYPE (expr),
23478 TREE_OPERAND (expr, 0),
23479 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
23480
23481 /* If the type is unknown, it can't really be non-dependent */
23482 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
23483
23484 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23485 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
23486 }
23487
23488 /* ARGS is a vector of expressions as arguments to a function call.
23489 Replace the arguments with equivalent non-dependent expressions.
23490 This modifies ARGS in place. */
23491
23492 void
23493 make_args_non_dependent (vec<tree, va_gc> *args)
23494 {
23495 unsigned int ix;
23496 tree arg;
23497
23498 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
23499 {
23500 tree newarg = build_non_dependent_expr (arg);
23501 if (newarg != arg)
23502 (*args)[ix] = newarg;
23503 }
23504 }
23505
23506 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23507 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23508 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
23509
23510 static tree
23511 make_auto_1 (tree name, bool set_canonical)
23512 {
23513 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
23514 TYPE_NAME (au) = build_decl (input_location,
23515 TYPE_DECL, name, au);
23516 TYPE_STUB_DECL (au) = TYPE_NAME (au);
23517 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
23518 (0, processing_template_decl + 1, processing_template_decl + 1,
23519 TYPE_NAME (au), NULL_TREE);
23520 if (set_canonical)
23521 TYPE_CANONICAL (au) = canonical_type_parameter (au);
23522 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
23523 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
23524
23525 return au;
23526 }
23527
23528 tree
23529 make_decltype_auto (void)
23530 {
23531 return make_auto_1 (get_identifier ("decltype(auto)"), true);
23532 }
23533
23534 tree
23535 make_auto (void)
23536 {
23537 return make_auto_1 (get_identifier ("auto"), true);
23538 }
23539
23540 /* Make a "constrained auto" type-specifier. This is an
23541 auto type with constraints that must be associated after
23542 deduction. The constraint is formed from the given
23543 CONC and its optional sequence of arguments, which are
23544 non-null if written as partial-concept-id. */
23545
23546 tree
23547 make_constrained_auto (tree con, tree args)
23548 {
23549 tree type = make_auto_1 (get_identifier ("auto"), false);
23550
23551 /* Build the constraint. */
23552 tree tmpl = DECL_TI_TEMPLATE (con);
23553 tree expr;
23554 if (VAR_P (con))
23555 expr = build_concept_check (tmpl, type, args);
23556 else
23557 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
23558
23559 tree constr = make_predicate_constraint (expr);
23560 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
23561
23562 /* Our canonical type depends on the constraint. */
23563 TYPE_CANONICAL (type) = canonical_type_parameter (type);
23564
23565 /* Attach the constraint to the type declaration. */
23566 tree decl = TYPE_NAME (type);
23567 return decl;
23568 }
23569
23570 /* Given type ARG, return std::initializer_list<ARG>. */
23571
23572 static tree
23573 listify (tree arg)
23574 {
23575 tree std_init_list = namespace_binding
23576 (get_identifier ("initializer_list"), std_node);
23577 tree argvec;
23578 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
23579 {
23580 error ("deducing from brace-enclosed initializer list requires "
23581 "#include <initializer_list>");
23582 return error_mark_node;
23583 }
23584 argvec = make_tree_vec (1);
23585 TREE_VEC_ELT (argvec, 0) = arg;
23586 return lookup_template_class (std_init_list, argvec, NULL_TREE,
23587 NULL_TREE, 0, tf_warning_or_error);
23588 }
23589
23590 /* Replace auto in TYPE with std::initializer_list<auto>. */
23591
23592 static tree
23593 listify_autos (tree type, tree auto_node)
23594 {
23595 tree init_auto = listify (auto_node);
23596 tree argvec = make_tree_vec (1);
23597 TREE_VEC_ELT (argvec, 0) = init_auto;
23598 if (processing_template_decl)
23599 argvec = add_to_template_args (current_template_args (), argvec);
23600 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
23601 }
23602
23603 /* Hash traits for hashing possibly constrained 'auto'
23604 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
23605
23606 struct auto_hash : default_hash_traits<tree>
23607 {
23608 static inline hashval_t hash (tree);
23609 static inline bool equal (tree, tree);
23610 };
23611
23612 /* Hash the 'auto' T. */
23613
23614 inline hashval_t
23615 auto_hash::hash (tree t)
23616 {
23617 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
23618 /* Matching constrained-type-specifiers denote the same template
23619 parameter, so hash the constraint. */
23620 return hash_placeholder_constraint (c);
23621 else
23622 /* But unconstrained autos are all separate, so just hash the pointer. */
23623 return iterative_hash_object (t, 0);
23624 }
23625
23626 /* Compare two 'auto's. */
23627
23628 inline bool
23629 auto_hash::equal (tree t1, tree t2)
23630 {
23631 if (t1 == t2)
23632 return true;
23633
23634 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
23635 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
23636
23637 /* Two unconstrained autos are distinct. */
23638 if (!c1 || !c2)
23639 return false;
23640
23641 return equivalent_placeholder_constraints (c1, c2);
23642 }
23643
23644 /* for_each_template_parm callback for extract_autos: if t is a (possibly
23645 constrained) auto, add it to the vector. */
23646
23647 static int
23648 extract_autos_r (tree t, void *data)
23649 {
23650 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
23651 if (is_auto_or_concept (t))
23652 {
23653 /* All the autos were built with index 0; fix that up now. */
23654 tree *p = hash.find_slot (t, INSERT);
23655 unsigned idx;
23656 if (*p)
23657 /* If this is a repeated constrained-type-specifier, use the index we
23658 chose before. */
23659 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
23660 else
23661 {
23662 /* Otherwise this is new, so use the current count. */
23663 *p = t;
23664 idx = hash.elements () - 1;
23665 }
23666 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
23667 }
23668
23669 /* Always keep walking. */
23670 return 0;
23671 }
23672
23673 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
23674 says they can appear anywhere in the type. */
23675
23676 static tree
23677 extract_autos (tree type)
23678 {
23679 hash_set<tree> visited;
23680 hash_table<auto_hash> hash (2);
23681
23682 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
23683
23684 tree tree_vec = make_tree_vec (hash.elements());
23685 for (hash_table<auto_hash>::iterator iter = hash.begin();
23686 iter != hash.end(); ++iter)
23687 {
23688 tree elt = *iter;
23689 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
23690 TREE_VEC_ELT (tree_vec, i)
23691 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
23692 }
23693
23694 return tree_vec;
23695 }
23696
23697 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23698 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
23699
23700 tree
23701 do_auto_deduction (tree type, tree init, tree auto_node)
23702 {
23703 return do_auto_deduction (type, init, auto_node,
23704 tf_warning_or_error,
23705 adc_unspecified);
23706 }
23707
23708 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23709 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
23710 The CONTEXT determines the context in which auto deduction is performed
23711 and is used to control error diagnostics. */
23712
23713 tree
23714 do_auto_deduction (tree type, tree init, tree auto_node,
23715 tsubst_flags_t complain, auto_deduction_context context)
23716 {
23717 tree targs;
23718
23719 if (init == error_mark_node)
23720 return error_mark_node;
23721
23722 if (type_dependent_expression_p (init))
23723 /* Defining a subset of type-dependent expressions that we can deduce
23724 from ahead of time isn't worth the trouble. */
23725 return type;
23726
23727 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
23728 with either a new invented type template parameter U or, if the
23729 initializer is a braced-init-list (8.5.4), with
23730 std::initializer_list<U>. */
23731 if (BRACE_ENCLOSED_INITIALIZER_P (init))
23732 {
23733 if (!DIRECT_LIST_INIT_P (init))
23734 type = listify_autos (type, auto_node);
23735 else if (CONSTRUCTOR_NELTS (init) == 1)
23736 init = CONSTRUCTOR_ELT (init, 0)->value;
23737 else
23738 {
23739 if (complain & tf_warning_or_error)
23740 {
23741 if (permerror (input_location, "direct-list-initialization of "
23742 "%<auto%> requires exactly one element"))
23743 inform (input_location,
23744 "for deduction to %<std::initializer_list%>, use copy-"
23745 "list-initialization (i.e. add %<=%> before the %<{%>)");
23746 }
23747 type = listify_autos (type, auto_node);
23748 }
23749 }
23750
23751 if (type == error_mark_node)
23752 return error_mark_node;
23753
23754 init = resolve_nondeduced_context (init);
23755
23756 if (AUTO_IS_DECLTYPE (auto_node))
23757 {
23758 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
23759 && !REF_PARENTHESIZED_P (init)));
23760 targs = make_tree_vec (1);
23761 TREE_VEC_ELT (targs, 0)
23762 = finish_decltype_type (init, id, tf_warning_or_error);
23763 if (type != auto_node)
23764 {
23765 if (complain & tf_error)
23766 error ("%qT as type rather than plain %<decltype(auto)%>", type);
23767 return error_mark_node;
23768 }
23769 }
23770 else
23771 {
23772 tree parms = build_tree_list (NULL_TREE, type);
23773 tree tparms;
23774
23775 if (flag_concepts)
23776 tparms = extract_autos (type);
23777 else
23778 {
23779 tparms = make_tree_vec (1);
23780 TREE_VEC_ELT (tparms, 0)
23781 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
23782 }
23783
23784 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
23785 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
23786 DEDUCE_CALL, LOOKUP_NORMAL,
23787 NULL, /*explain_p=*/false);
23788 if (val > 0)
23789 {
23790 if (processing_template_decl)
23791 /* Try again at instantiation time. */
23792 return type;
23793 if (type && type != error_mark_node
23794 && (complain & tf_error))
23795 /* If type is error_mark_node a diagnostic must have been
23796 emitted by now. Also, having a mention to '<type error>'
23797 in the diagnostic is not really useful to the user. */
23798 {
23799 if (cfun && auto_node == current_function_auto_return_pattern
23800 && LAMBDA_FUNCTION_P (current_function_decl))
23801 error ("unable to deduce lambda return type from %qE", init);
23802 else
23803 error ("unable to deduce %qT from %qE", type, init);
23804 type_unification_real (tparms, targs, parms, &init, 1, 0,
23805 DEDUCE_CALL, LOOKUP_NORMAL,
23806 NULL, /*explain_p=*/true);
23807 }
23808 return error_mark_node;
23809 }
23810 }
23811
23812 /* Check any placeholder constraints against the deduced type. */
23813 if (flag_concepts && !processing_template_decl)
23814 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
23815 {
23816 /* Use the deduced type to check the associated constraints. */
23817 if (!constraints_satisfied_p (constr, targs))
23818 {
23819 if (complain & tf_warning_or_error)
23820 {
23821 switch (context)
23822 {
23823 case adc_unspecified:
23824 error("placeholder constraints not satisfied");
23825 break;
23826 case adc_variable_type:
23827 error ("deduced initializer does not satisfy "
23828 "placeholder constraints");
23829 break;
23830 case adc_return_type:
23831 error ("deduced return type does not satisfy "
23832 "placeholder constraints");
23833 break;
23834 case adc_requirement:
23835 error ("deduced expression type does not saatisy "
23836 "placeholder constraints");
23837 break;
23838 }
23839 diagnose_constraints (input_location, constr, targs);
23840 }
23841 return error_mark_node;
23842 }
23843 }
23844
23845 if (processing_template_decl)
23846 targs = add_to_template_args (current_template_args (), targs);
23847 return tsubst (type, targs, complain, NULL_TREE);
23848 }
23849
23850 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
23851 result. */
23852
23853 tree
23854 splice_late_return_type (tree type, tree late_return_type)
23855 {
23856 if (is_auto (type))
23857 {
23858 if (late_return_type)
23859 return late_return_type;
23860
23861 tree idx = get_template_parm_index (type);
23862 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
23863 /* In an abbreviated function template we didn't know we were dealing
23864 with a function template when we saw the auto return type, so update
23865 it to have the correct level. */
23866 return make_auto_1 (TYPE_IDENTIFIER (type), true);
23867 }
23868 return type;
23869 }
23870
23871 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
23872 'decltype(auto)'. */
23873
23874 bool
23875 is_auto (const_tree type)
23876 {
23877 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23878 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
23879 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
23880 return true;
23881 else
23882 return false;
23883 }
23884
23885 /* for_each_template_parm callback for type_uses_auto. */
23886
23887 int
23888 is_auto_r (tree tp, void */*data*/)
23889 {
23890 return is_auto_or_concept (tp);
23891 }
23892
23893 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
23894 a use of `auto'. Returns NULL_TREE otherwise. */
23895
23896 tree
23897 type_uses_auto (tree type)
23898 {
23899 if (type == NULL_TREE)
23900 return NULL_TREE;
23901 else if (flag_concepts)
23902 {
23903 /* The Concepts TS allows multiple autos in one type-specifier; just
23904 return the first one we find, do_auto_deduction will collect all of
23905 them. */
23906 if (uses_template_parms (type))
23907 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
23908 /*visited*/NULL, /*nondeduced*/true);
23909 else
23910 return NULL_TREE;
23911 }
23912 else
23913 return find_type_usage (type, is_auto);
23914 }
23915
23916 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
23917 'decltype(auto)' or a concept. */
23918
23919 bool
23920 is_auto_or_concept (const_tree type)
23921 {
23922 return is_auto (type); // or concept
23923 }
23924
23925 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
23926 a concept identifier) iff TYPE contains a use of a generic type. Returns
23927 NULL_TREE otherwise. */
23928
23929 tree
23930 type_uses_auto_or_concept (tree type)
23931 {
23932 return find_type_usage (type, is_auto_or_concept);
23933 }
23934
23935
23936 /* For a given template T, return the vector of typedefs referenced
23937 in T for which access check is needed at T instantiation time.
23938 T is either a FUNCTION_DECL or a RECORD_TYPE.
23939 Those typedefs were added to T by the function
23940 append_type_to_template_for_access_check. */
23941
23942 vec<qualified_typedef_usage_t, va_gc> *
23943 get_types_needing_access_check (tree t)
23944 {
23945 tree ti;
23946 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
23947
23948 if (!t || t == error_mark_node)
23949 return NULL;
23950
23951 if (!(ti = get_template_info (t)))
23952 return NULL;
23953
23954 if (CLASS_TYPE_P (t)
23955 || TREE_CODE (t) == FUNCTION_DECL)
23956 {
23957 if (!TI_TEMPLATE (ti))
23958 return NULL;
23959
23960 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
23961 }
23962
23963 return result;
23964 }
23965
23966 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
23967 tied to T. That list of typedefs will be access checked at
23968 T instantiation time.
23969 T is either a FUNCTION_DECL or a RECORD_TYPE.
23970 TYPE_DECL is a TYPE_DECL node representing a typedef.
23971 SCOPE is the scope through which TYPE_DECL is accessed.
23972 LOCATION is the location of the usage point of TYPE_DECL.
23973
23974 This function is a subroutine of
23975 append_type_to_template_for_access_check. */
23976
23977 static void
23978 append_type_to_template_for_access_check_1 (tree t,
23979 tree type_decl,
23980 tree scope,
23981 location_t location)
23982 {
23983 qualified_typedef_usage_t typedef_usage;
23984 tree ti;
23985
23986 if (!t || t == error_mark_node)
23987 return;
23988
23989 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
23990 || CLASS_TYPE_P (t))
23991 && type_decl
23992 && TREE_CODE (type_decl) == TYPE_DECL
23993 && scope);
23994
23995 if (!(ti = get_template_info (t)))
23996 return;
23997
23998 gcc_assert (TI_TEMPLATE (ti));
23999
24000 typedef_usage.typedef_decl = type_decl;
24001 typedef_usage.context = scope;
24002 typedef_usage.locus = location;
24003
24004 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
24005 }
24006
24007 /* Append TYPE_DECL to the template TEMPL.
24008 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24009 At TEMPL instanciation time, TYPE_DECL will be checked to see
24010 if it can be accessed through SCOPE.
24011 LOCATION is the location of the usage point of TYPE_DECL.
24012
24013 e.g. consider the following code snippet:
24014
24015 class C
24016 {
24017 typedef int myint;
24018 };
24019
24020 template<class U> struct S
24021 {
24022 C::myint mi; // <-- usage point of the typedef C::myint
24023 };
24024
24025 S<char> s;
24026
24027 At S<char> instantiation time, we need to check the access of C::myint
24028 In other words, we need to check the access of the myint typedef through
24029 the C scope. For that purpose, this function will add the myint typedef
24030 and the scope C through which its being accessed to a list of typedefs
24031 tied to the template S. That list will be walked at template instantiation
24032 time and access check performed on each typedefs it contains.
24033 Note that this particular code snippet should yield an error because
24034 myint is private to C. */
24035
24036 void
24037 append_type_to_template_for_access_check (tree templ,
24038 tree type_decl,
24039 tree scope,
24040 location_t location)
24041 {
24042 qualified_typedef_usage_t *iter;
24043 unsigned i;
24044
24045 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
24046
24047 /* Make sure we don't append the type to the template twice. */
24048 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
24049 if (iter->typedef_decl == type_decl && scope == iter->context)
24050 return;
24051
24052 append_type_to_template_for_access_check_1 (templ, type_decl,
24053 scope, location);
24054 }
24055
24056 /* Convert the generic type parameters in PARM that match the types given in the
24057 range [START_IDX, END_IDX) from the current_template_parms into generic type
24058 packs. */
24059
24060 tree
24061 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
24062 {
24063 tree current = current_template_parms;
24064 int depth = TMPL_PARMS_DEPTH (current);
24065 current = INNERMOST_TEMPLATE_PARMS (current);
24066 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
24067
24068 for (int i = 0; i < start_idx; ++i)
24069 TREE_VEC_ELT (replacement, i)
24070 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24071
24072 for (int i = start_idx; i < end_idx; ++i)
24073 {
24074 /* Create a distinct parameter pack type from the current parm and add it
24075 to the replacement args to tsubst below into the generic function
24076 parameter. */
24077
24078 tree o = TREE_TYPE (TREE_VALUE
24079 (TREE_VEC_ELT (current, i)));
24080 tree t = copy_type (o);
24081 TEMPLATE_TYPE_PARM_INDEX (t)
24082 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
24083 o, 0, 0, tf_none);
24084 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
24085 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
24086 TYPE_MAIN_VARIANT (t) = t;
24087 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
24088 TYPE_CANONICAL (t) = canonical_type_parameter (t);
24089 TREE_VEC_ELT (replacement, i) = t;
24090 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
24091 }
24092
24093 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
24094 TREE_VEC_ELT (replacement, i)
24095 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24096
24097 /* If there are more levels then build up the replacement with the outer
24098 template parms. */
24099 if (depth > 1)
24100 replacement = add_to_template_args (template_parms_to_args
24101 (TREE_CHAIN (current_template_parms)),
24102 replacement);
24103
24104 return tsubst (parm, replacement, tf_none, NULL_TREE);
24105 }
24106
24107 /* Entries in the decl_constraint hash table. */
24108 struct GTY((for_user)) constr_entry
24109 {
24110 tree decl;
24111 tree ci;
24112 };
24113
24114 /* Hashing function and equality for constraint entries. */
24115 struct constr_hasher : ggc_ptr_hash<constr_entry>
24116 {
24117 static hashval_t hash (constr_entry *e)
24118 {
24119 return (hashval_t)DECL_UID (e->decl);
24120 }
24121
24122 static bool equal (constr_entry *e1, constr_entry *e2)
24123 {
24124 return e1->decl == e2->decl;
24125 }
24126 };
24127
24128 /* A mapping from declarations to constraint information. Note that
24129 both templates and their underlying declarations are mapped to the
24130 same constraint information.
24131
24132 FIXME: This is defined in pt.c because garbage collection
24133 code is not being generated for constraint.cc. */
24134
24135 static GTY (()) hash_table<constr_hasher> *decl_constraints;
24136
24137 /* Returns true iff cinfo contains a valid set of constraints.
24138 This is the case when the associated requirements have been
24139 successfully decomposed into lists of atomic constraints.
24140 That is, when the saved assumptions are not error_mark_node. */
24141
24142 bool
24143 valid_constraints_p (tree cinfo)
24144 {
24145 gcc_assert (cinfo);
24146 return CI_ASSUMPTIONS (cinfo) != error_mark_node;
24147 }
24148
24149 /* Returns the template constraints of declaration T. If T is not
24150 constrained, return NULL_TREE. Note that T must be non-null. */
24151
24152 tree
24153 get_constraints (tree t)
24154 {
24155 gcc_assert (DECL_P (t));
24156 if (TREE_CODE (t) == TEMPLATE_DECL)
24157 t = DECL_TEMPLATE_RESULT (t);
24158 constr_entry elt = { t, NULL_TREE };
24159 constr_entry* found = decl_constraints->find (&elt);
24160 if (found)
24161 return found->ci;
24162 else
24163 return NULL_TREE;
24164 }
24165
24166 /* Associate the given constraint information CI with the declaration
24167 T. If T is a template, then the constraints are associated with
24168 its underlying declaration. Don't build associations if CI is
24169 NULL_TREE. */
24170
24171 void
24172 set_constraints (tree t, tree ci)
24173 {
24174 if (!ci)
24175 return;
24176 gcc_assert (t);
24177 if (TREE_CODE (t) == TEMPLATE_DECL)
24178 t = DECL_TEMPLATE_RESULT (t);
24179 gcc_assert (!get_constraints (t));
24180 constr_entry elt = {t, ci};
24181 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
24182 constr_entry* entry = ggc_alloc<constr_entry> ();
24183 *entry = elt;
24184 *slot = entry;
24185 }
24186
24187 /* Remove the associated constraints of the declaration T. */
24188
24189 void
24190 remove_constraints (tree t)
24191 {
24192 gcc_assert (DECL_P (t));
24193 if (TREE_CODE (t) == TEMPLATE_DECL)
24194 t = DECL_TEMPLATE_RESULT (t);
24195
24196 constr_entry elt = {t, NULL_TREE};
24197 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
24198 if (slot)
24199 decl_constraints->clear_slot (slot);
24200 }
24201
24202 /* Set up the hash table for constraint association. */
24203
24204 void
24205 init_constraint_processing (void)
24206 {
24207 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
24208 }
24209
24210 /* Set up the hash tables for template instantiations. */
24211
24212 void
24213 init_template_processing (void)
24214 {
24215 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
24216 type_specializations = hash_table<spec_hasher>::create_ggc (37);
24217 }
24218
24219 /* Print stats about the template hash tables for -fstats. */
24220
24221 void
24222 print_template_statistics (void)
24223 {
24224 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
24225 "%f collisions\n", (long) decl_specializations->size (),
24226 (long) decl_specializations->elements (),
24227 decl_specializations->collisions ());
24228 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
24229 "%f collisions\n", (long) type_specializations->size (),
24230 (long) type_specializations->elements (),
24231 type_specializations->collisions ());
24232 }
24233
24234 #include "gt-cp-pt.h"