pt.c (tsubst_decl): SET_DECL_IMPLICIT_INSTANTIATION before register_specialization.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "double-int.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "stringpool.h"
42 #include "varasm.h"
43 #include "attribs.h"
44 #include "stor-layout.h"
45 #include "intl.h"
46 #include "flags.h"
47 #include "cp-tree.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "cp-objcp-common.h"
51 #include "tree-inline.h"
52 #include "decl.h"
53 #include "toplev.h"
54 #include "timevar.h"
55 #include "tree-iterator.h"
56 #include "type-utils.h"
57 #include "gimplify.h"
58
59 /* The type of functions taking a tree, and some additional data, and
60 returning an int. */
61 typedef int (*tree_fn_t) (tree, void*);
62
63 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
64 instantiations have been deferred, either because their definitions
65 were not yet available, or because we were putting off doing the work. */
66 struct GTY ((chain_next ("%h.next"))) pending_template {
67 struct pending_template *next;
68 struct tinst_level *tinst;
69 };
70
71 static GTY(()) struct pending_template *pending_templates;
72 static GTY(()) struct pending_template *last_pending_template;
73
74 int processing_template_parmlist;
75 static int template_header_count;
76
77 static GTY(()) tree saved_trees;
78 static vec<int> inline_parm_levels;
79
80 static GTY(()) struct tinst_level *current_tinst_level;
81
82 static GTY(()) tree saved_access_scope;
83
84 /* Live only within one (recursive) call to tsubst_expr. We use
85 this to pass the statement expression node from the STMT_EXPR
86 to the EXPR_STMT that is its result. */
87 static tree cur_stmt_expr;
88
89 /* True if we've recursed into fn_type_unification too many times. */
90 static bool excessive_deduction_depth;
91
92 struct GTY((for_user)) spec_entry
93 {
94 tree tmpl;
95 tree args;
96 tree spec;
97 };
98
99 struct spec_hasher : ggc_hasher<spec_entry *>
100 {
101 static hashval_t hash (spec_entry *);
102 static bool equal (spec_entry *, spec_entry *);
103 };
104
105 static GTY (()) hash_table<spec_hasher> *decl_specializations;
106
107 static GTY (()) hash_table<spec_hasher> *type_specializations;
108
109 /* Contains canonical template parameter types. The vector is indexed by
110 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
111 TREE_LIST, whose TREE_VALUEs contain the canonical template
112 parameters of various types and levels. */
113 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
114
115 #define UNIFY_ALLOW_NONE 0
116 #define UNIFY_ALLOW_MORE_CV_QUAL 1
117 #define UNIFY_ALLOW_LESS_CV_QUAL 2
118 #define UNIFY_ALLOW_DERIVED 4
119 #define UNIFY_ALLOW_INTEGER 8
120 #define UNIFY_ALLOW_OUTER_LEVEL 16
121 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
122 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
123
124 enum template_base_result {
125 tbr_incomplete_type,
126 tbr_ambiguous_baseclass,
127 tbr_success
128 };
129
130 static void push_access_scope (tree);
131 static void pop_access_scope (tree);
132 static bool resolve_overloaded_unification (tree, tree, tree, tree,
133 unification_kind_t, int,
134 bool);
135 static int try_one_overload (tree, tree, tree, tree, tree,
136 unification_kind_t, int, bool, bool);
137 static int unify (tree, tree, tree, tree, int, bool);
138 static void add_pending_template (tree);
139 static tree reopen_tinst_level (struct tinst_level *);
140 static tree tsubst_initializer_list (tree, tree);
141 static tree get_partial_spec_bindings (tree, tree, tree, tree);
142 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
143 bool, bool);
144 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146 static void tsubst_enum (tree, tree, tree);
147 static tree add_to_template_args (tree, tree);
148 static tree add_outermost_template_args (tree, tree);
149 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
150 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
151 tree);
152 static int type_unification_real (tree, tree, tree, const tree *,
153 unsigned int, int, unification_kind_t, int,
154 vec<deferred_access_check, va_gc> **,
155 bool);
156 static void note_template_header (int);
157 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
158 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
159 static tree convert_template_argument (tree, tree, tree,
160 tsubst_flags_t, int, tree);
161 static int for_each_template_parm (tree, tree_fn_t, void*,
162 hash_set<tree> *, bool);
163 static tree expand_template_argument_pack (tree);
164 static tree build_template_parm_index (int, int, int, tree, tree);
165 static bool inline_needs_template_parms (tree, bool);
166 static void push_inline_template_parms_recursive (tree, int);
167 static tree retrieve_local_specialization (tree);
168 static void register_local_specialization (tree, tree);
169 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170 static int mark_template_parm (tree, void *);
171 static int template_parm_this_level_p (tree, void *);
172 static tree tsubst_friend_function (tree, tree);
173 static tree tsubst_friend_class (tree, tree);
174 static int can_complete_type_without_circularity (tree);
175 static tree get_bindings (tree, tree, tree, bool);
176 static int template_decl_level (tree);
177 static int check_cv_quals_for_unify (int, tree, tree);
178 static void template_parm_level_and_index (tree, int*, int*);
179 static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
182 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
184 static void regenerate_decl_from_template (tree, tree);
185 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189 static bool check_specialization_scope (void);
190 static tree process_partial_specialization (tree);
191 static void set_current_access_from_decl (tree);
192 static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194 static tree try_class_unification (tree, tree, tree, tree, bool);
195 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197 static bool template_template_parm_bindings_ok_p (tree, tree);
198 static int template_args_equal (tree, tree);
199 static void tsubst_default_arguments (tree, tsubst_flags_t);
200 static tree for_each_template_parm_r (tree *, int *, void *);
201 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
202 static void copy_default_args_to_explicit_spec (tree);
203 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
204 static bool dependent_template_arg_p (tree);
205 static bool any_template_arguments_need_structural_equality_p (tree);
206 static bool dependent_type_p_r (tree);
207 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
208 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
209 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
210 static tree tsubst_decl (tree, tree, tsubst_flags_t);
211 static void perform_typedefs_access_check (tree tmpl, tree targs);
212 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
213 location_t);
214 static tree listify (tree);
215 static tree listify_autos (tree, tree);
216 static tree template_parm_to_arg (tree t);
217 static tree current_template_args (void);
218 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
219 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
220
221 /* Make the current scope suitable for access checking when we are
222 processing T. T can be FUNCTION_DECL for instantiated function
223 template, VAR_DECL for static member variable, or TYPE_DECL for
224 alias template (needed by instantiate_decl). */
225
226 static void
227 push_access_scope (tree t)
228 {
229 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
230 || TREE_CODE (t) == TYPE_DECL);
231
232 if (DECL_FRIEND_CONTEXT (t))
233 push_nested_class (DECL_FRIEND_CONTEXT (t));
234 else if (DECL_CLASS_SCOPE_P (t))
235 push_nested_class (DECL_CONTEXT (t));
236 else
237 push_to_top_level ();
238
239 if (TREE_CODE (t) == FUNCTION_DECL)
240 {
241 saved_access_scope = tree_cons
242 (NULL_TREE, current_function_decl, saved_access_scope);
243 current_function_decl = t;
244 }
245 }
246
247 /* Restore the scope set up by push_access_scope. T is the node we
248 are processing. */
249
250 static void
251 pop_access_scope (tree t)
252 {
253 if (TREE_CODE (t) == FUNCTION_DECL)
254 {
255 current_function_decl = TREE_VALUE (saved_access_scope);
256 saved_access_scope = TREE_CHAIN (saved_access_scope);
257 }
258
259 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
260 pop_nested_class ();
261 else
262 pop_from_top_level ();
263 }
264
265 /* Do any processing required when DECL (a member template
266 declaration) is finished. Returns the TEMPLATE_DECL corresponding
267 to DECL, unless it is a specialization, in which case the DECL
268 itself is returned. */
269
270 tree
271 finish_member_template_decl (tree decl)
272 {
273 if (decl == error_mark_node)
274 return error_mark_node;
275
276 gcc_assert (DECL_P (decl));
277
278 if (TREE_CODE (decl) == TYPE_DECL)
279 {
280 tree type;
281
282 type = TREE_TYPE (decl);
283 if (type == error_mark_node)
284 return error_mark_node;
285 if (MAYBE_CLASS_TYPE_P (type)
286 && CLASSTYPE_TEMPLATE_INFO (type)
287 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
288 {
289 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
290 check_member_template (tmpl);
291 return tmpl;
292 }
293 return NULL_TREE;
294 }
295 else if (TREE_CODE (decl) == FIELD_DECL)
296 error ("data member %qD cannot be a member template", decl);
297 else if (DECL_TEMPLATE_INFO (decl))
298 {
299 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
300 {
301 check_member_template (DECL_TI_TEMPLATE (decl));
302 return DECL_TI_TEMPLATE (decl);
303 }
304 else
305 return decl;
306 }
307 else
308 error ("invalid member template declaration %qD", decl);
309
310 return error_mark_node;
311 }
312
313 /* Create a template info node. */
314
315 tree
316 build_template_info (tree template_decl, tree template_args)
317 {
318 tree result = make_node (TEMPLATE_INFO);
319 TI_TEMPLATE (result) = template_decl;
320 TI_ARGS (result) = template_args;
321 return result;
322 }
323
324 /* Return the template info node corresponding to T, whatever T is. */
325
326 tree
327 get_template_info (const_tree t)
328 {
329 tree tinfo = NULL_TREE;
330
331 if (!t || t == error_mark_node)
332 return NULL;
333
334 if (TREE_CODE (t) == NAMESPACE_DECL)
335 return NULL;
336
337 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
338 tinfo = DECL_TEMPLATE_INFO (t);
339
340 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
341 t = TREE_TYPE (t);
342
343 if (OVERLOAD_TYPE_P (t))
344 tinfo = TYPE_TEMPLATE_INFO (t);
345 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
346 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
347
348 return tinfo;
349 }
350
351 /* Returns the template nesting level of the indicated class TYPE.
352
353 For example, in:
354 template <class T>
355 struct A
356 {
357 template <class U>
358 struct B {};
359 };
360
361 A<T>::B<U> has depth two, while A<T> has depth one.
362 Both A<T>::B<int> and A<int>::B<U> have depth one, if
363 they are instantiations, not specializations.
364
365 This function is guaranteed to return 0 if passed NULL_TREE so
366 that, for example, `template_class_depth (current_class_type)' is
367 always safe. */
368
369 int
370 template_class_depth (tree type)
371 {
372 int depth;
373
374 for (depth = 0;
375 type && TREE_CODE (type) != NAMESPACE_DECL;
376 type = (TREE_CODE (type) == FUNCTION_DECL)
377 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
378 {
379 tree tinfo = get_template_info (type);
380
381 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
382 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
383 ++depth;
384 }
385
386 return depth;
387 }
388
389 /* Subroutine of maybe_begin_member_template_processing.
390 Returns true if processing DECL needs us to push template parms. */
391
392 static bool
393 inline_needs_template_parms (tree decl, bool nsdmi)
394 {
395 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
396 return false;
397
398 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
399 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
400 }
401
402 /* Subroutine of maybe_begin_member_template_processing.
403 Push the template parms in PARMS, starting from LEVELS steps into the
404 chain, and ending at the beginning, since template parms are listed
405 innermost first. */
406
407 static void
408 push_inline_template_parms_recursive (tree parmlist, int levels)
409 {
410 tree parms = TREE_VALUE (parmlist);
411 int i;
412
413 if (levels > 1)
414 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
415
416 ++processing_template_decl;
417 current_template_parms
418 = tree_cons (size_int (processing_template_decl),
419 parms, current_template_parms);
420 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
421
422 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
423 NULL);
424 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
425 {
426 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
427
428 if (error_operand_p (parm))
429 continue;
430
431 gcc_assert (DECL_P (parm));
432
433 switch (TREE_CODE (parm))
434 {
435 case TYPE_DECL:
436 case TEMPLATE_DECL:
437 pushdecl (parm);
438 break;
439
440 case PARM_DECL:
441 {
442 /* Make a CONST_DECL as is done in process_template_parm.
443 It is ugly that we recreate this here; the original
444 version built in process_template_parm is no longer
445 available. */
446 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
447 CONST_DECL, DECL_NAME (parm),
448 TREE_TYPE (parm));
449 DECL_ARTIFICIAL (decl) = 1;
450 TREE_CONSTANT (decl) = 1;
451 TREE_READONLY (decl) = 1;
452 DECL_INITIAL (decl) = DECL_INITIAL (parm);
453 SET_DECL_TEMPLATE_PARM_P (decl);
454 pushdecl (decl);
455 }
456 break;
457
458 default:
459 gcc_unreachable ();
460 }
461 }
462 }
463
464 /* Restore the template parameter context for a member template, a
465 friend template defined in a class definition, or a non-template
466 member of template class. */
467
468 void
469 maybe_begin_member_template_processing (tree decl)
470 {
471 tree parms;
472 int levels = 0;
473 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
474
475 if (nsdmi)
476 {
477 tree ctx = DECL_CONTEXT (decl);
478 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
479 /* Disregard full specializations (c++/60999). */
480 && uses_template_parms (ctx)
481 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
482 }
483
484 if (inline_needs_template_parms (decl, nsdmi))
485 {
486 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
487 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
488
489 if (DECL_TEMPLATE_SPECIALIZATION (decl))
490 {
491 --levels;
492 parms = TREE_CHAIN (parms);
493 }
494
495 push_inline_template_parms_recursive (parms, levels);
496 }
497
498 /* Remember how many levels of template parameters we pushed so that
499 we can pop them later. */
500 inline_parm_levels.safe_push (levels);
501 }
502
503 /* Undo the effects of maybe_begin_member_template_processing. */
504
505 void
506 maybe_end_member_template_processing (void)
507 {
508 int i;
509 int last;
510
511 if (inline_parm_levels.length () == 0)
512 return;
513
514 last = inline_parm_levels.pop ();
515 for (i = 0; i < last; ++i)
516 {
517 --processing_template_decl;
518 current_template_parms = TREE_CHAIN (current_template_parms);
519 poplevel (0, 0, 0);
520 }
521 }
522
523 /* Return a new template argument vector which contains all of ARGS,
524 but has as its innermost set of arguments the EXTRA_ARGS. */
525
526 static tree
527 add_to_template_args (tree args, tree extra_args)
528 {
529 tree new_args;
530 int extra_depth;
531 int i;
532 int j;
533
534 if (args == NULL_TREE || extra_args == error_mark_node)
535 return extra_args;
536
537 extra_depth = TMPL_ARGS_DEPTH (extra_args);
538 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
539
540 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
541 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
542
543 for (j = 1; j <= extra_depth; ++j, ++i)
544 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
545
546 return new_args;
547 }
548
549 /* Like add_to_template_args, but only the outermost ARGS are added to
550 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
551 (EXTRA_ARGS) levels are added. This function is used to combine
552 the template arguments from a partial instantiation with the
553 template arguments used to attain the full instantiation from the
554 partial instantiation. */
555
556 static tree
557 add_outermost_template_args (tree args, tree extra_args)
558 {
559 tree new_args;
560
561 /* If there are more levels of EXTRA_ARGS than there are ARGS,
562 something very fishy is going on. */
563 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
564
565 /* If *all* the new arguments will be the EXTRA_ARGS, just return
566 them. */
567 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
568 return extra_args;
569
570 /* For the moment, we make ARGS look like it contains fewer levels. */
571 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
572
573 new_args = add_to_template_args (args, extra_args);
574
575 /* Now, we restore ARGS to its full dimensions. */
576 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
577
578 return new_args;
579 }
580
581 /* Return the N levels of innermost template arguments from the ARGS. */
582
583 tree
584 get_innermost_template_args (tree args, int n)
585 {
586 tree new_args;
587 int extra_levels;
588 int i;
589
590 gcc_assert (n >= 0);
591
592 /* If N is 1, just return the innermost set of template arguments. */
593 if (n == 1)
594 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
595
596 /* If we're not removing anything, just return the arguments we were
597 given. */
598 extra_levels = TMPL_ARGS_DEPTH (args) - n;
599 gcc_assert (extra_levels >= 0);
600 if (extra_levels == 0)
601 return args;
602
603 /* Make a new set of arguments, not containing the outer arguments. */
604 new_args = make_tree_vec (n);
605 for (i = 1; i <= n; ++i)
606 SET_TMPL_ARGS_LEVEL (new_args, i,
607 TMPL_ARGS_LEVEL (args, i + extra_levels));
608
609 return new_args;
610 }
611
612 /* The inverse of get_innermost_template_args: Return all but the innermost
613 EXTRA_LEVELS levels of template arguments from the ARGS. */
614
615 static tree
616 strip_innermost_template_args (tree args, int extra_levels)
617 {
618 tree new_args;
619 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
620 int i;
621
622 gcc_assert (n >= 0);
623
624 /* If N is 1, just return the outermost set of template arguments. */
625 if (n == 1)
626 return TMPL_ARGS_LEVEL (args, 1);
627
628 /* If we're not removing anything, just return the arguments we were
629 given. */
630 gcc_assert (extra_levels >= 0);
631 if (extra_levels == 0)
632 return args;
633
634 /* Make a new set of arguments, not containing the inner arguments. */
635 new_args = make_tree_vec (n);
636 for (i = 1; i <= n; ++i)
637 SET_TMPL_ARGS_LEVEL (new_args, i,
638 TMPL_ARGS_LEVEL (args, i));
639
640 return new_args;
641 }
642
643 /* We've got a template header coming up; push to a new level for storing
644 the parms. */
645
646 void
647 begin_template_parm_list (void)
648 {
649 /* We use a non-tag-transparent scope here, which causes pushtag to
650 put tags in this scope, rather than in the enclosing class or
651 namespace scope. This is the right thing, since we want
652 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
653 global template class, push_template_decl handles putting the
654 TEMPLATE_DECL into top-level scope. For a nested template class,
655 e.g.:
656
657 template <class T> struct S1 {
658 template <class T> struct S2 {};
659 };
660
661 pushtag contains special code to call pushdecl_with_scope on the
662 TEMPLATE_DECL for S2. */
663 begin_scope (sk_template_parms, NULL);
664 ++processing_template_decl;
665 ++processing_template_parmlist;
666 note_template_header (0);
667 }
668
669 /* This routine is called when a specialization is declared. If it is
670 invalid to declare a specialization here, an error is reported and
671 false is returned, otherwise this routine will return true. */
672
673 static bool
674 check_specialization_scope (void)
675 {
676 tree scope = current_scope ();
677
678 /* [temp.expl.spec]
679
680 An explicit specialization shall be declared in the namespace of
681 which the template is a member, or, for member templates, in the
682 namespace of which the enclosing class or enclosing class
683 template is a member. An explicit specialization of a member
684 function, member class or static data member of a class template
685 shall be declared in the namespace of which the class template
686 is a member. */
687 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
688 {
689 error ("explicit specialization in non-namespace scope %qD", scope);
690 return false;
691 }
692
693 /* [temp.expl.spec]
694
695 In an explicit specialization declaration for a member of a class
696 template or a member template that appears in namespace scope,
697 the member template and some of its enclosing class templates may
698 remain unspecialized, except that the declaration shall not
699 explicitly specialize a class member template if its enclosing
700 class templates are not explicitly specialized as well. */
701 if (current_template_parms)
702 {
703 error ("enclosing class templates are not explicitly specialized");
704 return false;
705 }
706
707 return true;
708 }
709
710 /* We've just seen template <>. */
711
712 bool
713 begin_specialization (void)
714 {
715 begin_scope (sk_template_spec, NULL);
716 note_template_header (1);
717 return check_specialization_scope ();
718 }
719
720 /* Called at then end of processing a declaration preceded by
721 template<>. */
722
723 void
724 end_specialization (void)
725 {
726 finish_scope ();
727 reset_specialization ();
728 }
729
730 /* Any template <>'s that we have seen thus far are not referring to a
731 function specialization. */
732
733 void
734 reset_specialization (void)
735 {
736 processing_specialization = 0;
737 template_header_count = 0;
738 }
739
740 /* We've just seen a template header. If SPECIALIZATION is nonzero,
741 it was of the form template <>. */
742
743 static void
744 note_template_header (int specialization)
745 {
746 processing_specialization = specialization;
747 template_header_count++;
748 }
749
750 /* We're beginning an explicit instantiation. */
751
752 void
753 begin_explicit_instantiation (void)
754 {
755 gcc_assert (!processing_explicit_instantiation);
756 processing_explicit_instantiation = true;
757 }
758
759
760 void
761 end_explicit_instantiation (void)
762 {
763 gcc_assert (processing_explicit_instantiation);
764 processing_explicit_instantiation = false;
765 }
766
767 /* An explicit specialization or partial specialization of TMPL is being
768 declared. Check that the namespace in which the specialization is
769 occurring is permissible. Returns false iff it is invalid to
770 specialize TMPL in the current namespace. */
771
772 static bool
773 check_specialization_namespace (tree tmpl)
774 {
775 tree tpl_ns = decl_namespace_context (tmpl);
776
777 /* [tmpl.expl.spec]
778
779 An explicit specialization shall be declared in the namespace of
780 which the template is a member, or, for member templates, in the
781 namespace of which the enclosing class or enclosing class
782 template is a member. An explicit specialization of a member
783 function, member class or static data member of a class template
784 shall be declared in the namespace of which the class template is
785 a member. */
786 if (current_scope() != DECL_CONTEXT (tmpl)
787 && !at_namespace_scope_p ())
788 {
789 error ("specialization of %qD must appear at namespace scope", tmpl);
790 return false;
791 }
792 if (is_associated_namespace (current_namespace, tpl_ns))
793 /* Same or super-using namespace. */
794 return true;
795 else
796 {
797 permerror (input_location, "specialization of %qD in different namespace", tmpl);
798 permerror (input_location, " from definition of %q+#D", tmpl);
799 return false;
800 }
801 }
802
803 /* SPEC is an explicit instantiation. Check that it is valid to
804 perform this explicit instantiation in the current namespace. */
805
806 static void
807 check_explicit_instantiation_namespace (tree spec)
808 {
809 tree ns;
810
811 /* DR 275: An explicit instantiation shall appear in an enclosing
812 namespace of its template. */
813 ns = decl_namespace_context (spec);
814 if (!is_ancestor (current_namespace, ns))
815 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
816 "(which does not enclose namespace %qD)",
817 spec, current_namespace, ns);
818 }
819
820 /* The TYPE is being declared. If it is a template type, that means it
821 is a partial specialization. Do appropriate error-checking. */
822
823 tree
824 maybe_process_partial_specialization (tree type)
825 {
826 tree context;
827
828 if (type == error_mark_node)
829 return error_mark_node;
830
831 /* A lambda that appears in specialization context is not itself a
832 specialization. */
833 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
834 return type;
835
836 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
837 {
838 error ("name of class shadows template template parameter %qD",
839 TYPE_NAME (type));
840 return error_mark_node;
841 }
842
843 context = TYPE_CONTEXT (type);
844
845 if (TYPE_ALIAS_P (type))
846 {
847 if (TYPE_TEMPLATE_INFO (type)
848 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
849 error ("specialization of alias template %qD",
850 TYPE_TI_TEMPLATE (type));
851 else
852 error ("explicit specialization of non-template %qT", type);
853 return error_mark_node;
854 }
855 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
856 {
857 /* This is for ordinary explicit specialization and partial
858 specialization of a template class such as:
859
860 template <> class C<int>;
861
862 or:
863
864 template <class T> class C<T*>;
865
866 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
867
868 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
869 && !COMPLETE_TYPE_P (type))
870 {
871 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
872 && !at_namespace_scope_p ())
873 return error_mark_node;
874 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
875 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
876 if (processing_template_decl)
877 {
878 if (push_template_decl (TYPE_MAIN_DECL (type))
879 == error_mark_node)
880 return error_mark_node;
881 }
882 }
883 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
884 error ("specialization of %qT after instantiation", type);
885 else if (errorcount && !processing_specialization
886 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
887 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
888 /* Trying to define a specialization either without a template<> header
889 or in an inappropriate place. We've already given an error, so just
890 bail now so we don't actually define the specialization. */
891 return error_mark_node;
892 }
893 else if (CLASS_TYPE_P (type)
894 && !CLASSTYPE_USE_TEMPLATE (type)
895 && CLASSTYPE_TEMPLATE_INFO (type)
896 && context && CLASS_TYPE_P (context)
897 && CLASSTYPE_TEMPLATE_INFO (context))
898 {
899 /* This is for an explicit specialization of member class
900 template according to [temp.expl.spec/18]:
901
902 template <> template <class U> class C<int>::D;
903
904 The context `C<int>' must be an implicit instantiation.
905 Otherwise this is just a member class template declared
906 earlier like:
907
908 template <> class C<int> { template <class U> class D; };
909 template <> template <class U> class C<int>::D;
910
911 In the first case, `C<int>::D' is a specialization of `C<T>::D'
912 while in the second case, `C<int>::D' is a primary template
913 and `C<T>::D' may not exist. */
914
915 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
916 && !COMPLETE_TYPE_P (type))
917 {
918 tree t;
919 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
920
921 if (current_namespace
922 != decl_namespace_context (tmpl))
923 {
924 permerror (input_location, "specializing %q#T in different namespace", type);
925 permerror (input_location, " from definition of %q+#D", tmpl);
926 }
927
928 /* Check for invalid specialization after instantiation:
929
930 template <> template <> class C<int>::D<int>;
931 template <> template <class U> class C<int>::D; */
932
933 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
934 t; t = TREE_CHAIN (t))
935 {
936 tree inst = TREE_VALUE (t);
937 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
938 || !COMPLETE_OR_OPEN_TYPE_P (inst))
939 {
940 /* We already have a full specialization of this partial
941 instantiation, or a full specialization has been
942 looked up but not instantiated. Reassign it to the
943 new member specialization template. */
944 spec_entry elt;
945 spec_entry *entry;
946
947 elt.tmpl = most_general_template (tmpl);
948 elt.args = CLASSTYPE_TI_ARGS (inst);
949 elt.spec = inst;
950
951 type_specializations->remove_elt (&elt);
952
953 elt.tmpl = tmpl;
954 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
955
956 spec_entry **slot
957 = type_specializations->find_slot (&elt, INSERT);
958 entry = ggc_alloc<spec_entry> ();
959 *entry = elt;
960 *slot = entry;
961 }
962 else
963 /* But if we've had an implicit instantiation, that's a
964 problem ([temp.expl.spec]/6). */
965 error ("specialization %qT after instantiation %qT",
966 type, inst);
967 }
968
969 /* Mark TYPE as a specialization. And as a result, we only
970 have one level of template argument for the innermost
971 class template. */
972 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
973 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
974 CLASSTYPE_TI_ARGS (type)
975 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
976 }
977 }
978 else if (processing_specialization)
979 {
980 /* Someday C++0x may allow for enum template specialization. */
981 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
982 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
983 pedwarn (input_location, OPT_Wpedantic, "template specialization "
984 "of %qD not allowed by ISO C++", type);
985 else
986 {
987 error ("explicit specialization of non-template %qT", type);
988 return error_mark_node;
989 }
990 }
991
992 return type;
993 }
994
995 /* Returns nonzero if we can optimize the retrieval of specializations
996 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
997 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
998
999 static inline bool
1000 optimize_specialization_lookup_p (tree tmpl)
1001 {
1002 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1003 && DECL_CLASS_SCOPE_P (tmpl)
1004 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1005 parameter. */
1006 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1007 /* The optimized lookup depends on the fact that the
1008 template arguments for the member function template apply
1009 purely to the containing class, which is not true if the
1010 containing class is an explicit or partial
1011 specialization. */
1012 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1013 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1014 && !DECL_CONV_FN_P (tmpl)
1015 /* It is possible to have a template that is not a member
1016 template and is not a member of a template class:
1017
1018 template <typename T>
1019 struct S { friend A::f(); };
1020
1021 Here, the friend function is a template, but the context does
1022 not have template information. The optimized lookup relies
1023 on having ARGS be the template arguments for both the class
1024 and the function template. */
1025 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1026 }
1027
1028 /* Retrieve the specialization (in the sense of [temp.spec] - a
1029 specialization is either an instantiation or an explicit
1030 specialization) of TMPL for the given template ARGS. If there is
1031 no such specialization, return NULL_TREE. The ARGS are a vector of
1032 arguments, or a vector of vectors of arguments, in the case of
1033 templates with more than one level of parameters.
1034
1035 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1036 then we search for a partial specialization matching ARGS. This
1037 parameter is ignored if TMPL is not a class template.
1038
1039 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1040 result is a NONTYPE_ARGUMENT_PACK. */
1041
1042 static tree
1043 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1044 {
1045 if (tmpl == NULL_TREE)
1046 return NULL_TREE;
1047
1048 if (args == error_mark_node)
1049 return NULL_TREE;
1050
1051 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1052 || TREE_CODE (tmpl) == FIELD_DECL);
1053
1054 /* There should be as many levels of arguments as there are
1055 levels of parameters. */
1056 gcc_assert (TMPL_ARGS_DEPTH (args)
1057 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1058 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1059 : template_class_depth (DECL_CONTEXT (tmpl))));
1060
1061 #ifdef ENABLE_CHECKING
1062 /* We should have gone through coerce_template_parms by now. */
1063 ++processing_template_decl;
1064 if (!any_dependent_template_arguments_p (args))
1065 gcc_assert (strip_typedefs_expr (args, NULL) == args);
1066 --processing_template_decl;
1067 #endif
1068
1069 if (optimize_specialization_lookup_p (tmpl))
1070 {
1071 tree class_template;
1072 tree class_specialization;
1073 vec<tree, va_gc> *methods;
1074 tree fns;
1075 int idx;
1076
1077 /* The template arguments actually apply to the containing
1078 class. Find the class specialization with those
1079 arguments. */
1080 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1081 class_specialization
1082 = retrieve_specialization (class_template, args, 0);
1083 if (!class_specialization)
1084 return NULL_TREE;
1085 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1086 for the specialization. */
1087 idx = class_method_index_for_fn (class_specialization, tmpl);
1088 if (idx == -1)
1089 return NULL_TREE;
1090 /* Iterate through the methods with the indicated name, looking
1091 for the one that has an instance of TMPL. */
1092 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1093 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1094 {
1095 tree fn = OVL_CURRENT (fns);
1096 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1097 /* using-declarations can add base methods to the method vec,
1098 and we don't want those here. */
1099 && DECL_CONTEXT (fn) == class_specialization)
1100 return fn;
1101 }
1102 return NULL_TREE;
1103 }
1104 else
1105 {
1106 spec_entry *found;
1107 spec_entry elt;
1108 hash_table<spec_hasher> *specializations;
1109
1110 elt.tmpl = tmpl;
1111 elt.args = args;
1112 elt.spec = NULL_TREE;
1113
1114 if (DECL_CLASS_TEMPLATE_P (tmpl))
1115 specializations = type_specializations;
1116 else
1117 specializations = decl_specializations;
1118
1119 if (hash == 0)
1120 hash = spec_hasher::hash (&elt);
1121 found = specializations->find_with_hash (&elt, hash);
1122 if (found)
1123 return found->spec;
1124 }
1125
1126 return NULL_TREE;
1127 }
1128
1129 /* Like retrieve_specialization, but for local declarations. */
1130
1131 static tree
1132 retrieve_local_specialization (tree tmpl)
1133 {
1134 if (local_specializations == NULL)
1135 return NULL_TREE;
1136
1137 tree *slot = local_specializations->get (tmpl);
1138 return slot ? *slot : NULL_TREE;
1139 }
1140
1141 /* Returns nonzero iff DECL is a specialization of TMPL. */
1142
1143 int
1144 is_specialization_of (tree decl, tree tmpl)
1145 {
1146 tree t;
1147
1148 if (TREE_CODE (decl) == FUNCTION_DECL)
1149 {
1150 for (t = decl;
1151 t != NULL_TREE;
1152 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1153 if (t == tmpl)
1154 return 1;
1155 }
1156 else
1157 {
1158 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1159
1160 for (t = TREE_TYPE (decl);
1161 t != NULL_TREE;
1162 t = CLASSTYPE_USE_TEMPLATE (t)
1163 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1164 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1165 return 1;
1166 }
1167
1168 return 0;
1169 }
1170
1171 /* Returns nonzero iff DECL is a specialization of friend declaration
1172 FRIEND_DECL according to [temp.friend]. */
1173
1174 bool
1175 is_specialization_of_friend (tree decl, tree friend_decl)
1176 {
1177 bool need_template = true;
1178 int template_depth;
1179
1180 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1181 || TREE_CODE (decl) == TYPE_DECL);
1182
1183 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1184 of a template class, we want to check if DECL is a specialization
1185 if this. */
1186 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1187 && DECL_TEMPLATE_INFO (friend_decl)
1188 && !DECL_USE_TEMPLATE (friend_decl))
1189 {
1190 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1191 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1192 need_template = false;
1193 }
1194 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1195 && !PRIMARY_TEMPLATE_P (friend_decl))
1196 need_template = false;
1197
1198 /* There is nothing to do if this is not a template friend. */
1199 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1200 return false;
1201
1202 if (is_specialization_of (decl, friend_decl))
1203 return true;
1204
1205 /* [temp.friend/6]
1206 A member of a class template may be declared to be a friend of a
1207 non-template class. In this case, the corresponding member of
1208 every specialization of the class template is a friend of the
1209 class granting friendship.
1210
1211 For example, given a template friend declaration
1212
1213 template <class T> friend void A<T>::f();
1214
1215 the member function below is considered a friend
1216
1217 template <> struct A<int> {
1218 void f();
1219 };
1220
1221 For this type of template friend, TEMPLATE_DEPTH below will be
1222 nonzero. To determine if DECL is a friend of FRIEND, we first
1223 check if the enclosing class is a specialization of another. */
1224
1225 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1226 if (template_depth
1227 && DECL_CLASS_SCOPE_P (decl)
1228 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1229 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1230 {
1231 /* Next, we check the members themselves. In order to handle
1232 a few tricky cases, such as when FRIEND_DECL's are
1233
1234 template <class T> friend void A<T>::g(T t);
1235 template <class T> template <T t> friend void A<T>::h();
1236
1237 and DECL's are
1238
1239 void A<int>::g(int);
1240 template <int> void A<int>::h();
1241
1242 we need to figure out ARGS, the template arguments from
1243 the context of DECL. This is required for template substitution
1244 of `T' in the function parameter of `g' and template parameter
1245 of `h' in the above examples. Here ARGS corresponds to `int'. */
1246
1247 tree context = DECL_CONTEXT (decl);
1248 tree args = NULL_TREE;
1249 int current_depth = 0;
1250
1251 while (current_depth < template_depth)
1252 {
1253 if (CLASSTYPE_TEMPLATE_INFO (context))
1254 {
1255 if (current_depth == 0)
1256 args = TYPE_TI_ARGS (context);
1257 else
1258 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1259 current_depth++;
1260 }
1261 context = TYPE_CONTEXT (context);
1262 }
1263
1264 if (TREE_CODE (decl) == FUNCTION_DECL)
1265 {
1266 bool is_template;
1267 tree friend_type;
1268 tree decl_type;
1269 tree friend_args_type;
1270 tree decl_args_type;
1271
1272 /* Make sure that both DECL and FRIEND_DECL are templates or
1273 non-templates. */
1274 is_template = DECL_TEMPLATE_INFO (decl)
1275 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1276 if (need_template ^ is_template)
1277 return false;
1278 else if (is_template)
1279 {
1280 /* If both are templates, check template parameter list. */
1281 tree friend_parms
1282 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1283 args, tf_none);
1284 if (!comp_template_parms
1285 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1286 friend_parms))
1287 return false;
1288
1289 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1290 }
1291 else
1292 decl_type = TREE_TYPE (decl);
1293
1294 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1295 tf_none, NULL_TREE);
1296 if (friend_type == error_mark_node)
1297 return false;
1298
1299 /* Check if return types match. */
1300 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1301 return false;
1302
1303 /* Check if function parameter types match, ignoring the
1304 `this' parameter. */
1305 friend_args_type = TYPE_ARG_TYPES (friend_type);
1306 decl_args_type = TYPE_ARG_TYPES (decl_type);
1307 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1308 friend_args_type = TREE_CHAIN (friend_args_type);
1309 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1310 decl_args_type = TREE_CHAIN (decl_args_type);
1311
1312 return compparms (decl_args_type, friend_args_type);
1313 }
1314 else
1315 {
1316 /* DECL is a TYPE_DECL */
1317 bool is_template;
1318 tree decl_type = TREE_TYPE (decl);
1319
1320 /* Make sure that both DECL and FRIEND_DECL are templates or
1321 non-templates. */
1322 is_template
1323 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1324 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1325
1326 if (need_template ^ is_template)
1327 return false;
1328 else if (is_template)
1329 {
1330 tree friend_parms;
1331 /* If both are templates, check the name of the two
1332 TEMPLATE_DECL's first because is_friend didn't. */
1333 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1334 != DECL_NAME (friend_decl))
1335 return false;
1336
1337 /* Now check template parameter list. */
1338 friend_parms
1339 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1340 args, tf_none);
1341 return comp_template_parms
1342 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1343 friend_parms);
1344 }
1345 else
1346 return (DECL_NAME (decl)
1347 == DECL_NAME (friend_decl));
1348 }
1349 }
1350 return false;
1351 }
1352
1353 /* Register the specialization SPEC as a specialization of TMPL with
1354 the indicated ARGS. IS_FRIEND indicates whether the specialization
1355 is actually just a friend declaration. Returns SPEC, or an
1356 equivalent prior declaration, if available.
1357
1358 We also store instantiations of field packs in the hash table, even
1359 though they are not themselves templates, to make lookup easier. */
1360
1361 static tree
1362 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1363 hashval_t hash)
1364 {
1365 tree fn;
1366 spec_entry **slot = NULL;
1367 spec_entry elt;
1368
1369 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1370 || (TREE_CODE (tmpl) == FIELD_DECL
1371 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1372
1373 if (TREE_CODE (spec) == FUNCTION_DECL
1374 && uses_template_parms (DECL_TI_ARGS (spec)))
1375 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1376 register it; we want the corresponding TEMPLATE_DECL instead.
1377 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1378 the more obvious `uses_template_parms (spec)' to avoid problems
1379 with default function arguments. In particular, given
1380 something like this:
1381
1382 template <class T> void f(T t1, T t = T())
1383
1384 the default argument expression is not substituted for in an
1385 instantiation unless and until it is actually needed. */
1386 return spec;
1387
1388 if (optimize_specialization_lookup_p (tmpl))
1389 /* We don't put these specializations in the hash table, but we might
1390 want to give an error about a mismatch. */
1391 fn = retrieve_specialization (tmpl, args, 0);
1392 else
1393 {
1394 elt.tmpl = tmpl;
1395 elt.args = args;
1396 elt.spec = spec;
1397
1398 if (hash == 0)
1399 hash = spec_hasher::hash (&elt);
1400
1401 slot =
1402 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1403 if (*slot)
1404 fn = ((spec_entry *) *slot)->spec;
1405 else
1406 fn = NULL_TREE;
1407 }
1408
1409 /* We can sometimes try to re-register a specialization that we've
1410 already got. In particular, regenerate_decl_from_template calls
1411 duplicate_decls which will update the specialization list. But,
1412 we'll still get called again here anyhow. It's more convenient
1413 to simply allow this than to try to prevent it. */
1414 if (fn == spec)
1415 return spec;
1416 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1417 {
1418 if (DECL_TEMPLATE_INSTANTIATION (fn))
1419 {
1420 if (DECL_ODR_USED (fn)
1421 || DECL_EXPLICIT_INSTANTIATION (fn))
1422 {
1423 error ("specialization of %qD after instantiation",
1424 fn);
1425 return error_mark_node;
1426 }
1427 else
1428 {
1429 tree clone;
1430 /* This situation should occur only if the first
1431 specialization is an implicit instantiation, the
1432 second is an explicit specialization, and the
1433 implicit instantiation has not yet been used. That
1434 situation can occur if we have implicitly
1435 instantiated a member function and then specialized
1436 it later.
1437
1438 We can also wind up here if a friend declaration that
1439 looked like an instantiation turns out to be a
1440 specialization:
1441
1442 template <class T> void foo(T);
1443 class S { friend void foo<>(int) };
1444 template <> void foo(int);
1445
1446 We transform the existing DECL in place so that any
1447 pointers to it become pointers to the updated
1448 declaration.
1449
1450 If there was a definition for the template, but not
1451 for the specialization, we want this to look as if
1452 there were no definition, and vice versa. */
1453 DECL_INITIAL (fn) = NULL_TREE;
1454 duplicate_decls (spec, fn, is_friend);
1455 /* The call to duplicate_decls will have applied
1456 [temp.expl.spec]:
1457
1458 An explicit specialization of a function template
1459 is inline only if it is explicitly declared to be,
1460 and independently of whether its function template
1461 is.
1462
1463 to the primary function; now copy the inline bits to
1464 the various clones. */
1465 FOR_EACH_CLONE (clone, fn)
1466 {
1467 DECL_DECLARED_INLINE_P (clone)
1468 = DECL_DECLARED_INLINE_P (fn);
1469 DECL_SOURCE_LOCATION (clone)
1470 = DECL_SOURCE_LOCATION (fn);
1471 DECL_DELETED_FN (clone)
1472 = DECL_DELETED_FN (fn);
1473 }
1474 check_specialization_namespace (tmpl);
1475
1476 return fn;
1477 }
1478 }
1479 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1480 {
1481 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1482 /* Dup decl failed, but this is a new definition. Set the
1483 line number so any errors match this new
1484 definition. */
1485 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1486
1487 return fn;
1488 }
1489 }
1490 else if (fn)
1491 return duplicate_decls (spec, fn, is_friend);
1492
1493 /* A specialization must be declared in the same namespace as the
1494 template it is specializing. */
1495 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1496 && !check_specialization_namespace (tmpl))
1497 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1498
1499 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1500 {
1501 spec_entry *entry = ggc_alloc<spec_entry> ();
1502 gcc_assert (tmpl && args && spec);
1503 *entry = elt;
1504 *slot = entry;
1505 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1506 && PRIMARY_TEMPLATE_P (tmpl)
1507 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1508 || variable_template_p (tmpl))
1509 /* If TMPL is a forward declaration of a template function, keep a list
1510 of all specializations in case we need to reassign them to a friend
1511 template later in tsubst_friend_function.
1512
1513 Also keep a list of all variable template instantiations so that
1514 process_partial_specialization can check whether a later partial
1515 specialization would have used it. */
1516 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1517 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1518 }
1519
1520 return spec;
1521 }
1522
1523 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1524 TMPL and ARGS members, ignores SPEC. */
1525
1526 int comparing_specializations;
1527
1528 bool
1529 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1530 {
1531 int equal;
1532
1533 ++comparing_specializations;
1534 equal = (e1->tmpl == e2->tmpl
1535 && comp_template_args (e1->args, e2->args));
1536 --comparing_specializations;
1537
1538 return equal;
1539 }
1540
1541 /* Returns a hash for a template TMPL and template arguments ARGS. */
1542
1543 static hashval_t
1544 hash_tmpl_and_args (tree tmpl, tree args)
1545 {
1546 hashval_t val = DECL_UID (tmpl);
1547 return iterative_hash_template_arg (args, val);
1548 }
1549
1550 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1551 ignoring SPEC. */
1552
1553 hashval_t
1554 spec_hasher::hash (spec_entry *e)
1555 {
1556 return hash_tmpl_and_args (e->tmpl, e->args);
1557 }
1558
1559 /* Recursively calculate a hash value for a template argument ARG, for use
1560 in the hash tables of template specializations. */
1561
1562 hashval_t
1563 iterative_hash_template_arg (tree arg, hashval_t val)
1564 {
1565 unsigned HOST_WIDE_INT i;
1566 enum tree_code code;
1567 char tclass;
1568
1569 if (arg == NULL_TREE)
1570 return iterative_hash_object (arg, val);
1571
1572 if (!TYPE_P (arg))
1573 STRIP_NOPS (arg);
1574
1575 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1576 /* We can get one of these when re-hashing a previous entry in the middle
1577 of substituting into a pack expansion. Just look through it. */
1578 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1579
1580 code = TREE_CODE (arg);
1581 tclass = TREE_CODE_CLASS (code);
1582
1583 val = iterative_hash_object (code, val);
1584
1585 switch (code)
1586 {
1587 case ERROR_MARK:
1588 return val;
1589
1590 case IDENTIFIER_NODE:
1591 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1592
1593 case TREE_VEC:
1594 {
1595 int i, len = TREE_VEC_LENGTH (arg);
1596 for (i = 0; i < len; ++i)
1597 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1598 return val;
1599 }
1600
1601 case TYPE_PACK_EXPANSION:
1602 case EXPR_PACK_EXPANSION:
1603 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1604 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1605
1606 case TYPE_ARGUMENT_PACK:
1607 case NONTYPE_ARGUMENT_PACK:
1608 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1609
1610 case TREE_LIST:
1611 for (; arg; arg = TREE_CHAIN (arg))
1612 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1613 return val;
1614
1615 case OVERLOAD:
1616 for (; arg; arg = OVL_NEXT (arg))
1617 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1618 return val;
1619
1620 case CONSTRUCTOR:
1621 {
1622 tree field, value;
1623 iterative_hash_template_arg (TREE_TYPE (arg), val);
1624 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1625 {
1626 val = iterative_hash_template_arg (field, val);
1627 val = iterative_hash_template_arg (value, val);
1628 }
1629 return val;
1630 }
1631
1632 case PARM_DECL:
1633 if (!DECL_ARTIFICIAL (arg))
1634 {
1635 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1636 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1637 }
1638 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1639
1640 case TARGET_EXPR:
1641 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1642
1643 case PTRMEM_CST:
1644 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1645 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1646
1647 case TEMPLATE_PARM_INDEX:
1648 val = iterative_hash_template_arg
1649 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1650 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1651 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1652
1653 case TRAIT_EXPR:
1654 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1655 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1656 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1657
1658 case BASELINK:
1659 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1660 val);
1661 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1662 val);
1663
1664 case MODOP_EXPR:
1665 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1666 code = TREE_CODE (TREE_OPERAND (arg, 1));
1667 val = iterative_hash_object (code, val);
1668 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1669
1670 case LAMBDA_EXPR:
1671 /* A lambda can't appear in a template arg, but don't crash on
1672 erroneous input. */
1673 gcc_assert (seen_error ());
1674 return val;
1675
1676 case CAST_EXPR:
1677 case IMPLICIT_CONV_EXPR:
1678 case STATIC_CAST_EXPR:
1679 case REINTERPRET_CAST_EXPR:
1680 case CONST_CAST_EXPR:
1681 case DYNAMIC_CAST_EXPR:
1682 case NEW_EXPR:
1683 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1684 /* Now hash operands as usual. */
1685 break;
1686
1687 default:
1688 break;
1689 }
1690
1691 switch (tclass)
1692 {
1693 case tcc_type:
1694 if (alias_template_specialization_p (arg))
1695 {
1696 // We want an alias specialization that survived strip_typedefs
1697 // to hash differently from its TYPE_CANONICAL, to avoid hash
1698 // collisions that compare as different in template_args_equal.
1699 // These could be dependent specializations that strip_typedefs
1700 // left alone, or untouched specializations because
1701 // coerce_template_parms returns the unconverted template
1702 // arguments if it sees incomplete argument packs.
1703 tree ti = TYPE_TEMPLATE_INFO (arg);
1704 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1705 }
1706 if (TYPE_CANONICAL (arg))
1707 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1708 val);
1709 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1710 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1711 /* Otherwise just compare the types during lookup. */
1712 return val;
1713
1714 case tcc_declaration:
1715 case tcc_constant:
1716 return iterative_hash_expr (arg, val);
1717
1718 default:
1719 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1720 {
1721 unsigned n = cp_tree_operand_length (arg);
1722 for (i = 0; i < n; ++i)
1723 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1724 return val;
1725 }
1726 }
1727 gcc_unreachable ();
1728 return 0;
1729 }
1730
1731 /* Unregister the specialization SPEC as a specialization of TMPL.
1732 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1733 if the SPEC was listed as a specialization of TMPL.
1734
1735 Note that SPEC has been ggc_freed, so we can't look inside it. */
1736
1737 bool
1738 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1739 {
1740 spec_entry *entry;
1741 spec_entry elt;
1742
1743 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1744 elt.args = TI_ARGS (tinfo);
1745 elt.spec = NULL_TREE;
1746
1747 entry = decl_specializations->find (&elt);
1748 if (entry != NULL)
1749 {
1750 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1751 gcc_assert (new_spec != NULL_TREE);
1752 entry->spec = new_spec;
1753 return 1;
1754 }
1755
1756 return 0;
1757 }
1758
1759 /* Like register_specialization, but for local declarations. We are
1760 registering SPEC, an instantiation of TMPL. */
1761
1762 static void
1763 register_local_specialization (tree spec, tree tmpl)
1764 {
1765 local_specializations->put (tmpl, spec);
1766 }
1767
1768 /* TYPE is a class type. Returns true if TYPE is an explicitly
1769 specialized class. */
1770
1771 bool
1772 explicit_class_specialization_p (tree type)
1773 {
1774 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1775 return false;
1776 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1777 }
1778
1779 /* Print the list of functions at FNS, going through all the overloads
1780 for each element of the list. Alternatively, FNS can not be a
1781 TREE_LIST, in which case it will be printed together with all the
1782 overloads.
1783
1784 MORE and *STR should respectively be FALSE and NULL when the function
1785 is called from the outside. They are used internally on recursive
1786 calls. print_candidates manages the two parameters and leaves NULL
1787 in *STR when it ends. */
1788
1789 static void
1790 print_candidates_1 (tree fns, bool more, const char **str)
1791 {
1792 tree fn, fn2;
1793 char *spaces = NULL;
1794
1795 for (fn = fns; fn; fn = OVL_NEXT (fn))
1796 if (TREE_CODE (fn) == TREE_LIST)
1797 {
1798 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1799 print_candidates_1 (TREE_VALUE (fn2),
1800 TREE_CHAIN (fn2) || more, str);
1801 }
1802 else
1803 {
1804 tree cand = OVL_CURRENT (fn);
1805 if (!*str)
1806 {
1807 /* Pick the prefix string. */
1808 if (!more && !OVL_NEXT (fns))
1809 {
1810 inform (DECL_SOURCE_LOCATION (cand),
1811 "candidate is: %#D", cand);
1812 continue;
1813 }
1814
1815 *str = _("candidates are:");
1816 spaces = get_spaces (*str);
1817 }
1818 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1819 *str = spaces ? spaces : *str;
1820 }
1821
1822 if (!more)
1823 {
1824 free (spaces);
1825 *str = NULL;
1826 }
1827 }
1828
1829 /* Print the list of candidate FNS in an error message. FNS can also
1830 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1831
1832 void
1833 print_candidates (tree fns)
1834 {
1835 const char *str = NULL;
1836 print_candidates_1 (fns, false, &str);
1837 gcc_assert (str == NULL);
1838 }
1839
1840 /* Returns the template (one of the functions given by TEMPLATE_ID)
1841 which can be specialized to match the indicated DECL with the
1842 explicit template args given in TEMPLATE_ID. The DECL may be
1843 NULL_TREE if none is available. In that case, the functions in
1844 TEMPLATE_ID are non-members.
1845
1846 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1847 specialization of a member template.
1848
1849 The TEMPLATE_COUNT is the number of references to qualifying
1850 template classes that appeared in the name of the function. See
1851 check_explicit_specialization for a more accurate description.
1852
1853 TSK indicates what kind of template declaration (if any) is being
1854 declared. TSK_TEMPLATE indicates that the declaration given by
1855 DECL, though a FUNCTION_DECL, has template parameters, and is
1856 therefore a template function.
1857
1858 The template args (those explicitly specified and those deduced)
1859 are output in a newly created vector *TARGS_OUT.
1860
1861 If it is impossible to determine the result, an error message is
1862 issued. The error_mark_node is returned to indicate failure. */
1863
1864 static tree
1865 determine_specialization (tree template_id,
1866 tree decl,
1867 tree* targs_out,
1868 int need_member_template,
1869 int template_count,
1870 tmpl_spec_kind tsk)
1871 {
1872 tree fns;
1873 tree targs;
1874 tree explicit_targs;
1875 tree candidates = NULL_TREE;
1876 /* A TREE_LIST of templates of which DECL may be a specialization.
1877 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1878 corresponding TREE_PURPOSE is the set of template arguments that,
1879 when used to instantiate the template, would produce a function
1880 with the signature of DECL. */
1881 tree templates = NULL_TREE;
1882 int header_count;
1883 cp_binding_level *b;
1884
1885 *targs_out = NULL_TREE;
1886
1887 if (template_id == error_mark_node || decl == error_mark_node)
1888 return error_mark_node;
1889
1890 /* We shouldn't be specializing a member template of an
1891 unspecialized class template; we already gave an error in
1892 check_specialization_scope, now avoid crashing. */
1893 if (template_count && DECL_CLASS_SCOPE_P (decl)
1894 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1895 {
1896 gcc_assert (errorcount);
1897 return error_mark_node;
1898 }
1899
1900 fns = TREE_OPERAND (template_id, 0);
1901 explicit_targs = TREE_OPERAND (template_id, 1);
1902
1903 if (fns == error_mark_node)
1904 return error_mark_node;
1905
1906 /* Check for baselinks. */
1907 if (BASELINK_P (fns))
1908 fns = BASELINK_FUNCTIONS (fns);
1909
1910 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
1911 {
1912 error ("%qD is not a function template", fns);
1913 return error_mark_node;
1914 }
1915 else if (VAR_P (decl) && !variable_template_p (fns))
1916 {
1917 error ("%qD is not a variable template", fns);
1918 return error_mark_node;
1919 }
1920
1921 /* Count the number of template headers specified for this
1922 specialization. */
1923 header_count = 0;
1924 for (b = current_binding_level;
1925 b->kind == sk_template_parms;
1926 b = b->level_chain)
1927 ++header_count;
1928
1929 if (variable_template_p (fns))
1930 {
1931 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
1932 targs = coerce_template_parms (parms, explicit_targs, fns,
1933 tf_warning_or_error,
1934 /*req_all*/true, /*use_defarg*/true);
1935 templates = tree_cons (targs, fns, templates);
1936 }
1937 else for (; fns; fns = OVL_NEXT (fns))
1938 {
1939 tree fn = OVL_CURRENT (fns);
1940
1941 if (TREE_CODE (fn) == TEMPLATE_DECL)
1942 {
1943 tree decl_arg_types;
1944 tree fn_arg_types;
1945 tree insttype;
1946
1947 /* In case of explicit specialization, we need to check if
1948 the number of template headers appearing in the specialization
1949 is correct. This is usually done in check_explicit_specialization,
1950 but the check done there cannot be exhaustive when specializing
1951 member functions. Consider the following code:
1952
1953 template <> void A<int>::f(int);
1954 template <> template <> void A<int>::f(int);
1955
1956 Assuming that A<int> is not itself an explicit specialization
1957 already, the first line specializes "f" which is a non-template
1958 member function, whilst the second line specializes "f" which
1959 is a template member function. So both lines are syntactically
1960 correct, and check_explicit_specialization does not reject
1961 them.
1962
1963 Here, we can do better, as we are matching the specialization
1964 against the declarations. We count the number of template
1965 headers, and we check if they match TEMPLATE_COUNT + 1
1966 (TEMPLATE_COUNT is the number of qualifying template classes,
1967 plus there must be another header for the member template
1968 itself).
1969
1970 Notice that if header_count is zero, this is not a
1971 specialization but rather a template instantiation, so there
1972 is no check we can perform here. */
1973 if (header_count && header_count != template_count + 1)
1974 continue;
1975
1976 /* Check that the number of template arguments at the
1977 innermost level for DECL is the same as for FN. */
1978 if (current_binding_level->kind == sk_template_parms
1979 && !current_binding_level->explicit_spec_p
1980 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1981 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1982 (current_template_parms))))
1983 continue;
1984
1985 /* DECL might be a specialization of FN. */
1986 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1987 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1988
1989 /* For a non-static member function, we need to make sure
1990 that the const qualification is the same. Since
1991 get_bindings does not try to merge the "this" parameter,
1992 we must do the comparison explicitly. */
1993 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1994 && !same_type_p (TREE_VALUE (fn_arg_types),
1995 TREE_VALUE (decl_arg_types)))
1996 continue;
1997
1998 /* Skip the "this" parameter and, for constructors of
1999 classes with virtual bases, the VTT parameter. A
2000 full specialization of a constructor will have a VTT
2001 parameter, but a template never will. */
2002 decl_arg_types
2003 = skip_artificial_parms_for (decl, decl_arg_types);
2004 fn_arg_types
2005 = skip_artificial_parms_for (fn, fn_arg_types);
2006
2007 /* Function templates cannot be specializations; there are
2008 no partial specializations of functions. Therefore, if
2009 the type of DECL does not match FN, there is no
2010 match. */
2011 if (tsk == tsk_template)
2012 {
2013 if (compparms (fn_arg_types, decl_arg_types))
2014 candidates = tree_cons (NULL_TREE, fn, candidates);
2015 continue;
2016 }
2017
2018 /* See whether this function might be a specialization of this
2019 template. Suppress access control because we might be trying
2020 to make this specialization a friend, and we have already done
2021 access control for the declaration of the specialization. */
2022 push_deferring_access_checks (dk_no_check);
2023 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2024 pop_deferring_access_checks ();
2025
2026 if (!targs)
2027 /* We cannot deduce template arguments that when used to
2028 specialize TMPL will produce DECL. */
2029 continue;
2030
2031 /* Make sure that the deduced arguments actually work. */
2032 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2033 if (insttype == error_mark_node)
2034 continue;
2035 fn_arg_types
2036 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2037 if (!compparms (fn_arg_types, decl_arg_types))
2038 continue;
2039
2040 /* Save this template, and the arguments deduced. */
2041 templates = tree_cons (targs, fn, templates);
2042 }
2043 else if (need_member_template)
2044 /* FN is an ordinary member function, and we need a
2045 specialization of a member template. */
2046 ;
2047 else if (TREE_CODE (fn) != FUNCTION_DECL)
2048 /* We can get IDENTIFIER_NODEs here in certain erroneous
2049 cases. */
2050 ;
2051 else if (!DECL_FUNCTION_MEMBER_P (fn))
2052 /* This is just an ordinary non-member function. Nothing can
2053 be a specialization of that. */
2054 ;
2055 else if (DECL_ARTIFICIAL (fn))
2056 /* Cannot specialize functions that are created implicitly. */
2057 ;
2058 else
2059 {
2060 tree decl_arg_types;
2061
2062 /* This is an ordinary member function. However, since
2063 we're here, we can assume its enclosing class is a
2064 template class. For example,
2065
2066 template <typename T> struct S { void f(); };
2067 template <> void S<int>::f() {}
2068
2069 Here, S<int>::f is a non-template, but S<int> is a
2070 template class. If FN has the same type as DECL, we
2071 might be in business. */
2072
2073 if (!DECL_TEMPLATE_INFO (fn))
2074 /* Its enclosing class is an explicit specialization
2075 of a template class. This is not a candidate. */
2076 continue;
2077
2078 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2079 TREE_TYPE (TREE_TYPE (fn))))
2080 /* The return types differ. */
2081 continue;
2082
2083 /* Adjust the type of DECL in case FN is a static member. */
2084 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2085 if (DECL_STATIC_FUNCTION_P (fn)
2086 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2087 decl_arg_types = TREE_CHAIN (decl_arg_types);
2088
2089 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2090 decl_arg_types))
2091 /* They match! */
2092 candidates = tree_cons (NULL_TREE, fn, candidates);
2093 }
2094 }
2095
2096 if (templates && TREE_CHAIN (templates))
2097 {
2098 /* We have:
2099
2100 [temp.expl.spec]
2101
2102 It is possible for a specialization with a given function
2103 signature to be instantiated from more than one function
2104 template. In such cases, explicit specification of the
2105 template arguments must be used to uniquely identify the
2106 function template specialization being specialized.
2107
2108 Note that here, there's no suggestion that we're supposed to
2109 determine which of the candidate templates is most
2110 specialized. However, we, also have:
2111
2112 [temp.func.order]
2113
2114 Partial ordering of overloaded function template
2115 declarations is used in the following contexts to select
2116 the function template to which a function template
2117 specialization refers:
2118
2119 -- when an explicit specialization refers to a function
2120 template.
2121
2122 So, we do use the partial ordering rules, at least for now.
2123 This extension can only serve to make invalid programs valid,
2124 so it's safe. And, there is strong anecdotal evidence that
2125 the committee intended the partial ordering rules to apply;
2126 the EDG front end has that behavior, and John Spicer claims
2127 that the committee simply forgot to delete the wording in
2128 [temp.expl.spec]. */
2129 tree tmpl = most_specialized_instantiation (templates);
2130 if (tmpl != error_mark_node)
2131 {
2132 templates = tmpl;
2133 TREE_CHAIN (templates) = NULL_TREE;
2134 }
2135 }
2136
2137 if (templates == NULL_TREE && candidates == NULL_TREE)
2138 {
2139 error ("template-id %qD for %q+D does not match any template "
2140 "declaration", template_id, decl);
2141 if (header_count && header_count != template_count + 1)
2142 inform (input_location, "saw %d %<template<>%>, need %d for "
2143 "specializing a member function template",
2144 header_count, template_count + 1);
2145 return error_mark_node;
2146 }
2147 else if ((templates && TREE_CHAIN (templates))
2148 || (candidates && TREE_CHAIN (candidates))
2149 || (templates && candidates))
2150 {
2151 error ("ambiguous template specialization %qD for %q+D",
2152 template_id, decl);
2153 candidates = chainon (candidates, templates);
2154 print_candidates (candidates);
2155 return error_mark_node;
2156 }
2157
2158 /* We have one, and exactly one, match. */
2159 if (candidates)
2160 {
2161 tree fn = TREE_VALUE (candidates);
2162 *targs_out = copy_node (DECL_TI_ARGS (fn));
2163 /* DECL is a re-declaration or partial instantiation of a template
2164 function. */
2165 if (TREE_CODE (fn) == TEMPLATE_DECL)
2166 return fn;
2167 /* It was a specialization of an ordinary member function in a
2168 template class. */
2169 return DECL_TI_TEMPLATE (fn);
2170 }
2171
2172 /* It was a specialization of a template. */
2173 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2174 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2175 {
2176 *targs_out = copy_node (targs);
2177 SET_TMPL_ARGS_LEVEL (*targs_out,
2178 TMPL_ARGS_DEPTH (*targs_out),
2179 TREE_PURPOSE (templates));
2180 }
2181 else
2182 *targs_out = TREE_PURPOSE (templates);
2183 return TREE_VALUE (templates);
2184 }
2185
2186 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2187 but with the default argument values filled in from those in the
2188 TMPL_TYPES. */
2189
2190 static tree
2191 copy_default_args_to_explicit_spec_1 (tree spec_types,
2192 tree tmpl_types)
2193 {
2194 tree new_spec_types;
2195
2196 if (!spec_types)
2197 return NULL_TREE;
2198
2199 if (spec_types == void_list_node)
2200 return void_list_node;
2201
2202 /* Substitute into the rest of the list. */
2203 new_spec_types =
2204 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2205 TREE_CHAIN (tmpl_types));
2206
2207 /* Add the default argument for this parameter. */
2208 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2209 TREE_VALUE (spec_types),
2210 new_spec_types);
2211 }
2212
2213 /* DECL is an explicit specialization. Replicate default arguments
2214 from the template it specializes. (That way, code like:
2215
2216 template <class T> void f(T = 3);
2217 template <> void f(double);
2218 void g () { f (); }
2219
2220 works, as required.) An alternative approach would be to look up
2221 the correct default arguments at the call-site, but this approach
2222 is consistent with how implicit instantiations are handled. */
2223
2224 static void
2225 copy_default_args_to_explicit_spec (tree decl)
2226 {
2227 tree tmpl;
2228 tree spec_types;
2229 tree tmpl_types;
2230 tree new_spec_types;
2231 tree old_type;
2232 tree new_type;
2233 tree t;
2234 tree object_type = NULL_TREE;
2235 tree in_charge = NULL_TREE;
2236 tree vtt = NULL_TREE;
2237
2238 /* See if there's anything we need to do. */
2239 tmpl = DECL_TI_TEMPLATE (decl);
2240 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2241 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2242 if (TREE_PURPOSE (t))
2243 break;
2244 if (!t)
2245 return;
2246
2247 old_type = TREE_TYPE (decl);
2248 spec_types = TYPE_ARG_TYPES (old_type);
2249
2250 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2251 {
2252 /* Remove the this pointer, but remember the object's type for
2253 CV quals. */
2254 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2255 spec_types = TREE_CHAIN (spec_types);
2256 tmpl_types = TREE_CHAIN (tmpl_types);
2257
2258 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2259 {
2260 /* DECL may contain more parameters than TMPL due to the extra
2261 in-charge parameter in constructors and destructors. */
2262 in_charge = spec_types;
2263 spec_types = TREE_CHAIN (spec_types);
2264 }
2265 if (DECL_HAS_VTT_PARM_P (decl))
2266 {
2267 vtt = spec_types;
2268 spec_types = TREE_CHAIN (spec_types);
2269 }
2270 }
2271
2272 /* Compute the merged default arguments. */
2273 new_spec_types =
2274 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2275
2276 /* Compute the new FUNCTION_TYPE. */
2277 if (object_type)
2278 {
2279 if (vtt)
2280 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2281 TREE_VALUE (vtt),
2282 new_spec_types);
2283
2284 if (in_charge)
2285 /* Put the in-charge parameter back. */
2286 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2287 TREE_VALUE (in_charge),
2288 new_spec_types);
2289
2290 new_type = build_method_type_directly (object_type,
2291 TREE_TYPE (old_type),
2292 new_spec_types);
2293 }
2294 else
2295 new_type = build_function_type (TREE_TYPE (old_type),
2296 new_spec_types);
2297 new_type = cp_build_type_attribute_variant (new_type,
2298 TYPE_ATTRIBUTES (old_type));
2299 new_type = build_exception_variant (new_type,
2300 TYPE_RAISES_EXCEPTIONS (old_type));
2301
2302 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2303 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2304
2305 TREE_TYPE (decl) = new_type;
2306 }
2307
2308 /* Return the number of template headers we expect to see for a definition
2309 or specialization of CTYPE or one of its non-template members. */
2310
2311 int
2312 num_template_headers_for_class (tree ctype)
2313 {
2314 int num_templates = 0;
2315
2316 while (ctype && CLASS_TYPE_P (ctype))
2317 {
2318 /* You're supposed to have one `template <...>' for every
2319 template class, but you don't need one for a full
2320 specialization. For example:
2321
2322 template <class T> struct S{};
2323 template <> struct S<int> { void f(); };
2324 void S<int>::f () {}
2325
2326 is correct; there shouldn't be a `template <>' for the
2327 definition of `S<int>::f'. */
2328 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2329 /* If CTYPE does not have template information of any
2330 kind, then it is not a template, nor is it nested
2331 within a template. */
2332 break;
2333 if (explicit_class_specialization_p (ctype))
2334 break;
2335 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2336 ++num_templates;
2337
2338 ctype = TYPE_CONTEXT (ctype);
2339 }
2340
2341 return num_templates;
2342 }
2343
2344 /* Do a simple sanity check on the template headers that precede the
2345 variable declaration DECL. */
2346
2347 void
2348 check_template_variable (tree decl)
2349 {
2350 tree ctx = CP_DECL_CONTEXT (decl);
2351 int wanted = num_template_headers_for_class (ctx);
2352 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2353 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2354 {
2355 if (cxx_dialect < cxx14)
2356 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2357 "variable templates only available with "
2358 "-std=c++14 or -std=gnu++14");
2359
2360 // Namespace-scope variable templates should have a template header.
2361 ++wanted;
2362 }
2363 if (template_header_count > wanted)
2364 {
2365 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2366 "too many template headers for %D (should be %d)",
2367 decl, wanted);
2368 if (warned && CLASS_TYPE_P (ctx)
2369 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2370 inform (DECL_SOURCE_LOCATION (decl),
2371 "members of an explicitly specialized class are defined "
2372 "without a template header");
2373 }
2374 }
2375
2376 /* Check to see if the function just declared, as indicated in
2377 DECLARATOR, and in DECL, is a specialization of a function
2378 template. We may also discover that the declaration is an explicit
2379 instantiation at this point.
2380
2381 Returns DECL, or an equivalent declaration that should be used
2382 instead if all goes well. Issues an error message if something is
2383 amiss. Returns error_mark_node if the error is not easily
2384 recoverable.
2385
2386 FLAGS is a bitmask consisting of the following flags:
2387
2388 2: The function has a definition.
2389 4: The function is a friend.
2390
2391 The TEMPLATE_COUNT is the number of references to qualifying
2392 template classes that appeared in the name of the function. For
2393 example, in
2394
2395 template <class T> struct S { void f(); };
2396 void S<int>::f();
2397
2398 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2399 classes are not counted in the TEMPLATE_COUNT, so that in
2400
2401 template <class T> struct S {};
2402 template <> struct S<int> { void f(); }
2403 template <> void S<int>::f();
2404
2405 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2406 invalid; there should be no template <>.)
2407
2408 If the function is a specialization, it is marked as such via
2409 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2410 is set up correctly, and it is added to the list of specializations
2411 for that template. */
2412
2413 tree
2414 check_explicit_specialization (tree declarator,
2415 tree decl,
2416 int template_count,
2417 int flags)
2418 {
2419 int have_def = flags & 2;
2420 int is_friend = flags & 4;
2421 int specialization = 0;
2422 int explicit_instantiation = 0;
2423 int member_specialization = 0;
2424 tree ctype = DECL_CLASS_CONTEXT (decl);
2425 tree dname = DECL_NAME (decl);
2426 tmpl_spec_kind tsk;
2427
2428 if (is_friend)
2429 {
2430 if (!processing_specialization)
2431 tsk = tsk_none;
2432 else
2433 tsk = tsk_excessive_parms;
2434 }
2435 else
2436 tsk = current_tmpl_spec_kind (template_count);
2437
2438 switch (tsk)
2439 {
2440 case tsk_none:
2441 if (processing_specialization && TREE_CODE (decl) != VAR_DECL)
2442 {
2443 specialization = 1;
2444 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2445 }
2446 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2447 {
2448 if (is_friend)
2449 /* This could be something like:
2450
2451 template <class T> void f(T);
2452 class S { friend void f<>(int); } */
2453 specialization = 1;
2454 else
2455 {
2456 /* This case handles bogus declarations like template <>
2457 template <class T> void f<int>(); */
2458
2459 error ("template-id %qD in declaration of primary template",
2460 declarator);
2461 return decl;
2462 }
2463 }
2464 break;
2465
2466 case tsk_invalid_member_spec:
2467 /* The error has already been reported in
2468 check_specialization_scope. */
2469 return error_mark_node;
2470
2471 case tsk_invalid_expl_inst:
2472 error ("template parameter list used in explicit instantiation");
2473
2474 /* Fall through. */
2475
2476 case tsk_expl_inst:
2477 if (have_def)
2478 error ("definition provided for explicit instantiation");
2479
2480 explicit_instantiation = 1;
2481 break;
2482
2483 case tsk_excessive_parms:
2484 case tsk_insufficient_parms:
2485 if (tsk == tsk_excessive_parms)
2486 error ("too many template parameter lists in declaration of %qD",
2487 decl);
2488 else if (template_header_count)
2489 error("too few template parameter lists in declaration of %qD", decl);
2490 else
2491 error("explicit specialization of %qD must be introduced by "
2492 "%<template <>%>", decl);
2493
2494 /* Fall through. */
2495 case tsk_expl_spec:
2496 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2497 /* In cases like template<> constexpr bool v = true;
2498 We'll give an error in check_template_variable. */
2499 break;
2500
2501 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2502 if (ctype)
2503 member_specialization = 1;
2504 else
2505 specialization = 1;
2506 break;
2507
2508 case tsk_template:
2509 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2510 {
2511 /* This case handles bogus declarations like template <>
2512 template <class T> void f<int>(); */
2513
2514 if (!uses_template_parms (declarator))
2515 error ("template-id %qD in declaration of primary template",
2516 declarator);
2517 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2518 {
2519 /* Partial specialization of variable template. */
2520 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2521 specialization = 1;
2522 goto ok;
2523 }
2524 else if (cxx_dialect < cxx14)
2525 error ("non-type partial specialization %qD "
2526 "is not allowed", declarator);
2527 else
2528 error ("non-class, non-variable partial specialization %qD "
2529 "is not allowed", declarator);
2530 return decl;
2531 ok:;
2532 }
2533
2534 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2535 /* This is a specialization of a member template, without
2536 specialization the containing class. Something like:
2537
2538 template <class T> struct S {
2539 template <class U> void f (U);
2540 };
2541 template <> template <class U> void S<int>::f(U) {}
2542
2543 That's a specialization -- but of the entire template. */
2544 specialization = 1;
2545 break;
2546
2547 default:
2548 gcc_unreachable ();
2549 }
2550
2551 if ((specialization || member_specialization)
2552 /* This doesn't apply to variable templates. */
2553 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2554 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2555 {
2556 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2557 for (; t; t = TREE_CHAIN (t))
2558 if (TREE_PURPOSE (t))
2559 {
2560 permerror (input_location,
2561 "default argument specified in explicit specialization");
2562 break;
2563 }
2564 }
2565
2566 if (specialization || member_specialization || explicit_instantiation)
2567 {
2568 tree tmpl = NULL_TREE;
2569 tree targs = NULL_TREE;
2570 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2571
2572 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2573 if (!was_template_id)
2574 {
2575 tree fns;
2576
2577 gcc_assert (identifier_p (declarator));
2578 if (ctype)
2579 fns = dname;
2580 else
2581 {
2582 /* If there is no class context, the explicit instantiation
2583 must be at namespace scope. */
2584 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2585
2586 /* Find the namespace binding, using the declaration
2587 context. */
2588 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2589 false, true);
2590 if (fns == error_mark_node || !is_overloaded_fn (fns))
2591 {
2592 error ("%qD is not a template function", dname);
2593 fns = error_mark_node;
2594 }
2595 else
2596 {
2597 tree fn = OVL_CURRENT (fns);
2598 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2599 CP_DECL_CONTEXT (fn)))
2600 error ("%qD is not declared in %qD",
2601 decl, current_namespace);
2602 }
2603 }
2604
2605 declarator = lookup_template_function (fns, NULL_TREE);
2606 }
2607
2608 if (declarator == error_mark_node)
2609 return error_mark_node;
2610
2611 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2612 {
2613 if (!explicit_instantiation)
2614 /* A specialization in class scope. This is invalid,
2615 but the error will already have been flagged by
2616 check_specialization_scope. */
2617 return error_mark_node;
2618 else
2619 {
2620 /* It's not valid to write an explicit instantiation in
2621 class scope, e.g.:
2622
2623 class C { template void f(); }
2624
2625 This case is caught by the parser. However, on
2626 something like:
2627
2628 template class C { void f(); };
2629
2630 (which is invalid) we can get here. The error will be
2631 issued later. */
2632 ;
2633 }
2634
2635 return decl;
2636 }
2637 else if (ctype != NULL_TREE
2638 && (identifier_p (TREE_OPERAND (declarator, 0))))
2639 {
2640 // We'll match variable templates in start_decl.
2641 if (VAR_P (decl))
2642 return decl;
2643
2644 /* Find the list of functions in ctype that have the same
2645 name as the declared function. */
2646 tree name = TREE_OPERAND (declarator, 0);
2647 tree fns = NULL_TREE;
2648 int idx;
2649
2650 if (constructor_name_p (name, ctype))
2651 {
2652 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2653
2654 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2655 : !CLASSTYPE_DESTRUCTORS (ctype))
2656 {
2657 /* From [temp.expl.spec]:
2658
2659 If such an explicit specialization for the member
2660 of a class template names an implicitly-declared
2661 special member function (clause _special_), the
2662 program is ill-formed.
2663
2664 Similar language is found in [temp.explicit]. */
2665 error ("specialization of implicitly-declared special member function");
2666 return error_mark_node;
2667 }
2668
2669 name = is_constructor ? ctor_identifier : dtor_identifier;
2670 }
2671
2672 if (!DECL_CONV_FN_P (decl))
2673 {
2674 idx = lookup_fnfields_1 (ctype, name);
2675 if (idx >= 0)
2676 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2677 }
2678 else
2679 {
2680 vec<tree, va_gc> *methods;
2681 tree ovl;
2682
2683 /* For a type-conversion operator, we cannot do a
2684 name-based lookup. We might be looking for `operator
2685 int' which will be a specialization of `operator T'.
2686 So, we find *all* the conversion operators, and then
2687 select from them. */
2688 fns = NULL_TREE;
2689
2690 methods = CLASSTYPE_METHOD_VEC (ctype);
2691 if (methods)
2692 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2693 methods->iterate (idx, &ovl);
2694 ++idx)
2695 {
2696 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2697 /* There are no more conversion functions. */
2698 break;
2699
2700 /* Glue all these conversion functions together
2701 with those we already have. */
2702 for (; ovl; ovl = OVL_NEXT (ovl))
2703 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2704 }
2705 }
2706
2707 if (fns == NULL_TREE)
2708 {
2709 error ("no member function %qD declared in %qT", name, ctype);
2710 return error_mark_node;
2711 }
2712 else
2713 TREE_OPERAND (declarator, 0) = fns;
2714 }
2715
2716 /* Figure out what exactly is being specialized at this point.
2717 Note that for an explicit instantiation, even one for a
2718 member function, we cannot tell apriori whether the
2719 instantiation is for a member template, or just a member
2720 function of a template class. Even if a member template is
2721 being instantiated, the member template arguments may be
2722 elided if they can be deduced from the rest of the
2723 declaration. */
2724 tmpl = determine_specialization (declarator, decl,
2725 &targs,
2726 member_specialization,
2727 template_count,
2728 tsk);
2729
2730 if (!tmpl || tmpl == error_mark_node)
2731 /* We couldn't figure out what this declaration was
2732 specializing. */
2733 return error_mark_node;
2734 else
2735 {
2736 tree gen_tmpl = most_general_template (tmpl);
2737
2738 if (explicit_instantiation)
2739 {
2740 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2741 is done by do_decl_instantiation later. */
2742
2743 int arg_depth = TMPL_ARGS_DEPTH (targs);
2744 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2745
2746 if (arg_depth > parm_depth)
2747 {
2748 /* If TMPL is not the most general template (for
2749 example, if TMPL is a friend template that is
2750 injected into namespace scope), then there will
2751 be too many levels of TARGS. Remove some of them
2752 here. */
2753 int i;
2754 tree new_targs;
2755
2756 new_targs = make_tree_vec (parm_depth);
2757 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2758 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2759 = TREE_VEC_ELT (targs, i);
2760 targs = new_targs;
2761 }
2762
2763 return instantiate_template (tmpl, targs, tf_error);
2764 }
2765
2766 /* If we thought that the DECL was a member function, but it
2767 turns out to be specializing a static member function,
2768 make DECL a static member function as well. */
2769 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2770 && DECL_STATIC_FUNCTION_P (tmpl)
2771 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2772 revert_static_member_fn (decl);
2773
2774 /* If this is a specialization of a member template of a
2775 template class, we want to return the TEMPLATE_DECL, not
2776 the specialization of it. */
2777 if (tsk == tsk_template && !was_template_id)
2778 {
2779 tree result = DECL_TEMPLATE_RESULT (tmpl);
2780 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2781 DECL_INITIAL (result) = NULL_TREE;
2782 if (have_def)
2783 {
2784 tree parm;
2785 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2786 DECL_SOURCE_LOCATION (result)
2787 = DECL_SOURCE_LOCATION (decl);
2788 /* We want to use the argument list specified in the
2789 definition, not in the original declaration. */
2790 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2791 for (parm = DECL_ARGUMENTS (result); parm;
2792 parm = DECL_CHAIN (parm))
2793 DECL_CONTEXT (parm) = result;
2794 }
2795 return register_specialization (tmpl, gen_tmpl, targs,
2796 is_friend, 0);
2797 }
2798
2799 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2800 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2801
2802 if (was_template_id)
2803 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
2804
2805 /* Inherit default function arguments from the template
2806 DECL is specializing. */
2807 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2808 copy_default_args_to_explicit_spec (decl);
2809
2810 /* This specialization has the same protection as the
2811 template it specializes. */
2812 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2813 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2814
2815 /* 7.1.1-1 [dcl.stc]
2816
2817 A storage-class-specifier shall not be specified in an
2818 explicit specialization...
2819
2820 The parser rejects these, so unless action is taken here,
2821 explicit function specializations will always appear with
2822 global linkage.
2823
2824 The action recommended by the C++ CWG in response to C++
2825 defect report 605 is to make the storage class and linkage
2826 of the explicit specialization match the templated function:
2827
2828 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2829 */
2830 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2831 {
2832 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2833 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2834
2835 /* This specialization has the same linkage and visibility as
2836 the function template it specializes. */
2837 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2838 if (! TREE_PUBLIC (decl))
2839 {
2840 DECL_INTERFACE_KNOWN (decl) = 1;
2841 DECL_NOT_REALLY_EXTERN (decl) = 1;
2842 }
2843 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2844 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2845 {
2846 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2847 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2848 }
2849 }
2850
2851 /* If DECL is a friend declaration, declared using an
2852 unqualified name, the namespace associated with DECL may
2853 have been set incorrectly. For example, in:
2854
2855 template <typename T> void f(T);
2856 namespace N {
2857 struct S { friend void f<int>(int); }
2858 }
2859
2860 we will have set the DECL_CONTEXT for the friend
2861 declaration to N, rather than to the global namespace. */
2862 if (DECL_NAMESPACE_SCOPE_P (decl))
2863 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2864
2865 if (is_friend && !have_def)
2866 /* This is not really a declaration of a specialization.
2867 It's just the name of an instantiation. But, it's not
2868 a request for an instantiation, either. */
2869 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2870 else if (TREE_CODE (decl) == FUNCTION_DECL)
2871 /* A specialization is not necessarily COMDAT. */
2872 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
2873 && DECL_DECLARED_INLINE_P (decl));
2874 else if (TREE_CODE (decl) == VAR_DECL)
2875 DECL_COMDAT (decl) = false;
2876
2877 /* Register this specialization so that we can find it
2878 again. */
2879 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2880
2881 /* A 'structor should already have clones. */
2882 gcc_assert (decl == error_mark_node
2883 || variable_template_p (tmpl)
2884 || !(DECL_CONSTRUCTOR_P (decl)
2885 || DECL_DESTRUCTOR_P (decl))
2886 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2887 }
2888 }
2889
2890 return decl;
2891 }
2892
2893 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2894 parameters. These are represented in the same format used for
2895 DECL_TEMPLATE_PARMS. */
2896
2897 int
2898 comp_template_parms (const_tree parms1, const_tree parms2)
2899 {
2900 const_tree p1;
2901 const_tree p2;
2902
2903 if (parms1 == parms2)
2904 return 1;
2905
2906 for (p1 = parms1, p2 = parms2;
2907 p1 != NULL_TREE && p2 != NULL_TREE;
2908 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2909 {
2910 tree t1 = TREE_VALUE (p1);
2911 tree t2 = TREE_VALUE (p2);
2912 int i;
2913
2914 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2915 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2916
2917 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2918 return 0;
2919
2920 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2921 {
2922 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2923 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2924
2925 /* If either of the template parameters are invalid, assume
2926 they match for the sake of error recovery. */
2927 if (error_operand_p (parm1) || error_operand_p (parm2))
2928 return 1;
2929
2930 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2931 return 0;
2932
2933 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2934 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2935 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2936 continue;
2937 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2938 return 0;
2939 }
2940 }
2941
2942 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2943 /* One set of parameters has more parameters lists than the
2944 other. */
2945 return 0;
2946
2947 return 1;
2948 }
2949
2950 /* Determine whether PARM is a parameter pack. */
2951
2952 bool
2953 template_parameter_pack_p (const_tree parm)
2954 {
2955 /* Determine if we have a non-type template parameter pack. */
2956 if (TREE_CODE (parm) == PARM_DECL)
2957 return (DECL_TEMPLATE_PARM_P (parm)
2958 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2959 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2960 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2961
2962 /* If this is a list of template parameters, we could get a
2963 TYPE_DECL or a TEMPLATE_DECL. */
2964 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2965 parm = TREE_TYPE (parm);
2966
2967 /* Otherwise it must be a type template parameter. */
2968 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2969 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2970 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2971 }
2972
2973 /* Determine if T is a function parameter pack. */
2974
2975 bool
2976 function_parameter_pack_p (const_tree t)
2977 {
2978 if (t && TREE_CODE (t) == PARM_DECL)
2979 return DECL_PACK_P (t);
2980 return false;
2981 }
2982
2983 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2984 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2985
2986 tree
2987 get_function_template_decl (const_tree primary_func_tmpl_inst)
2988 {
2989 if (! primary_func_tmpl_inst
2990 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2991 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2992 return NULL;
2993
2994 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2995 }
2996
2997 /* Return true iff the function parameter PARAM_DECL was expanded
2998 from the function parameter pack PACK. */
2999
3000 bool
3001 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3002 {
3003 if (DECL_ARTIFICIAL (param_decl)
3004 || !function_parameter_pack_p (pack))
3005 return false;
3006
3007 /* The parameter pack and its pack arguments have the same
3008 DECL_PARM_INDEX. */
3009 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3010 }
3011
3012 /* Determine whether ARGS describes a variadic template args list,
3013 i.e., one that is terminated by a template argument pack. */
3014
3015 static bool
3016 template_args_variadic_p (tree args)
3017 {
3018 int nargs;
3019 tree last_parm;
3020
3021 if (args == NULL_TREE)
3022 return false;
3023
3024 args = INNERMOST_TEMPLATE_ARGS (args);
3025 nargs = TREE_VEC_LENGTH (args);
3026
3027 if (nargs == 0)
3028 return false;
3029
3030 last_parm = TREE_VEC_ELT (args, nargs - 1);
3031
3032 return ARGUMENT_PACK_P (last_parm);
3033 }
3034
3035 /* Generate a new name for the parameter pack name NAME (an
3036 IDENTIFIER_NODE) that incorporates its */
3037
3038 static tree
3039 make_ith_pack_parameter_name (tree name, int i)
3040 {
3041 /* Munge the name to include the parameter index. */
3042 #define NUMBUF_LEN 128
3043 char numbuf[NUMBUF_LEN];
3044 char* newname;
3045 int newname_len;
3046
3047 if (name == NULL_TREE)
3048 return name;
3049 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3050 newname_len = IDENTIFIER_LENGTH (name)
3051 + strlen (numbuf) + 2;
3052 newname = (char*)alloca (newname_len);
3053 snprintf (newname, newname_len,
3054 "%s#%i", IDENTIFIER_POINTER (name), i);
3055 return get_identifier (newname);
3056 }
3057
3058 /* Return true if T is a primary function, class or alias template
3059 instantiation. */
3060
3061 bool
3062 primary_template_instantiation_p (const_tree t)
3063 {
3064 if (!t)
3065 return false;
3066
3067 if (TREE_CODE (t) == FUNCTION_DECL)
3068 return DECL_LANG_SPECIFIC (t)
3069 && DECL_TEMPLATE_INSTANTIATION (t)
3070 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3071 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3072 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3073 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3074 else if (alias_template_specialization_p (t))
3075 return true;
3076 return false;
3077 }
3078
3079 /* Return true if PARM is a template template parameter. */
3080
3081 bool
3082 template_template_parameter_p (const_tree parm)
3083 {
3084 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3085 }
3086
3087 /* Return true iff PARM is a DECL representing a type template
3088 parameter. */
3089
3090 bool
3091 template_type_parameter_p (const_tree parm)
3092 {
3093 return (parm
3094 && (TREE_CODE (parm) == TYPE_DECL
3095 || TREE_CODE (parm) == TEMPLATE_DECL)
3096 && DECL_TEMPLATE_PARM_P (parm));
3097 }
3098
3099 /* Return the template parameters of T if T is a
3100 primary template instantiation, NULL otherwise. */
3101
3102 tree
3103 get_primary_template_innermost_parameters (const_tree t)
3104 {
3105 tree parms = NULL, template_info = NULL;
3106
3107 if ((template_info = get_template_info (t))
3108 && primary_template_instantiation_p (t))
3109 parms = INNERMOST_TEMPLATE_PARMS
3110 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3111
3112 return parms;
3113 }
3114
3115 /* Return the template parameters of the LEVELth level from the full list
3116 of template parameters PARMS. */
3117
3118 tree
3119 get_template_parms_at_level (tree parms, int level)
3120 {
3121 tree p;
3122 if (!parms
3123 || TREE_CODE (parms) != TREE_LIST
3124 || level > TMPL_PARMS_DEPTH (parms))
3125 return NULL_TREE;
3126
3127 for (p = parms; p; p = TREE_CHAIN (p))
3128 if (TMPL_PARMS_DEPTH (p) == level)
3129 return p;
3130
3131 return NULL_TREE;
3132 }
3133
3134 /* Returns the template arguments of T if T is a template instantiation,
3135 NULL otherwise. */
3136
3137 tree
3138 get_template_innermost_arguments (const_tree t)
3139 {
3140 tree args = NULL, template_info = NULL;
3141
3142 if ((template_info = get_template_info (t))
3143 && TI_ARGS (template_info))
3144 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3145
3146 return args;
3147 }
3148
3149 /* Return the argument pack elements of T if T is a template argument pack,
3150 NULL otherwise. */
3151
3152 tree
3153 get_template_argument_pack_elems (const_tree t)
3154 {
3155 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3156 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3157 return NULL;
3158
3159 return ARGUMENT_PACK_ARGS (t);
3160 }
3161
3162 /* Structure used to track the progress of find_parameter_packs_r. */
3163 struct find_parameter_pack_data
3164 {
3165 /* TREE_LIST that will contain all of the parameter packs found by
3166 the traversal. */
3167 tree* parameter_packs;
3168
3169 /* Set of AST nodes that have been visited by the traversal. */
3170 hash_set<tree> *visited;
3171 };
3172
3173 /* Identifies all of the argument packs that occur in a template
3174 argument and appends them to the TREE_LIST inside DATA, which is a
3175 find_parameter_pack_data structure. This is a subroutine of
3176 make_pack_expansion and uses_parameter_packs. */
3177 static tree
3178 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3179 {
3180 tree t = *tp;
3181 struct find_parameter_pack_data* ppd =
3182 (struct find_parameter_pack_data*)data;
3183 bool parameter_pack_p = false;
3184
3185 /* Handle type aliases/typedefs. */
3186 if (TYPE_ALIAS_P (t))
3187 {
3188 if (TYPE_TEMPLATE_INFO (t))
3189 cp_walk_tree (&TYPE_TI_ARGS (t),
3190 &find_parameter_packs_r,
3191 ppd, ppd->visited);
3192 *walk_subtrees = 0;
3193 return NULL_TREE;
3194 }
3195
3196 /* Identify whether this is a parameter pack or not. */
3197 switch (TREE_CODE (t))
3198 {
3199 case TEMPLATE_PARM_INDEX:
3200 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3201 parameter_pack_p = true;
3202 break;
3203
3204 case TEMPLATE_TYPE_PARM:
3205 t = TYPE_MAIN_VARIANT (t);
3206 case TEMPLATE_TEMPLATE_PARM:
3207 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3208 parameter_pack_p = true;
3209 break;
3210
3211 case FIELD_DECL:
3212 case PARM_DECL:
3213 if (DECL_PACK_P (t))
3214 {
3215 /* We don't want to walk into the type of a PARM_DECL,
3216 because we don't want to see the type parameter pack. */
3217 *walk_subtrees = 0;
3218 parameter_pack_p = true;
3219 }
3220 break;
3221
3222 /* Look through a lambda capture proxy to the field pack. */
3223 case VAR_DECL:
3224 if (DECL_HAS_VALUE_EXPR_P (t))
3225 {
3226 tree v = DECL_VALUE_EXPR (t);
3227 cp_walk_tree (&v,
3228 &find_parameter_packs_r,
3229 ppd, ppd->visited);
3230 *walk_subtrees = 0;
3231 }
3232 break;
3233
3234 case BASES:
3235 parameter_pack_p = true;
3236 break;
3237 default:
3238 /* Not a parameter pack. */
3239 break;
3240 }
3241
3242 if (parameter_pack_p)
3243 {
3244 /* Add this parameter pack to the list. */
3245 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3246 }
3247
3248 if (TYPE_P (t))
3249 cp_walk_tree (&TYPE_CONTEXT (t),
3250 &find_parameter_packs_r, ppd, ppd->visited);
3251
3252 /* This switch statement will return immediately if we don't find a
3253 parameter pack. */
3254 switch (TREE_CODE (t))
3255 {
3256 case TEMPLATE_PARM_INDEX:
3257 return NULL_TREE;
3258
3259 case BOUND_TEMPLATE_TEMPLATE_PARM:
3260 /* Check the template itself. */
3261 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3262 &find_parameter_packs_r, ppd, ppd->visited);
3263 /* Check the template arguments. */
3264 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3265 ppd->visited);
3266 *walk_subtrees = 0;
3267 return NULL_TREE;
3268
3269 case TEMPLATE_TYPE_PARM:
3270 case TEMPLATE_TEMPLATE_PARM:
3271 return NULL_TREE;
3272
3273 case PARM_DECL:
3274 return NULL_TREE;
3275
3276 case RECORD_TYPE:
3277 if (TYPE_PTRMEMFUNC_P (t))
3278 return NULL_TREE;
3279 /* Fall through. */
3280
3281 case UNION_TYPE:
3282 case ENUMERAL_TYPE:
3283 if (TYPE_TEMPLATE_INFO (t))
3284 cp_walk_tree (&TYPE_TI_ARGS (t),
3285 &find_parameter_packs_r, ppd, ppd->visited);
3286
3287 *walk_subtrees = 0;
3288 return NULL_TREE;
3289
3290 case CONSTRUCTOR:
3291 case TEMPLATE_DECL:
3292 cp_walk_tree (&TREE_TYPE (t),
3293 &find_parameter_packs_r, ppd, ppd->visited);
3294 return NULL_TREE;
3295
3296 case TYPENAME_TYPE:
3297 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3298 ppd, ppd->visited);
3299 *walk_subtrees = 0;
3300 return NULL_TREE;
3301
3302 case TYPE_PACK_EXPANSION:
3303 case EXPR_PACK_EXPANSION:
3304 *walk_subtrees = 0;
3305 return NULL_TREE;
3306
3307 case INTEGER_TYPE:
3308 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3309 ppd, ppd->visited);
3310 *walk_subtrees = 0;
3311 return NULL_TREE;
3312
3313 case IDENTIFIER_NODE:
3314 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3315 ppd->visited);
3316 *walk_subtrees = 0;
3317 return NULL_TREE;
3318
3319 default:
3320 return NULL_TREE;
3321 }
3322
3323 return NULL_TREE;
3324 }
3325
3326 /* Determines if the expression or type T uses any parameter packs. */
3327 bool
3328 uses_parameter_packs (tree t)
3329 {
3330 tree parameter_packs = NULL_TREE;
3331 struct find_parameter_pack_data ppd;
3332 ppd.parameter_packs = &parameter_packs;
3333 ppd.visited = new hash_set<tree>;
3334 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3335 delete ppd.visited;
3336 return parameter_packs != NULL_TREE;
3337 }
3338
3339 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3340 representation a base-class initializer into a parameter pack
3341 expansion. If all goes well, the resulting node will be an
3342 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3343 respectively. */
3344 tree
3345 make_pack_expansion (tree arg)
3346 {
3347 tree result;
3348 tree parameter_packs = NULL_TREE;
3349 bool for_types = false;
3350 struct find_parameter_pack_data ppd;
3351
3352 if (!arg || arg == error_mark_node)
3353 return arg;
3354
3355 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3356 {
3357 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3358 class initializer. In this case, the TREE_PURPOSE will be a
3359 _TYPE node (representing the base class expansion we're
3360 initializing) and the TREE_VALUE will be a TREE_LIST
3361 containing the initialization arguments.
3362
3363 The resulting expansion looks somewhat different from most
3364 expansions. Rather than returning just one _EXPANSION, we
3365 return a TREE_LIST whose TREE_PURPOSE is a
3366 TYPE_PACK_EXPANSION containing the bases that will be
3367 initialized. The TREE_VALUE will be identical to the
3368 original TREE_VALUE, which is a list of arguments that will
3369 be passed to each base. We do not introduce any new pack
3370 expansion nodes into the TREE_VALUE (although it is possible
3371 that some already exist), because the TREE_PURPOSE and
3372 TREE_VALUE all need to be expanded together with the same
3373 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3374 resulting TREE_PURPOSE will mention the parameter packs in
3375 both the bases and the arguments to the bases. */
3376 tree purpose;
3377 tree value;
3378 tree parameter_packs = NULL_TREE;
3379
3380 /* Determine which parameter packs will be used by the base
3381 class expansion. */
3382 ppd.visited = new hash_set<tree>;
3383 ppd.parameter_packs = &parameter_packs;
3384 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3385 &ppd, ppd.visited);
3386
3387 if (parameter_packs == NULL_TREE)
3388 {
3389 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3390 delete ppd.visited;
3391 return error_mark_node;
3392 }
3393
3394 if (TREE_VALUE (arg) != void_type_node)
3395 {
3396 /* Collect the sets of parameter packs used in each of the
3397 initialization arguments. */
3398 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3399 {
3400 /* Determine which parameter packs will be expanded in this
3401 argument. */
3402 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3403 &ppd, ppd.visited);
3404 }
3405 }
3406
3407 delete ppd.visited;
3408
3409 /* Create the pack expansion type for the base type. */
3410 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3411 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3412 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3413
3414 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3415 they will rarely be compared to anything. */
3416 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3417
3418 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3419 }
3420
3421 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3422 for_types = true;
3423
3424 /* Build the PACK_EXPANSION_* node. */
3425 result = for_types
3426 ? cxx_make_type (TYPE_PACK_EXPANSION)
3427 : make_node (EXPR_PACK_EXPANSION);
3428 SET_PACK_EXPANSION_PATTERN (result, arg);
3429 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3430 {
3431 /* Propagate type and const-expression information. */
3432 TREE_TYPE (result) = TREE_TYPE (arg);
3433 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3434 }
3435 else
3436 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3437 they will rarely be compared to anything. */
3438 SET_TYPE_STRUCTURAL_EQUALITY (result);
3439
3440 /* Determine which parameter packs will be expanded. */
3441 ppd.parameter_packs = &parameter_packs;
3442 ppd.visited = new hash_set<tree>;
3443 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3444 delete ppd.visited;
3445
3446 /* Make sure we found some parameter packs. */
3447 if (parameter_packs == NULL_TREE)
3448 {
3449 if (TYPE_P (arg))
3450 error ("expansion pattern %<%T%> contains no argument packs", arg);
3451 else
3452 error ("expansion pattern %<%E%> contains no argument packs", arg);
3453 return error_mark_node;
3454 }
3455 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3456
3457 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3458
3459 return result;
3460 }
3461
3462 /* Checks T for any "bare" parameter packs, which have not yet been
3463 expanded, and issues an error if any are found. This operation can
3464 only be done on full expressions or types (e.g., an expression
3465 statement, "if" condition, etc.), because we could have expressions like:
3466
3467 foo(f(g(h(args)))...)
3468
3469 where "args" is a parameter pack. check_for_bare_parameter_packs
3470 should not be called for the subexpressions args, h(args),
3471 g(h(args)), or f(g(h(args))), because we would produce erroneous
3472 error messages.
3473
3474 Returns TRUE and emits an error if there were bare parameter packs,
3475 returns FALSE otherwise. */
3476 bool
3477 check_for_bare_parameter_packs (tree t)
3478 {
3479 tree parameter_packs = NULL_TREE;
3480 struct find_parameter_pack_data ppd;
3481
3482 if (!processing_template_decl || !t || t == error_mark_node)
3483 return false;
3484
3485 if (TREE_CODE (t) == TYPE_DECL)
3486 t = TREE_TYPE (t);
3487
3488 ppd.parameter_packs = &parameter_packs;
3489 ppd.visited = new hash_set<tree>;
3490 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3491 delete ppd.visited;
3492
3493 if (parameter_packs)
3494 {
3495 error ("parameter packs not expanded with %<...%>:");
3496 while (parameter_packs)
3497 {
3498 tree pack = TREE_VALUE (parameter_packs);
3499 tree name = NULL_TREE;
3500
3501 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3502 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3503 name = TYPE_NAME (pack);
3504 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3505 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3506 else
3507 name = DECL_NAME (pack);
3508
3509 if (name)
3510 inform (input_location, " %qD", name);
3511 else
3512 inform (input_location, " <anonymous>");
3513
3514 parameter_packs = TREE_CHAIN (parameter_packs);
3515 }
3516
3517 return true;
3518 }
3519
3520 return false;
3521 }
3522
3523 /* Expand any parameter packs that occur in the template arguments in
3524 ARGS. */
3525 tree
3526 expand_template_argument_pack (tree args)
3527 {
3528 tree result_args = NULL_TREE;
3529 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3530 int num_result_args = -1;
3531 int non_default_args_count = -1;
3532
3533 /* First, determine if we need to expand anything, and the number of
3534 slots we'll need. */
3535 for (in_arg = 0; in_arg < nargs; ++in_arg)
3536 {
3537 tree arg = TREE_VEC_ELT (args, in_arg);
3538 if (arg == NULL_TREE)
3539 return args;
3540 if (ARGUMENT_PACK_P (arg))
3541 {
3542 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3543 if (num_result_args < 0)
3544 num_result_args = in_arg + num_packed;
3545 else
3546 num_result_args += num_packed;
3547 }
3548 else
3549 {
3550 if (num_result_args >= 0)
3551 num_result_args++;
3552 }
3553 }
3554
3555 /* If no expansion is necessary, we're done. */
3556 if (num_result_args < 0)
3557 return args;
3558
3559 /* Expand arguments. */
3560 result_args = make_tree_vec (num_result_args);
3561 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3562 non_default_args_count =
3563 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3564 for (in_arg = 0; in_arg < nargs; ++in_arg)
3565 {
3566 tree arg = TREE_VEC_ELT (args, in_arg);
3567 if (ARGUMENT_PACK_P (arg))
3568 {
3569 tree packed = ARGUMENT_PACK_ARGS (arg);
3570 int i, num_packed = TREE_VEC_LENGTH (packed);
3571 for (i = 0; i < num_packed; ++i, ++out_arg)
3572 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3573 if (non_default_args_count > 0)
3574 non_default_args_count += num_packed - 1;
3575 }
3576 else
3577 {
3578 TREE_VEC_ELT (result_args, out_arg) = arg;
3579 ++out_arg;
3580 }
3581 }
3582 if (non_default_args_count >= 0)
3583 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3584 return result_args;
3585 }
3586
3587 /* Checks if DECL shadows a template parameter.
3588
3589 [temp.local]: A template-parameter shall not be redeclared within its
3590 scope (including nested scopes).
3591
3592 Emits an error and returns TRUE if the DECL shadows a parameter,
3593 returns FALSE otherwise. */
3594
3595 bool
3596 check_template_shadow (tree decl)
3597 {
3598 tree olddecl;
3599
3600 /* If we're not in a template, we can't possibly shadow a template
3601 parameter. */
3602 if (!current_template_parms)
3603 return true;
3604
3605 /* Figure out what we're shadowing. */
3606 if (TREE_CODE (decl) == OVERLOAD)
3607 decl = OVL_CURRENT (decl);
3608 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3609
3610 /* If there's no previous binding for this name, we're not shadowing
3611 anything, let alone a template parameter. */
3612 if (!olddecl)
3613 return true;
3614
3615 /* If we're not shadowing a template parameter, we're done. Note
3616 that OLDDECL might be an OVERLOAD (or perhaps even an
3617 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3618 node. */
3619 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3620 return true;
3621
3622 /* We check for decl != olddecl to avoid bogus errors for using a
3623 name inside a class. We check TPFI to avoid duplicate errors for
3624 inline member templates. */
3625 if (decl == olddecl
3626 || (DECL_TEMPLATE_PARM_P (decl)
3627 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3628 return true;
3629
3630 /* Don't complain about the injected class name, as we've already
3631 complained about the class itself. */
3632 if (DECL_SELF_REFERENCE_P (decl))
3633 return false;
3634
3635 error ("declaration of %q+#D", decl);
3636 error (" shadows template parm %q+#D", olddecl);
3637 return false;
3638 }
3639
3640 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3641 ORIG_LEVEL, DECL, and TYPE. */
3642
3643 static tree
3644 build_template_parm_index (int index,
3645 int level,
3646 int orig_level,
3647 tree decl,
3648 tree type)
3649 {
3650 tree t = make_node (TEMPLATE_PARM_INDEX);
3651 TEMPLATE_PARM_IDX (t) = index;
3652 TEMPLATE_PARM_LEVEL (t) = level;
3653 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3654 TEMPLATE_PARM_DECL (t) = decl;
3655 TREE_TYPE (t) = type;
3656 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3657 TREE_READONLY (t) = TREE_READONLY (decl);
3658
3659 return t;
3660 }
3661
3662 /* Find the canonical type parameter for the given template type
3663 parameter. Returns the canonical type parameter, which may be TYPE
3664 if no such parameter existed. */
3665
3666 static tree
3667 canonical_type_parameter (tree type)
3668 {
3669 tree list;
3670 int idx = TEMPLATE_TYPE_IDX (type);
3671 if (!canonical_template_parms)
3672 vec_alloc (canonical_template_parms, idx+1);
3673
3674 while (canonical_template_parms->length () <= (unsigned)idx)
3675 vec_safe_push (canonical_template_parms, NULL_TREE);
3676
3677 list = (*canonical_template_parms)[idx];
3678 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3679 list = TREE_CHAIN (list);
3680
3681 if (list)
3682 return TREE_VALUE (list);
3683 else
3684 {
3685 (*canonical_template_parms)[idx]
3686 = tree_cons (NULL_TREE, type,
3687 (*canonical_template_parms)[idx]);
3688 return type;
3689 }
3690 }
3691
3692 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3693 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3694 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3695 new one is created. */
3696
3697 static tree
3698 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3699 tsubst_flags_t complain)
3700 {
3701 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3702 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3703 != TEMPLATE_PARM_LEVEL (index) - levels)
3704 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3705 {
3706 tree orig_decl = TEMPLATE_PARM_DECL (index);
3707 tree decl, t;
3708
3709 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3710 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3711 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3712 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3713 DECL_ARTIFICIAL (decl) = 1;
3714 SET_DECL_TEMPLATE_PARM_P (decl);
3715
3716 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3717 TEMPLATE_PARM_LEVEL (index) - levels,
3718 TEMPLATE_PARM_ORIG_LEVEL (index),
3719 decl, type);
3720 TEMPLATE_PARM_DESCENDANTS (index) = t;
3721 TEMPLATE_PARM_PARAMETER_PACK (t)
3722 = TEMPLATE_PARM_PARAMETER_PACK (index);
3723
3724 /* Template template parameters need this. */
3725 if (TREE_CODE (decl) == TEMPLATE_DECL)
3726 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3727 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3728 args, complain);
3729 }
3730
3731 return TEMPLATE_PARM_DESCENDANTS (index);
3732 }
3733
3734 /* Process information from new template parameter PARM and append it
3735 to the LIST being built. This new parameter is a non-type
3736 parameter iff IS_NON_TYPE is true. This new parameter is a
3737 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3738 is in PARM_LOC. */
3739
3740 tree
3741 process_template_parm (tree list, location_t parm_loc, tree parm,
3742 bool is_non_type, bool is_parameter_pack)
3743 {
3744 tree decl = 0;
3745 tree defval;
3746 int idx = 0;
3747
3748 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3749 defval = TREE_PURPOSE (parm);
3750
3751 if (list)
3752 {
3753 tree p = tree_last (list);
3754
3755 if (p && TREE_VALUE (p) != error_mark_node)
3756 {
3757 p = TREE_VALUE (p);
3758 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3759 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3760 else
3761 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3762 }
3763
3764 ++idx;
3765 }
3766
3767 if (is_non_type)
3768 {
3769 parm = TREE_VALUE (parm);
3770
3771 SET_DECL_TEMPLATE_PARM_P (parm);
3772
3773 if (TREE_TYPE (parm) != error_mark_node)
3774 {
3775 /* [temp.param]
3776
3777 The top-level cv-qualifiers on the template-parameter are
3778 ignored when determining its type. */
3779 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3780 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3781 TREE_TYPE (parm) = error_mark_node;
3782 else if (uses_parameter_packs (TREE_TYPE (parm))
3783 && !is_parameter_pack
3784 /* If we're in a nested template parameter list, the template
3785 template parameter could be a parameter pack. */
3786 && processing_template_parmlist == 1)
3787 {
3788 /* This template parameter is not a parameter pack, but it
3789 should be. Complain about "bare" parameter packs. */
3790 check_for_bare_parameter_packs (TREE_TYPE (parm));
3791
3792 /* Recover by calling this a parameter pack. */
3793 is_parameter_pack = true;
3794 }
3795 }
3796
3797 /* A template parameter is not modifiable. */
3798 TREE_CONSTANT (parm) = 1;
3799 TREE_READONLY (parm) = 1;
3800 decl = build_decl (parm_loc,
3801 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3802 TREE_CONSTANT (decl) = 1;
3803 TREE_READONLY (decl) = 1;
3804 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3805 = build_template_parm_index (idx, processing_template_decl,
3806 processing_template_decl,
3807 decl, TREE_TYPE (parm));
3808
3809 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3810 = is_parameter_pack;
3811 }
3812 else
3813 {
3814 tree t;
3815 parm = TREE_VALUE (TREE_VALUE (parm));
3816
3817 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3818 {
3819 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3820 /* This is for distinguishing between real templates and template
3821 template parameters */
3822 TREE_TYPE (parm) = t;
3823 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3824 decl = parm;
3825 }
3826 else
3827 {
3828 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3829 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3830 decl = build_decl (parm_loc,
3831 TYPE_DECL, parm, t);
3832 }
3833
3834 TYPE_NAME (t) = decl;
3835 TYPE_STUB_DECL (t) = decl;
3836 parm = decl;
3837 TEMPLATE_TYPE_PARM_INDEX (t)
3838 = build_template_parm_index (idx, processing_template_decl,
3839 processing_template_decl,
3840 decl, TREE_TYPE (parm));
3841 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3842 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3843 }
3844 DECL_ARTIFICIAL (decl) = 1;
3845 SET_DECL_TEMPLATE_PARM_P (decl);
3846 pushdecl (decl);
3847 parm = build_tree_list (defval, parm);
3848 return chainon (list, parm);
3849 }
3850
3851 /* The end of a template parameter list has been reached. Process the
3852 tree list into a parameter vector, converting each parameter into a more
3853 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3854 as PARM_DECLs. */
3855
3856 tree
3857 end_template_parm_list (tree parms)
3858 {
3859 int nparms;
3860 tree parm, next;
3861 tree saved_parmlist = make_tree_vec (list_length (parms));
3862
3863 current_template_parms
3864 = tree_cons (size_int (processing_template_decl),
3865 saved_parmlist, current_template_parms);
3866
3867 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3868 {
3869 next = TREE_CHAIN (parm);
3870 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3871 TREE_CHAIN (parm) = NULL_TREE;
3872 }
3873
3874 --processing_template_parmlist;
3875
3876 return saved_parmlist;
3877 }
3878
3879 /* end_template_decl is called after a template declaration is seen. */
3880
3881 void
3882 end_template_decl (void)
3883 {
3884 reset_specialization ();
3885
3886 if (! processing_template_decl)
3887 return;
3888
3889 /* This matches the pushlevel in begin_template_parm_list. */
3890 finish_scope ();
3891
3892 --processing_template_decl;
3893 current_template_parms = TREE_CHAIN (current_template_parms);
3894 }
3895
3896 /* Takes a TREE_LIST representing a template parameter and convert it
3897 into an argument suitable to be passed to the type substitution
3898 functions. Note that If the TREE_LIST contains an error_mark
3899 node, the returned argument is error_mark_node. */
3900
3901 static tree
3902 template_parm_to_arg (tree t)
3903 {
3904
3905 if (t == NULL_TREE
3906 || TREE_CODE (t) != TREE_LIST)
3907 return t;
3908
3909 if (error_operand_p (TREE_VALUE (t)))
3910 return error_mark_node;
3911
3912 t = TREE_VALUE (t);
3913
3914 if (TREE_CODE (t) == TYPE_DECL
3915 || TREE_CODE (t) == TEMPLATE_DECL)
3916 {
3917 t = TREE_TYPE (t);
3918
3919 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3920 {
3921 /* Turn this argument into a TYPE_ARGUMENT_PACK
3922 with a single element, which expands T. */
3923 tree vec = make_tree_vec (1);
3924 #ifdef ENABLE_CHECKING
3925 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3926 (vec, TREE_VEC_LENGTH (vec));
3927 #endif
3928 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3929
3930 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3931 SET_ARGUMENT_PACK_ARGS (t, vec);
3932 }
3933 }
3934 else
3935 {
3936 t = DECL_INITIAL (t);
3937
3938 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3939 {
3940 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3941 with a single element, which expands T. */
3942 tree vec = make_tree_vec (1);
3943 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3944 #ifdef ENABLE_CHECKING
3945 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3946 (vec, TREE_VEC_LENGTH (vec));
3947 #endif
3948 t = convert_from_reference (t);
3949 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3950
3951 t = make_node (NONTYPE_ARGUMENT_PACK);
3952 SET_ARGUMENT_PACK_ARGS (t, vec);
3953 TREE_TYPE (t) = type;
3954 }
3955 else
3956 t = convert_from_reference (t);
3957 }
3958 return t;
3959 }
3960
3961 /* Given a set of template parameters, return them as a set of template
3962 arguments. The template parameters are represented as a TREE_VEC, in
3963 the form documented in cp-tree.h for template arguments. */
3964
3965 static tree
3966 template_parms_to_args (tree parms)
3967 {
3968 tree header;
3969 tree args = NULL_TREE;
3970 int length = TMPL_PARMS_DEPTH (parms);
3971 int l = length;
3972
3973 /* If there is only one level of template parameters, we do not
3974 create a TREE_VEC of TREE_VECs. Instead, we return a single
3975 TREE_VEC containing the arguments. */
3976 if (length > 1)
3977 args = make_tree_vec (length);
3978
3979 for (header = parms; header; header = TREE_CHAIN (header))
3980 {
3981 tree a = copy_node (TREE_VALUE (header));
3982 int i;
3983
3984 TREE_TYPE (a) = NULL_TREE;
3985 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3986 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3987
3988 #ifdef ENABLE_CHECKING
3989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3990 #endif
3991
3992 if (length > 1)
3993 TREE_VEC_ELT (args, --l) = a;
3994 else
3995 args = a;
3996 }
3997
3998 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3999 /* This can happen for template parms of a template template
4000 parameter, e.g:
4001
4002 template<template<class T, class U> class TT> struct S;
4003
4004 Consider the level of the parms of TT; T and U both have
4005 level 2; TT has no template parm of level 1. So in this case
4006 the first element of full_template_args is NULL_TREE. If we
4007 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
4008 of 2. This will make tsubst wrongly consider that T and U
4009 have level 1. Instead, let's create a dummy vector as the
4010 first element of full_template_args so that TMPL_ARGS_DEPTH
4011 returns the correct depth for args. */
4012 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4013 return args;
4014 }
4015
4016 /* Within the declaration of a template, return the currently active
4017 template parameters as an argument TREE_VEC. */
4018
4019 static tree
4020 current_template_args (void)
4021 {
4022 return template_parms_to_args (current_template_parms);
4023 }
4024
4025 /* Update the declared TYPE by doing any lookups which were thought to be
4026 dependent, but are not now that we know the SCOPE of the declarator. */
4027
4028 tree
4029 maybe_update_decl_type (tree orig_type, tree scope)
4030 {
4031 tree type = orig_type;
4032
4033 if (type == NULL_TREE)
4034 return type;
4035
4036 if (TREE_CODE (orig_type) == TYPE_DECL)
4037 type = TREE_TYPE (type);
4038
4039 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4040 && dependent_type_p (type)
4041 /* Don't bother building up the args in this case. */
4042 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4043 {
4044 /* tsubst in the args corresponding to the template parameters,
4045 including auto if present. Most things will be unchanged, but
4046 make_typename_type and tsubst_qualified_id will resolve
4047 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4048 tree args = current_template_args ();
4049 tree auto_node = type_uses_auto (type);
4050 tree pushed;
4051 if (auto_node)
4052 {
4053 tree auto_vec = make_tree_vec (1);
4054 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4055 args = add_to_template_args (args, auto_vec);
4056 }
4057 pushed = push_scope (scope);
4058 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4059 if (pushed)
4060 pop_scope (scope);
4061 }
4062
4063 if (type == error_mark_node)
4064 return orig_type;
4065
4066 if (TREE_CODE (orig_type) == TYPE_DECL)
4067 {
4068 if (same_type_p (type, TREE_TYPE (orig_type)))
4069 type = orig_type;
4070 else
4071 type = TYPE_NAME (type);
4072 }
4073 return type;
4074 }
4075
4076 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4077 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4078 a member template. Used by push_template_decl below. */
4079
4080 static tree
4081 build_template_decl (tree decl, tree parms, bool member_template_p)
4082 {
4083 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4084 DECL_TEMPLATE_PARMS (tmpl) = parms;
4085 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4086 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4087 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4088
4089 return tmpl;
4090 }
4091
4092 struct template_parm_data
4093 {
4094 /* The level of the template parameters we are currently
4095 processing. */
4096 int level;
4097
4098 /* The index of the specialization argument we are currently
4099 processing. */
4100 int current_arg;
4101
4102 /* An array whose size is the number of template parameters. The
4103 elements are nonzero if the parameter has been used in any one
4104 of the arguments processed so far. */
4105 int* parms;
4106
4107 /* An array whose size is the number of template arguments. The
4108 elements are nonzero if the argument makes use of template
4109 parameters of this level. */
4110 int* arg_uses_template_parms;
4111 };
4112
4113 /* Subroutine of push_template_decl used to see if each template
4114 parameter in a partial specialization is used in the explicit
4115 argument list. If T is of the LEVEL given in DATA (which is
4116 treated as a template_parm_data*), then DATA->PARMS is marked
4117 appropriately. */
4118
4119 static int
4120 mark_template_parm (tree t, void* data)
4121 {
4122 int level;
4123 int idx;
4124 struct template_parm_data* tpd = (struct template_parm_data*) data;
4125
4126 template_parm_level_and_index (t, &level, &idx);
4127
4128 if (level == tpd->level)
4129 {
4130 tpd->parms[idx] = 1;
4131 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4132 }
4133
4134 /* Return zero so that for_each_template_parm will continue the
4135 traversal of the tree; we want to mark *every* template parm. */
4136 return 0;
4137 }
4138
4139 /* Process the partial specialization DECL. */
4140
4141 static tree
4142 process_partial_specialization (tree decl)
4143 {
4144 tree type = TREE_TYPE (decl);
4145 tree tinfo = get_template_info (decl);
4146 tree maintmpl = TI_TEMPLATE (tinfo);
4147 tree specargs = TI_ARGS (tinfo);
4148 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4149 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4150 tree inner_parms;
4151 tree inst;
4152 int nargs = TREE_VEC_LENGTH (inner_args);
4153 int ntparms;
4154 int i;
4155 bool did_error_intro = false;
4156 struct template_parm_data tpd;
4157 struct template_parm_data tpd2;
4158
4159 gcc_assert (current_template_parms);
4160
4161 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4162 ntparms = TREE_VEC_LENGTH (inner_parms);
4163
4164 /* We check that each of the template parameters given in the
4165 partial specialization is used in the argument list to the
4166 specialization. For example:
4167
4168 template <class T> struct S;
4169 template <class T> struct S<T*>;
4170
4171 The second declaration is OK because `T*' uses the template
4172 parameter T, whereas
4173
4174 template <class T> struct S<int>;
4175
4176 is no good. Even trickier is:
4177
4178 template <class T>
4179 struct S1
4180 {
4181 template <class U>
4182 struct S2;
4183 template <class U>
4184 struct S2<T>;
4185 };
4186
4187 The S2<T> declaration is actually invalid; it is a
4188 full-specialization. Of course,
4189
4190 template <class U>
4191 struct S2<T (*)(U)>;
4192
4193 or some such would have been OK. */
4194 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4195 tpd.parms = XALLOCAVEC (int, ntparms);
4196 memset (tpd.parms, 0, sizeof (int) * ntparms);
4197
4198 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4199 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4200 for (i = 0; i < nargs; ++i)
4201 {
4202 tpd.current_arg = i;
4203 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4204 &mark_template_parm,
4205 &tpd,
4206 NULL,
4207 /*include_nondeduced_p=*/false);
4208 }
4209 for (i = 0; i < ntparms; ++i)
4210 if (tpd.parms[i] == 0)
4211 {
4212 /* One of the template parms was not used in a deduced context in the
4213 specialization. */
4214 if (!did_error_intro)
4215 {
4216 error ("template parameters not deducible in "
4217 "partial specialization:");
4218 did_error_intro = true;
4219 }
4220
4221 inform (input_location, " %qD",
4222 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4223 }
4224
4225 if (did_error_intro)
4226 return error_mark_node;
4227
4228 /* [temp.class.spec]
4229
4230 The argument list of the specialization shall not be identical to
4231 the implicit argument list of the primary template. */
4232 tree main_args
4233 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4234 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)))
4235 error ("partial specialization %qD does not specialize "
4236 "any template arguments", decl);
4237
4238 /* A partial specialization that replaces multiple parameters of the
4239 primary template with a pack expansion is less specialized for those
4240 parameters. */
4241 if (nargs < DECL_NTPARMS (maintmpl))
4242 {
4243 error ("partial specialization is not more specialized than the "
4244 "primary template because it replaces multiple parameters "
4245 "with a pack expansion");
4246 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4247 return decl;
4248 }
4249
4250 /* [temp.class.spec]
4251
4252 A partially specialized non-type argument expression shall not
4253 involve template parameters of the partial specialization except
4254 when the argument expression is a simple identifier.
4255
4256 The type of a template parameter corresponding to a specialized
4257 non-type argument shall not be dependent on a parameter of the
4258 specialization.
4259
4260 Also, we verify that pack expansions only occur at the
4261 end of the argument list. */
4262 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4263 tpd2.parms = 0;
4264 for (i = 0; i < nargs; ++i)
4265 {
4266 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4267 tree arg = TREE_VEC_ELT (inner_args, i);
4268 tree packed_args = NULL_TREE;
4269 int j, len = 1;
4270
4271 if (ARGUMENT_PACK_P (arg))
4272 {
4273 /* Extract the arguments from the argument pack. We'll be
4274 iterating over these in the following loop. */
4275 packed_args = ARGUMENT_PACK_ARGS (arg);
4276 len = TREE_VEC_LENGTH (packed_args);
4277 }
4278
4279 for (j = 0; j < len; j++)
4280 {
4281 if (packed_args)
4282 /* Get the Jth argument in the parameter pack. */
4283 arg = TREE_VEC_ELT (packed_args, j);
4284
4285 if (PACK_EXPANSION_P (arg))
4286 {
4287 /* Pack expansions must come at the end of the
4288 argument list. */
4289 if ((packed_args && j < len - 1)
4290 || (!packed_args && i < nargs - 1))
4291 {
4292 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4293 error ("parameter pack argument %qE must be at the "
4294 "end of the template argument list", arg);
4295 else
4296 error ("parameter pack argument %qT must be at the "
4297 "end of the template argument list", arg);
4298 }
4299 }
4300
4301 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4302 /* We only care about the pattern. */
4303 arg = PACK_EXPANSION_PATTERN (arg);
4304
4305 if (/* These first two lines are the `non-type' bit. */
4306 !TYPE_P (arg)
4307 && TREE_CODE (arg) != TEMPLATE_DECL
4308 /* This next two lines are the `argument expression is not just a
4309 simple identifier' condition and also the `specialized
4310 non-type argument' bit. */
4311 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4312 && !(REFERENCE_REF_P (arg)
4313 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4314 {
4315 if ((!packed_args && tpd.arg_uses_template_parms[i])
4316 || (packed_args && uses_template_parms (arg)))
4317 error ("template argument %qE involves template parameter(s)",
4318 arg);
4319 else
4320 {
4321 /* Look at the corresponding template parameter,
4322 marking which template parameters its type depends
4323 upon. */
4324 tree type = TREE_TYPE (parm);
4325
4326 if (!tpd2.parms)
4327 {
4328 /* We haven't yet initialized TPD2. Do so now. */
4329 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4330 /* The number of parameters here is the number in the
4331 main template, which, as checked in the assertion
4332 above, is NARGS. */
4333 tpd2.parms = XALLOCAVEC (int, nargs);
4334 tpd2.level =
4335 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4336 }
4337
4338 /* Mark the template parameters. But this time, we're
4339 looking for the template parameters of the main
4340 template, not in the specialization. */
4341 tpd2.current_arg = i;
4342 tpd2.arg_uses_template_parms[i] = 0;
4343 memset (tpd2.parms, 0, sizeof (int) * nargs);
4344 for_each_template_parm (type,
4345 &mark_template_parm,
4346 &tpd2,
4347 NULL,
4348 /*include_nondeduced_p=*/false);
4349
4350 if (tpd2.arg_uses_template_parms [i])
4351 {
4352 /* The type depended on some template parameters.
4353 If they are fully specialized in the
4354 specialization, that's OK. */
4355 int j;
4356 int count = 0;
4357 for (j = 0; j < nargs; ++j)
4358 if (tpd2.parms[j] != 0
4359 && tpd.arg_uses_template_parms [j])
4360 ++count;
4361 if (count != 0)
4362 error_n (input_location, count,
4363 "type %qT of template argument %qE depends "
4364 "on a template parameter",
4365 "type %qT of template argument %qE depends "
4366 "on template parameters",
4367 type,
4368 arg);
4369 }
4370 }
4371 }
4372 }
4373 }
4374
4375 /* We should only get here once. */
4376 if (TREE_CODE (decl) == TYPE_DECL)
4377 gcc_assert (!COMPLETE_TYPE_P (type));
4378
4379 tree tmpl = build_template_decl (decl, current_template_parms,
4380 DECL_MEMBER_TEMPLATE_P (maintmpl));
4381 TREE_TYPE (tmpl) = type;
4382 DECL_TEMPLATE_RESULT (tmpl) = decl;
4383 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4384 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4385 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4386
4387 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4388 = tree_cons (specargs, tmpl,
4389 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4390 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4391
4392 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4393 inst = TREE_CHAIN (inst))
4394 {
4395 tree instance = TREE_VALUE (inst);
4396 if (TYPE_P (instance)
4397 ? (COMPLETE_TYPE_P (instance)
4398 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4399 : DECL_TEMPLATE_INSTANTIATION (instance))
4400 {
4401 tree spec = most_specialized_partial_spec (instance, tf_none);
4402 if (spec && TREE_VALUE (spec) == tmpl)
4403 {
4404 tree inst_decl = (DECL_P (instance)
4405 ? instance : TYPE_NAME (instance));
4406 permerror (input_location,
4407 "partial specialization of %qD after instantiation "
4408 "of %qD", decl, inst_decl);
4409 }
4410 }
4411 }
4412
4413 return decl;
4414 }
4415
4416 /* PARM is a template parameter of some form; return the corresponding
4417 TEMPLATE_PARM_INDEX. */
4418
4419 static tree
4420 get_template_parm_index (tree parm)
4421 {
4422 if (TREE_CODE (parm) == PARM_DECL
4423 || TREE_CODE (parm) == CONST_DECL)
4424 parm = DECL_INITIAL (parm);
4425 else if (TREE_CODE (parm) == TYPE_DECL
4426 || TREE_CODE (parm) == TEMPLATE_DECL)
4427 parm = TREE_TYPE (parm);
4428 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4429 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4430 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4431 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4432 return parm;
4433 }
4434
4435 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4436 parameter packs used by the template parameter PARM. */
4437
4438 static void
4439 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4440 {
4441 /* A type parm can't refer to another parm. */
4442 if (TREE_CODE (parm) == TYPE_DECL)
4443 return;
4444 else if (TREE_CODE (parm) == PARM_DECL)
4445 {
4446 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4447 ppd, ppd->visited);
4448 return;
4449 }
4450
4451 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4452
4453 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4454 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4455 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4456 }
4457
4458 /* PARM is a template parameter pack. Return any parameter packs used in
4459 its type or the type of any of its template parameters. If there are
4460 any such packs, it will be instantiated into a fixed template parameter
4461 list by partial instantiation rather than be fully deduced. */
4462
4463 tree
4464 fixed_parameter_pack_p (tree parm)
4465 {
4466 /* This can only be true in a member template. */
4467 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4468 return NULL_TREE;
4469 /* This can only be true for a parameter pack. */
4470 if (!template_parameter_pack_p (parm))
4471 return NULL_TREE;
4472 /* A type parm can't refer to another parm. */
4473 if (TREE_CODE (parm) == TYPE_DECL)
4474 return NULL_TREE;
4475
4476 tree parameter_packs = NULL_TREE;
4477 struct find_parameter_pack_data ppd;
4478 ppd.parameter_packs = &parameter_packs;
4479 ppd.visited = new hash_set<tree>;
4480
4481 fixed_parameter_pack_p_1 (parm, &ppd);
4482
4483 delete ppd.visited;
4484 return parameter_packs;
4485 }
4486
4487 /* Check that a template declaration's use of default arguments and
4488 parameter packs is not invalid. Here, PARMS are the template
4489 parameters. IS_PRIMARY is true if DECL is the thing declared by
4490 a primary template. IS_PARTIAL is true if DECL is a partial
4491 specialization.
4492
4493 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4494 declaration (but not a definition); 1 indicates a declaration, 2
4495 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4496 emitted for extraneous default arguments.
4497
4498 Returns TRUE if there were no errors found, FALSE otherwise. */
4499
4500 bool
4501 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4502 bool is_partial, int is_friend_decl)
4503 {
4504 const char *msg;
4505 int last_level_to_check;
4506 tree parm_level;
4507 bool no_errors = true;
4508
4509 /* [temp.param]
4510
4511 A default template-argument shall not be specified in a
4512 function template declaration or a function template definition, nor
4513 in the template-parameter-list of the definition of a member of a
4514 class template. */
4515
4516 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4517 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4518 /* You can't have a function template declaration in a local
4519 scope, nor you can you define a member of a class template in a
4520 local scope. */
4521 return true;
4522
4523 if ((TREE_CODE (decl) == TYPE_DECL
4524 && TREE_TYPE (decl)
4525 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4526 || (TREE_CODE (decl) == FUNCTION_DECL
4527 && LAMBDA_FUNCTION_P (decl)))
4528 /* A lambda doesn't have an explicit declaration; don't complain
4529 about the parms of the enclosing class. */
4530 return true;
4531
4532 if (current_class_type
4533 && !TYPE_BEING_DEFINED (current_class_type)
4534 && DECL_LANG_SPECIFIC (decl)
4535 && DECL_DECLARES_FUNCTION_P (decl)
4536 /* If this is either a friend defined in the scope of the class
4537 or a member function. */
4538 && (DECL_FUNCTION_MEMBER_P (decl)
4539 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4540 : DECL_FRIEND_CONTEXT (decl)
4541 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4542 : false)
4543 /* And, if it was a member function, it really was defined in
4544 the scope of the class. */
4545 && (!DECL_FUNCTION_MEMBER_P (decl)
4546 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4547 /* We already checked these parameters when the template was
4548 declared, so there's no need to do it again now. This function
4549 was defined in class scope, but we're processing its body now
4550 that the class is complete. */
4551 return true;
4552
4553 /* Core issue 226 (C++0x only): the following only applies to class
4554 templates. */
4555 if (is_primary
4556 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4557 {
4558 /* [temp.param]
4559
4560 If a template-parameter has a default template-argument, all
4561 subsequent template-parameters shall have a default
4562 template-argument supplied. */
4563 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4564 {
4565 tree inner_parms = TREE_VALUE (parm_level);
4566 int ntparms = TREE_VEC_LENGTH (inner_parms);
4567 int seen_def_arg_p = 0;
4568 int i;
4569
4570 for (i = 0; i < ntparms; ++i)
4571 {
4572 tree parm = TREE_VEC_ELT (inner_parms, i);
4573
4574 if (parm == error_mark_node)
4575 continue;
4576
4577 if (TREE_PURPOSE (parm))
4578 seen_def_arg_p = 1;
4579 else if (seen_def_arg_p
4580 && !template_parameter_pack_p (TREE_VALUE (parm)))
4581 {
4582 error ("no default argument for %qD", TREE_VALUE (parm));
4583 /* For better subsequent error-recovery, we indicate that
4584 there should have been a default argument. */
4585 TREE_PURPOSE (parm) = error_mark_node;
4586 no_errors = false;
4587 }
4588 else if (!is_partial
4589 && !is_friend_decl
4590 /* Don't complain about an enclosing partial
4591 specialization. */
4592 && parm_level == parms
4593 && TREE_CODE (decl) == TYPE_DECL
4594 && i < ntparms - 1
4595 && template_parameter_pack_p (TREE_VALUE (parm))
4596 /* A fixed parameter pack will be partially
4597 instantiated into a fixed length list. */
4598 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4599 {
4600 /* A primary class template can only have one
4601 parameter pack, at the end of the template
4602 parameter list. */
4603
4604 error ("parameter pack %q+D must be at the end of the"
4605 " template parameter list", TREE_VALUE (parm));
4606
4607 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4608 = error_mark_node;
4609 no_errors = false;
4610 }
4611 }
4612 }
4613 }
4614
4615 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4616 || is_partial
4617 || !is_primary
4618 || is_friend_decl)
4619 /* For an ordinary class template, default template arguments are
4620 allowed at the innermost level, e.g.:
4621 template <class T = int>
4622 struct S {};
4623 but, in a partial specialization, they're not allowed even
4624 there, as we have in [temp.class.spec]:
4625
4626 The template parameter list of a specialization shall not
4627 contain default template argument values.
4628
4629 So, for a partial specialization, or for a function template
4630 (in C++98/C++03), we look at all of them. */
4631 ;
4632 else
4633 /* But, for a primary class template that is not a partial
4634 specialization we look at all template parameters except the
4635 innermost ones. */
4636 parms = TREE_CHAIN (parms);
4637
4638 /* Figure out what error message to issue. */
4639 if (is_friend_decl == 2)
4640 msg = G_("default template arguments may not be used in function template "
4641 "friend re-declaration");
4642 else if (is_friend_decl)
4643 msg = G_("default template arguments may not be used in function template "
4644 "friend declarations");
4645 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4646 msg = G_("default template arguments may not be used in function templates "
4647 "without -std=c++11 or -std=gnu++11");
4648 else if (is_partial)
4649 msg = G_("default template arguments may not be used in "
4650 "partial specializations");
4651 else
4652 msg = G_("default argument for template parameter for class enclosing %qD");
4653
4654 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4655 /* If we're inside a class definition, there's no need to
4656 examine the parameters to the class itself. On the one
4657 hand, they will be checked when the class is defined, and,
4658 on the other, default arguments are valid in things like:
4659 template <class T = double>
4660 struct S { template <class U> void f(U); };
4661 Here the default argument for `S' has no bearing on the
4662 declaration of `f'. */
4663 last_level_to_check = template_class_depth (current_class_type) + 1;
4664 else
4665 /* Check everything. */
4666 last_level_to_check = 0;
4667
4668 for (parm_level = parms;
4669 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4670 parm_level = TREE_CHAIN (parm_level))
4671 {
4672 tree inner_parms = TREE_VALUE (parm_level);
4673 int i;
4674 int ntparms;
4675
4676 ntparms = TREE_VEC_LENGTH (inner_parms);
4677 for (i = 0; i < ntparms; ++i)
4678 {
4679 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4680 continue;
4681
4682 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4683 {
4684 if (msg)
4685 {
4686 no_errors = false;
4687 if (is_friend_decl == 2)
4688 return no_errors;
4689
4690 error (msg, decl);
4691 msg = 0;
4692 }
4693
4694 /* Clear out the default argument so that we are not
4695 confused later. */
4696 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4697 }
4698 }
4699
4700 /* At this point, if we're still interested in issuing messages,
4701 they must apply to classes surrounding the object declared. */
4702 if (msg)
4703 msg = G_("default argument for template parameter for class "
4704 "enclosing %qD");
4705 }
4706
4707 return no_errors;
4708 }
4709
4710 /* Worker for push_template_decl_real, called via
4711 for_each_template_parm. DATA is really an int, indicating the
4712 level of the parameters we are interested in. If T is a template
4713 parameter of that level, return nonzero. */
4714
4715 static int
4716 template_parm_this_level_p (tree t, void* data)
4717 {
4718 int this_level = *(int *)data;
4719 int level;
4720
4721 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4722 level = TEMPLATE_PARM_LEVEL (t);
4723 else
4724 level = TEMPLATE_TYPE_LEVEL (t);
4725 return level == this_level;
4726 }
4727
4728 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4729 parameters given by current_template_args, or reuses a
4730 previously existing one, if appropriate. Returns the DECL, or an
4731 equivalent one, if it is replaced via a call to duplicate_decls.
4732
4733 If IS_FRIEND is true, DECL is a friend declaration. */
4734
4735 tree
4736 push_template_decl_real (tree decl, bool is_friend)
4737 {
4738 tree tmpl;
4739 tree args;
4740 tree info;
4741 tree ctx;
4742 bool is_primary;
4743 bool is_partial;
4744 int new_template_p = 0;
4745 /* True if the template is a member template, in the sense of
4746 [temp.mem]. */
4747 bool member_template_p = false;
4748
4749 if (decl == error_mark_node || !current_template_parms)
4750 return error_mark_node;
4751
4752 /* See if this is a partial specialization. */
4753 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
4754 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4755 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4756 || (TREE_CODE (decl) == VAR_DECL
4757 && DECL_LANG_SPECIFIC (decl)
4758 && DECL_TEMPLATE_SPECIALIZATION (decl)
4759 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
4760
4761 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4762 is_friend = true;
4763
4764 if (is_friend)
4765 /* For a friend, we want the context of the friend function, not
4766 the type of which it is a friend. */
4767 ctx = CP_DECL_CONTEXT (decl);
4768 else if (CP_DECL_CONTEXT (decl)
4769 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4770 /* In the case of a virtual function, we want the class in which
4771 it is defined. */
4772 ctx = CP_DECL_CONTEXT (decl);
4773 else
4774 /* Otherwise, if we're currently defining some class, the DECL
4775 is assumed to be a member of the class. */
4776 ctx = current_scope ();
4777
4778 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4779 ctx = NULL_TREE;
4780
4781 if (!DECL_CONTEXT (decl))
4782 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4783
4784 /* See if this is a primary template. */
4785 if (is_friend && ctx
4786 && uses_template_parms_level (ctx, processing_template_decl))
4787 /* A friend template that specifies a class context, i.e.
4788 template <typename T> friend void A<T>::f();
4789 is not primary. */
4790 is_primary = false;
4791 else if (TREE_CODE (decl) == TYPE_DECL
4792 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4793 is_primary = false;
4794 else
4795 is_primary = template_parm_scope_p ();
4796
4797 if (is_primary)
4798 {
4799 if (DECL_CLASS_SCOPE_P (decl))
4800 member_template_p = true;
4801 if (TREE_CODE (decl) == TYPE_DECL
4802 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4803 {
4804 error ("template class without a name");
4805 return error_mark_node;
4806 }
4807 else if (TREE_CODE (decl) == FUNCTION_DECL)
4808 {
4809 if (member_template_p)
4810 {
4811 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
4812 error ("member template %qD may not have virt-specifiers", decl);
4813 }
4814 if (DECL_DESTRUCTOR_P (decl))
4815 {
4816 /* [temp.mem]
4817
4818 A destructor shall not be a member template. */
4819 error ("destructor %qD declared as member template", decl);
4820 return error_mark_node;
4821 }
4822 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4823 && (!prototype_p (TREE_TYPE (decl))
4824 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4825 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4826 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4827 == void_list_node)))
4828 {
4829 /* [basic.stc.dynamic.allocation]
4830
4831 An allocation function can be a function
4832 template. ... Template allocation functions shall
4833 have two or more parameters. */
4834 error ("invalid template declaration of %qD", decl);
4835 return error_mark_node;
4836 }
4837 }
4838 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4839 && CLASS_TYPE_P (TREE_TYPE (decl)))
4840 /* OK */;
4841 else if (TREE_CODE (decl) == TYPE_DECL
4842 && TYPE_DECL_ALIAS_P (decl))
4843 /* alias-declaration */
4844 gcc_assert (!DECL_ARTIFICIAL (decl));
4845 else if (VAR_P (decl))
4846 /* C++14 variable template. */;
4847 else
4848 {
4849 error ("template declaration of %q#D", decl);
4850 return error_mark_node;
4851 }
4852 }
4853
4854 /* Check to see that the rules regarding the use of default
4855 arguments are not being violated. */
4856 check_default_tmpl_args (decl, current_template_parms,
4857 is_primary, is_partial, /*is_friend_decl=*/0);
4858
4859 /* Ensure that there are no parameter packs in the type of this
4860 declaration that have not been expanded. */
4861 if (TREE_CODE (decl) == FUNCTION_DECL)
4862 {
4863 /* Check each of the arguments individually to see if there are
4864 any bare parameter packs. */
4865 tree type = TREE_TYPE (decl);
4866 tree arg = DECL_ARGUMENTS (decl);
4867 tree argtype = TYPE_ARG_TYPES (type);
4868
4869 while (arg && argtype)
4870 {
4871 if (!DECL_PACK_P (arg)
4872 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4873 {
4874 /* This is a PARM_DECL that contains unexpanded parameter
4875 packs. We have already complained about this in the
4876 check_for_bare_parameter_packs call, so just replace
4877 these types with ERROR_MARK_NODE. */
4878 TREE_TYPE (arg) = error_mark_node;
4879 TREE_VALUE (argtype) = error_mark_node;
4880 }
4881
4882 arg = DECL_CHAIN (arg);
4883 argtype = TREE_CHAIN (argtype);
4884 }
4885
4886 /* Check for bare parameter packs in the return type and the
4887 exception specifiers. */
4888 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4889 /* Errors were already issued, set return type to int
4890 as the frontend doesn't expect error_mark_node as
4891 the return type. */
4892 TREE_TYPE (type) = integer_type_node;
4893 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4894 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4895 }
4896 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4897 && TYPE_DECL_ALIAS_P (decl))
4898 ? DECL_ORIGINAL_TYPE (decl)
4899 : TREE_TYPE (decl)))
4900 {
4901 TREE_TYPE (decl) = error_mark_node;
4902 return error_mark_node;
4903 }
4904
4905 if (is_partial)
4906 return process_partial_specialization (decl);
4907
4908 args = current_template_args ();
4909
4910 if (!ctx
4911 || TREE_CODE (ctx) == FUNCTION_DECL
4912 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4913 || (TREE_CODE (decl) == TYPE_DECL
4914 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4915 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4916 {
4917 if (DECL_LANG_SPECIFIC (decl)
4918 && DECL_TEMPLATE_INFO (decl)
4919 && DECL_TI_TEMPLATE (decl))
4920 tmpl = DECL_TI_TEMPLATE (decl);
4921 /* If DECL is a TYPE_DECL for a class-template, then there won't
4922 be DECL_LANG_SPECIFIC. The information equivalent to
4923 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4924 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4925 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4926 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4927 {
4928 /* Since a template declaration already existed for this
4929 class-type, we must be redeclaring it here. Make sure
4930 that the redeclaration is valid. */
4931 redeclare_class_template (TREE_TYPE (decl),
4932 current_template_parms);
4933 /* We don't need to create a new TEMPLATE_DECL; just use the
4934 one we already had. */
4935 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4936 }
4937 else
4938 {
4939 tmpl = build_template_decl (decl, current_template_parms,
4940 member_template_p);
4941 new_template_p = 1;
4942
4943 if (DECL_LANG_SPECIFIC (decl)
4944 && DECL_TEMPLATE_SPECIALIZATION (decl))
4945 {
4946 /* A specialization of a member template of a template
4947 class. */
4948 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4949 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4950 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4951 }
4952 }
4953 }
4954 else
4955 {
4956 tree a, t, current, parms;
4957 int i;
4958 tree tinfo = get_template_info (decl);
4959
4960 if (!tinfo)
4961 {
4962 error ("template definition of non-template %q#D", decl);
4963 return error_mark_node;
4964 }
4965
4966 tmpl = TI_TEMPLATE (tinfo);
4967
4968 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4969 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4970 && DECL_TEMPLATE_SPECIALIZATION (decl)
4971 && DECL_MEMBER_TEMPLATE_P (tmpl))
4972 {
4973 tree new_tmpl;
4974
4975 /* The declaration is a specialization of a member
4976 template, declared outside the class. Therefore, the
4977 innermost template arguments will be NULL, so we
4978 replace them with the arguments determined by the
4979 earlier call to check_explicit_specialization. */
4980 args = DECL_TI_ARGS (decl);
4981
4982 new_tmpl
4983 = build_template_decl (decl, current_template_parms,
4984 member_template_p);
4985 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4986 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4987 DECL_TI_TEMPLATE (decl) = new_tmpl;
4988 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4989 DECL_TEMPLATE_INFO (new_tmpl)
4990 = build_template_info (tmpl, args);
4991
4992 register_specialization (new_tmpl,
4993 most_general_template (tmpl),
4994 args,
4995 is_friend, 0);
4996 return decl;
4997 }
4998
4999 /* Make sure the template headers we got make sense. */
5000
5001 parms = DECL_TEMPLATE_PARMS (tmpl);
5002 i = TMPL_PARMS_DEPTH (parms);
5003 if (TMPL_ARGS_DEPTH (args) != i)
5004 {
5005 error ("expected %d levels of template parms for %q#D, got %d",
5006 i, decl, TMPL_ARGS_DEPTH (args));
5007 DECL_INTERFACE_KNOWN (decl) = 1;
5008 return error_mark_node;
5009 }
5010 else
5011 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5012 {
5013 a = TMPL_ARGS_LEVEL (args, i);
5014 t = INNERMOST_TEMPLATE_PARMS (parms);
5015
5016 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5017 {
5018 if (current == decl)
5019 error ("got %d template parameters for %q#D",
5020 TREE_VEC_LENGTH (a), decl);
5021 else
5022 error ("got %d template parameters for %q#T",
5023 TREE_VEC_LENGTH (a), current);
5024 error (" but %d required", TREE_VEC_LENGTH (t));
5025 /* Avoid crash in import_export_decl. */
5026 DECL_INTERFACE_KNOWN (decl) = 1;
5027 return error_mark_node;
5028 }
5029
5030 if (current == decl)
5031 current = ctx;
5032 else if (current == NULL_TREE)
5033 /* Can happen in erroneous input. */
5034 break;
5035 else
5036 current = get_containing_scope (current);
5037 }
5038
5039 /* Check that the parms are used in the appropriate qualifying scopes
5040 in the declarator. */
5041 if (!comp_template_args
5042 (TI_ARGS (tinfo),
5043 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5044 {
5045 error ("\
5046 template arguments to %qD do not match original template %qD",
5047 decl, DECL_TEMPLATE_RESULT (tmpl));
5048 if (!uses_template_parms (TI_ARGS (tinfo)))
5049 inform (input_location, "use template<> for an explicit specialization");
5050 /* Avoid crash in import_export_decl. */
5051 DECL_INTERFACE_KNOWN (decl) = 1;
5052 return error_mark_node;
5053 }
5054 }
5055
5056 DECL_TEMPLATE_RESULT (tmpl) = decl;
5057 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5058
5059 /* Push template declarations for global functions and types. Note
5060 that we do not try to push a global template friend declared in a
5061 template class; such a thing may well depend on the template
5062 parameters of the class. */
5063 if (new_template_p && !ctx
5064 && !(is_friend && template_class_depth (current_class_type) > 0))
5065 {
5066 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5067 if (tmpl == error_mark_node)
5068 return error_mark_node;
5069
5070 /* Hide template friend classes that haven't been declared yet. */
5071 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5072 {
5073 DECL_ANTICIPATED (tmpl) = 1;
5074 DECL_FRIEND_P (tmpl) = 1;
5075 }
5076 }
5077
5078 if (is_primary)
5079 {
5080 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5081 int i;
5082
5083 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5084 if (DECL_CONV_FN_P (tmpl))
5085 {
5086 int depth = TMPL_PARMS_DEPTH (parms);
5087
5088 /* It is a conversion operator. See if the type converted to
5089 depends on innermost template operands. */
5090
5091 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5092 depth))
5093 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5094 }
5095
5096 /* Give template template parms a DECL_CONTEXT of the template
5097 for which they are a parameter. */
5098 parms = INNERMOST_TEMPLATE_PARMS (parms);
5099 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5100 {
5101 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5102 if (TREE_CODE (parm) == TEMPLATE_DECL)
5103 DECL_CONTEXT (parm) = tmpl;
5104 }
5105 }
5106
5107 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5108 back to its most general template. If TMPL is a specialization,
5109 ARGS may only have the innermost set of arguments. Add the missing
5110 argument levels if necessary. */
5111 if (DECL_TEMPLATE_INFO (tmpl))
5112 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5113
5114 info = build_template_info (tmpl, args);
5115
5116 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5117 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5118 else
5119 {
5120 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5121 retrofit_lang_decl (decl);
5122 if (DECL_LANG_SPECIFIC (decl))
5123 DECL_TEMPLATE_INFO (decl) = info;
5124 }
5125
5126 if (flag_implicit_templates
5127 && !is_friend
5128 && TREE_PUBLIC (decl)
5129 && VAR_OR_FUNCTION_DECL_P (decl))
5130 /* Set DECL_COMDAT on template instantiations; if we force
5131 them to be emitted by explicit instantiation or -frepo,
5132 mark_needed will tell cgraph to do the right thing. */
5133 DECL_COMDAT (decl) = true;
5134
5135 return DECL_TEMPLATE_RESULT (tmpl);
5136 }
5137
5138 tree
5139 push_template_decl (tree decl)
5140 {
5141 return push_template_decl_real (decl, false);
5142 }
5143
5144 /* FN is an inheriting constructor that inherits from the constructor
5145 template INHERITED; turn FN into a constructor template with a matching
5146 template header. */
5147
5148 tree
5149 add_inherited_template_parms (tree fn, tree inherited)
5150 {
5151 tree inner_parms
5152 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5153 inner_parms = copy_node (inner_parms);
5154 tree parms
5155 = tree_cons (size_int (processing_template_decl + 1),
5156 inner_parms, current_template_parms);
5157 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5158 tree args = template_parms_to_args (parms);
5159 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5160 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5161 DECL_TEMPLATE_RESULT (tmpl) = fn;
5162 DECL_ARTIFICIAL (tmpl) = true;
5163 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5164 return tmpl;
5165 }
5166
5167 /* Called when a class template TYPE is redeclared with the indicated
5168 template PARMS, e.g.:
5169
5170 template <class T> struct S;
5171 template <class T> struct S {}; */
5172
5173 bool
5174 redeclare_class_template (tree type, tree parms)
5175 {
5176 tree tmpl;
5177 tree tmpl_parms;
5178 int i;
5179
5180 if (!TYPE_TEMPLATE_INFO (type))
5181 {
5182 error ("%qT is not a template type", type);
5183 return false;
5184 }
5185
5186 tmpl = TYPE_TI_TEMPLATE (type);
5187 if (!PRIMARY_TEMPLATE_P (tmpl))
5188 /* The type is nested in some template class. Nothing to worry
5189 about here; there are no new template parameters for the nested
5190 type. */
5191 return true;
5192
5193 if (!parms)
5194 {
5195 error ("template specifiers not specified in declaration of %qD",
5196 tmpl);
5197 return false;
5198 }
5199
5200 parms = INNERMOST_TEMPLATE_PARMS (parms);
5201 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5202
5203 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5204 {
5205 error_n (input_location, TREE_VEC_LENGTH (parms),
5206 "redeclared with %d template parameter",
5207 "redeclared with %d template parameters",
5208 TREE_VEC_LENGTH (parms));
5209 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5210 "previous declaration %q+D used %d template parameter",
5211 "previous declaration %q+D used %d template parameters",
5212 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5213 return false;
5214 }
5215
5216 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5217 {
5218 tree tmpl_parm;
5219 tree parm;
5220 tree tmpl_default;
5221 tree parm_default;
5222
5223 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5224 || TREE_VEC_ELT (parms, i) == error_mark_node)
5225 continue;
5226
5227 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5228 if (error_operand_p (tmpl_parm))
5229 return false;
5230
5231 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5232 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5233 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5234
5235 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5236 TEMPLATE_DECL. */
5237 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5238 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5239 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5240 || (TREE_CODE (tmpl_parm) != PARM_DECL
5241 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5242 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5243 || (TREE_CODE (tmpl_parm) == PARM_DECL
5244 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5245 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5246 {
5247 error ("template parameter %q+#D", tmpl_parm);
5248 error ("redeclared here as %q#D", parm);
5249 return false;
5250 }
5251
5252 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5253 {
5254 /* We have in [temp.param]:
5255
5256 A template-parameter may not be given default arguments
5257 by two different declarations in the same scope. */
5258 error_at (input_location, "redefinition of default argument for %q#D", parm);
5259 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5260 "original definition appeared here");
5261 return false;
5262 }
5263
5264 if (parm_default != NULL_TREE)
5265 /* Update the previous template parameters (which are the ones
5266 that will really count) with the new default value. */
5267 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5268 else if (tmpl_default != NULL_TREE)
5269 /* Update the new parameters, too; they'll be used as the
5270 parameters for any members. */
5271 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5272 }
5273
5274 return true;
5275 }
5276
5277 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5278 to be used when the caller has already checked
5279 (processing_template_decl
5280 && !instantiation_dependent_expression_p (expr)
5281 && potential_constant_expression (expr))
5282 and cleared processing_template_decl. */
5283
5284 tree
5285 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5286 {
5287 return tsubst_copy_and_build (expr,
5288 /*args=*/NULL_TREE,
5289 complain,
5290 /*in_decl=*/NULL_TREE,
5291 /*function_p=*/false,
5292 /*integral_constant_expression_p=*/true);
5293 }
5294
5295 /* Simplify EXPR if it is a non-dependent expression. Returns the
5296 (possibly simplified) expression. */
5297
5298 tree
5299 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5300 {
5301 if (expr == NULL_TREE)
5302 return NULL_TREE;
5303
5304 /* If we're in a template, but EXPR isn't value dependent, simplify
5305 it. We're supposed to treat:
5306
5307 template <typename T> void f(T[1 + 1]);
5308 template <typename T> void f(T[2]);
5309
5310 as two declarations of the same function, for example. */
5311 if (processing_template_decl
5312 && !instantiation_dependent_expression_p (expr)
5313 && potential_constant_expression (expr))
5314 {
5315 processing_template_decl_sentinel s;
5316 expr = instantiate_non_dependent_expr_internal (expr, complain);
5317 }
5318 return expr;
5319 }
5320
5321 tree
5322 instantiate_non_dependent_expr (tree expr)
5323 {
5324 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5325 }
5326
5327 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5328 template declaration, or a TYPE_DECL for an alias declaration. */
5329
5330 bool
5331 alias_type_or_template_p (tree t)
5332 {
5333 if (t == NULL_TREE)
5334 return false;
5335 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5336 || (TYPE_P (t)
5337 && TYPE_NAME (t)
5338 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5339 || DECL_ALIAS_TEMPLATE_P (t));
5340 }
5341
5342 /* Return TRUE iff T is a specialization of an alias template. */
5343
5344 bool
5345 alias_template_specialization_p (const_tree t)
5346 {
5347 /* It's an alias template specialization if it's an alias and its
5348 TYPE_NAME is a specialization of a primary template. */
5349 if (TYPE_ALIAS_P (t))
5350 {
5351 tree name = TYPE_NAME (t);
5352 if (DECL_LANG_SPECIFIC (name))
5353 if (tree ti = DECL_TEMPLATE_INFO (name))
5354 {
5355 tree tmpl = TI_TEMPLATE (ti);
5356 return PRIMARY_TEMPLATE_P (tmpl);
5357 }
5358 }
5359 return false;
5360 }
5361
5362 /* Return TRUE iff T is a specialization of an alias template with
5363 dependent template-arguments. */
5364
5365 bool
5366 dependent_alias_template_spec_p (const_tree t)
5367 {
5368 return (alias_template_specialization_p (t)
5369 && (any_dependent_template_arguments_p
5370 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5371 }
5372
5373 /* Return the number of innermost template parameters in TMPL. */
5374
5375 static int
5376 num_innermost_template_parms (tree tmpl)
5377 {
5378 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5379 return TREE_VEC_LENGTH (parms);
5380 }
5381
5382 /* Return either TMPL or another template that it is equivalent to under DR
5383 1286: An alias that just changes the name of a template is equivalent to
5384 the other template. */
5385
5386 static tree
5387 get_underlying_template (tree tmpl)
5388 {
5389 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5390 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5391 {
5392 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5393 if (TYPE_TEMPLATE_INFO (result))
5394 {
5395 tree sub = TYPE_TI_TEMPLATE (result);
5396 if (PRIMARY_TEMPLATE_P (sub)
5397 && (num_innermost_template_parms (tmpl)
5398 == num_innermost_template_parms (sub)))
5399 {
5400 tree alias_args = INNERMOST_TEMPLATE_ARGS
5401 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5402 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5403 break;
5404 /* The alias type is equivalent to the pattern of the
5405 underlying template, so strip the alias. */
5406 tmpl = sub;
5407 continue;
5408 }
5409 }
5410 break;
5411 }
5412 return tmpl;
5413 }
5414
5415 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5416 must be a function or a pointer-to-function type, as specified
5417 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5418 and check that the resulting function has external linkage. */
5419
5420 static tree
5421 convert_nontype_argument_function (tree type, tree expr,
5422 tsubst_flags_t complain)
5423 {
5424 tree fns = expr;
5425 tree fn, fn_no_ptr;
5426 linkage_kind linkage;
5427
5428 fn = instantiate_type (type, fns, tf_none);
5429 if (fn == error_mark_node)
5430 return error_mark_node;
5431
5432 fn_no_ptr = fn;
5433 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5434 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5435 if (BASELINK_P (fn_no_ptr))
5436 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5437
5438 /* [temp.arg.nontype]/1
5439
5440 A template-argument for a non-type, non-template template-parameter
5441 shall be one of:
5442 [...]
5443 -- the address of an object or function with external [C++11: or
5444 internal] linkage. */
5445
5446 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5447 {
5448 if (complain & tf_error)
5449 {
5450 error ("%qE is not a valid template argument for type %qT",
5451 expr, type);
5452 if (TYPE_PTR_P (type))
5453 error ("it must be the address of a function with "
5454 "external linkage");
5455 else
5456 error ("it must be the name of a function with "
5457 "external linkage");
5458 }
5459 return NULL_TREE;
5460 }
5461
5462 linkage = decl_linkage (fn_no_ptr);
5463 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5464 {
5465 if (complain & tf_error)
5466 {
5467 if (cxx_dialect >= cxx11)
5468 error ("%qE is not a valid template argument for type %qT "
5469 "because %qD has no linkage",
5470 expr, type, fn_no_ptr);
5471 else
5472 error ("%qE is not a valid template argument for type %qT "
5473 "because %qD does not have external linkage",
5474 expr, type, fn_no_ptr);
5475 }
5476 return NULL_TREE;
5477 }
5478
5479 return fn;
5480 }
5481
5482 /* Subroutine of convert_nontype_argument.
5483 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5484 Emit an error otherwise. */
5485
5486 static bool
5487 check_valid_ptrmem_cst_expr (tree type, tree expr,
5488 tsubst_flags_t complain)
5489 {
5490 STRIP_NOPS (expr);
5491 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5492 return true;
5493 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5494 return true;
5495 if (processing_template_decl
5496 && TREE_CODE (expr) == ADDR_EXPR
5497 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5498 return true;
5499 if (complain & tf_error)
5500 {
5501 error ("%qE is not a valid template argument for type %qT",
5502 expr, type);
5503 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5504 }
5505 return false;
5506 }
5507
5508 /* Returns TRUE iff the address of OP is value-dependent.
5509
5510 14.6.2.4 [temp.dep.temp]:
5511 A non-integral non-type template-argument is dependent if its type is
5512 dependent or it has either of the following forms
5513 qualified-id
5514 & qualified-id
5515 and contains a nested-name-specifier which specifies a class-name that
5516 names a dependent type.
5517
5518 We generalize this to just say that the address of a member of a
5519 dependent class is value-dependent; the above doesn't cover the
5520 address of a static data member named with an unqualified-id. */
5521
5522 static bool
5523 has_value_dependent_address (tree op)
5524 {
5525 /* We could use get_inner_reference here, but there's no need;
5526 this is only relevant for template non-type arguments, which
5527 can only be expressed as &id-expression. */
5528 if (DECL_P (op))
5529 {
5530 tree ctx = CP_DECL_CONTEXT (op);
5531 if (TYPE_P (ctx) && dependent_type_p (ctx))
5532 return true;
5533 }
5534
5535 return false;
5536 }
5537
5538 /* The next set of functions are used for providing helpful explanatory
5539 diagnostics for failed overload resolution. Their messages should be
5540 indented by two spaces for consistency with the messages in
5541 call.c */
5542
5543 static int
5544 unify_success (bool /*explain_p*/)
5545 {
5546 return 0;
5547 }
5548
5549 static int
5550 unify_parameter_deduction_failure (bool explain_p, tree parm)
5551 {
5552 if (explain_p)
5553 inform (input_location,
5554 " couldn't deduce template parameter %qD", parm);
5555 return 1;
5556 }
5557
5558 static int
5559 unify_invalid (bool /*explain_p*/)
5560 {
5561 return 1;
5562 }
5563
5564 static int
5565 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5566 {
5567 if (explain_p)
5568 inform (input_location,
5569 " types %qT and %qT have incompatible cv-qualifiers",
5570 parm, arg);
5571 return 1;
5572 }
5573
5574 static int
5575 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5576 {
5577 if (explain_p)
5578 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5579 return 1;
5580 }
5581
5582 static int
5583 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5584 {
5585 if (explain_p)
5586 inform (input_location,
5587 " template parameter %qD is not a parameter pack, but "
5588 "argument %qD is",
5589 parm, arg);
5590 return 1;
5591 }
5592
5593 static int
5594 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5595 {
5596 if (explain_p)
5597 inform (input_location,
5598 " template argument %qE does not match "
5599 "pointer-to-member constant %qE",
5600 arg, parm);
5601 return 1;
5602 }
5603
5604 static int
5605 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5606 {
5607 if (explain_p)
5608 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5609 return 1;
5610 }
5611
5612 static int
5613 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5614 {
5615 if (explain_p)
5616 inform (input_location,
5617 " inconsistent parameter pack deduction with %qT and %qT",
5618 old_arg, new_arg);
5619 return 1;
5620 }
5621
5622 static int
5623 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5624 {
5625 if (explain_p)
5626 {
5627 if (TYPE_P (parm))
5628 inform (input_location,
5629 " deduced conflicting types for parameter %qT (%qT and %qT)",
5630 parm, first, second);
5631 else
5632 inform (input_location,
5633 " deduced conflicting values for non-type parameter "
5634 "%qE (%qE and %qE)", parm, first, second);
5635 }
5636 return 1;
5637 }
5638
5639 static int
5640 unify_vla_arg (bool explain_p, tree arg)
5641 {
5642 if (explain_p)
5643 inform (input_location,
5644 " variable-sized array type %qT is not "
5645 "a valid template argument",
5646 arg);
5647 return 1;
5648 }
5649
5650 static int
5651 unify_method_type_error (bool explain_p, tree arg)
5652 {
5653 if (explain_p)
5654 inform (input_location,
5655 " member function type %qT is not a valid template argument",
5656 arg);
5657 return 1;
5658 }
5659
5660 static int
5661 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
5662 {
5663 if (explain_p)
5664 {
5665 if (least_p)
5666 inform_n (input_location, wanted,
5667 " candidate expects at least %d argument, %d provided",
5668 " candidate expects at least %d arguments, %d provided",
5669 wanted, have);
5670 else
5671 inform_n (input_location, wanted,
5672 " candidate expects %d argument, %d provided",
5673 " candidate expects %d arguments, %d provided",
5674 wanted, have);
5675 }
5676 return 1;
5677 }
5678
5679 static int
5680 unify_too_many_arguments (bool explain_p, int have, int wanted)
5681 {
5682 return unify_arity (explain_p, have, wanted);
5683 }
5684
5685 static int
5686 unify_too_few_arguments (bool explain_p, int have, int wanted,
5687 bool least_p = false)
5688 {
5689 return unify_arity (explain_p, have, wanted, least_p);
5690 }
5691
5692 static int
5693 unify_arg_conversion (bool explain_p, tree to_type,
5694 tree from_type, tree arg)
5695 {
5696 if (explain_p)
5697 inform (EXPR_LOC_OR_LOC (arg, input_location),
5698 " cannot convert %qE (type %qT) to type %qT",
5699 arg, from_type, to_type);
5700 return 1;
5701 }
5702
5703 static int
5704 unify_no_common_base (bool explain_p, enum template_base_result r,
5705 tree parm, tree arg)
5706 {
5707 if (explain_p)
5708 switch (r)
5709 {
5710 case tbr_ambiguous_baseclass:
5711 inform (input_location, " %qT is an ambiguous base class of %qT",
5712 parm, arg);
5713 break;
5714 default:
5715 inform (input_location, " %qT is not derived from %qT", arg, parm);
5716 break;
5717 }
5718 return 1;
5719 }
5720
5721 static int
5722 unify_inconsistent_template_template_parameters (bool explain_p)
5723 {
5724 if (explain_p)
5725 inform (input_location,
5726 " template parameters of a template template argument are "
5727 "inconsistent with other deduced template arguments");
5728 return 1;
5729 }
5730
5731 static int
5732 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5733 {
5734 if (explain_p)
5735 inform (input_location,
5736 " can't deduce a template for %qT from non-template type %qT",
5737 parm, arg);
5738 return 1;
5739 }
5740
5741 static int
5742 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5743 {
5744 if (explain_p)
5745 inform (input_location,
5746 " template argument %qE does not match %qD", arg, parm);
5747 return 1;
5748 }
5749
5750 static int
5751 unify_overload_resolution_failure (bool explain_p, tree arg)
5752 {
5753 if (explain_p)
5754 inform (input_location,
5755 " could not resolve address from overloaded function %qE",
5756 arg);
5757 return 1;
5758 }
5759
5760 /* Attempt to convert the non-type template parameter EXPR to the
5761 indicated TYPE. If the conversion is successful, return the
5762 converted value. If the conversion is unsuccessful, return
5763 NULL_TREE if we issued an error message, or error_mark_node if we
5764 did not. We issue error messages for out-and-out bad template
5765 parameters, but not simply because the conversion failed, since we
5766 might be just trying to do argument deduction. Both TYPE and EXPR
5767 must be non-dependent.
5768
5769 The conversion follows the special rules described in
5770 [temp.arg.nontype], and it is much more strict than an implicit
5771 conversion.
5772
5773 This function is called twice for each template argument (see
5774 lookup_template_class for a more accurate description of this
5775 problem). This means that we need to handle expressions which
5776 are not valid in a C++ source, but can be created from the
5777 first call (for instance, casts to perform conversions). These
5778 hacks can go away after we fix the double coercion problem. */
5779
5780 static tree
5781 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5782 {
5783 tree expr_type;
5784
5785 /* Detect immediately string literals as invalid non-type argument.
5786 This special-case is not needed for correctness (we would easily
5787 catch this later), but only to provide better diagnostic for this
5788 common user mistake. As suggested by DR 100, we do not mention
5789 linkage issues in the diagnostic as this is not the point. */
5790 /* FIXME we're making this OK. */
5791 if (TREE_CODE (expr) == STRING_CST)
5792 {
5793 if (complain & tf_error)
5794 error ("%qE is not a valid template argument for type %qT "
5795 "because string literals can never be used in this context",
5796 expr, type);
5797 return NULL_TREE;
5798 }
5799
5800 /* Add the ADDR_EXPR now for the benefit of
5801 value_dependent_expression_p. */
5802 if (TYPE_PTROBV_P (type)
5803 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5804 {
5805 expr = decay_conversion (expr, complain);
5806 if (expr == error_mark_node)
5807 return error_mark_node;
5808 }
5809
5810 /* If we are in a template, EXPR may be non-dependent, but still
5811 have a syntactic, rather than semantic, form. For example, EXPR
5812 might be a SCOPE_REF, rather than the VAR_DECL to which the
5813 SCOPE_REF refers. Preserving the qualifying scope is necessary
5814 so that access checking can be performed when the template is
5815 instantiated -- but here we need the resolved form so that we can
5816 convert the argument. */
5817 bool non_dep = false;
5818 if (TYPE_REF_OBJ_P (type)
5819 && has_value_dependent_address (expr))
5820 /* If we want the address and it's value-dependent, don't fold. */;
5821 else if (!type_unknown_p (expr)
5822 && processing_template_decl
5823 && !instantiation_dependent_expression_p (expr)
5824 && potential_constant_expression (expr))
5825 non_dep = true;
5826 if (error_operand_p (expr))
5827 return error_mark_node;
5828 expr_type = TREE_TYPE (expr);
5829 if (TREE_CODE (type) == REFERENCE_TYPE)
5830 expr = mark_lvalue_use (expr);
5831 else
5832 expr = mark_rvalue_use (expr);
5833
5834 /* If the argument is non-dependent, perform any conversions in
5835 non-dependent context as well. */
5836 processing_template_decl_sentinel s (non_dep);
5837 if (non_dep)
5838 expr = instantiate_non_dependent_expr_internal (expr, complain);
5839
5840 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5841 to a non-type argument of "nullptr". */
5842 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5843 expr = convert (type, expr);
5844
5845 /* In C++11, integral or enumeration non-type template arguments can be
5846 arbitrary constant expressions. Pointer and pointer to
5847 member arguments can be general constant expressions that evaluate
5848 to a null value, but otherwise still need to be of a specific form. */
5849 if (cxx_dialect >= cxx11)
5850 {
5851 if (TREE_CODE (expr) == PTRMEM_CST)
5852 /* A PTRMEM_CST is already constant, and a valid template
5853 argument for a parameter of pointer to member type, we just want
5854 to leave it in that form rather than lower it to a
5855 CONSTRUCTOR. */;
5856 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5857 expr = maybe_constant_value (expr);
5858 else if (TYPE_PTR_OR_PTRMEM_P (type))
5859 {
5860 tree folded = maybe_constant_value (expr);
5861 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5862 : null_member_pointer_value_p (folded))
5863 expr = folded;
5864 }
5865 }
5866
5867 /* HACK: Due to double coercion, we can get a
5868 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5869 which is the tree that we built on the first call (see
5870 below when coercing to reference to object or to reference to
5871 function). We just strip everything and get to the arg.
5872 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5873 for examples. */
5874 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5875 {
5876 tree probe_type, probe = expr;
5877 if (REFERENCE_REF_P (probe))
5878 probe = TREE_OPERAND (probe, 0);
5879 probe_type = TREE_TYPE (probe);
5880 if (TREE_CODE (probe) == NOP_EXPR)
5881 {
5882 /* ??? Maybe we could use convert_from_reference here, but we
5883 would need to relax its constraints because the NOP_EXPR
5884 could actually change the type to something more cv-qualified,
5885 and this is not folded by convert_from_reference. */
5886 tree addr = TREE_OPERAND (probe, 0);
5887 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5888 && TREE_CODE (addr) == ADDR_EXPR
5889 && TYPE_PTR_P (TREE_TYPE (addr))
5890 && (same_type_ignoring_top_level_qualifiers_p
5891 (TREE_TYPE (probe_type),
5892 TREE_TYPE (TREE_TYPE (addr)))))
5893 {
5894 expr = TREE_OPERAND (addr, 0);
5895 expr_type = TREE_TYPE (probe_type);
5896 }
5897 }
5898 }
5899
5900 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5901 parameter is a pointer to object, through decay and
5902 qualification conversion. Let's strip everything. */
5903 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5904 {
5905 tree probe = expr;
5906 STRIP_NOPS (probe);
5907 if (TREE_CODE (probe) == ADDR_EXPR
5908 && TYPE_PTR_P (TREE_TYPE (probe)))
5909 {
5910 /* Skip the ADDR_EXPR only if it is part of the decay for
5911 an array. Otherwise, it is part of the original argument
5912 in the source code. */
5913 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5914 probe = TREE_OPERAND (probe, 0);
5915 expr = probe;
5916 expr_type = TREE_TYPE (expr);
5917 }
5918 }
5919
5920 /* [temp.arg.nontype]/5, bullet 1
5921
5922 For a non-type template-parameter of integral or enumeration type,
5923 integral promotions (_conv.prom_) and integral conversions
5924 (_conv.integral_) are applied. */
5925 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5926 {
5927 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5928 t = maybe_constant_value (t);
5929 if (t != error_mark_node)
5930 expr = t;
5931
5932 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5933 return error_mark_node;
5934
5935 /* Notice that there are constant expressions like '4 % 0' which
5936 do not fold into integer constants. */
5937 if (TREE_CODE (expr) != INTEGER_CST)
5938 {
5939 if (complain & tf_error)
5940 {
5941 int errs = errorcount, warns = warningcount + werrorcount;
5942 if (processing_template_decl
5943 && !require_potential_constant_expression (expr))
5944 return NULL_TREE;
5945 expr = cxx_constant_value (expr);
5946 if (errorcount > errs || warningcount + werrorcount > warns)
5947 inform (EXPR_LOC_OR_LOC (expr, input_location),
5948 "in template argument for type %qT ", type);
5949 if (expr == error_mark_node)
5950 return NULL_TREE;
5951 /* else cxx_constant_value complained but gave us
5952 a real constant, so go ahead. */
5953 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5954 }
5955 else
5956 return NULL_TREE;
5957 }
5958
5959 /* Avoid typedef problems. */
5960 if (TREE_TYPE (expr) != type)
5961 expr = fold_convert (type, expr);
5962 }
5963 /* [temp.arg.nontype]/5, bullet 2
5964
5965 For a non-type template-parameter of type pointer to object,
5966 qualification conversions (_conv.qual_) and the array-to-pointer
5967 conversion (_conv.array_) are applied. */
5968 else if (TYPE_PTROBV_P (type))
5969 {
5970 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5971
5972 A template-argument for a non-type, non-template template-parameter
5973 shall be one of: [...]
5974
5975 -- the name of a non-type template-parameter;
5976 -- the address of an object or function with external linkage, [...]
5977 expressed as "& id-expression" where the & is optional if the name
5978 refers to a function or array, or if the corresponding
5979 template-parameter is a reference.
5980
5981 Here, we do not care about functions, as they are invalid anyway
5982 for a parameter of type pointer-to-object. */
5983
5984 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5985 /* Non-type template parameters are OK. */
5986 ;
5987 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5988 /* Null pointer values are OK in C++11. */;
5989 else if (TREE_CODE (expr) != ADDR_EXPR
5990 && TREE_CODE (expr_type) != ARRAY_TYPE)
5991 {
5992 if (VAR_P (expr))
5993 {
5994 if (complain & tf_error)
5995 error ("%qD is not a valid template argument "
5996 "because %qD is a variable, not the address of "
5997 "a variable", expr, expr);
5998 return NULL_TREE;
5999 }
6000 if (POINTER_TYPE_P (expr_type))
6001 {
6002 if (complain & tf_error)
6003 error ("%qE is not a valid template argument for %qT "
6004 "because it is not the address of a variable",
6005 expr, type);
6006 return NULL_TREE;
6007 }
6008 /* Other values, like integer constants, might be valid
6009 non-type arguments of some other type. */
6010 return error_mark_node;
6011 }
6012 else
6013 {
6014 tree decl;
6015
6016 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6017 ? TREE_OPERAND (expr, 0) : expr);
6018 if (!VAR_P (decl))
6019 {
6020 if (complain & tf_error)
6021 error ("%qE is not a valid template argument of type %qT "
6022 "because %qE is not a variable", expr, type, decl);
6023 return NULL_TREE;
6024 }
6025 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6026 {
6027 if (complain & tf_error)
6028 error ("%qE is not a valid template argument of type %qT "
6029 "because %qD does not have external linkage",
6030 expr, type, decl);
6031 return NULL_TREE;
6032 }
6033 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6034 {
6035 if (complain & tf_error)
6036 error ("%qE is not a valid template argument of type %qT "
6037 "because %qD has no linkage", expr, type, decl);
6038 return NULL_TREE;
6039 }
6040 }
6041
6042 expr = decay_conversion (expr, complain);
6043 if (expr == error_mark_node)
6044 return error_mark_node;
6045
6046 expr = perform_qualification_conversions (type, expr);
6047 if (expr == error_mark_node)
6048 return error_mark_node;
6049 }
6050 /* [temp.arg.nontype]/5, bullet 3
6051
6052 For a non-type template-parameter of type reference to object, no
6053 conversions apply. The type referred to by the reference may be more
6054 cv-qualified than the (otherwise identical) type of the
6055 template-argument. The template-parameter is bound directly to the
6056 template-argument, which must be an lvalue. */
6057 else if (TYPE_REF_OBJ_P (type))
6058 {
6059 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6060 expr_type))
6061 return error_mark_node;
6062
6063 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6064 {
6065 if (complain & tf_error)
6066 error ("%qE is not a valid template argument for type %qT "
6067 "because of conflicts in cv-qualification", expr, type);
6068 return NULL_TREE;
6069 }
6070
6071 if (!real_lvalue_p (expr))
6072 {
6073 if (complain & tf_error)
6074 error ("%qE is not a valid template argument for type %qT "
6075 "because it is not an lvalue", expr, type);
6076 return NULL_TREE;
6077 }
6078
6079 /* [temp.arg.nontype]/1
6080
6081 A template-argument for a non-type, non-template template-parameter
6082 shall be one of: [...]
6083
6084 -- the address of an object or function with external linkage. */
6085 if (INDIRECT_REF_P (expr)
6086 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6087 {
6088 expr = TREE_OPERAND (expr, 0);
6089 if (DECL_P (expr))
6090 {
6091 if (complain & tf_error)
6092 error ("%q#D is not a valid template argument for type %qT "
6093 "because a reference variable does not have a constant "
6094 "address", expr, type);
6095 return NULL_TREE;
6096 }
6097 }
6098
6099 if (!DECL_P (expr))
6100 {
6101 if (complain & tf_error)
6102 error ("%qE is not a valid template argument for type %qT "
6103 "because it is not an object with external linkage",
6104 expr, type);
6105 return NULL_TREE;
6106 }
6107
6108 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6109 {
6110 if (complain & tf_error)
6111 error ("%qE is not a valid template argument for type %qT "
6112 "because object %qD has not external linkage",
6113 expr, type, expr);
6114 return NULL_TREE;
6115 }
6116
6117 expr = build_nop (type, build_address (expr));
6118 }
6119 /* [temp.arg.nontype]/5, bullet 4
6120
6121 For a non-type template-parameter of type pointer to function, only
6122 the function-to-pointer conversion (_conv.func_) is applied. If the
6123 template-argument represents a set of overloaded functions (or a
6124 pointer to such), the matching function is selected from the set
6125 (_over.over_). */
6126 else if (TYPE_PTRFN_P (type))
6127 {
6128 /* If the argument is a template-id, we might not have enough
6129 context information to decay the pointer. */
6130 if (!type_unknown_p (expr_type))
6131 {
6132 expr = decay_conversion (expr, complain);
6133 if (expr == error_mark_node)
6134 return error_mark_node;
6135 }
6136
6137 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6138 /* Null pointer values are OK in C++11. */
6139 return perform_qualification_conversions (type, expr);
6140
6141 expr = convert_nontype_argument_function (type, expr, complain);
6142 if (!expr || expr == error_mark_node)
6143 return expr;
6144 }
6145 /* [temp.arg.nontype]/5, bullet 5
6146
6147 For a non-type template-parameter of type reference to function, no
6148 conversions apply. If the template-argument represents a set of
6149 overloaded functions, the matching function is selected from the set
6150 (_over.over_). */
6151 else if (TYPE_REFFN_P (type))
6152 {
6153 if (TREE_CODE (expr) == ADDR_EXPR)
6154 {
6155 if (complain & tf_error)
6156 {
6157 error ("%qE is not a valid template argument for type %qT "
6158 "because it is a pointer", expr, type);
6159 inform (input_location, "try using %qE instead",
6160 TREE_OPERAND (expr, 0));
6161 }
6162 return NULL_TREE;
6163 }
6164
6165 expr = convert_nontype_argument_function (type, expr, complain);
6166 if (!expr || expr == error_mark_node)
6167 return expr;
6168
6169 expr = build_nop (type, build_address (expr));
6170 }
6171 /* [temp.arg.nontype]/5, bullet 6
6172
6173 For a non-type template-parameter of type pointer to member function,
6174 no conversions apply. If the template-argument represents a set of
6175 overloaded member functions, the matching member function is selected
6176 from the set (_over.over_). */
6177 else if (TYPE_PTRMEMFUNC_P (type))
6178 {
6179 expr = instantiate_type (type, expr, tf_none);
6180 if (expr == error_mark_node)
6181 return error_mark_node;
6182
6183 /* [temp.arg.nontype] bullet 1 says the pointer to member
6184 expression must be a pointer-to-member constant. */
6185 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6186 return error_mark_node;
6187
6188 /* There is no way to disable standard conversions in
6189 resolve_address_of_overloaded_function (called by
6190 instantiate_type). It is possible that the call succeeded by
6191 converting &B::I to &D::I (where B is a base of D), so we need
6192 to reject this conversion here.
6193
6194 Actually, even if there was a way to disable standard conversions,
6195 it would still be better to reject them here so that we can
6196 provide a superior diagnostic. */
6197 if (!same_type_p (TREE_TYPE (expr), type))
6198 {
6199 if (complain & tf_error)
6200 {
6201 error ("%qE is not a valid template argument for type %qT "
6202 "because it is of type %qT", expr, type,
6203 TREE_TYPE (expr));
6204 /* If we are just one standard conversion off, explain. */
6205 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6206 inform (input_location,
6207 "standard conversions are not allowed in this context");
6208 }
6209 return NULL_TREE;
6210 }
6211 }
6212 /* [temp.arg.nontype]/5, bullet 7
6213
6214 For a non-type template-parameter of type pointer to data member,
6215 qualification conversions (_conv.qual_) are applied. */
6216 else if (TYPE_PTRDATAMEM_P (type))
6217 {
6218 /* [temp.arg.nontype] bullet 1 says the pointer to member
6219 expression must be a pointer-to-member constant. */
6220 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6221 return error_mark_node;
6222
6223 expr = perform_qualification_conversions (type, expr);
6224 if (expr == error_mark_node)
6225 return expr;
6226 }
6227 else if (NULLPTR_TYPE_P (type))
6228 {
6229 if (expr != nullptr_node)
6230 {
6231 if (complain & tf_error)
6232 error ("%qE is not a valid template argument for type %qT "
6233 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6234 return NULL_TREE;
6235 }
6236 return expr;
6237 }
6238 /* A template non-type parameter must be one of the above. */
6239 else
6240 gcc_unreachable ();
6241
6242 /* Sanity check: did we actually convert the argument to the
6243 right type? */
6244 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6245 (type, TREE_TYPE (expr)));
6246 return convert_from_reference (expr);
6247 }
6248
6249 /* Subroutine of coerce_template_template_parms, which returns 1 if
6250 PARM_PARM and ARG_PARM match using the rule for the template
6251 parameters of template template parameters. Both PARM and ARG are
6252 template parameters; the rest of the arguments are the same as for
6253 coerce_template_template_parms.
6254 */
6255 static int
6256 coerce_template_template_parm (tree parm,
6257 tree arg,
6258 tsubst_flags_t complain,
6259 tree in_decl,
6260 tree outer_args)
6261 {
6262 if (arg == NULL_TREE || error_operand_p (arg)
6263 || parm == NULL_TREE || error_operand_p (parm))
6264 return 0;
6265
6266 if (TREE_CODE (arg) != TREE_CODE (parm))
6267 return 0;
6268
6269 switch (TREE_CODE (parm))
6270 {
6271 case TEMPLATE_DECL:
6272 /* We encounter instantiations of templates like
6273 template <template <template <class> class> class TT>
6274 class C; */
6275 {
6276 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6277 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6278
6279 if (!coerce_template_template_parms
6280 (parmparm, argparm, complain, in_decl, outer_args))
6281 return 0;
6282 }
6283 /* Fall through. */
6284
6285 case TYPE_DECL:
6286 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6287 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6288 /* Argument is a parameter pack but parameter is not. */
6289 return 0;
6290 break;
6291
6292 case PARM_DECL:
6293 /* The tsubst call is used to handle cases such as
6294
6295 template <int> class C {};
6296 template <class T, template <T> class TT> class D {};
6297 D<int, C> d;
6298
6299 i.e. the parameter list of TT depends on earlier parameters. */
6300 if (!uses_template_parms (TREE_TYPE (arg))
6301 && !same_type_p
6302 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6303 TREE_TYPE (arg)))
6304 return 0;
6305
6306 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6307 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6308 /* Argument is a parameter pack but parameter is not. */
6309 return 0;
6310
6311 break;
6312
6313 default:
6314 gcc_unreachable ();
6315 }
6316
6317 return 1;
6318 }
6319
6320
6321 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6322 template template parameters. Both PARM_PARMS and ARG_PARMS are
6323 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6324 or PARM_DECL.
6325
6326 Consider the example:
6327 template <class T> class A;
6328 template<template <class U> class TT> class B;
6329
6330 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6331 the parameters to A, and OUTER_ARGS contains A. */
6332
6333 static int
6334 coerce_template_template_parms (tree parm_parms,
6335 tree arg_parms,
6336 tsubst_flags_t complain,
6337 tree in_decl,
6338 tree outer_args)
6339 {
6340 int nparms, nargs, i;
6341 tree parm, arg;
6342 int variadic_p = 0;
6343
6344 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6345 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6346
6347 nparms = TREE_VEC_LENGTH (parm_parms);
6348 nargs = TREE_VEC_LENGTH (arg_parms);
6349
6350 /* Determine whether we have a parameter pack at the end of the
6351 template template parameter's template parameter list. */
6352 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6353 {
6354 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6355
6356 if (error_operand_p (parm))
6357 return 0;
6358
6359 switch (TREE_CODE (parm))
6360 {
6361 case TEMPLATE_DECL:
6362 case TYPE_DECL:
6363 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6364 variadic_p = 1;
6365 break;
6366
6367 case PARM_DECL:
6368 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6369 variadic_p = 1;
6370 break;
6371
6372 default:
6373 gcc_unreachable ();
6374 }
6375 }
6376
6377 if (nargs != nparms
6378 && !(variadic_p && nargs >= nparms - 1))
6379 return 0;
6380
6381 /* Check all of the template parameters except the parameter pack at
6382 the end (if any). */
6383 for (i = 0; i < nparms - variadic_p; ++i)
6384 {
6385 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6386 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6387 continue;
6388
6389 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6390 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6391
6392 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6393 outer_args))
6394 return 0;
6395
6396 }
6397
6398 if (variadic_p)
6399 {
6400 /* Check each of the template parameters in the template
6401 argument against the template parameter pack at the end of
6402 the template template parameter. */
6403 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6404 return 0;
6405
6406 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6407
6408 for (; i < nargs; ++i)
6409 {
6410 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6411 continue;
6412
6413 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6414
6415 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6416 outer_args))
6417 return 0;
6418 }
6419 }
6420
6421 return 1;
6422 }
6423
6424 /* Verifies that the deduced template arguments (in TARGS) for the
6425 template template parameters (in TPARMS) represent valid bindings,
6426 by comparing the template parameter list of each template argument
6427 to the template parameter list of its corresponding template
6428 template parameter, in accordance with DR150. This
6429 routine can only be called after all template arguments have been
6430 deduced. It will return TRUE if all of the template template
6431 parameter bindings are okay, FALSE otherwise. */
6432 bool
6433 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6434 {
6435 int i, ntparms = TREE_VEC_LENGTH (tparms);
6436 bool ret = true;
6437
6438 /* We're dealing with template parms in this process. */
6439 ++processing_template_decl;
6440
6441 targs = INNERMOST_TEMPLATE_ARGS (targs);
6442
6443 for (i = 0; i < ntparms; ++i)
6444 {
6445 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6446 tree targ = TREE_VEC_ELT (targs, i);
6447
6448 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6449 {
6450 tree packed_args = NULL_TREE;
6451 int idx, len = 1;
6452
6453 if (ARGUMENT_PACK_P (targ))
6454 {
6455 /* Look inside the argument pack. */
6456 packed_args = ARGUMENT_PACK_ARGS (targ);
6457 len = TREE_VEC_LENGTH (packed_args);
6458 }
6459
6460 for (idx = 0; idx < len; ++idx)
6461 {
6462 tree targ_parms = NULL_TREE;
6463
6464 if (packed_args)
6465 /* Extract the next argument from the argument
6466 pack. */
6467 targ = TREE_VEC_ELT (packed_args, idx);
6468
6469 if (PACK_EXPANSION_P (targ))
6470 /* Look at the pattern of the pack expansion. */
6471 targ = PACK_EXPANSION_PATTERN (targ);
6472
6473 /* Extract the template parameters from the template
6474 argument. */
6475 if (TREE_CODE (targ) == TEMPLATE_DECL)
6476 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6477 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6478 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6479
6480 /* Verify that we can coerce the template template
6481 parameters from the template argument to the template
6482 parameter. This requires an exact match. */
6483 if (targ_parms
6484 && !coerce_template_template_parms
6485 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6486 targ_parms,
6487 tf_none,
6488 tparm,
6489 targs))
6490 {
6491 ret = false;
6492 goto out;
6493 }
6494 }
6495 }
6496 }
6497
6498 out:
6499
6500 --processing_template_decl;
6501 return ret;
6502 }
6503
6504 /* Since type attributes aren't mangled, we need to strip them from
6505 template type arguments. */
6506
6507 static tree
6508 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6509 {
6510 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6511 return arg;
6512 bool removed_attributes = false;
6513 tree canon = strip_typedefs (arg, &removed_attributes);
6514 if (removed_attributes
6515 && (complain & tf_warning))
6516 warning (0, "ignoring attributes on template argument %qT", arg);
6517 return canon;
6518 }
6519
6520 /* Convert the indicated template ARG as necessary to match the
6521 indicated template PARM. Returns the converted ARG, or
6522 error_mark_node if the conversion was unsuccessful. Error and
6523 warning messages are issued under control of COMPLAIN. This
6524 conversion is for the Ith parameter in the parameter list. ARGS is
6525 the full set of template arguments deduced so far. */
6526
6527 static tree
6528 convert_template_argument (tree parm,
6529 tree arg,
6530 tree args,
6531 tsubst_flags_t complain,
6532 int i,
6533 tree in_decl)
6534 {
6535 tree orig_arg;
6536 tree val;
6537 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6538
6539 if (parm == error_mark_node)
6540 return error_mark_node;
6541
6542 if (TREE_CODE (arg) == TREE_LIST
6543 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6544 {
6545 /* The template argument was the name of some
6546 member function. That's usually
6547 invalid, but static members are OK. In any
6548 case, grab the underlying fields/functions
6549 and issue an error later if required. */
6550 orig_arg = TREE_VALUE (arg);
6551 TREE_TYPE (arg) = unknown_type_node;
6552 }
6553
6554 orig_arg = arg;
6555
6556 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6557 requires_type = (TREE_CODE (parm) == TYPE_DECL
6558 || requires_tmpl_type);
6559
6560 /* When determining whether an argument pack expansion is a template,
6561 look at the pattern. */
6562 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6563 arg = PACK_EXPANSION_PATTERN (arg);
6564
6565 /* Deal with an injected-class-name used as a template template arg. */
6566 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6567 {
6568 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6569 if (TREE_CODE (t) == TEMPLATE_DECL)
6570 {
6571 if (cxx_dialect >= cxx11)
6572 /* OK under DR 1004. */;
6573 else if (complain & tf_warning_or_error)
6574 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6575 " used as template template argument", TYPE_NAME (arg));
6576 else if (flag_pedantic_errors)
6577 t = arg;
6578
6579 arg = t;
6580 }
6581 }
6582
6583 is_tmpl_type =
6584 ((TREE_CODE (arg) == TEMPLATE_DECL
6585 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6586 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6587 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6588 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6589
6590 if (is_tmpl_type
6591 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6592 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6593 arg = TYPE_STUB_DECL (arg);
6594
6595 is_type = TYPE_P (arg) || is_tmpl_type;
6596
6597 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6598 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6599 {
6600 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6601 {
6602 if (complain & tf_error)
6603 error ("invalid use of destructor %qE as a type", orig_arg);
6604 return error_mark_node;
6605 }
6606
6607 permerror (input_location,
6608 "to refer to a type member of a template parameter, "
6609 "use %<typename %E%>", orig_arg);
6610
6611 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6612 TREE_OPERAND (arg, 1),
6613 typename_type,
6614 complain);
6615 arg = orig_arg;
6616 is_type = 1;
6617 }
6618 if (is_type != requires_type)
6619 {
6620 if (in_decl)
6621 {
6622 if (complain & tf_error)
6623 {
6624 error ("type/value mismatch at argument %d in template "
6625 "parameter list for %qD",
6626 i + 1, in_decl);
6627 if (is_type)
6628 inform (input_location,
6629 " expected a constant of type %qT, got %qT",
6630 TREE_TYPE (parm),
6631 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6632 else if (requires_tmpl_type)
6633 inform (input_location,
6634 " expected a class template, got %qE", orig_arg);
6635 else
6636 inform (input_location,
6637 " expected a type, got %qE", orig_arg);
6638 }
6639 }
6640 return error_mark_node;
6641 }
6642 if (is_tmpl_type ^ requires_tmpl_type)
6643 {
6644 if (in_decl && (complain & tf_error))
6645 {
6646 error ("type/value mismatch at argument %d in template "
6647 "parameter list for %qD",
6648 i + 1, in_decl);
6649 if (is_tmpl_type)
6650 inform (input_location,
6651 " expected a type, got %qT", DECL_NAME (arg));
6652 else
6653 inform (input_location,
6654 " expected a class template, got %qT", orig_arg);
6655 }
6656 return error_mark_node;
6657 }
6658
6659 if (is_type)
6660 {
6661 if (requires_tmpl_type)
6662 {
6663 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6664 val = orig_arg;
6665 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6666 /* The number of argument required is not known yet.
6667 Just accept it for now. */
6668 val = TREE_TYPE (arg);
6669 else
6670 {
6671 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6672 tree argparm;
6673
6674 /* Strip alias templates that are equivalent to another
6675 template. */
6676 arg = get_underlying_template (arg);
6677 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6678
6679 if (coerce_template_template_parms (parmparm, argparm,
6680 complain, in_decl,
6681 args))
6682 {
6683 val = arg;
6684
6685 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6686 TEMPLATE_DECL. */
6687 if (val != error_mark_node)
6688 {
6689 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6690 val = TREE_TYPE (val);
6691 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6692 val = make_pack_expansion (val);
6693 }
6694 }
6695 else
6696 {
6697 if (in_decl && (complain & tf_error))
6698 {
6699 error ("type/value mismatch at argument %d in "
6700 "template parameter list for %qD",
6701 i + 1, in_decl);
6702 inform (input_location,
6703 " expected a template of type %qD, got %qT",
6704 parm, orig_arg);
6705 }
6706
6707 val = error_mark_node;
6708 }
6709 }
6710 }
6711 else
6712 val = orig_arg;
6713 /* We only form one instance of each template specialization.
6714 Therefore, if we use a non-canonical variant (i.e., a
6715 typedef), any future messages referring to the type will use
6716 the typedef, which is confusing if those future uses do not
6717 themselves also use the typedef. */
6718 if (TYPE_P (val))
6719 val = canonicalize_type_argument (val, complain);
6720 }
6721 else
6722 {
6723 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6724
6725 if (invalid_nontype_parm_type_p (t, complain))
6726 return error_mark_node;
6727
6728 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6729 {
6730 if (same_type_p (t, TREE_TYPE (orig_arg)))
6731 val = orig_arg;
6732 else
6733 {
6734 /* Not sure if this is reachable, but it doesn't hurt
6735 to be robust. */
6736 error ("type mismatch in nontype parameter pack");
6737 val = error_mark_node;
6738 }
6739 }
6740 else if (!dependent_template_arg_p (orig_arg)
6741 && !uses_template_parms (t))
6742 /* We used to call digest_init here. However, digest_init
6743 will report errors, which we don't want when complain
6744 is zero. More importantly, digest_init will try too
6745 hard to convert things: for example, `0' should not be
6746 converted to pointer type at this point according to
6747 the standard. Accepting this is not merely an
6748 extension, since deciding whether or not these
6749 conversions can occur is part of determining which
6750 function template to call, or whether a given explicit
6751 argument specification is valid. */
6752 val = convert_nontype_argument (t, orig_arg, complain);
6753 else
6754 {
6755 bool removed_attr = false;
6756 val = strip_typedefs_expr (orig_arg, &removed_attr);
6757 }
6758
6759 if (val == NULL_TREE)
6760 val = error_mark_node;
6761 else if (val == error_mark_node && (complain & tf_error))
6762 error ("could not convert template argument %qE to %qT", orig_arg, t);
6763
6764 if (TREE_CODE (val) == SCOPE_REF)
6765 {
6766 /* Strip typedefs from the SCOPE_REF. */
6767 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6768 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6769 complain);
6770 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6771 QUALIFIED_NAME_IS_TEMPLATE (val));
6772 }
6773 }
6774
6775 return val;
6776 }
6777
6778 /* Coerces the remaining template arguments in INNER_ARGS (from
6779 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6780 Returns the coerced argument pack. PARM_IDX is the position of this
6781 parameter in the template parameter list. ARGS is the original
6782 template argument list. */
6783 static tree
6784 coerce_template_parameter_pack (tree parms,
6785 int parm_idx,
6786 tree args,
6787 tree inner_args,
6788 int arg_idx,
6789 tree new_args,
6790 int* lost,
6791 tree in_decl,
6792 tsubst_flags_t complain)
6793 {
6794 tree parm = TREE_VEC_ELT (parms, parm_idx);
6795 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6796 tree packed_args;
6797 tree argument_pack;
6798 tree packed_parms = NULL_TREE;
6799
6800 if (arg_idx > nargs)
6801 arg_idx = nargs;
6802
6803 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6804 {
6805 /* When the template parameter is a non-type template parameter pack
6806 or template template parameter pack whose type or template
6807 parameters use parameter packs, we know exactly how many arguments
6808 we are looking for. Build a vector of the instantiated decls for
6809 these template parameters in PACKED_PARMS. */
6810 /* We can't use make_pack_expansion here because it would interpret a
6811 _DECL as a use rather than a declaration. */
6812 tree decl = TREE_VALUE (parm);
6813 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6814 SET_PACK_EXPANSION_PATTERN (exp, decl);
6815 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6816 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6817
6818 TREE_VEC_LENGTH (args)--;
6819 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6820 TREE_VEC_LENGTH (args)++;
6821
6822 if (packed_parms == error_mark_node)
6823 return error_mark_node;
6824
6825 /* If we're doing a partial instantiation of a member template,
6826 verify that all of the types used for the non-type
6827 template parameter pack are, in fact, valid for non-type
6828 template parameters. */
6829 if (arg_idx < nargs
6830 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6831 {
6832 int j, len = TREE_VEC_LENGTH (packed_parms);
6833 for (j = 0; j < len; ++j)
6834 {
6835 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6836 if (invalid_nontype_parm_type_p (t, complain))
6837 return error_mark_node;
6838 }
6839 /* We don't know how many args we have yet, just
6840 use the unconverted ones for now. */
6841 return NULL_TREE;
6842 }
6843
6844 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6845 }
6846 else
6847 packed_args = make_tree_vec (nargs - arg_idx);
6848
6849 /* Convert the remaining arguments, which will be a part of the
6850 parameter pack "parm". */
6851 for (; arg_idx < nargs; ++arg_idx)
6852 {
6853 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6854 tree actual_parm = TREE_VALUE (parm);
6855 int pack_idx = arg_idx - parm_idx;
6856
6857 if (packed_parms)
6858 {
6859 /* Once we've packed as many args as we have types, stop. */
6860 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6861 break;
6862 else if (PACK_EXPANSION_P (arg))
6863 /* We don't know how many args we have yet, just
6864 use the unconverted ones for now. */
6865 return NULL_TREE;
6866 else
6867 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6868 }
6869
6870 if (arg == error_mark_node)
6871 {
6872 if (complain & tf_error)
6873 error ("template argument %d is invalid", arg_idx + 1);
6874 }
6875 else
6876 arg = convert_template_argument (actual_parm,
6877 arg, new_args, complain, parm_idx,
6878 in_decl);
6879 if (arg == error_mark_node)
6880 (*lost)++;
6881 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6882 }
6883
6884 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6885 && TREE_VEC_LENGTH (packed_args) > 0)
6886 {
6887 if (complain & tf_error)
6888 error ("wrong number of template arguments (%d, should be %d)",
6889 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6890 return error_mark_node;
6891 }
6892
6893 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6894 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6895 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6896 else
6897 {
6898 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6899 TREE_TYPE (argument_pack)
6900 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6901 TREE_CONSTANT (argument_pack) = 1;
6902 }
6903
6904 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6905 #ifdef ENABLE_CHECKING
6906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6907 TREE_VEC_LENGTH (packed_args));
6908 #endif
6909 return argument_pack;
6910 }
6911
6912 /* Returns the number of pack expansions in the template argument vector
6913 ARGS. */
6914
6915 static int
6916 pack_expansion_args_count (tree args)
6917 {
6918 int i;
6919 int count = 0;
6920 if (args)
6921 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6922 {
6923 tree elt = TREE_VEC_ELT (args, i);
6924 if (elt && PACK_EXPANSION_P (elt))
6925 ++count;
6926 }
6927 return count;
6928 }
6929
6930 /* Convert all template arguments to their appropriate types, and
6931 return a vector containing the innermost resulting template
6932 arguments. If any error occurs, return error_mark_node. Error and
6933 warning messages are issued under control of COMPLAIN.
6934
6935 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6936 for arguments not specified in ARGS. Otherwise, if
6937 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6938 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6939 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6940 ARGS. */
6941
6942 static tree
6943 coerce_template_parms (tree parms,
6944 tree args,
6945 tree in_decl,
6946 tsubst_flags_t complain,
6947 bool require_all_args,
6948 bool use_default_args)
6949 {
6950 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6951 tree orig_inner_args;
6952 tree inner_args;
6953 tree new_args;
6954 tree new_inner_args;
6955 int saved_unevaluated_operand;
6956 int saved_inhibit_evaluation_warnings;
6957
6958 /* When used as a boolean value, indicates whether this is a
6959 variadic template parameter list. Since it's an int, we can also
6960 subtract it from nparms to get the number of non-variadic
6961 parameters. */
6962 int variadic_p = 0;
6963 int variadic_args_p = 0;
6964 int post_variadic_parms = 0;
6965
6966 /* Likewise for parameters with default arguments. */
6967 int default_p = 0;
6968
6969 if (args == error_mark_node)
6970 return error_mark_node;
6971
6972 nparms = TREE_VEC_LENGTH (parms);
6973
6974 /* Determine if there are any parameter packs or default arguments. */
6975 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6976 {
6977 tree parm = TREE_VEC_ELT (parms, parm_idx);
6978 if (variadic_p)
6979 ++post_variadic_parms;
6980 if (template_parameter_pack_p (TREE_VALUE (parm)))
6981 ++variadic_p;
6982 if (TREE_PURPOSE (parm))
6983 ++default_p;
6984 }
6985
6986 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
6987 /* If there are no parameters that follow a parameter pack, we need to
6988 expand any argument packs so that we can deduce a parameter pack from
6989 some non-packed args followed by an argument pack, as in variadic85.C.
6990 If there are such parameters, we need to leave argument packs intact
6991 so the arguments are assigned properly. This can happen when dealing
6992 with a nested class inside a partial specialization of a class
6993 template, as in variadic92.C, or when deducing a template parameter pack
6994 from a sub-declarator, as in variadic114.C. */
6995 if (!post_variadic_parms)
6996 inner_args = expand_template_argument_pack (inner_args);
6997
6998 /* Count any pack expansion args. */
6999 variadic_args_p = pack_expansion_args_count (inner_args);
7000
7001 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7002 if ((nargs > nparms && !variadic_p)
7003 || (nargs < nparms - variadic_p
7004 && require_all_args
7005 && !variadic_args_p
7006 && (!use_default_args
7007 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7008 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7009 {
7010 if (complain & tf_error)
7011 {
7012 if (variadic_p || default_p)
7013 {
7014 nparms -= variadic_p + default_p;
7015 error ("wrong number of template arguments "
7016 "(%d, should be at least %d)", nargs, nparms);
7017 }
7018 else
7019 error ("wrong number of template arguments "
7020 "(%d, should be %d)", nargs, nparms);
7021
7022 if (in_decl)
7023 inform (input_location, "provided for %q+D", in_decl);
7024 }
7025
7026 return error_mark_node;
7027 }
7028 /* We can't pass a pack expansion to a non-pack parameter of an alias
7029 template (DR 1430). */
7030 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7031 && variadic_args_p
7032 && nargs - variadic_args_p < nparms - variadic_p)
7033 {
7034 if (complain & tf_error)
7035 {
7036 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7037 {
7038 tree arg = TREE_VEC_ELT (inner_args, i);
7039 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7040
7041 if (PACK_EXPANSION_P (arg)
7042 && !template_parameter_pack_p (parm))
7043 {
7044 error ("pack expansion argument for non-pack parameter "
7045 "%qD of alias template %qD", parm, in_decl);
7046 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7047 goto found;
7048 }
7049 }
7050 gcc_unreachable ();
7051 found:;
7052 }
7053 return error_mark_node;
7054 }
7055
7056 /* We need to evaluate the template arguments, even though this
7057 template-id may be nested within a "sizeof". */
7058 saved_unevaluated_operand = cp_unevaluated_operand;
7059 cp_unevaluated_operand = 0;
7060 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7061 c_inhibit_evaluation_warnings = 0;
7062 new_inner_args = make_tree_vec (nparms);
7063 new_args = add_outermost_template_args (args, new_inner_args);
7064 int pack_adjust = 0;
7065 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7066 {
7067 tree arg;
7068 tree parm;
7069
7070 /* Get the Ith template parameter. */
7071 parm = TREE_VEC_ELT (parms, parm_idx);
7072
7073 if (parm == error_mark_node)
7074 {
7075 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7076 continue;
7077 }
7078
7079 /* Calculate the next argument. */
7080 if (arg_idx < nargs)
7081 arg = TREE_VEC_ELT (inner_args, arg_idx);
7082 else
7083 arg = NULL_TREE;
7084
7085 if (template_parameter_pack_p (TREE_VALUE (parm))
7086 && !(arg && ARGUMENT_PACK_P (arg)))
7087 {
7088 /* Some arguments will be placed in the
7089 template parameter pack PARM. */
7090 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7091 inner_args, arg_idx,
7092 new_args, &lost,
7093 in_decl, complain);
7094
7095 if (arg == NULL_TREE)
7096 {
7097 /* We don't know how many args we have yet, just use the
7098 unconverted (and still packed) ones for now. */
7099 new_inner_args = orig_inner_args;
7100 arg_idx = nargs;
7101 break;
7102 }
7103
7104 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7105
7106 /* Store this argument. */
7107 if (arg == error_mark_node)
7108 {
7109 lost++;
7110 /* We are done with all of the arguments. */
7111 arg_idx = nargs;
7112 }
7113 else
7114 {
7115 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7116 arg_idx += pack_adjust;
7117 }
7118
7119 continue;
7120 }
7121 else if (arg)
7122 {
7123 if (PACK_EXPANSION_P (arg))
7124 {
7125 /* "If every valid specialization of a variadic template
7126 requires an empty template parameter pack, the template is
7127 ill-formed, no diagnostic required." So check that the
7128 pattern works with this parameter. */
7129 tree pattern = PACK_EXPANSION_PATTERN (arg);
7130 tree conv = convert_template_argument (TREE_VALUE (parm),
7131 pattern, new_args,
7132 complain, parm_idx,
7133 in_decl);
7134 if (conv == error_mark_node)
7135 {
7136 inform (input_location, "so any instantiation with a "
7137 "non-empty parameter pack would be ill-formed");
7138 ++lost;
7139 }
7140 else if (TYPE_P (conv) && !TYPE_P (pattern))
7141 /* Recover from missing typename. */
7142 TREE_VEC_ELT (inner_args, arg_idx)
7143 = make_pack_expansion (conv);
7144
7145 /* We don't know how many args we have yet, just
7146 use the unconverted ones for now. */
7147 new_inner_args = inner_args;
7148 arg_idx = nargs;
7149 break;
7150 }
7151 }
7152 else if (require_all_args)
7153 {
7154 /* There must be a default arg in this case. */
7155 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7156 complain, in_decl);
7157 /* The position of the first default template argument,
7158 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7159 Record that. */
7160 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7161 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7162 arg_idx - pack_adjust);
7163 }
7164 else
7165 break;
7166
7167 if (arg == error_mark_node)
7168 {
7169 if (complain & tf_error)
7170 error ("template argument %d is invalid", arg_idx + 1);
7171 }
7172 else if (!arg)
7173 /* This only occurs if there was an error in the template
7174 parameter list itself (which we would already have
7175 reported) that we are trying to recover from, e.g., a class
7176 template with a parameter list such as
7177 template<typename..., typename>. */
7178 ++lost;
7179 else
7180 arg = convert_template_argument (TREE_VALUE (parm),
7181 arg, new_args, complain,
7182 parm_idx, in_decl);
7183
7184 if (arg == error_mark_node)
7185 lost++;
7186 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7187 }
7188 cp_unevaluated_operand = saved_unevaluated_operand;
7189 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7190
7191 if (variadic_p && arg_idx < nargs)
7192 {
7193 if (complain & tf_error)
7194 {
7195 error ("wrong number of template arguments "
7196 "(%d, should be %d)", nargs, arg_idx);
7197 if (in_decl)
7198 error ("provided for %q+D", in_decl);
7199 }
7200 return error_mark_node;
7201 }
7202
7203 if (lost)
7204 return error_mark_node;
7205
7206 #ifdef ENABLE_CHECKING
7207 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7208 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7209 TREE_VEC_LENGTH (new_inner_args));
7210 #endif
7211
7212 return new_inner_args;
7213 }
7214
7215 /* Like coerce_template_parms. If PARMS represents all template
7216 parameters levels, this function returns a vector of vectors
7217 representing all the resulting argument levels. Note that in this
7218 case, only the innermost arguments are coerced because the
7219 outermost ones are supposed to have been coerced already.
7220
7221 Otherwise, if PARMS represents only (the innermost) vector of
7222 parameters, this function returns a vector containing just the
7223 innermost resulting arguments. */
7224
7225 static tree
7226 coerce_innermost_template_parms (tree parms,
7227 tree args,
7228 tree in_decl,
7229 tsubst_flags_t complain,
7230 bool require_all_args,
7231 bool use_default_args)
7232 {
7233 int parms_depth = TMPL_PARMS_DEPTH (parms);
7234 int args_depth = TMPL_ARGS_DEPTH (args);
7235 tree coerced_args;
7236
7237 if (parms_depth > 1)
7238 {
7239 coerced_args = make_tree_vec (parms_depth);
7240 tree level;
7241 int cur_depth;
7242
7243 for (level = parms, cur_depth = parms_depth;
7244 parms_depth > 0 && level != NULL_TREE;
7245 level = TREE_CHAIN (level), --cur_depth)
7246 {
7247 tree l;
7248 if (cur_depth == args_depth)
7249 l = coerce_template_parms (TREE_VALUE (level),
7250 args, in_decl, complain,
7251 require_all_args,
7252 use_default_args);
7253 else
7254 l = TMPL_ARGS_LEVEL (args, cur_depth);
7255
7256 if (l == error_mark_node)
7257 return error_mark_node;
7258
7259 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7260 }
7261 }
7262 else
7263 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7264 args, in_decl, complain,
7265 require_all_args,
7266 use_default_args);
7267 return coerced_args;
7268 }
7269
7270 /* Returns 1 if template args OT and NT are equivalent. */
7271
7272 static int
7273 template_args_equal (tree ot, tree nt)
7274 {
7275 if (nt == ot)
7276 return 1;
7277 if (nt == NULL_TREE || ot == NULL_TREE)
7278 return false;
7279
7280 if (TREE_CODE (nt) == TREE_VEC)
7281 /* For member templates */
7282 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7283 else if (PACK_EXPANSION_P (ot))
7284 return (PACK_EXPANSION_P (nt)
7285 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7286 PACK_EXPANSION_PATTERN (nt))
7287 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7288 PACK_EXPANSION_EXTRA_ARGS (nt)));
7289 else if (ARGUMENT_PACK_P (ot))
7290 {
7291 int i, len;
7292 tree opack, npack;
7293
7294 if (!ARGUMENT_PACK_P (nt))
7295 return 0;
7296
7297 opack = ARGUMENT_PACK_ARGS (ot);
7298 npack = ARGUMENT_PACK_ARGS (nt);
7299 len = TREE_VEC_LENGTH (opack);
7300 if (TREE_VEC_LENGTH (npack) != len)
7301 return 0;
7302 for (i = 0; i < len; ++i)
7303 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7304 TREE_VEC_ELT (npack, i)))
7305 return 0;
7306 return 1;
7307 }
7308 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7309 {
7310 /* We get here probably because we are in the middle of substituting
7311 into the pattern of a pack expansion. In that case the
7312 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7313 interested in. So we want to use the initial pack argument for
7314 the comparison. */
7315 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7316 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7317 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7318 return template_args_equal (ot, nt);
7319 }
7320 else if (TYPE_P (nt))
7321 {
7322 if (!TYPE_P (ot))
7323 return false;
7324 /* Don't treat an alias template specialization with dependent
7325 arguments as equivalent to its underlying type when used as a
7326 template argument; we need them to be distinct so that we
7327 substitute into the specialization arguments at instantiation
7328 time. And aliases can't be equivalent without being ==, so
7329 we don't need to look any deeper. */
7330 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7331 return false;
7332 else
7333 return same_type_p (ot, nt);
7334 }
7335 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7336 return 0;
7337 else
7338 {
7339 /* Try to treat a template non-type argument that has been converted
7340 to the parameter type as equivalent to one that hasn't yet. */
7341 for (enum tree_code code1 = TREE_CODE (ot);
7342 CONVERT_EXPR_CODE_P (code1)
7343 || code1 == NON_LVALUE_EXPR;
7344 code1 = TREE_CODE (ot))
7345 ot = TREE_OPERAND (ot, 0);
7346 for (enum tree_code code2 = TREE_CODE (nt);
7347 CONVERT_EXPR_CODE_P (code2)
7348 || code2 == NON_LVALUE_EXPR;
7349 code2 = TREE_CODE (nt))
7350 nt = TREE_OPERAND (nt, 0);
7351
7352 return cp_tree_equal (ot, nt);
7353 }
7354 }
7355
7356 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7357 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7358 NEWARG_PTR with the offending arguments if they are non-NULL. */
7359
7360 static int
7361 comp_template_args_with_info (tree oldargs, tree newargs,
7362 tree *oldarg_ptr, tree *newarg_ptr)
7363 {
7364 int i;
7365
7366 if (oldargs == newargs)
7367 return 1;
7368
7369 if (!oldargs || !newargs)
7370 return 0;
7371
7372 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7373 return 0;
7374
7375 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7376 {
7377 tree nt = TREE_VEC_ELT (newargs, i);
7378 tree ot = TREE_VEC_ELT (oldargs, i);
7379
7380 if (! template_args_equal (ot, nt))
7381 {
7382 if (oldarg_ptr != NULL)
7383 *oldarg_ptr = ot;
7384 if (newarg_ptr != NULL)
7385 *newarg_ptr = nt;
7386 return 0;
7387 }
7388 }
7389 return 1;
7390 }
7391
7392 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7393 of template arguments. Returns 0 otherwise. */
7394
7395 int
7396 comp_template_args (tree oldargs, tree newargs)
7397 {
7398 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7399 }
7400
7401 static void
7402 add_pending_template (tree d)
7403 {
7404 tree ti = (TYPE_P (d)
7405 ? CLASSTYPE_TEMPLATE_INFO (d)
7406 : DECL_TEMPLATE_INFO (d));
7407 struct pending_template *pt;
7408 int level;
7409
7410 if (TI_PENDING_TEMPLATE_FLAG (ti))
7411 return;
7412
7413 /* We are called both from instantiate_decl, where we've already had a
7414 tinst_level pushed, and instantiate_template, where we haven't.
7415 Compensate. */
7416 level = !current_tinst_level || current_tinst_level->decl != d;
7417
7418 if (level)
7419 push_tinst_level (d);
7420
7421 pt = ggc_alloc<pending_template> ();
7422 pt->next = NULL;
7423 pt->tinst = current_tinst_level;
7424 if (last_pending_template)
7425 last_pending_template->next = pt;
7426 else
7427 pending_templates = pt;
7428
7429 last_pending_template = pt;
7430
7431 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7432
7433 if (level)
7434 pop_tinst_level ();
7435 }
7436
7437
7438 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7439 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7440 documentation for TEMPLATE_ID_EXPR. */
7441
7442 tree
7443 lookup_template_function (tree fns, tree arglist)
7444 {
7445 tree type;
7446
7447 if (fns == error_mark_node || arglist == error_mark_node)
7448 return error_mark_node;
7449
7450 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7451
7452 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7453 {
7454 error ("%q#D is not a function template", fns);
7455 return error_mark_node;
7456 }
7457
7458 if (BASELINK_P (fns))
7459 {
7460 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7461 unknown_type_node,
7462 BASELINK_FUNCTIONS (fns),
7463 arglist);
7464 return fns;
7465 }
7466
7467 type = TREE_TYPE (fns);
7468 if (TREE_CODE (fns) == OVERLOAD || !type)
7469 type = unknown_type_node;
7470
7471 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7472 }
7473
7474 /* Within the scope of a template class S<T>, the name S gets bound
7475 (in build_self_reference) to a TYPE_DECL for the class, not a
7476 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7477 or one of its enclosing classes, and that type is a template,
7478 return the associated TEMPLATE_DECL. Otherwise, the original
7479 DECL is returned.
7480
7481 Also handle the case when DECL is a TREE_LIST of ambiguous
7482 injected-class-names from different bases. */
7483
7484 tree
7485 maybe_get_template_decl_from_type_decl (tree decl)
7486 {
7487 if (decl == NULL_TREE)
7488 return decl;
7489
7490 /* DR 176: A lookup that finds an injected-class-name (10.2
7491 [class.member.lookup]) can result in an ambiguity in certain cases
7492 (for example, if it is found in more than one base class). If all of
7493 the injected-class-names that are found refer to specializations of
7494 the same class template, and if the name is followed by a
7495 template-argument-list, the reference refers to the class template
7496 itself and not a specialization thereof, and is not ambiguous. */
7497 if (TREE_CODE (decl) == TREE_LIST)
7498 {
7499 tree t, tmpl = NULL_TREE;
7500 for (t = decl; t; t = TREE_CHAIN (t))
7501 {
7502 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7503 if (!tmpl)
7504 tmpl = elt;
7505 else if (tmpl != elt)
7506 break;
7507 }
7508 if (tmpl && t == NULL_TREE)
7509 return tmpl;
7510 else
7511 return decl;
7512 }
7513
7514 return (decl != NULL_TREE
7515 && DECL_SELF_REFERENCE_P (decl)
7516 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7517 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7518 }
7519
7520 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7521 parameters, find the desired type.
7522
7523 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7524
7525 IN_DECL, if non-NULL, is the template declaration we are trying to
7526 instantiate.
7527
7528 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7529 the class we are looking up.
7530
7531 Issue error and warning messages under control of COMPLAIN.
7532
7533 If the template class is really a local class in a template
7534 function, then the FUNCTION_CONTEXT is the function in which it is
7535 being instantiated.
7536
7537 ??? Note that this function is currently called *twice* for each
7538 template-id: the first time from the parser, while creating the
7539 incomplete type (finish_template_type), and the second type during the
7540 real instantiation (instantiate_template_class). This is surely something
7541 that we want to avoid. It also causes some problems with argument
7542 coercion (see convert_nontype_argument for more information on this). */
7543
7544 static tree
7545 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7546 int entering_scope, tsubst_flags_t complain)
7547 {
7548 tree templ = NULL_TREE, parmlist;
7549 tree t;
7550 spec_entry **slot;
7551 spec_entry *entry;
7552 spec_entry elt;
7553 hashval_t hash;
7554
7555 if (identifier_p (d1))
7556 {
7557 tree value = innermost_non_namespace_value (d1);
7558 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7559 templ = value;
7560 else
7561 {
7562 if (context)
7563 push_decl_namespace (context);
7564 templ = lookup_name (d1);
7565 templ = maybe_get_template_decl_from_type_decl (templ);
7566 if (context)
7567 pop_decl_namespace ();
7568 }
7569 if (templ)
7570 context = DECL_CONTEXT (templ);
7571 }
7572 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7573 {
7574 tree type = TREE_TYPE (d1);
7575
7576 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7577 an implicit typename for the second A. Deal with it. */
7578 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7579 type = TREE_TYPE (type);
7580
7581 if (CLASSTYPE_TEMPLATE_INFO (type))
7582 {
7583 templ = CLASSTYPE_TI_TEMPLATE (type);
7584 d1 = DECL_NAME (templ);
7585 }
7586 }
7587 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7588 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7589 {
7590 templ = TYPE_TI_TEMPLATE (d1);
7591 d1 = DECL_NAME (templ);
7592 }
7593 else if (DECL_TYPE_TEMPLATE_P (d1))
7594 {
7595 templ = d1;
7596 d1 = DECL_NAME (templ);
7597 context = DECL_CONTEXT (templ);
7598 }
7599 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7600 {
7601 templ = d1;
7602 d1 = DECL_NAME (templ);
7603 }
7604
7605 /* Issue an error message if we didn't find a template. */
7606 if (! templ)
7607 {
7608 if (complain & tf_error)
7609 error ("%qT is not a template", d1);
7610 return error_mark_node;
7611 }
7612
7613 if (TREE_CODE (templ) != TEMPLATE_DECL
7614 /* Make sure it's a user visible template, if it was named by
7615 the user. */
7616 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7617 && !PRIMARY_TEMPLATE_P (templ)))
7618 {
7619 if (complain & tf_error)
7620 {
7621 error ("non-template type %qT used as a template", d1);
7622 if (in_decl)
7623 error ("for template declaration %q+D", in_decl);
7624 }
7625 return error_mark_node;
7626 }
7627
7628 complain &= ~tf_user;
7629
7630 /* An alias that just changes the name of a template is equivalent to the
7631 other template, so if any of the arguments are pack expansions, strip
7632 the alias to avoid problems with a pack expansion passed to a non-pack
7633 alias template parameter (DR 1430). */
7634 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7635 templ = get_underlying_template (templ);
7636
7637 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7638 {
7639 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7640 template arguments */
7641
7642 tree parm;
7643 tree arglist2;
7644 tree outer;
7645
7646 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7647
7648 /* Consider an example where a template template parameter declared as
7649
7650 template <class T, class U = std::allocator<T> > class TT
7651
7652 The template parameter level of T and U are one level larger than
7653 of TT. To proper process the default argument of U, say when an
7654 instantiation `TT<int>' is seen, we need to build the full
7655 arguments containing {int} as the innermost level. Outer levels,
7656 available when not appearing as default template argument, can be
7657 obtained from the arguments of the enclosing template.
7658
7659 Suppose that TT is later substituted with std::vector. The above
7660 instantiation is `TT<int, std::allocator<T> >' with TT at
7661 level 1, and T at level 2, while the template arguments at level 1
7662 becomes {std::vector} and the inner level 2 is {int}. */
7663
7664 outer = DECL_CONTEXT (templ);
7665 if (outer)
7666 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7667 else if (current_template_parms)
7668 /* This is an argument of the current template, so we haven't set
7669 DECL_CONTEXT yet. */
7670 outer = current_template_args ();
7671
7672 if (outer)
7673 arglist = add_to_template_args (outer, arglist);
7674
7675 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7676 complain,
7677 /*require_all_args=*/true,
7678 /*use_default_args=*/true);
7679 if (arglist2 == error_mark_node
7680 || (!uses_template_parms (arglist2)
7681 && check_instantiated_args (templ, arglist2, complain)))
7682 return error_mark_node;
7683
7684 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7685 return parm;
7686 }
7687 else
7688 {
7689 tree template_type = TREE_TYPE (templ);
7690 tree gen_tmpl;
7691 tree type_decl;
7692 tree found = NULL_TREE;
7693 int arg_depth;
7694 int parm_depth;
7695 int is_dependent_type;
7696 int use_partial_inst_tmpl = false;
7697
7698 if (template_type == error_mark_node)
7699 /* An error occurred while building the template TEMPL, and a
7700 diagnostic has most certainly been emitted for that
7701 already. Let's propagate that error. */
7702 return error_mark_node;
7703
7704 gen_tmpl = most_general_template (templ);
7705 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7706 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7707 arg_depth = TMPL_ARGS_DEPTH (arglist);
7708
7709 if (arg_depth == 1 && parm_depth > 1)
7710 {
7711 /* We've been given an incomplete set of template arguments.
7712 For example, given:
7713
7714 template <class T> struct S1 {
7715 template <class U> struct S2 {};
7716 template <class U> struct S2<U*> {};
7717 };
7718
7719 we will be called with an ARGLIST of `U*', but the
7720 TEMPLATE will be `template <class T> template
7721 <class U> struct S1<T>::S2'. We must fill in the missing
7722 arguments. */
7723 arglist
7724 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7725 arglist);
7726 arg_depth = TMPL_ARGS_DEPTH (arglist);
7727 }
7728
7729 /* Now we should have enough arguments. */
7730 gcc_assert (parm_depth == arg_depth);
7731
7732 /* From here on, we're only interested in the most general
7733 template. */
7734
7735 /* Calculate the BOUND_ARGS. These will be the args that are
7736 actually tsubst'd into the definition to create the
7737 instantiation. */
7738 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
7739 complain,
7740 /*require_all_args=*/true,
7741 /*use_default_args=*/true);
7742
7743 if (arglist == error_mark_node)
7744 /* We were unable to bind the arguments. */
7745 return error_mark_node;
7746
7747 /* In the scope of a template class, explicit references to the
7748 template class refer to the type of the template, not any
7749 instantiation of it. For example, in:
7750
7751 template <class T> class C { void f(C<T>); }
7752
7753 the `C<T>' is just the same as `C'. Outside of the
7754 class, however, such a reference is an instantiation. */
7755 if ((entering_scope
7756 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7757 || currently_open_class (template_type))
7758 /* comp_template_args is expensive, check it last. */
7759 && comp_template_args (TYPE_TI_ARGS (template_type),
7760 arglist))
7761 return template_type;
7762
7763 /* If we already have this specialization, return it. */
7764 elt.tmpl = gen_tmpl;
7765 elt.args = arglist;
7766 hash = spec_hasher::hash (&elt);
7767 entry = type_specializations->find_with_hash (&elt, hash);
7768
7769 if (entry)
7770 return entry->spec;
7771
7772 is_dependent_type = uses_template_parms (arglist);
7773
7774 /* If the deduced arguments are invalid, then the binding
7775 failed. */
7776 if (!is_dependent_type
7777 && check_instantiated_args (gen_tmpl,
7778 INNERMOST_TEMPLATE_ARGS (arglist),
7779 complain))
7780 return error_mark_node;
7781
7782 if (!is_dependent_type
7783 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7784 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7785 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7786 {
7787 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7788 DECL_NAME (gen_tmpl),
7789 /*tag_scope=*/ts_global);
7790 return found;
7791 }
7792
7793 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7794 complain, in_decl);
7795 if (context == error_mark_node)
7796 return error_mark_node;
7797
7798 if (!context)
7799 context = global_namespace;
7800
7801 /* Create the type. */
7802 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7803 {
7804 /* The user referred to a specialization of an alias
7805 template represented by GEN_TMPL.
7806
7807 [temp.alias]/2 says:
7808
7809 When a template-id refers to the specialization of an
7810 alias template, it is equivalent to the associated
7811 type obtained by substitution of its
7812 template-arguments for the template-parameters in the
7813 type-id of the alias template. */
7814
7815 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7816 /* Note that the call above (by indirectly calling
7817 register_specialization in tsubst_decl) registers the
7818 TYPE_DECL representing the specialization of the alias
7819 template. So next time someone substitutes ARGLIST for
7820 the template parms into the alias template (GEN_TMPL),
7821 she'll get that TYPE_DECL back. */
7822
7823 if (t == error_mark_node)
7824 return t;
7825 }
7826 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7827 {
7828 if (!is_dependent_type)
7829 {
7830 set_current_access_from_decl (TYPE_NAME (template_type));
7831 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7832 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7833 arglist, complain, in_decl),
7834 SCOPED_ENUM_P (template_type), NULL);
7835
7836 if (t == error_mark_node)
7837 return t;
7838 }
7839 else
7840 {
7841 /* We don't want to call start_enum for this type, since
7842 the values for the enumeration constants may involve
7843 template parameters. And, no one should be interested
7844 in the enumeration constants for such a type. */
7845 t = cxx_make_type (ENUMERAL_TYPE);
7846 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7847 }
7848 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7849 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7850 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7851 }
7852 else if (CLASS_TYPE_P (template_type))
7853 {
7854 t = make_class_type (TREE_CODE (template_type));
7855 CLASSTYPE_DECLARED_CLASS (t)
7856 = CLASSTYPE_DECLARED_CLASS (template_type);
7857 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7858 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7859
7860 /* A local class. Make sure the decl gets registered properly. */
7861 if (context == current_function_decl)
7862 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7863
7864 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7865 /* This instantiation is another name for the primary
7866 template type. Set the TYPE_CANONICAL field
7867 appropriately. */
7868 TYPE_CANONICAL (t) = template_type;
7869 else if (any_template_arguments_need_structural_equality_p (arglist))
7870 /* Some of the template arguments require structural
7871 equality testing, so this template class requires
7872 structural equality testing. */
7873 SET_TYPE_STRUCTURAL_EQUALITY (t);
7874 }
7875 else
7876 gcc_unreachable ();
7877
7878 /* If we called start_enum or pushtag above, this information
7879 will already be set up. */
7880 if (!TYPE_NAME (t))
7881 {
7882 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7883
7884 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7885 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7886 DECL_SOURCE_LOCATION (type_decl)
7887 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7888 }
7889 else
7890 type_decl = TYPE_NAME (t);
7891
7892 if (CLASS_TYPE_P (template_type))
7893 {
7894 TREE_PRIVATE (type_decl)
7895 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7896 TREE_PROTECTED (type_decl)
7897 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7898 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7899 {
7900 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7901 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7902 }
7903 }
7904
7905 if (OVERLOAD_TYPE_P (t)
7906 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7907 {
7908 if (tree attributes
7909 = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (template_type)))
7910 {
7911 if (!TREE_CHAIN (attributes))
7912 TYPE_ATTRIBUTES (t) = attributes;
7913 else
7914 TYPE_ATTRIBUTES (t)
7915 = build_tree_list (TREE_PURPOSE (attributes),
7916 TREE_VALUE (attributes));
7917 }
7918 }
7919
7920 /* Let's consider the explicit specialization of a member
7921 of a class template specialization that is implicitly instantiated,
7922 e.g.:
7923 template<class T>
7924 struct S
7925 {
7926 template<class U> struct M {}; //#0
7927 };
7928
7929 template<>
7930 template<>
7931 struct S<int>::M<char> //#1
7932 {
7933 int i;
7934 };
7935 [temp.expl.spec]/4 says this is valid.
7936
7937 In this case, when we write:
7938 S<int>::M<char> m;
7939
7940 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7941 the one of #0.
7942
7943 When we encounter #1, we want to store the partial instantiation
7944 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7945
7946 For all cases other than this "explicit specialization of member of a
7947 class template", we just want to store the most general template into
7948 the CLASSTYPE_TI_TEMPLATE of M.
7949
7950 This case of "explicit specialization of member of a class template"
7951 only happens when:
7952 1/ the enclosing class is an instantiation of, and therefore not
7953 the same as, the context of the most general template, and
7954 2/ we aren't looking at the partial instantiation itself, i.e.
7955 the innermost arguments are not the same as the innermost parms of
7956 the most general template.
7957
7958 So it's only when 1/ and 2/ happens that we want to use the partial
7959 instantiation of the member template in lieu of its most general
7960 template. */
7961
7962 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7963 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7964 /* the enclosing class must be an instantiation... */
7965 && CLASS_TYPE_P (context)
7966 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7967 {
7968 tree partial_inst_args;
7969 TREE_VEC_LENGTH (arglist)--;
7970 ++processing_template_decl;
7971 partial_inst_args =
7972 tsubst (INNERMOST_TEMPLATE_ARGS
7973 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7974 arglist, complain, NULL_TREE);
7975 --processing_template_decl;
7976 TREE_VEC_LENGTH (arglist)++;
7977 use_partial_inst_tmpl =
7978 /*...and we must not be looking at the partial instantiation
7979 itself. */
7980 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7981 partial_inst_args);
7982 }
7983
7984 if (!use_partial_inst_tmpl)
7985 /* This case is easy; there are no member templates involved. */
7986 found = gen_tmpl;
7987 else
7988 {
7989 /* This is a full instantiation of a member template. Find
7990 the partial instantiation of which this is an instance. */
7991
7992 /* Temporarily reduce by one the number of levels in the ARGLIST
7993 so as to avoid comparing the last set of arguments. */
7994 TREE_VEC_LENGTH (arglist)--;
7995 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7996 TREE_VEC_LENGTH (arglist)++;
7997 /* FOUND is either a proper class type, or an alias
7998 template specialization. In the later case, it's a
7999 TYPE_DECL, resulting from the substituting of arguments
8000 for parameters in the TYPE_DECL of the alias template
8001 done earlier. So be careful while getting the template
8002 of FOUND. */
8003 found = TREE_CODE (found) == TYPE_DECL
8004 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8005 : CLASSTYPE_TI_TEMPLATE (found);
8006 }
8007
8008 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8009
8010 elt.spec = t;
8011 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8012 entry = ggc_alloc<spec_entry> ();
8013 *entry = elt;
8014 *slot = entry;
8015
8016 /* Note this use of the partial instantiation so we can check it
8017 later in maybe_process_partial_specialization. */
8018 DECL_TEMPLATE_INSTANTIATIONS (found)
8019 = tree_cons (arglist, t,
8020 DECL_TEMPLATE_INSTANTIATIONS (found));
8021
8022 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8023 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8024 /* Now that the type has been registered on the instantiations
8025 list, we set up the enumerators. Because the enumeration
8026 constants may involve the enumeration type itself, we make
8027 sure to register the type first, and then create the
8028 constants. That way, doing tsubst_expr for the enumeration
8029 constants won't result in recursive calls here; we'll find
8030 the instantiation and exit above. */
8031 tsubst_enum (template_type, t, arglist);
8032
8033 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8034 /* If the type makes use of template parameters, the
8035 code that generates debugging information will crash. */
8036 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8037
8038 /* Possibly limit visibility based on template args. */
8039 TREE_PUBLIC (type_decl) = 1;
8040 determine_visibility (type_decl);
8041
8042 inherit_targ_abi_tags (t);
8043
8044 return t;
8045 }
8046 }
8047
8048 /* Wrapper for lookup_template_class_1. */
8049
8050 tree
8051 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8052 int entering_scope, tsubst_flags_t complain)
8053 {
8054 tree ret;
8055 timevar_push (TV_TEMPLATE_INST);
8056 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8057 entering_scope, complain);
8058 timevar_pop (TV_TEMPLATE_INST);
8059 return ret;
8060 }
8061
8062 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8063 The type of the expression is the unknown_type_node since the
8064 template-id could refer to an explicit or partial specialization. */
8065
8066 tree
8067 lookup_template_variable (tree templ, tree arglist)
8068 {
8069 tree type = unknown_type_node;
8070 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8071 }
8072
8073 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8074
8075 tree
8076 finish_template_variable (tree var)
8077 {
8078 tree templ = TREE_OPERAND (var, 0);
8079
8080 tree arglist = TREE_OPERAND (var, 1);
8081 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8082 arglist = add_outermost_template_args (tmpl_args, arglist);
8083
8084 tree parms = DECL_TEMPLATE_PARMS (templ);
8085 tsubst_flags_t complain = tf_warning_or_error;
8086 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8087 /*req_all*/true,
8088 /*use_default*/true);
8089
8090 return instantiate_template (templ, arglist, complain);
8091 }
8092 \f
8093 struct pair_fn_data
8094 {
8095 tree_fn_t fn;
8096 void *data;
8097 /* True when we should also visit template parameters that occur in
8098 non-deduced contexts. */
8099 bool include_nondeduced_p;
8100 hash_set<tree> *visited;
8101 };
8102
8103 /* Called from for_each_template_parm via walk_tree. */
8104
8105 static tree
8106 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8107 {
8108 tree t = *tp;
8109 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8110 tree_fn_t fn = pfd->fn;
8111 void *data = pfd->data;
8112
8113 if (TYPE_P (t)
8114 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8115 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8116 pfd->include_nondeduced_p))
8117 return error_mark_node;
8118
8119 switch (TREE_CODE (t))
8120 {
8121 case RECORD_TYPE:
8122 if (TYPE_PTRMEMFUNC_P (t))
8123 break;
8124 /* Fall through. */
8125
8126 case UNION_TYPE:
8127 case ENUMERAL_TYPE:
8128 if (!TYPE_TEMPLATE_INFO (t))
8129 *walk_subtrees = 0;
8130 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8131 fn, data, pfd->visited,
8132 pfd->include_nondeduced_p))
8133 return error_mark_node;
8134 break;
8135
8136 case INTEGER_TYPE:
8137 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8138 fn, data, pfd->visited,
8139 pfd->include_nondeduced_p)
8140 || for_each_template_parm (TYPE_MAX_VALUE (t),
8141 fn, data, pfd->visited,
8142 pfd->include_nondeduced_p))
8143 return error_mark_node;
8144 break;
8145
8146 case METHOD_TYPE:
8147 /* Since we're not going to walk subtrees, we have to do this
8148 explicitly here. */
8149 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8150 pfd->visited, pfd->include_nondeduced_p))
8151 return error_mark_node;
8152 /* Fall through. */
8153
8154 case FUNCTION_TYPE:
8155 /* Check the return type. */
8156 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8157 pfd->include_nondeduced_p))
8158 return error_mark_node;
8159
8160 /* Check the parameter types. Since default arguments are not
8161 instantiated until they are needed, the TYPE_ARG_TYPES may
8162 contain expressions that involve template parameters. But,
8163 no-one should be looking at them yet. And, once they're
8164 instantiated, they don't contain template parameters, so
8165 there's no point in looking at them then, either. */
8166 {
8167 tree parm;
8168
8169 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8170 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8171 pfd->visited, pfd->include_nondeduced_p))
8172 return error_mark_node;
8173
8174 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8175 want walk_tree walking into them itself. */
8176 *walk_subtrees = 0;
8177 }
8178 break;
8179
8180 case TYPEOF_TYPE:
8181 case UNDERLYING_TYPE:
8182 if (pfd->include_nondeduced_p
8183 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
8184 pfd->visited,
8185 pfd->include_nondeduced_p))
8186 return error_mark_node;
8187 break;
8188
8189 case FUNCTION_DECL:
8190 case VAR_DECL:
8191 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8192 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8193 pfd->visited, pfd->include_nondeduced_p))
8194 return error_mark_node;
8195 /* Fall through. */
8196
8197 case PARM_DECL:
8198 case CONST_DECL:
8199 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8200 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8201 pfd->visited, pfd->include_nondeduced_p))
8202 return error_mark_node;
8203 if (DECL_CONTEXT (t)
8204 && pfd->include_nondeduced_p
8205 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8206 pfd->visited, pfd->include_nondeduced_p))
8207 return error_mark_node;
8208 break;
8209
8210 case BOUND_TEMPLATE_TEMPLATE_PARM:
8211 /* Record template parameters such as `T' inside `TT<T>'. */
8212 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8213 pfd->include_nondeduced_p))
8214 return error_mark_node;
8215 /* Fall through. */
8216
8217 case TEMPLATE_TEMPLATE_PARM:
8218 case TEMPLATE_TYPE_PARM:
8219 case TEMPLATE_PARM_INDEX:
8220 if (fn && (*fn)(t, data))
8221 return error_mark_node;
8222 else if (!fn)
8223 return error_mark_node;
8224 break;
8225
8226 case TEMPLATE_DECL:
8227 /* A template template parameter is encountered. */
8228 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8229 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8230 pfd->include_nondeduced_p))
8231 return error_mark_node;
8232
8233 /* Already substituted template template parameter */
8234 *walk_subtrees = 0;
8235 break;
8236
8237 case TYPENAME_TYPE:
8238 if (!fn
8239 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8240 data, pfd->visited,
8241 pfd->include_nondeduced_p))
8242 return error_mark_node;
8243 break;
8244
8245 case CONSTRUCTOR:
8246 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8247 && pfd->include_nondeduced_p
8248 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8249 (TREE_TYPE (t)), fn, data,
8250 pfd->visited, pfd->include_nondeduced_p))
8251 return error_mark_node;
8252 break;
8253
8254 case INDIRECT_REF:
8255 case COMPONENT_REF:
8256 /* If there's no type, then this thing must be some expression
8257 involving template parameters. */
8258 if (!fn && !TREE_TYPE (t))
8259 return error_mark_node;
8260 break;
8261
8262 case MODOP_EXPR:
8263 case CAST_EXPR:
8264 case IMPLICIT_CONV_EXPR:
8265 case REINTERPRET_CAST_EXPR:
8266 case CONST_CAST_EXPR:
8267 case STATIC_CAST_EXPR:
8268 case DYNAMIC_CAST_EXPR:
8269 case ARROW_EXPR:
8270 case DOTSTAR_EXPR:
8271 case TYPEID_EXPR:
8272 case PSEUDO_DTOR_EXPR:
8273 if (!fn)
8274 return error_mark_node;
8275 break;
8276
8277 default:
8278 break;
8279 }
8280
8281 /* We didn't find any template parameters we liked. */
8282 return NULL_TREE;
8283 }
8284
8285 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8286 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8287 call FN with the parameter and the DATA.
8288 If FN returns nonzero, the iteration is terminated, and
8289 for_each_template_parm returns 1. Otherwise, the iteration
8290 continues. If FN never returns a nonzero value, the value
8291 returned by for_each_template_parm is 0. If FN is NULL, it is
8292 considered to be the function which always returns 1.
8293
8294 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8295 parameters that occur in non-deduced contexts. When false, only
8296 visits those template parameters that can be deduced. */
8297
8298 static int
8299 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8300 hash_set<tree> *visited,
8301 bool include_nondeduced_p)
8302 {
8303 struct pair_fn_data pfd;
8304 int result;
8305
8306 /* Set up. */
8307 pfd.fn = fn;
8308 pfd.data = data;
8309 pfd.include_nondeduced_p = include_nondeduced_p;
8310
8311 /* Walk the tree. (Conceptually, we would like to walk without
8312 duplicates, but for_each_template_parm_r recursively calls
8313 for_each_template_parm, so we would need to reorganize a fair
8314 bit to use walk_tree_without_duplicates, so we keep our own
8315 visited list.) */
8316 if (visited)
8317 pfd.visited = visited;
8318 else
8319 pfd.visited = new hash_set<tree>;
8320 result = cp_walk_tree (&t,
8321 for_each_template_parm_r,
8322 &pfd,
8323 pfd.visited) != NULL_TREE;
8324
8325 /* Clean up. */
8326 if (!visited)
8327 {
8328 delete pfd.visited;
8329 pfd.visited = 0;
8330 }
8331
8332 return result;
8333 }
8334
8335 /* Returns true if T depends on any template parameter. */
8336
8337 int
8338 uses_template_parms (tree t)
8339 {
8340 if (t == NULL_TREE)
8341 return false;
8342
8343 bool dependent_p;
8344 int saved_processing_template_decl;
8345
8346 saved_processing_template_decl = processing_template_decl;
8347 if (!saved_processing_template_decl)
8348 processing_template_decl = 1;
8349 if (TYPE_P (t))
8350 dependent_p = dependent_type_p (t);
8351 else if (TREE_CODE (t) == TREE_VEC)
8352 dependent_p = any_dependent_template_arguments_p (t);
8353 else if (TREE_CODE (t) == TREE_LIST)
8354 dependent_p = (uses_template_parms (TREE_VALUE (t))
8355 || uses_template_parms (TREE_CHAIN (t)));
8356 else if (TREE_CODE (t) == TYPE_DECL)
8357 dependent_p = dependent_type_p (TREE_TYPE (t));
8358 else if (DECL_P (t)
8359 || EXPR_P (t)
8360 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8361 || TREE_CODE (t) == OVERLOAD
8362 || BASELINK_P (t)
8363 || identifier_p (t)
8364 || TREE_CODE (t) == TRAIT_EXPR
8365 || TREE_CODE (t) == CONSTRUCTOR
8366 || CONSTANT_CLASS_P (t))
8367 dependent_p = (type_dependent_expression_p (t)
8368 || value_dependent_expression_p (t));
8369 else
8370 {
8371 gcc_assert (t == error_mark_node);
8372 dependent_p = false;
8373 }
8374
8375 processing_template_decl = saved_processing_template_decl;
8376
8377 return dependent_p;
8378 }
8379
8380 /* Returns true iff current_function_decl is an incompletely instantiated
8381 template. Useful instead of processing_template_decl because the latter
8382 is set to 0 during instantiate_non_dependent_expr. */
8383
8384 bool
8385 in_template_function (void)
8386 {
8387 tree fn = current_function_decl;
8388 bool ret;
8389 ++processing_template_decl;
8390 ret = (fn && DECL_LANG_SPECIFIC (fn)
8391 && DECL_TEMPLATE_INFO (fn)
8392 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8393 --processing_template_decl;
8394 return ret;
8395 }
8396
8397 /* Returns true if T depends on any template parameter with level LEVEL. */
8398
8399 int
8400 uses_template_parms_level (tree t, int level)
8401 {
8402 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8403 /*include_nondeduced_p=*/true);
8404 }
8405
8406 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8407 ill-formed translation unit, i.e. a variable or function that isn't
8408 usable in a constant expression. */
8409
8410 static inline bool
8411 neglectable_inst_p (tree d)
8412 {
8413 return (DECL_P (d)
8414 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8415 : decl_maybe_constant_var_p (d)));
8416 }
8417
8418 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8419 neglectable and instantiated from within an erroneous instantiation. */
8420
8421 static bool
8422 limit_bad_template_recursion (tree decl)
8423 {
8424 struct tinst_level *lev = current_tinst_level;
8425 int errs = errorcount + sorrycount;
8426 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8427 return false;
8428
8429 for (; lev; lev = lev->next)
8430 if (neglectable_inst_p (lev->decl))
8431 break;
8432
8433 return (lev && errs > lev->errors);
8434 }
8435
8436 static int tinst_depth;
8437 extern int max_tinst_depth;
8438 int depth_reached;
8439
8440 static GTY(()) struct tinst_level *last_error_tinst_level;
8441
8442 /* We're starting to instantiate D; record the template instantiation context
8443 for diagnostics and to restore it later. */
8444
8445 bool
8446 push_tinst_level (tree d)
8447 {
8448 return push_tinst_level_loc (d, input_location);
8449 }
8450
8451 /* We're starting to instantiate D; record the template instantiation context
8452 at LOC for diagnostics and to restore it later. */
8453
8454 bool
8455 push_tinst_level_loc (tree d, location_t loc)
8456 {
8457 struct tinst_level *new_level;
8458
8459 if (tinst_depth >= max_tinst_depth)
8460 {
8461 fatal_error (input_location,
8462 "template instantiation depth exceeds maximum of %d"
8463 " (use -ftemplate-depth= to increase the maximum)",
8464 max_tinst_depth);
8465 return false;
8466 }
8467
8468 /* If the current instantiation caused problems, don't let it instantiate
8469 anything else. Do allow deduction substitution and decls usable in
8470 constant expressions. */
8471 if (limit_bad_template_recursion (d))
8472 return false;
8473
8474 new_level = ggc_alloc<tinst_level> ();
8475 new_level->decl = d;
8476 new_level->locus = loc;
8477 new_level->errors = errorcount+sorrycount;
8478 new_level->in_system_header_p = in_system_header_at (input_location);
8479 new_level->next = current_tinst_level;
8480 current_tinst_level = new_level;
8481
8482 ++tinst_depth;
8483 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8484 depth_reached = tinst_depth;
8485
8486 return true;
8487 }
8488
8489 /* We're done instantiating this template; return to the instantiation
8490 context. */
8491
8492 void
8493 pop_tinst_level (void)
8494 {
8495 /* Restore the filename and line number stashed away when we started
8496 this instantiation. */
8497 input_location = current_tinst_level->locus;
8498 current_tinst_level = current_tinst_level->next;
8499 --tinst_depth;
8500 }
8501
8502 /* We're instantiating a deferred template; restore the template
8503 instantiation context in which the instantiation was requested, which
8504 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8505
8506 static tree
8507 reopen_tinst_level (struct tinst_level *level)
8508 {
8509 struct tinst_level *t;
8510
8511 tinst_depth = 0;
8512 for (t = level; t; t = t->next)
8513 ++tinst_depth;
8514
8515 current_tinst_level = level;
8516 pop_tinst_level ();
8517 if (current_tinst_level)
8518 current_tinst_level->errors = errorcount+sorrycount;
8519 return level->decl;
8520 }
8521
8522 /* Returns the TINST_LEVEL which gives the original instantiation
8523 context. */
8524
8525 struct tinst_level *
8526 outermost_tinst_level (void)
8527 {
8528 struct tinst_level *level = current_tinst_level;
8529 if (level)
8530 while (level->next)
8531 level = level->next;
8532 return level;
8533 }
8534
8535 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8536 vector of template arguments, as for tsubst.
8537
8538 Returns an appropriate tsubst'd friend declaration. */
8539
8540 static tree
8541 tsubst_friend_function (tree decl, tree args)
8542 {
8543 tree new_friend;
8544
8545 if (TREE_CODE (decl) == FUNCTION_DECL
8546 && DECL_TEMPLATE_INSTANTIATION (decl)
8547 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8548 /* This was a friend declared with an explicit template
8549 argument list, e.g.:
8550
8551 friend void f<>(T);
8552
8553 to indicate that f was a template instantiation, not a new
8554 function declaration. Now, we have to figure out what
8555 instantiation of what template. */
8556 {
8557 tree template_id, arglist, fns;
8558 tree new_args;
8559 tree tmpl;
8560 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8561
8562 /* Friend functions are looked up in the containing namespace scope.
8563 We must enter that scope, to avoid finding member functions of the
8564 current class with same name. */
8565 push_nested_namespace (ns);
8566 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8567 tf_warning_or_error, NULL_TREE,
8568 /*integral_constant_expression_p=*/false);
8569 pop_nested_namespace (ns);
8570 arglist = tsubst (DECL_TI_ARGS (decl), args,
8571 tf_warning_or_error, NULL_TREE);
8572 template_id = lookup_template_function (fns, arglist);
8573
8574 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8575 tmpl = determine_specialization (template_id, new_friend,
8576 &new_args,
8577 /*need_member_template=*/0,
8578 TREE_VEC_LENGTH (args),
8579 tsk_none);
8580 return instantiate_template (tmpl, new_args, tf_error);
8581 }
8582
8583 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8584
8585 /* The NEW_FRIEND will look like an instantiation, to the
8586 compiler, but is not an instantiation from the point of view of
8587 the language. For example, we might have had:
8588
8589 template <class T> struct S {
8590 template <class U> friend void f(T, U);
8591 };
8592
8593 Then, in S<int>, template <class U> void f(int, U) is not an
8594 instantiation of anything. */
8595 if (new_friend == error_mark_node)
8596 return error_mark_node;
8597
8598 DECL_USE_TEMPLATE (new_friend) = 0;
8599 if (TREE_CODE (decl) == TEMPLATE_DECL)
8600 {
8601 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8602 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8603 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8604 }
8605
8606 /* The mangled name for the NEW_FRIEND is incorrect. The function
8607 is not a template instantiation and should not be mangled like
8608 one. Therefore, we forget the mangling here; we'll recompute it
8609 later if we need it. */
8610 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8611 {
8612 SET_DECL_RTL (new_friend, NULL);
8613 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8614 }
8615
8616 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8617 {
8618 tree old_decl;
8619 tree new_friend_template_info;
8620 tree new_friend_result_template_info;
8621 tree ns;
8622 int new_friend_is_defn;
8623
8624 /* We must save some information from NEW_FRIEND before calling
8625 duplicate decls since that function will free NEW_FRIEND if
8626 possible. */
8627 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8628 new_friend_is_defn =
8629 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8630 (template_for_substitution (new_friend)))
8631 != NULL_TREE);
8632 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8633 {
8634 /* This declaration is a `primary' template. */
8635 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8636
8637 new_friend_result_template_info
8638 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8639 }
8640 else
8641 new_friend_result_template_info = NULL_TREE;
8642
8643 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8644 if (new_friend_is_defn)
8645 DECL_INITIAL (new_friend) = error_mark_node;
8646
8647 /* Inside pushdecl_namespace_level, we will push into the
8648 current namespace. However, the friend function should go
8649 into the namespace of the template. */
8650 ns = decl_namespace_context (new_friend);
8651 push_nested_namespace (ns);
8652 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8653 pop_nested_namespace (ns);
8654
8655 if (old_decl == error_mark_node)
8656 return error_mark_node;
8657
8658 if (old_decl != new_friend)
8659 {
8660 /* This new friend declaration matched an existing
8661 declaration. For example, given:
8662
8663 template <class T> void f(T);
8664 template <class U> class C {
8665 template <class T> friend void f(T) {}
8666 };
8667
8668 the friend declaration actually provides the definition
8669 of `f', once C has been instantiated for some type. So,
8670 old_decl will be the out-of-class template declaration,
8671 while new_friend is the in-class definition.
8672
8673 But, if `f' was called before this point, the
8674 instantiation of `f' will have DECL_TI_ARGS corresponding
8675 to `T' but not to `U', references to which might appear
8676 in the definition of `f'. Previously, the most general
8677 template for an instantiation of `f' was the out-of-class
8678 version; now it is the in-class version. Therefore, we
8679 run through all specialization of `f', adding to their
8680 DECL_TI_ARGS appropriately. In particular, they need a
8681 new set of outer arguments, corresponding to the
8682 arguments for this class instantiation.
8683
8684 The same situation can arise with something like this:
8685
8686 friend void f(int);
8687 template <class T> class C {
8688 friend void f(T) {}
8689 };
8690
8691 when `C<int>' is instantiated. Now, `f(int)' is defined
8692 in the class. */
8693
8694 if (!new_friend_is_defn)
8695 /* On the other hand, if the in-class declaration does
8696 *not* provide a definition, then we don't want to alter
8697 existing definitions. We can just leave everything
8698 alone. */
8699 ;
8700 else
8701 {
8702 tree new_template = TI_TEMPLATE (new_friend_template_info);
8703 tree new_args = TI_ARGS (new_friend_template_info);
8704
8705 /* Overwrite whatever template info was there before, if
8706 any, with the new template information pertaining to
8707 the declaration. */
8708 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8709
8710 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8711 {
8712 /* We should have called reregister_specialization in
8713 duplicate_decls. */
8714 gcc_assert (retrieve_specialization (new_template,
8715 new_args, 0)
8716 == old_decl);
8717
8718 /* Instantiate it if the global has already been used. */
8719 if (DECL_ODR_USED (old_decl))
8720 instantiate_decl (old_decl, /*defer_ok=*/true,
8721 /*expl_inst_class_mem_p=*/false);
8722 }
8723 else
8724 {
8725 tree t;
8726
8727 /* Indicate that the old function template is a partial
8728 instantiation. */
8729 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8730 = new_friend_result_template_info;
8731
8732 gcc_assert (new_template
8733 == most_general_template (new_template));
8734 gcc_assert (new_template != old_decl);
8735
8736 /* Reassign any specializations already in the hash table
8737 to the new more general template, and add the
8738 additional template args. */
8739 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8740 t != NULL_TREE;
8741 t = TREE_CHAIN (t))
8742 {
8743 tree spec = TREE_VALUE (t);
8744 spec_entry elt;
8745
8746 elt.tmpl = old_decl;
8747 elt.args = DECL_TI_ARGS (spec);
8748 elt.spec = NULL_TREE;
8749
8750 decl_specializations->remove_elt (&elt);
8751
8752 DECL_TI_ARGS (spec)
8753 = add_outermost_template_args (new_args,
8754 DECL_TI_ARGS (spec));
8755
8756 register_specialization
8757 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8758
8759 }
8760 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8761 }
8762 }
8763
8764 /* The information from NEW_FRIEND has been merged into OLD_DECL
8765 by duplicate_decls. */
8766 new_friend = old_decl;
8767 }
8768 }
8769 else
8770 {
8771 tree context = DECL_CONTEXT (new_friend);
8772 bool dependent_p;
8773
8774 /* In the code
8775 template <class T> class C {
8776 template <class U> friend void C1<U>::f (); // case 1
8777 friend void C2<T>::f (); // case 2
8778 };
8779 we only need to make sure CONTEXT is a complete type for
8780 case 2. To distinguish between the two cases, we note that
8781 CONTEXT of case 1 remains dependent type after tsubst while
8782 this isn't true for case 2. */
8783 ++processing_template_decl;
8784 dependent_p = dependent_type_p (context);
8785 --processing_template_decl;
8786
8787 if (!dependent_p
8788 && !complete_type_or_else (context, NULL_TREE))
8789 return error_mark_node;
8790
8791 if (COMPLETE_TYPE_P (context))
8792 {
8793 tree fn = new_friend;
8794 /* do_friend adds the TEMPLATE_DECL for any member friend
8795 template even if it isn't a member template, i.e.
8796 template <class T> friend A<T>::f();
8797 Look through it in that case. */
8798 if (TREE_CODE (fn) == TEMPLATE_DECL
8799 && !PRIMARY_TEMPLATE_P (fn))
8800 fn = DECL_TEMPLATE_RESULT (fn);
8801 /* Check to see that the declaration is really present, and,
8802 possibly obtain an improved declaration. */
8803 fn = check_classfn (context, fn, NULL_TREE);
8804
8805 if (fn)
8806 new_friend = fn;
8807 }
8808 }
8809
8810 return new_friend;
8811 }
8812
8813 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8814 template arguments, as for tsubst.
8815
8816 Returns an appropriate tsubst'd friend type or error_mark_node on
8817 failure. */
8818
8819 static tree
8820 tsubst_friend_class (tree friend_tmpl, tree args)
8821 {
8822 tree friend_type;
8823 tree tmpl;
8824 tree context;
8825
8826 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8827 {
8828 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8829 return TREE_TYPE (t);
8830 }
8831
8832 context = CP_DECL_CONTEXT (friend_tmpl);
8833
8834 if (context != global_namespace)
8835 {
8836 if (TREE_CODE (context) == NAMESPACE_DECL)
8837 push_nested_namespace (context);
8838 else
8839 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8840 }
8841
8842 /* Look for a class template declaration. We look for hidden names
8843 because two friend declarations of the same template are the
8844 same. For example, in:
8845
8846 struct A {
8847 template <typename> friend class F;
8848 };
8849 template <typename> struct B {
8850 template <typename> friend class F;
8851 };
8852
8853 both F templates are the same. */
8854 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8855 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8856
8857 /* But, if we don't find one, it might be because we're in a
8858 situation like this:
8859
8860 template <class T>
8861 struct S {
8862 template <class U>
8863 friend struct S;
8864 };
8865
8866 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8867 for `S<int>', not the TEMPLATE_DECL. */
8868 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8869 {
8870 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8871 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8872 }
8873
8874 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8875 {
8876 /* The friend template has already been declared. Just
8877 check to see that the declarations match, and install any new
8878 default parameters. We must tsubst the default parameters,
8879 of course. We only need the innermost template parameters
8880 because that is all that redeclare_class_template will look
8881 at. */
8882 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8883 > TMPL_ARGS_DEPTH (args))
8884 {
8885 tree parms;
8886 location_t saved_input_location;
8887 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8888 args, tf_warning_or_error);
8889
8890 saved_input_location = input_location;
8891 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8892 redeclare_class_template (TREE_TYPE (tmpl), parms);
8893 input_location = saved_input_location;
8894
8895 }
8896
8897 friend_type = TREE_TYPE (tmpl);
8898 }
8899 else
8900 {
8901 /* The friend template has not already been declared. In this
8902 case, the instantiation of the template class will cause the
8903 injection of this template into the global scope. */
8904 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8905 if (tmpl == error_mark_node)
8906 return error_mark_node;
8907
8908 /* The new TMPL is not an instantiation of anything, so we
8909 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8910 the new type because that is supposed to be the corresponding
8911 template decl, i.e., TMPL. */
8912 DECL_USE_TEMPLATE (tmpl) = 0;
8913 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8914 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8915 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8916 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8917
8918 /* Inject this template into the global scope. */
8919 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8920 }
8921
8922 if (context != global_namespace)
8923 {
8924 if (TREE_CODE (context) == NAMESPACE_DECL)
8925 pop_nested_namespace (context);
8926 else
8927 pop_nested_class ();
8928 }
8929
8930 return friend_type;
8931 }
8932
8933 /* Returns zero if TYPE cannot be completed later due to circularity.
8934 Otherwise returns one. */
8935
8936 static int
8937 can_complete_type_without_circularity (tree type)
8938 {
8939 if (type == NULL_TREE || type == error_mark_node)
8940 return 0;
8941 else if (COMPLETE_TYPE_P (type))
8942 return 1;
8943 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8944 return can_complete_type_without_circularity (TREE_TYPE (type));
8945 else if (CLASS_TYPE_P (type)
8946 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8947 return 0;
8948 else
8949 return 1;
8950 }
8951
8952 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8953
8954 /* Apply any attributes which had to be deferred until instantiation
8955 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8956 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8957
8958 static void
8959 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8960 tree args, tsubst_flags_t complain, tree in_decl)
8961 {
8962 tree last_dep = NULL_TREE;
8963 tree t;
8964 tree *p;
8965
8966 for (t = attributes; t; t = TREE_CHAIN (t))
8967 if (ATTR_IS_DEPENDENT (t))
8968 {
8969 last_dep = t;
8970 attributes = copy_list (attributes);
8971 break;
8972 }
8973
8974 if (DECL_P (*decl_p))
8975 {
8976 if (TREE_TYPE (*decl_p) == error_mark_node)
8977 return;
8978 p = &DECL_ATTRIBUTES (*decl_p);
8979 }
8980 else
8981 p = &TYPE_ATTRIBUTES (*decl_p);
8982
8983 if (last_dep)
8984 {
8985 tree late_attrs = NULL_TREE;
8986 tree *q = &late_attrs;
8987
8988 for (*p = attributes; *p; )
8989 {
8990 t = *p;
8991 if (ATTR_IS_DEPENDENT (t))
8992 {
8993 *p = TREE_CHAIN (t);
8994 TREE_CHAIN (t) = NULL_TREE;
8995 if ((flag_openmp || flag_cilkplus)
8996 && is_attribute_p ("omp declare simd",
8997 get_attribute_name (t))
8998 && TREE_VALUE (t))
8999 {
9000 tree clauses = TREE_VALUE (TREE_VALUE (t));
9001 clauses = tsubst_omp_clauses (clauses, true, args,
9002 complain, in_decl);
9003 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9004 clauses = finish_omp_clauses (clauses);
9005 tree parms = DECL_ARGUMENTS (*decl_p);
9006 clauses
9007 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9008 if (clauses)
9009 TREE_VALUE (TREE_VALUE (t)) = clauses;
9010 else
9011 TREE_VALUE (t) = NULL_TREE;
9012 }
9013 /* If the first attribute argument is an identifier, don't
9014 pass it through tsubst. Attributes like mode, format,
9015 cleanup and several target specific attributes expect it
9016 unmodified. */
9017 else if (attribute_takes_identifier_p (get_attribute_name (t))
9018 && TREE_VALUE (t))
9019 {
9020 tree chain
9021 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9022 in_decl,
9023 /*integral_constant_expression_p=*/false);
9024 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9025 TREE_VALUE (t)
9026 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9027 chain);
9028 }
9029 else if (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))
9030 {
9031 /* An attribute pack expansion. */
9032 tree purp = TREE_PURPOSE (t);
9033 tree pack = (tsubst_pack_expansion
9034 (TREE_VALUE (t), args, complain, in_decl));
9035 int len = TREE_VEC_LENGTH (pack);
9036 for (int i = 0; i < len; ++i)
9037 {
9038 tree elt = TREE_VEC_ELT (pack, i);
9039 *q = build_tree_list (purp, elt);
9040 q = &TREE_CHAIN (*q);
9041 }
9042 continue;
9043 }
9044 else
9045 TREE_VALUE (t)
9046 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9047 /*integral_constant_expression_p=*/false);
9048 *q = t;
9049 q = &TREE_CHAIN (t);
9050 }
9051 else
9052 p = &TREE_CHAIN (t);
9053 }
9054
9055 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9056 }
9057 }
9058
9059 /* Perform (or defer) access check for typedefs that were referenced
9060 from within the template TMPL code.
9061 This is a subroutine of instantiate_decl and instantiate_class_template.
9062 TMPL is the template to consider and TARGS is the list of arguments of
9063 that template. */
9064
9065 static void
9066 perform_typedefs_access_check (tree tmpl, tree targs)
9067 {
9068 location_t saved_location;
9069 unsigned i;
9070 qualified_typedef_usage_t *iter;
9071
9072 if (!tmpl
9073 || (!CLASS_TYPE_P (tmpl)
9074 && TREE_CODE (tmpl) != FUNCTION_DECL))
9075 return;
9076
9077 saved_location = input_location;
9078 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9079 {
9080 tree type_decl = iter->typedef_decl;
9081 tree type_scope = iter->context;
9082
9083 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9084 continue;
9085
9086 if (uses_template_parms (type_decl))
9087 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9088 if (uses_template_parms (type_scope))
9089 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9090
9091 /* Make access check error messages point to the location
9092 of the use of the typedef. */
9093 input_location = iter->locus;
9094 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9095 type_decl, type_decl,
9096 tf_warning_or_error);
9097 }
9098 input_location = saved_location;
9099 }
9100
9101 static tree
9102 instantiate_class_template_1 (tree type)
9103 {
9104 tree templ, args, pattern, t, member;
9105 tree typedecl;
9106 tree pbinfo;
9107 tree base_list;
9108 unsigned int saved_maximum_field_alignment;
9109 tree fn_context;
9110
9111 if (type == error_mark_node)
9112 return error_mark_node;
9113
9114 if (COMPLETE_OR_OPEN_TYPE_P (type)
9115 || uses_template_parms (type))
9116 return type;
9117
9118 /* Figure out which template is being instantiated. */
9119 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9120 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9121
9122 /* Determine what specialization of the original template to
9123 instantiate. */
9124 t = most_specialized_partial_spec (type, tf_warning_or_error);
9125 if (t == error_mark_node)
9126 {
9127 TYPE_BEING_DEFINED (type) = 1;
9128 return error_mark_node;
9129 }
9130 else if (t)
9131 {
9132 /* This TYPE is actually an instantiation of a partial
9133 specialization. We replace the innermost set of ARGS with
9134 the arguments appropriate for substitution. For example,
9135 given:
9136
9137 template <class T> struct S {};
9138 template <class T> struct S<T*> {};
9139
9140 and supposing that we are instantiating S<int*>, ARGS will
9141 presently be {int*} -- but we need {int}. */
9142 pattern = TREE_TYPE (t);
9143 args = TREE_PURPOSE (t);
9144 }
9145 else
9146 {
9147 pattern = TREE_TYPE (templ);
9148 args = CLASSTYPE_TI_ARGS (type);
9149 }
9150
9151 /* If the template we're instantiating is incomplete, then clearly
9152 there's nothing we can do. */
9153 if (!COMPLETE_TYPE_P (pattern))
9154 return type;
9155
9156 /* If we've recursively instantiated too many templates, stop. */
9157 if (! push_tinst_level (type))
9158 return type;
9159
9160 /* Now we're really doing the instantiation. Mark the type as in
9161 the process of being defined. */
9162 TYPE_BEING_DEFINED (type) = 1;
9163
9164 /* We may be in the middle of deferred access check. Disable
9165 it now. */
9166 push_deferring_access_checks (dk_no_deferred);
9167
9168 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9169 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9170 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9171 fn_context = error_mark_node;
9172 if (!fn_context)
9173 push_to_top_level ();
9174 /* Use #pragma pack from the template context. */
9175 saved_maximum_field_alignment = maximum_field_alignment;
9176 maximum_field_alignment = TYPE_PRECISION (pattern);
9177
9178 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9179
9180 /* Set the input location to the most specialized template definition.
9181 This is needed if tsubsting causes an error. */
9182 typedecl = TYPE_MAIN_DECL (pattern);
9183 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9184 DECL_SOURCE_LOCATION (typedecl);
9185
9186 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9187 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9188 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9189 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9190 if (ANON_AGGR_TYPE_P (pattern))
9191 SET_ANON_AGGR_TYPE_P (type);
9192 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9193 {
9194 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9195 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9196 /* Adjust visibility for template arguments. */
9197 determine_visibility (TYPE_MAIN_DECL (type));
9198 }
9199 if (CLASS_TYPE_P (type))
9200 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9201
9202 pbinfo = TYPE_BINFO (pattern);
9203
9204 /* We should never instantiate a nested class before its enclosing
9205 class; we need to look up the nested class by name before we can
9206 instantiate it, and that lookup should instantiate the enclosing
9207 class. */
9208 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9209 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9210
9211 base_list = NULL_TREE;
9212 if (BINFO_N_BASE_BINFOS (pbinfo))
9213 {
9214 tree pbase_binfo;
9215 tree pushed_scope;
9216 int i;
9217
9218 /* We must enter the scope containing the type, as that is where
9219 the accessibility of types named in dependent bases are
9220 looked up from. */
9221 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9222
9223 /* Substitute into each of the bases to determine the actual
9224 basetypes. */
9225 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9226 {
9227 tree base;
9228 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9229 tree expanded_bases = NULL_TREE;
9230 int idx, len = 1;
9231
9232 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9233 {
9234 expanded_bases =
9235 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9236 args, tf_error, NULL_TREE);
9237 if (expanded_bases == error_mark_node)
9238 continue;
9239
9240 len = TREE_VEC_LENGTH (expanded_bases);
9241 }
9242
9243 for (idx = 0; idx < len; idx++)
9244 {
9245 if (expanded_bases)
9246 /* Extract the already-expanded base class. */
9247 base = TREE_VEC_ELT (expanded_bases, idx);
9248 else
9249 /* Substitute to figure out the base class. */
9250 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9251 NULL_TREE);
9252
9253 if (base == error_mark_node)
9254 continue;
9255
9256 base_list = tree_cons (access, base, base_list);
9257 if (BINFO_VIRTUAL_P (pbase_binfo))
9258 TREE_TYPE (base_list) = integer_type_node;
9259 }
9260 }
9261
9262 /* The list is now in reverse order; correct that. */
9263 base_list = nreverse (base_list);
9264
9265 if (pushed_scope)
9266 pop_scope (pushed_scope);
9267 }
9268 /* Now call xref_basetypes to set up all the base-class
9269 information. */
9270 xref_basetypes (type, base_list);
9271
9272 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9273 (int) ATTR_FLAG_TYPE_IN_PLACE,
9274 args, tf_error, NULL_TREE);
9275 fixup_attribute_variants (type);
9276
9277 /* Now that our base classes are set up, enter the scope of the
9278 class, so that name lookups into base classes, etc. will work
9279 correctly. This is precisely analogous to what we do in
9280 begin_class_definition when defining an ordinary non-template
9281 class, except we also need to push the enclosing classes. */
9282 push_nested_class (type);
9283
9284 /* Now members are processed in the order of declaration. */
9285 for (member = CLASSTYPE_DECL_LIST (pattern);
9286 member; member = TREE_CHAIN (member))
9287 {
9288 tree t = TREE_VALUE (member);
9289
9290 if (TREE_PURPOSE (member))
9291 {
9292 if (TYPE_P (t))
9293 {
9294 /* Build new CLASSTYPE_NESTED_UTDS. */
9295
9296 tree newtag;
9297 bool class_template_p;
9298
9299 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9300 && TYPE_LANG_SPECIFIC (t)
9301 && CLASSTYPE_IS_TEMPLATE (t));
9302 /* If the member is a class template, then -- even after
9303 substitution -- there may be dependent types in the
9304 template argument list for the class. We increment
9305 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9306 that function will assume that no types are dependent
9307 when outside of a template. */
9308 if (class_template_p)
9309 ++processing_template_decl;
9310 newtag = tsubst (t, args, tf_error, NULL_TREE);
9311 if (class_template_p)
9312 --processing_template_decl;
9313 if (newtag == error_mark_node)
9314 continue;
9315
9316 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9317 {
9318 tree name = TYPE_IDENTIFIER (t);
9319
9320 if (class_template_p)
9321 /* Unfortunately, lookup_template_class sets
9322 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9323 instantiation (i.e., for the type of a member
9324 template class nested within a template class.)
9325 This behavior is required for
9326 maybe_process_partial_specialization to work
9327 correctly, but is not accurate in this case;
9328 the TAG is not an instantiation of anything.
9329 (The corresponding TEMPLATE_DECL is an
9330 instantiation, but the TYPE is not.) */
9331 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9332
9333 /* Now, we call pushtag to put this NEWTAG into the scope of
9334 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9335 pushtag calling push_template_decl. We don't have to do
9336 this for enums because it will already have been done in
9337 tsubst_enum. */
9338 if (name)
9339 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9340 pushtag (name, newtag, /*tag_scope=*/ts_current);
9341 }
9342 }
9343 else if (DECL_DECLARES_FUNCTION_P (t))
9344 {
9345 /* Build new TYPE_METHODS. */
9346 tree r;
9347
9348 if (TREE_CODE (t) == TEMPLATE_DECL)
9349 ++processing_template_decl;
9350 r = tsubst (t, args, tf_error, NULL_TREE);
9351 if (TREE_CODE (t) == TEMPLATE_DECL)
9352 --processing_template_decl;
9353 set_current_access_from_decl (r);
9354 finish_member_declaration (r);
9355 /* Instantiate members marked with attribute used. */
9356 if (r != error_mark_node && DECL_PRESERVE_P (r))
9357 mark_used (r);
9358 if (TREE_CODE (r) == FUNCTION_DECL
9359 && DECL_OMP_DECLARE_REDUCTION_P (r))
9360 cp_check_omp_declare_reduction (r);
9361 }
9362 else if (DECL_CLASS_TEMPLATE_P (t)
9363 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9364 /* A closure type for a lambda in a default argument for a
9365 member template. Ignore it; it will be instantiated with
9366 the default argument. */;
9367 else
9368 {
9369 /* Build new TYPE_FIELDS. */
9370 if (TREE_CODE (t) == STATIC_ASSERT)
9371 {
9372 tree condition;
9373
9374 ++c_inhibit_evaluation_warnings;
9375 condition =
9376 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9377 tf_warning_or_error, NULL_TREE,
9378 /*integral_constant_expression_p=*/true);
9379 --c_inhibit_evaluation_warnings;
9380
9381 finish_static_assert (condition,
9382 STATIC_ASSERT_MESSAGE (t),
9383 STATIC_ASSERT_SOURCE_LOCATION (t),
9384 /*member_p=*/true);
9385 }
9386 else if (TREE_CODE (t) != CONST_DECL)
9387 {
9388 tree r;
9389 tree vec = NULL_TREE;
9390 int len = 1;
9391
9392 /* The file and line for this declaration, to
9393 assist in error message reporting. Since we
9394 called push_tinst_level above, we don't need to
9395 restore these. */
9396 input_location = DECL_SOURCE_LOCATION (t);
9397
9398 if (TREE_CODE (t) == TEMPLATE_DECL)
9399 ++processing_template_decl;
9400 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9401 if (TREE_CODE (t) == TEMPLATE_DECL)
9402 --processing_template_decl;
9403
9404 if (TREE_CODE (r) == TREE_VEC)
9405 {
9406 /* A capture pack became multiple fields. */
9407 vec = r;
9408 len = TREE_VEC_LENGTH (vec);
9409 }
9410
9411 for (int i = 0; i < len; ++i)
9412 {
9413 if (vec)
9414 r = TREE_VEC_ELT (vec, i);
9415 if (VAR_P (r))
9416 {
9417 /* In [temp.inst]:
9418
9419 [t]he initialization (and any associated
9420 side-effects) of a static data member does
9421 not occur unless the static data member is
9422 itself used in a way that requires the
9423 definition of the static data member to
9424 exist.
9425
9426 Therefore, we do not substitute into the
9427 initialized for the static data member here. */
9428 finish_static_data_member_decl
9429 (r,
9430 /*init=*/NULL_TREE,
9431 /*init_const_expr_p=*/false,
9432 /*asmspec_tree=*/NULL_TREE,
9433 /*flags=*/0);
9434 /* Instantiate members marked with attribute used. */
9435 if (r != error_mark_node && DECL_PRESERVE_P (r))
9436 mark_used (r);
9437 }
9438 else if (TREE_CODE (r) == FIELD_DECL)
9439 {
9440 /* Determine whether R has a valid type and can be
9441 completed later. If R is invalid, then its type
9442 is replaced by error_mark_node. */
9443 tree rtype = TREE_TYPE (r);
9444 if (can_complete_type_without_circularity (rtype))
9445 complete_type (rtype);
9446
9447 if (!COMPLETE_TYPE_P (rtype))
9448 {
9449 cxx_incomplete_type_error (r, rtype);
9450 TREE_TYPE (r) = error_mark_node;
9451 }
9452 }
9453
9454 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9455 such a thing will already have been added to the field
9456 list by tsubst_enum in finish_member_declaration in the
9457 CLASSTYPE_NESTED_UTDS case above. */
9458 if (!(TREE_CODE (r) == TYPE_DECL
9459 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9460 && DECL_ARTIFICIAL (r)))
9461 {
9462 set_current_access_from_decl (r);
9463 finish_member_declaration (r);
9464 }
9465 }
9466 }
9467 }
9468 }
9469 else
9470 {
9471 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9472 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9473 {
9474 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9475
9476 tree friend_type = t;
9477 bool adjust_processing_template_decl = false;
9478
9479 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9480 {
9481 /* template <class T> friend class C; */
9482 friend_type = tsubst_friend_class (friend_type, args);
9483 adjust_processing_template_decl = true;
9484 }
9485 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9486 {
9487 /* template <class T> friend class C::D; */
9488 friend_type = tsubst (friend_type, args,
9489 tf_warning_or_error, NULL_TREE);
9490 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9491 friend_type = TREE_TYPE (friend_type);
9492 adjust_processing_template_decl = true;
9493 }
9494 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9495 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9496 {
9497 /* This could be either
9498
9499 friend class T::C;
9500
9501 when dependent_type_p is false or
9502
9503 template <class U> friend class T::C;
9504
9505 otherwise. */
9506 friend_type = tsubst (friend_type, args,
9507 tf_warning_or_error, NULL_TREE);
9508 /* Bump processing_template_decl for correct
9509 dependent_type_p calculation. */
9510 ++processing_template_decl;
9511 if (dependent_type_p (friend_type))
9512 adjust_processing_template_decl = true;
9513 --processing_template_decl;
9514 }
9515 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9516 && hidden_name_p (TYPE_NAME (friend_type)))
9517 {
9518 /* friend class C;
9519
9520 where C hasn't been declared yet. Let's lookup name
9521 from namespace scope directly, bypassing any name that
9522 come from dependent base class. */
9523 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9524
9525 /* The call to xref_tag_from_type does injection for friend
9526 classes. */
9527 push_nested_namespace (ns);
9528 friend_type =
9529 xref_tag_from_type (friend_type, NULL_TREE,
9530 /*tag_scope=*/ts_current);
9531 pop_nested_namespace (ns);
9532 }
9533 else if (uses_template_parms (friend_type))
9534 /* friend class C<T>; */
9535 friend_type = tsubst (friend_type, args,
9536 tf_warning_or_error, NULL_TREE);
9537 /* Otherwise it's
9538
9539 friend class C;
9540
9541 where C is already declared or
9542
9543 friend class C<int>;
9544
9545 We don't have to do anything in these cases. */
9546
9547 if (adjust_processing_template_decl)
9548 /* Trick make_friend_class into realizing that the friend
9549 we're adding is a template, not an ordinary class. It's
9550 important that we use make_friend_class since it will
9551 perform some error-checking and output cross-reference
9552 information. */
9553 ++processing_template_decl;
9554
9555 if (friend_type != error_mark_node)
9556 make_friend_class (type, friend_type, /*complain=*/false);
9557
9558 if (adjust_processing_template_decl)
9559 --processing_template_decl;
9560 }
9561 else
9562 {
9563 /* Build new DECL_FRIENDLIST. */
9564 tree r;
9565
9566 /* The file and line for this declaration, to
9567 assist in error message reporting. Since we
9568 called push_tinst_level above, we don't need to
9569 restore these. */
9570 input_location = DECL_SOURCE_LOCATION (t);
9571
9572 if (TREE_CODE (t) == TEMPLATE_DECL)
9573 {
9574 ++processing_template_decl;
9575 push_deferring_access_checks (dk_no_check);
9576 }
9577
9578 r = tsubst_friend_function (t, args);
9579 add_friend (type, r, /*complain=*/false);
9580 if (TREE_CODE (t) == TEMPLATE_DECL)
9581 {
9582 pop_deferring_access_checks ();
9583 --processing_template_decl;
9584 }
9585 }
9586 }
9587 }
9588
9589 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9590 {
9591 tree decl = lambda_function (type);
9592 if (decl)
9593 {
9594 if (!DECL_TEMPLATE_INFO (decl)
9595 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9596 instantiate_decl (decl, false, false);
9597
9598 /* We need to instantiate the capture list from the template
9599 after we've instantiated the closure members, but before we
9600 consider adding the conversion op. Also keep any captures
9601 that may have been added during instantiation of the op(). */
9602 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9603 tree tmpl_cap
9604 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9605 args, tf_warning_or_error, NULL_TREE,
9606 false, false);
9607
9608 LAMBDA_EXPR_CAPTURE_LIST (expr)
9609 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9610
9611 maybe_add_lambda_conv_op (type);
9612 }
9613 else
9614 gcc_assert (errorcount);
9615 }
9616
9617 /* Set the file and line number information to whatever is given for
9618 the class itself. This puts error messages involving generated
9619 implicit functions at a predictable point, and the same point
9620 that would be used for non-template classes. */
9621 input_location = DECL_SOURCE_LOCATION (typedecl);
9622
9623 unreverse_member_declarations (type);
9624 finish_struct_1 (type);
9625 TYPE_BEING_DEFINED (type) = 0;
9626
9627 /* We don't instantiate default arguments for member functions. 14.7.1:
9628
9629 The implicit instantiation of a class template specialization causes
9630 the implicit instantiation of the declarations, but not of the
9631 definitions or default arguments, of the class member functions,
9632 member classes, static data members and member templates.... */
9633
9634 /* Some typedefs referenced from within the template code need to be access
9635 checked at template instantiation time, i.e now. These types were
9636 added to the template at parsing time. Let's get those and perform
9637 the access checks then. */
9638 perform_typedefs_access_check (pattern, args);
9639 perform_deferred_access_checks (tf_warning_or_error);
9640 pop_nested_class ();
9641 maximum_field_alignment = saved_maximum_field_alignment;
9642 if (!fn_context)
9643 pop_from_top_level ();
9644 pop_deferring_access_checks ();
9645 pop_tinst_level ();
9646
9647 /* The vtable for a template class can be emitted in any translation
9648 unit in which the class is instantiated. When there is no key
9649 method, however, finish_struct_1 will already have added TYPE to
9650 the keyed_classes list. */
9651 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9652 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9653
9654 return type;
9655 }
9656
9657 /* Wrapper for instantiate_class_template_1. */
9658
9659 tree
9660 instantiate_class_template (tree type)
9661 {
9662 tree ret;
9663 timevar_push (TV_TEMPLATE_INST);
9664 ret = instantiate_class_template_1 (type);
9665 timevar_pop (TV_TEMPLATE_INST);
9666 return ret;
9667 }
9668
9669 static tree
9670 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9671 {
9672 tree r;
9673
9674 if (!t)
9675 r = t;
9676 else if (TYPE_P (t))
9677 r = tsubst (t, args, complain, in_decl);
9678 else
9679 {
9680 if (!(complain & tf_warning))
9681 ++c_inhibit_evaluation_warnings;
9682 r = tsubst_expr (t, args, complain, in_decl,
9683 /*integral_constant_expression_p=*/true);
9684 if (!(complain & tf_warning))
9685 --c_inhibit_evaluation_warnings;
9686 }
9687 return r;
9688 }
9689
9690 /* Given a function parameter pack TMPL_PARM and some function parameters
9691 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9692 and set *SPEC_P to point at the next point in the list. */
9693
9694 static tree
9695 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9696 {
9697 /* Collect all of the extra "packed" parameters into an
9698 argument pack. */
9699 tree parmvec;
9700 tree parmtypevec;
9701 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9702 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9703 tree spec_parm = *spec_p;
9704 int i, len;
9705
9706 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9707 if (tmpl_parm
9708 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9709 break;
9710
9711 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9712 parmvec = make_tree_vec (len);
9713 parmtypevec = make_tree_vec (len);
9714 spec_parm = *spec_p;
9715 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9716 {
9717 TREE_VEC_ELT (parmvec, i) = spec_parm;
9718 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9719 }
9720
9721 /* Build the argument packs. */
9722 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9723 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9724 TREE_TYPE (argpack) = argtypepack;
9725 *spec_p = spec_parm;
9726
9727 return argpack;
9728 }
9729
9730 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9731 NONTYPE_ARGUMENT_PACK. */
9732
9733 static tree
9734 make_fnparm_pack (tree spec_parm)
9735 {
9736 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9737 }
9738
9739 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9740 pack expansion. */
9741
9742 static bool
9743 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9744 {
9745 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9746 if (i >= TREE_VEC_LENGTH (vec))
9747 return false;
9748 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9749 }
9750
9751
9752 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9753
9754 static tree
9755 make_argument_pack_select (tree arg_pack, unsigned index)
9756 {
9757 tree aps = make_node (ARGUMENT_PACK_SELECT);
9758
9759 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9760 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9761
9762 return aps;
9763 }
9764
9765 /* This is a subroutine of tsubst_pack_expansion.
9766
9767 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9768 mechanism to store the (non complete list of) arguments of the
9769 substitution and return a non substituted pack expansion, in order
9770 to wait for when we have enough arguments to really perform the
9771 substitution. */
9772
9773 static bool
9774 use_pack_expansion_extra_args_p (tree parm_packs,
9775 int arg_pack_len,
9776 bool has_empty_arg)
9777 {
9778 /* If one pack has an expansion and another pack has a normal
9779 argument or if one pack has an empty argument and an another
9780 one hasn't then tsubst_pack_expansion cannot perform the
9781 substitution and need to fall back on the
9782 PACK_EXPANSION_EXTRA mechanism. */
9783 if (parm_packs == NULL_TREE)
9784 return false;
9785 else if (has_empty_arg)
9786 return true;
9787
9788 bool has_expansion_arg = false;
9789 for (int i = 0 ; i < arg_pack_len; ++i)
9790 {
9791 bool has_non_expansion_arg = false;
9792 for (tree parm_pack = parm_packs;
9793 parm_pack;
9794 parm_pack = TREE_CHAIN (parm_pack))
9795 {
9796 tree arg = TREE_VALUE (parm_pack);
9797
9798 if (argument_pack_element_is_expansion_p (arg, i))
9799 has_expansion_arg = true;
9800 else
9801 has_non_expansion_arg = true;
9802 }
9803
9804 if (has_expansion_arg && has_non_expansion_arg)
9805 return true;
9806 }
9807 return false;
9808 }
9809
9810 /* [temp.variadic]/6 says that:
9811
9812 The instantiation of a pack expansion [...]
9813 produces a list E1,E2, ..., En, where N is the number of elements
9814 in the pack expansion parameters.
9815
9816 This subroutine of tsubst_pack_expansion produces one of these Ei.
9817
9818 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9819 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9820 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9821 INDEX is the index 'i' of the element Ei to produce. ARGS,
9822 COMPLAIN, and IN_DECL are the same parameters as for the
9823 tsubst_pack_expansion function.
9824
9825 The function returns the resulting Ei upon successful completion,
9826 or error_mark_node.
9827
9828 Note that this function possibly modifies the ARGS parameter, so
9829 it's the responsibility of the caller to restore it. */
9830
9831 static tree
9832 gen_elem_of_pack_expansion_instantiation (tree pattern,
9833 tree parm_packs,
9834 unsigned index,
9835 tree args /* This parm gets
9836 modified. */,
9837 tsubst_flags_t complain,
9838 tree in_decl)
9839 {
9840 tree t;
9841 bool ith_elem_is_expansion = false;
9842
9843 /* For each parameter pack, change the substitution of the parameter
9844 pack to the ith argument in its argument pack, then expand the
9845 pattern. */
9846 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9847 {
9848 tree parm = TREE_PURPOSE (pack);
9849 tree arg_pack = TREE_VALUE (pack);
9850 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9851
9852 ith_elem_is_expansion |=
9853 argument_pack_element_is_expansion_p (arg_pack, index);
9854
9855 /* Select the Ith argument from the pack. */
9856 if (TREE_CODE (parm) == PARM_DECL
9857 || TREE_CODE (parm) == FIELD_DECL)
9858 {
9859 if (index == 0)
9860 {
9861 aps = make_argument_pack_select (arg_pack, index);
9862 if (!mark_used (parm, complain) && !(complain & tf_error))
9863 return error_mark_node;
9864 register_local_specialization (aps, parm);
9865 }
9866 else
9867 aps = retrieve_local_specialization (parm);
9868 }
9869 else
9870 {
9871 int idx, level;
9872 template_parm_level_and_index (parm, &level, &idx);
9873
9874 if (index == 0)
9875 {
9876 aps = make_argument_pack_select (arg_pack, index);
9877 /* Update the corresponding argument. */
9878 TMPL_ARG (args, level, idx) = aps;
9879 }
9880 else
9881 /* Re-use the ARGUMENT_PACK_SELECT. */
9882 aps = TMPL_ARG (args, level, idx);
9883 }
9884 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9885 }
9886
9887 /* Substitute into the PATTERN with the (possibly altered)
9888 arguments. */
9889 if (pattern == in_decl)
9890 /* Expanding a fixed parameter pack from
9891 coerce_template_parameter_pack. */
9892 t = tsubst_decl (pattern, args, complain);
9893 else if (!TYPE_P (pattern))
9894 t = tsubst_expr (pattern, args, complain, in_decl,
9895 /*integral_constant_expression_p=*/false);
9896 else
9897 t = tsubst (pattern, args, complain, in_decl);
9898
9899 /* If the Ith argument pack element is a pack expansion, then
9900 the Ith element resulting from the substituting is going to
9901 be a pack expansion as well. */
9902 if (ith_elem_is_expansion)
9903 t = make_pack_expansion (t);
9904
9905 return t;
9906 }
9907
9908 /* Substitute ARGS into T, which is an pack expansion
9909 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9910 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9911 (if only a partial substitution could be performed) or
9912 ERROR_MARK_NODE if there was an error. */
9913 tree
9914 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9915 tree in_decl)
9916 {
9917 tree pattern;
9918 tree pack, packs = NULL_TREE;
9919 bool unsubstituted_packs = false;
9920 int i, len = -1;
9921 tree result;
9922 hash_map<tree, tree> *saved_local_specializations = NULL;
9923 bool need_local_specializations = false;
9924 int levels;
9925
9926 gcc_assert (PACK_EXPANSION_P (t));
9927 pattern = PACK_EXPANSION_PATTERN (t);
9928
9929 /* Add in any args remembered from an earlier partial instantiation. */
9930 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9931
9932 levels = TMPL_ARGS_DEPTH (args);
9933
9934 /* Determine the argument packs that will instantiate the parameter
9935 packs used in the expansion expression. While we're at it,
9936 compute the number of arguments to be expanded and make sure it
9937 is consistent. */
9938 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9939 pack = TREE_CHAIN (pack))
9940 {
9941 tree parm_pack = TREE_VALUE (pack);
9942 tree arg_pack = NULL_TREE;
9943 tree orig_arg = NULL_TREE;
9944 int level = 0;
9945
9946 if (TREE_CODE (parm_pack) == BASES)
9947 {
9948 if (BASES_DIRECT (parm_pack))
9949 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9950 args, complain, in_decl, false));
9951 else
9952 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9953 args, complain, in_decl, false));
9954 }
9955 if (TREE_CODE (parm_pack) == PARM_DECL)
9956 {
9957 if (PACK_EXPANSION_LOCAL_P (t))
9958 arg_pack = retrieve_local_specialization (parm_pack);
9959 else
9960 {
9961 /* We can't rely on local_specializations for a parameter
9962 name used later in a function declaration (such as in a
9963 late-specified return type). Even if it exists, it might
9964 have the wrong value for a recursive call. Just make a
9965 dummy decl, since it's only used for its type. */
9966 arg_pack = tsubst_decl (parm_pack, args, complain);
9967 if (arg_pack && DECL_PACK_P (arg_pack))
9968 /* Partial instantiation of the parm_pack, we can't build
9969 up an argument pack yet. */
9970 arg_pack = NULL_TREE;
9971 else
9972 arg_pack = make_fnparm_pack (arg_pack);
9973 need_local_specializations = true;
9974 }
9975 }
9976 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9977 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9978 else
9979 {
9980 int idx;
9981 template_parm_level_and_index (parm_pack, &level, &idx);
9982
9983 if (level <= levels)
9984 arg_pack = TMPL_ARG (args, level, idx);
9985 }
9986
9987 orig_arg = arg_pack;
9988 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9989 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9990
9991 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9992 /* This can only happen if we forget to expand an argument
9993 pack somewhere else. Just return an error, silently. */
9994 {
9995 result = make_tree_vec (1);
9996 TREE_VEC_ELT (result, 0) = error_mark_node;
9997 return result;
9998 }
9999
10000 if (arg_pack)
10001 {
10002 int my_len =
10003 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10004
10005 /* Don't bother trying to do a partial substitution with
10006 incomplete packs; we'll try again after deduction. */
10007 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10008 return t;
10009
10010 if (len < 0)
10011 len = my_len;
10012 else if (len != my_len)
10013 {
10014 if (!(complain & tf_error))
10015 /* Fail quietly. */;
10016 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10017 error ("mismatched argument pack lengths while expanding "
10018 "%<%T%>",
10019 pattern);
10020 else
10021 error ("mismatched argument pack lengths while expanding "
10022 "%<%E%>",
10023 pattern);
10024 return error_mark_node;
10025 }
10026
10027 /* Keep track of the parameter packs and their corresponding
10028 argument packs. */
10029 packs = tree_cons (parm_pack, arg_pack, packs);
10030 TREE_TYPE (packs) = orig_arg;
10031 }
10032 else
10033 {
10034 /* We can't substitute for this parameter pack. We use a flag as
10035 well as the missing_level counter because function parameter
10036 packs don't have a level. */
10037 unsubstituted_packs = true;
10038 }
10039 }
10040
10041 /* If the expansion is just T..., return the matching argument pack. */
10042 if (!unsubstituted_packs
10043 && TREE_PURPOSE (packs) == pattern)
10044 {
10045 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10046 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10047 || pack_expansion_args_count (args))
10048 return args;
10049 /* Otherwise use the normal path so we get convert_from_reference. */
10050 }
10051
10052 /* We cannot expand this expansion expression, because we don't have
10053 all of the argument packs we need. */
10054 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10055 {
10056 /* We got some full packs, but we can't substitute them in until we
10057 have values for all the packs. So remember these until then. */
10058
10059 t = make_pack_expansion (pattern);
10060 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10061 return t;
10062 }
10063 else if (unsubstituted_packs)
10064 {
10065 /* There were no real arguments, we're just replacing a parameter
10066 pack with another version of itself. Substitute into the
10067 pattern and return a PACK_EXPANSION_*. The caller will need to
10068 deal with that. */
10069 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10070 t = tsubst_expr (pattern, args, complain, in_decl,
10071 /*integral_constant_expression_p=*/false);
10072 else
10073 t = tsubst (pattern, args, complain, in_decl);
10074 t = make_pack_expansion (t);
10075 return t;
10076 }
10077
10078 gcc_assert (len >= 0);
10079
10080 if (need_local_specializations)
10081 {
10082 /* We're in a late-specified return type, so create our own local
10083 specializations map; the current map is either NULL or (in the
10084 case of recursive unification) might have bindings that we don't
10085 want to use or alter. */
10086 saved_local_specializations = local_specializations;
10087 local_specializations = new hash_map<tree, tree>;
10088 }
10089
10090 /* For each argument in each argument pack, substitute into the
10091 pattern. */
10092 result = make_tree_vec (len);
10093 for (i = 0; i < len; ++i)
10094 {
10095 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10096 i,
10097 args, complain,
10098 in_decl);
10099 TREE_VEC_ELT (result, i) = t;
10100 if (t == error_mark_node)
10101 {
10102 result = error_mark_node;
10103 break;
10104 }
10105 }
10106
10107 /* Update ARGS to restore the substitution from parameter packs to
10108 their argument packs. */
10109 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10110 {
10111 tree parm = TREE_PURPOSE (pack);
10112
10113 if (TREE_CODE (parm) == PARM_DECL
10114 || TREE_CODE (parm) == FIELD_DECL)
10115 register_local_specialization (TREE_TYPE (pack), parm);
10116 else
10117 {
10118 int idx, level;
10119
10120 if (TREE_VALUE (pack) == NULL_TREE)
10121 continue;
10122
10123 template_parm_level_and_index (parm, &level, &idx);
10124
10125 /* Update the corresponding argument. */
10126 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10127 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10128 TREE_TYPE (pack);
10129 else
10130 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10131 }
10132 }
10133
10134 if (need_local_specializations)
10135 {
10136 delete local_specializations;
10137 local_specializations = saved_local_specializations;
10138 }
10139
10140 return result;
10141 }
10142
10143 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10144 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10145 parameter packs; all parms generated from a function parameter pack will
10146 have the same DECL_PARM_INDEX. */
10147
10148 tree
10149 get_pattern_parm (tree parm, tree tmpl)
10150 {
10151 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10152 tree patparm;
10153
10154 if (DECL_ARTIFICIAL (parm))
10155 {
10156 for (patparm = DECL_ARGUMENTS (pattern);
10157 patparm; patparm = DECL_CHAIN (patparm))
10158 if (DECL_ARTIFICIAL (patparm)
10159 && DECL_NAME (parm) == DECL_NAME (patparm))
10160 break;
10161 }
10162 else
10163 {
10164 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10165 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10166 gcc_assert (DECL_PARM_INDEX (patparm)
10167 == DECL_PARM_INDEX (parm));
10168 }
10169
10170 return patparm;
10171 }
10172
10173 /* Substitute ARGS into the vector or list of template arguments T. */
10174
10175 static tree
10176 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10177 {
10178 tree orig_t = t;
10179 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10180 tree *elts;
10181
10182 if (t == error_mark_node)
10183 return error_mark_node;
10184
10185 len = TREE_VEC_LENGTH (t);
10186 elts = XALLOCAVEC (tree, len);
10187
10188 for (i = 0; i < len; i++)
10189 {
10190 tree orig_arg = TREE_VEC_ELT (t, i);
10191 tree new_arg;
10192
10193 if (TREE_CODE (orig_arg) == TREE_VEC)
10194 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10195 else if (PACK_EXPANSION_P (orig_arg))
10196 {
10197 /* Substitute into an expansion expression. */
10198 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10199
10200 if (TREE_CODE (new_arg) == TREE_VEC)
10201 /* Add to the expanded length adjustment the number of
10202 expanded arguments. We subtract one from this
10203 measurement, because the argument pack expression
10204 itself is already counted as 1 in
10205 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10206 the argument pack is empty. */
10207 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10208 }
10209 else if (ARGUMENT_PACK_P (orig_arg))
10210 {
10211 /* Substitute into each of the arguments. */
10212 new_arg = TYPE_P (orig_arg)
10213 ? cxx_make_type (TREE_CODE (orig_arg))
10214 : make_node (TREE_CODE (orig_arg));
10215
10216 SET_ARGUMENT_PACK_ARGS (
10217 new_arg,
10218 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10219 args, complain, in_decl));
10220
10221 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10222 new_arg = error_mark_node;
10223
10224 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10225 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10226 complain, in_decl);
10227 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10228
10229 if (TREE_TYPE (new_arg) == error_mark_node)
10230 new_arg = error_mark_node;
10231 }
10232 }
10233 else
10234 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10235
10236 if (new_arg == error_mark_node)
10237 return error_mark_node;
10238
10239 elts[i] = new_arg;
10240 if (new_arg != orig_arg)
10241 need_new = 1;
10242 }
10243
10244 if (!need_new)
10245 return t;
10246
10247 /* Make space for the expanded arguments coming from template
10248 argument packs. */
10249 t = make_tree_vec (len + expanded_len_adjust);
10250 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10251 arguments for a member template.
10252 In that case each TREE_VEC in ORIG_T represents a level of template
10253 arguments, and ORIG_T won't carry any non defaulted argument count.
10254 It will rather be the nested TREE_VECs that will carry one.
10255 In other words, ORIG_T carries a non defaulted argument count only
10256 if it doesn't contain any nested TREE_VEC. */
10257 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10258 {
10259 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10260 count += expanded_len_adjust;
10261 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10262 }
10263 for (i = 0, out = 0; i < len; i++)
10264 {
10265 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10266 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10267 && TREE_CODE (elts[i]) == TREE_VEC)
10268 {
10269 int idx;
10270
10271 /* Now expand the template argument pack "in place". */
10272 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10273 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10274 }
10275 else
10276 {
10277 TREE_VEC_ELT (t, out) = elts[i];
10278 out++;
10279 }
10280 }
10281
10282 return t;
10283 }
10284
10285 /* Return the result of substituting ARGS into the template parameters
10286 given by PARMS. If there are m levels of ARGS and m + n levels of
10287 PARMS, then the result will contain n levels of PARMS. For
10288 example, if PARMS is `template <class T> template <class U>
10289 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10290 result will be `template <int*, double, class V>'. */
10291
10292 static tree
10293 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10294 {
10295 tree r = NULL_TREE;
10296 tree* new_parms;
10297
10298 /* When substituting into a template, we must set
10299 PROCESSING_TEMPLATE_DECL as the template parameters may be
10300 dependent if they are based on one-another, and the dependency
10301 predicates are short-circuit outside of templates. */
10302 ++processing_template_decl;
10303
10304 for (new_parms = &r;
10305 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10306 new_parms = &(TREE_CHAIN (*new_parms)),
10307 parms = TREE_CHAIN (parms))
10308 {
10309 tree new_vec =
10310 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10311 int i;
10312
10313 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10314 {
10315 tree tuple;
10316
10317 if (parms == error_mark_node)
10318 continue;
10319
10320 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10321
10322 if (tuple == error_mark_node)
10323 continue;
10324
10325 TREE_VEC_ELT (new_vec, i) =
10326 tsubst_template_parm (tuple, args, complain);
10327 }
10328
10329 *new_parms =
10330 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10331 - TMPL_ARGS_DEPTH (args)),
10332 new_vec, NULL_TREE);
10333 }
10334
10335 --processing_template_decl;
10336
10337 return r;
10338 }
10339
10340 /* Return the result of substituting ARGS into one template parameter
10341 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10342 parameter and which TREE_PURPOSE is the default argument of the
10343 template parameter. */
10344
10345 static tree
10346 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10347 {
10348 tree default_value, parm_decl;
10349
10350 if (args == NULL_TREE
10351 || t == NULL_TREE
10352 || t == error_mark_node)
10353 return t;
10354
10355 gcc_assert (TREE_CODE (t) == TREE_LIST);
10356
10357 default_value = TREE_PURPOSE (t);
10358 parm_decl = TREE_VALUE (t);
10359
10360 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10361 if (TREE_CODE (parm_decl) == PARM_DECL
10362 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10363 parm_decl = error_mark_node;
10364 default_value = tsubst_template_arg (default_value, args,
10365 complain, NULL_TREE);
10366
10367 return build_tree_list (default_value, parm_decl);
10368 }
10369
10370 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10371 type T. If T is not an aggregate or enumeration type, it is
10372 handled as if by tsubst. IN_DECL is as for tsubst. If
10373 ENTERING_SCOPE is nonzero, T is the context for a template which
10374 we are presently tsubst'ing. Return the substituted value. */
10375
10376 static tree
10377 tsubst_aggr_type (tree t,
10378 tree args,
10379 tsubst_flags_t complain,
10380 tree in_decl,
10381 int entering_scope)
10382 {
10383 if (t == NULL_TREE)
10384 return NULL_TREE;
10385
10386 switch (TREE_CODE (t))
10387 {
10388 case RECORD_TYPE:
10389 if (TYPE_PTRMEMFUNC_P (t))
10390 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10391
10392 /* Else fall through. */
10393 case ENUMERAL_TYPE:
10394 case UNION_TYPE:
10395 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10396 {
10397 tree argvec;
10398 tree context;
10399 tree r;
10400 int saved_unevaluated_operand;
10401 int saved_inhibit_evaluation_warnings;
10402
10403 /* In "sizeof(X<I>)" we need to evaluate "I". */
10404 saved_unevaluated_operand = cp_unevaluated_operand;
10405 cp_unevaluated_operand = 0;
10406 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10407 c_inhibit_evaluation_warnings = 0;
10408
10409 /* First, determine the context for the type we are looking
10410 up. */
10411 context = TYPE_CONTEXT (t);
10412 if (context && TYPE_P (context))
10413 {
10414 context = tsubst_aggr_type (context, args, complain,
10415 in_decl, /*entering_scope=*/1);
10416 /* If context is a nested class inside a class template,
10417 it may still need to be instantiated (c++/33959). */
10418 context = complete_type (context);
10419 }
10420
10421 /* Then, figure out what arguments are appropriate for the
10422 type we are trying to find. For example, given:
10423
10424 template <class T> struct S;
10425 template <class T, class U> void f(T, U) { S<U> su; }
10426
10427 and supposing that we are instantiating f<int, double>,
10428 then our ARGS will be {int, double}, but, when looking up
10429 S we only want {double}. */
10430 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10431 complain, in_decl);
10432 if (argvec == error_mark_node)
10433 r = error_mark_node;
10434 else
10435 {
10436 r = lookup_template_class (t, argvec, in_decl, context,
10437 entering_scope, complain);
10438 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10439 }
10440
10441 cp_unevaluated_operand = saved_unevaluated_operand;
10442 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10443
10444 return r;
10445 }
10446 else
10447 /* This is not a template type, so there's nothing to do. */
10448 return t;
10449
10450 default:
10451 return tsubst (t, args, complain, in_decl);
10452 }
10453 }
10454
10455 /* Substitute into the default argument ARG (a default argument for
10456 FN), which has the indicated TYPE. */
10457
10458 tree
10459 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10460 {
10461 tree saved_class_ptr = NULL_TREE;
10462 tree saved_class_ref = NULL_TREE;
10463 int errs = errorcount + sorrycount;
10464
10465 /* This can happen in invalid code. */
10466 if (TREE_CODE (arg) == DEFAULT_ARG)
10467 return arg;
10468
10469 /* This default argument came from a template. Instantiate the
10470 default argument here, not in tsubst. In the case of
10471 something like:
10472
10473 template <class T>
10474 struct S {
10475 static T t();
10476 void f(T = t());
10477 };
10478
10479 we must be careful to do name lookup in the scope of S<T>,
10480 rather than in the current class. */
10481 push_access_scope (fn);
10482 /* The "this" pointer is not valid in a default argument. */
10483 if (cfun)
10484 {
10485 saved_class_ptr = current_class_ptr;
10486 cp_function_chain->x_current_class_ptr = NULL_TREE;
10487 saved_class_ref = current_class_ref;
10488 cp_function_chain->x_current_class_ref = NULL_TREE;
10489 }
10490
10491 push_deferring_access_checks(dk_no_deferred);
10492 /* The default argument expression may cause implicitly defined
10493 member functions to be synthesized, which will result in garbage
10494 collection. We must treat this situation as if we were within
10495 the body of function so as to avoid collecting live data on the
10496 stack. */
10497 ++function_depth;
10498 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10499 complain, NULL_TREE,
10500 /*integral_constant_expression_p=*/false);
10501 --function_depth;
10502 pop_deferring_access_checks();
10503
10504 /* Restore the "this" pointer. */
10505 if (cfun)
10506 {
10507 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10508 cp_function_chain->x_current_class_ref = saved_class_ref;
10509 }
10510
10511 if (errorcount+sorrycount > errs
10512 && (complain & tf_warning_or_error))
10513 inform (input_location,
10514 " when instantiating default argument for call to %D", fn);
10515
10516 /* Make sure the default argument is reasonable. */
10517 arg = check_default_argument (type, arg, complain);
10518
10519 pop_access_scope (fn);
10520
10521 return arg;
10522 }
10523
10524 /* Substitute into all the default arguments for FN. */
10525
10526 static void
10527 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10528 {
10529 tree arg;
10530 tree tmpl_args;
10531
10532 tmpl_args = DECL_TI_ARGS (fn);
10533
10534 /* If this function is not yet instantiated, we certainly don't need
10535 its default arguments. */
10536 if (uses_template_parms (tmpl_args))
10537 return;
10538 /* Don't do this again for clones. */
10539 if (DECL_CLONED_FUNCTION_P (fn))
10540 return;
10541
10542 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10543 arg;
10544 arg = TREE_CHAIN (arg))
10545 if (TREE_PURPOSE (arg))
10546 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10547 TREE_VALUE (arg),
10548 TREE_PURPOSE (arg),
10549 complain);
10550 }
10551
10552 /* Substitute the ARGS into the T, which is a _DECL. Return the
10553 result of the substitution. Issue error and warning messages under
10554 control of COMPLAIN. */
10555
10556 static tree
10557 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10558 {
10559 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10560 location_t saved_loc;
10561 tree r = NULL_TREE;
10562 tree in_decl = t;
10563 hashval_t hash = 0;
10564
10565 /* Set the filename and linenumber to improve error-reporting. */
10566 saved_loc = input_location;
10567 input_location = DECL_SOURCE_LOCATION (t);
10568
10569 switch (TREE_CODE (t))
10570 {
10571 case TEMPLATE_DECL:
10572 {
10573 /* We can get here when processing a member function template,
10574 member class template, or template template parameter. */
10575 tree decl = DECL_TEMPLATE_RESULT (t);
10576 tree spec;
10577 tree tmpl_args;
10578 tree full_args;
10579
10580 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10581 {
10582 /* Template template parameter is treated here. */
10583 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10584 if (new_type == error_mark_node)
10585 RETURN (error_mark_node);
10586 /* If we get a real template back, return it. This can happen in
10587 the context of most_specialized_partial_spec. */
10588 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10589 return new_type;
10590
10591 r = copy_decl (t);
10592 DECL_CHAIN (r) = NULL_TREE;
10593 TREE_TYPE (r) = new_type;
10594 DECL_TEMPLATE_RESULT (r)
10595 = build_decl (DECL_SOURCE_LOCATION (decl),
10596 TYPE_DECL, DECL_NAME (decl), new_type);
10597 DECL_TEMPLATE_PARMS (r)
10598 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10599 complain);
10600 TYPE_NAME (new_type) = r;
10601 break;
10602 }
10603
10604 /* We might already have an instance of this template.
10605 The ARGS are for the surrounding class type, so the
10606 full args contain the tsubst'd args for the context,
10607 plus the innermost args from the template decl. */
10608 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10609 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10610 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10611 /* Because this is a template, the arguments will still be
10612 dependent, even after substitution. If
10613 PROCESSING_TEMPLATE_DECL is not set, the dependency
10614 predicates will short-circuit. */
10615 ++processing_template_decl;
10616 full_args = tsubst_template_args (tmpl_args, args,
10617 complain, in_decl);
10618 --processing_template_decl;
10619 if (full_args == error_mark_node)
10620 RETURN (error_mark_node);
10621
10622 /* If this is a default template template argument,
10623 tsubst might not have changed anything. */
10624 if (full_args == tmpl_args)
10625 RETURN (t);
10626
10627 hash = hash_tmpl_and_args (t, full_args);
10628 spec = retrieve_specialization (t, full_args, hash);
10629 if (spec != NULL_TREE)
10630 {
10631 r = spec;
10632 break;
10633 }
10634
10635 /* Make a new template decl. It will be similar to the
10636 original, but will record the current template arguments.
10637 We also create a new function declaration, which is just
10638 like the old one, but points to this new template, rather
10639 than the old one. */
10640 r = copy_decl (t);
10641 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10642 DECL_CHAIN (r) = NULL_TREE;
10643
10644 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10645
10646 if (TREE_CODE (decl) == TYPE_DECL
10647 && !TYPE_DECL_ALIAS_P (decl))
10648 {
10649 tree new_type;
10650 ++processing_template_decl;
10651 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10652 --processing_template_decl;
10653 if (new_type == error_mark_node)
10654 RETURN (error_mark_node);
10655
10656 TREE_TYPE (r) = new_type;
10657 /* For a partial specialization, we need to keep pointing to
10658 the primary template. */
10659 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10660 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10661 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10662 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10663 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10664 }
10665 else
10666 {
10667 tree new_decl;
10668 ++processing_template_decl;
10669 new_decl = tsubst (decl, args, complain, in_decl);
10670 --processing_template_decl;
10671 if (new_decl == error_mark_node)
10672 RETURN (error_mark_node);
10673
10674 DECL_TEMPLATE_RESULT (r) = new_decl;
10675 DECL_TI_TEMPLATE (new_decl) = r;
10676 TREE_TYPE (r) = TREE_TYPE (new_decl);
10677 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10678 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10679 }
10680
10681 SET_DECL_IMPLICIT_INSTANTIATION (r);
10682 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10683 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10684
10685 /* The template parameters for this new template are all the
10686 template parameters for the old template, except the
10687 outermost level of parameters. */
10688 DECL_TEMPLATE_PARMS (r)
10689 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10690 complain);
10691
10692 if (PRIMARY_TEMPLATE_P (t))
10693 DECL_PRIMARY_TEMPLATE (r) = r;
10694
10695 if (TREE_CODE (decl) != TYPE_DECL && TREE_CODE (decl) != VAR_DECL)
10696 /* Record this non-type partial instantiation. */
10697 register_specialization (r, t,
10698 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10699 false, hash);
10700 }
10701 break;
10702
10703 case FUNCTION_DECL:
10704 {
10705 tree ctx;
10706 tree argvec = NULL_TREE;
10707 tree *friends;
10708 tree gen_tmpl;
10709 tree type;
10710 int member;
10711 int args_depth;
10712 int parms_depth;
10713
10714 /* Nobody should be tsubst'ing into non-template functions. */
10715 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10716
10717 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10718 {
10719 tree spec;
10720 bool dependent_p;
10721
10722 /* If T is not dependent, just return it. We have to
10723 increment PROCESSING_TEMPLATE_DECL because
10724 value_dependent_expression_p assumes that nothing is
10725 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10726 ++processing_template_decl;
10727 dependent_p = value_dependent_expression_p (t);
10728 --processing_template_decl;
10729 if (!dependent_p)
10730 RETURN (t);
10731
10732 /* Calculate the most general template of which R is a
10733 specialization, and the complete set of arguments used to
10734 specialize R. */
10735 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10736 argvec = tsubst_template_args (DECL_TI_ARGS
10737 (DECL_TEMPLATE_RESULT
10738 (DECL_TI_TEMPLATE (t))),
10739 args, complain, in_decl);
10740 if (argvec == error_mark_node)
10741 RETURN (error_mark_node);
10742
10743 /* Check to see if we already have this specialization. */
10744 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10745 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10746
10747 if (spec)
10748 {
10749 r = spec;
10750 break;
10751 }
10752
10753 /* We can see more levels of arguments than parameters if
10754 there was a specialization of a member template, like
10755 this:
10756
10757 template <class T> struct S { template <class U> void f(); }
10758 template <> template <class U> void S<int>::f(U);
10759
10760 Here, we'll be substituting into the specialization,
10761 because that's where we can find the code we actually
10762 want to generate, but we'll have enough arguments for
10763 the most general template.
10764
10765 We also deal with the peculiar case:
10766
10767 template <class T> struct S {
10768 template <class U> friend void f();
10769 };
10770 template <class U> void f() {}
10771 template S<int>;
10772 template void f<double>();
10773
10774 Here, the ARGS for the instantiation of will be {int,
10775 double}. But, we only need as many ARGS as there are
10776 levels of template parameters in CODE_PATTERN. We are
10777 careful not to get fooled into reducing the ARGS in
10778 situations like:
10779
10780 template <class T> struct S { template <class U> void f(U); }
10781 template <class T> template <> void S<T>::f(int) {}
10782
10783 which we can spot because the pattern will be a
10784 specialization in this case. */
10785 args_depth = TMPL_ARGS_DEPTH (args);
10786 parms_depth =
10787 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10788 if (args_depth > parms_depth
10789 && !DECL_TEMPLATE_SPECIALIZATION (t))
10790 args = get_innermost_template_args (args, parms_depth);
10791 }
10792 else
10793 {
10794 /* This special case arises when we have something like this:
10795
10796 template <class T> struct S {
10797 friend void f<int>(int, double);
10798 };
10799
10800 Here, the DECL_TI_TEMPLATE for the friend declaration
10801 will be an IDENTIFIER_NODE. We are being called from
10802 tsubst_friend_function, and we want only to create a
10803 new decl (R) with appropriate types so that we can call
10804 determine_specialization. */
10805 gen_tmpl = NULL_TREE;
10806 }
10807
10808 if (DECL_CLASS_SCOPE_P (t))
10809 {
10810 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10811 member = 2;
10812 else
10813 member = 1;
10814 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10815 complain, t, /*entering_scope=*/1);
10816 }
10817 else
10818 {
10819 member = 0;
10820 ctx = DECL_CONTEXT (t);
10821 }
10822 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10823 if (type == error_mark_node)
10824 RETURN (error_mark_node);
10825
10826 /* If we hit excessive deduction depth, the type is bogus even if
10827 it isn't error_mark_node, so don't build a decl. */
10828 if (excessive_deduction_depth)
10829 RETURN (error_mark_node);
10830
10831 /* We do NOT check for matching decls pushed separately at this
10832 point, as they may not represent instantiations of this
10833 template, and in any case are considered separate under the
10834 discrete model. */
10835 r = copy_decl (t);
10836 DECL_USE_TEMPLATE (r) = 0;
10837 TREE_TYPE (r) = type;
10838 /* Clear out the mangled name and RTL for the instantiation. */
10839 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10840 SET_DECL_RTL (r, NULL);
10841 /* Leave DECL_INITIAL set on deleted instantiations. */
10842 if (!DECL_DELETED_FN (r))
10843 DECL_INITIAL (r) = NULL_TREE;
10844 DECL_CONTEXT (r) = ctx;
10845
10846 /* OpenMP UDRs have the only argument a reference to the declared
10847 type. We want to diagnose if the declared type is a reference,
10848 which is invalid, but as references to references are usually
10849 quietly merged, diagnose it here. */
10850 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10851 {
10852 tree argtype
10853 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10854 argtype = tsubst (argtype, args, complain, in_decl);
10855 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10856 error_at (DECL_SOURCE_LOCATION (t),
10857 "reference type %qT in "
10858 "%<#pragma omp declare reduction%>", argtype);
10859 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10860 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10861 argtype);
10862 }
10863
10864 if (member && DECL_CONV_FN_P (r))
10865 /* Type-conversion operator. Reconstruct the name, in
10866 case it's the name of one of the template's parameters. */
10867 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10868
10869 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10870 complain, t);
10871 DECL_RESULT (r) = NULL_TREE;
10872
10873 TREE_STATIC (r) = 0;
10874 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10875 DECL_EXTERNAL (r) = 1;
10876 /* If this is an instantiation of a function with internal
10877 linkage, we already know what object file linkage will be
10878 assigned to the instantiation. */
10879 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10880 DECL_DEFER_OUTPUT (r) = 0;
10881 DECL_CHAIN (r) = NULL_TREE;
10882 DECL_PENDING_INLINE_INFO (r) = 0;
10883 DECL_PENDING_INLINE_P (r) = 0;
10884 DECL_SAVED_TREE (r) = NULL_TREE;
10885 DECL_STRUCT_FUNCTION (r) = NULL;
10886 TREE_USED (r) = 0;
10887 /* We'll re-clone as appropriate in instantiate_template. */
10888 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10889
10890 /* If we aren't complaining now, return on error before we register
10891 the specialization so that we'll complain eventually. */
10892 if ((complain & tf_error) == 0
10893 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10894 && !grok_op_properties (r, /*complain=*/false))
10895 RETURN (error_mark_node);
10896
10897 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10898 this in the special friend case mentioned above where
10899 GEN_TMPL is NULL. */
10900 if (gen_tmpl)
10901 {
10902 DECL_TEMPLATE_INFO (r)
10903 = build_template_info (gen_tmpl, argvec);
10904 SET_DECL_IMPLICIT_INSTANTIATION (r);
10905
10906 tree new_r
10907 = register_specialization (r, gen_tmpl, argvec, false, hash);
10908 if (new_r != r)
10909 /* We instantiated this while substituting into
10910 the type earlier (template/friend54.C). */
10911 RETURN (new_r);
10912
10913 /* We're not supposed to instantiate default arguments
10914 until they are called, for a template. But, for a
10915 declaration like:
10916
10917 template <class T> void f ()
10918 { extern void g(int i = T()); }
10919
10920 we should do the substitution when the template is
10921 instantiated. We handle the member function case in
10922 instantiate_class_template since the default arguments
10923 might refer to other members of the class. */
10924 if (!member
10925 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10926 && !uses_template_parms (argvec))
10927 tsubst_default_arguments (r, complain);
10928 }
10929 else
10930 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10931
10932 /* Copy the list of befriending classes. */
10933 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10934 *friends;
10935 friends = &TREE_CHAIN (*friends))
10936 {
10937 *friends = copy_node (*friends);
10938 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10939 args, complain,
10940 in_decl);
10941 }
10942
10943 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10944 {
10945 maybe_retrofit_in_chrg (r);
10946 if (DECL_CONSTRUCTOR_P (r))
10947 grok_ctor_properties (ctx, r);
10948 if (DECL_INHERITED_CTOR_BASE (r))
10949 deduce_inheriting_ctor (r);
10950 /* If this is an instantiation of a member template, clone it.
10951 If it isn't, that'll be handled by
10952 clone_constructors_and_destructors. */
10953 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10954 clone_function_decl (r, /*update_method_vec_p=*/0);
10955 }
10956 else if ((complain & tf_error) != 0
10957 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10958 && !grok_op_properties (r, /*complain=*/true))
10959 RETURN (error_mark_node);
10960
10961 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10962 SET_DECL_FRIEND_CONTEXT (r,
10963 tsubst (DECL_FRIEND_CONTEXT (t),
10964 args, complain, in_decl));
10965
10966 /* Possibly limit visibility based on template args. */
10967 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10968 if (DECL_VISIBILITY_SPECIFIED (t))
10969 {
10970 DECL_VISIBILITY_SPECIFIED (r) = 0;
10971 DECL_ATTRIBUTES (r)
10972 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10973 }
10974 determine_visibility (r);
10975 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10976 && !processing_template_decl)
10977 defaulted_late_check (r);
10978
10979 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10980 args, complain, in_decl);
10981 }
10982 break;
10983
10984 case PARM_DECL:
10985 {
10986 tree type = NULL_TREE;
10987 int i, len = 1;
10988 tree expanded_types = NULL_TREE;
10989 tree prev_r = NULL_TREE;
10990 tree first_r = NULL_TREE;
10991
10992 if (DECL_PACK_P (t))
10993 {
10994 /* If there is a local specialization that isn't a
10995 parameter pack, it means that we're doing a "simple"
10996 substitution from inside tsubst_pack_expansion. Just
10997 return the local specialization (which will be a single
10998 parm). */
10999 tree spec = retrieve_local_specialization (t);
11000 if (spec
11001 && TREE_CODE (spec) == PARM_DECL
11002 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11003 RETURN (spec);
11004
11005 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11006 the parameters in this function parameter pack. */
11007 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11008 complain, in_decl);
11009 if (TREE_CODE (expanded_types) == TREE_VEC)
11010 {
11011 len = TREE_VEC_LENGTH (expanded_types);
11012
11013 /* Zero-length parameter packs are boring. Just substitute
11014 into the chain. */
11015 if (len == 0)
11016 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11017 TREE_CHAIN (t)));
11018 }
11019 else
11020 {
11021 /* All we did was update the type. Make a note of that. */
11022 type = expanded_types;
11023 expanded_types = NULL_TREE;
11024 }
11025 }
11026
11027 /* Loop through all of the parameters we'll build. When T is
11028 a function parameter pack, LEN is the number of expanded
11029 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11030 r = NULL_TREE;
11031 for (i = 0; i < len; ++i)
11032 {
11033 prev_r = r;
11034 r = copy_node (t);
11035 if (DECL_TEMPLATE_PARM_P (t))
11036 SET_DECL_TEMPLATE_PARM_P (r);
11037
11038 if (expanded_types)
11039 /* We're on the Ith parameter of the function parameter
11040 pack. */
11041 {
11042 /* Get the Ith type. */
11043 type = TREE_VEC_ELT (expanded_types, i);
11044
11045 /* Rename the parameter to include the index. */
11046 DECL_NAME (r)
11047 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11048 }
11049 else if (!type)
11050 /* We're dealing with a normal parameter. */
11051 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11052
11053 type = type_decays_to (type);
11054 TREE_TYPE (r) = type;
11055 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11056
11057 if (DECL_INITIAL (r))
11058 {
11059 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11060 DECL_INITIAL (r) = TREE_TYPE (r);
11061 else
11062 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11063 complain, in_decl);
11064 }
11065
11066 DECL_CONTEXT (r) = NULL_TREE;
11067
11068 if (!DECL_TEMPLATE_PARM_P (r))
11069 DECL_ARG_TYPE (r) = type_passed_as (type);
11070
11071 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11072 args, complain, in_decl);
11073
11074 /* Keep track of the first new parameter we
11075 generate. That's what will be returned to the
11076 caller. */
11077 if (!first_r)
11078 first_r = r;
11079
11080 /* Build a proper chain of parameters when substituting
11081 into a function parameter pack. */
11082 if (prev_r)
11083 DECL_CHAIN (prev_r) = r;
11084 }
11085
11086 /* If cp_unevaluated_operand is set, we're just looking for a
11087 single dummy parameter, so don't keep going. */
11088 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11089 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11090 complain, DECL_CHAIN (t));
11091
11092 /* FIRST_R contains the start of the chain we've built. */
11093 r = first_r;
11094 }
11095 break;
11096
11097 case FIELD_DECL:
11098 {
11099 tree type = NULL_TREE;
11100 tree vec = NULL_TREE;
11101 tree expanded_types = NULL_TREE;
11102 int len = 1;
11103
11104 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11105 {
11106 /* This field is a lambda capture pack. Return a TREE_VEC of
11107 the expanded fields to instantiate_class_template_1 and
11108 store them in the specializations hash table as a
11109 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11110 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11111 complain, in_decl);
11112 if (TREE_CODE (expanded_types) == TREE_VEC)
11113 {
11114 len = TREE_VEC_LENGTH (expanded_types);
11115 vec = make_tree_vec (len);
11116 }
11117 else
11118 {
11119 /* All we did was update the type. Make a note of that. */
11120 type = expanded_types;
11121 expanded_types = NULL_TREE;
11122 }
11123 }
11124
11125 for (int i = 0; i < len; ++i)
11126 {
11127 r = copy_decl (t);
11128 if (expanded_types)
11129 {
11130 type = TREE_VEC_ELT (expanded_types, i);
11131 DECL_NAME (r)
11132 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11133 }
11134 else if (!type)
11135 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11136
11137 if (type == error_mark_node)
11138 RETURN (error_mark_node);
11139 TREE_TYPE (r) = type;
11140 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11141
11142 if (DECL_C_BIT_FIELD (r))
11143 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11144 non-bit-fields DECL_INITIAL is a non-static data member
11145 initializer, which gets deferred instantiation. */
11146 DECL_INITIAL (r)
11147 = tsubst_expr (DECL_INITIAL (t), args,
11148 complain, in_decl,
11149 /*integral_constant_expression_p=*/true);
11150 else if (DECL_INITIAL (t))
11151 {
11152 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11153 NSDMI in perform_member_init. Still set DECL_INITIAL
11154 so that we know there is one. */
11155 DECL_INITIAL (r) = void_node;
11156 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11157 retrofit_lang_decl (r);
11158 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11159 }
11160 /* We don't have to set DECL_CONTEXT here; it is set by
11161 finish_member_declaration. */
11162 DECL_CHAIN (r) = NULL_TREE;
11163
11164 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11165 args, complain, in_decl);
11166
11167 if (vec)
11168 TREE_VEC_ELT (vec, i) = r;
11169 }
11170
11171 if (vec)
11172 {
11173 r = vec;
11174 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11175 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11176 SET_ARGUMENT_PACK_ARGS (pack, vec);
11177 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11178 TREE_TYPE (pack) = tpack;
11179 register_specialization (pack, t, args, false, 0);
11180 }
11181 }
11182 break;
11183
11184 case USING_DECL:
11185 /* We reach here only for member using decls. We also need to check
11186 uses_template_parms because DECL_DEPENDENT_P is not set for a
11187 using-declaration that designates a member of the current
11188 instantiation (c++/53549). */
11189 if (DECL_DEPENDENT_P (t)
11190 || uses_template_parms (USING_DECL_SCOPE (t)))
11191 {
11192 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11193 complain, in_decl);
11194 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11195 r = do_class_using_decl (inst_scope, name);
11196 if (!r)
11197 r = error_mark_node;
11198 else
11199 {
11200 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11201 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11202 }
11203 }
11204 else
11205 {
11206 r = copy_node (t);
11207 DECL_CHAIN (r) = NULL_TREE;
11208 }
11209 break;
11210
11211 case TYPE_DECL:
11212 case VAR_DECL:
11213 {
11214 tree argvec = NULL_TREE;
11215 tree gen_tmpl = NULL_TREE;
11216 tree spec;
11217 tree tmpl = NULL_TREE;
11218 tree ctx;
11219 tree type = NULL_TREE;
11220 bool local_p;
11221
11222 if (TREE_TYPE (t) == error_mark_node)
11223 RETURN (error_mark_node);
11224
11225 if (TREE_CODE (t) == TYPE_DECL
11226 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11227 {
11228 /* If this is the canonical decl, we don't have to
11229 mess with instantiations, and often we can't (for
11230 typename, template type parms and such). Note that
11231 TYPE_NAME is not correct for the above test if
11232 we've copied the type for a typedef. */
11233 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11234 if (type == error_mark_node)
11235 RETURN (error_mark_node);
11236 r = TYPE_NAME (type);
11237 break;
11238 }
11239
11240 /* Check to see if we already have the specialization we
11241 need. */
11242 spec = NULL_TREE;
11243 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11244 {
11245 /* T is a static data member or namespace-scope entity.
11246 We have to substitute into namespace-scope variables
11247 (even though such entities are never templates) because
11248 of cases like:
11249
11250 template <class T> void f() { extern T t; }
11251
11252 where the entity referenced is not known until
11253 instantiation time. */
11254 local_p = false;
11255 ctx = DECL_CONTEXT (t);
11256 if (DECL_CLASS_SCOPE_P (t))
11257 {
11258 ctx = tsubst_aggr_type (ctx, args,
11259 complain,
11260 in_decl, /*entering_scope=*/1);
11261 /* If CTX is unchanged, then T is in fact the
11262 specialization we want. That situation occurs when
11263 referencing a static data member within in its own
11264 class. We can use pointer equality, rather than
11265 same_type_p, because DECL_CONTEXT is always
11266 canonical... */
11267 if (ctx == DECL_CONTEXT (t)
11268 /* ... unless T is a member template; in which
11269 case our caller can be willing to create a
11270 specialization of that template represented
11271 by T. */
11272 && !(DECL_TI_TEMPLATE (t)
11273 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
11274 spec = t;
11275 }
11276
11277 if (!spec)
11278 {
11279 tmpl = DECL_TI_TEMPLATE (t);
11280 gen_tmpl = most_general_template (tmpl);
11281 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11282 if (argvec != error_mark_node)
11283 argvec = (coerce_innermost_template_parms
11284 (DECL_TEMPLATE_PARMS (gen_tmpl),
11285 argvec, t, complain,
11286 /*all*/true, /*defarg*/true));
11287 if (argvec == error_mark_node)
11288 RETURN (error_mark_node);
11289 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11290 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11291 }
11292 }
11293 else
11294 {
11295 /* A local variable. */
11296 local_p = true;
11297 /* Subsequent calls to pushdecl will fill this in. */
11298 ctx = NULL_TREE;
11299 spec = retrieve_local_specialization (t);
11300 }
11301 /* If we already have the specialization we need, there is
11302 nothing more to do. */
11303 if (spec)
11304 {
11305 r = spec;
11306 break;
11307 }
11308
11309 /* Create a new node for the specialization we need. */
11310 r = copy_decl (t);
11311 if (type == NULL_TREE)
11312 {
11313 if (is_typedef_decl (t))
11314 type = DECL_ORIGINAL_TYPE (t);
11315 else
11316 type = TREE_TYPE (t);
11317 if (VAR_P (t)
11318 && VAR_HAD_UNKNOWN_BOUND (t)
11319 && type != error_mark_node)
11320 type = strip_array_domain (type);
11321 type = tsubst (type, args, complain, in_decl);
11322 }
11323 if (VAR_P (r))
11324 {
11325 /* Even if the original location is out of scope, the
11326 newly substituted one is not. */
11327 DECL_DEAD_FOR_LOCAL (r) = 0;
11328 DECL_INITIALIZED_P (r) = 0;
11329 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11330 if (type == error_mark_node)
11331 RETURN (error_mark_node);
11332 if (TREE_CODE (type) == FUNCTION_TYPE)
11333 {
11334 /* It may seem that this case cannot occur, since:
11335
11336 typedef void f();
11337 void g() { f x; }
11338
11339 declares a function, not a variable. However:
11340
11341 typedef void f();
11342 template <typename T> void g() { T t; }
11343 template void g<f>();
11344
11345 is an attempt to declare a variable with function
11346 type. */
11347 error ("variable %qD has function type",
11348 /* R is not yet sufficiently initialized, so we
11349 just use its name. */
11350 DECL_NAME (r));
11351 RETURN (error_mark_node);
11352 }
11353 type = complete_type (type);
11354 /* Wait until cp_finish_decl to set this again, to handle
11355 circular dependency (template/instantiate6.C). */
11356 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11357 type = check_var_type (DECL_NAME (r), type);
11358
11359 if (DECL_HAS_VALUE_EXPR_P (t))
11360 {
11361 tree ve = DECL_VALUE_EXPR (t);
11362 ve = tsubst_expr (ve, args, complain, in_decl,
11363 /*constant_expression_p=*/false);
11364 if (REFERENCE_REF_P (ve))
11365 {
11366 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11367 ve = TREE_OPERAND (ve, 0);
11368 }
11369 SET_DECL_VALUE_EXPR (r, ve);
11370 }
11371 if (TREE_STATIC (r) || DECL_EXTERNAL (r))
11372 set_decl_tls_model (r, decl_tls_model (t));
11373 }
11374 else if (DECL_SELF_REFERENCE_P (t))
11375 SET_DECL_SELF_REFERENCE_P (r);
11376 TREE_TYPE (r) = type;
11377 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11378 DECL_CONTEXT (r) = ctx;
11379 /* Clear out the mangled name and RTL for the instantiation. */
11380 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11381 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11382 SET_DECL_RTL (r, NULL);
11383 /* The initializer must not be expanded until it is required;
11384 see [temp.inst]. */
11385 DECL_INITIAL (r) = NULL_TREE;
11386 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11387 SET_DECL_RTL (r, NULL);
11388 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11389 if (VAR_P (r))
11390 {
11391 /* Possibly limit visibility based on template args. */
11392 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11393 if (DECL_VISIBILITY_SPECIFIED (t))
11394 {
11395 DECL_VISIBILITY_SPECIFIED (r) = 0;
11396 DECL_ATTRIBUTES (r)
11397 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11398 }
11399 determine_visibility (r);
11400 }
11401
11402 if (!local_p)
11403 {
11404 /* A static data member declaration is always marked
11405 external when it is declared in-class, even if an
11406 initializer is present. We mimic the non-template
11407 processing here. */
11408 DECL_EXTERNAL (r) = 1;
11409
11410 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11411 SET_DECL_IMPLICIT_INSTANTIATION (r);
11412 register_specialization (r, gen_tmpl, argvec, false, hash);
11413 }
11414 else if (!cp_unevaluated_operand)
11415 register_local_specialization (r, t);
11416
11417 DECL_CHAIN (r) = NULL_TREE;
11418
11419 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11420 /*flags=*/0,
11421 args, complain, in_decl);
11422
11423 /* Preserve a typedef that names a type. */
11424 if (is_typedef_decl (r))
11425 {
11426 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11427 set_underlying_type (r);
11428 }
11429
11430 layout_decl (r, 0);
11431 }
11432 break;
11433
11434 default:
11435 gcc_unreachable ();
11436 }
11437 #undef RETURN
11438
11439 out:
11440 /* Restore the file and line information. */
11441 input_location = saved_loc;
11442
11443 return r;
11444 }
11445
11446 /* Substitute into the ARG_TYPES of a function type.
11447 If END is a TREE_CHAIN, leave it and any following types
11448 un-substituted. */
11449
11450 static tree
11451 tsubst_arg_types (tree arg_types,
11452 tree args,
11453 tree end,
11454 tsubst_flags_t complain,
11455 tree in_decl)
11456 {
11457 tree remaining_arg_types;
11458 tree type = NULL_TREE;
11459 int i = 1;
11460 tree expanded_args = NULL_TREE;
11461 tree default_arg;
11462
11463 if (!arg_types || arg_types == void_list_node || arg_types == end)
11464 return arg_types;
11465
11466 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11467 args, end, complain, in_decl);
11468 if (remaining_arg_types == error_mark_node)
11469 return error_mark_node;
11470
11471 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11472 {
11473 /* For a pack expansion, perform substitution on the
11474 entire expression. Later on, we'll handle the arguments
11475 one-by-one. */
11476 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11477 args, complain, in_decl);
11478
11479 if (TREE_CODE (expanded_args) == TREE_VEC)
11480 /* So that we'll spin through the parameters, one by one. */
11481 i = TREE_VEC_LENGTH (expanded_args);
11482 else
11483 {
11484 /* We only partially substituted into the parameter
11485 pack. Our type is TYPE_PACK_EXPANSION. */
11486 type = expanded_args;
11487 expanded_args = NULL_TREE;
11488 }
11489 }
11490
11491 while (i > 0) {
11492 --i;
11493
11494 if (expanded_args)
11495 type = TREE_VEC_ELT (expanded_args, i);
11496 else if (!type)
11497 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11498
11499 if (type == error_mark_node)
11500 return error_mark_node;
11501 if (VOID_TYPE_P (type))
11502 {
11503 if (complain & tf_error)
11504 {
11505 error ("invalid parameter type %qT", type);
11506 if (in_decl)
11507 error ("in declaration %q+D", in_decl);
11508 }
11509 return error_mark_node;
11510 }
11511 /* DR 657. */
11512 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11513 return error_mark_node;
11514
11515 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11516 top-level qualifiers as required. */
11517 type = cv_unqualified (type_decays_to (type));
11518
11519 /* We do not substitute into default arguments here. The standard
11520 mandates that they be instantiated only when needed, which is
11521 done in build_over_call. */
11522 default_arg = TREE_PURPOSE (arg_types);
11523
11524 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11525 {
11526 /* We've instantiated a template before its default arguments
11527 have been parsed. This can happen for a nested template
11528 class, and is not an error unless we require the default
11529 argument in a call of this function. */
11530 remaining_arg_types =
11531 tree_cons (default_arg, type, remaining_arg_types);
11532 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11533 }
11534 else
11535 remaining_arg_types =
11536 hash_tree_cons (default_arg, type, remaining_arg_types);
11537 }
11538
11539 return remaining_arg_types;
11540 }
11541
11542 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11543 *not* handle the exception-specification for FNTYPE, because the
11544 initial substitution of explicitly provided template parameters
11545 during argument deduction forbids substitution into the
11546 exception-specification:
11547
11548 [temp.deduct]
11549
11550 All references in the function type of the function template to the
11551 corresponding template parameters are replaced by the specified tem-
11552 plate argument values. If a substitution in a template parameter or
11553 in the function type of the function template results in an invalid
11554 type, type deduction fails. [Note: The equivalent substitution in
11555 exception specifications is done only when the function is instanti-
11556 ated, at which point a program is ill-formed if the substitution
11557 results in an invalid type.] */
11558
11559 static tree
11560 tsubst_function_type (tree t,
11561 tree args,
11562 tsubst_flags_t complain,
11563 tree in_decl)
11564 {
11565 tree return_type;
11566 tree arg_types = NULL_TREE;
11567 tree fntype;
11568
11569 /* The TYPE_CONTEXT is not used for function/method types. */
11570 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11571
11572 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11573 failure. */
11574 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
11575
11576 if (late_return_type_p)
11577 {
11578 /* Substitute the argument types. */
11579 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11580 complain, in_decl);
11581 if (arg_types == error_mark_node)
11582 return error_mark_node;
11583
11584 tree save_ccp = current_class_ptr;
11585 tree save_ccr = current_class_ref;
11586 tree this_type = (TREE_CODE (t) == METHOD_TYPE
11587 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
11588 bool do_inject = this_type && CLASS_TYPE_P (this_type);
11589 if (do_inject)
11590 {
11591 /* DR 1207: 'this' is in scope in the trailing return type. */
11592 inject_this_parameter (this_type, cp_type_quals (this_type));
11593 }
11594
11595 /* Substitute the return type. */
11596 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11597
11598 if (do_inject)
11599 {
11600 current_class_ptr = save_ccp;
11601 current_class_ref = save_ccr;
11602 }
11603 }
11604 else
11605 /* Substitute the return type. */
11606 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11607
11608 if (return_type == error_mark_node)
11609 return error_mark_node;
11610 /* DR 486 clarifies that creation of a function type with an
11611 invalid return type is a deduction failure. */
11612 if (TREE_CODE (return_type) == ARRAY_TYPE
11613 || TREE_CODE (return_type) == FUNCTION_TYPE)
11614 {
11615 if (complain & tf_error)
11616 {
11617 if (TREE_CODE (return_type) == ARRAY_TYPE)
11618 error ("function returning an array");
11619 else
11620 error ("function returning a function");
11621 }
11622 return error_mark_node;
11623 }
11624 /* And DR 657. */
11625 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11626 return error_mark_node;
11627
11628 if (!late_return_type_p)
11629 {
11630 /* Substitute the argument types. */
11631 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11632 complain, in_decl);
11633 if (arg_types == error_mark_node)
11634 return error_mark_node;
11635 }
11636
11637 /* Construct a new type node and return it. */
11638 if (TREE_CODE (t) == FUNCTION_TYPE)
11639 {
11640 fntype = build_function_type (return_type, arg_types);
11641 fntype = apply_memfn_quals (fntype,
11642 type_memfn_quals (t),
11643 type_memfn_rqual (t));
11644 }
11645 else
11646 {
11647 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11648 /* Don't pick up extra function qualifiers from the basetype. */
11649 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11650 if (! MAYBE_CLASS_TYPE_P (r))
11651 {
11652 /* [temp.deduct]
11653
11654 Type deduction may fail for any of the following
11655 reasons:
11656
11657 -- Attempting to create "pointer to member of T" when T
11658 is not a class type. */
11659 if (complain & tf_error)
11660 error ("creating pointer to member function of non-class type %qT",
11661 r);
11662 return error_mark_node;
11663 }
11664
11665 fntype = build_method_type_directly (r, return_type,
11666 TREE_CHAIN (arg_types));
11667 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11668 }
11669 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11670
11671 if (late_return_type_p)
11672 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
11673
11674 return fntype;
11675 }
11676
11677 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11678 ARGS into that specification, and return the substituted
11679 specification. If there is no specification, return NULL_TREE. */
11680
11681 static tree
11682 tsubst_exception_specification (tree fntype,
11683 tree args,
11684 tsubst_flags_t complain,
11685 tree in_decl,
11686 bool defer_ok)
11687 {
11688 tree specs;
11689 tree new_specs;
11690
11691 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11692 new_specs = NULL_TREE;
11693 if (specs && TREE_PURPOSE (specs))
11694 {
11695 /* A noexcept-specifier. */
11696 tree expr = TREE_PURPOSE (specs);
11697 if (TREE_CODE (expr) == INTEGER_CST)
11698 new_specs = expr;
11699 else if (defer_ok)
11700 {
11701 /* Defer instantiation of noexcept-specifiers to avoid
11702 excessive instantiations (c++/49107). */
11703 new_specs = make_node (DEFERRED_NOEXCEPT);
11704 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11705 {
11706 /* We already partially instantiated this member template,
11707 so combine the new args with the old. */
11708 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11709 = DEFERRED_NOEXCEPT_PATTERN (expr);
11710 DEFERRED_NOEXCEPT_ARGS (new_specs)
11711 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11712 }
11713 else
11714 {
11715 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11716 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11717 }
11718 }
11719 else
11720 new_specs = tsubst_copy_and_build
11721 (expr, args, complain, in_decl, /*function_p=*/false,
11722 /*integral_constant_expression_p=*/true);
11723 new_specs = build_noexcept_spec (new_specs, complain);
11724 }
11725 else if (specs)
11726 {
11727 if (! TREE_VALUE (specs))
11728 new_specs = specs;
11729 else
11730 while (specs)
11731 {
11732 tree spec;
11733 int i, len = 1;
11734 tree expanded_specs = NULL_TREE;
11735
11736 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11737 {
11738 /* Expand the pack expansion type. */
11739 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11740 args, complain,
11741 in_decl);
11742
11743 if (expanded_specs == error_mark_node)
11744 return error_mark_node;
11745 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11746 len = TREE_VEC_LENGTH (expanded_specs);
11747 else
11748 {
11749 /* We're substituting into a member template, so
11750 we got a TYPE_PACK_EXPANSION back. Add that
11751 expansion and move on. */
11752 gcc_assert (TREE_CODE (expanded_specs)
11753 == TYPE_PACK_EXPANSION);
11754 new_specs = add_exception_specifier (new_specs,
11755 expanded_specs,
11756 complain);
11757 specs = TREE_CHAIN (specs);
11758 continue;
11759 }
11760 }
11761
11762 for (i = 0; i < len; ++i)
11763 {
11764 if (expanded_specs)
11765 spec = TREE_VEC_ELT (expanded_specs, i);
11766 else
11767 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11768 if (spec == error_mark_node)
11769 return spec;
11770 new_specs = add_exception_specifier (new_specs, spec,
11771 complain);
11772 }
11773
11774 specs = TREE_CHAIN (specs);
11775 }
11776 }
11777 return new_specs;
11778 }
11779
11780 /* Take the tree structure T and replace template parameters used
11781 therein with the argument vector ARGS. IN_DECL is an associated
11782 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11783 Issue error and warning messages under control of COMPLAIN. Note
11784 that we must be relatively non-tolerant of extensions here, in
11785 order to preserve conformance; if we allow substitutions that
11786 should not be allowed, we may allow argument deductions that should
11787 not succeed, and therefore report ambiguous overload situations
11788 where there are none. In theory, we could allow the substitution,
11789 but indicate that it should have failed, and allow our caller to
11790 make sure that the right thing happens, but we don't try to do this
11791 yet.
11792
11793 This function is used for dealing with types, decls and the like;
11794 for expressions, use tsubst_expr or tsubst_copy. */
11795
11796 tree
11797 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11798 {
11799 enum tree_code code;
11800 tree type, r = NULL_TREE;
11801
11802 if (t == NULL_TREE || t == error_mark_node
11803 || t == integer_type_node
11804 || t == void_type_node
11805 || t == char_type_node
11806 || t == unknown_type_node
11807 || TREE_CODE (t) == NAMESPACE_DECL
11808 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11809 return t;
11810
11811 if (DECL_P (t))
11812 return tsubst_decl (t, args, complain);
11813
11814 if (args == NULL_TREE)
11815 return t;
11816
11817 code = TREE_CODE (t);
11818
11819 if (code == IDENTIFIER_NODE)
11820 type = IDENTIFIER_TYPE_VALUE (t);
11821 else
11822 type = TREE_TYPE (t);
11823
11824 gcc_assert (type != unknown_type_node);
11825
11826 /* Reuse typedefs. We need to do this to handle dependent attributes,
11827 such as attribute aligned. */
11828 if (TYPE_P (t)
11829 && typedef_variant_p (t))
11830 {
11831 tree decl = TYPE_NAME (t);
11832
11833 if (alias_template_specialization_p (t))
11834 {
11835 /* DECL represents an alias template and we want to
11836 instantiate it. */
11837 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11838 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11839 r = instantiate_alias_template (tmpl, gen_args, complain);
11840 }
11841 else if (DECL_CLASS_SCOPE_P (decl)
11842 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11843 && uses_template_parms (DECL_CONTEXT (decl)))
11844 {
11845 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11846 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11847 r = retrieve_specialization (tmpl, gen_args, 0);
11848 }
11849 else if (DECL_FUNCTION_SCOPE_P (decl)
11850 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11851 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11852 r = retrieve_local_specialization (decl);
11853 else
11854 /* The typedef is from a non-template context. */
11855 return t;
11856
11857 if (r)
11858 {
11859 r = TREE_TYPE (r);
11860 r = cp_build_qualified_type_real
11861 (r, cp_type_quals (t) | cp_type_quals (r),
11862 complain | tf_ignore_bad_quals);
11863 return r;
11864 }
11865 else
11866 {
11867 /* We don't have an instantiation yet, so drop the typedef. */
11868 int quals = cp_type_quals (t);
11869 t = DECL_ORIGINAL_TYPE (decl);
11870 t = cp_build_qualified_type_real (t, quals,
11871 complain | tf_ignore_bad_quals);
11872 }
11873 }
11874
11875 if (type
11876 && code != TYPENAME_TYPE
11877 && code != TEMPLATE_TYPE_PARM
11878 && code != IDENTIFIER_NODE
11879 && code != FUNCTION_TYPE
11880 && code != METHOD_TYPE)
11881 type = tsubst (type, args, complain, in_decl);
11882 if (type == error_mark_node)
11883 return error_mark_node;
11884
11885 switch (code)
11886 {
11887 case RECORD_TYPE:
11888 case UNION_TYPE:
11889 case ENUMERAL_TYPE:
11890 return tsubst_aggr_type (t, args, complain, in_decl,
11891 /*entering_scope=*/0);
11892
11893 case ERROR_MARK:
11894 case IDENTIFIER_NODE:
11895 case VOID_TYPE:
11896 case REAL_TYPE:
11897 case COMPLEX_TYPE:
11898 case VECTOR_TYPE:
11899 case BOOLEAN_TYPE:
11900 case NULLPTR_TYPE:
11901 case LANG_TYPE:
11902 return t;
11903
11904 case INTEGER_TYPE:
11905 if (t == integer_type_node)
11906 return t;
11907
11908 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11909 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11910 return t;
11911
11912 {
11913 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11914
11915 max = tsubst_expr (omax, args, complain, in_decl,
11916 /*integral_constant_expression_p=*/false);
11917
11918 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11919 needed. */
11920 if (TREE_CODE (max) == NOP_EXPR
11921 && TREE_SIDE_EFFECTS (omax)
11922 && !TREE_TYPE (max))
11923 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11924
11925 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11926 with TREE_SIDE_EFFECTS that indicates this is not an integral
11927 constant expression. */
11928 if (processing_template_decl
11929 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11930 {
11931 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11932 TREE_SIDE_EFFECTS (max) = 1;
11933 }
11934
11935 return compute_array_index_type (NULL_TREE, max, complain);
11936 }
11937
11938 case TEMPLATE_TYPE_PARM:
11939 case TEMPLATE_TEMPLATE_PARM:
11940 case BOUND_TEMPLATE_TEMPLATE_PARM:
11941 case TEMPLATE_PARM_INDEX:
11942 {
11943 int idx;
11944 int level;
11945 int levels;
11946 tree arg = NULL_TREE;
11947
11948 r = NULL_TREE;
11949
11950 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11951 template_parm_level_and_index (t, &level, &idx);
11952
11953 levels = TMPL_ARGS_DEPTH (args);
11954 if (level <= levels)
11955 {
11956 arg = TMPL_ARG (args, level, idx);
11957
11958 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11959 {
11960 /* See through ARGUMENT_PACK_SELECT arguments. */
11961 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11962 /* If the selected argument is an expansion E, that most
11963 likely means we were called from
11964 gen_elem_of_pack_expansion_instantiation during the
11965 substituting of pack an argument pack (which Ith
11966 element is a pack expansion, where I is
11967 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11968 In this case, the Ith element resulting from this
11969 substituting is going to be a pack expansion, which
11970 pattern is the pattern of E. Let's return the
11971 pattern of E, and
11972 gen_elem_of_pack_expansion_instantiation will
11973 build the resulting pack expansion from it. */
11974 if (PACK_EXPANSION_P (arg))
11975 {
11976 /* Make sure we aren't throwing away arg info. */
11977 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
11978 arg = PACK_EXPANSION_PATTERN (arg);
11979 }
11980 }
11981 }
11982
11983 if (arg == error_mark_node)
11984 return error_mark_node;
11985 else if (arg != NULL_TREE)
11986 {
11987 if (ARGUMENT_PACK_P (arg))
11988 /* If ARG is an argument pack, we don't actually want to
11989 perform a substitution here, because substitutions
11990 for argument packs are only done
11991 element-by-element. We can get to this point when
11992 substituting the type of a non-type template
11993 parameter pack, when that type actually contains
11994 template parameter packs from an outer template, e.g.,
11995
11996 template<typename... Types> struct A {
11997 template<Types... Values> struct B { };
11998 }; */
11999 return t;
12000
12001 if (code == TEMPLATE_TYPE_PARM)
12002 {
12003 int quals;
12004 gcc_assert (TYPE_P (arg));
12005
12006 quals = cp_type_quals (arg) | cp_type_quals (t);
12007
12008 return cp_build_qualified_type_real
12009 (arg, quals, complain | tf_ignore_bad_quals);
12010 }
12011 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12012 {
12013 /* We are processing a type constructed from a
12014 template template parameter. */
12015 tree argvec = tsubst (TYPE_TI_ARGS (t),
12016 args, complain, in_decl);
12017 if (argvec == error_mark_node)
12018 return error_mark_node;
12019
12020 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12021 || TREE_CODE (arg) == TEMPLATE_DECL
12022 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12023
12024 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12025 /* Consider this code:
12026
12027 template <template <class> class Template>
12028 struct Internal {
12029 template <class Arg> using Bind = Template<Arg>;
12030 };
12031
12032 template <template <class> class Template, class Arg>
12033 using Instantiate = Template<Arg>; //#0
12034
12035 template <template <class> class Template,
12036 class Argument>
12037 using Bind =
12038 Instantiate<Internal<Template>::template Bind,
12039 Argument>; //#1
12040
12041 When #1 is parsed, the
12042 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12043 parameter `Template' in #0 matches the
12044 UNBOUND_CLASS_TEMPLATE representing the argument
12045 `Internal<Template>::template Bind'; We then want
12046 to assemble the type `Bind<Argument>' that can't
12047 be fully created right now, because
12048 `Internal<Template>' not being complete, the Bind
12049 template cannot be looked up in that context. So
12050 we need to "store" `Bind<Argument>' for later
12051 when the context of Bind becomes complete. Let's
12052 store that in a TYPENAME_TYPE. */
12053 return make_typename_type (TYPE_CONTEXT (arg),
12054 build_nt (TEMPLATE_ID_EXPR,
12055 TYPE_IDENTIFIER (arg),
12056 argvec),
12057 typename_type,
12058 complain);
12059
12060 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12061 are resolving nested-types in the signature of a
12062 member function templates. Otherwise ARG is a
12063 TEMPLATE_DECL and is the real template to be
12064 instantiated. */
12065 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12066 arg = TYPE_NAME (arg);
12067
12068 r = lookup_template_class (arg,
12069 argvec, in_decl,
12070 DECL_CONTEXT (arg),
12071 /*entering_scope=*/0,
12072 complain);
12073 return cp_build_qualified_type_real
12074 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12075 }
12076 else
12077 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12078 return convert_from_reference (unshare_expr (arg));
12079 }
12080
12081 if (level == 1)
12082 /* This can happen during the attempted tsubst'ing in
12083 unify. This means that we don't yet have any information
12084 about the template parameter in question. */
12085 return t;
12086
12087 /* Early in template argument deduction substitution, we don't
12088 want to reduce the level of 'auto', or it will be confused
12089 with a normal template parm in subsequent deduction. */
12090 if (is_auto (t) && (complain & tf_partial))
12091 return t;
12092
12093 /* If we get here, we must have been looking at a parm for a
12094 more deeply nested template. Make a new version of this
12095 template parameter, but with a lower level. */
12096 switch (code)
12097 {
12098 case TEMPLATE_TYPE_PARM:
12099 case TEMPLATE_TEMPLATE_PARM:
12100 case BOUND_TEMPLATE_TEMPLATE_PARM:
12101 if (cp_type_quals (t))
12102 {
12103 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12104 r = cp_build_qualified_type_real
12105 (r, cp_type_quals (t),
12106 complain | (code == TEMPLATE_TYPE_PARM
12107 ? tf_ignore_bad_quals : 0));
12108 }
12109 else
12110 {
12111 r = copy_type (t);
12112 TEMPLATE_TYPE_PARM_INDEX (r)
12113 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12114 r, levels, args, complain);
12115 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12116 TYPE_MAIN_VARIANT (r) = r;
12117 TYPE_POINTER_TO (r) = NULL_TREE;
12118 TYPE_REFERENCE_TO (r) = NULL_TREE;
12119
12120 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12121 /* We have reduced the level of the template
12122 template parameter, but not the levels of its
12123 template parameters, so canonical_type_parameter
12124 will not be able to find the canonical template
12125 template parameter for this level. Thus, we
12126 require structural equality checking to compare
12127 TEMPLATE_TEMPLATE_PARMs. */
12128 SET_TYPE_STRUCTURAL_EQUALITY (r);
12129 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12130 SET_TYPE_STRUCTURAL_EQUALITY (r);
12131 else
12132 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12133
12134 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12135 {
12136 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12137 complain, in_decl);
12138 if (argvec == error_mark_node)
12139 return error_mark_node;
12140
12141 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12142 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12143 }
12144 }
12145 break;
12146
12147 case TEMPLATE_PARM_INDEX:
12148 r = reduce_template_parm_level (t, type, levels, args, complain);
12149 break;
12150
12151 default:
12152 gcc_unreachable ();
12153 }
12154
12155 return r;
12156 }
12157
12158 case TREE_LIST:
12159 {
12160 tree purpose, value, chain;
12161
12162 if (t == void_list_node)
12163 return t;
12164
12165 purpose = TREE_PURPOSE (t);
12166 if (purpose)
12167 {
12168 purpose = tsubst (purpose, args, complain, in_decl);
12169 if (purpose == error_mark_node)
12170 return error_mark_node;
12171 }
12172 value = TREE_VALUE (t);
12173 if (value)
12174 {
12175 value = tsubst (value, args, complain, in_decl);
12176 if (value == error_mark_node)
12177 return error_mark_node;
12178 }
12179 chain = TREE_CHAIN (t);
12180 if (chain && chain != void_type_node)
12181 {
12182 chain = tsubst (chain, args, complain, in_decl);
12183 if (chain == error_mark_node)
12184 return error_mark_node;
12185 }
12186 if (purpose == TREE_PURPOSE (t)
12187 && value == TREE_VALUE (t)
12188 && chain == TREE_CHAIN (t))
12189 return t;
12190 return hash_tree_cons (purpose, value, chain);
12191 }
12192
12193 case TREE_BINFO:
12194 /* We should never be tsubsting a binfo. */
12195 gcc_unreachable ();
12196
12197 case TREE_VEC:
12198 /* A vector of template arguments. */
12199 gcc_assert (!type);
12200 return tsubst_template_args (t, args, complain, in_decl);
12201
12202 case POINTER_TYPE:
12203 case REFERENCE_TYPE:
12204 {
12205 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12206 return t;
12207
12208 /* [temp.deduct]
12209
12210 Type deduction may fail for any of the following
12211 reasons:
12212
12213 -- Attempting to create a pointer to reference type.
12214 -- Attempting to create a reference to a reference type or
12215 a reference to void.
12216
12217 Core issue 106 says that creating a reference to a reference
12218 during instantiation is no longer a cause for failure. We
12219 only enforce this check in strict C++98 mode. */
12220 if ((TREE_CODE (type) == REFERENCE_TYPE
12221 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12222 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12223 {
12224 static location_t last_loc;
12225
12226 /* We keep track of the last time we issued this error
12227 message to avoid spewing a ton of messages during a
12228 single bad template instantiation. */
12229 if (complain & tf_error
12230 && last_loc != input_location)
12231 {
12232 if (VOID_TYPE_P (type))
12233 error ("forming reference to void");
12234 else if (code == POINTER_TYPE)
12235 error ("forming pointer to reference type %qT", type);
12236 else
12237 error ("forming reference to reference type %qT", type);
12238 last_loc = input_location;
12239 }
12240
12241 return error_mark_node;
12242 }
12243 else if (TREE_CODE (type) == FUNCTION_TYPE
12244 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12245 || type_memfn_rqual (type) != REF_QUAL_NONE))
12246 {
12247 if (complain & tf_error)
12248 {
12249 if (code == POINTER_TYPE)
12250 error ("forming pointer to qualified function type %qT",
12251 type);
12252 else
12253 error ("forming reference to qualified function type %qT",
12254 type);
12255 }
12256 return error_mark_node;
12257 }
12258 else if (code == POINTER_TYPE)
12259 {
12260 r = build_pointer_type (type);
12261 if (TREE_CODE (type) == METHOD_TYPE)
12262 r = build_ptrmemfunc_type (r);
12263 }
12264 else if (TREE_CODE (type) == REFERENCE_TYPE)
12265 /* In C++0x, during template argument substitution, when there is an
12266 attempt to create a reference to a reference type, reference
12267 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12268
12269 "If a template-argument for a template-parameter T names a type
12270 that is a reference to a type A, an attempt to create the type
12271 'lvalue reference to cv T' creates the type 'lvalue reference to
12272 A,' while an attempt to create the type type rvalue reference to
12273 cv T' creates the type T"
12274 */
12275 r = cp_build_reference_type
12276 (TREE_TYPE (type),
12277 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12278 else
12279 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12280 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12281
12282 if (r != error_mark_node)
12283 /* Will this ever be needed for TYPE_..._TO values? */
12284 layout_type (r);
12285
12286 return r;
12287 }
12288 case OFFSET_TYPE:
12289 {
12290 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12291 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12292 {
12293 /* [temp.deduct]
12294
12295 Type deduction may fail for any of the following
12296 reasons:
12297
12298 -- Attempting to create "pointer to member of T" when T
12299 is not a class type. */
12300 if (complain & tf_error)
12301 error ("creating pointer to member of non-class type %qT", r);
12302 return error_mark_node;
12303 }
12304 if (TREE_CODE (type) == REFERENCE_TYPE)
12305 {
12306 if (complain & tf_error)
12307 error ("creating pointer to member reference type %qT", type);
12308 return error_mark_node;
12309 }
12310 if (VOID_TYPE_P (type))
12311 {
12312 if (complain & tf_error)
12313 error ("creating pointer to member of type void");
12314 return error_mark_node;
12315 }
12316 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12317 if (TREE_CODE (type) == FUNCTION_TYPE)
12318 {
12319 /* The type of the implicit object parameter gets its
12320 cv-qualifiers from the FUNCTION_TYPE. */
12321 tree memptr;
12322 tree method_type
12323 = build_memfn_type (type, r, type_memfn_quals (type),
12324 type_memfn_rqual (type));
12325 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12326 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12327 complain);
12328 }
12329 else
12330 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12331 cp_type_quals (t),
12332 complain);
12333 }
12334 case FUNCTION_TYPE:
12335 case METHOD_TYPE:
12336 {
12337 tree fntype;
12338 tree specs;
12339 fntype = tsubst_function_type (t, args, complain, in_decl);
12340 if (fntype == error_mark_node)
12341 return error_mark_node;
12342
12343 /* Substitute the exception specification. */
12344 specs = tsubst_exception_specification (t, args, complain,
12345 in_decl, /*defer_ok*/true);
12346 if (specs == error_mark_node)
12347 return error_mark_node;
12348 if (specs)
12349 fntype = build_exception_variant (fntype, specs);
12350 return fntype;
12351 }
12352 case ARRAY_TYPE:
12353 {
12354 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12355 if (domain == error_mark_node)
12356 return error_mark_node;
12357
12358 /* As an optimization, we avoid regenerating the array type if
12359 it will obviously be the same as T. */
12360 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12361 return t;
12362
12363 /* These checks should match the ones in create_array_type_for_decl.
12364
12365 [temp.deduct]
12366
12367 The deduction may fail for any of the following reasons:
12368
12369 -- Attempting to create an array with an element type that
12370 is void, a function type, or a reference type, or [DR337]
12371 an abstract class type. */
12372 if (VOID_TYPE_P (type)
12373 || TREE_CODE (type) == FUNCTION_TYPE
12374 || (TREE_CODE (type) == ARRAY_TYPE
12375 && TYPE_DOMAIN (type) == NULL_TREE)
12376 || TREE_CODE (type) == REFERENCE_TYPE)
12377 {
12378 if (complain & tf_error)
12379 error ("creating array of %qT", type);
12380 return error_mark_node;
12381 }
12382
12383 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12384 return error_mark_node;
12385
12386 r = build_cplus_array_type (type, domain);
12387
12388 if (TYPE_USER_ALIGN (t))
12389 {
12390 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12391 TYPE_USER_ALIGN (r) = 1;
12392 }
12393
12394 return r;
12395 }
12396
12397 case TYPENAME_TYPE:
12398 {
12399 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12400 in_decl, /*entering_scope=*/1);
12401 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12402 complain, in_decl);
12403
12404 if (ctx == error_mark_node || f == error_mark_node)
12405 return error_mark_node;
12406
12407 if (!MAYBE_CLASS_TYPE_P (ctx))
12408 {
12409 if (complain & tf_error)
12410 error ("%qT is not a class, struct, or union type", ctx);
12411 return error_mark_node;
12412 }
12413 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12414 {
12415 /* Normally, make_typename_type does not require that the CTX
12416 have complete type in order to allow things like:
12417
12418 template <class T> struct S { typename S<T>::X Y; };
12419
12420 But, such constructs have already been resolved by this
12421 point, so here CTX really should have complete type, unless
12422 it's a partial instantiation. */
12423 ctx = complete_type (ctx);
12424 if (!COMPLETE_TYPE_P (ctx))
12425 {
12426 if (complain & tf_error)
12427 cxx_incomplete_type_error (NULL_TREE, ctx);
12428 return error_mark_node;
12429 }
12430 }
12431
12432 f = make_typename_type (ctx, f, typename_type,
12433 complain | tf_keep_type_decl);
12434 if (f == error_mark_node)
12435 return f;
12436 if (TREE_CODE (f) == TYPE_DECL)
12437 {
12438 complain |= tf_ignore_bad_quals;
12439 f = TREE_TYPE (f);
12440 }
12441
12442 if (TREE_CODE (f) != TYPENAME_TYPE)
12443 {
12444 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12445 {
12446 if (complain & tf_error)
12447 error ("%qT resolves to %qT, which is not an enumeration type",
12448 t, f);
12449 else
12450 return error_mark_node;
12451 }
12452 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12453 {
12454 if (complain & tf_error)
12455 error ("%qT resolves to %qT, which is is not a class type",
12456 t, f);
12457 else
12458 return error_mark_node;
12459 }
12460 }
12461
12462 return cp_build_qualified_type_real
12463 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12464 }
12465
12466 case UNBOUND_CLASS_TEMPLATE:
12467 {
12468 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12469 in_decl, /*entering_scope=*/1);
12470 tree name = TYPE_IDENTIFIER (t);
12471 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12472
12473 if (ctx == error_mark_node || name == error_mark_node)
12474 return error_mark_node;
12475
12476 if (parm_list)
12477 parm_list = tsubst_template_parms (parm_list, args, complain);
12478 return make_unbound_class_template (ctx, name, parm_list, complain);
12479 }
12480
12481 case TYPEOF_TYPE:
12482 {
12483 tree type;
12484
12485 ++cp_unevaluated_operand;
12486 ++c_inhibit_evaluation_warnings;
12487
12488 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12489 complain, in_decl,
12490 /*integral_constant_expression_p=*/false);
12491
12492 --cp_unevaluated_operand;
12493 --c_inhibit_evaluation_warnings;
12494
12495 type = finish_typeof (type);
12496 return cp_build_qualified_type_real (type,
12497 cp_type_quals (t)
12498 | cp_type_quals (type),
12499 complain);
12500 }
12501
12502 case DECLTYPE_TYPE:
12503 {
12504 tree type;
12505
12506 ++cp_unevaluated_operand;
12507 ++c_inhibit_evaluation_warnings;
12508
12509 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12510 complain|tf_decltype, in_decl,
12511 /*function_p*/false,
12512 /*integral_constant_expression*/false);
12513
12514 --cp_unevaluated_operand;
12515 --c_inhibit_evaluation_warnings;
12516
12517 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12518 type = lambda_capture_field_type (type,
12519 DECLTYPE_FOR_INIT_CAPTURE (t));
12520 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12521 type = lambda_proxy_type (type);
12522 else
12523 {
12524 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12525 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12526 && EXPR_P (type))
12527 /* In a template ~id could be either a complement expression
12528 or an unqualified-id naming a destructor; if instantiating
12529 it produces an expression, it's not an id-expression or
12530 member access. */
12531 id = false;
12532 type = finish_decltype_type (type, id, complain);
12533 }
12534 return cp_build_qualified_type_real (type,
12535 cp_type_quals (t)
12536 | cp_type_quals (type),
12537 complain | tf_ignore_bad_quals);
12538 }
12539
12540 case UNDERLYING_TYPE:
12541 {
12542 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12543 complain, in_decl);
12544 return finish_underlying_type (type);
12545 }
12546
12547 case TYPE_ARGUMENT_PACK:
12548 case NONTYPE_ARGUMENT_PACK:
12549 {
12550 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12551 tree packed_out =
12552 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12553 args,
12554 complain,
12555 in_decl);
12556 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12557
12558 /* For template nontype argument packs, also substitute into
12559 the type. */
12560 if (code == NONTYPE_ARGUMENT_PACK)
12561 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12562
12563 return r;
12564 }
12565 break;
12566
12567 case VOID_CST:
12568 case INTEGER_CST:
12569 case REAL_CST:
12570 case STRING_CST:
12571 case PLUS_EXPR:
12572 case MINUS_EXPR:
12573 case NEGATE_EXPR:
12574 case NOP_EXPR:
12575 case INDIRECT_REF:
12576 case ADDR_EXPR:
12577 case CALL_EXPR:
12578 case ARRAY_REF:
12579 case SCOPE_REF:
12580 /* We should use one of the expression tsubsts for these codes. */
12581 gcc_unreachable ();
12582
12583 default:
12584 sorry ("use of %qs in template", get_tree_code_name (code));
12585 return error_mark_node;
12586 }
12587 }
12588
12589 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12590 type of the expression on the left-hand side of the "." or "->"
12591 operator. */
12592
12593 static tree
12594 tsubst_baselink (tree baselink, tree object_type,
12595 tree args, tsubst_flags_t complain, tree in_decl)
12596 {
12597 tree name;
12598 tree qualifying_scope;
12599 tree fns;
12600 tree optype;
12601 tree template_args = 0;
12602 bool template_id_p = false;
12603 bool qualified = BASELINK_QUALIFIED_P (baselink);
12604
12605 /* A baselink indicates a function from a base class. Both the
12606 BASELINK_ACCESS_BINFO and the base class referenced may
12607 indicate bases of the template class, rather than the
12608 instantiated class. In addition, lookups that were not
12609 ambiguous before may be ambiguous now. Therefore, we perform
12610 the lookup again. */
12611 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12612 qualifying_scope = tsubst (qualifying_scope, args,
12613 complain, in_decl);
12614 fns = BASELINK_FUNCTIONS (baselink);
12615 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12616 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12617 {
12618 template_id_p = true;
12619 template_args = TREE_OPERAND (fns, 1);
12620 fns = TREE_OPERAND (fns, 0);
12621 if (template_args)
12622 template_args = tsubst_template_args (template_args, args,
12623 complain, in_decl);
12624 }
12625 name = DECL_NAME (get_first_fn (fns));
12626 if (IDENTIFIER_TYPENAME_P (name))
12627 name = mangle_conv_op_name_for_type (optype);
12628 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12629 if (!baselink)
12630 return error_mark_node;
12631
12632 /* If lookup found a single function, mark it as used at this
12633 point. (If it lookup found multiple functions the one selected
12634 later by overload resolution will be marked as used at that
12635 point.) */
12636 if (BASELINK_P (baselink))
12637 fns = BASELINK_FUNCTIONS (baselink);
12638 if (!template_id_p && !really_overloaded_fn (fns)
12639 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
12640 return error_mark_node;
12641
12642 /* Add back the template arguments, if present. */
12643 if (BASELINK_P (baselink) && template_id_p)
12644 BASELINK_FUNCTIONS (baselink)
12645 = build_nt (TEMPLATE_ID_EXPR,
12646 BASELINK_FUNCTIONS (baselink),
12647 template_args);
12648 /* Update the conversion operator type. */
12649 BASELINK_OPTYPE (baselink) = optype;
12650
12651 if (!object_type)
12652 object_type = current_class_type;
12653
12654 if (qualified)
12655 baselink = adjust_result_of_qualified_name_lookup (baselink,
12656 qualifying_scope,
12657 object_type);
12658 return baselink;
12659 }
12660
12661 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12662 true if the qualified-id will be a postfix-expression in-and-of
12663 itself; false if more of the postfix-expression follows the
12664 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12665 of "&". */
12666
12667 static tree
12668 tsubst_qualified_id (tree qualified_id, tree args,
12669 tsubst_flags_t complain, tree in_decl,
12670 bool done, bool address_p)
12671 {
12672 tree expr;
12673 tree scope;
12674 tree name;
12675 bool is_template;
12676 tree template_args;
12677 location_t loc = UNKNOWN_LOCATION;
12678
12679 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12680
12681 /* Figure out what name to look up. */
12682 name = TREE_OPERAND (qualified_id, 1);
12683 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12684 {
12685 is_template = true;
12686 loc = EXPR_LOCATION (name);
12687 template_args = TREE_OPERAND (name, 1);
12688 if (template_args)
12689 template_args = tsubst_template_args (template_args, args,
12690 complain, in_decl);
12691 name = TREE_OPERAND (name, 0);
12692 }
12693 else
12694 {
12695 is_template = false;
12696 template_args = NULL_TREE;
12697 }
12698
12699 /* Substitute into the qualifying scope. When there are no ARGS, we
12700 are just trying to simplify a non-dependent expression. In that
12701 case the qualifying scope may be dependent, and, in any case,
12702 substituting will not help. */
12703 scope = TREE_OPERAND (qualified_id, 0);
12704 if (args)
12705 {
12706 scope = tsubst (scope, args, complain, in_decl);
12707 expr = tsubst_copy (name, args, complain, in_decl);
12708 }
12709 else
12710 expr = name;
12711
12712 if (dependent_scope_p (scope))
12713 {
12714 if (is_template)
12715 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12716 return build_qualified_name (NULL_TREE, scope, expr,
12717 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12718 }
12719
12720 if (!BASELINK_P (name) && !DECL_P (expr))
12721 {
12722 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12723 {
12724 /* A BIT_NOT_EXPR is used to represent a destructor. */
12725 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12726 {
12727 error ("qualifying type %qT does not match destructor name ~%qT",
12728 scope, TREE_OPERAND (expr, 0));
12729 expr = error_mark_node;
12730 }
12731 else
12732 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12733 /*is_type_p=*/0, false);
12734 }
12735 else
12736 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12737 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12738 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12739 {
12740 if (complain & tf_error)
12741 {
12742 error ("dependent-name %qE is parsed as a non-type, but "
12743 "instantiation yields a type", qualified_id);
12744 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12745 }
12746 return error_mark_node;
12747 }
12748 }
12749
12750 if (DECL_P (expr))
12751 {
12752 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12753 scope);
12754 /* Remember that there was a reference to this entity. */
12755 if (!mark_used (expr, complain) && !(complain & tf_error))
12756 return error_mark_node;
12757 }
12758
12759 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12760 {
12761 if (complain & tf_error)
12762 qualified_name_lookup_error (scope,
12763 TREE_OPERAND (qualified_id, 1),
12764 expr, input_location);
12765 return error_mark_node;
12766 }
12767
12768 if (is_template)
12769 expr = lookup_template_function (expr, template_args);
12770
12771 if (expr == error_mark_node && complain & tf_error)
12772 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12773 expr, input_location);
12774 else if (TYPE_P (scope))
12775 {
12776 expr = (adjust_result_of_qualified_name_lookup
12777 (expr, scope, current_nonlambda_class_type ()));
12778 expr = (finish_qualified_id_expr
12779 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12780 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12781 /*template_arg_p=*/false, complain));
12782 }
12783
12784 /* Expressions do not generally have reference type. */
12785 if (TREE_CODE (expr) != SCOPE_REF
12786 /* However, if we're about to form a pointer-to-member, we just
12787 want the referenced member referenced. */
12788 && TREE_CODE (expr) != OFFSET_REF)
12789 expr = convert_from_reference (expr);
12790
12791 return expr;
12792 }
12793
12794 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12795 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12796 for tsubst. */
12797
12798 static tree
12799 tsubst_init (tree init, tree decl, tree args,
12800 tsubst_flags_t complain, tree in_decl)
12801 {
12802 if (!init)
12803 return NULL_TREE;
12804
12805 init = tsubst_expr (init, args, complain, in_decl, false);
12806
12807 if (!init)
12808 {
12809 /* If we had an initializer but it
12810 instantiated to nothing,
12811 value-initialize the object. This will
12812 only occur when the initializer was a
12813 pack expansion where the parameter packs
12814 used in that expansion were of length
12815 zero. */
12816 init = build_value_init (TREE_TYPE (decl),
12817 complain);
12818 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12819 init = get_target_expr_sfinae (init, complain);
12820 }
12821
12822 return init;
12823 }
12824
12825 /* Like tsubst, but deals with expressions. This function just replaces
12826 template parms; to finish processing the resultant expression, use
12827 tsubst_copy_and_build or tsubst_expr. */
12828
12829 static tree
12830 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12831 {
12832 enum tree_code code;
12833 tree r;
12834
12835 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12836 return t;
12837
12838 code = TREE_CODE (t);
12839
12840 switch (code)
12841 {
12842 case PARM_DECL:
12843 r = retrieve_local_specialization (t);
12844
12845 if (r == NULL_TREE)
12846 {
12847 /* We get here for a use of 'this' in an NSDMI. */
12848 if (DECL_NAME (t) == this_identifier
12849 && current_function_decl
12850 && DECL_CONSTRUCTOR_P (current_function_decl))
12851 return current_class_ptr;
12852
12853 /* This can happen for a parameter name used later in a function
12854 declaration (such as in a late-specified return type). Just
12855 make a dummy decl, since it's only used for its type. */
12856 gcc_assert (cp_unevaluated_operand != 0);
12857 r = tsubst_decl (t, args, complain);
12858 /* Give it the template pattern as its context; its true context
12859 hasn't been instantiated yet and this is good enough for
12860 mangling. */
12861 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12862 }
12863
12864 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12865 r = ARGUMENT_PACK_SELECT_ARG (r);
12866 if (!mark_used (r, complain) && !(complain & tf_error))
12867 return error_mark_node;
12868 return r;
12869
12870 case CONST_DECL:
12871 {
12872 tree enum_type;
12873 tree v;
12874
12875 if (DECL_TEMPLATE_PARM_P (t))
12876 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12877 /* There is no need to substitute into namespace-scope
12878 enumerators. */
12879 if (DECL_NAMESPACE_SCOPE_P (t))
12880 return t;
12881 /* If ARGS is NULL, then T is known to be non-dependent. */
12882 if (args == NULL_TREE)
12883 return scalar_constant_value (t);
12884
12885 /* Unfortunately, we cannot just call lookup_name here.
12886 Consider:
12887
12888 template <int I> int f() {
12889 enum E { a = I };
12890 struct S { void g() { E e = a; } };
12891 };
12892
12893 When we instantiate f<7>::S::g(), say, lookup_name is not
12894 clever enough to find f<7>::a. */
12895 enum_type
12896 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12897 /*entering_scope=*/0);
12898
12899 for (v = TYPE_VALUES (enum_type);
12900 v != NULL_TREE;
12901 v = TREE_CHAIN (v))
12902 if (TREE_PURPOSE (v) == DECL_NAME (t))
12903 return TREE_VALUE (v);
12904
12905 /* We didn't find the name. That should never happen; if
12906 name-lookup found it during preliminary parsing, we
12907 should find it again here during instantiation. */
12908 gcc_unreachable ();
12909 }
12910 return t;
12911
12912 case FIELD_DECL:
12913 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12914 {
12915 /* Check for a local specialization set up by
12916 tsubst_pack_expansion. */
12917 if (tree r = retrieve_local_specialization (t))
12918 {
12919 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12920 r = ARGUMENT_PACK_SELECT_ARG (r);
12921 return r;
12922 }
12923
12924 /* When retrieving a capture pack from a generic lambda, remove the
12925 lambda call op's own template argument list from ARGS. Only the
12926 template arguments active for the closure type should be used to
12927 retrieve the pack specialization. */
12928 if (LAMBDA_FUNCTION_P (current_function_decl)
12929 && (template_class_depth (DECL_CONTEXT (t))
12930 != TMPL_ARGS_DEPTH (args)))
12931 args = strip_innermost_template_args (args, 1);
12932
12933 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12934 tsubst_decl put in the hash table. */
12935 return retrieve_specialization (t, args, 0);
12936 }
12937
12938 if (DECL_CONTEXT (t))
12939 {
12940 tree ctx;
12941
12942 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12943 /*entering_scope=*/1);
12944 if (ctx != DECL_CONTEXT (t))
12945 {
12946 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12947 if (!r)
12948 {
12949 if (complain & tf_error)
12950 error ("using invalid field %qD", t);
12951 return error_mark_node;
12952 }
12953 return r;
12954 }
12955 }
12956
12957 return t;
12958
12959 case VAR_DECL:
12960 case FUNCTION_DECL:
12961 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12962 r = tsubst (t, args, complain, in_decl);
12963 else if (local_variable_p (t))
12964 {
12965 r = retrieve_local_specialization (t);
12966 if (r == NULL_TREE)
12967 {
12968 /* First try name lookup to find the instantiation. */
12969 r = lookup_name (DECL_NAME (t));
12970 if (r)
12971 {
12972 /* Make sure that the one we found is the one we want. */
12973 tree ctx = tsubst (DECL_CONTEXT (t), args,
12974 complain, in_decl);
12975 if (ctx != DECL_CONTEXT (r))
12976 r = NULL_TREE;
12977 }
12978
12979 if (r)
12980 /* OK */;
12981 else
12982 {
12983 /* This can happen for a variable used in a
12984 late-specified return type of a local lambda, or for a
12985 local static or constant. Building a new VAR_DECL
12986 should be OK in all those cases. */
12987 r = tsubst_decl (t, args, complain);
12988 if (decl_maybe_constant_var_p (r))
12989 {
12990 /* We can't call cp_finish_decl, so handle the
12991 initializer by hand. */
12992 tree init = tsubst_init (DECL_INITIAL (t), r, args,
12993 complain, in_decl);
12994 if (!processing_template_decl)
12995 init = maybe_constant_init (init);
12996 if (processing_template_decl
12997 ? potential_constant_expression (init)
12998 : reduced_constant_expression_p (init))
12999 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13000 = TREE_CONSTANT (r) = true;
13001 DECL_INITIAL (r) = init;
13002 }
13003 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13004 || decl_constant_var_p (r)
13005 || errorcount || sorrycount);
13006 if (!processing_template_decl)
13007 {
13008 if (TREE_STATIC (r))
13009 rest_of_decl_compilation (r, toplevel_bindings_p (),
13010 at_eof);
13011 else if (decl_constant_var_p (r))
13012 /* A use of a local constant decays to its value.
13013 FIXME update for core DR 696. */
13014 r = scalar_constant_value (r);
13015 }
13016 }
13017 /* Remember this for subsequent uses. */
13018 if (local_specializations)
13019 register_local_specialization (r, t);
13020 }
13021 }
13022 else
13023 r = t;
13024 if (!mark_used (r, complain) && !(complain & tf_error))
13025 return error_mark_node;
13026 return r;
13027
13028 case NAMESPACE_DECL:
13029 return t;
13030
13031 case OVERLOAD:
13032 /* An OVERLOAD will always be a non-dependent overload set; an
13033 overload set from function scope will just be represented with an
13034 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13035 gcc_assert (!uses_template_parms (t));
13036 return t;
13037
13038 case BASELINK:
13039 return tsubst_baselink (t, current_nonlambda_class_type (),
13040 args, complain, in_decl);
13041
13042 case TEMPLATE_DECL:
13043 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13044 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13045 args, complain, in_decl);
13046 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13047 return tsubst (t, args, complain, in_decl);
13048 else if (DECL_CLASS_SCOPE_P (t)
13049 && uses_template_parms (DECL_CONTEXT (t)))
13050 {
13051 /* Template template argument like the following example need
13052 special treatment:
13053
13054 template <template <class> class TT> struct C {};
13055 template <class T> struct D {
13056 template <class U> struct E {};
13057 C<E> c; // #1
13058 };
13059 D<int> d; // #2
13060
13061 We are processing the template argument `E' in #1 for
13062 the template instantiation #2. Originally, `E' is a
13063 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13064 have to substitute this with one having context `D<int>'. */
13065
13066 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13067 return lookup_field (context, DECL_NAME(t), 0, false);
13068 }
13069 else
13070 /* Ordinary template template argument. */
13071 return t;
13072
13073 case CAST_EXPR:
13074 case REINTERPRET_CAST_EXPR:
13075 case CONST_CAST_EXPR:
13076 case STATIC_CAST_EXPR:
13077 case DYNAMIC_CAST_EXPR:
13078 case IMPLICIT_CONV_EXPR:
13079 case CONVERT_EXPR:
13080 case NOP_EXPR:
13081 {
13082 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13083 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13084 return build1 (code, type, op0);
13085 }
13086
13087 case SIZEOF_EXPR:
13088 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13089 {
13090
13091 tree expanded, op = TREE_OPERAND (t, 0);
13092 int len = 0;
13093
13094 if (SIZEOF_EXPR_TYPE_P (t))
13095 op = TREE_TYPE (op);
13096
13097 ++cp_unevaluated_operand;
13098 ++c_inhibit_evaluation_warnings;
13099 /* We only want to compute the number of arguments. */
13100 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13101 --cp_unevaluated_operand;
13102 --c_inhibit_evaluation_warnings;
13103
13104 if (TREE_CODE (expanded) == TREE_VEC)
13105 len = TREE_VEC_LENGTH (expanded);
13106
13107 if (expanded == error_mark_node)
13108 return error_mark_node;
13109 else if (PACK_EXPANSION_P (expanded)
13110 || (TREE_CODE (expanded) == TREE_VEC
13111 && len > 0
13112 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13113 {
13114 if (TREE_CODE (expanded) == TREE_VEC)
13115 expanded = TREE_VEC_ELT (expanded, len - 1);
13116
13117 if (TYPE_P (expanded))
13118 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13119 complain & tf_error);
13120 else
13121 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13122 complain & tf_error);
13123 }
13124 else
13125 return build_int_cst (size_type_node, len);
13126 }
13127 if (SIZEOF_EXPR_TYPE_P (t))
13128 {
13129 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13130 args, complain, in_decl);
13131 r = build1 (NOP_EXPR, r, error_mark_node);
13132 r = build1 (SIZEOF_EXPR,
13133 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13134 SIZEOF_EXPR_TYPE_P (r) = 1;
13135 return r;
13136 }
13137 /* Fall through */
13138
13139 case INDIRECT_REF:
13140 case NEGATE_EXPR:
13141 case TRUTH_NOT_EXPR:
13142 case BIT_NOT_EXPR:
13143 case ADDR_EXPR:
13144 case UNARY_PLUS_EXPR: /* Unary + */
13145 case ALIGNOF_EXPR:
13146 case AT_ENCODE_EXPR:
13147 case ARROW_EXPR:
13148 case THROW_EXPR:
13149 case TYPEID_EXPR:
13150 case REALPART_EXPR:
13151 case IMAGPART_EXPR:
13152 case PAREN_EXPR:
13153 {
13154 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13155 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13156 return build1 (code, type, op0);
13157 }
13158
13159 case COMPONENT_REF:
13160 {
13161 tree object;
13162 tree name;
13163
13164 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13165 name = TREE_OPERAND (t, 1);
13166 if (TREE_CODE (name) == BIT_NOT_EXPR)
13167 {
13168 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13169 complain, in_decl);
13170 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13171 }
13172 else if (TREE_CODE (name) == SCOPE_REF
13173 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
13174 {
13175 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
13176 complain, in_decl);
13177 name = TREE_OPERAND (name, 1);
13178 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13179 complain, in_decl);
13180 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13181 name = build_qualified_name (/*type=*/NULL_TREE,
13182 base, name,
13183 /*template_p=*/false);
13184 }
13185 else if (BASELINK_P (name))
13186 name = tsubst_baselink (name,
13187 non_reference (TREE_TYPE (object)),
13188 args, complain,
13189 in_decl);
13190 else
13191 name = tsubst_copy (name, args, complain, in_decl);
13192 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
13193 }
13194
13195 case PLUS_EXPR:
13196 case MINUS_EXPR:
13197 case MULT_EXPR:
13198 case TRUNC_DIV_EXPR:
13199 case CEIL_DIV_EXPR:
13200 case FLOOR_DIV_EXPR:
13201 case ROUND_DIV_EXPR:
13202 case EXACT_DIV_EXPR:
13203 case BIT_AND_EXPR:
13204 case BIT_IOR_EXPR:
13205 case BIT_XOR_EXPR:
13206 case TRUNC_MOD_EXPR:
13207 case FLOOR_MOD_EXPR:
13208 case TRUTH_ANDIF_EXPR:
13209 case TRUTH_ORIF_EXPR:
13210 case TRUTH_AND_EXPR:
13211 case TRUTH_OR_EXPR:
13212 case RSHIFT_EXPR:
13213 case LSHIFT_EXPR:
13214 case RROTATE_EXPR:
13215 case LROTATE_EXPR:
13216 case EQ_EXPR:
13217 case NE_EXPR:
13218 case MAX_EXPR:
13219 case MIN_EXPR:
13220 case LE_EXPR:
13221 case GE_EXPR:
13222 case LT_EXPR:
13223 case GT_EXPR:
13224 case COMPOUND_EXPR:
13225 case DOTSTAR_EXPR:
13226 case MEMBER_REF:
13227 case PREDECREMENT_EXPR:
13228 case PREINCREMENT_EXPR:
13229 case POSTDECREMENT_EXPR:
13230 case POSTINCREMENT_EXPR:
13231 {
13232 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13233 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13234 return build_nt (code, op0, op1);
13235 }
13236
13237 case SCOPE_REF:
13238 {
13239 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13240 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13241 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
13242 QUALIFIED_NAME_IS_TEMPLATE (t));
13243 }
13244
13245 case ARRAY_REF:
13246 {
13247 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13248 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13249 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
13250 }
13251
13252 case CALL_EXPR:
13253 {
13254 int n = VL_EXP_OPERAND_LENGTH (t);
13255 tree result = build_vl_exp (CALL_EXPR, n);
13256 int i;
13257 for (i = 0; i < n; i++)
13258 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13259 complain, in_decl);
13260 return result;
13261 }
13262
13263 case COND_EXPR:
13264 case MODOP_EXPR:
13265 case PSEUDO_DTOR_EXPR:
13266 case VEC_PERM_EXPR:
13267 {
13268 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13269 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13270 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13271 r = build_nt (code, op0, op1, op2);
13272 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13273 return r;
13274 }
13275
13276 case NEW_EXPR:
13277 {
13278 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13279 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13280 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13281 r = build_nt (code, op0, op1, op2);
13282 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13283 return r;
13284 }
13285
13286 case DELETE_EXPR:
13287 {
13288 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13289 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13290 r = build_nt (code, op0, op1);
13291 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13292 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13293 return r;
13294 }
13295
13296 case TEMPLATE_ID_EXPR:
13297 {
13298 /* Substituted template arguments */
13299 tree fn = TREE_OPERAND (t, 0);
13300 tree targs = TREE_OPERAND (t, 1);
13301
13302 fn = tsubst_copy (fn, args, complain, in_decl);
13303 if (targs)
13304 targs = tsubst_template_args (targs, args, complain, in_decl);
13305
13306 return lookup_template_function (fn, targs);
13307 }
13308
13309 case TREE_LIST:
13310 {
13311 tree purpose, value, chain;
13312
13313 if (t == void_list_node)
13314 return t;
13315
13316 purpose = TREE_PURPOSE (t);
13317 if (purpose)
13318 purpose = tsubst_copy (purpose, args, complain, in_decl);
13319 value = TREE_VALUE (t);
13320 if (value)
13321 value = tsubst_copy (value, args, complain, in_decl);
13322 chain = TREE_CHAIN (t);
13323 if (chain && chain != void_type_node)
13324 chain = tsubst_copy (chain, args, complain, in_decl);
13325 if (purpose == TREE_PURPOSE (t)
13326 && value == TREE_VALUE (t)
13327 && chain == TREE_CHAIN (t))
13328 return t;
13329 return tree_cons (purpose, value, chain);
13330 }
13331
13332 case RECORD_TYPE:
13333 case UNION_TYPE:
13334 case ENUMERAL_TYPE:
13335 case INTEGER_TYPE:
13336 case TEMPLATE_TYPE_PARM:
13337 case TEMPLATE_TEMPLATE_PARM:
13338 case BOUND_TEMPLATE_TEMPLATE_PARM:
13339 case TEMPLATE_PARM_INDEX:
13340 case POINTER_TYPE:
13341 case REFERENCE_TYPE:
13342 case OFFSET_TYPE:
13343 case FUNCTION_TYPE:
13344 case METHOD_TYPE:
13345 case ARRAY_TYPE:
13346 case TYPENAME_TYPE:
13347 case UNBOUND_CLASS_TEMPLATE:
13348 case TYPEOF_TYPE:
13349 case DECLTYPE_TYPE:
13350 case TYPE_DECL:
13351 return tsubst (t, args, complain, in_decl);
13352
13353 case USING_DECL:
13354 t = DECL_NAME (t);
13355 /* Fall through. */
13356 case IDENTIFIER_NODE:
13357 if (IDENTIFIER_TYPENAME_P (t))
13358 {
13359 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13360 return mangle_conv_op_name_for_type (new_type);
13361 }
13362 else
13363 return t;
13364
13365 case CONSTRUCTOR:
13366 /* This is handled by tsubst_copy_and_build. */
13367 gcc_unreachable ();
13368
13369 case VA_ARG_EXPR:
13370 {
13371 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13372 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13373 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
13374 }
13375
13376 case CLEANUP_POINT_EXPR:
13377 /* We shouldn't have built any of these during initial template
13378 generation. Instead, they should be built during instantiation
13379 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13380 gcc_unreachable ();
13381
13382 case OFFSET_REF:
13383 {
13384 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13385 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13386 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13387 r = build2 (code, type, op0, op1);
13388 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13389 if (!mark_used (TREE_OPERAND (r, 1), complain)
13390 && !(complain & tf_error))
13391 return error_mark_node;
13392 return r;
13393 }
13394
13395 case EXPR_PACK_EXPANSION:
13396 error ("invalid use of pack expansion expression");
13397 return error_mark_node;
13398
13399 case NONTYPE_ARGUMENT_PACK:
13400 error ("use %<...%> to expand argument pack");
13401 return error_mark_node;
13402
13403 case VOID_CST:
13404 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
13405 return t;
13406
13407 case INTEGER_CST:
13408 case REAL_CST:
13409 case STRING_CST:
13410 case COMPLEX_CST:
13411 {
13412 /* Instantiate any typedefs in the type. */
13413 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13414 r = fold_convert (type, t);
13415 gcc_assert (TREE_CODE (r) == code);
13416 return r;
13417 }
13418
13419 case PTRMEM_CST:
13420 /* These can sometimes show up in a partial instantiation, but never
13421 involve template parms. */
13422 gcc_assert (!uses_template_parms (t));
13423 return t;
13424
13425 default:
13426 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13427 gcc_checking_assert (false);
13428 return t;
13429 }
13430 }
13431
13432 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13433
13434 static tree
13435 tsubst_omp_clauses (tree clauses, bool declare_simd,
13436 tree args, tsubst_flags_t complain, tree in_decl)
13437 {
13438 tree new_clauses = NULL, nc, oc;
13439
13440 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13441 {
13442 nc = copy_node (oc);
13443 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13444 new_clauses = nc;
13445
13446 switch (OMP_CLAUSE_CODE (nc))
13447 {
13448 case OMP_CLAUSE_LASTPRIVATE:
13449 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13450 {
13451 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13452 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13453 in_decl, /*integral_constant_expression_p=*/false);
13454 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13455 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13456 }
13457 /* FALLTHRU */
13458 case OMP_CLAUSE_PRIVATE:
13459 case OMP_CLAUSE_SHARED:
13460 case OMP_CLAUSE_FIRSTPRIVATE:
13461 case OMP_CLAUSE_COPYIN:
13462 case OMP_CLAUSE_COPYPRIVATE:
13463 case OMP_CLAUSE_IF:
13464 case OMP_CLAUSE_NUM_THREADS:
13465 case OMP_CLAUSE_SCHEDULE:
13466 case OMP_CLAUSE_COLLAPSE:
13467 case OMP_CLAUSE_FINAL:
13468 case OMP_CLAUSE_DEPEND:
13469 case OMP_CLAUSE_FROM:
13470 case OMP_CLAUSE_TO:
13471 case OMP_CLAUSE_UNIFORM:
13472 case OMP_CLAUSE_MAP:
13473 case OMP_CLAUSE_DEVICE:
13474 case OMP_CLAUSE_DIST_SCHEDULE:
13475 case OMP_CLAUSE_NUM_TEAMS:
13476 case OMP_CLAUSE_THREAD_LIMIT:
13477 case OMP_CLAUSE_SAFELEN:
13478 case OMP_CLAUSE_SIMDLEN:
13479 OMP_CLAUSE_OPERAND (nc, 0)
13480 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13481 in_decl, /*integral_constant_expression_p=*/false);
13482 break;
13483 case OMP_CLAUSE_REDUCTION:
13484 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13485 {
13486 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13487 if (TREE_CODE (placeholder) == SCOPE_REF)
13488 {
13489 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13490 complain, in_decl);
13491 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13492 = build_qualified_name (NULL_TREE, scope,
13493 TREE_OPERAND (placeholder, 1),
13494 false);
13495 }
13496 else
13497 gcc_assert (identifier_p (placeholder));
13498 }
13499 OMP_CLAUSE_OPERAND (nc, 0)
13500 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13501 in_decl, /*integral_constant_expression_p=*/false);
13502 break;
13503 case OMP_CLAUSE_LINEAR:
13504 case OMP_CLAUSE_ALIGNED:
13505 OMP_CLAUSE_OPERAND (nc, 0)
13506 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13507 in_decl, /*integral_constant_expression_p=*/false);
13508 OMP_CLAUSE_OPERAND (nc, 1)
13509 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13510 in_decl, /*integral_constant_expression_p=*/false);
13511 break;
13512
13513 case OMP_CLAUSE_NOWAIT:
13514 case OMP_CLAUSE_ORDERED:
13515 case OMP_CLAUSE_DEFAULT:
13516 case OMP_CLAUSE_UNTIED:
13517 case OMP_CLAUSE_MERGEABLE:
13518 case OMP_CLAUSE_INBRANCH:
13519 case OMP_CLAUSE_NOTINBRANCH:
13520 case OMP_CLAUSE_PROC_BIND:
13521 case OMP_CLAUSE_FOR:
13522 case OMP_CLAUSE_PARALLEL:
13523 case OMP_CLAUSE_SECTIONS:
13524 case OMP_CLAUSE_TASKGROUP:
13525 break;
13526 default:
13527 gcc_unreachable ();
13528 }
13529 }
13530
13531 new_clauses = nreverse (new_clauses);
13532 if (!declare_simd)
13533 new_clauses = finish_omp_clauses (new_clauses);
13534 return new_clauses;
13535 }
13536
13537 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13538
13539 static tree
13540 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13541 tree in_decl)
13542 {
13543 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13544
13545 tree purpose, value, chain;
13546
13547 if (t == NULL)
13548 return t;
13549
13550 if (TREE_CODE (t) != TREE_LIST)
13551 return tsubst_copy_and_build (t, args, complain, in_decl,
13552 /*function_p=*/false,
13553 /*integral_constant_expression_p=*/false);
13554
13555 if (t == void_list_node)
13556 return t;
13557
13558 purpose = TREE_PURPOSE (t);
13559 if (purpose)
13560 purpose = RECUR (purpose);
13561 value = TREE_VALUE (t);
13562 if (value)
13563 {
13564 if (TREE_CODE (value) != LABEL_DECL)
13565 value = RECUR (value);
13566 else
13567 {
13568 value = lookup_label (DECL_NAME (value));
13569 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13570 TREE_USED (value) = 1;
13571 }
13572 }
13573 chain = TREE_CHAIN (t);
13574 if (chain && chain != void_type_node)
13575 chain = RECUR (chain);
13576 return tree_cons (purpose, value, chain);
13577 #undef RECUR
13578 }
13579
13580 /* Substitute one OMP_FOR iterator. */
13581
13582 static void
13583 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13584 tree condv, tree incrv, tree *clauses,
13585 tree args, tsubst_flags_t complain, tree in_decl,
13586 bool integral_constant_expression_p)
13587 {
13588 #define RECUR(NODE) \
13589 tsubst_expr ((NODE), args, complain, in_decl, \
13590 integral_constant_expression_p)
13591 tree decl, init, cond, incr;
13592
13593 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13594 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13595 decl = TREE_OPERAND (init, 0);
13596 init = TREE_OPERAND (init, 1);
13597 tree decl_expr = NULL_TREE;
13598 if (init && TREE_CODE (init) == DECL_EXPR)
13599 {
13600 /* We need to jump through some hoops to handle declarations in the
13601 for-init-statement, since we might need to handle auto deduction,
13602 but we need to keep control of initialization. */
13603 decl_expr = init;
13604 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13605 decl = tsubst_decl (decl, args, complain);
13606 }
13607 else
13608 decl = RECUR (decl);
13609 init = RECUR (init);
13610
13611 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13612 if (auto_node && init)
13613 TREE_TYPE (decl)
13614 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13615
13616 gcc_assert (!type_dependent_expression_p (decl));
13617
13618 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13619 {
13620 if (decl_expr)
13621 {
13622 /* Declare the variable, but don't let that initialize it. */
13623 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13624 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13625 RECUR (decl_expr);
13626 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13627 }
13628
13629 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13630 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13631 if (TREE_CODE (incr) == MODIFY_EXPR)
13632 {
13633 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13634 tree rhs = RECUR (TREE_OPERAND (incr, 1));
13635 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
13636 NOP_EXPR, rhs, complain);
13637 }
13638 else
13639 incr = RECUR (incr);
13640 TREE_VEC_ELT (declv, i) = decl;
13641 TREE_VEC_ELT (initv, i) = init;
13642 TREE_VEC_ELT (condv, i) = cond;
13643 TREE_VEC_ELT (incrv, i) = incr;
13644 return;
13645 }
13646
13647 if (decl_expr)
13648 {
13649 /* Declare and initialize the variable. */
13650 RECUR (decl_expr);
13651 init = NULL_TREE;
13652 }
13653 else if (init)
13654 {
13655 tree c;
13656 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13657 {
13658 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13659 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13660 && OMP_CLAUSE_DECL (c) == decl)
13661 break;
13662 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13663 && OMP_CLAUSE_DECL (c) == decl)
13664 error ("iteration variable %qD should not be firstprivate", decl);
13665 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13666 && OMP_CLAUSE_DECL (c) == decl)
13667 error ("iteration variable %qD should not be reduction", decl);
13668 }
13669 if (c == NULL)
13670 {
13671 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13672 OMP_CLAUSE_DECL (c) = decl;
13673 c = finish_omp_clauses (c);
13674 if (c)
13675 {
13676 OMP_CLAUSE_CHAIN (c) = *clauses;
13677 *clauses = c;
13678 }
13679 }
13680 }
13681 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13682 if (COMPARISON_CLASS_P (cond))
13683 {
13684 tree op0 = RECUR (TREE_OPERAND (cond, 0));
13685 tree op1 = RECUR (TREE_OPERAND (cond, 1));
13686 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
13687 }
13688 else
13689 cond = RECUR (cond);
13690 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13691 switch (TREE_CODE (incr))
13692 {
13693 case PREINCREMENT_EXPR:
13694 case PREDECREMENT_EXPR:
13695 case POSTINCREMENT_EXPR:
13696 case POSTDECREMENT_EXPR:
13697 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13698 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13699 break;
13700 case MODIFY_EXPR:
13701 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13702 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13703 {
13704 tree rhs = TREE_OPERAND (incr, 1);
13705 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13706 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13707 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13708 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13709 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13710 rhs0, rhs1));
13711 }
13712 else
13713 incr = RECUR (incr);
13714 break;
13715 case MODOP_EXPR:
13716 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13717 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13718 {
13719 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13720 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13721 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13722 TREE_TYPE (decl), lhs,
13723 RECUR (TREE_OPERAND (incr, 2))));
13724 }
13725 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13726 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13727 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13728 {
13729 tree rhs = TREE_OPERAND (incr, 2);
13730 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13731 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13732 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13733 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13734 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13735 rhs0, rhs1));
13736 }
13737 else
13738 incr = RECUR (incr);
13739 break;
13740 default:
13741 incr = RECUR (incr);
13742 break;
13743 }
13744
13745 TREE_VEC_ELT (declv, i) = decl;
13746 TREE_VEC_ELT (initv, i) = init;
13747 TREE_VEC_ELT (condv, i) = cond;
13748 TREE_VEC_ELT (incrv, i) = incr;
13749 #undef RECUR
13750 }
13751
13752 /* Like tsubst_copy for expressions, etc. but also does semantic
13753 processing. */
13754
13755 static tree
13756 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13757 bool integral_constant_expression_p)
13758 {
13759 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13760 #define RECUR(NODE) \
13761 tsubst_expr ((NODE), args, complain, in_decl, \
13762 integral_constant_expression_p)
13763
13764 tree stmt, tmp;
13765 tree r;
13766 location_t loc;
13767
13768 if (t == NULL_TREE || t == error_mark_node)
13769 return t;
13770
13771 loc = input_location;
13772 if (EXPR_HAS_LOCATION (t))
13773 input_location = EXPR_LOCATION (t);
13774 if (STATEMENT_CODE_P (TREE_CODE (t)))
13775 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13776
13777 switch (TREE_CODE (t))
13778 {
13779 case STATEMENT_LIST:
13780 {
13781 tree_stmt_iterator i;
13782 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13783 RECUR (tsi_stmt (i));
13784 break;
13785 }
13786
13787 case CTOR_INITIALIZER:
13788 finish_mem_initializers (tsubst_initializer_list
13789 (TREE_OPERAND (t, 0), args));
13790 break;
13791
13792 case RETURN_EXPR:
13793 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13794 break;
13795
13796 case EXPR_STMT:
13797 tmp = RECUR (EXPR_STMT_EXPR (t));
13798 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13799 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13800 else
13801 finish_expr_stmt (tmp);
13802 break;
13803
13804 case USING_STMT:
13805 do_using_directive (USING_STMT_NAMESPACE (t));
13806 break;
13807
13808 case DECL_EXPR:
13809 {
13810 tree decl, pattern_decl;
13811 tree init;
13812
13813 pattern_decl = decl = DECL_EXPR_DECL (t);
13814 if (TREE_CODE (decl) == LABEL_DECL)
13815 finish_label_decl (DECL_NAME (decl));
13816 else if (TREE_CODE (decl) == USING_DECL)
13817 {
13818 tree scope = USING_DECL_SCOPE (decl);
13819 tree name = DECL_NAME (decl);
13820 tree decl;
13821
13822 scope = tsubst (scope, args, complain, in_decl);
13823 decl = lookup_qualified_name (scope, name,
13824 /*is_type_p=*/false,
13825 /*complain=*/false);
13826 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13827 qualified_name_lookup_error (scope, name, decl, input_location);
13828 else
13829 do_local_using_decl (decl, scope, name);
13830 }
13831 else if (DECL_PACK_P (decl))
13832 {
13833 /* Don't build up decls for a variadic capture proxy, we'll
13834 instantiate the elements directly as needed. */
13835 break;
13836 }
13837 else
13838 {
13839 init = DECL_INITIAL (decl);
13840 decl = tsubst (decl, args, complain, in_decl);
13841 if (decl != error_mark_node)
13842 {
13843 /* By marking the declaration as instantiated, we avoid
13844 trying to instantiate it. Since instantiate_decl can't
13845 handle local variables, and since we've already done
13846 all that needs to be done, that's the right thing to
13847 do. */
13848 if (VAR_P (decl))
13849 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13850 if (VAR_P (decl)
13851 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13852 /* Anonymous aggregates are a special case. */
13853 finish_anon_union (decl);
13854 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13855 {
13856 DECL_CONTEXT (decl) = current_function_decl;
13857 if (DECL_NAME (decl) == this_identifier)
13858 {
13859 tree lam = DECL_CONTEXT (current_function_decl);
13860 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13861 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13862 }
13863 insert_capture_proxy (decl);
13864 }
13865 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13866 /* We already did a pushtag. */;
13867 else if (TREE_CODE (decl) == FUNCTION_DECL
13868 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13869 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13870 {
13871 DECL_CONTEXT (decl) = NULL_TREE;
13872 pushdecl (decl);
13873 DECL_CONTEXT (decl) = current_function_decl;
13874 cp_check_omp_declare_reduction (decl);
13875 }
13876 else
13877 {
13878 int const_init = false;
13879 maybe_push_decl (decl);
13880 if (VAR_P (decl)
13881 && DECL_PRETTY_FUNCTION_P (decl))
13882 {
13883 /* For __PRETTY_FUNCTION__ we have to adjust the
13884 initializer. */
13885 const char *const name
13886 = cxx_printable_name (current_function_decl, 2);
13887 init = cp_fname_init (name, &TREE_TYPE (decl));
13888 }
13889 else
13890 init = tsubst_init (init, decl, args, complain, in_decl);
13891
13892 if (VAR_P (decl))
13893 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13894 (pattern_decl));
13895 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13896 }
13897 }
13898 }
13899
13900 break;
13901 }
13902
13903 case FOR_STMT:
13904 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13905 RECUR (FOR_INIT_STMT (t));
13906 finish_for_init_stmt (stmt);
13907 tmp = RECUR (FOR_COND (t));
13908 finish_for_cond (tmp, stmt, false);
13909 tmp = RECUR (FOR_EXPR (t));
13910 finish_for_expr (tmp, stmt);
13911 RECUR (FOR_BODY (t));
13912 finish_for_stmt (stmt);
13913 break;
13914
13915 case RANGE_FOR_STMT:
13916 {
13917 tree decl, expr;
13918 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13919 decl = RANGE_FOR_DECL (t);
13920 decl = tsubst (decl, args, complain, in_decl);
13921 maybe_push_decl (decl);
13922 expr = RECUR (RANGE_FOR_EXPR (t));
13923 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13924 RECUR (RANGE_FOR_BODY (t));
13925 finish_for_stmt (stmt);
13926 }
13927 break;
13928
13929 case WHILE_STMT:
13930 stmt = begin_while_stmt ();
13931 tmp = RECUR (WHILE_COND (t));
13932 finish_while_stmt_cond (tmp, stmt, false);
13933 RECUR (WHILE_BODY (t));
13934 finish_while_stmt (stmt);
13935 break;
13936
13937 case DO_STMT:
13938 stmt = begin_do_stmt ();
13939 RECUR (DO_BODY (t));
13940 finish_do_body (stmt);
13941 tmp = RECUR (DO_COND (t));
13942 finish_do_stmt (tmp, stmt, false);
13943 break;
13944
13945 case IF_STMT:
13946 stmt = begin_if_stmt ();
13947 tmp = RECUR (IF_COND (t));
13948 finish_if_stmt_cond (tmp, stmt);
13949 RECUR (THEN_CLAUSE (t));
13950 finish_then_clause (stmt);
13951
13952 if (ELSE_CLAUSE (t))
13953 {
13954 begin_else_clause (stmt);
13955 RECUR (ELSE_CLAUSE (t));
13956 finish_else_clause (stmt);
13957 }
13958
13959 finish_if_stmt (stmt);
13960 break;
13961
13962 case BIND_EXPR:
13963 if (BIND_EXPR_BODY_BLOCK (t))
13964 stmt = begin_function_body ();
13965 else
13966 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13967 ? BCS_TRY_BLOCK : 0);
13968
13969 RECUR (BIND_EXPR_BODY (t));
13970
13971 if (BIND_EXPR_BODY_BLOCK (t))
13972 finish_function_body (stmt);
13973 else
13974 finish_compound_stmt (stmt);
13975 break;
13976
13977 case BREAK_STMT:
13978 finish_break_stmt ();
13979 break;
13980
13981 case CONTINUE_STMT:
13982 finish_continue_stmt ();
13983 break;
13984
13985 case SWITCH_STMT:
13986 stmt = begin_switch_stmt ();
13987 tmp = RECUR (SWITCH_STMT_COND (t));
13988 finish_switch_cond (tmp, stmt);
13989 RECUR (SWITCH_STMT_BODY (t));
13990 finish_switch_stmt (stmt);
13991 break;
13992
13993 case CASE_LABEL_EXPR:
13994 {
13995 tree low = RECUR (CASE_LOW (t));
13996 tree high = RECUR (CASE_HIGH (t));
13997 finish_case_label (EXPR_LOCATION (t), low, high);
13998 }
13999 break;
14000
14001 case LABEL_EXPR:
14002 {
14003 tree decl = LABEL_EXPR_LABEL (t);
14004 tree label;
14005
14006 label = finish_label_stmt (DECL_NAME (decl));
14007 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
14008 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
14009 }
14010 break;
14011
14012 case GOTO_EXPR:
14013 tmp = GOTO_DESTINATION (t);
14014 if (TREE_CODE (tmp) != LABEL_DECL)
14015 /* Computed goto's must be tsubst'd into. On the other hand,
14016 non-computed gotos must not be; the identifier in question
14017 will have no binding. */
14018 tmp = RECUR (tmp);
14019 else
14020 tmp = DECL_NAME (tmp);
14021 finish_goto_stmt (tmp);
14022 break;
14023
14024 case ASM_EXPR:
14025 {
14026 tree string = RECUR (ASM_STRING (t));
14027 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
14028 complain, in_decl);
14029 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
14030 complain, in_decl);
14031 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
14032 complain, in_decl);
14033 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
14034 complain, in_decl);
14035 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
14036 clobbers, labels);
14037 tree asm_expr = tmp;
14038 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14039 asm_expr = TREE_OPERAND (asm_expr, 0);
14040 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14041 }
14042 break;
14043
14044 case TRY_BLOCK:
14045 if (CLEANUP_P (t))
14046 {
14047 stmt = begin_try_block ();
14048 RECUR (TRY_STMTS (t));
14049 finish_cleanup_try_block (stmt);
14050 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14051 }
14052 else
14053 {
14054 tree compound_stmt = NULL_TREE;
14055
14056 if (FN_TRY_BLOCK_P (t))
14057 stmt = begin_function_try_block (&compound_stmt);
14058 else
14059 stmt = begin_try_block ();
14060
14061 RECUR (TRY_STMTS (t));
14062
14063 if (FN_TRY_BLOCK_P (t))
14064 finish_function_try_block (stmt);
14065 else
14066 finish_try_block (stmt);
14067
14068 RECUR (TRY_HANDLERS (t));
14069 if (FN_TRY_BLOCK_P (t))
14070 finish_function_handler_sequence (stmt, compound_stmt);
14071 else
14072 finish_handler_sequence (stmt);
14073 }
14074 break;
14075
14076 case HANDLER:
14077 {
14078 tree decl = HANDLER_PARMS (t);
14079
14080 if (decl)
14081 {
14082 decl = tsubst (decl, args, complain, in_decl);
14083 /* Prevent instantiate_decl from trying to instantiate
14084 this variable. We've already done all that needs to be
14085 done. */
14086 if (decl != error_mark_node)
14087 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14088 }
14089 stmt = begin_handler ();
14090 finish_handler_parms (decl, stmt);
14091 RECUR (HANDLER_BODY (t));
14092 finish_handler (stmt);
14093 }
14094 break;
14095
14096 case TAG_DEFN:
14097 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14098 if (CLASS_TYPE_P (tmp))
14099 {
14100 /* Local classes are not independent templates; they are
14101 instantiated along with their containing function. And this
14102 way we don't have to deal with pushing out of one local class
14103 to instantiate a member of another local class. */
14104 tree fn;
14105 /* Closures are handled by the LAMBDA_EXPR. */
14106 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14107 complete_type (tmp);
14108 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14109 if (!DECL_ARTIFICIAL (fn))
14110 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14111 }
14112 break;
14113
14114 case STATIC_ASSERT:
14115 {
14116 tree condition;
14117
14118 ++c_inhibit_evaluation_warnings;
14119 condition =
14120 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14121 args,
14122 complain, in_decl,
14123 /*integral_constant_expression_p=*/true);
14124 --c_inhibit_evaluation_warnings;
14125
14126 finish_static_assert (condition,
14127 STATIC_ASSERT_MESSAGE (t),
14128 STATIC_ASSERT_SOURCE_LOCATION (t),
14129 /*member_p=*/false);
14130 }
14131 break;
14132
14133 case OMP_PARALLEL:
14134 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
14135 args, complain, in_decl);
14136 stmt = begin_omp_parallel ();
14137 RECUR (OMP_PARALLEL_BODY (t));
14138 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
14139 = OMP_PARALLEL_COMBINED (t);
14140 break;
14141
14142 case OMP_TASK:
14143 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
14144 args, complain, in_decl);
14145 stmt = begin_omp_task ();
14146 RECUR (OMP_TASK_BODY (t));
14147 finish_omp_task (tmp, stmt);
14148 break;
14149
14150 case OMP_FOR:
14151 case OMP_SIMD:
14152 case CILK_SIMD:
14153 case CILK_FOR:
14154 case OMP_DISTRIBUTE:
14155 {
14156 tree clauses, body, pre_body;
14157 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
14158 tree incrv = NULL_TREE;
14159 int i;
14160
14161 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
14162 args, complain, in_decl);
14163 if (OMP_FOR_INIT (t) != NULL_TREE)
14164 {
14165 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14166 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14167 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14168 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14169 }
14170
14171 stmt = begin_omp_structured_block ();
14172
14173 pre_body = push_stmt_list ();
14174 RECUR (OMP_FOR_PRE_BODY (t));
14175 pre_body = pop_stmt_list (pre_body);
14176
14177 if (OMP_FOR_INIT (t) != NULL_TREE)
14178 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
14179 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
14180 &clauses, args, complain, in_decl,
14181 integral_constant_expression_p);
14182
14183 body = push_stmt_list ();
14184 RECUR (OMP_FOR_BODY (t));
14185 body = pop_stmt_list (body);
14186
14187 if (OMP_FOR_INIT (t) != NULL_TREE)
14188 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
14189 condv, incrv, body, pre_body, clauses);
14190 else
14191 {
14192 t = make_node (TREE_CODE (t));
14193 TREE_TYPE (t) = void_type_node;
14194 OMP_FOR_BODY (t) = body;
14195 OMP_FOR_PRE_BODY (t) = pre_body;
14196 OMP_FOR_CLAUSES (t) = clauses;
14197 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
14198 add_stmt (t);
14199 }
14200
14201 add_stmt (finish_omp_structured_block (stmt));
14202 }
14203 break;
14204
14205 case OMP_SECTIONS:
14206 case OMP_SINGLE:
14207 case OMP_TEAMS:
14208 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14209 args, complain, in_decl);
14210 stmt = push_stmt_list ();
14211 RECUR (OMP_BODY (t));
14212 stmt = pop_stmt_list (stmt);
14213
14214 t = copy_node (t);
14215 OMP_BODY (t) = stmt;
14216 OMP_CLAUSES (t) = tmp;
14217 add_stmt (t);
14218 break;
14219
14220 case OMP_TARGET_DATA:
14221 case OMP_TARGET:
14222 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14223 args, complain, in_decl);
14224 keep_next_level (true);
14225 stmt = begin_omp_structured_block ();
14226
14227 RECUR (OMP_BODY (t));
14228 stmt = finish_omp_structured_block (stmt);
14229
14230 t = copy_node (t);
14231 OMP_BODY (t) = stmt;
14232 OMP_CLAUSES (t) = tmp;
14233 add_stmt (t);
14234 break;
14235
14236 case OMP_TARGET_UPDATE:
14237 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14238 args, complain, in_decl);
14239 t = copy_node (t);
14240 OMP_TARGET_UPDATE_CLAUSES (t) = tmp;
14241 add_stmt (t);
14242 break;
14243
14244 case OMP_SECTION:
14245 case OMP_CRITICAL:
14246 case OMP_MASTER:
14247 case OMP_TASKGROUP:
14248 case OMP_ORDERED:
14249 stmt = push_stmt_list ();
14250 RECUR (OMP_BODY (t));
14251 stmt = pop_stmt_list (stmt);
14252
14253 t = copy_node (t);
14254 OMP_BODY (t) = stmt;
14255 add_stmt (t);
14256 break;
14257
14258 case OMP_ATOMIC:
14259 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14260 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14261 {
14262 tree op1 = TREE_OPERAND (t, 1);
14263 tree rhs1 = NULL_TREE;
14264 tree lhs, rhs;
14265 if (TREE_CODE (op1) == COMPOUND_EXPR)
14266 {
14267 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14268 op1 = TREE_OPERAND (op1, 1);
14269 }
14270 lhs = RECUR (TREE_OPERAND (op1, 0));
14271 rhs = RECUR (TREE_OPERAND (op1, 1));
14272 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14273 NULL_TREE, NULL_TREE, rhs1,
14274 OMP_ATOMIC_SEQ_CST (t));
14275 }
14276 else
14277 {
14278 tree op1 = TREE_OPERAND (t, 1);
14279 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14280 tree rhs1 = NULL_TREE;
14281 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14282 enum tree_code opcode = NOP_EXPR;
14283 if (code == OMP_ATOMIC_READ)
14284 {
14285 v = RECUR (TREE_OPERAND (op1, 0));
14286 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14287 }
14288 else if (code == OMP_ATOMIC_CAPTURE_OLD
14289 || code == OMP_ATOMIC_CAPTURE_NEW)
14290 {
14291 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14292 v = RECUR (TREE_OPERAND (op1, 0));
14293 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14294 if (TREE_CODE (op11) == COMPOUND_EXPR)
14295 {
14296 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14297 op11 = TREE_OPERAND (op11, 1);
14298 }
14299 lhs = RECUR (TREE_OPERAND (op11, 0));
14300 rhs = RECUR (TREE_OPERAND (op11, 1));
14301 opcode = TREE_CODE (op11);
14302 if (opcode == MODIFY_EXPR)
14303 opcode = NOP_EXPR;
14304 }
14305 else
14306 {
14307 code = OMP_ATOMIC;
14308 lhs = RECUR (TREE_OPERAND (op1, 0));
14309 rhs = RECUR (TREE_OPERAND (op1, 1));
14310 }
14311 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14312 OMP_ATOMIC_SEQ_CST (t));
14313 }
14314 break;
14315
14316 case TRANSACTION_EXPR:
14317 {
14318 int flags = 0;
14319 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14320 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14321
14322 if (TRANSACTION_EXPR_IS_STMT (t))
14323 {
14324 tree body = TRANSACTION_EXPR_BODY (t);
14325 tree noex = NULL_TREE;
14326 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14327 {
14328 noex = MUST_NOT_THROW_COND (body);
14329 if (noex == NULL_TREE)
14330 noex = boolean_true_node;
14331 body = TREE_OPERAND (body, 0);
14332 }
14333 stmt = begin_transaction_stmt (input_location, NULL, flags);
14334 RECUR (body);
14335 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14336 }
14337 else
14338 {
14339 stmt = build_transaction_expr (EXPR_LOCATION (t),
14340 RECUR (TRANSACTION_EXPR_BODY (t)),
14341 flags, NULL_TREE);
14342 RETURN (stmt);
14343 }
14344 }
14345 break;
14346
14347 case MUST_NOT_THROW_EXPR:
14348 {
14349 tree op0 = RECUR (TREE_OPERAND (t, 0));
14350 tree cond = RECUR (MUST_NOT_THROW_COND (t));
14351 RETURN (build_must_not_throw_expr (op0, cond));
14352 }
14353
14354 case EXPR_PACK_EXPANSION:
14355 error ("invalid use of pack expansion expression");
14356 RETURN (error_mark_node);
14357
14358 case NONTYPE_ARGUMENT_PACK:
14359 error ("use %<...%> to expand argument pack");
14360 RETURN (error_mark_node);
14361
14362 case CILK_SPAWN_STMT:
14363 cfun->calls_cilk_spawn = 1;
14364 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14365
14366 case CILK_SYNC_STMT:
14367 RETURN (build_cilk_sync ());
14368
14369 case COMPOUND_EXPR:
14370 tmp = RECUR (TREE_OPERAND (t, 0));
14371 if (tmp == NULL_TREE)
14372 /* If the first operand was a statement, we're done with it. */
14373 RETURN (RECUR (TREE_OPERAND (t, 1)));
14374 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14375 RECUR (TREE_OPERAND (t, 1)),
14376 complain));
14377
14378 case ANNOTATE_EXPR:
14379 tmp = RECUR (TREE_OPERAND (t, 0));
14380 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14381 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14382
14383 default:
14384 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14385
14386 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14387 /*function_p=*/false,
14388 integral_constant_expression_p));
14389 }
14390
14391 RETURN (NULL_TREE);
14392 out:
14393 input_location = loc;
14394 return r;
14395 #undef RECUR
14396 #undef RETURN
14397 }
14398
14399 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14400 function. For description of the body see comment above
14401 cp_parser_omp_declare_reduction_exprs. */
14402
14403 static void
14404 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14405 {
14406 if (t == NULL_TREE || t == error_mark_node)
14407 return;
14408
14409 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14410
14411 tree_stmt_iterator tsi;
14412 int i;
14413 tree stmts[7];
14414 memset (stmts, 0, sizeof stmts);
14415 for (i = 0, tsi = tsi_start (t);
14416 i < 7 && !tsi_end_p (tsi);
14417 i++, tsi_next (&tsi))
14418 stmts[i] = tsi_stmt (tsi);
14419 gcc_assert (tsi_end_p (tsi));
14420
14421 if (i >= 3)
14422 {
14423 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14424 && TREE_CODE (stmts[1]) == DECL_EXPR);
14425 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14426 args, complain, in_decl);
14427 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14428 args, complain, in_decl);
14429 DECL_CONTEXT (omp_out) = current_function_decl;
14430 DECL_CONTEXT (omp_in) = current_function_decl;
14431 keep_next_level (true);
14432 tree block = begin_omp_structured_block ();
14433 tsubst_expr (stmts[2], args, complain, in_decl, false);
14434 block = finish_omp_structured_block (block);
14435 block = maybe_cleanup_point_expr_void (block);
14436 add_decl_expr (omp_out);
14437 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14438 TREE_NO_WARNING (omp_out) = 1;
14439 add_decl_expr (omp_in);
14440 finish_expr_stmt (block);
14441 }
14442 if (i >= 6)
14443 {
14444 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14445 && TREE_CODE (stmts[4]) == DECL_EXPR);
14446 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14447 args, complain, in_decl);
14448 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14449 args, complain, in_decl);
14450 DECL_CONTEXT (omp_priv) = current_function_decl;
14451 DECL_CONTEXT (omp_orig) = current_function_decl;
14452 keep_next_level (true);
14453 tree block = begin_omp_structured_block ();
14454 tsubst_expr (stmts[5], args, complain, in_decl, false);
14455 block = finish_omp_structured_block (block);
14456 block = maybe_cleanup_point_expr_void (block);
14457 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14458 add_decl_expr (omp_priv);
14459 add_decl_expr (omp_orig);
14460 finish_expr_stmt (block);
14461 if (i == 7)
14462 add_decl_expr (omp_orig);
14463 }
14464 }
14465
14466 /* T is a postfix-expression that is not being used in a function
14467 call. Return the substituted version of T. */
14468
14469 static tree
14470 tsubst_non_call_postfix_expression (tree t, tree args,
14471 tsubst_flags_t complain,
14472 tree in_decl)
14473 {
14474 if (TREE_CODE (t) == SCOPE_REF)
14475 t = tsubst_qualified_id (t, args, complain, in_decl,
14476 /*done=*/false, /*address_p=*/false);
14477 else
14478 t = tsubst_copy_and_build (t, args, complain, in_decl,
14479 /*function_p=*/false,
14480 /*integral_constant_expression_p=*/false);
14481
14482 return t;
14483 }
14484
14485 /* Like tsubst but deals with expressions and performs semantic
14486 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14487
14488 tree
14489 tsubst_copy_and_build (tree t,
14490 tree args,
14491 tsubst_flags_t complain,
14492 tree in_decl,
14493 bool function_p,
14494 bool integral_constant_expression_p)
14495 {
14496 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14497 #define RECUR(NODE) \
14498 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14499 /*function_p=*/false, \
14500 integral_constant_expression_p)
14501
14502 tree retval, op1;
14503 location_t loc;
14504
14505 if (t == NULL_TREE || t == error_mark_node)
14506 return t;
14507
14508 loc = input_location;
14509 if (EXPR_HAS_LOCATION (t))
14510 input_location = EXPR_LOCATION (t);
14511
14512 /* N3276 decltype magic only applies to calls at the top level or on the
14513 right side of a comma. */
14514 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14515 complain &= ~tf_decltype;
14516
14517 switch (TREE_CODE (t))
14518 {
14519 case USING_DECL:
14520 t = DECL_NAME (t);
14521 /* Fall through. */
14522 case IDENTIFIER_NODE:
14523 {
14524 tree decl;
14525 cp_id_kind idk;
14526 bool non_integral_constant_expression_p;
14527 const char *error_msg;
14528
14529 if (IDENTIFIER_TYPENAME_P (t))
14530 {
14531 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14532 t = mangle_conv_op_name_for_type (new_type);
14533 }
14534
14535 /* Look up the name. */
14536 decl = lookup_name (t);
14537
14538 /* By convention, expressions use ERROR_MARK_NODE to indicate
14539 failure, not NULL_TREE. */
14540 if (decl == NULL_TREE)
14541 decl = error_mark_node;
14542
14543 decl = finish_id_expression (t, decl, NULL_TREE,
14544 &idk,
14545 integral_constant_expression_p,
14546 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14547 &non_integral_constant_expression_p,
14548 /*template_p=*/false,
14549 /*done=*/true,
14550 /*address_p=*/false,
14551 /*template_arg_p=*/false,
14552 &error_msg,
14553 input_location);
14554 if (error_msg)
14555 error (error_msg);
14556 if (!function_p && identifier_p (decl))
14557 {
14558 if (complain & tf_error)
14559 unqualified_name_lookup_error (decl);
14560 decl = error_mark_node;
14561 }
14562 RETURN (decl);
14563 }
14564
14565 case TEMPLATE_ID_EXPR:
14566 {
14567 tree object;
14568 tree templ = RECUR (TREE_OPERAND (t, 0));
14569 tree targs = TREE_OPERAND (t, 1);
14570
14571 if (targs)
14572 targs = tsubst_template_args (targs, args, complain, in_decl);
14573
14574 if (TREE_CODE (templ) == COMPONENT_REF)
14575 {
14576 object = TREE_OPERAND (templ, 0);
14577 templ = TREE_OPERAND (templ, 1);
14578 }
14579 else
14580 object = NULL_TREE;
14581 templ = lookup_template_function (templ, targs);
14582
14583 if (object)
14584 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14585 object, templ, NULL_TREE));
14586 else
14587 RETURN (baselink_for_fns (templ));
14588 }
14589
14590 case INDIRECT_REF:
14591 {
14592 tree r = RECUR (TREE_OPERAND (t, 0));
14593
14594 if (REFERENCE_REF_P (t))
14595 {
14596 /* A type conversion to reference type will be enclosed in
14597 such an indirect ref, but the substitution of the cast
14598 will have also added such an indirect ref. */
14599 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14600 r = convert_from_reference (r);
14601 }
14602 else
14603 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14604 complain|decltype_flag);
14605 RETURN (r);
14606 }
14607
14608 case NOP_EXPR:
14609 {
14610 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14611 tree op0 = RECUR (TREE_OPERAND (t, 0));
14612 RETURN (build_nop (type, op0));
14613 }
14614
14615 case IMPLICIT_CONV_EXPR:
14616 {
14617 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14618 tree expr = RECUR (TREE_OPERAND (t, 0));
14619 int flags = LOOKUP_IMPLICIT;
14620 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14621 flags = LOOKUP_NORMAL;
14622 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14623 flags));
14624 }
14625
14626 case CONVERT_EXPR:
14627 {
14628 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14629 tree op0 = RECUR (TREE_OPERAND (t, 0));
14630 RETURN (build1 (CONVERT_EXPR, type, op0));
14631 }
14632
14633 case CAST_EXPR:
14634 case REINTERPRET_CAST_EXPR:
14635 case CONST_CAST_EXPR:
14636 case DYNAMIC_CAST_EXPR:
14637 case STATIC_CAST_EXPR:
14638 {
14639 tree type;
14640 tree op, r = NULL_TREE;
14641
14642 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14643 if (integral_constant_expression_p
14644 && !cast_valid_in_integral_constant_expression_p (type))
14645 {
14646 if (complain & tf_error)
14647 error ("a cast to a type other than an integral or "
14648 "enumeration type cannot appear in a constant-expression");
14649 RETURN (error_mark_node);
14650 }
14651
14652 op = RECUR (TREE_OPERAND (t, 0));
14653
14654 warning_sentinel s(warn_useless_cast);
14655 switch (TREE_CODE (t))
14656 {
14657 case CAST_EXPR:
14658 r = build_functional_cast (type, op, complain);
14659 break;
14660 case REINTERPRET_CAST_EXPR:
14661 r = build_reinterpret_cast (type, op, complain);
14662 break;
14663 case CONST_CAST_EXPR:
14664 r = build_const_cast (type, op, complain);
14665 break;
14666 case DYNAMIC_CAST_EXPR:
14667 r = build_dynamic_cast (type, op, complain);
14668 break;
14669 case STATIC_CAST_EXPR:
14670 r = build_static_cast (type, op, complain);
14671 break;
14672 default:
14673 gcc_unreachable ();
14674 }
14675
14676 RETURN (r);
14677 }
14678
14679 case POSTDECREMENT_EXPR:
14680 case POSTINCREMENT_EXPR:
14681 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14682 args, complain, in_decl);
14683 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14684 complain|decltype_flag));
14685
14686 case PREDECREMENT_EXPR:
14687 case PREINCREMENT_EXPR:
14688 case NEGATE_EXPR:
14689 case BIT_NOT_EXPR:
14690 case ABS_EXPR:
14691 case TRUTH_NOT_EXPR:
14692 case UNARY_PLUS_EXPR: /* Unary + */
14693 case REALPART_EXPR:
14694 case IMAGPART_EXPR:
14695 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14696 RECUR (TREE_OPERAND (t, 0)),
14697 complain|decltype_flag));
14698
14699 case FIX_TRUNC_EXPR:
14700 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14701 0, complain));
14702
14703 case ADDR_EXPR:
14704 op1 = TREE_OPERAND (t, 0);
14705 if (TREE_CODE (op1) == LABEL_DECL)
14706 RETURN (finish_label_address_expr (DECL_NAME (op1),
14707 EXPR_LOCATION (op1)));
14708 if (TREE_CODE (op1) == SCOPE_REF)
14709 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14710 /*done=*/true, /*address_p=*/true);
14711 else
14712 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14713 in_decl);
14714 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14715 complain|decltype_flag));
14716
14717 case PLUS_EXPR:
14718 case MINUS_EXPR:
14719 case MULT_EXPR:
14720 case TRUNC_DIV_EXPR:
14721 case CEIL_DIV_EXPR:
14722 case FLOOR_DIV_EXPR:
14723 case ROUND_DIV_EXPR:
14724 case EXACT_DIV_EXPR:
14725 case BIT_AND_EXPR:
14726 case BIT_IOR_EXPR:
14727 case BIT_XOR_EXPR:
14728 case TRUNC_MOD_EXPR:
14729 case FLOOR_MOD_EXPR:
14730 case TRUTH_ANDIF_EXPR:
14731 case TRUTH_ORIF_EXPR:
14732 case TRUTH_AND_EXPR:
14733 case TRUTH_OR_EXPR:
14734 case RSHIFT_EXPR:
14735 case LSHIFT_EXPR:
14736 case RROTATE_EXPR:
14737 case LROTATE_EXPR:
14738 case EQ_EXPR:
14739 case NE_EXPR:
14740 case MAX_EXPR:
14741 case MIN_EXPR:
14742 case LE_EXPR:
14743 case GE_EXPR:
14744 case LT_EXPR:
14745 case GT_EXPR:
14746 case MEMBER_REF:
14747 case DOTSTAR_EXPR:
14748 {
14749 warning_sentinel s1(warn_type_limits);
14750 warning_sentinel s2(warn_div_by_zero);
14751 tree op0 = RECUR (TREE_OPERAND (t, 0));
14752 tree op1 = RECUR (TREE_OPERAND (t, 1));
14753 tree r = build_x_binary_op
14754 (input_location, TREE_CODE (t),
14755 op0,
14756 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14757 ? ERROR_MARK
14758 : TREE_CODE (TREE_OPERAND (t, 0))),
14759 op1,
14760 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14761 ? ERROR_MARK
14762 : TREE_CODE (TREE_OPERAND (t, 1))),
14763 /*overload=*/NULL,
14764 complain|decltype_flag);
14765 if (EXPR_P (r) && TREE_NO_WARNING (t))
14766 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14767
14768 RETURN (r);
14769 }
14770
14771 case POINTER_PLUS_EXPR:
14772 {
14773 tree op0 = RECUR (TREE_OPERAND (t, 0));
14774 tree op1 = RECUR (TREE_OPERAND (t, 1));
14775 return fold_build_pointer_plus (op0, op1);
14776 }
14777
14778 case SCOPE_REF:
14779 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14780 /*address_p=*/false));
14781 case ARRAY_REF:
14782 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14783 args, complain, in_decl);
14784 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14785 RECUR (TREE_OPERAND (t, 1)),
14786 complain|decltype_flag));
14787
14788 case ARRAY_NOTATION_REF:
14789 {
14790 tree start_index, length, stride;
14791 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14792 args, complain, in_decl);
14793 start_index = RECUR (ARRAY_NOTATION_START (t));
14794 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14795 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14796 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14797 length, stride, TREE_TYPE (op1)));
14798 }
14799 case SIZEOF_EXPR:
14800 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14801 RETURN (tsubst_copy (t, args, complain, in_decl));
14802 /* Fall through */
14803
14804 case ALIGNOF_EXPR:
14805 {
14806 tree r;
14807
14808 op1 = TREE_OPERAND (t, 0);
14809 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14810 op1 = TREE_TYPE (op1);
14811 if (!args)
14812 {
14813 /* When there are no ARGS, we are trying to evaluate a
14814 non-dependent expression from the parser. Trying to do
14815 the substitutions may not work. */
14816 if (!TYPE_P (op1))
14817 op1 = TREE_TYPE (op1);
14818 }
14819 else
14820 {
14821 ++cp_unevaluated_operand;
14822 ++c_inhibit_evaluation_warnings;
14823 if (TYPE_P (op1))
14824 op1 = tsubst (op1, args, complain, in_decl);
14825 else
14826 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14827 /*function_p=*/false,
14828 /*integral_constant_expression_p=*/
14829 false);
14830 --cp_unevaluated_operand;
14831 --c_inhibit_evaluation_warnings;
14832 }
14833 if (TYPE_P (op1))
14834 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14835 complain & tf_error);
14836 else
14837 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14838 complain & tf_error);
14839 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14840 {
14841 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14842 {
14843 if (!processing_template_decl && TYPE_P (op1))
14844 {
14845 r = build_min (SIZEOF_EXPR, size_type_node,
14846 build1 (NOP_EXPR, op1, error_mark_node));
14847 SIZEOF_EXPR_TYPE_P (r) = 1;
14848 }
14849 else
14850 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14851 TREE_SIDE_EFFECTS (r) = 0;
14852 TREE_READONLY (r) = 1;
14853 }
14854 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14855 }
14856 RETURN (r);
14857 }
14858
14859 case AT_ENCODE_EXPR:
14860 {
14861 op1 = TREE_OPERAND (t, 0);
14862 ++cp_unevaluated_operand;
14863 ++c_inhibit_evaluation_warnings;
14864 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14865 /*function_p=*/false,
14866 /*integral_constant_expression_p=*/false);
14867 --cp_unevaluated_operand;
14868 --c_inhibit_evaluation_warnings;
14869 RETURN (objc_build_encode_expr (op1));
14870 }
14871
14872 case NOEXCEPT_EXPR:
14873 op1 = TREE_OPERAND (t, 0);
14874 ++cp_unevaluated_operand;
14875 ++c_inhibit_evaluation_warnings;
14876 ++cp_noexcept_operand;
14877 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14878 /*function_p=*/false,
14879 /*integral_constant_expression_p=*/false);
14880 --cp_unevaluated_operand;
14881 --c_inhibit_evaluation_warnings;
14882 --cp_noexcept_operand;
14883 RETURN (finish_noexcept_expr (op1, complain));
14884
14885 case MODOP_EXPR:
14886 {
14887 warning_sentinel s(warn_div_by_zero);
14888 tree lhs = RECUR (TREE_OPERAND (t, 0));
14889 tree rhs = RECUR (TREE_OPERAND (t, 2));
14890 tree r = build_x_modify_expr
14891 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
14892 complain|decltype_flag);
14893 /* TREE_NO_WARNING must be set if either the expression was
14894 parenthesized or it uses an operator such as >>= rather
14895 than plain assignment. In the former case, it was already
14896 set and must be copied. In the latter case,
14897 build_x_modify_expr sets it and it must not be reset
14898 here. */
14899 if (TREE_NO_WARNING (t))
14900 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14901
14902 RETURN (r);
14903 }
14904
14905 case ARROW_EXPR:
14906 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14907 args, complain, in_decl);
14908 /* Remember that there was a reference to this entity. */
14909 if (DECL_P (op1)
14910 && !mark_used (op1, complain) && !(complain & tf_error))
14911 RETURN (error_mark_node);
14912 RETURN (build_x_arrow (input_location, op1, complain));
14913
14914 case NEW_EXPR:
14915 {
14916 tree placement = RECUR (TREE_OPERAND (t, 0));
14917 tree init = RECUR (TREE_OPERAND (t, 3));
14918 vec<tree, va_gc> *placement_vec;
14919 vec<tree, va_gc> *init_vec;
14920 tree ret;
14921
14922 if (placement == NULL_TREE)
14923 placement_vec = NULL;
14924 else
14925 {
14926 placement_vec = make_tree_vector ();
14927 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14928 vec_safe_push (placement_vec, TREE_VALUE (placement));
14929 }
14930
14931 /* If there was an initializer in the original tree, but it
14932 instantiated to an empty list, then we should pass a
14933 non-NULL empty vector to tell build_new that it was an
14934 empty initializer() rather than no initializer. This can
14935 only happen when the initializer is a pack expansion whose
14936 parameter packs are of length zero. */
14937 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14938 init_vec = NULL;
14939 else
14940 {
14941 init_vec = make_tree_vector ();
14942 if (init == void_node)
14943 gcc_assert (init_vec != NULL);
14944 else
14945 {
14946 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14947 vec_safe_push (init_vec, TREE_VALUE (init));
14948 }
14949 }
14950
14951 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
14952 tree op2 = RECUR (TREE_OPERAND (t, 2));
14953 ret = build_new (&placement_vec, op1, op2, &init_vec,
14954 NEW_EXPR_USE_GLOBAL (t),
14955 complain);
14956
14957 if (placement_vec != NULL)
14958 release_tree_vector (placement_vec);
14959 if (init_vec != NULL)
14960 release_tree_vector (init_vec);
14961
14962 RETURN (ret);
14963 }
14964
14965 case DELETE_EXPR:
14966 {
14967 tree op0 = RECUR (TREE_OPERAND (t, 0));
14968 tree op1 = RECUR (TREE_OPERAND (t, 1));
14969 RETURN (delete_sanity (op0, op1,
14970 DELETE_EXPR_USE_VEC (t),
14971 DELETE_EXPR_USE_GLOBAL (t),
14972 complain));
14973 }
14974
14975 case COMPOUND_EXPR:
14976 {
14977 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14978 complain & ~tf_decltype, in_decl,
14979 /*function_p=*/false,
14980 integral_constant_expression_p);
14981 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14982 op0,
14983 RECUR (TREE_OPERAND (t, 1)),
14984 complain|decltype_flag));
14985 }
14986
14987 case CALL_EXPR:
14988 {
14989 tree function;
14990 vec<tree, va_gc> *call_args;
14991 unsigned int nargs, i;
14992 bool qualified_p;
14993 bool koenig_p;
14994 tree ret;
14995
14996 function = CALL_EXPR_FN (t);
14997 /* When we parsed the expression, we determined whether or
14998 not Koenig lookup should be performed. */
14999 koenig_p = KOENIG_LOOKUP_P (t);
15000 if (TREE_CODE (function) == SCOPE_REF)
15001 {
15002 qualified_p = true;
15003 function = tsubst_qualified_id (function, args, complain, in_decl,
15004 /*done=*/false,
15005 /*address_p=*/false);
15006 }
15007 else if (koenig_p && identifier_p (function))
15008 {
15009 /* Do nothing; calling tsubst_copy_and_build on an identifier
15010 would incorrectly perform unqualified lookup again.
15011
15012 Note that we can also have an IDENTIFIER_NODE if the earlier
15013 unqualified lookup found a member function; in that case
15014 koenig_p will be false and we do want to do the lookup
15015 again to find the instantiated member function.
15016
15017 FIXME but doing that causes c++/15272, so we need to stop
15018 using IDENTIFIER_NODE in that situation. */
15019 qualified_p = false;
15020 }
15021 else
15022 {
15023 if (TREE_CODE (function) == COMPONENT_REF)
15024 {
15025 tree op = TREE_OPERAND (function, 1);
15026
15027 qualified_p = (TREE_CODE (op) == SCOPE_REF
15028 || (BASELINK_P (op)
15029 && BASELINK_QUALIFIED_P (op)));
15030 }
15031 else
15032 qualified_p = false;
15033
15034 if (TREE_CODE (function) == ADDR_EXPR
15035 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
15036 /* Avoid error about taking the address of a constructor. */
15037 function = TREE_OPERAND (function, 0);
15038
15039 function = tsubst_copy_and_build (function, args, complain,
15040 in_decl,
15041 !qualified_p,
15042 integral_constant_expression_p);
15043
15044 if (BASELINK_P (function))
15045 qualified_p = true;
15046 }
15047
15048 nargs = call_expr_nargs (t);
15049 call_args = make_tree_vector ();
15050 for (i = 0; i < nargs; ++i)
15051 {
15052 tree arg = CALL_EXPR_ARG (t, i);
15053
15054 if (!PACK_EXPANSION_P (arg))
15055 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15056 else
15057 {
15058 /* Expand the pack expansion and push each entry onto
15059 CALL_ARGS. */
15060 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15061 if (TREE_CODE (arg) == TREE_VEC)
15062 {
15063 unsigned int len, j;
15064
15065 len = TREE_VEC_LENGTH (arg);
15066 for (j = 0; j < len; ++j)
15067 {
15068 tree value = TREE_VEC_ELT (arg, j);
15069 if (value != NULL_TREE)
15070 value = convert_from_reference (value);
15071 vec_safe_push (call_args, value);
15072 }
15073 }
15074 else
15075 {
15076 /* A partial substitution. Add one entry. */
15077 vec_safe_push (call_args, arg);
15078 }
15079 }
15080 }
15081
15082 /* We do not perform argument-dependent lookup if normal
15083 lookup finds a non-function, in accordance with the
15084 expected resolution of DR 218. */
15085 if (koenig_p
15086 && ((is_overloaded_fn (function)
15087 /* If lookup found a member function, the Koenig lookup is
15088 not appropriate, even if an unqualified-name was used
15089 to denote the function. */
15090 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15091 || identifier_p (function))
15092 /* Only do this when substitution turns a dependent call
15093 into a non-dependent call. */
15094 && type_dependent_expression_p_push (t)
15095 && !any_type_dependent_arguments_p (call_args))
15096 function = perform_koenig_lookup (function, call_args, tf_none);
15097
15098 if (identifier_p (function)
15099 && !any_type_dependent_arguments_p (call_args))
15100 {
15101 if (koenig_p && (complain & tf_warning_or_error))
15102 {
15103 /* For backwards compatibility and good diagnostics, try
15104 the unqualified lookup again if we aren't in SFINAE
15105 context. */
15106 tree unq = (tsubst_copy_and_build
15107 (function, args, complain, in_decl, true,
15108 integral_constant_expression_p));
15109 if (unq == error_mark_node)
15110 RETURN (error_mark_node);
15111
15112 if (unq != function)
15113 {
15114 tree fn = unq;
15115 if (INDIRECT_REF_P (fn))
15116 fn = TREE_OPERAND (fn, 0);
15117 if (TREE_CODE (fn) == COMPONENT_REF)
15118 fn = TREE_OPERAND (fn, 1);
15119 if (is_overloaded_fn (fn))
15120 fn = get_first_fn (fn);
15121 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
15122 "%qD was not declared in this scope, "
15123 "and no declarations were found by "
15124 "argument-dependent lookup at the point "
15125 "of instantiation", function))
15126 {
15127 if (!DECL_P (fn))
15128 /* Can't say anything more. */;
15129 else if (DECL_CLASS_SCOPE_P (fn))
15130 {
15131 location_t loc = EXPR_LOC_OR_LOC (t,
15132 input_location);
15133 inform (loc,
15134 "declarations in dependent base %qT are "
15135 "not found by unqualified lookup",
15136 DECL_CLASS_CONTEXT (fn));
15137 if (current_class_ptr)
15138 inform (loc,
15139 "use %<this->%D%> instead", function);
15140 else
15141 inform (loc,
15142 "use %<%T::%D%> instead",
15143 current_class_name, function);
15144 }
15145 else
15146 inform (0, "%q+D declared here, later in the "
15147 "translation unit", fn);
15148 }
15149 function = unq;
15150 }
15151 }
15152 if (identifier_p (function))
15153 {
15154 if (complain & tf_error)
15155 unqualified_name_lookup_error (function);
15156 release_tree_vector (call_args);
15157 RETURN (error_mark_node);
15158 }
15159 }
15160
15161 /* Remember that there was a reference to this entity. */
15162 if (DECL_P (function)
15163 && !mark_used (function, complain) && !(complain & tf_error))
15164 RETURN (error_mark_node);
15165
15166 /* Put back tf_decltype for the actual call. */
15167 complain |= decltype_flag;
15168
15169 if (TREE_CODE (function) == OFFSET_REF)
15170 ret = build_offset_ref_call_from_tree (function, &call_args,
15171 complain);
15172 else if (TREE_CODE (function) == COMPONENT_REF)
15173 {
15174 tree instance = TREE_OPERAND (function, 0);
15175 tree fn = TREE_OPERAND (function, 1);
15176
15177 if (processing_template_decl
15178 && (type_dependent_expression_p (instance)
15179 || (!BASELINK_P (fn)
15180 && TREE_CODE (fn) != FIELD_DECL)
15181 || type_dependent_expression_p (fn)
15182 || any_type_dependent_arguments_p (call_args)))
15183 ret = build_nt_call_vec (function, call_args);
15184 else if (!BASELINK_P (fn))
15185 ret = finish_call_expr (function, &call_args,
15186 /*disallow_virtual=*/false,
15187 /*koenig_p=*/false,
15188 complain);
15189 else
15190 ret = (build_new_method_call
15191 (instance, fn,
15192 &call_args, NULL_TREE,
15193 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
15194 /*fn_p=*/NULL,
15195 complain));
15196 }
15197 else
15198 ret = finish_call_expr (function, &call_args,
15199 /*disallow_virtual=*/qualified_p,
15200 koenig_p,
15201 complain);
15202
15203 release_tree_vector (call_args);
15204
15205 RETURN (ret);
15206 }
15207
15208 case COND_EXPR:
15209 {
15210 tree cond = RECUR (TREE_OPERAND (t, 0));
15211 tree folded_cond = fold_non_dependent_expr (cond);
15212 tree exp1, exp2;
15213
15214 if (TREE_CODE (folded_cond) == INTEGER_CST)
15215 {
15216 if (integer_zerop (folded_cond))
15217 {
15218 ++c_inhibit_evaluation_warnings;
15219 exp1 = RECUR (TREE_OPERAND (t, 1));
15220 --c_inhibit_evaluation_warnings;
15221 exp2 = RECUR (TREE_OPERAND (t, 2));
15222 }
15223 else
15224 {
15225 exp1 = RECUR (TREE_OPERAND (t, 1));
15226 ++c_inhibit_evaluation_warnings;
15227 exp2 = RECUR (TREE_OPERAND (t, 2));
15228 --c_inhibit_evaluation_warnings;
15229 }
15230 cond = folded_cond;
15231 }
15232 else
15233 {
15234 exp1 = RECUR (TREE_OPERAND (t, 1));
15235 exp2 = RECUR (TREE_OPERAND (t, 2));
15236 }
15237
15238 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
15239 cond, exp1, exp2, complain));
15240 }
15241
15242 case PSEUDO_DTOR_EXPR:
15243 {
15244 tree op0 = RECUR (TREE_OPERAND (t, 0));
15245 tree op1 = RECUR (TREE_OPERAND (t, 1));
15246 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
15247 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
15248 input_location));
15249 }
15250
15251 case TREE_LIST:
15252 {
15253 tree purpose, value, chain;
15254
15255 if (t == void_list_node)
15256 RETURN (t);
15257
15258 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15259 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15260 {
15261 /* We have pack expansions, so expand those and
15262 create a new list out of it. */
15263 tree purposevec = NULL_TREE;
15264 tree valuevec = NULL_TREE;
15265 tree chain;
15266 int i, len = -1;
15267
15268 /* Expand the argument expressions. */
15269 if (TREE_PURPOSE (t))
15270 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15271 complain, in_decl);
15272 if (TREE_VALUE (t))
15273 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15274 complain, in_decl);
15275
15276 /* Build the rest of the list. */
15277 chain = TREE_CHAIN (t);
15278 if (chain && chain != void_type_node)
15279 chain = RECUR (chain);
15280
15281 /* Determine the number of arguments. */
15282 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15283 {
15284 len = TREE_VEC_LENGTH (purposevec);
15285 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15286 }
15287 else if (TREE_CODE (valuevec) == TREE_VEC)
15288 len = TREE_VEC_LENGTH (valuevec);
15289 else
15290 {
15291 /* Since we only performed a partial substitution into
15292 the argument pack, we only RETURN (a single list
15293 node. */
15294 if (purposevec == TREE_PURPOSE (t)
15295 && valuevec == TREE_VALUE (t)
15296 && chain == TREE_CHAIN (t))
15297 RETURN (t);
15298
15299 RETURN (tree_cons (purposevec, valuevec, chain));
15300 }
15301
15302 /* Convert the argument vectors into a TREE_LIST */
15303 i = len;
15304 while (i > 0)
15305 {
15306 /* Grab the Ith values. */
15307 i--;
15308 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15309 : NULL_TREE;
15310 value
15311 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15312 : NULL_TREE;
15313
15314 /* Build the list (backwards). */
15315 chain = tree_cons (purpose, value, chain);
15316 }
15317
15318 RETURN (chain);
15319 }
15320
15321 purpose = TREE_PURPOSE (t);
15322 if (purpose)
15323 purpose = RECUR (purpose);
15324 value = TREE_VALUE (t);
15325 if (value)
15326 value = RECUR (value);
15327 chain = TREE_CHAIN (t);
15328 if (chain && chain != void_type_node)
15329 chain = RECUR (chain);
15330 if (purpose == TREE_PURPOSE (t)
15331 && value == TREE_VALUE (t)
15332 && chain == TREE_CHAIN (t))
15333 RETURN (t);
15334 RETURN (tree_cons (purpose, value, chain));
15335 }
15336
15337 case COMPONENT_REF:
15338 {
15339 tree object;
15340 tree object_type;
15341 tree member;
15342 tree r;
15343
15344 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15345 args, complain, in_decl);
15346 /* Remember that there was a reference to this entity. */
15347 if (DECL_P (object)
15348 && !mark_used (object, complain) && !(complain & tf_error))
15349 RETURN (error_mark_node);
15350 object_type = TREE_TYPE (object);
15351
15352 member = TREE_OPERAND (t, 1);
15353 if (BASELINK_P (member))
15354 member = tsubst_baselink (member,
15355 non_reference (TREE_TYPE (object)),
15356 args, complain, in_decl);
15357 else
15358 member = tsubst_copy (member, args, complain, in_decl);
15359 if (member == error_mark_node)
15360 RETURN (error_mark_node);
15361
15362 if (type_dependent_expression_p (object))
15363 /* We can't do much here. */;
15364 else if (!CLASS_TYPE_P (object_type))
15365 {
15366 if (scalarish_type_p (object_type))
15367 {
15368 tree s = NULL_TREE;
15369 tree dtor = member;
15370
15371 if (TREE_CODE (dtor) == SCOPE_REF)
15372 {
15373 s = TREE_OPERAND (dtor, 0);
15374 dtor = TREE_OPERAND (dtor, 1);
15375 }
15376 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15377 {
15378 dtor = TREE_OPERAND (dtor, 0);
15379 if (TYPE_P (dtor))
15380 RETURN (finish_pseudo_destructor_expr
15381 (object, s, dtor, input_location));
15382 }
15383 }
15384 }
15385 else if (TREE_CODE (member) == SCOPE_REF
15386 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15387 {
15388 /* Lookup the template functions now that we know what the
15389 scope is. */
15390 tree scope = TREE_OPERAND (member, 0);
15391 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15392 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15393 member = lookup_qualified_name (scope, tmpl,
15394 /*is_type_p=*/false,
15395 /*complain=*/false);
15396 if (BASELINK_P (member))
15397 {
15398 BASELINK_FUNCTIONS (member)
15399 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15400 args);
15401 member = (adjust_result_of_qualified_name_lookup
15402 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15403 object_type));
15404 }
15405 else
15406 {
15407 qualified_name_lookup_error (scope, tmpl, member,
15408 input_location);
15409 RETURN (error_mark_node);
15410 }
15411 }
15412 else if (TREE_CODE (member) == SCOPE_REF
15413 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15414 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15415 {
15416 if (complain & tf_error)
15417 {
15418 if (TYPE_P (TREE_OPERAND (member, 0)))
15419 error ("%qT is not a class or namespace",
15420 TREE_OPERAND (member, 0));
15421 else
15422 error ("%qD is not a class or namespace",
15423 TREE_OPERAND (member, 0));
15424 }
15425 RETURN (error_mark_node);
15426 }
15427 else if (TREE_CODE (member) == FIELD_DECL)
15428 {
15429 r = finish_non_static_data_member (member, object, NULL_TREE);
15430 if (TREE_CODE (r) == COMPONENT_REF)
15431 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15432 RETURN (r);
15433 }
15434
15435 r = finish_class_member_access_expr (object, member,
15436 /*template_p=*/false,
15437 complain);
15438 if (TREE_CODE (r) == COMPONENT_REF)
15439 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15440 RETURN (r);
15441 }
15442
15443 case THROW_EXPR:
15444 RETURN (build_throw
15445 (RECUR (TREE_OPERAND (t, 0))));
15446
15447 case CONSTRUCTOR:
15448 {
15449 vec<constructor_elt, va_gc> *n;
15450 constructor_elt *ce;
15451 unsigned HOST_WIDE_INT idx;
15452 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15453 bool process_index_p;
15454 int newlen;
15455 bool need_copy_p = false;
15456 tree r;
15457
15458 if (type == error_mark_node)
15459 RETURN (error_mark_node);
15460
15461 /* digest_init will do the wrong thing if we let it. */
15462 if (type && TYPE_PTRMEMFUNC_P (type))
15463 RETURN (t);
15464
15465 /* We do not want to process the index of aggregate
15466 initializers as they are identifier nodes which will be
15467 looked up by digest_init. */
15468 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15469
15470 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15471 newlen = vec_safe_length (n);
15472 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15473 {
15474 if (ce->index && process_index_p
15475 /* An identifier index is looked up in the type
15476 being initialized, not the current scope. */
15477 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15478 ce->index = RECUR (ce->index);
15479
15480 if (PACK_EXPANSION_P (ce->value))
15481 {
15482 /* Substitute into the pack expansion. */
15483 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15484 in_decl);
15485
15486 if (ce->value == error_mark_node
15487 || PACK_EXPANSION_P (ce->value))
15488 ;
15489 else if (TREE_VEC_LENGTH (ce->value) == 1)
15490 /* Just move the argument into place. */
15491 ce->value = TREE_VEC_ELT (ce->value, 0);
15492 else
15493 {
15494 /* Update the length of the final CONSTRUCTOR
15495 arguments vector, and note that we will need to
15496 copy.*/
15497 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15498 need_copy_p = true;
15499 }
15500 }
15501 else
15502 ce->value = RECUR (ce->value);
15503 }
15504
15505 if (need_copy_p)
15506 {
15507 vec<constructor_elt, va_gc> *old_n = n;
15508
15509 vec_alloc (n, newlen);
15510 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15511 {
15512 if (TREE_CODE (ce->value) == TREE_VEC)
15513 {
15514 int i, len = TREE_VEC_LENGTH (ce->value);
15515 for (i = 0; i < len; ++i)
15516 CONSTRUCTOR_APPEND_ELT (n, 0,
15517 TREE_VEC_ELT (ce->value, i));
15518 }
15519 else
15520 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15521 }
15522 }
15523
15524 r = build_constructor (init_list_type_node, n);
15525 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15526
15527 if (TREE_HAS_CONSTRUCTOR (t))
15528 RETURN (finish_compound_literal (type, r, complain));
15529
15530 TREE_TYPE (r) = type;
15531 RETURN (r);
15532 }
15533
15534 case TYPEID_EXPR:
15535 {
15536 tree operand_0 = TREE_OPERAND (t, 0);
15537 if (TYPE_P (operand_0))
15538 {
15539 operand_0 = tsubst (operand_0, args, complain, in_decl);
15540 RETURN (get_typeid (operand_0, complain));
15541 }
15542 else
15543 {
15544 operand_0 = RECUR (operand_0);
15545 RETURN (build_typeid (operand_0, complain));
15546 }
15547 }
15548
15549 case VAR_DECL:
15550 if (!args)
15551 RETURN (t);
15552 else if (DECL_PACK_P (t))
15553 {
15554 /* We don't build decls for an instantiation of a
15555 variadic capture proxy, we instantiate the elements
15556 when needed. */
15557 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15558 return RECUR (DECL_VALUE_EXPR (t));
15559 }
15560 /* Fall through */
15561
15562 case PARM_DECL:
15563 {
15564 tree r = tsubst_copy (t, args, complain, in_decl);
15565 /* ??? We're doing a subset of finish_id_expression here. */
15566 if (VAR_P (r)
15567 && !processing_template_decl
15568 && !cp_unevaluated_operand
15569 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
15570 && DECL_THREAD_LOCAL_P (r))
15571 {
15572 if (tree wrap = get_tls_wrapper_fn (r))
15573 /* Replace an evaluated use of the thread_local variable with
15574 a call to its wrapper. */
15575 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
15576 }
15577 else if (outer_automatic_var_p (r))
15578 r = process_outer_var_ref (r, complain);
15579
15580 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15581 /* If the original type was a reference, we'll be wrapped in
15582 the appropriate INDIRECT_REF. */
15583 r = convert_from_reference (r);
15584 RETURN (r);
15585 }
15586
15587 case VA_ARG_EXPR:
15588 {
15589 tree op0 = RECUR (TREE_OPERAND (t, 0));
15590 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15591 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
15592 }
15593
15594 case OFFSETOF_EXPR:
15595 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
15596 EXPR_LOCATION (t)));
15597
15598 case TRAIT_EXPR:
15599 {
15600 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15601 complain, in_decl);
15602
15603 tree type2 = TRAIT_EXPR_TYPE2 (t);
15604 if (type2 && TREE_CODE (type2) == TREE_LIST)
15605 type2 = RECUR (type2);
15606 else if (type2)
15607 type2 = tsubst (type2, args, complain, in_decl);
15608
15609 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15610 }
15611
15612 case STMT_EXPR:
15613 {
15614 tree old_stmt_expr = cur_stmt_expr;
15615 tree stmt_expr = begin_stmt_expr ();
15616
15617 cur_stmt_expr = stmt_expr;
15618 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15619 integral_constant_expression_p);
15620 stmt_expr = finish_stmt_expr (stmt_expr, false);
15621 cur_stmt_expr = old_stmt_expr;
15622
15623 /* If the resulting list of expression statement is empty,
15624 fold it further into void_node. */
15625 if (empty_expr_stmt_p (stmt_expr))
15626 stmt_expr = void_node;
15627
15628 RETURN (stmt_expr);
15629 }
15630
15631 case LAMBDA_EXPR:
15632 {
15633 tree r = build_lambda_expr ();
15634
15635 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15636 LAMBDA_EXPR_CLOSURE (r) = type;
15637 CLASSTYPE_LAMBDA_EXPR (type) = r;
15638
15639 LAMBDA_EXPR_LOCATION (r)
15640 = LAMBDA_EXPR_LOCATION (t);
15641 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15642 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15643 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15644 LAMBDA_EXPR_DISCRIMINATOR (r)
15645 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15646 /* For a function scope, we want to use tsubst so that we don't
15647 complain about referring to an auto function before its return
15648 type has been deduced. Otherwise, we want to use tsubst_copy so
15649 that we look up the existing field/parameter/variable rather
15650 than build a new one. */
15651 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15652 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15653 scope = tsubst (scope, args, complain, in_decl);
15654 else if (scope && TREE_CODE (scope) == PARM_DECL)
15655 {
15656 /* Look up the parameter we want directly, as tsubst_copy
15657 doesn't do what we need. */
15658 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15659 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15660 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15661 parm = DECL_CHAIN (parm);
15662 scope = parm;
15663 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15664 if (DECL_CONTEXT (scope) == NULL_TREE)
15665 DECL_CONTEXT (scope) = fn;
15666 }
15667 else
15668 scope = RECUR (scope);
15669 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15670 LAMBDA_EXPR_RETURN_TYPE (r)
15671 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15672
15673 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15674 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15675
15676 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15677 determine_visibility (TYPE_NAME (type));
15678 /* Now that we know visibility, instantiate the type so we have a
15679 declaration of the op() for later calls to lambda_function. */
15680 complete_type (type);
15681
15682 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15683
15684 RETURN (build_lambda_object (r));
15685 }
15686
15687 case TARGET_EXPR:
15688 /* We can get here for a constant initializer of non-dependent type.
15689 FIXME stop folding in cp_parser_initializer_clause. */
15690 {
15691 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15692 complain);
15693 RETURN (r);
15694 }
15695
15696 case TRANSACTION_EXPR:
15697 RETURN (tsubst_expr(t, args, complain, in_decl,
15698 integral_constant_expression_p));
15699
15700 case PAREN_EXPR:
15701 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15702
15703 case VEC_PERM_EXPR:
15704 {
15705 tree op0 = RECUR (TREE_OPERAND (t, 0));
15706 tree op1 = RECUR (TREE_OPERAND (t, 1));
15707 tree op2 = RECUR (TREE_OPERAND (t, 2));
15708 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
15709 complain));
15710 }
15711
15712 default:
15713 /* Handle Objective-C++ constructs, if appropriate. */
15714 {
15715 tree subst
15716 = objcp_tsubst_copy_and_build (t, args, complain,
15717 in_decl, /*function_p=*/false);
15718 if (subst)
15719 RETURN (subst);
15720 }
15721 RETURN (tsubst_copy (t, args, complain, in_decl));
15722 }
15723
15724 #undef RECUR
15725 #undef RETURN
15726 out:
15727 input_location = loc;
15728 return retval;
15729 }
15730
15731 /* Verify that the instantiated ARGS are valid. For type arguments,
15732 make sure that the type's linkage is ok. For non-type arguments,
15733 make sure they are constants if they are integral or enumerations.
15734 Emit an error under control of COMPLAIN, and return TRUE on error. */
15735
15736 static bool
15737 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15738 {
15739 if (dependent_template_arg_p (t))
15740 return false;
15741 if (ARGUMENT_PACK_P (t))
15742 {
15743 tree vec = ARGUMENT_PACK_ARGS (t);
15744 int len = TREE_VEC_LENGTH (vec);
15745 bool result = false;
15746 int i;
15747
15748 for (i = 0; i < len; ++i)
15749 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15750 result = true;
15751 return result;
15752 }
15753 else if (TYPE_P (t))
15754 {
15755 /* [basic.link]: A name with no linkage (notably, the name
15756 of a class or enumeration declared in a local scope)
15757 shall not be used to declare an entity with linkage.
15758 This implies that names with no linkage cannot be used as
15759 template arguments
15760
15761 DR 757 relaxes this restriction for C++0x. */
15762 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15763 : no_linkage_check (t, /*relaxed_p=*/false));
15764
15765 if (nt)
15766 {
15767 /* DR 488 makes use of a type with no linkage cause
15768 type deduction to fail. */
15769 if (complain & tf_error)
15770 {
15771 if (TYPE_ANONYMOUS_P (nt))
15772 error ("%qT is/uses anonymous type", t);
15773 else
15774 error ("template argument for %qD uses local type %qT",
15775 tmpl, t);
15776 }
15777 return true;
15778 }
15779 /* In order to avoid all sorts of complications, we do not
15780 allow variably-modified types as template arguments. */
15781 else if (variably_modified_type_p (t, NULL_TREE))
15782 {
15783 if (complain & tf_error)
15784 error ("%qT is a variably modified type", t);
15785 return true;
15786 }
15787 }
15788 /* Class template and alias template arguments should be OK. */
15789 else if (DECL_TYPE_TEMPLATE_P (t))
15790 ;
15791 /* A non-type argument of integral or enumerated type must be a
15792 constant. */
15793 else if (TREE_TYPE (t)
15794 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15795 && !REFERENCE_REF_P (t)
15796 && !TREE_CONSTANT (t))
15797 {
15798 if (complain & tf_error)
15799 error ("integral expression %qE is not constant", t);
15800 return true;
15801 }
15802 return false;
15803 }
15804
15805 static bool
15806 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15807 {
15808 int ix, len = DECL_NTPARMS (tmpl);
15809 bool result = false;
15810
15811 for (ix = 0; ix != len; ix++)
15812 {
15813 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15814 result = true;
15815 }
15816 if (result && (complain & tf_error))
15817 error (" trying to instantiate %qD", tmpl);
15818 return result;
15819 }
15820
15821 /* We're out of SFINAE context now, so generate diagnostics for the access
15822 errors we saw earlier when instantiating D from TMPL and ARGS. */
15823
15824 static void
15825 recheck_decl_substitution (tree d, tree tmpl, tree args)
15826 {
15827 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15828 tree type = TREE_TYPE (pattern);
15829 location_t loc = input_location;
15830
15831 push_access_scope (d);
15832 push_deferring_access_checks (dk_no_deferred);
15833 input_location = DECL_SOURCE_LOCATION (pattern);
15834 tsubst (type, args, tf_warning_or_error, d);
15835 input_location = loc;
15836 pop_deferring_access_checks ();
15837 pop_access_scope (d);
15838 }
15839
15840 /* Instantiate the indicated variable, function, or alias template TMPL with
15841 the template arguments in TARG_PTR. */
15842
15843 static tree
15844 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15845 {
15846 tree targ_ptr = orig_args;
15847 tree fndecl;
15848 tree gen_tmpl;
15849 tree spec;
15850 bool access_ok = true;
15851
15852 if (tmpl == error_mark_node)
15853 return error_mark_node;
15854
15855 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15856
15857 /* If this function is a clone, handle it specially. */
15858 if (DECL_CLONED_FUNCTION_P (tmpl))
15859 {
15860 tree spec;
15861 tree clone;
15862
15863 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15864 DECL_CLONED_FUNCTION. */
15865 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15866 targ_ptr, complain);
15867 if (spec == error_mark_node)
15868 return error_mark_node;
15869
15870 /* Look for the clone. */
15871 FOR_EACH_CLONE (clone, spec)
15872 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15873 return clone;
15874 /* We should always have found the clone by now. */
15875 gcc_unreachable ();
15876 return NULL_TREE;
15877 }
15878
15879 if (targ_ptr == error_mark_node)
15880 return error_mark_node;
15881
15882 /* Check to see if we already have this specialization. */
15883 gen_tmpl = most_general_template (tmpl);
15884 if (tmpl != gen_tmpl)
15885 /* The TMPL is a partial instantiation. To get a full set of
15886 arguments we must add the arguments used to perform the
15887 partial instantiation. */
15888 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15889 targ_ptr);
15890
15891 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15892 but it doesn't seem to be on the hot path. */
15893 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15894
15895 gcc_assert (tmpl == gen_tmpl
15896 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15897 == spec)
15898 || fndecl == NULL_TREE);
15899
15900 if (spec != NULL_TREE)
15901 {
15902 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15903 {
15904 if (complain & tf_error)
15905 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15906 return error_mark_node;
15907 }
15908 return spec;
15909 }
15910
15911 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15912 complain))
15913 return error_mark_node;
15914
15915 /* We are building a FUNCTION_DECL, during which the access of its
15916 parameters and return types have to be checked. However this
15917 FUNCTION_DECL which is the desired context for access checking
15918 is not built yet. We solve this chicken-and-egg problem by
15919 deferring all checks until we have the FUNCTION_DECL. */
15920 push_deferring_access_checks (dk_deferred);
15921
15922 /* Instantiation of the function happens in the context of the function
15923 template, not the context of the overload resolution we're doing. */
15924 push_to_top_level ();
15925 /* If there are dependent arguments, e.g. because we're doing partial
15926 ordering, make sure processing_template_decl stays set. */
15927 if (uses_template_parms (targ_ptr))
15928 ++processing_template_decl;
15929 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15930 {
15931 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
15932 complain, gen_tmpl, true);
15933 push_nested_class (ctx);
15934 }
15935
15936 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
15937
15938 if (VAR_P (pattern))
15939 {
15940 /* We need to determine if we're using a partial or explicit
15941 specialization now, because the type of the variable could be
15942 different. */
15943 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
15944 tree elt = most_specialized_partial_spec (tid, complain);
15945 if (elt == error_mark_node)
15946 pattern = error_mark_node;
15947 else if (elt)
15948 {
15949 tmpl = TREE_VALUE (elt);
15950 pattern = DECL_TEMPLATE_RESULT (tmpl);
15951 targ_ptr = TREE_PURPOSE (elt);
15952 }
15953 }
15954
15955 /* Substitute template parameters to obtain the specialization. */
15956 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
15957 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15958 pop_nested_class ();
15959 pop_from_top_level ();
15960
15961 if (fndecl == error_mark_node)
15962 {
15963 pop_deferring_access_checks ();
15964 return error_mark_node;
15965 }
15966
15967 /* The DECL_TI_TEMPLATE should always be the immediate parent
15968 template, not the most general template. */
15969 DECL_TI_TEMPLATE (fndecl) = tmpl;
15970
15971 /* Now we know the specialization, compute access previously
15972 deferred. */
15973 push_access_scope (fndecl);
15974 if (!perform_deferred_access_checks (complain))
15975 access_ok = false;
15976 pop_access_scope (fndecl);
15977 pop_deferring_access_checks ();
15978
15979 /* If we've just instantiated the main entry point for a function,
15980 instantiate all the alternate entry points as well. We do this
15981 by cloning the instantiation of the main entry point, not by
15982 instantiating the template clones. */
15983 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15984 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15985
15986 if (!access_ok)
15987 {
15988 if (!(complain & tf_error))
15989 {
15990 /* Remember to reinstantiate when we're out of SFINAE so the user
15991 can see the errors. */
15992 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15993 }
15994 return error_mark_node;
15995 }
15996 return fndecl;
15997 }
15998
15999 /* Wrapper for instantiate_template_1. */
16000
16001 tree
16002 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
16003 {
16004 tree ret;
16005 timevar_push (TV_TEMPLATE_INST);
16006 ret = instantiate_template_1 (tmpl, orig_args, complain);
16007 timevar_pop (TV_TEMPLATE_INST);
16008 return ret;
16009 }
16010
16011 /* Instantiate the alias template TMPL with ARGS. Also push a template
16012 instantiation level, which instantiate_template doesn't do because
16013 functions and variables have sufficient context established by the
16014 callers. */
16015
16016 static tree
16017 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
16018 {
16019 struct pending_template *old_last_pend = last_pending_template;
16020 struct tinst_level *old_error_tinst = last_error_tinst_level;
16021 if (tmpl == error_mark_node || args == error_mark_node)
16022 return error_mark_node;
16023 tree tinst = build_tree_list (tmpl, args);
16024 if (!push_tinst_level (tinst))
16025 {
16026 ggc_free (tinst);
16027 return error_mark_node;
16028 }
16029
16030 args =
16031 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
16032 args, tmpl, complain,
16033 /*require_all_args=*/true,
16034 /*use_default_args=*/true);
16035
16036 tree r = instantiate_template (tmpl, args, complain);
16037 pop_tinst_level ();
16038 /* We can't free this if a pending_template entry or last_error_tinst_level
16039 is pointing at it. */
16040 if (last_pending_template == old_last_pend
16041 && last_error_tinst_level == old_error_tinst)
16042 ggc_free (tinst);
16043
16044 return r;
16045 }
16046
16047 /* PARM is a template parameter pack for FN. Returns true iff
16048 PARM is used in a deducible way in the argument list of FN. */
16049
16050 static bool
16051 pack_deducible_p (tree parm, tree fn)
16052 {
16053 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
16054 for (; t; t = TREE_CHAIN (t))
16055 {
16056 tree type = TREE_VALUE (t);
16057 tree packs;
16058 if (!PACK_EXPANSION_P (type))
16059 continue;
16060 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16061 packs; packs = TREE_CHAIN (packs))
16062 if (template_args_equal (TREE_VALUE (packs), parm))
16063 {
16064 /* The template parameter pack is used in a function parameter
16065 pack. If this is the end of the parameter list, the
16066 template parameter pack is deducible. */
16067 if (TREE_CHAIN (t) == void_list_node)
16068 return true;
16069 else
16070 /* Otherwise, not. Well, it could be deduced from
16071 a non-pack parameter, but doing so would end up with
16072 a deduction mismatch, so don't bother. */
16073 return false;
16074 }
16075 }
16076 /* The template parameter pack isn't used in any function parameter
16077 packs, but it might be used deeper, e.g. tuple<Args...>. */
16078 return true;
16079 }
16080
16081 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16082 NARGS elements of the arguments that are being used when calling
16083 it. TARGS is a vector into which the deduced template arguments
16084 are placed.
16085
16086 Returns either a FUNCTION_DECL for the matching specialization of FN or
16087 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16088 true, diagnostics will be printed to explain why it failed.
16089
16090 If FN is a conversion operator, or we are trying to produce a specific
16091 specialization, RETURN_TYPE is the return type desired.
16092
16093 The EXPLICIT_TARGS are explicit template arguments provided via a
16094 template-id.
16095
16096 The parameter STRICT is one of:
16097
16098 DEDUCE_CALL:
16099 We are deducing arguments for a function call, as in
16100 [temp.deduct.call].
16101
16102 DEDUCE_CONV:
16103 We are deducing arguments for a conversion function, as in
16104 [temp.deduct.conv].
16105
16106 DEDUCE_EXACT:
16107 We are deducing arguments when doing an explicit instantiation
16108 as in [temp.explicit], when determining an explicit specialization
16109 as in [temp.expl.spec], or when taking the address of a function
16110 template, as in [temp.deduct.funcaddr]. */
16111
16112 tree
16113 fn_type_unification (tree fn,
16114 tree explicit_targs,
16115 tree targs,
16116 const tree *args,
16117 unsigned int nargs,
16118 tree return_type,
16119 unification_kind_t strict,
16120 int flags,
16121 bool explain_p,
16122 bool decltype_p)
16123 {
16124 tree parms;
16125 tree fntype;
16126 tree decl = NULL_TREE;
16127 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
16128 bool ok;
16129 static int deduction_depth;
16130 struct pending_template *old_last_pend = last_pending_template;
16131 struct tinst_level *old_error_tinst = last_error_tinst_level;
16132 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
16133 tree tinst;
16134 tree r = error_mark_node;
16135
16136 if (decltype_p)
16137 complain |= tf_decltype;
16138
16139 /* In C++0x, it's possible to have a function template whose type depends
16140 on itself recursively. This is most obvious with decltype, but can also
16141 occur with enumeration scope (c++/48969). So we need to catch infinite
16142 recursion and reject the substitution at deduction time; this function
16143 will return error_mark_node for any repeated substitution.
16144
16145 This also catches excessive recursion such as when f<N> depends on
16146 f<N-1> across all integers, and returns error_mark_node for all the
16147 substitutions back up to the initial one.
16148
16149 This is, of course, not reentrant. */
16150 if (excessive_deduction_depth)
16151 return error_mark_node;
16152 tinst = build_tree_list (fn, NULL_TREE);
16153 ++deduction_depth;
16154
16155 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
16156
16157 fntype = TREE_TYPE (fn);
16158 if (explicit_targs)
16159 {
16160 /* [temp.deduct]
16161
16162 The specified template arguments must match the template
16163 parameters in kind (i.e., type, nontype, template), and there
16164 must not be more arguments than there are parameters;
16165 otherwise type deduction fails.
16166
16167 Nontype arguments must match the types of the corresponding
16168 nontype template parameters, or must be convertible to the
16169 types of the corresponding nontype parameters as specified in
16170 _temp.arg.nontype_, otherwise type deduction fails.
16171
16172 All references in the function type of the function template
16173 to the corresponding template parameters are replaced by the
16174 specified template argument values. If a substitution in a
16175 template parameter or in the function type of the function
16176 template results in an invalid type, type deduction fails. */
16177 int i, len = TREE_VEC_LENGTH (tparms);
16178 location_t loc = input_location;
16179 bool incomplete = false;
16180
16181 /* Adjust any explicit template arguments before entering the
16182 substitution context. */
16183 explicit_targs
16184 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
16185 complain,
16186 /*require_all_args=*/false,
16187 /*use_default_args=*/false));
16188 if (explicit_targs == error_mark_node)
16189 goto fail;
16190
16191 /* Substitute the explicit args into the function type. This is
16192 necessary so that, for instance, explicitly declared function
16193 arguments can match null pointed constants. If we were given
16194 an incomplete set of explicit args, we must not do semantic
16195 processing during substitution as we could create partial
16196 instantiations. */
16197 for (i = 0; i < len; i++)
16198 {
16199 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16200 bool parameter_pack = false;
16201 tree targ = TREE_VEC_ELT (explicit_targs, i);
16202
16203 /* Dig out the actual parm. */
16204 if (TREE_CODE (parm) == TYPE_DECL
16205 || TREE_CODE (parm) == TEMPLATE_DECL)
16206 {
16207 parm = TREE_TYPE (parm);
16208 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
16209 }
16210 else if (TREE_CODE (parm) == PARM_DECL)
16211 {
16212 parm = DECL_INITIAL (parm);
16213 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
16214 }
16215
16216 if (!parameter_pack && targ == NULL_TREE)
16217 /* No explicit argument for this template parameter. */
16218 incomplete = true;
16219
16220 if (parameter_pack && pack_deducible_p (parm, fn))
16221 {
16222 /* Mark the argument pack as "incomplete". We could
16223 still deduce more arguments during unification.
16224 We remove this mark in type_unification_real. */
16225 if (targ)
16226 {
16227 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
16228 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
16229 = ARGUMENT_PACK_ARGS (targ);
16230 }
16231
16232 /* We have some incomplete argument packs. */
16233 incomplete = true;
16234 }
16235 }
16236
16237 TREE_VALUE (tinst) = explicit_targs;
16238 if (!push_tinst_level (tinst))
16239 {
16240 excessive_deduction_depth = true;
16241 goto fail;
16242 }
16243 processing_template_decl += incomplete;
16244 input_location = DECL_SOURCE_LOCATION (fn);
16245 /* Ignore any access checks; we'll see them again in
16246 instantiate_template and they might have the wrong
16247 access path at this point. */
16248 push_deferring_access_checks (dk_deferred);
16249 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
16250 complain | tf_partial, NULL_TREE);
16251 pop_deferring_access_checks ();
16252 input_location = loc;
16253 processing_template_decl -= incomplete;
16254 pop_tinst_level ();
16255
16256 if (fntype == error_mark_node)
16257 goto fail;
16258
16259 /* Place the explicitly specified arguments in TARGS. */
16260 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
16261 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
16262 }
16263
16264 /* Never do unification on the 'this' parameter. */
16265 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
16266
16267 if (return_type)
16268 {
16269 tree *new_args;
16270
16271 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
16272 new_args = XALLOCAVEC (tree, nargs + 1);
16273 new_args[0] = return_type;
16274 memcpy (new_args + 1, args, nargs * sizeof (tree));
16275 args = new_args;
16276 ++nargs;
16277 }
16278
16279 /* We allow incomplete unification without an error message here
16280 because the standard doesn't seem to explicitly prohibit it. Our
16281 callers must be ready to deal with unification failures in any
16282 event. */
16283
16284 TREE_VALUE (tinst) = targs;
16285 /* If we aren't explaining yet, push tinst context so we can see where
16286 any errors (e.g. from class instantiations triggered by instantiation
16287 of default template arguments) come from. If we are explaining, this
16288 context is redundant. */
16289 if (!explain_p && !push_tinst_level (tinst))
16290 {
16291 excessive_deduction_depth = true;
16292 goto fail;
16293 }
16294
16295 /* type_unification_real will pass back any access checks from default
16296 template argument substitution. */
16297 vec<deferred_access_check, va_gc> *checks;
16298 checks = NULL;
16299
16300 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16301 targs, parms, args, nargs, /*subr=*/0,
16302 strict, flags, &checks, explain_p);
16303 if (!explain_p)
16304 pop_tinst_level ();
16305 if (!ok)
16306 goto fail;
16307
16308 /* Now that we have bindings for all of the template arguments,
16309 ensure that the arguments deduced for the template template
16310 parameters have compatible template parameter lists. We cannot
16311 check this property before we have deduced all template
16312 arguments, because the template parameter types of a template
16313 template parameter might depend on prior template parameters
16314 deduced after the template template parameter. The following
16315 ill-formed example illustrates this issue:
16316
16317 template<typename T, template<T> class C> void f(C<5>, T);
16318
16319 template<int N> struct X {};
16320
16321 void g() {
16322 f(X<5>(), 5l); // error: template argument deduction fails
16323 }
16324
16325 The template parameter list of 'C' depends on the template type
16326 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16327 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16328 time that we deduce 'C'. */
16329 if (!template_template_parm_bindings_ok_p
16330 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16331 {
16332 unify_inconsistent_template_template_parameters (explain_p);
16333 goto fail;
16334 }
16335
16336 /* All is well so far. Now, check:
16337
16338 [temp.deduct]
16339
16340 When all template arguments have been deduced, all uses of
16341 template parameters in nondeduced contexts are replaced with
16342 the corresponding deduced argument values. If the
16343 substitution results in an invalid type, as described above,
16344 type deduction fails. */
16345 TREE_VALUE (tinst) = targs;
16346 if (!push_tinst_level (tinst))
16347 {
16348 excessive_deduction_depth = true;
16349 goto fail;
16350 }
16351
16352 /* Also collect access checks from the instantiation. */
16353 reopen_deferring_access_checks (checks);
16354
16355 decl = instantiate_template (fn, targs, complain);
16356
16357 checks = get_deferred_access_checks ();
16358 pop_deferring_access_checks ();
16359
16360 pop_tinst_level ();
16361
16362 if (decl == error_mark_node)
16363 goto fail;
16364
16365 /* Now perform any access checks encountered during substitution. */
16366 push_access_scope (decl);
16367 ok = perform_access_checks (checks, complain);
16368 pop_access_scope (decl);
16369 if (!ok)
16370 goto fail;
16371
16372 /* If we're looking for an exact match, check that what we got
16373 is indeed an exact match. It might not be if some template
16374 parameters are used in non-deduced contexts. But don't check
16375 for an exact match if we have dependent template arguments;
16376 in that case we're doing partial ordering, and we already know
16377 that we have two candidates that will provide the actual type. */
16378 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16379 {
16380 tree substed = TREE_TYPE (decl);
16381 unsigned int i;
16382
16383 tree sarg
16384 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16385 if (return_type)
16386 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16387 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16388 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16389 {
16390 unify_type_mismatch (explain_p, args[i],
16391 TREE_VALUE (sarg));
16392 goto fail;
16393 }
16394 }
16395
16396 r = decl;
16397
16398 fail:
16399 --deduction_depth;
16400 if (excessive_deduction_depth)
16401 {
16402 if (deduction_depth == 0)
16403 /* Reset once we're all the way out. */
16404 excessive_deduction_depth = false;
16405 }
16406
16407 /* We can't free this if a pending_template entry or last_error_tinst_level
16408 is pointing at it. */
16409 if (last_pending_template == old_last_pend
16410 && last_error_tinst_level == old_error_tinst)
16411 ggc_free (tinst);
16412
16413 return r;
16414 }
16415
16416 /* Adjust types before performing type deduction, as described in
16417 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16418 sections are symmetric. PARM is the type of a function parameter
16419 or the return type of the conversion function. ARG is the type of
16420 the argument passed to the call, or the type of the value
16421 initialized with the result of the conversion function.
16422 ARG_EXPR is the original argument expression, which may be null. */
16423
16424 static int
16425 maybe_adjust_types_for_deduction (unification_kind_t strict,
16426 tree* parm,
16427 tree* arg,
16428 tree arg_expr)
16429 {
16430 int result = 0;
16431
16432 switch (strict)
16433 {
16434 case DEDUCE_CALL:
16435 break;
16436
16437 case DEDUCE_CONV:
16438 {
16439 /* Swap PARM and ARG throughout the remainder of this
16440 function; the handling is precisely symmetric since PARM
16441 will initialize ARG rather than vice versa. */
16442 tree* temp = parm;
16443 parm = arg;
16444 arg = temp;
16445 break;
16446 }
16447
16448 case DEDUCE_EXACT:
16449 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16450 too, but here handle it by stripping the reference from PARM
16451 rather than by adding it to ARG. */
16452 if (TREE_CODE (*parm) == REFERENCE_TYPE
16453 && TYPE_REF_IS_RVALUE (*parm)
16454 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16455 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16456 && TREE_CODE (*arg) == REFERENCE_TYPE
16457 && !TYPE_REF_IS_RVALUE (*arg))
16458 *parm = TREE_TYPE (*parm);
16459 /* Nothing else to do in this case. */
16460 return 0;
16461
16462 default:
16463 gcc_unreachable ();
16464 }
16465
16466 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16467 {
16468 /* [temp.deduct.call]
16469
16470 If P is not a reference type:
16471
16472 --If A is an array type, the pointer type produced by the
16473 array-to-pointer standard conversion (_conv.array_) is
16474 used in place of A for type deduction; otherwise,
16475
16476 --If A is a function type, the pointer type produced by
16477 the function-to-pointer standard conversion
16478 (_conv.func_) is used in place of A for type deduction;
16479 otherwise,
16480
16481 --If A is a cv-qualified type, the top level
16482 cv-qualifiers of A's type are ignored for type
16483 deduction. */
16484 if (TREE_CODE (*arg) == ARRAY_TYPE)
16485 *arg = build_pointer_type (TREE_TYPE (*arg));
16486 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16487 *arg = build_pointer_type (*arg);
16488 else
16489 *arg = TYPE_MAIN_VARIANT (*arg);
16490 }
16491
16492 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16493 of the form T&&, where T is a template parameter, and the argument
16494 is an lvalue, T is deduced as A& */
16495 if (TREE_CODE (*parm) == REFERENCE_TYPE
16496 && TYPE_REF_IS_RVALUE (*parm)
16497 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16498 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16499 && (arg_expr ? real_lvalue_p (arg_expr)
16500 /* try_one_overload doesn't provide an arg_expr, but
16501 functions are always lvalues. */
16502 : TREE_CODE (*arg) == FUNCTION_TYPE))
16503 *arg = build_reference_type (*arg);
16504
16505 /* [temp.deduct.call]
16506
16507 If P is a cv-qualified type, the top level cv-qualifiers
16508 of P's type are ignored for type deduction. If P is a
16509 reference type, the type referred to by P is used for
16510 type deduction. */
16511 *parm = TYPE_MAIN_VARIANT (*parm);
16512 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16513 {
16514 *parm = TREE_TYPE (*parm);
16515 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16516 }
16517
16518 /* DR 322. For conversion deduction, remove a reference type on parm
16519 too (which has been swapped into ARG). */
16520 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16521 *arg = TREE_TYPE (*arg);
16522
16523 return result;
16524 }
16525
16526 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16527 template which does contain any deducible template parameters; check if
16528 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16529 unify_one_argument. */
16530
16531 static int
16532 check_non_deducible_conversion (tree parm, tree arg, int strict,
16533 int flags, bool explain_p)
16534 {
16535 tree type;
16536
16537 if (!TYPE_P (arg))
16538 type = TREE_TYPE (arg);
16539 else
16540 type = arg;
16541
16542 if (same_type_p (parm, type))
16543 return unify_success (explain_p);
16544
16545 if (strict == DEDUCE_CONV)
16546 {
16547 if (can_convert_arg (type, parm, NULL_TREE, flags,
16548 explain_p ? tf_warning_or_error : tf_none))
16549 return unify_success (explain_p);
16550 }
16551 else if (strict != DEDUCE_EXACT)
16552 {
16553 if (can_convert_arg (parm, type,
16554 TYPE_P (arg) ? NULL_TREE : arg,
16555 flags, explain_p ? tf_warning_or_error : tf_none))
16556 return unify_success (explain_p);
16557 }
16558
16559 if (strict == DEDUCE_EXACT)
16560 return unify_type_mismatch (explain_p, parm, arg);
16561 else
16562 return unify_arg_conversion (explain_p, parm, type, arg);
16563 }
16564
16565 static bool uses_deducible_template_parms (tree type);
16566
16567 /* Returns true iff the expression EXPR is one from which a template
16568 argument can be deduced. In other words, if it's an undecorated
16569 use of a template non-type parameter. */
16570
16571 static bool
16572 deducible_expression (tree expr)
16573 {
16574 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16575 }
16576
16577 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16578 deducible way; that is, if it has a max value of <PARM> - 1. */
16579
16580 static bool
16581 deducible_array_bound (tree domain)
16582 {
16583 if (domain == NULL_TREE)
16584 return false;
16585
16586 tree max = TYPE_MAX_VALUE (domain);
16587 if (TREE_CODE (max) != MINUS_EXPR)
16588 return false;
16589
16590 return deducible_expression (TREE_OPERAND (max, 0));
16591 }
16592
16593 /* Returns true iff the template arguments ARGS use a template parameter
16594 in a deducible way. */
16595
16596 static bool
16597 deducible_template_args (tree args)
16598 {
16599 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16600 {
16601 bool deducible;
16602 tree elt = TREE_VEC_ELT (args, i);
16603 if (ARGUMENT_PACK_P (elt))
16604 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16605 else
16606 {
16607 if (PACK_EXPANSION_P (elt))
16608 elt = PACK_EXPANSION_PATTERN (elt);
16609 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16610 deducible = true;
16611 else if (TYPE_P (elt))
16612 deducible = uses_deducible_template_parms (elt);
16613 else
16614 deducible = deducible_expression (elt);
16615 }
16616 if (deducible)
16617 return true;
16618 }
16619 return false;
16620 }
16621
16622 /* Returns true iff TYPE contains any deducible references to template
16623 parameters, as per 14.8.2.5. */
16624
16625 static bool
16626 uses_deducible_template_parms (tree type)
16627 {
16628 if (PACK_EXPANSION_P (type))
16629 type = PACK_EXPANSION_PATTERN (type);
16630
16631 /* T
16632 cv-list T
16633 TT<T>
16634 TT<i>
16635 TT<> */
16636 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16637 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16638 return true;
16639
16640 /* T*
16641 T&
16642 T&& */
16643 if (POINTER_TYPE_P (type))
16644 return uses_deducible_template_parms (TREE_TYPE (type));
16645
16646 /* T[integer-constant ]
16647 type [i] */
16648 if (TREE_CODE (type) == ARRAY_TYPE)
16649 return (uses_deducible_template_parms (TREE_TYPE (type))
16650 || deducible_array_bound (TYPE_DOMAIN (type)));
16651
16652 /* T type ::*
16653 type T::*
16654 T T::*
16655 T (type ::*)()
16656 type (T::*)()
16657 type (type ::*)(T)
16658 type (T::*)(T)
16659 T (type ::*)(T)
16660 T (T::*)()
16661 T (T::*)(T) */
16662 if (TYPE_PTRMEM_P (type))
16663 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16664 || (uses_deducible_template_parms
16665 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16666
16667 /* template-name <T> (where template-name refers to a class template)
16668 template-name <i> (where template-name refers to a class template) */
16669 if (CLASS_TYPE_P (type)
16670 && CLASSTYPE_TEMPLATE_INFO (type)
16671 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16672 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16673 (CLASSTYPE_TI_ARGS (type)));
16674
16675 /* type (T)
16676 T()
16677 T(T) */
16678 if (TREE_CODE (type) == FUNCTION_TYPE
16679 || TREE_CODE (type) == METHOD_TYPE)
16680 {
16681 if (uses_deducible_template_parms (TREE_TYPE (type)))
16682 return true;
16683 tree parm = TYPE_ARG_TYPES (type);
16684 if (TREE_CODE (type) == METHOD_TYPE)
16685 parm = TREE_CHAIN (parm);
16686 for (; parm; parm = TREE_CHAIN (parm))
16687 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16688 return true;
16689 }
16690
16691 return false;
16692 }
16693
16694 /* Subroutine of type_unification_real and unify_pack_expansion to
16695 handle unification of a single P/A pair. Parameters are as
16696 for those functions. */
16697
16698 static int
16699 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16700 int subr, unification_kind_t strict,
16701 bool explain_p)
16702 {
16703 tree arg_expr = NULL_TREE;
16704 int arg_strict;
16705
16706 if (arg == error_mark_node || parm == error_mark_node)
16707 return unify_invalid (explain_p);
16708 if (arg == unknown_type_node)
16709 /* We can't deduce anything from this, but we might get all the
16710 template args from other function args. */
16711 return unify_success (explain_p);
16712
16713 /* Implicit conversions (Clause 4) will be performed on a function
16714 argument to convert it to the type of the corresponding function
16715 parameter if the parameter type contains no template-parameters that
16716 participate in template argument deduction. */
16717 if (strict != DEDUCE_EXACT
16718 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16719 /* For function parameters with no deducible template parameters,
16720 just return. We'll check non-dependent conversions later. */
16721 return unify_success (explain_p);
16722
16723 switch (strict)
16724 {
16725 case DEDUCE_CALL:
16726 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16727 | UNIFY_ALLOW_MORE_CV_QUAL
16728 | UNIFY_ALLOW_DERIVED);
16729 break;
16730
16731 case DEDUCE_CONV:
16732 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16733 break;
16734
16735 case DEDUCE_EXACT:
16736 arg_strict = UNIFY_ALLOW_NONE;
16737 break;
16738
16739 default:
16740 gcc_unreachable ();
16741 }
16742
16743 /* We only do these transformations if this is the top-level
16744 parameter_type_list in a call or declaration matching; in other
16745 situations (nested function declarators, template argument lists) we
16746 won't be comparing a type to an expression, and we don't do any type
16747 adjustments. */
16748 if (!subr)
16749 {
16750 if (!TYPE_P (arg))
16751 {
16752 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16753 if (type_unknown_p (arg))
16754 {
16755 /* [temp.deduct.type] A template-argument can be
16756 deduced from a pointer to function or pointer
16757 to member function argument if the set of
16758 overloaded functions does not contain function
16759 templates and at most one of a set of
16760 overloaded functions provides a unique
16761 match. */
16762
16763 if (resolve_overloaded_unification
16764 (tparms, targs, parm, arg, strict,
16765 arg_strict, explain_p))
16766 return unify_success (explain_p);
16767 return unify_overload_resolution_failure (explain_p, arg);
16768 }
16769
16770 arg_expr = arg;
16771 arg = unlowered_expr_type (arg);
16772 if (arg == error_mark_node)
16773 return unify_invalid (explain_p);
16774 }
16775
16776 arg_strict |=
16777 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16778 }
16779 else
16780 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16781 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
16782 return unify_template_argument_mismatch (explain_p, parm, arg);
16783
16784 /* For deduction from an init-list we need the actual list. */
16785 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16786 arg = arg_expr;
16787 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16788 }
16789
16790 /* Most parms like fn_type_unification.
16791
16792 If SUBR is 1, we're being called recursively (to unify the
16793 arguments of a function or method parameter of a function
16794 template).
16795
16796 CHECKS is a pointer to a vector of access checks encountered while
16797 substituting default template arguments. */
16798
16799 static int
16800 type_unification_real (tree tparms,
16801 tree targs,
16802 tree xparms,
16803 const tree *xargs,
16804 unsigned int xnargs,
16805 int subr,
16806 unification_kind_t strict,
16807 int flags,
16808 vec<deferred_access_check, va_gc> **checks,
16809 bool explain_p)
16810 {
16811 tree parm, arg;
16812 int i;
16813 int ntparms = TREE_VEC_LENGTH (tparms);
16814 int saw_undeduced = 0;
16815 tree parms;
16816 const tree *args;
16817 unsigned int nargs;
16818 unsigned int ia;
16819
16820 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16821 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16822 gcc_assert (ntparms > 0);
16823
16824 /* Reset the number of non-defaulted template arguments contained
16825 in TARGS. */
16826 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16827
16828 again:
16829 parms = xparms;
16830 args = xargs;
16831 nargs = xnargs;
16832
16833 ia = 0;
16834 while (parms && parms != void_list_node
16835 && ia < nargs)
16836 {
16837 parm = TREE_VALUE (parms);
16838
16839 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16840 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16841 /* For a function parameter pack that occurs at the end of the
16842 parameter-declaration-list, the type A of each remaining
16843 argument of the call is compared with the type P of the
16844 declarator-id of the function parameter pack. */
16845 break;
16846
16847 parms = TREE_CHAIN (parms);
16848
16849 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16850 /* For a function parameter pack that does not occur at the
16851 end of the parameter-declaration-list, the type of the
16852 parameter pack is a non-deduced context. */
16853 continue;
16854
16855 arg = args[ia];
16856 ++ia;
16857
16858 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16859 explain_p))
16860 return 1;
16861 }
16862
16863 if (parms
16864 && parms != void_list_node
16865 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16866 {
16867 /* Unify the remaining arguments with the pack expansion type. */
16868 tree argvec;
16869 tree parmvec = make_tree_vec (1);
16870
16871 /* Allocate a TREE_VEC and copy in all of the arguments */
16872 argvec = make_tree_vec (nargs - ia);
16873 for (i = 0; ia < nargs; ++ia, ++i)
16874 TREE_VEC_ELT (argvec, i) = args[ia];
16875
16876 /* Copy the parameter into parmvec. */
16877 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16878 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16879 /*subr=*/subr, explain_p))
16880 return 1;
16881
16882 /* Advance to the end of the list of parameters. */
16883 parms = TREE_CHAIN (parms);
16884 }
16885
16886 /* Fail if we've reached the end of the parm list, and more args
16887 are present, and the parm list isn't variadic. */
16888 if (ia < nargs && parms == void_list_node)
16889 return unify_too_many_arguments (explain_p, nargs, ia);
16890 /* Fail if parms are left and they don't have default values and
16891 they aren't all deduced as empty packs (c++/57397). This is
16892 consistent with sufficient_parms_p. */
16893 if (parms && parms != void_list_node
16894 && TREE_PURPOSE (parms) == NULL_TREE)
16895 {
16896 unsigned int count = nargs;
16897 tree p = parms;
16898 bool type_pack_p;
16899 do
16900 {
16901 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
16902 if (!type_pack_p)
16903 count++;
16904 p = TREE_CHAIN (p);
16905 }
16906 while (p && p != void_list_node);
16907 if (count != nargs)
16908 return unify_too_few_arguments (explain_p, ia, count,
16909 type_pack_p);
16910 }
16911
16912 if (!subr)
16913 {
16914 tsubst_flags_t complain = (explain_p
16915 ? tf_warning_or_error
16916 : tf_none);
16917
16918 for (i = 0; i < ntparms; i++)
16919 {
16920 tree targ = TREE_VEC_ELT (targs, i);
16921 tree tparm = TREE_VEC_ELT (tparms, i);
16922
16923 /* Clear the "incomplete" flags on all argument packs now so that
16924 substituting them into later default arguments works. */
16925 if (targ && ARGUMENT_PACK_P (targ))
16926 {
16927 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16928 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16929 }
16930
16931 if (targ || tparm == error_mark_node)
16932 continue;
16933 tparm = TREE_VALUE (tparm);
16934
16935 /* If this is an undeduced nontype parameter that depends on
16936 a type parameter, try another pass; its type may have been
16937 deduced from a later argument than the one from which
16938 this parameter can be deduced. */
16939 if (TREE_CODE (tparm) == PARM_DECL
16940 && uses_template_parms (TREE_TYPE (tparm))
16941 && saw_undeduced < 2)
16942 {
16943 saw_undeduced = 1;
16944 continue;
16945 }
16946
16947 /* Core issue #226 (C++0x) [temp.deduct]:
16948
16949 If a template argument has not been deduced, its
16950 default template argument, if any, is used.
16951
16952 When we are in C++98 mode, TREE_PURPOSE will either
16953 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16954 to explicitly check cxx_dialect here. */
16955 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16956 /* OK, there is a default argument. Wait until after the
16957 conversion check to do substitution. */
16958 continue;
16959
16960 /* If the type parameter is a parameter pack, then it will
16961 be deduced to an empty parameter pack. */
16962 if (template_parameter_pack_p (tparm))
16963 {
16964 tree arg;
16965
16966 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16967 {
16968 arg = make_node (NONTYPE_ARGUMENT_PACK);
16969 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16970 TREE_CONSTANT (arg) = 1;
16971 }
16972 else
16973 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16974
16975 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16976
16977 TREE_VEC_ELT (targs, i) = arg;
16978 continue;
16979 }
16980
16981 return unify_parameter_deduction_failure (explain_p, tparm);
16982 }
16983
16984 /* DR 1391: All parameters have args, now check non-dependent parms for
16985 convertibility. */
16986 if (saw_undeduced < 2)
16987 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
16988 parms && parms != void_list_node && ia < nargs; )
16989 {
16990 parm = TREE_VALUE (parms);
16991
16992 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16993 && (!TREE_CHAIN (parms)
16994 || TREE_CHAIN (parms) == void_list_node))
16995 /* For a function parameter pack that occurs at the end of the
16996 parameter-declaration-list, the type A of each remaining
16997 argument of the call is compared with the type P of the
16998 declarator-id of the function parameter pack. */
16999 break;
17000
17001 parms = TREE_CHAIN (parms);
17002
17003 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
17004 /* For a function parameter pack that does not occur at the
17005 end of the parameter-declaration-list, the type of the
17006 parameter pack is a non-deduced context. */
17007 continue;
17008
17009 arg = args[ia];
17010 ++ia;
17011
17012 if (uses_template_parms (parm))
17013 continue;
17014 if (check_non_deducible_conversion (parm, arg, strict, flags,
17015 explain_p))
17016 return 1;
17017 }
17018
17019 /* Now substitute into the default template arguments. */
17020 for (i = 0; i < ntparms; i++)
17021 {
17022 tree targ = TREE_VEC_ELT (targs, i);
17023 tree tparm = TREE_VEC_ELT (tparms, i);
17024
17025 if (targ || tparm == error_mark_node)
17026 continue;
17027 tree parm = TREE_VALUE (tparm);
17028
17029 if (TREE_CODE (parm) == PARM_DECL
17030 && uses_template_parms (TREE_TYPE (parm))
17031 && saw_undeduced < 2)
17032 continue;
17033
17034 tree arg = TREE_PURPOSE (tparm);
17035 reopen_deferring_access_checks (*checks);
17036 location_t save_loc = input_location;
17037 if (DECL_P (parm))
17038 input_location = DECL_SOURCE_LOCATION (parm);
17039 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
17040 arg = convert_template_argument (parm, arg, targs, complain,
17041 i, NULL_TREE);
17042 input_location = save_loc;
17043 *checks = get_deferred_access_checks ();
17044 pop_deferring_access_checks ();
17045 if (arg == error_mark_node)
17046 return 1;
17047 else
17048 {
17049 TREE_VEC_ELT (targs, i) = arg;
17050 /* The position of the first default template argument,
17051 is also the number of non-defaulted arguments in TARGS.
17052 Record that. */
17053 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17054 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
17055 continue;
17056 }
17057 }
17058
17059 if (saw_undeduced++ == 1)
17060 goto again;
17061 }
17062 #ifdef ENABLE_CHECKING
17063 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17064 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
17065 #endif
17066
17067 return unify_success (explain_p);
17068 }
17069
17070 /* Subroutine of type_unification_real. Args are like the variables
17071 at the call site. ARG is an overloaded function (or template-id);
17072 we try deducing template args from each of the overloads, and if
17073 only one succeeds, we go with that. Modifies TARGS and returns
17074 true on success. */
17075
17076 static bool
17077 resolve_overloaded_unification (tree tparms,
17078 tree targs,
17079 tree parm,
17080 tree arg,
17081 unification_kind_t strict,
17082 int sub_strict,
17083 bool explain_p)
17084 {
17085 tree tempargs = copy_node (targs);
17086 int good = 0;
17087 tree goodfn = NULL_TREE;
17088 bool addr_p;
17089
17090 if (TREE_CODE (arg) == ADDR_EXPR)
17091 {
17092 arg = TREE_OPERAND (arg, 0);
17093 addr_p = true;
17094 }
17095 else
17096 addr_p = false;
17097
17098 if (TREE_CODE (arg) == COMPONENT_REF)
17099 /* Handle `&x' where `x' is some static or non-static member
17100 function name. */
17101 arg = TREE_OPERAND (arg, 1);
17102
17103 if (TREE_CODE (arg) == OFFSET_REF)
17104 arg = TREE_OPERAND (arg, 1);
17105
17106 /* Strip baselink information. */
17107 if (BASELINK_P (arg))
17108 arg = BASELINK_FUNCTIONS (arg);
17109
17110 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
17111 {
17112 /* If we got some explicit template args, we need to plug them into
17113 the affected templates before we try to unify, in case the
17114 explicit args will completely resolve the templates in question. */
17115
17116 int ok = 0;
17117 tree expl_subargs = TREE_OPERAND (arg, 1);
17118 arg = TREE_OPERAND (arg, 0);
17119
17120 for (; arg; arg = OVL_NEXT (arg))
17121 {
17122 tree fn = OVL_CURRENT (arg);
17123 tree subargs, elem;
17124
17125 if (TREE_CODE (fn) != TEMPLATE_DECL)
17126 continue;
17127
17128 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17129 expl_subargs, NULL_TREE, tf_none,
17130 /*require_all_args=*/true,
17131 /*use_default_args=*/true);
17132 if (subargs != error_mark_node
17133 && !any_dependent_template_arguments_p (subargs))
17134 {
17135 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
17136 if (try_one_overload (tparms, targs, tempargs, parm,
17137 elem, strict, sub_strict, addr_p, explain_p)
17138 && (!goodfn || !same_type_p (goodfn, elem)))
17139 {
17140 goodfn = elem;
17141 ++good;
17142 }
17143 }
17144 else if (subargs)
17145 ++ok;
17146 }
17147 /* If no templates (or more than one) are fully resolved by the
17148 explicit arguments, this template-id is a non-deduced context; it
17149 could still be OK if we deduce all template arguments for the
17150 enclosing call through other arguments. */
17151 if (good != 1)
17152 good = ok;
17153 }
17154 else if (TREE_CODE (arg) != OVERLOAD
17155 && TREE_CODE (arg) != FUNCTION_DECL)
17156 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17157 -- but the deduction does not succeed because the expression is
17158 not just the function on its own. */
17159 return false;
17160 else
17161 for (; arg; arg = OVL_NEXT (arg))
17162 if (try_one_overload (tparms, targs, tempargs, parm,
17163 TREE_TYPE (OVL_CURRENT (arg)),
17164 strict, sub_strict, addr_p, explain_p)
17165 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
17166 {
17167 goodfn = OVL_CURRENT (arg);
17168 ++good;
17169 }
17170
17171 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17172 to function or pointer to member function argument if the set of
17173 overloaded functions does not contain function templates and at most
17174 one of a set of overloaded functions provides a unique match.
17175
17176 So if we found multiple possibilities, we return success but don't
17177 deduce anything. */
17178
17179 if (good == 1)
17180 {
17181 int i = TREE_VEC_LENGTH (targs);
17182 for (; i--; )
17183 if (TREE_VEC_ELT (tempargs, i))
17184 {
17185 tree old = TREE_VEC_ELT (targs, i);
17186 tree new_ = TREE_VEC_ELT (tempargs, i);
17187 if (new_ && old && ARGUMENT_PACK_P (old)
17188 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
17189 /* Don't forget explicit template arguments in a pack. */
17190 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
17191 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
17192 TREE_VEC_ELT (targs, i) = new_;
17193 }
17194 }
17195 if (good)
17196 return true;
17197
17198 return false;
17199 }
17200
17201 /* Core DR 115: In contexts where deduction is done and fails, or in
17202 contexts where deduction is not done, if a template argument list is
17203 specified and it, along with any default template arguments, identifies
17204 a single function template specialization, then the template-id is an
17205 lvalue for the function template specialization. */
17206
17207 tree
17208 resolve_nondeduced_context (tree orig_expr)
17209 {
17210 tree expr, offset, baselink;
17211 bool addr;
17212
17213 if (!type_unknown_p (orig_expr))
17214 return orig_expr;
17215
17216 expr = orig_expr;
17217 addr = false;
17218 offset = NULL_TREE;
17219 baselink = NULL_TREE;
17220
17221 if (TREE_CODE (expr) == ADDR_EXPR)
17222 {
17223 expr = TREE_OPERAND (expr, 0);
17224 addr = true;
17225 }
17226 if (TREE_CODE (expr) == OFFSET_REF)
17227 {
17228 offset = expr;
17229 expr = TREE_OPERAND (expr, 1);
17230 }
17231 if (BASELINK_P (expr))
17232 {
17233 baselink = expr;
17234 expr = BASELINK_FUNCTIONS (expr);
17235 }
17236
17237 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
17238 {
17239 int good = 0;
17240 tree goodfn = NULL_TREE;
17241
17242 /* If we got some explicit template args, we need to plug them into
17243 the affected templates before we try to unify, in case the
17244 explicit args will completely resolve the templates in question. */
17245
17246 tree expl_subargs = TREE_OPERAND (expr, 1);
17247 tree arg = TREE_OPERAND (expr, 0);
17248 tree badfn = NULL_TREE;
17249 tree badargs = NULL_TREE;
17250
17251 for (; arg; arg = OVL_NEXT (arg))
17252 {
17253 tree fn = OVL_CURRENT (arg);
17254 tree subargs, elem;
17255
17256 if (TREE_CODE (fn) != TEMPLATE_DECL)
17257 continue;
17258
17259 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17260 expl_subargs, NULL_TREE, tf_none,
17261 /*require_all_args=*/true,
17262 /*use_default_args=*/true);
17263 if (subargs != error_mark_node
17264 && !any_dependent_template_arguments_p (subargs))
17265 {
17266 elem = instantiate_template (fn, subargs, tf_none);
17267 if (elem == error_mark_node)
17268 {
17269 badfn = fn;
17270 badargs = subargs;
17271 }
17272 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
17273 {
17274 goodfn = elem;
17275 ++good;
17276 }
17277 }
17278 }
17279 if (good == 1)
17280 {
17281 mark_used (goodfn);
17282 expr = goodfn;
17283 if (baselink)
17284 expr = build_baselink (BASELINK_BINFO (baselink),
17285 BASELINK_ACCESS_BINFO (baselink),
17286 expr, BASELINK_OPTYPE (baselink));
17287 if (offset)
17288 {
17289 tree base
17290 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
17291 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
17292 }
17293 if (addr)
17294 expr = cp_build_addr_expr (expr, tf_warning_or_error);
17295 return expr;
17296 }
17297 else if (good == 0 && badargs)
17298 /* There were no good options and at least one bad one, so let the
17299 user know what the problem is. */
17300 instantiate_template (badfn, badargs, tf_warning_or_error);
17301 }
17302 return orig_expr;
17303 }
17304
17305 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17306 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17307 different overloads deduce different arguments for a given parm.
17308 ADDR_P is true if the expression for which deduction is being
17309 performed was of the form "& fn" rather than simply "fn".
17310
17311 Returns 1 on success. */
17312
17313 static int
17314 try_one_overload (tree tparms,
17315 tree orig_targs,
17316 tree targs,
17317 tree parm,
17318 tree arg,
17319 unification_kind_t strict,
17320 int sub_strict,
17321 bool addr_p,
17322 bool explain_p)
17323 {
17324 int nargs;
17325 tree tempargs;
17326 int i;
17327
17328 if (arg == error_mark_node)
17329 return 0;
17330
17331 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17332 to function or pointer to member function argument if the set of
17333 overloaded functions does not contain function templates and at most
17334 one of a set of overloaded functions provides a unique match.
17335
17336 So if this is a template, just return success. */
17337
17338 if (uses_template_parms (arg))
17339 return 1;
17340
17341 if (TREE_CODE (arg) == METHOD_TYPE)
17342 arg = build_ptrmemfunc_type (build_pointer_type (arg));
17343 else if (addr_p)
17344 arg = build_pointer_type (arg);
17345
17346 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17347
17348 /* We don't copy orig_targs for this because if we have already deduced
17349 some template args from previous args, unify would complain when we
17350 try to deduce a template parameter for the same argument, even though
17351 there isn't really a conflict. */
17352 nargs = TREE_VEC_LENGTH (targs);
17353 tempargs = make_tree_vec (nargs);
17354
17355 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17356 return 0;
17357
17358 /* First make sure we didn't deduce anything that conflicts with
17359 explicitly specified args. */
17360 for (i = nargs; i--; )
17361 {
17362 tree elt = TREE_VEC_ELT (tempargs, i);
17363 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17364
17365 if (!elt)
17366 /*NOP*/;
17367 else if (uses_template_parms (elt))
17368 /* Since we're unifying against ourselves, we will fill in
17369 template args used in the function parm list with our own
17370 template parms. Discard them. */
17371 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17372 else if (oldelt && !template_args_equal (oldelt, elt))
17373 return 0;
17374 }
17375
17376 for (i = nargs; i--; )
17377 {
17378 tree elt = TREE_VEC_ELT (tempargs, i);
17379
17380 if (elt)
17381 TREE_VEC_ELT (targs, i) = elt;
17382 }
17383
17384 return 1;
17385 }
17386
17387 /* PARM is a template class (perhaps with unbound template
17388 parameters). ARG is a fully instantiated type. If ARG can be
17389 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17390 TARGS are as for unify. */
17391
17392 static tree
17393 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17394 bool explain_p)
17395 {
17396 tree copy_of_targs;
17397
17398 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17399 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17400 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17401 return NULL_TREE;
17402
17403 /* We need to make a new template argument vector for the call to
17404 unify. If we used TARGS, we'd clutter it up with the result of
17405 the attempted unification, even if this class didn't work out.
17406 We also don't want to commit ourselves to all the unifications
17407 we've already done, since unification is supposed to be done on
17408 an argument-by-argument basis. In other words, consider the
17409 following pathological case:
17410
17411 template <int I, int J, int K>
17412 struct S {};
17413
17414 template <int I, int J>
17415 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17416
17417 template <int I, int J, int K>
17418 void f(S<I, J, K>, S<I, I, I>);
17419
17420 void g() {
17421 S<0, 0, 0> s0;
17422 S<0, 1, 2> s2;
17423
17424 f(s0, s2);
17425 }
17426
17427 Now, by the time we consider the unification involving `s2', we
17428 already know that we must have `f<0, 0, 0>'. But, even though
17429 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17430 because there are two ways to unify base classes of S<0, 1, 2>
17431 with S<I, I, I>. If we kept the already deduced knowledge, we
17432 would reject the possibility I=1. */
17433 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17434
17435 /* If unification failed, we're done. */
17436 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17437 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17438 return NULL_TREE;
17439
17440 return arg;
17441 }
17442
17443 /* Given a template type PARM and a class type ARG, find the unique
17444 base type in ARG that is an instance of PARM. We do not examine
17445 ARG itself; only its base-classes. If there is not exactly one
17446 appropriate base class, return NULL_TREE. PARM may be the type of
17447 a partial specialization, as well as a plain template type. Used
17448 by unify. */
17449
17450 static enum template_base_result
17451 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17452 bool explain_p, tree *result)
17453 {
17454 tree rval = NULL_TREE;
17455 tree binfo;
17456
17457 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17458
17459 binfo = TYPE_BINFO (complete_type (arg));
17460 if (!binfo)
17461 {
17462 /* The type could not be completed. */
17463 *result = NULL_TREE;
17464 return tbr_incomplete_type;
17465 }
17466
17467 /* Walk in inheritance graph order. The search order is not
17468 important, and this avoids multiple walks of virtual bases. */
17469 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17470 {
17471 tree r = try_class_unification (tparms, targs, parm,
17472 BINFO_TYPE (binfo), explain_p);
17473
17474 if (r)
17475 {
17476 /* If there is more than one satisfactory baseclass, then:
17477
17478 [temp.deduct.call]
17479
17480 If they yield more than one possible deduced A, the type
17481 deduction fails.
17482
17483 applies. */
17484 if (rval && !same_type_p (r, rval))
17485 {
17486 *result = NULL_TREE;
17487 return tbr_ambiguous_baseclass;
17488 }
17489
17490 rval = r;
17491 }
17492 }
17493
17494 *result = rval;
17495 return tbr_success;
17496 }
17497
17498 /* Returns the level of DECL, which declares a template parameter. */
17499
17500 static int
17501 template_decl_level (tree decl)
17502 {
17503 switch (TREE_CODE (decl))
17504 {
17505 case TYPE_DECL:
17506 case TEMPLATE_DECL:
17507 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17508
17509 case PARM_DECL:
17510 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17511
17512 default:
17513 gcc_unreachable ();
17514 }
17515 return 0;
17516 }
17517
17518 /* Decide whether ARG can be unified with PARM, considering only the
17519 cv-qualifiers of each type, given STRICT as documented for unify.
17520 Returns nonzero iff the unification is OK on that basis. */
17521
17522 static int
17523 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17524 {
17525 int arg_quals = cp_type_quals (arg);
17526 int parm_quals = cp_type_quals (parm);
17527
17528 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17529 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17530 {
17531 /* Although a CVR qualifier is ignored when being applied to a
17532 substituted template parameter ([8.3.2]/1 for example), that
17533 does not allow us to unify "const T" with "int&" because both
17534 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17535 It is ok when we're allowing additional CV qualifiers
17536 at the outer level [14.8.2.1]/3,1st bullet. */
17537 if ((TREE_CODE (arg) == REFERENCE_TYPE
17538 || TREE_CODE (arg) == FUNCTION_TYPE
17539 || TREE_CODE (arg) == METHOD_TYPE)
17540 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17541 return 0;
17542
17543 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17544 && (parm_quals & TYPE_QUAL_RESTRICT))
17545 return 0;
17546 }
17547
17548 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17549 && (arg_quals & parm_quals) != parm_quals)
17550 return 0;
17551
17552 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17553 && (parm_quals & arg_quals) != arg_quals)
17554 return 0;
17555
17556 return 1;
17557 }
17558
17559 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17560 void
17561 template_parm_level_and_index (tree parm, int* level, int* index)
17562 {
17563 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17564 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17565 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17566 {
17567 *index = TEMPLATE_TYPE_IDX (parm);
17568 *level = TEMPLATE_TYPE_LEVEL (parm);
17569 }
17570 else
17571 {
17572 *index = TEMPLATE_PARM_IDX (parm);
17573 *level = TEMPLATE_PARM_LEVEL (parm);
17574 }
17575 }
17576
17577 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17578 do { \
17579 if (unify (TP, TA, P, A, S, EP)) \
17580 return 1; \
17581 } while (0);
17582
17583 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17584 expansion at the end of PACKED_PARMS. Returns 0 if the type
17585 deduction succeeds, 1 otherwise. STRICT is the same as in
17586 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17587 call argument list. We'll need to adjust the arguments to make them
17588 types. SUBR tells us if this is from a recursive call to
17589 type_unification_real, or for comparing two template argument
17590 lists. */
17591
17592 static int
17593 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17594 tree packed_args, unification_kind_t strict,
17595 bool subr, bool explain_p)
17596 {
17597 tree parm
17598 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17599 tree pattern = PACK_EXPANSION_PATTERN (parm);
17600 tree pack, packs = NULL_TREE;
17601 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17602
17603 packed_args = expand_template_argument_pack (packed_args);
17604
17605 int len = TREE_VEC_LENGTH (packed_args);
17606
17607 /* Determine the parameter packs we will be deducing from the
17608 pattern, and record their current deductions. */
17609 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17610 pack; pack = TREE_CHAIN (pack))
17611 {
17612 tree parm_pack = TREE_VALUE (pack);
17613 int idx, level;
17614
17615 /* Determine the index and level of this parameter pack. */
17616 template_parm_level_and_index (parm_pack, &level, &idx);
17617
17618 /* Keep track of the parameter packs and their corresponding
17619 argument packs. */
17620 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17621 TREE_TYPE (packs) = make_tree_vec (len - start);
17622 }
17623
17624 /* Loop through all of the arguments that have not yet been
17625 unified and unify each with the pattern. */
17626 for (i = start; i < len; i++)
17627 {
17628 tree parm;
17629 bool any_explicit = false;
17630 tree arg = TREE_VEC_ELT (packed_args, i);
17631
17632 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17633 or the element of its argument pack at the current index if
17634 this argument was explicitly specified. */
17635 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17636 {
17637 int idx, level;
17638 tree arg, pargs;
17639 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17640
17641 arg = NULL_TREE;
17642 if (TREE_VALUE (pack)
17643 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17644 && (i - start < TREE_VEC_LENGTH (pargs)))
17645 {
17646 any_explicit = true;
17647 arg = TREE_VEC_ELT (pargs, i - start);
17648 }
17649 TMPL_ARG (targs, level, idx) = arg;
17650 }
17651
17652 /* If we had explicit template arguments, substitute them into the
17653 pattern before deduction. */
17654 if (any_explicit)
17655 {
17656 /* Some arguments might still be unspecified or dependent. */
17657 bool dependent;
17658 ++processing_template_decl;
17659 dependent = any_dependent_template_arguments_p (targs);
17660 if (!dependent)
17661 --processing_template_decl;
17662 parm = tsubst (pattern, targs,
17663 explain_p ? tf_warning_or_error : tf_none,
17664 NULL_TREE);
17665 if (dependent)
17666 --processing_template_decl;
17667 if (parm == error_mark_node)
17668 return 1;
17669 }
17670 else
17671 parm = pattern;
17672
17673 /* Unify the pattern with the current argument. */
17674 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17675 explain_p))
17676 return 1;
17677
17678 /* For each parameter pack, collect the deduced value. */
17679 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17680 {
17681 int idx, level;
17682 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17683
17684 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17685 TMPL_ARG (targs, level, idx);
17686 }
17687 }
17688
17689 /* Verify that the results of unification with the parameter packs
17690 produce results consistent with what we've seen before, and make
17691 the deduced argument packs available. */
17692 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17693 {
17694 tree old_pack = TREE_VALUE (pack);
17695 tree new_args = TREE_TYPE (pack);
17696 int i, len = TREE_VEC_LENGTH (new_args);
17697 int idx, level;
17698 bool nondeduced_p = false;
17699
17700 /* By default keep the original deduced argument pack.
17701 If necessary, more specific code is going to update the
17702 resulting deduced argument later down in this function. */
17703 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17704 TMPL_ARG (targs, level, idx) = old_pack;
17705
17706 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17707 actually deduce anything. */
17708 for (i = 0; i < len && !nondeduced_p; ++i)
17709 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17710 nondeduced_p = true;
17711 if (nondeduced_p)
17712 continue;
17713
17714 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17715 {
17716 /* If we had fewer function args than explicit template args,
17717 just use the explicits. */
17718 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17719 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17720 if (len < explicit_len)
17721 new_args = explicit_args;
17722 }
17723
17724 if (!old_pack)
17725 {
17726 tree result;
17727 /* Build the deduced *_ARGUMENT_PACK. */
17728 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17729 {
17730 result = make_node (NONTYPE_ARGUMENT_PACK);
17731 TREE_TYPE (result) =
17732 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17733 TREE_CONSTANT (result) = 1;
17734 }
17735 else
17736 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17737
17738 SET_ARGUMENT_PACK_ARGS (result, new_args);
17739
17740 /* Note the deduced argument packs for this parameter
17741 pack. */
17742 TMPL_ARG (targs, level, idx) = result;
17743 }
17744 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17745 && (ARGUMENT_PACK_ARGS (old_pack)
17746 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17747 {
17748 /* We only had the explicitly-provided arguments before, but
17749 now we have a complete set of arguments. */
17750 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17751
17752 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17753 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17754 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17755 }
17756 else
17757 {
17758 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17759 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17760
17761 if (!comp_template_args_with_info (old_args, new_args,
17762 &bad_old_arg, &bad_new_arg))
17763 /* Inconsistent unification of this parameter pack. */
17764 return unify_parameter_pack_inconsistent (explain_p,
17765 bad_old_arg,
17766 bad_new_arg);
17767 }
17768 }
17769
17770 return unify_success (explain_p);
17771 }
17772
17773 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17774 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17775 parameters and return value are as for unify. */
17776
17777 static int
17778 unify_array_domain (tree tparms, tree targs,
17779 tree parm_dom, tree arg_dom,
17780 bool explain_p)
17781 {
17782 tree parm_max;
17783 tree arg_max;
17784 bool parm_cst;
17785 bool arg_cst;
17786
17787 /* Our representation of array types uses "N - 1" as the
17788 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17789 not an integer constant. We cannot unify arbitrarily
17790 complex expressions, so we eliminate the MINUS_EXPRs
17791 here. */
17792 parm_max = TYPE_MAX_VALUE (parm_dom);
17793 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17794 if (!parm_cst)
17795 {
17796 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17797 parm_max = TREE_OPERAND (parm_max, 0);
17798 }
17799 arg_max = TYPE_MAX_VALUE (arg_dom);
17800 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17801 if (!arg_cst)
17802 {
17803 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17804 trying to unify the type of a variable with the type
17805 of a template parameter. For example:
17806
17807 template <unsigned int N>
17808 void f (char (&) [N]);
17809 int g();
17810 void h(int i) {
17811 char a[g(i)];
17812 f(a);
17813 }
17814
17815 Here, the type of the ARG will be "int [g(i)]", and
17816 may be a SAVE_EXPR, etc. */
17817 if (TREE_CODE (arg_max) != MINUS_EXPR)
17818 return unify_vla_arg (explain_p, arg_dom);
17819 arg_max = TREE_OPERAND (arg_max, 0);
17820 }
17821
17822 /* If only one of the bounds used a MINUS_EXPR, compensate
17823 by adding one to the other bound. */
17824 if (parm_cst && !arg_cst)
17825 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17826 integer_type_node,
17827 parm_max,
17828 integer_one_node);
17829 else if (arg_cst && !parm_cst)
17830 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17831 integer_type_node,
17832 arg_max,
17833 integer_one_node);
17834
17835 return unify (tparms, targs, parm_max, arg_max,
17836 UNIFY_ALLOW_INTEGER, explain_p);
17837 }
17838
17839 /* Deduce the value of template parameters. TPARMS is the (innermost)
17840 set of template parameters to a template. TARGS is the bindings
17841 for those template parameters, as determined thus far; TARGS may
17842 include template arguments for outer levels of template parameters
17843 as well. PARM is a parameter to a template function, or a
17844 subcomponent of that parameter; ARG is the corresponding argument.
17845 This function attempts to match PARM with ARG in a manner
17846 consistent with the existing assignments in TARGS. If more values
17847 are deduced, then TARGS is updated.
17848
17849 Returns 0 if the type deduction succeeds, 1 otherwise. The
17850 parameter STRICT is a bitwise or of the following flags:
17851
17852 UNIFY_ALLOW_NONE:
17853 Require an exact match between PARM and ARG.
17854 UNIFY_ALLOW_MORE_CV_QUAL:
17855 Allow the deduced ARG to be more cv-qualified (by qualification
17856 conversion) than ARG.
17857 UNIFY_ALLOW_LESS_CV_QUAL:
17858 Allow the deduced ARG to be less cv-qualified than ARG.
17859 UNIFY_ALLOW_DERIVED:
17860 Allow the deduced ARG to be a template base class of ARG,
17861 or a pointer to a template base class of the type pointed to by
17862 ARG.
17863 UNIFY_ALLOW_INTEGER:
17864 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17865 case for more information.
17866 UNIFY_ALLOW_OUTER_LEVEL:
17867 This is the outermost level of a deduction. Used to determine validity
17868 of qualification conversions. A valid qualification conversion must
17869 have const qualified pointers leading up to the inner type which
17870 requires additional CV quals, except at the outer level, where const
17871 is not required [conv.qual]. It would be normal to set this flag in
17872 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17873 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17874 This is the outermost level of a deduction, and PARM can be more CV
17875 qualified at this point.
17876 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17877 This is the outermost level of a deduction, and PARM can be less CV
17878 qualified at this point. */
17879
17880 static int
17881 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17882 bool explain_p)
17883 {
17884 int idx;
17885 tree targ;
17886 tree tparm;
17887 int strict_in = strict;
17888
17889 /* I don't think this will do the right thing with respect to types.
17890 But the only case I've seen it in so far has been array bounds, where
17891 signedness is the only information lost, and I think that will be
17892 okay. */
17893 while (TREE_CODE (parm) == NOP_EXPR)
17894 parm = TREE_OPERAND (parm, 0);
17895
17896 if (arg == error_mark_node)
17897 return unify_invalid (explain_p);
17898 if (arg == unknown_type_node
17899 || arg == init_list_type_node)
17900 /* We can't deduce anything from this, but we might get all the
17901 template args from other function args. */
17902 return unify_success (explain_p);
17903
17904 /* If PARM uses template parameters, then we can't bail out here,
17905 even if ARG == PARM, since we won't record unifications for the
17906 template parameters. We might need them if we're trying to
17907 figure out which of two things is more specialized. */
17908 if (arg == parm && !uses_template_parms (parm))
17909 return unify_success (explain_p);
17910
17911 /* Handle init lists early, so the rest of the function can assume
17912 we're dealing with a type. */
17913 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17914 {
17915 tree elt, elttype;
17916 unsigned i;
17917 tree orig_parm = parm;
17918
17919 /* Replace T with std::initializer_list<T> for deduction. */
17920 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17921 && flag_deduce_init_list)
17922 parm = listify (parm);
17923
17924 if (!is_std_init_list (parm)
17925 && TREE_CODE (parm) != ARRAY_TYPE)
17926 /* We can only deduce from an initializer list argument if the
17927 parameter is std::initializer_list or an array; otherwise this
17928 is a non-deduced context. */
17929 return unify_success (explain_p);
17930
17931 if (TREE_CODE (parm) == ARRAY_TYPE)
17932 elttype = TREE_TYPE (parm);
17933 else
17934 {
17935 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17936 /* Deduction is defined in terms of a single type, so just punt
17937 on the (bizarre) std::initializer_list<T...>. */
17938 if (PACK_EXPANSION_P (elttype))
17939 return unify_success (explain_p);
17940 }
17941
17942 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17943 {
17944 int elt_strict = strict;
17945
17946 if (elt == error_mark_node)
17947 return unify_invalid (explain_p);
17948
17949 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17950 {
17951 tree type = TREE_TYPE (elt);
17952 if (type == error_mark_node)
17953 return unify_invalid (explain_p);
17954 /* It should only be possible to get here for a call. */
17955 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17956 elt_strict |= maybe_adjust_types_for_deduction
17957 (DEDUCE_CALL, &elttype, &type, elt);
17958 elt = type;
17959 }
17960
17961 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17962 explain_p);
17963 }
17964
17965 if (TREE_CODE (parm) == ARRAY_TYPE
17966 && deducible_array_bound (TYPE_DOMAIN (parm)))
17967 {
17968 /* Also deduce from the length of the initializer list. */
17969 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17970 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17971 if (idx == error_mark_node)
17972 return unify_invalid (explain_p);
17973 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17974 idx, explain_p);
17975 }
17976
17977 /* If the std::initializer_list<T> deduction worked, replace the
17978 deduced A with std::initializer_list<A>. */
17979 if (orig_parm != parm)
17980 {
17981 idx = TEMPLATE_TYPE_IDX (orig_parm);
17982 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17983 targ = listify (targ);
17984 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17985 }
17986 return unify_success (explain_p);
17987 }
17988
17989 /* Immediately reject some pairs that won't unify because of
17990 cv-qualification mismatches. */
17991 if (TREE_CODE (arg) == TREE_CODE (parm)
17992 && TYPE_P (arg)
17993 /* It is the elements of the array which hold the cv quals of an array
17994 type, and the elements might be template type parms. We'll check
17995 when we recurse. */
17996 && TREE_CODE (arg) != ARRAY_TYPE
17997 /* We check the cv-qualifiers when unifying with template type
17998 parameters below. We want to allow ARG `const T' to unify with
17999 PARM `T' for example, when computing which of two templates
18000 is more specialized, for example. */
18001 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
18002 && !check_cv_quals_for_unify (strict_in, arg, parm))
18003 return unify_cv_qual_mismatch (explain_p, parm, arg);
18004
18005 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
18006 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
18007 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
18008 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
18009 strict &= ~UNIFY_ALLOW_DERIVED;
18010 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18011 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
18012
18013 switch (TREE_CODE (parm))
18014 {
18015 case TYPENAME_TYPE:
18016 case SCOPE_REF:
18017 case UNBOUND_CLASS_TEMPLATE:
18018 /* In a type which contains a nested-name-specifier, template
18019 argument values cannot be deduced for template parameters used
18020 within the nested-name-specifier. */
18021 return unify_success (explain_p);
18022
18023 case TEMPLATE_TYPE_PARM:
18024 case TEMPLATE_TEMPLATE_PARM:
18025 case BOUND_TEMPLATE_TEMPLATE_PARM:
18026 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18027 if (error_operand_p (tparm))
18028 return unify_invalid (explain_p);
18029
18030 if (TEMPLATE_TYPE_LEVEL (parm)
18031 != template_decl_level (tparm))
18032 /* The PARM is not one we're trying to unify. Just check
18033 to see if it matches ARG. */
18034 {
18035 if (TREE_CODE (arg) == TREE_CODE (parm)
18036 && (is_auto (parm) ? is_auto (arg)
18037 : same_type_p (parm, arg)))
18038 return unify_success (explain_p);
18039 else
18040 return unify_type_mismatch (explain_p, parm, arg);
18041 }
18042 idx = TEMPLATE_TYPE_IDX (parm);
18043 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18044 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
18045 if (error_operand_p (tparm))
18046 return unify_invalid (explain_p);
18047
18048 /* Check for mixed types and values. */
18049 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18050 && TREE_CODE (tparm) != TYPE_DECL)
18051 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18052 && TREE_CODE (tparm) != TEMPLATE_DECL))
18053 gcc_unreachable ();
18054
18055 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18056 {
18057 /* ARG must be constructed from a template class or a template
18058 template parameter. */
18059 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
18060 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
18061 return unify_template_deduction_failure (explain_p, parm, arg);
18062 {
18063 tree parmvec = TYPE_TI_ARGS (parm);
18064 /* An alias template name is never deduced. */
18065 if (TYPE_ALIAS_P (arg))
18066 arg = strip_typedefs (arg);
18067 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
18068 tree full_argvec = add_to_template_args (targs, argvec);
18069 tree parm_parms
18070 = DECL_INNERMOST_TEMPLATE_PARMS
18071 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
18072 int i, len;
18073 int parm_variadic_p = 0;
18074
18075 /* The resolution to DR150 makes clear that default
18076 arguments for an N-argument may not be used to bind T
18077 to a template template parameter with fewer than N
18078 parameters. It is not safe to permit the binding of
18079 default arguments as an extension, as that may change
18080 the meaning of a conforming program. Consider:
18081
18082 struct Dense { static const unsigned int dim = 1; };
18083
18084 template <template <typename> class View,
18085 typename Block>
18086 void operator+(float, View<Block> const&);
18087
18088 template <typename Block,
18089 unsigned int Dim = Block::dim>
18090 struct Lvalue_proxy { operator float() const; };
18091
18092 void
18093 test_1d (void) {
18094 Lvalue_proxy<Dense> p;
18095 float b;
18096 b + p;
18097 }
18098
18099 Here, if Lvalue_proxy is permitted to bind to View, then
18100 the global operator+ will be used; if they are not, the
18101 Lvalue_proxy will be converted to float. */
18102 if (coerce_template_parms (parm_parms,
18103 full_argvec,
18104 TYPE_TI_TEMPLATE (parm),
18105 (explain_p
18106 ? tf_warning_or_error
18107 : tf_none),
18108 /*require_all_args=*/true,
18109 /*use_default_args=*/false)
18110 == error_mark_node)
18111 return 1;
18112
18113 /* Deduce arguments T, i from TT<T> or TT<i>.
18114 We check each element of PARMVEC and ARGVEC individually
18115 rather than the whole TREE_VEC since they can have
18116 different number of elements. */
18117
18118 parmvec = expand_template_argument_pack (parmvec);
18119 argvec = expand_template_argument_pack (argvec);
18120
18121 len = TREE_VEC_LENGTH (parmvec);
18122
18123 /* Check if the parameters end in a pack, making them
18124 variadic. */
18125 if (len > 0
18126 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
18127 parm_variadic_p = 1;
18128
18129 for (i = 0; i < len - parm_variadic_p; ++i)
18130 /* If the template argument list of P contains a pack
18131 expansion that is not the last template argument, the
18132 entire template argument list is a non-deduced
18133 context. */
18134 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
18135 return unify_success (explain_p);
18136
18137 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
18138 return unify_too_few_arguments (explain_p,
18139 TREE_VEC_LENGTH (argvec), len);
18140
18141 for (i = 0; i < len - parm_variadic_p; ++i)
18142 {
18143 RECUR_AND_CHECK_FAILURE (tparms, targs,
18144 TREE_VEC_ELT (parmvec, i),
18145 TREE_VEC_ELT (argvec, i),
18146 UNIFY_ALLOW_NONE, explain_p);
18147 }
18148
18149 if (parm_variadic_p
18150 && unify_pack_expansion (tparms, targs,
18151 parmvec, argvec,
18152 DEDUCE_EXACT,
18153 /*subr=*/true, explain_p))
18154 return 1;
18155 }
18156 arg = TYPE_TI_TEMPLATE (arg);
18157
18158 /* Fall through to deduce template name. */
18159 }
18160
18161 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18162 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18163 {
18164 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18165
18166 /* Simple cases: Value already set, does match or doesn't. */
18167 if (targ != NULL_TREE && template_args_equal (targ, arg))
18168 return unify_success (explain_p);
18169 else if (targ)
18170 return unify_inconsistency (explain_p, parm, targ, arg);
18171 }
18172 else
18173 {
18174 /* If PARM is `const T' and ARG is only `int', we don't have
18175 a match unless we are allowing additional qualification.
18176 If ARG is `const int' and PARM is just `T' that's OK;
18177 that binds `const int' to `T'. */
18178 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
18179 arg, parm))
18180 return unify_cv_qual_mismatch (explain_p, parm, arg);
18181
18182 /* Consider the case where ARG is `const volatile int' and
18183 PARM is `const T'. Then, T should be `volatile int'. */
18184 arg = cp_build_qualified_type_real
18185 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
18186 if (arg == error_mark_node)
18187 return unify_invalid (explain_p);
18188
18189 /* Simple cases: Value already set, does match or doesn't. */
18190 if (targ != NULL_TREE && same_type_p (targ, arg))
18191 return unify_success (explain_p);
18192 else if (targ)
18193 return unify_inconsistency (explain_p, parm, targ, arg);
18194
18195 /* Make sure that ARG is not a variable-sized array. (Note
18196 that were talking about variable-sized arrays (like
18197 `int[n]'), rather than arrays of unknown size (like
18198 `int[]').) We'll get very confused by such a type since
18199 the bound of the array is not constant, and therefore
18200 not mangleable. Besides, such types are not allowed in
18201 ISO C++, so we can do as we please here. We do allow
18202 them for 'auto' deduction, since that isn't ABI-exposed. */
18203 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
18204 return unify_vla_arg (explain_p, arg);
18205
18206 /* Strip typedefs as in convert_template_argument. */
18207 arg = canonicalize_type_argument (arg, tf_none);
18208 }
18209
18210 /* If ARG is a parameter pack or an expansion, we cannot unify
18211 against it unless PARM is also a parameter pack. */
18212 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18213 && !template_parameter_pack_p (parm))
18214 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18215
18216 /* If the argument deduction results is a METHOD_TYPE,
18217 then there is a problem.
18218 METHOD_TYPE doesn't map to any real C++ type the result of
18219 the deduction can not be of that type. */
18220 if (TREE_CODE (arg) == METHOD_TYPE)
18221 return unify_method_type_error (explain_p, arg);
18222
18223 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18224 return unify_success (explain_p);
18225
18226 case TEMPLATE_PARM_INDEX:
18227 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18228 if (error_operand_p (tparm))
18229 return unify_invalid (explain_p);
18230
18231 if (TEMPLATE_PARM_LEVEL (parm)
18232 != template_decl_level (tparm))
18233 {
18234 /* The PARM is not one we're trying to unify. Just check
18235 to see if it matches ARG. */
18236 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
18237 && cp_tree_equal (parm, arg));
18238 if (result)
18239 unify_expression_unequal (explain_p, parm, arg);
18240 return result;
18241 }
18242
18243 idx = TEMPLATE_PARM_IDX (parm);
18244 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18245
18246 if (targ)
18247 {
18248 int x = !cp_tree_equal (targ, arg);
18249 if (x)
18250 unify_inconsistency (explain_p, parm, targ, arg);
18251 return x;
18252 }
18253
18254 /* [temp.deduct.type] If, in the declaration of a function template
18255 with a non-type template-parameter, the non-type
18256 template-parameter is used in an expression in the function
18257 parameter-list and, if the corresponding template-argument is
18258 deduced, the template-argument type shall match the type of the
18259 template-parameter exactly, except that a template-argument
18260 deduced from an array bound may be of any integral type.
18261 The non-type parameter might use already deduced type parameters. */
18262 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
18263 if (!TREE_TYPE (arg))
18264 /* Template-parameter dependent expression. Just accept it for now.
18265 It will later be processed in convert_template_argument. */
18266 ;
18267 else if (same_type_p (TREE_TYPE (arg), tparm))
18268 /* OK */;
18269 else if ((strict & UNIFY_ALLOW_INTEGER)
18270 && CP_INTEGRAL_TYPE_P (tparm))
18271 /* Convert the ARG to the type of PARM; the deduced non-type
18272 template argument must exactly match the types of the
18273 corresponding parameter. */
18274 arg = fold (build_nop (tparm, arg));
18275 else if (uses_template_parms (tparm))
18276 /* We haven't deduced the type of this parameter yet. Try again
18277 later. */
18278 return unify_success (explain_p);
18279 else
18280 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
18281
18282 /* If ARG is a parameter pack or an expansion, we cannot unify
18283 against it unless PARM is also a parameter pack. */
18284 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18285 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
18286 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18287
18288 {
18289 bool removed_attr = false;
18290 arg = strip_typedefs_expr (arg, &removed_attr);
18291 }
18292 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18293 return unify_success (explain_p);
18294
18295 case PTRMEM_CST:
18296 {
18297 /* A pointer-to-member constant can be unified only with
18298 another constant. */
18299 if (TREE_CODE (arg) != PTRMEM_CST)
18300 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
18301
18302 /* Just unify the class member. It would be useless (and possibly
18303 wrong, depending on the strict flags) to unify also
18304 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18305 arg refer to the same variable, even if through different
18306 classes. For instance:
18307
18308 struct A { int x; };
18309 struct B : A { };
18310
18311 Unification of &A::x and &B::x must succeed. */
18312 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
18313 PTRMEM_CST_MEMBER (arg), strict, explain_p);
18314 }
18315
18316 case POINTER_TYPE:
18317 {
18318 if (!TYPE_PTR_P (arg))
18319 return unify_type_mismatch (explain_p, parm, arg);
18320
18321 /* [temp.deduct.call]
18322
18323 A can be another pointer or pointer to member type that can
18324 be converted to the deduced A via a qualification
18325 conversion (_conv.qual_).
18326
18327 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18328 This will allow for additional cv-qualification of the
18329 pointed-to types if appropriate. */
18330
18331 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
18332 /* The derived-to-base conversion only persists through one
18333 level of pointers. */
18334 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
18335
18336 return unify (tparms, targs, TREE_TYPE (parm),
18337 TREE_TYPE (arg), strict, explain_p);
18338 }
18339
18340 case REFERENCE_TYPE:
18341 if (TREE_CODE (arg) != REFERENCE_TYPE)
18342 return unify_type_mismatch (explain_p, parm, arg);
18343 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18344 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18345
18346 case ARRAY_TYPE:
18347 if (TREE_CODE (arg) != ARRAY_TYPE)
18348 return unify_type_mismatch (explain_p, parm, arg);
18349 if ((TYPE_DOMAIN (parm) == NULL_TREE)
18350 != (TYPE_DOMAIN (arg) == NULL_TREE))
18351 return unify_type_mismatch (explain_p, parm, arg);
18352 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18353 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18354 if (TYPE_DOMAIN (parm) != NULL_TREE)
18355 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18356 TYPE_DOMAIN (arg), explain_p);
18357 return unify_success (explain_p);
18358
18359 case REAL_TYPE:
18360 case COMPLEX_TYPE:
18361 case VECTOR_TYPE:
18362 case INTEGER_TYPE:
18363 case BOOLEAN_TYPE:
18364 case ENUMERAL_TYPE:
18365 case VOID_TYPE:
18366 case NULLPTR_TYPE:
18367 if (TREE_CODE (arg) != TREE_CODE (parm))
18368 return unify_type_mismatch (explain_p, parm, arg);
18369
18370 /* We have already checked cv-qualification at the top of the
18371 function. */
18372 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18373 return unify_type_mismatch (explain_p, parm, arg);
18374
18375 /* As far as unification is concerned, this wins. Later checks
18376 will invalidate it if necessary. */
18377 return unify_success (explain_p);
18378
18379 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18380 /* Type INTEGER_CST can come from ordinary constant template args. */
18381 case INTEGER_CST:
18382 while (TREE_CODE (arg) == NOP_EXPR)
18383 arg = TREE_OPERAND (arg, 0);
18384
18385 if (TREE_CODE (arg) != INTEGER_CST)
18386 return unify_template_argument_mismatch (explain_p, parm, arg);
18387 return (tree_int_cst_equal (parm, arg)
18388 ? unify_success (explain_p)
18389 : unify_template_argument_mismatch (explain_p, parm, arg));
18390
18391 case TREE_VEC:
18392 {
18393 int i, len, argslen;
18394 int parm_variadic_p = 0;
18395
18396 if (TREE_CODE (arg) != TREE_VEC)
18397 return unify_template_argument_mismatch (explain_p, parm, arg);
18398
18399 len = TREE_VEC_LENGTH (parm);
18400 argslen = TREE_VEC_LENGTH (arg);
18401
18402 /* Check for pack expansions in the parameters. */
18403 for (i = 0; i < len; ++i)
18404 {
18405 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18406 {
18407 if (i == len - 1)
18408 /* We can unify against something with a trailing
18409 parameter pack. */
18410 parm_variadic_p = 1;
18411 else
18412 /* [temp.deduct.type]/9: If the template argument list of
18413 P contains a pack expansion that is not the last
18414 template argument, the entire template argument list
18415 is a non-deduced context. */
18416 return unify_success (explain_p);
18417 }
18418 }
18419
18420 /* If we don't have enough arguments to satisfy the parameters
18421 (not counting the pack expression at the end), or we have
18422 too many arguments for a parameter list that doesn't end in
18423 a pack expression, we can't unify. */
18424 if (parm_variadic_p
18425 ? argslen < len - parm_variadic_p
18426 : argslen != len)
18427 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18428
18429 /* Unify all of the parameters that precede the (optional)
18430 pack expression. */
18431 for (i = 0; i < len - parm_variadic_p; ++i)
18432 {
18433 RECUR_AND_CHECK_FAILURE (tparms, targs,
18434 TREE_VEC_ELT (parm, i),
18435 TREE_VEC_ELT (arg, i),
18436 UNIFY_ALLOW_NONE, explain_p);
18437 }
18438 if (parm_variadic_p)
18439 return unify_pack_expansion (tparms, targs, parm, arg,
18440 DEDUCE_EXACT,
18441 /*subr=*/true, explain_p);
18442 return unify_success (explain_p);
18443 }
18444
18445 case RECORD_TYPE:
18446 case UNION_TYPE:
18447 if (TREE_CODE (arg) != TREE_CODE (parm))
18448 return unify_type_mismatch (explain_p, parm, arg);
18449
18450 if (TYPE_PTRMEMFUNC_P (parm))
18451 {
18452 if (!TYPE_PTRMEMFUNC_P (arg))
18453 return unify_type_mismatch (explain_p, parm, arg);
18454
18455 return unify (tparms, targs,
18456 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18457 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18458 strict, explain_p);
18459 }
18460 else if (TYPE_PTRMEMFUNC_P (arg))
18461 return unify_type_mismatch (explain_p, parm, arg);
18462
18463 if (CLASSTYPE_TEMPLATE_INFO (parm))
18464 {
18465 tree t = NULL_TREE;
18466
18467 if (strict_in & UNIFY_ALLOW_DERIVED)
18468 {
18469 /* First, we try to unify the PARM and ARG directly. */
18470 t = try_class_unification (tparms, targs,
18471 parm, arg, explain_p);
18472
18473 if (!t)
18474 {
18475 /* Fallback to the special case allowed in
18476 [temp.deduct.call]:
18477
18478 If P is a class, and P has the form
18479 template-id, then A can be a derived class of
18480 the deduced A. Likewise, if P is a pointer to
18481 a class of the form template-id, A can be a
18482 pointer to a derived class pointed to by the
18483 deduced A. */
18484 enum template_base_result r;
18485 r = get_template_base (tparms, targs, parm, arg,
18486 explain_p, &t);
18487
18488 if (!t)
18489 return unify_no_common_base (explain_p, r, parm, arg);
18490 }
18491 }
18492 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18493 && (CLASSTYPE_TI_TEMPLATE (parm)
18494 == CLASSTYPE_TI_TEMPLATE (arg)))
18495 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18496 Then, we should unify `int' and `U'. */
18497 t = arg;
18498 else
18499 /* There's no chance of unification succeeding. */
18500 return unify_type_mismatch (explain_p, parm, arg);
18501
18502 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18503 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18504 }
18505 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18506 return unify_type_mismatch (explain_p, parm, arg);
18507 return unify_success (explain_p);
18508
18509 case METHOD_TYPE:
18510 case FUNCTION_TYPE:
18511 {
18512 unsigned int nargs;
18513 tree *args;
18514 tree a;
18515 unsigned int i;
18516
18517 if (TREE_CODE (arg) != TREE_CODE (parm))
18518 return unify_type_mismatch (explain_p, parm, arg);
18519
18520 /* CV qualifications for methods can never be deduced, they must
18521 match exactly. We need to check them explicitly here,
18522 because type_unification_real treats them as any other
18523 cv-qualified parameter. */
18524 if (TREE_CODE (parm) == METHOD_TYPE
18525 && (!check_cv_quals_for_unify
18526 (UNIFY_ALLOW_NONE,
18527 class_of_this_parm (arg),
18528 class_of_this_parm (parm))))
18529 return unify_cv_qual_mismatch (explain_p, parm, arg);
18530
18531 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18532 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18533
18534 nargs = list_length (TYPE_ARG_TYPES (arg));
18535 args = XALLOCAVEC (tree, nargs);
18536 for (a = TYPE_ARG_TYPES (arg), i = 0;
18537 a != NULL_TREE && a != void_list_node;
18538 a = TREE_CHAIN (a), ++i)
18539 args[i] = TREE_VALUE (a);
18540 nargs = i;
18541
18542 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18543 args, nargs, 1, DEDUCE_EXACT,
18544 LOOKUP_NORMAL, NULL, explain_p);
18545 }
18546
18547 case OFFSET_TYPE:
18548 /* Unify a pointer to member with a pointer to member function, which
18549 deduces the type of the member as a function type. */
18550 if (TYPE_PTRMEMFUNC_P (arg))
18551 {
18552 /* Check top-level cv qualifiers */
18553 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18554 return unify_cv_qual_mismatch (explain_p, parm, arg);
18555
18556 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18557 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18558 UNIFY_ALLOW_NONE, explain_p);
18559
18560 /* Determine the type of the function we are unifying against. */
18561 tree fntype = static_fn_type (arg);
18562
18563 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18564 }
18565
18566 if (TREE_CODE (arg) != OFFSET_TYPE)
18567 return unify_type_mismatch (explain_p, parm, arg);
18568 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18569 TYPE_OFFSET_BASETYPE (arg),
18570 UNIFY_ALLOW_NONE, explain_p);
18571 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18572 strict, explain_p);
18573
18574 case CONST_DECL:
18575 if (DECL_TEMPLATE_PARM_P (parm))
18576 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18577 if (arg != scalar_constant_value (parm))
18578 return unify_template_argument_mismatch (explain_p, parm, arg);
18579 return unify_success (explain_p);
18580
18581 case FIELD_DECL:
18582 case TEMPLATE_DECL:
18583 /* Matched cases are handled by the ARG == PARM test above. */
18584 return unify_template_argument_mismatch (explain_p, parm, arg);
18585
18586 case VAR_DECL:
18587 /* A non-type template parameter that is a variable should be a
18588 an integral constant, in which case, it whould have been
18589 folded into its (constant) value. So we should not be getting
18590 a variable here. */
18591 gcc_unreachable ();
18592
18593 case TYPE_ARGUMENT_PACK:
18594 case NONTYPE_ARGUMENT_PACK:
18595 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18596 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18597
18598 case TYPEOF_TYPE:
18599 case DECLTYPE_TYPE:
18600 case UNDERLYING_TYPE:
18601 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18602 or UNDERLYING_TYPE nodes. */
18603 return unify_success (explain_p);
18604
18605 case ERROR_MARK:
18606 /* Unification fails if we hit an error node. */
18607 return unify_invalid (explain_p);
18608
18609 case INDIRECT_REF:
18610 if (REFERENCE_REF_P (parm))
18611 {
18612 if (REFERENCE_REF_P (arg))
18613 arg = TREE_OPERAND (arg, 0);
18614 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18615 strict, explain_p);
18616 }
18617 /* FALLTHRU */
18618
18619 default:
18620 /* An unresolved overload is a nondeduced context. */
18621 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18622 return unify_success (explain_p);
18623 gcc_assert (EXPR_P (parm));
18624
18625 /* We must be looking at an expression. This can happen with
18626 something like:
18627
18628 template <int I>
18629 void foo(S<I>, S<I + 2>);
18630
18631 This is a "nondeduced context":
18632
18633 [deduct.type]
18634
18635 The nondeduced contexts are:
18636
18637 --A type that is a template-id in which one or more of
18638 the template-arguments is an expression that references
18639 a template-parameter.
18640
18641 In these cases, we assume deduction succeeded, but don't
18642 actually infer any unifications. */
18643
18644 if (!uses_template_parms (parm)
18645 && !template_args_equal (parm, arg))
18646 return unify_expression_unequal (explain_p, parm, arg);
18647 else
18648 return unify_success (explain_p);
18649 }
18650 }
18651 #undef RECUR_AND_CHECK_FAILURE
18652 \f
18653 /* Note that DECL can be defined in this translation unit, if
18654 required. */
18655
18656 static void
18657 mark_definable (tree decl)
18658 {
18659 tree clone;
18660 DECL_NOT_REALLY_EXTERN (decl) = 1;
18661 FOR_EACH_CLONE (clone, decl)
18662 DECL_NOT_REALLY_EXTERN (clone) = 1;
18663 }
18664
18665 /* Called if RESULT is explicitly instantiated, or is a member of an
18666 explicitly instantiated class. */
18667
18668 void
18669 mark_decl_instantiated (tree result, int extern_p)
18670 {
18671 SET_DECL_EXPLICIT_INSTANTIATION (result);
18672
18673 /* If this entity has already been written out, it's too late to
18674 make any modifications. */
18675 if (TREE_ASM_WRITTEN (result))
18676 return;
18677
18678 /* For anonymous namespace we don't need to do anything. */
18679 if (decl_anon_ns_mem_p (result))
18680 {
18681 gcc_assert (!TREE_PUBLIC (result));
18682 return;
18683 }
18684
18685 if (TREE_CODE (result) != FUNCTION_DECL)
18686 /* The TREE_PUBLIC flag for function declarations will have been
18687 set correctly by tsubst. */
18688 TREE_PUBLIC (result) = 1;
18689
18690 /* This might have been set by an earlier implicit instantiation. */
18691 DECL_COMDAT (result) = 0;
18692
18693 if (extern_p)
18694 DECL_NOT_REALLY_EXTERN (result) = 0;
18695 else
18696 {
18697 mark_definable (result);
18698 mark_needed (result);
18699 /* Always make artificials weak. */
18700 if (DECL_ARTIFICIAL (result) && flag_weak)
18701 comdat_linkage (result);
18702 /* For WIN32 we also want to put explicit instantiations in
18703 linkonce sections. */
18704 else if (TREE_PUBLIC (result))
18705 maybe_make_one_only (result);
18706 }
18707
18708 /* If EXTERN_P, then this function will not be emitted -- unless
18709 followed by an explicit instantiation, at which point its linkage
18710 will be adjusted. If !EXTERN_P, then this function will be
18711 emitted here. In neither circumstance do we want
18712 import_export_decl to adjust the linkage. */
18713 DECL_INTERFACE_KNOWN (result) = 1;
18714 }
18715
18716 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18717 important template arguments. If any are missing, we check whether
18718 they're important by using error_mark_node for substituting into any
18719 args that were used for partial ordering (the ones between ARGS and END)
18720 and seeing if it bubbles up. */
18721
18722 static bool
18723 check_undeduced_parms (tree targs, tree args, tree end)
18724 {
18725 bool found = false;
18726 int i;
18727 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18728 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18729 {
18730 found = true;
18731 TREE_VEC_ELT (targs, i) = error_mark_node;
18732 }
18733 if (found)
18734 {
18735 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18736 if (substed == error_mark_node)
18737 return true;
18738 }
18739 return false;
18740 }
18741
18742 /* Given two function templates PAT1 and PAT2, return:
18743
18744 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18745 -1 if PAT2 is more specialized than PAT1.
18746 0 if neither is more specialized.
18747
18748 LEN indicates the number of parameters we should consider
18749 (defaulted parameters should not be considered).
18750
18751 The 1998 std underspecified function template partial ordering, and
18752 DR214 addresses the issue. We take pairs of arguments, one from
18753 each of the templates, and deduce them against each other. One of
18754 the templates will be more specialized if all the *other*
18755 template's arguments deduce against its arguments and at least one
18756 of its arguments *does* *not* deduce against the other template's
18757 corresponding argument. Deduction is done as for class templates.
18758 The arguments used in deduction have reference and top level cv
18759 qualifiers removed. Iff both arguments were originally reference
18760 types *and* deduction succeeds in both directions, an lvalue reference
18761 wins against an rvalue reference and otherwise the template
18762 with the more cv-qualified argument wins for that pairing (if
18763 neither is more cv-qualified, they both are equal). Unlike regular
18764 deduction, after all the arguments have been deduced in this way,
18765 we do *not* verify the deduced template argument values can be
18766 substituted into non-deduced contexts.
18767
18768 The logic can be a bit confusing here, because we look at deduce1 and
18769 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18770 can find template arguments for pat1 to make arg1 look like arg2, that
18771 means that arg2 is at least as specialized as arg1. */
18772
18773 int
18774 more_specialized_fn (tree pat1, tree pat2, int len)
18775 {
18776 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18777 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18778 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18779 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18780 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18781 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18782 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18783 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18784 tree origs1, origs2;
18785 bool lose1 = false;
18786 bool lose2 = false;
18787
18788 /* Remove the this parameter from non-static member functions. If
18789 one is a non-static member function and the other is not a static
18790 member function, remove the first parameter from that function
18791 also. This situation occurs for operator functions where we
18792 locate both a member function (with this pointer) and non-member
18793 operator (with explicit first operand). */
18794 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18795 {
18796 len--; /* LEN is the number of significant arguments for DECL1 */
18797 args1 = TREE_CHAIN (args1);
18798 if (!DECL_STATIC_FUNCTION_P (decl2))
18799 args2 = TREE_CHAIN (args2);
18800 }
18801 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18802 {
18803 args2 = TREE_CHAIN (args2);
18804 if (!DECL_STATIC_FUNCTION_P (decl1))
18805 {
18806 len--;
18807 args1 = TREE_CHAIN (args1);
18808 }
18809 }
18810
18811 /* If only one is a conversion operator, they are unordered. */
18812 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18813 return 0;
18814
18815 /* Consider the return type for a conversion function */
18816 if (DECL_CONV_FN_P (decl1))
18817 {
18818 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18819 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18820 len++;
18821 }
18822
18823 processing_template_decl++;
18824
18825 origs1 = args1;
18826 origs2 = args2;
18827
18828 while (len--
18829 /* Stop when an ellipsis is seen. */
18830 && args1 != NULL_TREE && args2 != NULL_TREE)
18831 {
18832 tree arg1 = TREE_VALUE (args1);
18833 tree arg2 = TREE_VALUE (args2);
18834 int deduce1, deduce2;
18835 int quals1 = -1;
18836 int quals2 = -1;
18837 int ref1 = 0;
18838 int ref2 = 0;
18839
18840 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18841 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18842 {
18843 /* When both arguments are pack expansions, we need only
18844 unify the patterns themselves. */
18845 arg1 = PACK_EXPANSION_PATTERN (arg1);
18846 arg2 = PACK_EXPANSION_PATTERN (arg2);
18847
18848 /* This is the last comparison we need to do. */
18849 len = 0;
18850 }
18851
18852 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18853 {
18854 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18855 arg1 = TREE_TYPE (arg1);
18856 quals1 = cp_type_quals (arg1);
18857 }
18858
18859 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18860 {
18861 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18862 arg2 = TREE_TYPE (arg2);
18863 quals2 = cp_type_quals (arg2);
18864 }
18865
18866 arg1 = TYPE_MAIN_VARIANT (arg1);
18867 arg2 = TYPE_MAIN_VARIANT (arg2);
18868
18869 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18870 {
18871 int i, len2 = list_length (args2);
18872 tree parmvec = make_tree_vec (1);
18873 tree argvec = make_tree_vec (len2);
18874 tree ta = args2;
18875
18876 /* Setup the parameter vector, which contains only ARG1. */
18877 TREE_VEC_ELT (parmvec, 0) = arg1;
18878
18879 /* Setup the argument vector, which contains the remaining
18880 arguments. */
18881 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18882 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18883
18884 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18885 argvec, DEDUCE_EXACT,
18886 /*subr=*/true, /*explain_p=*/false)
18887 == 0);
18888
18889 /* We cannot deduce in the other direction, because ARG1 is
18890 a pack expansion but ARG2 is not. */
18891 deduce2 = 0;
18892 }
18893 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18894 {
18895 int i, len1 = list_length (args1);
18896 tree parmvec = make_tree_vec (1);
18897 tree argvec = make_tree_vec (len1);
18898 tree ta = args1;
18899
18900 /* Setup the parameter vector, which contains only ARG1. */
18901 TREE_VEC_ELT (parmvec, 0) = arg2;
18902
18903 /* Setup the argument vector, which contains the remaining
18904 arguments. */
18905 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18906 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18907
18908 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18909 argvec, DEDUCE_EXACT,
18910 /*subr=*/true, /*explain_p=*/false)
18911 == 0);
18912
18913 /* We cannot deduce in the other direction, because ARG2 is
18914 a pack expansion but ARG1 is not.*/
18915 deduce1 = 0;
18916 }
18917
18918 else
18919 {
18920 /* The normal case, where neither argument is a pack
18921 expansion. */
18922 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18923 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18924 == 0);
18925 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18926 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18927 == 0);
18928 }
18929
18930 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18931 arg2, then arg2 is not as specialized as arg1. */
18932 if (!deduce1)
18933 lose2 = true;
18934 if (!deduce2)
18935 lose1 = true;
18936
18937 /* "If, for a given type, deduction succeeds in both directions
18938 (i.e., the types are identical after the transformations above)
18939 and both P and A were reference types (before being replaced with
18940 the type referred to above):
18941 - if the type from the argument template was an lvalue reference and
18942 the type from the parameter template was not, the argument type is
18943 considered to be more specialized than the other; otherwise,
18944 - if the type from the argument template is more cv-qualified
18945 than the type from the parameter template (as described above),
18946 the argument type is considered to be more specialized than the other;
18947 otherwise,
18948 - neither type is more specialized than the other." */
18949
18950 if (deduce1 && deduce2)
18951 {
18952 if (ref1 && ref2 && ref1 != ref2)
18953 {
18954 if (ref1 > ref2)
18955 lose1 = true;
18956 else
18957 lose2 = true;
18958 }
18959 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18960 {
18961 if ((quals1 & quals2) == quals2)
18962 lose2 = true;
18963 if ((quals1 & quals2) == quals1)
18964 lose1 = true;
18965 }
18966 }
18967
18968 if (lose1 && lose2)
18969 /* We've failed to deduce something in either direction.
18970 These must be unordered. */
18971 break;
18972
18973 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18974 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18975 /* We have already processed all of the arguments in our
18976 handing of the pack expansion type. */
18977 len = 0;
18978
18979 args1 = TREE_CHAIN (args1);
18980 args2 = TREE_CHAIN (args2);
18981 }
18982
18983 /* "In most cases, all template parameters must have values in order for
18984 deduction to succeed, but for partial ordering purposes a template
18985 parameter may remain without a value provided it is not used in the
18986 types being used for partial ordering."
18987
18988 Thus, if we are missing any of the targs1 we need to substitute into
18989 origs1, then pat2 is not as specialized as pat1. This can happen when
18990 there is a nondeduced context. */
18991 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18992 lose2 = true;
18993 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18994 lose1 = true;
18995
18996 processing_template_decl--;
18997
18998 /* All things being equal, if the next argument is a pack expansion
18999 for one function but not for the other, prefer the
19000 non-variadic function. FIXME this is bogus; see c++/41958. */
19001 if (lose1 == lose2
19002 && args1 && TREE_VALUE (args1)
19003 && args2 && TREE_VALUE (args2))
19004 {
19005 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
19006 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
19007 }
19008
19009 if (lose1 == lose2)
19010 return 0;
19011 else if (!lose1)
19012 return 1;
19013 else
19014 return -1;
19015 }
19016
19017 /* Determine which of two partial specializations of TMPL is more
19018 specialized.
19019
19020 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19021 to the first partial specialization. The TREE_PURPOSE is the
19022 innermost set of template parameters for the partial
19023 specialization. PAT2 is similar, but for the second template.
19024
19025 Return 1 if the first partial specialization is more specialized;
19026 -1 if the second is more specialized; 0 if neither is more
19027 specialized.
19028
19029 See [temp.class.order] for information about determining which of
19030 two templates is more specialized. */
19031
19032 static int
19033 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
19034 {
19035 tree targs;
19036 int winner = 0;
19037 bool any_deductions = false;
19038
19039 tree tmpl1 = TREE_VALUE (pat1);
19040 tree tmpl2 = TREE_VALUE (pat2);
19041 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
19042 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
19043 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
19044 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
19045
19046 /* Just like what happens for functions, if we are ordering between
19047 different template specializations, we may encounter dependent
19048 types in the arguments, and we need our dependency check functions
19049 to behave correctly. */
19050 ++processing_template_decl;
19051 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
19052 if (targs)
19053 {
19054 --winner;
19055 any_deductions = true;
19056 }
19057
19058 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
19059 if (targs)
19060 {
19061 ++winner;
19062 any_deductions = true;
19063 }
19064 --processing_template_decl;
19065
19066 /* In the case of a tie where at least one of the templates
19067 has a parameter pack at the end, the template with the most
19068 non-packed parameters wins. */
19069 if (winner == 0
19070 && any_deductions
19071 && (template_args_variadic_p (TREE_PURPOSE (pat1))
19072 || template_args_variadic_p (TREE_PURPOSE (pat2))))
19073 {
19074 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
19075 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
19076 int len1 = TREE_VEC_LENGTH (args1);
19077 int len2 = TREE_VEC_LENGTH (args2);
19078
19079 /* We don't count the pack expansion at the end. */
19080 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
19081 --len1;
19082 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
19083 --len2;
19084
19085 if (len1 > len2)
19086 return 1;
19087 else if (len1 < len2)
19088 return -1;
19089 }
19090
19091 return winner;
19092 }
19093
19094 /* Return the template arguments that will produce the function signature
19095 DECL from the function template FN, with the explicit template
19096 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19097 also match. Return NULL_TREE if no satisfactory arguments could be
19098 found. */
19099
19100 static tree
19101 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
19102 {
19103 int ntparms = DECL_NTPARMS (fn);
19104 tree targs = make_tree_vec (ntparms);
19105 tree decl_type = TREE_TYPE (decl);
19106 tree decl_arg_types;
19107 tree *args;
19108 unsigned int nargs, ix;
19109 tree arg;
19110
19111 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
19112
19113 /* Never do unification on the 'this' parameter. */
19114 decl_arg_types = skip_artificial_parms_for (decl,
19115 TYPE_ARG_TYPES (decl_type));
19116
19117 nargs = list_length (decl_arg_types);
19118 args = XALLOCAVEC (tree, nargs);
19119 for (arg = decl_arg_types, ix = 0;
19120 arg != NULL_TREE && arg != void_list_node;
19121 arg = TREE_CHAIN (arg), ++ix)
19122 args[ix] = TREE_VALUE (arg);
19123
19124 if (fn_type_unification (fn, explicit_args, targs,
19125 args, ix,
19126 (check_rettype || DECL_CONV_FN_P (fn)
19127 ? TREE_TYPE (decl_type) : NULL_TREE),
19128 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
19129 /*decltype*/false)
19130 == error_mark_node)
19131 return NULL_TREE;
19132
19133 return targs;
19134 }
19135
19136 /* Return the innermost template arguments that, when applied to a partial
19137 specialization of TMPL whose innermost template parameters are
19138 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19139 ARGS.
19140
19141 For example, suppose we have:
19142
19143 template <class T, class U> struct S {};
19144 template <class T> struct S<T*, int> {};
19145
19146 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19147 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19148 int}. The resulting vector will be {double}, indicating that `T'
19149 is bound to `double'. */
19150
19151 static tree
19152 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
19153 {
19154 int i, ntparms = TREE_VEC_LENGTH (tparms);
19155 tree deduced_args;
19156 tree innermost_deduced_args;
19157
19158 innermost_deduced_args = make_tree_vec (ntparms);
19159 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19160 {
19161 deduced_args = copy_node (args);
19162 SET_TMPL_ARGS_LEVEL (deduced_args,
19163 TMPL_ARGS_DEPTH (deduced_args),
19164 innermost_deduced_args);
19165 }
19166 else
19167 deduced_args = innermost_deduced_args;
19168
19169 if (unify (tparms, deduced_args,
19170 INNERMOST_TEMPLATE_ARGS (spec_args),
19171 INNERMOST_TEMPLATE_ARGS (args),
19172 UNIFY_ALLOW_NONE, /*explain_p=*/false))
19173 return NULL_TREE;
19174
19175 for (i = 0; i < ntparms; ++i)
19176 if (! TREE_VEC_ELT (innermost_deduced_args, i))
19177 return NULL_TREE;
19178
19179 /* Verify that nondeduced template arguments agree with the type
19180 obtained from argument deduction.
19181
19182 For example:
19183
19184 struct A { typedef int X; };
19185 template <class T, class U> struct C {};
19186 template <class T> struct C<T, typename T::X> {};
19187
19188 Then with the instantiation `C<A, int>', we can deduce that
19189 `T' is `A' but unify () does not check whether `typename T::X'
19190 is `int'. */
19191 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
19192 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19193 spec_args, tmpl,
19194 tf_none, false, false);
19195 if (spec_args == error_mark_node
19196 /* We only need to check the innermost arguments; the other
19197 arguments will always agree. */
19198 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
19199 INNERMOST_TEMPLATE_ARGS (args)))
19200 return NULL_TREE;
19201
19202 /* Now that we have bindings for all of the template arguments,
19203 ensure that the arguments deduced for the template template
19204 parameters have compatible template parameter lists. See the use
19205 of template_template_parm_bindings_ok_p in fn_type_unification
19206 for more information. */
19207 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
19208 return NULL_TREE;
19209
19210 return deduced_args;
19211 }
19212
19213 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19214 Return the TREE_LIST node with the most specialized template, if
19215 any. If there is no most specialized template, the error_mark_node
19216 is returned.
19217
19218 Note that this function does not look at, or modify, the
19219 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19220 returned is one of the elements of INSTANTIATIONS, callers may
19221 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19222 and retrieve it from the value returned. */
19223
19224 tree
19225 most_specialized_instantiation (tree templates)
19226 {
19227 tree fn, champ;
19228
19229 ++processing_template_decl;
19230
19231 champ = templates;
19232 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
19233 {
19234 int fate = 0;
19235
19236 if (get_bindings (TREE_VALUE (champ),
19237 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19238 NULL_TREE, /*check_ret=*/true))
19239 fate--;
19240
19241 if (get_bindings (TREE_VALUE (fn),
19242 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19243 NULL_TREE, /*check_ret=*/true))
19244 fate++;
19245
19246 if (fate == -1)
19247 champ = fn;
19248 else if (!fate)
19249 {
19250 /* Equally specialized, move to next function. If there
19251 is no next function, nothing's most specialized. */
19252 fn = TREE_CHAIN (fn);
19253 champ = fn;
19254 if (!fn)
19255 break;
19256 }
19257 }
19258
19259 if (champ)
19260 /* Now verify that champ is better than everything earlier in the
19261 instantiation list. */
19262 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
19263 if (get_bindings (TREE_VALUE (champ),
19264 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19265 NULL_TREE, /*check_ret=*/true)
19266 || !get_bindings (TREE_VALUE (fn),
19267 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19268 NULL_TREE, /*check_ret=*/true))
19269 {
19270 champ = NULL_TREE;
19271 break;
19272 }
19273
19274 processing_template_decl--;
19275
19276 if (!champ)
19277 return error_mark_node;
19278
19279 return champ;
19280 }
19281
19282 /* If DECL is a specialization of some template, return the most
19283 general such template. Otherwise, returns NULL_TREE.
19284
19285 For example, given:
19286
19287 template <class T> struct S { template <class U> void f(U); };
19288
19289 if TMPL is `template <class U> void S<int>::f(U)' this will return
19290 the full template. This function will not trace past partial
19291 specializations, however. For example, given in addition:
19292
19293 template <class T> struct S<T*> { template <class U> void f(U); };
19294
19295 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19296 `template <class T> template <class U> S<T*>::f(U)'. */
19297
19298 tree
19299 most_general_template (tree decl)
19300 {
19301 if (TREE_CODE (decl) != TEMPLATE_DECL)
19302 {
19303 if (tree tinfo = get_template_info (decl))
19304 decl = TI_TEMPLATE (tinfo);
19305 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19306 template friend, or a FIELD_DECL for a capture pack. */
19307 if (TREE_CODE (decl) != TEMPLATE_DECL)
19308 return NULL_TREE;
19309 }
19310
19311 /* Look for more and more general templates. */
19312 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
19313 {
19314 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19315 (See cp-tree.h for details.) */
19316 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
19317 break;
19318
19319 if (CLASS_TYPE_P (TREE_TYPE (decl))
19320 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
19321 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19322 break;
19323
19324 /* Stop if we run into an explicitly specialized class template. */
19325 if (!DECL_NAMESPACE_SCOPE_P (decl)
19326 && DECL_CONTEXT (decl)
19327 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
19328 break;
19329
19330 decl = DECL_TI_TEMPLATE (decl);
19331 }
19332
19333 return decl;
19334 }
19335
19336 /* Return the most specialized of the template partial specializations
19337 which can produce TARGET, a specialization of some class or variable
19338 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19339 a TEMPLATE_DECL node corresponding to the partial specialization, while
19340 the TREE_PURPOSE is the set of template arguments that must be
19341 substituted into the template pattern in order to generate TARGET.
19342
19343 If the choice of partial specialization is ambiguous, a diagnostic
19344 is issued, and the error_mark_node is returned. If there are no
19345 partial specializations matching TARGET, then NULL_TREE is
19346 returned, indicating that the primary template should be used. */
19347
19348 static tree
19349 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
19350 {
19351 tree list = NULL_TREE;
19352 tree t;
19353 tree champ;
19354 int fate;
19355 bool ambiguous_p;
19356 tree outer_args = NULL_TREE;
19357 tree tmpl, args;
19358
19359 if (TYPE_P (target))
19360 {
19361 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
19362 tmpl = TI_TEMPLATE (tinfo);
19363 args = TI_ARGS (tinfo);
19364 }
19365 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
19366 {
19367 tmpl = TREE_OPERAND (target, 0);
19368 args = TREE_OPERAND (target, 1);
19369 }
19370 else if (VAR_P (target))
19371 {
19372 tree tinfo = DECL_TEMPLATE_INFO (target);
19373 tmpl = TI_TEMPLATE (tinfo);
19374 args = TI_ARGS (tinfo);
19375 }
19376 else
19377 gcc_unreachable ();
19378
19379 tree main_tmpl = most_general_template (tmpl);
19380
19381 /* For determining which partial specialization to use, only the
19382 innermost args are interesting. */
19383 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19384 {
19385 outer_args = strip_innermost_template_args (args, 1);
19386 args = INNERMOST_TEMPLATE_ARGS (args);
19387 }
19388
19389 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
19390 {
19391 tree partial_spec_args;
19392 tree spec_args;
19393 tree spec_tmpl = TREE_VALUE (t);
19394
19395 partial_spec_args = TREE_PURPOSE (t);
19396
19397 ++processing_template_decl;
19398
19399 if (outer_args)
19400 {
19401 /* Discard the outer levels of args, and then substitute in the
19402 template args from the enclosing class. */
19403 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19404 partial_spec_args = tsubst_template_args
19405 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19406
19407 /* And the same for the partial specialization TEMPLATE_DECL. */
19408 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19409 }
19410
19411 partial_spec_args =
19412 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19413 partial_spec_args,
19414 tmpl, tf_none,
19415 /*require_all_args=*/true,
19416 /*use_default_args=*/true);
19417
19418 --processing_template_decl;
19419
19420 if (partial_spec_args == error_mark_node)
19421 return error_mark_node;
19422 if (spec_tmpl == error_mark_node)
19423 return error_mark_node;
19424
19425 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19426 spec_args = get_partial_spec_bindings (tmpl, parms,
19427 partial_spec_args,
19428 args);
19429 if (spec_args)
19430 {
19431 if (outer_args)
19432 spec_args = add_to_template_args (outer_args, spec_args);
19433 list = tree_cons (spec_args, TREE_VALUE (t), list);
19434 TREE_TYPE (list) = TREE_TYPE (t);
19435 }
19436 }
19437
19438 if (! list)
19439 return NULL_TREE;
19440
19441 ambiguous_p = false;
19442 t = list;
19443 champ = t;
19444 t = TREE_CHAIN (t);
19445 for (; t; t = TREE_CHAIN (t))
19446 {
19447 fate = more_specialized_partial_spec (tmpl, champ, t);
19448 if (fate == 1)
19449 ;
19450 else
19451 {
19452 if (fate == 0)
19453 {
19454 t = TREE_CHAIN (t);
19455 if (! t)
19456 {
19457 ambiguous_p = true;
19458 break;
19459 }
19460 }
19461 champ = t;
19462 }
19463 }
19464
19465 if (!ambiguous_p)
19466 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19467 {
19468 fate = more_specialized_partial_spec (tmpl, champ, t);
19469 if (fate != 1)
19470 {
19471 ambiguous_p = true;
19472 break;
19473 }
19474 }
19475
19476 if (ambiguous_p)
19477 {
19478 const char *str;
19479 char *spaces = NULL;
19480 if (!(complain & tf_error))
19481 return error_mark_node;
19482 if (TYPE_P (target))
19483 error ("ambiguous template instantiation for %q#T", target);
19484 else
19485 error ("ambiguous template instantiation for %q#D", target);
19486 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19487 for (t = list; t; t = TREE_CHAIN (t))
19488 {
19489 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
19490 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
19491 "%s %#S", spaces ? spaces : str, subst);
19492 spaces = spaces ? spaces : get_spaces (str);
19493 }
19494 free (spaces);
19495 return error_mark_node;
19496 }
19497
19498 return champ;
19499 }
19500
19501 /* Explicitly instantiate DECL. */
19502
19503 void
19504 do_decl_instantiation (tree decl, tree storage)
19505 {
19506 tree result = NULL_TREE;
19507 int extern_p = 0;
19508
19509 if (!decl || decl == error_mark_node)
19510 /* An error occurred, for which grokdeclarator has already issued
19511 an appropriate message. */
19512 return;
19513 else if (! DECL_LANG_SPECIFIC (decl))
19514 {
19515 error ("explicit instantiation of non-template %q#D", decl);
19516 return;
19517 }
19518
19519 bool var_templ = (DECL_TEMPLATE_INFO (decl)
19520 && variable_template_p (DECL_TI_TEMPLATE (decl)));
19521
19522 if (VAR_P (decl) && !var_templ)
19523 {
19524 /* There is an asymmetry here in the way VAR_DECLs and
19525 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19526 the latter, the DECL we get back will be marked as a
19527 template instantiation, and the appropriate
19528 DECL_TEMPLATE_INFO will be set up. This does not happen for
19529 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19530 should handle VAR_DECLs as it currently handles
19531 FUNCTION_DECLs. */
19532 if (!DECL_CLASS_SCOPE_P (decl))
19533 {
19534 error ("%qD is not a static data member of a class template", decl);
19535 return;
19536 }
19537 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19538 if (!result || !VAR_P (result))
19539 {
19540 error ("no matching template for %qD found", decl);
19541 return;
19542 }
19543 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19544 {
19545 error ("type %qT for explicit instantiation %qD does not match "
19546 "declared type %qT", TREE_TYPE (result), decl,
19547 TREE_TYPE (decl));
19548 return;
19549 }
19550 }
19551 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
19552 {
19553 error ("explicit instantiation of %q#D", decl);
19554 return;
19555 }
19556 else
19557 result = decl;
19558
19559 /* Check for various error cases. Note that if the explicit
19560 instantiation is valid the RESULT will currently be marked as an
19561 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19562 until we get here. */
19563
19564 if (DECL_TEMPLATE_SPECIALIZATION (result))
19565 {
19566 /* DR 259 [temp.spec].
19567
19568 Both an explicit instantiation and a declaration of an explicit
19569 specialization shall not appear in a program unless the explicit
19570 instantiation follows a declaration of the explicit specialization.
19571
19572 For a given set of template parameters, if an explicit
19573 instantiation of a template appears after a declaration of an
19574 explicit specialization for that template, the explicit
19575 instantiation has no effect. */
19576 return;
19577 }
19578 else if (DECL_EXPLICIT_INSTANTIATION (result))
19579 {
19580 /* [temp.spec]
19581
19582 No program shall explicitly instantiate any template more
19583 than once.
19584
19585 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19586 the first instantiation was `extern' and the second is not,
19587 and EXTERN_P for the opposite case. */
19588 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19589 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19590 /* If an "extern" explicit instantiation follows an ordinary
19591 explicit instantiation, the template is instantiated. */
19592 if (extern_p)
19593 return;
19594 }
19595 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19596 {
19597 error ("no matching template for %qD found", result);
19598 return;
19599 }
19600 else if (!DECL_TEMPLATE_INFO (result))
19601 {
19602 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19603 return;
19604 }
19605
19606 if (storage == NULL_TREE)
19607 ;
19608 else if (storage == ridpointers[(int) RID_EXTERN])
19609 {
19610 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19611 pedwarn (input_location, OPT_Wpedantic,
19612 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19613 "instantiations");
19614 extern_p = 1;
19615 }
19616 else
19617 error ("storage class %qD applied to template instantiation", storage);
19618
19619 check_explicit_instantiation_namespace (result);
19620 mark_decl_instantiated (result, extern_p);
19621 if (! extern_p)
19622 instantiate_decl (result, /*defer_ok=*/1,
19623 /*expl_inst_class_mem_p=*/false);
19624 }
19625
19626 static void
19627 mark_class_instantiated (tree t, int extern_p)
19628 {
19629 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19630 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19631 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19632 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19633 if (! extern_p)
19634 {
19635 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19636 rest_of_type_compilation (t, 1);
19637 }
19638 }
19639
19640 /* Called from do_type_instantiation through binding_table_foreach to
19641 do recursive instantiation for the type bound in ENTRY. */
19642 static void
19643 bt_instantiate_type_proc (binding_entry entry, void *data)
19644 {
19645 tree storage = *(tree *) data;
19646
19647 if (MAYBE_CLASS_TYPE_P (entry->type)
19648 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19649 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19650 }
19651
19652 /* Called from do_type_instantiation to instantiate a member
19653 (a member function or a static member variable) of an
19654 explicitly instantiated class template. */
19655 static void
19656 instantiate_class_member (tree decl, int extern_p)
19657 {
19658 mark_decl_instantiated (decl, extern_p);
19659 if (! extern_p)
19660 instantiate_decl (decl, /*defer_ok=*/1,
19661 /*expl_inst_class_mem_p=*/true);
19662 }
19663
19664 /* Perform an explicit instantiation of template class T. STORAGE, if
19665 non-null, is the RID for extern, inline or static. COMPLAIN is
19666 nonzero if this is called from the parser, zero if called recursively,
19667 since the standard is unclear (as detailed below). */
19668
19669 void
19670 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19671 {
19672 int extern_p = 0;
19673 int nomem_p = 0;
19674 int static_p = 0;
19675 int previous_instantiation_extern_p = 0;
19676
19677 if (TREE_CODE (t) == TYPE_DECL)
19678 t = TREE_TYPE (t);
19679
19680 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19681 {
19682 tree tmpl =
19683 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19684 if (tmpl)
19685 error ("explicit instantiation of non-class template %qD", tmpl);
19686 else
19687 error ("explicit instantiation of non-template type %qT", t);
19688 return;
19689 }
19690
19691 complete_type (t);
19692
19693 if (!COMPLETE_TYPE_P (t))
19694 {
19695 if (complain & tf_error)
19696 error ("explicit instantiation of %q#T before definition of template",
19697 t);
19698 return;
19699 }
19700
19701 if (storage != NULL_TREE)
19702 {
19703 if (!in_system_header_at (input_location))
19704 {
19705 if (storage == ridpointers[(int) RID_EXTERN])
19706 {
19707 if (cxx_dialect == cxx98)
19708 pedwarn (input_location, OPT_Wpedantic,
19709 "ISO C++ 1998 forbids the use of %<extern%> on "
19710 "explicit instantiations");
19711 }
19712 else
19713 pedwarn (input_location, OPT_Wpedantic,
19714 "ISO C++ forbids the use of %qE"
19715 " on explicit instantiations", storage);
19716 }
19717
19718 if (storage == ridpointers[(int) RID_INLINE])
19719 nomem_p = 1;
19720 else if (storage == ridpointers[(int) RID_EXTERN])
19721 extern_p = 1;
19722 else if (storage == ridpointers[(int) RID_STATIC])
19723 static_p = 1;
19724 else
19725 {
19726 error ("storage class %qD applied to template instantiation",
19727 storage);
19728 extern_p = 0;
19729 }
19730 }
19731
19732 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19733 {
19734 /* DR 259 [temp.spec].
19735
19736 Both an explicit instantiation and a declaration of an explicit
19737 specialization shall not appear in a program unless the explicit
19738 instantiation follows a declaration of the explicit specialization.
19739
19740 For a given set of template parameters, if an explicit
19741 instantiation of a template appears after a declaration of an
19742 explicit specialization for that template, the explicit
19743 instantiation has no effect. */
19744 return;
19745 }
19746 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19747 {
19748 /* [temp.spec]
19749
19750 No program shall explicitly instantiate any template more
19751 than once.
19752
19753 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19754 instantiation was `extern'. If EXTERN_P then the second is.
19755 These cases are OK. */
19756 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19757
19758 if (!previous_instantiation_extern_p && !extern_p
19759 && (complain & tf_error))
19760 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19761
19762 /* If we've already instantiated the template, just return now. */
19763 if (!CLASSTYPE_INTERFACE_ONLY (t))
19764 return;
19765 }
19766
19767 check_explicit_instantiation_namespace (TYPE_NAME (t));
19768 mark_class_instantiated (t, extern_p);
19769
19770 if (nomem_p)
19771 return;
19772
19773 {
19774 tree tmp;
19775
19776 /* In contrast to implicit instantiation, where only the
19777 declarations, and not the definitions, of members are
19778 instantiated, we have here:
19779
19780 [temp.explicit]
19781
19782 The explicit instantiation of a class template specialization
19783 implies the instantiation of all of its members not
19784 previously explicitly specialized in the translation unit
19785 containing the explicit instantiation.
19786
19787 Of course, we can't instantiate member template classes, since
19788 we don't have any arguments for them. Note that the standard
19789 is unclear on whether the instantiation of the members are
19790 *explicit* instantiations or not. However, the most natural
19791 interpretation is that it should be an explicit instantiation. */
19792
19793 if (! static_p)
19794 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19795 if (TREE_CODE (tmp) == FUNCTION_DECL
19796 && DECL_TEMPLATE_INSTANTIATION (tmp))
19797 instantiate_class_member (tmp, extern_p);
19798
19799 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19800 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19801 instantiate_class_member (tmp, extern_p);
19802
19803 if (CLASSTYPE_NESTED_UTDS (t))
19804 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19805 bt_instantiate_type_proc, &storage);
19806 }
19807 }
19808
19809 /* Given a function DECL, which is a specialization of TMPL, modify
19810 DECL to be a re-instantiation of TMPL with the same template
19811 arguments. TMPL should be the template into which tsubst'ing
19812 should occur for DECL, not the most general template.
19813
19814 One reason for doing this is a scenario like this:
19815
19816 template <class T>
19817 void f(const T&, int i);
19818
19819 void g() { f(3, 7); }
19820
19821 template <class T>
19822 void f(const T& t, const int i) { }
19823
19824 Note that when the template is first instantiated, with
19825 instantiate_template, the resulting DECL will have no name for the
19826 first parameter, and the wrong type for the second. So, when we go
19827 to instantiate the DECL, we regenerate it. */
19828
19829 static void
19830 regenerate_decl_from_template (tree decl, tree tmpl)
19831 {
19832 /* The arguments used to instantiate DECL, from the most general
19833 template. */
19834 tree args;
19835 tree code_pattern;
19836
19837 args = DECL_TI_ARGS (decl);
19838 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19839
19840 /* Make sure that we can see identifiers, and compute access
19841 correctly. */
19842 push_access_scope (decl);
19843
19844 if (TREE_CODE (decl) == FUNCTION_DECL)
19845 {
19846 tree decl_parm;
19847 tree pattern_parm;
19848 tree specs;
19849 int args_depth;
19850 int parms_depth;
19851
19852 args_depth = TMPL_ARGS_DEPTH (args);
19853 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19854 if (args_depth > parms_depth)
19855 args = get_innermost_template_args (args, parms_depth);
19856
19857 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19858 args, tf_error, NULL_TREE,
19859 /*defer_ok*/false);
19860 if (specs && specs != error_mark_node)
19861 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19862 specs);
19863
19864 /* Merge parameter declarations. */
19865 decl_parm = skip_artificial_parms_for (decl,
19866 DECL_ARGUMENTS (decl));
19867 pattern_parm
19868 = skip_artificial_parms_for (code_pattern,
19869 DECL_ARGUMENTS (code_pattern));
19870 while (decl_parm && !DECL_PACK_P (pattern_parm))
19871 {
19872 tree parm_type;
19873 tree attributes;
19874
19875 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19876 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19877 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19878 NULL_TREE);
19879 parm_type = type_decays_to (parm_type);
19880 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19881 TREE_TYPE (decl_parm) = parm_type;
19882 attributes = DECL_ATTRIBUTES (pattern_parm);
19883 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19884 {
19885 DECL_ATTRIBUTES (decl_parm) = attributes;
19886 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19887 }
19888 decl_parm = DECL_CHAIN (decl_parm);
19889 pattern_parm = DECL_CHAIN (pattern_parm);
19890 }
19891 /* Merge any parameters that match with the function parameter
19892 pack. */
19893 if (pattern_parm && DECL_PACK_P (pattern_parm))
19894 {
19895 int i, len;
19896 tree expanded_types;
19897 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19898 the parameters in this function parameter pack. */
19899 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19900 args, tf_error, NULL_TREE);
19901 len = TREE_VEC_LENGTH (expanded_types);
19902 for (i = 0; i < len; i++)
19903 {
19904 tree parm_type;
19905 tree attributes;
19906
19907 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19908 /* Rename the parameter to include the index. */
19909 DECL_NAME (decl_parm) =
19910 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19911 parm_type = TREE_VEC_ELT (expanded_types, i);
19912 parm_type = type_decays_to (parm_type);
19913 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19914 TREE_TYPE (decl_parm) = parm_type;
19915 attributes = DECL_ATTRIBUTES (pattern_parm);
19916 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19917 {
19918 DECL_ATTRIBUTES (decl_parm) = attributes;
19919 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19920 }
19921 decl_parm = DECL_CHAIN (decl_parm);
19922 }
19923 }
19924 /* Merge additional specifiers from the CODE_PATTERN. */
19925 if (DECL_DECLARED_INLINE_P (code_pattern)
19926 && !DECL_DECLARED_INLINE_P (decl))
19927 DECL_DECLARED_INLINE_P (decl) = 1;
19928 }
19929 else if (VAR_P (decl))
19930 {
19931 DECL_INITIAL (decl) =
19932 tsubst_expr (DECL_INITIAL (code_pattern), args,
19933 tf_error, DECL_TI_TEMPLATE (decl),
19934 /*integral_constant_expression_p=*/false);
19935 if (VAR_HAD_UNKNOWN_BOUND (decl))
19936 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19937 tf_error, DECL_TI_TEMPLATE (decl));
19938 }
19939 else
19940 gcc_unreachable ();
19941
19942 pop_access_scope (decl);
19943 }
19944
19945 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19946 substituted to get DECL. */
19947
19948 tree
19949 template_for_substitution (tree decl)
19950 {
19951 tree tmpl = DECL_TI_TEMPLATE (decl);
19952
19953 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19954 for the instantiation. This is not always the most general
19955 template. Consider, for example:
19956
19957 template <class T>
19958 struct S { template <class U> void f();
19959 template <> void f<int>(); };
19960
19961 and an instantiation of S<double>::f<int>. We want TD to be the
19962 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19963 while (/* An instantiation cannot have a definition, so we need a
19964 more general template. */
19965 DECL_TEMPLATE_INSTANTIATION (tmpl)
19966 /* We must also deal with friend templates. Given:
19967
19968 template <class T> struct S {
19969 template <class U> friend void f() {};
19970 };
19971
19972 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19973 so far as the language is concerned, but that's still
19974 where we get the pattern for the instantiation from. On
19975 other hand, if the definition comes outside the class, say:
19976
19977 template <class T> struct S {
19978 template <class U> friend void f();
19979 };
19980 template <class U> friend void f() {}
19981
19982 we don't need to look any further. That's what the check for
19983 DECL_INITIAL is for. */
19984 || (TREE_CODE (decl) == FUNCTION_DECL
19985 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19986 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19987 {
19988 /* The present template, TD, should not be a definition. If it
19989 were a definition, we should be using it! Note that we
19990 cannot restructure the loop to just keep going until we find
19991 a template with a definition, since that might go too far if
19992 a specialization was declared, but not defined. */
19993
19994 /* Fetch the more general template. */
19995 tmpl = DECL_TI_TEMPLATE (tmpl);
19996 }
19997
19998 return tmpl;
19999 }
20000
20001 /* Returns true if we need to instantiate this template instance even if we
20002 know we aren't going to emit it.. */
20003
20004 bool
20005 always_instantiate_p (tree decl)
20006 {
20007 /* We always instantiate inline functions so that we can inline them. An
20008 explicit instantiation declaration prohibits implicit instantiation of
20009 non-inline functions. With high levels of optimization, we would
20010 normally inline non-inline functions -- but we're not allowed to do
20011 that for "extern template" functions. Therefore, we check
20012 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20013 return ((TREE_CODE (decl) == FUNCTION_DECL
20014 && (DECL_DECLARED_INLINE_P (decl)
20015 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
20016 /* And we need to instantiate static data members so that
20017 their initializers are available in integral constant
20018 expressions. */
20019 || (VAR_P (decl)
20020 && decl_maybe_constant_var_p (decl)));
20021 }
20022
20023 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20024 instantiate it now, modifying TREE_TYPE (fn). */
20025
20026 void
20027 maybe_instantiate_noexcept (tree fn)
20028 {
20029 tree fntype, spec, noex, clone;
20030
20031 /* Don't instantiate a noexcept-specification from template context. */
20032 if (processing_template_decl)
20033 return;
20034
20035 if (DECL_CLONED_FUNCTION_P (fn))
20036 fn = DECL_CLONED_FUNCTION (fn);
20037 fntype = TREE_TYPE (fn);
20038 spec = TYPE_RAISES_EXCEPTIONS (fntype);
20039
20040 if (!spec || !TREE_PURPOSE (spec))
20041 return;
20042
20043 noex = TREE_PURPOSE (spec);
20044
20045 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
20046 {
20047 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
20048 spec = get_defaulted_eh_spec (fn);
20049 else if (push_tinst_level (fn))
20050 {
20051 push_access_scope (fn);
20052 push_deferring_access_checks (dk_no_deferred);
20053 input_location = DECL_SOURCE_LOCATION (fn);
20054 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
20055 DEFERRED_NOEXCEPT_ARGS (noex),
20056 tf_warning_or_error, fn,
20057 /*function_p=*/false,
20058 /*integral_constant_expression_p=*/true);
20059 pop_deferring_access_checks ();
20060 pop_access_scope (fn);
20061 pop_tinst_level ();
20062 spec = build_noexcept_spec (noex, tf_warning_or_error);
20063 if (spec == error_mark_node)
20064 spec = noexcept_false_spec;
20065 }
20066 else
20067 spec = noexcept_false_spec;
20068
20069 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
20070 }
20071
20072 FOR_EACH_CLONE (clone, fn)
20073 {
20074 if (TREE_TYPE (clone) == fntype)
20075 TREE_TYPE (clone) = TREE_TYPE (fn);
20076 else
20077 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
20078 }
20079 }
20080
20081 /* Produce the definition of D, a _DECL generated from a template. If
20082 DEFER_OK is nonzero, then we don't have to actually do the
20083 instantiation now; we just have to do it sometime. Normally it is
20084 an error if this is an explicit instantiation but D is undefined.
20085 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20086 explicitly instantiated class template. */
20087
20088 tree
20089 instantiate_decl (tree d, int defer_ok,
20090 bool expl_inst_class_mem_p)
20091 {
20092 tree tmpl = DECL_TI_TEMPLATE (d);
20093 tree gen_args;
20094 tree args;
20095 tree td;
20096 tree code_pattern;
20097 tree spec;
20098 tree gen_tmpl;
20099 bool pattern_defined;
20100 location_t saved_loc = input_location;
20101 int saved_unevaluated_operand = cp_unevaluated_operand;
20102 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20103 bool external_p;
20104 bool deleted_p;
20105 tree fn_context;
20106 bool nested;
20107
20108 /* This function should only be used to instantiate templates for
20109 functions and static member variables. */
20110 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
20111
20112 /* Variables are never deferred; if instantiation is required, they
20113 are instantiated right away. That allows for better code in the
20114 case that an expression refers to the value of the variable --
20115 if the variable has a constant value the referring expression can
20116 take advantage of that fact. */
20117 if (VAR_P (d)
20118 || DECL_DECLARED_CONSTEXPR_P (d))
20119 defer_ok = 0;
20120
20121 /* Don't instantiate cloned functions. Instead, instantiate the
20122 functions they cloned. */
20123 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
20124 d = DECL_CLONED_FUNCTION (d);
20125
20126 if (DECL_TEMPLATE_INSTANTIATED (d)
20127 || (TREE_CODE (d) == FUNCTION_DECL
20128 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
20129 || DECL_TEMPLATE_SPECIALIZATION (d))
20130 /* D has already been instantiated or explicitly specialized, so
20131 there's nothing for us to do here.
20132
20133 It might seem reasonable to check whether or not D is an explicit
20134 instantiation, and, if so, stop here. But when an explicit
20135 instantiation is deferred until the end of the compilation,
20136 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20137 the instantiation. */
20138 return d;
20139
20140 /* Check to see whether we know that this template will be
20141 instantiated in some other file, as with "extern template"
20142 extension. */
20143 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
20144
20145 /* In general, we do not instantiate such templates. */
20146 if (external_p && !always_instantiate_p (d))
20147 return d;
20148
20149 gen_tmpl = most_general_template (tmpl);
20150 gen_args = DECL_TI_ARGS (d);
20151
20152 if (tmpl != gen_tmpl)
20153 /* We should already have the extra args. */
20154 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
20155 == TMPL_ARGS_DEPTH (gen_args));
20156 /* And what's in the hash table should match D. */
20157 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
20158 || spec == NULL_TREE);
20159
20160 /* This needs to happen before any tsubsting. */
20161 if (! push_tinst_level (d))
20162 return d;
20163
20164 timevar_push (TV_TEMPLATE_INST);
20165
20166 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20167 for the instantiation. */
20168 td = template_for_substitution (d);
20169 code_pattern = DECL_TEMPLATE_RESULT (td);
20170
20171 /* We should never be trying to instantiate a member of a class
20172 template or partial specialization. */
20173 gcc_assert (d != code_pattern);
20174
20175 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
20176 || DECL_TEMPLATE_SPECIALIZATION (td))
20177 /* In the case of a friend template whose definition is provided
20178 outside the class, we may have too many arguments. Drop the
20179 ones we don't need. The same is true for specializations. */
20180 args = get_innermost_template_args
20181 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
20182 else
20183 args = gen_args;
20184
20185 if (TREE_CODE (d) == FUNCTION_DECL)
20186 {
20187 deleted_p = DECL_DELETED_FN (code_pattern);
20188 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
20189 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
20190 || deleted_p);
20191 }
20192 else
20193 {
20194 deleted_p = false;
20195 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
20196 }
20197
20198 /* We may be in the middle of deferred access check. Disable it now. */
20199 push_deferring_access_checks (dk_no_deferred);
20200
20201 /* Unless an explicit instantiation directive has already determined
20202 the linkage of D, remember that a definition is available for
20203 this entity. */
20204 if (pattern_defined
20205 && !DECL_INTERFACE_KNOWN (d)
20206 && !DECL_NOT_REALLY_EXTERN (d))
20207 mark_definable (d);
20208
20209 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
20210 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
20211 input_location = DECL_SOURCE_LOCATION (d);
20212
20213 /* If D is a member of an explicitly instantiated class template,
20214 and no definition is available, treat it like an implicit
20215 instantiation. */
20216 if (!pattern_defined && expl_inst_class_mem_p
20217 && DECL_EXPLICIT_INSTANTIATION (d))
20218 {
20219 /* Leave linkage flags alone on instantiations with anonymous
20220 visibility. */
20221 if (TREE_PUBLIC (d))
20222 {
20223 DECL_NOT_REALLY_EXTERN (d) = 0;
20224 DECL_INTERFACE_KNOWN (d) = 0;
20225 }
20226 SET_DECL_IMPLICIT_INSTANTIATION (d);
20227 }
20228
20229 /* Defer all other templates, unless we have been explicitly
20230 forbidden from doing so. */
20231 if (/* If there is no definition, we cannot instantiate the
20232 template. */
20233 ! pattern_defined
20234 /* If it's OK to postpone instantiation, do so. */
20235 || defer_ok
20236 /* If this is a static data member that will be defined
20237 elsewhere, we don't want to instantiate the entire data
20238 member, but we do want to instantiate the initializer so that
20239 we can substitute that elsewhere. */
20240 || (external_p && VAR_P (d))
20241 /* Handle here a deleted function too, avoid generating
20242 its body (c++/61080). */
20243 || deleted_p)
20244 {
20245 /* The definition of the static data member is now required so
20246 we must substitute the initializer. */
20247 if (VAR_P (d)
20248 && !DECL_INITIAL (d)
20249 && DECL_INITIAL (code_pattern))
20250 {
20251 tree ns;
20252 tree init;
20253 bool const_init = false;
20254 bool enter_context = DECL_CLASS_SCOPE_P (d);
20255
20256 ns = decl_namespace_context (d);
20257 push_nested_namespace (ns);
20258 if (enter_context)
20259 push_nested_class (DECL_CONTEXT (d));
20260 init = tsubst_expr (DECL_INITIAL (code_pattern),
20261 args,
20262 tf_warning_or_error, NULL_TREE,
20263 /*integral_constant_expression_p=*/false);
20264 /* If instantiating the initializer involved instantiating this
20265 again, don't call cp_finish_decl twice. */
20266 if (!DECL_INITIAL (d))
20267 {
20268 /* Make sure the initializer is still constant, in case of
20269 circular dependency (template/instantiate6.C). */
20270 const_init
20271 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20272 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
20273 /*asmspec_tree=*/NULL_TREE,
20274 LOOKUP_ONLYCONVERTING);
20275 }
20276 if (enter_context)
20277 pop_nested_class ();
20278 pop_nested_namespace (ns);
20279 }
20280
20281 /* We restore the source position here because it's used by
20282 add_pending_template. */
20283 input_location = saved_loc;
20284
20285 if (at_eof && !pattern_defined
20286 && DECL_EXPLICIT_INSTANTIATION (d)
20287 && DECL_NOT_REALLY_EXTERN (d))
20288 /* [temp.explicit]
20289
20290 The definition of a non-exported function template, a
20291 non-exported member function template, or a non-exported
20292 member function or static data member of a class template
20293 shall be present in every translation unit in which it is
20294 explicitly instantiated. */
20295 permerror (input_location, "explicit instantiation of %qD "
20296 "but no definition available", d);
20297
20298 /* If we're in unevaluated context, we just wanted to get the
20299 constant value; this isn't an odr use, so don't queue
20300 a full instantiation. */
20301 if (cp_unevaluated_operand != 0)
20302 goto out;
20303 /* ??? Historically, we have instantiated inline functions, even
20304 when marked as "extern template". */
20305 if (!(external_p && VAR_P (d)))
20306 add_pending_template (d);
20307 goto out;
20308 }
20309 /* Tell the repository that D is available in this translation unit
20310 -- and see if it is supposed to be instantiated here. */
20311 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
20312 {
20313 /* In a PCH file, despite the fact that the repository hasn't
20314 requested instantiation in the PCH it is still possible that
20315 an instantiation will be required in a file that includes the
20316 PCH. */
20317 if (pch_file)
20318 add_pending_template (d);
20319 /* Instantiate inline functions so that the inliner can do its
20320 job, even though we'll not be emitting a copy of this
20321 function. */
20322 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
20323 goto out;
20324 }
20325
20326 fn_context = decl_function_context (d);
20327 nested = (current_function_decl != NULL_TREE);
20328 if (!fn_context)
20329 push_to_top_level ();
20330 else
20331 {
20332 if (nested)
20333 push_function_context ();
20334 cp_unevaluated_operand = 0;
20335 c_inhibit_evaluation_warnings = 0;
20336 }
20337
20338 /* Mark D as instantiated so that recursive calls to
20339 instantiate_decl do not try to instantiate it again. */
20340 DECL_TEMPLATE_INSTANTIATED (d) = 1;
20341
20342 /* Regenerate the declaration in case the template has been modified
20343 by a subsequent redeclaration. */
20344 regenerate_decl_from_template (d, td);
20345
20346 /* We already set the file and line above. Reset them now in case
20347 they changed as a result of calling regenerate_decl_from_template. */
20348 input_location = DECL_SOURCE_LOCATION (d);
20349
20350 if (VAR_P (d))
20351 {
20352 tree init;
20353 bool const_init = false;
20354
20355 /* Clear out DECL_RTL; whatever was there before may not be right
20356 since we've reset the type of the declaration. */
20357 SET_DECL_RTL (d, NULL);
20358 DECL_IN_AGGR_P (d) = 0;
20359
20360 /* The initializer is placed in DECL_INITIAL by
20361 regenerate_decl_from_template so we don't need to
20362 push/pop_access_scope again here. Pull it out so that
20363 cp_finish_decl can process it. */
20364 init = DECL_INITIAL (d);
20365 DECL_INITIAL (d) = NULL_TREE;
20366 DECL_INITIALIZED_P (d) = 0;
20367
20368 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20369 initializer. That function will defer actual emission until
20370 we have a chance to determine linkage. */
20371 DECL_EXTERNAL (d) = 0;
20372
20373 /* Enter the scope of D so that access-checking works correctly. */
20374 bool enter_context = DECL_CLASS_SCOPE_P (d);
20375 if (enter_context)
20376 push_nested_class (DECL_CONTEXT (d));
20377
20378 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20379 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
20380
20381 if (enter_context)
20382 pop_nested_class ();
20383
20384 if (variable_template_p (td))
20385 note_variable_template_instantiation (d);
20386 }
20387 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
20388 synthesize_method (d);
20389 else if (TREE_CODE (d) == FUNCTION_DECL)
20390 {
20391 hash_map<tree, tree> *saved_local_specializations;
20392 tree subst_decl;
20393 tree tmpl_parm;
20394 tree spec_parm;
20395 tree block = NULL_TREE;
20396
20397 /* Save away the current list, in case we are instantiating one
20398 template from within the body of another. */
20399 saved_local_specializations = local_specializations;
20400
20401 /* Set up the list of local specializations. */
20402 local_specializations = new hash_map<tree, tree>;
20403
20404 /* Set up context. */
20405 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20406 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20407 block = push_stmt_list ();
20408 else
20409 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
20410
20411 /* Some typedefs referenced from within the template code need to be
20412 access checked at template instantiation time, i.e now. These
20413 types were added to the template at parsing time. Let's get those
20414 and perform the access checks then. */
20415 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
20416 gen_args);
20417
20418 /* Create substitution entries for the parameters. */
20419 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
20420 tmpl_parm = DECL_ARGUMENTS (subst_decl);
20421 spec_parm = DECL_ARGUMENTS (d);
20422 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
20423 {
20424 register_local_specialization (spec_parm, tmpl_parm);
20425 spec_parm = skip_artificial_parms_for (d, spec_parm);
20426 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
20427 }
20428 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
20429 {
20430 if (!DECL_PACK_P (tmpl_parm))
20431 {
20432 register_local_specialization (spec_parm, tmpl_parm);
20433 spec_parm = DECL_CHAIN (spec_parm);
20434 }
20435 else
20436 {
20437 /* Register the (value) argument pack as a specialization of
20438 TMPL_PARM, then move on. */
20439 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
20440 register_local_specialization (argpack, tmpl_parm);
20441 }
20442 }
20443 gcc_assert (!spec_parm);
20444
20445 /* Substitute into the body of the function. */
20446 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20447 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
20448 tf_warning_or_error, tmpl);
20449 else
20450 {
20451 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
20452 tf_warning_or_error, tmpl,
20453 /*integral_constant_expression_p=*/false);
20454
20455 /* Set the current input_location to the end of the function
20456 so that finish_function knows where we are. */
20457 input_location
20458 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
20459
20460 /* Remember if we saw an infinite loop in the template. */
20461 current_function_infinite_loop
20462 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
20463 }
20464
20465 /* We don't need the local specializations any more. */
20466 delete local_specializations;
20467 local_specializations = saved_local_specializations;
20468
20469 /* Finish the function. */
20470 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20471 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20472 DECL_SAVED_TREE (d) = pop_stmt_list (block);
20473 else
20474 {
20475 d = finish_function (0);
20476 expand_or_defer_fn (d);
20477 }
20478
20479 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20480 cp_check_omp_declare_reduction (d);
20481 }
20482
20483 /* We're not deferring instantiation any more. */
20484 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20485
20486 if (!fn_context)
20487 pop_from_top_level ();
20488 else if (nested)
20489 pop_function_context ();
20490
20491 out:
20492 input_location = saved_loc;
20493 cp_unevaluated_operand = saved_unevaluated_operand;
20494 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20495 pop_deferring_access_checks ();
20496 pop_tinst_level ();
20497
20498 timevar_pop (TV_TEMPLATE_INST);
20499
20500 return d;
20501 }
20502
20503 /* Run through the list of templates that we wish we could
20504 instantiate, and instantiate any we can. RETRIES is the
20505 number of times we retry pending template instantiation. */
20506
20507 void
20508 instantiate_pending_templates (int retries)
20509 {
20510 int reconsider;
20511 location_t saved_loc = input_location;
20512
20513 /* Instantiating templates may trigger vtable generation. This in turn
20514 may require further template instantiations. We place a limit here
20515 to avoid infinite loop. */
20516 if (pending_templates && retries >= max_tinst_depth)
20517 {
20518 tree decl = pending_templates->tinst->decl;
20519
20520 fatal_error (input_location,
20521 "template instantiation depth exceeds maximum of %d"
20522 " instantiating %q+D, possibly from virtual table generation"
20523 " (use -ftemplate-depth= to increase the maximum)",
20524 max_tinst_depth, decl);
20525 if (TREE_CODE (decl) == FUNCTION_DECL)
20526 /* Pretend that we defined it. */
20527 DECL_INITIAL (decl) = error_mark_node;
20528 return;
20529 }
20530
20531 do
20532 {
20533 struct pending_template **t = &pending_templates;
20534 struct pending_template *last = NULL;
20535 reconsider = 0;
20536 while (*t)
20537 {
20538 tree instantiation = reopen_tinst_level ((*t)->tinst);
20539 bool complete = false;
20540
20541 if (TYPE_P (instantiation))
20542 {
20543 tree fn;
20544
20545 if (!COMPLETE_TYPE_P (instantiation))
20546 {
20547 instantiate_class_template (instantiation);
20548 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20549 for (fn = TYPE_METHODS (instantiation);
20550 fn;
20551 fn = TREE_CHAIN (fn))
20552 if (! DECL_ARTIFICIAL (fn))
20553 instantiate_decl (fn,
20554 /*defer_ok=*/0,
20555 /*expl_inst_class_mem_p=*/false);
20556 if (COMPLETE_TYPE_P (instantiation))
20557 reconsider = 1;
20558 }
20559
20560 complete = COMPLETE_TYPE_P (instantiation);
20561 }
20562 else
20563 {
20564 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20565 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20566 {
20567 instantiation
20568 = instantiate_decl (instantiation,
20569 /*defer_ok=*/0,
20570 /*expl_inst_class_mem_p=*/false);
20571 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20572 reconsider = 1;
20573 }
20574
20575 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20576 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20577 }
20578
20579 if (complete)
20580 /* If INSTANTIATION has been instantiated, then we don't
20581 need to consider it again in the future. */
20582 *t = (*t)->next;
20583 else
20584 {
20585 last = *t;
20586 t = &(*t)->next;
20587 }
20588 tinst_depth = 0;
20589 current_tinst_level = NULL;
20590 }
20591 last_pending_template = last;
20592 }
20593 while (reconsider);
20594
20595 input_location = saved_loc;
20596 }
20597
20598 /* Substitute ARGVEC into T, which is a list of initializers for
20599 either base class or a non-static data member. The TREE_PURPOSEs
20600 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20601 instantiate_decl. */
20602
20603 static tree
20604 tsubst_initializer_list (tree t, tree argvec)
20605 {
20606 tree inits = NULL_TREE;
20607
20608 for (; t; t = TREE_CHAIN (t))
20609 {
20610 tree decl;
20611 tree init;
20612 tree expanded_bases = NULL_TREE;
20613 tree expanded_arguments = NULL_TREE;
20614 int i, len = 1;
20615
20616 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20617 {
20618 tree expr;
20619 tree arg;
20620
20621 /* Expand the base class expansion type into separate base
20622 classes. */
20623 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20624 tf_warning_or_error,
20625 NULL_TREE);
20626 if (expanded_bases == error_mark_node)
20627 continue;
20628
20629 /* We'll be building separate TREE_LISTs of arguments for
20630 each base. */
20631 len = TREE_VEC_LENGTH (expanded_bases);
20632 expanded_arguments = make_tree_vec (len);
20633 for (i = 0; i < len; i++)
20634 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20635
20636 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20637 expand each argument in the TREE_VALUE of t. */
20638 expr = make_node (EXPR_PACK_EXPANSION);
20639 PACK_EXPANSION_LOCAL_P (expr) = true;
20640 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20641 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20642
20643 if (TREE_VALUE (t) == void_type_node)
20644 /* VOID_TYPE_NODE is used to indicate
20645 value-initialization. */
20646 {
20647 for (i = 0; i < len; i++)
20648 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20649 }
20650 else
20651 {
20652 /* Substitute parameter packs into each argument in the
20653 TREE_LIST. */
20654 in_base_initializer = 1;
20655 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20656 {
20657 tree expanded_exprs;
20658
20659 /* Expand the argument. */
20660 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20661 expanded_exprs
20662 = tsubst_pack_expansion (expr, argvec,
20663 tf_warning_or_error,
20664 NULL_TREE);
20665 if (expanded_exprs == error_mark_node)
20666 continue;
20667
20668 /* Prepend each of the expanded expressions to the
20669 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20670 for (i = 0; i < len; i++)
20671 {
20672 TREE_VEC_ELT (expanded_arguments, i) =
20673 tree_cons (NULL_TREE,
20674 TREE_VEC_ELT (expanded_exprs, i),
20675 TREE_VEC_ELT (expanded_arguments, i));
20676 }
20677 }
20678 in_base_initializer = 0;
20679
20680 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20681 since we built them backwards. */
20682 for (i = 0; i < len; i++)
20683 {
20684 TREE_VEC_ELT (expanded_arguments, i) =
20685 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20686 }
20687 }
20688 }
20689
20690 for (i = 0; i < len; ++i)
20691 {
20692 if (expanded_bases)
20693 {
20694 decl = TREE_VEC_ELT (expanded_bases, i);
20695 decl = expand_member_init (decl);
20696 init = TREE_VEC_ELT (expanded_arguments, i);
20697 }
20698 else
20699 {
20700 tree tmp;
20701 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20702 tf_warning_or_error, NULL_TREE);
20703
20704 decl = expand_member_init (decl);
20705 if (decl && !DECL_P (decl))
20706 in_base_initializer = 1;
20707
20708 init = TREE_VALUE (t);
20709 tmp = init;
20710 if (init != void_type_node)
20711 init = tsubst_expr (init, argvec,
20712 tf_warning_or_error, NULL_TREE,
20713 /*integral_constant_expression_p=*/false);
20714 if (init == NULL_TREE && tmp != NULL_TREE)
20715 /* If we had an initializer but it instantiated to nothing,
20716 value-initialize the object. This will only occur when
20717 the initializer was a pack expansion where the parameter
20718 packs used in that expansion were of length zero. */
20719 init = void_type_node;
20720 in_base_initializer = 0;
20721 }
20722
20723 if (decl)
20724 {
20725 init = build_tree_list (decl, init);
20726 TREE_CHAIN (init) = inits;
20727 inits = init;
20728 }
20729 }
20730 }
20731 return inits;
20732 }
20733
20734 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20735
20736 static void
20737 set_current_access_from_decl (tree decl)
20738 {
20739 if (TREE_PRIVATE (decl))
20740 current_access_specifier = access_private_node;
20741 else if (TREE_PROTECTED (decl))
20742 current_access_specifier = access_protected_node;
20743 else
20744 current_access_specifier = access_public_node;
20745 }
20746
20747 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20748 is the instantiation (which should have been created with
20749 start_enum) and ARGS are the template arguments to use. */
20750
20751 static void
20752 tsubst_enum (tree tag, tree newtag, tree args)
20753 {
20754 tree e;
20755
20756 if (SCOPED_ENUM_P (newtag))
20757 begin_scope (sk_scoped_enum, newtag);
20758
20759 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20760 {
20761 tree value;
20762 tree decl;
20763
20764 decl = TREE_VALUE (e);
20765 /* Note that in a template enum, the TREE_VALUE is the
20766 CONST_DECL, not the corresponding INTEGER_CST. */
20767 value = tsubst_expr (DECL_INITIAL (decl),
20768 args, tf_warning_or_error, NULL_TREE,
20769 /*integral_constant_expression_p=*/true);
20770
20771 /* Give this enumeration constant the correct access. */
20772 set_current_access_from_decl (decl);
20773
20774 /* Actually build the enumerator itself. */
20775 build_enumerator
20776 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20777 }
20778
20779 if (SCOPED_ENUM_P (newtag))
20780 finish_scope ();
20781
20782 finish_enum_value_list (newtag);
20783 finish_enum (newtag);
20784
20785 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20786 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20787 }
20788
20789 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20790 its type -- but without substituting the innermost set of template
20791 arguments. So, innermost set of template parameters will appear in
20792 the type. */
20793
20794 tree
20795 get_mostly_instantiated_function_type (tree decl)
20796 {
20797 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
20798 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
20799 }
20800
20801 /* Return truthvalue if we're processing a template different from
20802 the last one involved in diagnostics. */
20803 bool
20804 problematic_instantiation_changed (void)
20805 {
20806 return current_tinst_level != last_error_tinst_level;
20807 }
20808
20809 /* Remember current template involved in diagnostics. */
20810 void
20811 record_last_problematic_instantiation (void)
20812 {
20813 last_error_tinst_level = current_tinst_level;
20814 }
20815
20816 struct tinst_level *
20817 current_instantiation (void)
20818 {
20819 return current_tinst_level;
20820 }
20821
20822 /* Return TRUE if current_function_decl is being instantiated, false
20823 otherwise. */
20824
20825 bool
20826 instantiating_current_function_p (void)
20827 {
20828 return (current_instantiation ()
20829 && current_instantiation ()->decl == current_function_decl);
20830 }
20831
20832 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20833 type. Return zero for ok, nonzero for disallowed. Issue error and
20834 warning messages under control of COMPLAIN. */
20835
20836 static int
20837 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20838 {
20839 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20840 return 0;
20841 else if (POINTER_TYPE_P (type))
20842 return 0;
20843 else if (TYPE_PTRMEM_P (type))
20844 return 0;
20845 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20846 return 0;
20847 else if (TREE_CODE (type) == TYPENAME_TYPE)
20848 return 0;
20849 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20850 return 0;
20851 else if (TREE_CODE (type) == NULLPTR_TYPE)
20852 return 0;
20853
20854 if (complain & tf_error)
20855 {
20856 if (type == error_mark_node)
20857 inform (input_location, "invalid template non-type parameter");
20858 else
20859 error ("%q#T is not a valid type for a template non-type parameter",
20860 type);
20861 }
20862 return 1;
20863 }
20864
20865 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20866 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20867
20868 static bool
20869 dependent_type_p_r (tree type)
20870 {
20871 tree scope;
20872
20873 /* [temp.dep.type]
20874
20875 A type is dependent if it is:
20876
20877 -- a template parameter. Template template parameters are types
20878 for us (since TYPE_P holds true for them) so we handle
20879 them here. */
20880 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20881 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20882 return true;
20883 /* -- a qualified-id with a nested-name-specifier which contains a
20884 class-name that names a dependent type or whose unqualified-id
20885 names a dependent type. */
20886 if (TREE_CODE (type) == TYPENAME_TYPE)
20887 return true;
20888 /* -- a cv-qualified type where the cv-unqualified type is
20889 dependent.
20890 No code is necessary for this bullet; the code below handles
20891 cv-qualified types, and we don't want to strip aliases with
20892 TYPE_MAIN_VARIANT because of DR 1558. */
20893 /* -- a compound type constructed from any dependent type. */
20894 if (TYPE_PTRMEM_P (type))
20895 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20896 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20897 (type)));
20898 else if (TYPE_PTR_P (type)
20899 || TREE_CODE (type) == REFERENCE_TYPE)
20900 return dependent_type_p (TREE_TYPE (type));
20901 else if (TREE_CODE (type) == FUNCTION_TYPE
20902 || TREE_CODE (type) == METHOD_TYPE)
20903 {
20904 tree arg_type;
20905
20906 if (dependent_type_p (TREE_TYPE (type)))
20907 return true;
20908 for (arg_type = TYPE_ARG_TYPES (type);
20909 arg_type;
20910 arg_type = TREE_CHAIN (arg_type))
20911 if (dependent_type_p (TREE_VALUE (arg_type)))
20912 return true;
20913 return false;
20914 }
20915 /* -- an array type constructed from any dependent type or whose
20916 size is specified by a constant expression that is
20917 value-dependent.
20918
20919 We checked for type- and value-dependence of the bounds in
20920 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20921 if (TREE_CODE (type) == ARRAY_TYPE)
20922 {
20923 if (TYPE_DOMAIN (type)
20924 && dependent_type_p (TYPE_DOMAIN (type)))
20925 return true;
20926 return dependent_type_p (TREE_TYPE (type));
20927 }
20928
20929 /* -- a template-id in which either the template name is a template
20930 parameter ... */
20931 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20932 return true;
20933 /* ... or any of the template arguments is a dependent type or
20934 an expression that is type-dependent or value-dependent. */
20935 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20936 && (any_dependent_template_arguments_p
20937 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20938 return true;
20939 /* For an alias template specialization, check the arguments both to the
20940 class template and the alias template. */
20941 else if (alias_template_specialization_p (type)
20942 && (any_dependent_template_arguments_p
20943 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (type)))))
20944 return true;
20945
20946 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20947 dependent; if the argument of the `typeof' expression is not
20948 type-dependent, then it should already been have resolved. */
20949 if (TREE_CODE (type) == TYPEOF_TYPE
20950 || TREE_CODE (type) == DECLTYPE_TYPE
20951 || TREE_CODE (type) == UNDERLYING_TYPE)
20952 return true;
20953
20954 /* A template argument pack is dependent if any of its packed
20955 arguments are. */
20956 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20957 {
20958 tree args = ARGUMENT_PACK_ARGS (type);
20959 int i, len = TREE_VEC_LENGTH (args);
20960 for (i = 0; i < len; ++i)
20961 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20962 return true;
20963 }
20964
20965 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20966 be template parameters. */
20967 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20968 return true;
20969
20970 /* The standard does not specifically mention types that are local
20971 to template functions or local classes, but they should be
20972 considered dependent too. For example:
20973
20974 template <int I> void f() {
20975 enum E { a = I };
20976 S<sizeof (E)> s;
20977 }
20978
20979 The size of `E' cannot be known until the value of `I' has been
20980 determined. Therefore, `E' must be considered dependent. */
20981 scope = TYPE_CONTEXT (type);
20982 if (scope && TYPE_P (scope))
20983 return dependent_type_p (scope);
20984 /* Don't use type_dependent_expression_p here, as it can lead
20985 to infinite recursion trying to determine whether a lambda
20986 nested in a lambda is dependent (c++/47687). */
20987 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20988 && DECL_LANG_SPECIFIC (scope)
20989 && DECL_TEMPLATE_INFO (scope)
20990 && (any_dependent_template_arguments_p
20991 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20992 return true;
20993
20994 /* Other types are non-dependent. */
20995 return false;
20996 }
20997
20998 /* Returns TRUE if TYPE is dependent, in the sense of
20999 [temp.dep.type]. Note that a NULL type is considered dependent. */
21000
21001 bool
21002 dependent_type_p (tree type)
21003 {
21004 /* If there are no template parameters in scope, then there can't be
21005 any dependent types. */
21006 if (!processing_template_decl)
21007 {
21008 /* If we are not processing a template, then nobody should be
21009 providing us with a dependent type. */
21010 gcc_assert (type);
21011 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
21012 return false;
21013 }
21014
21015 /* If the type is NULL, we have not computed a type for the entity
21016 in question; in that case, the type is dependent. */
21017 if (!type)
21018 return true;
21019
21020 /* Erroneous types can be considered non-dependent. */
21021 if (type == error_mark_node)
21022 return false;
21023
21024 /* If we have not already computed the appropriate value for TYPE,
21025 do so now. */
21026 if (!TYPE_DEPENDENT_P_VALID (type))
21027 {
21028 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
21029 TYPE_DEPENDENT_P_VALID (type) = 1;
21030 }
21031
21032 return TYPE_DEPENDENT_P (type);
21033 }
21034
21035 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21036 lookup. In other words, a dependent type that is not the current
21037 instantiation. */
21038
21039 bool
21040 dependent_scope_p (tree scope)
21041 {
21042 return (scope && TYPE_P (scope) && dependent_type_p (scope)
21043 && !currently_open_class (scope));
21044 }
21045
21046 /* T is a SCOPE_REF; return whether we need to consider it
21047 instantiation-dependent so that we can check access at instantiation
21048 time even though we know which member it resolves to. */
21049
21050 static bool
21051 instantiation_dependent_scope_ref_p (tree t)
21052 {
21053 if (DECL_P (TREE_OPERAND (t, 1))
21054 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
21055 && accessible_in_template_p (TREE_OPERAND (t, 0),
21056 TREE_OPERAND (t, 1)))
21057 return false;
21058 else
21059 return true;
21060 }
21061
21062 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21063 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21064 expression. */
21065
21066 /* Note that this predicate is not appropriate for general expressions;
21067 only constant expressions (that satisfy potential_constant_expression)
21068 can be tested for value dependence. */
21069
21070 bool
21071 value_dependent_expression_p (tree expression)
21072 {
21073 if (!processing_template_decl)
21074 return false;
21075
21076 /* A name declared with a dependent type. */
21077 if (DECL_P (expression) && type_dependent_expression_p (expression))
21078 return true;
21079
21080 switch (TREE_CODE (expression))
21081 {
21082 case IDENTIFIER_NODE:
21083 /* A name that has not been looked up -- must be dependent. */
21084 return true;
21085
21086 case TEMPLATE_PARM_INDEX:
21087 /* A non-type template parm. */
21088 return true;
21089
21090 case CONST_DECL:
21091 /* A non-type template parm. */
21092 if (DECL_TEMPLATE_PARM_P (expression))
21093 return true;
21094 return value_dependent_expression_p (DECL_INITIAL (expression));
21095
21096 case VAR_DECL:
21097 /* A constant with literal type and is initialized
21098 with an expression that is value-dependent.
21099
21100 Note that a non-dependent parenthesized initializer will have
21101 already been replaced with its constant value, so if we see
21102 a TREE_LIST it must be dependent. */
21103 if (DECL_INITIAL (expression)
21104 && decl_constant_var_p (expression)
21105 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
21106 /* cp_finish_decl doesn't fold reference initializers. */
21107 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
21108 || value_dependent_expression_p (DECL_INITIAL (expression))))
21109 return true;
21110 return false;
21111
21112 case DYNAMIC_CAST_EXPR:
21113 case STATIC_CAST_EXPR:
21114 case CONST_CAST_EXPR:
21115 case REINTERPRET_CAST_EXPR:
21116 case CAST_EXPR:
21117 /* These expressions are value-dependent if the type to which
21118 the cast occurs is dependent or the expression being casted
21119 is value-dependent. */
21120 {
21121 tree type = TREE_TYPE (expression);
21122
21123 if (dependent_type_p (type))
21124 return true;
21125
21126 /* A functional cast has a list of operands. */
21127 expression = TREE_OPERAND (expression, 0);
21128 if (!expression)
21129 {
21130 /* If there are no operands, it must be an expression such
21131 as "int()". This should not happen for aggregate types
21132 because it would form non-constant expressions. */
21133 gcc_assert (cxx_dialect >= cxx11
21134 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
21135
21136 return false;
21137 }
21138
21139 if (TREE_CODE (expression) == TREE_LIST)
21140 return any_value_dependent_elements_p (expression);
21141
21142 return value_dependent_expression_p (expression);
21143 }
21144
21145 case SIZEOF_EXPR:
21146 if (SIZEOF_EXPR_TYPE_P (expression))
21147 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
21148 /* FALLTHRU */
21149 case ALIGNOF_EXPR:
21150 case TYPEID_EXPR:
21151 /* A `sizeof' expression is value-dependent if the operand is
21152 type-dependent or is a pack expansion. */
21153 expression = TREE_OPERAND (expression, 0);
21154 if (PACK_EXPANSION_P (expression))
21155 return true;
21156 else if (TYPE_P (expression))
21157 return dependent_type_p (expression);
21158 return instantiation_dependent_expression_p (expression);
21159
21160 case AT_ENCODE_EXPR:
21161 /* An 'encode' expression is value-dependent if the operand is
21162 type-dependent. */
21163 expression = TREE_OPERAND (expression, 0);
21164 return dependent_type_p (expression);
21165
21166 case NOEXCEPT_EXPR:
21167 expression = TREE_OPERAND (expression, 0);
21168 return instantiation_dependent_expression_p (expression);
21169
21170 case SCOPE_REF:
21171 /* All instantiation-dependent expressions should also be considered
21172 value-dependent. */
21173 return instantiation_dependent_scope_ref_p (expression);
21174
21175 case COMPONENT_REF:
21176 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
21177 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
21178
21179 case NONTYPE_ARGUMENT_PACK:
21180 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21181 is value-dependent. */
21182 {
21183 tree values = ARGUMENT_PACK_ARGS (expression);
21184 int i, len = TREE_VEC_LENGTH (values);
21185
21186 for (i = 0; i < len; ++i)
21187 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
21188 return true;
21189
21190 return false;
21191 }
21192
21193 case TRAIT_EXPR:
21194 {
21195 tree type2 = TRAIT_EXPR_TYPE2 (expression);
21196 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
21197 || (type2 ? dependent_type_p (type2) : false));
21198 }
21199
21200 case MODOP_EXPR:
21201 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21202 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
21203
21204 case ARRAY_REF:
21205 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21206 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
21207
21208 case ADDR_EXPR:
21209 {
21210 tree op = TREE_OPERAND (expression, 0);
21211 return (value_dependent_expression_p (op)
21212 || has_value_dependent_address (op));
21213 }
21214
21215 case CALL_EXPR:
21216 {
21217 tree fn = get_callee_fndecl (expression);
21218 int i, nargs;
21219 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
21220 return true;
21221 nargs = call_expr_nargs (expression);
21222 for (i = 0; i < nargs; ++i)
21223 {
21224 tree op = CALL_EXPR_ARG (expression, i);
21225 /* In a call to a constexpr member function, look through the
21226 implicit ADDR_EXPR on the object argument so that it doesn't
21227 cause the call to be considered value-dependent. We also
21228 look through it in potential_constant_expression. */
21229 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
21230 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21231 && TREE_CODE (op) == ADDR_EXPR)
21232 op = TREE_OPERAND (op, 0);
21233 if (value_dependent_expression_p (op))
21234 return true;
21235 }
21236 return false;
21237 }
21238
21239 case TEMPLATE_ID_EXPR:
21240 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21241 type-dependent. */
21242 return type_dependent_expression_p (expression);
21243
21244 case CONSTRUCTOR:
21245 {
21246 unsigned ix;
21247 tree val;
21248 if (dependent_type_p (TREE_TYPE (expression)))
21249 return true;
21250 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
21251 if (value_dependent_expression_p (val))
21252 return true;
21253 return false;
21254 }
21255
21256 case STMT_EXPR:
21257 /* Treat a GNU statement expression as dependent to avoid crashing
21258 under instantiate_non_dependent_expr; it can't be constant. */
21259 return true;
21260
21261 default:
21262 /* A constant expression is value-dependent if any subexpression is
21263 value-dependent. */
21264 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
21265 {
21266 case tcc_reference:
21267 case tcc_unary:
21268 case tcc_comparison:
21269 case tcc_binary:
21270 case tcc_expression:
21271 case tcc_vl_exp:
21272 {
21273 int i, len = cp_tree_operand_length (expression);
21274
21275 for (i = 0; i < len; i++)
21276 {
21277 tree t = TREE_OPERAND (expression, i);
21278
21279 /* In some cases, some of the operands may be missing.l
21280 (For example, in the case of PREDECREMENT_EXPR, the
21281 amount to increment by may be missing.) That doesn't
21282 make the expression dependent. */
21283 if (t && value_dependent_expression_p (t))
21284 return true;
21285 }
21286 }
21287 break;
21288 default:
21289 break;
21290 }
21291 break;
21292 }
21293
21294 /* The expression is not value-dependent. */
21295 return false;
21296 }
21297
21298 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21299 [temp.dep.expr]. Note that an expression with no type is
21300 considered dependent. Other parts of the compiler arrange for an
21301 expression with type-dependent subexpressions to have no type, so
21302 this function doesn't have to be fully recursive. */
21303
21304 bool
21305 type_dependent_expression_p (tree expression)
21306 {
21307 if (!processing_template_decl)
21308 return false;
21309
21310 if (expression == NULL_TREE || expression == error_mark_node)
21311 return false;
21312
21313 /* An unresolved name is always dependent. */
21314 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
21315 return true;
21316
21317 /* Some expression forms are never type-dependent. */
21318 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
21319 || TREE_CODE (expression) == SIZEOF_EXPR
21320 || TREE_CODE (expression) == ALIGNOF_EXPR
21321 || TREE_CODE (expression) == AT_ENCODE_EXPR
21322 || TREE_CODE (expression) == NOEXCEPT_EXPR
21323 || TREE_CODE (expression) == TRAIT_EXPR
21324 || TREE_CODE (expression) == TYPEID_EXPR
21325 || TREE_CODE (expression) == DELETE_EXPR
21326 || TREE_CODE (expression) == VEC_DELETE_EXPR
21327 || TREE_CODE (expression) == THROW_EXPR)
21328 return false;
21329
21330 /* The types of these expressions depends only on the type to which
21331 the cast occurs. */
21332 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
21333 || TREE_CODE (expression) == STATIC_CAST_EXPR
21334 || TREE_CODE (expression) == CONST_CAST_EXPR
21335 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
21336 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
21337 || TREE_CODE (expression) == CAST_EXPR)
21338 return dependent_type_p (TREE_TYPE (expression));
21339
21340 /* The types of these expressions depends only on the type created
21341 by the expression. */
21342 if (TREE_CODE (expression) == NEW_EXPR
21343 || TREE_CODE (expression) == VEC_NEW_EXPR)
21344 {
21345 /* For NEW_EXPR tree nodes created inside a template, either
21346 the object type itself or a TREE_LIST may appear as the
21347 operand 1. */
21348 tree type = TREE_OPERAND (expression, 1);
21349 if (TREE_CODE (type) == TREE_LIST)
21350 /* This is an array type. We need to check array dimensions
21351 as well. */
21352 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
21353 || value_dependent_expression_p
21354 (TREE_OPERAND (TREE_VALUE (type), 1));
21355 else
21356 return dependent_type_p (type);
21357 }
21358
21359 if (TREE_CODE (expression) == SCOPE_REF)
21360 {
21361 tree scope = TREE_OPERAND (expression, 0);
21362 tree name = TREE_OPERAND (expression, 1);
21363
21364 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21365 contains an identifier associated by name lookup with one or more
21366 declarations declared with a dependent type, or...a
21367 nested-name-specifier or qualified-id that names a member of an
21368 unknown specialization. */
21369 return (type_dependent_expression_p (name)
21370 || dependent_scope_p (scope));
21371 }
21372
21373 if (TREE_CODE (expression) == FUNCTION_DECL
21374 && DECL_LANG_SPECIFIC (expression)
21375 && DECL_TEMPLATE_INFO (expression)
21376 && (any_dependent_template_arguments_p
21377 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21378 return true;
21379
21380 if (TREE_CODE (expression) == TEMPLATE_DECL
21381 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21382 return false;
21383
21384 if (TREE_CODE (expression) == STMT_EXPR)
21385 expression = stmt_expr_value_expr (expression);
21386
21387 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21388 {
21389 tree elt;
21390 unsigned i;
21391
21392 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
21393 {
21394 if (type_dependent_expression_p (elt))
21395 return true;
21396 }
21397 return false;
21398 }
21399
21400 /* A static data member of the current instantiation with incomplete
21401 array type is type-dependent, as the definition and specializations
21402 can have different bounds. */
21403 if (VAR_P (expression)
21404 && DECL_CLASS_SCOPE_P (expression)
21405 && dependent_type_p (DECL_CONTEXT (expression))
21406 && VAR_HAD_UNKNOWN_BOUND (expression))
21407 return true;
21408
21409 /* An array of unknown bound depending on a variadic parameter, eg:
21410
21411 template<typename... Args>
21412 void foo (Args... args)
21413 {
21414 int arr[] = { args... };
21415 }
21416
21417 template<int... vals>
21418 void bar ()
21419 {
21420 int arr[] = { vals... };
21421 }
21422
21423 If the array has no length and has an initializer, it must be that
21424 we couldn't determine its length in cp_complete_array_type because
21425 it is dependent. */
21426 if (VAR_P (expression)
21427 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
21428 && !TYPE_DOMAIN (TREE_TYPE (expression))
21429 && DECL_INITIAL (expression))
21430 return true;
21431
21432 /* A variable template specialization is type-dependent if it has any
21433 dependent template arguments. */
21434 if (VAR_P (expression)
21435 && DECL_LANG_SPECIFIC (expression)
21436 && DECL_TEMPLATE_INFO (expression)
21437 && variable_template_p (DECL_TI_TEMPLATE (expression)))
21438 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
21439
21440 if (TREE_TYPE (expression) == unknown_type_node)
21441 {
21442 if (TREE_CODE (expression) == ADDR_EXPR)
21443 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21444 if (TREE_CODE (expression) == COMPONENT_REF
21445 || TREE_CODE (expression) == OFFSET_REF)
21446 {
21447 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
21448 return true;
21449 expression = TREE_OPERAND (expression, 1);
21450 if (identifier_p (expression))
21451 return false;
21452 }
21453 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21454 if (TREE_CODE (expression) == SCOPE_REF)
21455 return false;
21456
21457 /* Always dependent, on the number of arguments if nothing else. */
21458 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21459 return true;
21460
21461 if (BASELINK_P (expression))
21462 {
21463 if (BASELINK_OPTYPE (expression)
21464 && dependent_type_p (BASELINK_OPTYPE (expression)))
21465 return true;
21466 expression = BASELINK_FUNCTIONS (expression);
21467 }
21468
21469 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21470 {
21471 if (any_dependent_template_arguments_p
21472 (TREE_OPERAND (expression, 1)))
21473 return true;
21474 expression = TREE_OPERAND (expression, 0);
21475 }
21476 gcc_assert (TREE_CODE (expression) == OVERLOAD
21477 || TREE_CODE (expression) == FUNCTION_DECL);
21478
21479 while (expression)
21480 {
21481 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21482 return true;
21483 expression = OVL_NEXT (expression);
21484 }
21485 return false;
21486 }
21487
21488 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21489
21490 return (dependent_type_p (TREE_TYPE (expression)));
21491 }
21492
21493 /* walk_tree callback function for instantiation_dependent_expression_p,
21494 below. Returns non-zero if a dependent subexpression is found. */
21495
21496 static tree
21497 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21498 void * /*data*/)
21499 {
21500 if (TYPE_P (*tp))
21501 {
21502 /* We don't have to worry about decltype currently because decltype
21503 of an instantiation-dependent expr is a dependent type. This
21504 might change depending on the resolution of DR 1172. */
21505 *walk_subtrees = false;
21506 return NULL_TREE;
21507 }
21508 enum tree_code code = TREE_CODE (*tp);
21509 switch (code)
21510 {
21511 /* Don't treat an argument list as dependent just because it has no
21512 TREE_TYPE. */
21513 case TREE_LIST:
21514 case TREE_VEC:
21515 return NULL_TREE;
21516
21517 case VAR_DECL:
21518 case CONST_DECL:
21519 /* A constant with a dependent initializer is dependent. */
21520 if (value_dependent_expression_p (*tp))
21521 return *tp;
21522 break;
21523
21524 case TEMPLATE_PARM_INDEX:
21525 return *tp;
21526
21527 /* Handle expressions with type operands. */
21528 case SIZEOF_EXPR:
21529 case ALIGNOF_EXPR:
21530 case TYPEID_EXPR:
21531 case AT_ENCODE_EXPR:
21532 {
21533 tree op = TREE_OPERAND (*tp, 0);
21534 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21535 op = TREE_TYPE (op);
21536 if (TYPE_P (op))
21537 {
21538 if (dependent_type_p (op))
21539 return *tp;
21540 else
21541 {
21542 *walk_subtrees = false;
21543 return NULL_TREE;
21544 }
21545 }
21546 break;
21547 }
21548
21549 case TRAIT_EXPR:
21550 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
21551 || (TRAIT_EXPR_TYPE2 (*tp)
21552 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
21553 return *tp;
21554 *walk_subtrees = false;
21555 return NULL_TREE;
21556
21557 case COMPONENT_REF:
21558 if (identifier_p (TREE_OPERAND (*tp, 1)))
21559 /* In a template, finish_class_member_access_expr creates a
21560 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21561 type-dependent, so that we can check access control at
21562 instantiation time (PR 42277). See also Core issue 1273. */
21563 return *tp;
21564 break;
21565
21566 case SCOPE_REF:
21567 if (instantiation_dependent_scope_ref_p (*tp))
21568 return *tp;
21569 else
21570 break;
21571
21572 /* Treat statement-expressions as dependent. */
21573 case BIND_EXPR:
21574 return *tp;
21575
21576 default:
21577 break;
21578 }
21579
21580 if (type_dependent_expression_p (*tp))
21581 return *tp;
21582 else
21583 return NULL_TREE;
21584 }
21585
21586 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21587 sense defined by the ABI:
21588
21589 "An expression is instantiation-dependent if it is type-dependent
21590 or value-dependent, or it has a subexpression that is type-dependent
21591 or value-dependent." */
21592
21593 bool
21594 instantiation_dependent_expression_p (tree expression)
21595 {
21596 tree result;
21597
21598 if (!processing_template_decl)
21599 return false;
21600
21601 if (expression == error_mark_node)
21602 return false;
21603
21604 result = cp_walk_tree_without_duplicates (&expression,
21605 instantiation_dependent_r, NULL);
21606 return result != NULL_TREE;
21607 }
21608
21609 /* Like type_dependent_expression_p, but it also works while not processing
21610 a template definition, i.e. during substitution or mangling. */
21611
21612 bool
21613 type_dependent_expression_p_push (tree expr)
21614 {
21615 bool b;
21616 ++processing_template_decl;
21617 b = type_dependent_expression_p (expr);
21618 --processing_template_decl;
21619 return b;
21620 }
21621
21622 /* Returns TRUE if ARGS contains a type-dependent expression. */
21623
21624 bool
21625 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21626 {
21627 unsigned int i;
21628 tree arg;
21629
21630 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21631 {
21632 if (type_dependent_expression_p (arg))
21633 return true;
21634 }
21635 return false;
21636 }
21637
21638 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21639 expressions) contains any type-dependent expressions. */
21640
21641 bool
21642 any_type_dependent_elements_p (const_tree list)
21643 {
21644 for (; list; list = TREE_CHAIN (list))
21645 if (type_dependent_expression_p (TREE_VALUE (list)))
21646 return true;
21647
21648 return false;
21649 }
21650
21651 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21652 expressions) contains any value-dependent expressions. */
21653
21654 bool
21655 any_value_dependent_elements_p (const_tree list)
21656 {
21657 for (; list; list = TREE_CHAIN (list))
21658 if (value_dependent_expression_p (TREE_VALUE (list)))
21659 return true;
21660
21661 return false;
21662 }
21663
21664 /* Returns TRUE if the ARG (a template argument) is dependent. */
21665
21666 bool
21667 dependent_template_arg_p (tree arg)
21668 {
21669 if (!processing_template_decl)
21670 return false;
21671
21672 /* Assume a template argument that was wrongly written by the user
21673 is dependent. This is consistent with what
21674 any_dependent_template_arguments_p [that calls this function]
21675 does. */
21676 if (!arg || arg == error_mark_node)
21677 return true;
21678
21679 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21680 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21681
21682 if (TREE_CODE (arg) == TEMPLATE_DECL
21683 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21684 return dependent_template_p (arg);
21685 else if (ARGUMENT_PACK_P (arg))
21686 {
21687 tree args = ARGUMENT_PACK_ARGS (arg);
21688 int i, len = TREE_VEC_LENGTH (args);
21689 for (i = 0; i < len; ++i)
21690 {
21691 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21692 return true;
21693 }
21694
21695 return false;
21696 }
21697 else if (TYPE_P (arg))
21698 return dependent_type_p (arg);
21699 else
21700 return (type_dependent_expression_p (arg)
21701 || value_dependent_expression_p (arg));
21702 }
21703
21704 /* Returns true if ARGS (a collection of template arguments) contains
21705 any types that require structural equality testing. */
21706
21707 bool
21708 any_template_arguments_need_structural_equality_p (tree args)
21709 {
21710 int i;
21711 int j;
21712
21713 if (!args)
21714 return false;
21715 if (args == error_mark_node)
21716 return true;
21717
21718 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21719 {
21720 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21721 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21722 {
21723 tree arg = TREE_VEC_ELT (level, j);
21724 tree packed_args = NULL_TREE;
21725 int k, len = 1;
21726
21727 if (ARGUMENT_PACK_P (arg))
21728 {
21729 /* Look inside the argument pack. */
21730 packed_args = ARGUMENT_PACK_ARGS (arg);
21731 len = TREE_VEC_LENGTH (packed_args);
21732 }
21733
21734 for (k = 0; k < len; ++k)
21735 {
21736 if (packed_args)
21737 arg = TREE_VEC_ELT (packed_args, k);
21738
21739 if (error_operand_p (arg))
21740 return true;
21741 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21742 continue;
21743 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21744 return true;
21745 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21746 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21747 return true;
21748 }
21749 }
21750 }
21751
21752 return false;
21753 }
21754
21755 /* Returns true if ARGS (a collection of template arguments) contains
21756 any dependent arguments. */
21757
21758 bool
21759 any_dependent_template_arguments_p (const_tree args)
21760 {
21761 int i;
21762 int j;
21763
21764 if (!args)
21765 return false;
21766 if (args == error_mark_node)
21767 return true;
21768
21769 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21770 {
21771 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21772 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21773 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21774 return true;
21775 }
21776
21777 return false;
21778 }
21779
21780 /* Returns TRUE if the template TMPL is dependent. */
21781
21782 bool
21783 dependent_template_p (tree tmpl)
21784 {
21785 if (TREE_CODE (tmpl) == OVERLOAD)
21786 {
21787 while (tmpl)
21788 {
21789 if (dependent_template_p (OVL_CURRENT (tmpl)))
21790 return true;
21791 tmpl = OVL_NEXT (tmpl);
21792 }
21793 return false;
21794 }
21795
21796 /* Template template parameters are dependent. */
21797 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21798 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21799 return true;
21800 /* So are names that have not been looked up. */
21801 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21802 return true;
21803 /* So are member templates of dependent classes. */
21804 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21805 return dependent_type_p (DECL_CONTEXT (tmpl));
21806 return false;
21807 }
21808
21809 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21810
21811 bool
21812 dependent_template_id_p (tree tmpl, tree args)
21813 {
21814 return (dependent_template_p (tmpl)
21815 || any_dependent_template_arguments_p (args));
21816 }
21817
21818 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21819 is dependent. */
21820
21821 bool
21822 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21823 {
21824 int i;
21825
21826 if (!processing_template_decl)
21827 return false;
21828
21829 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21830 {
21831 tree decl = TREE_VEC_ELT (declv, i);
21832 tree init = TREE_VEC_ELT (initv, i);
21833 tree cond = TREE_VEC_ELT (condv, i);
21834 tree incr = TREE_VEC_ELT (incrv, i);
21835
21836 if (type_dependent_expression_p (decl))
21837 return true;
21838
21839 if (init && type_dependent_expression_p (init))
21840 return true;
21841
21842 if (type_dependent_expression_p (cond))
21843 return true;
21844
21845 if (COMPARISON_CLASS_P (cond)
21846 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21847 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21848 return true;
21849
21850 if (TREE_CODE (incr) == MODOP_EXPR)
21851 {
21852 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21853 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21854 return true;
21855 }
21856 else if (type_dependent_expression_p (incr))
21857 return true;
21858 else if (TREE_CODE (incr) == MODIFY_EXPR)
21859 {
21860 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21861 return true;
21862 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21863 {
21864 tree t = TREE_OPERAND (incr, 1);
21865 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21866 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21867 return true;
21868 }
21869 }
21870 }
21871
21872 return false;
21873 }
21874
21875 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21876 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21877 no such TYPE can be found. Note that this function peers inside
21878 uninstantiated templates and therefore should be used only in
21879 extremely limited situations. ONLY_CURRENT_P restricts this
21880 peering to the currently open classes hierarchy (which is required
21881 when comparing types). */
21882
21883 tree
21884 resolve_typename_type (tree type, bool only_current_p)
21885 {
21886 tree scope;
21887 tree name;
21888 tree decl;
21889 int quals;
21890 tree pushed_scope;
21891 tree result;
21892
21893 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21894
21895 scope = TYPE_CONTEXT (type);
21896 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21897 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21898 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21899 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21900 identifier of the TYPENAME_TYPE anymore.
21901 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21902 TYPENAME_TYPE instead, we avoid messing up with a possible
21903 typedef variant case. */
21904 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21905
21906 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21907 it first before we can figure out what NAME refers to. */
21908 if (TREE_CODE (scope) == TYPENAME_TYPE)
21909 {
21910 if (TYPENAME_IS_RESOLVING_P (scope))
21911 /* Given a class template A with a dependent base with nested type C,
21912 typedef typename A::C::C C will land us here, as trying to resolve
21913 the initial A::C leads to the local C typedef, which leads back to
21914 A::C::C. So we break the recursion now. */
21915 return type;
21916 else
21917 scope = resolve_typename_type (scope, only_current_p);
21918 }
21919 /* If we don't know what SCOPE refers to, then we cannot resolve the
21920 TYPENAME_TYPE. */
21921 if (TREE_CODE (scope) == TYPENAME_TYPE)
21922 return type;
21923 /* If the SCOPE is a template type parameter, we have no way of
21924 resolving the name. */
21925 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21926 return type;
21927 /* If the SCOPE is not the current instantiation, there's no reason
21928 to look inside it. */
21929 if (only_current_p && !currently_open_class (scope))
21930 return type;
21931 /* If this is a typedef, we don't want to look inside (c++/11987). */
21932 if (typedef_variant_p (type))
21933 return type;
21934 /* If SCOPE isn't the template itself, it will not have a valid
21935 TYPE_FIELDS list. */
21936 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21937 /* scope is either the template itself or a compatible instantiation
21938 like X<T>, so look up the name in the original template. */
21939 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21940 else
21941 /* scope is a partial instantiation, so we can't do the lookup or we
21942 will lose the template arguments. */
21943 return type;
21944 /* Enter the SCOPE so that name lookup will be resolved as if we
21945 were in the class definition. In particular, SCOPE will no
21946 longer be considered a dependent type. */
21947 pushed_scope = push_scope (scope);
21948 /* Look up the declaration. */
21949 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21950 tf_warning_or_error);
21951
21952 result = NULL_TREE;
21953
21954 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21955 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21956 if (!decl)
21957 /*nop*/;
21958 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21959 && TREE_CODE (decl) == TYPE_DECL)
21960 {
21961 result = TREE_TYPE (decl);
21962 if (result == error_mark_node)
21963 result = NULL_TREE;
21964 }
21965 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21966 && DECL_CLASS_TEMPLATE_P (decl))
21967 {
21968 tree tmpl;
21969 tree args;
21970 /* Obtain the template and the arguments. */
21971 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21972 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21973 /* Instantiate the template. */
21974 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21975 /*entering_scope=*/0,
21976 tf_error | tf_user);
21977 if (result == error_mark_node)
21978 result = NULL_TREE;
21979 }
21980
21981 /* Leave the SCOPE. */
21982 if (pushed_scope)
21983 pop_scope (pushed_scope);
21984
21985 /* If we failed to resolve it, return the original typename. */
21986 if (!result)
21987 return type;
21988
21989 /* If lookup found a typename type, resolve that too. */
21990 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21991 {
21992 /* Ill-formed programs can cause infinite recursion here, so we
21993 must catch that. */
21994 TYPENAME_IS_RESOLVING_P (type) = 1;
21995 result = resolve_typename_type (result, only_current_p);
21996 TYPENAME_IS_RESOLVING_P (type) = 0;
21997 }
21998
21999 /* Qualify the resulting type. */
22000 quals = cp_type_quals (type);
22001 if (quals)
22002 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
22003
22004 return result;
22005 }
22006
22007 /* EXPR is an expression which is not type-dependent. Return a proxy
22008 for EXPR that can be used to compute the types of larger
22009 expressions containing EXPR. */
22010
22011 tree
22012 build_non_dependent_expr (tree expr)
22013 {
22014 tree inner_expr;
22015
22016 #ifdef ENABLE_CHECKING
22017 /* Try to get a constant value for all non-dependent expressions in
22018 order to expose bugs in *_dependent_expression_p and constexpr. */
22019 if (cxx_dialect >= cxx11)
22020 fold_non_dependent_expr (expr);
22021 #endif
22022
22023 /* Preserve OVERLOADs; the functions must be available to resolve
22024 types. */
22025 inner_expr = expr;
22026 if (TREE_CODE (inner_expr) == STMT_EXPR)
22027 inner_expr = stmt_expr_value_expr (inner_expr);
22028 if (TREE_CODE (inner_expr) == ADDR_EXPR)
22029 inner_expr = TREE_OPERAND (inner_expr, 0);
22030 if (TREE_CODE (inner_expr) == COMPONENT_REF)
22031 inner_expr = TREE_OPERAND (inner_expr, 1);
22032 if (is_overloaded_fn (inner_expr)
22033 || TREE_CODE (inner_expr) == OFFSET_REF)
22034 return expr;
22035 /* There is no need to return a proxy for a variable. */
22036 if (VAR_P (expr))
22037 return expr;
22038 /* Preserve string constants; conversions from string constants to
22039 "char *" are allowed, even though normally a "const char *"
22040 cannot be used to initialize a "char *". */
22041 if (TREE_CODE (expr) == STRING_CST)
22042 return expr;
22043 /* Preserve void and arithmetic constants, as an optimization -- there is no
22044 reason to create a new node. */
22045 if (TREE_CODE (expr) == VOID_CST
22046 || TREE_CODE (expr) == INTEGER_CST
22047 || TREE_CODE (expr) == REAL_CST)
22048 return expr;
22049 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22050 There is at least one place where we want to know that a
22051 particular expression is a throw-expression: when checking a ?:
22052 expression, there are special rules if the second or third
22053 argument is a throw-expression. */
22054 if (TREE_CODE (expr) == THROW_EXPR)
22055 return expr;
22056
22057 /* Don't wrap an initializer list, we need to be able to look inside. */
22058 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
22059 return expr;
22060
22061 /* Don't wrap a dummy object, we need to be able to test for it. */
22062 if (is_dummy_object (expr))
22063 return expr;
22064
22065 if (TREE_CODE (expr) == COND_EXPR)
22066 return build3 (COND_EXPR,
22067 TREE_TYPE (expr),
22068 TREE_OPERAND (expr, 0),
22069 (TREE_OPERAND (expr, 1)
22070 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
22071 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
22072 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
22073 if (TREE_CODE (expr) == COMPOUND_EXPR
22074 && !COMPOUND_EXPR_OVERLOADED (expr))
22075 return build2 (COMPOUND_EXPR,
22076 TREE_TYPE (expr),
22077 TREE_OPERAND (expr, 0),
22078 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
22079
22080 /* If the type is unknown, it can't really be non-dependent */
22081 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
22082
22083 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22084 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
22085 }
22086
22087 /* ARGS is a vector of expressions as arguments to a function call.
22088 Replace the arguments with equivalent non-dependent expressions.
22089 This modifies ARGS in place. */
22090
22091 void
22092 make_args_non_dependent (vec<tree, va_gc> *args)
22093 {
22094 unsigned int ix;
22095 tree arg;
22096
22097 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
22098 {
22099 tree newarg = build_non_dependent_expr (arg);
22100 if (newarg != arg)
22101 (*args)[ix] = newarg;
22102 }
22103 }
22104
22105 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22106 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22107 parms. */
22108
22109 static tree
22110 make_auto_1 (tree name)
22111 {
22112 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
22113 TYPE_NAME (au) = build_decl (input_location,
22114 TYPE_DECL, name, au);
22115 TYPE_STUB_DECL (au) = TYPE_NAME (au);
22116 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
22117 (0, processing_template_decl + 1, processing_template_decl + 1,
22118 TYPE_NAME (au), NULL_TREE);
22119 TYPE_CANONICAL (au) = canonical_type_parameter (au);
22120 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
22121 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
22122
22123 return au;
22124 }
22125
22126 tree
22127 make_decltype_auto (void)
22128 {
22129 return make_auto_1 (get_identifier ("decltype(auto)"));
22130 }
22131
22132 tree
22133 make_auto (void)
22134 {
22135 return make_auto_1 (get_identifier ("auto"));
22136 }
22137
22138 /* Given type ARG, return std::initializer_list<ARG>. */
22139
22140 static tree
22141 listify (tree arg)
22142 {
22143 tree std_init_list = namespace_binding
22144 (get_identifier ("initializer_list"), std_node);
22145 tree argvec;
22146 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
22147 {
22148 error ("deducing from brace-enclosed initializer list requires "
22149 "#include <initializer_list>");
22150 return error_mark_node;
22151 }
22152 argvec = make_tree_vec (1);
22153 TREE_VEC_ELT (argvec, 0) = arg;
22154 return lookup_template_class (std_init_list, argvec, NULL_TREE,
22155 NULL_TREE, 0, tf_warning_or_error);
22156 }
22157
22158 /* Replace auto in TYPE with std::initializer_list<auto>. */
22159
22160 static tree
22161 listify_autos (tree type, tree auto_node)
22162 {
22163 tree init_auto = listify (auto_node);
22164 tree argvec = make_tree_vec (1);
22165 TREE_VEC_ELT (argvec, 0) = init_auto;
22166 if (processing_template_decl)
22167 argvec = add_to_template_args (current_template_args (), argvec);
22168 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22169 }
22170
22171 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22172 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22173
22174 tree
22175 do_auto_deduction (tree type, tree init, tree auto_node)
22176 {
22177 tree targs;
22178
22179 if (init == error_mark_node)
22180 return error_mark_node;
22181
22182 if (type_dependent_expression_p (init))
22183 /* Defining a subset of type-dependent expressions that we can deduce
22184 from ahead of time isn't worth the trouble. */
22185 return type;
22186
22187 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22188 with either a new invented type template parameter U or, if the
22189 initializer is a braced-init-list (8.5.4), with
22190 std::initializer_list<U>. */
22191 if (BRACE_ENCLOSED_INITIALIZER_P (init))
22192 {
22193 if (!DIRECT_LIST_INIT_P (init))
22194 type = listify_autos (type, auto_node);
22195 else if (CONSTRUCTOR_NELTS (init) == 1)
22196 init = CONSTRUCTOR_ELT (init, 0)->value;
22197 else
22198 {
22199 if (permerror (input_location, "direct-list-initialization of "
22200 "%<auto%> requires exactly one element"))
22201 inform (input_location,
22202 "for deduction to %<std::initializer_list%>, use copy-"
22203 "list-initialization (i.e. add %<=%> before the %<{%>)");
22204 type = listify_autos (type, auto_node);
22205 }
22206 }
22207
22208 init = resolve_nondeduced_context (init);
22209
22210 targs = make_tree_vec (1);
22211 if (AUTO_IS_DECLTYPE (auto_node))
22212 {
22213 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
22214 && !REF_PARENTHESIZED_P (init)));
22215 TREE_VEC_ELT (targs, 0)
22216 = finish_decltype_type (init, id, tf_warning_or_error);
22217 if (type != auto_node)
22218 {
22219 error ("%qT as type rather than plain %<decltype(auto)%>", type);
22220 return error_mark_node;
22221 }
22222 }
22223 else
22224 {
22225 tree parms = build_tree_list (NULL_TREE, type);
22226 tree tparms = make_tree_vec (1);
22227 int val;
22228
22229 TREE_VEC_ELT (tparms, 0)
22230 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
22231 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
22232 DEDUCE_CALL, LOOKUP_NORMAL,
22233 NULL, /*explain_p=*/false);
22234 if (val > 0)
22235 {
22236 if (processing_template_decl)
22237 /* Try again at instantiation time. */
22238 return type;
22239 if (type && type != error_mark_node)
22240 /* If type is error_mark_node a diagnostic must have been
22241 emitted by now. Also, having a mention to '<type error>'
22242 in the diagnostic is not really useful to the user. */
22243 {
22244 if (cfun && auto_node == current_function_auto_return_pattern
22245 && LAMBDA_FUNCTION_P (current_function_decl))
22246 error ("unable to deduce lambda return type from %qE", init);
22247 else
22248 error ("unable to deduce %qT from %qE", type, init);
22249 }
22250 return error_mark_node;
22251 }
22252 }
22253
22254 /* If the list of declarators contains more than one declarator, the type
22255 of each declared variable is determined as described above. If the
22256 type deduced for the template parameter U is not the same in each
22257 deduction, the program is ill-formed. */
22258 if (TREE_TYPE (auto_node)
22259 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
22260 {
22261 if (cfun && auto_node == current_function_auto_return_pattern
22262 && LAMBDA_FUNCTION_P (current_function_decl))
22263 error ("inconsistent types %qT and %qT deduced for "
22264 "lambda return type", TREE_TYPE (auto_node),
22265 TREE_VEC_ELT (targs, 0));
22266 else
22267 error ("inconsistent deduction for %qT: %qT and then %qT",
22268 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
22269 return error_mark_node;
22270 }
22271 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
22272
22273 if (processing_template_decl)
22274 targs = add_to_template_args (current_template_args (), targs);
22275 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
22276 }
22277
22278 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22279 result. */
22280
22281 tree
22282 splice_late_return_type (tree type, tree late_return_type)
22283 {
22284 tree argvec;
22285
22286 if (late_return_type == NULL_TREE)
22287 return type;
22288 argvec = make_tree_vec (1);
22289 TREE_VEC_ELT (argvec, 0) = late_return_type;
22290 if (processing_template_parmlist)
22291 /* For a late-specified return type in a template type-parameter, we
22292 need to add a dummy argument level for its parmlist. */
22293 argvec = add_to_template_args
22294 (make_tree_vec (processing_template_parmlist), argvec);
22295 if (current_template_parms)
22296 argvec = add_to_template_args (current_template_args (), argvec);
22297 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22298 }
22299
22300 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22301 'decltype(auto)'. */
22302
22303 bool
22304 is_auto (const_tree type)
22305 {
22306 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22307 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
22308 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
22309 return true;
22310 else
22311 return false;
22312 }
22313
22314 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22315 a use of `auto'. Returns NULL_TREE otherwise. */
22316
22317 tree
22318 type_uses_auto (tree type)
22319 {
22320 return find_type_usage (type, is_auto);
22321 }
22322
22323 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22324 'decltype(auto)' or a concept. */
22325
22326 bool
22327 is_auto_or_concept (const_tree type)
22328 {
22329 return is_auto (type); // or concept
22330 }
22331
22332 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22333 a concept identifier) iff TYPE contains a use of a generic type. Returns
22334 NULL_TREE otherwise. */
22335
22336 tree
22337 type_uses_auto_or_concept (tree type)
22338 {
22339 return find_type_usage (type, is_auto_or_concept);
22340 }
22341
22342
22343 /* For a given template T, return the vector of typedefs referenced
22344 in T for which access check is needed at T instantiation time.
22345 T is either a FUNCTION_DECL or a RECORD_TYPE.
22346 Those typedefs were added to T by the function
22347 append_type_to_template_for_access_check. */
22348
22349 vec<qualified_typedef_usage_t, va_gc> *
22350 get_types_needing_access_check (tree t)
22351 {
22352 tree ti;
22353 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
22354
22355 if (!t || t == error_mark_node)
22356 return NULL;
22357
22358 if (!(ti = get_template_info (t)))
22359 return NULL;
22360
22361 if (CLASS_TYPE_P (t)
22362 || TREE_CODE (t) == FUNCTION_DECL)
22363 {
22364 if (!TI_TEMPLATE (ti))
22365 return NULL;
22366
22367 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
22368 }
22369
22370 return result;
22371 }
22372
22373 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22374 tied to T. That list of typedefs will be access checked at
22375 T instantiation time.
22376 T is either a FUNCTION_DECL or a RECORD_TYPE.
22377 TYPE_DECL is a TYPE_DECL node representing a typedef.
22378 SCOPE is the scope through which TYPE_DECL is accessed.
22379 LOCATION is the location of the usage point of TYPE_DECL.
22380
22381 This function is a subroutine of
22382 append_type_to_template_for_access_check. */
22383
22384 static void
22385 append_type_to_template_for_access_check_1 (tree t,
22386 tree type_decl,
22387 tree scope,
22388 location_t location)
22389 {
22390 qualified_typedef_usage_t typedef_usage;
22391 tree ti;
22392
22393 if (!t || t == error_mark_node)
22394 return;
22395
22396 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
22397 || CLASS_TYPE_P (t))
22398 && type_decl
22399 && TREE_CODE (type_decl) == TYPE_DECL
22400 && scope);
22401
22402 if (!(ti = get_template_info (t)))
22403 return;
22404
22405 gcc_assert (TI_TEMPLATE (ti));
22406
22407 typedef_usage.typedef_decl = type_decl;
22408 typedef_usage.context = scope;
22409 typedef_usage.locus = location;
22410
22411 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
22412 }
22413
22414 /* Append TYPE_DECL to the template TEMPL.
22415 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22416 At TEMPL instanciation time, TYPE_DECL will be checked to see
22417 if it can be accessed through SCOPE.
22418 LOCATION is the location of the usage point of TYPE_DECL.
22419
22420 e.g. consider the following code snippet:
22421
22422 class C
22423 {
22424 typedef int myint;
22425 };
22426
22427 template<class U> struct S
22428 {
22429 C::myint mi; // <-- usage point of the typedef C::myint
22430 };
22431
22432 S<char> s;
22433
22434 At S<char> instantiation time, we need to check the access of C::myint
22435 In other words, we need to check the access of the myint typedef through
22436 the C scope. For that purpose, this function will add the myint typedef
22437 and the scope C through which its being accessed to a list of typedefs
22438 tied to the template S. That list will be walked at template instantiation
22439 time and access check performed on each typedefs it contains.
22440 Note that this particular code snippet should yield an error because
22441 myint is private to C. */
22442
22443 void
22444 append_type_to_template_for_access_check (tree templ,
22445 tree type_decl,
22446 tree scope,
22447 location_t location)
22448 {
22449 qualified_typedef_usage_t *iter;
22450 unsigned i;
22451
22452 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
22453
22454 /* Make sure we don't append the type to the template twice. */
22455 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
22456 if (iter->typedef_decl == type_decl && scope == iter->context)
22457 return;
22458
22459 append_type_to_template_for_access_check_1 (templ, type_decl,
22460 scope, location);
22461 }
22462
22463 /* Convert the generic type parameters in PARM that match the types given in the
22464 range [START_IDX, END_IDX) from the current_template_parms into generic type
22465 packs. */
22466
22467 tree
22468 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22469 {
22470 tree current = current_template_parms;
22471 int depth = TMPL_PARMS_DEPTH (current);
22472 current = INNERMOST_TEMPLATE_PARMS (current);
22473 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22474
22475 for (int i = 0; i < start_idx; ++i)
22476 TREE_VEC_ELT (replacement, i)
22477 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22478
22479 for (int i = start_idx; i < end_idx; ++i)
22480 {
22481 /* Create a distinct parameter pack type from the current parm and add it
22482 to the replacement args to tsubst below into the generic function
22483 parameter. */
22484
22485 tree o = TREE_TYPE (TREE_VALUE
22486 (TREE_VEC_ELT (current, i)));
22487 tree t = copy_type (o);
22488 TEMPLATE_TYPE_PARM_INDEX (t)
22489 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22490 o, 0, 0, tf_none);
22491 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22492 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22493 TYPE_MAIN_VARIANT (t) = t;
22494 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22495 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22496 TREE_VEC_ELT (replacement, i) = t;
22497 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22498 }
22499
22500 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22501 TREE_VEC_ELT (replacement, i)
22502 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22503
22504 /* If there are more levels then build up the replacement with the outer
22505 template parms. */
22506 if (depth > 1)
22507 replacement = add_to_template_args (template_parms_to_args
22508 (TREE_CHAIN (current_template_parms)),
22509 replacement);
22510
22511 return tsubst (parm, replacement, tf_none, NULL_TREE);
22512 }
22513
22514
22515 /* Set up the hash tables for template instantiations. */
22516
22517 void
22518 init_template_processing (void)
22519 {
22520 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
22521 type_specializations = hash_table<spec_hasher>::create_ggc (37);
22522 }
22523
22524 /* Print stats about the template hash tables for -fstats. */
22525
22526 void
22527 print_template_statistics (void)
22528 {
22529 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22530 "%f collisions\n", (long) decl_specializations->size (),
22531 (long) decl_specializations->elements (),
22532 decl_specializations->collisions ());
22533 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22534 "%f collisions\n", (long) type_specializations->size (),
22535 (long) type_specializations->elements (),
22536 type_specializations->collisions ());
22537 }
22538
22539 #include "gt-cp-pt.h"