re PR c++/67130 (ICE: tree check: expected identifier_node, have template_decl in...
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "alias.h"
32 #include "tree.h"
33 #include "stringpool.h"
34 #include "varasm.h"
35 #include "attribs.h"
36 #include "stor-layout.h"
37 #include "intl.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "timevar.h"
47 #include "tree-iterator.h"
48 #include "type-utils.h"
49 #include "gimplify.h"
50
51 /* The type of functions taking a tree, and some additional data, and
52 returning an int. */
53 typedef int (*tree_fn_t) (tree, void*);
54
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY ((chain_next ("%h.next"))) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
61 };
62
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
65
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static GTY(()) tree saved_trees;
70 static vec<int> inline_parm_levels;
71
72 static GTY(()) struct tinst_level *current_tinst_level;
73
74 static GTY(()) tree saved_access_scope;
75
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
80
81 /* True if we've recursed into fn_type_unification too many times. */
82 static bool excessive_deduction_depth;
83
84 struct GTY((for_user)) spec_entry
85 {
86 tree tmpl;
87 tree args;
88 tree spec;
89 };
90
91 struct spec_hasher : ggc_ptr_hash<spec_entry>
92 {
93 static hashval_t hash (spec_entry *);
94 static bool equal (spec_entry *, spec_entry *);
95 };
96
97 static GTY (()) hash_table<spec_hasher> *decl_specializations;
98
99 static GTY (()) hash_table<spec_hasher> *type_specializations;
100
101 /* Contains canonical template parameter types. The vector is indexed by
102 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
103 TREE_LIST, whose TREE_VALUEs contain the canonical template
104 parameters of various types and levels. */
105 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
106
107 #define UNIFY_ALLOW_NONE 0
108 #define UNIFY_ALLOW_MORE_CV_QUAL 1
109 #define UNIFY_ALLOW_LESS_CV_QUAL 2
110 #define UNIFY_ALLOW_DERIVED 4
111 #define UNIFY_ALLOW_INTEGER 8
112 #define UNIFY_ALLOW_OUTER_LEVEL 16
113 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
114 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
115
116 enum template_base_result {
117 tbr_incomplete_type,
118 tbr_ambiguous_baseclass,
119 tbr_success
120 };
121
122 static void push_access_scope (tree);
123 static void pop_access_scope (tree);
124 static bool resolve_overloaded_unification (tree, tree, tree, tree,
125 unification_kind_t, int,
126 bool);
127 static int try_one_overload (tree, tree, tree, tree, tree,
128 unification_kind_t, int, bool, bool);
129 static int unify (tree, tree, tree, tree, int, bool);
130 static void add_pending_template (tree);
131 static tree reopen_tinst_level (struct tinst_level *);
132 static tree tsubst_initializer_list (tree, tree);
133 static tree get_partial_spec_bindings (tree, tree, tree, tree);
134 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
135 bool, bool);
136 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
137 bool, bool);
138 static void tsubst_enum (tree, tree, tree);
139 static tree add_to_template_args (tree, tree);
140 static tree add_outermost_template_args (tree, tree);
141 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
142 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
143 tree);
144 static int type_unification_real (tree, tree, tree, const tree *,
145 unsigned int, int, unification_kind_t, int,
146 vec<deferred_access_check, va_gc> **,
147 bool);
148 static void note_template_header (int);
149 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
150 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
151 static tree convert_template_argument (tree, tree, tree,
152 tsubst_flags_t, int, tree);
153 static int for_each_template_parm (tree, tree_fn_t, void*,
154 hash_set<tree> *, bool);
155 static tree expand_template_argument_pack (tree);
156 static tree build_template_parm_index (int, int, int, tree, tree);
157 static bool inline_needs_template_parms (tree, bool);
158 static void push_inline_template_parms_recursive (tree, int);
159 static tree retrieve_local_specialization (tree);
160 static void register_local_specialization (tree, tree);
161 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
162 static int mark_template_parm (tree, void *);
163 static int template_parm_this_level_p (tree, void *);
164 static tree tsubst_friend_function (tree, tree);
165 static tree tsubst_friend_class (tree, tree);
166 static int can_complete_type_without_circularity (tree);
167 static tree get_bindings (tree, tree, tree, bool);
168 static int template_decl_level (tree);
169 static int check_cv_quals_for_unify (int, tree, tree);
170 static void template_parm_level_and_index (tree, int*, int*);
171 static int unify_pack_expansion (tree, tree, tree,
172 tree, unification_kind_t, bool, bool);
173 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
175 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
176 static void regenerate_decl_from_template (tree, tree);
177 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
178 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
179 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
180 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
181 static bool check_specialization_scope (void);
182 static tree process_partial_specialization (tree);
183 static void set_current_access_from_decl (tree);
184 static enum template_base_result get_template_base (tree, tree, tree, tree,
185 bool , tree *);
186 static tree try_class_unification (tree, tree, tree, tree, bool);
187 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
188 tree, tree);
189 static bool template_template_parm_bindings_ok_p (tree, tree);
190 static int template_args_equal (tree, tree);
191 static void tsubst_default_arguments (tree, tsubst_flags_t);
192 static tree for_each_template_parm_r (tree *, int *, void *);
193 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
194 static void copy_default_args_to_explicit_spec (tree);
195 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
196 static bool dependent_template_arg_p (tree);
197 static bool any_template_arguments_need_structural_equality_p (tree);
198 static bool dependent_type_p_r (tree);
199 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
200 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
201 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
202 static tree tsubst_decl (tree, tree, tsubst_flags_t);
203 static void perform_typedefs_access_check (tree tmpl, tree targs);
204 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
205 location_t);
206 static tree listify (tree);
207 static tree listify_autos (tree, tree);
208 static tree template_parm_to_arg (tree t);
209 static tree current_template_args (void);
210 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
211 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
212 static bool complex_alias_template_p (const_tree tmpl);
213
214 /* Make the current scope suitable for access checking when we are
215 processing T. T can be FUNCTION_DECL for instantiated function
216 template, VAR_DECL for static member variable, or TYPE_DECL for
217 alias template (needed by instantiate_decl). */
218
219 static void
220 push_access_scope (tree t)
221 {
222 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
223 || TREE_CODE (t) == TYPE_DECL);
224
225 if (DECL_FRIEND_CONTEXT (t))
226 push_nested_class (DECL_FRIEND_CONTEXT (t));
227 else if (DECL_CLASS_SCOPE_P (t))
228 push_nested_class (DECL_CONTEXT (t));
229 else
230 push_to_top_level ();
231
232 if (TREE_CODE (t) == FUNCTION_DECL)
233 {
234 saved_access_scope = tree_cons
235 (NULL_TREE, current_function_decl, saved_access_scope);
236 current_function_decl = t;
237 }
238 }
239
240 /* Restore the scope set up by push_access_scope. T is the node we
241 are processing. */
242
243 static void
244 pop_access_scope (tree t)
245 {
246 if (TREE_CODE (t) == FUNCTION_DECL)
247 {
248 current_function_decl = TREE_VALUE (saved_access_scope);
249 saved_access_scope = TREE_CHAIN (saved_access_scope);
250 }
251
252 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
253 pop_nested_class ();
254 else
255 pop_from_top_level ();
256 }
257
258 /* Do any processing required when DECL (a member template
259 declaration) is finished. Returns the TEMPLATE_DECL corresponding
260 to DECL, unless it is a specialization, in which case the DECL
261 itself is returned. */
262
263 tree
264 finish_member_template_decl (tree decl)
265 {
266 if (decl == error_mark_node)
267 return error_mark_node;
268
269 gcc_assert (DECL_P (decl));
270
271 if (TREE_CODE (decl) == TYPE_DECL)
272 {
273 tree type;
274
275 type = TREE_TYPE (decl);
276 if (type == error_mark_node)
277 return error_mark_node;
278 if (MAYBE_CLASS_TYPE_P (type)
279 && CLASSTYPE_TEMPLATE_INFO (type)
280 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
281 {
282 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
283 check_member_template (tmpl);
284 return tmpl;
285 }
286 return NULL_TREE;
287 }
288 else if (TREE_CODE (decl) == FIELD_DECL)
289 error ("data member %qD cannot be a member template", decl);
290 else if (DECL_TEMPLATE_INFO (decl))
291 {
292 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
293 {
294 check_member_template (DECL_TI_TEMPLATE (decl));
295 return DECL_TI_TEMPLATE (decl);
296 }
297 else
298 return decl;
299 }
300 else
301 error ("invalid member template declaration %qD", decl);
302
303 return error_mark_node;
304 }
305
306 /* Create a template info node. */
307
308 tree
309 build_template_info (tree template_decl, tree template_args)
310 {
311 tree result = make_node (TEMPLATE_INFO);
312 TI_TEMPLATE (result) = template_decl;
313 TI_ARGS (result) = template_args;
314 return result;
315 }
316
317 /* Return the template info node corresponding to T, whatever T is. */
318
319 tree
320 get_template_info (const_tree t)
321 {
322 tree tinfo = NULL_TREE;
323
324 if (!t || t == error_mark_node)
325 return NULL;
326
327 if (TREE_CODE (t) == NAMESPACE_DECL)
328 return NULL;
329
330 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
331 tinfo = DECL_TEMPLATE_INFO (t);
332
333 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
334 t = TREE_TYPE (t);
335
336 if (OVERLOAD_TYPE_P (t))
337 tinfo = TYPE_TEMPLATE_INFO (t);
338 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
339 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
340
341 return tinfo;
342 }
343
344 /* Returns the template nesting level of the indicated class TYPE.
345
346 For example, in:
347 template <class T>
348 struct A
349 {
350 template <class U>
351 struct B {};
352 };
353
354 A<T>::B<U> has depth two, while A<T> has depth one.
355 Both A<T>::B<int> and A<int>::B<U> have depth one, if
356 they are instantiations, not specializations.
357
358 This function is guaranteed to return 0 if passed NULL_TREE so
359 that, for example, `template_class_depth (current_class_type)' is
360 always safe. */
361
362 int
363 template_class_depth (tree type)
364 {
365 int depth;
366
367 for (depth = 0;
368 type && TREE_CODE (type) != NAMESPACE_DECL;
369 type = (TREE_CODE (type) == FUNCTION_DECL)
370 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
371 {
372 tree tinfo = get_template_info (type);
373
374 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
375 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
376 ++depth;
377 }
378
379 return depth;
380 }
381
382 /* Subroutine of maybe_begin_member_template_processing.
383 Returns true if processing DECL needs us to push template parms. */
384
385 static bool
386 inline_needs_template_parms (tree decl, bool nsdmi)
387 {
388 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
389 return false;
390
391 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
392 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
393 }
394
395 /* Subroutine of maybe_begin_member_template_processing.
396 Push the template parms in PARMS, starting from LEVELS steps into the
397 chain, and ending at the beginning, since template parms are listed
398 innermost first. */
399
400 static void
401 push_inline_template_parms_recursive (tree parmlist, int levels)
402 {
403 tree parms = TREE_VALUE (parmlist);
404 int i;
405
406 if (levels > 1)
407 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
408
409 ++processing_template_decl;
410 current_template_parms
411 = tree_cons (size_int (processing_template_decl),
412 parms, current_template_parms);
413 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
414
415 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
416 NULL);
417 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
418 {
419 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
420
421 if (error_operand_p (parm))
422 continue;
423
424 gcc_assert (DECL_P (parm));
425
426 switch (TREE_CODE (parm))
427 {
428 case TYPE_DECL:
429 case TEMPLATE_DECL:
430 pushdecl (parm);
431 break;
432
433 case PARM_DECL:
434 {
435 /* Make a CONST_DECL as is done in process_template_parm.
436 It is ugly that we recreate this here; the original
437 version built in process_template_parm is no longer
438 available. */
439 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
440 CONST_DECL, DECL_NAME (parm),
441 TREE_TYPE (parm));
442 DECL_ARTIFICIAL (decl) = 1;
443 TREE_CONSTANT (decl) = 1;
444 TREE_READONLY (decl) = 1;
445 DECL_INITIAL (decl) = DECL_INITIAL (parm);
446 SET_DECL_TEMPLATE_PARM_P (decl);
447 pushdecl (decl);
448 }
449 break;
450
451 default:
452 gcc_unreachable ();
453 }
454 }
455 }
456
457 /* Restore the template parameter context for a member template, a
458 friend template defined in a class definition, or a non-template
459 member of template class. */
460
461 void
462 maybe_begin_member_template_processing (tree decl)
463 {
464 tree parms;
465 int levels = 0;
466 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
467
468 if (nsdmi)
469 {
470 tree ctx = DECL_CONTEXT (decl);
471 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
472 /* Disregard full specializations (c++/60999). */
473 && uses_template_parms (ctx)
474 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
475 }
476
477 if (inline_needs_template_parms (decl, nsdmi))
478 {
479 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
480 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
481
482 if (DECL_TEMPLATE_SPECIALIZATION (decl))
483 {
484 --levels;
485 parms = TREE_CHAIN (parms);
486 }
487
488 push_inline_template_parms_recursive (parms, levels);
489 }
490
491 /* Remember how many levels of template parameters we pushed so that
492 we can pop them later. */
493 inline_parm_levels.safe_push (levels);
494 }
495
496 /* Undo the effects of maybe_begin_member_template_processing. */
497
498 void
499 maybe_end_member_template_processing (void)
500 {
501 int i;
502 int last;
503
504 if (inline_parm_levels.length () == 0)
505 return;
506
507 last = inline_parm_levels.pop ();
508 for (i = 0; i < last; ++i)
509 {
510 --processing_template_decl;
511 current_template_parms = TREE_CHAIN (current_template_parms);
512 poplevel (0, 0, 0);
513 }
514 }
515
516 /* Return a new template argument vector which contains all of ARGS,
517 but has as its innermost set of arguments the EXTRA_ARGS. */
518
519 static tree
520 add_to_template_args (tree args, tree extra_args)
521 {
522 tree new_args;
523 int extra_depth;
524 int i;
525 int j;
526
527 if (args == NULL_TREE || extra_args == error_mark_node)
528 return extra_args;
529
530 extra_depth = TMPL_ARGS_DEPTH (extra_args);
531 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
532
533 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
534 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
535
536 for (j = 1; j <= extra_depth; ++j, ++i)
537 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
538
539 return new_args;
540 }
541
542 /* Like add_to_template_args, but only the outermost ARGS are added to
543 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
544 (EXTRA_ARGS) levels are added. This function is used to combine
545 the template arguments from a partial instantiation with the
546 template arguments used to attain the full instantiation from the
547 partial instantiation. */
548
549 static tree
550 add_outermost_template_args (tree args, tree extra_args)
551 {
552 tree new_args;
553
554 /* If there are more levels of EXTRA_ARGS than there are ARGS,
555 something very fishy is going on. */
556 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
557
558 /* If *all* the new arguments will be the EXTRA_ARGS, just return
559 them. */
560 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
561 return extra_args;
562
563 /* For the moment, we make ARGS look like it contains fewer levels. */
564 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
565
566 new_args = add_to_template_args (args, extra_args);
567
568 /* Now, we restore ARGS to its full dimensions. */
569 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
570
571 return new_args;
572 }
573
574 /* Return the N levels of innermost template arguments from the ARGS. */
575
576 tree
577 get_innermost_template_args (tree args, int n)
578 {
579 tree new_args;
580 int extra_levels;
581 int i;
582
583 gcc_assert (n >= 0);
584
585 /* If N is 1, just return the innermost set of template arguments. */
586 if (n == 1)
587 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
588
589 /* If we're not removing anything, just return the arguments we were
590 given. */
591 extra_levels = TMPL_ARGS_DEPTH (args) - n;
592 gcc_assert (extra_levels >= 0);
593 if (extra_levels == 0)
594 return args;
595
596 /* Make a new set of arguments, not containing the outer arguments. */
597 new_args = make_tree_vec (n);
598 for (i = 1; i <= n; ++i)
599 SET_TMPL_ARGS_LEVEL (new_args, i,
600 TMPL_ARGS_LEVEL (args, i + extra_levels));
601
602 return new_args;
603 }
604
605 /* The inverse of get_innermost_template_args: Return all but the innermost
606 EXTRA_LEVELS levels of template arguments from the ARGS. */
607
608 static tree
609 strip_innermost_template_args (tree args, int extra_levels)
610 {
611 tree new_args;
612 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
613 int i;
614
615 gcc_assert (n >= 0);
616
617 /* If N is 1, just return the outermost set of template arguments. */
618 if (n == 1)
619 return TMPL_ARGS_LEVEL (args, 1);
620
621 /* If we're not removing anything, just return the arguments we were
622 given. */
623 gcc_assert (extra_levels >= 0);
624 if (extra_levels == 0)
625 return args;
626
627 /* Make a new set of arguments, not containing the inner arguments. */
628 new_args = make_tree_vec (n);
629 for (i = 1; i <= n; ++i)
630 SET_TMPL_ARGS_LEVEL (new_args, i,
631 TMPL_ARGS_LEVEL (args, i));
632
633 return new_args;
634 }
635
636 /* We've got a template header coming up; push to a new level for storing
637 the parms. */
638
639 void
640 begin_template_parm_list (void)
641 {
642 /* We use a non-tag-transparent scope here, which causes pushtag to
643 put tags in this scope, rather than in the enclosing class or
644 namespace scope. This is the right thing, since we want
645 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
646 global template class, push_template_decl handles putting the
647 TEMPLATE_DECL into top-level scope. For a nested template class,
648 e.g.:
649
650 template <class T> struct S1 {
651 template <class T> struct S2 {};
652 };
653
654 pushtag contains special code to call pushdecl_with_scope on the
655 TEMPLATE_DECL for S2. */
656 begin_scope (sk_template_parms, NULL);
657 ++processing_template_decl;
658 ++processing_template_parmlist;
659 note_template_header (0);
660
661 /* Add a dummy parameter level while we process the parameter list. */
662 current_template_parms
663 = tree_cons (size_int (processing_template_decl),
664 make_tree_vec (0),
665 current_template_parms);
666 }
667
668 /* This routine is called when a specialization is declared. If it is
669 invalid to declare a specialization here, an error is reported and
670 false is returned, otherwise this routine will return true. */
671
672 static bool
673 check_specialization_scope (void)
674 {
675 tree scope = current_scope ();
676
677 /* [temp.expl.spec]
678
679 An explicit specialization shall be declared in the namespace of
680 which the template is a member, or, for member templates, in the
681 namespace of which the enclosing class or enclosing class
682 template is a member. An explicit specialization of a member
683 function, member class or static data member of a class template
684 shall be declared in the namespace of which the class template
685 is a member. */
686 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
687 {
688 error ("explicit specialization in non-namespace scope %qD", scope);
689 return false;
690 }
691
692 /* [temp.expl.spec]
693
694 In an explicit specialization declaration for a member of a class
695 template or a member template that appears in namespace scope,
696 the member template and some of its enclosing class templates may
697 remain unspecialized, except that the declaration shall not
698 explicitly specialize a class member template if its enclosing
699 class templates are not explicitly specialized as well. */
700 if (current_template_parms)
701 {
702 error ("enclosing class templates are not explicitly specialized");
703 return false;
704 }
705
706 return true;
707 }
708
709 /* We've just seen template <>. */
710
711 bool
712 begin_specialization (void)
713 {
714 begin_scope (sk_template_spec, NULL);
715 note_template_header (1);
716 return check_specialization_scope ();
717 }
718
719 /* Called at then end of processing a declaration preceded by
720 template<>. */
721
722 void
723 end_specialization (void)
724 {
725 finish_scope ();
726 reset_specialization ();
727 }
728
729 /* Any template <>'s that we have seen thus far are not referring to a
730 function specialization. */
731
732 void
733 reset_specialization (void)
734 {
735 processing_specialization = 0;
736 template_header_count = 0;
737 }
738
739 /* We've just seen a template header. If SPECIALIZATION is nonzero,
740 it was of the form template <>. */
741
742 static void
743 note_template_header (int specialization)
744 {
745 processing_specialization = specialization;
746 template_header_count++;
747 }
748
749 /* We're beginning an explicit instantiation. */
750
751 void
752 begin_explicit_instantiation (void)
753 {
754 gcc_assert (!processing_explicit_instantiation);
755 processing_explicit_instantiation = true;
756 }
757
758
759 void
760 end_explicit_instantiation (void)
761 {
762 gcc_assert (processing_explicit_instantiation);
763 processing_explicit_instantiation = false;
764 }
765
766 /* An explicit specialization or partial specialization of TMPL is being
767 declared. Check that the namespace in which the specialization is
768 occurring is permissible. Returns false iff it is invalid to
769 specialize TMPL in the current namespace. */
770
771 static bool
772 check_specialization_namespace (tree tmpl)
773 {
774 tree tpl_ns = decl_namespace_context (tmpl);
775
776 /* [tmpl.expl.spec]
777
778 An explicit specialization shall be declared in the namespace of
779 which the template is a member, or, for member templates, in the
780 namespace of which the enclosing class or enclosing class
781 template is a member. An explicit specialization of a member
782 function, member class or static data member of a class template
783 shall be declared in the namespace of which the class template is
784 a member. */
785 if (current_scope() != DECL_CONTEXT (tmpl)
786 && !at_namespace_scope_p ())
787 {
788 error ("specialization of %qD must appear at namespace scope", tmpl);
789 return false;
790 }
791 if (is_associated_namespace (current_namespace, tpl_ns))
792 /* Same or super-using namespace. */
793 return true;
794 else
795 {
796 permerror (input_location,
797 "specialization of %qD in different namespace", tmpl);
798 permerror (DECL_SOURCE_LOCATION (tmpl),
799 " from definition of %q#D", tmpl);
800 return false;
801 }
802 }
803
804 /* SPEC is an explicit instantiation. Check that it is valid to
805 perform this explicit instantiation in the current namespace. */
806
807 static void
808 check_explicit_instantiation_namespace (tree spec)
809 {
810 tree ns;
811
812 /* DR 275: An explicit instantiation shall appear in an enclosing
813 namespace of its template. */
814 ns = decl_namespace_context (spec);
815 if (!is_ancestor (current_namespace, ns))
816 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
817 "(which does not enclose namespace %qD)",
818 spec, current_namespace, ns);
819 }
820
821 /* The TYPE is being declared. If it is a template type, that means it
822 is a partial specialization. Do appropriate error-checking. */
823
824 tree
825 maybe_process_partial_specialization (tree type)
826 {
827 tree context;
828
829 if (type == error_mark_node)
830 return error_mark_node;
831
832 /* A lambda that appears in specialization context is not itself a
833 specialization. */
834 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
835 return type;
836
837 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
838 {
839 error ("name of class shadows template template parameter %qD",
840 TYPE_NAME (type));
841 return error_mark_node;
842 }
843
844 context = TYPE_CONTEXT (type);
845
846 if (TYPE_ALIAS_P (type))
847 {
848 if (TYPE_TEMPLATE_INFO (type)
849 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
850 error ("specialization of alias template %qD",
851 TYPE_TI_TEMPLATE (type));
852 else
853 error ("explicit specialization of non-template %qT", type);
854 return error_mark_node;
855 }
856 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
857 {
858 /* This is for ordinary explicit specialization and partial
859 specialization of a template class such as:
860
861 template <> class C<int>;
862
863 or:
864
865 template <class T> class C<T*>;
866
867 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
868
869 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
870 && !COMPLETE_TYPE_P (type))
871 {
872 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
873 && !at_namespace_scope_p ())
874 return error_mark_node;
875 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
876 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
877 if (processing_template_decl)
878 {
879 if (push_template_decl (TYPE_MAIN_DECL (type))
880 == error_mark_node)
881 return error_mark_node;
882 }
883 }
884 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
885 error ("specialization of %qT after instantiation", type);
886 else if (errorcount && !processing_specialization
887 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
888 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
889 /* Trying to define a specialization either without a template<> header
890 or in an inappropriate place. We've already given an error, so just
891 bail now so we don't actually define the specialization. */
892 return error_mark_node;
893 }
894 else if (CLASS_TYPE_P (type)
895 && !CLASSTYPE_USE_TEMPLATE (type)
896 && CLASSTYPE_TEMPLATE_INFO (type)
897 && context && CLASS_TYPE_P (context)
898 && CLASSTYPE_TEMPLATE_INFO (context))
899 {
900 /* This is for an explicit specialization of member class
901 template according to [temp.expl.spec/18]:
902
903 template <> template <class U> class C<int>::D;
904
905 The context `C<int>' must be an implicit instantiation.
906 Otherwise this is just a member class template declared
907 earlier like:
908
909 template <> class C<int> { template <class U> class D; };
910 template <> template <class U> class C<int>::D;
911
912 In the first case, `C<int>::D' is a specialization of `C<T>::D'
913 while in the second case, `C<int>::D' is a primary template
914 and `C<T>::D' may not exist. */
915
916 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
917 && !COMPLETE_TYPE_P (type))
918 {
919 tree t;
920 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
921
922 if (current_namespace
923 != decl_namespace_context (tmpl))
924 {
925 permerror (input_location,
926 "specializing %q#T in different namespace", type);
927 permerror (DECL_SOURCE_LOCATION (tmpl),
928 " from definition of %q#D", tmpl);
929 }
930
931 /* Check for invalid specialization after instantiation:
932
933 template <> template <> class C<int>::D<int>;
934 template <> template <class U> class C<int>::D; */
935
936 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
937 t; t = TREE_CHAIN (t))
938 {
939 tree inst = TREE_VALUE (t);
940 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
941 || !COMPLETE_OR_OPEN_TYPE_P (inst))
942 {
943 /* We already have a full specialization of this partial
944 instantiation, or a full specialization has been
945 looked up but not instantiated. Reassign it to the
946 new member specialization template. */
947 spec_entry elt;
948 spec_entry *entry;
949
950 elt.tmpl = most_general_template (tmpl);
951 elt.args = CLASSTYPE_TI_ARGS (inst);
952 elt.spec = inst;
953
954 type_specializations->remove_elt (&elt);
955
956 elt.tmpl = tmpl;
957 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
958
959 spec_entry **slot
960 = type_specializations->find_slot (&elt, INSERT);
961 entry = ggc_alloc<spec_entry> ();
962 *entry = elt;
963 *slot = entry;
964 }
965 else
966 /* But if we've had an implicit instantiation, that's a
967 problem ([temp.expl.spec]/6). */
968 error ("specialization %qT after instantiation %qT",
969 type, inst);
970 }
971
972 /* Mark TYPE as a specialization. And as a result, we only
973 have one level of template argument for the innermost
974 class template. */
975 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
976 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
977 CLASSTYPE_TI_ARGS (type)
978 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
979 }
980 }
981 else if (processing_specialization)
982 {
983 /* Someday C++0x may allow for enum template specialization. */
984 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
985 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
986 pedwarn (input_location, OPT_Wpedantic, "template specialization "
987 "of %qD not allowed by ISO C++", type);
988 else
989 {
990 error ("explicit specialization of non-template %qT", type);
991 return error_mark_node;
992 }
993 }
994
995 return type;
996 }
997
998 /* Returns nonzero if we can optimize the retrieval of specializations
999 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1000 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1001
1002 static inline bool
1003 optimize_specialization_lookup_p (tree tmpl)
1004 {
1005 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1006 && DECL_CLASS_SCOPE_P (tmpl)
1007 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1008 parameter. */
1009 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1010 /* The optimized lookup depends on the fact that the
1011 template arguments for the member function template apply
1012 purely to the containing class, which is not true if the
1013 containing class is an explicit or partial
1014 specialization. */
1015 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1016 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1017 && !DECL_CONV_FN_P (tmpl)
1018 /* It is possible to have a template that is not a member
1019 template and is not a member of a template class:
1020
1021 template <typename T>
1022 struct S { friend A::f(); };
1023
1024 Here, the friend function is a template, but the context does
1025 not have template information. The optimized lookup relies
1026 on having ARGS be the template arguments for both the class
1027 and the function template. */
1028 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1029 }
1030
1031 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1032 gone through coerce_template_parms by now. */
1033
1034 static void
1035 check_unstripped_args (tree args ATTRIBUTE_UNUSED)
1036 {
1037 #ifdef ENABLE_CHECKING
1038 ++processing_template_decl;
1039 if (!any_dependent_template_arguments_p (args))
1040 {
1041 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1042 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1043 {
1044 tree arg = TREE_VEC_ELT (inner, i);
1045 if (TREE_CODE (arg) == TEMPLATE_DECL)
1046 /* OK */;
1047 else if (TYPE_P (arg))
1048 gcc_assert (strip_typedefs (arg, NULL) == arg);
1049 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1050 /* Allow typedefs on the type of a non-type argument, since a
1051 parameter can have them. */;
1052 else
1053 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1054 }
1055 }
1056 --processing_template_decl;
1057 #endif
1058 }
1059
1060 /* Retrieve the specialization (in the sense of [temp.spec] - a
1061 specialization is either an instantiation or an explicit
1062 specialization) of TMPL for the given template ARGS. If there is
1063 no such specialization, return NULL_TREE. The ARGS are a vector of
1064 arguments, or a vector of vectors of arguments, in the case of
1065 templates with more than one level of parameters.
1066
1067 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1068 then we search for a partial specialization matching ARGS. This
1069 parameter is ignored if TMPL is not a class template.
1070
1071 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1072 result is a NONTYPE_ARGUMENT_PACK. */
1073
1074 static tree
1075 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1076 {
1077 if (tmpl == NULL_TREE)
1078 return NULL_TREE;
1079
1080 if (args == error_mark_node)
1081 return NULL_TREE;
1082
1083 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1084 || TREE_CODE (tmpl) == FIELD_DECL);
1085
1086 /* There should be as many levels of arguments as there are
1087 levels of parameters. */
1088 gcc_assert (TMPL_ARGS_DEPTH (args)
1089 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1090 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1091 : template_class_depth (DECL_CONTEXT (tmpl))));
1092
1093 check_unstripped_args (args);
1094
1095 if (optimize_specialization_lookup_p (tmpl))
1096 {
1097 tree class_template;
1098 tree class_specialization;
1099 vec<tree, va_gc> *methods;
1100 tree fns;
1101 int idx;
1102
1103 /* The template arguments actually apply to the containing
1104 class. Find the class specialization with those
1105 arguments. */
1106 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1107 class_specialization
1108 = retrieve_specialization (class_template, args, 0);
1109 if (!class_specialization)
1110 return NULL_TREE;
1111 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1112 for the specialization. */
1113 idx = class_method_index_for_fn (class_specialization, tmpl);
1114 if (idx == -1)
1115 return NULL_TREE;
1116 /* Iterate through the methods with the indicated name, looking
1117 for the one that has an instance of TMPL. */
1118 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1119 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1120 {
1121 tree fn = OVL_CURRENT (fns);
1122 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1123 /* using-declarations can add base methods to the method vec,
1124 and we don't want those here. */
1125 && DECL_CONTEXT (fn) == class_specialization)
1126 return fn;
1127 }
1128 return NULL_TREE;
1129 }
1130 else
1131 {
1132 spec_entry *found;
1133 spec_entry elt;
1134 hash_table<spec_hasher> *specializations;
1135
1136 elt.tmpl = tmpl;
1137 elt.args = args;
1138 elt.spec = NULL_TREE;
1139
1140 if (DECL_CLASS_TEMPLATE_P (tmpl))
1141 specializations = type_specializations;
1142 else
1143 specializations = decl_specializations;
1144
1145 if (hash == 0)
1146 hash = spec_hasher::hash (&elt);
1147 found = specializations->find_with_hash (&elt, hash);
1148 if (found)
1149 return found->spec;
1150 }
1151
1152 return NULL_TREE;
1153 }
1154
1155 /* Like retrieve_specialization, but for local declarations. */
1156
1157 static tree
1158 retrieve_local_specialization (tree tmpl)
1159 {
1160 if (local_specializations == NULL)
1161 return NULL_TREE;
1162
1163 tree *slot = local_specializations->get (tmpl);
1164 return slot ? *slot : NULL_TREE;
1165 }
1166
1167 /* Returns nonzero iff DECL is a specialization of TMPL. */
1168
1169 int
1170 is_specialization_of (tree decl, tree tmpl)
1171 {
1172 tree t;
1173
1174 if (TREE_CODE (decl) == FUNCTION_DECL)
1175 {
1176 for (t = decl;
1177 t != NULL_TREE;
1178 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1179 if (t == tmpl)
1180 return 1;
1181 }
1182 else
1183 {
1184 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1185
1186 for (t = TREE_TYPE (decl);
1187 t != NULL_TREE;
1188 t = CLASSTYPE_USE_TEMPLATE (t)
1189 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1190 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1191 return 1;
1192 }
1193
1194 return 0;
1195 }
1196
1197 /* Returns nonzero iff DECL is a specialization of friend declaration
1198 FRIEND_DECL according to [temp.friend]. */
1199
1200 bool
1201 is_specialization_of_friend (tree decl, tree friend_decl)
1202 {
1203 bool need_template = true;
1204 int template_depth;
1205
1206 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1207 || TREE_CODE (decl) == TYPE_DECL);
1208
1209 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1210 of a template class, we want to check if DECL is a specialization
1211 if this. */
1212 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1213 && DECL_TEMPLATE_INFO (friend_decl)
1214 && !DECL_USE_TEMPLATE (friend_decl))
1215 {
1216 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1217 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1218 need_template = false;
1219 }
1220 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1221 && !PRIMARY_TEMPLATE_P (friend_decl))
1222 need_template = false;
1223
1224 /* There is nothing to do if this is not a template friend. */
1225 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1226 return false;
1227
1228 if (is_specialization_of (decl, friend_decl))
1229 return true;
1230
1231 /* [temp.friend/6]
1232 A member of a class template may be declared to be a friend of a
1233 non-template class. In this case, the corresponding member of
1234 every specialization of the class template is a friend of the
1235 class granting friendship.
1236
1237 For example, given a template friend declaration
1238
1239 template <class T> friend void A<T>::f();
1240
1241 the member function below is considered a friend
1242
1243 template <> struct A<int> {
1244 void f();
1245 };
1246
1247 For this type of template friend, TEMPLATE_DEPTH below will be
1248 nonzero. To determine if DECL is a friend of FRIEND, we first
1249 check if the enclosing class is a specialization of another. */
1250
1251 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1252 if (template_depth
1253 && DECL_CLASS_SCOPE_P (decl)
1254 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1255 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1256 {
1257 /* Next, we check the members themselves. In order to handle
1258 a few tricky cases, such as when FRIEND_DECL's are
1259
1260 template <class T> friend void A<T>::g(T t);
1261 template <class T> template <T t> friend void A<T>::h();
1262
1263 and DECL's are
1264
1265 void A<int>::g(int);
1266 template <int> void A<int>::h();
1267
1268 we need to figure out ARGS, the template arguments from
1269 the context of DECL. This is required for template substitution
1270 of `T' in the function parameter of `g' and template parameter
1271 of `h' in the above examples. Here ARGS corresponds to `int'. */
1272
1273 tree context = DECL_CONTEXT (decl);
1274 tree args = NULL_TREE;
1275 int current_depth = 0;
1276
1277 while (current_depth < template_depth)
1278 {
1279 if (CLASSTYPE_TEMPLATE_INFO (context))
1280 {
1281 if (current_depth == 0)
1282 args = TYPE_TI_ARGS (context);
1283 else
1284 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1285 current_depth++;
1286 }
1287 context = TYPE_CONTEXT (context);
1288 }
1289
1290 if (TREE_CODE (decl) == FUNCTION_DECL)
1291 {
1292 bool is_template;
1293 tree friend_type;
1294 tree decl_type;
1295 tree friend_args_type;
1296 tree decl_args_type;
1297
1298 /* Make sure that both DECL and FRIEND_DECL are templates or
1299 non-templates. */
1300 is_template = DECL_TEMPLATE_INFO (decl)
1301 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1302 if (need_template ^ is_template)
1303 return false;
1304 else if (is_template)
1305 {
1306 /* If both are templates, check template parameter list. */
1307 tree friend_parms
1308 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1309 args, tf_none);
1310 if (!comp_template_parms
1311 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1312 friend_parms))
1313 return false;
1314
1315 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1316 }
1317 else
1318 decl_type = TREE_TYPE (decl);
1319
1320 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1321 tf_none, NULL_TREE);
1322 if (friend_type == error_mark_node)
1323 return false;
1324
1325 /* Check if return types match. */
1326 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1327 return false;
1328
1329 /* Check if function parameter types match, ignoring the
1330 `this' parameter. */
1331 friend_args_type = TYPE_ARG_TYPES (friend_type);
1332 decl_args_type = TYPE_ARG_TYPES (decl_type);
1333 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1334 friend_args_type = TREE_CHAIN (friend_args_type);
1335 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1336 decl_args_type = TREE_CHAIN (decl_args_type);
1337
1338 return compparms (decl_args_type, friend_args_type);
1339 }
1340 else
1341 {
1342 /* DECL is a TYPE_DECL */
1343 bool is_template;
1344 tree decl_type = TREE_TYPE (decl);
1345
1346 /* Make sure that both DECL and FRIEND_DECL are templates or
1347 non-templates. */
1348 is_template
1349 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1350 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1351
1352 if (need_template ^ is_template)
1353 return false;
1354 else if (is_template)
1355 {
1356 tree friend_parms;
1357 /* If both are templates, check the name of the two
1358 TEMPLATE_DECL's first because is_friend didn't. */
1359 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1360 != DECL_NAME (friend_decl))
1361 return false;
1362
1363 /* Now check template parameter list. */
1364 friend_parms
1365 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1366 args, tf_none);
1367 return comp_template_parms
1368 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1369 friend_parms);
1370 }
1371 else
1372 return (DECL_NAME (decl)
1373 == DECL_NAME (friend_decl));
1374 }
1375 }
1376 return false;
1377 }
1378
1379 /* Register the specialization SPEC as a specialization of TMPL with
1380 the indicated ARGS. IS_FRIEND indicates whether the specialization
1381 is actually just a friend declaration. Returns SPEC, or an
1382 equivalent prior declaration, if available.
1383
1384 We also store instantiations of field packs in the hash table, even
1385 though they are not themselves templates, to make lookup easier. */
1386
1387 static tree
1388 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1389 hashval_t hash)
1390 {
1391 tree fn;
1392 spec_entry **slot = NULL;
1393 spec_entry elt;
1394
1395 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1396 || (TREE_CODE (tmpl) == FIELD_DECL
1397 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1398
1399 if (TREE_CODE (spec) == FUNCTION_DECL
1400 && uses_template_parms (DECL_TI_ARGS (spec)))
1401 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1402 register it; we want the corresponding TEMPLATE_DECL instead.
1403 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1404 the more obvious `uses_template_parms (spec)' to avoid problems
1405 with default function arguments. In particular, given
1406 something like this:
1407
1408 template <class T> void f(T t1, T t = T())
1409
1410 the default argument expression is not substituted for in an
1411 instantiation unless and until it is actually needed. */
1412 return spec;
1413
1414 if (optimize_specialization_lookup_p (tmpl))
1415 /* We don't put these specializations in the hash table, but we might
1416 want to give an error about a mismatch. */
1417 fn = retrieve_specialization (tmpl, args, 0);
1418 else
1419 {
1420 elt.tmpl = tmpl;
1421 elt.args = args;
1422 elt.spec = spec;
1423
1424 if (hash == 0)
1425 hash = spec_hasher::hash (&elt);
1426
1427 slot =
1428 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1429 if (*slot)
1430 fn = ((spec_entry *) *slot)->spec;
1431 else
1432 fn = NULL_TREE;
1433 }
1434
1435 /* We can sometimes try to re-register a specialization that we've
1436 already got. In particular, regenerate_decl_from_template calls
1437 duplicate_decls which will update the specialization list. But,
1438 we'll still get called again here anyhow. It's more convenient
1439 to simply allow this than to try to prevent it. */
1440 if (fn == spec)
1441 return spec;
1442 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1443 {
1444 if (DECL_TEMPLATE_INSTANTIATION (fn))
1445 {
1446 if (DECL_ODR_USED (fn)
1447 || DECL_EXPLICIT_INSTANTIATION (fn))
1448 {
1449 error ("specialization of %qD after instantiation",
1450 fn);
1451 return error_mark_node;
1452 }
1453 else
1454 {
1455 tree clone;
1456 /* This situation should occur only if the first
1457 specialization is an implicit instantiation, the
1458 second is an explicit specialization, and the
1459 implicit instantiation has not yet been used. That
1460 situation can occur if we have implicitly
1461 instantiated a member function and then specialized
1462 it later.
1463
1464 We can also wind up here if a friend declaration that
1465 looked like an instantiation turns out to be a
1466 specialization:
1467
1468 template <class T> void foo(T);
1469 class S { friend void foo<>(int) };
1470 template <> void foo(int);
1471
1472 We transform the existing DECL in place so that any
1473 pointers to it become pointers to the updated
1474 declaration.
1475
1476 If there was a definition for the template, but not
1477 for the specialization, we want this to look as if
1478 there were no definition, and vice versa. */
1479 DECL_INITIAL (fn) = NULL_TREE;
1480 duplicate_decls (spec, fn, is_friend);
1481 /* The call to duplicate_decls will have applied
1482 [temp.expl.spec]:
1483
1484 An explicit specialization of a function template
1485 is inline only if it is explicitly declared to be,
1486 and independently of whether its function template
1487 is.
1488
1489 to the primary function; now copy the inline bits to
1490 the various clones. */
1491 FOR_EACH_CLONE (clone, fn)
1492 {
1493 DECL_DECLARED_INLINE_P (clone)
1494 = DECL_DECLARED_INLINE_P (fn);
1495 DECL_SOURCE_LOCATION (clone)
1496 = DECL_SOURCE_LOCATION (fn);
1497 DECL_DELETED_FN (clone)
1498 = DECL_DELETED_FN (fn);
1499 }
1500 check_specialization_namespace (tmpl);
1501
1502 return fn;
1503 }
1504 }
1505 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1506 {
1507 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1508 /* Dup decl failed, but this is a new definition. Set the
1509 line number so any errors match this new
1510 definition. */
1511 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1512
1513 return fn;
1514 }
1515 }
1516 else if (fn)
1517 return duplicate_decls (spec, fn, is_friend);
1518
1519 /* A specialization must be declared in the same namespace as the
1520 template it is specializing. */
1521 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1522 && !check_specialization_namespace (tmpl))
1523 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1524
1525 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1526 {
1527 spec_entry *entry = ggc_alloc<spec_entry> ();
1528 gcc_assert (tmpl && args && spec);
1529 *entry = elt;
1530 *slot = entry;
1531 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1532 && PRIMARY_TEMPLATE_P (tmpl)
1533 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1534 || variable_template_p (tmpl))
1535 /* If TMPL is a forward declaration of a template function, keep a list
1536 of all specializations in case we need to reassign them to a friend
1537 template later in tsubst_friend_function.
1538
1539 Also keep a list of all variable template instantiations so that
1540 process_partial_specialization can check whether a later partial
1541 specialization would have used it. */
1542 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1543 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1544 }
1545
1546 return spec;
1547 }
1548
1549 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1550 TMPL and ARGS members, ignores SPEC. */
1551
1552 int comparing_specializations;
1553
1554 bool
1555 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1556 {
1557 int equal;
1558
1559 ++comparing_specializations;
1560 equal = (e1->tmpl == e2->tmpl
1561 && comp_template_args (e1->args, e2->args));
1562 --comparing_specializations;
1563
1564 return equal;
1565 }
1566
1567 /* Returns a hash for a template TMPL and template arguments ARGS. */
1568
1569 static hashval_t
1570 hash_tmpl_and_args (tree tmpl, tree args)
1571 {
1572 hashval_t val = DECL_UID (tmpl);
1573 return iterative_hash_template_arg (args, val);
1574 }
1575
1576 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1577 ignoring SPEC. */
1578
1579 hashval_t
1580 spec_hasher::hash (spec_entry *e)
1581 {
1582 return hash_tmpl_and_args (e->tmpl, e->args);
1583 }
1584
1585 /* Recursively calculate a hash value for a template argument ARG, for use
1586 in the hash tables of template specializations. */
1587
1588 hashval_t
1589 iterative_hash_template_arg (tree arg, hashval_t val)
1590 {
1591 unsigned HOST_WIDE_INT i;
1592 enum tree_code code;
1593 char tclass;
1594
1595 if (arg == NULL_TREE)
1596 return iterative_hash_object (arg, val);
1597
1598 if (!TYPE_P (arg))
1599 STRIP_NOPS (arg);
1600
1601 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1602 /* We can get one of these when re-hashing a previous entry in the middle
1603 of substituting into a pack expansion. Just look through it. */
1604 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1605
1606 code = TREE_CODE (arg);
1607 tclass = TREE_CODE_CLASS (code);
1608
1609 val = iterative_hash_object (code, val);
1610
1611 switch (code)
1612 {
1613 case ERROR_MARK:
1614 return val;
1615
1616 case IDENTIFIER_NODE:
1617 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1618
1619 case TREE_VEC:
1620 {
1621 int i, len = TREE_VEC_LENGTH (arg);
1622 for (i = 0; i < len; ++i)
1623 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1624 return val;
1625 }
1626
1627 case TYPE_PACK_EXPANSION:
1628 case EXPR_PACK_EXPANSION:
1629 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1630 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1631
1632 case TYPE_ARGUMENT_PACK:
1633 case NONTYPE_ARGUMENT_PACK:
1634 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1635
1636 case TREE_LIST:
1637 for (; arg; arg = TREE_CHAIN (arg))
1638 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1639 return val;
1640
1641 case OVERLOAD:
1642 for (; arg; arg = OVL_NEXT (arg))
1643 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1644 return val;
1645
1646 case CONSTRUCTOR:
1647 {
1648 tree field, value;
1649 iterative_hash_template_arg (TREE_TYPE (arg), val);
1650 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1651 {
1652 val = iterative_hash_template_arg (field, val);
1653 val = iterative_hash_template_arg (value, val);
1654 }
1655 return val;
1656 }
1657
1658 case PARM_DECL:
1659 if (!DECL_ARTIFICIAL (arg))
1660 {
1661 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1662 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1663 }
1664 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1665
1666 case TARGET_EXPR:
1667 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1668
1669 case PTRMEM_CST:
1670 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1671 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1672
1673 case TEMPLATE_PARM_INDEX:
1674 val = iterative_hash_template_arg
1675 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1676 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1677 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1678
1679 case TRAIT_EXPR:
1680 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1681 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1682 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1683
1684 case BASELINK:
1685 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1686 val);
1687 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1688 val);
1689
1690 case MODOP_EXPR:
1691 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1692 code = TREE_CODE (TREE_OPERAND (arg, 1));
1693 val = iterative_hash_object (code, val);
1694 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1695
1696 case LAMBDA_EXPR:
1697 /* A lambda can't appear in a template arg, but don't crash on
1698 erroneous input. */
1699 gcc_assert (seen_error ());
1700 return val;
1701
1702 case CAST_EXPR:
1703 case IMPLICIT_CONV_EXPR:
1704 case STATIC_CAST_EXPR:
1705 case REINTERPRET_CAST_EXPR:
1706 case CONST_CAST_EXPR:
1707 case DYNAMIC_CAST_EXPR:
1708 case NEW_EXPR:
1709 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1710 /* Now hash operands as usual. */
1711 break;
1712
1713 default:
1714 break;
1715 }
1716
1717 switch (tclass)
1718 {
1719 case tcc_type:
1720 if (alias_template_specialization_p (arg))
1721 {
1722 // We want an alias specialization that survived strip_typedefs
1723 // to hash differently from its TYPE_CANONICAL, to avoid hash
1724 // collisions that compare as different in template_args_equal.
1725 // These could be dependent specializations that strip_typedefs
1726 // left alone, or untouched specializations because
1727 // coerce_template_parms returns the unconverted template
1728 // arguments if it sees incomplete argument packs.
1729 tree ti = TYPE_TEMPLATE_INFO (arg);
1730 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1731 }
1732 if (TYPE_CANONICAL (arg))
1733 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1734 val);
1735 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1736 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1737 /* Otherwise just compare the types during lookup. */
1738 return val;
1739
1740 case tcc_declaration:
1741 case tcc_constant:
1742 return iterative_hash_expr (arg, val);
1743
1744 default:
1745 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1746 {
1747 unsigned n = cp_tree_operand_length (arg);
1748 for (i = 0; i < n; ++i)
1749 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1750 return val;
1751 }
1752 }
1753 gcc_unreachable ();
1754 return 0;
1755 }
1756
1757 /* Unregister the specialization SPEC as a specialization of TMPL.
1758 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1759 if the SPEC was listed as a specialization of TMPL.
1760
1761 Note that SPEC has been ggc_freed, so we can't look inside it. */
1762
1763 bool
1764 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1765 {
1766 spec_entry *entry;
1767 spec_entry elt;
1768
1769 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1770 elt.args = TI_ARGS (tinfo);
1771 elt.spec = NULL_TREE;
1772
1773 entry = decl_specializations->find (&elt);
1774 if (entry != NULL)
1775 {
1776 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1777 gcc_assert (new_spec != NULL_TREE);
1778 entry->spec = new_spec;
1779 return 1;
1780 }
1781
1782 return 0;
1783 }
1784
1785 /* Like register_specialization, but for local declarations. We are
1786 registering SPEC, an instantiation of TMPL. */
1787
1788 static void
1789 register_local_specialization (tree spec, tree tmpl)
1790 {
1791 local_specializations->put (tmpl, spec);
1792 }
1793
1794 /* TYPE is a class type. Returns true if TYPE is an explicitly
1795 specialized class. */
1796
1797 bool
1798 explicit_class_specialization_p (tree type)
1799 {
1800 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1801 return false;
1802 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1803 }
1804
1805 /* Print the list of functions at FNS, going through all the overloads
1806 for each element of the list. Alternatively, FNS can not be a
1807 TREE_LIST, in which case it will be printed together with all the
1808 overloads.
1809
1810 MORE and *STR should respectively be FALSE and NULL when the function
1811 is called from the outside. They are used internally on recursive
1812 calls. print_candidates manages the two parameters and leaves NULL
1813 in *STR when it ends. */
1814
1815 static void
1816 print_candidates_1 (tree fns, bool more, const char **str)
1817 {
1818 tree fn, fn2;
1819 char *spaces = NULL;
1820
1821 for (fn = fns; fn; fn = OVL_NEXT (fn))
1822 if (TREE_CODE (fn) == TREE_LIST)
1823 {
1824 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1825 print_candidates_1 (TREE_VALUE (fn2),
1826 TREE_CHAIN (fn2) || more, str);
1827 }
1828 else
1829 {
1830 tree cand = OVL_CURRENT (fn);
1831 if (!*str)
1832 {
1833 /* Pick the prefix string. */
1834 if (!more && !OVL_NEXT (fns))
1835 {
1836 inform (DECL_SOURCE_LOCATION (cand),
1837 "candidate is: %#D", cand);
1838 continue;
1839 }
1840
1841 *str = _("candidates are:");
1842 spaces = get_spaces (*str);
1843 }
1844 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1845 *str = spaces ? spaces : *str;
1846 }
1847
1848 if (!more)
1849 {
1850 free (spaces);
1851 *str = NULL;
1852 }
1853 }
1854
1855 /* Print the list of candidate FNS in an error message. FNS can also
1856 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1857
1858 void
1859 print_candidates (tree fns)
1860 {
1861 const char *str = NULL;
1862 print_candidates_1 (fns, false, &str);
1863 gcc_assert (str == NULL);
1864 }
1865
1866 /* Returns the template (one of the functions given by TEMPLATE_ID)
1867 which can be specialized to match the indicated DECL with the
1868 explicit template args given in TEMPLATE_ID. The DECL may be
1869 NULL_TREE if none is available. In that case, the functions in
1870 TEMPLATE_ID are non-members.
1871
1872 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1873 specialization of a member template.
1874
1875 The TEMPLATE_COUNT is the number of references to qualifying
1876 template classes that appeared in the name of the function. See
1877 check_explicit_specialization for a more accurate description.
1878
1879 TSK indicates what kind of template declaration (if any) is being
1880 declared. TSK_TEMPLATE indicates that the declaration given by
1881 DECL, though a FUNCTION_DECL, has template parameters, and is
1882 therefore a template function.
1883
1884 The template args (those explicitly specified and those deduced)
1885 are output in a newly created vector *TARGS_OUT.
1886
1887 If it is impossible to determine the result, an error message is
1888 issued. The error_mark_node is returned to indicate failure. */
1889
1890 static tree
1891 determine_specialization (tree template_id,
1892 tree decl,
1893 tree* targs_out,
1894 int need_member_template,
1895 int template_count,
1896 tmpl_spec_kind tsk)
1897 {
1898 tree fns;
1899 tree targs;
1900 tree explicit_targs;
1901 tree candidates = NULL_TREE;
1902 /* A TREE_LIST of templates of which DECL may be a specialization.
1903 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1904 corresponding TREE_PURPOSE is the set of template arguments that,
1905 when used to instantiate the template, would produce a function
1906 with the signature of DECL. */
1907 tree templates = NULL_TREE;
1908 int header_count;
1909 cp_binding_level *b;
1910
1911 *targs_out = NULL_TREE;
1912
1913 if (template_id == error_mark_node || decl == error_mark_node)
1914 return error_mark_node;
1915
1916 /* We shouldn't be specializing a member template of an
1917 unspecialized class template; we already gave an error in
1918 check_specialization_scope, now avoid crashing. */
1919 if (template_count && DECL_CLASS_SCOPE_P (decl)
1920 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1921 {
1922 gcc_assert (errorcount);
1923 return error_mark_node;
1924 }
1925
1926 fns = TREE_OPERAND (template_id, 0);
1927 explicit_targs = TREE_OPERAND (template_id, 1);
1928
1929 if (fns == error_mark_node)
1930 return error_mark_node;
1931
1932 /* Check for baselinks. */
1933 if (BASELINK_P (fns))
1934 fns = BASELINK_FUNCTIONS (fns);
1935
1936 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
1937 {
1938 error ("%qD is not a function template", fns);
1939 return error_mark_node;
1940 }
1941 else if (VAR_P (decl) && !variable_template_p (fns))
1942 {
1943 error ("%qD is not a variable template", fns);
1944 return error_mark_node;
1945 }
1946
1947 /* Count the number of template headers specified for this
1948 specialization. */
1949 header_count = 0;
1950 for (b = current_binding_level;
1951 b->kind == sk_template_parms;
1952 b = b->level_chain)
1953 ++header_count;
1954
1955 tree orig_fns = fns;
1956
1957 if (variable_template_p (fns))
1958 {
1959 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
1960 targs = coerce_template_parms (parms, explicit_targs, fns,
1961 tf_warning_or_error,
1962 /*req_all*/true, /*use_defarg*/true);
1963 templates = tree_cons (targs, fns, templates);
1964 }
1965 else for (; fns; fns = OVL_NEXT (fns))
1966 {
1967 tree fn = OVL_CURRENT (fns);
1968
1969 if (TREE_CODE (fn) == TEMPLATE_DECL)
1970 {
1971 tree decl_arg_types;
1972 tree fn_arg_types;
1973 tree insttype;
1974
1975 /* In case of explicit specialization, we need to check if
1976 the number of template headers appearing in the specialization
1977 is correct. This is usually done in check_explicit_specialization,
1978 but the check done there cannot be exhaustive when specializing
1979 member functions. Consider the following code:
1980
1981 template <> void A<int>::f(int);
1982 template <> template <> void A<int>::f(int);
1983
1984 Assuming that A<int> is not itself an explicit specialization
1985 already, the first line specializes "f" which is a non-template
1986 member function, whilst the second line specializes "f" which
1987 is a template member function. So both lines are syntactically
1988 correct, and check_explicit_specialization does not reject
1989 them.
1990
1991 Here, we can do better, as we are matching the specialization
1992 against the declarations. We count the number of template
1993 headers, and we check if they match TEMPLATE_COUNT + 1
1994 (TEMPLATE_COUNT is the number of qualifying template classes,
1995 plus there must be another header for the member template
1996 itself).
1997
1998 Notice that if header_count is zero, this is not a
1999 specialization but rather a template instantiation, so there
2000 is no check we can perform here. */
2001 if (header_count && header_count != template_count + 1)
2002 continue;
2003
2004 /* Check that the number of template arguments at the
2005 innermost level for DECL is the same as for FN. */
2006 if (current_binding_level->kind == sk_template_parms
2007 && !current_binding_level->explicit_spec_p
2008 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2009 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2010 (current_template_parms))))
2011 continue;
2012
2013 /* DECL might be a specialization of FN. */
2014 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2015 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2016
2017 /* For a non-static member function, we need to make sure
2018 that the const qualification is the same. Since
2019 get_bindings does not try to merge the "this" parameter,
2020 we must do the comparison explicitly. */
2021 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2022 && !same_type_p (TREE_VALUE (fn_arg_types),
2023 TREE_VALUE (decl_arg_types)))
2024 continue;
2025
2026 /* Skip the "this" parameter and, for constructors of
2027 classes with virtual bases, the VTT parameter. A
2028 full specialization of a constructor will have a VTT
2029 parameter, but a template never will. */
2030 decl_arg_types
2031 = skip_artificial_parms_for (decl, decl_arg_types);
2032 fn_arg_types
2033 = skip_artificial_parms_for (fn, fn_arg_types);
2034
2035 /* Function templates cannot be specializations; there are
2036 no partial specializations of functions. Therefore, if
2037 the type of DECL does not match FN, there is no
2038 match. */
2039 if (tsk == tsk_template)
2040 {
2041 if (compparms (fn_arg_types, decl_arg_types))
2042 candidates = tree_cons (NULL_TREE, fn, candidates);
2043 continue;
2044 }
2045
2046 /* See whether this function might be a specialization of this
2047 template. Suppress access control because we might be trying
2048 to make this specialization a friend, and we have already done
2049 access control for the declaration of the specialization. */
2050 push_deferring_access_checks (dk_no_check);
2051 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2052 pop_deferring_access_checks ();
2053
2054 if (!targs)
2055 /* We cannot deduce template arguments that when used to
2056 specialize TMPL will produce DECL. */
2057 continue;
2058
2059 /* Make sure that the deduced arguments actually work. */
2060 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
2061 if (insttype == error_mark_node)
2062 continue;
2063 fn_arg_types
2064 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2065 if (!compparms (fn_arg_types, decl_arg_types))
2066 continue;
2067
2068 /* Save this template, and the arguments deduced. */
2069 templates = tree_cons (targs, fn, templates);
2070 }
2071 else if (need_member_template)
2072 /* FN is an ordinary member function, and we need a
2073 specialization of a member template. */
2074 ;
2075 else if (TREE_CODE (fn) != FUNCTION_DECL)
2076 /* We can get IDENTIFIER_NODEs here in certain erroneous
2077 cases. */
2078 ;
2079 else if (!DECL_FUNCTION_MEMBER_P (fn))
2080 /* This is just an ordinary non-member function. Nothing can
2081 be a specialization of that. */
2082 ;
2083 else if (DECL_ARTIFICIAL (fn))
2084 /* Cannot specialize functions that are created implicitly. */
2085 ;
2086 else
2087 {
2088 tree decl_arg_types;
2089
2090 /* This is an ordinary member function. However, since
2091 we're here, we can assume its enclosing class is a
2092 template class. For example,
2093
2094 template <typename T> struct S { void f(); };
2095 template <> void S<int>::f() {}
2096
2097 Here, S<int>::f is a non-template, but S<int> is a
2098 template class. If FN has the same type as DECL, we
2099 might be in business. */
2100
2101 if (!DECL_TEMPLATE_INFO (fn))
2102 /* Its enclosing class is an explicit specialization
2103 of a template class. This is not a candidate. */
2104 continue;
2105
2106 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2107 TREE_TYPE (TREE_TYPE (fn))))
2108 /* The return types differ. */
2109 continue;
2110
2111 /* Adjust the type of DECL in case FN is a static member. */
2112 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2113 if (DECL_STATIC_FUNCTION_P (fn)
2114 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2115 decl_arg_types = TREE_CHAIN (decl_arg_types);
2116
2117 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2118 decl_arg_types))
2119 /* They match! */
2120 candidates = tree_cons (NULL_TREE, fn, candidates);
2121 }
2122 }
2123
2124 if (templates && TREE_CHAIN (templates))
2125 {
2126 /* We have:
2127
2128 [temp.expl.spec]
2129
2130 It is possible for a specialization with a given function
2131 signature to be instantiated from more than one function
2132 template. In such cases, explicit specification of the
2133 template arguments must be used to uniquely identify the
2134 function template specialization being specialized.
2135
2136 Note that here, there's no suggestion that we're supposed to
2137 determine which of the candidate templates is most
2138 specialized. However, we, also have:
2139
2140 [temp.func.order]
2141
2142 Partial ordering of overloaded function template
2143 declarations is used in the following contexts to select
2144 the function template to which a function template
2145 specialization refers:
2146
2147 -- when an explicit specialization refers to a function
2148 template.
2149
2150 So, we do use the partial ordering rules, at least for now.
2151 This extension can only serve to make invalid programs valid,
2152 so it's safe. And, there is strong anecdotal evidence that
2153 the committee intended the partial ordering rules to apply;
2154 the EDG front end has that behavior, and John Spicer claims
2155 that the committee simply forgot to delete the wording in
2156 [temp.expl.spec]. */
2157 tree tmpl = most_specialized_instantiation (templates);
2158 if (tmpl != error_mark_node)
2159 {
2160 templates = tmpl;
2161 TREE_CHAIN (templates) = NULL_TREE;
2162 }
2163 }
2164
2165 if (templates == NULL_TREE && candidates == NULL_TREE)
2166 {
2167 error ("template-id %qD for %q+D does not match any template "
2168 "declaration", template_id, decl);
2169 if (header_count && header_count != template_count + 1)
2170 inform (input_location, "saw %d %<template<>%>, need %d for "
2171 "specializing a member function template",
2172 header_count, template_count + 1);
2173 else
2174 print_candidates (orig_fns);
2175 return error_mark_node;
2176 }
2177 else if ((templates && TREE_CHAIN (templates))
2178 || (candidates && TREE_CHAIN (candidates))
2179 || (templates && candidates))
2180 {
2181 error ("ambiguous template specialization %qD for %q+D",
2182 template_id, decl);
2183 candidates = chainon (candidates, templates);
2184 print_candidates (candidates);
2185 return error_mark_node;
2186 }
2187
2188 /* We have one, and exactly one, match. */
2189 if (candidates)
2190 {
2191 tree fn = TREE_VALUE (candidates);
2192 *targs_out = copy_node (DECL_TI_ARGS (fn));
2193 /* DECL is a re-declaration or partial instantiation of a template
2194 function. */
2195 if (TREE_CODE (fn) == TEMPLATE_DECL)
2196 return fn;
2197 /* It was a specialization of an ordinary member function in a
2198 template class. */
2199 return DECL_TI_TEMPLATE (fn);
2200 }
2201
2202 /* It was a specialization of a template. */
2203 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2204 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2205 {
2206 *targs_out = copy_node (targs);
2207 SET_TMPL_ARGS_LEVEL (*targs_out,
2208 TMPL_ARGS_DEPTH (*targs_out),
2209 TREE_PURPOSE (templates));
2210 }
2211 else
2212 *targs_out = TREE_PURPOSE (templates);
2213 return TREE_VALUE (templates);
2214 }
2215
2216 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2217 but with the default argument values filled in from those in the
2218 TMPL_TYPES. */
2219
2220 static tree
2221 copy_default_args_to_explicit_spec_1 (tree spec_types,
2222 tree tmpl_types)
2223 {
2224 tree new_spec_types;
2225
2226 if (!spec_types)
2227 return NULL_TREE;
2228
2229 if (spec_types == void_list_node)
2230 return void_list_node;
2231
2232 /* Substitute into the rest of the list. */
2233 new_spec_types =
2234 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2235 TREE_CHAIN (tmpl_types));
2236
2237 /* Add the default argument for this parameter. */
2238 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2239 TREE_VALUE (spec_types),
2240 new_spec_types);
2241 }
2242
2243 /* DECL is an explicit specialization. Replicate default arguments
2244 from the template it specializes. (That way, code like:
2245
2246 template <class T> void f(T = 3);
2247 template <> void f(double);
2248 void g () { f (); }
2249
2250 works, as required.) An alternative approach would be to look up
2251 the correct default arguments at the call-site, but this approach
2252 is consistent with how implicit instantiations are handled. */
2253
2254 static void
2255 copy_default_args_to_explicit_spec (tree decl)
2256 {
2257 tree tmpl;
2258 tree spec_types;
2259 tree tmpl_types;
2260 tree new_spec_types;
2261 tree old_type;
2262 tree new_type;
2263 tree t;
2264 tree object_type = NULL_TREE;
2265 tree in_charge = NULL_TREE;
2266 tree vtt = NULL_TREE;
2267
2268 /* See if there's anything we need to do. */
2269 tmpl = DECL_TI_TEMPLATE (decl);
2270 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2271 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2272 if (TREE_PURPOSE (t))
2273 break;
2274 if (!t)
2275 return;
2276
2277 old_type = TREE_TYPE (decl);
2278 spec_types = TYPE_ARG_TYPES (old_type);
2279
2280 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2281 {
2282 /* Remove the this pointer, but remember the object's type for
2283 CV quals. */
2284 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2285 spec_types = TREE_CHAIN (spec_types);
2286 tmpl_types = TREE_CHAIN (tmpl_types);
2287
2288 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2289 {
2290 /* DECL may contain more parameters than TMPL due to the extra
2291 in-charge parameter in constructors and destructors. */
2292 in_charge = spec_types;
2293 spec_types = TREE_CHAIN (spec_types);
2294 }
2295 if (DECL_HAS_VTT_PARM_P (decl))
2296 {
2297 vtt = spec_types;
2298 spec_types = TREE_CHAIN (spec_types);
2299 }
2300 }
2301
2302 /* Compute the merged default arguments. */
2303 new_spec_types =
2304 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2305
2306 /* Compute the new FUNCTION_TYPE. */
2307 if (object_type)
2308 {
2309 if (vtt)
2310 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2311 TREE_VALUE (vtt),
2312 new_spec_types);
2313
2314 if (in_charge)
2315 /* Put the in-charge parameter back. */
2316 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2317 TREE_VALUE (in_charge),
2318 new_spec_types);
2319
2320 new_type = build_method_type_directly (object_type,
2321 TREE_TYPE (old_type),
2322 new_spec_types);
2323 }
2324 else
2325 new_type = build_function_type (TREE_TYPE (old_type),
2326 new_spec_types);
2327 new_type = cp_build_type_attribute_variant (new_type,
2328 TYPE_ATTRIBUTES (old_type));
2329 new_type = build_exception_variant (new_type,
2330 TYPE_RAISES_EXCEPTIONS (old_type));
2331
2332 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2333 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2334
2335 TREE_TYPE (decl) = new_type;
2336 }
2337
2338 /* Return the number of template headers we expect to see for a definition
2339 or specialization of CTYPE or one of its non-template members. */
2340
2341 int
2342 num_template_headers_for_class (tree ctype)
2343 {
2344 int num_templates = 0;
2345
2346 while (ctype && CLASS_TYPE_P (ctype))
2347 {
2348 /* You're supposed to have one `template <...>' for every
2349 template class, but you don't need one for a full
2350 specialization. For example:
2351
2352 template <class T> struct S{};
2353 template <> struct S<int> { void f(); };
2354 void S<int>::f () {}
2355
2356 is correct; there shouldn't be a `template <>' for the
2357 definition of `S<int>::f'. */
2358 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2359 /* If CTYPE does not have template information of any
2360 kind, then it is not a template, nor is it nested
2361 within a template. */
2362 break;
2363 if (explicit_class_specialization_p (ctype))
2364 break;
2365 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2366 ++num_templates;
2367
2368 ctype = TYPE_CONTEXT (ctype);
2369 }
2370
2371 return num_templates;
2372 }
2373
2374 /* Do a simple sanity check on the template headers that precede the
2375 variable declaration DECL. */
2376
2377 void
2378 check_template_variable (tree decl)
2379 {
2380 tree ctx = CP_DECL_CONTEXT (decl);
2381 int wanted = num_template_headers_for_class (ctx);
2382 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2383 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2384 {
2385 if (cxx_dialect < cxx14)
2386 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2387 "variable templates only available with "
2388 "-std=c++14 or -std=gnu++14");
2389
2390 // Namespace-scope variable templates should have a template header.
2391 ++wanted;
2392 }
2393 if (template_header_count > wanted)
2394 {
2395 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2396 "too many template headers for %D (should be %d)",
2397 decl, wanted);
2398 if (warned && CLASS_TYPE_P (ctx)
2399 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2400 inform (DECL_SOURCE_LOCATION (decl),
2401 "members of an explicitly specialized class are defined "
2402 "without a template header");
2403 }
2404 }
2405
2406 /* Check to see if the function just declared, as indicated in
2407 DECLARATOR, and in DECL, is a specialization of a function
2408 template. We may also discover that the declaration is an explicit
2409 instantiation at this point.
2410
2411 Returns DECL, or an equivalent declaration that should be used
2412 instead if all goes well. Issues an error message if something is
2413 amiss. Returns error_mark_node if the error is not easily
2414 recoverable.
2415
2416 FLAGS is a bitmask consisting of the following flags:
2417
2418 2: The function has a definition.
2419 4: The function is a friend.
2420
2421 The TEMPLATE_COUNT is the number of references to qualifying
2422 template classes that appeared in the name of the function. For
2423 example, in
2424
2425 template <class T> struct S { void f(); };
2426 void S<int>::f();
2427
2428 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2429 classes are not counted in the TEMPLATE_COUNT, so that in
2430
2431 template <class T> struct S {};
2432 template <> struct S<int> { void f(); }
2433 template <> void S<int>::f();
2434
2435 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2436 invalid; there should be no template <>.)
2437
2438 If the function is a specialization, it is marked as such via
2439 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2440 is set up correctly, and it is added to the list of specializations
2441 for that template. */
2442
2443 tree
2444 check_explicit_specialization (tree declarator,
2445 tree decl,
2446 int template_count,
2447 int flags)
2448 {
2449 int have_def = flags & 2;
2450 int is_friend = flags & 4;
2451 int specialization = 0;
2452 int explicit_instantiation = 0;
2453 int member_specialization = 0;
2454 tree ctype = DECL_CLASS_CONTEXT (decl);
2455 tree dname = DECL_NAME (decl);
2456 tmpl_spec_kind tsk;
2457
2458 if (is_friend)
2459 {
2460 if (!processing_specialization)
2461 tsk = tsk_none;
2462 else
2463 tsk = tsk_excessive_parms;
2464 }
2465 else
2466 tsk = current_tmpl_spec_kind (template_count);
2467
2468 switch (tsk)
2469 {
2470 case tsk_none:
2471 if (processing_specialization && !VAR_P (decl))
2472 {
2473 specialization = 1;
2474 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2475 }
2476 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2477 {
2478 if (is_friend)
2479 /* This could be something like:
2480
2481 template <class T> void f(T);
2482 class S { friend void f<>(int); } */
2483 specialization = 1;
2484 else
2485 {
2486 /* This case handles bogus declarations like template <>
2487 template <class T> void f<int>(); */
2488
2489 error ("template-id %qD in declaration of primary template",
2490 declarator);
2491 return decl;
2492 }
2493 }
2494 break;
2495
2496 case tsk_invalid_member_spec:
2497 /* The error has already been reported in
2498 check_specialization_scope. */
2499 return error_mark_node;
2500
2501 case tsk_invalid_expl_inst:
2502 error ("template parameter list used in explicit instantiation");
2503
2504 /* Fall through. */
2505
2506 case tsk_expl_inst:
2507 if (have_def)
2508 error ("definition provided for explicit instantiation");
2509
2510 explicit_instantiation = 1;
2511 break;
2512
2513 case tsk_excessive_parms:
2514 case tsk_insufficient_parms:
2515 if (tsk == tsk_excessive_parms)
2516 error ("too many template parameter lists in declaration of %qD",
2517 decl);
2518 else if (template_header_count)
2519 error("too few template parameter lists in declaration of %qD", decl);
2520 else
2521 error("explicit specialization of %qD must be introduced by "
2522 "%<template <>%>", decl);
2523
2524 /* Fall through. */
2525 case tsk_expl_spec:
2526 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2527 /* In cases like template<> constexpr bool v = true;
2528 We'll give an error in check_template_variable. */
2529 break;
2530
2531 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2532 if (ctype)
2533 member_specialization = 1;
2534 else
2535 specialization = 1;
2536 break;
2537
2538 case tsk_template:
2539 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2540 {
2541 /* This case handles bogus declarations like template <>
2542 template <class T> void f<int>(); */
2543
2544 if (!uses_template_parms (declarator))
2545 error ("template-id %qD in declaration of primary template",
2546 declarator);
2547 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2548 {
2549 /* Partial specialization of variable template. */
2550 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2551 specialization = 1;
2552 goto ok;
2553 }
2554 else if (cxx_dialect < cxx14)
2555 error ("non-type partial specialization %qD "
2556 "is not allowed", declarator);
2557 else
2558 error ("non-class, non-variable partial specialization %qD "
2559 "is not allowed", declarator);
2560 return decl;
2561 ok:;
2562 }
2563
2564 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2565 /* This is a specialization of a member template, without
2566 specialization the containing class. Something like:
2567
2568 template <class T> struct S {
2569 template <class U> void f (U);
2570 };
2571 template <> template <class U> void S<int>::f(U) {}
2572
2573 That's a specialization -- but of the entire template. */
2574 specialization = 1;
2575 break;
2576
2577 default:
2578 gcc_unreachable ();
2579 }
2580
2581 if ((specialization || member_specialization)
2582 /* This doesn't apply to variable templates. */
2583 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2584 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2585 {
2586 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2587 for (; t; t = TREE_CHAIN (t))
2588 if (TREE_PURPOSE (t))
2589 {
2590 permerror (input_location,
2591 "default argument specified in explicit specialization");
2592 break;
2593 }
2594 }
2595
2596 if (specialization || member_specialization || explicit_instantiation)
2597 {
2598 tree tmpl = NULL_TREE;
2599 tree targs = NULL_TREE;
2600 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2601
2602 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2603 if (!was_template_id)
2604 {
2605 tree fns;
2606
2607 gcc_assert (identifier_p (declarator));
2608 if (ctype)
2609 fns = dname;
2610 else
2611 {
2612 /* If there is no class context, the explicit instantiation
2613 must be at namespace scope. */
2614 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2615
2616 /* Find the namespace binding, using the declaration
2617 context. */
2618 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2619 false, true);
2620 if (fns == error_mark_node || !is_overloaded_fn (fns))
2621 {
2622 error ("%qD is not a template function", dname);
2623 fns = error_mark_node;
2624 }
2625 else
2626 {
2627 tree fn = OVL_CURRENT (fns);
2628 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2629 CP_DECL_CONTEXT (fn)))
2630 error ("%qD is not declared in %qD",
2631 decl, current_namespace);
2632 }
2633 }
2634
2635 declarator = lookup_template_function (fns, NULL_TREE);
2636 }
2637
2638 if (declarator == error_mark_node)
2639 return error_mark_node;
2640
2641 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2642 {
2643 if (!explicit_instantiation)
2644 /* A specialization in class scope. This is invalid,
2645 but the error will already have been flagged by
2646 check_specialization_scope. */
2647 return error_mark_node;
2648 else
2649 {
2650 /* It's not valid to write an explicit instantiation in
2651 class scope, e.g.:
2652
2653 class C { template void f(); }
2654
2655 This case is caught by the parser. However, on
2656 something like:
2657
2658 template class C { void f(); };
2659
2660 (which is invalid) we can get here. The error will be
2661 issued later. */
2662 ;
2663 }
2664
2665 return decl;
2666 }
2667 else if (ctype != NULL_TREE
2668 && (identifier_p (TREE_OPERAND (declarator, 0))))
2669 {
2670 // We'll match variable templates in start_decl.
2671 if (VAR_P (decl))
2672 return decl;
2673
2674 /* Find the list of functions in ctype that have the same
2675 name as the declared function. */
2676 tree name = TREE_OPERAND (declarator, 0);
2677 tree fns = NULL_TREE;
2678 int idx;
2679
2680 if (constructor_name_p (name, ctype))
2681 {
2682 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2683
2684 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2685 : !CLASSTYPE_DESTRUCTORS (ctype))
2686 {
2687 /* From [temp.expl.spec]:
2688
2689 If such an explicit specialization for the member
2690 of a class template names an implicitly-declared
2691 special member function (clause _special_), the
2692 program is ill-formed.
2693
2694 Similar language is found in [temp.explicit]. */
2695 error ("specialization of implicitly-declared special member function");
2696 return error_mark_node;
2697 }
2698
2699 name = is_constructor ? ctor_identifier : dtor_identifier;
2700 }
2701
2702 if (!DECL_CONV_FN_P (decl))
2703 {
2704 idx = lookup_fnfields_1 (ctype, name);
2705 if (idx >= 0)
2706 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2707 }
2708 else
2709 {
2710 vec<tree, va_gc> *methods;
2711 tree ovl;
2712
2713 /* For a type-conversion operator, we cannot do a
2714 name-based lookup. We might be looking for `operator
2715 int' which will be a specialization of `operator T'.
2716 So, we find *all* the conversion operators, and then
2717 select from them. */
2718 fns = NULL_TREE;
2719
2720 methods = CLASSTYPE_METHOD_VEC (ctype);
2721 if (methods)
2722 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2723 methods->iterate (idx, &ovl);
2724 ++idx)
2725 {
2726 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2727 /* There are no more conversion functions. */
2728 break;
2729
2730 /* Glue all these conversion functions together
2731 with those we already have. */
2732 for (; ovl; ovl = OVL_NEXT (ovl))
2733 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2734 }
2735 }
2736
2737 if (fns == NULL_TREE)
2738 {
2739 error ("no member function %qD declared in %qT", name, ctype);
2740 return error_mark_node;
2741 }
2742 else
2743 TREE_OPERAND (declarator, 0) = fns;
2744 }
2745
2746 /* Figure out what exactly is being specialized at this point.
2747 Note that for an explicit instantiation, even one for a
2748 member function, we cannot tell apriori whether the
2749 instantiation is for a member template, or just a member
2750 function of a template class. Even if a member template is
2751 being instantiated, the member template arguments may be
2752 elided if they can be deduced from the rest of the
2753 declaration. */
2754 tmpl = determine_specialization (declarator, decl,
2755 &targs,
2756 member_specialization,
2757 template_count,
2758 tsk);
2759
2760 if (!tmpl || tmpl == error_mark_node)
2761 /* We couldn't figure out what this declaration was
2762 specializing. */
2763 return error_mark_node;
2764 else
2765 {
2766 tree gen_tmpl = most_general_template (tmpl);
2767
2768 if (explicit_instantiation)
2769 {
2770 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2771 is done by do_decl_instantiation later. */
2772
2773 int arg_depth = TMPL_ARGS_DEPTH (targs);
2774 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2775
2776 if (arg_depth > parm_depth)
2777 {
2778 /* If TMPL is not the most general template (for
2779 example, if TMPL is a friend template that is
2780 injected into namespace scope), then there will
2781 be too many levels of TARGS. Remove some of them
2782 here. */
2783 int i;
2784 tree new_targs;
2785
2786 new_targs = make_tree_vec (parm_depth);
2787 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2788 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2789 = TREE_VEC_ELT (targs, i);
2790 targs = new_targs;
2791 }
2792
2793 return instantiate_template (tmpl, targs, tf_error);
2794 }
2795
2796 /* If we thought that the DECL was a member function, but it
2797 turns out to be specializing a static member function,
2798 make DECL a static member function as well. */
2799 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2800 && DECL_STATIC_FUNCTION_P (tmpl)
2801 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2802 revert_static_member_fn (decl);
2803
2804 /* If this is a specialization of a member template of a
2805 template class, we want to return the TEMPLATE_DECL, not
2806 the specialization of it. */
2807 if (tsk == tsk_template && !was_template_id)
2808 {
2809 tree result = DECL_TEMPLATE_RESULT (tmpl);
2810 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2811 DECL_INITIAL (result) = NULL_TREE;
2812 if (have_def)
2813 {
2814 tree parm;
2815 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2816 DECL_SOURCE_LOCATION (result)
2817 = DECL_SOURCE_LOCATION (decl);
2818 /* We want to use the argument list specified in the
2819 definition, not in the original declaration. */
2820 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2821 for (parm = DECL_ARGUMENTS (result); parm;
2822 parm = DECL_CHAIN (parm))
2823 DECL_CONTEXT (parm) = result;
2824 }
2825 return register_specialization (tmpl, gen_tmpl, targs,
2826 is_friend, 0);
2827 }
2828
2829 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2830 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2831
2832 if (was_template_id)
2833 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
2834
2835 /* Inherit default function arguments from the template
2836 DECL is specializing. */
2837 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2838 copy_default_args_to_explicit_spec (decl);
2839
2840 /* This specialization has the same protection as the
2841 template it specializes. */
2842 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2843 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2844
2845 /* 7.1.1-1 [dcl.stc]
2846
2847 A storage-class-specifier shall not be specified in an
2848 explicit specialization...
2849
2850 The parser rejects these, so unless action is taken here,
2851 explicit function specializations will always appear with
2852 global linkage.
2853
2854 The action recommended by the C++ CWG in response to C++
2855 defect report 605 is to make the storage class and linkage
2856 of the explicit specialization match the templated function:
2857
2858 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2859 */
2860 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2861 {
2862 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2863 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2864
2865 /* This specialization has the same linkage and visibility as
2866 the function template it specializes. */
2867 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2868 if (! TREE_PUBLIC (decl))
2869 {
2870 DECL_INTERFACE_KNOWN (decl) = 1;
2871 DECL_NOT_REALLY_EXTERN (decl) = 1;
2872 }
2873 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2874 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2875 {
2876 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2877 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2878 }
2879 }
2880
2881 /* If DECL is a friend declaration, declared using an
2882 unqualified name, the namespace associated with DECL may
2883 have been set incorrectly. For example, in:
2884
2885 template <typename T> void f(T);
2886 namespace N {
2887 struct S { friend void f<int>(int); }
2888 }
2889
2890 we will have set the DECL_CONTEXT for the friend
2891 declaration to N, rather than to the global namespace. */
2892 if (DECL_NAMESPACE_SCOPE_P (decl))
2893 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2894
2895 if (is_friend && !have_def)
2896 /* This is not really a declaration of a specialization.
2897 It's just the name of an instantiation. But, it's not
2898 a request for an instantiation, either. */
2899 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2900 else if (TREE_CODE (decl) == FUNCTION_DECL)
2901 /* A specialization is not necessarily COMDAT. */
2902 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
2903 && DECL_DECLARED_INLINE_P (decl));
2904 else if (VAR_P (decl))
2905 DECL_COMDAT (decl) = false;
2906
2907 /* Register this specialization so that we can find it
2908 again. */
2909 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2910
2911 /* A 'structor should already have clones. */
2912 gcc_assert (decl == error_mark_node
2913 || variable_template_p (tmpl)
2914 || !(DECL_CONSTRUCTOR_P (decl)
2915 || DECL_DESTRUCTOR_P (decl))
2916 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
2917 }
2918 }
2919
2920 return decl;
2921 }
2922
2923 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2924 parameters. These are represented in the same format used for
2925 DECL_TEMPLATE_PARMS. */
2926
2927 int
2928 comp_template_parms (const_tree parms1, const_tree parms2)
2929 {
2930 const_tree p1;
2931 const_tree p2;
2932
2933 if (parms1 == parms2)
2934 return 1;
2935
2936 for (p1 = parms1, p2 = parms2;
2937 p1 != NULL_TREE && p2 != NULL_TREE;
2938 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2939 {
2940 tree t1 = TREE_VALUE (p1);
2941 tree t2 = TREE_VALUE (p2);
2942 int i;
2943
2944 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2945 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2946
2947 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2948 return 0;
2949
2950 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2951 {
2952 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2953 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2954
2955 /* If either of the template parameters are invalid, assume
2956 they match for the sake of error recovery. */
2957 if (error_operand_p (parm1) || error_operand_p (parm2))
2958 return 1;
2959
2960 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2961 return 0;
2962
2963 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2964 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2965 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2966 continue;
2967 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2968 return 0;
2969 }
2970 }
2971
2972 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2973 /* One set of parameters has more parameters lists than the
2974 other. */
2975 return 0;
2976
2977 return 1;
2978 }
2979
2980 /* Determine whether PARM is a parameter pack. */
2981
2982 bool
2983 template_parameter_pack_p (const_tree parm)
2984 {
2985 /* Determine if we have a non-type template parameter pack. */
2986 if (TREE_CODE (parm) == PARM_DECL)
2987 return (DECL_TEMPLATE_PARM_P (parm)
2988 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2989 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2990 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2991
2992 /* If this is a list of template parameters, we could get a
2993 TYPE_DECL or a TEMPLATE_DECL. */
2994 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2995 parm = TREE_TYPE (parm);
2996
2997 /* Otherwise it must be a type template parameter. */
2998 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2999 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3000 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3001 }
3002
3003 /* Determine if T is a function parameter pack. */
3004
3005 bool
3006 function_parameter_pack_p (const_tree t)
3007 {
3008 if (t && TREE_CODE (t) == PARM_DECL)
3009 return DECL_PACK_P (t);
3010 return false;
3011 }
3012
3013 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3014 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3015
3016 tree
3017 get_function_template_decl (const_tree primary_func_tmpl_inst)
3018 {
3019 if (! primary_func_tmpl_inst
3020 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3021 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3022 return NULL;
3023
3024 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3025 }
3026
3027 /* Return true iff the function parameter PARAM_DECL was expanded
3028 from the function parameter pack PACK. */
3029
3030 bool
3031 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3032 {
3033 if (DECL_ARTIFICIAL (param_decl)
3034 || !function_parameter_pack_p (pack))
3035 return false;
3036
3037 /* The parameter pack and its pack arguments have the same
3038 DECL_PARM_INDEX. */
3039 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3040 }
3041
3042 /* Determine whether ARGS describes a variadic template args list,
3043 i.e., one that is terminated by a template argument pack. */
3044
3045 static bool
3046 template_args_variadic_p (tree args)
3047 {
3048 int nargs;
3049 tree last_parm;
3050
3051 if (args == NULL_TREE)
3052 return false;
3053
3054 args = INNERMOST_TEMPLATE_ARGS (args);
3055 nargs = TREE_VEC_LENGTH (args);
3056
3057 if (nargs == 0)
3058 return false;
3059
3060 last_parm = TREE_VEC_ELT (args, nargs - 1);
3061
3062 return ARGUMENT_PACK_P (last_parm);
3063 }
3064
3065 /* Generate a new name for the parameter pack name NAME (an
3066 IDENTIFIER_NODE) that incorporates its */
3067
3068 static tree
3069 make_ith_pack_parameter_name (tree name, int i)
3070 {
3071 /* Munge the name to include the parameter index. */
3072 #define NUMBUF_LEN 128
3073 char numbuf[NUMBUF_LEN];
3074 char* newname;
3075 int newname_len;
3076
3077 if (name == NULL_TREE)
3078 return name;
3079 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3080 newname_len = IDENTIFIER_LENGTH (name)
3081 + strlen (numbuf) + 2;
3082 newname = (char*)alloca (newname_len);
3083 snprintf (newname, newname_len,
3084 "%s#%i", IDENTIFIER_POINTER (name), i);
3085 return get_identifier (newname);
3086 }
3087
3088 /* Return true if T is a primary function, class or alias template
3089 instantiation. */
3090
3091 bool
3092 primary_template_instantiation_p (const_tree t)
3093 {
3094 if (!t)
3095 return false;
3096
3097 if (TREE_CODE (t) == FUNCTION_DECL)
3098 return DECL_LANG_SPECIFIC (t)
3099 && DECL_TEMPLATE_INSTANTIATION (t)
3100 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3101 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3102 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3103 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3104 else if (alias_template_specialization_p (t))
3105 return true;
3106 return false;
3107 }
3108
3109 /* Return true if PARM is a template template parameter. */
3110
3111 bool
3112 template_template_parameter_p (const_tree parm)
3113 {
3114 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3115 }
3116
3117 /* Return true iff PARM is a DECL representing a type template
3118 parameter. */
3119
3120 bool
3121 template_type_parameter_p (const_tree parm)
3122 {
3123 return (parm
3124 && (TREE_CODE (parm) == TYPE_DECL
3125 || TREE_CODE (parm) == TEMPLATE_DECL)
3126 && DECL_TEMPLATE_PARM_P (parm));
3127 }
3128
3129 /* Return the template parameters of T if T is a
3130 primary template instantiation, NULL otherwise. */
3131
3132 tree
3133 get_primary_template_innermost_parameters (const_tree t)
3134 {
3135 tree parms = NULL, template_info = NULL;
3136
3137 if ((template_info = get_template_info (t))
3138 && primary_template_instantiation_p (t))
3139 parms = INNERMOST_TEMPLATE_PARMS
3140 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3141
3142 return parms;
3143 }
3144
3145 /* Return the template parameters of the LEVELth level from the full list
3146 of template parameters PARMS. */
3147
3148 tree
3149 get_template_parms_at_level (tree parms, int level)
3150 {
3151 tree p;
3152 if (!parms
3153 || TREE_CODE (parms) != TREE_LIST
3154 || level > TMPL_PARMS_DEPTH (parms))
3155 return NULL_TREE;
3156
3157 for (p = parms; p; p = TREE_CHAIN (p))
3158 if (TMPL_PARMS_DEPTH (p) == level)
3159 return p;
3160
3161 return NULL_TREE;
3162 }
3163
3164 /* Returns the template arguments of T if T is a template instantiation,
3165 NULL otherwise. */
3166
3167 tree
3168 get_template_innermost_arguments (const_tree t)
3169 {
3170 tree args = NULL, template_info = NULL;
3171
3172 if ((template_info = get_template_info (t))
3173 && TI_ARGS (template_info))
3174 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3175
3176 return args;
3177 }
3178
3179 /* Return the argument pack elements of T if T is a template argument pack,
3180 NULL otherwise. */
3181
3182 tree
3183 get_template_argument_pack_elems (const_tree t)
3184 {
3185 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3186 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3187 return NULL;
3188
3189 return ARGUMENT_PACK_ARGS (t);
3190 }
3191
3192 /* Structure used to track the progress of find_parameter_packs_r. */
3193 struct find_parameter_pack_data
3194 {
3195 /* TREE_LIST that will contain all of the parameter packs found by
3196 the traversal. */
3197 tree* parameter_packs;
3198
3199 /* Set of AST nodes that have been visited by the traversal. */
3200 hash_set<tree> *visited;
3201 };
3202
3203 /* Identifies all of the argument packs that occur in a template
3204 argument and appends them to the TREE_LIST inside DATA, which is a
3205 find_parameter_pack_data structure. This is a subroutine of
3206 make_pack_expansion and uses_parameter_packs. */
3207 static tree
3208 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3209 {
3210 tree t = *tp;
3211 struct find_parameter_pack_data* ppd =
3212 (struct find_parameter_pack_data*)data;
3213 bool parameter_pack_p = false;
3214
3215 /* Handle type aliases/typedefs. */
3216 if (TYPE_ALIAS_P (t))
3217 {
3218 if (TYPE_TEMPLATE_INFO (t))
3219 cp_walk_tree (&TYPE_TI_ARGS (t),
3220 &find_parameter_packs_r,
3221 ppd, ppd->visited);
3222 *walk_subtrees = 0;
3223 return NULL_TREE;
3224 }
3225
3226 /* Identify whether this is a parameter pack or not. */
3227 switch (TREE_CODE (t))
3228 {
3229 case TEMPLATE_PARM_INDEX:
3230 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3231 parameter_pack_p = true;
3232 break;
3233
3234 case TEMPLATE_TYPE_PARM:
3235 t = TYPE_MAIN_VARIANT (t);
3236 case TEMPLATE_TEMPLATE_PARM:
3237 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3238 parameter_pack_p = true;
3239 break;
3240
3241 case FIELD_DECL:
3242 case PARM_DECL:
3243 if (DECL_PACK_P (t))
3244 {
3245 /* We don't want to walk into the type of a PARM_DECL,
3246 because we don't want to see the type parameter pack. */
3247 *walk_subtrees = 0;
3248 parameter_pack_p = true;
3249 }
3250 break;
3251
3252 /* Look through a lambda capture proxy to the field pack. */
3253 case VAR_DECL:
3254 if (DECL_HAS_VALUE_EXPR_P (t))
3255 {
3256 tree v = DECL_VALUE_EXPR (t);
3257 cp_walk_tree (&v,
3258 &find_parameter_packs_r,
3259 ppd, ppd->visited);
3260 *walk_subtrees = 0;
3261 }
3262 else if (variable_template_specialization_p (t))
3263 {
3264 cp_walk_tree (&DECL_TI_ARGS (t),
3265 find_parameter_packs_r,
3266 ppd, ppd->visited);
3267 *walk_subtrees = 0;
3268 }
3269 break;
3270
3271 case BASES:
3272 parameter_pack_p = true;
3273 break;
3274 default:
3275 /* Not a parameter pack. */
3276 break;
3277 }
3278
3279 if (parameter_pack_p)
3280 {
3281 /* Add this parameter pack to the list. */
3282 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3283 }
3284
3285 if (TYPE_P (t))
3286 cp_walk_tree (&TYPE_CONTEXT (t),
3287 &find_parameter_packs_r, ppd, ppd->visited);
3288
3289 /* This switch statement will return immediately if we don't find a
3290 parameter pack. */
3291 switch (TREE_CODE (t))
3292 {
3293 case TEMPLATE_PARM_INDEX:
3294 return NULL_TREE;
3295
3296 case BOUND_TEMPLATE_TEMPLATE_PARM:
3297 /* Check the template itself. */
3298 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3299 &find_parameter_packs_r, ppd, ppd->visited);
3300 /* Check the template arguments. */
3301 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3302 ppd->visited);
3303 *walk_subtrees = 0;
3304 return NULL_TREE;
3305
3306 case TEMPLATE_TYPE_PARM:
3307 case TEMPLATE_TEMPLATE_PARM:
3308 return NULL_TREE;
3309
3310 case PARM_DECL:
3311 return NULL_TREE;
3312
3313 case RECORD_TYPE:
3314 if (TYPE_PTRMEMFUNC_P (t))
3315 return NULL_TREE;
3316 /* Fall through. */
3317
3318 case UNION_TYPE:
3319 case ENUMERAL_TYPE:
3320 if (TYPE_TEMPLATE_INFO (t))
3321 cp_walk_tree (&TYPE_TI_ARGS (t),
3322 &find_parameter_packs_r, ppd, ppd->visited);
3323
3324 *walk_subtrees = 0;
3325 return NULL_TREE;
3326
3327 case CONSTRUCTOR:
3328 case TEMPLATE_DECL:
3329 cp_walk_tree (&TREE_TYPE (t),
3330 &find_parameter_packs_r, ppd, ppd->visited);
3331 return NULL_TREE;
3332
3333 case TYPENAME_TYPE:
3334 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3335 ppd, ppd->visited);
3336 *walk_subtrees = 0;
3337 return NULL_TREE;
3338
3339 case TYPE_PACK_EXPANSION:
3340 case EXPR_PACK_EXPANSION:
3341 *walk_subtrees = 0;
3342 return NULL_TREE;
3343
3344 case INTEGER_TYPE:
3345 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3346 ppd, ppd->visited);
3347 *walk_subtrees = 0;
3348 return NULL_TREE;
3349
3350 case IDENTIFIER_NODE:
3351 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3352 ppd->visited);
3353 *walk_subtrees = 0;
3354 return NULL_TREE;
3355
3356 default:
3357 return NULL_TREE;
3358 }
3359
3360 return NULL_TREE;
3361 }
3362
3363 /* Determines if the expression or type T uses any parameter packs. */
3364 bool
3365 uses_parameter_packs (tree t)
3366 {
3367 tree parameter_packs = NULL_TREE;
3368 struct find_parameter_pack_data ppd;
3369 ppd.parameter_packs = &parameter_packs;
3370 ppd.visited = new hash_set<tree>;
3371 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3372 delete ppd.visited;
3373 return parameter_packs != NULL_TREE;
3374 }
3375
3376 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3377 representation a base-class initializer into a parameter pack
3378 expansion. If all goes well, the resulting node will be an
3379 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3380 respectively. */
3381 tree
3382 make_pack_expansion (tree arg)
3383 {
3384 tree result;
3385 tree parameter_packs = NULL_TREE;
3386 bool for_types = false;
3387 struct find_parameter_pack_data ppd;
3388
3389 if (!arg || arg == error_mark_node)
3390 return arg;
3391
3392 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3393 {
3394 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3395 class initializer. In this case, the TREE_PURPOSE will be a
3396 _TYPE node (representing the base class expansion we're
3397 initializing) and the TREE_VALUE will be a TREE_LIST
3398 containing the initialization arguments.
3399
3400 The resulting expansion looks somewhat different from most
3401 expansions. Rather than returning just one _EXPANSION, we
3402 return a TREE_LIST whose TREE_PURPOSE is a
3403 TYPE_PACK_EXPANSION containing the bases that will be
3404 initialized. The TREE_VALUE will be identical to the
3405 original TREE_VALUE, which is a list of arguments that will
3406 be passed to each base. We do not introduce any new pack
3407 expansion nodes into the TREE_VALUE (although it is possible
3408 that some already exist), because the TREE_PURPOSE and
3409 TREE_VALUE all need to be expanded together with the same
3410 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3411 resulting TREE_PURPOSE will mention the parameter packs in
3412 both the bases and the arguments to the bases. */
3413 tree purpose;
3414 tree value;
3415 tree parameter_packs = NULL_TREE;
3416
3417 /* Determine which parameter packs will be used by the base
3418 class expansion. */
3419 ppd.visited = new hash_set<tree>;
3420 ppd.parameter_packs = &parameter_packs;
3421 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3422 &ppd, ppd.visited);
3423
3424 if (parameter_packs == NULL_TREE)
3425 {
3426 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3427 delete ppd.visited;
3428 return error_mark_node;
3429 }
3430
3431 if (TREE_VALUE (arg) != void_type_node)
3432 {
3433 /* Collect the sets of parameter packs used in each of the
3434 initialization arguments. */
3435 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3436 {
3437 /* Determine which parameter packs will be expanded in this
3438 argument. */
3439 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3440 &ppd, ppd.visited);
3441 }
3442 }
3443
3444 delete ppd.visited;
3445
3446 /* Create the pack expansion type for the base type. */
3447 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3448 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3449 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3450
3451 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3452 they will rarely be compared to anything. */
3453 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3454
3455 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3456 }
3457
3458 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3459 for_types = true;
3460
3461 /* Build the PACK_EXPANSION_* node. */
3462 result = for_types
3463 ? cxx_make_type (TYPE_PACK_EXPANSION)
3464 : make_node (EXPR_PACK_EXPANSION);
3465 SET_PACK_EXPANSION_PATTERN (result, arg);
3466 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3467 {
3468 /* Propagate type and const-expression information. */
3469 TREE_TYPE (result) = TREE_TYPE (arg);
3470 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3471 }
3472 else
3473 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3474 they will rarely be compared to anything. */
3475 SET_TYPE_STRUCTURAL_EQUALITY (result);
3476
3477 /* Determine which parameter packs will be expanded. */
3478 ppd.parameter_packs = &parameter_packs;
3479 ppd.visited = new hash_set<tree>;
3480 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3481 delete ppd.visited;
3482
3483 /* Make sure we found some parameter packs. */
3484 if (parameter_packs == NULL_TREE)
3485 {
3486 if (TYPE_P (arg))
3487 error ("expansion pattern %<%T%> contains no argument packs", arg);
3488 else
3489 error ("expansion pattern %<%E%> contains no argument packs", arg);
3490 return error_mark_node;
3491 }
3492 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3493
3494 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3495
3496 return result;
3497 }
3498
3499 /* Checks T for any "bare" parameter packs, which have not yet been
3500 expanded, and issues an error if any are found. This operation can
3501 only be done on full expressions or types (e.g., an expression
3502 statement, "if" condition, etc.), because we could have expressions like:
3503
3504 foo(f(g(h(args)))...)
3505
3506 where "args" is a parameter pack. check_for_bare_parameter_packs
3507 should not be called for the subexpressions args, h(args),
3508 g(h(args)), or f(g(h(args))), because we would produce erroneous
3509 error messages.
3510
3511 Returns TRUE and emits an error if there were bare parameter packs,
3512 returns FALSE otherwise. */
3513 bool
3514 check_for_bare_parameter_packs (tree t)
3515 {
3516 tree parameter_packs = NULL_TREE;
3517 struct find_parameter_pack_data ppd;
3518
3519 if (!processing_template_decl || !t || t == error_mark_node)
3520 return false;
3521
3522 if (TREE_CODE (t) == TYPE_DECL)
3523 t = TREE_TYPE (t);
3524
3525 ppd.parameter_packs = &parameter_packs;
3526 ppd.visited = new hash_set<tree>;
3527 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3528 delete ppd.visited;
3529
3530 if (parameter_packs)
3531 {
3532 error ("parameter packs not expanded with %<...%>:");
3533 while (parameter_packs)
3534 {
3535 tree pack = TREE_VALUE (parameter_packs);
3536 tree name = NULL_TREE;
3537
3538 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3539 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3540 name = TYPE_NAME (pack);
3541 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3542 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3543 else
3544 name = DECL_NAME (pack);
3545
3546 if (name)
3547 inform (input_location, " %qD", name);
3548 else
3549 inform (input_location, " <anonymous>");
3550
3551 parameter_packs = TREE_CHAIN (parameter_packs);
3552 }
3553
3554 return true;
3555 }
3556
3557 return false;
3558 }
3559
3560 /* Expand any parameter packs that occur in the template arguments in
3561 ARGS. */
3562 tree
3563 expand_template_argument_pack (tree args)
3564 {
3565 tree result_args = NULL_TREE;
3566 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3567 int num_result_args = -1;
3568 int non_default_args_count = -1;
3569
3570 /* First, determine if we need to expand anything, and the number of
3571 slots we'll need. */
3572 for (in_arg = 0; in_arg < nargs; ++in_arg)
3573 {
3574 tree arg = TREE_VEC_ELT (args, in_arg);
3575 if (arg == NULL_TREE)
3576 return args;
3577 if (ARGUMENT_PACK_P (arg))
3578 {
3579 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3580 if (num_result_args < 0)
3581 num_result_args = in_arg + num_packed;
3582 else
3583 num_result_args += num_packed;
3584 }
3585 else
3586 {
3587 if (num_result_args >= 0)
3588 num_result_args++;
3589 }
3590 }
3591
3592 /* If no expansion is necessary, we're done. */
3593 if (num_result_args < 0)
3594 return args;
3595
3596 /* Expand arguments. */
3597 result_args = make_tree_vec (num_result_args);
3598 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3599 non_default_args_count =
3600 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3601 for (in_arg = 0; in_arg < nargs; ++in_arg)
3602 {
3603 tree arg = TREE_VEC_ELT (args, in_arg);
3604 if (ARGUMENT_PACK_P (arg))
3605 {
3606 tree packed = ARGUMENT_PACK_ARGS (arg);
3607 int i, num_packed = TREE_VEC_LENGTH (packed);
3608 for (i = 0; i < num_packed; ++i, ++out_arg)
3609 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3610 if (non_default_args_count > 0)
3611 non_default_args_count += num_packed - 1;
3612 }
3613 else
3614 {
3615 TREE_VEC_ELT (result_args, out_arg) = arg;
3616 ++out_arg;
3617 }
3618 }
3619 if (non_default_args_count >= 0)
3620 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3621 return result_args;
3622 }
3623
3624 /* Checks if DECL shadows a template parameter.
3625
3626 [temp.local]: A template-parameter shall not be redeclared within its
3627 scope (including nested scopes).
3628
3629 Emits an error and returns TRUE if the DECL shadows a parameter,
3630 returns FALSE otherwise. */
3631
3632 bool
3633 check_template_shadow (tree decl)
3634 {
3635 tree olddecl;
3636
3637 /* If we're not in a template, we can't possibly shadow a template
3638 parameter. */
3639 if (!current_template_parms)
3640 return true;
3641
3642 /* Figure out what we're shadowing. */
3643 if (TREE_CODE (decl) == OVERLOAD)
3644 decl = OVL_CURRENT (decl);
3645 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3646
3647 /* If there's no previous binding for this name, we're not shadowing
3648 anything, let alone a template parameter. */
3649 if (!olddecl)
3650 return true;
3651
3652 /* If we're not shadowing a template parameter, we're done. Note
3653 that OLDDECL might be an OVERLOAD (or perhaps even an
3654 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3655 node. */
3656 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3657 return true;
3658
3659 /* We check for decl != olddecl to avoid bogus errors for using a
3660 name inside a class. We check TPFI to avoid duplicate errors for
3661 inline member templates. */
3662 if (decl == olddecl
3663 || (DECL_TEMPLATE_PARM_P (decl)
3664 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3665 return true;
3666
3667 /* Don't complain about the injected class name, as we've already
3668 complained about the class itself. */
3669 if (DECL_SELF_REFERENCE_P (decl))
3670 return false;
3671
3672 if (DECL_TEMPLATE_PARM_P (decl))
3673 error ("declaration of template parameter %q+D shadows "
3674 "template parameter", decl);
3675 else
3676 error ("declaration of %q+#D shadows template parameter", decl);
3677 inform (DECL_SOURCE_LOCATION (olddecl),
3678 "template parameter %qD declared here", olddecl);
3679 return false;
3680 }
3681
3682 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3683 ORIG_LEVEL, DECL, and TYPE. */
3684
3685 static tree
3686 build_template_parm_index (int index,
3687 int level,
3688 int orig_level,
3689 tree decl,
3690 tree type)
3691 {
3692 tree t = make_node (TEMPLATE_PARM_INDEX);
3693 TEMPLATE_PARM_IDX (t) = index;
3694 TEMPLATE_PARM_LEVEL (t) = level;
3695 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3696 TEMPLATE_PARM_DECL (t) = decl;
3697 TREE_TYPE (t) = type;
3698 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3699 TREE_READONLY (t) = TREE_READONLY (decl);
3700
3701 return t;
3702 }
3703
3704 /* Find the canonical type parameter for the given template type
3705 parameter. Returns the canonical type parameter, which may be TYPE
3706 if no such parameter existed. */
3707
3708 static tree
3709 canonical_type_parameter (tree type)
3710 {
3711 tree list;
3712 int idx = TEMPLATE_TYPE_IDX (type);
3713 if (!canonical_template_parms)
3714 vec_alloc (canonical_template_parms, idx+1);
3715
3716 while (canonical_template_parms->length () <= (unsigned)idx)
3717 vec_safe_push (canonical_template_parms, NULL_TREE);
3718
3719 list = (*canonical_template_parms)[idx];
3720 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3721 list = TREE_CHAIN (list);
3722
3723 if (list)
3724 return TREE_VALUE (list);
3725 else
3726 {
3727 (*canonical_template_parms)[idx]
3728 = tree_cons (NULL_TREE, type,
3729 (*canonical_template_parms)[idx]);
3730 return type;
3731 }
3732 }
3733
3734 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3735 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3736 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3737 new one is created. */
3738
3739 static tree
3740 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3741 tsubst_flags_t complain)
3742 {
3743 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3744 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3745 != TEMPLATE_PARM_LEVEL (index) - levels)
3746 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3747 {
3748 tree orig_decl = TEMPLATE_PARM_DECL (index);
3749 tree decl, t;
3750
3751 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3752 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3753 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3754 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3755 DECL_ARTIFICIAL (decl) = 1;
3756 SET_DECL_TEMPLATE_PARM_P (decl);
3757
3758 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3759 TEMPLATE_PARM_LEVEL (index) - levels,
3760 TEMPLATE_PARM_ORIG_LEVEL (index),
3761 decl, type);
3762 TEMPLATE_PARM_DESCENDANTS (index) = t;
3763 TEMPLATE_PARM_PARAMETER_PACK (t)
3764 = TEMPLATE_PARM_PARAMETER_PACK (index);
3765
3766 /* Template template parameters need this. */
3767 if (TREE_CODE (decl) == TEMPLATE_DECL)
3768 {
3769 DECL_TEMPLATE_RESULT (decl)
3770 = build_decl (DECL_SOURCE_LOCATION (decl),
3771 TYPE_DECL, DECL_NAME (decl), type);
3772 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
3773 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3774 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
3775 }
3776 }
3777
3778 return TEMPLATE_PARM_DESCENDANTS (index);
3779 }
3780
3781 /* Process information from new template parameter PARM and append it
3782 to the LIST being built. This new parameter is a non-type
3783 parameter iff IS_NON_TYPE is true. This new parameter is a
3784 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3785 is in PARM_LOC. */
3786
3787 tree
3788 process_template_parm (tree list, location_t parm_loc, tree parm,
3789 bool is_non_type, bool is_parameter_pack)
3790 {
3791 tree decl = 0;
3792 tree defval;
3793 int idx = 0;
3794
3795 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3796 defval = TREE_PURPOSE (parm);
3797
3798 if (list)
3799 {
3800 tree p = tree_last (list);
3801
3802 if (p && TREE_VALUE (p) != error_mark_node)
3803 {
3804 p = TREE_VALUE (p);
3805 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3806 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3807 else
3808 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3809 }
3810
3811 ++idx;
3812 }
3813
3814 if (is_non_type)
3815 {
3816 parm = TREE_VALUE (parm);
3817
3818 SET_DECL_TEMPLATE_PARM_P (parm);
3819
3820 if (TREE_TYPE (parm) != error_mark_node)
3821 {
3822 /* [temp.param]
3823
3824 The top-level cv-qualifiers on the template-parameter are
3825 ignored when determining its type. */
3826 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3827 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3828 TREE_TYPE (parm) = error_mark_node;
3829 else if (uses_parameter_packs (TREE_TYPE (parm))
3830 && !is_parameter_pack
3831 /* If we're in a nested template parameter list, the template
3832 template parameter could be a parameter pack. */
3833 && processing_template_parmlist == 1)
3834 {
3835 /* This template parameter is not a parameter pack, but it
3836 should be. Complain about "bare" parameter packs. */
3837 check_for_bare_parameter_packs (TREE_TYPE (parm));
3838
3839 /* Recover by calling this a parameter pack. */
3840 is_parameter_pack = true;
3841 }
3842 }
3843
3844 /* A template parameter is not modifiable. */
3845 TREE_CONSTANT (parm) = 1;
3846 TREE_READONLY (parm) = 1;
3847 decl = build_decl (parm_loc,
3848 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3849 TREE_CONSTANT (decl) = 1;
3850 TREE_READONLY (decl) = 1;
3851 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3852 = build_template_parm_index (idx, processing_template_decl,
3853 processing_template_decl,
3854 decl, TREE_TYPE (parm));
3855
3856 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3857 = is_parameter_pack;
3858 }
3859 else
3860 {
3861 tree t;
3862 parm = TREE_VALUE (TREE_VALUE (parm));
3863
3864 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3865 {
3866 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3867 /* This is for distinguishing between real templates and template
3868 template parameters */
3869 TREE_TYPE (parm) = t;
3870 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3871 decl = parm;
3872 }
3873 else
3874 {
3875 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3876 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3877 decl = build_decl (parm_loc,
3878 TYPE_DECL, parm, t);
3879 }
3880
3881 TYPE_NAME (t) = decl;
3882 TYPE_STUB_DECL (t) = decl;
3883 parm = decl;
3884 TEMPLATE_TYPE_PARM_INDEX (t)
3885 = build_template_parm_index (idx, processing_template_decl,
3886 processing_template_decl,
3887 decl, TREE_TYPE (parm));
3888 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3889 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3890 }
3891 DECL_ARTIFICIAL (decl) = 1;
3892 SET_DECL_TEMPLATE_PARM_P (decl);
3893 pushdecl (decl);
3894 parm = build_tree_list (defval, parm);
3895 return chainon (list, parm);
3896 }
3897
3898 /* The end of a template parameter list has been reached. Process the
3899 tree list into a parameter vector, converting each parameter into a more
3900 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3901 as PARM_DECLs. */
3902
3903 tree
3904 end_template_parm_list (tree parms)
3905 {
3906 int nparms;
3907 tree parm, next;
3908 tree saved_parmlist = make_tree_vec (list_length (parms));
3909
3910 /* Pop the dummy parameter level and add the real one. */
3911 current_template_parms = TREE_CHAIN (current_template_parms);
3912
3913 current_template_parms
3914 = tree_cons (size_int (processing_template_decl),
3915 saved_parmlist, current_template_parms);
3916
3917 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3918 {
3919 next = TREE_CHAIN (parm);
3920 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3921 TREE_CHAIN (parm) = NULL_TREE;
3922 }
3923
3924 --processing_template_parmlist;
3925
3926 return saved_parmlist;
3927 }
3928
3929 /* end_template_decl is called after a template declaration is seen. */
3930
3931 void
3932 end_template_decl (void)
3933 {
3934 reset_specialization ();
3935
3936 if (! processing_template_decl)
3937 return;
3938
3939 /* This matches the pushlevel in begin_template_parm_list. */
3940 finish_scope ();
3941
3942 --processing_template_decl;
3943 current_template_parms = TREE_CHAIN (current_template_parms);
3944 }
3945
3946 /* Takes a TREE_LIST representing a template parameter and convert it
3947 into an argument suitable to be passed to the type substitution
3948 functions. Note that If the TREE_LIST contains an error_mark
3949 node, the returned argument is error_mark_node. */
3950
3951 static tree
3952 template_parm_to_arg (tree t)
3953 {
3954
3955 if (t == NULL_TREE
3956 || TREE_CODE (t) != TREE_LIST)
3957 return t;
3958
3959 if (error_operand_p (TREE_VALUE (t)))
3960 return error_mark_node;
3961
3962 t = TREE_VALUE (t);
3963
3964 if (TREE_CODE (t) == TYPE_DECL
3965 || TREE_CODE (t) == TEMPLATE_DECL)
3966 {
3967 t = TREE_TYPE (t);
3968
3969 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3970 {
3971 /* Turn this argument into a TYPE_ARGUMENT_PACK
3972 with a single element, which expands T. */
3973 tree vec = make_tree_vec (1);
3974 #ifdef ENABLE_CHECKING
3975 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3976 (vec, TREE_VEC_LENGTH (vec));
3977 #endif
3978 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3979
3980 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3981 SET_ARGUMENT_PACK_ARGS (t, vec);
3982 }
3983 }
3984 else
3985 {
3986 t = DECL_INITIAL (t);
3987
3988 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3989 {
3990 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3991 with a single element, which expands T. */
3992 tree vec = make_tree_vec (1);
3993 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3994 #ifdef ENABLE_CHECKING
3995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3996 (vec, TREE_VEC_LENGTH (vec));
3997 #endif
3998 t = convert_from_reference (t);
3999 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4000
4001 t = make_node (NONTYPE_ARGUMENT_PACK);
4002 SET_ARGUMENT_PACK_ARGS (t, vec);
4003 TREE_TYPE (t) = type;
4004 }
4005 else
4006 t = convert_from_reference (t);
4007 }
4008 return t;
4009 }
4010
4011 /* Given a set of template parameters, return them as a set of template
4012 arguments. The template parameters are represented as a TREE_VEC, in
4013 the form documented in cp-tree.h for template arguments. */
4014
4015 static tree
4016 template_parms_to_args (tree parms)
4017 {
4018 tree header;
4019 tree args = NULL_TREE;
4020 int length = TMPL_PARMS_DEPTH (parms);
4021 int l = length;
4022
4023 /* If there is only one level of template parameters, we do not
4024 create a TREE_VEC of TREE_VECs. Instead, we return a single
4025 TREE_VEC containing the arguments. */
4026 if (length > 1)
4027 args = make_tree_vec (length);
4028
4029 for (header = parms; header; header = TREE_CHAIN (header))
4030 {
4031 tree a = copy_node (TREE_VALUE (header));
4032 int i;
4033
4034 TREE_TYPE (a) = NULL_TREE;
4035 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4036 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4037
4038 #ifdef ENABLE_CHECKING
4039 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4040 #endif
4041
4042 if (length > 1)
4043 TREE_VEC_ELT (args, --l) = a;
4044 else
4045 args = a;
4046 }
4047
4048 return args;
4049 }
4050
4051 /* Within the declaration of a template, return the currently active
4052 template parameters as an argument TREE_VEC. */
4053
4054 static tree
4055 current_template_args (void)
4056 {
4057 return template_parms_to_args (current_template_parms);
4058 }
4059
4060 /* Update the declared TYPE by doing any lookups which were thought to be
4061 dependent, but are not now that we know the SCOPE of the declarator. */
4062
4063 tree
4064 maybe_update_decl_type (tree orig_type, tree scope)
4065 {
4066 tree type = orig_type;
4067
4068 if (type == NULL_TREE)
4069 return type;
4070
4071 if (TREE_CODE (orig_type) == TYPE_DECL)
4072 type = TREE_TYPE (type);
4073
4074 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4075 && dependent_type_p (type)
4076 /* Don't bother building up the args in this case. */
4077 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4078 {
4079 /* tsubst in the args corresponding to the template parameters,
4080 including auto if present. Most things will be unchanged, but
4081 make_typename_type and tsubst_qualified_id will resolve
4082 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4083 tree args = current_template_args ();
4084 tree auto_node = type_uses_auto (type);
4085 tree pushed;
4086 if (auto_node)
4087 {
4088 tree auto_vec = make_tree_vec (1);
4089 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4090 args = add_to_template_args (args, auto_vec);
4091 }
4092 pushed = push_scope (scope);
4093 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4094 if (pushed)
4095 pop_scope (scope);
4096 }
4097
4098 if (type == error_mark_node)
4099 return orig_type;
4100
4101 if (TREE_CODE (orig_type) == TYPE_DECL)
4102 {
4103 if (same_type_p (type, TREE_TYPE (orig_type)))
4104 type = orig_type;
4105 else
4106 type = TYPE_NAME (type);
4107 }
4108 return type;
4109 }
4110
4111 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4112 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4113 a member template. Used by push_template_decl below. */
4114
4115 static tree
4116 build_template_decl (tree decl, tree parms, bool member_template_p)
4117 {
4118 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4119 DECL_TEMPLATE_PARMS (tmpl) = parms;
4120 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4121 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4122 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4123
4124 return tmpl;
4125 }
4126
4127 struct template_parm_data
4128 {
4129 /* The level of the template parameters we are currently
4130 processing. */
4131 int level;
4132
4133 /* The index of the specialization argument we are currently
4134 processing. */
4135 int current_arg;
4136
4137 /* An array whose size is the number of template parameters. The
4138 elements are nonzero if the parameter has been used in any one
4139 of the arguments processed so far. */
4140 int* parms;
4141
4142 /* An array whose size is the number of template arguments. The
4143 elements are nonzero if the argument makes use of template
4144 parameters of this level. */
4145 int* arg_uses_template_parms;
4146 };
4147
4148 /* Subroutine of push_template_decl used to see if each template
4149 parameter in a partial specialization is used in the explicit
4150 argument list. If T is of the LEVEL given in DATA (which is
4151 treated as a template_parm_data*), then DATA->PARMS is marked
4152 appropriately. */
4153
4154 static int
4155 mark_template_parm (tree t, void* data)
4156 {
4157 int level;
4158 int idx;
4159 struct template_parm_data* tpd = (struct template_parm_data*) data;
4160
4161 template_parm_level_and_index (t, &level, &idx);
4162
4163 if (level == tpd->level)
4164 {
4165 tpd->parms[idx] = 1;
4166 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4167 }
4168
4169 /* Return zero so that for_each_template_parm will continue the
4170 traversal of the tree; we want to mark *every* template parm. */
4171 return 0;
4172 }
4173
4174 /* Process the partial specialization DECL. */
4175
4176 static tree
4177 process_partial_specialization (tree decl)
4178 {
4179 tree type = TREE_TYPE (decl);
4180 tree tinfo = get_template_info (decl);
4181 tree maintmpl = TI_TEMPLATE (tinfo);
4182 tree specargs = TI_ARGS (tinfo);
4183 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4184 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4185 tree inner_parms;
4186 tree inst;
4187 int nargs = TREE_VEC_LENGTH (inner_args);
4188 int ntparms;
4189 int i;
4190 bool did_error_intro = false;
4191 struct template_parm_data tpd;
4192 struct template_parm_data tpd2;
4193
4194 gcc_assert (current_template_parms);
4195
4196 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4197 ntparms = TREE_VEC_LENGTH (inner_parms);
4198
4199 /* We check that each of the template parameters given in the
4200 partial specialization is used in the argument list to the
4201 specialization. For example:
4202
4203 template <class T> struct S;
4204 template <class T> struct S<T*>;
4205
4206 The second declaration is OK because `T*' uses the template
4207 parameter T, whereas
4208
4209 template <class T> struct S<int>;
4210
4211 is no good. Even trickier is:
4212
4213 template <class T>
4214 struct S1
4215 {
4216 template <class U>
4217 struct S2;
4218 template <class U>
4219 struct S2<T>;
4220 };
4221
4222 The S2<T> declaration is actually invalid; it is a
4223 full-specialization. Of course,
4224
4225 template <class U>
4226 struct S2<T (*)(U)>;
4227
4228 or some such would have been OK. */
4229 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4230 tpd.parms = XALLOCAVEC (int, ntparms);
4231 memset (tpd.parms, 0, sizeof (int) * ntparms);
4232
4233 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4234 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4235 for (i = 0; i < nargs; ++i)
4236 {
4237 tpd.current_arg = i;
4238 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4239 &mark_template_parm,
4240 &tpd,
4241 NULL,
4242 /*include_nondeduced_p=*/false);
4243 }
4244 for (i = 0; i < ntparms; ++i)
4245 if (tpd.parms[i] == 0)
4246 {
4247 /* One of the template parms was not used in a deduced context in the
4248 specialization. */
4249 if (!did_error_intro)
4250 {
4251 error ("template parameters not deducible in "
4252 "partial specialization:");
4253 did_error_intro = true;
4254 }
4255
4256 inform (input_location, " %qD",
4257 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4258 }
4259
4260 if (did_error_intro)
4261 return error_mark_node;
4262
4263 /* [temp.class.spec]
4264
4265 The argument list of the specialization shall not be identical to
4266 the implicit argument list of the primary template. */
4267 tree main_args
4268 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4269 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)))
4270 error ("partial specialization %qD does not specialize "
4271 "any template arguments", decl);
4272
4273 /* A partial specialization that replaces multiple parameters of the
4274 primary template with a pack expansion is less specialized for those
4275 parameters. */
4276 if (nargs < DECL_NTPARMS (maintmpl))
4277 {
4278 error ("partial specialization is not more specialized than the "
4279 "primary template because it replaces multiple parameters "
4280 "with a pack expansion");
4281 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4282 return decl;
4283 }
4284
4285 /* [temp.class.spec]
4286
4287 A partially specialized non-type argument expression shall not
4288 involve template parameters of the partial specialization except
4289 when the argument expression is a simple identifier.
4290
4291 The type of a template parameter corresponding to a specialized
4292 non-type argument shall not be dependent on a parameter of the
4293 specialization.
4294
4295 Also, we verify that pack expansions only occur at the
4296 end of the argument list. */
4297 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4298 tpd2.parms = 0;
4299 for (i = 0; i < nargs; ++i)
4300 {
4301 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4302 tree arg = TREE_VEC_ELT (inner_args, i);
4303 tree packed_args = NULL_TREE;
4304 int j, len = 1;
4305
4306 if (ARGUMENT_PACK_P (arg))
4307 {
4308 /* Extract the arguments from the argument pack. We'll be
4309 iterating over these in the following loop. */
4310 packed_args = ARGUMENT_PACK_ARGS (arg);
4311 len = TREE_VEC_LENGTH (packed_args);
4312 }
4313
4314 for (j = 0; j < len; j++)
4315 {
4316 if (packed_args)
4317 /* Get the Jth argument in the parameter pack. */
4318 arg = TREE_VEC_ELT (packed_args, j);
4319
4320 if (PACK_EXPANSION_P (arg))
4321 {
4322 /* Pack expansions must come at the end of the
4323 argument list. */
4324 if ((packed_args && j < len - 1)
4325 || (!packed_args && i < nargs - 1))
4326 {
4327 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4328 error ("parameter pack argument %qE must be at the "
4329 "end of the template argument list", arg);
4330 else
4331 error ("parameter pack argument %qT must be at the "
4332 "end of the template argument list", arg);
4333 }
4334 }
4335
4336 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4337 /* We only care about the pattern. */
4338 arg = PACK_EXPANSION_PATTERN (arg);
4339
4340 if (/* These first two lines are the `non-type' bit. */
4341 !TYPE_P (arg)
4342 && TREE_CODE (arg) != TEMPLATE_DECL
4343 /* This next two lines are the `argument expression is not just a
4344 simple identifier' condition and also the `specialized
4345 non-type argument' bit. */
4346 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4347 && !(REFERENCE_REF_P (arg)
4348 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4349 {
4350 if ((!packed_args && tpd.arg_uses_template_parms[i])
4351 || (packed_args && uses_template_parms (arg)))
4352 error ("template argument %qE involves template parameter(s)",
4353 arg);
4354 else
4355 {
4356 /* Look at the corresponding template parameter,
4357 marking which template parameters its type depends
4358 upon. */
4359 tree type = TREE_TYPE (parm);
4360
4361 if (!tpd2.parms)
4362 {
4363 /* We haven't yet initialized TPD2. Do so now. */
4364 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4365 /* The number of parameters here is the number in the
4366 main template, which, as checked in the assertion
4367 above, is NARGS. */
4368 tpd2.parms = XALLOCAVEC (int, nargs);
4369 tpd2.level =
4370 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4371 }
4372
4373 /* Mark the template parameters. But this time, we're
4374 looking for the template parameters of the main
4375 template, not in the specialization. */
4376 tpd2.current_arg = i;
4377 tpd2.arg_uses_template_parms[i] = 0;
4378 memset (tpd2.parms, 0, sizeof (int) * nargs);
4379 for_each_template_parm (type,
4380 &mark_template_parm,
4381 &tpd2,
4382 NULL,
4383 /*include_nondeduced_p=*/false);
4384
4385 if (tpd2.arg_uses_template_parms [i])
4386 {
4387 /* The type depended on some template parameters.
4388 If they are fully specialized in the
4389 specialization, that's OK. */
4390 int j;
4391 int count = 0;
4392 for (j = 0; j < nargs; ++j)
4393 if (tpd2.parms[j] != 0
4394 && tpd.arg_uses_template_parms [j])
4395 ++count;
4396 if (count != 0)
4397 error_n (input_location, count,
4398 "type %qT of template argument %qE depends "
4399 "on a template parameter",
4400 "type %qT of template argument %qE depends "
4401 "on template parameters",
4402 type,
4403 arg);
4404 }
4405 }
4406 }
4407 }
4408 }
4409
4410 /* We should only get here once. */
4411 if (TREE_CODE (decl) == TYPE_DECL)
4412 gcc_assert (!COMPLETE_TYPE_P (type));
4413
4414 tree tmpl = build_template_decl (decl, current_template_parms,
4415 DECL_MEMBER_TEMPLATE_P (maintmpl));
4416 TREE_TYPE (tmpl) = type;
4417 DECL_TEMPLATE_RESULT (tmpl) = decl;
4418 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4419 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4420 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4421
4422 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4423 = tree_cons (specargs, tmpl,
4424 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4425 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4426
4427 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4428 inst = TREE_CHAIN (inst))
4429 {
4430 tree instance = TREE_VALUE (inst);
4431 if (TYPE_P (instance)
4432 ? (COMPLETE_TYPE_P (instance)
4433 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4434 : DECL_TEMPLATE_INSTANTIATION (instance))
4435 {
4436 tree spec = most_specialized_partial_spec (instance, tf_none);
4437 if (spec && TREE_VALUE (spec) == tmpl)
4438 {
4439 tree inst_decl = (DECL_P (instance)
4440 ? instance : TYPE_NAME (instance));
4441 permerror (input_location,
4442 "partial specialization of %qD after instantiation "
4443 "of %qD", decl, inst_decl);
4444 }
4445 }
4446 }
4447
4448 return decl;
4449 }
4450
4451 /* PARM is a template parameter of some form; return the corresponding
4452 TEMPLATE_PARM_INDEX. */
4453
4454 static tree
4455 get_template_parm_index (tree parm)
4456 {
4457 if (TREE_CODE (parm) == PARM_DECL
4458 || TREE_CODE (parm) == CONST_DECL)
4459 parm = DECL_INITIAL (parm);
4460 else if (TREE_CODE (parm) == TYPE_DECL
4461 || TREE_CODE (parm) == TEMPLATE_DECL)
4462 parm = TREE_TYPE (parm);
4463 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4464 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4465 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4466 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4467 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4468 return parm;
4469 }
4470
4471 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4472 parameter packs used by the template parameter PARM. */
4473
4474 static void
4475 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4476 {
4477 /* A type parm can't refer to another parm. */
4478 if (TREE_CODE (parm) == TYPE_DECL)
4479 return;
4480 else if (TREE_CODE (parm) == PARM_DECL)
4481 {
4482 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4483 ppd, ppd->visited);
4484 return;
4485 }
4486
4487 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4488
4489 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4490 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4491 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4492 }
4493
4494 /* PARM is a template parameter pack. Return any parameter packs used in
4495 its type or the type of any of its template parameters. If there are
4496 any such packs, it will be instantiated into a fixed template parameter
4497 list by partial instantiation rather than be fully deduced. */
4498
4499 tree
4500 fixed_parameter_pack_p (tree parm)
4501 {
4502 /* This can only be true in a member template. */
4503 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4504 return NULL_TREE;
4505 /* This can only be true for a parameter pack. */
4506 if (!template_parameter_pack_p (parm))
4507 return NULL_TREE;
4508 /* A type parm can't refer to another parm. */
4509 if (TREE_CODE (parm) == TYPE_DECL)
4510 return NULL_TREE;
4511
4512 tree parameter_packs = NULL_TREE;
4513 struct find_parameter_pack_data ppd;
4514 ppd.parameter_packs = &parameter_packs;
4515 ppd.visited = new hash_set<tree>;
4516
4517 fixed_parameter_pack_p_1 (parm, &ppd);
4518
4519 delete ppd.visited;
4520 return parameter_packs;
4521 }
4522
4523 /* Check that a template declaration's use of default arguments and
4524 parameter packs is not invalid. Here, PARMS are the template
4525 parameters. IS_PRIMARY is true if DECL is the thing declared by
4526 a primary template. IS_PARTIAL is true if DECL is a partial
4527 specialization.
4528
4529 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4530 declaration (but not a definition); 1 indicates a declaration, 2
4531 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4532 emitted for extraneous default arguments.
4533
4534 Returns TRUE if there were no errors found, FALSE otherwise. */
4535
4536 bool
4537 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4538 bool is_partial, int is_friend_decl)
4539 {
4540 const char *msg;
4541 int last_level_to_check;
4542 tree parm_level;
4543 bool no_errors = true;
4544
4545 /* [temp.param]
4546
4547 A default template-argument shall not be specified in a
4548 function template declaration or a function template definition, nor
4549 in the template-parameter-list of the definition of a member of a
4550 class template. */
4551
4552 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4553 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4554 /* You can't have a function template declaration in a local
4555 scope, nor you can you define a member of a class template in a
4556 local scope. */
4557 return true;
4558
4559 if ((TREE_CODE (decl) == TYPE_DECL
4560 && TREE_TYPE (decl)
4561 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4562 || (TREE_CODE (decl) == FUNCTION_DECL
4563 && LAMBDA_FUNCTION_P (decl)))
4564 /* A lambda doesn't have an explicit declaration; don't complain
4565 about the parms of the enclosing class. */
4566 return true;
4567
4568 if (current_class_type
4569 && !TYPE_BEING_DEFINED (current_class_type)
4570 && DECL_LANG_SPECIFIC (decl)
4571 && DECL_DECLARES_FUNCTION_P (decl)
4572 /* If this is either a friend defined in the scope of the class
4573 or a member function. */
4574 && (DECL_FUNCTION_MEMBER_P (decl)
4575 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4576 : DECL_FRIEND_CONTEXT (decl)
4577 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4578 : false)
4579 /* And, if it was a member function, it really was defined in
4580 the scope of the class. */
4581 && (!DECL_FUNCTION_MEMBER_P (decl)
4582 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4583 /* We already checked these parameters when the template was
4584 declared, so there's no need to do it again now. This function
4585 was defined in class scope, but we're processing its body now
4586 that the class is complete. */
4587 return true;
4588
4589 /* Core issue 226 (C++0x only): the following only applies to class
4590 templates. */
4591 if (is_primary
4592 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4593 {
4594 /* [temp.param]
4595
4596 If a template-parameter has a default template-argument, all
4597 subsequent template-parameters shall have a default
4598 template-argument supplied. */
4599 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4600 {
4601 tree inner_parms = TREE_VALUE (parm_level);
4602 int ntparms = TREE_VEC_LENGTH (inner_parms);
4603 int seen_def_arg_p = 0;
4604 int i;
4605
4606 for (i = 0; i < ntparms; ++i)
4607 {
4608 tree parm = TREE_VEC_ELT (inner_parms, i);
4609
4610 if (parm == error_mark_node)
4611 continue;
4612
4613 if (TREE_PURPOSE (parm))
4614 seen_def_arg_p = 1;
4615 else if (seen_def_arg_p
4616 && !template_parameter_pack_p (TREE_VALUE (parm)))
4617 {
4618 error ("no default argument for %qD", TREE_VALUE (parm));
4619 /* For better subsequent error-recovery, we indicate that
4620 there should have been a default argument. */
4621 TREE_PURPOSE (parm) = error_mark_node;
4622 no_errors = false;
4623 }
4624 else if (!is_partial
4625 && !is_friend_decl
4626 /* Don't complain about an enclosing partial
4627 specialization. */
4628 && parm_level == parms
4629 && TREE_CODE (decl) == TYPE_DECL
4630 && i < ntparms - 1
4631 && template_parameter_pack_p (TREE_VALUE (parm))
4632 /* A fixed parameter pack will be partially
4633 instantiated into a fixed length list. */
4634 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4635 {
4636 /* A primary class template can only have one
4637 parameter pack, at the end of the template
4638 parameter list. */
4639
4640 error ("parameter pack %q+D must be at the end of the"
4641 " template parameter list", TREE_VALUE (parm));
4642
4643 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4644 = error_mark_node;
4645 no_errors = false;
4646 }
4647 }
4648 }
4649 }
4650
4651 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4652 || is_partial
4653 || !is_primary
4654 || is_friend_decl)
4655 /* For an ordinary class template, default template arguments are
4656 allowed at the innermost level, e.g.:
4657 template <class T = int>
4658 struct S {};
4659 but, in a partial specialization, they're not allowed even
4660 there, as we have in [temp.class.spec]:
4661
4662 The template parameter list of a specialization shall not
4663 contain default template argument values.
4664
4665 So, for a partial specialization, or for a function template
4666 (in C++98/C++03), we look at all of them. */
4667 ;
4668 else
4669 /* But, for a primary class template that is not a partial
4670 specialization we look at all template parameters except the
4671 innermost ones. */
4672 parms = TREE_CHAIN (parms);
4673
4674 /* Figure out what error message to issue. */
4675 if (is_friend_decl == 2)
4676 msg = G_("default template arguments may not be used in function template "
4677 "friend re-declaration");
4678 else if (is_friend_decl)
4679 msg = G_("default template arguments may not be used in function template "
4680 "friend declarations");
4681 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4682 msg = G_("default template arguments may not be used in function templates "
4683 "without -std=c++11 or -std=gnu++11");
4684 else if (is_partial)
4685 msg = G_("default template arguments may not be used in "
4686 "partial specializations");
4687 else
4688 msg = G_("default argument for template parameter for class enclosing %qD");
4689
4690 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4691 /* If we're inside a class definition, there's no need to
4692 examine the parameters to the class itself. On the one
4693 hand, they will be checked when the class is defined, and,
4694 on the other, default arguments are valid in things like:
4695 template <class T = double>
4696 struct S { template <class U> void f(U); };
4697 Here the default argument for `S' has no bearing on the
4698 declaration of `f'. */
4699 last_level_to_check = template_class_depth (current_class_type) + 1;
4700 else
4701 /* Check everything. */
4702 last_level_to_check = 0;
4703
4704 for (parm_level = parms;
4705 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4706 parm_level = TREE_CHAIN (parm_level))
4707 {
4708 tree inner_parms = TREE_VALUE (parm_level);
4709 int i;
4710 int ntparms;
4711
4712 ntparms = TREE_VEC_LENGTH (inner_parms);
4713 for (i = 0; i < ntparms; ++i)
4714 {
4715 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4716 continue;
4717
4718 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4719 {
4720 if (msg)
4721 {
4722 no_errors = false;
4723 if (is_friend_decl == 2)
4724 return no_errors;
4725
4726 error (msg, decl);
4727 msg = 0;
4728 }
4729
4730 /* Clear out the default argument so that we are not
4731 confused later. */
4732 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4733 }
4734 }
4735
4736 /* At this point, if we're still interested in issuing messages,
4737 they must apply to classes surrounding the object declared. */
4738 if (msg)
4739 msg = G_("default argument for template parameter for class "
4740 "enclosing %qD");
4741 }
4742
4743 return no_errors;
4744 }
4745
4746 /* Worker for push_template_decl_real, called via
4747 for_each_template_parm. DATA is really an int, indicating the
4748 level of the parameters we are interested in. If T is a template
4749 parameter of that level, return nonzero. */
4750
4751 static int
4752 template_parm_this_level_p (tree t, void* data)
4753 {
4754 int this_level = *(int *)data;
4755 int level;
4756
4757 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4758 level = TEMPLATE_PARM_LEVEL (t);
4759 else
4760 level = TEMPLATE_TYPE_LEVEL (t);
4761 return level == this_level;
4762 }
4763
4764 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4765 parameters given by current_template_args, or reuses a
4766 previously existing one, if appropriate. Returns the DECL, or an
4767 equivalent one, if it is replaced via a call to duplicate_decls.
4768
4769 If IS_FRIEND is true, DECL is a friend declaration. */
4770
4771 tree
4772 push_template_decl_real (tree decl, bool is_friend)
4773 {
4774 tree tmpl;
4775 tree args;
4776 tree info;
4777 tree ctx;
4778 bool is_primary;
4779 bool is_partial;
4780 int new_template_p = 0;
4781 /* True if the template is a member template, in the sense of
4782 [temp.mem]. */
4783 bool member_template_p = false;
4784
4785 if (decl == error_mark_node || !current_template_parms)
4786 return error_mark_node;
4787
4788 /* See if this is a partial specialization. */
4789 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
4790 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4791 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
4792 || (VAR_P (decl)
4793 && DECL_LANG_SPECIFIC (decl)
4794 && DECL_TEMPLATE_SPECIALIZATION (decl)
4795 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
4796
4797 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4798 is_friend = true;
4799
4800 if (is_friend)
4801 /* For a friend, we want the context of the friend function, not
4802 the type of which it is a friend. */
4803 ctx = CP_DECL_CONTEXT (decl);
4804 else if (CP_DECL_CONTEXT (decl)
4805 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4806 /* In the case of a virtual function, we want the class in which
4807 it is defined. */
4808 ctx = CP_DECL_CONTEXT (decl);
4809 else
4810 /* Otherwise, if we're currently defining some class, the DECL
4811 is assumed to be a member of the class. */
4812 ctx = current_scope ();
4813
4814 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4815 ctx = NULL_TREE;
4816
4817 if (!DECL_CONTEXT (decl))
4818 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4819
4820 /* See if this is a primary template. */
4821 if (is_friend && ctx
4822 && uses_template_parms_level (ctx, processing_template_decl))
4823 /* A friend template that specifies a class context, i.e.
4824 template <typename T> friend void A<T>::f();
4825 is not primary. */
4826 is_primary = false;
4827 else if (TREE_CODE (decl) == TYPE_DECL
4828 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4829 is_primary = false;
4830 else
4831 is_primary = template_parm_scope_p ();
4832
4833 if (is_primary)
4834 {
4835 if (DECL_CLASS_SCOPE_P (decl))
4836 member_template_p = true;
4837 if (TREE_CODE (decl) == TYPE_DECL
4838 && anon_aggrname_p (DECL_NAME (decl)))
4839 {
4840 error ("template class without a name");
4841 return error_mark_node;
4842 }
4843 else if (TREE_CODE (decl) == FUNCTION_DECL)
4844 {
4845 if (member_template_p)
4846 {
4847 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
4848 error ("member template %qD may not have virt-specifiers", decl);
4849 }
4850 if (DECL_DESTRUCTOR_P (decl))
4851 {
4852 /* [temp.mem]
4853
4854 A destructor shall not be a member template. */
4855 error ("destructor %qD declared as member template", decl);
4856 return error_mark_node;
4857 }
4858 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4859 && (!prototype_p (TREE_TYPE (decl))
4860 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4861 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4862 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4863 == void_list_node)))
4864 {
4865 /* [basic.stc.dynamic.allocation]
4866
4867 An allocation function can be a function
4868 template. ... Template allocation functions shall
4869 have two or more parameters. */
4870 error ("invalid template declaration of %qD", decl);
4871 return error_mark_node;
4872 }
4873 }
4874 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4875 && CLASS_TYPE_P (TREE_TYPE (decl)))
4876 /* OK */;
4877 else if (TREE_CODE (decl) == TYPE_DECL
4878 && TYPE_DECL_ALIAS_P (decl))
4879 /* alias-declaration */
4880 gcc_assert (!DECL_ARTIFICIAL (decl));
4881 else if (VAR_P (decl))
4882 /* C++14 variable template. */;
4883 else
4884 {
4885 error ("template declaration of %q#D", decl);
4886 return error_mark_node;
4887 }
4888 }
4889
4890 /* Check to see that the rules regarding the use of default
4891 arguments are not being violated. */
4892 check_default_tmpl_args (decl, current_template_parms,
4893 is_primary, is_partial, /*is_friend_decl=*/0);
4894
4895 /* Ensure that there are no parameter packs in the type of this
4896 declaration that have not been expanded. */
4897 if (TREE_CODE (decl) == FUNCTION_DECL)
4898 {
4899 /* Check each of the arguments individually to see if there are
4900 any bare parameter packs. */
4901 tree type = TREE_TYPE (decl);
4902 tree arg = DECL_ARGUMENTS (decl);
4903 tree argtype = TYPE_ARG_TYPES (type);
4904
4905 while (arg && argtype)
4906 {
4907 if (!DECL_PACK_P (arg)
4908 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4909 {
4910 /* This is a PARM_DECL that contains unexpanded parameter
4911 packs. We have already complained about this in the
4912 check_for_bare_parameter_packs call, so just replace
4913 these types with ERROR_MARK_NODE. */
4914 TREE_TYPE (arg) = error_mark_node;
4915 TREE_VALUE (argtype) = error_mark_node;
4916 }
4917
4918 arg = DECL_CHAIN (arg);
4919 argtype = TREE_CHAIN (argtype);
4920 }
4921
4922 /* Check for bare parameter packs in the return type and the
4923 exception specifiers. */
4924 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4925 /* Errors were already issued, set return type to int
4926 as the frontend doesn't expect error_mark_node as
4927 the return type. */
4928 TREE_TYPE (type) = integer_type_node;
4929 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4930 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4931 }
4932 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4933 && TYPE_DECL_ALIAS_P (decl))
4934 ? DECL_ORIGINAL_TYPE (decl)
4935 : TREE_TYPE (decl)))
4936 {
4937 TREE_TYPE (decl) = error_mark_node;
4938 return error_mark_node;
4939 }
4940
4941 if (is_partial)
4942 return process_partial_specialization (decl);
4943
4944 args = current_template_args ();
4945
4946 if (!ctx
4947 || TREE_CODE (ctx) == FUNCTION_DECL
4948 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4949 || (TREE_CODE (decl) == TYPE_DECL
4950 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4951 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4952 {
4953 if (DECL_LANG_SPECIFIC (decl)
4954 && DECL_TEMPLATE_INFO (decl)
4955 && DECL_TI_TEMPLATE (decl))
4956 tmpl = DECL_TI_TEMPLATE (decl);
4957 /* If DECL is a TYPE_DECL for a class-template, then there won't
4958 be DECL_LANG_SPECIFIC. The information equivalent to
4959 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4960 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4961 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4962 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4963 {
4964 /* Since a template declaration already existed for this
4965 class-type, we must be redeclaring it here. Make sure
4966 that the redeclaration is valid. */
4967 redeclare_class_template (TREE_TYPE (decl),
4968 current_template_parms);
4969 /* We don't need to create a new TEMPLATE_DECL; just use the
4970 one we already had. */
4971 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4972 }
4973 else
4974 {
4975 tmpl = build_template_decl (decl, current_template_parms,
4976 member_template_p);
4977 new_template_p = 1;
4978
4979 if (DECL_LANG_SPECIFIC (decl)
4980 && DECL_TEMPLATE_SPECIALIZATION (decl))
4981 {
4982 /* A specialization of a member template of a template
4983 class. */
4984 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4985 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4986 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4987 }
4988 }
4989 }
4990 else
4991 {
4992 tree a, t, current, parms;
4993 int i;
4994 tree tinfo = get_template_info (decl);
4995
4996 if (!tinfo)
4997 {
4998 error ("template definition of non-template %q#D", decl);
4999 return error_mark_node;
5000 }
5001
5002 tmpl = TI_TEMPLATE (tinfo);
5003
5004 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5005 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5006 && DECL_TEMPLATE_SPECIALIZATION (decl)
5007 && DECL_MEMBER_TEMPLATE_P (tmpl))
5008 {
5009 tree new_tmpl;
5010
5011 /* The declaration is a specialization of a member
5012 template, declared outside the class. Therefore, the
5013 innermost template arguments will be NULL, so we
5014 replace them with the arguments determined by the
5015 earlier call to check_explicit_specialization. */
5016 args = DECL_TI_ARGS (decl);
5017
5018 new_tmpl
5019 = build_template_decl (decl, current_template_parms,
5020 member_template_p);
5021 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5022 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5023 DECL_TI_TEMPLATE (decl) = new_tmpl;
5024 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5025 DECL_TEMPLATE_INFO (new_tmpl)
5026 = build_template_info (tmpl, args);
5027
5028 register_specialization (new_tmpl,
5029 most_general_template (tmpl),
5030 args,
5031 is_friend, 0);
5032 return decl;
5033 }
5034
5035 /* Make sure the template headers we got make sense. */
5036
5037 parms = DECL_TEMPLATE_PARMS (tmpl);
5038 i = TMPL_PARMS_DEPTH (parms);
5039 if (TMPL_ARGS_DEPTH (args) != i)
5040 {
5041 error ("expected %d levels of template parms for %q#D, got %d",
5042 i, decl, TMPL_ARGS_DEPTH (args));
5043 DECL_INTERFACE_KNOWN (decl) = 1;
5044 return error_mark_node;
5045 }
5046 else
5047 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5048 {
5049 a = TMPL_ARGS_LEVEL (args, i);
5050 t = INNERMOST_TEMPLATE_PARMS (parms);
5051
5052 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5053 {
5054 if (current == decl)
5055 error ("got %d template parameters for %q#D",
5056 TREE_VEC_LENGTH (a), decl);
5057 else
5058 error ("got %d template parameters for %q#T",
5059 TREE_VEC_LENGTH (a), current);
5060 error (" but %d required", TREE_VEC_LENGTH (t));
5061 /* Avoid crash in import_export_decl. */
5062 DECL_INTERFACE_KNOWN (decl) = 1;
5063 return error_mark_node;
5064 }
5065
5066 if (current == decl)
5067 current = ctx;
5068 else if (current == NULL_TREE)
5069 /* Can happen in erroneous input. */
5070 break;
5071 else
5072 current = get_containing_scope (current);
5073 }
5074
5075 /* Check that the parms are used in the appropriate qualifying scopes
5076 in the declarator. */
5077 if (!comp_template_args
5078 (TI_ARGS (tinfo),
5079 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5080 {
5081 error ("\
5082 template arguments to %qD do not match original template %qD",
5083 decl, DECL_TEMPLATE_RESULT (tmpl));
5084 if (!uses_template_parms (TI_ARGS (tinfo)))
5085 inform (input_location, "use template<> for an explicit specialization");
5086 /* Avoid crash in import_export_decl. */
5087 DECL_INTERFACE_KNOWN (decl) = 1;
5088 return error_mark_node;
5089 }
5090 }
5091
5092 DECL_TEMPLATE_RESULT (tmpl) = decl;
5093 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5094
5095 /* Push template declarations for global functions and types. Note
5096 that we do not try to push a global template friend declared in a
5097 template class; such a thing may well depend on the template
5098 parameters of the class. */
5099 if (new_template_p && !ctx
5100 && !(is_friend && template_class_depth (current_class_type) > 0))
5101 {
5102 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5103 if (tmpl == error_mark_node)
5104 return error_mark_node;
5105
5106 /* Hide template friend classes that haven't been declared yet. */
5107 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5108 {
5109 DECL_ANTICIPATED (tmpl) = 1;
5110 DECL_FRIEND_P (tmpl) = 1;
5111 }
5112 }
5113
5114 if (is_primary)
5115 {
5116 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5117 int i;
5118
5119 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5120 if (DECL_CONV_FN_P (tmpl))
5121 {
5122 int depth = TMPL_PARMS_DEPTH (parms);
5123
5124 /* It is a conversion operator. See if the type converted to
5125 depends on innermost template operands. */
5126
5127 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5128 depth))
5129 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5130 }
5131
5132 /* Give template template parms a DECL_CONTEXT of the template
5133 for which they are a parameter. */
5134 parms = INNERMOST_TEMPLATE_PARMS (parms);
5135 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5136 {
5137 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5138 if (TREE_CODE (parm) == TEMPLATE_DECL)
5139 DECL_CONTEXT (parm) = tmpl;
5140 }
5141
5142 if (TREE_CODE (decl) == TYPE_DECL
5143 && TYPE_DECL_ALIAS_P (decl)
5144 && complex_alias_template_p (tmpl))
5145 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5146 }
5147
5148 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5149 back to its most general template. If TMPL is a specialization,
5150 ARGS may only have the innermost set of arguments. Add the missing
5151 argument levels if necessary. */
5152 if (DECL_TEMPLATE_INFO (tmpl))
5153 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5154
5155 info = build_template_info (tmpl, args);
5156
5157 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5158 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5159 else
5160 {
5161 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5162 retrofit_lang_decl (decl);
5163 if (DECL_LANG_SPECIFIC (decl))
5164 DECL_TEMPLATE_INFO (decl) = info;
5165 }
5166
5167 if (flag_implicit_templates
5168 && !is_friend
5169 && TREE_PUBLIC (decl)
5170 && VAR_OR_FUNCTION_DECL_P (decl))
5171 /* Set DECL_COMDAT on template instantiations; if we force
5172 them to be emitted by explicit instantiation or -frepo,
5173 mark_needed will tell cgraph to do the right thing. */
5174 DECL_COMDAT (decl) = true;
5175
5176 return DECL_TEMPLATE_RESULT (tmpl);
5177 }
5178
5179 tree
5180 push_template_decl (tree decl)
5181 {
5182 return push_template_decl_real (decl, false);
5183 }
5184
5185 /* FN is an inheriting constructor that inherits from the constructor
5186 template INHERITED; turn FN into a constructor template with a matching
5187 template header. */
5188
5189 tree
5190 add_inherited_template_parms (tree fn, tree inherited)
5191 {
5192 tree inner_parms
5193 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5194 inner_parms = copy_node (inner_parms);
5195 tree parms
5196 = tree_cons (size_int (processing_template_decl + 1),
5197 inner_parms, current_template_parms);
5198 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5199 tree args = template_parms_to_args (parms);
5200 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5201 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5202 DECL_TEMPLATE_RESULT (tmpl) = fn;
5203 DECL_ARTIFICIAL (tmpl) = true;
5204 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5205 return tmpl;
5206 }
5207
5208 /* Called when a class template TYPE is redeclared with the indicated
5209 template PARMS, e.g.:
5210
5211 template <class T> struct S;
5212 template <class T> struct S {}; */
5213
5214 bool
5215 redeclare_class_template (tree type, tree parms)
5216 {
5217 tree tmpl;
5218 tree tmpl_parms;
5219 int i;
5220
5221 if (!TYPE_TEMPLATE_INFO (type))
5222 {
5223 error ("%qT is not a template type", type);
5224 return false;
5225 }
5226
5227 tmpl = TYPE_TI_TEMPLATE (type);
5228 if (!PRIMARY_TEMPLATE_P (tmpl))
5229 /* The type is nested in some template class. Nothing to worry
5230 about here; there are no new template parameters for the nested
5231 type. */
5232 return true;
5233
5234 if (!parms)
5235 {
5236 error ("template specifiers not specified in declaration of %qD",
5237 tmpl);
5238 return false;
5239 }
5240
5241 parms = INNERMOST_TEMPLATE_PARMS (parms);
5242 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5243
5244 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5245 {
5246 error_n (input_location, TREE_VEC_LENGTH (parms),
5247 "redeclared with %d template parameter",
5248 "redeclared with %d template parameters",
5249 TREE_VEC_LENGTH (parms));
5250 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5251 "previous declaration %qD used %d template parameter",
5252 "previous declaration %qD used %d template parameters",
5253 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5254 return false;
5255 }
5256
5257 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5258 {
5259 tree tmpl_parm;
5260 tree parm;
5261 tree tmpl_default;
5262 tree parm_default;
5263
5264 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5265 || TREE_VEC_ELT (parms, i) == error_mark_node)
5266 continue;
5267
5268 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5269 if (error_operand_p (tmpl_parm))
5270 return false;
5271
5272 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5273 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5274 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5275
5276 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5277 TEMPLATE_DECL. */
5278 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5279 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5280 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5281 || (TREE_CODE (tmpl_parm) != PARM_DECL
5282 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5283 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5284 || (TREE_CODE (tmpl_parm) == PARM_DECL
5285 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5286 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5287 {
5288 error ("template parameter %q+#D", tmpl_parm);
5289 error ("redeclared here as %q#D", parm);
5290 return false;
5291 }
5292
5293 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5294 {
5295 /* We have in [temp.param]:
5296
5297 A template-parameter may not be given default arguments
5298 by two different declarations in the same scope. */
5299 error_at (input_location, "redefinition of default argument for %q#D", parm);
5300 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5301 "original definition appeared here");
5302 return false;
5303 }
5304
5305 if (parm_default != NULL_TREE)
5306 /* Update the previous template parameters (which are the ones
5307 that will really count) with the new default value. */
5308 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5309 else if (tmpl_default != NULL_TREE)
5310 /* Update the new parameters, too; they'll be used as the
5311 parameters for any members. */
5312 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5313
5314 /* Give each template template parm in this redeclaration a
5315 DECL_CONTEXT of the template for which they are a parameter. */
5316 if (TREE_CODE (parm) == TEMPLATE_DECL)
5317 {
5318 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5319 DECL_CONTEXT (parm) = tmpl;
5320 }
5321 }
5322
5323 return true;
5324 }
5325
5326 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5327 to be used when the caller has already checked
5328 (processing_template_decl
5329 && !instantiation_dependent_expression_p (expr)
5330 && potential_constant_expression (expr))
5331 and cleared processing_template_decl. */
5332
5333 tree
5334 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5335 {
5336 return tsubst_copy_and_build (expr,
5337 /*args=*/NULL_TREE,
5338 complain,
5339 /*in_decl=*/NULL_TREE,
5340 /*function_p=*/false,
5341 /*integral_constant_expression_p=*/true);
5342 }
5343
5344 /* Simplify EXPR if it is a non-dependent expression. Returns the
5345 (possibly simplified) expression. */
5346
5347 tree
5348 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5349 {
5350 if (expr == NULL_TREE)
5351 return NULL_TREE;
5352
5353 /* If we're in a template, but EXPR isn't value dependent, simplify
5354 it. We're supposed to treat:
5355
5356 template <typename T> void f(T[1 + 1]);
5357 template <typename T> void f(T[2]);
5358
5359 as two declarations of the same function, for example. */
5360 if (processing_template_decl
5361 && !instantiation_dependent_expression_p (expr)
5362 && potential_constant_expression (expr))
5363 {
5364 processing_template_decl_sentinel s;
5365 expr = instantiate_non_dependent_expr_internal (expr, complain);
5366 }
5367 return expr;
5368 }
5369
5370 tree
5371 instantiate_non_dependent_expr (tree expr)
5372 {
5373 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5374 }
5375
5376 /* True iff T is a specialization of a variable template. */
5377
5378 bool
5379 variable_template_specialization_p (tree t)
5380 {
5381 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5382 return false;
5383 tree tmpl = DECL_TI_TEMPLATE (t);
5384 return variable_template_p (tmpl);
5385 }
5386
5387 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5388 template declaration, or a TYPE_DECL for an alias declaration. */
5389
5390 bool
5391 alias_type_or_template_p (tree t)
5392 {
5393 if (t == NULL_TREE)
5394 return false;
5395 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5396 || (TYPE_P (t)
5397 && TYPE_NAME (t)
5398 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5399 || DECL_ALIAS_TEMPLATE_P (t));
5400 }
5401
5402 /* Return TRUE iff T is a specialization of an alias template. */
5403
5404 bool
5405 alias_template_specialization_p (const_tree t)
5406 {
5407 /* It's an alias template specialization if it's an alias and its
5408 TYPE_NAME is a specialization of a primary template. */
5409 if (TYPE_ALIAS_P (t))
5410 {
5411 tree name = TYPE_NAME (t);
5412 if (DECL_LANG_SPECIFIC (name))
5413 if (tree ti = DECL_TEMPLATE_INFO (name))
5414 {
5415 tree tmpl = TI_TEMPLATE (ti);
5416 return PRIMARY_TEMPLATE_P (tmpl);
5417 }
5418 }
5419 return false;
5420 }
5421
5422 /* An alias template is complex from a SFINAE perspective if a template-id
5423 using that alias can be ill-formed when the expansion is not, as with
5424 the void_t template. We determine this by checking whether the
5425 expansion for the alias template uses all its template parameters. */
5426
5427 struct uses_all_template_parms_data
5428 {
5429 int level;
5430 bool *seen;
5431 };
5432
5433 static int
5434 uses_all_template_parms_r (tree t, void *data_)
5435 {
5436 struct uses_all_template_parms_data &data
5437 = *(struct uses_all_template_parms_data*)data_;
5438 tree idx = get_template_parm_index (t);
5439
5440 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5441 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5442 return 0;
5443 }
5444
5445 static bool
5446 complex_alias_template_p (const_tree tmpl)
5447 {
5448 struct uses_all_template_parms_data data;
5449 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5450 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5451 data.level = TMPL_PARMS_DEPTH (parms);
5452 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5453 data.seen = XALLOCAVEC (bool, len);
5454 for (int i = 0; i < len; ++i)
5455 data.seen[i] = false;
5456
5457 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5458 for (int i = 0; i < len; ++i)
5459 if (!data.seen[i])
5460 return true;
5461 return false;
5462 }
5463
5464 /* Return TRUE iff T is a specialization of a complex alias template with
5465 dependent template-arguments. */
5466
5467 bool
5468 dependent_alias_template_spec_p (const_tree t)
5469 {
5470 return (alias_template_specialization_p (t)
5471 && TEMPLATE_DECL_COMPLEX_ALIAS_P (DECL_TI_TEMPLATE (TYPE_NAME (t)))
5472 && (any_dependent_template_arguments_p
5473 (INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (t)))));
5474 }
5475
5476 /* Return the number of innermost template parameters in TMPL. */
5477
5478 static int
5479 num_innermost_template_parms (tree tmpl)
5480 {
5481 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5482 return TREE_VEC_LENGTH (parms);
5483 }
5484
5485 /* Return either TMPL or another template that it is equivalent to under DR
5486 1286: An alias that just changes the name of a template is equivalent to
5487 the other template. */
5488
5489 static tree
5490 get_underlying_template (tree tmpl)
5491 {
5492 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5493 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5494 {
5495 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5496 if (TYPE_TEMPLATE_INFO (result))
5497 {
5498 tree sub = TYPE_TI_TEMPLATE (result);
5499 if (PRIMARY_TEMPLATE_P (sub)
5500 && (num_innermost_template_parms (tmpl)
5501 == num_innermost_template_parms (sub)))
5502 {
5503 tree alias_args = INNERMOST_TEMPLATE_ARGS
5504 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5505 if (!comp_template_args (TYPE_TI_ARGS (result), alias_args))
5506 break;
5507 /* The alias type is equivalent to the pattern of the
5508 underlying template, so strip the alias. */
5509 tmpl = sub;
5510 continue;
5511 }
5512 }
5513 break;
5514 }
5515 return tmpl;
5516 }
5517
5518 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5519 must be a function or a pointer-to-function type, as specified
5520 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5521 and check that the resulting function has external linkage. */
5522
5523 static tree
5524 convert_nontype_argument_function (tree type, tree expr,
5525 tsubst_flags_t complain)
5526 {
5527 tree fns = expr;
5528 tree fn, fn_no_ptr;
5529 linkage_kind linkage;
5530
5531 fn = instantiate_type (type, fns, tf_none);
5532 if (fn == error_mark_node)
5533 return error_mark_node;
5534
5535 fn_no_ptr = fn;
5536 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5537 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5538 if (BASELINK_P (fn_no_ptr))
5539 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5540
5541 /* [temp.arg.nontype]/1
5542
5543 A template-argument for a non-type, non-template template-parameter
5544 shall be one of:
5545 [...]
5546 -- the address of an object or function with external [C++11: or
5547 internal] linkage. */
5548
5549 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5550 {
5551 if (complain & tf_error)
5552 {
5553 error ("%qE is not a valid template argument for type %qT",
5554 expr, type);
5555 if (TYPE_PTR_P (type))
5556 error ("it must be the address of a function with "
5557 "external linkage");
5558 else
5559 error ("it must be the name of a function with "
5560 "external linkage");
5561 }
5562 return NULL_TREE;
5563 }
5564
5565 linkage = decl_linkage (fn_no_ptr);
5566 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5567 {
5568 if (complain & tf_error)
5569 {
5570 if (cxx_dialect >= cxx11)
5571 error ("%qE is not a valid template argument for type %qT "
5572 "because %qD has no linkage",
5573 expr, type, fn_no_ptr);
5574 else
5575 error ("%qE is not a valid template argument for type %qT "
5576 "because %qD does not have external linkage",
5577 expr, type, fn_no_ptr);
5578 }
5579 return NULL_TREE;
5580 }
5581
5582 return fn;
5583 }
5584
5585 /* Subroutine of convert_nontype_argument.
5586 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5587 Emit an error otherwise. */
5588
5589 static bool
5590 check_valid_ptrmem_cst_expr (tree type, tree expr,
5591 tsubst_flags_t complain)
5592 {
5593 STRIP_NOPS (expr);
5594 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5595 return true;
5596 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5597 return true;
5598 if (processing_template_decl
5599 && TREE_CODE (expr) == ADDR_EXPR
5600 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
5601 return true;
5602 if (complain & tf_error)
5603 {
5604 error ("%qE is not a valid template argument for type %qT",
5605 expr, type);
5606 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5607 }
5608 return false;
5609 }
5610
5611 /* Returns TRUE iff the address of OP is value-dependent.
5612
5613 14.6.2.4 [temp.dep.temp]:
5614 A non-integral non-type template-argument is dependent if its type is
5615 dependent or it has either of the following forms
5616 qualified-id
5617 & qualified-id
5618 and contains a nested-name-specifier which specifies a class-name that
5619 names a dependent type.
5620
5621 We generalize this to just say that the address of a member of a
5622 dependent class is value-dependent; the above doesn't cover the
5623 address of a static data member named with an unqualified-id. */
5624
5625 static bool
5626 has_value_dependent_address (tree op)
5627 {
5628 /* We could use get_inner_reference here, but there's no need;
5629 this is only relevant for template non-type arguments, which
5630 can only be expressed as &id-expression. */
5631 if (DECL_P (op))
5632 {
5633 tree ctx = CP_DECL_CONTEXT (op);
5634 if (TYPE_P (ctx) && dependent_type_p (ctx))
5635 return true;
5636 }
5637
5638 return false;
5639 }
5640
5641 /* The next set of functions are used for providing helpful explanatory
5642 diagnostics for failed overload resolution. Their messages should be
5643 indented by two spaces for consistency with the messages in
5644 call.c */
5645
5646 static int
5647 unify_success (bool /*explain_p*/)
5648 {
5649 return 0;
5650 }
5651
5652 static int
5653 unify_parameter_deduction_failure (bool explain_p, tree parm)
5654 {
5655 if (explain_p)
5656 inform (input_location,
5657 " couldn't deduce template parameter %qD", parm);
5658 return 1;
5659 }
5660
5661 static int
5662 unify_invalid (bool /*explain_p*/)
5663 {
5664 return 1;
5665 }
5666
5667 static int
5668 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5669 {
5670 if (explain_p)
5671 inform (input_location,
5672 " types %qT and %qT have incompatible cv-qualifiers",
5673 parm, arg);
5674 return 1;
5675 }
5676
5677 static int
5678 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5679 {
5680 if (explain_p)
5681 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5682 return 1;
5683 }
5684
5685 static int
5686 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5687 {
5688 if (explain_p)
5689 inform (input_location,
5690 " template parameter %qD is not a parameter pack, but "
5691 "argument %qD is",
5692 parm, arg);
5693 return 1;
5694 }
5695
5696 static int
5697 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5698 {
5699 if (explain_p)
5700 inform (input_location,
5701 " template argument %qE does not match "
5702 "pointer-to-member constant %qE",
5703 arg, parm);
5704 return 1;
5705 }
5706
5707 static int
5708 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5709 {
5710 if (explain_p)
5711 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5712 return 1;
5713 }
5714
5715 static int
5716 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5717 {
5718 if (explain_p)
5719 inform (input_location,
5720 " inconsistent parameter pack deduction with %qT and %qT",
5721 old_arg, new_arg);
5722 return 1;
5723 }
5724
5725 static int
5726 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5727 {
5728 if (explain_p)
5729 {
5730 if (TYPE_P (parm))
5731 inform (input_location,
5732 " deduced conflicting types for parameter %qT (%qT and %qT)",
5733 parm, first, second);
5734 else
5735 inform (input_location,
5736 " deduced conflicting values for non-type parameter "
5737 "%qE (%qE and %qE)", parm, first, second);
5738 }
5739 return 1;
5740 }
5741
5742 static int
5743 unify_vla_arg (bool explain_p, tree arg)
5744 {
5745 if (explain_p)
5746 inform (input_location,
5747 " variable-sized array type %qT is not "
5748 "a valid template argument",
5749 arg);
5750 return 1;
5751 }
5752
5753 static int
5754 unify_method_type_error (bool explain_p, tree arg)
5755 {
5756 if (explain_p)
5757 inform (input_location,
5758 " member function type %qT is not a valid template argument",
5759 arg);
5760 return 1;
5761 }
5762
5763 static int
5764 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
5765 {
5766 if (explain_p)
5767 {
5768 if (least_p)
5769 inform_n (input_location, wanted,
5770 " candidate expects at least %d argument, %d provided",
5771 " candidate expects at least %d arguments, %d provided",
5772 wanted, have);
5773 else
5774 inform_n (input_location, wanted,
5775 " candidate expects %d argument, %d provided",
5776 " candidate expects %d arguments, %d provided",
5777 wanted, have);
5778 }
5779 return 1;
5780 }
5781
5782 static int
5783 unify_too_many_arguments (bool explain_p, int have, int wanted)
5784 {
5785 return unify_arity (explain_p, have, wanted);
5786 }
5787
5788 static int
5789 unify_too_few_arguments (bool explain_p, int have, int wanted,
5790 bool least_p = false)
5791 {
5792 return unify_arity (explain_p, have, wanted, least_p);
5793 }
5794
5795 static int
5796 unify_arg_conversion (bool explain_p, tree to_type,
5797 tree from_type, tree arg)
5798 {
5799 if (explain_p)
5800 inform (EXPR_LOC_OR_LOC (arg, input_location),
5801 " cannot convert %qE (type %qT) to type %qT",
5802 arg, from_type, to_type);
5803 return 1;
5804 }
5805
5806 static int
5807 unify_no_common_base (bool explain_p, enum template_base_result r,
5808 tree parm, tree arg)
5809 {
5810 if (explain_p)
5811 switch (r)
5812 {
5813 case tbr_ambiguous_baseclass:
5814 inform (input_location, " %qT is an ambiguous base class of %qT",
5815 parm, arg);
5816 break;
5817 default:
5818 inform (input_location, " %qT is not derived from %qT", arg, parm);
5819 break;
5820 }
5821 return 1;
5822 }
5823
5824 static int
5825 unify_inconsistent_template_template_parameters (bool explain_p)
5826 {
5827 if (explain_p)
5828 inform (input_location,
5829 " template parameters of a template template argument are "
5830 "inconsistent with other deduced template arguments");
5831 return 1;
5832 }
5833
5834 static int
5835 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5836 {
5837 if (explain_p)
5838 inform (input_location,
5839 " can't deduce a template for %qT from non-template type %qT",
5840 parm, arg);
5841 return 1;
5842 }
5843
5844 static int
5845 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5846 {
5847 if (explain_p)
5848 inform (input_location,
5849 " template argument %qE does not match %qD", arg, parm);
5850 return 1;
5851 }
5852
5853 static int
5854 unify_overload_resolution_failure (bool explain_p, tree arg)
5855 {
5856 if (explain_p)
5857 inform (input_location,
5858 " could not resolve address from overloaded function %qE",
5859 arg);
5860 return 1;
5861 }
5862
5863 /* Attempt to convert the non-type template parameter EXPR to the
5864 indicated TYPE. If the conversion is successful, return the
5865 converted value. If the conversion is unsuccessful, return
5866 NULL_TREE if we issued an error message, or error_mark_node if we
5867 did not. We issue error messages for out-and-out bad template
5868 parameters, but not simply because the conversion failed, since we
5869 might be just trying to do argument deduction. Both TYPE and EXPR
5870 must be non-dependent.
5871
5872 The conversion follows the special rules described in
5873 [temp.arg.nontype], and it is much more strict than an implicit
5874 conversion.
5875
5876 This function is called twice for each template argument (see
5877 lookup_template_class for a more accurate description of this
5878 problem). This means that we need to handle expressions which
5879 are not valid in a C++ source, but can be created from the
5880 first call (for instance, casts to perform conversions). These
5881 hacks can go away after we fix the double coercion problem. */
5882
5883 static tree
5884 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5885 {
5886 tree expr_type;
5887
5888 /* Detect immediately string literals as invalid non-type argument.
5889 This special-case is not needed for correctness (we would easily
5890 catch this later), but only to provide better diagnostic for this
5891 common user mistake. As suggested by DR 100, we do not mention
5892 linkage issues in the diagnostic as this is not the point. */
5893 /* FIXME we're making this OK. */
5894 if (TREE_CODE (expr) == STRING_CST)
5895 {
5896 if (complain & tf_error)
5897 error ("%qE is not a valid template argument for type %qT "
5898 "because string literals can never be used in this context",
5899 expr, type);
5900 return NULL_TREE;
5901 }
5902
5903 /* Add the ADDR_EXPR now for the benefit of
5904 value_dependent_expression_p. */
5905 if (TYPE_PTROBV_P (type)
5906 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5907 {
5908 expr = decay_conversion (expr, complain);
5909 if (expr == error_mark_node)
5910 return error_mark_node;
5911 }
5912
5913 /* If we are in a template, EXPR may be non-dependent, but still
5914 have a syntactic, rather than semantic, form. For example, EXPR
5915 might be a SCOPE_REF, rather than the VAR_DECL to which the
5916 SCOPE_REF refers. Preserving the qualifying scope is necessary
5917 so that access checking can be performed when the template is
5918 instantiated -- but here we need the resolved form so that we can
5919 convert the argument. */
5920 bool non_dep = false;
5921 if (TYPE_REF_OBJ_P (type)
5922 && has_value_dependent_address (expr))
5923 /* If we want the address and it's value-dependent, don't fold. */;
5924 else if (!type_unknown_p (expr)
5925 && processing_template_decl
5926 && !instantiation_dependent_expression_p (expr)
5927 && potential_constant_expression (expr))
5928 non_dep = true;
5929 if (error_operand_p (expr))
5930 return error_mark_node;
5931 expr_type = TREE_TYPE (expr);
5932 if (TREE_CODE (type) == REFERENCE_TYPE)
5933 expr = mark_lvalue_use (expr);
5934 else
5935 expr = mark_rvalue_use (expr);
5936
5937 /* If the argument is non-dependent, perform any conversions in
5938 non-dependent context as well. */
5939 processing_template_decl_sentinel s (non_dep);
5940 if (non_dep)
5941 expr = instantiate_non_dependent_expr_internal (expr, complain);
5942
5943 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5944 to a non-type argument of "nullptr". */
5945 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5946 expr = convert (type, expr);
5947
5948 /* In C++11, integral or enumeration non-type template arguments can be
5949 arbitrary constant expressions. Pointer and pointer to
5950 member arguments can be general constant expressions that evaluate
5951 to a null value, but otherwise still need to be of a specific form. */
5952 if (cxx_dialect >= cxx11)
5953 {
5954 if (TREE_CODE (expr) == PTRMEM_CST)
5955 /* A PTRMEM_CST is already constant, and a valid template
5956 argument for a parameter of pointer to member type, we just want
5957 to leave it in that form rather than lower it to a
5958 CONSTRUCTOR. */;
5959 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5960 expr = maybe_constant_value (expr);
5961 else if (TYPE_PTR_OR_PTRMEM_P (type))
5962 {
5963 tree folded = maybe_constant_value (expr);
5964 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5965 : null_member_pointer_value_p (folded))
5966 expr = folded;
5967 }
5968 }
5969
5970 /* HACK: Due to double coercion, we can get a
5971 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5972 which is the tree that we built on the first call (see
5973 below when coercing to reference to object or to reference to
5974 function). We just strip everything and get to the arg.
5975 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5976 for examples. */
5977 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5978 {
5979 tree probe_type, probe = expr;
5980 if (REFERENCE_REF_P (probe))
5981 probe = TREE_OPERAND (probe, 0);
5982 probe_type = TREE_TYPE (probe);
5983 if (TREE_CODE (probe) == NOP_EXPR)
5984 {
5985 /* ??? Maybe we could use convert_from_reference here, but we
5986 would need to relax its constraints because the NOP_EXPR
5987 could actually change the type to something more cv-qualified,
5988 and this is not folded by convert_from_reference. */
5989 tree addr = TREE_OPERAND (probe, 0);
5990 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5991 && TREE_CODE (addr) == ADDR_EXPR
5992 && TYPE_PTR_P (TREE_TYPE (addr))
5993 && (same_type_ignoring_top_level_qualifiers_p
5994 (TREE_TYPE (probe_type),
5995 TREE_TYPE (TREE_TYPE (addr)))))
5996 {
5997 expr = TREE_OPERAND (addr, 0);
5998 expr_type = TREE_TYPE (probe_type);
5999 }
6000 }
6001 }
6002
6003 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6004 parameter is a pointer to object, through decay and
6005 qualification conversion. Let's strip everything. */
6006 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6007 {
6008 tree probe = expr;
6009 STRIP_NOPS (probe);
6010 if (TREE_CODE (probe) == ADDR_EXPR
6011 && TYPE_PTR_P (TREE_TYPE (probe)))
6012 {
6013 /* Skip the ADDR_EXPR only if it is part of the decay for
6014 an array. Otherwise, it is part of the original argument
6015 in the source code. */
6016 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6017 probe = TREE_OPERAND (probe, 0);
6018 expr = probe;
6019 expr_type = TREE_TYPE (expr);
6020 }
6021 }
6022
6023 /* [temp.arg.nontype]/5, bullet 1
6024
6025 For a non-type template-parameter of integral or enumeration type,
6026 integral promotions (_conv.prom_) and integral conversions
6027 (_conv.integral_) are applied. */
6028 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6029 {
6030 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6031 t = maybe_constant_value (t);
6032 if (t != error_mark_node)
6033 expr = t;
6034
6035 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6036 return error_mark_node;
6037
6038 /* Notice that there are constant expressions like '4 % 0' which
6039 do not fold into integer constants. */
6040 if (TREE_CODE (expr) != INTEGER_CST)
6041 {
6042 if (complain & tf_error)
6043 {
6044 int errs = errorcount, warns = warningcount + werrorcount;
6045 if (processing_template_decl
6046 && !require_potential_constant_expression (expr))
6047 return NULL_TREE;
6048 expr = cxx_constant_value (expr);
6049 if (errorcount > errs || warningcount + werrorcount > warns)
6050 inform (EXPR_LOC_OR_LOC (expr, input_location),
6051 "in template argument for type %qT ", type);
6052 if (expr == error_mark_node)
6053 return NULL_TREE;
6054 /* else cxx_constant_value complained but gave us
6055 a real constant, so go ahead. */
6056 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6057 }
6058 else
6059 return NULL_TREE;
6060 }
6061
6062 /* Avoid typedef problems. */
6063 if (TREE_TYPE (expr) != type)
6064 expr = fold_convert (type, expr);
6065 }
6066 /* [temp.arg.nontype]/5, bullet 2
6067
6068 For a non-type template-parameter of type pointer to object,
6069 qualification conversions (_conv.qual_) and the array-to-pointer
6070 conversion (_conv.array_) are applied. */
6071 else if (TYPE_PTROBV_P (type))
6072 {
6073 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6074
6075 A template-argument for a non-type, non-template template-parameter
6076 shall be one of: [...]
6077
6078 -- the name of a non-type template-parameter;
6079 -- the address of an object or function with external linkage, [...]
6080 expressed as "& id-expression" where the & is optional if the name
6081 refers to a function or array, or if the corresponding
6082 template-parameter is a reference.
6083
6084 Here, we do not care about functions, as they are invalid anyway
6085 for a parameter of type pointer-to-object. */
6086
6087 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
6088 /* Non-type template parameters are OK. */
6089 ;
6090 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6091 /* Null pointer values are OK in C++11. */;
6092 else if (TREE_CODE (expr) != ADDR_EXPR
6093 && TREE_CODE (expr_type) != ARRAY_TYPE)
6094 {
6095 if (VAR_P (expr))
6096 {
6097 if (complain & tf_error)
6098 error ("%qD is not a valid template argument "
6099 "because %qD is a variable, not the address of "
6100 "a variable", expr, expr);
6101 return NULL_TREE;
6102 }
6103 if (POINTER_TYPE_P (expr_type))
6104 {
6105 if (complain & tf_error)
6106 error ("%qE is not a valid template argument for %qT "
6107 "because it is not the address of a variable",
6108 expr, type);
6109 return NULL_TREE;
6110 }
6111 /* Other values, like integer constants, might be valid
6112 non-type arguments of some other type. */
6113 return error_mark_node;
6114 }
6115 else
6116 {
6117 tree decl;
6118
6119 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6120 ? TREE_OPERAND (expr, 0) : expr);
6121 if (!VAR_P (decl))
6122 {
6123 if (complain & tf_error)
6124 error ("%qE is not a valid template argument of type %qT "
6125 "because %qE is not a variable", expr, type, decl);
6126 return NULL_TREE;
6127 }
6128 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6129 {
6130 if (complain & tf_error)
6131 error ("%qE is not a valid template argument of type %qT "
6132 "because %qD does not have external linkage",
6133 expr, type, decl);
6134 return NULL_TREE;
6135 }
6136 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6137 {
6138 if (complain & tf_error)
6139 error ("%qE is not a valid template argument of type %qT "
6140 "because %qD has no linkage", expr, type, decl);
6141 return NULL_TREE;
6142 }
6143 }
6144
6145 expr = decay_conversion (expr, complain);
6146 if (expr == error_mark_node)
6147 return error_mark_node;
6148
6149 expr = perform_qualification_conversions (type, expr);
6150 if (expr == error_mark_node)
6151 return error_mark_node;
6152 }
6153 /* [temp.arg.nontype]/5, bullet 3
6154
6155 For a non-type template-parameter of type reference to object, no
6156 conversions apply. The type referred to by the reference may be more
6157 cv-qualified than the (otherwise identical) type of the
6158 template-argument. The template-parameter is bound directly to the
6159 template-argument, which must be an lvalue. */
6160 else if (TYPE_REF_OBJ_P (type))
6161 {
6162 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6163 expr_type))
6164 return error_mark_node;
6165
6166 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6167 {
6168 if (complain & tf_error)
6169 error ("%qE is not a valid template argument for type %qT "
6170 "because of conflicts in cv-qualification", expr, type);
6171 return NULL_TREE;
6172 }
6173
6174 if (!real_lvalue_p (expr))
6175 {
6176 if (complain & tf_error)
6177 error ("%qE is not a valid template argument for type %qT "
6178 "because it is not an lvalue", expr, type);
6179 return NULL_TREE;
6180 }
6181
6182 /* [temp.arg.nontype]/1
6183
6184 A template-argument for a non-type, non-template template-parameter
6185 shall be one of: [...]
6186
6187 -- the address of an object or function with external linkage. */
6188 if (INDIRECT_REF_P (expr)
6189 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6190 {
6191 expr = TREE_OPERAND (expr, 0);
6192 if (DECL_P (expr))
6193 {
6194 if (complain & tf_error)
6195 error ("%q#D is not a valid template argument for type %qT "
6196 "because a reference variable does not have a constant "
6197 "address", expr, type);
6198 return NULL_TREE;
6199 }
6200 }
6201
6202 if (!DECL_P (expr))
6203 {
6204 if (complain & tf_error)
6205 error ("%qE is not a valid template argument for type %qT "
6206 "because it is not an object with external linkage",
6207 expr, type);
6208 return NULL_TREE;
6209 }
6210
6211 if (!DECL_EXTERNAL_LINKAGE_P (expr))
6212 {
6213 if (complain & tf_error)
6214 error ("%qE is not a valid template argument for type %qT "
6215 "because object %qD has not external linkage",
6216 expr, type, expr);
6217 return NULL_TREE;
6218 }
6219
6220 expr = build_nop (type, build_address (expr));
6221 }
6222 /* [temp.arg.nontype]/5, bullet 4
6223
6224 For a non-type template-parameter of type pointer to function, only
6225 the function-to-pointer conversion (_conv.func_) is applied. If the
6226 template-argument represents a set of overloaded functions (or a
6227 pointer to such), the matching function is selected from the set
6228 (_over.over_). */
6229 else if (TYPE_PTRFN_P (type))
6230 {
6231 /* If the argument is a template-id, we might not have enough
6232 context information to decay the pointer. */
6233 if (!type_unknown_p (expr_type))
6234 {
6235 expr = decay_conversion (expr, complain);
6236 if (expr == error_mark_node)
6237 return error_mark_node;
6238 }
6239
6240 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6241 /* Null pointer values are OK in C++11. */
6242 return perform_qualification_conversions (type, expr);
6243
6244 expr = convert_nontype_argument_function (type, expr, complain);
6245 if (!expr || expr == error_mark_node)
6246 return expr;
6247 }
6248 /* [temp.arg.nontype]/5, bullet 5
6249
6250 For a non-type template-parameter of type reference to function, no
6251 conversions apply. If the template-argument represents a set of
6252 overloaded functions, the matching function is selected from the set
6253 (_over.over_). */
6254 else if (TYPE_REFFN_P (type))
6255 {
6256 if (TREE_CODE (expr) == ADDR_EXPR)
6257 {
6258 if (complain & tf_error)
6259 {
6260 error ("%qE is not a valid template argument for type %qT "
6261 "because it is a pointer", expr, type);
6262 inform (input_location, "try using %qE instead",
6263 TREE_OPERAND (expr, 0));
6264 }
6265 return NULL_TREE;
6266 }
6267
6268 expr = convert_nontype_argument_function (type, expr, complain);
6269 if (!expr || expr == error_mark_node)
6270 return expr;
6271
6272 expr = build_nop (type, build_address (expr));
6273 }
6274 /* [temp.arg.nontype]/5, bullet 6
6275
6276 For a non-type template-parameter of type pointer to member function,
6277 no conversions apply. If the template-argument represents a set of
6278 overloaded member functions, the matching member function is selected
6279 from the set (_over.over_). */
6280 else if (TYPE_PTRMEMFUNC_P (type))
6281 {
6282 expr = instantiate_type (type, expr, tf_none);
6283 if (expr == error_mark_node)
6284 return error_mark_node;
6285
6286 /* [temp.arg.nontype] bullet 1 says the pointer to member
6287 expression must be a pointer-to-member constant. */
6288 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6289 return error_mark_node;
6290
6291 /* There is no way to disable standard conversions in
6292 resolve_address_of_overloaded_function (called by
6293 instantiate_type). It is possible that the call succeeded by
6294 converting &B::I to &D::I (where B is a base of D), so we need
6295 to reject this conversion here.
6296
6297 Actually, even if there was a way to disable standard conversions,
6298 it would still be better to reject them here so that we can
6299 provide a superior diagnostic. */
6300 if (!same_type_p (TREE_TYPE (expr), type))
6301 {
6302 if (complain & tf_error)
6303 {
6304 error ("%qE is not a valid template argument for type %qT "
6305 "because it is of type %qT", expr, type,
6306 TREE_TYPE (expr));
6307 /* If we are just one standard conversion off, explain. */
6308 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6309 inform (input_location,
6310 "standard conversions are not allowed in this context");
6311 }
6312 return NULL_TREE;
6313 }
6314 }
6315 /* [temp.arg.nontype]/5, bullet 7
6316
6317 For a non-type template-parameter of type pointer to data member,
6318 qualification conversions (_conv.qual_) are applied. */
6319 else if (TYPE_PTRDATAMEM_P (type))
6320 {
6321 /* [temp.arg.nontype] bullet 1 says the pointer to member
6322 expression must be a pointer-to-member constant. */
6323 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
6324 return error_mark_node;
6325
6326 expr = perform_qualification_conversions (type, expr);
6327 if (expr == error_mark_node)
6328 return expr;
6329 }
6330 else if (NULLPTR_TYPE_P (type))
6331 {
6332 if (expr != nullptr_node)
6333 {
6334 if (complain & tf_error)
6335 error ("%qE is not a valid template argument for type %qT "
6336 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6337 return NULL_TREE;
6338 }
6339 return expr;
6340 }
6341 /* A template non-type parameter must be one of the above. */
6342 else
6343 gcc_unreachable ();
6344
6345 /* Sanity check: did we actually convert the argument to the
6346 right type? */
6347 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6348 (type, TREE_TYPE (expr)));
6349 return convert_from_reference (expr);
6350 }
6351
6352 /* Subroutine of coerce_template_template_parms, which returns 1 if
6353 PARM_PARM and ARG_PARM match using the rule for the template
6354 parameters of template template parameters. Both PARM and ARG are
6355 template parameters; the rest of the arguments are the same as for
6356 coerce_template_template_parms.
6357 */
6358 static int
6359 coerce_template_template_parm (tree parm,
6360 tree arg,
6361 tsubst_flags_t complain,
6362 tree in_decl,
6363 tree outer_args)
6364 {
6365 if (arg == NULL_TREE || error_operand_p (arg)
6366 || parm == NULL_TREE || error_operand_p (parm))
6367 return 0;
6368
6369 if (TREE_CODE (arg) != TREE_CODE (parm))
6370 return 0;
6371
6372 switch (TREE_CODE (parm))
6373 {
6374 case TEMPLATE_DECL:
6375 /* We encounter instantiations of templates like
6376 template <template <template <class> class> class TT>
6377 class C; */
6378 {
6379 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6380 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6381
6382 if (!coerce_template_template_parms
6383 (parmparm, argparm, complain, in_decl, outer_args))
6384 return 0;
6385 }
6386 /* Fall through. */
6387
6388 case TYPE_DECL:
6389 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6390 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6391 /* Argument is a parameter pack but parameter is not. */
6392 return 0;
6393 break;
6394
6395 case PARM_DECL:
6396 /* The tsubst call is used to handle cases such as
6397
6398 template <int> class C {};
6399 template <class T, template <T> class TT> class D {};
6400 D<int, C> d;
6401
6402 i.e. the parameter list of TT depends on earlier parameters. */
6403 if (!uses_template_parms (TREE_TYPE (arg)))
6404 {
6405 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6406 if (!uses_template_parms (t)
6407 && !same_type_p (t, TREE_TYPE (arg)))
6408 return 0;
6409 }
6410
6411 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6412 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6413 /* Argument is a parameter pack but parameter is not. */
6414 return 0;
6415
6416 break;
6417
6418 default:
6419 gcc_unreachable ();
6420 }
6421
6422 return 1;
6423 }
6424
6425
6426 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6427 template template parameters. Both PARM_PARMS and ARG_PARMS are
6428 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6429 or PARM_DECL.
6430
6431 Consider the example:
6432 template <class T> class A;
6433 template<template <class U> class TT> class B;
6434
6435 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6436 the parameters to A, and OUTER_ARGS contains A. */
6437
6438 static int
6439 coerce_template_template_parms (tree parm_parms,
6440 tree arg_parms,
6441 tsubst_flags_t complain,
6442 tree in_decl,
6443 tree outer_args)
6444 {
6445 int nparms, nargs, i;
6446 tree parm, arg;
6447 int variadic_p = 0;
6448
6449 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6450 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6451
6452 nparms = TREE_VEC_LENGTH (parm_parms);
6453 nargs = TREE_VEC_LENGTH (arg_parms);
6454
6455 /* Determine whether we have a parameter pack at the end of the
6456 template template parameter's template parameter list. */
6457 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6458 {
6459 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6460
6461 if (error_operand_p (parm))
6462 return 0;
6463
6464 switch (TREE_CODE (parm))
6465 {
6466 case TEMPLATE_DECL:
6467 case TYPE_DECL:
6468 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6469 variadic_p = 1;
6470 break;
6471
6472 case PARM_DECL:
6473 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6474 variadic_p = 1;
6475 break;
6476
6477 default:
6478 gcc_unreachable ();
6479 }
6480 }
6481
6482 if (nargs != nparms
6483 && !(variadic_p && nargs >= nparms - 1))
6484 return 0;
6485
6486 /* Check all of the template parameters except the parameter pack at
6487 the end (if any). */
6488 for (i = 0; i < nparms - variadic_p; ++i)
6489 {
6490 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6491 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6492 continue;
6493
6494 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6495 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6496
6497 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6498 outer_args))
6499 return 0;
6500
6501 }
6502
6503 if (variadic_p)
6504 {
6505 /* Check each of the template parameters in the template
6506 argument against the template parameter pack at the end of
6507 the template template parameter. */
6508 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6509 return 0;
6510
6511 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6512
6513 for (; i < nargs; ++i)
6514 {
6515 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6516 continue;
6517
6518 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6519
6520 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6521 outer_args))
6522 return 0;
6523 }
6524 }
6525
6526 return 1;
6527 }
6528
6529 /* Verifies that the deduced template arguments (in TARGS) for the
6530 template template parameters (in TPARMS) represent valid bindings,
6531 by comparing the template parameter list of each template argument
6532 to the template parameter list of its corresponding template
6533 template parameter, in accordance with DR150. This
6534 routine can only be called after all template arguments have been
6535 deduced. It will return TRUE if all of the template template
6536 parameter bindings are okay, FALSE otherwise. */
6537 bool
6538 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6539 {
6540 int i, ntparms = TREE_VEC_LENGTH (tparms);
6541 bool ret = true;
6542
6543 /* We're dealing with template parms in this process. */
6544 ++processing_template_decl;
6545
6546 targs = INNERMOST_TEMPLATE_ARGS (targs);
6547
6548 for (i = 0; i < ntparms; ++i)
6549 {
6550 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6551 tree targ = TREE_VEC_ELT (targs, i);
6552
6553 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6554 {
6555 tree packed_args = NULL_TREE;
6556 int idx, len = 1;
6557
6558 if (ARGUMENT_PACK_P (targ))
6559 {
6560 /* Look inside the argument pack. */
6561 packed_args = ARGUMENT_PACK_ARGS (targ);
6562 len = TREE_VEC_LENGTH (packed_args);
6563 }
6564
6565 for (idx = 0; idx < len; ++idx)
6566 {
6567 tree targ_parms = NULL_TREE;
6568
6569 if (packed_args)
6570 /* Extract the next argument from the argument
6571 pack. */
6572 targ = TREE_VEC_ELT (packed_args, idx);
6573
6574 if (PACK_EXPANSION_P (targ))
6575 /* Look at the pattern of the pack expansion. */
6576 targ = PACK_EXPANSION_PATTERN (targ);
6577
6578 /* Extract the template parameters from the template
6579 argument. */
6580 if (TREE_CODE (targ) == TEMPLATE_DECL)
6581 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6582 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6583 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6584
6585 /* Verify that we can coerce the template template
6586 parameters from the template argument to the template
6587 parameter. This requires an exact match. */
6588 if (targ_parms
6589 && !coerce_template_template_parms
6590 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6591 targ_parms,
6592 tf_none,
6593 tparm,
6594 targs))
6595 {
6596 ret = false;
6597 goto out;
6598 }
6599 }
6600 }
6601 }
6602
6603 out:
6604
6605 --processing_template_decl;
6606 return ret;
6607 }
6608
6609 /* Since type attributes aren't mangled, we need to strip them from
6610 template type arguments. */
6611
6612 static tree
6613 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6614 {
6615 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6616 return arg;
6617 bool removed_attributes = false;
6618 tree canon = strip_typedefs (arg, &removed_attributes);
6619 if (removed_attributes
6620 && (complain & tf_warning))
6621 warning (0, "ignoring attributes on template argument %qT", arg);
6622 return canon;
6623 }
6624
6625 /* Convert the indicated template ARG as necessary to match the
6626 indicated template PARM. Returns the converted ARG, or
6627 error_mark_node if the conversion was unsuccessful. Error and
6628 warning messages are issued under control of COMPLAIN. This
6629 conversion is for the Ith parameter in the parameter list. ARGS is
6630 the full set of template arguments deduced so far. */
6631
6632 static tree
6633 convert_template_argument (tree parm,
6634 tree arg,
6635 tree args,
6636 tsubst_flags_t complain,
6637 int i,
6638 tree in_decl)
6639 {
6640 tree orig_arg;
6641 tree val;
6642 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6643
6644 if (parm == error_mark_node)
6645 return error_mark_node;
6646
6647 if (TREE_CODE (arg) == TREE_LIST
6648 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6649 {
6650 /* The template argument was the name of some
6651 member function. That's usually
6652 invalid, but static members are OK. In any
6653 case, grab the underlying fields/functions
6654 and issue an error later if required. */
6655 orig_arg = TREE_VALUE (arg);
6656 TREE_TYPE (arg) = unknown_type_node;
6657 }
6658
6659 orig_arg = arg;
6660
6661 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6662 requires_type = (TREE_CODE (parm) == TYPE_DECL
6663 || requires_tmpl_type);
6664
6665 /* When determining whether an argument pack expansion is a template,
6666 look at the pattern. */
6667 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6668 arg = PACK_EXPANSION_PATTERN (arg);
6669
6670 /* Deal with an injected-class-name used as a template template arg. */
6671 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6672 {
6673 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6674 if (TREE_CODE (t) == TEMPLATE_DECL)
6675 {
6676 if (cxx_dialect >= cxx11)
6677 /* OK under DR 1004. */;
6678 else if (complain & tf_warning_or_error)
6679 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6680 " used as template template argument", TYPE_NAME (arg));
6681 else if (flag_pedantic_errors)
6682 t = arg;
6683
6684 arg = t;
6685 }
6686 }
6687
6688 is_tmpl_type =
6689 ((TREE_CODE (arg) == TEMPLATE_DECL
6690 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6691 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6692 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6693 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6694
6695 if (is_tmpl_type
6696 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6697 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6698 arg = TYPE_STUB_DECL (arg);
6699
6700 is_type = TYPE_P (arg) || is_tmpl_type;
6701
6702 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6703 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6704 {
6705 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6706 {
6707 if (complain & tf_error)
6708 error ("invalid use of destructor %qE as a type", orig_arg);
6709 return error_mark_node;
6710 }
6711
6712 permerror (input_location,
6713 "to refer to a type member of a template parameter, "
6714 "use %<typename %E%>", orig_arg);
6715
6716 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6717 TREE_OPERAND (arg, 1),
6718 typename_type,
6719 complain);
6720 arg = orig_arg;
6721 is_type = 1;
6722 }
6723 if (is_type != requires_type)
6724 {
6725 if (in_decl)
6726 {
6727 if (complain & tf_error)
6728 {
6729 error ("type/value mismatch at argument %d in template "
6730 "parameter list for %qD",
6731 i + 1, in_decl);
6732 if (is_type)
6733 inform (input_location,
6734 " expected a constant of type %qT, got %qT",
6735 TREE_TYPE (parm),
6736 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6737 else if (requires_tmpl_type)
6738 inform (input_location,
6739 " expected a class template, got %qE", orig_arg);
6740 else
6741 inform (input_location,
6742 " expected a type, got %qE", orig_arg);
6743 }
6744 }
6745 return error_mark_node;
6746 }
6747 if (is_tmpl_type ^ requires_tmpl_type)
6748 {
6749 if (in_decl && (complain & tf_error))
6750 {
6751 error ("type/value mismatch at argument %d in template "
6752 "parameter list for %qD",
6753 i + 1, in_decl);
6754 if (is_tmpl_type)
6755 inform (input_location,
6756 " expected a type, got %qT", DECL_NAME (arg));
6757 else
6758 inform (input_location,
6759 " expected a class template, got %qT", orig_arg);
6760 }
6761 return error_mark_node;
6762 }
6763
6764 if (is_type)
6765 {
6766 if (requires_tmpl_type)
6767 {
6768 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6769 val = orig_arg;
6770 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6771 /* The number of argument required is not known yet.
6772 Just accept it for now. */
6773 val = TREE_TYPE (arg);
6774 else
6775 {
6776 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6777 tree argparm;
6778
6779 /* Strip alias templates that are equivalent to another
6780 template. */
6781 arg = get_underlying_template (arg);
6782 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6783
6784 if (coerce_template_template_parms (parmparm, argparm,
6785 complain, in_decl,
6786 args))
6787 {
6788 val = arg;
6789
6790 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6791 TEMPLATE_DECL. */
6792 if (val != error_mark_node)
6793 {
6794 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6795 val = TREE_TYPE (val);
6796 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6797 val = make_pack_expansion (val);
6798 }
6799 }
6800 else
6801 {
6802 if (in_decl && (complain & tf_error))
6803 {
6804 error ("type/value mismatch at argument %d in "
6805 "template parameter list for %qD",
6806 i + 1, in_decl);
6807 inform (input_location,
6808 " expected a template of type %qD, got %qT",
6809 parm, orig_arg);
6810 }
6811
6812 val = error_mark_node;
6813 }
6814 }
6815 }
6816 else
6817 val = orig_arg;
6818 /* We only form one instance of each template specialization.
6819 Therefore, if we use a non-canonical variant (i.e., a
6820 typedef), any future messages referring to the type will use
6821 the typedef, which is confusing if those future uses do not
6822 themselves also use the typedef. */
6823 if (TYPE_P (val))
6824 val = canonicalize_type_argument (val, complain);
6825 }
6826 else
6827 {
6828 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6829
6830 if (invalid_nontype_parm_type_p (t, complain))
6831 return error_mark_node;
6832
6833 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6834 {
6835 if (same_type_p (t, TREE_TYPE (orig_arg)))
6836 val = orig_arg;
6837 else
6838 {
6839 /* Not sure if this is reachable, but it doesn't hurt
6840 to be robust. */
6841 error ("type mismatch in nontype parameter pack");
6842 val = error_mark_node;
6843 }
6844 }
6845 else if (!dependent_template_arg_p (orig_arg)
6846 && !uses_template_parms (t))
6847 /* We used to call digest_init here. However, digest_init
6848 will report errors, which we don't want when complain
6849 is zero. More importantly, digest_init will try too
6850 hard to convert things: for example, `0' should not be
6851 converted to pointer type at this point according to
6852 the standard. Accepting this is not merely an
6853 extension, since deciding whether or not these
6854 conversions can occur is part of determining which
6855 function template to call, or whether a given explicit
6856 argument specification is valid. */
6857 val = convert_nontype_argument (t, orig_arg, complain);
6858 else
6859 {
6860 bool removed_attr = false;
6861 val = strip_typedefs_expr (orig_arg, &removed_attr);
6862 }
6863
6864 if (val == NULL_TREE)
6865 val = error_mark_node;
6866 else if (val == error_mark_node && (complain & tf_error))
6867 error ("could not convert template argument %qE to %qT", orig_arg, t);
6868
6869 if (TREE_CODE (val) == SCOPE_REF)
6870 {
6871 /* Strip typedefs from the SCOPE_REF. */
6872 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6873 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6874 complain);
6875 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6876 QUALIFIED_NAME_IS_TEMPLATE (val));
6877 }
6878 }
6879
6880 return val;
6881 }
6882
6883 /* Coerces the remaining template arguments in INNER_ARGS (from
6884 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6885 Returns the coerced argument pack. PARM_IDX is the position of this
6886 parameter in the template parameter list. ARGS is the original
6887 template argument list. */
6888 static tree
6889 coerce_template_parameter_pack (tree parms,
6890 int parm_idx,
6891 tree args,
6892 tree inner_args,
6893 int arg_idx,
6894 tree new_args,
6895 int* lost,
6896 tree in_decl,
6897 tsubst_flags_t complain)
6898 {
6899 tree parm = TREE_VEC_ELT (parms, parm_idx);
6900 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6901 tree packed_args;
6902 tree argument_pack;
6903 tree packed_parms = NULL_TREE;
6904
6905 if (arg_idx > nargs)
6906 arg_idx = nargs;
6907
6908 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
6909 {
6910 /* When the template parameter is a non-type template parameter pack
6911 or template template parameter pack whose type or template
6912 parameters use parameter packs, we know exactly how many arguments
6913 we are looking for. Build a vector of the instantiated decls for
6914 these template parameters in PACKED_PARMS. */
6915 /* We can't use make_pack_expansion here because it would interpret a
6916 _DECL as a use rather than a declaration. */
6917 tree decl = TREE_VALUE (parm);
6918 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
6919 SET_PACK_EXPANSION_PATTERN (exp, decl);
6920 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
6921 SET_TYPE_STRUCTURAL_EQUALITY (exp);
6922
6923 TREE_VEC_LENGTH (args)--;
6924 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
6925 TREE_VEC_LENGTH (args)++;
6926
6927 if (packed_parms == error_mark_node)
6928 return error_mark_node;
6929
6930 /* If we're doing a partial instantiation of a member template,
6931 verify that all of the types used for the non-type
6932 template parameter pack are, in fact, valid for non-type
6933 template parameters. */
6934 if (arg_idx < nargs
6935 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6936 {
6937 int j, len = TREE_VEC_LENGTH (packed_parms);
6938 for (j = 0; j < len; ++j)
6939 {
6940 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
6941 if (invalid_nontype_parm_type_p (t, complain))
6942 return error_mark_node;
6943 }
6944 /* We don't know how many args we have yet, just
6945 use the unconverted ones for now. */
6946 return NULL_TREE;
6947 }
6948
6949 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
6950 }
6951 else
6952 packed_args = make_tree_vec (nargs - arg_idx);
6953
6954 /* Convert the remaining arguments, which will be a part of the
6955 parameter pack "parm". */
6956 for (; arg_idx < nargs; ++arg_idx)
6957 {
6958 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6959 tree actual_parm = TREE_VALUE (parm);
6960 int pack_idx = arg_idx - parm_idx;
6961
6962 if (packed_parms)
6963 {
6964 /* Once we've packed as many args as we have types, stop. */
6965 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
6966 break;
6967 else if (PACK_EXPANSION_P (arg))
6968 /* We don't know how many args we have yet, just
6969 use the unconverted ones for now. */
6970 return NULL_TREE;
6971 else
6972 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
6973 }
6974
6975 if (arg == error_mark_node)
6976 {
6977 if (complain & tf_error)
6978 error ("template argument %d is invalid", arg_idx + 1);
6979 }
6980 else
6981 arg = convert_template_argument (actual_parm,
6982 arg, new_args, complain, parm_idx,
6983 in_decl);
6984 if (arg == error_mark_node)
6985 (*lost)++;
6986 TREE_VEC_ELT (packed_args, pack_idx) = arg;
6987 }
6988
6989 if (arg_idx - parm_idx < TREE_VEC_LENGTH (packed_args)
6990 && TREE_VEC_LENGTH (packed_args) > 0)
6991 {
6992 if (complain & tf_error)
6993 error ("wrong number of template arguments (%d, should be %d)",
6994 arg_idx - parm_idx, TREE_VEC_LENGTH (packed_args));
6995 return error_mark_node;
6996 }
6997
6998 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6999 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7000 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7001 else
7002 {
7003 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7004 TREE_TYPE (argument_pack)
7005 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7006 TREE_CONSTANT (argument_pack) = 1;
7007 }
7008
7009 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7010 #ifdef ENABLE_CHECKING
7011 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7012 TREE_VEC_LENGTH (packed_args));
7013 #endif
7014 return argument_pack;
7015 }
7016
7017 /* Returns the number of pack expansions in the template argument vector
7018 ARGS. */
7019
7020 static int
7021 pack_expansion_args_count (tree args)
7022 {
7023 int i;
7024 int count = 0;
7025 if (args)
7026 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7027 {
7028 tree elt = TREE_VEC_ELT (args, i);
7029 if (elt && PACK_EXPANSION_P (elt))
7030 ++count;
7031 }
7032 return count;
7033 }
7034
7035 /* Convert all template arguments to their appropriate types, and
7036 return a vector containing the innermost resulting template
7037 arguments. If any error occurs, return error_mark_node. Error and
7038 warning messages are issued under control of COMPLAIN.
7039
7040 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7041 for arguments not specified in ARGS. Otherwise, if
7042 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7043 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7044 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7045 ARGS. */
7046
7047 static tree
7048 coerce_template_parms (tree parms,
7049 tree args,
7050 tree in_decl,
7051 tsubst_flags_t complain,
7052 bool require_all_args,
7053 bool use_default_args)
7054 {
7055 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7056 tree orig_inner_args;
7057 tree inner_args;
7058 tree new_args;
7059 tree new_inner_args;
7060 int saved_unevaluated_operand;
7061 int saved_inhibit_evaluation_warnings;
7062
7063 /* When used as a boolean value, indicates whether this is a
7064 variadic template parameter list. Since it's an int, we can also
7065 subtract it from nparms to get the number of non-variadic
7066 parameters. */
7067 int variadic_p = 0;
7068 int variadic_args_p = 0;
7069 int post_variadic_parms = 0;
7070
7071 /* Likewise for parameters with default arguments. */
7072 int default_p = 0;
7073
7074 if (args == error_mark_node)
7075 return error_mark_node;
7076
7077 nparms = TREE_VEC_LENGTH (parms);
7078
7079 /* Determine if there are any parameter packs or default arguments. */
7080 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7081 {
7082 tree parm = TREE_VEC_ELT (parms, parm_idx);
7083 if (variadic_p)
7084 ++post_variadic_parms;
7085 if (template_parameter_pack_p (TREE_VALUE (parm)))
7086 ++variadic_p;
7087 if (TREE_PURPOSE (parm))
7088 ++default_p;
7089 }
7090
7091 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7092 /* If there are no parameters that follow a parameter pack, we need to
7093 expand any argument packs so that we can deduce a parameter pack from
7094 some non-packed args followed by an argument pack, as in variadic85.C.
7095 If there are such parameters, we need to leave argument packs intact
7096 so the arguments are assigned properly. This can happen when dealing
7097 with a nested class inside a partial specialization of a class
7098 template, as in variadic92.C, or when deducing a template parameter pack
7099 from a sub-declarator, as in variadic114.C. */
7100 if (!post_variadic_parms)
7101 inner_args = expand_template_argument_pack (inner_args);
7102
7103 /* Count any pack expansion args. */
7104 variadic_args_p = pack_expansion_args_count (inner_args);
7105
7106 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7107 if ((nargs > nparms && !variadic_p)
7108 || (nargs < nparms - variadic_p
7109 && require_all_args
7110 && !variadic_args_p
7111 && (!use_default_args
7112 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7113 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7114 {
7115 if (complain & tf_error)
7116 {
7117 if (variadic_p || default_p)
7118 {
7119 nparms -= variadic_p + default_p;
7120 error ("wrong number of template arguments "
7121 "(%d, should be at least %d)", nargs, nparms);
7122 }
7123 else
7124 error ("wrong number of template arguments "
7125 "(%d, should be %d)", nargs, nparms);
7126
7127 if (in_decl)
7128 inform (DECL_SOURCE_LOCATION (in_decl),
7129 "provided for %qD", in_decl);
7130 }
7131
7132 return error_mark_node;
7133 }
7134 /* We can't pass a pack expansion to a non-pack parameter of an alias
7135 template (DR 1430). */
7136 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
7137 && variadic_args_p
7138 && nargs - variadic_args_p < nparms - variadic_p)
7139 {
7140 if (complain & tf_error)
7141 {
7142 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7143 {
7144 tree arg = TREE_VEC_ELT (inner_args, i);
7145 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7146
7147 if (PACK_EXPANSION_P (arg)
7148 && !template_parameter_pack_p (parm))
7149 {
7150 error ("pack expansion argument for non-pack parameter "
7151 "%qD of alias template %qD", parm, in_decl);
7152 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7153 goto found;
7154 }
7155 }
7156 gcc_unreachable ();
7157 found:;
7158 }
7159 return error_mark_node;
7160 }
7161
7162 /* We need to evaluate the template arguments, even though this
7163 template-id may be nested within a "sizeof". */
7164 saved_unevaluated_operand = cp_unevaluated_operand;
7165 cp_unevaluated_operand = 0;
7166 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7167 c_inhibit_evaluation_warnings = 0;
7168 new_inner_args = make_tree_vec (nparms);
7169 new_args = add_outermost_template_args (args, new_inner_args);
7170 int pack_adjust = 0;
7171 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7172 {
7173 tree arg;
7174 tree parm;
7175
7176 /* Get the Ith template parameter. */
7177 parm = TREE_VEC_ELT (parms, parm_idx);
7178
7179 if (parm == error_mark_node)
7180 {
7181 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7182 continue;
7183 }
7184
7185 /* Calculate the next argument. */
7186 if (arg_idx < nargs)
7187 arg = TREE_VEC_ELT (inner_args, arg_idx);
7188 else
7189 arg = NULL_TREE;
7190
7191 if (template_parameter_pack_p (TREE_VALUE (parm))
7192 && !(arg && ARGUMENT_PACK_P (arg)))
7193 {
7194 /* Some arguments will be placed in the
7195 template parameter pack PARM. */
7196 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7197 inner_args, arg_idx,
7198 new_args, &lost,
7199 in_decl, complain);
7200
7201 if (arg == NULL_TREE)
7202 {
7203 /* We don't know how many args we have yet, just use the
7204 unconverted (and still packed) ones for now. */
7205 new_inner_args = orig_inner_args;
7206 arg_idx = nargs;
7207 break;
7208 }
7209
7210 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
7211
7212 /* Store this argument. */
7213 if (arg == error_mark_node)
7214 {
7215 lost++;
7216 /* We are done with all of the arguments. */
7217 arg_idx = nargs;
7218 }
7219 else
7220 {
7221 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
7222 arg_idx += pack_adjust;
7223 }
7224
7225 continue;
7226 }
7227 else if (arg)
7228 {
7229 if (PACK_EXPANSION_P (arg))
7230 {
7231 /* "If every valid specialization of a variadic template
7232 requires an empty template parameter pack, the template is
7233 ill-formed, no diagnostic required." So check that the
7234 pattern works with this parameter. */
7235 tree pattern = PACK_EXPANSION_PATTERN (arg);
7236 tree conv = convert_template_argument (TREE_VALUE (parm),
7237 pattern, new_args,
7238 complain, parm_idx,
7239 in_decl);
7240 if (conv == error_mark_node)
7241 {
7242 inform (input_location, "so any instantiation with a "
7243 "non-empty parameter pack would be ill-formed");
7244 ++lost;
7245 }
7246 else if (TYPE_P (conv) && !TYPE_P (pattern))
7247 /* Recover from missing typename. */
7248 TREE_VEC_ELT (inner_args, arg_idx)
7249 = make_pack_expansion (conv);
7250
7251 /* We don't know how many args we have yet, just
7252 use the unconverted ones for now. */
7253 new_inner_args = inner_args;
7254 arg_idx = nargs;
7255 break;
7256 }
7257 }
7258 else if (require_all_args)
7259 {
7260 /* There must be a default arg in this case. */
7261 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
7262 complain, in_decl);
7263 /* The position of the first default template argument,
7264 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
7265 Record that. */
7266 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7267 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7268 arg_idx - pack_adjust);
7269 }
7270 else
7271 break;
7272
7273 if (arg == error_mark_node)
7274 {
7275 if (complain & tf_error)
7276 error ("template argument %d is invalid", arg_idx + 1);
7277 }
7278 else if (!arg)
7279 /* This only occurs if there was an error in the template
7280 parameter list itself (which we would already have
7281 reported) that we are trying to recover from, e.g., a class
7282 template with a parameter list such as
7283 template<typename..., typename>. */
7284 ++lost;
7285 else
7286 arg = convert_template_argument (TREE_VALUE (parm),
7287 arg, new_args, complain,
7288 parm_idx, in_decl);
7289
7290 if (arg == error_mark_node)
7291 lost++;
7292 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
7293 }
7294 cp_unevaluated_operand = saved_unevaluated_operand;
7295 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
7296
7297 if (variadic_p && arg_idx < nargs)
7298 {
7299 if (complain & tf_error)
7300 {
7301 error ("wrong number of template arguments "
7302 "(%d, should be %d)", nargs, arg_idx);
7303 if (in_decl)
7304 error ("provided for %q+D", in_decl);
7305 }
7306 return error_mark_node;
7307 }
7308
7309 if (lost)
7310 return error_mark_node;
7311
7312 #ifdef ENABLE_CHECKING
7313 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
7314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
7315 TREE_VEC_LENGTH (new_inner_args));
7316 #endif
7317
7318 return new_inner_args;
7319 }
7320
7321 /* Like coerce_template_parms. If PARMS represents all template
7322 parameters levels, this function returns a vector of vectors
7323 representing all the resulting argument levels. Note that in this
7324 case, only the innermost arguments are coerced because the
7325 outermost ones are supposed to have been coerced already.
7326
7327 Otherwise, if PARMS represents only (the innermost) vector of
7328 parameters, this function returns a vector containing just the
7329 innermost resulting arguments. */
7330
7331 static tree
7332 coerce_innermost_template_parms (tree parms,
7333 tree args,
7334 tree in_decl,
7335 tsubst_flags_t complain,
7336 bool require_all_args,
7337 bool use_default_args)
7338 {
7339 int parms_depth = TMPL_PARMS_DEPTH (parms);
7340 int args_depth = TMPL_ARGS_DEPTH (args);
7341 tree coerced_args;
7342
7343 if (parms_depth > 1)
7344 {
7345 coerced_args = make_tree_vec (parms_depth);
7346 tree level;
7347 int cur_depth;
7348
7349 for (level = parms, cur_depth = parms_depth;
7350 parms_depth > 0 && level != NULL_TREE;
7351 level = TREE_CHAIN (level), --cur_depth)
7352 {
7353 tree l;
7354 if (cur_depth == args_depth)
7355 l = coerce_template_parms (TREE_VALUE (level),
7356 args, in_decl, complain,
7357 require_all_args,
7358 use_default_args);
7359 else
7360 l = TMPL_ARGS_LEVEL (args, cur_depth);
7361
7362 if (l == error_mark_node)
7363 return error_mark_node;
7364
7365 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
7366 }
7367 }
7368 else
7369 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
7370 args, in_decl, complain,
7371 require_all_args,
7372 use_default_args);
7373 return coerced_args;
7374 }
7375
7376 /* Returns 1 if template args OT and NT are equivalent. */
7377
7378 static int
7379 template_args_equal (tree ot, tree nt)
7380 {
7381 if (nt == ot)
7382 return 1;
7383 if (nt == NULL_TREE || ot == NULL_TREE)
7384 return false;
7385
7386 if (TREE_CODE (nt) == TREE_VEC)
7387 /* For member templates */
7388 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
7389 else if (PACK_EXPANSION_P (ot))
7390 return (PACK_EXPANSION_P (nt)
7391 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
7392 PACK_EXPANSION_PATTERN (nt))
7393 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
7394 PACK_EXPANSION_EXTRA_ARGS (nt)));
7395 else if (ARGUMENT_PACK_P (ot))
7396 {
7397 int i, len;
7398 tree opack, npack;
7399
7400 if (!ARGUMENT_PACK_P (nt))
7401 return 0;
7402
7403 opack = ARGUMENT_PACK_ARGS (ot);
7404 npack = ARGUMENT_PACK_ARGS (nt);
7405 len = TREE_VEC_LENGTH (opack);
7406 if (TREE_VEC_LENGTH (npack) != len)
7407 return 0;
7408 for (i = 0; i < len; ++i)
7409 if (!template_args_equal (TREE_VEC_ELT (opack, i),
7410 TREE_VEC_ELT (npack, i)))
7411 return 0;
7412 return 1;
7413 }
7414 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
7415 {
7416 /* We get here probably because we are in the middle of substituting
7417 into the pattern of a pack expansion. In that case the
7418 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7419 interested in. So we want to use the initial pack argument for
7420 the comparison. */
7421 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
7422 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
7423 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
7424 return template_args_equal (ot, nt);
7425 }
7426 else if (TYPE_P (nt))
7427 {
7428 if (!TYPE_P (ot))
7429 return false;
7430 /* Don't treat an alias template specialization with dependent
7431 arguments as equivalent to its underlying type when used as a
7432 template argument; we need them to be distinct so that we
7433 substitute into the specialization arguments at instantiation
7434 time. And aliases can't be equivalent without being ==, so
7435 we don't need to look any deeper. */
7436 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
7437 return false;
7438 else
7439 return same_type_p (ot, nt);
7440 }
7441 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
7442 return 0;
7443 else
7444 {
7445 /* Try to treat a template non-type argument that has been converted
7446 to the parameter type as equivalent to one that hasn't yet. */
7447 for (enum tree_code code1 = TREE_CODE (ot);
7448 CONVERT_EXPR_CODE_P (code1)
7449 || code1 == NON_LVALUE_EXPR;
7450 code1 = TREE_CODE (ot))
7451 ot = TREE_OPERAND (ot, 0);
7452 for (enum tree_code code2 = TREE_CODE (nt);
7453 CONVERT_EXPR_CODE_P (code2)
7454 || code2 == NON_LVALUE_EXPR;
7455 code2 = TREE_CODE (nt))
7456 nt = TREE_OPERAND (nt, 0);
7457
7458 return cp_tree_equal (ot, nt);
7459 }
7460 }
7461
7462 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7463 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7464 NEWARG_PTR with the offending arguments if they are non-NULL. */
7465
7466 static int
7467 comp_template_args_with_info (tree oldargs, tree newargs,
7468 tree *oldarg_ptr, tree *newarg_ptr)
7469 {
7470 int i;
7471
7472 if (oldargs == newargs)
7473 return 1;
7474
7475 if (!oldargs || !newargs)
7476 return 0;
7477
7478 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7479 return 0;
7480
7481 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7482 {
7483 tree nt = TREE_VEC_ELT (newargs, i);
7484 tree ot = TREE_VEC_ELT (oldargs, i);
7485
7486 if (! template_args_equal (ot, nt))
7487 {
7488 if (oldarg_ptr != NULL)
7489 *oldarg_ptr = ot;
7490 if (newarg_ptr != NULL)
7491 *newarg_ptr = nt;
7492 return 0;
7493 }
7494 }
7495 return 1;
7496 }
7497
7498 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7499 of template arguments. Returns 0 otherwise. */
7500
7501 int
7502 comp_template_args (tree oldargs, tree newargs)
7503 {
7504 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7505 }
7506
7507 static void
7508 add_pending_template (tree d)
7509 {
7510 tree ti = (TYPE_P (d)
7511 ? CLASSTYPE_TEMPLATE_INFO (d)
7512 : DECL_TEMPLATE_INFO (d));
7513 struct pending_template *pt;
7514 int level;
7515
7516 if (TI_PENDING_TEMPLATE_FLAG (ti))
7517 return;
7518
7519 /* We are called both from instantiate_decl, where we've already had a
7520 tinst_level pushed, and instantiate_template, where we haven't.
7521 Compensate. */
7522 level = !current_tinst_level || current_tinst_level->decl != d;
7523
7524 if (level)
7525 push_tinst_level (d);
7526
7527 pt = ggc_alloc<pending_template> ();
7528 pt->next = NULL;
7529 pt->tinst = current_tinst_level;
7530 if (last_pending_template)
7531 last_pending_template->next = pt;
7532 else
7533 pending_templates = pt;
7534
7535 last_pending_template = pt;
7536
7537 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7538
7539 if (level)
7540 pop_tinst_level ();
7541 }
7542
7543
7544 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7545 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7546 documentation for TEMPLATE_ID_EXPR. */
7547
7548 tree
7549 lookup_template_function (tree fns, tree arglist)
7550 {
7551 tree type;
7552
7553 if (fns == error_mark_node || arglist == error_mark_node)
7554 return error_mark_node;
7555
7556 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7557
7558 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7559 {
7560 error ("%q#D is not a function template", fns);
7561 return error_mark_node;
7562 }
7563
7564 if (BASELINK_P (fns))
7565 {
7566 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7567 unknown_type_node,
7568 BASELINK_FUNCTIONS (fns),
7569 arglist);
7570 return fns;
7571 }
7572
7573 type = TREE_TYPE (fns);
7574 if (TREE_CODE (fns) == OVERLOAD || !type)
7575 type = unknown_type_node;
7576
7577 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7578 }
7579
7580 /* Within the scope of a template class S<T>, the name S gets bound
7581 (in build_self_reference) to a TYPE_DECL for the class, not a
7582 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7583 or one of its enclosing classes, and that type is a template,
7584 return the associated TEMPLATE_DECL. Otherwise, the original
7585 DECL is returned.
7586
7587 Also handle the case when DECL is a TREE_LIST of ambiguous
7588 injected-class-names from different bases. */
7589
7590 tree
7591 maybe_get_template_decl_from_type_decl (tree decl)
7592 {
7593 if (decl == NULL_TREE)
7594 return decl;
7595
7596 /* DR 176: A lookup that finds an injected-class-name (10.2
7597 [class.member.lookup]) can result in an ambiguity in certain cases
7598 (for example, if it is found in more than one base class). If all of
7599 the injected-class-names that are found refer to specializations of
7600 the same class template, and if the name is followed by a
7601 template-argument-list, the reference refers to the class template
7602 itself and not a specialization thereof, and is not ambiguous. */
7603 if (TREE_CODE (decl) == TREE_LIST)
7604 {
7605 tree t, tmpl = NULL_TREE;
7606 for (t = decl; t; t = TREE_CHAIN (t))
7607 {
7608 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7609 if (!tmpl)
7610 tmpl = elt;
7611 else if (tmpl != elt)
7612 break;
7613 }
7614 if (tmpl && t == NULL_TREE)
7615 return tmpl;
7616 else
7617 return decl;
7618 }
7619
7620 return (decl != NULL_TREE
7621 && DECL_SELF_REFERENCE_P (decl)
7622 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7623 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7624 }
7625
7626 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7627 parameters, find the desired type.
7628
7629 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7630
7631 IN_DECL, if non-NULL, is the template declaration we are trying to
7632 instantiate.
7633
7634 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7635 the class we are looking up.
7636
7637 Issue error and warning messages under control of COMPLAIN.
7638
7639 If the template class is really a local class in a template
7640 function, then the FUNCTION_CONTEXT is the function in which it is
7641 being instantiated.
7642
7643 ??? Note that this function is currently called *twice* for each
7644 template-id: the first time from the parser, while creating the
7645 incomplete type (finish_template_type), and the second type during the
7646 real instantiation (instantiate_template_class). This is surely something
7647 that we want to avoid. It also causes some problems with argument
7648 coercion (see convert_nontype_argument for more information on this). */
7649
7650 static tree
7651 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7652 int entering_scope, tsubst_flags_t complain)
7653 {
7654 tree templ = NULL_TREE, parmlist;
7655 tree t;
7656 spec_entry **slot;
7657 spec_entry *entry;
7658 spec_entry elt;
7659 hashval_t hash;
7660
7661 if (identifier_p (d1))
7662 {
7663 tree value = innermost_non_namespace_value (d1);
7664 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7665 templ = value;
7666 else
7667 {
7668 if (context)
7669 push_decl_namespace (context);
7670 templ = lookup_name (d1);
7671 templ = maybe_get_template_decl_from_type_decl (templ);
7672 if (context)
7673 pop_decl_namespace ();
7674 }
7675 if (templ)
7676 context = DECL_CONTEXT (templ);
7677 }
7678 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7679 {
7680 tree type = TREE_TYPE (d1);
7681
7682 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7683 an implicit typename for the second A. Deal with it. */
7684 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7685 type = TREE_TYPE (type);
7686
7687 if (CLASSTYPE_TEMPLATE_INFO (type))
7688 {
7689 templ = CLASSTYPE_TI_TEMPLATE (type);
7690 d1 = DECL_NAME (templ);
7691 }
7692 }
7693 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7694 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7695 {
7696 templ = TYPE_TI_TEMPLATE (d1);
7697 d1 = DECL_NAME (templ);
7698 }
7699 else if (DECL_TYPE_TEMPLATE_P (d1))
7700 {
7701 templ = d1;
7702 d1 = DECL_NAME (templ);
7703 context = DECL_CONTEXT (templ);
7704 }
7705 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7706 {
7707 templ = d1;
7708 d1 = DECL_NAME (templ);
7709 }
7710
7711 /* Issue an error message if we didn't find a template. */
7712 if (! templ)
7713 {
7714 if (complain & tf_error)
7715 error ("%qT is not a template", d1);
7716 return error_mark_node;
7717 }
7718
7719 if (TREE_CODE (templ) != TEMPLATE_DECL
7720 /* Make sure it's a user visible template, if it was named by
7721 the user. */
7722 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7723 && !PRIMARY_TEMPLATE_P (templ)))
7724 {
7725 if (complain & tf_error)
7726 {
7727 error ("non-template type %qT used as a template", d1);
7728 if (in_decl)
7729 error ("for template declaration %q+D", in_decl);
7730 }
7731 return error_mark_node;
7732 }
7733
7734 complain &= ~tf_user;
7735
7736 /* An alias that just changes the name of a template is equivalent to the
7737 other template, so if any of the arguments are pack expansions, strip
7738 the alias to avoid problems with a pack expansion passed to a non-pack
7739 alias template parameter (DR 1430). */
7740 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7741 templ = get_underlying_template (templ);
7742
7743 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7744 {
7745 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7746 template arguments */
7747
7748 tree parm;
7749 tree arglist2;
7750 tree outer;
7751
7752 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7753
7754 /* Consider an example where a template template parameter declared as
7755
7756 template <class T, class U = std::allocator<T> > class TT
7757
7758 The template parameter level of T and U are one level larger than
7759 of TT. To proper process the default argument of U, say when an
7760 instantiation `TT<int>' is seen, we need to build the full
7761 arguments containing {int} as the innermost level. Outer levels,
7762 available when not appearing as default template argument, can be
7763 obtained from the arguments of the enclosing template.
7764
7765 Suppose that TT is later substituted with std::vector. The above
7766 instantiation is `TT<int, std::allocator<T> >' with TT at
7767 level 1, and T at level 2, while the template arguments at level 1
7768 becomes {std::vector} and the inner level 2 is {int}. */
7769
7770 outer = DECL_CONTEXT (templ);
7771 if (outer)
7772 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7773 else if (current_template_parms)
7774 {
7775 /* This is an argument of the current template, so we haven't set
7776 DECL_CONTEXT yet. */
7777 tree relevant_template_parms;
7778
7779 /* Parameter levels that are greater than the level of the given
7780 template template parm are irrelevant. */
7781 relevant_template_parms = current_template_parms;
7782 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7783 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7784 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7785
7786 outer = template_parms_to_args (relevant_template_parms);
7787 }
7788
7789 if (outer)
7790 arglist = add_to_template_args (outer, arglist);
7791
7792 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7793 complain,
7794 /*require_all_args=*/true,
7795 /*use_default_args=*/true);
7796 if (arglist2 == error_mark_node
7797 || (!uses_template_parms (arglist2)
7798 && check_instantiated_args (templ, arglist2, complain)))
7799 return error_mark_node;
7800
7801 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7802 return parm;
7803 }
7804 else
7805 {
7806 tree template_type = TREE_TYPE (templ);
7807 tree gen_tmpl;
7808 tree type_decl;
7809 tree found = NULL_TREE;
7810 int arg_depth;
7811 int parm_depth;
7812 int is_dependent_type;
7813 int use_partial_inst_tmpl = false;
7814
7815 if (template_type == error_mark_node)
7816 /* An error occurred while building the template TEMPL, and a
7817 diagnostic has most certainly been emitted for that
7818 already. Let's propagate that error. */
7819 return error_mark_node;
7820
7821 gen_tmpl = most_general_template (templ);
7822 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7823 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7824 arg_depth = TMPL_ARGS_DEPTH (arglist);
7825
7826 if (arg_depth == 1 && parm_depth > 1)
7827 {
7828 /* We've been given an incomplete set of template arguments.
7829 For example, given:
7830
7831 template <class T> struct S1 {
7832 template <class U> struct S2 {};
7833 template <class U> struct S2<U*> {};
7834 };
7835
7836 we will be called with an ARGLIST of `U*', but the
7837 TEMPLATE will be `template <class T> template
7838 <class U> struct S1<T>::S2'. We must fill in the missing
7839 arguments. */
7840 arglist
7841 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7842 arglist);
7843 arg_depth = TMPL_ARGS_DEPTH (arglist);
7844 }
7845
7846 /* Now we should have enough arguments. */
7847 gcc_assert (parm_depth == arg_depth);
7848
7849 /* From here on, we're only interested in the most general
7850 template. */
7851
7852 /* Calculate the BOUND_ARGS. These will be the args that are
7853 actually tsubst'd into the definition to create the
7854 instantiation. */
7855 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
7856 complain,
7857 /*require_all_args=*/true,
7858 /*use_default_args=*/true);
7859
7860 if (arglist == error_mark_node)
7861 /* We were unable to bind the arguments. */
7862 return error_mark_node;
7863
7864 /* In the scope of a template class, explicit references to the
7865 template class refer to the type of the template, not any
7866 instantiation of it. For example, in:
7867
7868 template <class T> class C { void f(C<T>); }
7869
7870 the `C<T>' is just the same as `C'. Outside of the
7871 class, however, such a reference is an instantiation. */
7872 if ((entering_scope
7873 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7874 || currently_open_class (template_type))
7875 /* comp_template_args is expensive, check it last. */
7876 && comp_template_args (TYPE_TI_ARGS (template_type),
7877 arglist))
7878 return template_type;
7879
7880 /* If we already have this specialization, return it. */
7881 elt.tmpl = gen_tmpl;
7882 elt.args = arglist;
7883 elt.spec = NULL_TREE;
7884 hash = spec_hasher::hash (&elt);
7885 entry = type_specializations->find_with_hash (&elt, hash);
7886
7887 if (entry)
7888 return entry->spec;
7889
7890 is_dependent_type = uses_template_parms (arglist);
7891
7892 /* If the deduced arguments are invalid, then the binding
7893 failed. */
7894 if (!is_dependent_type
7895 && check_instantiated_args (gen_tmpl,
7896 INNERMOST_TEMPLATE_ARGS (arglist),
7897 complain))
7898 return error_mark_node;
7899
7900 if (!is_dependent_type
7901 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7902 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7903 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7904 {
7905 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7906 DECL_NAME (gen_tmpl),
7907 /*tag_scope=*/ts_global);
7908 return found;
7909 }
7910
7911 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7912 complain, in_decl);
7913 if (context == error_mark_node)
7914 return error_mark_node;
7915
7916 if (!context)
7917 context = global_namespace;
7918
7919 /* Create the type. */
7920 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7921 {
7922 /* The user referred to a specialization of an alias
7923 template represented by GEN_TMPL.
7924
7925 [temp.alias]/2 says:
7926
7927 When a template-id refers to the specialization of an
7928 alias template, it is equivalent to the associated
7929 type obtained by substitution of its
7930 template-arguments for the template-parameters in the
7931 type-id of the alias template. */
7932
7933 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7934 /* Note that the call above (by indirectly calling
7935 register_specialization in tsubst_decl) registers the
7936 TYPE_DECL representing the specialization of the alias
7937 template. So next time someone substitutes ARGLIST for
7938 the template parms into the alias template (GEN_TMPL),
7939 she'll get that TYPE_DECL back. */
7940
7941 if (t == error_mark_node)
7942 return t;
7943 }
7944 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7945 {
7946 if (!is_dependent_type)
7947 {
7948 set_current_access_from_decl (TYPE_NAME (template_type));
7949 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7950 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7951 arglist, complain, in_decl),
7952 SCOPED_ENUM_P (template_type), NULL);
7953
7954 if (t == error_mark_node)
7955 return t;
7956 }
7957 else
7958 {
7959 /* We don't want to call start_enum for this type, since
7960 the values for the enumeration constants may involve
7961 template parameters. And, no one should be interested
7962 in the enumeration constants for such a type. */
7963 t = cxx_make_type (ENUMERAL_TYPE);
7964 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7965 }
7966 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7967 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7968 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7969 }
7970 else if (CLASS_TYPE_P (template_type))
7971 {
7972 t = make_class_type (TREE_CODE (template_type));
7973 CLASSTYPE_DECLARED_CLASS (t)
7974 = CLASSTYPE_DECLARED_CLASS (template_type);
7975 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7976 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7977
7978 /* A local class. Make sure the decl gets registered properly. */
7979 if (context == current_function_decl)
7980 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7981
7982 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7983 /* This instantiation is another name for the primary
7984 template type. Set the TYPE_CANONICAL field
7985 appropriately. */
7986 TYPE_CANONICAL (t) = template_type;
7987 else if (any_template_arguments_need_structural_equality_p (arglist))
7988 /* Some of the template arguments require structural
7989 equality testing, so this template class requires
7990 structural equality testing. */
7991 SET_TYPE_STRUCTURAL_EQUALITY (t);
7992 }
7993 else
7994 gcc_unreachable ();
7995
7996 /* If we called start_enum or pushtag above, this information
7997 will already be set up. */
7998 if (!TYPE_NAME (t))
7999 {
8000 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8001
8002 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8003 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8004 DECL_SOURCE_LOCATION (type_decl)
8005 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8006 }
8007 else
8008 type_decl = TYPE_NAME (t);
8009
8010 if (CLASS_TYPE_P (template_type))
8011 {
8012 TREE_PRIVATE (type_decl)
8013 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8014 TREE_PROTECTED (type_decl)
8015 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8016 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8017 {
8018 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8019 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8020 }
8021 }
8022
8023 if (OVERLOAD_TYPE_P (t)
8024 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8025 {
8026 static const char *tags[] = {"abi_tag", "may_alias"};
8027
8028 for (unsigned ix = 0; ix != 2; ix++)
8029 {
8030 tree attributes
8031 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8032
8033 if (!attributes)
8034 ;
8035 else if (!TREE_CHAIN (attributes) && !TYPE_ATTRIBUTES (t))
8036 TYPE_ATTRIBUTES (t) = attributes;
8037 else
8038 TYPE_ATTRIBUTES (t)
8039 = tree_cons (TREE_PURPOSE (attributes),
8040 TREE_VALUE (attributes),
8041 TYPE_ATTRIBUTES (t));
8042 }
8043 }
8044
8045 /* Let's consider the explicit specialization of a member
8046 of a class template specialization that is implicitly instantiated,
8047 e.g.:
8048 template<class T>
8049 struct S
8050 {
8051 template<class U> struct M {}; //#0
8052 };
8053
8054 template<>
8055 template<>
8056 struct S<int>::M<char> //#1
8057 {
8058 int i;
8059 };
8060 [temp.expl.spec]/4 says this is valid.
8061
8062 In this case, when we write:
8063 S<int>::M<char> m;
8064
8065 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8066 the one of #0.
8067
8068 When we encounter #1, we want to store the partial instantiation
8069 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8070
8071 For all cases other than this "explicit specialization of member of a
8072 class template", we just want to store the most general template into
8073 the CLASSTYPE_TI_TEMPLATE of M.
8074
8075 This case of "explicit specialization of member of a class template"
8076 only happens when:
8077 1/ the enclosing class is an instantiation of, and therefore not
8078 the same as, the context of the most general template, and
8079 2/ we aren't looking at the partial instantiation itself, i.e.
8080 the innermost arguments are not the same as the innermost parms of
8081 the most general template.
8082
8083 So it's only when 1/ and 2/ happens that we want to use the partial
8084 instantiation of the member template in lieu of its most general
8085 template. */
8086
8087 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8088 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8089 /* the enclosing class must be an instantiation... */
8090 && CLASS_TYPE_P (context)
8091 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8092 {
8093 tree partial_inst_args;
8094 TREE_VEC_LENGTH (arglist)--;
8095 ++processing_template_decl;
8096 partial_inst_args =
8097 tsubst (INNERMOST_TEMPLATE_ARGS
8098 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
8099 arglist, complain, NULL_TREE);
8100 --processing_template_decl;
8101 TREE_VEC_LENGTH (arglist)++;
8102 use_partial_inst_tmpl =
8103 /*...and we must not be looking at the partial instantiation
8104 itself. */
8105 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8106 partial_inst_args);
8107 }
8108
8109 if (!use_partial_inst_tmpl)
8110 /* This case is easy; there are no member templates involved. */
8111 found = gen_tmpl;
8112 else
8113 {
8114 /* This is a full instantiation of a member template. Find
8115 the partial instantiation of which this is an instance. */
8116
8117 /* Temporarily reduce by one the number of levels in the ARGLIST
8118 so as to avoid comparing the last set of arguments. */
8119 TREE_VEC_LENGTH (arglist)--;
8120 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8121 TREE_VEC_LENGTH (arglist)++;
8122 /* FOUND is either a proper class type, or an alias
8123 template specialization. In the later case, it's a
8124 TYPE_DECL, resulting from the substituting of arguments
8125 for parameters in the TYPE_DECL of the alias template
8126 done earlier. So be careful while getting the template
8127 of FOUND. */
8128 found = TREE_CODE (found) == TYPE_DECL
8129 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
8130 : CLASSTYPE_TI_TEMPLATE (found);
8131 }
8132
8133 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8134
8135 elt.spec = t;
8136 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8137 entry = ggc_alloc<spec_entry> ();
8138 *entry = elt;
8139 *slot = entry;
8140
8141 /* Note this use of the partial instantiation so we can check it
8142 later in maybe_process_partial_specialization. */
8143 DECL_TEMPLATE_INSTANTIATIONS (found)
8144 = tree_cons (arglist, t,
8145 DECL_TEMPLATE_INSTANTIATIONS (found));
8146
8147 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8148 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8149 /* Now that the type has been registered on the instantiations
8150 list, we set up the enumerators. Because the enumeration
8151 constants may involve the enumeration type itself, we make
8152 sure to register the type first, and then create the
8153 constants. That way, doing tsubst_expr for the enumeration
8154 constants won't result in recursive calls here; we'll find
8155 the instantiation and exit above. */
8156 tsubst_enum (template_type, t, arglist);
8157
8158 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8159 /* If the type makes use of template parameters, the
8160 code that generates debugging information will crash. */
8161 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8162
8163 /* Possibly limit visibility based on template args. */
8164 TREE_PUBLIC (type_decl) = 1;
8165 determine_visibility (type_decl);
8166
8167 inherit_targ_abi_tags (t);
8168
8169 return t;
8170 }
8171 }
8172
8173 /* Wrapper for lookup_template_class_1. */
8174
8175 tree
8176 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8177 int entering_scope, tsubst_flags_t complain)
8178 {
8179 tree ret;
8180 timevar_push (TV_TEMPLATE_INST);
8181 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8182 entering_scope, complain);
8183 timevar_pop (TV_TEMPLATE_INST);
8184 return ret;
8185 }
8186
8187 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.
8188 The type of the expression is the unknown_type_node since the
8189 template-id could refer to an explicit or partial specialization. */
8190
8191 tree
8192 lookup_template_variable (tree templ, tree arglist)
8193 {
8194 tree type = NULL_TREE;
8195 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8196 }
8197
8198 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8199
8200 tree
8201 finish_template_variable (tree var, tsubst_flags_t complain)
8202 {
8203 tree templ = TREE_OPERAND (var, 0);
8204
8205 tree arglist = TREE_OPERAND (var, 1);
8206 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
8207 arglist = add_outermost_template_args (tmpl_args, arglist);
8208
8209 tree parms = DECL_TEMPLATE_PARMS (templ);
8210 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
8211 /*req_all*/true,
8212 /*use_default*/true);
8213
8214 return instantiate_template (templ, arglist, complain);
8215 }
8216 \f
8217 struct pair_fn_data
8218 {
8219 tree_fn_t fn;
8220 void *data;
8221 /* True when we should also visit template parameters that occur in
8222 non-deduced contexts. */
8223 bool include_nondeduced_p;
8224 hash_set<tree> *visited;
8225 };
8226
8227 /* Called from for_each_template_parm via walk_tree. */
8228
8229 static tree
8230 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8231 {
8232 tree t = *tp;
8233 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
8234 tree_fn_t fn = pfd->fn;
8235 void *data = pfd->data;
8236
8237 if (TYPE_P (t)
8238 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
8239 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
8240 pfd->include_nondeduced_p))
8241 return error_mark_node;
8242
8243 switch (TREE_CODE (t))
8244 {
8245 case RECORD_TYPE:
8246 if (TYPE_PTRMEMFUNC_P (t))
8247 break;
8248 /* Fall through. */
8249
8250 case UNION_TYPE:
8251 case ENUMERAL_TYPE:
8252 if (!TYPE_TEMPLATE_INFO (t))
8253 *walk_subtrees = 0;
8254 else if (for_each_template_parm (TYPE_TI_ARGS (t),
8255 fn, data, pfd->visited,
8256 pfd->include_nondeduced_p))
8257 return error_mark_node;
8258 break;
8259
8260 case INTEGER_TYPE:
8261 if (for_each_template_parm (TYPE_MIN_VALUE (t),
8262 fn, data, pfd->visited,
8263 pfd->include_nondeduced_p)
8264 || for_each_template_parm (TYPE_MAX_VALUE (t),
8265 fn, data, pfd->visited,
8266 pfd->include_nondeduced_p))
8267 return error_mark_node;
8268 break;
8269
8270 case METHOD_TYPE:
8271 /* Since we're not going to walk subtrees, we have to do this
8272 explicitly here. */
8273 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
8274 pfd->visited, pfd->include_nondeduced_p))
8275 return error_mark_node;
8276 /* Fall through. */
8277
8278 case FUNCTION_TYPE:
8279 /* Check the return type. */
8280 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8281 pfd->include_nondeduced_p))
8282 return error_mark_node;
8283
8284 /* Check the parameter types. Since default arguments are not
8285 instantiated until they are needed, the TYPE_ARG_TYPES may
8286 contain expressions that involve template parameters. But,
8287 no-one should be looking at them yet. And, once they're
8288 instantiated, they don't contain template parameters, so
8289 there's no point in looking at them then, either. */
8290 {
8291 tree parm;
8292
8293 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
8294 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
8295 pfd->visited, pfd->include_nondeduced_p))
8296 return error_mark_node;
8297
8298 /* Since we've already handled the TYPE_ARG_TYPES, we don't
8299 want walk_tree walking into them itself. */
8300 *walk_subtrees = 0;
8301 }
8302 break;
8303
8304 case TYPEOF_TYPE:
8305 case UNDERLYING_TYPE:
8306 if (pfd->include_nondeduced_p
8307 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
8308 pfd->visited,
8309 pfd->include_nondeduced_p))
8310 return error_mark_node;
8311 break;
8312
8313 case FUNCTION_DECL:
8314 case VAR_DECL:
8315 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
8316 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
8317 pfd->visited, pfd->include_nondeduced_p))
8318 return error_mark_node;
8319 /* Fall through. */
8320
8321 case PARM_DECL:
8322 case CONST_DECL:
8323 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
8324 && for_each_template_parm (DECL_INITIAL (t), fn, data,
8325 pfd->visited, pfd->include_nondeduced_p))
8326 return error_mark_node;
8327 if (DECL_CONTEXT (t)
8328 && pfd->include_nondeduced_p
8329 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
8330 pfd->visited, pfd->include_nondeduced_p))
8331 return error_mark_node;
8332 break;
8333
8334 case BOUND_TEMPLATE_TEMPLATE_PARM:
8335 /* Record template parameters such as `T' inside `TT<T>'. */
8336 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
8337 pfd->include_nondeduced_p))
8338 return error_mark_node;
8339 /* Fall through. */
8340
8341 case TEMPLATE_TEMPLATE_PARM:
8342 case TEMPLATE_TYPE_PARM:
8343 case TEMPLATE_PARM_INDEX:
8344 if (fn && (*fn)(t, data))
8345 return error_mark_node;
8346 else if (!fn)
8347 return error_mark_node;
8348 break;
8349
8350 case TEMPLATE_DECL:
8351 /* A template template parameter is encountered. */
8352 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
8353 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
8354 pfd->include_nondeduced_p))
8355 return error_mark_node;
8356
8357 /* Already substituted template template parameter */
8358 *walk_subtrees = 0;
8359 break;
8360
8361 case TYPENAME_TYPE:
8362 if (!fn
8363 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
8364 data, pfd->visited,
8365 pfd->include_nondeduced_p))
8366 return error_mark_node;
8367 break;
8368
8369 case CONSTRUCTOR:
8370 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
8371 && pfd->include_nondeduced_p
8372 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
8373 (TREE_TYPE (t)), fn, data,
8374 pfd->visited, pfd->include_nondeduced_p))
8375 return error_mark_node;
8376 break;
8377
8378 case INDIRECT_REF:
8379 case COMPONENT_REF:
8380 /* If there's no type, then this thing must be some expression
8381 involving template parameters. */
8382 if (!fn && !TREE_TYPE (t))
8383 return error_mark_node;
8384 break;
8385
8386 case MODOP_EXPR:
8387 case CAST_EXPR:
8388 case IMPLICIT_CONV_EXPR:
8389 case REINTERPRET_CAST_EXPR:
8390 case CONST_CAST_EXPR:
8391 case STATIC_CAST_EXPR:
8392 case DYNAMIC_CAST_EXPR:
8393 case ARROW_EXPR:
8394 case DOTSTAR_EXPR:
8395 case TYPEID_EXPR:
8396 case PSEUDO_DTOR_EXPR:
8397 if (!fn)
8398 return error_mark_node;
8399 break;
8400
8401 default:
8402 break;
8403 }
8404
8405 /* We didn't find any template parameters we liked. */
8406 return NULL_TREE;
8407 }
8408
8409 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
8410 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
8411 call FN with the parameter and the DATA.
8412 If FN returns nonzero, the iteration is terminated, and
8413 for_each_template_parm returns 1. Otherwise, the iteration
8414 continues. If FN never returns a nonzero value, the value
8415 returned by for_each_template_parm is 0. If FN is NULL, it is
8416 considered to be the function which always returns 1.
8417
8418 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
8419 parameters that occur in non-deduced contexts. When false, only
8420 visits those template parameters that can be deduced. */
8421
8422 static int
8423 for_each_template_parm (tree t, tree_fn_t fn, void* data,
8424 hash_set<tree> *visited,
8425 bool include_nondeduced_p)
8426 {
8427 struct pair_fn_data pfd;
8428 int result;
8429
8430 /* Set up. */
8431 pfd.fn = fn;
8432 pfd.data = data;
8433 pfd.include_nondeduced_p = include_nondeduced_p;
8434
8435 /* Walk the tree. (Conceptually, we would like to walk without
8436 duplicates, but for_each_template_parm_r recursively calls
8437 for_each_template_parm, so we would need to reorganize a fair
8438 bit to use walk_tree_without_duplicates, so we keep our own
8439 visited list.) */
8440 if (visited)
8441 pfd.visited = visited;
8442 else
8443 pfd.visited = new hash_set<tree>;
8444 result = cp_walk_tree (&t,
8445 for_each_template_parm_r,
8446 &pfd,
8447 pfd.visited) != NULL_TREE;
8448
8449 /* Clean up. */
8450 if (!visited)
8451 {
8452 delete pfd.visited;
8453 pfd.visited = 0;
8454 }
8455
8456 return result;
8457 }
8458
8459 /* Returns true if T depends on any template parameter. */
8460
8461 int
8462 uses_template_parms (tree t)
8463 {
8464 if (t == NULL_TREE)
8465 return false;
8466
8467 bool dependent_p;
8468 int saved_processing_template_decl;
8469
8470 saved_processing_template_decl = processing_template_decl;
8471 if (!saved_processing_template_decl)
8472 processing_template_decl = 1;
8473 if (TYPE_P (t))
8474 dependent_p = dependent_type_p (t);
8475 else if (TREE_CODE (t) == TREE_VEC)
8476 dependent_p = any_dependent_template_arguments_p (t);
8477 else if (TREE_CODE (t) == TREE_LIST)
8478 dependent_p = (uses_template_parms (TREE_VALUE (t))
8479 || uses_template_parms (TREE_CHAIN (t)));
8480 else if (TREE_CODE (t) == TYPE_DECL)
8481 dependent_p = dependent_type_p (TREE_TYPE (t));
8482 else if (DECL_P (t)
8483 || EXPR_P (t)
8484 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
8485 || TREE_CODE (t) == OVERLOAD
8486 || BASELINK_P (t)
8487 || identifier_p (t)
8488 || TREE_CODE (t) == TRAIT_EXPR
8489 || TREE_CODE (t) == CONSTRUCTOR
8490 || CONSTANT_CLASS_P (t))
8491 dependent_p = (type_dependent_expression_p (t)
8492 || value_dependent_expression_p (t));
8493 else
8494 {
8495 gcc_assert (t == error_mark_node);
8496 dependent_p = false;
8497 }
8498
8499 processing_template_decl = saved_processing_template_decl;
8500
8501 return dependent_p;
8502 }
8503
8504 /* Returns true iff current_function_decl is an incompletely instantiated
8505 template. Useful instead of processing_template_decl because the latter
8506 is set to 0 during instantiate_non_dependent_expr. */
8507
8508 bool
8509 in_template_function (void)
8510 {
8511 tree fn = current_function_decl;
8512 bool ret;
8513 ++processing_template_decl;
8514 ret = (fn && DECL_LANG_SPECIFIC (fn)
8515 && DECL_TEMPLATE_INFO (fn)
8516 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8517 --processing_template_decl;
8518 return ret;
8519 }
8520
8521 /* Returns true if T depends on any template parameter with level LEVEL. */
8522
8523 int
8524 uses_template_parms_level (tree t, int level)
8525 {
8526 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8527 /*include_nondeduced_p=*/true);
8528 }
8529
8530 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8531 ill-formed translation unit, i.e. a variable or function that isn't
8532 usable in a constant expression. */
8533
8534 static inline bool
8535 neglectable_inst_p (tree d)
8536 {
8537 return (DECL_P (d)
8538 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8539 : decl_maybe_constant_var_p (d)));
8540 }
8541
8542 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8543 neglectable and instantiated from within an erroneous instantiation. */
8544
8545 static bool
8546 limit_bad_template_recursion (tree decl)
8547 {
8548 struct tinst_level *lev = current_tinst_level;
8549 int errs = errorcount + sorrycount;
8550 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8551 return false;
8552
8553 for (; lev; lev = lev->next)
8554 if (neglectable_inst_p (lev->decl))
8555 break;
8556
8557 return (lev && errs > lev->errors);
8558 }
8559
8560 static int tinst_depth;
8561 extern int max_tinst_depth;
8562 int depth_reached;
8563
8564 static GTY(()) struct tinst_level *last_error_tinst_level;
8565
8566 /* We're starting to instantiate D; record the template instantiation context
8567 for diagnostics and to restore it later. */
8568
8569 bool
8570 push_tinst_level (tree d)
8571 {
8572 return push_tinst_level_loc (d, input_location);
8573 }
8574
8575 /* We're starting to instantiate D; record the template instantiation context
8576 at LOC for diagnostics and to restore it later. */
8577
8578 bool
8579 push_tinst_level_loc (tree d, location_t loc)
8580 {
8581 struct tinst_level *new_level;
8582
8583 if (tinst_depth >= max_tinst_depth)
8584 {
8585 fatal_error (input_location,
8586 "template instantiation depth exceeds maximum of %d"
8587 " (use -ftemplate-depth= to increase the maximum)",
8588 max_tinst_depth);
8589 return false;
8590 }
8591
8592 /* If the current instantiation caused problems, don't let it instantiate
8593 anything else. Do allow deduction substitution and decls usable in
8594 constant expressions. */
8595 if (limit_bad_template_recursion (d))
8596 return false;
8597
8598 new_level = ggc_alloc<tinst_level> ();
8599 new_level->decl = d;
8600 new_level->locus = loc;
8601 new_level->errors = errorcount+sorrycount;
8602 new_level->in_system_header_p = in_system_header_at (input_location);
8603 new_level->next = current_tinst_level;
8604 current_tinst_level = new_level;
8605
8606 ++tinst_depth;
8607 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8608 depth_reached = tinst_depth;
8609
8610 return true;
8611 }
8612
8613 /* We're done instantiating this template; return to the instantiation
8614 context. */
8615
8616 void
8617 pop_tinst_level (void)
8618 {
8619 /* Restore the filename and line number stashed away when we started
8620 this instantiation. */
8621 input_location = current_tinst_level->locus;
8622 current_tinst_level = current_tinst_level->next;
8623 --tinst_depth;
8624 }
8625
8626 /* We're instantiating a deferred template; restore the template
8627 instantiation context in which the instantiation was requested, which
8628 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8629
8630 static tree
8631 reopen_tinst_level (struct tinst_level *level)
8632 {
8633 struct tinst_level *t;
8634
8635 tinst_depth = 0;
8636 for (t = level; t; t = t->next)
8637 ++tinst_depth;
8638
8639 current_tinst_level = level;
8640 pop_tinst_level ();
8641 if (current_tinst_level)
8642 current_tinst_level->errors = errorcount+sorrycount;
8643 return level->decl;
8644 }
8645
8646 /* Returns the TINST_LEVEL which gives the original instantiation
8647 context. */
8648
8649 struct tinst_level *
8650 outermost_tinst_level (void)
8651 {
8652 struct tinst_level *level = current_tinst_level;
8653 if (level)
8654 while (level->next)
8655 level = level->next;
8656 return level;
8657 }
8658
8659 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8660 vector of template arguments, as for tsubst.
8661
8662 Returns an appropriate tsubst'd friend declaration. */
8663
8664 static tree
8665 tsubst_friend_function (tree decl, tree args)
8666 {
8667 tree new_friend;
8668
8669 if (TREE_CODE (decl) == FUNCTION_DECL
8670 && DECL_TEMPLATE_INSTANTIATION (decl)
8671 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8672 /* This was a friend declared with an explicit template
8673 argument list, e.g.:
8674
8675 friend void f<>(T);
8676
8677 to indicate that f was a template instantiation, not a new
8678 function declaration. Now, we have to figure out what
8679 instantiation of what template. */
8680 {
8681 tree template_id, arglist, fns;
8682 tree new_args;
8683 tree tmpl;
8684 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8685
8686 /* Friend functions are looked up in the containing namespace scope.
8687 We must enter that scope, to avoid finding member functions of the
8688 current class with same name. */
8689 push_nested_namespace (ns);
8690 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8691 tf_warning_or_error, NULL_TREE,
8692 /*integral_constant_expression_p=*/false);
8693 pop_nested_namespace (ns);
8694 arglist = tsubst (DECL_TI_ARGS (decl), args,
8695 tf_warning_or_error, NULL_TREE);
8696 template_id = lookup_template_function (fns, arglist);
8697
8698 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8699 tmpl = determine_specialization (template_id, new_friend,
8700 &new_args,
8701 /*need_member_template=*/0,
8702 TREE_VEC_LENGTH (args),
8703 tsk_none);
8704 return instantiate_template (tmpl, new_args, tf_error);
8705 }
8706
8707 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8708
8709 /* The NEW_FRIEND will look like an instantiation, to the
8710 compiler, but is not an instantiation from the point of view of
8711 the language. For example, we might have had:
8712
8713 template <class T> struct S {
8714 template <class U> friend void f(T, U);
8715 };
8716
8717 Then, in S<int>, template <class U> void f(int, U) is not an
8718 instantiation of anything. */
8719 if (new_friend == error_mark_node)
8720 return error_mark_node;
8721
8722 DECL_USE_TEMPLATE (new_friend) = 0;
8723 if (TREE_CODE (decl) == TEMPLATE_DECL)
8724 {
8725 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8726 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8727 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8728 }
8729
8730 /* The mangled name for the NEW_FRIEND is incorrect. The function
8731 is not a template instantiation and should not be mangled like
8732 one. Therefore, we forget the mangling here; we'll recompute it
8733 later if we need it. */
8734 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8735 {
8736 SET_DECL_RTL (new_friend, NULL);
8737 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8738 }
8739
8740 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8741 {
8742 tree old_decl;
8743 tree new_friend_template_info;
8744 tree new_friend_result_template_info;
8745 tree ns;
8746 int new_friend_is_defn;
8747
8748 /* We must save some information from NEW_FRIEND before calling
8749 duplicate decls since that function will free NEW_FRIEND if
8750 possible. */
8751 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8752 new_friend_is_defn =
8753 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8754 (template_for_substitution (new_friend)))
8755 != NULL_TREE);
8756 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8757 {
8758 /* This declaration is a `primary' template. */
8759 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8760
8761 new_friend_result_template_info
8762 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8763 }
8764 else
8765 new_friend_result_template_info = NULL_TREE;
8766
8767 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8768 if (new_friend_is_defn)
8769 DECL_INITIAL (new_friend) = error_mark_node;
8770
8771 /* Inside pushdecl_namespace_level, we will push into the
8772 current namespace. However, the friend function should go
8773 into the namespace of the template. */
8774 ns = decl_namespace_context (new_friend);
8775 push_nested_namespace (ns);
8776 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8777 pop_nested_namespace (ns);
8778
8779 if (old_decl == error_mark_node)
8780 return error_mark_node;
8781
8782 if (old_decl != new_friend)
8783 {
8784 /* This new friend declaration matched an existing
8785 declaration. For example, given:
8786
8787 template <class T> void f(T);
8788 template <class U> class C {
8789 template <class T> friend void f(T) {}
8790 };
8791
8792 the friend declaration actually provides the definition
8793 of `f', once C has been instantiated for some type. So,
8794 old_decl will be the out-of-class template declaration,
8795 while new_friend is the in-class definition.
8796
8797 But, if `f' was called before this point, the
8798 instantiation of `f' will have DECL_TI_ARGS corresponding
8799 to `T' but not to `U', references to which might appear
8800 in the definition of `f'. Previously, the most general
8801 template for an instantiation of `f' was the out-of-class
8802 version; now it is the in-class version. Therefore, we
8803 run through all specialization of `f', adding to their
8804 DECL_TI_ARGS appropriately. In particular, they need a
8805 new set of outer arguments, corresponding to the
8806 arguments for this class instantiation.
8807
8808 The same situation can arise with something like this:
8809
8810 friend void f(int);
8811 template <class T> class C {
8812 friend void f(T) {}
8813 };
8814
8815 when `C<int>' is instantiated. Now, `f(int)' is defined
8816 in the class. */
8817
8818 if (!new_friend_is_defn)
8819 /* On the other hand, if the in-class declaration does
8820 *not* provide a definition, then we don't want to alter
8821 existing definitions. We can just leave everything
8822 alone. */
8823 ;
8824 else
8825 {
8826 tree new_template = TI_TEMPLATE (new_friend_template_info);
8827 tree new_args = TI_ARGS (new_friend_template_info);
8828
8829 /* Overwrite whatever template info was there before, if
8830 any, with the new template information pertaining to
8831 the declaration. */
8832 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8833
8834 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8835 {
8836 /* We should have called reregister_specialization in
8837 duplicate_decls. */
8838 gcc_assert (retrieve_specialization (new_template,
8839 new_args, 0)
8840 == old_decl);
8841
8842 /* Instantiate it if the global has already been used. */
8843 if (DECL_ODR_USED (old_decl))
8844 instantiate_decl (old_decl, /*defer_ok=*/true,
8845 /*expl_inst_class_mem_p=*/false);
8846 }
8847 else
8848 {
8849 tree t;
8850
8851 /* Indicate that the old function template is a partial
8852 instantiation. */
8853 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8854 = new_friend_result_template_info;
8855
8856 gcc_assert (new_template
8857 == most_general_template (new_template));
8858 gcc_assert (new_template != old_decl);
8859
8860 /* Reassign any specializations already in the hash table
8861 to the new more general template, and add the
8862 additional template args. */
8863 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8864 t != NULL_TREE;
8865 t = TREE_CHAIN (t))
8866 {
8867 tree spec = TREE_VALUE (t);
8868 spec_entry elt;
8869
8870 elt.tmpl = old_decl;
8871 elt.args = DECL_TI_ARGS (spec);
8872 elt.spec = NULL_TREE;
8873
8874 decl_specializations->remove_elt (&elt);
8875
8876 DECL_TI_ARGS (spec)
8877 = add_outermost_template_args (new_args,
8878 DECL_TI_ARGS (spec));
8879
8880 register_specialization
8881 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8882
8883 }
8884 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8885 }
8886 }
8887
8888 /* The information from NEW_FRIEND has been merged into OLD_DECL
8889 by duplicate_decls. */
8890 new_friend = old_decl;
8891 }
8892 }
8893 else
8894 {
8895 tree context = DECL_CONTEXT (new_friend);
8896 bool dependent_p;
8897
8898 /* In the code
8899 template <class T> class C {
8900 template <class U> friend void C1<U>::f (); // case 1
8901 friend void C2<T>::f (); // case 2
8902 };
8903 we only need to make sure CONTEXT is a complete type for
8904 case 2. To distinguish between the two cases, we note that
8905 CONTEXT of case 1 remains dependent type after tsubst while
8906 this isn't true for case 2. */
8907 ++processing_template_decl;
8908 dependent_p = dependent_type_p (context);
8909 --processing_template_decl;
8910
8911 if (!dependent_p
8912 && !complete_type_or_else (context, NULL_TREE))
8913 return error_mark_node;
8914
8915 if (COMPLETE_TYPE_P (context))
8916 {
8917 tree fn = new_friend;
8918 /* do_friend adds the TEMPLATE_DECL for any member friend
8919 template even if it isn't a member template, i.e.
8920 template <class T> friend A<T>::f();
8921 Look through it in that case. */
8922 if (TREE_CODE (fn) == TEMPLATE_DECL
8923 && !PRIMARY_TEMPLATE_P (fn))
8924 fn = DECL_TEMPLATE_RESULT (fn);
8925 /* Check to see that the declaration is really present, and,
8926 possibly obtain an improved declaration. */
8927 fn = check_classfn (context, fn, NULL_TREE);
8928
8929 if (fn)
8930 new_friend = fn;
8931 }
8932 }
8933
8934 return new_friend;
8935 }
8936
8937 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8938 template arguments, as for tsubst.
8939
8940 Returns an appropriate tsubst'd friend type or error_mark_node on
8941 failure. */
8942
8943 static tree
8944 tsubst_friend_class (tree friend_tmpl, tree args)
8945 {
8946 tree friend_type;
8947 tree tmpl;
8948 tree context;
8949
8950 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8951 {
8952 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8953 return TREE_TYPE (t);
8954 }
8955
8956 context = CP_DECL_CONTEXT (friend_tmpl);
8957
8958 if (context != global_namespace)
8959 {
8960 if (TREE_CODE (context) == NAMESPACE_DECL)
8961 push_nested_namespace (context);
8962 else
8963 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8964 }
8965
8966 /* Look for a class template declaration. We look for hidden names
8967 because two friend declarations of the same template are the
8968 same. For example, in:
8969
8970 struct A {
8971 template <typename> friend class F;
8972 };
8973 template <typename> struct B {
8974 template <typename> friend class F;
8975 };
8976
8977 both F templates are the same. */
8978 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8979 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8980
8981 /* But, if we don't find one, it might be because we're in a
8982 situation like this:
8983
8984 template <class T>
8985 struct S {
8986 template <class U>
8987 friend struct S;
8988 };
8989
8990 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8991 for `S<int>', not the TEMPLATE_DECL. */
8992 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8993 {
8994 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8995 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8996 }
8997
8998 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8999 {
9000 /* The friend template has already been declared. Just
9001 check to see that the declarations match, and install any new
9002 default parameters. We must tsubst the default parameters,
9003 of course. We only need the innermost template parameters
9004 because that is all that redeclare_class_template will look
9005 at. */
9006 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9007 > TMPL_ARGS_DEPTH (args))
9008 {
9009 tree parms;
9010 location_t saved_input_location;
9011 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9012 args, tf_warning_or_error);
9013
9014 saved_input_location = input_location;
9015 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9016 redeclare_class_template (TREE_TYPE (tmpl), parms);
9017 input_location = saved_input_location;
9018
9019 }
9020
9021 friend_type = TREE_TYPE (tmpl);
9022 }
9023 else
9024 {
9025 /* The friend template has not already been declared. In this
9026 case, the instantiation of the template class will cause the
9027 injection of this template into the global scope. */
9028 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9029 if (tmpl == error_mark_node)
9030 return error_mark_node;
9031
9032 /* The new TMPL is not an instantiation of anything, so we
9033 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9034 the new type because that is supposed to be the corresponding
9035 template decl, i.e., TMPL. */
9036 DECL_USE_TEMPLATE (tmpl) = 0;
9037 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9038 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9039 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9040 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9041
9042 /* Inject this template into the global scope. */
9043 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9044 }
9045
9046 if (context != global_namespace)
9047 {
9048 if (TREE_CODE (context) == NAMESPACE_DECL)
9049 pop_nested_namespace (context);
9050 else
9051 pop_nested_class ();
9052 }
9053
9054 return friend_type;
9055 }
9056
9057 /* Returns zero if TYPE cannot be completed later due to circularity.
9058 Otherwise returns one. */
9059
9060 static int
9061 can_complete_type_without_circularity (tree type)
9062 {
9063 if (type == NULL_TREE || type == error_mark_node)
9064 return 0;
9065 else if (COMPLETE_TYPE_P (type))
9066 return 1;
9067 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
9068 return can_complete_type_without_circularity (TREE_TYPE (type));
9069 else if (CLASS_TYPE_P (type)
9070 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9071 return 0;
9072 else
9073 return 1;
9074 }
9075
9076 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
9077
9078 /* Apply any attributes which had to be deferred until instantiation
9079 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
9080 ARGS, COMPLAIN, IN_DECL are as tsubst. */
9081
9082 static void
9083 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
9084 tree args, tsubst_flags_t complain, tree in_decl)
9085 {
9086 tree last_dep = NULL_TREE;
9087 tree t;
9088 tree *p;
9089
9090 for (t = attributes; t; t = TREE_CHAIN (t))
9091 if (ATTR_IS_DEPENDENT (t))
9092 {
9093 last_dep = t;
9094 attributes = copy_list (attributes);
9095 break;
9096 }
9097
9098 if (DECL_P (*decl_p))
9099 {
9100 if (TREE_TYPE (*decl_p) == error_mark_node)
9101 return;
9102 p = &DECL_ATTRIBUTES (*decl_p);
9103 }
9104 else
9105 p = &TYPE_ATTRIBUTES (*decl_p);
9106
9107 if (last_dep)
9108 {
9109 tree late_attrs = NULL_TREE;
9110 tree *q = &late_attrs;
9111
9112 for (*p = attributes; *p; )
9113 {
9114 t = *p;
9115 if (ATTR_IS_DEPENDENT (t))
9116 {
9117 *p = TREE_CHAIN (t);
9118 TREE_CHAIN (t) = NULL_TREE;
9119 if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9120 && is_attribute_p ("omp declare simd",
9121 get_attribute_name (t))
9122 && TREE_VALUE (t))
9123 {
9124 tree clauses = TREE_VALUE (TREE_VALUE (t));
9125 clauses = tsubst_omp_clauses (clauses, true, args,
9126 complain, in_decl);
9127 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9128 clauses = finish_omp_clauses (clauses);
9129 tree parms = DECL_ARGUMENTS (*decl_p);
9130 clauses
9131 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9132 if (clauses)
9133 TREE_VALUE (TREE_VALUE (t)) = clauses;
9134 else
9135 TREE_VALUE (t) = NULL_TREE;
9136 }
9137 /* If the first attribute argument is an identifier, don't
9138 pass it through tsubst. Attributes like mode, format,
9139 cleanup and several target specific attributes expect it
9140 unmodified. */
9141 else if (attribute_takes_identifier_p (get_attribute_name (t))
9142 && TREE_VALUE (t))
9143 {
9144 tree chain
9145 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
9146 in_decl,
9147 /*integral_constant_expression_p=*/false);
9148 if (chain != TREE_CHAIN (TREE_VALUE (t)))
9149 TREE_VALUE (t)
9150 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
9151 chain);
9152 }
9153 else if (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))
9154 {
9155 /* An attribute pack expansion. */
9156 tree purp = TREE_PURPOSE (t);
9157 tree pack = (tsubst_pack_expansion
9158 (TREE_VALUE (t), args, complain, in_decl));
9159 int len = TREE_VEC_LENGTH (pack);
9160 for (int i = 0; i < len; ++i)
9161 {
9162 tree elt = TREE_VEC_ELT (pack, i);
9163 *q = build_tree_list (purp, elt);
9164 q = &TREE_CHAIN (*q);
9165 }
9166 continue;
9167 }
9168 else
9169 TREE_VALUE (t)
9170 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
9171 /*integral_constant_expression_p=*/false);
9172 *q = t;
9173 q = &TREE_CHAIN (t);
9174 }
9175 else
9176 p = &TREE_CHAIN (t);
9177 }
9178
9179 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
9180 }
9181 }
9182
9183 /* Perform (or defer) access check for typedefs that were referenced
9184 from within the template TMPL code.
9185 This is a subroutine of instantiate_decl and instantiate_class_template.
9186 TMPL is the template to consider and TARGS is the list of arguments of
9187 that template. */
9188
9189 static void
9190 perform_typedefs_access_check (tree tmpl, tree targs)
9191 {
9192 location_t saved_location;
9193 unsigned i;
9194 qualified_typedef_usage_t *iter;
9195
9196 if (!tmpl
9197 || (!CLASS_TYPE_P (tmpl)
9198 && TREE_CODE (tmpl) != FUNCTION_DECL))
9199 return;
9200
9201 saved_location = input_location;
9202 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
9203 {
9204 tree type_decl = iter->typedef_decl;
9205 tree type_scope = iter->context;
9206
9207 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
9208 continue;
9209
9210 if (uses_template_parms (type_decl))
9211 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
9212 if (uses_template_parms (type_scope))
9213 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
9214
9215 /* Make access check error messages point to the location
9216 of the use of the typedef. */
9217 input_location = iter->locus;
9218 perform_or_defer_access_check (TYPE_BINFO (type_scope),
9219 type_decl, type_decl,
9220 tf_warning_or_error);
9221 }
9222 input_location = saved_location;
9223 }
9224
9225 static tree
9226 instantiate_class_template_1 (tree type)
9227 {
9228 tree templ, args, pattern, t, member;
9229 tree typedecl;
9230 tree pbinfo;
9231 tree base_list;
9232 unsigned int saved_maximum_field_alignment;
9233 tree fn_context;
9234
9235 if (type == error_mark_node)
9236 return error_mark_node;
9237
9238 if (COMPLETE_OR_OPEN_TYPE_P (type)
9239 || uses_template_parms (type))
9240 return type;
9241
9242 /* Figure out which template is being instantiated. */
9243 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
9244 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
9245
9246 /* Determine what specialization of the original template to
9247 instantiate. */
9248 t = most_specialized_partial_spec (type, tf_warning_or_error);
9249 if (t == error_mark_node)
9250 {
9251 TYPE_BEING_DEFINED (type) = 1;
9252 return error_mark_node;
9253 }
9254 else if (t)
9255 {
9256 /* This TYPE is actually an instantiation of a partial
9257 specialization. We replace the innermost set of ARGS with
9258 the arguments appropriate for substitution. For example,
9259 given:
9260
9261 template <class T> struct S {};
9262 template <class T> struct S<T*> {};
9263
9264 and supposing that we are instantiating S<int*>, ARGS will
9265 presently be {int*} -- but we need {int}. */
9266 pattern = TREE_TYPE (t);
9267 args = TREE_PURPOSE (t);
9268 }
9269 else
9270 {
9271 pattern = TREE_TYPE (templ);
9272 args = CLASSTYPE_TI_ARGS (type);
9273 }
9274
9275 /* If the template we're instantiating is incomplete, then clearly
9276 there's nothing we can do. */
9277 if (!COMPLETE_TYPE_P (pattern))
9278 return type;
9279
9280 /* If we've recursively instantiated too many templates, stop. */
9281 if (! push_tinst_level (type))
9282 return type;
9283
9284 /* Now we're really doing the instantiation. Mark the type as in
9285 the process of being defined. */
9286 TYPE_BEING_DEFINED (type) = 1;
9287
9288 /* We may be in the middle of deferred access check. Disable
9289 it now. */
9290 push_deferring_access_checks (dk_no_deferred);
9291
9292 int saved_unevaluated_operand = cp_unevaluated_operand;
9293 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9294
9295 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
9296 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
9297 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
9298 fn_context = error_mark_node;
9299 if (!fn_context)
9300 push_to_top_level ();
9301 else
9302 {
9303 cp_unevaluated_operand = 0;
9304 c_inhibit_evaluation_warnings = 0;
9305 }
9306 /* Use #pragma pack from the template context. */
9307 saved_maximum_field_alignment = maximum_field_alignment;
9308 maximum_field_alignment = TYPE_PRECISION (pattern);
9309
9310 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
9311
9312 /* Set the input location to the most specialized template definition.
9313 This is needed if tsubsting causes an error. */
9314 typedecl = TYPE_MAIN_DECL (pattern);
9315 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
9316 DECL_SOURCE_LOCATION (typedecl);
9317
9318 TYPE_PACKED (type) = TYPE_PACKED (pattern);
9319 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
9320 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
9321 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
9322 if (ANON_AGGR_TYPE_P (pattern))
9323 SET_ANON_AGGR_TYPE_P (type);
9324 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
9325 {
9326 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
9327 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
9328 /* Adjust visibility for template arguments. */
9329 determine_visibility (TYPE_MAIN_DECL (type));
9330 }
9331 if (CLASS_TYPE_P (type))
9332 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
9333
9334 pbinfo = TYPE_BINFO (pattern);
9335
9336 /* We should never instantiate a nested class before its enclosing
9337 class; we need to look up the nested class by name before we can
9338 instantiate it, and that lookup should instantiate the enclosing
9339 class. */
9340 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
9341 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
9342
9343 base_list = NULL_TREE;
9344 if (BINFO_N_BASE_BINFOS (pbinfo))
9345 {
9346 tree pbase_binfo;
9347 tree pushed_scope;
9348 int i;
9349
9350 /* We must enter the scope containing the type, as that is where
9351 the accessibility of types named in dependent bases are
9352 looked up from. */
9353 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
9354
9355 /* Substitute into each of the bases to determine the actual
9356 basetypes. */
9357 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
9358 {
9359 tree base;
9360 tree access = BINFO_BASE_ACCESS (pbinfo, i);
9361 tree expanded_bases = NULL_TREE;
9362 int idx, len = 1;
9363
9364 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
9365 {
9366 expanded_bases =
9367 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
9368 args, tf_error, NULL_TREE);
9369 if (expanded_bases == error_mark_node)
9370 continue;
9371
9372 len = TREE_VEC_LENGTH (expanded_bases);
9373 }
9374
9375 for (idx = 0; idx < len; idx++)
9376 {
9377 if (expanded_bases)
9378 /* Extract the already-expanded base class. */
9379 base = TREE_VEC_ELT (expanded_bases, idx);
9380 else
9381 /* Substitute to figure out the base class. */
9382 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
9383 NULL_TREE);
9384
9385 if (base == error_mark_node)
9386 continue;
9387
9388 base_list = tree_cons (access, base, base_list);
9389 if (BINFO_VIRTUAL_P (pbase_binfo))
9390 TREE_TYPE (base_list) = integer_type_node;
9391 }
9392 }
9393
9394 /* The list is now in reverse order; correct that. */
9395 base_list = nreverse (base_list);
9396
9397 if (pushed_scope)
9398 pop_scope (pushed_scope);
9399 }
9400 /* Now call xref_basetypes to set up all the base-class
9401 information. */
9402 xref_basetypes (type, base_list);
9403
9404 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
9405 (int) ATTR_FLAG_TYPE_IN_PLACE,
9406 args, tf_error, NULL_TREE);
9407 fixup_attribute_variants (type);
9408
9409 /* Now that our base classes are set up, enter the scope of the
9410 class, so that name lookups into base classes, etc. will work
9411 correctly. This is precisely analogous to what we do in
9412 begin_class_definition when defining an ordinary non-template
9413 class, except we also need to push the enclosing classes. */
9414 push_nested_class (type);
9415
9416 /* Now members are processed in the order of declaration. */
9417 for (member = CLASSTYPE_DECL_LIST (pattern);
9418 member; member = TREE_CHAIN (member))
9419 {
9420 tree t = TREE_VALUE (member);
9421
9422 if (TREE_PURPOSE (member))
9423 {
9424 if (TYPE_P (t))
9425 {
9426 /* Build new CLASSTYPE_NESTED_UTDS. */
9427
9428 tree newtag;
9429 bool class_template_p;
9430
9431 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
9432 && TYPE_LANG_SPECIFIC (t)
9433 && CLASSTYPE_IS_TEMPLATE (t));
9434 /* If the member is a class template, then -- even after
9435 substitution -- there may be dependent types in the
9436 template argument list for the class. We increment
9437 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
9438 that function will assume that no types are dependent
9439 when outside of a template. */
9440 if (class_template_p)
9441 ++processing_template_decl;
9442 newtag = tsubst (t, args, tf_error, NULL_TREE);
9443 if (class_template_p)
9444 --processing_template_decl;
9445 if (newtag == error_mark_node)
9446 continue;
9447
9448 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
9449 {
9450 tree name = TYPE_IDENTIFIER (t);
9451
9452 if (class_template_p)
9453 /* Unfortunately, lookup_template_class sets
9454 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
9455 instantiation (i.e., for the type of a member
9456 template class nested within a template class.)
9457 This behavior is required for
9458 maybe_process_partial_specialization to work
9459 correctly, but is not accurate in this case;
9460 the TAG is not an instantiation of anything.
9461 (The corresponding TEMPLATE_DECL is an
9462 instantiation, but the TYPE is not.) */
9463 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
9464
9465 /* Now, we call pushtag to put this NEWTAG into the scope of
9466 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
9467 pushtag calling push_template_decl. We don't have to do
9468 this for enums because it will already have been done in
9469 tsubst_enum. */
9470 if (name)
9471 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
9472 pushtag (name, newtag, /*tag_scope=*/ts_current);
9473 }
9474 }
9475 else if (DECL_DECLARES_FUNCTION_P (t))
9476 {
9477 /* Build new TYPE_METHODS. */
9478 tree r;
9479
9480 if (TREE_CODE (t) == TEMPLATE_DECL)
9481 ++processing_template_decl;
9482 r = tsubst (t, args, tf_error, NULL_TREE);
9483 if (TREE_CODE (t) == TEMPLATE_DECL)
9484 --processing_template_decl;
9485 set_current_access_from_decl (r);
9486 finish_member_declaration (r);
9487 /* Instantiate members marked with attribute used. */
9488 if (r != error_mark_node && DECL_PRESERVE_P (r))
9489 mark_used (r);
9490 if (TREE_CODE (r) == FUNCTION_DECL
9491 && DECL_OMP_DECLARE_REDUCTION_P (r))
9492 cp_check_omp_declare_reduction (r);
9493 }
9494 else if (DECL_CLASS_TEMPLATE_P (t)
9495 && LAMBDA_TYPE_P (TREE_TYPE (t)))
9496 /* A closure type for a lambda in a default argument for a
9497 member template. Ignore it; it will be instantiated with
9498 the default argument. */;
9499 else
9500 {
9501 /* Build new TYPE_FIELDS. */
9502 if (TREE_CODE (t) == STATIC_ASSERT)
9503 {
9504 tree condition;
9505
9506 ++c_inhibit_evaluation_warnings;
9507 condition =
9508 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
9509 tf_warning_or_error, NULL_TREE,
9510 /*integral_constant_expression_p=*/true);
9511 --c_inhibit_evaluation_warnings;
9512
9513 finish_static_assert (condition,
9514 STATIC_ASSERT_MESSAGE (t),
9515 STATIC_ASSERT_SOURCE_LOCATION (t),
9516 /*member_p=*/true);
9517 }
9518 else if (TREE_CODE (t) != CONST_DECL)
9519 {
9520 tree r;
9521 tree vec = NULL_TREE;
9522 int len = 1;
9523
9524 /* The file and line for this declaration, to
9525 assist in error message reporting. Since we
9526 called push_tinst_level above, we don't need to
9527 restore these. */
9528 input_location = DECL_SOURCE_LOCATION (t);
9529
9530 if (TREE_CODE (t) == TEMPLATE_DECL)
9531 ++processing_template_decl;
9532 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9533 if (TREE_CODE (t) == TEMPLATE_DECL)
9534 --processing_template_decl;
9535
9536 if (TREE_CODE (r) == TREE_VEC)
9537 {
9538 /* A capture pack became multiple fields. */
9539 vec = r;
9540 len = TREE_VEC_LENGTH (vec);
9541 }
9542
9543 for (int i = 0; i < len; ++i)
9544 {
9545 if (vec)
9546 r = TREE_VEC_ELT (vec, i);
9547 if (VAR_P (r))
9548 {
9549 /* In [temp.inst]:
9550
9551 [t]he initialization (and any associated
9552 side-effects) of a static data member does
9553 not occur unless the static data member is
9554 itself used in a way that requires the
9555 definition of the static data member to
9556 exist.
9557
9558 Therefore, we do not substitute into the
9559 initialized for the static data member here. */
9560 finish_static_data_member_decl
9561 (r,
9562 /*init=*/NULL_TREE,
9563 /*init_const_expr_p=*/false,
9564 /*asmspec_tree=*/NULL_TREE,
9565 /*flags=*/0);
9566 /* Instantiate members marked with attribute used. */
9567 if (r != error_mark_node && DECL_PRESERVE_P (r))
9568 mark_used (r);
9569 }
9570 else if (TREE_CODE (r) == FIELD_DECL)
9571 {
9572 /* Determine whether R has a valid type and can be
9573 completed later. If R is invalid, then its type
9574 is replaced by error_mark_node. */
9575 tree rtype = TREE_TYPE (r);
9576 if (can_complete_type_without_circularity (rtype))
9577 complete_type (rtype);
9578
9579 if (!COMPLETE_TYPE_P (rtype))
9580 {
9581 cxx_incomplete_type_error (r, rtype);
9582 TREE_TYPE (r) = error_mark_node;
9583 }
9584 }
9585
9586 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9587 such a thing will already have been added to the field
9588 list by tsubst_enum in finish_member_declaration in the
9589 CLASSTYPE_NESTED_UTDS case above. */
9590 if (!(TREE_CODE (r) == TYPE_DECL
9591 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9592 && DECL_ARTIFICIAL (r)))
9593 {
9594 set_current_access_from_decl (r);
9595 finish_member_declaration (r);
9596 }
9597 }
9598 }
9599 }
9600 }
9601 else
9602 {
9603 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9604 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9605 {
9606 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9607
9608 tree friend_type = t;
9609 bool adjust_processing_template_decl = false;
9610
9611 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9612 {
9613 /* template <class T> friend class C; */
9614 friend_type = tsubst_friend_class (friend_type, args);
9615 adjust_processing_template_decl = true;
9616 }
9617 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9618 {
9619 /* template <class T> friend class C::D; */
9620 friend_type = tsubst (friend_type, args,
9621 tf_warning_or_error, NULL_TREE);
9622 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9623 friend_type = TREE_TYPE (friend_type);
9624 adjust_processing_template_decl = true;
9625 }
9626 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9627 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9628 {
9629 /* This could be either
9630
9631 friend class T::C;
9632
9633 when dependent_type_p is false or
9634
9635 template <class U> friend class T::C;
9636
9637 otherwise. */
9638 friend_type = tsubst (friend_type, args,
9639 tf_warning_or_error, NULL_TREE);
9640 /* Bump processing_template_decl for correct
9641 dependent_type_p calculation. */
9642 ++processing_template_decl;
9643 if (dependent_type_p (friend_type))
9644 adjust_processing_template_decl = true;
9645 --processing_template_decl;
9646 }
9647 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9648 && hidden_name_p (TYPE_NAME (friend_type)))
9649 {
9650 /* friend class C;
9651
9652 where C hasn't been declared yet. Let's lookup name
9653 from namespace scope directly, bypassing any name that
9654 come from dependent base class. */
9655 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9656
9657 /* The call to xref_tag_from_type does injection for friend
9658 classes. */
9659 push_nested_namespace (ns);
9660 friend_type =
9661 xref_tag_from_type (friend_type, NULL_TREE,
9662 /*tag_scope=*/ts_current);
9663 pop_nested_namespace (ns);
9664 }
9665 else if (uses_template_parms (friend_type))
9666 /* friend class C<T>; */
9667 friend_type = tsubst (friend_type, args,
9668 tf_warning_or_error, NULL_TREE);
9669 /* Otherwise it's
9670
9671 friend class C;
9672
9673 where C is already declared or
9674
9675 friend class C<int>;
9676
9677 We don't have to do anything in these cases. */
9678
9679 if (adjust_processing_template_decl)
9680 /* Trick make_friend_class into realizing that the friend
9681 we're adding is a template, not an ordinary class. It's
9682 important that we use make_friend_class since it will
9683 perform some error-checking and output cross-reference
9684 information. */
9685 ++processing_template_decl;
9686
9687 if (friend_type != error_mark_node)
9688 make_friend_class (type, friend_type, /*complain=*/false);
9689
9690 if (adjust_processing_template_decl)
9691 --processing_template_decl;
9692 }
9693 else
9694 {
9695 /* Build new DECL_FRIENDLIST. */
9696 tree r;
9697
9698 /* The file and line for this declaration, to
9699 assist in error message reporting. Since we
9700 called push_tinst_level above, we don't need to
9701 restore these. */
9702 input_location = DECL_SOURCE_LOCATION (t);
9703
9704 if (TREE_CODE (t) == TEMPLATE_DECL)
9705 {
9706 ++processing_template_decl;
9707 push_deferring_access_checks (dk_no_check);
9708 }
9709
9710 r = tsubst_friend_function (t, args);
9711 add_friend (type, r, /*complain=*/false);
9712 if (TREE_CODE (t) == TEMPLATE_DECL)
9713 {
9714 pop_deferring_access_checks ();
9715 --processing_template_decl;
9716 }
9717 }
9718 }
9719 }
9720
9721 if (fn_context)
9722 {
9723 /* Restore these before substituting into the lambda capture
9724 initializers. */
9725 cp_unevaluated_operand = saved_unevaluated_operand;
9726 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9727 }
9728
9729 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9730 {
9731 tree decl = lambda_function (type);
9732 if (decl)
9733 {
9734 if (!DECL_TEMPLATE_INFO (decl)
9735 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9736 instantiate_decl (decl, false, false);
9737
9738 /* We need to instantiate the capture list from the template
9739 after we've instantiated the closure members, but before we
9740 consider adding the conversion op. Also keep any captures
9741 that may have been added during instantiation of the op(). */
9742 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9743 tree tmpl_cap
9744 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9745 args, tf_warning_or_error, NULL_TREE,
9746 false, false);
9747
9748 LAMBDA_EXPR_CAPTURE_LIST (expr)
9749 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9750
9751 maybe_add_lambda_conv_op (type);
9752 }
9753 else
9754 gcc_assert (errorcount);
9755 }
9756
9757 /* Set the file and line number information to whatever is given for
9758 the class itself. This puts error messages involving generated
9759 implicit functions at a predictable point, and the same point
9760 that would be used for non-template classes. */
9761 input_location = DECL_SOURCE_LOCATION (typedecl);
9762
9763 unreverse_member_declarations (type);
9764 finish_struct_1 (type);
9765 TYPE_BEING_DEFINED (type) = 0;
9766
9767 /* We don't instantiate default arguments for member functions. 14.7.1:
9768
9769 The implicit instantiation of a class template specialization causes
9770 the implicit instantiation of the declarations, but not of the
9771 definitions or default arguments, of the class member functions,
9772 member classes, static data members and member templates.... */
9773
9774 /* Some typedefs referenced from within the template code need to be access
9775 checked at template instantiation time, i.e now. These types were
9776 added to the template at parsing time. Let's get those and perform
9777 the access checks then. */
9778 perform_typedefs_access_check (pattern, args);
9779 perform_deferred_access_checks (tf_warning_or_error);
9780 pop_nested_class ();
9781 maximum_field_alignment = saved_maximum_field_alignment;
9782 if (!fn_context)
9783 pop_from_top_level ();
9784 pop_deferring_access_checks ();
9785 pop_tinst_level ();
9786
9787 /* The vtable for a template class can be emitted in any translation
9788 unit in which the class is instantiated. When there is no key
9789 method, however, finish_struct_1 will already have added TYPE to
9790 the keyed_classes list. */
9791 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9792 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9793
9794 return type;
9795 }
9796
9797 /* Wrapper for instantiate_class_template_1. */
9798
9799 tree
9800 instantiate_class_template (tree type)
9801 {
9802 tree ret;
9803 timevar_push (TV_TEMPLATE_INST);
9804 ret = instantiate_class_template_1 (type);
9805 timevar_pop (TV_TEMPLATE_INST);
9806 return ret;
9807 }
9808
9809 static tree
9810 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9811 {
9812 tree r;
9813
9814 if (!t)
9815 r = t;
9816 else if (TYPE_P (t))
9817 r = tsubst (t, args, complain, in_decl);
9818 else
9819 {
9820 if (!(complain & tf_warning))
9821 ++c_inhibit_evaluation_warnings;
9822 r = tsubst_expr (t, args, complain, in_decl,
9823 /*integral_constant_expression_p=*/true);
9824 if (!(complain & tf_warning))
9825 --c_inhibit_evaluation_warnings;
9826 }
9827 return r;
9828 }
9829
9830 /* Given a function parameter pack TMPL_PARM and some function parameters
9831 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9832 and set *SPEC_P to point at the next point in the list. */
9833
9834 static tree
9835 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9836 {
9837 /* Collect all of the extra "packed" parameters into an
9838 argument pack. */
9839 tree parmvec;
9840 tree parmtypevec;
9841 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9842 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9843 tree spec_parm = *spec_p;
9844 int i, len;
9845
9846 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9847 if (tmpl_parm
9848 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9849 break;
9850
9851 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9852 parmvec = make_tree_vec (len);
9853 parmtypevec = make_tree_vec (len);
9854 spec_parm = *spec_p;
9855 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9856 {
9857 TREE_VEC_ELT (parmvec, i) = spec_parm;
9858 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9859 }
9860
9861 /* Build the argument packs. */
9862 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9863 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9864 TREE_TYPE (argpack) = argtypepack;
9865 *spec_p = spec_parm;
9866
9867 return argpack;
9868 }
9869
9870 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9871 NONTYPE_ARGUMENT_PACK. */
9872
9873 static tree
9874 make_fnparm_pack (tree spec_parm)
9875 {
9876 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9877 }
9878
9879 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
9880 pack expansion with no extra args, 2 if it has extra args, or 0
9881 if it is not a pack expansion. */
9882
9883 static int
9884 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9885 {
9886 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9887 if (i >= TREE_VEC_LENGTH (vec))
9888 return 0;
9889 tree elt = TREE_VEC_ELT (vec, i);
9890 if (DECL_P (elt))
9891 /* A decl pack is itself an expansion. */
9892 elt = TREE_TYPE (elt);
9893 if (!PACK_EXPANSION_P (elt))
9894 return 0;
9895 if (PACK_EXPANSION_EXTRA_ARGS (elt))
9896 return 2;
9897 return 1;
9898 }
9899
9900
9901 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9902
9903 static tree
9904 make_argument_pack_select (tree arg_pack, unsigned index)
9905 {
9906 tree aps = make_node (ARGUMENT_PACK_SELECT);
9907
9908 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9909 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9910
9911 return aps;
9912 }
9913
9914 /* This is a subroutine of tsubst_pack_expansion.
9915
9916 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9917 mechanism to store the (non complete list of) arguments of the
9918 substitution and return a non substituted pack expansion, in order
9919 to wait for when we have enough arguments to really perform the
9920 substitution. */
9921
9922 static bool
9923 use_pack_expansion_extra_args_p (tree parm_packs,
9924 int arg_pack_len,
9925 bool has_empty_arg)
9926 {
9927 /* If one pack has an expansion and another pack has a normal
9928 argument or if one pack has an empty argument and an another
9929 one hasn't then tsubst_pack_expansion cannot perform the
9930 substitution and need to fall back on the
9931 PACK_EXPANSION_EXTRA mechanism. */
9932 if (parm_packs == NULL_TREE)
9933 return false;
9934 else if (has_empty_arg)
9935 return true;
9936
9937 bool has_expansion_arg = false;
9938 for (int i = 0 ; i < arg_pack_len; ++i)
9939 {
9940 bool has_non_expansion_arg = false;
9941 for (tree parm_pack = parm_packs;
9942 parm_pack;
9943 parm_pack = TREE_CHAIN (parm_pack))
9944 {
9945 tree arg = TREE_VALUE (parm_pack);
9946
9947 int exp = argument_pack_element_is_expansion_p (arg, i);
9948 if (exp == 2)
9949 /* We can't substitute a pack expansion with extra args into
9950 our pattern. */
9951 return true;
9952 else if (exp)
9953 has_expansion_arg = true;
9954 else
9955 has_non_expansion_arg = true;
9956 }
9957
9958 if (has_expansion_arg && has_non_expansion_arg)
9959 return true;
9960 }
9961 return false;
9962 }
9963
9964 /* [temp.variadic]/6 says that:
9965
9966 The instantiation of a pack expansion [...]
9967 produces a list E1,E2, ..., En, where N is the number of elements
9968 in the pack expansion parameters.
9969
9970 This subroutine of tsubst_pack_expansion produces one of these Ei.
9971
9972 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9973 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9974 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9975 INDEX is the index 'i' of the element Ei to produce. ARGS,
9976 COMPLAIN, and IN_DECL are the same parameters as for the
9977 tsubst_pack_expansion function.
9978
9979 The function returns the resulting Ei upon successful completion,
9980 or error_mark_node.
9981
9982 Note that this function possibly modifies the ARGS parameter, so
9983 it's the responsibility of the caller to restore it. */
9984
9985 static tree
9986 gen_elem_of_pack_expansion_instantiation (tree pattern,
9987 tree parm_packs,
9988 unsigned index,
9989 tree args /* This parm gets
9990 modified. */,
9991 tsubst_flags_t complain,
9992 tree in_decl)
9993 {
9994 tree t;
9995 bool ith_elem_is_expansion = false;
9996
9997 /* For each parameter pack, change the substitution of the parameter
9998 pack to the ith argument in its argument pack, then expand the
9999 pattern. */
10000 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10001 {
10002 tree parm = TREE_PURPOSE (pack);
10003 tree arg_pack = TREE_VALUE (pack);
10004 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10005
10006 ith_elem_is_expansion |=
10007 argument_pack_element_is_expansion_p (arg_pack, index);
10008
10009 /* Select the Ith argument from the pack. */
10010 if (TREE_CODE (parm) == PARM_DECL
10011 || TREE_CODE (parm) == FIELD_DECL)
10012 {
10013 if (index == 0)
10014 {
10015 aps = make_argument_pack_select (arg_pack, index);
10016 if (!mark_used (parm, complain) && !(complain & tf_error))
10017 return error_mark_node;
10018 register_local_specialization (aps, parm);
10019 }
10020 else
10021 aps = retrieve_local_specialization (parm);
10022 }
10023 else
10024 {
10025 int idx, level;
10026 template_parm_level_and_index (parm, &level, &idx);
10027
10028 if (index == 0)
10029 {
10030 aps = make_argument_pack_select (arg_pack, index);
10031 /* Update the corresponding argument. */
10032 TMPL_ARG (args, level, idx) = aps;
10033 }
10034 else
10035 /* Re-use the ARGUMENT_PACK_SELECT. */
10036 aps = TMPL_ARG (args, level, idx);
10037 }
10038 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10039 }
10040
10041 /* Substitute into the PATTERN with the (possibly altered)
10042 arguments. */
10043 if (pattern == in_decl)
10044 /* Expanding a fixed parameter pack from
10045 coerce_template_parameter_pack. */
10046 t = tsubst_decl (pattern, args, complain);
10047 else if (!TYPE_P (pattern))
10048 t = tsubst_expr (pattern, args, complain, in_decl,
10049 /*integral_constant_expression_p=*/false);
10050 else
10051 t = tsubst (pattern, args, complain, in_decl);
10052
10053 /* If the Ith argument pack element is a pack expansion, then
10054 the Ith element resulting from the substituting is going to
10055 be a pack expansion as well. */
10056 if (ith_elem_is_expansion)
10057 t = make_pack_expansion (t);
10058
10059 return t;
10060 }
10061
10062 /* Substitute ARGS into T, which is an pack expansion
10063 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
10064 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
10065 (if only a partial substitution could be performed) or
10066 ERROR_MARK_NODE if there was an error. */
10067 tree
10068 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
10069 tree in_decl)
10070 {
10071 tree pattern;
10072 tree pack, packs = NULL_TREE;
10073 bool unsubstituted_packs = false;
10074 int i, len = -1;
10075 tree result;
10076 hash_map<tree, tree> *saved_local_specializations = NULL;
10077 bool need_local_specializations = false;
10078 int levels;
10079
10080 gcc_assert (PACK_EXPANSION_P (t));
10081 pattern = PACK_EXPANSION_PATTERN (t);
10082
10083 /* Add in any args remembered from an earlier partial instantiation. */
10084 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
10085
10086 levels = TMPL_ARGS_DEPTH (args);
10087
10088 /* Determine the argument packs that will instantiate the parameter
10089 packs used in the expansion expression. While we're at it,
10090 compute the number of arguments to be expanded and make sure it
10091 is consistent. */
10092 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
10093 pack = TREE_CHAIN (pack))
10094 {
10095 tree parm_pack = TREE_VALUE (pack);
10096 tree arg_pack = NULL_TREE;
10097 tree orig_arg = NULL_TREE;
10098 int level = 0;
10099
10100 if (TREE_CODE (parm_pack) == BASES)
10101 {
10102 if (BASES_DIRECT (parm_pack))
10103 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
10104 args, complain, in_decl, false));
10105 else
10106 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
10107 args, complain, in_decl, false));
10108 }
10109 if (TREE_CODE (parm_pack) == PARM_DECL)
10110 {
10111 if (PACK_EXPANSION_LOCAL_P (t))
10112 arg_pack = retrieve_local_specialization (parm_pack);
10113 else
10114 {
10115 /* We can't rely on local_specializations for a parameter
10116 name used later in a function declaration (such as in a
10117 late-specified return type). Even if it exists, it might
10118 have the wrong value for a recursive call. Just make a
10119 dummy decl, since it's only used for its type. */
10120 arg_pack = tsubst_decl (parm_pack, args, complain);
10121 if (arg_pack && DECL_PACK_P (arg_pack))
10122 /* Partial instantiation of the parm_pack, we can't build
10123 up an argument pack yet. */
10124 arg_pack = NULL_TREE;
10125 else
10126 arg_pack = make_fnparm_pack (arg_pack);
10127 need_local_specializations = true;
10128 }
10129 }
10130 else if (TREE_CODE (parm_pack) == FIELD_DECL)
10131 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
10132 else
10133 {
10134 int idx;
10135 template_parm_level_and_index (parm_pack, &level, &idx);
10136
10137 if (level <= levels)
10138 arg_pack = TMPL_ARG (args, level, idx);
10139 }
10140
10141 orig_arg = arg_pack;
10142 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
10143 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
10144
10145 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
10146 /* This can only happen if we forget to expand an argument
10147 pack somewhere else. Just return an error, silently. */
10148 {
10149 result = make_tree_vec (1);
10150 TREE_VEC_ELT (result, 0) = error_mark_node;
10151 return result;
10152 }
10153
10154 if (arg_pack)
10155 {
10156 int my_len =
10157 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
10158
10159 /* Don't bother trying to do a partial substitution with
10160 incomplete packs; we'll try again after deduction. */
10161 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
10162 return t;
10163
10164 if (len < 0)
10165 len = my_len;
10166 else if (len != my_len)
10167 {
10168 if (!(complain & tf_error))
10169 /* Fail quietly. */;
10170 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
10171 error ("mismatched argument pack lengths while expanding "
10172 "%<%T%>",
10173 pattern);
10174 else
10175 error ("mismatched argument pack lengths while expanding "
10176 "%<%E%>",
10177 pattern);
10178 return error_mark_node;
10179 }
10180
10181 /* Keep track of the parameter packs and their corresponding
10182 argument packs. */
10183 packs = tree_cons (parm_pack, arg_pack, packs);
10184 TREE_TYPE (packs) = orig_arg;
10185 }
10186 else
10187 {
10188 /* We can't substitute for this parameter pack. We use a flag as
10189 well as the missing_level counter because function parameter
10190 packs don't have a level. */
10191 unsubstituted_packs = true;
10192 }
10193 }
10194
10195 /* If the expansion is just T..., return the matching argument pack. */
10196 if (!unsubstituted_packs
10197 && TREE_PURPOSE (packs) == pattern)
10198 {
10199 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
10200 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
10201 || pack_expansion_args_count (args))
10202 return args;
10203 /* Otherwise use the normal path so we get convert_from_reference. */
10204 }
10205
10206 /* We cannot expand this expansion expression, because we don't have
10207 all of the argument packs we need. */
10208 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
10209 {
10210 /* We got some full packs, but we can't substitute them in until we
10211 have values for all the packs. So remember these until then. */
10212
10213 t = make_pack_expansion (pattern);
10214 PACK_EXPANSION_EXTRA_ARGS (t) = args;
10215 return t;
10216 }
10217 else if (unsubstituted_packs)
10218 {
10219 /* There were no real arguments, we're just replacing a parameter
10220 pack with another version of itself. Substitute into the
10221 pattern and return a PACK_EXPANSION_*. The caller will need to
10222 deal with that. */
10223 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
10224 t = tsubst_expr (pattern, args, complain, in_decl,
10225 /*integral_constant_expression_p=*/false);
10226 else
10227 t = tsubst (pattern, args, complain, in_decl);
10228 t = make_pack_expansion (t);
10229 return t;
10230 }
10231
10232 gcc_assert (len >= 0);
10233
10234 if (need_local_specializations)
10235 {
10236 /* We're in a late-specified return type, so create our own local
10237 specializations map; the current map is either NULL or (in the
10238 case of recursive unification) might have bindings that we don't
10239 want to use or alter. */
10240 saved_local_specializations = local_specializations;
10241 local_specializations = new hash_map<tree, tree>;
10242 }
10243
10244 /* For each argument in each argument pack, substitute into the
10245 pattern. */
10246 result = make_tree_vec (len);
10247 for (i = 0; i < len; ++i)
10248 {
10249 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
10250 i,
10251 args, complain,
10252 in_decl);
10253 TREE_VEC_ELT (result, i) = t;
10254 if (t == error_mark_node)
10255 {
10256 result = error_mark_node;
10257 break;
10258 }
10259 }
10260
10261 /* Update ARGS to restore the substitution from parameter packs to
10262 their argument packs. */
10263 for (pack = packs; pack; pack = TREE_CHAIN (pack))
10264 {
10265 tree parm = TREE_PURPOSE (pack);
10266
10267 if (TREE_CODE (parm) == PARM_DECL
10268 || TREE_CODE (parm) == FIELD_DECL)
10269 register_local_specialization (TREE_TYPE (pack), parm);
10270 else
10271 {
10272 int idx, level;
10273
10274 if (TREE_VALUE (pack) == NULL_TREE)
10275 continue;
10276
10277 template_parm_level_and_index (parm, &level, &idx);
10278
10279 /* Update the corresponding argument. */
10280 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
10281 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
10282 TREE_TYPE (pack);
10283 else
10284 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
10285 }
10286 }
10287
10288 if (need_local_specializations)
10289 {
10290 delete local_specializations;
10291 local_specializations = saved_local_specializations;
10292 }
10293
10294 return result;
10295 }
10296
10297 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
10298 TMPL. We do this using DECL_PARM_INDEX, which should work even with
10299 parameter packs; all parms generated from a function parameter pack will
10300 have the same DECL_PARM_INDEX. */
10301
10302 tree
10303 get_pattern_parm (tree parm, tree tmpl)
10304 {
10305 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
10306 tree patparm;
10307
10308 if (DECL_ARTIFICIAL (parm))
10309 {
10310 for (patparm = DECL_ARGUMENTS (pattern);
10311 patparm; patparm = DECL_CHAIN (patparm))
10312 if (DECL_ARTIFICIAL (patparm)
10313 && DECL_NAME (parm) == DECL_NAME (patparm))
10314 break;
10315 }
10316 else
10317 {
10318 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
10319 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
10320 gcc_assert (DECL_PARM_INDEX (patparm)
10321 == DECL_PARM_INDEX (parm));
10322 }
10323
10324 return patparm;
10325 }
10326
10327 /* Substitute ARGS into the vector or list of template arguments T. */
10328
10329 static tree
10330 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10331 {
10332 tree orig_t = t;
10333 int len, need_new = 0, i, expanded_len_adjust = 0, out;
10334 tree *elts;
10335
10336 if (t == error_mark_node)
10337 return error_mark_node;
10338
10339 len = TREE_VEC_LENGTH (t);
10340 elts = XALLOCAVEC (tree, len);
10341
10342 for (i = 0; i < len; i++)
10343 {
10344 tree orig_arg = TREE_VEC_ELT (t, i);
10345 tree new_arg;
10346
10347 if (TREE_CODE (orig_arg) == TREE_VEC)
10348 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
10349 else if (PACK_EXPANSION_P (orig_arg))
10350 {
10351 /* Substitute into an expansion expression. */
10352 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
10353
10354 if (TREE_CODE (new_arg) == TREE_VEC)
10355 /* Add to the expanded length adjustment the number of
10356 expanded arguments. We subtract one from this
10357 measurement, because the argument pack expression
10358 itself is already counted as 1 in
10359 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
10360 the argument pack is empty. */
10361 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
10362 }
10363 else if (ARGUMENT_PACK_P (orig_arg))
10364 {
10365 /* Substitute into each of the arguments. */
10366 new_arg = TYPE_P (orig_arg)
10367 ? cxx_make_type (TREE_CODE (orig_arg))
10368 : make_node (TREE_CODE (orig_arg));
10369
10370 SET_ARGUMENT_PACK_ARGS (
10371 new_arg,
10372 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
10373 args, complain, in_decl));
10374
10375 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
10376 new_arg = error_mark_node;
10377
10378 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
10379 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
10380 complain, in_decl);
10381 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
10382
10383 if (TREE_TYPE (new_arg) == error_mark_node)
10384 new_arg = error_mark_node;
10385 }
10386 }
10387 else
10388 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
10389
10390 if (new_arg == error_mark_node)
10391 return error_mark_node;
10392
10393 elts[i] = new_arg;
10394 if (new_arg != orig_arg)
10395 need_new = 1;
10396 }
10397
10398 if (!need_new)
10399 return t;
10400
10401 /* Make space for the expanded arguments coming from template
10402 argument packs. */
10403 t = make_tree_vec (len + expanded_len_adjust);
10404 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
10405 arguments for a member template.
10406 In that case each TREE_VEC in ORIG_T represents a level of template
10407 arguments, and ORIG_T won't carry any non defaulted argument count.
10408 It will rather be the nested TREE_VECs that will carry one.
10409 In other words, ORIG_T carries a non defaulted argument count only
10410 if it doesn't contain any nested TREE_VEC. */
10411 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
10412 {
10413 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
10414 count += expanded_len_adjust;
10415 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
10416 }
10417 for (i = 0, out = 0; i < len; i++)
10418 {
10419 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
10420 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
10421 && TREE_CODE (elts[i]) == TREE_VEC)
10422 {
10423 int idx;
10424
10425 /* Now expand the template argument pack "in place". */
10426 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
10427 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
10428 }
10429 else
10430 {
10431 TREE_VEC_ELT (t, out) = elts[i];
10432 out++;
10433 }
10434 }
10435
10436 return t;
10437 }
10438
10439 /* Return the result of substituting ARGS into the template parameters
10440 given by PARMS. If there are m levels of ARGS and m + n levels of
10441 PARMS, then the result will contain n levels of PARMS. For
10442 example, if PARMS is `template <class T> template <class U>
10443 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
10444 result will be `template <int*, double, class V>'. */
10445
10446 static tree
10447 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
10448 {
10449 tree r = NULL_TREE;
10450 tree* new_parms;
10451
10452 /* When substituting into a template, we must set
10453 PROCESSING_TEMPLATE_DECL as the template parameters may be
10454 dependent if they are based on one-another, and the dependency
10455 predicates are short-circuit outside of templates. */
10456 ++processing_template_decl;
10457
10458 for (new_parms = &r;
10459 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
10460 new_parms = &(TREE_CHAIN (*new_parms)),
10461 parms = TREE_CHAIN (parms))
10462 {
10463 tree new_vec =
10464 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
10465 int i;
10466
10467 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
10468 {
10469 tree tuple;
10470
10471 if (parms == error_mark_node)
10472 continue;
10473
10474 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
10475
10476 if (tuple == error_mark_node)
10477 continue;
10478
10479 TREE_VEC_ELT (new_vec, i) =
10480 tsubst_template_parm (tuple, args, complain);
10481 }
10482
10483 *new_parms =
10484 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
10485 - TMPL_ARGS_DEPTH (args)),
10486 new_vec, NULL_TREE);
10487 }
10488
10489 --processing_template_decl;
10490
10491 return r;
10492 }
10493
10494 /* Return the result of substituting ARGS into one template parameter
10495 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
10496 parameter and which TREE_PURPOSE is the default argument of the
10497 template parameter. */
10498
10499 static tree
10500 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
10501 {
10502 tree default_value, parm_decl;
10503
10504 if (args == NULL_TREE
10505 || t == NULL_TREE
10506 || t == error_mark_node)
10507 return t;
10508
10509 gcc_assert (TREE_CODE (t) == TREE_LIST);
10510
10511 default_value = TREE_PURPOSE (t);
10512 parm_decl = TREE_VALUE (t);
10513
10514 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
10515 if (TREE_CODE (parm_decl) == PARM_DECL
10516 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
10517 parm_decl = error_mark_node;
10518 default_value = tsubst_template_arg (default_value, args,
10519 complain, NULL_TREE);
10520
10521 return build_tree_list (default_value, parm_decl);
10522 }
10523
10524 /* Substitute the ARGS into the indicated aggregate (or enumeration)
10525 type T. If T is not an aggregate or enumeration type, it is
10526 handled as if by tsubst. IN_DECL is as for tsubst. If
10527 ENTERING_SCOPE is nonzero, T is the context for a template which
10528 we are presently tsubst'ing. Return the substituted value. */
10529
10530 static tree
10531 tsubst_aggr_type (tree t,
10532 tree args,
10533 tsubst_flags_t complain,
10534 tree in_decl,
10535 int entering_scope)
10536 {
10537 if (t == NULL_TREE)
10538 return NULL_TREE;
10539
10540 switch (TREE_CODE (t))
10541 {
10542 case RECORD_TYPE:
10543 if (TYPE_PTRMEMFUNC_P (t))
10544 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
10545
10546 /* Else fall through. */
10547 case ENUMERAL_TYPE:
10548 case UNION_TYPE:
10549 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
10550 {
10551 tree argvec;
10552 tree context;
10553 tree r;
10554 int saved_unevaluated_operand;
10555 int saved_inhibit_evaluation_warnings;
10556
10557 /* In "sizeof(X<I>)" we need to evaluate "I". */
10558 saved_unevaluated_operand = cp_unevaluated_operand;
10559 cp_unevaluated_operand = 0;
10560 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10561 c_inhibit_evaluation_warnings = 0;
10562
10563 /* First, determine the context for the type we are looking
10564 up. */
10565 context = TYPE_CONTEXT (t);
10566 if (context && TYPE_P (context))
10567 {
10568 context = tsubst_aggr_type (context, args, complain,
10569 in_decl, /*entering_scope=*/1);
10570 /* If context is a nested class inside a class template,
10571 it may still need to be instantiated (c++/33959). */
10572 context = complete_type (context);
10573 }
10574
10575 /* Then, figure out what arguments are appropriate for the
10576 type we are trying to find. For example, given:
10577
10578 template <class T> struct S;
10579 template <class T, class U> void f(T, U) { S<U> su; }
10580
10581 and supposing that we are instantiating f<int, double>,
10582 then our ARGS will be {int, double}, but, when looking up
10583 S we only want {double}. */
10584 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10585 complain, in_decl);
10586 if (argvec == error_mark_node)
10587 r = error_mark_node;
10588 else
10589 {
10590 r = lookup_template_class (t, argvec, in_decl, context,
10591 entering_scope, complain);
10592 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10593 }
10594
10595 cp_unevaluated_operand = saved_unevaluated_operand;
10596 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10597
10598 return r;
10599 }
10600 else
10601 /* This is not a template type, so there's nothing to do. */
10602 return t;
10603
10604 default:
10605 return tsubst (t, args, complain, in_decl);
10606 }
10607 }
10608
10609 /* Substitute into the default argument ARG (a default argument for
10610 FN), which has the indicated TYPE. */
10611
10612 tree
10613 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10614 {
10615 tree saved_class_ptr = NULL_TREE;
10616 tree saved_class_ref = NULL_TREE;
10617 int errs = errorcount + sorrycount;
10618
10619 /* This can happen in invalid code. */
10620 if (TREE_CODE (arg) == DEFAULT_ARG)
10621 return arg;
10622
10623 /* This default argument came from a template. Instantiate the
10624 default argument here, not in tsubst. In the case of
10625 something like:
10626
10627 template <class T>
10628 struct S {
10629 static T t();
10630 void f(T = t());
10631 };
10632
10633 we must be careful to do name lookup in the scope of S<T>,
10634 rather than in the current class. */
10635 push_access_scope (fn);
10636 /* The "this" pointer is not valid in a default argument. */
10637 if (cfun)
10638 {
10639 saved_class_ptr = current_class_ptr;
10640 cp_function_chain->x_current_class_ptr = NULL_TREE;
10641 saved_class_ref = current_class_ref;
10642 cp_function_chain->x_current_class_ref = NULL_TREE;
10643 }
10644
10645 push_deferring_access_checks(dk_no_deferred);
10646 /* The default argument expression may cause implicitly defined
10647 member functions to be synthesized, which will result in garbage
10648 collection. We must treat this situation as if we were within
10649 the body of function so as to avoid collecting live data on the
10650 stack. */
10651 ++function_depth;
10652 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10653 complain, NULL_TREE,
10654 /*integral_constant_expression_p=*/false);
10655 --function_depth;
10656 pop_deferring_access_checks();
10657
10658 /* Restore the "this" pointer. */
10659 if (cfun)
10660 {
10661 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10662 cp_function_chain->x_current_class_ref = saved_class_ref;
10663 }
10664
10665 if (errorcount+sorrycount > errs
10666 && (complain & tf_warning_or_error))
10667 inform (input_location,
10668 " when instantiating default argument for call to %D", fn);
10669
10670 /* Make sure the default argument is reasonable. */
10671 arg = check_default_argument (type, arg, complain);
10672
10673 pop_access_scope (fn);
10674
10675 return arg;
10676 }
10677
10678 /* Substitute into all the default arguments for FN. */
10679
10680 static void
10681 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10682 {
10683 tree arg;
10684 tree tmpl_args;
10685
10686 tmpl_args = DECL_TI_ARGS (fn);
10687
10688 /* If this function is not yet instantiated, we certainly don't need
10689 its default arguments. */
10690 if (uses_template_parms (tmpl_args))
10691 return;
10692 /* Don't do this again for clones. */
10693 if (DECL_CLONED_FUNCTION_P (fn))
10694 return;
10695
10696 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10697 arg;
10698 arg = TREE_CHAIN (arg))
10699 if (TREE_PURPOSE (arg))
10700 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10701 TREE_VALUE (arg),
10702 TREE_PURPOSE (arg),
10703 complain);
10704 }
10705
10706 /* Substitute the ARGS into the T, which is a _DECL. Return the
10707 result of the substitution. Issue error and warning messages under
10708 control of COMPLAIN. */
10709
10710 static tree
10711 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10712 {
10713 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10714 location_t saved_loc;
10715 tree r = NULL_TREE;
10716 tree in_decl = t;
10717 hashval_t hash = 0;
10718
10719 /* Set the filename and linenumber to improve error-reporting. */
10720 saved_loc = input_location;
10721 input_location = DECL_SOURCE_LOCATION (t);
10722
10723 switch (TREE_CODE (t))
10724 {
10725 case TEMPLATE_DECL:
10726 {
10727 /* We can get here when processing a member function template,
10728 member class template, or template template parameter. */
10729 tree decl = DECL_TEMPLATE_RESULT (t);
10730 tree spec;
10731 tree tmpl_args;
10732 tree full_args;
10733
10734 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10735 {
10736 /* Template template parameter is treated here. */
10737 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10738 if (new_type == error_mark_node)
10739 r = error_mark_node;
10740 /* If we get a real template back, return it. This can happen in
10741 the context of most_specialized_partial_spec. */
10742 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
10743 r = new_type;
10744 else
10745 /* The new TEMPLATE_DECL was built in
10746 reduce_template_parm_level. */
10747 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
10748 break;
10749 }
10750
10751 /* We might already have an instance of this template.
10752 The ARGS are for the surrounding class type, so the
10753 full args contain the tsubst'd args for the context,
10754 plus the innermost args from the template decl. */
10755 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10756 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10757 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10758 /* Because this is a template, the arguments will still be
10759 dependent, even after substitution. If
10760 PROCESSING_TEMPLATE_DECL is not set, the dependency
10761 predicates will short-circuit. */
10762 ++processing_template_decl;
10763 full_args = tsubst_template_args (tmpl_args, args,
10764 complain, in_decl);
10765 --processing_template_decl;
10766 if (full_args == error_mark_node)
10767 RETURN (error_mark_node);
10768
10769 /* If this is a default template template argument,
10770 tsubst might not have changed anything. */
10771 if (full_args == tmpl_args)
10772 RETURN (t);
10773
10774 hash = hash_tmpl_and_args (t, full_args);
10775 spec = retrieve_specialization (t, full_args, hash);
10776 if (spec != NULL_TREE)
10777 {
10778 r = spec;
10779 break;
10780 }
10781
10782 /* Make a new template decl. It will be similar to the
10783 original, but will record the current template arguments.
10784 We also create a new function declaration, which is just
10785 like the old one, but points to this new template, rather
10786 than the old one. */
10787 r = copy_decl (t);
10788 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10789 DECL_CHAIN (r) = NULL_TREE;
10790
10791 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10792
10793 if (TREE_CODE (decl) == TYPE_DECL
10794 && !TYPE_DECL_ALIAS_P (decl))
10795 {
10796 tree new_type;
10797 ++processing_template_decl;
10798 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10799 --processing_template_decl;
10800 if (new_type == error_mark_node)
10801 RETURN (error_mark_node);
10802
10803 TREE_TYPE (r) = new_type;
10804 /* For a partial specialization, we need to keep pointing to
10805 the primary template. */
10806 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10807 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10808 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10809 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10810 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10811 }
10812 else
10813 {
10814 tree new_decl;
10815 ++processing_template_decl;
10816 new_decl = tsubst (decl, args, complain, in_decl);
10817 --processing_template_decl;
10818 if (new_decl == error_mark_node)
10819 RETURN (error_mark_node);
10820
10821 DECL_TEMPLATE_RESULT (r) = new_decl;
10822 DECL_TI_TEMPLATE (new_decl) = r;
10823 TREE_TYPE (r) = TREE_TYPE (new_decl);
10824 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10825 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10826 }
10827
10828 SET_DECL_IMPLICIT_INSTANTIATION (r);
10829 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10830 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10831
10832 /* The template parameters for this new template are all the
10833 template parameters for the old template, except the
10834 outermost level of parameters. */
10835 DECL_TEMPLATE_PARMS (r)
10836 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10837 complain);
10838
10839 if (PRIMARY_TEMPLATE_P (t))
10840 DECL_PRIMARY_TEMPLATE (r) = r;
10841
10842 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
10843 /* Record this non-type partial instantiation. */
10844 register_specialization (r, t,
10845 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10846 false, hash);
10847 }
10848 break;
10849
10850 case FUNCTION_DECL:
10851 {
10852 tree ctx;
10853 tree argvec = NULL_TREE;
10854 tree *friends;
10855 tree gen_tmpl;
10856 tree type;
10857 int member;
10858 int args_depth;
10859 int parms_depth;
10860
10861 /* Nobody should be tsubst'ing into non-template functions. */
10862 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10863
10864 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10865 {
10866 tree spec;
10867 bool dependent_p;
10868
10869 /* If T is not dependent, just return it. We have to
10870 increment PROCESSING_TEMPLATE_DECL because
10871 value_dependent_expression_p assumes that nothing is
10872 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10873 ++processing_template_decl;
10874 dependent_p = value_dependent_expression_p (t);
10875 --processing_template_decl;
10876 if (!dependent_p)
10877 RETURN (t);
10878
10879 /* Calculate the most general template of which R is a
10880 specialization, and the complete set of arguments used to
10881 specialize R. */
10882 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10883 argvec = tsubst_template_args (DECL_TI_ARGS
10884 (DECL_TEMPLATE_RESULT
10885 (DECL_TI_TEMPLATE (t))),
10886 args, complain, in_decl);
10887 if (argvec == error_mark_node)
10888 RETURN (error_mark_node);
10889
10890 /* Check to see if we already have this specialization. */
10891 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10892 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10893
10894 if (spec)
10895 {
10896 r = spec;
10897 break;
10898 }
10899
10900 /* We can see more levels of arguments than parameters if
10901 there was a specialization of a member template, like
10902 this:
10903
10904 template <class T> struct S { template <class U> void f(); }
10905 template <> template <class U> void S<int>::f(U);
10906
10907 Here, we'll be substituting into the specialization,
10908 because that's where we can find the code we actually
10909 want to generate, but we'll have enough arguments for
10910 the most general template.
10911
10912 We also deal with the peculiar case:
10913
10914 template <class T> struct S {
10915 template <class U> friend void f();
10916 };
10917 template <class U> void f() {}
10918 template S<int>;
10919 template void f<double>();
10920
10921 Here, the ARGS for the instantiation of will be {int,
10922 double}. But, we only need as many ARGS as there are
10923 levels of template parameters in CODE_PATTERN. We are
10924 careful not to get fooled into reducing the ARGS in
10925 situations like:
10926
10927 template <class T> struct S { template <class U> void f(U); }
10928 template <class T> template <> void S<T>::f(int) {}
10929
10930 which we can spot because the pattern will be a
10931 specialization in this case. */
10932 args_depth = TMPL_ARGS_DEPTH (args);
10933 parms_depth =
10934 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10935 if (args_depth > parms_depth
10936 && !DECL_TEMPLATE_SPECIALIZATION (t))
10937 args = get_innermost_template_args (args, parms_depth);
10938 }
10939 else
10940 {
10941 /* This special case arises when we have something like this:
10942
10943 template <class T> struct S {
10944 friend void f<int>(int, double);
10945 };
10946
10947 Here, the DECL_TI_TEMPLATE for the friend declaration
10948 will be an IDENTIFIER_NODE. We are being called from
10949 tsubst_friend_function, and we want only to create a
10950 new decl (R) with appropriate types so that we can call
10951 determine_specialization. */
10952 gen_tmpl = NULL_TREE;
10953 }
10954
10955 if (DECL_CLASS_SCOPE_P (t))
10956 {
10957 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10958 member = 2;
10959 else
10960 member = 1;
10961 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10962 complain, t, /*entering_scope=*/1);
10963 }
10964 else
10965 {
10966 member = 0;
10967 ctx = DECL_CONTEXT (t);
10968 }
10969 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10970 if (type == error_mark_node)
10971 RETURN (error_mark_node);
10972
10973 /* If we hit excessive deduction depth, the type is bogus even if
10974 it isn't error_mark_node, so don't build a decl. */
10975 if (excessive_deduction_depth)
10976 RETURN (error_mark_node);
10977
10978 /* We do NOT check for matching decls pushed separately at this
10979 point, as they may not represent instantiations of this
10980 template, and in any case are considered separate under the
10981 discrete model. */
10982 r = copy_decl (t);
10983 DECL_USE_TEMPLATE (r) = 0;
10984 TREE_TYPE (r) = type;
10985 /* Clear out the mangled name and RTL for the instantiation. */
10986 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10987 SET_DECL_RTL (r, NULL);
10988 /* Leave DECL_INITIAL set on deleted instantiations. */
10989 if (!DECL_DELETED_FN (r))
10990 DECL_INITIAL (r) = NULL_TREE;
10991 DECL_CONTEXT (r) = ctx;
10992
10993 /* OpenMP UDRs have the only argument a reference to the declared
10994 type. We want to diagnose if the declared type is a reference,
10995 which is invalid, but as references to references are usually
10996 quietly merged, diagnose it here. */
10997 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10998 {
10999 tree argtype
11000 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
11001 argtype = tsubst (argtype, args, complain, in_decl);
11002 if (TREE_CODE (argtype) == REFERENCE_TYPE)
11003 error_at (DECL_SOURCE_LOCATION (t),
11004 "reference type %qT in "
11005 "%<#pragma omp declare reduction%>", argtype);
11006 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
11007 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
11008 argtype);
11009 }
11010
11011 if (member && DECL_CONV_FN_P (r))
11012 /* Type-conversion operator. Reconstruct the name, in
11013 case it's the name of one of the template's parameters. */
11014 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
11015
11016 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
11017 complain, t);
11018 DECL_RESULT (r) = NULL_TREE;
11019
11020 TREE_STATIC (r) = 0;
11021 TREE_PUBLIC (r) = TREE_PUBLIC (t);
11022 DECL_EXTERNAL (r) = 1;
11023 /* If this is an instantiation of a function with internal
11024 linkage, we already know what object file linkage will be
11025 assigned to the instantiation. */
11026 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
11027 DECL_DEFER_OUTPUT (r) = 0;
11028 DECL_CHAIN (r) = NULL_TREE;
11029 DECL_PENDING_INLINE_INFO (r) = 0;
11030 DECL_PENDING_INLINE_P (r) = 0;
11031 DECL_SAVED_TREE (r) = NULL_TREE;
11032 DECL_STRUCT_FUNCTION (r) = NULL;
11033 TREE_USED (r) = 0;
11034 /* We'll re-clone as appropriate in instantiate_template. */
11035 DECL_CLONED_FUNCTION (r) = NULL_TREE;
11036
11037 /* If we aren't complaining now, return on error before we register
11038 the specialization so that we'll complain eventually. */
11039 if ((complain & tf_error) == 0
11040 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11041 && !grok_op_properties (r, /*complain=*/false))
11042 RETURN (error_mark_node);
11043
11044 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
11045 this in the special friend case mentioned above where
11046 GEN_TMPL is NULL. */
11047 if (gen_tmpl)
11048 {
11049 DECL_TEMPLATE_INFO (r)
11050 = build_template_info (gen_tmpl, argvec);
11051 SET_DECL_IMPLICIT_INSTANTIATION (r);
11052
11053 tree new_r
11054 = register_specialization (r, gen_tmpl, argvec, false, hash);
11055 if (new_r != r)
11056 /* We instantiated this while substituting into
11057 the type earlier (template/friend54.C). */
11058 RETURN (new_r);
11059
11060 /* We're not supposed to instantiate default arguments
11061 until they are called, for a template. But, for a
11062 declaration like:
11063
11064 template <class T> void f ()
11065 { extern void g(int i = T()); }
11066
11067 we should do the substitution when the template is
11068 instantiated. We handle the member function case in
11069 instantiate_class_template since the default arguments
11070 might refer to other members of the class. */
11071 if (!member
11072 && !PRIMARY_TEMPLATE_P (gen_tmpl)
11073 && !uses_template_parms (argvec))
11074 tsubst_default_arguments (r, complain);
11075 }
11076 else
11077 DECL_TEMPLATE_INFO (r) = NULL_TREE;
11078
11079 /* Copy the list of befriending classes. */
11080 for (friends = &DECL_BEFRIENDING_CLASSES (r);
11081 *friends;
11082 friends = &TREE_CHAIN (*friends))
11083 {
11084 *friends = copy_node (*friends);
11085 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
11086 args, complain,
11087 in_decl);
11088 }
11089
11090 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
11091 {
11092 maybe_retrofit_in_chrg (r);
11093 if (DECL_CONSTRUCTOR_P (r))
11094 grok_ctor_properties (ctx, r);
11095 if (DECL_INHERITED_CTOR_BASE (r))
11096 deduce_inheriting_ctor (r);
11097 /* If this is an instantiation of a member template, clone it.
11098 If it isn't, that'll be handled by
11099 clone_constructors_and_destructors. */
11100 if (PRIMARY_TEMPLATE_P (gen_tmpl))
11101 clone_function_decl (r, /*update_method_vec_p=*/0);
11102 }
11103 else if ((complain & tf_error) != 0
11104 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
11105 && !grok_op_properties (r, /*complain=*/true))
11106 RETURN (error_mark_node);
11107
11108 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
11109 SET_DECL_FRIEND_CONTEXT (r,
11110 tsubst (DECL_FRIEND_CONTEXT (t),
11111 args, complain, in_decl));
11112
11113 /* Possibly limit visibility based on template args. */
11114 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11115 if (DECL_VISIBILITY_SPECIFIED (t))
11116 {
11117 DECL_VISIBILITY_SPECIFIED (r) = 0;
11118 DECL_ATTRIBUTES (r)
11119 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11120 }
11121 determine_visibility (r);
11122 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
11123 && !processing_template_decl)
11124 defaulted_late_check (r);
11125
11126 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11127 args, complain, in_decl);
11128 }
11129 break;
11130
11131 case PARM_DECL:
11132 {
11133 tree type = NULL_TREE;
11134 int i, len = 1;
11135 tree expanded_types = NULL_TREE;
11136 tree prev_r = NULL_TREE;
11137 tree first_r = NULL_TREE;
11138
11139 if (DECL_PACK_P (t))
11140 {
11141 /* If there is a local specialization that isn't a
11142 parameter pack, it means that we're doing a "simple"
11143 substitution from inside tsubst_pack_expansion. Just
11144 return the local specialization (which will be a single
11145 parm). */
11146 tree spec = retrieve_local_specialization (t);
11147 if (spec
11148 && TREE_CODE (spec) == PARM_DECL
11149 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
11150 RETURN (spec);
11151
11152 /* Expand the TYPE_PACK_EXPANSION that provides the types for
11153 the parameters in this function parameter pack. */
11154 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11155 complain, in_decl);
11156 if (TREE_CODE (expanded_types) == TREE_VEC)
11157 {
11158 len = TREE_VEC_LENGTH (expanded_types);
11159
11160 /* Zero-length parameter packs are boring. Just substitute
11161 into the chain. */
11162 if (len == 0)
11163 RETURN (tsubst (TREE_CHAIN (t), args, complain,
11164 TREE_CHAIN (t)));
11165 }
11166 else
11167 {
11168 /* All we did was update the type. Make a note of that. */
11169 type = expanded_types;
11170 expanded_types = NULL_TREE;
11171 }
11172 }
11173
11174 /* Loop through all of the parameters we'll build. When T is
11175 a function parameter pack, LEN is the number of expanded
11176 types in EXPANDED_TYPES; otherwise, LEN is 1. */
11177 r = NULL_TREE;
11178 for (i = 0; i < len; ++i)
11179 {
11180 prev_r = r;
11181 r = copy_node (t);
11182 if (DECL_TEMPLATE_PARM_P (t))
11183 SET_DECL_TEMPLATE_PARM_P (r);
11184
11185 if (expanded_types)
11186 /* We're on the Ith parameter of the function parameter
11187 pack. */
11188 {
11189 /* Get the Ith type. */
11190 type = TREE_VEC_ELT (expanded_types, i);
11191
11192 /* Rename the parameter to include the index. */
11193 DECL_NAME (r)
11194 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11195 }
11196 else if (!type)
11197 /* We're dealing with a normal parameter. */
11198 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11199
11200 type = type_decays_to (type);
11201 TREE_TYPE (r) = type;
11202 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11203
11204 if (DECL_INITIAL (r))
11205 {
11206 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
11207 DECL_INITIAL (r) = TREE_TYPE (r);
11208 else
11209 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
11210 complain, in_decl);
11211 }
11212
11213 DECL_CONTEXT (r) = NULL_TREE;
11214
11215 if (!DECL_TEMPLATE_PARM_P (r))
11216 DECL_ARG_TYPE (r) = type_passed_as (type);
11217
11218 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11219 args, complain, in_decl);
11220
11221 /* Keep track of the first new parameter we
11222 generate. That's what will be returned to the
11223 caller. */
11224 if (!first_r)
11225 first_r = r;
11226
11227 /* Build a proper chain of parameters when substituting
11228 into a function parameter pack. */
11229 if (prev_r)
11230 DECL_CHAIN (prev_r) = r;
11231 }
11232
11233 /* If cp_unevaluated_operand is set, we're just looking for a
11234 single dummy parameter, so don't keep going. */
11235 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
11236 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
11237 complain, DECL_CHAIN (t));
11238
11239 /* FIRST_R contains the start of the chain we've built. */
11240 r = first_r;
11241 }
11242 break;
11243
11244 case FIELD_DECL:
11245 {
11246 tree type = NULL_TREE;
11247 tree vec = NULL_TREE;
11248 tree expanded_types = NULL_TREE;
11249 int len = 1;
11250
11251 if (PACK_EXPANSION_P (TREE_TYPE (t)))
11252 {
11253 /* This field is a lambda capture pack. Return a TREE_VEC of
11254 the expanded fields to instantiate_class_template_1 and
11255 store them in the specializations hash table as a
11256 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
11257 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
11258 complain, in_decl);
11259 if (TREE_CODE (expanded_types) == TREE_VEC)
11260 {
11261 len = TREE_VEC_LENGTH (expanded_types);
11262 vec = make_tree_vec (len);
11263 }
11264 else
11265 {
11266 /* All we did was update the type. Make a note of that. */
11267 type = expanded_types;
11268 expanded_types = NULL_TREE;
11269 }
11270 }
11271
11272 for (int i = 0; i < len; ++i)
11273 {
11274 r = copy_decl (t);
11275 if (expanded_types)
11276 {
11277 type = TREE_VEC_ELT (expanded_types, i);
11278 DECL_NAME (r)
11279 = make_ith_pack_parameter_name (DECL_NAME (r), i);
11280 }
11281 else if (!type)
11282 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11283
11284 if (type == error_mark_node)
11285 RETURN (error_mark_node);
11286 TREE_TYPE (r) = type;
11287 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11288
11289 if (DECL_C_BIT_FIELD (r))
11290 /* For bit-fields, DECL_INITIAL gives the number of bits. For
11291 non-bit-fields DECL_INITIAL is a non-static data member
11292 initializer, which gets deferred instantiation. */
11293 DECL_INITIAL (r)
11294 = tsubst_expr (DECL_INITIAL (t), args,
11295 complain, in_decl,
11296 /*integral_constant_expression_p=*/true);
11297 else if (DECL_INITIAL (t))
11298 {
11299 /* Set up DECL_TEMPLATE_INFO so that we can get at the
11300 NSDMI in perform_member_init. Still set DECL_INITIAL
11301 so that we know there is one. */
11302 DECL_INITIAL (r) = void_node;
11303 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
11304 retrofit_lang_decl (r);
11305 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
11306 }
11307 /* We don't have to set DECL_CONTEXT here; it is set by
11308 finish_member_declaration. */
11309 DECL_CHAIN (r) = NULL_TREE;
11310
11311 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
11312 args, complain, in_decl);
11313
11314 if (vec)
11315 TREE_VEC_ELT (vec, i) = r;
11316 }
11317
11318 if (vec)
11319 {
11320 r = vec;
11321 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
11322 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
11323 SET_ARGUMENT_PACK_ARGS (pack, vec);
11324 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
11325 TREE_TYPE (pack) = tpack;
11326 register_specialization (pack, t, args, false, 0);
11327 }
11328 }
11329 break;
11330
11331 case USING_DECL:
11332 /* We reach here only for member using decls. We also need to check
11333 uses_template_parms because DECL_DEPENDENT_P is not set for a
11334 using-declaration that designates a member of the current
11335 instantiation (c++/53549). */
11336 if (DECL_DEPENDENT_P (t)
11337 || uses_template_parms (USING_DECL_SCOPE (t)))
11338 {
11339 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
11340 complain, in_decl);
11341 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
11342 r = do_class_using_decl (inst_scope, name);
11343 if (!r)
11344 r = error_mark_node;
11345 else
11346 {
11347 TREE_PROTECTED (r) = TREE_PROTECTED (t);
11348 TREE_PRIVATE (r) = TREE_PRIVATE (t);
11349 }
11350 }
11351 else
11352 {
11353 r = copy_node (t);
11354 DECL_CHAIN (r) = NULL_TREE;
11355 }
11356 break;
11357
11358 case TYPE_DECL:
11359 case VAR_DECL:
11360 {
11361 tree argvec = NULL_TREE;
11362 tree gen_tmpl = NULL_TREE;
11363 tree spec;
11364 tree tmpl = NULL_TREE;
11365 tree ctx;
11366 tree type = NULL_TREE;
11367 bool local_p;
11368
11369 if (TREE_TYPE (t) == error_mark_node)
11370 RETURN (error_mark_node);
11371
11372 if (TREE_CODE (t) == TYPE_DECL
11373 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
11374 {
11375 /* If this is the canonical decl, we don't have to
11376 mess with instantiations, and often we can't (for
11377 typename, template type parms and such). Note that
11378 TYPE_NAME is not correct for the above test if
11379 we've copied the type for a typedef. */
11380 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11381 if (type == error_mark_node)
11382 RETURN (error_mark_node);
11383 r = TYPE_NAME (type);
11384 break;
11385 }
11386
11387 /* Check to see if we already have the specialization we
11388 need. */
11389 spec = NULL_TREE;
11390 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
11391 {
11392 /* T is a static data member or namespace-scope entity.
11393 We have to substitute into namespace-scope variables
11394 (not just variable templates) because of cases like:
11395
11396 template <class T> void f() { extern T t; }
11397
11398 where the entity referenced is not known until
11399 instantiation time. */
11400 local_p = false;
11401 ctx = DECL_CONTEXT (t);
11402 if (DECL_CLASS_SCOPE_P (t))
11403 {
11404 ctx = tsubst_aggr_type (ctx, args,
11405 complain,
11406 in_decl, /*entering_scope=*/1);
11407 /* If CTX is unchanged, then T is in fact the
11408 specialization we want. That situation occurs when
11409 referencing a static data member within in its own
11410 class. We can use pointer equality, rather than
11411 same_type_p, because DECL_CONTEXT is always
11412 canonical... */
11413 if (ctx == DECL_CONTEXT (t)
11414 /* ... unless T is a member template; in which
11415 case our caller can be willing to create a
11416 specialization of that template represented
11417 by T. */
11418 && !(DECL_TI_TEMPLATE (t)
11419 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
11420 spec = t;
11421 }
11422
11423 if (!spec)
11424 {
11425 tmpl = DECL_TI_TEMPLATE (t);
11426 gen_tmpl = most_general_template (tmpl);
11427 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
11428 if (argvec != error_mark_node)
11429 argvec = (coerce_innermost_template_parms
11430 (DECL_TEMPLATE_PARMS (gen_tmpl),
11431 argvec, t, complain,
11432 /*all*/true, /*defarg*/true));
11433 if (argvec == error_mark_node)
11434 RETURN (error_mark_node);
11435 hash = hash_tmpl_and_args (gen_tmpl, argvec);
11436 spec = retrieve_specialization (gen_tmpl, argvec, hash);
11437 }
11438 }
11439 else
11440 {
11441 /* A local variable. */
11442 local_p = true;
11443 /* Subsequent calls to pushdecl will fill this in. */
11444 ctx = NULL_TREE;
11445 spec = retrieve_local_specialization (t);
11446 }
11447 /* If we already have the specialization we need, there is
11448 nothing more to do. */
11449 if (spec)
11450 {
11451 r = spec;
11452 break;
11453 }
11454
11455 /* Create a new node for the specialization we need. */
11456 r = copy_decl (t);
11457 if (type == NULL_TREE)
11458 {
11459 if (is_typedef_decl (t))
11460 type = DECL_ORIGINAL_TYPE (t);
11461 else
11462 type = TREE_TYPE (t);
11463 if (VAR_P (t)
11464 && VAR_HAD_UNKNOWN_BOUND (t)
11465 && type != error_mark_node)
11466 type = strip_array_domain (type);
11467 type = tsubst (type, args, complain, in_decl);
11468 }
11469 if (VAR_P (r))
11470 {
11471 /* Even if the original location is out of scope, the
11472 newly substituted one is not. */
11473 DECL_DEAD_FOR_LOCAL (r) = 0;
11474 DECL_INITIALIZED_P (r) = 0;
11475 DECL_TEMPLATE_INSTANTIATED (r) = 0;
11476 if (type == error_mark_node)
11477 RETURN (error_mark_node);
11478 if (TREE_CODE (type) == FUNCTION_TYPE)
11479 {
11480 /* It may seem that this case cannot occur, since:
11481
11482 typedef void f();
11483 void g() { f x; }
11484
11485 declares a function, not a variable. However:
11486
11487 typedef void f();
11488 template <typename T> void g() { T t; }
11489 template void g<f>();
11490
11491 is an attempt to declare a variable with function
11492 type. */
11493 error ("variable %qD has function type",
11494 /* R is not yet sufficiently initialized, so we
11495 just use its name. */
11496 DECL_NAME (r));
11497 RETURN (error_mark_node);
11498 }
11499 type = complete_type (type);
11500 /* Wait until cp_finish_decl to set this again, to handle
11501 circular dependency (template/instantiate6.C). */
11502 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
11503 type = check_var_type (DECL_NAME (r), type);
11504
11505 if (DECL_HAS_VALUE_EXPR_P (t))
11506 {
11507 tree ve = DECL_VALUE_EXPR (t);
11508 ve = tsubst_expr (ve, args, complain, in_decl,
11509 /*constant_expression_p=*/false);
11510 if (REFERENCE_REF_P (ve))
11511 {
11512 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
11513 ve = TREE_OPERAND (ve, 0);
11514 }
11515 SET_DECL_VALUE_EXPR (r, ve);
11516 }
11517 if (CP_DECL_THREAD_LOCAL_P (r)
11518 && !processing_template_decl)
11519 set_decl_tls_model (r, decl_default_tls_model (r));
11520 }
11521 else if (DECL_SELF_REFERENCE_P (t))
11522 SET_DECL_SELF_REFERENCE_P (r);
11523 TREE_TYPE (r) = type;
11524 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
11525 DECL_CONTEXT (r) = ctx;
11526 /* Clear out the mangled name and RTL for the instantiation. */
11527 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
11528 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11529 SET_DECL_RTL (r, NULL);
11530 /* The initializer must not be expanded until it is required;
11531 see [temp.inst]. */
11532 DECL_INITIAL (r) = NULL_TREE;
11533 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
11534 SET_DECL_RTL (r, NULL);
11535 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
11536 if (VAR_P (r))
11537 {
11538 /* Possibly limit visibility based on template args. */
11539 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
11540 if (DECL_VISIBILITY_SPECIFIED (t))
11541 {
11542 DECL_VISIBILITY_SPECIFIED (r) = 0;
11543 DECL_ATTRIBUTES (r)
11544 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
11545 }
11546 determine_visibility (r);
11547 }
11548
11549 if (!local_p)
11550 {
11551 /* A static data member declaration is always marked
11552 external when it is declared in-class, even if an
11553 initializer is present. We mimic the non-template
11554 processing here. */
11555 DECL_EXTERNAL (r) = 1;
11556 if (DECL_NAMESPACE_SCOPE_P (t))
11557 DECL_NOT_REALLY_EXTERN (r) = 1;
11558
11559 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
11560 SET_DECL_IMPLICIT_INSTANTIATION (r);
11561 register_specialization (r, gen_tmpl, argvec, false, hash);
11562 }
11563 else if (!cp_unevaluated_operand)
11564 register_local_specialization (r, t);
11565
11566 DECL_CHAIN (r) = NULL_TREE;
11567
11568 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
11569 /*flags=*/0,
11570 args, complain, in_decl);
11571
11572 /* Preserve a typedef that names a type. */
11573 if (is_typedef_decl (r))
11574 {
11575 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11576 set_underlying_type (r);
11577 if (TYPE_DECL_ALIAS_P (r) && type != error_mark_node)
11578 /* An alias template specialization can be dependent
11579 even if its underlying type is not. */
11580 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
11581 }
11582
11583 layout_decl (r, 0);
11584 }
11585 break;
11586
11587 default:
11588 gcc_unreachable ();
11589 }
11590 #undef RETURN
11591
11592 out:
11593 /* Restore the file and line information. */
11594 input_location = saved_loc;
11595
11596 return r;
11597 }
11598
11599 /* Substitute into the ARG_TYPES of a function type.
11600 If END is a TREE_CHAIN, leave it and any following types
11601 un-substituted. */
11602
11603 static tree
11604 tsubst_arg_types (tree arg_types,
11605 tree args,
11606 tree end,
11607 tsubst_flags_t complain,
11608 tree in_decl)
11609 {
11610 tree remaining_arg_types;
11611 tree type = NULL_TREE;
11612 int i = 1;
11613 tree expanded_args = NULL_TREE;
11614 tree default_arg;
11615
11616 if (!arg_types || arg_types == void_list_node || arg_types == end)
11617 return arg_types;
11618
11619 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11620 args, end, complain, in_decl);
11621 if (remaining_arg_types == error_mark_node)
11622 return error_mark_node;
11623
11624 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11625 {
11626 /* For a pack expansion, perform substitution on the
11627 entire expression. Later on, we'll handle the arguments
11628 one-by-one. */
11629 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11630 args, complain, in_decl);
11631
11632 if (TREE_CODE (expanded_args) == TREE_VEC)
11633 /* So that we'll spin through the parameters, one by one. */
11634 i = TREE_VEC_LENGTH (expanded_args);
11635 else
11636 {
11637 /* We only partially substituted into the parameter
11638 pack. Our type is TYPE_PACK_EXPANSION. */
11639 type = expanded_args;
11640 expanded_args = NULL_TREE;
11641 }
11642 }
11643
11644 while (i > 0) {
11645 --i;
11646
11647 if (expanded_args)
11648 type = TREE_VEC_ELT (expanded_args, i);
11649 else if (!type)
11650 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11651
11652 if (type == error_mark_node)
11653 return error_mark_node;
11654 if (VOID_TYPE_P (type))
11655 {
11656 if (complain & tf_error)
11657 {
11658 error ("invalid parameter type %qT", type);
11659 if (in_decl)
11660 error ("in declaration %q+D", in_decl);
11661 }
11662 return error_mark_node;
11663 }
11664 /* DR 657. */
11665 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11666 return error_mark_node;
11667
11668 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11669 top-level qualifiers as required. */
11670 type = cv_unqualified (type_decays_to (type));
11671
11672 /* We do not substitute into default arguments here. The standard
11673 mandates that they be instantiated only when needed, which is
11674 done in build_over_call. */
11675 default_arg = TREE_PURPOSE (arg_types);
11676
11677 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11678 {
11679 /* We've instantiated a template before its default arguments
11680 have been parsed. This can happen for a nested template
11681 class, and is not an error unless we require the default
11682 argument in a call of this function. */
11683 remaining_arg_types =
11684 tree_cons (default_arg, type, remaining_arg_types);
11685 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11686 }
11687 else
11688 remaining_arg_types =
11689 hash_tree_cons (default_arg, type, remaining_arg_types);
11690 }
11691
11692 return remaining_arg_types;
11693 }
11694
11695 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11696 *not* handle the exception-specification for FNTYPE, because the
11697 initial substitution of explicitly provided template parameters
11698 during argument deduction forbids substitution into the
11699 exception-specification:
11700
11701 [temp.deduct]
11702
11703 All references in the function type of the function template to the
11704 corresponding template parameters are replaced by the specified tem-
11705 plate argument values. If a substitution in a template parameter or
11706 in the function type of the function template results in an invalid
11707 type, type deduction fails. [Note: The equivalent substitution in
11708 exception specifications is done only when the function is instanti-
11709 ated, at which point a program is ill-formed if the substitution
11710 results in an invalid type.] */
11711
11712 static tree
11713 tsubst_function_type (tree t,
11714 tree args,
11715 tsubst_flags_t complain,
11716 tree in_decl)
11717 {
11718 tree return_type;
11719 tree arg_types = NULL_TREE;
11720 tree fntype;
11721
11722 /* The TYPE_CONTEXT is not used for function/method types. */
11723 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11724
11725 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
11726 failure. */
11727 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
11728
11729 if (late_return_type_p)
11730 {
11731 /* Substitute the argument types. */
11732 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11733 complain, in_decl);
11734 if (arg_types == error_mark_node)
11735 return error_mark_node;
11736
11737 tree save_ccp = current_class_ptr;
11738 tree save_ccr = current_class_ref;
11739 tree this_type = (TREE_CODE (t) == METHOD_TYPE
11740 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
11741 bool do_inject = this_type && CLASS_TYPE_P (this_type);
11742 if (do_inject)
11743 {
11744 /* DR 1207: 'this' is in scope in the trailing return type. */
11745 inject_this_parameter (this_type, cp_type_quals (this_type));
11746 }
11747
11748 /* Substitute the return type. */
11749 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11750
11751 if (do_inject)
11752 {
11753 current_class_ptr = save_ccp;
11754 current_class_ref = save_ccr;
11755 }
11756 }
11757 else
11758 /* Substitute the return type. */
11759 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11760
11761 if (return_type == error_mark_node)
11762 return error_mark_node;
11763 /* DR 486 clarifies that creation of a function type with an
11764 invalid return type is a deduction failure. */
11765 if (TREE_CODE (return_type) == ARRAY_TYPE
11766 || TREE_CODE (return_type) == FUNCTION_TYPE)
11767 {
11768 if (complain & tf_error)
11769 {
11770 if (TREE_CODE (return_type) == ARRAY_TYPE)
11771 error ("function returning an array");
11772 else
11773 error ("function returning a function");
11774 }
11775 return error_mark_node;
11776 }
11777 /* And DR 657. */
11778 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11779 return error_mark_node;
11780
11781 if (!late_return_type_p)
11782 {
11783 /* Substitute the argument types. */
11784 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11785 complain, in_decl);
11786 if (arg_types == error_mark_node)
11787 return error_mark_node;
11788 }
11789
11790 /* Construct a new type node and return it. */
11791 if (TREE_CODE (t) == FUNCTION_TYPE)
11792 {
11793 fntype = build_function_type (return_type, arg_types);
11794 fntype = apply_memfn_quals (fntype,
11795 type_memfn_quals (t),
11796 type_memfn_rqual (t));
11797 }
11798 else
11799 {
11800 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11801 /* Don't pick up extra function qualifiers from the basetype. */
11802 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11803 if (! MAYBE_CLASS_TYPE_P (r))
11804 {
11805 /* [temp.deduct]
11806
11807 Type deduction may fail for any of the following
11808 reasons:
11809
11810 -- Attempting to create "pointer to member of T" when T
11811 is not a class type. */
11812 if (complain & tf_error)
11813 error ("creating pointer to member function of non-class type %qT",
11814 r);
11815 return error_mark_node;
11816 }
11817
11818 fntype = build_method_type_directly (r, return_type,
11819 TREE_CHAIN (arg_types));
11820 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11821 }
11822 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11823
11824 if (late_return_type_p)
11825 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
11826
11827 return fntype;
11828 }
11829
11830 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11831 ARGS into that specification, and return the substituted
11832 specification. If there is no specification, return NULL_TREE. */
11833
11834 static tree
11835 tsubst_exception_specification (tree fntype,
11836 tree args,
11837 tsubst_flags_t complain,
11838 tree in_decl,
11839 bool defer_ok)
11840 {
11841 tree specs;
11842 tree new_specs;
11843
11844 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11845 new_specs = NULL_TREE;
11846 if (specs && TREE_PURPOSE (specs))
11847 {
11848 /* A noexcept-specifier. */
11849 tree expr = TREE_PURPOSE (specs);
11850 if (TREE_CODE (expr) == INTEGER_CST)
11851 new_specs = expr;
11852 else if (defer_ok)
11853 {
11854 /* Defer instantiation of noexcept-specifiers to avoid
11855 excessive instantiations (c++/49107). */
11856 new_specs = make_node (DEFERRED_NOEXCEPT);
11857 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11858 {
11859 /* We already partially instantiated this member template,
11860 so combine the new args with the old. */
11861 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11862 = DEFERRED_NOEXCEPT_PATTERN (expr);
11863 DEFERRED_NOEXCEPT_ARGS (new_specs)
11864 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11865 }
11866 else
11867 {
11868 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11869 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11870 }
11871 }
11872 else
11873 new_specs = tsubst_copy_and_build
11874 (expr, args, complain, in_decl, /*function_p=*/false,
11875 /*integral_constant_expression_p=*/true);
11876 new_specs = build_noexcept_spec (new_specs, complain);
11877 }
11878 else if (specs)
11879 {
11880 if (! TREE_VALUE (specs))
11881 new_specs = specs;
11882 else
11883 while (specs)
11884 {
11885 tree spec;
11886 int i, len = 1;
11887 tree expanded_specs = NULL_TREE;
11888
11889 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11890 {
11891 /* Expand the pack expansion type. */
11892 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11893 args, complain,
11894 in_decl);
11895
11896 if (expanded_specs == error_mark_node)
11897 return error_mark_node;
11898 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11899 len = TREE_VEC_LENGTH (expanded_specs);
11900 else
11901 {
11902 /* We're substituting into a member template, so
11903 we got a TYPE_PACK_EXPANSION back. Add that
11904 expansion and move on. */
11905 gcc_assert (TREE_CODE (expanded_specs)
11906 == TYPE_PACK_EXPANSION);
11907 new_specs = add_exception_specifier (new_specs,
11908 expanded_specs,
11909 complain);
11910 specs = TREE_CHAIN (specs);
11911 continue;
11912 }
11913 }
11914
11915 for (i = 0; i < len; ++i)
11916 {
11917 if (expanded_specs)
11918 spec = TREE_VEC_ELT (expanded_specs, i);
11919 else
11920 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11921 if (spec == error_mark_node)
11922 return spec;
11923 new_specs = add_exception_specifier (new_specs, spec,
11924 complain);
11925 }
11926
11927 specs = TREE_CHAIN (specs);
11928 }
11929 }
11930 return new_specs;
11931 }
11932
11933 /* Take the tree structure T and replace template parameters used
11934 therein with the argument vector ARGS. IN_DECL is an associated
11935 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11936 Issue error and warning messages under control of COMPLAIN. Note
11937 that we must be relatively non-tolerant of extensions here, in
11938 order to preserve conformance; if we allow substitutions that
11939 should not be allowed, we may allow argument deductions that should
11940 not succeed, and therefore report ambiguous overload situations
11941 where there are none. In theory, we could allow the substitution,
11942 but indicate that it should have failed, and allow our caller to
11943 make sure that the right thing happens, but we don't try to do this
11944 yet.
11945
11946 This function is used for dealing with types, decls and the like;
11947 for expressions, use tsubst_expr or tsubst_copy. */
11948
11949 tree
11950 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11951 {
11952 enum tree_code code;
11953 tree type, r = NULL_TREE;
11954
11955 if (t == NULL_TREE || t == error_mark_node
11956 || t == integer_type_node
11957 || t == void_type_node
11958 || t == char_type_node
11959 || t == unknown_type_node
11960 || TREE_CODE (t) == NAMESPACE_DECL
11961 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11962 return t;
11963
11964 if (DECL_P (t))
11965 return tsubst_decl (t, args, complain);
11966
11967 if (args == NULL_TREE)
11968 return t;
11969
11970 code = TREE_CODE (t);
11971
11972 if (code == IDENTIFIER_NODE)
11973 type = IDENTIFIER_TYPE_VALUE (t);
11974 else
11975 type = TREE_TYPE (t);
11976
11977 gcc_assert (type != unknown_type_node);
11978
11979 /* Reuse typedefs. We need to do this to handle dependent attributes,
11980 such as attribute aligned. */
11981 if (TYPE_P (t)
11982 && typedef_variant_p (t))
11983 {
11984 tree decl = TYPE_NAME (t);
11985
11986 if (alias_template_specialization_p (t))
11987 {
11988 /* DECL represents an alias template and we want to
11989 instantiate it. */
11990 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11991 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11992 r = instantiate_alias_template (tmpl, gen_args, complain);
11993 }
11994 else if (DECL_CLASS_SCOPE_P (decl)
11995 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11996 && uses_template_parms (DECL_CONTEXT (decl)))
11997 {
11998 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11999 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
12000 r = retrieve_specialization (tmpl, gen_args, 0);
12001 }
12002 else if (DECL_FUNCTION_SCOPE_P (decl)
12003 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
12004 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
12005 r = retrieve_local_specialization (decl);
12006 else
12007 /* The typedef is from a non-template context. */
12008 return t;
12009
12010 if (r)
12011 {
12012 r = TREE_TYPE (r);
12013 r = cp_build_qualified_type_real
12014 (r, cp_type_quals (t) | cp_type_quals (r),
12015 complain | tf_ignore_bad_quals);
12016 return r;
12017 }
12018 else
12019 {
12020 /* We don't have an instantiation yet, so drop the typedef. */
12021 int quals = cp_type_quals (t);
12022 t = DECL_ORIGINAL_TYPE (decl);
12023 t = cp_build_qualified_type_real (t, quals,
12024 complain | tf_ignore_bad_quals);
12025 }
12026 }
12027
12028 if (type
12029 && code != TYPENAME_TYPE
12030 && code != TEMPLATE_TYPE_PARM
12031 && code != IDENTIFIER_NODE
12032 && code != FUNCTION_TYPE
12033 && code != METHOD_TYPE)
12034 type = tsubst (type, args, complain, in_decl);
12035 if (type == error_mark_node)
12036 return error_mark_node;
12037
12038 switch (code)
12039 {
12040 case RECORD_TYPE:
12041 case UNION_TYPE:
12042 case ENUMERAL_TYPE:
12043 return tsubst_aggr_type (t, args, complain, in_decl,
12044 /*entering_scope=*/0);
12045
12046 case ERROR_MARK:
12047 case IDENTIFIER_NODE:
12048 case VOID_TYPE:
12049 case REAL_TYPE:
12050 case COMPLEX_TYPE:
12051 case VECTOR_TYPE:
12052 case BOOLEAN_TYPE:
12053 case NULLPTR_TYPE:
12054 case LANG_TYPE:
12055 return t;
12056
12057 case INTEGER_TYPE:
12058 if (t == integer_type_node)
12059 return t;
12060
12061 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
12062 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
12063 return t;
12064
12065 {
12066 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
12067
12068 max = tsubst_expr (omax, args, complain, in_decl,
12069 /*integral_constant_expression_p=*/false);
12070
12071 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
12072 needed. */
12073 if (TREE_CODE (max) == NOP_EXPR
12074 && TREE_SIDE_EFFECTS (omax)
12075 && !TREE_TYPE (max))
12076 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
12077
12078 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
12079 with TREE_SIDE_EFFECTS that indicates this is not an integral
12080 constant expression. */
12081 if (processing_template_decl
12082 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
12083 {
12084 gcc_assert (TREE_CODE (max) == NOP_EXPR);
12085 TREE_SIDE_EFFECTS (max) = 1;
12086 }
12087
12088 return compute_array_index_type (NULL_TREE, max, complain);
12089 }
12090
12091 case TEMPLATE_TYPE_PARM:
12092 case TEMPLATE_TEMPLATE_PARM:
12093 case BOUND_TEMPLATE_TEMPLATE_PARM:
12094 case TEMPLATE_PARM_INDEX:
12095 {
12096 int idx;
12097 int level;
12098 int levels;
12099 tree arg = NULL_TREE;
12100
12101 r = NULL_TREE;
12102
12103 gcc_assert (TREE_VEC_LENGTH (args) > 0);
12104 template_parm_level_and_index (t, &level, &idx);
12105
12106 levels = TMPL_ARGS_DEPTH (args);
12107 if (level <= levels
12108 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
12109 {
12110 arg = TMPL_ARG (args, level, idx);
12111
12112 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
12113 {
12114 /* See through ARGUMENT_PACK_SELECT arguments. */
12115 arg = ARGUMENT_PACK_SELECT_ARG (arg);
12116 /* If the selected argument is an expansion E, that most
12117 likely means we were called from
12118 gen_elem_of_pack_expansion_instantiation during the
12119 substituting of pack an argument pack (which Ith
12120 element is a pack expansion, where I is
12121 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
12122 In this case, the Ith element resulting from this
12123 substituting is going to be a pack expansion, which
12124 pattern is the pattern of E. Let's return the
12125 pattern of E, and
12126 gen_elem_of_pack_expansion_instantiation will
12127 build the resulting pack expansion from it. */
12128 if (PACK_EXPANSION_P (arg))
12129 {
12130 /* Make sure we aren't throwing away arg info. */
12131 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
12132 arg = PACK_EXPANSION_PATTERN (arg);
12133 }
12134 }
12135 }
12136
12137 if (arg == error_mark_node)
12138 return error_mark_node;
12139 else if (arg != NULL_TREE)
12140 {
12141 if (ARGUMENT_PACK_P (arg))
12142 /* If ARG is an argument pack, we don't actually want to
12143 perform a substitution here, because substitutions
12144 for argument packs are only done
12145 element-by-element. We can get to this point when
12146 substituting the type of a non-type template
12147 parameter pack, when that type actually contains
12148 template parameter packs from an outer template, e.g.,
12149
12150 template<typename... Types> struct A {
12151 template<Types... Values> struct B { };
12152 }; */
12153 return t;
12154
12155 if (code == TEMPLATE_TYPE_PARM)
12156 {
12157 int quals;
12158 gcc_assert (TYPE_P (arg));
12159
12160 quals = cp_type_quals (arg) | cp_type_quals (t);
12161
12162 return cp_build_qualified_type_real
12163 (arg, quals, complain | tf_ignore_bad_quals);
12164 }
12165 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12166 {
12167 /* We are processing a type constructed from a
12168 template template parameter. */
12169 tree argvec = tsubst (TYPE_TI_ARGS (t),
12170 args, complain, in_decl);
12171 if (argvec == error_mark_node)
12172 return error_mark_node;
12173
12174 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
12175 || TREE_CODE (arg) == TEMPLATE_DECL
12176 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
12177
12178 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
12179 /* Consider this code:
12180
12181 template <template <class> class Template>
12182 struct Internal {
12183 template <class Arg> using Bind = Template<Arg>;
12184 };
12185
12186 template <template <class> class Template, class Arg>
12187 using Instantiate = Template<Arg>; //#0
12188
12189 template <template <class> class Template,
12190 class Argument>
12191 using Bind =
12192 Instantiate<Internal<Template>::template Bind,
12193 Argument>; //#1
12194
12195 When #1 is parsed, the
12196 BOUND_TEMPLATE_TEMPLATE_PARM representing the
12197 parameter `Template' in #0 matches the
12198 UNBOUND_CLASS_TEMPLATE representing the argument
12199 `Internal<Template>::template Bind'; We then want
12200 to assemble the type `Bind<Argument>' that can't
12201 be fully created right now, because
12202 `Internal<Template>' not being complete, the Bind
12203 template cannot be looked up in that context. So
12204 we need to "store" `Bind<Argument>' for later
12205 when the context of Bind becomes complete. Let's
12206 store that in a TYPENAME_TYPE. */
12207 return make_typename_type (TYPE_CONTEXT (arg),
12208 build_nt (TEMPLATE_ID_EXPR,
12209 TYPE_IDENTIFIER (arg),
12210 argvec),
12211 typename_type,
12212 complain);
12213
12214 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
12215 are resolving nested-types in the signature of a
12216 member function templates. Otherwise ARG is a
12217 TEMPLATE_DECL and is the real template to be
12218 instantiated. */
12219 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12220 arg = TYPE_NAME (arg);
12221
12222 r = lookup_template_class (arg,
12223 argvec, in_decl,
12224 DECL_CONTEXT (arg),
12225 /*entering_scope=*/0,
12226 complain);
12227 return cp_build_qualified_type_real
12228 (r, cp_type_quals (t) | cp_type_quals (r), complain);
12229 }
12230 else
12231 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
12232 return convert_from_reference (unshare_expr (arg));
12233 }
12234
12235 if (level == 1)
12236 /* This can happen during the attempted tsubst'ing in
12237 unify. This means that we don't yet have any information
12238 about the template parameter in question. */
12239 return t;
12240
12241 /* Early in template argument deduction substitution, we don't
12242 want to reduce the level of 'auto', or it will be confused
12243 with a normal template parm in subsequent deduction. */
12244 if (is_auto (t) && (complain & tf_partial))
12245 return t;
12246
12247 /* If we get here, we must have been looking at a parm for a
12248 more deeply nested template. Make a new version of this
12249 template parameter, but with a lower level. */
12250 switch (code)
12251 {
12252 case TEMPLATE_TYPE_PARM:
12253 case TEMPLATE_TEMPLATE_PARM:
12254 case BOUND_TEMPLATE_TEMPLATE_PARM:
12255 if (cp_type_quals (t))
12256 {
12257 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
12258 r = cp_build_qualified_type_real
12259 (r, cp_type_quals (t),
12260 complain | (code == TEMPLATE_TYPE_PARM
12261 ? tf_ignore_bad_quals : 0));
12262 }
12263 else
12264 {
12265 r = copy_type (t);
12266 TEMPLATE_TYPE_PARM_INDEX (r)
12267 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
12268 r, levels, args, complain);
12269 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
12270 TYPE_MAIN_VARIANT (r) = r;
12271 TYPE_POINTER_TO (r) = NULL_TREE;
12272 TYPE_REFERENCE_TO (r) = NULL_TREE;
12273
12274 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
12275 /* We have reduced the level of the template
12276 template parameter, but not the levels of its
12277 template parameters, so canonical_type_parameter
12278 will not be able to find the canonical template
12279 template parameter for this level. Thus, we
12280 require structural equality checking to compare
12281 TEMPLATE_TEMPLATE_PARMs. */
12282 SET_TYPE_STRUCTURAL_EQUALITY (r);
12283 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
12284 SET_TYPE_STRUCTURAL_EQUALITY (r);
12285 else
12286 TYPE_CANONICAL (r) = canonical_type_parameter (r);
12287
12288 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
12289 {
12290 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
12291 complain, in_decl);
12292 if (argvec == error_mark_node)
12293 return error_mark_node;
12294
12295 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
12296 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
12297 }
12298 }
12299 break;
12300
12301 case TEMPLATE_PARM_INDEX:
12302 r = reduce_template_parm_level (t, type, levels, args, complain);
12303 break;
12304
12305 default:
12306 gcc_unreachable ();
12307 }
12308
12309 return r;
12310 }
12311
12312 case TREE_LIST:
12313 {
12314 tree purpose, value, chain;
12315
12316 if (t == void_list_node)
12317 return t;
12318
12319 purpose = TREE_PURPOSE (t);
12320 if (purpose)
12321 {
12322 purpose = tsubst (purpose, args, complain, in_decl);
12323 if (purpose == error_mark_node)
12324 return error_mark_node;
12325 }
12326 value = TREE_VALUE (t);
12327 if (value)
12328 {
12329 value = tsubst (value, args, complain, in_decl);
12330 if (value == error_mark_node)
12331 return error_mark_node;
12332 }
12333 chain = TREE_CHAIN (t);
12334 if (chain && chain != void_type_node)
12335 {
12336 chain = tsubst (chain, args, complain, in_decl);
12337 if (chain == error_mark_node)
12338 return error_mark_node;
12339 }
12340 if (purpose == TREE_PURPOSE (t)
12341 && value == TREE_VALUE (t)
12342 && chain == TREE_CHAIN (t))
12343 return t;
12344 return hash_tree_cons (purpose, value, chain);
12345 }
12346
12347 case TREE_BINFO:
12348 /* We should never be tsubsting a binfo. */
12349 gcc_unreachable ();
12350
12351 case TREE_VEC:
12352 /* A vector of template arguments. */
12353 gcc_assert (!type);
12354 return tsubst_template_args (t, args, complain, in_decl);
12355
12356 case POINTER_TYPE:
12357 case REFERENCE_TYPE:
12358 {
12359 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
12360 return t;
12361
12362 /* [temp.deduct]
12363
12364 Type deduction may fail for any of the following
12365 reasons:
12366
12367 -- Attempting to create a pointer to reference type.
12368 -- Attempting to create a reference to a reference type or
12369 a reference to void.
12370
12371 Core issue 106 says that creating a reference to a reference
12372 during instantiation is no longer a cause for failure. We
12373 only enforce this check in strict C++98 mode. */
12374 if ((TREE_CODE (type) == REFERENCE_TYPE
12375 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
12376 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
12377 {
12378 static location_t last_loc;
12379
12380 /* We keep track of the last time we issued this error
12381 message to avoid spewing a ton of messages during a
12382 single bad template instantiation. */
12383 if (complain & tf_error
12384 && last_loc != input_location)
12385 {
12386 if (VOID_TYPE_P (type))
12387 error ("forming reference to void");
12388 else if (code == POINTER_TYPE)
12389 error ("forming pointer to reference type %qT", type);
12390 else
12391 error ("forming reference to reference type %qT", type);
12392 last_loc = input_location;
12393 }
12394
12395 return error_mark_node;
12396 }
12397 else if (TREE_CODE (type) == FUNCTION_TYPE
12398 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
12399 || type_memfn_rqual (type) != REF_QUAL_NONE))
12400 {
12401 if (complain & tf_error)
12402 {
12403 if (code == POINTER_TYPE)
12404 error ("forming pointer to qualified function type %qT",
12405 type);
12406 else
12407 error ("forming reference to qualified function type %qT",
12408 type);
12409 }
12410 return error_mark_node;
12411 }
12412 else if (code == POINTER_TYPE)
12413 {
12414 r = build_pointer_type (type);
12415 if (TREE_CODE (type) == METHOD_TYPE)
12416 r = build_ptrmemfunc_type (r);
12417 }
12418 else if (TREE_CODE (type) == REFERENCE_TYPE)
12419 /* In C++0x, during template argument substitution, when there is an
12420 attempt to create a reference to a reference type, reference
12421 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
12422
12423 "If a template-argument for a template-parameter T names a type
12424 that is a reference to a type A, an attempt to create the type
12425 'lvalue reference to cv T' creates the type 'lvalue reference to
12426 A,' while an attempt to create the type type rvalue reference to
12427 cv T' creates the type T"
12428 */
12429 r = cp_build_reference_type
12430 (TREE_TYPE (type),
12431 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
12432 else
12433 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
12434 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12435
12436 if (r != error_mark_node)
12437 /* Will this ever be needed for TYPE_..._TO values? */
12438 layout_type (r);
12439
12440 return r;
12441 }
12442 case OFFSET_TYPE:
12443 {
12444 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
12445 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
12446 {
12447 /* [temp.deduct]
12448
12449 Type deduction may fail for any of the following
12450 reasons:
12451
12452 -- Attempting to create "pointer to member of T" when T
12453 is not a class type. */
12454 if (complain & tf_error)
12455 error ("creating pointer to member of non-class type %qT", r);
12456 return error_mark_node;
12457 }
12458 if (TREE_CODE (type) == REFERENCE_TYPE)
12459 {
12460 if (complain & tf_error)
12461 error ("creating pointer to member reference type %qT", type);
12462 return error_mark_node;
12463 }
12464 if (VOID_TYPE_P (type))
12465 {
12466 if (complain & tf_error)
12467 error ("creating pointer to member of type void");
12468 return error_mark_node;
12469 }
12470 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
12471 if (TREE_CODE (type) == FUNCTION_TYPE)
12472 {
12473 /* The type of the implicit object parameter gets its
12474 cv-qualifiers from the FUNCTION_TYPE. */
12475 tree memptr;
12476 tree method_type
12477 = build_memfn_type (type, r, type_memfn_quals (type),
12478 type_memfn_rqual (type));
12479 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
12480 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
12481 complain);
12482 }
12483 else
12484 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
12485 cp_type_quals (t),
12486 complain);
12487 }
12488 case FUNCTION_TYPE:
12489 case METHOD_TYPE:
12490 {
12491 tree fntype;
12492 tree specs;
12493 fntype = tsubst_function_type (t, args, complain, in_decl);
12494 if (fntype == error_mark_node)
12495 return error_mark_node;
12496
12497 /* Substitute the exception specification. */
12498 specs = tsubst_exception_specification (t, args, complain,
12499 in_decl, /*defer_ok*/true);
12500 if (specs == error_mark_node)
12501 return error_mark_node;
12502 if (specs)
12503 fntype = build_exception_variant (fntype, specs);
12504 return fntype;
12505 }
12506 case ARRAY_TYPE:
12507 {
12508 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
12509 if (domain == error_mark_node)
12510 return error_mark_node;
12511
12512 /* As an optimization, we avoid regenerating the array type if
12513 it will obviously be the same as T. */
12514 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
12515 return t;
12516
12517 /* These checks should match the ones in create_array_type_for_decl.
12518
12519 [temp.deduct]
12520
12521 The deduction may fail for any of the following reasons:
12522
12523 -- Attempting to create an array with an element type that
12524 is void, a function type, or a reference type, or [DR337]
12525 an abstract class type. */
12526 if (VOID_TYPE_P (type)
12527 || TREE_CODE (type) == FUNCTION_TYPE
12528 || (TREE_CODE (type) == ARRAY_TYPE
12529 && TYPE_DOMAIN (type) == NULL_TREE)
12530 || TREE_CODE (type) == REFERENCE_TYPE)
12531 {
12532 if (complain & tf_error)
12533 error ("creating array of %qT", type);
12534 return error_mark_node;
12535 }
12536
12537 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
12538 return error_mark_node;
12539
12540 r = build_cplus_array_type (type, domain);
12541
12542 if (TYPE_USER_ALIGN (t))
12543 {
12544 TYPE_ALIGN (r) = TYPE_ALIGN (t);
12545 TYPE_USER_ALIGN (r) = 1;
12546 }
12547
12548 return r;
12549 }
12550
12551 case TYPENAME_TYPE:
12552 {
12553 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12554 in_decl, /*entering_scope=*/1);
12555 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
12556 complain, in_decl);
12557
12558 if (ctx == error_mark_node || f == error_mark_node)
12559 return error_mark_node;
12560
12561 if (!MAYBE_CLASS_TYPE_P (ctx))
12562 {
12563 if (complain & tf_error)
12564 error ("%qT is not a class, struct, or union type", ctx);
12565 return error_mark_node;
12566 }
12567 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
12568 {
12569 /* Normally, make_typename_type does not require that the CTX
12570 have complete type in order to allow things like:
12571
12572 template <class T> struct S { typename S<T>::X Y; };
12573
12574 But, such constructs have already been resolved by this
12575 point, so here CTX really should have complete type, unless
12576 it's a partial instantiation. */
12577 ctx = complete_type (ctx);
12578 if (!COMPLETE_TYPE_P (ctx))
12579 {
12580 if (complain & tf_error)
12581 cxx_incomplete_type_error (NULL_TREE, ctx);
12582 return error_mark_node;
12583 }
12584 }
12585
12586 f = make_typename_type (ctx, f, typename_type,
12587 complain | tf_keep_type_decl);
12588 if (f == error_mark_node)
12589 return f;
12590 if (TREE_CODE (f) == TYPE_DECL)
12591 {
12592 complain |= tf_ignore_bad_quals;
12593 f = TREE_TYPE (f);
12594 }
12595
12596 if (TREE_CODE (f) != TYPENAME_TYPE)
12597 {
12598 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
12599 {
12600 if (complain & tf_error)
12601 error ("%qT resolves to %qT, which is not an enumeration type",
12602 t, f);
12603 else
12604 return error_mark_node;
12605 }
12606 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12607 {
12608 if (complain & tf_error)
12609 error ("%qT resolves to %qT, which is is not a class type",
12610 t, f);
12611 else
12612 return error_mark_node;
12613 }
12614 }
12615
12616 return cp_build_qualified_type_real
12617 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12618 }
12619
12620 case UNBOUND_CLASS_TEMPLATE:
12621 {
12622 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12623 in_decl, /*entering_scope=*/1);
12624 tree name = TYPE_IDENTIFIER (t);
12625 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12626
12627 if (ctx == error_mark_node || name == error_mark_node)
12628 return error_mark_node;
12629
12630 if (parm_list)
12631 parm_list = tsubst_template_parms (parm_list, args, complain);
12632 return make_unbound_class_template (ctx, name, parm_list, complain);
12633 }
12634
12635 case TYPEOF_TYPE:
12636 {
12637 tree type;
12638
12639 ++cp_unevaluated_operand;
12640 ++c_inhibit_evaluation_warnings;
12641
12642 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12643 complain, in_decl,
12644 /*integral_constant_expression_p=*/false);
12645
12646 --cp_unevaluated_operand;
12647 --c_inhibit_evaluation_warnings;
12648
12649 type = finish_typeof (type);
12650 return cp_build_qualified_type_real (type,
12651 cp_type_quals (t)
12652 | cp_type_quals (type),
12653 complain);
12654 }
12655
12656 case DECLTYPE_TYPE:
12657 {
12658 tree type;
12659
12660 ++cp_unevaluated_operand;
12661 ++c_inhibit_evaluation_warnings;
12662
12663 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12664 complain|tf_decltype, in_decl,
12665 /*function_p*/false,
12666 /*integral_constant_expression*/false);
12667
12668 --cp_unevaluated_operand;
12669 --c_inhibit_evaluation_warnings;
12670
12671 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12672 type = lambda_capture_field_type (type,
12673 DECLTYPE_FOR_INIT_CAPTURE (t));
12674 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12675 type = lambda_proxy_type (type);
12676 else
12677 {
12678 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12679 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12680 && EXPR_P (type))
12681 /* In a template ~id could be either a complement expression
12682 or an unqualified-id naming a destructor; if instantiating
12683 it produces an expression, it's not an id-expression or
12684 member access. */
12685 id = false;
12686 type = finish_decltype_type (type, id, complain);
12687 }
12688 return cp_build_qualified_type_real (type,
12689 cp_type_quals (t)
12690 | cp_type_quals (type),
12691 complain | tf_ignore_bad_quals);
12692 }
12693
12694 case UNDERLYING_TYPE:
12695 {
12696 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12697 complain, in_decl);
12698 return finish_underlying_type (type);
12699 }
12700
12701 case TYPE_ARGUMENT_PACK:
12702 case NONTYPE_ARGUMENT_PACK:
12703 {
12704 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12705 tree packed_out =
12706 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12707 args,
12708 complain,
12709 in_decl);
12710 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12711
12712 /* For template nontype argument packs, also substitute into
12713 the type. */
12714 if (code == NONTYPE_ARGUMENT_PACK)
12715 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12716
12717 return r;
12718 }
12719 break;
12720
12721 case VOID_CST:
12722 case INTEGER_CST:
12723 case REAL_CST:
12724 case STRING_CST:
12725 case PLUS_EXPR:
12726 case MINUS_EXPR:
12727 case NEGATE_EXPR:
12728 case NOP_EXPR:
12729 case INDIRECT_REF:
12730 case ADDR_EXPR:
12731 case CALL_EXPR:
12732 case ARRAY_REF:
12733 case SCOPE_REF:
12734 /* We should use one of the expression tsubsts for these codes. */
12735 gcc_unreachable ();
12736
12737 default:
12738 sorry ("use of %qs in template", get_tree_code_name (code));
12739 return error_mark_node;
12740 }
12741 }
12742
12743 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12744 type of the expression on the left-hand side of the "." or "->"
12745 operator. */
12746
12747 static tree
12748 tsubst_baselink (tree baselink, tree object_type,
12749 tree args, tsubst_flags_t complain, tree in_decl)
12750 {
12751 tree name;
12752 tree qualifying_scope;
12753 tree fns;
12754 tree optype;
12755 tree template_args = 0;
12756 bool template_id_p = false;
12757 bool qualified = BASELINK_QUALIFIED_P (baselink);
12758
12759 /* A baselink indicates a function from a base class. Both the
12760 BASELINK_ACCESS_BINFO and the base class referenced may
12761 indicate bases of the template class, rather than the
12762 instantiated class. In addition, lookups that were not
12763 ambiguous before may be ambiguous now. Therefore, we perform
12764 the lookup again. */
12765 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12766 qualifying_scope = tsubst (qualifying_scope, args,
12767 complain, in_decl);
12768 fns = BASELINK_FUNCTIONS (baselink);
12769 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12770 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12771 {
12772 template_id_p = true;
12773 template_args = TREE_OPERAND (fns, 1);
12774 fns = TREE_OPERAND (fns, 0);
12775 if (template_args)
12776 template_args = tsubst_template_args (template_args, args,
12777 complain, in_decl);
12778 }
12779 name = DECL_NAME (get_first_fn (fns));
12780 if (IDENTIFIER_TYPENAME_P (name))
12781 name = mangle_conv_op_name_for_type (optype);
12782 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12783 if (!baselink)
12784 return error_mark_node;
12785
12786 /* If lookup found a single function, mark it as used at this
12787 point. (If it lookup found multiple functions the one selected
12788 later by overload resolution will be marked as used at that
12789 point.) */
12790 if (BASELINK_P (baselink))
12791 fns = BASELINK_FUNCTIONS (baselink);
12792 if (!template_id_p && !really_overloaded_fn (fns)
12793 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
12794 return error_mark_node;
12795
12796 /* Add back the template arguments, if present. */
12797 if (BASELINK_P (baselink) && template_id_p)
12798 BASELINK_FUNCTIONS (baselink)
12799 = build_nt (TEMPLATE_ID_EXPR,
12800 BASELINK_FUNCTIONS (baselink),
12801 template_args);
12802 /* Update the conversion operator type. */
12803 BASELINK_OPTYPE (baselink) = optype;
12804
12805 if (!object_type)
12806 object_type = current_class_type;
12807
12808 if (qualified)
12809 baselink = adjust_result_of_qualified_name_lookup (baselink,
12810 qualifying_scope,
12811 object_type);
12812 return baselink;
12813 }
12814
12815 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12816 true if the qualified-id will be a postfix-expression in-and-of
12817 itself; false if more of the postfix-expression follows the
12818 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12819 of "&". */
12820
12821 static tree
12822 tsubst_qualified_id (tree qualified_id, tree args,
12823 tsubst_flags_t complain, tree in_decl,
12824 bool done, bool address_p)
12825 {
12826 tree expr;
12827 tree scope;
12828 tree name;
12829 bool is_template;
12830 tree template_args;
12831 location_t loc = UNKNOWN_LOCATION;
12832
12833 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12834
12835 /* Figure out what name to look up. */
12836 name = TREE_OPERAND (qualified_id, 1);
12837 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12838 {
12839 is_template = true;
12840 loc = EXPR_LOCATION (name);
12841 template_args = TREE_OPERAND (name, 1);
12842 if (template_args)
12843 template_args = tsubst_template_args (template_args, args,
12844 complain, in_decl);
12845 name = TREE_OPERAND (name, 0);
12846 }
12847 else
12848 {
12849 is_template = false;
12850 template_args = NULL_TREE;
12851 }
12852
12853 /* Substitute into the qualifying scope. When there are no ARGS, we
12854 are just trying to simplify a non-dependent expression. In that
12855 case the qualifying scope may be dependent, and, in any case,
12856 substituting will not help. */
12857 scope = TREE_OPERAND (qualified_id, 0);
12858 if (args)
12859 {
12860 scope = tsubst (scope, args, complain, in_decl);
12861 expr = tsubst_copy (name, args, complain, in_decl);
12862 }
12863 else
12864 expr = name;
12865
12866 if (dependent_scope_p (scope))
12867 {
12868 if (is_template)
12869 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12870 return build_qualified_name (NULL_TREE, scope, expr,
12871 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12872 }
12873
12874 if (!BASELINK_P (name) && !DECL_P (expr))
12875 {
12876 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12877 {
12878 /* A BIT_NOT_EXPR is used to represent a destructor. */
12879 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12880 {
12881 error ("qualifying type %qT does not match destructor name ~%qT",
12882 scope, TREE_OPERAND (expr, 0));
12883 expr = error_mark_node;
12884 }
12885 else
12886 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12887 /*is_type_p=*/0, false);
12888 }
12889 else
12890 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12891 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12892 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12893 {
12894 if (complain & tf_error)
12895 {
12896 error ("dependent-name %qE is parsed as a non-type, but "
12897 "instantiation yields a type", qualified_id);
12898 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12899 }
12900 return error_mark_node;
12901 }
12902 }
12903
12904 if (DECL_P (expr))
12905 {
12906 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12907 scope);
12908 /* Remember that there was a reference to this entity. */
12909 if (!mark_used (expr, complain) && !(complain & tf_error))
12910 return error_mark_node;
12911 }
12912
12913 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12914 {
12915 if (complain & tf_error)
12916 qualified_name_lookup_error (scope,
12917 TREE_OPERAND (qualified_id, 1),
12918 expr, input_location);
12919 return error_mark_node;
12920 }
12921
12922 if (is_template)
12923 expr = lookup_template_function (expr, template_args);
12924
12925 if (expr == error_mark_node && complain & tf_error)
12926 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12927 expr, input_location);
12928 else if (TYPE_P (scope))
12929 {
12930 expr = (adjust_result_of_qualified_name_lookup
12931 (expr, scope, current_nonlambda_class_type ()));
12932 expr = (finish_qualified_id_expr
12933 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12934 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12935 /*template_arg_p=*/false, complain));
12936 }
12937
12938 /* Expressions do not generally have reference type. */
12939 if (TREE_CODE (expr) != SCOPE_REF
12940 /* However, if we're about to form a pointer-to-member, we just
12941 want the referenced member referenced. */
12942 && TREE_CODE (expr) != OFFSET_REF)
12943 expr = convert_from_reference (expr);
12944
12945 return expr;
12946 }
12947
12948 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
12949 initializer, DECL is the substituted VAR_DECL. Other arguments are as
12950 for tsubst. */
12951
12952 static tree
12953 tsubst_init (tree init, tree decl, tree args,
12954 tsubst_flags_t complain, tree in_decl)
12955 {
12956 if (!init)
12957 return NULL_TREE;
12958
12959 init = tsubst_expr (init, args, complain, in_decl, false);
12960
12961 if (!init)
12962 {
12963 /* If we had an initializer but it
12964 instantiated to nothing,
12965 value-initialize the object. This will
12966 only occur when the initializer was a
12967 pack expansion where the parameter packs
12968 used in that expansion were of length
12969 zero. */
12970 init = build_value_init (TREE_TYPE (decl),
12971 complain);
12972 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12973 init = get_target_expr_sfinae (init, complain);
12974 }
12975
12976 return init;
12977 }
12978
12979 /* Like tsubst, but deals with expressions. This function just replaces
12980 template parms; to finish processing the resultant expression, use
12981 tsubst_copy_and_build or tsubst_expr. */
12982
12983 static tree
12984 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12985 {
12986 enum tree_code code;
12987 tree r;
12988
12989 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12990 return t;
12991
12992 code = TREE_CODE (t);
12993
12994 switch (code)
12995 {
12996 case PARM_DECL:
12997 r = retrieve_local_specialization (t);
12998
12999 if (r == NULL_TREE)
13000 {
13001 /* We get here for a use of 'this' in an NSDMI. */
13002 if (DECL_NAME (t) == this_identifier
13003 && current_function_decl
13004 && DECL_CONSTRUCTOR_P (current_function_decl))
13005 return current_class_ptr;
13006
13007 /* This can happen for a parameter name used later in a function
13008 declaration (such as in a late-specified return type). Just
13009 make a dummy decl, since it's only used for its type. */
13010 gcc_assert (cp_unevaluated_operand != 0);
13011 r = tsubst_decl (t, args, complain);
13012 /* Give it the template pattern as its context; its true context
13013 hasn't been instantiated yet and this is good enough for
13014 mangling. */
13015 DECL_CONTEXT (r) = DECL_CONTEXT (t);
13016 }
13017
13018 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13019 r = ARGUMENT_PACK_SELECT_ARG (r);
13020 if (!mark_used (r, complain) && !(complain & tf_error))
13021 return error_mark_node;
13022 return r;
13023
13024 case CONST_DECL:
13025 {
13026 tree enum_type;
13027 tree v;
13028
13029 if (DECL_TEMPLATE_PARM_P (t))
13030 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
13031 /* There is no need to substitute into namespace-scope
13032 enumerators. */
13033 if (DECL_NAMESPACE_SCOPE_P (t))
13034 return t;
13035 /* If ARGS is NULL, then T is known to be non-dependent. */
13036 if (args == NULL_TREE)
13037 return scalar_constant_value (t);
13038
13039 /* Unfortunately, we cannot just call lookup_name here.
13040 Consider:
13041
13042 template <int I> int f() {
13043 enum E { a = I };
13044 struct S { void g() { E e = a; } };
13045 };
13046
13047 When we instantiate f<7>::S::g(), say, lookup_name is not
13048 clever enough to find f<7>::a. */
13049 enum_type
13050 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13051 /*entering_scope=*/0);
13052
13053 for (v = TYPE_VALUES (enum_type);
13054 v != NULL_TREE;
13055 v = TREE_CHAIN (v))
13056 if (TREE_PURPOSE (v) == DECL_NAME (t))
13057 return TREE_VALUE (v);
13058
13059 /* We didn't find the name. That should never happen; if
13060 name-lookup found it during preliminary parsing, we
13061 should find it again here during instantiation. */
13062 gcc_unreachable ();
13063 }
13064 return t;
13065
13066 case FIELD_DECL:
13067 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13068 {
13069 /* Check for a local specialization set up by
13070 tsubst_pack_expansion. */
13071 if (tree r = retrieve_local_specialization (t))
13072 {
13073 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
13074 r = ARGUMENT_PACK_SELECT_ARG (r);
13075 return r;
13076 }
13077
13078 /* When retrieving a capture pack from a generic lambda, remove the
13079 lambda call op's own template argument list from ARGS. Only the
13080 template arguments active for the closure type should be used to
13081 retrieve the pack specialization. */
13082 if (LAMBDA_FUNCTION_P (current_function_decl)
13083 && (template_class_depth (DECL_CONTEXT (t))
13084 != TMPL_ARGS_DEPTH (args)))
13085 args = strip_innermost_template_args (args, 1);
13086
13087 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
13088 tsubst_decl put in the hash table. */
13089 return retrieve_specialization (t, args, 0);
13090 }
13091
13092 if (DECL_CONTEXT (t))
13093 {
13094 tree ctx;
13095
13096 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
13097 /*entering_scope=*/1);
13098 if (ctx != DECL_CONTEXT (t))
13099 {
13100 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
13101 if (!r)
13102 {
13103 if (complain & tf_error)
13104 error ("using invalid field %qD", t);
13105 return error_mark_node;
13106 }
13107 return r;
13108 }
13109 }
13110
13111 return t;
13112
13113 case VAR_DECL:
13114 case FUNCTION_DECL:
13115 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
13116 r = tsubst (t, args, complain, in_decl);
13117 else if (local_variable_p (t))
13118 {
13119 r = retrieve_local_specialization (t);
13120 if (r == NULL_TREE)
13121 {
13122 /* First try name lookup to find the instantiation. */
13123 r = lookup_name (DECL_NAME (t));
13124 if (r)
13125 {
13126 /* Make sure that the one we found is the one we want. */
13127 tree ctx = tsubst (DECL_CONTEXT (t), args,
13128 complain, in_decl);
13129 if (ctx != DECL_CONTEXT (r))
13130 r = NULL_TREE;
13131 }
13132
13133 if (r)
13134 /* OK */;
13135 else
13136 {
13137 /* This can happen for a variable used in a
13138 late-specified return type of a local lambda, or for a
13139 local static or constant. Building a new VAR_DECL
13140 should be OK in all those cases. */
13141 r = tsubst_decl (t, args, complain);
13142 if (decl_maybe_constant_var_p (r))
13143 {
13144 /* We can't call cp_finish_decl, so handle the
13145 initializer by hand. */
13146 tree init = tsubst_init (DECL_INITIAL (t), r, args,
13147 complain, in_decl);
13148 if (!processing_template_decl)
13149 init = maybe_constant_init (init);
13150 if (processing_template_decl
13151 ? potential_constant_expression (init)
13152 : reduced_constant_expression_p (init))
13153 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
13154 = TREE_CONSTANT (r) = true;
13155 DECL_INITIAL (r) = init;
13156 }
13157 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
13158 || decl_constant_var_p (r)
13159 || errorcount || sorrycount);
13160 if (!processing_template_decl)
13161 {
13162 if (TREE_STATIC (r))
13163 rest_of_decl_compilation (r, toplevel_bindings_p (),
13164 at_eof);
13165 else
13166 r = process_outer_var_ref (r, complain);
13167 }
13168 }
13169 /* Remember this for subsequent uses. */
13170 if (local_specializations)
13171 register_local_specialization (r, t);
13172 }
13173 }
13174 else
13175 r = t;
13176 if (!mark_used (r, complain) && !(complain & tf_error))
13177 return error_mark_node;
13178 return r;
13179
13180 case NAMESPACE_DECL:
13181 return t;
13182
13183 case OVERLOAD:
13184 /* An OVERLOAD will always be a non-dependent overload set; an
13185 overload set from function scope will just be represented with an
13186 IDENTIFIER_NODE, and from class scope with a BASELINK. */
13187 gcc_assert (!uses_template_parms (t));
13188 return t;
13189
13190 case BASELINK:
13191 return tsubst_baselink (t, current_nonlambda_class_type (),
13192 args, complain, in_decl);
13193
13194 case TEMPLATE_DECL:
13195 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13196 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
13197 args, complain, in_decl);
13198 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
13199 return tsubst (t, args, complain, in_decl);
13200 else if (DECL_CLASS_SCOPE_P (t)
13201 && uses_template_parms (DECL_CONTEXT (t)))
13202 {
13203 /* Template template argument like the following example need
13204 special treatment:
13205
13206 template <template <class> class TT> struct C {};
13207 template <class T> struct D {
13208 template <class U> struct E {};
13209 C<E> c; // #1
13210 };
13211 D<int> d; // #2
13212
13213 We are processing the template argument `E' in #1 for
13214 the template instantiation #2. Originally, `E' is a
13215 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
13216 have to substitute this with one having context `D<int>'. */
13217
13218 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
13219 return lookup_field (context, DECL_NAME(t), 0, false);
13220 }
13221 else
13222 /* Ordinary template template argument. */
13223 return t;
13224
13225 case CAST_EXPR:
13226 case REINTERPRET_CAST_EXPR:
13227 case CONST_CAST_EXPR:
13228 case STATIC_CAST_EXPR:
13229 case DYNAMIC_CAST_EXPR:
13230 case IMPLICIT_CONV_EXPR:
13231 case CONVERT_EXPR:
13232 case NOP_EXPR:
13233 {
13234 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13235 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13236 return build1 (code, type, op0);
13237 }
13238
13239 case SIZEOF_EXPR:
13240 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13241 {
13242
13243 tree expanded, op = TREE_OPERAND (t, 0);
13244 int len = 0;
13245
13246 if (SIZEOF_EXPR_TYPE_P (t))
13247 op = TREE_TYPE (op);
13248
13249 ++cp_unevaluated_operand;
13250 ++c_inhibit_evaluation_warnings;
13251 /* We only want to compute the number of arguments. */
13252 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
13253 --cp_unevaluated_operand;
13254 --c_inhibit_evaluation_warnings;
13255
13256 if (TREE_CODE (expanded) == TREE_VEC)
13257 len = TREE_VEC_LENGTH (expanded);
13258
13259 if (expanded == error_mark_node)
13260 return error_mark_node;
13261 else if (PACK_EXPANSION_P (expanded)
13262 || (TREE_CODE (expanded) == TREE_VEC
13263 && len > 0
13264 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
13265 {
13266 if (TREE_CODE (expanded) == TREE_VEC)
13267 expanded = TREE_VEC_ELT (expanded, len - 1);
13268
13269 if (TYPE_P (expanded))
13270 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
13271 complain & tf_error);
13272 else
13273 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
13274 complain & tf_error);
13275 }
13276 else
13277 return build_int_cst (size_type_node, len);
13278 }
13279 if (SIZEOF_EXPR_TYPE_P (t))
13280 {
13281 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
13282 args, complain, in_decl);
13283 r = build1 (NOP_EXPR, r, error_mark_node);
13284 r = build1 (SIZEOF_EXPR,
13285 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
13286 SIZEOF_EXPR_TYPE_P (r) = 1;
13287 return r;
13288 }
13289 /* Fall through */
13290
13291 case INDIRECT_REF:
13292 case NEGATE_EXPR:
13293 case TRUTH_NOT_EXPR:
13294 case BIT_NOT_EXPR:
13295 case ADDR_EXPR:
13296 case UNARY_PLUS_EXPR: /* Unary + */
13297 case ALIGNOF_EXPR:
13298 case AT_ENCODE_EXPR:
13299 case ARROW_EXPR:
13300 case THROW_EXPR:
13301 case TYPEID_EXPR:
13302 case REALPART_EXPR:
13303 case IMAGPART_EXPR:
13304 case PAREN_EXPR:
13305 {
13306 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13307 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13308 return build1 (code, type, op0);
13309 }
13310
13311 case COMPONENT_REF:
13312 {
13313 tree object;
13314 tree name;
13315
13316 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13317 name = TREE_OPERAND (t, 1);
13318 if (TREE_CODE (name) == BIT_NOT_EXPR)
13319 {
13320 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13321 complain, in_decl);
13322 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13323 }
13324 else if (TREE_CODE (name) == SCOPE_REF
13325 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
13326 {
13327 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
13328 complain, in_decl);
13329 name = TREE_OPERAND (name, 1);
13330 name = tsubst_copy (TREE_OPERAND (name, 0), args,
13331 complain, in_decl);
13332 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
13333 name = build_qualified_name (/*type=*/NULL_TREE,
13334 base, name,
13335 /*template_p=*/false);
13336 }
13337 else if (BASELINK_P (name))
13338 name = tsubst_baselink (name,
13339 non_reference (TREE_TYPE (object)),
13340 args, complain,
13341 in_decl);
13342 else
13343 name = tsubst_copy (name, args, complain, in_decl);
13344 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
13345 }
13346
13347 case PLUS_EXPR:
13348 case MINUS_EXPR:
13349 case MULT_EXPR:
13350 case TRUNC_DIV_EXPR:
13351 case CEIL_DIV_EXPR:
13352 case FLOOR_DIV_EXPR:
13353 case ROUND_DIV_EXPR:
13354 case EXACT_DIV_EXPR:
13355 case BIT_AND_EXPR:
13356 case BIT_IOR_EXPR:
13357 case BIT_XOR_EXPR:
13358 case TRUNC_MOD_EXPR:
13359 case FLOOR_MOD_EXPR:
13360 case TRUTH_ANDIF_EXPR:
13361 case TRUTH_ORIF_EXPR:
13362 case TRUTH_AND_EXPR:
13363 case TRUTH_OR_EXPR:
13364 case RSHIFT_EXPR:
13365 case LSHIFT_EXPR:
13366 case RROTATE_EXPR:
13367 case LROTATE_EXPR:
13368 case EQ_EXPR:
13369 case NE_EXPR:
13370 case MAX_EXPR:
13371 case MIN_EXPR:
13372 case LE_EXPR:
13373 case GE_EXPR:
13374 case LT_EXPR:
13375 case GT_EXPR:
13376 case COMPOUND_EXPR:
13377 case DOTSTAR_EXPR:
13378 case MEMBER_REF:
13379 case PREDECREMENT_EXPR:
13380 case PREINCREMENT_EXPR:
13381 case POSTDECREMENT_EXPR:
13382 case POSTINCREMENT_EXPR:
13383 {
13384 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13385 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13386 return build_nt (code, op0, op1);
13387 }
13388
13389 case SCOPE_REF:
13390 {
13391 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13392 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13393 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
13394 QUALIFIED_NAME_IS_TEMPLATE (t));
13395 }
13396
13397 case ARRAY_REF:
13398 {
13399 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13400 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13401 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
13402 }
13403
13404 case CALL_EXPR:
13405 {
13406 int n = VL_EXP_OPERAND_LENGTH (t);
13407 tree result = build_vl_exp (CALL_EXPR, n);
13408 int i;
13409 for (i = 0; i < n; i++)
13410 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
13411 complain, in_decl);
13412 return result;
13413 }
13414
13415 case COND_EXPR:
13416 case MODOP_EXPR:
13417 case PSEUDO_DTOR_EXPR:
13418 case VEC_PERM_EXPR:
13419 {
13420 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13421 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13422 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13423 r = build_nt (code, op0, op1, op2);
13424 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13425 return r;
13426 }
13427
13428 case NEW_EXPR:
13429 {
13430 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13431 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13432 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
13433 r = build_nt (code, op0, op1, op2);
13434 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
13435 return r;
13436 }
13437
13438 case DELETE_EXPR:
13439 {
13440 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13441 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13442 r = build_nt (code, op0, op1);
13443 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
13444 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
13445 return r;
13446 }
13447
13448 case TEMPLATE_ID_EXPR:
13449 {
13450 /* Substituted template arguments */
13451 tree fn = TREE_OPERAND (t, 0);
13452 tree targs = TREE_OPERAND (t, 1);
13453
13454 fn = tsubst_copy (fn, args, complain, in_decl);
13455 if (targs)
13456 targs = tsubst_template_args (targs, args, complain, in_decl);
13457
13458 return lookup_template_function (fn, targs);
13459 }
13460
13461 case TREE_LIST:
13462 {
13463 tree purpose, value, chain;
13464
13465 if (t == void_list_node)
13466 return t;
13467
13468 purpose = TREE_PURPOSE (t);
13469 if (purpose)
13470 purpose = tsubst_copy (purpose, args, complain, in_decl);
13471 value = TREE_VALUE (t);
13472 if (value)
13473 value = tsubst_copy (value, args, complain, in_decl);
13474 chain = TREE_CHAIN (t);
13475 if (chain && chain != void_type_node)
13476 chain = tsubst_copy (chain, args, complain, in_decl);
13477 if (purpose == TREE_PURPOSE (t)
13478 && value == TREE_VALUE (t)
13479 && chain == TREE_CHAIN (t))
13480 return t;
13481 return tree_cons (purpose, value, chain);
13482 }
13483
13484 case RECORD_TYPE:
13485 case UNION_TYPE:
13486 case ENUMERAL_TYPE:
13487 case INTEGER_TYPE:
13488 case TEMPLATE_TYPE_PARM:
13489 case TEMPLATE_TEMPLATE_PARM:
13490 case BOUND_TEMPLATE_TEMPLATE_PARM:
13491 case TEMPLATE_PARM_INDEX:
13492 case POINTER_TYPE:
13493 case REFERENCE_TYPE:
13494 case OFFSET_TYPE:
13495 case FUNCTION_TYPE:
13496 case METHOD_TYPE:
13497 case ARRAY_TYPE:
13498 case TYPENAME_TYPE:
13499 case UNBOUND_CLASS_TEMPLATE:
13500 case TYPEOF_TYPE:
13501 case DECLTYPE_TYPE:
13502 case TYPE_DECL:
13503 return tsubst (t, args, complain, in_decl);
13504
13505 case USING_DECL:
13506 t = DECL_NAME (t);
13507 /* Fall through. */
13508 case IDENTIFIER_NODE:
13509 if (IDENTIFIER_TYPENAME_P (t))
13510 {
13511 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13512 return mangle_conv_op_name_for_type (new_type);
13513 }
13514 else
13515 return t;
13516
13517 case CONSTRUCTOR:
13518 /* This is handled by tsubst_copy_and_build. */
13519 gcc_unreachable ();
13520
13521 case VA_ARG_EXPR:
13522 {
13523 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13524 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13525 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
13526 }
13527
13528 case CLEANUP_POINT_EXPR:
13529 /* We shouldn't have built any of these during initial template
13530 generation. Instead, they should be built during instantiation
13531 in response to the saved STMT_IS_FULL_EXPR_P setting. */
13532 gcc_unreachable ();
13533
13534 case OFFSET_REF:
13535 {
13536 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13537 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
13538 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
13539 r = build2 (code, type, op0, op1);
13540 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
13541 if (!mark_used (TREE_OPERAND (r, 1), complain)
13542 && !(complain & tf_error))
13543 return error_mark_node;
13544 return r;
13545 }
13546
13547 case EXPR_PACK_EXPANSION:
13548 error ("invalid use of pack expansion expression");
13549 return error_mark_node;
13550
13551 case NONTYPE_ARGUMENT_PACK:
13552 error ("use %<...%> to expand argument pack");
13553 return error_mark_node;
13554
13555 case VOID_CST:
13556 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
13557 return t;
13558
13559 case INTEGER_CST:
13560 case REAL_CST:
13561 case STRING_CST:
13562 case COMPLEX_CST:
13563 {
13564 /* Instantiate any typedefs in the type. */
13565 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13566 r = fold_convert (type, t);
13567 gcc_assert (TREE_CODE (r) == code);
13568 return r;
13569 }
13570
13571 case PTRMEM_CST:
13572 /* These can sometimes show up in a partial instantiation, but never
13573 involve template parms. */
13574 gcc_assert (!uses_template_parms (t));
13575 return t;
13576
13577 default:
13578 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
13579 gcc_checking_assert (false);
13580 return t;
13581 }
13582 }
13583
13584 /* Helper function for tsubst_omp_clauses, used for instantiation of
13585 OMP_CLAUSE_DECL of clauses that handles also OpenMP array sections
13586 represented with TREE_LIST. */
13587
13588 static tree
13589 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
13590 tree in_decl)
13591 {
13592 if (TREE_CODE (decl) == TREE_LIST)
13593 {
13594 tree low_bound
13595 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
13596 /*integral_constant_expression_p=*/false);
13597 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
13598 /*integral_constant_expression_p=*/false);
13599 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
13600 in_decl);
13601 if (TREE_PURPOSE (decl) == low_bound
13602 && TREE_VALUE (decl) == length
13603 && TREE_CHAIN (decl) == chain)
13604 return decl;
13605 return tree_cons (low_bound, length, chain);
13606 }
13607 return tsubst_copy (decl, args, complain, in_decl);
13608 }
13609
13610 /* Like tsubst_copy, but specifically for OpenMP clauses. */
13611
13612 static tree
13613 tsubst_omp_clauses (tree clauses, bool declare_simd,
13614 tree args, tsubst_flags_t complain, tree in_decl)
13615 {
13616 tree new_clauses = NULL, nc, oc;
13617
13618 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
13619 {
13620 nc = copy_node (oc);
13621 OMP_CLAUSE_CHAIN (nc) = new_clauses;
13622 new_clauses = nc;
13623
13624 switch (OMP_CLAUSE_CODE (nc))
13625 {
13626 case OMP_CLAUSE_LASTPRIVATE:
13627 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
13628 {
13629 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
13630 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
13631 in_decl, /*integral_constant_expression_p=*/false);
13632 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
13633 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
13634 }
13635 /* FALLTHRU */
13636 case OMP_CLAUSE_PRIVATE:
13637 case OMP_CLAUSE_SHARED:
13638 case OMP_CLAUSE_FIRSTPRIVATE:
13639 case OMP_CLAUSE_COPYIN:
13640 case OMP_CLAUSE_COPYPRIVATE:
13641 case OMP_CLAUSE_UNIFORM:
13642 OMP_CLAUSE_DECL (nc) = tsubst_copy (OMP_CLAUSE_DECL (oc), args,
13643 complain, in_decl);
13644 break;
13645 case OMP_CLAUSE_DEPEND:
13646 case OMP_CLAUSE_FROM:
13647 case OMP_CLAUSE_TO:
13648 case OMP_CLAUSE_MAP:
13649 OMP_CLAUSE_DECL (nc)
13650 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
13651 in_decl);
13652 break;
13653 case OMP_CLAUSE_IF:
13654 case OMP_CLAUSE_NUM_THREADS:
13655 case OMP_CLAUSE_SCHEDULE:
13656 case OMP_CLAUSE_COLLAPSE:
13657 case OMP_CLAUSE_FINAL:
13658 case OMP_CLAUSE_DEVICE:
13659 case OMP_CLAUSE_DIST_SCHEDULE:
13660 case OMP_CLAUSE_NUM_TEAMS:
13661 case OMP_CLAUSE_THREAD_LIMIT:
13662 case OMP_CLAUSE_SAFELEN:
13663 case OMP_CLAUSE_SIMDLEN:
13664 OMP_CLAUSE_OPERAND (nc, 0)
13665 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
13666 in_decl, /*integral_constant_expression_p=*/false);
13667 break;
13668 case OMP_CLAUSE_REDUCTION:
13669 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
13670 {
13671 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
13672 if (TREE_CODE (placeholder) == SCOPE_REF)
13673 {
13674 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
13675 complain, in_decl);
13676 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
13677 = build_qualified_name (NULL_TREE, scope,
13678 TREE_OPERAND (placeholder, 1),
13679 false);
13680 }
13681 else
13682 gcc_assert (identifier_p (placeholder));
13683 }
13684 OMP_CLAUSE_DECL (nc) = tsubst_copy (OMP_CLAUSE_DECL (oc), args,
13685 complain, in_decl);
13686 break;
13687 case OMP_CLAUSE_LINEAR:
13688 case OMP_CLAUSE_ALIGNED:
13689 OMP_CLAUSE_DECL (nc) = tsubst_copy (OMP_CLAUSE_DECL (oc), args,
13690 complain, in_decl);
13691 OMP_CLAUSE_OPERAND (nc, 1)
13692 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
13693 in_decl, /*integral_constant_expression_p=*/false);
13694 break;
13695 case OMP_CLAUSE_NOWAIT:
13696 case OMP_CLAUSE_ORDERED:
13697 case OMP_CLAUSE_DEFAULT:
13698 case OMP_CLAUSE_UNTIED:
13699 case OMP_CLAUSE_MERGEABLE:
13700 case OMP_CLAUSE_INBRANCH:
13701 case OMP_CLAUSE_NOTINBRANCH:
13702 case OMP_CLAUSE_PROC_BIND:
13703 case OMP_CLAUSE_FOR:
13704 case OMP_CLAUSE_PARALLEL:
13705 case OMP_CLAUSE_SECTIONS:
13706 case OMP_CLAUSE_TASKGROUP:
13707 break;
13708 default:
13709 gcc_unreachable ();
13710 }
13711 }
13712
13713 new_clauses = nreverse (new_clauses);
13714 if (!declare_simd)
13715 new_clauses = finish_omp_clauses (new_clauses);
13716 return new_clauses;
13717 }
13718
13719 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
13720
13721 static tree
13722 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
13723 tree in_decl)
13724 {
13725 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
13726
13727 tree purpose, value, chain;
13728
13729 if (t == NULL)
13730 return t;
13731
13732 if (TREE_CODE (t) != TREE_LIST)
13733 return tsubst_copy_and_build (t, args, complain, in_decl,
13734 /*function_p=*/false,
13735 /*integral_constant_expression_p=*/false);
13736
13737 if (t == void_list_node)
13738 return t;
13739
13740 purpose = TREE_PURPOSE (t);
13741 if (purpose)
13742 purpose = RECUR (purpose);
13743 value = TREE_VALUE (t);
13744 if (value)
13745 {
13746 if (TREE_CODE (value) != LABEL_DECL)
13747 value = RECUR (value);
13748 else
13749 {
13750 value = lookup_label (DECL_NAME (value));
13751 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13752 TREE_USED (value) = 1;
13753 }
13754 }
13755 chain = TREE_CHAIN (t);
13756 if (chain && chain != void_type_node)
13757 chain = RECUR (chain);
13758 return tree_cons (purpose, value, chain);
13759 #undef RECUR
13760 }
13761
13762 /* Substitute one OMP_FOR iterator. */
13763
13764 static void
13765 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13766 tree condv, tree incrv, tree *clauses,
13767 tree args, tsubst_flags_t complain, tree in_decl,
13768 bool integral_constant_expression_p)
13769 {
13770 #define RECUR(NODE) \
13771 tsubst_expr ((NODE), args, complain, in_decl, \
13772 integral_constant_expression_p)
13773 tree decl, init, cond, incr;
13774
13775 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13776 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13777 decl = TREE_OPERAND (init, 0);
13778 init = TREE_OPERAND (init, 1);
13779 tree decl_expr = NULL_TREE;
13780 if (init && TREE_CODE (init) == DECL_EXPR)
13781 {
13782 /* We need to jump through some hoops to handle declarations in the
13783 for-init-statement, since we might need to handle auto deduction,
13784 but we need to keep control of initialization. */
13785 decl_expr = init;
13786 init = DECL_INITIAL (DECL_EXPR_DECL (init));
13787 decl = tsubst_decl (decl, args, complain);
13788 }
13789 else
13790 decl = RECUR (decl);
13791 init = RECUR (init);
13792
13793 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13794 if (auto_node && init)
13795 TREE_TYPE (decl)
13796 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
13797
13798 gcc_assert (!type_dependent_expression_p (decl));
13799
13800 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13801 {
13802 if (decl_expr)
13803 {
13804 /* Declare the variable, but don't let that initialize it. */
13805 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
13806 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
13807 RECUR (decl_expr);
13808 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
13809 }
13810
13811 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13812 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13813 if (TREE_CODE (incr) == MODIFY_EXPR)
13814 {
13815 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13816 tree rhs = RECUR (TREE_OPERAND (incr, 1));
13817 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
13818 NOP_EXPR, rhs, complain);
13819 }
13820 else
13821 incr = RECUR (incr);
13822 TREE_VEC_ELT (declv, i) = decl;
13823 TREE_VEC_ELT (initv, i) = init;
13824 TREE_VEC_ELT (condv, i) = cond;
13825 TREE_VEC_ELT (incrv, i) = incr;
13826 return;
13827 }
13828
13829 if (decl_expr)
13830 {
13831 /* Declare and initialize the variable. */
13832 RECUR (decl_expr);
13833 init = NULL_TREE;
13834 }
13835 else if (init)
13836 {
13837 tree c;
13838 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13839 {
13840 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13841 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13842 && OMP_CLAUSE_DECL (c) == decl)
13843 break;
13844 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13845 && OMP_CLAUSE_DECL (c) == decl)
13846 error ("iteration variable %qD should not be firstprivate", decl);
13847 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13848 && OMP_CLAUSE_DECL (c) == decl)
13849 error ("iteration variable %qD should not be reduction", decl);
13850 }
13851 if (c == NULL)
13852 {
13853 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13854 OMP_CLAUSE_DECL (c) = decl;
13855 c = finish_omp_clauses (c);
13856 if (c)
13857 {
13858 OMP_CLAUSE_CHAIN (c) = *clauses;
13859 *clauses = c;
13860 }
13861 }
13862 }
13863 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13864 if (COMPARISON_CLASS_P (cond))
13865 {
13866 tree op0 = RECUR (TREE_OPERAND (cond, 0));
13867 tree op1 = RECUR (TREE_OPERAND (cond, 1));
13868 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
13869 }
13870 else
13871 cond = RECUR (cond);
13872 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13873 switch (TREE_CODE (incr))
13874 {
13875 case PREINCREMENT_EXPR:
13876 case PREDECREMENT_EXPR:
13877 case POSTINCREMENT_EXPR:
13878 case POSTDECREMENT_EXPR:
13879 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13880 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13881 break;
13882 case MODIFY_EXPR:
13883 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13884 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13885 {
13886 tree rhs = TREE_OPERAND (incr, 1);
13887 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13888 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13889 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13890 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13891 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13892 rhs0, rhs1));
13893 }
13894 else
13895 incr = RECUR (incr);
13896 break;
13897 case MODOP_EXPR:
13898 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13899 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13900 {
13901 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13902 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13903 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13904 TREE_TYPE (decl), lhs,
13905 RECUR (TREE_OPERAND (incr, 2))));
13906 }
13907 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13908 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13909 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13910 {
13911 tree rhs = TREE_OPERAND (incr, 2);
13912 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13913 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
13914 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
13915 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13916 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13917 rhs0, rhs1));
13918 }
13919 else
13920 incr = RECUR (incr);
13921 break;
13922 default:
13923 incr = RECUR (incr);
13924 break;
13925 }
13926
13927 TREE_VEC_ELT (declv, i) = decl;
13928 TREE_VEC_ELT (initv, i) = init;
13929 TREE_VEC_ELT (condv, i) = cond;
13930 TREE_VEC_ELT (incrv, i) = incr;
13931 #undef RECUR
13932 }
13933
13934 /* Like tsubst_copy for expressions, etc. but also does semantic
13935 processing. */
13936
13937 static tree
13938 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13939 bool integral_constant_expression_p)
13940 {
13941 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13942 #define RECUR(NODE) \
13943 tsubst_expr ((NODE), args, complain, in_decl, \
13944 integral_constant_expression_p)
13945
13946 tree stmt, tmp;
13947 tree r;
13948 location_t loc;
13949
13950 if (t == NULL_TREE || t == error_mark_node)
13951 return t;
13952
13953 loc = input_location;
13954 if (EXPR_HAS_LOCATION (t))
13955 input_location = EXPR_LOCATION (t);
13956 if (STATEMENT_CODE_P (TREE_CODE (t)))
13957 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13958
13959 switch (TREE_CODE (t))
13960 {
13961 case STATEMENT_LIST:
13962 {
13963 tree_stmt_iterator i;
13964 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13965 RECUR (tsi_stmt (i));
13966 break;
13967 }
13968
13969 case CTOR_INITIALIZER:
13970 finish_mem_initializers (tsubst_initializer_list
13971 (TREE_OPERAND (t, 0), args));
13972 break;
13973
13974 case RETURN_EXPR:
13975 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13976 break;
13977
13978 case EXPR_STMT:
13979 tmp = RECUR (EXPR_STMT_EXPR (t));
13980 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13981 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13982 else
13983 finish_expr_stmt (tmp);
13984 break;
13985
13986 case USING_STMT:
13987 do_using_directive (USING_STMT_NAMESPACE (t));
13988 break;
13989
13990 case DECL_EXPR:
13991 {
13992 tree decl, pattern_decl;
13993 tree init;
13994
13995 pattern_decl = decl = DECL_EXPR_DECL (t);
13996 if (TREE_CODE (decl) == LABEL_DECL)
13997 finish_label_decl (DECL_NAME (decl));
13998 else if (TREE_CODE (decl) == USING_DECL)
13999 {
14000 tree scope = USING_DECL_SCOPE (decl);
14001 tree name = DECL_NAME (decl);
14002 tree decl;
14003
14004 scope = tsubst (scope, args, complain, in_decl);
14005 decl = lookup_qualified_name (scope, name,
14006 /*is_type_p=*/false,
14007 /*complain=*/false);
14008 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
14009 qualified_name_lookup_error (scope, name, decl, input_location);
14010 else
14011 do_local_using_decl (decl, scope, name);
14012 }
14013 else if (DECL_PACK_P (decl))
14014 {
14015 /* Don't build up decls for a variadic capture proxy, we'll
14016 instantiate the elements directly as needed. */
14017 break;
14018 }
14019 else
14020 {
14021 init = DECL_INITIAL (decl);
14022 decl = tsubst (decl, args, complain, in_decl);
14023 if (decl != error_mark_node)
14024 {
14025 /* By marking the declaration as instantiated, we avoid
14026 trying to instantiate it. Since instantiate_decl can't
14027 handle local variables, and since we've already done
14028 all that needs to be done, that's the right thing to
14029 do. */
14030 if (VAR_P (decl))
14031 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14032 if (VAR_P (decl)
14033 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
14034 /* Anonymous aggregates are a special case. */
14035 finish_anon_union (decl);
14036 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
14037 {
14038 DECL_CONTEXT (decl) = current_function_decl;
14039 if (DECL_NAME (decl) == this_identifier)
14040 {
14041 tree lam = DECL_CONTEXT (current_function_decl);
14042 lam = CLASSTYPE_LAMBDA_EXPR (lam);
14043 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
14044 }
14045 insert_capture_proxy (decl);
14046 }
14047 else if (DECL_IMPLICIT_TYPEDEF_P (t))
14048 /* We already did a pushtag. */;
14049 else if (TREE_CODE (decl) == FUNCTION_DECL
14050 && DECL_OMP_DECLARE_REDUCTION_P (decl)
14051 && DECL_FUNCTION_SCOPE_P (pattern_decl))
14052 {
14053 DECL_CONTEXT (decl) = NULL_TREE;
14054 pushdecl (decl);
14055 DECL_CONTEXT (decl) = current_function_decl;
14056 cp_check_omp_declare_reduction (decl);
14057 }
14058 else
14059 {
14060 int const_init = false;
14061 maybe_push_decl (decl);
14062 if (VAR_P (decl)
14063 && DECL_PRETTY_FUNCTION_P (decl))
14064 {
14065 /* For __PRETTY_FUNCTION__ we have to adjust the
14066 initializer. */
14067 const char *const name
14068 = cxx_printable_name (current_function_decl, 2);
14069 init = cp_fname_init (name, &TREE_TYPE (decl));
14070 }
14071 else
14072 init = tsubst_init (init, decl, args, complain, in_decl);
14073
14074 if (VAR_P (decl))
14075 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
14076 (pattern_decl));
14077 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
14078 }
14079 }
14080 }
14081
14082 break;
14083 }
14084
14085 case FOR_STMT:
14086 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
14087 RECUR (FOR_INIT_STMT (t));
14088 finish_for_init_stmt (stmt);
14089 tmp = RECUR (FOR_COND (t));
14090 finish_for_cond (tmp, stmt, false);
14091 tmp = RECUR (FOR_EXPR (t));
14092 finish_for_expr (tmp, stmt);
14093 RECUR (FOR_BODY (t));
14094 finish_for_stmt (stmt);
14095 break;
14096
14097 case RANGE_FOR_STMT:
14098 {
14099 tree decl, expr;
14100 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
14101 decl = RANGE_FOR_DECL (t);
14102 decl = tsubst (decl, args, complain, in_decl);
14103 maybe_push_decl (decl);
14104 expr = RECUR (RANGE_FOR_EXPR (t));
14105 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
14106 RECUR (RANGE_FOR_BODY (t));
14107 finish_for_stmt (stmt);
14108 }
14109 break;
14110
14111 case WHILE_STMT:
14112 stmt = begin_while_stmt ();
14113 tmp = RECUR (WHILE_COND (t));
14114 finish_while_stmt_cond (tmp, stmt, false);
14115 RECUR (WHILE_BODY (t));
14116 finish_while_stmt (stmt);
14117 break;
14118
14119 case DO_STMT:
14120 stmt = begin_do_stmt ();
14121 RECUR (DO_BODY (t));
14122 finish_do_body (stmt);
14123 tmp = RECUR (DO_COND (t));
14124 finish_do_stmt (tmp, stmt, false);
14125 break;
14126
14127 case IF_STMT:
14128 stmt = begin_if_stmt ();
14129 tmp = RECUR (IF_COND (t));
14130 finish_if_stmt_cond (tmp, stmt);
14131 RECUR (THEN_CLAUSE (t));
14132 finish_then_clause (stmt);
14133
14134 if (ELSE_CLAUSE (t))
14135 {
14136 begin_else_clause (stmt);
14137 RECUR (ELSE_CLAUSE (t));
14138 finish_else_clause (stmt);
14139 }
14140
14141 finish_if_stmt (stmt);
14142 break;
14143
14144 case BIND_EXPR:
14145 if (BIND_EXPR_BODY_BLOCK (t))
14146 stmt = begin_function_body ();
14147 else
14148 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
14149 ? BCS_TRY_BLOCK : 0);
14150
14151 RECUR (BIND_EXPR_BODY (t));
14152
14153 if (BIND_EXPR_BODY_BLOCK (t))
14154 finish_function_body (stmt);
14155 else
14156 finish_compound_stmt (stmt);
14157 break;
14158
14159 case BREAK_STMT:
14160 finish_break_stmt ();
14161 break;
14162
14163 case CONTINUE_STMT:
14164 finish_continue_stmt ();
14165 break;
14166
14167 case SWITCH_STMT:
14168 stmt = begin_switch_stmt ();
14169 tmp = RECUR (SWITCH_STMT_COND (t));
14170 finish_switch_cond (tmp, stmt);
14171 RECUR (SWITCH_STMT_BODY (t));
14172 finish_switch_stmt (stmt);
14173 break;
14174
14175 case CASE_LABEL_EXPR:
14176 {
14177 tree low = RECUR (CASE_LOW (t));
14178 tree high = RECUR (CASE_HIGH (t));
14179 finish_case_label (EXPR_LOCATION (t), low, high);
14180 }
14181 break;
14182
14183 case LABEL_EXPR:
14184 {
14185 tree decl = LABEL_EXPR_LABEL (t);
14186 tree label;
14187
14188 label = finish_label_stmt (DECL_NAME (decl));
14189 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
14190 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
14191 }
14192 break;
14193
14194 case GOTO_EXPR:
14195 tmp = GOTO_DESTINATION (t);
14196 if (TREE_CODE (tmp) != LABEL_DECL)
14197 /* Computed goto's must be tsubst'd into. On the other hand,
14198 non-computed gotos must not be; the identifier in question
14199 will have no binding. */
14200 tmp = RECUR (tmp);
14201 else
14202 tmp = DECL_NAME (tmp);
14203 finish_goto_stmt (tmp);
14204 break;
14205
14206 case ASM_EXPR:
14207 {
14208 tree string = RECUR (ASM_STRING (t));
14209 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
14210 complain, in_decl);
14211 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
14212 complain, in_decl);
14213 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
14214 complain, in_decl);
14215 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
14216 complain, in_decl);
14217 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
14218 clobbers, labels);
14219 tree asm_expr = tmp;
14220 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
14221 asm_expr = TREE_OPERAND (asm_expr, 0);
14222 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
14223 }
14224 break;
14225
14226 case TRY_BLOCK:
14227 if (CLEANUP_P (t))
14228 {
14229 stmt = begin_try_block ();
14230 RECUR (TRY_STMTS (t));
14231 finish_cleanup_try_block (stmt);
14232 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
14233 }
14234 else
14235 {
14236 tree compound_stmt = NULL_TREE;
14237
14238 if (FN_TRY_BLOCK_P (t))
14239 stmt = begin_function_try_block (&compound_stmt);
14240 else
14241 stmt = begin_try_block ();
14242
14243 RECUR (TRY_STMTS (t));
14244
14245 if (FN_TRY_BLOCK_P (t))
14246 finish_function_try_block (stmt);
14247 else
14248 finish_try_block (stmt);
14249
14250 RECUR (TRY_HANDLERS (t));
14251 if (FN_TRY_BLOCK_P (t))
14252 finish_function_handler_sequence (stmt, compound_stmt);
14253 else
14254 finish_handler_sequence (stmt);
14255 }
14256 break;
14257
14258 case HANDLER:
14259 {
14260 tree decl = HANDLER_PARMS (t);
14261
14262 if (decl)
14263 {
14264 decl = tsubst (decl, args, complain, in_decl);
14265 /* Prevent instantiate_decl from trying to instantiate
14266 this variable. We've already done all that needs to be
14267 done. */
14268 if (decl != error_mark_node)
14269 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
14270 }
14271 stmt = begin_handler ();
14272 finish_handler_parms (decl, stmt);
14273 RECUR (HANDLER_BODY (t));
14274 finish_handler (stmt);
14275 }
14276 break;
14277
14278 case TAG_DEFN:
14279 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
14280 if (CLASS_TYPE_P (tmp))
14281 {
14282 /* Local classes are not independent templates; they are
14283 instantiated along with their containing function. And this
14284 way we don't have to deal with pushing out of one local class
14285 to instantiate a member of another local class. */
14286 tree fn;
14287 /* Closures are handled by the LAMBDA_EXPR. */
14288 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
14289 complete_type (tmp);
14290 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
14291 if (!DECL_ARTIFICIAL (fn))
14292 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
14293 }
14294 break;
14295
14296 case STATIC_ASSERT:
14297 {
14298 tree condition;
14299
14300 ++c_inhibit_evaluation_warnings;
14301 condition =
14302 tsubst_expr (STATIC_ASSERT_CONDITION (t),
14303 args,
14304 complain, in_decl,
14305 /*integral_constant_expression_p=*/true);
14306 --c_inhibit_evaluation_warnings;
14307
14308 finish_static_assert (condition,
14309 STATIC_ASSERT_MESSAGE (t),
14310 STATIC_ASSERT_SOURCE_LOCATION (t),
14311 /*member_p=*/false);
14312 }
14313 break;
14314
14315 case OMP_PARALLEL:
14316 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
14317 args, complain, in_decl);
14318 stmt = begin_omp_parallel ();
14319 RECUR (OMP_PARALLEL_BODY (t));
14320 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
14321 = OMP_PARALLEL_COMBINED (t);
14322 break;
14323
14324 case OMP_TASK:
14325 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
14326 args, complain, in_decl);
14327 stmt = begin_omp_task ();
14328 RECUR (OMP_TASK_BODY (t));
14329 finish_omp_task (tmp, stmt);
14330 break;
14331
14332 case OMP_FOR:
14333 case OMP_SIMD:
14334 case CILK_SIMD:
14335 case CILK_FOR:
14336 case OMP_DISTRIBUTE:
14337 {
14338 tree clauses, body, pre_body;
14339 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
14340 tree incrv = NULL_TREE;
14341 int i;
14342
14343 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
14344 args, complain, in_decl);
14345 if (OMP_FOR_INIT (t) != NULL_TREE)
14346 {
14347 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14348 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14349 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14350 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
14351 }
14352
14353 stmt = begin_omp_structured_block ();
14354
14355 pre_body = push_stmt_list ();
14356 RECUR (OMP_FOR_PRE_BODY (t));
14357 pre_body = pop_stmt_list (pre_body);
14358
14359 if (OMP_FOR_INIT (t) != NULL_TREE)
14360 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
14361 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
14362 &clauses, args, complain, in_decl,
14363 integral_constant_expression_p);
14364
14365 body = push_stmt_list ();
14366 RECUR (OMP_FOR_BODY (t));
14367 body = pop_stmt_list (body);
14368
14369 if (OMP_FOR_INIT (t) != NULL_TREE)
14370 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
14371 condv, incrv, body, pre_body, clauses);
14372 else
14373 {
14374 t = make_node (TREE_CODE (t));
14375 TREE_TYPE (t) = void_type_node;
14376 OMP_FOR_BODY (t) = body;
14377 OMP_FOR_PRE_BODY (t) = pre_body;
14378 OMP_FOR_CLAUSES (t) = clauses;
14379 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
14380 add_stmt (t);
14381 }
14382
14383 add_stmt (finish_omp_structured_block (stmt));
14384 }
14385 break;
14386
14387 case OMP_SECTIONS:
14388 case OMP_SINGLE:
14389 case OMP_TEAMS:
14390 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14391 args, complain, in_decl);
14392 stmt = push_stmt_list ();
14393 RECUR (OMP_BODY (t));
14394 stmt = pop_stmt_list (stmt);
14395
14396 t = copy_node (t);
14397 OMP_BODY (t) = stmt;
14398 OMP_CLAUSES (t) = tmp;
14399 add_stmt (t);
14400 break;
14401
14402 case OMP_TARGET_DATA:
14403 case OMP_TARGET:
14404 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
14405 args, complain, in_decl);
14406 keep_next_level (true);
14407 stmt = begin_omp_structured_block ();
14408
14409 RECUR (OMP_BODY (t));
14410 stmt = finish_omp_structured_block (stmt);
14411
14412 t = copy_node (t);
14413 OMP_BODY (t) = stmt;
14414 OMP_CLAUSES (t) = tmp;
14415 add_stmt (t);
14416 break;
14417
14418 case OMP_TARGET_UPDATE:
14419 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
14420 args, complain, in_decl);
14421 t = copy_node (t);
14422 OMP_TARGET_UPDATE_CLAUSES (t) = tmp;
14423 add_stmt (t);
14424 break;
14425
14426 case OMP_SECTION:
14427 case OMP_CRITICAL:
14428 case OMP_MASTER:
14429 case OMP_TASKGROUP:
14430 case OMP_ORDERED:
14431 stmt = push_stmt_list ();
14432 RECUR (OMP_BODY (t));
14433 stmt = pop_stmt_list (stmt);
14434
14435 t = copy_node (t);
14436 OMP_BODY (t) = stmt;
14437 add_stmt (t);
14438 break;
14439
14440 case OMP_ATOMIC:
14441 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
14442 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
14443 {
14444 tree op1 = TREE_OPERAND (t, 1);
14445 tree rhs1 = NULL_TREE;
14446 tree lhs, rhs;
14447 if (TREE_CODE (op1) == COMPOUND_EXPR)
14448 {
14449 rhs1 = RECUR (TREE_OPERAND (op1, 0));
14450 op1 = TREE_OPERAND (op1, 1);
14451 }
14452 lhs = RECUR (TREE_OPERAND (op1, 0));
14453 rhs = RECUR (TREE_OPERAND (op1, 1));
14454 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
14455 NULL_TREE, NULL_TREE, rhs1,
14456 OMP_ATOMIC_SEQ_CST (t));
14457 }
14458 else
14459 {
14460 tree op1 = TREE_OPERAND (t, 1);
14461 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
14462 tree rhs1 = NULL_TREE;
14463 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
14464 enum tree_code opcode = NOP_EXPR;
14465 if (code == OMP_ATOMIC_READ)
14466 {
14467 v = RECUR (TREE_OPERAND (op1, 0));
14468 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14469 }
14470 else if (code == OMP_ATOMIC_CAPTURE_OLD
14471 || code == OMP_ATOMIC_CAPTURE_NEW)
14472 {
14473 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
14474 v = RECUR (TREE_OPERAND (op1, 0));
14475 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
14476 if (TREE_CODE (op11) == COMPOUND_EXPR)
14477 {
14478 rhs1 = RECUR (TREE_OPERAND (op11, 0));
14479 op11 = TREE_OPERAND (op11, 1);
14480 }
14481 lhs = RECUR (TREE_OPERAND (op11, 0));
14482 rhs = RECUR (TREE_OPERAND (op11, 1));
14483 opcode = TREE_CODE (op11);
14484 if (opcode == MODIFY_EXPR)
14485 opcode = NOP_EXPR;
14486 }
14487 else
14488 {
14489 code = OMP_ATOMIC;
14490 lhs = RECUR (TREE_OPERAND (op1, 0));
14491 rhs = RECUR (TREE_OPERAND (op1, 1));
14492 }
14493 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
14494 OMP_ATOMIC_SEQ_CST (t));
14495 }
14496 break;
14497
14498 case TRANSACTION_EXPR:
14499 {
14500 int flags = 0;
14501 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
14502 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
14503
14504 if (TRANSACTION_EXPR_IS_STMT (t))
14505 {
14506 tree body = TRANSACTION_EXPR_BODY (t);
14507 tree noex = NULL_TREE;
14508 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
14509 {
14510 noex = MUST_NOT_THROW_COND (body);
14511 if (noex == NULL_TREE)
14512 noex = boolean_true_node;
14513 body = TREE_OPERAND (body, 0);
14514 }
14515 stmt = begin_transaction_stmt (input_location, NULL, flags);
14516 RECUR (body);
14517 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
14518 }
14519 else
14520 {
14521 stmt = build_transaction_expr (EXPR_LOCATION (t),
14522 RECUR (TRANSACTION_EXPR_BODY (t)),
14523 flags, NULL_TREE);
14524 RETURN (stmt);
14525 }
14526 }
14527 break;
14528
14529 case MUST_NOT_THROW_EXPR:
14530 {
14531 tree op0 = RECUR (TREE_OPERAND (t, 0));
14532 tree cond = RECUR (MUST_NOT_THROW_COND (t));
14533 RETURN (build_must_not_throw_expr (op0, cond));
14534 }
14535
14536 case EXPR_PACK_EXPANSION:
14537 error ("invalid use of pack expansion expression");
14538 RETURN (error_mark_node);
14539
14540 case NONTYPE_ARGUMENT_PACK:
14541 error ("use %<...%> to expand argument pack");
14542 RETURN (error_mark_node);
14543
14544 case CILK_SPAWN_STMT:
14545 cfun->calls_cilk_spawn = 1;
14546 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
14547
14548 case CILK_SYNC_STMT:
14549 RETURN (build_cilk_sync ());
14550
14551 case COMPOUND_EXPR:
14552 tmp = RECUR (TREE_OPERAND (t, 0));
14553 if (tmp == NULL_TREE)
14554 /* If the first operand was a statement, we're done with it. */
14555 RETURN (RECUR (TREE_OPERAND (t, 1)));
14556 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
14557 RECUR (TREE_OPERAND (t, 1)),
14558 complain));
14559
14560 case ANNOTATE_EXPR:
14561 tmp = RECUR (TREE_OPERAND (t, 0));
14562 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
14563 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
14564
14565 default:
14566 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
14567
14568 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
14569 /*function_p=*/false,
14570 integral_constant_expression_p));
14571 }
14572
14573 RETURN (NULL_TREE);
14574 out:
14575 input_location = loc;
14576 return r;
14577 #undef RECUR
14578 #undef RETURN
14579 }
14580
14581 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
14582 function. For description of the body see comment above
14583 cp_parser_omp_declare_reduction_exprs. */
14584
14585 static void
14586 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14587 {
14588 if (t == NULL_TREE || t == error_mark_node)
14589 return;
14590
14591 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
14592
14593 tree_stmt_iterator tsi;
14594 int i;
14595 tree stmts[7];
14596 memset (stmts, 0, sizeof stmts);
14597 for (i = 0, tsi = tsi_start (t);
14598 i < 7 && !tsi_end_p (tsi);
14599 i++, tsi_next (&tsi))
14600 stmts[i] = tsi_stmt (tsi);
14601 gcc_assert (tsi_end_p (tsi));
14602
14603 if (i >= 3)
14604 {
14605 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
14606 && TREE_CODE (stmts[1]) == DECL_EXPR);
14607 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
14608 args, complain, in_decl);
14609 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
14610 args, complain, in_decl);
14611 DECL_CONTEXT (omp_out) = current_function_decl;
14612 DECL_CONTEXT (omp_in) = current_function_decl;
14613 keep_next_level (true);
14614 tree block = begin_omp_structured_block ();
14615 tsubst_expr (stmts[2], args, complain, in_decl, false);
14616 block = finish_omp_structured_block (block);
14617 block = maybe_cleanup_point_expr_void (block);
14618 add_decl_expr (omp_out);
14619 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
14620 TREE_NO_WARNING (omp_out) = 1;
14621 add_decl_expr (omp_in);
14622 finish_expr_stmt (block);
14623 }
14624 if (i >= 6)
14625 {
14626 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
14627 && TREE_CODE (stmts[4]) == DECL_EXPR);
14628 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
14629 args, complain, in_decl);
14630 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
14631 args, complain, in_decl);
14632 DECL_CONTEXT (omp_priv) = current_function_decl;
14633 DECL_CONTEXT (omp_orig) = current_function_decl;
14634 keep_next_level (true);
14635 tree block = begin_omp_structured_block ();
14636 tsubst_expr (stmts[5], args, complain, in_decl, false);
14637 block = finish_omp_structured_block (block);
14638 block = maybe_cleanup_point_expr_void (block);
14639 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
14640 add_decl_expr (omp_priv);
14641 add_decl_expr (omp_orig);
14642 finish_expr_stmt (block);
14643 if (i == 7)
14644 add_decl_expr (omp_orig);
14645 }
14646 }
14647
14648 /* T is a postfix-expression that is not being used in a function
14649 call. Return the substituted version of T. */
14650
14651 static tree
14652 tsubst_non_call_postfix_expression (tree t, tree args,
14653 tsubst_flags_t complain,
14654 tree in_decl)
14655 {
14656 if (TREE_CODE (t) == SCOPE_REF)
14657 t = tsubst_qualified_id (t, args, complain, in_decl,
14658 /*done=*/false, /*address_p=*/false);
14659 else
14660 t = tsubst_copy_and_build (t, args, complain, in_decl,
14661 /*function_p=*/false,
14662 /*integral_constant_expression_p=*/false);
14663
14664 return t;
14665 }
14666
14667 /* Like tsubst but deals with expressions and performs semantic
14668 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
14669
14670 tree
14671 tsubst_copy_and_build (tree t,
14672 tree args,
14673 tsubst_flags_t complain,
14674 tree in_decl,
14675 bool function_p,
14676 bool integral_constant_expression_p)
14677 {
14678 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
14679 #define RECUR(NODE) \
14680 tsubst_copy_and_build (NODE, args, complain, in_decl, \
14681 /*function_p=*/false, \
14682 integral_constant_expression_p)
14683
14684 tree retval, op1;
14685 location_t loc;
14686
14687 if (t == NULL_TREE || t == error_mark_node)
14688 return t;
14689
14690 loc = input_location;
14691 if (EXPR_HAS_LOCATION (t))
14692 input_location = EXPR_LOCATION (t);
14693
14694 /* N3276 decltype magic only applies to calls at the top level or on the
14695 right side of a comma. */
14696 tsubst_flags_t decltype_flag = (complain & tf_decltype);
14697 complain &= ~tf_decltype;
14698
14699 switch (TREE_CODE (t))
14700 {
14701 case USING_DECL:
14702 t = DECL_NAME (t);
14703 /* Fall through. */
14704 case IDENTIFIER_NODE:
14705 {
14706 tree decl;
14707 cp_id_kind idk;
14708 bool non_integral_constant_expression_p;
14709 const char *error_msg;
14710
14711 if (IDENTIFIER_TYPENAME_P (t))
14712 {
14713 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14714 t = mangle_conv_op_name_for_type (new_type);
14715 }
14716
14717 /* Look up the name. */
14718 decl = lookup_name (t);
14719
14720 /* By convention, expressions use ERROR_MARK_NODE to indicate
14721 failure, not NULL_TREE. */
14722 if (decl == NULL_TREE)
14723 decl = error_mark_node;
14724
14725 decl = finish_id_expression (t, decl, NULL_TREE,
14726 &idk,
14727 integral_constant_expression_p,
14728 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
14729 &non_integral_constant_expression_p,
14730 /*template_p=*/false,
14731 /*done=*/true,
14732 /*address_p=*/false,
14733 /*template_arg_p=*/false,
14734 &error_msg,
14735 input_location);
14736 if (error_msg)
14737 error (error_msg);
14738 if (!function_p && identifier_p (decl))
14739 {
14740 if (complain & tf_error)
14741 unqualified_name_lookup_error (decl);
14742 decl = error_mark_node;
14743 }
14744 RETURN (decl);
14745 }
14746
14747 case TEMPLATE_ID_EXPR:
14748 {
14749 tree object;
14750 tree templ = RECUR (TREE_OPERAND (t, 0));
14751 tree targs = TREE_OPERAND (t, 1);
14752
14753 if (targs)
14754 targs = tsubst_template_args (targs, args, complain, in_decl);
14755 if (targs == error_mark_node)
14756 return error_mark_node;
14757
14758 if (variable_template_p (templ))
14759 {
14760 templ = lookup_template_variable (templ, targs);
14761 if (!any_dependent_template_arguments_p (targs))
14762 {
14763 templ = finish_template_variable (templ, complain);
14764 mark_used (templ);
14765 }
14766 RETURN (convert_from_reference (templ));
14767 }
14768
14769 if (TREE_CODE (templ) == COMPONENT_REF)
14770 {
14771 object = TREE_OPERAND (templ, 0);
14772 templ = TREE_OPERAND (templ, 1);
14773 }
14774 else
14775 object = NULL_TREE;
14776 templ = lookup_template_function (templ, targs);
14777
14778 if (object)
14779 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14780 object, templ, NULL_TREE));
14781 else
14782 RETURN (baselink_for_fns (templ));
14783 }
14784
14785 case INDIRECT_REF:
14786 {
14787 tree r = RECUR (TREE_OPERAND (t, 0));
14788
14789 if (REFERENCE_REF_P (t))
14790 {
14791 /* A type conversion to reference type will be enclosed in
14792 such an indirect ref, but the substitution of the cast
14793 will have also added such an indirect ref. */
14794 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14795 r = convert_from_reference (r);
14796 }
14797 else
14798 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14799 complain|decltype_flag);
14800 RETURN (r);
14801 }
14802
14803 case NOP_EXPR:
14804 {
14805 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14806 tree op0 = RECUR (TREE_OPERAND (t, 0));
14807 RETURN (build_nop (type, op0));
14808 }
14809
14810 case IMPLICIT_CONV_EXPR:
14811 {
14812 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14813 tree expr = RECUR (TREE_OPERAND (t, 0));
14814 int flags = LOOKUP_IMPLICIT;
14815 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14816 flags = LOOKUP_NORMAL;
14817 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14818 flags));
14819 }
14820
14821 case CONVERT_EXPR:
14822 {
14823 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14824 tree op0 = RECUR (TREE_OPERAND (t, 0));
14825 RETURN (build1 (CONVERT_EXPR, type, op0));
14826 }
14827
14828 case CAST_EXPR:
14829 case REINTERPRET_CAST_EXPR:
14830 case CONST_CAST_EXPR:
14831 case DYNAMIC_CAST_EXPR:
14832 case STATIC_CAST_EXPR:
14833 {
14834 tree type;
14835 tree op, r = NULL_TREE;
14836
14837 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14838 if (integral_constant_expression_p
14839 && !cast_valid_in_integral_constant_expression_p (type))
14840 {
14841 if (complain & tf_error)
14842 error ("a cast to a type other than an integral or "
14843 "enumeration type cannot appear in a constant-expression");
14844 RETURN (error_mark_node);
14845 }
14846
14847 op = RECUR (TREE_OPERAND (t, 0));
14848
14849 warning_sentinel s(warn_useless_cast);
14850 switch (TREE_CODE (t))
14851 {
14852 case CAST_EXPR:
14853 r = build_functional_cast (type, op, complain);
14854 break;
14855 case REINTERPRET_CAST_EXPR:
14856 r = build_reinterpret_cast (type, op, complain);
14857 break;
14858 case CONST_CAST_EXPR:
14859 r = build_const_cast (type, op, complain);
14860 break;
14861 case DYNAMIC_CAST_EXPR:
14862 r = build_dynamic_cast (type, op, complain);
14863 break;
14864 case STATIC_CAST_EXPR:
14865 r = build_static_cast (type, op, complain);
14866 break;
14867 default:
14868 gcc_unreachable ();
14869 }
14870
14871 RETURN (r);
14872 }
14873
14874 case POSTDECREMENT_EXPR:
14875 case POSTINCREMENT_EXPR:
14876 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14877 args, complain, in_decl);
14878 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14879 complain|decltype_flag));
14880
14881 case PREDECREMENT_EXPR:
14882 case PREINCREMENT_EXPR:
14883 case NEGATE_EXPR:
14884 case BIT_NOT_EXPR:
14885 case ABS_EXPR:
14886 case TRUTH_NOT_EXPR:
14887 case UNARY_PLUS_EXPR: /* Unary + */
14888 case REALPART_EXPR:
14889 case IMAGPART_EXPR:
14890 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14891 RECUR (TREE_OPERAND (t, 0)),
14892 complain|decltype_flag));
14893
14894 case FIX_TRUNC_EXPR:
14895 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14896 0, complain));
14897
14898 case ADDR_EXPR:
14899 op1 = TREE_OPERAND (t, 0);
14900 if (TREE_CODE (op1) == LABEL_DECL)
14901 RETURN (finish_label_address_expr (DECL_NAME (op1),
14902 EXPR_LOCATION (op1)));
14903 if (TREE_CODE (op1) == SCOPE_REF)
14904 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14905 /*done=*/true, /*address_p=*/true);
14906 else
14907 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14908 in_decl);
14909 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14910 complain|decltype_flag));
14911
14912 case PLUS_EXPR:
14913 case MINUS_EXPR:
14914 case MULT_EXPR:
14915 case TRUNC_DIV_EXPR:
14916 case CEIL_DIV_EXPR:
14917 case FLOOR_DIV_EXPR:
14918 case ROUND_DIV_EXPR:
14919 case EXACT_DIV_EXPR:
14920 case BIT_AND_EXPR:
14921 case BIT_IOR_EXPR:
14922 case BIT_XOR_EXPR:
14923 case TRUNC_MOD_EXPR:
14924 case FLOOR_MOD_EXPR:
14925 case TRUTH_ANDIF_EXPR:
14926 case TRUTH_ORIF_EXPR:
14927 case TRUTH_AND_EXPR:
14928 case TRUTH_OR_EXPR:
14929 case RSHIFT_EXPR:
14930 case LSHIFT_EXPR:
14931 case RROTATE_EXPR:
14932 case LROTATE_EXPR:
14933 case EQ_EXPR:
14934 case NE_EXPR:
14935 case MAX_EXPR:
14936 case MIN_EXPR:
14937 case LE_EXPR:
14938 case GE_EXPR:
14939 case LT_EXPR:
14940 case GT_EXPR:
14941 case MEMBER_REF:
14942 case DOTSTAR_EXPR:
14943 {
14944 warning_sentinel s1(warn_type_limits);
14945 warning_sentinel s2(warn_div_by_zero);
14946 warning_sentinel s3(warn_logical_op);
14947 warning_sentinel s4(warn_tautological_compare);
14948 tree op0 = RECUR (TREE_OPERAND (t, 0));
14949 tree op1 = RECUR (TREE_OPERAND (t, 1));
14950 tree r = build_x_binary_op
14951 (input_location, TREE_CODE (t),
14952 op0,
14953 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14954 ? ERROR_MARK
14955 : TREE_CODE (TREE_OPERAND (t, 0))),
14956 op1,
14957 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14958 ? ERROR_MARK
14959 : TREE_CODE (TREE_OPERAND (t, 1))),
14960 /*overload=*/NULL,
14961 complain|decltype_flag);
14962 if (EXPR_P (r) && TREE_NO_WARNING (t))
14963 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14964
14965 RETURN (r);
14966 }
14967
14968 case POINTER_PLUS_EXPR:
14969 {
14970 tree op0 = RECUR (TREE_OPERAND (t, 0));
14971 tree op1 = RECUR (TREE_OPERAND (t, 1));
14972 return fold_build_pointer_plus (op0, op1);
14973 }
14974
14975 case SCOPE_REF:
14976 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14977 /*address_p=*/false));
14978 case ARRAY_REF:
14979 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14980 args, complain, in_decl);
14981 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14982 RECUR (TREE_OPERAND (t, 1)),
14983 complain|decltype_flag));
14984
14985 case ARRAY_NOTATION_REF:
14986 {
14987 tree start_index, length, stride;
14988 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14989 args, complain, in_decl);
14990 start_index = RECUR (ARRAY_NOTATION_START (t));
14991 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14992 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14993 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14994 length, stride, TREE_TYPE (op1)));
14995 }
14996 case SIZEOF_EXPR:
14997 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14998 RETURN (tsubst_copy (t, args, complain, in_decl));
14999 /* Fall through */
15000
15001 case ALIGNOF_EXPR:
15002 {
15003 tree r;
15004
15005 op1 = TREE_OPERAND (t, 0);
15006 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
15007 op1 = TREE_TYPE (op1);
15008 if (!args)
15009 {
15010 /* When there are no ARGS, we are trying to evaluate a
15011 non-dependent expression from the parser. Trying to do
15012 the substitutions may not work. */
15013 if (!TYPE_P (op1))
15014 op1 = TREE_TYPE (op1);
15015 }
15016 else
15017 {
15018 ++cp_unevaluated_operand;
15019 ++c_inhibit_evaluation_warnings;
15020 if (TYPE_P (op1))
15021 op1 = tsubst (op1, args, complain, in_decl);
15022 else
15023 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15024 /*function_p=*/false,
15025 /*integral_constant_expression_p=*/
15026 false);
15027 --cp_unevaluated_operand;
15028 --c_inhibit_evaluation_warnings;
15029 }
15030 if (TYPE_P (op1))
15031 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
15032 complain & tf_error);
15033 else
15034 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
15035 complain & tf_error);
15036 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
15037 {
15038 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
15039 {
15040 if (!processing_template_decl && TYPE_P (op1))
15041 {
15042 r = build_min (SIZEOF_EXPR, size_type_node,
15043 build1 (NOP_EXPR, op1, error_mark_node));
15044 SIZEOF_EXPR_TYPE_P (r) = 1;
15045 }
15046 else
15047 r = build_min (SIZEOF_EXPR, size_type_node, op1);
15048 TREE_SIDE_EFFECTS (r) = 0;
15049 TREE_READONLY (r) = 1;
15050 }
15051 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
15052 }
15053 RETURN (r);
15054 }
15055
15056 case AT_ENCODE_EXPR:
15057 {
15058 op1 = TREE_OPERAND (t, 0);
15059 ++cp_unevaluated_operand;
15060 ++c_inhibit_evaluation_warnings;
15061 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15062 /*function_p=*/false,
15063 /*integral_constant_expression_p=*/false);
15064 --cp_unevaluated_operand;
15065 --c_inhibit_evaluation_warnings;
15066 RETURN (objc_build_encode_expr (op1));
15067 }
15068
15069 case NOEXCEPT_EXPR:
15070 op1 = TREE_OPERAND (t, 0);
15071 ++cp_unevaluated_operand;
15072 ++c_inhibit_evaluation_warnings;
15073 ++cp_noexcept_operand;
15074 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
15075 /*function_p=*/false,
15076 /*integral_constant_expression_p=*/false);
15077 --cp_unevaluated_operand;
15078 --c_inhibit_evaluation_warnings;
15079 --cp_noexcept_operand;
15080 RETURN (finish_noexcept_expr (op1, complain));
15081
15082 case MODOP_EXPR:
15083 {
15084 warning_sentinel s(warn_div_by_zero);
15085 tree lhs = RECUR (TREE_OPERAND (t, 0));
15086 tree rhs = RECUR (TREE_OPERAND (t, 2));
15087 tree r = build_x_modify_expr
15088 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
15089 complain|decltype_flag);
15090 /* TREE_NO_WARNING must be set if either the expression was
15091 parenthesized or it uses an operator such as >>= rather
15092 than plain assignment. In the former case, it was already
15093 set and must be copied. In the latter case,
15094 build_x_modify_expr sets it and it must not be reset
15095 here. */
15096 if (TREE_NO_WARNING (t))
15097 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15098
15099 RETURN (r);
15100 }
15101
15102 case ARROW_EXPR:
15103 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15104 args, complain, in_decl);
15105 /* Remember that there was a reference to this entity. */
15106 if (DECL_P (op1)
15107 && !mark_used (op1, complain) && !(complain & tf_error))
15108 RETURN (error_mark_node);
15109 RETURN (build_x_arrow (input_location, op1, complain));
15110
15111 case NEW_EXPR:
15112 {
15113 tree placement = RECUR (TREE_OPERAND (t, 0));
15114 tree init = RECUR (TREE_OPERAND (t, 3));
15115 vec<tree, va_gc> *placement_vec;
15116 vec<tree, va_gc> *init_vec;
15117 tree ret;
15118
15119 if (placement == NULL_TREE)
15120 placement_vec = NULL;
15121 else
15122 {
15123 placement_vec = make_tree_vector ();
15124 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
15125 vec_safe_push (placement_vec, TREE_VALUE (placement));
15126 }
15127
15128 /* If there was an initializer in the original tree, but it
15129 instantiated to an empty list, then we should pass a
15130 non-NULL empty vector to tell build_new that it was an
15131 empty initializer() rather than no initializer. This can
15132 only happen when the initializer is a pack expansion whose
15133 parameter packs are of length zero. */
15134 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
15135 init_vec = NULL;
15136 else
15137 {
15138 init_vec = make_tree_vector ();
15139 if (init == void_node)
15140 gcc_assert (init_vec != NULL);
15141 else
15142 {
15143 for (; init != NULL_TREE; init = TREE_CHAIN (init))
15144 vec_safe_push (init_vec, TREE_VALUE (init));
15145 }
15146 }
15147
15148 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
15149 tree op2 = RECUR (TREE_OPERAND (t, 2));
15150 ret = build_new (&placement_vec, op1, op2, &init_vec,
15151 NEW_EXPR_USE_GLOBAL (t),
15152 complain);
15153
15154 if (placement_vec != NULL)
15155 release_tree_vector (placement_vec);
15156 if (init_vec != NULL)
15157 release_tree_vector (init_vec);
15158
15159 RETURN (ret);
15160 }
15161
15162 case DELETE_EXPR:
15163 {
15164 tree op0 = RECUR (TREE_OPERAND (t, 0));
15165 tree op1 = RECUR (TREE_OPERAND (t, 1));
15166 RETURN (delete_sanity (op0, op1,
15167 DELETE_EXPR_USE_VEC (t),
15168 DELETE_EXPR_USE_GLOBAL (t),
15169 complain));
15170 }
15171
15172 case COMPOUND_EXPR:
15173 {
15174 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
15175 complain & ~tf_decltype, in_decl,
15176 /*function_p=*/false,
15177 integral_constant_expression_p);
15178 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
15179 op0,
15180 RECUR (TREE_OPERAND (t, 1)),
15181 complain|decltype_flag));
15182 }
15183
15184 case CALL_EXPR:
15185 {
15186 tree function;
15187 vec<tree, va_gc> *call_args;
15188 unsigned int nargs, i;
15189 bool qualified_p;
15190 bool koenig_p;
15191 tree ret;
15192
15193 function = CALL_EXPR_FN (t);
15194 /* When we parsed the expression, we determined whether or
15195 not Koenig lookup should be performed. */
15196 koenig_p = KOENIG_LOOKUP_P (t);
15197 if (TREE_CODE (function) == SCOPE_REF)
15198 {
15199 qualified_p = true;
15200 function = tsubst_qualified_id (function, args, complain, in_decl,
15201 /*done=*/false,
15202 /*address_p=*/false);
15203 }
15204 else if (koenig_p && identifier_p (function))
15205 {
15206 /* Do nothing; calling tsubst_copy_and_build on an identifier
15207 would incorrectly perform unqualified lookup again.
15208
15209 Note that we can also have an IDENTIFIER_NODE if the earlier
15210 unqualified lookup found a member function; in that case
15211 koenig_p will be false and we do want to do the lookup
15212 again to find the instantiated member function.
15213
15214 FIXME but doing that causes c++/15272, so we need to stop
15215 using IDENTIFIER_NODE in that situation. */
15216 qualified_p = false;
15217 }
15218 else
15219 {
15220 if (TREE_CODE (function) == COMPONENT_REF)
15221 {
15222 tree op = TREE_OPERAND (function, 1);
15223
15224 qualified_p = (TREE_CODE (op) == SCOPE_REF
15225 || (BASELINK_P (op)
15226 && BASELINK_QUALIFIED_P (op)));
15227 }
15228 else
15229 qualified_p = false;
15230
15231 if (TREE_CODE (function) == ADDR_EXPR
15232 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
15233 /* Avoid error about taking the address of a constructor. */
15234 function = TREE_OPERAND (function, 0);
15235
15236 function = tsubst_copy_and_build (function, args, complain,
15237 in_decl,
15238 !qualified_p,
15239 integral_constant_expression_p);
15240
15241 if (BASELINK_P (function))
15242 qualified_p = true;
15243 }
15244
15245 nargs = call_expr_nargs (t);
15246 call_args = make_tree_vector ();
15247 for (i = 0; i < nargs; ++i)
15248 {
15249 tree arg = CALL_EXPR_ARG (t, i);
15250
15251 if (!PACK_EXPANSION_P (arg))
15252 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
15253 else
15254 {
15255 /* Expand the pack expansion and push each entry onto
15256 CALL_ARGS. */
15257 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
15258 if (TREE_CODE (arg) == TREE_VEC)
15259 {
15260 unsigned int len, j;
15261
15262 len = TREE_VEC_LENGTH (arg);
15263 for (j = 0; j < len; ++j)
15264 {
15265 tree value = TREE_VEC_ELT (arg, j);
15266 if (value != NULL_TREE)
15267 value = convert_from_reference (value);
15268 vec_safe_push (call_args, value);
15269 }
15270 }
15271 else
15272 {
15273 /* A partial substitution. Add one entry. */
15274 vec_safe_push (call_args, arg);
15275 }
15276 }
15277 }
15278
15279 /* We do not perform argument-dependent lookup if normal
15280 lookup finds a non-function, in accordance with the
15281 expected resolution of DR 218. */
15282 if (koenig_p
15283 && ((is_overloaded_fn (function)
15284 /* If lookup found a member function, the Koenig lookup is
15285 not appropriate, even if an unqualified-name was used
15286 to denote the function. */
15287 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
15288 || identifier_p (function))
15289 /* Only do this when substitution turns a dependent call
15290 into a non-dependent call. */
15291 && type_dependent_expression_p_push (t)
15292 && !any_type_dependent_arguments_p (call_args))
15293 function = perform_koenig_lookup (function, call_args, tf_none);
15294
15295 if (identifier_p (function)
15296 && !any_type_dependent_arguments_p (call_args))
15297 {
15298 if (koenig_p && (complain & tf_warning_or_error))
15299 {
15300 /* For backwards compatibility and good diagnostics, try
15301 the unqualified lookup again if we aren't in SFINAE
15302 context. */
15303 tree unq = (tsubst_copy_and_build
15304 (function, args, complain, in_decl, true,
15305 integral_constant_expression_p));
15306 if (unq == error_mark_node)
15307 RETURN (error_mark_node);
15308
15309 if (unq != function)
15310 {
15311 tree fn = unq;
15312 if (INDIRECT_REF_P (fn))
15313 fn = TREE_OPERAND (fn, 0);
15314 if (TREE_CODE (fn) == COMPONENT_REF)
15315 fn = TREE_OPERAND (fn, 1);
15316 if (is_overloaded_fn (fn))
15317 fn = get_first_fn (fn);
15318 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
15319 "%qD was not declared in this scope, "
15320 "and no declarations were found by "
15321 "argument-dependent lookup at the point "
15322 "of instantiation", function))
15323 {
15324 if (!DECL_P (fn))
15325 /* Can't say anything more. */;
15326 else if (DECL_CLASS_SCOPE_P (fn))
15327 {
15328 location_t loc = EXPR_LOC_OR_LOC (t,
15329 input_location);
15330 inform (loc,
15331 "declarations in dependent base %qT are "
15332 "not found by unqualified lookup",
15333 DECL_CLASS_CONTEXT (fn));
15334 if (current_class_ptr)
15335 inform (loc,
15336 "use %<this->%D%> instead", function);
15337 else
15338 inform (loc,
15339 "use %<%T::%D%> instead",
15340 current_class_name, function);
15341 }
15342 else
15343 inform (DECL_SOURCE_LOCATION (fn),
15344 "%qD declared here, later in the "
15345 "translation unit", fn);
15346 }
15347 function = unq;
15348 }
15349 }
15350 if (identifier_p (function))
15351 {
15352 if (complain & tf_error)
15353 unqualified_name_lookup_error (function);
15354 release_tree_vector (call_args);
15355 RETURN (error_mark_node);
15356 }
15357 }
15358
15359 /* Remember that there was a reference to this entity. */
15360 if (DECL_P (function)
15361 && !mark_used (function, complain) && !(complain & tf_error))
15362 RETURN (error_mark_node);
15363
15364 /* Put back tf_decltype for the actual call. */
15365 complain |= decltype_flag;
15366
15367 if (TREE_CODE (function) == OFFSET_REF)
15368 ret = build_offset_ref_call_from_tree (function, &call_args,
15369 complain);
15370 else if (TREE_CODE (function) == COMPONENT_REF)
15371 {
15372 tree instance = TREE_OPERAND (function, 0);
15373 tree fn = TREE_OPERAND (function, 1);
15374
15375 if (processing_template_decl
15376 && (type_dependent_expression_p (instance)
15377 || (!BASELINK_P (fn)
15378 && TREE_CODE (fn) != FIELD_DECL)
15379 || type_dependent_expression_p (fn)
15380 || any_type_dependent_arguments_p (call_args)))
15381 ret = build_nt_call_vec (function, call_args);
15382 else if (!BASELINK_P (fn))
15383 ret = finish_call_expr (function, &call_args,
15384 /*disallow_virtual=*/false,
15385 /*koenig_p=*/false,
15386 complain);
15387 else
15388 ret = (build_new_method_call
15389 (instance, fn,
15390 &call_args, NULL_TREE,
15391 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
15392 /*fn_p=*/NULL,
15393 complain));
15394 }
15395 else
15396 ret = finish_call_expr (function, &call_args,
15397 /*disallow_virtual=*/qualified_p,
15398 koenig_p,
15399 complain);
15400
15401 release_tree_vector (call_args);
15402
15403 RETURN (ret);
15404 }
15405
15406 case COND_EXPR:
15407 {
15408 tree cond = RECUR (TREE_OPERAND (t, 0));
15409 tree folded_cond = fold_non_dependent_expr (cond);
15410 tree exp1, exp2;
15411
15412 if (TREE_CODE (folded_cond) == INTEGER_CST)
15413 {
15414 if (integer_zerop (folded_cond))
15415 {
15416 ++c_inhibit_evaluation_warnings;
15417 exp1 = RECUR (TREE_OPERAND (t, 1));
15418 --c_inhibit_evaluation_warnings;
15419 exp2 = RECUR (TREE_OPERAND (t, 2));
15420 }
15421 else
15422 {
15423 exp1 = RECUR (TREE_OPERAND (t, 1));
15424 ++c_inhibit_evaluation_warnings;
15425 exp2 = RECUR (TREE_OPERAND (t, 2));
15426 --c_inhibit_evaluation_warnings;
15427 }
15428 cond = folded_cond;
15429 }
15430 else
15431 {
15432 exp1 = RECUR (TREE_OPERAND (t, 1));
15433 exp2 = RECUR (TREE_OPERAND (t, 2));
15434 }
15435
15436 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
15437 cond, exp1, exp2, complain));
15438 }
15439
15440 case PSEUDO_DTOR_EXPR:
15441 {
15442 tree op0 = RECUR (TREE_OPERAND (t, 0));
15443 tree op1 = RECUR (TREE_OPERAND (t, 1));
15444 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
15445 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
15446 input_location));
15447 }
15448
15449 case TREE_LIST:
15450 {
15451 tree purpose, value, chain;
15452
15453 if (t == void_list_node)
15454 RETURN (t);
15455
15456 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15457 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15458 {
15459 /* We have pack expansions, so expand those and
15460 create a new list out of it. */
15461 tree purposevec = NULL_TREE;
15462 tree valuevec = NULL_TREE;
15463 tree chain;
15464 int i, len = -1;
15465
15466 /* Expand the argument expressions. */
15467 if (TREE_PURPOSE (t))
15468 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15469 complain, in_decl);
15470 if (TREE_VALUE (t))
15471 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15472 complain, in_decl);
15473
15474 /* Build the rest of the list. */
15475 chain = TREE_CHAIN (t);
15476 if (chain && chain != void_type_node)
15477 chain = RECUR (chain);
15478
15479 /* Determine the number of arguments. */
15480 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15481 {
15482 len = TREE_VEC_LENGTH (purposevec);
15483 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15484 }
15485 else if (TREE_CODE (valuevec) == TREE_VEC)
15486 len = TREE_VEC_LENGTH (valuevec);
15487 else
15488 {
15489 /* Since we only performed a partial substitution into
15490 the argument pack, we only RETURN (a single list
15491 node. */
15492 if (purposevec == TREE_PURPOSE (t)
15493 && valuevec == TREE_VALUE (t)
15494 && chain == TREE_CHAIN (t))
15495 RETURN (t);
15496
15497 RETURN (tree_cons (purposevec, valuevec, chain));
15498 }
15499
15500 /* Convert the argument vectors into a TREE_LIST */
15501 i = len;
15502 while (i > 0)
15503 {
15504 /* Grab the Ith values. */
15505 i--;
15506 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
15507 : NULL_TREE;
15508 value
15509 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
15510 : NULL_TREE;
15511
15512 /* Build the list (backwards). */
15513 chain = tree_cons (purpose, value, chain);
15514 }
15515
15516 RETURN (chain);
15517 }
15518
15519 purpose = TREE_PURPOSE (t);
15520 if (purpose)
15521 purpose = RECUR (purpose);
15522 value = TREE_VALUE (t);
15523 if (value)
15524 value = RECUR (value);
15525 chain = TREE_CHAIN (t);
15526 if (chain && chain != void_type_node)
15527 chain = RECUR (chain);
15528 if (purpose == TREE_PURPOSE (t)
15529 && value == TREE_VALUE (t)
15530 && chain == TREE_CHAIN (t))
15531 RETURN (t);
15532 RETURN (tree_cons (purpose, value, chain));
15533 }
15534
15535 case COMPONENT_REF:
15536 {
15537 tree object;
15538 tree object_type;
15539 tree member;
15540 tree r;
15541
15542 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
15543 args, complain, in_decl);
15544 /* Remember that there was a reference to this entity. */
15545 if (DECL_P (object)
15546 && !mark_used (object, complain) && !(complain & tf_error))
15547 RETURN (error_mark_node);
15548 object_type = TREE_TYPE (object);
15549
15550 member = TREE_OPERAND (t, 1);
15551 if (BASELINK_P (member))
15552 member = tsubst_baselink (member,
15553 non_reference (TREE_TYPE (object)),
15554 args, complain, in_decl);
15555 else
15556 member = tsubst_copy (member, args, complain, in_decl);
15557 if (member == error_mark_node)
15558 RETURN (error_mark_node);
15559
15560 if (type_dependent_expression_p (object))
15561 /* We can't do much here. */;
15562 else if (!CLASS_TYPE_P (object_type))
15563 {
15564 if (scalarish_type_p (object_type))
15565 {
15566 tree s = NULL_TREE;
15567 tree dtor = member;
15568
15569 if (TREE_CODE (dtor) == SCOPE_REF)
15570 {
15571 s = TREE_OPERAND (dtor, 0);
15572 dtor = TREE_OPERAND (dtor, 1);
15573 }
15574 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
15575 {
15576 dtor = TREE_OPERAND (dtor, 0);
15577 if (TYPE_P (dtor))
15578 RETURN (finish_pseudo_destructor_expr
15579 (object, s, dtor, input_location));
15580 }
15581 }
15582 }
15583 else if (TREE_CODE (member) == SCOPE_REF
15584 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
15585 {
15586 /* Lookup the template functions now that we know what the
15587 scope is. */
15588 tree scope = TREE_OPERAND (member, 0);
15589 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
15590 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
15591 member = lookup_qualified_name (scope, tmpl,
15592 /*is_type_p=*/false,
15593 /*complain=*/false);
15594 if (BASELINK_P (member))
15595 {
15596 BASELINK_FUNCTIONS (member)
15597 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
15598 args);
15599 member = (adjust_result_of_qualified_name_lookup
15600 (member, BINFO_TYPE (BASELINK_BINFO (member)),
15601 object_type));
15602 }
15603 else
15604 {
15605 qualified_name_lookup_error (scope, tmpl, member,
15606 input_location);
15607 RETURN (error_mark_node);
15608 }
15609 }
15610 else if (TREE_CODE (member) == SCOPE_REF
15611 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
15612 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
15613 {
15614 if (complain & tf_error)
15615 {
15616 if (TYPE_P (TREE_OPERAND (member, 0)))
15617 error ("%qT is not a class or namespace",
15618 TREE_OPERAND (member, 0));
15619 else
15620 error ("%qD is not a class or namespace",
15621 TREE_OPERAND (member, 0));
15622 }
15623 RETURN (error_mark_node);
15624 }
15625 else if (TREE_CODE (member) == FIELD_DECL)
15626 {
15627 r = finish_non_static_data_member (member, object, NULL_TREE);
15628 if (TREE_CODE (r) == COMPONENT_REF)
15629 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15630 RETURN (r);
15631 }
15632
15633 r = finish_class_member_access_expr (object, member,
15634 /*template_p=*/false,
15635 complain);
15636 if (TREE_CODE (r) == COMPONENT_REF)
15637 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
15638 RETURN (r);
15639 }
15640
15641 case THROW_EXPR:
15642 RETURN (build_throw
15643 (RECUR (TREE_OPERAND (t, 0))));
15644
15645 case CONSTRUCTOR:
15646 {
15647 vec<constructor_elt, va_gc> *n;
15648 constructor_elt *ce;
15649 unsigned HOST_WIDE_INT idx;
15650 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15651 bool process_index_p;
15652 int newlen;
15653 bool need_copy_p = false;
15654 tree r;
15655
15656 if (type == error_mark_node)
15657 RETURN (error_mark_node);
15658
15659 /* digest_init will do the wrong thing if we let it. */
15660 if (type && TYPE_PTRMEMFUNC_P (type))
15661 RETURN (t);
15662
15663 /* We do not want to process the index of aggregate
15664 initializers as they are identifier nodes which will be
15665 looked up by digest_init. */
15666 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
15667
15668 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
15669 newlen = vec_safe_length (n);
15670 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
15671 {
15672 if (ce->index && process_index_p
15673 /* An identifier index is looked up in the type
15674 being initialized, not the current scope. */
15675 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
15676 ce->index = RECUR (ce->index);
15677
15678 if (PACK_EXPANSION_P (ce->value))
15679 {
15680 /* Substitute into the pack expansion. */
15681 ce->value = tsubst_pack_expansion (ce->value, args, complain,
15682 in_decl);
15683
15684 if (ce->value == error_mark_node
15685 || PACK_EXPANSION_P (ce->value))
15686 ;
15687 else if (TREE_VEC_LENGTH (ce->value) == 1)
15688 /* Just move the argument into place. */
15689 ce->value = TREE_VEC_ELT (ce->value, 0);
15690 else
15691 {
15692 /* Update the length of the final CONSTRUCTOR
15693 arguments vector, and note that we will need to
15694 copy.*/
15695 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
15696 need_copy_p = true;
15697 }
15698 }
15699 else
15700 ce->value = RECUR (ce->value);
15701 }
15702
15703 if (need_copy_p)
15704 {
15705 vec<constructor_elt, va_gc> *old_n = n;
15706
15707 vec_alloc (n, newlen);
15708 FOR_EACH_VEC_ELT (*old_n, idx, ce)
15709 {
15710 if (TREE_CODE (ce->value) == TREE_VEC)
15711 {
15712 int i, len = TREE_VEC_LENGTH (ce->value);
15713 for (i = 0; i < len; ++i)
15714 CONSTRUCTOR_APPEND_ELT (n, 0,
15715 TREE_VEC_ELT (ce->value, i));
15716 }
15717 else
15718 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
15719 }
15720 }
15721
15722 r = build_constructor (init_list_type_node, n);
15723 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
15724
15725 if (TREE_HAS_CONSTRUCTOR (t))
15726 RETURN (finish_compound_literal (type, r, complain));
15727
15728 TREE_TYPE (r) = type;
15729 RETURN (r);
15730 }
15731
15732 case TYPEID_EXPR:
15733 {
15734 tree operand_0 = TREE_OPERAND (t, 0);
15735 if (TYPE_P (operand_0))
15736 {
15737 operand_0 = tsubst (operand_0, args, complain, in_decl);
15738 RETURN (get_typeid (operand_0, complain));
15739 }
15740 else
15741 {
15742 operand_0 = RECUR (operand_0);
15743 RETURN (build_typeid (operand_0, complain));
15744 }
15745 }
15746
15747 case VAR_DECL:
15748 if (!args)
15749 RETURN (t);
15750 else if (DECL_PACK_P (t))
15751 {
15752 /* We don't build decls for an instantiation of a
15753 variadic capture proxy, we instantiate the elements
15754 when needed. */
15755 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
15756 return RECUR (DECL_VALUE_EXPR (t));
15757 }
15758 /* Fall through */
15759
15760 case PARM_DECL:
15761 {
15762 tree r = tsubst_copy (t, args, complain, in_decl);
15763 /* ??? We're doing a subset of finish_id_expression here. */
15764 if (VAR_P (r)
15765 && !processing_template_decl
15766 && !cp_unevaluated_operand
15767 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
15768 && CP_DECL_THREAD_LOCAL_P (r))
15769 {
15770 if (tree wrap = get_tls_wrapper_fn (r))
15771 /* Replace an evaluated use of the thread_local variable with
15772 a call to its wrapper. */
15773 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
15774 }
15775 else if (outer_automatic_var_p (r))
15776 {
15777 r = process_outer_var_ref (r, complain);
15778 if (is_capture_proxy (r))
15779 register_local_specialization (r, t);
15780 }
15781
15782 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
15783 /* If the original type was a reference, we'll be wrapped in
15784 the appropriate INDIRECT_REF. */
15785 r = convert_from_reference (r);
15786 RETURN (r);
15787 }
15788
15789 case VA_ARG_EXPR:
15790 {
15791 tree op0 = RECUR (TREE_OPERAND (t, 0));
15792 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15793 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
15794 }
15795
15796 case OFFSETOF_EXPR:
15797 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0)),
15798 EXPR_LOCATION (t)));
15799
15800 case TRAIT_EXPR:
15801 {
15802 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
15803 complain, in_decl);
15804
15805 tree type2 = TRAIT_EXPR_TYPE2 (t);
15806 if (type2 && TREE_CODE (type2) == TREE_LIST)
15807 type2 = RECUR (type2);
15808 else if (type2)
15809 type2 = tsubst (type2, args, complain, in_decl);
15810
15811 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
15812 }
15813
15814 case STMT_EXPR:
15815 {
15816 tree old_stmt_expr = cur_stmt_expr;
15817 tree stmt_expr = begin_stmt_expr ();
15818
15819 cur_stmt_expr = stmt_expr;
15820 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15821 integral_constant_expression_p);
15822 stmt_expr = finish_stmt_expr (stmt_expr, false);
15823 cur_stmt_expr = old_stmt_expr;
15824
15825 /* If the resulting list of expression statement is empty,
15826 fold it further into void_node. */
15827 if (empty_expr_stmt_p (stmt_expr))
15828 stmt_expr = void_node;
15829
15830 RETURN (stmt_expr);
15831 }
15832
15833 case LAMBDA_EXPR:
15834 {
15835 tree r = build_lambda_expr ();
15836
15837 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15838 LAMBDA_EXPR_CLOSURE (r) = type;
15839 CLASSTYPE_LAMBDA_EXPR (type) = r;
15840
15841 LAMBDA_EXPR_LOCATION (r)
15842 = LAMBDA_EXPR_LOCATION (t);
15843 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15844 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15845 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15846 LAMBDA_EXPR_DISCRIMINATOR (r)
15847 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15848 /* For a function scope, we want to use tsubst so that we don't
15849 complain about referring to an auto function before its return
15850 type has been deduced. Otherwise, we want to use tsubst_copy so
15851 that we look up the existing field/parameter/variable rather
15852 than build a new one. */
15853 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15854 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15855 scope = tsubst (scope, args, complain, in_decl);
15856 else if (scope && TREE_CODE (scope) == PARM_DECL)
15857 {
15858 /* Look up the parameter we want directly, as tsubst_copy
15859 doesn't do what we need. */
15860 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15861 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15862 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15863 parm = DECL_CHAIN (parm);
15864 scope = parm;
15865 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15866 if (DECL_CONTEXT (scope) == NULL_TREE)
15867 DECL_CONTEXT (scope) = fn;
15868 }
15869 else
15870 scope = RECUR (scope);
15871 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15872 LAMBDA_EXPR_RETURN_TYPE (r)
15873 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15874
15875 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15876 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15877
15878 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15879 determine_visibility (TYPE_NAME (type));
15880 /* Now that we know visibility, instantiate the type so we have a
15881 declaration of the op() for later calls to lambda_function. */
15882 complete_type (type);
15883
15884 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15885
15886 RETURN (build_lambda_object (r));
15887 }
15888
15889 case TARGET_EXPR:
15890 /* We can get here for a constant initializer of non-dependent type.
15891 FIXME stop folding in cp_parser_initializer_clause. */
15892 {
15893 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15894 complain);
15895 RETURN (r);
15896 }
15897
15898 case TRANSACTION_EXPR:
15899 RETURN (tsubst_expr(t, args, complain, in_decl,
15900 integral_constant_expression_p));
15901
15902 case PAREN_EXPR:
15903 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15904
15905 case VEC_PERM_EXPR:
15906 {
15907 tree op0 = RECUR (TREE_OPERAND (t, 0));
15908 tree op1 = RECUR (TREE_OPERAND (t, 1));
15909 tree op2 = RECUR (TREE_OPERAND (t, 2));
15910 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
15911 complain));
15912 }
15913
15914 default:
15915 /* Handle Objective-C++ constructs, if appropriate. */
15916 {
15917 tree subst
15918 = objcp_tsubst_copy_and_build (t, args, complain,
15919 in_decl, /*function_p=*/false);
15920 if (subst)
15921 RETURN (subst);
15922 }
15923 RETURN (tsubst_copy (t, args, complain, in_decl));
15924 }
15925
15926 #undef RECUR
15927 #undef RETURN
15928 out:
15929 input_location = loc;
15930 return retval;
15931 }
15932
15933 /* Verify that the instantiated ARGS are valid. For type arguments,
15934 make sure that the type's linkage is ok. For non-type arguments,
15935 make sure they are constants if they are integral or enumerations.
15936 Emit an error under control of COMPLAIN, and return TRUE on error. */
15937
15938 static bool
15939 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15940 {
15941 if (dependent_template_arg_p (t))
15942 return false;
15943 if (ARGUMENT_PACK_P (t))
15944 {
15945 tree vec = ARGUMENT_PACK_ARGS (t);
15946 int len = TREE_VEC_LENGTH (vec);
15947 bool result = false;
15948 int i;
15949
15950 for (i = 0; i < len; ++i)
15951 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15952 result = true;
15953 return result;
15954 }
15955 else if (TYPE_P (t))
15956 {
15957 /* [basic.link]: A name with no linkage (notably, the name
15958 of a class or enumeration declared in a local scope)
15959 shall not be used to declare an entity with linkage.
15960 This implies that names with no linkage cannot be used as
15961 template arguments
15962
15963 DR 757 relaxes this restriction for C++0x. */
15964 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15965 : no_linkage_check (t, /*relaxed_p=*/false));
15966
15967 if (nt)
15968 {
15969 /* DR 488 makes use of a type with no linkage cause
15970 type deduction to fail. */
15971 if (complain & tf_error)
15972 {
15973 if (TYPE_ANONYMOUS_P (nt))
15974 error ("%qT is/uses anonymous type", t);
15975 else
15976 error ("template argument for %qD uses local type %qT",
15977 tmpl, t);
15978 }
15979 return true;
15980 }
15981 /* In order to avoid all sorts of complications, we do not
15982 allow variably-modified types as template arguments. */
15983 else if (variably_modified_type_p (t, NULL_TREE))
15984 {
15985 if (complain & tf_error)
15986 error ("%qT is a variably modified type", t);
15987 return true;
15988 }
15989 }
15990 /* Class template and alias template arguments should be OK. */
15991 else if (DECL_TYPE_TEMPLATE_P (t))
15992 ;
15993 /* A non-type argument of integral or enumerated type must be a
15994 constant. */
15995 else if (TREE_TYPE (t)
15996 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15997 && !REFERENCE_REF_P (t)
15998 && !TREE_CONSTANT (t))
15999 {
16000 if (complain & tf_error)
16001 error ("integral expression %qE is not constant", t);
16002 return true;
16003 }
16004 return false;
16005 }
16006
16007 static bool
16008 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
16009 {
16010 int ix, len = DECL_NTPARMS (tmpl);
16011 bool result = false;
16012
16013 for (ix = 0; ix != len; ix++)
16014 {
16015 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
16016 result = true;
16017 }
16018 if (result && (complain & tf_error))
16019 error (" trying to instantiate %qD", tmpl);
16020 return result;
16021 }
16022
16023 /* We're out of SFINAE context now, so generate diagnostics for the access
16024 errors we saw earlier when instantiating D from TMPL and ARGS. */
16025
16026 static void
16027 recheck_decl_substitution (tree d, tree tmpl, tree args)
16028 {
16029 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
16030 tree type = TREE_TYPE (pattern);
16031 location_t loc = input_location;
16032
16033 push_access_scope (d);
16034 push_deferring_access_checks (dk_no_deferred);
16035 input_location = DECL_SOURCE_LOCATION (pattern);
16036 tsubst (type, args, tf_warning_or_error, d);
16037 input_location = loc;
16038 pop_deferring_access_checks ();
16039 pop_access_scope (d);
16040 }
16041
16042 /* Instantiate the indicated variable, function, or alias template TMPL with
16043 the template arguments in TARG_PTR. */
16044
16045 static tree
16046 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
16047 {
16048 tree targ_ptr = orig_args;
16049 tree fndecl;
16050 tree gen_tmpl;
16051 tree spec;
16052 bool access_ok = true;
16053
16054 if (tmpl == error_mark_node)
16055 return error_mark_node;
16056
16057 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
16058
16059 /* If this function is a clone, handle it specially. */
16060 if (DECL_CLONED_FUNCTION_P (tmpl))
16061 {
16062 tree spec;
16063 tree clone;
16064
16065 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
16066 DECL_CLONED_FUNCTION. */
16067 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
16068 targ_ptr, complain);
16069 if (spec == error_mark_node)
16070 return error_mark_node;
16071
16072 /* Look for the clone. */
16073 FOR_EACH_CLONE (clone, spec)
16074 if (DECL_NAME (clone) == DECL_NAME (tmpl))
16075 return clone;
16076 /* We should always have found the clone by now. */
16077 gcc_unreachable ();
16078 return NULL_TREE;
16079 }
16080
16081 if (targ_ptr == error_mark_node)
16082 return error_mark_node;
16083
16084 /* Check to see if we already have this specialization. */
16085 gen_tmpl = most_general_template (tmpl);
16086 if (tmpl != gen_tmpl)
16087 /* The TMPL is a partial instantiation. To get a full set of
16088 arguments we must add the arguments used to perform the
16089 partial instantiation. */
16090 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
16091 targ_ptr);
16092
16093 /* It would be nice to avoid hashing here and then again in tsubst_decl,
16094 but it doesn't seem to be on the hot path. */
16095 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
16096
16097 gcc_assert (tmpl == gen_tmpl
16098 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
16099 == spec)
16100 || fndecl == NULL_TREE);
16101
16102 if (spec != NULL_TREE)
16103 {
16104 if (FNDECL_HAS_ACCESS_ERRORS (spec))
16105 {
16106 if (complain & tf_error)
16107 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
16108 return error_mark_node;
16109 }
16110 return spec;
16111 }
16112
16113 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
16114 complain))
16115 return error_mark_node;
16116
16117 /* We are building a FUNCTION_DECL, during which the access of its
16118 parameters and return types have to be checked. However this
16119 FUNCTION_DECL which is the desired context for access checking
16120 is not built yet. We solve this chicken-and-egg problem by
16121 deferring all checks until we have the FUNCTION_DECL. */
16122 push_deferring_access_checks (dk_deferred);
16123
16124 /* Instantiation of the function happens in the context of the function
16125 template, not the context of the overload resolution we're doing. */
16126 push_to_top_level ();
16127 /* If there are dependent arguments, e.g. because we're doing partial
16128 ordering, make sure processing_template_decl stays set. */
16129 if (uses_template_parms (targ_ptr))
16130 ++processing_template_decl;
16131 if (DECL_CLASS_SCOPE_P (gen_tmpl))
16132 {
16133 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
16134 complain, gen_tmpl, true);
16135 push_nested_class (ctx);
16136 }
16137
16138 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
16139
16140 if (VAR_P (pattern))
16141 {
16142 /* We need to determine if we're using a partial or explicit
16143 specialization now, because the type of the variable could be
16144 different. */
16145 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
16146 tree elt = most_specialized_partial_spec (tid, complain);
16147 if (elt == error_mark_node)
16148 pattern = error_mark_node;
16149 else if (elt)
16150 {
16151 tmpl = TREE_VALUE (elt);
16152 pattern = DECL_TEMPLATE_RESULT (tmpl);
16153 targ_ptr = TREE_PURPOSE (elt);
16154 }
16155 }
16156
16157 /* Substitute template parameters to obtain the specialization. */
16158 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
16159 if (DECL_CLASS_SCOPE_P (gen_tmpl))
16160 pop_nested_class ();
16161 pop_from_top_level ();
16162
16163 if (fndecl == error_mark_node)
16164 {
16165 pop_deferring_access_checks ();
16166 return error_mark_node;
16167 }
16168
16169 /* The DECL_TI_TEMPLATE should always be the immediate parent
16170 template, not the most general template. */
16171 DECL_TI_TEMPLATE (fndecl) = tmpl;
16172 DECL_TI_ARGS (fndecl) = targ_ptr;
16173
16174 /* Now we know the specialization, compute access previously
16175 deferred. */
16176 push_access_scope (fndecl);
16177 if (!perform_deferred_access_checks (complain))
16178 access_ok = false;
16179 pop_access_scope (fndecl);
16180 pop_deferring_access_checks ();
16181
16182 /* If we've just instantiated the main entry point for a function,
16183 instantiate all the alternate entry points as well. We do this
16184 by cloning the instantiation of the main entry point, not by
16185 instantiating the template clones. */
16186 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
16187 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
16188
16189 if (!access_ok)
16190 {
16191 if (!(complain & tf_error))
16192 {
16193 /* Remember to reinstantiate when we're out of SFINAE so the user
16194 can see the errors. */
16195 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
16196 }
16197 return error_mark_node;
16198 }
16199 return fndecl;
16200 }
16201
16202 /* Wrapper for instantiate_template_1. */
16203
16204 tree
16205 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
16206 {
16207 tree ret;
16208 timevar_push (TV_TEMPLATE_INST);
16209 ret = instantiate_template_1 (tmpl, orig_args, complain);
16210 timevar_pop (TV_TEMPLATE_INST);
16211 return ret;
16212 }
16213
16214 /* Instantiate the alias template TMPL with ARGS. Also push a template
16215 instantiation level, which instantiate_template doesn't do because
16216 functions and variables have sufficient context established by the
16217 callers. */
16218
16219 static tree
16220 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
16221 {
16222 struct pending_template *old_last_pend = last_pending_template;
16223 struct tinst_level *old_error_tinst = last_error_tinst_level;
16224 if (tmpl == error_mark_node || args == error_mark_node)
16225 return error_mark_node;
16226 tree tinst = build_tree_list (tmpl, args);
16227 if (!push_tinst_level (tinst))
16228 {
16229 ggc_free (tinst);
16230 return error_mark_node;
16231 }
16232
16233 args =
16234 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
16235 args, tmpl, complain,
16236 /*require_all_args=*/true,
16237 /*use_default_args=*/true);
16238
16239 tree r = instantiate_template (tmpl, args, complain);
16240 pop_tinst_level ();
16241 /* We can't free this if a pending_template entry or last_error_tinst_level
16242 is pointing at it. */
16243 if (last_pending_template == old_last_pend
16244 && last_error_tinst_level == old_error_tinst)
16245 ggc_free (tinst);
16246
16247 return r;
16248 }
16249
16250 /* PARM is a template parameter pack for FN. Returns true iff
16251 PARM is used in a deducible way in the argument list of FN. */
16252
16253 static bool
16254 pack_deducible_p (tree parm, tree fn)
16255 {
16256 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
16257 for (; t; t = TREE_CHAIN (t))
16258 {
16259 tree type = TREE_VALUE (t);
16260 tree packs;
16261 if (!PACK_EXPANSION_P (type))
16262 continue;
16263 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
16264 packs; packs = TREE_CHAIN (packs))
16265 if (template_args_equal (TREE_VALUE (packs), parm))
16266 {
16267 /* The template parameter pack is used in a function parameter
16268 pack. If this is the end of the parameter list, the
16269 template parameter pack is deducible. */
16270 if (TREE_CHAIN (t) == void_list_node)
16271 return true;
16272 else
16273 /* Otherwise, not. Well, it could be deduced from
16274 a non-pack parameter, but doing so would end up with
16275 a deduction mismatch, so don't bother. */
16276 return false;
16277 }
16278 }
16279 /* The template parameter pack isn't used in any function parameter
16280 packs, but it might be used deeper, e.g. tuple<Args...>. */
16281 return true;
16282 }
16283
16284 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
16285 NARGS elements of the arguments that are being used when calling
16286 it. TARGS is a vector into which the deduced template arguments
16287 are placed.
16288
16289 Returns either a FUNCTION_DECL for the matching specialization of FN or
16290 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
16291 true, diagnostics will be printed to explain why it failed.
16292
16293 If FN is a conversion operator, or we are trying to produce a specific
16294 specialization, RETURN_TYPE is the return type desired.
16295
16296 The EXPLICIT_TARGS are explicit template arguments provided via a
16297 template-id.
16298
16299 The parameter STRICT is one of:
16300
16301 DEDUCE_CALL:
16302 We are deducing arguments for a function call, as in
16303 [temp.deduct.call].
16304
16305 DEDUCE_CONV:
16306 We are deducing arguments for a conversion function, as in
16307 [temp.deduct.conv].
16308
16309 DEDUCE_EXACT:
16310 We are deducing arguments when doing an explicit instantiation
16311 as in [temp.explicit], when determining an explicit specialization
16312 as in [temp.expl.spec], or when taking the address of a function
16313 template, as in [temp.deduct.funcaddr]. */
16314
16315 tree
16316 fn_type_unification (tree fn,
16317 tree explicit_targs,
16318 tree targs,
16319 const tree *args,
16320 unsigned int nargs,
16321 tree return_type,
16322 unification_kind_t strict,
16323 int flags,
16324 bool explain_p,
16325 bool decltype_p)
16326 {
16327 tree parms;
16328 tree fntype;
16329 tree decl = NULL_TREE;
16330 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
16331 bool ok;
16332 static int deduction_depth;
16333 struct pending_template *old_last_pend = last_pending_template;
16334 struct tinst_level *old_error_tinst = last_error_tinst_level;
16335 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
16336 tree tinst;
16337 tree r = error_mark_node;
16338
16339 if (decltype_p)
16340 complain |= tf_decltype;
16341
16342 /* In C++0x, it's possible to have a function template whose type depends
16343 on itself recursively. This is most obvious with decltype, but can also
16344 occur with enumeration scope (c++/48969). So we need to catch infinite
16345 recursion and reject the substitution at deduction time; this function
16346 will return error_mark_node for any repeated substitution.
16347
16348 This also catches excessive recursion such as when f<N> depends on
16349 f<N-1> across all integers, and returns error_mark_node for all the
16350 substitutions back up to the initial one.
16351
16352 This is, of course, not reentrant. */
16353 if (excessive_deduction_depth)
16354 return error_mark_node;
16355 tinst = build_tree_list (fn, NULL_TREE);
16356 ++deduction_depth;
16357
16358 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
16359
16360 fntype = TREE_TYPE (fn);
16361 if (explicit_targs)
16362 {
16363 /* [temp.deduct]
16364
16365 The specified template arguments must match the template
16366 parameters in kind (i.e., type, nontype, template), and there
16367 must not be more arguments than there are parameters;
16368 otherwise type deduction fails.
16369
16370 Nontype arguments must match the types of the corresponding
16371 nontype template parameters, or must be convertible to the
16372 types of the corresponding nontype parameters as specified in
16373 _temp.arg.nontype_, otherwise type deduction fails.
16374
16375 All references in the function type of the function template
16376 to the corresponding template parameters are replaced by the
16377 specified template argument values. If a substitution in a
16378 template parameter or in the function type of the function
16379 template results in an invalid type, type deduction fails. */
16380 int i, len = TREE_VEC_LENGTH (tparms);
16381 location_t loc = input_location;
16382 bool incomplete = false;
16383
16384 /* Adjust any explicit template arguments before entering the
16385 substitution context. */
16386 explicit_targs
16387 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
16388 complain,
16389 /*require_all_args=*/false,
16390 /*use_default_args=*/false));
16391 if (explicit_targs == error_mark_node)
16392 goto fail;
16393
16394 /* Substitute the explicit args into the function type. This is
16395 necessary so that, for instance, explicitly declared function
16396 arguments can match null pointed constants. If we were given
16397 an incomplete set of explicit args, we must not do semantic
16398 processing during substitution as we could create partial
16399 instantiations. */
16400 for (i = 0; i < len; i++)
16401 {
16402 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16403 bool parameter_pack = false;
16404 tree targ = TREE_VEC_ELT (explicit_targs, i);
16405
16406 /* Dig out the actual parm. */
16407 if (TREE_CODE (parm) == TYPE_DECL
16408 || TREE_CODE (parm) == TEMPLATE_DECL)
16409 {
16410 parm = TREE_TYPE (parm);
16411 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
16412 }
16413 else if (TREE_CODE (parm) == PARM_DECL)
16414 {
16415 parm = DECL_INITIAL (parm);
16416 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
16417 }
16418
16419 if (!parameter_pack && targ == NULL_TREE)
16420 /* No explicit argument for this template parameter. */
16421 incomplete = true;
16422
16423 if (parameter_pack && pack_deducible_p (parm, fn))
16424 {
16425 /* Mark the argument pack as "incomplete". We could
16426 still deduce more arguments during unification.
16427 We remove this mark in type_unification_real. */
16428 if (targ)
16429 {
16430 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
16431 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
16432 = ARGUMENT_PACK_ARGS (targ);
16433 }
16434
16435 /* We have some incomplete argument packs. */
16436 incomplete = true;
16437 }
16438 }
16439
16440 TREE_VALUE (tinst) = explicit_targs;
16441 if (!push_tinst_level (tinst))
16442 {
16443 excessive_deduction_depth = true;
16444 goto fail;
16445 }
16446 processing_template_decl += incomplete;
16447 input_location = DECL_SOURCE_LOCATION (fn);
16448 /* Ignore any access checks; we'll see them again in
16449 instantiate_template and they might have the wrong
16450 access path at this point. */
16451 push_deferring_access_checks (dk_deferred);
16452 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
16453 complain | tf_partial, NULL_TREE);
16454 pop_deferring_access_checks ();
16455 input_location = loc;
16456 processing_template_decl -= incomplete;
16457 pop_tinst_level ();
16458
16459 if (fntype == error_mark_node)
16460 goto fail;
16461
16462 /* Place the explicitly specified arguments in TARGS. */
16463 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
16464 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
16465 }
16466
16467 /* Never do unification on the 'this' parameter. */
16468 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
16469
16470 if (return_type)
16471 {
16472 tree *new_args;
16473
16474 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
16475 new_args = XALLOCAVEC (tree, nargs + 1);
16476 new_args[0] = return_type;
16477 memcpy (new_args + 1, args, nargs * sizeof (tree));
16478 args = new_args;
16479 ++nargs;
16480 }
16481
16482 /* We allow incomplete unification without an error message here
16483 because the standard doesn't seem to explicitly prohibit it. Our
16484 callers must be ready to deal with unification failures in any
16485 event. */
16486
16487 TREE_VALUE (tinst) = targs;
16488 /* If we aren't explaining yet, push tinst context so we can see where
16489 any errors (e.g. from class instantiations triggered by instantiation
16490 of default template arguments) come from. If we are explaining, this
16491 context is redundant. */
16492 if (!explain_p && !push_tinst_level (tinst))
16493 {
16494 excessive_deduction_depth = true;
16495 goto fail;
16496 }
16497
16498 /* type_unification_real will pass back any access checks from default
16499 template argument substitution. */
16500 vec<deferred_access_check, va_gc> *checks;
16501 checks = NULL;
16502
16503 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16504 targs, parms, args, nargs, /*subr=*/0,
16505 strict, flags, &checks, explain_p);
16506 if (!explain_p)
16507 pop_tinst_level ();
16508 if (!ok)
16509 goto fail;
16510
16511 /* Now that we have bindings for all of the template arguments,
16512 ensure that the arguments deduced for the template template
16513 parameters have compatible template parameter lists. We cannot
16514 check this property before we have deduced all template
16515 arguments, because the template parameter types of a template
16516 template parameter might depend on prior template parameters
16517 deduced after the template template parameter. The following
16518 ill-formed example illustrates this issue:
16519
16520 template<typename T, template<T> class C> void f(C<5>, T);
16521
16522 template<int N> struct X {};
16523
16524 void g() {
16525 f(X<5>(), 5l); // error: template argument deduction fails
16526 }
16527
16528 The template parameter list of 'C' depends on the template type
16529 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
16530 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
16531 time that we deduce 'C'. */
16532 if (!template_template_parm_bindings_ok_p
16533 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
16534 {
16535 unify_inconsistent_template_template_parameters (explain_p);
16536 goto fail;
16537 }
16538
16539 /* All is well so far. Now, check:
16540
16541 [temp.deduct]
16542
16543 When all template arguments have been deduced, all uses of
16544 template parameters in nondeduced contexts are replaced with
16545 the corresponding deduced argument values. If the
16546 substitution results in an invalid type, as described above,
16547 type deduction fails. */
16548 TREE_VALUE (tinst) = targs;
16549 if (!push_tinst_level (tinst))
16550 {
16551 excessive_deduction_depth = true;
16552 goto fail;
16553 }
16554
16555 /* Also collect access checks from the instantiation. */
16556 reopen_deferring_access_checks (checks);
16557
16558 decl = instantiate_template (fn, targs, complain);
16559
16560 checks = get_deferred_access_checks ();
16561 pop_deferring_access_checks ();
16562
16563 pop_tinst_level ();
16564
16565 if (decl == error_mark_node)
16566 goto fail;
16567
16568 /* Now perform any access checks encountered during substitution. */
16569 push_access_scope (decl);
16570 ok = perform_access_checks (checks, complain);
16571 pop_access_scope (decl);
16572 if (!ok)
16573 goto fail;
16574
16575 /* If we're looking for an exact match, check that what we got
16576 is indeed an exact match. It might not be if some template
16577 parameters are used in non-deduced contexts. But don't check
16578 for an exact match if we have dependent template arguments;
16579 in that case we're doing partial ordering, and we already know
16580 that we have two candidates that will provide the actual type. */
16581 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
16582 {
16583 tree substed = TREE_TYPE (decl);
16584 unsigned int i;
16585
16586 tree sarg
16587 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
16588 if (return_type)
16589 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
16590 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
16591 if (!same_type_p (args[i], TREE_VALUE (sarg)))
16592 {
16593 unify_type_mismatch (explain_p, args[i],
16594 TREE_VALUE (sarg));
16595 goto fail;
16596 }
16597 }
16598
16599 r = decl;
16600
16601 fail:
16602 --deduction_depth;
16603 if (excessive_deduction_depth)
16604 {
16605 if (deduction_depth == 0)
16606 /* Reset once we're all the way out. */
16607 excessive_deduction_depth = false;
16608 }
16609
16610 /* We can't free this if a pending_template entry or last_error_tinst_level
16611 is pointing at it. */
16612 if (last_pending_template == old_last_pend
16613 && last_error_tinst_level == old_error_tinst)
16614 ggc_free (tinst);
16615
16616 return r;
16617 }
16618
16619 /* Adjust types before performing type deduction, as described in
16620 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
16621 sections are symmetric. PARM is the type of a function parameter
16622 or the return type of the conversion function. ARG is the type of
16623 the argument passed to the call, or the type of the value
16624 initialized with the result of the conversion function.
16625 ARG_EXPR is the original argument expression, which may be null. */
16626
16627 static int
16628 maybe_adjust_types_for_deduction (unification_kind_t strict,
16629 tree* parm,
16630 tree* arg,
16631 tree arg_expr)
16632 {
16633 int result = 0;
16634
16635 switch (strict)
16636 {
16637 case DEDUCE_CALL:
16638 break;
16639
16640 case DEDUCE_CONV:
16641 /* Swap PARM and ARG throughout the remainder of this
16642 function; the handling is precisely symmetric since PARM
16643 will initialize ARG rather than vice versa. */
16644 std::swap (parm, arg);
16645 break;
16646
16647 case DEDUCE_EXACT:
16648 /* Core issue #873: Do the DR606 thing (see below) for these cases,
16649 too, but here handle it by stripping the reference from PARM
16650 rather than by adding it to ARG. */
16651 if (TREE_CODE (*parm) == REFERENCE_TYPE
16652 && TYPE_REF_IS_RVALUE (*parm)
16653 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16654 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16655 && TREE_CODE (*arg) == REFERENCE_TYPE
16656 && !TYPE_REF_IS_RVALUE (*arg))
16657 *parm = TREE_TYPE (*parm);
16658 /* Nothing else to do in this case. */
16659 return 0;
16660
16661 default:
16662 gcc_unreachable ();
16663 }
16664
16665 if (TREE_CODE (*parm) != REFERENCE_TYPE)
16666 {
16667 /* [temp.deduct.call]
16668
16669 If P is not a reference type:
16670
16671 --If A is an array type, the pointer type produced by the
16672 array-to-pointer standard conversion (_conv.array_) is
16673 used in place of A for type deduction; otherwise,
16674
16675 --If A is a function type, the pointer type produced by
16676 the function-to-pointer standard conversion
16677 (_conv.func_) is used in place of A for type deduction;
16678 otherwise,
16679
16680 --If A is a cv-qualified type, the top level
16681 cv-qualifiers of A's type are ignored for type
16682 deduction. */
16683 if (TREE_CODE (*arg) == ARRAY_TYPE)
16684 *arg = build_pointer_type (TREE_TYPE (*arg));
16685 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
16686 *arg = build_pointer_type (*arg);
16687 else
16688 *arg = TYPE_MAIN_VARIANT (*arg);
16689 }
16690
16691 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
16692 of the form T&&, where T is a template parameter, and the argument
16693 is an lvalue, T is deduced as A& */
16694 if (TREE_CODE (*parm) == REFERENCE_TYPE
16695 && TYPE_REF_IS_RVALUE (*parm)
16696 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
16697 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
16698 && (arg_expr ? real_lvalue_p (arg_expr)
16699 /* try_one_overload doesn't provide an arg_expr, but
16700 functions are always lvalues. */
16701 : TREE_CODE (*arg) == FUNCTION_TYPE))
16702 *arg = build_reference_type (*arg);
16703
16704 /* [temp.deduct.call]
16705
16706 If P is a cv-qualified type, the top level cv-qualifiers
16707 of P's type are ignored for type deduction. If P is a
16708 reference type, the type referred to by P is used for
16709 type deduction. */
16710 *parm = TYPE_MAIN_VARIANT (*parm);
16711 if (TREE_CODE (*parm) == REFERENCE_TYPE)
16712 {
16713 *parm = TREE_TYPE (*parm);
16714 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16715 }
16716
16717 /* DR 322. For conversion deduction, remove a reference type on parm
16718 too (which has been swapped into ARG). */
16719 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
16720 *arg = TREE_TYPE (*arg);
16721
16722 return result;
16723 }
16724
16725 /* Subroutine of unify_one_argument. PARM is a function parameter of a
16726 template which does contain any deducible template parameters; check if
16727 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
16728 unify_one_argument. */
16729
16730 static int
16731 check_non_deducible_conversion (tree parm, tree arg, int strict,
16732 int flags, bool explain_p)
16733 {
16734 tree type;
16735
16736 if (!TYPE_P (arg))
16737 type = TREE_TYPE (arg);
16738 else
16739 type = arg;
16740
16741 if (same_type_p (parm, type))
16742 return unify_success (explain_p);
16743
16744 if (strict == DEDUCE_CONV)
16745 {
16746 if (can_convert_arg (type, parm, NULL_TREE, flags,
16747 explain_p ? tf_warning_or_error : tf_none))
16748 return unify_success (explain_p);
16749 }
16750 else if (strict != DEDUCE_EXACT)
16751 {
16752 if (can_convert_arg (parm, type,
16753 TYPE_P (arg) ? NULL_TREE : arg,
16754 flags, explain_p ? tf_warning_or_error : tf_none))
16755 return unify_success (explain_p);
16756 }
16757
16758 if (strict == DEDUCE_EXACT)
16759 return unify_type_mismatch (explain_p, parm, arg);
16760 else
16761 return unify_arg_conversion (explain_p, parm, type, arg);
16762 }
16763
16764 static bool uses_deducible_template_parms (tree type);
16765
16766 /* Returns true iff the expression EXPR is one from which a template
16767 argument can be deduced. In other words, if it's an undecorated
16768 use of a template non-type parameter. */
16769
16770 static bool
16771 deducible_expression (tree expr)
16772 {
16773 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
16774 }
16775
16776 /* Returns true iff the array domain DOMAIN uses a template parameter in a
16777 deducible way; that is, if it has a max value of <PARM> - 1. */
16778
16779 static bool
16780 deducible_array_bound (tree domain)
16781 {
16782 if (domain == NULL_TREE)
16783 return false;
16784
16785 tree max = TYPE_MAX_VALUE (domain);
16786 if (TREE_CODE (max) != MINUS_EXPR)
16787 return false;
16788
16789 return deducible_expression (TREE_OPERAND (max, 0));
16790 }
16791
16792 /* Returns true iff the template arguments ARGS use a template parameter
16793 in a deducible way. */
16794
16795 static bool
16796 deducible_template_args (tree args)
16797 {
16798 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
16799 {
16800 bool deducible;
16801 tree elt = TREE_VEC_ELT (args, i);
16802 if (ARGUMENT_PACK_P (elt))
16803 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
16804 else
16805 {
16806 if (PACK_EXPANSION_P (elt))
16807 elt = PACK_EXPANSION_PATTERN (elt);
16808 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
16809 deducible = true;
16810 else if (TYPE_P (elt))
16811 deducible = uses_deducible_template_parms (elt);
16812 else
16813 deducible = deducible_expression (elt);
16814 }
16815 if (deducible)
16816 return true;
16817 }
16818 return false;
16819 }
16820
16821 /* Returns true iff TYPE contains any deducible references to template
16822 parameters, as per 14.8.2.5. */
16823
16824 static bool
16825 uses_deducible_template_parms (tree type)
16826 {
16827 if (PACK_EXPANSION_P (type))
16828 type = PACK_EXPANSION_PATTERN (type);
16829
16830 /* T
16831 cv-list T
16832 TT<T>
16833 TT<i>
16834 TT<> */
16835 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16836 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16837 return true;
16838
16839 /* T*
16840 T&
16841 T&& */
16842 if (POINTER_TYPE_P (type))
16843 return uses_deducible_template_parms (TREE_TYPE (type));
16844
16845 /* T[integer-constant ]
16846 type [i] */
16847 if (TREE_CODE (type) == ARRAY_TYPE)
16848 return (uses_deducible_template_parms (TREE_TYPE (type))
16849 || deducible_array_bound (TYPE_DOMAIN (type)));
16850
16851 /* T type ::*
16852 type T::*
16853 T T::*
16854 T (type ::*)()
16855 type (T::*)()
16856 type (type ::*)(T)
16857 type (T::*)(T)
16858 T (type ::*)(T)
16859 T (T::*)()
16860 T (T::*)(T) */
16861 if (TYPE_PTRMEM_P (type))
16862 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16863 || (uses_deducible_template_parms
16864 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16865
16866 /* template-name <T> (where template-name refers to a class template)
16867 template-name <i> (where template-name refers to a class template) */
16868 if (CLASS_TYPE_P (type)
16869 && CLASSTYPE_TEMPLATE_INFO (type)
16870 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16871 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16872 (CLASSTYPE_TI_ARGS (type)));
16873
16874 /* type (T)
16875 T()
16876 T(T) */
16877 if (TREE_CODE (type) == FUNCTION_TYPE
16878 || TREE_CODE (type) == METHOD_TYPE)
16879 {
16880 if (uses_deducible_template_parms (TREE_TYPE (type)))
16881 return true;
16882 tree parm = TYPE_ARG_TYPES (type);
16883 if (TREE_CODE (type) == METHOD_TYPE)
16884 parm = TREE_CHAIN (parm);
16885 for (; parm; parm = TREE_CHAIN (parm))
16886 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16887 return true;
16888 }
16889
16890 return false;
16891 }
16892
16893 /* Subroutine of type_unification_real and unify_pack_expansion to
16894 handle unification of a single P/A pair. Parameters are as
16895 for those functions. */
16896
16897 static int
16898 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16899 int subr, unification_kind_t strict,
16900 bool explain_p)
16901 {
16902 tree arg_expr = NULL_TREE;
16903 int arg_strict;
16904
16905 if (arg == error_mark_node || parm == error_mark_node)
16906 return unify_invalid (explain_p);
16907 if (arg == unknown_type_node)
16908 /* We can't deduce anything from this, but we might get all the
16909 template args from other function args. */
16910 return unify_success (explain_p);
16911
16912 /* Implicit conversions (Clause 4) will be performed on a function
16913 argument to convert it to the type of the corresponding function
16914 parameter if the parameter type contains no template-parameters that
16915 participate in template argument deduction. */
16916 if (strict != DEDUCE_EXACT
16917 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16918 /* For function parameters with no deducible template parameters,
16919 just return. We'll check non-dependent conversions later. */
16920 return unify_success (explain_p);
16921
16922 switch (strict)
16923 {
16924 case DEDUCE_CALL:
16925 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16926 | UNIFY_ALLOW_MORE_CV_QUAL
16927 | UNIFY_ALLOW_DERIVED);
16928 break;
16929
16930 case DEDUCE_CONV:
16931 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16932 break;
16933
16934 case DEDUCE_EXACT:
16935 arg_strict = UNIFY_ALLOW_NONE;
16936 break;
16937
16938 default:
16939 gcc_unreachable ();
16940 }
16941
16942 /* We only do these transformations if this is the top-level
16943 parameter_type_list in a call or declaration matching; in other
16944 situations (nested function declarators, template argument lists) we
16945 won't be comparing a type to an expression, and we don't do any type
16946 adjustments. */
16947 if (!subr)
16948 {
16949 if (!TYPE_P (arg))
16950 {
16951 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16952 if (type_unknown_p (arg))
16953 {
16954 /* [temp.deduct.type] A template-argument can be
16955 deduced from a pointer to function or pointer
16956 to member function argument if the set of
16957 overloaded functions does not contain function
16958 templates and at most one of a set of
16959 overloaded functions provides a unique
16960 match. */
16961
16962 if (resolve_overloaded_unification
16963 (tparms, targs, parm, arg, strict,
16964 arg_strict, explain_p))
16965 return unify_success (explain_p);
16966 return unify_overload_resolution_failure (explain_p, arg);
16967 }
16968
16969 arg_expr = arg;
16970 arg = unlowered_expr_type (arg);
16971 if (arg == error_mark_node)
16972 return unify_invalid (explain_p);
16973 }
16974
16975 arg_strict |=
16976 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16977 }
16978 else
16979 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16980 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
16981 return unify_template_argument_mismatch (explain_p, parm, arg);
16982
16983 /* For deduction from an init-list we need the actual list. */
16984 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16985 arg = arg_expr;
16986 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16987 }
16988
16989 /* Most parms like fn_type_unification.
16990
16991 If SUBR is 1, we're being called recursively (to unify the
16992 arguments of a function or method parameter of a function
16993 template).
16994
16995 CHECKS is a pointer to a vector of access checks encountered while
16996 substituting default template arguments. */
16997
16998 static int
16999 type_unification_real (tree tparms,
17000 tree targs,
17001 tree xparms,
17002 const tree *xargs,
17003 unsigned int xnargs,
17004 int subr,
17005 unification_kind_t strict,
17006 int flags,
17007 vec<deferred_access_check, va_gc> **checks,
17008 bool explain_p)
17009 {
17010 tree parm, arg;
17011 int i;
17012 int ntparms = TREE_VEC_LENGTH (tparms);
17013 int saw_undeduced = 0;
17014 tree parms;
17015 const tree *args;
17016 unsigned int nargs;
17017 unsigned int ia;
17018
17019 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
17020 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
17021 gcc_assert (ntparms > 0);
17022
17023 /* Reset the number of non-defaulted template arguments contained
17024 in TARGS. */
17025 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
17026
17027 again:
17028 parms = xparms;
17029 args = xargs;
17030 nargs = xnargs;
17031
17032 ia = 0;
17033 while (parms && parms != void_list_node
17034 && ia < nargs)
17035 {
17036 parm = TREE_VALUE (parms);
17037
17038 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
17039 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
17040 /* For a function parameter pack that occurs at the end of the
17041 parameter-declaration-list, the type A of each remaining
17042 argument of the call is compared with the type P of the
17043 declarator-id of the function parameter pack. */
17044 break;
17045
17046 parms = TREE_CHAIN (parms);
17047
17048 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
17049 /* For a function parameter pack that does not occur at the
17050 end of the parameter-declaration-list, the type of the
17051 parameter pack is a non-deduced context. */
17052 continue;
17053
17054 arg = args[ia];
17055 ++ia;
17056
17057 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17058 explain_p))
17059 return 1;
17060 }
17061
17062 if (parms
17063 && parms != void_list_node
17064 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
17065 {
17066 /* Unify the remaining arguments with the pack expansion type. */
17067 tree argvec;
17068 tree parmvec = make_tree_vec (1);
17069
17070 /* Allocate a TREE_VEC and copy in all of the arguments */
17071 argvec = make_tree_vec (nargs - ia);
17072 for (i = 0; ia < nargs; ++ia, ++i)
17073 TREE_VEC_ELT (argvec, i) = args[ia];
17074
17075 /* Copy the parameter into parmvec. */
17076 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
17077 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
17078 /*subr=*/subr, explain_p))
17079 return 1;
17080
17081 /* Advance to the end of the list of parameters. */
17082 parms = TREE_CHAIN (parms);
17083 }
17084
17085 /* Fail if we've reached the end of the parm list, and more args
17086 are present, and the parm list isn't variadic. */
17087 if (ia < nargs && parms == void_list_node)
17088 return unify_too_many_arguments (explain_p, nargs, ia);
17089 /* Fail if parms are left and they don't have default values and
17090 they aren't all deduced as empty packs (c++/57397). This is
17091 consistent with sufficient_parms_p. */
17092 if (parms && parms != void_list_node
17093 && TREE_PURPOSE (parms) == NULL_TREE)
17094 {
17095 unsigned int count = nargs;
17096 tree p = parms;
17097 bool type_pack_p;
17098 do
17099 {
17100 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
17101 if (!type_pack_p)
17102 count++;
17103 p = TREE_CHAIN (p);
17104 }
17105 while (p && p != void_list_node);
17106 if (count != nargs)
17107 return unify_too_few_arguments (explain_p, ia, count,
17108 type_pack_p);
17109 }
17110
17111 if (!subr)
17112 {
17113 tsubst_flags_t complain = (explain_p
17114 ? tf_warning_or_error
17115 : tf_none);
17116
17117 for (i = 0; i < ntparms; i++)
17118 {
17119 tree targ = TREE_VEC_ELT (targs, i);
17120 tree tparm = TREE_VEC_ELT (tparms, i);
17121
17122 /* Clear the "incomplete" flags on all argument packs now so that
17123 substituting them into later default arguments works. */
17124 if (targ && ARGUMENT_PACK_P (targ))
17125 {
17126 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
17127 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
17128 }
17129
17130 if (targ || tparm == error_mark_node)
17131 continue;
17132 tparm = TREE_VALUE (tparm);
17133
17134 /* If this is an undeduced nontype parameter that depends on
17135 a type parameter, try another pass; its type may have been
17136 deduced from a later argument than the one from which
17137 this parameter can be deduced. */
17138 if (TREE_CODE (tparm) == PARM_DECL
17139 && uses_template_parms (TREE_TYPE (tparm))
17140 && saw_undeduced < 2)
17141 {
17142 saw_undeduced = 1;
17143 continue;
17144 }
17145
17146 /* Core issue #226 (C++0x) [temp.deduct]:
17147
17148 If a template argument has not been deduced, its
17149 default template argument, if any, is used.
17150
17151 When we are in C++98 mode, TREE_PURPOSE will either
17152 be NULL_TREE or ERROR_MARK_NODE, so we do not need
17153 to explicitly check cxx_dialect here. */
17154 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
17155 /* OK, there is a default argument. Wait until after the
17156 conversion check to do substitution. */
17157 continue;
17158
17159 /* If the type parameter is a parameter pack, then it will
17160 be deduced to an empty parameter pack. */
17161 if (template_parameter_pack_p (tparm))
17162 {
17163 tree arg;
17164
17165 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
17166 {
17167 arg = make_node (NONTYPE_ARGUMENT_PACK);
17168 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
17169 TREE_CONSTANT (arg) = 1;
17170 }
17171 else
17172 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
17173
17174 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
17175
17176 TREE_VEC_ELT (targs, i) = arg;
17177 continue;
17178 }
17179
17180 return unify_parameter_deduction_failure (explain_p, tparm);
17181 }
17182
17183 /* DR 1391: All parameters have args, now check non-dependent parms for
17184 convertibility. */
17185 if (saw_undeduced < 2)
17186 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
17187 parms && parms != void_list_node && ia < nargs; )
17188 {
17189 parm = TREE_VALUE (parms);
17190
17191 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
17192 && (!TREE_CHAIN (parms)
17193 || TREE_CHAIN (parms) == void_list_node))
17194 /* For a function parameter pack that occurs at the end of the
17195 parameter-declaration-list, the type A of each remaining
17196 argument of the call is compared with the type P of the
17197 declarator-id of the function parameter pack. */
17198 break;
17199
17200 parms = TREE_CHAIN (parms);
17201
17202 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
17203 /* For a function parameter pack that does not occur at the
17204 end of the parameter-declaration-list, the type of the
17205 parameter pack is a non-deduced context. */
17206 continue;
17207
17208 arg = args[ia];
17209 ++ia;
17210
17211 if (uses_template_parms (parm))
17212 continue;
17213 if (check_non_deducible_conversion (parm, arg, strict, flags,
17214 explain_p))
17215 return 1;
17216 }
17217
17218 /* Now substitute into the default template arguments. */
17219 for (i = 0; i < ntparms; i++)
17220 {
17221 tree targ = TREE_VEC_ELT (targs, i);
17222 tree tparm = TREE_VEC_ELT (tparms, i);
17223
17224 if (targ || tparm == error_mark_node)
17225 continue;
17226 tree parm = TREE_VALUE (tparm);
17227
17228 if (TREE_CODE (parm) == PARM_DECL
17229 && uses_template_parms (TREE_TYPE (parm))
17230 && saw_undeduced < 2)
17231 continue;
17232
17233 tree arg = TREE_PURPOSE (tparm);
17234 reopen_deferring_access_checks (*checks);
17235 location_t save_loc = input_location;
17236 if (DECL_P (parm))
17237 input_location = DECL_SOURCE_LOCATION (parm);
17238 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
17239 arg = convert_template_argument (parm, arg, targs, complain,
17240 i, NULL_TREE);
17241 input_location = save_loc;
17242 *checks = get_deferred_access_checks ();
17243 pop_deferring_access_checks ();
17244 if (arg == error_mark_node)
17245 return 1;
17246 else
17247 {
17248 TREE_VEC_ELT (targs, i) = arg;
17249 /* The position of the first default template argument,
17250 is also the number of non-defaulted arguments in TARGS.
17251 Record that. */
17252 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17253 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
17254 continue;
17255 }
17256 }
17257
17258 if (saw_undeduced++ == 1)
17259 goto again;
17260 }
17261 #ifdef ENABLE_CHECKING
17262 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
17263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
17264 #endif
17265
17266 return unify_success (explain_p);
17267 }
17268
17269 /* Subroutine of type_unification_real. Args are like the variables
17270 at the call site. ARG is an overloaded function (or template-id);
17271 we try deducing template args from each of the overloads, and if
17272 only one succeeds, we go with that. Modifies TARGS and returns
17273 true on success. */
17274
17275 static bool
17276 resolve_overloaded_unification (tree tparms,
17277 tree targs,
17278 tree parm,
17279 tree arg,
17280 unification_kind_t strict,
17281 int sub_strict,
17282 bool explain_p)
17283 {
17284 tree tempargs = copy_node (targs);
17285 int good = 0;
17286 tree goodfn = NULL_TREE;
17287 bool addr_p;
17288
17289 if (TREE_CODE (arg) == ADDR_EXPR)
17290 {
17291 arg = TREE_OPERAND (arg, 0);
17292 addr_p = true;
17293 }
17294 else
17295 addr_p = false;
17296
17297 if (TREE_CODE (arg) == COMPONENT_REF)
17298 /* Handle `&x' where `x' is some static or non-static member
17299 function name. */
17300 arg = TREE_OPERAND (arg, 1);
17301
17302 if (TREE_CODE (arg) == OFFSET_REF)
17303 arg = TREE_OPERAND (arg, 1);
17304
17305 /* Strip baselink information. */
17306 if (BASELINK_P (arg))
17307 arg = BASELINK_FUNCTIONS (arg);
17308
17309 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
17310 {
17311 /* If we got some explicit template args, we need to plug them into
17312 the affected templates before we try to unify, in case the
17313 explicit args will completely resolve the templates in question. */
17314
17315 int ok = 0;
17316 tree expl_subargs = TREE_OPERAND (arg, 1);
17317 arg = TREE_OPERAND (arg, 0);
17318
17319 for (; arg; arg = OVL_NEXT (arg))
17320 {
17321 tree fn = OVL_CURRENT (arg);
17322 tree subargs, elem;
17323
17324 if (TREE_CODE (fn) != TEMPLATE_DECL)
17325 continue;
17326
17327 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17328 expl_subargs, NULL_TREE, tf_none,
17329 /*require_all_args=*/true,
17330 /*use_default_args=*/true);
17331 if (subargs != error_mark_node
17332 && !any_dependent_template_arguments_p (subargs))
17333 {
17334 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
17335 if (try_one_overload (tparms, targs, tempargs, parm,
17336 elem, strict, sub_strict, addr_p, explain_p)
17337 && (!goodfn || !same_type_p (goodfn, elem)))
17338 {
17339 goodfn = elem;
17340 ++good;
17341 }
17342 }
17343 else if (subargs)
17344 ++ok;
17345 }
17346 /* If no templates (or more than one) are fully resolved by the
17347 explicit arguments, this template-id is a non-deduced context; it
17348 could still be OK if we deduce all template arguments for the
17349 enclosing call through other arguments. */
17350 if (good != 1)
17351 good = ok;
17352 }
17353 else if (TREE_CODE (arg) != OVERLOAD
17354 && TREE_CODE (arg) != FUNCTION_DECL)
17355 /* If ARG is, for example, "(0, &f)" then its type will be unknown
17356 -- but the deduction does not succeed because the expression is
17357 not just the function on its own. */
17358 return false;
17359 else
17360 for (; arg; arg = OVL_NEXT (arg))
17361 if (try_one_overload (tparms, targs, tempargs, parm,
17362 TREE_TYPE (OVL_CURRENT (arg)),
17363 strict, sub_strict, addr_p, explain_p)
17364 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
17365 {
17366 goodfn = OVL_CURRENT (arg);
17367 ++good;
17368 }
17369
17370 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17371 to function or pointer to member function argument if the set of
17372 overloaded functions does not contain function templates and at most
17373 one of a set of overloaded functions provides a unique match.
17374
17375 So if we found multiple possibilities, we return success but don't
17376 deduce anything. */
17377
17378 if (good == 1)
17379 {
17380 int i = TREE_VEC_LENGTH (targs);
17381 for (; i--; )
17382 if (TREE_VEC_ELT (tempargs, i))
17383 {
17384 tree old = TREE_VEC_ELT (targs, i);
17385 tree new_ = TREE_VEC_ELT (tempargs, i);
17386 if (new_ && old && ARGUMENT_PACK_P (old)
17387 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
17388 /* Don't forget explicit template arguments in a pack. */
17389 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
17390 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
17391 TREE_VEC_ELT (targs, i) = new_;
17392 }
17393 }
17394 if (good)
17395 return true;
17396
17397 return false;
17398 }
17399
17400 /* Core DR 115: In contexts where deduction is done and fails, or in
17401 contexts where deduction is not done, if a template argument list is
17402 specified and it, along with any default template arguments, identifies
17403 a single function template specialization, then the template-id is an
17404 lvalue for the function template specialization. */
17405
17406 tree
17407 resolve_nondeduced_context (tree orig_expr)
17408 {
17409 tree expr, offset, baselink;
17410 bool addr;
17411
17412 if (!type_unknown_p (orig_expr))
17413 return orig_expr;
17414
17415 expr = orig_expr;
17416 addr = false;
17417 offset = NULL_TREE;
17418 baselink = NULL_TREE;
17419
17420 if (TREE_CODE (expr) == ADDR_EXPR)
17421 {
17422 expr = TREE_OPERAND (expr, 0);
17423 addr = true;
17424 }
17425 if (TREE_CODE (expr) == OFFSET_REF)
17426 {
17427 offset = expr;
17428 expr = TREE_OPERAND (expr, 1);
17429 }
17430 if (BASELINK_P (expr))
17431 {
17432 baselink = expr;
17433 expr = BASELINK_FUNCTIONS (expr);
17434 }
17435
17436 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
17437 {
17438 int good = 0;
17439 tree goodfn = NULL_TREE;
17440
17441 /* If we got some explicit template args, we need to plug them into
17442 the affected templates before we try to unify, in case the
17443 explicit args will completely resolve the templates in question. */
17444
17445 tree expl_subargs = TREE_OPERAND (expr, 1);
17446 tree arg = TREE_OPERAND (expr, 0);
17447 tree badfn = NULL_TREE;
17448 tree badargs = NULL_TREE;
17449
17450 for (; arg; arg = OVL_NEXT (arg))
17451 {
17452 tree fn = OVL_CURRENT (arg);
17453 tree subargs, elem;
17454
17455 if (TREE_CODE (fn) != TEMPLATE_DECL)
17456 continue;
17457
17458 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
17459 expl_subargs, NULL_TREE, tf_none,
17460 /*require_all_args=*/true,
17461 /*use_default_args=*/true);
17462 if (subargs != error_mark_node
17463 && !any_dependent_template_arguments_p (subargs))
17464 {
17465 elem = instantiate_template (fn, subargs, tf_none);
17466 if (elem == error_mark_node)
17467 {
17468 badfn = fn;
17469 badargs = subargs;
17470 }
17471 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
17472 {
17473 goodfn = elem;
17474 ++good;
17475 }
17476 }
17477 }
17478 if (good == 1)
17479 {
17480 mark_used (goodfn);
17481 expr = goodfn;
17482 if (baselink)
17483 expr = build_baselink (BASELINK_BINFO (baselink),
17484 BASELINK_ACCESS_BINFO (baselink),
17485 expr, BASELINK_OPTYPE (baselink));
17486 if (offset)
17487 {
17488 tree base
17489 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
17490 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
17491 }
17492 if (addr)
17493 expr = cp_build_addr_expr (expr, tf_warning_or_error);
17494 return expr;
17495 }
17496 else if (good == 0 && badargs)
17497 /* There were no good options and at least one bad one, so let the
17498 user know what the problem is. */
17499 instantiate_template (badfn, badargs, tf_warning_or_error);
17500 }
17501 return orig_expr;
17502 }
17503
17504 /* Subroutine of resolve_overloaded_unification; does deduction for a single
17505 overload. Fills TARGS with any deduced arguments, or error_mark_node if
17506 different overloads deduce different arguments for a given parm.
17507 ADDR_P is true if the expression for which deduction is being
17508 performed was of the form "& fn" rather than simply "fn".
17509
17510 Returns 1 on success. */
17511
17512 static int
17513 try_one_overload (tree tparms,
17514 tree orig_targs,
17515 tree targs,
17516 tree parm,
17517 tree arg,
17518 unification_kind_t strict,
17519 int sub_strict,
17520 bool addr_p,
17521 bool explain_p)
17522 {
17523 int nargs;
17524 tree tempargs;
17525 int i;
17526
17527 if (arg == error_mark_node)
17528 return 0;
17529
17530 /* [temp.deduct.type] A template-argument can be deduced from a pointer
17531 to function or pointer to member function argument if the set of
17532 overloaded functions does not contain function templates and at most
17533 one of a set of overloaded functions provides a unique match.
17534
17535 So if this is a template, just return success. */
17536
17537 if (uses_template_parms (arg))
17538 return 1;
17539
17540 if (TREE_CODE (arg) == METHOD_TYPE)
17541 arg = build_ptrmemfunc_type (build_pointer_type (arg));
17542 else if (addr_p)
17543 arg = build_pointer_type (arg);
17544
17545 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
17546
17547 /* We don't copy orig_targs for this because if we have already deduced
17548 some template args from previous args, unify would complain when we
17549 try to deduce a template parameter for the same argument, even though
17550 there isn't really a conflict. */
17551 nargs = TREE_VEC_LENGTH (targs);
17552 tempargs = make_tree_vec (nargs);
17553
17554 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
17555 return 0;
17556
17557 /* First make sure we didn't deduce anything that conflicts with
17558 explicitly specified args. */
17559 for (i = nargs; i--; )
17560 {
17561 tree elt = TREE_VEC_ELT (tempargs, i);
17562 tree oldelt = TREE_VEC_ELT (orig_targs, i);
17563
17564 if (!elt)
17565 /*NOP*/;
17566 else if (uses_template_parms (elt))
17567 /* Since we're unifying against ourselves, we will fill in
17568 template args used in the function parm list with our own
17569 template parms. Discard them. */
17570 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
17571 else if (oldelt && !template_args_equal (oldelt, elt))
17572 return 0;
17573 }
17574
17575 for (i = nargs; i--; )
17576 {
17577 tree elt = TREE_VEC_ELT (tempargs, i);
17578
17579 if (elt)
17580 TREE_VEC_ELT (targs, i) = elt;
17581 }
17582
17583 return 1;
17584 }
17585
17586 /* PARM is a template class (perhaps with unbound template
17587 parameters). ARG is a fully instantiated type. If ARG can be
17588 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
17589 TARGS are as for unify. */
17590
17591 static tree
17592 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
17593 bool explain_p)
17594 {
17595 tree copy_of_targs;
17596
17597 if (!CLASSTYPE_TEMPLATE_INFO (arg)
17598 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
17599 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
17600 return NULL_TREE;
17601
17602 /* We need to make a new template argument vector for the call to
17603 unify. If we used TARGS, we'd clutter it up with the result of
17604 the attempted unification, even if this class didn't work out.
17605 We also don't want to commit ourselves to all the unifications
17606 we've already done, since unification is supposed to be done on
17607 an argument-by-argument basis. In other words, consider the
17608 following pathological case:
17609
17610 template <int I, int J, int K>
17611 struct S {};
17612
17613 template <int I, int J>
17614 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
17615
17616 template <int I, int J, int K>
17617 void f(S<I, J, K>, S<I, I, I>);
17618
17619 void g() {
17620 S<0, 0, 0> s0;
17621 S<0, 1, 2> s2;
17622
17623 f(s0, s2);
17624 }
17625
17626 Now, by the time we consider the unification involving `s2', we
17627 already know that we must have `f<0, 0, 0>'. But, even though
17628 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
17629 because there are two ways to unify base classes of S<0, 1, 2>
17630 with S<I, I, I>. If we kept the already deduced knowledge, we
17631 would reject the possibility I=1. */
17632 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
17633
17634 /* If unification failed, we're done. */
17635 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
17636 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
17637 return NULL_TREE;
17638
17639 return arg;
17640 }
17641
17642 /* Given a template type PARM and a class type ARG, find the unique
17643 base type in ARG that is an instance of PARM. We do not examine
17644 ARG itself; only its base-classes. If there is not exactly one
17645 appropriate base class, return NULL_TREE. PARM may be the type of
17646 a partial specialization, as well as a plain template type. Used
17647 by unify. */
17648
17649 static enum template_base_result
17650 get_template_base (tree tparms, tree targs, tree parm, tree arg,
17651 bool explain_p, tree *result)
17652 {
17653 tree rval = NULL_TREE;
17654 tree binfo;
17655
17656 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
17657
17658 binfo = TYPE_BINFO (complete_type (arg));
17659 if (!binfo)
17660 {
17661 /* The type could not be completed. */
17662 *result = NULL_TREE;
17663 return tbr_incomplete_type;
17664 }
17665
17666 /* Walk in inheritance graph order. The search order is not
17667 important, and this avoids multiple walks of virtual bases. */
17668 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
17669 {
17670 tree r = try_class_unification (tparms, targs, parm,
17671 BINFO_TYPE (binfo), explain_p);
17672
17673 if (r)
17674 {
17675 /* If there is more than one satisfactory baseclass, then:
17676
17677 [temp.deduct.call]
17678
17679 If they yield more than one possible deduced A, the type
17680 deduction fails.
17681
17682 applies. */
17683 if (rval && !same_type_p (r, rval))
17684 {
17685 *result = NULL_TREE;
17686 return tbr_ambiguous_baseclass;
17687 }
17688
17689 rval = r;
17690 }
17691 }
17692
17693 *result = rval;
17694 return tbr_success;
17695 }
17696
17697 /* Returns the level of DECL, which declares a template parameter. */
17698
17699 static int
17700 template_decl_level (tree decl)
17701 {
17702 switch (TREE_CODE (decl))
17703 {
17704 case TYPE_DECL:
17705 case TEMPLATE_DECL:
17706 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
17707
17708 case PARM_DECL:
17709 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
17710
17711 default:
17712 gcc_unreachable ();
17713 }
17714 return 0;
17715 }
17716
17717 /* Decide whether ARG can be unified with PARM, considering only the
17718 cv-qualifiers of each type, given STRICT as documented for unify.
17719 Returns nonzero iff the unification is OK on that basis. */
17720
17721 static int
17722 check_cv_quals_for_unify (int strict, tree arg, tree parm)
17723 {
17724 int arg_quals = cp_type_quals (arg);
17725 int parm_quals = cp_type_quals (parm);
17726
17727 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17728 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17729 {
17730 /* Although a CVR qualifier is ignored when being applied to a
17731 substituted template parameter ([8.3.2]/1 for example), that
17732 does not allow us to unify "const T" with "int&" because both
17733 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
17734 It is ok when we're allowing additional CV qualifiers
17735 at the outer level [14.8.2.1]/3,1st bullet. */
17736 if ((TREE_CODE (arg) == REFERENCE_TYPE
17737 || TREE_CODE (arg) == FUNCTION_TYPE
17738 || TREE_CODE (arg) == METHOD_TYPE)
17739 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
17740 return 0;
17741
17742 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
17743 && (parm_quals & TYPE_QUAL_RESTRICT))
17744 return 0;
17745 }
17746
17747 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
17748 && (arg_quals & parm_quals) != parm_quals)
17749 return 0;
17750
17751 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
17752 && (parm_quals & arg_quals) != arg_quals)
17753 return 0;
17754
17755 return 1;
17756 }
17757
17758 /* Determines the LEVEL and INDEX for the template parameter PARM. */
17759 void
17760 template_parm_level_and_index (tree parm, int* level, int* index)
17761 {
17762 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17763 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17764 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17765 {
17766 *index = TEMPLATE_TYPE_IDX (parm);
17767 *level = TEMPLATE_TYPE_LEVEL (parm);
17768 }
17769 else
17770 {
17771 *index = TEMPLATE_PARM_IDX (parm);
17772 *level = TEMPLATE_PARM_LEVEL (parm);
17773 }
17774 }
17775
17776 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
17777 do { \
17778 if (unify (TP, TA, P, A, S, EP)) \
17779 return 1; \
17780 } while (0);
17781
17782 /* Unifies the remaining arguments in PACKED_ARGS with the pack
17783 expansion at the end of PACKED_PARMS. Returns 0 if the type
17784 deduction succeeds, 1 otherwise. STRICT is the same as in
17785 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
17786 call argument list. We'll need to adjust the arguments to make them
17787 types. SUBR tells us if this is from a recursive call to
17788 type_unification_real, or for comparing two template argument
17789 lists. */
17790
17791 static int
17792 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
17793 tree packed_args, unification_kind_t strict,
17794 bool subr, bool explain_p)
17795 {
17796 tree parm
17797 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
17798 tree pattern = PACK_EXPANSION_PATTERN (parm);
17799 tree pack, packs = NULL_TREE;
17800 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
17801
17802 packed_args = expand_template_argument_pack (packed_args);
17803
17804 int len = TREE_VEC_LENGTH (packed_args);
17805
17806 /* Determine the parameter packs we will be deducing from the
17807 pattern, and record their current deductions. */
17808 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
17809 pack; pack = TREE_CHAIN (pack))
17810 {
17811 tree parm_pack = TREE_VALUE (pack);
17812 int idx, level;
17813
17814 /* Determine the index and level of this parameter pack. */
17815 template_parm_level_and_index (parm_pack, &level, &idx);
17816
17817 /* Keep track of the parameter packs and their corresponding
17818 argument packs. */
17819 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
17820 TREE_TYPE (packs) = make_tree_vec (len - start);
17821 }
17822
17823 /* Loop through all of the arguments that have not yet been
17824 unified and unify each with the pattern. */
17825 for (i = start; i < len; i++)
17826 {
17827 tree parm;
17828 bool any_explicit = false;
17829 tree arg = TREE_VEC_ELT (packed_args, i);
17830
17831 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
17832 or the element of its argument pack at the current index if
17833 this argument was explicitly specified. */
17834 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17835 {
17836 int idx, level;
17837 tree arg, pargs;
17838 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17839
17840 arg = NULL_TREE;
17841 if (TREE_VALUE (pack)
17842 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
17843 && (i - start < TREE_VEC_LENGTH (pargs)))
17844 {
17845 any_explicit = true;
17846 arg = TREE_VEC_ELT (pargs, i - start);
17847 }
17848 TMPL_ARG (targs, level, idx) = arg;
17849 }
17850
17851 /* If we had explicit template arguments, substitute them into the
17852 pattern before deduction. */
17853 if (any_explicit)
17854 {
17855 /* Some arguments might still be unspecified or dependent. */
17856 bool dependent;
17857 ++processing_template_decl;
17858 dependent = any_dependent_template_arguments_p (targs);
17859 if (!dependent)
17860 --processing_template_decl;
17861 parm = tsubst (pattern, targs,
17862 explain_p ? tf_warning_or_error : tf_none,
17863 NULL_TREE);
17864 if (dependent)
17865 --processing_template_decl;
17866 if (parm == error_mark_node)
17867 return 1;
17868 }
17869 else
17870 parm = pattern;
17871
17872 /* Unify the pattern with the current argument. */
17873 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
17874 explain_p))
17875 return 1;
17876
17877 /* For each parameter pack, collect the deduced value. */
17878 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17879 {
17880 int idx, level;
17881 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17882
17883 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
17884 TMPL_ARG (targs, level, idx);
17885 }
17886 }
17887
17888 /* Verify that the results of unification with the parameter packs
17889 produce results consistent with what we've seen before, and make
17890 the deduced argument packs available. */
17891 for (pack = packs; pack; pack = TREE_CHAIN (pack))
17892 {
17893 tree old_pack = TREE_VALUE (pack);
17894 tree new_args = TREE_TYPE (pack);
17895 int i, len = TREE_VEC_LENGTH (new_args);
17896 int idx, level;
17897 bool nondeduced_p = false;
17898
17899 /* By default keep the original deduced argument pack.
17900 If necessary, more specific code is going to update the
17901 resulting deduced argument later down in this function. */
17902 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
17903 TMPL_ARG (targs, level, idx) = old_pack;
17904
17905 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
17906 actually deduce anything. */
17907 for (i = 0; i < len && !nondeduced_p; ++i)
17908 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
17909 nondeduced_p = true;
17910 if (nondeduced_p)
17911 continue;
17912
17913 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17914 {
17915 /* If we had fewer function args than explicit template args,
17916 just use the explicits. */
17917 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17918 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17919 if (len < explicit_len)
17920 new_args = explicit_args;
17921 }
17922
17923 if (!old_pack)
17924 {
17925 tree result;
17926 /* Build the deduced *_ARGUMENT_PACK. */
17927 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17928 {
17929 result = make_node (NONTYPE_ARGUMENT_PACK);
17930 TREE_TYPE (result) =
17931 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17932 TREE_CONSTANT (result) = 1;
17933 }
17934 else
17935 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17936
17937 SET_ARGUMENT_PACK_ARGS (result, new_args);
17938
17939 /* Note the deduced argument packs for this parameter
17940 pack. */
17941 TMPL_ARG (targs, level, idx) = result;
17942 }
17943 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17944 && (ARGUMENT_PACK_ARGS (old_pack)
17945 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17946 {
17947 /* We only had the explicitly-provided arguments before, but
17948 now we have a complete set of arguments. */
17949 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17950
17951 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17952 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17953 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17954 }
17955 else
17956 {
17957 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17958 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17959
17960 if (!comp_template_args_with_info (old_args, new_args,
17961 &bad_old_arg, &bad_new_arg))
17962 /* Inconsistent unification of this parameter pack. */
17963 return unify_parameter_pack_inconsistent (explain_p,
17964 bad_old_arg,
17965 bad_new_arg);
17966 }
17967 }
17968
17969 return unify_success (explain_p);
17970 }
17971
17972 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17973 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17974 parameters and return value are as for unify. */
17975
17976 static int
17977 unify_array_domain (tree tparms, tree targs,
17978 tree parm_dom, tree arg_dom,
17979 bool explain_p)
17980 {
17981 tree parm_max;
17982 tree arg_max;
17983 bool parm_cst;
17984 bool arg_cst;
17985
17986 /* Our representation of array types uses "N - 1" as the
17987 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17988 not an integer constant. We cannot unify arbitrarily
17989 complex expressions, so we eliminate the MINUS_EXPRs
17990 here. */
17991 parm_max = TYPE_MAX_VALUE (parm_dom);
17992 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17993 if (!parm_cst)
17994 {
17995 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17996 parm_max = TREE_OPERAND (parm_max, 0);
17997 }
17998 arg_max = TYPE_MAX_VALUE (arg_dom);
17999 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
18000 if (!arg_cst)
18001 {
18002 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
18003 trying to unify the type of a variable with the type
18004 of a template parameter. For example:
18005
18006 template <unsigned int N>
18007 void f (char (&) [N]);
18008 int g();
18009 void h(int i) {
18010 char a[g(i)];
18011 f(a);
18012 }
18013
18014 Here, the type of the ARG will be "int [g(i)]", and
18015 may be a SAVE_EXPR, etc. */
18016 if (TREE_CODE (arg_max) != MINUS_EXPR)
18017 return unify_vla_arg (explain_p, arg_dom);
18018 arg_max = TREE_OPERAND (arg_max, 0);
18019 }
18020
18021 /* If only one of the bounds used a MINUS_EXPR, compensate
18022 by adding one to the other bound. */
18023 if (parm_cst && !arg_cst)
18024 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
18025 integer_type_node,
18026 parm_max,
18027 integer_one_node);
18028 else if (arg_cst && !parm_cst)
18029 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
18030 integer_type_node,
18031 arg_max,
18032 integer_one_node);
18033
18034 return unify (tparms, targs, parm_max, arg_max,
18035 UNIFY_ALLOW_INTEGER, explain_p);
18036 }
18037
18038 /* Deduce the value of template parameters. TPARMS is the (innermost)
18039 set of template parameters to a template. TARGS is the bindings
18040 for those template parameters, as determined thus far; TARGS may
18041 include template arguments for outer levels of template parameters
18042 as well. PARM is a parameter to a template function, or a
18043 subcomponent of that parameter; ARG is the corresponding argument.
18044 This function attempts to match PARM with ARG in a manner
18045 consistent with the existing assignments in TARGS. If more values
18046 are deduced, then TARGS is updated.
18047
18048 Returns 0 if the type deduction succeeds, 1 otherwise. The
18049 parameter STRICT is a bitwise or of the following flags:
18050
18051 UNIFY_ALLOW_NONE:
18052 Require an exact match between PARM and ARG.
18053 UNIFY_ALLOW_MORE_CV_QUAL:
18054 Allow the deduced ARG to be more cv-qualified (by qualification
18055 conversion) than ARG.
18056 UNIFY_ALLOW_LESS_CV_QUAL:
18057 Allow the deduced ARG to be less cv-qualified than ARG.
18058 UNIFY_ALLOW_DERIVED:
18059 Allow the deduced ARG to be a template base class of ARG,
18060 or a pointer to a template base class of the type pointed to by
18061 ARG.
18062 UNIFY_ALLOW_INTEGER:
18063 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
18064 case for more information.
18065 UNIFY_ALLOW_OUTER_LEVEL:
18066 This is the outermost level of a deduction. Used to determine validity
18067 of qualification conversions. A valid qualification conversion must
18068 have const qualified pointers leading up to the inner type which
18069 requires additional CV quals, except at the outer level, where const
18070 is not required [conv.qual]. It would be normal to set this flag in
18071 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
18072 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
18073 This is the outermost level of a deduction, and PARM can be more CV
18074 qualified at this point.
18075 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
18076 This is the outermost level of a deduction, and PARM can be less CV
18077 qualified at this point. */
18078
18079 static int
18080 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
18081 bool explain_p)
18082 {
18083 int idx;
18084 tree targ;
18085 tree tparm;
18086 int strict_in = strict;
18087
18088 /* I don't think this will do the right thing with respect to types.
18089 But the only case I've seen it in so far has been array bounds, where
18090 signedness is the only information lost, and I think that will be
18091 okay. */
18092 while (TREE_CODE (parm) == NOP_EXPR)
18093 parm = TREE_OPERAND (parm, 0);
18094
18095 if (arg == error_mark_node)
18096 return unify_invalid (explain_p);
18097 if (arg == unknown_type_node
18098 || arg == init_list_type_node)
18099 /* We can't deduce anything from this, but we might get all the
18100 template args from other function args. */
18101 return unify_success (explain_p);
18102
18103 /* If PARM uses template parameters, then we can't bail out here,
18104 even if ARG == PARM, since we won't record unifications for the
18105 template parameters. We might need them if we're trying to
18106 figure out which of two things is more specialized. */
18107 if (arg == parm && !uses_template_parms (parm))
18108 return unify_success (explain_p);
18109
18110 /* Handle init lists early, so the rest of the function can assume
18111 we're dealing with a type. */
18112 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
18113 {
18114 tree elt, elttype;
18115 unsigned i;
18116 tree orig_parm = parm;
18117
18118 /* Replace T with std::initializer_list<T> for deduction. */
18119 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18120 && flag_deduce_init_list)
18121 parm = listify (parm);
18122
18123 if (!is_std_init_list (parm)
18124 && TREE_CODE (parm) != ARRAY_TYPE)
18125 /* We can only deduce from an initializer list argument if the
18126 parameter is std::initializer_list or an array; otherwise this
18127 is a non-deduced context. */
18128 return unify_success (explain_p);
18129
18130 if (TREE_CODE (parm) == ARRAY_TYPE)
18131 elttype = TREE_TYPE (parm);
18132 else
18133 {
18134 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
18135 /* Deduction is defined in terms of a single type, so just punt
18136 on the (bizarre) std::initializer_list<T...>. */
18137 if (PACK_EXPANSION_P (elttype))
18138 return unify_success (explain_p);
18139 }
18140
18141 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
18142 {
18143 int elt_strict = strict;
18144
18145 if (elt == error_mark_node)
18146 return unify_invalid (explain_p);
18147
18148 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
18149 {
18150 tree type = TREE_TYPE (elt);
18151 if (type == error_mark_node)
18152 return unify_invalid (explain_p);
18153 /* It should only be possible to get here for a call. */
18154 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
18155 elt_strict |= maybe_adjust_types_for_deduction
18156 (DEDUCE_CALL, &elttype, &type, elt);
18157 elt = type;
18158 }
18159
18160 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
18161 explain_p);
18162 }
18163
18164 if (TREE_CODE (parm) == ARRAY_TYPE
18165 && deducible_array_bound (TYPE_DOMAIN (parm)))
18166 {
18167 /* Also deduce from the length of the initializer list. */
18168 tree max = size_int (CONSTRUCTOR_NELTS (arg));
18169 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
18170 if (idx == error_mark_node)
18171 return unify_invalid (explain_p);
18172 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18173 idx, explain_p);
18174 }
18175
18176 /* If the std::initializer_list<T> deduction worked, replace the
18177 deduced A with std::initializer_list<A>. */
18178 if (orig_parm != parm)
18179 {
18180 idx = TEMPLATE_TYPE_IDX (orig_parm);
18181 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18182 targ = listify (targ);
18183 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
18184 }
18185 return unify_success (explain_p);
18186 }
18187
18188 /* Immediately reject some pairs that won't unify because of
18189 cv-qualification mismatches. */
18190 if (TREE_CODE (arg) == TREE_CODE (parm)
18191 && TYPE_P (arg)
18192 /* It is the elements of the array which hold the cv quals of an array
18193 type, and the elements might be template type parms. We'll check
18194 when we recurse. */
18195 && TREE_CODE (arg) != ARRAY_TYPE
18196 /* We check the cv-qualifiers when unifying with template type
18197 parameters below. We want to allow ARG `const T' to unify with
18198 PARM `T' for example, when computing which of two templates
18199 is more specialized, for example. */
18200 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
18201 && !check_cv_quals_for_unify (strict_in, arg, parm))
18202 return unify_cv_qual_mismatch (explain_p, parm, arg);
18203
18204 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
18205 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
18206 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
18207 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
18208 strict &= ~UNIFY_ALLOW_DERIVED;
18209 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18210 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
18211
18212 switch (TREE_CODE (parm))
18213 {
18214 case TYPENAME_TYPE:
18215 case SCOPE_REF:
18216 case UNBOUND_CLASS_TEMPLATE:
18217 /* In a type which contains a nested-name-specifier, template
18218 argument values cannot be deduced for template parameters used
18219 within the nested-name-specifier. */
18220 return unify_success (explain_p);
18221
18222 case TEMPLATE_TYPE_PARM:
18223 case TEMPLATE_TEMPLATE_PARM:
18224 case BOUND_TEMPLATE_TEMPLATE_PARM:
18225 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18226 if (error_operand_p (tparm))
18227 return unify_invalid (explain_p);
18228
18229 if (TEMPLATE_TYPE_LEVEL (parm)
18230 != template_decl_level (tparm))
18231 /* The PARM is not one we're trying to unify. Just check
18232 to see if it matches ARG. */
18233 {
18234 if (TREE_CODE (arg) == TREE_CODE (parm)
18235 && (is_auto (parm) ? is_auto (arg)
18236 : same_type_p (parm, arg)))
18237 return unify_success (explain_p);
18238 else
18239 return unify_type_mismatch (explain_p, parm, arg);
18240 }
18241 idx = TEMPLATE_TYPE_IDX (parm);
18242 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18243 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
18244 if (error_operand_p (tparm))
18245 return unify_invalid (explain_p);
18246
18247 /* Check for mixed types and values. */
18248 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
18249 && TREE_CODE (tparm) != TYPE_DECL)
18250 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18251 && TREE_CODE (tparm) != TEMPLATE_DECL))
18252 gcc_unreachable ();
18253
18254 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18255 {
18256 /* ARG must be constructed from a template class or a template
18257 template parameter. */
18258 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
18259 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
18260 return unify_template_deduction_failure (explain_p, parm, arg);
18261 {
18262 tree parmvec = TYPE_TI_ARGS (parm);
18263 /* An alias template name is never deduced. */
18264 if (TYPE_ALIAS_P (arg))
18265 arg = strip_typedefs (arg);
18266 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
18267 tree full_argvec = add_to_template_args (targs, argvec);
18268 tree parm_parms
18269 = DECL_INNERMOST_TEMPLATE_PARMS
18270 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
18271 int i, len;
18272 int parm_variadic_p = 0;
18273
18274 /* The resolution to DR150 makes clear that default
18275 arguments for an N-argument may not be used to bind T
18276 to a template template parameter with fewer than N
18277 parameters. It is not safe to permit the binding of
18278 default arguments as an extension, as that may change
18279 the meaning of a conforming program. Consider:
18280
18281 struct Dense { static const unsigned int dim = 1; };
18282
18283 template <template <typename> class View,
18284 typename Block>
18285 void operator+(float, View<Block> const&);
18286
18287 template <typename Block,
18288 unsigned int Dim = Block::dim>
18289 struct Lvalue_proxy { operator float() const; };
18290
18291 void
18292 test_1d (void) {
18293 Lvalue_proxy<Dense> p;
18294 float b;
18295 b + p;
18296 }
18297
18298 Here, if Lvalue_proxy is permitted to bind to View, then
18299 the global operator+ will be used; if they are not, the
18300 Lvalue_proxy will be converted to float. */
18301 if (coerce_template_parms (parm_parms,
18302 full_argvec,
18303 TYPE_TI_TEMPLATE (parm),
18304 (explain_p
18305 ? tf_warning_or_error
18306 : tf_none),
18307 /*require_all_args=*/true,
18308 /*use_default_args=*/false)
18309 == error_mark_node)
18310 return 1;
18311
18312 /* Deduce arguments T, i from TT<T> or TT<i>.
18313 We check each element of PARMVEC and ARGVEC individually
18314 rather than the whole TREE_VEC since they can have
18315 different number of elements. */
18316
18317 parmvec = expand_template_argument_pack (parmvec);
18318 argvec = expand_template_argument_pack (argvec);
18319
18320 len = TREE_VEC_LENGTH (parmvec);
18321
18322 /* Check if the parameters end in a pack, making them
18323 variadic. */
18324 if (len > 0
18325 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
18326 parm_variadic_p = 1;
18327
18328 for (i = 0; i < len - parm_variadic_p; ++i)
18329 /* If the template argument list of P contains a pack
18330 expansion that is not the last template argument, the
18331 entire template argument list is a non-deduced
18332 context. */
18333 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
18334 return unify_success (explain_p);
18335
18336 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
18337 return unify_too_few_arguments (explain_p,
18338 TREE_VEC_LENGTH (argvec), len);
18339
18340 for (i = 0; i < len - parm_variadic_p; ++i)
18341 {
18342 RECUR_AND_CHECK_FAILURE (tparms, targs,
18343 TREE_VEC_ELT (parmvec, i),
18344 TREE_VEC_ELT (argvec, i),
18345 UNIFY_ALLOW_NONE, explain_p);
18346 }
18347
18348 if (parm_variadic_p
18349 && unify_pack_expansion (tparms, targs,
18350 parmvec, argvec,
18351 DEDUCE_EXACT,
18352 /*subr=*/true, explain_p))
18353 return 1;
18354 }
18355 arg = TYPE_TI_TEMPLATE (arg);
18356
18357 /* Fall through to deduce template name. */
18358 }
18359
18360 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
18361 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
18362 {
18363 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
18364
18365 /* Simple cases: Value already set, does match or doesn't. */
18366 if (targ != NULL_TREE && template_args_equal (targ, arg))
18367 return unify_success (explain_p);
18368 else if (targ)
18369 return unify_inconsistency (explain_p, parm, targ, arg);
18370 }
18371 else
18372 {
18373 /* If PARM is `const T' and ARG is only `int', we don't have
18374 a match unless we are allowing additional qualification.
18375 If ARG is `const int' and PARM is just `T' that's OK;
18376 that binds `const int' to `T'. */
18377 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
18378 arg, parm))
18379 return unify_cv_qual_mismatch (explain_p, parm, arg);
18380
18381 /* Consider the case where ARG is `const volatile int' and
18382 PARM is `const T'. Then, T should be `volatile int'. */
18383 arg = cp_build_qualified_type_real
18384 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
18385 if (arg == error_mark_node)
18386 return unify_invalid (explain_p);
18387
18388 /* Simple cases: Value already set, does match or doesn't. */
18389 if (targ != NULL_TREE && same_type_p (targ, arg))
18390 return unify_success (explain_p);
18391 else if (targ)
18392 return unify_inconsistency (explain_p, parm, targ, arg);
18393
18394 /* Make sure that ARG is not a variable-sized array. (Note
18395 that were talking about variable-sized arrays (like
18396 `int[n]'), rather than arrays of unknown size (like
18397 `int[]').) We'll get very confused by such a type since
18398 the bound of the array is not constant, and therefore
18399 not mangleable. Besides, such types are not allowed in
18400 ISO C++, so we can do as we please here. We do allow
18401 them for 'auto' deduction, since that isn't ABI-exposed. */
18402 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
18403 return unify_vla_arg (explain_p, arg);
18404
18405 /* Strip typedefs as in convert_template_argument. */
18406 arg = canonicalize_type_argument (arg, tf_none);
18407 }
18408
18409 /* If ARG is a parameter pack or an expansion, we cannot unify
18410 against it unless PARM is also a parameter pack. */
18411 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18412 && !template_parameter_pack_p (parm))
18413 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18414
18415 /* If the argument deduction results is a METHOD_TYPE,
18416 then there is a problem.
18417 METHOD_TYPE doesn't map to any real C++ type the result of
18418 the deduction can not be of that type. */
18419 if (TREE_CODE (arg) == METHOD_TYPE)
18420 return unify_method_type_error (explain_p, arg);
18421
18422 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18423 return unify_success (explain_p);
18424
18425 case TEMPLATE_PARM_INDEX:
18426 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
18427 if (error_operand_p (tparm))
18428 return unify_invalid (explain_p);
18429
18430 if (TEMPLATE_PARM_LEVEL (parm)
18431 != template_decl_level (tparm))
18432 {
18433 /* The PARM is not one we're trying to unify. Just check
18434 to see if it matches ARG. */
18435 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
18436 && cp_tree_equal (parm, arg));
18437 if (result)
18438 unify_expression_unequal (explain_p, parm, arg);
18439 return result;
18440 }
18441
18442 idx = TEMPLATE_PARM_IDX (parm);
18443 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
18444
18445 if (targ)
18446 {
18447 int x = !cp_tree_equal (targ, arg);
18448 if (x)
18449 unify_inconsistency (explain_p, parm, targ, arg);
18450 return x;
18451 }
18452
18453 /* [temp.deduct.type] If, in the declaration of a function template
18454 with a non-type template-parameter, the non-type
18455 template-parameter is used in an expression in the function
18456 parameter-list and, if the corresponding template-argument is
18457 deduced, the template-argument type shall match the type of the
18458 template-parameter exactly, except that a template-argument
18459 deduced from an array bound may be of any integral type.
18460 The non-type parameter might use already deduced type parameters. */
18461 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
18462 if (!TREE_TYPE (arg))
18463 /* Template-parameter dependent expression. Just accept it for now.
18464 It will later be processed in convert_template_argument. */
18465 ;
18466 else if (same_type_p (TREE_TYPE (arg), tparm))
18467 /* OK */;
18468 else if ((strict & UNIFY_ALLOW_INTEGER)
18469 && CP_INTEGRAL_TYPE_P (tparm))
18470 /* Convert the ARG to the type of PARM; the deduced non-type
18471 template argument must exactly match the types of the
18472 corresponding parameter. */
18473 arg = fold (build_nop (tparm, arg));
18474 else if (uses_template_parms (tparm))
18475 /* We haven't deduced the type of this parameter yet. Try again
18476 later. */
18477 return unify_success (explain_p);
18478 else
18479 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
18480
18481 /* If ARG is a parameter pack or an expansion, we cannot unify
18482 against it unless PARM is also a parameter pack. */
18483 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
18484 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
18485 return unify_parameter_pack_mismatch (explain_p, parm, arg);
18486
18487 {
18488 bool removed_attr = false;
18489 arg = strip_typedefs_expr (arg, &removed_attr);
18490 }
18491 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
18492 return unify_success (explain_p);
18493
18494 case PTRMEM_CST:
18495 {
18496 /* A pointer-to-member constant can be unified only with
18497 another constant. */
18498 if (TREE_CODE (arg) != PTRMEM_CST)
18499 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
18500
18501 /* Just unify the class member. It would be useless (and possibly
18502 wrong, depending on the strict flags) to unify also
18503 PTRMEM_CST_CLASS, because we want to be sure that both parm and
18504 arg refer to the same variable, even if through different
18505 classes. For instance:
18506
18507 struct A { int x; };
18508 struct B : A { };
18509
18510 Unification of &A::x and &B::x must succeed. */
18511 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
18512 PTRMEM_CST_MEMBER (arg), strict, explain_p);
18513 }
18514
18515 case POINTER_TYPE:
18516 {
18517 if (!TYPE_PTR_P (arg))
18518 return unify_type_mismatch (explain_p, parm, arg);
18519
18520 /* [temp.deduct.call]
18521
18522 A can be another pointer or pointer to member type that can
18523 be converted to the deduced A via a qualification
18524 conversion (_conv.qual_).
18525
18526 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
18527 This will allow for additional cv-qualification of the
18528 pointed-to types if appropriate. */
18529
18530 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
18531 /* The derived-to-base conversion only persists through one
18532 level of pointers. */
18533 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
18534
18535 return unify (tparms, targs, TREE_TYPE (parm),
18536 TREE_TYPE (arg), strict, explain_p);
18537 }
18538
18539 case REFERENCE_TYPE:
18540 if (TREE_CODE (arg) != REFERENCE_TYPE)
18541 return unify_type_mismatch (explain_p, parm, arg);
18542 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18543 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18544
18545 case ARRAY_TYPE:
18546 if (TREE_CODE (arg) != ARRAY_TYPE)
18547 return unify_type_mismatch (explain_p, parm, arg);
18548 if ((TYPE_DOMAIN (parm) == NULL_TREE)
18549 != (TYPE_DOMAIN (arg) == NULL_TREE))
18550 return unify_type_mismatch (explain_p, parm, arg);
18551 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18552 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
18553 if (TYPE_DOMAIN (parm) != NULL_TREE)
18554 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
18555 TYPE_DOMAIN (arg), explain_p);
18556 return unify_success (explain_p);
18557
18558 case REAL_TYPE:
18559 case COMPLEX_TYPE:
18560 case VECTOR_TYPE:
18561 case INTEGER_TYPE:
18562 case BOOLEAN_TYPE:
18563 case ENUMERAL_TYPE:
18564 case VOID_TYPE:
18565 case NULLPTR_TYPE:
18566 if (TREE_CODE (arg) != TREE_CODE (parm))
18567 return unify_type_mismatch (explain_p, parm, arg);
18568
18569 /* We have already checked cv-qualification at the top of the
18570 function. */
18571 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
18572 return unify_type_mismatch (explain_p, parm, arg);
18573
18574 /* As far as unification is concerned, this wins. Later checks
18575 will invalidate it if necessary. */
18576 return unify_success (explain_p);
18577
18578 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
18579 /* Type INTEGER_CST can come from ordinary constant template args. */
18580 case INTEGER_CST:
18581 while (TREE_CODE (arg) == NOP_EXPR)
18582 arg = TREE_OPERAND (arg, 0);
18583
18584 if (TREE_CODE (arg) != INTEGER_CST)
18585 return unify_template_argument_mismatch (explain_p, parm, arg);
18586 return (tree_int_cst_equal (parm, arg)
18587 ? unify_success (explain_p)
18588 : unify_template_argument_mismatch (explain_p, parm, arg));
18589
18590 case TREE_VEC:
18591 {
18592 int i, len, argslen;
18593 int parm_variadic_p = 0;
18594
18595 if (TREE_CODE (arg) != TREE_VEC)
18596 return unify_template_argument_mismatch (explain_p, parm, arg);
18597
18598 len = TREE_VEC_LENGTH (parm);
18599 argslen = TREE_VEC_LENGTH (arg);
18600
18601 /* Check for pack expansions in the parameters. */
18602 for (i = 0; i < len; ++i)
18603 {
18604 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
18605 {
18606 if (i == len - 1)
18607 /* We can unify against something with a trailing
18608 parameter pack. */
18609 parm_variadic_p = 1;
18610 else
18611 /* [temp.deduct.type]/9: If the template argument list of
18612 P contains a pack expansion that is not the last
18613 template argument, the entire template argument list
18614 is a non-deduced context. */
18615 return unify_success (explain_p);
18616 }
18617 }
18618
18619 /* If we don't have enough arguments to satisfy the parameters
18620 (not counting the pack expression at the end), or we have
18621 too many arguments for a parameter list that doesn't end in
18622 a pack expression, we can't unify. */
18623 if (parm_variadic_p
18624 ? argslen < len - parm_variadic_p
18625 : argslen != len)
18626 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
18627
18628 /* Unify all of the parameters that precede the (optional)
18629 pack expression. */
18630 for (i = 0; i < len - parm_variadic_p; ++i)
18631 {
18632 RECUR_AND_CHECK_FAILURE (tparms, targs,
18633 TREE_VEC_ELT (parm, i),
18634 TREE_VEC_ELT (arg, i),
18635 UNIFY_ALLOW_NONE, explain_p);
18636 }
18637 if (parm_variadic_p)
18638 return unify_pack_expansion (tparms, targs, parm, arg,
18639 DEDUCE_EXACT,
18640 /*subr=*/true, explain_p);
18641 return unify_success (explain_p);
18642 }
18643
18644 case RECORD_TYPE:
18645 case UNION_TYPE:
18646 if (TREE_CODE (arg) != TREE_CODE (parm))
18647 return unify_type_mismatch (explain_p, parm, arg);
18648
18649 if (TYPE_PTRMEMFUNC_P (parm))
18650 {
18651 if (!TYPE_PTRMEMFUNC_P (arg))
18652 return unify_type_mismatch (explain_p, parm, arg);
18653
18654 return unify (tparms, targs,
18655 TYPE_PTRMEMFUNC_FN_TYPE (parm),
18656 TYPE_PTRMEMFUNC_FN_TYPE (arg),
18657 strict, explain_p);
18658 }
18659 else if (TYPE_PTRMEMFUNC_P (arg))
18660 return unify_type_mismatch (explain_p, parm, arg);
18661
18662 if (CLASSTYPE_TEMPLATE_INFO (parm))
18663 {
18664 tree t = NULL_TREE;
18665
18666 if (strict_in & UNIFY_ALLOW_DERIVED)
18667 {
18668 /* First, we try to unify the PARM and ARG directly. */
18669 t = try_class_unification (tparms, targs,
18670 parm, arg, explain_p);
18671
18672 if (!t)
18673 {
18674 /* Fallback to the special case allowed in
18675 [temp.deduct.call]:
18676
18677 If P is a class, and P has the form
18678 template-id, then A can be a derived class of
18679 the deduced A. Likewise, if P is a pointer to
18680 a class of the form template-id, A can be a
18681 pointer to a derived class pointed to by the
18682 deduced A. */
18683 enum template_base_result r;
18684 r = get_template_base (tparms, targs, parm, arg,
18685 explain_p, &t);
18686
18687 if (!t)
18688 return unify_no_common_base (explain_p, r, parm, arg);
18689 }
18690 }
18691 else if (CLASSTYPE_TEMPLATE_INFO (arg)
18692 && (CLASSTYPE_TI_TEMPLATE (parm)
18693 == CLASSTYPE_TI_TEMPLATE (arg)))
18694 /* Perhaps PARM is something like S<U> and ARG is S<int>.
18695 Then, we should unify `int' and `U'. */
18696 t = arg;
18697 else
18698 /* There's no chance of unification succeeding. */
18699 return unify_type_mismatch (explain_p, parm, arg);
18700
18701 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
18702 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
18703 }
18704 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
18705 return unify_type_mismatch (explain_p, parm, arg);
18706 return unify_success (explain_p);
18707
18708 case METHOD_TYPE:
18709 case FUNCTION_TYPE:
18710 {
18711 unsigned int nargs;
18712 tree *args;
18713 tree a;
18714 unsigned int i;
18715
18716 if (TREE_CODE (arg) != TREE_CODE (parm))
18717 return unify_type_mismatch (explain_p, parm, arg);
18718
18719 /* CV qualifications for methods can never be deduced, they must
18720 match exactly. We need to check them explicitly here,
18721 because type_unification_real treats them as any other
18722 cv-qualified parameter. */
18723 if (TREE_CODE (parm) == METHOD_TYPE
18724 && (!check_cv_quals_for_unify
18725 (UNIFY_ALLOW_NONE,
18726 class_of_this_parm (arg),
18727 class_of_this_parm (parm))))
18728 return unify_cv_qual_mismatch (explain_p, parm, arg);
18729
18730 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
18731 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
18732
18733 nargs = list_length (TYPE_ARG_TYPES (arg));
18734 args = XALLOCAVEC (tree, nargs);
18735 for (a = TYPE_ARG_TYPES (arg), i = 0;
18736 a != NULL_TREE && a != void_list_node;
18737 a = TREE_CHAIN (a), ++i)
18738 args[i] = TREE_VALUE (a);
18739 nargs = i;
18740
18741 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
18742 args, nargs, 1, DEDUCE_EXACT,
18743 LOOKUP_NORMAL, NULL, explain_p);
18744 }
18745
18746 case OFFSET_TYPE:
18747 /* Unify a pointer to member with a pointer to member function, which
18748 deduces the type of the member as a function type. */
18749 if (TYPE_PTRMEMFUNC_P (arg))
18750 {
18751 /* Check top-level cv qualifiers */
18752 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
18753 return unify_cv_qual_mismatch (explain_p, parm, arg);
18754
18755 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18756 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
18757 UNIFY_ALLOW_NONE, explain_p);
18758
18759 /* Determine the type of the function we are unifying against. */
18760 tree fntype = static_fn_type (arg);
18761
18762 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
18763 }
18764
18765 if (TREE_CODE (arg) != OFFSET_TYPE)
18766 return unify_type_mismatch (explain_p, parm, arg);
18767 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
18768 TYPE_OFFSET_BASETYPE (arg),
18769 UNIFY_ALLOW_NONE, explain_p);
18770 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
18771 strict, explain_p);
18772
18773 case CONST_DECL:
18774 if (DECL_TEMPLATE_PARM_P (parm))
18775 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
18776 if (arg != scalar_constant_value (parm))
18777 return unify_template_argument_mismatch (explain_p, parm, arg);
18778 return unify_success (explain_p);
18779
18780 case FIELD_DECL:
18781 case TEMPLATE_DECL:
18782 /* Matched cases are handled by the ARG == PARM test above. */
18783 return unify_template_argument_mismatch (explain_p, parm, arg);
18784
18785 case VAR_DECL:
18786 /* A non-type template parameter that is a variable should be a
18787 an integral constant, in which case, it whould have been
18788 folded into its (constant) value. So we should not be getting
18789 a variable here. */
18790 gcc_unreachable ();
18791
18792 case TYPE_ARGUMENT_PACK:
18793 case NONTYPE_ARGUMENT_PACK:
18794 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
18795 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
18796
18797 case TYPEOF_TYPE:
18798 case DECLTYPE_TYPE:
18799 case UNDERLYING_TYPE:
18800 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
18801 or UNDERLYING_TYPE nodes. */
18802 return unify_success (explain_p);
18803
18804 case ERROR_MARK:
18805 /* Unification fails if we hit an error node. */
18806 return unify_invalid (explain_p);
18807
18808 case INDIRECT_REF:
18809 if (REFERENCE_REF_P (parm))
18810 {
18811 if (REFERENCE_REF_P (arg))
18812 arg = TREE_OPERAND (arg, 0);
18813 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
18814 strict, explain_p);
18815 }
18816 /* FALLTHRU */
18817
18818 default:
18819 /* An unresolved overload is a nondeduced context. */
18820 if (is_overloaded_fn (parm) || type_unknown_p (parm))
18821 return unify_success (explain_p);
18822 gcc_assert (EXPR_P (parm));
18823
18824 /* We must be looking at an expression. This can happen with
18825 something like:
18826
18827 template <int I>
18828 void foo(S<I>, S<I + 2>);
18829
18830 This is a "nondeduced context":
18831
18832 [deduct.type]
18833
18834 The nondeduced contexts are:
18835
18836 --A type that is a template-id in which one or more of
18837 the template-arguments is an expression that references
18838 a template-parameter.
18839
18840 In these cases, we assume deduction succeeded, but don't
18841 actually infer any unifications. */
18842
18843 if (!uses_template_parms (parm)
18844 && !template_args_equal (parm, arg))
18845 return unify_expression_unequal (explain_p, parm, arg);
18846 else
18847 return unify_success (explain_p);
18848 }
18849 }
18850 #undef RECUR_AND_CHECK_FAILURE
18851 \f
18852 /* Note that DECL can be defined in this translation unit, if
18853 required. */
18854
18855 static void
18856 mark_definable (tree decl)
18857 {
18858 tree clone;
18859 DECL_NOT_REALLY_EXTERN (decl) = 1;
18860 FOR_EACH_CLONE (clone, decl)
18861 DECL_NOT_REALLY_EXTERN (clone) = 1;
18862 }
18863
18864 /* Called if RESULT is explicitly instantiated, or is a member of an
18865 explicitly instantiated class. */
18866
18867 void
18868 mark_decl_instantiated (tree result, int extern_p)
18869 {
18870 SET_DECL_EXPLICIT_INSTANTIATION (result);
18871
18872 /* If this entity has already been written out, it's too late to
18873 make any modifications. */
18874 if (TREE_ASM_WRITTEN (result))
18875 return;
18876
18877 /* For anonymous namespace we don't need to do anything. */
18878 if (decl_anon_ns_mem_p (result))
18879 {
18880 gcc_assert (!TREE_PUBLIC (result));
18881 return;
18882 }
18883
18884 if (TREE_CODE (result) != FUNCTION_DECL)
18885 /* The TREE_PUBLIC flag for function declarations will have been
18886 set correctly by tsubst. */
18887 TREE_PUBLIC (result) = 1;
18888
18889 /* This might have been set by an earlier implicit instantiation. */
18890 DECL_COMDAT (result) = 0;
18891
18892 if (extern_p)
18893 DECL_NOT_REALLY_EXTERN (result) = 0;
18894 else
18895 {
18896 mark_definable (result);
18897 mark_needed (result);
18898 /* Always make artificials weak. */
18899 if (DECL_ARTIFICIAL (result) && flag_weak)
18900 comdat_linkage (result);
18901 /* For WIN32 we also want to put explicit instantiations in
18902 linkonce sections. */
18903 else if (TREE_PUBLIC (result))
18904 maybe_make_one_only (result);
18905 }
18906
18907 /* If EXTERN_P, then this function will not be emitted -- unless
18908 followed by an explicit instantiation, at which point its linkage
18909 will be adjusted. If !EXTERN_P, then this function will be
18910 emitted here. In neither circumstance do we want
18911 import_export_decl to adjust the linkage. */
18912 DECL_INTERFACE_KNOWN (result) = 1;
18913 }
18914
18915 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
18916 important template arguments. If any are missing, we check whether
18917 they're important by using error_mark_node for substituting into any
18918 args that were used for partial ordering (the ones between ARGS and END)
18919 and seeing if it bubbles up. */
18920
18921 static bool
18922 check_undeduced_parms (tree targs, tree args, tree end)
18923 {
18924 bool found = false;
18925 int i;
18926 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
18927 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
18928 {
18929 found = true;
18930 TREE_VEC_ELT (targs, i) = error_mark_node;
18931 }
18932 if (found)
18933 {
18934 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
18935 if (substed == error_mark_node)
18936 return true;
18937 }
18938 return false;
18939 }
18940
18941 /* Given two function templates PAT1 and PAT2, return:
18942
18943 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18944 -1 if PAT2 is more specialized than PAT1.
18945 0 if neither is more specialized.
18946
18947 LEN indicates the number of parameters we should consider
18948 (defaulted parameters should not be considered).
18949
18950 The 1998 std underspecified function template partial ordering, and
18951 DR214 addresses the issue. We take pairs of arguments, one from
18952 each of the templates, and deduce them against each other. One of
18953 the templates will be more specialized if all the *other*
18954 template's arguments deduce against its arguments and at least one
18955 of its arguments *does* *not* deduce against the other template's
18956 corresponding argument. Deduction is done as for class templates.
18957 The arguments used in deduction have reference and top level cv
18958 qualifiers removed. Iff both arguments were originally reference
18959 types *and* deduction succeeds in both directions, an lvalue reference
18960 wins against an rvalue reference and otherwise the template
18961 with the more cv-qualified argument wins for that pairing (if
18962 neither is more cv-qualified, they both are equal). Unlike regular
18963 deduction, after all the arguments have been deduced in this way,
18964 we do *not* verify the deduced template argument values can be
18965 substituted into non-deduced contexts.
18966
18967 The logic can be a bit confusing here, because we look at deduce1 and
18968 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18969 can find template arguments for pat1 to make arg1 look like arg2, that
18970 means that arg2 is at least as specialized as arg1. */
18971
18972 int
18973 more_specialized_fn (tree pat1, tree pat2, int len)
18974 {
18975 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18976 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18977 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18978 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18979 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18980 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18981 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18982 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18983 tree origs1, origs2;
18984 bool lose1 = false;
18985 bool lose2 = false;
18986
18987 /* Remove the this parameter from non-static member functions. If
18988 one is a non-static member function and the other is not a static
18989 member function, remove the first parameter from that function
18990 also. This situation occurs for operator functions where we
18991 locate both a member function (with this pointer) and non-member
18992 operator (with explicit first operand). */
18993 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18994 {
18995 len--; /* LEN is the number of significant arguments for DECL1 */
18996 args1 = TREE_CHAIN (args1);
18997 if (!DECL_STATIC_FUNCTION_P (decl2))
18998 args2 = TREE_CHAIN (args2);
18999 }
19000 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
19001 {
19002 args2 = TREE_CHAIN (args2);
19003 if (!DECL_STATIC_FUNCTION_P (decl1))
19004 {
19005 len--;
19006 args1 = TREE_CHAIN (args1);
19007 }
19008 }
19009
19010 /* If only one is a conversion operator, they are unordered. */
19011 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
19012 return 0;
19013
19014 /* Consider the return type for a conversion function */
19015 if (DECL_CONV_FN_P (decl1))
19016 {
19017 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
19018 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
19019 len++;
19020 }
19021
19022 processing_template_decl++;
19023
19024 origs1 = args1;
19025 origs2 = args2;
19026
19027 while (len--
19028 /* Stop when an ellipsis is seen. */
19029 && args1 != NULL_TREE && args2 != NULL_TREE)
19030 {
19031 tree arg1 = TREE_VALUE (args1);
19032 tree arg2 = TREE_VALUE (args2);
19033 int deduce1, deduce2;
19034 int quals1 = -1;
19035 int quals2 = -1;
19036 int ref1 = 0;
19037 int ref2 = 0;
19038
19039 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
19040 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19041 {
19042 /* When both arguments are pack expansions, we need only
19043 unify the patterns themselves. */
19044 arg1 = PACK_EXPANSION_PATTERN (arg1);
19045 arg2 = PACK_EXPANSION_PATTERN (arg2);
19046
19047 /* This is the last comparison we need to do. */
19048 len = 0;
19049 }
19050
19051 if (TREE_CODE (arg1) == REFERENCE_TYPE)
19052 {
19053 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
19054 arg1 = TREE_TYPE (arg1);
19055 quals1 = cp_type_quals (arg1);
19056 }
19057
19058 if (TREE_CODE (arg2) == REFERENCE_TYPE)
19059 {
19060 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
19061 arg2 = TREE_TYPE (arg2);
19062 quals2 = cp_type_quals (arg2);
19063 }
19064
19065 arg1 = TYPE_MAIN_VARIANT (arg1);
19066 arg2 = TYPE_MAIN_VARIANT (arg2);
19067
19068 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
19069 {
19070 int i, len2 = list_length (args2);
19071 tree parmvec = make_tree_vec (1);
19072 tree argvec = make_tree_vec (len2);
19073 tree ta = args2;
19074
19075 /* Setup the parameter vector, which contains only ARG1. */
19076 TREE_VEC_ELT (parmvec, 0) = arg1;
19077
19078 /* Setup the argument vector, which contains the remaining
19079 arguments. */
19080 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
19081 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
19082
19083 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
19084 argvec, DEDUCE_EXACT,
19085 /*subr=*/true, /*explain_p=*/false)
19086 == 0);
19087
19088 /* We cannot deduce in the other direction, because ARG1 is
19089 a pack expansion but ARG2 is not. */
19090 deduce2 = 0;
19091 }
19092 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19093 {
19094 int i, len1 = list_length (args1);
19095 tree parmvec = make_tree_vec (1);
19096 tree argvec = make_tree_vec (len1);
19097 tree ta = args1;
19098
19099 /* Setup the parameter vector, which contains only ARG1. */
19100 TREE_VEC_ELT (parmvec, 0) = arg2;
19101
19102 /* Setup the argument vector, which contains the remaining
19103 arguments. */
19104 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
19105 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
19106
19107 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
19108 argvec, DEDUCE_EXACT,
19109 /*subr=*/true, /*explain_p=*/false)
19110 == 0);
19111
19112 /* We cannot deduce in the other direction, because ARG2 is
19113 a pack expansion but ARG1 is not.*/
19114 deduce1 = 0;
19115 }
19116
19117 else
19118 {
19119 /* The normal case, where neither argument is a pack
19120 expansion. */
19121 deduce1 = (unify (tparms1, targs1, arg1, arg2,
19122 UNIFY_ALLOW_NONE, /*explain_p=*/false)
19123 == 0);
19124 deduce2 = (unify (tparms2, targs2, arg2, arg1,
19125 UNIFY_ALLOW_NONE, /*explain_p=*/false)
19126 == 0);
19127 }
19128
19129 /* If we couldn't deduce arguments for tparms1 to make arg1 match
19130 arg2, then arg2 is not as specialized as arg1. */
19131 if (!deduce1)
19132 lose2 = true;
19133 if (!deduce2)
19134 lose1 = true;
19135
19136 /* "If, for a given type, deduction succeeds in both directions
19137 (i.e., the types are identical after the transformations above)
19138 and both P and A were reference types (before being replaced with
19139 the type referred to above):
19140 - if the type from the argument template was an lvalue reference and
19141 the type from the parameter template was not, the argument type is
19142 considered to be more specialized than the other; otherwise,
19143 - if the type from the argument template is more cv-qualified
19144 than the type from the parameter template (as described above),
19145 the argument type is considered to be more specialized than the other;
19146 otherwise,
19147 - neither type is more specialized than the other." */
19148
19149 if (deduce1 && deduce2)
19150 {
19151 if (ref1 && ref2 && ref1 != ref2)
19152 {
19153 if (ref1 > ref2)
19154 lose1 = true;
19155 else
19156 lose2 = true;
19157 }
19158 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
19159 {
19160 if ((quals1 & quals2) == quals2)
19161 lose2 = true;
19162 if ((quals1 & quals2) == quals1)
19163 lose1 = true;
19164 }
19165 }
19166
19167 if (lose1 && lose2)
19168 /* We've failed to deduce something in either direction.
19169 These must be unordered. */
19170 break;
19171
19172 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
19173 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
19174 /* We have already processed all of the arguments in our
19175 handing of the pack expansion type. */
19176 len = 0;
19177
19178 args1 = TREE_CHAIN (args1);
19179 args2 = TREE_CHAIN (args2);
19180 }
19181
19182 /* "In most cases, all template parameters must have values in order for
19183 deduction to succeed, but for partial ordering purposes a template
19184 parameter may remain without a value provided it is not used in the
19185 types being used for partial ordering."
19186
19187 Thus, if we are missing any of the targs1 we need to substitute into
19188 origs1, then pat2 is not as specialized as pat1. This can happen when
19189 there is a nondeduced context. */
19190 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
19191 lose2 = true;
19192 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
19193 lose1 = true;
19194
19195 processing_template_decl--;
19196
19197 /* All things being equal, if the next argument is a pack expansion
19198 for one function but not for the other, prefer the
19199 non-variadic function. FIXME this is bogus; see c++/41958. */
19200 if (lose1 == lose2
19201 && args1 && TREE_VALUE (args1)
19202 && args2 && TREE_VALUE (args2))
19203 {
19204 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
19205 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
19206 }
19207
19208 if (lose1 == lose2)
19209 return 0;
19210 else if (!lose1)
19211 return 1;
19212 else
19213 return -1;
19214 }
19215
19216 /* Determine which of two partial specializations of TMPL is more
19217 specialized.
19218
19219 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
19220 to the first partial specialization. The TREE_PURPOSE is the
19221 innermost set of template parameters for the partial
19222 specialization. PAT2 is similar, but for the second template.
19223
19224 Return 1 if the first partial specialization is more specialized;
19225 -1 if the second is more specialized; 0 if neither is more
19226 specialized.
19227
19228 See [temp.class.order] for information about determining which of
19229 two templates is more specialized. */
19230
19231 static int
19232 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
19233 {
19234 tree targs;
19235 int winner = 0;
19236 bool any_deductions = false;
19237
19238 tree tmpl1 = TREE_VALUE (pat1);
19239 tree tmpl2 = TREE_VALUE (pat2);
19240 tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
19241 tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
19242 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
19243 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
19244
19245 /* Just like what happens for functions, if we are ordering between
19246 different template specializations, we may encounter dependent
19247 types in the arguments, and we need our dependency check functions
19248 to behave correctly. */
19249 ++processing_template_decl;
19250 targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
19251 if (targs)
19252 {
19253 --winner;
19254 any_deductions = true;
19255 }
19256
19257 targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
19258 if (targs)
19259 {
19260 ++winner;
19261 any_deductions = true;
19262 }
19263 --processing_template_decl;
19264
19265 /* In the case of a tie where at least one of the templates
19266 has a parameter pack at the end, the template with the most
19267 non-packed parameters wins. */
19268 if (winner == 0
19269 && any_deductions
19270 && (template_args_variadic_p (TREE_PURPOSE (pat1))
19271 || template_args_variadic_p (TREE_PURPOSE (pat2))))
19272 {
19273 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
19274 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
19275 int len1 = TREE_VEC_LENGTH (args1);
19276 int len2 = TREE_VEC_LENGTH (args2);
19277
19278 /* We don't count the pack expansion at the end. */
19279 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
19280 --len1;
19281 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
19282 --len2;
19283
19284 if (len1 > len2)
19285 return 1;
19286 else if (len1 < len2)
19287 return -1;
19288 }
19289
19290 return winner;
19291 }
19292
19293 /* Return the template arguments that will produce the function signature
19294 DECL from the function template FN, with the explicit template
19295 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
19296 also match. Return NULL_TREE if no satisfactory arguments could be
19297 found. */
19298
19299 static tree
19300 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
19301 {
19302 int ntparms = DECL_NTPARMS (fn);
19303 tree targs = make_tree_vec (ntparms);
19304 tree decl_type = TREE_TYPE (decl);
19305 tree decl_arg_types;
19306 tree *args;
19307 unsigned int nargs, ix;
19308 tree arg;
19309
19310 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
19311
19312 /* Never do unification on the 'this' parameter. */
19313 decl_arg_types = skip_artificial_parms_for (decl,
19314 TYPE_ARG_TYPES (decl_type));
19315
19316 nargs = list_length (decl_arg_types);
19317 args = XALLOCAVEC (tree, nargs);
19318 for (arg = decl_arg_types, ix = 0;
19319 arg != NULL_TREE && arg != void_list_node;
19320 arg = TREE_CHAIN (arg), ++ix)
19321 args[ix] = TREE_VALUE (arg);
19322
19323 if (fn_type_unification (fn, explicit_args, targs,
19324 args, ix,
19325 (check_rettype || DECL_CONV_FN_P (fn)
19326 ? TREE_TYPE (decl_type) : NULL_TREE),
19327 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
19328 /*decltype*/false)
19329 == error_mark_node)
19330 return NULL_TREE;
19331
19332 return targs;
19333 }
19334
19335 /* Return the innermost template arguments that, when applied to a partial
19336 specialization of TMPL whose innermost template parameters are
19337 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
19338 ARGS.
19339
19340 For example, suppose we have:
19341
19342 template <class T, class U> struct S {};
19343 template <class T> struct S<T*, int> {};
19344
19345 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
19346 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
19347 int}. The resulting vector will be {double}, indicating that `T'
19348 is bound to `double'. */
19349
19350 static tree
19351 get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
19352 {
19353 int i, ntparms = TREE_VEC_LENGTH (tparms);
19354 tree deduced_args;
19355 tree innermost_deduced_args;
19356
19357 innermost_deduced_args = make_tree_vec (ntparms);
19358 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19359 {
19360 deduced_args = copy_node (args);
19361 SET_TMPL_ARGS_LEVEL (deduced_args,
19362 TMPL_ARGS_DEPTH (deduced_args),
19363 innermost_deduced_args);
19364 }
19365 else
19366 deduced_args = innermost_deduced_args;
19367
19368 if (unify (tparms, deduced_args,
19369 INNERMOST_TEMPLATE_ARGS (spec_args),
19370 INNERMOST_TEMPLATE_ARGS (args),
19371 UNIFY_ALLOW_NONE, /*explain_p=*/false))
19372 return NULL_TREE;
19373
19374 for (i = 0; i < ntparms; ++i)
19375 if (! TREE_VEC_ELT (innermost_deduced_args, i))
19376 return NULL_TREE;
19377
19378 /* Verify that nondeduced template arguments agree with the type
19379 obtained from argument deduction.
19380
19381 For example:
19382
19383 struct A { typedef int X; };
19384 template <class T, class U> struct C {};
19385 template <class T> struct C<T, typename T::X> {};
19386
19387 Then with the instantiation `C<A, int>', we can deduce that
19388 `T' is `A' but unify () does not check whether `typename T::X'
19389 is `int'. */
19390 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
19391 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19392 spec_args, tmpl,
19393 tf_none, false, false);
19394 if (spec_args == error_mark_node
19395 /* We only need to check the innermost arguments; the other
19396 arguments will always agree. */
19397 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
19398 INNERMOST_TEMPLATE_ARGS (args)))
19399 return NULL_TREE;
19400
19401 /* Now that we have bindings for all of the template arguments,
19402 ensure that the arguments deduced for the template template
19403 parameters have compatible template parameter lists. See the use
19404 of template_template_parm_bindings_ok_p in fn_type_unification
19405 for more information. */
19406 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
19407 return NULL_TREE;
19408
19409 return deduced_args;
19410 }
19411
19412 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
19413 Return the TREE_LIST node with the most specialized template, if
19414 any. If there is no most specialized template, the error_mark_node
19415 is returned.
19416
19417 Note that this function does not look at, or modify, the
19418 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
19419 returned is one of the elements of INSTANTIATIONS, callers may
19420 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
19421 and retrieve it from the value returned. */
19422
19423 tree
19424 most_specialized_instantiation (tree templates)
19425 {
19426 tree fn, champ;
19427
19428 ++processing_template_decl;
19429
19430 champ = templates;
19431 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
19432 {
19433 int fate = 0;
19434
19435 if (get_bindings (TREE_VALUE (champ),
19436 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19437 NULL_TREE, /*check_ret=*/true))
19438 fate--;
19439
19440 if (get_bindings (TREE_VALUE (fn),
19441 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19442 NULL_TREE, /*check_ret=*/true))
19443 fate++;
19444
19445 if (fate == -1)
19446 champ = fn;
19447 else if (!fate)
19448 {
19449 /* Equally specialized, move to next function. If there
19450 is no next function, nothing's most specialized. */
19451 fn = TREE_CHAIN (fn);
19452 champ = fn;
19453 if (!fn)
19454 break;
19455 }
19456 }
19457
19458 if (champ)
19459 /* Now verify that champ is better than everything earlier in the
19460 instantiation list. */
19461 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
19462 if (get_bindings (TREE_VALUE (champ),
19463 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
19464 NULL_TREE, /*check_ret=*/true)
19465 || !get_bindings (TREE_VALUE (fn),
19466 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
19467 NULL_TREE, /*check_ret=*/true))
19468 {
19469 champ = NULL_TREE;
19470 break;
19471 }
19472
19473 processing_template_decl--;
19474
19475 if (!champ)
19476 return error_mark_node;
19477
19478 return champ;
19479 }
19480
19481 /* If DECL is a specialization of some template, return the most
19482 general such template. Otherwise, returns NULL_TREE.
19483
19484 For example, given:
19485
19486 template <class T> struct S { template <class U> void f(U); };
19487
19488 if TMPL is `template <class U> void S<int>::f(U)' this will return
19489 the full template. This function will not trace past partial
19490 specializations, however. For example, given in addition:
19491
19492 template <class T> struct S<T*> { template <class U> void f(U); };
19493
19494 if TMPL is `template <class U> void S<int*>::f(U)' this will return
19495 `template <class T> template <class U> S<T*>::f(U)'. */
19496
19497 tree
19498 most_general_template (tree decl)
19499 {
19500 if (TREE_CODE (decl) != TEMPLATE_DECL)
19501 {
19502 if (tree tinfo = get_template_info (decl))
19503 decl = TI_TEMPLATE (tinfo);
19504 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
19505 template friend, or a FIELD_DECL for a capture pack. */
19506 if (TREE_CODE (decl) != TEMPLATE_DECL)
19507 return NULL_TREE;
19508 }
19509
19510 /* Look for more and more general templates. */
19511 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
19512 {
19513 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
19514 (See cp-tree.h for details.) */
19515 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
19516 break;
19517
19518 if (CLASS_TYPE_P (TREE_TYPE (decl))
19519 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
19520 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
19521 break;
19522
19523 /* Stop if we run into an explicitly specialized class template. */
19524 if (!DECL_NAMESPACE_SCOPE_P (decl)
19525 && DECL_CONTEXT (decl)
19526 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
19527 break;
19528
19529 decl = DECL_TI_TEMPLATE (decl);
19530 }
19531
19532 return decl;
19533 }
19534
19535 /* Return the most specialized of the template partial specializations
19536 which can produce TARGET, a specialization of some class or variable
19537 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
19538 a TEMPLATE_DECL node corresponding to the partial specialization, while
19539 the TREE_PURPOSE is the set of template arguments that must be
19540 substituted into the template pattern in order to generate TARGET.
19541
19542 If the choice of partial specialization is ambiguous, a diagnostic
19543 is issued, and the error_mark_node is returned. If there are no
19544 partial specializations matching TARGET, then NULL_TREE is
19545 returned, indicating that the primary template should be used. */
19546
19547 static tree
19548 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
19549 {
19550 tree list = NULL_TREE;
19551 tree t;
19552 tree champ;
19553 int fate;
19554 bool ambiguous_p;
19555 tree outer_args = NULL_TREE;
19556 tree tmpl, args;
19557
19558 if (TYPE_P (target))
19559 {
19560 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
19561 tmpl = TI_TEMPLATE (tinfo);
19562 args = TI_ARGS (tinfo);
19563 }
19564 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
19565 {
19566 tmpl = TREE_OPERAND (target, 0);
19567 args = TREE_OPERAND (target, 1);
19568 }
19569 else if (VAR_P (target))
19570 {
19571 tree tinfo = DECL_TEMPLATE_INFO (target);
19572 tmpl = TI_TEMPLATE (tinfo);
19573 args = TI_ARGS (tinfo);
19574 }
19575 else
19576 gcc_unreachable ();
19577
19578 tree main_tmpl = most_general_template (tmpl);
19579
19580 /* For determining which partial specialization to use, only the
19581 innermost args are interesting. */
19582 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
19583 {
19584 outer_args = strip_innermost_template_args (args, 1);
19585 args = INNERMOST_TEMPLATE_ARGS (args);
19586 }
19587
19588 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
19589 {
19590 tree partial_spec_args;
19591 tree spec_args;
19592 tree spec_tmpl = TREE_VALUE (t);
19593
19594 partial_spec_args = TREE_PURPOSE (t);
19595
19596 ++processing_template_decl;
19597
19598 if (outer_args)
19599 {
19600 /* Discard the outer levels of args, and then substitute in the
19601 template args from the enclosing class. */
19602 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
19603 partial_spec_args = tsubst_template_args
19604 (partial_spec_args, outer_args, tf_none, NULL_TREE);
19605
19606 /* And the same for the partial specialization TEMPLATE_DECL. */
19607 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
19608 }
19609
19610 partial_spec_args =
19611 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
19612 partial_spec_args,
19613 tmpl, tf_none,
19614 /*require_all_args=*/true,
19615 /*use_default_args=*/true);
19616
19617 --processing_template_decl;
19618
19619 if (partial_spec_args == error_mark_node)
19620 return error_mark_node;
19621 if (spec_tmpl == error_mark_node)
19622 return error_mark_node;
19623
19624 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
19625 spec_args = get_partial_spec_bindings (tmpl, parms,
19626 partial_spec_args,
19627 args);
19628 if (spec_args)
19629 {
19630 if (outer_args)
19631 spec_args = add_to_template_args (outer_args, spec_args);
19632 list = tree_cons (spec_args, TREE_VALUE (t), list);
19633 TREE_TYPE (list) = TREE_TYPE (t);
19634 }
19635 }
19636
19637 if (! list)
19638 return NULL_TREE;
19639
19640 ambiguous_p = false;
19641 t = list;
19642 champ = t;
19643 t = TREE_CHAIN (t);
19644 for (; t; t = TREE_CHAIN (t))
19645 {
19646 fate = more_specialized_partial_spec (tmpl, champ, t);
19647 if (fate == 1)
19648 ;
19649 else
19650 {
19651 if (fate == 0)
19652 {
19653 t = TREE_CHAIN (t);
19654 if (! t)
19655 {
19656 ambiguous_p = true;
19657 break;
19658 }
19659 }
19660 champ = t;
19661 }
19662 }
19663
19664 if (!ambiguous_p)
19665 for (t = list; t && t != champ; t = TREE_CHAIN (t))
19666 {
19667 fate = more_specialized_partial_spec (tmpl, champ, t);
19668 if (fate != 1)
19669 {
19670 ambiguous_p = true;
19671 break;
19672 }
19673 }
19674
19675 if (ambiguous_p)
19676 {
19677 const char *str;
19678 char *spaces = NULL;
19679 if (!(complain & tf_error))
19680 return error_mark_node;
19681 if (TYPE_P (target))
19682 error ("ambiguous template instantiation for %q#T", target);
19683 else
19684 error ("ambiguous template instantiation for %q#D", target);
19685 str = ngettext ("candidate is:", "candidates are:", list_length (list));
19686 for (t = list; t; t = TREE_CHAIN (t))
19687 {
19688 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
19689 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
19690 "%s %#S", spaces ? spaces : str, subst);
19691 spaces = spaces ? spaces : get_spaces (str);
19692 }
19693 free (spaces);
19694 return error_mark_node;
19695 }
19696
19697 return champ;
19698 }
19699
19700 /* Explicitly instantiate DECL. */
19701
19702 void
19703 do_decl_instantiation (tree decl, tree storage)
19704 {
19705 tree result = NULL_TREE;
19706 int extern_p = 0;
19707
19708 if (!decl || decl == error_mark_node)
19709 /* An error occurred, for which grokdeclarator has already issued
19710 an appropriate message. */
19711 return;
19712 else if (! DECL_LANG_SPECIFIC (decl))
19713 {
19714 error ("explicit instantiation of non-template %q#D", decl);
19715 return;
19716 }
19717
19718 bool var_templ = (DECL_TEMPLATE_INFO (decl)
19719 && variable_template_p (DECL_TI_TEMPLATE (decl)));
19720
19721 if (VAR_P (decl) && !var_templ)
19722 {
19723 /* There is an asymmetry here in the way VAR_DECLs and
19724 FUNCTION_DECLs are handled by grokdeclarator. In the case of
19725 the latter, the DECL we get back will be marked as a
19726 template instantiation, and the appropriate
19727 DECL_TEMPLATE_INFO will be set up. This does not happen for
19728 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
19729 should handle VAR_DECLs as it currently handles
19730 FUNCTION_DECLs. */
19731 if (!DECL_CLASS_SCOPE_P (decl))
19732 {
19733 error ("%qD is not a static data member of a class template", decl);
19734 return;
19735 }
19736 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
19737 if (!result || !VAR_P (result))
19738 {
19739 error ("no matching template for %qD found", decl);
19740 return;
19741 }
19742 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
19743 {
19744 error ("type %qT for explicit instantiation %qD does not match "
19745 "declared type %qT", TREE_TYPE (result), decl,
19746 TREE_TYPE (decl));
19747 return;
19748 }
19749 }
19750 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
19751 {
19752 error ("explicit instantiation of %q#D", decl);
19753 return;
19754 }
19755 else
19756 result = decl;
19757
19758 /* Check for various error cases. Note that if the explicit
19759 instantiation is valid the RESULT will currently be marked as an
19760 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
19761 until we get here. */
19762
19763 if (DECL_TEMPLATE_SPECIALIZATION (result))
19764 {
19765 /* DR 259 [temp.spec].
19766
19767 Both an explicit instantiation and a declaration of an explicit
19768 specialization shall not appear in a program unless the explicit
19769 instantiation follows a declaration of the explicit specialization.
19770
19771 For a given set of template parameters, if an explicit
19772 instantiation of a template appears after a declaration of an
19773 explicit specialization for that template, the explicit
19774 instantiation has no effect. */
19775 return;
19776 }
19777 else if (DECL_EXPLICIT_INSTANTIATION (result))
19778 {
19779 /* [temp.spec]
19780
19781 No program shall explicitly instantiate any template more
19782 than once.
19783
19784 We check DECL_NOT_REALLY_EXTERN so as not to complain when
19785 the first instantiation was `extern' and the second is not,
19786 and EXTERN_P for the opposite case. */
19787 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
19788 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
19789 /* If an "extern" explicit instantiation follows an ordinary
19790 explicit instantiation, the template is instantiated. */
19791 if (extern_p)
19792 return;
19793 }
19794 else if (!DECL_IMPLICIT_INSTANTIATION (result))
19795 {
19796 error ("no matching template for %qD found", result);
19797 return;
19798 }
19799 else if (!DECL_TEMPLATE_INFO (result))
19800 {
19801 permerror (input_location, "explicit instantiation of non-template %q#D", result);
19802 return;
19803 }
19804
19805 if (storage == NULL_TREE)
19806 ;
19807 else if (storage == ridpointers[(int) RID_EXTERN])
19808 {
19809 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
19810 pedwarn (input_location, OPT_Wpedantic,
19811 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
19812 "instantiations");
19813 extern_p = 1;
19814 }
19815 else
19816 error ("storage class %qD applied to template instantiation", storage);
19817
19818 check_explicit_instantiation_namespace (result);
19819 mark_decl_instantiated (result, extern_p);
19820 if (! extern_p)
19821 instantiate_decl (result, /*defer_ok=*/1,
19822 /*expl_inst_class_mem_p=*/false);
19823 }
19824
19825 static void
19826 mark_class_instantiated (tree t, int extern_p)
19827 {
19828 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
19829 SET_CLASSTYPE_INTERFACE_KNOWN (t);
19830 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
19831 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
19832 if (! extern_p)
19833 {
19834 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
19835 rest_of_type_compilation (t, 1);
19836 }
19837 }
19838
19839 /* Called from do_type_instantiation through binding_table_foreach to
19840 do recursive instantiation for the type bound in ENTRY. */
19841 static void
19842 bt_instantiate_type_proc (binding_entry entry, void *data)
19843 {
19844 tree storage = *(tree *) data;
19845
19846 if (MAYBE_CLASS_TYPE_P (entry->type)
19847 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
19848 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
19849 }
19850
19851 /* Called from do_type_instantiation to instantiate a member
19852 (a member function or a static member variable) of an
19853 explicitly instantiated class template. */
19854 static void
19855 instantiate_class_member (tree decl, int extern_p)
19856 {
19857 mark_decl_instantiated (decl, extern_p);
19858 if (! extern_p)
19859 instantiate_decl (decl, /*defer_ok=*/1,
19860 /*expl_inst_class_mem_p=*/true);
19861 }
19862
19863 /* Perform an explicit instantiation of template class T. STORAGE, if
19864 non-null, is the RID for extern, inline or static. COMPLAIN is
19865 nonzero if this is called from the parser, zero if called recursively,
19866 since the standard is unclear (as detailed below). */
19867
19868 void
19869 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
19870 {
19871 int extern_p = 0;
19872 int nomem_p = 0;
19873 int static_p = 0;
19874 int previous_instantiation_extern_p = 0;
19875
19876 if (TREE_CODE (t) == TYPE_DECL)
19877 t = TREE_TYPE (t);
19878
19879 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
19880 {
19881 tree tmpl =
19882 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
19883 if (tmpl)
19884 error ("explicit instantiation of non-class template %qD", tmpl);
19885 else
19886 error ("explicit instantiation of non-template type %qT", t);
19887 return;
19888 }
19889
19890 complete_type (t);
19891
19892 if (!COMPLETE_TYPE_P (t))
19893 {
19894 if (complain & tf_error)
19895 error ("explicit instantiation of %q#T before definition of template",
19896 t);
19897 return;
19898 }
19899
19900 if (storage != NULL_TREE)
19901 {
19902 if (!in_system_header_at (input_location))
19903 {
19904 if (storage == ridpointers[(int) RID_EXTERN])
19905 {
19906 if (cxx_dialect == cxx98)
19907 pedwarn (input_location, OPT_Wpedantic,
19908 "ISO C++ 1998 forbids the use of %<extern%> on "
19909 "explicit instantiations");
19910 }
19911 else
19912 pedwarn (input_location, OPT_Wpedantic,
19913 "ISO C++ forbids the use of %qE"
19914 " on explicit instantiations", storage);
19915 }
19916
19917 if (storage == ridpointers[(int) RID_INLINE])
19918 nomem_p = 1;
19919 else if (storage == ridpointers[(int) RID_EXTERN])
19920 extern_p = 1;
19921 else if (storage == ridpointers[(int) RID_STATIC])
19922 static_p = 1;
19923 else
19924 {
19925 error ("storage class %qD applied to template instantiation",
19926 storage);
19927 extern_p = 0;
19928 }
19929 }
19930
19931 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
19932 {
19933 /* DR 259 [temp.spec].
19934
19935 Both an explicit instantiation and a declaration of an explicit
19936 specialization shall not appear in a program unless the explicit
19937 instantiation follows a declaration of the explicit specialization.
19938
19939 For a given set of template parameters, if an explicit
19940 instantiation of a template appears after a declaration of an
19941 explicit specialization for that template, the explicit
19942 instantiation has no effect. */
19943 return;
19944 }
19945 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
19946 {
19947 /* [temp.spec]
19948
19949 No program shall explicitly instantiate any template more
19950 than once.
19951
19952 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
19953 instantiation was `extern'. If EXTERN_P then the second is.
19954 These cases are OK. */
19955 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
19956
19957 if (!previous_instantiation_extern_p && !extern_p
19958 && (complain & tf_error))
19959 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
19960
19961 /* If we've already instantiated the template, just return now. */
19962 if (!CLASSTYPE_INTERFACE_ONLY (t))
19963 return;
19964 }
19965
19966 check_explicit_instantiation_namespace (TYPE_NAME (t));
19967 mark_class_instantiated (t, extern_p);
19968
19969 if (nomem_p)
19970 return;
19971
19972 {
19973 tree tmp;
19974
19975 /* In contrast to implicit instantiation, where only the
19976 declarations, and not the definitions, of members are
19977 instantiated, we have here:
19978
19979 [temp.explicit]
19980
19981 The explicit instantiation of a class template specialization
19982 implies the instantiation of all of its members not
19983 previously explicitly specialized in the translation unit
19984 containing the explicit instantiation.
19985
19986 Of course, we can't instantiate member template classes, since
19987 we don't have any arguments for them. Note that the standard
19988 is unclear on whether the instantiation of the members are
19989 *explicit* instantiations or not. However, the most natural
19990 interpretation is that it should be an explicit instantiation. */
19991
19992 if (! static_p)
19993 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19994 if (TREE_CODE (tmp) == FUNCTION_DECL
19995 && DECL_TEMPLATE_INSTANTIATION (tmp))
19996 instantiate_class_member (tmp, extern_p);
19997
19998 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19999 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
20000 instantiate_class_member (tmp, extern_p);
20001
20002 if (CLASSTYPE_NESTED_UTDS (t))
20003 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
20004 bt_instantiate_type_proc, &storage);
20005 }
20006 }
20007
20008 /* Given a function DECL, which is a specialization of TMPL, modify
20009 DECL to be a re-instantiation of TMPL with the same template
20010 arguments. TMPL should be the template into which tsubst'ing
20011 should occur for DECL, not the most general template.
20012
20013 One reason for doing this is a scenario like this:
20014
20015 template <class T>
20016 void f(const T&, int i);
20017
20018 void g() { f(3, 7); }
20019
20020 template <class T>
20021 void f(const T& t, const int i) { }
20022
20023 Note that when the template is first instantiated, with
20024 instantiate_template, the resulting DECL will have no name for the
20025 first parameter, and the wrong type for the second. So, when we go
20026 to instantiate the DECL, we regenerate it. */
20027
20028 static void
20029 regenerate_decl_from_template (tree decl, tree tmpl)
20030 {
20031 /* The arguments used to instantiate DECL, from the most general
20032 template. */
20033 tree args;
20034 tree code_pattern;
20035
20036 args = DECL_TI_ARGS (decl);
20037 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
20038
20039 /* Make sure that we can see identifiers, and compute access
20040 correctly. */
20041 push_access_scope (decl);
20042
20043 if (TREE_CODE (decl) == FUNCTION_DECL)
20044 {
20045 tree decl_parm;
20046 tree pattern_parm;
20047 tree specs;
20048 int args_depth;
20049 int parms_depth;
20050
20051 args_depth = TMPL_ARGS_DEPTH (args);
20052 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
20053 if (args_depth > parms_depth)
20054 args = get_innermost_template_args (args, parms_depth);
20055
20056 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
20057 args, tf_error, NULL_TREE,
20058 /*defer_ok*/false);
20059 if (specs && specs != error_mark_node)
20060 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
20061 specs);
20062
20063 /* Merge parameter declarations. */
20064 decl_parm = skip_artificial_parms_for (decl,
20065 DECL_ARGUMENTS (decl));
20066 pattern_parm
20067 = skip_artificial_parms_for (code_pattern,
20068 DECL_ARGUMENTS (code_pattern));
20069 while (decl_parm && !DECL_PACK_P (pattern_parm))
20070 {
20071 tree parm_type;
20072 tree attributes;
20073
20074 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
20075 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
20076 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
20077 NULL_TREE);
20078 parm_type = type_decays_to (parm_type);
20079 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
20080 TREE_TYPE (decl_parm) = parm_type;
20081 attributes = DECL_ATTRIBUTES (pattern_parm);
20082 if (DECL_ATTRIBUTES (decl_parm) != attributes)
20083 {
20084 DECL_ATTRIBUTES (decl_parm) = attributes;
20085 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
20086 }
20087 decl_parm = DECL_CHAIN (decl_parm);
20088 pattern_parm = DECL_CHAIN (pattern_parm);
20089 }
20090 /* Merge any parameters that match with the function parameter
20091 pack. */
20092 if (pattern_parm && DECL_PACK_P (pattern_parm))
20093 {
20094 int i, len;
20095 tree expanded_types;
20096 /* Expand the TYPE_PACK_EXPANSION that provides the types for
20097 the parameters in this function parameter pack. */
20098 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
20099 args, tf_error, NULL_TREE);
20100 len = TREE_VEC_LENGTH (expanded_types);
20101 for (i = 0; i < len; i++)
20102 {
20103 tree parm_type;
20104 tree attributes;
20105
20106 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
20107 /* Rename the parameter to include the index. */
20108 DECL_NAME (decl_parm) =
20109 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
20110 parm_type = TREE_VEC_ELT (expanded_types, i);
20111 parm_type = type_decays_to (parm_type);
20112 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
20113 TREE_TYPE (decl_parm) = parm_type;
20114 attributes = DECL_ATTRIBUTES (pattern_parm);
20115 if (DECL_ATTRIBUTES (decl_parm) != attributes)
20116 {
20117 DECL_ATTRIBUTES (decl_parm) = attributes;
20118 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
20119 }
20120 decl_parm = DECL_CHAIN (decl_parm);
20121 }
20122 }
20123 /* Merge additional specifiers from the CODE_PATTERN. */
20124 if (DECL_DECLARED_INLINE_P (code_pattern)
20125 && !DECL_DECLARED_INLINE_P (decl))
20126 DECL_DECLARED_INLINE_P (decl) = 1;
20127 }
20128 else if (VAR_P (decl))
20129 {
20130 DECL_INITIAL (decl) =
20131 tsubst_expr (DECL_INITIAL (code_pattern), args,
20132 tf_error, DECL_TI_TEMPLATE (decl),
20133 /*integral_constant_expression_p=*/false);
20134 if (VAR_HAD_UNKNOWN_BOUND (decl))
20135 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
20136 tf_error, DECL_TI_TEMPLATE (decl));
20137 }
20138 else
20139 gcc_unreachable ();
20140
20141 pop_access_scope (decl);
20142 }
20143
20144 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
20145 substituted to get DECL. */
20146
20147 tree
20148 template_for_substitution (tree decl)
20149 {
20150 tree tmpl = DECL_TI_TEMPLATE (decl);
20151
20152 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
20153 for the instantiation. This is not always the most general
20154 template. Consider, for example:
20155
20156 template <class T>
20157 struct S { template <class U> void f();
20158 template <> void f<int>(); };
20159
20160 and an instantiation of S<double>::f<int>. We want TD to be the
20161 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
20162 while (/* An instantiation cannot have a definition, so we need a
20163 more general template. */
20164 DECL_TEMPLATE_INSTANTIATION (tmpl)
20165 /* We must also deal with friend templates. Given:
20166
20167 template <class T> struct S {
20168 template <class U> friend void f() {};
20169 };
20170
20171 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
20172 so far as the language is concerned, but that's still
20173 where we get the pattern for the instantiation from. On
20174 other hand, if the definition comes outside the class, say:
20175
20176 template <class T> struct S {
20177 template <class U> friend void f();
20178 };
20179 template <class U> friend void f() {}
20180
20181 we don't need to look any further. That's what the check for
20182 DECL_INITIAL is for. */
20183 || (TREE_CODE (decl) == FUNCTION_DECL
20184 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
20185 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
20186 {
20187 /* The present template, TD, should not be a definition. If it
20188 were a definition, we should be using it! Note that we
20189 cannot restructure the loop to just keep going until we find
20190 a template with a definition, since that might go too far if
20191 a specialization was declared, but not defined. */
20192
20193 /* Fetch the more general template. */
20194 tmpl = DECL_TI_TEMPLATE (tmpl);
20195 }
20196
20197 return tmpl;
20198 }
20199
20200 /* Returns true if we need to instantiate this template instance even if we
20201 know we aren't going to emit it. */
20202
20203 bool
20204 always_instantiate_p (tree decl)
20205 {
20206 /* We always instantiate inline functions so that we can inline them. An
20207 explicit instantiation declaration prohibits implicit instantiation of
20208 non-inline functions. With high levels of optimization, we would
20209 normally inline non-inline functions -- but we're not allowed to do
20210 that for "extern template" functions. Therefore, we check
20211 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
20212 return ((TREE_CODE (decl) == FUNCTION_DECL
20213 && (DECL_DECLARED_INLINE_P (decl)
20214 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
20215 /* And we need to instantiate static data members so that
20216 their initializers are available in integral constant
20217 expressions. */
20218 || (VAR_P (decl)
20219 && decl_maybe_constant_var_p (decl)));
20220 }
20221
20222 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
20223 instantiate it now, modifying TREE_TYPE (fn). */
20224
20225 void
20226 maybe_instantiate_noexcept (tree fn)
20227 {
20228 tree fntype, spec, noex, clone;
20229
20230 /* Don't instantiate a noexcept-specification from template context. */
20231 if (processing_template_decl)
20232 return;
20233
20234 if (DECL_CLONED_FUNCTION_P (fn))
20235 fn = DECL_CLONED_FUNCTION (fn);
20236 fntype = TREE_TYPE (fn);
20237 spec = TYPE_RAISES_EXCEPTIONS (fntype);
20238
20239 if (!spec || !TREE_PURPOSE (spec))
20240 return;
20241
20242 noex = TREE_PURPOSE (spec);
20243
20244 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
20245 {
20246 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
20247 spec = get_defaulted_eh_spec (fn);
20248 else if (push_tinst_level (fn))
20249 {
20250 push_access_scope (fn);
20251 push_deferring_access_checks (dk_no_deferred);
20252 input_location = DECL_SOURCE_LOCATION (fn);
20253 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
20254 DEFERRED_NOEXCEPT_ARGS (noex),
20255 tf_warning_or_error, fn,
20256 /*function_p=*/false,
20257 /*integral_constant_expression_p=*/true);
20258 pop_deferring_access_checks ();
20259 pop_access_scope (fn);
20260 pop_tinst_level ();
20261 spec = build_noexcept_spec (noex, tf_warning_or_error);
20262 if (spec == error_mark_node)
20263 spec = noexcept_false_spec;
20264 }
20265 else
20266 spec = noexcept_false_spec;
20267
20268 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
20269 }
20270
20271 FOR_EACH_CLONE (clone, fn)
20272 {
20273 if (TREE_TYPE (clone) == fntype)
20274 TREE_TYPE (clone) = TREE_TYPE (fn);
20275 else
20276 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
20277 }
20278 }
20279
20280 /* Produce the definition of D, a _DECL generated from a template. If
20281 DEFER_OK is nonzero, then we don't have to actually do the
20282 instantiation now; we just have to do it sometime. Normally it is
20283 an error if this is an explicit instantiation but D is undefined.
20284 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
20285 explicitly instantiated class template. */
20286
20287 tree
20288 instantiate_decl (tree d, int defer_ok,
20289 bool expl_inst_class_mem_p)
20290 {
20291 tree tmpl = DECL_TI_TEMPLATE (d);
20292 tree gen_args;
20293 tree args;
20294 tree td;
20295 tree code_pattern;
20296 tree spec;
20297 tree gen_tmpl;
20298 bool pattern_defined;
20299 location_t saved_loc = input_location;
20300 int saved_unevaluated_operand = cp_unevaluated_operand;
20301 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20302 bool external_p;
20303 bool deleted_p;
20304 tree fn_context;
20305 bool nested;
20306
20307 /* This function should only be used to instantiate templates for
20308 functions and static member variables. */
20309 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
20310
20311 /* Variables are never deferred; if instantiation is required, they
20312 are instantiated right away. That allows for better code in the
20313 case that an expression refers to the value of the variable --
20314 if the variable has a constant value the referring expression can
20315 take advantage of that fact. */
20316 if (VAR_P (d)
20317 || DECL_DECLARED_CONSTEXPR_P (d))
20318 defer_ok = 0;
20319
20320 /* Don't instantiate cloned functions. Instead, instantiate the
20321 functions they cloned. */
20322 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
20323 d = DECL_CLONED_FUNCTION (d);
20324
20325 if (DECL_TEMPLATE_INSTANTIATED (d)
20326 || (TREE_CODE (d) == FUNCTION_DECL
20327 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
20328 || DECL_TEMPLATE_SPECIALIZATION (d))
20329 /* D has already been instantiated or explicitly specialized, so
20330 there's nothing for us to do here.
20331
20332 It might seem reasonable to check whether or not D is an explicit
20333 instantiation, and, if so, stop here. But when an explicit
20334 instantiation is deferred until the end of the compilation,
20335 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
20336 the instantiation. */
20337 return d;
20338
20339 /* Check to see whether we know that this template will be
20340 instantiated in some other file, as with "extern template"
20341 extension. */
20342 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
20343
20344 /* In general, we do not instantiate such templates. */
20345 if (external_p && !always_instantiate_p (d))
20346 return d;
20347
20348 gen_tmpl = most_general_template (tmpl);
20349 gen_args = DECL_TI_ARGS (d);
20350
20351 if (tmpl != gen_tmpl)
20352 /* We should already have the extra args. */
20353 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
20354 == TMPL_ARGS_DEPTH (gen_args));
20355 /* And what's in the hash table should match D. */
20356 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
20357 || spec == NULL_TREE);
20358
20359 /* This needs to happen before any tsubsting. */
20360 if (! push_tinst_level (d))
20361 return d;
20362
20363 timevar_push (TV_TEMPLATE_INST);
20364
20365 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
20366 for the instantiation. */
20367 td = template_for_substitution (d);
20368 code_pattern = DECL_TEMPLATE_RESULT (td);
20369
20370 /* We should never be trying to instantiate a member of a class
20371 template or partial specialization. */
20372 gcc_assert (d != code_pattern);
20373
20374 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
20375 || DECL_TEMPLATE_SPECIALIZATION (td))
20376 /* In the case of a friend template whose definition is provided
20377 outside the class, we may have too many arguments. Drop the
20378 ones we don't need. The same is true for specializations. */
20379 args = get_innermost_template_args
20380 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
20381 else
20382 args = gen_args;
20383
20384 if (TREE_CODE (d) == FUNCTION_DECL)
20385 {
20386 deleted_p = DECL_DELETED_FN (code_pattern);
20387 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
20388 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
20389 || deleted_p);
20390 }
20391 else
20392 {
20393 deleted_p = false;
20394 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
20395 }
20396
20397 /* We may be in the middle of deferred access check. Disable it now. */
20398 push_deferring_access_checks (dk_no_deferred);
20399
20400 /* Unless an explicit instantiation directive has already determined
20401 the linkage of D, remember that a definition is available for
20402 this entity. */
20403 if (pattern_defined
20404 && !DECL_INTERFACE_KNOWN (d)
20405 && !DECL_NOT_REALLY_EXTERN (d))
20406 mark_definable (d);
20407
20408 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
20409 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
20410 input_location = DECL_SOURCE_LOCATION (d);
20411
20412 /* If D is a member of an explicitly instantiated class template,
20413 and no definition is available, treat it like an implicit
20414 instantiation. */
20415 if (!pattern_defined && expl_inst_class_mem_p
20416 && DECL_EXPLICIT_INSTANTIATION (d))
20417 {
20418 /* Leave linkage flags alone on instantiations with anonymous
20419 visibility. */
20420 if (TREE_PUBLIC (d))
20421 {
20422 DECL_NOT_REALLY_EXTERN (d) = 0;
20423 DECL_INTERFACE_KNOWN (d) = 0;
20424 }
20425 SET_DECL_IMPLICIT_INSTANTIATION (d);
20426 }
20427
20428 /* Defer all other templates, unless we have been explicitly
20429 forbidden from doing so. */
20430 if (/* If there is no definition, we cannot instantiate the
20431 template. */
20432 ! pattern_defined
20433 /* If it's OK to postpone instantiation, do so. */
20434 || defer_ok
20435 /* If this is a static data member that will be defined
20436 elsewhere, we don't want to instantiate the entire data
20437 member, but we do want to instantiate the initializer so that
20438 we can substitute that elsewhere. */
20439 || (external_p && VAR_P (d))
20440 /* Handle here a deleted function too, avoid generating
20441 its body (c++/61080). */
20442 || deleted_p)
20443 {
20444 /* The definition of the static data member is now required so
20445 we must substitute the initializer. */
20446 if (VAR_P (d)
20447 && !DECL_INITIAL (d)
20448 && DECL_INITIAL (code_pattern))
20449 {
20450 tree ns;
20451 tree init;
20452 bool const_init = false;
20453 bool enter_context = DECL_CLASS_SCOPE_P (d);
20454
20455 ns = decl_namespace_context (d);
20456 push_nested_namespace (ns);
20457 if (enter_context)
20458 push_nested_class (DECL_CONTEXT (d));
20459 init = tsubst_expr (DECL_INITIAL (code_pattern),
20460 args,
20461 tf_warning_or_error, NULL_TREE,
20462 /*integral_constant_expression_p=*/false);
20463 /* If instantiating the initializer involved instantiating this
20464 again, don't call cp_finish_decl twice. */
20465 if (!DECL_INITIAL (d))
20466 {
20467 /* Make sure the initializer is still constant, in case of
20468 circular dependency (template/instantiate6.C). */
20469 const_init
20470 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20471 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
20472 /*asmspec_tree=*/NULL_TREE,
20473 LOOKUP_ONLYCONVERTING);
20474 }
20475 if (enter_context)
20476 pop_nested_class ();
20477 pop_nested_namespace (ns);
20478 }
20479
20480 /* We restore the source position here because it's used by
20481 add_pending_template. */
20482 input_location = saved_loc;
20483
20484 if (at_eof && !pattern_defined
20485 && DECL_EXPLICIT_INSTANTIATION (d)
20486 && DECL_NOT_REALLY_EXTERN (d))
20487 /* [temp.explicit]
20488
20489 The definition of a non-exported function template, a
20490 non-exported member function template, or a non-exported
20491 member function or static data member of a class template
20492 shall be present in every translation unit in which it is
20493 explicitly instantiated. */
20494 permerror (input_location, "explicit instantiation of %qD "
20495 "but no definition available", d);
20496
20497 /* If we're in unevaluated context, we just wanted to get the
20498 constant value; this isn't an odr use, so don't queue
20499 a full instantiation. */
20500 if (cp_unevaluated_operand != 0)
20501 goto out;
20502 /* ??? Historically, we have instantiated inline functions, even
20503 when marked as "extern template". */
20504 if (!(external_p && VAR_P (d)))
20505 add_pending_template (d);
20506 goto out;
20507 }
20508 /* Tell the repository that D is available in this translation unit
20509 -- and see if it is supposed to be instantiated here. */
20510 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
20511 {
20512 /* In a PCH file, despite the fact that the repository hasn't
20513 requested instantiation in the PCH it is still possible that
20514 an instantiation will be required in a file that includes the
20515 PCH. */
20516 if (pch_file)
20517 add_pending_template (d);
20518 /* Instantiate inline functions so that the inliner can do its
20519 job, even though we'll not be emitting a copy of this
20520 function. */
20521 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
20522 goto out;
20523 }
20524
20525 fn_context = decl_function_context (d);
20526 nested = (current_function_decl != NULL_TREE);
20527 if (!fn_context)
20528 push_to_top_level ();
20529 else
20530 {
20531 if (nested)
20532 push_function_context ();
20533 cp_unevaluated_operand = 0;
20534 c_inhibit_evaluation_warnings = 0;
20535 }
20536
20537 /* Mark D as instantiated so that recursive calls to
20538 instantiate_decl do not try to instantiate it again. */
20539 DECL_TEMPLATE_INSTANTIATED (d) = 1;
20540
20541 /* Regenerate the declaration in case the template has been modified
20542 by a subsequent redeclaration. */
20543 regenerate_decl_from_template (d, td);
20544
20545 /* We already set the file and line above. Reset them now in case
20546 they changed as a result of calling regenerate_decl_from_template. */
20547 input_location = DECL_SOURCE_LOCATION (d);
20548
20549 if (VAR_P (d))
20550 {
20551 tree init;
20552 bool const_init = false;
20553
20554 /* Clear out DECL_RTL; whatever was there before may not be right
20555 since we've reset the type of the declaration. */
20556 SET_DECL_RTL (d, NULL);
20557 DECL_IN_AGGR_P (d) = 0;
20558
20559 /* The initializer is placed in DECL_INITIAL by
20560 regenerate_decl_from_template so we don't need to
20561 push/pop_access_scope again here. Pull it out so that
20562 cp_finish_decl can process it. */
20563 init = DECL_INITIAL (d);
20564 DECL_INITIAL (d) = NULL_TREE;
20565 DECL_INITIALIZED_P (d) = 0;
20566
20567 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
20568 initializer. That function will defer actual emission until
20569 we have a chance to determine linkage. */
20570 DECL_EXTERNAL (d) = 0;
20571
20572 /* Enter the scope of D so that access-checking works correctly. */
20573 bool enter_context = DECL_CLASS_SCOPE_P (d);
20574 if (enter_context)
20575 push_nested_class (DECL_CONTEXT (d));
20576
20577 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
20578 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
20579
20580 if (enter_context)
20581 pop_nested_class ();
20582
20583 if (variable_template_p (td))
20584 note_variable_template_instantiation (d);
20585 }
20586 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
20587 synthesize_method (d);
20588 else if (TREE_CODE (d) == FUNCTION_DECL)
20589 {
20590 hash_map<tree, tree> *saved_local_specializations;
20591 tree subst_decl;
20592 tree tmpl_parm;
20593 tree spec_parm;
20594 tree block = NULL_TREE;
20595
20596 /* Save away the current list, in case we are instantiating one
20597 template from within the body of another. */
20598 saved_local_specializations = local_specializations;
20599
20600 /* Set up the list of local specializations. */
20601 local_specializations = new hash_map<tree, tree>;
20602
20603 /* Set up context. */
20604 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20605 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20606 block = push_stmt_list ();
20607 else
20608 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
20609
20610 /* Some typedefs referenced from within the template code need to be
20611 access checked at template instantiation time, i.e now. These
20612 types were added to the template at parsing time. Let's get those
20613 and perform the access checks then. */
20614 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
20615 gen_args);
20616
20617 /* Create substitution entries for the parameters. */
20618 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
20619 tmpl_parm = DECL_ARGUMENTS (subst_decl);
20620 spec_parm = DECL_ARGUMENTS (d);
20621 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
20622 {
20623 register_local_specialization (spec_parm, tmpl_parm);
20624 spec_parm = skip_artificial_parms_for (d, spec_parm);
20625 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
20626 }
20627 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
20628 {
20629 if (!DECL_PACK_P (tmpl_parm))
20630 {
20631 register_local_specialization (spec_parm, tmpl_parm);
20632 spec_parm = DECL_CHAIN (spec_parm);
20633 }
20634 else
20635 {
20636 /* Register the (value) argument pack as a specialization of
20637 TMPL_PARM, then move on. */
20638 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
20639 register_local_specialization (argpack, tmpl_parm);
20640 }
20641 }
20642 gcc_assert (!spec_parm);
20643
20644 /* Substitute into the body of the function. */
20645 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20646 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
20647 tf_warning_or_error, tmpl);
20648 else
20649 {
20650 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
20651 tf_warning_or_error, tmpl,
20652 /*integral_constant_expression_p=*/false);
20653
20654 /* Set the current input_location to the end of the function
20655 so that finish_function knows where we are. */
20656 input_location
20657 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
20658
20659 /* Remember if we saw an infinite loop in the template. */
20660 current_function_infinite_loop
20661 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
20662 }
20663
20664 /* We don't need the local specializations any more. */
20665 delete local_specializations;
20666 local_specializations = saved_local_specializations;
20667
20668 /* Finish the function. */
20669 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
20670 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
20671 DECL_SAVED_TREE (d) = pop_stmt_list (block);
20672 else
20673 {
20674 d = finish_function (0);
20675 expand_or_defer_fn (d);
20676 }
20677
20678 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
20679 cp_check_omp_declare_reduction (d);
20680 }
20681
20682 /* We're not deferring instantiation any more. */
20683 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
20684
20685 if (!fn_context)
20686 pop_from_top_level ();
20687 else if (nested)
20688 pop_function_context ();
20689
20690 out:
20691 input_location = saved_loc;
20692 cp_unevaluated_operand = saved_unevaluated_operand;
20693 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20694 pop_deferring_access_checks ();
20695 pop_tinst_level ();
20696
20697 timevar_pop (TV_TEMPLATE_INST);
20698
20699 return d;
20700 }
20701
20702 /* Run through the list of templates that we wish we could
20703 instantiate, and instantiate any we can. RETRIES is the
20704 number of times we retry pending template instantiation. */
20705
20706 void
20707 instantiate_pending_templates (int retries)
20708 {
20709 int reconsider;
20710 location_t saved_loc = input_location;
20711
20712 /* Instantiating templates may trigger vtable generation. This in turn
20713 may require further template instantiations. We place a limit here
20714 to avoid infinite loop. */
20715 if (pending_templates && retries >= max_tinst_depth)
20716 {
20717 tree decl = pending_templates->tinst->decl;
20718
20719 fatal_error (input_location,
20720 "template instantiation depth exceeds maximum of %d"
20721 " instantiating %q+D, possibly from virtual table generation"
20722 " (use -ftemplate-depth= to increase the maximum)",
20723 max_tinst_depth, decl);
20724 if (TREE_CODE (decl) == FUNCTION_DECL)
20725 /* Pretend that we defined it. */
20726 DECL_INITIAL (decl) = error_mark_node;
20727 return;
20728 }
20729
20730 do
20731 {
20732 struct pending_template **t = &pending_templates;
20733 struct pending_template *last = NULL;
20734 reconsider = 0;
20735 while (*t)
20736 {
20737 tree instantiation = reopen_tinst_level ((*t)->tinst);
20738 bool complete = false;
20739
20740 if (TYPE_P (instantiation))
20741 {
20742 tree fn;
20743
20744 if (!COMPLETE_TYPE_P (instantiation))
20745 {
20746 instantiate_class_template (instantiation);
20747 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
20748 for (fn = TYPE_METHODS (instantiation);
20749 fn;
20750 fn = TREE_CHAIN (fn))
20751 if (! DECL_ARTIFICIAL (fn))
20752 instantiate_decl (fn,
20753 /*defer_ok=*/0,
20754 /*expl_inst_class_mem_p=*/false);
20755 if (COMPLETE_TYPE_P (instantiation))
20756 reconsider = 1;
20757 }
20758
20759 complete = COMPLETE_TYPE_P (instantiation);
20760 }
20761 else
20762 {
20763 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
20764 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
20765 {
20766 instantiation
20767 = instantiate_decl (instantiation,
20768 /*defer_ok=*/0,
20769 /*expl_inst_class_mem_p=*/false);
20770 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
20771 reconsider = 1;
20772 }
20773
20774 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
20775 || DECL_TEMPLATE_INSTANTIATED (instantiation));
20776 }
20777
20778 if (complete)
20779 /* If INSTANTIATION has been instantiated, then we don't
20780 need to consider it again in the future. */
20781 *t = (*t)->next;
20782 else
20783 {
20784 last = *t;
20785 t = &(*t)->next;
20786 }
20787 tinst_depth = 0;
20788 current_tinst_level = NULL;
20789 }
20790 last_pending_template = last;
20791 }
20792 while (reconsider);
20793
20794 input_location = saved_loc;
20795 }
20796
20797 /* Substitute ARGVEC into T, which is a list of initializers for
20798 either base class or a non-static data member. The TREE_PURPOSEs
20799 are DECLs, and the TREE_VALUEs are the initializer values. Used by
20800 instantiate_decl. */
20801
20802 static tree
20803 tsubst_initializer_list (tree t, tree argvec)
20804 {
20805 tree inits = NULL_TREE;
20806
20807 for (; t; t = TREE_CHAIN (t))
20808 {
20809 tree decl;
20810 tree init;
20811 tree expanded_bases = NULL_TREE;
20812 tree expanded_arguments = NULL_TREE;
20813 int i, len = 1;
20814
20815 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
20816 {
20817 tree expr;
20818 tree arg;
20819
20820 /* Expand the base class expansion type into separate base
20821 classes. */
20822 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
20823 tf_warning_or_error,
20824 NULL_TREE);
20825 if (expanded_bases == error_mark_node)
20826 continue;
20827
20828 /* We'll be building separate TREE_LISTs of arguments for
20829 each base. */
20830 len = TREE_VEC_LENGTH (expanded_bases);
20831 expanded_arguments = make_tree_vec (len);
20832 for (i = 0; i < len; i++)
20833 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
20834
20835 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
20836 expand each argument in the TREE_VALUE of t. */
20837 expr = make_node (EXPR_PACK_EXPANSION);
20838 PACK_EXPANSION_LOCAL_P (expr) = true;
20839 PACK_EXPANSION_PARAMETER_PACKS (expr) =
20840 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
20841
20842 if (TREE_VALUE (t) == void_type_node)
20843 /* VOID_TYPE_NODE is used to indicate
20844 value-initialization. */
20845 {
20846 for (i = 0; i < len; i++)
20847 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
20848 }
20849 else
20850 {
20851 /* Substitute parameter packs into each argument in the
20852 TREE_LIST. */
20853 in_base_initializer = 1;
20854 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
20855 {
20856 tree expanded_exprs;
20857
20858 /* Expand the argument. */
20859 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
20860 expanded_exprs
20861 = tsubst_pack_expansion (expr, argvec,
20862 tf_warning_or_error,
20863 NULL_TREE);
20864 if (expanded_exprs == error_mark_node)
20865 continue;
20866
20867 /* Prepend each of the expanded expressions to the
20868 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
20869 for (i = 0; i < len; i++)
20870 {
20871 TREE_VEC_ELT (expanded_arguments, i) =
20872 tree_cons (NULL_TREE,
20873 TREE_VEC_ELT (expanded_exprs, i),
20874 TREE_VEC_ELT (expanded_arguments, i));
20875 }
20876 }
20877 in_base_initializer = 0;
20878
20879 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
20880 since we built them backwards. */
20881 for (i = 0; i < len; i++)
20882 {
20883 TREE_VEC_ELT (expanded_arguments, i) =
20884 nreverse (TREE_VEC_ELT (expanded_arguments, i));
20885 }
20886 }
20887 }
20888
20889 for (i = 0; i < len; ++i)
20890 {
20891 if (expanded_bases)
20892 {
20893 decl = TREE_VEC_ELT (expanded_bases, i);
20894 decl = expand_member_init (decl);
20895 init = TREE_VEC_ELT (expanded_arguments, i);
20896 }
20897 else
20898 {
20899 tree tmp;
20900 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
20901 tf_warning_or_error, NULL_TREE);
20902
20903 decl = expand_member_init (decl);
20904 if (decl && !DECL_P (decl))
20905 in_base_initializer = 1;
20906
20907 init = TREE_VALUE (t);
20908 tmp = init;
20909 if (init != void_type_node)
20910 init = tsubst_expr (init, argvec,
20911 tf_warning_or_error, NULL_TREE,
20912 /*integral_constant_expression_p=*/false);
20913 if (init == NULL_TREE && tmp != NULL_TREE)
20914 /* If we had an initializer but it instantiated to nothing,
20915 value-initialize the object. This will only occur when
20916 the initializer was a pack expansion where the parameter
20917 packs used in that expansion were of length zero. */
20918 init = void_type_node;
20919 in_base_initializer = 0;
20920 }
20921
20922 if (decl)
20923 {
20924 init = build_tree_list (decl, init);
20925 TREE_CHAIN (init) = inits;
20926 inits = init;
20927 }
20928 }
20929 }
20930 return inits;
20931 }
20932
20933 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
20934
20935 static void
20936 set_current_access_from_decl (tree decl)
20937 {
20938 if (TREE_PRIVATE (decl))
20939 current_access_specifier = access_private_node;
20940 else if (TREE_PROTECTED (decl))
20941 current_access_specifier = access_protected_node;
20942 else
20943 current_access_specifier = access_public_node;
20944 }
20945
20946 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
20947 is the instantiation (which should have been created with
20948 start_enum) and ARGS are the template arguments to use. */
20949
20950 static void
20951 tsubst_enum (tree tag, tree newtag, tree args)
20952 {
20953 tree e;
20954
20955 if (SCOPED_ENUM_P (newtag))
20956 begin_scope (sk_scoped_enum, newtag);
20957
20958 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
20959 {
20960 tree value;
20961 tree decl;
20962
20963 decl = TREE_VALUE (e);
20964 /* Note that in a template enum, the TREE_VALUE is the
20965 CONST_DECL, not the corresponding INTEGER_CST. */
20966 value = tsubst_expr (DECL_INITIAL (decl),
20967 args, tf_warning_or_error, NULL_TREE,
20968 /*integral_constant_expression_p=*/true);
20969
20970 /* Give this enumeration constant the correct access. */
20971 set_current_access_from_decl (decl);
20972
20973 /* Actually build the enumerator itself. Here we're assuming that
20974 enumerators can't have dependent attributes. */
20975 build_enumerator (DECL_NAME (decl), value, newtag,
20976 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
20977 }
20978
20979 if (SCOPED_ENUM_P (newtag))
20980 finish_scope ();
20981
20982 finish_enum_value_list (newtag);
20983 finish_enum (newtag);
20984
20985 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20986 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20987 }
20988
20989 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20990 its type -- but without substituting the innermost set of template
20991 arguments. So, innermost set of template parameters will appear in
20992 the type. */
20993
20994 tree
20995 get_mostly_instantiated_function_type (tree decl)
20996 {
20997 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
20998 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
20999 }
21000
21001 /* Return truthvalue if we're processing a template different from
21002 the last one involved in diagnostics. */
21003 bool
21004 problematic_instantiation_changed (void)
21005 {
21006 return current_tinst_level != last_error_tinst_level;
21007 }
21008
21009 /* Remember current template involved in diagnostics. */
21010 void
21011 record_last_problematic_instantiation (void)
21012 {
21013 last_error_tinst_level = current_tinst_level;
21014 }
21015
21016 struct tinst_level *
21017 current_instantiation (void)
21018 {
21019 return current_tinst_level;
21020 }
21021
21022 /* Return TRUE if current_function_decl is being instantiated, false
21023 otherwise. */
21024
21025 bool
21026 instantiating_current_function_p (void)
21027 {
21028 return (current_instantiation ()
21029 && current_instantiation ()->decl == current_function_decl);
21030 }
21031
21032 /* [temp.param] Check that template non-type parm TYPE is of an allowable
21033 type. Return zero for ok, nonzero for disallowed. Issue error and
21034 warning messages under control of COMPLAIN. */
21035
21036 static int
21037 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
21038 {
21039 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
21040 return 0;
21041 else if (POINTER_TYPE_P (type))
21042 return 0;
21043 else if (TYPE_PTRMEM_P (type))
21044 return 0;
21045 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
21046 return 0;
21047 else if (TREE_CODE (type) == TYPENAME_TYPE)
21048 return 0;
21049 else if (TREE_CODE (type) == DECLTYPE_TYPE)
21050 return 0;
21051 else if (TREE_CODE (type) == NULLPTR_TYPE)
21052 return 0;
21053 /* A bound template template parm could later be instantiated to have a valid
21054 nontype parm type via an alias template. */
21055 else if (cxx_dialect >= cxx11
21056 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21057 return 0;
21058
21059 if (complain & tf_error)
21060 {
21061 if (type == error_mark_node)
21062 inform (input_location, "invalid template non-type parameter");
21063 else
21064 error ("%q#T is not a valid type for a template non-type parameter",
21065 type);
21066 }
21067 return 1;
21068 }
21069
21070 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
21071 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
21072
21073 static bool
21074 dependent_type_p_r (tree type)
21075 {
21076 tree scope;
21077
21078 /* [temp.dep.type]
21079
21080 A type is dependent if it is:
21081
21082 -- a template parameter. Template template parameters are types
21083 for us (since TYPE_P holds true for them) so we handle
21084 them here. */
21085 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21086 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
21087 return true;
21088 /* -- a qualified-id with a nested-name-specifier which contains a
21089 class-name that names a dependent type or whose unqualified-id
21090 names a dependent type. */
21091 if (TREE_CODE (type) == TYPENAME_TYPE)
21092 return true;
21093
21094 /* An alias template specialization can be dependent even if the
21095 resulting type is not. */
21096 if (dependent_alias_template_spec_p (type))
21097 return true;
21098
21099 /* -- a cv-qualified type where the cv-unqualified type is
21100 dependent.
21101 No code is necessary for this bullet; the code below handles
21102 cv-qualified types, and we don't want to strip aliases with
21103 TYPE_MAIN_VARIANT because of DR 1558. */
21104 /* -- a compound type constructed from any dependent type. */
21105 if (TYPE_PTRMEM_P (type))
21106 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
21107 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
21108 (type)));
21109 else if (TYPE_PTR_P (type)
21110 || TREE_CODE (type) == REFERENCE_TYPE)
21111 return dependent_type_p (TREE_TYPE (type));
21112 else if (TREE_CODE (type) == FUNCTION_TYPE
21113 || TREE_CODE (type) == METHOD_TYPE)
21114 {
21115 tree arg_type;
21116
21117 if (dependent_type_p (TREE_TYPE (type)))
21118 return true;
21119 for (arg_type = TYPE_ARG_TYPES (type);
21120 arg_type;
21121 arg_type = TREE_CHAIN (arg_type))
21122 if (dependent_type_p (TREE_VALUE (arg_type)))
21123 return true;
21124 return false;
21125 }
21126 /* -- an array type constructed from any dependent type or whose
21127 size is specified by a constant expression that is
21128 value-dependent.
21129
21130 We checked for type- and value-dependence of the bounds in
21131 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
21132 if (TREE_CODE (type) == ARRAY_TYPE)
21133 {
21134 if (TYPE_DOMAIN (type)
21135 && dependent_type_p (TYPE_DOMAIN (type)))
21136 return true;
21137 return dependent_type_p (TREE_TYPE (type));
21138 }
21139
21140 /* -- a template-id in which either the template name is a template
21141 parameter ... */
21142 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21143 return true;
21144 /* ... or any of the template arguments is a dependent type or
21145 an expression that is type-dependent or value-dependent. */
21146 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
21147 && (any_dependent_template_arguments_p
21148 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
21149 return true;
21150
21151 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
21152 dependent; if the argument of the `typeof' expression is not
21153 type-dependent, then it should already been have resolved. */
21154 if (TREE_CODE (type) == TYPEOF_TYPE
21155 || TREE_CODE (type) == DECLTYPE_TYPE
21156 || TREE_CODE (type) == UNDERLYING_TYPE)
21157 return true;
21158
21159 /* A template argument pack is dependent if any of its packed
21160 arguments are. */
21161 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
21162 {
21163 tree args = ARGUMENT_PACK_ARGS (type);
21164 int i, len = TREE_VEC_LENGTH (args);
21165 for (i = 0; i < len; ++i)
21166 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21167 return true;
21168 }
21169
21170 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
21171 be template parameters. */
21172 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
21173 return true;
21174
21175 /* The standard does not specifically mention types that are local
21176 to template functions or local classes, but they should be
21177 considered dependent too. For example:
21178
21179 template <int I> void f() {
21180 enum E { a = I };
21181 S<sizeof (E)> s;
21182 }
21183
21184 The size of `E' cannot be known until the value of `I' has been
21185 determined. Therefore, `E' must be considered dependent. */
21186 scope = TYPE_CONTEXT (type);
21187 if (scope && TYPE_P (scope))
21188 return dependent_type_p (scope);
21189 /* Don't use type_dependent_expression_p here, as it can lead
21190 to infinite recursion trying to determine whether a lambda
21191 nested in a lambda is dependent (c++/47687). */
21192 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
21193 && DECL_LANG_SPECIFIC (scope)
21194 && DECL_TEMPLATE_INFO (scope)
21195 && (any_dependent_template_arguments_p
21196 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
21197 return true;
21198
21199 /* Other types are non-dependent. */
21200 return false;
21201 }
21202
21203 /* Returns TRUE if TYPE is dependent, in the sense of
21204 [temp.dep.type]. Note that a NULL type is considered dependent. */
21205
21206 bool
21207 dependent_type_p (tree type)
21208 {
21209 /* If there are no template parameters in scope, then there can't be
21210 any dependent types. */
21211 if (!processing_template_decl)
21212 {
21213 /* If we are not processing a template, then nobody should be
21214 providing us with a dependent type. */
21215 gcc_assert (type);
21216 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
21217 return false;
21218 }
21219
21220 /* If the type is NULL, we have not computed a type for the entity
21221 in question; in that case, the type is dependent. */
21222 if (!type)
21223 return true;
21224
21225 /* Erroneous types can be considered non-dependent. */
21226 if (type == error_mark_node)
21227 return false;
21228
21229 /* If we have not already computed the appropriate value for TYPE,
21230 do so now. */
21231 if (!TYPE_DEPENDENT_P_VALID (type))
21232 {
21233 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
21234 TYPE_DEPENDENT_P_VALID (type) = 1;
21235 }
21236
21237 return TYPE_DEPENDENT_P (type);
21238 }
21239
21240 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
21241 lookup. In other words, a dependent type that is not the current
21242 instantiation. */
21243
21244 bool
21245 dependent_scope_p (tree scope)
21246 {
21247 return (scope && TYPE_P (scope) && dependent_type_p (scope)
21248 && !currently_open_class (scope));
21249 }
21250
21251 /* T is a SCOPE_REF; return whether we need to consider it
21252 instantiation-dependent so that we can check access at instantiation
21253 time even though we know which member it resolves to. */
21254
21255 static bool
21256 instantiation_dependent_scope_ref_p (tree t)
21257 {
21258 if (DECL_P (TREE_OPERAND (t, 1))
21259 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
21260 && accessible_in_template_p (TREE_OPERAND (t, 0),
21261 TREE_OPERAND (t, 1)))
21262 return false;
21263 else
21264 return true;
21265 }
21266
21267 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
21268 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
21269 expression. */
21270
21271 /* Note that this predicate is not appropriate for general expressions;
21272 only constant expressions (that satisfy potential_constant_expression)
21273 can be tested for value dependence. */
21274
21275 bool
21276 value_dependent_expression_p (tree expression)
21277 {
21278 if (!processing_template_decl)
21279 return false;
21280
21281 /* A name declared with a dependent type. */
21282 if (DECL_P (expression) && type_dependent_expression_p (expression))
21283 return true;
21284
21285 switch (TREE_CODE (expression))
21286 {
21287 case IDENTIFIER_NODE:
21288 /* A name that has not been looked up -- must be dependent. */
21289 return true;
21290
21291 case TEMPLATE_PARM_INDEX:
21292 /* A non-type template parm. */
21293 return true;
21294
21295 case CONST_DECL:
21296 /* A non-type template parm. */
21297 if (DECL_TEMPLATE_PARM_P (expression))
21298 return true;
21299 return value_dependent_expression_p (DECL_INITIAL (expression));
21300
21301 case VAR_DECL:
21302 /* A constant with literal type and is initialized
21303 with an expression that is value-dependent.
21304
21305 Note that a non-dependent parenthesized initializer will have
21306 already been replaced with its constant value, so if we see
21307 a TREE_LIST it must be dependent. */
21308 if (DECL_INITIAL (expression)
21309 && decl_constant_var_p (expression)
21310 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
21311 /* cp_finish_decl doesn't fold reference initializers. */
21312 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
21313 || value_dependent_expression_p (DECL_INITIAL (expression))))
21314 return true;
21315 return false;
21316
21317 case DYNAMIC_CAST_EXPR:
21318 case STATIC_CAST_EXPR:
21319 case CONST_CAST_EXPR:
21320 case REINTERPRET_CAST_EXPR:
21321 case CAST_EXPR:
21322 /* These expressions are value-dependent if the type to which
21323 the cast occurs is dependent or the expression being casted
21324 is value-dependent. */
21325 {
21326 tree type = TREE_TYPE (expression);
21327
21328 if (dependent_type_p (type))
21329 return true;
21330
21331 /* A functional cast has a list of operands. */
21332 expression = TREE_OPERAND (expression, 0);
21333 if (!expression)
21334 {
21335 /* If there are no operands, it must be an expression such
21336 as "int()". This should not happen for aggregate types
21337 because it would form non-constant expressions. */
21338 gcc_assert (cxx_dialect >= cxx11
21339 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
21340
21341 return false;
21342 }
21343
21344 if (TREE_CODE (expression) == TREE_LIST)
21345 return any_value_dependent_elements_p (expression);
21346
21347 return value_dependent_expression_p (expression);
21348 }
21349
21350 case SIZEOF_EXPR:
21351 if (SIZEOF_EXPR_TYPE_P (expression))
21352 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
21353 /* FALLTHRU */
21354 case ALIGNOF_EXPR:
21355 case TYPEID_EXPR:
21356 /* A `sizeof' expression is value-dependent if the operand is
21357 type-dependent or is a pack expansion. */
21358 expression = TREE_OPERAND (expression, 0);
21359 if (PACK_EXPANSION_P (expression))
21360 return true;
21361 else if (TYPE_P (expression))
21362 return dependent_type_p (expression);
21363 return instantiation_dependent_expression_p (expression);
21364
21365 case AT_ENCODE_EXPR:
21366 /* An 'encode' expression is value-dependent if the operand is
21367 type-dependent. */
21368 expression = TREE_OPERAND (expression, 0);
21369 return dependent_type_p (expression);
21370
21371 case NOEXCEPT_EXPR:
21372 expression = TREE_OPERAND (expression, 0);
21373 return instantiation_dependent_expression_p (expression);
21374
21375 case SCOPE_REF:
21376 /* All instantiation-dependent expressions should also be considered
21377 value-dependent. */
21378 return instantiation_dependent_scope_ref_p (expression);
21379
21380 case COMPONENT_REF:
21381 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
21382 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
21383
21384 case NONTYPE_ARGUMENT_PACK:
21385 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
21386 is value-dependent. */
21387 {
21388 tree values = ARGUMENT_PACK_ARGS (expression);
21389 int i, len = TREE_VEC_LENGTH (values);
21390
21391 for (i = 0; i < len; ++i)
21392 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
21393 return true;
21394
21395 return false;
21396 }
21397
21398 case TRAIT_EXPR:
21399 {
21400 tree type2 = TRAIT_EXPR_TYPE2 (expression);
21401 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
21402 || (type2 ? dependent_type_p (type2) : false));
21403 }
21404
21405 case MODOP_EXPR:
21406 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21407 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
21408
21409 case ARRAY_REF:
21410 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
21411 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
21412
21413 case ADDR_EXPR:
21414 {
21415 tree op = TREE_OPERAND (expression, 0);
21416 return (value_dependent_expression_p (op)
21417 || has_value_dependent_address (op));
21418 }
21419
21420 case CALL_EXPR:
21421 {
21422 tree fn = get_callee_fndecl (expression);
21423 int i, nargs;
21424 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
21425 return true;
21426 nargs = call_expr_nargs (expression);
21427 for (i = 0; i < nargs; ++i)
21428 {
21429 tree op = CALL_EXPR_ARG (expression, i);
21430 /* In a call to a constexpr member function, look through the
21431 implicit ADDR_EXPR on the object argument so that it doesn't
21432 cause the call to be considered value-dependent. We also
21433 look through it in potential_constant_expression. */
21434 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
21435 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21436 && TREE_CODE (op) == ADDR_EXPR)
21437 op = TREE_OPERAND (op, 0);
21438 if (value_dependent_expression_p (op))
21439 return true;
21440 }
21441 return false;
21442 }
21443
21444 case TEMPLATE_ID_EXPR:
21445 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
21446 type-dependent. */
21447 return type_dependent_expression_p (expression);
21448
21449 case CONSTRUCTOR:
21450 {
21451 unsigned ix;
21452 tree val;
21453 if (dependent_type_p (TREE_TYPE (expression)))
21454 return true;
21455 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
21456 if (value_dependent_expression_p (val))
21457 return true;
21458 return false;
21459 }
21460
21461 case STMT_EXPR:
21462 /* Treat a GNU statement expression as dependent to avoid crashing
21463 under instantiate_non_dependent_expr; it can't be constant. */
21464 return true;
21465
21466 default:
21467 /* A constant expression is value-dependent if any subexpression is
21468 value-dependent. */
21469 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
21470 {
21471 case tcc_reference:
21472 case tcc_unary:
21473 case tcc_comparison:
21474 case tcc_binary:
21475 case tcc_expression:
21476 case tcc_vl_exp:
21477 {
21478 int i, len = cp_tree_operand_length (expression);
21479
21480 for (i = 0; i < len; i++)
21481 {
21482 tree t = TREE_OPERAND (expression, i);
21483
21484 /* In some cases, some of the operands may be missing.l
21485 (For example, in the case of PREDECREMENT_EXPR, the
21486 amount to increment by may be missing.) That doesn't
21487 make the expression dependent. */
21488 if (t && value_dependent_expression_p (t))
21489 return true;
21490 }
21491 }
21492 break;
21493 default:
21494 break;
21495 }
21496 break;
21497 }
21498
21499 /* The expression is not value-dependent. */
21500 return false;
21501 }
21502
21503 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
21504 [temp.dep.expr]. Note that an expression with no type is
21505 considered dependent. Other parts of the compiler arrange for an
21506 expression with type-dependent subexpressions to have no type, so
21507 this function doesn't have to be fully recursive. */
21508
21509 bool
21510 type_dependent_expression_p (tree expression)
21511 {
21512 if (!processing_template_decl)
21513 return false;
21514
21515 if (expression == NULL_TREE || expression == error_mark_node)
21516 return false;
21517
21518 /* An unresolved name is always dependent. */
21519 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
21520 return true;
21521
21522 /* Some expression forms are never type-dependent. */
21523 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
21524 || TREE_CODE (expression) == SIZEOF_EXPR
21525 || TREE_CODE (expression) == ALIGNOF_EXPR
21526 || TREE_CODE (expression) == AT_ENCODE_EXPR
21527 || TREE_CODE (expression) == NOEXCEPT_EXPR
21528 || TREE_CODE (expression) == TRAIT_EXPR
21529 || TREE_CODE (expression) == TYPEID_EXPR
21530 || TREE_CODE (expression) == DELETE_EXPR
21531 || TREE_CODE (expression) == VEC_DELETE_EXPR
21532 || TREE_CODE (expression) == THROW_EXPR)
21533 return false;
21534
21535 /* The types of these expressions depends only on the type to which
21536 the cast occurs. */
21537 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
21538 || TREE_CODE (expression) == STATIC_CAST_EXPR
21539 || TREE_CODE (expression) == CONST_CAST_EXPR
21540 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
21541 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
21542 || TREE_CODE (expression) == CAST_EXPR)
21543 return dependent_type_p (TREE_TYPE (expression));
21544
21545 /* The types of these expressions depends only on the type created
21546 by the expression. */
21547 if (TREE_CODE (expression) == NEW_EXPR
21548 || TREE_CODE (expression) == VEC_NEW_EXPR)
21549 {
21550 /* For NEW_EXPR tree nodes created inside a template, either
21551 the object type itself or a TREE_LIST may appear as the
21552 operand 1. */
21553 tree type = TREE_OPERAND (expression, 1);
21554 if (TREE_CODE (type) == TREE_LIST)
21555 /* This is an array type. We need to check array dimensions
21556 as well. */
21557 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
21558 || value_dependent_expression_p
21559 (TREE_OPERAND (TREE_VALUE (type), 1));
21560 else
21561 return dependent_type_p (type);
21562 }
21563
21564 if (TREE_CODE (expression) == SCOPE_REF)
21565 {
21566 tree scope = TREE_OPERAND (expression, 0);
21567 tree name = TREE_OPERAND (expression, 1);
21568
21569 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
21570 contains an identifier associated by name lookup with one or more
21571 declarations declared with a dependent type, or...a
21572 nested-name-specifier or qualified-id that names a member of an
21573 unknown specialization. */
21574 return (type_dependent_expression_p (name)
21575 || dependent_scope_p (scope));
21576 }
21577
21578 if (TREE_CODE (expression) == FUNCTION_DECL
21579 && DECL_LANG_SPECIFIC (expression)
21580 && DECL_TEMPLATE_INFO (expression)
21581 && (any_dependent_template_arguments_p
21582 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
21583 return true;
21584
21585 if (TREE_CODE (expression) == TEMPLATE_DECL
21586 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
21587 return false;
21588
21589 if (TREE_CODE (expression) == STMT_EXPR)
21590 expression = stmt_expr_value_expr (expression);
21591
21592 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
21593 {
21594 tree elt;
21595 unsigned i;
21596
21597 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
21598 {
21599 if (type_dependent_expression_p (elt))
21600 return true;
21601 }
21602 return false;
21603 }
21604
21605 /* A static data member of the current instantiation with incomplete
21606 array type is type-dependent, as the definition and specializations
21607 can have different bounds. */
21608 if (VAR_P (expression)
21609 && DECL_CLASS_SCOPE_P (expression)
21610 && dependent_type_p (DECL_CONTEXT (expression))
21611 && VAR_HAD_UNKNOWN_BOUND (expression))
21612 return true;
21613
21614 /* An array of unknown bound depending on a variadic parameter, eg:
21615
21616 template<typename... Args>
21617 void foo (Args... args)
21618 {
21619 int arr[] = { args... };
21620 }
21621
21622 template<int... vals>
21623 void bar ()
21624 {
21625 int arr[] = { vals... };
21626 }
21627
21628 If the array has no length and has an initializer, it must be that
21629 we couldn't determine its length in cp_complete_array_type because
21630 it is dependent. */
21631 if (VAR_P (expression)
21632 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
21633 && !TYPE_DOMAIN (TREE_TYPE (expression))
21634 && DECL_INITIAL (expression))
21635 return true;
21636
21637 /* A variable template specialization is type-dependent if it has any
21638 dependent template arguments. */
21639 if (VAR_P (expression)
21640 && DECL_LANG_SPECIFIC (expression)
21641 && DECL_TEMPLATE_INFO (expression)
21642 && variable_template_p (DECL_TI_TEMPLATE (expression)))
21643 return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
21644
21645 /* Always dependent, on the number of arguments if nothing else. */
21646 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
21647 return true;
21648
21649 if (TREE_TYPE (expression) == unknown_type_node)
21650 {
21651 if (TREE_CODE (expression) == ADDR_EXPR)
21652 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
21653 if (TREE_CODE (expression) == COMPONENT_REF
21654 || TREE_CODE (expression) == OFFSET_REF)
21655 {
21656 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
21657 return true;
21658 expression = TREE_OPERAND (expression, 1);
21659 if (identifier_p (expression))
21660 return false;
21661 }
21662 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
21663 if (TREE_CODE (expression) == SCOPE_REF)
21664 return false;
21665
21666 if (BASELINK_P (expression))
21667 {
21668 if (BASELINK_OPTYPE (expression)
21669 && dependent_type_p (BASELINK_OPTYPE (expression)))
21670 return true;
21671 expression = BASELINK_FUNCTIONS (expression);
21672 }
21673
21674 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
21675 {
21676 if (any_dependent_template_arguments_p
21677 (TREE_OPERAND (expression, 1)))
21678 return true;
21679 expression = TREE_OPERAND (expression, 0);
21680 if (identifier_p (expression))
21681 return true;
21682 }
21683
21684 gcc_assert (TREE_CODE (expression) == OVERLOAD
21685 || TREE_CODE (expression) == FUNCTION_DECL);
21686
21687 while (expression)
21688 {
21689 if (type_dependent_expression_p (OVL_CURRENT (expression)))
21690 return true;
21691 expression = OVL_NEXT (expression);
21692 }
21693 return false;
21694 }
21695
21696 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
21697
21698 return (dependent_type_p (TREE_TYPE (expression)));
21699 }
21700
21701 /* walk_tree callback function for instantiation_dependent_expression_p,
21702 below. Returns non-zero if a dependent subexpression is found. */
21703
21704 static tree
21705 instantiation_dependent_r (tree *tp, int *walk_subtrees,
21706 void * /*data*/)
21707 {
21708 if (TYPE_P (*tp))
21709 {
21710 /* We don't have to worry about decltype currently because decltype
21711 of an instantiation-dependent expr is a dependent type. This
21712 might change depending on the resolution of DR 1172. */
21713 *walk_subtrees = false;
21714 return NULL_TREE;
21715 }
21716 enum tree_code code = TREE_CODE (*tp);
21717 switch (code)
21718 {
21719 /* Don't treat an argument list as dependent just because it has no
21720 TREE_TYPE. */
21721 case TREE_LIST:
21722 case TREE_VEC:
21723 return NULL_TREE;
21724
21725 case VAR_DECL:
21726 case CONST_DECL:
21727 /* A constant with a dependent initializer is dependent. */
21728 if (value_dependent_expression_p (*tp))
21729 return *tp;
21730 break;
21731
21732 case TEMPLATE_PARM_INDEX:
21733 return *tp;
21734
21735 /* Handle expressions with type operands. */
21736 case SIZEOF_EXPR:
21737 case ALIGNOF_EXPR:
21738 case TYPEID_EXPR:
21739 case AT_ENCODE_EXPR:
21740 {
21741 tree op = TREE_OPERAND (*tp, 0);
21742 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
21743 op = TREE_TYPE (op);
21744 if (TYPE_P (op))
21745 {
21746 if (dependent_type_p (op))
21747 return *tp;
21748 else
21749 {
21750 *walk_subtrees = false;
21751 return NULL_TREE;
21752 }
21753 }
21754 break;
21755 }
21756
21757 case TRAIT_EXPR:
21758 if (value_dependent_expression_p (*tp))
21759 return *tp;
21760 *walk_subtrees = false;
21761 return NULL_TREE;
21762
21763 case COMPONENT_REF:
21764 if (identifier_p (TREE_OPERAND (*tp, 1)))
21765 /* In a template, finish_class_member_access_expr creates a
21766 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
21767 type-dependent, so that we can check access control at
21768 instantiation time (PR 42277). See also Core issue 1273. */
21769 return *tp;
21770 break;
21771
21772 case SCOPE_REF:
21773 if (instantiation_dependent_scope_ref_p (*tp))
21774 return *tp;
21775 else
21776 break;
21777
21778 /* Treat statement-expressions as dependent. */
21779 case BIND_EXPR:
21780 return *tp;
21781
21782 default:
21783 break;
21784 }
21785
21786 if (type_dependent_expression_p (*tp))
21787 return *tp;
21788 else
21789 return NULL_TREE;
21790 }
21791
21792 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
21793 sense defined by the ABI:
21794
21795 "An expression is instantiation-dependent if it is type-dependent
21796 or value-dependent, or it has a subexpression that is type-dependent
21797 or value-dependent." */
21798
21799 bool
21800 instantiation_dependent_expression_p (tree expression)
21801 {
21802 tree result;
21803
21804 if (!processing_template_decl)
21805 return false;
21806
21807 if (expression == error_mark_node)
21808 return false;
21809
21810 result = cp_walk_tree_without_duplicates (&expression,
21811 instantiation_dependent_r, NULL);
21812 return result != NULL_TREE;
21813 }
21814
21815 /* Like type_dependent_expression_p, but it also works while not processing
21816 a template definition, i.e. during substitution or mangling. */
21817
21818 bool
21819 type_dependent_expression_p_push (tree expr)
21820 {
21821 bool b;
21822 ++processing_template_decl;
21823 b = type_dependent_expression_p (expr);
21824 --processing_template_decl;
21825 return b;
21826 }
21827
21828 /* Returns TRUE if ARGS contains a type-dependent expression. */
21829
21830 bool
21831 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
21832 {
21833 unsigned int i;
21834 tree arg;
21835
21836 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
21837 {
21838 if (type_dependent_expression_p (arg))
21839 return true;
21840 }
21841 return false;
21842 }
21843
21844 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21845 expressions) contains any type-dependent expressions. */
21846
21847 bool
21848 any_type_dependent_elements_p (const_tree list)
21849 {
21850 for (; list; list = TREE_CHAIN (list))
21851 if (type_dependent_expression_p (TREE_VALUE (list)))
21852 return true;
21853
21854 return false;
21855 }
21856
21857 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
21858 expressions) contains any value-dependent expressions. */
21859
21860 bool
21861 any_value_dependent_elements_p (const_tree list)
21862 {
21863 for (; list; list = TREE_CHAIN (list))
21864 if (value_dependent_expression_p (TREE_VALUE (list)))
21865 return true;
21866
21867 return false;
21868 }
21869
21870 /* Returns TRUE if the ARG (a template argument) is dependent. */
21871
21872 bool
21873 dependent_template_arg_p (tree arg)
21874 {
21875 if (!processing_template_decl)
21876 return false;
21877
21878 /* Assume a template argument that was wrongly written by the user
21879 is dependent. This is consistent with what
21880 any_dependent_template_arguments_p [that calls this function]
21881 does. */
21882 if (!arg || arg == error_mark_node)
21883 return true;
21884
21885 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
21886 arg = ARGUMENT_PACK_SELECT_ARG (arg);
21887
21888 if (TREE_CODE (arg) == TEMPLATE_DECL
21889 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
21890 return dependent_template_p (arg);
21891 else if (ARGUMENT_PACK_P (arg))
21892 {
21893 tree args = ARGUMENT_PACK_ARGS (arg);
21894 int i, len = TREE_VEC_LENGTH (args);
21895 for (i = 0; i < len; ++i)
21896 {
21897 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
21898 return true;
21899 }
21900
21901 return false;
21902 }
21903 else if (TYPE_P (arg))
21904 return dependent_type_p (arg);
21905 else
21906 return (type_dependent_expression_p (arg)
21907 || value_dependent_expression_p (arg));
21908 }
21909
21910 /* Returns true if ARGS (a collection of template arguments) contains
21911 any types that require structural equality testing. */
21912
21913 bool
21914 any_template_arguments_need_structural_equality_p (tree args)
21915 {
21916 int i;
21917 int j;
21918
21919 if (!args)
21920 return false;
21921 if (args == error_mark_node)
21922 return true;
21923
21924 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21925 {
21926 tree level = TMPL_ARGS_LEVEL (args, i + 1);
21927 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21928 {
21929 tree arg = TREE_VEC_ELT (level, j);
21930 tree packed_args = NULL_TREE;
21931 int k, len = 1;
21932
21933 if (ARGUMENT_PACK_P (arg))
21934 {
21935 /* Look inside the argument pack. */
21936 packed_args = ARGUMENT_PACK_ARGS (arg);
21937 len = TREE_VEC_LENGTH (packed_args);
21938 }
21939
21940 for (k = 0; k < len; ++k)
21941 {
21942 if (packed_args)
21943 arg = TREE_VEC_ELT (packed_args, k);
21944
21945 if (error_operand_p (arg))
21946 return true;
21947 else if (TREE_CODE (arg) == TEMPLATE_DECL)
21948 continue;
21949 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
21950 return true;
21951 else if (!TYPE_P (arg) && TREE_TYPE (arg)
21952 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
21953 return true;
21954 }
21955 }
21956 }
21957
21958 return false;
21959 }
21960
21961 /* Returns true if ARGS (a collection of template arguments) contains
21962 any dependent arguments. */
21963
21964 bool
21965 any_dependent_template_arguments_p (const_tree args)
21966 {
21967 int i;
21968 int j;
21969
21970 if (!args)
21971 return false;
21972 if (args == error_mark_node)
21973 return true;
21974
21975 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21976 {
21977 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21978 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21979 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21980 return true;
21981 }
21982
21983 return false;
21984 }
21985
21986 /* Returns TRUE if the template TMPL is dependent. */
21987
21988 bool
21989 dependent_template_p (tree tmpl)
21990 {
21991 if (TREE_CODE (tmpl) == OVERLOAD)
21992 {
21993 while (tmpl)
21994 {
21995 if (dependent_template_p (OVL_CURRENT (tmpl)))
21996 return true;
21997 tmpl = OVL_NEXT (tmpl);
21998 }
21999 return false;
22000 }
22001
22002 /* Template template parameters are dependent. */
22003 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
22004 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
22005 return true;
22006 /* So are names that have not been looked up. */
22007 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
22008 return true;
22009 /* So are member templates of dependent classes. */
22010 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
22011 return dependent_type_p (DECL_CONTEXT (tmpl));
22012 return false;
22013 }
22014
22015 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
22016
22017 bool
22018 dependent_template_id_p (tree tmpl, tree args)
22019 {
22020 return (dependent_template_p (tmpl)
22021 || any_dependent_template_arguments_p (args));
22022 }
22023
22024 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
22025 is dependent. */
22026
22027 bool
22028 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
22029 {
22030 int i;
22031
22032 if (!processing_template_decl)
22033 return false;
22034
22035 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
22036 {
22037 tree decl = TREE_VEC_ELT (declv, i);
22038 tree init = TREE_VEC_ELT (initv, i);
22039 tree cond = TREE_VEC_ELT (condv, i);
22040 tree incr = TREE_VEC_ELT (incrv, i);
22041
22042 if (type_dependent_expression_p (decl))
22043 return true;
22044
22045 if (init && type_dependent_expression_p (init))
22046 return true;
22047
22048 if (type_dependent_expression_p (cond))
22049 return true;
22050
22051 if (COMPARISON_CLASS_P (cond)
22052 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
22053 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
22054 return true;
22055
22056 if (TREE_CODE (incr) == MODOP_EXPR)
22057 {
22058 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
22059 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
22060 return true;
22061 }
22062 else if (type_dependent_expression_p (incr))
22063 return true;
22064 else if (TREE_CODE (incr) == MODIFY_EXPR)
22065 {
22066 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
22067 return true;
22068 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
22069 {
22070 tree t = TREE_OPERAND (incr, 1);
22071 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
22072 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
22073 return true;
22074 }
22075 }
22076 }
22077
22078 return false;
22079 }
22080
22081 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
22082 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
22083 no such TYPE can be found. Note that this function peers inside
22084 uninstantiated templates and therefore should be used only in
22085 extremely limited situations. ONLY_CURRENT_P restricts this
22086 peering to the currently open classes hierarchy (which is required
22087 when comparing types). */
22088
22089 tree
22090 resolve_typename_type (tree type, bool only_current_p)
22091 {
22092 tree scope;
22093 tree name;
22094 tree decl;
22095 int quals;
22096 tree pushed_scope;
22097 tree result;
22098
22099 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
22100
22101 scope = TYPE_CONTEXT (type);
22102 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
22103 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
22104 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
22105 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
22106 identifier of the TYPENAME_TYPE anymore.
22107 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
22108 TYPENAME_TYPE instead, we avoid messing up with a possible
22109 typedef variant case. */
22110 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
22111
22112 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
22113 it first before we can figure out what NAME refers to. */
22114 if (TREE_CODE (scope) == TYPENAME_TYPE)
22115 {
22116 if (TYPENAME_IS_RESOLVING_P (scope))
22117 /* Given a class template A with a dependent base with nested type C,
22118 typedef typename A::C::C C will land us here, as trying to resolve
22119 the initial A::C leads to the local C typedef, which leads back to
22120 A::C::C. So we break the recursion now. */
22121 return type;
22122 else
22123 scope = resolve_typename_type (scope, only_current_p);
22124 }
22125 /* If we don't know what SCOPE refers to, then we cannot resolve the
22126 TYPENAME_TYPE. */
22127 if (TREE_CODE (scope) == TYPENAME_TYPE)
22128 return type;
22129 /* If the SCOPE is a template type parameter, we have no way of
22130 resolving the name. */
22131 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
22132 return type;
22133 /* If the SCOPE is not the current instantiation, there's no reason
22134 to look inside it. */
22135 if (only_current_p && !currently_open_class (scope))
22136 return type;
22137 /* If this is a typedef, we don't want to look inside (c++/11987). */
22138 if (typedef_variant_p (type))
22139 return type;
22140 /* If SCOPE isn't the template itself, it will not have a valid
22141 TYPE_FIELDS list. */
22142 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
22143 /* scope is either the template itself or a compatible instantiation
22144 like X<T>, so look up the name in the original template. */
22145 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
22146 else
22147 /* scope is a partial instantiation, so we can't do the lookup or we
22148 will lose the template arguments. */
22149 return type;
22150 /* Enter the SCOPE so that name lookup will be resolved as if we
22151 were in the class definition. In particular, SCOPE will no
22152 longer be considered a dependent type. */
22153 pushed_scope = push_scope (scope);
22154 /* Look up the declaration. */
22155 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
22156 tf_warning_or_error);
22157
22158 result = NULL_TREE;
22159
22160 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
22161 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
22162 if (!decl)
22163 /*nop*/;
22164 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
22165 && TREE_CODE (decl) == TYPE_DECL)
22166 {
22167 result = TREE_TYPE (decl);
22168 if (result == error_mark_node)
22169 result = NULL_TREE;
22170 }
22171 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
22172 && DECL_CLASS_TEMPLATE_P (decl))
22173 {
22174 tree tmpl;
22175 tree args;
22176 /* Obtain the template and the arguments. */
22177 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
22178 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
22179 /* Instantiate the template. */
22180 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
22181 /*entering_scope=*/0,
22182 tf_error | tf_user);
22183 if (result == error_mark_node)
22184 result = NULL_TREE;
22185 }
22186
22187 /* Leave the SCOPE. */
22188 if (pushed_scope)
22189 pop_scope (pushed_scope);
22190
22191 /* If we failed to resolve it, return the original typename. */
22192 if (!result)
22193 return type;
22194
22195 /* If lookup found a typename type, resolve that too. */
22196 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
22197 {
22198 /* Ill-formed programs can cause infinite recursion here, so we
22199 must catch that. */
22200 TYPENAME_IS_RESOLVING_P (type) = 1;
22201 result = resolve_typename_type (result, only_current_p);
22202 TYPENAME_IS_RESOLVING_P (type) = 0;
22203 }
22204
22205 /* Qualify the resulting type. */
22206 quals = cp_type_quals (type);
22207 if (quals)
22208 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
22209
22210 return result;
22211 }
22212
22213 /* EXPR is an expression which is not type-dependent. Return a proxy
22214 for EXPR that can be used to compute the types of larger
22215 expressions containing EXPR. */
22216
22217 tree
22218 build_non_dependent_expr (tree expr)
22219 {
22220 tree inner_expr;
22221
22222 #ifdef ENABLE_CHECKING
22223 /* Try to get a constant value for all non-dependent expressions in
22224 order to expose bugs in *_dependent_expression_p and constexpr. */
22225 if (cxx_dialect >= cxx11)
22226 fold_non_dependent_expr (expr);
22227 #endif
22228
22229 /* Preserve OVERLOADs; the functions must be available to resolve
22230 types. */
22231 inner_expr = expr;
22232 if (TREE_CODE (inner_expr) == STMT_EXPR)
22233 inner_expr = stmt_expr_value_expr (inner_expr);
22234 if (TREE_CODE (inner_expr) == ADDR_EXPR)
22235 inner_expr = TREE_OPERAND (inner_expr, 0);
22236 if (TREE_CODE (inner_expr) == COMPONENT_REF)
22237 inner_expr = TREE_OPERAND (inner_expr, 1);
22238 if (is_overloaded_fn (inner_expr)
22239 || TREE_CODE (inner_expr) == OFFSET_REF)
22240 return expr;
22241 /* There is no need to return a proxy for a variable. */
22242 if (VAR_P (expr))
22243 return expr;
22244 /* Preserve string constants; conversions from string constants to
22245 "char *" are allowed, even though normally a "const char *"
22246 cannot be used to initialize a "char *". */
22247 if (TREE_CODE (expr) == STRING_CST)
22248 return expr;
22249 /* Preserve void and arithmetic constants, as an optimization -- there is no
22250 reason to create a new node. */
22251 if (TREE_CODE (expr) == VOID_CST
22252 || TREE_CODE (expr) == INTEGER_CST
22253 || TREE_CODE (expr) == REAL_CST)
22254 return expr;
22255 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
22256 There is at least one place where we want to know that a
22257 particular expression is a throw-expression: when checking a ?:
22258 expression, there are special rules if the second or third
22259 argument is a throw-expression. */
22260 if (TREE_CODE (expr) == THROW_EXPR)
22261 return expr;
22262
22263 /* Don't wrap an initializer list, we need to be able to look inside. */
22264 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
22265 return expr;
22266
22267 /* Don't wrap a dummy object, we need to be able to test for it. */
22268 if (is_dummy_object (expr))
22269 return expr;
22270
22271 if (TREE_CODE (expr) == COND_EXPR)
22272 return build3 (COND_EXPR,
22273 TREE_TYPE (expr),
22274 TREE_OPERAND (expr, 0),
22275 (TREE_OPERAND (expr, 1)
22276 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
22277 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
22278 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
22279 if (TREE_CODE (expr) == COMPOUND_EXPR
22280 && !COMPOUND_EXPR_OVERLOADED (expr))
22281 return build2 (COMPOUND_EXPR,
22282 TREE_TYPE (expr),
22283 TREE_OPERAND (expr, 0),
22284 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
22285
22286 /* If the type is unknown, it can't really be non-dependent */
22287 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
22288
22289 /* Otherwise, build a NON_DEPENDENT_EXPR. */
22290 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
22291 }
22292
22293 /* ARGS is a vector of expressions as arguments to a function call.
22294 Replace the arguments with equivalent non-dependent expressions.
22295 This modifies ARGS in place. */
22296
22297 void
22298 make_args_non_dependent (vec<tree, va_gc> *args)
22299 {
22300 unsigned int ix;
22301 tree arg;
22302
22303 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
22304 {
22305 tree newarg = build_non_dependent_expr (arg);
22306 if (newarg != arg)
22307 (*args)[ix] = newarg;
22308 }
22309 }
22310
22311 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
22312 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
22313 parms. */
22314
22315 static tree
22316 make_auto_1 (tree name)
22317 {
22318 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
22319 TYPE_NAME (au) = build_decl (input_location,
22320 TYPE_DECL, name, au);
22321 TYPE_STUB_DECL (au) = TYPE_NAME (au);
22322 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
22323 (0, processing_template_decl + 1, processing_template_decl + 1,
22324 TYPE_NAME (au), NULL_TREE);
22325 TYPE_CANONICAL (au) = canonical_type_parameter (au);
22326 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
22327 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
22328
22329 return au;
22330 }
22331
22332 tree
22333 make_decltype_auto (void)
22334 {
22335 return make_auto_1 (get_identifier ("decltype(auto)"));
22336 }
22337
22338 tree
22339 make_auto (void)
22340 {
22341 return make_auto_1 (get_identifier ("auto"));
22342 }
22343
22344 /* Given type ARG, return std::initializer_list<ARG>. */
22345
22346 static tree
22347 listify (tree arg)
22348 {
22349 tree std_init_list = namespace_binding
22350 (get_identifier ("initializer_list"), std_node);
22351 tree argvec;
22352 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
22353 {
22354 error ("deducing from brace-enclosed initializer list requires "
22355 "#include <initializer_list>");
22356 return error_mark_node;
22357 }
22358 argvec = make_tree_vec (1);
22359 TREE_VEC_ELT (argvec, 0) = arg;
22360 return lookup_template_class (std_init_list, argvec, NULL_TREE,
22361 NULL_TREE, 0, tf_warning_or_error);
22362 }
22363
22364 /* Replace auto in TYPE with std::initializer_list<auto>. */
22365
22366 static tree
22367 listify_autos (tree type, tree auto_node)
22368 {
22369 tree init_auto = listify (auto_node);
22370 tree argvec = make_tree_vec (1);
22371 TREE_VEC_ELT (argvec, 0) = init_auto;
22372 if (processing_template_decl)
22373 argvec = add_to_template_args (current_template_args (), argvec);
22374 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
22375 }
22376
22377 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
22378 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
22379
22380 tree
22381 do_auto_deduction (tree type, tree init, tree auto_node)
22382 {
22383 tree targs;
22384
22385 if (init == error_mark_node)
22386 return error_mark_node;
22387
22388 if (type_dependent_expression_p (init))
22389 /* Defining a subset of type-dependent expressions that we can deduce
22390 from ahead of time isn't worth the trouble. */
22391 return type;
22392
22393 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
22394 with either a new invented type template parameter U or, if the
22395 initializer is a braced-init-list (8.5.4), with
22396 std::initializer_list<U>. */
22397 if (BRACE_ENCLOSED_INITIALIZER_P (init))
22398 {
22399 if (!DIRECT_LIST_INIT_P (init))
22400 type = listify_autos (type, auto_node);
22401 else if (CONSTRUCTOR_NELTS (init) == 1)
22402 init = CONSTRUCTOR_ELT (init, 0)->value;
22403 else
22404 {
22405 if (permerror (input_location, "direct-list-initialization of "
22406 "%<auto%> requires exactly one element"))
22407 inform (input_location,
22408 "for deduction to %<std::initializer_list%>, use copy-"
22409 "list-initialization (i.e. add %<=%> before the %<{%>)");
22410 type = listify_autos (type, auto_node);
22411 }
22412 }
22413
22414 init = resolve_nondeduced_context (init);
22415
22416 targs = make_tree_vec (1);
22417 if (AUTO_IS_DECLTYPE (auto_node))
22418 {
22419 bool id = (DECL_P (init) || (TREE_CODE (init) == COMPONENT_REF
22420 && !REF_PARENTHESIZED_P (init)));
22421 TREE_VEC_ELT (targs, 0)
22422 = finish_decltype_type (init, id, tf_warning_or_error);
22423 if (type != auto_node)
22424 {
22425 error ("%qT as type rather than plain %<decltype(auto)%>", type);
22426 return error_mark_node;
22427 }
22428 }
22429 else
22430 {
22431 tree parms = build_tree_list (NULL_TREE, type);
22432 tree tparms = make_tree_vec (1);
22433 int val;
22434
22435 TREE_VEC_ELT (tparms, 0)
22436 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
22437 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
22438 DEDUCE_CALL, LOOKUP_NORMAL,
22439 NULL, /*explain_p=*/false);
22440 if (val > 0)
22441 {
22442 if (processing_template_decl)
22443 /* Try again at instantiation time. */
22444 return type;
22445 if (type && type != error_mark_node)
22446 /* If type is error_mark_node a diagnostic must have been
22447 emitted by now. Also, having a mention to '<type error>'
22448 in the diagnostic is not really useful to the user. */
22449 {
22450 if (cfun && auto_node == current_function_auto_return_pattern
22451 && LAMBDA_FUNCTION_P (current_function_decl))
22452 error ("unable to deduce lambda return type from %qE", init);
22453 else
22454 error ("unable to deduce %qT from %qE", type, init);
22455 }
22456 return error_mark_node;
22457 }
22458 }
22459
22460 /* If the list of declarators contains more than one declarator, the type
22461 of each declared variable is determined as described above. If the
22462 type deduced for the template parameter U is not the same in each
22463 deduction, the program is ill-formed. */
22464 if (TREE_TYPE (auto_node)
22465 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
22466 {
22467 if (cfun && auto_node == current_function_auto_return_pattern
22468 && LAMBDA_FUNCTION_P (current_function_decl))
22469 error ("inconsistent types %qT and %qT deduced for "
22470 "lambda return type", TREE_TYPE (auto_node),
22471 TREE_VEC_ELT (targs, 0));
22472 else
22473 error ("inconsistent deduction for %qT: %qT and then %qT",
22474 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
22475 return error_mark_node;
22476 }
22477 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
22478
22479 if (processing_template_decl)
22480 targs = add_to_template_args (current_template_args (), targs);
22481 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
22482 }
22483
22484 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
22485 result. */
22486
22487 tree
22488 splice_late_return_type (tree type, tree late_return_type)
22489 {
22490 if (is_auto (type))
22491 {
22492 if (late_return_type)
22493 return late_return_type;
22494
22495 tree idx = get_template_parm_index (type);
22496 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
22497 /* In an abbreviated function template we didn't know we were dealing
22498 with a function template when we saw the auto return type, so update
22499 it to have the correct level. */
22500 return make_auto_1 (TYPE_IDENTIFIER (type));
22501 }
22502 return type;
22503 }
22504
22505 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
22506 'decltype(auto)'. */
22507
22508 bool
22509 is_auto (const_tree type)
22510 {
22511 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
22512 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
22513 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
22514 return true;
22515 else
22516 return false;
22517 }
22518
22519 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
22520 a use of `auto'. Returns NULL_TREE otherwise. */
22521
22522 tree
22523 type_uses_auto (tree type)
22524 {
22525 return find_type_usage (type, is_auto);
22526 }
22527
22528 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
22529 'decltype(auto)' or a concept. */
22530
22531 bool
22532 is_auto_or_concept (const_tree type)
22533 {
22534 return is_auto (type); // or concept
22535 }
22536
22537 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
22538 a concept identifier) iff TYPE contains a use of a generic type. Returns
22539 NULL_TREE otherwise. */
22540
22541 tree
22542 type_uses_auto_or_concept (tree type)
22543 {
22544 return find_type_usage (type, is_auto_or_concept);
22545 }
22546
22547
22548 /* For a given template T, return the vector of typedefs referenced
22549 in T for which access check is needed at T instantiation time.
22550 T is either a FUNCTION_DECL or a RECORD_TYPE.
22551 Those typedefs were added to T by the function
22552 append_type_to_template_for_access_check. */
22553
22554 vec<qualified_typedef_usage_t, va_gc> *
22555 get_types_needing_access_check (tree t)
22556 {
22557 tree ti;
22558 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
22559
22560 if (!t || t == error_mark_node)
22561 return NULL;
22562
22563 if (!(ti = get_template_info (t)))
22564 return NULL;
22565
22566 if (CLASS_TYPE_P (t)
22567 || TREE_CODE (t) == FUNCTION_DECL)
22568 {
22569 if (!TI_TEMPLATE (ti))
22570 return NULL;
22571
22572 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
22573 }
22574
22575 return result;
22576 }
22577
22578 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
22579 tied to T. That list of typedefs will be access checked at
22580 T instantiation time.
22581 T is either a FUNCTION_DECL or a RECORD_TYPE.
22582 TYPE_DECL is a TYPE_DECL node representing a typedef.
22583 SCOPE is the scope through which TYPE_DECL is accessed.
22584 LOCATION is the location of the usage point of TYPE_DECL.
22585
22586 This function is a subroutine of
22587 append_type_to_template_for_access_check. */
22588
22589 static void
22590 append_type_to_template_for_access_check_1 (tree t,
22591 tree type_decl,
22592 tree scope,
22593 location_t location)
22594 {
22595 qualified_typedef_usage_t typedef_usage;
22596 tree ti;
22597
22598 if (!t || t == error_mark_node)
22599 return;
22600
22601 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
22602 || CLASS_TYPE_P (t))
22603 && type_decl
22604 && TREE_CODE (type_decl) == TYPE_DECL
22605 && scope);
22606
22607 if (!(ti = get_template_info (t)))
22608 return;
22609
22610 gcc_assert (TI_TEMPLATE (ti));
22611
22612 typedef_usage.typedef_decl = type_decl;
22613 typedef_usage.context = scope;
22614 typedef_usage.locus = location;
22615
22616 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
22617 }
22618
22619 /* Append TYPE_DECL to the template TEMPL.
22620 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
22621 At TEMPL instanciation time, TYPE_DECL will be checked to see
22622 if it can be accessed through SCOPE.
22623 LOCATION is the location of the usage point of TYPE_DECL.
22624
22625 e.g. consider the following code snippet:
22626
22627 class C
22628 {
22629 typedef int myint;
22630 };
22631
22632 template<class U> struct S
22633 {
22634 C::myint mi; // <-- usage point of the typedef C::myint
22635 };
22636
22637 S<char> s;
22638
22639 At S<char> instantiation time, we need to check the access of C::myint
22640 In other words, we need to check the access of the myint typedef through
22641 the C scope. For that purpose, this function will add the myint typedef
22642 and the scope C through which its being accessed to a list of typedefs
22643 tied to the template S. That list will be walked at template instantiation
22644 time and access check performed on each typedefs it contains.
22645 Note that this particular code snippet should yield an error because
22646 myint is private to C. */
22647
22648 void
22649 append_type_to_template_for_access_check (tree templ,
22650 tree type_decl,
22651 tree scope,
22652 location_t location)
22653 {
22654 qualified_typedef_usage_t *iter;
22655 unsigned i;
22656
22657 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
22658
22659 /* Make sure we don't append the type to the template twice. */
22660 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
22661 if (iter->typedef_decl == type_decl && scope == iter->context)
22662 return;
22663
22664 append_type_to_template_for_access_check_1 (templ, type_decl,
22665 scope, location);
22666 }
22667
22668 /* Convert the generic type parameters in PARM that match the types given in the
22669 range [START_IDX, END_IDX) from the current_template_parms into generic type
22670 packs. */
22671
22672 tree
22673 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
22674 {
22675 tree current = current_template_parms;
22676 int depth = TMPL_PARMS_DEPTH (current);
22677 current = INNERMOST_TEMPLATE_PARMS (current);
22678 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
22679
22680 for (int i = 0; i < start_idx; ++i)
22681 TREE_VEC_ELT (replacement, i)
22682 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22683
22684 for (int i = start_idx; i < end_idx; ++i)
22685 {
22686 /* Create a distinct parameter pack type from the current parm and add it
22687 to the replacement args to tsubst below into the generic function
22688 parameter. */
22689
22690 tree o = TREE_TYPE (TREE_VALUE
22691 (TREE_VEC_ELT (current, i)));
22692 tree t = copy_type (o);
22693 TEMPLATE_TYPE_PARM_INDEX (t)
22694 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
22695 o, 0, 0, tf_none);
22696 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
22697 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
22698 TYPE_MAIN_VARIANT (t) = t;
22699 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
22700 TYPE_CANONICAL (t) = canonical_type_parameter (t);
22701 TREE_VEC_ELT (replacement, i) = t;
22702 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
22703 }
22704
22705 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
22706 TREE_VEC_ELT (replacement, i)
22707 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
22708
22709 /* If there are more levels then build up the replacement with the outer
22710 template parms. */
22711 if (depth > 1)
22712 replacement = add_to_template_args (template_parms_to_args
22713 (TREE_CHAIN (current_template_parms)),
22714 replacement);
22715
22716 return tsubst (parm, replacement, tf_none, NULL_TREE);
22717 }
22718
22719
22720 /* Set up the hash tables for template instantiations. */
22721
22722 void
22723 init_template_processing (void)
22724 {
22725 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
22726 type_specializations = hash_table<spec_hasher>::create_ggc (37);
22727 }
22728
22729 /* Print stats about the template hash tables for -fstats. */
22730
22731 void
22732 print_template_statistics (void)
22733 {
22734 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
22735 "%f collisions\n", (long) decl_specializations->size (),
22736 (long) decl_specializations->elements (),
22737 decl_specializations->collisions ());
22738 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
22739 "%f collisions\n", (long) type_specializations->size (),
22740 (long) type_specializations->elements (),
22741 type_specializations->collisions ());
22742 }
22743
22744 #include "gt-cp-pt.h"