]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.c
Implement P0012R1, Make exception specifications part of the type system.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43
44 /* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46 typedef int (*tree_fn_t) (tree, void*);
47
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
54 };
55
56 static GTY(()) struct pending_template *pending_templates;
57 static GTY(()) struct pending_template *last_pending_template;
58
59 int processing_template_parmlist;
60 static int template_header_count;
61
62 static GTY(()) tree saved_trees;
63 static vec<int> inline_parm_levels;
64
65 static GTY(()) struct tinst_level *current_tinst_level;
66
67 static GTY(()) tree saved_access_scope;
68
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr;
73
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
76 //
77 // Implementation of the RAII helper for creating new local
78 // specializations.
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
81 {
82 local_specializations = new hash_map<tree, tree>;
83 }
84
85 local_specialization_stack::~local_specialization_stack ()
86 {
87 delete local_specializations;
88 local_specializations = saved;
89 }
90
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth;
93
94 struct GTY((for_user)) spec_entry
95 {
96 tree tmpl;
97 tree args;
98 tree spec;
99 };
100
101 struct spec_hasher : ggc_ptr_hash<spec_entry>
102 {
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
105 };
106
107 static GTY (()) hash_table<spec_hasher> *decl_specializations;
108
109 static GTY (()) hash_table<spec_hasher> *type_specializations;
110
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
116
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
125
126 enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
130 };
131
132 static void push_access_scope (tree);
133 static void pop_access_scope (tree);
134 static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137 static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139 static int unify (tree, tree, tree, tree, int, bool);
140 static void add_pending_template (tree);
141 static tree reopen_tinst_level (struct tinst_level *);
142 static tree tsubst_initializer_list (tree, tree);
143 static tree get_partial_spec_bindings (tree, tree, tree);
144 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
147 bool, bool);
148 static void tsubst_enum (tree, tree, tree);
149 static tree add_to_template_args (tree, tree);
150 static tree add_outermost_template_args (tree, tree);
151 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
152 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
153 tree);
154 static int type_unification_real (tree, tree, tree, const tree *,
155 unsigned int, int, unification_kind_t, int,
156 vec<deferred_access_check, va_gc> **,
157 bool);
158 static void note_template_header (int);
159 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
160 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
161 static tree convert_template_argument (tree, tree, tree,
162 tsubst_flags_t, int, tree);
163 static tree for_each_template_parm (tree, tree_fn_t, void*,
164 hash_set<tree> *, bool);
165 static tree expand_template_argument_pack (tree);
166 static tree build_template_parm_index (int, int, int, tree, tree);
167 static bool inline_needs_template_parms (tree, bool);
168 static void push_inline_template_parms_recursive (tree, int);
169 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170 static int mark_template_parm (tree, void *);
171 static int template_parm_this_level_p (tree, void *);
172 static tree tsubst_friend_function (tree, tree);
173 static tree tsubst_friend_class (tree, tree);
174 static int can_complete_type_without_circularity (tree);
175 static tree get_bindings (tree, tree, tree, bool);
176 static int template_decl_level (tree);
177 static int check_cv_quals_for_unify (int, tree, tree);
178 static void template_parm_level_and_index (tree, int*, int*);
179 static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181 static tree copy_template_args (tree);
182 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
184 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189 static bool check_specialization_scope (void);
190 static tree process_partial_specialization (tree);
191 static void set_current_access_from_decl (tree);
192 static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194 static tree try_class_unification (tree, tree, tree, tree, bool);
195 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197 static bool template_template_parm_bindings_ok_p (tree, tree);
198 static void tsubst_default_arguments (tree, tsubst_flags_t);
199 static tree for_each_template_parm_r (tree *, int *, void *);
200 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
201 static void copy_default_args_to_explicit_spec (tree);
202 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
203 static bool dependent_template_arg_p (tree);
204 static bool any_template_arguments_need_structural_equality_p (tree);
205 static bool dependent_type_p_r (tree);
206 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_decl (tree, tree, tsubst_flags_t);
208 static void perform_typedefs_access_check (tree tmpl, tree targs);
209 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
218
219 /* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
223
224 static void
225 push_access_scope (tree t)
226 {
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
228 || TREE_CODE (t) == TYPE_DECL);
229
230 if (DECL_FRIEND_CONTEXT (t))
231 push_nested_class (DECL_FRIEND_CONTEXT (t));
232 else if (DECL_CLASS_SCOPE_P (t))
233 push_nested_class (DECL_CONTEXT (t));
234 else
235 push_to_top_level ();
236
237 if (TREE_CODE (t) == FUNCTION_DECL)
238 {
239 saved_access_scope = tree_cons
240 (NULL_TREE, current_function_decl, saved_access_scope);
241 current_function_decl = t;
242 }
243 }
244
245 /* Restore the scope set up by push_access_scope. T is the node we
246 are processing. */
247
248 static void
249 pop_access_scope (tree t)
250 {
251 if (TREE_CODE (t) == FUNCTION_DECL)
252 {
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
255 }
256
257 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
261 }
262
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
267
268 tree
269 finish_member_template_decl (tree decl)
270 {
271 if (decl == error_mark_node)
272 return error_mark_node;
273
274 gcc_assert (DECL_P (decl));
275
276 if (TREE_CODE (decl) == TYPE_DECL)
277 {
278 tree type;
279
280 type = TREE_TYPE (decl);
281 if (type == error_mark_node)
282 return error_mark_node;
283 if (MAYBE_CLASS_TYPE_P (type)
284 && CLASSTYPE_TEMPLATE_INFO (type)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
286 {
287 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
288 check_member_template (tmpl);
289 return tmpl;
290 }
291 return NULL_TREE;
292 }
293 else if (TREE_CODE (decl) == FIELD_DECL)
294 error ("data member %qD cannot be a member template", decl);
295 else if (DECL_TEMPLATE_INFO (decl))
296 {
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
298 {
299 check_member_template (DECL_TI_TEMPLATE (decl));
300 return DECL_TI_TEMPLATE (decl);
301 }
302 else
303 return decl;
304 }
305 else
306 error ("invalid member template declaration %qD", decl);
307
308 return error_mark_node;
309 }
310
311 /* Create a template info node. */
312
313 tree
314 build_template_info (tree template_decl, tree template_args)
315 {
316 tree result = make_node (TEMPLATE_INFO);
317 TI_TEMPLATE (result) = template_decl;
318 TI_ARGS (result) = template_args;
319 return result;
320 }
321
322 /* Return the template info node corresponding to T, whatever T is. */
323
324 tree
325 get_template_info (const_tree t)
326 {
327 tree tinfo = NULL_TREE;
328
329 if (!t || t == error_mark_node)
330 return NULL;
331
332 if (TREE_CODE (t) == NAMESPACE_DECL
333 || TREE_CODE (t) == PARM_DECL)
334 return NULL;
335
336 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
337 tinfo = DECL_TEMPLATE_INFO (t);
338
339 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
340 t = TREE_TYPE (t);
341
342 if (OVERLOAD_TYPE_P (t))
343 tinfo = TYPE_TEMPLATE_INFO (t);
344 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
345 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
346
347 return tinfo;
348 }
349
350 /* Returns the template nesting level of the indicated class TYPE.
351
352 For example, in:
353 template <class T>
354 struct A
355 {
356 template <class U>
357 struct B {};
358 };
359
360 A<T>::B<U> has depth two, while A<T> has depth one.
361 Both A<T>::B<int> and A<int>::B<U> have depth one, if
362 they are instantiations, not specializations.
363
364 This function is guaranteed to return 0 if passed NULL_TREE so
365 that, for example, `template_class_depth (current_class_type)' is
366 always safe. */
367
368 int
369 template_class_depth (tree type)
370 {
371 int depth;
372
373 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
374 {
375 tree tinfo = get_template_info (type);
376
377 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
378 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
379 ++depth;
380
381 if (DECL_P (type))
382 type = CP_DECL_CONTEXT (type);
383 else if (LAMBDA_TYPE_P (type))
384 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
385 else
386 type = CP_TYPE_CONTEXT (type);
387 }
388
389 return depth;
390 }
391
392 /* Subroutine of maybe_begin_member_template_processing.
393 Returns true if processing DECL needs us to push template parms. */
394
395 static bool
396 inline_needs_template_parms (tree decl, bool nsdmi)
397 {
398 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
399 return false;
400
401 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
402 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
403 }
404
405 /* Subroutine of maybe_begin_member_template_processing.
406 Push the template parms in PARMS, starting from LEVELS steps into the
407 chain, and ending at the beginning, since template parms are listed
408 innermost first. */
409
410 static void
411 push_inline_template_parms_recursive (tree parmlist, int levels)
412 {
413 tree parms = TREE_VALUE (parmlist);
414 int i;
415
416 if (levels > 1)
417 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
418
419 ++processing_template_decl;
420 current_template_parms
421 = tree_cons (size_int (processing_template_decl),
422 parms, current_template_parms);
423 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
424
425 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
426 NULL);
427 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
428 {
429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
430
431 if (error_operand_p (parm))
432 continue;
433
434 gcc_assert (DECL_P (parm));
435
436 switch (TREE_CODE (parm))
437 {
438 case TYPE_DECL:
439 case TEMPLATE_DECL:
440 pushdecl (parm);
441 break;
442
443 case PARM_DECL:
444 /* Push the CONST_DECL. */
445 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
446 break;
447
448 default:
449 gcc_unreachable ();
450 }
451 }
452 }
453
454 /* Restore the template parameter context for a member template, a
455 friend template defined in a class definition, or a non-template
456 member of template class. */
457
458 void
459 maybe_begin_member_template_processing (tree decl)
460 {
461 tree parms;
462 int levels = 0;
463 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
464
465 if (nsdmi)
466 {
467 tree ctx = DECL_CONTEXT (decl);
468 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
469 /* Disregard full specializations (c++/60999). */
470 && uses_template_parms (ctx)
471 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
472 }
473
474 if (inline_needs_template_parms (decl, nsdmi))
475 {
476 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
477 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
478
479 if (DECL_TEMPLATE_SPECIALIZATION (decl))
480 {
481 --levels;
482 parms = TREE_CHAIN (parms);
483 }
484
485 push_inline_template_parms_recursive (parms, levels);
486 }
487
488 /* Remember how many levels of template parameters we pushed so that
489 we can pop them later. */
490 inline_parm_levels.safe_push (levels);
491 }
492
493 /* Undo the effects of maybe_begin_member_template_processing. */
494
495 void
496 maybe_end_member_template_processing (void)
497 {
498 int i;
499 int last;
500
501 if (inline_parm_levels.length () == 0)
502 return;
503
504 last = inline_parm_levels.pop ();
505 for (i = 0; i < last; ++i)
506 {
507 --processing_template_decl;
508 current_template_parms = TREE_CHAIN (current_template_parms);
509 poplevel (0, 0, 0);
510 }
511 }
512
513 /* Return a new template argument vector which contains all of ARGS,
514 but has as its innermost set of arguments the EXTRA_ARGS. */
515
516 static tree
517 add_to_template_args (tree args, tree extra_args)
518 {
519 tree new_args;
520 int extra_depth;
521 int i;
522 int j;
523
524 if (args == NULL_TREE || extra_args == error_mark_node)
525 return extra_args;
526
527 extra_depth = TMPL_ARGS_DEPTH (extra_args);
528 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
529
530 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
531 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
532
533 for (j = 1; j <= extra_depth; ++j, ++i)
534 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
535
536 return new_args;
537 }
538
539 /* Like add_to_template_args, but only the outermost ARGS are added to
540 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
541 (EXTRA_ARGS) levels are added. This function is used to combine
542 the template arguments from a partial instantiation with the
543 template arguments used to attain the full instantiation from the
544 partial instantiation. */
545
546 static tree
547 add_outermost_template_args (tree args, tree extra_args)
548 {
549 tree new_args;
550
551 /* If there are more levels of EXTRA_ARGS than there are ARGS,
552 something very fishy is going on. */
553 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
554
555 /* If *all* the new arguments will be the EXTRA_ARGS, just return
556 them. */
557 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
558 return extra_args;
559
560 /* For the moment, we make ARGS look like it contains fewer levels. */
561 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
562
563 new_args = add_to_template_args (args, extra_args);
564
565 /* Now, we restore ARGS to its full dimensions. */
566 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
567
568 return new_args;
569 }
570
571 /* Return the N levels of innermost template arguments from the ARGS. */
572
573 tree
574 get_innermost_template_args (tree args, int n)
575 {
576 tree new_args;
577 int extra_levels;
578 int i;
579
580 gcc_assert (n >= 0);
581
582 /* If N is 1, just return the innermost set of template arguments. */
583 if (n == 1)
584 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
585
586 /* If we're not removing anything, just return the arguments we were
587 given. */
588 extra_levels = TMPL_ARGS_DEPTH (args) - n;
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
592
593 /* Make a new set of arguments, not containing the outer arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i + extra_levels));
598
599 return new_args;
600 }
601
602 /* The inverse of get_innermost_template_args: Return all but the innermost
603 EXTRA_LEVELS levels of template arguments from the ARGS. */
604
605 static tree
606 strip_innermost_template_args (tree args, int extra_levels)
607 {
608 tree new_args;
609 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
610 int i;
611
612 gcc_assert (n >= 0);
613
614 /* If N is 1, just return the outermost set of template arguments. */
615 if (n == 1)
616 return TMPL_ARGS_LEVEL (args, 1);
617
618 /* If we're not removing anything, just return the arguments we were
619 given. */
620 gcc_assert (extra_levels >= 0);
621 if (extra_levels == 0)
622 return args;
623
624 /* Make a new set of arguments, not containing the inner arguments. */
625 new_args = make_tree_vec (n);
626 for (i = 1; i <= n; ++i)
627 SET_TMPL_ARGS_LEVEL (new_args, i,
628 TMPL_ARGS_LEVEL (args, i));
629
630 return new_args;
631 }
632
633 /* We've got a template header coming up; push to a new level for storing
634 the parms. */
635
636 void
637 begin_template_parm_list (void)
638 {
639 /* We use a non-tag-transparent scope here, which causes pushtag to
640 put tags in this scope, rather than in the enclosing class or
641 namespace scope. This is the right thing, since we want
642 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
643 global template class, push_template_decl handles putting the
644 TEMPLATE_DECL into top-level scope. For a nested template class,
645 e.g.:
646
647 template <class T> struct S1 {
648 template <class T> struct S2 {};
649 };
650
651 pushtag contains special code to call pushdecl_with_scope on the
652 TEMPLATE_DECL for S2. */
653 begin_scope (sk_template_parms, NULL);
654 ++processing_template_decl;
655 ++processing_template_parmlist;
656 note_template_header (0);
657
658 /* Add a dummy parameter level while we process the parameter list. */
659 current_template_parms
660 = tree_cons (size_int (processing_template_decl),
661 make_tree_vec (0),
662 current_template_parms);
663 }
664
665 /* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
668
669 static bool
670 check_specialization_scope (void)
671 {
672 tree scope = current_scope ();
673
674 /* [temp.expl.spec]
675
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
682 is a member. */
683 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
684 {
685 error ("explicit specialization in non-namespace scope %qD", scope);
686 return false;
687 }
688
689 /* [temp.expl.spec]
690
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms)
698 {
699 error ("enclosing class templates are not explicitly specialized");
700 return false;
701 }
702
703 return true;
704 }
705
706 /* We've just seen template <>. */
707
708 bool
709 begin_specialization (void)
710 {
711 begin_scope (sk_template_spec, NULL);
712 note_template_header (1);
713 return check_specialization_scope ();
714 }
715
716 /* Called at then end of processing a declaration preceded by
717 template<>. */
718
719 void
720 end_specialization (void)
721 {
722 finish_scope ();
723 reset_specialization ();
724 }
725
726 /* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
728
729 void
730 reset_specialization (void)
731 {
732 processing_specialization = 0;
733 template_header_count = 0;
734 }
735
736 /* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
738
739 static void
740 note_template_header (int specialization)
741 {
742 processing_specialization = specialization;
743 template_header_count++;
744 }
745
746 /* We're beginning an explicit instantiation. */
747
748 void
749 begin_explicit_instantiation (void)
750 {
751 gcc_assert (!processing_explicit_instantiation);
752 processing_explicit_instantiation = true;
753 }
754
755
756 void
757 end_explicit_instantiation (void)
758 {
759 gcc_assert (processing_explicit_instantiation);
760 processing_explicit_instantiation = false;
761 }
762
763 /* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
767
768 static bool
769 check_specialization_namespace (tree tmpl)
770 {
771 tree tpl_ns = decl_namespace_context (tmpl);
772
773 /* [tmpl.expl.spec]
774
775 An explicit specialization shall be declared in the namespace of
776 which the template is a member, or, for member templates, in the
777 namespace of which the enclosing class or enclosing class
778 template is a member. An explicit specialization of a member
779 function, member class or static data member of a class template
780 shall be declared in the namespace of which the class template is
781 a member. */
782 if (current_scope() != DECL_CONTEXT (tmpl)
783 && !at_namespace_scope_p ())
784 {
785 error ("specialization of %qD must appear at namespace scope", tmpl);
786 return false;
787 }
788 if (is_associated_namespace (current_namespace, tpl_ns))
789 /* Same or super-using namespace. */
790 return true;
791 else
792 {
793 permerror (input_location,
794 "specialization of %qD in different namespace", tmpl);
795 permerror (DECL_SOURCE_LOCATION (tmpl),
796 " from definition of %q#D", tmpl);
797 return false;
798 }
799 }
800
801 /* SPEC is an explicit instantiation. Check that it is valid to
802 perform this explicit instantiation in the current namespace. */
803
804 static void
805 check_explicit_instantiation_namespace (tree spec)
806 {
807 tree ns;
808
809 /* DR 275: An explicit instantiation shall appear in an enclosing
810 namespace of its template. */
811 ns = decl_namespace_context (spec);
812 if (!is_ancestor (current_namespace, ns))
813 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
814 "(which does not enclose namespace %qD)",
815 spec, current_namespace, ns);
816 }
817
818 // Returns the type of a template specialization only if that
819 // specialization needs to be defined. Otherwise (e.g., if the type has
820 // already been defined), the function returns NULL_TREE.
821 static tree
822 maybe_new_partial_specialization (tree type)
823 {
824 // An implicit instantiation of an incomplete type implies
825 // the definition of a new class template.
826 //
827 // template<typename T>
828 // struct S;
829 //
830 // template<typename T>
831 // struct S<T*>;
832 //
833 // Here, S<T*> is an implicit instantiation of S whose type
834 // is incomplete.
835 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
836 return type;
837
838 // It can also be the case that TYPE is a completed specialization.
839 // Continuing the previous example, suppose we also declare:
840 //
841 // template<typename T>
842 // requires Integral<T>
843 // struct S<T*>;
844 //
845 // Here, S<T*> refers to the specialization S<T*> defined
846 // above. However, we need to differentiate definitions because
847 // we intend to define a new partial specialization. In this case,
848 // we rely on the fact that the constraints are different for
849 // this declaration than that above.
850 //
851 // Note that we also get here for injected class names and
852 // late-parsed template definitions. We must ensure that we
853 // do not create new type declarations for those cases.
854 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
855 {
856 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
857 tree args = CLASSTYPE_TI_ARGS (type);
858
859 // If there are no template parameters, this cannot be a new
860 // partial template specializtion?
861 if (!current_template_parms)
862 return NULL_TREE;
863
864 // The injected-class-name is not a new partial specialization.
865 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
866 return NULL_TREE;
867
868 // If the constraints are not the same as those of the primary
869 // then, we can probably create a new specialization.
870 tree type_constr = current_template_constraints ();
871
872 if (type == TREE_TYPE (tmpl))
873 {
874 tree main_constr = get_constraints (tmpl);
875 if (equivalent_constraints (type_constr, main_constr))
876 return NULL_TREE;
877 }
878
879 // Also, if there's a pre-existing specialization with matching
880 // constraints, then this also isn't new.
881 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
882 while (specs)
883 {
884 tree spec_tmpl = TREE_VALUE (specs);
885 tree spec_args = TREE_PURPOSE (specs);
886 tree spec_constr = get_constraints (spec_tmpl);
887 if (comp_template_args (args, spec_args)
888 && equivalent_constraints (type_constr, spec_constr))
889 return NULL_TREE;
890 specs = TREE_CHAIN (specs);
891 }
892
893 // Create a new type node (and corresponding type decl)
894 // for the newly declared specialization.
895 tree t = make_class_type (TREE_CODE (type));
896 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
897 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
898
899 /* We only need a separate type node for storing the definition of this
900 partial specialization; uses of S<T*> are unconstrained, so all are
901 equivalent. So keep TYPE_CANONICAL the same. */
902 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
903
904 // Build the corresponding type decl.
905 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
906 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
907 DECL_SOURCE_LOCATION (d) = input_location;
908
909 return t;
910 }
911
912 return NULL_TREE;
913 }
914
915 /* The TYPE is being declared. If it is a template type, that means it
916 is a partial specialization. Do appropriate error-checking. */
917
918 tree
919 maybe_process_partial_specialization (tree type)
920 {
921 tree context;
922
923 if (type == error_mark_node)
924 return error_mark_node;
925
926 /* A lambda that appears in specialization context is not itself a
927 specialization. */
928 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
929 return type;
930
931 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
932 {
933 error ("name of class shadows template template parameter %qD",
934 TYPE_NAME (type));
935 return error_mark_node;
936 }
937
938 context = TYPE_CONTEXT (type);
939
940 if (TYPE_ALIAS_P (type))
941 {
942 if (TYPE_TEMPLATE_INFO (type)
943 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
944 error ("specialization of alias template %qD",
945 TYPE_TI_TEMPLATE (type));
946 else
947 error ("explicit specialization of non-template %qT", type);
948 return error_mark_node;
949 }
950 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
951 {
952 /* This is for ordinary explicit specialization and partial
953 specialization of a template class such as:
954
955 template <> class C<int>;
956
957 or:
958
959 template <class T> class C<T*>;
960
961 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
962
963 if (tree t = maybe_new_partial_specialization (type))
964 {
965 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
966 && !at_namespace_scope_p ())
967 return error_mark_node;
968 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
969 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
970 if (processing_template_decl)
971 {
972 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
973 if (decl == error_mark_node)
974 return error_mark_node;
975 return TREE_TYPE (decl);
976 }
977 }
978 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
979 error ("specialization of %qT after instantiation", type);
980 else if (errorcount && !processing_specialization
981 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
982 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
983 /* Trying to define a specialization either without a template<> header
984 or in an inappropriate place. We've already given an error, so just
985 bail now so we don't actually define the specialization. */
986 return error_mark_node;
987 }
988 else if (CLASS_TYPE_P (type)
989 && !CLASSTYPE_USE_TEMPLATE (type)
990 && CLASSTYPE_TEMPLATE_INFO (type)
991 && context && CLASS_TYPE_P (context)
992 && CLASSTYPE_TEMPLATE_INFO (context))
993 {
994 /* This is for an explicit specialization of member class
995 template according to [temp.expl.spec/18]:
996
997 template <> template <class U> class C<int>::D;
998
999 The context `C<int>' must be an implicit instantiation.
1000 Otherwise this is just a member class template declared
1001 earlier like:
1002
1003 template <> class C<int> { template <class U> class D; };
1004 template <> template <class U> class C<int>::D;
1005
1006 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1007 while in the second case, `C<int>::D' is a primary template
1008 and `C<T>::D' may not exist. */
1009
1010 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1011 && !COMPLETE_TYPE_P (type))
1012 {
1013 tree t;
1014 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1015
1016 if (current_namespace
1017 != decl_namespace_context (tmpl))
1018 {
1019 permerror (input_location,
1020 "specializing %q#T in different namespace", type);
1021 permerror (DECL_SOURCE_LOCATION (tmpl),
1022 " from definition of %q#D", tmpl);
1023 }
1024
1025 /* Check for invalid specialization after instantiation:
1026
1027 template <> template <> class C<int>::D<int>;
1028 template <> template <class U> class C<int>::D; */
1029
1030 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1031 t; t = TREE_CHAIN (t))
1032 {
1033 tree inst = TREE_VALUE (t);
1034 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1035 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1036 {
1037 /* We already have a full specialization of this partial
1038 instantiation, or a full specialization has been
1039 looked up but not instantiated. Reassign it to the
1040 new member specialization template. */
1041 spec_entry elt;
1042 spec_entry *entry;
1043
1044 elt.tmpl = most_general_template (tmpl);
1045 elt.args = CLASSTYPE_TI_ARGS (inst);
1046 elt.spec = inst;
1047
1048 type_specializations->remove_elt (&elt);
1049
1050 elt.tmpl = tmpl;
1051 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1052
1053 spec_entry **slot
1054 = type_specializations->find_slot (&elt, INSERT);
1055 entry = ggc_alloc<spec_entry> ();
1056 *entry = elt;
1057 *slot = entry;
1058 }
1059 else
1060 /* But if we've had an implicit instantiation, that's a
1061 problem ([temp.expl.spec]/6). */
1062 error ("specialization %qT after instantiation %qT",
1063 type, inst);
1064 }
1065
1066 /* Mark TYPE as a specialization. And as a result, we only
1067 have one level of template argument for the innermost
1068 class template. */
1069 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1070 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1071 CLASSTYPE_TI_ARGS (type)
1072 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1073 }
1074 }
1075 else if (processing_specialization)
1076 {
1077 /* Someday C++0x may allow for enum template specialization. */
1078 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1079 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1080 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1081 "of %qD not allowed by ISO C++", type);
1082 else
1083 {
1084 error ("explicit specialization of non-template %qT", type);
1085 return error_mark_node;
1086 }
1087 }
1088
1089 return type;
1090 }
1091
1092 /* Returns nonzero if we can optimize the retrieval of specializations
1093 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1094 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1095
1096 static inline bool
1097 optimize_specialization_lookup_p (tree tmpl)
1098 {
1099 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1100 && DECL_CLASS_SCOPE_P (tmpl)
1101 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1102 parameter. */
1103 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1104 /* The optimized lookup depends on the fact that the
1105 template arguments for the member function template apply
1106 purely to the containing class, which is not true if the
1107 containing class is an explicit or partial
1108 specialization. */
1109 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1110 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1111 && !DECL_CONV_FN_P (tmpl)
1112 /* It is possible to have a template that is not a member
1113 template and is not a member of a template class:
1114
1115 template <typename T>
1116 struct S { friend A::f(); };
1117
1118 Here, the friend function is a template, but the context does
1119 not have template information. The optimized lookup relies
1120 on having ARGS be the template arguments for both the class
1121 and the function template. */
1122 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1123 }
1124
1125 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1126 gone through coerce_template_parms by now. */
1127
1128 static void
1129 verify_unstripped_args (tree args)
1130 {
1131 ++processing_template_decl;
1132 if (!any_dependent_template_arguments_p (args))
1133 {
1134 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1135 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1136 {
1137 tree arg = TREE_VEC_ELT (inner, i);
1138 if (TREE_CODE (arg) == TEMPLATE_DECL)
1139 /* OK */;
1140 else if (TYPE_P (arg))
1141 gcc_assert (strip_typedefs (arg, NULL) == arg);
1142 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1143 /* Allow typedefs on the type of a non-type argument, since a
1144 parameter can have them. */;
1145 else
1146 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1147 }
1148 }
1149 --processing_template_decl;
1150 }
1151
1152 /* Retrieve the specialization (in the sense of [temp.spec] - a
1153 specialization is either an instantiation or an explicit
1154 specialization) of TMPL for the given template ARGS. If there is
1155 no such specialization, return NULL_TREE. The ARGS are a vector of
1156 arguments, or a vector of vectors of arguments, in the case of
1157 templates with more than one level of parameters.
1158
1159 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1160 then we search for a partial specialization matching ARGS. This
1161 parameter is ignored if TMPL is not a class template.
1162
1163 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1164 result is a NONTYPE_ARGUMENT_PACK. */
1165
1166 static tree
1167 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1168 {
1169 if (tmpl == NULL_TREE)
1170 return NULL_TREE;
1171
1172 if (args == error_mark_node)
1173 return NULL_TREE;
1174
1175 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1176 || TREE_CODE (tmpl) == FIELD_DECL);
1177
1178 /* There should be as many levels of arguments as there are
1179 levels of parameters. */
1180 gcc_assert (TMPL_ARGS_DEPTH (args)
1181 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1183 : template_class_depth (DECL_CONTEXT (tmpl))));
1184
1185 if (flag_checking)
1186 verify_unstripped_args (args);
1187
1188 if (optimize_specialization_lookup_p (tmpl))
1189 {
1190 tree class_template;
1191 tree class_specialization;
1192 vec<tree, va_gc> *methods;
1193 tree fns;
1194 int idx;
1195
1196 /* The template arguments actually apply to the containing
1197 class. Find the class specialization with those
1198 arguments. */
1199 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1200 class_specialization
1201 = retrieve_specialization (class_template, args, 0);
1202 if (!class_specialization)
1203 return NULL_TREE;
1204 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1205 for the specialization. */
1206 idx = class_method_index_for_fn (class_specialization, tmpl);
1207 if (idx == -1)
1208 return NULL_TREE;
1209 /* Iterate through the methods with the indicated name, looking
1210 for the one that has an instance of TMPL. */
1211 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1212 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1213 {
1214 tree fn = OVL_CURRENT (fns);
1215 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn) == class_specialization)
1219 return fn;
1220 }
1221 return NULL_TREE;
1222 }
1223 else
1224 {
1225 spec_entry *found;
1226 spec_entry elt;
1227 hash_table<spec_hasher> *specializations;
1228
1229 elt.tmpl = tmpl;
1230 elt.args = args;
1231 elt.spec = NULL_TREE;
1232
1233 if (DECL_CLASS_TEMPLATE_P (tmpl))
1234 specializations = type_specializations;
1235 else
1236 specializations = decl_specializations;
1237
1238 if (hash == 0)
1239 hash = spec_hasher::hash (&elt);
1240 found = specializations->find_with_hash (&elt, hash);
1241 if (found)
1242 return found->spec;
1243 }
1244
1245 return NULL_TREE;
1246 }
1247
1248 /* Like retrieve_specialization, but for local declarations. */
1249
1250 tree
1251 retrieve_local_specialization (tree tmpl)
1252 {
1253 if (local_specializations == NULL)
1254 return NULL_TREE;
1255
1256 tree *slot = local_specializations->get (tmpl);
1257 return slot ? *slot : NULL_TREE;
1258 }
1259
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1261
1262 int
1263 is_specialization_of (tree decl, tree tmpl)
1264 {
1265 tree t;
1266
1267 if (TREE_CODE (decl) == FUNCTION_DECL)
1268 {
1269 for (t = decl;
1270 t != NULL_TREE;
1271 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1272 if (t == tmpl)
1273 return 1;
1274 }
1275 else
1276 {
1277 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1278
1279 for (t = TREE_TYPE (decl);
1280 t != NULL_TREE;
1281 t = CLASSTYPE_USE_TEMPLATE (t)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1283 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1284 return 1;
1285 }
1286
1287 return 0;
1288 }
1289
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1292
1293 bool
1294 is_specialization_of_friend (tree decl, tree friend_decl)
1295 {
1296 bool need_template = true;
1297 int template_depth;
1298
1299 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1300 || TREE_CODE (decl) == TYPE_DECL);
1301
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1304 if this. */
1305 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl)
1307 && !DECL_USE_TEMPLATE (friend_decl))
1308 {
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1311 need_template = false;
1312 }
1313 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl))
1315 need_template = false;
1316
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1319 return false;
1320
1321 if (is_specialization_of (decl, friend_decl))
1322 return true;
1323
1324 /* [temp.friend/6]
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1329
1330 For example, given a template friend declaration
1331
1332 template <class T> friend void A<T>::f();
1333
1334 the member function below is considered a friend
1335
1336 template <> struct A<int> {
1337 void f();
1338 };
1339
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1343
1344 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1345 if (template_depth
1346 && DECL_CLASS_SCOPE_P (decl)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1349 {
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1352
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1355
1356 and DECL's are
1357
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1360
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1365
1366 tree context = DECL_CONTEXT (decl);
1367 tree args = NULL_TREE;
1368 int current_depth = 0;
1369
1370 while (current_depth < template_depth)
1371 {
1372 if (CLASSTYPE_TEMPLATE_INFO (context))
1373 {
1374 if (current_depth == 0)
1375 args = TYPE_TI_ARGS (context);
1376 else
1377 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1378 current_depth++;
1379 }
1380 context = TYPE_CONTEXT (context);
1381 }
1382
1383 if (TREE_CODE (decl) == FUNCTION_DECL)
1384 {
1385 bool is_template;
1386 tree friend_type;
1387 tree decl_type;
1388 tree friend_args_type;
1389 tree decl_args_type;
1390
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1392 non-templates. */
1393 is_template = DECL_TEMPLATE_INFO (decl)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1395 if (need_template ^ is_template)
1396 return false;
1397 else if (is_template)
1398 {
1399 /* If both are templates, check template parameter list. */
1400 tree friend_parms
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1402 args, tf_none);
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1405 friend_parms))
1406 return false;
1407
1408 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1409 }
1410 else
1411 decl_type = TREE_TYPE (decl);
1412
1413 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1414 tf_none, NULL_TREE);
1415 if (friend_type == error_mark_node)
1416 return false;
1417
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1420 return false;
1421
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type = TYPE_ARG_TYPES (friend_type);
1425 decl_args_type = TYPE_ARG_TYPES (decl_type);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1427 friend_args_type = TREE_CHAIN (friend_args_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1429 decl_args_type = TREE_CHAIN (decl_args_type);
1430
1431 return compparms (decl_args_type, friend_args_type);
1432 }
1433 else
1434 {
1435 /* DECL is a TYPE_DECL */
1436 bool is_template;
1437 tree decl_type = TREE_TYPE (decl);
1438
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1444
1445 if (need_template ^ is_template)
1446 return false;
1447 else if (is_template)
1448 {
1449 tree friend_parms;
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1453 != DECL_NAME (friend_decl))
1454 return false;
1455
1456 /* Now check template parameter list. */
1457 friend_parms
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1459 args, tf_none);
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1462 friend_parms);
1463 }
1464 else
1465 return (DECL_NAME (decl)
1466 == DECL_NAME (friend_decl));
1467 }
1468 }
1469 return false;
1470 }
1471
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1476
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1479
1480 static tree
1481 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1482 hashval_t hash)
1483 {
1484 tree fn;
1485 spec_entry **slot = NULL;
1486 spec_entry elt;
1487
1488 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1489 || (TREE_CODE (tmpl) == FIELD_DECL
1490 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1491
1492 if (TREE_CODE (spec) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1500
1501 template <class T> void f(T t1, T t = T())
1502
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1505 return spec;
1506
1507 if (optimize_specialization_lookup_p (tmpl))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn = retrieve_specialization (tmpl, args, 0);
1511 else
1512 {
1513 elt.tmpl = tmpl;
1514 elt.args = args;
1515 elt.spec = spec;
1516
1517 if (hash == 0)
1518 hash = spec_hasher::hash (&elt);
1519
1520 slot =
1521 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1522 if (*slot)
1523 fn = ((spec_entry *) *slot)->spec;
1524 else
1525 fn = NULL_TREE;
1526 }
1527
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1533 if (fn == spec)
1534 return spec;
1535 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1536 {
1537 if (DECL_TEMPLATE_INSTANTIATION (fn))
1538 {
1539 if (DECL_ODR_USED (fn)
1540 || DECL_EXPLICIT_INSTANTIATION (fn))
1541 {
1542 error ("specialization of %qD after instantiation",
1543 fn);
1544 return error_mark_node;
1545 }
1546 else
1547 {
1548 tree clone;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1555 it later.
1556
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1559 specialization:
1560
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1564
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1567 declaration.
1568
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn) = NULL_TREE;
1573 duplicate_decls (spec, fn, is_friend);
1574 /* The call to duplicate_decls will have applied
1575 [temp.expl.spec]:
1576
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1580 is.
1581
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone, fn)
1585 {
1586 DECL_DECLARED_INLINE_P (clone)
1587 = DECL_DECLARED_INLINE_P (fn);
1588 DECL_SOURCE_LOCATION (clone)
1589 = DECL_SOURCE_LOCATION (fn);
1590 DECL_DELETED_FN (clone)
1591 = DECL_DELETED_FN (fn);
1592 }
1593 check_specialization_namespace (tmpl);
1594
1595 return fn;
1596 }
1597 }
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1599 {
1600 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1601 /* Dup decl failed, but this is a new definition. Set the
1602 line number so any errors match this new
1603 definition. */
1604 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1605
1606 return fn;
1607 }
1608 }
1609 else if (fn)
1610 return duplicate_decls (spec, fn, is_friend);
1611
1612 /* A specialization must be declared in the same namespace as the
1613 template it is specializing. */
1614 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1615 && !check_specialization_namespace (tmpl))
1616 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1617
1618 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1619 {
1620 spec_entry *entry = ggc_alloc<spec_entry> ();
1621 gcc_assert (tmpl && args && spec);
1622 *entry = elt;
1623 *slot = entry;
1624 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1625 && PRIMARY_TEMPLATE_P (tmpl)
1626 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1627 || variable_template_p (tmpl))
1628 /* If TMPL is a forward declaration of a template function, keep a list
1629 of all specializations in case we need to reassign them to a friend
1630 template later in tsubst_friend_function.
1631
1632 Also keep a list of all variable template instantiations so that
1633 process_partial_specialization can check whether a later partial
1634 specialization would have used it. */
1635 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1636 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1637 }
1638
1639 return spec;
1640 }
1641
1642 /* Returns true iff two spec_entry nodes are equivalent. */
1643
1644 int comparing_specializations;
1645
1646 bool
1647 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1648 {
1649 int equal;
1650
1651 ++comparing_specializations;
1652 equal = (e1->tmpl == e2->tmpl
1653 && comp_template_args (e1->args, e2->args));
1654 if (equal && flag_concepts
1655 /* tmpl could be a FIELD_DECL for a capture pack. */
1656 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1657 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1658 && uses_template_parms (e1->args))
1659 {
1660 /* Partial specializations of a variable template can be distinguished by
1661 constraints. */
1662 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1663 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1664 equal = equivalent_constraints (c1, c2);
1665 }
1666 --comparing_specializations;
1667
1668 return equal;
1669 }
1670
1671 /* Returns a hash for a template TMPL and template arguments ARGS. */
1672
1673 static hashval_t
1674 hash_tmpl_and_args (tree tmpl, tree args)
1675 {
1676 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1677 return iterative_hash_template_arg (args, val);
1678 }
1679
1680 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1681 ignoring SPEC. */
1682
1683 hashval_t
1684 spec_hasher::hash (spec_entry *e)
1685 {
1686 return hash_tmpl_and_args (e->tmpl, e->args);
1687 }
1688
1689 /* Recursively calculate a hash value for a template argument ARG, for use
1690 in the hash tables of template specializations. */
1691
1692 hashval_t
1693 iterative_hash_template_arg (tree arg, hashval_t val)
1694 {
1695 unsigned HOST_WIDE_INT i;
1696 enum tree_code code;
1697 char tclass;
1698
1699 if (arg == NULL_TREE)
1700 return iterative_hash_object (arg, val);
1701
1702 if (!TYPE_P (arg))
1703 STRIP_NOPS (arg);
1704
1705 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1706 gcc_unreachable ();
1707
1708 code = TREE_CODE (arg);
1709 tclass = TREE_CODE_CLASS (code);
1710
1711 val = iterative_hash_object (code, val);
1712
1713 switch (code)
1714 {
1715 case ERROR_MARK:
1716 return val;
1717
1718 case IDENTIFIER_NODE:
1719 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1720
1721 case TREE_VEC:
1722 {
1723 int i, len = TREE_VEC_LENGTH (arg);
1724 for (i = 0; i < len; ++i)
1725 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1726 return val;
1727 }
1728
1729 case TYPE_PACK_EXPANSION:
1730 case EXPR_PACK_EXPANSION:
1731 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1732 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1733
1734 case TYPE_ARGUMENT_PACK:
1735 case NONTYPE_ARGUMENT_PACK:
1736 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1737
1738 case TREE_LIST:
1739 for (; arg; arg = TREE_CHAIN (arg))
1740 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1741 return val;
1742
1743 case OVERLOAD:
1744 for (; arg; arg = OVL_NEXT (arg))
1745 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1746 return val;
1747
1748 case CONSTRUCTOR:
1749 {
1750 tree field, value;
1751 iterative_hash_template_arg (TREE_TYPE (arg), val);
1752 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1753 {
1754 val = iterative_hash_template_arg (field, val);
1755 val = iterative_hash_template_arg (value, val);
1756 }
1757 return val;
1758 }
1759
1760 case PARM_DECL:
1761 if (!DECL_ARTIFICIAL (arg))
1762 {
1763 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1764 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1765 }
1766 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1767
1768 case TARGET_EXPR:
1769 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1770
1771 case PTRMEM_CST:
1772 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1773 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1774
1775 case TEMPLATE_PARM_INDEX:
1776 val = iterative_hash_template_arg
1777 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1778 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1779 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1780
1781 case TRAIT_EXPR:
1782 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1783 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1784 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1785
1786 case BASELINK:
1787 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1788 val);
1789 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1790 val);
1791
1792 case MODOP_EXPR:
1793 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1794 code = TREE_CODE (TREE_OPERAND (arg, 1));
1795 val = iterative_hash_object (code, val);
1796 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1797
1798 case LAMBDA_EXPR:
1799 /* A lambda can't appear in a template arg, but don't crash on
1800 erroneous input. */
1801 gcc_assert (seen_error ());
1802 return val;
1803
1804 case CAST_EXPR:
1805 case IMPLICIT_CONV_EXPR:
1806 case STATIC_CAST_EXPR:
1807 case REINTERPRET_CAST_EXPR:
1808 case CONST_CAST_EXPR:
1809 case DYNAMIC_CAST_EXPR:
1810 case NEW_EXPR:
1811 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1812 /* Now hash operands as usual. */
1813 break;
1814
1815 default:
1816 break;
1817 }
1818
1819 switch (tclass)
1820 {
1821 case tcc_type:
1822 if (alias_template_specialization_p (arg))
1823 {
1824 // We want an alias specialization that survived strip_typedefs
1825 // to hash differently from its TYPE_CANONICAL, to avoid hash
1826 // collisions that compare as different in template_args_equal.
1827 // These could be dependent specializations that strip_typedefs
1828 // left alone, or untouched specializations because
1829 // coerce_template_parms returns the unconverted template
1830 // arguments if it sees incomplete argument packs.
1831 tree ti = TYPE_TEMPLATE_INFO (arg);
1832 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1833 }
1834 if (TYPE_CANONICAL (arg))
1835 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1836 val);
1837 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1838 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1839 /* Otherwise just compare the types during lookup. */
1840 return val;
1841
1842 case tcc_declaration:
1843 case tcc_constant:
1844 return iterative_hash_expr (arg, val);
1845
1846 default:
1847 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1848 {
1849 unsigned n = cp_tree_operand_length (arg);
1850 for (i = 0; i < n; ++i)
1851 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1852 return val;
1853 }
1854 }
1855 gcc_unreachable ();
1856 return 0;
1857 }
1858
1859 /* Unregister the specialization SPEC as a specialization of TMPL.
1860 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1861 if the SPEC was listed as a specialization of TMPL.
1862
1863 Note that SPEC has been ggc_freed, so we can't look inside it. */
1864
1865 bool
1866 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1867 {
1868 spec_entry *entry;
1869 spec_entry elt;
1870
1871 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1872 elt.args = TI_ARGS (tinfo);
1873 elt.spec = NULL_TREE;
1874
1875 entry = decl_specializations->find (&elt);
1876 if (entry != NULL)
1877 {
1878 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1879 gcc_assert (new_spec != NULL_TREE);
1880 entry->spec = new_spec;
1881 return 1;
1882 }
1883
1884 return 0;
1885 }
1886
1887 /* Like register_specialization, but for local declarations. We are
1888 registering SPEC, an instantiation of TMPL. */
1889
1890 void
1891 register_local_specialization (tree spec, tree tmpl)
1892 {
1893 local_specializations->put (tmpl, spec);
1894 }
1895
1896 /* TYPE is a class type. Returns true if TYPE is an explicitly
1897 specialized class. */
1898
1899 bool
1900 explicit_class_specialization_p (tree type)
1901 {
1902 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1903 return false;
1904 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1905 }
1906
1907 /* Print the list of functions at FNS, going through all the overloads
1908 for each element of the list. Alternatively, FNS can not be a
1909 TREE_LIST, in which case it will be printed together with all the
1910 overloads.
1911
1912 MORE and *STR should respectively be FALSE and NULL when the function
1913 is called from the outside. They are used internally on recursive
1914 calls. print_candidates manages the two parameters and leaves NULL
1915 in *STR when it ends. */
1916
1917 static void
1918 print_candidates_1 (tree fns, bool more, const char **str)
1919 {
1920 tree fn, fn2;
1921 char *spaces = NULL;
1922
1923 for (fn = fns; fn; fn = OVL_NEXT (fn))
1924 if (TREE_CODE (fn) == TREE_LIST)
1925 {
1926 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1927 print_candidates_1 (TREE_VALUE (fn2),
1928 TREE_CHAIN (fn2) || more, str);
1929 }
1930 else
1931 {
1932 tree cand = OVL_CURRENT (fn);
1933 if (!*str)
1934 {
1935 /* Pick the prefix string. */
1936 if (!more && !OVL_NEXT (fns))
1937 {
1938 inform (DECL_SOURCE_LOCATION (cand),
1939 "candidate is: %#D", cand);
1940 continue;
1941 }
1942
1943 *str = _("candidates are:");
1944 spaces = get_spaces (*str);
1945 }
1946 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1947 *str = spaces ? spaces : *str;
1948 }
1949
1950 if (!more)
1951 {
1952 free (spaces);
1953 *str = NULL;
1954 }
1955 }
1956
1957 /* Print the list of candidate FNS in an error message. FNS can also
1958 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1959
1960 void
1961 print_candidates (tree fns)
1962 {
1963 const char *str = NULL;
1964 print_candidates_1 (fns, false, &str);
1965 gcc_assert (str == NULL);
1966 }
1967
1968 /* Get a (possibly) constrained template declaration for the
1969 purpose of ordering candidates. */
1970 static tree
1971 get_template_for_ordering (tree list)
1972 {
1973 gcc_assert (TREE_CODE (list) == TREE_LIST);
1974 tree f = TREE_VALUE (list);
1975 if (tree ti = DECL_TEMPLATE_INFO (f))
1976 return TI_TEMPLATE (ti);
1977 return f;
1978 }
1979
1980 /* Among candidates having the same signature, return the
1981 most constrained or NULL_TREE if there is no best candidate.
1982 If the signatures of candidates vary (e.g., template
1983 specialization vs. member function), then there can be no
1984 most constrained.
1985
1986 Note that we don't compare constraints on the functions
1987 themselves, but rather those of their templates. */
1988 static tree
1989 most_constrained_function (tree candidates)
1990 {
1991 // Try to find the best candidate in a first pass.
1992 tree champ = candidates;
1993 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1994 {
1995 int winner = more_constrained (get_template_for_ordering (champ),
1996 get_template_for_ordering (c));
1997 if (winner == -1)
1998 champ = c; // The candidate is more constrained
1999 else if (winner == 0)
2000 return NULL_TREE; // Neither is more constrained
2001 }
2002
2003 // Verify that the champ is better than previous candidates.
2004 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2005 if (!more_constrained (get_template_for_ordering (champ),
2006 get_template_for_ordering (c)))
2007 return NULL_TREE;
2008 }
2009
2010 return champ;
2011 }
2012
2013
2014 /* Returns the template (one of the functions given by TEMPLATE_ID)
2015 which can be specialized to match the indicated DECL with the
2016 explicit template args given in TEMPLATE_ID. The DECL may be
2017 NULL_TREE if none is available. In that case, the functions in
2018 TEMPLATE_ID are non-members.
2019
2020 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2021 specialization of a member template.
2022
2023 The TEMPLATE_COUNT is the number of references to qualifying
2024 template classes that appeared in the name of the function. See
2025 check_explicit_specialization for a more accurate description.
2026
2027 TSK indicates what kind of template declaration (if any) is being
2028 declared. TSK_TEMPLATE indicates that the declaration given by
2029 DECL, though a FUNCTION_DECL, has template parameters, and is
2030 therefore a template function.
2031
2032 The template args (those explicitly specified and those deduced)
2033 are output in a newly created vector *TARGS_OUT.
2034
2035 If it is impossible to determine the result, an error message is
2036 issued. The error_mark_node is returned to indicate failure. */
2037
2038 static tree
2039 determine_specialization (tree template_id,
2040 tree decl,
2041 tree* targs_out,
2042 int need_member_template,
2043 int template_count,
2044 tmpl_spec_kind tsk)
2045 {
2046 tree fns;
2047 tree targs;
2048 tree explicit_targs;
2049 tree candidates = NULL_TREE;
2050
2051 /* A TREE_LIST of templates of which DECL may be a specialization.
2052 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2053 corresponding TREE_PURPOSE is the set of template arguments that,
2054 when used to instantiate the template, would produce a function
2055 with the signature of DECL. */
2056 tree templates = NULL_TREE;
2057 int header_count;
2058 cp_binding_level *b;
2059
2060 *targs_out = NULL_TREE;
2061
2062 if (template_id == error_mark_node || decl == error_mark_node)
2063 return error_mark_node;
2064
2065 /* We shouldn't be specializing a member template of an
2066 unspecialized class template; we already gave an error in
2067 check_specialization_scope, now avoid crashing. */
2068 if (template_count && DECL_CLASS_SCOPE_P (decl)
2069 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2070 {
2071 gcc_assert (errorcount);
2072 return error_mark_node;
2073 }
2074
2075 fns = TREE_OPERAND (template_id, 0);
2076 explicit_targs = TREE_OPERAND (template_id, 1);
2077
2078 if (fns == error_mark_node)
2079 return error_mark_node;
2080
2081 /* Check for baselinks. */
2082 if (BASELINK_P (fns))
2083 fns = BASELINK_FUNCTIONS (fns);
2084
2085 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2086 {
2087 error ("%qD is not a function template", fns);
2088 return error_mark_node;
2089 }
2090 else if (VAR_P (decl) && !variable_template_p (fns))
2091 {
2092 error ("%qD is not a variable template", fns);
2093 return error_mark_node;
2094 }
2095
2096 /* Count the number of template headers specified for this
2097 specialization. */
2098 header_count = 0;
2099 for (b = current_binding_level;
2100 b->kind == sk_template_parms;
2101 b = b->level_chain)
2102 ++header_count;
2103
2104 tree orig_fns = fns;
2105
2106 if (variable_template_p (fns))
2107 {
2108 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2109 targs = coerce_template_parms (parms, explicit_targs, fns,
2110 tf_warning_or_error,
2111 /*req_all*/true, /*use_defarg*/true);
2112 if (targs != error_mark_node)
2113 templates = tree_cons (targs, fns, templates);
2114 }
2115 else for (; fns; fns = OVL_NEXT (fns))
2116 {
2117 tree fn = OVL_CURRENT (fns);
2118
2119 if (TREE_CODE (fn) == TEMPLATE_DECL)
2120 {
2121 tree decl_arg_types;
2122 tree fn_arg_types;
2123 tree insttype;
2124
2125 /* In case of explicit specialization, we need to check if
2126 the number of template headers appearing in the specialization
2127 is correct. This is usually done in check_explicit_specialization,
2128 but the check done there cannot be exhaustive when specializing
2129 member functions. Consider the following code:
2130
2131 template <> void A<int>::f(int);
2132 template <> template <> void A<int>::f(int);
2133
2134 Assuming that A<int> is not itself an explicit specialization
2135 already, the first line specializes "f" which is a non-template
2136 member function, whilst the second line specializes "f" which
2137 is a template member function. So both lines are syntactically
2138 correct, and check_explicit_specialization does not reject
2139 them.
2140
2141 Here, we can do better, as we are matching the specialization
2142 against the declarations. We count the number of template
2143 headers, and we check if they match TEMPLATE_COUNT + 1
2144 (TEMPLATE_COUNT is the number of qualifying template classes,
2145 plus there must be another header for the member template
2146 itself).
2147
2148 Notice that if header_count is zero, this is not a
2149 specialization but rather a template instantiation, so there
2150 is no check we can perform here. */
2151 if (header_count && header_count != template_count + 1)
2152 continue;
2153
2154 /* Check that the number of template arguments at the
2155 innermost level for DECL is the same as for FN. */
2156 if (current_binding_level->kind == sk_template_parms
2157 && !current_binding_level->explicit_spec_p
2158 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2159 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2160 (current_template_parms))))
2161 continue;
2162
2163 /* DECL might be a specialization of FN. */
2164 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2165 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2166
2167 /* For a non-static member function, we need to make sure
2168 that the const qualification is the same. Since
2169 get_bindings does not try to merge the "this" parameter,
2170 we must do the comparison explicitly. */
2171 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2172 && !same_type_p (TREE_VALUE (fn_arg_types),
2173 TREE_VALUE (decl_arg_types)))
2174 continue;
2175
2176 /* Skip the "this" parameter and, for constructors of
2177 classes with virtual bases, the VTT parameter. A
2178 full specialization of a constructor will have a VTT
2179 parameter, but a template never will. */
2180 decl_arg_types
2181 = skip_artificial_parms_for (decl, decl_arg_types);
2182 fn_arg_types
2183 = skip_artificial_parms_for (fn, fn_arg_types);
2184
2185 /* Function templates cannot be specializations; there are
2186 no partial specializations of functions. Therefore, if
2187 the type of DECL does not match FN, there is no
2188 match.
2189
2190 Note that it should never be the case that we have both
2191 candidates added here, and for regular member functions
2192 below. */
2193 if (tsk == tsk_template)
2194 {
2195 if (compparms (fn_arg_types, decl_arg_types))
2196 candidates = tree_cons (NULL_TREE, fn, candidates);
2197 continue;
2198 }
2199
2200 /* See whether this function might be a specialization of this
2201 template. Suppress access control because we might be trying
2202 to make this specialization a friend, and we have already done
2203 access control for the declaration of the specialization. */
2204 push_deferring_access_checks (dk_no_check);
2205 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2206 pop_deferring_access_checks ();
2207
2208 if (!targs)
2209 /* We cannot deduce template arguments that when used to
2210 specialize TMPL will produce DECL. */
2211 continue;
2212
2213 /* Remove, from the set of candidates, all those functions
2214 whose constraints are not satisfied. */
2215 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2216 continue;
2217
2218 // Then, try to form the new function type.
2219 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2220 if (insttype == error_mark_node)
2221 continue;
2222 fn_arg_types
2223 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2224 if (!compparms (fn_arg_types, decl_arg_types))
2225 continue;
2226
2227 /* Save this template, and the arguments deduced. */
2228 templates = tree_cons (targs, fn, templates);
2229 }
2230 else if (need_member_template)
2231 /* FN is an ordinary member function, and we need a
2232 specialization of a member template. */
2233 ;
2234 else if (TREE_CODE (fn) != FUNCTION_DECL)
2235 /* We can get IDENTIFIER_NODEs here in certain erroneous
2236 cases. */
2237 ;
2238 else if (!DECL_FUNCTION_MEMBER_P (fn))
2239 /* This is just an ordinary non-member function. Nothing can
2240 be a specialization of that. */
2241 ;
2242 else if (DECL_ARTIFICIAL (fn))
2243 /* Cannot specialize functions that are created implicitly. */
2244 ;
2245 else
2246 {
2247 tree decl_arg_types;
2248
2249 /* This is an ordinary member function. However, since
2250 we're here, we can assume its enclosing class is a
2251 template class. For example,
2252
2253 template <typename T> struct S { void f(); };
2254 template <> void S<int>::f() {}
2255
2256 Here, S<int>::f is a non-template, but S<int> is a
2257 template class. If FN has the same type as DECL, we
2258 might be in business. */
2259
2260 if (!DECL_TEMPLATE_INFO (fn))
2261 /* Its enclosing class is an explicit specialization
2262 of a template class. This is not a candidate. */
2263 continue;
2264
2265 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2266 TREE_TYPE (TREE_TYPE (fn))))
2267 /* The return types differ. */
2268 continue;
2269
2270 /* Adjust the type of DECL in case FN is a static member. */
2271 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2272 if (DECL_STATIC_FUNCTION_P (fn)
2273 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2274 decl_arg_types = TREE_CHAIN (decl_arg_types);
2275
2276 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2277 decl_arg_types))
2278 continue;
2279
2280 // If the deduced arguments do not satisfy the constraints,
2281 // this is not a candidate.
2282 if (flag_concepts && !constraints_satisfied_p (fn))
2283 continue;
2284
2285 // Add the candidate.
2286 candidates = tree_cons (NULL_TREE, fn, candidates);
2287 }
2288 }
2289
2290 if (templates && TREE_CHAIN (templates))
2291 {
2292 /* We have:
2293
2294 [temp.expl.spec]
2295
2296 It is possible for a specialization with a given function
2297 signature to be instantiated from more than one function
2298 template. In such cases, explicit specification of the
2299 template arguments must be used to uniquely identify the
2300 function template specialization being specialized.
2301
2302 Note that here, there's no suggestion that we're supposed to
2303 determine which of the candidate templates is most
2304 specialized. However, we, also have:
2305
2306 [temp.func.order]
2307
2308 Partial ordering of overloaded function template
2309 declarations is used in the following contexts to select
2310 the function template to which a function template
2311 specialization refers:
2312
2313 -- when an explicit specialization refers to a function
2314 template.
2315
2316 So, we do use the partial ordering rules, at least for now.
2317 This extension can only serve to make invalid programs valid,
2318 so it's safe. And, there is strong anecdotal evidence that
2319 the committee intended the partial ordering rules to apply;
2320 the EDG front end has that behavior, and John Spicer claims
2321 that the committee simply forgot to delete the wording in
2322 [temp.expl.spec]. */
2323 tree tmpl = most_specialized_instantiation (templates);
2324 if (tmpl != error_mark_node)
2325 {
2326 templates = tmpl;
2327 TREE_CHAIN (templates) = NULL_TREE;
2328 }
2329 }
2330
2331 // Concepts allows multiple declarations of member functions
2332 // with the same signature. Like above, we need to rely on
2333 // on the partial ordering of those candidates to determine which
2334 // is the best.
2335 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2336 {
2337 if (tree cand = most_constrained_function (candidates))
2338 {
2339 candidates = cand;
2340 TREE_CHAIN (cand) = NULL_TREE;
2341 }
2342 }
2343
2344 if (templates == NULL_TREE && candidates == NULL_TREE)
2345 {
2346 error ("template-id %qD for %q+D does not match any template "
2347 "declaration", template_id, decl);
2348 if (header_count && header_count != template_count + 1)
2349 inform (input_location, "saw %d %<template<>%>, need %d for "
2350 "specializing a member function template",
2351 header_count, template_count + 1);
2352 else
2353 print_candidates (orig_fns);
2354 return error_mark_node;
2355 }
2356 else if ((templates && TREE_CHAIN (templates))
2357 || (candidates && TREE_CHAIN (candidates))
2358 || (templates && candidates))
2359 {
2360 error ("ambiguous template specialization %qD for %q+D",
2361 template_id, decl);
2362 candidates = chainon (candidates, templates);
2363 print_candidates (candidates);
2364 return error_mark_node;
2365 }
2366
2367 /* We have one, and exactly one, match. */
2368 if (candidates)
2369 {
2370 tree fn = TREE_VALUE (candidates);
2371 *targs_out = copy_node (DECL_TI_ARGS (fn));
2372
2373 // Propagate the candidate's constraints to the declaration.
2374 set_constraints (decl, get_constraints (fn));
2375
2376 /* DECL is a re-declaration or partial instantiation of a template
2377 function. */
2378 if (TREE_CODE (fn) == TEMPLATE_DECL)
2379 return fn;
2380 /* It was a specialization of an ordinary member function in a
2381 template class. */
2382 return DECL_TI_TEMPLATE (fn);
2383 }
2384
2385 /* It was a specialization of a template. */
2386 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2387 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2388 {
2389 *targs_out = copy_node (targs);
2390 SET_TMPL_ARGS_LEVEL (*targs_out,
2391 TMPL_ARGS_DEPTH (*targs_out),
2392 TREE_PURPOSE (templates));
2393 }
2394 else
2395 *targs_out = TREE_PURPOSE (templates);
2396 return TREE_VALUE (templates);
2397 }
2398
2399 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2400 but with the default argument values filled in from those in the
2401 TMPL_TYPES. */
2402
2403 static tree
2404 copy_default_args_to_explicit_spec_1 (tree spec_types,
2405 tree tmpl_types)
2406 {
2407 tree new_spec_types;
2408
2409 if (!spec_types)
2410 return NULL_TREE;
2411
2412 if (spec_types == void_list_node)
2413 return void_list_node;
2414
2415 /* Substitute into the rest of the list. */
2416 new_spec_types =
2417 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2418 TREE_CHAIN (tmpl_types));
2419
2420 /* Add the default argument for this parameter. */
2421 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2422 TREE_VALUE (spec_types),
2423 new_spec_types);
2424 }
2425
2426 /* DECL is an explicit specialization. Replicate default arguments
2427 from the template it specializes. (That way, code like:
2428
2429 template <class T> void f(T = 3);
2430 template <> void f(double);
2431 void g () { f (); }
2432
2433 works, as required.) An alternative approach would be to look up
2434 the correct default arguments at the call-site, but this approach
2435 is consistent with how implicit instantiations are handled. */
2436
2437 static void
2438 copy_default_args_to_explicit_spec (tree decl)
2439 {
2440 tree tmpl;
2441 tree spec_types;
2442 tree tmpl_types;
2443 tree new_spec_types;
2444 tree old_type;
2445 tree new_type;
2446 tree t;
2447 tree object_type = NULL_TREE;
2448 tree in_charge = NULL_TREE;
2449 tree vtt = NULL_TREE;
2450
2451 /* See if there's anything we need to do. */
2452 tmpl = DECL_TI_TEMPLATE (decl);
2453 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2454 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2455 if (TREE_PURPOSE (t))
2456 break;
2457 if (!t)
2458 return;
2459
2460 old_type = TREE_TYPE (decl);
2461 spec_types = TYPE_ARG_TYPES (old_type);
2462
2463 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2464 {
2465 /* Remove the this pointer, but remember the object's type for
2466 CV quals. */
2467 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2468 spec_types = TREE_CHAIN (spec_types);
2469 tmpl_types = TREE_CHAIN (tmpl_types);
2470
2471 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2472 {
2473 /* DECL may contain more parameters than TMPL due to the extra
2474 in-charge parameter in constructors and destructors. */
2475 in_charge = spec_types;
2476 spec_types = TREE_CHAIN (spec_types);
2477 }
2478 if (DECL_HAS_VTT_PARM_P (decl))
2479 {
2480 vtt = spec_types;
2481 spec_types = TREE_CHAIN (spec_types);
2482 }
2483 }
2484
2485 /* Compute the merged default arguments. */
2486 new_spec_types =
2487 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2488
2489 /* Compute the new FUNCTION_TYPE. */
2490 if (object_type)
2491 {
2492 if (vtt)
2493 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2494 TREE_VALUE (vtt),
2495 new_spec_types);
2496
2497 if (in_charge)
2498 /* Put the in-charge parameter back. */
2499 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2500 TREE_VALUE (in_charge),
2501 new_spec_types);
2502
2503 new_type = build_method_type_directly (object_type,
2504 TREE_TYPE (old_type),
2505 new_spec_types);
2506 }
2507 else
2508 new_type = build_function_type (TREE_TYPE (old_type),
2509 new_spec_types);
2510 new_type = cp_build_type_attribute_variant (new_type,
2511 TYPE_ATTRIBUTES (old_type));
2512 new_type = build_exception_variant (new_type,
2513 TYPE_RAISES_EXCEPTIONS (old_type));
2514
2515 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2516 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2517
2518 TREE_TYPE (decl) = new_type;
2519 }
2520
2521 /* Return the number of template headers we expect to see for a definition
2522 or specialization of CTYPE or one of its non-template members. */
2523
2524 int
2525 num_template_headers_for_class (tree ctype)
2526 {
2527 int num_templates = 0;
2528
2529 while (ctype && CLASS_TYPE_P (ctype))
2530 {
2531 /* You're supposed to have one `template <...>' for every
2532 template class, but you don't need one for a full
2533 specialization. For example:
2534
2535 template <class T> struct S{};
2536 template <> struct S<int> { void f(); };
2537 void S<int>::f () {}
2538
2539 is correct; there shouldn't be a `template <>' for the
2540 definition of `S<int>::f'. */
2541 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2542 /* If CTYPE does not have template information of any
2543 kind, then it is not a template, nor is it nested
2544 within a template. */
2545 break;
2546 if (explicit_class_specialization_p (ctype))
2547 break;
2548 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2549 ++num_templates;
2550
2551 ctype = TYPE_CONTEXT (ctype);
2552 }
2553
2554 return num_templates;
2555 }
2556
2557 /* Do a simple sanity check on the template headers that precede the
2558 variable declaration DECL. */
2559
2560 void
2561 check_template_variable (tree decl)
2562 {
2563 tree ctx = CP_DECL_CONTEXT (decl);
2564 int wanted = num_template_headers_for_class (ctx);
2565 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2566 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2567 {
2568 if (cxx_dialect < cxx14)
2569 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2570 "variable templates only available with "
2571 "-std=c++14 or -std=gnu++14");
2572
2573 // Namespace-scope variable templates should have a template header.
2574 ++wanted;
2575 }
2576 if (template_header_count > wanted)
2577 {
2578 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2579 "too many template headers for %D (should be %d)",
2580 decl, wanted);
2581 if (warned && CLASS_TYPE_P (ctx)
2582 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2583 inform (DECL_SOURCE_LOCATION (decl),
2584 "members of an explicitly specialized class are defined "
2585 "without a template header");
2586 }
2587 }
2588
2589 /* Check to see if the function just declared, as indicated in
2590 DECLARATOR, and in DECL, is a specialization of a function
2591 template. We may also discover that the declaration is an explicit
2592 instantiation at this point.
2593
2594 Returns DECL, or an equivalent declaration that should be used
2595 instead if all goes well. Issues an error message if something is
2596 amiss. Returns error_mark_node if the error is not easily
2597 recoverable.
2598
2599 FLAGS is a bitmask consisting of the following flags:
2600
2601 2: The function has a definition.
2602 4: The function is a friend.
2603
2604 The TEMPLATE_COUNT is the number of references to qualifying
2605 template classes that appeared in the name of the function. For
2606 example, in
2607
2608 template <class T> struct S { void f(); };
2609 void S<int>::f();
2610
2611 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2612 classes are not counted in the TEMPLATE_COUNT, so that in
2613
2614 template <class T> struct S {};
2615 template <> struct S<int> { void f(); }
2616 template <> void S<int>::f();
2617
2618 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2619 invalid; there should be no template <>.)
2620
2621 If the function is a specialization, it is marked as such via
2622 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2623 is set up correctly, and it is added to the list of specializations
2624 for that template. */
2625
2626 tree
2627 check_explicit_specialization (tree declarator,
2628 tree decl,
2629 int template_count,
2630 int flags)
2631 {
2632 int have_def = flags & 2;
2633 int is_friend = flags & 4;
2634 bool is_concept = flags & 8;
2635 int specialization = 0;
2636 int explicit_instantiation = 0;
2637 int member_specialization = 0;
2638 tree ctype = DECL_CLASS_CONTEXT (decl);
2639 tree dname = DECL_NAME (decl);
2640 tmpl_spec_kind tsk;
2641
2642 if (is_friend)
2643 {
2644 if (!processing_specialization)
2645 tsk = tsk_none;
2646 else
2647 tsk = tsk_excessive_parms;
2648 }
2649 else
2650 tsk = current_tmpl_spec_kind (template_count);
2651
2652 switch (tsk)
2653 {
2654 case tsk_none:
2655 if (processing_specialization && !VAR_P (decl))
2656 {
2657 specialization = 1;
2658 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2659 }
2660 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2661 {
2662 if (is_friend)
2663 /* This could be something like:
2664
2665 template <class T> void f(T);
2666 class S { friend void f<>(int); } */
2667 specialization = 1;
2668 else
2669 {
2670 /* This case handles bogus declarations like template <>
2671 template <class T> void f<int>(); */
2672
2673 error ("template-id %qD in declaration of primary template",
2674 declarator);
2675 return decl;
2676 }
2677 }
2678 break;
2679
2680 case tsk_invalid_member_spec:
2681 /* The error has already been reported in
2682 check_specialization_scope. */
2683 return error_mark_node;
2684
2685 case tsk_invalid_expl_inst:
2686 error ("template parameter list used in explicit instantiation");
2687
2688 /* Fall through. */
2689
2690 case tsk_expl_inst:
2691 if (have_def)
2692 error ("definition provided for explicit instantiation");
2693
2694 explicit_instantiation = 1;
2695 break;
2696
2697 case tsk_excessive_parms:
2698 case tsk_insufficient_parms:
2699 if (tsk == tsk_excessive_parms)
2700 error ("too many template parameter lists in declaration of %qD",
2701 decl);
2702 else if (template_header_count)
2703 error("too few template parameter lists in declaration of %qD", decl);
2704 else
2705 error("explicit specialization of %qD must be introduced by "
2706 "%<template <>%>", decl);
2707
2708 /* Fall through. */
2709 case tsk_expl_spec:
2710 if (is_concept)
2711 error ("explicit specialization declared %<concept%>");
2712
2713 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2714 /* In cases like template<> constexpr bool v = true;
2715 We'll give an error in check_template_variable. */
2716 break;
2717
2718 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2719 if (ctype)
2720 member_specialization = 1;
2721 else
2722 specialization = 1;
2723 break;
2724
2725 case tsk_template:
2726 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2727 {
2728 /* This case handles bogus declarations like template <>
2729 template <class T> void f<int>(); */
2730
2731 if (!uses_template_parms (declarator))
2732 error ("template-id %qD in declaration of primary template",
2733 declarator);
2734 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2735 {
2736 /* Partial specialization of variable template. */
2737 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2738 specialization = 1;
2739 goto ok;
2740 }
2741 else if (cxx_dialect < cxx14)
2742 error ("non-type partial specialization %qD "
2743 "is not allowed", declarator);
2744 else
2745 error ("non-class, non-variable partial specialization %qD "
2746 "is not allowed", declarator);
2747 return decl;
2748 ok:;
2749 }
2750
2751 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2752 /* This is a specialization of a member template, without
2753 specialization the containing class. Something like:
2754
2755 template <class T> struct S {
2756 template <class U> void f (U);
2757 };
2758 template <> template <class U> void S<int>::f(U) {}
2759
2760 That's a specialization -- but of the entire template. */
2761 specialization = 1;
2762 break;
2763
2764 default:
2765 gcc_unreachable ();
2766 }
2767
2768 if ((specialization || member_specialization)
2769 /* This doesn't apply to variable templates. */
2770 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2771 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2772 {
2773 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2774 for (; t; t = TREE_CHAIN (t))
2775 if (TREE_PURPOSE (t))
2776 {
2777 permerror (input_location,
2778 "default argument specified in explicit specialization");
2779 break;
2780 }
2781 }
2782
2783 if (specialization || member_specialization || explicit_instantiation)
2784 {
2785 tree tmpl = NULL_TREE;
2786 tree targs = NULL_TREE;
2787 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2788
2789 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2790 if (!was_template_id)
2791 {
2792 tree fns;
2793
2794 gcc_assert (identifier_p (declarator));
2795 if (ctype)
2796 fns = dname;
2797 else
2798 {
2799 /* If there is no class context, the explicit instantiation
2800 must be at namespace scope. */
2801 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2802
2803 /* Find the namespace binding, using the declaration
2804 context. */
2805 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2806 false, true);
2807 if (fns == error_mark_node)
2808 /* If lookup fails, look for a friend declaration so we can
2809 give a better diagnostic. */
2810 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2811 /*type*/false, /*complain*/true,
2812 /*hidden*/true);
2813
2814 if (fns == error_mark_node || !is_overloaded_fn (fns))
2815 {
2816 error ("%qD is not a template function", dname);
2817 fns = error_mark_node;
2818 }
2819 }
2820
2821 declarator = lookup_template_function (fns, NULL_TREE);
2822 }
2823
2824 if (declarator == error_mark_node)
2825 return error_mark_node;
2826
2827 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2828 {
2829 if (!explicit_instantiation)
2830 /* A specialization in class scope. This is invalid,
2831 but the error will already have been flagged by
2832 check_specialization_scope. */
2833 return error_mark_node;
2834 else
2835 {
2836 /* It's not valid to write an explicit instantiation in
2837 class scope, e.g.:
2838
2839 class C { template void f(); }
2840
2841 This case is caught by the parser. However, on
2842 something like:
2843
2844 template class C { void f(); };
2845
2846 (which is invalid) we can get here. The error will be
2847 issued later. */
2848 ;
2849 }
2850
2851 return decl;
2852 }
2853 else if (ctype != NULL_TREE
2854 && (identifier_p (TREE_OPERAND (declarator, 0))))
2855 {
2856 // We'll match variable templates in start_decl.
2857 if (VAR_P (decl))
2858 return decl;
2859
2860 /* Find the list of functions in ctype that have the same
2861 name as the declared function. */
2862 tree name = TREE_OPERAND (declarator, 0);
2863 tree fns = NULL_TREE;
2864 int idx;
2865
2866 if (constructor_name_p (name, ctype))
2867 {
2868 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2869
2870 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2871 : !CLASSTYPE_DESTRUCTORS (ctype))
2872 {
2873 /* From [temp.expl.spec]:
2874
2875 If such an explicit specialization for the member
2876 of a class template names an implicitly-declared
2877 special member function (clause _special_), the
2878 program is ill-formed.
2879
2880 Similar language is found in [temp.explicit]. */
2881 error ("specialization of implicitly-declared special member function");
2882 return error_mark_node;
2883 }
2884
2885 name = is_constructor ? ctor_identifier : dtor_identifier;
2886 }
2887
2888 if (!DECL_CONV_FN_P (decl))
2889 {
2890 idx = lookup_fnfields_1 (ctype, name);
2891 if (idx >= 0)
2892 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2893 }
2894 else
2895 {
2896 vec<tree, va_gc> *methods;
2897 tree ovl;
2898
2899 /* For a type-conversion operator, we cannot do a
2900 name-based lookup. We might be looking for `operator
2901 int' which will be a specialization of `operator T'.
2902 So, we find *all* the conversion operators, and then
2903 select from them. */
2904 fns = NULL_TREE;
2905
2906 methods = CLASSTYPE_METHOD_VEC (ctype);
2907 if (methods)
2908 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2909 methods->iterate (idx, &ovl);
2910 ++idx)
2911 {
2912 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2913 /* There are no more conversion functions. */
2914 break;
2915
2916 /* Glue all these conversion functions together
2917 with those we already have. */
2918 for (; ovl; ovl = OVL_NEXT (ovl))
2919 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2920 }
2921 }
2922
2923 if (fns == NULL_TREE)
2924 {
2925 error ("no member function %qD declared in %qT", name, ctype);
2926 return error_mark_node;
2927 }
2928 else
2929 TREE_OPERAND (declarator, 0) = fns;
2930 }
2931
2932 /* Figure out what exactly is being specialized at this point.
2933 Note that for an explicit instantiation, even one for a
2934 member function, we cannot tell apriori whether the
2935 instantiation is for a member template, or just a member
2936 function of a template class. Even if a member template is
2937 being instantiated, the member template arguments may be
2938 elided if they can be deduced from the rest of the
2939 declaration. */
2940 tmpl = determine_specialization (declarator, decl,
2941 &targs,
2942 member_specialization,
2943 template_count,
2944 tsk);
2945
2946 if (!tmpl || tmpl == error_mark_node)
2947 /* We couldn't figure out what this declaration was
2948 specializing. */
2949 return error_mark_node;
2950 else
2951 {
2952 if (!ctype && !was_template_id
2953 && (specialization || member_specialization
2954 || explicit_instantiation)
2955 && !is_associated_namespace (CP_DECL_CONTEXT (decl),
2956 CP_DECL_CONTEXT (tmpl)))
2957 error ("%qD is not declared in %qD",
2958 tmpl, current_namespace);
2959 else if (TREE_CODE (decl) == FUNCTION_DECL
2960 && DECL_HIDDEN_FRIEND_P (tmpl))
2961 {
2962 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2963 "friend declaration %qD is not visible to "
2964 "explicit specialization", tmpl))
2965 inform (DECL_SOURCE_LOCATION (tmpl),
2966 "friend declaration here");
2967 }
2968
2969 tree gen_tmpl = most_general_template (tmpl);
2970
2971 if (explicit_instantiation)
2972 {
2973 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2974 is done by do_decl_instantiation later. */
2975
2976 int arg_depth = TMPL_ARGS_DEPTH (targs);
2977 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2978
2979 if (arg_depth > parm_depth)
2980 {
2981 /* If TMPL is not the most general template (for
2982 example, if TMPL is a friend template that is
2983 injected into namespace scope), then there will
2984 be too many levels of TARGS. Remove some of them
2985 here. */
2986 int i;
2987 tree new_targs;
2988
2989 new_targs = make_tree_vec (parm_depth);
2990 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2991 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2992 = TREE_VEC_ELT (targs, i);
2993 targs = new_targs;
2994 }
2995
2996 return instantiate_template (tmpl, targs, tf_error);
2997 }
2998
2999 /* If we thought that the DECL was a member function, but it
3000 turns out to be specializing a static member function,
3001 make DECL a static member function as well. */
3002 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3003 && DECL_STATIC_FUNCTION_P (tmpl)
3004 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3005 revert_static_member_fn (decl);
3006
3007 /* If this is a specialization of a member template of a
3008 template class, we want to return the TEMPLATE_DECL, not
3009 the specialization of it. */
3010 if (tsk == tsk_template && !was_template_id)
3011 {
3012 tree result = DECL_TEMPLATE_RESULT (tmpl);
3013 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3014 DECL_INITIAL (result) = NULL_TREE;
3015 if (have_def)
3016 {
3017 tree parm;
3018 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3019 DECL_SOURCE_LOCATION (result)
3020 = DECL_SOURCE_LOCATION (decl);
3021 /* We want to use the argument list specified in the
3022 definition, not in the original declaration. */
3023 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3024 for (parm = DECL_ARGUMENTS (result); parm;
3025 parm = DECL_CHAIN (parm))
3026 DECL_CONTEXT (parm) = result;
3027 }
3028 return register_specialization (tmpl, gen_tmpl, targs,
3029 is_friend, 0);
3030 }
3031
3032 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3033 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3034
3035 if (was_template_id)
3036 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3037
3038 /* Inherit default function arguments from the template
3039 DECL is specializing. */
3040 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3041 copy_default_args_to_explicit_spec (decl);
3042
3043 /* This specialization has the same protection as the
3044 template it specializes. */
3045 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3046 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3047
3048 /* 7.1.1-1 [dcl.stc]
3049
3050 A storage-class-specifier shall not be specified in an
3051 explicit specialization...
3052
3053 The parser rejects these, so unless action is taken here,
3054 explicit function specializations will always appear with
3055 global linkage.
3056
3057 The action recommended by the C++ CWG in response to C++
3058 defect report 605 is to make the storage class and linkage
3059 of the explicit specialization match the templated function:
3060
3061 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3062 */
3063 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3064 {
3065 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3066 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3067
3068 /* A concept cannot be specialized. */
3069 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3070 {
3071 error ("explicit specialization of function concept %qD",
3072 gen_tmpl);
3073 return error_mark_node;
3074 }
3075
3076 /* This specialization has the same linkage and visibility as
3077 the function template it specializes. */
3078 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3079 if (! TREE_PUBLIC (decl))
3080 {
3081 DECL_INTERFACE_KNOWN (decl) = 1;
3082 DECL_NOT_REALLY_EXTERN (decl) = 1;
3083 }
3084 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3085 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3086 {
3087 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3088 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3089 }
3090 }
3091
3092 /* If DECL is a friend declaration, declared using an
3093 unqualified name, the namespace associated with DECL may
3094 have been set incorrectly. For example, in:
3095
3096 template <typename T> void f(T);
3097 namespace N {
3098 struct S { friend void f<int>(int); }
3099 }
3100
3101 we will have set the DECL_CONTEXT for the friend
3102 declaration to N, rather than to the global namespace. */
3103 if (DECL_NAMESPACE_SCOPE_P (decl))
3104 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3105
3106 if (is_friend && !have_def)
3107 /* This is not really a declaration of a specialization.
3108 It's just the name of an instantiation. But, it's not
3109 a request for an instantiation, either. */
3110 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3111 else if (TREE_CODE (decl) == FUNCTION_DECL)
3112 /* A specialization is not necessarily COMDAT. */
3113 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3114 && DECL_DECLARED_INLINE_P (decl));
3115 else if (VAR_P (decl))
3116 DECL_COMDAT (decl) = false;
3117
3118 /* If this is a full specialization, register it so that we can find
3119 it again. Partial specializations will be registered in
3120 process_partial_specialization. */
3121 if (!processing_template_decl)
3122 decl = register_specialization (decl, gen_tmpl, targs,
3123 is_friend, 0);
3124
3125 /* A 'structor should already have clones. */
3126 gcc_assert (decl == error_mark_node
3127 || variable_template_p (tmpl)
3128 || !(DECL_CONSTRUCTOR_P (decl)
3129 || DECL_DESTRUCTOR_P (decl))
3130 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3131 }
3132 }
3133
3134 return decl;
3135 }
3136
3137 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3138 parameters. These are represented in the same format used for
3139 DECL_TEMPLATE_PARMS. */
3140
3141 int
3142 comp_template_parms (const_tree parms1, const_tree parms2)
3143 {
3144 const_tree p1;
3145 const_tree p2;
3146
3147 if (parms1 == parms2)
3148 return 1;
3149
3150 for (p1 = parms1, p2 = parms2;
3151 p1 != NULL_TREE && p2 != NULL_TREE;
3152 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3153 {
3154 tree t1 = TREE_VALUE (p1);
3155 tree t2 = TREE_VALUE (p2);
3156 int i;
3157
3158 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3159 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3160
3161 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3162 return 0;
3163
3164 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3165 {
3166 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3167 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3168
3169 /* If either of the template parameters are invalid, assume
3170 they match for the sake of error recovery. */
3171 if (error_operand_p (parm1) || error_operand_p (parm2))
3172 return 1;
3173
3174 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3175 return 0;
3176
3177 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3178 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3179 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3180 continue;
3181 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3182 return 0;
3183 }
3184 }
3185
3186 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3187 /* One set of parameters has more parameters lists than the
3188 other. */
3189 return 0;
3190
3191 return 1;
3192 }
3193
3194 /* Determine whether PARM is a parameter pack. */
3195
3196 bool
3197 template_parameter_pack_p (const_tree parm)
3198 {
3199 /* Determine if we have a non-type template parameter pack. */
3200 if (TREE_CODE (parm) == PARM_DECL)
3201 return (DECL_TEMPLATE_PARM_P (parm)
3202 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3203 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3204 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3205
3206 /* If this is a list of template parameters, we could get a
3207 TYPE_DECL or a TEMPLATE_DECL. */
3208 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3209 parm = TREE_TYPE (parm);
3210
3211 /* Otherwise it must be a type template parameter. */
3212 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3213 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3214 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3215 }
3216
3217 /* Determine if T is a function parameter pack. */
3218
3219 bool
3220 function_parameter_pack_p (const_tree t)
3221 {
3222 if (t && TREE_CODE (t) == PARM_DECL)
3223 return DECL_PACK_P (t);
3224 return false;
3225 }
3226
3227 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3228 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3229
3230 tree
3231 get_function_template_decl (const_tree primary_func_tmpl_inst)
3232 {
3233 if (! primary_func_tmpl_inst
3234 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3235 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3236 return NULL;
3237
3238 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3239 }
3240
3241 /* Return true iff the function parameter PARAM_DECL was expanded
3242 from the function parameter pack PACK. */
3243
3244 bool
3245 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3246 {
3247 if (DECL_ARTIFICIAL (param_decl)
3248 || !function_parameter_pack_p (pack))
3249 return false;
3250
3251 /* The parameter pack and its pack arguments have the same
3252 DECL_PARM_INDEX. */
3253 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3254 }
3255
3256 /* Determine whether ARGS describes a variadic template args list,
3257 i.e., one that is terminated by a template argument pack. */
3258
3259 static bool
3260 template_args_variadic_p (tree args)
3261 {
3262 int nargs;
3263 tree last_parm;
3264
3265 if (args == NULL_TREE)
3266 return false;
3267
3268 args = INNERMOST_TEMPLATE_ARGS (args);
3269 nargs = TREE_VEC_LENGTH (args);
3270
3271 if (nargs == 0)
3272 return false;
3273
3274 last_parm = TREE_VEC_ELT (args, nargs - 1);
3275
3276 return ARGUMENT_PACK_P (last_parm);
3277 }
3278
3279 /* Generate a new name for the parameter pack name NAME (an
3280 IDENTIFIER_NODE) that incorporates its */
3281
3282 static tree
3283 make_ith_pack_parameter_name (tree name, int i)
3284 {
3285 /* Munge the name to include the parameter index. */
3286 #define NUMBUF_LEN 128
3287 char numbuf[NUMBUF_LEN];
3288 char* newname;
3289 int newname_len;
3290
3291 if (name == NULL_TREE)
3292 return name;
3293 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3294 newname_len = IDENTIFIER_LENGTH (name)
3295 + strlen (numbuf) + 2;
3296 newname = (char*)alloca (newname_len);
3297 snprintf (newname, newname_len,
3298 "%s#%i", IDENTIFIER_POINTER (name), i);
3299 return get_identifier (newname);
3300 }
3301
3302 /* Return true if T is a primary function, class or alias template
3303 instantiation. */
3304
3305 bool
3306 primary_template_instantiation_p (const_tree t)
3307 {
3308 if (!t)
3309 return false;
3310
3311 if (TREE_CODE (t) == FUNCTION_DECL)
3312 return DECL_LANG_SPECIFIC (t)
3313 && DECL_TEMPLATE_INSTANTIATION (t)
3314 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3315 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3316 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3317 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3318 else if (alias_template_specialization_p (t))
3319 return true;
3320 return false;
3321 }
3322
3323 /* Return true if PARM is a template template parameter. */
3324
3325 bool
3326 template_template_parameter_p (const_tree parm)
3327 {
3328 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3329 }
3330
3331 /* Return true iff PARM is a DECL representing a type template
3332 parameter. */
3333
3334 bool
3335 template_type_parameter_p (const_tree parm)
3336 {
3337 return (parm
3338 && (TREE_CODE (parm) == TYPE_DECL
3339 || TREE_CODE (parm) == TEMPLATE_DECL)
3340 && DECL_TEMPLATE_PARM_P (parm));
3341 }
3342
3343 /* Return the template parameters of T if T is a
3344 primary template instantiation, NULL otherwise. */
3345
3346 tree
3347 get_primary_template_innermost_parameters (const_tree t)
3348 {
3349 tree parms = NULL, template_info = NULL;
3350
3351 if ((template_info = get_template_info (t))
3352 && primary_template_instantiation_p (t))
3353 parms = INNERMOST_TEMPLATE_PARMS
3354 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3355
3356 return parms;
3357 }
3358
3359 /* Return the template parameters of the LEVELth level from the full list
3360 of template parameters PARMS. */
3361
3362 tree
3363 get_template_parms_at_level (tree parms, int level)
3364 {
3365 tree p;
3366 if (!parms
3367 || TREE_CODE (parms) != TREE_LIST
3368 || level > TMPL_PARMS_DEPTH (parms))
3369 return NULL_TREE;
3370
3371 for (p = parms; p; p = TREE_CHAIN (p))
3372 if (TMPL_PARMS_DEPTH (p) == level)
3373 return p;
3374
3375 return NULL_TREE;
3376 }
3377
3378 /* Returns the template arguments of T if T is a template instantiation,
3379 NULL otherwise. */
3380
3381 tree
3382 get_template_innermost_arguments (const_tree t)
3383 {
3384 tree args = NULL, template_info = NULL;
3385
3386 if ((template_info = get_template_info (t))
3387 && TI_ARGS (template_info))
3388 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3389
3390 return args;
3391 }
3392
3393 /* Return the argument pack elements of T if T is a template argument pack,
3394 NULL otherwise. */
3395
3396 tree
3397 get_template_argument_pack_elems (const_tree t)
3398 {
3399 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3400 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3401 return NULL;
3402
3403 return ARGUMENT_PACK_ARGS (t);
3404 }
3405
3406 /* Structure used to track the progress of find_parameter_packs_r. */
3407 struct find_parameter_pack_data
3408 {
3409 /* TREE_LIST that will contain all of the parameter packs found by
3410 the traversal. */
3411 tree* parameter_packs;
3412
3413 /* Set of AST nodes that have been visited by the traversal. */
3414 hash_set<tree> *visited;
3415
3416 /* True iff we're making a type pack expansion. */
3417 bool type_pack_expansion_p;
3418 };
3419
3420 /* Identifies all of the argument packs that occur in a template
3421 argument and appends them to the TREE_LIST inside DATA, which is a
3422 find_parameter_pack_data structure. This is a subroutine of
3423 make_pack_expansion and uses_parameter_packs. */
3424 static tree
3425 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3426 {
3427 tree t = *tp;
3428 struct find_parameter_pack_data* ppd =
3429 (struct find_parameter_pack_data*)data;
3430 bool parameter_pack_p = false;
3431
3432 /* Handle type aliases/typedefs. */
3433 if (TYPE_ALIAS_P (t))
3434 {
3435 if (TYPE_TEMPLATE_INFO (t))
3436 cp_walk_tree (&TYPE_TI_ARGS (t),
3437 &find_parameter_packs_r,
3438 ppd, ppd->visited);
3439 *walk_subtrees = 0;
3440 return NULL_TREE;
3441 }
3442
3443 /* Identify whether this is a parameter pack or not. */
3444 switch (TREE_CODE (t))
3445 {
3446 case TEMPLATE_PARM_INDEX:
3447 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3448 parameter_pack_p = true;
3449 break;
3450
3451 case TEMPLATE_TYPE_PARM:
3452 t = TYPE_MAIN_VARIANT (t);
3453 /* FALLTHRU */
3454 case TEMPLATE_TEMPLATE_PARM:
3455 /* If the placeholder appears in the decl-specifier-seq of a function
3456 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3457 is a pack expansion, the invented template parameter is a template
3458 parameter pack. */
3459 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3460 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3461 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3462 parameter_pack_p = true;
3463 break;
3464
3465 case FIELD_DECL:
3466 case PARM_DECL:
3467 if (DECL_PACK_P (t))
3468 {
3469 /* We don't want to walk into the type of a PARM_DECL,
3470 because we don't want to see the type parameter pack. */
3471 *walk_subtrees = 0;
3472 parameter_pack_p = true;
3473 }
3474 break;
3475
3476 /* Look through a lambda capture proxy to the field pack. */
3477 case VAR_DECL:
3478 if (DECL_HAS_VALUE_EXPR_P (t))
3479 {
3480 tree v = DECL_VALUE_EXPR (t);
3481 cp_walk_tree (&v,
3482 &find_parameter_packs_r,
3483 ppd, ppd->visited);
3484 *walk_subtrees = 0;
3485 }
3486 else if (variable_template_specialization_p (t))
3487 {
3488 cp_walk_tree (&DECL_TI_ARGS (t),
3489 find_parameter_packs_r,
3490 ppd, ppd->visited);
3491 *walk_subtrees = 0;
3492 }
3493 break;
3494
3495 case BASES:
3496 parameter_pack_p = true;
3497 break;
3498 default:
3499 /* Not a parameter pack. */
3500 break;
3501 }
3502
3503 if (parameter_pack_p)
3504 {
3505 /* Add this parameter pack to the list. */
3506 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3507 }
3508
3509 if (TYPE_P (t))
3510 cp_walk_tree (&TYPE_CONTEXT (t),
3511 &find_parameter_packs_r, ppd, ppd->visited);
3512
3513 /* This switch statement will return immediately if we don't find a
3514 parameter pack. */
3515 switch (TREE_CODE (t))
3516 {
3517 case TEMPLATE_PARM_INDEX:
3518 return NULL_TREE;
3519
3520 case BOUND_TEMPLATE_TEMPLATE_PARM:
3521 /* Check the template itself. */
3522 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3523 &find_parameter_packs_r, ppd, ppd->visited);
3524 /* Check the template arguments. */
3525 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3526 ppd->visited);
3527 *walk_subtrees = 0;
3528 return NULL_TREE;
3529
3530 case TEMPLATE_TYPE_PARM:
3531 case TEMPLATE_TEMPLATE_PARM:
3532 return NULL_TREE;
3533
3534 case PARM_DECL:
3535 return NULL_TREE;
3536
3537 case RECORD_TYPE:
3538 if (TYPE_PTRMEMFUNC_P (t))
3539 return NULL_TREE;
3540 /* Fall through. */
3541
3542 case UNION_TYPE:
3543 case ENUMERAL_TYPE:
3544 if (TYPE_TEMPLATE_INFO (t))
3545 cp_walk_tree (&TYPE_TI_ARGS (t),
3546 &find_parameter_packs_r, ppd, ppd->visited);
3547
3548 *walk_subtrees = 0;
3549 return NULL_TREE;
3550
3551 case CONSTRUCTOR:
3552 case TEMPLATE_DECL:
3553 cp_walk_tree (&TREE_TYPE (t),
3554 &find_parameter_packs_r, ppd, ppd->visited);
3555 return NULL_TREE;
3556
3557 case TYPENAME_TYPE:
3558 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3559 ppd, ppd->visited);
3560 *walk_subtrees = 0;
3561 return NULL_TREE;
3562
3563 case TYPE_PACK_EXPANSION:
3564 case EXPR_PACK_EXPANSION:
3565 *walk_subtrees = 0;
3566 return NULL_TREE;
3567
3568 case INTEGER_TYPE:
3569 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3570 ppd, ppd->visited);
3571 *walk_subtrees = 0;
3572 return NULL_TREE;
3573
3574 case IDENTIFIER_NODE:
3575 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3576 ppd->visited);
3577 *walk_subtrees = 0;
3578 return NULL_TREE;
3579
3580 case DECLTYPE_TYPE:
3581 {
3582 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3583 type_pack_expansion_p to false so that any placeholders
3584 within the expression don't get marked as parameter packs. */
3585 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3586 ppd->type_pack_expansion_p = false;
3587 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3588 ppd, ppd->visited);
3589 ppd->type_pack_expansion_p = type_pack_expansion_p;
3590 *walk_subtrees = 0;
3591 return NULL_TREE;
3592 }
3593
3594 default:
3595 return NULL_TREE;
3596 }
3597
3598 return NULL_TREE;
3599 }
3600
3601 /* Determines if the expression or type T uses any parameter packs. */
3602 bool
3603 uses_parameter_packs (tree t)
3604 {
3605 tree parameter_packs = NULL_TREE;
3606 struct find_parameter_pack_data ppd;
3607 ppd.parameter_packs = &parameter_packs;
3608 ppd.visited = new hash_set<tree>;
3609 ppd.type_pack_expansion_p = false;
3610 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3611 delete ppd.visited;
3612 return parameter_packs != NULL_TREE;
3613 }
3614
3615 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3616 representation a base-class initializer into a parameter pack
3617 expansion. If all goes well, the resulting node will be an
3618 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3619 respectively. */
3620 tree
3621 make_pack_expansion (tree arg)
3622 {
3623 tree result;
3624 tree parameter_packs = NULL_TREE;
3625 bool for_types = false;
3626 struct find_parameter_pack_data ppd;
3627
3628 if (!arg || arg == error_mark_node)
3629 return arg;
3630
3631 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3632 {
3633 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3634 class initializer. In this case, the TREE_PURPOSE will be a
3635 _TYPE node (representing the base class expansion we're
3636 initializing) and the TREE_VALUE will be a TREE_LIST
3637 containing the initialization arguments.
3638
3639 The resulting expansion looks somewhat different from most
3640 expansions. Rather than returning just one _EXPANSION, we
3641 return a TREE_LIST whose TREE_PURPOSE is a
3642 TYPE_PACK_EXPANSION containing the bases that will be
3643 initialized. The TREE_VALUE will be identical to the
3644 original TREE_VALUE, which is a list of arguments that will
3645 be passed to each base. We do not introduce any new pack
3646 expansion nodes into the TREE_VALUE (although it is possible
3647 that some already exist), because the TREE_PURPOSE and
3648 TREE_VALUE all need to be expanded together with the same
3649 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3650 resulting TREE_PURPOSE will mention the parameter packs in
3651 both the bases and the arguments to the bases. */
3652 tree purpose;
3653 tree value;
3654 tree parameter_packs = NULL_TREE;
3655
3656 /* Determine which parameter packs will be used by the base
3657 class expansion. */
3658 ppd.visited = new hash_set<tree>;
3659 ppd.parameter_packs = &parameter_packs;
3660 ppd.type_pack_expansion_p = true;
3661 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3662 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3663 &ppd, ppd.visited);
3664
3665 if (parameter_packs == NULL_TREE)
3666 {
3667 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3668 delete ppd.visited;
3669 return error_mark_node;
3670 }
3671
3672 if (TREE_VALUE (arg) != void_type_node)
3673 {
3674 /* Collect the sets of parameter packs used in each of the
3675 initialization arguments. */
3676 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3677 {
3678 /* Determine which parameter packs will be expanded in this
3679 argument. */
3680 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3681 &ppd, ppd.visited);
3682 }
3683 }
3684
3685 delete ppd.visited;
3686
3687 /* Create the pack expansion type for the base type. */
3688 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3689 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3690 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3691
3692 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3693 they will rarely be compared to anything. */
3694 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3695
3696 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3697 }
3698
3699 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3700 for_types = true;
3701
3702 /* Build the PACK_EXPANSION_* node. */
3703 result = for_types
3704 ? cxx_make_type (TYPE_PACK_EXPANSION)
3705 : make_node (EXPR_PACK_EXPANSION);
3706 SET_PACK_EXPANSION_PATTERN (result, arg);
3707 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3708 {
3709 /* Propagate type and const-expression information. */
3710 TREE_TYPE (result) = TREE_TYPE (arg);
3711 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3712 /* Mark this read now, since the expansion might be length 0. */
3713 mark_exp_read (arg);
3714 }
3715 else
3716 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3717 they will rarely be compared to anything. */
3718 SET_TYPE_STRUCTURAL_EQUALITY (result);
3719
3720 /* Determine which parameter packs will be expanded. */
3721 ppd.parameter_packs = &parameter_packs;
3722 ppd.visited = new hash_set<tree>;
3723 ppd.type_pack_expansion_p = TYPE_P (arg);
3724 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3725 delete ppd.visited;
3726
3727 /* Make sure we found some parameter packs. */
3728 if (parameter_packs == NULL_TREE)
3729 {
3730 if (TYPE_P (arg))
3731 error ("expansion pattern %<%T%> contains no argument packs", arg);
3732 else
3733 error ("expansion pattern %<%E%> contains no argument packs", arg);
3734 return error_mark_node;
3735 }
3736 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3737
3738 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3739
3740 return result;
3741 }
3742
3743 /* Checks T for any "bare" parameter packs, which have not yet been
3744 expanded, and issues an error if any are found. This operation can
3745 only be done on full expressions or types (e.g., an expression
3746 statement, "if" condition, etc.), because we could have expressions like:
3747
3748 foo(f(g(h(args)))...)
3749
3750 where "args" is a parameter pack. check_for_bare_parameter_packs
3751 should not be called for the subexpressions args, h(args),
3752 g(h(args)), or f(g(h(args))), because we would produce erroneous
3753 error messages.
3754
3755 Returns TRUE and emits an error if there were bare parameter packs,
3756 returns FALSE otherwise. */
3757 bool
3758 check_for_bare_parameter_packs (tree t)
3759 {
3760 tree parameter_packs = NULL_TREE;
3761 struct find_parameter_pack_data ppd;
3762
3763 if (!processing_template_decl || !t || t == error_mark_node)
3764 return false;
3765
3766 if (TREE_CODE (t) == TYPE_DECL)
3767 t = TREE_TYPE (t);
3768
3769 ppd.parameter_packs = &parameter_packs;
3770 ppd.visited = new hash_set<tree>;
3771 ppd.type_pack_expansion_p = false;
3772 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3773 delete ppd.visited;
3774
3775 if (parameter_packs)
3776 {
3777 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3778 error_at (loc, "parameter packs not expanded with %<...%>:");
3779 while (parameter_packs)
3780 {
3781 tree pack = TREE_VALUE (parameter_packs);
3782 tree name = NULL_TREE;
3783
3784 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3785 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3786 name = TYPE_NAME (pack);
3787 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3788 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3789 else
3790 name = DECL_NAME (pack);
3791
3792 if (name)
3793 inform (loc, " %qD", name);
3794 else
3795 inform (loc, " <anonymous>");
3796
3797 parameter_packs = TREE_CHAIN (parameter_packs);
3798 }
3799
3800 return true;
3801 }
3802
3803 return false;
3804 }
3805
3806 /* Expand any parameter packs that occur in the template arguments in
3807 ARGS. */
3808 tree
3809 expand_template_argument_pack (tree args)
3810 {
3811 tree result_args = NULL_TREE;
3812 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3813 int num_result_args = -1;
3814 int non_default_args_count = -1;
3815
3816 /* First, determine if we need to expand anything, and the number of
3817 slots we'll need. */
3818 for (in_arg = 0; in_arg < nargs; ++in_arg)
3819 {
3820 tree arg = TREE_VEC_ELT (args, in_arg);
3821 if (arg == NULL_TREE)
3822 return args;
3823 if (ARGUMENT_PACK_P (arg))
3824 {
3825 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3826 if (num_result_args < 0)
3827 num_result_args = in_arg + num_packed;
3828 else
3829 num_result_args += num_packed;
3830 }
3831 else
3832 {
3833 if (num_result_args >= 0)
3834 num_result_args++;
3835 }
3836 }
3837
3838 /* If no expansion is necessary, we're done. */
3839 if (num_result_args < 0)
3840 return args;
3841
3842 /* Expand arguments. */
3843 result_args = make_tree_vec (num_result_args);
3844 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3845 non_default_args_count =
3846 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3847 for (in_arg = 0; in_arg < nargs; ++in_arg)
3848 {
3849 tree arg = TREE_VEC_ELT (args, in_arg);
3850 if (ARGUMENT_PACK_P (arg))
3851 {
3852 tree packed = ARGUMENT_PACK_ARGS (arg);
3853 int i, num_packed = TREE_VEC_LENGTH (packed);
3854 for (i = 0; i < num_packed; ++i, ++out_arg)
3855 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3856 if (non_default_args_count > 0)
3857 non_default_args_count += num_packed - 1;
3858 }
3859 else
3860 {
3861 TREE_VEC_ELT (result_args, out_arg) = arg;
3862 ++out_arg;
3863 }
3864 }
3865 if (non_default_args_count >= 0)
3866 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3867 return result_args;
3868 }
3869
3870 /* Checks if DECL shadows a template parameter.
3871
3872 [temp.local]: A template-parameter shall not be redeclared within its
3873 scope (including nested scopes).
3874
3875 Emits an error and returns TRUE if the DECL shadows a parameter,
3876 returns FALSE otherwise. */
3877
3878 bool
3879 check_template_shadow (tree decl)
3880 {
3881 tree olddecl;
3882
3883 /* If we're not in a template, we can't possibly shadow a template
3884 parameter. */
3885 if (!current_template_parms)
3886 return true;
3887
3888 /* Figure out what we're shadowing. */
3889 if (TREE_CODE (decl) == OVERLOAD)
3890 decl = OVL_CURRENT (decl);
3891 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3892
3893 /* If there's no previous binding for this name, we're not shadowing
3894 anything, let alone a template parameter. */
3895 if (!olddecl)
3896 return true;
3897
3898 /* If we're not shadowing a template parameter, we're done. Note
3899 that OLDDECL might be an OVERLOAD (or perhaps even an
3900 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3901 node. */
3902 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3903 return true;
3904
3905 /* We check for decl != olddecl to avoid bogus errors for using a
3906 name inside a class. We check TPFI to avoid duplicate errors for
3907 inline member templates. */
3908 if (decl == olddecl
3909 || (DECL_TEMPLATE_PARM_P (decl)
3910 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3911 return true;
3912
3913 /* Don't complain about the injected class name, as we've already
3914 complained about the class itself. */
3915 if (DECL_SELF_REFERENCE_P (decl))
3916 return false;
3917
3918 if (DECL_TEMPLATE_PARM_P (decl))
3919 error ("declaration of template parameter %q+D shadows "
3920 "template parameter", decl);
3921 else
3922 error ("declaration of %q+#D shadows template parameter", decl);
3923 inform (DECL_SOURCE_LOCATION (olddecl),
3924 "template parameter %qD declared here", olddecl);
3925 return false;
3926 }
3927
3928 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3929 ORIG_LEVEL, DECL, and TYPE. */
3930
3931 static tree
3932 build_template_parm_index (int index,
3933 int level,
3934 int orig_level,
3935 tree decl,
3936 tree type)
3937 {
3938 tree t = make_node (TEMPLATE_PARM_INDEX);
3939 TEMPLATE_PARM_IDX (t) = index;
3940 TEMPLATE_PARM_LEVEL (t) = level;
3941 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3942 TEMPLATE_PARM_DECL (t) = decl;
3943 TREE_TYPE (t) = type;
3944 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3945 TREE_READONLY (t) = TREE_READONLY (decl);
3946
3947 return t;
3948 }
3949
3950 /* Find the canonical type parameter for the given template type
3951 parameter. Returns the canonical type parameter, which may be TYPE
3952 if no such parameter existed. */
3953
3954 static tree
3955 canonical_type_parameter (tree type)
3956 {
3957 tree list;
3958 int idx = TEMPLATE_TYPE_IDX (type);
3959 if (!canonical_template_parms)
3960 vec_alloc (canonical_template_parms, idx+1);
3961
3962 while (canonical_template_parms->length () <= (unsigned)idx)
3963 vec_safe_push (canonical_template_parms, NULL_TREE);
3964
3965 list = (*canonical_template_parms)[idx];
3966 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3967 list = TREE_CHAIN (list);
3968
3969 if (list)
3970 return TREE_VALUE (list);
3971 else
3972 {
3973 (*canonical_template_parms)[idx]
3974 = tree_cons (NULL_TREE, type,
3975 (*canonical_template_parms)[idx]);
3976 return type;
3977 }
3978 }
3979
3980 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3981 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3982 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3983 new one is created. */
3984
3985 static tree
3986 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3987 tsubst_flags_t complain)
3988 {
3989 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3990 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3991 != TEMPLATE_PARM_LEVEL (index) - levels)
3992 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3993 {
3994 tree orig_decl = TEMPLATE_PARM_DECL (index);
3995 tree decl, t;
3996
3997 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3998 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3999 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4000 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4001 DECL_ARTIFICIAL (decl) = 1;
4002 SET_DECL_TEMPLATE_PARM_P (decl);
4003
4004 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4005 TEMPLATE_PARM_LEVEL (index) - levels,
4006 TEMPLATE_PARM_ORIG_LEVEL (index),
4007 decl, type);
4008 TEMPLATE_PARM_DESCENDANTS (index) = t;
4009 TEMPLATE_PARM_PARAMETER_PACK (t)
4010 = TEMPLATE_PARM_PARAMETER_PACK (index);
4011
4012 /* Template template parameters need this. */
4013 if (TREE_CODE (decl) == TEMPLATE_DECL)
4014 {
4015 DECL_TEMPLATE_RESULT (decl)
4016 = build_decl (DECL_SOURCE_LOCATION (decl),
4017 TYPE_DECL, DECL_NAME (decl), type);
4018 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4019 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4020 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4021 }
4022 }
4023
4024 return TEMPLATE_PARM_DESCENDANTS (index);
4025 }
4026
4027 /* Process information from new template parameter PARM and append it
4028 to the LIST being built. This new parameter is a non-type
4029 parameter iff IS_NON_TYPE is true. This new parameter is a
4030 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4031 is in PARM_LOC. */
4032
4033 tree
4034 process_template_parm (tree list, location_t parm_loc, tree parm,
4035 bool is_non_type, bool is_parameter_pack)
4036 {
4037 tree decl = 0;
4038 int idx = 0;
4039
4040 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4041 tree defval = TREE_PURPOSE (parm);
4042 tree constr = TREE_TYPE (parm);
4043
4044 if (list)
4045 {
4046 tree p = tree_last (list);
4047
4048 if (p && TREE_VALUE (p) != error_mark_node)
4049 {
4050 p = TREE_VALUE (p);
4051 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4052 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4053 else
4054 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4055 }
4056
4057 ++idx;
4058 }
4059
4060 if (is_non_type)
4061 {
4062 parm = TREE_VALUE (parm);
4063
4064 SET_DECL_TEMPLATE_PARM_P (parm);
4065
4066 if (TREE_TYPE (parm) != error_mark_node)
4067 {
4068 /* [temp.param]
4069
4070 The top-level cv-qualifiers on the template-parameter are
4071 ignored when determining its type. */
4072 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4073 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4074 TREE_TYPE (parm) = error_mark_node;
4075 else if (uses_parameter_packs (TREE_TYPE (parm))
4076 && !is_parameter_pack
4077 /* If we're in a nested template parameter list, the template
4078 template parameter could be a parameter pack. */
4079 && processing_template_parmlist == 1)
4080 {
4081 /* This template parameter is not a parameter pack, but it
4082 should be. Complain about "bare" parameter packs. */
4083 check_for_bare_parameter_packs (TREE_TYPE (parm));
4084
4085 /* Recover by calling this a parameter pack. */
4086 is_parameter_pack = true;
4087 }
4088 }
4089
4090 /* A template parameter is not modifiable. */
4091 TREE_CONSTANT (parm) = 1;
4092 TREE_READONLY (parm) = 1;
4093 decl = build_decl (parm_loc,
4094 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4095 TREE_CONSTANT (decl) = 1;
4096 TREE_READONLY (decl) = 1;
4097 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4098 = build_template_parm_index (idx, processing_template_decl,
4099 processing_template_decl,
4100 decl, TREE_TYPE (parm));
4101
4102 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4103 = is_parameter_pack;
4104 }
4105 else
4106 {
4107 tree t;
4108 parm = TREE_VALUE (TREE_VALUE (parm));
4109
4110 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4111 {
4112 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4113 /* This is for distinguishing between real templates and template
4114 template parameters */
4115 TREE_TYPE (parm) = t;
4116 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4117 decl = parm;
4118 }
4119 else
4120 {
4121 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4122 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4123 decl = build_decl (parm_loc,
4124 TYPE_DECL, parm, t);
4125 }
4126
4127 TYPE_NAME (t) = decl;
4128 TYPE_STUB_DECL (t) = decl;
4129 parm = decl;
4130 TEMPLATE_TYPE_PARM_INDEX (t)
4131 = build_template_parm_index (idx, processing_template_decl,
4132 processing_template_decl,
4133 decl, TREE_TYPE (parm));
4134 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4135 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4136 }
4137 DECL_ARTIFICIAL (decl) = 1;
4138 SET_DECL_TEMPLATE_PARM_P (decl);
4139
4140 /* Build requirements for the type/template parameter.
4141 This must be done after SET_DECL_TEMPLATE_PARM_P or
4142 process_template_parm could fail. */
4143 tree reqs = finish_shorthand_constraint (parm, constr);
4144
4145 pushdecl (decl);
4146
4147 /* Build the parameter node linking the parameter declaration,
4148 its default argument (if any), and its constraints (if any). */
4149 parm = build_tree_list (defval, parm);
4150 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4151
4152 return chainon (list, parm);
4153 }
4154
4155 /* The end of a template parameter list has been reached. Process the
4156 tree list into a parameter vector, converting each parameter into a more
4157 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4158 as PARM_DECLs. */
4159
4160 tree
4161 end_template_parm_list (tree parms)
4162 {
4163 int nparms;
4164 tree parm, next;
4165 tree saved_parmlist = make_tree_vec (list_length (parms));
4166
4167 /* Pop the dummy parameter level and add the real one. */
4168 current_template_parms = TREE_CHAIN (current_template_parms);
4169
4170 current_template_parms
4171 = tree_cons (size_int (processing_template_decl),
4172 saved_parmlist, current_template_parms);
4173
4174 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4175 {
4176 next = TREE_CHAIN (parm);
4177 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4178 TREE_CHAIN (parm) = NULL_TREE;
4179 }
4180
4181 --processing_template_parmlist;
4182
4183 return saved_parmlist;
4184 }
4185
4186 // Explicitly indicate the end of the template parameter list. We assume
4187 // that the current template parameters have been constructed and/or
4188 // managed explicitly, as when creating new template template parameters
4189 // from a shorthand constraint.
4190 void
4191 end_template_parm_list ()
4192 {
4193 --processing_template_parmlist;
4194 }
4195
4196 /* end_template_decl is called after a template declaration is seen. */
4197
4198 void
4199 end_template_decl (void)
4200 {
4201 reset_specialization ();
4202
4203 if (! processing_template_decl)
4204 return;
4205
4206 /* This matches the pushlevel in begin_template_parm_list. */
4207 finish_scope ();
4208
4209 --processing_template_decl;
4210 current_template_parms = TREE_CHAIN (current_template_parms);
4211 }
4212
4213 /* Takes a TREE_LIST representing a template parameter and convert it
4214 into an argument suitable to be passed to the type substitution
4215 functions. Note that If the TREE_LIST contains an error_mark
4216 node, the returned argument is error_mark_node. */
4217
4218 tree
4219 template_parm_to_arg (tree t)
4220 {
4221
4222 if (t == NULL_TREE
4223 || TREE_CODE (t) != TREE_LIST)
4224 return t;
4225
4226 if (error_operand_p (TREE_VALUE (t)))
4227 return error_mark_node;
4228
4229 t = TREE_VALUE (t);
4230
4231 if (TREE_CODE (t) == TYPE_DECL
4232 || TREE_CODE (t) == TEMPLATE_DECL)
4233 {
4234 t = TREE_TYPE (t);
4235
4236 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4237 {
4238 /* Turn this argument into a TYPE_ARGUMENT_PACK
4239 with a single element, which expands T. */
4240 tree vec = make_tree_vec (1);
4241 if (CHECKING_P)
4242 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4243
4244 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4245
4246 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4247 SET_ARGUMENT_PACK_ARGS (t, vec);
4248 }
4249 }
4250 else
4251 {
4252 t = DECL_INITIAL (t);
4253
4254 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4255 {
4256 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4257 with a single element, which expands T. */
4258 tree vec = make_tree_vec (1);
4259 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4260 if (CHECKING_P)
4261 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4262
4263 t = convert_from_reference (t);
4264 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4265
4266 t = make_node (NONTYPE_ARGUMENT_PACK);
4267 SET_ARGUMENT_PACK_ARGS (t, vec);
4268 TREE_TYPE (t) = type;
4269 }
4270 else
4271 t = convert_from_reference (t);
4272 }
4273 return t;
4274 }
4275
4276 /* Given a single level of template parameters (a TREE_VEC), return it
4277 as a set of template arguments. */
4278
4279 static tree
4280 template_parms_level_to_args (tree parms)
4281 {
4282 tree a = copy_node (parms);
4283 TREE_TYPE (a) = NULL_TREE;
4284 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4285 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4286
4287 if (CHECKING_P)
4288 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4289
4290 return a;
4291 }
4292
4293 /* Given a set of template parameters, return them as a set of template
4294 arguments. The template parameters are represented as a TREE_VEC, in
4295 the form documented in cp-tree.h for template arguments. */
4296
4297 static tree
4298 template_parms_to_args (tree parms)
4299 {
4300 tree header;
4301 tree args = NULL_TREE;
4302 int length = TMPL_PARMS_DEPTH (parms);
4303 int l = length;
4304
4305 /* If there is only one level of template parameters, we do not
4306 create a TREE_VEC of TREE_VECs. Instead, we return a single
4307 TREE_VEC containing the arguments. */
4308 if (length > 1)
4309 args = make_tree_vec (length);
4310
4311 for (header = parms; header; header = TREE_CHAIN (header))
4312 {
4313 tree a = template_parms_level_to_args (TREE_VALUE (header));
4314
4315 if (length > 1)
4316 TREE_VEC_ELT (args, --l) = a;
4317 else
4318 args = a;
4319 }
4320
4321 return args;
4322 }
4323
4324 /* Within the declaration of a template, return the currently active
4325 template parameters as an argument TREE_VEC. */
4326
4327 static tree
4328 current_template_args (void)
4329 {
4330 return template_parms_to_args (current_template_parms);
4331 }
4332
4333 /* Update the declared TYPE by doing any lookups which were thought to be
4334 dependent, but are not now that we know the SCOPE of the declarator. */
4335
4336 tree
4337 maybe_update_decl_type (tree orig_type, tree scope)
4338 {
4339 tree type = orig_type;
4340
4341 if (type == NULL_TREE)
4342 return type;
4343
4344 if (TREE_CODE (orig_type) == TYPE_DECL)
4345 type = TREE_TYPE (type);
4346
4347 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4348 && dependent_type_p (type)
4349 /* Don't bother building up the args in this case. */
4350 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4351 {
4352 /* tsubst in the args corresponding to the template parameters,
4353 including auto if present. Most things will be unchanged, but
4354 make_typename_type and tsubst_qualified_id will resolve
4355 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4356 tree args = current_template_args ();
4357 tree auto_node = type_uses_auto (type);
4358 tree pushed;
4359 if (auto_node)
4360 {
4361 tree auto_vec = make_tree_vec (1);
4362 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4363 args = add_to_template_args (args, auto_vec);
4364 }
4365 pushed = push_scope (scope);
4366 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4367 if (pushed)
4368 pop_scope (scope);
4369 }
4370
4371 if (type == error_mark_node)
4372 return orig_type;
4373
4374 if (TREE_CODE (orig_type) == TYPE_DECL)
4375 {
4376 if (same_type_p (type, TREE_TYPE (orig_type)))
4377 type = orig_type;
4378 else
4379 type = TYPE_NAME (type);
4380 }
4381 return type;
4382 }
4383
4384 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4385 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4386 the new template is a member template. */
4387
4388 tree
4389 build_template_decl (tree decl, tree parms, bool member_template_p)
4390 {
4391 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4392 DECL_TEMPLATE_PARMS (tmpl) = parms;
4393 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4394 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4395 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4396
4397 return tmpl;
4398 }
4399
4400 struct template_parm_data
4401 {
4402 /* The level of the template parameters we are currently
4403 processing. */
4404 int level;
4405
4406 /* The index of the specialization argument we are currently
4407 processing. */
4408 int current_arg;
4409
4410 /* An array whose size is the number of template parameters. The
4411 elements are nonzero if the parameter has been used in any one
4412 of the arguments processed so far. */
4413 int* parms;
4414
4415 /* An array whose size is the number of template arguments. The
4416 elements are nonzero if the argument makes use of template
4417 parameters of this level. */
4418 int* arg_uses_template_parms;
4419 };
4420
4421 /* Subroutine of push_template_decl used to see if each template
4422 parameter in a partial specialization is used in the explicit
4423 argument list. If T is of the LEVEL given in DATA (which is
4424 treated as a template_parm_data*), then DATA->PARMS is marked
4425 appropriately. */
4426
4427 static int
4428 mark_template_parm (tree t, void* data)
4429 {
4430 int level;
4431 int idx;
4432 struct template_parm_data* tpd = (struct template_parm_data*) data;
4433
4434 template_parm_level_and_index (t, &level, &idx);
4435
4436 if (level == tpd->level)
4437 {
4438 tpd->parms[idx] = 1;
4439 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4440 }
4441
4442 /* Return zero so that for_each_template_parm will continue the
4443 traversal of the tree; we want to mark *every* template parm. */
4444 return 0;
4445 }
4446
4447 /* Process the partial specialization DECL. */
4448
4449 static tree
4450 process_partial_specialization (tree decl)
4451 {
4452 tree type = TREE_TYPE (decl);
4453 tree tinfo = get_template_info (decl);
4454 tree maintmpl = TI_TEMPLATE (tinfo);
4455 tree specargs = TI_ARGS (tinfo);
4456 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4457 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4458 tree inner_parms;
4459 tree inst;
4460 int nargs = TREE_VEC_LENGTH (inner_args);
4461 int ntparms;
4462 int i;
4463 bool did_error_intro = false;
4464 struct template_parm_data tpd;
4465 struct template_parm_data tpd2;
4466
4467 gcc_assert (current_template_parms);
4468
4469 /* A concept cannot be specialized. */
4470 if (flag_concepts && variable_concept_p (maintmpl))
4471 {
4472 error ("specialization of variable concept %q#D", maintmpl);
4473 return error_mark_node;
4474 }
4475
4476 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4477 ntparms = TREE_VEC_LENGTH (inner_parms);
4478
4479 /* We check that each of the template parameters given in the
4480 partial specialization is used in the argument list to the
4481 specialization. For example:
4482
4483 template <class T> struct S;
4484 template <class T> struct S<T*>;
4485
4486 The second declaration is OK because `T*' uses the template
4487 parameter T, whereas
4488
4489 template <class T> struct S<int>;
4490
4491 is no good. Even trickier is:
4492
4493 template <class T>
4494 struct S1
4495 {
4496 template <class U>
4497 struct S2;
4498 template <class U>
4499 struct S2<T>;
4500 };
4501
4502 The S2<T> declaration is actually invalid; it is a
4503 full-specialization. Of course,
4504
4505 template <class U>
4506 struct S2<T (*)(U)>;
4507
4508 or some such would have been OK. */
4509 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4510 tpd.parms = XALLOCAVEC (int, ntparms);
4511 memset (tpd.parms, 0, sizeof (int) * ntparms);
4512
4513 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4514 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4515 for (i = 0; i < nargs; ++i)
4516 {
4517 tpd.current_arg = i;
4518 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4519 &mark_template_parm,
4520 &tpd,
4521 NULL,
4522 /*include_nondeduced_p=*/false);
4523 }
4524 for (i = 0; i < ntparms; ++i)
4525 if (tpd.parms[i] == 0)
4526 {
4527 /* One of the template parms was not used in a deduced context in the
4528 specialization. */
4529 if (!did_error_intro)
4530 {
4531 error ("template parameters not deducible in "
4532 "partial specialization:");
4533 did_error_intro = true;
4534 }
4535
4536 inform (input_location, " %qD",
4537 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4538 }
4539
4540 if (did_error_intro)
4541 return error_mark_node;
4542
4543 /* [temp.class.spec]
4544
4545 The argument list of the specialization shall not be identical to
4546 the implicit argument list of the primary template. */
4547 tree main_args
4548 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4549 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4550 && (!flag_concepts
4551 || !strictly_subsumes (current_template_constraints (),
4552 get_constraints (maintmpl))))
4553 {
4554 if (!flag_concepts)
4555 error ("partial specialization %q+D does not specialize "
4556 "any template arguments", decl);
4557 else
4558 error ("partial specialization %q+D does not specialize any "
4559 "template arguments and is not more constrained than", decl);
4560 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4561 }
4562
4563 /* A partial specialization that replaces multiple parameters of the
4564 primary template with a pack expansion is less specialized for those
4565 parameters. */
4566 if (nargs < DECL_NTPARMS (maintmpl))
4567 {
4568 error ("partial specialization is not more specialized than the "
4569 "primary template because it replaces multiple parameters "
4570 "with a pack expansion");
4571 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4572 return decl;
4573 }
4574
4575 /* [temp.class.spec]
4576
4577 A partially specialized non-type argument expression shall not
4578 involve template parameters of the partial specialization except
4579 when the argument expression is a simple identifier.
4580
4581 The type of a template parameter corresponding to a specialized
4582 non-type argument shall not be dependent on a parameter of the
4583 specialization.
4584
4585 Also, we verify that pack expansions only occur at the
4586 end of the argument list. */
4587 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4588 tpd2.parms = 0;
4589 for (i = 0; i < nargs; ++i)
4590 {
4591 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4592 tree arg = TREE_VEC_ELT (inner_args, i);
4593 tree packed_args = NULL_TREE;
4594 int j, len = 1;
4595
4596 if (ARGUMENT_PACK_P (arg))
4597 {
4598 /* Extract the arguments from the argument pack. We'll be
4599 iterating over these in the following loop. */
4600 packed_args = ARGUMENT_PACK_ARGS (arg);
4601 len = TREE_VEC_LENGTH (packed_args);
4602 }
4603
4604 for (j = 0; j < len; j++)
4605 {
4606 if (packed_args)
4607 /* Get the Jth argument in the parameter pack. */
4608 arg = TREE_VEC_ELT (packed_args, j);
4609
4610 if (PACK_EXPANSION_P (arg))
4611 {
4612 /* Pack expansions must come at the end of the
4613 argument list. */
4614 if ((packed_args && j < len - 1)
4615 || (!packed_args && i < nargs - 1))
4616 {
4617 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4618 error ("parameter pack argument %qE must be at the "
4619 "end of the template argument list", arg);
4620 else
4621 error ("parameter pack argument %qT must be at the "
4622 "end of the template argument list", arg);
4623 }
4624 }
4625
4626 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4627 /* We only care about the pattern. */
4628 arg = PACK_EXPANSION_PATTERN (arg);
4629
4630 if (/* These first two lines are the `non-type' bit. */
4631 !TYPE_P (arg)
4632 && TREE_CODE (arg) != TEMPLATE_DECL
4633 /* This next two lines are the `argument expression is not just a
4634 simple identifier' condition and also the `specialized
4635 non-type argument' bit. */
4636 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4637 && !(REFERENCE_REF_P (arg)
4638 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4639 {
4640 if ((!packed_args && tpd.arg_uses_template_parms[i])
4641 || (packed_args && uses_template_parms (arg)))
4642 error ("template argument %qE involves template parameter(s)",
4643 arg);
4644 else
4645 {
4646 /* Look at the corresponding template parameter,
4647 marking which template parameters its type depends
4648 upon. */
4649 tree type = TREE_TYPE (parm);
4650
4651 if (!tpd2.parms)
4652 {
4653 /* We haven't yet initialized TPD2. Do so now. */
4654 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4655 /* The number of parameters here is the number in the
4656 main template, which, as checked in the assertion
4657 above, is NARGS. */
4658 tpd2.parms = XALLOCAVEC (int, nargs);
4659 tpd2.level =
4660 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4661 }
4662
4663 /* Mark the template parameters. But this time, we're
4664 looking for the template parameters of the main
4665 template, not in the specialization. */
4666 tpd2.current_arg = i;
4667 tpd2.arg_uses_template_parms[i] = 0;
4668 memset (tpd2.parms, 0, sizeof (int) * nargs);
4669 for_each_template_parm (type,
4670 &mark_template_parm,
4671 &tpd2,
4672 NULL,
4673 /*include_nondeduced_p=*/false);
4674
4675 if (tpd2.arg_uses_template_parms [i])
4676 {
4677 /* The type depended on some template parameters.
4678 If they are fully specialized in the
4679 specialization, that's OK. */
4680 int j;
4681 int count = 0;
4682 for (j = 0; j < nargs; ++j)
4683 if (tpd2.parms[j] != 0
4684 && tpd.arg_uses_template_parms [j])
4685 ++count;
4686 if (count != 0)
4687 error_n (input_location, count,
4688 "type %qT of template argument %qE depends "
4689 "on a template parameter",
4690 "type %qT of template argument %qE depends "
4691 "on template parameters",
4692 type,
4693 arg);
4694 }
4695 }
4696 }
4697 }
4698 }
4699
4700 /* We should only get here once. */
4701 if (TREE_CODE (decl) == TYPE_DECL)
4702 gcc_assert (!COMPLETE_TYPE_P (type));
4703
4704 // Build the template decl.
4705 tree tmpl = build_template_decl (decl, current_template_parms,
4706 DECL_MEMBER_TEMPLATE_P (maintmpl));
4707 TREE_TYPE (tmpl) = type;
4708 DECL_TEMPLATE_RESULT (tmpl) = decl;
4709 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4710 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4711 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4712
4713 if (VAR_P (decl))
4714 /* We didn't register this in check_explicit_specialization so we could
4715 wait until the constraints were set. */
4716 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4717 else
4718 associate_classtype_constraints (type);
4719
4720 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4721 = tree_cons (specargs, tmpl,
4722 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4723 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4724
4725 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4726 inst = TREE_CHAIN (inst))
4727 {
4728 tree instance = TREE_VALUE (inst);
4729 if (TYPE_P (instance)
4730 ? (COMPLETE_TYPE_P (instance)
4731 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4732 : DECL_TEMPLATE_INSTANTIATION (instance))
4733 {
4734 tree spec = most_specialized_partial_spec (instance, tf_none);
4735 tree inst_decl = (DECL_P (instance)
4736 ? instance : TYPE_NAME (instance));
4737 if (!spec)
4738 /* OK */;
4739 else if (spec == error_mark_node)
4740 permerror (input_location,
4741 "declaration of %qD ambiguates earlier template "
4742 "instantiation for %qD", decl, inst_decl);
4743 else if (TREE_VALUE (spec) == tmpl)
4744 permerror (input_location,
4745 "partial specialization of %qD after instantiation "
4746 "of %qD", decl, inst_decl);
4747 }
4748 }
4749
4750 return decl;
4751 }
4752
4753 /* PARM is a template parameter of some form; return the corresponding
4754 TEMPLATE_PARM_INDEX. */
4755
4756 static tree
4757 get_template_parm_index (tree parm)
4758 {
4759 if (TREE_CODE (parm) == PARM_DECL
4760 || TREE_CODE (parm) == CONST_DECL)
4761 parm = DECL_INITIAL (parm);
4762 else if (TREE_CODE (parm) == TYPE_DECL
4763 || TREE_CODE (parm) == TEMPLATE_DECL)
4764 parm = TREE_TYPE (parm);
4765 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4766 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4767 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4768 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4769 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4770 return parm;
4771 }
4772
4773 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4774 parameter packs used by the template parameter PARM. */
4775
4776 static void
4777 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4778 {
4779 /* A type parm can't refer to another parm. */
4780 if (TREE_CODE (parm) == TYPE_DECL)
4781 return;
4782 else if (TREE_CODE (parm) == PARM_DECL)
4783 {
4784 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4785 ppd, ppd->visited);
4786 return;
4787 }
4788
4789 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4790
4791 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4792 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4793 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4794 }
4795
4796 /* PARM is a template parameter pack. Return any parameter packs used in
4797 its type or the type of any of its template parameters. If there are
4798 any such packs, it will be instantiated into a fixed template parameter
4799 list by partial instantiation rather than be fully deduced. */
4800
4801 tree
4802 fixed_parameter_pack_p (tree parm)
4803 {
4804 /* This can only be true in a member template. */
4805 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4806 return NULL_TREE;
4807 /* This can only be true for a parameter pack. */
4808 if (!template_parameter_pack_p (parm))
4809 return NULL_TREE;
4810 /* A type parm can't refer to another parm. */
4811 if (TREE_CODE (parm) == TYPE_DECL)
4812 return NULL_TREE;
4813
4814 tree parameter_packs = NULL_TREE;
4815 struct find_parameter_pack_data ppd;
4816 ppd.parameter_packs = &parameter_packs;
4817 ppd.visited = new hash_set<tree>;
4818 ppd.type_pack_expansion_p = false;
4819
4820 fixed_parameter_pack_p_1 (parm, &ppd);
4821
4822 delete ppd.visited;
4823 return parameter_packs;
4824 }
4825
4826 /* Check that a template declaration's use of default arguments and
4827 parameter packs is not invalid. Here, PARMS are the template
4828 parameters. IS_PRIMARY is true if DECL is the thing declared by
4829 a primary template. IS_PARTIAL is true if DECL is a partial
4830 specialization.
4831
4832 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4833 declaration (but not a definition); 1 indicates a declaration, 2
4834 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4835 emitted for extraneous default arguments.
4836
4837 Returns TRUE if there were no errors found, FALSE otherwise. */
4838
4839 bool
4840 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4841 bool is_partial, int is_friend_decl)
4842 {
4843 const char *msg;
4844 int last_level_to_check;
4845 tree parm_level;
4846 bool no_errors = true;
4847
4848 /* [temp.param]
4849
4850 A default template-argument shall not be specified in a
4851 function template declaration or a function template definition, nor
4852 in the template-parameter-list of the definition of a member of a
4853 class template. */
4854
4855 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4856 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4857 /* You can't have a function template declaration in a local
4858 scope, nor you can you define a member of a class template in a
4859 local scope. */
4860 return true;
4861
4862 if ((TREE_CODE (decl) == TYPE_DECL
4863 && TREE_TYPE (decl)
4864 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4865 || (TREE_CODE (decl) == FUNCTION_DECL
4866 && LAMBDA_FUNCTION_P (decl)))
4867 /* A lambda doesn't have an explicit declaration; don't complain
4868 about the parms of the enclosing class. */
4869 return true;
4870
4871 if (current_class_type
4872 && !TYPE_BEING_DEFINED (current_class_type)
4873 && DECL_LANG_SPECIFIC (decl)
4874 && DECL_DECLARES_FUNCTION_P (decl)
4875 /* If this is either a friend defined in the scope of the class
4876 or a member function. */
4877 && (DECL_FUNCTION_MEMBER_P (decl)
4878 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4879 : DECL_FRIEND_CONTEXT (decl)
4880 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4881 : false)
4882 /* And, if it was a member function, it really was defined in
4883 the scope of the class. */
4884 && (!DECL_FUNCTION_MEMBER_P (decl)
4885 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4886 /* We already checked these parameters when the template was
4887 declared, so there's no need to do it again now. This function
4888 was defined in class scope, but we're processing its body now
4889 that the class is complete. */
4890 return true;
4891
4892 /* Core issue 226 (C++0x only): the following only applies to class
4893 templates. */
4894 if (is_primary
4895 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4896 {
4897 /* [temp.param]
4898
4899 If a template-parameter has a default template-argument, all
4900 subsequent template-parameters shall have a default
4901 template-argument supplied. */
4902 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4903 {
4904 tree inner_parms = TREE_VALUE (parm_level);
4905 int ntparms = TREE_VEC_LENGTH (inner_parms);
4906 int seen_def_arg_p = 0;
4907 int i;
4908
4909 for (i = 0; i < ntparms; ++i)
4910 {
4911 tree parm = TREE_VEC_ELT (inner_parms, i);
4912
4913 if (parm == error_mark_node)
4914 continue;
4915
4916 if (TREE_PURPOSE (parm))
4917 seen_def_arg_p = 1;
4918 else if (seen_def_arg_p
4919 && !template_parameter_pack_p (TREE_VALUE (parm)))
4920 {
4921 error ("no default argument for %qD", TREE_VALUE (parm));
4922 /* For better subsequent error-recovery, we indicate that
4923 there should have been a default argument. */
4924 TREE_PURPOSE (parm) = error_mark_node;
4925 no_errors = false;
4926 }
4927 else if (!is_partial
4928 && !is_friend_decl
4929 /* Don't complain about an enclosing partial
4930 specialization. */
4931 && parm_level == parms
4932 && TREE_CODE (decl) == TYPE_DECL
4933 && i < ntparms - 1
4934 && template_parameter_pack_p (TREE_VALUE (parm))
4935 /* A fixed parameter pack will be partially
4936 instantiated into a fixed length list. */
4937 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4938 {
4939 /* A primary class template can only have one
4940 parameter pack, at the end of the template
4941 parameter list. */
4942
4943 error ("parameter pack %q+D must be at the end of the"
4944 " template parameter list", TREE_VALUE (parm));
4945
4946 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4947 = error_mark_node;
4948 no_errors = false;
4949 }
4950 }
4951 }
4952 }
4953
4954 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4955 || is_partial
4956 || !is_primary
4957 || is_friend_decl)
4958 /* For an ordinary class template, default template arguments are
4959 allowed at the innermost level, e.g.:
4960 template <class T = int>
4961 struct S {};
4962 but, in a partial specialization, they're not allowed even
4963 there, as we have in [temp.class.spec]:
4964
4965 The template parameter list of a specialization shall not
4966 contain default template argument values.
4967
4968 So, for a partial specialization, or for a function template
4969 (in C++98/C++03), we look at all of them. */
4970 ;
4971 else
4972 /* But, for a primary class template that is not a partial
4973 specialization we look at all template parameters except the
4974 innermost ones. */
4975 parms = TREE_CHAIN (parms);
4976
4977 /* Figure out what error message to issue. */
4978 if (is_friend_decl == 2)
4979 msg = G_("default template arguments may not be used in function template "
4980 "friend re-declaration");
4981 else if (is_friend_decl)
4982 msg = G_("default template arguments may not be used in function template "
4983 "friend declarations");
4984 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4985 msg = G_("default template arguments may not be used in function templates "
4986 "without -std=c++11 or -std=gnu++11");
4987 else if (is_partial)
4988 msg = G_("default template arguments may not be used in "
4989 "partial specializations");
4990 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
4991 msg = G_("default argument for template parameter for class enclosing %qD");
4992 else
4993 /* Per [temp.param]/9, "A default template-argument shall not be
4994 specified in the template-parameter-lists of the definition of
4995 a member of a class template that appears outside of the member's
4996 class.", thus if we aren't handling a member of a class template
4997 there is no need to examine the parameters. */
4998 return true;
4999
5000 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5001 /* If we're inside a class definition, there's no need to
5002 examine the parameters to the class itself. On the one
5003 hand, they will be checked when the class is defined, and,
5004 on the other, default arguments are valid in things like:
5005 template <class T = double>
5006 struct S { template <class U> void f(U); };
5007 Here the default argument for `S' has no bearing on the
5008 declaration of `f'. */
5009 last_level_to_check = template_class_depth (current_class_type) + 1;
5010 else
5011 /* Check everything. */
5012 last_level_to_check = 0;
5013
5014 for (parm_level = parms;
5015 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5016 parm_level = TREE_CHAIN (parm_level))
5017 {
5018 tree inner_parms = TREE_VALUE (parm_level);
5019 int i;
5020 int ntparms;
5021
5022 ntparms = TREE_VEC_LENGTH (inner_parms);
5023 for (i = 0; i < ntparms; ++i)
5024 {
5025 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5026 continue;
5027
5028 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5029 {
5030 if (msg)
5031 {
5032 no_errors = false;
5033 if (is_friend_decl == 2)
5034 return no_errors;
5035
5036 error (msg, decl);
5037 msg = 0;
5038 }
5039
5040 /* Clear out the default argument so that we are not
5041 confused later. */
5042 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5043 }
5044 }
5045
5046 /* At this point, if we're still interested in issuing messages,
5047 they must apply to classes surrounding the object declared. */
5048 if (msg)
5049 msg = G_("default argument for template parameter for class "
5050 "enclosing %qD");
5051 }
5052
5053 return no_errors;
5054 }
5055
5056 /* Worker for push_template_decl_real, called via
5057 for_each_template_parm. DATA is really an int, indicating the
5058 level of the parameters we are interested in. If T is a template
5059 parameter of that level, return nonzero. */
5060
5061 static int
5062 template_parm_this_level_p (tree t, void* data)
5063 {
5064 int this_level = *(int *)data;
5065 int level;
5066
5067 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5068 level = TEMPLATE_PARM_LEVEL (t);
5069 else
5070 level = TEMPLATE_TYPE_LEVEL (t);
5071 return level == this_level;
5072 }
5073
5074 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5075 DATA is really an int, indicating the innermost outer level of parameters.
5076 If T is a template parameter of that level or further out, return
5077 nonzero. */
5078
5079 static int
5080 template_parm_outer_level (tree t, void *data)
5081 {
5082 int this_level = *(int *)data;
5083 int level;
5084
5085 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5086 level = TEMPLATE_PARM_LEVEL (t);
5087 else
5088 level = TEMPLATE_TYPE_LEVEL (t);
5089 return level <= this_level;
5090 }
5091
5092 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5093 parameters given by current_template_args, or reuses a
5094 previously existing one, if appropriate. Returns the DECL, or an
5095 equivalent one, if it is replaced via a call to duplicate_decls.
5096
5097 If IS_FRIEND is true, DECL is a friend declaration. */
5098
5099 tree
5100 push_template_decl_real (tree decl, bool is_friend)
5101 {
5102 tree tmpl;
5103 tree args;
5104 tree info;
5105 tree ctx;
5106 bool is_primary;
5107 bool is_partial;
5108 int new_template_p = 0;
5109 /* True if the template is a member template, in the sense of
5110 [temp.mem]. */
5111 bool member_template_p = false;
5112
5113 if (decl == error_mark_node || !current_template_parms)
5114 return error_mark_node;
5115
5116 /* See if this is a partial specialization. */
5117 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5118 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5119 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5120 || (VAR_P (decl)
5121 && DECL_LANG_SPECIFIC (decl)
5122 && DECL_TEMPLATE_SPECIALIZATION (decl)
5123 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5124
5125 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5126 is_friend = true;
5127
5128 if (is_friend)
5129 /* For a friend, we want the context of the friend function, not
5130 the type of which it is a friend. */
5131 ctx = CP_DECL_CONTEXT (decl);
5132 else if (CP_DECL_CONTEXT (decl)
5133 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5134 /* In the case of a virtual function, we want the class in which
5135 it is defined. */
5136 ctx = CP_DECL_CONTEXT (decl);
5137 else
5138 /* Otherwise, if we're currently defining some class, the DECL
5139 is assumed to be a member of the class. */
5140 ctx = current_scope ();
5141
5142 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5143 ctx = NULL_TREE;
5144
5145 if (!DECL_CONTEXT (decl))
5146 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5147
5148 /* See if this is a primary template. */
5149 if (is_friend && ctx
5150 && uses_template_parms_level (ctx, processing_template_decl))
5151 /* A friend template that specifies a class context, i.e.
5152 template <typename T> friend void A<T>::f();
5153 is not primary. */
5154 is_primary = false;
5155 else if (TREE_CODE (decl) == TYPE_DECL
5156 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5157 is_primary = false;
5158 else
5159 is_primary = template_parm_scope_p ();
5160
5161 if (is_primary)
5162 {
5163 warning (OPT_Wtemplates, "template %qD declared", decl);
5164
5165 if (DECL_CLASS_SCOPE_P (decl))
5166 member_template_p = true;
5167 if (TREE_CODE (decl) == TYPE_DECL
5168 && anon_aggrname_p (DECL_NAME (decl)))
5169 {
5170 error ("template class without a name");
5171 return error_mark_node;
5172 }
5173 else if (TREE_CODE (decl) == FUNCTION_DECL)
5174 {
5175 if (member_template_p)
5176 {
5177 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5178 error ("member template %qD may not have virt-specifiers", decl);
5179 }
5180 if (DECL_DESTRUCTOR_P (decl))
5181 {
5182 /* [temp.mem]
5183
5184 A destructor shall not be a member template. */
5185 error ("destructor %qD declared as member template", decl);
5186 return error_mark_node;
5187 }
5188 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5189 && (!prototype_p (TREE_TYPE (decl))
5190 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5191 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5192 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5193 == void_list_node)))
5194 {
5195 /* [basic.stc.dynamic.allocation]
5196
5197 An allocation function can be a function
5198 template. ... Template allocation functions shall
5199 have two or more parameters. */
5200 error ("invalid template declaration of %qD", decl);
5201 return error_mark_node;
5202 }
5203 }
5204 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5205 && CLASS_TYPE_P (TREE_TYPE (decl)))
5206 /* OK */;
5207 else if (TREE_CODE (decl) == TYPE_DECL
5208 && TYPE_DECL_ALIAS_P (decl))
5209 /* alias-declaration */
5210 gcc_assert (!DECL_ARTIFICIAL (decl));
5211 else if (VAR_P (decl))
5212 /* C++14 variable template. */;
5213 else
5214 {
5215 error ("template declaration of %q#D", decl);
5216 return error_mark_node;
5217 }
5218 }
5219
5220 /* Check to see that the rules regarding the use of default
5221 arguments are not being violated. */
5222 check_default_tmpl_args (decl, current_template_parms,
5223 is_primary, is_partial, /*is_friend_decl=*/0);
5224
5225 /* Ensure that there are no parameter packs in the type of this
5226 declaration that have not been expanded. */
5227 if (TREE_CODE (decl) == FUNCTION_DECL)
5228 {
5229 /* Check each of the arguments individually to see if there are
5230 any bare parameter packs. */
5231 tree type = TREE_TYPE (decl);
5232 tree arg = DECL_ARGUMENTS (decl);
5233 tree argtype = TYPE_ARG_TYPES (type);
5234
5235 while (arg && argtype)
5236 {
5237 if (!DECL_PACK_P (arg)
5238 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5239 {
5240 /* This is a PARM_DECL that contains unexpanded parameter
5241 packs. We have already complained about this in the
5242 check_for_bare_parameter_packs call, so just replace
5243 these types with ERROR_MARK_NODE. */
5244 TREE_TYPE (arg) = error_mark_node;
5245 TREE_VALUE (argtype) = error_mark_node;
5246 }
5247
5248 arg = DECL_CHAIN (arg);
5249 argtype = TREE_CHAIN (argtype);
5250 }
5251
5252 /* Check for bare parameter packs in the return type and the
5253 exception specifiers. */
5254 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5255 /* Errors were already issued, set return type to int
5256 as the frontend doesn't expect error_mark_node as
5257 the return type. */
5258 TREE_TYPE (type) = integer_type_node;
5259 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5260 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5261 }
5262 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5263 && TYPE_DECL_ALIAS_P (decl))
5264 ? DECL_ORIGINAL_TYPE (decl)
5265 : TREE_TYPE (decl)))
5266 {
5267 TREE_TYPE (decl) = error_mark_node;
5268 return error_mark_node;
5269 }
5270
5271 if (is_partial)
5272 return process_partial_specialization (decl);
5273
5274 args = current_template_args ();
5275
5276 if (!ctx
5277 || TREE_CODE (ctx) == FUNCTION_DECL
5278 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5279 || (TREE_CODE (decl) == TYPE_DECL
5280 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5281 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5282 {
5283 if (DECL_LANG_SPECIFIC (decl)
5284 && DECL_TEMPLATE_INFO (decl)
5285 && DECL_TI_TEMPLATE (decl))
5286 tmpl = DECL_TI_TEMPLATE (decl);
5287 /* If DECL is a TYPE_DECL for a class-template, then there won't
5288 be DECL_LANG_SPECIFIC. The information equivalent to
5289 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5290 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5291 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5292 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5293 {
5294 /* Since a template declaration already existed for this
5295 class-type, we must be redeclaring it here. Make sure
5296 that the redeclaration is valid. */
5297 redeclare_class_template (TREE_TYPE (decl),
5298 current_template_parms,
5299 current_template_constraints ());
5300 /* We don't need to create a new TEMPLATE_DECL; just use the
5301 one we already had. */
5302 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5303 }
5304 else
5305 {
5306 tmpl = build_template_decl (decl, current_template_parms,
5307 member_template_p);
5308 new_template_p = 1;
5309
5310 if (DECL_LANG_SPECIFIC (decl)
5311 && DECL_TEMPLATE_SPECIALIZATION (decl))
5312 {
5313 /* A specialization of a member template of a template
5314 class. */
5315 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5316 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5317 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5318 }
5319 }
5320 }
5321 else
5322 {
5323 tree a, t, current, parms;
5324 int i;
5325 tree tinfo = get_template_info (decl);
5326
5327 if (!tinfo)
5328 {
5329 error ("template definition of non-template %q#D", decl);
5330 return error_mark_node;
5331 }
5332
5333 tmpl = TI_TEMPLATE (tinfo);
5334
5335 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5336 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5337 && DECL_TEMPLATE_SPECIALIZATION (decl)
5338 && DECL_MEMBER_TEMPLATE_P (tmpl))
5339 {
5340 tree new_tmpl;
5341
5342 /* The declaration is a specialization of a member
5343 template, declared outside the class. Therefore, the
5344 innermost template arguments will be NULL, so we
5345 replace them with the arguments determined by the
5346 earlier call to check_explicit_specialization. */
5347 args = DECL_TI_ARGS (decl);
5348
5349 new_tmpl
5350 = build_template_decl (decl, current_template_parms,
5351 member_template_p);
5352 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5353 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5354 DECL_TI_TEMPLATE (decl) = new_tmpl;
5355 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5356 DECL_TEMPLATE_INFO (new_tmpl)
5357 = build_template_info (tmpl, args);
5358
5359 register_specialization (new_tmpl,
5360 most_general_template (tmpl),
5361 args,
5362 is_friend, 0);
5363 return decl;
5364 }
5365
5366 /* Make sure the template headers we got make sense. */
5367
5368 parms = DECL_TEMPLATE_PARMS (tmpl);
5369 i = TMPL_PARMS_DEPTH (parms);
5370 if (TMPL_ARGS_DEPTH (args) != i)
5371 {
5372 error ("expected %d levels of template parms for %q#D, got %d",
5373 i, decl, TMPL_ARGS_DEPTH (args));
5374 DECL_INTERFACE_KNOWN (decl) = 1;
5375 return error_mark_node;
5376 }
5377 else
5378 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5379 {
5380 a = TMPL_ARGS_LEVEL (args, i);
5381 t = INNERMOST_TEMPLATE_PARMS (parms);
5382
5383 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5384 {
5385 if (current == decl)
5386 error ("got %d template parameters for %q#D",
5387 TREE_VEC_LENGTH (a), decl);
5388 else
5389 error ("got %d template parameters for %q#T",
5390 TREE_VEC_LENGTH (a), current);
5391 error (" but %d required", TREE_VEC_LENGTH (t));
5392 /* Avoid crash in import_export_decl. */
5393 DECL_INTERFACE_KNOWN (decl) = 1;
5394 return error_mark_node;
5395 }
5396
5397 if (current == decl)
5398 current = ctx;
5399 else if (current == NULL_TREE)
5400 /* Can happen in erroneous input. */
5401 break;
5402 else
5403 current = get_containing_scope (current);
5404 }
5405
5406 /* Check that the parms are used in the appropriate qualifying scopes
5407 in the declarator. */
5408 if (!comp_template_args
5409 (TI_ARGS (tinfo),
5410 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5411 {
5412 error ("\
5413 template arguments to %qD do not match original template %qD",
5414 decl, DECL_TEMPLATE_RESULT (tmpl));
5415 if (!uses_template_parms (TI_ARGS (tinfo)))
5416 inform (input_location, "use template<> for an explicit specialization");
5417 /* Avoid crash in import_export_decl. */
5418 DECL_INTERFACE_KNOWN (decl) = 1;
5419 return error_mark_node;
5420 }
5421 }
5422
5423 DECL_TEMPLATE_RESULT (tmpl) = decl;
5424 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5425
5426 /* Push template declarations for global functions and types. Note
5427 that we do not try to push a global template friend declared in a
5428 template class; such a thing may well depend on the template
5429 parameters of the class. */
5430 if (new_template_p && !ctx
5431 && !(is_friend && template_class_depth (current_class_type) > 0))
5432 {
5433 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5434 if (tmpl == error_mark_node)
5435 return error_mark_node;
5436
5437 /* Hide template friend classes that haven't been declared yet. */
5438 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5439 {
5440 DECL_ANTICIPATED (tmpl) = 1;
5441 DECL_FRIEND_P (tmpl) = 1;
5442 }
5443 }
5444
5445 if (is_primary)
5446 {
5447 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5448 int i;
5449
5450 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5451 if (DECL_CONV_FN_P (tmpl))
5452 {
5453 int depth = TMPL_PARMS_DEPTH (parms);
5454
5455 /* It is a conversion operator. See if the type converted to
5456 depends on innermost template operands. */
5457
5458 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5459 depth))
5460 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5461 }
5462
5463 /* Give template template parms a DECL_CONTEXT of the template
5464 for which they are a parameter. */
5465 parms = INNERMOST_TEMPLATE_PARMS (parms);
5466 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5467 {
5468 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5469 if (TREE_CODE (parm) == TEMPLATE_DECL)
5470 DECL_CONTEXT (parm) = tmpl;
5471 }
5472
5473 if (TREE_CODE (decl) == TYPE_DECL
5474 && TYPE_DECL_ALIAS_P (decl)
5475 && complex_alias_template_p (tmpl))
5476 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5477 }
5478
5479 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5480 back to its most general template. If TMPL is a specialization,
5481 ARGS may only have the innermost set of arguments. Add the missing
5482 argument levels if necessary. */
5483 if (DECL_TEMPLATE_INFO (tmpl))
5484 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5485
5486 info = build_template_info (tmpl, args);
5487
5488 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5489 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5490 else
5491 {
5492 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5493 retrofit_lang_decl (decl);
5494 if (DECL_LANG_SPECIFIC (decl))
5495 DECL_TEMPLATE_INFO (decl) = info;
5496 }
5497
5498 if (flag_implicit_templates
5499 && !is_friend
5500 && TREE_PUBLIC (decl)
5501 && VAR_OR_FUNCTION_DECL_P (decl))
5502 /* Set DECL_COMDAT on template instantiations; if we force
5503 them to be emitted by explicit instantiation or -frepo,
5504 mark_needed will tell cgraph to do the right thing. */
5505 DECL_COMDAT (decl) = true;
5506
5507 return DECL_TEMPLATE_RESULT (tmpl);
5508 }
5509
5510 tree
5511 push_template_decl (tree decl)
5512 {
5513 return push_template_decl_real (decl, false);
5514 }
5515
5516 /* FN is an inheriting constructor that inherits from the constructor
5517 template INHERITED; turn FN into a constructor template with a matching
5518 template header. */
5519
5520 tree
5521 add_inherited_template_parms (tree fn, tree inherited)
5522 {
5523 tree inner_parms
5524 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5525 inner_parms = copy_node (inner_parms);
5526 tree parms
5527 = tree_cons (size_int (processing_template_decl + 1),
5528 inner_parms, current_template_parms);
5529 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5530 tree args = template_parms_to_args (parms);
5531 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5532 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5533 DECL_TEMPLATE_RESULT (tmpl) = fn;
5534 DECL_ARTIFICIAL (tmpl) = true;
5535 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5536 return tmpl;
5537 }
5538
5539 /* Called when a class template TYPE is redeclared with the indicated
5540 template PARMS, e.g.:
5541
5542 template <class T> struct S;
5543 template <class T> struct S {}; */
5544
5545 bool
5546 redeclare_class_template (tree type, tree parms, tree cons)
5547 {
5548 tree tmpl;
5549 tree tmpl_parms;
5550 int i;
5551
5552 if (!TYPE_TEMPLATE_INFO (type))
5553 {
5554 error ("%qT is not a template type", type);
5555 return false;
5556 }
5557
5558 tmpl = TYPE_TI_TEMPLATE (type);
5559 if (!PRIMARY_TEMPLATE_P (tmpl))
5560 /* The type is nested in some template class. Nothing to worry
5561 about here; there are no new template parameters for the nested
5562 type. */
5563 return true;
5564
5565 if (!parms)
5566 {
5567 error ("template specifiers not specified in declaration of %qD",
5568 tmpl);
5569 return false;
5570 }
5571
5572 parms = INNERMOST_TEMPLATE_PARMS (parms);
5573 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5574
5575 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5576 {
5577 error_n (input_location, TREE_VEC_LENGTH (parms),
5578 "redeclared with %d template parameter",
5579 "redeclared with %d template parameters",
5580 TREE_VEC_LENGTH (parms));
5581 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5582 "previous declaration %qD used %d template parameter",
5583 "previous declaration %qD used %d template parameters",
5584 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5585 return false;
5586 }
5587
5588 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5589 {
5590 tree tmpl_parm;
5591 tree parm;
5592 tree tmpl_default;
5593 tree parm_default;
5594
5595 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5596 || TREE_VEC_ELT (parms, i) == error_mark_node)
5597 continue;
5598
5599 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5600 if (error_operand_p (tmpl_parm))
5601 return false;
5602
5603 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5604 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5605 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5606
5607 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5608 TEMPLATE_DECL. */
5609 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5610 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5611 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5612 || (TREE_CODE (tmpl_parm) != PARM_DECL
5613 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5614 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5615 || (TREE_CODE (tmpl_parm) == PARM_DECL
5616 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5617 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5618 {
5619 error ("template parameter %q+#D", tmpl_parm);
5620 error ("redeclared here as %q#D", parm);
5621 return false;
5622 }
5623
5624 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5625 {
5626 /* We have in [temp.param]:
5627
5628 A template-parameter may not be given default arguments
5629 by two different declarations in the same scope. */
5630 error_at (input_location, "redefinition of default argument for %q#D", parm);
5631 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5632 "original definition appeared here");
5633 return false;
5634 }
5635
5636 if (parm_default != NULL_TREE)
5637 /* Update the previous template parameters (which are the ones
5638 that will really count) with the new default value. */
5639 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5640 else if (tmpl_default != NULL_TREE)
5641 /* Update the new parameters, too; they'll be used as the
5642 parameters for any members. */
5643 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5644
5645 /* Give each template template parm in this redeclaration a
5646 DECL_CONTEXT of the template for which they are a parameter. */
5647 if (TREE_CODE (parm) == TEMPLATE_DECL)
5648 {
5649 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5650 DECL_CONTEXT (parm) = tmpl;
5651 }
5652 }
5653
5654 // Cannot redeclare a class template with a different set of constraints.
5655 if (!equivalent_constraints (get_constraints (tmpl), cons))
5656 {
5657 error_at (input_location, "redeclaration %q#D with different "
5658 "constraints", tmpl);
5659 inform (DECL_SOURCE_LOCATION (tmpl),
5660 "original declaration appeared here");
5661 }
5662
5663 return true;
5664 }
5665
5666 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5667 to be used when the caller has already checked
5668 (processing_template_decl
5669 && !instantiation_dependent_expression_p (expr)
5670 && potential_constant_expression (expr))
5671 and cleared processing_template_decl. */
5672
5673 tree
5674 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5675 {
5676 return tsubst_copy_and_build (expr,
5677 /*args=*/NULL_TREE,
5678 complain,
5679 /*in_decl=*/NULL_TREE,
5680 /*function_p=*/false,
5681 /*integral_constant_expression_p=*/true);
5682 }
5683
5684 /* Simplify EXPR if it is a non-dependent expression. Returns the
5685 (possibly simplified) expression. */
5686
5687 tree
5688 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5689 {
5690 if (expr == NULL_TREE)
5691 return NULL_TREE;
5692
5693 /* If we're in a template, but EXPR isn't value dependent, simplify
5694 it. We're supposed to treat:
5695
5696 template <typename T> void f(T[1 + 1]);
5697 template <typename T> void f(T[2]);
5698
5699 as two declarations of the same function, for example. */
5700 if (processing_template_decl
5701 && potential_nondependent_constant_expression (expr))
5702 {
5703 processing_template_decl_sentinel s;
5704 expr = instantiate_non_dependent_expr_internal (expr, complain);
5705 }
5706 return expr;
5707 }
5708
5709 tree
5710 instantiate_non_dependent_expr (tree expr)
5711 {
5712 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5713 }
5714
5715 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5716 an uninstantiated expression. */
5717
5718 tree
5719 instantiate_non_dependent_or_null (tree expr)
5720 {
5721 if (expr == NULL_TREE)
5722 return NULL_TREE;
5723 if (processing_template_decl)
5724 {
5725 if (!potential_nondependent_constant_expression (expr))
5726 expr = NULL_TREE;
5727 else
5728 {
5729 processing_template_decl_sentinel s;
5730 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5731 }
5732 }
5733 return expr;
5734 }
5735
5736 /* True iff T is a specialization of a variable template. */
5737
5738 bool
5739 variable_template_specialization_p (tree t)
5740 {
5741 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5742 return false;
5743 tree tmpl = DECL_TI_TEMPLATE (t);
5744 return variable_template_p (tmpl);
5745 }
5746
5747 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5748 template declaration, or a TYPE_DECL for an alias declaration. */
5749
5750 bool
5751 alias_type_or_template_p (tree t)
5752 {
5753 if (t == NULL_TREE)
5754 return false;
5755 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5756 || (TYPE_P (t)
5757 && TYPE_NAME (t)
5758 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5759 || DECL_ALIAS_TEMPLATE_P (t));
5760 }
5761
5762 /* Return TRUE iff T is a specialization of an alias template. */
5763
5764 bool
5765 alias_template_specialization_p (const_tree t)
5766 {
5767 /* It's an alias template specialization if it's an alias and its
5768 TYPE_NAME is a specialization of a primary template. */
5769 if (TYPE_ALIAS_P (t))
5770 {
5771 tree name = TYPE_NAME (t);
5772 if (DECL_LANG_SPECIFIC (name))
5773 if (tree ti = DECL_TEMPLATE_INFO (name))
5774 {
5775 tree tmpl = TI_TEMPLATE (ti);
5776 return PRIMARY_TEMPLATE_P (tmpl);
5777 }
5778 }
5779 return false;
5780 }
5781
5782 /* An alias template is complex from a SFINAE perspective if a template-id
5783 using that alias can be ill-formed when the expansion is not, as with
5784 the void_t template. We determine this by checking whether the
5785 expansion for the alias template uses all its template parameters. */
5786
5787 struct uses_all_template_parms_data
5788 {
5789 int level;
5790 bool *seen;
5791 };
5792
5793 static int
5794 uses_all_template_parms_r (tree t, void *data_)
5795 {
5796 struct uses_all_template_parms_data &data
5797 = *(struct uses_all_template_parms_data*)data_;
5798 tree idx = get_template_parm_index (t);
5799
5800 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5801 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5802 return 0;
5803 }
5804
5805 static bool
5806 complex_alias_template_p (const_tree tmpl)
5807 {
5808 struct uses_all_template_parms_data data;
5809 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5810 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5811 data.level = TMPL_PARMS_DEPTH (parms);
5812 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5813 data.seen = XALLOCAVEC (bool, len);
5814 for (int i = 0; i < len; ++i)
5815 data.seen[i] = false;
5816
5817 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5818 for (int i = 0; i < len; ++i)
5819 if (!data.seen[i])
5820 return true;
5821 return false;
5822 }
5823
5824 /* Return TRUE iff T is a specialization of a complex alias template with
5825 dependent template-arguments. */
5826
5827 bool
5828 dependent_alias_template_spec_p (const_tree t)
5829 {
5830 return (alias_template_specialization_p (t)
5831 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5832 && (any_dependent_template_arguments_p
5833 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5834 }
5835
5836 /* Return the number of innermost template parameters in TMPL. */
5837
5838 static int
5839 num_innermost_template_parms (tree tmpl)
5840 {
5841 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5842 return TREE_VEC_LENGTH (parms);
5843 }
5844
5845 /* Return either TMPL or another template that it is equivalent to under DR
5846 1286: An alias that just changes the name of a template is equivalent to
5847 the other template. */
5848
5849 static tree
5850 get_underlying_template (tree tmpl)
5851 {
5852 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5853 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5854 {
5855 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5856 if (TYPE_TEMPLATE_INFO (result))
5857 {
5858 tree sub = TYPE_TI_TEMPLATE (result);
5859 if (PRIMARY_TEMPLATE_P (sub)
5860 && (num_innermost_template_parms (tmpl)
5861 == num_innermost_template_parms (sub)))
5862 {
5863 tree alias_args = INNERMOST_TEMPLATE_ARGS
5864 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5865 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5866 break;
5867 /* The alias type is equivalent to the pattern of the
5868 underlying template, so strip the alias. */
5869 tmpl = sub;
5870 continue;
5871 }
5872 }
5873 break;
5874 }
5875 return tmpl;
5876 }
5877
5878 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5879 must be a reference-to-function or a pointer-to-function type, as specified
5880 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5881 and check that the resulting function has external linkage. */
5882
5883 static tree
5884 convert_nontype_argument_function (tree type, tree expr,
5885 tsubst_flags_t complain)
5886 {
5887 tree fns = expr;
5888 tree fn, fn_no_ptr;
5889 linkage_kind linkage;
5890
5891 fn = instantiate_type (type, fns, tf_none);
5892 if (fn == error_mark_node)
5893 return error_mark_node;
5894
5895 fn_no_ptr = strip_fnptr_conv (fn);
5896 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5897 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5898 if (BASELINK_P (fn_no_ptr))
5899 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5900
5901 /* [temp.arg.nontype]/1
5902
5903 A template-argument for a non-type, non-template template-parameter
5904 shall be one of:
5905 [...]
5906 -- the address of an object or function with external [C++11: or
5907 internal] linkage. */
5908
5909 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5910 {
5911 if (complain & tf_error)
5912 {
5913 error ("%qE is not a valid template argument for type %qT",
5914 expr, type);
5915 if (TYPE_PTR_P (type))
5916 inform (input_location, "it must be the address of a function "
5917 "with external linkage");
5918 else
5919 inform (input_location, "it must be the name of a function with "
5920 "external linkage");
5921 }
5922 return NULL_TREE;
5923 }
5924
5925 linkage = decl_linkage (fn_no_ptr);
5926 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5927 {
5928 if (complain & tf_error)
5929 {
5930 if (cxx_dialect >= cxx11)
5931 error ("%qE is not a valid template argument for type %qT "
5932 "because %qD has no linkage",
5933 expr, type, fn_no_ptr);
5934 else
5935 error ("%qE is not a valid template argument for type %qT "
5936 "because %qD does not have external linkage",
5937 expr, type, fn_no_ptr);
5938 }
5939 return NULL_TREE;
5940 }
5941
5942 if (TREE_CODE (type) == REFERENCE_TYPE)
5943 fn = build_address (fn);
5944 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
5945 fn = build_nop (type, fn);
5946
5947 return fn;
5948 }
5949
5950 /* Subroutine of convert_nontype_argument.
5951 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5952 Emit an error otherwise. */
5953
5954 static bool
5955 check_valid_ptrmem_cst_expr (tree type, tree expr,
5956 tsubst_flags_t complain)
5957 {
5958 STRIP_NOPS (expr);
5959 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5960 return true;
5961 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5962 return true;
5963 if (processing_template_decl
5964 && TREE_CODE (expr) == ADDR_EXPR
5965 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5966 return true;
5967 if (complain & tf_error)
5968 {
5969 error ("%qE is not a valid template argument for type %qT",
5970 expr, type);
5971 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5972 }
5973 return false;
5974 }
5975
5976 /* Returns TRUE iff the address of OP is value-dependent.
5977
5978 14.6.2.4 [temp.dep.temp]:
5979 A non-integral non-type template-argument is dependent if its type is
5980 dependent or it has either of the following forms
5981 qualified-id
5982 & qualified-id
5983 and contains a nested-name-specifier which specifies a class-name that
5984 names a dependent type.
5985
5986 We generalize this to just say that the address of a member of a
5987 dependent class is value-dependent; the above doesn't cover the
5988 address of a static data member named with an unqualified-id. */
5989
5990 static bool
5991 has_value_dependent_address (tree op)
5992 {
5993 /* We could use get_inner_reference here, but there's no need;
5994 this is only relevant for template non-type arguments, which
5995 can only be expressed as &id-expression. */
5996 if (DECL_P (op))
5997 {
5998 tree ctx = CP_DECL_CONTEXT (op);
5999 if (TYPE_P (ctx) && dependent_type_p (ctx))
6000 return true;
6001 }
6002
6003 return false;
6004 }
6005
6006 /* The next set of functions are used for providing helpful explanatory
6007 diagnostics for failed overload resolution. Their messages should be
6008 indented by two spaces for consistency with the messages in
6009 call.c */
6010
6011 static int
6012 unify_success (bool /*explain_p*/)
6013 {
6014 return 0;
6015 }
6016
6017 static int
6018 unify_parameter_deduction_failure (bool explain_p, tree parm)
6019 {
6020 if (explain_p)
6021 inform (input_location,
6022 " couldn't deduce template parameter %qD", parm);
6023 return 1;
6024 }
6025
6026 static int
6027 unify_invalid (bool /*explain_p*/)
6028 {
6029 return 1;
6030 }
6031
6032 static int
6033 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6034 {
6035 if (explain_p)
6036 inform (input_location,
6037 " types %qT and %qT have incompatible cv-qualifiers",
6038 parm, arg);
6039 return 1;
6040 }
6041
6042 static int
6043 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6044 {
6045 if (explain_p)
6046 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6047 return 1;
6048 }
6049
6050 static int
6051 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6052 {
6053 if (explain_p)
6054 inform (input_location,
6055 " template parameter %qD is not a parameter pack, but "
6056 "argument %qD is",
6057 parm, arg);
6058 return 1;
6059 }
6060
6061 static int
6062 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6063 {
6064 if (explain_p)
6065 inform (input_location,
6066 " template argument %qE does not match "
6067 "pointer-to-member constant %qE",
6068 arg, parm);
6069 return 1;
6070 }
6071
6072 static int
6073 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6074 {
6075 if (explain_p)
6076 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6077 return 1;
6078 }
6079
6080 static int
6081 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6082 {
6083 if (explain_p)
6084 inform (input_location,
6085 " inconsistent parameter pack deduction with %qT and %qT",
6086 old_arg, new_arg);
6087 return 1;
6088 }
6089
6090 static int
6091 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6092 {
6093 if (explain_p)
6094 {
6095 if (TYPE_P (parm))
6096 inform (input_location,
6097 " deduced conflicting types for parameter %qT (%qT and %qT)",
6098 parm, first, second);
6099 else
6100 inform (input_location,
6101 " deduced conflicting values for non-type parameter "
6102 "%qE (%qE and %qE)", parm, first, second);
6103 }
6104 return 1;
6105 }
6106
6107 static int
6108 unify_vla_arg (bool explain_p, tree arg)
6109 {
6110 if (explain_p)
6111 inform (input_location,
6112 " variable-sized array type %qT is not "
6113 "a valid template argument",
6114 arg);
6115 return 1;
6116 }
6117
6118 static int
6119 unify_method_type_error (bool explain_p, tree arg)
6120 {
6121 if (explain_p)
6122 inform (input_location,
6123 " member function type %qT is not a valid template argument",
6124 arg);
6125 return 1;
6126 }
6127
6128 static int
6129 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6130 {
6131 if (explain_p)
6132 {
6133 if (least_p)
6134 inform_n (input_location, wanted,
6135 " candidate expects at least %d argument, %d provided",
6136 " candidate expects at least %d arguments, %d provided",
6137 wanted, have);
6138 else
6139 inform_n (input_location, wanted,
6140 " candidate expects %d argument, %d provided",
6141 " candidate expects %d arguments, %d provided",
6142 wanted, have);
6143 }
6144 return 1;
6145 }
6146
6147 static int
6148 unify_too_many_arguments (bool explain_p, int have, int wanted)
6149 {
6150 return unify_arity (explain_p, have, wanted);
6151 }
6152
6153 static int
6154 unify_too_few_arguments (bool explain_p, int have, int wanted,
6155 bool least_p = false)
6156 {
6157 return unify_arity (explain_p, have, wanted, least_p);
6158 }
6159
6160 static int
6161 unify_arg_conversion (bool explain_p, tree to_type,
6162 tree from_type, tree arg)
6163 {
6164 if (explain_p)
6165 inform (EXPR_LOC_OR_LOC (arg, input_location),
6166 " cannot convert %qE (type %qT) to type %qT",
6167 arg, from_type, to_type);
6168 return 1;
6169 }
6170
6171 static int
6172 unify_no_common_base (bool explain_p, enum template_base_result r,
6173 tree parm, tree arg)
6174 {
6175 if (explain_p)
6176 switch (r)
6177 {
6178 case tbr_ambiguous_baseclass:
6179 inform (input_location, " %qT is an ambiguous base class of %qT",
6180 parm, arg);
6181 break;
6182 default:
6183 inform (input_location, " %qT is not derived from %qT", arg, parm);
6184 break;
6185 }
6186 return 1;
6187 }
6188
6189 static int
6190 unify_inconsistent_template_template_parameters (bool explain_p)
6191 {
6192 if (explain_p)
6193 inform (input_location,
6194 " template parameters of a template template argument are "
6195 "inconsistent with other deduced template arguments");
6196 return 1;
6197 }
6198
6199 static int
6200 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6201 {
6202 if (explain_p)
6203 inform (input_location,
6204 " can't deduce a template for %qT from non-template type %qT",
6205 parm, arg);
6206 return 1;
6207 }
6208
6209 static int
6210 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6211 {
6212 if (explain_p)
6213 inform (input_location,
6214 " template argument %qE does not match %qE", arg, parm);
6215 return 1;
6216 }
6217
6218 static int
6219 unify_overload_resolution_failure (bool explain_p, tree arg)
6220 {
6221 if (explain_p)
6222 inform (input_location,
6223 " could not resolve address from overloaded function %qE",
6224 arg);
6225 return 1;
6226 }
6227
6228 /* Attempt to convert the non-type template parameter EXPR to the
6229 indicated TYPE. If the conversion is successful, return the
6230 converted value. If the conversion is unsuccessful, return
6231 NULL_TREE if we issued an error message, or error_mark_node if we
6232 did not. We issue error messages for out-and-out bad template
6233 parameters, but not simply because the conversion failed, since we
6234 might be just trying to do argument deduction. Both TYPE and EXPR
6235 must be non-dependent.
6236
6237 The conversion follows the special rules described in
6238 [temp.arg.nontype], and it is much more strict than an implicit
6239 conversion.
6240
6241 This function is called twice for each template argument (see
6242 lookup_template_class for a more accurate description of this
6243 problem). This means that we need to handle expressions which
6244 are not valid in a C++ source, but can be created from the
6245 first call (for instance, casts to perform conversions). These
6246 hacks can go away after we fix the double coercion problem. */
6247
6248 static tree
6249 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6250 {
6251 tree expr_type;
6252
6253 /* Detect immediately string literals as invalid non-type argument.
6254 This special-case is not needed for correctness (we would easily
6255 catch this later), but only to provide better diagnostic for this
6256 common user mistake. As suggested by DR 100, we do not mention
6257 linkage issues in the diagnostic as this is not the point. */
6258 /* FIXME we're making this OK. */
6259 if (TREE_CODE (expr) == STRING_CST)
6260 {
6261 if (complain & tf_error)
6262 error ("%qE is not a valid template argument for type %qT "
6263 "because string literals can never be used in this context",
6264 expr, type);
6265 return NULL_TREE;
6266 }
6267
6268 /* Add the ADDR_EXPR now for the benefit of
6269 value_dependent_expression_p. */
6270 if (TYPE_PTROBV_P (type)
6271 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6272 {
6273 expr = decay_conversion (expr, complain);
6274 if (expr == error_mark_node)
6275 return error_mark_node;
6276 }
6277
6278 /* If we are in a template, EXPR may be non-dependent, but still
6279 have a syntactic, rather than semantic, form. For example, EXPR
6280 might be a SCOPE_REF, rather than the VAR_DECL to which the
6281 SCOPE_REF refers. Preserving the qualifying scope is necessary
6282 so that access checking can be performed when the template is
6283 instantiated -- but here we need the resolved form so that we can
6284 convert the argument. */
6285 bool non_dep = false;
6286 if (TYPE_REF_OBJ_P (type)
6287 && has_value_dependent_address (expr))
6288 /* If we want the address and it's value-dependent, don't fold. */;
6289 else if (processing_template_decl
6290 && potential_nondependent_constant_expression (expr))
6291 non_dep = true;
6292 if (error_operand_p (expr))
6293 return error_mark_node;
6294 expr_type = TREE_TYPE (expr);
6295 if (TREE_CODE (type) == REFERENCE_TYPE)
6296 expr = mark_lvalue_use (expr);
6297 else
6298 expr = mark_rvalue_use (expr);
6299
6300 /* If the argument is non-dependent, perform any conversions in
6301 non-dependent context as well. */
6302 processing_template_decl_sentinel s (non_dep);
6303 if (non_dep)
6304 expr = instantiate_non_dependent_expr_internal (expr, complain);
6305
6306 if (value_dependent_expression_p (expr))
6307 expr = canonicalize_expr_argument (expr, complain);
6308
6309 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6310 to a non-type argument of "nullptr". */
6311 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6312 expr = fold_simple (convert (type, expr));
6313
6314 /* In C++11, integral or enumeration non-type template arguments can be
6315 arbitrary constant expressions. Pointer and pointer to
6316 member arguments can be general constant expressions that evaluate
6317 to a null value, but otherwise still need to be of a specific form. */
6318 if (cxx_dialect >= cxx11)
6319 {
6320 if (TREE_CODE (expr) == PTRMEM_CST)
6321 /* A PTRMEM_CST is already constant, and a valid template
6322 argument for a parameter of pointer to member type, we just want
6323 to leave it in that form rather than lower it to a
6324 CONSTRUCTOR. */;
6325 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6326 expr = maybe_constant_value (expr);
6327 else if (cxx_dialect >= cxx1z)
6328 {
6329 if (TREE_CODE (type) != REFERENCE_TYPE)
6330 expr = maybe_constant_value (expr);
6331 else if (REFERENCE_REF_P (expr))
6332 {
6333 expr = TREE_OPERAND (expr, 0);
6334 expr = maybe_constant_value (expr);
6335 expr = convert_from_reference (expr);
6336 }
6337 }
6338 else if (TYPE_PTR_OR_PTRMEM_P (type))
6339 {
6340 tree folded = maybe_constant_value (expr);
6341 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6342 : null_member_pointer_value_p (folded))
6343 expr = folded;
6344 }
6345 }
6346
6347 /* HACK: Due to double coercion, we can get a
6348 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6349 which is the tree that we built on the first call (see
6350 below when coercing to reference to object or to reference to
6351 function). We just strip everything and get to the arg.
6352 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6353 for examples. */
6354 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6355 {
6356 tree probe_type, probe = expr;
6357 if (REFERENCE_REF_P (probe))
6358 probe = TREE_OPERAND (probe, 0);
6359 probe_type = TREE_TYPE (probe);
6360 if (TREE_CODE (probe) == NOP_EXPR)
6361 {
6362 /* ??? Maybe we could use convert_from_reference here, but we
6363 would need to relax its constraints because the NOP_EXPR
6364 could actually change the type to something more cv-qualified,
6365 and this is not folded by convert_from_reference. */
6366 tree addr = TREE_OPERAND (probe, 0);
6367 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6368 && TREE_CODE (addr) == ADDR_EXPR
6369 && TYPE_PTR_P (TREE_TYPE (addr))
6370 && (same_type_ignoring_top_level_qualifiers_p
6371 (TREE_TYPE (probe_type),
6372 TREE_TYPE (TREE_TYPE (addr)))))
6373 {
6374 expr = TREE_OPERAND (addr, 0);
6375 expr_type = TREE_TYPE (probe_type);
6376 }
6377 }
6378 }
6379
6380 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6381 parameter is a pointer to object, through decay and
6382 qualification conversion. Let's strip everything. */
6383 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6384 {
6385 tree probe = expr;
6386 STRIP_NOPS (probe);
6387 if (TREE_CODE (probe) == ADDR_EXPR
6388 && TYPE_PTR_P (TREE_TYPE (probe)))
6389 {
6390 /* Skip the ADDR_EXPR only if it is part of the decay for
6391 an array. Otherwise, it is part of the original argument
6392 in the source code. */
6393 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6394 probe = TREE_OPERAND (probe, 0);
6395 expr = probe;
6396 expr_type = TREE_TYPE (expr);
6397 }
6398 }
6399
6400 /* [temp.arg.nontype]/5, bullet 1
6401
6402 For a non-type template-parameter of integral or enumeration type,
6403 integral promotions (_conv.prom_) and integral conversions
6404 (_conv.integral_) are applied. */
6405 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6406 {
6407 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6408 t = maybe_constant_value (t);
6409 if (t != error_mark_node)
6410 expr = t;
6411
6412 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6413 return error_mark_node;
6414
6415 /* Notice that there are constant expressions like '4 % 0' which
6416 do not fold into integer constants. */
6417 if (TREE_CODE (expr) != INTEGER_CST
6418 && !value_dependent_expression_p (expr))
6419 {
6420 if (complain & tf_error)
6421 {
6422 int errs = errorcount, warns = warningcount + werrorcount;
6423 if (processing_template_decl
6424 && !require_potential_constant_expression (expr))
6425 return NULL_TREE;
6426 expr = cxx_constant_value (expr);
6427 if (errorcount > errs || warningcount + werrorcount > warns)
6428 inform (EXPR_LOC_OR_LOC (expr, input_location),
6429 "in template argument for type %qT ", type);
6430 if (expr == error_mark_node)
6431 return NULL_TREE;
6432 /* else cxx_constant_value complained but gave us
6433 a real constant, so go ahead. */
6434 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6435 }
6436 else
6437 return NULL_TREE;
6438 }
6439
6440 /* Avoid typedef problems. */
6441 if (TREE_TYPE (expr) != type)
6442 expr = fold_convert (type, expr);
6443 }
6444 /* [temp.arg.nontype]/5, bullet 2
6445
6446 For a non-type template-parameter of type pointer to object,
6447 qualification conversions (_conv.qual_) and the array-to-pointer
6448 conversion (_conv.array_) are applied. */
6449 else if (TYPE_PTROBV_P (type))
6450 {
6451 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6452
6453 A template-argument for a non-type, non-template template-parameter
6454 shall be one of: [...]
6455
6456 -- the name of a non-type template-parameter;
6457 -- the address of an object or function with external linkage, [...]
6458 expressed as "& id-expression" where the & is optional if the name
6459 refers to a function or array, or if the corresponding
6460 template-parameter is a reference.
6461
6462 Here, we do not care about functions, as they are invalid anyway
6463 for a parameter of type pointer-to-object. */
6464
6465 if (value_dependent_expression_p (expr))
6466 /* Non-type template parameters are OK. */
6467 ;
6468 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6469 /* Null pointer values are OK in C++11. */;
6470 else if (TREE_CODE (expr) != ADDR_EXPR
6471 && TREE_CODE (expr_type) != ARRAY_TYPE)
6472 {
6473 if (VAR_P (expr))
6474 {
6475 if (complain & tf_error)
6476 error ("%qD is not a valid template argument "
6477 "because %qD is a variable, not the address of "
6478 "a variable", expr, expr);
6479 return NULL_TREE;
6480 }
6481 if (POINTER_TYPE_P (expr_type))
6482 {
6483 if (complain & tf_error)
6484 error ("%qE is not a valid template argument for %qT "
6485 "because it is not the address of a variable",
6486 expr, type);
6487 return NULL_TREE;
6488 }
6489 /* Other values, like integer constants, might be valid
6490 non-type arguments of some other type. */
6491 return error_mark_node;
6492 }
6493 else
6494 {
6495 tree decl;
6496
6497 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6498 ? TREE_OPERAND (expr, 0) : expr);
6499 if (!VAR_P (decl))
6500 {
6501 if (complain & tf_error)
6502 error ("%qE is not a valid template argument of type %qT "
6503 "because %qE is not a variable", expr, type, decl);
6504 return NULL_TREE;
6505 }
6506 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6507 {
6508 if (complain & tf_error)
6509 error ("%qE is not a valid template argument of type %qT "
6510 "because %qD does not have external linkage",
6511 expr, type, decl);
6512 return NULL_TREE;
6513 }
6514 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6515 {
6516 if (complain & tf_error)
6517 error ("%qE is not a valid template argument of type %qT "
6518 "because %qD has no linkage", expr, type, decl);
6519 return NULL_TREE;
6520 }
6521 }
6522
6523 expr = decay_conversion (expr, complain);
6524 if (expr == error_mark_node)
6525 return error_mark_node;
6526
6527 expr = perform_qualification_conversions (type, expr);
6528 if (expr == error_mark_node)
6529 return error_mark_node;
6530 }
6531 /* [temp.arg.nontype]/5, bullet 3
6532
6533 For a non-type template-parameter of type reference to object, no
6534 conversions apply. The type referred to by the reference may be more
6535 cv-qualified than the (otherwise identical) type of the
6536 template-argument. The template-parameter is bound directly to the
6537 template-argument, which must be an lvalue. */
6538 else if (TYPE_REF_OBJ_P (type))
6539 {
6540 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6541 expr_type))
6542 return error_mark_node;
6543
6544 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6545 {
6546 if (complain & tf_error)
6547 error ("%qE is not a valid template argument for type %qT "
6548 "because of conflicts in cv-qualification", expr, type);
6549 return NULL_TREE;
6550 }
6551
6552 if (!lvalue_p (expr))
6553 {
6554 if (complain & tf_error)
6555 error ("%qE is not a valid template argument for type %qT "
6556 "because it is not an lvalue", expr, type);
6557 return NULL_TREE;
6558 }
6559
6560 /* [temp.arg.nontype]/1
6561
6562 A template-argument for a non-type, non-template template-parameter
6563 shall be one of: [...]
6564
6565 -- the address of an object or function with external linkage. */
6566 if (INDIRECT_REF_P (expr)
6567 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6568 {
6569 expr = TREE_OPERAND (expr, 0);
6570 if (DECL_P (expr))
6571 {
6572 if (complain & tf_error)
6573 error ("%q#D is not a valid template argument for type %qT "
6574 "because a reference variable does not have a constant "
6575 "address", expr, type);
6576 return NULL_TREE;
6577 }
6578 }
6579
6580 if (!value_dependent_expression_p (expr))
6581 {
6582 if (!DECL_P (expr))
6583 {
6584 if (complain & tf_error)
6585 error ("%qE is not a valid template argument for type %qT "
6586 "because it is not an object with linkage",
6587 expr, type);
6588 return NULL_TREE;
6589 }
6590
6591 /* DR 1155 allows internal linkage in C++11 and up. */
6592 linkage_kind linkage = decl_linkage (expr);
6593 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6594 {
6595 if (complain & tf_error)
6596 error ("%qE is not a valid template argument for type %qT "
6597 "because object %qD does not have linkage",
6598 expr, type, expr);
6599 return NULL_TREE;
6600 }
6601
6602 expr = build_nop (type, build_address (expr));
6603 }
6604 }
6605 /* [temp.arg.nontype]/5, bullet 4
6606
6607 For a non-type template-parameter of type pointer to function, only
6608 the function-to-pointer conversion (_conv.func_) is applied. If the
6609 template-argument represents a set of overloaded functions (or a
6610 pointer to such), the matching function is selected from the set
6611 (_over.over_). */
6612 else if (TYPE_PTRFN_P (type))
6613 {
6614 /* If the argument is a template-id, we might not have enough
6615 context information to decay the pointer. */
6616 if (!type_unknown_p (expr_type))
6617 {
6618 expr = decay_conversion (expr, complain);
6619 if (expr == error_mark_node)
6620 return error_mark_node;
6621 }
6622
6623 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6624 /* Null pointer values are OK in C++11. */
6625 return perform_qualification_conversions (type, expr);
6626
6627 if (!value_dependent_expression_p (expr))
6628 expr = convert_nontype_argument_function (type, expr, complain);
6629 if (!expr || expr == error_mark_node)
6630 return expr;
6631 }
6632 /* [temp.arg.nontype]/5, bullet 5
6633
6634 For a non-type template-parameter of type reference to function, no
6635 conversions apply. If the template-argument represents a set of
6636 overloaded functions, the matching function is selected from the set
6637 (_over.over_). */
6638 else if (TYPE_REFFN_P (type))
6639 {
6640 if (TREE_CODE (expr) == ADDR_EXPR)
6641 {
6642 if (complain & tf_error)
6643 {
6644 error ("%qE is not a valid template argument for type %qT "
6645 "because it is a pointer", expr, type);
6646 inform (input_location, "try using %qE instead",
6647 TREE_OPERAND (expr, 0));
6648 }
6649 return NULL_TREE;
6650 }
6651
6652 if (!value_dependent_expression_p (expr))
6653 expr = convert_nontype_argument_function (type, expr, complain);
6654 if (!expr || expr == error_mark_node)
6655 return expr;
6656 }
6657 /* [temp.arg.nontype]/5, bullet 6
6658
6659 For a non-type template-parameter of type pointer to member function,
6660 no conversions apply. If the template-argument represents a set of
6661 overloaded member functions, the matching member function is selected
6662 from the set (_over.over_). */
6663 else if (TYPE_PTRMEMFUNC_P (type))
6664 {
6665 expr = instantiate_type (type, expr, tf_none);
6666 if (expr == error_mark_node)
6667 return error_mark_node;
6668
6669 /* [temp.arg.nontype] bullet 1 says the pointer to member
6670 expression must be a pointer-to-member constant. */
6671 if (!value_dependent_expression_p (expr)
6672 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6673 return error_mark_node;
6674
6675 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6676 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6677 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6678 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6679
6680 /* There is no way to disable standard conversions in
6681 resolve_address_of_overloaded_function (called by
6682 instantiate_type). It is possible that the call succeeded by
6683 converting &B::I to &D::I (where B is a base of D), so we need
6684 to reject this conversion here.
6685
6686 Actually, even if there was a way to disable standard conversions,
6687 it would still be better to reject them here so that we can
6688 provide a superior diagnostic. */
6689 if (!same_type_p (TREE_TYPE (expr), type))
6690 {
6691 if (complain & tf_error)
6692 {
6693 error ("%qE is not a valid template argument for type %qT "
6694 "because it is of type %qT", expr, type,
6695 TREE_TYPE (expr));
6696 /* If we are just one standard conversion off, explain. */
6697 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6698 inform (input_location,
6699 "standard conversions are not allowed in this context");
6700 }
6701 return NULL_TREE;
6702 }
6703 }
6704 /* [temp.arg.nontype]/5, bullet 7
6705
6706 For a non-type template-parameter of type pointer to data member,
6707 qualification conversions (_conv.qual_) are applied. */
6708 else if (TYPE_PTRDATAMEM_P (type))
6709 {
6710 /* [temp.arg.nontype] bullet 1 says the pointer to member
6711 expression must be a pointer-to-member constant. */
6712 if (!value_dependent_expression_p (expr)
6713 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6714 return error_mark_node;
6715
6716 expr = perform_qualification_conversions (type, expr);
6717 if (expr == error_mark_node)
6718 return expr;
6719 }
6720 else if (NULLPTR_TYPE_P (type))
6721 {
6722 if (expr != nullptr_node)
6723 {
6724 if (complain & tf_error)
6725 error ("%qE is not a valid template argument for type %qT "
6726 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6727 return NULL_TREE;
6728 }
6729 return expr;
6730 }
6731 /* A template non-type parameter must be one of the above. */
6732 else
6733 gcc_unreachable ();
6734
6735 /* Sanity check: did we actually convert the argument to the
6736 right type? */
6737 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6738 (type, TREE_TYPE (expr)));
6739 return convert_from_reference (expr);
6740 }
6741
6742 /* Subroutine of coerce_template_template_parms, which returns 1 if
6743 PARM_PARM and ARG_PARM match using the rule for the template
6744 parameters of template template parameters. Both PARM and ARG are
6745 template parameters; the rest of the arguments are the same as for
6746 coerce_template_template_parms.
6747 */
6748 static int
6749 coerce_template_template_parm (tree parm,
6750 tree arg,
6751 tsubst_flags_t complain,
6752 tree in_decl,
6753 tree outer_args)
6754 {
6755 if (arg == NULL_TREE || error_operand_p (arg)
6756 || parm == NULL_TREE || error_operand_p (parm))
6757 return 0;
6758
6759 if (TREE_CODE (arg) != TREE_CODE (parm))
6760 return 0;
6761
6762 switch (TREE_CODE (parm))
6763 {
6764 case TEMPLATE_DECL:
6765 /* We encounter instantiations of templates like
6766 template <template <template <class> class> class TT>
6767 class C; */
6768 {
6769 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6770 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6771
6772 if (!coerce_template_template_parms
6773 (parmparm, argparm, complain, in_decl, outer_args))
6774 return 0;
6775 }
6776 /* Fall through. */
6777
6778 case TYPE_DECL:
6779 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6780 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6781 /* Argument is a parameter pack but parameter is not. */
6782 return 0;
6783 break;
6784
6785 case PARM_DECL:
6786 /* The tsubst call is used to handle cases such as
6787
6788 template <int> class C {};
6789 template <class T, template <T> class TT> class D {};
6790 D<int, C> d;
6791
6792 i.e. the parameter list of TT depends on earlier parameters. */
6793 if (!uses_template_parms (TREE_TYPE (arg)))
6794 {
6795 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6796 if (!uses_template_parms (t)
6797 && !same_type_p (t, TREE_TYPE (arg)))
6798 return 0;
6799 }
6800
6801 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6802 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6803 /* Argument is a parameter pack but parameter is not. */
6804 return 0;
6805
6806 break;
6807
6808 default:
6809 gcc_unreachable ();
6810 }
6811
6812 return 1;
6813 }
6814
6815
6816 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6817 template template parameters. Both PARM_PARMS and ARG_PARMS are
6818 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6819 or PARM_DECL.
6820
6821 Consider the example:
6822 template <class T> class A;
6823 template<template <class U> class TT> class B;
6824
6825 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6826 the parameters to A, and OUTER_ARGS contains A. */
6827
6828 static int
6829 coerce_template_template_parms (tree parm_parms,
6830 tree arg_parms,
6831 tsubst_flags_t complain,
6832 tree in_decl,
6833 tree outer_args)
6834 {
6835 int nparms, nargs, i;
6836 tree parm, arg;
6837 int variadic_p = 0;
6838
6839 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6840 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6841
6842 nparms = TREE_VEC_LENGTH (parm_parms);
6843 nargs = TREE_VEC_LENGTH (arg_parms);
6844
6845 /* Determine whether we have a parameter pack at the end of the
6846 template template parameter's template parameter list. */
6847 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6848 {
6849 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6850
6851 if (error_operand_p (parm))
6852 return 0;
6853
6854 switch (TREE_CODE (parm))
6855 {
6856 case TEMPLATE_DECL:
6857 case TYPE_DECL:
6858 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6859 variadic_p = 1;
6860 break;
6861
6862 case PARM_DECL:
6863 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6864 variadic_p = 1;
6865 break;
6866
6867 default:
6868 gcc_unreachable ();
6869 }
6870 }
6871
6872 if (nargs != nparms
6873 && !(variadic_p && nargs >= nparms - 1))
6874 return 0;
6875
6876 /* Check all of the template parameters except the parameter pack at
6877 the end (if any). */
6878 for (i = 0; i < nparms - variadic_p; ++i)
6879 {
6880 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6881 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6882 continue;
6883
6884 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6885 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6886
6887 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6888 outer_args))
6889 return 0;
6890
6891 }
6892
6893 if (variadic_p)
6894 {
6895 /* Check each of the template parameters in the template
6896 argument against the template parameter pack at the end of
6897 the template template parameter. */
6898 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6899 return 0;
6900
6901 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6902
6903 for (; i < nargs; ++i)
6904 {
6905 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6906 continue;
6907
6908 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6909
6910 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6911 outer_args))
6912 return 0;
6913 }
6914 }
6915
6916 return 1;
6917 }
6918
6919 /* Verifies that the deduced template arguments (in TARGS) for the
6920 template template parameters (in TPARMS) represent valid bindings,
6921 by comparing the template parameter list of each template argument
6922 to the template parameter list of its corresponding template
6923 template parameter, in accordance with DR150. This
6924 routine can only be called after all template arguments have been
6925 deduced. It will return TRUE if all of the template template
6926 parameter bindings are okay, FALSE otherwise. */
6927 bool
6928 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6929 {
6930 int i, ntparms = TREE_VEC_LENGTH (tparms);
6931 bool ret = true;
6932
6933 /* We're dealing with template parms in this process. */
6934 ++processing_template_decl;
6935
6936 targs = INNERMOST_TEMPLATE_ARGS (targs);
6937
6938 for (i = 0; i < ntparms; ++i)
6939 {
6940 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6941 tree targ = TREE_VEC_ELT (targs, i);
6942
6943 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6944 {
6945 tree packed_args = NULL_TREE;
6946 int idx, len = 1;
6947
6948 if (ARGUMENT_PACK_P (targ))
6949 {
6950 /* Look inside the argument pack. */
6951 packed_args = ARGUMENT_PACK_ARGS (targ);
6952 len = TREE_VEC_LENGTH (packed_args);
6953 }
6954
6955 for (idx = 0; idx < len; ++idx)
6956 {
6957 tree targ_parms = NULL_TREE;
6958
6959 if (packed_args)
6960 /* Extract the next argument from the argument
6961 pack. */
6962 targ = TREE_VEC_ELT (packed_args, idx);
6963
6964 if (PACK_EXPANSION_P (targ))
6965 /* Look at the pattern of the pack expansion. */
6966 targ = PACK_EXPANSION_PATTERN (targ);
6967
6968 /* Extract the template parameters from the template
6969 argument. */
6970 if (TREE_CODE (targ) == TEMPLATE_DECL)
6971 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6972 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6973 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6974
6975 /* Verify that we can coerce the template template
6976 parameters from the template argument to the template
6977 parameter. This requires an exact match. */
6978 if (targ_parms
6979 && !coerce_template_template_parms
6980 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6981 targ_parms,
6982 tf_none,
6983 tparm,
6984 targs))
6985 {
6986 ret = false;
6987 goto out;
6988 }
6989 }
6990 }
6991 }
6992
6993 out:
6994
6995 --processing_template_decl;
6996 return ret;
6997 }
6998
6999 /* Since type attributes aren't mangled, we need to strip them from
7000 template type arguments. */
7001
7002 static tree
7003 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7004 {
7005 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7006 return arg;
7007 bool removed_attributes = false;
7008 tree canon = strip_typedefs (arg, &removed_attributes);
7009 if (removed_attributes
7010 && (complain & tf_warning))
7011 warning (OPT_Wignored_attributes,
7012 "ignoring attributes on template argument %qT", arg);
7013 return canon;
7014 }
7015
7016 /* And from inside dependent non-type arguments like sizeof(Type). */
7017
7018 static tree
7019 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7020 {
7021 if (!arg || arg == error_mark_node)
7022 return arg;
7023 bool removed_attributes = false;
7024 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7025 if (removed_attributes
7026 && (complain & tf_warning))
7027 warning (OPT_Wignored_attributes,
7028 "ignoring attributes in template argument %qE", arg);
7029 return canon;
7030 }
7031
7032 // A template declaration can be substituted for a constrained
7033 // template template parameter only when the argument is more
7034 // constrained than the parameter.
7035 static bool
7036 is_compatible_template_arg (tree parm, tree arg)
7037 {
7038 tree parm_cons = get_constraints (parm);
7039
7040 /* For now, allow constrained template template arguments
7041 and unconstrained template template parameters. */
7042 if (parm_cons == NULL_TREE)
7043 return true;
7044
7045 tree arg_cons = get_constraints (arg);
7046
7047 // If the template parameter is constrained, we need to rewrite its
7048 // constraints in terms of the ARG's template parameters. This ensures
7049 // that all of the template parameter types will have the same depth.
7050 //
7051 // Note that this is only valid when coerce_template_template_parm is
7052 // true for the innermost template parameters of PARM and ARG. In other
7053 // words, because coercion is successful, this conversion will be valid.
7054 if (parm_cons)
7055 {
7056 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7057 parm_cons = tsubst_constraint_info (parm_cons,
7058 INNERMOST_TEMPLATE_ARGS (args),
7059 tf_none, NULL_TREE);
7060 if (parm_cons == error_mark_node)
7061 return false;
7062 }
7063
7064 return subsumes (parm_cons, arg_cons);
7065 }
7066
7067 // Convert a placeholder argument into a binding to the original
7068 // parameter. The original parameter is saved as the TREE_TYPE of
7069 // ARG.
7070 static inline tree
7071 convert_wildcard_argument (tree parm, tree arg)
7072 {
7073 TREE_TYPE (arg) = parm;
7074 return arg;
7075 }
7076
7077 /* Convert the indicated template ARG as necessary to match the
7078 indicated template PARM. Returns the converted ARG, or
7079 error_mark_node if the conversion was unsuccessful. Error and
7080 warning messages are issued under control of COMPLAIN. This
7081 conversion is for the Ith parameter in the parameter list. ARGS is
7082 the full set of template arguments deduced so far. */
7083
7084 static tree
7085 convert_template_argument (tree parm,
7086 tree arg,
7087 tree args,
7088 tsubst_flags_t complain,
7089 int i,
7090 tree in_decl)
7091 {
7092 tree orig_arg;
7093 tree val;
7094 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7095
7096 if (parm == error_mark_node)
7097 return error_mark_node;
7098
7099 /* Trivially convert placeholders. */
7100 if (TREE_CODE (arg) == WILDCARD_DECL)
7101 return convert_wildcard_argument (parm, arg);
7102
7103 if (TREE_CODE (arg) == TREE_LIST
7104 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7105 {
7106 /* The template argument was the name of some
7107 member function. That's usually
7108 invalid, but static members are OK. In any
7109 case, grab the underlying fields/functions
7110 and issue an error later if required. */
7111 orig_arg = TREE_VALUE (arg);
7112 TREE_TYPE (arg) = unknown_type_node;
7113 }
7114
7115 orig_arg = arg;
7116
7117 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7118 requires_type = (TREE_CODE (parm) == TYPE_DECL
7119 || requires_tmpl_type);
7120
7121 /* When determining whether an argument pack expansion is a template,
7122 look at the pattern. */
7123 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7124 arg = PACK_EXPANSION_PATTERN (arg);
7125
7126 /* Deal with an injected-class-name used as a template template arg. */
7127 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7128 {
7129 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7130 if (TREE_CODE (t) == TEMPLATE_DECL)
7131 {
7132 if (cxx_dialect >= cxx11)
7133 /* OK under DR 1004. */;
7134 else if (complain & tf_warning_or_error)
7135 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7136 " used as template template argument", TYPE_NAME (arg));
7137 else if (flag_pedantic_errors)
7138 t = arg;
7139
7140 arg = t;
7141 }
7142 }
7143
7144 is_tmpl_type =
7145 ((TREE_CODE (arg) == TEMPLATE_DECL
7146 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7147 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7148 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7149 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7150
7151 if (is_tmpl_type
7152 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7153 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7154 arg = TYPE_STUB_DECL (arg);
7155
7156 is_type = TYPE_P (arg) || is_tmpl_type;
7157
7158 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7159 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7160 {
7161 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7162 {
7163 if (complain & tf_error)
7164 error ("invalid use of destructor %qE as a type", orig_arg);
7165 return error_mark_node;
7166 }
7167
7168 permerror (input_location,
7169 "to refer to a type member of a template parameter, "
7170 "use %<typename %E%>", orig_arg);
7171
7172 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7173 TREE_OPERAND (arg, 1),
7174 typename_type,
7175 complain);
7176 arg = orig_arg;
7177 is_type = 1;
7178 }
7179 if (is_type != requires_type)
7180 {
7181 if (in_decl)
7182 {
7183 if (complain & tf_error)
7184 {
7185 error ("type/value mismatch at argument %d in template "
7186 "parameter list for %qD",
7187 i + 1, in_decl);
7188 if (is_type)
7189 inform (input_location,
7190 " expected a constant of type %qT, got %qT",
7191 TREE_TYPE (parm),
7192 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7193 else if (requires_tmpl_type)
7194 inform (input_location,
7195 " expected a class template, got %qE", orig_arg);
7196 else
7197 inform (input_location,
7198 " expected a type, got %qE", orig_arg);
7199 }
7200 }
7201 return error_mark_node;
7202 }
7203 if (is_tmpl_type ^ requires_tmpl_type)
7204 {
7205 if (in_decl && (complain & tf_error))
7206 {
7207 error ("type/value mismatch at argument %d in template "
7208 "parameter list for %qD",
7209 i + 1, in_decl);
7210 if (is_tmpl_type)
7211 inform (input_location,
7212 " expected a type, got %qT", DECL_NAME (arg));
7213 else
7214 inform (input_location,
7215 " expected a class template, got %qT", orig_arg);
7216 }
7217 return error_mark_node;
7218 }
7219
7220 if (is_type)
7221 {
7222 if (requires_tmpl_type)
7223 {
7224 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7225 val = orig_arg;
7226 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7227 /* The number of argument required is not known yet.
7228 Just accept it for now. */
7229 val = TREE_TYPE (arg);
7230 else
7231 {
7232 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7233 tree argparm;
7234
7235 /* Strip alias templates that are equivalent to another
7236 template. */
7237 arg = get_underlying_template (arg);
7238 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7239
7240 if (coerce_template_template_parms (parmparm, argparm,
7241 complain, in_decl,
7242 args))
7243 {
7244 val = arg;
7245
7246 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7247 TEMPLATE_DECL. */
7248 if (val != error_mark_node)
7249 {
7250 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7251 val = TREE_TYPE (val);
7252 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7253 val = make_pack_expansion (val);
7254 }
7255 }
7256 else
7257 {
7258 if (in_decl && (complain & tf_error))
7259 {
7260 error ("type/value mismatch at argument %d in "
7261 "template parameter list for %qD",
7262 i + 1, in_decl);
7263 inform (input_location,
7264 " expected a template of type %qD, got %qT",
7265 parm, orig_arg);
7266 }
7267
7268 val = error_mark_node;
7269 }
7270
7271 // Check that the constraints are compatible before allowing the
7272 // substitution.
7273 if (val != error_mark_node)
7274 if (!is_compatible_template_arg (parm, arg))
7275 {
7276 if (in_decl && (complain & tf_error))
7277 {
7278 error ("constraint mismatch at argument %d in "
7279 "template parameter list for %qD",
7280 i + 1, in_decl);
7281 inform (input_location, " expected %qD but got %qD",
7282 parm, arg);
7283 }
7284 val = error_mark_node;
7285 }
7286 }
7287 }
7288 else
7289 val = orig_arg;
7290 /* We only form one instance of each template specialization.
7291 Therefore, if we use a non-canonical variant (i.e., a
7292 typedef), any future messages referring to the type will use
7293 the typedef, which is confusing if those future uses do not
7294 themselves also use the typedef. */
7295 if (TYPE_P (val))
7296 val = canonicalize_type_argument (val, complain);
7297 }
7298 else
7299 {
7300 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
7301
7302 if (invalid_nontype_parm_type_p (t, complain))
7303 return error_mark_node;
7304
7305 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7306 {
7307 if (same_type_p (t, TREE_TYPE (orig_arg)))
7308 val = orig_arg;
7309 else
7310 {
7311 /* Not sure if this is reachable, but it doesn't hurt
7312 to be robust. */
7313 error ("type mismatch in nontype parameter pack");
7314 val = error_mark_node;
7315 }
7316 }
7317 else if (!type_dependent_expression_p (orig_arg)
7318 && !uses_template_parms (t))
7319 /* We used to call digest_init here. However, digest_init
7320 will report errors, which we don't want when complain
7321 is zero. More importantly, digest_init will try too
7322 hard to convert things: for example, `0' should not be
7323 converted to pointer type at this point according to
7324 the standard. Accepting this is not merely an
7325 extension, since deciding whether or not these
7326 conversions can occur is part of determining which
7327 function template to call, or whether a given explicit
7328 argument specification is valid. */
7329 val = convert_nontype_argument (t, orig_arg, complain);
7330 else
7331 val = canonicalize_expr_argument (orig_arg, complain);
7332
7333 if (val == NULL_TREE)
7334 val = error_mark_node;
7335 else if (val == error_mark_node && (complain & tf_error))
7336 error ("could not convert template argument %qE from %qT to %qT",
7337 orig_arg, TREE_TYPE (orig_arg), t);
7338
7339 if (INDIRECT_REF_P (val))
7340 {
7341 /* Reject template arguments that are references to built-in
7342 functions with no library fallbacks. */
7343 const_tree inner = TREE_OPERAND (val, 0);
7344 if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7345 && TREE_CODE (TREE_TYPE (TREE_TYPE (inner))) == FUNCTION_TYPE
7346 && TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE
7347 && 0 < TREE_OPERAND_LENGTH (inner)
7348 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7349 return error_mark_node;
7350 }
7351
7352 if (TREE_CODE (val) == SCOPE_REF)
7353 {
7354 /* Strip typedefs from the SCOPE_REF. */
7355 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7356 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7357 complain);
7358 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7359 QUALIFIED_NAME_IS_TEMPLATE (val));
7360 }
7361 }
7362
7363 return val;
7364 }
7365
7366 /* Coerces the remaining template arguments in INNER_ARGS (from
7367 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7368 Returns the coerced argument pack. PARM_IDX is the position of this
7369 parameter in the template parameter list. ARGS is the original
7370 template argument list. */
7371 static tree
7372 coerce_template_parameter_pack (tree parms,
7373 int parm_idx,
7374 tree args,
7375 tree inner_args,
7376 int arg_idx,
7377 tree new_args,
7378 int* lost,
7379 tree in_decl,
7380 tsubst_flags_t complain)
7381 {
7382 tree parm = TREE_VEC_ELT (parms, parm_idx);
7383 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7384 tree packed_args;
7385 tree argument_pack;
7386 tree packed_parms = NULL_TREE;
7387
7388 if (arg_idx > nargs)
7389 arg_idx = nargs;
7390
7391 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7392 {
7393 /* When the template parameter is a non-type template parameter pack
7394 or template template parameter pack whose type or template
7395 parameters use parameter packs, we know exactly how many arguments
7396 we are looking for. Build a vector of the instantiated decls for
7397 these template parameters in PACKED_PARMS. */
7398 /* We can't use make_pack_expansion here because it would interpret a
7399 _DECL as a use rather than a declaration. */
7400 tree decl = TREE_VALUE (parm);
7401 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7402 SET_PACK_EXPANSION_PATTERN (exp, decl);
7403 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7404 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7405
7406 TREE_VEC_LENGTH (args)--;
7407 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7408 TREE_VEC_LENGTH (args)++;
7409
7410 if (packed_parms == error_mark_node)
7411 return error_mark_node;
7412
7413 /* If we're doing a partial instantiation of a member template,
7414 verify that all of the types used for the non-type
7415 template parameter pack are, in fact, valid for non-type
7416 template parameters. */
7417 if (arg_idx < nargs
7418 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7419 {
7420 int j, len = TREE_VEC_LENGTH (packed_parms);
7421 for (j = 0; j < len; ++j)
7422 {
7423 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7424 if (invalid_nontype_parm_type_p (t, complain))
7425 return error_mark_node;
7426 }
7427 /* We don't know how many args we have yet, just
7428 use the unconverted ones for now. */
7429 return NULL_TREE;
7430 }
7431
7432 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7433 }
7434 /* Check if we have a placeholder pack, which indicates we're
7435 in the context of a introduction list. In that case we want
7436 to match this pack to the single placeholder. */
7437 else if (arg_idx < nargs
7438 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7439 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7440 {
7441 nargs = arg_idx + 1;
7442 packed_args = make_tree_vec (1);
7443 }
7444 else
7445 packed_args = make_tree_vec (nargs - arg_idx);
7446
7447 /* Convert the remaining arguments, which will be a part of the
7448 parameter pack "parm". */
7449 int first_pack_arg = arg_idx;
7450 for (; arg_idx < nargs; ++arg_idx)
7451 {
7452 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7453 tree actual_parm = TREE_VALUE (parm);
7454 int pack_idx = arg_idx - first_pack_arg;
7455
7456 if (packed_parms)
7457 {
7458 /* Once we've packed as many args as we have types, stop. */
7459 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7460 break;
7461 else if (PACK_EXPANSION_P (arg))
7462 /* We don't know how many args we have yet, just
7463 use the unconverted ones for now. */
7464 return NULL_TREE;
7465 else
7466 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7467 }
7468
7469 if (arg == error_mark_node)
7470 {
7471 if (complain & tf_error)
7472 error ("template argument %d is invalid", arg_idx + 1);
7473 }
7474 else
7475 arg = convert_template_argument (actual_parm,
7476 arg, new_args, complain, parm_idx,
7477 in_decl);
7478 if (arg == error_mark_node)
7479 (*lost)++;
7480 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7481 }
7482
7483 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7484 && TREE_VEC_LENGTH (packed_args) > 0)
7485 {
7486 if (complain & tf_error)
7487 error ("wrong number of template arguments (%d, should be %d)",
7488 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7489 return error_mark_node;
7490 }
7491
7492 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7493 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7494 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7495 else
7496 {
7497 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7498 TREE_TYPE (argument_pack)
7499 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7500 TREE_CONSTANT (argument_pack) = 1;
7501 }
7502
7503 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7504 if (CHECKING_P)
7505 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7506 TREE_VEC_LENGTH (packed_args));
7507 return argument_pack;
7508 }
7509
7510 /* Returns the number of pack expansions in the template argument vector
7511 ARGS. */
7512
7513 static int
7514 pack_expansion_args_count (tree args)
7515 {
7516 int i;
7517 int count = 0;
7518 if (args)
7519 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7520 {
7521 tree elt = TREE_VEC_ELT (args, i);
7522 if (elt && PACK_EXPANSION_P (elt))
7523 ++count;
7524 }
7525 return count;
7526 }
7527
7528 /* Convert all template arguments to their appropriate types, and
7529 return a vector containing the innermost resulting template
7530 arguments. If any error occurs, return error_mark_node. Error and
7531 warning messages are issued under control of COMPLAIN.
7532
7533 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7534 for arguments not specified in ARGS. Otherwise, if
7535 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7536 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7537 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7538 ARGS. */
7539
7540 static tree
7541 coerce_template_parms (tree parms,
7542 tree args,
7543 tree in_decl,
7544 tsubst_flags_t complain,
7545 bool require_all_args,
7546 bool use_default_args)
7547 {
7548 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7549 tree orig_inner_args;
7550 tree inner_args;
7551 tree new_args;
7552 tree new_inner_args;
7553 int saved_unevaluated_operand;
7554 int saved_inhibit_evaluation_warnings;
7555
7556 /* When used as a boolean value, indicates whether this is a
7557 variadic template parameter list. Since it's an int, we can also
7558 subtract it from nparms to get the number of non-variadic
7559 parameters. */
7560 int variadic_p = 0;
7561 int variadic_args_p = 0;
7562 int post_variadic_parms = 0;
7563
7564 /* Likewise for parameters with default arguments. */
7565 int default_p = 0;
7566
7567 if (args == error_mark_node)
7568 return error_mark_node;
7569
7570 nparms = TREE_VEC_LENGTH (parms);
7571
7572 /* Determine if there are any parameter packs or default arguments. */
7573 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7574 {
7575 tree parm = TREE_VEC_ELT (parms, parm_idx);
7576 if (variadic_p)
7577 ++post_variadic_parms;
7578 if (template_parameter_pack_p (TREE_VALUE (parm)))
7579 ++variadic_p;
7580 if (TREE_PURPOSE (parm))
7581 ++default_p;
7582 }
7583
7584 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7585 /* If there are no parameters that follow a parameter pack, we need to
7586 expand any argument packs so that we can deduce a parameter pack from
7587 some non-packed args followed by an argument pack, as in variadic85.C.
7588 If there are such parameters, we need to leave argument packs intact
7589 so the arguments are assigned properly. This can happen when dealing
7590 with a nested class inside a partial specialization of a class
7591 template, as in variadic92.C, or when deducing a template parameter pack
7592 from a sub-declarator, as in variadic114.C. */
7593 if (!post_variadic_parms)
7594 inner_args = expand_template_argument_pack (inner_args);
7595
7596 /* Count any pack expansion args. */
7597 variadic_args_p = pack_expansion_args_count (inner_args);
7598
7599 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7600 if ((nargs > nparms && !variadic_p)
7601 || (nargs < nparms - variadic_p
7602 && require_all_args
7603 && !variadic_args_p
7604 && (!use_default_args
7605 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7606 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7607 {
7608 if (complain & tf_error)
7609 {
7610 if (variadic_p || default_p)
7611 {
7612 nparms -= variadic_p + default_p;
7613 error ("wrong number of template arguments "
7614 "(%d, should be at least %d)", nargs, nparms);
7615 }
7616 else
7617 error ("wrong number of template arguments "
7618 "(%d, should be %d)", nargs, nparms);
7619
7620 if (in_decl)
7621 inform (DECL_SOURCE_LOCATION (in_decl),
7622 "provided for %qD", in_decl);
7623 }
7624
7625 return error_mark_node;
7626 }
7627 /* We can't pass a pack expansion to a non-pack parameter of an alias
7628 template (DR 1430). */
7629 else if (in_decl
7630 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7631 || concept_template_p (in_decl))
7632 && variadic_args_p
7633 && nargs - variadic_args_p < nparms - variadic_p)
7634 {
7635 if (complain & tf_error)
7636 {
7637 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7638 {
7639 tree arg = TREE_VEC_ELT (inner_args, i);
7640 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7641
7642 if (PACK_EXPANSION_P (arg)
7643 && !template_parameter_pack_p (parm))
7644 {
7645 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7646 error_at (location_of (arg),
7647 "pack expansion argument for non-pack parameter "
7648 "%qD of alias template %qD", parm, in_decl);
7649 else
7650 error_at (location_of (arg),
7651 "pack expansion argument for non-pack parameter "
7652 "%qD of concept %qD", parm, in_decl);
7653 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7654 goto found;
7655 }
7656 }
7657 gcc_unreachable ();
7658 found:;
7659 }
7660 return error_mark_node;
7661 }
7662
7663 /* We need to evaluate the template arguments, even though this
7664 template-id may be nested within a "sizeof". */
7665 saved_unevaluated_operand = cp_unevaluated_operand;
7666 cp_unevaluated_operand = 0;
7667 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7668 c_inhibit_evaluation_warnings = 0;
7669 new_inner_args = make_tree_vec (nparms);
7670 new_args = add_outermost_template_args (args, new_inner_args);
7671 int pack_adjust = 0;
7672 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7673 {
7674 tree arg;
7675 tree parm;
7676
7677 /* Get the Ith template parameter. */
7678 parm = TREE_VEC_ELT (parms, parm_idx);
7679
7680 if (parm == error_mark_node)
7681 {
7682 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7683 continue;
7684 }
7685
7686 /* Calculate the next argument. */
7687 if (arg_idx < nargs)
7688 arg = TREE_VEC_ELT (inner_args, arg_idx);
7689 else
7690 arg = NULL_TREE;
7691
7692 if (template_parameter_pack_p (TREE_VALUE (parm))
7693 && !(arg && ARGUMENT_PACK_P (arg)))
7694 {
7695 /* Some arguments will be placed in the
7696 template parameter pack PARM. */
7697 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7698 inner_args, arg_idx,
7699 new_args, &lost,
7700 in_decl, complain);
7701
7702 if (arg == NULL_TREE)
7703 {
7704 /* We don't know how many args we have yet, just use the
7705 unconverted (and still packed) ones for now. */
7706 new_inner_args = orig_inner_args;
7707 arg_idx = nargs;
7708 break;
7709 }
7710
7711 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7712
7713 /* Store this argument. */
7714 if (arg == error_mark_node)
7715 {
7716 lost++;
7717 /* We are done with all of the arguments. */
7718 arg_idx = nargs;
7719 }
7720 else
7721 {
7722 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7723 arg_idx += pack_adjust;
7724 }
7725
7726 continue;
7727 }
7728 else if (arg)
7729 {
7730 if (PACK_EXPANSION_P (arg))
7731 {
7732 /* "If every valid specialization of a variadic template
7733 requires an empty template parameter pack, the template is
7734 ill-formed, no diagnostic required." So check that the
7735 pattern works with this parameter. */
7736 tree pattern = PACK_EXPANSION_PATTERN (arg);
7737 tree conv = convert_template_argument (TREE_VALUE (parm),
7738 pattern, new_args,
7739 complain, parm_idx,
7740 in_decl);
7741 if (conv == error_mark_node)
7742 {
7743 inform (input_location, "so any instantiation with a "
7744 "non-empty parameter pack would be ill-formed");
7745 ++lost;
7746 }
7747 else if (TYPE_P (conv) && !TYPE_P (pattern))
7748 /* Recover from missing typename. */
7749 TREE_VEC_ELT (inner_args, arg_idx)
7750 = make_pack_expansion (conv);
7751
7752 /* We don't know how many args we have yet, just
7753 use the unconverted ones for now. */
7754 new_inner_args = inner_args;
7755 arg_idx = nargs;
7756 break;
7757 }
7758 }
7759 else if (require_all_args)
7760 {
7761 /* There must be a default arg in this case. */
7762 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7763 complain, in_decl);
7764 /* The position of the first default template argument,
7765 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7766 Record that. */
7767 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7768 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7769 arg_idx - pack_adjust);
7770 }
7771 else
7772 break;
7773
7774 if (arg == error_mark_node)
7775 {
7776 if (complain & tf_error)
7777 error ("template argument %d is invalid", arg_idx + 1);
7778 }
7779 else if (!arg)
7780 /* This only occurs if there was an error in the template
7781 parameter list itself (which we would already have
7782 reported) that we are trying to recover from, e.g., a class
7783 template with a parameter list such as
7784 template<typename..., typename>. */
7785 ++lost;
7786 else
7787 arg = convert_template_argument (TREE_VALUE (parm),
7788 arg, new_args, complain,
7789 parm_idx, in_decl);
7790
7791 if (arg == error_mark_node)
7792 lost++;
7793 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7794 }
7795 cp_unevaluated_operand = saved_unevaluated_operand;
7796 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7797
7798 if (variadic_p && arg_idx < nargs)
7799 {
7800 if (complain & tf_error)
7801 {
7802 error ("wrong number of template arguments "
7803 "(%d, should be %d)", nargs, arg_idx);
7804 if (in_decl)
7805 error ("provided for %q+D", in_decl);
7806 }
7807 return error_mark_node;
7808 }
7809
7810 if (lost)
7811 return error_mark_node;
7812
7813 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7815 TREE_VEC_LENGTH (new_inner_args));
7816
7817 return new_inner_args;
7818 }
7819
7820 /* Convert all template arguments to their appropriate types, and
7821 return a vector containing the innermost resulting template
7822 arguments. If any error occurs, return error_mark_node. Error and
7823 warning messages are not issued.
7824
7825 Note that no function argument deduction is performed, and default
7826 arguments are used to fill in unspecified arguments. */
7827 tree
7828 coerce_template_parms (tree parms, tree args, tree in_decl)
7829 {
7830 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
7831 }
7832
7833 /* Convert all template arguments to their appropriate type, and
7834 instantiate default arguments as needed. This returns a vector
7835 containing the innermost resulting template arguments, or
7836 error_mark_node if unsuccessful. */
7837 tree
7838 coerce_template_parms (tree parms, tree args, tree in_decl,
7839 tsubst_flags_t complain)
7840 {
7841 return coerce_template_parms (parms, args, in_decl, complain, true, true);
7842 }
7843
7844 /* Like coerce_template_parms. If PARMS represents all template
7845 parameters levels, this function returns a vector of vectors
7846 representing all the resulting argument levels. Note that in this
7847 case, only the innermost arguments are coerced because the
7848 outermost ones are supposed to have been coerced already.
7849
7850 Otherwise, if PARMS represents only (the innermost) vector of
7851 parameters, this function returns a vector containing just the
7852 innermost resulting arguments. */
7853
7854 static tree
7855 coerce_innermost_template_parms (tree parms,
7856 tree args,
7857 tree in_decl,
7858 tsubst_flags_t complain,
7859 bool require_all_args,
7860 bool use_default_args)
7861 {
7862 int parms_depth = TMPL_PARMS_DEPTH (parms);
7863 int args_depth = TMPL_ARGS_DEPTH (args);
7864 tree coerced_args;
7865
7866 if (parms_depth > 1)
7867 {
7868 coerced_args = make_tree_vec (parms_depth);
7869 tree level;
7870 int cur_depth;
7871
7872 for (level = parms, cur_depth = parms_depth;
7873 parms_depth > 0 && level != NULL_TREE;
7874 level = TREE_CHAIN (level), --cur_depth)
7875 {
7876 tree l;
7877 if (cur_depth == args_depth)
7878 l = coerce_template_parms (TREE_VALUE (level),
7879 args, in_decl, complain,
7880 require_all_args,
7881 use_default_args);
7882 else
7883 l = TMPL_ARGS_LEVEL (args, cur_depth);
7884
7885 if (l == error_mark_node)
7886 return error_mark_node;
7887
7888 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7889 }
7890 }
7891 else
7892 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7893 args, in_decl, complain,
7894 require_all_args,
7895 use_default_args);
7896 return coerced_args;
7897 }
7898
7899 /* Returns 1 if template args OT and NT are equivalent. */
7900
7901 int
7902 template_args_equal (tree ot, tree nt)
7903 {
7904 if (nt == ot)
7905 return 1;
7906 if (nt == NULL_TREE || ot == NULL_TREE)
7907 return false;
7908
7909 if (TREE_CODE (nt) == TREE_VEC)
7910 /* For member templates */
7911 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7912 else if (PACK_EXPANSION_P (ot))
7913 return (PACK_EXPANSION_P (nt)
7914 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7915 PACK_EXPANSION_PATTERN (nt))
7916 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7917 PACK_EXPANSION_EXTRA_ARGS (nt)));
7918 else if (ARGUMENT_PACK_P (ot))
7919 {
7920 int i, len;
7921 tree opack, npack;
7922
7923 if (!ARGUMENT_PACK_P (nt))
7924 return 0;
7925
7926 opack = ARGUMENT_PACK_ARGS (ot);
7927 npack = ARGUMENT_PACK_ARGS (nt);
7928 len = TREE_VEC_LENGTH (opack);
7929 if (TREE_VEC_LENGTH (npack) != len)
7930 return 0;
7931 for (i = 0; i < len; ++i)
7932 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7933 TREE_VEC_ELT (npack, i)))
7934 return 0;
7935 return 1;
7936 }
7937 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7938 gcc_unreachable ();
7939 else if (TYPE_P (nt))
7940 {
7941 if (!TYPE_P (ot))
7942 return false;
7943 /* Don't treat an alias template specialization with dependent
7944 arguments as equivalent to its underlying type when used as a
7945 template argument; we need them to be distinct so that we
7946 substitute into the specialization arguments at instantiation
7947 time. And aliases can't be equivalent without being ==, so
7948 we don't need to look any deeper. */
7949 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7950 return false;
7951 else
7952 return same_type_p (ot, nt);
7953 }
7954 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7955 return 0;
7956 else
7957 {
7958 /* Try to treat a template non-type argument that has been converted
7959 to the parameter type as equivalent to one that hasn't yet. */
7960 for (enum tree_code code1 = TREE_CODE (ot);
7961 CONVERT_EXPR_CODE_P (code1)
7962 || code1 == NON_LVALUE_EXPR;
7963 code1 = TREE_CODE (ot))
7964 ot = TREE_OPERAND (ot, 0);
7965 for (enum tree_code code2 = TREE_CODE (nt);
7966 CONVERT_EXPR_CODE_P (code2)
7967 || code2 == NON_LVALUE_EXPR;
7968 code2 = TREE_CODE (nt))
7969 nt = TREE_OPERAND (nt, 0);
7970
7971 return cp_tree_equal (ot, nt);
7972 }
7973 }
7974
7975 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7976 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7977 NEWARG_PTR with the offending arguments if they are non-NULL. */
7978
7979 int
7980 comp_template_args (tree oldargs, tree newargs,
7981 tree *oldarg_ptr, tree *newarg_ptr)
7982 {
7983 int i;
7984
7985 if (oldargs == newargs)
7986 return 1;
7987
7988 if (!oldargs || !newargs)
7989 return 0;
7990
7991 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7992 return 0;
7993
7994 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7995 {
7996 tree nt = TREE_VEC_ELT (newargs, i);
7997 tree ot = TREE_VEC_ELT (oldargs, i);
7998
7999 if (! template_args_equal (ot, nt))
8000 {
8001 if (oldarg_ptr != NULL)
8002 *oldarg_ptr = ot;
8003 if (newarg_ptr != NULL)
8004 *newarg_ptr = nt;
8005 return 0;
8006 }
8007 }
8008 return 1;
8009 }
8010
8011 static void
8012 add_pending_template (tree d)
8013 {
8014 tree ti = (TYPE_P (d)
8015 ? CLASSTYPE_TEMPLATE_INFO (d)
8016 : DECL_TEMPLATE_INFO (d));
8017 struct pending_template *pt;
8018 int level;
8019
8020 if (TI_PENDING_TEMPLATE_FLAG (ti))
8021 return;
8022
8023 /* We are called both from instantiate_decl, where we've already had a
8024 tinst_level pushed, and instantiate_template, where we haven't.
8025 Compensate. */
8026 level = !current_tinst_level || current_tinst_level->decl != d;
8027
8028 if (level)
8029 push_tinst_level (d);
8030
8031 pt = ggc_alloc<pending_template> ();
8032 pt->next = NULL;
8033 pt->tinst = current_tinst_level;
8034 if (last_pending_template)
8035 last_pending_template->next = pt;
8036 else
8037 pending_templates = pt;
8038
8039 last_pending_template = pt;
8040
8041 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8042
8043 if (level)
8044 pop_tinst_level ();
8045 }
8046
8047
8048 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8049 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8050 documentation for TEMPLATE_ID_EXPR. */
8051
8052 tree
8053 lookup_template_function (tree fns, tree arglist)
8054 {
8055 tree type;
8056
8057 if (fns == error_mark_node || arglist == error_mark_node)
8058 return error_mark_node;
8059
8060 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8061
8062 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8063 {
8064 error ("%q#D is not a function template", fns);
8065 return error_mark_node;
8066 }
8067
8068 if (BASELINK_P (fns))
8069 {
8070 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8071 unknown_type_node,
8072 BASELINK_FUNCTIONS (fns),
8073 arglist);
8074 return fns;
8075 }
8076
8077 type = TREE_TYPE (fns);
8078 if (TREE_CODE (fns) == OVERLOAD || !type)
8079 type = unknown_type_node;
8080
8081 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8082 }
8083
8084 /* Within the scope of a template class S<T>, the name S gets bound
8085 (in build_self_reference) to a TYPE_DECL for the class, not a
8086 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8087 or one of its enclosing classes, and that type is a template,
8088 return the associated TEMPLATE_DECL. Otherwise, the original
8089 DECL is returned.
8090
8091 Also handle the case when DECL is a TREE_LIST of ambiguous
8092 injected-class-names from different bases. */
8093
8094 tree
8095 maybe_get_template_decl_from_type_decl (tree decl)
8096 {
8097 if (decl == NULL_TREE)
8098 return decl;
8099
8100 /* DR 176: A lookup that finds an injected-class-name (10.2
8101 [class.member.lookup]) can result in an ambiguity in certain cases
8102 (for example, if it is found in more than one base class). If all of
8103 the injected-class-names that are found refer to specializations of
8104 the same class template, and if the name is followed by a
8105 template-argument-list, the reference refers to the class template
8106 itself and not a specialization thereof, and is not ambiguous. */
8107 if (TREE_CODE (decl) == TREE_LIST)
8108 {
8109 tree t, tmpl = NULL_TREE;
8110 for (t = decl; t; t = TREE_CHAIN (t))
8111 {
8112 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8113 if (!tmpl)
8114 tmpl = elt;
8115 else if (tmpl != elt)
8116 break;
8117 }
8118 if (tmpl && t == NULL_TREE)
8119 return tmpl;
8120 else
8121 return decl;
8122 }
8123
8124 return (decl != NULL_TREE
8125 && DECL_SELF_REFERENCE_P (decl)
8126 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8127 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8128 }
8129
8130 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8131 parameters, find the desired type.
8132
8133 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8134
8135 IN_DECL, if non-NULL, is the template declaration we are trying to
8136 instantiate.
8137
8138 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8139 the class we are looking up.
8140
8141 Issue error and warning messages under control of COMPLAIN.
8142
8143 If the template class is really a local class in a template
8144 function, then the FUNCTION_CONTEXT is the function in which it is
8145 being instantiated.
8146
8147 ??? Note that this function is currently called *twice* for each
8148 template-id: the first time from the parser, while creating the
8149 incomplete type (finish_template_type), and the second type during the
8150 real instantiation (instantiate_template_class). This is surely something
8151 that we want to avoid. It also causes some problems with argument
8152 coercion (see convert_nontype_argument for more information on this). */
8153
8154 static tree
8155 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8156 int entering_scope, tsubst_flags_t complain)
8157 {
8158 tree templ = NULL_TREE, parmlist;
8159 tree t;
8160 spec_entry **slot;
8161 spec_entry *entry;
8162 spec_entry elt;
8163 hashval_t hash;
8164
8165 if (identifier_p (d1))
8166 {
8167 tree value = innermost_non_namespace_value (d1);
8168 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8169 templ = value;
8170 else
8171 {
8172 if (context)
8173 push_decl_namespace (context);
8174 templ = lookup_name (d1);
8175 templ = maybe_get_template_decl_from_type_decl (templ);
8176 if (context)
8177 pop_decl_namespace ();
8178 }
8179 if (templ)
8180 context = DECL_CONTEXT (templ);
8181 }
8182 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8183 {
8184 tree type = TREE_TYPE (d1);
8185
8186 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8187 an implicit typename for the second A. Deal with it. */
8188 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8189 type = TREE_TYPE (type);
8190
8191 if (CLASSTYPE_TEMPLATE_INFO (type))
8192 {
8193 templ = CLASSTYPE_TI_TEMPLATE (type);
8194 d1 = DECL_NAME (templ);
8195 }
8196 }
8197 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8198 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8199 {
8200 templ = TYPE_TI_TEMPLATE (d1);
8201 d1 = DECL_NAME (templ);
8202 }
8203 else if (DECL_TYPE_TEMPLATE_P (d1))
8204 {
8205 templ = d1;
8206 d1 = DECL_NAME (templ);
8207 context = DECL_CONTEXT (templ);
8208 }
8209 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8210 {
8211 templ = d1;
8212 d1 = DECL_NAME (templ);
8213 }
8214
8215 /* Issue an error message if we didn't find a template. */
8216 if (! templ)
8217 {
8218 if (complain & tf_error)
8219 error ("%qT is not a template", d1);
8220 return error_mark_node;
8221 }
8222
8223 if (TREE_CODE (templ) != TEMPLATE_DECL
8224 /* Make sure it's a user visible template, if it was named by
8225 the user. */
8226 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8227 && !PRIMARY_TEMPLATE_P (templ)))
8228 {
8229 if (complain & tf_error)
8230 {
8231 error ("non-template type %qT used as a template", d1);
8232 if (in_decl)
8233 error ("for template declaration %q+D", in_decl);
8234 }
8235 return error_mark_node;
8236 }
8237
8238 complain &= ~tf_user;
8239
8240 /* An alias that just changes the name of a template is equivalent to the
8241 other template, so if any of the arguments are pack expansions, strip
8242 the alias to avoid problems with a pack expansion passed to a non-pack
8243 alias template parameter (DR 1430). */
8244 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8245 templ = get_underlying_template (templ);
8246
8247 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8248 {
8249 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
8250 template arguments */
8251
8252 tree parm;
8253 tree arglist2;
8254 tree outer;
8255
8256 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
8257
8258 /* Consider an example where a template template parameter declared as
8259
8260 template <class T, class U = std::allocator<T> > class TT
8261
8262 The template parameter level of T and U are one level larger than
8263 of TT. To proper process the default argument of U, say when an
8264 instantiation `TT<int>' is seen, we need to build the full
8265 arguments containing {int} as the innermost level. Outer levels,
8266 available when not appearing as default template argument, can be
8267 obtained from the arguments of the enclosing template.
8268
8269 Suppose that TT is later substituted with std::vector. The above
8270 instantiation is `TT<int, std::allocator<T> >' with TT at
8271 level 1, and T at level 2, while the template arguments at level 1
8272 becomes {std::vector} and the inner level 2 is {int}. */
8273
8274 outer = DECL_CONTEXT (templ);
8275 if (outer)
8276 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
8277 else if (current_template_parms)
8278 {
8279 /* This is an argument of the current template, so we haven't set
8280 DECL_CONTEXT yet. */
8281 tree relevant_template_parms;
8282
8283 /* Parameter levels that are greater than the level of the given
8284 template template parm are irrelevant. */
8285 relevant_template_parms = current_template_parms;
8286 while (TMPL_PARMS_DEPTH (relevant_template_parms)
8287 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
8288 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
8289
8290 outer = template_parms_to_args (relevant_template_parms);
8291 }
8292
8293 if (outer)
8294 arglist = add_to_template_args (outer, arglist);
8295
8296 arglist2 = coerce_template_parms (parmlist, arglist, templ,
8297 complain,
8298 /*require_all_args=*/true,
8299 /*use_default_args=*/true);
8300 if (arglist2 == error_mark_node
8301 || (!uses_template_parms (arglist2)
8302 && check_instantiated_args (templ, arglist2, complain)))
8303 return error_mark_node;
8304
8305 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8306 return parm;
8307 }
8308 else
8309 {
8310 tree template_type = TREE_TYPE (templ);
8311 tree gen_tmpl;
8312 tree type_decl;
8313 tree found = NULL_TREE;
8314 int arg_depth;
8315 int parm_depth;
8316 int is_dependent_type;
8317 int use_partial_inst_tmpl = false;
8318
8319 if (template_type == error_mark_node)
8320 /* An error occurred while building the template TEMPL, and a
8321 diagnostic has most certainly been emitted for that
8322 already. Let's propagate that error. */
8323 return error_mark_node;
8324
8325 gen_tmpl = most_general_template (templ);
8326 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8327 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8328 arg_depth = TMPL_ARGS_DEPTH (arglist);
8329
8330 if (arg_depth == 1 && parm_depth > 1)
8331 {
8332 /* We've been given an incomplete set of template arguments.
8333 For example, given:
8334
8335 template <class T> struct S1 {
8336 template <class U> struct S2 {};
8337 template <class U> struct S2<U*> {};
8338 };
8339
8340 we will be called with an ARGLIST of `U*', but the
8341 TEMPLATE will be `template <class T> template
8342 <class U> struct S1<T>::S2'. We must fill in the missing
8343 arguments. */
8344 arglist
8345 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
8346 arglist);
8347 arg_depth = TMPL_ARGS_DEPTH (arglist);
8348 }
8349
8350 /* Now we should have enough arguments. */
8351 gcc_assert (parm_depth == arg_depth);
8352
8353 /* From here on, we're only interested in the most general
8354 template. */
8355
8356 /* Calculate the BOUND_ARGS. These will be the args that are
8357 actually tsubst'd into the definition to create the
8358 instantiation. */
8359 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8360 complain,
8361 /*require_all_args=*/true,
8362 /*use_default_args=*/true);
8363
8364 if (arglist == error_mark_node)
8365 /* We were unable to bind the arguments. */
8366 return error_mark_node;
8367
8368 /* In the scope of a template class, explicit references to the
8369 template class refer to the type of the template, not any
8370 instantiation of it. For example, in:
8371
8372 template <class T> class C { void f(C<T>); }
8373
8374 the `C<T>' is just the same as `C'. Outside of the
8375 class, however, such a reference is an instantiation. */
8376 if ((entering_scope
8377 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8378 || currently_open_class (template_type))
8379 /* comp_template_args is expensive, check it last. */
8380 && comp_template_args (TYPE_TI_ARGS (template_type),
8381 arglist))
8382 return template_type;
8383
8384 /* If we already have this specialization, return it. */
8385 elt.tmpl = gen_tmpl;
8386 elt.args = arglist;
8387 elt.spec = NULL_TREE;
8388 hash = spec_hasher::hash (&elt);
8389 entry = type_specializations->find_with_hash (&elt, hash);
8390
8391 if (entry)
8392 return entry->spec;
8393
8394 /* If the the template's constraints are not satisfied,
8395 then we cannot form a valid type.
8396
8397 Note that the check is deferred until after the hash
8398 lookup. This prevents redundant checks on previously
8399 instantiated specializations. */
8400 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8401 {
8402 if (complain & tf_error)
8403 {
8404 error ("template constraint failure");
8405 diagnose_constraints (input_location, gen_tmpl, arglist);
8406 }
8407 return error_mark_node;
8408 }
8409
8410 is_dependent_type = uses_template_parms (arglist);
8411
8412 /* If the deduced arguments are invalid, then the binding
8413 failed. */
8414 if (!is_dependent_type
8415 && check_instantiated_args (gen_tmpl,
8416 INNERMOST_TEMPLATE_ARGS (arglist),
8417 complain))
8418 return error_mark_node;
8419
8420 if (!is_dependent_type
8421 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8422 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8423 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8424 {
8425 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8426 DECL_NAME (gen_tmpl),
8427 /*tag_scope=*/ts_global);
8428 return found;
8429 }
8430
8431 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8432 complain, in_decl);
8433 if (context == error_mark_node)
8434 return error_mark_node;
8435
8436 if (!context)
8437 context = global_namespace;
8438
8439 /* Create the type. */
8440 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8441 {
8442 /* The user referred to a specialization of an alias
8443 template represented by GEN_TMPL.
8444
8445 [temp.alias]/2 says:
8446
8447 When a template-id refers to the specialization of an
8448 alias template, it is equivalent to the associated
8449 type obtained by substitution of its
8450 template-arguments for the template-parameters in the
8451 type-id of the alias template. */
8452
8453 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8454 /* Note that the call above (by indirectly calling
8455 register_specialization in tsubst_decl) registers the
8456 TYPE_DECL representing the specialization of the alias
8457 template. So next time someone substitutes ARGLIST for
8458 the template parms into the alias template (GEN_TMPL),
8459 she'll get that TYPE_DECL back. */
8460
8461 if (t == error_mark_node)
8462 return t;
8463 }
8464 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8465 {
8466 if (!is_dependent_type)
8467 {
8468 set_current_access_from_decl (TYPE_NAME (template_type));
8469 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8470 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8471 arglist, complain, in_decl),
8472 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8473 arglist, complain, in_decl),
8474 SCOPED_ENUM_P (template_type), NULL);
8475
8476 if (t == error_mark_node)
8477 return t;
8478 }
8479 else
8480 {
8481 /* We don't want to call start_enum for this type, since
8482 the values for the enumeration constants may involve
8483 template parameters. And, no one should be interested
8484 in the enumeration constants for such a type. */
8485 t = cxx_make_type (ENUMERAL_TYPE);
8486 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8487 }
8488 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8489 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8490 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8491 }
8492 else if (CLASS_TYPE_P (template_type))
8493 {
8494 t = make_class_type (TREE_CODE (template_type));
8495 CLASSTYPE_DECLARED_CLASS (t)
8496 = CLASSTYPE_DECLARED_CLASS (template_type);
8497 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8498
8499 /* A local class. Make sure the decl gets registered properly. */
8500 if (context == current_function_decl)
8501 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8502
8503 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8504 /* This instantiation is another name for the primary
8505 template type. Set the TYPE_CANONICAL field
8506 appropriately. */
8507 TYPE_CANONICAL (t) = template_type;
8508 else if (any_template_arguments_need_structural_equality_p (arglist))
8509 /* Some of the template arguments require structural
8510 equality testing, so this template class requires
8511 structural equality testing. */
8512 SET_TYPE_STRUCTURAL_EQUALITY (t);
8513 }
8514 else
8515 gcc_unreachable ();
8516
8517 /* If we called start_enum or pushtag above, this information
8518 will already be set up. */
8519 if (!TYPE_NAME (t))
8520 {
8521 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8522
8523 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8524 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8525 DECL_SOURCE_LOCATION (type_decl)
8526 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8527 }
8528 else
8529 type_decl = TYPE_NAME (t);
8530
8531 if (CLASS_TYPE_P (template_type))
8532 {
8533 TREE_PRIVATE (type_decl)
8534 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8535 TREE_PROTECTED (type_decl)
8536 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8537 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8538 {
8539 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8540 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8541 }
8542 }
8543
8544 if (OVERLOAD_TYPE_P (t)
8545 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8546 {
8547 static const char *tags[] = {"abi_tag", "may_alias"};
8548
8549 for (unsigned ix = 0; ix != 2; ix++)
8550 {
8551 tree attributes
8552 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8553
8554 if (attributes)
8555 TYPE_ATTRIBUTES (t)
8556 = tree_cons (TREE_PURPOSE (attributes),
8557 TREE_VALUE (attributes),
8558 TYPE_ATTRIBUTES (t));
8559 }
8560 }
8561
8562 /* Let's consider the explicit specialization of a member
8563 of a class template specialization that is implicitly instantiated,
8564 e.g.:
8565 template<class T>
8566 struct S
8567 {
8568 template<class U> struct M {}; //#0
8569 };
8570
8571 template<>
8572 template<>
8573 struct S<int>::M<char> //#1
8574 {
8575 int i;
8576 };
8577 [temp.expl.spec]/4 says this is valid.
8578
8579 In this case, when we write:
8580 S<int>::M<char> m;
8581
8582 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8583 the one of #0.
8584
8585 When we encounter #1, we want to store the partial instantiation
8586 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8587
8588 For all cases other than this "explicit specialization of member of a
8589 class template", we just want to store the most general template into
8590 the CLASSTYPE_TI_TEMPLATE of M.
8591
8592 This case of "explicit specialization of member of a class template"
8593 only happens when:
8594 1/ the enclosing class is an instantiation of, and therefore not
8595 the same as, the context of the most general template, and
8596 2/ we aren't looking at the partial instantiation itself, i.e.
8597 the innermost arguments are not the same as the innermost parms of
8598 the most general template.
8599
8600 So it's only when 1/ and 2/ happens that we want to use the partial
8601 instantiation of the member template in lieu of its most general
8602 template. */
8603
8604 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8605 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8606 /* the enclosing class must be an instantiation... */
8607 && CLASS_TYPE_P (context)
8608 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8609 {
8610 tree partial_inst_args;
8611 TREE_VEC_LENGTH (arglist)--;
8612 ++processing_template_decl;
8613 partial_inst_args =
8614 tsubst (INNERMOST_TEMPLATE_ARGS
8615 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8616 arglist, complain, NULL_TREE);
8617 --processing_template_decl;
8618 TREE_VEC_LENGTH (arglist)++;
8619 if (partial_inst_args == error_mark_node)
8620 return error_mark_node;
8621 use_partial_inst_tmpl =
8622 /*...and we must not be looking at the partial instantiation
8623 itself. */
8624 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8625 partial_inst_args);
8626 }
8627
8628 if (!use_partial_inst_tmpl)
8629 /* This case is easy; there are no member templates involved. */
8630 found = gen_tmpl;
8631 else
8632 {
8633 /* This is a full instantiation of a member template. Find
8634 the partial instantiation of which this is an instance. */
8635
8636 /* Temporarily reduce by one the number of levels in the ARGLIST
8637 so as to avoid comparing the last set of arguments. */
8638 TREE_VEC_LENGTH (arglist)--;
8639 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8640 TREE_VEC_LENGTH (arglist)++;
8641 /* FOUND is either a proper class type, or an alias
8642 template specialization. In the later case, it's a
8643 TYPE_DECL, resulting from the substituting of arguments
8644 for parameters in the TYPE_DECL of the alias template
8645 done earlier. So be careful while getting the template
8646 of FOUND. */
8647 found = TREE_CODE (found) == TEMPLATE_DECL
8648 ? found
8649 : TREE_CODE (found) == TYPE_DECL
8650 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8651 : CLASSTYPE_TI_TEMPLATE (found);
8652 }
8653
8654 // Build template info for the new specialization.
8655 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8656
8657 elt.spec = t;
8658 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8659 entry = ggc_alloc<spec_entry> ();
8660 *entry = elt;
8661 *slot = entry;
8662
8663 /* Note this use of the partial instantiation so we can check it
8664 later in maybe_process_partial_specialization. */
8665 DECL_TEMPLATE_INSTANTIATIONS (found)
8666 = tree_cons (arglist, t,
8667 DECL_TEMPLATE_INSTANTIATIONS (found));
8668
8669 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8670 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8671 /* Now that the type has been registered on the instantiations
8672 list, we set up the enumerators. Because the enumeration
8673 constants may involve the enumeration type itself, we make
8674 sure to register the type first, and then create the
8675 constants. That way, doing tsubst_expr for the enumeration
8676 constants won't result in recursive calls here; we'll find
8677 the instantiation and exit above. */
8678 tsubst_enum (template_type, t, arglist);
8679
8680 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8681 /* If the type makes use of template parameters, the
8682 code that generates debugging information will crash. */
8683 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8684
8685 /* Possibly limit visibility based on template args. */
8686 TREE_PUBLIC (type_decl) = 1;
8687 determine_visibility (type_decl);
8688
8689 inherit_targ_abi_tags (t);
8690
8691 return t;
8692 }
8693 }
8694
8695 /* Wrapper for lookup_template_class_1. */
8696
8697 tree
8698 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8699 int entering_scope, tsubst_flags_t complain)
8700 {
8701 tree ret;
8702 timevar_push (TV_TEMPLATE_INST);
8703 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8704 entering_scope, complain);
8705 timevar_pop (TV_TEMPLATE_INST);
8706 return ret;
8707 }
8708
8709 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8710
8711 tree
8712 lookup_template_variable (tree templ, tree arglist)
8713 {
8714 /* The type of the expression is NULL_TREE since the template-id could refer
8715 to an explicit or partial specialization. */
8716 tree type = NULL_TREE;
8717 if (flag_concepts && variable_concept_p (templ))
8718 /* Except that concepts are always bool. */
8719 type = boolean_type_node;
8720 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8721 }
8722
8723 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8724
8725 tree
8726 finish_template_variable (tree var, tsubst_flags_t complain)
8727 {
8728 tree templ = TREE_OPERAND (var, 0);
8729 tree arglist = TREE_OPERAND (var, 1);
8730
8731 /* We never want to return a VAR_DECL for a variable concept, since they
8732 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
8733 bool concept_p = flag_concepts && variable_concept_p (templ);
8734 if (concept_p && processing_template_decl)
8735 return var;
8736
8737 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8738 arglist = add_outermost_template_args (tmpl_args, arglist);
8739
8740 tree parms = DECL_TEMPLATE_PARMS (templ);
8741 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8742 /*req_all*/true,
8743 /*use_default*/true);
8744
8745 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
8746 {
8747 if (complain & tf_error)
8748 {
8749 error ("use of invalid variable template %qE", var);
8750 diagnose_constraints (location_of (var), templ, arglist);
8751 }
8752 return error_mark_node;
8753 }
8754
8755 /* If a template-id refers to a specialization of a variable
8756 concept, then the expression is true if and only if the
8757 concept's constraints are satisfied by the given template
8758 arguments.
8759
8760 NOTE: This is an extension of Concepts Lite TS that
8761 allows constraints to be used in expressions. */
8762 if (concept_p)
8763 {
8764 tree decl = DECL_TEMPLATE_RESULT (templ);
8765 return evaluate_variable_concept (decl, arglist);
8766 }
8767
8768 return instantiate_template (templ, arglist, complain);
8769 }
8770
8771 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
8772 TARGS template args, and instantiate it if it's not dependent. */
8773
8774 static tree
8775 lookup_and_finish_template_variable (tree templ, tree targs,
8776 tsubst_flags_t complain)
8777 {
8778 templ = lookup_template_variable (templ, targs);
8779 if (!any_dependent_template_arguments_p (targs))
8780 {
8781 templ = finish_template_variable (templ, complain);
8782 mark_used (templ);
8783 }
8784
8785 return convert_from_reference (templ);
8786 }
8787
8788 \f
8789 struct pair_fn_data
8790 {
8791 tree_fn_t fn;
8792 void *data;
8793 /* True when we should also visit template parameters that occur in
8794 non-deduced contexts. */
8795 bool include_nondeduced_p;
8796 hash_set<tree> *visited;
8797 };
8798
8799 /* Called from for_each_template_parm via walk_tree. */
8800
8801 static tree
8802 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8803 {
8804 tree t = *tp;
8805 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8806 tree_fn_t fn = pfd->fn;
8807 void *data = pfd->data;
8808 tree result = NULL_TREE;
8809
8810 #define WALK_SUBTREE(NODE) \
8811 do \
8812 { \
8813 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
8814 pfd->include_nondeduced_p); \
8815 if (result) goto out; \
8816 } \
8817 while (0)
8818
8819 if (TYPE_P (t)
8820 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
8821 WALK_SUBTREE (TYPE_CONTEXT (t));
8822
8823 switch (TREE_CODE (t))
8824 {
8825 case RECORD_TYPE:
8826 if (TYPE_PTRMEMFUNC_P (t))
8827 break;
8828 /* Fall through. */
8829
8830 case UNION_TYPE:
8831 case ENUMERAL_TYPE:
8832 if (!TYPE_TEMPLATE_INFO (t))
8833 *walk_subtrees = 0;
8834 else
8835 WALK_SUBTREE (TYPE_TI_ARGS (t));
8836 break;
8837
8838 case INTEGER_TYPE:
8839 WALK_SUBTREE (TYPE_MIN_VALUE (t));
8840 WALK_SUBTREE (TYPE_MAX_VALUE (t));
8841 break;
8842
8843 case METHOD_TYPE:
8844 /* Since we're not going to walk subtrees, we have to do this
8845 explicitly here. */
8846 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
8847 /* Fall through. */
8848
8849 case FUNCTION_TYPE:
8850 /* Check the return type. */
8851 WALK_SUBTREE (TREE_TYPE (t));
8852
8853 /* Check the parameter types. Since default arguments are not
8854 instantiated until they are needed, the TYPE_ARG_TYPES may
8855 contain expressions that involve template parameters. But,
8856 no-one should be looking at them yet. And, once they're
8857 instantiated, they don't contain template parameters, so
8858 there's no point in looking at them then, either. */
8859 {
8860 tree parm;
8861
8862 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8863 WALK_SUBTREE (TREE_VALUE (parm));
8864
8865 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8866 want walk_tree walking into them itself. */
8867 *walk_subtrees = 0;
8868 }
8869
8870 if (flag_noexcept_type)
8871 {
8872 tree spec = TYPE_RAISES_EXCEPTIONS (t);
8873 if (spec)
8874 WALK_SUBTREE (TREE_PURPOSE (spec));
8875 }
8876 break;
8877
8878 case TYPEOF_TYPE:
8879 case UNDERLYING_TYPE:
8880 if (pfd->include_nondeduced_p
8881 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8882 pfd->visited,
8883 pfd->include_nondeduced_p))
8884 return error_mark_node;
8885 break;
8886
8887 case FUNCTION_DECL:
8888 case VAR_DECL:
8889 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8890 WALK_SUBTREE (DECL_TI_ARGS (t));
8891 /* Fall through. */
8892
8893 case PARM_DECL:
8894 case CONST_DECL:
8895 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
8896 WALK_SUBTREE (DECL_INITIAL (t));
8897 if (DECL_CONTEXT (t)
8898 && pfd->include_nondeduced_p)
8899 WALK_SUBTREE (DECL_CONTEXT (t));
8900 break;
8901
8902 case BOUND_TEMPLATE_TEMPLATE_PARM:
8903 /* Record template parameters such as `T' inside `TT<T>'. */
8904 WALK_SUBTREE (TYPE_TI_ARGS (t));
8905 /* Fall through. */
8906
8907 case TEMPLATE_TEMPLATE_PARM:
8908 case TEMPLATE_TYPE_PARM:
8909 case TEMPLATE_PARM_INDEX:
8910 if (fn && (*fn)(t, data))
8911 return t;
8912 else if (!fn)
8913 return t;
8914 break;
8915
8916 case TEMPLATE_DECL:
8917 /* A template template parameter is encountered. */
8918 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8919 WALK_SUBTREE (TREE_TYPE (t));
8920
8921 /* Already substituted template template parameter */
8922 *walk_subtrees = 0;
8923 break;
8924
8925 case TYPENAME_TYPE:
8926 /* A template-id in a TYPENAME_TYPE might be a deduced context after
8927 partial instantiation. */
8928 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
8929 break;
8930
8931 case CONSTRUCTOR:
8932 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8933 && pfd->include_nondeduced_p)
8934 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
8935 break;
8936
8937 case INDIRECT_REF:
8938 case COMPONENT_REF:
8939 /* If there's no type, then this thing must be some expression
8940 involving template parameters. */
8941 if (!fn && !TREE_TYPE (t))
8942 return error_mark_node;
8943 break;
8944
8945 case MODOP_EXPR:
8946 case CAST_EXPR:
8947 case IMPLICIT_CONV_EXPR:
8948 case REINTERPRET_CAST_EXPR:
8949 case CONST_CAST_EXPR:
8950 case STATIC_CAST_EXPR:
8951 case DYNAMIC_CAST_EXPR:
8952 case ARROW_EXPR:
8953 case DOTSTAR_EXPR:
8954 case TYPEID_EXPR:
8955 case PSEUDO_DTOR_EXPR:
8956 if (!fn)
8957 return error_mark_node;
8958 break;
8959
8960 default:
8961 break;
8962 }
8963
8964 #undef WALK_SUBTREE
8965
8966 /* We didn't find any template parameters we liked. */
8967 out:
8968 return result;
8969 }
8970
8971 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8972 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8973 call FN with the parameter and the DATA.
8974 If FN returns nonzero, the iteration is terminated, and
8975 for_each_template_parm returns 1. Otherwise, the iteration
8976 continues. If FN never returns a nonzero value, the value
8977 returned by for_each_template_parm is 0. If FN is NULL, it is
8978 considered to be the function which always returns 1.
8979
8980 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8981 parameters that occur in non-deduced contexts. When false, only
8982 visits those template parameters that can be deduced. */
8983
8984 static tree
8985 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8986 hash_set<tree> *visited,
8987 bool include_nondeduced_p)
8988 {
8989 struct pair_fn_data pfd;
8990 tree result;
8991
8992 /* Set up. */
8993 pfd.fn = fn;
8994 pfd.data = data;
8995 pfd.include_nondeduced_p = include_nondeduced_p;
8996
8997 /* Walk the tree. (Conceptually, we would like to walk without
8998 duplicates, but for_each_template_parm_r recursively calls
8999 for_each_template_parm, so we would need to reorganize a fair
9000 bit to use walk_tree_without_duplicates, so we keep our own
9001 visited list.) */
9002 if (visited)
9003 pfd.visited = visited;
9004 else
9005 pfd.visited = new hash_set<tree>;
9006 result = cp_walk_tree (&t,
9007 for_each_template_parm_r,
9008 &pfd,
9009 pfd.visited);
9010
9011 /* Clean up. */
9012 if (!visited)
9013 {
9014 delete pfd.visited;
9015 pfd.visited = 0;
9016 }
9017
9018 return result;
9019 }
9020
9021 /* Returns true if T depends on any template parameter. */
9022
9023 int
9024 uses_template_parms (tree t)
9025 {
9026 if (t == NULL_TREE)
9027 return false;
9028
9029 bool dependent_p;
9030 int saved_processing_template_decl;
9031
9032 saved_processing_template_decl = processing_template_decl;
9033 if (!saved_processing_template_decl)
9034 processing_template_decl = 1;
9035 if (TYPE_P (t))
9036 dependent_p = dependent_type_p (t);
9037 else if (TREE_CODE (t) == TREE_VEC)
9038 dependent_p = any_dependent_template_arguments_p (t);
9039 else if (TREE_CODE (t) == TREE_LIST)
9040 dependent_p = (uses_template_parms (TREE_VALUE (t))
9041 || uses_template_parms (TREE_CHAIN (t)));
9042 else if (TREE_CODE (t) == TYPE_DECL)
9043 dependent_p = dependent_type_p (TREE_TYPE (t));
9044 else if (DECL_P (t)
9045 || EXPR_P (t)
9046 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9047 || TREE_CODE (t) == OVERLOAD
9048 || BASELINK_P (t)
9049 || identifier_p (t)
9050 || TREE_CODE (t) == TRAIT_EXPR
9051 || TREE_CODE (t) == CONSTRUCTOR
9052 || CONSTANT_CLASS_P (t))
9053 dependent_p = (type_dependent_expression_p (t)
9054 || value_dependent_expression_p (t));
9055 else
9056 {
9057 gcc_assert (t == error_mark_node);
9058 dependent_p = false;
9059 }
9060
9061 processing_template_decl = saved_processing_template_decl;
9062
9063 return dependent_p;
9064 }
9065
9066 /* Returns true iff current_function_decl is an incompletely instantiated
9067 template. Useful instead of processing_template_decl because the latter
9068 is set to 0 during instantiate_non_dependent_expr. */
9069
9070 bool
9071 in_template_function (void)
9072 {
9073 tree fn = current_function_decl;
9074 bool ret;
9075 ++processing_template_decl;
9076 ret = (fn && DECL_LANG_SPECIFIC (fn)
9077 && DECL_TEMPLATE_INFO (fn)
9078 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9079 --processing_template_decl;
9080 return ret;
9081 }
9082
9083 /* Returns true if T depends on any template parameter with level LEVEL. */
9084
9085 bool
9086 uses_template_parms_level (tree t, int level)
9087 {
9088 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9089 /*include_nondeduced_p=*/true);
9090 }
9091
9092 /* Returns true if the signature of DECL depends on any template parameter from
9093 its enclosing class. */
9094
9095 bool
9096 uses_outer_template_parms (tree decl)
9097 {
9098 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9099 if (depth == 0)
9100 return false;
9101 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9102 &depth, NULL, /*include_nondeduced_p=*/true))
9103 return true;
9104 if (PRIMARY_TEMPLATE_P (decl)
9105 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9106 (DECL_TEMPLATE_PARMS (decl)),
9107 template_parm_outer_level,
9108 &depth, NULL, /*include_nondeduced_p=*/true))
9109 return true;
9110 tree ci = get_constraints (decl);
9111 if (ci)
9112 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9113 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9114 &depth, NULL, /*nondeduced*/true))
9115 return true;
9116 return false;
9117 }
9118
9119 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9120 ill-formed translation unit, i.e. a variable or function that isn't
9121 usable in a constant expression. */
9122
9123 static inline bool
9124 neglectable_inst_p (tree d)
9125 {
9126 return (DECL_P (d)
9127 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9128 : decl_maybe_constant_var_p (d)));
9129 }
9130
9131 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9132 neglectable and instantiated from within an erroneous instantiation. */
9133
9134 static bool
9135 limit_bad_template_recursion (tree decl)
9136 {
9137 struct tinst_level *lev = current_tinst_level;
9138 int errs = errorcount + sorrycount;
9139 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9140 return false;
9141
9142 for (; lev; lev = lev->next)
9143 if (neglectable_inst_p (lev->decl))
9144 break;
9145
9146 return (lev && errs > lev->errors);
9147 }
9148
9149 static int tinst_depth;
9150 extern int max_tinst_depth;
9151 int depth_reached;
9152
9153 static GTY(()) struct tinst_level *last_error_tinst_level;
9154
9155 /* We're starting to instantiate D; record the template instantiation context
9156 for diagnostics and to restore it later. */
9157
9158 bool
9159 push_tinst_level (tree d)
9160 {
9161 return push_tinst_level_loc (d, input_location);
9162 }
9163
9164 /* We're starting to instantiate D; record the template instantiation context
9165 at LOC for diagnostics and to restore it later. */
9166
9167 bool
9168 push_tinst_level_loc (tree d, location_t loc)
9169 {
9170 struct tinst_level *new_level;
9171
9172 if (tinst_depth >= max_tinst_depth)
9173 {
9174 /* Tell error.c not to try to instantiate any templates. */
9175 at_eof = 2;
9176 fatal_error (input_location,
9177 "template instantiation depth exceeds maximum of %d"
9178 " (use -ftemplate-depth= to increase the maximum)",
9179 max_tinst_depth);
9180 return false;
9181 }
9182
9183 /* If the current instantiation caused problems, don't let it instantiate
9184 anything else. Do allow deduction substitution and decls usable in
9185 constant expressions. */
9186 if (limit_bad_template_recursion (d))
9187 return false;
9188
9189 /* When not -quiet, dump template instantiations other than functions, since
9190 announce_function will take care of those. */
9191 if (!quiet_flag
9192 && TREE_CODE (d) != TREE_LIST
9193 && TREE_CODE (d) != FUNCTION_DECL)
9194 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9195
9196 new_level = ggc_alloc<tinst_level> ();
9197 new_level->decl = d;
9198 new_level->locus = loc;
9199 new_level->errors = errorcount+sorrycount;
9200 new_level->in_system_header_p = in_system_header_at (input_location);
9201 new_level->next = current_tinst_level;
9202 current_tinst_level = new_level;
9203
9204 ++tinst_depth;
9205 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9206 depth_reached = tinst_depth;
9207
9208 return true;
9209 }
9210
9211 /* We're done instantiating this template; return to the instantiation
9212 context. */
9213
9214 void
9215 pop_tinst_level (void)
9216 {
9217 /* Restore the filename and line number stashed away when we started
9218 this instantiation. */
9219 input_location = current_tinst_level->locus;
9220 current_tinst_level = current_tinst_level->next;
9221 --tinst_depth;
9222 }
9223
9224 /* We're instantiating a deferred template; restore the template
9225 instantiation context in which the instantiation was requested, which
9226 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9227
9228 static tree
9229 reopen_tinst_level (struct tinst_level *level)
9230 {
9231 struct tinst_level *t;
9232
9233 tinst_depth = 0;
9234 for (t = level; t; t = t->next)
9235 ++tinst_depth;
9236
9237 current_tinst_level = level;
9238 pop_tinst_level ();
9239 if (current_tinst_level)
9240 current_tinst_level->errors = errorcount+sorrycount;
9241 return level->decl;
9242 }
9243
9244 /* Returns the TINST_LEVEL which gives the original instantiation
9245 context. */
9246
9247 struct tinst_level *
9248 outermost_tinst_level (void)
9249 {
9250 struct tinst_level *level = current_tinst_level;
9251 if (level)
9252 while (level->next)
9253 level = level->next;
9254 return level;
9255 }
9256
9257 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9258 vector of template arguments, as for tsubst.
9259
9260 Returns an appropriate tsubst'd friend declaration. */
9261
9262 static tree
9263 tsubst_friend_function (tree decl, tree args)
9264 {
9265 tree new_friend;
9266
9267 if (TREE_CODE (decl) == FUNCTION_DECL
9268 && DECL_TEMPLATE_INSTANTIATION (decl)
9269 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9270 /* This was a friend declared with an explicit template
9271 argument list, e.g.:
9272
9273 friend void f<>(T);
9274
9275 to indicate that f was a template instantiation, not a new
9276 function declaration. Now, we have to figure out what
9277 instantiation of what template. */
9278 {
9279 tree template_id, arglist, fns;
9280 tree new_args;
9281 tree tmpl;
9282 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9283
9284 /* Friend functions are looked up in the containing namespace scope.
9285 We must enter that scope, to avoid finding member functions of the
9286 current class with same name. */
9287 push_nested_namespace (ns);
9288 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9289 tf_warning_or_error, NULL_TREE,
9290 /*integral_constant_expression_p=*/false);
9291 pop_nested_namespace (ns);
9292 arglist = tsubst (DECL_TI_ARGS (decl), args,
9293 tf_warning_or_error, NULL_TREE);
9294 template_id = lookup_template_function (fns, arglist);
9295
9296 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9297 tmpl = determine_specialization (template_id, new_friend,
9298 &new_args,
9299 /*need_member_template=*/0,
9300 TREE_VEC_LENGTH (args),
9301 tsk_none);
9302 return instantiate_template (tmpl, new_args, tf_error);
9303 }
9304
9305 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9306
9307 /* The NEW_FRIEND will look like an instantiation, to the
9308 compiler, but is not an instantiation from the point of view of
9309 the language. For example, we might have had:
9310
9311 template <class T> struct S {
9312 template <class U> friend void f(T, U);
9313 };
9314
9315 Then, in S<int>, template <class U> void f(int, U) is not an
9316 instantiation of anything. */
9317 if (new_friend == error_mark_node)
9318 return error_mark_node;
9319
9320 DECL_USE_TEMPLATE (new_friend) = 0;
9321 if (TREE_CODE (decl) == TEMPLATE_DECL)
9322 {
9323 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9324 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9325 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9326 }
9327
9328 /* The mangled name for the NEW_FRIEND is incorrect. The function
9329 is not a template instantiation and should not be mangled like
9330 one. Therefore, we forget the mangling here; we'll recompute it
9331 later if we need it. */
9332 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9333 {
9334 SET_DECL_RTL (new_friend, NULL);
9335 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9336 }
9337
9338 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9339 {
9340 tree old_decl;
9341 tree new_friend_template_info;
9342 tree new_friend_result_template_info;
9343 tree ns;
9344 int new_friend_is_defn;
9345
9346 /* We must save some information from NEW_FRIEND before calling
9347 duplicate decls since that function will free NEW_FRIEND if
9348 possible. */
9349 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9350 new_friend_is_defn =
9351 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9352 (template_for_substitution (new_friend)))
9353 != NULL_TREE);
9354 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9355 {
9356 /* This declaration is a `primary' template. */
9357 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9358
9359 new_friend_result_template_info
9360 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9361 }
9362 else
9363 new_friend_result_template_info = NULL_TREE;
9364
9365 /* Make the init_value nonzero so pushdecl knows this is a defn. */
9366 if (new_friend_is_defn)
9367 DECL_INITIAL (new_friend) = error_mark_node;
9368
9369 /* Inside pushdecl_namespace_level, we will push into the
9370 current namespace. However, the friend function should go
9371 into the namespace of the template. */
9372 ns = decl_namespace_context (new_friend);
9373 push_nested_namespace (ns);
9374 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9375 pop_nested_namespace (ns);
9376
9377 if (old_decl == error_mark_node)
9378 return error_mark_node;
9379
9380 if (old_decl != new_friend)
9381 {
9382 /* This new friend declaration matched an existing
9383 declaration. For example, given:
9384
9385 template <class T> void f(T);
9386 template <class U> class C {
9387 template <class T> friend void f(T) {}
9388 };
9389
9390 the friend declaration actually provides the definition
9391 of `f', once C has been instantiated for some type. So,
9392 old_decl will be the out-of-class template declaration,
9393 while new_friend is the in-class definition.
9394
9395 But, if `f' was called before this point, the
9396 instantiation of `f' will have DECL_TI_ARGS corresponding
9397 to `T' but not to `U', references to which might appear
9398 in the definition of `f'. Previously, the most general
9399 template for an instantiation of `f' was the out-of-class
9400 version; now it is the in-class version. Therefore, we
9401 run through all specialization of `f', adding to their
9402 DECL_TI_ARGS appropriately. In particular, they need a
9403 new set of outer arguments, corresponding to the
9404 arguments for this class instantiation.
9405
9406 The same situation can arise with something like this:
9407
9408 friend void f(int);
9409 template <class T> class C {
9410 friend void f(T) {}
9411 };
9412
9413 when `C<int>' is instantiated. Now, `f(int)' is defined
9414 in the class. */
9415
9416 if (!new_friend_is_defn)
9417 /* On the other hand, if the in-class declaration does
9418 *not* provide a definition, then we don't want to alter
9419 existing definitions. We can just leave everything
9420 alone. */
9421 ;
9422 else
9423 {
9424 tree new_template = TI_TEMPLATE (new_friend_template_info);
9425 tree new_args = TI_ARGS (new_friend_template_info);
9426
9427 /* Overwrite whatever template info was there before, if
9428 any, with the new template information pertaining to
9429 the declaration. */
9430 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9431
9432 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9433 {
9434 /* We should have called reregister_specialization in
9435 duplicate_decls. */
9436 gcc_assert (retrieve_specialization (new_template,
9437 new_args, 0)
9438 == old_decl);
9439
9440 /* Instantiate it if the global has already been used. */
9441 if (DECL_ODR_USED (old_decl))
9442 instantiate_decl (old_decl, /*defer_ok=*/true,
9443 /*expl_inst_class_mem_p=*/false);
9444 }
9445 else
9446 {
9447 tree t;
9448
9449 /* Indicate that the old function template is a partial
9450 instantiation. */
9451 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9452 = new_friend_result_template_info;
9453
9454 gcc_assert (new_template
9455 == most_general_template (new_template));
9456 gcc_assert (new_template != old_decl);
9457
9458 /* Reassign any specializations already in the hash table
9459 to the new more general template, and add the
9460 additional template args. */
9461 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9462 t != NULL_TREE;
9463 t = TREE_CHAIN (t))
9464 {
9465 tree spec = TREE_VALUE (t);
9466 spec_entry elt;
9467
9468 elt.tmpl = old_decl;
9469 elt.args = DECL_TI_ARGS (spec);
9470 elt.spec = NULL_TREE;
9471
9472 decl_specializations->remove_elt (&elt);
9473
9474 DECL_TI_ARGS (spec)
9475 = add_outermost_template_args (new_args,
9476 DECL_TI_ARGS (spec));
9477
9478 register_specialization
9479 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9480
9481 }
9482 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9483 }
9484 }
9485
9486 /* The information from NEW_FRIEND has been merged into OLD_DECL
9487 by duplicate_decls. */
9488 new_friend = old_decl;
9489 }
9490 }
9491 else
9492 {
9493 tree context = DECL_CONTEXT (new_friend);
9494 bool dependent_p;
9495
9496 /* In the code
9497 template <class T> class C {
9498 template <class U> friend void C1<U>::f (); // case 1
9499 friend void C2<T>::f (); // case 2
9500 };
9501 we only need to make sure CONTEXT is a complete type for
9502 case 2. To distinguish between the two cases, we note that
9503 CONTEXT of case 1 remains dependent type after tsubst while
9504 this isn't true for case 2. */
9505 ++processing_template_decl;
9506 dependent_p = dependent_type_p (context);
9507 --processing_template_decl;
9508
9509 if (!dependent_p
9510 && !complete_type_or_else (context, NULL_TREE))
9511 return error_mark_node;
9512
9513 if (COMPLETE_TYPE_P (context))
9514 {
9515 tree fn = new_friend;
9516 /* do_friend adds the TEMPLATE_DECL for any member friend
9517 template even if it isn't a member template, i.e.
9518 template <class T> friend A<T>::f();
9519 Look through it in that case. */
9520 if (TREE_CODE (fn) == TEMPLATE_DECL
9521 && !PRIMARY_TEMPLATE_P (fn))
9522 fn = DECL_TEMPLATE_RESULT (fn);
9523 /* Check to see that the declaration is really present, and,
9524 possibly obtain an improved declaration. */
9525 fn = check_classfn (context, fn, NULL_TREE);
9526
9527 if (fn)
9528 new_friend = fn;
9529 }
9530 }
9531
9532 return new_friend;
9533 }
9534
9535 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9536 template arguments, as for tsubst.
9537
9538 Returns an appropriate tsubst'd friend type or error_mark_node on
9539 failure. */
9540
9541 static tree
9542 tsubst_friend_class (tree friend_tmpl, tree args)
9543 {
9544 tree friend_type;
9545 tree tmpl;
9546 tree context;
9547
9548 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9549 {
9550 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9551 return TREE_TYPE (t);
9552 }
9553
9554 context = CP_DECL_CONTEXT (friend_tmpl);
9555
9556 if (context != global_namespace)
9557 {
9558 if (TREE_CODE (context) == NAMESPACE_DECL)
9559 push_nested_namespace (context);
9560 else
9561 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9562 }
9563
9564 /* Look for a class template declaration. We look for hidden names
9565 because two friend declarations of the same template are the
9566 same. For example, in:
9567
9568 struct A {
9569 template <typename> friend class F;
9570 };
9571 template <typename> struct B {
9572 template <typename> friend class F;
9573 };
9574
9575 both F templates are the same. */
9576 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9577 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9578
9579 /* But, if we don't find one, it might be because we're in a
9580 situation like this:
9581
9582 template <class T>
9583 struct S {
9584 template <class U>
9585 friend struct S;
9586 };
9587
9588 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9589 for `S<int>', not the TEMPLATE_DECL. */
9590 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9591 {
9592 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9593 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9594 }
9595
9596 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9597 {
9598 /* The friend template has already been declared. Just
9599 check to see that the declarations match, and install any new
9600 default parameters. We must tsubst the default parameters,
9601 of course. We only need the innermost template parameters
9602 because that is all that redeclare_class_template will look
9603 at. */
9604 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9605 > TMPL_ARGS_DEPTH (args))
9606 {
9607 tree parms;
9608 location_t saved_input_location;
9609 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9610 args, tf_warning_or_error);
9611
9612 saved_input_location = input_location;
9613 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9614 tree cons = get_constraints (tmpl);
9615 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9616 input_location = saved_input_location;
9617
9618 }
9619
9620 friend_type = TREE_TYPE (tmpl);
9621 }
9622 else
9623 {
9624 /* The friend template has not already been declared. In this
9625 case, the instantiation of the template class will cause the
9626 injection of this template into the global scope. */
9627 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9628 if (tmpl == error_mark_node)
9629 return error_mark_node;
9630
9631 /* The new TMPL is not an instantiation of anything, so we
9632 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9633 the new type because that is supposed to be the corresponding
9634 template decl, i.e., TMPL. */
9635 DECL_USE_TEMPLATE (tmpl) = 0;
9636 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9637 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9638 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9639 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9640
9641 /* Inject this template into the global scope. */
9642 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9643 }
9644
9645 if (context != global_namespace)
9646 {
9647 if (TREE_CODE (context) == NAMESPACE_DECL)
9648 pop_nested_namespace (context);
9649 else
9650 pop_nested_class ();
9651 }
9652
9653 return friend_type;
9654 }
9655
9656 /* Returns zero if TYPE cannot be completed later due to circularity.
9657 Otherwise returns one. */
9658
9659 static int
9660 can_complete_type_without_circularity (tree type)
9661 {
9662 if (type == NULL_TREE || type == error_mark_node)
9663 return 0;
9664 else if (COMPLETE_TYPE_P (type))
9665 return 1;
9666 else if (TREE_CODE (type) == ARRAY_TYPE)
9667 return can_complete_type_without_circularity (TREE_TYPE (type));
9668 else if (CLASS_TYPE_P (type)
9669 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9670 return 0;
9671 else
9672 return 1;
9673 }
9674
9675 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9676 tsubst_flags_t, tree);
9677
9678 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9679 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9680
9681 static tree
9682 tsubst_attribute (tree t, tree *decl_p, tree args,
9683 tsubst_flags_t complain, tree in_decl)
9684 {
9685 gcc_assert (ATTR_IS_DEPENDENT (t));
9686
9687 tree val = TREE_VALUE (t);
9688 if (val == NULL_TREE)
9689 /* Nothing to do. */;
9690 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9691 && is_attribute_p ("omp declare simd",
9692 get_attribute_name (t)))
9693 {
9694 tree clauses = TREE_VALUE (val);
9695 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9696 complain, in_decl);
9697 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9698 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9699 tree parms = DECL_ARGUMENTS (*decl_p);
9700 clauses
9701 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9702 if (clauses)
9703 val = build_tree_list (NULL_TREE, clauses);
9704 else
9705 val = NULL_TREE;
9706 }
9707 /* If the first attribute argument is an identifier, don't
9708 pass it through tsubst. Attributes like mode, format,
9709 cleanup and several target specific attributes expect it
9710 unmodified. */
9711 else if (attribute_takes_identifier_p (get_attribute_name (t)))
9712 {
9713 tree chain
9714 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
9715 /*integral_constant_expression_p=*/false);
9716 if (chain != TREE_CHAIN (val))
9717 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
9718 }
9719 else if (PACK_EXPANSION_P (val))
9720 {
9721 /* An attribute pack expansion. */
9722 tree purp = TREE_PURPOSE (t);
9723 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
9724 int len = TREE_VEC_LENGTH (pack);
9725 tree list = NULL_TREE;
9726 tree *q = &list;
9727 for (int i = 0; i < len; ++i)
9728 {
9729 tree elt = TREE_VEC_ELT (pack, i);
9730 *q = build_tree_list (purp, elt);
9731 q = &TREE_CHAIN (*q);
9732 }
9733 return list;
9734 }
9735 else
9736 val = tsubst_expr (val, args, complain, in_decl,
9737 /*integral_constant_expression_p=*/false);
9738
9739 if (val != TREE_VALUE (t))
9740 return build_tree_list (TREE_PURPOSE (t), val);
9741 return t;
9742 }
9743
9744 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
9745 unchanged or a new TREE_LIST chain. */
9746
9747 static tree
9748 tsubst_attributes (tree attributes, tree args,
9749 tsubst_flags_t complain, tree in_decl)
9750 {
9751 tree last_dep = NULL_TREE;
9752
9753 for (tree t = attributes; t; t = TREE_CHAIN (t))
9754 if (ATTR_IS_DEPENDENT (t))
9755 {
9756 last_dep = t;
9757 attributes = copy_list (attributes);
9758 break;
9759 }
9760
9761 if (last_dep)
9762 for (tree *p = &attributes; *p; )
9763 {
9764 tree t = *p;
9765 if (ATTR_IS_DEPENDENT (t))
9766 {
9767 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
9768 if (subst != t)
9769 {
9770 *p = subst;
9771 do
9772 p = &TREE_CHAIN (*p);
9773 while (*p);
9774 *p = TREE_CHAIN (t);
9775 continue;
9776 }
9777 }
9778 p = &TREE_CHAIN (*p);
9779 }
9780
9781 return attributes;
9782 }
9783
9784 /* Apply any attributes which had to be deferred until instantiation
9785 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9786 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9787
9788 static void
9789 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9790 tree args, tsubst_flags_t complain, tree in_decl)
9791 {
9792 tree last_dep = NULL_TREE;
9793 tree t;
9794 tree *p;
9795
9796 for (t = attributes; t; t = TREE_CHAIN (t))
9797 if (ATTR_IS_DEPENDENT (t))
9798 {
9799 last_dep = t;
9800 attributes = copy_list (attributes);
9801 break;
9802 }
9803
9804 if (DECL_P (*decl_p))
9805 {
9806 if (TREE_TYPE (*decl_p) == error_mark_node)
9807 return;
9808 p = &DECL_ATTRIBUTES (*decl_p);
9809 }
9810 else
9811 p = &TYPE_ATTRIBUTES (*decl_p);
9812
9813 if (last_dep)
9814 {
9815 tree late_attrs = NULL_TREE;
9816 tree *q = &late_attrs;
9817
9818 for (*p = attributes; *p; )
9819 {
9820 t = *p;
9821 if (ATTR_IS_DEPENDENT (t))
9822 {
9823 *p = TREE_CHAIN (t);
9824 TREE_CHAIN (t) = NULL_TREE;
9825 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
9826 do
9827 q = &TREE_CHAIN (*q);
9828 while (*q);
9829 }
9830 else
9831 p = &TREE_CHAIN (t);
9832 }
9833
9834 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9835 }
9836 }
9837
9838 /* Perform (or defer) access check for typedefs that were referenced
9839 from within the template TMPL code.
9840 This is a subroutine of instantiate_decl and instantiate_class_template.
9841 TMPL is the template to consider and TARGS is the list of arguments of
9842 that template. */
9843
9844 static void
9845 perform_typedefs_access_check (tree tmpl, tree targs)
9846 {
9847 location_t saved_location;
9848 unsigned i;
9849 qualified_typedef_usage_t *iter;
9850
9851 if (!tmpl
9852 || (!CLASS_TYPE_P (tmpl)
9853 && TREE_CODE (tmpl) != FUNCTION_DECL))
9854 return;
9855
9856 saved_location = input_location;
9857 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9858 {
9859 tree type_decl = iter->typedef_decl;
9860 tree type_scope = iter->context;
9861
9862 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9863 continue;
9864
9865 if (uses_template_parms (type_decl))
9866 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9867 if (uses_template_parms (type_scope))
9868 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9869
9870 /* Make access check error messages point to the location
9871 of the use of the typedef. */
9872 input_location = iter->locus;
9873 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9874 type_decl, type_decl,
9875 tf_warning_or_error);
9876 }
9877 input_location = saved_location;
9878 }
9879
9880 static tree
9881 instantiate_class_template_1 (tree type)
9882 {
9883 tree templ, args, pattern, t, member;
9884 tree typedecl;
9885 tree pbinfo;
9886 tree base_list;
9887 unsigned int saved_maximum_field_alignment;
9888 tree fn_context;
9889
9890 if (type == error_mark_node)
9891 return error_mark_node;
9892
9893 if (COMPLETE_OR_OPEN_TYPE_P (type)
9894 || uses_template_parms (type))
9895 return type;
9896
9897 /* Figure out which template is being instantiated. */
9898 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9899 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9900
9901 /* Determine what specialization of the original template to
9902 instantiate. */
9903 t = most_specialized_partial_spec (type, tf_warning_or_error);
9904 if (t == error_mark_node)
9905 {
9906 TYPE_BEING_DEFINED (type) = 1;
9907 return error_mark_node;
9908 }
9909 else if (t)
9910 {
9911 /* This TYPE is actually an instantiation of a partial
9912 specialization. We replace the innermost set of ARGS with
9913 the arguments appropriate for substitution. For example,
9914 given:
9915
9916 template <class T> struct S {};
9917 template <class T> struct S<T*> {};
9918
9919 and supposing that we are instantiating S<int*>, ARGS will
9920 presently be {int*} -- but we need {int}. */
9921 pattern = TREE_TYPE (t);
9922 args = TREE_PURPOSE (t);
9923 }
9924 else
9925 {
9926 pattern = TREE_TYPE (templ);
9927 args = CLASSTYPE_TI_ARGS (type);
9928 }
9929
9930 /* If the template we're instantiating is incomplete, then clearly
9931 there's nothing we can do. */
9932 if (!COMPLETE_TYPE_P (pattern))
9933 return type;
9934
9935 /* If we've recursively instantiated too many templates, stop. */
9936 if (! push_tinst_level (type))
9937 return type;
9938
9939 /* Now we're really doing the instantiation. Mark the type as in
9940 the process of being defined. */
9941 TYPE_BEING_DEFINED (type) = 1;
9942
9943 /* We may be in the middle of deferred access check. Disable
9944 it now. */
9945 push_deferring_access_checks (dk_no_deferred);
9946
9947 int saved_unevaluated_operand = cp_unevaluated_operand;
9948 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9949
9950 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9951 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9952 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9953 fn_context = error_mark_node;
9954 if (!fn_context)
9955 push_to_top_level ();
9956 else
9957 {
9958 cp_unevaluated_operand = 0;
9959 c_inhibit_evaluation_warnings = 0;
9960 }
9961 /* Use #pragma pack from the template context. */
9962 saved_maximum_field_alignment = maximum_field_alignment;
9963 maximum_field_alignment = TYPE_PRECISION (pattern);
9964
9965 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9966
9967 /* Set the input location to the most specialized template definition.
9968 This is needed if tsubsting causes an error. */
9969 typedecl = TYPE_MAIN_DECL (pattern);
9970 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9971 DECL_SOURCE_LOCATION (typedecl);
9972
9973 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9974 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
9975 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9976 if (ANON_AGGR_TYPE_P (pattern))
9977 SET_ANON_AGGR_TYPE_P (type);
9978 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9979 {
9980 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9981 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9982 /* Adjust visibility for template arguments. */
9983 determine_visibility (TYPE_MAIN_DECL (type));
9984 }
9985 if (CLASS_TYPE_P (type))
9986 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9987
9988 pbinfo = TYPE_BINFO (pattern);
9989
9990 /* We should never instantiate a nested class before its enclosing
9991 class; we need to look up the nested class by name before we can
9992 instantiate it, and that lookup should instantiate the enclosing
9993 class. */
9994 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9995 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9996
9997 base_list = NULL_TREE;
9998 if (BINFO_N_BASE_BINFOS (pbinfo))
9999 {
10000 tree pbase_binfo;
10001 tree pushed_scope;
10002 int i;
10003
10004 /* We must enter the scope containing the type, as that is where
10005 the accessibility of types named in dependent bases are
10006 looked up from. */
10007 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10008
10009 /* Substitute into each of the bases to determine the actual
10010 basetypes. */
10011 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10012 {
10013 tree base;
10014 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10015 tree expanded_bases = NULL_TREE;
10016 int idx, len = 1;
10017
10018 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10019 {
10020 expanded_bases =
10021 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10022 args, tf_error, NULL_TREE);
10023 if (expanded_bases == error_mark_node)
10024 continue;
10025
10026 len = TREE_VEC_LENGTH (expanded_bases);
10027 }
10028
10029 for (idx = 0; idx < len; idx++)
10030 {
10031 if (expanded_bases)
10032 /* Extract the already-expanded base class. */
10033 base = TREE_VEC_ELT (expanded_bases, idx);
10034 else
10035 /* Substitute to figure out the base class. */
10036 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10037 NULL_TREE);
10038
10039 if (base == error_mark_node)
10040 continue;
10041
10042 base_list = tree_cons (access, base, base_list);
10043 if (BINFO_VIRTUAL_P (pbase_binfo))
10044 TREE_TYPE (base_list) = integer_type_node;
10045 }
10046 }
10047
10048 /* The list is now in reverse order; correct that. */
10049 base_list = nreverse (base_list);
10050
10051 if (pushed_scope)
10052 pop_scope (pushed_scope);
10053 }
10054 /* Now call xref_basetypes to set up all the base-class
10055 information. */
10056 xref_basetypes (type, base_list);
10057
10058 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10059 (int) ATTR_FLAG_TYPE_IN_PLACE,
10060 args, tf_error, NULL_TREE);
10061 fixup_attribute_variants (type);
10062
10063 /* Now that our base classes are set up, enter the scope of the
10064 class, so that name lookups into base classes, etc. will work
10065 correctly. This is precisely analogous to what we do in
10066 begin_class_definition when defining an ordinary non-template
10067 class, except we also need to push the enclosing classes. */
10068 push_nested_class (type);
10069
10070 /* Now members are processed in the order of declaration. */
10071 for (member = CLASSTYPE_DECL_LIST (pattern);
10072 member; member = TREE_CHAIN (member))
10073 {
10074 tree t = TREE_VALUE (member);
10075
10076 if (TREE_PURPOSE (member))
10077 {
10078 if (TYPE_P (t))
10079 {
10080 /* Build new CLASSTYPE_NESTED_UTDS. */
10081
10082 tree newtag;
10083 bool class_template_p;
10084
10085 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10086 && TYPE_LANG_SPECIFIC (t)
10087 && CLASSTYPE_IS_TEMPLATE (t));
10088 /* If the member is a class template, then -- even after
10089 substitution -- there may be dependent types in the
10090 template argument list for the class. We increment
10091 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10092 that function will assume that no types are dependent
10093 when outside of a template. */
10094 if (class_template_p)
10095 ++processing_template_decl;
10096 newtag = tsubst (t, args, tf_error, NULL_TREE);
10097 if (class_template_p)
10098 --processing_template_decl;
10099 if (newtag == error_mark_node)
10100 continue;
10101
10102 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10103 {
10104 tree name = TYPE_IDENTIFIER (t);
10105
10106 if (class_template_p)
10107 /* Unfortunately, lookup_template_class sets
10108 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10109 instantiation (i.e., for the type of a member
10110 template class nested within a template class.)
10111 This behavior is required for
10112 maybe_process_partial_specialization to work
10113 correctly, but is not accurate in this case;
10114 the TAG is not an instantiation of anything.
10115 (The corresponding TEMPLATE_DECL is an
10116 instantiation, but the TYPE is not.) */
10117 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10118
10119 /* Now, we call pushtag to put this NEWTAG into the scope of
10120 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10121 pushtag calling push_template_decl. We don't have to do
10122 this for enums because it will already have been done in
10123 tsubst_enum. */
10124 if (name)
10125 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10126 pushtag (name, newtag, /*tag_scope=*/ts_current);
10127 }
10128 }
10129 else if (DECL_DECLARES_FUNCTION_P (t))
10130 {
10131 /* Build new TYPE_METHODS. */
10132 tree r;
10133
10134 if (TREE_CODE (t) == TEMPLATE_DECL)
10135 ++processing_template_decl;
10136 r = tsubst (t, args, tf_error, NULL_TREE);
10137 if (TREE_CODE (t) == TEMPLATE_DECL)
10138 --processing_template_decl;
10139 set_current_access_from_decl (r);
10140 finish_member_declaration (r);
10141 /* Instantiate members marked with attribute used. */
10142 if (r != error_mark_node && DECL_PRESERVE_P (r))
10143 mark_used (r);
10144 if (TREE_CODE (r) == FUNCTION_DECL
10145 && DECL_OMP_DECLARE_REDUCTION_P (r))
10146 cp_check_omp_declare_reduction (r);
10147 }
10148 else if (DECL_CLASS_TEMPLATE_P (t)
10149 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10150 /* A closure type for a lambda in a default argument for a
10151 member template. Ignore it; it will be instantiated with
10152 the default argument. */;
10153 else
10154 {
10155 /* Build new TYPE_FIELDS. */
10156 if (TREE_CODE (t) == STATIC_ASSERT)
10157 {
10158 tree condition;
10159
10160 ++c_inhibit_evaluation_warnings;
10161 condition =
10162 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10163 tf_warning_or_error, NULL_TREE,
10164 /*integral_constant_expression_p=*/true);
10165 --c_inhibit_evaluation_warnings;
10166
10167 finish_static_assert (condition,
10168 STATIC_ASSERT_MESSAGE (t),
10169 STATIC_ASSERT_SOURCE_LOCATION (t),
10170 /*member_p=*/true);
10171 }
10172 else if (TREE_CODE (t) != CONST_DECL)
10173 {
10174 tree r;
10175 tree vec = NULL_TREE;
10176 int len = 1;
10177
10178 /* The file and line for this declaration, to
10179 assist in error message reporting. Since we
10180 called push_tinst_level above, we don't need to
10181 restore these. */
10182 input_location = DECL_SOURCE_LOCATION (t);
10183
10184 if (TREE_CODE (t) == TEMPLATE_DECL)
10185 ++processing_template_decl;
10186 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10187 if (TREE_CODE (t) == TEMPLATE_DECL)
10188 --processing_template_decl;
10189
10190 if (TREE_CODE (r) == TREE_VEC)
10191 {
10192 /* A capture pack became multiple fields. */
10193 vec = r;
10194 len = TREE_VEC_LENGTH (vec);
10195 }
10196
10197 for (int i = 0; i < len; ++i)
10198 {
10199 if (vec)
10200 r = TREE_VEC_ELT (vec, i);
10201 if (VAR_P (r))
10202 {
10203 /* In [temp.inst]:
10204
10205 [t]he initialization (and any associated
10206 side-effects) of a static data member does
10207 not occur unless the static data member is
10208 itself used in a way that requires the
10209 definition of the static data member to
10210 exist.
10211
10212 Therefore, we do not substitute into the
10213 initialized for the static data member here. */
10214 finish_static_data_member_decl
10215 (r,
10216 /*init=*/NULL_TREE,
10217 /*init_const_expr_p=*/false,
10218 /*asmspec_tree=*/NULL_TREE,
10219 /*flags=*/0);
10220 /* Instantiate members marked with attribute used. */
10221 if (r != error_mark_node && DECL_PRESERVE_P (r))
10222 mark_used (r);
10223 }
10224 else if (TREE_CODE (r) == FIELD_DECL)
10225 {
10226 /* Determine whether R has a valid type and can be
10227 completed later. If R is invalid, then its type
10228 is replaced by error_mark_node. */
10229 tree rtype = TREE_TYPE (r);
10230 if (can_complete_type_without_circularity (rtype))
10231 complete_type (rtype);
10232
10233 if (!complete_or_array_type_p (rtype))
10234 {
10235 /* If R's type couldn't be completed and
10236 it isn't a flexible array member (whose
10237 type is incomplete by definition) give
10238 an error. */
10239 cxx_incomplete_type_error (r, rtype);
10240 TREE_TYPE (r) = error_mark_node;
10241 }
10242 }
10243
10244 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10245 such a thing will already have been added to the field
10246 list by tsubst_enum in finish_member_declaration in the
10247 CLASSTYPE_NESTED_UTDS case above. */
10248 if (!(TREE_CODE (r) == TYPE_DECL
10249 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10250 && DECL_ARTIFICIAL (r)))
10251 {
10252 set_current_access_from_decl (r);
10253 finish_member_declaration (r);
10254 }
10255 }
10256 }
10257 }
10258 }
10259 else
10260 {
10261 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10262 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10263 {
10264 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10265
10266 tree friend_type = t;
10267 bool adjust_processing_template_decl = false;
10268
10269 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10270 {
10271 /* template <class T> friend class C; */
10272 friend_type = tsubst_friend_class (friend_type, args);
10273 adjust_processing_template_decl = true;
10274 }
10275 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10276 {
10277 /* template <class T> friend class C::D; */
10278 friend_type = tsubst (friend_type, args,
10279 tf_warning_or_error, NULL_TREE);
10280 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10281 friend_type = TREE_TYPE (friend_type);
10282 adjust_processing_template_decl = true;
10283 }
10284 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10285 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10286 {
10287 /* This could be either
10288
10289 friend class T::C;
10290
10291 when dependent_type_p is false or
10292
10293 template <class U> friend class T::C;
10294
10295 otherwise. */
10296 /* Bump processing_template_decl in case this is something like
10297 template <class T> friend struct A<T>::B. */
10298 ++processing_template_decl;
10299 friend_type = tsubst (friend_type, args,
10300 tf_warning_or_error, NULL_TREE);
10301 if (dependent_type_p (friend_type))
10302 adjust_processing_template_decl = true;
10303 --processing_template_decl;
10304 }
10305 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10306 && hidden_name_p (TYPE_NAME (friend_type)))
10307 {
10308 /* friend class C;
10309
10310 where C hasn't been declared yet. Let's lookup name
10311 from namespace scope directly, bypassing any name that
10312 come from dependent base class. */
10313 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10314
10315 /* The call to xref_tag_from_type does injection for friend
10316 classes. */
10317 push_nested_namespace (ns);
10318 friend_type =
10319 xref_tag_from_type (friend_type, NULL_TREE,
10320 /*tag_scope=*/ts_current);
10321 pop_nested_namespace (ns);
10322 }
10323 else if (uses_template_parms (friend_type))
10324 /* friend class C<T>; */
10325 friend_type = tsubst (friend_type, args,
10326 tf_warning_or_error, NULL_TREE);
10327 /* Otherwise it's
10328
10329 friend class C;
10330
10331 where C is already declared or
10332
10333 friend class C<int>;
10334
10335 We don't have to do anything in these cases. */
10336
10337 if (adjust_processing_template_decl)
10338 /* Trick make_friend_class into realizing that the friend
10339 we're adding is a template, not an ordinary class. It's
10340 important that we use make_friend_class since it will
10341 perform some error-checking and output cross-reference
10342 information. */
10343 ++processing_template_decl;
10344
10345 if (friend_type != error_mark_node)
10346 make_friend_class (type, friend_type, /*complain=*/false);
10347
10348 if (adjust_processing_template_decl)
10349 --processing_template_decl;
10350 }
10351 else
10352 {
10353 /* Build new DECL_FRIENDLIST. */
10354 tree r;
10355
10356 /* The file and line for this declaration, to
10357 assist in error message reporting. Since we
10358 called push_tinst_level above, we don't need to
10359 restore these. */
10360 input_location = DECL_SOURCE_LOCATION (t);
10361
10362 if (TREE_CODE (t) == TEMPLATE_DECL)
10363 {
10364 ++processing_template_decl;
10365 push_deferring_access_checks (dk_no_check);
10366 }
10367
10368 r = tsubst_friend_function (t, args);
10369 add_friend (type, r, /*complain=*/false);
10370 if (TREE_CODE (t) == TEMPLATE_DECL)
10371 {
10372 pop_deferring_access_checks ();
10373 --processing_template_decl;
10374 }
10375 }
10376 }
10377 }
10378
10379 if (fn_context)
10380 {
10381 /* Restore these before substituting into the lambda capture
10382 initializers. */
10383 cp_unevaluated_operand = saved_unevaluated_operand;
10384 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10385 }
10386
10387 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10388 {
10389 tree decl = lambda_function (type);
10390 if (decl)
10391 {
10392 if (cxx_dialect >= cxx1z)
10393 CLASSTYPE_LITERAL_P (type) = true;
10394
10395 if (!DECL_TEMPLATE_INFO (decl)
10396 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10397 {
10398 /* Set function_depth to avoid garbage collection. */
10399 ++function_depth;
10400 instantiate_decl (decl, false, false);
10401 --function_depth;
10402 }
10403
10404 /* We need to instantiate the capture list from the template
10405 after we've instantiated the closure members, but before we
10406 consider adding the conversion op. Also keep any captures
10407 that may have been added during instantiation of the op(). */
10408 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10409 tree tmpl_cap
10410 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10411 args, tf_warning_or_error, NULL_TREE,
10412 false, false);
10413
10414 LAMBDA_EXPR_CAPTURE_LIST (expr)
10415 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10416
10417 maybe_add_lambda_conv_op (type);
10418 }
10419 else
10420 gcc_assert (errorcount);
10421 }
10422
10423 /* Set the file and line number information to whatever is given for
10424 the class itself. This puts error messages involving generated
10425 implicit functions at a predictable point, and the same point
10426 that would be used for non-template classes. */
10427 input_location = DECL_SOURCE_LOCATION (typedecl);
10428
10429 unreverse_member_declarations (type);
10430 finish_struct_1 (type);
10431 TYPE_BEING_DEFINED (type) = 0;
10432
10433 /* We don't instantiate default arguments for member functions. 14.7.1:
10434
10435 The implicit instantiation of a class template specialization causes
10436 the implicit instantiation of the declarations, but not of the
10437 definitions or default arguments, of the class member functions,
10438 member classes, static data members and member templates.... */
10439
10440 /* Some typedefs referenced from within the template code need to be access
10441 checked at template instantiation time, i.e now. These types were
10442 added to the template at parsing time. Let's get those and perform
10443 the access checks then. */
10444 perform_typedefs_access_check (pattern, args);
10445 perform_deferred_access_checks (tf_warning_or_error);
10446 pop_nested_class ();
10447 maximum_field_alignment = saved_maximum_field_alignment;
10448 if (!fn_context)
10449 pop_from_top_level ();
10450 pop_deferring_access_checks ();
10451 pop_tinst_level ();
10452
10453 /* The vtable for a template class can be emitted in any translation
10454 unit in which the class is instantiated. When there is no key
10455 method, however, finish_struct_1 will already have added TYPE to
10456 the keyed_classes list. */
10457 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10458 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10459
10460 return type;
10461 }
10462
10463 /* Wrapper for instantiate_class_template_1. */
10464
10465 tree
10466 instantiate_class_template (tree type)
10467 {
10468 tree ret;
10469 timevar_push (TV_TEMPLATE_INST);
10470 ret = instantiate_class_template_1 (type);
10471 timevar_pop (TV_TEMPLATE_INST);
10472 return ret;
10473 }
10474
10475 static tree
10476 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10477 {
10478 tree r;
10479
10480 if (!t)
10481 r = t;
10482 else if (TYPE_P (t))
10483 r = tsubst (t, args, complain, in_decl);
10484 else
10485 {
10486 if (!(complain & tf_warning))
10487 ++c_inhibit_evaluation_warnings;
10488 r = tsubst_expr (t, args, complain, in_decl,
10489 /*integral_constant_expression_p=*/true);
10490 if (!(complain & tf_warning))
10491 --c_inhibit_evaluation_warnings;
10492 }
10493 return r;
10494 }
10495
10496 /* Given a function parameter pack TMPL_PARM and some function parameters
10497 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10498 and set *SPEC_P to point at the next point in the list. */
10499
10500 tree
10501 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10502 {
10503 /* Collect all of the extra "packed" parameters into an
10504 argument pack. */
10505 tree parmvec;
10506 tree parmtypevec;
10507 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10508 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10509 tree spec_parm = *spec_p;
10510 int i, len;
10511
10512 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10513 if (tmpl_parm
10514 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10515 break;
10516
10517 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10518 parmvec = make_tree_vec (len);
10519 parmtypevec = make_tree_vec (len);
10520 spec_parm = *spec_p;
10521 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10522 {
10523 TREE_VEC_ELT (parmvec, i) = spec_parm;
10524 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10525 }
10526
10527 /* Build the argument packs. */
10528 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10529 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10530 TREE_TYPE (argpack) = argtypepack;
10531 *spec_p = spec_parm;
10532
10533 return argpack;
10534 }
10535
10536 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10537 NONTYPE_ARGUMENT_PACK. */
10538
10539 static tree
10540 make_fnparm_pack (tree spec_parm)
10541 {
10542 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10543 }
10544
10545 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10546 pack expansion with no extra args, 2 if it has extra args, or 0
10547 if it is not a pack expansion. */
10548
10549 static int
10550 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10551 {
10552 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10553 if (i >= TREE_VEC_LENGTH (vec))
10554 return 0;
10555 tree elt = TREE_VEC_ELT (vec, i);
10556 if (DECL_P (elt))
10557 /* A decl pack is itself an expansion. */
10558 elt = TREE_TYPE (elt);
10559 if (!PACK_EXPANSION_P (elt))
10560 return 0;
10561 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10562 return 2;
10563 return 1;
10564 }
10565
10566
10567 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10568
10569 static tree
10570 make_argument_pack_select (tree arg_pack, unsigned index)
10571 {
10572 tree aps = make_node (ARGUMENT_PACK_SELECT);
10573
10574 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10575 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10576
10577 return aps;
10578 }
10579
10580 /* This is a subroutine of tsubst_pack_expansion.
10581
10582 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10583 mechanism to store the (non complete list of) arguments of the
10584 substitution and return a non substituted pack expansion, in order
10585 to wait for when we have enough arguments to really perform the
10586 substitution. */
10587
10588 static bool
10589 use_pack_expansion_extra_args_p (tree parm_packs,
10590 int arg_pack_len,
10591 bool has_empty_arg)
10592 {
10593 /* If one pack has an expansion and another pack has a normal
10594 argument or if one pack has an empty argument and an another
10595 one hasn't then tsubst_pack_expansion cannot perform the
10596 substitution and need to fall back on the
10597 PACK_EXPANSION_EXTRA mechanism. */
10598 if (parm_packs == NULL_TREE)
10599 return false;
10600 else if (has_empty_arg)
10601 return true;
10602
10603 bool has_expansion_arg = false;
10604 for (int i = 0 ; i < arg_pack_len; ++i)
10605 {
10606 bool has_non_expansion_arg = false;
10607 for (tree parm_pack = parm_packs;
10608 parm_pack;
10609 parm_pack = TREE_CHAIN (parm_pack))
10610 {
10611 tree arg = TREE_VALUE (parm_pack);
10612
10613 int exp = argument_pack_element_is_expansion_p (arg, i);
10614 if (exp == 2)
10615 /* We can't substitute a pack expansion with extra args into
10616 our pattern. */
10617 return true;
10618 else if (exp)
10619 has_expansion_arg = true;
10620 else
10621 has_non_expansion_arg = true;
10622 }
10623
10624 if (has_expansion_arg && has_non_expansion_arg)
10625 return true;
10626 }
10627 return false;
10628 }
10629
10630 /* [temp.variadic]/6 says that:
10631
10632 The instantiation of a pack expansion [...]
10633 produces a list E1,E2, ..., En, where N is the number of elements
10634 in the pack expansion parameters.
10635
10636 This subroutine of tsubst_pack_expansion produces one of these Ei.
10637
10638 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10639 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10640 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10641 INDEX is the index 'i' of the element Ei to produce. ARGS,
10642 COMPLAIN, and IN_DECL are the same parameters as for the
10643 tsubst_pack_expansion function.
10644
10645 The function returns the resulting Ei upon successful completion,
10646 or error_mark_node.
10647
10648 Note that this function possibly modifies the ARGS parameter, so
10649 it's the responsibility of the caller to restore it. */
10650
10651 static tree
10652 gen_elem_of_pack_expansion_instantiation (tree pattern,
10653 tree parm_packs,
10654 unsigned index,
10655 tree args /* This parm gets
10656 modified. */,
10657 tsubst_flags_t complain,
10658 tree in_decl)
10659 {
10660 tree t;
10661 bool ith_elem_is_expansion = false;
10662
10663 /* For each parameter pack, change the substitution of the parameter
10664 pack to the ith argument in its argument pack, then expand the
10665 pattern. */
10666 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10667 {
10668 tree parm = TREE_PURPOSE (pack);
10669 tree arg_pack = TREE_VALUE (pack);
10670 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10671
10672 ith_elem_is_expansion |=
10673 argument_pack_element_is_expansion_p (arg_pack, index);
10674
10675 /* Select the Ith argument from the pack. */
10676 if (TREE_CODE (parm) == PARM_DECL
10677 || TREE_CODE (parm) == FIELD_DECL)
10678 {
10679 if (index == 0)
10680 {
10681 aps = make_argument_pack_select (arg_pack, index);
10682 if (!mark_used (parm, complain) && !(complain & tf_error))
10683 return error_mark_node;
10684 register_local_specialization (aps, parm);
10685 }
10686 else
10687 aps = retrieve_local_specialization (parm);
10688 }
10689 else
10690 {
10691 int idx, level;
10692 template_parm_level_and_index (parm, &level, &idx);
10693
10694 if (index == 0)
10695 {
10696 aps = make_argument_pack_select (arg_pack, index);
10697 /* Update the corresponding argument. */
10698 TMPL_ARG (args, level, idx) = aps;
10699 }
10700 else
10701 /* Re-use the ARGUMENT_PACK_SELECT. */
10702 aps = TMPL_ARG (args, level, idx);
10703 }
10704 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10705 }
10706
10707 /* Substitute into the PATTERN with the (possibly altered)
10708 arguments. */
10709 if (pattern == in_decl)
10710 /* Expanding a fixed parameter pack from
10711 coerce_template_parameter_pack. */
10712 t = tsubst_decl (pattern, args, complain);
10713 else if (pattern == error_mark_node)
10714 t = error_mark_node;
10715 else if (constraint_p (pattern))
10716 {
10717 if (processing_template_decl)
10718 t = tsubst_constraint (pattern, args, complain, in_decl);
10719 else
10720 t = (constraints_satisfied_p (pattern, args)
10721 ? boolean_true_node : boolean_false_node);
10722 }
10723 else if (!TYPE_P (pattern))
10724 t = tsubst_expr (pattern, args, complain, in_decl,
10725 /*integral_constant_expression_p=*/false);
10726 else
10727 t = tsubst (pattern, args, complain, in_decl);
10728
10729 /* If the Ith argument pack element is a pack expansion, then
10730 the Ith element resulting from the substituting is going to
10731 be a pack expansion as well. */
10732 if (ith_elem_is_expansion)
10733 t = make_pack_expansion (t);
10734
10735 return t;
10736 }
10737
10738 /* When the unexpanded parameter pack in a fold expression expands to an empty
10739 sequence, the value of the expression is as follows; the program is
10740 ill-formed if the operator is not listed in this table.
10741
10742 && true
10743 || false
10744 , void() */
10745
10746 tree
10747 expand_empty_fold (tree t, tsubst_flags_t complain)
10748 {
10749 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
10750 if (!FOLD_EXPR_MODIFY_P (t))
10751 switch (code)
10752 {
10753 case TRUTH_ANDIF_EXPR:
10754 return boolean_true_node;
10755 case TRUTH_ORIF_EXPR:
10756 return boolean_false_node;
10757 case COMPOUND_EXPR:
10758 return void_node;
10759 default:
10760 break;
10761 }
10762
10763 if (complain & tf_error)
10764 error_at (location_of (t),
10765 "fold of empty expansion over %O", code);
10766 return error_mark_node;
10767 }
10768
10769 /* Given a fold-expression T and a current LEFT and RIGHT operand,
10770 form an expression that combines the two terms using the
10771 operator of T. */
10772
10773 static tree
10774 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
10775 {
10776 tree op = FOLD_EXPR_OP (t);
10777 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
10778
10779 // Handle compound assignment operators.
10780 if (FOLD_EXPR_MODIFY_P (t))
10781 return build_x_modify_expr (input_location, left, code, right, complain);
10782
10783 switch (code)
10784 {
10785 case COMPOUND_EXPR:
10786 return build_x_compound_expr (input_location, left, right, complain);
10787 case DOTSTAR_EXPR:
10788 return build_m_component_ref (left, right, complain);
10789 default:
10790 return build_x_binary_op (input_location, code,
10791 left, TREE_CODE (left),
10792 right, TREE_CODE (right),
10793 /*overload=*/NULL,
10794 complain);
10795 }
10796 }
10797
10798 /* Substitute ARGS into the pack of a fold expression T. */
10799
10800 static inline tree
10801 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10802 {
10803 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
10804 }
10805
10806 /* Substitute ARGS into the pack of a fold expression T. */
10807
10808 static inline tree
10809 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10810 {
10811 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
10812 }
10813
10814 /* Expand a PACK of arguments into a grouped as left fold.
10815 Given a pack containing elements A0, A1, ..., An and an
10816 operator @, this builds the expression:
10817
10818 ((A0 @ A1) @ A2) ... @ An
10819
10820 Note that PACK must not be empty.
10821
10822 The operator is defined by the original fold expression T. */
10823
10824 static tree
10825 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
10826 {
10827 tree left = TREE_VEC_ELT (pack, 0);
10828 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
10829 {
10830 tree right = TREE_VEC_ELT (pack, i);
10831 left = fold_expression (t, left, right, complain);
10832 }
10833 return left;
10834 }
10835
10836 /* Substitute into a unary left fold expression. */
10837
10838 static tree
10839 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
10840 tree in_decl)
10841 {
10842 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10843 if (pack == error_mark_node)
10844 return error_mark_node;
10845 if (PACK_EXPANSION_P (pack))
10846 {
10847 tree r = copy_node (t);
10848 FOLD_EXPR_PACK (r) = pack;
10849 return r;
10850 }
10851 if (TREE_VEC_LENGTH (pack) == 0)
10852 return expand_empty_fold (t, complain);
10853 else
10854 return expand_left_fold (t, pack, complain);
10855 }
10856
10857 /* Substitute into a binary left fold expression.
10858
10859 Do ths by building a single (non-empty) vector of argumnts and
10860 building the expression from those elements. */
10861
10862 static tree
10863 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
10864 tree in_decl)
10865 {
10866 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10867 if (pack == error_mark_node)
10868 return error_mark_node;
10869 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10870 if (init == error_mark_node)
10871 return error_mark_node;
10872
10873 if (PACK_EXPANSION_P (pack))
10874 {
10875 tree r = copy_node (t);
10876 FOLD_EXPR_PACK (r) = pack;
10877 FOLD_EXPR_INIT (r) = init;
10878 return r;
10879 }
10880
10881 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
10882 TREE_VEC_ELT (vec, 0) = init;
10883 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
10884 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
10885
10886 return expand_left_fold (t, vec, complain);
10887 }
10888
10889 /* Expand a PACK of arguments into a grouped as right fold.
10890 Given a pack containing elementns A0, A1, ..., and an
10891 operator @, this builds the expression:
10892
10893 A0@ ... (An-2 @ (An-1 @ An))
10894
10895 Note that PACK must not be empty.
10896
10897 The operator is defined by the original fold expression T. */
10898
10899 tree
10900 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
10901 {
10902 // Build the expression.
10903 int n = TREE_VEC_LENGTH (pack);
10904 tree right = TREE_VEC_ELT (pack, n - 1);
10905 for (--n; n != 0; --n)
10906 {
10907 tree left = TREE_VEC_ELT (pack, n - 1);
10908 right = fold_expression (t, left, right, complain);
10909 }
10910 return right;
10911 }
10912
10913 /* Substitute into a unary right fold expression. */
10914
10915 static tree
10916 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
10917 tree in_decl)
10918 {
10919 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10920 if (pack == error_mark_node)
10921 return error_mark_node;
10922 if (PACK_EXPANSION_P (pack))
10923 {
10924 tree r = copy_node (t);
10925 FOLD_EXPR_PACK (r) = pack;
10926 return r;
10927 }
10928 if (TREE_VEC_LENGTH (pack) == 0)
10929 return expand_empty_fold (t, complain);
10930 else
10931 return expand_right_fold (t, pack, complain);
10932 }
10933
10934 /* Substitute into a binary right fold expression.
10935
10936 Do ths by building a single (non-empty) vector of arguments and
10937 building the expression from those elements. */
10938
10939 static tree
10940 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
10941 tree in_decl)
10942 {
10943 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
10944 if (pack == error_mark_node)
10945 return error_mark_node;
10946 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
10947 if (init == error_mark_node)
10948 return error_mark_node;
10949
10950 if (PACK_EXPANSION_P (pack))
10951 {
10952 tree r = copy_node (t);
10953 FOLD_EXPR_PACK (r) = pack;
10954 FOLD_EXPR_INIT (r) = init;
10955 return r;
10956 }
10957
10958 int n = TREE_VEC_LENGTH (pack);
10959 tree vec = make_tree_vec (n + 1);
10960 for (int i = 0; i < n; ++i)
10961 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
10962 TREE_VEC_ELT (vec, n) = init;
10963
10964 return expand_right_fold (t, vec, complain);
10965 }
10966
10967
10968 /* Substitute ARGS into T, which is an pack expansion
10969 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10970 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10971 (if only a partial substitution could be performed) or
10972 ERROR_MARK_NODE if there was an error. */
10973 tree
10974 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10975 tree in_decl)
10976 {
10977 tree pattern;
10978 tree pack, packs = NULL_TREE;
10979 bool unsubstituted_packs = false;
10980 int i, len = -1;
10981 tree result;
10982 hash_map<tree, tree> *saved_local_specializations = NULL;
10983 bool need_local_specializations = false;
10984 int levels;
10985
10986 gcc_assert (PACK_EXPANSION_P (t));
10987 pattern = PACK_EXPANSION_PATTERN (t);
10988
10989 /* Add in any args remembered from an earlier partial instantiation. */
10990 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10991
10992 levels = TMPL_ARGS_DEPTH (args);
10993
10994 /* Determine the argument packs that will instantiate the parameter
10995 packs used in the expansion expression. While we're at it,
10996 compute the number of arguments to be expanded and make sure it
10997 is consistent. */
10998 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10999 pack = TREE_CHAIN (pack))
11000 {
11001 tree parm_pack = TREE_VALUE (pack);
11002 tree arg_pack = NULL_TREE;
11003 tree orig_arg = NULL_TREE;
11004 int level = 0;
11005
11006 if (TREE_CODE (parm_pack) == BASES)
11007 {
11008 if (BASES_DIRECT (parm_pack))
11009 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11010 args, complain, in_decl, false));
11011 else
11012 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11013 args, complain, in_decl, false));
11014 }
11015 if (TREE_CODE (parm_pack) == PARM_DECL)
11016 {
11017 /* We know we have correct local_specializations if this
11018 expansion is at function scope, or if we're dealing with a
11019 local parameter in a requires expression; for the latter,
11020 tsubst_requires_expr set it up appropriately. */
11021 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11022 arg_pack = retrieve_local_specialization (parm_pack);
11023 else
11024 /* We can't rely on local_specializations for a parameter
11025 name used later in a function declaration (such as in a
11026 late-specified return type). Even if it exists, it might
11027 have the wrong value for a recursive call. */
11028 need_local_specializations = true;
11029
11030 if (!arg_pack)
11031 {
11032 /* This parameter pack was used in an unevaluated context. Just
11033 make a dummy decl, since it's only used for its type. */
11034 arg_pack = tsubst_decl (parm_pack, args, complain);
11035 if (arg_pack && DECL_PACK_P (arg_pack))
11036 /* Partial instantiation of the parm_pack, we can't build
11037 up an argument pack yet. */
11038 arg_pack = NULL_TREE;
11039 else
11040 arg_pack = make_fnparm_pack (arg_pack);
11041 }
11042 }
11043 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11044 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11045 else
11046 {
11047 int idx;
11048 template_parm_level_and_index (parm_pack, &level, &idx);
11049
11050 if (level <= levels)
11051 arg_pack = TMPL_ARG (args, level, idx);
11052 }
11053
11054 orig_arg = arg_pack;
11055 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11056 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11057
11058 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11059 /* This can only happen if we forget to expand an argument
11060 pack somewhere else. Just return an error, silently. */
11061 {
11062 result = make_tree_vec (1);
11063 TREE_VEC_ELT (result, 0) = error_mark_node;
11064 return result;
11065 }
11066
11067 if (arg_pack)
11068 {
11069 int my_len =
11070 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11071
11072 /* Don't bother trying to do a partial substitution with
11073 incomplete packs; we'll try again after deduction. */
11074 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11075 return t;
11076
11077 if (len < 0)
11078 len = my_len;
11079 else if (len != my_len)
11080 {
11081 if (!(complain & tf_error))
11082 /* Fail quietly. */;
11083 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11084 error ("mismatched argument pack lengths while expanding "
11085 "%<%T%>",
11086 pattern);
11087 else
11088 error ("mismatched argument pack lengths while expanding "
11089 "%<%E%>",
11090 pattern);
11091 return error_mark_node;
11092 }
11093
11094 /* Keep track of the parameter packs and their corresponding
11095 argument packs. */
11096 packs = tree_cons (parm_pack, arg_pack, packs);
11097 TREE_TYPE (packs) = orig_arg;
11098 }
11099 else
11100 {
11101 /* We can't substitute for this parameter pack. We use a flag as
11102 well as the missing_level counter because function parameter
11103 packs don't have a level. */
11104 gcc_assert (processing_template_decl);
11105 unsubstituted_packs = true;
11106 }
11107 }
11108
11109 /* If the expansion is just T..., return the matching argument pack, unless
11110 we need to call convert_from_reference on all the elements. This is an
11111 important optimization; see c++/68422. */
11112 if (!unsubstituted_packs
11113 && TREE_PURPOSE (packs) == pattern)
11114 {
11115 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11116 /* Types need no adjustment, nor does sizeof..., and if we still have
11117 some pack expansion args we won't do anything yet. */
11118 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11119 || PACK_EXPANSION_SIZEOF_P (t)
11120 || pack_expansion_args_count (args))
11121 return args;
11122 /* Also optimize expression pack expansions if we can tell that the
11123 elements won't have reference type. */
11124 tree type = TREE_TYPE (pattern);
11125 if (type && TREE_CODE (type) != REFERENCE_TYPE
11126 && !PACK_EXPANSION_P (type)
11127 && !WILDCARD_TYPE_P (type))
11128 return args;
11129 /* Otherwise use the normal path so we get convert_from_reference. */
11130 }
11131
11132 /* We cannot expand this expansion expression, because we don't have
11133 all of the argument packs we need. */
11134 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11135 {
11136 /* We got some full packs, but we can't substitute them in until we
11137 have values for all the packs. So remember these until then. */
11138
11139 t = make_pack_expansion (pattern);
11140 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11141 return t;
11142 }
11143 else if (unsubstituted_packs)
11144 {
11145 /* There were no real arguments, we're just replacing a parameter
11146 pack with another version of itself. Substitute into the
11147 pattern and return a PACK_EXPANSION_*. The caller will need to
11148 deal with that. */
11149 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11150 t = tsubst_expr (pattern, args, complain, in_decl,
11151 /*integral_constant_expression_p=*/false);
11152 else
11153 t = tsubst (pattern, args, complain, in_decl);
11154 t = make_pack_expansion (t);
11155 return t;
11156 }
11157
11158 gcc_assert (len >= 0);
11159
11160 if (need_local_specializations)
11161 {
11162 /* We're in a late-specified return type, so create our own local
11163 specializations map; the current map is either NULL or (in the
11164 case of recursive unification) might have bindings that we don't
11165 want to use or alter. */
11166 saved_local_specializations = local_specializations;
11167 local_specializations = new hash_map<tree, tree>;
11168 }
11169
11170 /* For each argument in each argument pack, substitute into the
11171 pattern. */
11172 result = make_tree_vec (len);
11173 tree elem_args = copy_template_args (args);
11174 for (i = 0; i < len; ++i)
11175 {
11176 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11177 i,
11178 elem_args, complain,
11179 in_decl);
11180 TREE_VEC_ELT (result, i) = t;
11181 if (t == error_mark_node)
11182 {
11183 result = error_mark_node;
11184 break;
11185 }
11186 }
11187
11188 /* Update ARGS to restore the substitution from parameter packs to
11189 their argument packs. */
11190 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11191 {
11192 tree parm = TREE_PURPOSE (pack);
11193
11194 if (TREE_CODE (parm) == PARM_DECL
11195 || TREE_CODE (parm) == FIELD_DECL)
11196 register_local_specialization (TREE_TYPE (pack), parm);
11197 else
11198 {
11199 int idx, level;
11200
11201 if (TREE_VALUE (pack) == NULL_TREE)
11202 continue;
11203
11204 template_parm_level_and_index (parm, &level, &idx);
11205
11206 /* Update the corresponding argument. */
11207 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11208 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11209 TREE_TYPE (pack);
11210 else
11211 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11212 }
11213 }
11214
11215 if (need_local_specializations)
11216 {
11217 delete local_specializations;
11218 local_specializations = saved_local_specializations;
11219 }
11220
11221 /* If the dependent pack arguments were such that we end up with only a
11222 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11223 if (len == 1 && TREE_CODE (result) == TREE_VEC
11224 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11225 return TREE_VEC_ELT (result, 0);
11226
11227 return result;
11228 }
11229
11230 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11231 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11232 parameter packs; all parms generated from a function parameter pack will
11233 have the same DECL_PARM_INDEX. */
11234
11235 tree
11236 get_pattern_parm (tree parm, tree tmpl)
11237 {
11238 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11239 tree patparm;
11240
11241 if (DECL_ARTIFICIAL (parm))
11242 {
11243 for (patparm = DECL_ARGUMENTS (pattern);
11244 patparm; patparm = DECL_CHAIN (patparm))
11245 if (DECL_ARTIFICIAL (patparm)
11246 && DECL_NAME (parm) == DECL_NAME (patparm))
11247 break;
11248 }
11249 else
11250 {
11251 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11252 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11253 gcc_assert (DECL_PARM_INDEX (patparm)
11254 == DECL_PARM_INDEX (parm));
11255 }
11256
11257 return patparm;
11258 }
11259
11260 /* Make an argument pack out of the TREE_VEC VEC. */
11261
11262 static tree
11263 make_argument_pack (tree vec)
11264 {
11265 tree pack;
11266 tree elt = TREE_VEC_ELT (vec, 0);
11267 if (TYPE_P (elt))
11268 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11269 else
11270 {
11271 pack = make_node (NONTYPE_ARGUMENT_PACK);
11272 TREE_TYPE (pack) = TREE_TYPE (elt);
11273 TREE_CONSTANT (pack) = 1;
11274 }
11275 SET_ARGUMENT_PACK_ARGS (pack, vec);
11276 return pack;
11277 }
11278
11279 /* Return an exact copy of template args T that can be modified
11280 independently. */
11281
11282 static tree
11283 copy_template_args (tree t)
11284 {
11285 if (t == error_mark_node)
11286 return t;
11287
11288 int len = TREE_VEC_LENGTH (t);
11289 tree new_vec = make_tree_vec (len);
11290
11291 for (int i = 0; i < len; ++i)
11292 {
11293 tree elt = TREE_VEC_ELT (t, i);
11294 if (elt && TREE_CODE (elt) == TREE_VEC)
11295 elt = copy_template_args (elt);
11296 TREE_VEC_ELT (new_vec, i) = elt;
11297 }
11298
11299 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11300 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11301
11302 return new_vec;
11303 }
11304
11305 /* Substitute ARGS into the vector or list of template arguments T. */
11306
11307 static tree
11308 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11309 {
11310 tree orig_t = t;
11311 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11312 tree *elts;
11313
11314 if (t == error_mark_node)
11315 return error_mark_node;
11316
11317 len = TREE_VEC_LENGTH (t);
11318 elts = XALLOCAVEC (tree, len);
11319
11320 for (i = 0; i < len; i++)
11321 {
11322 tree orig_arg = TREE_VEC_ELT (t, i);
11323 tree new_arg;
11324
11325 if (TREE_CODE (orig_arg) == TREE_VEC)
11326 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11327 else if (PACK_EXPANSION_P (orig_arg))
11328 {
11329 /* Substitute into an expansion expression. */
11330 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11331
11332 if (TREE_CODE (new_arg) == TREE_VEC)
11333 /* Add to the expanded length adjustment the number of
11334 expanded arguments. We subtract one from this
11335 measurement, because the argument pack expression
11336 itself is already counted as 1 in
11337 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11338 the argument pack is empty. */
11339 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11340 }
11341 else if (ARGUMENT_PACK_P (orig_arg))
11342 {
11343 /* Substitute into each of the arguments. */
11344 new_arg = TYPE_P (orig_arg)
11345 ? cxx_make_type (TREE_CODE (orig_arg))
11346 : make_node (TREE_CODE (orig_arg));
11347
11348 SET_ARGUMENT_PACK_ARGS (
11349 new_arg,
11350 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11351 args, complain, in_decl));
11352
11353 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11354 new_arg = error_mark_node;
11355
11356 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11357 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11358 complain, in_decl);
11359 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11360
11361 if (TREE_TYPE (new_arg) == error_mark_node)
11362 new_arg = error_mark_node;
11363 }
11364 }
11365 else
11366 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11367
11368 if (new_arg == error_mark_node)
11369 return error_mark_node;
11370
11371 elts[i] = new_arg;
11372 if (new_arg != orig_arg)
11373 need_new = 1;
11374 }
11375
11376 if (!need_new)
11377 return t;
11378
11379 /* Make space for the expanded arguments coming from template
11380 argument packs. */
11381 t = make_tree_vec (len + expanded_len_adjust);
11382 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11383 arguments for a member template.
11384 In that case each TREE_VEC in ORIG_T represents a level of template
11385 arguments, and ORIG_T won't carry any non defaulted argument count.
11386 It will rather be the nested TREE_VECs that will carry one.
11387 In other words, ORIG_T carries a non defaulted argument count only
11388 if it doesn't contain any nested TREE_VEC. */
11389 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11390 {
11391 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11392 count += expanded_len_adjust;
11393 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11394 }
11395 for (i = 0, out = 0; i < len; i++)
11396 {
11397 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11398 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11399 && TREE_CODE (elts[i]) == TREE_VEC)
11400 {
11401 int idx;
11402
11403 /* Now expand the template argument pack "in place". */
11404 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11405 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11406 }
11407 else
11408 {
11409 TREE_VEC_ELT (t, out) = elts[i];
11410 out++;
11411 }
11412 }
11413
11414 return t;
11415 }
11416
11417 /* Substitute ARGS into one level PARMS of template parameters. */
11418
11419 static tree
11420 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11421 {
11422 if (parms == error_mark_node)
11423 return error_mark_node;
11424
11425 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11426
11427 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11428 {
11429 tree tuple = TREE_VEC_ELT (parms, i);
11430
11431 if (tuple == error_mark_node)
11432 continue;
11433
11434 TREE_VEC_ELT (new_vec, i) =
11435 tsubst_template_parm (tuple, args, complain);
11436 }
11437
11438 return new_vec;
11439 }
11440
11441 /* Return the result of substituting ARGS into the template parameters
11442 given by PARMS. If there are m levels of ARGS and m + n levels of
11443 PARMS, then the result will contain n levels of PARMS. For
11444 example, if PARMS is `template <class T> template <class U>
11445 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11446 result will be `template <int*, double, class V>'. */
11447
11448 static tree
11449 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11450 {
11451 tree r = NULL_TREE;
11452 tree* new_parms;
11453
11454 /* When substituting into a template, we must set
11455 PROCESSING_TEMPLATE_DECL as the template parameters may be
11456 dependent if they are based on one-another, and the dependency
11457 predicates are short-circuit outside of templates. */
11458 ++processing_template_decl;
11459
11460 for (new_parms = &r;
11461 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11462 new_parms = &(TREE_CHAIN (*new_parms)),
11463 parms = TREE_CHAIN (parms))
11464 {
11465 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11466 args, complain);
11467 *new_parms =
11468 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11469 - TMPL_ARGS_DEPTH (args)),
11470 new_vec, NULL_TREE);
11471 }
11472
11473 --processing_template_decl;
11474
11475 return r;
11476 }
11477
11478 /* Return the result of substituting ARGS into one template parameter
11479 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11480 parameter and which TREE_PURPOSE is the default argument of the
11481 template parameter. */
11482
11483 static tree
11484 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11485 {
11486 tree default_value, parm_decl;
11487
11488 if (args == NULL_TREE
11489 || t == NULL_TREE
11490 || t == error_mark_node)
11491 return t;
11492
11493 gcc_assert (TREE_CODE (t) == TREE_LIST);
11494
11495 default_value = TREE_PURPOSE (t);
11496 parm_decl = TREE_VALUE (t);
11497
11498 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11499 if (TREE_CODE (parm_decl) == PARM_DECL
11500 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11501 parm_decl = error_mark_node;
11502 default_value = tsubst_template_arg (default_value, args,
11503 complain, NULL_TREE);
11504
11505 return build_tree_list (default_value, parm_decl);
11506 }
11507
11508 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11509 type T. If T is not an aggregate or enumeration type, it is
11510 handled as if by tsubst. IN_DECL is as for tsubst. If
11511 ENTERING_SCOPE is nonzero, T is the context for a template which
11512 we are presently tsubst'ing. Return the substituted value. */
11513
11514 static tree
11515 tsubst_aggr_type (tree t,
11516 tree args,
11517 tsubst_flags_t complain,
11518 tree in_decl,
11519 int entering_scope)
11520 {
11521 if (t == NULL_TREE)
11522 return NULL_TREE;
11523
11524 switch (TREE_CODE (t))
11525 {
11526 case RECORD_TYPE:
11527 if (TYPE_PTRMEMFUNC_P (t))
11528 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11529
11530 /* Fall through. */
11531 case ENUMERAL_TYPE:
11532 case UNION_TYPE:
11533 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11534 {
11535 tree argvec;
11536 tree context;
11537 tree r;
11538 int saved_unevaluated_operand;
11539 int saved_inhibit_evaluation_warnings;
11540
11541 /* In "sizeof(X<I>)" we need to evaluate "I". */
11542 saved_unevaluated_operand = cp_unevaluated_operand;
11543 cp_unevaluated_operand = 0;
11544 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11545 c_inhibit_evaluation_warnings = 0;
11546
11547 /* First, determine the context for the type we are looking
11548 up. */
11549 context = TYPE_CONTEXT (t);
11550 if (context && TYPE_P (context))
11551 {
11552 context = tsubst_aggr_type (context, args, complain,
11553 in_decl, /*entering_scope=*/1);
11554 /* If context is a nested class inside a class template,
11555 it may still need to be instantiated (c++/33959). */
11556 context = complete_type (context);
11557 }
11558
11559 /* Then, figure out what arguments are appropriate for the
11560 type we are trying to find. For example, given:
11561
11562 template <class T> struct S;
11563 template <class T, class U> void f(T, U) { S<U> su; }
11564
11565 and supposing that we are instantiating f<int, double>,
11566 then our ARGS will be {int, double}, but, when looking up
11567 S we only want {double}. */
11568 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11569 complain, in_decl);
11570 if (argvec == error_mark_node)
11571 r = error_mark_node;
11572 else
11573 {
11574 r = lookup_template_class (t, argvec, in_decl, context,
11575 entering_scope, complain);
11576 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11577 }
11578
11579 cp_unevaluated_operand = saved_unevaluated_operand;
11580 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11581
11582 return r;
11583 }
11584 else
11585 /* This is not a template type, so there's nothing to do. */
11586 return t;
11587
11588 default:
11589 return tsubst (t, args, complain, in_decl);
11590 }
11591 }
11592
11593 /* Substitute into the default argument ARG (a default argument for
11594 FN), which has the indicated TYPE. */
11595
11596 tree
11597 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11598 {
11599 tree saved_class_ptr = NULL_TREE;
11600 tree saved_class_ref = NULL_TREE;
11601 int errs = errorcount + sorrycount;
11602
11603 /* This can happen in invalid code. */
11604 if (TREE_CODE (arg) == DEFAULT_ARG)
11605 return arg;
11606
11607 /* This default argument came from a template. Instantiate the
11608 default argument here, not in tsubst. In the case of
11609 something like:
11610
11611 template <class T>
11612 struct S {
11613 static T t();
11614 void f(T = t());
11615 };
11616
11617 we must be careful to do name lookup in the scope of S<T>,
11618 rather than in the current class. */
11619 push_access_scope (fn);
11620 /* The "this" pointer is not valid in a default argument. */
11621 if (cfun)
11622 {
11623 saved_class_ptr = current_class_ptr;
11624 cp_function_chain->x_current_class_ptr = NULL_TREE;
11625 saved_class_ref = current_class_ref;
11626 cp_function_chain->x_current_class_ref = NULL_TREE;
11627 }
11628
11629 push_deferring_access_checks(dk_no_deferred);
11630 /* The default argument expression may cause implicitly defined
11631 member functions to be synthesized, which will result in garbage
11632 collection. We must treat this situation as if we were within
11633 the body of function so as to avoid collecting live data on the
11634 stack. */
11635 ++function_depth;
11636 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11637 complain, NULL_TREE,
11638 /*integral_constant_expression_p=*/false);
11639 --function_depth;
11640 pop_deferring_access_checks();
11641
11642 /* Restore the "this" pointer. */
11643 if (cfun)
11644 {
11645 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11646 cp_function_chain->x_current_class_ref = saved_class_ref;
11647 }
11648
11649 if (errorcount+sorrycount > errs
11650 && (complain & tf_warning_or_error))
11651 inform (input_location,
11652 " when instantiating default argument for call to %D", fn);
11653
11654 /* Make sure the default argument is reasonable. */
11655 arg = check_default_argument (type, arg, complain);
11656
11657 pop_access_scope (fn);
11658
11659 return arg;
11660 }
11661
11662 /* Substitute into all the default arguments for FN. */
11663
11664 static void
11665 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11666 {
11667 tree arg;
11668 tree tmpl_args;
11669
11670 tmpl_args = DECL_TI_ARGS (fn);
11671
11672 /* If this function is not yet instantiated, we certainly don't need
11673 its default arguments. */
11674 if (uses_template_parms (tmpl_args))
11675 return;
11676 /* Don't do this again for clones. */
11677 if (DECL_CLONED_FUNCTION_P (fn))
11678 return;
11679
11680 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11681 arg;
11682 arg = TREE_CHAIN (arg))
11683 if (TREE_PURPOSE (arg))
11684 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11685 TREE_VALUE (arg),
11686 TREE_PURPOSE (arg),
11687 complain);
11688 }
11689
11690 /* Substitute the ARGS into the T, which is a _DECL. Return the
11691 result of the substitution. Issue error and warning messages under
11692 control of COMPLAIN. */
11693
11694 static tree
11695 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11696 {
11697 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11698 location_t saved_loc;
11699 tree r = NULL_TREE;
11700 tree in_decl = t;
11701 hashval_t hash = 0;
11702
11703 /* Set the filename and linenumber to improve error-reporting. */
11704 saved_loc = input_location;
11705 input_location = DECL_SOURCE_LOCATION (t);
11706
11707 switch (TREE_CODE (t))
11708 {
11709 case TEMPLATE_DECL:
11710 {
11711 /* We can get here when processing a member function template,
11712 member class template, or template template parameter. */
11713 tree decl = DECL_TEMPLATE_RESULT (t);
11714 tree spec;
11715 tree tmpl_args;
11716 tree full_args;
11717
11718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11719 {
11720 /* Template template parameter is treated here. */
11721 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11722 if (new_type == error_mark_node)
11723 r = error_mark_node;
11724 /* If we get a real template back, return it. This can happen in
11725 the context of most_specialized_partial_spec. */
11726 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11727 r = new_type;
11728 else
11729 /* The new TEMPLATE_DECL was built in
11730 reduce_template_parm_level. */
11731 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
11732 break;
11733 }
11734
11735 /* We might already have an instance of this template.
11736 The ARGS are for the surrounding class type, so the
11737 full args contain the tsubst'd args for the context,
11738 plus the innermost args from the template decl. */
11739 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
11740 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
11741 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
11742 /* Because this is a template, the arguments will still be
11743 dependent, even after substitution. If
11744 PROCESSING_TEMPLATE_DECL is not set, the dependency
11745 predicates will short-circuit. */
11746 ++processing_template_decl;
11747 full_args = tsubst_template_args (tmpl_args, args,
11748 complain, in_decl);
11749 --processing_template_decl;
11750 if (full_args == error_mark_node)
11751 RETURN (error_mark_node);
11752
11753 /* If this is a default template template argument,
11754 tsubst might not have changed anything. */
11755 if (full_args == tmpl_args)
11756 RETURN (t);
11757
11758 hash = hash_tmpl_and_args (t, full_args);
11759 spec = retrieve_specialization (t, full_args, hash);
11760 if (spec != NULL_TREE)
11761 {
11762 r = spec;
11763 break;
11764 }
11765
11766 /* Make a new template decl. It will be similar to the
11767 original, but will record the current template arguments.
11768 We also create a new function declaration, which is just
11769 like the old one, but points to this new template, rather
11770 than the old one. */
11771 r = copy_decl (t);
11772 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
11773 DECL_CHAIN (r) = NULL_TREE;
11774
11775 // Build new template info linking to the original template decl.
11776 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11777
11778 if (TREE_CODE (decl) == TYPE_DECL
11779 && !TYPE_DECL_ALIAS_P (decl))
11780 {
11781 tree new_type;
11782 ++processing_template_decl;
11783 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11784 --processing_template_decl;
11785 if (new_type == error_mark_node)
11786 RETURN (error_mark_node);
11787
11788 TREE_TYPE (r) = new_type;
11789 /* For a partial specialization, we need to keep pointing to
11790 the primary template. */
11791 if (!DECL_TEMPLATE_SPECIALIZATION (t))
11792 CLASSTYPE_TI_TEMPLATE (new_type) = r;
11793 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
11794 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
11795 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
11796 }
11797 else
11798 {
11799 tree new_decl;
11800 ++processing_template_decl;
11801 new_decl = tsubst (decl, args, complain, in_decl);
11802 --processing_template_decl;
11803 if (new_decl == error_mark_node)
11804 RETURN (error_mark_node);
11805
11806 DECL_TEMPLATE_RESULT (r) = new_decl;
11807 DECL_TI_TEMPLATE (new_decl) = r;
11808 TREE_TYPE (r) = TREE_TYPE (new_decl);
11809 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
11810 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
11811 }
11812
11813 SET_DECL_IMPLICIT_INSTANTIATION (r);
11814 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
11815 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
11816
11817 /* The template parameters for this new template are all the
11818 template parameters for the old template, except the
11819 outermost level of parameters. */
11820 DECL_TEMPLATE_PARMS (r)
11821 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
11822 complain);
11823
11824 if (PRIMARY_TEMPLATE_P (t))
11825 DECL_PRIMARY_TEMPLATE (r) = r;
11826
11827 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
11828 /* Record this non-type partial instantiation. */
11829 register_specialization (r, t,
11830 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
11831 false, hash);
11832 }
11833 break;
11834
11835 case FUNCTION_DECL:
11836 {
11837 tree ctx;
11838 tree argvec = NULL_TREE;
11839 tree *friends;
11840 tree gen_tmpl;
11841 tree type;
11842 int member;
11843 int args_depth;
11844 int parms_depth;
11845
11846 /* Nobody should be tsubst'ing into non-template functions. */
11847 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
11848
11849 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
11850 {
11851 tree spec;
11852
11853 /* If T is not dependent, just return it. */
11854 if (!uses_template_parms (DECL_TI_ARGS (t)))
11855 RETURN (t);
11856
11857 /* Calculate the most general template of which R is a
11858 specialization, and the complete set of arguments used to
11859 specialize R. */
11860 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
11861 argvec = tsubst_template_args (DECL_TI_ARGS
11862 (DECL_TEMPLATE_RESULT
11863 (DECL_TI_TEMPLATE (t))),
11864 args, complain, in_decl);
11865 if (argvec == error_mark_node)
11866 RETURN (error_mark_node);
11867
11868 /* Check to see if we already have this specialization. */
11869 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11870 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11871
11872 if (spec)
11873 {
11874 r = spec;
11875 break;
11876 }
11877
11878 /* We can see more levels of arguments than parameters if
11879 there was a specialization of a member template, like
11880 this:
11881
11882 template <class T> struct S { template <class U> void f(); }
11883 template <> template <class U> void S<int>::f(U);
11884
11885 Here, we'll be substituting into the specialization,
11886 because that's where we can find the code we actually
11887 want to generate, but we'll have enough arguments for
11888 the most general template.
11889
11890 We also deal with the peculiar case:
11891
11892 template <class T> struct S {
11893 template <class U> friend void f();
11894 };
11895 template <class U> void f() {}
11896 template S<int>;
11897 template void f<double>();
11898
11899 Here, the ARGS for the instantiation of will be {int,
11900 double}. But, we only need as many ARGS as there are
11901 levels of template parameters in CODE_PATTERN. We are
11902 careful not to get fooled into reducing the ARGS in
11903 situations like:
11904
11905 template <class T> struct S { template <class U> void f(U); }
11906 template <class T> template <> void S<T>::f(int) {}
11907
11908 which we can spot because the pattern will be a
11909 specialization in this case. */
11910 args_depth = TMPL_ARGS_DEPTH (args);
11911 parms_depth =
11912 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
11913 if (args_depth > parms_depth
11914 && !DECL_TEMPLATE_SPECIALIZATION (t))
11915 args = get_innermost_template_args (args, parms_depth);
11916 }
11917 else
11918 {
11919 /* This special case arises when we have something like this:
11920
11921 template <class T> struct S {
11922 friend void f<int>(int, double);
11923 };
11924
11925 Here, the DECL_TI_TEMPLATE for the friend declaration
11926 will be an IDENTIFIER_NODE. We are being called from
11927 tsubst_friend_function, and we want only to create a
11928 new decl (R) with appropriate types so that we can call
11929 determine_specialization. */
11930 gen_tmpl = NULL_TREE;
11931 }
11932
11933 if (DECL_CLASS_SCOPE_P (t))
11934 {
11935 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
11936 member = 2;
11937 else
11938 member = 1;
11939 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
11940 complain, t, /*entering_scope=*/1);
11941 }
11942 else
11943 {
11944 member = 0;
11945 ctx = DECL_CONTEXT (t);
11946 }
11947 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
11948 if (type == error_mark_node)
11949 RETURN (error_mark_node);
11950
11951 /* If we hit excessive deduction depth, the type is bogus even if
11952 it isn't error_mark_node, so don't build a decl. */
11953 if (excessive_deduction_depth)
11954 RETURN (error_mark_node);
11955
11956 /* We do NOT check for matching decls pushed separately at this
11957 point, as they may not represent instantiations of this
11958 template, and in any case are considered separate under the
11959 discrete model. */
11960 r = copy_decl (t);
11961 DECL_USE_TEMPLATE (r) = 0;
11962 TREE_TYPE (r) = type;
11963 /* Clear out the mangled name and RTL for the instantiation. */
11964 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11965 SET_DECL_RTL (r, NULL);
11966 /* Leave DECL_INITIAL set on deleted instantiations. */
11967 if (!DECL_DELETED_FN (r))
11968 DECL_INITIAL (r) = NULL_TREE;
11969 DECL_CONTEXT (r) = ctx;
11970
11971 /* OpenMP UDRs have the only argument a reference to the declared
11972 type. We want to diagnose if the declared type is a reference,
11973 which is invalid, but as references to references are usually
11974 quietly merged, diagnose it here. */
11975 if (DECL_OMP_DECLARE_REDUCTION_P (t))
11976 {
11977 tree argtype
11978 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11979 argtype = tsubst (argtype, args, complain, in_decl);
11980 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11981 error_at (DECL_SOURCE_LOCATION (t),
11982 "reference type %qT in "
11983 "%<#pragma omp declare reduction%>", argtype);
11984 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11985 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11986 argtype);
11987 }
11988
11989 if (member && DECL_CONV_FN_P (r))
11990 /* Type-conversion operator. Reconstruct the name, in
11991 case it's the name of one of the template's parameters. */
11992 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11993
11994 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11995 complain, t);
11996 DECL_RESULT (r) = NULL_TREE;
11997
11998 TREE_STATIC (r) = 0;
11999 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12000 DECL_EXTERNAL (r) = 1;
12001 /* If this is an instantiation of a function with internal
12002 linkage, we already know what object file linkage will be
12003 assigned to the instantiation. */
12004 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12005 DECL_DEFER_OUTPUT (r) = 0;
12006 DECL_CHAIN (r) = NULL_TREE;
12007 DECL_PENDING_INLINE_INFO (r) = 0;
12008 DECL_PENDING_INLINE_P (r) = 0;
12009 DECL_SAVED_TREE (r) = NULL_TREE;
12010 DECL_STRUCT_FUNCTION (r) = NULL;
12011 TREE_USED (r) = 0;
12012 /* We'll re-clone as appropriate in instantiate_template. */
12013 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12014
12015 /* If we aren't complaining now, return on error before we register
12016 the specialization so that we'll complain eventually. */
12017 if ((complain & tf_error) == 0
12018 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12019 && !grok_op_properties (r, /*complain=*/false))
12020 RETURN (error_mark_node);
12021
12022 /* When instantiating a constrained member, substitute
12023 into the constraints to create a new constraint. */
12024 if (tree ci = get_constraints (t))
12025 if (member)
12026 {
12027 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12028 set_constraints (r, ci);
12029 }
12030
12031 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12032 this in the special friend case mentioned above where
12033 GEN_TMPL is NULL. */
12034 if (gen_tmpl)
12035 {
12036 DECL_TEMPLATE_INFO (r)
12037 = build_template_info (gen_tmpl, argvec);
12038 SET_DECL_IMPLICIT_INSTANTIATION (r);
12039
12040 tree new_r
12041 = register_specialization (r, gen_tmpl, argvec, false, hash);
12042 if (new_r != r)
12043 /* We instantiated this while substituting into
12044 the type earlier (template/friend54.C). */
12045 RETURN (new_r);
12046
12047 /* We're not supposed to instantiate default arguments
12048 until they are called, for a template. But, for a
12049 declaration like:
12050
12051 template <class T> void f ()
12052 { extern void g(int i = T()); }
12053
12054 we should do the substitution when the template is
12055 instantiated. We handle the member function case in
12056 instantiate_class_template since the default arguments
12057 might refer to other members of the class. */
12058 if (!member
12059 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12060 && !uses_template_parms (argvec))
12061 tsubst_default_arguments (r, complain);
12062 }
12063 else
12064 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12065
12066 /* Copy the list of befriending classes. */
12067 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12068 *friends;
12069 friends = &TREE_CHAIN (*friends))
12070 {
12071 *friends = copy_node (*friends);
12072 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12073 args, complain,
12074 in_decl);
12075 }
12076
12077 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12078 {
12079 maybe_retrofit_in_chrg (r);
12080 if (DECL_CONSTRUCTOR_P (r))
12081 grok_ctor_properties (ctx, r);
12082 if (DECL_INHERITED_CTOR (r))
12083 deduce_inheriting_ctor (r);
12084 /* If this is an instantiation of a member template, clone it.
12085 If it isn't, that'll be handled by
12086 clone_constructors_and_destructors. */
12087 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12088 clone_function_decl (r, /*update_method_vec_p=*/0);
12089 }
12090 else if ((complain & tf_error) != 0
12091 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12092 && !grok_op_properties (r, /*complain=*/true))
12093 RETURN (error_mark_node);
12094
12095 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12096 SET_DECL_FRIEND_CONTEXT (r,
12097 tsubst (DECL_FRIEND_CONTEXT (t),
12098 args, complain, in_decl));
12099
12100 /* Possibly limit visibility based on template args. */
12101 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12102 if (DECL_VISIBILITY_SPECIFIED (t))
12103 {
12104 DECL_VISIBILITY_SPECIFIED (r) = 0;
12105 DECL_ATTRIBUTES (r)
12106 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12107 }
12108 determine_visibility (r);
12109 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12110 && !processing_template_decl)
12111 defaulted_late_check (r);
12112
12113 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12114 args, complain, in_decl);
12115 }
12116 break;
12117
12118 case PARM_DECL:
12119 {
12120 tree type = NULL_TREE;
12121 int i, len = 1;
12122 tree expanded_types = NULL_TREE;
12123 tree prev_r = NULL_TREE;
12124 tree first_r = NULL_TREE;
12125
12126 if (DECL_PACK_P (t))
12127 {
12128 /* If there is a local specialization that isn't a
12129 parameter pack, it means that we're doing a "simple"
12130 substitution from inside tsubst_pack_expansion. Just
12131 return the local specialization (which will be a single
12132 parm). */
12133 tree spec = retrieve_local_specialization (t);
12134 if (spec
12135 && TREE_CODE (spec) == PARM_DECL
12136 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12137 RETURN (spec);
12138
12139 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12140 the parameters in this function parameter pack. */
12141 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12142 complain, in_decl);
12143 if (TREE_CODE (expanded_types) == TREE_VEC)
12144 {
12145 len = TREE_VEC_LENGTH (expanded_types);
12146
12147 /* Zero-length parameter packs are boring. Just substitute
12148 into the chain. */
12149 if (len == 0)
12150 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12151 TREE_CHAIN (t)));
12152 }
12153 else
12154 {
12155 /* All we did was update the type. Make a note of that. */
12156 type = expanded_types;
12157 expanded_types = NULL_TREE;
12158 }
12159 }
12160
12161 /* Loop through all of the parameters we'll build. When T is
12162 a function parameter pack, LEN is the number of expanded
12163 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12164 r = NULL_TREE;
12165 for (i = 0; i < len; ++i)
12166 {
12167 prev_r = r;
12168 r = copy_node (t);
12169 if (DECL_TEMPLATE_PARM_P (t))
12170 SET_DECL_TEMPLATE_PARM_P (r);
12171
12172 if (expanded_types)
12173 /* We're on the Ith parameter of the function parameter
12174 pack. */
12175 {
12176 /* Get the Ith type. */
12177 type = TREE_VEC_ELT (expanded_types, i);
12178
12179 /* Rename the parameter to include the index. */
12180 DECL_NAME (r)
12181 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12182 }
12183 else if (!type)
12184 /* We're dealing with a normal parameter. */
12185 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12186
12187 type = type_decays_to (type);
12188 TREE_TYPE (r) = type;
12189 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12190
12191 if (DECL_INITIAL (r))
12192 {
12193 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12194 DECL_INITIAL (r) = TREE_TYPE (r);
12195 else
12196 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12197 complain, in_decl);
12198 }
12199
12200 DECL_CONTEXT (r) = NULL_TREE;
12201
12202 if (!DECL_TEMPLATE_PARM_P (r))
12203 DECL_ARG_TYPE (r) = type_passed_as (type);
12204
12205 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12206 args, complain, in_decl);
12207
12208 /* Keep track of the first new parameter we
12209 generate. That's what will be returned to the
12210 caller. */
12211 if (!first_r)
12212 first_r = r;
12213
12214 /* Build a proper chain of parameters when substituting
12215 into a function parameter pack. */
12216 if (prev_r)
12217 DECL_CHAIN (prev_r) = r;
12218 }
12219
12220 /* If cp_unevaluated_operand is set, we're just looking for a
12221 single dummy parameter, so don't keep going. */
12222 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12223 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12224 complain, DECL_CHAIN (t));
12225
12226 /* FIRST_R contains the start of the chain we've built. */
12227 r = first_r;
12228 }
12229 break;
12230
12231 case FIELD_DECL:
12232 {
12233 tree type = NULL_TREE;
12234 tree vec = NULL_TREE;
12235 tree expanded_types = NULL_TREE;
12236 int len = 1;
12237
12238 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12239 {
12240 /* This field is a lambda capture pack. Return a TREE_VEC of
12241 the expanded fields to instantiate_class_template_1 and
12242 store them in the specializations hash table as a
12243 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12244 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12245 complain, in_decl);
12246 if (TREE_CODE (expanded_types) == TREE_VEC)
12247 {
12248 len = TREE_VEC_LENGTH (expanded_types);
12249 vec = make_tree_vec (len);
12250 }
12251 else
12252 {
12253 /* All we did was update the type. Make a note of that. */
12254 type = expanded_types;
12255 expanded_types = NULL_TREE;
12256 }
12257 }
12258
12259 for (int i = 0; i < len; ++i)
12260 {
12261 r = copy_decl (t);
12262 if (expanded_types)
12263 {
12264 type = TREE_VEC_ELT (expanded_types, i);
12265 DECL_NAME (r)
12266 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12267 }
12268 else if (!type)
12269 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12270
12271 if (type == error_mark_node)
12272 RETURN (error_mark_node);
12273 TREE_TYPE (r) = type;
12274 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12275
12276 if (DECL_C_BIT_FIELD (r))
12277 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12278 non-bit-fields DECL_INITIAL is a non-static data member
12279 initializer, which gets deferred instantiation. */
12280 DECL_INITIAL (r)
12281 = tsubst_expr (DECL_INITIAL (t), args,
12282 complain, in_decl,
12283 /*integral_constant_expression_p=*/true);
12284 else if (DECL_INITIAL (t))
12285 {
12286 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12287 NSDMI in perform_member_init. Still set DECL_INITIAL
12288 so that we know there is one. */
12289 DECL_INITIAL (r) = void_node;
12290 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12291 retrofit_lang_decl (r);
12292 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12293 }
12294 /* We don't have to set DECL_CONTEXT here; it is set by
12295 finish_member_declaration. */
12296 DECL_CHAIN (r) = NULL_TREE;
12297
12298 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12299 args, complain, in_decl);
12300
12301 if (vec)
12302 TREE_VEC_ELT (vec, i) = r;
12303 }
12304
12305 if (vec)
12306 {
12307 r = vec;
12308 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12309 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12310 SET_ARGUMENT_PACK_ARGS (pack, vec);
12311 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12312 TREE_TYPE (pack) = tpack;
12313 register_specialization (pack, t, args, false, 0);
12314 }
12315 }
12316 break;
12317
12318 case USING_DECL:
12319 /* We reach here only for member using decls. We also need to check
12320 uses_template_parms because DECL_DEPENDENT_P is not set for a
12321 using-declaration that designates a member of the current
12322 instantiation (c++/53549). */
12323 if (DECL_DEPENDENT_P (t)
12324 || uses_template_parms (USING_DECL_SCOPE (t)))
12325 {
12326 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12327 complain, in_decl);
12328 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12329 r = do_class_using_decl (inst_scope, name);
12330 if (!r)
12331 r = error_mark_node;
12332 else
12333 {
12334 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12335 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12336 }
12337 }
12338 else
12339 {
12340 r = copy_node (t);
12341 DECL_CHAIN (r) = NULL_TREE;
12342 }
12343 break;
12344
12345 case TYPE_DECL:
12346 case VAR_DECL:
12347 {
12348 tree argvec = NULL_TREE;
12349 tree gen_tmpl = NULL_TREE;
12350 tree spec;
12351 tree tmpl = NULL_TREE;
12352 tree ctx;
12353 tree type = NULL_TREE;
12354 bool local_p;
12355
12356 if (TREE_TYPE (t) == error_mark_node)
12357 RETURN (error_mark_node);
12358
12359 if (TREE_CODE (t) == TYPE_DECL
12360 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12361 {
12362 /* If this is the canonical decl, we don't have to
12363 mess with instantiations, and often we can't (for
12364 typename, template type parms and such). Note that
12365 TYPE_NAME is not correct for the above test if
12366 we've copied the type for a typedef. */
12367 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12368 if (type == error_mark_node)
12369 RETURN (error_mark_node);
12370 r = TYPE_NAME (type);
12371 break;
12372 }
12373
12374 /* Check to see if we already have the specialization we
12375 need. */
12376 spec = NULL_TREE;
12377 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12378 {
12379 /* T is a static data member or namespace-scope entity.
12380 We have to substitute into namespace-scope variables
12381 (not just variable templates) because of cases like:
12382
12383 template <class T> void f() { extern T t; }
12384
12385 where the entity referenced is not known until
12386 instantiation time. */
12387 local_p = false;
12388 ctx = DECL_CONTEXT (t);
12389 if (DECL_CLASS_SCOPE_P (t))
12390 {
12391 ctx = tsubst_aggr_type (ctx, args,
12392 complain,
12393 in_decl, /*entering_scope=*/1);
12394 /* If CTX is unchanged, then T is in fact the
12395 specialization we want. That situation occurs when
12396 referencing a static data member within in its own
12397 class. We can use pointer equality, rather than
12398 same_type_p, because DECL_CONTEXT is always
12399 canonical... */
12400 if (ctx == DECL_CONTEXT (t)
12401 /* ... unless T is a member template; in which
12402 case our caller can be willing to create a
12403 specialization of that template represented
12404 by T. */
12405 && !(DECL_TI_TEMPLATE (t)
12406 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12407 spec = t;
12408 }
12409
12410 if (!spec)
12411 {
12412 tmpl = DECL_TI_TEMPLATE (t);
12413 gen_tmpl = most_general_template (tmpl);
12414 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12415 if (argvec != error_mark_node)
12416 argvec = (coerce_innermost_template_parms
12417 (DECL_TEMPLATE_PARMS (gen_tmpl),
12418 argvec, t, complain,
12419 /*all*/true, /*defarg*/true));
12420 if (argvec == error_mark_node)
12421 RETURN (error_mark_node);
12422 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12423 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12424 }
12425 }
12426 else
12427 {
12428 /* A local variable. */
12429 local_p = true;
12430 /* Subsequent calls to pushdecl will fill this in. */
12431 ctx = NULL_TREE;
12432 /* Unless this is a reference to a static variable from an
12433 enclosing function, in which case we need to fill it in now. */
12434 if (TREE_STATIC (t))
12435 {
12436 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12437 if (fn != current_function_decl)
12438 ctx = fn;
12439 }
12440 spec = retrieve_local_specialization (t);
12441 }
12442 /* If we already have the specialization we need, there is
12443 nothing more to do. */
12444 if (spec)
12445 {
12446 r = spec;
12447 break;
12448 }
12449
12450 /* Create a new node for the specialization we need. */
12451 r = copy_decl (t);
12452 if (type == NULL_TREE)
12453 {
12454 if (is_typedef_decl (t))
12455 type = DECL_ORIGINAL_TYPE (t);
12456 else
12457 type = TREE_TYPE (t);
12458 if (VAR_P (t)
12459 && VAR_HAD_UNKNOWN_BOUND (t)
12460 && type != error_mark_node)
12461 type = strip_array_domain (type);
12462 type = tsubst (type, args, complain, in_decl);
12463 }
12464 if (VAR_P (r))
12465 {
12466 /* Even if the original location is out of scope, the
12467 newly substituted one is not. */
12468 DECL_DEAD_FOR_LOCAL (r) = 0;
12469 DECL_INITIALIZED_P (r) = 0;
12470 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12471 if (type == error_mark_node)
12472 RETURN (error_mark_node);
12473 if (TREE_CODE (type) == FUNCTION_TYPE)
12474 {
12475 /* It may seem that this case cannot occur, since:
12476
12477 typedef void f();
12478 void g() { f x; }
12479
12480 declares a function, not a variable. However:
12481
12482 typedef void f();
12483 template <typename T> void g() { T t; }
12484 template void g<f>();
12485
12486 is an attempt to declare a variable with function
12487 type. */
12488 error ("variable %qD has function type",
12489 /* R is not yet sufficiently initialized, so we
12490 just use its name. */
12491 DECL_NAME (r));
12492 RETURN (error_mark_node);
12493 }
12494 type = complete_type (type);
12495 /* Wait until cp_finish_decl to set this again, to handle
12496 circular dependency (template/instantiate6.C). */
12497 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12498 type = check_var_type (DECL_NAME (r), type);
12499
12500 if (DECL_HAS_VALUE_EXPR_P (t))
12501 {
12502 tree ve = DECL_VALUE_EXPR (t);
12503 ve = tsubst_expr (ve, args, complain, in_decl,
12504 /*constant_expression_p=*/false);
12505 if (REFERENCE_REF_P (ve))
12506 {
12507 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12508 ve = TREE_OPERAND (ve, 0);
12509 }
12510 SET_DECL_VALUE_EXPR (r, ve);
12511 }
12512 if (CP_DECL_THREAD_LOCAL_P (r)
12513 && !processing_template_decl)
12514 set_decl_tls_model (r, decl_default_tls_model (r));
12515 }
12516 else if (DECL_SELF_REFERENCE_P (t))
12517 SET_DECL_SELF_REFERENCE_P (r);
12518 TREE_TYPE (r) = type;
12519 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12520 DECL_CONTEXT (r) = ctx;
12521 /* Clear out the mangled name and RTL for the instantiation. */
12522 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12523 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12524 SET_DECL_RTL (r, NULL);
12525 /* The initializer must not be expanded until it is required;
12526 see [temp.inst]. */
12527 DECL_INITIAL (r) = NULL_TREE;
12528 if (VAR_P (r))
12529 DECL_MODE (r) = VOIDmode;
12530 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12531 SET_DECL_RTL (r, NULL);
12532 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12533 if (VAR_P (r))
12534 {
12535 /* Possibly limit visibility based on template args. */
12536 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12537 if (DECL_VISIBILITY_SPECIFIED (t))
12538 {
12539 DECL_VISIBILITY_SPECIFIED (r) = 0;
12540 DECL_ATTRIBUTES (r)
12541 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12542 }
12543 determine_visibility (r);
12544 }
12545
12546 if (!local_p)
12547 {
12548 /* A static data member declaration is always marked
12549 external when it is declared in-class, even if an
12550 initializer is present. We mimic the non-template
12551 processing here. */
12552 DECL_EXTERNAL (r) = 1;
12553 if (DECL_NAMESPACE_SCOPE_P (t))
12554 DECL_NOT_REALLY_EXTERN (r) = 1;
12555
12556 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12557 SET_DECL_IMPLICIT_INSTANTIATION (r);
12558 register_specialization (r, gen_tmpl, argvec, false, hash);
12559 }
12560 else
12561 {
12562 if (DECL_LANG_SPECIFIC (r))
12563 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12564 if (!cp_unevaluated_operand)
12565 register_local_specialization (r, t);
12566 }
12567
12568 DECL_CHAIN (r) = NULL_TREE;
12569
12570 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12571 /*flags=*/0,
12572 args, complain, in_decl);
12573
12574 /* Preserve a typedef that names a type. */
12575 if (is_typedef_decl (r))
12576 {
12577 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12578 set_underlying_type (r);
12579 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
12580 /* An alias template specialization can be dependent
12581 even if its underlying type is not. */
12582 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12583 }
12584
12585 layout_decl (r, 0);
12586 }
12587 break;
12588
12589 default:
12590 gcc_unreachable ();
12591 }
12592 #undef RETURN
12593
12594 out:
12595 /* Restore the file and line information. */
12596 input_location = saved_loc;
12597
12598 return r;
12599 }
12600
12601 /* Substitute into the ARG_TYPES of a function type.
12602 If END is a TREE_CHAIN, leave it and any following types
12603 un-substituted. */
12604
12605 static tree
12606 tsubst_arg_types (tree arg_types,
12607 tree args,
12608 tree end,
12609 tsubst_flags_t complain,
12610 tree in_decl)
12611 {
12612 tree remaining_arg_types;
12613 tree type = NULL_TREE;
12614 int i = 1;
12615 tree expanded_args = NULL_TREE;
12616 tree default_arg;
12617
12618 if (!arg_types || arg_types == void_list_node || arg_types == end)
12619 return arg_types;
12620
12621 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12622 args, end, complain, in_decl);
12623 if (remaining_arg_types == error_mark_node)
12624 return error_mark_node;
12625
12626 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12627 {
12628 /* For a pack expansion, perform substitution on the
12629 entire expression. Later on, we'll handle the arguments
12630 one-by-one. */
12631 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12632 args, complain, in_decl);
12633
12634 if (TREE_CODE (expanded_args) == TREE_VEC)
12635 /* So that we'll spin through the parameters, one by one. */
12636 i = TREE_VEC_LENGTH (expanded_args);
12637 else
12638 {
12639 /* We only partially substituted into the parameter
12640 pack. Our type is TYPE_PACK_EXPANSION. */
12641 type = expanded_args;
12642 expanded_args = NULL_TREE;
12643 }
12644 }
12645
12646 while (i > 0) {
12647 --i;
12648
12649 if (expanded_args)
12650 type = TREE_VEC_ELT (expanded_args, i);
12651 else if (!type)
12652 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12653
12654 if (type == error_mark_node)
12655 return error_mark_node;
12656 if (VOID_TYPE_P (type))
12657 {
12658 if (complain & tf_error)
12659 {
12660 error ("invalid parameter type %qT", type);
12661 if (in_decl)
12662 error ("in declaration %q+D", in_decl);
12663 }
12664 return error_mark_node;
12665 }
12666 /* DR 657. */
12667 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12668 return error_mark_node;
12669
12670 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12671 top-level qualifiers as required. */
12672 type = cv_unqualified (type_decays_to (type));
12673
12674 /* We do not substitute into default arguments here. The standard
12675 mandates that they be instantiated only when needed, which is
12676 done in build_over_call. */
12677 default_arg = TREE_PURPOSE (arg_types);
12678
12679 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12680 {
12681 /* We've instantiated a template before its default arguments
12682 have been parsed. This can happen for a nested template
12683 class, and is not an error unless we require the default
12684 argument in a call of this function. */
12685 remaining_arg_types =
12686 tree_cons (default_arg, type, remaining_arg_types);
12687 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12688 }
12689 else
12690 remaining_arg_types =
12691 hash_tree_cons (default_arg, type, remaining_arg_types);
12692 }
12693
12694 return remaining_arg_types;
12695 }
12696
12697 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12698 *not* handle the exception-specification for FNTYPE, because the
12699 initial substitution of explicitly provided template parameters
12700 during argument deduction forbids substitution into the
12701 exception-specification:
12702
12703 [temp.deduct]
12704
12705 All references in the function type of the function template to the
12706 corresponding template parameters are replaced by the specified tem-
12707 plate argument values. If a substitution in a template parameter or
12708 in the function type of the function template results in an invalid
12709 type, type deduction fails. [Note: The equivalent substitution in
12710 exception specifications is done only when the function is instanti-
12711 ated, at which point a program is ill-formed if the substitution
12712 results in an invalid type.] */
12713
12714 static tree
12715 tsubst_function_type (tree t,
12716 tree args,
12717 tsubst_flags_t complain,
12718 tree in_decl)
12719 {
12720 tree return_type;
12721 tree arg_types = NULL_TREE;
12722 tree fntype;
12723
12724 /* The TYPE_CONTEXT is not used for function/method types. */
12725 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
12726
12727 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
12728 failure. */
12729 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
12730
12731 if (late_return_type_p)
12732 {
12733 /* Substitute the argument types. */
12734 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12735 complain, in_decl);
12736 if (arg_types == error_mark_node)
12737 return error_mark_node;
12738
12739 tree save_ccp = current_class_ptr;
12740 tree save_ccr = current_class_ref;
12741 tree this_type = (TREE_CODE (t) == METHOD_TYPE
12742 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
12743 bool do_inject = this_type && CLASS_TYPE_P (this_type);
12744 if (do_inject)
12745 {
12746 /* DR 1207: 'this' is in scope in the trailing return type. */
12747 inject_this_parameter (this_type, cp_type_quals (this_type));
12748 }
12749
12750 /* Substitute the return type. */
12751 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12752
12753 if (do_inject)
12754 {
12755 current_class_ptr = save_ccp;
12756 current_class_ref = save_ccr;
12757 }
12758 }
12759 else
12760 /* Substitute the return type. */
12761 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12762
12763 if (return_type == error_mark_node)
12764 return error_mark_node;
12765 /* DR 486 clarifies that creation of a function type with an
12766 invalid return type is a deduction failure. */
12767 if (TREE_CODE (return_type) == ARRAY_TYPE
12768 || TREE_CODE (return_type) == FUNCTION_TYPE)
12769 {
12770 if (complain & tf_error)
12771 {
12772 if (TREE_CODE (return_type) == ARRAY_TYPE)
12773 error ("function returning an array");
12774 else
12775 error ("function returning a function");
12776 }
12777 return error_mark_node;
12778 }
12779 /* And DR 657. */
12780 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
12781 return error_mark_node;
12782
12783 if (!late_return_type_p)
12784 {
12785 /* Substitute the argument types. */
12786 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
12787 complain, in_decl);
12788 if (arg_types == error_mark_node)
12789 return error_mark_node;
12790 }
12791
12792 /* Construct a new type node and return it. */
12793 if (TREE_CODE (t) == FUNCTION_TYPE)
12794 {
12795 fntype = build_function_type (return_type, arg_types);
12796 fntype = apply_memfn_quals (fntype,
12797 type_memfn_quals (t),
12798 type_memfn_rqual (t));
12799 }
12800 else
12801 {
12802 tree r = TREE_TYPE (TREE_VALUE (arg_types));
12803 /* Don't pick up extra function qualifiers from the basetype. */
12804 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
12805 if (! MAYBE_CLASS_TYPE_P (r))
12806 {
12807 /* [temp.deduct]
12808
12809 Type deduction may fail for any of the following
12810 reasons:
12811
12812 -- Attempting to create "pointer to member of T" when T
12813 is not a class type. */
12814 if (complain & tf_error)
12815 error ("creating pointer to member function of non-class type %qT",
12816 r);
12817 return error_mark_node;
12818 }
12819
12820 fntype = build_method_type_directly (r, return_type,
12821 TREE_CHAIN (arg_types));
12822 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
12823 }
12824 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
12825
12826 if (late_return_type_p)
12827 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
12828
12829 return fntype;
12830 }
12831
12832 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
12833 ARGS into that specification, and return the substituted
12834 specification. If there is no specification, return NULL_TREE. */
12835
12836 static tree
12837 tsubst_exception_specification (tree fntype,
12838 tree args,
12839 tsubst_flags_t complain,
12840 tree in_decl,
12841 bool defer_ok)
12842 {
12843 tree specs;
12844 tree new_specs;
12845
12846 specs = TYPE_RAISES_EXCEPTIONS (fntype);
12847 new_specs = NULL_TREE;
12848 if (specs && TREE_PURPOSE (specs))
12849 {
12850 /* A noexcept-specifier. */
12851 tree expr = TREE_PURPOSE (specs);
12852 if (TREE_CODE (expr) == INTEGER_CST)
12853 new_specs = expr;
12854 else if (defer_ok)
12855 {
12856 /* Defer instantiation of noexcept-specifiers to avoid
12857 excessive instantiations (c++/49107). */
12858 new_specs = make_node (DEFERRED_NOEXCEPT);
12859 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
12860 {
12861 /* We already partially instantiated this member template,
12862 so combine the new args with the old. */
12863 DEFERRED_NOEXCEPT_PATTERN (new_specs)
12864 = DEFERRED_NOEXCEPT_PATTERN (expr);
12865 DEFERRED_NOEXCEPT_ARGS (new_specs)
12866 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
12867 }
12868 else
12869 {
12870 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
12871 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
12872 }
12873 }
12874 else
12875 new_specs = tsubst_copy_and_build
12876 (expr, args, complain, in_decl, /*function_p=*/false,
12877 /*integral_constant_expression_p=*/true);
12878 new_specs = build_noexcept_spec (new_specs, complain);
12879 }
12880 else if (specs)
12881 {
12882 if (! TREE_VALUE (specs))
12883 new_specs = specs;
12884 else
12885 while (specs)
12886 {
12887 tree spec;
12888 int i, len = 1;
12889 tree expanded_specs = NULL_TREE;
12890
12891 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
12892 {
12893 /* Expand the pack expansion type. */
12894 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
12895 args, complain,
12896 in_decl);
12897
12898 if (expanded_specs == error_mark_node)
12899 return error_mark_node;
12900 else if (TREE_CODE (expanded_specs) == TREE_VEC)
12901 len = TREE_VEC_LENGTH (expanded_specs);
12902 else
12903 {
12904 /* We're substituting into a member template, so
12905 we got a TYPE_PACK_EXPANSION back. Add that
12906 expansion and move on. */
12907 gcc_assert (TREE_CODE (expanded_specs)
12908 == TYPE_PACK_EXPANSION);
12909 new_specs = add_exception_specifier (new_specs,
12910 expanded_specs,
12911 complain);
12912 specs = TREE_CHAIN (specs);
12913 continue;
12914 }
12915 }
12916
12917 for (i = 0; i < len; ++i)
12918 {
12919 if (expanded_specs)
12920 spec = TREE_VEC_ELT (expanded_specs, i);
12921 else
12922 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
12923 if (spec == error_mark_node)
12924 return spec;
12925 new_specs = add_exception_specifier (new_specs, spec,
12926 complain);
12927 }
12928
12929 specs = TREE_CHAIN (specs);
12930 }
12931 }
12932 return new_specs;
12933 }
12934
12935 /* Take the tree structure T and replace template parameters used
12936 therein with the argument vector ARGS. IN_DECL is an associated
12937 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
12938 Issue error and warning messages under control of COMPLAIN. Note
12939 that we must be relatively non-tolerant of extensions here, in
12940 order to preserve conformance; if we allow substitutions that
12941 should not be allowed, we may allow argument deductions that should
12942 not succeed, and therefore report ambiguous overload situations
12943 where there are none. In theory, we could allow the substitution,
12944 but indicate that it should have failed, and allow our caller to
12945 make sure that the right thing happens, but we don't try to do this
12946 yet.
12947
12948 This function is used for dealing with types, decls and the like;
12949 for expressions, use tsubst_expr or tsubst_copy. */
12950
12951 tree
12952 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12953 {
12954 enum tree_code code;
12955 tree type, r = NULL_TREE;
12956
12957 if (t == NULL_TREE || t == error_mark_node
12958 || t == integer_type_node
12959 || t == void_type_node
12960 || t == char_type_node
12961 || t == unknown_type_node
12962 || TREE_CODE (t) == NAMESPACE_DECL
12963 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
12964 return t;
12965
12966 if (DECL_P (t))
12967 return tsubst_decl (t, args, complain);
12968
12969 if (args == NULL_TREE)
12970 return t;
12971
12972 code = TREE_CODE (t);
12973
12974 if (code == IDENTIFIER_NODE)
12975 type = IDENTIFIER_TYPE_VALUE (t);
12976 else
12977 type = TREE_TYPE (t);
12978
12979 gcc_assert (type != unknown_type_node);
12980
12981 /* Reuse typedefs. We need to do this to handle dependent attributes,
12982 such as attribute aligned. */
12983 if (TYPE_P (t)
12984 && typedef_variant_p (t))
12985 {
12986 tree decl = TYPE_NAME (t);
12987
12988 if (alias_template_specialization_p (t))
12989 {
12990 /* DECL represents an alias template and we want to
12991 instantiate it. */
12992 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12993 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12994 r = instantiate_alias_template (tmpl, gen_args, complain);
12995 }
12996 else if (DECL_CLASS_SCOPE_P (decl)
12997 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
12998 && uses_template_parms (DECL_CONTEXT (decl)))
12999 {
13000 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13001 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13002 r = retrieve_specialization (tmpl, gen_args, 0);
13003 }
13004 else if (DECL_FUNCTION_SCOPE_P (decl)
13005 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13006 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13007 r = retrieve_local_specialization (decl);
13008 else
13009 /* The typedef is from a non-template context. */
13010 return t;
13011
13012 if (r)
13013 {
13014 r = TREE_TYPE (r);
13015 r = cp_build_qualified_type_real
13016 (r, cp_type_quals (t) | cp_type_quals (r),
13017 complain | tf_ignore_bad_quals);
13018 return r;
13019 }
13020 else
13021 {
13022 /* We don't have an instantiation yet, so drop the typedef. */
13023 int quals = cp_type_quals (t);
13024 t = DECL_ORIGINAL_TYPE (decl);
13025 t = cp_build_qualified_type_real (t, quals,
13026 complain | tf_ignore_bad_quals);
13027 }
13028 }
13029
13030 bool fndecl_type = (complain & tf_fndecl_type);
13031 complain &= ~tf_fndecl_type;
13032
13033 if (type
13034 && code != TYPENAME_TYPE
13035 && code != TEMPLATE_TYPE_PARM
13036 && code != IDENTIFIER_NODE
13037 && code != FUNCTION_TYPE
13038 && code != METHOD_TYPE)
13039 type = tsubst (type, args, complain, in_decl);
13040 if (type == error_mark_node)
13041 return error_mark_node;
13042
13043 switch (code)
13044 {
13045 case RECORD_TYPE:
13046 case UNION_TYPE:
13047 case ENUMERAL_TYPE:
13048 return tsubst_aggr_type (t, args, complain, in_decl,
13049 /*entering_scope=*/0);
13050
13051 case ERROR_MARK:
13052 case IDENTIFIER_NODE:
13053 case VOID_TYPE:
13054 case REAL_TYPE:
13055 case COMPLEX_TYPE:
13056 case VECTOR_TYPE:
13057 case BOOLEAN_TYPE:
13058 case NULLPTR_TYPE:
13059 case LANG_TYPE:
13060 return t;
13061
13062 case INTEGER_TYPE:
13063 if (t == integer_type_node)
13064 return t;
13065
13066 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13067 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13068 return t;
13069
13070 {
13071 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13072
13073 max = tsubst_expr (omax, args, complain, in_decl,
13074 /*integral_constant_expression_p=*/false);
13075
13076 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13077 needed. */
13078 if (TREE_CODE (max) == NOP_EXPR
13079 && TREE_SIDE_EFFECTS (omax)
13080 && !TREE_TYPE (max))
13081 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13082
13083 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13084 with TREE_SIDE_EFFECTS that indicates this is not an integral
13085 constant expression. */
13086 if (processing_template_decl
13087 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13088 {
13089 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13090 TREE_SIDE_EFFECTS (max) = 1;
13091 }
13092
13093 return compute_array_index_type (NULL_TREE, max, complain);
13094 }
13095
13096 case TEMPLATE_TYPE_PARM:
13097 case TEMPLATE_TEMPLATE_PARM:
13098 case BOUND_TEMPLATE_TEMPLATE_PARM:
13099 case TEMPLATE_PARM_INDEX:
13100 {
13101 int idx;
13102 int level;
13103 int levels;
13104 tree arg = NULL_TREE;
13105
13106 /* Early in template argument deduction substitution, we don't
13107 want to reduce the level of 'auto', or it will be confused
13108 with a normal template parm in subsequent deduction. */
13109 if (is_auto (t) && (complain & tf_partial))
13110 return t;
13111
13112 r = NULL_TREE;
13113
13114 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13115 template_parm_level_and_index (t, &level, &idx);
13116
13117 levels = TMPL_ARGS_DEPTH (args);
13118 if (level <= levels
13119 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13120 {
13121 arg = TMPL_ARG (args, level, idx);
13122
13123 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13124 {
13125 /* See through ARGUMENT_PACK_SELECT arguments. */
13126 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13127 /* If the selected argument is an expansion E, that most
13128 likely means we were called from
13129 gen_elem_of_pack_expansion_instantiation during the
13130 substituting of pack an argument pack (which Ith
13131 element is a pack expansion, where I is
13132 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13133 In this case, the Ith element resulting from this
13134 substituting is going to be a pack expansion, which
13135 pattern is the pattern of E. Let's return the
13136 pattern of E, and
13137 gen_elem_of_pack_expansion_instantiation will
13138 build the resulting pack expansion from it. */
13139 if (PACK_EXPANSION_P (arg))
13140 {
13141 /* Make sure we aren't throwing away arg info. */
13142 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13143 arg = PACK_EXPANSION_PATTERN (arg);
13144 }
13145 }
13146 }
13147
13148 if (arg == error_mark_node)
13149 return error_mark_node;
13150 else if (arg != NULL_TREE)
13151 {
13152 if (ARGUMENT_PACK_P (arg))
13153 /* If ARG is an argument pack, we don't actually want to
13154 perform a substitution here, because substitutions
13155 for argument packs are only done
13156 element-by-element. We can get to this point when
13157 substituting the type of a non-type template
13158 parameter pack, when that type actually contains
13159 template parameter packs from an outer template, e.g.,
13160
13161 template<typename... Types> struct A {
13162 template<Types... Values> struct B { };
13163 }; */
13164 return t;
13165
13166 if (code == TEMPLATE_TYPE_PARM)
13167 {
13168 int quals;
13169 gcc_assert (TYPE_P (arg));
13170
13171 quals = cp_type_quals (arg) | cp_type_quals (t);
13172
13173 return cp_build_qualified_type_real
13174 (arg, quals, complain | tf_ignore_bad_quals);
13175 }
13176 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13177 {
13178 /* We are processing a type constructed from a
13179 template template parameter. */
13180 tree argvec = tsubst (TYPE_TI_ARGS (t),
13181 args, complain, in_decl);
13182 if (argvec == error_mark_node)
13183 return error_mark_node;
13184
13185 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13186 || TREE_CODE (arg) == TEMPLATE_DECL
13187 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13188
13189 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13190 /* Consider this code:
13191
13192 template <template <class> class Template>
13193 struct Internal {
13194 template <class Arg> using Bind = Template<Arg>;
13195 };
13196
13197 template <template <class> class Template, class Arg>
13198 using Instantiate = Template<Arg>; //#0
13199
13200 template <template <class> class Template,
13201 class Argument>
13202 using Bind =
13203 Instantiate<Internal<Template>::template Bind,
13204 Argument>; //#1
13205
13206 When #1 is parsed, the
13207 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13208 parameter `Template' in #0 matches the
13209 UNBOUND_CLASS_TEMPLATE representing the argument
13210 `Internal<Template>::template Bind'; We then want
13211 to assemble the type `Bind<Argument>' that can't
13212 be fully created right now, because
13213 `Internal<Template>' not being complete, the Bind
13214 template cannot be looked up in that context. So
13215 we need to "store" `Bind<Argument>' for later
13216 when the context of Bind becomes complete. Let's
13217 store that in a TYPENAME_TYPE. */
13218 return make_typename_type (TYPE_CONTEXT (arg),
13219 build_nt (TEMPLATE_ID_EXPR,
13220 TYPE_IDENTIFIER (arg),
13221 argvec),
13222 typename_type,
13223 complain);
13224
13225 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13226 are resolving nested-types in the signature of a
13227 member function templates. Otherwise ARG is a
13228 TEMPLATE_DECL and is the real template to be
13229 instantiated. */
13230 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13231 arg = TYPE_NAME (arg);
13232
13233 r = lookup_template_class (arg,
13234 argvec, in_decl,
13235 DECL_CONTEXT (arg),
13236 /*entering_scope=*/0,
13237 complain);
13238 return cp_build_qualified_type_real
13239 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13240 }
13241 else if (code == TEMPLATE_TEMPLATE_PARM)
13242 return arg;
13243 else
13244 /* TEMPLATE_PARM_INDEX. */
13245 return convert_from_reference (unshare_expr (arg));
13246 }
13247
13248 if (level == 1)
13249 /* This can happen during the attempted tsubst'ing in
13250 unify. This means that we don't yet have any information
13251 about the template parameter in question. */
13252 return t;
13253
13254 /* If we get here, we must have been looking at a parm for a
13255 more deeply nested template. Make a new version of this
13256 template parameter, but with a lower level. */
13257 switch (code)
13258 {
13259 case TEMPLATE_TYPE_PARM:
13260 case TEMPLATE_TEMPLATE_PARM:
13261 case BOUND_TEMPLATE_TEMPLATE_PARM:
13262 if (cp_type_quals (t))
13263 {
13264 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13265 r = cp_build_qualified_type_real
13266 (r, cp_type_quals (t),
13267 complain | (code == TEMPLATE_TYPE_PARM
13268 ? tf_ignore_bad_quals : 0));
13269 }
13270 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13271 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13272 && (r = (TEMPLATE_PARM_DESCENDANTS
13273 (TEMPLATE_TYPE_PARM_INDEX (t))))
13274 && (r = TREE_TYPE (r))
13275 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13276 /* Break infinite recursion when substituting the constraints
13277 of a constrained placeholder. */;
13278 else
13279 {
13280 r = copy_type (t);
13281 TEMPLATE_TYPE_PARM_INDEX (r)
13282 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13283 r, levels, args, complain);
13284 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13285 TYPE_MAIN_VARIANT (r) = r;
13286 TYPE_POINTER_TO (r) = NULL_TREE;
13287 TYPE_REFERENCE_TO (r) = NULL_TREE;
13288
13289 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13290 {
13291 /* Propagate constraints on placeholders. */
13292 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13293 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13294 = tsubst_constraint (constr, args, complain, in_decl);
13295 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13296 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13297 }
13298
13299 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13300 /* We have reduced the level of the template
13301 template parameter, but not the levels of its
13302 template parameters, so canonical_type_parameter
13303 will not be able to find the canonical template
13304 template parameter for this level. Thus, we
13305 require structural equality checking to compare
13306 TEMPLATE_TEMPLATE_PARMs. */
13307 SET_TYPE_STRUCTURAL_EQUALITY (r);
13308 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13309 SET_TYPE_STRUCTURAL_EQUALITY (r);
13310 else
13311 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13312
13313 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13314 {
13315 tree tinfo = TYPE_TEMPLATE_INFO (t);
13316 /* We might need to substitute into the types of non-type
13317 template parameters. */
13318 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13319 complain, in_decl);
13320 if (tmpl == error_mark_node)
13321 return error_mark_node;
13322 tree argvec = tsubst (TI_ARGS (tinfo), args,
13323 complain, in_decl);
13324 if (argvec == error_mark_node)
13325 return error_mark_node;
13326
13327 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13328 = build_template_info (tmpl, argvec);
13329 }
13330 }
13331 break;
13332
13333 case TEMPLATE_PARM_INDEX:
13334 r = reduce_template_parm_level (t, type, levels, args, complain);
13335 break;
13336
13337 default:
13338 gcc_unreachable ();
13339 }
13340
13341 return r;
13342 }
13343
13344 case TREE_LIST:
13345 {
13346 tree purpose, value, chain;
13347
13348 if (t == void_list_node)
13349 return t;
13350
13351 purpose = TREE_PURPOSE (t);
13352 if (purpose)
13353 {
13354 purpose = tsubst (purpose, args, complain, in_decl);
13355 if (purpose == error_mark_node)
13356 return error_mark_node;
13357 }
13358 value = TREE_VALUE (t);
13359 if (value)
13360 {
13361 value = tsubst (value, args, complain, in_decl);
13362 if (value == error_mark_node)
13363 return error_mark_node;
13364 }
13365 chain = TREE_CHAIN (t);
13366 if (chain && chain != void_type_node)
13367 {
13368 chain = tsubst (chain, args, complain, in_decl);
13369 if (chain == error_mark_node)
13370 return error_mark_node;
13371 }
13372 if (purpose == TREE_PURPOSE (t)
13373 && value == TREE_VALUE (t)
13374 && chain == TREE_CHAIN (t))
13375 return t;
13376 return hash_tree_cons (purpose, value, chain);
13377 }
13378
13379 case TREE_BINFO:
13380 /* We should never be tsubsting a binfo. */
13381 gcc_unreachable ();
13382
13383 case TREE_VEC:
13384 /* A vector of template arguments. */
13385 gcc_assert (!type);
13386 return tsubst_template_args (t, args, complain, in_decl);
13387
13388 case POINTER_TYPE:
13389 case REFERENCE_TYPE:
13390 {
13391 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13392 return t;
13393
13394 /* [temp.deduct]
13395
13396 Type deduction may fail for any of the following
13397 reasons:
13398
13399 -- Attempting to create a pointer to reference type.
13400 -- Attempting to create a reference to a reference type or
13401 a reference to void.
13402
13403 Core issue 106 says that creating a reference to a reference
13404 during instantiation is no longer a cause for failure. We
13405 only enforce this check in strict C++98 mode. */
13406 if ((TREE_CODE (type) == REFERENCE_TYPE
13407 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13408 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13409 {
13410 static location_t last_loc;
13411
13412 /* We keep track of the last time we issued this error
13413 message to avoid spewing a ton of messages during a
13414 single bad template instantiation. */
13415 if (complain & tf_error
13416 && last_loc != input_location)
13417 {
13418 if (VOID_TYPE_P (type))
13419 error ("forming reference to void");
13420 else if (code == POINTER_TYPE)
13421 error ("forming pointer to reference type %qT", type);
13422 else
13423 error ("forming reference to reference type %qT", type);
13424 last_loc = input_location;
13425 }
13426
13427 return error_mark_node;
13428 }
13429 else if (TREE_CODE (type) == FUNCTION_TYPE
13430 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13431 || type_memfn_rqual (type) != REF_QUAL_NONE))
13432 {
13433 if (complain & tf_error)
13434 {
13435 if (code == POINTER_TYPE)
13436 error ("forming pointer to qualified function type %qT",
13437 type);
13438 else
13439 error ("forming reference to qualified function type %qT",
13440 type);
13441 }
13442 return error_mark_node;
13443 }
13444 else if (code == POINTER_TYPE)
13445 {
13446 r = build_pointer_type (type);
13447 if (TREE_CODE (type) == METHOD_TYPE)
13448 r = build_ptrmemfunc_type (r);
13449 }
13450 else if (TREE_CODE (type) == REFERENCE_TYPE)
13451 /* In C++0x, during template argument substitution, when there is an
13452 attempt to create a reference to a reference type, reference
13453 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13454
13455 "If a template-argument for a template-parameter T names a type
13456 that is a reference to a type A, an attempt to create the type
13457 'lvalue reference to cv T' creates the type 'lvalue reference to
13458 A,' while an attempt to create the type type rvalue reference to
13459 cv T' creates the type T"
13460 */
13461 r = cp_build_reference_type
13462 (TREE_TYPE (type),
13463 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13464 else
13465 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13466 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13467
13468 if (r != error_mark_node)
13469 /* Will this ever be needed for TYPE_..._TO values? */
13470 layout_type (r);
13471
13472 return r;
13473 }
13474 case OFFSET_TYPE:
13475 {
13476 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13477 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13478 {
13479 /* [temp.deduct]
13480
13481 Type deduction may fail for any of the following
13482 reasons:
13483
13484 -- Attempting to create "pointer to member of T" when T
13485 is not a class type. */
13486 if (complain & tf_error)
13487 error ("creating pointer to member of non-class type %qT", r);
13488 return error_mark_node;
13489 }
13490 if (TREE_CODE (type) == REFERENCE_TYPE)
13491 {
13492 if (complain & tf_error)
13493 error ("creating pointer to member reference type %qT", type);
13494 return error_mark_node;
13495 }
13496 if (VOID_TYPE_P (type))
13497 {
13498 if (complain & tf_error)
13499 error ("creating pointer to member of type void");
13500 return error_mark_node;
13501 }
13502 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13503 if (TREE_CODE (type) == FUNCTION_TYPE)
13504 {
13505 /* The type of the implicit object parameter gets its
13506 cv-qualifiers from the FUNCTION_TYPE. */
13507 tree memptr;
13508 tree method_type
13509 = build_memfn_type (type, r, type_memfn_quals (type),
13510 type_memfn_rqual (type));
13511 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13512 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13513 complain);
13514 }
13515 else
13516 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13517 cp_type_quals (t),
13518 complain);
13519 }
13520 case FUNCTION_TYPE:
13521 case METHOD_TYPE:
13522 {
13523 tree fntype;
13524 tree specs;
13525 fntype = tsubst_function_type (t, args, complain, in_decl);
13526 if (fntype == error_mark_node)
13527 return error_mark_node;
13528
13529 /* Substitute the exception specification. */
13530 specs = tsubst_exception_specification (t, args, complain, in_decl,
13531 /*defer_ok*/fndecl_type);
13532 if (specs == error_mark_node)
13533 return error_mark_node;
13534 if (specs)
13535 fntype = build_exception_variant (fntype, specs);
13536 return fntype;
13537 }
13538 case ARRAY_TYPE:
13539 {
13540 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13541 if (domain == error_mark_node)
13542 return error_mark_node;
13543
13544 /* As an optimization, we avoid regenerating the array type if
13545 it will obviously be the same as T. */
13546 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13547 return t;
13548
13549 /* These checks should match the ones in create_array_type_for_decl.
13550
13551 [temp.deduct]
13552
13553 The deduction may fail for any of the following reasons:
13554
13555 -- Attempting to create an array with an element type that
13556 is void, a function type, or a reference type, or [DR337]
13557 an abstract class type. */
13558 if (VOID_TYPE_P (type)
13559 || TREE_CODE (type) == FUNCTION_TYPE
13560 || (TREE_CODE (type) == ARRAY_TYPE
13561 && TYPE_DOMAIN (type) == NULL_TREE)
13562 || TREE_CODE (type) == REFERENCE_TYPE)
13563 {
13564 if (complain & tf_error)
13565 error ("creating array of %qT", type);
13566 return error_mark_node;
13567 }
13568
13569 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13570 return error_mark_node;
13571
13572 r = build_cplus_array_type (type, domain);
13573
13574 if (TYPE_USER_ALIGN (t))
13575 {
13576 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13577 TYPE_USER_ALIGN (r) = 1;
13578 }
13579
13580 return r;
13581 }
13582
13583 case TYPENAME_TYPE:
13584 {
13585 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13586 in_decl, /*entering_scope=*/1);
13587 if (ctx == error_mark_node)
13588 return error_mark_node;
13589
13590 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13591 complain, in_decl);
13592 if (f == error_mark_node)
13593 return error_mark_node;
13594
13595 if (!MAYBE_CLASS_TYPE_P (ctx))
13596 {
13597 if (complain & tf_error)
13598 error ("%qT is not a class, struct, or union type", ctx);
13599 return error_mark_node;
13600 }
13601 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13602 {
13603 /* Normally, make_typename_type does not require that the CTX
13604 have complete type in order to allow things like:
13605
13606 template <class T> struct S { typename S<T>::X Y; };
13607
13608 But, such constructs have already been resolved by this
13609 point, so here CTX really should have complete type, unless
13610 it's a partial instantiation. */
13611 ctx = complete_type (ctx);
13612 if (!COMPLETE_TYPE_P (ctx))
13613 {
13614 if (complain & tf_error)
13615 cxx_incomplete_type_error (NULL_TREE, ctx);
13616 return error_mark_node;
13617 }
13618 }
13619
13620 f = make_typename_type (ctx, f, typename_type,
13621 complain | tf_keep_type_decl);
13622 if (f == error_mark_node)
13623 return f;
13624 if (TREE_CODE (f) == TYPE_DECL)
13625 {
13626 complain |= tf_ignore_bad_quals;
13627 f = TREE_TYPE (f);
13628 }
13629
13630 if (TREE_CODE (f) != TYPENAME_TYPE)
13631 {
13632 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13633 {
13634 if (complain & tf_error)
13635 error ("%qT resolves to %qT, which is not an enumeration type",
13636 t, f);
13637 else
13638 return error_mark_node;
13639 }
13640 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13641 {
13642 if (complain & tf_error)
13643 error ("%qT resolves to %qT, which is is not a class type",
13644 t, f);
13645 else
13646 return error_mark_node;
13647 }
13648 }
13649
13650 return cp_build_qualified_type_real
13651 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13652 }
13653
13654 case UNBOUND_CLASS_TEMPLATE:
13655 {
13656 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13657 in_decl, /*entering_scope=*/1);
13658 tree name = TYPE_IDENTIFIER (t);
13659 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13660
13661 if (ctx == error_mark_node || name == error_mark_node)
13662 return error_mark_node;
13663
13664 if (parm_list)
13665 parm_list = tsubst_template_parms (parm_list, args, complain);
13666 return make_unbound_class_template (ctx, name, parm_list, complain);
13667 }
13668
13669 case TYPEOF_TYPE:
13670 {
13671 tree type;
13672
13673 ++cp_unevaluated_operand;
13674 ++c_inhibit_evaluation_warnings;
13675
13676 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13677 complain, in_decl,
13678 /*integral_constant_expression_p=*/false);
13679
13680 --cp_unevaluated_operand;
13681 --c_inhibit_evaluation_warnings;
13682
13683 type = finish_typeof (type);
13684 return cp_build_qualified_type_real (type,
13685 cp_type_quals (t)
13686 | cp_type_quals (type),
13687 complain);
13688 }
13689
13690 case DECLTYPE_TYPE:
13691 {
13692 tree type;
13693
13694 ++cp_unevaluated_operand;
13695 ++c_inhibit_evaluation_warnings;
13696
13697 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13698 complain|tf_decltype, in_decl,
13699 /*function_p*/false,
13700 /*integral_constant_expression*/false);
13701
13702 if (DECLTYPE_FOR_INIT_CAPTURE (t))
13703 {
13704 if (type == NULL_TREE)
13705 {
13706 if (complain & tf_error)
13707 error ("empty initializer in lambda init-capture");
13708 type = error_mark_node;
13709 }
13710 else if (TREE_CODE (type) == TREE_LIST)
13711 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
13712 }
13713
13714 --cp_unevaluated_operand;
13715 --c_inhibit_evaluation_warnings;
13716
13717 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
13718 type = lambda_capture_field_type (type,
13719 DECLTYPE_FOR_INIT_CAPTURE (t));
13720 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
13721 type = lambda_proxy_type (type);
13722 else
13723 {
13724 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
13725 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
13726 && EXPR_P (type))
13727 /* In a template ~id could be either a complement expression
13728 or an unqualified-id naming a destructor; if instantiating
13729 it produces an expression, it's not an id-expression or
13730 member access. */
13731 id = false;
13732 type = finish_decltype_type (type, id, complain);
13733 }
13734 return cp_build_qualified_type_real (type,
13735 cp_type_quals (t)
13736 | cp_type_quals (type),
13737 complain | tf_ignore_bad_quals);
13738 }
13739
13740 case UNDERLYING_TYPE:
13741 {
13742 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
13743 complain, in_decl);
13744 return finish_underlying_type (type);
13745 }
13746
13747 case TYPE_ARGUMENT_PACK:
13748 case NONTYPE_ARGUMENT_PACK:
13749 {
13750 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
13751 tree packed_out =
13752 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
13753 args,
13754 complain,
13755 in_decl);
13756 SET_ARGUMENT_PACK_ARGS (r, packed_out);
13757
13758 /* For template nontype argument packs, also substitute into
13759 the type. */
13760 if (code == NONTYPE_ARGUMENT_PACK)
13761 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
13762
13763 return r;
13764 }
13765 break;
13766
13767 case VOID_CST:
13768 case INTEGER_CST:
13769 case REAL_CST:
13770 case STRING_CST:
13771 case PLUS_EXPR:
13772 case MINUS_EXPR:
13773 case NEGATE_EXPR:
13774 case NOP_EXPR:
13775 case INDIRECT_REF:
13776 case ADDR_EXPR:
13777 case CALL_EXPR:
13778 case ARRAY_REF:
13779 case SCOPE_REF:
13780 /* We should use one of the expression tsubsts for these codes. */
13781 gcc_unreachable ();
13782
13783 default:
13784 sorry ("use of %qs in template", get_tree_code_name (code));
13785 return error_mark_node;
13786 }
13787 }
13788
13789 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
13790 type of the expression on the left-hand side of the "." or "->"
13791 operator. */
13792
13793 static tree
13794 tsubst_baselink (tree baselink, tree object_type,
13795 tree args, tsubst_flags_t complain, tree in_decl)
13796 {
13797 tree name;
13798 tree qualifying_scope;
13799 tree fns;
13800 tree optype;
13801 tree template_args = 0;
13802 bool template_id_p = false;
13803 bool qualified = BASELINK_QUALIFIED_P (baselink);
13804
13805 /* A baselink indicates a function from a base class. Both the
13806 BASELINK_ACCESS_BINFO and the base class referenced may
13807 indicate bases of the template class, rather than the
13808 instantiated class. In addition, lookups that were not
13809 ambiguous before may be ambiguous now. Therefore, we perform
13810 the lookup again. */
13811 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
13812 qualifying_scope = tsubst (qualifying_scope, args,
13813 complain, in_decl);
13814 fns = BASELINK_FUNCTIONS (baselink);
13815 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
13816 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
13817 {
13818 template_id_p = true;
13819 template_args = TREE_OPERAND (fns, 1);
13820 fns = TREE_OPERAND (fns, 0);
13821 if (template_args)
13822 template_args = tsubst_template_args (template_args, args,
13823 complain, in_decl);
13824 }
13825 name = DECL_NAME (get_first_fn (fns));
13826 if (IDENTIFIER_TYPENAME_P (name))
13827 name = mangle_conv_op_name_for_type (optype);
13828 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
13829 if (!baselink)
13830 {
13831 if (constructor_name_p (name, qualifying_scope))
13832 {
13833 if (complain & tf_error)
13834 error ("cannot call constructor %<%T::%D%> directly",
13835 qualifying_scope, name);
13836 }
13837 return error_mark_node;
13838 }
13839
13840 /* If lookup found a single function, mark it as used at this
13841 point. (If it lookup found multiple functions the one selected
13842 later by overload resolution will be marked as used at that
13843 point.) */
13844 if (BASELINK_P (baselink))
13845 fns = BASELINK_FUNCTIONS (baselink);
13846 if (!template_id_p && !really_overloaded_fn (fns)
13847 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
13848 return error_mark_node;
13849
13850 /* Add back the template arguments, if present. */
13851 if (BASELINK_P (baselink) && template_id_p)
13852 BASELINK_FUNCTIONS (baselink)
13853 = build2 (TEMPLATE_ID_EXPR,
13854 unknown_type_node,
13855 BASELINK_FUNCTIONS (baselink),
13856 template_args);
13857 /* Update the conversion operator type. */
13858 if (BASELINK_P (baselink))
13859 BASELINK_OPTYPE (baselink) = optype;
13860
13861 if (!object_type)
13862 object_type = current_class_type;
13863
13864 if (qualified || name == complete_dtor_identifier)
13865 {
13866 baselink = adjust_result_of_qualified_name_lookup (baselink,
13867 qualifying_scope,
13868 object_type);
13869 if (!qualified)
13870 /* We need to call adjust_result_of_qualified_name_lookup in case the
13871 destructor names a base class, but we unset BASELINK_QUALIFIED_P
13872 so that we still get virtual function binding. */
13873 BASELINK_QUALIFIED_P (baselink) = false;
13874 }
13875 return baselink;
13876 }
13877
13878 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
13879 true if the qualified-id will be a postfix-expression in-and-of
13880 itself; false if more of the postfix-expression follows the
13881 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
13882 of "&". */
13883
13884 static tree
13885 tsubst_qualified_id (tree qualified_id, tree args,
13886 tsubst_flags_t complain, tree in_decl,
13887 bool done, bool address_p)
13888 {
13889 tree expr;
13890 tree scope;
13891 tree name;
13892 bool is_template;
13893 tree template_args;
13894 location_t loc = UNKNOWN_LOCATION;
13895
13896 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
13897
13898 /* Figure out what name to look up. */
13899 name = TREE_OPERAND (qualified_id, 1);
13900 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13901 {
13902 is_template = true;
13903 loc = EXPR_LOCATION (name);
13904 template_args = TREE_OPERAND (name, 1);
13905 if (template_args)
13906 template_args = tsubst_template_args (template_args, args,
13907 complain, in_decl);
13908 if (template_args == error_mark_node)
13909 return error_mark_node;
13910 name = TREE_OPERAND (name, 0);
13911 }
13912 else
13913 {
13914 is_template = false;
13915 template_args = NULL_TREE;
13916 }
13917
13918 /* Substitute into the qualifying scope. When there are no ARGS, we
13919 are just trying to simplify a non-dependent expression. In that
13920 case the qualifying scope may be dependent, and, in any case,
13921 substituting will not help. */
13922 scope = TREE_OPERAND (qualified_id, 0);
13923 if (args)
13924 {
13925 scope = tsubst (scope, args, complain, in_decl);
13926 expr = tsubst_copy (name, args, complain, in_decl);
13927 }
13928 else
13929 expr = name;
13930
13931 if (dependent_scope_p (scope))
13932 {
13933 if (is_template)
13934 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
13935 tree r = build_qualified_name (NULL_TREE, scope, expr,
13936 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
13937 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
13938 return r;
13939 }
13940
13941 if (!BASELINK_P (name) && !DECL_P (expr))
13942 {
13943 if (TREE_CODE (expr) == BIT_NOT_EXPR)
13944 {
13945 /* A BIT_NOT_EXPR is used to represent a destructor. */
13946 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
13947 {
13948 error ("qualifying type %qT does not match destructor name ~%qT",
13949 scope, TREE_OPERAND (expr, 0));
13950 expr = error_mark_node;
13951 }
13952 else
13953 expr = lookup_qualified_name (scope, complete_dtor_identifier,
13954 /*is_type_p=*/0, false);
13955 }
13956 else
13957 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
13958 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
13959 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
13960 {
13961 if (complain & tf_error)
13962 {
13963 error ("dependent-name %qE is parsed as a non-type, but "
13964 "instantiation yields a type", qualified_id);
13965 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
13966 }
13967 return error_mark_node;
13968 }
13969 }
13970
13971 if (DECL_P (expr))
13972 {
13973 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
13974 scope);
13975 /* Remember that there was a reference to this entity. */
13976 if (!mark_used (expr, complain) && !(complain & tf_error))
13977 return error_mark_node;
13978 }
13979
13980 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
13981 {
13982 if (complain & tf_error)
13983 qualified_name_lookup_error (scope,
13984 TREE_OPERAND (qualified_id, 1),
13985 expr, input_location);
13986 return error_mark_node;
13987 }
13988
13989 if (is_template)
13990 {
13991 if (variable_template_p (expr))
13992 expr = lookup_and_finish_template_variable (expr, template_args,
13993 complain);
13994 else
13995 expr = lookup_template_function (expr, template_args);
13996 }
13997
13998 if (expr == error_mark_node && complain & tf_error)
13999 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14000 expr, input_location);
14001 else if (TYPE_P (scope))
14002 {
14003 expr = (adjust_result_of_qualified_name_lookup
14004 (expr, scope, current_nonlambda_class_type ()));
14005 expr = (finish_qualified_id_expr
14006 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14007 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14008 /*template_arg_p=*/false, complain));
14009 }
14010
14011 /* Expressions do not generally have reference type. */
14012 if (TREE_CODE (expr) != SCOPE_REF
14013 /* However, if we're about to form a pointer-to-member, we just
14014 want the referenced member referenced. */
14015 && TREE_CODE (expr) != OFFSET_REF)
14016 expr = convert_from_reference (expr);
14017
14018 if (REF_PARENTHESIZED_P (qualified_id))
14019 expr = force_paren_expr (expr);
14020
14021 return expr;
14022 }
14023
14024 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14025 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14026 for tsubst. */
14027
14028 static tree
14029 tsubst_init (tree init, tree decl, tree args,
14030 tsubst_flags_t complain, tree in_decl)
14031 {
14032 if (!init)
14033 return NULL_TREE;
14034
14035 init = tsubst_expr (init, args, complain, in_decl, false);
14036
14037 if (!init)
14038 {
14039 /* If we had an initializer but it
14040 instantiated to nothing,
14041 value-initialize the object. This will
14042 only occur when the initializer was a
14043 pack expansion where the parameter packs
14044 used in that expansion were of length
14045 zero. */
14046 init = build_value_init (TREE_TYPE (decl),
14047 complain);
14048 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14049 init = get_target_expr_sfinae (init, complain);
14050 }
14051
14052 return init;
14053 }
14054
14055 /* Like tsubst, but deals with expressions. This function just replaces
14056 template parms; to finish processing the resultant expression, use
14057 tsubst_copy_and_build or tsubst_expr. */
14058
14059 static tree
14060 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14061 {
14062 enum tree_code code;
14063 tree r;
14064
14065 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14066 return t;
14067
14068 code = TREE_CODE (t);
14069
14070 switch (code)
14071 {
14072 case PARM_DECL:
14073 r = retrieve_local_specialization (t);
14074
14075 if (r == NULL_TREE)
14076 {
14077 /* We get here for a use of 'this' in an NSDMI as part of a
14078 constructor call or as part of an aggregate initialization. */
14079 if (DECL_NAME (t) == this_identifier
14080 && ((current_function_decl
14081 && DECL_CONSTRUCTOR_P (current_function_decl))
14082 || (current_class_ref
14083 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14084 return current_class_ptr;
14085
14086 /* This can happen for a parameter name used later in a function
14087 declaration (such as in a late-specified return type). Just
14088 make a dummy decl, since it's only used for its type. */
14089 gcc_assert (cp_unevaluated_operand != 0);
14090 r = tsubst_decl (t, args, complain);
14091 /* Give it the template pattern as its context; its true context
14092 hasn't been instantiated yet and this is good enough for
14093 mangling. */
14094 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14095 }
14096
14097 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14098 r = ARGUMENT_PACK_SELECT_ARG (r);
14099 if (!mark_used (r, complain) && !(complain & tf_error))
14100 return error_mark_node;
14101 return r;
14102
14103 case CONST_DECL:
14104 {
14105 tree enum_type;
14106 tree v;
14107
14108 if (DECL_TEMPLATE_PARM_P (t))
14109 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14110 /* There is no need to substitute into namespace-scope
14111 enumerators. */
14112 if (DECL_NAMESPACE_SCOPE_P (t))
14113 return t;
14114 /* If ARGS is NULL, then T is known to be non-dependent. */
14115 if (args == NULL_TREE)
14116 return scalar_constant_value (t);
14117
14118 /* Unfortunately, we cannot just call lookup_name here.
14119 Consider:
14120
14121 template <int I> int f() {
14122 enum E { a = I };
14123 struct S { void g() { E e = a; } };
14124 };
14125
14126 When we instantiate f<7>::S::g(), say, lookup_name is not
14127 clever enough to find f<7>::a. */
14128 enum_type
14129 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14130 /*entering_scope=*/0);
14131
14132 for (v = TYPE_VALUES (enum_type);
14133 v != NULL_TREE;
14134 v = TREE_CHAIN (v))
14135 if (TREE_PURPOSE (v) == DECL_NAME (t))
14136 return TREE_VALUE (v);
14137
14138 /* We didn't find the name. That should never happen; if
14139 name-lookup found it during preliminary parsing, we
14140 should find it again here during instantiation. */
14141 gcc_unreachable ();
14142 }
14143 return t;
14144
14145 case FIELD_DECL:
14146 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14147 {
14148 /* Check for a local specialization set up by
14149 tsubst_pack_expansion. */
14150 if (tree r = retrieve_local_specialization (t))
14151 {
14152 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14153 r = ARGUMENT_PACK_SELECT_ARG (r);
14154 return r;
14155 }
14156
14157 /* When retrieving a capture pack from a generic lambda, remove the
14158 lambda call op's own template argument list from ARGS. Only the
14159 template arguments active for the closure type should be used to
14160 retrieve the pack specialization. */
14161 if (LAMBDA_FUNCTION_P (current_function_decl)
14162 && (template_class_depth (DECL_CONTEXT (t))
14163 != TMPL_ARGS_DEPTH (args)))
14164 args = strip_innermost_template_args (args, 1);
14165
14166 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14167 tsubst_decl put in the hash table. */
14168 return retrieve_specialization (t, args, 0);
14169 }
14170
14171 if (DECL_CONTEXT (t))
14172 {
14173 tree ctx;
14174
14175 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14176 /*entering_scope=*/1);
14177 if (ctx != DECL_CONTEXT (t))
14178 {
14179 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14180 if (!r)
14181 {
14182 if (complain & tf_error)
14183 error ("using invalid field %qD", t);
14184 return error_mark_node;
14185 }
14186 return r;
14187 }
14188 }
14189
14190 return t;
14191
14192 case VAR_DECL:
14193 case FUNCTION_DECL:
14194 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14195 r = tsubst (t, args, complain, in_decl);
14196 else if (local_variable_p (t)
14197 && uses_template_parms (DECL_CONTEXT (t)))
14198 {
14199 r = retrieve_local_specialization (t);
14200 if (r == NULL_TREE)
14201 {
14202 /* First try name lookup to find the instantiation. */
14203 r = lookup_name (DECL_NAME (t));
14204 if (r)
14205 {
14206 /* Make sure that the one we found is the one we want. */
14207 tree ctx = DECL_CONTEXT (t);
14208 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14209 ctx = tsubst (ctx, args, complain, in_decl);
14210 if (ctx != DECL_CONTEXT (r))
14211 r = NULL_TREE;
14212 }
14213
14214 if (r)
14215 /* OK */;
14216 else
14217 {
14218 /* This can happen for a variable used in a
14219 late-specified return type of a local lambda, or for a
14220 local static or constant. Building a new VAR_DECL
14221 should be OK in all those cases. */
14222 r = tsubst_decl (t, args, complain);
14223 if (decl_maybe_constant_var_p (r))
14224 {
14225 /* We can't call cp_finish_decl, so handle the
14226 initializer by hand. */
14227 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14228 complain, in_decl);
14229 if (!processing_template_decl)
14230 init = maybe_constant_init (init);
14231 if (processing_template_decl
14232 ? potential_constant_expression (init)
14233 : reduced_constant_expression_p (init))
14234 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14235 = TREE_CONSTANT (r) = true;
14236 DECL_INITIAL (r) = init;
14237 }
14238 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14239 || decl_constant_var_p (r)
14240 || errorcount || sorrycount);
14241 if (!processing_template_decl
14242 && !TREE_STATIC (r))
14243 r = process_outer_var_ref (r, complain);
14244 }
14245 /* Remember this for subsequent uses. */
14246 if (local_specializations)
14247 register_local_specialization (r, t);
14248 }
14249 }
14250 else
14251 r = t;
14252 if (!mark_used (r, complain) && !(complain & tf_error))
14253 return error_mark_node;
14254 return r;
14255
14256 case NAMESPACE_DECL:
14257 return t;
14258
14259 case OVERLOAD:
14260 /* An OVERLOAD will always be a non-dependent overload set; an
14261 overload set from function scope will just be represented with an
14262 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14263 gcc_assert (!uses_template_parms (t));
14264 return t;
14265
14266 case BASELINK:
14267 return tsubst_baselink (t, current_nonlambda_class_type (),
14268 args, complain, in_decl);
14269
14270 case TEMPLATE_DECL:
14271 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14272 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14273 args, complain, in_decl);
14274 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14275 return tsubst (t, args, complain, in_decl);
14276 else if (DECL_CLASS_SCOPE_P (t)
14277 && uses_template_parms (DECL_CONTEXT (t)))
14278 {
14279 /* Template template argument like the following example need
14280 special treatment:
14281
14282 template <template <class> class TT> struct C {};
14283 template <class T> struct D {
14284 template <class U> struct E {};
14285 C<E> c; // #1
14286 };
14287 D<int> d; // #2
14288
14289 We are processing the template argument `E' in #1 for
14290 the template instantiation #2. Originally, `E' is a
14291 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14292 have to substitute this with one having context `D<int>'. */
14293
14294 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14295 return lookup_field (context, DECL_NAME(t), 0, false);
14296 }
14297 else
14298 /* Ordinary template template argument. */
14299 return t;
14300
14301 case CAST_EXPR:
14302 case REINTERPRET_CAST_EXPR:
14303 case CONST_CAST_EXPR:
14304 case STATIC_CAST_EXPR:
14305 case DYNAMIC_CAST_EXPR:
14306 case IMPLICIT_CONV_EXPR:
14307 case CONVERT_EXPR:
14308 case NOP_EXPR:
14309 {
14310 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14311 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14312 return build1 (code, type, op0);
14313 }
14314
14315 case SIZEOF_EXPR:
14316 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14317 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14318 {
14319 tree expanded, op = TREE_OPERAND (t, 0);
14320 int len = 0;
14321
14322 if (SIZEOF_EXPR_TYPE_P (t))
14323 op = TREE_TYPE (op);
14324
14325 ++cp_unevaluated_operand;
14326 ++c_inhibit_evaluation_warnings;
14327 /* We only want to compute the number of arguments. */
14328 if (PACK_EXPANSION_P (op))
14329 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14330 else
14331 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14332 args, complain, in_decl);
14333 --cp_unevaluated_operand;
14334 --c_inhibit_evaluation_warnings;
14335
14336 if (TREE_CODE (expanded) == TREE_VEC)
14337 {
14338 len = TREE_VEC_LENGTH (expanded);
14339 /* Set TREE_USED for the benefit of -Wunused. */
14340 for (int i = 0; i < len; i++)
14341 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14342 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14343 }
14344
14345 if (expanded == error_mark_node)
14346 return error_mark_node;
14347 else if (PACK_EXPANSION_P (expanded)
14348 || (TREE_CODE (expanded) == TREE_VEC
14349 && pack_expansion_args_count (expanded)))
14350
14351 {
14352 if (PACK_EXPANSION_P (expanded))
14353 /* OK. */;
14354 else if (TREE_VEC_LENGTH (expanded) == 1)
14355 expanded = TREE_VEC_ELT (expanded, 0);
14356 else
14357 expanded = make_argument_pack (expanded);
14358
14359 if (TYPE_P (expanded))
14360 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14361 complain & tf_error);
14362 else
14363 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14364 complain & tf_error);
14365 }
14366 else
14367 return build_int_cst (size_type_node, len);
14368 }
14369 if (SIZEOF_EXPR_TYPE_P (t))
14370 {
14371 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14372 args, complain, in_decl);
14373 r = build1 (NOP_EXPR, r, error_mark_node);
14374 r = build1 (SIZEOF_EXPR,
14375 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14376 SIZEOF_EXPR_TYPE_P (r) = 1;
14377 return r;
14378 }
14379 /* Fall through */
14380
14381 case INDIRECT_REF:
14382 case NEGATE_EXPR:
14383 case TRUTH_NOT_EXPR:
14384 case BIT_NOT_EXPR:
14385 case ADDR_EXPR:
14386 case UNARY_PLUS_EXPR: /* Unary + */
14387 case ALIGNOF_EXPR:
14388 case AT_ENCODE_EXPR:
14389 case ARROW_EXPR:
14390 case THROW_EXPR:
14391 case TYPEID_EXPR:
14392 case REALPART_EXPR:
14393 case IMAGPART_EXPR:
14394 case PAREN_EXPR:
14395 {
14396 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14397 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14398 return build1 (code, type, op0);
14399 }
14400
14401 case COMPONENT_REF:
14402 {
14403 tree object;
14404 tree name;
14405
14406 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14407 name = TREE_OPERAND (t, 1);
14408 if (TREE_CODE (name) == BIT_NOT_EXPR)
14409 {
14410 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14411 complain, in_decl);
14412 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14413 }
14414 else if (TREE_CODE (name) == SCOPE_REF
14415 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14416 {
14417 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14418 complain, in_decl);
14419 name = TREE_OPERAND (name, 1);
14420 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14421 complain, in_decl);
14422 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14423 name = build_qualified_name (/*type=*/NULL_TREE,
14424 base, name,
14425 /*template_p=*/false);
14426 }
14427 else if (BASELINK_P (name))
14428 name = tsubst_baselink (name,
14429 non_reference (TREE_TYPE (object)),
14430 args, complain,
14431 in_decl);
14432 else
14433 name = tsubst_copy (name, args, complain, in_decl);
14434 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14435 }
14436
14437 case PLUS_EXPR:
14438 case MINUS_EXPR:
14439 case MULT_EXPR:
14440 case TRUNC_DIV_EXPR:
14441 case CEIL_DIV_EXPR:
14442 case FLOOR_DIV_EXPR:
14443 case ROUND_DIV_EXPR:
14444 case EXACT_DIV_EXPR:
14445 case BIT_AND_EXPR:
14446 case BIT_IOR_EXPR:
14447 case BIT_XOR_EXPR:
14448 case TRUNC_MOD_EXPR:
14449 case FLOOR_MOD_EXPR:
14450 case TRUTH_ANDIF_EXPR:
14451 case TRUTH_ORIF_EXPR:
14452 case TRUTH_AND_EXPR:
14453 case TRUTH_OR_EXPR:
14454 case RSHIFT_EXPR:
14455 case LSHIFT_EXPR:
14456 case RROTATE_EXPR:
14457 case LROTATE_EXPR:
14458 case EQ_EXPR:
14459 case NE_EXPR:
14460 case MAX_EXPR:
14461 case MIN_EXPR:
14462 case LE_EXPR:
14463 case GE_EXPR:
14464 case LT_EXPR:
14465 case GT_EXPR:
14466 case COMPOUND_EXPR:
14467 case DOTSTAR_EXPR:
14468 case MEMBER_REF:
14469 case PREDECREMENT_EXPR:
14470 case PREINCREMENT_EXPR:
14471 case POSTDECREMENT_EXPR:
14472 case POSTINCREMENT_EXPR:
14473 {
14474 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14475 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14476 return build_nt (code, op0, op1);
14477 }
14478
14479 case SCOPE_REF:
14480 {
14481 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14482 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14483 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14484 QUALIFIED_NAME_IS_TEMPLATE (t));
14485 }
14486
14487 case ARRAY_REF:
14488 {
14489 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14490 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14491 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14492 }
14493
14494 case CALL_EXPR:
14495 {
14496 int n = VL_EXP_OPERAND_LENGTH (t);
14497 tree result = build_vl_exp (CALL_EXPR, n);
14498 int i;
14499 for (i = 0; i < n; i++)
14500 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14501 complain, in_decl);
14502 return result;
14503 }
14504
14505 case COND_EXPR:
14506 case MODOP_EXPR:
14507 case PSEUDO_DTOR_EXPR:
14508 case VEC_PERM_EXPR:
14509 {
14510 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14511 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14512 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14513 r = build_nt (code, op0, op1, op2);
14514 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14515 return r;
14516 }
14517
14518 case NEW_EXPR:
14519 {
14520 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14521 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14522 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14523 r = build_nt (code, op0, op1, op2);
14524 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14525 return r;
14526 }
14527
14528 case DELETE_EXPR:
14529 {
14530 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14531 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14532 r = build_nt (code, op0, op1);
14533 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14534 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14535 return r;
14536 }
14537
14538 case TEMPLATE_ID_EXPR:
14539 {
14540 /* Substituted template arguments */
14541 tree fn = TREE_OPERAND (t, 0);
14542 tree targs = TREE_OPERAND (t, 1);
14543
14544 fn = tsubst_copy (fn, args, complain, in_decl);
14545 if (targs)
14546 targs = tsubst_template_args (targs, args, complain, in_decl);
14547
14548 return lookup_template_function (fn, targs);
14549 }
14550
14551 case TREE_LIST:
14552 {
14553 tree purpose, value, chain;
14554
14555 if (t == void_list_node)
14556 return t;
14557
14558 purpose = TREE_PURPOSE (t);
14559 if (purpose)
14560 purpose = tsubst_copy (purpose, args, complain, in_decl);
14561 value = TREE_VALUE (t);
14562 if (value)
14563 value = tsubst_copy (value, args, complain, in_decl);
14564 chain = TREE_CHAIN (t);
14565 if (chain && chain != void_type_node)
14566 chain = tsubst_copy (chain, args, complain, in_decl);
14567 if (purpose == TREE_PURPOSE (t)
14568 && value == TREE_VALUE (t)
14569 && chain == TREE_CHAIN (t))
14570 return t;
14571 return tree_cons (purpose, value, chain);
14572 }
14573
14574 case RECORD_TYPE:
14575 case UNION_TYPE:
14576 case ENUMERAL_TYPE:
14577 case INTEGER_TYPE:
14578 case TEMPLATE_TYPE_PARM:
14579 case TEMPLATE_TEMPLATE_PARM:
14580 case BOUND_TEMPLATE_TEMPLATE_PARM:
14581 case TEMPLATE_PARM_INDEX:
14582 case POINTER_TYPE:
14583 case REFERENCE_TYPE:
14584 case OFFSET_TYPE:
14585 case FUNCTION_TYPE:
14586 case METHOD_TYPE:
14587 case ARRAY_TYPE:
14588 case TYPENAME_TYPE:
14589 case UNBOUND_CLASS_TEMPLATE:
14590 case TYPEOF_TYPE:
14591 case DECLTYPE_TYPE:
14592 case TYPE_DECL:
14593 return tsubst (t, args, complain, in_decl);
14594
14595 case USING_DECL:
14596 t = DECL_NAME (t);
14597 /* Fall through. */
14598 case IDENTIFIER_NODE:
14599 if (IDENTIFIER_TYPENAME_P (t))
14600 {
14601 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14602 return mangle_conv_op_name_for_type (new_type);
14603 }
14604 else
14605 return t;
14606
14607 case CONSTRUCTOR:
14608 /* This is handled by tsubst_copy_and_build. */
14609 gcc_unreachable ();
14610
14611 case VA_ARG_EXPR:
14612 {
14613 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14614 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14615 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14616 }
14617
14618 case CLEANUP_POINT_EXPR:
14619 /* We shouldn't have built any of these during initial template
14620 generation. Instead, they should be built during instantiation
14621 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14622 gcc_unreachable ();
14623
14624 case OFFSET_REF:
14625 {
14626 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14627 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14628 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14629 r = build2 (code, type, op0, op1);
14630 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14631 if (!mark_used (TREE_OPERAND (r, 1), complain)
14632 && !(complain & tf_error))
14633 return error_mark_node;
14634 return r;
14635 }
14636
14637 case EXPR_PACK_EXPANSION:
14638 error ("invalid use of pack expansion expression");
14639 return error_mark_node;
14640
14641 case NONTYPE_ARGUMENT_PACK:
14642 error ("use %<...%> to expand argument pack");
14643 return error_mark_node;
14644
14645 case VOID_CST:
14646 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14647 return t;
14648
14649 case INTEGER_CST:
14650 case REAL_CST:
14651 case STRING_CST:
14652 case COMPLEX_CST:
14653 {
14654 /* Instantiate any typedefs in the type. */
14655 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14656 r = fold_convert (type, t);
14657 gcc_assert (TREE_CODE (r) == code);
14658 return r;
14659 }
14660
14661 case PTRMEM_CST:
14662 /* These can sometimes show up in a partial instantiation, but never
14663 involve template parms. */
14664 gcc_assert (!uses_template_parms (t));
14665 return t;
14666
14667 case UNARY_LEFT_FOLD_EXPR:
14668 return tsubst_unary_left_fold (t, args, complain, in_decl);
14669 case UNARY_RIGHT_FOLD_EXPR:
14670 return tsubst_unary_right_fold (t, args, complain, in_decl);
14671 case BINARY_LEFT_FOLD_EXPR:
14672 return tsubst_binary_left_fold (t, args, complain, in_decl);
14673 case BINARY_RIGHT_FOLD_EXPR:
14674 return tsubst_binary_right_fold (t, args, complain, in_decl);
14675
14676 default:
14677 /* We shouldn't get here, but keep going if !flag_checking. */
14678 if (flag_checking)
14679 gcc_unreachable ();
14680 return t;
14681 }
14682 }
14683
14684 /* Helper function for tsubst_omp_clauses, used for instantiation of
14685 OMP_CLAUSE_DECL of clauses. */
14686
14687 static tree
14688 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
14689 tree in_decl)
14690 {
14691 if (decl == NULL_TREE)
14692 return NULL_TREE;
14693
14694 /* Handle an OpenMP array section represented as a TREE_LIST (or
14695 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
14696 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
14697 TREE_LIST. We can handle it exactly the same as an array section
14698 (purpose, value, and a chain), even though the nomenclature
14699 (low_bound, length, etc) is different. */
14700 if (TREE_CODE (decl) == TREE_LIST)
14701 {
14702 tree low_bound
14703 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
14704 /*integral_constant_expression_p=*/false);
14705 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
14706 /*integral_constant_expression_p=*/false);
14707 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
14708 in_decl);
14709 if (TREE_PURPOSE (decl) == low_bound
14710 && TREE_VALUE (decl) == length
14711 && TREE_CHAIN (decl) == chain)
14712 return decl;
14713 tree ret = tree_cons (low_bound, length, chain);
14714 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
14715 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
14716 return ret;
14717 }
14718 tree ret = tsubst_expr (decl, args, complain, in_decl,
14719 /*integral_constant_expression_p=*/false);
14720 /* Undo convert_from_reference tsubst_expr could have called. */
14721 if (decl
14722 && REFERENCE_REF_P (ret)
14723 && !REFERENCE_REF_P (decl))
14724 ret = TREE_OPERAND (ret, 0);
14725 return ret;
14726 }
14727
14728 /* Like tsubst_copy, but specifically for OpenMP clauses. */
14729
14730 static tree
14731 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
14732 tree args, tsubst_flags_t complain, tree in_decl)
14733 {
14734 tree new_clauses = NULL_TREE, nc, oc;
14735 tree linear_no_step = NULL_TREE;
14736
14737 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
14738 {
14739 nc = copy_node (oc);
14740 OMP_CLAUSE_CHAIN (nc) = new_clauses;
14741 new_clauses = nc;
14742
14743 switch (OMP_CLAUSE_CODE (nc))
14744 {
14745 case OMP_CLAUSE_LASTPRIVATE:
14746 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
14747 {
14748 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
14749 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
14750 in_decl, /*integral_constant_expression_p=*/false);
14751 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
14752 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
14753 }
14754 /* FALLTHRU */
14755 case OMP_CLAUSE_PRIVATE:
14756 case OMP_CLAUSE_SHARED:
14757 case OMP_CLAUSE_FIRSTPRIVATE:
14758 case OMP_CLAUSE_COPYIN:
14759 case OMP_CLAUSE_COPYPRIVATE:
14760 case OMP_CLAUSE_UNIFORM:
14761 case OMP_CLAUSE_DEPEND:
14762 case OMP_CLAUSE_FROM:
14763 case OMP_CLAUSE_TO:
14764 case OMP_CLAUSE_MAP:
14765 case OMP_CLAUSE_USE_DEVICE_PTR:
14766 case OMP_CLAUSE_IS_DEVICE_PTR:
14767 OMP_CLAUSE_DECL (nc)
14768 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14769 in_decl);
14770 break;
14771 case OMP_CLAUSE_IF:
14772 case OMP_CLAUSE_NUM_THREADS:
14773 case OMP_CLAUSE_SCHEDULE:
14774 case OMP_CLAUSE_COLLAPSE:
14775 case OMP_CLAUSE_FINAL:
14776 case OMP_CLAUSE_DEVICE:
14777 case OMP_CLAUSE_DIST_SCHEDULE:
14778 case OMP_CLAUSE_NUM_TEAMS:
14779 case OMP_CLAUSE_THREAD_LIMIT:
14780 case OMP_CLAUSE_SAFELEN:
14781 case OMP_CLAUSE_SIMDLEN:
14782 case OMP_CLAUSE_NUM_TASKS:
14783 case OMP_CLAUSE_GRAINSIZE:
14784 case OMP_CLAUSE_PRIORITY:
14785 case OMP_CLAUSE_ORDERED:
14786 case OMP_CLAUSE_HINT:
14787 case OMP_CLAUSE_NUM_GANGS:
14788 case OMP_CLAUSE_NUM_WORKERS:
14789 case OMP_CLAUSE_VECTOR_LENGTH:
14790 case OMP_CLAUSE_WORKER:
14791 case OMP_CLAUSE_VECTOR:
14792 case OMP_CLAUSE_ASYNC:
14793 case OMP_CLAUSE_WAIT:
14794 OMP_CLAUSE_OPERAND (nc, 0)
14795 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
14796 in_decl, /*integral_constant_expression_p=*/false);
14797 break;
14798 case OMP_CLAUSE_REDUCTION:
14799 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
14800 {
14801 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
14802 if (TREE_CODE (placeholder) == SCOPE_REF)
14803 {
14804 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
14805 complain, in_decl);
14806 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
14807 = build_qualified_name (NULL_TREE, scope,
14808 TREE_OPERAND (placeholder, 1),
14809 false);
14810 }
14811 else
14812 gcc_assert (identifier_p (placeholder));
14813 }
14814 OMP_CLAUSE_DECL (nc)
14815 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14816 in_decl);
14817 break;
14818 case OMP_CLAUSE_GANG:
14819 case OMP_CLAUSE_ALIGNED:
14820 OMP_CLAUSE_DECL (nc)
14821 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14822 in_decl);
14823 OMP_CLAUSE_OPERAND (nc, 1)
14824 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
14825 in_decl, /*integral_constant_expression_p=*/false);
14826 break;
14827 case OMP_CLAUSE_LINEAR:
14828 OMP_CLAUSE_DECL (nc)
14829 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
14830 in_decl);
14831 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
14832 {
14833 gcc_assert (!linear_no_step);
14834 linear_no_step = nc;
14835 }
14836 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
14837 OMP_CLAUSE_LINEAR_STEP (nc)
14838 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
14839 complain, in_decl);
14840 else
14841 OMP_CLAUSE_LINEAR_STEP (nc)
14842 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
14843 in_decl,
14844 /*integral_constant_expression_p=*/false);
14845 break;
14846 case OMP_CLAUSE_NOWAIT:
14847 case OMP_CLAUSE_DEFAULT:
14848 case OMP_CLAUSE_UNTIED:
14849 case OMP_CLAUSE_MERGEABLE:
14850 case OMP_CLAUSE_INBRANCH:
14851 case OMP_CLAUSE_NOTINBRANCH:
14852 case OMP_CLAUSE_PROC_BIND:
14853 case OMP_CLAUSE_FOR:
14854 case OMP_CLAUSE_PARALLEL:
14855 case OMP_CLAUSE_SECTIONS:
14856 case OMP_CLAUSE_TASKGROUP:
14857 case OMP_CLAUSE_NOGROUP:
14858 case OMP_CLAUSE_THREADS:
14859 case OMP_CLAUSE_SIMD:
14860 case OMP_CLAUSE_DEFAULTMAP:
14861 case OMP_CLAUSE_INDEPENDENT:
14862 case OMP_CLAUSE_AUTO:
14863 case OMP_CLAUSE_SEQ:
14864 break;
14865 case OMP_CLAUSE_TILE:
14866 {
14867 tree lnc, loc;
14868 for (lnc = OMP_CLAUSE_TILE_LIST (nc),
14869 loc = OMP_CLAUSE_TILE_LIST (oc);
14870 loc;
14871 loc = TREE_CHAIN (loc), lnc = TREE_CHAIN (lnc))
14872 {
14873 TREE_VALUE (lnc) = tsubst_expr (TREE_VALUE (loc), args,
14874 complain, in_decl, false);
14875 }
14876 }
14877 break;
14878 default:
14879 gcc_unreachable ();
14880 }
14881 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
14882 switch (OMP_CLAUSE_CODE (nc))
14883 {
14884 case OMP_CLAUSE_SHARED:
14885 case OMP_CLAUSE_PRIVATE:
14886 case OMP_CLAUSE_FIRSTPRIVATE:
14887 case OMP_CLAUSE_LASTPRIVATE:
14888 case OMP_CLAUSE_COPYPRIVATE:
14889 case OMP_CLAUSE_LINEAR:
14890 case OMP_CLAUSE_REDUCTION:
14891 case OMP_CLAUSE_USE_DEVICE_PTR:
14892 case OMP_CLAUSE_IS_DEVICE_PTR:
14893 /* tsubst_expr on SCOPE_REF results in returning
14894 finish_non_static_data_member result. Undo that here. */
14895 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
14896 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
14897 == IDENTIFIER_NODE))
14898 {
14899 tree t = OMP_CLAUSE_DECL (nc);
14900 tree v = t;
14901 while (v)
14902 switch (TREE_CODE (v))
14903 {
14904 case COMPONENT_REF:
14905 case MEM_REF:
14906 case INDIRECT_REF:
14907 CASE_CONVERT:
14908 case POINTER_PLUS_EXPR:
14909 v = TREE_OPERAND (v, 0);
14910 continue;
14911 case PARM_DECL:
14912 if (DECL_CONTEXT (v) == current_function_decl
14913 && DECL_ARTIFICIAL (v)
14914 && DECL_NAME (v) == this_identifier)
14915 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
14916 /* FALLTHRU */
14917 default:
14918 v = NULL_TREE;
14919 break;
14920 }
14921 }
14922 else if (VAR_P (OMP_CLAUSE_DECL (oc))
14923 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
14924 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
14925 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
14926 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
14927 {
14928 tree decl = OMP_CLAUSE_DECL (nc);
14929 if (VAR_P (decl))
14930 {
14931 if (!DECL_LANG_SPECIFIC (decl))
14932 retrofit_lang_decl (decl);
14933 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
14934 }
14935 }
14936 break;
14937 default:
14938 break;
14939 }
14940 }
14941
14942 new_clauses = nreverse (new_clauses);
14943 if (ort != C_ORT_OMP_DECLARE_SIMD)
14944 {
14945 new_clauses = finish_omp_clauses (new_clauses, ort);
14946 if (linear_no_step)
14947 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
14948 if (nc == linear_no_step)
14949 {
14950 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
14951 break;
14952 }
14953 }
14954 return new_clauses;
14955 }
14956
14957 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
14958
14959 static tree
14960 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
14961 tree in_decl)
14962 {
14963 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
14964
14965 tree purpose, value, chain;
14966
14967 if (t == NULL)
14968 return t;
14969
14970 if (TREE_CODE (t) != TREE_LIST)
14971 return tsubst_copy_and_build (t, args, complain, in_decl,
14972 /*function_p=*/false,
14973 /*integral_constant_expression_p=*/false);
14974
14975 if (t == void_list_node)
14976 return t;
14977
14978 purpose = TREE_PURPOSE (t);
14979 if (purpose)
14980 purpose = RECUR (purpose);
14981 value = TREE_VALUE (t);
14982 if (value)
14983 {
14984 if (TREE_CODE (value) != LABEL_DECL)
14985 value = RECUR (value);
14986 else
14987 {
14988 value = lookup_label (DECL_NAME (value));
14989 gcc_assert (TREE_CODE (value) == LABEL_DECL);
14990 TREE_USED (value) = 1;
14991 }
14992 }
14993 chain = TREE_CHAIN (t);
14994 if (chain && chain != void_type_node)
14995 chain = RECUR (chain);
14996 return tree_cons (purpose, value, chain);
14997 #undef RECUR
14998 }
14999
15000 /* Used to temporarily communicate the list of #pragma omp parallel
15001 clauses to #pragma omp for instantiation if they are combined
15002 together. */
15003
15004 static tree *omp_parallel_combined_clauses;
15005
15006 /* Substitute one OMP_FOR iterator. */
15007
15008 static void
15009 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15010 tree initv, tree condv, tree incrv, tree *clauses,
15011 tree args, tsubst_flags_t complain, tree in_decl,
15012 bool integral_constant_expression_p)
15013 {
15014 #define RECUR(NODE) \
15015 tsubst_expr ((NODE), args, complain, in_decl, \
15016 integral_constant_expression_p)
15017 tree decl, init, cond, incr;
15018
15019 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15020 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15021
15022 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15023 {
15024 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15025 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15026 }
15027
15028 decl = TREE_OPERAND (init, 0);
15029 init = TREE_OPERAND (init, 1);
15030 tree decl_expr = NULL_TREE;
15031 if (init && TREE_CODE (init) == DECL_EXPR)
15032 {
15033 /* We need to jump through some hoops to handle declarations in the
15034 init-statement, since we might need to handle auto deduction,
15035 but we need to keep control of initialization. */
15036 decl_expr = init;
15037 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15038 decl = tsubst_decl (decl, args, complain);
15039 }
15040 else
15041 {
15042 if (TREE_CODE (decl) == SCOPE_REF)
15043 {
15044 decl = RECUR (decl);
15045 if (TREE_CODE (decl) == COMPONENT_REF)
15046 {
15047 tree v = decl;
15048 while (v)
15049 switch (TREE_CODE (v))
15050 {
15051 case COMPONENT_REF:
15052 case MEM_REF:
15053 case INDIRECT_REF:
15054 CASE_CONVERT:
15055 case POINTER_PLUS_EXPR:
15056 v = TREE_OPERAND (v, 0);
15057 continue;
15058 case PARM_DECL:
15059 if (DECL_CONTEXT (v) == current_function_decl
15060 && DECL_ARTIFICIAL (v)
15061 && DECL_NAME (v) == this_identifier)
15062 {
15063 decl = TREE_OPERAND (decl, 1);
15064 decl = omp_privatize_field (decl, false);
15065 }
15066 /* FALLTHRU */
15067 default:
15068 v = NULL_TREE;
15069 break;
15070 }
15071 }
15072 }
15073 else
15074 decl = RECUR (decl);
15075 }
15076 init = RECUR (init);
15077
15078 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15079 if (auto_node && init)
15080 TREE_TYPE (decl)
15081 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15082
15083 gcc_assert (!type_dependent_expression_p (decl));
15084
15085 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15086 {
15087 if (decl_expr)
15088 {
15089 /* Declare the variable, but don't let that initialize it. */
15090 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15091 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15092 RECUR (decl_expr);
15093 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15094 }
15095
15096 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15097 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15098 if (TREE_CODE (incr) == MODIFY_EXPR)
15099 {
15100 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15101 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15102 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15103 NOP_EXPR, rhs, complain);
15104 }
15105 else
15106 incr = RECUR (incr);
15107 TREE_VEC_ELT (declv, i) = decl;
15108 TREE_VEC_ELT (initv, i) = init;
15109 TREE_VEC_ELT (condv, i) = cond;
15110 TREE_VEC_ELT (incrv, i) = incr;
15111 return;
15112 }
15113
15114 if (decl_expr)
15115 {
15116 /* Declare and initialize the variable. */
15117 RECUR (decl_expr);
15118 init = NULL_TREE;
15119 }
15120 else if (init)
15121 {
15122 tree *pc;
15123 int j;
15124 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15125 {
15126 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15127 {
15128 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15129 && OMP_CLAUSE_DECL (*pc) == decl)
15130 break;
15131 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15132 && OMP_CLAUSE_DECL (*pc) == decl)
15133 {
15134 if (j)
15135 break;
15136 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15137 tree c = *pc;
15138 *pc = OMP_CLAUSE_CHAIN (c);
15139 OMP_CLAUSE_CHAIN (c) = *clauses;
15140 *clauses = c;
15141 }
15142 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15143 && OMP_CLAUSE_DECL (*pc) == decl)
15144 {
15145 error ("iteration variable %qD should not be firstprivate",
15146 decl);
15147 *pc = OMP_CLAUSE_CHAIN (*pc);
15148 }
15149 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15150 && OMP_CLAUSE_DECL (*pc) == decl)
15151 {
15152 error ("iteration variable %qD should not be reduction",
15153 decl);
15154 *pc = OMP_CLAUSE_CHAIN (*pc);
15155 }
15156 else
15157 pc = &OMP_CLAUSE_CHAIN (*pc);
15158 }
15159 if (*pc)
15160 break;
15161 }
15162 if (*pc == NULL_TREE)
15163 {
15164 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15165 OMP_CLAUSE_DECL (c) = decl;
15166 c = finish_omp_clauses (c, C_ORT_OMP);
15167 if (c)
15168 {
15169 OMP_CLAUSE_CHAIN (c) = *clauses;
15170 *clauses = c;
15171 }
15172 }
15173 }
15174 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15175 if (COMPARISON_CLASS_P (cond))
15176 {
15177 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15178 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15179 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15180 }
15181 else
15182 cond = RECUR (cond);
15183 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15184 switch (TREE_CODE (incr))
15185 {
15186 case PREINCREMENT_EXPR:
15187 case PREDECREMENT_EXPR:
15188 case POSTINCREMENT_EXPR:
15189 case POSTDECREMENT_EXPR:
15190 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15191 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15192 break;
15193 case MODIFY_EXPR:
15194 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15195 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15196 {
15197 tree rhs = TREE_OPERAND (incr, 1);
15198 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15199 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15200 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15201 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15202 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15203 rhs0, rhs1));
15204 }
15205 else
15206 incr = RECUR (incr);
15207 break;
15208 case MODOP_EXPR:
15209 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15210 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15211 {
15212 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15213 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15214 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15215 TREE_TYPE (decl), lhs,
15216 RECUR (TREE_OPERAND (incr, 2))));
15217 }
15218 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15219 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15220 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15221 {
15222 tree rhs = TREE_OPERAND (incr, 2);
15223 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15224 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15225 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15226 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15227 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15228 rhs0, rhs1));
15229 }
15230 else
15231 incr = RECUR (incr);
15232 break;
15233 default:
15234 incr = RECUR (incr);
15235 break;
15236 }
15237
15238 TREE_VEC_ELT (declv, i) = decl;
15239 TREE_VEC_ELT (initv, i) = init;
15240 TREE_VEC_ELT (condv, i) = cond;
15241 TREE_VEC_ELT (incrv, i) = incr;
15242 #undef RECUR
15243 }
15244
15245 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15246 of OMP_TARGET's body. */
15247
15248 static tree
15249 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15250 {
15251 *walk_subtrees = 0;
15252 switch (TREE_CODE (*tp))
15253 {
15254 case OMP_TEAMS:
15255 return *tp;
15256 case BIND_EXPR:
15257 case STATEMENT_LIST:
15258 *walk_subtrees = 1;
15259 break;
15260 default:
15261 break;
15262 }
15263 return NULL_TREE;
15264 }
15265
15266 /* Like tsubst_copy for expressions, etc. but also does semantic
15267 processing. */
15268
15269 tree
15270 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15271 bool integral_constant_expression_p)
15272 {
15273 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15274 #define RECUR(NODE) \
15275 tsubst_expr ((NODE), args, complain, in_decl, \
15276 integral_constant_expression_p)
15277
15278 tree stmt, tmp;
15279 tree r;
15280 location_t loc;
15281
15282 if (t == NULL_TREE || t == error_mark_node)
15283 return t;
15284
15285 loc = input_location;
15286 if (EXPR_HAS_LOCATION (t))
15287 input_location = EXPR_LOCATION (t);
15288 if (STATEMENT_CODE_P (TREE_CODE (t)))
15289 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15290
15291 switch (TREE_CODE (t))
15292 {
15293 case STATEMENT_LIST:
15294 {
15295 tree_stmt_iterator i;
15296 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15297 RECUR (tsi_stmt (i));
15298 break;
15299 }
15300
15301 case CTOR_INITIALIZER:
15302 finish_mem_initializers (tsubst_initializer_list
15303 (TREE_OPERAND (t, 0), args));
15304 break;
15305
15306 case RETURN_EXPR:
15307 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15308 break;
15309
15310 case EXPR_STMT:
15311 tmp = RECUR (EXPR_STMT_EXPR (t));
15312 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15313 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15314 else
15315 finish_expr_stmt (tmp);
15316 break;
15317
15318 case USING_STMT:
15319 do_using_directive (USING_STMT_NAMESPACE (t));
15320 break;
15321
15322 case DECL_EXPR:
15323 {
15324 tree decl, pattern_decl;
15325 tree init;
15326
15327 pattern_decl = decl = DECL_EXPR_DECL (t);
15328 if (TREE_CODE (decl) == LABEL_DECL)
15329 finish_label_decl (DECL_NAME (decl));
15330 else if (TREE_CODE (decl) == USING_DECL)
15331 {
15332 tree scope = USING_DECL_SCOPE (decl);
15333 tree name = DECL_NAME (decl);
15334
15335 scope = tsubst (scope, args, complain, in_decl);
15336 decl = lookup_qualified_name (scope, name,
15337 /*is_type_p=*/false,
15338 /*complain=*/false);
15339 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15340 qualified_name_lookup_error (scope, name, decl, input_location);
15341 else
15342 do_local_using_decl (decl, scope, name);
15343 }
15344 else if (DECL_PACK_P (decl))
15345 {
15346 /* Don't build up decls for a variadic capture proxy, we'll
15347 instantiate the elements directly as needed. */
15348 break;
15349 }
15350 else
15351 {
15352 init = DECL_INITIAL (decl);
15353 decl = tsubst (decl, args, complain, in_decl);
15354 if (decl != error_mark_node)
15355 {
15356 /* By marking the declaration as instantiated, we avoid
15357 trying to instantiate it. Since instantiate_decl can't
15358 handle local variables, and since we've already done
15359 all that needs to be done, that's the right thing to
15360 do. */
15361 if (VAR_P (decl))
15362 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15363 if (VAR_P (decl)
15364 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15365 /* Anonymous aggregates are a special case. */
15366 finish_anon_union (decl);
15367 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15368 {
15369 DECL_CONTEXT (decl) = current_function_decl;
15370 if (DECL_NAME (decl) == this_identifier)
15371 {
15372 tree lam = DECL_CONTEXT (current_function_decl);
15373 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15374 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15375 }
15376 insert_capture_proxy (decl);
15377 }
15378 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15379 /* We already did a pushtag. */;
15380 else if (TREE_CODE (decl) == FUNCTION_DECL
15381 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15382 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15383 {
15384 DECL_CONTEXT (decl) = NULL_TREE;
15385 pushdecl (decl);
15386 DECL_CONTEXT (decl) = current_function_decl;
15387 cp_check_omp_declare_reduction (decl);
15388 }
15389 else
15390 {
15391 int const_init = false;
15392 maybe_push_decl (decl);
15393 if (VAR_P (decl)
15394 && DECL_PRETTY_FUNCTION_P (decl))
15395 {
15396 /* For __PRETTY_FUNCTION__ we have to adjust the
15397 initializer. */
15398 const char *const name
15399 = cxx_printable_name (current_function_decl, 2);
15400 init = cp_fname_init (name, &TREE_TYPE (decl));
15401 }
15402 else
15403 init = tsubst_init (init, decl, args, complain, in_decl);
15404
15405 if (VAR_P (decl))
15406 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15407 (pattern_decl));
15408 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15409 }
15410 }
15411 }
15412
15413 break;
15414 }
15415
15416 case FOR_STMT:
15417 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15418 RECUR (FOR_INIT_STMT (t));
15419 finish_init_stmt (stmt);
15420 tmp = RECUR (FOR_COND (t));
15421 finish_for_cond (tmp, stmt, false);
15422 tmp = RECUR (FOR_EXPR (t));
15423 finish_for_expr (tmp, stmt);
15424 RECUR (FOR_BODY (t));
15425 finish_for_stmt (stmt);
15426 break;
15427
15428 case RANGE_FOR_STMT:
15429 {
15430 tree decl, expr;
15431 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15432 decl = RANGE_FOR_DECL (t);
15433 decl = tsubst (decl, args, complain, in_decl);
15434 maybe_push_decl (decl);
15435 expr = RECUR (RANGE_FOR_EXPR (t));
15436 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
15437 RECUR (RANGE_FOR_BODY (t));
15438 finish_for_stmt (stmt);
15439 }
15440 break;
15441
15442 case WHILE_STMT:
15443 stmt = begin_while_stmt ();
15444 tmp = RECUR (WHILE_COND (t));
15445 finish_while_stmt_cond (tmp, stmt, false);
15446 RECUR (WHILE_BODY (t));
15447 finish_while_stmt (stmt);
15448 break;
15449
15450 case DO_STMT:
15451 stmt = begin_do_stmt ();
15452 RECUR (DO_BODY (t));
15453 finish_do_body (stmt);
15454 tmp = RECUR (DO_COND (t));
15455 finish_do_stmt (tmp, stmt, false);
15456 break;
15457
15458 case IF_STMT:
15459 stmt = begin_if_stmt ();
15460 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15461 tmp = RECUR (IF_COND (t));
15462 tmp = finish_if_stmt_cond (tmp, stmt);
15463 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15464 /* Don't instantiate the THEN_CLAUSE. */;
15465 else
15466 {
15467 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15468 if (inhibit)
15469 ++c_inhibit_evaluation_warnings;
15470 RECUR (THEN_CLAUSE (t));
15471 if (inhibit)
15472 --c_inhibit_evaluation_warnings;
15473 }
15474 finish_then_clause (stmt);
15475
15476 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15477 /* Don't instantiate the ELSE_CLAUSE. */;
15478 else if (ELSE_CLAUSE (t))
15479 {
15480 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15481 begin_else_clause (stmt);
15482 if (inhibit)
15483 ++c_inhibit_evaluation_warnings;
15484 RECUR (ELSE_CLAUSE (t));
15485 if (inhibit)
15486 --c_inhibit_evaluation_warnings;
15487 finish_else_clause (stmt);
15488 }
15489
15490 finish_if_stmt (stmt);
15491 break;
15492
15493 case BIND_EXPR:
15494 if (BIND_EXPR_BODY_BLOCK (t))
15495 stmt = begin_function_body ();
15496 else
15497 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15498 ? BCS_TRY_BLOCK : 0);
15499
15500 RECUR (BIND_EXPR_BODY (t));
15501
15502 if (BIND_EXPR_BODY_BLOCK (t))
15503 finish_function_body (stmt);
15504 else
15505 finish_compound_stmt (stmt);
15506 break;
15507
15508 case BREAK_STMT:
15509 finish_break_stmt ();
15510 break;
15511
15512 case CONTINUE_STMT:
15513 finish_continue_stmt ();
15514 break;
15515
15516 case SWITCH_STMT:
15517 stmt = begin_switch_stmt ();
15518 tmp = RECUR (SWITCH_STMT_COND (t));
15519 finish_switch_cond (tmp, stmt);
15520 RECUR (SWITCH_STMT_BODY (t));
15521 finish_switch_stmt (stmt);
15522 break;
15523
15524 case CASE_LABEL_EXPR:
15525 {
15526 tree low = RECUR (CASE_LOW (t));
15527 tree high = RECUR (CASE_HIGH (t));
15528 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15529 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15530 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15531 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15532 }
15533 break;
15534
15535 case LABEL_EXPR:
15536 {
15537 tree decl = LABEL_EXPR_LABEL (t);
15538 tree label;
15539
15540 label = finish_label_stmt (DECL_NAME (decl));
15541 if (TREE_CODE (label) == LABEL_DECL)
15542 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15543 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15544 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15545 }
15546 break;
15547
15548 case GOTO_EXPR:
15549 tmp = GOTO_DESTINATION (t);
15550 if (TREE_CODE (tmp) != LABEL_DECL)
15551 /* Computed goto's must be tsubst'd into. On the other hand,
15552 non-computed gotos must not be; the identifier in question
15553 will have no binding. */
15554 tmp = RECUR (tmp);
15555 else
15556 tmp = DECL_NAME (tmp);
15557 finish_goto_stmt (tmp);
15558 break;
15559
15560 case ASM_EXPR:
15561 {
15562 tree string = RECUR (ASM_STRING (t));
15563 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15564 complain, in_decl);
15565 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15566 complain, in_decl);
15567 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15568 complain, in_decl);
15569 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15570 complain, in_decl);
15571 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15572 clobbers, labels);
15573 tree asm_expr = tmp;
15574 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15575 asm_expr = TREE_OPERAND (asm_expr, 0);
15576 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15577 }
15578 break;
15579
15580 case TRY_BLOCK:
15581 if (CLEANUP_P (t))
15582 {
15583 stmt = begin_try_block ();
15584 RECUR (TRY_STMTS (t));
15585 finish_cleanup_try_block (stmt);
15586 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15587 }
15588 else
15589 {
15590 tree compound_stmt = NULL_TREE;
15591
15592 if (FN_TRY_BLOCK_P (t))
15593 stmt = begin_function_try_block (&compound_stmt);
15594 else
15595 stmt = begin_try_block ();
15596
15597 RECUR (TRY_STMTS (t));
15598
15599 if (FN_TRY_BLOCK_P (t))
15600 finish_function_try_block (stmt);
15601 else
15602 finish_try_block (stmt);
15603
15604 RECUR (TRY_HANDLERS (t));
15605 if (FN_TRY_BLOCK_P (t))
15606 finish_function_handler_sequence (stmt, compound_stmt);
15607 else
15608 finish_handler_sequence (stmt);
15609 }
15610 break;
15611
15612 case HANDLER:
15613 {
15614 tree decl = HANDLER_PARMS (t);
15615
15616 if (decl)
15617 {
15618 decl = tsubst (decl, args, complain, in_decl);
15619 /* Prevent instantiate_decl from trying to instantiate
15620 this variable. We've already done all that needs to be
15621 done. */
15622 if (decl != error_mark_node)
15623 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15624 }
15625 stmt = begin_handler ();
15626 finish_handler_parms (decl, stmt);
15627 RECUR (HANDLER_BODY (t));
15628 finish_handler (stmt);
15629 }
15630 break;
15631
15632 case TAG_DEFN:
15633 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
15634 if (CLASS_TYPE_P (tmp))
15635 {
15636 /* Local classes are not independent templates; they are
15637 instantiated along with their containing function. And this
15638 way we don't have to deal with pushing out of one local class
15639 to instantiate a member of another local class. */
15640 tree fn;
15641 /* Closures are handled by the LAMBDA_EXPR. */
15642 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
15643 complete_type (tmp);
15644 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
15645 if (!DECL_ARTIFICIAL (fn))
15646 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
15647 }
15648 break;
15649
15650 case STATIC_ASSERT:
15651 {
15652 tree condition;
15653
15654 ++c_inhibit_evaluation_warnings;
15655 condition =
15656 tsubst_expr (STATIC_ASSERT_CONDITION (t),
15657 args,
15658 complain, in_decl,
15659 /*integral_constant_expression_p=*/true);
15660 --c_inhibit_evaluation_warnings;
15661
15662 finish_static_assert (condition,
15663 STATIC_ASSERT_MESSAGE (t),
15664 STATIC_ASSERT_SOURCE_LOCATION (t),
15665 /*member_p=*/false);
15666 }
15667 break;
15668
15669 case OACC_KERNELS:
15670 case OACC_PARALLEL:
15671 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
15672 in_decl);
15673 stmt = begin_omp_parallel ();
15674 RECUR (OMP_BODY (t));
15675 finish_omp_construct (TREE_CODE (t), stmt, tmp);
15676 break;
15677
15678 case OMP_PARALLEL:
15679 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
15680 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
15681 complain, in_decl);
15682 if (OMP_PARALLEL_COMBINED (t))
15683 omp_parallel_combined_clauses = &tmp;
15684 stmt = begin_omp_parallel ();
15685 RECUR (OMP_PARALLEL_BODY (t));
15686 gcc_assert (omp_parallel_combined_clauses == NULL);
15687 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
15688 = OMP_PARALLEL_COMBINED (t);
15689 pop_omp_privatization_clauses (r);
15690 break;
15691
15692 case OMP_TASK:
15693 r = push_omp_privatization_clauses (false);
15694 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
15695 complain, in_decl);
15696 stmt = begin_omp_task ();
15697 RECUR (OMP_TASK_BODY (t));
15698 finish_omp_task (tmp, stmt);
15699 pop_omp_privatization_clauses (r);
15700 break;
15701
15702 case OMP_FOR:
15703 case OMP_SIMD:
15704 case CILK_SIMD:
15705 case CILK_FOR:
15706 case OMP_DISTRIBUTE:
15707 case OMP_TASKLOOP:
15708 case OACC_LOOP:
15709 {
15710 tree clauses, body, pre_body;
15711 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
15712 tree orig_declv = NULL_TREE;
15713 tree incrv = NULL_TREE;
15714 enum c_omp_region_type ort = C_ORT_OMP;
15715 int i;
15716
15717 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
15718 ort = C_ORT_CILK;
15719 else if (TREE_CODE (t) == OACC_LOOP)
15720 ort = C_ORT_ACC;
15721
15722 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
15723 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
15724 in_decl);
15725 if (OMP_FOR_INIT (t) != NULL_TREE)
15726 {
15727 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15728 if (OMP_FOR_ORIG_DECLS (t))
15729 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15730 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15731 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15732 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
15733 }
15734
15735 stmt = begin_omp_structured_block ();
15736
15737 pre_body = push_stmt_list ();
15738 RECUR (OMP_FOR_PRE_BODY (t));
15739 pre_body = pop_stmt_list (pre_body);
15740
15741 if (OMP_FOR_INIT (t) != NULL_TREE)
15742 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
15743 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
15744 incrv, &clauses, args, complain, in_decl,
15745 integral_constant_expression_p);
15746 omp_parallel_combined_clauses = NULL;
15747
15748 body = push_stmt_list ();
15749 RECUR (OMP_FOR_BODY (t));
15750 body = pop_stmt_list (body);
15751
15752 if (OMP_FOR_INIT (t) != NULL_TREE)
15753 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
15754 orig_declv, initv, condv, incrv, body, pre_body,
15755 NULL, clauses);
15756 else
15757 {
15758 t = make_node (TREE_CODE (t));
15759 TREE_TYPE (t) = void_type_node;
15760 OMP_FOR_BODY (t) = body;
15761 OMP_FOR_PRE_BODY (t) = pre_body;
15762 OMP_FOR_CLAUSES (t) = clauses;
15763 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
15764 add_stmt (t);
15765 }
15766
15767 add_stmt (finish_omp_structured_block (stmt));
15768 pop_omp_privatization_clauses (r);
15769 }
15770 break;
15771
15772 case OMP_SECTIONS:
15773 omp_parallel_combined_clauses = NULL;
15774 /* FALLTHRU */
15775 case OMP_SINGLE:
15776 case OMP_TEAMS:
15777 case OMP_CRITICAL:
15778 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
15779 && OMP_TEAMS_COMBINED (t));
15780 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
15781 in_decl);
15782 stmt = push_stmt_list ();
15783 RECUR (OMP_BODY (t));
15784 stmt = pop_stmt_list (stmt);
15785
15786 t = copy_node (t);
15787 OMP_BODY (t) = stmt;
15788 OMP_CLAUSES (t) = tmp;
15789 add_stmt (t);
15790 pop_omp_privatization_clauses (r);
15791 break;
15792
15793 case OACC_DATA:
15794 case OMP_TARGET_DATA:
15795 case OMP_TARGET:
15796 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
15797 ? C_ORT_ACC : C_ORT_OMP, args, complain,
15798 in_decl);
15799 keep_next_level (true);
15800 stmt = begin_omp_structured_block ();
15801
15802 RECUR (OMP_BODY (t));
15803 stmt = finish_omp_structured_block (stmt);
15804
15805 t = copy_node (t);
15806 OMP_BODY (t) = stmt;
15807 OMP_CLAUSES (t) = tmp;
15808 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
15809 {
15810 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
15811 if (teams)
15812 {
15813 /* For combined target teams, ensure the num_teams and
15814 thread_limit clause expressions are evaluated on the host,
15815 before entering the target construct. */
15816 tree c;
15817 for (c = OMP_TEAMS_CLAUSES (teams);
15818 c; c = OMP_CLAUSE_CHAIN (c))
15819 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15820 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15821 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15822 {
15823 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15824 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
15825 if (expr == error_mark_node)
15826 continue;
15827 tmp = TARGET_EXPR_SLOT (expr);
15828 add_stmt (expr);
15829 OMP_CLAUSE_OPERAND (c, 0) = expr;
15830 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15831 OMP_CLAUSE_FIRSTPRIVATE);
15832 OMP_CLAUSE_DECL (tc) = tmp;
15833 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
15834 OMP_TARGET_CLAUSES (t) = tc;
15835 }
15836 }
15837 }
15838 add_stmt (t);
15839 break;
15840
15841 case OACC_DECLARE:
15842 t = copy_node (t);
15843 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
15844 complain, in_decl);
15845 OACC_DECLARE_CLAUSES (t) = tmp;
15846 add_stmt (t);
15847 break;
15848
15849 case OMP_TARGET_UPDATE:
15850 case OMP_TARGET_ENTER_DATA:
15851 case OMP_TARGET_EXIT_DATA:
15852 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
15853 complain, in_decl);
15854 t = copy_node (t);
15855 OMP_STANDALONE_CLAUSES (t) = tmp;
15856 add_stmt (t);
15857 break;
15858
15859 case OACC_ENTER_DATA:
15860 case OACC_EXIT_DATA:
15861 case OACC_UPDATE:
15862 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
15863 complain, in_decl);
15864 t = copy_node (t);
15865 OMP_STANDALONE_CLAUSES (t) = tmp;
15866 add_stmt (t);
15867 break;
15868
15869 case OMP_ORDERED:
15870 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
15871 complain, in_decl);
15872 stmt = push_stmt_list ();
15873 RECUR (OMP_BODY (t));
15874 stmt = pop_stmt_list (stmt);
15875
15876 t = copy_node (t);
15877 OMP_BODY (t) = stmt;
15878 OMP_ORDERED_CLAUSES (t) = tmp;
15879 add_stmt (t);
15880 break;
15881
15882 case OMP_SECTION:
15883 case OMP_MASTER:
15884 case OMP_TASKGROUP:
15885 stmt = push_stmt_list ();
15886 RECUR (OMP_BODY (t));
15887 stmt = pop_stmt_list (stmt);
15888
15889 t = copy_node (t);
15890 OMP_BODY (t) = stmt;
15891 add_stmt (t);
15892 break;
15893
15894 case OMP_ATOMIC:
15895 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
15896 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
15897 {
15898 tree op1 = TREE_OPERAND (t, 1);
15899 tree rhs1 = NULL_TREE;
15900 tree lhs, rhs;
15901 if (TREE_CODE (op1) == COMPOUND_EXPR)
15902 {
15903 rhs1 = RECUR (TREE_OPERAND (op1, 0));
15904 op1 = TREE_OPERAND (op1, 1);
15905 }
15906 lhs = RECUR (TREE_OPERAND (op1, 0));
15907 rhs = RECUR (TREE_OPERAND (op1, 1));
15908 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
15909 NULL_TREE, NULL_TREE, rhs1,
15910 OMP_ATOMIC_SEQ_CST (t));
15911 }
15912 else
15913 {
15914 tree op1 = TREE_OPERAND (t, 1);
15915 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
15916 tree rhs1 = NULL_TREE;
15917 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
15918 enum tree_code opcode = NOP_EXPR;
15919 if (code == OMP_ATOMIC_READ)
15920 {
15921 v = RECUR (TREE_OPERAND (op1, 0));
15922 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15923 }
15924 else if (code == OMP_ATOMIC_CAPTURE_OLD
15925 || code == OMP_ATOMIC_CAPTURE_NEW)
15926 {
15927 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
15928 v = RECUR (TREE_OPERAND (op1, 0));
15929 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
15930 if (TREE_CODE (op11) == COMPOUND_EXPR)
15931 {
15932 rhs1 = RECUR (TREE_OPERAND (op11, 0));
15933 op11 = TREE_OPERAND (op11, 1);
15934 }
15935 lhs = RECUR (TREE_OPERAND (op11, 0));
15936 rhs = RECUR (TREE_OPERAND (op11, 1));
15937 opcode = TREE_CODE (op11);
15938 if (opcode == MODIFY_EXPR)
15939 opcode = NOP_EXPR;
15940 }
15941 else
15942 {
15943 code = OMP_ATOMIC;
15944 lhs = RECUR (TREE_OPERAND (op1, 0));
15945 rhs = RECUR (TREE_OPERAND (op1, 1));
15946 }
15947 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
15948 OMP_ATOMIC_SEQ_CST (t));
15949 }
15950 break;
15951
15952 case TRANSACTION_EXPR:
15953 {
15954 int flags = 0;
15955 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
15956 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
15957
15958 if (TRANSACTION_EXPR_IS_STMT (t))
15959 {
15960 tree body = TRANSACTION_EXPR_BODY (t);
15961 tree noex = NULL_TREE;
15962 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
15963 {
15964 noex = MUST_NOT_THROW_COND (body);
15965 if (noex == NULL_TREE)
15966 noex = boolean_true_node;
15967 body = TREE_OPERAND (body, 0);
15968 }
15969 stmt = begin_transaction_stmt (input_location, NULL, flags);
15970 RECUR (body);
15971 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
15972 }
15973 else
15974 {
15975 stmt = build_transaction_expr (EXPR_LOCATION (t),
15976 RECUR (TRANSACTION_EXPR_BODY (t)),
15977 flags, NULL_TREE);
15978 RETURN (stmt);
15979 }
15980 }
15981 break;
15982
15983 case MUST_NOT_THROW_EXPR:
15984 {
15985 tree op0 = RECUR (TREE_OPERAND (t, 0));
15986 tree cond = RECUR (MUST_NOT_THROW_COND (t));
15987 RETURN (build_must_not_throw_expr (op0, cond));
15988 }
15989
15990 case EXPR_PACK_EXPANSION:
15991 error ("invalid use of pack expansion expression");
15992 RETURN (error_mark_node);
15993
15994 case NONTYPE_ARGUMENT_PACK:
15995 error ("use %<...%> to expand argument pack");
15996 RETURN (error_mark_node);
15997
15998 case CILK_SPAWN_STMT:
15999 cfun->calls_cilk_spawn = 1;
16000 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16001
16002 case CILK_SYNC_STMT:
16003 RETURN (build_cilk_sync ());
16004
16005 case COMPOUND_EXPR:
16006 tmp = RECUR (TREE_OPERAND (t, 0));
16007 if (tmp == NULL_TREE)
16008 /* If the first operand was a statement, we're done with it. */
16009 RETURN (RECUR (TREE_OPERAND (t, 1)));
16010 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16011 RECUR (TREE_OPERAND (t, 1)),
16012 complain));
16013
16014 case ANNOTATE_EXPR:
16015 tmp = RECUR (TREE_OPERAND (t, 0));
16016 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16017 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16018
16019 default:
16020 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16021
16022 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16023 /*function_p=*/false,
16024 integral_constant_expression_p));
16025 }
16026
16027 RETURN (NULL_TREE);
16028 out:
16029 input_location = loc;
16030 return r;
16031 #undef RECUR
16032 #undef RETURN
16033 }
16034
16035 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16036 function. For description of the body see comment above
16037 cp_parser_omp_declare_reduction_exprs. */
16038
16039 static void
16040 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16041 {
16042 if (t == NULL_TREE || t == error_mark_node)
16043 return;
16044
16045 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16046
16047 tree_stmt_iterator tsi;
16048 int i;
16049 tree stmts[7];
16050 memset (stmts, 0, sizeof stmts);
16051 for (i = 0, tsi = tsi_start (t);
16052 i < 7 && !tsi_end_p (tsi);
16053 i++, tsi_next (&tsi))
16054 stmts[i] = tsi_stmt (tsi);
16055 gcc_assert (tsi_end_p (tsi));
16056
16057 if (i >= 3)
16058 {
16059 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16060 && TREE_CODE (stmts[1]) == DECL_EXPR);
16061 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16062 args, complain, in_decl);
16063 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16064 args, complain, in_decl);
16065 DECL_CONTEXT (omp_out) = current_function_decl;
16066 DECL_CONTEXT (omp_in) = current_function_decl;
16067 keep_next_level (true);
16068 tree block = begin_omp_structured_block ();
16069 tsubst_expr (stmts[2], args, complain, in_decl, false);
16070 block = finish_omp_structured_block (block);
16071 block = maybe_cleanup_point_expr_void (block);
16072 add_decl_expr (omp_out);
16073 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16074 TREE_NO_WARNING (omp_out) = 1;
16075 add_decl_expr (omp_in);
16076 finish_expr_stmt (block);
16077 }
16078 if (i >= 6)
16079 {
16080 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16081 && TREE_CODE (stmts[4]) == DECL_EXPR);
16082 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16083 args, complain, in_decl);
16084 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16085 args, complain, in_decl);
16086 DECL_CONTEXT (omp_priv) = current_function_decl;
16087 DECL_CONTEXT (omp_orig) = current_function_decl;
16088 keep_next_level (true);
16089 tree block = begin_omp_structured_block ();
16090 tsubst_expr (stmts[5], args, complain, in_decl, false);
16091 block = finish_omp_structured_block (block);
16092 block = maybe_cleanup_point_expr_void (block);
16093 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16094 add_decl_expr (omp_priv);
16095 add_decl_expr (omp_orig);
16096 finish_expr_stmt (block);
16097 if (i == 7)
16098 add_decl_expr (omp_orig);
16099 }
16100 }
16101
16102 /* T is a postfix-expression that is not being used in a function
16103 call. Return the substituted version of T. */
16104
16105 static tree
16106 tsubst_non_call_postfix_expression (tree t, tree args,
16107 tsubst_flags_t complain,
16108 tree in_decl)
16109 {
16110 if (TREE_CODE (t) == SCOPE_REF)
16111 t = tsubst_qualified_id (t, args, complain, in_decl,
16112 /*done=*/false, /*address_p=*/false);
16113 else
16114 t = tsubst_copy_and_build (t, args, complain, in_decl,
16115 /*function_p=*/false,
16116 /*integral_constant_expression_p=*/false);
16117
16118 return t;
16119 }
16120
16121 /* Like tsubst but deals with expressions and performs semantic
16122 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16123
16124 tree
16125 tsubst_copy_and_build (tree t,
16126 tree args,
16127 tsubst_flags_t complain,
16128 tree in_decl,
16129 bool function_p,
16130 bool integral_constant_expression_p)
16131 {
16132 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16133 #define RECUR(NODE) \
16134 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16135 /*function_p=*/false, \
16136 integral_constant_expression_p)
16137
16138 tree retval, op1;
16139 location_t loc;
16140
16141 if (t == NULL_TREE || t == error_mark_node)
16142 return t;
16143
16144 loc = input_location;
16145 if (EXPR_HAS_LOCATION (t))
16146 input_location = EXPR_LOCATION (t);
16147
16148 /* N3276 decltype magic only applies to calls at the top level or on the
16149 right side of a comma. */
16150 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16151 complain &= ~tf_decltype;
16152
16153 switch (TREE_CODE (t))
16154 {
16155 case USING_DECL:
16156 t = DECL_NAME (t);
16157 /* Fall through. */
16158 case IDENTIFIER_NODE:
16159 {
16160 tree decl;
16161 cp_id_kind idk;
16162 bool non_integral_constant_expression_p;
16163 const char *error_msg;
16164
16165 if (IDENTIFIER_TYPENAME_P (t))
16166 {
16167 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16168 t = mangle_conv_op_name_for_type (new_type);
16169 }
16170
16171 /* Look up the name. */
16172 decl = lookup_name (t);
16173
16174 /* By convention, expressions use ERROR_MARK_NODE to indicate
16175 failure, not NULL_TREE. */
16176 if (decl == NULL_TREE)
16177 decl = error_mark_node;
16178
16179 decl = finish_id_expression (t, decl, NULL_TREE,
16180 &idk,
16181 integral_constant_expression_p,
16182 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16183 &non_integral_constant_expression_p,
16184 /*template_p=*/false,
16185 /*done=*/true,
16186 /*address_p=*/false,
16187 /*template_arg_p=*/false,
16188 &error_msg,
16189 input_location);
16190 if (error_msg)
16191 error (error_msg);
16192 if (!function_p && identifier_p (decl))
16193 {
16194 if (complain & tf_error)
16195 unqualified_name_lookup_error (decl);
16196 decl = error_mark_node;
16197 }
16198 RETURN (decl);
16199 }
16200
16201 case TEMPLATE_ID_EXPR:
16202 {
16203 tree object;
16204 tree templ = RECUR (TREE_OPERAND (t, 0));
16205 tree targs = TREE_OPERAND (t, 1);
16206
16207 if (targs)
16208 targs = tsubst_template_args (targs, args, complain, in_decl);
16209 if (targs == error_mark_node)
16210 return error_mark_node;
16211
16212 if (variable_template_p (templ))
16213 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16214
16215 if (TREE_CODE (templ) == COMPONENT_REF)
16216 {
16217 object = TREE_OPERAND (templ, 0);
16218 templ = TREE_OPERAND (templ, 1);
16219 }
16220 else
16221 object = NULL_TREE;
16222 templ = lookup_template_function (templ, targs);
16223
16224 if (object)
16225 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16226 object, templ, NULL_TREE));
16227 else
16228 RETURN (baselink_for_fns (templ));
16229 }
16230
16231 case INDIRECT_REF:
16232 {
16233 tree r = RECUR (TREE_OPERAND (t, 0));
16234
16235 if (REFERENCE_REF_P (t))
16236 {
16237 /* A type conversion to reference type will be enclosed in
16238 such an indirect ref, but the substitution of the cast
16239 will have also added such an indirect ref. */
16240 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16241 r = convert_from_reference (r);
16242 }
16243 else
16244 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16245 complain|decltype_flag);
16246
16247 if (TREE_CODE (r) == INDIRECT_REF)
16248 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16249
16250 RETURN (r);
16251 }
16252
16253 case NOP_EXPR:
16254 {
16255 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16256 tree op0 = RECUR (TREE_OPERAND (t, 0));
16257 RETURN (build_nop (type, op0));
16258 }
16259
16260 case IMPLICIT_CONV_EXPR:
16261 {
16262 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16263 tree expr = RECUR (TREE_OPERAND (t, 0));
16264 int flags = LOOKUP_IMPLICIT;
16265 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16266 flags = LOOKUP_NORMAL;
16267 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16268 flags));
16269 }
16270
16271 case CONVERT_EXPR:
16272 {
16273 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16274 tree op0 = RECUR (TREE_OPERAND (t, 0));
16275 RETURN (build1 (CONVERT_EXPR, type, op0));
16276 }
16277
16278 case CAST_EXPR:
16279 case REINTERPRET_CAST_EXPR:
16280 case CONST_CAST_EXPR:
16281 case DYNAMIC_CAST_EXPR:
16282 case STATIC_CAST_EXPR:
16283 {
16284 tree type;
16285 tree op, r = NULL_TREE;
16286
16287 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16288 if (integral_constant_expression_p
16289 && !cast_valid_in_integral_constant_expression_p (type))
16290 {
16291 if (complain & tf_error)
16292 error ("a cast to a type other than an integral or "
16293 "enumeration type cannot appear in a constant-expression");
16294 RETURN (error_mark_node);
16295 }
16296
16297 op = RECUR (TREE_OPERAND (t, 0));
16298
16299 warning_sentinel s(warn_useless_cast);
16300 switch (TREE_CODE (t))
16301 {
16302 case CAST_EXPR:
16303 r = build_functional_cast (type, op, complain);
16304 break;
16305 case REINTERPRET_CAST_EXPR:
16306 r = build_reinterpret_cast (type, op, complain);
16307 break;
16308 case CONST_CAST_EXPR:
16309 r = build_const_cast (type, op, complain);
16310 break;
16311 case DYNAMIC_CAST_EXPR:
16312 r = build_dynamic_cast (type, op, complain);
16313 break;
16314 case STATIC_CAST_EXPR:
16315 r = build_static_cast (type, op, complain);
16316 break;
16317 default:
16318 gcc_unreachable ();
16319 }
16320
16321 RETURN (r);
16322 }
16323
16324 case POSTDECREMENT_EXPR:
16325 case POSTINCREMENT_EXPR:
16326 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16327 args, complain, in_decl);
16328 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16329 complain|decltype_flag));
16330
16331 case PREDECREMENT_EXPR:
16332 case PREINCREMENT_EXPR:
16333 case NEGATE_EXPR:
16334 case BIT_NOT_EXPR:
16335 case ABS_EXPR:
16336 case TRUTH_NOT_EXPR:
16337 case UNARY_PLUS_EXPR: /* Unary + */
16338 case REALPART_EXPR:
16339 case IMAGPART_EXPR:
16340 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16341 RECUR (TREE_OPERAND (t, 0)),
16342 complain|decltype_flag));
16343
16344 case FIX_TRUNC_EXPR:
16345 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16346 false, complain));
16347
16348 case ADDR_EXPR:
16349 op1 = TREE_OPERAND (t, 0);
16350 if (TREE_CODE (op1) == LABEL_DECL)
16351 RETURN (finish_label_address_expr (DECL_NAME (op1),
16352 EXPR_LOCATION (op1)));
16353 if (TREE_CODE (op1) == SCOPE_REF)
16354 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16355 /*done=*/true, /*address_p=*/true);
16356 else
16357 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16358 in_decl);
16359 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16360 complain|decltype_flag));
16361
16362 case PLUS_EXPR:
16363 case MINUS_EXPR:
16364 case MULT_EXPR:
16365 case TRUNC_DIV_EXPR:
16366 case CEIL_DIV_EXPR:
16367 case FLOOR_DIV_EXPR:
16368 case ROUND_DIV_EXPR:
16369 case EXACT_DIV_EXPR:
16370 case BIT_AND_EXPR:
16371 case BIT_IOR_EXPR:
16372 case BIT_XOR_EXPR:
16373 case TRUNC_MOD_EXPR:
16374 case FLOOR_MOD_EXPR:
16375 case TRUTH_ANDIF_EXPR:
16376 case TRUTH_ORIF_EXPR:
16377 case TRUTH_AND_EXPR:
16378 case TRUTH_OR_EXPR:
16379 case RSHIFT_EXPR:
16380 case LSHIFT_EXPR:
16381 case RROTATE_EXPR:
16382 case LROTATE_EXPR:
16383 case EQ_EXPR:
16384 case NE_EXPR:
16385 case MAX_EXPR:
16386 case MIN_EXPR:
16387 case LE_EXPR:
16388 case GE_EXPR:
16389 case LT_EXPR:
16390 case GT_EXPR:
16391 case MEMBER_REF:
16392 case DOTSTAR_EXPR:
16393 {
16394 warning_sentinel s1(warn_type_limits);
16395 warning_sentinel s2(warn_div_by_zero);
16396 warning_sentinel s3(warn_logical_op);
16397 warning_sentinel s4(warn_tautological_compare);
16398 tree op0 = RECUR (TREE_OPERAND (t, 0));
16399 tree op1 = RECUR (TREE_OPERAND (t, 1));
16400 tree r = build_x_binary_op
16401 (input_location, TREE_CODE (t),
16402 op0,
16403 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16404 ? ERROR_MARK
16405 : TREE_CODE (TREE_OPERAND (t, 0))),
16406 op1,
16407 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16408 ? ERROR_MARK
16409 : TREE_CODE (TREE_OPERAND (t, 1))),
16410 /*overload=*/NULL,
16411 complain|decltype_flag);
16412 if (EXPR_P (r) && TREE_NO_WARNING (t))
16413 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16414
16415 RETURN (r);
16416 }
16417
16418 case POINTER_PLUS_EXPR:
16419 {
16420 tree op0 = RECUR (TREE_OPERAND (t, 0));
16421 tree op1 = RECUR (TREE_OPERAND (t, 1));
16422 return fold_build_pointer_plus (op0, op1);
16423 }
16424
16425 case SCOPE_REF:
16426 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16427 /*address_p=*/false));
16428 case ARRAY_REF:
16429 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16430 args, complain, in_decl);
16431 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16432 RECUR (TREE_OPERAND (t, 1)),
16433 complain|decltype_flag));
16434
16435 case ARRAY_NOTATION_REF:
16436 {
16437 tree start_index, length, stride;
16438 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16439 args, complain, in_decl);
16440 start_index = RECUR (ARRAY_NOTATION_START (t));
16441 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16442 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16443 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16444 length, stride, TREE_TYPE (op1)));
16445 }
16446 case SIZEOF_EXPR:
16447 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16448 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16449 RETURN (tsubst_copy (t, args, complain, in_decl));
16450 /* Fall through */
16451
16452 case ALIGNOF_EXPR:
16453 {
16454 tree r;
16455
16456 op1 = TREE_OPERAND (t, 0);
16457 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16458 op1 = TREE_TYPE (op1);
16459 if (!args)
16460 {
16461 /* When there are no ARGS, we are trying to evaluate a
16462 non-dependent expression from the parser. Trying to do
16463 the substitutions may not work. */
16464 if (!TYPE_P (op1))
16465 op1 = TREE_TYPE (op1);
16466 }
16467 else
16468 {
16469 ++cp_unevaluated_operand;
16470 ++c_inhibit_evaluation_warnings;
16471 if (TYPE_P (op1))
16472 op1 = tsubst (op1, args, complain, in_decl);
16473 else
16474 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16475 /*function_p=*/false,
16476 /*integral_constant_expression_p=*/
16477 false);
16478 --cp_unevaluated_operand;
16479 --c_inhibit_evaluation_warnings;
16480 }
16481 if (TYPE_P (op1))
16482 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16483 complain & tf_error);
16484 else
16485 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16486 complain & tf_error);
16487 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16488 {
16489 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16490 {
16491 if (!processing_template_decl && TYPE_P (op1))
16492 {
16493 r = build_min (SIZEOF_EXPR, size_type_node,
16494 build1 (NOP_EXPR, op1, error_mark_node));
16495 SIZEOF_EXPR_TYPE_P (r) = 1;
16496 }
16497 else
16498 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16499 TREE_SIDE_EFFECTS (r) = 0;
16500 TREE_READONLY (r) = 1;
16501 }
16502 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16503 }
16504 RETURN (r);
16505 }
16506
16507 case AT_ENCODE_EXPR:
16508 {
16509 op1 = TREE_OPERAND (t, 0);
16510 ++cp_unevaluated_operand;
16511 ++c_inhibit_evaluation_warnings;
16512 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16513 /*function_p=*/false,
16514 /*integral_constant_expression_p=*/false);
16515 --cp_unevaluated_operand;
16516 --c_inhibit_evaluation_warnings;
16517 RETURN (objc_build_encode_expr (op1));
16518 }
16519
16520 case NOEXCEPT_EXPR:
16521 op1 = TREE_OPERAND (t, 0);
16522 ++cp_unevaluated_operand;
16523 ++c_inhibit_evaluation_warnings;
16524 ++cp_noexcept_operand;
16525 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16526 /*function_p=*/false,
16527 /*integral_constant_expression_p=*/false);
16528 --cp_unevaluated_operand;
16529 --c_inhibit_evaluation_warnings;
16530 --cp_noexcept_operand;
16531 RETURN (finish_noexcept_expr (op1, complain));
16532
16533 case MODOP_EXPR:
16534 {
16535 warning_sentinel s(warn_div_by_zero);
16536 tree lhs = RECUR (TREE_OPERAND (t, 0));
16537 tree rhs = RECUR (TREE_OPERAND (t, 2));
16538 tree r = build_x_modify_expr
16539 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16540 complain|decltype_flag);
16541 /* TREE_NO_WARNING must be set if either the expression was
16542 parenthesized or it uses an operator such as >>= rather
16543 than plain assignment. In the former case, it was already
16544 set and must be copied. In the latter case,
16545 build_x_modify_expr sets it and it must not be reset
16546 here. */
16547 if (TREE_NO_WARNING (t))
16548 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16549
16550 RETURN (r);
16551 }
16552
16553 case ARROW_EXPR:
16554 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16555 args, complain, in_decl);
16556 /* Remember that there was a reference to this entity. */
16557 if (DECL_P (op1)
16558 && !mark_used (op1, complain) && !(complain & tf_error))
16559 RETURN (error_mark_node);
16560 RETURN (build_x_arrow (input_location, op1, complain));
16561
16562 case NEW_EXPR:
16563 {
16564 tree placement = RECUR (TREE_OPERAND (t, 0));
16565 tree init = RECUR (TREE_OPERAND (t, 3));
16566 vec<tree, va_gc> *placement_vec;
16567 vec<tree, va_gc> *init_vec;
16568 tree ret;
16569
16570 if (placement == NULL_TREE)
16571 placement_vec = NULL;
16572 else
16573 {
16574 placement_vec = make_tree_vector ();
16575 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16576 vec_safe_push (placement_vec, TREE_VALUE (placement));
16577 }
16578
16579 /* If there was an initializer in the original tree, but it
16580 instantiated to an empty list, then we should pass a
16581 non-NULL empty vector to tell build_new that it was an
16582 empty initializer() rather than no initializer. This can
16583 only happen when the initializer is a pack expansion whose
16584 parameter packs are of length zero. */
16585 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16586 init_vec = NULL;
16587 else
16588 {
16589 init_vec = make_tree_vector ();
16590 if (init == void_node)
16591 gcc_assert (init_vec != NULL);
16592 else
16593 {
16594 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16595 vec_safe_push (init_vec, TREE_VALUE (init));
16596 }
16597 }
16598
16599 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16600 tree op2 = RECUR (TREE_OPERAND (t, 2));
16601 ret = build_new (&placement_vec, op1, op2, &init_vec,
16602 NEW_EXPR_USE_GLOBAL (t),
16603 complain);
16604
16605 if (placement_vec != NULL)
16606 release_tree_vector (placement_vec);
16607 if (init_vec != NULL)
16608 release_tree_vector (init_vec);
16609
16610 RETURN (ret);
16611 }
16612
16613 case DELETE_EXPR:
16614 {
16615 tree op0 = RECUR (TREE_OPERAND (t, 0));
16616 tree op1 = RECUR (TREE_OPERAND (t, 1));
16617 RETURN (delete_sanity (op0, op1,
16618 DELETE_EXPR_USE_VEC (t),
16619 DELETE_EXPR_USE_GLOBAL (t),
16620 complain));
16621 }
16622
16623 case COMPOUND_EXPR:
16624 {
16625 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
16626 complain & ~tf_decltype, in_decl,
16627 /*function_p=*/false,
16628 integral_constant_expression_p);
16629 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
16630 op0,
16631 RECUR (TREE_OPERAND (t, 1)),
16632 complain|decltype_flag));
16633 }
16634
16635 case CALL_EXPR:
16636 {
16637 tree function;
16638 vec<tree, va_gc> *call_args;
16639 unsigned int nargs, i;
16640 bool qualified_p;
16641 bool koenig_p;
16642 tree ret;
16643
16644 function = CALL_EXPR_FN (t);
16645 /* Internal function with no arguments. */
16646 if (function == NULL_TREE && call_expr_nargs (t) == 0)
16647 RETURN (t);
16648
16649 /* When we parsed the expression, we determined whether or
16650 not Koenig lookup should be performed. */
16651 koenig_p = KOENIG_LOOKUP_P (t);
16652 if (function == NULL_TREE)
16653 {
16654 koenig_p = false;
16655 qualified_p = false;
16656 }
16657 else if (TREE_CODE (function) == SCOPE_REF)
16658 {
16659 qualified_p = true;
16660 function = tsubst_qualified_id (function, args, complain, in_decl,
16661 /*done=*/false,
16662 /*address_p=*/false);
16663 }
16664 else if (koenig_p && identifier_p (function))
16665 {
16666 /* Do nothing; calling tsubst_copy_and_build on an identifier
16667 would incorrectly perform unqualified lookup again.
16668
16669 Note that we can also have an IDENTIFIER_NODE if the earlier
16670 unqualified lookup found a member function; in that case
16671 koenig_p will be false and we do want to do the lookup
16672 again to find the instantiated member function.
16673
16674 FIXME but doing that causes c++/15272, so we need to stop
16675 using IDENTIFIER_NODE in that situation. */
16676 qualified_p = false;
16677 }
16678 else
16679 {
16680 if (TREE_CODE (function) == COMPONENT_REF)
16681 {
16682 tree op = TREE_OPERAND (function, 1);
16683
16684 qualified_p = (TREE_CODE (op) == SCOPE_REF
16685 || (BASELINK_P (op)
16686 && BASELINK_QUALIFIED_P (op)));
16687 }
16688 else
16689 qualified_p = false;
16690
16691 if (TREE_CODE (function) == ADDR_EXPR
16692 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
16693 /* Avoid error about taking the address of a constructor. */
16694 function = TREE_OPERAND (function, 0);
16695
16696 function = tsubst_copy_and_build (function, args, complain,
16697 in_decl,
16698 !qualified_p,
16699 integral_constant_expression_p);
16700
16701 if (BASELINK_P (function))
16702 qualified_p = true;
16703 }
16704
16705 nargs = call_expr_nargs (t);
16706 call_args = make_tree_vector ();
16707 for (i = 0; i < nargs; ++i)
16708 {
16709 tree arg = CALL_EXPR_ARG (t, i);
16710
16711 if (!PACK_EXPANSION_P (arg))
16712 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
16713 else
16714 {
16715 /* Expand the pack expansion and push each entry onto
16716 CALL_ARGS. */
16717 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
16718 if (TREE_CODE (arg) == TREE_VEC)
16719 {
16720 unsigned int len, j;
16721
16722 len = TREE_VEC_LENGTH (arg);
16723 for (j = 0; j < len; ++j)
16724 {
16725 tree value = TREE_VEC_ELT (arg, j);
16726 if (value != NULL_TREE)
16727 value = convert_from_reference (value);
16728 vec_safe_push (call_args, value);
16729 }
16730 }
16731 else
16732 {
16733 /* A partial substitution. Add one entry. */
16734 vec_safe_push (call_args, arg);
16735 }
16736 }
16737 }
16738
16739 /* We do not perform argument-dependent lookup if normal
16740 lookup finds a non-function, in accordance with the
16741 expected resolution of DR 218. */
16742 if (koenig_p
16743 && ((is_overloaded_fn (function)
16744 /* If lookup found a member function, the Koenig lookup is
16745 not appropriate, even if an unqualified-name was used
16746 to denote the function. */
16747 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
16748 || identifier_p (function))
16749 /* Only do this when substitution turns a dependent call
16750 into a non-dependent call. */
16751 && type_dependent_expression_p_push (t)
16752 && !any_type_dependent_arguments_p (call_args))
16753 function = perform_koenig_lookup (function, call_args, tf_none);
16754
16755 if (function != NULL_TREE
16756 && identifier_p (function)
16757 && !any_type_dependent_arguments_p (call_args))
16758 {
16759 if (koenig_p && (complain & tf_warning_or_error))
16760 {
16761 /* For backwards compatibility and good diagnostics, try
16762 the unqualified lookup again if we aren't in SFINAE
16763 context. */
16764 tree unq = (tsubst_copy_and_build
16765 (function, args, complain, in_decl, true,
16766 integral_constant_expression_p));
16767 if (unq == error_mark_node)
16768 {
16769 release_tree_vector (call_args);
16770 RETURN (error_mark_node);
16771 }
16772
16773 if (unq != function)
16774 {
16775 tree fn = unq;
16776 if (INDIRECT_REF_P (fn))
16777 fn = TREE_OPERAND (fn, 0);
16778 if (TREE_CODE (fn) == COMPONENT_REF)
16779 fn = TREE_OPERAND (fn, 1);
16780 if (is_overloaded_fn (fn))
16781 fn = get_first_fn (fn);
16782 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
16783 "%qD was not declared in this scope, "
16784 "and no declarations were found by "
16785 "argument-dependent lookup at the point "
16786 "of instantiation", function))
16787 {
16788 if (!DECL_P (fn))
16789 /* Can't say anything more. */;
16790 else if (DECL_CLASS_SCOPE_P (fn))
16791 {
16792 location_t loc = EXPR_LOC_OR_LOC (t,
16793 input_location);
16794 inform (loc,
16795 "declarations in dependent base %qT are "
16796 "not found by unqualified lookup",
16797 DECL_CLASS_CONTEXT (fn));
16798 if (current_class_ptr)
16799 inform (loc,
16800 "use %<this->%D%> instead", function);
16801 else
16802 inform (loc,
16803 "use %<%T::%D%> instead",
16804 current_class_name, function);
16805 }
16806 else
16807 inform (DECL_SOURCE_LOCATION (fn),
16808 "%qD declared here, later in the "
16809 "translation unit", fn);
16810 }
16811 function = unq;
16812 }
16813 }
16814 if (identifier_p (function))
16815 {
16816 if (complain & tf_error)
16817 unqualified_name_lookup_error (function);
16818 release_tree_vector (call_args);
16819 RETURN (error_mark_node);
16820 }
16821 }
16822
16823 /* Remember that there was a reference to this entity. */
16824 if (function != NULL_TREE
16825 && DECL_P (function)
16826 && !mark_used (function, complain) && !(complain & tf_error))
16827 {
16828 release_tree_vector (call_args);
16829 RETURN (error_mark_node);
16830 }
16831
16832 /* Put back tf_decltype for the actual call. */
16833 complain |= decltype_flag;
16834
16835 if (function == NULL_TREE)
16836 switch (CALL_EXPR_IFN (t))
16837 {
16838 case IFN_LAUNDER:
16839 gcc_assert (nargs == 1);
16840 if (vec_safe_length (call_args) != 1)
16841 {
16842 error_at (EXPR_LOC_OR_LOC (t, input_location),
16843 "wrong number of arguments to "
16844 "%<__builtin_launder%>");
16845 ret = error_mark_node;
16846 }
16847 else
16848 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
16849 input_location),
16850 (*call_args)[0], complain);
16851 break;
16852
16853 default:
16854 /* Unsupported internal function with arguments. */
16855 gcc_unreachable ();
16856 }
16857 else if (TREE_CODE (function) == OFFSET_REF)
16858 ret = build_offset_ref_call_from_tree (function, &call_args,
16859 complain);
16860 else if (TREE_CODE (function) == COMPONENT_REF)
16861 {
16862 tree instance = TREE_OPERAND (function, 0);
16863 tree fn = TREE_OPERAND (function, 1);
16864
16865 if (processing_template_decl
16866 && (type_dependent_expression_p (instance)
16867 || (!BASELINK_P (fn)
16868 && TREE_CODE (fn) != FIELD_DECL)
16869 || type_dependent_expression_p (fn)
16870 || any_type_dependent_arguments_p (call_args)))
16871 ret = build_nt_call_vec (function, call_args);
16872 else if (!BASELINK_P (fn))
16873 ret = finish_call_expr (function, &call_args,
16874 /*disallow_virtual=*/false,
16875 /*koenig_p=*/false,
16876 complain);
16877 else
16878 ret = (build_new_method_call
16879 (instance, fn,
16880 &call_args, NULL_TREE,
16881 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
16882 /*fn_p=*/NULL,
16883 complain));
16884 }
16885 else
16886 ret = finish_call_expr (function, &call_args,
16887 /*disallow_virtual=*/qualified_p,
16888 koenig_p,
16889 complain);
16890
16891 release_tree_vector (call_args);
16892
16893 if (ret != error_mark_node)
16894 {
16895 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
16896 bool ord = CALL_EXPR_ORDERED_ARGS (t);
16897 bool rev = CALL_EXPR_REVERSE_ARGS (t);
16898 bool thk = CALL_FROM_THUNK_P (t);
16899 if (op || ord || rev || thk)
16900 {
16901 function = extract_call_expr (ret);
16902 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
16903 CALL_EXPR_ORDERED_ARGS (function) = ord;
16904 CALL_EXPR_REVERSE_ARGS (function) = rev;
16905 if (thk)
16906 {
16907 CALL_FROM_THUNK_P (function) = true;
16908 /* The thunk location is not interesting. */
16909 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
16910 }
16911 }
16912 }
16913
16914 RETURN (ret);
16915 }
16916
16917 case COND_EXPR:
16918 {
16919 tree cond = RECUR (TREE_OPERAND (t, 0));
16920 tree folded_cond = fold_non_dependent_expr (cond);
16921 tree exp1, exp2;
16922
16923 if (TREE_CODE (folded_cond) == INTEGER_CST)
16924 {
16925 if (integer_zerop (folded_cond))
16926 {
16927 ++c_inhibit_evaluation_warnings;
16928 exp1 = RECUR (TREE_OPERAND (t, 1));
16929 --c_inhibit_evaluation_warnings;
16930 exp2 = RECUR (TREE_OPERAND (t, 2));
16931 }
16932 else
16933 {
16934 exp1 = RECUR (TREE_OPERAND (t, 1));
16935 ++c_inhibit_evaluation_warnings;
16936 exp2 = RECUR (TREE_OPERAND (t, 2));
16937 --c_inhibit_evaluation_warnings;
16938 }
16939 cond = folded_cond;
16940 }
16941 else
16942 {
16943 exp1 = RECUR (TREE_OPERAND (t, 1));
16944 exp2 = RECUR (TREE_OPERAND (t, 2));
16945 }
16946
16947 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
16948 cond, exp1, exp2, complain));
16949 }
16950
16951 case PSEUDO_DTOR_EXPR:
16952 {
16953 tree op0 = RECUR (TREE_OPERAND (t, 0));
16954 tree op1 = RECUR (TREE_OPERAND (t, 1));
16955 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
16956 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
16957 input_location));
16958 }
16959
16960 case TREE_LIST:
16961 {
16962 tree purpose, value, chain;
16963
16964 if (t == void_list_node)
16965 RETURN (t);
16966
16967 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
16968 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
16969 {
16970 /* We have pack expansions, so expand those and
16971 create a new list out of it. */
16972 tree purposevec = NULL_TREE;
16973 tree valuevec = NULL_TREE;
16974 tree chain;
16975 int i, len = -1;
16976
16977 /* Expand the argument expressions. */
16978 if (TREE_PURPOSE (t))
16979 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
16980 complain, in_decl);
16981 if (TREE_VALUE (t))
16982 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
16983 complain, in_decl);
16984
16985 /* Build the rest of the list. */
16986 chain = TREE_CHAIN (t);
16987 if (chain && chain != void_type_node)
16988 chain = RECUR (chain);
16989
16990 /* Determine the number of arguments. */
16991 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
16992 {
16993 len = TREE_VEC_LENGTH (purposevec);
16994 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
16995 }
16996 else if (TREE_CODE (valuevec) == TREE_VEC)
16997 len = TREE_VEC_LENGTH (valuevec);
16998 else
16999 {
17000 /* Since we only performed a partial substitution into
17001 the argument pack, we only RETURN (a single list
17002 node. */
17003 if (purposevec == TREE_PURPOSE (t)
17004 && valuevec == TREE_VALUE (t)
17005 && chain == TREE_CHAIN (t))
17006 RETURN (t);
17007
17008 RETURN (tree_cons (purposevec, valuevec, chain));
17009 }
17010
17011 /* Convert the argument vectors into a TREE_LIST */
17012 i = len;
17013 while (i > 0)
17014 {
17015 /* Grab the Ith values. */
17016 i--;
17017 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17018 : NULL_TREE;
17019 value
17020 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17021 : NULL_TREE;
17022
17023 /* Build the list (backwards). */
17024 chain = tree_cons (purpose, value, chain);
17025 }
17026
17027 RETURN (chain);
17028 }
17029
17030 purpose = TREE_PURPOSE (t);
17031 if (purpose)
17032 purpose = RECUR (purpose);
17033 value = TREE_VALUE (t);
17034 if (value)
17035 value = RECUR (value);
17036 chain = TREE_CHAIN (t);
17037 if (chain && chain != void_type_node)
17038 chain = RECUR (chain);
17039 if (purpose == TREE_PURPOSE (t)
17040 && value == TREE_VALUE (t)
17041 && chain == TREE_CHAIN (t))
17042 RETURN (t);
17043 RETURN (tree_cons (purpose, value, chain));
17044 }
17045
17046 case COMPONENT_REF:
17047 {
17048 tree object;
17049 tree object_type;
17050 tree member;
17051 tree r;
17052
17053 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17054 args, complain, in_decl);
17055 /* Remember that there was a reference to this entity. */
17056 if (DECL_P (object)
17057 && !mark_used (object, complain) && !(complain & tf_error))
17058 RETURN (error_mark_node);
17059 object_type = TREE_TYPE (object);
17060
17061 member = TREE_OPERAND (t, 1);
17062 if (BASELINK_P (member))
17063 member = tsubst_baselink (member,
17064 non_reference (TREE_TYPE (object)),
17065 args, complain, in_decl);
17066 else
17067 member = tsubst_copy (member, args, complain, in_decl);
17068 if (member == error_mark_node)
17069 RETURN (error_mark_node);
17070
17071 if (type_dependent_expression_p (object))
17072 /* We can't do much here. */;
17073 else if (!CLASS_TYPE_P (object_type))
17074 {
17075 if (scalarish_type_p (object_type))
17076 {
17077 tree s = NULL_TREE;
17078 tree dtor = member;
17079
17080 if (TREE_CODE (dtor) == SCOPE_REF)
17081 {
17082 s = TREE_OPERAND (dtor, 0);
17083 dtor = TREE_OPERAND (dtor, 1);
17084 }
17085 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17086 {
17087 dtor = TREE_OPERAND (dtor, 0);
17088 if (TYPE_P (dtor))
17089 RETURN (finish_pseudo_destructor_expr
17090 (object, s, dtor, input_location));
17091 }
17092 }
17093 }
17094 else if (TREE_CODE (member) == SCOPE_REF
17095 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17096 {
17097 /* Lookup the template functions now that we know what the
17098 scope is. */
17099 tree scope = TREE_OPERAND (member, 0);
17100 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17101 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17102 member = lookup_qualified_name (scope, tmpl,
17103 /*is_type_p=*/false,
17104 /*complain=*/false);
17105 if (BASELINK_P (member))
17106 {
17107 BASELINK_FUNCTIONS (member)
17108 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17109 args);
17110 member = (adjust_result_of_qualified_name_lookup
17111 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17112 object_type));
17113 }
17114 else
17115 {
17116 qualified_name_lookup_error (scope, tmpl, member,
17117 input_location);
17118 RETURN (error_mark_node);
17119 }
17120 }
17121 else if (TREE_CODE (member) == SCOPE_REF
17122 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17123 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17124 {
17125 if (complain & tf_error)
17126 {
17127 if (TYPE_P (TREE_OPERAND (member, 0)))
17128 error ("%qT is not a class or namespace",
17129 TREE_OPERAND (member, 0));
17130 else
17131 error ("%qD is not a class or namespace",
17132 TREE_OPERAND (member, 0));
17133 }
17134 RETURN (error_mark_node);
17135 }
17136 else if (TREE_CODE (member) == FIELD_DECL)
17137 {
17138 r = finish_non_static_data_member (member, object, NULL_TREE);
17139 if (TREE_CODE (r) == COMPONENT_REF)
17140 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17141 RETURN (r);
17142 }
17143
17144 r = finish_class_member_access_expr (object, member,
17145 /*template_p=*/false,
17146 complain);
17147 if (TREE_CODE (r) == COMPONENT_REF)
17148 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17149 RETURN (r);
17150 }
17151
17152 case THROW_EXPR:
17153 RETURN (build_throw
17154 (RECUR (TREE_OPERAND (t, 0))));
17155
17156 case CONSTRUCTOR:
17157 {
17158 vec<constructor_elt, va_gc> *n;
17159 constructor_elt *ce;
17160 unsigned HOST_WIDE_INT idx;
17161 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17162 bool process_index_p;
17163 int newlen;
17164 bool need_copy_p = false;
17165 tree r;
17166
17167 if (type == error_mark_node)
17168 RETURN (error_mark_node);
17169
17170 /* digest_init will do the wrong thing if we let it. */
17171 if (type && TYPE_PTRMEMFUNC_P (type))
17172 RETURN (t);
17173
17174 /* We do not want to process the index of aggregate
17175 initializers as they are identifier nodes which will be
17176 looked up by digest_init. */
17177 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17178
17179 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17180 newlen = vec_safe_length (n);
17181 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17182 {
17183 if (ce->index && process_index_p
17184 /* An identifier index is looked up in the type
17185 being initialized, not the current scope. */
17186 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17187 ce->index = RECUR (ce->index);
17188
17189 if (PACK_EXPANSION_P (ce->value))
17190 {
17191 /* Substitute into the pack expansion. */
17192 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17193 in_decl);
17194
17195 if (ce->value == error_mark_node
17196 || PACK_EXPANSION_P (ce->value))
17197 ;
17198 else if (TREE_VEC_LENGTH (ce->value) == 1)
17199 /* Just move the argument into place. */
17200 ce->value = TREE_VEC_ELT (ce->value, 0);
17201 else
17202 {
17203 /* Update the length of the final CONSTRUCTOR
17204 arguments vector, and note that we will need to
17205 copy.*/
17206 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17207 need_copy_p = true;
17208 }
17209 }
17210 else
17211 ce->value = RECUR (ce->value);
17212 }
17213
17214 if (need_copy_p)
17215 {
17216 vec<constructor_elt, va_gc> *old_n = n;
17217
17218 vec_alloc (n, newlen);
17219 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17220 {
17221 if (TREE_CODE (ce->value) == TREE_VEC)
17222 {
17223 int i, len = TREE_VEC_LENGTH (ce->value);
17224 for (i = 0; i < len; ++i)
17225 CONSTRUCTOR_APPEND_ELT (n, 0,
17226 TREE_VEC_ELT (ce->value, i));
17227 }
17228 else
17229 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17230 }
17231 }
17232
17233 r = build_constructor (init_list_type_node, n);
17234 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17235
17236 if (TREE_HAS_CONSTRUCTOR (t))
17237 RETURN (finish_compound_literal (type, r, complain));
17238
17239 TREE_TYPE (r) = type;
17240 RETURN (r);
17241 }
17242
17243 case TYPEID_EXPR:
17244 {
17245 tree operand_0 = TREE_OPERAND (t, 0);
17246 if (TYPE_P (operand_0))
17247 {
17248 operand_0 = tsubst (operand_0, args, complain, in_decl);
17249 RETURN (get_typeid (operand_0, complain));
17250 }
17251 else
17252 {
17253 operand_0 = RECUR (operand_0);
17254 RETURN (build_typeid (operand_0, complain));
17255 }
17256 }
17257
17258 case VAR_DECL:
17259 if (!args)
17260 RETURN (t);
17261 else if (DECL_PACK_P (t))
17262 {
17263 /* We don't build decls for an instantiation of a
17264 variadic capture proxy, we instantiate the elements
17265 when needed. */
17266 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17267 return RECUR (DECL_VALUE_EXPR (t));
17268 }
17269 /* Fall through */
17270
17271 case PARM_DECL:
17272 {
17273 tree r = tsubst_copy (t, args, complain, in_decl);
17274 /* ??? We're doing a subset of finish_id_expression here. */
17275 if (VAR_P (r)
17276 && !processing_template_decl
17277 && !cp_unevaluated_operand
17278 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17279 && CP_DECL_THREAD_LOCAL_P (r))
17280 {
17281 if (tree wrap = get_tls_wrapper_fn (r))
17282 /* Replace an evaluated use of the thread_local variable with
17283 a call to its wrapper. */
17284 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17285 }
17286 else if (outer_automatic_var_p (r))
17287 {
17288 r = process_outer_var_ref (r, complain);
17289 if (is_capture_proxy (r))
17290 register_local_specialization (r, t);
17291 }
17292
17293 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17294 /* If the original type was a reference, we'll be wrapped in
17295 the appropriate INDIRECT_REF. */
17296 r = convert_from_reference (r);
17297 RETURN (r);
17298 }
17299
17300 case VA_ARG_EXPR:
17301 {
17302 tree op0 = RECUR (TREE_OPERAND (t, 0));
17303 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17304 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17305 }
17306
17307 case OFFSETOF_EXPR:
17308 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
17309 EXPR_LOCATION (t)));
17310
17311 case ADDRESSOF_EXPR:
17312 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17313 RECUR (TREE_OPERAND (t, 0)), complain));
17314
17315 case TRAIT_EXPR:
17316 {
17317 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17318 complain, in_decl);
17319
17320 tree type2 = TRAIT_EXPR_TYPE2 (t);
17321 if (type2 && TREE_CODE (type2) == TREE_LIST)
17322 type2 = RECUR (type2);
17323 else if (type2)
17324 type2 = tsubst (type2, args, complain, in_decl);
17325
17326 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17327 }
17328
17329 case STMT_EXPR:
17330 {
17331 tree old_stmt_expr = cur_stmt_expr;
17332 tree stmt_expr = begin_stmt_expr ();
17333
17334 cur_stmt_expr = stmt_expr;
17335 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17336 integral_constant_expression_p);
17337 stmt_expr = finish_stmt_expr (stmt_expr, false);
17338 cur_stmt_expr = old_stmt_expr;
17339
17340 /* If the resulting list of expression statement is empty,
17341 fold it further into void_node. */
17342 if (empty_expr_stmt_p (stmt_expr))
17343 stmt_expr = void_node;
17344
17345 RETURN (stmt_expr);
17346 }
17347
17348 case LAMBDA_EXPR:
17349 {
17350 tree r = build_lambda_expr ();
17351
17352 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17353 LAMBDA_EXPR_CLOSURE (r) = type;
17354 CLASSTYPE_LAMBDA_EXPR (type) = r;
17355
17356 LAMBDA_EXPR_LOCATION (r)
17357 = LAMBDA_EXPR_LOCATION (t);
17358 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17359 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17360 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17361 LAMBDA_EXPR_DISCRIMINATOR (r)
17362 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17363 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17364 if (!scope)
17365 /* No substitution needed. */;
17366 else if (VAR_OR_FUNCTION_DECL_P (scope))
17367 /* For a function or variable scope, we want to use tsubst so that we
17368 don't complain about referring to an auto before deduction. */
17369 scope = tsubst (scope, args, complain, in_decl);
17370 else if (TREE_CODE (scope) == PARM_DECL)
17371 {
17372 /* Look up the parameter we want directly, as tsubst_copy
17373 doesn't do what we need. */
17374 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17375 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17376 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17377 parm = DECL_CHAIN (parm);
17378 scope = parm;
17379 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17380 if (DECL_CONTEXT (scope) == NULL_TREE)
17381 DECL_CONTEXT (scope) = fn;
17382 }
17383 else if (TREE_CODE (scope) == FIELD_DECL)
17384 /* For a field, use tsubst_copy so that we look up the existing field
17385 rather than build a new one. */
17386 scope = RECUR (scope);
17387 else
17388 gcc_unreachable ();
17389 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17390
17391 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17392 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17393
17394 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17395 determine_visibility (TYPE_NAME (type));
17396 /* Now that we know visibility, instantiate the type so we have a
17397 declaration of the op() for later calls to lambda_function. */
17398 complete_type (type);
17399
17400 if (tree fn = lambda_function (type))
17401 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17402
17403 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17404
17405 insert_pending_capture_proxies ();
17406
17407 RETURN (build_lambda_object (r));
17408 }
17409
17410 case TARGET_EXPR:
17411 /* We can get here for a constant initializer of non-dependent type.
17412 FIXME stop folding in cp_parser_initializer_clause. */
17413 {
17414 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17415 complain);
17416 RETURN (r);
17417 }
17418
17419 case TRANSACTION_EXPR:
17420 RETURN (tsubst_expr(t, args, complain, in_decl,
17421 integral_constant_expression_p));
17422
17423 case PAREN_EXPR:
17424 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17425
17426 case VEC_PERM_EXPR:
17427 {
17428 tree op0 = RECUR (TREE_OPERAND (t, 0));
17429 tree op1 = RECUR (TREE_OPERAND (t, 1));
17430 tree op2 = RECUR (TREE_OPERAND (t, 2));
17431 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17432 complain));
17433 }
17434
17435 case REQUIRES_EXPR:
17436 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17437
17438 default:
17439 /* Handle Objective-C++ constructs, if appropriate. */
17440 {
17441 tree subst
17442 = objcp_tsubst_copy_and_build (t, args, complain,
17443 in_decl, /*function_p=*/false);
17444 if (subst)
17445 RETURN (subst);
17446 }
17447 RETURN (tsubst_copy (t, args, complain, in_decl));
17448 }
17449
17450 #undef RECUR
17451 #undef RETURN
17452 out:
17453 input_location = loc;
17454 return retval;
17455 }
17456
17457 /* Verify that the instantiated ARGS are valid. For type arguments,
17458 make sure that the type's linkage is ok. For non-type arguments,
17459 make sure they are constants if they are integral or enumerations.
17460 Emit an error under control of COMPLAIN, and return TRUE on error. */
17461
17462 static bool
17463 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17464 {
17465 if (dependent_template_arg_p (t))
17466 return false;
17467 if (ARGUMENT_PACK_P (t))
17468 {
17469 tree vec = ARGUMENT_PACK_ARGS (t);
17470 int len = TREE_VEC_LENGTH (vec);
17471 bool result = false;
17472 int i;
17473
17474 for (i = 0; i < len; ++i)
17475 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17476 result = true;
17477 return result;
17478 }
17479 else if (TYPE_P (t))
17480 {
17481 /* [basic.link]: A name with no linkage (notably, the name
17482 of a class or enumeration declared in a local scope)
17483 shall not be used to declare an entity with linkage.
17484 This implies that names with no linkage cannot be used as
17485 template arguments
17486
17487 DR 757 relaxes this restriction for C++0x. */
17488 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17489 : no_linkage_check (t, /*relaxed_p=*/false));
17490
17491 if (nt)
17492 {
17493 /* DR 488 makes use of a type with no linkage cause
17494 type deduction to fail. */
17495 if (complain & tf_error)
17496 {
17497 if (TYPE_UNNAMED_P (nt))
17498 error ("%qT is/uses unnamed type", t);
17499 else
17500 error ("template argument for %qD uses local type %qT",
17501 tmpl, t);
17502 }
17503 return true;
17504 }
17505 /* In order to avoid all sorts of complications, we do not
17506 allow variably-modified types as template arguments. */
17507 else if (variably_modified_type_p (t, NULL_TREE))
17508 {
17509 if (complain & tf_error)
17510 error ("%qT is a variably modified type", t);
17511 return true;
17512 }
17513 }
17514 /* Class template and alias template arguments should be OK. */
17515 else if (DECL_TYPE_TEMPLATE_P (t))
17516 ;
17517 /* A non-type argument of integral or enumerated type must be a
17518 constant. */
17519 else if (TREE_TYPE (t)
17520 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17521 && !REFERENCE_REF_P (t)
17522 && !TREE_CONSTANT (t))
17523 {
17524 if (complain & tf_error)
17525 error ("integral expression %qE is not constant", t);
17526 return true;
17527 }
17528 return false;
17529 }
17530
17531 static bool
17532 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17533 {
17534 int ix, len = DECL_NTPARMS (tmpl);
17535 bool result = false;
17536
17537 for (ix = 0; ix != len; ix++)
17538 {
17539 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17540 result = true;
17541 }
17542 if (result && (complain & tf_error))
17543 error (" trying to instantiate %qD", tmpl);
17544 return result;
17545 }
17546
17547 /* We're out of SFINAE context now, so generate diagnostics for the access
17548 errors we saw earlier when instantiating D from TMPL and ARGS. */
17549
17550 static void
17551 recheck_decl_substitution (tree d, tree tmpl, tree args)
17552 {
17553 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17554 tree type = TREE_TYPE (pattern);
17555 location_t loc = input_location;
17556
17557 push_access_scope (d);
17558 push_deferring_access_checks (dk_no_deferred);
17559 input_location = DECL_SOURCE_LOCATION (pattern);
17560 tsubst (type, args, tf_warning_or_error, d);
17561 input_location = loc;
17562 pop_deferring_access_checks ();
17563 pop_access_scope (d);
17564 }
17565
17566 /* Instantiate the indicated variable, function, or alias template TMPL with
17567 the template arguments in TARG_PTR. */
17568
17569 static tree
17570 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17571 {
17572 tree targ_ptr = orig_args;
17573 tree fndecl;
17574 tree gen_tmpl;
17575 tree spec;
17576 bool access_ok = true;
17577
17578 if (tmpl == error_mark_node)
17579 return error_mark_node;
17580
17581 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
17582
17583 /* If this function is a clone, handle it specially. */
17584 if (DECL_CLONED_FUNCTION_P (tmpl))
17585 {
17586 tree spec;
17587 tree clone;
17588
17589 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
17590 DECL_CLONED_FUNCTION. */
17591 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
17592 targ_ptr, complain);
17593 if (spec == error_mark_node)
17594 return error_mark_node;
17595
17596 /* Look for the clone. */
17597 FOR_EACH_CLONE (clone, spec)
17598 if (DECL_NAME (clone) == DECL_NAME (tmpl))
17599 return clone;
17600 /* We should always have found the clone by now. */
17601 gcc_unreachable ();
17602 return NULL_TREE;
17603 }
17604
17605 if (targ_ptr == error_mark_node)
17606 return error_mark_node;
17607
17608 /* Check to see if we already have this specialization. */
17609 gen_tmpl = most_general_template (tmpl);
17610 if (TMPL_ARGS_DEPTH (targ_ptr)
17611 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
17612 /* targ_ptr only has the innermost template args, so add the outer ones
17613 from tmpl, which could be either a partial instantiation or gen_tmpl (in
17614 the case of a non-dependent call within a template definition). */
17615 targ_ptr = (add_outermost_template_args
17616 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
17617 targ_ptr));
17618
17619 /* It would be nice to avoid hashing here and then again in tsubst_decl,
17620 but it doesn't seem to be on the hot path. */
17621 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
17622
17623 gcc_assert (tmpl == gen_tmpl
17624 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
17625 == spec)
17626 || fndecl == NULL_TREE);
17627
17628 if (spec != NULL_TREE)
17629 {
17630 if (FNDECL_HAS_ACCESS_ERRORS (spec))
17631 {
17632 if (complain & tf_error)
17633 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
17634 return error_mark_node;
17635 }
17636 return spec;
17637 }
17638
17639 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
17640 complain))
17641 return error_mark_node;
17642
17643 /* We are building a FUNCTION_DECL, during which the access of its
17644 parameters and return types have to be checked. However this
17645 FUNCTION_DECL which is the desired context for access checking
17646 is not built yet. We solve this chicken-and-egg problem by
17647 deferring all checks until we have the FUNCTION_DECL. */
17648 push_deferring_access_checks (dk_deferred);
17649
17650 /* Instantiation of the function happens in the context of the function
17651 template, not the context of the overload resolution we're doing. */
17652 push_to_top_level ();
17653 /* If there are dependent arguments, e.g. because we're doing partial
17654 ordering, make sure processing_template_decl stays set. */
17655 if (uses_template_parms (targ_ptr))
17656 ++processing_template_decl;
17657 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17658 {
17659 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
17660 complain, gen_tmpl, true);
17661 push_nested_class (ctx);
17662 }
17663
17664 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
17665
17666 fndecl = NULL_TREE;
17667 if (VAR_P (pattern))
17668 {
17669 /* We need to determine if we're using a partial or explicit
17670 specialization now, because the type of the variable could be
17671 different. */
17672 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
17673 tree elt = most_specialized_partial_spec (tid, complain);
17674 if (elt == error_mark_node)
17675 pattern = error_mark_node;
17676 else if (elt)
17677 {
17678 tree partial_tmpl = TREE_VALUE (elt);
17679 tree partial_args = TREE_PURPOSE (elt);
17680 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
17681 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
17682 }
17683 }
17684
17685 /* Substitute template parameters to obtain the specialization. */
17686 if (fndecl == NULL_TREE)
17687 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
17688 if (DECL_CLASS_SCOPE_P (gen_tmpl))
17689 pop_nested_class ();
17690 pop_from_top_level ();
17691
17692 if (fndecl == error_mark_node)
17693 {
17694 pop_deferring_access_checks ();
17695 return error_mark_node;
17696 }
17697
17698 /* The DECL_TI_TEMPLATE should always be the immediate parent
17699 template, not the most general template. */
17700 DECL_TI_TEMPLATE (fndecl) = tmpl;
17701 DECL_TI_ARGS (fndecl) = targ_ptr;
17702
17703 /* Now we know the specialization, compute access previously
17704 deferred. Do no access control for inheriting constructors,
17705 as we already checked access for the inherited constructor. */
17706 if (!(flag_new_inheriting_ctors
17707 && DECL_INHERITED_CTOR (fndecl)))
17708 {
17709 push_access_scope (fndecl);
17710 if (!perform_deferred_access_checks (complain))
17711 access_ok = false;
17712 pop_access_scope (fndecl);
17713 }
17714 pop_deferring_access_checks ();
17715
17716 /* If we've just instantiated the main entry point for a function,
17717 instantiate all the alternate entry points as well. We do this
17718 by cloning the instantiation of the main entry point, not by
17719 instantiating the template clones. */
17720 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
17721 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
17722
17723 if (!access_ok)
17724 {
17725 if (!(complain & tf_error))
17726 {
17727 /* Remember to reinstantiate when we're out of SFINAE so the user
17728 can see the errors. */
17729 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
17730 }
17731 return error_mark_node;
17732 }
17733 return fndecl;
17734 }
17735
17736 /* Wrapper for instantiate_template_1. */
17737
17738 tree
17739 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
17740 {
17741 tree ret;
17742 timevar_push (TV_TEMPLATE_INST);
17743 ret = instantiate_template_1 (tmpl, orig_args, complain);
17744 timevar_pop (TV_TEMPLATE_INST);
17745 return ret;
17746 }
17747
17748 /* Instantiate the alias template TMPL with ARGS. Also push a template
17749 instantiation level, which instantiate_template doesn't do because
17750 functions and variables have sufficient context established by the
17751 callers. */
17752
17753 static tree
17754 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
17755 {
17756 struct pending_template *old_last_pend = last_pending_template;
17757 struct tinst_level *old_error_tinst = last_error_tinst_level;
17758 if (tmpl == error_mark_node || args == error_mark_node)
17759 return error_mark_node;
17760 tree tinst = build_tree_list (tmpl, args);
17761 if (!push_tinst_level (tinst))
17762 {
17763 ggc_free (tinst);
17764 return error_mark_node;
17765 }
17766
17767 args =
17768 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
17769 args, tmpl, complain,
17770 /*require_all_args=*/true,
17771 /*use_default_args=*/true);
17772
17773 tree r = instantiate_template (tmpl, args, complain);
17774 pop_tinst_level ();
17775 /* We can't free this if a pending_template entry or last_error_tinst_level
17776 is pointing at it. */
17777 if (last_pending_template == old_last_pend
17778 && last_error_tinst_level == old_error_tinst)
17779 ggc_free (tinst);
17780
17781 return r;
17782 }
17783
17784 /* PARM is a template parameter pack for FN. Returns true iff
17785 PARM is used in a deducible way in the argument list of FN. */
17786
17787 static bool
17788 pack_deducible_p (tree parm, tree fn)
17789 {
17790 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
17791 for (; t; t = TREE_CHAIN (t))
17792 {
17793 tree type = TREE_VALUE (t);
17794 tree packs;
17795 if (!PACK_EXPANSION_P (type))
17796 continue;
17797 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
17798 packs; packs = TREE_CHAIN (packs))
17799 if (template_args_equal (TREE_VALUE (packs), parm))
17800 {
17801 /* The template parameter pack is used in a function parameter
17802 pack. If this is the end of the parameter list, the
17803 template parameter pack is deducible. */
17804 if (TREE_CHAIN (t) == void_list_node)
17805 return true;
17806 else
17807 /* Otherwise, not. Well, it could be deduced from
17808 a non-pack parameter, but doing so would end up with
17809 a deduction mismatch, so don't bother. */
17810 return false;
17811 }
17812 }
17813 /* The template parameter pack isn't used in any function parameter
17814 packs, but it might be used deeper, e.g. tuple<Args...>. */
17815 return true;
17816 }
17817
17818 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
17819 NARGS elements of the arguments that are being used when calling
17820 it. TARGS is a vector into which the deduced template arguments
17821 are placed.
17822
17823 Returns either a FUNCTION_DECL for the matching specialization of FN or
17824 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
17825 true, diagnostics will be printed to explain why it failed.
17826
17827 If FN is a conversion operator, or we are trying to produce a specific
17828 specialization, RETURN_TYPE is the return type desired.
17829
17830 The EXPLICIT_TARGS are explicit template arguments provided via a
17831 template-id.
17832
17833 The parameter STRICT is one of:
17834
17835 DEDUCE_CALL:
17836 We are deducing arguments for a function call, as in
17837 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
17838 deducing arguments for a call to the result of a conversion
17839 function template, as in [over.call.object].
17840
17841 DEDUCE_CONV:
17842 We are deducing arguments for a conversion function, as in
17843 [temp.deduct.conv].
17844
17845 DEDUCE_EXACT:
17846 We are deducing arguments when doing an explicit instantiation
17847 as in [temp.explicit], when determining an explicit specialization
17848 as in [temp.expl.spec], or when taking the address of a function
17849 template, as in [temp.deduct.funcaddr]. */
17850
17851 tree
17852 fn_type_unification (tree fn,
17853 tree explicit_targs,
17854 tree targs,
17855 const tree *args,
17856 unsigned int nargs,
17857 tree return_type,
17858 unification_kind_t strict,
17859 int flags,
17860 bool explain_p,
17861 bool decltype_p)
17862 {
17863 tree parms;
17864 tree fntype;
17865 tree decl = NULL_TREE;
17866 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
17867 bool ok;
17868 static int deduction_depth;
17869 struct pending_template *old_last_pend = last_pending_template;
17870 struct tinst_level *old_error_tinst = last_error_tinst_level;
17871
17872 tree orig_fn = fn;
17873 if (flag_new_inheriting_ctors)
17874 fn = strip_inheriting_ctors (fn);
17875
17876 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
17877 tree tinst;
17878 tree r = error_mark_node;
17879
17880 tree full_targs = targs;
17881 if (TMPL_ARGS_DEPTH (targs)
17882 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
17883 full_targs = (add_outermost_template_args
17884 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
17885 targs));
17886
17887 if (decltype_p)
17888 complain |= tf_decltype;
17889
17890 /* In C++0x, it's possible to have a function template whose type depends
17891 on itself recursively. This is most obvious with decltype, but can also
17892 occur with enumeration scope (c++/48969). So we need to catch infinite
17893 recursion and reject the substitution at deduction time; this function
17894 will return error_mark_node for any repeated substitution.
17895
17896 This also catches excessive recursion such as when f<N> depends on
17897 f<N-1> across all integers, and returns error_mark_node for all the
17898 substitutions back up to the initial one.
17899
17900 This is, of course, not reentrant. */
17901 if (excessive_deduction_depth)
17902 return error_mark_node;
17903 tinst = build_tree_list (fn, NULL_TREE);
17904 ++deduction_depth;
17905
17906 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
17907
17908 fntype = TREE_TYPE (fn);
17909 if (explicit_targs)
17910 {
17911 /* [temp.deduct]
17912
17913 The specified template arguments must match the template
17914 parameters in kind (i.e., type, nontype, template), and there
17915 must not be more arguments than there are parameters;
17916 otherwise type deduction fails.
17917
17918 Nontype arguments must match the types of the corresponding
17919 nontype template parameters, or must be convertible to the
17920 types of the corresponding nontype parameters as specified in
17921 _temp.arg.nontype_, otherwise type deduction fails.
17922
17923 All references in the function type of the function template
17924 to the corresponding template parameters are replaced by the
17925 specified template argument values. If a substitution in a
17926 template parameter or in the function type of the function
17927 template results in an invalid type, type deduction fails. */
17928 int i, len = TREE_VEC_LENGTH (tparms);
17929 location_t loc = input_location;
17930 bool incomplete = false;
17931
17932 if (explicit_targs == error_mark_node)
17933 goto fail;
17934
17935 if (TMPL_ARGS_DEPTH (explicit_targs)
17936 < TMPL_ARGS_DEPTH (full_targs))
17937 explicit_targs = add_outermost_template_args (full_targs,
17938 explicit_targs);
17939
17940 /* Adjust any explicit template arguments before entering the
17941 substitution context. */
17942 explicit_targs
17943 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
17944 complain,
17945 /*require_all_args=*/false,
17946 /*use_default_args=*/false));
17947 if (explicit_targs == error_mark_node)
17948 goto fail;
17949
17950 /* Substitute the explicit args into the function type. This is
17951 necessary so that, for instance, explicitly declared function
17952 arguments can match null pointed constants. If we were given
17953 an incomplete set of explicit args, we must not do semantic
17954 processing during substitution as we could create partial
17955 instantiations. */
17956 for (i = 0; i < len; i++)
17957 {
17958 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
17959 bool parameter_pack = false;
17960 tree targ = TREE_VEC_ELT (explicit_targs, i);
17961
17962 /* Dig out the actual parm. */
17963 if (TREE_CODE (parm) == TYPE_DECL
17964 || TREE_CODE (parm) == TEMPLATE_DECL)
17965 {
17966 parm = TREE_TYPE (parm);
17967 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
17968 }
17969 else if (TREE_CODE (parm) == PARM_DECL)
17970 {
17971 parm = DECL_INITIAL (parm);
17972 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
17973 }
17974
17975 if (!parameter_pack && targ == NULL_TREE)
17976 /* No explicit argument for this template parameter. */
17977 incomplete = true;
17978
17979 if (parameter_pack && pack_deducible_p (parm, fn))
17980 {
17981 /* Mark the argument pack as "incomplete". We could
17982 still deduce more arguments during unification.
17983 We remove this mark in type_unification_real. */
17984 if (targ)
17985 {
17986 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
17987 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
17988 = ARGUMENT_PACK_ARGS (targ);
17989 }
17990
17991 /* We have some incomplete argument packs. */
17992 incomplete = true;
17993 }
17994 }
17995
17996 TREE_VALUE (tinst) = explicit_targs;
17997 if (!push_tinst_level (tinst))
17998 {
17999 excessive_deduction_depth = true;
18000 goto fail;
18001 }
18002 processing_template_decl += incomplete;
18003 input_location = DECL_SOURCE_LOCATION (fn);
18004 /* Ignore any access checks; we'll see them again in
18005 instantiate_template and they might have the wrong
18006 access path at this point. */
18007 push_deferring_access_checks (dk_deferred);
18008 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18009 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18010 pop_deferring_access_checks ();
18011 input_location = loc;
18012 processing_template_decl -= incomplete;
18013 pop_tinst_level ();
18014
18015 if (fntype == error_mark_node)
18016 goto fail;
18017
18018 /* Place the explicitly specified arguments in TARGS. */
18019 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18020 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18021 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18022 }
18023
18024 /* Never do unification on the 'this' parameter. */
18025 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18026
18027 if (return_type && strict == DEDUCE_CALL)
18028 {
18029 /* We're deducing for a call to the result of a template conversion
18030 function. The parms we really want are in return_type. */
18031 if (POINTER_TYPE_P (return_type))
18032 return_type = TREE_TYPE (return_type);
18033 parms = TYPE_ARG_TYPES (return_type);
18034 }
18035 else if (return_type)
18036 {
18037 tree *new_args;
18038
18039 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18040 new_args = XALLOCAVEC (tree, nargs + 1);
18041 new_args[0] = return_type;
18042 memcpy (new_args + 1, args, nargs * sizeof (tree));
18043 args = new_args;
18044 ++nargs;
18045 }
18046
18047 /* We allow incomplete unification without an error message here
18048 because the standard doesn't seem to explicitly prohibit it. Our
18049 callers must be ready to deal with unification failures in any
18050 event. */
18051
18052 TREE_VALUE (tinst) = targs;
18053 /* If we aren't explaining yet, push tinst context so we can see where
18054 any errors (e.g. from class instantiations triggered by instantiation
18055 of default template arguments) come from. If we are explaining, this
18056 context is redundant. */
18057 if (!explain_p && !push_tinst_level (tinst))
18058 {
18059 excessive_deduction_depth = true;
18060 goto fail;
18061 }
18062
18063 /* type_unification_real will pass back any access checks from default
18064 template argument substitution. */
18065 vec<deferred_access_check, va_gc> *checks;
18066 checks = NULL;
18067
18068 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18069 full_targs, parms, args, nargs, /*subr=*/0,
18070 strict, flags, &checks, explain_p);
18071 if (!explain_p)
18072 pop_tinst_level ();
18073 if (!ok)
18074 goto fail;
18075
18076 /* Now that we have bindings for all of the template arguments,
18077 ensure that the arguments deduced for the template template
18078 parameters have compatible template parameter lists. We cannot
18079 check this property before we have deduced all template
18080 arguments, because the template parameter types of a template
18081 template parameter might depend on prior template parameters
18082 deduced after the template template parameter. The following
18083 ill-formed example illustrates this issue:
18084
18085 template<typename T, template<T> class C> void f(C<5>, T);
18086
18087 template<int N> struct X {};
18088
18089 void g() {
18090 f(X<5>(), 5l); // error: template argument deduction fails
18091 }
18092
18093 The template parameter list of 'C' depends on the template type
18094 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18095 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18096 time that we deduce 'C'. */
18097 if (!template_template_parm_bindings_ok_p
18098 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18099 {
18100 unify_inconsistent_template_template_parameters (explain_p);
18101 goto fail;
18102 }
18103
18104 /* All is well so far. Now, check:
18105
18106 [temp.deduct]
18107
18108 When all template arguments have been deduced, all uses of
18109 template parameters in nondeduced contexts are replaced with
18110 the corresponding deduced argument values. If the
18111 substitution results in an invalid type, as described above,
18112 type deduction fails. */
18113 TREE_VALUE (tinst) = targs;
18114 if (!push_tinst_level (tinst))
18115 {
18116 excessive_deduction_depth = true;
18117 goto fail;
18118 }
18119
18120 /* Also collect access checks from the instantiation. */
18121 reopen_deferring_access_checks (checks);
18122
18123 decl = instantiate_template (fn, targs, complain);
18124
18125 checks = get_deferred_access_checks ();
18126 pop_deferring_access_checks ();
18127
18128 pop_tinst_level ();
18129
18130 if (decl == error_mark_node)
18131 goto fail;
18132
18133 /* Now perform any access checks encountered during substitution. */
18134 push_access_scope (decl);
18135 ok = perform_access_checks (checks, complain);
18136 pop_access_scope (decl);
18137 if (!ok)
18138 goto fail;
18139
18140 /* If we're looking for an exact match, check that what we got
18141 is indeed an exact match. It might not be if some template
18142 parameters are used in non-deduced contexts. But don't check
18143 for an exact match if we have dependent template arguments;
18144 in that case we're doing partial ordering, and we already know
18145 that we have two candidates that will provide the actual type. */
18146 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18147 {
18148 tree substed = TREE_TYPE (decl);
18149 unsigned int i;
18150
18151 tree sarg
18152 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18153 if (return_type)
18154 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18155 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18156 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18157 {
18158 unify_type_mismatch (explain_p, args[i],
18159 TREE_VALUE (sarg));
18160 goto fail;
18161 }
18162 }
18163
18164 /* After doing deduction with the inherited constructor, actually return an
18165 instantiation of the inheriting constructor. */
18166 if (orig_fn != fn)
18167 decl = instantiate_template (orig_fn, targs, complain);
18168
18169 r = decl;
18170
18171 fail:
18172 --deduction_depth;
18173 if (excessive_deduction_depth)
18174 {
18175 if (deduction_depth == 0)
18176 /* Reset once we're all the way out. */
18177 excessive_deduction_depth = false;
18178 }
18179
18180 /* We can't free this if a pending_template entry or last_error_tinst_level
18181 is pointing at it. */
18182 if (last_pending_template == old_last_pend
18183 && last_error_tinst_level == old_error_tinst)
18184 ggc_free (tinst);
18185
18186 return r;
18187 }
18188
18189 /* Adjust types before performing type deduction, as described in
18190 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18191 sections are symmetric. PARM is the type of a function parameter
18192 or the return type of the conversion function. ARG is the type of
18193 the argument passed to the call, or the type of the value
18194 initialized with the result of the conversion function.
18195 ARG_EXPR is the original argument expression, which may be null. */
18196
18197 static int
18198 maybe_adjust_types_for_deduction (unification_kind_t strict,
18199 tree* parm,
18200 tree* arg,
18201 tree arg_expr)
18202 {
18203 int result = 0;
18204
18205 switch (strict)
18206 {
18207 case DEDUCE_CALL:
18208 break;
18209
18210 case DEDUCE_CONV:
18211 /* Swap PARM and ARG throughout the remainder of this
18212 function; the handling is precisely symmetric since PARM
18213 will initialize ARG rather than vice versa. */
18214 std::swap (parm, arg);
18215 break;
18216
18217 case DEDUCE_EXACT:
18218 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18219 too, but here handle it by stripping the reference from PARM
18220 rather than by adding it to ARG. */
18221 if (TREE_CODE (*parm) == REFERENCE_TYPE
18222 && TYPE_REF_IS_RVALUE (*parm)
18223 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18224 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18225 && TREE_CODE (*arg) == REFERENCE_TYPE
18226 && !TYPE_REF_IS_RVALUE (*arg))
18227 *parm = TREE_TYPE (*parm);
18228 /* Nothing else to do in this case. */
18229 return 0;
18230
18231 default:
18232 gcc_unreachable ();
18233 }
18234
18235 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18236 {
18237 /* [temp.deduct.call]
18238
18239 If P is not a reference type:
18240
18241 --If A is an array type, the pointer type produced by the
18242 array-to-pointer standard conversion (_conv.array_) is
18243 used in place of A for type deduction; otherwise,
18244
18245 --If A is a function type, the pointer type produced by
18246 the function-to-pointer standard conversion
18247 (_conv.func_) is used in place of A for type deduction;
18248 otherwise,
18249
18250 --If A is a cv-qualified type, the top level
18251 cv-qualifiers of A's type are ignored for type
18252 deduction. */
18253 if (TREE_CODE (*arg) == ARRAY_TYPE)
18254 *arg = build_pointer_type (TREE_TYPE (*arg));
18255 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18256 *arg = build_pointer_type (*arg);
18257 else
18258 *arg = TYPE_MAIN_VARIANT (*arg);
18259 }
18260
18261 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
18262 of the form T&&, where T is a template parameter, and the argument
18263 is an lvalue, T is deduced as A& */
18264 if (TREE_CODE (*parm) == REFERENCE_TYPE
18265 && TYPE_REF_IS_RVALUE (*parm)
18266 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18267 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18268 && (arg_expr ? lvalue_p (arg_expr)
18269 /* try_one_overload doesn't provide an arg_expr, but
18270 functions are always lvalues. */
18271 : TREE_CODE (*arg) == FUNCTION_TYPE))
18272 *arg = build_reference_type (*arg);
18273
18274 /* [temp.deduct.call]
18275
18276 If P is a cv-qualified type, the top level cv-qualifiers
18277 of P's type are ignored for type deduction. If P is a
18278 reference type, the type referred to by P is used for
18279 type deduction. */
18280 *parm = TYPE_MAIN_VARIANT (*parm);
18281 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18282 {
18283 *parm = TREE_TYPE (*parm);
18284 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18285 }
18286
18287 /* DR 322. For conversion deduction, remove a reference type on parm
18288 too (which has been swapped into ARG). */
18289 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18290 *arg = TREE_TYPE (*arg);
18291
18292 return result;
18293 }
18294
18295 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18296 template which does contain any deducible template parameters; check if
18297 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18298 unify_one_argument. */
18299
18300 static int
18301 check_non_deducible_conversion (tree parm, tree arg, int strict,
18302 int flags, bool explain_p)
18303 {
18304 tree type;
18305
18306 if (!TYPE_P (arg))
18307 type = TREE_TYPE (arg);
18308 else
18309 type = arg;
18310
18311 if (same_type_p (parm, type))
18312 return unify_success (explain_p);
18313
18314 if (strict == DEDUCE_CONV)
18315 {
18316 if (can_convert_arg (type, parm, NULL_TREE, flags,
18317 explain_p ? tf_warning_or_error : tf_none))
18318 return unify_success (explain_p);
18319 }
18320 else if (strict != DEDUCE_EXACT)
18321 {
18322 if (can_convert_arg (parm, type,
18323 TYPE_P (arg) ? NULL_TREE : arg,
18324 flags, explain_p ? tf_warning_or_error : tf_none))
18325 return unify_success (explain_p);
18326 }
18327
18328 if (strict == DEDUCE_EXACT)
18329 return unify_type_mismatch (explain_p, parm, arg);
18330 else
18331 return unify_arg_conversion (explain_p, parm, type, arg);
18332 }
18333
18334 static bool uses_deducible_template_parms (tree type);
18335
18336 /* Returns true iff the expression EXPR is one from which a template
18337 argument can be deduced. In other words, if it's an undecorated
18338 use of a template non-type parameter. */
18339
18340 static bool
18341 deducible_expression (tree expr)
18342 {
18343 /* Strip implicit conversions. */
18344 while (CONVERT_EXPR_P (expr))
18345 expr = TREE_OPERAND (expr, 0);
18346 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18347 }
18348
18349 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18350 deducible way; that is, if it has a max value of <PARM> - 1. */
18351
18352 static bool
18353 deducible_array_bound (tree domain)
18354 {
18355 if (domain == NULL_TREE)
18356 return false;
18357
18358 tree max = TYPE_MAX_VALUE (domain);
18359 if (TREE_CODE (max) != MINUS_EXPR)
18360 return false;
18361
18362 return deducible_expression (TREE_OPERAND (max, 0));
18363 }
18364
18365 /* Returns true iff the template arguments ARGS use a template parameter
18366 in a deducible way. */
18367
18368 static bool
18369 deducible_template_args (tree args)
18370 {
18371 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18372 {
18373 bool deducible;
18374 tree elt = TREE_VEC_ELT (args, i);
18375 if (ARGUMENT_PACK_P (elt))
18376 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18377 else
18378 {
18379 if (PACK_EXPANSION_P (elt))
18380 elt = PACK_EXPANSION_PATTERN (elt);
18381 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18382 deducible = true;
18383 else if (TYPE_P (elt))
18384 deducible = uses_deducible_template_parms (elt);
18385 else
18386 deducible = deducible_expression (elt);
18387 }
18388 if (deducible)
18389 return true;
18390 }
18391 return false;
18392 }
18393
18394 /* Returns true iff TYPE contains any deducible references to template
18395 parameters, as per 14.8.2.5. */
18396
18397 static bool
18398 uses_deducible_template_parms (tree type)
18399 {
18400 if (PACK_EXPANSION_P (type))
18401 type = PACK_EXPANSION_PATTERN (type);
18402
18403 /* T
18404 cv-list T
18405 TT<T>
18406 TT<i>
18407 TT<> */
18408 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18409 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18410 return true;
18411
18412 /* T*
18413 T&
18414 T&& */
18415 if (POINTER_TYPE_P (type))
18416 return uses_deducible_template_parms (TREE_TYPE (type));
18417
18418 /* T[integer-constant ]
18419 type [i] */
18420 if (TREE_CODE (type) == ARRAY_TYPE)
18421 return (uses_deducible_template_parms (TREE_TYPE (type))
18422 || deducible_array_bound (TYPE_DOMAIN (type)));
18423
18424 /* T type ::*
18425 type T::*
18426 T T::*
18427 T (type ::*)()
18428 type (T::*)()
18429 type (type ::*)(T)
18430 type (T::*)(T)
18431 T (type ::*)(T)
18432 T (T::*)()
18433 T (T::*)(T) */
18434 if (TYPE_PTRMEM_P (type))
18435 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18436 || (uses_deducible_template_parms
18437 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18438
18439 /* template-name <T> (where template-name refers to a class template)
18440 template-name <i> (where template-name refers to a class template) */
18441 if (CLASS_TYPE_P (type)
18442 && CLASSTYPE_TEMPLATE_INFO (type)
18443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18444 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18445 (CLASSTYPE_TI_ARGS (type)));
18446
18447 /* type (T)
18448 T()
18449 T(T) */
18450 if (TREE_CODE (type) == FUNCTION_TYPE
18451 || TREE_CODE (type) == METHOD_TYPE)
18452 {
18453 if (uses_deducible_template_parms (TREE_TYPE (type)))
18454 return true;
18455 tree parm = TYPE_ARG_TYPES (type);
18456 if (TREE_CODE (type) == METHOD_TYPE)
18457 parm = TREE_CHAIN (parm);
18458 for (; parm; parm = TREE_CHAIN (parm))
18459 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18460 return true;
18461 }
18462
18463 return false;
18464 }
18465
18466 /* Subroutine of type_unification_real and unify_pack_expansion to
18467 handle unification of a single P/A pair. Parameters are as
18468 for those functions. */
18469
18470 static int
18471 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18472 int subr, unification_kind_t strict,
18473 bool explain_p)
18474 {
18475 tree arg_expr = NULL_TREE;
18476 int arg_strict;
18477
18478 if (arg == error_mark_node || parm == error_mark_node)
18479 return unify_invalid (explain_p);
18480 if (arg == unknown_type_node)
18481 /* We can't deduce anything from this, but we might get all the
18482 template args from other function args. */
18483 return unify_success (explain_p);
18484
18485 /* Implicit conversions (Clause 4) will be performed on a function
18486 argument to convert it to the type of the corresponding function
18487 parameter if the parameter type contains no template-parameters that
18488 participate in template argument deduction. */
18489 if (strict != DEDUCE_EXACT
18490 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18491 /* For function parameters with no deducible template parameters,
18492 just return. We'll check non-dependent conversions later. */
18493 return unify_success (explain_p);
18494
18495 switch (strict)
18496 {
18497 case DEDUCE_CALL:
18498 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18499 | UNIFY_ALLOW_MORE_CV_QUAL
18500 | UNIFY_ALLOW_DERIVED);
18501 break;
18502
18503 case DEDUCE_CONV:
18504 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18505 break;
18506
18507 case DEDUCE_EXACT:
18508 arg_strict = UNIFY_ALLOW_NONE;
18509 break;
18510
18511 default:
18512 gcc_unreachable ();
18513 }
18514
18515 /* We only do these transformations if this is the top-level
18516 parameter_type_list in a call or declaration matching; in other
18517 situations (nested function declarators, template argument lists) we
18518 won't be comparing a type to an expression, and we don't do any type
18519 adjustments. */
18520 if (!subr)
18521 {
18522 if (!TYPE_P (arg))
18523 {
18524 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18525 if (type_unknown_p (arg))
18526 {
18527 /* [temp.deduct.type] A template-argument can be
18528 deduced from a pointer to function or pointer
18529 to member function argument if the set of
18530 overloaded functions does not contain function
18531 templates and at most one of a set of
18532 overloaded functions provides a unique
18533 match. */
18534
18535 if (resolve_overloaded_unification
18536 (tparms, targs, parm, arg, strict,
18537 arg_strict, explain_p))
18538 return unify_success (explain_p);
18539 return unify_overload_resolution_failure (explain_p, arg);
18540 }
18541
18542 arg_expr = arg;
18543 arg = unlowered_expr_type (arg);
18544 if (arg == error_mark_node)
18545 return unify_invalid (explain_p);
18546 }
18547
18548 arg_strict |=
18549 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18550 }
18551 else
18552 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18553 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18554 return unify_template_argument_mismatch (explain_p, parm, arg);
18555
18556 /* For deduction from an init-list we need the actual list. */
18557 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18558 arg = arg_expr;
18559 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18560 }
18561
18562 /* Most parms like fn_type_unification.
18563
18564 If SUBR is 1, we're being called recursively (to unify the
18565 arguments of a function or method parameter of a function
18566 template).
18567
18568 CHECKS is a pointer to a vector of access checks encountered while
18569 substituting default template arguments. */
18570
18571 static int
18572 type_unification_real (tree tparms,
18573 tree full_targs,
18574 tree xparms,
18575 const tree *xargs,
18576 unsigned int xnargs,
18577 int subr,
18578 unification_kind_t strict,
18579 int flags,
18580 vec<deferred_access_check, va_gc> **checks,
18581 bool explain_p)
18582 {
18583 tree parm, arg;
18584 int i;
18585 int ntparms = TREE_VEC_LENGTH (tparms);
18586 int saw_undeduced = 0;
18587 tree parms;
18588 const tree *args;
18589 unsigned int nargs;
18590 unsigned int ia;
18591
18592 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
18593 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
18594 gcc_assert (ntparms > 0);
18595
18596 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
18597
18598 /* Reset the number of non-defaulted template arguments contained
18599 in TARGS. */
18600 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
18601
18602 again:
18603 parms = xparms;
18604 args = xargs;
18605 nargs = xnargs;
18606
18607 ia = 0;
18608 while (parms && parms != void_list_node
18609 && ia < nargs)
18610 {
18611 parm = TREE_VALUE (parms);
18612
18613 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18614 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
18615 /* For a function parameter pack that occurs at the end of the
18616 parameter-declaration-list, the type A of each remaining
18617 argument of the call is compared with the type P of the
18618 declarator-id of the function parameter pack. */
18619 break;
18620
18621 parms = TREE_CHAIN (parms);
18622
18623 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18624 /* For a function parameter pack that does not occur at the
18625 end of the parameter-declaration-list, the type of the
18626 parameter pack is a non-deduced context. */
18627 continue;
18628
18629 arg = args[ia];
18630 ++ia;
18631
18632 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
18633 explain_p))
18634 return 1;
18635 }
18636
18637 if (parms
18638 && parms != void_list_node
18639 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
18640 {
18641 /* Unify the remaining arguments with the pack expansion type. */
18642 tree argvec;
18643 tree parmvec = make_tree_vec (1);
18644
18645 /* Allocate a TREE_VEC and copy in all of the arguments */
18646 argvec = make_tree_vec (nargs - ia);
18647 for (i = 0; ia < nargs; ++ia, ++i)
18648 TREE_VEC_ELT (argvec, i) = args[ia];
18649
18650 /* Copy the parameter into parmvec. */
18651 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
18652 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
18653 /*subr=*/subr, explain_p))
18654 return 1;
18655
18656 /* Advance to the end of the list of parameters. */
18657 parms = TREE_CHAIN (parms);
18658 }
18659
18660 /* Fail if we've reached the end of the parm list, and more args
18661 are present, and the parm list isn't variadic. */
18662 if (ia < nargs && parms == void_list_node)
18663 return unify_too_many_arguments (explain_p, nargs, ia);
18664 /* Fail if parms are left and they don't have default values and
18665 they aren't all deduced as empty packs (c++/57397). This is
18666 consistent with sufficient_parms_p. */
18667 if (parms && parms != void_list_node
18668 && TREE_PURPOSE (parms) == NULL_TREE)
18669 {
18670 unsigned int count = nargs;
18671 tree p = parms;
18672 bool type_pack_p;
18673 do
18674 {
18675 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
18676 if (!type_pack_p)
18677 count++;
18678 p = TREE_CHAIN (p);
18679 }
18680 while (p && p != void_list_node);
18681 if (count != nargs)
18682 return unify_too_few_arguments (explain_p, ia, count,
18683 type_pack_p);
18684 }
18685
18686 if (!subr)
18687 {
18688 tsubst_flags_t complain = (explain_p
18689 ? tf_warning_or_error
18690 : tf_none);
18691
18692 for (i = 0; i < ntparms; i++)
18693 {
18694 tree targ = TREE_VEC_ELT (targs, i);
18695 tree tparm = TREE_VEC_ELT (tparms, i);
18696
18697 /* Clear the "incomplete" flags on all argument packs now so that
18698 substituting them into later default arguments works. */
18699 if (targ && ARGUMENT_PACK_P (targ))
18700 {
18701 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
18702 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
18703 }
18704
18705 if (targ || tparm == error_mark_node)
18706 continue;
18707 tparm = TREE_VALUE (tparm);
18708
18709 /* If this is an undeduced nontype parameter that depends on
18710 a type parameter, try another pass; its type may have been
18711 deduced from a later argument than the one from which
18712 this parameter can be deduced. */
18713 if (TREE_CODE (tparm) == PARM_DECL
18714 && uses_template_parms (TREE_TYPE (tparm))
18715 && saw_undeduced < 2)
18716 {
18717 saw_undeduced = 1;
18718 continue;
18719 }
18720
18721 /* Core issue #226 (C++0x) [temp.deduct]:
18722
18723 If a template argument has not been deduced, its
18724 default template argument, if any, is used.
18725
18726 When we are in C++98 mode, TREE_PURPOSE will either
18727 be NULL_TREE or ERROR_MARK_NODE, so we do not need
18728 to explicitly check cxx_dialect here. */
18729 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
18730 /* OK, there is a default argument. Wait until after the
18731 conversion check to do substitution. */
18732 continue;
18733
18734 /* If the type parameter is a parameter pack, then it will
18735 be deduced to an empty parameter pack. */
18736 if (template_parameter_pack_p (tparm))
18737 {
18738 tree arg;
18739
18740 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
18741 {
18742 arg = make_node (NONTYPE_ARGUMENT_PACK);
18743 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
18744 TREE_CONSTANT (arg) = 1;
18745 }
18746 else
18747 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
18748
18749 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
18750
18751 TREE_VEC_ELT (targs, i) = arg;
18752 continue;
18753 }
18754
18755 return unify_parameter_deduction_failure (explain_p, tparm);
18756 }
18757
18758 /* DR 1391: All parameters have args, now check non-dependent parms for
18759 convertibility. */
18760 if (saw_undeduced < 2)
18761 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
18762 parms && parms != void_list_node && ia < nargs; )
18763 {
18764 parm = TREE_VALUE (parms);
18765
18766 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
18767 && (!TREE_CHAIN (parms)
18768 || TREE_CHAIN (parms) == void_list_node))
18769 /* For a function parameter pack that occurs at the end of the
18770 parameter-declaration-list, the type A of each remaining
18771 argument of the call is compared with the type P of the
18772 declarator-id of the function parameter pack. */
18773 break;
18774
18775 parms = TREE_CHAIN (parms);
18776
18777 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
18778 /* For a function parameter pack that does not occur at the
18779 end of the parameter-declaration-list, the type of the
18780 parameter pack is a non-deduced context. */
18781 continue;
18782
18783 arg = args[ia];
18784 ++ia;
18785
18786 if (uses_template_parms (parm))
18787 continue;
18788 if (check_non_deducible_conversion (parm, arg, strict, flags,
18789 explain_p))
18790 return 1;
18791 }
18792
18793 /* Now substitute into the default template arguments. */
18794 for (i = 0; i < ntparms; i++)
18795 {
18796 tree targ = TREE_VEC_ELT (targs, i);
18797 tree tparm = TREE_VEC_ELT (tparms, i);
18798
18799 if (targ || tparm == error_mark_node)
18800 continue;
18801 tree parm = TREE_VALUE (tparm);
18802
18803 if (TREE_CODE (parm) == PARM_DECL
18804 && uses_template_parms (TREE_TYPE (parm))
18805 && saw_undeduced < 2)
18806 continue;
18807
18808 tree arg = TREE_PURPOSE (tparm);
18809 reopen_deferring_access_checks (*checks);
18810 location_t save_loc = input_location;
18811 if (DECL_P (parm))
18812 input_location = DECL_SOURCE_LOCATION (parm);
18813 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
18814 arg = convert_template_argument (parm, arg, full_targs, complain,
18815 i, NULL_TREE);
18816 input_location = save_loc;
18817 *checks = get_deferred_access_checks ();
18818 pop_deferring_access_checks ();
18819 if (arg == error_mark_node)
18820 return 1;
18821 else
18822 {
18823 TREE_VEC_ELT (targs, i) = arg;
18824 /* The position of the first default template argument,
18825 is also the number of non-defaulted arguments in TARGS.
18826 Record that. */
18827 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18828 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
18829 continue;
18830 }
18831 }
18832
18833 if (saw_undeduced++ == 1)
18834 goto again;
18835 }
18836
18837 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
18838 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
18839
18840 return unify_success (explain_p);
18841 }
18842
18843 /* Subroutine of type_unification_real. Args are like the variables
18844 at the call site. ARG is an overloaded function (or template-id);
18845 we try deducing template args from each of the overloads, and if
18846 only one succeeds, we go with that. Modifies TARGS and returns
18847 true on success. */
18848
18849 static bool
18850 resolve_overloaded_unification (tree tparms,
18851 tree targs,
18852 tree parm,
18853 tree arg,
18854 unification_kind_t strict,
18855 int sub_strict,
18856 bool explain_p)
18857 {
18858 tree tempargs = copy_node (targs);
18859 int good = 0;
18860 tree goodfn = NULL_TREE;
18861 bool addr_p;
18862
18863 if (TREE_CODE (arg) == ADDR_EXPR)
18864 {
18865 arg = TREE_OPERAND (arg, 0);
18866 addr_p = true;
18867 }
18868 else
18869 addr_p = false;
18870
18871 if (TREE_CODE (arg) == COMPONENT_REF)
18872 /* Handle `&x' where `x' is some static or non-static member
18873 function name. */
18874 arg = TREE_OPERAND (arg, 1);
18875
18876 if (TREE_CODE (arg) == OFFSET_REF)
18877 arg = TREE_OPERAND (arg, 1);
18878
18879 /* Strip baselink information. */
18880 if (BASELINK_P (arg))
18881 arg = BASELINK_FUNCTIONS (arg);
18882
18883 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
18884 {
18885 /* If we got some explicit template args, we need to plug them into
18886 the affected templates before we try to unify, in case the
18887 explicit args will completely resolve the templates in question. */
18888
18889 int ok = 0;
18890 tree expl_subargs = TREE_OPERAND (arg, 1);
18891 arg = TREE_OPERAND (arg, 0);
18892
18893 for (; arg; arg = OVL_NEXT (arg))
18894 {
18895 tree fn = OVL_CURRENT (arg);
18896 tree subargs, elem;
18897
18898 if (TREE_CODE (fn) != TEMPLATE_DECL)
18899 continue;
18900
18901 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18902 expl_subargs, NULL_TREE, tf_none,
18903 /*require_all_args=*/true,
18904 /*use_default_args=*/true);
18905 if (subargs != error_mark_node
18906 && !any_dependent_template_arguments_p (subargs))
18907 {
18908 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
18909 if (try_one_overload (tparms, targs, tempargs, parm,
18910 elem, strict, sub_strict, addr_p, explain_p)
18911 && (!goodfn || !same_type_p (goodfn, elem)))
18912 {
18913 goodfn = elem;
18914 ++good;
18915 }
18916 }
18917 else if (subargs)
18918 ++ok;
18919 }
18920 /* If no templates (or more than one) are fully resolved by the
18921 explicit arguments, this template-id is a non-deduced context; it
18922 could still be OK if we deduce all template arguments for the
18923 enclosing call through other arguments. */
18924 if (good != 1)
18925 good = ok;
18926 }
18927 else if (TREE_CODE (arg) != OVERLOAD
18928 && TREE_CODE (arg) != FUNCTION_DECL)
18929 /* If ARG is, for example, "(0, &f)" then its type will be unknown
18930 -- but the deduction does not succeed because the expression is
18931 not just the function on its own. */
18932 return false;
18933 else
18934 for (; arg; arg = OVL_NEXT (arg))
18935 if (try_one_overload (tparms, targs, tempargs, parm,
18936 TREE_TYPE (OVL_CURRENT (arg)),
18937 strict, sub_strict, addr_p, explain_p)
18938 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
18939 {
18940 goodfn = OVL_CURRENT (arg);
18941 ++good;
18942 }
18943
18944 /* [temp.deduct.type] A template-argument can be deduced from a pointer
18945 to function or pointer to member function argument if the set of
18946 overloaded functions does not contain function templates and at most
18947 one of a set of overloaded functions provides a unique match.
18948
18949 So if we found multiple possibilities, we return success but don't
18950 deduce anything. */
18951
18952 if (good == 1)
18953 {
18954 int i = TREE_VEC_LENGTH (targs);
18955 for (; i--; )
18956 if (TREE_VEC_ELT (tempargs, i))
18957 {
18958 tree old = TREE_VEC_ELT (targs, i);
18959 tree new_ = TREE_VEC_ELT (tempargs, i);
18960 if (new_ && old && ARGUMENT_PACK_P (old)
18961 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
18962 /* Don't forget explicit template arguments in a pack. */
18963 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
18964 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
18965 TREE_VEC_ELT (targs, i) = new_;
18966 }
18967 }
18968 if (good)
18969 return true;
18970
18971 return false;
18972 }
18973
18974 /* Core DR 115: In contexts where deduction is done and fails, or in
18975 contexts where deduction is not done, if a template argument list is
18976 specified and it, along with any default template arguments, identifies
18977 a single function template specialization, then the template-id is an
18978 lvalue for the function template specialization. */
18979
18980 tree
18981 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
18982 {
18983 tree expr, offset, baselink;
18984 bool addr;
18985
18986 if (!type_unknown_p (orig_expr))
18987 return orig_expr;
18988
18989 expr = orig_expr;
18990 addr = false;
18991 offset = NULL_TREE;
18992 baselink = NULL_TREE;
18993
18994 if (TREE_CODE (expr) == ADDR_EXPR)
18995 {
18996 expr = TREE_OPERAND (expr, 0);
18997 addr = true;
18998 }
18999 if (TREE_CODE (expr) == OFFSET_REF)
19000 {
19001 offset = expr;
19002 expr = TREE_OPERAND (expr, 1);
19003 }
19004 if (BASELINK_P (expr))
19005 {
19006 baselink = expr;
19007 expr = BASELINK_FUNCTIONS (expr);
19008 }
19009
19010 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19011 {
19012 int good = 0;
19013 tree goodfn = NULL_TREE;
19014
19015 /* If we got some explicit template args, we need to plug them into
19016 the affected templates before we try to unify, in case the
19017 explicit args will completely resolve the templates in question. */
19018
19019 tree expl_subargs = TREE_OPERAND (expr, 1);
19020 tree arg = TREE_OPERAND (expr, 0);
19021 tree badfn = NULL_TREE;
19022 tree badargs = NULL_TREE;
19023
19024 for (; arg; arg = OVL_NEXT (arg))
19025 {
19026 tree fn = OVL_CURRENT (arg);
19027 tree subargs, elem;
19028
19029 if (TREE_CODE (fn) != TEMPLATE_DECL)
19030 continue;
19031
19032 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19033 expl_subargs, NULL_TREE, tf_none,
19034 /*require_all_args=*/true,
19035 /*use_default_args=*/true);
19036 if (subargs != error_mark_node
19037 && !any_dependent_template_arguments_p (subargs))
19038 {
19039 elem = instantiate_template (fn, subargs, tf_none);
19040 if (elem == error_mark_node)
19041 {
19042 badfn = fn;
19043 badargs = subargs;
19044 }
19045 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19046 {
19047 goodfn = elem;
19048 ++good;
19049 }
19050 }
19051 }
19052 if (good == 1)
19053 {
19054 mark_used (goodfn);
19055 expr = goodfn;
19056 if (baselink)
19057 expr = build_baselink (BASELINK_BINFO (baselink),
19058 BASELINK_ACCESS_BINFO (baselink),
19059 expr, BASELINK_OPTYPE (baselink));
19060 if (offset)
19061 {
19062 tree base
19063 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19064 expr = build_offset_ref (base, expr, addr, complain);
19065 }
19066 if (addr)
19067 expr = cp_build_addr_expr (expr, complain);
19068 return expr;
19069 }
19070 else if (good == 0 && badargs && (complain & tf_error))
19071 /* There were no good options and at least one bad one, so let the
19072 user know what the problem is. */
19073 instantiate_template (badfn, badargs, complain);
19074 }
19075 return orig_expr;
19076 }
19077
19078 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19079 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19080 different overloads deduce different arguments for a given parm.
19081 ADDR_P is true if the expression for which deduction is being
19082 performed was of the form "& fn" rather than simply "fn".
19083
19084 Returns 1 on success. */
19085
19086 static int
19087 try_one_overload (tree tparms,
19088 tree orig_targs,
19089 tree targs,
19090 tree parm,
19091 tree arg,
19092 unification_kind_t strict,
19093 int sub_strict,
19094 bool addr_p,
19095 bool explain_p)
19096 {
19097 int nargs;
19098 tree tempargs;
19099 int i;
19100
19101 if (arg == error_mark_node)
19102 return 0;
19103
19104 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19105 to function or pointer to member function argument if the set of
19106 overloaded functions does not contain function templates and at most
19107 one of a set of overloaded functions provides a unique match.
19108
19109 So if this is a template, just return success. */
19110
19111 if (uses_template_parms (arg))
19112 return 1;
19113
19114 if (TREE_CODE (arg) == METHOD_TYPE)
19115 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19116 else if (addr_p)
19117 arg = build_pointer_type (arg);
19118
19119 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19120
19121 /* We don't copy orig_targs for this because if we have already deduced
19122 some template args from previous args, unify would complain when we
19123 try to deduce a template parameter for the same argument, even though
19124 there isn't really a conflict. */
19125 nargs = TREE_VEC_LENGTH (targs);
19126 tempargs = make_tree_vec (nargs);
19127
19128 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19129 return 0;
19130
19131 /* First make sure we didn't deduce anything that conflicts with
19132 explicitly specified args. */
19133 for (i = nargs; i--; )
19134 {
19135 tree elt = TREE_VEC_ELT (tempargs, i);
19136 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19137
19138 if (!elt)
19139 /*NOP*/;
19140 else if (uses_template_parms (elt))
19141 /* Since we're unifying against ourselves, we will fill in
19142 template args used in the function parm list with our own
19143 template parms. Discard them. */
19144 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19145 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19146 {
19147 /* Check that the argument at each index of the deduced argument pack
19148 is equivalent to the corresponding explicitly specified argument.
19149 We may have deduced more arguments than were explicitly specified,
19150 and that's OK. */
19151 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
19152 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
19153 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
19154
19155 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19156 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19157
19158 if (TREE_VEC_LENGTH (deduced_pack)
19159 < TREE_VEC_LENGTH (explicit_pack))
19160 return 0;
19161
19162 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19163 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19164 TREE_VEC_ELT (deduced_pack, j)))
19165 return 0;
19166 }
19167 else if (oldelt && !template_args_equal (oldelt, elt))
19168 return 0;
19169 }
19170
19171 for (i = nargs; i--; )
19172 {
19173 tree elt = TREE_VEC_ELT (tempargs, i);
19174
19175 if (elt)
19176 TREE_VEC_ELT (targs, i) = elt;
19177 }
19178
19179 return 1;
19180 }
19181
19182 /* PARM is a template class (perhaps with unbound template
19183 parameters). ARG is a fully instantiated type. If ARG can be
19184 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19185 TARGS are as for unify. */
19186
19187 static tree
19188 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19189 bool explain_p)
19190 {
19191 tree copy_of_targs;
19192
19193 if (!CLASSTYPE_TEMPLATE_INFO (arg)
19194 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19195 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
19196 return NULL_TREE;
19197
19198 /* We need to make a new template argument vector for the call to
19199 unify. If we used TARGS, we'd clutter it up with the result of
19200 the attempted unification, even if this class didn't work out.
19201 We also don't want to commit ourselves to all the unifications
19202 we've already done, since unification is supposed to be done on
19203 an argument-by-argument basis. In other words, consider the
19204 following pathological case:
19205
19206 template <int I, int J, int K>
19207 struct S {};
19208
19209 template <int I, int J>
19210 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19211
19212 template <int I, int J, int K>
19213 void f(S<I, J, K>, S<I, I, I>);
19214
19215 void g() {
19216 S<0, 0, 0> s0;
19217 S<0, 1, 2> s2;
19218
19219 f(s0, s2);
19220 }
19221
19222 Now, by the time we consider the unification involving `s2', we
19223 already know that we must have `f<0, 0, 0>'. But, even though
19224 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19225 because there are two ways to unify base classes of S<0, 1, 2>
19226 with S<I, I, I>. If we kept the already deduced knowledge, we
19227 would reject the possibility I=1. */
19228 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19229
19230 /* If unification failed, we're done. */
19231 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19232 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19233 return NULL_TREE;
19234
19235 return arg;
19236 }
19237
19238 /* Given a template type PARM and a class type ARG, find the unique
19239 base type in ARG that is an instance of PARM. We do not examine
19240 ARG itself; only its base-classes. If there is not exactly one
19241 appropriate base class, return NULL_TREE. PARM may be the type of
19242 a partial specialization, as well as a plain template type. Used
19243 by unify. */
19244
19245 static enum template_base_result
19246 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19247 bool explain_p, tree *result)
19248 {
19249 tree rval = NULL_TREE;
19250 tree binfo;
19251
19252 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19253
19254 binfo = TYPE_BINFO (complete_type (arg));
19255 if (!binfo)
19256 {
19257 /* The type could not be completed. */
19258 *result = NULL_TREE;
19259 return tbr_incomplete_type;
19260 }
19261
19262 /* Walk in inheritance graph order. The search order is not
19263 important, and this avoids multiple walks of virtual bases. */
19264 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19265 {
19266 tree r = try_class_unification (tparms, targs, parm,
19267 BINFO_TYPE (binfo), explain_p);
19268
19269 if (r)
19270 {
19271 /* If there is more than one satisfactory baseclass, then:
19272
19273 [temp.deduct.call]
19274
19275 If they yield more than one possible deduced A, the type
19276 deduction fails.
19277
19278 applies. */
19279 if (rval && !same_type_p (r, rval))
19280 {
19281 *result = NULL_TREE;
19282 return tbr_ambiguous_baseclass;
19283 }
19284
19285 rval = r;
19286 }
19287 }
19288
19289 *result = rval;
19290 return tbr_success;
19291 }
19292
19293 /* Returns the level of DECL, which declares a template parameter. */
19294
19295 static int
19296 template_decl_level (tree decl)
19297 {
19298 switch (TREE_CODE (decl))
19299 {
19300 case TYPE_DECL:
19301 case TEMPLATE_DECL:
19302 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19303
19304 case PARM_DECL:
19305 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19306
19307 default:
19308 gcc_unreachable ();
19309 }
19310 return 0;
19311 }
19312
19313 /* Decide whether ARG can be unified with PARM, considering only the
19314 cv-qualifiers of each type, given STRICT as documented for unify.
19315 Returns nonzero iff the unification is OK on that basis. */
19316
19317 static int
19318 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19319 {
19320 int arg_quals = cp_type_quals (arg);
19321 int parm_quals = cp_type_quals (parm);
19322
19323 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19324 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19325 {
19326 /* Although a CVR qualifier is ignored when being applied to a
19327 substituted template parameter ([8.3.2]/1 for example), that
19328 does not allow us to unify "const T" with "int&" because both
19329 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19330 It is ok when we're allowing additional CV qualifiers
19331 at the outer level [14.8.2.1]/3,1st bullet. */
19332 if ((TREE_CODE (arg) == REFERENCE_TYPE
19333 || TREE_CODE (arg) == FUNCTION_TYPE
19334 || TREE_CODE (arg) == METHOD_TYPE)
19335 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19336 return 0;
19337
19338 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19339 && (parm_quals & TYPE_QUAL_RESTRICT))
19340 return 0;
19341 }
19342
19343 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19344 && (arg_quals & parm_quals) != parm_quals)
19345 return 0;
19346
19347 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19348 && (parm_quals & arg_quals) != arg_quals)
19349 return 0;
19350
19351 return 1;
19352 }
19353
19354 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19355 void
19356 template_parm_level_and_index (tree parm, int* level, int* index)
19357 {
19358 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19359 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19360 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19361 {
19362 *index = TEMPLATE_TYPE_IDX (parm);
19363 *level = TEMPLATE_TYPE_LEVEL (parm);
19364 }
19365 else
19366 {
19367 *index = TEMPLATE_PARM_IDX (parm);
19368 *level = TEMPLATE_PARM_LEVEL (parm);
19369 }
19370 }
19371
19372 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19373 do { \
19374 if (unify (TP, TA, P, A, S, EP)) \
19375 return 1; \
19376 } while (0)
19377
19378 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19379 expansion at the end of PACKED_PARMS. Returns 0 if the type
19380 deduction succeeds, 1 otherwise. STRICT is the same as in
19381 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
19382 call argument list. We'll need to adjust the arguments to make them
19383 types. SUBR tells us if this is from a recursive call to
19384 type_unification_real, or for comparing two template argument
19385 lists. */
19386
19387 static int
19388 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19389 tree packed_args, unification_kind_t strict,
19390 bool subr, bool explain_p)
19391 {
19392 tree parm
19393 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19394 tree pattern = PACK_EXPANSION_PATTERN (parm);
19395 tree pack, packs = NULL_TREE;
19396 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19397
19398 packed_args = expand_template_argument_pack (packed_args);
19399
19400 int len = TREE_VEC_LENGTH (packed_args);
19401
19402 /* Determine the parameter packs we will be deducing from the
19403 pattern, and record their current deductions. */
19404 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19405 pack; pack = TREE_CHAIN (pack))
19406 {
19407 tree parm_pack = TREE_VALUE (pack);
19408 int idx, level;
19409
19410 /* Determine the index and level of this parameter pack. */
19411 template_parm_level_and_index (parm_pack, &level, &idx);
19412
19413 /* Keep track of the parameter packs and their corresponding
19414 argument packs. */
19415 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19416 TREE_TYPE (packs) = make_tree_vec (len - start);
19417 }
19418
19419 /* Loop through all of the arguments that have not yet been
19420 unified and unify each with the pattern. */
19421 for (i = start; i < len; i++)
19422 {
19423 tree parm;
19424 bool any_explicit = false;
19425 tree arg = TREE_VEC_ELT (packed_args, i);
19426
19427 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19428 or the element of its argument pack at the current index if
19429 this argument was explicitly specified. */
19430 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19431 {
19432 int idx, level;
19433 tree arg, pargs;
19434 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19435
19436 arg = NULL_TREE;
19437 if (TREE_VALUE (pack)
19438 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19439 && (i - start < TREE_VEC_LENGTH (pargs)))
19440 {
19441 any_explicit = true;
19442 arg = TREE_VEC_ELT (pargs, i - start);
19443 }
19444 TMPL_ARG (targs, level, idx) = arg;
19445 }
19446
19447 /* If we had explicit template arguments, substitute them into the
19448 pattern before deduction. */
19449 if (any_explicit)
19450 {
19451 /* Some arguments might still be unspecified or dependent. */
19452 bool dependent;
19453 ++processing_template_decl;
19454 dependent = any_dependent_template_arguments_p (targs);
19455 if (!dependent)
19456 --processing_template_decl;
19457 parm = tsubst (pattern, targs,
19458 explain_p ? tf_warning_or_error : tf_none,
19459 NULL_TREE);
19460 if (dependent)
19461 --processing_template_decl;
19462 if (parm == error_mark_node)
19463 return 1;
19464 }
19465 else
19466 parm = pattern;
19467
19468 /* Unify the pattern with the current argument. */
19469 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19470 explain_p))
19471 return 1;
19472
19473 /* For each parameter pack, collect the deduced value. */
19474 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19475 {
19476 int idx, level;
19477 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19478
19479 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19480 TMPL_ARG (targs, level, idx);
19481 }
19482 }
19483
19484 /* Verify that the results of unification with the parameter packs
19485 produce results consistent with what we've seen before, and make
19486 the deduced argument packs available. */
19487 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19488 {
19489 tree old_pack = TREE_VALUE (pack);
19490 tree new_args = TREE_TYPE (pack);
19491 int i, len = TREE_VEC_LENGTH (new_args);
19492 int idx, level;
19493 bool nondeduced_p = false;
19494
19495 /* By default keep the original deduced argument pack.
19496 If necessary, more specific code is going to update the
19497 resulting deduced argument later down in this function. */
19498 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19499 TMPL_ARG (targs, level, idx) = old_pack;
19500
19501 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
19502 actually deduce anything. */
19503 for (i = 0; i < len && !nondeduced_p; ++i)
19504 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
19505 nondeduced_p = true;
19506 if (nondeduced_p)
19507 continue;
19508
19509 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
19510 {
19511 /* If we had fewer function args than explicit template args,
19512 just use the explicits. */
19513 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19514 int explicit_len = TREE_VEC_LENGTH (explicit_args);
19515 if (len < explicit_len)
19516 new_args = explicit_args;
19517 }
19518
19519 if (!old_pack)
19520 {
19521 tree result;
19522 /* Build the deduced *_ARGUMENT_PACK. */
19523 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
19524 {
19525 result = make_node (NONTYPE_ARGUMENT_PACK);
19526 TREE_TYPE (result) =
19527 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
19528 TREE_CONSTANT (result) = 1;
19529 }
19530 else
19531 result = cxx_make_type (TYPE_ARGUMENT_PACK);
19532
19533 SET_ARGUMENT_PACK_ARGS (result, new_args);
19534
19535 /* Note the deduced argument packs for this parameter
19536 pack. */
19537 TMPL_ARG (targs, level, idx) = result;
19538 }
19539 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
19540 && (ARGUMENT_PACK_ARGS (old_pack)
19541 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
19542 {
19543 /* We only had the explicitly-provided arguments before, but
19544 now we have a complete set of arguments. */
19545 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
19546
19547 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
19548 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
19549 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
19550 }
19551 else
19552 {
19553 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
19554 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
19555
19556 if (!comp_template_args (old_args, new_args,
19557 &bad_old_arg, &bad_new_arg))
19558 /* Inconsistent unification of this parameter pack. */
19559 return unify_parameter_pack_inconsistent (explain_p,
19560 bad_old_arg,
19561 bad_new_arg);
19562 }
19563 }
19564
19565 return unify_success (explain_p);
19566 }
19567
19568 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
19569 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
19570 parameters and return value are as for unify. */
19571
19572 static int
19573 unify_array_domain (tree tparms, tree targs,
19574 tree parm_dom, tree arg_dom,
19575 bool explain_p)
19576 {
19577 tree parm_max;
19578 tree arg_max;
19579 bool parm_cst;
19580 bool arg_cst;
19581
19582 /* Our representation of array types uses "N - 1" as the
19583 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
19584 not an integer constant. We cannot unify arbitrarily
19585 complex expressions, so we eliminate the MINUS_EXPRs
19586 here. */
19587 parm_max = TYPE_MAX_VALUE (parm_dom);
19588 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
19589 if (!parm_cst)
19590 {
19591 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
19592 parm_max = TREE_OPERAND (parm_max, 0);
19593 }
19594 arg_max = TYPE_MAX_VALUE (arg_dom);
19595 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
19596 if (!arg_cst)
19597 {
19598 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
19599 trying to unify the type of a variable with the type
19600 of a template parameter. For example:
19601
19602 template <unsigned int N>
19603 void f (char (&) [N]);
19604 int g();
19605 void h(int i) {
19606 char a[g(i)];
19607 f(a);
19608 }
19609
19610 Here, the type of the ARG will be "int [g(i)]", and
19611 may be a SAVE_EXPR, etc. */
19612 if (TREE_CODE (arg_max) != MINUS_EXPR)
19613 return unify_vla_arg (explain_p, arg_dom);
19614 arg_max = TREE_OPERAND (arg_max, 0);
19615 }
19616
19617 /* If only one of the bounds used a MINUS_EXPR, compensate
19618 by adding one to the other bound. */
19619 if (parm_cst && !arg_cst)
19620 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
19621 integer_type_node,
19622 parm_max,
19623 integer_one_node);
19624 else if (arg_cst && !parm_cst)
19625 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
19626 integer_type_node,
19627 arg_max,
19628 integer_one_node);
19629
19630 return unify (tparms, targs, parm_max, arg_max,
19631 UNIFY_ALLOW_INTEGER, explain_p);
19632 }
19633
19634 /* Deduce the value of template parameters. TPARMS is the (innermost)
19635 set of template parameters to a template. TARGS is the bindings
19636 for those template parameters, as determined thus far; TARGS may
19637 include template arguments for outer levels of template parameters
19638 as well. PARM is a parameter to a template function, or a
19639 subcomponent of that parameter; ARG is the corresponding argument.
19640 This function attempts to match PARM with ARG in a manner
19641 consistent with the existing assignments in TARGS. If more values
19642 are deduced, then TARGS is updated.
19643
19644 Returns 0 if the type deduction succeeds, 1 otherwise. The
19645 parameter STRICT is a bitwise or of the following flags:
19646
19647 UNIFY_ALLOW_NONE:
19648 Require an exact match between PARM and ARG.
19649 UNIFY_ALLOW_MORE_CV_QUAL:
19650 Allow the deduced ARG to be more cv-qualified (by qualification
19651 conversion) than ARG.
19652 UNIFY_ALLOW_LESS_CV_QUAL:
19653 Allow the deduced ARG to be less cv-qualified than ARG.
19654 UNIFY_ALLOW_DERIVED:
19655 Allow the deduced ARG to be a template base class of ARG,
19656 or a pointer to a template base class of the type pointed to by
19657 ARG.
19658 UNIFY_ALLOW_INTEGER:
19659 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
19660 case for more information.
19661 UNIFY_ALLOW_OUTER_LEVEL:
19662 This is the outermost level of a deduction. Used to determine validity
19663 of qualification conversions. A valid qualification conversion must
19664 have const qualified pointers leading up to the inner type which
19665 requires additional CV quals, except at the outer level, where const
19666 is not required [conv.qual]. It would be normal to set this flag in
19667 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
19668 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
19669 This is the outermost level of a deduction, and PARM can be more CV
19670 qualified at this point.
19671 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
19672 This is the outermost level of a deduction, and PARM can be less CV
19673 qualified at this point. */
19674
19675 static int
19676 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
19677 bool explain_p)
19678 {
19679 int idx;
19680 tree targ;
19681 tree tparm;
19682 int strict_in = strict;
19683
19684 /* I don't think this will do the right thing with respect to types.
19685 But the only case I've seen it in so far has been array bounds, where
19686 signedness is the only information lost, and I think that will be
19687 okay. */
19688 while (CONVERT_EXPR_P (parm))
19689 parm = TREE_OPERAND (parm, 0);
19690
19691 if (arg == error_mark_node)
19692 return unify_invalid (explain_p);
19693 if (arg == unknown_type_node
19694 || arg == init_list_type_node)
19695 /* We can't deduce anything from this, but we might get all the
19696 template args from other function args. */
19697 return unify_success (explain_p);
19698
19699 /* If PARM uses template parameters, then we can't bail out here,
19700 even if ARG == PARM, since we won't record unifications for the
19701 template parameters. We might need them if we're trying to
19702 figure out which of two things is more specialized. */
19703 if (arg == parm && !uses_template_parms (parm))
19704 return unify_success (explain_p);
19705
19706 /* Handle init lists early, so the rest of the function can assume
19707 we're dealing with a type. */
19708 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
19709 {
19710 tree elt, elttype;
19711 unsigned i;
19712 tree orig_parm = parm;
19713
19714 /* Replace T with std::initializer_list<T> for deduction. */
19715 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19716 && flag_deduce_init_list)
19717 parm = listify (parm);
19718
19719 if (!is_std_init_list (parm)
19720 && TREE_CODE (parm) != ARRAY_TYPE)
19721 /* We can only deduce from an initializer list argument if the
19722 parameter is std::initializer_list or an array; otherwise this
19723 is a non-deduced context. */
19724 return unify_success (explain_p);
19725
19726 if (TREE_CODE (parm) == ARRAY_TYPE)
19727 elttype = TREE_TYPE (parm);
19728 else
19729 {
19730 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
19731 /* Deduction is defined in terms of a single type, so just punt
19732 on the (bizarre) std::initializer_list<T...>. */
19733 if (PACK_EXPANSION_P (elttype))
19734 return unify_success (explain_p);
19735 }
19736
19737 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
19738 {
19739 int elt_strict = strict;
19740
19741 if (elt == error_mark_node)
19742 return unify_invalid (explain_p);
19743
19744 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
19745 {
19746 tree type = TREE_TYPE (elt);
19747 if (type == error_mark_node)
19748 return unify_invalid (explain_p);
19749 /* It should only be possible to get here for a call. */
19750 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
19751 elt_strict |= maybe_adjust_types_for_deduction
19752 (DEDUCE_CALL, &elttype, &type, elt);
19753 elt = type;
19754 }
19755
19756 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
19757 explain_p);
19758 }
19759
19760 if (TREE_CODE (parm) == ARRAY_TYPE
19761 && deducible_array_bound (TYPE_DOMAIN (parm)))
19762 {
19763 /* Also deduce from the length of the initializer list. */
19764 tree max = size_int (CONSTRUCTOR_NELTS (arg));
19765 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
19766 if (idx == error_mark_node)
19767 return unify_invalid (explain_p);
19768 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
19769 idx, explain_p);
19770 }
19771
19772 /* If the std::initializer_list<T> deduction worked, replace the
19773 deduced A with std::initializer_list<A>. */
19774 if (orig_parm != parm)
19775 {
19776 idx = TEMPLATE_TYPE_IDX (orig_parm);
19777 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19778 targ = listify (targ);
19779 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
19780 }
19781 return unify_success (explain_p);
19782 }
19783
19784 /* Immediately reject some pairs that won't unify because of
19785 cv-qualification mismatches. */
19786 if (TREE_CODE (arg) == TREE_CODE (parm)
19787 && TYPE_P (arg)
19788 /* It is the elements of the array which hold the cv quals of an array
19789 type, and the elements might be template type parms. We'll check
19790 when we recurse. */
19791 && TREE_CODE (arg) != ARRAY_TYPE
19792 /* We check the cv-qualifiers when unifying with template type
19793 parameters below. We want to allow ARG `const T' to unify with
19794 PARM `T' for example, when computing which of two templates
19795 is more specialized, for example. */
19796 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
19797 && !check_cv_quals_for_unify (strict_in, arg, parm))
19798 return unify_cv_qual_mismatch (explain_p, parm, arg);
19799
19800 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
19801 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
19802 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
19803 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
19804 strict &= ~UNIFY_ALLOW_DERIVED;
19805 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19806 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
19807
19808 switch (TREE_CODE (parm))
19809 {
19810 case TYPENAME_TYPE:
19811 case SCOPE_REF:
19812 case UNBOUND_CLASS_TEMPLATE:
19813 /* In a type which contains a nested-name-specifier, template
19814 argument values cannot be deduced for template parameters used
19815 within the nested-name-specifier. */
19816 return unify_success (explain_p);
19817
19818 case TEMPLATE_TYPE_PARM:
19819 case TEMPLATE_TEMPLATE_PARM:
19820 case BOUND_TEMPLATE_TEMPLATE_PARM:
19821 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
19822 if (error_operand_p (tparm))
19823 return unify_invalid (explain_p);
19824
19825 if (TEMPLATE_TYPE_LEVEL (parm)
19826 != template_decl_level (tparm))
19827 /* The PARM is not one we're trying to unify. Just check
19828 to see if it matches ARG. */
19829 {
19830 if (TREE_CODE (arg) == TREE_CODE (parm)
19831 && (is_auto (parm) ? is_auto (arg)
19832 : same_type_p (parm, arg)))
19833 return unify_success (explain_p);
19834 else
19835 return unify_type_mismatch (explain_p, parm, arg);
19836 }
19837 idx = TEMPLATE_TYPE_IDX (parm);
19838 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
19839 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
19840 if (error_operand_p (tparm))
19841 return unify_invalid (explain_p);
19842
19843 /* Check for mixed types and values. */
19844 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19845 && TREE_CODE (tparm) != TYPE_DECL)
19846 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19847 && TREE_CODE (tparm) != TEMPLATE_DECL))
19848 gcc_unreachable ();
19849
19850 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19851 {
19852 /* ARG must be constructed from a template class or a template
19853 template parameter. */
19854 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
19855 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19856 return unify_template_deduction_failure (explain_p, parm, arg);
19857 {
19858 tree parmvec = TYPE_TI_ARGS (parm);
19859 /* An alias template name is never deduced. */
19860 if (TYPE_ALIAS_P (arg))
19861 arg = strip_typedefs (arg);
19862 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
19863 tree full_argvec = add_to_template_args (targs, argvec);
19864 tree parm_parms
19865 = DECL_INNERMOST_TEMPLATE_PARMS
19866 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
19867 int i, len;
19868 int parm_variadic_p = 0;
19869
19870 /* The resolution to DR150 makes clear that default
19871 arguments for an N-argument may not be used to bind T
19872 to a template template parameter with fewer than N
19873 parameters. It is not safe to permit the binding of
19874 default arguments as an extension, as that may change
19875 the meaning of a conforming program. Consider:
19876
19877 struct Dense { static const unsigned int dim = 1; };
19878
19879 template <template <typename> class View,
19880 typename Block>
19881 void operator+(float, View<Block> const&);
19882
19883 template <typename Block,
19884 unsigned int Dim = Block::dim>
19885 struct Lvalue_proxy { operator float() const; };
19886
19887 void
19888 test_1d (void) {
19889 Lvalue_proxy<Dense> p;
19890 float b;
19891 b + p;
19892 }
19893
19894 Here, if Lvalue_proxy is permitted to bind to View, then
19895 the global operator+ will be used; if they are not, the
19896 Lvalue_proxy will be converted to float. */
19897 if (coerce_template_parms (parm_parms,
19898 full_argvec,
19899 TYPE_TI_TEMPLATE (parm),
19900 (explain_p
19901 ? tf_warning_or_error
19902 : tf_none),
19903 /*require_all_args=*/true,
19904 /*use_default_args=*/false)
19905 == error_mark_node)
19906 return 1;
19907
19908 /* Deduce arguments T, i from TT<T> or TT<i>.
19909 We check each element of PARMVEC and ARGVEC individually
19910 rather than the whole TREE_VEC since they can have
19911 different number of elements. */
19912
19913 parmvec = expand_template_argument_pack (parmvec);
19914 argvec = expand_template_argument_pack (argvec);
19915
19916 len = TREE_VEC_LENGTH (parmvec);
19917
19918 /* Check if the parameters end in a pack, making them
19919 variadic. */
19920 if (len > 0
19921 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
19922 parm_variadic_p = 1;
19923
19924 for (i = 0; i < len - parm_variadic_p; ++i)
19925 /* If the template argument list of P contains a pack
19926 expansion that is not the last template argument, the
19927 entire template argument list is a non-deduced
19928 context. */
19929 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
19930 return unify_success (explain_p);
19931
19932 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
19933 return unify_too_few_arguments (explain_p,
19934 TREE_VEC_LENGTH (argvec), len);
19935
19936 for (i = 0; i < len - parm_variadic_p; ++i)
19937 {
19938 RECUR_AND_CHECK_FAILURE (tparms, targs,
19939 TREE_VEC_ELT (parmvec, i),
19940 TREE_VEC_ELT (argvec, i),
19941 UNIFY_ALLOW_NONE, explain_p);
19942 }
19943
19944 if (parm_variadic_p
19945 && unify_pack_expansion (tparms, targs,
19946 parmvec, argvec,
19947 DEDUCE_EXACT,
19948 /*subr=*/true, explain_p))
19949 return 1;
19950 }
19951 arg = TYPE_TI_TEMPLATE (arg);
19952
19953 /* Fall through to deduce template name. */
19954 }
19955
19956 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19957 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19958 {
19959 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
19960
19961 /* Simple cases: Value already set, does match or doesn't. */
19962 if (targ != NULL_TREE && template_args_equal (targ, arg))
19963 return unify_success (explain_p);
19964 else if (targ)
19965 return unify_inconsistency (explain_p, parm, targ, arg);
19966 }
19967 else
19968 {
19969 /* If PARM is `const T' and ARG is only `int', we don't have
19970 a match unless we are allowing additional qualification.
19971 If ARG is `const int' and PARM is just `T' that's OK;
19972 that binds `const int' to `T'. */
19973 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
19974 arg, parm))
19975 return unify_cv_qual_mismatch (explain_p, parm, arg);
19976
19977 /* Consider the case where ARG is `const volatile int' and
19978 PARM is `const T'. Then, T should be `volatile int'. */
19979 arg = cp_build_qualified_type_real
19980 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
19981 if (arg == error_mark_node)
19982 return unify_invalid (explain_p);
19983
19984 /* Simple cases: Value already set, does match or doesn't. */
19985 if (targ != NULL_TREE && same_type_p (targ, arg))
19986 return unify_success (explain_p);
19987 else if (targ)
19988 return unify_inconsistency (explain_p, parm, targ, arg);
19989
19990 /* Make sure that ARG is not a variable-sized array. (Note
19991 that were talking about variable-sized arrays (like
19992 `int[n]'), rather than arrays of unknown size (like
19993 `int[]').) We'll get very confused by such a type since
19994 the bound of the array is not constant, and therefore
19995 not mangleable. Besides, such types are not allowed in
19996 ISO C++, so we can do as we please here. We do allow
19997 them for 'auto' deduction, since that isn't ABI-exposed. */
19998 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
19999 return unify_vla_arg (explain_p, arg);
20000
20001 /* Strip typedefs as in convert_template_argument. */
20002 arg = canonicalize_type_argument (arg, tf_none);
20003 }
20004
20005 /* If ARG is a parameter pack or an expansion, we cannot unify
20006 against it unless PARM is also a parameter pack. */
20007 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20008 && !template_parameter_pack_p (parm))
20009 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20010
20011 /* If the argument deduction results is a METHOD_TYPE,
20012 then there is a problem.
20013 METHOD_TYPE doesn't map to any real C++ type the result of
20014 the deduction can not be of that type. */
20015 if (TREE_CODE (arg) == METHOD_TYPE)
20016 return unify_method_type_error (explain_p, arg);
20017
20018 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20019 return unify_success (explain_p);
20020
20021 case TEMPLATE_PARM_INDEX:
20022 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20023 if (error_operand_p (tparm))
20024 return unify_invalid (explain_p);
20025
20026 if (TEMPLATE_PARM_LEVEL (parm)
20027 != template_decl_level (tparm))
20028 {
20029 /* The PARM is not one we're trying to unify. Just check
20030 to see if it matches ARG. */
20031 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20032 && cp_tree_equal (parm, arg));
20033 if (result)
20034 unify_expression_unequal (explain_p, parm, arg);
20035 return result;
20036 }
20037
20038 idx = TEMPLATE_PARM_IDX (parm);
20039 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20040
20041 if (targ)
20042 {
20043 int x = !cp_tree_equal (targ, arg);
20044 if (x)
20045 unify_inconsistency (explain_p, parm, targ, arg);
20046 return x;
20047 }
20048
20049 /* [temp.deduct.type] If, in the declaration of a function template
20050 with a non-type template-parameter, the non-type
20051 template-parameter is used in an expression in the function
20052 parameter-list and, if the corresponding template-argument is
20053 deduced, the template-argument type shall match the type of the
20054 template-parameter exactly, except that a template-argument
20055 deduced from an array bound may be of any integral type.
20056 The non-type parameter might use already deduced type parameters. */
20057 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20058 if (!TREE_TYPE (arg))
20059 /* Template-parameter dependent expression. Just accept it for now.
20060 It will later be processed in convert_template_argument. */
20061 ;
20062 else if (same_type_p (TREE_TYPE (arg), tparm))
20063 /* OK */;
20064 else if ((strict & UNIFY_ALLOW_INTEGER)
20065 && CP_INTEGRAL_TYPE_P (tparm))
20066 /* Convert the ARG to the type of PARM; the deduced non-type
20067 template argument must exactly match the types of the
20068 corresponding parameter. */
20069 arg = fold (build_nop (tparm, arg));
20070 else if (uses_template_parms (tparm))
20071 /* We haven't deduced the type of this parameter yet. Try again
20072 later. */
20073 return unify_success (explain_p);
20074 else
20075 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20076
20077 /* If ARG is a parameter pack or an expansion, we cannot unify
20078 against it unless PARM is also a parameter pack. */
20079 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20080 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20081 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20082
20083 {
20084 bool removed_attr = false;
20085 arg = strip_typedefs_expr (arg, &removed_attr);
20086 }
20087 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20088 return unify_success (explain_p);
20089
20090 case PTRMEM_CST:
20091 {
20092 /* A pointer-to-member constant can be unified only with
20093 another constant. */
20094 if (TREE_CODE (arg) != PTRMEM_CST)
20095 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20096
20097 /* Just unify the class member. It would be useless (and possibly
20098 wrong, depending on the strict flags) to unify also
20099 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20100 arg refer to the same variable, even if through different
20101 classes. For instance:
20102
20103 struct A { int x; };
20104 struct B : A { };
20105
20106 Unification of &A::x and &B::x must succeed. */
20107 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20108 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20109 }
20110
20111 case POINTER_TYPE:
20112 {
20113 if (!TYPE_PTR_P (arg))
20114 return unify_type_mismatch (explain_p, parm, arg);
20115
20116 /* [temp.deduct.call]
20117
20118 A can be another pointer or pointer to member type that can
20119 be converted to the deduced A via a qualification
20120 conversion (_conv.qual_).
20121
20122 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20123 This will allow for additional cv-qualification of the
20124 pointed-to types if appropriate. */
20125
20126 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20127 /* The derived-to-base conversion only persists through one
20128 level of pointers. */
20129 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20130
20131 return unify (tparms, targs, TREE_TYPE (parm),
20132 TREE_TYPE (arg), strict, explain_p);
20133 }
20134
20135 case REFERENCE_TYPE:
20136 if (TREE_CODE (arg) != REFERENCE_TYPE)
20137 return unify_type_mismatch (explain_p, parm, arg);
20138 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20139 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20140
20141 case ARRAY_TYPE:
20142 if (TREE_CODE (arg) != ARRAY_TYPE)
20143 return unify_type_mismatch (explain_p, parm, arg);
20144 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20145 != (TYPE_DOMAIN (arg) == NULL_TREE))
20146 return unify_type_mismatch (explain_p, parm, arg);
20147 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20148 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20149 if (TYPE_DOMAIN (parm) != NULL_TREE)
20150 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20151 TYPE_DOMAIN (arg), explain_p);
20152 return unify_success (explain_p);
20153
20154 case REAL_TYPE:
20155 case COMPLEX_TYPE:
20156 case VECTOR_TYPE:
20157 case INTEGER_TYPE:
20158 case BOOLEAN_TYPE:
20159 case ENUMERAL_TYPE:
20160 case VOID_TYPE:
20161 case NULLPTR_TYPE:
20162 if (TREE_CODE (arg) != TREE_CODE (parm))
20163 return unify_type_mismatch (explain_p, parm, arg);
20164
20165 /* We have already checked cv-qualification at the top of the
20166 function. */
20167 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20168 return unify_type_mismatch (explain_p, parm, arg);
20169
20170 /* As far as unification is concerned, this wins. Later checks
20171 will invalidate it if necessary. */
20172 return unify_success (explain_p);
20173
20174 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20175 /* Type INTEGER_CST can come from ordinary constant template args. */
20176 case INTEGER_CST:
20177 while (CONVERT_EXPR_P (arg))
20178 arg = TREE_OPERAND (arg, 0);
20179
20180 if (TREE_CODE (arg) != INTEGER_CST)
20181 return unify_template_argument_mismatch (explain_p, parm, arg);
20182 return (tree_int_cst_equal (parm, arg)
20183 ? unify_success (explain_p)
20184 : unify_template_argument_mismatch (explain_p, parm, arg));
20185
20186 case TREE_VEC:
20187 {
20188 int i, len, argslen;
20189 int parm_variadic_p = 0;
20190
20191 if (TREE_CODE (arg) != TREE_VEC)
20192 return unify_template_argument_mismatch (explain_p, parm, arg);
20193
20194 len = TREE_VEC_LENGTH (parm);
20195 argslen = TREE_VEC_LENGTH (arg);
20196
20197 /* Check for pack expansions in the parameters. */
20198 for (i = 0; i < len; ++i)
20199 {
20200 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20201 {
20202 if (i == len - 1)
20203 /* We can unify against something with a trailing
20204 parameter pack. */
20205 parm_variadic_p = 1;
20206 else
20207 /* [temp.deduct.type]/9: If the template argument list of
20208 P contains a pack expansion that is not the last
20209 template argument, the entire template argument list
20210 is a non-deduced context. */
20211 return unify_success (explain_p);
20212 }
20213 }
20214
20215 /* If we don't have enough arguments to satisfy the parameters
20216 (not counting the pack expression at the end), or we have
20217 too many arguments for a parameter list that doesn't end in
20218 a pack expression, we can't unify. */
20219 if (parm_variadic_p
20220 ? argslen < len - parm_variadic_p
20221 : argslen != len)
20222 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20223
20224 /* Unify all of the parameters that precede the (optional)
20225 pack expression. */
20226 for (i = 0; i < len - parm_variadic_p; ++i)
20227 {
20228 RECUR_AND_CHECK_FAILURE (tparms, targs,
20229 TREE_VEC_ELT (parm, i),
20230 TREE_VEC_ELT (arg, i),
20231 UNIFY_ALLOW_NONE, explain_p);
20232 }
20233 if (parm_variadic_p)
20234 return unify_pack_expansion (tparms, targs, parm, arg,
20235 DEDUCE_EXACT,
20236 /*subr=*/true, explain_p);
20237 return unify_success (explain_p);
20238 }
20239
20240 case RECORD_TYPE:
20241 case UNION_TYPE:
20242 if (TREE_CODE (arg) != TREE_CODE (parm))
20243 return unify_type_mismatch (explain_p, parm, arg);
20244
20245 if (TYPE_PTRMEMFUNC_P (parm))
20246 {
20247 if (!TYPE_PTRMEMFUNC_P (arg))
20248 return unify_type_mismatch (explain_p, parm, arg);
20249
20250 return unify (tparms, targs,
20251 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20252 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20253 strict, explain_p);
20254 }
20255 else if (TYPE_PTRMEMFUNC_P (arg))
20256 return unify_type_mismatch (explain_p, parm, arg);
20257
20258 if (CLASSTYPE_TEMPLATE_INFO (parm))
20259 {
20260 tree t = NULL_TREE;
20261
20262 if (strict_in & UNIFY_ALLOW_DERIVED)
20263 {
20264 /* First, we try to unify the PARM and ARG directly. */
20265 t = try_class_unification (tparms, targs,
20266 parm, arg, explain_p);
20267
20268 if (!t)
20269 {
20270 /* Fallback to the special case allowed in
20271 [temp.deduct.call]:
20272
20273 If P is a class, and P has the form
20274 template-id, then A can be a derived class of
20275 the deduced A. Likewise, if P is a pointer to
20276 a class of the form template-id, A can be a
20277 pointer to a derived class pointed to by the
20278 deduced A. */
20279 enum template_base_result r;
20280 r = get_template_base (tparms, targs, parm, arg,
20281 explain_p, &t);
20282
20283 if (!t)
20284 {
20285 /* Don't give the derived diagnostic if we're
20286 already dealing with the same template. */
20287 bool same_template
20288 = (CLASSTYPE_TEMPLATE_INFO (arg)
20289 && (CLASSTYPE_TI_TEMPLATE (parm)
20290 == CLASSTYPE_TI_TEMPLATE (arg)));
20291 return unify_no_common_base (explain_p && !same_template,
20292 r, parm, arg);
20293 }
20294 }
20295 }
20296 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20297 && (CLASSTYPE_TI_TEMPLATE (parm)
20298 == CLASSTYPE_TI_TEMPLATE (arg)))
20299 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20300 Then, we should unify `int' and `U'. */
20301 t = arg;
20302 else
20303 /* There's no chance of unification succeeding. */
20304 return unify_type_mismatch (explain_p, parm, arg);
20305
20306 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20307 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20308 }
20309 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20310 return unify_type_mismatch (explain_p, parm, arg);
20311 return unify_success (explain_p);
20312
20313 case METHOD_TYPE:
20314 case FUNCTION_TYPE:
20315 {
20316 unsigned int nargs;
20317 tree *args;
20318 tree a;
20319 unsigned int i;
20320
20321 if (TREE_CODE (arg) != TREE_CODE (parm))
20322 return unify_type_mismatch (explain_p, parm, arg);
20323
20324 /* CV qualifications for methods can never be deduced, they must
20325 match exactly. We need to check them explicitly here,
20326 because type_unification_real treats them as any other
20327 cv-qualified parameter. */
20328 if (TREE_CODE (parm) == METHOD_TYPE
20329 && (!check_cv_quals_for_unify
20330 (UNIFY_ALLOW_NONE,
20331 class_of_this_parm (arg),
20332 class_of_this_parm (parm))))
20333 return unify_cv_qual_mismatch (explain_p, parm, arg);
20334 if (TREE_CODE (arg) == FUNCTION_TYPE
20335 && type_memfn_quals (parm) != type_memfn_quals (arg))
20336 return unify_cv_qual_mismatch (explain_p, parm, arg);
20337 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20338 return unify_type_mismatch (explain_p, parm, arg);
20339
20340 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20341 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20342
20343 nargs = list_length (TYPE_ARG_TYPES (arg));
20344 args = XALLOCAVEC (tree, nargs);
20345 for (a = TYPE_ARG_TYPES (arg), i = 0;
20346 a != NULL_TREE && a != void_list_node;
20347 a = TREE_CHAIN (a), ++i)
20348 args[i] = TREE_VALUE (a);
20349 nargs = i;
20350
20351 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20352 args, nargs, 1, DEDUCE_EXACT,
20353 LOOKUP_NORMAL, NULL, explain_p))
20354 return 1;
20355
20356 if (flag_noexcept_type)
20357 {
20358 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20359 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20360 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20361 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20362 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20363 && uses_template_parms (TREE_PURPOSE (pspec)))
20364 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20365 TREE_PURPOSE (aspec),
20366 UNIFY_ALLOW_NONE, explain_p);
20367 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20368 return unify_type_mismatch (explain_p, parm, arg);
20369 }
20370
20371 return 0;
20372 }
20373
20374 case OFFSET_TYPE:
20375 /* Unify a pointer to member with a pointer to member function, which
20376 deduces the type of the member as a function type. */
20377 if (TYPE_PTRMEMFUNC_P (arg))
20378 {
20379 /* Check top-level cv qualifiers */
20380 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20381 return unify_cv_qual_mismatch (explain_p, parm, arg);
20382
20383 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20384 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20385 UNIFY_ALLOW_NONE, explain_p);
20386
20387 /* Determine the type of the function we are unifying against. */
20388 tree fntype = static_fn_type (arg);
20389
20390 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20391 }
20392
20393 if (TREE_CODE (arg) != OFFSET_TYPE)
20394 return unify_type_mismatch (explain_p, parm, arg);
20395 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20396 TYPE_OFFSET_BASETYPE (arg),
20397 UNIFY_ALLOW_NONE, explain_p);
20398 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20399 strict, explain_p);
20400
20401 case CONST_DECL:
20402 if (DECL_TEMPLATE_PARM_P (parm))
20403 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20404 if (arg != scalar_constant_value (parm))
20405 return unify_template_argument_mismatch (explain_p, parm, arg);
20406 return unify_success (explain_p);
20407
20408 case FIELD_DECL:
20409 case TEMPLATE_DECL:
20410 /* Matched cases are handled by the ARG == PARM test above. */
20411 return unify_template_argument_mismatch (explain_p, parm, arg);
20412
20413 case VAR_DECL:
20414 /* We might get a variable as a non-type template argument in parm if the
20415 corresponding parameter is type-dependent. Make any necessary
20416 adjustments based on whether arg is a reference. */
20417 if (CONSTANT_CLASS_P (arg))
20418 parm = fold_non_dependent_expr (parm);
20419 else if (REFERENCE_REF_P (arg))
20420 {
20421 tree sub = TREE_OPERAND (arg, 0);
20422 STRIP_NOPS (sub);
20423 if (TREE_CODE (sub) == ADDR_EXPR)
20424 arg = TREE_OPERAND (sub, 0);
20425 }
20426 /* Now use the normal expression code to check whether they match. */
20427 goto expr;
20428
20429 case TYPE_ARGUMENT_PACK:
20430 case NONTYPE_ARGUMENT_PACK:
20431 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20432 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20433
20434 case TYPEOF_TYPE:
20435 case DECLTYPE_TYPE:
20436 case UNDERLYING_TYPE:
20437 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20438 or UNDERLYING_TYPE nodes. */
20439 return unify_success (explain_p);
20440
20441 case ERROR_MARK:
20442 /* Unification fails if we hit an error node. */
20443 return unify_invalid (explain_p);
20444
20445 case INDIRECT_REF:
20446 if (REFERENCE_REF_P (parm))
20447 {
20448 if (REFERENCE_REF_P (arg))
20449 arg = TREE_OPERAND (arg, 0);
20450 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20451 strict, explain_p);
20452 }
20453 /* FALLTHRU */
20454
20455 default:
20456 /* An unresolved overload is a nondeduced context. */
20457 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20458 return unify_success (explain_p);
20459 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20460 expr:
20461 /* We must be looking at an expression. This can happen with
20462 something like:
20463
20464 template <int I>
20465 void foo(S<I>, S<I + 2>);
20466
20467 This is a "nondeduced context":
20468
20469 [deduct.type]
20470
20471 The nondeduced contexts are:
20472
20473 --A type that is a template-id in which one or more of
20474 the template-arguments is an expression that references
20475 a template-parameter.
20476
20477 In these cases, we assume deduction succeeded, but don't
20478 actually infer any unifications. */
20479
20480 if (!uses_template_parms (parm)
20481 && !template_args_equal (parm, arg))
20482 return unify_expression_unequal (explain_p, parm, arg);
20483 else
20484 return unify_success (explain_p);
20485 }
20486 }
20487 #undef RECUR_AND_CHECK_FAILURE
20488 \f
20489 /* Note that DECL can be defined in this translation unit, if
20490 required. */
20491
20492 static void
20493 mark_definable (tree decl)
20494 {
20495 tree clone;
20496 DECL_NOT_REALLY_EXTERN (decl) = 1;
20497 FOR_EACH_CLONE (clone, decl)
20498 DECL_NOT_REALLY_EXTERN (clone) = 1;
20499 }
20500
20501 /* Called if RESULT is explicitly instantiated, or is a member of an
20502 explicitly instantiated class. */
20503
20504 void
20505 mark_decl_instantiated (tree result, int extern_p)
20506 {
20507 SET_DECL_EXPLICIT_INSTANTIATION (result);
20508
20509 /* If this entity has already been written out, it's too late to
20510 make any modifications. */
20511 if (TREE_ASM_WRITTEN (result))
20512 return;
20513
20514 /* For anonymous namespace we don't need to do anything. */
20515 if (decl_anon_ns_mem_p (result))
20516 {
20517 gcc_assert (!TREE_PUBLIC (result));
20518 return;
20519 }
20520
20521 if (TREE_CODE (result) != FUNCTION_DECL)
20522 /* The TREE_PUBLIC flag for function declarations will have been
20523 set correctly by tsubst. */
20524 TREE_PUBLIC (result) = 1;
20525
20526 /* This might have been set by an earlier implicit instantiation. */
20527 DECL_COMDAT (result) = 0;
20528
20529 if (extern_p)
20530 DECL_NOT_REALLY_EXTERN (result) = 0;
20531 else
20532 {
20533 mark_definable (result);
20534 mark_needed (result);
20535 /* Always make artificials weak. */
20536 if (DECL_ARTIFICIAL (result) && flag_weak)
20537 comdat_linkage (result);
20538 /* For WIN32 we also want to put explicit instantiations in
20539 linkonce sections. */
20540 else if (TREE_PUBLIC (result))
20541 maybe_make_one_only (result);
20542 }
20543
20544 /* If EXTERN_P, then this function will not be emitted -- unless
20545 followed by an explicit instantiation, at which point its linkage
20546 will be adjusted. If !EXTERN_P, then this function will be
20547 emitted here. In neither circumstance do we want
20548 import_export_decl to adjust the linkage. */
20549 DECL_INTERFACE_KNOWN (result) = 1;
20550 }
20551
20552 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
20553 important template arguments. If any are missing, we check whether
20554 they're important by using error_mark_node for substituting into any
20555 args that were used for partial ordering (the ones between ARGS and END)
20556 and seeing if it bubbles up. */
20557
20558 static bool
20559 check_undeduced_parms (tree targs, tree args, tree end)
20560 {
20561 bool found = false;
20562 int i;
20563 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
20564 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
20565 {
20566 found = true;
20567 TREE_VEC_ELT (targs, i) = error_mark_node;
20568 }
20569 if (found)
20570 {
20571 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
20572 if (substed == error_mark_node)
20573 return true;
20574 }
20575 return false;
20576 }
20577
20578 /* Given two function templates PAT1 and PAT2, return:
20579
20580 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
20581 -1 if PAT2 is more specialized than PAT1.
20582 0 if neither is more specialized.
20583
20584 LEN indicates the number of parameters we should consider
20585 (defaulted parameters should not be considered).
20586
20587 The 1998 std underspecified function template partial ordering, and
20588 DR214 addresses the issue. We take pairs of arguments, one from
20589 each of the templates, and deduce them against each other. One of
20590 the templates will be more specialized if all the *other*
20591 template's arguments deduce against its arguments and at least one
20592 of its arguments *does* *not* deduce against the other template's
20593 corresponding argument. Deduction is done as for class templates.
20594 The arguments used in deduction have reference and top level cv
20595 qualifiers removed. Iff both arguments were originally reference
20596 types *and* deduction succeeds in both directions, an lvalue reference
20597 wins against an rvalue reference and otherwise the template
20598 with the more cv-qualified argument wins for that pairing (if
20599 neither is more cv-qualified, they both are equal). Unlike regular
20600 deduction, after all the arguments have been deduced in this way,
20601 we do *not* verify the deduced template argument values can be
20602 substituted into non-deduced contexts.
20603
20604 The logic can be a bit confusing here, because we look at deduce1 and
20605 targs1 to see if pat2 is at least as specialized, and vice versa; if we
20606 can find template arguments for pat1 to make arg1 look like arg2, that
20607 means that arg2 is at least as specialized as arg1. */
20608
20609 int
20610 more_specialized_fn (tree pat1, tree pat2, int len)
20611 {
20612 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
20613 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
20614 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
20615 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
20616 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
20617 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
20618 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
20619 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
20620 tree origs1, origs2;
20621 bool lose1 = false;
20622 bool lose2 = false;
20623
20624 /* Remove the this parameter from non-static member functions. If
20625 one is a non-static member function and the other is not a static
20626 member function, remove the first parameter from that function
20627 also. This situation occurs for operator functions where we
20628 locate both a member function (with this pointer) and non-member
20629 operator (with explicit first operand). */
20630 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
20631 {
20632 len--; /* LEN is the number of significant arguments for DECL1 */
20633 args1 = TREE_CHAIN (args1);
20634 if (!DECL_STATIC_FUNCTION_P (decl2))
20635 args2 = TREE_CHAIN (args2);
20636 }
20637 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
20638 {
20639 args2 = TREE_CHAIN (args2);
20640 if (!DECL_STATIC_FUNCTION_P (decl1))
20641 {
20642 len--;
20643 args1 = TREE_CHAIN (args1);
20644 }
20645 }
20646
20647 /* If only one is a conversion operator, they are unordered. */
20648 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
20649 return 0;
20650
20651 /* Consider the return type for a conversion function */
20652 if (DECL_CONV_FN_P (decl1))
20653 {
20654 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
20655 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
20656 len++;
20657 }
20658
20659 processing_template_decl++;
20660
20661 origs1 = args1;
20662 origs2 = args2;
20663
20664 while (len--
20665 /* Stop when an ellipsis is seen. */
20666 && args1 != NULL_TREE && args2 != NULL_TREE)
20667 {
20668 tree arg1 = TREE_VALUE (args1);
20669 tree arg2 = TREE_VALUE (args2);
20670 int deduce1, deduce2;
20671 int quals1 = -1;
20672 int quals2 = -1;
20673 int ref1 = 0;
20674 int ref2 = 0;
20675
20676 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20677 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20678 {
20679 /* When both arguments are pack expansions, we need only
20680 unify the patterns themselves. */
20681 arg1 = PACK_EXPANSION_PATTERN (arg1);
20682 arg2 = PACK_EXPANSION_PATTERN (arg2);
20683
20684 /* This is the last comparison we need to do. */
20685 len = 0;
20686 }
20687
20688 if (TREE_CODE (arg1) == REFERENCE_TYPE)
20689 {
20690 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
20691 arg1 = TREE_TYPE (arg1);
20692 quals1 = cp_type_quals (arg1);
20693 }
20694
20695 if (TREE_CODE (arg2) == REFERENCE_TYPE)
20696 {
20697 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
20698 arg2 = TREE_TYPE (arg2);
20699 quals2 = cp_type_quals (arg2);
20700 }
20701
20702 arg1 = TYPE_MAIN_VARIANT (arg1);
20703 arg2 = TYPE_MAIN_VARIANT (arg2);
20704
20705 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
20706 {
20707 int i, len2 = remaining_arguments (args2);
20708 tree parmvec = make_tree_vec (1);
20709 tree argvec = make_tree_vec (len2);
20710 tree ta = args2;
20711
20712 /* Setup the parameter vector, which contains only ARG1. */
20713 TREE_VEC_ELT (parmvec, 0) = arg1;
20714
20715 /* Setup the argument vector, which contains the remaining
20716 arguments. */
20717 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
20718 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20719
20720 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
20721 argvec, DEDUCE_EXACT,
20722 /*subr=*/true, /*explain_p=*/false)
20723 == 0);
20724
20725 /* We cannot deduce in the other direction, because ARG1 is
20726 a pack expansion but ARG2 is not. */
20727 deduce2 = 0;
20728 }
20729 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20730 {
20731 int i, len1 = remaining_arguments (args1);
20732 tree parmvec = make_tree_vec (1);
20733 tree argvec = make_tree_vec (len1);
20734 tree ta = args1;
20735
20736 /* Setup the parameter vector, which contains only ARG1. */
20737 TREE_VEC_ELT (parmvec, 0) = arg2;
20738
20739 /* Setup the argument vector, which contains the remaining
20740 arguments. */
20741 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
20742 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
20743
20744 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
20745 argvec, DEDUCE_EXACT,
20746 /*subr=*/true, /*explain_p=*/false)
20747 == 0);
20748
20749 /* We cannot deduce in the other direction, because ARG2 is
20750 a pack expansion but ARG1 is not.*/
20751 deduce1 = 0;
20752 }
20753
20754 else
20755 {
20756 /* The normal case, where neither argument is a pack
20757 expansion. */
20758 deduce1 = (unify (tparms1, targs1, arg1, arg2,
20759 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20760 == 0);
20761 deduce2 = (unify (tparms2, targs2, arg2, arg1,
20762 UNIFY_ALLOW_NONE, /*explain_p=*/false)
20763 == 0);
20764 }
20765
20766 /* If we couldn't deduce arguments for tparms1 to make arg1 match
20767 arg2, then arg2 is not as specialized as arg1. */
20768 if (!deduce1)
20769 lose2 = true;
20770 if (!deduce2)
20771 lose1 = true;
20772
20773 /* "If, for a given type, deduction succeeds in both directions
20774 (i.e., the types are identical after the transformations above)
20775 and both P and A were reference types (before being replaced with
20776 the type referred to above):
20777 - if the type from the argument template was an lvalue reference and
20778 the type from the parameter template was not, the argument type is
20779 considered to be more specialized than the other; otherwise,
20780 - if the type from the argument template is more cv-qualified
20781 than the type from the parameter template (as described above),
20782 the argument type is considered to be more specialized than the other;
20783 otherwise,
20784 - neither type is more specialized than the other." */
20785
20786 if (deduce1 && deduce2)
20787 {
20788 if (ref1 && ref2 && ref1 != ref2)
20789 {
20790 if (ref1 > ref2)
20791 lose1 = true;
20792 else
20793 lose2 = true;
20794 }
20795 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
20796 {
20797 if ((quals1 & quals2) == quals2)
20798 lose2 = true;
20799 if ((quals1 & quals2) == quals1)
20800 lose1 = true;
20801 }
20802 }
20803
20804 if (lose1 && lose2)
20805 /* We've failed to deduce something in either direction.
20806 These must be unordered. */
20807 break;
20808
20809 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
20810 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
20811 /* We have already processed all of the arguments in our
20812 handing of the pack expansion type. */
20813 len = 0;
20814
20815 args1 = TREE_CHAIN (args1);
20816 args2 = TREE_CHAIN (args2);
20817 }
20818
20819 /* "In most cases, all template parameters must have values in order for
20820 deduction to succeed, but for partial ordering purposes a template
20821 parameter may remain without a value provided it is not used in the
20822 types being used for partial ordering."
20823
20824 Thus, if we are missing any of the targs1 we need to substitute into
20825 origs1, then pat2 is not as specialized as pat1. This can happen when
20826 there is a nondeduced context. */
20827 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
20828 lose2 = true;
20829 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
20830 lose1 = true;
20831
20832 processing_template_decl--;
20833
20834 /* If both deductions succeed, the partial ordering selects the more
20835 constrained template. */
20836 if (!lose1 && !lose2)
20837 {
20838 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
20839 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
20840 lose1 = !subsumes_constraints (c1, c2);
20841 lose2 = !subsumes_constraints (c2, c1);
20842 }
20843
20844 /* All things being equal, if the next argument is a pack expansion
20845 for one function but not for the other, prefer the
20846 non-variadic function. FIXME this is bogus; see c++/41958. */
20847 if (lose1 == lose2
20848 && args1 && TREE_VALUE (args1)
20849 && args2 && TREE_VALUE (args2))
20850 {
20851 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
20852 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
20853 }
20854
20855 if (lose1 == lose2)
20856 return 0;
20857 else if (!lose1)
20858 return 1;
20859 else
20860 return -1;
20861 }
20862
20863 /* Determine which of two partial specializations of TMPL is more
20864 specialized.
20865
20866 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
20867 to the first partial specialization. The TREE_PURPOSE is the
20868 innermost set of template parameters for the partial
20869 specialization. PAT2 is similar, but for the second template.
20870
20871 Return 1 if the first partial specialization is more specialized;
20872 -1 if the second is more specialized; 0 if neither is more
20873 specialized.
20874
20875 See [temp.class.order] for information about determining which of
20876 two templates is more specialized. */
20877
20878 static int
20879 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
20880 {
20881 tree targs;
20882 int winner = 0;
20883 bool any_deductions = false;
20884
20885 tree tmpl1 = TREE_VALUE (pat1);
20886 tree tmpl2 = TREE_VALUE (pat2);
20887 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
20888 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
20889
20890 /* Just like what happens for functions, if we are ordering between
20891 different template specializations, we may encounter dependent
20892 types in the arguments, and we need our dependency check functions
20893 to behave correctly. */
20894 ++processing_template_decl;
20895 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
20896 if (targs)
20897 {
20898 --winner;
20899 any_deductions = true;
20900 }
20901
20902 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
20903 if (targs)
20904 {
20905 ++winner;
20906 any_deductions = true;
20907 }
20908 --processing_template_decl;
20909
20910 /* If both deductions succeed, the partial ordering selects the more
20911 constrained template. */
20912 if (!winner && any_deductions)
20913 return more_constrained (tmpl1, tmpl2);
20914
20915 /* In the case of a tie where at least one of the templates
20916 has a parameter pack at the end, the template with the most
20917 non-packed parameters wins. */
20918 if (winner == 0
20919 && any_deductions
20920 && (template_args_variadic_p (TREE_PURPOSE (pat1))
20921 || template_args_variadic_p (TREE_PURPOSE (pat2))))
20922 {
20923 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
20924 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
20925 int len1 = TREE_VEC_LENGTH (args1);
20926 int len2 = TREE_VEC_LENGTH (args2);
20927
20928 /* We don't count the pack expansion at the end. */
20929 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
20930 --len1;
20931 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
20932 --len2;
20933
20934 if (len1 > len2)
20935 return 1;
20936 else if (len1 < len2)
20937 return -1;
20938 }
20939
20940 return winner;
20941 }
20942
20943 /* Return the template arguments that will produce the function signature
20944 DECL from the function template FN, with the explicit template
20945 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
20946 also match. Return NULL_TREE if no satisfactory arguments could be
20947 found. */
20948
20949 static tree
20950 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
20951 {
20952 int ntparms = DECL_NTPARMS (fn);
20953 tree targs = make_tree_vec (ntparms);
20954 tree decl_type = TREE_TYPE (decl);
20955 tree decl_arg_types;
20956 tree *args;
20957 unsigned int nargs, ix;
20958 tree arg;
20959
20960 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
20961
20962 /* Never do unification on the 'this' parameter. */
20963 decl_arg_types = skip_artificial_parms_for (decl,
20964 TYPE_ARG_TYPES (decl_type));
20965
20966 nargs = list_length (decl_arg_types);
20967 args = XALLOCAVEC (tree, nargs);
20968 for (arg = decl_arg_types, ix = 0;
20969 arg != NULL_TREE && arg != void_list_node;
20970 arg = TREE_CHAIN (arg), ++ix)
20971 args[ix] = TREE_VALUE (arg);
20972
20973 if (fn_type_unification (fn, explicit_args, targs,
20974 args, ix,
20975 (check_rettype || DECL_CONV_FN_P (fn)
20976 ? TREE_TYPE (decl_type) : NULL_TREE),
20977 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
20978 /*decltype*/false)
20979 == error_mark_node)
20980 return NULL_TREE;
20981
20982 return targs;
20983 }
20984
20985 /* Return the innermost template arguments that, when applied to a partial
20986 specialization SPEC_TMPL of TMPL, yield the ARGS.
20987
20988 For example, suppose we have:
20989
20990 template <class T, class U> struct S {};
20991 template <class T> struct S<T*, int> {};
20992
20993 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
20994 partial specialization and the ARGS will be {double*, int}. The resulting
20995 vector will be {double}, indicating that `T' is bound to `double'. */
20996
20997 static tree
20998 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
20999 {
21000 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21001 tree spec_args
21002 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21003 int i, ntparms = TREE_VEC_LENGTH (tparms);
21004 tree deduced_args;
21005 tree innermost_deduced_args;
21006
21007 innermost_deduced_args = make_tree_vec (ntparms);
21008 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21009 {
21010 deduced_args = copy_node (args);
21011 SET_TMPL_ARGS_LEVEL (deduced_args,
21012 TMPL_ARGS_DEPTH (deduced_args),
21013 innermost_deduced_args);
21014 }
21015 else
21016 deduced_args = innermost_deduced_args;
21017
21018 if (unify (tparms, deduced_args,
21019 INNERMOST_TEMPLATE_ARGS (spec_args),
21020 INNERMOST_TEMPLATE_ARGS (args),
21021 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21022 return NULL_TREE;
21023
21024 for (i = 0; i < ntparms; ++i)
21025 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21026 return NULL_TREE;
21027
21028 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21029 if (!push_tinst_level (tinst))
21030 {
21031 excessive_deduction_depth = true;
21032 return NULL_TREE;
21033 }
21034
21035 /* Verify that nondeduced template arguments agree with the type
21036 obtained from argument deduction.
21037
21038 For example:
21039
21040 struct A { typedef int X; };
21041 template <class T, class U> struct C {};
21042 template <class T> struct C<T, typename T::X> {};
21043
21044 Then with the instantiation `C<A, int>', we can deduce that
21045 `T' is `A' but unify () does not check whether `typename T::X'
21046 is `int'. */
21047 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21048 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21049 spec_args, tmpl,
21050 tf_none, false, false);
21051
21052 pop_tinst_level ();
21053
21054 if (spec_args == error_mark_node
21055 /* We only need to check the innermost arguments; the other
21056 arguments will always agree. */
21057 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
21058 INNERMOST_TEMPLATE_ARGS (args)))
21059 return NULL_TREE;
21060
21061 /* Now that we have bindings for all of the template arguments,
21062 ensure that the arguments deduced for the template template
21063 parameters have compatible template parameter lists. See the use
21064 of template_template_parm_bindings_ok_p in fn_type_unification
21065 for more information. */
21066 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21067 return NULL_TREE;
21068
21069 return deduced_args;
21070 }
21071
21072 // Compare two function templates T1 and T2 by deducing bindings
21073 // from one against the other. If both deductions succeed, compare
21074 // constraints to see which is more constrained.
21075 static int
21076 more_specialized_inst (tree t1, tree t2)
21077 {
21078 int fate = 0;
21079 int count = 0;
21080
21081 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21082 {
21083 --fate;
21084 ++count;
21085 }
21086
21087 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21088 {
21089 ++fate;
21090 ++count;
21091 }
21092
21093 // If both deductions succeed, then one may be more constrained.
21094 if (count == 2 && fate == 0)
21095 fate = more_constrained (t1, t2);
21096
21097 return fate;
21098 }
21099
21100 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21101 Return the TREE_LIST node with the most specialized template, if
21102 any. If there is no most specialized template, the error_mark_node
21103 is returned.
21104
21105 Note that this function does not look at, or modify, the
21106 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21107 returned is one of the elements of INSTANTIATIONS, callers may
21108 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21109 and retrieve it from the value returned. */
21110
21111 tree
21112 most_specialized_instantiation (tree templates)
21113 {
21114 tree fn, champ;
21115
21116 ++processing_template_decl;
21117
21118 champ = templates;
21119 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21120 {
21121 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21122 if (fate == -1)
21123 champ = fn;
21124 else if (!fate)
21125 {
21126 /* Equally specialized, move to next function. If there
21127 is no next function, nothing's most specialized. */
21128 fn = TREE_CHAIN (fn);
21129 champ = fn;
21130 if (!fn)
21131 break;
21132 }
21133 }
21134
21135 if (champ)
21136 /* Now verify that champ is better than everything earlier in the
21137 instantiation list. */
21138 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21139 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21140 {
21141 champ = NULL_TREE;
21142 break;
21143 }
21144 }
21145
21146 processing_template_decl--;
21147
21148 if (!champ)
21149 return error_mark_node;
21150
21151 return champ;
21152 }
21153
21154 /* If DECL is a specialization of some template, return the most
21155 general such template. Otherwise, returns NULL_TREE.
21156
21157 For example, given:
21158
21159 template <class T> struct S { template <class U> void f(U); };
21160
21161 if TMPL is `template <class U> void S<int>::f(U)' this will return
21162 the full template. This function will not trace past partial
21163 specializations, however. For example, given in addition:
21164
21165 template <class T> struct S<T*> { template <class U> void f(U); };
21166
21167 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21168 `template <class T> template <class U> S<T*>::f(U)'. */
21169
21170 tree
21171 most_general_template (tree decl)
21172 {
21173 if (TREE_CODE (decl) != TEMPLATE_DECL)
21174 {
21175 if (tree tinfo = get_template_info (decl))
21176 decl = TI_TEMPLATE (tinfo);
21177 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21178 template friend, or a FIELD_DECL for a capture pack. */
21179 if (TREE_CODE (decl) != TEMPLATE_DECL)
21180 return NULL_TREE;
21181 }
21182
21183 /* Look for more and more general templates. */
21184 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21185 {
21186 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21187 (See cp-tree.h for details.) */
21188 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21189 break;
21190
21191 if (CLASS_TYPE_P (TREE_TYPE (decl))
21192 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21193 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21194 break;
21195
21196 /* Stop if we run into an explicitly specialized class template. */
21197 if (!DECL_NAMESPACE_SCOPE_P (decl)
21198 && DECL_CONTEXT (decl)
21199 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21200 break;
21201
21202 decl = DECL_TI_TEMPLATE (decl);
21203 }
21204
21205 return decl;
21206 }
21207
21208 /* Return the most specialized of the template partial specializations
21209 which can produce TARGET, a specialization of some class or variable
21210 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21211 a TEMPLATE_DECL node corresponding to the partial specialization, while
21212 the TREE_PURPOSE is the set of template arguments that must be
21213 substituted into the template pattern in order to generate TARGET.
21214
21215 If the choice of partial specialization is ambiguous, a diagnostic
21216 is issued, and the error_mark_node is returned. If there are no
21217 partial specializations matching TARGET, then NULL_TREE is
21218 returned, indicating that the primary template should be used. */
21219
21220 static tree
21221 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21222 {
21223 tree list = NULL_TREE;
21224 tree t;
21225 tree champ;
21226 int fate;
21227 bool ambiguous_p;
21228 tree outer_args = NULL_TREE;
21229 tree tmpl, args;
21230
21231 if (TYPE_P (target))
21232 {
21233 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21234 tmpl = TI_TEMPLATE (tinfo);
21235 args = TI_ARGS (tinfo);
21236 }
21237 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21238 {
21239 tmpl = TREE_OPERAND (target, 0);
21240 args = TREE_OPERAND (target, 1);
21241 }
21242 else if (VAR_P (target))
21243 {
21244 tree tinfo = DECL_TEMPLATE_INFO (target);
21245 tmpl = TI_TEMPLATE (tinfo);
21246 args = TI_ARGS (tinfo);
21247 }
21248 else
21249 gcc_unreachable ();
21250
21251 tree main_tmpl = most_general_template (tmpl);
21252
21253 /* For determining which partial specialization to use, only the
21254 innermost args are interesting. */
21255 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21256 {
21257 outer_args = strip_innermost_template_args (args, 1);
21258 args = INNERMOST_TEMPLATE_ARGS (args);
21259 }
21260
21261 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21262 {
21263 tree spec_args;
21264 tree spec_tmpl = TREE_VALUE (t);
21265
21266 if (outer_args)
21267 {
21268 /* Substitute in the template args from the enclosing class. */
21269 ++processing_template_decl;
21270 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21271 --processing_template_decl;
21272 }
21273
21274 if (spec_tmpl == error_mark_node)
21275 return error_mark_node;
21276
21277 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21278 if (spec_args)
21279 {
21280 if (outer_args)
21281 spec_args = add_to_template_args (outer_args, spec_args);
21282
21283 /* Keep the candidate only if the constraints are satisfied,
21284 or if we're not compiling with concepts. */
21285 if (!flag_concepts
21286 || constraints_satisfied_p (spec_tmpl, spec_args))
21287 {
21288 list = tree_cons (spec_args, TREE_VALUE (t), list);
21289 TREE_TYPE (list) = TREE_TYPE (t);
21290 }
21291 }
21292 }
21293
21294 if (! list)
21295 return NULL_TREE;
21296
21297 ambiguous_p = false;
21298 t = list;
21299 champ = t;
21300 t = TREE_CHAIN (t);
21301 for (; t; t = TREE_CHAIN (t))
21302 {
21303 fate = more_specialized_partial_spec (tmpl, champ, t);
21304 if (fate == 1)
21305 ;
21306 else
21307 {
21308 if (fate == 0)
21309 {
21310 t = TREE_CHAIN (t);
21311 if (! t)
21312 {
21313 ambiguous_p = true;
21314 break;
21315 }
21316 }
21317 champ = t;
21318 }
21319 }
21320
21321 if (!ambiguous_p)
21322 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21323 {
21324 fate = more_specialized_partial_spec (tmpl, champ, t);
21325 if (fate != 1)
21326 {
21327 ambiguous_p = true;
21328 break;
21329 }
21330 }
21331
21332 if (ambiguous_p)
21333 {
21334 const char *str;
21335 char *spaces = NULL;
21336 if (!(complain & tf_error))
21337 return error_mark_node;
21338 if (TYPE_P (target))
21339 error ("ambiguous template instantiation for %q#T", target);
21340 else
21341 error ("ambiguous template instantiation for %q#D", target);
21342 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21343 for (t = list; t; t = TREE_CHAIN (t))
21344 {
21345 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21346 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21347 "%s %#S", spaces ? spaces : str, subst);
21348 spaces = spaces ? spaces : get_spaces (str);
21349 }
21350 free (spaces);
21351 return error_mark_node;
21352 }
21353
21354 return champ;
21355 }
21356
21357 /* Explicitly instantiate DECL. */
21358
21359 void
21360 do_decl_instantiation (tree decl, tree storage)
21361 {
21362 tree result = NULL_TREE;
21363 int extern_p = 0;
21364
21365 if (!decl || decl == error_mark_node)
21366 /* An error occurred, for which grokdeclarator has already issued
21367 an appropriate message. */
21368 return;
21369 else if (! DECL_LANG_SPECIFIC (decl))
21370 {
21371 error ("explicit instantiation of non-template %q#D", decl);
21372 return;
21373 }
21374
21375 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21376 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21377
21378 if (VAR_P (decl) && !var_templ)
21379 {
21380 /* There is an asymmetry here in the way VAR_DECLs and
21381 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21382 the latter, the DECL we get back will be marked as a
21383 template instantiation, and the appropriate
21384 DECL_TEMPLATE_INFO will be set up. This does not happen for
21385 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21386 should handle VAR_DECLs as it currently handles
21387 FUNCTION_DECLs. */
21388 if (!DECL_CLASS_SCOPE_P (decl))
21389 {
21390 error ("%qD is not a static data member of a class template", decl);
21391 return;
21392 }
21393 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21394 if (!result || !VAR_P (result))
21395 {
21396 error ("no matching template for %qD found", decl);
21397 return;
21398 }
21399 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21400 {
21401 error ("type %qT for explicit instantiation %qD does not match "
21402 "declared type %qT", TREE_TYPE (result), decl,
21403 TREE_TYPE (decl));
21404 return;
21405 }
21406 }
21407 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21408 {
21409 error ("explicit instantiation of %q#D", decl);
21410 return;
21411 }
21412 else
21413 result = decl;
21414
21415 /* Check for various error cases. Note that if the explicit
21416 instantiation is valid the RESULT will currently be marked as an
21417 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21418 until we get here. */
21419
21420 if (DECL_TEMPLATE_SPECIALIZATION (result))
21421 {
21422 /* DR 259 [temp.spec].
21423
21424 Both an explicit instantiation and a declaration of an explicit
21425 specialization shall not appear in a program unless the explicit
21426 instantiation follows a declaration of the explicit specialization.
21427
21428 For a given set of template parameters, if an explicit
21429 instantiation of a template appears after a declaration of an
21430 explicit specialization for that template, the explicit
21431 instantiation has no effect. */
21432 return;
21433 }
21434 else if (DECL_EXPLICIT_INSTANTIATION (result))
21435 {
21436 /* [temp.spec]
21437
21438 No program shall explicitly instantiate any template more
21439 than once.
21440
21441 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21442 the first instantiation was `extern' and the second is not,
21443 and EXTERN_P for the opposite case. */
21444 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21445 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21446 /* If an "extern" explicit instantiation follows an ordinary
21447 explicit instantiation, the template is instantiated. */
21448 if (extern_p)
21449 return;
21450 }
21451 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21452 {
21453 error ("no matching template for %qD found", result);
21454 return;
21455 }
21456 else if (!DECL_TEMPLATE_INFO (result))
21457 {
21458 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21459 return;
21460 }
21461
21462 if (storage == NULL_TREE)
21463 ;
21464 else if (storage == ridpointers[(int) RID_EXTERN])
21465 {
21466 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21467 pedwarn (input_location, OPT_Wpedantic,
21468 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21469 "instantiations");
21470 extern_p = 1;
21471 }
21472 else
21473 error ("storage class %qD applied to template instantiation", storage);
21474
21475 check_explicit_instantiation_namespace (result);
21476 mark_decl_instantiated (result, extern_p);
21477 if (! extern_p)
21478 instantiate_decl (result, /*defer_ok=*/1,
21479 /*expl_inst_class_mem_p=*/false);
21480 }
21481
21482 static void
21483 mark_class_instantiated (tree t, int extern_p)
21484 {
21485 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21486 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21487 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21488 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21489 if (! extern_p)
21490 {
21491 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21492 rest_of_type_compilation (t, 1);
21493 }
21494 }
21495
21496 /* Called from do_type_instantiation through binding_table_foreach to
21497 do recursive instantiation for the type bound in ENTRY. */
21498 static void
21499 bt_instantiate_type_proc (binding_entry entry, void *data)
21500 {
21501 tree storage = *(tree *) data;
21502
21503 if (MAYBE_CLASS_TYPE_P (entry->type)
21504 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
21505 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
21506 }
21507
21508 /* Called from do_type_instantiation to instantiate a member
21509 (a member function or a static member variable) of an
21510 explicitly instantiated class template. */
21511 static void
21512 instantiate_class_member (tree decl, int extern_p)
21513 {
21514 mark_decl_instantiated (decl, extern_p);
21515 if (! extern_p)
21516 instantiate_decl (decl, /*defer_ok=*/1,
21517 /*expl_inst_class_mem_p=*/true);
21518 }
21519
21520 /* Perform an explicit instantiation of template class T. STORAGE, if
21521 non-null, is the RID for extern, inline or static. COMPLAIN is
21522 nonzero if this is called from the parser, zero if called recursively,
21523 since the standard is unclear (as detailed below). */
21524
21525 void
21526 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
21527 {
21528 int extern_p = 0;
21529 int nomem_p = 0;
21530 int static_p = 0;
21531 int previous_instantiation_extern_p = 0;
21532
21533 if (TREE_CODE (t) == TYPE_DECL)
21534 t = TREE_TYPE (t);
21535
21536 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
21537 {
21538 tree tmpl =
21539 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
21540 if (tmpl)
21541 error ("explicit instantiation of non-class template %qD", tmpl);
21542 else
21543 error ("explicit instantiation of non-template type %qT", t);
21544 return;
21545 }
21546
21547 complete_type (t);
21548
21549 if (!COMPLETE_TYPE_P (t))
21550 {
21551 if (complain & tf_error)
21552 error ("explicit instantiation of %q#T before definition of template",
21553 t);
21554 return;
21555 }
21556
21557 if (storage != NULL_TREE)
21558 {
21559 if (!in_system_header_at (input_location))
21560 {
21561 if (storage == ridpointers[(int) RID_EXTERN])
21562 {
21563 if (cxx_dialect == cxx98)
21564 pedwarn (input_location, OPT_Wpedantic,
21565 "ISO C++ 1998 forbids the use of %<extern%> on "
21566 "explicit instantiations");
21567 }
21568 else
21569 pedwarn (input_location, OPT_Wpedantic,
21570 "ISO C++ forbids the use of %qE"
21571 " on explicit instantiations", storage);
21572 }
21573
21574 if (storage == ridpointers[(int) RID_INLINE])
21575 nomem_p = 1;
21576 else if (storage == ridpointers[(int) RID_EXTERN])
21577 extern_p = 1;
21578 else if (storage == ridpointers[(int) RID_STATIC])
21579 static_p = 1;
21580 else
21581 {
21582 error ("storage class %qD applied to template instantiation",
21583 storage);
21584 extern_p = 0;
21585 }
21586 }
21587
21588 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
21589 {
21590 /* DR 259 [temp.spec].
21591
21592 Both an explicit instantiation and a declaration of an explicit
21593 specialization shall not appear in a program unless the explicit
21594 instantiation follows a declaration of the explicit specialization.
21595
21596 For a given set of template parameters, if an explicit
21597 instantiation of a template appears after a declaration of an
21598 explicit specialization for that template, the explicit
21599 instantiation has no effect. */
21600 return;
21601 }
21602 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
21603 {
21604 /* [temp.spec]
21605
21606 No program shall explicitly instantiate any template more
21607 than once.
21608
21609 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
21610 instantiation was `extern'. If EXTERN_P then the second is.
21611 These cases are OK. */
21612 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
21613
21614 if (!previous_instantiation_extern_p && !extern_p
21615 && (complain & tf_error))
21616 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
21617
21618 /* If we've already instantiated the template, just return now. */
21619 if (!CLASSTYPE_INTERFACE_ONLY (t))
21620 return;
21621 }
21622
21623 check_explicit_instantiation_namespace (TYPE_NAME (t));
21624 mark_class_instantiated (t, extern_p);
21625
21626 if (nomem_p)
21627 return;
21628
21629 {
21630 tree tmp;
21631
21632 /* In contrast to implicit instantiation, where only the
21633 declarations, and not the definitions, of members are
21634 instantiated, we have here:
21635
21636 [temp.explicit]
21637
21638 The explicit instantiation of a class template specialization
21639 implies the instantiation of all of its members not
21640 previously explicitly specialized in the translation unit
21641 containing the explicit instantiation.
21642
21643 Of course, we can't instantiate member template classes, since
21644 we don't have any arguments for them. Note that the standard
21645 is unclear on whether the instantiation of the members are
21646 *explicit* instantiations or not. However, the most natural
21647 interpretation is that it should be an explicit instantiation. */
21648
21649 if (! static_p)
21650 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
21651 if (TREE_CODE (tmp) == FUNCTION_DECL
21652 && DECL_TEMPLATE_INSTANTIATION (tmp)
21653 && user_provided_p (tmp))
21654 instantiate_class_member (tmp, extern_p);
21655
21656 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
21657 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
21658 instantiate_class_member (tmp, extern_p);
21659
21660 if (CLASSTYPE_NESTED_UTDS (t))
21661 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
21662 bt_instantiate_type_proc, &storage);
21663 }
21664 }
21665
21666 /* Given a function DECL, which is a specialization of TMPL, modify
21667 DECL to be a re-instantiation of TMPL with the same template
21668 arguments. TMPL should be the template into which tsubst'ing
21669 should occur for DECL, not the most general template.
21670
21671 One reason for doing this is a scenario like this:
21672
21673 template <class T>
21674 void f(const T&, int i);
21675
21676 void g() { f(3, 7); }
21677
21678 template <class T>
21679 void f(const T& t, const int i) { }
21680
21681 Note that when the template is first instantiated, with
21682 instantiate_template, the resulting DECL will have no name for the
21683 first parameter, and the wrong type for the second. So, when we go
21684 to instantiate the DECL, we regenerate it. */
21685
21686 static void
21687 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
21688 {
21689 /* The arguments used to instantiate DECL, from the most general
21690 template. */
21691 tree code_pattern;
21692
21693 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
21694
21695 /* Make sure that we can see identifiers, and compute access
21696 correctly. */
21697 push_access_scope (decl);
21698
21699 if (TREE_CODE (decl) == FUNCTION_DECL)
21700 {
21701 tree decl_parm;
21702 tree pattern_parm;
21703 tree specs;
21704 int args_depth;
21705 int parms_depth;
21706
21707 args_depth = TMPL_ARGS_DEPTH (args);
21708 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
21709 if (args_depth > parms_depth)
21710 args = get_innermost_template_args (args, parms_depth);
21711
21712 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
21713 args, tf_error, NULL_TREE,
21714 /*defer_ok*/false);
21715 if (specs && specs != error_mark_node)
21716 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
21717 specs);
21718
21719 /* Merge parameter declarations. */
21720 decl_parm = skip_artificial_parms_for (decl,
21721 DECL_ARGUMENTS (decl));
21722 pattern_parm
21723 = skip_artificial_parms_for (code_pattern,
21724 DECL_ARGUMENTS (code_pattern));
21725 while (decl_parm && !DECL_PACK_P (pattern_parm))
21726 {
21727 tree parm_type;
21728 tree attributes;
21729
21730 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21731 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
21732 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
21733 NULL_TREE);
21734 parm_type = type_decays_to (parm_type);
21735 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21736 TREE_TYPE (decl_parm) = parm_type;
21737 attributes = DECL_ATTRIBUTES (pattern_parm);
21738 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21739 {
21740 DECL_ATTRIBUTES (decl_parm) = attributes;
21741 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21742 }
21743 decl_parm = DECL_CHAIN (decl_parm);
21744 pattern_parm = DECL_CHAIN (pattern_parm);
21745 }
21746 /* Merge any parameters that match with the function parameter
21747 pack. */
21748 if (pattern_parm && DECL_PACK_P (pattern_parm))
21749 {
21750 int i, len;
21751 tree expanded_types;
21752 /* Expand the TYPE_PACK_EXPANSION that provides the types for
21753 the parameters in this function parameter pack. */
21754 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
21755 args, tf_error, NULL_TREE);
21756 len = TREE_VEC_LENGTH (expanded_types);
21757 for (i = 0; i < len; i++)
21758 {
21759 tree parm_type;
21760 tree attributes;
21761
21762 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
21763 /* Rename the parameter to include the index. */
21764 DECL_NAME (decl_parm) =
21765 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
21766 parm_type = TREE_VEC_ELT (expanded_types, i);
21767 parm_type = type_decays_to (parm_type);
21768 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
21769 TREE_TYPE (decl_parm) = parm_type;
21770 attributes = DECL_ATTRIBUTES (pattern_parm);
21771 if (DECL_ATTRIBUTES (decl_parm) != attributes)
21772 {
21773 DECL_ATTRIBUTES (decl_parm) = attributes;
21774 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
21775 }
21776 decl_parm = DECL_CHAIN (decl_parm);
21777 }
21778 }
21779 /* Merge additional specifiers from the CODE_PATTERN. */
21780 if (DECL_DECLARED_INLINE_P (code_pattern)
21781 && !DECL_DECLARED_INLINE_P (decl))
21782 DECL_DECLARED_INLINE_P (decl) = 1;
21783 }
21784 else if (VAR_P (decl))
21785 {
21786 DECL_INITIAL (decl) =
21787 tsubst_expr (DECL_INITIAL (code_pattern), args,
21788 tf_error, DECL_TI_TEMPLATE (decl),
21789 /*integral_constant_expression_p=*/false);
21790 if (VAR_HAD_UNKNOWN_BOUND (decl))
21791 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
21792 tf_error, DECL_TI_TEMPLATE (decl));
21793 }
21794 else
21795 gcc_unreachable ();
21796
21797 pop_access_scope (decl);
21798 }
21799
21800 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
21801 substituted to get DECL. */
21802
21803 tree
21804 template_for_substitution (tree decl)
21805 {
21806 tree tmpl = DECL_TI_TEMPLATE (decl);
21807
21808 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
21809 for the instantiation. This is not always the most general
21810 template. Consider, for example:
21811
21812 template <class T>
21813 struct S { template <class U> void f();
21814 template <> void f<int>(); };
21815
21816 and an instantiation of S<double>::f<int>. We want TD to be the
21817 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
21818 while (/* An instantiation cannot have a definition, so we need a
21819 more general template. */
21820 DECL_TEMPLATE_INSTANTIATION (tmpl)
21821 /* We must also deal with friend templates. Given:
21822
21823 template <class T> struct S {
21824 template <class U> friend void f() {};
21825 };
21826
21827 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
21828 so far as the language is concerned, but that's still
21829 where we get the pattern for the instantiation from. On
21830 other hand, if the definition comes outside the class, say:
21831
21832 template <class T> struct S {
21833 template <class U> friend void f();
21834 };
21835 template <class U> friend void f() {}
21836
21837 we don't need to look any further. That's what the check for
21838 DECL_INITIAL is for. */
21839 || (TREE_CODE (decl) == FUNCTION_DECL
21840 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
21841 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
21842 {
21843 /* The present template, TD, should not be a definition. If it
21844 were a definition, we should be using it! Note that we
21845 cannot restructure the loop to just keep going until we find
21846 a template with a definition, since that might go too far if
21847 a specialization was declared, but not defined. */
21848
21849 /* Fetch the more general template. */
21850 tmpl = DECL_TI_TEMPLATE (tmpl);
21851 }
21852
21853 return tmpl;
21854 }
21855
21856 /* Returns true if we need to instantiate this template instance even if we
21857 know we aren't going to emit it. */
21858
21859 bool
21860 always_instantiate_p (tree decl)
21861 {
21862 /* We always instantiate inline functions so that we can inline them. An
21863 explicit instantiation declaration prohibits implicit instantiation of
21864 non-inline functions. With high levels of optimization, we would
21865 normally inline non-inline functions -- but we're not allowed to do
21866 that for "extern template" functions. Therefore, we check
21867 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
21868 return ((TREE_CODE (decl) == FUNCTION_DECL
21869 && (DECL_DECLARED_INLINE_P (decl)
21870 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
21871 /* And we need to instantiate static data members so that
21872 their initializers are available in integral constant
21873 expressions. */
21874 || (VAR_P (decl)
21875 && decl_maybe_constant_var_p (decl)));
21876 }
21877
21878 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
21879 instantiate it now, modifying TREE_TYPE (fn). */
21880
21881 void
21882 maybe_instantiate_noexcept (tree fn)
21883 {
21884 tree fntype, spec, noex, clone;
21885
21886 /* Don't instantiate a noexcept-specification from template context. */
21887 if (processing_template_decl)
21888 return;
21889
21890 if (DECL_CLONED_FUNCTION_P (fn))
21891 fn = DECL_CLONED_FUNCTION (fn);
21892 fntype = TREE_TYPE (fn);
21893 spec = TYPE_RAISES_EXCEPTIONS (fntype);
21894
21895 if (!spec || !TREE_PURPOSE (spec))
21896 return;
21897
21898 noex = TREE_PURPOSE (spec);
21899
21900 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
21901 {
21902 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
21903 spec = get_defaulted_eh_spec (fn);
21904 else if (push_tinst_level (fn))
21905 {
21906 push_access_scope (fn);
21907 push_deferring_access_checks (dk_no_deferred);
21908 input_location = DECL_SOURCE_LOCATION (fn);
21909 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
21910 DEFERRED_NOEXCEPT_ARGS (noex),
21911 tf_warning_or_error, fn,
21912 /*function_p=*/false,
21913 /*integral_constant_expression_p=*/true);
21914 pop_deferring_access_checks ();
21915 pop_access_scope (fn);
21916 pop_tinst_level ();
21917 spec = build_noexcept_spec (noex, tf_warning_or_error);
21918 if (spec == error_mark_node)
21919 spec = noexcept_false_spec;
21920 }
21921 else
21922 spec = noexcept_false_spec;
21923
21924 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
21925 }
21926
21927 FOR_EACH_CLONE (clone, fn)
21928 {
21929 if (TREE_TYPE (clone) == fntype)
21930 TREE_TYPE (clone) = TREE_TYPE (fn);
21931 else
21932 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
21933 }
21934 }
21935
21936 /* Produce the definition of D, a _DECL generated from a template. If
21937 DEFER_OK is nonzero, then we don't have to actually do the
21938 instantiation now; we just have to do it sometime. Normally it is
21939 an error if this is an explicit instantiation but D is undefined.
21940 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
21941 explicitly instantiated class template. */
21942
21943 tree
21944 instantiate_decl (tree d, int defer_ok,
21945 bool expl_inst_class_mem_p)
21946 {
21947 tree tmpl = DECL_TI_TEMPLATE (d);
21948 tree gen_args;
21949 tree args;
21950 tree td;
21951 tree code_pattern;
21952 tree spec;
21953 tree gen_tmpl;
21954 bool pattern_defined;
21955 location_t saved_loc = input_location;
21956 int saved_unevaluated_operand = cp_unevaluated_operand;
21957 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21958 bool external_p;
21959 bool deleted_p;
21960 tree fn_context;
21961 bool nested = false;
21962
21963 /* This function should only be used to instantiate templates for
21964 functions and static member variables. */
21965 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
21966
21967 /* A concept is never instantiated. */
21968 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
21969
21970 /* Variables are never deferred; if instantiation is required, they
21971 are instantiated right away. That allows for better code in the
21972 case that an expression refers to the value of the variable --
21973 if the variable has a constant value the referring expression can
21974 take advantage of that fact. */
21975 if (VAR_P (d)
21976 || DECL_DECLARED_CONSTEXPR_P (d))
21977 defer_ok = 0;
21978
21979 /* Don't instantiate cloned functions. Instead, instantiate the
21980 functions they cloned. */
21981 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
21982 d = DECL_CLONED_FUNCTION (d);
21983
21984 if (DECL_TEMPLATE_INSTANTIATED (d)
21985 || (TREE_CODE (d) == FUNCTION_DECL
21986 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
21987 || DECL_TEMPLATE_SPECIALIZATION (d))
21988 /* D has already been instantiated or explicitly specialized, so
21989 there's nothing for us to do here.
21990
21991 It might seem reasonable to check whether or not D is an explicit
21992 instantiation, and, if so, stop here. But when an explicit
21993 instantiation is deferred until the end of the compilation,
21994 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
21995 the instantiation. */
21996 return d;
21997
21998 /* Check to see whether we know that this template will be
21999 instantiated in some other file, as with "extern template"
22000 extension. */
22001 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22002
22003 /* In general, we do not instantiate such templates. */
22004 if (external_p && !always_instantiate_p (d))
22005 return d;
22006
22007 gen_tmpl = most_general_template (tmpl);
22008 gen_args = DECL_TI_ARGS (d);
22009
22010 if (tmpl != gen_tmpl)
22011 /* We should already have the extra args. */
22012 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22013 == TMPL_ARGS_DEPTH (gen_args));
22014 /* And what's in the hash table should match D. */
22015 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22016 || spec == NULL_TREE);
22017
22018 /* This needs to happen before any tsubsting. */
22019 if (! push_tinst_level (d))
22020 return d;
22021
22022 timevar_push (TV_TEMPLATE_INST);
22023
22024 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22025 for the instantiation. */
22026 td = template_for_substitution (d);
22027 args = gen_args;
22028
22029 if (VAR_P (d))
22030 {
22031 /* Look up an explicit specialization, if any. */
22032 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22033 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22034 if (elt && elt != error_mark_node)
22035 {
22036 td = TREE_VALUE (elt);
22037 args = TREE_PURPOSE (elt);
22038 }
22039 }
22040
22041 code_pattern = DECL_TEMPLATE_RESULT (td);
22042
22043 /* We should never be trying to instantiate a member of a class
22044 template or partial specialization. */
22045 gcc_assert (d != code_pattern);
22046
22047 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22048 || DECL_TEMPLATE_SPECIALIZATION (td))
22049 /* In the case of a friend template whose definition is provided
22050 outside the class, we may have too many arguments. Drop the
22051 ones we don't need. The same is true for specializations. */
22052 args = get_innermost_template_args
22053 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22054
22055 if (TREE_CODE (d) == FUNCTION_DECL)
22056 {
22057 deleted_p = DECL_DELETED_FN (code_pattern);
22058 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22059 && DECL_INITIAL (code_pattern) != error_mark_node)
22060 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22061 || deleted_p);
22062 }
22063 else
22064 {
22065 deleted_p = false;
22066 if (DECL_CLASS_SCOPE_P (code_pattern))
22067 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22068 || DECL_INLINE_VAR_P (code_pattern));
22069 else
22070 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22071 }
22072
22073 /* We may be in the middle of deferred access check. Disable it now. */
22074 push_deferring_access_checks (dk_no_deferred);
22075
22076 /* Unless an explicit instantiation directive has already determined
22077 the linkage of D, remember that a definition is available for
22078 this entity. */
22079 if (pattern_defined
22080 && !DECL_INTERFACE_KNOWN (d)
22081 && !DECL_NOT_REALLY_EXTERN (d))
22082 mark_definable (d);
22083
22084 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22085 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22086 input_location = DECL_SOURCE_LOCATION (d);
22087
22088 /* If D is a member of an explicitly instantiated class template,
22089 and no definition is available, treat it like an implicit
22090 instantiation. */
22091 if (!pattern_defined && expl_inst_class_mem_p
22092 && DECL_EXPLICIT_INSTANTIATION (d))
22093 {
22094 /* Leave linkage flags alone on instantiations with anonymous
22095 visibility. */
22096 if (TREE_PUBLIC (d))
22097 {
22098 DECL_NOT_REALLY_EXTERN (d) = 0;
22099 DECL_INTERFACE_KNOWN (d) = 0;
22100 }
22101 SET_DECL_IMPLICIT_INSTANTIATION (d);
22102 }
22103
22104 /* Defer all other templates, unless we have been explicitly
22105 forbidden from doing so. */
22106 if (/* If there is no definition, we cannot instantiate the
22107 template. */
22108 ! pattern_defined
22109 /* If it's OK to postpone instantiation, do so. */
22110 || defer_ok
22111 /* If this is a static data member that will be defined
22112 elsewhere, we don't want to instantiate the entire data
22113 member, but we do want to instantiate the initializer so that
22114 we can substitute that elsewhere. */
22115 || (external_p && VAR_P (d))
22116 /* Handle here a deleted function too, avoid generating
22117 its body (c++/61080). */
22118 || deleted_p)
22119 {
22120 /* The definition of the static data member is now required so
22121 we must substitute the initializer. */
22122 if (VAR_P (d)
22123 && !DECL_INITIAL (d)
22124 && DECL_INITIAL (code_pattern))
22125 {
22126 tree ns;
22127 tree init;
22128 bool const_init = false;
22129 bool enter_context = DECL_CLASS_SCOPE_P (d);
22130
22131 ns = decl_namespace_context (d);
22132 push_nested_namespace (ns);
22133 if (enter_context)
22134 push_nested_class (DECL_CONTEXT (d));
22135 init = tsubst_expr (DECL_INITIAL (code_pattern),
22136 args,
22137 tf_warning_or_error, NULL_TREE,
22138 /*integral_constant_expression_p=*/false);
22139 /* If instantiating the initializer involved instantiating this
22140 again, don't call cp_finish_decl twice. */
22141 if (!DECL_INITIAL (d))
22142 {
22143 /* Make sure the initializer is still constant, in case of
22144 circular dependency (template/instantiate6.C). */
22145 const_init
22146 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22147 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22148 /*asmspec_tree=*/NULL_TREE,
22149 LOOKUP_ONLYCONVERTING);
22150 }
22151 if (enter_context)
22152 pop_nested_class ();
22153 pop_nested_namespace (ns);
22154 }
22155
22156 /* We restore the source position here because it's used by
22157 add_pending_template. */
22158 input_location = saved_loc;
22159
22160 if (at_eof && !pattern_defined
22161 && DECL_EXPLICIT_INSTANTIATION (d)
22162 && DECL_NOT_REALLY_EXTERN (d))
22163 /* [temp.explicit]
22164
22165 The definition of a non-exported function template, a
22166 non-exported member function template, or a non-exported
22167 member function or static data member of a class template
22168 shall be present in every translation unit in which it is
22169 explicitly instantiated. */
22170 permerror (input_location, "explicit instantiation of %qD "
22171 "but no definition available", d);
22172
22173 /* If we're in unevaluated context, we just wanted to get the
22174 constant value; this isn't an odr use, so don't queue
22175 a full instantiation. */
22176 if (cp_unevaluated_operand != 0)
22177 goto out;
22178 /* ??? Historically, we have instantiated inline functions, even
22179 when marked as "extern template". */
22180 if (!(external_p && VAR_P (d)))
22181 add_pending_template (d);
22182 goto out;
22183 }
22184 /* Tell the repository that D is available in this translation unit
22185 -- and see if it is supposed to be instantiated here. */
22186 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22187 {
22188 /* In a PCH file, despite the fact that the repository hasn't
22189 requested instantiation in the PCH it is still possible that
22190 an instantiation will be required in a file that includes the
22191 PCH. */
22192 if (pch_file)
22193 add_pending_template (d);
22194 /* Instantiate inline functions so that the inliner can do its
22195 job, even though we'll not be emitting a copy of this
22196 function. */
22197 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22198 goto out;
22199 }
22200
22201 fn_context = decl_function_context (d);
22202 nested = (current_function_decl != NULL_TREE);
22203 vec<tree> omp_privatization_save;
22204 if (nested)
22205 save_omp_privatization_clauses (omp_privatization_save);
22206
22207 if (!fn_context)
22208 push_to_top_level ();
22209 else
22210 {
22211 if (nested)
22212 push_function_context ();
22213 cp_unevaluated_operand = 0;
22214 c_inhibit_evaluation_warnings = 0;
22215 }
22216
22217 /* Mark D as instantiated so that recursive calls to
22218 instantiate_decl do not try to instantiate it again. */
22219 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22220
22221 /* Regenerate the declaration in case the template has been modified
22222 by a subsequent redeclaration. */
22223 regenerate_decl_from_template (d, td, args);
22224
22225 /* We already set the file and line above. Reset them now in case
22226 they changed as a result of calling regenerate_decl_from_template. */
22227 input_location = DECL_SOURCE_LOCATION (d);
22228
22229 if (VAR_P (d))
22230 {
22231 tree init;
22232 bool const_init = false;
22233
22234 /* Clear out DECL_RTL; whatever was there before may not be right
22235 since we've reset the type of the declaration. */
22236 SET_DECL_RTL (d, NULL);
22237 DECL_IN_AGGR_P (d) = 0;
22238
22239 /* The initializer is placed in DECL_INITIAL by
22240 regenerate_decl_from_template so we don't need to
22241 push/pop_access_scope again here. Pull it out so that
22242 cp_finish_decl can process it. */
22243 init = DECL_INITIAL (d);
22244 DECL_INITIAL (d) = NULL_TREE;
22245 DECL_INITIALIZED_P (d) = 0;
22246
22247 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22248 initializer. That function will defer actual emission until
22249 we have a chance to determine linkage. */
22250 DECL_EXTERNAL (d) = 0;
22251
22252 /* Enter the scope of D so that access-checking works correctly. */
22253 bool enter_context = DECL_CLASS_SCOPE_P (d);
22254 if (enter_context)
22255 push_nested_class (DECL_CONTEXT (d));
22256
22257 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22258 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22259
22260 if (enter_context)
22261 pop_nested_class ();
22262
22263 if (variable_template_p (gen_tmpl))
22264 note_variable_template_instantiation (d);
22265 }
22266 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22267 synthesize_method (d);
22268 else if (TREE_CODE (d) == FUNCTION_DECL)
22269 {
22270 hash_map<tree, tree> *saved_local_specializations;
22271 tree tmpl_parm;
22272 tree spec_parm;
22273 tree block = NULL_TREE;
22274
22275 /* Save away the current list, in case we are instantiating one
22276 template from within the body of another. */
22277 saved_local_specializations = local_specializations;
22278
22279 /* Set up the list of local specializations. */
22280 local_specializations = new hash_map<tree, tree>;
22281
22282 /* Set up context. */
22283 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22284 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22285 block = push_stmt_list ();
22286 else
22287 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22288
22289 /* Some typedefs referenced from within the template code need to be
22290 access checked at template instantiation time, i.e now. These
22291 types were added to the template at parsing time. Let's get those
22292 and perform the access checks then. */
22293 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22294 args);
22295
22296 /* Create substitution entries for the parameters. */
22297 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22298 spec_parm = DECL_ARGUMENTS (d);
22299 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22300 {
22301 register_local_specialization (spec_parm, tmpl_parm);
22302 spec_parm = skip_artificial_parms_for (d, spec_parm);
22303 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22304 }
22305 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22306 {
22307 if (!DECL_PACK_P (tmpl_parm))
22308 {
22309 register_local_specialization (spec_parm, tmpl_parm);
22310 spec_parm = DECL_CHAIN (spec_parm);
22311 }
22312 else
22313 {
22314 /* Register the (value) argument pack as a specialization of
22315 TMPL_PARM, then move on. */
22316 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22317 register_local_specialization (argpack, tmpl_parm);
22318 }
22319 }
22320 gcc_assert (!spec_parm);
22321
22322 /* Substitute into the body of the function. */
22323 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22324 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22325 tf_warning_or_error, tmpl);
22326 else
22327 {
22328 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22329 tf_warning_or_error, tmpl,
22330 /*integral_constant_expression_p=*/false);
22331
22332 /* Set the current input_location to the end of the function
22333 so that finish_function knows where we are. */
22334 input_location
22335 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22336
22337 /* Remember if we saw an infinite loop in the template. */
22338 current_function_infinite_loop
22339 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22340 }
22341
22342 /* We don't need the local specializations any more. */
22343 delete local_specializations;
22344 local_specializations = saved_local_specializations;
22345
22346 /* Finish the function. */
22347 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22348 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22349 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22350 else
22351 {
22352 d = finish_function (0);
22353 expand_or_defer_fn (d);
22354 }
22355
22356 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22357 cp_check_omp_declare_reduction (d);
22358 }
22359
22360 /* We're not deferring instantiation any more. */
22361 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22362
22363 if (!fn_context)
22364 pop_from_top_level ();
22365 else if (nested)
22366 pop_function_context ();
22367
22368 out:
22369 input_location = saved_loc;
22370 cp_unevaluated_operand = saved_unevaluated_operand;
22371 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22372 pop_deferring_access_checks ();
22373 pop_tinst_level ();
22374 if (nested)
22375 restore_omp_privatization_clauses (omp_privatization_save);
22376
22377 timevar_pop (TV_TEMPLATE_INST);
22378
22379 return d;
22380 }
22381
22382 /* Run through the list of templates that we wish we could
22383 instantiate, and instantiate any we can. RETRIES is the
22384 number of times we retry pending template instantiation. */
22385
22386 void
22387 instantiate_pending_templates (int retries)
22388 {
22389 int reconsider;
22390 location_t saved_loc = input_location;
22391
22392 /* Instantiating templates may trigger vtable generation. This in turn
22393 may require further template instantiations. We place a limit here
22394 to avoid infinite loop. */
22395 if (pending_templates && retries >= max_tinst_depth)
22396 {
22397 tree decl = pending_templates->tinst->decl;
22398
22399 fatal_error (input_location,
22400 "template instantiation depth exceeds maximum of %d"
22401 " instantiating %q+D, possibly from virtual table generation"
22402 " (use -ftemplate-depth= to increase the maximum)",
22403 max_tinst_depth, decl);
22404 if (TREE_CODE (decl) == FUNCTION_DECL)
22405 /* Pretend that we defined it. */
22406 DECL_INITIAL (decl) = error_mark_node;
22407 return;
22408 }
22409
22410 do
22411 {
22412 struct pending_template **t = &pending_templates;
22413 struct pending_template *last = NULL;
22414 reconsider = 0;
22415 while (*t)
22416 {
22417 tree instantiation = reopen_tinst_level ((*t)->tinst);
22418 bool complete = false;
22419
22420 if (TYPE_P (instantiation))
22421 {
22422 tree fn;
22423
22424 if (!COMPLETE_TYPE_P (instantiation))
22425 {
22426 instantiate_class_template (instantiation);
22427 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22428 for (fn = TYPE_METHODS (instantiation);
22429 fn;
22430 fn = TREE_CHAIN (fn))
22431 if (! DECL_ARTIFICIAL (fn))
22432 instantiate_decl (fn,
22433 /*defer_ok=*/0,
22434 /*expl_inst_class_mem_p=*/false);
22435 if (COMPLETE_TYPE_P (instantiation))
22436 reconsider = 1;
22437 }
22438
22439 complete = COMPLETE_TYPE_P (instantiation);
22440 }
22441 else
22442 {
22443 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22444 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22445 {
22446 instantiation
22447 = instantiate_decl (instantiation,
22448 /*defer_ok=*/0,
22449 /*expl_inst_class_mem_p=*/false);
22450 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22451 reconsider = 1;
22452 }
22453
22454 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22455 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22456 }
22457
22458 if (complete)
22459 /* If INSTANTIATION has been instantiated, then we don't
22460 need to consider it again in the future. */
22461 *t = (*t)->next;
22462 else
22463 {
22464 last = *t;
22465 t = &(*t)->next;
22466 }
22467 tinst_depth = 0;
22468 current_tinst_level = NULL;
22469 }
22470 last_pending_template = last;
22471 }
22472 while (reconsider);
22473
22474 input_location = saved_loc;
22475 }
22476
22477 /* Substitute ARGVEC into T, which is a list of initializers for
22478 either base class or a non-static data member. The TREE_PURPOSEs
22479 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22480 instantiate_decl. */
22481
22482 static tree
22483 tsubst_initializer_list (tree t, tree argvec)
22484 {
22485 tree inits = NULL_TREE;
22486
22487 for (; t; t = TREE_CHAIN (t))
22488 {
22489 tree decl;
22490 tree init;
22491 tree expanded_bases = NULL_TREE;
22492 tree expanded_arguments = NULL_TREE;
22493 int i, len = 1;
22494
22495 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
22496 {
22497 tree expr;
22498 tree arg;
22499
22500 /* Expand the base class expansion type into separate base
22501 classes. */
22502 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
22503 tf_warning_or_error,
22504 NULL_TREE);
22505 if (expanded_bases == error_mark_node)
22506 continue;
22507
22508 /* We'll be building separate TREE_LISTs of arguments for
22509 each base. */
22510 len = TREE_VEC_LENGTH (expanded_bases);
22511 expanded_arguments = make_tree_vec (len);
22512 for (i = 0; i < len; i++)
22513 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
22514
22515 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
22516 expand each argument in the TREE_VALUE of t. */
22517 expr = make_node (EXPR_PACK_EXPANSION);
22518 PACK_EXPANSION_LOCAL_P (expr) = true;
22519 PACK_EXPANSION_PARAMETER_PACKS (expr) =
22520 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
22521
22522 if (TREE_VALUE (t) == void_type_node)
22523 /* VOID_TYPE_NODE is used to indicate
22524 value-initialization. */
22525 {
22526 for (i = 0; i < len; i++)
22527 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
22528 }
22529 else
22530 {
22531 /* Substitute parameter packs into each argument in the
22532 TREE_LIST. */
22533 in_base_initializer = 1;
22534 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
22535 {
22536 tree expanded_exprs;
22537
22538 /* Expand the argument. */
22539 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
22540 expanded_exprs
22541 = tsubst_pack_expansion (expr, argvec,
22542 tf_warning_or_error,
22543 NULL_TREE);
22544 if (expanded_exprs == error_mark_node)
22545 continue;
22546
22547 /* Prepend each of the expanded expressions to the
22548 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
22549 for (i = 0; i < len; i++)
22550 {
22551 TREE_VEC_ELT (expanded_arguments, i) =
22552 tree_cons (NULL_TREE,
22553 TREE_VEC_ELT (expanded_exprs, i),
22554 TREE_VEC_ELT (expanded_arguments, i));
22555 }
22556 }
22557 in_base_initializer = 0;
22558
22559 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
22560 since we built them backwards. */
22561 for (i = 0; i < len; i++)
22562 {
22563 TREE_VEC_ELT (expanded_arguments, i) =
22564 nreverse (TREE_VEC_ELT (expanded_arguments, i));
22565 }
22566 }
22567 }
22568
22569 for (i = 0; i < len; ++i)
22570 {
22571 if (expanded_bases)
22572 {
22573 decl = TREE_VEC_ELT (expanded_bases, i);
22574 decl = expand_member_init (decl);
22575 init = TREE_VEC_ELT (expanded_arguments, i);
22576 }
22577 else
22578 {
22579 tree tmp;
22580 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
22581 tf_warning_or_error, NULL_TREE);
22582
22583 decl = expand_member_init (decl);
22584 if (decl && !DECL_P (decl))
22585 in_base_initializer = 1;
22586
22587 init = TREE_VALUE (t);
22588 tmp = init;
22589 if (init != void_type_node)
22590 init = tsubst_expr (init, argvec,
22591 tf_warning_or_error, NULL_TREE,
22592 /*integral_constant_expression_p=*/false);
22593 if (init == NULL_TREE && tmp != NULL_TREE)
22594 /* If we had an initializer but it instantiated to nothing,
22595 value-initialize the object. This will only occur when
22596 the initializer was a pack expansion where the parameter
22597 packs used in that expansion were of length zero. */
22598 init = void_type_node;
22599 in_base_initializer = 0;
22600 }
22601
22602 if (decl)
22603 {
22604 init = build_tree_list (decl, init);
22605 TREE_CHAIN (init) = inits;
22606 inits = init;
22607 }
22608 }
22609 }
22610 return inits;
22611 }
22612
22613 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
22614
22615 static void
22616 set_current_access_from_decl (tree decl)
22617 {
22618 if (TREE_PRIVATE (decl))
22619 current_access_specifier = access_private_node;
22620 else if (TREE_PROTECTED (decl))
22621 current_access_specifier = access_protected_node;
22622 else
22623 current_access_specifier = access_public_node;
22624 }
22625
22626 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
22627 is the instantiation (which should have been created with
22628 start_enum) and ARGS are the template arguments to use. */
22629
22630 static void
22631 tsubst_enum (tree tag, tree newtag, tree args)
22632 {
22633 tree e;
22634
22635 if (SCOPED_ENUM_P (newtag))
22636 begin_scope (sk_scoped_enum, newtag);
22637
22638 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
22639 {
22640 tree value;
22641 tree decl;
22642
22643 decl = TREE_VALUE (e);
22644 /* Note that in a template enum, the TREE_VALUE is the
22645 CONST_DECL, not the corresponding INTEGER_CST. */
22646 value = tsubst_expr (DECL_INITIAL (decl),
22647 args, tf_warning_or_error, NULL_TREE,
22648 /*integral_constant_expression_p=*/true);
22649
22650 /* Give this enumeration constant the correct access. */
22651 set_current_access_from_decl (decl);
22652
22653 /* Actually build the enumerator itself. Here we're assuming that
22654 enumerators can't have dependent attributes. */
22655 build_enumerator (DECL_NAME (decl), value, newtag,
22656 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
22657 }
22658
22659 if (SCOPED_ENUM_P (newtag))
22660 finish_scope ();
22661
22662 finish_enum_value_list (newtag);
22663 finish_enum (newtag);
22664
22665 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
22666 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
22667 }
22668
22669 /* DECL is a FUNCTION_DECL that is a template specialization. Return
22670 its type -- but without substituting the innermost set of template
22671 arguments. So, innermost set of template parameters will appear in
22672 the type. */
22673
22674 tree
22675 get_mostly_instantiated_function_type (tree decl)
22676 {
22677 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
22678 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
22679 }
22680
22681 /* Return truthvalue if we're processing a template different from
22682 the last one involved in diagnostics. */
22683 bool
22684 problematic_instantiation_changed (void)
22685 {
22686 return current_tinst_level != last_error_tinst_level;
22687 }
22688
22689 /* Remember current template involved in diagnostics. */
22690 void
22691 record_last_problematic_instantiation (void)
22692 {
22693 last_error_tinst_level = current_tinst_level;
22694 }
22695
22696 struct tinst_level *
22697 current_instantiation (void)
22698 {
22699 return current_tinst_level;
22700 }
22701
22702 /* Return TRUE if current_function_decl is being instantiated, false
22703 otherwise. */
22704
22705 bool
22706 instantiating_current_function_p (void)
22707 {
22708 return (current_instantiation ()
22709 && current_instantiation ()->decl == current_function_decl);
22710 }
22711
22712 /* [temp.param] Check that template non-type parm TYPE is of an allowable
22713 type. Return zero for ok, nonzero for disallowed. Issue error and
22714 warning messages under control of COMPLAIN. */
22715
22716 static int
22717 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
22718 {
22719 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
22720 return 0;
22721 else if (POINTER_TYPE_P (type))
22722 return 0;
22723 else if (TYPE_PTRMEM_P (type))
22724 return 0;
22725 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
22726 return 0;
22727 else if (TREE_CODE (type) == TYPENAME_TYPE)
22728 return 0;
22729 else if (TREE_CODE (type) == DECLTYPE_TYPE)
22730 return 0;
22731 else if (TREE_CODE (type) == NULLPTR_TYPE)
22732 return 0;
22733 /* A bound template template parm could later be instantiated to have a valid
22734 nontype parm type via an alias template. */
22735 else if (cxx_dialect >= cxx11
22736 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22737 return 0;
22738
22739 if (complain & tf_error)
22740 {
22741 if (type == error_mark_node)
22742 inform (input_location, "invalid template non-type parameter");
22743 else
22744 error ("%q#T is not a valid type for a template non-type parameter",
22745 type);
22746 }
22747 return 1;
22748 }
22749
22750 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
22751 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
22752
22753 static bool
22754 dependent_type_p_r (tree type)
22755 {
22756 tree scope;
22757
22758 /* [temp.dep.type]
22759
22760 A type is dependent if it is:
22761
22762 -- a template parameter. Template template parameters are types
22763 for us (since TYPE_P holds true for them) so we handle
22764 them here. */
22765 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22766 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
22767 return true;
22768 /* -- a qualified-id with a nested-name-specifier which contains a
22769 class-name that names a dependent type or whose unqualified-id
22770 names a dependent type. */
22771 if (TREE_CODE (type) == TYPENAME_TYPE)
22772 return true;
22773
22774 /* An alias template specialization can be dependent even if the
22775 resulting type is not. */
22776 if (dependent_alias_template_spec_p (type))
22777 return true;
22778
22779 /* -- a cv-qualified type where the cv-unqualified type is
22780 dependent.
22781 No code is necessary for this bullet; the code below handles
22782 cv-qualified types, and we don't want to strip aliases with
22783 TYPE_MAIN_VARIANT because of DR 1558. */
22784 /* -- a compound type constructed from any dependent type. */
22785 if (TYPE_PTRMEM_P (type))
22786 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
22787 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
22788 (type)));
22789 else if (TYPE_PTR_P (type)
22790 || TREE_CODE (type) == REFERENCE_TYPE)
22791 return dependent_type_p (TREE_TYPE (type));
22792 else if (TREE_CODE (type) == FUNCTION_TYPE
22793 || TREE_CODE (type) == METHOD_TYPE)
22794 {
22795 tree arg_type;
22796
22797 if (dependent_type_p (TREE_TYPE (type)))
22798 return true;
22799 for (arg_type = TYPE_ARG_TYPES (type);
22800 arg_type;
22801 arg_type = TREE_CHAIN (arg_type))
22802 if (dependent_type_p (TREE_VALUE (arg_type)))
22803 return true;
22804 return false;
22805 }
22806 /* -- an array type constructed from any dependent type or whose
22807 size is specified by a constant expression that is
22808 value-dependent.
22809
22810 We checked for type- and value-dependence of the bounds in
22811 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
22812 if (TREE_CODE (type) == ARRAY_TYPE)
22813 {
22814 if (TYPE_DOMAIN (type)
22815 && dependent_type_p (TYPE_DOMAIN (type)))
22816 return true;
22817 return dependent_type_p (TREE_TYPE (type));
22818 }
22819
22820 /* -- a template-id in which either the template name is a template
22821 parameter ... */
22822 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
22823 return true;
22824 /* ... or any of the template arguments is a dependent type or
22825 an expression that is type-dependent or value-dependent. */
22826 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
22827 && (any_dependent_template_arguments_p
22828 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
22829 return true;
22830
22831 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
22832 dependent; if the argument of the `typeof' expression is not
22833 type-dependent, then it should already been have resolved. */
22834 if (TREE_CODE (type) == TYPEOF_TYPE
22835 || TREE_CODE (type) == DECLTYPE_TYPE
22836 || TREE_CODE (type) == UNDERLYING_TYPE)
22837 return true;
22838
22839 /* A template argument pack is dependent if any of its packed
22840 arguments are. */
22841 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
22842 {
22843 tree args = ARGUMENT_PACK_ARGS (type);
22844 int i, len = TREE_VEC_LENGTH (args);
22845 for (i = 0; i < len; ++i)
22846 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
22847 return true;
22848 }
22849
22850 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
22851 be template parameters. */
22852 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
22853 return true;
22854
22855 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
22856 return true;
22857
22858 /* The standard does not specifically mention types that are local
22859 to template functions or local classes, but they should be
22860 considered dependent too. For example:
22861
22862 template <int I> void f() {
22863 enum E { a = I };
22864 S<sizeof (E)> s;
22865 }
22866
22867 The size of `E' cannot be known until the value of `I' has been
22868 determined. Therefore, `E' must be considered dependent. */
22869 scope = TYPE_CONTEXT (type);
22870 if (scope && TYPE_P (scope))
22871 return dependent_type_p (scope);
22872 /* Don't use type_dependent_expression_p here, as it can lead
22873 to infinite recursion trying to determine whether a lambda
22874 nested in a lambda is dependent (c++/47687). */
22875 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
22876 && DECL_LANG_SPECIFIC (scope)
22877 && DECL_TEMPLATE_INFO (scope)
22878 && (any_dependent_template_arguments_p
22879 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
22880 return true;
22881
22882 /* Other types are non-dependent. */
22883 return false;
22884 }
22885
22886 /* Returns TRUE if TYPE is dependent, in the sense of
22887 [temp.dep.type]. Note that a NULL type is considered dependent. */
22888
22889 bool
22890 dependent_type_p (tree type)
22891 {
22892 /* If there are no template parameters in scope, then there can't be
22893 any dependent types. */
22894 if (!processing_template_decl)
22895 {
22896 /* If we are not processing a template, then nobody should be
22897 providing us with a dependent type. */
22898 gcc_assert (type);
22899 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
22900 return false;
22901 }
22902
22903 /* If the type is NULL, we have not computed a type for the entity
22904 in question; in that case, the type is dependent. */
22905 if (!type)
22906 return true;
22907
22908 /* Erroneous types can be considered non-dependent. */
22909 if (type == error_mark_node)
22910 return false;
22911
22912 /* If we have not already computed the appropriate value for TYPE,
22913 do so now. */
22914 if (!TYPE_DEPENDENT_P_VALID (type))
22915 {
22916 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
22917 TYPE_DEPENDENT_P_VALID (type) = 1;
22918 }
22919
22920 return TYPE_DEPENDENT_P (type);
22921 }
22922
22923 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
22924 lookup. In other words, a dependent type that is not the current
22925 instantiation. */
22926
22927 bool
22928 dependent_scope_p (tree scope)
22929 {
22930 return (scope && TYPE_P (scope) && dependent_type_p (scope)
22931 && !currently_open_class (scope));
22932 }
22933
22934 /* T is a SCOPE_REF; return whether we need to consider it
22935 instantiation-dependent so that we can check access at instantiation
22936 time even though we know which member it resolves to. */
22937
22938 static bool
22939 instantiation_dependent_scope_ref_p (tree t)
22940 {
22941 if (DECL_P (TREE_OPERAND (t, 1))
22942 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
22943 && accessible_in_template_p (TREE_OPERAND (t, 0),
22944 TREE_OPERAND (t, 1)))
22945 return false;
22946 else
22947 return true;
22948 }
22949
22950 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
22951 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
22952 expression. */
22953
22954 /* Note that this predicate is not appropriate for general expressions;
22955 only constant expressions (that satisfy potential_constant_expression)
22956 can be tested for value dependence. */
22957
22958 bool
22959 value_dependent_expression_p (tree expression)
22960 {
22961 if (!processing_template_decl || expression == NULL_TREE)
22962 return false;
22963
22964 /* A name declared with a dependent type. */
22965 if (DECL_P (expression) && type_dependent_expression_p (expression))
22966 return true;
22967
22968 switch (TREE_CODE (expression))
22969 {
22970 case BASELINK:
22971 /* A dependent member function of the current instantiation. */
22972 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
22973
22974 case FUNCTION_DECL:
22975 /* A dependent member function of the current instantiation. */
22976 if (DECL_CLASS_SCOPE_P (expression)
22977 && dependent_type_p (DECL_CONTEXT (expression)))
22978 return true;
22979 break;
22980
22981 case IDENTIFIER_NODE:
22982 /* A name that has not been looked up -- must be dependent. */
22983 return true;
22984
22985 case TEMPLATE_PARM_INDEX:
22986 /* A non-type template parm. */
22987 return true;
22988
22989 case CONST_DECL:
22990 /* A non-type template parm. */
22991 if (DECL_TEMPLATE_PARM_P (expression))
22992 return true;
22993 return value_dependent_expression_p (DECL_INITIAL (expression));
22994
22995 case VAR_DECL:
22996 /* A constant with literal type and is initialized
22997 with an expression that is value-dependent.
22998
22999 Note that a non-dependent parenthesized initializer will have
23000 already been replaced with its constant value, so if we see
23001 a TREE_LIST it must be dependent. */
23002 if (DECL_INITIAL (expression)
23003 && decl_constant_var_p (expression)
23004 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23005 /* cp_finish_decl doesn't fold reference initializers. */
23006 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23007 || type_dependent_expression_p (DECL_INITIAL (expression))
23008 || value_dependent_expression_p (DECL_INITIAL (expression))))
23009 return true;
23010 return false;
23011
23012 case DYNAMIC_CAST_EXPR:
23013 case STATIC_CAST_EXPR:
23014 case CONST_CAST_EXPR:
23015 case REINTERPRET_CAST_EXPR:
23016 case CAST_EXPR:
23017 /* These expressions are value-dependent if the type to which
23018 the cast occurs is dependent or the expression being casted
23019 is value-dependent. */
23020 {
23021 tree type = TREE_TYPE (expression);
23022
23023 if (dependent_type_p (type))
23024 return true;
23025
23026 /* A functional cast has a list of operands. */
23027 expression = TREE_OPERAND (expression, 0);
23028 if (!expression)
23029 {
23030 /* If there are no operands, it must be an expression such
23031 as "int()". This should not happen for aggregate types
23032 because it would form non-constant expressions. */
23033 gcc_assert (cxx_dialect >= cxx11
23034 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23035
23036 return false;
23037 }
23038
23039 if (TREE_CODE (expression) == TREE_LIST)
23040 return any_value_dependent_elements_p (expression);
23041
23042 return value_dependent_expression_p (expression);
23043 }
23044
23045 case SIZEOF_EXPR:
23046 if (SIZEOF_EXPR_TYPE_P (expression))
23047 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23048 /* FALLTHRU */
23049 case ALIGNOF_EXPR:
23050 case TYPEID_EXPR:
23051 /* A `sizeof' expression is value-dependent if the operand is
23052 type-dependent or is a pack expansion. */
23053 expression = TREE_OPERAND (expression, 0);
23054 if (PACK_EXPANSION_P (expression))
23055 return true;
23056 else if (TYPE_P (expression))
23057 return dependent_type_p (expression);
23058 return instantiation_dependent_uneval_expression_p (expression);
23059
23060 case AT_ENCODE_EXPR:
23061 /* An 'encode' expression is value-dependent if the operand is
23062 type-dependent. */
23063 expression = TREE_OPERAND (expression, 0);
23064 return dependent_type_p (expression);
23065
23066 case NOEXCEPT_EXPR:
23067 expression = TREE_OPERAND (expression, 0);
23068 return instantiation_dependent_uneval_expression_p (expression);
23069
23070 case SCOPE_REF:
23071 /* All instantiation-dependent expressions should also be considered
23072 value-dependent. */
23073 return instantiation_dependent_scope_ref_p (expression);
23074
23075 case COMPONENT_REF:
23076 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23077 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23078
23079 case NONTYPE_ARGUMENT_PACK:
23080 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23081 is value-dependent. */
23082 {
23083 tree values = ARGUMENT_PACK_ARGS (expression);
23084 int i, len = TREE_VEC_LENGTH (values);
23085
23086 for (i = 0; i < len; ++i)
23087 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23088 return true;
23089
23090 return false;
23091 }
23092
23093 case TRAIT_EXPR:
23094 {
23095 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23096 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23097 || (type2 ? dependent_type_p (type2) : false));
23098 }
23099
23100 case MODOP_EXPR:
23101 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23102 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23103
23104 case ARRAY_REF:
23105 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23106 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23107
23108 case ADDR_EXPR:
23109 {
23110 tree op = TREE_OPERAND (expression, 0);
23111 return (value_dependent_expression_p (op)
23112 || has_value_dependent_address (op));
23113 }
23114
23115 case REQUIRES_EXPR:
23116 /* Treat all requires-expressions as value-dependent so
23117 we don't try to fold them. */
23118 return true;
23119
23120 case TYPE_REQ:
23121 return dependent_type_p (TREE_OPERAND (expression, 0));
23122
23123 case CALL_EXPR:
23124 {
23125 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23126 return true;
23127 tree fn = get_callee_fndecl (expression);
23128 int i, nargs;
23129 nargs = call_expr_nargs (expression);
23130 for (i = 0; i < nargs; ++i)
23131 {
23132 tree op = CALL_EXPR_ARG (expression, i);
23133 /* In a call to a constexpr member function, look through the
23134 implicit ADDR_EXPR on the object argument so that it doesn't
23135 cause the call to be considered value-dependent. We also
23136 look through it in potential_constant_expression. */
23137 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23138 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23139 && TREE_CODE (op) == ADDR_EXPR)
23140 op = TREE_OPERAND (op, 0);
23141 if (value_dependent_expression_p (op))
23142 return true;
23143 }
23144 return false;
23145 }
23146
23147 case TEMPLATE_ID_EXPR:
23148 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23149 type-dependent. */
23150 return type_dependent_expression_p (expression)
23151 || variable_concept_p (TREE_OPERAND (expression, 0));
23152
23153 case CONSTRUCTOR:
23154 {
23155 unsigned ix;
23156 tree val;
23157 if (dependent_type_p (TREE_TYPE (expression)))
23158 return true;
23159 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23160 if (value_dependent_expression_p (val))
23161 return true;
23162 return false;
23163 }
23164
23165 case STMT_EXPR:
23166 /* Treat a GNU statement expression as dependent to avoid crashing
23167 under instantiate_non_dependent_expr; it can't be constant. */
23168 return true;
23169
23170 default:
23171 /* A constant expression is value-dependent if any subexpression is
23172 value-dependent. */
23173 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23174 {
23175 case tcc_reference:
23176 case tcc_unary:
23177 case tcc_comparison:
23178 case tcc_binary:
23179 case tcc_expression:
23180 case tcc_vl_exp:
23181 {
23182 int i, len = cp_tree_operand_length (expression);
23183
23184 for (i = 0; i < len; i++)
23185 {
23186 tree t = TREE_OPERAND (expression, i);
23187
23188 /* In some cases, some of the operands may be missing.l
23189 (For example, in the case of PREDECREMENT_EXPR, the
23190 amount to increment by may be missing.) That doesn't
23191 make the expression dependent. */
23192 if (t && value_dependent_expression_p (t))
23193 return true;
23194 }
23195 }
23196 break;
23197 default:
23198 break;
23199 }
23200 break;
23201 }
23202
23203 /* The expression is not value-dependent. */
23204 return false;
23205 }
23206
23207 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23208 [temp.dep.expr]. Note that an expression with no type is
23209 considered dependent. Other parts of the compiler arrange for an
23210 expression with type-dependent subexpressions to have no type, so
23211 this function doesn't have to be fully recursive. */
23212
23213 bool
23214 type_dependent_expression_p (tree expression)
23215 {
23216 if (!processing_template_decl)
23217 return false;
23218
23219 if (expression == NULL_TREE || expression == error_mark_node)
23220 return false;
23221
23222 /* An unresolved name is always dependent. */
23223 if (identifier_p (expression)
23224 || TREE_CODE (expression) == USING_DECL
23225 || TREE_CODE (expression) == WILDCARD_DECL)
23226 return true;
23227
23228 /* A fold expression is type-dependent. */
23229 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23230 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23231 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23232 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23233 return true;
23234
23235 /* Some expression forms are never type-dependent. */
23236 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23237 || TREE_CODE (expression) == SIZEOF_EXPR
23238 || TREE_CODE (expression) == ALIGNOF_EXPR
23239 || TREE_CODE (expression) == AT_ENCODE_EXPR
23240 || TREE_CODE (expression) == NOEXCEPT_EXPR
23241 || TREE_CODE (expression) == TRAIT_EXPR
23242 || TREE_CODE (expression) == TYPEID_EXPR
23243 || TREE_CODE (expression) == DELETE_EXPR
23244 || TREE_CODE (expression) == VEC_DELETE_EXPR
23245 || TREE_CODE (expression) == THROW_EXPR
23246 || TREE_CODE (expression) == REQUIRES_EXPR)
23247 return false;
23248
23249 /* The types of these expressions depends only on the type to which
23250 the cast occurs. */
23251 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23252 || TREE_CODE (expression) == STATIC_CAST_EXPR
23253 || TREE_CODE (expression) == CONST_CAST_EXPR
23254 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23255 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23256 || TREE_CODE (expression) == CAST_EXPR)
23257 return dependent_type_p (TREE_TYPE (expression));
23258
23259 /* The types of these expressions depends only on the type created
23260 by the expression. */
23261 if (TREE_CODE (expression) == NEW_EXPR
23262 || TREE_CODE (expression) == VEC_NEW_EXPR)
23263 {
23264 /* For NEW_EXPR tree nodes created inside a template, either
23265 the object type itself or a TREE_LIST may appear as the
23266 operand 1. */
23267 tree type = TREE_OPERAND (expression, 1);
23268 if (TREE_CODE (type) == TREE_LIST)
23269 /* This is an array type. We need to check array dimensions
23270 as well. */
23271 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23272 || value_dependent_expression_p
23273 (TREE_OPERAND (TREE_VALUE (type), 1));
23274 else
23275 return dependent_type_p (type);
23276 }
23277
23278 if (TREE_CODE (expression) == SCOPE_REF)
23279 {
23280 tree scope = TREE_OPERAND (expression, 0);
23281 tree name = TREE_OPERAND (expression, 1);
23282
23283 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23284 contains an identifier associated by name lookup with one or more
23285 declarations declared with a dependent type, or...a
23286 nested-name-specifier or qualified-id that names a member of an
23287 unknown specialization. */
23288 return (type_dependent_expression_p (name)
23289 || dependent_scope_p (scope));
23290 }
23291
23292 if (TREE_CODE (expression) == TEMPLATE_DECL
23293 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23294 return uses_outer_template_parms (expression);
23295
23296 if (TREE_CODE (expression) == STMT_EXPR)
23297 expression = stmt_expr_value_expr (expression);
23298
23299 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23300 {
23301 tree elt;
23302 unsigned i;
23303
23304 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23305 {
23306 if (type_dependent_expression_p (elt))
23307 return true;
23308 }
23309 return false;
23310 }
23311
23312 /* A static data member of the current instantiation with incomplete
23313 array type is type-dependent, as the definition and specializations
23314 can have different bounds. */
23315 if (VAR_P (expression)
23316 && DECL_CLASS_SCOPE_P (expression)
23317 && dependent_type_p (DECL_CONTEXT (expression))
23318 && VAR_HAD_UNKNOWN_BOUND (expression))
23319 return true;
23320
23321 /* An array of unknown bound depending on a variadic parameter, eg:
23322
23323 template<typename... Args>
23324 void foo (Args... args)
23325 {
23326 int arr[] = { args... };
23327 }
23328
23329 template<int... vals>
23330 void bar ()
23331 {
23332 int arr[] = { vals... };
23333 }
23334
23335 If the array has no length and has an initializer, it must be that
23336 we couldn't determine its length in cp_complete_array_type because
23337 it is dependent. */
23338 if (VAR_P (expression)
23339 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23340 && !TYPE_DOMAIN (TREE_TYPE (expression))
23341 && DECL_INITIAL (expression))
23342 return true;
23343
23344 /* A function or variable template-id is type-dependent if it has any
23345 dependent template arguments. Note that we only consider the innermost
23346 template arguments here, since those are the ones that come from the
23347 template-id; the template arguments for the enclosing class do not make it
23348 type-dependent, they only make a member function value-dependent. */
23349 if (VAR_OR_FUNCTION_DECL_P (expression)
23350 && DECL_LANG_SPECIFIC (expression)
23351 && DECL_TEMPLATE_INFO (expression)
23352 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23353 && (any_dependent_template_arguments_p
23354 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23355 return true;
23356
23357 /* Always dependent, on the number of arguments if nothing else. */
23358 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23359 return true;
23360
23361 if (TREE_TYPE (expression) == unknown_type_node)
23362 {
23363 if (TREE_CODE (expression) == ADDR_EXPR)
23364 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23365 if (TREE_CODE (expression) == COMPONENT_REF
23366 || TREE_CODE (expression) == OFFSET_REF)
23367 {
23368 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23369 return true;
23370 expression = TREE_OPERAND (expression, 1);
23371 if (identifier_p (expression))
23372 return false;
23373 }
23374 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23375 if (TREE_CODE (expression) == SCOPE_REF)
23376 return false;
23377
23378 if (BASELINK_P (expression))
23379 {
23380 if (BASELINK_OPTYPE (expression)
23381 && dependent_type_p (BASELINK_OPTYPE (expression)))
23382 return true;
23383 expression = BASELINK_FUNCTIONS (expression);
23384 }
23385
23386 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23387 {
23388 if (any_dependent_template_arguments_p
23389 (TREE_OPERAND (expression, 1)))
23390 return true;
23391 expression = TREE_OPERAND (expression, 0);
23392 if (identifier_p (expression))
23393 return true;
23394 }
23395
23396 gcc_assert (TREE_CODE (expression) == OVERLOAD
23397 || TREE_CODE (expression) == FUNCTION_DECL);
23398
23399 while (expression)
23400 {
23401 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23402 return true;
23403 expression = OVL_NEXT (expression);
23404 }
23405 return false;
23406 }
23407
23408 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23409
23410 /* Dependent type attributes might not have made it from the decl to
23411 the type yet. */
23412 if (DECL_P (expression)
23413 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23414 return true;
23415
23416 return (dependent_type_p (TREE_TYPE (expression)));
23417 }
23418
23419 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23420 type-dependent if the expression refers to a member of the current
23421 instantiation and the type of the referenced member is dependent, or the
23422 class member access expression refers to a member of an unknown
23423 specialization.
23424
23425 This function returns true if the OBJECT in such a class member access
23426 expression is of an unknown specialization. */
23427
23428 bool
23429 type_dependent_object_expression_p (tree object)
23430 {
23431 tree scope = TREE_TYPE (object);
23432 return (!scope || dependent_scope_p (scope));
23433 }
23434
23435 /* walk_tree callback function for instantiation_dependent_expression_p,
23436 below. Returns non-zero if a dependent subexpression is found. */
23437
23438 static tree
23439 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23440 void * /*data*/)
23441 {
23442 if (TYPE_P (*tp))
23443 {
23444 /* We don't have to worry about decltype currently because decltype
23445 of an instantiation-dependent expr is a dependent type. This
23446 might change depending on the resolution of DR 1172. */
23447 *walk_subtrees = false;
23448 return NULL_TREE;
23449 }
23450 enum tree_code code = TREE_CODE (*tp);
23451 switch (code)
23452 {
23453 /* Don't treat an argument list as dependent just because it has no
23454 TREE_TYPE. */
23455 case TREE_LIST:
23456 case TREE_VEC:
23457 return NULL_TREE;
23458
23459 case TEMPLATE_PARM_INDEX:
23460 return *tp;
23461
23462 /* Handle expressions with type operands. */
23463 case SIZEOF_EXPR:
23464 case ALIGNOF_EXPR:
23465 case TYPEID_EXPR:
23466 case AT_ENCODE_EXPR:
23467 {
23468 tree op = TREE_OPERAND (*tp, 0);
23469 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
23470 op = TREE_TYPE (op);
23471 if (TYPE_P (op))
23472 {
23473 if (dependent_type_p (op))
23474 return *tp;
23475 else
23476 {
23477 *walk_subtrees = false;
23478 return NULL_TREE;
23479 }
23480 }
23481 break;
23482 }
23483
23484 case COMPONENT_REF:
23485 if (identifier_p (TREE_OPERAND (*tp, 1)))
23486 /* In a template, finish_class_member_access_expr creates a
23487 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
23488 type-dependent, so that we can check access control at
23489 instantiation time (PR 42277). See also Core issue 1273. */
23490 return *tp;
23491 break;
23492
23493 case SCOPE_REF:
23494 if (instantiation_dependent_scope_ref_p (*tp))
23495 return *tp;
23496 else
23497 break;
23498
23499 /* Treat statement-expressions as dependent. */
23500 case BIND_EXPR:
23501 return *tp;
23502
23503 /* Treat requires-expressions as dependent. */
23504 case REQUIRES_EXPR:
23505 return *tp;
23506
23507 case CALL_EXPR:
23508 /* Treat calls to function concepts as dependent. */
23509 if (function_concept_check_p (*tp))
23510 return *tp;
23511 break;
23512
23513 case TEMPLATE_ID_EXPR:
23514 /* And variable concepts. */
23515 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
23516 return *tp;
23517 break;
23518
23519 default:
23520 break;
23521 }
23522
23523 if (type_dependent_expression_p (*tp))
23524 return *tp;
23525 else
23526 return NULL_TREE;
23527 }
23528
23529 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
23530 sense defined by the ABI:
23531
23532 "An expression is instantiation-dependent if it is type-dependent
23533 or value-dependent, or it has a subexpression that is type-dependent
23534 or value-dependent."
23535
23536 Except don't actually check value-dependence for unevaluated expressions,
23537 because in sizeof(i) we don't care about the value of i. Checking
23538 type-dependence will in turn check value-dependence of array bounds/template
23539 arguments as needed. */
23540
23541 bool
23542 instantiation_dependent_uneval_expression_p (tree expression)
23543 {
23544 tree result;
23545
23546 if (!processing_template_decl)
23547 return false;
23548
23549 if (expression == error_mark_node)
23550 return false;
23551
23552 result = cp_walk_tree_without_duplicates (&expression,
23553 instantiation_dependent_r, NULL);
23554 return result != NULL_TREE;
23555 }
23556
23557 /* As above, but also check value-dependence of the expression as a whole. */
23558
23559 bool
23560 instantiation_dependent_expression_p (tree expression)
23561 {
23562 return (instantiation_dependent_uneval_expression_p (expression)
23563 || value_dependent_expression_p (expression));
23564 }
23565
23566 /* Like type_dependent_expression_p, but it also works while not processing
23567 a template definition, i.e. during substitution or mangling. */
23568
23569 bool
23570 type_dependent_expression_p_push (tree expr)
23571 {
23572 bool b;
23573 ++processing_template_decl;
23574 b = type_dependent_expression_p (expr);
23575 --processing_template_decl;
23576 return b;
23577 }
23578
23579 /* Returns TRUE if ARGS contains a type-dependent expression. */
23580
23581 bool
23582 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
23583 {
23584 unsigned int i;
23585 tree arg;
23586
23587 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
23588 {
23589 if (type_dependent_expression_p (arg))
23590 return true;
23591 }
23592 return false;
23593 }
23594
23595 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23596 expressions) contains any type-dependent expressions. */
23597
23598 bool
23599 any_type_dependent_elements_p (const_tree list)
23600 {
23601 for (; list; list = TREE_CHAIN (list))
23602 if (type_dependent_expression_p (TREE_VALUE (list)))
23603 return true;
23604
23605 return false;
23606 }
23607
23608 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
23609 expressions) contains any value-dependent expressions. */
23610
23611 bool
23612 any_value_dependent_elements_p (const_tree list)
23613 {
23614 for (; list; list = TREE_CHAIN (list))
23615 if (value_dependent_expression_p (TREE_VALUE (list)))
23616 return true;
23617
23618 return false;
23619 }
23620
23621 /* Returns TRUE if the ARG (a template argument) is dependent. */
23622
23623 bool
23624 dependent_template_arg_p (tree arg)
23625 {
23626 if (!processing_template_decl)
23627 return false;
23628
23629 /* Assume a template argument that was wrongly written by the user
23630 is dependent. This is consistent with what
23631 any_dependent_template_arguments_p [that calls this function]
23632 does. */
23633 if (!arg || arg == error_mark_node)
23634 return true;
23635
23636 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
23637 arg = ARGUMENT_PACK_SELECT_ARG (arg);
23638
23639 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
23640 return true;
23641 if (TREE_CODE (arg) == TEMPLATE_DECL)
23642 {
23643 if (DECL_TEMPLATE_PARM_P (arg))
23644 return true;
23645 /* A member template of a dependent class is not necessarily
23646 type-dependent, but it is a dependent template argument because it
23647 will be a member of an unknown specialization to that template. */
23648 tree scope = CP_DECL_CONTEXT (arg);
23649 return TYPE_P (scope) && dependent_type_p (scope);
23650 }
23651 else if (ARGUMENT_PACK_P (arg))
23652 {
23653 tree args = ARGUMENT_PACK_ARGS (arg);
23654 int i, len = TREE_VEC_LENGTH (args);
23655 for (i = 0; i < len; ++i)
23656 {
23657 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23658 return true;
23659 }
23660
23661 return false;
23662 }
23663 else if (TYPE_P (arg))
23664 return dependent_type_p (arg);
23665 else
23666 return (type_dependent_expression_p (arg)
23667 || value_dependent_expression_p (arg));
23668 }
23669
23670 /* Returns true if ARGS (a collection of template arguments) contains
23671 any types that require structural equality testing. */
23672
23673 bool
23674 any_template_arguments_need_structural_equality_p (tree args)
23675 {
23676 int i;
23677 int j;
23678
23679 if (!args)
23680 return false;
23681 if (args == error_mark_node)
23682 return true;
23683
23684 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23685 {
23686 tree level = TMPL_ARGS_LEVEL (args, i + 1);
23687 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23688 {
23689 tree arg = TREE_VEC_ELT (level, j);
23690 tree packed_args = NULL_TREE;
23691 int k, len = 1;
23692
23693 if (ARGUMENT_PACK_P (arg))
23694 {
23695 /* Look inside the argument pack. */
23696 packed_args = ARGUMENT_PACK_ARGS (arg);
23697 len = TREE_VEC_LENGTH (packed_args);
23698 }
23699
23700 for (k = 0; k < len; ++k)
23701 {
23702 if (packed_args)
23703 arg = TREE_VEC_ELT (packed_args, k);
23704
23705 if (error_operand_p (arg))
23706 return true;
23707 else if (TREE_CODE (arg) == TEMPLATE_DECL)
23708 continue;
23709 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
23710 return true;
23711 else if (!TYPE_P (arg) && TREE_TYPE (arg)
23712 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
23713 return true;
23714 }
23715 }
23716 }
23717
23718 return false;
23719 }
23720
23721 /* Returns true if ARGS (a collection of template arguments) contains
23722 any dependent arguments. */
23723
23724 bool
23725 any_dependent_template_arguments_p (const_tree args)
23726 {
23727 int i;
23728 int j;
23729
23730 if (!args)
23731 return false;
23732 if (args == error_mark_node)
23733 return true;
23734
23735 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
23736 {
23737 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
23738 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
23739 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
23740 return true;
23741 }
23742
23743 return false;
23744 }
23745
23746 /* Returns TRUE if the template TMPL is type-dependent. */
23747
23748 bool
23749 dependent_template_p (tree tmpl)
23750 {
23751 if (TREE_CODE (tmpl) == OVERLOAD)
23752 {
23753 while (tmpl)
23754 {
23755 if (dependent_template_p (OVL_CURRENT (tmpl)))
23756 return true;
23757 tmpl = OVL_NEXT (tmpl);
23758 }
23759 return false;
23760 }
23761
23762 /* Template template parameters are dependent. */
23763 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
23764 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
23765 return true;
23766 /* So are names that have not been looked up. */
23767 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
23768 return true;
23769 return false;
23770 }
23771
23772 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
23773
23774 bool
23775 dependent_template_id_p (tree tmpl, tree args)
23776 {
23777 return (dependent_template_p (tmpl)
23778 || any_dependent_template_arguments_p (args));
23779 }
23780
23781 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
23782 are dependent. */
23783
23784 bool
23785 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
23786 {
23787 int i;
23788
23789 if (!processing_template_decl)
23790 return false;
23791
23792 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
23793 {
23794 tree decl = TREE_VEC_ELT (declv, i);
23795 tree init = TREE_VEC_ELT (initv, i);
23796 tree cond = TREE_VEC_ELT (condv, i);
23797 tree incr = TREE_VEC_ELT (incrv, i);
23798
23799 if (type_dependent_expression_p (decl)
23800 || TREE_CODE (decl) == SCOPE_REF)
23801 return true;
23802
23803 if (init && type_dependent_expression_p (init))
23804 return true;
23805
23806 if (type_dependent_expression_p (cond))
23807 return true;
23808
23809 if (COMPARISON_CLASS_P (cond)
23810 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
23811 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
23812 return true;
23813
23814 if (TREE_CODE (incr) == MODOP_EXPR)
23815 {
23816 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
23817 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
23818 return true;
23819 }
23820 else if (type_dependent_expression_p (incr))
23821 return true;
23822 else if (TREE_CODE (incr) == MODIFY_EXPR)
23823 {
23824 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
23825 return true;
23826 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
23827 {
23828 tree t = TREE_OPERAND (incr, 1);
23829 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
23830 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
23831 return true;
23832 }
23833 }
23834 }
23835
23836 return false;
23837 }
23838
23839 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
23840 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
23841 no such TYPE can be found. Note that this function peers inside
23842 uninstantiated templates and therefore should be used only in
23843 extremely limited situations. ONLY_CURRENT_P restricts this
23844 peering to the currently open classes hierarchy (which is required
23845 when comparing types). */
23846
23847 tree
23848 resolve_typename_type (tree type, bool only_current_p)
23849 {
23850 tree scope;
23851 tree name;
23852 tree decl;
23853 int quals;
23854 tree pushed_scope;
23855 tree result;
23856
23857 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
23858
23859 scope = TYPE_CONTEXT (type);
23860 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
23861 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
23862 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
23863 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
23864 identifier of the TYPENAME_TYPE anymore.
23865 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
23866 TYPENAME_TYPE instead, we avoid messing up with a possible
23867 typedef variant case. */
23868 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
23869
23870 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
23871 it first before we can figure out what NAME refers to. */
23872 if (TREE_CODE (scope) == TYPENAME_TYPE)
23873 {
23874 if (TYPENAME_IS_RESOLVING_P (scope))
23875 /* Given a class template A with a dependent base with nested type C,
23876 typedef typename A::C::C C will land us here, as trying to resolve
23877 the initial A::C leads to the local C typedef, which leads back to
23878 A::C::C. So we break the recursion now. */
23879 return type;
23880 else
23881 scope = resolve_typename_type (scope, only_current_p);
23882 }
23883 /* If we don't know what SCOPE refers to, then we cannot resolve the
23884 TYPENAME_TYPE. */
23885 if (!CLASS_TYPE_P (scope))
23886 return type;
23887 /* If this is a typedef, we don't want to look inside (c++/11987). */
23888 if (typedef_variant_p (type))
23889 return type;
23890 /* If SCOPE isn't the template itself, it will not have a valid
23891 TYPE_FIELDS list. */
23892 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
23893 /* scope is either the template itself or a compatible instantiation
23894 like X<T>, so look up the name in the original template. */
23895 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
23896 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
23897 gcc_checking_assert (uses_template_parms (scope));
23898 /* If scope has no fields, it can't be a current instantiation. Check this
23899 before currently_open_class to avoid infinite recursion (71515). */
23900 if (!TYPE_FIELDS (scope))
23901 return type;
23902 /* If the SCOPE is not the current instantiation, there's no reason
23903 to look inside it. */
23904 if (only_current_p && !currently_open_class (scope))
23905 return type;
23906 /* Enter the SCOPE so that name lookup will be resolved as if we
23907 were in the class definition. In particular, SCOPE will no
23908 longer be considered a dependent type. */
23909 pushed_scope = push_scope (scope);
23910 /* Look up the declaration. */
23911 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
23912 tf_warning_or_error);
23913
23914 result = NULL_TREE;
23915
23916 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
23917 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
23918 if (!decl)
23919 /*nop*/;
23920 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
23921 && TREE_CODE (decl) == TYPE_DECL)
23922 {
23923 result = TREE_TYPE (decl);
23924 if (result == error_mark_node)
23925 result = NULL_TREE;
23926 }
23927 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
23928 && DECL_CLASS_TEMPLATE_P (decl))
23929 {
23930 tree tmpl;
23931 tree args;
23932 /* Obtain the template and the arguments. */
23933 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
23934 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
23935 /* Instantiate the template. */
23936 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
23937 /*entering_scope=*/0,
23938 tf_error | tf_user);
23939 if (result == error_mark_node)
23940 result = NULL_TREE;
23941 }
23942
23943 /* Leave the SCOPE. */
23944 if (pushed_scope)
23945 pop_scope (pushed_scope);
23946
23947 /* If we failed to resolve it, return the original typename. */
23948 if (!result)
23949 return type;
23950
23951 /* If lookup found a typename type, resolve that too. */
23952 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
23953 {
23954 /* Ill-formed programs can cause infinite recursion here, so we
23955 must catch that. */
23956 TYPENAME_IS_RESOLVING_P (result) = 1;
23957 result = resolve_typename_type (result, only_current_p);
23958 TYPENAME_IS_RESOLVING_P (result) = 0;
23959 }
23960
23961 /* Qualify the resulting type. */
23962 quals = cp_type_quals (type);
23963 if (quals)
23964 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
23965
23966 return result;
23967 }
23968
23969 /* EXPR is an expression which is not type-dependent. Return a proxy
23970 for EXPR that can be used to compute the types of larger
23971 expressions containing EXPR. */
23972
23973 tree
23974 build_non_dependent_expr (tree expr)
23975 {
23976 tree inner_expr;
23977
23978 /* When checking, try to get a constant value for all non-dependent
23979 expressions in order to expose bugs in *_dependent_expression_p
23980 and constexpr. This can affect code generation, see PR70704, so
23981 only do this for -fchecking=2. */
23982 if (flag_checking > 1
23983 && cxx_dialect >= cxx11
23984 /* Don't do this during nsdmi parsing as it can lead to
23985 unexpected recursive instantiations. */
23986 && !parsing_nsdmi ()
23987 /* Don't do this during concept expansion either and for
23988 the same reason. */
23989 && !expanding_concept ())
23990 fold_non_dependent_expr (expr);
23991
23992 /* Preserve OVERLOADs; the functions must be available to resolve
23993 types. */
23994 inner_expr = expr;
23995 if (TREE_CODE (inner_expr) == STMT_EXPR)
23996 inner_expr = stmt_expr_value_expr (inner_expr);
23997 if (TREE_CODE (inner_expr) == ADDR_EXPR)
23998 inner_expr = TREE_OPERAND (inner_expr, 0);
23999 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24000 inner_expr = TREE_OPERAND (inner_expr, 1);
24001 if (is_overloaded_fn (inner_expr)
24002 || TREE_CODE (inner_expr) == OFFSET_REF)
24003 return expr;
24004 /* There is no need to return a proxy for a variable. */
24005 if (VAR_P (expr))
24006 return expr;
24007 /* Preserve string constants; conversions from string constants to
24008 "char *" are allowed, even though normally a "const char *"
24009 cannot be used to initialize a "char *". */
24010 if (TREE_CODE (expr) == STRING_CST)
24011 return expr;
24012 /* Preserve void and arithmetic constants, as an optimization -- there is no
24013 reason to create a new node. */
24014 if (TREE_CODE (expr) == VOID_CST
24015 || TREE_CODE (expr) == INTEGER_CST
24016 || TREE_CODE (expr) == REAL_CST)
24017 return expr;
24018 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24019 There is at least one place where we want to know that a
24020 particular expression is a throw-expression: when checking a ?:
24021 expression, there are special rules if the second or third
24022 argument is a throw-expression. */
24023 if (TREE_CODE (expr) == THROW_EXPR)
24024 return expr;
24025
24026 /* Don't wrap an initializer list, we need to be able to look inside. */
24027 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24028 return expr;
24029
24030 /* Don't wrap a dummy object, we need to be able to test for it. */
24031 if (is_dummy_object (expr))
24032 return expr;
24033
24034 if (TREE_CODE (expr) == COND_EXPR)
24035 return build3 (COND_EXPR,
24036 TREE_TYPE (expr),
24037 TREE_OPERAND (expr, 0),
24038 (TREE_OPERAND (expr, 1)
24039 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24040 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24041 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24042 if (TREE_CODE (expr) == COMPOUND_EXPR
24043 && !COMPOUND_EXPR_OVERLOADED (expr))
24044 return build2 (COMPOUND_EXPR,
24045 TREE_TYPE (expr),
24046 TREE_OPERAND (expr, 0),
24047 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24048
24049 /* If the type is unknown, it can't really be non-dependent */
24050 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24051
24052 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24053 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24054 }
24055
24056 /* ARGS is a vector of expressions as arguments to a function call.
24057 Replace the arguments with equivalent non-dependent expressions.
24058 This modifies ARGS in place. */
24059
24060 void
24061 make_args_non_dependent (vec<tree, va_gc> *args)
24062 {
24063 unsigned int ix;
24064 tree arg;
24065
24066 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24067 {
24068 tree newarg = build_non_dependent_expr (arg);
24069 if (newarg != arg)
24070 (*args)[ix] = newarg;
24071 }
24072 }
24073
24074 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24075 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24076 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24077
24078 static tree
24079 make_auto_1 (tree name, bool set_canonical)
24080 {
24081 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24082 TYPE_NAME (au) = build_decl (input_location,
24083 TYPE_DECL, name, au);
24084 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24085 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24086 (0, processing_template_decl + 1, processing_template_decl + 1,
24087 TYPE_NAME (au), NULL_TREE);
24088 if (set_canonical)
24089 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24090 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24091 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24092
24093 return au;
24094 }
24095
24096 tree
24097 make_decltype_auto (void)
24098 {
24099 return make_auto_1 (get_identifier ("decltype(auto)"), true);
24100 }
24101
24102 tree
24103 make_auto (void)
24104 {
24105 return make_auto_1 (get_identifier ("auto"), true);
24106 }
24107
24108 /* Return a C++17 deduction placeholder for class template TMPL. */
24109
24110 tree
24111 make_template_placeholder (tree tmpl)
24112 {
24113 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24114 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24115 return t;
24116 }
24117
24118 /* Make a "constrained auto" type-specifier. This is an
24119 auto type with constraints that must be associated after
24120 deduction. The constraint is formed from the given
24121 CONC and its optional sequence of arguments, which are
24122 non-null if written as partial-concept-id. */
24123
24124 tree
24125 make_constrained_auto (tree con, tree args)
24126 {
24127 tree type = make_auto_1 (get_identifier ("auto"), false);
24128
24129 /* Build the constraint. */
24130 tree tmpl = DECL_TI_TEMPLATE (con);
24131 tree expr;
24132 if (VAR_P (con))
24133 expr = build_concept_check (tmpl, type, args);
24134 else
24135 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24136
24137 tree constr = normalize_expression (expr);
24138 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24139
24140 /* Our canonical type depends on the constraint. */
24141 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24142
24143 /* Attach the constraint to the type declaration. */
24144 tree decl = TYPE_NAME (type);
24145 return decl;
24146 }
24147
24148 /* Given type ARG, return std::initializer_list<ARG>. */
24149
24150 static tree
24151 listify (tree arg)
24152 {
24153 tree std_init_list = namespace_binding
24154 (get_identifier ("initializer_list"), std_node);
24155 tree argvec;
24156 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24157 {
24158 error ("deducing from brace-enclosed initializer list requires "
24159 "#include <initializer_list>");
24160 return error_mark_node;
24161 }
24162 argvec = make_tree_vec (1);
24163 TREE_VEC_ELT (argvec, 0) = arg;
24164 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24165 NULL_TREE, 0, tf_warning_or_error);
24166 }
24167
24168 /* Replace auto in TYPE with std::initializer_list<auto>. */
24169
24170 static tree
24171 listify_autos (tree type, tree auto_node)
24172 {
24173 tree init_auto = listify (auto_node);
24174 tree argvec = make_tree_vec (1);
24175 TREE_VEC_ELT (argvec, 0) = init_auto;
24176 if (processing_template_decl)
24177 argvec = add_to_template_args (current_template_args (), argvec);
24178 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24179 }
24180
24181 /* Hash traits for hashing possibly constrained 'auto'
24182 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24183
24184 struct auto_hash : default_hash_traits<tree>
24185 {
24186 static inline hashval_t hash (tree);
24187 static inline bool equal (tree, tree);
24188 };
24189
24190 /* Hash the 'auto' T. */
24191
24192 inline hashval_t
24193 auto_hash::hash (tree t)
24194 {
24195 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24196 /* Matching constrained-type-specifiers denote the same template
24197 parameter, so hash the constraint. */
24198 return hash_placeholder_constraint (c);
24199 else
24200 /* But unconstrained autos are all separate, so just hash the pointer. */
24201 return iterative_hash_object (t, 0);
24202 }
24203
24204 /* Compare two 'auto's. */
24205
24206 inline bool
24207 auto_hash::equal (tree t1, tree t2)
24208 {
24209 if (t1 == t2)
24210 return true;
24211
24212 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24213 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24214
24215 /* Two unconstrained autos are distinct. */
24216 if (!c1 || !c2)
24217 return false;
24218
24219 return equivalent_placeholder_constraints (c1, c2);
24220 }
24221
24222 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24223 constrained) auto, add it to the vector. */
24224
24225 static int
24226 extract_autos_r (tree t, void *data)
24227 {
24228 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24229 if (is_auto_or_concept (t))
24230 {
24231 /* All the autos were built with index 0; fix that up now. */
24232 tree *p = hash.find_slot (t, INSERT);
24233 unsigned idx;
24234 if (*p)
24235 /* If this is a repeated constrained-type-specifier, use the index we
24236 chose before. */
24237 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24238 else
24239 {
24240 /* Otherwise this is new, so use the current count. */
24241 *p = t;
24242 idx = hash.elements () - 1;
24243 }
24244 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24245 }
24246
24247 /* Always keep walking. */
24248 return 0;
24249 }
24250
24251 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24252 says they can appear anywhere in the type. */
24253
24254 static tree
24255 extract_autos (tree type)
24256 {
24257 hash_set<tree> visited;
24258 hash_table<auto_hash> hash (2);
24259
24260 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24261
24262 tree tree_vec = make_tree_vec (hash.elements());
24263 for (hash_table<auto_hash>::iterator iter = hash.begin();
24264 iter != hash.end(); ++iter)
24265 {
24266 tree elt = *iter;
24267 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24268 TREE_VEC_ELT (tree_vec, i)
24269 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24270 }
24271
24272 return tree_vec;
24273 }
24274
24275 /* The stem for deduction guide names. */
24276 const char *const dguide_base = "__dguide_";
24277
24278 /* Return the name for a deduction guide for class template TMPL. */
24279
24280 tree
24281 dguide_name (tree tmpl)
24282 {
24283 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24284 tree tname = TYPE_IDENTIFIER (type);
24285 char *buf = (char *) alloca (1 + strlen (dguide_base)
24286 + IDENTIFIER_LENGTH (tname));
24287 memcpy (buf, dguide_base, strlen (dguide_base));
24288 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24289 IDENTIFIER_LENGTH (tname) + 1);
24290 tree dname = get_identifier (buf);
24291 TREE_TYPE (dname) = type;
24292 return dname;
24293 }
24294
24295 /* True if NAME is the name of a deduction guide. */
24296
24297 bool
24298 dguide_name_p (tree name)
24299 {
24300 return (TREE_TYPE (name)
24301 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24302 strlen (dguide_base)));
24303 }
24304
24305 /* True if FN is a deduction guide. */
24306
24307 bool
24308 deduction_guide_p (tree fn)
24309 {
24310 if (tree name = DECL_NAME (fn))
24311 return dguide_name_p (name);
24312 return false;
24313 }
24314
24315 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24316 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24317 template parameter types. Note that the handling of template template
24318 parameters relies on current_template_parms being set appropriately for the
24319 new template. */
24320
24321 static tree
24322 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24323 tree tsubst_args, tsubst_flags_t complain)
24324 {
24325 tree oldidx = get_template_parm_index (olddecl);
24326
24327 tree newtype;
24328 if (TREE_CODE (olddecl) == TYPE_DECL
24329 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24330 {
24331 newtype = cxx_make_type (TREE_CODE (TREE_TYPE (olddecl)));
24332 TYPE_MAIN_VARIANT (newtype) = newtype;
24333 }
24334 else
24335 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24336 complain, NULL_TREE);
24337
24338 tree newdecl
24339 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24340 DECL_NAME (olddecl), newtype);
24341 SET_DECL_TEMPLATE_PARM_P (newdecl);
24342
24343 tree newidx;
24344 if (TREE_CODE (olddecl) == TYPE_DECL
24345 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24346 {
24347 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24348 = build_template_parm_index (index, level, level,
24349 newdecl, newtype);
24350 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24351 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24352
24353 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24354 {
24355 DECL_TEMPLATE_RESULT (newdecl)
24356 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24357 DECL_NAME (olddecl), newtype);
24358 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24359 // First create a copy (ttargs) of tsubst_args with an
24360 // additional level for the template template parameter's own
24361 // template parameters (ttparms).
24362 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24363 (DECL_TEMPLATE_PARMS (olddecl)));
24364 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24365 tree ttargs = make_tree_vec (depth + 1);
24366 for (int i = 0; i < depth; ++i)
24367 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24368 TREE_VEC_ELT (ttargs, depth)
24369 = template_parms_level_to_args (ttparms);
24370 // Substitute ttargs into ttparms to fix references to
24371 // other template parameters.
24372 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24373 complain);
24374 // Now substitute again with args based on tparms, to reduce
24375 // the level of the ttparms.
24376 ttargs = current_template_args ();
24377 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24378 complain);
24379 // Finally, tack the adjusted parms onto tparms.
24380 ttparms = tree_cons (size_int (depth), ttparms,
24381 current_template_parms);
24382 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24383 }
24384 }
24385 else
24386 {
24387 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24388 tree newconst
24389 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24390 TREE_CODE (oldconst),
24391 DECL_NAME (oldconst), newtype);
24392 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
24393 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
24394 SET_DECL_TEMPLATE_PARM_P (newconst);
24395 newidx = build_template_parm_index (index, level, level,
24396 newconst, newtype);
24397 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
24398 }
24399
24400 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24401 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24402 return newdecl;
24403 }
24404
24405 /* Returns a C++17 class deduction guide template based on the constructor
24406 CTOR. */
24407
24408 static tree
24409 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
24410 {
24411 if (outer_args)
24412 ctor = tsubst (ctor, outer_args, complain, ctor);
24413 tree type = DECL_CONTEXT (ctor);
24414 tree fn_tmpl;
24415 if (TREE_CODE (ctor) == TEMPLATE_DECL)
24416 {
24417 fn_tmpl = ctor;
24418 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
24419 }
24420 else
24421 fn_tmpl = DECL_TI_TEMPLATE (ctor);
24422
24423 tree tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
24424 /* If type is a member class template, DECL_TI_ARGS (ctor) will have fully
24425 specialized args for the enclosing class. Strip those off, as the
24426 deduction guide won't have those template parameters. */
24427 tree targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
24428 TMPL_PARMS_DEPTH (tparms));
24429 /* Discard the 'this' parameter. */
24430 tree fparms = FUNCTION_ARG_CHAIN (ctor);
24431 tree fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
24432 tree ci = get_constraints (ctor);
24433
24434 if (PRIMARY_TEMPLATE_P (fn_tmpl))
24435 {
24436 /* For a member template constructor, we need to flatten the two template
24437 parameter lists into one, and then adjust the function signature
24438 accordingly. This gets...complicated. */
24439 ++processing_template_decl;
24440 tree save_parms = current_template_parms;
24441
24442 /* For a member template we should have two levels of parms/args, one for
24443 the class and one for the constructor. We stripped specialized args
24444 for further enclosing classes above. */
24445 const int depth = 2;
24446 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
24447
24448 /* Template args for translating references to the two-level template
24449 parameters into references to the one-level template parameters we are
24450 creating. */
24451 tree tsubst_args = copy_node (targs);
24452 TMPL_ARGS_LEVEL (tsubst_args, depth)
24453 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
24454
24455 /* Template parms for the constructor template. */
24456 tree ftparms = TREE_VALUE (tparms);
24457 unsigned flen = TREE_VEC_LENGTH (ftparms);
24458 /* Template parms for the class template. */
24459 tparms = TREE_CHAIN (tparms);
24460 tree ctparms = TREE_VALUE (tparms);
24461 unsigned clen = TREE_VEC_LENGTH (ctparms);
24462 /* Template parms for the deduction guide start as a copy of the template
24463 parms for the class. We set current_template_parms for
24464 lookup_template_class_1. */
24465 current_template_parms = tparms = copy_node (tparms);
24466 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
24467 for (unsigned i = 0; i < clen; ++i)
24468 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
24469
24470 /* Now we need to rewrite the constructor parms to append them to the
24471 class parms. */
24472 for (unsigned i = 0; i < flen; ++i)
24473 {
24474 unsigned index = i + clen;
24475 unsigned level = 1;
24476 tree oldelt = TREE_VEC_ELT (ftparms, i);
24477 tree olddecl = TREE_VALUE (oldelt);
24478 tree newdecl = rewrite_template_parm (olddecl, index, level,
24479 tsubst_args, complain);
24480 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
24481 tsubst_args, complain, ctor);
24482 tree list = build_tree_list (newdef, newdecl);
24483 TEMPLATE_PARM_CONSTRAINTS (list)
24484 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
24485 tsubst_args, complain, ctor);
24486 TREE_VEC_ELT (new_vec, index) = list;
24487 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
24488 }
24489
24490 /* Now we have a final set of template parms to substitute into the
24491 function signature. */
24492 targs = template_parms_to_args (tparms);
24493 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
24494 complain, ctor);
24495 fargs = tsubst (fargs, tsubst_args, complain, ctor);
24496 if (ci)
24497 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
24498
24499 current_template_parms = save_parms;
24500 --processing_template_decl;
24501 }
24502
24503 tree fntype = build_function_type (type, fparms);
24504 tree ded_fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (ctor),
24505 FUNCTION_DECL,
24506 dguide_name (type), fntype);
24507 DECL_ARGUMENTS (ded_fn) = fargs;
24508 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
24509 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
24510 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
24511 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
24512 if (ci)
24513 set_constraints (ded_tmpl, ci);
24514
24515 return ded_tmpl;
24516 }
24517
24518 /* Deduce template arguments for the class template TMPL based on the
24519 initializer INIT, and return the resulting type. */
24520
24521 tree
24522 do_class_deduction (tree tmpl, tree init, tsubst_flags_t complain)
24523 {
24524 gcc_assert (DECL_CLASS_TEMPLATE_P (tmpl));
24525 tree type = TREE_TYPE (tmpl);
24526
24527 vec<tree,va_gc> *args;
24528 if (TREE_CODE (init) == TREE_LIST)
24529 args = make_tree_vector_from_list (init);
24530 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
24531 args = make_tree_vector_from_ctor (init);
24532 else
24533 args = make_tree_vector_single (init);
24534
24535 if (args->length() == 1)
24536 {
24537 /* First try to deduce directly, since we don't have implicitly-declared
24538 constructors yet. */
24539 tree parms = build_tree_list (NULL_TREE, type);
24540 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
24541 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
24542 int err = type_unification_real (tparms, targs, parms, &(*args)[0],
24543 1, /*subr*/false, DEDUCE_CALL,
24544 LOOKUP_NORMAL, NULL, /*explain*/false);
24545 if (err == 0)
24546 return tsubst (type, targs, complain, tmpl);
24547 }
24548
24549 tree dname = dguide_name (tmpl);
24550 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
24551 /*type*/false, /*complain*/false,
24552 /*hidden*/false);
24553 if (cands == error_mark_node)
24554 cands = NULL_TREE;
24555
24556 tree outer_args = NULL_TREE;
24557 if (DECL_CLASS_SCOPE_P (tmpl)
24558 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
24559 {
24560 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
24561 type = TREE_TYPE (most_general_template (tmpl));
24562 }
24563
24564 if (CLASSTYPE_METHOD_VEC (type))
24565 // FIXME cache artificial deduction guides
24566 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
24567 {
24568 tree fn = OVL_CURRENT (fns);
24569 tree guide = build_deduction_guide (fn, outer_args, complain);
24570 cands = ovl_cons (guide, cands);
24571 }
24572
24573 if (cands == NULL_TREE)
24574 {
24575 error ("cannot deduce template arguments for %qT, as it has "
24576 "no deduction guides or user-declared constructors", type);
24577 return error_mark_node;
24578 }
24579
24580 ++cp_unevaluated_operand;
24581 tree t = build_new_function_call (cands, &args, /*koenig*/false,
24582 complain|tf_decltype);
24583 --cp_unevaluated_operand;
24584 release_tree_vector (args);
24585
24586 return TREE_TYPE (t);
24587 }
24588
24589 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24590 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
24591
24592 tree
24593 do_auto_deduction (tree type, tree init, tree auto_node)
24594 {
24595 return do_auto_deduction (type, init, auto_node,
24596 tf_warning_or_error,
24597 adc_unspecified);
24598 }
24599
24600 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
24601 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
24602 The CONTEXT determines the context in which auto deduction is performed
24603 and is used to control error diagnostics.
24604
24605 For partial-concept-ids, extra args may be appended to the list of deduced
24606 template arguments prior to determining constraint satisfaction. */
24607
24608 tree
24609 do_auto_deduction (tree type, tree init, tree auto_node,
24610 tsubst_flags_t complain, auto_deduction_context context)
24611 {
24612 tree targs;
24613
24614 if (init == error_mark_node)
24615 return error_mark_node;
24616
24617 if (type_dependent_expression_p (init))
24618 /* Defining a subset of type-dependent expressions that we can deduce
24619 from ahead of time isn't worth the trouble. */
24620 return type;
24621
24622 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24623 /* C++17 class template argument deduction. */
24624 return do_class_deduction (tmpl, init, complain);
24625
24626 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
24627 with either a new invented type template parameter U or, if the
24628 initializer is a braced-init-list (8.5.4), with
24629 std::initializer_list<U>. */
24630 if (BRACE_ENCLOSED_INITIALIZER_P (init))
24631 {
24632 if (!DIRECT_LIST_INIT_P (init))
24633 type = listify_autos (type, auto_node);
24634 else if (CONSTRUCTOR_NELTS (init) == 1)
24635 init = CONSTRUCTOR_ELT (init, 0)->value;
24636 else
24637 {
24638 if (complain & tf_warning_or_error)
24639 {
24640 if (permerror (input_location, "direct-list-initialization of "
24641 "%<auto%> requires exactly one element"))
24642 inform (input_location,
24643 "for deduction to %<std::initializer_list%>, use copy-"
24644 "list-initialization (i.e. add %<=%> before the %<{%>)");
24645 }
24646 type = listify_autos (type, auto_node);
24647 }
24648 }
24649
24650 if (type == error_mark_node)
24651 return error_mark_node;
24652
24653 init = resolve_nondeduced_context (init, complain);
24654
24655 if (AUTO_IS_DECLTYPE (auto_node))
24656 {
24657 bool id = (DECL_P (init)
24658 || ((TREE_CODE (init) == COMPONENT_REF
24659 || TREE_CODE (init) == SCOPE_REF)
24660 && !REF_PARENTHESIZED_P (init)));
24661 targs = make_tree_vec (1);
24662 TREE_VEC_ELT (targs, 0)
24663 = finish_decltype_type (init, id, tf_warning_or_error);
24664 if (type != auto_node)
24665 {
24666 if (complain & tf_error)
24667 error ("%qT as type rather than plain %<decltype(auto)%>", type);
24668 return error_mark_node;
24669 }
24670 }
24671 else
24672 {
24673 tree parms = build_tree_list (NULL_TREE, type);
24674 tree tparms;
24675
24676 if (flag_concepts)
24677 tparms = extract_autos (type);
24678 else
24679 {
24680 tparms = make_tree_vec (1);
24681 TREE_VEC_ELT (tparms, 0)
24682 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
24683 }
24684
24685 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
24686 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
24687 DEDUCE_CALL, LOOKUP_NORMAL,
24688 NULL, /*explain_p=*/false);
24689 if (val > 0)
24690 {
24691 if (processing_template_decl)
24692 /* Try again at instantiation time. */
24693 return type;
24694 if (type && type != error_mark_node
24695 && (complain & tf_error))
24696 /* If type is error_mark_node a diagnostic must have been
24697 emitted by now. Also, having a mention to '<type error>'
24698 in the diagnostic is not really useful to the user. */
24699 {
24700 if (cfun && auto_node == current_function_auto_return_pattern
24701 && LAMBDA_FUNCTION_P (current_function_decl))
24702 error ("unable to deduce lambda return type from %qE", init);
24703 else
24704 error ("unable to deduce %qT from %qE", type, init);
24705 type_unification_real (tparms, targs, parms, &init, 1, 0,
24706 DEDUCE_CALL, LOOKUP_NORMAL,
24707 NULL, /*explain_p=*/true);
24708 }
24709 return error_mark_node;
24710 }
24711 }
24712
24713 /* Check any placeholder constraints against the deduced type. */
24714 if (flag_concepts && !processing_template_decl)
24715 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
24716 {
24717 /* Use the deduced type to check the associated constraints. If we
24718 have a partial-concept-id, rebuild the argument list so that
24719 we check using the extra arguments. */
24720 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
24721 tree cargs = CHECK_CONSTR_ARGS (constr);
24722 if (TREE_VEC_LENGTH (cargs) > 1)
24723 {
24724 cargs = copy_node (cargs);
24725 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
24726 }
24727 else
24728 cargs = targs;
24729 if (!constraints_satisfied_p (constr, cargs))
24730 {
24731 if (complain & tf_warning_or_error)
24732 {
24733 switch (context)
24734 {
24735 case adc_unspecified:
24736 error("placeholder constraints not satisfied");
24737 break;
24738 case adc_variable_type:
24739 error ("deduced initializer does not satisfy "
24740 "placeholder constraints");
24741 break;
24742 case adc_return_type:
24743 error ("deduced return type does not satisfy "
24744 "placeholder constraints");
24745 break;
24746 case adc_requirement:
24747 error ("deduced expression type does not saatisy "
24748 "placeholder constraints");
24749 break;
24750 }
24751 diagnose_constraints (input_location, constr, targs);
24752 }
24753 return error_mark_node;
24754 }
24755 }
24756
24757 if (processing_template_decl)
24758 targs = add_to_template_args (current_template_args (), targs);
24759 return tsubst (type, targs, complain, NULL_TREE);
24760 }
24761
24762 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
24763 result. */
24764
24765 tree
24766 splice_late_return_type (tree type, tree late_return_type)
24767 {
24768 if (is_auto (type))
24769 {
24770 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type))
24771 {
24772 if (!late_return_type)
24773 error ("deduction guide must have trailing return type");
24774 else if (CLASS_TYPE_P (late_return_type)
24775 && CLASSTYPE_TEMPLATE_INFO (late_return_type)
24776 && CLASSTYPE_TI_TEMPLATE (late_return_type) == tmpl)
24777 /* OK */;
24778 else
24779 error ("trailing return type %qT of deduction guide is not "
24780 "a specialization of %qT",
24781 late_return_type, TREE_TYPE (tmpl));
24782 }
24783
24784 if (late_return_type)
24785 return late_return_type;
24786
24787 tree idx = get_template_parm_index (type);
24788 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
24789 /* In an abbreviated function template we didn't know we were dealing
24790 with a function template when we saw the auto return type, so update
24791 it to have the correct level. */
24792 return make_auto_1 (TYPE_IDENTIFIER (type), true);
24793 }
24794 return type;
24795 }
24796
24797 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
24798 'decltype(auto)' or a deduced class template. */
24799
24800 bool
24801 is_auto (const_tree type)
24802 {
24803 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24804 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
24805 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")
24806 || CLASS_PLACEHOLDER_TEMPLATE (type)))
24807 return true;
24808 else
24809 return false;
24810 }
24811
24812 /* for_each_template_parm callback for type_uses_auto. */
24813
24814 int
24815 is_auto_r (tree tp, void */*data*/)
24816 {
24817 return is_auto_or_concept (tp);
24818 }
24819
24820 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
24821 a use of `auto'. Returns NULL_TREE otherwise. */
24822
24823 tree
24824 type_uses_auto (tree type)
24825 {
24826 if (type == NULL_TREE)
24827 return NULL_TREE;
24828 else if (flag_concepts)
24829 {
24830 /* The Concepts TS allows multiple autos in one type-specifier; just
24831 return the first one we find, do_auto_deduction will collect all of
24832 them. */
24833 if (uses_template_parms (type))
24834 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
24835 /*visited*/NULL, /*nondeduced*/true);
24836 else
24837 return NULL_TREE;
24838 }
24839 else
24840 return find_type_usage (type, is_auto);
24841 }
24842
24843 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
24844 'decltype(auto)' or a concept. */
24845
24846 bool
24847 is_auto_or_concept (const_tree type)
24848 {
24849 return is_auto (type); // or concept
24850 }
24851
24852 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
24853 a concept identifier) iff TYPE contains a use of a generic type. Returns
24854 NULL_TREE otherwise. */
24855
24856 tree
24857 type_uses_auto_or_concept (tree type)
24858 {
24859 return find_type_usage (type, is_auto_or_concept);
24860 }
24861
24862
24863 /* For a given template T, return the vector of typedefs referenced
24864 in T for which access check is needed at T instantiation time.
24865 T is either a FUNCTION_DECL or a RECORD_TYPE.
24866 Those typedefs were added to T by the function
24867 append_type_to_template_for_access_check. */
24868
24869 vec<qualified_typedef_usage_t, va_gc> *
24870 get_types_needing_access_check (tree t)
24871 {
24872 tree ti;
24873 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
24874
24875 if (!t || t == error_mark_node)
24876 return NULL;
24877
24878 if (!(ti = get_template_info (t)))
24879 return NULL;
24880
24881 if (CLASS_TYPE_P (t)
24882 || TREE_CODE (t) == FUNCTION_DECL)
24883 {
24884 if (!TI_TEMPLATE (ti))
24885 return NULL;
24886
24887 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
24888 }
24889
24890 return result;
24891 }
24892
24893 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
24894 tied to T. That list of typedefs will be access checked at
24895 T instantiation time.
24896 T is either a FUNCTION_DECL or a RECORD_TYPE.
24897 TYPE_DECL is a TYPE_DECL node representing a typedef.
24898 SCOPE is the scope through which TYPE_DECL is accessed.
24899 LOCATION is the location of the usage point of TYPE_DECL.
24900
24901 This function is a subroutine of
24902 append_type_to_template_for_access_check. */
24903
24904 static void
24905 append_type_to_template_for_access_check_1 (tree t,
24906 tree type_decl,
24907 tree scope,
24908 location_t location)
24909 {
24910 qualified_typedef_usage_t typedef_usage;
24911 tree ti;
24912
24913 if (!t || t == error_mark_node)
24914 return;
24915
24916 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
24917 || CLASS_TYPE_P (t))
24918 && type_decl
24919 && TREE_CODE (type_decl) == TYPE_DECL
24920 && scope);
24921
24922 if (!(ti = get_template_info (t)))
24923 return;
24924
24925 gcc_assert (TI_TEMPLATE (ti));
24926
24927 typedef_usage.typedef_decl = type_decl;
24928 typedef_usage.context = scope;
24929 typedef_usage.locus = location;
24930
24931 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
24932 }
24933
24934 /* Append TYPE_DECL to the template TEMPL.
24935 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
24936 At TEMPL instanciation time, TYPE_DECL will be checked to see
24937 if it can be accessed through SCOPE.
24938 LOCATION is the location of the usage point of TYPE_DECL.
24939
24940 e.g. consider the following code snippet:
24941
24942 class C
24943 {
24944 typedef int myint;
24945 };
24946
24947 template<class U> struct S
24948 {
24949 C::myint mi; // <-- usage point of the typedef C::myint
24950 };
24951
24952 S<char> s;
24953
24954 At S<char> instantiation time, we need to check the access of C::myint
24955 In other words, we need to check the access of the myint typedef through
24956 the C scope. For that purpose, this function will add the myint typedef
24957 and the scope C through which its being accessed to a list of typedefs
24958 tied to the template S. That list will be walked at template instantiation
24959 time and access check performed on each typedefs it contains.
24960 Note that this particular code snippet should yield an error because
24961 myint is private to C. */
24962
24963 void
24964 append_type_to_template_for_access_check (tree templ,
24965 tree type_decl,
24966 tree scope,
24967 location_t location)
24968 {
24969 qualified_typedef_usage_t *iter;
24970 unsigned i;
24971
24972 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
24973
24974 /* Make sure we don't append the type to the template twice. */
24975 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
24976 if (iter->typedef_decl == type_decl && scope == iter->context)
24977 return;
24978
24979 append_type_to_template_for_access_check_1 (templ, type_decl,
24980 scope, location);
24981 }
24982
24983 /* Convert the generic type parameters in PARM that match the types given in the
24984 range [START_IDX, END_IDX) from the current_template_parms into generic type
24985 packs. */
24986
24987 tree
24988 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
24989 {
24990 tree current = current_template_parms;
24991 int depth = TMPL_PARMS_DEPTH (current);
24992 current = INNERMOST_TEMPLATE_PARMS (current);
24993 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
24994
24995 for (int i = 0; i < start_idx; ++i)
24996 TREE_VEC_ELT (replacement, i)
24997 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
24998
24999 for (int i = start_idx; i < end_idx; ++i)
25000 {
25001 /* Create a distinct parameter pack type from the current parm and add it
25002 to the replacement args to tsubst below into the generic function
25003 parameter. */
25004
25005 tree o = TREE_TYPE (TREE_VALUE
25006 (TREE_VEC_ELT (current, i)));
25007 tree t = copy_type (o);
25008 TEMPLATE_TYPE_PARM_INDEX (t)
25009 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25010 o, 0, 0, tf_none);
25011 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25012 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25013 TYPE_MAIN_VARIANT (t) = t;
25014 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25015 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25016 TREE_VEC_ELT (replacement, i) = t;
25017 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25018 }
25019
25020 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25021 TREE_VEC_ELT (replacement, i)
25022 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25023
25024 /* If there are more levels then build up the replacement with the outer
25025 template parms. */
25026 if (depth > 1)
25027 replacement = add_to_template_args (template_parms_to_args
25028 (TREE_CHAIN (current_template_parms)),
25029 replacement);
25030
25031 return tsubst (parm, replacement, tf_none, NULL_TREE);
25032 }
25033
25034 /* Entries in the decl_constraint hash table. */
25035 struct GTY((for_user)) constr_entry
25036 {
25037 tree decl;
25038 tree ci;
25039 };
25040
25041 /* Hashing function and equality for constraint entries. */
25042 struct constr_hasher : ggc_ptr_hash<constr_entry>
25043 {
25044 static hashval_t hash (constr_entry *e)
25045 {
25046 return (hashval_t)DECL_UID (e->decl);
25047 }
25048
25049 static bool equal (constr_entry *e1, constr_entry *e2)
25050 {
25051 return e1->decl == e2->decl;
25052 }
25053 };
25054
25055 /* A mapping from declarations to constraint information. Note that
25056 both templates and their underlying declarations are mapped to the
25057 same constraint information.
25058
25059 FIXME: This is defined in pt.c because garbage collection
25060 code is not being generated for constraint.cc. */
25061
25062 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25063
25064 /* Returns the template constraints of declaration T. If T is not
25065 constrained, return NULL_TREE. Note that T must be non-null. */
25066
25067 tree
25068 get_constraints (tree t)
25069 {
25070 if (!flag_concepts)
25071 return NULL_TREE;
25072
25073 gcc_assert (DECL_P (t));
25074 if (TREE_CODE (t) == TEMPLATE_DECL)
25075 t = DECL_TEMPLATE_RESULT (t);
25076 constr_entry elt = { t, NULL_TREE };
25077 constr_entry* found = decl_constraints->find (&elt);
25078 if (found)
25079 return found->ci;
25080 else
25081 return NULL_TREE;
25082 }
25083
25084 /* Associate the given constraint information CI with the declaration
25085 T. If T is a template, then the constraints are associated with
25086 its underlying declaration. Don't build associations if CI is
25087 NULL_TREE. */
25088
25089 void
25090 set_constraints (tree t, tree ci)
25091 {
25092 if (!ci)
25093 return;
25094 gcc_assert (t && flag_concepts);
25095 if (TREE_CODE (t) == TEMPLATE_DECL)
25096 t = DECL_TEMPLATE_RESULT (t);
25097 gcc_assert (!get_constraints (t));
25098 constr_entry elt = {t, ci};
25099 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25100 constr_entry* entry = ggc_alloc<constr_entry> ();
25101 *entry = elt;
25102 *slot = entry;
25103 }
25104
25105 /* Remove the associated constraints of the declaration T. */
25106
25107 void
25108 remove_constraints (tree t)
25109 {
25110 gcc_assert (DECL_P (t));
25111 if (TREE_CODE (t) == TEMPLATE_DECL)
25112 t = DECL_TEMPLATE_RESULT (t);
25113
25114 constr_entry elt = {t, NULL_TREE};
25115 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25116 if (slot)
25117 decl_constraints->clear_slot (slot);
25118 }
25119
25120 /* Memoized satisfaction results for declarations. This
25121 maps the pair (constraint_info, arguments) to the result computed
25122 by constraints_satisfied_p. */
25123
25124 struct GTY((for_user)) constraint_sat_entry
25125 {
25126 tree ci;
25127 tree args;
25128 tree result;
25129 };
25130
25131 /* Hashing function and equality for constraint entries. */
25132
25133 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25134 {
25135 static hashval_t hash (constraint_sat_entry *e)
25136 {
25137 hashval_t val = iterative_hash_object(e->ci, 0);
25138 return iterative_hash_template_arg (e->args, val);
25139 }
25140
25141 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25142 {
25143 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25144 }
25145 };
25146
25147 /* Memoized satisfaction results for concept checks. */
25148
25149 struct GTY((for_user)) concept_spec_entry
25150 {
25151 tree tmpl;
25152 tree args;
25153 tree result;
25154 };
25155
25156 /* Hashing function and equality for constraint entries. */
25157
25158 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25159 {
25160 static hashval_t hash (concept_spec_entry *e)
25161 {
25162 return hash_tmpl_and_args (e->tmpl, e->args);
25163 }
25164
25165 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25166 {
25167 ++comparing_specializations;
25168 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25169 --comparing_specializations;
25170 return eq;
25171 }
25172 };
25173
25174 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25175 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25176
25177 /* Search for a memoized satisfaction result. Returns one of the
25178 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25179
25180 tree
25181 lookup_constraint_satisfaction (tree ci, tree args)
25182 {
25183 constraint_sat_entry elt = { ci, args, NULL_TREE };
25184 constraint_sat_entry* found = constraint_memos->find (&elt);
25185 if (found)
25186 return found->result;
25187 else
25188 return NULL_TREE;
25189 }
25190
25191 /* Memoize the result of a satisfication test. Returns the saved result. */
25192
25193 tree
25194 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25195 {
25196 constraint_sat_entry elt = {ci, args, result};
25197 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25198 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25199 *entry = elt;
25200 *slot = entry;
25201 return result;
25202 }
25203
25204 /* Search for a memoized satisfaction result for a concept. */
25205
25206 tree
25207 lookup_concept_satisfaction (tree tmpl, tree args)
25208 {
25209 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25210 concept_spec_entry* found = concept_memos->find (&elt);
25211 if (found)
25212 return found->result;
25213 else
25214 return NULL_TREE;
25215 }
25216
25217 /* Memoize the result of a concept check. Returns the saved result. */
25218
25219 tree
25220 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25221 {
25222 concept_spec_entry elt = {tmpl, args, result};
25223 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25224 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25225 *entry = elt;
25226 *slot = entry;
25227 return result;
25228 }
25229
25230 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25231
25232 /* Returns a prior concept specialization. This returns the substituted
25233 and normalized constraints defined by the concept. */
25234
25235 tree
25236 get_concept_expansion (tree tmpl, tree args)
25237 {
25238 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25239 concept_spec_entry* found = concept_expansions->find (&elt);
25240 if (found)
25241 return found->result;
25242 else
25243 return NULL_TREE;
25244 }
25245
25246 /* Save a concept expansion for later. */
25247
25248 tree
25249 save_concept_expansion (tree tmpl, tree args, tree def)
25250 {
25251 concept_spec_entry elt = {tmpl, args, def};
25252 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25253 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25254 *entry = elt;
25255 *slot = entry;
25256 return def;
25257 }
25258
25259 static hashval_t
25260 hash_subsumption_args (tree t1, tree t2)
25261 {
25262 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25263 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25264 int val = 0;
25265 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25266 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25267 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25268 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25269 return val;
25270 }
25271
25272 /* Compare the constraints of two subsumption entries. The LEFT1 and
25273 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25274 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25275
25276 static bool
25277 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25278 {
25279 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25280 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25281 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25282 CHECK_CONSTR_ARGS (right1)))
25283 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25284 CHECK_CONSTR_ARGS (right2));
25285 return false;
25286 }
25287
25288 /* Key/value pair for learning and memoizing subsumption results. This
25289 associates a pair of check constraints (including arguments) with
25290 a boolean value indicating the result. */
25291
25292 struct GTY((for_user)) subsumption_entry
25293 {
25294 tree t1;
25295 tree t2;
25296 bool result;
25297 };
25298
25299 /* Hashing function and equality for constraint entries. */
25300
25301 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
25302 {
25303 static hashval_t hash (subsumption_entry *e)
25304 {
25305 return hash_subsumption_args (e->t1, e->t2);
25306 }
25307
25308 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
25309 {
25310 ++comparing_specializations;
25311 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
25312 --comparing_specializations;
25313 return eq;
25314 }
25315 };
25316
25317 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
25318
25319 /* Search for a previously cached subsumption result. */
25320
25321 bool*
25322 lookup_subsumption_result (tree t1, tree t2)
25323 {
25324 subsumption_entry elt = { t1, t2, false };
25325 subsumption_entry* found = subsumption_table->find (&elt);
25326 if (found)
25327 return &found->result;
25328 else
25329 return 0;
25330 }
25331
25332 /* Save a subsumption result. */
25333
25334 bool
25335 save_subsumption_result (tree t1, tree t2, bool result)
25336 {
25337 subsumption_entry elt = {t1, t2, result};
25338 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
25339 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
25340 *entry = elt;
25341 *slot = entry;
25342 return result;
25343 }
25344
25345 /* Set up the hash table for constraint association. */
25346
25347 void
25348 init_constraint_processing (void)
25349 {
25350 if (!flag_concepts)
25351 return;
25352
25353 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
25354 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
25355 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
25356 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
25357 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
25358 }
25359
25360 /* Set up the hash tables for template instantiations. */
25361
25362 void
25363 init_template_processing (void)
25364 {
25365 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
25366 type_specializations = hash_table<spec_hasher>::create_ggc (37);
25367 }
25368
25369 /* Print stats about the template hash tables for -fstats. */
25370
25371 void
25372 print_template_statistics (void)
25373 {
25374 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
25375 "%f collisions\n", (long) decl_specializations->size (),
25376 (long) decl_specializations->elements (),
25377 decl_specializations->collisions ());
25378 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
25379 "%f collisions\n", (long) type_specializations->size (),
25380 (long) type_specializations->elements (),
25381 type_specializations->collisions ());
25382 }
25383
25384 #include "gt-cp-pt.h"
This page took 1.272214 seconds and 5 git commands to generate.