Implement N4295 fold-expressions.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 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 "tm.h"
31 #include "alias.h"
32 #include "tree.h"
33 #include "stringpool.h"
34 #include "varasm.h"
35 #include "attribs.h"
36 #include "stor-layout.h"
37 #include "intl.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimplify.h"
50
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
54
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
61 };
62
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
65
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static GTY(()) tree saved_trees;
70 static vec<int> inline_parm_levels;
71
72 static GTY(()) struct tinst_level *current_tinst_level;
73
74 static GTY(()) tree saved_access_scope;
75
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
80
81 // -------------------------------------------------------------------------- //
82 // Local Specialization Stack
83 //
84 // Implementation of the RAII helper for creating new local
85 // specializations.
86 local_specialization_stack::local_specialization_stack ()
87 : saved (local_specializations)
88 {
89 local_specializations = new hash_map<tree, tree>;
90 }
91
92 local_specialization_stack::~local_specialization_stack ()
93 {
94 delete local_specializations;
95 local_specializations = saved;
96 }
97
98 /* True if we've recursed into fn_type_unification too many times. */
99 static bool excessive_deduction_depth;
100
101 struct GTY((for_user)) spec_entry
102 {
103 tree tmpl;
104 tree args;
105 tree spec;
106 };
107
108 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 {
110 static hashval_t hash (spec_entry *);
111 static bool equal (spec_entry *, spec_entry *);
112 };
113
114 static GTY (()) hash_table<spec_hasher> *decl_specializations;
115
116 static GTY (()) hash_table<spec_hasher> *type_specializations;
117
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
123
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132
133 enum template_base_result {
134 tbr_incomplete_type,
135 tbr_ambiguous_baseclass,
136 tbr_success
137 };
138
139 static void push_access_scope (tree);
140 static void pop_access_scope (tree);
141 static bool resolve_overloaded_unification (tree, tree, tree, tree,
142 unification_kind_t, int,
143 bool);
144 static int try_one_overload (tree, tree, tree, tree, tree,
145 unification_kind_t, int, bool, bool);
146 static int unify (tree, tree, tree, tree, int, bool);
147 static void add_pending_template (tree);
148 static tree reopen_tinst_level (struct tinst_level *);
149 static tree tsubst_initializer_list (tree, tree);
150 static tree get_partial_spec_bindings (tree, tree, tree, tree);
151 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
154 bool, bool);
155 static void tsubst_enum (tree, tree, tree);
156 static tree add_to_template_args (tree, tree);
157 static tree add_outermost_template_args (tree, tree);
158 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
159 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
160 tree);
161 static int type_unification_real (tree, tree, tree, const tree *,
162 unsigned int, int, unification_kind_t, int,
163 vec<deferred_access_check, va_gc> **,
164 bool);
165 static void note_template_header (int);
166 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
167 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
168 static tree convert_template_argument (tree, tree, tree,
169 tsubst_flags_t, int, tree);
170 static int for_each_template_parm (tree, tree_fn_t, void*,
171 hash_set<tree> *, bool);
172 static tree expand_template_argument_pack (tree);
173 static tree build_template_parm_index (int, int, int, tree, tree);
174 static bool inline_needs_template_parms (tree, bool);
175 static void push_inline_template_parms_recursive (tree, int);
176 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
177 static int mark_template_parm (tree, void *);
178 static int template_parm_this_level_p (tree, void *);
179 static tree tsubst_friend_function (tree, tree);
180 static tree tsubst_friend_class (tree, tree);
181 static int can_complete_type_without_circularity (tree);
182 static tree get_bindings (tree, tree, tree, bool);
183 static int template_decl_level (tree);
184 static int check_cv_quals_for_unify (int, tree, tree);
185 static void template_parm_level_and_index (tree, int*, int*);
186 static int unify_pack_expansion (tree, tree, tree,
187 tree, unification_kind_t, bool, bool);
188 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
191 static void regenerate_decl_from_template (tree, tree);
192 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
193 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
194 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
195 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
196 static bool check_specialization_scope (void);
197 static tree process_partial_specialization (tree);
198 static void set_current_access_from_decl (tree);
199 static enum template_base_result get_template_base (tree, tree, tree, tree,
200 bool , tree *);
201 static tree try_class_unification (tree, tree, tree, tree, bool);
202 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
203 tree, tree);
204 static bool template_template_parm_bindings_ok_p (tree, tree);
205 static int template_args_equal (tree, tree);
206 static void tsubst_default_arguments (tree, tsubst_flags_t);
207 static tree for_each_template_parm_r (tree *, int *, void *);
208 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
209 static void copy_default_args_to_explicit_spec (tree);
210 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
211 static bool dependent_template_arg_p (tree);
212 static bool any_template_arguments_need_structural_equality_p (tree);
213 static bool dependent_type_p_r (tree);
214 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
229
230 static void
231 push_access_scope (tree t)
232 {
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
234 || TREE_CODE (t) == TYPE_DECL);
235
236 if (DECL_FRIEND_CONTEXT (t))
237 push_nested_class (DECL_FRIEND_CONTEXT (t));
238 else if (DECL_CLASS_SCOPE_P (t))
239 push_nested_class (DECL_CONTEXT (t));
240 else
241 push_to_top_level ();
242
243 if (TREE_CODE (t) == FUNCTION_DECL)
244 {
245 saved_access_scope = tree_cons
246 (NULL_TREE, current_function_decl, saved_access_scope);
247 current_function_decl = t;
248 }
249 }
250
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
253
254 static void
255 pop_access_scope (tree t)
256 {
257 if (TREE_CODE (t) == FUNCTION_DECL)
258 {
259 current_function_decl = TREE_VALUE (saved_access_scope);
260 saved_access_scope = TREE_CHAIN (saved_access_scope);
261 }
262
263 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
264 pop_nested_class ();
265 else
266 pop_from_top_level ();
267 }
268
269 /* Do any processing required when DECL (a member template
270 declaration) is finished. Returns the TEMPLATE_DECL corresponding
271 to DECL, unless it is a specialization, in which case the DECL
272 itself is returned. */
273
274 tree
275 finish_member_template_decl (tree decl)
276 {
277 if (decl == error_mark_node)
278 return error_mark_node;
279
280 gcc_assert (DECL_P (decl));
281
282 if (TREE_CODE (decl) == TYPE_DECL)
283 {
284 tree type;
285
286 type = TREE_TYPE (decl);
287 if (type == error_mark_node)
288 return error_mark_node;
289 if (MAYBE_CLASS_TYPE_P (type)
290 && CLASSTYPE_TEMPLATE_INFO (type)
291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
292 {
293 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
294 check_member_template (tmpl);
295 return tmpl;
296 }
297 return NULL_TREE;
298 }
299 else if (TREE_CODE (decl) == FIELD_DECL)
300 error ("data member %qD cannot be a member template", decl);
301 else if (DECL_TEMPLATE_INFO (decl))
302 {
303 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
304 {
305 check_member_template (DECL_TI_TEMPLATE (decl));
306 return DECL_TI_TEMPLATE (decl);
307 }
308 else
309 return decl;
310 }
311 else
312 error ("invalid member template declaration %qD", decl);
313
314 return error_mark_node;
315 }
316
317 /* Create a template info node. */
318
319 tree
320 build_template_info (tree template_decl, tree template_args)
321 {
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
326 }
327
328 /* Return the template info node corresponding to T, whatever T is. */
329
330 tree
331 get_template_info (const_tree t)
332 {
333 tree tinfo = NULL_TREE;
334
335 if (!t || t == error_mark_node)
336 return NULL;
337
338 if (TREE_CODE (t) == NAMESPACE_DECL)
339 return NULL;
340
341 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
342 tinfo = DECL_TEMPLATE_INFO (t);
343
344 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
345 t = TREE_TYPE (t);
346
347 if (OVERLOAD_TYPE_P (t))
348 tinfo = TYPE_TEMPLATE_INFO (t);
349 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
350 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
351
352 return tinfo;
353 }
354
355 /* Returns the template nesting level of the indicated class TYPE.
356
357 For example, in:
358 template <class T>
359 struct A
360 {
361 template <class U>
362 struct B {};
363 };
364
365 A<T>::B<U> has depth two, while A<T> has depth one.
366 Both A<T>::B<int> and A<int>::B<U> have depth one, if
367 they are instantiations, not specializations.
368
369 This function is guaranteed to return 0 if passed NULL_TREE so
370 that, for example, `template_class_depth (current_class_type)' is
371 always safe. */
372
373 int
374 template_class_depth (tree type)
375 {
376 int depth;
377
378 for (depth = 0;
379 type && TREE_CODE (type) != NAMESPACE_DECL;
380 type = (TREE_CODE (type) == FUNCTION_DECL)
381 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
382 {
383 tree tinfo = get_template_info (type);
384
385 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
387 ++depth;
388 }
389
390 return depth;
391 }
392
393 /* Subroutine of maybe_begin_member_template_processing.
394 Returns true if processing DECL needs us to push template parms. */
395
396 static bool
397 inline_needs_template_parms (tree decl, bool nsdmi)
398 {
399 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
400 return false;
401
402 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
403 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
404 }
405
406 /* Subroutine of maybe_begin_member_template_processing.
407 Push the template parms in PARMS, starting from LEVELS steps into the
408 chain, and ending at the beginning, since template parms are listed
409 innermost first. */
410
411 static void
412 push_inline_template_parms_recursive (tree parmlist, int levels)
413 {
414 tree parms = TREE_VALUE (parmlist);
415 int i;
416
417 if (levels > 1)
418 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
419
420 ++processing_template_decl;
421 current_template_parms
422 = tree_cons (size_int (processing_template_decl),
423 parms, current_template_parms);
424 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
425
426 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
427 NULL);
428 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
429 {
430 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
431
432 if (error_operand_p (parm))
433 continue;
434
435 gcc_assert (DECL_P (parm));
436
437 switch (TREE_CODE (parm))
438 {
439 case TYPE_DECL:
440 case TEMPLATE_DECL:
441 pushdecl (parm);
442 break;
443
444 case PARM_DECL:
445 {
446 /* Make a CONST_DECL as is done in process_template_parm.
447 It is ugly that we recreate this here; the original
448 version built in process_template_parm is no longer
449 available. */
450 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
451 CONST_DECL, DECL_NAME (parm),
452 TREE_TYPE (parm));
453 DECL_ARTIFICIAL (decl) = 1;
454 TREE_CONSTANT (decl) = 1;
455 TREE_READONLY (decl) = 1;
456 DECL_INITIAL (decl) = DECL_INITIAL (parm);
457 SET_DECL_TEMPLATE_PARM_P (decl);
458 pushdecl (decl);
459 }
460 break;
461
462 default:
463 gcc_unreachable ();
464 }
465 }
466 }
467
468 /* Restore the template parameter context for a member template, a
469 friend template defined in a class definition, or a non-template
470 member of template class. */
471
472 void
473 maybe_begin_member_template_processing (tree decl)
474 {
475 tree parms;
476 int levels = 0;
477 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
478
479 if (nsdmi)
480 {
481 tree ctx = DECL_CONTEXT (decl);
482 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
483 /* Disregard full specializations (c++/60999). */
484 && uses_template_parms (ctx)
485 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
486 }
487
488 if (inline_needs_template_parms (decl, nsdmi))
489 {
490 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
491 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
492
493 if (DECL_TEMPLATE_SPECIALIZATION (decl))
494 {
495 --levels;
496 parms = TREE_CHAIN (parms);
497 }
498
499 push_inline_template_parms_recursive (parms, levels);
500 }
501
502 /* Remember how many levels of template parameters we pushed so that
503 we can pop them later. */
504 inline_parm_levels.safe_push (levels);
505 }
506
507 /* Undo the effects of maybe_begin_member_template_processing. */
508
509 void
510 maybe_end_member_template_processing (void)
511 {
512 int i;
513 int last;
514
515 if (inline_parm_levels.length () == 0)
516 return;
517
518 last = inline_parm_levels.pop ();
519 for (i = 0; i < last; ++i)
520 {
521 --processing_template_decl;
522 current_template_parms = TREE_CHAIN (current_template_parms);
523 poplevel (0, 0, 0);
524 }
525 }
526
527 /* Return a new template argument vector which contains all of ARGS,
528 but has as its innermost set of arguments the EXTRA_ARGS. */
529
530 static tree
531 add_to_template_args (tree args, tree extra_args)
532 {
533 tree new_args;
534 int extra_depth;
535 int i;
536 int j;
537
538 if (args == NULL_TREE || extra_args == error_mark_node)
539 return extra_args;
540
541 extra_depth = TMPL_ARGS_DEPTH (extra_args);
542 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
543
544 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
545 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
546
547 for (j = 1; j <= extra_depth; ++j, ++i)
548 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
549
550 return new_args;
551 }
552
553 /* Like add_to_template_args, but only the outermost ARGS are added to
554 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
555 (EXTRA_ARGS) levels are added. This function is used to combine
556 the template arguments from a partial instantiation with the
557 template arguments used to attain the full instantiation from the
558 partial instantiation. */
559
560 static tree
561 add_outermost_template_args (tree args, tree extra_args)
562 {
563 tree new_args;
564
565 /* If there are more levels of EXTRA_ARGS than there are ARGS,
566 something very fishy is going on. */
567 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
568
569 /* If *all* the new arguments will be the EXTRA_ARGS, just return
570 them. */
571 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
572 return extra_args;
573
574 /* For the moment, we make ARGS look like it contains fewer levels. */
575 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
576
577 new_args = add_to_template_args (args, extra_args);
578
579 /* Now, we restore ARGS to its full dimensions. */
580 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
581
582 return new_args;
583 }
584
585 /* Return the N levels of innermost template arguments from the ARGS. */
586
587 tree
588 get_innermost_template_args (tree args, int n)
589 {
590 tree new_args;
591 int extra_levels;
592 int i;
593
594 gcc_assert (n >= 0);
595
596 /* If N is 1, just return the innermost set of template arguments. */
597 if (n == 1)
598 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
599
600 /* If we're not removing anything, just return the arguments we were
601 given. */
602 extra_levels = TMPL_ARGS_DEPTH (args) - n;
603 gcc_assert (extra_levels >= 0);
604 if (extra_levels == 0)
605 return args;
606
607 /* Make a new set of arguments, not containing the outer arguments. */
608 new_args = make_tree_vec (n);
609 for (i = 1; i <= n; ++i)
610 SET_TMPL_ARGS_LEVEL (new_args, i,
611 TMPL_ARGS_LEVEL (args, i + extra_levels));
612
613 return new_args;
614 }
615
616 /* The inverse of get_innermost_template_args: Return all but the innermost
617 EXTRA_LEVELS levels of template arguments from the ARGS. */
618
619 static tree
620 strip_innermost_template_args (tree args, int extra_levels)
621 {
622 tree new_args;
623 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
624 int i;
625
626 gcc_assert (n >= 0);
627
628 /* If N is 1, just return the outermost set of template arguments. */
629 if (n == 1)
630 return TMPL_ARGS_LEVEL (args, 1);
631
632 /* If we're not removing anything, just return the arguments we were
633 given. */
634 gcc_assert (extra_levels >= 0);
635 if (extra_levels == 0)
636 return args;
637
638 /* Make a new set of arguments, not containing the inner arguments. */
639 new_args = make_tree_vec (n);
640 for (i = 1; i <= n; ++i)
641 SET_TMPL_ARGS_LEVEL (new_args, i,
642 TMPL_ARGS_LEVEL (args, i));
643
644 return new_args;
645 }
646
647 /* We've got a template header coming up; push to a new level for storing
648 the parms. */
649
650 void
651 begin_template_parm_list (void)
652 {
653 /* We use a non-tag-transparent scope here, which causes pushtag to
654 put tags in this scope, rather than in the enclosing class or
655 namespace scope. This is the right thing, since we want
656 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
657 global template class, push_template_decl handles putting the
658 TEMPLATE_DECL into top-level scope. For a nested template class,
659 e.g.:
660
661 template <class T> struct S1 {
662 template <class T> struct S2 {};
663 };
664
665 pushtag contains special code to call pushdecl_with_scope on the
666 TEMPLATE_DECL for S2. */
667 begin_scope (sk_template_parms, NULL);
668 ++processing_template_decl;
669 ++processing_template_parmlist;
670 note_template_header (0);
671
672 /* Add a dummy parameter level while we process the parameter list. */
673 current_template_parms
674 = tree_cons (size_int (processing_template_decl),
675 make_tree_vec (0),
676 current_template_parms);
677 }
678
679 /* This routine is called when a specialization is declared. If it is
680 invalid to declare a specialization here, an error is reported and
681 false is returned, otherwise this routine will return true. */
682
683 static bool
684 check_specialization_scope (void)
685 {
686 tree scope = current_scope ();
687
688 /* [temp.expl.spec]
689
690 An explicit specialization shall be declared in the namespace of
691 which the template is a member, or, for member templates, in the
692 namespace of which the enclosing class or enclosing class
693 template is a member. An explicit specialization of a member
694 function, member class or static data member of a class template
695 shall be declared in the namespace of which the class template
696 is a member. */
697 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
698 {
699 error ("explicit specialization in non-namespace scope %qD", scope);
700 return false;
701 }
702
703 /* [temp.expl.spec]
704
705 In an explicit specialization declaration for a member of a class
706 template or a member template that appears in namespace scope,
707 the member template and some of its enclosing class templates may
708 remain unspecialized, except that the declaration shall not
709 explicitly specialize a class member template if its enclosing
710 class templates are not explicitly specialized as well. */
711 if (current_template_parms)
712 {
713 error ("enclosing class templates are not explicitly specialized");
714 return false;
715 }
716
717 return true;
718 }
719
720 /* We've just seen template <>. */
721
722 bool
723 begin_specialization (void)
724 {
725 begin_scope (sk_template_spec, NULL);
726 note_template_header (1);
727 return check_specialization_scope ();
728 }
729
730 /* Called at then end of processing a declaration preceded by
731 template<>. */
732
733 void
734 end_specialization (void)
735 {
736 finish_scope ();
737 reset_specialization ();
738 }
739
740 /* Any template <>'s that we have seen thus far are not referring to a
741 function specialization. */
742
743 void
744 reset_specialization (void)
745 {
746 processing_specialization = 0;
747 template_header_count = 0;
748 }
749
750 /* We've just seen a template header. If SPECIALIZATION is nonzero,
751 it was of the form template <>. */
752
753 static void
754 note_template_header (int specialization)
755 {
756 processing_specialization = specialization;
757 template_header_count++;
758 }
759
760 /* We're beginning an explicit instantiation. */
761
762 void
763 begin_explicit_instantiation (void)
764 {
765 gcc_assert (!processing_explicit_instantiation);
766 processing_explicit_instantiation = true;
767 }
768
769
770 void
771 end_explicit_instantiation (void)
772 {
773 gcc_assert (processing_explicit_instantiation);
774 processing_explicit_instantiation = false;
775 }
776
777 /* An explicit specialization or partial specialization of TMPL is being
778 declared. Check that the namespace in which the specialization is
779 occurring is permissible. Returns false iff it is invalid to
780 specialize TMPL in the current namespace. */
781
782 static bool
783 check_specialization_namespace (tree tmpl)
784 {
785 tree tpl_ns = decl_namespace_context (tmpl);
786
787 /* [tmpl.expl.spec]
788
789 An explicit specialization shall be declared in the namespace of
790 which the template is a member, or, for member templates, in the
791 namespace of which the enclosing class or enclosing class
792 template is a member. An explicit specialization of a member
793 function, member class or static data member of a class template
794 shall be declared in the namespace of which the class template is
795 a member. */
796 if (current_scope() != DECL_CONTEXT (tmpl)
797 && !at_namespace_scope_p ())
798 {
799 error ("specialization of %qD must appear at namespace scope", tmpl);
800 return false;
801 }
802 if (is_associated_namespace (current_namespace, tpl_ns))
803 /* Same or super-using namespace. */
804 return true;
805 else
806 {
807 permerror (input_location,
808 "specialization of %qD in different namespace", tmpl);
809 permerror (DECL_SOURCE_LOCATION (tmpl),
810 " from definition of %q#D", tmpl);
811 return false;
812 }
813 }
814
815 /* SPEC is an explicit instantiation. Check that it is valid to
816 perform this explicit instantiation in the current namespace. */
817
818 static void
819 check_explicit_instantiation_namespace (tree spec)
820 {
821 tree ns;
822
823 /* DR 275: An explicit instantiation shall appear in an enclosing
824 namespace of its template. */
825 ns = decl_namespace_context (spec);
826 if (!is_ancestor (current_namespace, ns))
827 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
828 "(which does not enclose namespace %qD)",
829 spec, current_namespace, ns);
830 }
831
832 // Returns the type of a template specialization only if that
833 // specialization needs to be defined. Otherwise (e.g., if the type has
834 // already been defined), the function returns NULL_TREE.
835 static tree
836 maybe_new_partial_specialization (tree type)
837 {
838 // An implicit instantiation of an incomplete type implies
839 // the definition of a new class template.
840 //
841 // template<typename T>
842 // struct S;
843 //
844 // template<typename T>
845 // struct S<T*>;
846 //
847 // Here, S<T*> is an implicit instantiation of S whose type
848 // is incomplete.
849 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
850 return type;
851
852 // It can also be the case that TYPE is a completed specialization.
853 // Continuing the previous example, suppose we also declare:
854 //
855 // template<typename T>
856 // requires Integral<T>
857 // struct S<T*>;
858 //
859 // Here, S<T*> refers to the specialization S<T*> defined
860 // above. However, we need to differentiate definitions because
861 // we intend to define a new partial specialization. In this case,
862 // we rely on the fact that the constraints are different for
863 // this declaration than that above.
864 //
865 // Note that we also get here for injected class names and
866 // late-parsed template definitions. We must ensure that we
867 // do not create new type declarations for those cases.
868 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
869 {
870 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
871 tree args = CLASSTYPE_TI_ARGS (type);
872
873 // If there are no template parameters, this cannot be a new
874 // partial template specializtion?
875 if (!current_template_parms)
876 return NULL_TREE;
877
878 // If the constraints are not the same as those of the primary
879 // then, we can probably create a new specialization.
880 tree type_constr = current_template_constraints ();
881
882 if (type == TREE_TYPE (tmpl))
883 if (tree main_constr = get_constraints (tmpl))
884 if (equivalent_constraints (type_constr, main_constr))
885 return NULL_TREE;
886
887 // Also, if there's a pre-existing specialization with matching
888 // constraints, then this also isn't new.
889 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
890 while (specs)
891 {
892 tree spec_tmpl = TREE_VALUE (specs);
893 tree spec_args = TREE_PURPOSE (specs);
894 tree spec_constr = get_constraints (spec_tmpl);
895 if (comp_template_args (args, spec_args)
896 && equivalent_constraints (type_constr, spec_constr))
897 return NULL_TREE;
898 specs = TREE_CHAIN (specs);
899 }
900
901 // Create a new type node (and corresponding type decl)
902 // for the newly declared specialization.
903 tree t = make_class_type (TREE_CODE (type));
904 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
905 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (type);
906 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
907
908 /* We only need a separate type node for storing the definition of this
909 partial specialization; uses of S<T*> are unconstrained, so all are
910 equivalent. So keep TYPE_CANONICAL the same. */
911 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
912
913 // Build the corresponding type decl.
914 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
915 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
916 DECL_SOURCE_LOCATION (d) = input_location;
917
918 return t;
919 }
920
921 return NULL_TREE;
922 }
923
924 /* The TYPE is being declared. If it is a template type, that means it
925 is a partial specialization. Do appropriate error-checking. */
926
927 tree
928 maybe_process_partial_specialization (tree type)
929 {
930 tree context;
931
932 if (type == error_mark_node)
933 return error_mark_node;
934
935 /* A lambda that appears in specialization context is not itself a
936 specialization. */
937 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
938 return type;
939
940 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
941 {
942 error ("name of class shadows template template parameter %qD",
943 TYPE_NAME (type));
944 return error_mark_node;
945 }
946
947 context = TYPE_CONTEXT (type);
948
949 if (TYPE_ALIAS_P (type))
950 {
951 if (TYPE_TEMPLATE_INFO (type)
952 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
953 error ("specialization of alias template %qD",
954 TYPE_TI_TEMPLATE (type));
955 else
956 error ("explicit specialization of non-template %qT", type);
957 return error_mark_node;
958 }
959 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
960 {
961 /* This is for ordinary explicit specialization and partial
962 specialization of a template class such as:
963
964 template <> class C<int>;
965
966 or:
967
968 template <class T> class C<T*>;
969
970 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971
972 if (tree t = maybe_new_partial_specialization (type))
973 {
974 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
975 && !at_namespace_scope_p ())
976 return error_mark_node;
977 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
978 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
979 if (processing_template_decl)
980 {
981 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
982 if (decl == error_mark_node)
983 return error_mark_node;
984 return TREE_TYPE (decl);
985 }
986 }
987 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
988 error ("specialization of %qT after instantiation", type);
989 else if (errorcount && !processing_specialization
990 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
991 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
992 /* Trying to define a specialization either without a template<> header
993 or in an inappropriate place. We've already given an error, so just
994 bail now so we don't actually define the specialization. */
995 return error_mark_node;
996 }
997 else if (CLASS_TYPE_P (type)
998 && !CLASSTYPE_USE_TEMPLATE (type)
999 && CLASSTYPE_TEMPLATE_INFO (type)
1000 && context && CLASS_TYPE_P (context)
1001 && CLASSTYPE_TEMPLATE_INFO (context))
1002 {
1003 /* This is for an explicit specialization of member class
1004 template according to [temp.expl.spec/18]:
1005
1006 template <> template <class U> class C<int>::D;
1007
1008 The context `C<int>' must be an implicit instantiation.
1009 Otherwise this is just a member class template declared
1010 earlier like:
1011
1012 template <> class C<int> { template <class U> class D; };
1013 template <> template <class U> class C<int>::D;
1014
1015 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1016 while in the second case, `C<int>::D' is a primary template
1017 and `C<T>::D' may not exist. */
1018
1019 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1020 && !COMPLETE_TYPE_P (type))
1021 {
1022 tree t;
1023 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1024
1025 if (current_namespace
1026 != decl_namespace_context (tmpl))
1027 {
1028 permerror (input_location,
1029 "specializing %q#T in different namespace", type);
1030 permerror (DECL_SOURCE_LOCATION (tmpl),
1031 " from definition of %q#D", tmpl);
1032 }
1033
1034 /* Check for invalid specialization after instantiation:
1035
1036 template <> template <> class C<int>::D<int>;
1037 template <> template <class U> class C<int>::D; */
1038
1039 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1040 t; t = TREE_CHAIN (t))
1041 {
1042 tree inst = TREE_VALUE (t);
1043 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1044 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1045 {
1046 /* We already have a full specialization of this partial
1047 instantiation, or a full specialization has been
1048 looked up but not instantiated. Reassign it to the
1049 new member specialization template. */
1050 spec_entry elt;
1051 spec_entry *entry;
1052
1053 elt.tmpl = most_general_template (tmpl);
1054 elt.args = CLASSTYPE_TI_ARGS (inst);
1055 elt.spec = inst;
1056
1057 type_specializations->remove_elt (&elt);
1058
1059 elt.tmpl = tmpl;
1060 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1061
1062 spec_entry **slot
1063 = type_specializations->find_slot (&elt, INSERT);
1064 entry = ggc_alloc<spec_entry> ();
1065 *entry = elt;
1066 *slot = entry;
1067 }
1068 else
1069 /* But if we've had an implicit instantiation, that's a
1070 problem ([temp.expl.spec]/6). */
1071 error ("specialization %qT after instantiation %qT",
1072 type, inst);
1073 }
1074
1075 /* Mark TYPE as a specialization. And as a result, we only
1076 have one level of template argument for the innermost
1077 class template. */
1078 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1079 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1080 CLASSTYPE_TI_ARGS (type)
1081 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1082 }
1083 }
1084 else if (processing_specialization)
1085 {
1086 /* Someday C++0x may allow for enum template specialization. */
1087 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1088 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1089 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1090 "of %qD not allowed by ISO C++", type);
1091 else
1092 {
1093 error ("explicit specialization of non-template %qT", type);
1094 return error_mark_node;
1095 }
1096 }
1097
1098 return type;
1099 }
1100
1101 /* Returns nonzero if we can optimize the retrieval of specializations
1102 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1103 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1104
1105 static inline bool
1106 optimize_specialization_lookup_p (tree tmpl)
1107 {
1108 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1109 && DECL_CLASS_SCOPE_P (tmpl)
1110 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1111 parameter. */
1112 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1113 /* The optimized lookup depends on the fact that the
1114 template arguments for the member function template apply
1115 purely to the containing class, which is not true if the
1116 containing class is an explicit or partial
1117 specialization. */
1118 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1119 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1120 && !DECL_CONV_FN_P (tmpl)
1121 /* It is possible to have a template that is not a member
1122 template and is not a member of a template class:
1123
1124 template <typename T>
1125 struct S { friend A::f(); };
1126
1127 Here, the friend function is a template, but the context does
1128 not have template information. The optimized lookup relies
1129 on having ARGS be the template arguments for both the class
1130 and the function template. */
1131 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1132 }
1133
1134 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1135 gone through coerce_template_parms by now. */
1136
1137 static void
1138 check_unstripped_args (tree args ATTRIBUTE_UNUSED)
1139 {
1140 #ifdef ENABLE_CHECKING
1141 ++processing_template_decl;
1142 if (!any_dependent_template_arguments_p (args))
1143 {
1144 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1145 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1146 {
1147 tree arg = TREE_VEC_ELT (inner, i);
1148 if (TREE_CODE (arg) == TEMPLATE_DECL)
1149 /* OK */;
1150 else if (TYPE_P (arg))
1151 gcc_assert (strip_typedefs (arg, NULL) == arg);
1152 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1153 /* Allow typedefs on the type of a non-type argument, since a
1154 parameter can have them. */;
1155 else
1156 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1157 }
1158 }
1159 --processing_template_decl;
1160 #endif
1161 }
1162
1163 /* Retrieve the specialization (in the sense of [temp.spec] - a
1164 specialization is either an instantiation or an explicit
1165 specialization) of TMPL for the given template ARGS. If there is
1166 no such specialization, return NULL_TREE. The ARGS are a vector of
1167 arguments, or a vector of vectors of arguments, in the case of
1168 templates with more than one level of parameters.
1169
1170 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1171 then we search for a partial specialization matching ARGS. This
1172 parameter is ignored if TMPL is not a class template.
1173
1174 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1175 result is a NONTYPE_ARGUMENT_PACK. */
1176
1177 static tree
1178 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1179 {
1180 if (tmpl == NULL_TREE)
1181 return NULL_TREE;
1182
1183 if (args == error_mark_node)
1184 return NULL_TREE;
1185
1186 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1187 || TREE_CODE (tmpl) == FIELD_DECL);
1188
1189 /* There should be as many levels of arguments as there are
1190 levels of parameters. */
1191 gcc_assert (TMPL_ARGS_DEPTH (args)
1192 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1193 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1194 : template_class_depth (DECL_CONTEXT (tmpl))));
1195
1196 check_unstripped_args (args);
1197
1198 if (optimize_specialization_lookup_p (tmpl))
1199 {
1200 tree class_template;
1201 tree class_specialization;
1202 vec<tree, va_gc> *methods;
1203 tree fns;
1204 int idx;
1205
1206 /* The template arguments actually apply to the containing
1207 class. Find the class specialization with those
1208 arguments. */
1209 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1210 class_specialization
1211 = retrieve_specialization (class_template, args, 0);
1212 if (!class_specialization)
1213 return NULL_TREE;
1214 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1215 for the specialization. */
1216 idx = class_method_index_for_fn (class_specialization, tmpl);
1217 if (idx == -1)
1218 return NULL_TREE;
1219 /* Iterate through the methods with the indicated name, looking
1220 for the one that has an instance of TMPL. */
1221 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1222 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1223 {
1224 tree fn = OVL_CURRENT (fns);
1225 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1226 /* using-declarations can add base methods to the method vec,
1227 and we don't want those here. */
1228 && DECL_CONTEXT (fn) == class_specialization)
1229 return fn;
1230 }
1231 return NULL_TREE;
1232 }
1233 else
1234 {
1235 spec_entry *found;
1236 spec_entry elt;
1237 hash_table<spec_hasher> *specializations;
1238
1239 elt.tmpl = tmpl;
1240 elt.args = args;
1241 elt.spec = NULL_TREE;
1242
1243 if (DECL_CLASS_TEMPLATE_P (tmpl))
1244 specializations = type_specializations;
1245 else
1246 specializations = decl_specializations;
1247
1248 if (hash == 0)
1249 hash = spec_hasher::hash (&elt);
1250 found = specializations->find_with_hash (&elt, hash);
1251 if (found)
1252 return found->spec;
1253 }
1254
1255 return NULL_TREE;
1256 }
1257
1258 /* Like retrieve_specialization, but for local declarations. */
1259
1260 tree
1261 retrieve_local_specialization (tree tmpl)
1262 {
1263 if (local_specializations == NULL)
1264 return NULL_TREE;
1265
1266 tree *slot = local_specializations->get (tmpl);
1267 return slot ? *slot : NULL_TREE;
1268 }
1269
1270 /* Returns nonzero iff DECL is a specialization of TMPL. */
1271
1272 int
1273 is_specialization_of (tree decl, tree tmpl)
1274 {
1275 tree t;
1276
1277 if (TREE_CODE (decl) == FUNCTION_DECL)
1278 {
1279 for (t = decl;
1280 t != NULL_TREE;
1281 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1282 if (t == tmpl)
1283 return 1;
1284 }
1285 else
1286 {
1287 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1288
1289 for (t = TREE_TYPE (decl);
1290 t != NULL_TREE;
1291 t = CLASSTYPE_USE_TEMPLATE (t)
1292 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1293 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1294 return 1;
1295 }
1296
1297 return 0;
1298 }
1299
1300 /* Returns nonzero iff DECL is a specialization of friend declaration
1301 FRIEND_DECL according to [temp.friend]. */
1302
1303 bool
1304 is_specialization_of_friend (tree decl, tree friend_decl)
1305 {
1306 bool need_template = true;
1307 int template_depth;
1308
1309 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1310 || TREE_CODE (decl) == TYPE_DECL);
1311
1312 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1313 of a template class, we want to check if DECL is a specialization
1314 if this. */
1315 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1316 && DECL_TEMPLATE_INFO (friend_decl)
1317 && !DECL_USE_TEMPLATE (friend_decl))
1318 {
1319 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1320 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1321 need_template = false;
1322 }
1323 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1324 && !PRIMARY_TEMPLATE_P (friend_decl))
1325 need_template = false;
1326
1327 /* There is nothing to do if this is not a template friend. */
1328 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1329 return false;
1330
1331 if (is_specialization_of (decl, friend_decl))
1332 return true;
1333
1334 /* [temp.friend/6]
1335 A member of a class template may be declared to be a friend of a
1336 non-template class. In this case, the corresponding member of
1337 every specialization of the class template is a friend of the
1338 class granting friendship.
1339
1340 For example, given a template friend declaration
1341
1342 template <class T> friend void A<T>::f();
1343
1344 the member function below is considered a friend
1345
1346 template <> struct A<int> {
1347 void f();
1348 };
1349
1350 For this type of template friend, TEMPLATE_DEPTH below will be
1351 nonzero. To determine if DECL is a friend of FRIEND, we first
1352 check if the enclosing class is a specialization of another. */
1353
1354 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1355 if (template_depth
1356 && DECL_CLASS_SCOPE_P (decl)
1357 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1358 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1359 {
1360 /* Next, we check the members themselves. In order to handle
1361 a few tricky cases, such as when FRIEND_DECL's are
1362
1363 template <class T> friend void A<T>::g(T t);
1364 template <class T> template <T t> friend void A<T>::h();
1365
1366 and DECL's are
1367
1368 void A<int>::g(int);
1369 template <int> void A<int>::h();
1370
1371 we need to figure out ARGS, the template arguments from
1372 the context of DECL. This is required for template substitution
1373 of `T' in the function parameter of `g' and template parameter
1374 of `h' in the above examples. Here ARGS corresponds to `int'. */
1375
1376 tree context = DECL_CONTEXT (decl);
1377 tree args = NULL_TREE;
1378 int current_depth = 0;
1379
1380 while (current_depth < template_depth)
1381 {
1382 if (CLASSTYPE_TEMPLATE_INFO (context))
1383 {
1384 if (current_depth == 0)
1385 args = TYPE_TI_ARGS (context);
1386 else
1387 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1388 current_depth++;
1389 }
1390 context = TYPE_CONTEXT (context);
1391 }
1392
1393 if (TREE_CODE (decl) == FUNCTION_DECL)
1394 {
1395 bool is_template;
1396 tree friend_type;
1397 tree decl_type;
1398 tree friend_args_type;
1399 tree decl_args_type;
1400
1401 /* Make sure that both DECL and FRIEND_DECL are templates or
1402 non-templates. */
1403 is_template = DECL_TEMPLATE_INFO (decl)
1404 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1405 if (need_template ^ is_template)
1406 return false;
1407 else if (is_template)
1408 {
1409 /* If both are templates, check template parameter list. */
1410 tree friend_parms
1411 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1412 args, tf_none);
1413 if (!comp_template_parms
1414 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1415 friend_parms))
1416 return false;
1417
1418 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1419 }
1420 else
1421 decl_type = TREE_TYPE (decl);
1422
1423 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1424 tf_none, NULL_TREE);
1425 if (friend_type == error_mark_node)
1426 return false;
1427
1428 /* Check if return types match. */
1429 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1430 return false;
1431
1432 /* Check if function parameter types match, ignoring the
1433 `this' parameter. */
1434 friend_args_type = TYPE_ARG_TYPES (friend_type);
1435 decl_args_type = TYPE_ARG_TYPES (decl_type);
1436 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1437 friend_args_type = TREE_CHAIN (friend_args_type);
1438 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1439 decl_args_type = TREE_CHAIN (decl_args_type);
1440
1441 return compparms (decl_args_type, friend_args_type);
1442 }
1443 else
1444 {
1445 /* DECL is a TYPE_DECL */
1446 bool is_template;
1447 tree decl_type = TREE_TYPE (decl);
1448
1449 /* Make sure that both DECL and FRIEND_DECL are templates or
1450 non-templates. */
1451 is_template
1452 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1453 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1454
1455 if (need_template ^ is_template)
1456 return false;
1457 else if (is_template)
1458 {
1459 tree friend_parms;
1460 /* If both are templates, check the name of the two
1461 TEMPLATE_DECL's first because is_friend didn't. */
1462 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1463 != DECL_NAME (friend_decl))
1464 return false;
1465
1466 /* Now check template parameter list. */
1467 friend_parms
1468 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1469 args, tf_none);
1470 return comp_template_parms
1471 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1472 friend_parms);
1473 }
1474 else
1475 return (DECL_NAME (decl)
1476 == DECL_NAME (friend_decl));
1477 }
1478 }
1479 return false;
1480 }
1481
1482 /* Register the specialization SPEC as a specialization of TMPL with
1483 the indicated ARGS. IS_FRIEND indicates whether the specialization
1484 is actually just a friend declaration. Returns SPEC, or an
1485 equivalent prior declaration, if available.
1486
1487 We also store instantiations of field packs in the hash table, even
1488 though they are not themselves templates, to make lookup easier. */
1489
1490 static tree
1491 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1492 hashval_t hash)
1493 {
1494 tree fn;
1495 spec_entry **slot = NULL;
1496 spec_entry elt;
1497
1498 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1499 || (TREE_CODE (tmpl) == FIELD_DECL
1500 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1501
1502 if (TREE_CODE (spec) == FUNCTION_DECL
1503 && uses_template_parms (DECL_TI_ARGS (spec)))
1504 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1505 register it; we want the corresponding TEMPLATE_DECL instead.
1506 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1507 the more obvious `uses_template_parms (spec)' to avoid problems
1508 with default function arguments. In particular, given
1509 something like this:
1510
1511 template <class T> void f(T t1, T t = T())
1512
1513 the default argument expression is not substituted for in an
1514 instantiation unless and until it is actually needed. */
1515 return spec;
1516
1517 if (optimize_specialization_lookup_p (tmpl))
1518 /* We don't put these specializations in the hash table, but we might
1519 want to give an error about a mismatch. */
1520 fn = retrieve_specialization (tmpl, args, 0);
1521 else
1522 {
1523 elt.tmpl = tmpl;
1524 elt.args = args;
1525 elt.spec = spec;
1526
1527 if (hash == 0)
1528 hash = spec_hasher::hash (&elt);
1529
1530 slot =
1531 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1532 if (*slot)
1533 fn = ((spec_entry *) *slot)->spec;
1534 else
1535 fn = NULL_TREE;
1536 }
1537
1538 /* We can sometimes try to re-register a specialization that we've
1539 already got. In particular, regenerate_decl_from_template calls
1540 duplicate_decls which will update the specialization list. But,
1541 we'll still get called again here anyhow. It's more convenient
1542 to simply allow this than to try to prevent it. */
1543 if (fn == spec)
1544 return spec;
1545 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1546 {
1547 if (DECL_TEMPLATE_INSTANTIATION (fn))
1548 {
1549 if (DECL_ODR_USED (fn)
1550 || DECL_EXPLICIT_INSTANTIATION (fn))
1551 {
1552 error ("specialization of %qD after instantiation",
1553 fn);
1554 return error_mark_node;
1555 }
1556 else
1557 {
1558 tree clone;
1559 /* This situation should occur only if the first
1560 specialization is an implicit instantiation, the
1561 second is an explicit specialization, and the
1562 implicit instantiation has not yet been used. That
1563 situation can occur if we have implicitly
1564 instantiated a member function and then specialized
1565 it later.
1566
1567 We can also wind up here if a friend declaration that
1568 looked like an instantiation turns out to be a
1569 specialization:
1570
1571 template <class T> void foo(T);
1572 class S { friend void foo<>(int) };
1573 template <> void foo(int);
1574
1575 We transform the existing DECL in place so that any
1576 pointers to it become pointers to the updated
1577 declaration.
1578
1579 If there was a definition for the template, but not
1580 for the specialization, we want this to look as if
1581 there were no definition, and vice versa. */
1582 DECL_INITIAL (fn) = NULL_TREE;
1583 duplicate_decls (spec, fn, is_friend);
1584 /* The call to duplicate_decls will have applied
1585 [temp.expl.spec]:
1586
1587 An explicit specialization of a function template
1588 is inline only if it is explicitly declared to be,
1589 and independently of whether its function template
1590 is.
1591
1592 to the primary function; now copy the inline bits to
1593 the various clones. */
1594 FOR_EACH_CLONE (clone, fn)
1595 {
1596 DECL_DECLARED_INLINE_P (clone)
1597 = DECL_DECLARED_INLINE_P (fn);
1598 DECL_SOURCE_LOCATION (clone)
1599 = DECL_SOURCE_LOCATION (fn);
1600 DECL_DELETED_FN (clone)
1601 = DECL_DELETED_FN (fn);
1602 }
1603 check_specialization_namespace (tmpl);
1604
1605 return fn;
1606 }
1607 }
1608 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1609 {
1610 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1611 /* Dup decl failed, but this is a new definition. Set the
1612 line number so any errors match this new
1613 definition. */
1614 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1615
1616 return fn;
1617 }
1618 }
1619 else if (fn)
1620 return duplicate_decls (spec, fn, is_friend);
1621
1622 /* A specialization must be declared in the same namespace as the
1623 template it is specializing. */
1624 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1625 && !check_specialization_namespace (tmpl))
1626 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1627
1628 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1629 {
1630 spec_entry *entry = ggc_alloc<spec_entry> ();
1631 gcc_assert (tmpl && args && spec);
1632 *entry = elt;
1633 *slot = entry;
1634 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1635 && PRIMARY_TEMPLATE_P (tmpl)
1636 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1637 || variable_template_p (tmpl))
1638 /* If TMPL is a forward declaration of a template function, keep a list
1639 of all specializations in case we need to reassign them to a friend
1640 template later in tsubst_friend_function.
1641
1642 Also keep a list of all variable template instantiations so that
1643 process_partial_specialization can check whether a later partial
1644 specialization would have used it. */
1645 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1646 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1647 }
1648
1649 return spec;
1650 }
1651
1652 /* Returns true iff two spec_entry nodes are equivalent. */
1653
1654 int comparing_specializations;
1655
1656 bool
1657 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1658 {
1659 int equal;
1660
1661 ++comparing_specializations;
1662 equal = (e1->tmpl == e2->tmpl
1663 && comp_template_args (e1->args, e2->args));
1664 if (equal && flag_concepts
1665 /* tmpl could be a FIELD_DECL for a capture pack. */
1666 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1667 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1668 && uses_template_parms (e1->args))
1669 {
1670 /* Partial specializations of a variable template can be distinguished by
1671 constraints. */
1672 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1673 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1674 equal = equivalent_constraints (c1, c2);
1675 }
1676 --comparing_specializations;
1677
1678 return equal;
1679 }
1680
1681 /* Returns a hash for a template TMPL and template arguments ARGS. */
1682
1683 static hashval_t
1684 hash_tmpl_and_args (tree tmpl, tree args)
1685 {
1686 hashval_t val = DECL_UID (tmpl);
1687 return iterative_hash_template_arg (args, val);
1688 }
1689
1690 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1691 ignoring SPEC. */
1692
1693 hashval_t
1694 spec_hasher::hash (spec_entry *e)
1695 {
1696 return hash_tmpl_and_args (e->tmpl, e->args);
1697 }
1698
1699 /* Recursively calculate a hash value for a template argument ARG, for use
1700 in the hash tables of template specializations. */
1701
1702 hashval_t
1703 iterative_hash_template_arg (tree arg, hashval_t val)
1704 {
1705 unsigned HOST_WIDE_INT i;
1706 enum tree_code code;
1707 char tclass;
1708
1709 if (arg == NULL_TREE)
1710 return iterative_hash_object (arg, val);
1711
1712 if (!TYPE_P (arg))
1713 STRIP_NOPS (arg);
1714
1715 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1716 /* We can get one of these when re-hashing a previous entry in the middle
1717 of substituting into a pack expansion. Just look through it. */
1718 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1719
1720 code = TREE_CODE (arg);
1721 tclass = TREE_CODE_CLASS (code);
1722
1723 val = iterative_hash_object (code, val);
1724
1725 switch (code)
1726 {
1727 case ERROR_MARK:
1728 return val;
1729
1730 case IDENTIFIER_NODE:
1731 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1732
1733 case TREE_VEC:
1734 {
1735 int i, len = TREE_VEC_LENGTH (arg);
1736 for (i = 0; i < len; ++i)
1737 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1738 return val;
1739 }
1740
1741 case TYPE_PACK_EXPANSION:
1742 case EXPR_PACK_EXPANSION:
1743 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1744 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1745
1746 case TYPE_ARGUMENT_PACK:
1747 case NONTYPE_ARGUMENT_PACK:
1748 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1749
1750 case TREE_LIST:
1751 for (; arg; arg = TREE_CHAIN (arg))
1752 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1753 return val;
1754
1755 case OVERLOAD:
1756 for (; arg; arg = OVL_NEXT (arg))
1757 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1758 return val;
1759
1760 case CONSTRUCTOR:
1761 {
1762 tree field, value;
1763 iterative_hash_template_arg (TREE_TYPE (arg), val);
1764 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1765 {
1766 val = iterative_hash_template_arg (field, val);
1767 val = iterative_hash_template_arg (value, val);
1768 }
1769 return val;
1770 }
1771
1772 case PARM_DECL:
1773 if (!DECL_ARTIFICIAL (arg))
1774 {
1775 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1776 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1777 }
1778 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1779
1780 case TARGET_EXPR:
1781 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1782
1783 case PTRMEM_CST:
1784 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1785 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1786
1787 case TEMPLATE_PARM_INDEX:
1788 val = iterative_hash_template_arg
1789 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1790 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1791 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1792
1793 case TRAIT_EXPR:
1794 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1795 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1796 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1797
1798 case BASELINK:
1799 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1800 val);
1801 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1802 val);
1803
1804 case MODOP_EXPR:
1805 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1806 code = TREE_CODE (TREE_OPERAND (arg, 1));
1807 val = iterative_hash_object (code, val);
1808 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1809
1810 case LAMBDA_EXPR:
1811 /* A lambda can't appear in a template arg, but don't crash on
1812 erroneous input. */
1813 gcc_assert (seen_error ());
1814 return val;
1815
1816 case CAST_EXPR:
1817 case IMPLICIT_CONV_EXPR:
1818 case STATIC_CAST_EXPR:
1819 case REINTERPRET_CAST_EXPR:
1820 case CONST_CAST_EXPR:
1821 case DYNAMIC_CAST_EXPR:
1822 case NEW_EXPR:
1823 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1824 /* Now hash operands as usual. */
1825 break;
1826
1827 default:
1828 break;
1829 }
1830
1831 switch (tclass)
1832 {
1833 case tcc_type:
1834 if (alias_template_specialization_p (arg))
1835 {
1836 // We want an alias specialization that survived strip_typedefs
1837 // to hash differently from its TYPE_CANONICAL, to avoid hash
1838 // collisions that compare as different in template_args_equal.
1839 // These could be dependent specializations that strip_typedefs
1840 // left alone, or untouched specializations because
1841 // coerce_template_parms returns the unconverted template
1842 // arguments if it sees incomplete argument packs.
1843 tree ti = TYPE_TEMPLATE_INFO (arg);
1844 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1845 }
1846 if (TYPE_CANONICAL (arg))
1847 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1848 val);
1849 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1850 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1851 /* Otherwise just compare the types during lookup. */
1852 return val;
1853
1854 case tcc_declaration:
1855 case tcc_constant:
1856 return iterative_hash_expr (arg, val);
1857
1858 default:
1859 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1860 {
1861 unsigned n = cp_tree_operand_length (arg);
1862 for (i = 0; i < n; ++i)
1863 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1864 return val;
1865 }
1866 }
1867 gcc_unreachable ();
1868 return 0;
1869 }
1870
1871 /* Unregister the specialization SPEC as a specialization of TMPL.
1872 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1873 if the SPEC was listed as a specialization of TMPL.
1874
1875 Note that SPEC has been ggc_freed, so we can't look inside it. */
1876
1877 bool
1878 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1879 {
1880 spec_entry *entry;
1881 spec_entry elt;
1882
1883 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1884 elt.args = TI_ARGS (tinfo);
1885 elt.spec = NULL_TREE;
1886
1887 entry = decl_specializations->find (&elt);
1888 if (entry != NULL)
1889 {
1890 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1891 gcc_assert (new_spec != NULL_TREE);
1892 entry->spec = new_spec;
1893 return 1;
1894 }
1895
1896 return 0;
1897 }
1898
1899 /* Like register_specialization, but for local declarations. We are
1900 registering SPEC, an instantiation of TMPL. */
1901
1902 void
1903 register_local_specialization (tree spec, tree tmpl)
1904 {
1905 local_specializations->put (tmpl, spec);
1906 }
1907
1908 /* TYPE is a class type. Returns true if TYPE is an explicitly
1909 specialized class. */
1910
1911 bool
1912 explicit_class_specialization_p (tree type)
1913 {
1914 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1915 return false;
1916 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1917 }
1918
1919 /* Print the list of functions at FNS, going through all the overloads
1920 for each element of the list. Alternatively, FNS can not be a
1921 TREE_LIST, in which case it will be printed together with all the
1922 overloads.
1923
1924 MORE and *STR should respectively be FALSE and NULL when the function
1925 is called from the outside. They are used internally on recursive
1926 calls. print_candidates manages the two parameters and leaves NULL
1927 in *STR when it ends. */
1928
1929 static void
1930 print_candidates_1 (tree fns, bool more, const char **str)
1931 {
1932 tree fn, fn2;
1933 char *spaces = NULL;
1934
1935 for (fn = fns; fn; fn = OVL_NEXT (fn))
1936 if (TREE_CODE (fn) == TREE_LIST)
1937 {
1938 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1939 print_candidates_1 (TREE_VALUE (fn2),
1940 TREE_CHAIN (fn2) || more, str);
1941 }
1942 else
1943 {
1944 tree cand = OVL_CURRENT (fn);
1945 if (!*str)
1946 {
1947 /* Pick the prefix string. */
1948 if (!more && !OVL_NEXT (fns))
1949 {
1950 inform (DECL_SOURCE_LOCATION (cand),
1951 "candidate is: %#D", cand);
1952 continue;
1953 }
1954
1955 *str = _("candidates are:");
1956 spaces = get_spaces (*str);
1957 }
1958 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1959 *str = spaces ? spaces : *str;
1960 }
1961
1962 if (!more)
1963 {
1964 free (spaces);
1965 *str = NULL;
1966 }
1967 }
1968
1969 /* Print the list of candidate FNS in an error message. FNS can also
1970 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1971
1972 void
1973 print_candidates (tree fns)
1974 {
1975 const char *str = NULL;
1976 print_candidates_1 (fns, false, &str);
1977 gcc_assert (str == NULL);
1978 }
1979
1980 /* Get a (possibly) constrained template declaration for the
1981 purpose of ordering candidates. */
1982 static tree
1983 get_template_for_ordering (tree list)
1984 {
1985 gcc_assert (TREE_CODE (list) == TREE_LIST);
1986 tree f = TREE_VALUE (list);
1987 if (tree ti = DECL_TEMPLATE_INFO (f))
1988 return TI_TEMPLATE (ti);
1989 return f;
1990 }
1991
1992 /* Among candidates having the same signature, return the
1993 most constrained or NULL_TREE if there is no best candidate.
1994 If the signatures of candidates vary (e.g., template
1995 specialization vs. member function), then there can be no
1996 most constrained.
1997
1998 Note that we don't compare constraints on the functions
1999 themselves, but rather those of their templates. */
2000 static tree
2001 most_constrained_function (tree candidates)
2002 {
2003 // Try to find the best candidate in a first pass.
2004 tree champ = candidates;
2005 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2006 {
2007 int winner = more_constrained (get_template_for_ordering (champ),
2008 get_template_for_ordering (c));
2009 if (winner == -1)
2010 champ = c; // The candidate is more constrained
2011 else if (winner == 0)
2012 return NULL_TREE; // Neither is more constrained
2013 }
2014
2015 // Verify that the champ is better than previous candidates.
2016 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2017 if (!more_constrained (get_template_for_ordering (champ),
2018 get_template_for_ordering (c)))
2019 return NULL_TREE;
2020 }
2021
2022 return champ;
2023 }
2024
2025
2026 /* Returns the template (one of the functions given by TEMPLATE_ID)
2027 which can be specialized to match the indicated DECL with the
2028 explicit template args given in TEMPLATE_ID. The DECL may be
2029 NULL_TREE if none is available. In that case, the functions in
2030 TEMPLATE_ID are non-members.
2031
2032 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2033 specialization of a member template.
2034
2035 The TEMPLATE_COUNT is the number of references to qualifying
2036 template classes that appeared in the name of the function. See
2037 check_explicit_specialization for a more accurate description.
2038
2039 TSK indicates what kind of template declaration (if any) is being
2040 declared. TSK_TEMPLATE indicates that the declaration given by
2041 DECL, though a FUNCTION_DECL, has template parameters, and is
2042 therefore a template function.
2043
2044 The template args (those explicitly specified and those deduced)
2045 are output in a newly created vector *TARGS_OUT.
2046
2047 If it is impossible to determine the result, an error message is
2048 issued. The error_mark_node is returned to indicate failure. */
2049
2050 static tree
2051 determine_specialization (tree template_id,
2052 tree decl,
2053 tree* targs_out,
2054 int need_member_template,
2055 int template_count,
2056 tmpl_spec_kind tsk)
2057 {
2058 tree fns;
2059 tree targs;
2060 tree explicit_targs;
2061 tree candidates = NULL_TREE;
2062
2063 /* A TREE_LIST of templates of which DECL may be a specialization.
2064 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2065 corresponding TREE_PURPOSE is the set of template arguments that,
2066 when used to instantiate the template, would produce a function
2067 with the signature of DECL. */
2068 tree templates = NULL_TREE;
2069 int header_count;
2070 cp_binding_level *b;
2071
2072 *targs_out = NULL_TREE;
2073
2074 if (template_id == error_mark_node || decl == error_mark_node)
2075 return error_mark_node;
2076
2077 /* We shouldn't be specializing a member template of an
2078 unspecialized class template; we already gave an error in
2079 check_specialization_scope, now avoid crashing. */
2080 if (template_count && DECL_CLASS_SCOPE_P (decl)
2081 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2082 {
2083 gcc_assert (errorcount);
2084 return error_mark_node;
2085 }
2086
2087 fns = TREE_OPERAND (template_id, 0);
2088 explicit_targs = TREE_OPERAND (template_id, 1);
2089
2090 if (fns == error_mark_node)
2091 return error_mark_node;
2092
2093 /* Check for baselinks. */
2094 if (BASELINK_P (fns))
2095 fns = BASELINK_FUNCTIONS (fns);
2096
2097 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2098 {
2099 error ("%qD is not a function template", fns);
2100 return error_mark_node;
2101 }
2102 else if (VAR_P (decl) && !variable_template_p (fns))
2103 {
2104 error ("%qD is not a variable template", fns);
2105 return error_mark_node;
2106 }
2107
2108 /* Count the number of template headers specified for this
2109 specialization. */
2110 header_count = 0;
2111 for (b = current_binding_level;
2112 b->kind == sk_template_parms;
2113 b = b->level_chain)
2114 ++header_count;
2115
2116 tree orig_fns = fns;
2117
2118 if (variable_template_p (fns))
2119 {
2120 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2121 targs = coerce_template_parms (parms, explicit_targs, fns,
2122 tf_warning_or_error,
2123 /*req_all*/true, /*use_defarg*/true);
2124 if (targs != error_mark_node)
2125 templates = tree_cons (targs, fns, templates);
2126 }
2127 else for (; fns; fns = OVL_NEXT (fns))
2128 {
2129 tree fn = OVL_CURRENT (fns);
2130
2131 if (TREE_CODE (fn) == TEMPLATE_DECL)
2132 {
2133 tree decl_arg_types;
2134 tree fn_arg_types;
2135 tree insttype;
2136
2137 /* In case of explicit specialization, we need to check if
2138 the number of template headers appearing in the specialization
2139 is correct. This is usually done in check_explicit_specialization,
2140 but the check done there cannot be exhaustive when specializing
2141 member functions. Consider the following code:
2142
2143 template <> void A<int>::f(int);
2144 template <> template <> void A<int>::f(int);
2145
2146 Assuming that A<int> is not itself an explicit specialization
2147 already, the first line specializes "f" which is a non-template
2148 member function, whilst the second line specializes "f" which
2149 is a template member function. So both lines are syntactically
2150 correct, and check_explicit_specialization does not reject
2151 them.
2152
2153 Here, we can do better, as we are matching the specialization
2154 against the declarations. We count the number of template
2155 headers, and we check if they match TEMPLATE_COUNT + 1
2156 (TEMPLATE_COUNT is the number of qualifying template classes,
2157 plus there must be another header for the member template
2158 itself).
2159
2160 Notice that if header_count is zero, this is not a
2161 specialization but rather a template instantiation, so there
2162 is no check we can perform here. */
2163 if (header_count && header_count != template_count + 1)
2164 continue;
2165
2166 /* Check that the number of template arguments at the
2167 innermost level for DECL is the same as for FN. */
2168 if (current_binding_level->kind == sk_template_parms
2169 && !current_binding_level->explicit_spec_p
2170 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2171 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2172 (current_template_parms))))
2173 continue;
2174
2175 /* DECL might be a specialization of FN. */
2176 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2177 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2178
2179 /* For a non-static member function, we need to make sure
2180 that the const qualification is the same. Since
2181 get_bindings does not try to merge the "this" parameter,
2182 we must do the comparison explicitly. */
2183 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2184 && !same_type_p (TREE_VALUE (fn_arg_types),
2185 TREE_VALUE (decl_arg_types)))
2186 continue;
2187
2188 /* Skip the "this" parameter and, for constructors of
2189 classes with virtual bases, the VTT parameter. A
2190 full specialization of a constructor will have a VTT
2191 parameter, but a template never will. */
2192 decl_arg_types
2193 = skip_artificial_parms_for (decl, decl_arg_types);
2194 fn_arg_types
2195 = skip_artificial_parms_for (fn, fn_arg_types);
2196
2197 /* Function templates cannot be specializations; there are
2198 no partial specializations of functions. Therefore, if
2199 the type of DECL does not match FN, there is no
2200 match.
2201
2202 Note that it should never be the case that we have both
2203 candidates added here, and for regular member functions
2204 below. */
2205 if (tsk == tsk_template)
2206 {
2207 if (compparms (fn_arg_types, decl_arg_types))
2208 candidates = tree_cons (NULL_TREE, fn, candidates);
2209 continue;
2210 }
2211
2212 /* See whether this function might be a specialization of this
2213 template. Suppress access control because we might be trying
2214 to make this specialization a friend, and we have already done
2215 access control for the declaration of the specialization. */
2216 push_deferring_access_checks (dk_no_check);
2217 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2218 pop_deferring_access_checks ();
2219
2220 if (!targs)
2221 /* We cannot deduce template arguments that when used to
2222 specialize TMPL will produce DECL. */
2223 continue;
2224
2225 /* Remove, from the set of candidates, all those functions
2226 whose constraints are not satisfied. */
2227 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2228 continue;
2229
2230 // Then, try to form the new function type.
2231 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2232 if (insttype == error_mark_node)
2233 continue;
2234 fn_arg_types
2235 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2236 if (!compparms (fn_arg_types, decl_arg_types))
2237 continue;
2238
2239 /* Save this template, and the arguments deduced. */
2240 templates = tree_cons (targs, fn, templates);
2241 }
2242 else if (need_member_template)
2243 /* FN is an ordinary member function, and we need a
2244 specialization of a member template. */
2245 ;
2246 else if (TREE_CODE (fn) != FUNCTION_DECL)
2247 /* We can get IDENTIFIER_NODEs here in certain erroneous
2248 cases. */
2249 ;
2250 else if (!DECL_FUNCTION_MEMBER_P (fn))
2251 /* This is just an ordinary non-member function. Nothing can
2252 be a specialization of that. */
2253 ;
2254 else if (DECL_ARTIFICIAL (fn))
2255 /* Cannot specialize functions that are created implicitly. */
2256 ;
2257 else
2258 {
2259 tree decl_arg_types;
2260
2261 /* This is an ordinary member function. However, since
2262 we're here, we can assume its enclosing class is a
2263 template class. For example,
2264
2265 template <typename T> struct S { void f(); };
2266 template <> void S<int>::f() {}
2267
2268 Here, S<int>::f is a non-template, but S<int> is a
2269 template class. If FN has the same type as DECL, we
2270 might be in business. */
2271
2272 if (!DECL_TEMPLATE_INFO (fn))
2273 /* Its enclosing class is an explicit specialization
2274 of a template class. This is not a candidate. */
2275 continue;
2276
2277 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2278 TREE_TYPE (TREE_TYPE (fn))))
2279 /* The return types differ. */
2280 continue;
2281
2282 /* Adjust the type of DECL in case FN is a static member. */
2283 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2284 if (DECL_STATIC_FUNCTION_P (fn)
2285 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2286 decl_arg_types = TREE_CHAIN (decl_arg_types);
2287
2288 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2289 decl_arg_types))
2290 continue;
2291
2292 // If the deduced arguments do not satisfy the constraints,
2293 // this is not a candidate.
2294 if (flag_concepts && !constraints_satisfied_p (fn))
2295 continue;
2296
2297 // Add the candidate.
2298 candidates = tree_cons (NULL_TREE, fn, candidates);
2299 }
2300 }
2301
2302 if (templates && TREE_CHAIN (templates))
2303 {
2304 /* We have:
2305
2306 [temp.expl.spec]
2307
2308 It is possible for a specialization with a given function
2309 signature to be instantiated from more than one function
2310 template. In such cases, explicit specification of the
2311 template arguments must be used to uniquely identify the
2312 function template specialization being specialized.
2313
2314 Note that here, there's no suggestion that we're supposed to
2315 determine which of the candidate templates is most
2316 specialized. However, we, also have:
2317
2318 [temp.func.order]
2319
2320 Partial ordering of overloaded function template
2321 declarations is used in the following contexts to select
2322 the function template to which a function template
2323 specialization refers:
2324
2325 -- when an explicit specialization refers to a function
2326 template.
2327
2328 So, we do use the partial ordering rules, at least for now.
2329 This extension can only serve to make invalid programs valid,
2330 so it's safe. And, there is strong anecdotal evidence that
2331 the committee intended the partial ordering rules to apply;
2332 the EDG front end has that behavior, and John Spicer claims
2333 that the committee simply forgot to delete the wording in
2334 [temp.expl.spec]. */
2335 tree tmpl = most_specialized_instantiation (templates);
2336 if (tmpl != error_mark_node)
2337 {
2338 templates = tmpl;
2339 TREE_CHAIN (templates) = NULL_TREE;
2340 }
2341 }
2342
2343 // Concepts allows multiple declarations of member functions
2344 // with the same signature. Like above, we need to rely on
2345 // on the partial ordering of those candidates to determine which
2346 // is the best.
2347 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2348 {
2349 if (tree cand = most_constrained_function (candidates))
2350 {
2351 candidates = cand;
2352 TREE_CHAIN (cand) = NULL_TREE;
2353 }
2354 }
2355
2356 if (templates == NULL_TREE && candidates == NULL_TREE)
2357 {
2358 error ("template-id %qD for %q+D does not match any template "
2359 "declaration", template_id, decl);
2360 if (header_count && header_count != template_count + 1)
2361 inform (input_location, "saw %d %<template<>%>, need %d for "
2362 "specializing a member function template",
2363 header_count, template_count + 1);
2364 else
2365 print_candidates (orig_fns);
2366 return error_mark_node;
2367 }
2368 else if ((templates && TREE_CHAIN (templates))
2369 || (candidates && TREE_CHAIN (candidates))
2370 || (templates && candidates))
2371 {
2372 error ("ambiguous template specialization %qD for %q+D",
2373 template_id, decl);
2374 candidates = chainon (candidates, templates);
2375 print_candidates (candidates);
2376 return error_mark_node;
2377 }
2378
2379 /* We have one, and exactly one, match. */
2380 if (candidates)
2381 {
2382 tree fn = TREE_VALUE (candidates);
2383 *targs_out = copy_node (DECL_TI_ARGS (fn));
2384
2385 // Propagate the candidate's constraints to the declaration.
2386 set_constraints (decl, get_constraints (fn));
2387
2388 /* DECL is a re-declaration or partial instantiation of a template
2389 function. */
2390 if (TREE_CODE (fn) == TEMPLATE_DECL)
2391 return fn;
2392 /* It was a specialization of an ordinary member function in a
2393 template class. */
2394 return DECL_TI_TEMPLATE (fn);
2395 }
2396
2397 /* It was a specialization of a template. */
2398 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2399 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2400 {
2401 *targs_out = copy_node (targs);
2402 SET_TMPL_ARGS_LEVEL (*targs_out,
2403 TMPL_ARGS_DEPTH (*targs_out),
2404 TREE_PURPOSE (templates));
2405 }
2406 else
2407 *targs_out = TREE_PURPOSE (templates);
2408 return TREE_VALUE (templates);
2409 }
2410
2411 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2412 but with the default argument values filled in from those in the
2413 TMPL_TYPES. */
2414
2415 static tree
2416 copy_default_args_to_explicit_spec_1 (tree spec_types,
2417 tree tmpl_types)
2418 {
2419 tree new_spec_types;
2420
2421 if (!spec_types)
2422 return NULL_TREE;
2423
2424 if (spec_types == void_list_node)
2425 return void_list_node;
2426
2427 /* Substitute into the rest of the list. */
2428 new_spec_types =
2429 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2430 TREE_CHAIN (tmpl_types));
2431
2432 /* Add the default argument for this parameter. */
2433 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2434 TREE_VALUE (spec_types),
2435 new_spec_types);
2436 }
2437
2438 /* DECL is an explicit specialization. Replicate default arguments
2439 from the template it specializes. (That way, code like:
2440
2441 template <class T> void f(T = 3);
2442 template <> void f(double);
2443 void g () { f (); }
2444
2445 works, as required.) An alternative approach would be to look up
2446 the correct default arguments at the call-site, but this approach
2447 is consistent with how implicit instantiations are handled. */
2448
2449 static void
2450 copy_default_args_to_explicit_spec (tree decl)
2451 {
2452 tree tmpl;
2453 tree spec_types;
2454 tree tmpl_types;
2455 tree new_spec_types;
2456 tree old_type;
2457 tree new_type;
2458 tree t;
2459 tree object_type = NULL_TREE;
2460 tree in_charge = NULL_TREE;
2461 tree vtt = NULL_TREE;
2462
2463 /* See if there's anything we need to do. */
2464 tmpl = DECL_TI_TEMPLATE (decl);
2465 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2466 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2467 if (TREE_PURPOSE (t))
2468 break;
2469 if (!t)
2470 return;
2471
2472 old_type = TREE_TYPE (decl);
2473 spec_types = TYPE_ARG_TYPES (old_type);
2474
2475 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2476 {
2477 /* Remove the this pointer, but remember the object's type for
2478 CV quals. */
2479 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2480 spec_types = TREE_CHAIN (spec_types);
2481 tmpl_types = TREE_CHAIN (tmpl_types);
2482
2483 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2484 {
2485 /* DECL may contain more parameters than TMPL due to the extra
2486 in-charge parameter in constructors and destructors. */
2487 in_charge = spec_types;
2488 spec_types = TREE_CHAIN (spec_types);
2489 }
2490 if (DECL_HAS_VTT_PARM_P (decl))
2491 {
2492 vtt = spec_types;
2493 spec_types = TREE_CHAIN (spec_types);
2494 }
2495 }
2496
2497 /* Compute the merged default arguments. */
2498 new_spec_types =
2499 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2500
2501 /* Compute the new FUNCTION_TYPE. */
2502 if (object_type)
2503 {
2504 if (vtt)
2505 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2506 TREE_VALUE (vtt),
2507 new_spec_types);
2508
2509 if (in_charge)
2510 /* Put the in-charge parameter back. */
2511 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2512 TREE_VALUE (in_charge),
2513 new_spec_types);
2514
2515 new_type = build_method_type_directly (object_type,
2516 TREE_TYPE (old_type),
2517 new_spec_types);
2518 }
2519 else
2520 new_type = build_function_type (TREE_TYPE (old_type),
2521 new_spec_types);
2522 new_type = cp_build_type_attribute_variant (new_type,
2523 TYPE_ATTRIBUTES (old_type));
2524 new_type = build_exception_variant (new_type,
2525 TYPE_RAISES_EXCEPTIONS (old_type));
2526
2527 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2528 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2529
2530 TREE_TYPE (decl) = new_type;
2531 }
2532
2533 /* Return the number of template headers we expect to see for a definition
2534 or specialization of CTYPE or one of its non-template members. */
2535
2536 int
2537 num_template_headers_for_class (tree ctype)
2538 {
2539 int num_templates = 0;
2540
2541 while (ctype && CLASS_TYPE_P (ctype))
2542 {
2543 /* You're supposed to have one `template <...>' for every
2544 template class, but you don't need one for a full
2545 specialization. For example:
2546
2547 template <class T> struct S{};
2548 template <> struct S<int> { void f(); };
2549 void S<int>::f () {}
2550
2551 is correct; there shouldn't be a `template <>' for the
2552 definition of `S<int>::f'. */
2553 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2554 /* If CTYPE does not have template information of any
2555 kind, then it is not a template, nor is it nested
2556 within a template. */
2557 break;
2558 if (explicit_class_specialization_p (ctype))
2559 break;
2560 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2561 ++num_templates;
2562
2563 ctype = TYPE_CONTEXT (ctype);
2564 }
2565
2566 return num_templates;
2567 }
2568
2569 /* Do a simple sanity check on the template headers that precede the
2570 variable declaration DECL. */
2571
2572 void
2573 check_template_variable (tree decl)
2574 {
2575 tree ctx = CP_DECL_CONTEXT (decl);
2576 int wanted = num_template_headers_for_class (ctx);
2577 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2578 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2579 {
2580 if (cxx_dialect < cxx14)
2581 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2582 "variable templates only available with "
2583 "-std=c++14 or -std=gnu++14");
2584
2585 // Namespace-scope variable templates should have a template header.
2586 ++wanted;
2587 }
2588 if (template_header_count > wanted)
2589 {
2590 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2591 "too many template headers for %D (should be %d)",
2592 decl, wanted);
2593 if (warned && CLASS_TYPE_P (ctx)
2594 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2595 inform (DECL_SOURCE_LOCATION (decl),
2596 "members of an explicitly specialized class are defined "
2597 "without a template header");
2598 }
2599 }
2600
2601 /* Check to see if the function just declared, as indicated in
2602 DECLARATOR, and in DECL, is a specialization of a function
2603 template. We may also discover that the declaration is an explicit
2604 instantiation at this point.
2605
2606 Returns DECL, or an equivalent declaration that should be used
2607 instead if all goes well. Issues an error message if something is
2608 amiss. Returns error_mark_node if the error is not easily
2609 recoverable.
2610
2611 FLAGS is a bitmask consisting of the following flags:
2612
2613 2: The function has a definition.
2614 4: The function is a friend.
2615
2616 The TEMPLATE_COUNT is the number of references to qualifying
2617 template classes that appeared in the name of the function. For
2618 example, in
2619
2620 template <class T> struct S { void f(); };
2621 void S<int>::f();
2622
2623 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2624 classes are not counted in the TEMPLATE_COUNT, so that in
2625
2626 template <class T> struct S {};
2627 template <> struct S<int> { void f(); }
2628 template <> void S<int>::f();
2629
2630 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2631 invalid; there should be no template <>.)
2632
2633 If the function is a specialization, it is marked as such via
2634 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2635 is set up correctly, and it is added to the list of specializations
2636 for that template. */
2637
2638 tree
2639 check_explicit_specialization (tree declarator,
2640 tree decl,
2641 int template_count,
2642 int flags)
2643 {
2644 int have_def = flags & 2;
2645 int is_friend = flags & 4;
2646 bool is_concept = flags & 8;
2647 int specialization = 0;
2648 int explicit_instantiation = 0;
2649 int member_specialization = 0;
2650 tree ctype = DECL_CLASS_CONTEXT (decl);
2651 tree dname = DECL_NAME (decl);
2652 tmpl_spec_kind tsk;
2653
2654 if (is_friend)
2655 {
2656 if (!processing_specialization)
2657 tsk = tsk_none;
2658 else
2659 tsk = tsk_excessive_parms;
2660 }
2661 else
2662 tsk = current_tmpl_spec_kind (template_count);
2663
2664 switch (tsk)
2665 {
2666 case tsk_none:
2667 if (processing_specialization && !VAR_P (decl))
2668 {
2669 specialization = 1;
2670 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2671 }
2672 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2673 {
2674 if (is_friend)
2675 /* This could be something like:
2676
2677 template <class T> void f(T);
2678 class S { friend void f<>(int); } */
2679 specialization = 1;
2680 else
2681 {
2682 /* This case handles bogus declarations like template <>
2683 template <class T> void f<int>(); */
2684
2685 error ("template-id %qD in declaration of primary template",
2686 declarator);
2687 return decl;
2688 }
2689 }
2690 break;
2691
2692 case tsk_invalid_member_spec:
2693 /* The error has already been reported in
2694 check_specialization_scope. */
2695 return error_mark_node;
2696
2697 case tsk_invalid_expl_inst:
2698 error ("template parameter list used in explicit instantiation");
2699
2700 /* Fall through. */
2701
2702 case tsk_expl_inst:
2703 if (have_def)
2704 error ("definition provided for explicit instantiation");
2705
2706 explicit_instantiation = 1;
2707 break;
2708
2709 case tsk_excessive_parms:
2710 case tsk_insufficient_parms:
2711 if (tsk == tsk_excessive_parms)
2712 error ("too many template parameter lists in declaration of %qD",
2713 decl);
2714 else if (template_header_count)
2715 error("too few template parameter lists in declaration of %qD", decl);
2716 else
2717 error("explicit specialization of %qD must be introduced by "
2718 "%<template <>%>", decl);
2719
2720 /* Fall through. */
2721 case tsk_expl_spec:
2722 if (is_concept)
2723 error ("explicit specialization declared %<concept%>");
2724
2725 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2726 /* In cases like template<> constexpr bool v = true;
2727 We'll give an error in check_template_variable. */
2728 break;
2729
2730 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2731 if (ctype)
2732 member_specialization = 1;
2733 else
2734 specialization = 1;
2735 break;
2736
2737 case tsk_template:
2738 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2739 {
2740 /* This case handles bogus declarations like template <>
2741 template <class T> void f<int>(); */
2742
2743 if (!uses_template_parms (declarator))
2744 error ("template-id %qD in declaration of primary template",
2745 declarator);
2746 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2747 {
2748 /* Partial specialization of variable template. */
2749 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2750 specialization = 1;
2751 goto ok;
2752 }
2753 else if (cxx_dialect < cxx14)
2754 error ("non-type partial specialization %qD "
2755 "is not allowed", declarator);
2756 else
2757 error ("non-class, non-variable partial specialization %qD "
2758 "is not allowed", declarator);
2759 return decl;
2760 ok:;
2761 }
2762
2763 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2764 /* This is a specialization of a member template, without
2765 specialization the containing class. Something like:
2766
2767 template <class T> struct S {
2768 template <class U> void f (U);
2769 };
2770 template <> template <class U> void S<int>::f(U) {}
2771
2772 That's a specialization -- but of the entire template. */
2773 specialization = 1;
2774 break;
2775
2776 default:
2777 gcc_unreachable ();
2778 }
2779
2780 if ((specialization || member_specialization)
2781 /* This doesn't apply to variable templates. */
2782 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2783 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2784 {
2785 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2786 for (; t; t = TREE_CHAIN (t))
2787 if (TREE_PURPOSE (t))
2788 {
2789 permerror (input_location,
2790 "default argument specified in explicit specialization");
2791 break;
2792 }
2793 }
2794
2795 if (specialization || member_specialization || explicit_instantiation)
2796 {
2797 tree tmpl = NULL_TREE;
2798 tree targs = NULL_TREE;
2799 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2800
2801 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2802 if (!was_template_id)
2803 {
2804 tree fns;
2805
2806 gcc_assert (identifier_p (declarator));
2807 if (ctype)
2808 fns = dname;
2809 else
2810 {
2811 /* If there is no class context, the explicit instantiation
2812 must be at namespace scope. */
2813 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2814
2815 /* Find the namespace binding, using the declaration
2816 context. */
2817 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2818 false, true);
2819 if (fns == error_mark_node || !is_overloaded_fn (fns))
2820 {
2821 error ("%qD is not a template function", dname);
2822 fns = error_mark_node;
2823 }
2824 else
2825 {
2826 tree fn = OVL_CURRENT (fns);
2827 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2828 CP_DECL_CONTEXT (fn)))
2829 error ("%qD is not declared in %qD",
2830 decl, current_namespace);
2831 }
2832 }
2833
2834 declarator = lookup_template_function (fns, NULL_TREE);
2835 }
2836
2837 if (declarator == error_mark_node)
2838 return error_mark_node;
2839
2840 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2841 {
2842 if (!explicit_instantiation)
2843 /* A specialization in class scope. This is invalid,
2844 but the error will already have been flagged by
2845 check_specialization_scope. */
2846 return error_mark_node;
2847 else
2848 {
2849 /* It's not valid to write an explicit instantiation in
2850 class scope, e.g.:
2851
2852 class C { template void f(); }
2853
2854 This case is caught by the parser. However, on
2855 something like:
2856
2857 template class C { void f(); };
2858
2859 (which is invalid) we can get here. The error will be
2860 issued later. */
2861 ;
2862 }
2863
2864 return decl;
2865 }
2866 else if (ctype != NULL_TREE
2867 && (identifier_p (TREE_OPERAND (declarator, 0))))
2868 {
2869 // We'll match variable templates in start_decl.
2870 if (VAR_P (decl))
2871 return decl;
2872
2873 /* Find the list of functions in ctype that have the same
2874 name as the declared function. */
2875 tree name = TREE_OPERAND (declarator, 0);
2876 tree fns = NULL_TREE;
2877 int idx;
2878
2879 if (constructor_name_p (name, ctype))
2880 {
2881 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2882
2883 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2884 : !CLASSTYPE_DESTRUCTORS (ctype))
2885 {
2886 /* From [temp.expl.spec]:
2887
2888 If such an explicit specialization for the member
2889 of a class template names an implicitly-declared
2890 special member function (clause _special_), the
2891 program is ill-formed.
2892
2893 Similar language is found in [temp.explicit]. */
2894 error ("specialization of implicitly-declared special member function");
2895 return error_mark_node;
2896 }
2897
2898 name = is_constructor ? ctor_identifier : dtor_identifier;
2899 }
2900
2901 if (!DECL_CONV_FN_P (decl))
2902 {
2903 idx = lookup_fnfields_1 (ctype, name);
2904 if (idx >= 0)
2905 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2906 }
2907 else
2908 {
2909 vec<tree, va_gc> *methods;
2910 tree ovl;
2911
2912 /* For a type-conversion operator, we cannot do a
2913 name-based lookup. We might be looking for `operator
2914 int' which will be a specialization of `operator T'.
2915 So, we find *all* the conversion operators, and then
2916 select from them. */
2917 fns = NULL_TREE;
2918
2919 methods = CLASSTYPE_METHOD_VEC (ctype);
2920 if (methods)
2921 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2922 methods->iterate (idx, &ovl);
2923 ++idx)
2924 {
2925 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2926 /* There are no more conversion functions. */
2927 break;
2928
2929 /* Glue all these conversion functions together
2930 with those we already have. */
2931 for (; ovl; ovl = OVL_NEXT (ovl))
2932 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2933 }
2934 }
2935
2936 if (fns == NULL_TREE)
2937 {
2938 error ("no member function %qD declared in %qT", name, ctype);
2939 return error_mark_node;
2940 }
2941 else
2942 TREE_OPERAND (declarator, 0) = fns;
2943 }
2944
2945 /* Figure out what exactly is being specialized at this point.
2946 Note that for an explicit instantiation, even one for a
2947 member function, we cannot tell apriori whether the
2948 instantiation is for a member template, or just a member
2949 function of a template class. Even if a member template is
2950 being instantiated, the member template arguments may be
2951 elided if they can be deduced from the rest of the
2952 declaration. */
2953 tmpl = determine_specialization (declarator, decl,
2954 &targs,
2955 member_specialization,
2956 template_count,
2957 tsk);
2958
2959 if (!tmpl || tmpl == error_mark_node)
2960 /* We couldn't figure out what this declaration was
2961 specializing. */
2962 return error_mark_node;
2963 else
2964 {
2965 tree gen_tmpl = most_general_template (tmpl);
2966
2967 if (explicit_instantiation)
2968 {
2969 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2970 is done by do_decl_instantiation later. */
2971
2972 int arg_depth = TMPL_ARGS_DEPTH (targs);
2973 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2974
2975 if (arg_depth > parm_depth)
2976 {
2977 /* If TMPL is not the most general template (for
2978 example, if TMPL is a friend template that is
2979 injected into namespace scope), then there will
2980 be too many levels of TARGS. Remove some of them
2981 here. */
2982 int i;
2983 tree new_targs;
2984
2985 new_targs = make_tree_vec (parm_depth);
2986 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2987 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2988 = TREE_VEC_ELT (targs, i);
2989 targs = new_targs;
2990 }
2991
2992 return instantiate_template (tmpl, targs, tf_error);
2993 }
2994
2995 /* If we thought that the DECL was a member function, but it
2996 turns out to be specializing a static member function,
2997 make DECL a static member function as well. */
2998 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2999 && DECL_STATIC_FUNCTION_P (tmpl)
3000 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3001 revert_static_member_fn (decl);
3002
3003 /* If this is a specialization of a member template of a
3004 template class, we want to return the TEMPLATE_DECL, not
3005 the specialization of it. */
3006 if (tsk == tsk_template && !was_template_id)
3007 {
3008 tree result = DECL_TEMPLATE_RESULT (tmpl);
3009 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3010 DECL_INITIAL (result) = NULL_TREE;
3011 if (have_def)
3012 {
3013 tree parm;
3014 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3015 DECL_SOURCE_LOCATION (result)
3016 = DECL_SOURCE_LOCATION (decl);
3017 /* We want to use the argument list specified in the
3018 definition, not in the original declaration. */
3019 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3020 for (parm = DECL_ARGUMENTS (result); parm;
3021 parm = DECL_CHAIN (parm))
3022 DECL_CONTEXT (parm) = result;
3023 }
3024 return register_specialization (tmpl, gen_tmpl, targs,
3025 is_friend, 0);
3026 }
3027
3028 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3029 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3030
3031 if (was_template_id)
3032 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3033
3034 /* Inherit default function arguments from the template
3035 DECL is specializing. */
3036 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3037 copy_default_args_to_explicit_spec (decl);
3038
3039 /* This specialization has the same protection as the
3040 template it specializes. */
3041 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3042 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3043
3044 /* 7.1.1-1 [dcl.stc]
3045
3046 A storage-class-specifier shall not be specified in an
3047 explicit specialization...
3048
3049 The parser rejects these, so unless action is taken here,
3050 explicit function specializations will always appear with
3051 global linkage.
3052
3053 The action recommended by the C++ CWG in response to C++
3054 defect report 605 is to make the storage class and linkage
3055 of the explicit specialization match the templated function:
3056
3057 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3058 */
3059 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3060 {
3061 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3062 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3063
3064 /* A concept cannot be specialized. */
3065 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3066 {
3067 error ("explicit specialization of function concept %qD",
3068 gen_tmpl);
3069 return error_mark_node;
3070 }
3071
3072 /* This specialization has the same linkage and visibility as
3073 the function template it specializes. */
3074 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3075 if (! TREE_PUBLIC (decl))
3076 {
3077 DECL_INTERFACE_KNOWN (decl) = 1;
3078 DECL_NOT_REALLY_EXTERN (decl) = 1;
3079 }
3080 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3081 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3082 {
3083 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3084 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3085 }
3086 }
3087
3088 /* If DECL is a friend declaration, declared using an
3089 unqualified name, the namespace associated with DECL may
3090 have been set incorrectly. For example, in:
3091
3092 template <typename T> void f(T);
3093 namespace N {
3094 struct S { friend void f<int>(int); }
3095 }
3096
3097 we will have set the DECL_CONTEXT for the friend
3098 declaration to N, rather than to the global namespace. */
3099 if (DECL_NAMESPACE_SCOPE_P (decl))
3100 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3101
3102 if (is_friend && !have_def)
3103 /* This is not really a declaration of a specialization.
3104 It's just the name of an instantiation. But, it's not
3105 a request for an instantiation, either. */
3106 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3107 else if (TREE_CODE (decl) == FUNCTION_DECL)
3108 /* A specialization is not necessarily COMDAT. */
3109 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3110 && DECL_DECLARED_INLINE_P (decl));
3111 else if (VAR_P (decl))
3112 DECL_COMDAT (decl) = false;
3113
3114 /* If this is a full specialization, register it so that we can find
3115 it again. Partial specializations will be registered in
3116 process_partial_specialization. */
3117 if (!processing_template_decl)
3118 decl = register_specialization (decl, gen_tmpl, targs,
3119 is_friend, 0);
3120
3121 /* A 'structor should already have clones. */
3122 gcc_assert (decl == error_mark_node
3123 || variable_template_p (tmpl)
3124 || !(DECL_CONSTRUCTOR_P (decl)
3125 || DECL_DESTRUCTOR_P (decl))
3126 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3127 }
3128 }
3129
3130 return decl;
3131 }
3132
3133 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3134 parameters. These are represented in the same format used for
3135 DECL_TEMPLATE_PARMS. */
3136
3137 int
3138 comp_template_parms (const_tree parms1, const_tree parms2)
3139 {
3140 const_tree p1;
3141 const_tree p2;
3142
3143 if (parms1 == parms2)
3144 return 1;
3145
3146 for (p1 = parms1, p2 = parms2;
3147 p1 != NULL_TREE && p2 != NULL_TREE;
3148 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3149 {
3150 tree t1 = TREE_VALUE (p1);
3151 tree t2 = TREE_VALUE (p2);
3152 int i;
3153
3154 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3155 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3156
3157 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3158 return 0;
3159
3160 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3161 {
3162 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3163 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3164
3165 /* If either of the template parameters are invalid, assume
3166 they match for the sake of error recovery. */
3167 if (error_operand_p (parm1) || error_operand_p (parm2))
3168 return 1;
3169
3170 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3171 return 0;
3172
3173 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3174 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3175 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3176 continue;
3177 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3178 return 0;
3179 }
3180 }
3181
3182 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3183 /* One set of parameters has more parameters lists than the
3184 other. */
3185 return 0;
3186
3187 return 1;
3188 }
3189
3190 /* Determine whether PARM is a parameter pack. */
3191
3192 bool
3193 template_parameter_pack_p (const_tree parm)
3194 {
3195 /* Determine if we have a non-type template parameter pack. */
3196 if (TREE_CODE (parm) == PARM_DECL)
3197 return (DECL_TEMPLATE_PARM_P (parm)
3198 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3199 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3200 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3201
3202 /* If this is a list of template parameters, we could get a
3203 TYPE_DECL or a TEMPLATE_DECL. */
3204 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3205 parm = TREE_TYPE (parm);
3206
3207 /* Otherwise it must be a type template parameter. */
3208 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3209 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3210 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3211 }
3212
3213 /* Determine if T is a function parameter pack. */
3214
3215 bool
3216 function_parameter_pack_p (const_tree t)
3217 {
3218 if (t && TREE_CODE (t) == PARM_DECL)
3219 return DECL_PACK_P (t);
3220 return false;
3221 }
3222
3223 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3224 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3225
3226 tree
3227 get_function_template_decl (const_tree primary_func_tmpl_inst)
3228 {
3229 if (! primary_func_tmpl_inst
3230 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3231 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3232 return NULL;
3233
3234 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3235 }
3236
3237 /* Return true iff the function parameter PARAM_DECL was expanded
3238 from the function parameter pack PACK. */
3239
3240 bool
3241 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3242 {
3243 if (DECL_ARTIFICIAL (param_decl)
3244 || !function_parameter_pack_p (pack))
3245 return false;
3246
3247 /* The parameter pack and its pack arguments have the same
3248 DECL_PARM_INDEX. */
3249 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3250 }
3251
3252 /* Determine whether ARGS describes a variadic template args list,
3253 i.e., one that is terminated by a template argument pack. */
3254
3255 static bool
3256 template_args_variadic_p (tree args)
3257 {
3258 int nargs;
3259 tree last_parm;
3260
3261 if (args == NULL_TREE)
3262 return false;
3263
3264 args = INNERMOST_TEMPLATE_ARGS (args);
3265 nargs = TREE_VEC_LENGTH (args);
3266
3267 if (nargs == 0)
3268 return false;
3269
3270 last_parm = TREE_VEC_ELT (args, nargs - 1);
3271
3272 return ARGUMENT_PACK_P (last_parm);
3273 }
3274
3275 /* Generate a new name for the parameter pack name NAME (an
3276 IDENTIFIER_NODE) that incorporates its */
3277
3278 static tree
3279 make_ith_pack_parameter_name (tree name, int i)
3280 {
3281 /* Munge the name to include the parameter index. */
3282 #define NUMBUF_LEN 128
3283 char numbuf[NUMBUF_LEN];
3284 char* newname;
3285 int newname_len;
3286
3287 if (name == NULL_TREE)
3288 return name;
3289 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3290 newname_len = IDENTIFIER_LENGTH (name)
3291 + strlen (numbuf) + 2;
3292 newname = (char*)alloca (newname_len);
3293 snprintf (newname, newname_len,
3294 "%s#%i", IDENTIFIER_POINTER (name), i);
3295 return get_identifier (newname);
3296 }
3297
3298 /* Return true if T is a primary function, class or alias template
3299 instantiation. */
3300
3301 bool
3302 primary_template_instantiation_p (const_tree t)
3303 {
3304 if (!t)
3305 return false;
3306
3307 if (TREE_CODE (t) == FUNCTION_DECL)
3308 return DECL_LANG_SPECIFIC (t)
3309 && DECL_TEMPLATE_INSTANTIATION (t)
3310 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3311 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3312 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3313 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3314 else if (alias_template_specialization_p (t))
3315 return true;
3316 return false;
3317 }
3318
3319 /* Return true if PARM is a template template parameter. */
3320
3321 bool
3322 template_template_parameter_p (const_tree parm)
3323 {
3324 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3325 }
3326
3327 /* Return true iff PARM is a DECL representing a type template
3328 parameter. */
3329
3330 bool
3331 template_type_parameter_p (const_tree parm)
3332 {
3333 return (parm
3334 && (TREE_CODE (parm) == TYPE_DECL
3335 || TREE_CODE (parm) == TEMPLATE_DECL)
3336 && DECL_TEMPLATE_PARM_P (parm));
3337 }
3338
3339 /* Return the template parameters of T if T is a
3340 primary template instantiation, NULL otherwise. */
3341
3342 tree
3343 get_primary_template_innermost_parameters (const_tree t)
3344 {
3345 tree parms = NULL, template_info = NULL;
3346
3347 if ((template_info = get_template_info (t))
3348 && primary_template_instantiation_p (t))
3349 parms = INNERMOST_TEMPLATE_PARMS
3350 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3351
3352 return parms;
3353 }
3354
3355 /* Return the template parameters of the LEVELth level from the full list
3356 of template parameters PARMS. */
3357
3358 tree
3359 get_template_parms_at_level (tree parms, int level)
3360 {
3361 tree p;
3362 if (!parms
3363 || TREE_CODE (parms) != TREE_LIST
3364 || level > TMPL_PARMS_DEPTH (parms))
3365 return NULL_TREE;
3366
3367 for (p = parms; p; p = TREE_CHAIN (p))
3368 if (TMPL_PARMS_DEPTH (p) == level)
3369 return p;
3370
3371 return NULL_TREE;
3372 }
3373
3374 /* Returns the template arguments of T if T is a template instantiation,
3375 NULL otherwise. */
3376
3377 tree
3378 get_template_innermost_arguments (const_tree t)
3379 {
3380 tree args = NULL, template_info = NULL;
3381
3382 if ((template_info = get_template_info (t))
3383 && TI_ARGS (template_info))
3384 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3385
3386 return args;
3387 }
3388
3389 /* Return the argument pack elements of T if T is a template argument pack,
3390 NULL otherwise. */
3391
3392 tree
3393 get_template_argument_pack_elems (const_tree t)
3394 {
3395 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3396 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3397 return NULL;
3398
3399 return ARGUMENT_PACK_ARGS (t);
3400 }
3401
3402 /* Structure used to track the progress of find_parameter_packs_r. */
3403 struct find_parameter_pack_data
3404 {
3405 /* TREE_LIST that will contain all of the parameter packs found by
3406 the traversal. */
3407 tree* parameter_packs;
3408
3409 /* Set of AST nodes that have been visited by the traversal. */
3410 hash_set<tree> *visited;
3411 };
3412
3413 /* Identifies all of the argument packs that occur in a template
3414 argument and appends them to the TREE_LIST inside DATA, which is a
3415 find_parameter_pack_data structure. This is a subroutine of
3416 make_pack_expansion and uses_parameter_packs. */
3417 static tree
3418 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3419 {
3420 tree t = *tp;
3421 struct find_parameter_pack_data* ppd =
3422 (struct find_parameter_pack_data*)data;
3423 bool parameter_pack_p = false;
3424
3425 /* Handle type aliases/typedefs. */
3426 if (TYPE_ALIAS_P (t))
3427 {
3428 if (TYPE_TEMPLATE_INFO (t))
3429 cp_walk_tree (&TYPE_TI_ARGS (t),
3430 &find_parameter_packs_r,
3431 ppd, ppd->visited);
3432 *walk_subtrees = 0;
3433 return NULL_TREE;
3434 }
3435
3436 /* Identify whether this is a parameter pack or not. */
3437 switch (TREE_CODE (t))
3438 {
3439 case TEMPLATE_PARM_INDEX:
3440 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3441 parameter_pack_p = true;
3442 break;
3443
3444 case TEMPLATE_TYPE_PARM:
3445 t = TYPE_MAIN_VARIANT (t);
3446 case TEMPLATE_TEMPLATE_PARM:
3447 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3448 parameter_pack_p = true;
3449 break;
3450
3451 case FIELD_DECL:
3452 case PARM_DECL:
3453 if (DECL_PACK_P (t))
3454 {
3455 /* We don't want to walk into the type of a PARM_DECL,
3456 because we don't want to see the type parameter pack. */
3457 *walk_subtrees = 0;
3458 parameter_pack_p = true;
3459 }
3460 break;
3461
3462 /* Look through a lambda capture proxy to the field pack. */
3463 case VAR_DECL:
3464 if (DECL_HAS_VALUE_EXPR_P (t))
3465 {
3466 tree v = DECL_VALUE_EXPR (t);
3467 cp_walk_tree (&v,
3468 &find_parameter_packs_r,
3469 ppd, ppd->visited);
3470 *walk_subtrees = 0;
3471 }
3472 else if (variable_template_specialization_p (t))
3473 {
3474 cp_walk_tree (&DECL_TI_ARGS (t),
3475 find_parameter_packs_r,
3476 ppd, ppd->visited);
3477 *walk_subtrees = 0;
3478 }
3479 break;
3480
3481 case BASES:
3482 parameter_pack_p = true;
3483 break;
3484 default:
3485 /* Not a parameter pack. */
3486 break;
3487 }
3488
3489 if (parameter_pack_p)
3490 {
3491 /* Add this parameter pack to the list. */
3492 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3493 }
3494
3495 if (TYPE_P (t))
3496 cp_walk_tree (&TYPE_CONTEXT (t),
3497 &find_parameter_packs_r, ppd, ppd->visited);
3498
3499 /* This switch statement will return immediately if we don't find a
3500 parameter pack. */
3501 switch (TREE_CODE (t))
3502 {
3503 case TEMPLATE_PARM_INDEX:
3504 return NULL_TREE;
3505
3506 case BOUND_TEMPLATE_TEMPLATE_PARM:
3507 /* Check the template itself. */
3508 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3509 &find_parameter_packs_r, ppd, ppd->visited);
3510 /* Check the template arguments. */
3511 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3512 ppd->visited);
3513 *walk_subtrees = 0;
3514 return NULL_TREE;
3515
3516 case TEMPLATE_TYPE_PARM:
3517 case TEMPLATE_TEMPLATE_PARM:
3518 return NULL_TREE;
3519
3520 case PARM_DECL:
3521 return NULL_TREE;
3522
3523 case RECORD_TYPE:
3524 if (TYPE_PTRMEMFUNC_P (t))
3525 return NULL_TREE;
3526 /* Fall through. */
3527
3528 case UNION_TYPE:
3529 case ENUMERAL_TYPE:
3530 if (TYPE_TEMPLATE_INFO (t))
3531 cp_walk_tree (&TYPE_TI_ARGS (t),
3532 &find_parameter_packs_r, ppd, ppd->visited);
3533
3534 *walk_subtrees = 0;
3535 return NULL_TREE;
3536
3537 case CONSTRUCTOR:
3538 case TEMPLATE_DECL:
3539 cp_walk_tree (&TREE_TYPE (t),
3540 &find_parameter_packs_r, ppd, ppd->visited);
3541 return NULL_TREE;
3542
3543 case TYPENAME_TYPE:
3544 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3545 ppd, ppd->visited);
3546 *walk_subtrees = 0;
3547 return NULL_TREE;
3548
3549 case TYPE_PACK_EXPANSION:
3550 case EXPR_PACK_EXPANSION:
3551 *walk_subtrees = 0;
3552 return NULL_TREE;
3553
3554 case INTEGER_TYPE:
3555 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3556 ppd, ppd->visited);
3557 *walk_subtrees = 0;
3558 return NULL_TREE;
3559
3560 case IDENTIFIER_NODE:
3561 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3562 ppd->visited);
3563 *walk_subtrees = 0;
3564 return NULL_TREE;
3565
3566 default:
3567 return NULL_TREE;
3568 }
3569
3570 return NULL_TREE;
3571 }
3572
3573 /* Determines if the expression or type T uses any parameter packs. */
3574 bool
3575 uses_parameter_packs (tree t)
3576 {
3577 tree parameter_packs = NULL_TREE;
3578 struct find_parameter_pack_data ppd;
3579 ppd.parameter_packs = &parameter_packs;
3580 ppd.visited = new hash_set<tree>;
3581 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3582 delete ppd.visited;
3583 return parameter_packs != NULL_TREE;
3584 }
3585
3586 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3587 representation a base-class initializer into a parameter pack
3588 expansion. If all goes well, the resulting node will be an
3589 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3590 respectively. */
3591 tree
3592 make_pack_expansion (tree arg)
3593 {
3594 tree result;
3595 tree parameter_packs = NULL_TREE;
3596 bool for_types = false;
3597 struct find_parameter_pack_data ppd;
3598
3599 if (!arg || arg == error_mark_node)
3600 return arg;
3601
3602 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3603 {
3604 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3605 class initializer. In this case, the TREE_PURPOSE will be a
3606 _TYPE node (representing the base class expansion we're
3607 initializing) and the TREE_VALUE will be a TREE_LIST
3608 containing the initialization arguments.
3609
3610 The resulting expansion looks somewhat different from most
3611 expansions. Rather than returning just one _EXPANSION, we
3612 return a TREE_LIST whose TREE_PURPOSE is a
3613 TYPE_PACK_EXPANSION containing the bases that will be
3614 initialized. The TREE_VALUE will be identical to the
3615 original TREE_VALUE, which is a list of arguments that will
3616 be passed to each base. We do not introduce any new pack
3617 expansion nodes into the TREE_VALUE (although it is possible
3618 that some already exist), because the TREE_PURPOSE and
3619 TREE_VALUE all need to be expanded together with the same
3620 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3621 resulting TREE_PURPOSE will mention the parameter packs in
3622 both the bases and the arguments to the bases. */
3623 tree purpose;
3624 tree value;
3625 tree parameter_packs = NULL_TREE;
3626
3627 /* Determine which parameter packs will be used by the base
3628 class expansion. */
3629 ppd.visited = new hash_set<tree>;
3630 ppd.parameter_packs = &parameter_packs;
3631 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3632 &ppd, ppd.visited);
3633
3634 if (parameter_packs == NULL_TREE)
3635 {
3636 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3637 delete ppd.visited;
3638 return error_mark_node;
3639 }
3640
3641 if (TREE_VALUE (arg) != void_type_node)
3642 {
3643 /* Collect the sets of parameter packs used in each of the
3644 initialization arguments. */
3645 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3646 {
3647 /* Determine which parameter packs will be expanded in this
3648 argument. */
3649 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3650 &ppd, ppd.visited);
3651 }
3652 }
3653
3654 delete ppd.visited;
3655
3656 /* Create the pack expansion type for the base type. */
3657 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3658 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3659 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3660
3661 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3662 they will rarely be compared to anything. */
3663 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3664
3665 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3666 }
3667
3668 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3669 for_types = true;
3670
3671 /* Build the PACK_EXPANSION_* node. */
3672 result = for_types
3673 ? cxx_make_type (TYPE_PACK_EXPANSION)
3674 : make_node (EXPR_PACK_EXPANSION);
3675 SET_PACK_EXPANSION_PATTERN (result, arg);
3676 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3677 {
3678 /* Propagate type and const-expression information. */
3679 TREE_TYPE (result) = TREE_TYPE (arg);
3680 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3681 }
3682 else
3683 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3684 they will rarely be compared to anything. */
3685 SET_TYPE_STRUCTURAL_EQUALITY (result);
3686
3687 /* Determine which parameter packs will be expanded. */
3688 ppd.parameter_packs = &parameter_packs;
3689 ppd.visited = new hash_set<tree>;
3690 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3691 delete ppd.visited;
3692
3693 /* Make sure we found some parameter packs. */
3694 if (parameter_packs == NULL_TREE)
3695 {
3696 if (TYPE_P (arg))
3697 error ("expansion pattern %<%T%> contains no argument packs", arg);
3698 else
3699 error ("expansion pattern %<%E%> contains no argument packs", arg);
3700 return error_mark_node;
3701 }
3702 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3703
3704 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3705
3706 return result;
3707 }
3708
3709 /* Checks T for any "bare" parameter packs, which have not yet been
3710 expanded, and issues an error if any are found. This operation can
3711 only be done on full expressions or types (e.g., an expression
3712 statement, "if" condition, etc.), because we could have expressions like:
3713
3714 foo(f(g(h(args)))...)
3715
3716 where "args" is a parameter pack. check_for_bare_parameter_packs
3717 should not be called for the subexpressions args, h(args),
3718 g(h(args)), or f(g(h(args))), because we would produce erroneous
3719 error messages.
3720
3721 Returns TRUE and emits an error if there were bare parameter packs,
3722 returns FALSE otherwise. */
3723 bool
3724 check_for_bare_parameter_packs (tree t)
3725 {
3726 tree parameter_packs = NULL_TREE;
3727 struct find_parameter_pack_data ppd;
3728
3729 if (!processing_template_decl || !t || t == error_mark_node)
3730 return false;
3731
3732 if (TREE_CODE (t) == TYPE_DECL)
3733 t = TREE_TYPE (t);
3734
3735 ppd.parameter_packs = &parameter_packs;
3736 ppd.visited = new hash_set<tree>;
3737 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3738 delete ppd.visited;
3739
3740 if (parameter_packs)
3741 {
3742 error ("parameter packs not expanded with %<...%>:");
3743 while (parameter_packs)
3744 {
3745 tree pack = TREE_VALUE (parameter_packs);
3746 tree name = NULL_TREE;
3747
3748 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3749 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3750 name = TYPE_NAME (pack);
3751 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3752 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3753 else
3754 name = DECL_NAME (pack);
3755
3756 if (name)
3757 inform (input_location, " %qD", name);
3758 else
3759 inform (input_location, " <anonymous>");
3760
3761 parameter_packs = TREE_CHAIN (parameter_packs);
3762 }
3763
3764 return true;
3765 }
3766
3767 return false;
3768 }
3769
3770 /* Expand any parameter packs that occur in the template arguments in
3771 ARGS. */
3772 tree
3773 expand_template_argument_pack (tree args)
3774 {
3775 tree result_args = NULL_TREE;
3776 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3777 int num_result_args = -1;
3778 int non_default_args_count = -1;
3779
3780 /* First, determine if we need to expand anything, and the number of
3781 slots we'll need. */
3782 for (in_arg = 0; in_arg < nargs; ++in_arg)
3783 {
3784 tree arg = TREE_VEC_ELT (args, in_arg);
3785 if (arg == NULL_TREE)
3786 return args;
3787 if (ARGUMENT_PACK_P (arg))
3788 {
3789 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3790 if (num_result_args < 0)
3791 num_result_args = in_arg + num_packed;
3792 else
3793 num_result_args += num_packed;
3794 }
3795 else
3796 {
3797 if (num_result_args >= 0)
3798 num_result_args++;
3799 }
3800 }
3801
3802 /* If no expansion is necessary, we're done. */
3803 if (num_result_args < 0)
3804 return args;
3805
3806 /* Expand arguments. */
3807 result_args = make_tree_vec (num_result_args);
3808 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3809 non_default_args_count =
3810 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3811 for (in_arg = 0; in_arg < nargs; ++in_arg)
3812 {
3813 tree arg = TREE_VEC_ELT (args, in_arg);
3814 if (ARGUMENT_PACK_P (arg))
3815 {
3816 tree packed = ARGUMENT_PACK_ARGS (arg);
3817 int i, num_packed = TREE_VEC_LENGTH (packed);
3818 for (i = 0; i < num_packed; ++i, ++out_arg)
3819 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3820 if (non_default_args_count > 0)
3821 non_default_args_count += num_packed - 1;
3822 }
3823 else
3824 {
3825 TREE_VEC_ELT (result_args, out_arg) = arg;
3826 ++out_arg;
3827 }
3828 }
3829 if (non_default_args_count >= 0)
3830 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3831 return result_args;
3832 }
3833
3834 /* Checks if DECL shadows a template parameter.
3835
3836 [temp.local]: A template-parameter shall not be redeclared within its
3837 scope (including nested scopes).
3838
3839 Emits an error and returns TRUE if the DECL shadows a parameter,
3840 returns FALSE otherwise. */
3841
3842 bool
3843 check_template_shadow (tree decl)
3844 {
3845 tree olddecl;
3846
3847 /* If we're not in a template, we can't possibly shadow a template
3848 parameter. */
3849 if (!current_template_parms)
3850 return true;
3851
3852 /* Figure out what we're shadowing. */
3853 if (TREE_CODE (decl) == OVERLOAD)
3854 decl = OVL_CURRENT (decl);
3855 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3856
3857 /* If there's no previous binding for this name, we're not shadowing
3858 anything, let alone a template parameter. */
3859 if (!olddecl)
3860 return true;
3861
3862 /* If we're not shadowing a template parameter, we're done. Note
3863 that OLDDECL might be an OVERLOAD (or perhaps even an
3864 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3865 node. */
3866 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3867 return true;
3868
3869 /* We check for decl != olddecl to avoid bogus errors for using a
3870 name inside a class. We check TPFI to avoid duplicate errors for
3871 inline member templates. */
3872 if (decl == olddecl
3873 || (DECL_TEMPLATE_PARM_P (decl)
3874 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3875 return true;
3876
3877 /* Don't complain about the injected class name, as we've already
3878 complained about the class itself. */
3879 if (DECL_SELF_REFERENCE_P (decl))
3880 return false;
3881
3882 if (DECL_TEMPLATE_PARM_P (decl))
3883 error ("declaration of template parameter %q+D shadows "
3884 "template parameter", decl);
3885 else
3886 error ("declaration of %q+#D shadows template parameter", decl);
3887 inform (DECL_SOURCE_LOCATION (olddecl),
3888 "template parameter %qD declared here", olddecl);
3889 return false;
3890 }
3891
3892 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3893 ORIG_LEVEL, DECL, and TYPE. */
3894
3895 static tree
3896 build_template_parm_index (int index,
3897 int level,
3898 int orig_level,
3899 tree decl,
3900 tree type)
3901 {
3902 tree t = make_node (TEMPLATE_PARM_INDEX);
3903 TEMPLATE_PARM_IDX (t) = index;
3904 TEMPLATE_PARM_LEVEL (t) = level;
3905 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3906 TEMPLATE_PARM_DECL (t) = decl;
3907 TREE_TYPE (t) = type;
3908 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3909 TREE_READONLY (t) = TREE_READONLY (decl);
3910
3911 return t;
3912 }
3913
3914 /* Find the canonical type parameter for the given template type
3915 parameter. Returns the canonical type parameter, which may be TYPE
3916 if no such parameter existed. */
3917
3918 static tree
3919 canonical_type_parameter (tree type)
3920 {
3921 tree list;
3922 int idx = TEMPLATE_TYPE_IDX (type);
3923 if (!canonical_template_parms)
3924 vec_alloc (canonical_template_parms, idx+1);
3925
3926 while (canonical_template_parms->length () <= (unsigned)idx)
3927 vec_safe_push (canonical_template_parms, NULL_TREE);
3928
3929 list = (*canonical_template_parms)[idx];
3930 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3931 list = TREE_CHAIN (list);
3932
3933 if (list)
3934 return TREE_VALUE (list);
3935 else
3936 {
3937 (*canonical_template_parms)[idx]
3938 = tree_cons (NULL_TREE, type,
3939 (*canonical_template_parms)[idx]);
3940 return type;
3941 }
3942 }
3943
3944 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3945 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3946 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3947 new one is created. */
3948
3949 static tree
3950 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3951 tsubst_flags_t complain)
3952 {
3953 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3954 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3955 != TEMPLATE_PARM_LEVEL (index) - levels)
3956 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3957 {
3958 tree orig_decl = TEMPLATE_PARM_DECL (index);
3959 tree decl, t;
3960
3961 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3962 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3963 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3964 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3965 DECL_ARTIFICIAL (decl) = 1;
3966 SET_DECL_TEMPLATE_PARM_P (decl);
3967
3968 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3969 TEMPLATE_PARM_LEVEL (index) - levels,
3970 TEMPLATE_PARM_ORIG_LEVEL (index),
3971 decl, type);
3972 TEMPLATE_PARM_DESCENDANTS (index) = t;
3973 TEMPLATE_PARM_PARAMETER_PACK (t)
3974 = TEMPLATE_PARM_PARAMETER_PACK (index);
3975
3976 /* Template template parameters need this. */
3977 if (TREE_CODE (decl) == TEMPLATE_DECL)
3978 {
3979 DECL_TEMPLATE_RESULT (decl)
3980 = build_decl (DECL_SOURCE_LOCATION (decl),
3981 TYPE_DECL, DECL_NAME (decl), type);
3982 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
3983 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3984 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
3985 }
3986 }
3987
3988 return TEMPLATE_PARM_DESCENDANTS (index);
3989 }
3990
3991 /* Process information from new template parameter PARM and append it
3992 to the LIST being built. This new parameter is a non-type
3993 parameter iff IS_NON_TYPE is true. This new parameter is a
3994 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3995 is in PARM_LOC. */
3996
3997 tree
3998 process_template_parm (tree list, location_t parm_loc, tree parm,
3999 bool is_non_type, bool is_parameter_pack)
4000 {
4001 tree decl = 0;
4002 int idx = 0;
4003
4004 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4005 tree defval = TREE_PURPOSE (parm);
4006 tree constr = TREE_TYPE (parm);
4007
4008 if (list)
4009 {
4010 tree p = tree_last (list);
4011
4012 if (p && TREE_VALUE (p) != error_mark_node)
4013 {
4014 p = TREE_VALUE (p);
4015 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4016 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4017 else
4018 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4019 }
4020
4021 ++idx;
4022 }
4023
4024 if (is_non_type)
4025 {
4026 parm = TREE_VALUE (parm);
4027
4028 SET_DECL_TEMPLATE_PARM_P (parm);
4029
4030 if (TREE_TYPE (parm) != error_mark_node)
4031 {
4032 /* [temp.param]
4033
4034 The top-level cv-qualifiers on the template-parameter are
4035 ignored when determining its type. */
4036 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4037 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4038 TREE_TYPE (parm) = error_mark_node;
4039 else if (uses_parameter_packs (TREE_TYPE (parm))
4040 && !is_parameter_pack
4041 /* If we're in a nested template parameter list, the template
4042 template parameter could be a parameter pack. */
4043 && processing_template_parmlist == 1)
4044 {
4045 /* This template parameter is not a parameter pack, but it
4046 should be. Complain about "bare" parameter packs. */
4047 check_for_bare_parameter_packs (TREE_TYPE (parm));
4048
4049 /* Recover by calling this a parameter pack. */
4050 is_parameter_pack = true;
4051 }
4052 }
4053
4054 /* A template parameter is not modifiable. */
4055 TREE_CONSTANT (parm) = 1;
4056 TREE_READONLY (parm) = 1;
4057 decl = build_decl (parm_loc,
4058 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4059 TREE_CONSTANT (decl) = 1;
4060 TREE_READONLY (decl) = 1;
4061 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4062 = build_template_parm_index (idx, processing_template_decl,
4063 processing_template_decl,
4064 decl, TREE_TYPE (parm));
4065
4066 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4067 = is_parameter_pack;
4068 }
4069 else
4070 {
4071 tree t;
4072 parm = TREE_VALUE (TREE_VALUE (parm));
4073
4074 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4075 {
4076 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4077 /* This is for distinguishing between real templates and template
4078 template parameters */
4079 TREE_TYPE (parm) = t;
4080 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4081 decl = parm;
4082 }
4083 else
4084 {
4085 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4086 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4087 decl = build_decl (parm_loc,
4088 TYPE_DECL, parm, t);
4089 }
4090
4091 TYPE_NAME (t) = decl;
4092 TYPE_STUB_DECL (t) = decl;
4093 parm = decl;
4094 TEMPLATE_TYPE_PARM_INDEX (t)
4095 = build_template_parm_index (idx, processing_template_decl,
4096 processing_template_decl,
4097 decl, TREE_TYPE (parm));
4098 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4099 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4100 }
4101 DECL_ARTIFICIAL (decl) = 1;
4102 SET_DECL_TEMPLATE_PARM_P (decl);
4103
4104 /* Build requirements for the type/template parameter.
4105 This must be done after SET_DECL_TEMPLATE_PARM_P or
4106 process_template_parm could fail. */
4107 tree reqs = finish_shorthand_constraint (parm, constr);
4108
4109 pushdecl (decl);
4110
4111 /* Build the parameter node linking the parameter declaration,
4112 its default argument (if any), and its constraints (if any). */
4113 parm = build_tree_list (defval, parm);
4114 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4115
4116 return chainon (list, parm);
4117 }
4118
4119 /* The end of a template parameter list has been reached. Process the
4120 tree list into a parameter vector, converting each parameter into a more
4121 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4122 as PARM_DECLs. */
4123
4124 tree
4125 end_template_parm_list (tree parms)
4126 {
4127 int nparms;
4128 tree parm, next;
4129 tree saved_parmlist = make_tree_vec (list_length (parms));
4130
4131 /* Pop the dummy parameter level and add the real one. */
4132 current_template_parms = TREE_CHAIN (current_template_parms);
4133
4134 current_template_parms
4135 = tree_cons (size_int (processing_template_decl),
4136 saved_parmlist, current_template_parms);
4137
4138 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4139 {
4140 next = TREE_CHAIN (parm);
4141 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4142 TREE_CHAIN (parm) = NULL_TREE;
4143 }
4144
4145 --processing_template_parmlist;
4146
4147 return saved_parmlist;
4148 }
4149
4150 // Explicitly indicate the end of the template parameter list. We assume
4151 // that the current template parameters have been constructed and/or
4152 // managed explicitly, as when creating new template template parameters
4153 // from a shorthand constraint.
4154 void
4155 end_template_parm_list ()
4156 {
4157 --processing_template_parmlist;
4158 }
4159
4160 /* end_template_decl is called after a template declaration is seen. */
4161
4162 void
4163 end_template_decl (void)
4164 {
4165 reset_specialization ();
4166
4167 if (! processing_template_decl)
4168 return;
4169
4170 /* This matches the pushlevel in begin_template_parm_list. */
4171 finish_scope ();
4172
4173 --processing_template_decl;
4174 current_template_parms = TREE_CHAIN (current_template_parms);
4175 }
4176
4177 /* Takes a TREE_LIST representing a template parameter and convert it
4178 into an argument suitable to be passed to the type substitution
4179 functions. Note that If the TREE_LIST contains an error_mark
4180 node, the returned argument is error_mark_node. */
4181
4182 tree
4183 template_parm_to_arg (tree t)
4184 {
4185
4186 if (t == NULL_TREE
4187 || TREE_CODE (t) != TREE_LIST)
4188 return t;
4189
4190 if (error_operand_p (TREE_VALUE (t)))
4191 return error_mark_node;
4192
4193 t = TREE_VALUE (t);
4194
4195 if (TREE_CODE (t) == TYPE_DECL
4196 || TREE_CODE (t) == TEMPLATE_DECL)
4197 {
4198 t = TREE_TYPE (t);
4199
4200 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4201 {
4202 /* Turn this argument into a TYPE_ARGUMENT_PACK
4203 with a single element, which expands T. */
4204 tree vec = make_tree_vec (1);
4205 #ifdef ENABLE_CHECKING
4206 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4207 (vec, TREE_VEC_LENGTH (vec));
4208 #endif
4209 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4210
4211 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4212 SET_ARGUMENT_PACK_ARGS (t, vec);
4213 }
4214 }
4215 else
4216 {
4217 t = DECL_INITIAL (t);
4218
4219 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4220 {
4221 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4222 with a single element, which expands T. */
4223 tree vec = make_tree_vec (1);
4224 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4225 #ifdef ENABLE_CHECKING
4226 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4227 (vec, TREE_VEC_LENGTH (vec));
4228 #endif
4229 t = convert_from_reference (t);
4230 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4231
4232 t = make_node (NONTYPE_ARGUMENT_PACK);
4233 SET_ARGUMENT_PACK_ARGS (t, vec);
4234 TREE_TYPE (t) = type;
4235 }
4236 else
4237 t = convert_from_reference (t);
4238 }
4239 return t;
4240 }
4241
4242 /* Given a set of template parameters, return them as a set of template
4243 arguments. The template parameters are represented as a TREE_VEC, in
4244 the form documented in cp-tree.h for template arguments. */
4245
4246 static tree
4247 template_parms_to_args (tree parms)
4248 {
4249 tree header;
4250 tree args = NULL_TREE;
4251 int length = TMPL_PARMS_DEPTH (parms);
4252 int l = length;
4253
4254 /* If there is only one level of template parameters, we do not
4255 create a TREE_VEC of TREE_VECs. Instead, we return a single
4256 TREE_VEC containing the arguments. */
4257 if (length > 1)
4258 args = make_tree_vec (length);
4259
4260 for (header = parms; header; header = TREE_CHAIN (header))
4261 {
4262 tree a = copy_node (TREE_VALUE (header));
4263 int i;
4264
4265 TREE_TYPE (a) = NULL_TREE;
4266 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4267 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4268
4269 #ifdef ENABLE_CHECKING
4270 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4271 #endif
4272
4273 if (length > 1)
4274 TREE_VEC_ELT (args, --l) = a;
4275 else
4276 args = a;
4277 }
4278
4279 return args;
4280 }
4281
4282 /* Within the declaration of a template, return the currently active
4283 template parameters as an argument TREE_VEC. */
4284
4285 static tree
4286 current_template_args (void)
4287 {
4288 return template_parms_to_args (current_template_parms);
4289 }
4290
4291 /* Update the declared TYPE by doing any lookups which were thought to be
4292 dependent, but are not now that we know the SCOPE of the declarator. */
4293
4294 tree
4295 maybe_update_decl_type (tree orig_type, tree scope)
4296 {
4297 tree type = orig_type;
4298
4299 if (type == NULL_TREE)
4300 return type;
4301
4302 if (TREE_CODE (orig_type) == TYPE_DECL)
4303 type = TREE_TYPE (type);
4304
4305 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4306 && dependent_type_p (type)
4307 /* Don't bother building up the args in this case. */
4308 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4309 {
4310 /* tsubst in the args corresponding to the template parameters,
4311 including auto if present. Most things will be unchanged, but
4312 make_typename_type and tsubst_qualified_id will resolve
4313 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4314 tree args = current_template_args ();
4315 tree auto_node = type_uses_auto (type);
4316 tree pushed;
4317 if (auto_node)
4318 {
4319 tree auto_vec = make_tree_vec (1);
4320 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4321 args = add_to_template_args (args, auto_vec);
4322 }
4323 pushed = push_scope (scope);
4324 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4325 if (pushed)
4326 pop_scope (scope);
4327 }
4328
4329 if (type == error_mark_node)
4330 return orig_type;
4331
4332 if (TREE_CODE (orig_type) == TYPE_DECL)
4333 {
4334 if (same_type_p (type, TREE_TYPE (orig_type)))
4335 type = orig_type;
4336 else
4337 type = TYPE_NAME (type);
4338 }
4339 return type;
4340 }
4341
4342 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4343 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4344 the new template is a member template. */
4345
4346 tree
4347 build_template_decl (tree decl, tree parms, bool member_template_p)
4348 {
4349 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4350 DECL_TEMPLATE_PARMS (tmpl) = parms;
4351 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4352 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4353 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4354
4355 return tmpl;
4356 }
4357
4358 struct template_parm_data
4359 {
4360 /* The level of the template parameters we are currently
4361 processing. */
4362 int level;
4363
4364 /* The index of the specialization argument we are currently
4365 processing. */
4366 int current_arg;
4367
4368 /* An array whose size is the number of template parameters. The
4369 elements are nonzero if the parameter has been used in any one
4370 of the arguments processed so far. */
4371 int* parms;
4372
4373 /* An array whose size is the number of template arguments. The
4374 elements are nonzero if the argument makes use of template
4375 parameters of this level. */
4376 int* arg_uses_template_parms;
4377 };
4378
4379 /* Subroutine of push_template_decl used to see if each template
4380 parameter in a partial specialization is used in the explicit
4381 argument list. If T is of the LEVEL given in DATA (which is
4382 treated as a template_parm_data*), then DATA->PARMS is marked
4383 appropriately. */
4384
4385 static int
4386 mark_template_parm (tree t, void* data)
4387 {
4388 int level;
4389 int idx;
4390 struct template_parm_data* tpd = (struct template_parm_data*) data;
4391
4392 template_parm_level_and_index (t, &level, &idx);
4393
4394 if (level == tpd->level)
4395 {
4396 tpd->parms[idx] = 1;
4397 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4398 }
4399
4400 /* Return zero so that for_each_template_parm will continue the
4401 traversal of the tree; we want to mark *every* template parm. */
4402 return 0;
4403 }
4404
4405 /* Process the partial specialization DECL. */
4406
4407 static tree
4408 process_partial_specialization (tree decl)
4409 {
4410 tree type = TREE_TYPE (decl);
4411 tree tinfo = get_template_info (decl);
4412 tree maintmpl = TI_TEMPLATE (tinfo);
4413 tree specargs = TI_ARGS (tinfo);
4414 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4415 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4416 tree inner_parms;
4417 tree inst;
4418 int nargs = TREE_VEC_LENGTH (inner_args);
4419 int ntparms;
4420 int i;
4421 bool did_error_intro = false;
4422 struct template_parm_data tpd;
4423 struct template_parm_data tpd2;
4424
4425 gcc_assert (current_template_parms);
4426
4427 /* A concept cannot be specialized. */
4428 if (flag_concepts && variable_concept_p (maintmpl))
4429 {
4430 error ("specialization of variable concept %q#D", maintmpl);
4431 return error_mark_node;
4432 }
4433
4434 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4435 ntparms = TREE_VEC_LENGTH (inner_parms);
4436
4437 /* We check that each of the template parameters given in the
4438 partial specialization is used in the argument list to the
4439 specialization. For example:
4440
4441 template <class T> struct S;
4442 template <class T> struct S<T*>;
4443
4444 The second declaration is OK because `T*' uses the template
4445 parameter T, whereas
4446
4447 template <class T> struct S<int>;
4448
4449 is no good. Even trickier is:
4450
4451 template <class T>
4452 struct S1
4453 {
4454 template <class U>
4455 struct S2;
4456 template <class U>
4457 struct S2<T>;
4458 };
4459
4460 The S2<T> declaration is actually invalid; it is a
4461 full-specialization. Of course,
4462
4463 template <class U>
4464 struct S2<T (*)(U)>;
4465
4466 or some such would have been OK. */
4467 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4468 tpd.parms = XALLOCAVEC (int, ntparms);
4469 memset (tpd.parms, 0, sizeof (int) * ntparms);
4470
4471 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4472 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4473 for (i = 0; i < nargs; ++i)
4474 {
4475 tpd.current_arg = i;
4476 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4477 &mark_template_parm,
4478 &tpd,
4479 NULL,
4480 /*include_nondeduced_p=*/false);
4481 }
4482 for (i = 0; i < ntparms; ++i)
4483 if (tpd.parms[i] == 0)
4484 {
4485 /* One of the template parms was not used in a deduced context in the
4486 specialization. */
4487 if (!did_error_intro)
4488 {
4489 error ("template parameters not deducible in "
4490 "partial specialization:");
4491 did_error_intro = true;
4492 }
4493
4494 inform (input_location, " %qD",
4495 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4496 }
4497
4498 if (did_error_intro)
4499 return error_mark_node;
4500
4501 /* [temp.class.spec]
4502
4503 The argument list of the specialization shall not be identical to
4504 the implicit argument list of the primary template. */
4505 tree main_args
4506 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4507 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4508 && (!flag_concepts
4509 || !subsumes_constraints (current_template_constraints (),
4510 get_constraints (maintmpl))))
4511 {
4512 if (!flag_concepts)
4513 error ("partial specialization %q+D does not specialize "
4514 "any template arguments", decl);
4515 else
4516 error ("partial specialization %q+D does not specialize any "
4517 "template arguments and is not more constrained than", decl);
4518 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4519 }
4520
4521 /* A partial specialization that replaces multiple parameters of the
4522 primary template with a pack expansion is less specialized for those
4523 parameters. */
4524 if (nargs < DECL_NTPARMS (maintmpl))
4525 {
4526 error ("partial specialization is not more specialized than the "
4527 "primary template because it replaces multiple parameters "
4528 "with a pack expansion");
4529 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4530 return decl;
4531 }
4532
4533 /* [temp.class.spec]
4534
4535 A partially specialized non-type argument expression shall not
4536 involve template parameters of the partial specialization except
4537 when the argument expression is a simple identifier.
4538
4539 The type of a template parameter corresponding to a specialized
4540 non-type argument shall not be dependent on a parameter of the
4541 specialization.
4542
4543 Also, we verify that pack expansions only occur at the
4544 end of the argument list. */
4545 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4546 tpd2.parms = 0;
4547 for (i = 0; i < nargs; ++i)
4548 {
4549 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4550 tree arg = TREE_VEC_ELT (inner_args, i);
4551 tree packed_args = NULL_TREE;
4552 int j, len = 1;
4553
4554 if (ARGUMENT_PACK_P (arg))
4555 {
4556 /* Extract the arguments from the argument pack. We'll be
4557 iterating over these in the following loop. */
4558 packed_args = ARGUMENT_PACK_ARGS (arg);
4559 len = TREE_VEC_LENGTH (packed_args);
4560 }
4561
4562 for (j = 0; j < len; j++)
4563 {
4564 if (packed_args)
4565 /* Get the Jth argument in the parameter pack. */
4566 arg = TREE_VEC_ELT (packed_args, j);
4567
4568 if (PACK_EXPANSION_P (arg))
4569 {
4570 /* Pack expansions must come at the end of the
4571 argument list. */
4572 if ((packed_args && j < len - 1)
4573 || (!packed_args && i < nargs - 1))
4574 {
4575 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4576 error ("parameter pack argument %qE must be at the "
4577 "end of the template argument list", arg);
4578 else
4579 error ("parameter pack argument %qT must be at the "
4580 "end of the template argument list", arg);
4581 }
4582 }
4583
4584 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4585 /* We only care about the pattern. */
4586 arg = PACK_EXPANSION_PATTERN (arg);
4587
4588 if (/* These first two lines are the `non-type' bit. */
4589 !TYPE_P (arg)
4590 && TREE_CODE (arg) != TEMPLATE_DECL
4591 /* This next two lines are the `argument expression is not just a
4592 simple identifier' condition and also the `specialized
4593 non-type argument' bit. */
4594 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4595 && !(REFERENCE_REF_P (arg)
4596 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4597 {
4598 if ((!packed_args && tpd.arg_uses_template_parms[i])
4599 || (packed_args && uses_template_parms (arg)))
4600 error ("template argument %qE involves template parameter(s)",
4601 arg);
4602 else
4603 {
4604 /* Look at the corresponding template parameter,
4605 marking which template parameters its type depends
4606 upon. */
4607 tree type = TREE_TYPE (parm);
4608
4609 if (!tpd2.parms)
4610 {
4611 /* We haven't yet initialized TPD2. Do so now. */
4612 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4613 /* The number of parameters here is the number in the
4614 main template, which, as checked in the assertion
4615 above, is NARGS. */
4616 tpd2.parms = XALLOCAVEC (int, nargs);
4617 tpd2.level =
4618 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4619 }
4620
4621 /* Mark the template parameters. But this time, we're
4622 looking for the template parameters of the main
4623 template, not in the specialization. */
4624 tpd2.current_arg = i;
4625 tpd2.arg_uses_template_parms[i] = 0;
4626 memset (tpd2.parms, 0, sizeof (int) * nargs);
4627 for_each_template_parm (type,
4628 &mark_template_parm,
4629 &tpd2,
4630 NULL,
4631 /*include_nondeduced_p=*/false);
4632
4633 if (tpd2.arg_uses_template_parms [i])
4634 {
4635 /* The type depended on some template parameters.
4636 If they are fully specialized in the
4637 specialization, that's OK. */
4638 int j;
4639 int count = 0;
4640 for (j = 0; j < nargs; ++j)
4641 if (tpd2.parms[j] != 0
4642 && tpd.arg_uses_template_parms [j])
4643 ++count;
4644 if (count != 0)
4645 error_n (input_location, count,
4646 "type %qT of template argument %qE depends "
4647 "on a template parameter",
4648 "type %qT of template argument %qE depends "
4649 "on template parameters",
4650 type,
4651 arg);
4652 }
4653 }
4654 }
4655 }
4656 }
4657
4658 /* We should only get here once. */
4659 if (TREE_CODE (decl) == TYPE_DECL)
4660 gcc_assert (!COMPLETE_TYPE_P (type));
4661
4662 // Build the template decl.
4663 tree tmpl = build_template_decl (decl, current_template_parms,
4664 DECL_MEMBER_TEMPLATE_P (maintmpl));
4665 TREE_TYPE (tmpl) = type;
4666 DECL_TEMPLATE_RESULT (tmpl) = decl;
4667 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4668 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4669 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4670
4671 if (VAR_P (decl))
4672 /* We didn't register this in check_explicit_specialization so we could
4673 wait until the constraints were set. */
4674 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4675 else
4676 associate_classtype_constraints (type);
4677
4678 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4679 = tree_cons (specargs, tmpl,
4680 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4681 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4682
4683 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4684 inst = TREE_CHAIN (inst))
4685 {
4686 tree instance = TREE_VALUE (inst);
4687 if (TYPE_P (instance)
4688 ? (COMPLETE_TYPE_P (instance)
4689 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4690 : DECL_TEMPLATE_INSTANTIATION (instance))
4691 {
4692 tree spec = most_specialized_partial_spec (instance, tf_none);
4693 if (spec && TREE_VALUE (spec) == tmpl)
4694 {
4695 tree inst_decl = (DECL_P (instance)
4696 ? instance : TYPE_NAME (instance));
4697 permerror (input_location,
4698 "partial specialization of %qD after instantiation "
4699 "of %qD", decl, inst_decl);
4700 }
4701 }
4702 }
4703
4704 return decl;
4705 }
4706
4707 /* PARM is a template parameter of some form; return the corresponding
4708 TEMPLATE_PARM_INDEX. */
4709
4710 static tree
4711 get_template_parm_index (tree parm)
4712 {
4713 if (TREE_CODE (parm) == PARM_DECL
4714 || TREE_CODE (parm) == CONST_DECL)
4715 parm = DECL_INITIAL (parm);
4716 else if (TREE_CODE (parm) == TYPE_DECL
4717 || TREE_CODE (parm) == TEMPLATE_DECL)
4718 parm = TREE_TYPE (parm);
4719 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4720 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4721 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4722 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4723 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4724 return parm;
4725 }
4726
4727 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4728 parameter packs used by the template parameter PARM. */
4729
4730 static void
4731 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4732 {
4733 /* A type parm can't refer to another parm. */
4734 if (TREE_CODE (parm) == TYPE_DECL)
4735 return;
4736 else if (TREE_CODE (parm) == PARM_DECL)
4737 {
4738 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4739 ppd, ppd->visited);
4740 return;
4741 }
4742
4743 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4744
4745 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4746 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4747 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4748 }
4749
4750 /* PARM is a template parameter pack. Return any parameter packs used in
4751 its type or the type of any of its template parameters. If there are
4752 any such packs, it will be instantiated into a fixed template parameter
4753 list by partial instantiation rather than be fully deduced. */
4754
4755 tree
4756 fixed_parameter_pack_p (tree parm)
4757 {
4758 /* This can only be true in a member template. */
4759 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4760 return NULL_TREE;
4761 /* This can only be true for a parameter pack. */
4762 if (!template_parameter_pack_p (parm))
4763 return NULL_TREE;
4764 /* A type parm can't refer to another parm. */
4765 if (TREE_CODE (parm) == TYPE_DECL)
4766 return NULL_TREE;
4767
4768 tree parameter_packs = NULL_TREE;
4769 struct find_parameter_pack_data ppd;
4770 ppd.parameter_packs = &parameter_packs;
4771 ppd.visited = new hash_set<tree>;
4772
4773 fixed_parameter_pack_p_1 (parm, &ppd);
4774
4775 delete ppd.visited;
4776 return parameter_packs;
4777 }
4778
4779 /* Check that a template declaration's use of default arguments and
4780 parameter packs is not invalid. Here, PARMS are the template
4781 parameters. IS_PRIMARY is true if DECL is the thing declared by
4782 a primary template. IS_PARTIAL is true if DECL is a partial
4783 specialization.
4784
4785 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4786 declaration (but not a definition); 1 indicates a declaration, 2
4787 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4788 emitted for extraneous default arguments.
4789
4790 Returns TRUE if there were no errors found, FALSE otherwise. */
4791
4792 bool
4793 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4794 bool is_partial, int is_friend_decl)
4795 {
4796 const char *msg;
4797 int last_level_to_check;
4798 tree parm_level;
4799 bool no_errors = true;
4800
4801 /* [temp.param]
4802
4803 A default template-argument shall not be specified in a
4804 function template declaration or a function template definition, nor
4805 in the template-parameter-list of the definition of a member of a
4806 class template. */
4807
4808 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4809 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4810 /* You can't have a function template declaration in a local
4811 scope, nor you can you define a member of a class template in a
4812 local scope. */
4813 return true;
4814
4815 if ((TREE_CODE (decl) == TYPE_DECL
4816 && TREE_TYPE (decl)
4817 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4818 || (TREE_CODE (decl) == FUNCTION_DECL
4819 && LAMBDA_FUNCTION_P (decl)))
4820 /* A lambda doesn't have an explicit declaration; don't complain
4821 about the parms of the enclosing class. */
4822 return true;
4823
4824 if (current_class_type
4825 && !TYPE_BEING_DEFINED (current_class_type)
4826 && DECL_LANG_SPECIFIC (decl)
4827 && DECL_DECLARES_FUNCTION_P (decl)
4828 /* If this is either a friend defined in the scope of the class
4829 or a member function. */
4830 && (DECL_FUNCTION_MEMBER_P (decl)
4831 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4832 : DECL_FRIEND_CONTEXT (decl)
4833 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4834 : false)
4835 /* And, if it was a member function, it really was defined in
4836 the scope of the class. */
4837 && (!DECL_FUNCTION_MEMBER_P (decl)
4838 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4839 /* We already checked these parameters when the template was
4840 declared, so there's no need to do it again now. This function
4841 was defined in class scope, but we're processing its body now
4842 that the class is complete. */
4843 return true;
4844
4845 /* Core issue 226 (C++0x only): the following only applies to class
4846 templates. */
4847 if (is_primary
4848 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4849 {
4850 /* [temp.param]
4851
4852 If a template-parameter has a default template-argument, all
4853 subsequent template-parameters shall have a default
4854 template-argument supplied. */
4855 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4856 {
4857 tree inner_parms = TREE_VALUE (parm_level);
4858 int ntparms = TREE_VEC_LENGTH (inner_parms);
4859 int seen_def_arg_p = 0;
4860 int i;
4861
4862 for (i = 0; i < ntparms; ++i)
4863 {
4864 tree parm = TREE_VEC_ELT (inner_parms, i);
4865
4866 if (parm == error_mark_node)
4867 continue;
4868
4869 if (TREE_PURPOSE (parm))
4870 seen_def_arg_p = 1;
4871 else if (seen_def_arg_p
4872 && !template_parameter_pack_p (TREE_VALUE (parm)))
4873 {
4874 error ("no default argument for %qD", TREE_VALUE (parm));
4875 /* For better subsequent error-recovery, we indicate that
4876 there should have been a default argument. */
4877 TREE_PURPOSE (parm) = error_mark_node;
4878 no_errors = false;
4879 }
4880 else if (!is_partial
4881 && !is_friend_decl
4882 /* Don't complain about an enclosing partial
4883 specialization. */
4884 && parm_level == parms
4885 && TREE_CODE (decl) == TYPE_DECL
4886 && i < ntparms - 1
4887 && template_parameter_pack_p (TREE_VALUE (parm))
4888 /* A fixed parameter pack will be partially
4889 instantiated into a fixed length list. */
4890 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4891 {
4892 /* A primary class template can only have one
4893 parameter pack, at the end of the template
4894 parameter list. */
4895
4896 error ("parameter pack %q+D must be at the end of the"
4897 " template parameter list", TREE_VALUE (parm));
4898
4899 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4900 = error_mark_node;
4901 no_errors = false;
4902 }
4903 }
4904 }
4905 }
4906
4907 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4908 || is_partial
4909 || !is_primary
4910 || is_friend_decl)
4911 /* For an ordinary class template, default template arguments are
4912 allowed at the innermost level, e.g.:
4913 template <class T = int>
4914 struct S {};
4915 but, in a partial specialization, they're not allowed even
4916 there, as we have in [temp.class.spec]:
4917
4918 The template parameter list of a specialization shall not
4919 contain default template argument values.
4920
4921 So, for a partial specialization, or for a function template
4922 (in C++98/C++03), we look at all of them. */
4923 ;
4924 else
4925 /* But, for a primary class template that is not a partial
4926 specialization we look at all template parameters except the
4927 innermost ones. */
4928 parms = TREE_CHAIN (parms);
4929
4930 /* Figure out what error message to issue. */
4931 if (is_friend_decl == 2)
4932 msg = G_("default template arguments may not be used in function template "
4933 "friend re-declaration");
4934 else if (is_friend_decl)
4935 msg = G_("default template arguments may not be used in function template "
4936 "friend declarations");
4937 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4938 msg = G_("default template arguments may not be used in function templates "
4939 "without -std=c++11 or -std=gnu++11");
4940 else if (is_partial)
4941 msg = G_("default template arguments may not be used in "
4942 "partial specializations");
4943 else
4944 msg = G_("default argument for template parameter for class enclosing %qD");
4945
4946 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4947 /* If we're inside a class definition, there's no need to
4948 examine the parameters to the class itself. On the one
4949 hand, they will be checked when the class is defined, and,
4950 on the other, default arguments are valid in things like:
4951 template <class T = double>
4952 struct S { template <class U> void f(U); };
4953 Here the default argument for `S' has no bearing on the
4954 declaration of `f'. */
4955 last_level_to_check = template_class_depth (current_class_type) + 1;
4956 else
4957 /* Check everything. */
4958 last_level_to_check = 0;
4959
4960 for (parm_level = parms;
4961 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4962 parm_level = TREE_CHAIN (parm_level))
4963 {
4964 tree inner_parms = TREE_VALUE (parm_level);
4965 int i;
4966 int ntparms;
4967
4968 ntparms = TREE_VEC_LENGTH (inner_parms);
4969 for (i = 0; i < ntparms; ++i)
4970 {
4971 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4972 continue;
4973
4974 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4975 {
4976 if (msg)
4977 {
4978 no_errors = false;
4979 if (is_friend_decl == 2)
4980 return no_errors;
4981
4982 error (msg, decl);
4983 msg = 0;
4984 }
4985
4986 /* Clear out the default argument so that we are not
4987 confused later. */
4988 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4989 }
4990 }
4991
4992 /* At this point, if we're still interested in issuing messages,
4993 they must apply to classes surrounding the object declared. */
4994 if (msg)
4995 msg = G_("default argument for template parameter for class "
4996 "enclosing %qD");
4997 }
4998
4999 return no_errors;
5000 }
5001
5002 /* Worker for push_template_decl_real, called via
5003 for_each_template_parm. DATA is really an int, indicating the
5004 level of the parameters we are interested in. If T is a template
5005 parameter of that level, return nonzero. */
5006
5007 static int
5008 template_parm_this_level_p (tree t, void* data)
5009 {
5010 int this_level = *(int *)data;
5011 int level;
5012
5013 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5014 level = TEMPLATE_PARM_LEVEL (t);
5015 else
5016 level = TEMPLATE_TYPE_LEVEL (t);
5017 return level == this_level;
5018 }
5019
5020 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5021 parameters given by current_template_args, or reuses a
5022 previously existing one, if appropriate. Returns the DECL, or an
5023 equivalent one, if it is replaced via a call to duplicate_decls.
5024
5025 If IS_FRIEND is true, DECL is a friend declaration. */
5026
5027 tree
5028 push_template_decl_real (tree decl, bool is_friend)
5029 {
5030 tree tmpl;
5031 tree args;
5032 tree info;
5033 tree ctx;
5034 bool is_primary;
5035 bool is_partial;
5036 int new_template_p = 0;
5037 /* True if the template is a member template, in the sense of
5038 [temp.mem]. */
5039 bool member_template_p = false;
5040
5041 if (decl == error_mark_node || !current_template_parms)
5042 return error_mark_node;
5043
5044 /* See if this is a partial specialization. */
5045 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5046 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5047 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5048 || (VAR_P (decl)
5049 && DECL_LANG_SPECIFIC (decl)
5050 && DECL_TEMPLATE_SPECIALIZATION (decl)
5051 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5052
5053 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5054 is_friend = true;
5055
5056 if (is_friend)
5057 /* For a friend, we want the context of the friend function, not
5058 the type of which it is a friend. */
5059 ctx = CP_DECL_CONTEXT (decl);
5060 else if (CP_DECL_CONTEXT (decl)
5061 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5062 /* In the case of a virtual function, we want the class in which
5063 it is defined. */
5064 ctx = CP_DECL_CONTEXT (decl);
5065 else
5066 /* Otherwise, if we're currently defining some class, the DECL
5067 is assumed to be a member of the class. */
5068 ctx = current_scope ();
5069
5070 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5071 ctx = NULL_TREE;
5072
5073 if (!DECL_CONTEXT (decl))
5074 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5075
5076 /* See if this is a primary template. */
5077 if (is_friend && ctx
5078 && uses_template_parms_level (ctx, processing_template_decl))
5079 /* A friend template that specifies a class context, i.e.
5080 template <typename T> friend void A<T>::f();
5081 is not primary. */
5082 is_primary = false;
5083 else if (TREE_CODE (decl) == TYPE_DECL
5084 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5085 is_primary = false;
5086 else
5087 is_primary = template_parm_scope_p ();
5088
5089 if (is_primary)
5090 {
5091 if (DECL_CLASS_SCOPE_P (decl))
5092 member_template_p = true;
5093 if (TREE_CODE (decl) == TYPE_DECL
5094 && anon_aggrname_p (DECL_NAME (decl)))
5095 {
5096 error ("template class without a name");
5097 return error_mark_node;
5098 }
5099 else if (TREE_CODE (decl) == FUNCTION_DECL)
5100 {
5101 if (member_template_p)
5102 {
5103 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5104 error ("member template %qD may not have virt-specifiers", decl);
5105 }
5106 if (DECL_DESTRUCTOR_P (decl))
5107 {
5108 /* [temp.mem]
5109
5110 A destructor shall not be a member template. */
5111 error ("destructor %qD declared as member template", decl);
5112 return error_mark_node;
5113 }
5114 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5115 && (!prototype_p (TREE_TYPE (decl))
5116 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5117 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5118 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5119 == void_list_node)))
5120 {
5121 /* [basic.stc.dynamic.allocation]
5122
5123 An allocation function can be a function
5124 template. ... Template allocation functions shall
5125 have two or more parameters. */
5126 error ("invalid template declaration of %qD", decl);
5127 return error_mark_node;
5128 }
5129 }
5130 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5131 && CLASS_TYPE_P (TREE_TYPE (decl)))
5132 /* OK */;
5133 else if (TREE_CODE (decl) == TYPE_DECL
5134 && TYPE_DECL_ALIAS_P (decl))
5135 /* alias-declaration */
5136 gcc_assert (!DECL_ARTIFICIAL (decl));
5137 else if (VAR_P (decl))
5138 /* C++14 variable template. */;
5139 else
5140 {
5141 error ("template declaration of %q#D", decl);
5142 return error_mark_node;
5143 }
5144 }
5145
5146 /* Check to see that the rules regarding the use of default
5147 arguments are not being violated. */
5148 check_default_tmpl_args (decl, current_template_parms,
5149 is_primary, is_partial, /*is_friend_decl=*/0);
5150
5151 /* Ensure that there are no parameter packs in the type of this
5152 declaration that have not been expanded. */
5153 if (TREE_CODE (decl) == FUNCTION_DECL)
5154 {
5155 /* Check each of the arguments individually to see if there are
5156 any bare parameter packs. */
5157 tree type = TREE_TYPE (decl);
5158 tree arg = DECL_ARGUMENTS (decl);
5159 tree argtype = TYPE_ARG_TYPES (type);
5160
5161 while (arg && argtype)
5162 {
5163 if (!DECL_PACK_P (arg)
5164 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5165 {
5166 /* This is a PARM_DECL that contains unexpanded parameter
5167 packs. We have already complained about this in the
5168 check_for_bare_parameter_packs call, so just replace
5169 these types with ERROR_MARK_NODE. */
5170 TREE_TYPE (arg) = error_mark_node;
5171 TREE_VALUE (argtype) = error_mark_node;
5172 }
5173
5174 arg = DECL_CHAIN (arg);
5175 argtype = TREE_CHAIN (argtype);
5176 }
5177
5178 /* Check for bare parameter packs in the return type and the
5179 exception specifiers. */
5180 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5181 /* Errors were already issued, set return type to int
5182 as the frontend doesn't expect error_mark_node as
5183 the return type. */
5184 TREE_TYPE (type) = integer_type_node;
5185 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5186 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5187 }
5188 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5189 && TYPE_DECL_ALIAS_P (decl))
5190 ? DECL_ORIGINAL_TYPE (decl)
5191 : TREE_TYPE (decl)))
5192 {
5193 TREE_TYPE (decl) = error_mark_node;
5194 return error_mark_node;
5195 }
5196
5197 if (is_partial)
5198 return process_partial_specialization (decl);
5199
5200 args = current_template_args ();
5201
5202 if (!ctx
5203 || TREE_CODE (ctx) == FUNCTION_DECL
5204 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5205 || (TREE_CODE (decl) == TYPE_DECL
5206 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5207 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5208 {
5209 if (DECL_LANG_SPECIFIC (decl)
5210 && DECL_TEMPLATE_INFO (decl)
5211 && DECL_TI_TEMPLATE (decl))
5212 tmpl = DECL_TI_TEMPLATE (decl);
5213 /* If DECL is a TYPE_DECL for a class-template, then there won't
5214 be DECL_LANG_SPECIFIC. The information equivalent to
5215 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5216 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5217 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5218 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5219 {
5220 /* Since a template declaration already existed for this
5221 class-type, we must be redeclaring it here. Make sure
5222 that the redeclaration is valid. */
5223 redeclare_class_template (TREE_TYPE (decl),
5224 current_template_parms,
5225 current_template_constraints ());
5226 /* We don't need to create a new TEMPLATE_DECL; just use the
5227 one we already had. */
5228 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5229 }
5230 else
5231 {
5232 tmpl = build_template_decl (decl, current_template_parms,
5233 member_template_p);
5234 new_template_p = 1;
5235
5236 if (DECL_LANG_SPECIFIC (decl)
5237 && DECL_TEMPLATE_SPECIALIZATION (decl))
5238 {
5239 /* A specialization of a member template of a template
5240 class. */
5241 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5242 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5243 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5244 }
5245 }
5246 }
5247 else
5248 {
5249 tree a, t, current, parms;
5250 int i;
5251 tree tinfo = get_template_info (decl);
5252
5253 if (!tinfo)
5254 {
5255 error ("template definition of non-template %q#D", decl);
5256 return error_mark_node;
5257 }
5258
5259 tmpl = TI_TEMPLATE (tinfo);
5260
5261 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5262 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5263 && DECL_TEMPLATE_SPECIALIZATION (decl)
5264 && DECL_MEMBER_TEMPLATE_P (tmpl))
5265 {
5266 tree new_tmpl;
5267
5268 /* The declaration is a specialization of a member
5269 template, declared outside the class. Therefore, the
5270 innermost template arguments will be NULL, so we
5271 replace them with the arguments determined by the
5272 earlier call to check_explicit_specialization. */
5273 args = DECL_TI_ARGS (decl);
5274
5275 new_tmpl
5276 = build_template_decl (decl, current_template_parms,
5277 member_template_p);
5278 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5279 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5280 DECL_TI_TEMPLATE (decl) = new_tmpl;
5281 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5282 DECL_TEMPLATE_INFO (new_tmpl)
5283 = build_template_info (tmpl, args);
5284
5285 register_specialization (new_tmpl,
5286 most_general_template (tmpl),
5287 args,
5288 is_friend, 0);
5289 return decl;
5290 }
5291
5292 /* Make sure the template headers we got make sense. */
5293
5294 parms = DECL_TEMPLATE_PARMS (tmpl);
5295 i = TMPL_PARMS_DEPTH (parms);
5296 if (TMPL_ARGS_DEPTH (args) != i)
5297 {
5298 error ("expected %d levels of template parms for %q#D, got %d",
5299 i, decl, TMPL_ARGS_DEPTH (args));
5300 DECL_INTERFACE_KNOWN (decl) = 1;
5301 return error_mark_node;
5302 }
5303 else
5304 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5305 {
5306 a = TMPL_ARGS_LEVEL (args, i);
5307 t = INNERMOST_TEMPLATE_PARMS (parms);
5308
5309 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5310 {
5311 if (current == decl)
5312 error ("got %d template parameters for %q#D",
5313 TREE_VEC_LENGTH (a), decl);
5314 else
5315 error ("got %d template parameters for %q#T",
5316 TREE_VEC_LENGTH (a), current);
5317 error (" but %d required", TREE_VEC_LENGTH (t));
5318 /* Avoid crash in import_export_decl. */
5319 DECL_INTERFACE_KNOWN (decl) = 1;
5320 return error_mark_node;
5321 }
5322
5323 if (current == decl)
5324 current = ctx;
5325 else if (current == NULL_TREE)
5326 /* Can happen in erroneous input. */
5327 break;
5328 else
5329 current = get_containing_scope (current);
5330 }
5331
5332 /* Check that the parms are used in the appropriate qualifying scopes
5333 in the declarator. */
5334 if (!comp_template_args
5335 (TI_ARGS (tinfo),
5336 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5337 {
5338 error ("\
5339 template arguments to %qD do not match original template %qD",
5340 decl, DECL_TEMPLATE_RESULT (tmpl));
5341 if (!uses_template_parms (TI_ARGS (tinfo)))
5342 inform (input_location, "use template<> for an explicit specialization");
5343 /* Avoid crash in import_export_decl. */
5344 DECL_INTERFACE_KNOWN (decl) = 1;
5345 return error_mark_node;
5346 }
5347 }
5348
5349 DECL_TEMPLATE_RESULT (tmpl) = decl;
5350 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5351
5352 /* Push template declarations for global functions and types. Note
5353 that we do not try to push a global template friend declared in a
5354 template class; such a thing may well depend on the template
5355 parameters of the class. */
5356 if (new_template_p && !ctx
5357 && !(is_friend && template_class_depth (current_class_type) > 0))
5358 {
5359 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5360 if (tmpl == error_mark_node)
5361 return error_mark_node;
5362
5363 /* Hide template friend classes that haven't been declared yet. */
5364 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5365 {
5366 DECL_ANTICIPATED (tmpl) = 1;
5367 DECL_FRIEND_P (tmpl) = 1;
5368 }
5369 }
5370
5371 if (is_primary)
5372 {
5373 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5374 int i;
5375
5376 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5377 if (DECL_CONV_FN_P (tmpl))
5378 {
5379 int depth = TMPL_PARMS_DEPTH (parms);
5380
5381 /* It is a conversion operator. See if the type converted to
5382 depends on innermost template operands. */
5383
5384 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5385 depth))
5386 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5387 }
5388
5389 /* Give template template parms a DECL_CONTEXT of the template
5390 for which they are a parameter. */
5391 parms = INNERMOST_TEMPLATE_PARMS (parms);
5392 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5393 {
5394 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5395 if (TREE_CODE (parm) == TEMPLATE_DECL)
5396 DECL_CONTEXT (parm) = tmpl;
5397 }
5398
5399 if (TREE_CODE (decl) == TYPE_DECL
5400 && TYPE_DECL_ALIAS_P (decl)
5401 && complex_alias_template_p (tmpl))
5402 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5403 }
5404
5405 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5406 back to its most general template. If TMPL is a specialization,
5407 ARGS may only have the innermost set of arguments. Add the missing
5408 argument levels if necessary. */
5409 if (DECL_TEMPLATE_INFO (tmpl))
5410 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5411
5412 info = build_template_info (tmpl, args);
5413
5414 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5415 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5416 else
5417 {
5418 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5419 retrofit_lang_decl (decl);
5420 if (DECL_LANG_SPECIFIC (decl))
5421 DECL_TEMPLATE_INFO (decl) = info;
5422 }
5423
5424 if (flag_implicit_templates
5425 && !is_friend
5426 && TREE_PUBLIC (decl)
5427 && VAR_OR_FUNCTION_DECL_P (decl))
5428 /* Set DECL_COMDAT on template instantiations; if we force
5429 them to be emitted by explicit instantiation or -frepo,
5430 mark_needed will tell cgraph to do the right thing. */
5431 DECL_COMDAT (decl) = true;
5432
5433 return DECL_TEMPLATE_RESULT (tmpl);
5434 }
5435
5436 tree
5437 push_template_decl (tree decl)
5438 {
5439 return push_template_decl_real (decl, false);
5440 }
5441
5442 /* FN is an inheriting constructor that inherits from the constructor
5443 template INHERITED; turn FN into a constructor template with a matching
5444 template header. */
5445
5446 tree
5447 add_inherited_template_parms (tree fn, tree inherited)
5448 {
5449 tree inner_parms
5450 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5451 inner_parms = copy_node (inner_parms);
5452 tree parms
5453 = tree_cons (size_int (processing_template_decl + 1),
5454 inner_parms, current_template_parms);
5455 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5456 tree args = template_parms_to_args (parms);
5457 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5458 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5459 DECL_TEMPLATE_RESULT (tmpl) = fn;
5460 DECL_ARTIFICIAL (tmpl) = true;
5461 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5462 return tmpl;
5463 }
5464
5465 /* Called when a class template TYPE is redeclared with the indicated
5466 template PARMS, e.g.:
5467
5468 template <class T> struct S;
5469 template <class T> struct S {}; */
5470
5471 bool
5472 redeclare_class_template (tree type, tree parms, tree cons)
5473 {
5474 tree tmpl;
5475 tree tmpl_parms;
5476 int i;
5477
5478 if (!TYPE_TEMPLATE_INFO (type))
5479 {
5480 error ("%qT is not a template type", type);
5481 return false;
5482 }
5483
5484 tmpl = TYPE_TI_TEMPLATE (type);
5485 if (!PRIMARY_TEMPLATE_P (tmpl))
5486 /* The type is nested in some template class. Nothing to worry
5487 about here; there are no new template parameters for the nested
5488 type. */
5489 return true;
5490
5491 if (!parms)
5492 {
5493 error ("template specifiers not specified in declaration of %qD",
5494 tmpl);
5495 return false;
5496 }
5497
5498 parms = INNERMOST_TEMPLATE_PARMS (parms);
5499 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5500
5501 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5502 {
5503 error_n (input_location, TREE_VEC_LENGTH (parms),
5504 "redeclared with %d template parameter",
5505 "redeclared with %d template parameters",
5506 TREE_VEC_LENGTH (parms));
5507 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5508 "previous declaration %qD used %d template parameter",
5509 "previous declaration %qD used %d template parameters",
5510 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5511 return false;
5512 }
5513
5514 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5515 {
5516 tree tmpl_parm;
5517 tree parm;
5518 tree tmpl_default;
5519 tree parm_default;
5520
5521 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5522 || TREE_VEC_ELT (parms, i) == error_mark_node)
5523 continue;
5524
5525 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5526 if (error_operand_p (tmpl_parm))
5527 return false;
5528
5529 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5530 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5531 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5532
5533 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5534 TEMPLATE_DECL. */
5535 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5536 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5537 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5538 || (TREE_CODE (tmpl_parm) != PARM_DECL
5539 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5540 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5541 || (TREE_CODE (tmpl_parm) == PARM_DECL
5542 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5543 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5544 {
5545 error ("template parameter %q+#D", tmpl_parm);
5546 error ("redeclared here as %q#D", parm);
5547 return false;
5548 }
5549
5550 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5551 {
5552 /* We have in [temp.param]:
5553
5554 A template-parameter may not be given default arguments
5555 by two different declarations in the same scope. */
5556 error_at (input_location, "redefinition of default argument for %q#D", parm);
5557 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5558 "original definition appeared here");
5559 return false;
5560 }
5561
5562 if (parm_default != NULL_TREE)
5563 /* Update the previous template parameters (which are the ones
5564 that will really count) with the new default value. */
5565 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5566 else if (tmpl_default != NULL_TREE)
5567 /* Update the new parameters, too; they'll be used as the
5568 parameters for any members. */
5569 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5570
5571 /* Give each template template parm in this redeclaration a
5572 DECL_CONTEXT of the template for which they are a parameter. */
5573 if (TREE_CODE (parm) == TEMPLATE_DECL)
5574 {
5575 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5576 DECL_CONTEXT (parm) = tmpl;
5577 }
5578 }
5579
5580 // Cannot redeclare a class template with a different set of constraints.
5581 if (!equivalent_constraints (get_constraints (tmpl), cons))
5582 {
5583 error_at (input_location, "redeclaration %q#D with different "
5584 "constraints", tmpl);
5585 inform (DECL_SOURCE_LOCATION (tmpl),
5586 "original declaration appeared here");
5587 }
5588
5589 return true;
5590 }
5591
5592 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5593 to be used when the caller has already checked
5594 (processing_template_decl
5595 && !instantiation_dependent_expression_p (expr)
5596 && potential_constant_expression (expr))
5597 and cleared processing_template_decl. */
5598
5599 tree
5600 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5601 {
5602 return tsubst_copy_and_build (expr,
5603 /*args=*/NULL_TREE,
5604 complain,
5605 /*in_decl=*/NULL_TREE,
5606 /*function_p=*/false,
5607 /*integral_constant_expression_p=*/true);
5608 }
5609
5610 /* Simplify EXPR if it is a non-dependent expression. Returns the
5611 (possibly simplified) expression. */
5612
5613 tree
5614 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5615 {
5616 if (expr == NULL_TREE)
5617 return NULL_TREE;
5618
5619 /* If we're in a template, but EXPR isn't value dependent, simplify
5620 it. We're supposed to treat:
5621
5622 template <typename T> void f(T[1 + 1]);
5623 template <typename T> void f(T[2]);
5624
5625 as two declarations of the same function, for example. */
5626 if (processing_template_decl
5627 && !instantiation_dependent_expression_p (expr)
5628 && potential_constant_expression (expr))
5629 {
5630 processing_template_decl_sentinel s;
5631 expr = instantiate_non_dependent_expr_internal (expr, complain);
5632 }
5633 return expr;
5634 }
5635
5636 tree
5637 instantiate_non_dependent_expr (tree expr)
5638 {
5639 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5640 }
5641
5642 /* True iff T is a specialization of a variable template. */
5643
5644 bool
5645 variable_template_specialization_p (tree t)
5646 {
5647 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5648 return false;
5649 tree tmpl = DECL_TI_TEMPLATE (t);
5650 return variable_template_p (tmpl);
5651 }
5652
5653 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5654 template declaration, or a TYPE_DECL for an alias declaration. */
5655
5656 bool
5657 alias_type_or_template_p (tree t)
5658 {
5659 if (t == NULL_TREE)
5660 return false;
5661 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5662 || (TYPE_P (t)
5663 && TYPE_NAME (t)
5664 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5665 || DECL_ALIAS_TEMPLATE_P (t));
5666 }
5667
5668 /* Return TRUE iff T is a specialization of an alias template. */
5669
5670 bool
5671 alias_template_specialization_p (const_tree t)
5672 {
5673 /* It's an alias template specialization if it's an alias and its
5674 TYPE_NAME is a specialization of a primary template. */
5675 if (TYPE_ALIAS_P (t))
5676 {
5677 tree name = TYPE_NAME (t);
5678 if (DECL_LANG_SPECIFIC (name))
5679 if (tree ti = DECL_TEMPLATE_INFO (name))
5680 {
5681 tree tmpl = TI_TEMPLATE (ti);
5682 return PRIMARY_TEMPLATE_P (tmpl);
5683 }
5684 }
5685 return false;
5686 }
5687
5688 /* An alias template is complex from a SFINAE perspective if a template-id
5689 using that alias can be ill-formed when the expansion is not, as with
5690 the void_t template. We determine this by checking whether the
5691 expansion for the alias template uses all its template parameters. */
5692
5693 struct uses_all_template_parms_data
5694 {
5695 int level;
5696 bool *seen;
5697 };
5698
5699 static int
5700 uses_all_template_parms_r (tree t, void *data_)
5701 {
5702 struct uses_all_template_parms_data &data
5703 = *(struct uses_all_template_parms_data*)data_;
5704 tree idx = get_template_parm_index (t);
5705
5706 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5707 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5708 return 0;
5709 }
5710
5711 static bool
5712 complex_alias_template_p (const_tree tmpl)
5713 {
5714 struct uses_all_template_parms_data data;
5715 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5716 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5717 data.level = TMPL_PARMS_DEPTH (parms);
5718 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5719 data.seen = XALLOCAVEC (bool, len);
5720 for (int i = 0; i < len; ++i)
5721 data.seen[i] = false;
5722
5723 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5724 for (int i = 0; i < len; ++i)
5725 if (!data.seen[i])
5726 return true;
5727 return false;
5728 }
5729
5730 /* Return TRUE iff T is a specialization of a complex alias template with
5731 dependent template-arguments. */
5732
5733 bool
5734 dependent_alias_template_spec_p (const_tree t)
5735 {
5736 return (alias_template_specialization_p (t)
5737 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5738 && (any_dependent_template_arguments_p
5739 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5740 }
5741
5742 /* Return the number of innermost template parameters in TMPL. */
5743
5744 static int
5745 num_innermost_template_parms (tree tmpl)
5746 {
5747 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5748 return TREE_VEC_LENGTH (parms);
5749 }
5750
5751 /* Return either TMPL or another template that it is equivalent to under DR
5752 1286: An alias that just changes the name of a template is equivalent to
5753 the other template. */
5754
5755 static tree
5756 get_underlying_template (tree tmpl)
5757 {
5758 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5759 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5760 {
5761 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5762 if (TYPE_TEMPLATE_INFO (result))
5763 {
5764 tree sub = TYPE_TI_TEMPLATE (result);
5765 if (PRIMARY_TEMPLATE_P (sub)
5766 && (num_innermost_template_parms (tmpl)
5767 == num_innermost_template_parms (sub)))
5768 {
5769 tree alias_args = INNERMOST_TEMPLATE_ARGS
5770 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5771 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5772 break;
5773 /* The alias type is equivalent to the pattern of the
5774 underlying template, so strip the alias. */
5775 tmpl = sub;
5776 continue;
5777 }
5778 }
5779 break;
5780 }
5781 return tmpl;
5782 }
5783
5784 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5785 must be a function or a pointer-to-function type, as specified
5786 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5787 and check that the resulting function has external linkage. */
5788
5789 static tree
5790 convert_nontype_argument_function (tree type, tree expr,
5791 tsubst_flags_t complain)
5792 {
5793 tree fns = expr;
5794 tree fn, fn_no_ptr;
5795 linkage_kind linkage;
5796
5797 fn = instantiate_type (type, fns, tf_none);
5798 if (fn == error_mark_node)
5799 return error_mark_node;
5800
5801 fn_no_ptr = fn;
5802 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5803 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5804 if (BASELINK_P (fn_no_ptr))
5805 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5806
5807 /* [temp.arg.nontype]/1
5808
5809 A template-argument for a non-type, non-template template-parameter
5810 shall be one of:
5811 [...]
5812 -- the address of an object or function with external [C++11: or
5813 internal] linkage. */
5814
5815 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5816 {
5817 if (complain & tf_error)
5818 {
5819 error ("%qE is not a valid template argument for type %qT",
5820 expr, type);
5821 if (TYPE_PTR_P (type))
5822 error ("it must be the address of a function with "
5823 "external linkage");
5824 else
5825 error ("it must be the name of a function with "
5826 "external linkage");
5827 }
5828 return NULL_TREE;
5829 }
5830
5831 linkage = decl_linkage (fn_no_ptr);
5832 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5833 {
5834 if (complain & tf_error)
5835 {
5836 if (cxx_dialect >= cxx11)
5837 error ("%qE is not a valid template argument for type %qT "
5838 "because %qD has no linkage",
5839 expr, type, fn_no_ptr);
5840 else
5841 error ("%qE is not a valid template argument for type %qT "
5842 "because %qD does not have external linkage",
5843 expr, type, fn_no_ptr);
5844 }
5845 return NULL_TREE;
5846 }
5847
5848 return fn;
5849 }
5850
5851 /* Subroutine of convert_nontype_argument.
5852 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5853 Emit an error otherwise. */
5854
5855 static bool
5856 check_valid_ptrmem_cst_expr (tree type, tree expr,
5857 tsubst_flags_t complain)
5858 {
5859 STRIP_NOPS (expr);
5860 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5861 return true;
5862 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5863 return true;
5864 if (processing_template_decl
5865 && TREE_CODE (expr) == ADDR_EXPR
5866 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5867 return true;
5868 if (complain & tf_error)
5869 {
5870 error ("%qE is not a valid template argument for type %qT",
5871 expr, type);
5872 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5873 }
5874 return false;
5875 }
5876
5877 /* Returns TRUE iff the address of OP is value-dependent.
5878
5879 14.6.2.4 [temp.dep.temp]:
5880 A non-integral non-type template-argument is dependent if its type is
5881 dependent or it has either of the following forms
5882 qualified-id
5883 & qualified-id
5884 and contains a nested-name-specifier which specifies a class-name that
5885 names a dependent type.
5886
5887 We generalize this to just say that the address of a member of a
5888 dependent class is value-dependent; the above doesn't cover the
5889 address of a static data member named with an unqualified-id. */
5890
5891 static bool
5892 has_value_dependent_address (tree op)
5893 {
5894 /* We could use get_inner_reference here, but there's no need;
5895 this is only relevant for template non-type arguments, which
5896 can only be expressed as &id-expression. */
5897 if (DECL_P (op))
5898 {
5899 tree ctx = CP_DECL_CONTEXT (op);
5900 if (TYPE_P (ctx) && dependent_type_p (ctx))
5901 return true;
5902 }
5903
5904 return false;
5905 }
5906
5907 /* The next set of functions are used for providing helpful explanatory
5908 diagnostics for failed overload resolution. Their messages should be
5909 indented by two spaces for consistency with the messages in
5910 call.c */
5911
5912 static int
5913 unify_success (bool /*explain_p*/)
5914 {
5915 return 0;
5916 }
5917
5918 static int
5919 unify_parameter_deduction_failure (bool explain_p, tree parm)
5920 {
5921 if (explain_p)
5922 inform (input_location,
5923 " couldn't deduce template parameter %qD", parm);
5924 return 1;
5925 }
5926
5927 static int
5928 unify_invalid (bool /*explain_p*/)
5929 {
5930 return 1;
5931 }
5932
5933 static int
5934 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5935 {
5936 if (explain_p)
5937 inform (input_location,
5938 " types %qT and %qT have incompatible cv-qualifiers",
5939 parm, arg);
5940 return 1;
5941 }
5942
5943 static int
5944 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5945 {
5946 if (explain_p)
5947 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5948 return 1;
5949 }
5950
5951 static int
5952 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5953 {
5954 if (explain_p)
5955 inform (input_location,
5956 " template parameter %qD is not a parameter pack, but "
5957 "argument %qD is",
5958 parm, arg);
5959 return 1;
5960 }
5961
5962 static int
5963 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5964 {
5965 if (explain_p)
5966 inform (input_location,
5967 " template argument %qE does not match "
5968 "pointer-to-member constant %qE",
5969 arg, parm);
5970 return 1;
5971 }
5972
5973 static int
5974 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5975 {
5976 if (explain_p)
5977 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5978 return 1;
5979 }
5980
5981 static int
5982 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5983 {
5984 if (explain_p)
5985 inform (input_location,
5986 " inconsistent parameter pack deduction with %qT and %qT",
5987 old_arg, new_arg);
5988 return 1;
5989 }
5990
5991 static int
5992 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5993 {
5994 if (explain_p)
5995 {
5996 if (TYPE_P (parm))
5997 inform (input_location,
5998 " deduced conflicting types for parameter %qT (%qT and %qT)",
5999 parm, first, second);
6000 else
6001 inform (input_location,
6002 " deduced conflicting values for non-type parameter "
6003 "%qE (%qE and %qE)", parm, first, second);
6004 }
6005 return 1;
6006 }
6007
6008 static int
6009 unify_vla_arg (bool explain_p, tree arg)
6010 {
6011 if (explain_p)
6012 inform (input_location,
6013 " variable-sized array type %qT is not "
6014 "a valid template argument",
6015 arg);
6016 return 1;
6017 }
6018
6019 static int
6020 unify_method_type_error (bool explain_p, tree arg)
6021 {
6022 if (explain_p)
6023 inform (input_location,
6024 " member function type %qT is not a valid template argument",
6025 arg);
6026 return 1;
6027 }
6028
6029 static int
6030 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6031 {
6032 if (explain_p)
6033 {
6034 if (least_p)
6035 inform_n (input_location, wanted,
6036 " candidate expects at least %d argument, %d provided",
6037 " candidate expects at least %d arguments, %d provided",
6038 wanted, have);
6039 else
6040 inform_n (input_location, wanted,
6041 " candidate expects %d argument, %d provided",
6042 " candidate expects %d arguments, %d provided",
6043 wanted, have);
6044 }
6045 return 1;
6046 }
6047
6048 static int
6049 unify_too_many_arguments (bool explain_p, int have, int wanted)
6050 {
6051 return unify_arity (explain_p, have, wanted);
6052 }
6053
6054 static int
6055 unify_too_few_arguments (bool explain_p, int have, int wanted,
6056 bool least_p = false)
6057 {
6058 return unify_arity (explain_p, have, wanted, least_p);
6059 }
6060
6061 static int
6062 unify_arg_conversion (bool explain_p, tree to_type,
6063 tree from_type, tree arg)
6064 {
6065 if (explain_p)
6066 inform (EXPR_LOC_OR_LOC (arg, input_location),
6067 " cannot convert %qE (type %qT) to type %qT",
6068 arg, from_type, to_type);
6069 return 1;
6070 }
6071
6072 static int
6073 unify_no_common_base (bool explain_p, enum template_base_result r,
6074 tree parm, tree arg)
6075 {
6076 if (explain_p)
6077 switch (r)
6078 {
6079 case tbr_ambiguous_baseclass:
6080 inform (input_location, " %qT is an ambiguous base class of %qT",
6081 parm, arg);
6082 break;
6083 default:
6084 inform (input_location, " %qT is not derived from %qT", arg, parm);
6085 break;
6086 }
6087 return 1;
6088 }
6089
6090 static int
6091 unify_inconsistent_template_template_parameters (bool explain_p)
6092 {
6093 if (explain_p)
6094 inform (input_location,
6095 " template parameters of a template template argument are "
6096 "inconsistent with other deduced template arguments");
6097 return 1;
6098 }
6099
6100 static int
6101 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6102 {
6103 if (explain_p)
6104 inform (input_location,
6105 " can't deduce a template for %qT from non-template type %qT",
6106 parm, arg);
6107 return 1;
6108 }
6109
6110 static int
6111 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6112 {
6113 if (explain_p)
6114 inform (input_location,
6115 " template argument %qE does not match %qD", arg, parm);
6116 return 1;
6117 }
6118
6119 static int
6120 unify_overload_resolution_failure (bool explain_p, tree arg)
6121 {
6122 if (explain_p)
6123 inform (input_location,
6124 " could not resolve address from overloaded function %qE",
6125 arg);
6126 return 1;
6127 }
6128
6129 /* Attempt to convert the non-type template parameter EXPR to the
6130 indicated TYPE. If the conversion is successful, return the
6131 converted value. If the conversion is unsuccessful, return
6132 NULL_TREE if we issued an error message, or error_mark_node if we
6133 did not. We issue error messages for out-and-out bad template
6134 parameters, but not simply because the conversion failed, since we
6135 might be just trying to do argument deduction. Both TYPE and EXPR
6136 must be non-dependent.
6137
6138 The conversion follows the special rules described in
6139 [temp.arg.nontype], and it is much more strict than an implicit
6140 conversion.
6141
6142 This function is called twice for each template argument (see
6143 lookup_template_class for a more accurate description of this
6144 problem). This means that we need to handle expressions which
6145 are not valid in a C++ source, but can be created from the
6146 first call (for instance, casts to perform conversions). These
6147 hacks can go away after we fix the double coercion problem. */
6148
6149 static tree
6150 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6151 {
6152 tree expr_type;
6153
6154 /* Detect immediately string literals as invalid non-type argument.
6155 This special-case is not needed for correctness (we would easily
6156 catch this later), but only to provide better diagnostic for this
6157 common user mistake. As suggested by DR 100, we do not mention
6158 linkage issues in the diagnostic as this is not the point. */
6159 /* FIXME we're making this OK. */
6160 if (TREE_CODE (expr) == STRING_CST)
6161 {
6162 if (complain & tf_error)
6163 error ("%qE is not a valid template argument for type %qT "
6164 "because string literals can never be used in this context",
6165 expr, type);
6166 return NULL_TREE;
6167 }
6168
6169 /* Add the ADDR_EXPR now for the benefit of
6170 value_dependent_expression_p. */
6171 if (TYPE_PTROBV_P (type)
6172 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6173 {
6174 expr = decay_conversion (expr, complain);
6175 if (expr == error_mark_node)
6176 return error_mark_node;
6177 }
6178
6179 /* If we are in a template, EXPR may be non-dependent, but still
6180 have a syntactic, rather than semantic, form. For example, EXPR
6181 might be a SCOPE_REF, rather than the VAR_DECL to which the
6182 SCOPE_REF refers. Preserving the qualifying scope is necessary
6183 so that access checking can be performed when the template is
6184 instantiated -- but here we need the resolved form so that we can
6185 convert the argument. */
6186 bool non_dep = false;
6187 if (TYPE_REF_OBJ_P (type)
6188 && has_value_dependent_address (expr))
6189 /* If we want the address and it's value-dependent, don't fold. */;
6190 else if (!type_unknown_p (expr)
6191 && processing_template_decl
6192 && !instantiation_dependent_expression_p (expr)
6193 && potential_constant_expression (expr))
6194 non_dep = true;
6195 if (error_operand_p (expr))
6196 return error_mark_node;
6197 expr_type = TREE_TYPE (expr);
6198 if (TREE_CODE (type) == REFERENCE_TYPE)
6199 expr = mark_lvalue_use (expr);
6200 else
6201 expr = mark_rvalue_use (expr);
6202
6203 /* If the argument is non-dependent, perform any conversions in
6204 non-dependent context as well. */
6205 processing_template_decl_sentinel s (non_dep);
6206 if (non_dep)
6207 expr = instantiate_non_dependent_expr_internal (expr, complain);
6208
6209 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6210 to a non-type argument of "nullptr". */
6211 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6212 expr = convert (type, expr);
6213
6214 /* In C++11, integral or enumeration non-type template arguments can be
6215 arbitrary constant expressions. Pointer and pointer to
6216 member arguments can be general constant expressions that evaluate
6217 to a null value, but otherwise still need to be of a specific form. */
6218 if (cxx_dialect >= cxx11)
6219 {
6220 if (TREE_CODE (expr) == PTRMEM_CST)
6221 /* A PTRMEM_CST is already constant, and a valid template
6222 argument for a parameter of pointer to member type, we just want
6223 to leave it in that form rather than lower it to a
6224 CONSTRUCTOR. */;
6225 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6226 expr = maybe_constant_value (expr);
6227 else if (TYPE_PTR_OR_PTRMEM_P (type))
6228 {
6229 tree folded = maybe_constant_value (expr);
6230 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6231 : null_member_pointer_value_p (folded))
6232 expr = folded;
6233 }
6234 }
6235
6236 /* HACK: Due to double coercion, we can get a
6237 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6238 which is the tree that we built on the first call (see
6239 below when coercing to reference to object or to reference to
6240 function). We just strip everything and get to the arg.
6241 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6242 for examples. */
6243 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6244 {
6245 tree probe_type, probe = expr;
6246 if (REFERENCE_REF_P (probe))
6247 probe = TREE_OPERAND (probe, 0);
6248 probe_type = TREE_TYPE (probe);
6249 if (TREE_CODE (probe) == NOP_EXPR)
6250 {
6251 /* ??? Maybe we could use convert_from_reference here, but we
6252 would need to relax its constraints because the NOP_EXPR
6253 could actually change the type to something more cv-qualified,
6254 and this is not folded by convert_from_reference. */
6255 tree addr = TREE_OPERAND (probe, 0);
6256 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6257 && TREE_CODE (addr) == ADDR_EXPR
6258 && TYPE_PTR_P (TREE_TYPE (addr))
6259 && (same_type_ignoring_top_level_qualifiers_p
6260 (TREE_TYPE (probe_type),
6261 TREE_TYPE (TREE_TYPE (addr)))))
6262 {
6263 expr = TREE_OPERAND (addr, 0);
6264 expr_type = TREE_TYPE (probe_type);
6265 }
6266 }
6267 }
6268
6269 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6270 parameter is a pointer to object, through decay and
6271 qualification conversion. Let's strip everything. */
6272 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6273 {
6274 tree probe = expr;
6275 STRIP_NOPS (probe);
6276 if (TREE_CODE (probe) == ADDR_EXPR
6277 && TYPE_PTR_P (TREE_TYPE (probe)))
6278 {
6279 /* Skip the ADDR_EXPR only if it is part of the decay for
6280 an array. Otherwise, it is part of the original argument
6281 in the source code. */
6282 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6283 probe = TREE_OPERAND (probe, 0);
6284 expr = probe;
6285 expr_type = TREE_TYPE (expr);
6286 }
6287 }
6288
6289 /* [temp.arg.nontype]/5, bullet 1
6290
6291 For a non-type template-parameter of integral or enumeration type,
6292 integral promotions (_conv.prom_) and integral conversions
6293 (_conv.integral_) are applied. */
6294 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6295 {
6296 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6297 t = maybe_constant_value (t);
6298 if (t != error_mark_node)
6299 expr = t;
6300
6301 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6302 return error_mark_node;
6303
6304 /* Notice that there are constant expressions like '4 % 0' which
6305 do not fold into integer constants. */
6306 if (TREE_CODE (expr) != INTEGER_CST)
6307 {
6308 if (complain & tf_error)
6309 {
6310 int errs = errorcount, warns = warningcount + werrorcount;
6311 if (processing_template_decl
6312 && !require_potential_constant_expression (expr))
6313 return NULL_TREE;
6314 expr = cxx_constant_value (expr);
6315 if (errorcount > errs || warningcount + werrorcount > warns)
6316 inform (EXPR_LOC_OR_LOC (expr, input_location),
6317 "in template argument for type %qT ", type);
6318 if (expr == error_mark_node)
6319 return NULL_TREE;
6320 /* else cxx_constant_value complained but gave us
6321 a real constant, so go ahead. */
6322 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6323 }
6324 else
6325 return NULL_TREE;
6326 }
6327
6328 /* Avoid typedef problems. */
6329 if (TREE_TYPE (expr) != type)
6330 expr = fold_convert (type, expr);
6331 }
6332 /* [temp.arg.nontype]/5, bullet 2
6333
6334 For a non-type template-parameter of type pointer to object,
6335 qualification conversions (_conv.qual_) and the array-to-pointer
6336 conversion (_conv.array_) are applied. */
6337 else if (TYPE_PTROBV_P (type))
6338 {
6339 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6340
6341 A template-argument for a non-type, non-template template-parameter
6342 shall be one of: [...]
6343
6344 -- the name of a non-type template-parameter;
6345 -- the address of an object or function with external linkage, [...]
6346 expressed as "& id-expression" where the & is optional if the name
6347 refers to a function or array, or if the corresponding
6348 template-parameter is a reference.
6349
6350 Here, we do not care about functions, as they are invalid anyway
6351 for a parameter of type pointer-to-object. */
6352
6353 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6354 /* Non-type template parameters are OK. */
6355 ;
6356 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6357 /* Null pointer values are OK in C++11. */;
6358 else if (TREE_CODE (expr) != ADDR_EXPR
6359 && TREE_CODE (expr_type) != ARRAY_TYPE)
6360 {
6361 if (VAR_P (expr))
6362 {
6363 if (complain & tf_error)
6364 error ("%qD is not a valid template argument "
6365 "because %qD is a variable, not the address of "
6366 "a variable", expr, expr);
6367 return NULL_TREE;
6368 }
6369 if (POINTER_TYPE_P (expr_type))
6370 {
6371 if (complain & tf_error)
6372 error ("%qE is not a valid template argument for %qT "
6373 "because it is not the address of a variable",
6374 expr, type);
6375 return NULL_TREE;
6376 }
6377 /* Other values, like integer constants, might be valid
6378 non-type arguments of some other type. */
6379 return error_mark_node;
6380 }
6381 else
6382 {
6383 tree decl;
6384
6385 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6386 ? TREE_OPERAND (expr, 0) : expr);
6387 if (!VAR_P (decl))
6388 {
6389 if (complain & tf_error)
6390 error ("%qE is not a valid template argument of type %qT "
6391 "because %qE is not a variable", expr, type, decl);
6392 return NULL_TREE;
6393 }
6394 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6395 {
6396 if (complain & tf_error)
6397 error ("%qE is not a valid template argument of type %qT "
6398 "because %qD does not have external linkage",
6399 expr, type, decl);
6400 return NULL_TREE;
6401 }
6402 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6403 {
6404 if (complain & tf_error)
6405 error ("%qE is not a valid template argument of type %qT "
6406 "because %qD has no linkage", expr, type, decl);
6407 return NULL_TREE;
6408 }
6409 }
6410
6411 expr = decay_conversion (expr, complain);
6412 if (expr == error_mark_node)
6413 return error_mark_node;
6414
6415 expr = perform_qualification_conversions (type, expr);
6416 if (expr == error_mark_node)
6417 return error_mark_node;
6418 }
6419 /* [temp.arg.nontype]/5, bullet 3
6420
6421 For a non-type template-parameter of type reference to object, no
6422 conversions apply. The type referred to by the reference may be more
6423 cv-qualified than the (otherwise identical) type of the
6424 template-argument. The template-parameter is bound directly to the
6425 template-argument, which must be an lvalue. */
6426 else if (TYPE_REF_OBJ_P (type))
6427 {
6428 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6429 expr_type))
6430 return error_mark_node;
6431
6432 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6433 {
6434 if (complain & tf_error)
6435 error ("%qE is not a valid template argument for type %qT "
6436 "because of conflicts in cv-qualification", expr, type);
6437 return NULL_TREE;
6438 }
6439
6440 if (!real_lvalue_p (expr))
6441 {
6442 if (complain & tf_error)
6443 error ("%qE is not a valid template argument for type %qT "
6444 "because it is not an lvalue", expr, type);
6445 return NULL_TREE;
6446 }
6447
6448 /* [temp.arg.nontype]/1
6449
6450 A template-argument for a non-type, non-template template-parameter
6451 shall be one of: [...]
6452
6453 -- the address of an object or function with external linkage. */
6454 if (INDIRECT_REF_P (expr)
6455 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6456 {
6457 expr = TREE_OPERAND (expr, 0);
6458 if (DECL_P (expr))
6459 {
6460 if (complain & tf_error)
6461 error ("%q#D is not a valid template argument for type %qT "
6462 "because a reference variable does not have a constant "
6463 "address", expr, type);
6464 return NULL_TREE;
6465 }
6466 }
6467
6468 if (!DECL_P (expr))
6469 {
6470 if (complain & tf_error)
6471 error ("%qE is not a valid template argument for type %qT "
6472 "because it is not an object with linkage",
6473 expr, type);
6474 return NULL_TREE;
6475 }
6476
6477 /* DR 1155 allows internal linkage in C++11 and up. */
6478 linkage_kind linkage = decl_linkage (expr);
6479 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6480 {
6481 if (complain & tf_error)
6482 error ("%qE is not a valid template argument for type %qT "
6483 "because object %qD does not have linkage",
6484 expr, type, expr);
6485 return NULL_TREE;
6486 }
6487
6488 expr = build_nop (type, build_address (expr));
6489 }
6490 /* [temp.arg.nontype]/5, bullet 4
6491
6492 For a non-type template-parameter of type pointer to function, only
6493 the function-to-pointer conversion (_conv.func_) is applied. If the
6494 template-argument represents a set of overloaded functions (or a
6495 pointer to such), the matching function is selected from the set
6496 (_over.over_). */
6497 else if (TYPE_PTRFN_P (type))
6498 {
6499 /* If the argument is a template-id, we might not have enough
6500 context information to decay the pointer. */
6501 if (!type_unknown_p (expr_type))
6502 {
6503 expr = decay_conversion (expr, complain);
6504 if (expr == error_mark_node)
6505 return error_mark_node;
6506 }
6507
6508 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6509 /* Null pointer values are OK in C++11. */
6510 return perform_qualification_conversions (type, expr);
6511
6512 expr = convert_nontype_argument_function (type, expr, complain);
6513 if (!expr || expr == error_mark_node)
6514 return expr;
6515 }
6516 /* [temp.arg.nontype]/5, bullet 5
6517
6518 For a non-type template-parameter of type reference to function, no
6519 conversions apply. If the template-argument represents a set of
6520 overloaded functions, the matching function is selected from the set
6521 (_over.over_). */
6522 else if (TYPE_REFFN_P (type))
6523 {
6524 if (TREE_CODE (expr) == ADDR_EXPR)
6525 {
6526 if (complain & tf_error)
6527 {
6528 error ("%qE is not a valid template argument for type %qT "
6529 "because it is a pointer", expr, type);
6530 inform (input_location, "try using %qE instead",
6531 TREE_OPERAND (expr, 0));
6532 }
6533 return NULL_TREE;
6534 }
6535
6536 expr = convert_nontype_argument_function (type, expr, complain);
6537 if (!expr || expr == error_mark_node)
6538 return expr;
6539
6540 expr = build_nop (type, build_address (expr));
6541 }
6542 /* [temp.arg.nontype]/5, bullet 6
6543
6544 For a non-type template-parameter of type pointer to member function,
6545 no conversions apply. If the template-argument represents a set of
6546 overloaded member functions, the matching member function is selected
6547 from the set (_over.over_). */
6548 else if (TYPE_PTRMEMFUNC_P (type))
6549 {
6550 expr = instantiate_type (type, expr, tf_none);
6551 if (expr == error_mark_node)
6552 return error_mark_node;
6553
6554 /* [temp.arg.nontype] bullet 1 says the pointer to member
6555 expression must be a pointer-to-member constant. */
6556 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6557 return error_mark_node;
6558
6559 /* There is no way to disable standard conversions in
6560 resolve_address_of_overloaded_function (called by
6561 instantiate_type). It is possible that the call succeeded by
6562 converting &B::I to &D::I (where B is a base of D), so we need
6563 to reject this conversion here.
6564
6565 Actually, even if there was a way to disable standard conversions,
6566 it would still be better to reject them here so that we can
6567 provide a superior diagnostic. */
6568 if (!same_type_p (TREE_TYPE (expr), type))
6569 {
6570 if (complain & tf_error)
6571 {
6572 error ("%qE is not a valid template argument for type %qT "
6573 "because it is of type %qT", expr, type,
6574 TREE_TYPE (expr));
6575 /* If we are just one standard conversion off, explain. */
6576 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6577 inform (input_location,
6578 "standard conversions are not allowed in this context");
6579 }
6580 return NULL_TREE;
6581 }
6582 }
6583 /* [temp.arg.nontype]/5, bullet 7
6584
6585 For a non-type template-parameter of type pointer to data member,
6586 qualification conversions (_conv.qual_) are applied. */
6587 else if (TYPE_PTRDATAMEM_P (type))
6588 {
6589 /* [temp.arg.nontype] bullet 1 says the pointer to member
6590 expression must be a pointer-to-member constant. */
6591 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6592 return error_mark_node;
6593
6594 expr = perform_qualification_conversions (type, expr);
6595 if (expr == error_mark_node)
6596 return expr;
6597 }
6598 else if (NULLPTR_TYPE_P (type))
6599 {
6600 if (expr != nullptr_node)
6601 {
6602 if (complain & tf_error)
6603 error ("%qE is not a valid template argument for type %qT "
6604 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6605 return NULL_TREE;
6606 }
6607 return expr;
6608 }
6609 /* A template non-type parameter must be one of the above. */
6610 else
6611 gcc_unreachable ();
6612
6613 /* Sanity check: did we actually convert the argument to the
6614 right type? */
6615 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6616 (type, TREE_TYPE (expr)));
6617 return convert_from_reference (expr);
6618 }
6619
6620 /* Subroutine of coerce_template_template_parms, which returns 1 if
6621 PARM_PARM and ARG_PARM match using the rule for the template
6622 parameters of template template parameters. Both PARM and ARG are
6623 template parameters; the rest of the arguments are the same as for
6624 coerce_template_template_parms.
6625 */
6626 static int
6627 coerce_template_template_parm (tree parm,
6628 tree arg,
6629 tsubst_flags_t complain,
6630 tree in_decl,
6631 tree outer_args)
6632 {
6633 if (arg == NULL_TREE || error_operand_p (arg)
6634 || parm == NULL_TREE || error_operand_p (parm))
6635 return 0;
6636
6637 if (TREE_CODE (arg) != TREE_CODE (parm))
6638 return 0;
6639
6640 switch (TREE_CODE (parm))
6641 {
6642 case TEMPLATE_DECL:
6643 /* We encounter instantiations of templates like
6644 template <template <template <class> class> class TT>
6645 class C; */
6646 {
6647 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6648 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6649
6650 if (!coerce_template_template_parms
6651 (parmparm, argparm, complain, in_decl, outer_args))
6652 return 0;
6653 }
6654 /* Fall through. */
6655
6656 case TYPE_DECL:
6657 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6658 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6659 /* Argument is a parameter pack but parameter is not. */
6660 return 0;
6661 break;
6662
6663 case PARM_DECL:
6664 /* The tsubst call is used to handle cases such as
6665
6666 template <int> class C {};
6667 template <class T, template <T> class TT> class D {};
6668 D<int, C> d;
6669
6670 i.e. the parameter list of TT depends on earlier parameters. */
6671 if (!uses_template_parms (TREE_TYPE (arg)))
6672 {
6673 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6674 if (!uses_template_parms (t)
6675 && !same_type_p (t, TREE_TYPE (arg)))
6676 return 0;
6677 }
6678
6679 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6680 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6681 /* Argument is a parameter pack but parameter is not. */
6682 return 0;
6683
6684 break;
6685
6686 default:
6687 gcc_unreachable ();
6688 }
6689
6690 return 1;
6691 }
6692
6693
6694 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6695 template template parameters. Both PARM_PARMS and ARG_PARMS are
6696 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6697 or PARM_DECL.
6698
6699 Consider the example:
6700 template <class T> class A;
6701 template<template <class U> class TT> class B;
6702
6703 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6704 the parameters to A, and OUTER_ARGS contains A. */
6705
6706 static int
6707 coerce_template_template_parms (tree parm_parms,
6708 tree arg_parms,
6709 tsubst_flags_t complain,
6710 tree in_decl,
6711 tree outer_args)
6712 {
6713 int nparms, nargs, i;
6714 tree parm, arg;
6715 int variadic_p = 0;
6716
6717 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6718 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6719
6720 nparms = TREE_VEC_LENGTH (parm_parms);
6721 nargs = TREE_VEC_LENGTH (arg_parms);
6722
6723 /* Determine whether we have a parameter pack at the end of the
6724 template template parameter's template parameter list. */
6725 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6726 {
6727 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6728
6729 if (error_operand_p (parm))
6730 return 0;
6731
6732 switch (TREE_CODE (parm))
6733 {
6734 case TEMPLATE_DECL:
6735 case TYPE_DECL:
6736 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6737 variadic_p = 1;
6738 break;
6739
6740 case PARM_DECL:
6741 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6742 variadic_p = 1;
6743 break;
6744
6745 default:
6746 gcc_unreachable ();
6747 }
6748 }
6749
6750 if (nargs != nparms
6751 && !(variadic_p && nargs >= nparms - 1))
6752 return 0;
6753
6754 /* Check all of the template parameters except the parameter pack at
6755 the end (if any). */
6756 for (i = 0; i < nparms - variadic_p; ++i)
6757 {
6758 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6759 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6760 continue;
6761
6762 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6763 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6764
6765 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6766 outer_args))
6767 return 0;
6768
6769 }
6770
6771 if (variadic_p)
6772 {
6773 /* Check each of the template parameters in the template
6774 argument against the template parameter pack at the end of
6775 the template template parameter. */
6776 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6777 return 0;
6778
6779 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6780
6781 for (; i < nargs; ++i)
6782 {
6783 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6784 continue;
6785
6786 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6787
6788 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6789 outer_args))
6790 return 0;
6791 }
6792 }
6793
6794 return 1;
6795 }
6796
6797 /* Verifies that the deduced template arguments (in TARGS) for the
6798 template template parameters (in TPARMS) represent valid bindings,
6799 by comparing the template parameter list of each template argument
6800 to the template parameter list of its corresponding template
6801 template parameter, in accordance with DR150. This
6802 routine can only be called after all template arguments have been
6803 deduced. It will return TRUE if all of the template template
6804 parameter bindings are okay, FALSE otherwise. */
6805 bool
6806 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6807 {
6808 int i, ntparms = TREE_VEC_LENGTH (tparms);
6809 bool ret = true;
6810
6811 /* We're dealing with template parms in this process. */
6812 ++processing_template_decl;
6813
6814 targs = INNERMOST_TEMPLATE_ARGS (targs);
6815
6816 for (i = 0; i < ntparms; ++i)
6817 {
6818 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6819 tree targ = TREE_VEC_ELT (targs, i);
6820
6821 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6822 {
6823 tree packed_args = NULL_TREE;
6824 int idx, len = 1;
6825
6826 if (ARGUMENT_PACK_P (targ))
6827 {
6828 /* Look inside the argument pack. */
6829 packed_args = ARGUMENT_PACK_ARGS (targ);
6830 len = TREE_VEC_LENGTH (packed_args);
6831 }
6832
6833 for (idx = 0; idx < len; ++idx)
6834 {
6835 tree targ_parms = NULL_TREE;
6836
6837 if (packed_args)
6838 /* Extract the next argument from the argument
6839 pack. */
6840 targ = TREE_VEC_ELT (packed_args, idx);
6841
6842 if (PACK_EXPANSION_P (targ))
6843 /* Look at the pattern of the pack expansion. */
6844 targ = PACK_EXPANSION_PATTERN (targ);
6845
6846 /* Extract the template parameters from the template
6847 argument. */
6848 if (TREE_CODE (targ) == TEMPLATE_DECL)
6849 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6850 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6851 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6852
6853 /* Verify that we can coerce the template template
6854 parameters from the template argument to the template
6855 parameter. This requires an exact match. */
6856 if (targ_parms
6857 && !coerce_template_template_parms
6858 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6859 targ_parms,
6860 tf_none,
6861 tparm,
6862 targs))
6863 {
6864 ret = false;
6865 goto out;
6866 }
6867 }
6868 }
6869 }
6870
6871 out:
6872
6873 --processing_template_decl;
6874 return ret;
6875 }
6876
6877 /* Since type attributes aren't mangled, we need to strip them from
6878 template type arguments. */
6879
6880 static tree
6881 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6882 {
6883 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6884 return arg;
6885 bool removed_attributes = false;
6886 tree canon = strip_typedefs (arg, &removed_attributes);
6887 if (removed_attributes
6888 && (complain & tf_warning))
6889 warning (0, "ignoring attributes on template argument %qT", arg);
6890 return canon;
6891 }
6892
6893 // A template declaration can be substituted for a constrained
6894 // template template parameter only when the argument is more
6895 // constrained than the parameter.
6896 static bool
6897 is_compatible_template_arg (tree parm, tree arg)
6898 {
6899 tree parm_cons = get_constraints (parm);
6900
6901 /* For now, allow constrained template template arguments
6902 and unconstrained template template parameters. */
6903 if (parm_cons == NULL_TREE)
6904 return true;
6905
6906 tree arg_cons = get_constraints (arg);
6907
6908 // If the template parameter is constrained, we need to rewrite its
6909 // constraints in terms of the ARG's template parameters. This ensures
6910 // that all of the template parameter types will have the same depth.
6911 //
6912 // Note that this is only valid when coerce_template_template_parm is
6913 // true for the innermost template parameters of PARM and ARG. In other
6914 // words, because coercion is successful, this conversion will be valid.
6915 if (parm_cons)
6916 {
6917 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
6918 parm_cons = tsubst_constraint_info (parm_cons,
6919 INNERMOST_TEMPLATE_ARGS (args),
6920 tf_none, NULL_TREE);
6921 if (parm_cons == error_mark_node)
6922 return false;
6923 }
6924
6925 return subsumes (parm_cons, arg_cons);
6926 }
6927
6928 // Convert a placeholder argument into a binding to the original
6929 // parameter. The original parameter is saved as the TREE_TYPE of
6930 // ARG.
6931 static inline tree
6932 convert_wildcard_argument (tree parm, tree arg)
6933 {
6934 TREE_TYPE (arg) = parm;
6935 return arg;
6936 }
6937
6938 /* Convert the indicated template ARG as necessary to match the
6939 indicated template PARM. Returns the converted ARG, or
6940 error_mark_node if the conversion was unsuccessful. Error and
6941 warning messages are issued under control of COMPLAIN. This
6942 conversion is for the Ith parameter in the parameter list. ARGS is
6943 the full set of template arguments deduced so far. */
6944
6945 static tree
6946 convert_template_argument (tree parm,
6947 tree arg,
6948 tree args,
6949 tsubst_flags_t complain,
6950 int i,
6951 tree in_decl)
6952 {
6953 tree orig_arg;
6954 tree val;
6955 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6956
6957 if (parm == error_mark_node)
6958 return error_mark_node;
6959
6960 /* Trivially convert placeholders. */
6961 if (TREE_CODE (arg) == WILDCARD_DECL)
6962 return convert_wildcard_argument (parm, arg);
6963
6964 if (TREE_CODE (arg) == TREE_LIST
6965 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6966 {
6967 /* The template argument was the name of some
6968 member function. That's usually
6969 invalid, but static members are OK. In any
6970 case, grab the underlying fields/functions
6971 and issue an error later if required. */
6972 orig_arg = TREE_VALUE (arg);
6973 TREE_TYPE (arg) = unknown_type_node;
6974 }
6975
6976 orig_arg = arg;
6977
6978 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6979 requires_type = (TREE_CODE (parm) == TYPE_DECL
6980 || requires_tmpl_type);
6981
6982 /* When determining whether an argument pack expansion is a template,
6983 look at the pattern. */
6984 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6985 arg = PACK_EXPANSION_PATTERN (arg);
6986
6987 /* Deal with an injected-class-name used as a template template arg. */
6988 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6989 {
6990 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6991 if (TREE_CODE (t) == TEMPLATE_DECL)
6992 {
6993 if (cxx_dialect >= cxx11)
6994 /* OK under DR 1004. */;
6995 else if (complain & tf_warning_or_error)
6996 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6997 " used as template template argument", TYPE_NAME (arg));
6998 else if (flag_pedantic_errors)
6999 t = arg;
7000
7001 arg = t;
7002 }
7003 }
7004
7005 is_tmpl_type =
7006 ((TREE_CODE (arg) == TEMPLATE_DECL
7007 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7008 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7009 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7010 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7011
7012 if (is_tmpl_type
7013 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7014 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7015 arg = TYPE_STUB_DECL (arg);
7016
7017 is_type = TYPE_P (arg) || is_tmpl_type;
7018
7019 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7020 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7021 {
7022 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7023 {
7024 if (complain & tf_error)
7025 error ("invalid use of destructor %qE as a type", orig_arg);
7026 return error_mark_node;
7027 }
7028
7029 permerror (input_location,
7030 "to refer to a type member of a template parameter, "
7031 "use %<typename %E%>", orig_arg);
7032
7033 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7034 TREE_OPERAND (arg, 1),
7035 typename_type,
7036 complain);
7037 arg = orig_arg;
7038 is_type = 1;
7039 }
7040 if (is_type != requires_type)
7041 {
7042 if (in_decl)
7043 {
7044 if (complain & tf_error)
7045 {
7046 error ("type/value mismatch at argument %d in template "
7047 "parameter list for %qD",
7048 i + 1, in_decl);
7049 if (is_type)
7050 inform (input_location,
7051 " expected a constant of type %qT, got %qT",
7052 TREE_TYPE (parm),
7053 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7054 else if (requires_tmpl_type)
7055 inform (input_location,
7056 " expected a class template, got %qE", orig_arg);
7057 else
7058 inform (input_location,
7059 " expected a type, got %qE", orig_arg);
7060 }
7061 }
7062 return error_mark_node;
7063 }
7064 if (is_tmpl_type ^ requires_tmpl_type)
7065 {
7066 if (in_decl && (complain & tf_error))
7067 {
7068 error ("type/value mismatch at argument %d in template "
7069 "parameter list for %qD",
7070 i + 1, in_decl);
7071 if (is_tmpl_type)
7072 inform (input_location,
7073 " expected a type, got %qT", DECL_NAME (arg));
7074 else
7075 inform (input_location,
7076 " expected a class template, got %qT", orig_arg);
7077 }
7078 return error_mark_node;
7079 }
7080
7081 if (is_type)
7082 {
7083 if (requires_tmpl_type)
7084 {
7085 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7086 val = orig_arg;
7087 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7088 /* The number of argument required is not known yet.
7089 Just accept it for now. */
7090 val = TREE_TYPE (arg);
7091 else
7092 {
7093 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7094 tree argparm;
7095
7096 /* Strip alias templates that are equivalent to another
7097 template. */
7098 arg = get_underlying_template (arg);
7099 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7100
7101 if (coerce_template_template_parms (parmparm, argparm,
7102 complain, in_decl,
7103 args))
7104 {
7105 val = arg;
7106
7107 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7108 TEMPLATE_DECL. */
7109 if (val != error_mark_node)
7110 {
7111 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7112 val = TREE_TYPE (val);
7113 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7114 val = make_pack_expansion (val);
7115 }
7116 }
7117 else
7118 {
7119 if (in_decl && (complain & tf_error))
7120 {
7121 error ("type/value mismatch at argument %d in "
7122 "template parameter list for %qD",
7123 i + 1, in_decl);
7124 inform (input_location,
7125 " expected a template of type %qD, got %qT",
7126 parm, orig_arg);
7127 }
7128
7129 val = error_mark_node;
7130 }
7131
7132 // Check that the constraints are compatible before allowing the
7133 // substitution.
7134 if (val != error_mark_node)
7135 if (!is_compatible_template_arg (parm, arg))
7136 {
7137 if (in_decl && (complain & tf_error))
7138 {
7139 error ("constraint mismatch at argument %d in "
7140 "template parameter list for %qD",
7141 i + 1, in_decl);
7142 inform (input_location, " expected %qD but got %qD",
7143 parm, arg);
7144 }
7145 val = error_mark_node;
7146 }
7147 }
7148 }
7149 else
7150 val = orig_arg;
7151 /* We only form one instance of each template specialization.
7152 Therefore, if we use a non-canonical variant (i.e., a
7153 typedef), any future messages referring to the type will use
7154 the typedef, which is confusing if those future uses do not
7155 themselves also use the typedef. */
7156 if (TYPE_P (val))
7157 val = canonicalize_type_argument (val, complain);
7158 }
7159 else
7160 {
7161 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7162
7163 if (invalid_nontype_parm_type_p (t, complain))
7164 return error_mark_node;
7165
7166 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7167 {
7168 if (same_type_p (t, TREE_TYPE (orig_arg)))
7169 val = orig_arg;
7170 else
7171 {
7172 /* Not sure if this is reachable, but it doesn't hurt
7173 to be robust. */
7174 error ("type mismatch in nontype parameter pack");
7175 val = error_mark_node;
7176 }
7177 }
7178 else if (!dependent_template_arg_p (orig_arg)
7179 && !uses_template_parms (t))
7180 /* We used to call digest_init here. However, digest_init
7181 will report errors, which we don't want when complain
7182 is zero. More importantly, digest_init will try too
7183 hard to convert things: for example, `0' should not be
7184 converted to pointer type at this point according to
7185 the standard. Accepting this is not merely an
7186 extension, since deciding whether or not these
7187 conversions can occur is part of determining which
7188 function template to call, or whether a given explicit
7189 argument specification is valid. */
7190 val = convert_nontype_argument (t, orig_arg, complain);
7191 else
7192 {
7193 bool removed_attr = false;
7194 val = strip_typedefs_expr (orig_arg, &removed_attr);
7195 }
7196
7197 if (val == NULL_TREE)
7198 val = error_mark_node;
7199 else if (val == error_mark_node && (complain & tf_error))
7200 error ("could not convert template argument %qE to %qT", orig_arg, t);
7201
7202 if (INDIRECT_REF_P (val))
7203 {
7204 /* Reject template arguments that are references to built-in
7205 functions with no library fallbacks. */
7206 const_tree inner = TREE_OPERAND (val, 0);
7207 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7208 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner))) == FUNCTION_TYPE
7209 && TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7210 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7211 return error_mark_node;
7212 }
7213
7214 if (TREE_CODE (val) == SCOPE_REF)
7215 {
7216 /* Strip typedefs from the SCOPE_REF. */
7217 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7218 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7219 complain);
7220 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7221 QUALIFIED_NAME_IS_TEMPLATE (val));
7222 }
7223 }
7224
7225 return val;
7226 }
7227
7228 /* Coerces the remaining template arguments in INNER_ARGS (from
7229 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7230 Returns the coerced argument pack. PARM_IDX is the position of this
7231 parameter in the template parameter list. ARGS is the original
7232 template argument list. */
7233 static tree
7234 coerce_template_parameter_pack (tree parms,
7235 int parm_idx,
7236 tree args,
7237 tree inner_args,
7238 int arg_idx,
7239 tree new_args,
7240 int* lost,
7241 tree in_decl,
7242 tsubst_flags_t complain)
7243 {
7244 tree parm = TREE_VEC_ELT (parms, parm_idx);
7245 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7246 tree packed_args;
7247 tree argument_pack;
7248 tree packed_parms = NULL_TREE;
7249
7250 if (arg_idx > nargs)
7251 arg_idx = nargs;
7252
7253 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7254 {
7255 /* When the template parameter is a non-type template parameter pack
7256 or template template parameter pack whose type or template
7257 parameters use parameter packs, we know exactly how many arguments
7258 we are looking for. Build a vector of the instantiated decls for
7259 these template parameters in PACKED_PARMS. */
7260 /* We can't use make_pack_expansion here because it would interpret a
7261 _DECL as a use rather than a declaration. */
7262 tree decl = TREE_VALUE (parm);
7263 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7264 SET_PACK_EXPANSION_PATTERN (exp, decl);
7265 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7266 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7267
7268 TREE_VEC_LENGTH (args)--;
7269 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7270 TREE_VEC_LENGTH (args)++;
7271
7272 if (packed_parms == error_mark_node)
7273 return error_mark_node;
7274
7275 /* If we're doing a partial instantiation of a member template,
7276 verify that all of the types used for the non-type
7277 template parameter pack are, in fact, valid for non-type
7278 template parameters. */
7279 if (arg_idx < nargs
7280 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7281 {
7282 int j, len = TREE_VEC_LENGTH (packed_parms);
7283 for (j = 0; j < len; ++j)
7284 {
7285 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7286 if (invalid_nontype_parm_type_p (t, complain))
7287 return error_mark_node;
7288 }
7289 /* We don't know how many args we have yet, just
7290 use the unconverted ones for now. */
7291 return NULL_TREE;
7292 }
7293
7294 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7295 }
7296 /* Check if we have a placeholder pack, which indicates we're
7297 in the context of a introduction list. In that case we want
7298 to match this pack to the single placeholder. */
7299 else if (arg_idx < nargs
7300 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7301 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7302 {
7303 nargs = arg_idx + 1;
7304 packed_args = make_tree_vec (1);
7305 }
7306 else
7307 packed_args = make_tree_vec (nargs - arg_idx);
7308
7309 /* Convert the remaining arguments, which will be a part of the
7310 parameter pack "parm". */
7311 for (; arg_idx < nargs; ++arg_idx)
7312 {
7313 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7314 tree actual_parm = TREE_VALUE (parm);
7315 int pack_idx = arg_idx - parm_idx;
7316
7317 if (packed_parms)
7318 {
7319 /* Once we've packed as many args as we have types, stop. */
7320 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7321 break;
7322 else if (PACK_EXPANSION_P (arg))
7323 /* We don't know how many args we have yet, just
7324 use the unconverted ones for now. */
7325 return NULL_TREE;
7326 else
7327 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7328 }
7329
7330 if (arg == error_mark_node)
7331 {
7332 if (complain & tf_error)
7333 error ("template argument %d is invalid", arg_idx + 1);
7334 }
7335 else
7336 arg = convert_template_argument (actual_parm,
7337 arg, new_args, complain, parm_idx,
7338 in_decl);
7339 if (arg == error_mark_node)
7340 (*lost)++;
7341 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7342 }
7343
7344 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
7345 && TREE_VEC_LENGTH (packed_args) > 0)
7346 {
7347 if (complain & tf_error)
7348 error ("wrong number of template arguments (%d, should be %d)",
7349 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
7350 return error_mark_node;
7351 }
7352
7353 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7354 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7355 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7356 else
7357 {
7358 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7359 TREE_TYPE (argument_pack)
7360 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7361 TREE_CONSTANT (argument_pack) = 1;
7362 }
7363
7364 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7365 #ifdef ENABLE_CHECKING
7366 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7367 TREE_VEC_LENGTH (packed_args));
7368 #endif
7369 return argument_pack;
7370 }
7371
7372 /* Returns the number of pack expansions in the template argument vector
7373 ARGS. */
7374
7375 static int
7376 pack_expansion_args_count (tree args)
7377 {
7378 int i;
7379 int count = 0;
7380 if (args)
7381 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7382 {
7383 tree elt = TREE_VEC_ELT (args, i);
7384 if (elt && PACK_EXPANSION_P (elt))
7385 ++count;
7386 }
7387 return count;
7388 }
7389
7390 /* Convert all template arguments to their appropriate types, and
7391 return a vector containing the innermost resulting template
7392 arguments. If any error occurs, return error_mark_node. Error and
7393 warning messages are issued under control of COMPLAIN.
7394
7395 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7396 for arguments not specified in ARGS. Otherwise, if
7397 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7398 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7399 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7400 ARGS. */
7401
7402 static tree
7403 coerce_template_parms (tree parms,
7404 tree args,
7405 tree in_decl,
7406 tsubst_flags_t complain,
7407 bool require_all_args,
7408 bool use_default_args)
7409 {
7410 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7411 tree orig_inner_args;
7412 tree inner_args;
7413 tree new_args;
7414 tree new_inner_args;
7415 int saved_unevaluated_operand;
7416 int saved_inhibit_evaluation_warnings;
7417
7418 /* When used as a boolean value, indicates whether this is a
7419 variadic template parameter list. Since it's an int, we can also
7420 subtract it from nparms to get the number of non-variadic
7421 parameters. */
7422 int variadic_p = 0;
7423 int variadic_args_p = 0;
7424 int post_variadic_parms = 0;
7425
7426 /* Likewise for parameters with default arguments. */
7427 int default_p = 0;
7428
7429 if (args == error_mark_node)
7430 return error_mark_node;
7431
7432 nparms = TREE_VEC_LENGTH (parms);
7433
7434 /* Determine if there are any parameter packs or default arguments. */
7435 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7436 {
7437 tree parm = TREE_VEC_ELT (parms, parm_idx);
7438 if (variadic_p)
7439 ++post_variadic_parms;
7440 if (template_parameter_pack_p (TREE_VALUE (parm)))
7441 ++variadic_p;
7442 if (TREE_PURPOSE (parm))
7443 ++default_p;
7444 }
7445
7446 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7447 /* If there are no parameters that follow a parameter pack, we need to
7448 expand any argument packs so that we can deduce a parameter pack from
7449 some non-packed args followed by an argument pack, as in variadic85.C.
7450 If there are such parameters, we need to leave argument packs intact
7451 so the arguments are assigned properly. This can happen when dealing
7452 with a nested class inside a partial specialization of a class
7453 template, as in variadic92.C, or when deducing a template parameter pack
7454 from a sub-declarator, as in variadic114.C. */
7455 if (!post_variadic_parms)
7456 inner_args = expand_template_argument_pack (inner_args);
7457
7458 /* Count any pack expansion args. */
7459 variadic_args_p = pack_expansion_args_count (inner_args);
7460
7461 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7462 if ((nargs > nparms && !variadic_p)
7463 || (nargs < nparms - variadic_p
7464 && require_all_args
7465 && !variadic_args_p
7466 && (!use_default_args
7467 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7468 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7469 {
7470 if (complain & tf_error)
7471 {
7472 if (variadic_p || default_p)
7473 {
7474 nparms -= variadic_p + default_p;
7475 error ("wrong number of template arguments "
7476 "(%d, should be at least %d)", nargs, nparms);
7477 }
7478 else
7479 error ("wrong number of template arguments "
7480 "(%d, should be %d)", nargs, nparms);
7481
7482 if (in_decl)
7483 inform (DECL_SOURCE_LOCATION (in_decl),
7484 "provided for %qD", in_decl);
7485 }
7486
7487 return error_mark_node;
7488 }
7489 /* We can't pass a pack expansion to a non-pack parameter of an alias
7490 template (DR 1430). */
7491 else if (in_decl
7492 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7493 || concept_template_p (in_decl))
7494 && variadic_args_p
7495 && nargs - variadic_args_p < nparms - variadic_p)
7496 {
7497 if (complain & tf_error)
7498 {
7499 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7500 {
7501 tree arg = TREE_VEC_ELT (inner_args, i);
7502 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7503
7504 if (PACK_EXPANSION_P (arg)
7505 && !template_parameter_pack_p (parm))
7506 {
7507 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7508 error_at (location_of (arg),
7509 "pack expansion argument for non-pack parameter "
7510 "%qD of alias template %qD", parm, in_decl);
7511 else
7512 error_at (location_of (arg),
7513 "pack expansion argument for non-pack parameter "
7514 "%qD of concept %qD", parm, in_decl);
7515 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7516 goto found;
7517 }
7518 }
7519 gcc_unreachable ();
7520 found:;
7521 }
7522 return error_mark_node;
7523 }
7524
7525 /* We need to evaluate the template arguments, even though this
7526 template-id may be nested within a "sizeof". */
7527 saved_unevaluated_operand = cp_unevaluated_operand;
7528 cp_unevaluated_operand = 0;
7529 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7530 c_inhibit_evaluation_warnings = 0;
7531 new_inner_args = make_tree_vec (nparms);
7532 new_args = add_outermost_template_args (args, new_inner_args);
7533 int pack_adjust = 0;
7534 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7535 {
7536 tree arg;
7537 tree parm;
7538
7539 /* Get the Ith template parameter. */
7540 parm = TREE_VEC_ELT (parms, parm_idx);
7541
7542 if (parm == error_mark_node)
7543 {
7544 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7545 continue;
7546 }
7547
7548 /* Calculate the next argument. */
7549 if (arg_idx < nargs)
7550 arg = TREE_VEC_ELT (inner_args, arg_idx);
7551 else
7552 arg = NULL_TREE;
7553
7554 if (template_parameter_pack_p (TREE_VALUE (parm))
7555 && !(arg && ARGUMENT_PACK_P (arg)))
7556 {
7557 /* Some arguments will be placed in the
7558 template parameter pack PARM. */
7559 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7560 inner_args, arg_idx,
7561 new_args, &lost,
7562 in_decl, complain);
7563
7564 if (arg == NULL_TREE)
7565 {
7566 /* We don't know how many args we have yet, just use the
7567 unconverted (and still packed) ones for now. */
7568 new_inner_args = orig_inner_args;
7569 arg_idx = nargs;
7570 break;
7571 }
7572
7573 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7574
7575 /* Store this argument. */
7576 if (arg == error_mark_node)
7577 {
7578 lost++;
7579 /* We are done with all of the arguments. */
7580 arg_idx = nargs;
7581 }
7582 else
7583 {
7584 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7585 arg_idx += pack_adjust;
7586 }
7587
7588 continue;
7589 }
7590 else if (arg)
7591 {
7592 if (PACK_EXPANSION_P (arg))
7593 {
7594 /* "If every valid specialization of a variadic template
7595 requires an empty template parameter pack, the template is
7596 ill-formed, no diagnostic required." So check that the
7597 pattern works with this parameter. */
7598 tree pattern = PACK_EXPANSION_PATTERN (arg);
7599 tree conv = convert_template_argument (TREE_VALUE (parm),
7600 pattern, new_args,
7601 complain, parm_idx,
7602 in_decl);
7603 if (conv == error_mark_node)
7604 {
7605 inform (input_location, "so any instantiation with a "
7606 "non-empty parameter pack would be ill-formed");
7607 ++lost;
7608 }
7609 else if (TYPE_P (conv) && !TYPE_P (pattern))
7610 /* Recover from missing typename. */
7611 TREE_VEC_ELT (inner_args, arg_idx)
7612 = make_pack_expansion (conv);
7613
7614 /* We don't know how many args we have yet, just
7615 use the unconverted ones for now. */
7616 new_inner_args = inner_args;
7617 arg_idx = nargs;
7618 break;
7619 }
7620 }
7621 else if (require_all_args)
7622 {
7623 /* There must be a default arg in this case. */
7624 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7625 complain, in_decl);
7626 /* The position of the first default template argument,
7627 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7628 Record that. */
7629 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7630 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7631 arg_idx - pack_adjust);
7632 }
7633 else
7634 break;
7635
7636 if (arg == error_mark_node)
7637 {
7638 if (complain & tf_error)
7639 error ("template argument %d is invalid", arg_idx + 1);
7640 }
7641 else if (!arg)
7642 /* This only occurs if there was an error in the template
7643 parameter list itself (which we would already have
7644 reported) that we are trying to recover from, e.g., a class
7645 template with a parameter list such as
7646 template<typename..., typename>. */
7647 ++lost;
7648 else
7649 arg = convert_template_argument (TREE_VALUE (parm),
7650 arg, new_args, complain,
7651 parm_idx, in_decl);
7652
7653 if (arg == error_mark_node)
7654 lost++;
7655 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7656 }
7657 cp_unevaluated_operand = saved_unevaluated_operand;
7658 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7659
7660 if (variadic_p && arg_idx < nargs)
7661 {
7662 if (complain & tf_error)
7663 {
7664 error ("wrong number of template arguments "
7665 "(%d, should be %d)", nargs, arg_idx);
7666 if (in_decl)
7667 error ("provided for %q+D", in_decl);
7668 }
7669 return error_mark_node;
7670 }
7671
7672 if (lost)
7673 return error_mark_node;
7674
7675 #ifdef ENABLE_CHECKING
7676 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7677 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7678 TREE_VEC_LENGTH (new_inner_args));
7679 #endif
7680
7681 return new_inner_args;
7682 }
7683
7684 /* Convert all template arguments to their appropriate types, and
7685 return a vector containing the innermost resulting template
7686 arguments. If any error occurs, return error_mark_node. Error and
7687 warning messages are not issued.
7688
7689 Note that no function argument deduction is performed, and default
7690 arguments are used to fill in unspecified arguments. */
7691 tree
7692 coerce_template_parms (tree parms, tree args, tree in_decl)
7693 {
7694 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7695 }
7696
7697 /* Convert all template arguments to their appropriate type, and
7698 instantiate default arguments as needed. This returns a vector
7699 containing the innermost resulting template arguments, or
7700 error_mark_node if unsuccessful. */
7701 tree
7702 coerce_template_parms (tree parms, tree args, tree in_decl,
7703 tsubst_flags_t complain)
7704 {
7705 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7706 }
7707
7708 /* Like coerce_template_parms. If PARMS represents all template
7709 parameters levels, this function returns a vector of vectors
7710 representing all the resulting argument levels. Note that in this
7711 case, only the innermost arguments are coerced because the
7712 outermost ones are supposed to have been coerced already.
7713
7714 Otherwise, if PARMS represents only (the innermost) vector of
7715 parameters, this function returns a vector containing just the
7716 innermost resulting arguments. */
7717
7718 static tree
7719 coerce_innermost_template_parms (tree parms,
7720 tree args,
7721 tree in_decl,
7722 tsubst_flags_t complain,
7723 bool require_all_args,
7724 bool use_default_args)
7725 {
7726 int parms_depth = TMPL_PARMS_DEPTH (parms);
7727 int args_depth = TMPL_ARGS_DEPTH (args);
7728 tree coerced_args;
7729
7730 if (parms_depth > 1)
7731 {
7732 coerced_args = make_tree_vec (parms_depth);
7733 tree level;
7734 int cur_depth;
7735
7736 for (level = parms, cur_depth = parms_depth;
7737 parms_depth > 0 && level != NULL_TREE;
7738 level = TREE_CHAIN (level), --cur_depth)
7739 {
7740 tree l;
7741 if (cur_depth == args_depth)
7742 l = coerce_template_parms (TREE_VALUE (level),
7743 args, in_decl, complain,
7744 require_all_args,
7745 use_default_args);
7746 else
7747 l = TMPL_ARGS_LEVEL (args, cur_depth);
7748
7749 if (l == error_mark_node)
7750 return error_mark_node;
7751
7752 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7753 }
7754 }
7755 else
7756 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7757 args, in_decl, complain,
7758 require_all_args,
7759 use_default_args);
7760 return coerced_args;
7761 }
7762
7763 /* Returns 1 if template args OT and NT are equivalent. */
7764
7765 static int
7766 template_args_equal (tree ot, tree nt)
7767 {
7768 if (nt == ot)
7769 return 1;
7770 if (nt == NULL_TREE || ot == NULL_TREE)
7771 return false;
7772
7773 if (TREE_CODE (nt) == TREE_VEC)
7774 /* For member templates */
7775 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7776 else if (PACK_EXPANSION_P (ot))
7777 return (PACK_EXPANSION_P (nt)
7778 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7779 PACK_EXPANSION_PATTERN (nt))
7780 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7781 PACK_EXPANSION_EXTRA_ARGS (nt)));
7782 else if (ARGUMENT_PACK_P (ot))
7783 {
7784 int i, len;
7785 tree opack, npack;
7786
7787 if (!ARGUMENT_PACK_P (nt))
7788 return 0;
7789
7790 opack = ARGUMENT_PACK_ARGS (ot);
7791 npack = ARGUMENT_PACK_ARGS (nt);
7792 len = TREE_VEC_LENGTH (opack);
7793 if (TREE_VEC_LENGTH (npack) != len)
7794 return 0;
7795 for (i = 0; i < len; ++i)
7796 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7797 TREE_VEC_ELT (npack, i)))
7798 return 0;
7799 return 1;
7800 }
7801 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7802 {
7803 /* We get here probably because we are in the middle of substituting
7804 into the pattern of a pack expansion. In that case the
7805 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7806 interested in. So we want to use the initial pack argument for
7807 the comparison. */
7808 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7809 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7810 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7811 return template_args_equal (ot, nt);
7812 }
7813 else if (TYPE_P (nt))
7814 {
7815 if (!TYPE_P (ot))
7816 return false;
7817 /* Don't treat an alias template specialization with dependent
7818 arguments as equivalent to its underlying type when used as a
7819 template argument; we need them to be distinct so that we
7820 substitute into the specialization arguments at instantiation
7821 time. And aliases can't be equivalent without being ==, so
7822 we don't need to look any deeper. */
7823 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7824 return false;
7825 else
7826 return same_type_p (ot, nt);
7827 }
7828 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7829 return 0;
7830 else
7831 {
7832 /* Try to treat a template non-type argument that has been converted
7833 to the parameter type as equivalent to one that hasn't yet. */
7834 for (enum tree_code code1 = TREE_CODE (ot);
7835 CONVERT_EXPR_CODE_P (code1)
7836 || code1 == NON_LVALUE_EXPR;
7837 code1 = TREE_CODE (ot))
7838 ot = TREE_OPERAND (ot, 0);
7839 for (enum tree_code code2 = TREE_CODE (nt);
7840 CONVERT_EXPR_CODE_P (code2)
7841 || code2 == NON_LVALUE_EXPR;
7842 code2 = TREE_CODE (nt))
7843 nt = TREE_OPERAND (nt, 0);
7844
7845 return cp_tree_equal (ot, nt);
7846 }
7847 }
7848
7849 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7850 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7851 NEWARG_PTR with the offending arguments if they are non-NULL. */
7852
7853 static int
7854 comp_template_args_with_info (tree oldargs, tree newargs,
7855 tree *oldarg_ptr, tree *newarg_ptr)
7856 {
7857 int i;
7858
7859 if (oldargs == newargs)
7860 return 1;
7861
7862 if (!oldargs || !newargs)
7863 return 0;
7864
7865 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7866 return 0;
7867
7868 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7869 {
7870 tree nt = TREE_VEC_ELT (newargs, i);
7871 tree ot = TREE_VEC_ELT (oldargs, i);
7872
7873 if (! template_args_equal (ot, nt))
7874 {
7875 if (oldarg_ptr != NULL)
7876 *oldarg_ptr = ot;
7877 if (newarg_ptr != NULL)
7878 *newarg_ptr = nt;
7879 return 0;
7880 }
7881 }
7882 return 1;
7883 }
7884
7885 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7886 of template arguments. Returns 0 otherwise. */
7887
7888 int
7889 comp_template_args (tree oldargs, tree newargs)
7890 {
7891 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7892 }
7893
7894 static void
7895 add_pending_template (tree d)
7896 {
7897 tree ti = (TYPE_P (d)
7898 ? CLASSTYPE_TEMPLATE_INFO (d)
7899 : DECL_TEMPLATE_INFO (d));
7900 struct pending_template *pt;
7901 int level;
7902
7903 if (TI_PENDING_TEMPLATE_FLAG (ti))
7904 return;
7905
7906 /* We are called both from instantiate_decl, where we've already had a
7907 tinst_level pushed, and instantiate_template, where we haven't.
7908 Compensate. */
7909 level = !current_tinst_level || current_tinst_level->decl != d;
7910
7911 if (level)
7912 push_tinst_level (d);
7913
7914 pt = ggc_alloc<pending_template> ();
7915 pt->next = NULL;
7916 pt->tinst = current_tinst_level;
7917 if (last_pending_template)
7918 last_pending_template->next = pt;
7919 else
7920 pending_templates = pt;
7921
7922 last_pending_template = pt;
7923
7924 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7925
7926 if (level)
7927 pop_tinst_level ();
7928 }
7929
7930
7931 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7932 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7933 documentation for TEMPLATE_ID_EXPR. */
7934
7935 tree
7936 lookup_template_function (tree fns, tree arglist)
7937 {
7938 tree type;
7939
7940 if (fns == error_mark_node || arglist == error_mark_node)
7941 return error_mark_node;
7942
7943 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7944
7945 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7946 {
7947 error ("%q#D is not a function template", fns);
7948 return error_mark_node;
7949 }
7950
7951 if (BASELINK_P (fns))
7952 {
7953 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7954 unknown_type_node,
7955 BASELINK_FUNCTIONS (fns),
7956 arglist);
7957 return fns;
7958 }
7959
7960 type = TREE_TYPE (fns);
7961 if (TREE_CODE (fns) == OVERLOAD || !type)
7962 type = unknown_type_node;
7963
7964 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7965 }
7966
7967 /* Within the scope of a template class S<T>, the name S gets bound
7968 (in build_self_reference) to a TYPE_DECL for the class, not a
7969 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7970 or one of its enclosing classes, and that type is a template,
7971 return the associated TEMPLATE_DECL. Otherwise, the original
7972 DECL is returned.
7973
7974 Also handle the case when DECL is a TREE_LIST of ambiguous
7975 injected-class-names from different bases. */
7976
7977 tree
7978 maybe_get_template_decl_from_type_decl (tree decl)
7979 {
7980 if (decl == NULL_TREE)
7981 return decl;
7982
7983 /* DR 176: A lookup that finds an injected-class-name (10.2
7984 [class.member.lookup]) can result in an ambiguity in certain cases
7985 (for example, if it is found in more than one base class). If all of
7986 the injected-class-names that are found refer to specializations of
7987 the same class template, and if the name is followed by a
7988 template-argument-list, the reference refers to the class template
7989 itself and not a specialization thereof, and is not ambiguous. */
7990 if (TREE_CODE (decl) == TREE_LIST)
7991 {
7992 tree t, tmpl = NULL_TREE;
7993 for (t = decl; t; t = TREE_CHAIN (t))
7994 {
7995 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7996 if (!tmpl)
7997 tmpl = elt;
7998 else if (tmpl != elt)
7999 break;
8000 }
8001 if (tmpl && t == NULL_TREE)
8002 return tmpl;
8003 else
8004 return decl;
8005 }
8006
8007 return (decl != NULL_TREE
8008 && DECL_SELF_REFERENCE_P (decl)
8009 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8010 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8011 }
8012
8013 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8014 parameters, find the desired type.
8015
8016 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8017
8018 IN_DECL, if non-NULL, is the template declaration we are trying to
8019 instantiate.
8020
8021 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8022 the class we are looking up.
8023
8024 Issue error and warning messages under control of COMPLAIN.
8025
8026 If the template class is really a local class in a template
8027 function, then the FUNCTION_CONTEXT is the function in which it is
8028 being instantiated.
8029
8030 ??? Note that this function is currently called *twice* for each
8031 template-id: the first time from the parser, while creating the
8032 incomplete type (finish_template_type), and the second type during the
8033 real instantiation (instantiate_template_class). This is surely something
8034 that we want to avoid. It also causes some problems with argument
8035 coercion (see convert_nontype_argument for more information on this). */
8036
8037 static tree
8038 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8039 int entering_scope, tsubst_flags_t complain)
8040 {
8041 tree templ = NULL_TREE, parmlist;
8042 tree t;
8043 spec_entry **slot;
8044 spec_entry *entry;
8045 spec_entry elt;
8046 hashval_t hash;
8047
8048 if (identifier_p (d1))
8049 {
8050 tree value = innermost_non_namespace_value (d1);
8051 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8052 templ = value;
8053 else
8054 {
8055 if (context)
8056 push_decl_namespace (context);
8057 templ = lookup_name (d1);
8058 templ = maybe_get_template_decl_from_type_decl (templ);
8059 if (context)
8060 pop_decl_namespace ();
8061 }
8062 if (templ)
8063 context = DECL_CONTEXT (templ);
8064 }
8065 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8066 {
8067 tree type = TREE_TYPE (d1);
8068
8069 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8070 an implicit typename for the second A. Deal with it. */
8071 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8072 type = TREE_TYPE (type);
8073
8074 if (CLASSTYPE_TEMPLATE_INFO (type))
8075 {
8076 templ = CLASSTYPE_TI_TEMPLATE (type);
8077 d1 = DECL_NAME (templ);
8078 }
8079 }
8080 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8081 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8082 {
8083 templ = TYPE_TI_TEMPLATE (d1);
8084 d1 = DECL_NAME (templ);
8085 }
8086 else if (DECL_TYPE_TEMPLATE_P (d1))
8087 {
8088 templ = d1;
8089 d1 = DECL_NAME (templ);
8090 context = DECL_CONTEXT (templ);
8091 }
8092 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8093 {
8094 templ = d1;
8095 d1 = DECL_NAME (templ);
8096 }
8097
8098 /* Issue an error message if we didn't find a template. */
8099 if (! templ)
8100 {
8101 if (complain & tf_error)
8102 error ("%qT is not a template", d1);
8103 return error_mark_node;
8104 }
8105
8106 if (TREE_CODE (templ) != TEMPLATE_DECL
8107 /* Make sure it's a user visible template, if it was named by
8108 the user. */
8109 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8110 && !PRIMARY_TEMPLATE_P (templ)))
8111 {
8112 if (complain & tf_error)
8113 {
8114 error ("non-template type %qT used as a template", d1);
8115 if (in_decl)
8116 error ("for template declaration %q+D", in_decl);
8117 }
8118 return error_mark_node;
8119 }
8120
8121 complain &= ~tf_user;
8122
8123 /* An alias that just changes the name of a template is equivalent to the
8124 other template, so if any of the arguments are pack expansions, strip
8125 the alias to avoid problems with a pack expansion passed to a non-pack
8126 alias template parameter (DR 1430). */
8127 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8128 templ = get_underlying_template (templ);
8129
8130 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8131 {
8132 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8133 template arguments */
8134
8135 tree parm;
8136 tree arglist2;
8137 tree outer;
8138
8139 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8140
8141 /* Consider an example where a template template parameter declared as
8142
8143 template <class T, class U = std::allocator<T> > class TT
8144
8145 The template parameter level of T and U are one level larger than
8146 of TT. To proper process the default argument of U, say when an
8147 instantiation `TT<int>' is seen, we need to build the full
8148 arguments containing {int} as the innermost level. Outer levels,
8149 available when not appearing as default template argument, can be
8150 obtained from the arguments of the enclosing template.
8151
8152 Suppose that TT is later substituted with std::vector. The above
8153 instantiation is `TT<int, std::allocator<T> >' with TT at
8154 level 1, and T at level 2, while the template arguments at level 1
8155 becomes {std::vector} and the inner level 2 is {int}. */
8156
8157 outer = DECL_CONTEXT (templ);
8158 if (outer)
8159 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8160 else if (current_template_parms)
8161 {
8162 /* This is an argument of the current template, so we haven't set
8163 DECL_CONTEXT yet. */
8164 tree relevant_template_parms;
8165
8166 /* Parameter levels that are greater than the level of the given
8167 template template parm are irrelevant. */
8168 relevant_template_parms = current_template_parms;
8169 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8170 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8171 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8172
8173 outer = template_parms_to_args (relevant_template_parms);
8174 }
8175
8176 if (outer)
8177 arglist = add_to_template_args (outer, arglist);
8178
8179 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8180 complain,
8181 /*require_all_args=*/true,
8182 /*use_default_args=*/true);
8183 if (arglist2 == error_mark_node
8184 || (!uses_template_parms (arglist2)
8185 && check_instantiated_args (templ, arglist2, complain)))
8186 return error_mark_node;
8187
8188 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8189 return parm;
8190 }
8191 else
8192 {
8193 tree template_type = TREE_TYPE (templ);
8194 tree gen_tmpl;
8195 tree type_decl;
8196 tree found = NULL_TREE;
8197 int arg_depth;
8198 int parm_depth;
8199 int is_dependent_type;
8200 int use_partial_inst_tmpl = false;
8201
8202 if (template_type == error_mark_node)
8203 /* An error occurred while building the template TEMPL, and a
8204 diagnostic has most certainly been emitted for that
8205 already. Let's propagate that error. */
8206 return error_mark_node;
8207
8208 gen_tmpl = most_general_template (templ);
8209 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8210 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8211 arg_depth = TMPL_ARGS_DEPTH (arglist);
8212
8213 if (arg_depth == 1 && parm_depth > 1)
8214 {
8215 /* We've been given an incomplete set of template arguments.
8216 For example, given:
8217
8218 template <class T> struct S1 {
8219 template <class U> struct S2 {};
8220 template <class U> struct S2<U*> {};
8221 };
8222
8223 we will be called with an ARGLIST of `U*', but the
8224 TEMPLATE will be `template <class T> template
8225 <class U> struct S1<T>::S2'. We must fill in the missing
8226 arguments. */
8227 arglist
8228 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8229 arglist);
8230 arg_depth = TMPL_ARGS_DEPTH (arglist);
8231 }
8232
8233 /* Now we should have enough arguments. */
8234 gcc_assert (parm_depth == arg_depth);
8235
8236 /* From here on, we're only interested in the most general
8237 template. */
8238
8239 /* Calculate the BOUND_ARGS. These will be the args that are
8240 actually tsubst'd into the definition to create the
8241 instantiation. */
8242 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8243 complain,
8244 /*require_all_args=*/true,
8245 /*use_default_args=*/true);
8246
8247 if (arglist == error_mark_node)
8248 /* We were unable to bind the arguments. */
8249 return error_mark_node;
8250
8251 /* In the scope of a template class, explicit references to the
8252 template class refer to the type of the template, not any
8253 instantiation of it. For example, in:
8254
8255 template <class T> class C { void f(C<T>); }
8256
8257 the `C<T>' is just the same as `C'. Outside of the
8258 class, however, such a reference is an instantiation. */
8259 if ((entering_scope
8260 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8261 || currently_open_class (template_type))
8262 /* comp_template_args is expensive, check it last. */
8263 && comp_template_args (TYPE_TI_ARGS (template_type),
8264 arglist))
8265 return template_type;
8266
8267 /* If we already have this specialization, return it. */
8268 elt.tmpl = gen_tmpl;
8269 elt.args = arglist;
8270 elt.spec = NULL_TREE;
8271 hash = spec_hasher::hash (&elt);
8272 entry = type_specializations->find_with_hash (&elt, hash);
8273
8274 if (entry)
8275 return entry->spec;
8276
8277 /* If the the template's constraints are not satisfied,
8278 then we cannot form a valid type.
8279
8280 Note that the check is deferred until after the hash
8281 lookup. This prevents redundant checks on previously
8282 instantiated specializations. */
8283 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8284 {
8285 if (complain & tf_error)
8286 {
8287 error ("template constraint failure");
8288 diagnose_constraints (input_location, gen_tmpl, arglist);
8289 }
8290 return error_mark_node;
8291 }
8292
8293 is_dependent_type = uses_template_parms (arglist);
8294
8295 /* If the deduced arguments are invalid, then the binding
8296 failed. */
8297 if (!is_dependent_type
8298 && check_instantiated_args (gen_tmpl,
8299 INNERMOST_TEMPLATE_ARGS (arglist),
8300 complain))
8301 return error_mark_node;
8302
8303 if (!is_dependent_type
8304 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8305 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8306 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8307 {
8308 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8309 DECL_NAME (gen_tmpl),
8310 /*tag_scope=*/ts_global);
8311 return found;
8312 }
8313
8314 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8315 complain, in_decl);
8316 if (context == error_mark_node)
8317 return error_mark_node;
8318
8319 if (!context)
8320 context = global_namespace;
8321
8322 /* Create the type. */
8323 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8324 {
8325 /* The user referred to a specialization of an alias
8326 template represented by GEN_TMPL.
8327
8328 [temp.alias]/2 says:
8329
8330 When a template-id refers to the specialization of an
8331 alias template, it is equivalent to the associated
8332 type obtained by substitution of its
8333 template-arguments for the template-parameters in the
8334 type-id of the alias template. */
8335
8336 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8337 /* Note that the call above (by indirectly calling
8338 register_specialization in tsubst_decl) registers the
8339 TYPE_DECL representing the specialization of the alias
8340 template. So next time someone substitutes ARGLIST for
8341 the template parms into the alias template (GEN_TMPL),
8342 she'll get that TYPE_DECL back. */
8343
8344 if (t == error_mark_node)
8345 return t;
8346 }
8347 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8348 {
8349 if (!is_dependent_type)
8350 {
8351 set_current_access_from_decl (TYPE_NAME (template_type));
8352 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8353 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8354 arglist, complain, in_decl),
8355 SCOPED_ENUM_P (template_type), NULL);
8356
8357 if (t == error_mark_node)
8358 return t;
8359 }
8360 else
8361 {
8362 /* We don't want to call start_enum for this type, since
8363 the values for the enumeration constants may involve
8364 template parameters. And, no one should be interested
8365 in the enumeration constants for such a type. */
8366 t = cxx_make_type (ENUMERAL_TYPE);
8367 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8368 }
8369 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8370 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8371 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8372 }
8373 else if (CLASS_TYPE_P (template_type))
8374 {
8375 t = make_class_type (TREE_CODE (template_type));
8376 CLASSTYPE_DECLARED_CLASS (t)
8377 = CLASSTYPE_DECLARED_CLASS (template_type);
8378 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8379 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
8380
8381 /* A local class. Make sure the decl gets registered properly. */
8382 if (context == current_function_decl)
8383 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8384
8385 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8386 /* This instantiation is another name for the primary
8387 template type. Set the TYPE_CANONICAL field
8388 appropriately. */
8389 TYPE_CANONICAL (t) = template_type;
8390 else if (any_template_arguments_need_structural_equality_p (arglist))
8391 /* Some of the template arguments require structural
8392 equality testing, so this template class requires
8393 structural equality testing. */
8394 SET_TYPE_STRUCTURAL_EQUALITY (t);
8395 }
8396 else
8397 gcc_unreachable ();
8398
8399 /* If we called start_enum or pushtag above, this information
8400 will already be set up. */
8401 if (!TYPE_NAME (t))
8402 {
8403 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8404
8405 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8406 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8407 DECL_SOURCE_LOCATION (type_decl)
8408 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8409 }
8410 else
8411 type_decl = TYPE_NAME (t);
8412
8413 if (CLASS_TYPE_P (template_type))
8414 {
8415 TREE_PRIVATE (type_decl)
8416 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8417 TREE_PROTECTED (type_decl)
8418 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8419 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8420 {
8421 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8422 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8423 }
8424 }
8425
8426 if (OVERLOAD_TYPE_P (t)
8427 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8428 {
8429 static const char *tags[] = {"abi_tag", "may_alias"};
8430
8431 for (unsigned ix = 0; ix != 2; ix++)
8432 {
8433 tree attributes
8434 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8435
8436 if (!attributes)
8437 ;
8438 else if (!TREE_CHAIN (attributes) && !TYPE_ATTRIBUTES (t))
8439 TYPE_ATTRIBUTES (t) = attributes;
8440 else
8441 TYPE_ATTRIBUTES (t)
8442 = tree_cons (TREE_PURPOSE (attributes),
8443 TREE_VALUE (attributes),
8444 TYPE_ATTRIBUTES (t));
8445 }
8446 }
8447
8448 /* Let's consider the explicit specialization of a member
8449 of a class template specialization that is implicitly instantiated,
8450 e.g.:
8451 template<class T>
8452 struct S
8453 {
8454 template<class U> struct M {}; //#0
8455 };
8456
8457 template<>
8458 template<>
8459 struct S<int>::M<char> //#1
8460 {
8461 int i;
8462 };
8463 [temp.expl.spec]/4 says this is valid.
8464
8465 In this case, when we write:
8466 S<int>::M<char> m;
8467
8468 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8469 the one of #0.
8470
8471 When we encounter #1, we want to store the partial instantiation
8472 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8473
8474 For all cases other than this "explicit specialization of member of a
8475 class template", we just want to store the most general template into
8476 the CLASSTYPE_TI_TEMPLATE of M.
8477
8478 This case of "explicit specialization of member of a class template"
8479 only happens when:
8480 1/ the enclosing class is an instantiation of, and therefore not
8481 the same as, the context of the most general template, and
8482 2/ we aren't looking at the partial instantiation itself, i.e.
8483 the innermost arguments are not the same as the innermost parms of
8484 the most general template.
8485
8486 So it's only when 1/ and 2/ happens that we want to use the partial
8487 instantiation of the member template in lieu of its most general
8488 template. */
8489
8490 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8491 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8492 /* the enclosing class must be an instantiation... */
8493 && CLASS_TYPE_P (context)
8494 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8495 {
8496 tree partial_inst_args;
8497 TREE_VEC_LENGTH (arglist)--;
8498 ++processing_template_decl;
8499 partial_inst_args =
8500 tsubst (INNERMOST_TEMPLATE_ARGS
8501 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8502 arglist, complain, NULL_TREE);
8503 --processing_template_decl;
8504 TREE_VEC_LENGTH (arglist)++;
8505 use_partial_inst_tmpl =
8506 /*...and we must not be looking at the partial instantiation
8507 itself. */
8508 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8509 partial_inst_args);
8510 }
8511
8512 if (!use_partial_inst_tmpl)
8513 /* This case is easy; there are no member templates involved. */
8514 found = gen_tmpl;
8515 else
8516 {
8517 /* This is a full instantiation of a member template. Find
8518 the partial instantiation of which this is an instance. */
8519
8520 /* Temporarily reduce by one the number of levels in the ARGLIST
8521 so as to avoid comparing the last set of arguments. */
8522 TREE_VEC_LENGTH (arglist)--;
8523 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8524 TREE_VEC_LENGTH (arglist)++;
8525 /* FOUND is either a proper class type, or an alias
8526 template specialization. In the later case, it's a
8527 TYPE_DECL, resulting from the substituting of arguments
8528 for parameters in the TYPE_DECL of the alias template
8529 done earlier. So be careful while getting the template
8530 of FOUND. */
8531 found = TREE_CODE (found) == TYPE_DECL
8532 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8533 : CLASSTYPE_TI_TEMPLATE (found);
8534 }
8535
8536 // Build template info for the new specialization.
8537 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8538
8539 elt.spec = t;
8540 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8541 entry = ggc_alloc<spec_entry> ();
8542 *entry = elt;
8543 *slot = entry;
8544
8545 /* Note this use of the partial instantiation so we can check it
8546 later in maybe_process_partial_specialization. */
8547 DECL_TEMPLATE_INSTANTIATIONS (found)
8548 = tree_cons (arglist, t,
8549 DECL_TEMPLATE_INSTANTIATIONS (found));
8550
8551 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8552 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8553 /* Now that the type has been registered on the instantiations
8554 list, we set up the enumerators. Because the enumeration
8555 constants may involve the enumeration type itself, we make
8556 sure to register the type first, and then create the
8557 constants. That way, doing tsubst_expr for the enumeration
8558 constants won't result in recursive calls here; we'll find
8559 the instantiation and exit above. */
8560 tsubst_enum (template_type, t, arglist);
8561
8562 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8563 /* If the type makes use of template parameters, the
8564 code that generates debugging information will crash. */
8565 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8566
8567 /* Possibly limit visibility based on template args. */
8568 TREE_PUBLIC (type_decl) = 1;
8569 determine_visibility (type_decl);
8570
8571 inherit_targ_abi_tags (t);
8572
8573 return t;
8574 }
8575 }
8576
8577 /* Wrapper for lookup_template_class_1. */
8578
8579 tree
8580 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8581 int entering_scope, tsubst_flags_t complain)
8582 {
8583 tree ret;
8584 timevar_push (TV_TEMPLATE_INST);
8585 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8586 entering_scope, complain);
8587 timevar_pop (TV_TEMPLATE_INST);
8588 return ret;
8589 }
8590
8591 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8592
8593 tree
8594 lookup_template_variable (tree templ, tree arglist)
8595 {
8596 /* The type of the expression is NULL_TREE since the template-id could refer
8597 to an explicit or partial specialization. */
8598 tree type = NULL_TREE;
8599 if (flag_concepts && variable_concept_p (templ))
8600 /* Except that concepts are always bool. */
8601 type = boolean_type_node;
8602 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8603 }
8604
8605 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8606
8607 tree
8608 finish_template_variable (tree var, tsubst_flags_t complain)
8609 {
8610 tree templ = TREE_OPERAND (var, 0);
8611 tree arglist = TREE_OPERAND (var, 1);
8612
8613 /* We never want to return a VAR_DECL for a variable concept, since they
8614 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8615 bool concept_p = flag_concepts && variable_concept_p (templ);
8616 if (concept_p && processing_template_decl)
8617 return var;
8618
8619 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8620 arglist = add_outermost_template_args (tmpl_args, arglist);
8621
8622 tree parms = DECL_TEMPLATE_PARMS (templ);
8623 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8624 /*req_all*/true,
8625 /*use_default*/true);
8626
8627 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
8628 {
8629 if (complain & tf_error)
8630 {
8631 error ("constraints for %qD not satisfied", templ);
8632 diagnose_constraints (location_of (var), templ, arglist);
8633 }
8634 return error_mark_node;
8635 }
8636
8637 /* If a template-id refers to a specialization of a variable
8638 concept, then the expression is true if and only if the
8639 concept's constraints are satisfied by the given template
8640 arguments.
8641
8642 NOTE: This is an extension of Concepts Lite TS that
8643 allows constraints to be used in expressions. */
8644 if (concept_p)
8645 {
8646 tree decl = DECL_TEMPLATE_RESULT (templ);
8647 return evaluate_variable_concept (decl, arglist);
8648 }
8649
8650 return instantiate_template (templ, arglist, complain);
8651 }
8652 \f
8653 struct pair_fn_data
8654 {
8655 tree_fn_t fn;
8656 void *data;
8657 /* True when we should also visit template parameters that occur in
8658 non-deduced contexts. */
8659 bool include_nondeduced_p;
8660 hash_set<tree> *visited;
8661 };
8662
8663 /* Called from for_each_template_parm via walk_tree. */
8664
8665 static tree
8666 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8667 {
8668 tree t = *tp;
8669 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8670 tree_fn_t fn = pfd->fn;
8671 void *data = pfd->data;
8672
8673 if (TYPE_P (t)
8674 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8675 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8676 pfd->include_nondeduced_p))
8677 return error_mark_node;
8678
8679 switch (TREE_CODE (t))
8680 {
8681 case RECORD_TYPE:
8682 if (TYPE_PTRMEMFUNC_P (t))
8683 break;
8684 /* Fall through. */
8685
8686 case UNION_TYPE:
8687 case ENUMERAL_TYPE:
8688 if (!TYPE_TEMPLATE_INFO (t))
8689 *walk_subtrees = 0;
8690 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8691 fn, data, pfd->visited,
8692 pfd->include_nondeduced_p))
8693 return error_mark_node;
8694 break;
8695
8696 case INTEGER_TYPE:
8697 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8698 fn, data, pfd->visited,
8699 pfd->include_nondeduced_p)
8700 || for_each_template_parm (TYPE_MAX_VALUE (t),
8701 fn, data, pfd->visited,
8702 pfd->include_nondeduced_p))
8703 return error_mark_node;
8704 break;
8705
8706 case METHOD_TYPE:
8707 /* Since we're not going to walk subtrees, we have to do this
8708 explicitly here. */
8709 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8710 pfd->visited, pfd->include_nondeduced_p))
8711 return error_mark_node;
8712 /* Fall through. */
8713
8714 case FUNCTION_TYPE:
8715 /* Check the return type. */
8716 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8717 pfd->include_nondeduced_p))
8718 return error_mark_node;
8719
8720 /* Check the parameter types. Since default arguments are not
8721 instantiated until they are needed, the TYPE_ARG_TYPES may
8722 contain expressions that involve template parameters. But,
8723 no-one should be looking at them yet. And, once they're
8724 instantiated, they don't contain template parameters, so
8725 there's no point in looking at them then, either. */
8726 {
8727 tree parm;
8728
8729 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8730 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8731 pfd->visited, pfd->include_nondeduced_p))
8732 return error_mark_node;
8733
8734 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8735 want walk_tree walking into them itself. */
8736 *walk_subtrees = 0;
8737 }
8738 break;
8739
8740 case TYPEOF_TYPE:
8741 case UNDERLYING_TYPE:
8742 if (pfd->include_nondeduced_p
8743 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8744 pfd->visited,
8745 pfd->include_nondeduced_p))
8746 return error_mark_node;
8747 break;
8748
8749 case FUNCTION_DECL:
8750 case VAR_DECL:
8751 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8752 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8753 pfd->visited, pfd->include_nondeduced_p))
8754 return error_mark_node;
8755 /* Fall through. */
8756
8757 case PARM_DECL:
8758 case CONST_DECL:
8759 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8760 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8761 pfd->visited, pfd->include_nondeduced_p))
8762 return error_mark_node;
8763 if (DECL_CONTEXT (t)
8764 && pfd->include_nondeduced_p
8765 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8766 pfd->visited, pfd->include_nondeduced_p))
8767 return error_mark_node;
8768 break;
8769
8770 case BOUND_TEMPLATE_TEMPLATE_PARM:
8771 /* Record template parameters such as `T' inside `TT<T>'. */
8772 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8773 pfd->include_nondeduced_p))
8774 return error_mark_node;
8775 /* Fall through. */
8776
8777 case TEMPLATE_TEMPLATE_PARM:
8778 case TEMPLATE_TYPE_PARM:
8779 case TEMPLATE_PARM_INDEX:
8780 if (fn && (*fn)(t, data))
8781 return error_mark_node;
8782 else if (!fn)
8783 return error_mark_node;
8784 break;
8785
8786 case TEMPLATE_DECL:
8787 /* A template template parameter is encountered. */
8788 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8789 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8790 pfd->include_nondeduced_p))
8791 return error_mark_node;
8792
8793 /* Already substituted template template parameter */
8794 *walk_subtrees = 0;
8795 break;
8796
8797 case TYPENAME_TYPE:
8798 if (!fn
8799 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8800 data, pfd->visited,
8801 pfd->include_nondeduced_p))
8802 return error_mark_node;
8803 break;
8804
8805 case CONSTRUCTOR:
8806 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8807 && pfd->include_nondeduced_p
8808 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8809 (TREE_TYPE (t)), fn, data,
8810 pfd->visited, pfd->include_nondeduced_p))
8811 return error_mark_node;
8812 break;
8813
8814 case INDIRECT_REF:
8815 case COMPONENT_REF:
8816 /* If there's no type, then this thing must be some expression
8817 involving template parameters. */
8818 if (!fn && !TREE_TYPE (t))
8819 return error_mark_node;
8820 break;
8821
8822 case MODOP_EXPR:
8823 case CAST_EXPR:
8824 case IMPLICIT_CONV_EXPR:
8825 case REINTERPRET_CAST_EXPR:
8826 case CONST_CAST_EXPR:
8827 case STATIC_CAST_EXPR:
8828 case DYNAMIC_CAST_EXPR:
8829 case ARROW_EXPR:
8830 case DOTSTAR_EXPR:
8831 case TYPEID_EXPR:
8832 case PSEUDO_DTOR_EXPR:
8833 if (!fn)
8834 return error_mark_node;
8835 break;
8836
8837 default:
8838 break;
8839 }
8840
8841 /* We didn't find any template parameters we liked. */
8842 return NULL_TREE;
8843 }
8844
8845 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8846 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8847 call FN with the parameter and the DATA.
8848 If FN returns nonzero, the iteration is terminated, and
8849 for_each_template_parm returns 1. Otherwise, the iteration
8850 continues. If FN never returns a nonzero value, the value
8851 returned by for_each_template_parm is 0. If FN is NULL, it is
8852 considered to be the function which always returns 1.
8853
8854 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8855 parameters that occur in non-deduced contexts. When false, only
8856 visits those template parameters that can be deduced. */
8857
8858 static int
8859 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8860 hash_set<tree> *visited,
8861 bool include_nondeduced_p)
8862 {
8863 struct pair_fn_data pfd;
8864 int result;
8865
8866 /* Set up. */
8867 pfd.fn = fn;
8868 pfd.data = data;
8869 pfd.include_nondeduced_p = include_nondeduced_p;
8870
8871 /* Walk the tree. (Conceptually, we would like to walk without
8872 duplicates, but for_each_template_parm_r recursively calls
8873 for_each_template_parm, so we would need to reorganize a fair
8874 bit to use walk_tree_without_duplicates, so we keep our own
8875 visited list.) */
8876 if (visited)
8877 pfd.visited = visited;
8878 else
8879 pfd.visited = new hash_set<tree>;
8880 result = cp_walk_tree (&t,
8881 for_each_template_parm_r,
8882 &pfd,
8883 pfd.visited) != NULL_TREE;
8884
8885 /* Clean up. */
8886 if (!visited)
8887 {
8888 delete pfd.visited;
8889 pfd.visited = 0;
8890 }
8891
8892 return result;
8893 }
8894
8895 /* Returns true if T depends on any template parameter. */
8896
8897 int
8898 uses_template_parms (tree t)
8899 {
8900 if (t == NULL_TREE)
8901 return false;
8902
8903 bool dependent_p;
8904 int saved_processing_template_decl;
8905
8906 saved_processing_template_decl = processing_template_decl;
8907 if (!saved_processing_template_decl)
8908 processing_template_decl = 1;
8909 if (TYPE_P (t))
8910 dependent_p = dependent_type_p (t);
8911 else if (TREE_CODE (t) == TREE_VEC)
8912 dependent_p = any_dependent_template_arguments_p (t);
8913 else if (TREE_CODE (t) == TREE_LIST)
8914 dependent_p = (uses_template_parms (TREE_VALUE (t))
8915 || uses_template_parms (TREE_CHAIN (t)));
8916 else if (TREE_CODE (t) == TYPE_DECL)
8917 dependent_p = dependent_type_p (TREE_TYPE (t));
8918 else if (DECL_P (t)
8919 || EXPR_P (t)
8920 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8921 || TREE_CODE (t) == OVERLOAD
8922 || BASELINK_P (t)
8923 || identifier_p (t)
8924 || TREE_CODE (t) == TRAIT_EXPR
8925 || TREE_CODE (t) == CONSTRUCTOR
8926 || CONSTANT_CLASS_P (t))
8927 dependent_p = (type_dependent_expression_p (t)
8928 || value_dependent_expression_p (t));
8929 else
8930 {
8931 gcc_assert (t == error_mark_node);
8932 dependent_p = false;
8933 }
8934
8935 processing_template_decl = saved_processing_template_decl;
8936
8937 return dependent_p;
8938 }
8939
8940 /* Returns true iff current_function_decl is an incompletely instantiated
8941 template. Useful instead of processing_template_decl because the latter
8942 is set to 0 during instantiate_non_dependent_expr. */
8943
8944 bool
8945 in_template_function (void)
8946 {
8947 tree fn = current_function_decl;
8948 bool ret;
8949 ++processing_template_decl;
8950 ret = (fn && DECL_LANG_SPECIFIC (fn)
8951 && DECL_TEMPLATE_INFO (fn)
8952 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8953 --processing_template_decl;
8954 return ret;
8955 }
8956
8957 /* Returns true if T depends on any template parameter with level LEVEL. */
8958
8959 int
8960 uses_template_parms_level (tree t, int level)
8961 {
8962 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8963 /*include_nondeduced_p=*/true);
8964 }
8965
8966 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8967 ill-formed translation unit, i.e. a variable or function that isn't
8968 usable in a constant expression. */
8969
8970 static inline bool
8971 neglectable_inst_p (tree d)
8972 {
8973 return (DECL_P (d)
8974 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8975 : decl_maybe_constant_var_p (d)));
8976 }
8977
8978 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8979 neglectable and instantiated from within an erroneous instantiation. */
8980
8981 static bool
8982 limit_bad_template_recursion (tree decl)
8983 {
8984 struct tinst_level *lev = current_tinst_level;
8985 int errs = errorcount + sorrycount;
8986 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8987 return false;
8988
8989 for (; lev; lev = lev->next)
8990 if (neglectable_inst_p (lev->decl))
8991 break;
8992
8993 return (lev && errs > lev->errors);
8994 }
8995
8996 static int tinst_depth;
8997 extern int max_tinst_depth;
8998 int depth_reached;
8999
9000 static GTY(()) struct tinst_level *last_error_tinst_level;
9001
9002 /* We're starting to instantiate D; record the template instantiation context
9003 for diagnostics and to restore it later. */
9004
9005 bool
9006 push_tinst_level (tree d)
9007 {
9008 return push_tinst_level_loc (d, input_location);
9009 }
9010
9011 /* We're starting to instantiate D; record the template instantiation context
9012 at LOC for diagnostics and to restore it later. */
9013
9014 bool
9015 push_tinst_level_loc (tree d, location_t loc)
9016 {
9017 struct tinst_level *new_level;
9018
9019 if (tinst_depth >= max_tinst_depth)
9020 {
9021 fatal_error (input_location,
9022 "template instantiation depth exceeds maximum of %d"
9023 " (use -ftemplate-depth= to increase the maximum)",
9024 max_tinst_depth);
9025 return false;
9026 }
9027
9028 /* If the current instantiation caused problems, don't let it instantiate
9029 anything else. Do allow deduction substitution and decls usable in
9030 constant expressions. */
9031 if (limit_bad_template_recursion (d))
9032 return false;
9033
9034 new_level = ggc_alloc<tinst_level> ();
9035 new_level->decl = d;
9036 new_level->locus = loc;
9037 new_level->errors = errorcount+sorrycount;
9038 new_level->in_system_header_p = in_system_header_at (input_location);
9039 new_level->next = current_tinst_level;
9040 current_tinst_level = new_level;
9041
9042 ++tinst_depth;
9043 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9044 depth_reached = tinst_depth;
9045
9046 return true;
9047 }
9048
9049 /* We're done instantiating this template; return to the instantiation
9050 context. */
9051
9052 void
9053 pop_tinst_level (void)
9054 {
9055 /* Restore the filename and line number stashed away when we started
9056 this instantiation. */
9057 input_location = current_tinst_level->locus;
9058 current_tinst_level = current_tinst_level->next;
9059 --tinst_depth;
9060 }
9061
9062 /* We're instantiating a deferred template; restore the template
9063 instantiation context in which the instantiation was requested, which
9064 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9065
9066 static tree
9067 reopen_tinst_level (struct tinst_level *level)
9068 {
9069 struct tinst_level *t;
9070
9071 tinst_depth = 0;
9072 for (t = level; t; t = t->next)
9073 ++tinst_depth;
9074
9075 current_tinst_level = level;
9076 pop_tinst_level ();
9077 if (current_tinst_level)
9078 current_tinst_level->errors = errorcount+sorrycount;
9079 return level->decl;
9080 }
9081
9082 /* Returns the TINST_LEVEL which gives the original instantiation
9083 context. */
9084
9085 struct tinst_level *
9086 outermost_tinst_level (void)
9087 {
9088 struct tinst_level *level = current_tinst_level;
9089 if (level)
9090 while (level->next)
9091 level = level->next;
9092 return level;
9093 }
9094
9095 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9096 vector of template arguments, as for tsubst.
9097
9098 Returns an appropriate tsubst'd friend declaration. */
9099
9100 static tree
9101 tsubst_friend_function (tree decl, tree args)
9102 {
9103 tree new_friend;
9104
9105 if (TREE_CODE (decl) == FUNCTION_DECL
9106 && DECL_TEMPLATE_INSTANTIATION (decl)
9107 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9108 /* This was a friend declared with an explicit template
9109 argument list, e.g.:
9110
9111 friend void f<>(T);
9112
9113 to indicate that f was a template instantiation, not a new
9114 function declaration. Now, we have to figure out what
9115 instantiation of what template. */
9116 {
9117 tree template_id, arglist, fns;
9118 tree new_args;
9119 tree tmpl;
9120 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9121
9122 /* Friend functions are looked up in the containing namespace scope.
9123 We must enter that scope, to avoid finding member functions of the
9124 current class with same name. */
9125 push_nested_namespace (ns);
9126 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9127 tf_warning_or_error, NULL_TREE,
9128 /*integral_constant_expression_p=*/false);
9129 pop_nested_namespace (ns);
9130 arglist = tsubst (DECL_TI_ARGS (decl), args,
9131 tf_warning_or_error, NULL_TREE);
9132 template_id = lookup_template_function (fns, arglist);
9133
9134 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9135 tmpl = determine_specialization (template_id, new_friend,
9136 &new_args,
9137 /*need_member_template=*/0,
9138 TREE_VEC_LENGTH (args),
9139 tsk_none);
9140 return instantiate_template (tmpl, new_args, tf_error);
9141 }
9142
9143 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9144
9145 /* The NEW_FRIEND will look like an instantiation, to the
9146 compiler, but is not an instantiation from the point of view of
9147 the language. For example, we might have had:
9148
9149 template <class T> struct S {
9150 template <class U> friend void f(T, U);
9151 };
9152
9153 Then, in S<int>, template <class U> void f(int, U) is not an
9154 instantiation of anything. */
9155 if (new_friend == error_mark_node)
9156 return error_mark_node;
9157
9158 DECL_USE_TEMPLATE (new_friend) = 0;
9159 if (TREE_CODE (decl) == TEMPLATE_DECL)
9160 {
9161 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9162 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9163 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9164 }
9165
9166 /* The mangled name for the NEW_FRIEND is incorrect. The function
9167 is not a template instantiation and should not be mangled like
9168 one. Therefore, we forget the mangling here; we'll recompute it
9169 later if we need it. */
9170 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9171 {
9172 SET_DECL_RTL (new_friend, NULL);
9173 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9174 }
9175
9176 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9177 {
9178 tree old_decl;
9179 tree new_friend_template_info;
9180 tree new_friend_result_template_info;
9181 tree ns;
9182 int new_friend_is_defn;
9183
9184 /* We must save some information from NEW_FRIEND before calling
9185 duplicate decls since that function will free NEW_FRIEND if
9186 possible. */
9187 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9188 new_friend_is_defn =
9189 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9190 (template_for_substitution (new_friend)))
9191 != NULL_TREE);
9192 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9193 {
9194 /* This declaration is a `primary' template. */
9195 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9196
9197 new_friend_result_template_info
9198 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9199 }
9200 else
9201 new_friend_result_template_info = NULL_TREE;
9202
9203 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9204 if (new_friend_is_defn)
9205 DECL_INITIAL (new_friend) = error_mark_node;
9206
9207 /* Inside pushdecl_namespace_level, we will push into the
9208 current namespace. However, the friend function should go
9209 into the namespace of the template. */
9210 ns = decl_namespace_context (new_friend);
9211 push_nested_namespace (ns);
9212 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9213 pop_nested_namespace (ns);
9214
9215 if (old_decl == error_mark_node)
9216 return error_mark_node;
9217
9218 if (old_decl != new_friend)
9219 {
9220 /* This new friend declaration matched an existing
9221 declaration. For example, given:
9222
9223 template <class T> void f(T);
9224 template <class U> class C {
9225 template <class T> friend void f(T) {}
9226 };
9227
9228 the friend declaration actually provides the definition
9229 of `f', once C has been instantiated for some type. So,
9230 old_decl will be the out-of-class template declaration,
9231 while new_friend is the in-class definition.
9232
9233 But, if `f' was called before this point, the
9234 instantiation of `f' will have DECL_TI_ARGS corresponding
9235 to `T' but not to `U', references to which might appear
9236 in the definition of `f'. Previously, the most general
9237 template for an instantiation of `f' was the out-of-class
9238 version; now it is the in-class version. Therefore, we
9239 run through all specialization of `f', adding to their
9240 DECL_TI_ARGS appropriately. In particular, they need a
9241 new set of outer arguments, corresponding to the
9242 arguments for this class instantiation.
9243
9244 The same situation can arise with something like this:
9245
9246 friend void f(int);
9247 template <class T> class C {
9248 friend void f(T) {}
9249 };
9250
9251 when `C<int>' is instantiated. Now, `f(int)' is defined
9252 in the class. */
9253
9254 if (!new_friend_is_defn)
9255 /* On the other hand, if the in-class declaration does
9256 *not* provide a definition, then we don't want to alter
9257 existing definitions. We can just leave everything
9258 alone. */
9259 ;
9260 else
9261 {
9262 tree new_template = TI_TEMPLATE (new_friend_template_info);
9263 tree new_args = TI_ARGS (new_friend_template_info);
9264
9265 /* Overwrite whatever template info was there before, if
9266 any, with the new template information pertaining to
9267 the declaration. */
9268 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9269
9270 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9271 {
9272 /* We should have called reregister_specialization in
9273 duplicate_decls. */
9274 gcc_assert (retrieve_specialization (new_template,
9275 new_args, 0)
9276 == old_decl);
9277
9278 /* Instantiate it if the global has already been used. */
9279 if (DECL_ODR_USED (old_decl))
9280 instantiate_decl (old_decl, /*defer_ok=*/true,
9281 /*expl_inst_class_mem_p=*/false);
9282 }
9283 else
9284 {
9285 tree t;
9286
9287 /* Indicate that the old function template is a partial
9288 instantiation. */
9289 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9290 = new_friend_result_template_info;
9291
9292 gcc_assert (new_template
9293 == most_general_template (new_template));
9294 gcc_assert (new_template != old_decl);
9295
9296 /* Reassign any specializations already in the hash table
9297 to the new more general template, and add the
9298 additional template args. */
9299 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9300 t != NULL_TREE;
9301 t = TREE_CHAIN (t))
9302 {
9303 tree spec = TREE_VALUE (t);
9304 spec_entry elt;
9305
9306 elt.tmpl = old_decl;
9307 elt.args = DECL_TI_ARGS (spec);
9308 elt.spec = NULL_TREE;
9309
9310 decl_specializations->remove_elt (&elt);
9311
9312 DECL_TI_ARGS (spec)
9313 = add_outermost_template_args (new_args,
9314 DECL_TI_ARGS (spec));
9315
9316 register_specialization
9317 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9318
9319 }
9320 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9321 }
9322 }
9323
9324 /* The information from NEW_FRIEND has been merged into OLD_DECL
9325 by duplicate_decls. */
9326 new_friend = old_decl;
9327 }
9328 }
9329 else
9330 {
9331 tree context = DECL_CONTEXT (new_friend);
9332 bool dependent_p;
9333
9334 /* In the code
9335 template <class T> class C {
9336 template <class U> friend void C1<U>::f (); // case 1
9337 friend void C2<T>::f (); // case 2
9338 };
9339 we only need to make sure CONTEXT is a complete type for
9340 case 2. To distinguish between the two cases, we note that
9341 CONTEXT of case 1 remains dependent type after tsubst while
9342 this isn't true for case 2. */
9343 ++processing_template_decl;
9344 dependent_p = dependent_type_p (context);
9345 --processing_template_decl;
9346
9347 if (!dependent_p
9348 && !complete_type_or_else (context, NULL_TREE))
9349 return error_mark_node;
9350
9351 if (COMPLETE_TYPE_P (context))
9352 {
9353 tree fn = new_friend;
9354 /* do_friend adds the TEMPLATE_DECL for any member friend
9355 template even if it isn't a member template, i.e.
9356 template <class T> friend A<T>::f();
9357 Look through it in that case. */
9358 if (TREE_CODE (fn) == TEMPLATE_DECL
9359 && !PRIMARY_TEMPLATE_P (fn))
9360 fn = DECL_TEMPLATE_RESULT (fn);
9361 /* Check to see that the declaration is really present, and,
9362 possibly obtain an improved declaration. */
9363 fn = check_classfn (context, fn, NULL_TREE);
9364
9365 if (fn)
9366 new_friend = fn;
9367 }
9368 }
9369
9370 return new_friend;
9371 }
9372
9373 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9374 template arguments, as for tsubst.
9375
9376 Returns an appropriate tsubst'd friend type or error_mark_node on
9377 failure. */
9378
9379 static tree
9380 tsubst_friend_class (tree friend_tmpl, tree args)
9381 {
9382 tree friend_type;
9383 tree tmpl;
9384 tree context;
9385
9386 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9387 {
9388 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9389 return TREE_TYPE (t);
9390 }
9391
9392 context = CP_DECL_CONTEXT (friend_tmpl);
9393
9394 if (context != global_namespace)
9395 {
9396 if (TREE_CODE (context) == NAMESPACE_DECL)
9397 push_nested_namespace (context);
9398 else
9399 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9400 }
9401
9402 /* Look for a class template declaration. We look for hidden names
9403 because two friend declarations of the same template are the
9404 same. For example, in:
9405
9406 struct A {
9407 template <typename> friend class F;
9408 };
9409 template <typename> struct B {
9410 template <typename> friend class F;
9411 };
9412
9413 both F templates are the same. */
9414 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9415 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9416
9417 /* But, if we don't find one, it might be because we're in a
9418 situation like this:
9419
9420 template <class T>
9421 struct S {
9422 template <class U>
9423 friend struct S;
9424 };
9425
9426 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9427 for `S<int>', not the TEMPLATE_DECL. */
9428 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9429 {
9430 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9431 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9432 }
9433
9434 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9435 {
9436 /* The friend template has already been declared. Just
9437 check to see that the declarations match, and install any new
9438 default parameters. We must tsubst the default parameters,
9439 of course. We only need the innermost template parameters
9440 because that is all that redeclare_class_template will look
9441 at. */
9442 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9443 > TMPL_ARGS_DEPTH (args))
9444 {
9445 tree parms;
9446 location_t saved_input_location;
9447 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9448 args, tf_warning_or_error);
9449
9450 saved_input_location = input_location;
9451 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9452 tree cons = get_constraints (tmpl);
9453 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9454 input_location = saved_input_location;
9455
9456 }
9457
9458 friend_type = TREE_TYPE (tmpl);
9459 }
9460 else
9461 {
9462 /* The friend template has not already been declared. In this
9463 case, the instantiation of the template class will cause the
9464 injection of this template into the global scope. */
9465 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9466 if (tmpl == error_mark_node)
9467 return error_mark_node;
9468
9469 /* The new TMPL is not an instantiation of anything, so we
9470 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9471 the new type because that is supposed to be the corresponding
9472 template decl, i.e., TMPL. */
9473 DECL_USE_TEMPLATE (tmpl) = 0;
9474 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9475 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9476 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9477 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9478
9479 /* Inject this template into the global scope. */
9480 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9481 }
9482
9483 if (context != global_namespace)
9484 {
9485 if (TREE_CODE (context) == NAMESPACE_DECL)
9486 pop_nested_namespace (context);
9487 else
9488 pop_nested_class ();
9489 }
9490
9491 return friend_type;
9492 }
9493
9494 /* Returns zero if TYPE cannot be completed later due to circularity.
9495 Otherwise returns one. */
9496
9497 static int
9498 can_complete_type_without_circularity (tree type)
9499 {
9500 if (type == NULL_TREE || type == error_mark_node)
9501 return 0;
9502 else if (COMPLETE_TYPE_P (type))
9503 return 1;
9504 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
9505 return can_complete_type_without_circularity (TREE_TYPE (type));
9506 else if (CLASS_TYPE_P (type)
9507 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9508 return 0;
9509 else
9510 return 1;
9511 }
9512
9513 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
9514
9515 /* Apply any attributes which had to be deferred until instantiation
9516 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9517 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9518
9519 static void
9520 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9521 tree args, tsubst_flags_t complain, tree in_decl)
9522 {
9523 tree last_dep = NULL_TREE;
9524 tree t;
9525 tree *p;
9526
9527 for (t = attributes; t; t = TREE_CHAIN (t))
9528 if (ATTR_IS_DEPENDENT (t))
9529 {
9530 last_dep = t;
9531 attributes = copy_list (attributes);
9532 break;
9533 }
9534
9535 if (DECL_P (*decl_p))
9536 {
9537 if (TREE_TYPE (*decl_p) == error_mark_node)
9538 return;
9539 p = &DECL_ATTRIBUTES (*decl_p);
9540 }
9541 else
9542 p = &TYPE_ATTRIBUTES (*decl_p);
9543
9544 if (last_dep)
9545 {
9546 tree late_attrs = NULL_TREE;
9547 tree *q = &late_attrs;
9548
9549 for (*p = attributes; *p; )
9550 {
9551 t = *p;
9552 if (ATTR_IS_DEPENDENT (t))
9553 {
9554 *p = TREE_CHAIN (t);
9555 TREE_CHAIN (t) = NULL_TREE;
9556 if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9557 && is_attribute_p ("omp declare simd",
9558 get_attribute_name (t))
9559 && TREE_VALUE (t))
9560 {
9561 tree clauses = TREE_VALUE (TREE_VALUE (t));
9562 clauses = tsubst_omp_clauses (clauses, true, args,
9563 complain, in_decl);
9564 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9565 clauses = finish_omp_clauses (clauses);
9566 tree parms = DECL_ARGUMENTS (*decl_p);
9567 clauses
9568 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9569 if (clauses)
9570 TREE_VALUE (TREE_VALUE (t)) = clauses;
9571 else
9572 TREE_VALUE (t) = NULL_TREE;
9573 }
9574 /* If the first attribute argument is an identifier, don't
9575 pass it through tsubst. Attributes like mode, format,
9576 cleanup and several target specific attributes expect it
9577 unmodified. */
9578 else if (attribute_takes_identifier_p (get_attribute_name (t))
9579 && TREE_VALUE (t))
9580 {
9581 tree chain
9582 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9583 in_decl,
9584 /*integral_constant_expression_p=*/false);
9585 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9586 TREE_VALUE (t)
9587 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9588 chain);
9589 }
9590 else if (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))
9591 {
9592 /* An attribute pack expansion. */
9593 tree purp = TREE_PURPOSE (t);
9594 tree pack = (tsubst_pack_expansion
9595 (TREE_VALUE (t), args, complain, in_decl));
9596 int len = TREE_VEC_LENGTH (pack);
9597 for (int i = 0; i < len; ++i)
9598 {
9599 tree elt = TREE_VEC_ELT (pack, i);
9600 *q = build_tree_list (purp, elt);
9601 q = &TREE_CHAIN (*q);
9602 }
9603 continue;
9604 }
9605 else
9606 TREE_VALUE (t)
9607 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9608 /*integral_constant_expression_p=*/false);
9609 *q = t;
9610 q = &TREE_CHAIN (t);
9611 }
9612 else
9613 p = &TREE_CHAIN (t);
9614 }
9615
9616 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9617 }
9618 }
9619
9620 /* Perform (or defer) access check for typedefs that were referenced
9621 from within the template TMPL code.
9622 This is a subroutine of instantiate_decl and instantiate_class_template.
9623 TMPL is the template to consider and TARGS is the list of arguments of
9624 that template. */
9625
9626 static void
9627 perform_typedefs_access_check (tree tmpl, tree targs)
9628 {
9629 location_t saved_location;
9630 unsigned i;
9631 qualified_typedef_usage_t *iter;
9632
9633 if (!tmpl
9634 || (!CLASS_TYPE_P (tmpl)
9635 && TREE_CODE (tmpl) != FUNCTION_DECL))
9636 return;
9637
9638 saved_location = input_location;
9639 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9640 {
9641 tree type_decl = iter->typedef_decl;
9642 tree type_scope = iter->context;
9643
9644 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9645 continue;
9646
9647 if (uses_template_parms (type_decl))
9648 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9649 if (uses_template_parms (type_scope))
9650 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9651
9652 /* Make access check error messages point to the location
9653 of the use of the typedef. */
9654 input_location = iter->locus;
9655 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9656 type_decl, type_decl,
9657 tf_warning_or_error);
9658 }
9659 input_location = saved_location;
9660 }
9661
9662 static tree
9663 instantiate_class_template_1 (tree type)
9664 {
9665 tree templ, args, pattern, t, member;
9666 tree typedecl;
9667 tree pbinfo;
9668 tree base_list;
9669 unsigned int saved_maximum_field_alignment;
9670 tree fn_context;
9671
9672 if (type == error_mark_node)
9673 return error_mark_node;
9674
9675 if (COMPLETE_OR_OPEN_TYPE_P (type)
9676 || uses_template_parms (type))
9677 return type;
9678
9679 /* Figure out which template is being instantiated. */
9680 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9681 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9682
9683 /* Determine what specialization of the original template to
9684 instantiate. */
9685 t = most_specialized_partial_spec (type, tf_warning_or_error);
9686 if (t == error_mark_node)
9687 {
9688 TYPE_BEING_DEFINED (type) = 1;
9689 return error_mark_node;
9690 }
9691 else if (t)
9692 {
9693 /* This TYPE is actually an instantiation of a partial
9694 specialization. We replace the innermost set of ARGS with
9695 the arguments appropriate for substitution. For example,
9696 given:
9697
9698 template <class T> struct S {};
9699 template <class T> struct S<T*> {};
9700
9701 and supposing that we are instantiating S<int*>, ARGS will
9702 presently be {int*} -- but we need {int}. */
9703 pattern = TREE_TYPE (t);
9704 args = TREE_PURPOSE (t);
9705 }
9706 else
9707 {
9708 pattern = TREE_TYPE (templ);
9709 args = CLASSTYPE_TI_ARGS (type);
9710 }
9711
9712 /* If the template we're instantiating is incomplete, then clearly
9713 there's nothing we can do. */
9714 if (!COMPLETE_TYPE_P (pattern))
9715 return type;
9716
9717 /* If we've recursively instantiated too many templates, stop. */
9718 if (! push_tinst_level (type))
9719 return type;
9720
9721 /* Now we're really doing the instantiation. Mark the type as in
9722 the process of being defined. */
9723 TYPE_BEING_DEFINED (type) = 1;
9724
9725 /* We may be in the middle of deferred access check. Disable
9726 it now. */
9727 push_deferring_access_checks (dk_no_deferred);
9728
9729 int saved_unevaluated_operand = cp_unevaluated_operand;
9730 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9731
9732 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9733 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9734 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9735 fn_context = error_mark_node;
9736 if (!fn_context)
9737 push_to_top_level ();
9738 else
9739 {
9740 cp_unevaluated_operand = 0;
9741 c_inhibit_evaluation_warnings = 0;
9742 }
9743 /* Use #pragma pack from the template context. */
9744 saved_maximum_field_alignment = maximum_field_alignment;
9745 maximum_field_alignment = TYPE_PRECISION (pattern);
9746
9747 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9748
9749 /* Set the input location to the most specialized template definition.
9750 This is needed if tsubsting causes an error. */
9751 typedecl = TYPE_MAIN_DECL (pattern);
9752 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9753 DECL_SOURCE_LOCATION (typedecl);
9754
9755 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9756 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9757 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9758 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9759 if (ANON_AGGR_TYPE_P (pattern))
9760 SET_ANON_AGGR_TYPE_P (type);
9761 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9762 {
9763 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9764 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9765 /* Adjust visibility for template arguments. */
9766 determine_visibility (TYPE_MAIN_DECL (type));
9767 }
9768 if (CLASS_TYPE_P (type))
9769 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9770
9771 pbinfo = TYPE_BINFO (pattern);
9772
9773 /* We should never instantiate a nested class before its enclosing
9774 class; we need to look up the nested class by name before we can
9775 instantiate it, and that lookup should instantiate the enclosing
9776 class. */
9777 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9778 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9779
9780 base_list = NULL_TREE;
9781 if (BINFO_N_BASE_BINFOS (pbinfo))
9782 {
9783 tree pbase_binfo;
9784 tree pushed_scope;
9785 int i;
9786
9787 /* We must enter the scope containing the type, as that is where
9788 the accessibility of types named in dependent bases are
9789 looked up from. */
9790 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9791
9792 /* Substitute into each of the bases to determine the actual
9793 basetypes. */
9794 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9795 {
9796 tree base;
9797 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9798 tree expanded_bases = NULL_TREE;
9799 int idx, len = 1;
9800
9801 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9802 {
9803 expanded_bases =
9804 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9805 args, tf_error, NULL_TREE);
9806 if (expanded_bases == error_mark_node)
9807 continue;
9808
9809 len = TREE_VEC_LENGTH (expanded_bases);
9810 }
9811
9812 for (idx = 0; idx < len; idx++)
9813 {
9814 if (expanded_bases)
9815 /* Extract the already-expanded base class. */
9816 base = TREE_VEC_ELT (expanded_bases, idx);
9817 else
9818 /* Substitute to figure out the base class. */
9819 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9820 NULL_TREE);
9821
9822 if (base == error_mark_node)
9823 continue;
9824
9825 base_list = tree_cons (access, base, base_list);
9826 if (BINFO_VIRTUAL_P (pbase_binfo))
9827 TREE_TYPE (base_list) = integer_type_node;
9828 }
9829 }
9830
9831 /* The list is now in reverse order; correct that. */
9832 base_list = nreverse (base_list);
9833
9834 if (pushed_scope)
9835 pop_scope (pushed_scope);
9836 }
9837 /* Now call xref_basetypes to set up all the base-class
9838 information. */
9839 xref_basetypes (type, base_list);
9840
9841 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9842 (int) ATTR_FLAG_TYPE_IN_PLACE,
9843 args, tf_error, NULL_TREE);
9844 fixup_attribute_variants (type);
9845
9846 /* Now that our base classes are set up, enter the scope of the
9847 class, so that name lookups into base classes, etc. will work
9848 correctly. This is precisely analogous to what we do in
9849 begin_class_definition when defining an ordinary non-template
9850 class, except we also need to push the enclosing classes. */
9851 push_nested_class (type);
9852
9853 /* Now members are processed in the order of declaration. */
9854 for (member = CLASSTYPE_DECL_LIST (pattern);
9855 member; member = TREE_CHAIN (member))
9856 {
9857 tree t = TREE_VALUE (member);
9858
9859 if (TREE_PURPOSE (member))
9860 {
9861 if (TYPE_P (t))
9862 {
9863 /* Build new CLASSTYPE_NESTED_UTDS. */
9864
9865 tree newtag;
9866 bool class_template_p;
9867
9868 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9869 && TYPE_LANG_SPECIFIC (t)
9870 && CLASSTYPE_IS_TEMPLATE (t));
9871 /* If the member is a class template, then -- even after
9872 substitution -- there may be dependent types in the
9873 template argument list for the class. We increment
9874 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9875 that function will assume that no types are dependent
9876 when outside of a template. */
9877 if (class_template_p)
9878 ++processing_template_decl;
9879 newtag = tsubst (t, args, tf_error, NULL_TREE);
9880 if (class_template_p)
9881 --processing_template_decl;
9882 if (newtag == error_mark_node)
9883 continue;
9884
9885 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9886 {
9887 tree name = TYPE_IDENTIFIER (t);
9888
9889 if (class_template_p)
9890 /* Unfortunately, lookup_template_class sets
9891 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9892 instantiation (i.e., for the type of a member
9893 template class nested within a template class.)
9894 This behavior is required for
9895 maybe_process_partial_specialization to work
9896 correctly, but is not accurate in this case;
9897 the TAG is not an instantiation of anything.
9898 (The corresponding TEMPLATE_DECL is an
9899 instantiation, but the TYPE is not.) */
9900 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9901
9902 /* Now, we call pushtag to put this NEWTAG into the scope of
9903 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9904 pushtag calling push_template_decl. We don't have to do
9905 this for enums because it will already have been done in
9906 tsubst_enum. */
9907 if (name)
9908 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9909 pushtag (name, newtag, /*tag_scope=*/ts_current);
9910 }
9911 }
9912 else if (DECL_DECLARES_FUNCTION_P (t))
9913 {
9914 /* Build new TYPE_METHODS. */
9915 tree r;
9916
9917 if (TREE_CODE (t) == TEMPLATE_DECL)
9918 ++processing_template_decl;
9919 r = tsubst (t, args, tf_error, NULL_TREE);
9920 if (TREE_CODE (t) == TEMPLATE_DECL)
9921 --processing_template_decl;
9922 set_current_access_from_decl (r);
9923 finish_member_declaration (r);
9924 /* Instantiate members marked with attribute used. */
9925 if (r != error_mark_node && DECL_PRESERVE_P (r))
9926 mark_used (r);
9927 if (TREE_CODE (r) == FUNCTION_DECL
9928 && DECL_OMP_DECLARE_REDUCTION_P (r))
9929 cp_check_omp_declare_reduction (r);
9930 }
9931 else if (DECL_CLASS_TEMPLATE_P (t)
9932 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9933 /* A closure type for a lambda in a default argument for a
9934 member template. Ignore it; it will be instantiated with
9935 the default argument. */;
9936 else
9937 {
9938 /* Build new TYPE_FIELDS. */
9939 if (TREE_CODE (t) == STATIC_ASSERT)
9940 {
9941 tree condition;
9942
9943 ++c_inhibit_evaluation_warnings;
9944 condition =
9945 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9946 tf_warning_or_error, NULL_TREE,
9947 /*integral_constant_expression_p=*/true);
9948 --c_inhibit_evaluation_warnings;
9949
9950 finish_static_assert (condition,
9951 STATIC_ASSERT_MESSAGE (t),
9952 STATIC_ASSERT_SOURCE_LOCATION (t),
9953 /*member_p=*/true);
9954 }
9955 else if (TREE_CODE (t) != CONST_DECL)
9956 {
9957 tree r;
9958 tree vec = NULL_TREE;
9959 int len = 1;
9960
9961 /* The file and line for this declaration, to
9962 assist in error message reporting. Since we
9963 called push_tinst_level above, we don't need to
9964 restore these. */
9965 input_location = DECL_SOURCE_LOCATION (t);
9966
9967 if (TREE_CODE (t) == TEMPLATE_DECL)
9968 ++processing_template_decl;
9969 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9970 if (TREE_CODE (t) == TEMPLATE_DECL)
9971 --processing_template_decl;
9972
9973 if (TREE_CODE (r) == TREE_VEC)
9974 {
9975 /* A capture pack became multiple fields. */
9976 vec = r;
9977 len = TREE_VEC_LENGTH (vec);
9978 }
9979
9980 for (int i = 0; i < len; ++i)
9981 {
9982 if (vec)
9983 r = TREE_VEC_ELT (vec, i);
9984 if (VAR_P (r))
9985 {
9986 /* In [temp.inst]:
9987
9988 [t]he initialization (and any associated
9989 side-effects) of a static data member does
9990 not occur unless the static data member is
9991 itself used in a way that requires the
9992 definition of the static data member to
9993 exist.
9994
9995 Therefore, we do not substitute into the
9996 initialized for the static data member here. */
9997 finish_static_data_member_decl
9998 (r,
9999 /*init=*/NULL_TREE,
10000 /*init_const_expr_p=*/false,
10001 /*asmspec_tree=*/NULL_TREE,
10002 /*flags=*/0);
10003 /* Instantiate members marked with attribute used. */
10004 if (r != error_mark_node && DECL_PRESERVE_P (r))
10005 mark_used (r);
10006 }
10007 else if (TREE_CODE (r) == FIELD_DECL)
10008 {
10009 /* Determine whether R has a valid type and can be
10010 completed later. If R is invalid, then its type
10011 is replaced by error_mark_node. */
10012 tree rtype = TREE_TYPE (r);
10013 if (can_complete_type_without_circularity (rtype))
10014 complete_type (rtype);
10015
10016 if (!COMPLETE_TYPE_P (rtype))
10017 {
10018 cxx_incomplete_type_error (r, rtype);
10019 TREE_TYPE (r) = error_mark_node;
10020 }
10021 }
10022
10023 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10024 such a thing will already have been added to the field
10025 list by tsubst_enum in finish_member_declaration in the
10026 CLASSTYPE_NESTED_UTDS case above. */
10027 if (!(TREE_CODE (r) == TYPE_DECL
10028 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10029 && DECL_ARTIFICIAL (r)))
10030 {
10031 set_current_access_from_decl (r);
10032 finish_member_declaration (r);
10033 }
10034 }
10035 }
10036 }
10037 }
10038 else
10039 {
10040 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10041 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10042 {
10043 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10044
10045 tree friend_type = t;
10046 bool adjust_processing_template_decl = false;
10047
10048 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10049 {
10050 /* template <class T> friend class C; */
10051 friend_type = tsubst_friend_class (friend_type, args);
10052 adjust_processing_template_decl = true;
10053 }
10054 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10055 {
10056 /* template <class T> friend class C::D; */
10057 friend_type = tsubst (friend_type, args,
10058 tf_warning_or_error, NULL_TREE);
10059 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10060 friend_type = TREE_TYPE (friend_type);
10061 adjust_processing_template_decl = true;
10062 }
10063 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10064 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10065 {
10066 /* This could be either
10067
10068 friend class T::C;
10069
10070 when dependent_type_p is false or
10071
10072 template <class U> friend class T::C;
10073
10074 otherwise. */
10075 friend_type = tsubst (friend_type, args,
10076 tf_warning_or_error, NULL_TREE);
10077 /* Bump processing_template_decl for correct
10078 dependent_type_p calculation. */
10079 ++processing_template_decl;
10080 if (dependent_type_p (friend_type))
10081 adjust_processing_template_decl = true;
10082 --processing_template_decl;
10083 }
10084 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10085 && hidden_name_p (TYPE_NAME (friend_type)))
10086 {
10087 /* friend class C;
10088
10089 where C hasn't been declared yet. Let's lookup name
10090 from namespace scope directly, bypassing any name that
10091 come from dependent base class. */
10092 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10093
10094 /* The call to xref_tag_from_type does injection for friend
10095 classes. */
10096 push_nested_namespace (ns);
10097 friend_type =
10098 xref_tag_from_type (friend_type, NULL_TREE,
10099 /*tag_scope=*/ts_current);
10100 pop_nested_namespace (ns);
10101 }
10102 else if (uses_template_parms (friend_type))
10103 /* friend class C<T>; */
10104 friend_type = tsubst (friend_type, args,
10105 tf_warning_or_error, NULL_TREE);
10106 /* Otherwise it's
10107
10108 friend class C;
10109
10110 where C is already declared or
10111
10112 friend class C<int>;
10113
10114 We don't have to do anything in these cases. */
10115
10116 if (adjust_processing_template_decl)
10117 /* Trick make_friend_class into realizing that the friend
10118 we're adding is a template, not an ordinary class. It's
10119 important that we use make_friend_class since it will
10120 perform some error-checking and output cross-reference
10121 information. */
10122 ++processing_template_decl;
10123
10124 if (friend_type != error_mark_node)
10125 make_friend_class (type, friend_type, /*complain=*/false);
10126
10127 if (adjust_processing_template_decl)
10128 --processing_template_decl;
10129 }
10130 else
10131 {
10132 /* Build new DECL_FRIENDLIST. */
10133 tree r;
10134
10135 /* The file and line for this declaration, to
10136 assist in error message reporting. Since we
10137 called push_tinst_level above, we don't need to
10138 restore these. */
10139 input_location = DECL_SOURCE_LOCATION (t);
10140
10141 if (TREE_CODE (t) == TEMPLATE_DECL)
10142 {
10143 ++processing_template_decl;
10144 push_deferring_access_checks (dk_no_check);
10145 }
10146
10147 r = tsubst_friend_function (t, args);
10148 add_friend (type, r, /*complain=*/false);
10149 if (TREE_CODE (t) == TEMPLATE_DECL)
10150 {
10151 pop_deferring_access_checks ();
10152 --processing_template_decl;
10153 }
10154 }
10155 }
10156 }
10157
10158 if (fn_context)
10159 {
10160 /* Restore these before substituting into the lambda capture
10161 initializers. */
10162 cp_unevaluated_operand = saved_unevaluated_operand;
10163 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10164 }
10165
10166 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10167 {
10168 tree decl = lambda_function (type);
10169 if (decl)
10170 {
10171 if (!DECL_TEMPLATE_INFO (decl)
10172 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10173 instantiate_decl (decl, false, false);
10174
10175 /* We need to instantiate the capture list from the template
10176 after we've instantiated the closure members, but before we
10177 consider adding the conversion op. Also keep any captures
10178 that may have been added during instantiation of the op(). */
10179 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10180 tree tmpl_cap
10181 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10182 args, tf_warning_or_error, NULL_TREE,
10183 false, false);
10184
10185 LAMBDA_EXPR_CAPTURE_LIST (expr)
10186 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10187
10188 maybe_add_lambda_conv_op (type);
10189 }
10190 else
10191 gcc_assert (errorcount);
10192 }
10193
10194 /* Set the file and line number information to whatever is given for
10195 the class itself. This puts error messages involving generated
10196 implicit functions at a predictable point, and the same point
10197 that would be used for non-template classes. */
10198 input_location = DECL_SOURCE_LOCATION (typedecl);
10199
10200 unreverse_member_declarations (type);
10201 finish_struct_1 (type);
10202 TYPE_BEING_DEFINED (type) = 0;
10203
10204 /* We don't instantiate default arguments for member functions. 14.7.1:
10205
10206 The implicit instantiation of a class template specialization causes
10207 the implicit instantiation of the declarations, but not of the
10208 definitions or default arguments, of the class member functions,
10209 member classes, static data members and member templates.... */
10210
10211 /* Some typedefs referenced from within the template code need to be access
10212 checked at template instantiation time, i.e now. These types were
10213 added to the template at parsing time. Let's get those and perform
10214 the access checks then. */
10215 perform_typedefs_access_check (pattern, args);
10216 perform_deferred_access_checks (tf_warning_or_error);
10217 pop_nested_class ();
10218 maximum_field_alignment = saved_maximum_field_alignment;
10219 if (!fn_context)
10220 pop_from_top_level ();
10221 pop_deferring_access_checks ();
10222 pop_tinst_level ();
10223
10224 /* The vtable for a template class can be emitted in any translation
10225 unit in which the class is instantiated. When there is no key
10226 method, however, finish_struct_1 will already have added TYPE to
10227 the keyed_classes list. */
10228 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10229 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10230
10231 return type;
10232 }
10233
10234 /* Wrapper for instantiate_class_template_1. */
10235
10236 tree
10237 instantiate_class_template (tree type)
10238 {
10239 tree ret;
10240 timevar_push (TV_TEMPLATE_INST);
10241 ret = instantiate_class_template_1 (type);
10242 timevar_pop (TV_TEMPLATE_INST);
10243 return ret;
10244 }
10245
10246 static tree
10247 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10248 {
10249 tree r;
10250
10251 if (!t)
10252 r = t;
10253 else if (TYPE_P (t))
10254 r = tsubst (t, args, complain, in_decl);
10255 else
10256 {
10257 if (!(complain & tf_warning))
10258 ++c_inhibit_evaluation_warnings;
10259 r = tsubst_expr (t, args, complain, in_decl,
10260 /*integral_constant_expression_p=*/true);
10261 if (!(complain & tf_warning))
10262 --c_inhibit_evaluation_warnings;
10263 }
10264 return r;
10265 }
10266
10267 /* Given a function parameter pack TMPL_PARM and some function parameters
10268 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10269 and set *SPEC_P to point at the next point in the list. */
10270
10271 tree
10272 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10273 {
10274 /* Collect all of the extra "packed" parameters into an
10275 argument pack. */
10276 tree parmvec;
10277 tree parmtypevec;
10278 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10279 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10280 tree spec_parm = *spec_p;
10281 int i, len;
10282
10283 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10284 if (tmpl_parm
10285 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10286 break;
10287
10288 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10289 parmvec = make_tree_vec (len);
10290 parmtypevec = make_tree_vec (len);
10291 spec_parm = *spec_p;
10292 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10293 {
10294 TREE_VEC_ELT (parmvec, i) = spec_parm;
10295 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10296 }
10297
10298 /* Build the argument packs. */
10299 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10300 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10301 TREE_TYPE (argpack) = argtypepack;
10302 *spec_p = spec_parm;
10303
10304 return argpack;
10305 }
10306
10307 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10308 NONTYPE_ARGUMENT_PACK. */
10309
10310 static tree
10311 make_fnparm_pack (tree spec_parm)
10312 {
10313 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10314 }
10315
10316 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10317 pack expansion with no extra args, 2 if it has extra args, or 0
10318 if it is not a pack expansion. */
10319
10320 static int
10321 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10322 {
10323 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10324 if (i >= TREE_VEC_LENGTH (vec))
10325 return 0;
10326 tree elt = TREE_VEC_ELT (vec, i);
10327 if (DECL_P (elt))
10328 /* A decl pack is itself an expansion. */
10329 elt = TREE_TYPE (elt);
10330 if (!PACK_EXPANSION_P (elt))
10331 return 0;
10332 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10333 return 2;
10334 return 1;
10335 }
10336
10337
10338 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10339
10340 static tree
10341 make_argument_pack_select (tree arg_pack, unsigned index)
10342 {
10343 tree aps = make_node (ARGUMENT_PACK_SELECT);
10344
10345 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10346 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10347
10348 return aps;
10349 }
10350
10351 /* This is a subroutine of tsubst_pack_expansion.
10352
10353 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10354 mechanism to store the (non complete list of) arguments of the
10355 substitution and return a non substituted pack expansion, in order
10356 to wait for when we have enough arguments to really perform the
10357 substitution. */
10358
10359 static bool
10360 use_pack_expansion_extra_args_p (tree parm_packs,
10361 int arg_pack_len,
10362 bool has_empty_arg)
10363 {
10364 /* If one pack has an expansion and another pack has a normal
10365 argument or if one pack has an empty argument and an another
10366 one hasn't then tsubst_pack_expansion cannot perform the
10367 substitution and need to fall back on the
10368 PACK_EXPANSION_EXTRA mechanism. */
10369 if (parm_packs == NULL_TREE)
10370 return false;
10371 else if (has_empty_arg)
10372 return true;
10373
10374 bool has_expansion_arg = false;
10375 for (int i = 0 ; i < arg_pack_len; ++i)
10376 {
10377 bool has_non_expansion_arg = false;
10378 for (tree parm_pack = parm_packs;
10379 parm_pack;
10380 parm_pack = TREE_CHAIN (parm_pack))
10381 {
10382 tree arg = TREE_VALUE (parm_pack);
10383
10384 int exp = argument_pack_element_is_expansion_p (arg, i);
10385 if (exp == 2)
10386 /* We can't substitute a pack expansion with extra args into
10387 our pattern. */
10388 return true;
10389 else if (exp)
10390 has_expansion_arg = true;
10391 else
10392 has_non_expansion_arg = true;
10393 }
10394
10395 if (has_expansion_arg && has_non_expansion_arg)
10396 return true;
10397 }
10398 return false;
10399 }
10400
10401 /* [temp.variadic]/6 says that:
10402
10403 The instantiation of a pack expansion [...]
10404 produces a list E1,E2, ..., En, where N is the number of elements
10405 in the pack expansion parameters.
10406
10407 This subroutine of tsubst_pack_expansion produces one of these Ei.
10408
10409 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10410 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10411 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10412 INDEX is the index 'i' of the element Ei to produce. ARGS,
10413 COMPLAIN, and IN_DECL are the same parameters as for the
10414 tsubst_pack_expansion function.
10415
10416 The function returns the resulting Ei upon successful completion,
10417 or error_mark_node.
10418
10419 Note that this function possibly modifies the ARGS parameter, so
10420 it's the responsibility of the caller to restore it. */
10421
10422 static tree
10423 gen_elem_of_pack_expansion_instantiation (tree pattern,
10424 tree parm_packs,
10425 unsigned index,
10426 tree args /* This parm gets
10427 modified. */,
10428 tsubst_flags_t complain,
10429 tree in_decl)
10430 {
10431 tree t;
10432 bool ith_elem_is_expansion = false;
10433
10434 /* For each parameter pack, change the substitution of the parameter
10435 pack to the ith argument in its argument pack, then expand the
10436 pattern. */
10437 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10438 {
10439 tree parm = TREE_PURPOSE (pack);
10440 tree arg_pack = TREE_VALUE (pack);
10441 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10442
10443 ith_elem_is_expansion |=
10444 argument_pack_element_is_expansion_p (arg_pack, index);
10445
10446 /* Select the Ith argument from the pack. */
10447 if (TREE_CODE (parm) == PARM_DECL
10448 || TREE_CODE (parm) == FIELD_DECL)
10449 {
10450 if (index == 0)
10451 {
10452 aps = make_argument_pack_select (arg_pack, index);
10453 if (!mark_used (parm, complain) && !(complain & tf_error))
10454 return error_mark_node;
10455 register_local_specialization (aps, parm);
10456 }
10457 else
10458 aps = retrieve_local_specialization (parm);
10459 }
10460 else
10461 {
10462 int idx, level;
10463 template_parm_level_and_index (parm, &level, &idx);
10464
10465 if (index == 0)
10466 {
10467 aps = make_argument_pack_select (arg_pack, index);
10468 /* Update the corresponding argument. */
10469 TMPL_ARG (args, level, idx) = aps;
10470 }
10471 else
10472 /* Re-use the ARGUMENT_PACK_SELECT. */
10473 aps = TMPL_ARG (args, level, idx);
10474 }
10475 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10476 }
10477
10478 /* Substitute into the PATTERN with the (possibly altered)
10479 arguments. */
10480 if (pattern == in_decl)
10481 /* Expanding a fixed parameter pack from
10482 coerce_template_parameter_pack. */
10483 t = tsubst_decl (pattern, args, complain);
10484 else if (pattern == error_mark_node)
10485 t = error_mark_node;
10486 else if (constraint_p (pattern))
10487 {
10488 if (processing_template_decl)
10489 t = tsubst_constraint (pattern, args, complain, in_decl);
10490 else
10491 t = (constraints_satisfied_p (pattern, args)
10492 ? boolean_true_node : boolean_false_node);
10493 }
10494 else if (!TYPE_P (pattern))
10495 t = tsubst_expr (pattern, args, complain, in_decl,
10496 /*integral_constant_expression_p=*/false);
10497 else
10498 t = tsubst (pattern, args, complain, in_decl);
10499
10500 /* If the Ith argument pack element is a pack expansion, then
10501 the Ith element resulting from the substituting is going to
10502 be a pack expansion as well. */
10503 if (ith_elem_is_expansion)
10504 t = make_pack_expansion (t);
10505
10506 return t;
10507 }
10508
10509 /* When the unexpanded parameter pack in a fold expression expands to an empty
10510 sequence, the value of the expression is as follows; the program is
10511 ill-formed if the operator is not listed in this table.
10512
10513 * 1
10514 + 0
10515 & -1
10516 | 0
10517 && true
10518 || false
10519 , void() */
10520
10521 tree
10522 expand_empty_fold (tree t, tsubst_flags_t complain)
10523 {
10524 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
10525 if (!FOLD_EXPR_MODIFY_P (t))
10526 switch (code)
10527 {
10528 case MULT_EXPR:
10529 return integer_one_node;
10530 case PLUS_EXPR:
10531 return integer_zero_node;
10532 case BIT_AND_EXPR:
10533 return integer_minus_one_node;
10534 case BIT_IOR_EXPR:
10535 return integer_zero_node;
10536 case TRUTH_ANDIF_EXPR:
10537 return boolean_true_node;
10538 case TRUTH_ORIF_EXPR:
10539 return boolean_false_node;
10540 case COMPOUND_EXPR:
10541 return void_node;
10542 default:
10543 break;
10544 }
10545
10546 if (complain & tf_error)
10547 error_at (location_of (t),
10548 "fold of empty expansion over %O", code);
10549 return error_mark_node;
10550 }
10551
10552 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10553 form an expression that combines the two terms using the
10554 operator of T. */
10555
10556 static tree
10557 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
10558 {
10559 tree op = FOLD_EXPR_OP (t);
10560 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
10561
10562 // Handle compound assignment operators.
10563 if (FOLD_EXPR_MODIFY_P (t))
10564 return build_x_modify_expr (input_location, left, code, right, complain);
10565
10566 switch (code)
10567 {
10568 case COMPOUND_EXPR:
10569 return build_x_compound_expr (input_location, left, right, complain);
10570 case DOTSTAR_EXPR:
10571 return build_m_component_ref (left, right, complain);
10572 default:
10573 return build_x_binary_op (input_location, code,
10574 left, TREE_CODE (left),
10575 right, TREE_CODE (right),
10576 /*overload=*/NULL,
10577 complain);
10578 }
10579 }
10580
10581 /* Substitute ARGS into the pack of a fold expression T. */
10582
10583 static inline tree
10584 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10585 {
10586 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
10587 }
10588
10589 /* Substitute ARGS into the pack of a fold expression T. */
10590
10591 static inline tree
10592 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10593 {
10594 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
10595 }
10596
10597 /* Expand a PACK of arguments into a grouped as left fold.
10598 Given a pack containing elements A0, A1, ..., An and an
10599 operator @, this builds the expression:
10600
10601 ((A0 @ A1) @ A2) ... @ An
10602
10603 Note that PACK must not be empty.
10604
10605 The operator is defined by the original fold expression T. */
10606
10607 static tree
10608 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
10609 {
10610 tree left = TREE_VEC_ELT (pack, 0);
10611 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
10612 {
10613 tree right = TREE_VEC_ELT (pack, i);
10614 left = fold_expression (t, left, right, complain);
10615 }
10616 return left;
10617 }
10618
10619 /* Substitute into a unary left fold expression. */
10620
10621 static tree
10622 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
10623 tree in_decl)
10624 {
10625 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10626 if (TREE_VEC_LENGTH (pack) == 0)
10627 return expand_empty_fold (t, complain);
10628 else
10629 return expand_left_fold (t, pack, complain);
10630 }
10631
10632 /* Substitute into a binary left fold expression.
10633
10634 Do ths by building a single (non-empty) vector of argumnts and
10635 building the expression from those elements. */
10636
10637 static tree
10638 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
10639 tree in_decl)
10640 {
10641 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10642 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10643
10644 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
10645 TREE_VEC_ELT (vec, 0) = init;
10646 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
10647 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
10648
10649 return expand_left_fold (t, vec, complain);
10650 }
10651
10652 /* Expand a PACK of arguments into a grouped as right fold.
10653 Given a pack containing elementns A0, A1, ..., and an
10654 operator @, this builds the expression:
10655
10656 A0@ ... (An-2 @ (An-1 @ An))
10657
10658 Note that PACK must not be empty.
10659
10660 The operator is defined by the original fold expression T. */
10661
10662 tree
10663 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
10664 {
10665 // Build the expression.
10666 int n = TREE_VEC_LENGTH (pack);
10667 tree right = TREE_VEC_ELT (pack, n - 1);
10668 for (--n; n != 0; --n)
10669 {
10670 tree left = TREE_VEC_ELT (pack, n - 1);
10671 right = fold_expression (t, left, right, complain);
10672 }
10673 return right;
10674 }
10675
10676 /* Substitute into a unary right fold expression. */
10677
10678 static tree
10679 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
10680 tree in_decl)
10681 {
10682 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10683 if (TREE_VEC_LENGTH (pack) == 0)
10684 return expand_empty_fold (t, complain);
10685 else
10686 return expand_right_fold (t, pack, complain);
10687 }
10688
10689 /* Substitute into a binary right fold expression.
10690
10691 Do ths by building a single (non-empty) vector of arguments and
10692 building the expression from those elements. */
10693
10694 static tree
10695 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
10696 tree in_decl)
10697 {
10698 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10699 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10700
10701 int n = TREE_VEC_LENGTH (pack);
10702 tree vec = make_tree_vec (n + 1);
10703 for (int i = 0; i < n; ++i)
10704 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
10705 TREE_VEC_ELT (vec, n) = init;
10706
10707 return expand_right_fold (t, vec, complain);
10708 }
10709
10710
10711 /* Substitute ARGS into T, which is an pack expansion
10712 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10713 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10714 (if only a partial substitution could be performed) or
10715 ERROR_MARK_NODE if there was an error. */
10716 tree
10717 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10718 tree in_decl)
10719 {
10720 tree pattern;
10721 tree pack, packs = NULL_TREE;
10722 bool unsubstituted_packs = false;
10723 int i, len = -1;
10724 tree result;
10725 hash_map<tree, tree> *saved_local_specializations = NULL;
10726 bool need_local_specializations = false;
10727 int levels;
10728
10729 gcc_assert (PACK_EXPANSION_P (t));
10730 pattern = PACK_EXPANSION_PATTERN (t);
10731
10732 /* Add in any args remembered from an earlier partial instantiation. */
10733 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10734
10735 levels = TMPL_ARGS_DEPTH (args);
10736
10737 /* Determine the argument packs that will instantiate the parameter
10738 packs used in the expansion expression. While we're at it,
10739 compute the number of arguments to be expanded and make sure it
10740 is consistent. */
10741 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10742 pack = TREE_CHAIN (pack))
10743 {
10744 tree parm_pack = TREE_VALUE (pack);
10745 tree arg_pack = NULL_TREE;
10746 tree orig_arg = NULL_TREE;
10747 int level = 0;
10748
10749 if (TREE_CODE (parm_pack) == BASES)
10750 {
10751 if (BASES_DIRECT (parm_pack))
10752 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10753 args, complain, in_decl, false));
10754 else
10755 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10756 args, complain, in_decl, false));
10757 }
10758 if (TREE_CODE (parm_pack) == PARM_DECL)
10759 {
10760 /* We know we have correct local_specializations if this
10761 expansion is at function scope, or if we're dealing with a
10762 local parameter in a requires expression; for the latter,
10763 tsubst_requires_expr set it up appropriately. */
10764 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
10765 arg_pack = retrieve_local_specialization (parm_pack);
10766 else
10767 {
10768 /* We can't rely on local_specializations for a parameter
10769 name used later in a function declaration (such as in a
10770 late-specified return type). Even if it exists, it might
10771 have the wrong value for a recursive call. Just make a
10772 dummy decl, since it's only used for its type. */
10773 arg_pack = tsubst_decl (parm_pack, args, complain);
10774 if (arg_pack && DECL_PACK_P (arg_pack))
10775 /* Partial instantiation of the parm_pack, we can't build
10776 up an argument pack yet. */
10777 arg_pack = NULL_TREE;
10778 else
10779 arg_pack = make_fnparm_pack (arg_pack);
10780 need_local_specializations = true;
10781 }
10782 }
10783 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10784 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10785 else
10786 {
10787 int idx;
10788 template_parm_level_and_index (parm_pack, &level, &idx);
10789
10790 if (level <= levels)
10791 arg_pack = TMPL_ARG (args, level, idx);
10792 }
10793
10794 orig_arg = arg_pack;
10795 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
10796 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
10797
10798 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
10799 /* This can only happen if we forget to expand an argument
10800 pack somewhere else. Just return an error, silently. */
10801 {
10802 result = make_tree_vec (1);
10803 TREE_VEC_ELT (result, 0) = error_mark_node;
10804 return result;
10805 }
10806
10807 if (arg_pack)
10808 {
10809 int my_len =
10810 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10811
10812 /* Don't bother trying to do a partial substitution with
10813 incomplete packs; we'll try again after deduction. */
10814 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10815 return t;
10816
10817 if (len < 0)
10818 len = my_len;
10819 else if (len != my_len)
10820 {
10821 if (!(complain & tf_error))
10822 /* Fail quietly. */;
10823 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10824 error ("mismatched argument pack lengths while expanding "
10825 "%<%T%>",
10826 pattern);
10827 else
10828 error ("mismatched argument pack lengths while expanding "
10829 "%<%E%>",
10830 pattern);
10831 return error_mark_node;
10832 }
10833
10834 /* Keep track of the parameter packs and their corresponding
10835 argument packs. */
10836 packs = tree_cons (parm_pack, arg_pack, packs);
10837 TREE_TYPE (packs) = orig_arg;
10838 }
10839 else
10840 {
10841 /* We can't substitute for this parameter pack. We use a flag as
10842 well as the missing_level counter because function parameter
10843 packs don't have a level. */
10844 unsubstituted_packs = true;
10845 }
10846 }
10847
10848 /* If the expansion is just T..., return the matching argument pack. */
10849 if (!unsubstituted_packs
10850 && TREE_PURPOSE (packs) == pattern)
10851 {
10852 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10853 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10854 || pack_expansion_args_count (args))
10855 return args;
10856 /* Otherwise use the normal path so we get convert_from_reference. */
10857 }
10858
10859 /* We cannot expand this expansion expression, because we don't have
10860 all of the argument packs we need. */
10861 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10862 {
10863 /* We got some full packs, but we can't substitute them in until we
10864 have values for all the packs. So remember these until then. */
10865
10866 t = make_pack_expansion (pattern);
10867 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10868 return t;
10869 }
10870 else if (unsubstituted_packs)
10871 {
10872 /* There were no real arguments, we're just replacing a parameter
10873 pack with another version of itself. Substitute into the
10874 pattern and return a PACK_EXPANSION_*. The caller will need to
10875 deal with that. */
10876 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10877 t = tsubst_expr (pattern, args, complain, in_decl,
10878 /*integral_constant_expression_p=*/false);
10879 else
10880 t = tsubst (pattern, args, complain, in_decl);
10881 t = make_pack_expansion (t);
10882 return t;
10883 }
10884
10885 gcc_assert (len >= 0);
10886
10887 if (need_local_specializations)
10888 {
10889 /* We're in a late-specified return type, so create our own local
10890 specializations map; the current map is either NULL or (in the
10891 case of recursive unification) might have bindings that we don't
10892 want to use or alter. */
10893 saved_local_specializations = local_specializations;
10894 local_specializations = new hash_map<tree, tree>;
10895 }
10896
10897 /* For each argument in each argument pack, substitute into the
10898 pattern. */
10899 result = make_tree_vec (len);
10900 for (i = 0; i < len; ++i)
10901 {
10902 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10903 i,
10904 args, complain,
10905 in_decl);
10906 TREE_VEC_ELT (result, i) = t;
10907 if (t == error_mark_node)
10908 {
10909 result = error_mark_node;
10910 break;
10911 }
10912 }
10913
10914 /* Update ARGS to restore the substitution from parameter packs to
10915 their argument packs. */
10916 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10917 {
10918 tree parm = TREE_PURPOSE (pack);
10919
10920 if (TREE_CODE (parm) == PARM_DECL
10921 || TREE_CODE (parm) == FIELD_DECL)
10922 register_local_specialization (TREE_TYPE (pack), parm);
10923 else
10924 {
10925 int idx, level;
10926
10927 if (TREE_VALUE (pack) == NULL_TREE)
10928 continue;
10929
10930 template_parm_level_and_index (parm, &level, &idx);
10931
10932 /* Update the corresponding argument. */
10933 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10934 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10935 TREE_TYPE (pack);
10936 else
10937 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10938 }
10939 }
10940
10941 if (need_local_specializations)
10942 {
10943 delete local_specializations;
10944 local_specializations = saved_local_specializations;
10945 }
10946
10947 return result;
10948 }
10949
10950 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10951 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10952 parameter packs; all parms generated from a function parameter pack will
10953 have the same DECL_PARM_INDEX. */
10954
10955 tree
10956 get_pattern_parm (tree parm, tree tmpl)
10957 {
10958 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10959 tree patparm;
10960
10961 if (DECL_ARTIFICIAL (parm))
10962 {
10963 for (patparm = DECL_ARGUMENTS (pattern);
10964 patparm; patparm = DECL_CHAIN (patparm))
10965 if (DECL_ARTIFICIAL (patparm)
10966 && DECL_NAME (parm) == DECL_NAME (patparm))
10967 break;
10968 }
10969 else
10970 {
10971 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10972 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10973 gcc_assert (DECL_PARM_INDEX (patparm)
10974 == DECL_PARM_INDEX (parm));
10975 }
10976
10977 return patparm;
10978 }
10979
10980 /* Substitute ARGS into the vector or list of template arguments T. */
10981
10982 static tree
10983 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10984 {
10985 tree orig_t = t;
10986 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10987 tree *elts;
10988
10989 if (t == error_mark_node)
10990 return error_mark_node;
10991
10992 len = TREE_VEC_LENGTH (t);
10993 elts = XALLOCAVEC (tree, len);
10994
10995 for (i = 0; i < len; i++)
10996 {
10997 tree orig_arg = TREE_VEC_ELT (t, i);
10998 tree new_arg;
10999
11000 if (TREE_CODE (orig_arg) == TREE_VEC)
11001 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11002 else if (PACK_EXPANSION_P (orig_arg))
11003 {
11004 /* Substitute into an expansion expression. */
11005 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11006
11007 if (TREE_CODE (new_arg) == TREE_VEC)
11008 /* Add to the expanded length adjustment the number of
11009 expanded arguments. We subtract one from this
11010 measurement, because the argument pack expression
11011 itself is already counted as 1 in
11012 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11013 the argument pack is empty. */
11014 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11015 }
11016 else if (ARGUMENT_PACK_P (orig_arg))
11017 {
11018 /* Substitute into each of the arguments. */
11019 new_arg = TYPE_P (orig_arg)
11020 ? cxx_make_type (TREE_CODE (orig_arg))
11021 : make_node (TREE_CODE (orig_arg));
11022
11023 SET_ARGUMENT_PACK_ARGS (
11024 new_arg,
11025 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11026 args, complain, in_decl));
11027
11028 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11029 new_arg = error_mark_node;
11030
11031 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11032 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11033 complain, in_decl);
11034 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11035
11036 if (TREE_TYPE (new_arg) == error_mark_node)
11037 new_arg = error_mark_node;
11038 }
11039 }
11040 else
11041 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11042
11043 if (new_arg == error_mark_node)
11044 return error_mark_node;
11045
11046 elts[i] = new_arg;
11047 if (new_arg != orig_arg)
11048 need_new = 1;
11049 }
11050
11051 if (!need_new)
11052 return t;
11053
11054 /* Make space for the expanded arguments coming from template
11055 argument packs. */
11056 t = make_tree_vec (len + expanded_len_adjust);
11057 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11058 arguments for a member template.
11059 In that case each TREE_VEC in ORIG_T represents a level of template
11060 arguments, and ORIG_T won't carry any non defaulted argument count.
11061 It will rather be the nested TREE_VECs that will carry one.
11062 In other words, ORIG_T carries a non defaulted argument count only
11063 if it doesn't contain any nested TREE_VEC. */
11064 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11065 {
11066 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11067 count += expanded_len_adjust;
11068 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11069 }
11070 for (i = 0, out = 0; i < len; i++)
11071 {
11072 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11073 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11074 && TREE_CODE (elts[i]) == TREE_VEC)
11075 {
11076 int idx;
11077
11078 /* Now expand the template argument pack "in place". */
11079 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11080 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11081 }
11082 else
11083 {
11084 TREE_VEC_ELT (t, out) = elts[i];
11085 out++;
11086 }
11087 }
11088
11089 return t;
11090 }
11091
11092 /* Return the result of substituting ARGS into the template parameters
11093 given by PARMS. If there are m levels of ARGS and m + n levels of
11094 PARMS, then the result will contain n levels of PARMS. For
11095 example, if PARMS is `template <class T> template <class U>
11096 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11097 result will be `template <int*, double, class V>'. */
11098
11099 static tree
11100 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11101 {
11102 tree r = NULL_TREE;
11103 tree* new_parms;
11104
11105 /* When substituting into a template, we must set
11106 PROCESSING_TEMPLATE_DECL as the template parameters may be
11107 dependent if they are based on one-another, and the dependency
11108 predicates are short-circuit outside of templates. */
11109 ++processing_template_decl;
11110
11111 for (new_parms = &r;
11112 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11113 new_parms = &(TREE_CHAIN (*new_parms)),
11114 parms = TREE_CHAIN (parms))
11115 {
11116 tree new_vec =
11117 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
11118 int i;
11119
11120 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11121 {
11122 tree tuple;
11123
11124 if (parms == error_mark_node)
11125 continue;
11126
11127 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
11128
11129 if (tuple == error_mark_node)
11130 continue;
11131
11132 TREE_VEC_ELT (new_vec, i) =
11133 tsubst_template_parm (tuple, args, complain);
11134 }
11135
11136 *new_parms =
11137 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11138 - TMPL_ARGS_DEPTH (args)),
11139 new_vec, NULL_TREE);
11140 }
11141
11142 --processing_template_decl;
11143
11144 return r;
11145 }
11146
11147 /* Return the result of substituting ARGS into one template parameter
11148 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11149 parameter and which TREE_PURPOSE is the default argument of the
11150 template parameter. */
11151
11152 static tree
11153 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11154 {
11155 tree default_value, parm_decl;
11156
11157 if (args == NULL_TREE
11158 || t == NULL_TREE
11159 || t == error_mark_node)
11160 return t;
11161
11162 gcc_assert (TREE_CODE (t) == TREE_LIST);
11163
11164 default_value = TREE_PURPOSE (t);
11165 parm_decl = TREE_VALUE (t);
11166
11167 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11168 if (TREE_CODE (parm_decl) == PARM_DECL
11169 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11170 parm_decl = error_mark_node;
11171 default_value = tsubst_template_arg (default_value, args,
11172 complain, NULL_TREE);
11173
11174 return build_tree_list (default_value, parm_decl);
11175 }
11176
11177 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11178 type T. If T is not an aggregate or enumeration type, it is
11179 handled as if by tsubst. IN_DECL is as for tsubst. If
11180 ENTERING_SCOPE is nonzero, T is the context for a template which
11181 we are presently tsubst'ing. Return the substituted value. */
11182
11183 static tree
11184 tsubst_aggr_type (tree t,
11185 tree args,
11186 tsubst_flags_t complain,
11187 tree in_decl,
11188 int entering_scope)
11189 {
11190 if (t == NULL_TREE)
11191 return NULL_TREE;
11192
11193 switch (TREE_CODE (t))
11194 {
11195 case RECORD_TYPE:
11196 if (TYPE_PTRMEMFUNC_P (t))
11197 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11198
11199 /* Else fall through. */
11200 case ENUMERAL_TYPE:
11201 case UNION_TYPE:
11202 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11203 {
11204 tree argvec;
11205 tree context;
11206 tree r;
11207 int saved_unevaluated_operand;
11208 int saved_inhibit_evaluation_warnings;
11209
11210 /* In "sizeof(X<I>)" we need to evaluate "I". */
11211 saved_unevaluated_operand = cp_unevaluated_operand;
11212 cp_unevaluated_operand = 0;
11213 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11214 c_inhibit_evaluation_warnings = 0;
11215
11216 /* First, determine the context for the type we are looking
11217 up. */
11218 context = TYPE_CONTEXT (t);
11219 if (context && TYPE_P (context))
11220 {
11221 context = tsubst_aggr_type (context, args, complain,
11222 in_decl, /*entering_scope=*/1);
11223 /* If context is a nested class inside a class template,
11224 it may still need to be instantiated (c++/33959). */
11225 context = complete_type (context);
11226 }
11227
11228 /* Then, figure out what arguments are appropriate for the
11229 type we are trying to find. For example, given:
11230
11231 template <class T> struct S;
11232 template <class T, class U> void f(T, U) { S<U> su; }
11233
11234 and supposing that we are instantiating f<int, double>,
11235 then our ARGS will be {int, double}, but, when looking up
11236 S we only want {double}. */
11237 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11238 complain, in_decl);
11239 if (argvec == error_mark_node)
11240 r = error_mark_node;
11241 else
11242 {
11243 r = lookup_template_class (t, argvec, in_decl, context,
11244 entering_scope, complain);
11245 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11246 }
11247
11248 cp_unevaluated_operand = saved_unevaluated_operand;
11249 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11250
11251 return r;
11252 }
11253 else
11254 /* This is not a template type, so there's nothing to do. */
11255 return t;
11256
11257 default:
11258 return tsubst (t, args, complain, in_decl);
11259 }
11260 }
11261
11262 /* Substitute into the default argument ARG (a default argument for
11263 FN), which has the indicated TYPE. */
11264
11265 tree
11266 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11267 {
11268 tree saved_class_ptr = NULL_TREE;
11269 tree saved_class_ref = NULL_TREE;
11270 int errs = errorcount + sorrycount;
11271
11272 /* This can happen in invalid code. */
11273 if (TREE_CODE (arg) == DEFAULT_ARG)
11274 return arg;
11275
11276 /* This default argument came from a template. Instantiate the
11277 default argument here, not in tsubst. In the case of
11278 something like:
11279
11280 template <class T>
11281 struct S {
11282 static T t();
11283 void f(T = t());
11284 };
11285
11286 we must be careful to do name lookup in the scope of S<T>,
11287 rather than in the current class. */
11288 push_access_scope (fn);
11289 /* The "this" pointer is not valid in a default argument. */
11290 if (cfun)
11291 {
11292 saved_class_ptr = current_class_ptr;
11293 cp_function_chain->x_current_class_ptr = NULL_TREE;
11294 saved_class_ref = current_class_ref;
11295 cp_function_chain->x_current_class_ref = NULL_TREE;
11296 }
11297
11298 push_deferring_access_checks(dk_no_deferred);
11299 /* The default argument expression may cause implicitly defined
11300 member functions to be synthesized, which will result in garbage
11301 collection. We must treat this situation as if we were within
11302 the body of function so as to avoid collecting live data on the
11303 stack. */
11304 ++function_depth;
11305 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11306 complain, NULL_TREE,
11307 /*integral_constant_expression_p=*/false);
11308 --function_depth;
11309 pop_deferring_access_checks();
11310
11311 /* Restore the "this" pointer. */
11312 if (cfun)
11313 {
11314 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11315 cp_function_chain->x_current_class_ref = saved_class_ref;
11316 }
11317
11318 if (errorcount+sorrycount > errs
11319 && (complain & tf_warning_or_error))
11320 inform (input_location,
11321 " when instantiating default argument for call to %D", fn);
11322
11323 /* Make sure the default argument is reasonable. */
11324 arg = check_default_argument (type, arg, complain);
11325
11326 pop_access_scope (fn);
11327
11328 return arg;
11329 }
11330
11331 /* Substitute into all the default arguments for FN. */
11332
11333 static void
11334 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11335 {
11336 tree arg;
11337 tree tmpl_args;
11338
11339 tmpl_args = DECL_TI_ARGS (fn);
11340
11341 /* If this function is not yet instantiated, we certainly don't need
11342 its default arguments. */
11343 if (uses_template_parms (tmpl_args))
11344 return;
11345 /* Don't do this again for clones. */
11346 if (DECL_CLONED_FUNCTION_P (fn))
11347 return;
11348
11349 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11350 arg;
11351 arg = TREE_CHAIN (arg))
11352 if (TREE_PURPOSE (arg))
11353 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11354 TREE_VALUE (arg),
11355 TREE_PURPOSE (arg),
11356 complain);
11357 }
11358
11359 /* Substitute the ARGS into the T, which is a _DECL. Return the
11360 result of the substitution. Issue error and warning messages under
11361 control of COMPLAIN. */
11362
11363 static tree
11364 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11365 {
11366 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11367 location_t saved_loc;
11368 tree r = NULL_TREE;
11369 tree in_decl = t;
11370 hashval_t hash = 0;
11371
11372 /* Set the filename and linenumber to improve error-reporting. */
11373 saved_loc = input_location;
11374 input_location = DECL_SOURCE_LOCATION (t);
11375
11376 switch (TREE_CODE (t))
11377 {
11378 case TEMPLATE_DECL:
11379 {
11380 /* We can get here when processing a member function template,
11381 member class template, or template template parameter. */
11382 tree decl = DECL_TEMPLATE_RESULT (t);
11383 tree spec;
11384 tree tmpl_args;
11385 tree full_args;
11386
11387 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11388 {
11389 /* Template template parameter is treated here. */
11390 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11391 if (new_type == error_mark_node)
11392 r = error_mark_node;
11393 /* If we get a real template back, return it. This can happen in
11394 the context of most_specialized_partial_spec. */
11395 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11396 r = new_type;
11397 else
11398 /* The new TEMPLATE_DECL was built in
11399 reduce_template_parm_level. */
11400 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
11401 break;
11402 }
11403
11404 /* We might already have an instance of this template.
11405 The ARGS are for the surrounding class type, so the
11406 full args contain the tsubst'd args for the context,
11407 plus the innermost args from the template decl. */
11408 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11409 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11410 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11411 /* Because this is a template, the arguments will still be
11412 dependent, even after substitution. If
11413 PROCESSING_TEMPLATE_DECL is not set, the dependency
11414 predicates will short-circuit. */
11415 ++processing_template_decl;
11416 full_args = tsubst_template_args (tmpl_args, args,
11417 complain, in_decl);
11418 --processing_template_decl;
11419 if (full_args == error_mark_node)
11420 RETURN (error_mark_node);
11421
11422 /* If this is a default template template argument,
11423 tsubst might not have changed anything. */
11424 if (full_args == tmpl_args)
11425 RETURN (t);
11426
11427 hash = hash_tmpl_and_args (t, full_args);
11428 spec = retrieve_specialization (t, full_args, hash);
11429 if (spec != NULL_TREE)
11430 {
11431 r = spec;
11432 break;
11433 }
11434
11435 /* Make a new template decl. It will be similar to the
11436 original, but will record the current template arguments.
11437 We also create a new function declaration, which is just
11438 like the old one, but points to this new template, rather
11439 than the old one. */
11440 r = copy_decl (t);
11441 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11442 DECL_CHAIN (r) = NULL_TREE;
11443
11444 // Build new template info linking to the original template decl.
11445 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11446
11447 if (TREE_CODE (decl) == TYPE_DECL
11448 && !TYPE_DECL_ALIAS_P (decl))
11449 {
11450 tree new_type;
11451 ++processing_template_decl;
11452 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11453 --processing_template_decl;
11454 if (new_type == error_mark_node)
11455 RETURN (error_mark_node);
11456
11457 TREE_TYPE (r) = new_type;
11458 /* For a partial specialization, we need to keep pointing to
11459 the primary template. */
11460 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11461 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11462 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11463 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11464 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11465 }
11466 else
11467 {
11468 tree new_decl;
11469 ++processing_template_decl;
11470 new_decl = tsubst (decl, args, complain, in_decl);
11471 --processing_template_decl;
11472 if (new_decl == error_mark_node)
11473 RETURN (error_mark_node);
11474
11475 DECL_TEMPLATE_RESULT (r) = new_decl;
11476 DECL_TI_TEMPLATE (new_decl) = r;
11477 TREE_TYPE (r) = TREE_TYPE (new_decl);
11478 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11479 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11480 }
11481
11482 SET_DECL_IMPLICIT_INSTANTIATION (r);
11483 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11484 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11485
11486 /* The template parameters for this new template are all the
11487 template parameters for the old template, except the
11488 outermost level of parameters. */
11489 DECL_TEMPLATE_PARMS (r)
11490 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11491 complain);
11492
11493 if (PRIMARY_TEMPLATE_P (t))
11494 DECL_PRIMARY_TEMPLATE (r) = r;
11495
11496 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
11497 /* Record this non-type partial instantiation. */
11498 register_specialization (r, t,
11499 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11500 false, hash);
11501 }
11502 break;
11503
11504 case FUNCTION_DECL:
11505 {
11506 tree ctx;
11507 tree argvec = NULL_TREE;
11508 tree *friends;
11509 tree gen_tmpl;
11510 tree type;
11511 int member;
11512 int args_depth;
11513 int parms_depth;
11514
11515 /* Nobody should be tsubst'ing into non-template functions. */
11516 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11517
11518 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11519 {
11520 tree spec;
11521 bool dependent_p;
11522
11523 /* If T is not dependent, just return it. We have to
11524 increment PROCESSING_TEMPLATE_DECL because
11525 value_dependent_expression_p assumes that nothing is
11526 dependent when PROCESSING_TEMPLATE_DECL is zero. */
11527 ++processing_template_decl;
11528 dependent_p = value_dependent_expression_p (t);
11529 --processing_template_decl;
11530 if (!dependent_p)
11531 RETURN (t);
11532
11533 /* Calculate the most general template of which R is a
11534 specialization, and the complete set of arguments used to
11535 specialize R. */
11536 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11537 argvec = tsubst_template_args (DECL_TI_ARGS
11538 (DECL_TEMPLATE_RESULT
11539 (DECL_TI_TEMPLATE (t))),
11540 args, complain, in_decl);
11541 if (argvec == error_mark_node)
11542 RETURN (error_mark_node);
11543
11544 /* Check to see if we already have this specialization. */
11545 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11546 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11547
11548 if (spec)
11549 {
11550 r = spec;
11551 break;
11552 }
11553
11554 /* We can see more levels of arguments than parameters if
11555 there was a specialization of a member template, like
11556 this:
11557
11558 template <class T> struct S { template <class U> void f(); }
11559 template <> template <class U> void S<int>::f(U);
11560
11561 Here, we'll be substituting into the specialization,
11562 because that's where we can find the code we actually
11563 want to generate, but we'll have enough arguments for
11564 the most general template.
11565
11566 We also deal with the peculiar case:
11567
11568 template <class T> struct S {
11569 template <class U> friend void f();
11570 };
11571 template <class U> void f() {}
11572 template S<int>;
11573 template void f<double>();
11574
11575 Here, the ARGS for the instantiation of will be {int,
11576 double}. But, we only need as many ARGS as there are
11577 levels of template parameters in CODE_PATTERN. We are
11578 careful not to get fooled into reducing the ARGS in
11579 situations like:
11580
11581 template <class T> struct S { template <class U> void f(U); }
11582 template <class T> template <> void S<T>::f(int) {}
11583
11584 which we can spot because the pattern will be a
11585 specialization in this case. */
11586 args_depth = TMPL_ARGS_DEPTH (args);
11587 parms_depth =
11588 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11589 if (args_depth > parms_depth
11590 && !DECL_TEMPLATE_SPECIALIZATION (t))
11591 args = get_innermost_template_args (args, parms_depth);
11592 }
11593 else
11594 {
11595 /* This special case arises when we have something like this:
11596
11597 template <class T> struct S {
11598 friend void f<int>(int, double);
11599 };
11600
11601 Here, the DECL_TI_TEMPLATE for the friend declaration
11602 will be an IDENTIFIER_NODE. We are being called from
11603 tsubst_friend_function, and we want only to create a
11604 new decl (R) with appropriate types so that we can call
11605 determine_specialization. */
11606 gen_tmpl = NULL_TREE;
11607 }
11608
11609 if (DECL_CLASS_SCOPE_P (t))
11610 {
11611 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11612 member = 2;
11613 else
11614 member = 1;
11615 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11616 complain, t, /*entering_scope=*/1);
11617 }
11618 else
11619 {
11620 member = 0;
11621 ctx = DECL_CONTEXT (t);
11622 }
11623 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11624 if (type == error_mark_node)
11625 RETURN (error_mark_node);
11626
11627 /* If we hit excessive deduction depth, the type is bogus even if
11628 it isn't error_mark_node, so don't build a decl. */
11629 if (excessive_deduction_depth)
11630 RETURN (error_mark_node);
11631
11632 /* We do NOT check for matching decls pushed separately at this
11633 point, as they may not represent instantiations of this
11634 template, and in any case are considered separate under the
11635 discrete model. */
11636 r = copy_decl (t);
11637 DECL_USE_TEMPLATE (r) = 0;
11638 TREE_TYPE (r) = type;
11639 /* Clear out the mangled name and RTL for the instantiation. */
11640 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11641 SET_DECL_RTL (r, NULL);
11642 /* Leave DECL_INITIAL set on deleted instantiations. */
11643 if (!DECL_DELETED_FN (r))
11644 DECL_INITIAL (r) = NULL_TREE;
11645 DECL_CONTEXT (r) = ctx;
11646
11647 /* OpenMP UDRs have the only argument a reference to the declared
11648 type. We want to diagnose if the declared type is a reference,
11649 which is invalid, but as references to references are usually
11650 quietly merged, diagnose it here. */
11651 if (DECL_OMP_DECLARE_REDUCTION_P (t))
11652 {
11653 tree argtype
11654 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11655 argtype = tsubst (argtype, args, complain, in_decl);
11656 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11657 error_at (DECL_SOURCE_LOCATION (t),
11658 "reference type %qT in "
11659 "%<#pragma omp declare reduction%>", argtype);
11660 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11661 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11662 argtype);
11663 }
11664
11665 if (member && DECL_CONV_FN_P (r))
11666 /* Type-conversion operator. Reconstruct the name, in
11667 case it's the name of one of the template's parameters. */
11668 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11669
11670 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11671 complain, t);
11672 DECL_RESULT (r) = NULL_TREE;
11673
11674 TREE_STATIC (r) = 0;
11675 TREE_PUBLIC (r) = TREE_PUBLIC (t);
11676 DECL_EXTERNAL (r) = 1;
11677 /* If this is an instantiation of a function with internal
11678 linkage, we already know what object file linkage will be
11679 assigned to the instantiation. */
11680 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
11681 DECL_DEFER_OUTPUT (r) = 0;
11682 DECL_CHAIN (r) = NULL_TREE;
11683 DECL_PENDING_INLINE_INFO (r) = 0;
11684 DECL_PENDING_INLINE_P (r) = 0;
11685 DECL_SAVED_TREE (r) = NULL_TREE;
11686 DECL_STRUCT_FUNCTION (r) = NULL;
11687 TREE_USED (r) = 0;
11688 /* We'll re-clone as appropriate in instantiate_template. */
11689 DECL_CLONED_FUNCTION (r) = NULL_TREE;
11690
11691 /* If we aren't complaining now, return on error before we register
11692 the specialization so that we'll complain eventually. */
11693 if ((complain & tf_error) == 0
11694 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11695 && !grok_op_properties (r, /*complain=*/false))
11696 RETURN (error_mark_node);
11697
11698 /* When instantiating a constrained member, substitute
11699 into the constraints to create a new constraint. */
11700 if (tree ci = get_constraints (t))
11701 if (member)
11702 {
11703 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
11704 set_constraints (r, ci);
11705 }
11706
11707 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11708 this in the special friend case mentioned above where
11709 GEN_TMPL is NULL. */
11710 if (gen_tmpl)
11711 {
11712 DECL_TEMPLATE_INFO (r)
11713 = build_template_info (gen_tmpl, argvec);
11714 SET_DECL_IMPLICIT_INSTANTIATION (r);
11715
11716 tree new_r
11717 = register_specialization (r, gen_tmpl, argvec, false, hash);
11718 if (new_r != r)
11719 /* We instantiated this while substituting into
11720 the type earlier (template/friend54.C). */
11721 RETURN (new_r);
11722
11723 /* We're not supposed to instantiate default arguments
11724 until they are called, for a template. But, for a
11725 declaration like:
11726
11727 template <class T> void f ()
11728 { extern void g(int i = T()); }
11729
11730 we should do the substitution when the template is
11731 instantiated. We handle the member function case in
11732 instantiate_class_template since the default arguments
11733 might refer to other members of the class. */
11734 if (!member
11735 && !PRIMARY_TEMPLATE_P (gen_tmpl)
11736 && !uses_template_parms (argvec))
11737 tsubst_default_arguments (r, complain);
11738 }
11739 else
11740 DECL_TEMPLATE_INFO (r) = NULL_TREE;
11741
11742 /* Copy the list of befriending classes. */
11743 for (friends = &DECL_BEFRIENDING_CLASSES (r);
11744 *friends;
11745 friends = &TREE_CHAIN (*friends))
11746 {
11747 *friends = copy_node (*friends);
11748 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
11749 args, complain,
11750 in_decl);
11751 }
11752
11753 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
11754 {
11755 maybe_retrofit_in_chrg (r);
11756 if (DECL_CONSTRUCTOR_P (r))
11757 grok_ctor_properties (ctx, r);
11758 if (DECL_INHERITED_CTOR_BASE (r))
11759 deduce_inheriting_ctor (r);
11760 /* If this is an instantiation of a member template, clone it.
11761 If it isn't, that'll be handled by
11762 clone_constructors_and_destructors. */
11763 if (PRIMARY_TEMPLATE_P (gen_tmpl))
11764 clone_function_decl (r, /*update_method_vec_p=*/0);
11765 }
11766 else if ((complain & tf_error) != 0
11767 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11768 && !grok_op_properties (r, /*complain=*/true))
11769 RETURN (error_mark_node);
11770
11771 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
11772 SET_DECL_FRIEND_CONTEXT (r,
11773 tsubst (DECL_FRIEND_CONTEXT (t),
11774 args, complain, in_decl));
11775
11776 /* Possibly limit visibility based on template args. */
11777 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11778 if (DECL_VISIBILITY_SPECIFIED (t))
11779 {
11780 DECL_VISIBILITY_SPECIFIED (r) = 0;
11781 DECL_ATTRIBUTES (r)
11782 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11783 }
11784 determine_visibility (r);
11785 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
11786 && !processing_template_decl)
11787 defaulted_late_check (r);
11788
11789 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11790 args, complain, in_decl);
11791 }
11792 break;
11793
11794 case PARM_DECL:
11795 {
11796 tree type = NULL_TREE;
11797 int i, len = 1;
11798 tree expanded_types = NULL_TREE;
11799 tree prev_r = NULL_TREE;
11800 tree first_r = NULL_TREE;
11801
11802 if (DECL_PACK_P (t))
11803 {
11804 /* If there is a local specialization that isn't a
11805 parameter pack, it means that we're doing a "simple"
11806 substitution from inside tsubst_pack_expansion. Just
11807 return the local specialization (which will be a single
11808 parm). */
11809 tree spec = retrieve_local_specialization (t);
11810 if (spec
11811 && TREE_CODE (spec) == PARM_DECL
11812 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11813 RETURN (spec);
11814
11815 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11816 the parameters in this function parameter pack. */
11817 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11818 complain, in_decl);
11819 if (TREE_CODE (expanded_types) == TREE_VEC)
11820 {
11821 len = TREE_VEC_LENGTH (expanded_types);
11822
11823 /* Zero-length parameter packs are boring. Just substitute
11824 into the chain. */
11825 if (len == 0)
11826 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11827 TREE_CHAIN (t)));
11828 }
11829 else
11830 {
11831 /* All we did was update the type. Make a note of that. */
11832 type = expanded_types;
11833 expanded_types = NULL_TREE;
11834 }
11835 }
11836
11837 /* Loop through all of the parameters we'll build. When T is
11838 a function parameter pack, LEN is the number of expanded
11839 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11840 r = NULL_TREE;
11841 for (i = 0; i < len; ++i)
11842 {
11843 prev_r = r;
11844 r = copy_node (t);
11845 if (DECL_TEMPLATE_PARM_P (t))
11846 SET_DECL_TEMPLATE_PARM_P (r);
11847
11848 if (expanded_types)
11849 /* We're on the Ith parameter of the function parameter
11850 pack. */
11851 {
11852 /* Get the Ith type. */
11853 type = TREE_VEC_ELT (expanded_types, i);
11854
11855 /* Rename the parameter to include the index. */
11856 DECL_NAME (r)
11857 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11858 }
11859 else if (!type)
11860 /* We're dealing with a normal parameter. */
11861 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11862
11863 type = type_decays_to (type);
11864 TREE_TYPE (r) = type;
11865 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11866
11867 if (DECL_INITIAL (r))
11868 {
11869 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11870 DECL_INITIAL (r) = TREE_TYPE (r);
11871 else
11872 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11873 complain, in_decl);
11874 }
11875
11876 DECL_CONTEXT (r) = NULL_TREE;
11877
11878 if (!DECL_TEMPLATE_PARM_P (r))
11879 DECL_ARG_TYPE (r) = type_passed_as (type);
11880
11881 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11882 args, complain, in_decl);
11883
11884 /* Keep track of the first new parameter we
11885 generate. That's what will be returned to the
11886 caller. */
11887 if (!first_r)
11888 first_r = r;
11889
11890 /* Build a proper chain of parameters when substituting
11891 into a function parameter pack. */
11892 if (prev_r)
11893 DECL_CHAIN (prev_r) = r;
11894 }
11895
11896 /* If cp_unevaluated_operand is set, we're just looking for a
11897 single dummy parameter, so don't keep going. */
11898 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11899 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11900 complain, DECL_CHAIN (t));
11901
11902 /* FIRST_R contains the start of the chain we've built. */
11903 r = first_r;
11904 }
11905 break;
11906
11907 case FIELD_DECL:
11908 {
11909 tree type = NULL_TREE;
11910 tree vec = NULL_TREE;
11911 tree expanded_types = NULL_TREE;
11912 int len = 1;
11913
11914 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11915 {
11916 /* This field is a lambda capture pack. Return a TREE_VEC of
11917 the expanded fields to instantiate_class_template_1 and
11918 store them in the specializations hash table as a
11919 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11920 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11921 complain, in_decl);
11922 if (TREE_CODE (expanded_types) == TREE_VEC)
11923 {
11924 len = TREE_VEC_LENGTH (expanded_types);
11925 vec = make_tree_vec (len);
11926 }
11927 else
11928 {
11929 /* All we did was update the type. Make a note of that. */
11930 type = expanded_types;
11931 expanded_types = NULL_TREE;
11932 }
11933 }
11934
11935 for (int i = 0; i < len; ++i)
11936 {
11937 r = copy_decl (t);
11938 if (expanded_types)
11939 {
11940 type = TREE_VEC_ELT (expanded_types, i);
11941 DECL_NAME (r)
11942 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11943 }
11944 else if (!type)
11945 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11946
11947 if (type == error_mark_node)
11948 RETURN (error_mark_node);
11949 TREE_TYPE (r) = type;
11950 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11951
11952 if (DECL_C_BIT_FIELD (r))
11953 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11954 non-bit-fields DECL_INITIAL is a non-static data member
11955 initializer, which gets deferred instantiation. */
11956 DECL_INITIAL (r)
11957 = tsubst_expr (DECL_INITIAL (t), args,
11958 complain, in_decl,
11959 /*integral_constant_expression_p=*/true);
11960 else if (DECL_INITIAL (t))
11961 {
11962 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11963 NSDMI in perform_member_init. Still set DECL_INITIAL
11964 so that we know there is one. */
11965 DECL_INITIAL (r) = void_node;
11966 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11967 retrofit_lang_decl (r);
11968 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11969 }
11970 /* We don't have to set DECL_CONTEXT here; it is set by
11971 finish_member_declaration. */
11972 DECL_CHAIN (r) = NULL_TREE;
11973
11974 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11975 args, complain, in_decl);
11976
11977 if (vec)
11978 TREE_VEC_ELT (vec, i) = r;
11979 }
11980
11981 if (vec)
11982 {
11983 r = vec;
11984 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11985 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11986 SET_ARGUMENT_PACK_ARGS (pack, vec);
11987 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11988 TREE_TYPE (pack) = tpack;
11989 register_specialization (pack, t, args, false, 0);
11990 }
11991 }
11992 break;
11993
11994 case USING_DECL:
11995 /* We reach here only for member using decls. We also need to check
11996 uses_template_parms because DECL_DEPENDENT_P is not set for a
11997 using-declaration that designates a member of the current
11998 instantiation (c++/53549). */
11999 if (DECL_DEPENDENT_P (t)
12000 || uses_template_parms (USING_DECL_SCOPE (t)))
12001 {
12002 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12003 complain, in_decl);
12004 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12005 r = do_class_using_decl (inst_scope, name);
12006 if (!r)
12007 r = error_mark_node;
12008 else
12009 {
12010 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12011 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12012 }
12013 }
12014 else
12015 {
12016 r = copy_node (t);
12017 DECL_CHAIN (r) = NULL_TREE;
12018 }
12019 break;
12020
12021 case TYPE_DECL:
12022 case VAR_DECL:
12023 {
12024 tree argvec = NULL_TREE;
12025 tree gen_tmpl = NULL_TREE;
12026 tree spec;
12027 tree tmpl = NULL_TREE;
12028 tree ctx;
12029 tree type = NULL_TREE;
12030 bool local_p;
12031
12032 if (TREE_TYPE (t) == error_mark_node)
12033 RETURN (error_mark_node);
12034
12035 if (TREE_CODE (t) == TYPE_DECL
12036 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12037 {
12038 /* If this is the canonical decl, we don't have to
12039 mess with instantiations, and often we can't (for
12040 typename, template type parms and such). Note that
12041 TYPE_NAME is not correct for the above test if
12042 we've copied the type for a typedef. */
12043 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12044 if (type == error_mark_node)
12045 RETURN (error_mark_node);
12046 r = TYPE_NAME (type);
12047 break;
12048 }
12049
12050 /* Check to see if we already have the specialization we
12051 need. */
12052 spec = NULL_TREE;
12053 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12054 {
12055 /* T is a static data member or namespace-scope entity.
12056 We have to substitute into namespace-scope variables
12057 (not just variable templates) because of cases like:
12058
12059 template <class T> void f() { extern T t; }
12060
12061 where the entity referenced is not known until
12062 instantiation time. */
12063 local_p = false;
12064 ctx = DECL_CONTEXT (t);
12065 if (DECL_CLASS_SCOPE_P (t))
12066 {
12067 ctx = tsubst_aggr_type (ctx, args,
12068 complain,
12069 in_decl, /*entering_scope=*/1);
12070 /* If CTX is unchanged, then T is in fact the
12071 specialization we want. That situation occurs when
12072 referencing a static data member within in its own
12073 class. We can use pointer equality, rather than
12074 same_type_p, because DECL_CONTEXT is always
12075 canonical... */
12076 if (ctx == DECL_CONTEXT (t)
12077 /* ... unless T is a member template; in which
12078 case our caller can be willing to create a
12079 specialization of that template represented
12080 by T. */
12081 && !(DECL_TI_TEMPLATE (t)
12082 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12083 spec = t;
12084 }
12085
12086 if (!spec)
12087 {
12088 tmpl = DECL_TI_TEMPLATE (t);
12089 gen_tmpl = most_general_template (tmpl);
12090 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12091 if (argvec != error_mark_node)
12092 argvec = (coerce_innermost_template_parms
12093 (DECL_TEMPLATE_PARMS (gen_tmpl),
12094 argvec, t, complain,
12095 /*all*/true, /*defarg*/true));
12096 if (argvec == error_mark_node)
12097 RETURN (error_mark_node);
12098 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12099 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12100 }
12101 }
12102 else
12103 {
12104 /* A local variable. */
12105 local_p = true;
12106 /* Subsequent calls to pushdecl will fill this in. */
12107 ctx = NULL_TREE;
12108 spec = retrieve_local_specialization (t);
12109 }
12110 /* If we already have the specialization we need, there is
12111 nothing more to do. */
12112 if (spec)
12113 {
12114 r = spec;
12115 break;
12116 }
12117
12118 /* Create a new node for the specialization we need. */
12119 r = copy_decl (t);
12120 if (type == NULL_TREE)
12121 {
12122 if (is_typedef_decl (t))
12123 type = DECL_ORIGINAL_TYPE (t);
12124 else
12125 type = TREE_TYPE (t);
12126 if (VAR_P (t)
12127 && VAR_HAD_UNKNOWN_BOUND (t)
12128 && type != error_mark_node)
12129 type = strip_array_domain (type);
12130 type = tsubst (type, args, complain, in_decl);
12131 }
12132 if (VAR_P (r))
12133 {
12134 /* Even if the original location is out of scope, the
12135 newly substituted one is not. */
12136 DECL_DEAD_FOR_LOCAL (r) = 0;
12137 DECL_INITIALIZED_P (r) = 0;
12138 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12139 if (type == error_mark_node)
12140 RETURN (error_mark_node);
12141 if (TREE_CODE (type) == FUNCTION_TYPE)
12142 {
12143 /* It may seem that this case cannot occur, since:
12144
12145 typedef void f();
12146 void g() { f x; }
12147
12148 declares a function, not a variable. However:
12149
12150 typedef void f();
12151 template <typename T> void g() { T t; }
12152 template void g<f>();
12153
12154 is an attempt to declare a variable with function
12155 type. */
12156 error ("variable %qD has function type",
12157 /* R is not yet sufficiently initialized, so we
12158 just use its name. */
12159 DECL_NAME (r));
12160 RETURN (error_mark_node);
12161 }
12162 type = complete_type (type);
12163 /* Wait until cp_finish_decl to set this again, to handle
12164 circular dependency (template/instantiate6.C). */
12165 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12166 type = check_var_type (DECL_NAME (r), type);
12167
12168 if (DECL_HAS_VALUE_EXPR_P (t))
12169 {
12170 tree ve = DECL_VALUE_EXPR (t);
12171 ve = tsubst_expr (ve, args, complain, in_decl,
12172 /*constant_expression_p=*/false);
12173 if (REFERENCE_REF_P (ve))
12174 {
12175 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12176 ve = TREE_OPERAND (ve, 0);
12177 }
12178 SET_DECL_VALUE_EXPR (r, ve);
12179 }
12180 if (CP_DECL_THREAD_LOCAL_P (r)
12181 && !processing_template_decl)
12182 set_decl_tls_model (r, decl_default_tls_model (r));
12183 }
12184 else if (DECL_SELF_REFERENCE_P (t))
12185 SET_DECL_SELF_REFERENCE_P (r);
12186 TREE_TYPE (r) = type;
12187 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12188 DECL_CONTEXT (r) = ctx;
12189 /* Clear out the mangled name and RTL for the instantiation. */
12190 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12191 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12192 SET_DECL_RTL (r, NULL);
12193 /* The initializer must not be expanded until it is required;
12194 see [temp.inst]. */
12195 DECL_INITIAL (r) = NULL_TREE;
12196 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12197 SET_DECL_RTL (r, NULL);
12198 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12199 if (VAR_P (r))
12200 {
12201 /* Possibly limit visibility based on template args. */
12202 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12203 if (DECL_VISIBILITY_SPECIFIED (t))
12204 {
12205 DECL_VISIBILITY_SPECIFIED (r) = 0;
12206 DECL_ATTRIBUTES (r)
12207 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12208 }
12209 determine_visibility (r);
12210 }
12211
12212 if (!local_p)
12213 {
12214 /* A static data member declaration is always marked
12215 external when it is declared in-class, even if an
12216 initializer is present. We mimic the non-template
12217 processing here. */
12218 DECL_EXTERNAL (r) = 1;
12219 if (DECL_NAMESPACE_SCOPE_P (t))
12220 DECL_NOT_REALLY_EXTERN (r) = 1;
12221
12222 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12223 SET_DECL_IMPLICIT_INSTANTIATION (r);
12224 register_specialization (r, gen_tmpl, argvec, false, hash);
12225 }
12226 else if (!cp_unevaluated_operand)
12227 register_local_specialization (r, t);
12228
12229 DECL_CHAIN (r) = NULL_TREE;
12230
12231 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12232 /*flags=*/0,
12233 args, complain, in_decl);
12234
12235 /* Preserve a typedef that names a type. */
12236 if (is_typedef_decl (r))
12237 {
12238 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12239 set_underlying_type (r);
12240 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12241 /* An alias template specialization can be dependent
12242 even if its underlying type is not. */
12243 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12244 }
12245
12246 layout_decl (r, 0);
12247 }
12248 break;
12249
12250 default:
12251 gcc_unreachable ();
12252 }
12253 #undef RETURN
12254
12255 out:
12256 /* Restore the file and line information. */
12257 input_location = saved_loc;
12258
12259 return r;
12260 }
12261
12262 /* Substitute into the ARG_TYPES of a function type.
12263 If END is a TREE_CHAIN, leave it and any following types
12264 un-substituted. */
12265
12266 static tree
12267 tsubst_arg_types (tree arg_types,
12268 tree args,
12269 tree end,
12270 tsubst_flags_t complain,
12271 tree in_decl)
12272 {
12273 tree remaining_arg_types;
12274 tree type = NULL_TREE;
12275 int i = 1;
12276 tree expanded_args = NULL_TREE;
12277 tree default_arg;
12278
12279 if (!arg_types || arg_types == void_list_node || arg_types == end)
12280 return arg_types;
12281
12282 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12283 args, end, complain, in_decl);
12284 if (remaining_arg_types == error_mark_node)
12285 return error_mark_node;
12286
12287 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12288 {
12289 /* For a pack expansion, perform substitution on the
12290 entire expression. Later on, we'll handle the arguments
12291 one-by-one. */
12292 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12293 args, complain, in_decl);
12294
12295 if (TREE_CODE (expanded_args) == TREE_VEC)
12296 /* So that we'll spin through the parameters, one by one. */
12297 i = TREE_VEC_LENGTH (expanded_args);
12298 else
12299 {
12300 /* We only partially substituted into the parameter
12301 pack. Our type is TYPE_PACK_EXPANSION. */
12302 type = expanded_args;
12303 expanded_args = NULL_TREE;
12304 }
12305 }
12306
12307 while (i > 0) {
12308 --i;
12309
12310 if (expanded_args)
12311 type = TREE_VEC_ELT (expanded_args, i);
12312 else if (!type)
12313 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12314
12315 if (type == error_mark_node)
12316 return error_mark_node;
12317 if (VOID_TYPE_P (type))
12318 {
12319 if (complain & tf_error)
12320 {
12321 error ("invalid parameter type %qT", type);
12322 if (in_decl)
12323 error ("in declaration %q+D", in_decl);
12324 }
12325 return error_mark_node;
12326 }
12327 /* DR 657. */
12328 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12329 return error_mark_node;
12330
12331 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12332 top-level qualifiers as required. */
12333 type = cv_unqualified (type_decays_to (type));
12334
12335 /* We do not substitute into default arguments here. The standard
12336 mandates that they be instantiated only when needed, which is
12337 done in build_over_call. */
12338 default_arg = TREE_PURPOSE (arg_types);
12339
12340 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12341 {
12342 /* We've instantiated a template before its default arguments
12343 have been parsed. This can happen for a nested template
12344 class, and is not an error unless we require the default
12345 argument in a call of this function. */
12346 remaining_arg_types =
12347 tree_cons (default_arg, type, remaining_arg_types);
12348 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12349 }
12350 else
12351 remaining_arg_types =
12352 hash_tree_cons (default_arg, type, remaining_arg_types);
12353 }
12354
12355 return remaining_arg_types;
12356 }
12357
12358 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12359 *not* handle the exception-specification for FNTYPE, because the
12360 initial substitution of explicitly provided template parameters
12361 during argument deduction forbids substitution into the
12362 exception-specification:
12363
12364 [temp.deduct]
12365
12366 All references in the function type of the function template to the
12367 corresponding template parameters are replaced by the specified tem-
12368 plate argument values. If a substitution in a template parameter or
12369 in the function type of the function template results in an invalid
12370 type, type deduction fails. [Note: The equivalent substitution in
12371 exception specifications is done only when the function is instanti-
12372 ated, at which point a program is ill-formed if the substitution
12373 results in an invalid type.] */
12374
12375 static tree
12376 tsubst_function_type (tree t,
12377 tree args,
12378 tsubst_flags_t complain,
12379 tree in_decl)
12380 {
12381 tree return_type;
12382 tree arg_types = NULL_TREE;
12383 tree fntype;
12384
12385 /* The TYPE_CONTEXT is not used for function/method types. */
12386 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12387
12388 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12389 failure. */
12390 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12391
12392 if (late_return_type_p)
12393 {
12394 /* Substitute the argument types. */
12395 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12396 complain, in_decl);
12397 if (arg_types == error_mark_node)
12398 return error_mark_node;
12399
12400 tree save_ccp = current_class_ptr;
12401 tree save_ccr = current_class_ref;
12402 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12403 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12404 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12405 if (do_inject)
12406 {
12407 /* DR 1207: 'this' is in scope in the trailing return type. */
12408 inject_this_parameter (this_type, cp_type_quals (this_type));
12409 }
12410
12411 /* Substitute the return type. */
12412 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12413
12414 if (do_inject)
12415 {
12416 current_class_ptr = save_ccp;
12417 current_class_ref = save_ccr;
12418 }
12419 }
12420 else
12421 /* Substitute the return type. */
12422 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12423
12424 if (return_type == error_mark_node)
12425 return error_mark_node;
12426 /* DR 486 clarifies that creation of a function type with an
12427 invalid return type is a deduction failure. */
12428 if (TREE_CODE (return_type) == ARRAY_TYPE
12429 || TREE_CODE (return_type) == FUNCTION_TYPE)
12430 {
12431 if (complain & tf_error)
12432 {
12433 if (TREE_CODE (return_type) == ARRAY_TYPE)
12434 error ("function returning an array");
12435 else
12436 error ("function returning a function");
12437 }
12438 return error_mark_node;
12439 }
12440 /* And DR 657. */
12441 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12442 return error_mark_node;
12443
12444 if (!late_return_type_p)
12445 {
12446 /* Substitute the argument types. */
12447 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12448 complain, in_decl);
12449 if (arg_types == error_mark_node)
12450 return error_mark_node;
12451 }
12452
12453 /* Construct a new type node and return it. */
12454 if (TREE_CODE (t) == FUNCTION_TYPE)
12455 {
12456 fntype = build_function_type (return_type, arg_types);
12457 fntype = apply_memfn_quals (fntype,
12458 type_memfn_quals (t),
12459 type_memfn_rqual (t));
12460 }
12461 else
12462 {
12463 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12464 /* Don't pick up extra function qualifiers from the basetype. */
12465 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12466 if (! MAYBE_CLASS_TYPE_P (r))
12467 {
12468 /* [temp.deduct]
12469
12470 Type deduction may fail for any of the following
12471 reasons:
12472
12473 -- Attempting to create "pointer to member of T" when T
12474 is not a class type. */
12475 if (complain & tf_error)
12476 error ("creating pointer to member function of non-class type %qT",
12477 r);
12478 return error_mark_node;
12479 }
12480
12481 fntype = build_method_type_directly (r, return_type,
12482 TREE_CHAIN (arg_types));
12483 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12484 }
12485 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12486
12487 if (late_return_type_p)
12488 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12489
12490 return fntype;
12491 }
12492
12493 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12494 ARGS into that specification, and return the substituted
12495 specification. If there is no specification, return NULL_TREE. */
12496
12497 static tree
12498 tsubst_exception_specification (tree fntype,
12499 tree args,
12500 tsubst_flags_t complain,
12501 tree in_decl,
12502 bool defer_ok)
12503 {
12504 tree specs;
12505 tree new_specs;
12506
12507 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12508 new_specs = NULL_TREE;
12509 if (specs && TREE_PURPOSE (specs))
12510 {
12511 /* A noexcept-specifier. */
12512 tree expr = TREE_PURPOSE (specs);
12513 if (TREE_CODE (expr) == INTEGER_CST)
12514 new_specs = expr;
12515 else if (defer_ok)
12516 {
12517 /* Defer instantiation of noexcept-specifiers to avoid
12518 excessive instantiations (c++/49107). */
12519 new_specs = make_node (DEFERRED_NOEXCEPT);
12520 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12521 {
12522 /* We already partially instantiated this member template,
12523 so combine the new args with the old. */
12524 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12525 = DEFERRED_NOEXCEPT_PATTERN (expr);
12526 DEFERRED_NOEXCEPT_ARGS (new_specs)
12527 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12528 }
12529 else
12530 {
12531 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12532 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12533 }
12534 }
12535 else
12536 new_specs = tsubst_copy_and_build
12537 (expr, args, complain, in_decl, /*function_p=*/false,
12538 /*integral_constant_expression_p=*/true);
12539 new_specs = build_noexcept_spec (new_specs, complain);
12540 }
12541 else if (specs)
12542 {
12543 if (! TREE_VALUE (specs))
12544 new_specs = specs;
12545 else
12546 while (specs)
12547 {
12548 tree spec;
12549 int i, len = 1;
12550 tree expanded_specs = NULL_TREE;
12551
12552 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12553 {
12554 /* Expand the pack expansion type. */
12555 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12556 args, complain,
12557 in_decl);
12558
12559 if (expanded_specs == error_mark_node)
12560 return error_mark_node;
12561 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12562 len = TREE_VEC_LENGTH (expanded_specs);
12563 else
12564 {
12565 /* We're substituting into a member template, so
12566 we got a TYPE_PACK_EXPANSION back. Add that
12567 expansion and move on. */
12568 gcc_assert (TREE_CODE (expanded_specs)
12569 == TYPE_PACK_EXPANSION);
12570 new_specs = add_exception_specifier (new_specs,
12571 expanded_specs,
12572 complain);
12573 specs = TREE_CHAIN (specs);
12574 continue;
12575 }
12576 }
12577
12578 for (i = 0; i < len; ++i)
12579 {
12580 if (expanded_specs)
12581 spec = TREE_VEC_ELT (expanded_specs, i);
12582 else
12583 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12584 if (spec == error_mark_node)
12585 return spec;
12586 new_specs = add_exception_specifier (new_specs, spec,
12587 complain);
12588 }
12589
12590 specs = TREE_CHAIN (specs);
12591 }
12592 }
12593 return new_specs;
12594 }
12595
12596 /* Take the tree structure T and replace template parameters used
12597 therein with the argument vector ARGS. IN_DECL is an associated
12598 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12599 Issue error and warning messages under control of COMPLAIN. Note
12600 that we must be relatively non-tolerant of extensions here, in
12601 order to preserve conformance; if we allow substitutions that
12602 should not be allowed, we may allow argument deductions that should
12603 not succeed, and therefore report ambiguous overload situations
12604 where there are none. In theory, we could allow the substitution,
12605 but indicate that it should have failed, and allow our caller to
12606 make sure that the right thing happens, but we don't try to do this
12607 yet.
12608
12609 This function is used for dealing with types, decls and the like;
12610 for expressions, use tsubst_expr or tsubst_copy. */
12611
12612 tree
12613 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12614 {
12615 enum tree_code code;
12616 tree type, r = NULL_TREE;
12617
12618 if (t == NULL_TREE || t == error_mark_node
12619 || t == integer_type_node
12620 || t == void_type_node
12621 || t == char_type_node
12622 || t == unknown_type_node
12623 || TREE_CODE (t) == NAMESPACE_DECL
12624 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12625 return t;
12626
12627 if (DECL_P (t))
12628 return tsubst_decl (t, args, complain);
12629
12630 if (args == NULL_TREE)
12631 return t;
12632
12633 code = TREE_CODE (t);
12634
12635 if (code == IDENTIFIER_NODE)
12636 type = IDENTIFIER_TYPE_VALUE (t);
12637 else
12638 type = TREE_TYPE (t);
12639
12640 gcc_assert (type != unknown_type_node);
12641
12642 /* Reuse typedefs. We need to do this to handle dependent attributes,
12643 such as attribute aligned. */
12644 if (TYPE_P (t)
12645 && typedef_variant_p (t))
12646 {
12647 tree decl = TYPE_NAME (t);
12648
12649 if (alias_template_specialization_p (t))
12650 {
12651 /* DECL represents an alias template and we want to
12652 instantiate it. */
12653 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12654 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12655 r = instantiate_alias_template (tmpl, gen_args, complain);
12656 }
12657 else if (DECL_CLASS_SCOPE_P (decl)
12658 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12659 && uses_template_parms (DECL_CONTEXT (decl)))
12660 {
12661 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12662 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12663 r = retrieve_specialization (tmpl, gen_args, 0);
12664 }
12665 else if (DECL_FUNCTION_SCOPE_P (decl)
12666 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12667 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12668 r = retrieve_local_specialization (decl);
12669 else
12670 /* The typedef is from a non-template context. */
12671 return t;
12672
12673 if (r)
12674 {
12675 r = TREE_TYPE (r);
12676 r = cp_build_qualified_type_real
12677 (r, cp_type_quals (t) | cp_type_quals (r),
12678 complain | tf_ignore_bad_quals);
12679 return r;
12680 }
12681 else
12682 {
12683 /* We don't have an instantiation yet, so drop the typedef. */
12684 int quals = cp_type_quals (t);
12685 t = DECL_ORIGINAL_TYPE (decl);
12686 t = cp_build_qualified_type_real (t, quals,
12687 complain | tf_ignore_bad_quals);
12688 }
12689 }
12690
12691 if (type
12692 && code != TYPENAME_TYPE
12693 && code != TEMPLATE_TYPE_PARM
12694 && code != IDENTIFIER_NODE
12695 && code != FUNCTION_TYPE
12696 && code != METHOD_TYPE)
12697 type = tsubst (type, args, complain, in_decl);
12698 if (type == error_mark_node)
12699 return error_mark_node;
12700
12701 switch (code)
12702 {
12703 case RECORD_TYPE:
12704 case UNION_TYPE:
12705 case ENUMERAL_TYPE:
12706 return tsubst_aggr_type (t, args, complain, in_decl,
12707 /*entering_scope=*/0);
12708
12709 case ERROR_MARK:
12710 case IDENTIFIER_NODE:
12711 case VOID_TYPE:
12712 case REAL_TYPE:
12713 case COMPLEX_TYPE:
12714 case VECTOR_TYPE:
12715 case BOOLEAN_TYPE:
12716 case NULLPTR_TYPE:
12717 case LANG_TYPE:
12718 return t;
12719
12720 case INTEGER_TYPE:
12721 if (t == integer_type_node)
12722 return t;
12723
12724 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
12725 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
12726 return t;
12727
12728 {
12729 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
12730
12731 max = tsubst_expr (omax, args, complain, in_decl,
12732 /*integral_constant_expression_p=*/false);
12733
12734 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12735 needed. */
12736 if (TREE_CODE (max) == NOP_EXPR
12737 && TREE_SIDE_EFFECTS (omax)
12738 && !TREE_TYPE (max))
12739 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
12740
12741 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12742 with TREE_SIDE_EFFECTS that indicates this is not an integral
12743 constant expression. */
12744 if (processing_template_decl
12745 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
12746 {
12747 gcc_assert (TREE_CODE (max) == NOP_EXPR);
12748 TREE_SIDE_EFFECTS (max) = 1;
12749 }
12750
12751 return compute_array_index_type (NULL_TREE, max, complain);
12752 }
12753
12754 case TEMPLATE_TYPE_PARM:
12755 case TEMPLATE_TEMPLATE_PARM:
12756 case BOUND_TEMPLATE_TEMPLATE_PARM:
12757 case TEMPLATE_PARM_INDEX:
12758 {
12759 int idx;
12760 int level;
12761 int levels;
12762 tree arg = NULL_TREE;
12763
12764 /* Early in template argument deduction substitution, we don't
12765 want to reduce the level of 'auto', or it will be confused
12766 with a normal template parm in subsequent deduction. */
12767 if (is_auto (t) && (complain & tf_partial))
12768 return t;
12769
12770 r = NULL_TREE;
12771
12772 gcc_assert (TREE_VEC_LENGTH (args) > 0);
12773 template_parm_level_and_index (t, &level, &idx);
12774
12775 levels = TMPL_ARGS_DEPTH (args);
12776 if (level <= levels
12777 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
12778 {
12779 arg = TMPL_ARG (args, level, idx);
12780
12781 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
12782 {
12783 /* See through ARGUMENT_PACK_SELECT arguments. */
12784 arg = ARGUMENT_PACK_SELECT_ARG (arg);
12785 /* If the selected argument is an expansion E, that most
12786 likely means we were called from
12787 gen_elem_of_pack_expansion_instantiation during the
12788 substituting of pack an argument pack (which Ith
12789 element is a pack expansion, where I is
12790 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12791 In this case, the Ith element resulting from this
12792 substituting is going to be a pack expansion, which
12793 pattern is the pattern of E. Let's return the
12794 pattern of E, and
12795 gen_elem_of_pack_expansion_instantiation will
12796 build the resulting pack expansion from it. */
12797 if (PACK_EXPANSION_P (arg))
12798 {
12799 /* Make sure we aren't throwing away arg info. */
12800 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
12801 arg = PACK_EXPANSION_PATTERN (arg);
12802 }
12803 }
12804 }
12805
12806 if (arg == error_mark_node)
12807 return error_mark_node;
12808 else if (arg != NULL_TREE)
12809 {
12810 if (ARGUMENT_PACK_P (arg))
12811 /* If ARG is an argument pack, we don't actually want to
12812 perform a substitution here, because substitutions
12813 for argument packs are only done
12814 element-by-element. We can get to this point when
12815 substituting the type of a non-type template
12816 parameter pack, when that type actually contains
12817 template parameter packs from an outer template, e.g.,
12818
12819 template<typename... Types> struct A {
12820 template<Types... Values> struct B { };
12821 }; */
12822 return t;
12823
12824 if (code == TEMPLATE_TYPE_PARM)
12825 {
12826 int quals;
12827 gcc_assert (TYPE_P (arg));
12828
12829 quals = cp_type_quals (arg) | cp_type_quals (t);
12830
12831 return cp_build_qualified_type_real
12832 (arg, quals, complain | tf_ignore_bad_quals);
12833 }
12834 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12835 {
12836 /* We are processing a type constructed from a
12837 template template parameter. */
12838 tree argvec = tsubst (TYPE_TI_ARGS (t),
12839 args, complain, in_decl);
12840 if (argvec == error_mark_node)
12841 return error_mark_node;
12842
12843 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12844 || TREE_CODE (arg) == TEMPLATE_DECL
12845 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12846
12847 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12848 /* Consider this code:
12849
12850 template <template <class> class Template>
12851 struct Internal {
12852 template <class Arg> using Bind = Template<Arg>;
12853 };
12854
12855 template <template <class> class Template, class Arg>
12856 using Instantiate = Template<Arg>; //#0
12857
12858 template <template <class> class Template,
12859 class Argument>
12860 using Bind =
12861 Instantiate<Internal<Template>::template Bind,
12862 Argument>; //#1
12863
12864 When #1 is parsed, the
12865 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12866 parameter `Template' in #0 matches the
12867 UNBOUND_CLASS_TEMPLATE representing the argument
12868 `Internal<Template>::template Bind'; We then want
12869 to assemble the type `Bind<Argument>' that can't
12870 be fully created right now, because
12871 `Internal<Template>' not being complete, the Bind
12872 template cannot be looked up in that context. So
12873 we need to "store" `Bind<Argument>' for later
12874 when the context of Bind becomes complete. Let's
12875 store that in a TYPENAME_TYPE. */
12876 return make_typename_type (TYPE_CONTEXT (arg),
12877 build_nt (TEMPLATE_ID_EXPR,
12878 TYPE_IDENTIFIER (arg),
12879 argvec),
12880 typename_type,
12881 complain);
12882
12883 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12884 are resolving nested-types in the signature of a
12885 member function templates. Otherwise ARG is a
12886 TEMPLATE_DECL and is the real template to be
12887 instantiated. */
12888 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12889 arg = TYPE_NAME (arg);
12890
12891 r = lookup_template_class (arg,
12892 argvec, in_decl,
12893 DECL_CONTEXT (arg),
12894 /*entering_scope=*/0,
12895 complain);
12896 return cp_build_qualified_type_real
12897 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12898 }
12899 else
12900 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12901 return convert_from_reference (unshare_expr (arg));
12902 }
12903
12904 if (level == 1)
12905 /* This can happen during the attempted tsubst'ing in
12906 unify. This means that we don't yet have any information
12907 about the template parameter in question. */
12908 return t;
12909
12910 /* If we get here, we must have been looking at a parm for a
12911 more deeply nested template. Make a new version of this
12912 template parameter, but with a lower level. */
12913 switch (code)
12914 {
12915 case TEMPLATE_TYPE_PARM:
12916 case TEMPLATE_TEMPLATE_PARM:
12917 case BOUND_TEMPLATE_TEMPLATE_PARM:
12918 if (cp_type_quals (t))
12919 {
12920 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12921 r = cp_build_qualified_type_real
12922 (r, cp_type_quals (t),
12923 complain | (code == TEMPLATE_TYPE_PARM
12924 ? tf_ignore_bad_quals : 0));
12925 }
12926 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
12927 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
12928 && (r = (TEMPLATE_PARM_DESCENDANTS
12929 (TEMPLATE_TYPE_PARM_INDEX (t))))
12930 && (r = TREE_TYPE (r))
12931 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
12932 /* Break infinite recursion when substituting the constraints
12933 of a constrained placeholder. */;
12934 else
12935 {
12936 r = copy_type (t);
12937 TEMPLATE_TYPE_PARM_INDEX (r)
12938 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12939 r, levels, args, complain);
12940 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12941 TYPE_MAIN_VARIANT (r) = r;
12942 TYPE_POINTER_TO (r) = NULL_TREE;
12943 TYPE_REFERENCE_TO (r) = NULL_TREE;
12944
12945 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12946 /* We have reduced the level of the template
12947 template parameter, but not the levels of its
12948 template parameters, so canonical_type_parameter
12949 will not be able to find the canonical template
12950 template parameter for this level. Thus, we
12951 require structural equality checking to compare
12952 TEMPLATE_TEMPLATE_PARMs. */
12953 SET_TYPE_STRUCTURAL_EQUALITY (r);
12954 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12955 SET_TYPE_STRUCTURAL_EQUALITY (r);
12956 else
12957 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12958
12959 /* Propagate constraints on placeholders. */
12960 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12961 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
12962 PLACEHOLDER_TYPE_CONSTRAINTS (r)
12963 = tsubst_constraint (constr, args, complain, in_decl);
12964
12965 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12966 {
12967 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12968 complain, in_decl);
12969 if (argvec == error_mark_node)
12970 return error_mark_node;
12971
12972 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12973 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12974 }
12975 }
12976 break;
12977
12978 case TEMPLATE_PARM_INDEX:
12979 r = reduce_template_parm_level (t, type, levels, args, complain);
12980 break;
12981
12982 default:
12983 gcc_unreachable ();
12984 }
12985
12986 return r;
12987 }
12988
12989 case TREE_LIST:
12990 {
12991 tree purpose, value, chain;
12992
12993 if (t == void_list_node)
12994 return t;
12995
12996 purpose = TREE_PURPOSE (t);
12997 if (purpose)
12998 {
12999 purpose = tsubst (purpose, args, complain, in_decl);
13000 if (purpose == error_mark_node)
13001 return error_mark_node;
13002 }
13003 value = TREE_VALUE (t);
13004 if (value)
13005 {
13006 value = tsubst (value, args, complain, in_decl);
13007 if (value == error_mark_node)
13008 return error_mark_node;
13009 }
13010 chain = TREE_CHAIN (t);
13011 if (chain && chain != void_type_node)
13012 {
13013 chain = tsubst (chain, args, complain, in_decl);
13014 if (chain == error_mark_node)
13015 return error_mark_node;
13016 }
13017 if (purpose == TREE_PURPOSE (t)
13018 && value == TREE_VALUE (t)
13019 && chain == TREE_CHAIN (t))
13020 return t;
13021 return hash_tree_cons (purpose, value, chain);
13022 }
13023
13024 case TREE_BINFO:
13025 /* We should never be tsubsting a binfo. */
13026 gcc_unreachable ();
13027
13028 case TREE_VEC:
13029 /* A vector of template arguments. */
13030 gcc_assert (!type);
13031 return tsubst_template_args (t, args, complain, in_decl);
13032
13033 case POINTER_TYPE:
13034 case REFERENCE_TYPE:
13035 {
13036 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13037 return t;
13038
13039 /* [temp.deduct]
13040
13041 Type deduction may fail for any of the following
13042 reasons:
13043
13044 -- Attempting to create a pointer to reference type.
13045 -- Attempting to create a reference to a reference type or
13046 a reference to void.
13047
13048 Core issue 106 says that creating a reference to a reference
13049 during instantiation is no longer a cause for failure. We
13050 only enforce this check in strict C++98 mode. */
13051 if ((TREE_CODE (type) == REFERENCE_TYPE
13052 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13053 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13054 {
13055 static location_t last_loc;
13056
13057 /* We keep track of the last time we issued this error
13058 message to avoid spewing a ton of messages during a
13059 single bad template instantiation. */
13060 if (complain & tf_error
13061 && last_loc != input_location)
13062 {
13063 if (VOID_TYPE_P (type))
13064 error ("forming reference to void");
13065 else if (code == POINTER_TYPE)
13066 error ("forming pointer to reference type %qT", type);
13067 else
13068 error ("forming reference to reference type %qT", type);
13069 last_loc = input_location;
13070 }
13071
13072 return error_mark_node;
13073 }
13074 else if (TREE_CODE (type) == FUNCTION_TYPE
13075 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13076 || type_memfn_rqual (type) != REF_QUAL_NONE))
13077 {
13078 if (complain & tf_error)
13079 {
13080 if (code == POINTER_TYPE)
13081 error ("forming pointer to qualified function type %qT",
13082 type);
13083 else
13084 error ("forming reference to qualified function type %qT",
13085 type);
13086 }
13087 return error_mark_node;
13088 }
13089 else if (code == POINTER_TYPE)
13090 {
13091 r = build_pointer_type (type);
13092 if (TREE_CODE (type) == METHOD_TYPE)
13093 r = build_ptrmemfunc_type (r);
13094 }
13095 else if (TREE_CODE (type) == REFERENCE_TYPE)
13096 /* In C++0x, during template argument substitution, when there is an
13097 attempt to create a reference to a reference type, reference
13098 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13099
13100 "If a template-argument for a template-parameter T names a type
13101 that is a reference to a type A, an attempt to create the type
13102 'lvalue reference to cv T' creates the type 'lvalue reference to
13103 A,' while an attempt to create the type type rvalue reference to
13104 cv T' creates the type T"
13105 */
13106 r = cp_build_reference_type
13107 (TREE_TYPE (type),
13108 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13109 else
13110 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13111 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13112
13113 if (r != error_mark_node)
13114 /* Will this ever be needed for TYPE_..._TO values? */
13115 layout_type (r);
13116
13117 return r;
13118 }
13119 case OFFSET_TYPE:
13120 {
13121 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13122 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13123 {
13124 /* [temp.deduct]
13125
13126 Type deduction may fail for any of the following
13127 reasons:
13128
13129 -- Attempting to create "pointer to member of T" when T
13130 is not a class type. */
13131 if (complain & tf_error)
13132 error ("creating pointer to member of non-class type %qT", r);
13133 return error_mark_node;
13134 }
13135 if (TREE_CODE (type) == REFERENCE_TYPE)
13136 {
13137 if (complain & tf_error)
13138 error ("creating pointer to member reference type %qT", type);
13139 return error_mark_node;
13140 }
13141 if (VOID_TYPE_P (type))
13142 {
13143 if (complain & tf_error)
13144 error ("creating pointer to member of type void");
13145 return error_mark_node;
13146 }
13147 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13148 if (TREE_CODE (type) == FUNCTION_TYPE)
13149 {
13150 /* The type of the implicit object parameter gets its
13151 cv-qualifiers from the FUNCTION_TYPE. */
13152 tree memptr;
13153 tree method_type
13154 = build_memfn_type (type, r, type_memfn_quals (type),
13155 type_memfn_rqual (type));
13156 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13157 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13158 complain);
13159 }
13160 else
13161 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13162 cp_type_quals (t),
13163 complain);
13164 }
13165 case FUNCTION_TYPE:
13166 case METHOD_TYPE:
13167 {
13168 tree fntype;
13169 tree specs;
13170 fntype = tsubst_function_type (t, args, complain, in_decl);
13171 if (fntype == error_mark_node)
13172 return error_mark_node;
13173
13174 /* Substitute the exception specification. */
13175 specs = tsubst_exception_specification (t, args, complain,
13176 in_decl, /*defer_ok*/true);
13177 if (specs == error_mark_node)
13178 return error_mark_node;
13179 if (specs)
13180 fntype = build_exception_variant (fntype, specs);
13181 return fntype;
13182 }
13183 case ARRAY_TYPE:
13184 {
13185 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13186 if (domain == error_mark_node)
13187 return error_mark_node;
13188
13189 /* As an optimization, we avoid regenerating the array type if
13190 it will obviously be the same as T. */
13191 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13192 return t;
13193
13194 /* These checks should match the ones in create_array_type_for_decl.
13195
13196 [temp.deduct]
13197
13198 The deduction may fail for any of the following reasons:
13199
13200 -- Attempting to create an array with an element type that
13201 is void, a function type, or a reference type, or [DR337]
13202 an abstract class type. */
13203 if (VOID_TYPE_P (type)
13204 || TREE_CODE (type) == FUNCTION_TYPE
13205 || (TREE_CODE (type) == ARRAY_TYPE
13206 && TYPE_DOMAIN (type) == NULL_TREE)
13207 || TREE_CODE (type) == REFERENCE_TYPE)
13208 {
13209 if (complain & tf_error)
13210 error ("creating array of %qT", type);
13211 return error_mark_node;
13212 }
13213
13214 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13215 return error_mark_node;
13216
13217 r = build_cplus_array_type (type, domain);
13218
13219 if (TYPE_USER_ALIGN (t))
13220 {
13221 TYPE_ALIGN (r) = TYPE_ALIGN (t);
13222 TYPE_USER_ALIGN (r) = 1;
13223 }
13224
13225 return r;
13226 }
13227
13228 case TYPENAME_TYPE:
13229 {
13230 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13231 in_decl, /*entering_scope=*/1);
13232 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13233 complain, in_decl);
13234
13235 if (ctx == error_mark_node || f == error_mark_node)
13236 return error_mark_node;
13237
13238 if (!MAYBE_CLASS_TYPE_P (ctx))
13239 {
13240 if (complain & tf_error)
13241 error ("%qT is not a class, struct, or union type", ctx);
13242 return error_mark_node;
13243 }
13244 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13245 {
13246 /* Normally, make_typename_type does not require that the CTX
13247 have complete type in order to allow things like:
13248
13249 template <class T> struct S { typename S<T>::X Y; };
13250
13251 But, such constructs have already been resolved by this
13252 point, so here CTX really should have complete type, unless
13253 it's a partial instantiation. */
13254 ctx = complete_type (ctx);
13255 if (!COMPLETE_TYPE_P (ctx))
13256 {
13257 if (complain & tf_error)
13258 cxx_incomplete_type_error (NULL_TREE, ctx);
13259 return error_mark_node;
13260 }
13261 }
13262
13263 f = make_typename_type (ctx, f, typename_type,
13264 complain | tf_keep_type_decl);
13265 if (f == error_mark_node)
13266 return f;
13267 if (TREE_CODE (f) == TYPE_DECL)
13268 {
13269 complain |= tf_ignore_bad_quals;
13270 f = TREE_TYPE (f);
13271 }
13272
13273 if (TREE_CODE (f) != TYPENAME_TYPE)
13274 {
13275 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13276 {
13277 if (complain & tf_error)
13278 error ("%qT resolves to %qT, which is not an enumeration type",
13279 t, f);
13280 else
13281 return error_mark_node;
13282 }
13283 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13284 {
13285 if (complain & tf_error)
13286 error ("%qT resolves to %qT, which is is not a class type",
13287 t, f);
13288 else
13289 return error_mark_node;
13290 }
13291 }
13292
13293 return cp_build_qualified_type_real
13294 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13295 }
13296
13297 case UNBOUND_CLASS_TEMPLATE:
13298 {
13299 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13300 in_decl, /*entering_scope=*/1);
13301 tree name = TYPE_IDENTIFIER (t);
13302 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13303
13304 if (ctx == error_mark_node || name == error_mark_node)
13305 return error_mark_node;
13306
13307 if (parm_list)
13308 parm_list = tsubst_template_parms (parm_list, args, complain);
13309 return make_unbound_class_template (ctx, name, parm_list, complain);
13310 }
13311
13312 case TYPEOF_TYPE:
13313 {
13314 tree type;
13315
13316 ++cp_unevaluated_operand;
13317 ++c_inhibit_evaluation_warnings;
13318
13319 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13320 complain, in_decl,
13321 /*integral_constant_expression_p=*/false);
13322
13323 --cp_unevaluated_operand;
13324 --c_inhibit_evaluation_warnings;
13325
13326 type = finish_typeof (type);
13327 return cp_build_qualified_type_real (type,
13328 cp_type_quals (t)
13329 | cp_type_quals (type),
13330 complain);
13331 }
13332
13333 case DECLTYPE_TYPE:
13334 {
13335 tree type;
13336
13337 ++cp_unevaluated_operand;
13338 ++c_inhibit_evaluation_warnings;
13339
13340 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13341 complain|tf_decltype, in_decl,
13342 /*function_p*/false,
13343 /*integral_constant_expression*/false);
13344
13345 --cp_unevaluated_operand;
13346 --c_inhibit_evaluation_warnings;
13347
13348 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13349 type = lambda_capture_field_type (type,
13350 DECLTYPE_FOR_INIT_CAPTURE (t));
13351 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13352 type = lambda_proxy_type (type);
13353 else
13354 {
13355 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13356 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13357 && EXPR_P (type))
13358 /* In a template ~id could be either a complement expression
13359 or an unqualified-id naming a destructor; if instantiating
13360 it produces an expression, it's not an id-expression or
13361 member access. */
13362 id = false;
13363 type = finish_decltype_type (type, id, complain);
13364 }
13365 return cp_build_qualified_type_real (type,
13366 cp_type_quals (t)
13367 | cp_type_quals (type),
13368 complain | tf_ignore_bad_quals);
13369 }
13370
13371 case UNDERLYING_TYPE:
13372 {
13373 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13374 complain, in_decl);
13375 return finish_underlying_type (type);
13376 }
13377
13378 case TYPE_ARGUMENT_PACK:
13379 case NONTYPE_ARGUMENT_PACK:
13380 {
13381 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13382 tree packed_out =
13383 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13384 args,
13385 complain,
13386 in_decl);
13387 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13388
13389 /* For template nontype argument packs, also substitute into
13390 the type. */
13391 if (code == NONTYPE_ARGUMENT_PACK)
13392 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13393
13394 return r;
13395 }
13396 break;
13397
13398 case VOID_CST:
13399 case INTEGER_CST:
13400 case REAL_CST:
13401 case STRING_CST:
13402 case PLUS_EXPR:
13403 case MINUS_EXPR:
13404 case NEGATE_EXPR:
13405 case NOP_EXPR:
13406 case INDIRECT_REF:
13407 case ADDR_EXPR:
13408 case CALL_EXPR:
13409 case ARRAY_REF:
13410 case SCOPE_REF:
13411 /* We should use one of the expression tsubsts for these codes. */
13412 gcc_unreachable ();
13413
13414 default:
13415 sorry ("use of %qs in template", get_tree_code_name (code));
13416 return error_mark_node;
13417 }
13418 }
13419
13420 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13421 type of the expression on the left-hand side of the "." or "->"
13422 operator. */
13423
13424 static tree
13425 tsubst_baselink (tree baselink, tree object_type,
13426 tree args, tsubst_flags_t complain, tree in_decl)
13427 {
13428 tree name;
13429 tree qualifying_scope;
13430 tree fns;
13431 tree optype;
13432 tree template_args = 0;
13433 bool template_id_p = false;
13434 bool qualified = BASELINK_QUALIFIED_P (baselink);
13435
13436 /* A baselink indicates a function from a base class. Both the
13437 BASELINK_ACCESS_BINFO and the base class referenced may
13438 indicate bases of the template class, rather than the
13439 instantiated class. In addition, lookups that were not
13440 ambiguous before may be ambiguous now. Therefore, we perform
13441 the lookup again. */
13442 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13443 qualifying_scope = tsubst (qualifying_scope, args,
13444 complain, in_decl);
13445 fns = BASELINK_FUNCTIONS (baselink);
13446 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13447 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13448 {
13449 template_id_p = true;
13450 template_args = TREE_OPERAND (fns, 1);
13451 fns = TREE_OPERAND (fns, 0);
13452 if (template_args)
13453 template_args = tsubst_template_args (template_args, args,
13454 complain, in_decl);
13455 }
13456 name = DECL_NAME (get_first_fn (fns));
13457 if (IDENTIFIER_TYPENAME_P (name))
13458 name = mangle_conv_op_name_for_type (optype);
13459 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13460 if (!baselink)
13461 return error_mark_node;
13462
13463 /* If lookup found a single function, mark it as used at this
13464 point. (If it lookup found multiple functions the one selected
13465 later by overload resolution will be marked as used at that
13466 point.) */
13467 if (BASELINK_P (baselink))
13468 fns = BASELINK_FUNCTIONS (baselink);
13469 if (!template_id_p && !really_overloaded_fn (fns)
13470 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13471 return error_mark_node;
13472
13473 /* Add back the template arguments, if present. */
13474 if (BASELINK_P (baselink) && template_id_p)
13475 BASELINK_FUNCTIONS (baselink)
13476 = build_nt (TEMPLATE_ID_EXPR,
13477 BASELINK_FUNCTIONS (baselink),
13478 template_args);
13479 /* Update the conversion operator type. */
13480 BASELINK_OPTYPE (baselink) = optype;
13481
13482 if (!object_type)
13483 object_type = current_class_type;
13484
13485 if (qualified)
13486 baselink = adjust_result_of_qualified_name_lookup (baselink,
13487 qualifying_scope,
13488 object_type);
13489 return baselink;
13490 }
13491
13492 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13493 true if the qualified-id will be a postfix-expression in-and-of
13494 itself; false if more of the postfix-expression follows the
13495 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13496 of "&". */
13497
13498 static tree
13499 tsubst_qualified_id (tree qualified_id, tree args,
13500 tsubst_flags_t complain, tree in_decl,
13501 bool done, bool address_p)
13502 {
13503 tree expr;
13504 tree scope;
13505 tree name;
13506 bool is_template;
13507 tree template_args;
13508 location_t loc = UNKNOWN_LOCATION;
13509
13510 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13511
13512 /* Figure out what name to look up. */
13513 name = TREE_OPERAND (qualified_id, 1);
13514 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13515 {
13516 is_template = true;
13517 loc = EXPR_LOCATION (name);
13518 template_args = TREE_OPERAND (name, 1);
13519 if (template_args)
13520 template_args = tsubst_template_args (template_args, args,
13521 complain, in_decl);
13522 name = TREE_OPERAND (name, 0);
13523 }
13524 else
13525 {
13526 is_template = false;
13527 template_args = NULL_TREE;
13528 }
13529
13530 /* Substitute into the qualifying scope. When there are no ARGS, we
13531 are just trying to simplify a non-dependent expression. In that
13532 case the qualifying scope may be dependent, and, in any case,
13533 substituting will not help. */
13534 scope = TREE_OPERAND (qualified_id, 0);
13535 if (args)
13536 {
13537 scope = tsubst (scope, args, complain, in_decl);
13538 expr = tsubst_copy (name, args, complain, in_decl);
13539 }
13540 else
13541 expr = name;
13542
13543 if (dependent_scope_p (scope))
13544 {
13545 if (is_template)
13546 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13547 return build_qualified_name (NULL_TREE, scope, expr,
13548 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13549 }
13550
13551 if (!BASELINK_P (name) && !DECL_P (expr))
13552 {
13553 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13554 {
13555 /* A BIT_NOT_EXPR is used to represent a destructor. */
13556 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13557 {
13558 error ("qualifying type %qT does not match destructor name ~%qT",
13559 scope, TREE_OPERAND (expr, 0));
13560 expr = error_mark_node;
13561 }
13562 else
13563 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13564 /*is_type_p=*/0, false);
13565 }
13566 else
13567 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13568 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13569 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13570 {
13571 if (complain & tf_error)
13572 {
13573 error ("dependent-name %qE is parsed as a non-type, but "
13574 "instantiation yields a type", qualified_id);
13575 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13576 }
13577 return error_mark_node;
13578 }
13579 }
13580
13581 if (DECL_P (expr))
13582 {
13583 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13584 scope);
13585 /* Remember that there was a reference to this entity. */
13586 if (!mark_used (expr, complain) && !(complain & tf_error))
13587 return error_mark_node;
13588 }
13589
13590 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13591 {
13592 if (complain & tf_error)
13593 qualified_name_lookup_error (scope,
13594 TREE_OPERAND (qualified_id, 1),
13595 expr, input_location);
13596 return error_mark_node;
13597 }
13598
13599 if (is_template)
13600 expr = lookup_template_function (expr, template_args);
13601
13602 if (expr == error_mark_node && complain & tf_error)
13603 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
13604 expr, input_location);
13605 else if (TYPE_P (scope))
13606 {
13607 expr = (adjust_result_of_qualified_name_lookup
13608 (expr, scope, current_nonlambda_class_type ()));
13609 expr = (finish_qualified_id_expr
13610 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
13611 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
13612 /*template_arg_p=*/false, complain));
13613 }
13614
13615 /* Expressions do not generally have reference type. */
13616 if (TREE_CODE (expr) != SCOPE_REF
13617 /* However, if we're about to form a pointer-to-member, we just
13618 want the referenced member referenced. */
13619 && TREE_CODE (expr) != OFFSET_REF)
13620 expr = convert_from_reference (expr);
13621
13622 return expr;
13623 }
13624
13625 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
13626 initializer, DECL is the substituted VAR_DECL. Other arguments are as
13627 for tsubst. */
13628
13629 static tree
13630 tsubst_init (tree init, tree decl, tree args,
13631 tsubst_flags_t complain, tree in_decl)
13632 {
13633 if (!init)
13634 return NULL_TREE;
13635
13636 init = tsubst_expr (init, args, complain, in_decl, false);
13637
13638 if (!init)
13639 {
13640 /* If we had an initializer but it
13641 instantiated to nothing,
13642 value-initialize the object. This will
13643 only occur when the initializer was a
13644 pack expansion where the parameter packs
13645 used in that expansion were of length
13646 zero. */
13647 init = build_value_init (TREE_TYPE (decl),
13648 complain);
13649 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13650 init = get_target_expr_sfinae (init, complain);
13651 }
13652
13653 return init;
13654 }
13655
13656 /* Like tsubst, but deals with expressions. This function just replaces
13657 template parms; to finish processing the resultant expression, use
13658 tsubst_copy_and_build or tsubst_expr. */
13659
13660 static tree
13661 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13662 {
13663 enum tree_code code;
13664 tree r;
13665
13666 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
13667 return t;
13668
13669 code = TREE_CODE (t);
13670
13671 switch (code)
13672 {
13673 case PARM_DECL:
13674 r = retrieve_local_specialization (t);
13675
13676 if (r == NULL_TREE)
13677 {
13678 /* We get here for a use of 'this' in an NSDMI. */
13679 if (DECL_NAME (t) == this_identifier
13680 && current_function_decl
13681 && DECL_CONSTRUCTOR_P (current_function_decl))
13682 return current_class_ptr;
13683
13684 /* This can happen for a parameter name used later in a function
13685 declaration (such as in a late-specified return type). Just
13686 make a dummy decl, since it's only used for its type. */
13687 gcc_assert (cp_unevaluated_operand != 0);
13688 r = tsubst_decl (t, args, complain);
13689 /* Give it the template pattern as its context; its true context
13690 hasn't been instantiated yet and this is good enough for
13691 mangling. */
13692 DECL_CONTEXT (r) = DECL_CONTEXT (t);
13693 }
13694
13695 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13696 r = ARGUMENT_PACK_SELECT_ARG (r);
13697 if (!mark_used (r, complain) && !(complain & tf_error))
13698 return error_mark_node;
13699 return r;
13700
13701 case CONST_DECL:
13702 {
13703 tree enum_type;
13704 tree v;
13705
13706 if (DECL_TEMPLATE_PARM_P (t))
13707 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
13708 /* There is no need to substitute into namespace-scope
13709 enumerators. */
13710 if (DECL_NAMESPACE_SCOPE_P (t))
13711 return t;
13712 /* If ARGS is NULL, then T is known to be non-dependent. */
13713 if (args == NULL_TREE)
13714 return scalar_constant_value (t);
13715
13716 /* Unfortunately, we cannot just call lookup_name here.
13717 Consider:
13718
13719 template <int I> int f() {
13720 enum E { a = I };
13721 struct S { void g() { E e = a; } };
13722 };
13723
13724 When we instantiate f<7>::S::g(), say, lookup_name is not
13725 clever enough to find f<7>::a. */
13726 enum_type
13727 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13728 /*entering_scope=*/0);
13729
13730 for (v = TYPE_VALUES (enum_type);
13731 v != NULL_TREE;
13732 v = TREE_CHAIN (v))
13733 if (TREE_PURPOSE (v) == DECL_NAME (t))
13734 return TREE_VALUE (v);
13735
13736 /* We didn't find the name. That should never happen; if
13737 name-lookup found it during preliminary parsing, we
13738 should find it again here during instantiation. */
13739 gcc_unreachable ();
13740 }
13741 return t;
13742
13743 case FIELD_DECL:
13744 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13745 {
13746 /* Check for a local specialization set up by
13747 tsubst_pack_expansion. */
13748 if (tree r = retrieve_local_specialization (t))
13749 {
13750 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13751 r = ARGUMENT_PACK_SELECT_ARG (r);
13752 return r;
13753 }
13754
13755 /* When retrieving a capture pack from a generic lambda, remove the
13756 lambda call op's own template argument list from ARGS. Only the
13757 template arguments active for the closure type should be used to
13758 retrieve the pack specialization. */
13759 if (LAMBDA_FUNCTION_P (current_function_decl)
13760 && (template_class_depth (DECL_CONTEXT (t))
13761 != TMPL_ARGS_DEPTH (args)))
13762 args = strip_innermost_template_args (args, 1);
13763
13764 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13765 tsubst_decl put in the hash table. */
13766 return retrieve_specialization (t, args, 0);
13767 }
13768
13769 if (DECL_CONTEXT (t))
13770 {
13771 tree ctx;
13772
13773 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13774 /*entering_scope=*/1);
13775 if (ctx != DECL_CONTEXT (t))
13776 {
13777 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
13778 if (!r)
13779 {
13780 if (complain & tf_error)
13781 error ("using invalid field %qD", t);
13782 return error_mark_node;
13783 }
13784 return r;
13785 }
13786 }
13787
13788 return t;
13789
13790 case VAR_DECL:
13791 case FUNCTION_DECL:
13792 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
13793 r = tsubst (t, args, complain, in_decl);
13794 else if (local_variable_p (t))
13795 {
13796 r = retrieve_local_specialization (t);
13797 if (r == NULL_TREE)
13798 {
13799 /* First try name lookup to find the instantiation. */
13800 r = lookup_name (DECL_NAME (t));
13801 if (r)
13802 {
13803 /* Make sure that the one we found is the one we want. */
13804 tree ctx = DECL_CONTEXT (t);
13805 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
13806 ctx = tsubst (ctx, args, complain, in_decl);
13807 if (ctx != DECL_CONTEXT (r))
13808 r = NULL_TREE;
13809 }
13810
13811 if (r)
13812 /* OK */;
13813 else
13814 {
13815 /* This can happen for a variable used in a
13816 late-specified return type of a local lambda, or for a
13817 local static or constant. Building a new VAR_DECL
13818 should be OK in all those cases. */
13819 r = tsubst_decl (t, args, complain);
13820 if (decl_maybe_constant_var_p (r))
13821 {
13822 /* We can't call cp_finish_decl, so handle the
13823 initializer by hand. */
13824 tree init = tsubst_init (DECL_INITIAL (t), r, args,
13825 complain, in_decl);
13826 if (!processing_template_decl)
13827 init = maybe_constant_init (init);
13828 if (processing_template_decl
13829 ? potential_constant_expression (init)
13830 : reduced_constant_expression_p (init))
13831 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13832 = TREE_CONSTANT (r) = true;
13833 DECL_INITIAL (r) = init;
13834 }
13835 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13836 || decl_constant_var_p (r)
13837 || errorcount || sorrycount);
13838 if (!processing_template_decl)
13839 {
13840 if (TREE_STATIC (r))
13841 rest_of_decl_compilation (r, toplevel_bindings_p (),
13842 at_eof);
13843 else
13844 r = process_outer_var_ref (r, complain);
13845 }
13846 }
13847 /* Remember this for subsequent uses. */
13848 if (local_specializations)
13849 register_local_specialization (r, t);
13850 }
13851 }
13852 else
13853 r = t;
13854 if (!mark_used (r, complain) && !(complain & tf_error))
13855 return error_mark_node;
13856 return r;
13857
13858 case NAMESPACE_DECL:
13859 return t;
13860
13861 case OVERLOAD:
13862 /* An OVERLOAD will always be a non-dependent overload set; an
13863 overload set from function scope will just be represented with an
13864 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13865 gcc_assert (!uses_template_parms (t));
13866 return t;
13867
13868 case BASELINK:
13869 return tsubst_baselink (t, current_nonlambda_class_type (),
13870 args, complain, in_decl);
13871
13872 case TEMPLATE_DECL:
13873 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13874 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13875 args, complain, in_decl);
13876 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13877 return tsubst (t, args, complain, in_decl);
13878 else if (DECL_CLASS_SCOPE_P (t)
13879 && uses_template_parms (DECL_CONTEXT (t)))
13880 {
13881 /* Template template argument like the following example need
13882 special treatment:
13883
13884 template <template <class> class TT> struct C {};
13885 template <class T> struct D {
13886 template <class U> struct E {};
13887 C<E> c; // #1
13888 };
13889 D<int> d; // #2
13890
13891 We are processing the template argument `E' in #1 for
13892 the template instantiation #2. Originally, `E' is a
13893 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13894 have to substitute this with one having context `D<int>'. */
13895
13896 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13897 return lookup_field (context, DECL_NAME(t), 0, false);
13898 }
13899 else
13900 /* Ordinary template template argument. */
13901 return t;
13902
13903 case CAST_EXPR:
13904 case REINTERPRET_CAST_EXPR:
13905 case CONST_CAST_EXPR:
13906 case STATIC_CAST_EXPR:
13907 case DYNAMIC_CAST_EXPR:
13908 case IMPLICIT_CONV_EXPR:
13909 case CONVERT_EXPR:
13910 case NOP_EXPR:
13911 {
13912 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13913 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13914 return build1 (code, type, op0);
13915 }
13916
13917 case SIZEOF_EXPR:
13918 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13919 {
13920
13921 tree expanded, op = TREE_OPERAND (t, 0);
13922 int len = 0;
13923
13924 if (SIZEOF_EXPR_TYPE_P (t))
13925 op = TREE_TYPE (op);
13926
13927 ++cp_unevaluated_operand;
13928 ++c_inhibit_evaluation_warnings;
13929 /* We only want to compute the number of arguments. */
13930 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13931 --cp_unevaluated_operand;
13932 --c_inhibit_evaluation_warnings;
13933
13934 if (TREE_CODE (expanded) == TREE_VEC)
13935 len = TREE_VEC_LENGTH (expanded);
13936
13937 if (expanded == error_mark_node)
13938 return error_mark_node;
13939 else if (PACK_EXPANSION_P (expanded)
13940 || (TREE_CODE (expanded) == TREE_VEC
13941 && len > 0
13942 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13943 {
13944 if (TREE_CODE (expanded) == TREE_VEC)
13945 expanded = TREE_VEC_ELT (expanded, len - 1);
13946
13947 if (TYPE_P (expanded))
13948 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13949 complain & tf_error);
13950 else
13951 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13952 complain & tf_error);
13953 }
13954 else
13955 return build_int_cst (size_type_node, len);
13956 }
13957 if (SIZEOF_EXPR_TYPE_P (t))
13958 {
13959 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13960 args, complain, in_decl);
13961 r = build1 (NOP_EXPR, r, error_mark_node);
13962 r = build1 (SIZEOF_EXPR,
13963 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13964 SIZEOF_EXPR_TYPE_P (r) = 1;
13965 return r;
13966 }
13967 /* Fall through */
13968
13969 case INDIRECT_REF:
13970 case NEGATE_EXPR:
13971 case TRUTH_NOT_EXPR:
13972 case BIT_NOT_EXPR:
13973 case ADDR_EXPR:
13974 case UNARY_PLUS_EXPR: /* Unary + */
13975 case ALIGNOF_EXPR:
13976 case AT_ENCODE_EXPR:
13977 case ARROW_EXPR:
13978 case THROW_EXPR:
13979 case TYPEID_EXPR:
13980 case REALPART_EXPR:
13981 case IMAGPART_EXPR:
13982 case PAREN_EXPR:
13983 {
13984 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13985 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13986 return build1 (code, type, op0);
13987 }
13988
13989 case COMPONENT_REF:
13990 {
13991 tree object;
13992 tree name;
13993
13994 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13995 name = TREE_OPERAND (t, 1);
13996 if (TREE_CODE (name) == BIT_NOT_EXPR)
13997 {
13998 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13999 complain, in_decl);
14000 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14001 }
14002 else if (TREE_CODE (name) == SCOPE_REF
14003 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14004 {
14005 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14006 complain, in_decl);
14007 name = TREE_OPERAND (name, 1);
14008 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14009 complain, in_decl);
14010 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14011 name = build_qualified_name (/*type=*/NULL_TREE,
14012 base, name,
14013 /*template_p=*/false);
14014 }
14015 else if (BASELINK_P (name))
14016 name = tsubst_baselink (name,
14017 non_reference (TREE_TYPE (object)),
14018 args, complain,
14019 in_decl);
14020 else
14021 name = tsubst_copy (name, args, complain, in_decl);
14022 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14023 }
14024
14025 case PLUS_EXPR:
14026 case MINUS_EXPR:
14027 case MULT_EXPR:
14028 case TRUNC_DIV_EXPR:
14029 case CEIL_DIV_EXPR:
14030 case FLOOR_DIV_EXPR:
14031 case ROUND_DIV_EXPR:
14032 case EXACT_DIV_EXPR:
14033 case BIT_AND_EXPR:
14034 case BIT_IOR_EXPR:
14035 case BIT_XOR_EXPR:
14036 case TRUNC_MOD_EXPR:
14037 case FLOOR_MOD_EXPR:
14038 case TRUTH_ANDIF_EXPR:
14039 case TRUTH_ORIF_EXPR:
14040 case TRUTH_AND_EXPR:
14041 case TRUTH_OR_EXPR:
14042 case RSHIFT_EXPR:
14043 case LSHIFT_EXPR:
14044 case RROTATE_EXPR:
14045 case LROTATE_EXPR:
14046 case EQ_EXPR:
14047 case NE_EXPR:
14048 case MAX_EXPR:
14049 case MIN_EXPR:
14050 case LE_EXPR:
14051 case GE_EXPR:
14052 case LT_EXPR:
14053 case GT_EXPR:
14054 case COMPOUND_EXPR:
14055 case DOTSTAR_EXPR:
14056 case MEMBER_REF:
14057 case PREDECREMENT_EXPR:
14058 case PREINCREMENT_EXPR:
14059 case POSTDECREMENT_EXPR:
14060 case POSTINCREMENT_EXPR:
14061 {
14062 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14063 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14064 return build_nt (code, op0, op1);
14065 }
14066
14067 case SCOPE_REF:
14068 {
14069 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14070 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14071 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14072 QUALIFIED_NAME_IS_TEMPLATE (t));
14073 }
14074
14075 case ARRAY_REF:
14076 {
14077 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14078 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14079 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14080 }
14081
14082 case CALL_EXPR:
14083 {
14084 int n = VL_EXP_OPERAND_LENGTH (t);
14085 tree result = build_vl_exp (CALL_EXPR, n);
14086 int i;
14087 for (i = 0; i < n; i++)
14088 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14089 complain, in_decl);
14090 return result;
14091 }
14092
14093 case COND_EXPR:
14094 case MODOP_EXPR:
14095 case PSEUDO_DTOR_EXPR:
14096 case VEC_PERM_EXPR:
14097 {
14098 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14099 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14100 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14101 r = build_nt (code, op0, op1, op2);
14102 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14103 return r;
14104 }
14105
14106 case NEW_EXPR:
14107 {
14108 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14109 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14110 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14111 r = build_nt (code, op0, op1, op2);
14112 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14113 return r;
14114 }
14115
14116 case DELETE_EXPR:
14117 {
14118 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14119 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14120 r = build_nt (code, op0, op1);
14121 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14122 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14123 return r;
14124 }
14125
14126 case TEMPLATE_ID_EXPR:
14127 {
14128 /* Substituted template arguments */
14129 tree fn = TREE_OPERAND (t, 0);
14130 tree targs = TREE_OPERAND (t, 1);
14131
14132 fn = tsubst_copy (fn, args, complain, in_decl);
14133 if (targs)
14134 targs = tsubst_template_args (targs, args, complain, in_decl);
14135
14136 return lookup_template_function (fn, targs);
14137 }
14138
14139 case TREE_LIST:
14140 {
14141 tree purpose, value, chain;
14142
14143 if (t == void_list_node)
14144 return t;
14145
14146 purpose = TREE_PURPOSE (t);
14147 if (purpose)
14148 purpose = tsubst_copy (purpose, args, complain, in_decl);
14149 value = TREE_VALUE (t);
14150 if (value)
14151 value = tsubst_copy (value, args, complain, in_decl);
14152 chain = TREE_CHAIN (t);
14153 if (chain && chain != void_type_node)
14154 chain = tsubst_copy (chain, args, complain, in_decl);
14155 if (purpose == TREE_PURPOSE (t)
14156 && value == TREE_VALUE (t)
14157 && chain == TREE_CHAIN (t))
14158 return t;
14159 return tree_cons (purpose, value, chain);
14160 }
14161
14162 case RECORD_TYPE:
14163 case UNION_TYPE:
14164 case ENUMERAL_TYPE:
14165 case INTEGER_TYPE:
14166 case TEMPLATE_TYPE_PARM:
14167 case TEMPLATE_TEMPLATE_PARM:
14168 case BOUND_TEMPLATE_TEMPLATE_PARM:
14169 case TEMPLATE_PARM_INDEX:
14170 case POINTER_TYPE:
14171 case REFERENCE_TYPE:
14172 case OFFSET_TYPE:
14173 case FUNCTION_TYPE:
14174 case METHOD_TYPE:
14175 case ARRAY_TYPE:
14176 case TYPENAME_TYPE:
14177 case UNBOUND_CLASS_TEMPLATE:
14178 case TYPEOF_TYPE:
14179 case DECLTYPE_TYPE:
14180 case TYPE_DECL:
14181 return tsubst (t, args, complain, in_decl);
14182
14183 case USING_DECL:
14184 t = DECL_NAME (t);
14185 /* Fall through. */
14186 case IDENTIFIER_NODE:
14187 if (IDENTIFIER_TYPENAME_P (t))
14188 {
14189 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14190 return mangle_conv_op_name_for_type (new_type);
14191 }
14192 else
14193 return t;
14194
14195 case CONSTRUCTOR:
14196 /* This is handled by tsubst_copy_and_build. */
14197 gcc_unreachable ();
14198
14199 case VA_ARG_EXPR:
14200 {
14201 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14202 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14203 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14204 }
14205
14206 case CLEANUP_POINT_EXPR:
14207 /* We shouldn't have built any of these during initial template
14208 generation. Instead, they should be built during instantiation
14209 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14210 gcc_unreachable ();
14211
14212 case OFFSET_REF:
14213 {
14214 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14215 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14216 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14217 r = build2 (code, type, op0, op1);
14218 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14219 if (!mark_used (TREE_OPERAND (r, 1), complain)
14220 && !(complain & tf_error))
14221 return error_mark_node;
14222 return r;
14223 }
14224
14225 case EXPR_PACK_EXPANSION:
14226 error ("invalid use of pack expansion expression");
14227 return error_mark_node;
14228
14229 case NONTYPE_ARGUMENT_PACK:
14230 error ("use %<...%> to expand argument pack");
14231 return error_mark_node;
14232
14233 case VOID_CST:
14234 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14235 return t;
14236
14237 case INTEGER_CST:
14238 case REAL_CST:
14239 case STRING_CST:
14240 case COMPLEX_CST:
14241 {
14242 /* Instantiate any typedefs in the type. */
14243 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14244 r = fold_convert (type, t);
14245 gcc_assert (TREE_CODE (r) == code);
14246 return r;
14247 }
14248
14249 case PTRMEM_CST:
14250 /* These can sometimes show up in a partial instantiation, but never
14251 involve template parms. */
14252 gcc_assert (!uses_template_parms (t));
14253 return t;
14254
14255 case UNARY_LEFT_FOLD_EXPR:
14256 return tsubst_unary_left_fold (t, args, complain, in_decl);
14257 case UNARY_RIGHT_FOLD_EXPR:
14258 return tsubst_unary_right_fold (t, args, complain, in_decl);
14259 case BINARY_LEFT_FOLD_EXPR:
14260 return tsubst_binary_left_fold (t, args, complain, in_decl);
14261 case BINARY_RIGHT_FOLD_EXPR:
14262 return tsubst_binary_right_fold (t, args, complain, in_decl);
14263
14264 default:
14265 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
14266 gcc_checking_assert (false);
14267 return t;
14268 }
14269 }
14270
14271 /* Helper function for tsubst_omp_clauses, used for instantiation of
14272 OMP_CLAUSE_DECL of clauses that handles also OpenMP array sections
14273 represented with TREE_LIST. */
14274
14275 static tree
14276 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14277 tree in_decl)
14278 {
14279 if (TREE_CODE (decl) == TREE_LIST)
14280 {
14281 tree low_bound
14282 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14283 /*integral_constant_expression_p=*/false);
14284 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14285 /*integral_constant_expression_p=*/false);
14286 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14287 in_decl);
14288 if (TREE_PURPOSE (decl) == low_bound
14289 && TREE_VALUE (decl) == length
14290 && TREE_CHAIN (decl) == chain)
14291 return decl;
14292 return tree_cons (low_bound, length, chain);
14293 }
14294 return tsubst_copy (decl, args, complain, in_decl);
14295 }
14296
14297 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14298
14299 static tree
14300 tsubst_omp_clauses (tree clauses, bool declare_simd,
14301 tree args, tsubst_flags_t complain, tree in_decl)
14302 {
14303 tree new_clauses = NULL, nc, oc;
14304
14305 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14306 {
14307 nc = copy_node (oc);
14308 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14309 new_clauses = nc;
14310
14311 switch (OMP_CLAUSE_CODE (nc))
14312 {
14313 case OMP_CLAUSE_LASTPRIVATE:
14314 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14315 {
14316 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14317 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14318 in_decl, /*integral_constant_expression_p=*/false);
14319 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14320 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14321 }
14322 /* FALLTHRU */
14323 case OMP_CLAUSE_PRIVATE:
14324 case OMP_CLAUSE_SHARED:
14325 case OMP_CLAUSE_FIRSTPRIVATE:
14326 case OMP_CLAUSE_COPYIN:
14327 case OMP_CLAUSE_COPYPRIVATE:
14328 case OMP_CLAUSE_UNIFORM:
14329 OMP_CLAUSE_DECL (nc) = tsubst_copy (OMP_CLAUSE_DECL (oc), args,
14330 complain, in_decl);
14331 break;
14332 case OMP_CLAUSE_DEPEND:
14333 case OMP_CLAUSE_FROM:
14334 case OMP_CLAUSE_TO:
14335 case OMP_CLAUSE_MAP:
14336 OMP_CLAUSE_DECL (nc)
14337 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14338 in_decl);
14339 break;
14340 case OMP_CLAUSE_IF:
14341 case OMP_CLAUSE_NUM_THREADS:
14342 case OMP_CLAUSE_SCHEDULE:
14343 case OMP_CLAUSE_COLLAPSE:
14344 case OMP_CLAUSE_FINAL:
14345 case OMP_CLAUSE_DEVICE:
14346 case OMP_CLAUSE_DIST_SCHEDULE:
14347 case OMP_CLAUSE_NUM_TEAMS:
14348 case OMP_CLAUSE_THREAD_LIMIT:
14349 case OMP_CLAUSE_SAFELEN:
14350 case OMP_CLAUSE_SIMDLEN:
14351 OMP_CLAUSE_OPERAND (nc, 0)
14352 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14353 in_decl, /*integral_constant_expression_p=*/false);
14354 break;
14355 case OMP_CLAUSE_REDUCTION:
14356 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14357 {
14358 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14359 if (TREE_CODE (placeholder) == SCOPE_REF)
14360 {
14361 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14362 complain, in_decl);
14363 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14364 = build_qualified_name (NULL_TREE, scope,
14365 TREE_OPERAND (placeholder, 1),
14366 false);
14367 }
14368 else
14369 gcc_assert (identifier_p (placeholder));
14370 }
14371 OMP_CLAUSE_DECL (nc) = tsubst_copy (OMP_CLAUSE_DECL (oc), args,
14372 complain, in_decl);
14373 break;
14374 case OMP_CLAUSE_LINEAR:
14375 case OMP_CLAUSE_ALIGNED:
14376 OMP_CLAUSE_DECL (nc) = tsubst_copy (OMP_CLAUSE_DECL (oc), args,
14377 complain, in_decl);
14378 OMP_CLAUSE_OPERAND (nc, 1)
14379 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14380 in_decl, /*integral_constant_expression_p=*/false);
14381 break;
14382 case OMP_CLAUSE_NOWAIT:
14383 case OMP_CLAUSE_ORDERED:
14384 case OMP_CLAUSE_DEFAULT:
14385 case OMP_CLAUSE_UNTIED:
14386 case OMP_CLAUSE_MERGEABLE:
14387 case OMP_CLAUSE_INBRANCH:
14388 case OMP_CLAUSE_NOTINBRANCH:
14389 case OMP_CLAUSE_PROC_BIND:
14390 case OMP_CLAUSE_FOR:
14391 case OMP_CLAUSE_PARALLEL:
14392 case OMP_CLAUSE_SECTIONS:
14393 case OMP_CLAUSE_TASKGROUP:
14394 break;
14395 default:
14396 gcc_unreachable ();
14397 }
14398 }
14399
14400 new_clauses = nreverse (new_clauses);
14401 if (!declare_simd)
14402 new_clauses = finish_omp_clauses (new_clauses);
14403 return new_clauses;
14404 }
14405
14406 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14407
14408 static tree
14409 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14410 tree in_decl)
14411 {
14412 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14413
14414 tree purpose, value, chain;
14415
14416 if (t == NULL)
14417 return t;
14418
14419 if (TREE_CODE (t) != TREE_LIST)
14420 return tsubst_copy_and_build (t, args, complain, in_decl,
14421 /*function_p=*/false,
14422 /*integral_constant_expression_p=*/false);
14423
14424 if (t == void_list_node)
14425 return t;
14426
14427 purpose = TREE_PURPOSE (t);
14428 if (purpose)
14429 purpose = RECUR (purpose);
14430 value = TREE_VALUE (t);
14431 if (value)
14432 {
14433 if (TREE_CODE (value) != LABEL_DECL)
14434 value = RECUR (value);
14435 else
14436 {
14437 value = lookup_label (DECL_NAME (value));
14438 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14439 TREE_USED (value) = 1;
14440 }
14441 }
14442 chain = TREE_CHAIN (t);
14443 if (chain && chain != void_type_node)
14444 chain = RECUR (chain);
14445 return tree_cons (purpose, value, chain);
14446 #undef RECUR
14447 }
14448
14449 /* Substitute one OMP_FOR iterator. */
14450
14451 static void
14452 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
14453 tree condv, tree incrv, tree *clauses,
14454 tree args, tsubst_flags_t complain, tree in_decl,
14455 bool integral_constant_expression_p)
14456 {
14457 #define RECUR(NODE) \
14458 tsubst_expr ((NODE), args, complain, in_decl, \
14459 integral_constant_expression_p)
14460 tree decl, init, cond, incr;
14461
14462 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
14463 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
14464 decl = TREE_OPERAND (init, 0);
14465 init = TREE_OPERAND (init, 1);
14466 tree decl_expr = NULL_TREE;
14467 if (init && TREE_CODE (init) == DECL_EXPR)
14468 {
14469 /* We need to jump through some hoops to handle declarations in the
14470 for-init-statement, since we might need to handle auto deduction,
14471 but we need to keep control of initialization. */
14472 decl_expr = init;
14473 init = DECL_INITIAL (DECL_EXPR_DECL (init));
14474 decl = tsubst_decl (decl, args, complain);
14475 }
14476 else
14477 decl = RECUR (decl);
14478 init = RECUR (init);
14479
14480 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14481 if (auto_node && init)
14482 TREE_TYPE (decl)
14483 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
14484
14485 gcc_assert (!type_dependent_expression_p (decl));
14486
14487 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14488 {
14489 if (decl_expr)
14490 {
14491 /* Declare the variable, but don't let that initialize it. */
14492 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
14493 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
14494 RECUR (decl_expr);
14495 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
14496 }
14497
14498 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
14499 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14500 if (TREE_CODE (incr) == MODIFY_EXPR)
14501 {
14502 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14503 tree rhs = RECUR (TREE_OPERAND (incr, 1));
14504 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
14505 NOP_EXPR, rhs, complain);
14506 }
14507 else
14508 incr = RECUR (incr);
14509 TREE_VEC_ELT (declv, i) = decl;
14510 TREE_VEC_ELT (initv, i) = init;
14511 TREE_VEC_ELT (condv, i) = cond;
14512 TREE_VEC_ELT (incrv, i) = incr;
14513 return;
14514 }
14515
14516 if (decl_expr)
14517 {
14518 /* Declare and initialize the variable. */
14519 RECUR (decl_expr);
14520 init = NULL_TREE;
14521 }
14522 else if (init)
14523 {
14524 tree c;
14525 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
14526 {
14527 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14528 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
14529 && OMP_CLAUSE_DECL (c) == decl)
14530 break;
14531 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14532 && OMP_CLAUSE_DECL (c) == decl)
14533 error ("iteration variable %qD should not be firstprivate", decl);
14534 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14535 && OMP_CLAUSE_DECL (c) == decl)
14536 error ("iteration variable %qD should not be reduction", decl);
14537 }
14538 if (c == NULL)
14539 {
14540 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14541 OMP_CLAUSE_DECL (c) = decl;
14542 c = finish_omp_clauses (c);
14543 if (c)
14544 {
14545 OMP_CLAUSE_CHAIN (c) = *clauses;
14546 *clauses = c;
14547 }
14548 }
14549 }
14550 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
14551 if (COMPARISON_CLASS_P (cond))
14552 {
14553 tree op0 = RECUR (TREE_OPERAND (cond, 0));
14554 tree op1 = RECUR (TREE_OPERAND (cond, 1));
14555 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
14556 }
14557 else
14558 cond = RECUR (cond);
14559 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
14560 switch (TREE_CODE (incr))
14561 {
14562 case PREINCREMENT_EXPR:
14563 case PREDECREMENT_EXPR:
14564 case POSTINCREMENT_EXPR:
14565 case POSTDECREMENT_EXPR:
14566 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
14567 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
14568 break;
14569 case MODIFY_EXPR:
14570 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14571 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14572 {
14573 tree rhs = TREE_OPERAND (incr, 1);
14574 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14575 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14576 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14577 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14578 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14579 rhs0, rhs1));
14580 }
14581 else
14582 incr = RECUR (incr);
14583 break;
14584 case MODOP_EXPR:
14585 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
14586 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
14587 {
14588 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14589 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14590 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
14591 TREE_TYPE (decl), lhs,
14592 RECUR (TREE_OPERAND (incr, 2))));
14593 }
14594 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
14595 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
14596 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
14597 {
14598 tree rhs = TREE_OPERAND (incr, 2);
14599 tree lhs = RECUR (TREE_OPERAND (incr, 0));
14600 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
14601 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
14602 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
14603 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
14604 rhs0, rhs1));
14605 }
14606 else
14607 incr = RECUR (incr);
14608 break;
14609 default:
14610 incr = RECUR (incr);
14611 break;
14612 }
14613
14614 TREE_VEC_ELT (declv, i) = decl;
14615 TREE_VEC_ELT (initv, i) = init;
14616 TREE_VEC_ELT (condv, i) = cond;
14617 TREE_VEC_ELT (incrv, i) = incr;
14618 #undef RECUR
14619 }
14620
14621 /* Like tsubst_copy for expressions, etc. but also does semantic
14622 processing. */
14623
14624 tree
14625 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
14626 bool integral_constant_expression_p)
14627 {
14628 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14629 #define RECUR(NODE) \
14630 tsubst_expr ((NODE), args, complain, in_decl, \
14631 integral_constant_expression_p)
14632
14633 tree stmt, tmp;
14634 tree r;
14635 location_t loc;
14636
14637 if (t == NULL_TREE || t == error_mark_node)
14638 return t;
14639
14640 loc = input_location;
14641 if (EXPR_HAS_LOCATION (t))
14642 input_location = EXPR_LOCATION (t);
14643 if (STATEMENT_CODE_P (TREE_CODE (t)))
14644 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
14645
14646 switch (TREE_CODE (t))
14647 {
14648 case STATEMENT_LIST:
14649 {
14650 tree_stmt_iterator i;
14651 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
14652 RECUR (tsi_stmt (i));
14653 break;
14654 }
14655
14656 case CTOR_INITIALIZER:
14657 finish_mem_initializers (tsubst_initializer_list
14658 (TREE_OPERAND (t, 0), args));
14659 break;
14660
14661 case RETURN_EXPR:
14662 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
14663 break;
14664
14665 case EXPR_STMT:
14666 tmp = RECUR (EXPR_STMT_EXPR (t));
14667 if (EXPR_STMT_STMT_EXPR_RESULT (t))
14668 finish_stmt_expr_expr (tmp, cur_stmt_expr);
14669 else
14670 finish_expr_stmt (tmp);
14671 break;
14672
14673 case USING_STMT:
14674 do_using_directive (USING_STMT_NAMESPACE (t));
14675 break;
14676
14677 case DECL_EXPR:
14678 {
14679 tree decl, pattern_decl;
14680 tree init;
14681
14682 pattern_decl = decl = DECL_EXPR_DECL (t);
14683 if (TREE_CODE (decl) == LABEL_DECL)
14684 finish_label_decl (DECL_NAME (decl));
14685 else if (TREE_CODE (decl) == USING_DECL)
14686 {
14687 tree scope = USING_DECL_SCOPE (decl);
14688 tree name = DECL_NAME (decl);
14689 tree decl;
14690
14691 scope = tsubst (scope, args, complain, in_decl);
14692 decl = lookup_qualified_name (scope, name,
14693 /*is_type_p=*/false,
14694 /*complain=*/false);
14695 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
14696 qualified_name_lookup_error (scope, name, decl, input_location);
14697 else
14698 do_local_using_decl (decl, scope, name);
14699 }
14700 else if (DECL_PACK_P (decl))
14701 {
14702 /* Don't build up decls for a variadic capture proxy, we'll
14703 instantiate the elements directly as needed. */
14704 break;
14705 }
14706 else
14707 {
14708 init = DECL_INITIAL (decl);
14709 decl = tsubst (decl, args, complain, in_decl);
14710 if (decl != error_mark_node)
14711 {
14712 /* By marking the declaration as instantiated, we avoid
14713 trying to instantiate it. Since instantiate_decl can't
14714 handle local variables, and since we've already done
14715 all that needs to be done, that's the right thing to
14716 do. */
14717 if (VAR_P (decl))
14718 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14719 if (VAR_P (decl)
14720 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
14721 /* Anonymous aggregates are a special case. */
14722 finish_anon_union (decl);
14723 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
14724 {
14725 DECL_CONTEXT (decl) = current_function_decl;
14726 if (DECL_NAME (decl) == this_identifier)
14727 {
14728 tree lam = DECL_CONTEXT (current_function_decl);
14729 lam = CLASSTYPE_LAMBDA_EXPR (lam);
14730 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
14731 }
14732 insert_capture_proxy (decl);
14733 }
14734 else if (DECL_IMPLICIT_TYPEDEF_P (t))
14735 /* We already did a pushtag. */;
14736 else if (TREE_CODE (decl) == FUNCTION_DECL
14737 && DECL_OMP_DECLARE_REDUCTION_P (decl)
14738 && DECL_FUNCTION_SCOPE_P (pattern_decl))
14739 {
14740 DECL_CONTEXT (decl) = NULL_TREE;
14741 pushdecl (decl);
14742 DECL_CONTEXT (decl) = current_function_decl;
14743 cp_check_omp_declare_reduction (decl);
14744 }
14745 else
14746 {
14747 int const_init = false;
14748 maybe_push_decl (decl);
14749 if (VAR_P (decl)
14750 && DECL_PRETTY_FUNCTION_P (decl))
14751 {
14752 /* For __PRETTY_FUNCTION__ we have to adjust the
14753 initializer. */
14754 const char *const name
14755 = cxx_printable_name (current_function_decl, 2);
14756 init = cp_fname_init (name, &TREE_TYPE (decl));
14757 }
14758 else
14759 init = tsubst_init (init, decl, args, complain, in_decl);
14760
14761 if (VAR_P (decl))
14762 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
14763 (pattern_decl));
14764 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
14765 }
14766 }
14767 }
14768
14769 break;
14770 }
14771
14772 case FOR_STMT:
14773 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
14774 RECUR (FOR_INIT_STMT (t));
14775 finish_for_init_stmt (stmt);
14776 tmp = RECUR (FOR_COND (t));
14777 finish_for_cond (tmp, stmt, false);
14778 tmp = RECUR (FOR_EXPR (t));
14779 finish_for_expr (tmp, stmt);
14780 RECUR (FOR_BODY (t));
14781 finish_for_stmt (stmt);
14782 break;
14783
14784 case RANGE_FOR_STMT:
14785 {
14786 tree decl, expr;
14787 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
14788 decl = RANGE_FOR_DECL (t);
14789 decl = tsubst (decl, args, complain, in_decl);
14790 maybe_push_decl (decl);
14791 expr = RECUR (RANGE_FOR_EXPR (t));
14792 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
14793 RECUR (RANGE_FOR_BODY (t));
14794 finish_for_stmt (stmt);
14795 }
14796 break;
14797
14798 case WHILE_STMT:
14799 stmt = begin_while_stmt ();
14800 tmp = RECUR (WHILE_COND (t));
14801 finish_while_stmt_cond (tmp, stmt, false);
14802 RECUR (WHILE_BODY (t));
14803 finish_while_stmt (stmt);
14804 break;
14805
14806 case DO_STMT:
14807 stmt = begin_do_stmt ();
14808 RECUR (DO_BODY (t));
14809 finish_do_body (stmt);
14810 tmp = RECUR (DO_COND (t));
14811 finish_do_stmt (tmp, stmt, false);
14812 break;
14813
14814 case IF_STMT:
14815 stmt = begin_if_stmt ();
14816 tmp = RECUR (IF_COND (t));
14817 finish_if_stmt_cond (tmp, stmt);
14818 RECUR (THEN_CLAUSE (t));
14819 finish_then_clause (stmt);
14820
14821 if (ELSE_CLAUSE (t))
14822 {
14823 begin_else_clause (stmt);
14824 RECUR (ELSE_CLAUSE (t));
14825 finish_else_clause (stmt);
14826 }
14827
14828 finish_if_stmt (stmt);
14829 break;
14830
14831 case BIND_EXPR:
14832 if (BIND_EXPR_BODY_BLOCK (t))
14833 stmt = begin_function_body ();
14834 else
14835 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
14836 ? BCS_TRY_BLOCK : 0);
14837
14838 RECUR (BIND_EXPR_BODY (t));
14839
14840 if (BIND_EXPR_BODY_BLOCK (t))
14841 finish_function_body (stmt);
14842 else
14843 finish_compound_stmt (stmt);
14844 break;
14845
14846 case BREAK_STMT:
14847 finish_break_stmt ();
14848 break;
14849
14850 case CONTINUE_STMT:
14851 finish_continue_stmt ();
14852 break;
14853
14854 case SWITCH_STMT:
14855 stmt = begin_switch_stmt ();
14856 tmp = RECUR (SWITCH_STMT_COND (t));
14857 finish_switch_cond (tmp, stmt);
14858 RECUR (SWITCH_STMT_BODY (t));
14859 finish_switch_stmt (stmt);
14860 break;
14861
14862 case CASE_LABEL_EXPR:
14863 {
14864 tree low = RECUR (CASE_LOW (t));
14865 tree high = RECUR (CASE_HIGH (t));
14866 finish_case_label (EXPR_LOCATION (t), low, high);
14867 }
14868 break;
14869
14870 case LABEL_EXPR:
14871 {
14872 tree decl = LABEL_EXPR_LABEL (t);
14873 tree label;
14874
14875 label = finish_label_stmt (DECL_NAME (decl));
14876 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
14877 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
14878 }
14879 break;
14880
14881 case GOTO_EXPR:
14882 tmp = GOTO_DESTINATION (t);
14883 if (TREE_CODE (tmp) != LABEL_DECL)
14884 /* Computed goto's must be tsubst'd into. On the other hand,
14885 non-computed gotos must not be; the identifier in question
14886 will have no binding. */
14887 tmp = RECUR (tmp);
14888 else
14889 tmp = DECL_NAME (tmp);
14890 finish_goto_stmt (tmp);
14891 break;
14892
14893 case ASM_EXPR:
14894 {
14895 tree string = RECUR (ASM_STRING (t));
14896 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
14897 complain, in_decl);
14898 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
14899 complain, in_decl);
14900 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
14901 complain, in_decl);
14902 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
14903 complain, in_decl);
14904 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
14905 clobbers, labels);
14906 tree asm_expr = tmp;
14907 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14908 asm_expr = TREE_OPERAND (asm_expr, 0);
14909 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14910 }
14911 break;
14912
14913 case TRY_BLOCK:
14914 if (CLEANUP_P (t))
14915 {
14916 stmt = begin_try_block ();
14917 RECUR (TRY_STMTS (t));
14918 finish_cleanup_try_block (stmt);
14919 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14920 }
14921 else
14922 {
14923 tree compound_stmt = NULL_TREE;
14924
14925 if (FN_TRY_BLOCK_P (t))
14926 stmt = begin_function_try_block (&compound_stmt);
14927 else
14928 stmt = begin_try_block ();
14929
14930 RECUR (TRY_STMTS (t));
14931
14932 if (FN_TRY_BLOCK_P (t))
14933 finish_function_try_block (stmt);
14934 else
14935 finish_try_block (stmt);
14936
14937 RECUR (TRY_HANDLERS (t));
14938 if (FN_TRY_BLOCK_P (t))
14939 finish_function_handler_sequence (stmt, compound_stmt);
14940 else
14941 finish_handler_sequence (stmt);
14942 }
14943 break;
14944
14945 case HANDLER:
14946 {
14947 tree decl = HANDLER_PARMS (t);
14948
14949 if (decl)
14950 {
14951 decl = tsubst (decl, args, complain, in_decl);
14952 /* Prevent instantiate_decl from trying to instantiate
14953 this variable. We've already done all that needs to be
14954 done. */
14955 if (decl != error_mark_node)
14956 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14957 }
14958 stmt = begin_handler ();
14959 finish_handler_parms (decl, stmt);
14960 RECUR (HANDLER_BODY (t));
14961 finish_handler (stmt);
14962 }
14963 break;
14964
14965 case TAG_DEFN:
14966 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14967 if (CLASS_TYPE_P (tmp))
14968 {
14969 /* Local classes are not independent templates; they are
14970 instantiated along with their containing function. And this
14971 way we don't have to deal with pushing out of one local class
14972 to instantiate a member of another local class. */
14973 tree fn;
14974 /* Closures are handled by the LAMBDA_EXPR. */
14975 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14976 complete_type (tmp);
14977 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14978 if (!DECL_ARTIFICIAL (fn))
14979 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14980 }
14981 break;
14982
14983 case STATIC_ASSERT:
14984 {
14985 tree condition;
14986
14987 ++c_inhibit_evaluation_warnings;
14988 condition =
14989 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14990 args,
14991 complain, in_decl,
14992 /*integral_constant_expression_p=*/true);
14993 --c_inhibit_evaluation_warnings;
14994
14995 finish_static_assert (condition,
14996 STATIC_ASSERT_MESSAGE (t),
14997 STATIC_ASSERT_SOURCE_LOCATION (t),
14998 /*member_p=*/false);
14999 }
15000 break;
15001
15002 case OMP_PARALLEL:
15003 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
15004 args, complain, in_decl);
15005 stmt = begin_omp_parallel ();
15006 RECUR (OMP_PARALLEL_BODY (t));
15007 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15008 = OMP_PARALLEL_COMBINED (t);
15009 break;
15010
15011 case OMP_TASK:
15012 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
15013 args, complain, in_decl);
15014 stmt = begin_omp_task ();
15015 RECUR (OMP_TASK_BODY (t));
15016 finish_omp_task (tmp, stmt);
15017 break;
15018
15019 case OMP_FOR:
15020 case OMP_SIMD:
15021 case CILK_SIMD:
15022 case CILK_FOR:
15023 case OMP_DISTRIBUTE:
15024 {
15025 tree clauses, body, pre_body;
15026 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15027 tree incrv = NULL_TREE;
15028 int i;
15029
15030 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
15031 args, complain, in_decl);
15032 if (OMP_FOR_INIT (t) != NULL_TREE)
15033 {
15034 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15035 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15036 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15037 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15038 }
15039
15040 stmt = begin_omp_structured_block ();
15041
15042 pre_body = push_stmt_list ();
15043 RECUR (OMP_FOR_PRE_BODY (t));
15044 pre_body = pop_stmt_list (pre_body);
15045
15046 if (OMP_FOR_INIT (t) != NULL_TREE)
15047 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15048 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
15049 &clauses, args, complain, in_decl,
15050 integral_constant_expression_p);
15051
15052 body = push_stmt_list ();
15053 RECUR (OMP_FOR_BODY (t));
15054 body = pop_stmt_list (body);
15055
15056 if (OMP_FOR_INIT (t) != NULL_TREE)
15057 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
15058 condv, incrv, body, pre_body, clauses);
15059 else
15060 {
15061 t = make_node (TREE_CODE (t));
15062 TREE_TYPE (t) = void_type_node;
15063 OMP_FOR_BODY (t) = body;
15064 OMP_FOR_PRE_BODY (t) = pre_body;
15065 OMP_FOR_CLAUSES (t) = clauses;
15066 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15067 add_stmt (t);
15068 }
15069
15070 add_stmt (finish_omp_structured_block (stmt));
15071 }
15072 break;
15073
15074 case OMP_SECTIONS:
15075 case OMP_SINGLE:
15076 case OMP_TEAMS:
15077 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
15078 args, complain, in_decl);
15079 stmt = push_stmt_list ();
15080 RECUR (OMP_BODY (t));
15081 stmt = pop_stmt_list (stmt);
15082
15083 t = copy_node (t);
15084 OMP_BODY (t) = stmt;
15085 OMP_CLAUSES (t) = tmp;
15086 add_stmt (t);
15087 break;
15088
15089 case OMP_TARGET_DATA:
15090 case OMP_TARGET:
15091 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
15092 args, complain, in_decl);
15093 keep_next_level (true);
15094 stmt = begin_omp_structured_block ();
15095
15096 RECUR (OMP_BODY (t));
15097 stmt = finish_omp_structured_block (stmt);
15098
15099 t = copy_node (t);
15100 OMP_BODY (t) = stmt;
15101 OMP_CLAUSES (t) = tmp;
15102 add_stmt (t);
15103 break;
15104
15105 case OMP_TARGET_UPDATE:
15106 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
15107 args, complain, in_decl);
15108 t = copy_node (t);
15109 OMP_TARGET_UPDATE_CLAUSES (t) = tmp;
15110 add_stmt (t);
15111 break;
15112
15113 case OMP_SECTION:
15114 case OMP_CRITICAL:
15115 case OMP_MASTER:
15116 case OMP_TASKGROUP:
15117 case OMP_ORDERED:
15118 stmt = push_stmt_list ();
15119 RECUR (OMP_BODY (t));
15120 stmt = pop_stmt_list (stmt);
15121
15122 t = copy_node (t);
15123 OMP_BODY (t) = stmt;
15124 add_stmt (t);
15125 break;
15126
15127 case OMP_ATOMIC:
15128 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
15129 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
15130 {
15131 tree op1 = TREE_OPERAND (t, 1);
15132 tree rhs1 = NULL_TREE;
15133 tree lhs, rhs;
15134 if (TREE_CODE (op1) == COMPOUND_EXPR)
15135 {
15136 rhs1 = RECUR (TREE_OPERAND (op1, 0));
15137 op1 = TREE_OPERAND (op1, 1);
15138 }
15139 lhs = RECUR (TREE_OPERAND (op1, 0));
15140 rhs = RECUR (TREE_OPERAND (op1, 1));
15141 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
15142 NULL_TREE, NULL_TREE, rhs1,
15143 OMP_ATOMIC_SEQ_CST (t));
15144 }
15145 else
15146 {
15147 tree op1 = TREE_OPERAND (t, 1);
15148 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
15149 tree rhs1 = NULL_TREE;
15150 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
15151 enum tree_code opcode = NOP_EXPR;
15152 if (code == OMP_ATOMIC_READ)
15153 {
15154 v = RECUR (TREE_OPERAND (op1, 0));
15155 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15156 }
15157 else if (code == OMP_ATOMIC_CAPTURE_OLD
15158 || code == OMP_ATOMIC_CAPTURE_NEW)
15159 {
15160 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
15161 v = RECUR (TREE_OPERAND (op1, 0));
15162 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15163 if (TREE_CODE (op11) == COMPOUND_EXPR)
15164 {
15165 rhs1 = RECUR (TREE_OPERAND (op11, 0));
15166 op11 = TREE_OPERAND (op11, 1);
15167 }
15168 lhs = RECUR (TREE_OPERAND (op11, 0));
15169 rhs = RECUR (TREE_OPERAND (op11, 1));
15170 opcode = TREE_CODE (op11);
15171 if (opcode == MODIFY_EXPR)
15172 opcode = NOP_EXPR;
15173 }
15174 else
15175 {
15176 code = OMP_ATOMIC;
15177 lhs = RECUR (TREE_OPERAND (op1, 0));
15178 rhs = RECUR (TREE_OPERAND (op1, 1));
15179 }
15180 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
15181 OMP_ATOMIC_SEQ_CST (t));
15182 }
15183 break;
15184
15185 case TRANSACTION_EXPR:
15186 {
15187 int flags = 0;
15188 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
15189 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
15190
15191 if (TRANSACTION_EXPR_IS_STMT (t))
15192 {
15193 tree body = TRANSACTION_EXPR_BODY (t);
15194 tree noex = NULL_TREE;
15195 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
15196 {
15197 noex = MUST_NOT_THROW_COND (body);
15198 if (noex == NULL_TREE)
15199 noex = boolean_true_node;
15200 body = TREE_OPERAND (body, 0);
15201 }
15202 stmt = begin_transaction_stmt (input_location, NULL, flags);
15203 RECUR (body);
15204 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
15205 }
15206 else
15207 {
15208 stmt = build_transaction_expr (EXPR_LOCATION (t),
15209 RECUR (TRANSACTION_EXPR_BODY (t)),
15210 flags, NULL_TREE);
15211 RETURN (stmt);
15212 }
15213 }
15214 break;
15215
15216 case MUST_NOT_THROW_EXPR:
15217 {
15218 tree op0 = RECUR (TREE_OPERAND (t, 0));
15219 tree cond = RECUR (MUST_NOT_THROW_COND (t));
15220 RETURN (build_must_not_throw_expr (op0, cond));
15221 }
15222
15223 case EXPR_PACK_EXPANSION:
15224 error ("invalid use of pack expansion expression");
15225 RETURN (error_mark_node);
15226
15227 case NONTYPE_ARGUMENT_PACK:
15228 error ("use %<...%> to expand argument pack");
15229 RETURN (error_mark_node);
15230
15231 case CILK_SPAWN_STMT:
15232 cfun->calls_cilk_spawn = 1;
15233 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
15234
15235 case CILK_SYNC_STMT:
15236 RETURN (build_cilk_sync ());
15237
15238 case COMPOUND_EXPR:
15239 tmp = RECUR (TREE_OPERAND (t, 0));
15240 if (tmp == NULL_TREE)
15241 /* If the first operand was a statement, we're done with it. */
15242 RETURN (RECUR (TREE_OPERAND (t, 1)));
15243 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
15244 RECUR (TREE_OPERAND (t, 1)),
15245 complain));
15246
15247 case ANNOTATE_EXPR:
15248 tmp = RECUR (TREE_OPERAND (t, 0));
15249 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
15250 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
15251
15252 default:
15253 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
15254
15255 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
15256 /*function_p=*/false,
15257 integral_constant_expression_p));
15258 }
15259
15260 RETURN (NULL_TREE);
15261 out:
15262 input_location = loc;
15263 return r;
15264 #undef RECUR
15265 #undef RETURN
15266 }
15267
15268 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
15269 function. For description of the body see comment above
15270 cp_parser_omp_declare_reduction_exprs. */
15271
15272 static void
15273 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15274 {
15275 if (t == NULL_TREE || t == error_mark_node)
15276 return;
15277
15278 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
15279
15280 tree_stmt_iterator tsi;
15281 int i;
15282 tree stmts[7];
15283 memset (stmts, 0, sizeof stmts);
15284 for (i = 0, tsi = tsi_start (t);
15285 i < 7 && !tsi_end_p (tsi);
15286 i++, tsi_next (&tsi))
15287 stmts[i] = tsi_stmt (tsi);
15288 gcc_assert (tsi_end_p (tsi));
15289
15290 if (i >= 3)
15291 {
15292 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
15293 && TREE_CODE (stmts[1]) == DECL_EXPR);
15294 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
15295 args, complain, in_decl);
15296 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
15297 args, complain, in_decl);
15298 DECL_CONTEXT (omp_out) = current_function_decl;
15299 DECL_CONTEXT (omp_in) = current_function_decl;
15300 keep_next_level (true);
15301 tree block = begin_omp_structured_block ();
15302 tsubst_expr (stmts[2], args, complain, in_decl, false);
15303 block = finish_omp_structured_block (block);
15304 block = maybe_cleanup_point_expr_void (block);
15305 add_decl_expr (omp_out);
15306 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
15307 TREE_NO_WARNING (omp_out) = 1;
15308 add_decl_expr (omp_in);
15309 finish_expr_stmt (block);
15310 }
15311 if (i >= 6)
15312 {
15313 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
15314 && TREE_CODE (stmts[4]) == DECL_EXPR);
15315 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
15316 args, complain, in_decl);
15317 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
15318 args, complain, in_decl);
15319 DECL_CONTEXT (omp_priv) = current_function_decl;
15320 DECL_CONTEXT (omp_orig) = current_function_decl;
15321 keep_next_level (true);
15322 tree block = begin_omp_structured_block ();
15323 tsubst_expr (stmts[5], args, complain, in_decl, false);
15324 block = finish_omp_structured_block (block);
15325 block = maybe_cleanup_point_expr_void (block);
15326 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
15327 add_decl_expr (omp_priv);
15328 add_decl_expr (omp_orig);
15329 finish_expr_stmt (block);
15330 if (i == 7)
15331 add_decl_expr (omp_orig);
15332 }
15333 }
15334
15335 /* T is a postfix-expression that is not being used in a function
15336 call. Return the substituted version of T. */
15337
15338 static tree
15339 tsubst_non_call_postfix_expression (tree t, tree args,
15340 tsubst_flags_t complain,
15341 tree in_decl)
15342 {
15343 if (TREE_CODE (t) == SCOPE_REF)
15344 t = tsubst_qualified_id (t, args, complain, in_decl,
15345 /*done=*/false, /*address_p=*/false);
15346 else
15347 t = tsubst_copy_and_build (t, args, complain, in_decl,
15348 /*function_p=*/false,
15349 /*integral_constant_expression_p=*/false);
15350
15351 return t;
15352 }
15353
15354 /* Like tsubst but deals with expressions and performs semantic
15355 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
15356
15357 tree
15358 tsubst_copy_and_build (tree t,
15359 tree args,
15360 tsubst_flags_t complain,
15361 tree in_decl,
15362 bool function_p,
15363 bool integral_constant_expression_p)
15364 {
15365 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
15366 #define RECUR(NODE) \
15367 tsubst_copy_and_build (NODE, args, complain, in_decl, \
15368 /*function_p=*/false, \
15369 integral_constant_expression_p)
15370
15371 tree retval, op1;
15372 location_t loc;
15373
15374 if (t == NULL_TREE || t == error_mark_node)
15375 return t;
15376
15377 loc = input_location;
15378 if (EXPR_HAS_LOCATION (t))
15379 input_location = EXPR_LOCATION (t);
15380
15381 /* N3276 decltype magic only applies to calls at the top level or on the
15382 right side of a comma. */
15383 tsubst_flags_t decltype_flag = (complain & tf_decltype);
15384 complain &= ~tf_decltype;
15385
15386 switch (TREE_CODE (t))
15387 {
15388 case USING_DECL:
15389 t = DECL_NAME (t);
15390 /* Fall through. */
15391 case IDENTIFIER_NODE:
15392 {
15393 tree decl;
15394 cp_id_kind idk;
15395 bool non_integral_constant_expression_p;
15396 const char *error_msg;
15397
15398 if (IDENTIFIER_TYPENAME_P (t))
15399 {
15400 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15401 t = mangle_conv_op_name_for_type (new_type);
15402 }
15403
15404 /* Look up the name. */
15405 decl = lookup_name (t);
15406
15407 /* By convention, expressions use ERROR_MARK_NODE to indicate
15408 failure, not NULL_TREE. */
15409 if (decl == NULL_TREE)
15410 decl = error_mark_node;
15411
15412 decl = finish_id_expression (t, decl, NULL_TREE,
15413 &idk,
15414 integral_constant_expression_p,
15415 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
15416 &non_integral_constant_expression_p,
15417 /*template_p=*/false,
15418 /*done=*/true,
15419 /*address_p=*/false,
15420 /*template_arg_p=*/false,
15421 &error_msg,
15422 input_location);
15423 if (error_msg)
15424 error (error_msg);
15425 if (!function_p && identifier_p (decl))
15426 {
15427 if (complain & tf_error)
15428 unqualified_name_lookup_error (decl);
15429 decl = error_mark_node;
15430 }
15431 RETURN (decl);
15432 }
15433
15434 case TEMPLATE_ID_EXPR:
15435 {
15436 tree object;
15437 tree templ = RECUR (TREE_OPERAND (t, 0));
15438 tree targs = TREE_OPERAND (t, 1);
15439
15440 if (targs)
15441 targs = tsubst_template_args (targs, args, complain, in_decl);
15442 if (targs == error_mark_node)
15443 return error_mark_node;
15444
15445 if (variable_template_p (templ))
15446 {
15447 templ = lookup_template_variable (templ, targs);
15448 if (!any_dependent_template_arguments_p (targs))
15449 {
15450 templ = finish_template_variable (templ, complain);
15451 mark_used (templ);
15452 }
15453 RETURN (convert_from_reference (templ));
15454 }
15455
15456 if (TREE_CODE (templ) == COMPONENT_REF)
15457 {
15458 object = TREE_OPERAND (templ, 0);
15459 templ = TREE_OPERAND (templ, 1);
15460 }
15461 else
15462 object = NULL_TREE;
15463 templ = lookup_template_function (templ, targs);
15464
15465 if (object)
15466 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
15467 object, templ, NULL_TREE));
15468 else
15469 RETURN (baselink_for_fns (templ));
15470 }
15471
15472 case INDIRECT_REF:
15473 {
15474 tree r = RECUR (TREE_OPERAND (t, 0));
15475
15476 if (REFERENCE_REF_P (t))
15477 {
15478 /* A type conversion to reference type will be enclosed in
15479 such an indirect ref, but the substitution of the cast
15480 will have also added such an indirect ref. */
15481 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
15482 r = convert_from_reference (r);
15483 }
15484 else
15485 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
15486 complain|decltype_flag);
15487 RETURN (r);
15488 }
15489
15490 case NOP_EXPR:
15491 {
15492 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15493 tree op0 = RECUR (TREE_OPERAND (t, 0));
15494 RETURN (build_nop (type, op0));
15495 }
15496
15497 case IMPLICIT_CONV_EXPR:
15498 {
15499 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15500 tree expr = RECUR (TREE_OPERAND (t, 0));
15501 int flags = LOOKUP_IMPLICIT;
15502 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
15503 flags = LOOKUP_NORMAL;
15504 RETURN (perform_implicit_conversion_flags (type, expr, complain,
15505 flags));
15506 }
15507
15508 case CONVERT_EXPR:
15509 {
15510 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15511 tree op0 = RECUR (TREE_OPERAND (t, 0));
15512 RETURN (build1 (CONVERT_EXPR, type, op0));
15513 }
15514
15515 case CAST_EXPR:
15516 case REINTERPRET_CAST_EXPR:
15517 case CONST_CAST_EXPR:
15518 case DYNAMIC_CAST_EXPR:
15519 case STATIC_CAST_EXPR:
15520 {
15521 tree type;
15522 tree op, r = NULL_TREE;
15523
15524 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15525 if (integral_constant_expression_p
15526 && !cast_valid_in_integral_constant_expression_p (type))
15527 {
15528 if (complain & tf_error)
15529 error ("a cast to a type other than an integral or "
15530 "enumeration type cannot appear in a constant-expression");
15531 RETURN (error_mark_node);
15532 }
15533
15534 op = RECUR (TREE_OPERAND (t, 0));
15535
15536 warning_sentinel s(warn_useless_cast);
15537 switch (TREE_CODE (t))
15538 {
15539 case CAST_EXPR:
15540 r = build_functional_cast (type, op, complain);
15541 break;
15542 case REINTERPRET_CAST_EXPR:
15543 r = build_reinterpret_cast (type, op, complain);
15544 break;
15545 case CONST_CAST_EXPR:
15546 r = build_const_cast (type, op, complain);
15547 break;
15548 case DYNAMIC_CAST_EXPR:
15549 r = build_dynamic_cast (type, op, complain);
15550 break;
15551 case STATIC_CAST_EXPR:
15552 r = build_static_cast (type, op, complain);
15553 break;
15554 default:
15555 gcc_unreachable ();
15556 }
15557
15558 RETURN (r);
15559 }
15560
15561 case POSTDECREMENT_EXPR:
15562 case POSTINCREMENT_EXPR:
15563 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15564 args, complain, in_decl);
15565 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
15566 complain|decltype_flag));
15567
15568 case PREDECREMENT_EXPR:
15569 case PREINCREMENT_EXPR:
15570 case NEGATE_EXPR:
15571 case BIT_NOT_EXPR:
15572 case ABS_EXPR:
15573 case TRUTH_NOT_EXPR:
15574 case UNARY_PLUS_EXPR: /* Unary + */
15575 case REALPART_EXPR:
15576 case IMAGPART_EXPR:
15577 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
15578 RECUR (TREE_OPERAND (t, 0)),
15579 complain|decltype_flag));
15580
15581 case FIX_TRUNC_EXPR:
15582 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
15583 0, complain));
15584
15585 case ADDR_EXPR:
15586 op1 = TREE_OPERAND (t, 0);
15587 if (TREE_CODE (op1) == LABEL_DECL)
15588 RETURN (finish_label_address_expr (DECL_NAME (op1),
15589 EXPR_LOCATION (op1)));
15590 if (TREE_CODE (op1) == SCOPE_REF)
15591 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
15592 /*done=*/true, /*address_p=*/true);
15593 else
15594 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
15595 in_decl);
15596 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
15597 complain|decltype_flag));
15598
15599 case PLUS_EXPR:
15600 case MINUS_EXPR:
15601 case MULT_EXPR:
15602 case TRUNC_DIV_EXPR:
15603 case CEIL_DIV_EXPR:
15604 case FLOOR_DIV_EXPR:
15605 case ROUND_DIV_EXPR:
15606 case EXACT_DIV_EXPR:
15607 case BIT_AND_EXPR:
15608 case BIT_IOR_EXPR:
15609 case BIT_XOR_EXPR:
15610 case TRUNC_MOD_EXPR:
15611 case FLOOR_MOD_EXPR:
15612 case TRUTH_ANDIF_EXPR:
15613 case TRUTH_ORIF_EXPR:
15614 case TRUTH_AND_EXPR:
15615 case TRUTH_OR_EXPR:
15616 case RSHIFT_EXPR:
15617 case LSHIFT_EXPR:
15618 case RROTATE_EXPR:
15619 case LROTATE_EXPR:
15620 case EQ_EXPR:
15621 case NE_EXPR:
15622 case MAX_EXPR:
15623 case MIN_EXPR:
15624 case LE_EXPR:
15625 case GE_EXPR:
15626 case LT_EXPR:
15627 case GT_EXPR:
15628 case MEMBER_REF:
15629 case DOTSTAR_EXPR:
15630 {
15631 warning_sentinel s1(warn_type_limits);
15632 warning_sentinel s2(warn_div_by_zero);
15633 warning_sentinel s3(warn_logical_op);
15634 warning_sentinel s4(warn_tautological_compare);
15635 tree op0 = RECUR (TREE_OPERAND (t, 0));
15636 tree op1 = RECUR (TREE_OPERAND (t, 1));
15637 tree r = build_x_binary_op
15638 (input_location, TREE_CODE (t),
15639 op0,
15640 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
15641 ? ERROR_MARK
15642 : TREE_CODE (TREE_OPERAND (t, 0))),
15643 op1,
15644 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
15645 ? ERROR_MARK
15646 : TREE_CODE (TREE_OPERAND (t, 1))),
15647 /*overload=*/NULL,
15648 complain|decltype_flag);
15649 if (EXPR_P (r) && TREE_NO_WARNING (t))
15650 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15651
15652 RETURN (r);
15653 }
15654
15655 case POINTER_PLUS_EXPR:
15656 {
15657 tree op0 = RECUR (TREE_OPERAND (t, 0));
15658 tree op1 = RECUR (TREE_OPERAND (t, 1));
15659 return fold_build_pointer_plus (op0, op1);
15660 }
15661
15662 case SCOPE_REF:
15663 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
15664 /*address_p=*/false));
15665 case ARRAY_REF:
15666 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15667 args, complain, in_decl);
15668 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
15669 RECUR (TREE_OPERAND (t, 1)),
15670 complain|decltype_flag));
15671
15672 case ARRAY_NOTATION_REF:
15673 {
15674 tree start_index, length, stride;
15675 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
15676 args, complain, in_decl);
15677 start_index = RECUR (ARRAY_NOTATION_START (t));
15678 length = RECUR (ARRAY_NOTATION_LENGTH (t));
15679 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
15680 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
15681 length, stride, TREE_TYPE (op1)));
15682 }
15683 case SIZEOF_EXPR:
15684 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
15685 RETURN (tsubst_copy (t, args, complain, in_decl));
15686 /* Fall through */
15687
15688 case ALIGNOF_EXPR:
15689 {
15690 tree r;
15691
15692 op1 = TREE_OPERAND (t, 0);
15693 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
15694 op1 = TREE_TYPE (op1);
15695 if (!args)
15696 {
15697 /* When there are no ARGS, we are trying to evaluate a
15698 non-dependent expression from the parser. Trying to do
15699 the substitutions may not work. */
15700 if (!TYPE_P (op1))
15701 op1 = TREE_TYPE (op1);
15702 }
15703 else
15704 {
15705 ++cp_unevaluated_operand;
15706 ++c_inhibit_evaluation_warnings;
15707 if (TYPE_P (op1))
15708 op1 = tsubst (op1, args, complain, in_decl);
15709 else
15710 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15711 /*function_p=*/false,
15712 /*integral_constant_expression_p=*/
15713 false);
15714 --cp_unevaluated_operand;
15715 --c_inhibit_evaluation_warnings;
15716 }
15717 if (TYPE_P (op1))
15718 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
15719 complain & tf_error);
15720 else
15721 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
15722 complain & tf_error);
15723 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
15724 {
15725 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
15726 {
15727 if (!processing_template_decl && TYPE_P (op1))
15728 {
15729 r = build_min (SIZEOF_EXPR, size_type_node,
15730 build1 (NOP_EXPR, op1, error_mark_node));
15731 SIZEOF_EXPR_TYPE_P (r) = 1;
15732 }
15733 else
15734 r = build_min (SIZEOF_EXPR, size_type_node, op1);
15735 TREE_SIDE_EFFECTS (r) = 0;
15736 TREE_READONLY (r) = 1;
15737 }
15738 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
15739 }
15740 RETURN (r);
15741 }
15742
15743 case AT_ENCODE_EXPR:
15744 {
15745 op1 = TREE_OPERAND (t, 0);
15746 ++cp_unevaluated_operand;
15747 ++c_inhibit_evaluation_warnings;
15748 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15749 /*function_p=*/false,
15750 /*integral_constant_expression_p=*/false);
15751 --cp_unevaluated_operand;
15752 --c_inhibit_evaluation_warnings;
15753 RETURN (objc_build_encode_expr (op1));
15754 }
15755
15756 case NOEXCEPT_EXPR:
15757 op1 = TREE_OPERAND (t, 0);
15758 ++cp_unevaluated_operand;
15759 ++c_inhibit_evaluation_warnings;
15760 ++cp_noexcept_operand;
15761 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15762 /*function_p=*/false,
15763 /*integral_constant_expression_p=*/false);
15764 --cp_unevaluated_operand;
15765 --c_inhibit_evaluation_warnings;
15766 --cp_noexcept_operand;
15767 RETURN (finish_noexcept_expr (op1, complain));
15768
15769 case MODOP_EXPR:
15770 {
15771 warning_sentinel s(warn_div_by_zero);
15772 tree lhs = RECUR (TREE_OPERAND (t, 0));
15773 tree rhs = RECUR (TREE_OPERAND (t, 2));
15774 tree r = build_x_modify_expr
15775 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
15776 complain|decltype_flag);
15777 /* TREE_NO_WARNING must be set if either the expression was
15778 parenthesized or it uses an operator such as >>= rather
15779 than plain assignment. In the former case, it was already
15780 set and must be copied. In the latter case,
15781 build_x_modify_expr sets it and it must not be reset
15782 here. */
15783 if (TREE_NO_WARNING (t))
15784 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15785
15786 RETURN (r);
15787 }
15788
15789 case ARROW_EXPR:
15790 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15791 args, complain, in_decl);
15792 /* Remember that there was a reference to this entity. */
15793 if (DECL_P (op1)
15794 && !mark_used (op1, complain) && !(complain & tf_error))
15795 RETURN (error_mark_node);
15796 RETURN (build_x_arrow (input_location, op1, complain));
15797
15798 case NEW_EXPR:
15799 {
15800 tree placement = RECUR (TREE_OPERAND (t, 0));
15801 tree init = RECUR (TREE_OPERAND (t, 3));
15802 vec<tree, va_gc> *placement_vec;
15803 vec<tree, va_gc> *init_vec;
15804 tree ret;
15805
15806 if (placement == NULL_TREE)
15807 placement_vec = NULL;
15808 else
15809 {
15810 placement_vec = make_tree_vector ();
15811 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
15812 vec_safe_push (placement_vec, TREE_VALUE (placement));
15813 }
15814
15815 /* If there was an initializer in the original tree, but it
15816 instantiated to an empty list, then we should pass a
15817 non-NULL empty vector to tell build_new that it was an
15818 empty initializer() rather than no initializer. This can
15819 only happen when the initializer is a pack expansion whose
15820 parameter packs are of length zero. */
15821 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
15822 init_vec = NULL;
15823 else
15824 {
15825 init_vec = make_tree_vector ();
15826 if (init == void_node)
15827 gcc_assert (init_vec != NULL);
15828 else
15829 {
15830 for (; init != NULL_TREE; init = TREE_CHAIN (init))
15831 vec_safe_push (init_vec, TREE_VALUE (init));
15832 }
15833 }
15834
15835 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
15836 tree op2 = RECUR (TREE_OPERAND (t, 2));
15837 ret = build_new (&placement_vec, op1, op2, &init_vec,
15838 NEW_EXPR_USE_GLOBAL (t),
15839 complain);
15840
15841 if (placement_vec != NULL)
15842 release_tree_vector (placement_vec);
15843 if (init_vec != NULL)
15844 release_tree_vector (init_vec);
15845
15846 RETURN (ret);
15847 }
15848
15849 case DELETE_EXPR:
15850 {
15851 tree op0 = RECUR (TREE_OPERAND (t, 0));
15852 tree op1 = RECUR (TREE_OPERAND (t, 1));
15853 RETURN (delete_sanity (op0, op1,
15854 DELETE_EXPR_USE_VEC (t),
15855 DELETE_EXPR_USE_GLOBAL (t),
15856 complain));
15857 }
15858
15859 case COMPOUND_EXPR:
15860 {
15861 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
15862 complain & ~tf_decltype, in_decl,
15863 /*function_p=*/false,
15864 integral_constant_expression_p);
15865 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
15866 op0,
15867 RECUR (TREE_OPERAND (t, 1)),
15868 complain|decltype_flag));
15869 }
15870
15871 case CALL_EXPR:
15872 {
15873 tree function;
15874 vec<tree, va_gc> *call_args;
15875 unsigned int nargs, i;
15876 bool qualified_p;
15877 bool koenig_p;
15878 tree ret;
15879
15880 function = CALL_EXPR_FN (t);
15881 /* When we parsed the expression, we determined whether or
15882 not Koenig lookup should be performed. */
15883 koenig_p = KOENIG_LOOKUP_P (t);
15884 if (TREE_CODE (function) == SCOPE_REF)
15885 {
15886 qualified_p = true;
15887 function = tsubst_qualified_id (function, args, complain, in_decl,
15888 /*done=*/false,
15889 /*address_p=*/false);
15890 }
15891 else if (koenig_p && identifier_p (function))
15892 {
15893 /* Do nothing; calling tsubst_copy_and_build on an identifier
15894 would incorrectly perform unqualified lookup again.
15895
15896 Note that we can also have an IDENTIFIER_NODE if the earlier
15897 unqualified lookup found a member function; in that case
15898 koenig_p will be false and we do want to do the lookup
15899 again to find the instantiated member function.
15900
15901 FIXME but doing that causes c++/15272, so we need to stop
15902 using IDENTIFIER_NODE in that situation. */
15903 qualified_p = false;
15904 }
15905 else
15906 {
15907 if (TREE_CODE (function) == COMPONENT_REF)
15908 {
15909 tree op = TREE_OPERAND (function, 1);
15910
15911 qualified_p = (TREE_CODE (op) == SCOPE_REF
15912 || (BASELINK_P (op)
15913 && BASELINK_QUALIFIED_P (op)));
15914 }
15915 else
15916 qualified_p = false;
15917
15918 if (TREE_CODE (function) == ADDR_EXPR
15919 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
15920 /* Avoid error about taking the address of a constructor. */
15921 function = TREE_OPERAND (function, 0);
15922
15923 function = tsubst_copy_and_build (function, args, complain,
15924 in_decl,
15925 !qualified_p,
15926 integral_constant_expression_p);
15927
15928 if (BASELINK_P (function))
15929 qualified_p = true;
15930 }
15931
15932 nargs = call_expr_nargs (t);
15933 call_args = make_tree_vector ();
15934 for (i = 0; i < nargs; ++i)
15935 {
15936 tree arg = CALL_EXPR_ARG (t, i);
15937
15938 if (!PACK_EXPANSION_P (arg))
15939 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15940 else
15941 {
15942 /* Expand the pack expansion and push each entry onto
15943 CALL_ARGS. */
15944 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15945 if (TREE_CODE (arg) == TREE_VEC)
15946 {
15947 unsigned int len, j;
15948
15949 len = TREE_VEC_LENGTH (arg);
15950 for (j = 0; j < len; ++j)
15951 {
15952 tree value = TREE_VEC_ELT (arg, j);
15953 if (value != NULL_TREE)
15954 value = convert_from_reference (value);
15955 vec_safe_push (call_args, value);
15956 }
15957 }
15958 else
15959 {
15960 /* A partial substitution. Add one entry. */
15961 vec_safe_push (call_args, arg);
15962 }
15963 }
15964 }
15965
15966 /* We do not perform argument-dependent lookup if normal
15967 lookup finds a non-function, in accordance with the
15968 expected resolution of DR 218. */
15969 if (koenig_p
15970 && ((is_overloaded_fn (function)
15971 /* If lookup found a member function, the Koenig lookup is
15972 not appropriate, even if an unqualified-name was used
15973 to denote the function. */
15974 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15975 || identifier_p (function))
15976 /* Only do this when substitution turns a dependent call
15977 into a non-dependent call. */
15978 && type_dependent_expression_p_push (t)
15979 && !any_type_dependent_arguments_p (call_args))
15980 function = perform_koenig_lookup (function, call_args, tf_none);
15981
15982 if (identifier_p (function)
15983 && !any_type_dependent_arguments_p (call_args))
15984 {
15985 if (koenig_p && (complain & tf_warning_or_error))
15986 {
15987 /* For backwards compatibility and good diagnostics, try
15988 the unqualified lookup again if we aren't in SFINAE
15989 context. */
15990 tree unq = (tsubst_copy_and_build
15991 (function, args, complain, in_decl, true,
15992 integral_constant_expression_p));
15993 if (unq == error_mark_node)
15994 RETURN (error_mark_node);
15995
15996 if (unq != function)
15997 {
15998 tree fn = unq;
15999 if (INDIRECT_REF_P (fn))
16000 fn = TREE_OPERAND (fn, 0);
16001 if (TREE_CODE (fn) == COMPONENT_REF)
16002 fn = TREE_OPERAND (fn, 1);
16003 if (is_overloaded_fn (fn))
16004 fn = get_first_fn (fn);
16005 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16006 "%qD was not declared in this scope, "
16007 "and no declarations were found by "
16008 "argument-dependent lookup at the point "
16009 "of instantiation", function))
16010 {
16011 if (!DECL_P (fn))
16012 /* Can't say anything more. */;
16013 else if (DECL_CLASS_SCOPE_P (fn))
16014 {
16015 location_t loc = EXPR_LOC_OR_LOC (t,
16016 input_location);
16017 inform (loc,
16018 "declarations in dependent base %qT are "
16019 "not found by unqualified lookup",
16020 DECL_CLASS_CONTEXT (fn));
16021 if (current_class_ptr)
16022 inform (loc,
16023 "use %<this->%D%> instead", function);
16024 else
16025 inform (loc,
16026 "use %<%T::%D%> instead",
16027 current_class_name, function);
16028 }
16029 else
16030 inform (DECL_SOURCE_LOCATION (fn),
16031 "%qD declared here, later in the "
16032 "translation unit", fn);
16033 }
16034 function = unq;
16035 }
16036 }
16037 if (identifier_p (function))
16038 {
16039 if (complain & tf_error)
16040 unqualified_name_lookup_error (function);
16041 release_tree_vector (call_args);
16042 RETURN (error_mark_node);
16043 }
16044 }
16045
16046 /* Remember that there was a reference to this entity. */
16047 if (DECL_P (function)
16048 && !mark_used (function, complain) && !(complain & tf_error))
16049 RETURN (error_mark_node);
16050
16051 /* Put back tf_decltype for the actual call. */
16052 complain |= decltype_flag;
16053
16054 if (TREE_CODE (function) == OFFSET_REF)
16055 ret = build_offset_ref_call_from_tree (function, &call_args,
16056 complain);
16057 else if (TREE_CODE (function) == COMPONENT_REF)
16058 {
16059 tree instance = TREE_OPERAND (function, 0);
16060 tree fn = TREE_OPERAND (function, 1);
16061
16062 if (processing_template_decl
16063 && (type_dependent_expression_p (instance)
16064 || (!BASELINK_P (fn)
16065 && TREE_CODE (fn) != FIELD_DECL)
16066 || type_dependent_expression_p (fn)
16067 || any_type_dependent_arguments_p (call_args)))
16068 ret = build_nt_call_vec (function, call_args);
16069 else if (!BASELINK_P (fn))
16070 ret = finish_call_expr (function, &call_args,
16071 /*disallow_virtual=*/false,
16072 /*koenig_p=*/false,
16073 complain);
16074 else
16075 ret = (build_new_method_call
16076 (instance, fn,
16077 &call_args, NULL_TREE,
16078 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
16079 /*fn_p=*/NULL,
16080 complain));
16081 }
16082 else
16083 ret = finish_call_expr (function, &call_args,
16084 /*disallow_virtual=*/qualified_p,
16085 koenig_p,
16086 complain);
16087
16088 release_tree_vector (call_args);
16089
16090 RETURN (ret);
16091 }
16092
16093 case COND_EXPR:
16094 {
16095 tree cond = RECUR (TREE_OPERAND (t, 0));
16096 tree folded_cond = fold_non_dependent_expr (cond);
16097 tree exp1, exp2;
16098
16099 if (TREE_CODE (folded_cond) == INTEGER_CST)
16100 {
16101 if (integer_zerop (folded_cond))
16102 {
16103 ++c_inhibit_evaluation_warnings;
16104 exp1 = RECUR (TREE_OPERAND (t, 1));
16105 --c_inhibit_evaluation_warnings;
16106 exp2 = RECUR (TREE_OPERAND (t, 2));
16107 }
16108 else
16109 {
16110 exp1 = RECUR (TREE_OPERAND (t, 1));
16111 ++c_inhibit_evaluation_warnings;
16112 exp2 = RECUR (TREE_OPERAND (t, 2));
16113 --c_inhibit_evaluation_warnings;
16114 }
16115 cond = folded_cond;
16116 }
16117 else
16118 {
16119 exp1 = RECUR (TREE_OPERAND (t, 1));
16120 exp2 = RECUR (TREE_OPERAND (t, 2));
16121 }
16122
16123 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
16124 cond, exp1, exp2, complain));
16125 }
16126
16127 case PSEUDO_DTOR_EXPR:
16128 {
16129 tree op0 = RECUR (TREE_OPERAND (t, 0));
16130 tree op1 = RECUR (TREE_OPERAND (t, 1));
16131 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
16132 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
16133 input_location));
16134 }
16135
16136 case TREE_LIST:
16137 {
16138 tree purpose, value, chain;
16139
16140 if (t == void_list_node)
16141 RETURN (t);
16142
16143 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
16144 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
16145 {
16146 /* We have pack expansions, so expand those and
16147 create a new list out of it. */
16148 tree purposevec = NULL_TREE;
16149 tree valuevec = NULL_TREE;
16150 tree chain;
16151 int i, len = -1;
16152
16153 /* Expand the argument expressions. */
16154 if (TREE_PURPOSE (t))
16155 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
16156 complain, in_decl);
16157 if (TREE_VALUE (t))
16158 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
16159 complain, in_decl);
16160
16161 /* Build the rest of the list. */
16162 chain = TREE_CHAIN (t);
16163 if (chain && chain != void_type_node)
16164 chain = RECUR (chain);
16165
16166 /* Determine the number of arguments. */
16167 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
16168 {
16169 len = TREE_VEC_LENGTH (purposevec);
16170 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16171 }
16172 else if (TREE_CODE (valuevec) == TREE_VEC)
16173 len = TREE_VEC_LENGTH (valuevec);
16174 else
16175 {
16176 /* Since we only performed a partial substitution into
16177 the argument pack, we only RETURN (a single list
16178 node. */
16179 if (purposevec == TREE_PURPOSE (t)
16180 && valuevec == TREE_VALUE (t)
16181 && chain == TREE_CHAIN (t))
16182 RETURN (t);
16183
16184 RETURN (tree_cons (purposevec, valuevec, chain));
16185 }
16186
16187 /* Convert the argument vectors into a TREE_LIST */
16188 i = len;
16189 while (i > 0)
16190 {
16191 /* Grab the Ith values. */
16192 i--;
16193 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
16194 : NULL_TREE;
16195 value
16196 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
16197 : NULL_TREE;
16198
16199 /* Build the list (backwards). */
16200 chain = tree_cons (purpose, value, chain);
16201 }
16202
16203 RETURN (chain);
16204 }
16205
16206 purpose = TREE_PURPOSE (t);
16207 if (purpose)
16208 purpose = RECUR (purpose);
16209 value = TREE_VALUE (t);
16210 if (value)
16211 value = RECUR (value);
16212 chain = TREE_CHAIN (t);
16213 if (chain && chain != void_type_node)
16214 chain = RECUR (chain);
16215 if (purpose == TREE_PURPOSE (t)
16216 && value == TREE_VALUE (t)
16217 && chain == TREE_CHAIN (t))
16218 RETURN (t);
16219 RETURN (tree_cons (purpose, value, chain));
16220 }
16221
16222 case COMPONENT_REF:
16223 {
16224 tree object;
16225 tree object_type;
16226 tree member;
16227 tree r;
16228
16229 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16230 args, complain, in_decl);
16231 /* Remember that there was a reference to this entity. */
16232 if (DECL_P (object)
16233 && !mark_used (object, complain) && !(complain & tf_error))
16234 RETURN (error_mark_node);
16235 object_type = TREE_TYPE (object);
16236
16237 member = TREE_OPERAND (t, 1);
16238 if (BASELINK_P (member))
16239 member = tsubst_baselink (member,
16240 non_reference (TREE_TYPE (object)),
16241 args, complain, in_decl);
16242 else
16243 member = tsubst_copy (member, args, complain, in_decl);
16244 if (member == error_mark_node)
16245 RETURN (error_mark_node);
16246
16247 if (type_dependent_expression_p (object))
16248 /* We can't do much here. */;
16249 else if (!CLASS_TYPE_P (object_type))
16250 {
16251 if (scalarish_type_p (object_type))
16252 {
16253 tree s = NULL_TREE;
16254 tree dtor = member;
16255
16256 if (TREE_CODE (dtor) == SCOPE_REF)
16257 {
16258 s = TREE_OPERAND (dtor, 0);
16259 dtor = TREE_OPERAND (dtor, 1);
16260 }
16261 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
16262 {
16263 dtor = TREE_OPERAND (dtor, 0);
16264 if (TYPE_P (dtor))
16265 RETURN (finish_pseudo_destructor_expr
16266 (object, s, dtor, input_location));
16267 }
16268 }
16269 }
16270 else if (TREE_CODE (member) == SCOPE_REF
16271 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
16272 {
16273 /* Lookup the template functions now that we know what the
16274 scope is. */
16275 tree scope = TREE_OPERAND (member, 0);
16276 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
16277 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
16278 member = lookup_qualified_name (scope, tmpl,
16279 /*is_type_p=*/false,
16280 /*complain=*/false);
16281 if (BASELINK_P (member))
16282 {
16283 BASELINK_FUNCTIONS (member)
16284 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
16285 args);
16286 member = (adjust_result_of_qualified_name_lookup
16287 (member, BINFO_TYPE (BASELINK_BINFO (member)),
16288 object_type));
16289 }
16290 else
16291 {
16292 qualified_name_lookup_error (scope, tmpl, member,
16293 input_location);
16294 RETURN (error_mark_node);
16295 }
16296 }
16297 else if (TREE_CODE (member) == SCOPE_REF
16298 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
16299 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
16300 {
16301 if (complain & tf_error)
16302 {
16303 if (TYPE_P (TREE_OPERAND (member, 0)))
16304 error ("%qT is not a class or namespace",
16305 TREE_OPERAND (member, 0));
16306 else
16307 error ("%qD is not a class or namespace",
16308 TREE_OPERAND (member, 0));
16309 }
16310 RETURN (error_mark_node);
16311 }
16312 else if (TREE_CODE (member) == FIELD_DECL)
16313 {
16314 r = finish_non_static_data_member (member, object, NULL_TREE);
16315 if (TREE_CODE (r) == COMPONENT_REF)
16316 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16317 RETURN (r);
16318 }
16319
16320 r = finish_class_member_access_expr (object, member,
16321 /*template_p=*/false,
16322 complain);
16323 if (TREE_CODE (r) == COMPONENT_REF)
16324 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16325 RETURN (r);
16326 }
16327
16328 case THROW_EXPR:
16329 RETURN (build_throw
16330 (RECUR (TREE_OPERAND (t, 0))));
16331
16332 case CONSTRUCTOR:
16333 {
16334 vec<constructor_elt, va_gc> *n;
16335 constructor_elt *ce;
16336 unsigned HOST_WIDE_INT idx;
16337 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16338 bool process_index_p;
16339 int newlen;
16340 bool need_copy_p = false;
16341 tree r;
16342
16343 if (type == error_mark_node)
16344 RETURN (error_mark_node);
16345
16346 /* digest_init will do the wrong thing if we let it. */
16347 if (type && TYPE_PTRMEMFUNC_P (type))
16348 RETURN (t);
16349
16350 /* We do not want to process the index of aggregate
16351 initializers as they are identifier nodes which will be
16352 looked up by digest_init. */
16353 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
16354
16355 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
16356 newlen = vec_safe_length (n);
16357 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
16358 {
16359 if (ce->index && process_index_p
16360 /* An identifier index is looked up in the type
16361 being initialized, not the current scope. */
16362 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
16363 ce->index = RECUR (ce->index);
16364
16365 if (PACK_EXPANSION_P (ce->value))
16366 {
16367 /* Substitute into the pack expansion. */
16368 ce->value = tsubst_pack_expansion (ce->value, args, complain,
16369 in_decl);
16370
16371 if (ce->value == error_mark_node
16372 || PACK_EXPANSION_P (ce->value))
16373 ;
16374 else if (TREE_VEC_LENGTH (ce->value) == 1)
16375 /* Just move the argument into place. */
16376 ce->value = TREE_VEC_ELT (ce->value, 0);
16377 else
16378 {
16379 /* Update the length of the final CONSTRUCTOR
16380 arguments vector, and note that we will need to
16381 copy.*/
16382 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
16383 need_copy_p = true;
16384 }
16385 }
16386 else
16387 ce->value = RECUR (ce->value);
16388 }
16389
16390 if (need_copy_p)
16391 {
16392 vec<constructor_elt, va_gc> *old_n = n;
16393
16394 vec_alloc (n, newlen);
16395 FOR_EACH_VEC_ELT (*old_n, idx, ce)
16396 {
16397 if (TREE_CODE (ce->value) == TREE_VEC)
16398 {
16399 int i, len = TREE_VEC_LENGTH (ce->value);
16400 for (i = 0; i < len; ++i)
16401 CONSTRUCTOR_APPEND_ELT (n, 0,
16402 TREE_VEC_ELT (ce->value, i));
16403 }
16404 else
16405 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
16406 }
16407 }
16408
16409 r = build_constructor (init_list_type_node, n);
16410 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
16411
16412 if (TREE_HAS_CONSTRUCTOR (t))
16413 RETURN (finish_compound_literal (type, r, complain));
16414
16415 TREE_TYPE (r) = type;
16416 RETURN (r);
16417 }
16418
16419 case TYPEID_EXPR:
16420 {
16421 tree operand_0 = TREE_OPERAND (t, 0);
16422 if (TYPE_P (operand_0))
16423 {
16424 operand_0 = tsubst (operand_0, args, complain, in_decl);
16425 RETURN (get_typeid (operand_0, complain));
16426 }
16427 else
16428 {
16429 operand_0 = RECUR (operand_0);
16430 RETURN (build_typeid (operand_0, complain));
16431 }
16432 }
16433
16434 case VAR_DECL:
16435 if (!args)
16436 RETURN (t);
16437 else if (DECL_PACK_P (t))
16438 {
16439 /* We don't build decls for an instantiation of a
16440 variadic capture proxy, we instantiate the elements
16441 when needed. */
16442 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
16443 return RECUR (DECL_VALUE_EXPR (t));
16444 }
16445 /* Fall through */
16446
16447 case PARM_DECL:
16448 {
16449 tree r = tsubst_copy (t, args, complain, in_decl);
16450 /* ??? We're doing a subset of finish_id_expression here. */
16451 if (VAR_P (r)
16452 && !processing_template_decl
16453 && !cp_unevaluated_operand
16454 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
16455 && CP_DECL_THREAD_LOCAL_P (r))
16456 {
16457 if (tree wrap = get_tls_wrapper_fn (r))
16458 /* Replace an evaluated use of the thread_local variable with
16459 a call to its wrapper. */
16460 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
16461 }
16462 else if (outer_automatic_var_p (r))
16463 {
16464 r = process_outer_var_ref (r, complain);
16465 if (is_capture_proxy (r))
16466 register_local_specialization (r, t);
16467 }
16468
16469 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
16470 /* If the original type was a reference, we'll be wrapped in
16471 the appropriate INDIRECT_REF. */
16472 r = convert_from_reference (r);
16473 RETURN (r);
16474 }
16475
16476 case VA_ARG_EXPR:
16477 {
16478 tree op0 = RECUR (TREE_OPERAND (t, 0));
16479 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16480 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
16481 }
16482
16483 case OFFSETOF_EXPR:
16484 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
16485 EXPR_LOCATION (t)));
16486
16487 case TRAIT_EXPR:
16488 {
16489 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
16490 complain, in_decl);
16491
16492 tree type2 = TRAIT_EXPR_TYPE2 (t);
16493 if (type2 && TREE_CODE (type2) == TREE_LIST)
16494 type2 = RECUR (type2);
16495 else if (type2)
16496 type2 = tsubst (type2, args, complain, in_decl);
16497
16498 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
16499 }
16500
16501 case STMT_EXPR:
16502 {
16503 tree old_stmt_expr = cur_stmt_expr;
16504 tree stmt_expr = begin_stmt_expr ();
16505
16506 cur_stmt_expr = stmt_expr;
16507 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
16508 integral_constant_expression_p);
16509 stmt_expr = finish_stmt_expr (stmt_expr, false);
16510 cur_stmt_expr = old_stmt_expr;
16511
16512 /* If the resulting list of expression statement is empty,
16513 fold it further into void_node. */
16514 if (empty_expr_stmt_p (stmt_expr))
16515 stmt_expr = void_node;
16516
16517 RETURN (stmt_expr);
16518 }
16519
16520 case LAMBDA_EXPR:
16521 {
16522 tree r = build_lambda_expr ();
16523
16524 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
16525 LAMBDA_EXPR_CLOSURE (r) = type;
16526 CLASSTYPE_LAMBDA_EXPR (type) = r;
16527
16528 LAMBDA_EXPR_LOCATION (r)
16529 = LAMBDA_EXPR_LOCATION (t);
16530 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16531 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16532 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16533 LAMBDA_EXPR_DISCRIMINATOR (r)
16534 = (LAMBDA_EXPR_DISCRIMINATOR (t));
16535 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
16536 if (!scope)
16537 /* No substitution needed. */;
16538 else if (VAR_OR_FUNCTION_DECL_P (scope))
16539 /* For a function or variable scope, we want to use tsubst so that we
16540 don't complain about referring to an auto before deduction. */
16541 scope = tsubst (scope, args, complain, in_decl);
16542 else if (TREE_CODE (scope) == PARM_DECL)
16543 {
16544 /* Look up the parameter we want directly, as tsubst_copy
16545 doesn't do what we need. */
16546 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
16547 tree parm = FUNCTION_FIRST_USER_PARM (fn);
16548 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
16549 parm = DECL_CHAIN (parm);
16550 scope = parm;
16551 /* FIXME Work around the parm not having DECL_CONTEXT set. */
16552 if (DECL_CONTEXT (scope) == NULL_TREE)
16553 DECL_CONTEXT (scope) = fn;
16554 }
16555 else if (TREE_CODE (scope) == FIELD_DECL)
16556 /* For a field, use tsubst_copy so that we look up the existing field
16557 rather than build a new one. */
16558 scope = RECUR (scope);
16559 else
16560 gcc_unreachable ();
16561 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
16562 LAMBDA_EXPR_RETURN_TYPE (r)
16563 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
16564
16565 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16566 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16567
16568 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16569 determine_visibility (TYPE_NAME (type));
16570 /* Now that we know visibility, instantiate the type so we have a
16571 declaration of the op() for later calls to lambda_function. */
16572 complete_type (type);
16573
16574 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16575
16576 insert_pending_capture_proxies ();
16577
16578 RETURN (build_lambda_object (r));
16579 }
16580
16581 case TARGET_EXPR:
16582 /* We can get here for a constant initializer of non-dependent type.
16583 FIXME stop folding in cp_parser_initializer_clause. */
16584 {
16585 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
16586 complain);
16587 RETURN (r);
16588 }
16589
16590 case TRANSACTION_EXPR:
16591 RETURN (tsubst_expr(t, args, complain, in_decl,
16592 integral_constant_expression_p));
16593
16594 case PAREN_EXPR:
16595 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
16596
16597 case VEC_PERM_EXPR:
16598 {
16599 tree op0 = RECUR (TREE_OPERAND (t, 0));
16600 tree op1 = RECUR (TREE_OPERAND (t, 1));
16601 tree op2 = RECUR (TREE_OPERAND (t, 2));
16602 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
16603 complain));
16604 }
16605
16606 case REQUIRES_EXPR:
16607 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
16608
16609 default:
16610 /* Handle Objective-C++ constructs, if appropriate. */
16611 {
16612 tree subst
16613 = objcp_tsubst_copy_and_build (t, args, complain,
16614 in_decl, /*function_p=*/false);
16615 if (subst)
16616 RETURN (subst);
16617 }
16618 RETURN (tsubst_copy (t, args, complain, in_decl));
16619 }
16620
16621 #undef RECUR
16622 #undef RETURN
16623 out:
16624 input_location = loc;
16625 return retval;
16626 }
16627
16628 /* Verify that the instantiated ARGS are valid. For type arguments,
16629 make sure that the type's linkage is ok. For non-type arguments,
16630 make sure they are constants if they are integral or enumerations.
16631 Emit an error under control of COMPLAIN, and return TRUE on error. */
16632
16633 static bool
16634 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
16635 {
16636 if (dependent_template_arg_p (t))
16637 return false;
16638 if (ARGUMENT_PACK_P (t))
16639 {
16640 tree vec = ARGUMENT_PACK_ARGS (t);
16641 int len = TREE_VEC_LENGTH (vec);
16642 bool result = false;
16643 int i;
16644
16645 for (i = 0; i < len; ++i)
16646 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
16647 result = true;
16648 return result;
16649 }
16650 else if (TYPE_P (t))
16651 {
16652 /* [basic.link]: A name with no linkage (notably, the name
16653 of a class or enumeration declared in a local scope)
16654 shall not be used to declare an entity with linkage.
16655 This implies that names with no linkage cannot be used as
16656 template arguments
16657
16658 DR 757 relaxes this restriction for C++0x. */
16659 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
16660 : no_linkage_check (t, /*relaxed_p=*/false));
16661
16662 if (nt)
16663 {
16664 /* DR 488 makes use of a type with no linkage cause
16665 type deduction to fail. */
16666 if (complain & tf_error)
16667 {
16668 if (TYPE_ANONYMOUS_P (nt))
16669 error ("%qT is/uses anonymous type", t);
16670 else
16671 error ("template argument for %qD uses local type %qT",
16672 tmpl, t);
16673 }
16674 return true;
16675 }
16676 /* In order to avoid all sorts of complications, we do not
16677 allow variably-modified types as template arguments. */
16678 else if (variably_modified_type_p (t, NULL_TREE))
16679 {
16680 if (complain & tf_error)
16681 error ("%qT is a variably modified type", t);
16682 return true;
16683 }
16684 }
16685 /* Class template and alias template arguments should be OK. */
16686 else if (DECL_TYPE_TEMPLATE_P (t))
16687 ;
16688 /* A non-type argument of integral or enumerated type must be a
16689 constant. */
16690 else if (TREE_TYPE (t)
16691 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
16692 && !REFERENCE_REF_P (t)
16693 && !TREE_CONSTANT (t))
16694 {
16695 if (complain & tf_error)
16696 error ("integral expression %qE is not constant", t);
16697 return true;
16698 }
16699 return false;
16700 }
16701
16702 static bool
16703 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
16704 {
16705 int ix, len = DECL_NTPARMS (tmpl);
16706 bool result = false;
16707
16708 for (ix = 0; ix != len; ix++)
16709 {
16710 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
16711 result = true;
16712 }
16713 if (result && (complain & tf_error))
16714 error (" trying to instantiate %qD", tmpl);
16715 return result;
16716 }
16717
16718 /* We're out of SFINAE context now, so generate diagnostics for the access
16719 errors we saw earlier when instantiating D from TMPL and ARGS. */
16720
16721 static void
16722 recheck_decl_substitution (tree d, tree tmpl, tree args)
16723 {
16724 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
16725 tree type = TREE_TYPE (pattern);
16726 location_t loc = input_location;
16727
16728 push_access_scope (d);
16729 push_deferring_access_checks (dk_no_deferred);
16730 input_location = DECL_SOURCE_LOCATION (pattern);
16731 tsubst (type, args, tf_warning_or_error, d);
16732 input_location = loc;
16733 pop_deferring_access_checks ();
16734 pop_access_scope (d);
16735 }
16736
16737 /* Instantiate the indicated variable, function, or alias template TMPL with
16738 the template arguments in TARG_PTR. */
16739
16740 static tree
16741 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
16742 {
16743 tree targ_ptr = orig_args;
16744 tree fndecl;
16745 tree gen_tmpl;
16746 tree spec;
16747 bool access_ok = true;
16748
16749 if (tmpl == error_mark_node)
16750 return error_mark_node;
16751
16752 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
16753
16754 /* If this function is a clone, handle it specially. */
16755 if (DECL_CLONED_FUNCTION_P (tmpl))
16756 {
16757 tree spec;
16758 tree clone;
16759
16760 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
16761 DECL_CLONED_FUNCTION. */
16762 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
16763 targ_ptr, complain);
16764 if (spec == error_mark_node)
16765 return error_mark_node;
16766
16767 /* Look for the clone. */
16768 FOR_EACH_CLONE (clone, spec)
16769 if (DECL_NAME (clone) == DECL_NAME (tmpl))
16770 return clone;
16771 /* We should always have found the clone by now. */
16772 gcc_unreachable ();
16773 return NULL_TREE;
16774 }
16775
16776 if (targ_ptr == error_mark_node)
16777 return error_mark_node;
16778
16779 /* Check to see if we already have this specialization. */
16780 gen_tmpl = most_general_template (tmpl);
16781 if (tmpl != gen_tmpl)
16782 /* The TMPL is a partial instantiation. To get a full set of
16783 arguments we must add the arguments used to perform the
16784 partial instantiation. */
16785 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
16786 targ_ptr);
16787
16788 /* It would be nice to avoid hashing here and then again in tsubst_decl,
16789 but it doesn't seem to be on the hot path. */
16790 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
16791
16792 gcc_assert (tmpl == gen_tmpl
16793 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
16794 == spec)
16795 || fndecl == NULL_TREE);
16796
16797 if (spec != NULL_TREE)
16798 {
16799 if (FNDECL_HAS_ACCESS_ERRORS (spec))
16800 {
16801 if (complain & tf_error)
16802 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
16803 return error_mark_node;
16804 }
16805 return spec;
16806 }
16807
16808 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
16809 complain))
16810 return error_mark_node;
16811
16812 /* We are building a FUNCTION_DECL, during which the access of its
16813 parameters and return types have to be checked. However this
16814 FUNCTION_DECL which is the desired context for access checking
16815 is not built yet. We solve this chicken-and-egg problem by
16816 deferring all checks until we have the FUNCTION_DECL. */
16817 push_deferring_access_checks (dk_deferred);
16818
16819 /* Instantiation of the function happens in the context of the function
16820 template, not the context of the overload resolution we're doing. */
16821 push_to_top_level ();
16822 /* If there are dependent arguments, e.g. because we're doing partial
16823 ordering, make sure processing_template_decl stays set. */
16824 if (uses_template_parms (targ_ptr))
16825 ++processing_template_decl;
16826 if (DECL_CLASS_SCOPE_P (gen_tmpl))
16827 {
16828 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
16829 complain, gen_tmpl, true);
16830 push_nested_class (ctx);
16831 }
16832
16833 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
16834
16835 if (VAR_P (pattern))
16836 {
16837 /* We need to determine if we're using a partial or explicit
16838 specialization now, because the type of the variable could be
16839 different. */
16840 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
16841 tree elt = most_specialized_partial_spec (tid, complain);
16842 if (elt == error_mark_node)
16843 pattern = error_mark_node;
16844 else if (elt)
16845 {
16846 tmpl = TREE_VALUE (elt);
16847 pattern = DECL_TEMPLATE_RESULT (tmpl);
16848 targ_ptr = TREE_PURPOSE (elt);
16849 }
16850 }
16851
16852 /* Substitute template parameters to obtain the specialization. */
16853 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
16854 if (DECL_CLASS_SCOPE_P (gen_tmpl))
16855 pop_nested_class ();
16856 pop_from_top_level ();
16857
16858 if (fndecl == error_mark_node)
16859 {
16860 pop_deferring_access_checks ();
16861 return error_mark_node;
16862 }
16863
16864 /* The DECL_TI_TEMPLATE should always be the immediate parent
16865 template, not the most general template. */
16866 DECL_TI_TEMPLATE (fndecl) = tmpl;
16867 DECL_TI_ARGS (fndecl) = targ_ptr;
16868
16869 /* Now we know the specialization, compute access previously
16870 deferred. */
16871 push_access_scope (fndecl);
16872 if (!perform_deferred_access_checks (complain))
16873 access_ok = false;
16874 pop_access_scope (fndecl);
16875 pop_deferring_access_checks ();
16876
16877 /* If we've just instantiated the main entry point for a function,
16878 instantiate all the alternate entry points as well. We do this
16879 by cloning the instantiation of the main entry point, not by
16880 instantiating the template clones. */
16881 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
16882 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
16883
16884 if (!access_ok)
16885 {
16886 if (!(complain & tf_error))
16887 {
16888 /* Remember to reinstantiate when we're out of SFINAE so the user
16889 can see the errors. */
16890 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
16891 }
16892 return error_mark_node;
16893 }
16894 return fndecl;
16895 }
16896
16897 /* Wrapper for instantiate_template_1. */
16898
16899 tree
16900 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
16901 {
16902 tree ret;
16903 timevar_push (TV_TEMPLATE_INST);
16904 ret = instantiate_template_1 (tmpl, orig_args, complain);
16905 timevar_pop (TV_TEMPLATE_INST);
16906 return ret;
16907 }
16908
16909 /* Instantiate the alias template TMPL with ARGS. Also push a template
16910 instantiation level, which instantiate_template doesn't do because
16911 functions and variables have sufficient context established by the
16912 callers. */
16913
16914 static tree
16915 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
16916 {
16917 struct pending_template *old_last_pend = last_pending_template;
16918 struct tinst_level *old_error_tinst = last_error_tinst_level;
16919 if (tmpl == error_mark_node || args == error_mark_node)
16920 return error_mark_node;
16921 tree tinst = build_tree_list (tmpl, args);
16922 if (!push_tinst_level (tinst))
16923 {
16924 ggc_free (tinst);
16925 return error_mark_node;
16926 }
16927
16928 args =
16929 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
16930 args, tmpl, complain,
16931 /*require_all_args=*/true,
16932 /*use_default_args=*/true);
16933
16934 tree r = instantiate_template (tmpl, args, complain);
16935 pop_tinst_level ();
16936 /* We can't free this if a pending_template entry or last_error_tinst_level
16937 is pointing at it. */
16938 if (last_pending_template == old_last_pend
16939 && last_error_tinst_level == old_error_tinst)
16940 ggc_free (tinst);
16941
16942 return r;
16943 }
16944
16945 /* PARM is a template parameter pack for FN. Returns true iff
16946 PARM is used in a deducible way in the argument list of FN. */
16947
16948 static bool
16949 pack_deducible_p (tree parm, tree fn)
16950 {
16951 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
16952 for (; t; t = TREE_CHAIN (t))
16953 {
16954 tree type = TREE_VALUE (t);
16955 tree packs;
16956 if (!PACK_EXPANSION_P (type))
16957 continue;
16958 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16959 packs; packs = TREE_CHAIN (packs))
16960 if (template_args_equal (TREE_VALUE (packs), parm))
16961 {
16962 /* The template parameter pack is used in a function parameter
16963 pack. If this is the end of the parameter list, the
16964 template parameter pack is deducible. */
16965 if (TREE_CHAIN (t) == void_list_node)
16966 return true;
16967 else
16968 /* Otherwise, not. Well, it could be deduced from
16969 a non-pack parameter, but doing so would end up with
16970 a deduction mismatch, so don't bother. */
16971 return false;
16972 }
16973 }
16974 /* The template parameter pack isn't used in any function parameter
16975 packs, but it might be used deeper, e.g. tuple<Args...>. */
16976 return true;
16977 }
16978
16979 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16980 NARGS elements of the arguments that are being used when calling
16981 it. TARGS is a vector into which the deduced template arguments
16982 are placed.
16983
16984 Returns either a FUNCTION_DECL for the matching specialization of FN or
16985 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16986 true, diagnostics will be printed to explain why it failed.
16987
16988 If FN is a conversion operator, or we are trying to produce a specific
16989 specialization, RETURN_TYPE is the return type desired.
16990
16991 The EXPLICIT_TARGS are explicit template arguments provided via a
16992 template-id.
16993
16994 The parameter STRICT is one of:
16995
16996 DEDUCE_CALL:
16997 We are deducing arguments for a function call, as in
16998 [temp.deduct.call].
16999
17000 DEDUCE_CONV:
17001 We are deducing arguments for a conversion function, as in
17002 [temp.deduct.conv].
17003
17004 DEDUCE_EXACT:
17005 We are deducing arguments when doing an explicit instantiation
17006 as in [temp.explicit], when determining an explicit specialization
17007 as in [temp.expl.spec], or when taking the address of a function
17008 template, as in [temp.deduct.funcaddr]. */
17009
17010 tree
17011 fn_type_unification (tree fn,
17012 tree explicit_targs,
17013 tree targs,
17014 const tree *args,
17015 unsigned int nargs,
17016 tree return_type,
17017 unification_kind_t strict,
17018 int flags,
17019 bool explain_p,
17020 bool decltype_p)
17021 {
17022 tree parms;
17023 tree fntype;
17024 tree decl = NULL_TREE;
17025 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17026 bool ok;
17027 static int deduction_depth;
17028 struct pending_template *old_last_pend = last_pending_template;
17029 struct tinst_level *old_error_tinst = last_error_tinst_level;
17030 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17031 tree tinst;
17032 tree r = error_mark_node;
17033
17034 if (decltype_p)
17035 complain |= tf_decltype;
17036
17037 /* In C++0x, it's possible to have a function template whose type depends
17038 on itself recursively. This is most obvious with decltype, but can also
17039 occur with enumeration scope (c++/48969). So we need to catch infinite
17040 recursion and reject the substitution at deduction time; this function
17041 will return error_mark_node for any repeated substitution.
17042
17043 This also catches excessive recursion such as when f<N> depends on
17044 f<N-1> across all integers, and returns error_mark_node for all the
17045 substitutions back up to the initial one.
17046
17047 This is, of course, not reentrant. */
17048 if (excessive_deduction_depth)
17049 return error_mark_node;
17050 tinst = build_tree_list (fn, NULL_TREE);
17051 ++deduction_depth;
17052
17053 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
17054
17055 fntype = TREE_TYPE (fn);
17056 if (explicit_targs)
17057 {
17058 /* [temp.deduct]
17059
17060 The specified template arguments must match the template
17061 parameters in kind (i.e., type, nontype, template), and there
17062 must not be more arguments than there are parameters;
17063 otherwise type deduction fails.
17064
17065 Nontype arguments must match the types of the corresponding
17066 nontype template parameters, or must be convertible to the
17067 types of the corresponding nontype parameters as specified in
17068 _temp.arg.nontype_, otherwise type deduction fails.
17069
17070 All references in the function type of the function template
17071 to the corresponding template parameters are replaced by the
17072 specified template argument values. If a substitution in a
17073 template parameter or in the function type of the function
17074 template results in an invalid type, type deduction fails. */
17075 int i, len = TREE_VEC_LENGTH (tparms);
17076 location_t loc = input_location;
17077 bool incomplete = false;
17078
17079 /* Adjust any explicit template arguments before entering the
17080 substitution context. */
17081 explicit_targs
17082 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
17083 complain,
17084 /*require_all_args=*/false,
17085 /*use_default_args=*/false));
17086 if (explicit_targs == error_mark_node)
17087 goto fail;
17088
17089 /* Substitute the explicit args into the function type. This is
17090 necessary so that, for instance, explicitly declared function
17091 arguments can match null pointed constants. If we were given
17092 an incomplete set of explicit args, we must not do semantic
17093 processing during substitution as we could create partial
17094 instantiations. */
17095 for (i = 0; i < len; i++)
17096 {
17097 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17098 bool parameter_pack = false;
17099 tree targ = TREE_VEC_ELT (explicit_targs, i);
17100
17101 /* Dig out the actual parm. */
17102 if (TREE_CODE (parm) == TYPE_DECL
17103 || TREE_CODE (parm) == TEMPLATE_DECL)
17104 {
17105 parm = TREE_TYPE (parm);
17106 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
17107 }
17108 else if (TREE_CODE (parm) == PARM_DECL)
17109 {
17110 parm = DECL_INITIAL (parm);
17111 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
17112 }
17113
17114 if (!parameter_pack && targ == NULL_TREE)
17115 /* No explicit argument for this template parameter. */
17116 incomplete = true;
17117
17118 if (parameter_pack && pack_deducible_p (parm, fn))
17119 {
17120 /* Mark the argument pack as "incomplete". We could
17121 still deduce more arguments during unification.
17122 We remove this mark in type_unification_real. */
17123 if (targ)
17124 {
17125 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
17126 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
17127 = ARGUMENT_PACK_ARGS (targ);
17128 }
17129
17130 /* We have some incomplete argument packs. */
17131 incomplete = true;
17132 }
17133 }
17134
17135 TREE_VALUE (tinst) = explicit_targs;
17136 if (!push_tinst_level (tinst))
17137 {
17138 excessive_deduction_depth = true;
17139 goto fail;
17140 }
17141 processing_template_decl += incomplete;
17142 input_location = DECL_SOURCE_LOCATION (fn);
17143 /* Ignore any access checks; we'll see them again in
17144 instantiate_template and they might have the wrong
17145 access path at this point. */
17146 push_deferring_access_checks (dk_deferred);
17147 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
17148 complain | tf_partial, NULL_TREE);
17149 pop_deferring_access_checks ();
17150 input_location = loc;
17151 processing_template_decl -= incomplete;
17152 pop_tinst_level ();
17153
17154 if (fntype == error_mark_node)
17155 goto fail;
17156
17157 /* Place the explicitly specified arguments in TARGS. */
17158 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
17159 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
17160 }
17161
17162 /* Never do unification on the 'this' parameter. */
17163 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
17164
17165 if (return_type)
17166 {
17167 tree *new_args;
17168
17169 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
17170 new_args = XALLOCAVEC (tree, nargs + 1);
17171 new_args[0] = return_type;
17172 memcpy (new_args + 1, args, nargs * sizeof (tree));
17173 args = new_args;
17174 ++nargs;
17175 }
17176
17177 /* We allow incomplete unification without an error message here
17178 because the standard doesn't seem to explicitly prohibit it. Our
17179 callers must be ready to deal with unification failures in any
17180 event. */
17181
17182 TREE_VALUE (tinst) = targs;
17183 /* If we aren't explaining yet, push tinst context so we can see where
17184 any errors (e.g. from class instantiations triggered by instantiation
17185 of default template arguments) come from. If we are explaining, this
17186 context is redundant. */
17187 if (!explain_p && !push_tinst_level (tinst))
17188 {
17189 excessive_deduction_depth = true;
17190 goto fail;
17191 }
17192
17193 /* type_unification_real will pass back any access checks from default
17194 template argument substitution. */
17195 vec<deferred_access_check, va_gc> *checks;
17196 checks = NULL;
17197
17198 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17199 targs, parms, args, nargs, /*subr=*/0,
17200 strict, flags, &checks, explain_p);
17201 if (!explain_p)
17202 pop_tinst_level ();
17203 if (!ok)
17204 goto fail;
17205
17206 /* Now that we have bindings for all of the template arguments,
17207 ensure that the arguments deduced for the template template
17208 parameters have compatible template parameter lists. We cannot
17209 check this property before we have deduced all template
17210 arguments, because the template parameter types of a template
17211 template parameter might depend on prior template parameters
17212 deduced after the template template parameter. The following
17213 ill-formed example illustrates this issue:
17214
17215 template<typename T, template<T> class C> void f(C<5>, T);
17216
17217 template<int N> struct X {};
17218
17219 void g() {
17220 f(X<5>(), 5l); // error: template argument deduction fails
17221 }
17222
17223 The template parameter list of 'C' depends on the template type
17224 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
17225 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
17226 time that we deduce 'C'. */
17227 if (!template_template_parm_bindings_ok_p
17228 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
17229 {
17230 unify_inconsistent_template_template_parameters (explain_p);
17231 goto fail;
17232 }
17233
17234 /* All is well so far. Now, check:
17235
17236 [temp.deduct]
17237
17238 When all template arguments have been deduced, all uses of
17239 template parameters in nondeduced contexts are replaced with
17240 the corresponding deduced argument values. If the
17241 substitution results in an invalid type, as described above,
17242 type deduction fails. */
17243 TREE_VALUE (tinst) = targs;
17244 if (!push_tinst_level (tinst))
17245 {
17246 excessive_deduction_depth = true;
17247 goto fail;
17248 }
17249
17250 /* Also collect access checks from the instantiation. */
17251 reopen_deferring_access_checks (checks);
17252
17253 decl = instantiate_template (fn, targs, complain);
17254
17255 checks = get_deferred_access_checks ();
17256 pop_deferring_access_checks ();
17257
17258 pop_tinst_level ();
17259
17260 if (decl == error_mark_node)
17261 goto fail;
17262
17263 /* Now perform any access checks encountered during substitution. */
17264 push_access_scope (decl);
17265 ok = perform_access_checks (checks, complain);
17266 pop_access_scope (decl);
17267 if (!ok)
17268 goto fail;
17269
17270 /* If we're looking for an exact match, check that what we got
17271 is indeed an exact match. It might not be if some template
17272 parameters are used in non-deduced contexts. But don't check
17273 for an exact match if we have dependent template arguments;
17274 in that case we're doing partial ordering, and we already know
17275 that we have two candidates that will provide the actual type. */
17276 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
17277 {
17278 tree substed = TREE_TYPE (decl);
17279 unsigned int i;
17280
17281 tree sarg
17282 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
17283 if (return_type)
17284 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
17285 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
17286 if (!same_type_p (args[i], TREE_VALUE (sarg)))
17287 {
17288 unify_type_mismatch (explain_p, args[i],
17289 TREE_VALUE (sarg));
17290 goto fail;
17291 }
17292 }
17293
17294 r = decl;
17295
17296 fail:
17297 --deduction_depth;
17298 if (excessive_deduction_depth)
17299 {
17300 if (deduction_depth == 0)
17301 /* Reset once we're all the way out. */
17302 excessive_deduction_depth = false;
17303 }
17304
17305 /* We can't free this if a pending_template entry or last_error_tinst_level
17306 is pointing at it. */
17307 if (last_pending_template == old_last_pend
17308 && last_error_tinst_level == old_error_tinst)
17309 ggc_free (tinst);
17310
17311 return r;
17312 }
17313
17314 /* Adjust types before performing type deduction, as described in
17315 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
17316 sections are symmetric. PARM is the type of a function parameter
17317 or the return type of the conversion function. ARG is the type of
17318 the argument passed to the call, or the type of the value
17319 initialized with the result of the conversion function.
17320 ARG_EXPR is the original argument expression, which may be null. */
17321
17322 static int
17323 maybe_adjust_types_for_deduction (unification_kind_t strict,
17324 tree* parm,
17325 tree* arg,
17326 tree arg_expr)
17327 {
17328 int result = 0;
17329
17330 switch (strict)
17331 {
17332 case DEDUCE_CALL:
17333 break;
17334
17335 case DEDUCE_CONV:
17336 /* Swap PARM and ARG throughout the remainder of this
17337 function; the handling is precisely symmetric since PARM
17338 will initialize ARG rather than vice versa. */
17339 std::swap (parm, arg);
17340 break;
17341
17342 case DEDUCE_EXACT:
17343 /* Core issue #873: Do the DR606 thing (see below) for these cases,
17344 too, but here handle it by stripping the reference from PARM
17345 rather than by adding it to ARG. */
17346 if (TREE_CODE (*parm) == REFERENCE_TYPE
17347 && TYPE_REF_IS_RVALUE (*parm)
17348 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17349 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17350 && TREE_CODE (*arg) == REFERENCE_TYPE
17351 && !TYPE_REF_IS_RVALUE (*arg))
17352 *parm = TREE_TYPE (*parm);
17353 /* Nothing else to do in this case. */
17354 return 0;
17355
17356 default:
17357 gcc_unreachable ();
17358 }
17359
17360 if (TREE_CODE (*parm) != REFERENCE_TYPE)
17361 {
17362 /* [temp.deduct.call]
17363
17364 If P is not a reference type:
17365
17366 --If A is an array type, the pointer type produced by the
17367 array-to-pointer standard conversion (_conv.array_) is
17368 used in place of A for type deduction; otherwise,
17369
17370 --If A is a function type, the pointer type produced by
17371 the function-to-pointer standard conversion
17372 (_conv.func_) is used in place of A for type deduction;
17373 otherwise,
17374
17375 --If A is a cv-qualified type, the top level
17376 cv-qualifiers of A's type are ignored for type
17377 deduction. */
17378 if (TREE_CODE (*arg) == ARRAY_TYPE)
17379 *arg = build_pointer_type (TREE_TYPE (*arg));
17380 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
17381 *arg = build_pointer_type (*arg);
17382 else
17383 *arg = TYPE_MAIN_VARIANT (*arg);
17384 }
17385
17386 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
17387 of the form T&&, where T is a template parameter, and the argument
17388 is an lvalue, T is deduced as A& */
17389 if (TREE_CODE (*parm) == REFERENCE_TYPE
17390 && TYPE_REF_IS_RVALUE (*parm)
17391 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
17392 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
17393 && (arg_expr ? real_lvalue_p (arg_expr)
17394 /* try_one_overload doesn't provide an arg_expr, but
17395 functions are always lvalues. */
17396 : TREE_CODE (*arg) == FUNCTION_TYPE))
17397 *arg = build_reference_type (*arg);
17398
17399 /* [temp.deduct.call]
17400
17401 If P is a cv-qualified type, the top level cv-qualifiers
17402 of P's type are ignored for type deduction. If P is a
17403 reference type, the type referred to by P is used for
17404 type deduction. */
17405 *parm = TYPE_MAIN_VARIANT (*parm);
17406 if (TREE_CODE (*parm) == REFERENCE_TYPE)
17407 {
17408 *parm = TREE_TYPE (*parm);
17409 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17410 }
17411
17412 /* DR 322. For conversion deduction, remove a reference type on parm
17413 too (which has been swapped into ARG). */
17414 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
17415 *arg = TREE_TYPE (*arg);
17416
17417 return result;
17418 }
17419
17420 /* Subroutine of unify_one_argument. PARM is a function parameter of a
17421 template which does contain any deducible template parameters; check if
17422 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
17423 unify_one_argument. */
17424
17425 static int
17426 check_non_deducible_conversion (tree parm, tree arg, int strict,
17427 int flags, bool explain_p)
17428 {
17429 tree type;
17430
17431 if (!TYPE_P (arg))
17432 type = TREE_TYPE (arg);
17433 else
17434 type = arg;
17435
17436 if (same_type_p (parm, type))
17437 return unify_success (explain_p);
17438
17439 if (strict == DEDUCE_CONV)
17440 {
17441 if (can_convert_arg (type, parm, NULL_TREE, flags,
17442 explain_p ? tf_warning_or_error : tf_none))
17443 return unify_success (explain_p);
17444 }
17445 else if (strict != DEDUCE_EXACT)
17446 {
17447 if (can_convert_arg (parm, type,
17448 TYPE_P (arg) ? NULL_TREE : arg,
17449 flags, explain_p ? tf_warning_or_error : tf_none))
17450 return unify_success (explain_p);
17451 }
17452
17453 if (strict == DEDUCE_EXACT)
17454 return unify_type_mismatch (explain_p, parm, arg);
17455 else
17456 return unify_arg_conversion (explain_p, parm, type, arg);
17457 }
17458
17459 static bool uses_deducible_template_parms (tree type);
17460
17461 /* Returns true iff the expression EXPR is one from which a template
17462 argument can be deduced. In other words, if it's an undecorated
17463 use of a template non-type parameter. */
17464
17465 static bool
17466 deducible_expression (tree expr)
17467 {
17468 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
17469 }
17470
17471 /* Returns true iff the array domain DOMAIN uses a template parameter in a
17472 deducible way; that is, if it has a max value of <PARM> - 1. */
17473
17474 static bool
17475 deducible_array_bound (tree domain)
17476 {
17477 if (domain == NULL_TREE)
17478 return false;
17479
17480 tree max = TYPE_MAX_VALUE (domain);
17481 if (TREE_CODE (max) != MINUS_EXPR)
17482 return false;
17483
17484 return deducible_expression (TREE_OPERAND (max, 0));
17485 }
17486
17487 /* Returns true iff the template arguments ARGS use a template parameter
17488 in a deducible way. */
17489
17490 static bool
17491 deducible_template_args (tree args)
17492 {
17493 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
17494 {
17495 bool deducible;
17496 tree elt = TREE_VEC_ELT (args, i);
17497 if (ARGUMENT_PACK_P (elt))
17498 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
17499 else
17500 {
17501 if (PACK_EXPANSION_P (elt))
17502 elt = PACK_EXPANSION_PATTERN (elt);
17503 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
17504 deducible = true;
17505 else if (TYPE_P (elt))
17506 deducible = uses_deducible_template_parms (elt);
17507 else
17508 deducible = deducible_expression (elt);
17509 }
17510 if (deducible)
17511 return true;
17512 }
17513 return false;
17514 }
17515
17516 /* Returns true iff TYPE contains any deducible references to template
17517 parameters, as per 14.8.2.5. */
17518
17519 static bool
17520 uses_deducible_template_parms (tree type)
17521 {
17522 if (PACK_EXPANSION_P (type))
17523 type = PACK_EXPANSION_PATTERN (type);
17524
17525 /* T
17526 cv-list T
17527 TT<T>
17528 TT<i>
17529 TT<> */
17530 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17531 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17532 return true;
17533
17534 /* T*
17535 T&
17536 T&& */
17537 if (POINTER_TYPE_P (type))
17538 return uses_deducible_template_parms (TREE_TYPE (type));
17539
17540 /* T[integer-constant ]
17541 type [i] */
17542 if (TREE_CODE (type) == ARRAY_TYPE)
17543 return (uses_deducible_template_parms (TREE_TYPE (type))
17544 || deducible_array_bound (TYPE_DOMAIN (type)));
17545
17546 /* T type ::*
17547 type T::*
17548 T T::*
17549 T (type ::*)()
17550 type (T::*)()
17551 type (type ::*)(T)
17552 type (T::*)(T)
17553 T (type ::*)(T)
17554 T (T::*)()
17555 T (T::*)(T) */
17556 if (TYPE_PTRMEM_P (type))
17557 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
17558 || (uses_deducible_template_parms
17559 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
17560
17561 /* template-name <T> (where template-name refers to a class template)
17562 template-name <i> (where template-name refers to a class template) */
17563 if (CLASS_TYPE_P (type)
17564 && CLASSTYPE_TEMPLATE_INFO (type)
17565 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
17566 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
17567 (CLASSTYPE_TI_ARGS (type)));
17568
17569 /* type (T)
17570 T()
17571 T(T) */
17572 if (TREE_CODE (type) == FUNCTION_TYPE
17573 || TREE_CODE (type) == METHOD_TYPE)
17574 {
17575 if (uses_deducible_template_parms (TREE_TYPE (type)))
17576 return true;
17577 tree parm = TYPE_ARG_TYPES (type);
17578 if (TREE_CODE (type) == METHOD_TYPE)
17579 parm = TREE_CHAIN (parm);
17580 for (; parm; parm = TREE_CHAIN (parm))
17581 if (uses_deducible_template_parms (TREE_VALUE (parm)))
17582 return true;
17583 }
17584
17585 return false;
17586 }
17587
17588 /* Subroutine of type_unification_real and unify_pack_expansion to
17589 handle unification of a single P/A pair. Parameters are as
17590 for those functions. */
17591
17592 static int
17593 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
17594 int subr, unification_kind_t strict,
17595 bool explain_p)
17596 {
17597 tree arg_expr = NULL_TREE;
17598 int arg_strict;
17599
17600 if (arg == error_mark_node || parm == error_mark_node)
17601 return unify_invalid (explain_p);
17602 if (arg == unknown_type_node)
17603 /* We can't deduce anything from this, but we might get all the
17604 template args from other function args. */
17605 return unify_success (explain_p);
17606
17607 /* Implicit conversions (Clause 4) will be performed on a function
17608 argument to convert it to the type of the corresponding function
17609 parameter if the parameter type contains no template-parameters that
17610 participate in template argument deduction. */
17611 if (strict != DEDUCE_EXACT
17612 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
17613 /* For function parameters with no deducible template parameters,
17614 just return. We'll check non-dependent conversions later. */
17615 return unify_success (explain_p);
17616
17617 switch (strict)
17618 {
17619 case DEDUCE_CALL:
17620 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
17621 | UNIFY_ALLOW_MORE_CV_QUAL
17622 | UNIFY_ALLOW_DERIVED);
17623 break;
17624
17625 case DEDUCE_CONV:
17626 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
17627 break;
17628
17629 case DEDUCE_EXACT:
17630 arg_strict = UNIFY_ALLOW_NONE;
17631 break;
17632
17633 default:
17634 gcc_unreachable ();
17635 }
17636
17637 /* We only do these transformations if this is the top-level
17638 parameter_type_list in a call or declaration matching; in other
17639 situations (nested function declarators, template argument lists) we
17640 won't be comparing a type to an expression, and we don't do any type
17641 adjustments. */
17642 if (!subr)
17643 {
17644 if (!TYPE_P (arg))
17645 {
17646 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
17647 if (type_unknown_p (arg))
17648 {
17649 /* [temp.deduct.type] A template-argument can be
17650 deduced from a pointer to function or pointer
17651 to member function argument if the set of
17652 overloaded functions does not contain function
17653 templates and at most one of a set of
17654 overloaded functions provides a unique
17655 match. */
17656
17657 if (resolve_overloaded_unification
17658 (tparms, targs, parm, arg, strict,
17659 arg_strict, explain_p))
17660 return unify_success (explain_p);
17661 return unify_overload_resolution_failure (explain_p, arg);
17662 }
17663
17664 arg_expr = arg;
17665 arg = unlowered_expr_type (arg);
17666 if (arg == error_mark_node)
17667 return unify_invalid (explain_p);
17668 }
17669
17670 arg_strict |=
17671 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
17672 }
17673 else
17674 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
17675 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
17676 return unify_template_argument_mismatch (explain_p, parm, arg);
17677
17678 /* For deduction from an init-list we need the actual list. */
17679 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
17680 arg = arg_expr;
17681 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
17682 }
17683
17684 /* Most parms like fn_type_unification.
17685
17686 If SUBR is 1, we're being called recursively (to unify the
17687 arguments of a function or method parameter of a function
17688 template).
17689
17690 CHECKS is a pointer to a vector of access checks encountered while
17691 substituting default template arguments. */
17692
17693 static int
17694 type_unification_real (tree tparms,
17695 tree targs,
17696 tree xparms,
17697 const tree *xargs,
17698 unsigned int xnargs,
17699 int subr,
17700 unification_kind_t strict,
17701 int flags,
17702 vec<deferred_access_check, va_gc> **checks,
17703 bool explain_p)
17704 {
17705 tree parm, arg;
17706 int i;
17707 int ntparms = TREE_VEC_LENGTH (tparms);
17708 int saw_undeduced = 0;
17709 tree parms;
17710 const tree *args;
17711 unsigned int nargs;
17712 unsigned int ia;
17713
17714 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
17715 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
17716 gcc_assert (ntparms > 0);
17717
17718 /* Reset the number of non-defaulted template arguments contained
17719 in TARGS. */
17720 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
17721
17722 again:
17723 parms = xparms;
17724 args = xargs;
17725 nargs = xnargs;
17726
17727 ia = 0;
17728 while (parms && parms != void_list_node
17729 && ia < nargs)
17730 {
17731 parm = TREE_VALUE (parms);
17732
17733 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
17734 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
17735 /* For a function parameter pack that occurs at the end of the
17736 parameter-declaration-list, the type A of each remaining
17737 argument of the call is compared with the type P of the
17738 declarator-id of the function parameter pack. */
17739 break;
17740
17741 parms = TREE_CHAIN (parms);
17742
17743 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
17744 /* For a function parameter pack that does not occur at the
17745 end of the parameter-declaration-list, the type of the
17746 parameter pack is a non-deduced context. */
17747 continue;
17748
17749 arg = args[ia];
17750 ++ia;
17751
17752 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17753 explain_p))
17754 return 1;
17755 }
17756
17757 if (parms
17758 && parms != void_list_node
17759 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
17760 {
17761 /* Unify the remaining arguments with the pack expansion type. */
17762 tree argvec;
17763 tree parmvec = make_tree_vec (1);
17764
17765 /* Allocate a TREE_VEC and copy in all of the arguments */
17766 argvec = make_tree_vec (nargs - ia);
17767 for (i = 0; ia < nargs; ++ia, ++i)
17768 TREE_VEC_ELT (argvec, i) = args[ia];
17769
17770 /* Copy the parameter into parmvec. */
17771 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
17772 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
17773 /*subr=*/subr, explain_p))
17774 return 1;
17775
17776 /* Advance to the end of the list of parameters. */
17777 parms = TREE_CHAIN (parms);
17778 }
17779
17780 /* Fail if we've reached the end of the parm list, and more args
17781 are present, and the parm list isn't variadic. */
17782 if (ia < nargs && parms == void_list_node)
17783 return unify_too_many_arguments (explain_p, nargs, ia);
17784 /* Fail if parms are left and they don't have default values and
17785 they aren't all deduced as empty packs (c++/57397). This is
17786 consistent with sufficient_parms_p. */
17787 if (parms && parms != void_list_node
17788 && TREE_PURPOSE (parms) == NULL_TREE)
17789 {
17790 unsigned int count = nargs;
17791 tree p = parms;
17792 bool type_pack_p;
17793 do
17794 {
17795 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
17796 if (!type_pack_p)
17797 count++;
17798 p = TREE_CHAIN (p);
17799 }
17800 while (p && p != void_list_node);
17801 if (count != nargs)
17802 return unify_too_few_arguments (explain_p, ia, count,
17803 type_pack_p);
17804 }
17805
17806 if (!subr)
17807 {
17808 tsubst_flags_t complain = (explain_p
17809 ? tf_warning_or_error
17810 : tf_none);
17811
17812 for (i = 0; i < ntparms; i++)
17813 {
17814 tree targ = TREE_VEC_ELT (targs, i);
17815 tree tparm = TREE_VEC_ELT (tparms, i);
17816
17817 /* Clear the "incomplete" flags on all argument packs now so that
17818 substituting them into later default arguments works. */
17819 if (targ && ARGUMENT_PACK_P (targ))
17820 {
17821 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
17822 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
17823 }
17824
17825 if (targ || tparm == error_mark_node)
17826 continue;
17827 tparm = TREE_VALUE (tparm);
17828
17829 /* If this is an undeduced nontype parameter that depends on
17830 a type parameter, try another pass; its type may have been
17831 deduced from a later argument than the one from which
17832 this parameter can be deduced. */
17833 if (TREE_CODE (tparm) == PARM_DECL
17834 && uses_template_parms (TREE_TYPE (tparm))
17835 && saw_undeduced < 2)
17836 {
17837 saw_undeduced = 1;
17838 continue;
17839 }
17840
17841 /* Core issue #226 (C++0x) [temp.deduct]:
17842
17843 If a template argument has not been deduced, its
17844 default template argument, if any, is used.
17845
17846 When we are in C++98 mode, TREE_PURPOSE will either
17847 be NULL_TREE or ERROR_MARK_NODE, so we do not need
17848 to explicitly check cxx_dialect here. */
17849 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
17850 /* OK, there is a default argument. Wait until after the
17851 conversion check to do substitution. */
17852 continue;
17853
17854 /* If the type parameter is a parameter pack, then it will
17855 be deduced to an empty parameter pack. */
17856 if (template_parameter_pack_p (tparm))
17857 {
17858 tree arg;
17859
17860 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
17861 {
17862 arg = make_node (NONTYPE_ARGUMENT_PACK);
17863 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
17864 TREE_CONSTANT (arg) = 1;
17865 }
17866 else
17867 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
17868
17869 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
17870
17871 TREE_VEC_ELT (targs, i) = arg;
17872 continue;
17873 }
17874
17875 return unify_parameter_deduction_failure (explain_p, tparm);
17876 }
17877
17878 /* DR 1391: All parameters have args, now check non-dependent parms for
17879 convertibility. */
17880 if (saw_undeduced < 2)
17881 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
17882 parms && parms != void_list_node && ia < nargs; )
17883 {
17884 parm = TREE_VALUE (parms);
17885
17886 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
17887 && (!TREE_CHAIN (parms)
17888 || TREE_CHAIN (parms) == void_list_node))
17889 /* For a function parameter pack that occurs at the end of the
17890 parameter-declaration-list, the type A of each remaining
17891 argument of the call is compared with the type P of the
17892 declarator-id of the function parameter pack. */
17893 break;
17894
17895 parms = TREE_CHAIN (parms);
17896
17897 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
17898 /* For a function parameter pack that does not occur at the
17899 end of the parameter-declaration-list, the type of the
17900 parameter pack is a non-deduced context. */
17901 continue;
17902
17903 arg = args[ia];
17904 ++ia;
17905
17906 if (uses_template_parms (parm))
17907 continue;
17908 if (check_non_deducible_conversion (parm, arg, strict, flags,
17909 explain_p))
17910 return 1;
17911 }
17912
17913 /* Now substitute into the default template arguments. */
17914 for (i = 0; i < ntparms; i++)
17915 {
17916 tree targ = TREE_VEC_ELT (targs, i);
17917 tree tparm = TREE_VEC_ELT (tparms, i);
17918
17919 if (targ || tparm == error_mark_node)
17920 continue;
17921 tree parm = TREE_VALUE (tparm);
17922
17923 if (TREE_CODE (parm) == PARM_DECL
17924 && uses_template_parms (TREE_TYPE (parm))
17925 && saw_undeduced < 2)
17926 continue;
17927
17928 tree arg = TREE_PURPOSE (tparm);
17929 reopen_deferring_access_checks (*checks);
17930 location_t save_loc = input_location;
17931 if (DECL_P (parm))
17932 input_location = DECL_SOURCE_LOCATION (parm);
17933 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
17934 arg = convert_template_argument (parm, arg, targs, complain,
17935 i, NULL_TREE);
17936 input_location = save_loc;
17937 *checks = get_deferred_access_checks ();
17938 pop_deferring_access_checks ();
17939 if (arg == error_mark_node)
17940 return 1;
17941 else
17942 {
17943 TREE_VEC_ELT (targs, i) = arg;
17944 /* The position of the first default template argument,
17945 is also the number of non-defaulted arguments in TARGS.
17946 Record that. */
17947 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17948 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
17949 continue;
17950 }
17951 }
17952
17953 if (saw_undeduced++ == 1)
17954 goto again;
17955 }
17956 #ifdef ENABLE_CHECKING
17957 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17958 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
17959 #endif
17960
17961 return unify_success (explain_p);
17962 }
17963
17964 /* Subroutine of type_unification_real. Args are like the variables
17965 at the call site. ARG is an overloaded function (or template-id);
17966 we try deducing template args from each of the overloads, and if
17967 only one succeeds, we go with that. Modifies TARGS and returns
17968 true on success. */
17969
17970 static bool
17971 resolve_overloaded_unification (tree tparms,
17972 tree targs,
17973 tree parm,
17974 tree arg,
17975 unification_kind_t strict,
17976 int sub_strict,
17977 bool explain_p)
17978 {
17979 tree tempargs = copy_node (targs);
17980 int good = 0;
17981 tree goodfn = NULL_TREE;
17982 bool addr_p;
17983
17984 if (TREE_CODE (arg) == ADDR_EXPR)
17985 {
17986 arg = TREE_OPERAND (arg, 0);
17987 addr_p = true;
17988 }
17989 else
17990 addr_p = false;
17991
17992 if (TREE_CODE (arg) == COMPONENT_REF)
17993 /* Handle `&x' where `x' is some static or non-static member
17994 function name. */
17995 arg = TREE_OPERAND (arg, 1);
17996
17997 if (TREE_CODE (arg) == OFFSET_REF)
17998 arg = TREE_OPERAND (arg, 1);
17999
18000 /* Strip baselink information. */
18001 if (BASELINK_P (arg))
18002 arg = BASELINK_FUNCTIONS (arg);
18003
18004 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
18005 {
18006 /* If we got some explicit template args, we need to plug them into
18007 the affected templates before we try to unify, in case the
18008 explicit args will completely resolve the templates in question. */
18009
18010 int ok = 0;
18011 tree expl_subargs = TREE_OPERAND (arg, 1);
18012 arg = TREE_OPERAND (arg, 0);
18013
18014 for (; arg; arg = OVL_NEXT (arg))
18015 {
18016 tree fn = OVL_CURRENT (arg);
18017 tree subargs, elem;
18018
18019 if (TREE_CODE (fn) != TEMPLATE_DECL)
18020 continue;
18021
18022 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18023 expl_subargs, NULL_TREE, tf_none,
18024 /*require_all_args=*/true,
18025 /*use_default_args=*/true);
18026 if (subargs != error_mark_node
18027 && !any_dependent_template_arguments_p (subargs))
18028 {
18029 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
18030 if (try_one_overload (tparms, targs, tempargs, parm,
18031 elem, strict, sub_strict, addr_p, explain_p)
18032 && (!goodfn || !same_type_p (goodfn, elem)))
18033 {
18034 goodfn = elem;
18035 ++good;
18036 }
18037 }
18038 else if (subargs)
18039 ++ok;
18040 }
18041 /* If no templates (or more than one) are fully resolved by the
18042 explicit arguments, this template-id is a non-deduced context; it
18043 could still be OK if we deduce all template arguments for the
18044 enclosing call through other arguments. */
18045 if (good != 1)
18046 good = ok;
18047 }
18048 else if (TREE_CODE (arg) != OVERLOAD
18049 && TREE_CODE (arg) != FUNCTION_DECL)
18050 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18051 -- but the deduction does not succeed because the expression is
18052 not just the function on its own. */
18053 return false;
18054 else
18055 for (; arg; arg = OVL_NEXT (arg))
18056 if (try_one_overload (tparms, targs, tempargs, parm,
18057 TREE_TYPE (OVL_CURRENT (arg)),
18058 strict, sub_strict, addr_p, explain_p)
18059 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
18060 {
18061 goodfn = OVL_CURRENT (arg);
18062 ++good;
18063 }
18064
18065 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18066 to function or pointer to member function argument if the set of
18067 overloaded functions does not contain function templates and at most
18068 one of a set of overloaded functions provides a unique match.
18069
18070 So if we found multiple possibilities, we return success but don't
18071 deduce anything. */
18072
18073 if (good == 1)
18074 {
18075 int i = TREE_VEC_LENGTH (targs);
18076 for (; i--; )
18077 if (TREE_VEC_ELT (tempargs, i))
18078 {
18079 tree old = TREE_VEC_ELT (targs, i);
18080 tree new_ = TREE_VEC_ELT (tempargs, i);
18081 if (new_ && old && ARGUMENT_PACK_P (old)
18082 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
18083 /* Don't forget explicit template arguments in a pack. */
18084 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
18085 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
18086 TREE_VEC_ELT (targs, i) = new_;
18087 }
18088 }
18089 if (good)
18090 return true;
18091
18092 return false;
18093 }
18094
18095 /* Core DR 115: In contexts where deduction is done and fails, or in
18096 contexts where deduction is not done, if a template argument list is
18097 specified and it, along with any default template arguments, identifies
18098 a single function template specialization, then the template-id is an
18099 lvalue for the function template specialization. */
18100
18101 tree
18102 resolve_nondeduced_context (tree orig_expr)
18103 {
18104 tree expr, offset, baselink;
18105 bool addr;
18106
18107 if (!type_unknown_p (orig_expr))
18108 return orig_expr;
18109
18110 expr = orig_expr;
18111 addr = false;
18112 offset = NULL_TREE;
18113 baselink = NULL_TREE;
18114
18115 if (TREE_CODE (expr) == ADDR_EXPR)
18116 {
18117 expr = TREE_OPERAND (expr, 0);
18118 addr = true;
18119 }
18120 if (TREE_CODE (expr) == OFFSET_REF)
18121 {
18122 offset = expr;
18123 expr = TREE_OPERAND (expr, 1);
18124 }
18125 if (BASELINK_P (expr))
18126 {
18127 baselink = expr;
18128 expr = BASELINK_FUNCTIONS (expr);
18129 }
18130
18131 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
18132 {
18133 int good = 0;
18134 tree goodfn = NULL_TREE;
18135
18136 /* If we got some explicit template args, we need to plug them into
18137 the affected templates before we try to unify, in case the
18138 explicit args will completely resolve the templates in question. */
18139
18140 tree expl_subargs = TREE_OPERAND (expr, 1);
18141 tree arg = TREE_OPERAND (expr, 0);
18142 tree badfn = NULL_TREE;
18143 tree badargs = NULL_TREE;
18144
18145 for (; arg; arg = OVL_NEXT (arg))
18146 {
18147 tree fn = OVL_CURRENT (arg);
18148 tree subargs, elem;
18149
18150 if (TREE_CODE (fn) != TEMPLATE_DECL)
18151 continue;
18152
18153 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18154 expl_subargs, NULL_TREE, tf_none,
18155 /*require_all_args=*/true,
18156 /*use_default_args=*/true);
18157 if (subargs != error_mark_node
18158 && !any_dependent_template_arguments_p (subargs))
18159 {
18160 elem = instantiate_template (fn, subargs, tf_none);
18161 if (elem == error_mark_node)
18162 {
18163 badfn = fn;
18164 badargs = subargs;
18165 }
18166 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
18167 {
18168 goodfn = elem;
18169 ++good;
18170 }
18171 }
18172 }
18173 if (good == 1)
18174 {
18175 mark_used (goodfn);
18176 expr = goodfn;
18177 if (baselink)
18178 expr = build_baselink (BASELINK_BINFO (baselink),
18179 BASELINK_ACCESS_BINFO (baselink),
18180 expr, BASELINK_OPTYPE (baselink));
18181 if (offset)
18182 {
18183 tree base
18184 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
18185 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
18186 }
18187 if (addr)
18188 expr = cp_build_addr_expr (expr, tf_warning_or_error);
18189 return expr;
18190 }
18191 else if (good == 0 && badargs)
18192 /* There were no good options and at least one bad one, so let the
18193 user know what the problem is. */
18194 instantiate_template (badfn, badargs, tf_warning_or_error);
18195 }
18196 return orig_expr;
18197 }
18198
18199 /* Subroutine of resolve_overloaded_unification; does deduction for a single
18200 overload. Fills TARGS with any deduced arguments, or error_mark_node if
18201 different overloads deduce different arguments for a given parm.
18202 ADDR_P is true if the expression for which deduction is being
18203 performed was of the form "& fn" rather than simply "fn".
18204
18205 Returns 1 on success. */
18206
18207 static int
18208 try_one_overload (tree tparms,
18209 tree orig_targs,
18210 tree targs,
18211 tree parm,
18212 tree arg,
18213 unification_kind_t strict,
18214 int sub_strict,
18215 bool addr_p,
18216 bool explain_p)
18217 {
18218 int nargs;
18219 tree tempargs;
18220 int i;
18221
18222 if (arg == error_mark_node)
18223 return 0;
18224
18225 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18226 to function or pointer to member function argument if the set of
18227 overloaded functions does not contain function templates and at most
18228 one of a set of overloaded functions provides a unique match.
18229
18230 So if this is a template, just return success. */
18231
18232 if (uses_template_parms (arg))
18233 return 1;
18234
18235 if (TREE_CODE (arg) == METHOD_TYPE)
18236 arg = build_ptrmemfunc_type (build_pointer_type (arg));
18237 else if (addr_p)
18238 arg = build_pointer_type (arg);
18239
18240 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
18241
18242 /* We don't copy orig_targs for this because if we have already deduced
18243 some template args from previous args, unify would complain when we
18244 try to deduce a template parameter for the same argument, even though
18245 there isn't really a conflict. */
18246 nargs = TREE_VEC_LENGTH (targs);
18247 tempargs = make_tree_vec (nargs);
18248
18249 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
18250 return 0;
18251
18252 /* First make sure we didn't deduce anything that conflicts with
18253 explicitly specified args. */
18254 for (i = nargs; i--; )
18255 {
18256 tree elt = TREE_VEC_ELT (tempargs, i);
18257 tree oldelt = TREE_VEC_ELT (orig_targs, i);
18258
18259 if (!elt)
18260 /*NOP*/;
18261 else if (uses_template_parms (elt))
18262 /* Since we're unifying against ourselves, we will fill in
18263 template args used in the function parm list with our own
18264 template parms. Discard them. */
18265 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
18266 else if (oldelt && !template_args_equal (oldelt, elt))
18267 return 0;
18268 }
18269
18270 for (i = nargs; i--; )
18271 {
18272 tree elt = TREE_VEC_ELT (tempargs, i);
18273
18274 if (elt)
18275 TREE_VEC_ELT (targs, i) = elt;
18276 }
18277
18278 return 1;
18279 }
18280
18281 /* PARM is a template class (perhaps with unbound template
18282 parameters). ARG is a fully instantiated type. If ARG can be
18283 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
18284 TARGS are as for unify. */
18285
18286 static tree
18287 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
18288 bool explain_p)
18289 {
18290 tree copy_of_targs;
18291
18292 if (!CLASSTYPE_TEMPLATE_INFO (arg)
18293 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
18294 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
18295 return NULL_TREE;
18296
18297 /* We need to make a new template argument vector for the call to
18298 unify. If we used TARGS, we'd clutter it up with the result of
18299 the attempted unification, even if this class didn't work out.
18300 We also don't want to commit ourselves to all the unifications
18301 we've already done, since unification is supposed to be done on
18302 an argument-by-argument basis. In other words, consider the
18303 following pathological case:
18304
18305 template <int I, int J, int K>
18306 struct S {};
18307
18308 template <int I, int J>
18309 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
18310
18311 template <int I, int J, int K>
18312 void f(S<I, J, K>, S<I, I, I>);
18313
18314 void g() {
18315 S<0, 0, 0> s0;
18316 S<0, 1, 2> s2;
18317
18318 f(s0, s2);
18319 }
18320
18321 Now, by the time we consider the unification involving `s2', we
18322 already know that we must have `f<0, 0, 0>'. But, even though
18323 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
18324 because there are two ways to unify base classes of S<0, 1, 2>
18325 with S<I, I, I>. If we kept the already deduced knowledge, we
18326 would reject the possibility I=1. */
18327 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
18328
18329 /* If unification failed, we're done. */
18330 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
18331 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
18332 return NULL_TREE;
18333
18334 return arg;
18335 }
18336
18337 /* Given a template type PARM and a class type ARG, find the unique
18338 base type in ARG that is an instance of PARM. We do not examine
18339 ARG itself; only its base-classes. If there is not exactly one
18340 appropriate base class, return NULL_TREE. PARM may be the type of
18341 a partial specialization, as well as a plain template type. Used
18342 by unify. */
18343
18344 static enum template_base_result
18345 get_template_base (tree tparms, tree targs, tree parm, tree arg,
18346 bool explain_p, tree *result)
18347 {
18348 tree rval = NULL_TREE;
18349 tree binfo;
18350
18351 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
18352
18353 binfo = TYPE_BINFO (complete_type (arg));
18354 if (!binfo)
18355 {
18356 /* The type could not be completed. */
18357 *result = NULL_TREE;
18358 return tbr_incomplete_type;
18359 }
18360
18361 /* Walk in inheritance graph order. The search order is not
18362 important, and this avoids multiple walks of virtual bases. */
18363 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
18364 {
18365 tree r = try_class_unification (tparms, targs, parm,
18366 BINFO_TYPE (binfo), explain_p);
18367
18368 if (r)
18369 {
18370 /* If there is more than one satisfactory baseclass, then:
18371
18372 [temp.deduct.call]
18373
18374 If they yield more than one possible deduced A, the type
18375 deduction fails.
18376
18377 applies. */
18378 if (rval && !same_type_p (r, rval))
18379 {
18380 *result = NULL_TREE;
18381 return tbr_ambiguous_baseclass;
18382 }
18383
18384 rval = r;
18385 }
18386 }
18387
18388 *result = rval;
18389 return tbr_success;
18390 }
18391
18392 /* Returns the level of DECL, which declares a template parameter. */
18393
18394 static int
18395 template_decl_level (tree decl)
18396 {
18397 switch (TREE_CODE (decl))
18398 {
18399 case TYPE_DECL:
18400 case TEMPLATE_DECL:
18401 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
18402
18403 case PARM_DECL:
18404 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
18405
18406 default:
18407 gcc_unreachable ();
18408 }
18409 return 0;
18410 }
18411
18412 /* Decide whether ARG can be unified with PARM, considering only the
18413 cv-qualifiers of each type, given STRICT as documented for unify.
18414 Returns nonzero iff the unification is OK on that basis. */
18415
18416 static int
18417 check_cv_quals_for_unify (int strict, tree arg, tree parm)
18418 {
18419 int arg_quals = cp_type_quals (arg);
18420 int parm_quals = cp_type_quals (parm);
18421
18422 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18423 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18424 {
18425 /* Although a CVR qualifier is ignored when being applied to a
18426 substituted template parameter ([8.3.2]/1 for example), that
18427 does not allow us to unify "const T" with "int&" because both
18428 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
18429 It is ok when we're allowing additional CV qualifiers
18430 at the outer level [14.8.2.1]/3,1st bullet. */
18431 if ((TREE_CODE (arg) == REFERENCE_TYPE
18432 || TREE_CODE (arg) == FUNCTION_TYPE
18433 || TREE_CODE (arg) == METHOD_TYPE)
18434 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
18435 return 0;
18436
18437 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
18438 && (parm_quals & TYPE_QUAL_RESTRICT))
18439 return 0;
18440 }
18441
18442 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
18443 && (arg_quals & parm_quals) != parm_quals)
18444 return 0;
18445
18446 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
18447 && (parm_quals & arg_quals) != arg_quals)
18448 return 0;
18449
18450 return 1;
18451 }
18452
18453 /* Determines the LEVEL and INDEX for the template parameter PARM. */
18454 void
18455 template_parm_level_and_index (tree parm, int* level, int* index)
18456 {
18457 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18458 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18459 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18460 {
18461 *index = TEMPLATE_TYPE_IDX (parm);
18462 *level = TEMPLATE_TYPE_LEVEL (parm);
18463 }
18464 else
18465 {
18466 *index = TEMPLATE_PARM_IDX (parm);
18467 *level = TEMPLATE_PARM_LEVEL (parm);
18468 }
18469 }
18470
18471 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
18472 do { \
18473 if (unify (TP, TA, P, A, S, EP)) \
18474 return 1; \
18475 } while (0);
18476
18477 /* Unifies the remaining arguments in PACKED_ARGS with the pack
18478 expansion at the end of PACKED_PARMS. Returns 0 if the type
18479 deduction succeeds, 1 otherwise. STRICT is the same as in
18480 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
18481 call argument list. We'll need to adjust the arguments to make them
18482 types. SUBR tells us if this is from a recursive call to
18483 type_unification_real, or for comparing two template argument
18484 lists. */
18485
18486 static int
18487 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
18488 tree packed_args, unification_kind_t strict,
18489 bool subr, bool explain_p)
18490 {
18491 tree parm
18492 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
18493 tree pattern = PACK_EXPANSION_PATTERN (parm);
18494 tree pack, packs = NULL_TREE;
18495 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
18496
18497 packed_args = expand_template_argument_pack (packed_args);
18498
18499 int len = TREE_VEC_LENGTH (packed_args);
18500
18501 /* Determine the parameter packs we will be deducing from the
18502 pattern, and record their current deductions. */
18503 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
18504 pack; pack = TREE_CHAIN (pack))
18505 {
18506 tree parm_pack = TREE_VALUE (pack);
18507 int idx, level;
18508
18509 /* Determine the index and level of this parameter pack. */
18510 template_parm_level_and_index (parm_pack, &level, &idx);
18511
18512 /* Keep track of the parameter packs and their corresponding
18513 argument packs. */
18514 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
18515 TREE_TYPE (packs) = make_tree_vec (len - start);
18516 }
18517
18518 /* Loop through all of the arguments that have not yet been
18519 unified and unify each with the pattern. */
18520 for (i = start; i < len; i++)
18521 {
18522 tree parm;
18523 bool any_explicit = false;
18524 tree arg = TREE_VEC_ELT (packed_args, i);
18525
18526 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
18527 or the element of its argument pack at the current index if
18528 this argument was explicitly specified. */
18529 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18530 {
18531 int idx, level;
18532 tree arg, pargs;
18533 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18534
18535 arg = NULL_TREE;
18536 if (TREE_VALUE (pack)
18537 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
18538 && (i - start < TREE_VEC_LENGTH (pargs)))
18539 {
18540 any_explicit = true;
18541 arg = TREE_VEC_ELT (pargs, i - start);
18542 }
18543 TMPL_ARG (targs, level, idx) = arg;
18544 }
18545
18546 /* If we had explicit template arguments, substitute them into the
18547 pattern before deduction. */
18548 if (any_explicit)
18549 {
18550 /* Some arguments might still be unspecified or dependent. */
18551 bool dependent;
18552 ++processing_template_decl;
18553 dependent = any_dependent_template_arguments_p (targs);
18554 if (!dependent)
18555 --processing_template_decl;
18556 parm = tsubst (pattern, targs,
18557 explain_p ? tf_warning_or_error : tf_none,
18558 NULL_TREE);
18559 if (dependent)
18560 --processing_template_decl;
18561 if (parm == error_mark_node)
18562 return 1;
18563 }
18564 else
18565 parm = pattern;
18566
18567 /* Unify the pattern with the current argument. */
18568 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
18569 explain_p))
18570 return 1;
18571
18572 /* For each parameter pack, collect the deduced value. */
18573 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18574 {
18575 int idx, level;
18576 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18577
18578 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
18579 TMPL_ARG (targs, level, idx);
18580 }
18581 }
18582
18583 /* Verify that the results of unification with the parameter packs
18584 produce results consistent with what we've seen before, and make
18585 the deduced argument packs available. */
18586 for (pack = packs; pack; pack = TREE_CHAIN (pack))
18587 {
18588 tree old_pack = TREE_VALUE (pack);
18589 tree new_args = TREE_TYPE (pack);
18590 int i, len = TREE_VEC_LENGTH (new_args);
18591 int idx, level;
18592 bool nondeduced_p = false;
18593
18594 /* By default keep the original deduced argument pack.
18595 If necessary, more specific code is going to update the
18596 resulting deduced argument later down in this function. */
18597 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
18598 TMPL_ARG (targs, level, idx) = old_pack;
18599
18600 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
18601 actually deduce anything. */
18602 for (i = 0; i < len && !nondeduced_p; ++i)
18603 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
18604 nondeduced_p = true;
18605 if (nondeduced_p)
18606 continue;
18607
18608 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
18609 {
18610 /* If we had fewer function args than explicit template args,
18611 just use the explicits. */
18612 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
18613 int explicit_len = TREE_VEC_LENGTH (explicit_args);
18614 if (len < explicit_len)
18615 new_args = explicit_args;
18616 }
18617
18618 if (!old_pack)
18619 {
18620 tree result;
18621 /* Build the deduced *_ARGUMENT_PACK. */
18622 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
18623 {
18624 result = make_node (NONTYPE_ARGUMENT_PACK);
18625 TREE_TYPE (result) =
18626 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
18627 TREE_CONSTANT (result) = 1;
18628 }
18629 else
18630 result = cxx_make_type (TYPE_ARGUMENT_PACK);
18631
18632 SET_ARGUMENT_PACK_ARGS (result, new_args);
18633
18634 /* Note the deduced argument packs for this parameter
18635 pack. */
18636 TMPL_ARG (targs, level, idx) = result;
18637 }
18638 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
18639 && (ARGUMENT_PACK_ARGS (old_pack)
18640 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
18641 {
18642 /* We only had the explicitly-provided arguments before, but
18643 now we have a complete set of arguments. */
18644 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
18645
18646 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
18647 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
18648 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
18649 }
18650 else
18651 {
18652 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
18653 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
18654
18655 if (!comp_template_args_with_info (old_args, new_args,
18656 &bad_old_arg, &bad_new_arg))
18657 /* Inconsistent unification of this parameter pack. */
18658 return unify_parameter_pack_inconsistent (explain_p,
18659 bad_old_arg,
18660 bad_new_arg);
18661 }
18662 }
18663
18664 return unify_success (explain_p);
18665 }
18666
18667 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
18668 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
18669 parameters and return value are as for unify. */
18670
18671 static int
18672 unify_array_domain (tree tparms, tree targs,
18673 tree parm_dom, tree arg_dom,
18674 bool explain_p)
18675 {
18676 tree parm_max;
18677 tree arg_max;
18678 bool parm_cst;
18679 bool arg_cst;
18680
18681 /* Our representation of array types uses "N - 1" as the
18682 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
18683 not an integer constant. We cannot unify arbitrarily
18684 complex expressions, so we eliminate the MINUS_EXPRs
18685 here. */
18686 parm_max = TYPE_MAX_VALUE (parm_dom);
18687 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
18688 if (!parm_cst)
18689 {
18690 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
18691 parm_max = TREE_OPERAND (parm_max, 0);
18692 }
18693 arg_max = TYPE_MAX_VALUE (arg_dom);
18694 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
18695 if (!arg_cst)
18696 {
18697 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
18698 trying to unify the type of a variable with the type
18699 of a template parameter. For example:
18700
18701 template <unsigned int N>
18702 void f (char (&) [N]);
18703 int g();
18704 void h(int i) {
18705 char a[g(i)];
18706 f(a);
18707 }
18708
18709 Here, the type of the ARG will be "int [g(i)]", and
18710 may be a SAVE_EXPR, etc. */
18711 if (TREE_CODE (arg_max) != MINUS_EXPR)
18712 return unify_vla_arg (explain_p, arg_dom);
18713 arg_max = TREE_OPERAND (arg_max, 0);
18714 }
18715
18716 /* If only one of the bounds used a MINUS_EXPR, compensate
18717 by adding one to the other bound. */
18718 if (parm_cst && !arg_cst)
18719 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
18720 integer_type_node,
18721 parm_max,
18722 integer_one_node);
18723 else if (arg_cst && !parm_cst)
18724 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
18725 integer_type_node,
18726 arg_max,
18727 integer_one_node);
18728
18729 return unify (tparms, targs, parm_max, arg_max,
18730 UNIFY_ALLOW_INTEGER, explain_p);
18731 }
18732
18733 /* Deduce the value of template parameters. TPARMS is the (innermost)
18734 set of template parameters to a template. TARGS is the bindings
18735 for those template parameters, as determined thus far; TARGS may
18736 include template arguments for outer levels of template parameters
18737 as well. PARM is a parameter to a template function, or a
18738 subcomponent of that parameter; ARG is the corresponding argument.
18739 This function attempts to match PARM with ARG in a manner
18740 consistent with the existing assignments in TARGS. If more values
18741 are deduced, then TARGS is updated.
18742
18743 Returns 0 if the type deduction succeeds, 1 otherwise. The
18744 parameter STRICT is a bitwise or of the following flags:
18745
18746 UNIFY_ALLOW_NONE:
18747 Require an exact match between PARM and ARG.
18748 UNIFY_ALLOW_MORE_CV_QUAL:
18749 Allow the deduced ARG to be more cv-qualified (by qualification
18750 conversion) than ARG.
18751 UNIFY_ALLOW_LESS_CV_QUAL:
18752 Allow the deduced ARG to be less cv-qualified than ARG.
18753 UNIFY_ALLOW_DERIVED:
18754 Allow the deduced ARG to be a template base class of ARG,
18755 or a pointer to a template base class of the type pointed to by
18756 ARG.
18757 UNIFY_ALLOW_INTEGER:
18758 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
18759 case for more information.
18760 UNIFY_ALLOW_OUTER_LEVEL:
18761 This is the outermost level of a deduction. Used to determine validity
18762 of qualification conversions. A valid qualification conversion must
18763 have const qualified pointers leading up to the inner type which
18764 requires additional CV quals, except at the outer level, where const
18765 is not required [conv.qual]. It would be normal to set this flag in
18766 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
18767 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
18768 This is the outermost level of a deduction, and PARM can be more CV
18769 qualified at this point.
18770 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
18771 This is the outermost level of a deduction, and PARM can be less CV
18772 qualified at this point. */
18773
18774 static int
18775 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
18776 bool explain_p)
18777 {
18778 int idx;
18779 tree targ;
18780 tree tparm;
18781 int strict_in = strict;
18782
18783 /* I don't think this will do the right thing with respect to types.
18784 But the only case I've seen it in so far has been array bounds, where
18785 signedness is the only information lost, and I think that will be
18786 okay. */
18787 while (TREE_CODE (parm) == NOP_EXPR)
18788 parm = TREE_OPERAND (parm, 0);
18789
18790 if (arg == error_mark_node)
18791 return unify_invalid (explain_p);
18792 if (arg == unknown_type_node
18793 || arg == init_list_type_node)
18794 /* We can't deduce anything from this, but we might get all the
18795 template args from other function args. */
18796 return unify_success (explain_p);
18797
18798 /* If PARM uses template parameters, then we can't bail out here,
18799 even if ARG == PARM, since we won't record unifications for the
18800 template parameters. We might need them if we're trying to
18801 figure out which of two things is more specialized. */
18802 if (arg == parm && !uses_template_parms (parm))
18803 return unify_success (explain_p);
18804
18805 /* Handle init lists early, so the rest of the function can assume
18806 we're dealing with a type. */
18807 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
18808 {
18809 tree elt, elttype;
18810 unsigned i;
18811 tree orig_parm = parm;
18812
18813 /* Replace T with std::initializer_list<T> for deduction. */
18814 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18815 && flag_deduce_init_list)
18816 parm = listify (parm);
18817
18818 if (!is_std_init_list (parm)
18819 && TREE_CODE (parm) != ARRAY_TYPE)
18820 /* We can only deduce from an initializer list argument if the
18821 parameter is std::initializer_list or an array; otherwise this
18822 is a non-deduced context. */
18823 return unify_success (explain_p);
18824
18825 if (TREE_CODE (parm) == ARRAY_TYPE)
18826 elttype = TREE_TYPE (parm);
18827 else
18828 {
18829 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
18830 /* Deduction is defined in terms of a single type, so just punt
18831 on the (bizarre) std::initializer_list<T...>. */
18832 if (PACK_EXPANSION_P (elttype))
18833 return unify_success (explain_p);
18834 }
18835
18836 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
18837 {
18838 int elt_strict = strict;
18839
18840 if (elt == error_mark_node)
18841 return unify_invalid (explain_p);
18842
18843 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
18844 {
18845 tree type = TREE_TYPE (elt);
18846 if (type == error_mark_node)
18847 return unify_invalid (explain_p);
18848 /* It should only be possible to get here for a call. */
18849 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
18850 elt_strict |= maybe_adjust_types_for_deduction
18851 (DEDUCE_CALL, &elttype, &type, elt);
18852 elt = type;
18853 }
18854
18855 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
18856 explain_p);
18857 }
18858
18859 if (TREE_CODE (parm) == ARRAY_TYPE
18860 && deducible_array_bound (TYPE_DOMAIN (parm)))
18861 {
18862 /* Also deduce from the length of the initializer list. */
18863 tree max = size_int (CONSTRUCTOR_NELTS (arg));
18864 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
18865 if (idx == error_mark_node)
18866 return unify_invalid (explain_p);
18867 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18868 idx, explain_p);
18869 }
18870
18871 /* If the std::initializer_list<T> deduction worked, replace the
18872 deduced A with std::initializer_list<A>. */
18873 if (orig_parm != parm)
18874 {
18875 idx = TEMPLATE_TYPE_IDX (orig_parm);
18876 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18877 targ = listify (targ);
18878 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
18879 }
18880 return unify_success (explain_p);
18881 }
18882
18883 /* Immediately reject some pairs that won't unify because of
18884 cv-qualification mismatches. */
18885 if (TREE_CODE (arg) == TREE_CODE (parm)
18886 && TYPE_P (arg)
18887 /* It is the elements of the array which hold the cv quals of an array
18888 type, and the elements might be template type parms. We'll check
18889 when we recurse. */
18890 && TREE_CODE (arg) != ARRAY_TYPE
18891 /* We check the cv-qualifiers when unifying with template type
18892 parameters below. We want to allow ARG `const T' to unify with
18893 PARM `T' for example, when computing which of two templates
18894 is more specialized, for example. */
18895 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
18896 && !check_cv_quals_for_unify (strict_in, arg, parm))
18897 return unify_cv_qual_mismatch (explain_p, parm, arg);
18898
18899 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
18900 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
18901 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
18902 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
18903 strict &= ~UNIFY_ALLOW_DERIVED;
18904 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18905 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
18906
18907 switch (TREE_CODE (parm))
18908 {
18909 case TYPENAME_TYPE:
18910 case SCOPE_REF:
18911 case UNBOUND_CLASS_TEMPLATE:
18912 /* In a type which contains a nested-name-specifier, template
18913 argument values cannot be deduced for template parameters used
18914 within the nested-name-specifier. */
18915 return unify_success (explain_p);
18916
18917 case TEMPLATE_TYPE_PARM:
18918 case TEMPLATE_TEMPLATE_PARM:
18919 case BOUND_TEMPLATE_TEMPLATE_PARM:
18920 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18921 if (error_operand_p (tparm))
18922 return unify_invalid (explain_p);
18923
18924 if (TEMPLATE_TYPE_LEVEL (parm)
18925 != template_decl_level (tparm))
18926 /* The PARM is not one we're trying to unify. Just check
18927 to see if it matches ARG. */
18928 {
18929 if (TREE_CODE (arg) == TREE_CODE (parm)
18930 && (is_auto (parm) ? is_auto (arg)
18931 : same_type_p (parm, arg)))
18932 return unify_success (explain_p);
18933 else
18934 return unify_type_mismatch (explain_p, parm, arg);
18935 }
18936 idx = TEMPLATE_TYPE_IDX (parm);
18937 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18938 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
18939 if (error_operand_p (tparm))
18940 return unify_invalid (explain_p);
18941
18942 /* Check for mixed types and values. */
18943 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18944 && TREE_CODE (tparm) != TYPE_DECL)
18945 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18946 && TREE_CODE (tparm) != TEMPLATE_DECL))
18947 gcc_unreachable ();
18948
18949 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18950 {
18951 /* ARG must be constructed from a template class or a template
18952 template parameter. */
18953 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
18954 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
18955 return unify_template_deduction_failure (explain_p, parm, arg);
18956 {
18957 tree parmvec = TYPE_TI_ARGS (parm);
18958 /* An alias template name is never deduced. */
18959 if (TYPE_ALIAS_P (arg))
18960 arg = strip_typedefs (arg);
18961 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
18962 tree full_argvec = add_to_template_args (targs, argvec);
18963 tree parm_parms
18964 = DECL_INNERMOST_TEMPLATE_PARMS
18965 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
18966 int i, len;
18967 int parm_variadic_p = 0;
18968
18969 /* The resolution to DR150 makes clear that default
18970 arguments for an N-argument may not be used to bind T
18971 to a template template parameter with fewer than N
18972 parameters. It is not safe to permit the binding of
18973 default arguments as an extension, as that may change
18974 the meaning of a conforming program. Consider:
18975
18976 struct Dense { static const unsigned int dim = 1; };
18977
18978 template <template <typename> class View,
18979 typename Block>
18980 void operator+(float, View<Block> const&);
18981
18982 template <typename Block,
18983 unsigned int Dim = Block::dim>
18984 struct Lvalue_proxy { operator float() const; };
18985
18986 void
18987 test_1d (void) {
18988 Lvalue_proxy<Dense> p;
18989 float b;
18990 b + p;
18991 }
18992
18993 Here, if Lvalue_proxy is permitted to bind to View, then
18994 the global operator+ will be used; if they are not, the
18995 Lvalue_proxy will be converted to float. */
18996 if (coerce_template_parms (parm_parms,
18997 full_argvec,
18998 TYPE_TI_TEMPLATE (parm),
18999 (explain_p
19000 ? tf_warning_or_error
19001 : tf_none),
19002 /*require_all_args=*/true,
19003 /*use_default_args=*/false)
19004 == error_mark_node)
19005 return 1;
19006
19007 /* Deduce arguments T, i from TT<T> or TT<i>.
19008 We check each element of PARMVEC and ARGVEC individually
19009 rather than the whole TREE_VEC since they can have
19010 different number of elements. */
19011
19012 parmvec = expand_template_argument_pack (parmvec);
19013 argvec = expand_template_argument_pack (argvec);
19014
19015 len = TREE_VEC_LENGTH (parmvec);
19016
19017 /* Check if the parameters end in a pack, making them
19018 variadic. */
19019 if (len > 0
19020 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
19021 parm_variadic_p = 1;
19022
19023 for (i = 0; i < len - parm_variadic_p; ++i)
19024 /* If the template argument list of P contains a pack
19025 expansion that is not the last template argument, the
19026 entire template argument list is a non-deduced
19027 context. */
19028 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
19029 return unify_success (explain_p);
19030
19031 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
19032 return unify_too_few_arguments (explain_p,
19033 TREE_VEC_LENGTH (argvec), len);
19034
19035 for (i = 0; i < len - parm_variadic_p; ++i)
19036 {
19037 RECUR_AND_CHECK_FAILURE (tparms, targs,
19038 TREE_VEC_ELT (parmvec, i),
19039 TREE_VEC_ELT (argvec, i),
19040 UNIFY_ALLOW_NONE, explain_p);
19041 }
19042
19043 if (parm_variadic_p
19044 && unify_pack_expansion (tparms, targs,
19045 parmvec, argvec,
19046 DEDUCE_EXACT,
19047 /*subr=*/true, explain_p))
19048 return 1;
19049 }
19050 arg = TYPE_TI_TEMPLATE (arg);
19051
19052 /* Fall through to deduce template name. */
19053 }
19054
19055 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19056 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19057 {
19058 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19059
19060 /* Simple cases: Value already set, does match or doesn't. */
19061 if (targ != NULL_TREE && template_args_equal (targ, arg))
19062 return unify_success (explain_p);
19063 else if (targ)
19064 return unify_inconsistency (explain_p, parm, targ, arg);
19065 }
19066 else
19067 {
19068 /* If PARM is `const T' and ARG is only `int', we don't have
19069 a match unless we are allowing additional qualification.
19070 If ARG is `const int' and PARM is just `T' that's OK;
19071 that binds `const int' to `T'. */
19072 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
19073 arg, parm))
19074 return unify_cv_qual_mismatch (explain_p, parm, arg);
19075
19076 /* Consider the case where ARG is `const volatile int' and
19077 PARM is `const T'. Then, T should be `volatile int'. */
19078 arg = cp_build_qualified_type_real
19079 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
19080 if (arg == error_mark_node)
19081 return unify_invalid (explain_p);
19082
19083 /* Simple cases: Value already set, does match or doesn't. */
19084 if (targ != NULL_TREE && same_type_p (targ, arg))
19085 return unify_success (explain_p);
19086 else if (targ)
19087 return unify_inconsistency (explain_p, parm, targ, arg);
19088
19089 /* Make sure that ARG is not a variable-sized array. (Note
19090 that were talking about variable-sized arrays (like
19091 `int[n]'), rather than arrays of unknown size (like
19092 `int[]').) We'll get very confused by such a type since
19093 the bound of the array is not constant, and therefore
19094 not mangleable. Besides, such types are not allowed in
19095 ISO C++, so we can do as we please here. We do allow
19096 them for 'auto' deduction, since that isn't ABI-exposed. */
19097 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
19098 return unify_vla_arg (explain_p, arg);
19099
19100 /* Strip typedefs as in convert_template_argument. */
19101 arg = canonicalize_type_argument (arg, tf_none);
19102 }
19103
19104 /* If ARG is a parameter pack or an expansion, we cannot unify
19105 against it unless PARM is also a parameter pack. */
19106 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19107 && !template_parameter_pack_p (parm))
19108 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19109
19110 /* If the argument deduction results is a METHOD_TYPE,
19111 then there is a problem.
19112 METHOD_TYPE doesn't map to any real C++ type the result of
19113 the deduction can not be of that type. */
19114 if (TREE_CODE (arg) == METHOD_TYPE)
19115 return unify_method_type_error (explain_p, arg);
19116
19117 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19118 return unify_success (explain_p);
19119
19120 case TEMPLATE_PARM_INDEX:
19121 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19122 if (error_operand_p (tparm))
19123 return unify_invalid (explain_p);
19124
19125 if (TEMPLATE_PARM_LEVEL (parm)
19126 != template_decl_level (tparm))
19127 {
19128 /* The PARM is not one we're trying to unify. Just check
19129 to see if it matches ARG. */
19130 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
19131 && cp_tree_equal (parm, arg));
19132 if (result)
19133 unify_expression_unequal (explain_p, parm, arg);
19134 return result;
19135 }
19136
19137 idx = TEMPLATE_PARM_IDX (parm);
19138 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19139
19140 if (targ)
19141 {
19142 int x = !cp_tree_equal (targ, arg);
19143 if (x)
19144 unify_inconsistency (explain_p, parm, targ, arg);
19145 return x;
19146 }
19147
19148 /* [temp.deduct.type] If, in the declaration of a function template
19149 with a non-type template-parameter, the non-type
19150 template-parameter is used in an expression in the function
19151 parameter-list and, if the corresponding template-argument is
19152 deduced, the template-argument type shall match the type of the
19153 template-parameter exactly, except that a template-argument
19154 deduced from an array bound may be of any integral type.
19155 The non-type parameter might use already deduced type parameters. */
19156 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
19157 if (!TREE_TYPE (arg))
19158 /* Template-parameter dependent expression. Just accept it for now.
19159 It will later be processed in convert_template_argument. */
19160 ;
19161 else if (same_type_p (TREE_TYPE (arg), tparm))
19162 /* OK */;
19163 else if ((strict & UNIFY_ALLOW_INTEGER)
19164 && CP_INTEGRAL_TYPE_P (tparm))
19165 /* Convert the ARG to the type of PARM; the deduced non-type
19166 template argument must exactly match the types of the
19167 corresponding parameter. */
19168 arg = fold (build_nop (tparm, arg));
19169 else if (uses_template_parms (tparm))
19170 /* We haven't deduced the type of this parameter yet. Try again
19171 later. */
19172 return unify_success (explain_p);
19173 else
19174 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
19175
19176 /* If ARG is a parameter pack or an expansion, we cannot unify
19177 against it unless PARM is also a parameter pack. */
19178 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
19179 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
19180 return unify_parameter_pack_mismatch (explain_p, parm, arg);
19181
19182 {
19183 bool removed_attr = false;
19184 arg = strip_typedefs_expr (arg, &removed_attr);
19185 }
19186 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
19187 return unify_success (explain_p);
19188
19189 case PTRMEM_CST:
19190 {
19191 /* A pointer-to-member constant can be unified only with
19192 another constant. */
19193 if (TREE_CODE (arg) != PTRMEM_CST)
19194 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
19195
19196 /* Just unify the class member. It would be useless (and possibly
19197 wrong, depending on the strict flags) to unify also
19198 PTRMEM_CST_CLASS, because we want to be sure that both parm and
19199 arg refer to the same variable, even if through different
19200 classes. For instance:
19201
19202 struct A { int x; };
19203 struct B : A { };
19204
19205 Unification of &A::x and &B::x must succeed. */
19206 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
19207 PTRMEM_CST_MEMBER (arg), strict, explain_p);
19208 }
19209
19210 case POINTER_TYPE:
19211 {
19212 if (!TYPE_PTR_P (arg))
19213 return unify_type_mismatch (explain_p, parm, arg);
19214
19215 /* [temp.deduct.call]
19216
19217 A can be another pointer or pointer to member type that can
19218 be converted to the deduced A via a qualification
19219 conversion (_conv.qual_).
19220
19221 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
19222 This will allow for additional cv-qualification of the
19223 pointed-to types if appropriate. */
19224
19225 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
19226 /* The derived-to-base conversion only persists through one
19227 level of pointers. */
19228 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
19229
19230 return unify (tparms, targs, TREE_TYPE (parm),
19231 TREE_TYPE (arg), strict, explain_p);
19232 }
19233
19234 case REFERENCE_TYPE:
19235 if (TREE_CODE (arg) != REFERENCE_TYPE)
19236 return unify_type_mismatch (explain_p, parm, arg);
19237 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19238 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19239
19240 case ARRAY_TYPE:
19241 if (TREE_CODE (arg) != ARRAY_TYPE)
19242 return unify_type_mismatch (explain_p, parm, arg);
19243 if ((TYPE_DOMAIN (parm) == NULL_TREE)
19244 != (TYPE_DOMAIN (arg) == NULL_TREE))
19245 return unify_type_mismatch (explain_p, parm, arg);
19246 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19247 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
19248 if (TYPE_DOMAIN (parm) != NULL_TREE)
19249 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19250 TYPE_DOMAIN (arg), explain_p);
19251 return unify_success (explain_p);
19252
19253 case REAL_TYPE:
19254 case COMPLEX_TYPE:
19255 case VECTOR_TYPE:
19256 case INTEGER_TYPE:
19257 case BOOLEAN_TYPE:
19258 case ENUMERAL_TYPE:
19259 case VOID_TYPE:
19260 case NULLPTR_TYPE:
19261 if (TREE_CODE (arg) != TREE_CODE (parm))
19262 return unify_type_mismatch (explain_p, parm, arg);
19263
19264 /* We have already checked cv-qualification at the top of the
19265 function. */
19266 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
19267 return unify_type_mismatch (explain_p, parm, arg);
19268
19269 /* As far as unification is concerned, this wins. Later checks
19270 will invalidate it if necessary. */
19271 return unify_success (explain_p);
19272
19273 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
19274 /* Type INTEGER_CST can come from ordinary constant template args. */
19275 case INTEGER_CST:
19276 while (TREE_CODE (arg) == NOP_EXPR)
19277 arg = TREE_OPERAND (arg, 0);
19278
19279 if (TREE_CODE (arg) != INTEGER_CST)
19280 return unify_template_argument_mismatch (explain_p, parm, arg);
19281 return (tree_int_cst_equal (parm, arg)
19282 ? unify_success (explain_p)
19283 : unify_template_argument_mismatch (explain_p, parm, arg));
19284
19285 case TREE_VEC:
19286 {
19287 int i, len, argslen;
19288 int parm_variadic_p = 0;
19289
19290 if (TREE_CODE (arg) != TREE_VEC)
19291 return unify_template_argument_mismatch (explain_p, parm, arg);
19292
19293 len = TREE_VEC_LENGTH (parm);
19294 argslen = TREE_VEC_LENGTH (arg);
19295
19296 /* Check for pack expansions in the parameters. */
19297 for (i = 0; i < len; ++i)
19298 {
19299 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
19300 {
19301 if (i == len - 1)
19302 /* We can unify against something with a trailing
19303 parameter pack. */
19304 parm_variadic_p = 1;
19305 else
19306 /* [temp.deduct.type]/9: If the template argument list of
19307 P contains a pack expansion that is not the last
19308 template argument, the entire template argument list
19309 is a non-deduced context. */
19310 return unify_success (explain_p);
19311 }
19312 }
19313
19314 /* If we don't have enough arguments to satisfy the parameters
19315 (not counting the pack expression at the end), or we have
19316 too many arguments for a parameter list that doesn't end in
19317 a pack expression, we can't unify. */
19318 if (parm_variadic_p
19319 ? argslen < len - parm_variadic_p
19320 : argslen != len)
19321 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
19322
19323 /* Unify all of the parameters that precede the (optional)
19324 pack expression. */
19325 for (i = 0; i < len - parm_variadic_p; ++i)
19326 {
19327 RECUR_AND_CHECK_FAILURE (tparms, targs,
19328 TREE_VEC_ELT (parm, i),
19329 TREE_VEC_ELT (arg, i),
19330 UNIFY_ALLOW_NONE, explain_p);
19331 }
19332 if (parm_variadic_p)
19333 return unify_pack_expansion (tparms, targs, parm, arg,
19334 DEDUCE_EXACT,
19335 /*subr=*/true, explain_p);
19336 return unify_success (explain_p);
19337 }
19338
19339 case RECORD_TYPE:
19340 case UNION_TYPE:
19341 if (TREE_CODE (arg) != TREE_CODE (parm))
19342 return unify_type_mismatch (explain_p, parm, arg);
19343
19344 if (TYPE_PTRMEMFUNC_P (parm))
19345 {
19346 if (!TYPE_PTRMEMFUNC_P (arg))
19347 return unify_type_mismatch (explain_p, parm, arg);
19348
19349 return unify (tparms, targs,
19350 TYPE_PTRMEMFUNC_FN_TYPE (parm),
19351 TYPE_PTRMEMFUNC_FN_TYPE (arg),
19352 strict, explain_p);
19353 }
19354 else if (TYPE_PTRMEMFUNC_P (arg))
19355 return unify_type_mismatch (explain_p, parm, arg);
19356
19357 if (CLASSTYPE_TEMPLATE_INFO (parm))
19358 {
19359 tree t = NULL_TREE;
19360
19361 if (strict_in & UNIFY_ALLOW_DERIVED)
19362 {
19363 /* First, we try to unify the PARM and ARG directly. */
19364 t = try_class_unification (tparms, targs,
19365 parm, arg, explain_p);
19366
19367 if (!t)
19368 {
19369 /* Fallback to the special case allowed in
19370 [temp.deduct.call]:
19371
19372 If P is a class, and P has the form
19373 template-id, then A can be a derived class of
19374 the deduced A. Likewise, if P is a pointer to
19375 a class of the form template-id, A can be a
19376 pointer to a derived class pointed to by the
19377 deduced A. */
19378 enum template_base_result r;
19379 r = get_template_base (tparms, targs, parm, arg,
19380 explain_p, &t);
19381
19382 if (!t)
19383 return unify_no_common_base (explain_p, r, parm, arg);
19384 }
19385 }
19386 else if (CLASSTYPE_TEMPLATE_INFO (arg)
19387 && (CLASSTYPE_TI_TEMPLATE (parm)
19388 == CLASSTYPE_TI_TEMPLATE (arg)))
19389 /* Perhaps PARM is something like S<U> and ARG is S<int>.
19390 Then, we should unify `int' and `U'. */
19391 t = arg;
19392 else
19393 /* There's no chance of unification succeeding. */
19394 return unify_type_mismatch (explain_p, parm, arg);
19395
19396 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
19397 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
19398 }
19399 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
19400 return unify_type_mismatch (explain_p, parm, arg);
19401 return unify_success (explain_p);
19402
19403 case METHOD_TYPE:
19404 case FUNCTION_TYPE:
19405 {
19406 unsigned int nargs;
19407 tree *args;
19408 tree a;
19409 unsigned int i;
19410
19411 if (TREE_CODE (arg) != TREE_CODE (parm))
19412 return unify_type_mismatch (explain_p, parm, arg);
19413
19414 /* CV qualifications for methods can never be deduced, they must
19415 match exactly. We need to check them explicitly here,
19416 because type_unification_real treats them as any other
19417 cv-qualified parameter. */
19418 if (TREE_CODE (parm) == METHOD_TYPE
19419 && (!check_cv_quals_for_unify
19420 (UNIFY_ALLOW_NONE,
19421 class_of_this_parm (arg),
19422 class_of_this_parm (parm))))
19423 return unify_cv_qual_mismatch (explain_p, parm, arg);
19424
19425 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
19426 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
19427
19428 nargs = list_length (TYPE_ARG_TYPES (arg));
19429 args = XALLOCAVEC (tree, nargs);
19430 for (a = TYPE_ARG_TYPES (arg), i = 0;
19431 a != NULL_TREE && a != void_list_node;
19432 a = TREE_CHAIN (a), ++i)
19433 args[i] = TREE_VALUE (a);
19434 nargs = i;
19435
19436 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
19437 args, nargs, 1, DEDUCE_EXACT,
19438 LOOKUP_NORMAL, NULL, explain_p);
19439 }
19440
19441 case OFFSET_TYPE:
19442 /* Unify a pointer to member with a pointer to member function, which
19443 deduces the type of the member as a function type. */
19444 if (TYPE_PTRMEMFUNC_P (arg))
19445 {
19446 /* Check top-level cv qualifiers */
19447 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
19448 return unify_cv_qual_mismatch (explain_p, parm, arg);
19449
19450 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19451 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
19452 UNIFY_ALLOW_NONE, explain_p);
19453
19454 /* Determine the type of the function we are unifying against. */
19455 tree fntype = static_fn_type (arg);
19456
19457 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
19458 }
19459
19460 if (TREE_CODE (arg) != OFFSET_TYPE)
19461 return unify_type_mismatch (explain_p, parm, arg);
19462 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
19463 TYPE_OFFSET_BASETYPE (arg),
19464 UNIFY_ALLOW_NONE, explain_p);
19465 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
19466 strict, explain_p);
19467
19468 case CONST_DECL:
19469 if (DECL_TEMPLATE_PARM_P (parm))
19470 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
19471 if (arg != scalar_constant_value (parm))
19472 return unify_template_argument_mismatch (explain_p, parm, arg);
19473 return unify_success (explain_p);
19474
19475 case FIELD_DECL:
19476 case TEMPLATE_DECL:
19477 /* Matched cases are handled by the ARG == PARM test above. */
19478 return unify_template_argument_mismatch (explain_p, parm, arg);
19479
19480 case VAR_DECL:
19481 /* A non-type template parameter that is a variable should be a
19482 an integral constant, in which case, it whould have been
19483 folded into its (constant) value. So we should not be getting
19484 a variable here. */
19485 gcc_unreachable ();
19486
19487 case TYPE_ARGUMENT_PACK:
19488 case NONTYPE_ARGUMENT_PACK:
19489 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
19490 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
19491
19492 case TYPEOF_TYPE:
19493 case DECLTYPE_TYPE:
19494 case UNDERLYING_TYPE:
19495 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
19496 or UNDERLYING_TYPE nodes. */
19497 return unify_success (explain_p);
19498
19499 case ERROR_MARK:
19500 /* Unification fails if we hit an error node. */
19501 return unify_invalid (explain_p);
19502
19503 case INDIRECT_REF:
19504 if (REFERENCE_REF_P (parm))
19505 {
19506 if (REFERENCE_REF_P (arg))
19507 arg = TREE_OPERAND (arg, 0);
19508 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
19509 strict, explain_p);
19510 }
19511 /* FALLTHRU */
19512
19513 default:
19514 /* An unresolved overload is a nondeduced context. */
19515 if (is_overloaded_fn (parm) || type_unknown_p (parm))
19516 return unify_success (explain_p);
19517 gcc_assert (EXPR_P (parm));
19518
19519 /* We must be looking at an expression. This can happen with
19520 something like:
19521
19522 template <int I>
19523 void foo(S<I>, S<I + 2>);
19524
19525 This is a "nondeduced context":
19526
19527 [deduct.type]
19528
19529 The nondeduced contexts are:
19530
19531 --A type that is a template-id in which one or more of
19532 the template-arguments is an expression that references
19533 a template-parameter.
19534
19535 In these cases, we assume deduction succeeded, but don't
19536 actually infer any unifications. */
19537
19538 if (!uses_template_parms (parm)
19539 && !template_args_equal (parm, arg))
19540 return unify_expression_unequal (explain_p, parm, arg);
19541 else
19542 return unify_success (explain_p);
19543 }
19544 }
19545 #undef RECUR_AND_CHECK_FAILURE
19546 \f
19547 /* Note that DECL can be defined in this translation unit, if
19548 required. */
19549
19550 static void
19551 mark_definable (tree decl)
19552 {
19553 tree clone;
19554 DECL_NOT_REALLY_EXTERN (decl) = 1;
19555 FOR_EACH_CLONE (clone, decl)
19556 DECL_NOT_REALLY_EXTERN (clone) = 1;
19557 }
19558
19559 /* Called if RESULT is explicitly instantiated, or is a member of an
19560 explicitly instantiated class. */
19561
19562 void
19563 mark_decl_instantiated (tree result, int extern_p)
19564 {
19565 SET_DECL_EXPLICIT_INSTANTIATION (result);
19566
19567 /* If this entity has already been written out, it's too late to
19568 make any modifications. */
19569 if (TREE_ASM_WRITTEN (result))
19570 return;
19571
19572 /* For anonymous namespace we don't need to do anything. */
19573 if (decl_anon_ns_mem_p (result))
19574 {
19575 gcc_assert (!TREE_PUBLIC (result));
19576 return;
19577 }
19578
19579 if (TREE_CODE (result) != FUNCTION_DECL)
19580 /* The TREE_PUBLIC flag for function declarations will have been
19581 set correctly by tsubst. */
19582 TREE_PUBLIC (result) = 1;
19583
19584 /* This might have been set by an earlier implicit instantiation. */
19585 DECL_COMDAT (result) = 0;
19586
19587 if (extern_p)
19588 DECL_NOT_REALLY_EXTERN (result) = 0;
19589 else
19590 {
19591 mark_definable (result);
19592 mark_needed (result);
19593 /* Always make artificials weak. */
19594 if (DECL_ARTIFICIAL (result) && flag_weak)
19595 comdat_linkage (result);
19596 /* For WIN32 we also want to put explicit instantiations in
19597 linkonce sections. */
19598 else if (TREE_PUBLIC (result))
19599 maybe_make_one_only (result);
19600 }
19601
19602 /* If EXTERN_P, then this function will not be emitted -- unless
19603 followed by an explicit instantiation, at which point its linkage
19604 will be adjusted. If !EXTERN_P, then this function will be
19605 emitted here. In neither circumstance do we want
19606 import_export_decl to adjust the linkage. */
19607 DECL_INTERFACE_KNOWN (result) = 1;
19608 }
19609
19610 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
19611 important template arguments. If any are missing, we check whether
19612 they're important by using error_mark_node for substituting into any
19613 args that were used for partial ordering (the ones between ARGS and END)
19614 and seeing if it bubbles up. */
19615
19616 static bool
19617 check_undeduced_parms (tree targs, tree args, tree end)
19618 {
19619 bool found = false;
19620 int i;
19621 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
19622 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
19623 {
19624 found = true;
19625 TREE_VEC_ELT (targs, i) = error_mark_node;
19626 }
19627 if (found)
19628 {
19629 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
19630 if (substed == error_mark_node)
19631 return true;
19632 }
19633 return false;
19634 }
19635
19636 /* Given two function templates PAT1 and PAT2, return:
19637
19638 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
19639 -1 if PAT2 is more specialized than PAT1.
19640 0 if neither is more specialized.
19641
19642 LEN indicates the number of parameters we should consider
19643 (defaulted parameters should not be considered).
19644
19645 The 1998 std underspecified function template partial ordering, and
19646 DR214 addresses the issue. We take pairs of arguments, one from
19647 each of the templates, and deduce them against each other. One of
19648 the templates will be more specialized if all the *other*
19649 template's arguments deduce against its arguments and at least one
19650 of its arguments *does* *not* deduce against the other template's
19651 corresponding argument. Deduction is done as for class templates.
19652 The arguments used in deduction have reference and top level cv
19653 qualifiers removed. Iff both arguments were originally reference
19654 types *and* deduction succeeds in both directions, an lvalue reference
19655 wins against an rvalue reference and otherwise the template
19656 with the more cv-qualified argument wins for that pairing (if
19657 neither is more cv-qualified, they both are equal). Unlike regular
19658 deduction, after all the arguments have been deduced in this way,
19659 we do *not* verify the deduced template argument values can be
19660 substituted into non-deduced contexts.
19661
19662 The logic can be a bit confusing here, because we look at deduce1 and
19663 targs1 to see if pat2 is at least as specialized, and vice versa; if we
19664 can find template arguments for pat1 to make arg1 look like arg2, that
19665 means that arg2 is at least as specialized as arg1. */
19666
19667 int
19668 more_specialized_fn (tree pat1, tree pat2, int len)
19669 {
19670 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
19671 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
19672 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
19673 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
19674 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
19675 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
19676 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
19677 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
19678 tree origs1, origs2;
19679 bool lose1 = false;
19680 bool lose2 = false;
19681
19682 /* Remove the this parameter from non-static member functions. If
19683 one is a non-static member function and the other is not a static
19684 member function, remove the first parameter from that function
19685 also. This situation occurs for operator functions where we
19686 locate both a member function (with this pointer) and non-member
19687 operator (with explicit first operand). */
19688 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
19689 {
19690 len--; /* LEN is the number of significant arguments for DECL1 */
19691 args1 = TREE_CHAIN (args1);
19692 if (!DECL_STATIC_FUNCTION_P (decl2))
19693 args2 = TREE_CHAIN (args2);
19694 }
19695 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
19696 {
19697 args2 = TREE_CHAIN (args2);
19698 if (!DECL_STATIC_FUNCTION_P (decl1))
19699 {
19700 len--;
19701 args1 = TREE_CHAIN (args1);
19702 }
19703 }
19704
19705 /* If only one is a conversion operator, they are unordered. */
19706 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
19707 return 0;
19708
19709 /* Consider the return type for a conversion function */
19710 if (DECL_CONV_FN_P (decl1))
19711 {
19712 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
19713 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
19714 len++;
19715 }
19716
19717 processing_template_decl++;
19718
19719 origs1 = args1;
19720 origs2 = args2;
19721
19722 while (len--
19723 /* Stop when an ellipsis is seen. */
19724 && args1 != NULL_TREE && args2 != NULL_TREE)
19725 {
19726 tree arg1 = TREE_VALUE (args1);
19727 tree arg2 = TREE_VALUE (args2);
19728 int deduce1, deduce2;
19729 int quals1 = -1;
19730 int quals2 = -1;
19731 int ref1 = 0;
19732 int ref2 = 0;
19733
19734 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
19735 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19736 {
19737 /* When both arguments are pack expansions, we need only
19738 unify the patterns themselves. */
19739 arg1 = PACK_EXPANSION_PATTERN (arg1);
19740 arg2 = PACK_EXPANSION_PATTERN (arg2);
19741
19742 /* This is the last comparison we need to do. */
19743 len = 0;
19744 }
19745
19746 if (TREE_CODE (arg1) == REFERENCE_TYPE)
19747 {
19748 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
19749 arg1 = TREE_TYPE (arg1);
19750 quals1 = cp_type_quals (arg1);
19751 }
19752
19753 if (TREE_CODE (arg2) == REFERENCE_TYPE)
19754 {
19755 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
19756 arg2 = TREE_TYPE (arg2);
19757 quals2 = cp_type_quals (arg2);
19758 }
19759
19760 arg1 = TYPE_MAIN_VARIANT (arg1);
19761 arg2 = TYPE_MAIN_VARIANT (arg2);
19762
19763 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
19764 {
19765 int i, len2 = list_length (args2);
19766 tree parmvec = make_tree_vec (1);
19767 tree argvec = make_tree_vec (len2);
19768 tree ta = args2;
19769
19770 /* Setup the parameter vector, which contains only ARG1. */
19771 TREE_VEC_ELT (parmvec, 0) = arg1;
19772
19773 /* Setup the argument vector, which contains the remaining
19774 arguments. */
19775 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
19776 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
19777
19778 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
19779 argvec, DEDUCE_EXACT,
19780 /*subr=*/true, /*explain_p=*/false)
19781 == 0);
19782
19783 /* We cannot deduce in the other direction, because ARG1 is
19784 a pack expansion but ARG2 is not. */
19785 deduce2 = 0;
19786 }
19787 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19788 {
19789 int i, len1 = list_length (args1);
19790 tree parmvec = make_tree_vec (1);
19791 tree argvec = make_tree_vec (len1);
19792 tree ta = args1;
19793
19794 /* Setup the parameter vector, which contains only ARG1. */
19795 TREE_VEC_ELT (parmvec, 0) = arg2;
19796
19797 /* Setup the argument vector, which contains the remaining
19798 arguments. */
19799 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
19800 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
19801
19802 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
19803 argvec, DEDUCE_EXACT,
19804 /*subr=*/true, /*explain_p=*/false)
19805 == 0);
19806
19807 /* We cannot deduce in the other direction, because ARG2 is
19808 a pack expansion but ARG1 is not.*/
19809 deduce1 = 0;
19810 }
19811
19812 else
19813 {
19814 /* The normal case, where neither argument is a pack
19815 expansion. */
19816 deduce1 = (unify (tparms1, targs1, arg1, arg2,
19817 UNIFY_ALLOW_NONE, /*explain_p=*/false)
19818 == 0);
19819 deduce2 = (unify (tparms2, targs2, arg2, arg1,
19820 UNIFY_ALLOW_NONE, /*explain_p=*/false)
19821 == 0);
19822 }
19823
19824 /* If we couldn't deduce arguments for tparms1 to make arg1 match
19825 arg2, then arg2 is not as specialized as arg1. */
19826 if (!deduce1)
19827 lose2 = true;
19828 if (!deduce2)
19829 lose1 = true;
19830
19831 /* "If, for a given type, deduction succeeds in both directions
19832 (i.e., the types are identical after the transformations above)
19833 and both P and A were reference types (before being replaced with
19834 the type referred to above):
19835 - if the type from the argument template was an lvalue reference and
19836 the type from the parameter template was not, the argument type is
19837 considered to be more specialized than the other; otherwise,
19838 - if the type from the argument template is more cv-qualified
19839 than the type from the parameter template (as described above),
19840 the argument type is considered to be more specialized than the other;
19841 otherwise,
19842 - neither type is more specialized than the other." */
19843
19844 if (deduce1 && deduce2)
19845 {
19846 if (ref1 && ref2 && ref1 != ref2)
19847 {
19848 if (ref1 > ref2)
19849 lose1 = true;
19850 else
19851 lose2 = true;
19852 }
19853 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
19854 {
19855 if ((quals1 & quals2) == quals2)
19856 lose2 = true;
19857 if ((quals1 & quals2) == quals1)
19858 lose1 = true;
19859 }
19860 }
19861
19862 if (lose1 && lose2)
19863 /* We've failed to deduce something in either direction.
19864 These must be unordered. */
19865 break;
19866
19867 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
19868 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19869 /* We have already processed all of the arguments in our
19870 handing of the pack expansion type. */
19871 len = 0;
19872
19873 args1 = TREE_CHAIN (args1);
19874 args2 = TREE_CHAIN (args2);
19875 }
19876
19877 /* "In most cases, all template parameters must have values in order for
19878 deduction to succeed, but for partial ordering purposes a template
19879 parameter may remain without a value provided it is not used in the
19880 types being used for partial ordering."
19881
19882 Thus, if we are missing any of the targs1 we need to substitute into
19883 origs1, then pat2 is not as specialized as pat1. This can happen when
19884 there is a nondeduced context. */
19885 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
19886 lose2 = true;
19887 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
19888 lose1 = true;
19889
19890 processing_template_decl--;
19891
19892 /* If both deductions succeed, the partial ordering selects the more
19893 constrained template. */
19894 if (!lose1 && !lose2)
19895 {
19896 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
19897 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
19898 lose1 = !subsumes_constraints (c1, c2);
19899 lose2 = !subsumes_constraints (c2, c1);
19900 }
19901
19902 /* All things being equal, if the next argument is a pack expansion
19903 for one function but not for the other, prefer the
19904 non-variadic function. FIXME this is bogus; see c++/41958. */
19905 if (lose1 == lose2
19906 && args1 && TREE_VALUE (args1)
19907 && args2 && TREE_VALUE (args2))
19908 {
19909 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
19910 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
19911 }
19912
19913 if (lose1 == lose2)
19914 return 0;
19915 else if (!lose1)
19916 return 1;
19917 else
19918 return -1;
19919 }
19920
19921 /* Determine which of two partial specializations of TMPL is more
19922 specialized.
19923
19924 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19925 to the first partial specialization. The TREE_PURPOSE is the
19926 innermost set of template parameters for the partial
19927 specialization. PAT2 is similar, but for the second template.
19928
19929 Return 1 if the first partial specialization is more specialized;
19930 -1 if the second is more specialized; 0 if neither is more
19931 specialized.
19932
19933 See [temp.class.order] for information about determining which of
19934 two templates is more specialized. */
19935
19936 static int
19937 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
19938 {
19939 tree targs;
19940 int winner = 0;
19941 bool any_deductions = false;
19942
19943 tree tmpl1 = TREE_VALUE (pat1);
19944 tree tmpl2 = TREE_VALUE (pat2);
19945 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
19946 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
19947 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
19948 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
19949
19950 /* Just like what happens for functions, if we are ordering between
19951 different template specializations, we may encounter dependent
19952 types in the arguments, and we need our dependency check functions
19953 to behave correctly. */
19954 ++processing_template_decl;
19955 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
19956 if (targs)
19957 {
19958 --winner;
19959 any_deductions = true;
19960 }
19961
19962 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
19963 if (targs)
19964 {
19965 ++winner;
19966 any_deductions = true;
19967 }
19968 --processing_template_decl;
19969
19970 /* If both deductions succeed, the partial ordering selects the more
19971 constrained template. */
19972 if (!winner && any_deductions)
19973 return more_constrained (tmpl1, tmpl2);
19974
19975 /* In the case of a tie where at least one of the templates
19976 has a parameter pack at the end, the template with the most
19977 non-packed parameters wins. */
19978 if (winner == 0
19979 && any_deductions
19980 && (template_args_variadic_p (TREE_PURPOSE (pat1))
19981 || template_args_variadic_p (TREE_PURPOSE (pat2))))
19982 {
19983 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
19984 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
19985 int len1 = TREE_VEC_LENGTH (args1);
19986 int len2 = TREE_VEC_LENGTH (args2);
19987
19988 /* We don't count the pack expansion at the end. */
19989 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
19990 --len1;
19991 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
19992 --len2;
19993
19994 if (len1 > len2)
19995 return 1;
19996 else if (len1 < len2)
19997 return -1;
19998 }
19999
20000 return winner;
20001 }
20002
20003 /* Return the template arguments that will produce the function signature
20004 DECL from the function template FN, with the explicit template
20005 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20006 also match. Return NULL_TREE if no satisfactory arguments could be
20007 found. */
20008
20009 static tree
20010 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
20011 {
20012 int ntparms = DECL_NTPARMS (fn);
20013 tree targs = make_tree_vec (ntparms);
20014 tree decl_type = TREE_TYPE (decl);
20015 tree decl_arg_types;
20016 tree *args;
20017 unsigned int nargs, ix;
20018 tree arg;
20019
20020 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
20021
20022 /* Never do unification on the 'this' parameter. */
20023 decl_arg_types = skip_artificial_parms_for (decl,
20024 TYPE_ARG_TYPES (decl_type));
20025
20026 nargs = list_length (decl_arg_types);
20027 args = XALLOCAVEC (tree, nargs);
20028 for (arg = decl_arg_types, ix = 0;
20029 arg != NULL_TREE && arg != void_list_node;
20030 arg = TREE_CHAIN (arg), ++ix)
20031 args[ix] = TREE_VALUE (arg);
20032
20033 if (fn_type_unification (fn, explicit_args, targs,
20034 args, ix,
20035 (check_rettype || DECL_CONV_FN_P (fn)
20036 ? TREE_TYPE (decl_type) : NULL_TREE),
20037 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
20038 /*decltype*/false)
20039 == error_mark_node)
20040 return NULL_TREE;
20041
20042 return targs;
20043 }
20044
20045 /* Return the innermost template arguments that, when applied to a partial
20046 specialization of TMPL whose innermost template parameters are
20047 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
20048 ARGS.
20049
20050 For example, suppose we have:
20051
20052 template <class T, class U> struct S {};
20053 template <class T> struct S<T*, int> {};
20054
20055 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
20056 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
20057 int}. The resulting vector will be {double}, indicating that `T'
20058 is bound to `double'. */
20059
20060 static tree
20061 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
20062 {
20063 int i, ntparms = TREE_VEC_LENGTH (tparms);
20064 tree deduced_args;
20065 tree innermost_deduced_args;
20066
20067 innermost_deduced_args = make_tree_vec (ntparms);
20068 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20069 {
20070 deduced_args = copy_node (args);
20071 SET_TMPL_ARGS_LEVEL (deduced_args,
20072 TMPL_ARGS_DEPTH (deduced_args),
20073 innermost_deduced_args);
20074 }
20075 else
20076 deduced_args = innermost_deduced_args;
20077
20078 if (unify (tparms, deduced_args,
20079 INNERMOST_TEMPLATE_ARGS (spec_args),
20080 INNERMOST_TEMPLATE_ARGS (args),
20081 UNIFY_ALLOW_NONE, /*explain_p=*/false))
20082 return NULL_TREE;
20083
20084 for (i = 0; i < ntparms; ++i)
20085 if (! TREE_VEC_ELT (innermost_deduced_args, i))
20086 return NULL_TREE;
20087
20088 /* Verify that nondeduced template arguments agree with the type
20089 obtained from argument deduction.
20090
20091 For example:
20092
20093 struct A { typedef int X; };
20094 template <class T, class U> struct C {};
20095 template <class T> struct C<T, typename T::X> {};
20096
20097 Then with the instantiation `C<A, int>', we can deduce that
20098 `T' is `A' but unify () does not check whether `typename T::X'
20099 is `int'. */
20100 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
20101 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20102 spec_args, tmpl,
20103 tf_none, false, false);
20104 if (spec_args == error_mark_node
20105 /* We only need to check the innermost arguments; the other
20106 arguments will always agree. */
20107 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
20108 INNERMOST_TEMPLATE_ARGS (args)))
20109 return NULL_TREE;
20110
20111 /* Now that we have bindings for all of the template arguments,
20112 ensure that the arguments deduced for the template template
20113 parameters have compatible template parameter lists. See the use
20114 of template_template_parm_bindings_ok_p in fn_type_unification
20115 for more information. */
20116 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
20117 return NULL_TREE;
20118
20119 return deduced_args;
20120 }
20121
20122 // Compare two function templates T1 and T2 by deducing bindings
20123 // from one against the other. If both deductions succeed, compare
20124 // constraints to see which is more constrained.
20125 static int
20126 more_specialized_inst (tree t1, tree t2)
20127 {
20128 int fate = 0;
20129 int count = 0;
20130
20131 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
20132 {
20133 --fate;
20134 ++count;
20135 }
20136
20137 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
20138 {
20139 ++fate;
20140 ++count;
20141 }
20142
20143 // If both deductions succeed, then one may be more constrained.
20144 if (count == 2 && fate == 0)
20145 fate = more_constrained (t1, t2);
20146
20147 return fate;
20148 }
20149
20150 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
20151 Return the TREE_LIST node with the most specialized template, if
20152 any. If there is no most specialized template, the error_mark_node
20153 is returned.
20154
20155 Note that this function does not look at, or modify, the
20156 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
20157 returned is one of the elements of INSTANTIATIONS, callers may
20158 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
20159 and retrieve it from the value returned. */
20160
20161 tree
20162 most_specialized_instantiation (tree templates)
20163 {
20164 tree fn, champ;
20165
20166 ++processing_template_decl;
20167
20168 champ = templates;
20169 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
20170 {
20171 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
20172 if (fate == -1)
20173 champ = fn;
20174 else if (!fate)
20175 {
20176 /* Equally specialized, move to next function. If there
20177 is no next function, nothing's most specialized. */
20178 fn = TREE_CHAIN (fn);
20179 champ = fn;
20180 if (!fn)
20181 break;
20182 }
20183 }
20184
20185 if (champ)
20186 /* Now verify that champ is better than everything earlier in the
20187 instantiation list. */
20188 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
20189 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
20190 {
20191 champ = NULL_TREE;
20192 break;
20193 }
20194 }
20195
20196 processing_template_decl--;
20197
20198 if (!champ)
20199 return error_mark_node;
20200
20201 return champ;
20202 }
20203
20204 /* If DECL is a specialization of some template, return the most
20205 general such template. Otherwise, returns NULL_TREE.
20206
20207 For example, given:
20208
20209 template <class T> struct S { template <class U> void f(U); };
20210
20211 if TMPL is `template <class U> void S<int>::f(U)' this will return
20212 the full template. This function will not trace past partial
20213 specializations, however. For example, given in addition:
20214
20215 template <class T> struct S<T*> { template <class U> void f(U); };
20216
20217 if TMPL is `template <class U> void S<int*>::f(U)' this will return
20218 `template <class T> template <class U> S<T*>::f(U)'. */
20219
20220 tree
20221 most_general_template (tree decl)
20222 {
20223 if (TREE_CODE (decl) != TEMPLATE_DECL)
20224 {
20225 if (tree tinfo = get_template_info (decl))
20226 decl = TI_TEMPLATE (tinfo);
20227 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
20228 template friend, or a FIELD_DECL for a capture pack. */
20229 if (TREE_CODE (decl) != TEMPLATE_DECL)
20230 return NULL_TREE;
20231 }
20232
20233 /* Look for more and more general templates. */
20234 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
20235 {
20236 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
20237 (See cp-tree.h for details.) */
20238 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
20239 break;
20240
20241 if (CLASS_TYPE_P (TREE_TYPE (decl))
20242 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
20243 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
20244 break;
20245
20246 /* Stop if we run into an explicitly specialized class template. */
20247 if (!DECL_NAMESPACE_SCOPE_P (decl)
20248 && DECL_CONTEXT (decl)
20249 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
20250 break;
20251
20252 decl = DECL_TI_TEMPLATE (decl);
20253 }
20254
20255 return decl;
20256 }
20257
20258 /* Return the most specialized of the template partial specializations
20259 which can produce TARGET, a specialization of some class or variable
20260 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
20261 a TEMPLATE_DECL node corresponding to the partial specialization, while
20262 the TREE_PURPOSE is the set of template arguments that must be
20263 substituted into the template pattern in order to generate TARGET.
20264
20265 If the choice of partial specialization is ambiguous, a diagnostic
20266 is issued, and the error_mark_node is returned. If there are no
20267 partial specializations matching TARGET, then NULL_TREE is
20268 returned, indicating that the primary template should be used. */
20269
20270 static tree
20271 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
20272 {
20273 tree list = NULL_TREE;
20274 tree t;
20275 tree champ;
20276 int fate;
20277 bool ambiguous_p;
20278 tree outer_args = NULL_TREE;
20279 tree tmpl, args;
20280
20281 if (TYPE_P (target))
20282 {
20283 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
20284 tmpl = TI_TEMPLATE (tinfo);
20285 args = TI_ARGS (tinfo);
20286 }
20287 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
20288 {
20289 tmpl = TREE_OPERAND (target, 0);
20290 args = TREE_OPERAND (target, 1);
20291 }
20292 else if (VAR_P (target))
20293 {
20294 tree tinfo = DECL_TEMPLATE_INFO (target);
20295 tmpl = TI_TEMPLATE (tinfo);
20296 args = TI_ARGS (tinfo);
20297 }
20298 else
20299 gcc_unreachable ();
20300
20301 tree main_tmpl = most_general_template (tmpl);
20302
20303 /* For determining which partial specialization to use, only the
20304 innermost args are interesting. */
20305 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
20306 {
20307 outer_args = strip_innermost_template_args (args, 1);
20308 args = INNERMOST_TEMPLATE_ARGS (args);
20309 }
20310
20311 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
20312 {
20313 tree partial_spec_args;
20314 tree spec_args;
20315 tree spec_tmpl = TREE_VALUE (t);
20316
20317 partial_spec_args = TREE_PURPOSE (t);
20318
20319 ++processing_template_decl;
20320
20321 if (outer_args)
20322 {
20323 /* Discard the outer levels of args, and then substitute in the
20324 template args from the enclosing class. */
20325 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
20326 partial_spec_args = tsubst_template_args
20327 (partial_spec_args, outer_args, tf_none, NULL_TREE);
20328
20329 /* And the same for the partial specialization TEMPLATE_DECL. */
20330 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
20331 }
20332
20333 partial_spec_args =
20334 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
20335 partial_spec_args,
20336 tmpl, tf_none,
20337 /*require_all_args=*/true,
20338 /*use_default_args=*/true);
20339
20340 --processing_template_decl;
20341
20342 if (partial_spec_args == error_mark_node)
20343 return error_mark_node;
20344 if (spec_tmpl == error_mark_node)
20345 return error_mark_node;
20346
20347 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
20348 spec_args = get_partial_spec_bindings (tmpl, parms,
20349 partial_spec_args,
20350 args);
20351 if (spec_args)
20352 {
20353 if (outer_args)
20354 spec_args = add_to_template_args (outer_args, spec_args);
20355
20356 /* Keep the candidate only if the constraints are satisfied,
20357 or if we're not compiling with concepts. */
20358 if (!flag_concepts
20359 || constraints_satisfied_p (spec_tmpl, spec_args))
20360 {
20361 list = tree_cons (spec_args, TREE_VALUE (t), list);
20362 TREE_TYPE (list) = TREE_TYPE (t);
20363 }
20364 }
20365 }
20366
20367 if (! list)
20368 return NULL_TREE;
20369
20370 ambiguous_p = false;
20371 t = list;
20372 champ = t;
20373 t = TREE_CHAIN (t);
20374 for (; t; t = TREE_CHAIN (t))
20375 {
20376 fate = more_specialized_partial_spec (tmpl, champ, t);
20377 if (fate == 1)
20378 ;
20379 else
20380 {
20381 if (fate == 0)
20382 {
20383 t = TREE_CHAIN (t);
20384 if (! t)
20385 {
20386 ambiguous_p = true;
20387 break;
20388 }
20389 }
20390 champ = t;
20391 }
20392 }
20393
20394 if (!ambiguous_p)
20395 for (t = list; t && t != champ; t = TREE_CHAIN (t))
20396 {
20397 fate = more_specialized_partial_spec (tmpl, champ, t);
20398 if (fate != 1)
20399 {
20400 ambiguous_p = true;
20401 break;
20402 }
20403 }
20404
20405 if (ambiguous_p)
20406 {
20407 const char *str;
20408 char *spaces = NULL;
20409 if (!(complain & tf_error))
20410 return error_mark_node;
20411 if (TYPE_P (target))
20412 error ("ambiguous template instantiation for %q#T", target);
20413 else
20414 error ("ambiguous template instantiation for %q#D", target);
20415 str = ngettext ("candidate is:", "candidates are:", list_length (list));
20416 for (t = list; t; t = TREE_CHAIN (t))
20417 {
20418 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
20419 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
20420 "%s %#S", spaces ? spaces : str, subst);
20421 spaces = spaces ? spaces : get_spaces (str);
20422 }
20423 free (spaces);
20424 return error_mark_node;
20425 }
20426
20427 return champ;
20428 }
20429
20430 /* Explicitly instantiate DECL. */
20431
20432 void
20433 do_decl_instantiation (tree decl, tree storage)
20434 {
20435 tree result = NULL_TREE;
20436 int extern_p = 0;
20437
20438 if (!decl || decl == error_mark_node)
20439 /* An error occurred, for which grokdeclarator has already issued
20440 an appropriate message. */
20441 return;
20442 else if (! DECL_LANG_SPECIFIC (decl))
20443 {
20444 error ("explicit instantiation of non-template %q#D", decl);
20445 return;
20446 }
20447
20448 bool var_templ = (DECL_TEMPLATE_INFO (decl)
20449 && variable_template_p (DECL_TI_TEMPLATE (decl)));
20450
20451 if (VAR_P (decl) && !var_templ)
20452 {
20453 /* There is an asymmetry here in the way VAR_DECLs and
20454 FUNCTION_DECLs are handled by grokdeclarator. In the case of
20455 the latter, the DECL we get back will be marked as a
20456 template instantiation, and the appropriate
20457 DECL_TEMPLATE_INFO will be set up. This does not happen for
20458 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
20459 should handle VAR_DECLs as it currently handles
20460 FUNCTION_DECLs. */
20461 if (!DECL_CLASS_SCOPE_P (decl))
20462 {
20463 error ("%qD is not a static data member of a class template", decl);
20464 return;
20465 }
20466 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
20467 if (!result || !VAR_P (result))
20468 {
20469 error ("no matching template for %qD found", decl);
20470 return;
20471 }
20472 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
20473 {
20474 error ("type %qT for explicit instantiation %qD does not match "
20475 "declared type %qT", TREE_TYPE (result), decl,
20476 TREE_TYPE (decl));
20477 return;
20478 }
20479 }
20480 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
20481 {
20482 error ("explicit instantiation of %q#D", decl);
20483 return;
20484 }
20485 else
20486 result = decl;
20487
20488 /* Check for various error cases. Note that if the explicit
20489 instantiation is valid the RESULT will currently be marked as an
20490 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
20491 until we get here. */
20492
20493 if (DECL_TEMPLATE_SPECIALIZATION (result))
20494 {
20495 /* DR 259 [temp.spec].
20496
20497 Both an explicit instantiation and a declaration of an explicit
20498 specialization shall not appear in a program unless the explicit
20499 instantiation follows a declaration of the explicit specialization.
20500
20501 For a given set of template parameters, if an explicit
20502 instantiation of a template appears after a declaration of an
20503 explicit specialization for that template, the explicit
20504 instantiation has no effect. */
20505 return;
20506 }
20507 else if (DECL_EXPLICIT_INSTANTIATION (result))
20508 {
20509 /* [temp.spec]
20510
20511 No program shall explicitly instantiate any template more
20512 than once.
20513
20514 We check DECL_NOT_REALLY_EXTERN so as not to complain when
20515 the first instantiation was `extern' and the second is not,
20516 and EXTERN_P for the opposite case. */
20517 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
20518 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
20519 /* If an "extern" explicit instantiation follows an ordinary
20520 explicit instantiation, the template is instantiated. */
20521 if (extern_p)
20522 return;
20523 }
20524 else if (!DECL_IMPLICIT_INSTANTIATION (result))
20525 {
20526 error ("no matching template for %qD found", result);
20527 return;
20528 }
20529 else if (!DECL_TEMPLATE_INFO (result))
20530 {
20531 permerror (input_location, "explicit instantiation of non-template %q#D", result);
20532 return;
20533 }
20534
20535 if (storage == NULL_TREE)
20536 ;
20537 else if (storage == ridpointers[(int) RID_EXTERN])
20538 {
20539 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
20540 pedwarn (input_location, OPT_Wpedantic,
20541 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
20542 "instantiations");
20543 extern_p = 1;
20544 }
20545 else
20546 error ("storage class %qD applied to template instantiation", storage);
20547
20548 check_explicit_instantiation_namespace (result);
20549 mark_decl_instantiated (result, extern_p);
20550 if (! extern_p)
20551 instantiate_decl (result, /*defer_ok=*/1,
20552 /*expl_inst_class_mem_p=*/false);
20553 }
20554
20555 static void
20556 mark_class_instantiated (tree t, int extern_p)
20557 {
20558 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
20559 SET_CLASSTYPE_INTERFACE_KNOWN (t);
20560 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
20561 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
20562 if (! extern_p)
20563 {
20564 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
20565 rest_of_type_compilation (t, 1);
20566 }
20567 }
20568
20569 /* Called from do_type_instantiation through binding_table_foreach to
20570 do recursive instantiation for the type bound in ENTRY. */
20571 static void
20572 bt_instantiate_type_proc (binding_entry entry, void *data)
20573 {
20574 tree storage = *(tree *) data;
20575
20576 if (MAYBE_CLASS_TYPE_P (entry->type)
20577 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
20578 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
20579 }
20580
20581 /* Called from do_type_instantiation to instantiate a member
20582 (a member function or a static member variable) of an
20583 explicitly instantiated class template. */
20584 static void
20585 instantiate_class_member (tree decl, int extern_p)
20586 {
20587 mark_decl_instantiated (decl, extern_p);
20588 if (! extern_p)
20589 instantiate_decl (decl, /*defer_ok=*/1,
20590 /*expl_inst_class_mem_p=*/true);
20591 }
20592
20593 /* Perform an explicit instantiation of template class T. STORAGE, if
20594 non-null, is the RID for extern, inline or static. COMPLAIN is
20595 nonzero if this is called from the parser, zero if called recursively,
20596 since the standard is unclear (as detailed below). */
20597
20598 void
20599 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
20600 {
20601 int extern_p = 0;
20602 int nomem_p = 0;
20603 int static_p = 0;
20604 int previous_instantiation_extern_p = 0;
20605
20606 if (TREE_CODE (t) == TYPE_DECL)
20607 t = TREE_TYPE (t);
20608
20609 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
20610 {
20611 tree tmpl =
20612 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
20613 if (tmpl)
20614 error ("explicit instantiation of non-class template %qD", tmpl);
20615 else
20616 error ("explicit instantiation of non-template type %qT", t);
20617 return;
20618 }
20619
20620 complete_type (t);
20621
20622 if (!COMPLETE_TYPE_P (t))
20623 {
20624 if (complain & tf_error)
20625 error ("explicit instantiation of %q#T before definition of template",
20626 t);
20627 return;
20628 }
20629
20630 if (storage != NULL_TREE)
20631 {
20632 if (!in_system_header_at (input_location))
20633 {
20634 if (storage == ridpointers[(int) RID_EXTERN])
20635 {
20636 if (cxx_dialect == cxx98)
20637 pedwarn (input_location, OPT_Wpedantic,
20638 "ISO C++ 1998 forbids the use of %<extern%> on "
20639 "explicit instantiations");
20640 }
20641 else
20642 pedwarn (input_location, OPT_Wpedantic,
20643 "ISO C++ forbids the use of %qE"
20644 " on explicit instantiations", storage);
20645 }
20646
20647 if (storage == ridpointers[(int) RID_INLINE])
20648 nomem_p = 1;
20649 else if (storage == ridpointers[(int) RID_EXTERN])
20650 extern_p = 1;
20651 else if (storage == ridpointers[(int) RID_STATIC])
20652 static_p = 1;
20653 else
20654 {
20655 error ("storage class %qD applied to template instantiation",
20656 storage);
20657 extern_p = 0;
20658 }
20659 }
20660
20661 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
20662 {
20663 /* DR 259 [temp.spec].
20664
20665 Both an explicit instantiation and a declaration of an explicit
20666 specialization shall not appear in a program unless the explicit
20667 instantiation follows a declaration of the explicit specialization.
20668
20669 For a given set of template parameters, if an explicit
20670 instantiation of a template appears after a declaration of an
20671 explicit specialization for that template, the explicit
20672 instantiation has no effect. */
20673 return;
20674 }
20675 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
20676 {
20677 /* [temp.spec]
20678
20679 No program shall explicitly instantiate any template more
20680 than once.
20681
20682 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
20683 instantiation was `extern'. If EXTERN_P then the second is.
20684 These cases are OK. */
20685 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
20686
20687 if (!previous_instantiation_extern_p && !extern_p
20688 && (complain & tf_error))
20689 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
20690
20691 /* If we've already instantiated the template, just return now. */
20692 if (!CLASSTYPE_INTERFACE_ONLY (t))
20693 return;
20694 }
20695
20696 check_explicit_instantiation_namespace (TYPE_NAME (t));
20697 mark_class_instantiated (t, extern_p);
20698
20699 if (nomem_p)
20700 return;
20701
20702 {
20703 tree tmp;
20704
20705 /* In contrast to implicit instantiation, where only the
20706 declarations, and not the definitions, of members are
20707 instantiated, we have here:
20708
20709 [temp.explicit]
20710
20711 The explicit instantiation of a class template specialization
20712 implies the instantiation of all of its members not
20713 previously explicitly specialized in the translation unit
20714 containing the explicit instantiation.
20715
20716 Of course, we can't instantiate member template classes, since
20717 we don't have any arguments for them. Note that the standard
20718 is unclear on whether the instantiation of the members are
20719 *explicit* instantiations or not. However, the most natural
20720 interpretation is that it should be an explicit instantiation. */
20721
20722 if (! static_p)
20723 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
20724 if (TREE_CODE (tmp) == FUNCTION_DECL
20725 && DECL_TEMPLATE_INSTANTIATION (tmp))
20726 instantiate_class_member (tmp, extern_p);
20727
20728 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
20729 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
20730 instantiate_class_member (tmp, extern_p);
20731
20732 if (CLASSTYPE_NESTED_UTDS (t))
20733 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
20734 bt_instantiate_type_proc, &storage);
20735 }
20736 }
20737
20738 /* Given a function DECL, which is a specialization of TMPL, modify
20739 DECL to be a re-instantiation of TMPL with the same template
20740 arguments. TMPL should be the template into which tsubst'ing
20741 should occur for DECL, not the most general template.
20742
20743 One reason for doing this is a scenario like this:
20744
20745 template <class T>
20746 void f(const T&, int i);
20747
20748 void g() { f(3, 7); }
20749
20750 template <class T>
20751 void f(const T& t, const int i) { }
20752
20753 Note that when the template is first instantiated, with
20754 instantiate_template, the resulting DECL will have no name for the
20755 first parameter, and the wrong type for the second. So, when we go
20756 to instantiate the DECL, we regenerate it. */
20757
20758 static void
20759 regenerate_decl_from_template (tree decl, tree tmpl)
20760 {
20761 /* The arguments used to instantiate DECL, from the most general
20762 template. */
20763 tree args;
20764 tree code_pattern;
20765
20766 args = DECL_TI_ARGS (decl);
20767 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
20768
20769 /* Make sure that we can see identifiers, and compute access
20770 correctly. */
20771 push_access_scope (decl);
20772
20773 if (TREE_CODE (decl) == FUNCTION_DECL)
20774 {
20775 tree decl_parm;
20776 tree pattern_parm;
20777 tree specs;
20778 int args_depth;
20779 int parms_depth;
20780
20781 args_depth = TMPL_ARGS_DEPTH (args);
20782 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
20783 if (args_depth > parms_depth)
20784 args = get_innermost_template_args (args, parms_depth);
20785
20786 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
20787 args, tf_error, NULL_TREE,
20788 /*defer_ok*/false);
20789 if (specs && specs != error_mark_node)
20790 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
20791 specs);
20792
20793 /* Merge parameter declarations. */
20794 decl_parm = skip_artificial_parms_for (decl,
20795 DECL_ARGUMENTS (decl));
20796 pattern_parm
20797 = skip_artificial_parms_for (code_pattern,
20798 DECL_ARGUMENTS (code_pattern));
20799 while (decl_parm && !DECL_PACK_P (pattern_parm))
20800 {
20801 tree parm_type;
20802 tree attributes;
20803
20804 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
20805 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
20806 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
20807 NULL_TREE);
20808 parm_type = type_decays_to (parm_type);
20809 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
20810 TREE_TYPE (decl_parm) = parm_type;
20811 attributes = DECL_ATTRIBUTES (pattern_parm);
20812 if (DECL_ATTRIBUTES (decl_parm) != attributes)
20813 {
20814 DECL_ATTRIBUTES (decl_parm) = attributes;
20815 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
20816 }
20817 decl_parm = DECL_CHAIN (decl_parm);
20818 pattern_parm = DECL_CHAIN (pattern_parm);
20819 }
20820 /* Merge any parameters that match with the function parameter
20821 pack. */
20822 if (pattern_parm && DECL_PACK_P (pattern_parm))
20823 {
20824 int i, len;
20825 tree expanded_types;
20826 /* Expand the TYPE_PACK_EXPANSION that provides the types for
20827 the parameters in this function parameter pack. */
20828 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
20829 args, tf_error, NULL_TREE);
20830 len = TREE_VEC_LENGTH (expanded_types);
20831 for (i = 0; i < len; i++)
20832 {
20833 tree parm_type;
20834 tree attributes;
20835
20836 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
20837 /* Rename the parameter to include the index. */
20838 DECL_NAME (decl_parm) =
20839 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
20840 parm_type = TREE_VEC_ELT (expanded_types, i);
20841 parm_type = type_decays_to (parm_type);
20842 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
20843 TREE_TYPE (decl_parm) = parm_type;
20844 attributes = DECL_ATTRIBUTES (pattern_parm);
20845 if (DECL_ATTRIBUTES (decl_parm) != attributes)
20846 {
20847 DECL_ATTRIBUTES (decl_parm) = attributes;
20848 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
20849 }
20850 decl_parm = DECL_CHAIN (decl_parm);
20851 }
20852 }
20853 /* Merge additional specifiers from the CODE_PATTERN. */
20854 if (DECL_DECLARED_INLINE_P (code_pattern)
20855 && !DECL_DECLARED_INLINE_P (decl))
20856 DECL_DECLARED_INLINE_P (decl) = 1;
20857 }
20858 else if (VAR_P (decl))
20859 {
20860 DECL_INITIAL (decl) =
20861 tsubst_expr (DECL_INITIAL (code_pattern), args,
20862 tf_error, DECL_TI_TEMPLATE (decl),
20863 /*integral_constant_expression_p=*/false);
20864 if (VAR_HAD_UNKNOWN_BOUND (decl))
20865 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
20866 tf_error, DECL_TI_TEMPLATE (decl));
20867 }
20868 else
20869 gcc_unreachable ();
20870
20871 pop_access_scope (decl);
20872 }
20873
20874 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
20875 substituted to get DECL. */
20876
20877 tree
20878 template_for_substitution (tree decl)
20879 {
20880 tree tmpl = DECL_TI_TEMPLATE (decl);
20881
20882 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
20883 for the instantiation. This is not always the most general
20884 template. Consider, for example:
20885
20886 template <class T>
20887 struct S { template <class U> void f();
20888 template <> void f<int>(); };
20889
20890 and an instantiation of S<double>::f<int>. We want TD to be the
20891 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
20892 while (/* An instantiation cannot have a definition, so we need a
20893 more general template. */
20894 DECL_TEMPLATE_INSTANTIATION (tmpl)
20895 /* We must also deal with friend templates. Given:
20896
20897 template <class T> struct S {
20898 template <class U> friend void f() {};
20899 };
20900
20901 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
20902 so far as the language is concerned, but that's still
20903 where we get the pattern for the instantiation from. On
20904 other hand, if the definition comes outside the class, say:
20905
20906 template <class T> struct S {
20907 template <class U> friend void f();
20908 };
20909 template <class U> friend void f() {}
20910
20911 we don't need to look any further. That's what the check for
20912 DECL_INITIAL is for. */
20913 || (TREE_CODE (decl) == FUNCTION_DECL
20914 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
20915 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
20916 {
20917 /* The present template, TD, should not be a definition. If it
20918 were a definition, we should be using it! Note that we
20919 cannot restructure the loop to just keep going until we find
20920 a template with a definition, since that might go too far if
20921 a specialization was declared, but not defined. */
20922
20923 /* Fetch the more general template. */
20924 tmpl = DECL_TI_TEMPLATE (tmpl);
20925 }
20926
20927 return tmpl;
20928 }
20929
20930 /* Returns true if we need to instantiate this template instance even if we
20931 know we aren't going to emit it. */
20932
20933 bool
20934 always_instantiate_p (tree decl)
20935 {
20936 /* We always instantiate inline functions so that we can inline them. An
20937 explicit instantiation declaration prohibits implicit instantiation of
20938 non-inline functions. With high levels of optimization, we would
20939 normally inline non-inline functions -- but we're not allowed to do
20940 that for "extern template" functions. Therefore, we check
20941 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20942 return ((TREE_CODE (decl) == FUNCTION_DECL
20943 && (DECL_DECLARED_INLINE_P (decl)
20944 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
20945 /* And we need to instantiate static data members so that
20946 their initializers are available in integral constant
20947 expressions. */
20948 || (VAR_P (decl)
20949 && decl_maybe_constant_var_p (decl)));
20950 }
20951
20952 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20953 instantiate it now, modifying TREE_TYPE (fn). */
20954
20955 void
20956 maybe_instantiate_noexcept (tree fn)
20957 {
20958 tree fntype, spec, noex, clone;
20959
20960 /* Don't instantiate a noexcept-specification from template context. */
20961 if (processing_template_decl)
20962 return;
20963
20964 if (DECL_CLONED_FUNCTION_P (fn))
20965 fn = DECL_CLONED_FUNCTION (fn);
20966 fntype = TREE_TYPE (fn);
20967 spec = TYPE_RAISES_EXCEPTIONS (fntype);
20968
20969 if (!spec || !TREE_PURPOSE (spec))
20970 return;
20971
20972 noex = TREE_PURPOSE (spec);
20973
20974 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
20975 {
20976 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
20977 spec = get_defaulted_eh_spec (fn);
20978 else if (push_tinst_level (fn))
20979 {
20980 push_access_scope (fn);
20981 push_deferring_access_checks (dk_no_deferred);
20982 input_location = DECL_SOURCE_LOCATION (fn);
20983 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
20984 DEFERRED_NOEXCEPT_ARGS (noex),
20985 tf_warning_or_error, fn,
20986 /*function_p=*/false,
20987 /*integral_constant_expression_p=*/true);
20988 pop_deferring_access_checks ();
20989 pop_access_scope (fn);
20990 pop_tinst_level ();
20991 spec = build_noexcept_spec (noex, tf_warning_or_error);
20992 if (spec == error_mark_node)
20993 spec = noexcept_false_spec;
20994 }
20995 else
20996 spec = noexcept_false_spec;
20997
20998 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
20999 }
21000
21001 FOR_EACH_CLONE (clone, fn)
21002 {
21003 if (TREE_TYPE (clone) == fntype)
21004 TREE_TYPE (clone) = TREE_TYPE (fn);
21005 else
21006 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
21007 }
21008 }
21009
21010 /* Produce the definition of D, a _DECL generated from a template. If
21011 DEFER_OK is nonzero, then we don't have to actually do the
21012 instantiation now; we just have to do it sometime. Normally it is
21013 an error if this is an explicit instantiation but D is undefined.
21014 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21015 explicitly instantiated class template. */
21016
21017 tree
21018 instantiate_decl (tree d, int defer_ok,
21019 bool expl_inst_class_mem_p)
21020 {
21021 tree tmpl = DECL_TI_TEMPLATE (d);
21022 tree gen_args;
21023 tree args;
21024 tree td;
21025 tree code_pattern;
21026 tree spec;
21027 tree gen_tmpl;
21028 bool pattern_defined;
21029 location_t saved_loc = input_location;
21030 int saved_unevaluated_operand = cp_unevaluated_operand;
21031 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21032 bool external_p;
21033 bool deleted_p;
21034 tree fn_context;
21035 bool nested;
21036
21037 /* This function should only be used to instantiate templates for
21038 functions and static member variables. */
21039 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
21040
21041 /* A concept is never instantiated. */
21042 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
21043
21044 /* Variables are never deferred; if instantiation is required, they
21045 are instantiated right away. That allows for better code in the
21046 case that an expression refers to the value of the variable --
21047 if the variable has a constant value the referring expression can
21048 take advantage of that fact. */
21049 if (VAR_P (d)
21050 || DECL_DECLARED_CONSTEXPR_P (d))
21051 defer_ok = 0;
21052
21053 /* Don't instantiate cloned functions. Instead, instantiate the
21054 functions they cloned. */
21055 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
21056 d = DECL_CLONED_FUNCTION (d);
21057
21058 if (DECL_TEMPLATE_INSTANTIATED (d)
21059 || (TREE_CODE (d) == FUNCTION_DECL
21060 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
21061 || DECL_TEMPLATE_SPECIALIZATION (d))
21062 /* D has already been instantiated or explicitly specialized, so
21063 there's nothing for us to do here.
21064
21065 It might seem reasonable to check whether or not D is an explicit
21066 instantiation, and, if so, stop here. But when an explicit
21067 instantiation is deferred until the end of the compilation,
21068 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21069 the instantiation. */
21070 return d;
21071
21072 /* Check to see whether we know that this template will be
21073 instantiated in some other file, as with "extern template"
21074 extension. */
21075 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
21076
21077 /* In general, we do not instantiate such templates. */
21078 if (external_p && !always_instantiate_p (d))
21079 return d;
21080
21081 gen_tmpl = most_general_template (tmpl);
21082 gen_args = DECL_TI_ARGS (d);
21083
21084 if (tmpl != gen_tmpl)
21085 /* We should already have the extra args. */
21086 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
21087 == TMPL_ARGS_DEPTH (gen_args));
21088 /* And what's in the hash table should match D. */
21089 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
21090 || spec == NULL_TREE);
21091
21092 /* This needs to happen before any tsubsting. */
21093 if (! push_tinst_level (d))
21094 return d;
21095
21096 timevar_push (TV_TEMPLATE_INST);
21097
21098 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
21099 for the instantiation. */
21100 td = template_for_substitution (d);
21101 code_pattern = DECL_TEMPLATE_RESULT (td);
21102
21103 /* We should never be trying to instantiate a member of a class
21104 template or partial specialization. */
21105 gcc_assert (d != code_pattern);
21106
21107 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
21108 || DECL_TEMPLATE_SPECIALIZATION (td))
21109 /* In the case of a friend template whose definition is provided
21110 outside the class, we may have too many arguments. Drop the
21111 ones we don't need. The same is true for specializations. */
21112 args = get_innermost_template_args
21113 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
21114 else
21115 args = gen_args;
21116
21117 if (TREE_CODE (d) == FUNCTION_DECL)
21118 {
21119 deleted_p = DECL_DELETED_FN (code_pattern);
21120 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
21121 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
21122 || deleted_p);
21123 }
21124 else
21125 {
21126 deleted_p = false;
21127 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
21128 }
21129
21130 /* We may be in the middle of deferred access check. Disable it now. */
21131 push_deferring_access_checks (dk_no_deferred);
21132
21133 /* Unless an explicit instantiation directive has already determined
21134 the linkage of D, remember that a definition is available for
21135 this entity. */
21136 if (pattern_defined
21137 && !DECL_INTERFACE_KNOWN (d)
21138 && !DECL_NOT_REALLY_EXTERN (d))
21139 mark_definable (d);
21140
21141 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
21142 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
21143 input_location = DECL_SOURCE_LOCATION (d);
21144
21145 /* If D is a member of an explicitly instantiated class template,
21146 and no definition is available, treat it like an implicit
21147 instantiation. */
21148 if (!pattern_defined && expl_inst_class_mem_p
21149 && DECL_EXPLICIT_INSTANTIATION (d))
21150 {
21151 /* Leave linkage flags alone on instantiations with anonymous
21152 visibility. */
21153 if (TREE_PUBLIC (d))
21154 {
21155 DECL_NOT_REALLY_EXTERN (d) = 0;
21156 DECL_INTERFACE_KNOWN (d) = 0;
21157 }
21158 SET_DECL_IMPLICIT_INSTANTIATION (d);
21159 }
21160
21161 /* Defer all other templates, unless we have been explicitly
21162 forbidden from doing so. */
21163 if (/* If there is no definition, we cannot instantiate the
21164 template. */
21165 ! pattern_defined
21166 /* If it's OK to postpone instantiation, do so. */
21167 || defer_ok
21168 /* If this is a static data member that will be defined
21169 elsewhere, we don't want to instantiate the entire data
21170 member, but we do want to instantiate the initializer so that
21171 we can substitute that elsewhere. */
21172 || (external_p && VAR_P (d))
21173 /* Handle here a deleted function too, avoid generating
21174 its body (c++/61080). */
21175 || deleted_p)
21176 {
21177 /* The definition of the static data member is now required so
21178 we must substitute the initializer. */
21179 if (VAR_P (d)
21180 && !DECL_INITIAL (d)
21181 && DECL_INITIAL (code_pattern))
21182 {
21183 tree ns;
21184 tree init;
21185 bool const_init = false;
21186 bool enter_context = DECL_CLASS_SCOPE_P (d);
21187
21188 ns = decl_namespace_context (d);
21189 push_nested_namespace (ns);
21190 if (enter_context)
21191 push_nested_class (DECL_CONTEXT (d));
21192 init = tsubst_expr (DECL_INITIAL (code_pattern),
21193 args,
21194 tf_warning_or_error, NULL_TREE,
21195 /*integral_constant_expression_p=*/false);
21196 /* If instantiating the initializer involved instantiating this
21197 again, don't call cp_finish_decl twice. */
21198 if (!DECL_INITIAL (d))
21199 {
21200 /* Make sure the initializer is still constant, in case of
21201 circular dependency (template/instantiate6.C). */
21202 const_init
21203 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21204 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
21205 /*asmspec_tree=*/NULL_TREE,
21206 LOOKUP_ONLYCONVERTING);
21207 }
21208 if (enter_context)
21209 pop_nested_class ();
21210 pop_nested_namespace (ns);
21211 }
21212
21213 /* We restore the source position here because it's used by
21214 add_pending_template. */
21215 input_location = saved_loc;
21216
21217 if (at_eof && !pattern_defined
21218 && DECL_EXPLICIT_INSTANTIATION (d)
21219 && DECL_NOT_REALLY_EXTERN (d))
21220 /* [temp.explicit]
21221
21222 The definition of a non-exported function template, a
21223 non-exported member function template, or a non-exported
21224 member function or static data member of a class template
21225 shall be present in every translation unit in which it is
21226 explicitly instantiated. */
21227 permerror (input_location, "explicit instantiation of %qD "
21228 "but no definition available", d);
21229
21230 /* If we're in unevaluated context, we just wanted to get the
21231 constant value; this isn't an odr use, so don't queue
21232 a full instantiation. */
21233 if (cp_unevaluated_operand != 0)
21234 goto out;
21235 /* ??? Historically, we have instantiated inline functions, even
21236 when marked as "extern template". */
21237 if (!(external_p && VAR_P (d)))
21238 add_pending_template (d);
21239 goto out;
21240 }
21241 /* Tell the repository that D is available in this translation unit
21242 -- and see if it is supposed to be instantiated here. */
21243 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
21244 {
21245 /* In a PCH file, despite the fact that the repository hasn't
21246 requested instantiation in the PCH it is still possible that
21247 an instantiation will be required in a file that includes the
21248 PCH. */
21249 if (pch_file)
21250 add_pending_template (d);
21251 /* Instantiate inline functions so that the inliner can do its
21252 job, even though we'll not be emitting a copy of this
21253 function. */
21254 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
21255 goto out;
21256 }
21257
21258 fn_context = decl_function_context (d);
21259 nested = (current_function_decl != NULL_TREE);
21260 if (!fn_context)
21261 push_to_top_level ();
21262 else
21263 {
21264 if (nested)
21265 push_function_context ();
21266 cp_unevaluated_operand = 0;
21267 c_inhibit_evaluation_warnings = 0;
21268 }
21269
21270 /* Mark D as instantiated so that recursive calls to
21271 instantiate_decl do not try to instantiate it again. */
21272 DECL_TEMPLATE_INSTANTIATED (d) = 1;
21273
21274 /* Regenerate the declaration in case the template has been modified
21275 by a subsequent redeclaration. */
21276 regenerate_decl_from_template (d, td);
21277
21278 /* We already set the file and line above. Reset them now in case
21279 they changed as a result of calling regenerate_decl_from_template. */
21280 input_location = DECL_SOURCE_LOCATION (d);
21281
21282 if (VAR_P (d))
21283 {
21284 tree init;
21285 bool const_init = false;
21286
21287 /* Clear out DECL_RTL; whatever was there before may not be right
21288 since we've reset the type of the declaration. */
21289 SET_DECL_RTL (d, NULL);
21290 DECL_IN_AGGR_P (d) = 0;
21291
21292 /* The initializer is placed in DECL_INITIAL by
21293 regenerate_decl_from_template so we don't need to
21294 push/pop_access_scope again here. Pull it out so that
21295 cp_finish_decl can process it. */
21296 init = DECL_INITIAL (d);
21297 DECL_INITIAL (d) = NULL_TREE;
21298 DECL_INITIALIZED_P (d) = 0;
21299
21300 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
21301 initializer. That function will defer actual emission until
21302 we have a chance to determine linkage. */
21303 DECL_EXTERNAL (d) = 0;
21304
21305 /* Enter the scope of D so that access-checking works correctly. */
21306 bool enter_context = DECL_CLASS_SCOPE_P (d);
21307 if (enter_context)
21308 push_nested_class (DECL_CONTEXT (d));
21309
21310 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
21311 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
21312
21313 if (enter_context)
21314 pop_nested_class ();
21315
21316 if (variable_template_p (td))
21317 note_variable_template_instantiation (d);
21318 }
21319 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
21320 synthesize_method (d);
21321 else if (TREE_CODE (d) == FUNCTION_DECL)
21322 {
21323 hash_map<tree, tree> *saved_local_specializations;
21324 tree subst_decl;
21325 tree tmpl_parm;
21326 tree spec_parm;
21327 tree block = NULL_TREE;
21328
21329 /* Save away the current list, in case we are instantiating one
21330 template from within the body of another. */
21331 saved_local_specializations = local_specializations;
21332
21333 /* Set up the list of local specializations. */
21334 local_specializations = new hash_map<tree, tree>;
21335
21336 /* Set up context. */
21337 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21338 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21339 block = push_stmt_list ();
21340 else
21341 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
21342
21343 /* Some typedefs referenced from within the template code need to be
21344 access checked at template instantiation time, i.e now. These
21345 types were added to the template at parsing time. Let's get those
21346 and perform the access checks then. */
21347 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
21348 gen_args);
21349
21350 /* Create substitution entries for the parameters. */
21351 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
21352 tmpl_parm = DECL_ARGUMENTS (subst_decl);
21353 spec_parm = DECL_ARGUMENTS (d);
21354 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
21355 {
21356 register_local_specialization (spec_parm, tmpl_parm);
21357 spec_parm = skip_artificial_parms_for (d, spec_parm);
21358 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
21359 }
21360 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
21361 {
21362 if (!DECL_PACK_P (tmpl_parm))
21363 {
21364 register_local_specialization (spec_parm, tmpl_parm);
21365 spec_parm = DECL_CHAIN (spec_parm);
21366 }
21367 else
21368 {
21369 /* Register the (value) argument pack as a specialization of
21370 TMPL_PARM, then move on. */
21371 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
21372 register_local_specialization (argpack, tmpl_parm);
21373 }
21374 }
21375 gcc_assert (!spec_parm);
21376
21377 /* Substitute into the body of the function. */
21378 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21379 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
21380 tf_warning_or_error, tmpl);
21381 else
21382 {
21383 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
21384 tf_warning_or_error, tmpl,
21385 /*integral_constant_expression_p=*/false);
21386
21387 /* Set the current input_location to the end of the function
21388 so that finish_function knows where we are. */
21389 input_location
21390 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
21391
21392 /* Remember if we saw an infinite loop in the template. */
21393 current_function_infinite_loop
21394 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
21395 }
21396
21397 /* We don't need the local specializations any more. */
21398 delete local_specializations;
21399 local_specializations = saved_local_specializations;
21400
21401 /* Finish the function. */
21402 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
21403 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
21404 DECL_SAVED_TREE (d) = pop_stmt_list (block);
21405 else
21406 {
21407 d = finish_function (0);
21408 expand_or_defer_fn (d);
21409 }
21410
21411 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
21412 cp_check_omp_declare_reduction (d);
21413 }
21414
21415 /* We're not deferring instantiation any more. */
21416 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
21417
21418 if (!fn_context)
21419 pop_from_top_level ();
21420 else if (nested)
21421 pop_function_context ();
21422
21423 out:
21424 input_location = saved_loc;
21425 cp_unevaluated_operand = saved_unevaluated_operand;
21426 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21427 pop_deferring_access_checks ();
21428 pop_tinst_level ();
21429
21430 timevar_pop (TV_TEMPLATE_INST);
21431
21432 return d;
21433 }
21434
21435 /* Run through the list of templates that we wish we could
21436 instantiate, and instantiate any we can. RETRIES is the
21437 number of times we retry pending template instantiation. */
21438
21439 void
21440 instantiate_pending_templates (int retries)
21441 {
21442 int reconsider;
21443 location_t saved_loc = input_location;
21444
21445 /* Instantiating templates may trigger vtable generation. This in turn
21446 may require further template instantiations. We place a limit here
21447 to avoid infinite loop. */
21448 if (pending_templates && retries >= max_tinst_depth)
21449 {
21450 tree decl = pending_templates->tinst->decl;
21451
21452 fatal_error (input_location,
21453 "template instantiation depth exceeds maximum of %d"
21454 " instantiating %q+D, possibly from virtual table generation"
21455 " (use -ftemplate-depth= to increase the maximum)",
21456 max_tinst_depth, decl);
21457 if (TREE_CODE (decl) == FUNCTION_DECL)
21458 /* Pretend that we defined it. */
21459 DECL_INITIAL (decl) = error_mark_node;
21460 return;
21461 }
21462
21463 do
21464 {
21465 struct pending_template **t = &pending_templates;
21466 struct pending_template *last = NULL;
21467 reconsider = 0;
21468 while (*t)
21469 {
21470 tree instantiation = reopen_tinst_level ((*t)->tinst);
21471 bool complete = false;
21472
21473 if (TYPE_P (instantiation))
21474 {
21475 tree fn;
21476
21477 if (!COMPLETE_TYPE_P (instantiation))
21478 {
21479 instantiate_class_template (instantiation);
21480 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
21481 for (fn = TYPE_METHODS (instantiation);
21482 fn;
21483 fn = TREE_CHAIN (fn))
21484 if (! DECL_ARTIFICIAL (fn))
21485 instantiate_decl (fn,
21486 /*defer_ok=*/0,
21487 /*expl_inst_class_mem_p=*/false);
21488 if (COMPLETE_TYPE_P (instantiation))
21489 reconsider = 1;
21490 }
21491
21492 complete = COMPLETE_TYPE_P (instantiation);
21493 }
21494 else
21495 {
21496 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
21497 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
21498 {
21499 instantiation
21500 = instantiate_decl (instantiation,
21501 /*defer_ok=*/0,
21502 /*expl_inst_class_mem_p=*/false);
21503 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
21504 reconsider = 1;
21505 }
21506
21507 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
21508 || DECL_TEMPLATE_INSTANTIATED (instantiation));
21509 }
21510
21511 if (complete)
21512 /* If INSTANTIATION has been instantiated, then we don't
21513 need to consider it again in the future. */
21514 *t = (*t)->next;
21515 else
21516 {
21517 last = *t;
21518 t = &(*t)->next;
21519 }
21520 tinst_depth = 0;
21521 current_tinst_level = NULL;
21522 }
21523 last_pending_template = last;
21524 }
21525 while (reconsider);
21526
21527 input_location = saved_loc;
21528 }
21529
21530 /* Substitute ARGVEC into T, which is a list of initializers for
21531 either base class or a non-static data member. The TREE_PURPOSEs
21532 are DECLs, and the TREE_VALUEs are the initializer values. Used by
21533 instantiate_decl. */
21534
21535 static tree
21536 tsubst_initializer_list (tree t, tree argvec)
21537 {
21538 tree inits = NULL_TREE;
21539
21540 for (; t; t = TREE_CHAIN (t))
21541 {
21542 tree decl;
21543 tree init;
21544 tree expanded_bases = NULL_TREE;
21545 tree expanded_arguments = NULL_TREE;
21546 int i, len = 1;
21547
21548 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
21549 {
21550 tree expr;
21551 tree arg;
21552
21553 /* Expand the base class expansion type into separate base
21554 classes. */
21555 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
21556 tf_warning_or_error,
21557 NULL_TREE);
21558 if (expanded_bases == error_mark_node)
21559 continue;
21560
21561 /* We'll be building separate TREE_LISTs of arguments for
21562 each base. */
21563 len = TREE_VEC_LENGTH (expanded_bases);
21564 expanded_arguments = make_tree_vec (len);
21565 for (i = 0; i < len; i++)
21566 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
21567
21568 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
21569 expand each argument in the TREE_VALUE of t. */
21570 expr = make_node (EXPR_PACK_EXPANSION);
21571 PACK_EXPANSION_LOCAL_P (expr) = true;
21572 PACK_EXPANSION_PARAMETER_PACKS (expr) =
21573 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
21574
21575 if (TREE_VALUE (t) == void_type_node)
21576 /* VOID_TYPE_NODE is used to indicate
21577 value-initialization. */
21578 {
21579 for (i = 0; i < len; i++)
21580 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
21581 }
21582 else
21583 {
21584 /* Substitute parameter packs into each argument in the
21585 TREE_LIST. */
21586 in_base_initializer = 1;
21587 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
21588 {
21589 tree expanded_exprs;
21590
21591 /* Expand the argument. */
21592 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
21593 expanded_exprs
21594 = tsubst_pack_expansion (expr, argvec,
21595 tf_warning_or_error,
21596 NULL_TREE);
21597 if (expanded_exprs == error_mark_node)
21598 continue;
21599
21600 /* Prepend each of the expanded expressions to the
21601 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
21602 for (i = 0; i < len; i++)
21603 {
21604 TREE_VEC_ELT (expanded_arguments, i) =
21605 tree_cons (NULL_TREE,
21606 TREE_VEC_ELT (expanded_exprs, i),
21607 TREE_VEC_ELT (expanded_arguments, i));
21608 }
21609 }
21610 in_base_initializer = 0;
21611
21612 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
21613 since we built them backwards. */
21614 for (i = 0; i < len; i++)
21615 {
21616 TREE_VEC_ELT (expanded_arguments, i) =
21617 nreverse (TREE_VEC_ELT (expanded_arguments, i));
21618 }
21619 }
21620 }
21621
21622 for (i = 0; i < len; ++i)
21623 {
21624 if (expanded_bases)
21625 {
21626 decl = TREE_VEC_ELT (expanded_bases, i);
21627 decl = expand_member_init (decl);
21628 init = TREE_VEC_ELT (expanded_arguments, i);
21629 }
21630 else
21631 {
21632 tree tmp;
21633 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
21634 tf_warning_or_error, NULL_TREE);
21635
21636 decl = expand_member_init (decl);
21637 if (decl && !DECL_P (decl))
21638 in_base_initializer = 1;
21639
21640 init = TREE_VALUE (t);
21641 tmp = init;
21642 if (init != void_type_node)
21643 init = tsubst_expr (init, argvec,
21644 tf_warning_or_error, NULL_TREE,
21645 /*integral_constant_expression_p=*/false);
21646 if (init == NULL_TREE && tmp != NULL_TREE)
21647 /* If we had an initializer but it instantiated to nothing,
21648 value-initialize the object. This will only occur when
21649 the initializer was a pack expansion where the parameter
21650 packs used in that expansion were of length zero. */
21651 init = void_type_node;
21652 in_base_initializer = 0;
21653 }
21654
21655 if (decl)
21656 {
21657 init = build_tree_list (decl, init);
21658 TREE_CHAIN (init) = inits;
21659 inits = init;
21660 }
21661 }
21662 }
21663 return inits;
21664 }
21665
21666 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
21667
21668 static void
21669 set_current_access_from_decl (tree decl)
21670 {
21671 if (TREE_PRIVATE (decl))
21672 current_access_specifier = access_private_node;
21673 else if (TREE_PROTECTED (decl))
21674 current_access_specifier = access_protected_node;
21675 else
21676 current_access_specifier = access_public_node;
21677 }
21678
21679 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
21680 is the instantiation (which should have been created with
21681 start_enum) and ARGS are the template arguments to use. */
21682
21683 static void
21684 tsubst_enum (tree tag, tree newtag, tree args)
21685 {
21686 tree e;
21687
21688 if (SCOPED_ENUM_P (newtag))
21689 begin_scope (sk_scoped_enum, newtag);
21690
21691 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
21692 {
21693 tree value;
21694 tree decl;
21695
21696 decl = TREE_VALUE (e);
21697 /* Note that in a template enum, the TREE_VALUE is the
21698 CONST_DECL, not the corresponding INTEGER_CST. */
21699 value = tsubst_expr (DECL_INITIAL (decl),
21700 args, tf_warning_or_error, NULL_TREE,
21701 /*integral_constant_expression_p=*/true);
21702
21703 /* Give this enumeration constant the correct access. */
21704 set_current_access_from_decl (decl);
21705
21706 /* Actually build the enumerator itself. Here we're assuming that
21707 enumerators can't have dependent attributes. */
21708 build_enumerator (DECL_NAME (decl), value, newtag,
21709 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
21710 }
21711
21712 if (SCOPED_ENUM_P (newtag))
21713 finish_scope ();
21714
21715 finish_enum_value_list (newtag);
21716 finish_enum (newtag);
21717
21718 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
21719 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
21720 }
21721
21722 /* DECL is a FUNCTION_DECL that is a template specialization. Return
21723 its type -- but without substituting the innermost set of template
21724 arguments. So, innermost set of template parameters will appear in
21725 the type. */
21726
21727 tree
21728 get_mostly_instantiated_function_type (tree decl)
21729 {
21730 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
21731 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
21732 }
21733
21734 /* Return truthvalue if we're processing a template different from
21735 the last one involved in diagnostics. */
21736 bool
21737 problematic_instantiation_changed (void)
21738 {
21739 return current_tinst_level != last_error_tinst_level;
21740 }
21741
21742 /* Remember current template involved in diagnostics. */
21743 void
21744 record_last_problematic_instantiation (void)
21745 {
21746 last_error_tinst_level = current_tinst_level;
21747 }
21748
21749 struct tinst_level *
21750 current_instantiation (void)
21751 {
21752 return current_tinst_level;
21753 }
21754
21755 /* Return TRUE if current_function_decl is being instantiated, false
21756 otherwise. */
21757
21758 bool
21759 instantiating_current_function_p (void)
21760 {
21761 return (current_instantiation ()
21762 && current_instantiation ()->decl == current_function_decl);
21763 }
21764
21765 /* [temp.param] Check that template non-type parm TYPE is of an allowable
21766 type. Return zero for ok, nonzero for disallowed. Issue error and
21767 warning messages under control of COMPLAIN. */
21768
21769 static int
21770 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
21771 {
21772 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
21773 return 0;
21774 else if (POINTER_TYPE_P (type))
21775 return 0;
21776 else if (TYPE_PTRMEM_P (type))
21777 return 0;
21778 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
21779 return 0;
21780 else if (TREE_CODE (type) == TYPENAME_TYPE)
21781 return 0;
21782 else if (TREE_CODE (type) == DECLTYPE_TYPE)
21783 return 0;
21784 else if (TREE_CODE (type) == NULLPTR_TYPE)
21785 return 0;
21786 /* A bound template template parm could later be instantiated to have a valid
21787 nontype parm type via an alias template. */
21788 else if (cxx_dialect >= cxx11
21789 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21790 return 0;
21791
21792 if (complain & tf_error)
21793 {
21794 if (type == error_mark_node)
21795 inform (input_location, "invalid template non-type parameter");
21796 else
21797 error ("%q#T is not a valid type for a template non-type parameter",
21798 type);
21799 }
21800 return 1;
21801 }
21802
21803 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
21804 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
21805
21806 static bool
21807 dependent_type_p_r (tree type)
21808 {
21809 tree scope;
21810
21811 /* [temp.dep.type]
21812
21813 A type is dependent if it is:
21814
21815 -- a template parameter. Template template parameters are types
21816 for us (since TYPE_P holds true for them) so we handle
21817 them here. */
21818 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21819 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
21820 return true;
21821 /* -- a qualified-id with a nested-name-specifier which contains a
21822 class-name that names a dependent type or whose unqualified-id
21823 names a dependent type. */
21824 if (TREE_CODE (type) == TYPENAME_TYPE)
21825 return true;
21826
21827 /* An alias template specialization can be dependent even if the
21828 resulting type is not. */
21829 if (dependent_alias_template_spec_p (type))
21830 return true;
21831
21832 /* -- a cv-qualified type where the cv-unqualified type is
21833 dependent.
21834 No code is necessary for this bullet; the code below handles
21835 cv-qualified types, and we don't want to strip aliases with
21836 TYPE_MAIN_VARIANT because of DR 1558. */
21837 /* -- a compound type constructed from any dependent type. */
21838 if (TYPE_PTRMEM_P (type))
21839 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
21840 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
21841 (type)));
21842 else if (TYPE_PTR_P (type)
21843 || TREE_CODE (type) == REFERENCE_TYPE)
21844 return dependent_type_p (TREE_TYPE (type));
21845 else if (TREE_CODE (type) == FUNCTION_TYPE
21846 || TREE_CODE (type) == METHOD_TYPE)
21847 {
21848 tree arg_type;
21849
21850 if (dependent_type_p (TREE_TYPE (type)))
21851 return true;
21852 for (arg_type = TYPE_ARG_TYPES (type);
21853 arg_type;
21854 arg_type = TREE_CHAIN (arg_type))
21855 if (dependent_type_p (TREE_VALUE (arg_type)))
21856 return true;
21857 return false;
21858 }
21859 /* -- an array type constructed from any dependent type or whose
21860 size is specified by a constant expression that is
21861 value-dependent.
21862
21863 We checked for type- and value-dependence of the bounds in
21864 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
21865 if (TREE_CODE (type) == ARRAY_TYPE)
21866 {
21867 if (TYPE_DOMAIN (type)
21868 && dependent_type_p (TYPE_DOMAIN (type)))
21869 return true;
21870 return dependent_type_p (TREE_TYPE (type));
21871 }
21872
21873 /* -- a template-id in which either the template name is a template
21874 parameter ... */
21875 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21876 return true;
21877 /* ... or any of the template arguments is a dependent type or
21878 an expression that is type-dependent or value-dependent. */
21879 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
21880 && (any_dependent_template_arguments_p
21881 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
21882 return true;
21883
21884 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
21885 dependent; if the argument of the `typeof' expression is not
21886 type-dependent, then it should already been have resolved. */
21887 if (TREE_CODE (type) == TYPEOF_TYPE
21888 || TREE_CODE (type) == DECLTYPE_TYPE
21889 || TREE_CODE (type) == UNDERLYING_TYPE)
21890 return true;
21891
21892 /* A template argument pack is dependent if any of its packed
21893 arguments are. */
21894 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
21895 {
21896 tree args = ARGUMENT_PACK_ARGS (type);
21897 int i, len = TREE_VEC_LENGTH (args);
21898 for (i = 0; i < len; ++i)
21899 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21900 return true;
21901 }
21902
21903 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
21904 be template parameters. */
21905 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
21906 return true;
21907
21908 /* The standard does not specifically mention types that are local
21909 to template functions or local classes, but they should be
21910 considered dependent too. For example:
21911
21912 template <int I> void f() {
21913 enum E { a = I };
21914 S<sizeof (E)> s;
21915 }
21916
21917 The size of `E' cannot be known until the value of `I' has been
21918 determined. Therefore, `E' must be considered dependent. */
21919 scope = TYPE_CONTEXT (type);
21920 if (scope && TYPE_P (scope))
21921 return dependent_type_p (scope);
21922 /* Don't use type_dependent_expression_p here, as it can lead
21923 to infinite recursion trying to determine whether a lambda
21924 nested in a lambda is dependent (c++/47687). */
21925 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
21926 && DECL_LANG_SPECIFIC (scope)
21927 && DECL_TEMPLATE_INFO (scope)
21928 && (any_dependent_template_arguments_p
21929 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
21930 return true;
21931
21932 /* Other types are non-dependent. */
21933 return false;
21934 }
21935
21936 /* Returns TRUE if TYPE is dependent, in the sense of
21937 [temp.dep.type]. Note that a NULL type is considered dependent. */
21938
21939 bool
21940 dependent_type_p (tree type)
21941 {
21942 /* If there are no template parameters in scope, then there can't be
21943 any dependent types. */
21944 if (!processing_template_decl)
21945 {
21946 /* If we are not processing a template, then nobody should be
21947 providing us with a dependent type. */
21948 gcc_assert (type);
21949 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
21950 return false;
21951 }
21952
21953 /* If the type is NULL, we have not computed a type for the entity
21954 in question; in that case, the type is dependent. */
21955 if (!type)
21956 return true;
21957
21958 /* Erroneous types can be considered non-dependent. */
21959 if (type == error_mark_node)
21960 return false;
21961
21962 /* If we have not already computed the appropriate value for TYPE,
21963 do so now. */
21964 if (!TYPE_DEPENDENT_P_VALID (type))
21965 {
21966 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
21967 TYPE_DEPENDENT_P_VALID (type) = 1;
21968 }
21969
21970 return TYPE_DEPENDENT_P (type);
21971 }
21972
21973 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21974 lookup. In other words, a dependent type that is not the current
21975 instantiation. */
21976
21977 bool
21978 dependent_scope_p (tree scope)
21979 {
21980 return (scope && TYPE_P (scope) && dependent_type_p (scope)
21981 && !currently_open_class (scope));
21982 }
21983
21984 /* T is a SCOPE_REF; return whether we need to consider it
21985 instantiation-dependent so that we can check access at instantiation
21986 time even though we know which member it resolves to. */
21987
21988 static bool
21989 instantiation_dependent_scope_ref_p (tree t)
21990 {
21991 if (DECL_P (TREE_OPERAND (t, 1))
21992 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
21993 && accessible_in_template_p (TREE_OPERAND (t, 0),
21994 TREE_OPERAND (t, 1)))
21995 return false;
21996 else
21997 return true;
21998 }
21999
22000 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22001 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22002 expression. */
22003
22004 /* Note that this predicate is not appropriate for general expressions;
22005 only constant expressions (that satisfy potential_constant_expression)
22006 can be tested for value dependence. */
22007
22008 bool
22009 value_dependent_expression_p (tree expression)
22010 {
22011 if (!processing_template_decl)
22012 return false;
22013
22014 /* A name declared with a dependent type. */
22015 if (DECL_P (expression) && type_dependent_expression_p (expression))
22016 return true;
22017
22018 switch (TREE_CODE (expression))
22019 {
22020 case IDENTIFIER_NODE:
22021 /* A name that has not been looked up -- must be dependent. */
22022 return true;
22023
22024 case TEMPLATE_PARM_INDEX:
22025 /* A non-type template parm. */
22026 return true;
22027
22028 case CONST_DECL:
22029 /* A non-type template parm. */
22030 if (DECL_TEMPLATE_PARM_P (expression))
22031 return true;
22032 return value_dependent_expression_p (DECL_INITIAL (expression));
22033
22034 case VAR_DECL:
22035 /* A constant with literal type and is initialized
22036 with an expression that is value-dependent.
22037
22038 Note that a non-dependent parenthesized initializer will have
22039 already been replaced with its constant value, so if we see
22040 a TREE_LIST it must be dependent. */
22041 if (DECL_INITIAL (expression)
22042 && decl_constant_var_p (expression)
22043 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
22044 /* cp_finish_decl doesn't fold reference initializers. */
22045 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
22046 || value_dependent_expression_p (DECL_INITIAL (expression))))
22047 return true;
22048 return false;
22049
22050 case DYNAMIC_CAST_EXPR:
22051 case STATIC_CAST_EXPR:
22052 case CONST_CAST_EXPR:
22053 case REINTERPRET_CAST_EXPR:
22054 case CAST_EXPR:
22055 /* These expressions are value-dependent if the type to which
22056 the cast occurs is dependent or the expression being casted
22057 is value-dependent. */
22058 {
22059 tree type = TREE_TYPE (expression);
22060
22061 if (dependent_type_p (type))
22062 return true;
22063
22064 /* A functional cast has a list of operands. */
22065 expression = TREE_OPERAND (expression, 0);
22066 if (!expression)
22067 {
22068 /* If there are no operands, it must be an expression such
22069 as "int()". This should not happen for aggregate types
22070 because it would form non-constant expressions. */
22071 gcc_assert (cxx_dialect >= cxx11
22072 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
22073
22074 return false;
22075 }
22076
22077 if (TREE_CODE (expression) == TREE_LIST)
22078 return any_value_dependent_elements_p (expression);
22079
22080 return value_dependent_expression_p (expression);
22081 }
22082
22083 case SIZEOF_EXPR:
22084 if (SIZEOF_EXPR_TYPE_P (expression))
22085 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
22086 /* FALLTHRU */
22087 case ALIGNOF_EXPR:
22088 case TYPEID_EXPR:
22089 /* A `sizeof' expression is value-dependent if the operand is
22090 type-dependent or is a pack expansion. */
22091 expression = TREE_OPERAND (expression, 0);
22092 if (PACK_EXPANSION_P (expression))
22093 return true;
22094 else if (TYPE_P (expression))
22095 return dependent_type_p (expression);
22096 return instantiation_dependent_expression_p (expression);
22097
22098 case AT_ENCODE_EXPR:
22099 /* An 'encode' expression is value-dependent if the operand is
22100 type-dependent. */
22101 expression = TREE_OPERAND (expression, 0);
22102 return dependent_type_p (expression);
22103
22104 case NOEXCEPT_EXPR:
22105 expression = TREE_OPERAND (expression, 0);
22106 return instantiation_dependent_expression_p (expression);
22107
22108 case SCOPE_REF:
22109 /* All instantiation-dependent expressions should also be considered
22110 value-dependent. */
22111 return instantiation_dependent_scope_ref_p (expression);
22112
22113 case COMPONENT_REF:
22114 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
22115 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
22116
22117 case NONTYPE_ARGUMENT_PACK:
22118 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
22119 is value-dependent. */
22120 {
22121 tree values = ARGUMENT_PACK_ARGS (expression);
22122 int i, len = TREE_VEC_LENGTH (values);
22123
22124 for (i = 0; i < len; ++i)
22125 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
22126 return true;
22127
22128 return false;
22129 }
22130
22131 case TRAIT_EXPR:
22132 {
22133 tree type2 = TRAIT_EXPR_TYPE2 (expression);
22134 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
22135 || (type2 ? dependent_type_p (type2) : false));
22136 }
22137
22138 case MODOP_EXPR:
22139 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22140 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
22141
22142 case ARRAY_REF:
22143 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
22144 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
22145
22146 case ADDR_EXPR:
22147 {
22148 tree op = TREE_OPERAND (expression, 0);
22149 return (value_dependent_expression_p (op)
22150 || has_value_dependent_address (op));
22151 }
22152
22153 case REQUIRES_EXPR:
22154 /* Treat all requires-expressions as value-dependent so
22155 we don't try to fold them. */
22156 return true;
22157
22158 case TYPE_REQ:
22159 return dependent_type_p (TREE_OPERAND (expression, 0));
22160
22161 case CALL_EXPR:
22162 {
22163 tree fn = get_callee_fndecl (expression);
22164 int i, nargs;
22165 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
22166 return true;
22167 nargs = call_expr_nargs (expression);
22168 for (i = 0; i < nargs; ++i)
22169 {
22170 tree op = CALL_EXPR_ARG (expression, i);
22171 /* In a call to a constexpr member function, look through the
22172 implicit ADDR_EXPR on the object argument so that it doesn't
22173 cause the call to be considered value-dependent. We also
22174 look through it in potential_constant_expression. */
22175 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
22176 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
22177 && TREE_CODE (op) == ADDR_EXPR)
22178 op = TREE_OPERAND (op, 0);
22179 if (value_dependent_expression_p (op))
22180 return true;
22181 }
22182 return false;
22183 }
22184
22185 case TEMPLATE_ID_EXPR:
22186 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
22187 type-dependent. */
22188 return type_dependent_expression_p (expression)
22189 || variable_concept_p (TREE_OPERAND (expression, 0));
22190
22191 case CONSTRUCTOR:
22192 {
22193 unsigned ix;
22194 tree val;
22195 if (dependent_type_p (TREE_TYPE (expression)))
22196 return true;
22197 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
22198 if (value_dependent_expression_p (val))
22199 return true;
22200 return false;
22201 }
22202
22203 case STMT_EXPR:
22204 /* Treat a GNU statement expression as dependent to avoid crashing
22205 under instantiate_non_dependent_expr; it can't be constant. */
22206 return true;
22207
22208 default:
22209 /* A constant expression is value-dependent if any subexpression is
22210 value-dependent. */
22211 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
22212 {
22213 case tcc_reference:
22214 case tcc_unary:
22215 case tcc_comparison:
22216 case tcc_binary:
22217 case tcc_expression:
22218 case tcc_vl_exp:
22219 {
22220 int i, len = cp_tree_operand_length (expression);
22221
22222 for (i = 0; i < len; i++)
22223 {
22224 tree t = TREE_OPERAND (expression, i);
22225
22226 /* In some cases, some of the operands may be missing.l
22227 (For example, in the case of PREDECREMENT_EXPR, the
22228 amount to increment by may be missing.) That doesn't
22229 make the expression dependent. */
22230 if (t && value_dependent_expression_p (t))
22231 return true;
22232 }
22233 }
22234 break;
22235 default:
22236 break;
22237 }
22238 break;
22239 }
22240
22241 /* The expression is not value-dependent. */
22242 return false;
22243 }
22244
22245 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
22246 [temp.dep.expr]. Note that an expression with no type is
22247 considered dependent. Other parts of the compiler arrange for an
22248 expression with type-dependent subexpressions to have no type, so
22249 this function doesn't have to be fully recursive. */
22250
22251 bool
22252 type_dependent_expression_p (tree expression)
22253 {
22254 if (!processing_template_decl)
22255 return false;
22256
22257 if (expression == NULL_TREE || expression == error_mark_node)
22258 return false;
22259
22260 /* An unresolved name is always dependent. */
22261 if (identifier_p (expression)
22262 || TREE_CODE (expression) == USING_DECL
22263 || TREE_CODE (expression) == WILDCARD_DECL)
22264 return true;
22265
22266 /* A fold expression is type-dependent. */
22267 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
22268 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
22269 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
22270 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
22271 return true;
22272
22273 /* Some expression forms are never type-dependent. */
22274 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
22275 || TREE_CODE (expression) == SIZEOF_EXPR
22276 || TREE_CODE (expression) == ALIGNOF_EXPR
22277 || TREE_CODE (expression) == AT_ENCODE_EXPR
22278 || TREE_CODE (expression) == NOEXCEPT_EXPR
22279 || TREE_CODE (expression) == TRAIT_EXPR
22280 || TREE_CODE (expression) == TYPEID_EXPR
22281 || TREE_CODE (expression) == DELETE_EXPR
22282 || TREE_CODE (expression) == VEC_DELETE_EXPR
22283 || TREE_CODE (expression) == THROW_EXPR
22284 || TREE_CODE (expression) == REQUIRES_EXPR)
22285 return false;
22286
22287 /* The types of these expressions depends only on the type to which
22288 the cast occurs. */
22289 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
22290 || TREE_CODE (expression) == STATIC_CAST_EXPR
22291 || TREE_CODE (expression) == CONST_CAST_EXPR
22292 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
22293 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
22294 || TREE_CODE (expression) == CAST_EXPR)
22295 return dependent_type_p (TREE_TYPE (expression));
22296
22297 /* The types of these expressions depends only on the type created
22298 by the expression. */
22299 if (TREE_CODE (expression) == NEW_EXPR
22300 || TREE_CODE (expression) == VEC_NEW_EXPR)
22301 {
22302 /* For NEW_EXPR tree nodes created inside a template, either
22303 the object type itself or a TREE_LIST may appear as the
22304 operand 1. */
22305 tree type = TREE_OPERAND (expression, 1);
22306 if (TREE_CODE (type) == TREE_LIST)
22307 /* This is an array type. We need to check array dimensions
22308 as well. */
22309 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
22310 || value_dependent_expression_p
22311 (TREE_OPERAND (TREE_VALUE (type), 1));
22312 else
22313 return dependent_type_p (type);
22314 }
22315
22316 if (TREE_CODE (expression) == SCOPE_REF)
22317 {
22318 tree scope = TREE_OPERAND (expression, 0);
22319 tree name = TREE_OPERAND (expression, 1);
22320
22321 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
22322 contains an identifier associated by name lookup with one or more
22323 declarations declared with a dependent type, or...a
22324 nested-name-specifier or qualified-id that names a member of an
22325 unknown specialization. */
22326 return (type_dependent_expression_p (name)
22327 || dependent_scope_p (scope));
22328 }
22329
22330 if (TREE_CODE (expression) == FUNCTION_DECL
22331 && DECL_LANG_SPECIFIC (expression)
22332 && DECL_TEMPLATE_INFO (expression)
22333 && (any_dependent_template_arguments_p
22334 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
22335 return true;
22336
22337 if (TREE_CODE (expression) == TEMPLATE_DECL
22338 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
22339 return false;
22340
22341 if (TREE_CODE (expression) == STMT_EXPR)
22342 expression = stmt_expr_value_expr (expression);
22343
22344 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
22345 {
22346 tree elt;
22347 unsigned i;
22348
22349 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
22350 {
22351 if (type_dependent_expression_p (elt))
22352 return true;
22353 }
22354 return false;
22355 }
22356
22357 /* A static data member of the current instantiation with incomplete
22358 array type is type-dependent, as the definition and specializations
22359 can have different bounds. */
22360 if (VAR_P (expression)
22361 && DECL_CLASS_SCOPE_P (expression)
22362 && dependent_type_p (DECL_CONTEXT (expression))
22363 && VAR_HAD_UNKNOWN_BOUND (expression))
22364 return true;
22365
22366 /* An array of unknown bound depending on a variadic parameter, eg:
22367
22368 template<typename... Args>
22369 void foo (Args... args)
22370 {
22371 int arr[] = { args... };
22372 }
22373
22374 template<int... vals>
22375 void bar ()
22376 {
22377 int arr[] = { vals... };
22378 }
22379
22380 If the array has no length and has an initializer, it must be that
22381 we couldn't determine its length in cp_complete_array_type because
22382 it is dependent. */
22383 if (VAR_P (expression)
22384 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
22385 && !TYPE_DOMAIN (TREE_TYPE (expression))
22386 && DECL_INITIAL (expression))
22387 return true;
22388
22389 /* A variable template specialization is type-dependent if it has any
22390 dependent template arguments. */
22391 if (VAR_P (expression)
22392 && DECL_LANG_SPECIFIC (expression)
22393 && DECL_TEMPLATE_INFO (expression)
22394 && variable_template_p (DECL_TI_TEMPLATE (expression)))
22395 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
22396
22397 /* Always dependent, on the number of arguments if nothing else. */
22398 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
22399 return true;
22400
22401 if (TREE_TYPE (expression) == unknown_type_node)
22402 {
22403 if (TREE_CODE (expression) == ADDR_EXPR)
22404 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
22405 if (TREE_CODE (expression) == COMPONENT_REF
22406 || TREE_CODE (expression) == OFFSET_REF)
22407 {
22408 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
22409 return true;
22410 expression = TREE_OPERAND (expression, 1);
22411 if (identifier_p (expression))
22412 return false;
22413 }
22414 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
22415 if (TREE_CODE (expression) == SCOPE_REF)
22416 return false;
22417
22418 if (BASELINK_P (expression))
22419 {
22420 if (BASELINK_OPTYPE (expression)
22421 && dependent_type_p (BASELINK_OPTYPE (expression)))
22422 return true;
22423 expression = BASELINK_FUNCTIONS (expression);
22424 }
22425
22426 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
22427 {
22428 if (any_dependent_template_arguments_p
22429 (TREE_OPERAND (expression, 1)))
22430 return true;
22431 expression = TREE_OPERAND (expression, 0);
22432 if (identifier_p (expression))
22433 return true;
22434 }
22435
22436 gcc_assert (TREE_CODE (expression) == OVERLOAD
22437 || TREE_CODE (expression) == FUNCTION_DECL);
22438
22439 while (expression)
22440 {
22441 if (type_dependent_expression_p (OVL_CURRENT (expression)))
22442 return true;
22443 expression = OVL_NEXT (expression);
22444 }
22445 return false;
22446 }
22447
22448 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
22449
22450 return (dependent_type_p (TREE_TYPE (expression)));
22451 }
22452
22453 /* walk_tree callback function for instantiation_dependent_expression_p,
22454 below. Returns non-zero if a dependent subexpression is found. */
22455
22456 static tree
22457 instantiation_dependent_r (tree *tp, int *walk_subtrees,
22458 void * /*data*/)
22459 {
22460 if (TYPE_P (*tp))
22461 {
22462 /* We don't have to worry about decltype currently because decltype
22463 of an instantiation-dependent expr is a dependent type. This
22464 might change depending on the resolution of DR 1172. */
22465 *walk_subtrees = false;
22466 return NULL_TREE;
22467 }
22468 enum tree_code code = TREE_CODE (*tp);
22469 switch (code)
22470 {
22471 /* Don't treat an argument list as dependent just because it has no
22472 TREE_TYPE. */
22473 case TREE_LIST:
22474 case TREE_VEC:
22475 return NULL_TREE;
22476
22477 case VAR_DECL:
22478 case CONST_DECL:
22479 /* A constant with a dependent initializer is dependent. */
22480 if (value_dependent_expression_p (*tp))
22481 return *tp;
22482 break;
22483
22484 case TEMPLATE_PARM_INDEX:
22485 return *tp;
22486
22487 /* Handle expressions with type operands. */
22488 case SIZEOF_EXPR:
22489 case ALIGNOF_EXPR:
22490 case TYPEID_EXPR:
22491 case AT_ENCODE_EXPR:
22492 {
22493 tree op = TREE_OPERAND (*tp, 0);
22494 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
22495 op = TREE_TYPE (op);
22496 if (TYPE_P (op))
22497 {
22498 if (dependent_type_p (op))
22499 return *tp;
22500 else
22501 {
22502 *walk_subtrees = false;
22503 return NULL_TREE;
22504 }
22505 }
22506 break;
22507 }
22508
22509 case TRAIT_EXPR:
22510 if (value_dependent_expression_p (*tp))
22511 return *tp;
22512 *walk_subtrees = false;
22513 return NULL_TREE;
22514
22515 case COMPONENT_REF:
22516 if (identifier_p (TREE_OPERAND (*tp, 1)))
22517 /* In a template, finish_class_member_access_expr creates a
22518 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
22519 type-dependent, so that we can check access control at
22520 instantiation time (PR 42277). See also Core issue 1273. */
22521 return *tp;
22522 break;
22523
22524 case SCOPE_REF:
22525 if (instantiation_dependent_scope_ref_p (*tp))
22526 return *tp;
22527 else
22528 break;
22529
22530 /* Treat statement-expressions as dependent. */
22531 case BIND_EXPR:
22532 return *tp;
22533
22534 /* Treat requires-expressions as dependent. */
22535 case REQUIRES_EXPR:
22536 return *tp;
22537
22538 case CALL_EXPR:
22539 /* Treat calls to function concepts as dependent. */
22540 if (function_concept_check_p (*tp))
22541 return *tp;
22542 break;
22543
22544 case TEMPLATE_ID_EXPR:
22545 /* And variable concepts. */
22546 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
22547 return *tp;
22548 break;
22549
22550 default:
22551 break;
22552 }
22553
22554 if (type_dependent_expression_p (*tp))
22555 return *tp;
22556 else
22557 return NULL_TREE;
22558 }
22559
22560 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
22561 sense defined by the ABI:
22562
22563 "An expression is instantiation-dependent if it is type-dependent
22564 or value-dependent, or it has a subexpression that is type-dependent
22565 or value-dependent." */
22566
22567 bool
22568 instantiation_dependent_expression_p (tree expression)
22569 {
22570 tree result;
22571
22572 if (!processing_template_decl)
22573 return false;
22574
22575 if (expression == error_mark_node)
22576 return false;
22577
22578 result = cp_walk_tree_without_duplicates (&expression,
22579 instantiation_dependent_r, NULL);
22580 return result != NULL_TREE;
22581 }
22582
22583 /* Like type_dependent_expression_p, but it also works while not processing
22584 a template definition, i.e. during substitution or mangling. */
22585
22586 bool
22587 type_dependent_expression_p_push (tree expr)
22588 {
22589 bool b;
22590 ++processing_template_decl;
22591 b = type_dependent_expression_p (expr);
22592 --processing_template_decl;
22593 return b;
22594 }
22595
22596 /* Returns TRUE if ARGS contains a type-dependent expression. */
22597
22598 bool
22599 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
22600 {
22601 unsigned int i;
22602 tree arg;
22603
22604 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
22605 {
22606 if (type_dependent_expression_p (arg))
22607 return true;
22608 }
22609 return false;
22610 }
22611
22612 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
22613 expressions) contains any type-dependent expressions. */
22614
22615 bool
22616 any_type_dependent_elements_p (const_tree list)
22617 {
22618 for (; list; list = TREE_CHAIN (list))
22619 if (type_dependent_expression_p (TREE_VALUE (list)))
22620 return true;
22621
22622 return false;
22623 }
22624
22625 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
22626 expressions) contains any value-dependent expressions. */
22627
22628 bool
22629 any_value_dependent_elements_p (const_tree list)
22630 {
22631 for (; list; list = TREE_CHAIN (list))
22632 if (value_dependent_expression_p (TREE_VALUE (list)))
22633 return true;
22634
22635 return false;
22636 }
22637
22638 /* Returns TRUE if the ARG (a template argument) is dependent. */
22639
22640 bool
22641 dependent_template_arg_p (tree arg)
22642 {
22643 if (!processing_template_decl)
22644 return false;
22645
22646 /* Assume a template argument that was wrongly written by the user
22647 is dependent. This is consistent with what
22648 any_dependent_template_arguments_p [that calls this function]
22649 does. */
22650 if (!arg || arg == error_mark_node)
22651 return true;
22652
22653 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
22654 arg = ARGUMENT_PACK_SELECT_ARG (arg);
22655
22656 if (TREE_CODE (arg) == TEMPLATE_DECL
22657 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
22658 return dependent_template_p (arg);
22659 else if (ARGUMENT_PACK_P (arg))
22660 {
22661 tree args = ARGUMENT_PACK_ARGS (arg);
22662 int i, len = TREE_VEC_LENGTH (args);
22663 for (i = 0; i < len; ++i)
22664 {
22665 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22666 return true;
22667 }
22668
22669 return false;
22670 }
22671 else if (TYPE_P (arg))
22672 return dependent_type_p (arg);
22673 else
22674 return (type_dependent_expression_p (arg)
22675 || value_dependent_expression_p (arg));
22676 }
22677
22678 /* Returns true if ARGS (a collection of template arguments) contains
22679 any types that require structural equality testing. */
22680
22681 bool
22682 any_template_arguments_need_structural_equality_p (tree args)
22683 {
22684 int i;
22685 int j;
22686
22687 if (!args)
22688 return false;
22689 if (args == error_mark_node)
22690 return true;
22691
22692 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
22693 {
22694 tree level = TMPL_ARGS_LEVEL (args, i + 1);
22695 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
22696 {
22697 tree arg = TREE_VEC_ELT (level, j);
22698 tree packed_args = NULL_TREE;
22699 int k, len = 1;
22700
22701 if (ARGUMENT_PACK_P (arg))
22702 {
22703 /* Look inside the argument pack. */
22704 packed_args = ARGUMENT_PACK_ARGS (arg);
22705 len = TREE_VEC_LENGTH (packed_args);
22706 }
22707
22708 for (k = 0; k < len; ++k)
22709 {
22710 if (packed_args)
22711 arg = TREE_VEC_ELT (packed_args, k);
22712
22713 if (error_operand_p (arg))
22714 return true;
22715 else if (TREE_CODE (arg) == TEMPLATE_DECL)
22716 continue;
22717 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
22718 return true;
22719 else if (!TYPE_P (arg) && TREE_TYPE (arg)
22720 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
22721 return true;
22722 }
22723 }
22724 }
22725
22726 return false;
22727 }
22728
22729 /* Returns true if ARGS (a collection of template arguments) contains
22730 any dependent arguments. */
22731
22732 bool
22733 any_dependent_template_arguments_p (const_tree args)
22734 {
22735 int i;
22736 int j;
22737
22738 if (!args)
22739 return false;
22740 if (args == error_mark_node)
22741 return true;
22742
22743 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
22744 {
22745 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
22746 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
22747 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
22748 return true;
22749 }
22750
22751 return false;
22752 }
22753
22754 /* Returns TRUE if the template TMPL is dependent. */
22755
22756 bool
22757 dependent_template_p (tree tmpl)
22758 {
22759 if (TREE_CODE (tmpl) == OVERLOAD)
22760 {
22761 while (tmpl)
22762 {
22763 if (dependent_template_p (OVL_CURRENT (tmpl)))
22764 return true;
22765 tmpl = OVL_NEXT (tmpl);
22766 }
22767 return false;
22768 }
22769
22770 /* Template template parameters are dependent. */
22771 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
22772 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
22773 return true;
22774 /* So are names that have not been looked up. */
22775 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
22776 return true;
22777 /* So are member templates of dependent classes. */
22778 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
22779 return dependent_type_p (DECL_CONTEXT (tmpl));
22780 return false;
22781 }
22782
22783 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
22784
22785 bool
22786 dependent_template_id_p (tree tmpl, tree args)
22787 {
22788 return (dependent_template_p (tmpl)
22789 || any_dependent_template_arguments_p (args));
22790 }
22791
22792 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
22793 is dependent. */
22794
22795 bool
22796 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
22797 {
22798 int i;
22799
22800 if (!processing_template_decl)
22801 return false;
22802
22803 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
22804 {
22805 tree decl = TREE_VEC_ELT (declv, i);
22806 tree init = TREE_VEC_ELT (initv, i);
22807 tree cond = TREE_VEC_ELT (condv, i);
22808 tree incr = TREE_VEC_ELT (incrv, i);
22809
22810 if (type_dependent_expression_p (decl))
22811 return true;
22812
22813 if (init && type_dependent_expression_p (init))
22814 return true;
22815
22816 if (type_dependent_expression_p (cond))
22817 return true;
22818
22819 if (COMPARISON_CLASS_P (cond)
22820 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
22821 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
22822 return true;
22823
22824 if (TREE_CODE (incr) == MODOP_EXPR)
22825 {
22826 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
22827 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
22828 return true;
22829 }
22830 else if (type_dependent_expression_p (incr))
22831 return true;
22832 else if (TREE_CODE (incr) == MODIFY_EXPR)
22833 {
22834 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
22835 return true;
22836 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
22837 {
22838 tree t = TREE_OPERAND (incr, 1);
22839 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
22840 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
22841 return true;
22842 }
22843 }
22844 }
22845
22846 return false;
22847 }
22848
22849 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
22850 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
22851 no such TYPE can be found. Note that this function peers inside
22852 uninstantiated templates and therefore should be used only in
22853 extremely limited situations. ONLY_CURRENT_P restricts this
22854 peering to the currently open classes hierarchy (which is required
22855 when comparing types). */
22856
22857 tree
22858 resolve_typename_type (tree type, bool only_current_p)
22859 {
22860 tree scope;
22861 tree name;
22862 tree decl;
22863 int quals;
22864 tree pushed_scope;
22865 tree result;
22866
22867 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
22868
22869 scope = TYPE_CONTEXT (type);
22870 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
22871 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
22872 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
22873 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
22874 identifier of the TYPENAME_TYPE anymore.
22875 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
22876 TYPENAME_TYPE instead, we avoid messing up with a possible
22877 typedef variant case. */
22878 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
22879
22880 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
22881 it first before we can figure out what NAME refers to. */
22882 if (TREE_CODE (scope) == TYPENAME_TYPE)
22883 {
22884 if (TYPENAME_IS_RESOLVING_P (scope))
22885 /* Given a class template A with a dependent base with nested type C,
22886 typedef typename A::C::C C will land us here, as trying to resolve
22887 the initial A::C leads to the local C typedef, which leads back to
22888 A::C::C. So we break the recursion now. */
22889 return type;
22890 else
22891 scope = resolve_typename_type (scope, only_current_p);
22892 }
22893 /* If we don't know what SCOPE refers to, then we cannot resolve the
22894 TYPENAME_TYPE. */
22895 if (TREE_CODE (scope) == TYPENAME_TYPE)
22896 return type;
22897 /* If the SCOPE is a template type parameter, we have no way of
22898 resolving the name. */
22899 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
22900 return type;
22901 /* If the SCOPE is not the current instantiation, there's no reason
22902 to look inside it. */
22903 if (only_current_p && !currently_open_class (scope))
22904 return type;
22905 /* If this is a typedef, we don't want to look inside (c++/11987). */
22906 if (typedef_variant_p (type))
22907 return type;
22908 /* If SCOPE isn't the template itself, it will not have a valid
22909 TYPE_FIELDS list. */
22910 if (CLASS_TYPE_P (scope)
22911 && same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
22912 /* scope is either the template itself or a compatible instantiation
22913 like X<T>, so look up the name in the original template. */
22914 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
22915 else
22916 /* scope is a partial instantiation, so we can't do the lookup or we
22917 will lose the template arguments. */
22918 return type;
22919 /* Enter the SCOPE so that name lookup will be resolved as if we
22920 were in the class definition. In particular, SCOPE will no
22921 longer be considered a dependent type. */
22922 pushed_scope = push_scope (scope);
22923 /* Look up the declaration. */
22924 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
22925 tf_warning_or_error);
22926
22927 result = NULL_TREE;
22928
22929 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
22930 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
22931 if (!decl)
22932 /*nop*/;
22933 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
22934 && TREE_CODE (decl) == TYPE_DECL)
22935 {
22936 result = TREE_TYPE (decl);
22937 if (result == error_mark_node)
22938 result = NULL_TREE;
22939 }
22940 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
22941 && DECL_CLASS_TEMPLATE_P (decl))
22942 {
22943 tree tmpl;
22944 tree args;
22945 /* Obtain the template and the arguments. */
22946 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
22947 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
22948 /* Instantiate the template. */
22949 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
22950 /*entering_scope=*/0,
22951 tf_error | tf_user);
22952 if (result == error_mark_node)
22953 result = NULL_TREE;
22954 }
22955
22956 /* Leave the SCOPE. */
22957 if (pushed_scope)
22958 pop_scope (pushed_scope);
22959
22960 /* If we failed to resolve it, return the original typename. */
22961 if (!result)
22962 return type;
22963
22964 /* If lookup found a typename type, resolve that too. */
22965 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
22966 {
22967 /* Ill-formed programs can cause infinite recursion here, so we
22968 must catch that. */
22969 TYPENAME_IS_RESOLVING_P (type) = 1;
22970 result = resolve_typename_type (result, only_current_p);
22971 TYPENAME_IS_RESOLVING_P (type) = 0;
22972 }
22973
22974 /* Qualify the resulting type. */
22975 quals = cp_type_quals (type);
22976 if (quals)
22977 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
22978
22979 return result;
22980 }
22981
22982 /* EXPR is an expression which is not type-dependent. Return a proxy
22983 for EXPR that can be used to compute the types of larger
22984 expressions containing EXPR. */
22985
22986 tree
22987 build_non_dependent_expr (tree expr)
22988 {
22989 tree inner_expr;
22990
22991 #ifdef ENABLE_CHECKING
22992 /* Try to get a constant value for all non-dependent expressions in
22993 order to expose bugs in *_dependent_expression_p and constexpr. */
22994 if (cxx_dialect >= cxx11)
22995 fold_non_dependent_expr (expr);
22996 #endif
22997
22998 /* Preserve OVERLOADs; the functions must be available to resolve
22999 types. */
23000 inner_expr = expr;
23001 if (TREE_CODE (inner_expr) == STMT_EXPR)
23002 inner_expr = stmt_expr_value_expr (inner_expr);
23003 if (TREE_CODE (inner_expr) == ADDR_EXPR)
23004 inner_expr = TREE_OPERAND (inner_expr, 0);
23005 if (TREE_CODE (inner_expr) == COMPONENT_REF)
23006 inner_expr = TREE_OPERAND (inner_expr, 1);
23007 if (is_overloaded_fn (inner_expr)
23008 || TREE_CODE (inner_expr) == OFFSET_REF)
23009 return expr;
23010 /* There is no need to return a proxy for a variable. */
23011 if (VAR_P (expr))
23012 return expr;
23013 /* Preserve string constants; conversions from string constants to
23014 "char *" are allowed, even though normally a "const char *"
23015 cannot be used to initialize a "char *". */
23016 if (TREE_CODE (expr) == STRING_CST)
23017 return expr;
23018 /* Preserve void and arithmetic constants, as an optimization -- there is no
23019 reason to create a new node. */
23020 if (TREE_CODE (expr) == VOID_CST
23021 || TREE_CODE (expr) == INTEGER_CST
23022 || TREE_CODE (expr) == REAL_CST)
23023 return expr;
23024 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
23025 There is at least one place where we want to know that a
23026 particular expression is a throw-expression: when checking a ?:
23027 expression, there are special rules if the second or third
23028 argument is a throw-expression. */
23029 if (TREE_CODE (expr) == THROW_EXPR)
23030 return expr;
23031
23032 /* Don't wrap an initializer list, we need to be able to look inside. */
23033 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
23034 return expr;
23035
23036 /* Don't wrap a dummy object, we need to be able to test for it. */
23037 if (is_dummy_object (expr))
23038 return expr;
23039
23040 if (TREE_CODE (expr) == COND_EXPR)
23041 return build3 (COND_EXPR,
23042 TREE_TYPE (expr),
23043 TREE_OPERAND (expr, 0),
23044 (TREE_OPERAND (expr, 1)
23045 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
23046 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
23047 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
23048 if (TREE_CODE (expr) == COMPOUND_EXPR
23049 && !COMPOUND_EXPR_OVERLOADED (expr))
23050 return build2 (COMPOUND_EXPR,
23051 TREE_TYPE (expr),
23052 TREE_OPERAND (expr, 0),
23053 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
23054
23055 /* If the type is unknown, it can't really be non-dependent */
23056 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
23057
23058 /* Otherwise, build a NON_DEPENDENT_EXPR. */
23059 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
23060 }
23061
23062 /* ARGS is a vector of expressions as arguments to a function call.
23063 Replace the arguments with equivalent non-dependent expressions.
23064 This modifies ARGS in place. */
23065
23066 void
23067 make_args_non_dependent (vec<tree, va_gc> *args)
23068 {
23069 unsigned int ix;
23070 tree arg;
23071
23072 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
23073 {
23074 tree newarg = build_non_dependent_expr (arg);
23075 if (newarg != arg)
23076 (*args)[ix] = newarg;
23077 }
23078 }
23079
23080 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
23081 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
23082 parms. */
23083
23084 static tree
23085 make_auto_1 (tree name)
23086 {
23087 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
23088 TYPE_NAME (au) = build_decl (input_location,
23089 TYPE_DECL, name, au);
23090 TYPE_STUB_DECL (au) = TYPE_NAME (au);
23091 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
23092 (0, processing_template_decl + 1, processing_template_decl + 1,
23093 TYPE_NAME (au), NULL_TREE);
23094 TYPE_CANONICAL (au) = canonical_type_parameter (au);
23095 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
23096 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
23097
23098 return au;
23099 }
23100
23101 tree
23102 make_decltype_auto (void)
23103 {
23104 return make_auto_1 (get_identifier ("decltype(auto)"));
23105 }
23106
23107 tree
23108 make_auto (void)
23109 {
23110 return make_auto_1 (get_identifier ("auto"));
23111 }
23112
23113 /* Given type ARG, return std::initializer_list<ARG>. */
23114
23115 static tree
23116 listify (tree arg)
23117 {
23118 tree std_init_list = namespace_binding
23119 (get_identifier ("initializer_list"), std_node);
23120 tree argvec;
23121 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
23122 {
23123 error ("deducing from brace-enclosed initializer list requires "
23124 "#include <initializer_list>");
23125 return error_mark_node;
23126 }
23127 argvec = make_tree_vec (1);
23128 TREE_VEC_ELT (argvec, 0) = arg;
23129 return lookup_template_class (std_init_list, argvec, NULL_TREE,
23130 NULL_TREE, 0, tf_warning_or_error);
23131 }
23132
23133 /* Replace auto in TYPE with std::initializer_list<auto>. */
23134
23135 static tree
23136 listify_autos (tree type, tree auto_node)
23137 {
23138 tree init_auto = listify (auto_node);
23139 tree argvec = make_tree_vec (1);
23140 TREE_VEC_ELT (argvec, 0) = init_auto;
23141 if (processing_template_decl)
23142 argvec = add_to_template_args (current_template_args (), argvec);
23143 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
23144 }
23145
23146 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23147 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
23148
23149 tree
23150 do_auto_deduction (tree type, tree init, tree auto_node)
23151 {
23152 return do_auto_deduction (type, init, auto_node,
23153 tf_warning_or_error,
23154 adc_unspecified);
23155 }
23156
23157 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
23158 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
23159 The CONTEXT determines the context in which auto deduction is performed
23160 and is used to control error diagnostics. */
23161
23162 tree
23163 do_auto_deduction (tree type, tree init, tree auto_node,
23164 tsubst_flags_t complain, auto_deduction_context context)
23165 {
23166 tree targs;
23167
23168 if (init == error_mark_node)
23169 return error_mark_node;
23170
23171 if (type_dependent_expression_p (init))
23172 /* Defining a subset of type-dependent expressions that we can deduce
23173 from ahead of time isn't worth the trouble. */
23174 return type;
23175
23176 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
23177 with either a new invented type template parameter U or, if the
23178 initializer is a braced-init-list (8.5.4), with
23179 std::initializer_list<U>. */
23180 if (BRACE_ENCLOSED_INITIALIZER_P (init))
23181 {
23182 if (!DIRECT_LIST_INIT_P (init))
23183 type = listify_autos (type, auto_node);
23184 else if (CONSTRUCTOR_NELTS (init) == 1)
23185 init = CONSTRUCTOR_ELT (init, 0)->value;
23186 else
23187 {
23188 if (complain & tf_warning_or_error)
23189 {
23190 if (permerror (input_location, "direct-list-initialization of "
23191 "%<auto%> requires exactly one element"))
23192 inform (input_location,
23193 "for deduction to %<std::initializer_list%>, use copy-"
23194 "list-initialization (i.e. add %<=%> before the %<{%>)");
23195 }
23196 type = listify_autos (type, auto_node);
23197 }
23198 }
23199
23200 init = resolve_nondeduced_context (init);
23201
23202 targs = make_tree_vec (1);
23203 if (AUTO_IS_DECLTYPE (auto_node))
23204 {
23205 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
23206 && !REF_PARENTHESIZED_P (init)));
23207 TREE_VEC_ELT (targs, 0)
23208 = finish_decltype_type (init, id, tf_warning_or_error);
23209 if (type != auto_node)
23210 {
23211 if (complain & tf_error)
23212 error ("%qT as type rather than plain %<decltype(auto)%>", type);
23213 return error_mark_node;
23214 }
23215 }
23216 else
23217 {
23218 tree parms = build_tree_list (NULL_TREE, type);
23219 tree tparms = make_tree_vec (1);
23220 int val;
23221
23222 TREE_VEC_ELT (tparms, 0)
23223 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
23224 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
23225 DEDUCE_CALL, LOOKUP_NORMAL,
23226 NULL, /*explain_p=*/false);
23227 if (val > 0)
23228 {
23229 if (processing_template_decl)
23230 /* Try again at instantiation time. */
23231 return type;
23232 if (type && type != error_mark_node
23233 && (complain & tf_error))
23234 /* If type is error_mark_node a diagnostic must have been
23235 emitted by now. Also, having a mention to '<type error>'
23236 in the diagnostic is not really useful to the user. */
23237 {
23238 if (cfun && auto_node == current_function_auto_return_pattern
23239 && LAMBDA_FUNCTION_P (current_function_decl))
23240 error ("unable to deduce lambda return type from %qE", init);
23241 else
23242 error ("unable to deduce %qT from %qE", type, init);
23243 }
23244 return error_mark_node;
23245 }
23246 }
23247
23248 /* If the list of declarators contains more than one declarator, the type
23249 of each declared variable is determined as described above. If the
23250 type deduced for the template parameter U is not the same in each
23251 deduction, the program is ill-formed. */
23252 if (TREE_TYPE (auto_node)
23253 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
23254 {
23255 if (cfun && auto_node == current_function_auto_return_pattern
23256 && LAMBDA_FUNCTION_P (current_function_decl))
23257 error ("inconsistent types %qT and %qT deduced for "
23258 "lambda return type", TREE_TYPE (auto_node),
23259 TREE_VEC_ELT (targs, 0));
23260 else
23261 error ("inconsistent deduction for %qT: %qT and then %qT",
23262 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
23263 return error_mark_node;
23264 }
23265 if (context != adc_requirement)
23266 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
23267
23268 /* Check any placeholder constraints against the deduced type. */
23269 if (flag_concepts && !processing_template_decl)
23270 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
23271 {
23272 /* Use the deduced type to check the associated constraints. */
23273 if (!constraints_satisfied_p (constr, targs))
23274 {
23275 if (complain & tf_warning_or_error)
23276 {
23277 switch (context)
23278 {
23279 case adc_unspecified:
23280 error("placeholder constraints not satisfied");
23281 break;
23282 case adc_variable_type:
23283 error ("deduced initializer does not satisfy "
23284 "placeholder constraints");
23285 break;
23286 case adc_return_type:
23287 error ("deduced return type does not satisfy "
23288 "placeholder constraints");
23289 break;
23290 case adc_requirement:
23291 error ("deduced expression type does not saatisy "
23292 "placeholder constraints");
23293 break;
23294 }
23295 diagnose_constraints (input_location, constr, targs);
23296 }
23297 return error_mark_node;
23298 }
23299 }
23300
23301 if (processing_template_decl)
23302 targs = add_to_template_args (current_template_args (), targs);
23303 return tsubst (type, targs, complain, NULL_TREE);
23304 }
23305
23306 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
23307 result. */
23308
23309 tree
23310 splice_late_return_type (tree type, tree late_return_type)
23311 {
23312 if (is_auto (type))
23313 {
23314 if (late_return_type)
23315 return late_return_type;
23316
23317 tree idx = get_template_parm_index (type);
23318 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
23319 /* In an abbreviated function template we didn't know we were dealing
23320 with a function template when we saw the auto return type, so update
23321 it to have the correct level. */
23322 return make_auto_1 (TYPE_IDENTIFIER (type));
23323 }
23324 return type;
23325 }
23326
23327 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
23328 'decltype(auto)'. */
23329
23330 bool
23331 is_auto (const_tree type)
23332 {
23333 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23334 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
23335 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
23336 return true;
23337 else
23338 return false;
23339 }
23340
23341 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
23342 a use of `auto'. Returns NULL_TREE otherwise. */
23343
23344 tree
23345 type_uses_auto (tree type)
23346 {
23347 return find_type_usage (type, is_auto);
23348 }
23349
23350 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
23351 'decltype(auto)' or a concept. */
23352
23353 bool
23354 is_auto_or_concept (const_tree type)
23355 {
23356 return is_auto (type); // or concept
23357 }
23358
23359 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
23360 a concept identifier) iff TYPE contains a use of a generic type. Returns
23361 NULL_TREE otherwise. */
23362
23363 tree
23364 type_uses_auto_or_concept (tree type)
23365 {
23366 return find_type_usage (type, is_auto_or_concept);
23367 }
23368
23369
23370 /* For a given template T, return the vector of typedefs referenced
23371 in T for which access check is needed at T instantiation time.
23372 T is either a FUNCTION_DECL or a RECORD_TYPE.
23373 Those typedefs were added to T by the function
23374 append_type_to_template_for_access_check. */
23375
23376 vec<qualified_typedef_usage_t, va_gc> *
23377 get_types_needing_access_check (tree t)
23378 {
23379 tree ti;
23380 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
23381
23382 if (!t || t == error_mark_node)
23383 return NULL;
23384
23385 if (!(ti = get_template_info (t)))
23386 return NULL;
23387
23388 if (CLASS_TYPE_P (t)
23389 || TREE_CODE (t) == FUNCTION_DECL)
23390 {
23391 if (!TI_TEMPLATE (ti))
23392 return NULL;
23393
23394 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
23395 }
23396
23397 return result;
23398 }
23399
23400 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
23401 tied to T. That list of typedefs will be access checked at
23402 T instantiation time.
23403 T is either a FUNCTION_DECL or a RECORD_TYPE.
23404 TYPE_DECL is a TYPE_DECL node representing a typedef.
23405 SCOPE is the scope through which TYPE_DECL is accessed.
23406 LOCATION is the location of the usage point of TYPE_DECL.
23407
23408 This function is a subroutine of
23409 append_type_to_template_for_access_check. */
23410
23411 static void
23412 append_type_to_template_for_access_check_1 (tree t,
23413 tree type_decl,
23414 tree scope,
23415 location_t location)
23416 {
23417 qualified_typedef_usage_t typedef_usage;
23418 tree ti;
23419
23420 if (!t || t == error_mark_node)
23421 return;
23422
23423 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
23424 || CLASS_TYPE_P (t))
23425 && type_decl
23426 && TREE_CODE (type_decl) == TYPE_DECL
23427 && scope);
23428
23429 if (!(ti = get_template_info (t)))
23430 return;
23431
23432 gcc_assert (TI_TEMPLATE (ti));
23433
23434 typedef_usage.typedef_decl = type_decl;
23435 typedef_usage.context = scope;
23436 typedef_usage.locus = location;
23437
23438 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
23439 }
23440
23441 /* Append TYPE_DECL to the template TEMPL.
23442 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
23443 At TEMPL instanciation time, TYPE_DECL will be checked to see
23444 if it can be accessed through SCOPE.
23445 LOCATION is the location of the usage point of TYPE_DECL.
23446
23447 e.g. consider the following code snippet:
23448
23449 class C
23450 {
23451 typedef int myint;
23452 };
23453
23454 template<class U> struct S
23455 {
23456 C::myint mi; // <-- usage point of the typedef C::myint
23457 };
23458
23459 S<char> s;
23460
23461 At S<char> instantiation time, we need to check the access of C::myint
23462 In other words, we need to check the access of the myint typedef through
23463 the C scope. For that purpose, this function will add the myint typedef
23464 and the scope C through which its being accessed to a list of typedefs
23465 tied to the template S. That list will be walked at template instantiation
23466 time and access check performed on each typedefs it contains.
23467 Note that this particular code snippet should yield an error because
23468 myint is private to C. */
23469
23470 void
23471 append_type_to_template_for_access_check (tree templ,
23472 tree type_decl,
23473 tree scope,
23474 location_t location)
23475 {
23476 qualified_typedef_usage_t *iter;
23477 unsigned i;
23478
23479 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
23480
23481 /* Make sure we don't append the type to the template twice. */
23482 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
23483 if (iter->typedef_decl == type_decl && scope == iter->context)
23484 return;
23485
23486 append_type_to_template_for_access_check_1 (templ, type_decl,
23487 scope, location);
23488 }
23489
23490 /* Convert the generic type parameters in PARM that match the types given in the
23491 range [START_IDX, END_IDX) from the current_template_parms into generic type
23492 packs. */
23493
23494 tree
23495 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
23496 {
23497 tree current = current_template_parms;
23498 int depth = TMPL_PARMS_DEPTH (current);
23499 current = INNERMOST_TEMPLATE_PARMS (current);
23500 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
23501
23502 for (int i = 0; i < start_idx; ++i)
23503 TREE_VEC_ELT (replacement, i)
23504 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
23505
23506 for (int i = start_idx; i < end_idx; ++i)
23507 {
23508 /* Create a distinct parameter pack type from the current parm and add it
23509 to the replacement args to tsubst below into the generic function
23510 parameter. */
23511
23512 tree o = TREE_TYPE (TREE_VALUE
23513 (TREE_VEC_ELT (current, i)));
23514 tree t = copy_type (o);
23515 TEMPLATE_TYPE_PARM_INDEX (t)
23516 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
23517 o, 0, 0, tf_none);
23518 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
23519 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
23520 TYPE_MAIN_VARIANT (t) = t;
23521 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
23522 TYPE_CANONICAL (t) = canonical_type_parameter (t);
23523 TREE_VEC_ELT (replacement, i) = t;
23524 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
23525 }
23526
23527 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
23528 TREE_VEC_ELT (replacement, i)
23529 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
23530
23531 /* If there are more levels then build up the replacement with the outer
23532 template parms. */
23533 if (depth > 1)
23534 replacement = add_to_template_args (template_parms_to_args
23535 (TREE_CHAIN (current_template_parms)),
23536 replacement);
23537
23538 return tsubst (parm, replacement, tf_none, NULL_TREE);
23539 }
23540
23541 /* Entries in the decl_constraint hash table. */
23542 struct GTY((for_user)) constr_entry
23543 {
23544 tree decl;
23545 tree ci;
23546 };
23547
23548 /* Hashing function and equality for constraint entries. */
23549 struct constr_hasher : ggc_ptr_hash<constr_entry>
23550 {
23551 static hashval_t hash (constr_entry *e)
23552 {
23553 return (hashval_t)DECL_UID (e->decl);
23554 }
23555
23556 static bool equal (constr_entry *e1, constr_entry *e2)
23557 {
23558 return e1->decl == e2->decl;
23559 }
23560 };
23561
23562 /* A mapping from declarations to constraint information. Note that
23563 both templates and their underlying declarations are mapped to the
23564 same constraint information.
23565
23566 FIXME: This is defined in pt.c because garbage collection
23567 code is not being generated for constraint.cc. */
23568
23569 static GTY (()) hash_table<constr_hasher> *decl_constraints;
23570
23571 /* Returns true iff cinfo contains a valid set of constraints.
23572 This is the case when the associated requirements have been
23573 successfully decomposed into lists of atomic constraints.
23574 That is, when the saved assumptions are not error_mark_node. */
23575
23576 bool
23577 valid_constraints_p (tree cinfo)
23578 {
23579 gcc_assert (cinfo);
23580 return CI_ASSUMPTIONS (cinfo) != error_mark_node;
23581 }
23582
23583 /* Returns the template constraints of declaration T. If T is not
23584 constrained, return NULL_TREE. Note that T must be non-null. */
23585
23586 tree
23587 get_constraints (tree t)
23588 {
23589 gcc_assert (DECL_P (t));
23590 if (TREE_CODE (t) == TEMPLATE_DECL)
23591 t = DECL_TEMPLATE_RESULT (t);
23592 constr_entry elt = { t, NULL_TREE };
23593 constr_entry* found = decl_constraints->find (&elt);
23594 if (found)
23595 return found->ci;
23596 else
23597 return NULL_TREE;
23598 }
23599
23600 /* Associate the given constraint information CI with the declaration
23601 T. If T is a template, then the constraints are associated with
23602 its underlying declaration. Don't build associations if CI is
23603 NULL_TREE. */
23604
23605 void
23606 set_constraints (tree t, tree ci)
23607 {
23608 if (!ci)
23609 return;
23610 gcc_assert (t);
23611 if (TREE_CODE (t) == TEMPLATE_DECL)
23612 t = DECL_TEMPLATE_RESULT (t);
23613 gcc_assert (!get_constraints (t));
23614 constr_entry elt = {t, ci};
23615 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
23616 constr_entry* entry = ggc_alloc<constr_entry> ();
23617 *entry = elt;
23618 *slot = entry;
23619 }
23620
23621 /* Remove the associated constraints of the declaration T. */
23622
23623 void
23624 remove_constraints (tree t)
23625 {
23626 gcc_assert (DECL_P (t));
23627 if (TREE_CODE (t) == TEMPLATE_DECL)
23628 t = DECL_TEMPLATE_RESULT (t);
23629
23630 constr_entry elt = {t, NULL_TREE};
23631 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
23632 if (slot)
23633 decl_constraints->clear_slot (slot);
23634 }
23635
23636 /* Set up the hash table for constraint association. */
23637
23638 void
23639 init_constraint_processing (void)
23640 {
23641 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
23642 }
23643
23644 /* Set up the hash tables for template instantiations. */
23645
23646 void
23647 init_template_processing (void)
23648 {
23649 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
23650 type_specializations = hash_table<spec_hasher>::create_ggc (37);
23651 }
23652
23653 /* Print stats about the template hash tables for -fstats. */
23654
23655 void
23656 print_template_statistics (void)
23657 {
23658 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
23659 "%f collisions\n", (long) decl_specializations->size (),
23660 (long) decl_specializations->elements (),
23661 decl_specializations->collisions ());
23662 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
23663 "%f collisions\n", (long) type_specializations->size (),
23664 (long) type_specializations->elements (),
23665 type_specializations->collisions ());
23666 }
23667
23668 #include "gt-cp-pt.h"