re PR c++/58504 (ICE with type trait as default template parameter)
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2014 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 "tree.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "pointer-set.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 /* A map from local variable declarations in the body of the template
82 presently being instantiated to the corresponding instantiated
83 local variables. */
84 static struct pointer_map_t *local_specializations;
85
86 /* True if we've recursed into fn_type_unification too many times. */
87 static bool excessive_deduction_depth;
88
89 typedef struct GTY(()) spec_entry
90 {
91 tree tmpl;
92 tree args;
93 tree spec;
94 } spec_entry;
95
96 static GTY ((param_is (spec_entry)))
97 htab_t decl_specializations;
98
99 static GTY ((param_is (spec_entry)))
100 htab_t type_specializations;
101
102 /* Contains canonical template parameter types. The vector is indexed by
103 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
104 TREE_LIST, whose TREE_VALUEs contain the canonical template
105 parameters of various types and levels. */
106 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
107
108 #define UNIFY_ALLOW_NONE 0
109 #define UNIFY_ALLOW_MORE_CV_QUAL 1
110 #define UNIFY_ALLOW_LESS_CV_QUAL 2
111 #define UNIFY_ALLOW_DERIVED 4
112 #define UNIFY_ALLOW_INTEGER 8
113 #define UNIFY_ALLOW_OUTER_LEVEL 16
114 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
115 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
116
117 enum template_base_result {
118 tbr_incomplete_type,
119 tbr_ambiguous_baseclass,
120 tbr_success
121 };
122
123 static void push_access_scope (tree);
124 static void pop_access_scope (tree);
125 static bool resolve_overloaded_unification (tree, tree, tree, tree,
126 unification_kind_t, int,
127 bool);
128 static int try_one_overload (tree, tree, tree, tree, tree,
129 unification_kind_t, int, bool, bool);
130 static int unify (tree, tree, tree, tree, int, bool);
131 static void add_pending_template (tree);
132 static tree reopen_tinst_level (struct tinst_level *);
133 static tree tsubst_initializer_list (tree, tree);
134 static tree get_class_bindings (tree, tree, tree, tree);
135 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
136 bool, bool);
137 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
138 bool, bool);
139 static void tsubst_enum (tree, tree, tree);
140 static tree add_to_template_args (tree, tree);
141 static tree add_outermost_template_args (tree, tree);
142 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
143 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
144 tree);
145 static int type_unification_real (tree, tree, tree, const tree *,
146 unsigned int, int, unification_kind_t, int,
147 vec<deferred_access_check, va_gc> **,
148 bool);
149 static void note_template_header (int);
150 static tree convert_nontype_argument_function (tree, tree);
151 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
152 static tree convert_template_argument (tree, tree, tree,
153 tsubst_flags_t, int, tree);
154 static int for_each_template_parm (tree, tree_fn_t, void*,
155 struct pointer_set_t*, bool);
156 static tree expand_template_argument_pack (tree);
157 static tree build_template_parm_index (int, int, int, tree, tree);
158 static bool inline_needs_template_parms (tree, bool);
159 static void push_inline_template_parms_recursive (tree, int);
160 static tree retrieve_local_specialization (tree);
161 static void register_local_specialization (tree, tree);
162 static hashval_t hash_specialization (const void *p);
163 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
164 static int mark_template_parm (tree, void *);
165 static int template_parm_this_level_p (tree, void *);
166 static tree tsubst_friend_function (tree, tree);
167 static tree tsubst_friend_class (tree, tree);
168 static int can_complete_type_without_circularity (tree);
169 static tree get_bindings (tree, tree, tree, bool);
170 static int template_decl_level (tree);
171 static int check_cv_quals_for_unify (int, tree, tree);
172 static void template_parm_level_and_index (tree, int*, int*);
173 static int unify_pack_expansion (tree, tree, tree,
174 tree, unification_kind_t, bool, bool);
175 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
176 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
177 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
178 static void regenerate_decl_from_template (tree, tree);
179 static tree most_specialized_class (tree, tsubst_flags_t);
180 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
181 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
182 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
183 static bool check_specialization_scope (void);
184 static tree process_partial_specialization (tree);
185 static void set_current_access_from_decl (tree);
186 static enum template_base_result get_template_base (tree, tree, tree, tree,
187 bool , tree *);
188 static tree try_class_unification (tree, tree, tree, tree, bool);
189 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
190 tree, tree);
191 static bool template_template_parm_bindings_ok_p (tree, tree);
192 static int template_args_equal (tree, tree);
193 static void tsubst_default_arguments (tree, tsubst_flags_t);
194 static tree for_each_template_parm_r (tree *, int *, void *);
195 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
196 static void copy_default_args_to_explicit_spec (tree);
197 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
198 static bool dependent_template_arg_p (tree);
199 static bool any_template_arguments_need_structural_equality_p (tree);
200 static bool dependent_type_p_r (tree);
201 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
202 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
203 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
204 static tree tsubst_decl (tree, tree, tsubst_flags_t);
205 static void perform_typedefs_access_check (tree tmpl, tree targs);
206 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
207 location_t);
208 static tree listify (tree);
209 static tree listify_autos (tree, tree);
210 static tree template_parm_to_arg (tree t);
211 static tree current_template_args (void);
212 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
213 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
214
215 /* Make the current scope suitable for access checking when we are
216 processing T. T can be FUNCTION_DECL for instantiated function
217 template, VAR_DECL for static member variable, or TYPE_DECL for
218 alias template (needed by instantiate_decl). */
219
220 static void
221 push_access_scope (tree t)
222 {
223 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
224 || TREE_CODE (t) == TYPE_DECL);
225
226 if (DECL_FRIEND_CONTEXT (t))
227 push_nested_class (DECL_FRIEND_CONTEXT (t));
228 else if (DECL_CLASS_SCOPE_P (t))
229 push_nested_class (DECL_CONTEXT (t));
230 else
231 push_to_top_level ();
232
233 if (TREE_CODE (t) == FUNCTION_DECL)
234 {
235 saved_access_scope = tree_cons
236 (NULL_TREE, current_function_decl, saved_access_scope);
237 current_function_decl = t;
238 }
239 }
240
241 /* Restore the scope set up by push_access_scope. T is the node we
242 are processing. */
243
244 static void
245 pop_access_scope (tree t)
246 {
247 if (TREE_CODE (t) == FUNCTION_DECL)
248 {
249 current_function_decl = TREE_VALUE (saved_access_scope);
250 saved_access_scope = TREE_CHAIN (saved_access_scope);
251 }
252
253 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
254 pop_nested_class ();
255 else
256 pop_from_top_level ();
257 }
258
259 /* Do any processing required when DECL (a member template
260 declaration) is finished. Returns the TEMPLATE_DECL corresponding
261 to DECL, unless it is a specialization, in which case the DECL
262 itself is returned. */
263
264 tree
265 finish_member_template_decl (tree decl)
266 {
267 if (decl == error_mark_node)
268 return error_mark_node;
269
270 gcc_assert (DECL_P (decl));
271
272 if (TREE_CODE (decl) == TYPE_DECL)
273 {
274 tree type;
275
276 type = TREE_TYPE (decl);
277 if (type == error_mark_node)
278 return error_mark_node;
279 if (MAYBE_CLASS_TYPE_P (type)
280 && CLASSTYPE_TEMPLATE_INFO (type)
281 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
282 {
283 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
284 check_member_template (tmpl);
285 return tmpl;
286 }
287 return NULL_TREE;
288 }
289 else if (TREE_CODE (decl) == FIELD_DECL)
290 error ("data member %qD cannot be a member template", decl);
291 else if (DECL_TEMPLATE_INFO (decl))
292 {
293 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
294 {
295 check_member_template (DECL_TI_TEMPLATE (decl));
296 return DECL_TI_TEMPLATE (decl);
297 }
298 else
299 return decl;
300 }
301 else
302 error ("invalid member template declaration %qD", decl);
303
304 return error_mark_node;
305 }
306
307 /* Create a template info node. */
308
309 tree
310 build_template_info (tree template_decl, tree template_args)
311 {
312 tree result = make_node (TEMPLATE_INFO);
313 TI_TEMPLATE (result) = template_decl;
314 TI_ARGS (result) = template_args;
315 return result;
316 }
317
318 /* Return the template info node corresponding to T, whatever T is. */
319
320 tree
321 get_template_info (const_tree t)
322 {
323 tree tinfo = NULL_TREE;
324
325 if (!t || t == error_mark_node)
326 return NULL;
327
328 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
329 tinfo = DECL_TEMPLATE_INFO (t);
330
331 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
332 t = TREE_TYPE (t);
333
334 if (OVERLOAD_TYPE_P (t))
335 tinfo = TYPE_TEMPLATE_INFO (t);
336 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
337 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
338
339 return tinfo;
340 }
341
342 /* Returns the template nesting level of the indicated class TYPE.
343
344 For example, in:
345 template <class T>
346 struct A
347 {
348 template <class U>
349 struct B {};
350 };
351
352 A<T>::B<U> has depth two, while A<T> has depth one.
353 Both A<T>::B<int> and A<int>::B<U> have depth one, if
354 they are instantiations, not specializations.
355
356 This function is guaranteed to return 0 if passed NULL_TREE so
357 that, for example, `template_class_depth (current_class_type)' is
358 always safe. */
359
360 int
361 template_class_depth (tree type)
362 {
363 int depth;
364
365 for (depth = 0;
366 type && TREE_CODE (type) != NAMESPACE_DECL;
367 type = (TREE_CODE (type) == FUNCTION_DECL)
368 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
369 {
370 tree tinfo = get_template_info (type);
371
372 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
373 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
374 ++depth;
375 }
376
377 return depth;
378 }
379
380 /* Subroutine of maybe_begin_member_template_processing.
381 Returns true if processing DECL needs us to push template parms. */
382
383 static bool
384 inline_needs_template_parms (tree decl, bool nsdmi)
385 {
386 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
387 return false;
388
389 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
390 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
391 }
392
393 /* Subroutine of maybe_begin_member_template_processing.
394 Push the template parms in PARMS, starting from LEVELS steps into the
395 chain, and ending at the beginning, since template parms are listed
396 innermost first. */
397
398 static void
399 push_inline_template_parms_recursive (tree parmlist, int levels)
400 {
401 tree parms = TREE_VALUE (parmlist);
402 int i;
403
404 if (levels > 1)
405 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
406
407 ++processing_template_decl;
408 current_template_parms
409 = tree_cons (size_int (processing_template_decl),
410 parms, current_template_parms);
411 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
412
413 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
414 NULL);
415 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
416 {
417 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
418
419 if (parm == error_mark_node)
420 continue;
421
422 gcc_assert (DECL_P (parm));
423
424 switch (TREE_CODE (parm))
425 {
426 case TYPE_DECL:
427 case TEMPLATE_DECL:
428 pushdecl (parm);
429 break;
430
431 case PARM_DECL:
432 {
433 /* Make a CONST_DECL as is done in process_template_parm.
434 It is ugly that we recreate this here; the original
435 version built in process_template_parm is no longer
436 available. */
437 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
438 CONST_DECL, DECL_NAME (parm),
439 TREE_TYPE (parm));
440 DECL_ARTIFICIAL (decl) = 1;
441 TREE_CONSTANT (decl) = 1;
442 TREE_READONLY (decl) = 1;
443 DECL_INITIAL (decl) = DECL_INITIAL (parm);
444 SET_DECL_TEMPLATE_PARM_P (decl);
445 pushdecl (decl);
446 }
447 break;
448
449 default:
450 gcc_unreachable ();
451 }
452 }
453 }
454
455 /* Restore the template parameter context for a member template, a
456 friend template defined in a class definition, or a non-template
457 member of template class. */
458
459 void
460 maybe_begin_member_template_processing (tree decl)
461 {
462 tree parms;
463 int levels = 0;
464 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
465
466 if (nsdmi)
467 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
468 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
469 : NULL_TREE);
470
471 if (inline_needs_template_parms (decl, nsdmi))
472 {
473 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
474 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
475
476 if (DECL_TEMPLATE_SPECIALIZATION (decl))
477 {
478 --levels;
479 parms = TREE_CHAIN (parms);
480 }
481
482 push_inline_template_parms_recursive (parms, levels);
483 }
484
485 /* Remember how many levels of template parameters we pushed so that
486 we can pop them later. */
487 inline_parm_levels.safe_push (levels);
488 }
489
490 /* Undo the effects of maybe_begin_member_template_processing. */
491
492 void
493 maybe_end_member_template_processing (void)
494 {
495 int i;
496 int last;
497
498 if (inline_parm_levels.length () == 0)
499 return;
500
501 last = inline_parm_levels.pop ();
502 for (i = 0; i < last; ++i)
503 {
504 --processing_template_decl;
505 current_template_parms = TREE_CHAIN (current_template_parms);
506 poplevel (0, 0, 0);
507 }
508 }
509
510 /* Return a new template argument vector which contains all of ARGS,
511 but has as its innermost set of arguments the EXTRA_ARGS. */
512
513 static tree
514 add_to_template_args (tree args, tree extra_args)
515 {
516 tree new_args;
517 int extra_depth;
518 int i;
519 int j;
520
521 if (args == NULL_TREE || extra_args == error_mark_node)
522 return extra_args;
523
524 extra_depth = TMPL_ARGS_DEPTH (extra_args);
525 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
526
527 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
528 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
529
530 for (j = 1; j <= extra_depth; ++j, ++i)
531 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
532
533 return new_args;
534 }
535
536 /* Like add_to_template_args, but only the outermost ARGS are added to
537 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
538 (EXTRA_ARGS) levels are added. This function is used to combine
539 the template arguments from a partial instantiation with the
540 template arguments used to attain the full instantiation from the
541 partial instantiation. */
542
543 static tree
544 add_outermost_template_args (tree args, tree extra_args)
545 {
546 tree new_args;
547
548 /* If there are more levels of EXTRA_ARGS than there are ARGS,
549 something very fishy is going on. */
550 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
551
552 /* If *all* the new arguments will be the EXTRA_ARGS, just return
553 them. */
554 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
555 return extra_args;
556
557 /* For the moment, we make ARGS look like it contains fewer levels. */
558 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
559
560 new_args = add_to_template_args (args, extra_args);
561
562 /* Now, we restore ARGS to its full dimensions. */
563 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
564
565 return new_args;
566 }
567
568 /* Return the N levels of innermost template arguments from the ARGS. */
569
570 tree
571 get_innermost_template_args (tree args, int n)
572 {
573 tree new_args;
574 int extra_levels;
575 int i;
576
577 gcc_assert (n >= 0);
578
579 /* If N is 1, just return the innermost set of template arguments. */
580 if (n == 1)
581 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
582
583 /* If we're not removing anything, just return the arguments we were
584 given. */
585 extra_levels = TMPL_ARGS_DEPTH (args) - n;
586 gcc_assert (extra_levels >= 0);
587 if (extra_levels == 0)
588 return args;
589
590 /* Make a new set of arguments, not containing the outer arguments. */
591 new_args = make_tree_vec (n);
592 for (i = 1; i <= n; ++i)
593 SET_TMPL_ARGS_LEVEL (new_args, i,
594 TMPL_ARGS_LEVEL (args, i + extra_levels));
595
596 return new_args;
597 }
598
599 /* The inverse of get_innermost_template_args: Return all but the innermost
600 EXTRA_LEVELS levels of template arguments from the ARGS. */
601
602 static tree
603 strip_innermost_template_args (tree args, int extra_levels)
604 {
605 tree new_args;
606 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
607 int i;
608
609 gcc_assert (n >= 0);
610
611 /* If N is 1, just return the outermost set of template arguments. */
612 if (n == 1)
613 return TMPL_ARGS_LEVEL (args, 1);
614
615 /* If we're not removing anything, just return the arguments we were
616 given. */
617 gcc_assert (extra_levels >= 0);
618 if (extra_levels == 0)
619 return args;
620
621 /* Make a new set of arguments, not containing the inner arguments. */
622 new_args = make_tree_vec (n);
623 for (i = 1; i <= n; ++i)
624 SET_TMPL_ARGS_LEVEL (new_args, i,
625 TMPL_ARGS_LEVEL (args, i));
626
627 return new_args;
628 }
629
630 /* We've got a template header coming up; push to a new level for storing
631 the parms. */
632
633 void
634 begin_template_parm_list (void)
635 {
636 /* We use a non-tag-transparent scope here, which causes pushtag to
637 put tags in this scope, rather than in the enclosing class or
638 namespace scope. This is the right thing, since we want
639 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
640 global template class, push_template_decl handles putting the
641 TEMPLATE_DECL into top-level scope. For a nested template class,
642 e.g.:
643
644 template <class T> struct S1 {
645 template <class T> struct S2 {};
646 };
647
648 pushtag contains special code to call pushdecl_with_scope on the
649 TEMPLATE_DECL for S2. */
650 begin_scope (sk_template_parms, NULL);
651 ++processing_template_decl;
652 ++processing_template_parmlist;
653 note_template_header (0);
654 }
655
656 /* This routine is called when a specialization is declared. If it is
657 invalid to declare a specialization here, an error is reported and
658 false is returned, otherwise this routine will return true. */
659
660 static bool
661 check_specialization_scope (void)
662 {
663 tree scope = current_scope ();
664
665 /* [temp.expl.spec]
666
667 An explicit specialization shall be declared in the namespace of
668 which the template is a member, or, for member templates, in the
669 namespace of which the enclosing class or enclosing class
670 template is a member. An explicit specialization of a member
671 function, member class or static data member of a class template
672 shall be declared in the namespace of which the class template
673 is a member. */
674 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
675 {
676 error ("explicit specialization in non-namespace scope %qD", scope);
677 return false;
678 }
679
680 /* [temp.expl.spec]
681
682 In an explicit specialization declaration for a member of a class
683 template or a member template that appears in namespace scope,
684 the member template and some of its enclosing class templates may
685 remain unspecialized, except that the declaration shall not
686 explicitly specialize a class member template if its enclosing
687 class templates are not explicitly specialized as well. */
688 if (current_template_parms)
689 {
690 error ("enclosing class templates are not explicitly specialized");
691 return false;
692 }
693
694 return true;
695 }
696
697 /* We've just seen template <>. */
698
699 bool
700 begin_specialization (void)
701 {
702 begin_scope (sk_template_spec, NULL);
703 note_template_header (1);
704 return check_specialization_scope ();
705 }
706
707 /* Called at then end of processing a declaration preceded by
708 template<>. */
709
710 void
711 end_specialization (void)
712 {
713 finish_scope ();
714 reset_specialization ();
715 }
716
717 /* Any template <>'s that we have seen thus far are not referring to a
718 function specialization. */
719
720 void
721 reset_specialization (void)
722 {
723 processing_specialization = 0;
724 template_header_count = 0;
725 }
726
727 /* We've just seen a template header. If SPECIALIZATION is nonzero,
728 it was of the form template <>. */
729
730 static void
731 note_template_header (int specialization)
732 {
733 processing_specialization = specialization;
734 template_header_count++;
735 }
736
737 /* We're beginning an explicit instantiation. */
738
739 void
740 begin_explicit_instantiation (void)
741 {
742 gcc_assert (!processing_explicit_instantiation);
743 processing_explicit_instantiation = true;
744 }
745
746
747 void
748 end_explicit_instantiation (void)
749 {
750 gcc_assert (processing_explicit_instantiation);
751 processing_explicit_instantiation = false;
752 }
753
754 /* An explicit specialization or partial specialization of TMPL is being
755 declared. Check that the namespace in which the specialization is
756 occurring is permissible. Returns false iff it is invalid to
757 specialize TMPL in the current namespace. */
758
759 static bool
760 check_specialization_namespace (tree tmpl)
761 {
762 tree tpl_ns = decl_namespace_context (tmpl);
763
764 /* [tmpl.expl.spec]
765
766 An explicit specialization shall be declared in the namespace of
767 which the template is a member, or, for member templates, in the
768 namespace of which the enclosing class or enclosing class
769 template is a member. An explicit specialization of a member
770 function, member class or static data member of a class template
771 shall be declared in the namespace of which the class template is
772 a member. */
773 if (current_scope() != DECL_CONTEXT (tmpl)
774 && !at_namespace_scope_p ())
775 {
776 error ("specialization of %qD must appear at namespace scope", tmpl);
777 return false;
778 }
779 if (is_associated_namespace (current_namespace, tpl_ns))
780 /* Same or super-using namespace. */
781 return true;
782 else
783 {
784 permerror (input_location, "specialization of %qD in different namespace", tmpl);
785 permerror (input_location, " from definition of %q+#D", tmpl);
786 return false;
787 }
788 }
789
790 /* SPEC is an explicit instantiation. Check that it is valid to
791 perform this explicit instantiation in the current namespace. */
792
793 static void
794 check_explicit_instantiation_namespace (tree spec)
795 {
796 tree ns;
797
798 /* DR 275: An explicit instantiation shall appear in an enclosing
799 namespace of its template. */
800 ns = decl_namespace_context (spec);
801 if (!is_ancestor (current_namespace, ns))
802 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
803 "(which does not enclose namespace %qD)",
804 spec, current_namespace, ns);
805 }
806
807 /* The TYPE is being declared. If it is a template type, that means it
808 is a partial specialization. Do appropriate error-checking. */
809
810 tree
811 maybe_process_partial_specialization (tree type)
812 {
813 tree context;
814
815 if (type == error_mark_node)
816 return error_mark_node;
817
818 /* A lambda that appears in specialization context is not itself a
819 specialization. */
820 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
821 return type;
822
823 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
824 {
825 error ("name of class shadows template template parameter %qD",
826 TYPE_NAME (type));
827 return error_mark_node;
828 }
829
830 context = TYPE_CONTEXT (type);
831
832 if (TYPE_ALIAS_P (type))
833 {
834 if (TYPE_TEMPLATE_INFO (type)
835 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
836 error ("specialization of alias template %qD",
837 TYPE_TI_TEMPLATE (type));
838 else
839 error ("explicit specialization of non-template %qT", type);
840 return error_mark_node;
841 }
842 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
843 {
844 /* This is for ordinary explicit specialization and partial
845 specialization of a template class such as:
846
847 template <> class C<int>;
848
849 or:
850
851 template <class T> class C<T*>;
852
853 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
854
855 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
856 && !COMPLETE_TYPE_P (type))
857 {
858 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
859 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
860 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
861 if (processing_template_decl)
862 {
863 if (push_template_decl (TYPE_MAIN_DECL (type))
864 == error_mark_node)
865 return error_mark_node;
866 }
867 }
868 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
869 error ("specialization of %qT after instantiation", type);
870 else if (errorcount && !processing_specialization
871 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
872 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
873 /* Trying to define a specialization either without a template<> header
874 or in an inappropriate place. We've already given an error, so just
875 bail now so we don't actually define the specialization. */
876 return error_mark_node;
877 }
878 else if (CLASS_TYPE_P (type)
879 && !CLASSTYPE_USE_TEMPLATE (type)
880 && CLASSTYPE_TEMPLATE_INFO (type)
881 && context && CLASS_TYPE_P (context)
882 && CLASSTYPE_TEMPLATE_INFO (context))
883 {
884 /* This is for an explicit specialization of member class
885 template according to [temp.expl.spec/18]:
886
887 template <> template <class U> class C<int>::D;
888
889 The context `C<int>' must be an implicit instantiation.
890 Otherwise this is just a member class template declared
891 earlier like:
892
893 template <> class C<int> { template <class U> class D; };
894 template <> template <class U> class C<int>::D;
895
896 In the first case, `C<int>::D' is a specialization of `C<T>::D'
897 while in the second case, `C<int>::D' is a primary template
898 and `C<T>::D' may not exist. */
899
900 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
901 && !COMPLETE_TYPE_P (type))
902 {
903 tree t;
904 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
905
906 if (current_namespace
907 != decl_namespace_context (tmpl))
908 {
909 permerror (input_location, "specializing %q#T in different namespace", type);
910 permerror (input_location, " from definition of %q+#D", tmpl);
911 }
912
913 /* Check for invalid specialization after instantiation:
914
915 template <> template <> class C<int>::D<int>;
916 template <> template <class U> class C<int>::D; */
917
918 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
919 t; t = TREE_CHAIN (t))
920 {
921 tree inst = TREE_VALUE (t);
922 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
923 {
924 /* We already have a full specialization of this partial
925 instantiation. Reassign it to the new member
926 specialization template. */
927 spec_entry elt;
928 spec_entry *entry;
929 void **slot;
930
931 elt.tmpl = most_general_template (tmpl);
932 elt.args = CLASSTYPE_TI_ARGS (inst);
933 elt.spec = inst;
934
935 htab_remove_elt (type_specializations, &elt);
936
937 elt.tmpl = tmpl;
938 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
939
940 slot = htab_find_slot (type_specializations, &elt, INSERT);
941 entry = ggc_alloc_spec_entry ();
942 *entry = elt;
943 *slot = entry;
944 }
945 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
946 /* But if we've had an implicit instantiation, that's a
947 problem ([temp.expl.spec]/6). */
948 error ("specialization %qT after instantiation %qT",
949 type, inst);
950 }
951
952 /* Mark TYPE as a specialization. And as a result, we only
953 have one level of template argument for the innermost
954 class template. */
955 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
956 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
957 CLASSTYPE_TI_ARGS (type)
958 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
959 }
960 }
961 else if (processing_specialization)
962 {
963 /* Someday C++0x may allow for enum template specialization. */
964 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
965 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
966 pedwarn (input_location, OPT_Wpedantic, "template specialization "
967 "of %qD not allowed by ISO C++", type);
968 else
969 {
970 error ("explicit specialization of non-template %qT", type);
971 return error_mark_node;
972 }
973 }
974
975 return type;
976 }
977
978 /* Returns nonzero if we can optimize the retrieval of specializations
979 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
980 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
981
982 static inline bool
983 optimize_specialization_lookup_p (tree tmpl)
984 {
985 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
986 && DECL_CLASS_SCOPE_P (tmpl)
987 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
988 parameter. */
989 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
990 /* The optimized lookup depends on the fact that the
991 template arguments for the member function template apply
992 purely to the containing class, which is not true if the
993 containing class is an explicit or partial
994 specialization. */
995 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
996 && !DECL_MEMBER_TEMPLATE_P (tmpl)
997 && !DECL_CONV_FN_P (tmpl)
998 /* It is possible to have a template that is not a member
999 template and is not a member of a template class:
1000
1001 template <typename T>
1002 struct S { friend A::f(); };
1003
1004 Here, the friend function is a template, but the context does
1005 not have template information. The optimized lookup relies
1006 on having ARGS be the template arguments for both the class
1007 and the function template. */
1008 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1009 }
1010
1011 /* Retrieve the specialization (in the sense of [temp.spec] - a
1012 specialization is either an instantiation or an explicit
1013 specialization) of TMPL for the given template ARGS. If there is
1014 no such specialization, return NULL_TREE. The ARGS are a vector of
1015 arguments, or a vector of vectors of arguments, in the case of
1016 templates with more than one level of parameters.
1017
1018 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1019 then we search for a partial specialization matching ARGS. This
1020 parameter is ignored if TMPL is not a class template.
1021
1022 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1023 result is a NONTYPE_ARGUMENT_PACK. */
1024
1025 static tree
1026 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1027 {
1028 if (tmpl == NULL_TREE)
1029 return NULL_TREE;
1030
1031 if (args == error_mark_node)
1032 return NULL_TREE;
1033
1034 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1035 || TREE_CODE (tmpl) == FIELD_DECL);
1036
1037 /* There should be as many levels of arguments as there are
1038 levels of parameters. */
1039 gcc_assert (TMPL_ARGS_DEPTH (args)
1040 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1041 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1042 : template_class_depth (DECL_CONTEXT (tmpl))));
1043
1044 if (optimize_specialization_lookup_p (tmpl))
1045 {
1046 tree class_template;
1047 tree class_specialization;
1048 vec<tree, va_gc> *methods;
1049 tree fns;
1050 int idx;
1051
1052 /* The template arguments actually apply to the containing
1053 class. Find the class specialization with those
1054 arguments. */
1055 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1056 class_specialization
1057 = retrieve_specialization (class_template, args, 0);
1058 if (!class_specialization)
1059 return NULL_TREE;
1060 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1061 for the specialization. */
1062 idx = class_method_index_for_fn (class_specialization, tmpl);
1063 if (idx == -1)
1064 return NULL_TREE;
1065 /* Iterate through the methods with the indicated name, looking
1066 for the one that has an instance of TMPL. */
1067 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1068 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1069 {
1070 tree fn = OVL_CURRENT (fns);
1071 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1072 /* using-declarations can add base methods to the method vec,
1073 and we don't want those here. */
1074 && DECL_CONTEXT (fn) == class_specialization)
1075 return fn;
1076 }
1077 return NULL_TREE;
1078 }
1079 else
1080 {
1081 spec_entry *found;
1082 spec_entry elt;
1083 htab_t specializations;
1084
1085 elt.tmpl = tmpl;
1086 elt.args = args;
1087 elt.spec = NULL_TREE;
1088
1089 if (DECL_CLASS_TEMPLATE_P (tmpl))
1090 specializations = type_specializations;
1091 else
1092 specializations = decl_specializations;
1093
1094 if (hash == 0)
1095 hash = hash_specialization (&elt);
1096 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1097 if (found)
1098 return found->spec;
1099 }
1100
1101 return NULL_TREE;
1102 }
1103
1104 /* Like retrieve_specialization, but for local declarations. */
1105
1106 static tree
1107 retrieve_local_specialization (tree tmpl)
1108 {
1109 void **slot;
1110
1111 if (local_specializations == NULL)
1112 return NULL_TREE;
1113
1114 slot = pointer_map_contains (local_specializations, tmpl);
1115 return slot ? (tree) *slot : NULL_TREE;
1116 }
1117
1118 /* Returns nonzero iff DECL is a specialization of TMPL. */
1119
1120 int
1121 is_specialization_of (tree decl, tree tmpl)
1122 {
1123 tree t;
1124
1125 if (TREE_CODE (decl) == FUNCTION_DECL)
1126 {
1127 for (t = decl;
1128 t != NULL_TREE;
1129 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1130 if (t == tmpl)
1131 return 1;
1132 }
1133 else
1134 {
1135 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1136
1137 for (t = TREE_TYPE (decl);
1138 t != NULL_TREE;
1139 t = CLASSTYPE_USE_TEMPLATE (t)
1140 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1141 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1142 return 1;
1143 }
1144
1145 return 0;
1146 }
1147
1148 /* Returns nonzero iff DECL is a specialization of friend declaration
1149 FRIEND_DECL according to [temp.friend]. */
1150
1151 bool
1152 is_specialization_of_friend (tree decl, tree friend_decl)
1153 {
1154 bool need_template = true;
1155 int template_depth;
1156
1157 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1158 || TREE_CODE (decl) == TYPE_DECL);
1159
1160 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1161 of a template class, we want to check if DECL is a specialization
1162 if this. */
1163 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1164 && DECL_TEMPLATE_INFO (friend_decl)
1165 && !DECL_USE_TEMPLATE (friend_decl))
1166 {
1167 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1168 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1169 need_template = false;
1170 }
1171 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1172 && !PRIMARY_TEMPLATE_P (friend_decl))
1173 need_template = false;
1174
1175 /* There is nothing to do if this is not a template friend. */
1176 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1177 return false;
1178
1179 if (is_specialization_of (decl, friend_decl))
1180 return true;
1181
1182 /* [temp.friend/6]
1183 A member of a class template may be declared to be a friend of a
1184 non-template class. In this case, the corresponding member of
1185 every specialization of the class template is a friend of the
1186 class granting friendship.
1187
1188 For example, given a template friend declaration
1189
1190 template <class T> friend void A<T>::f();
1191
1192 the member function below is considered a friend
1193
1194 template <> struct A<int> {
1195 void f();
1196 };
1197
1198 For this type of template friend, TEMPLATE_DEPTH below will be
1199 nonzero. To determine if DECL is a friend of FRIEND, we first
1200 check if the enclosing class is a specialization of another. */
1201
1202 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1203 if (template_depth
1204 && DECL_CLASS_SCOPE_P (decl)
1205 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1206 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1207 {
1208 /* Next, we check the members themselves. In order to handle
1209 a few tricky cases, such as when FRIEND_DECL's are
1210
1211 template <class T> friend void A<T>::g(T t);
1212 template <class T> template <T t> friend void A<T>::h();
1213
1214 and DECL's are
1215
1216 void A<int>::g(int);
1217 template <int> void A<int>::h();
1218
1219 we need to figure out ARGS, the template arguments from
1220 the context of DECL. This is required for template substitution
1221 of `T' in the function parameter of `g' and template parameter
1222 of `h' in the above examples. Here ARGS corresponds to `int'. */
1223
1224 tree context = DECL_CONTEXT (decl);
1225 tree args = NULL_TREE;
1226 int current_depth = 0;
1227
1228 while (current_depth < template_depth)
1229 {
1230 if (CLASSTYPE_TEMPLATE_INFO (context))
1231 {
1232 if (current_depth == 0)
1233 args = TYPE_TI_ARGS (context);
1234 else
1235 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1236 current_depth++;
1237 }
1238 context = TYPE_CONTEXT (context);
1239 }
1240
1241 if (TREE_CODE (decl) == FUNCTION_DECL)
1242 {
1243 bool is_template;
1244 tree friend_type;
1245 tree decl_type;
1246 tree friend_args_type;
1247 tree decl_args_type;
1248
1249 /* Make sure that both DECL and FRIEND_DECL are templates or
1250 non-templates. */
1251 is_template = DECL_TEMPLATE_INFO (decl)
1252 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1253 if (need_template ^ is_template)
1254 return false;
1255 else if (is_template)
1256 {
1257 /* If both are templates, check template parameter list. */
1258 tree friend_parms
1259 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1260 args, tf_none);
1261 if (!comp_template_parms
1262 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1263 friend_parms))
1264 return false;
1265
1266 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1267 }
1268 else
1269 decl_type = TREE_TYPE (decl);
1270
1271 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1272 tf_none, NULL_TREE);
1273 if (friend_type == error_mark_node)
1274 return false;
1275
1276 /* Check if return types match. */
1277 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1278 return false;
1279
1280 /* Check if function parameter types match, ignoring the
1281 `this' parameter. */
1282 friend_args_type = TYPE_ARG_TYPES (friend_type);
1283 decl_args_type = TYPE_ARG_TYPES (decl_type);
1284 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1285 friend_args_type = TREE_CHAIN (friend_args_type);
1286 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1287 decl_args_type = TREE_CHAIN (decl_args_type);
1288
1289 return compparms (decl_args_type, friend_args_type);
1290 }
1291 else
1292 {
1293 /* DECL is a TYPE_DECL */
1294 bool is_template;
1295 tree decl_type = TREE_TYPE (decl);
1296
1297 /* Make sure that both DECL and FRIEND_DECL are templates or
1298 non-templates. */
1299 is_template
1300 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1301 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1302
1303 if (need_template ^ is_template)
1304 return false;
1305 else if (is_template)
1306 {
1307 tree friend_parms;
1308 /* If both are templates, check the name of the two
1309 TEMPLATE_DECL's first because is_friend didn't. */
1310 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1311 != DECL_NAME (friend_decl))
1312 return false;
1313
1314 /* Now check template parameter list. */
1315 friend_parms
1316 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1317 args, tf_none);
1318 return comp_template_parms
1319 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1320 friend_parms);
1321 }
1322 else
1323 return (DECL_NAME (decl)
1324 == DECL_NAME (friend_decl));
1325 }
1326 }
1327 return false;
1328 }
1329
1330 /* Register the specialization SPEC as a specialization of TMPL with
1331 the indicated ARGS. IS_FRIEND indicates whether the specialization
1332 is actually just a friend declaration. Returns SPEC, or an
1333 equivalent prior declaration, if available.
1334
1335 We also store instantiations of field packs in the hash table, even
1336 though they are not themselves templates, to make lookup easier. */
1337
1338 static tree
1339 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1340 hashval_t hash)
1341 {
1342 tree fn;
1343 void **slot = NULL;
1344 spec_entry elt;
1345
1346 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1347 || (TREE_CODE (tmpl) == FIELD_DECL
1348 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1349
1350 if (TREE_CODE (spec) == FUNCTION_DECL
1351 && uses_template_parms (DECL_TI_ARGS (spec)))
1352 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1353 register it; we want the corresponding TEMPLATE_DECL instead.
1354 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1355 the more obvious `uses_template_parms (spec)' to avoid problems
1356 with default function arguments. In particular, given
1357 something like this:
1358
1359 template <class T> void f(T t1, T t = T())
1360
1361 the default argument expression is not substituted for in an
1362 instantiation unless and until it is actually needed. */
1363 return spec;
1364
1365 if (optimize_specialization_lookup_p (tmpl))
1366 /* We don't put these specializations in the hash table, but we might
1367 want to give an error about a mismatch. */
1368 fn = retrieve_specialization (tmpl, args, 0);
1369 else
1370 {
1371 elt.tmpl = tmpl;
1372 elt.args = args;
1373 elt.spec = spec;
1374
1375 if (hash == 0)
1376 hash = hash_specialization (&elt);
1377
1378 slot =
1379 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1380 if (*slot)
1381 fn = ((spec_entry *) *slot)->spec;
1382 else
1383 fn = NULL_TREE;
1384 }
1385
1386 /* We can sometimes try to re-register a specialization that we've
1387 already got. In particular, regenerate_decl_from_template calls
1388 duplicate_decls which will update the specialization list. But,
1389 we'll still get called again here anyhow. It's more convenient
1390 to simply allow this than to try to prevent it. */
1391 if (fn == spec)
1392 return spec;
1393 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1394 {
1395 if (DECL_TEMPLATE_INSTANTIATION (fn))
1396 {
1397 if (DECL_ODR_USED (fn)
1398 || DECL_EXPLICIT_INSTANTIATION (fn))
1399 {
1400 error ("specialization of %qD after instantiation",
1401 fn);
1402 return error_mark_node;
1403 }
1404 else
1405 {
1406 tree clone;
1407 /* This situation should occur only if the first
1408 specialization is an implicit instantiation, the
1409 second is an explicit specialization, and the
1410 implicit instantiation has not yet been used. That
1411 situation can occur if we have implicitly
1412 instantiated a member function and then specialized
1413 it later.
1414
1415 We can also wind up here if a friend declaration that
1416 looked like an instantiation turns out to be a
1417 specialization:
1418
1419 template <class T> void foo(T);
1420 class S { friend void foo<>(int) };
1421 template <> void foo(int);
1422
1423 We transform the existing DECL in place so that any
1424 pointers to it become pointers to the updated
1425 declaration.
1426
1427 If there was a definition for the template, but not
1428 for the specialization, we want this to look as if
1429 there were no definition, and vice versa. */
1430 DECL_INITIAL (fn) = NULL_TREE;
1431 duplicate_decls (spec, fn, is_friend);
1432 /* The call to duplicate_decls will have applied
1433 [temp.expl.spec]:
1434
1435 An explicit specialization of a function template
1436 is inline only if it is explicitly declared to be,
1437 and independently of whether its function template
1438 is.
1439
1440 to the primary function; now copy the inline bits to
1441 the various clones. */
1442 FOR_EACH_CLONE (clone, fn)
1443 {
1444 DECL_DECLARED_INLINE_P (clone)
1445 = DECL_DECLARED_INLINE_P (fn);
1446 DECL_SOURCE_LOCATION (clone)
1447 = DECL_SOURCE_LOCATION (fn);
1448 }
1449 check_specialization_namespace (tmpl);
1450
1451 return fn;
1452 }
1453 }
1454 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1455 {
1456 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1457 /* Dup decl failed, but this is a new definition. Set the
1458 line number so any errors match this new
1459 definition. */
1460 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1461
1462 return fn;
1463 }
1464 }
1465 else if (fn)
1466 return duplicate_decls (spec, fn, is_friend);
1467
1468 /* A specialization must be declared in the same namespace as the
1469 template it is specializing. */
1470 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1471 && !check_specialization_namespace (tmpl))
1472 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1473
1474 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1475 {
1476 spec_entry *entry = ggc_alloc_spec_entry ();
1477 gcc_assert (tmpl && args && spec);
1478 *entry = elt;
1479 *slot = entry;
1480 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1481 && PRIMARY_TEMPLATE_P (tmpl)
1482 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1483 /* TMPL is a forward declaration of a template function; keep a list
1484 of all specializations in case we need to reassign them to a friend
1485 template later in tsubst_friend_function. */
1486 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1487 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1488 }
1489
1490 return spec;
1491 }
1492
1493 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1494 TMPL and ARGS members, ignores SPEC. */
1495
1496 int comparing_specializations;
1497
1498 static int
1499 eq_specializations (const void *p1, const void *p2)
1500 {
1501 const spec_entry *e1 = (const spec_entry *)p1;
1502 const spec_entry *e2 = (const spec_entry *)p2;
1503 int equal;
1504
1505 ++comparing_specializations;
1506 equal = (e1->tmpl == e2->tmpl
1507 && comp_template_args (e1->args, e2->args));
1508 --comparing_specializations;
1509
1510 return equal;
1511 }
1512
1513 /* Returns a hash for a template TMPL and template arguments ARGS. */
1514
1515 static hashval_t
1516 hash_tmpl_and_args (tree tmpl, tree args)
1517 {
1518 hashval_t val = DECL_UID (tmpl);
1519 return iterative_hash_template_arg (args, val);
1520 }
1521
1522 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1523 ignoring SPEC. */
1524
1525 static hashval_t
1526 hash_specialization (const void *p)
1527 {
1528 const spec_entry *e = (const spec_entry *)p;
1529 return hash_tmpl_and_args (e->tmpl, e->args);
1530 }
1531
1532 /* Recursively calculate a hash value for a template argument ARG, for use
1533 in the hash tables of template specializations. */
1534
1535 hashval_t
1536 iterative_hash_template_arg (tree arg, hashval_t val)
1537 {
1538 unsigned HOST_WIDE_INT i;
1539 enum tree_code code;
1540 char tclass;
1541
1542 if (arg == NULL_TREE)
1543 return iterative_hash_object (arg, val);
1544
1545 if (!TYPE_P (arg))
1546 STRIP_NOPS (arg);
1547
1548 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1549 /* We can get one of these when re-hashing a previous entry in the middle
1550 of substituting into a pack expansion. Just look through it. */
1551 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1552
1553 code = TREE_CODE (arg);
1554 tclass = TREE_CODE_CLASS (code);
1555
1556 val = iterative_hash_object (code, val);
1557
1558 switch (code)
1559 {
1560 case ERROR_MARK:
1561 return val;
1562
1563 case IDENTIFIER_NODE:
1564 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1565
1566 case TREE_VEC:
1567 {
1568 int i, len = TREE_VEC_LENGTH (arg);
1569 for (i = 0; i < len; ++i)
1570 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1571 return val;
1572 }
1573
1574 case TYPE_PACK_EXPANSION:
1575 case EXPR_PACK_EXPANSION:
1576 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1577 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1578
1579 case TYPE_ARGUMENT_PACK:
1580 case NONTYPE_ARGUMENT_PACK:
1581 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1582
1583 case TREE_LIST:
1584 for (; arg; arg = TREE_CHAIN (arg))
1585 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1586 return val;
1587
1588 case OVERLOAD:
1589 for (; arg; arg = OVL_NEXT (arg))
1590 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1591 return val;
1592
1593 case CONSTRUCTOR:
1594 {
1595 tree field, value;
1596 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1597 {
1598 val = iterative_hash_template_arg (field, val);
1599 val = iterative_hash_template_arg (value, val);
1600 }
1601 return val;
1602 }
1603
1604 case PARM_DECL:
1605 if (!DECL_ARTIFICIAL (arg))
1606 {
1607 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1608 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1609 }
1610 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1611
1612 case TARGET_EXPR:
1613 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1614
1615 case PTRMEM_CST:
1616 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1617 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1618
1619 case TEMPLATE_PARM_INDEX:
1620 val = iterative_hash_template_arg
1621 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1622 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1623 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1624
1625 case TRAIT_EXPR:
1626 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1627 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1628 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1629
1630 case BASELINK:
1631 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1632 val);
1633 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1634 val);
1635
1636 case MODOP_EXPR:
1637 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1638 code = TREE_CODE (TREE_OPERAND (arg, 1));
1639 val = iterative_hash_object (code, val);
1640 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1641
1642 case LAMBDA_EXPR:
1643 /* A lambda can't appear in a template arg, but don't crash on
1644 erroneous input. */
1645 gcc_assert (seen_error ());
1646 return val;
1647
1648 case CAST_EXPR:
1649 case IMPLICIT_CONV_EXPR:
1650 case STATIC_CAST_EXPR:
1651 case REINTERPRET_CAST_EXPR:
1652 case CONST_CAST_EXPR:
1653 case DYNAMIC_CAST_EXPR:
1654 case NEW_EXPR:
1655 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1656 /* Now hash operands as usual. */
1657 break;
1658
1659 default:
1660 break;
1661 }
1662
1663 switch (tclass)
1664 {
1665 case tcc_type:
1666 if (TYPE_CANONICAL (arg))
1667 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1668 val);
1669 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1670 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1671 /* Otherwise just compare the types during lookup. */
1672 return val;
1673
1674 case tcc_declaration:
1675 case tcc_constant:
1676 return iterative_hash_expr (arg, val);
1677
1678 default:
1679 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1680 {
1681 unsigned n = cp_tree_operand_length (arg);
1682 for (i = 0; i < n; ++i)
1683 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1684 return val;
1685 }
1686 }
1687 gcc_unreachable ();
1688 return 0;
1689 }
1690
1691 /* Unregister the specialization SPEC as a specialization of TMPL.
1692 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1693 if the SPEC was listed as a specialization of TMPL.
1694
1695 Note that SPEC has been ggc_freed, so we can't look inside it. */
1696
1697 bool
1698 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1699 {
1700 spec_entry *entry;
1701 spec_entry elt;
1702
1703 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1704 elt.args = TI_ARGS (tinfo);
1705 elt.spec = NULL_TREE;
1706
1707 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1708 if (entry != NULL)
1709 {
1710 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1711 gcc_assert (new_spec != NULL_TREE);
1712 entry->spec = new_spec;
1713 return 1;
1714 }
1715
1716 return 0;
1717 }
1718
1719 /* Like register_specialization, but for local declarations. We are
1720 registering SPEC, an instantiation of TMPL. */
1721
1722 static void
1723 register_local_specialization (tree spec, tree tmpl)
1724 {
1725 void **slot;
1726
1727 slot = pointer_map_insert (local_specializations, tmpl);
1728 *slot = spec;
1729 }
1730
1731 /* TYPE is a class type. Returns true if TYPE is an explicitly
1732 specialized class. */
1733
1734 bool
1735 explicit_class_specialization_p (tree type)
1736 {
1737 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1738 return false;
1739 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1740 }
1741
1742 /* Print the list of functions at FNS, going through all the overloads
1743 for each element of the list. Alternatively, FNS can not be a
1744 TREE_LIST, in which case it will be printed together with all the
1745 overloads.
1746
1747 MORE and *STR should respectively be FALSE and NULL when the function
1748 is called from the outside. They are used internally on recursive
1749 calls. print_candidates manages the two parameters and leaves NULL
1750 in *STR when it ends. */
1751
1752 static void
1753 print_candidates_1 (tree fns, bool more, const char **str)
1754 {
1755 tree fn, fn2;
1756 char *spaces = NULL;
1757
1758 for (fn = fns; fn; fn = OVL_NEXT (fn))
1759 if (TREE_CODE (fn) == TREE_LIST)
1760 {
1761 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1762 print_candidates_1 (TREE_VALUE (fn2),
1763 TREE_CHAIN (fn2) || more, str);
1764 }
1765 else
1766 {
1767 tree cand = OVL_CURRENT (fn);
1768 if (!*str)
1769 {
1770 /* Pick the prefix string. */
1771 if (!more && !OVL_NEXT (fns))
1772 {
1773 inform (DECL_SOURCE_LOCATION (cand),
1774 "candidate is: %#D", cand);
1775 continue;
1776 }
1777
1778 *str = _("candidates are:");
1779 spaces = get_spaces (*str);
1780 }
1781 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1782 *str = spaces ? spaces : *str;
1783 }
1784
1785 if (!more)
1786 {
1787 free (spaces);
1788 *str = NULL;
1789 }
1790 }
1791
1792 /* Print the list of candidate FNS in an error message. FNS can also
1793 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1794
1795 void
1796 print_candidates (tree fns)
1797 {
1798 const char *str = NULL;
1799 print_candidates_1 (fns, false, &str);
1800 gcc_assert (str == NULL);
1801 }
1802
1803 /* Returns the template (one of the functions given by TEMPLATE_ID)
1804 which can be specialized to match the indicated DECL with the
1805 explicit template args given in TEMPLATE_ID. The DECL may be
1806 NULL_TREE if none is available. In that case, the functions in
1807 TEMPLATE_ID are non-members.
1808
1809 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1810 specialization of a member template.
1811
1812 The TEMPLATE_COUNT is the number of references to qualifying
1813 template classes that appeared in the name of the function. See
1814 check_explicit_specialization for a more accurate description.
1815
1816 TSK indicates what kind of template declaration (if any) is being
1817 declared. TSK_TEMPLATE indicates that the declaration given by
1818 DECL, though a FUNCTION_DECL, has template parameters, and is
1819 therefore a template function.
1820
1821 The template args (those explicitly specified and those deduced)
1822 are output in a newly created vector *TARGS_OUT.
1823
1824 If it is impossible to determine the result, an error message is
1825 issued. The error_mark_node is returned to indicate failure. */
1826
1827 static tree
1828 determine_specialization (tree template_id,
1829 tree decl,
1830 tree* targs_out,
1831 int need_member_template,
1832 int template_count,
1833 tmpl_spec_kind tsk)
1834 {
1835 tree fns;
1836 tree targs;
1837 tree explicit_targs;
1838 tree candidates = NULL_TREE;
1839 /* A TREE_LIST of templates of which DECL may be a specialization.
1840 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1841 corresponding TREE_PURPOSE is the set of template arguments that,
1842 when used to instantiate the template, would produce a function
1843 with the signature of DECL. */
1844 tree templates = NULL_TREE;
1845 int header_count;
1846 cp_binding_level *b;
1847
1848 *targs_out = NULL_TREE;
1849
1850 if (template_id == error_mark_node || decl == error_mark_node)
1851 return error_mark_node;
1852
1853 /* We shouldn't be specializing a member template of an
1854 unspecialized class template; we already gave an error in
1855 check_specialization_scope, now avoid crashing. */
1856 if (template_count && DECL_CLASS_SCOPE_P (decl)
1857 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1858 {
1859 gcc_assert (errorcount);
1860 return error_mark_node;
1861 }
1862
1863 fns = TREE_OPERAND (template_id, 0);
1864 explicit_targs = TREE_OPERAND (template_id, 1);
1865
1866 if (fns == error_mark_node)
1867 return error_mark_node;
1868
1869 /* Check for baselinks. */
1870 if (BASELINK_P (fns))
1871 fns = BASELINK_FUNCTIONS (fns);
1872
1873 if (!is_overloaded_fn (fns))
1874 {
1875 error ("%qD is not a function template", fns);
1876 return error_mark_node;
1877 }
1878
1879 /* Count the number of template headers specified for this
1880 specialization. */
1881 header_count = 0;
1882 for (b = current_binding_level;
1883 b->kind == sk_template_parms;
1884 b = b->level_chain)
1885 ++header_count;
1886
1887 for (; fns; fns = OVL_NEXT (fns))
1888 {
1889 tree fn = OVL_CURRENT (fns);
1890
1891 if (TREE_CODE (fn) == TEMPLATE_DECL)
1892 {
1893 tree decl_arg_types;
1894 tree fn_arg_types;
1895 tree insttype;
1896
1897 /* In case of explicit specialization, we need to check if
1898 the number of template headers appearing in the specialization
1899 is correct. This is usually done in check_explicit_specialization,
1900 but the check done there cannot be exhaustive when specializing
1901 member functions. Consider the following code:
1902
1903 template <> void A<int>::f(int);
1904 template <> template <> void A<int>::f(int);
1905
1906 Assuming that A<int> is not itself an explicit specialization
1907 already, the first line specializes "f" which is a non-template
1908 member function, whilst the second line specializes "f" which
1909 is a template member function. So both lines are syntactically
1910 correct, and check_explicit_specialization does not reject
1911 them.
1912
1913 Here, we can do better, as we are matching the specialization
1914 against the declarations. We count the number of template
1915 headers, and we check if they match TEMPLATE_COUNT + 1
1916 (TEMPLATE_COUNT is the number of qualifying template classes,
1917 plus there must be another header for the member template
1918 itself).
1919
1920 Notice that if header_count is zero, this is not a
1921 specialization but rather a template instantiation, so there
1922 is no check we can perform here. */
1923 if (header_count && header_count != template_count + 1)
1924 continue;
1925
1926 /* Check that the number of template arguments at the
1927 innermost level for DECL is the same as for FN. */
1928 if (current_binding_level->kind == sk_template_parms
1929 && !current_binding_level->explicit_spec_p
1930 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1931 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1932 (current_template_parms))))
1933 continue;
1934
1935 /* DECL might be a specialization of FN. */
1936 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1937 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1938
1939 /* For a non-static member function, we need to make sure
1940 that the const qualification is the same. Since
1941 get_bindings does not try to merge the "this" parameter,
1942 we must do the comparison explicitly. */
1943 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1944 && !same_type_p (TREE_VALUE (fn_arg_types),
1945 TREE_VALUE (decl_arg_types)))
1946 continue;
1947
1948 /* Skip the "this" parameter and, for constructors of
1949 classes with virtual bases, the VTT parameter. A
1950 full specialization of a constructor will have a VTT
1951 parameter, but a template never will. */
1952 decl_arg_types
1953 = skip_artificial_parms_for (decl, decl_arg_types);
1954 fn_arg_types
1955 = skip_artificial_parms_for (fn, fn_arg_types);
1956
1957 /* Function templates cannot be specializations; there are
1958 no partial specializations of functions. Therefore, if
1959 the type of DECL does not match FN, there is no
1960 match. */
1961 if (tsk == tsk_template)
1962 {
1963 if (compparms (fn_arg_types, decl_arg_types))
1964 candidates = tree_cons (NULL_TREE, fn, candidates);
1965 continue;
1966 }
1967
1968 /* See whether this function might be a specialization of this
1969 template. Suppress access control because we might be trying
1970 to make this specialization a friend, and we have already done
1971 access control for the declaration of the specialization. */
1972 push_deferring_access_checks (dk_no_check);
1973 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1974 pop_deferring_access_checks ();
1975
1976 if (!targs)
1977 /* We cannot deduce template arguments that when used to
1978 specialize TMPL will produce DECL. */
1979 continue;
1980
1981 /* Make sure that the deduced arguments actually work. */
1982 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1983 if (insttype == error_mark_node)
1984 continue;
1985 fn_arg_types
1986 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1987 if (!compparms (fn_arg_types, decl_arg_types))
1988 continue;
1989
1990 /* Save this template, and the arguments deduced. */
1991 templates = tree_cons (targs, fn, templates);
1992 }
1993 else if (need_member_template)
1994 /* FN is an ordinary member function, and we need a
1995 specialization of a member template. */
1996 ;
1997 else if (TREE_CODE (fn) != FUNCTION_DECL)
1998 /* We can get IDENTIFIER_NODEs here in certain erroneous
1999 cases. */
2000 ;
2001 else if (!DECL_FUNCTION_MEMBER_P (fn))
2002 /* This is just an ordinary non-member function. Nothing can
2003 be a specialization of that. */
2004 ;
2005 else if (DECL_ARTIFICIAL (fn))
2006 /* Cannot specialize functions that are created implicitly. */
2007 ;
2008 else
2009 {
2010 tree decl_arg_types;
2011
2012 /* This is an ordinary member function. However, since
2013 we're here, we can assume its enclosing class is a
2014 template class. For example,
2015
2016 template <typename T> struct S { void f(); };
2017 template <> void S<int>::f() {}
2018
2019 Here, S<int>::f is a non-template, but S<int> is a
2020 template class. If FN has the same type as DECL, we
2021 might be in business. */
2022
2023 if (!DECL_TEMPLATE_INFO (fn))
2024 /* Its enclosing class is an explicit specialization
2025 of a template class. This is not a candidate. */
2026 continue;
2027
2028 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2029 TREE_TYPE (TREE_TYPE (fn))))
2030 /* The return types differ. */
2031 continue;
2032
2033 /* Adjust the type of DECL in case FN is a static member. */
2034 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2035 if (DECL_STATIC_FUNCTION_P (fn)
2036 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2037 decl_arg_types = TREE_CHAIN (decl_arg_types);
2038
2039 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2040 decl_arg_types))
2041 /* They match! */
2042 candidates = tree_cons (NULL_TREE, fn, candidates);
2043 }
2044 }
2045
2046 if (templates && TREE_CHAIN (templates))
2047 {
2048 /* We have:
2049
2050 [temp.expl.spec]
2051
2052 It is possible for a specialization with a given function
2053 signature to be instantiated from more than one function
2054 template. In such cases, explicit specification of the
2055 template arguments must be used to uniquely identify the
2056 function template specialization being specialized.
2057
2058 Note that here, there's no suggestion that we're supposed to
2059 determine which of the candidate templates is most
2060 specialized. However, we, also have:
2061
2062 [temp.func.order]
2063
2064 Partial ordering of overloaded function template
2065 declarations is used in the following contexts to select
2066 the function template to which a function template
2067 specialization refers:
2068
2069 -- when an explicit specialization refers to a function
2070 template.
2071
2072 So, we do use the partial ordering rules, at least for now.
2073 This extension can only serve to make invalid programs valid,
2074 so it's safe. And, there is strong anecdotal evidence that
2075 the committee intended the partial ordering rules to apply;
2076 the EDG front end has that behavior, and John Spicer claims
2077 that the committee simply forgot to delete the wording in
2078 [temp.expl.spec]. */
2079 tree tmpl = most_specialized_instantiation (templates);
2080 if (tmpl != error_mark_node)
2081 {
2082 templates = tmpl;
2083 TREE_CHAIN (templates) = NULL_TREE;
2084 }
2085 }
2086
2087 if (templates == NULL_TREE && candidates == NULL_TREE)
2088 {
2089 error ("template-id %qD for %q+D does not match any template "
2090 "declaration", template_id, decl);
2091 if (header_count && header_count != template_count + 1)
2092 inform (input_location, "saw %d %<template<>%>, need %d for "
2093 "specializing a member function template",
2094 header_count, template_count + 1);
2095 return error_mark_node;
2096 }
2097 else if ((templates && TREE_CHAIN (templates))
2098 || (candidates && TREE_CHAIN (candidates))
2099 || (templates && candidates))
2100 {
2101 error ("ambiguous template specialization %qD for %q+D",
2102 template_id, decl);
2103 candidates = chainon (candidates, templates);
2104 print_candidates (candidates);
2105 return error_mark_node;
2106 }
2107
2108 /* We have one, and exactly one, match. */
2109 if (candidates)
2110 {
2111 tree fn = TREE_VALUE (candidates);
2112 *targs_out = copy_node (DECL_TI_ARGS (fn));
2113 /* DECL is a re-declaration or partial instantiation of a template
2114 function. */
2115 if (TREE_CODE (fn) == TEMPLATE_DECL)
2116 return fn;
2117 /* It was a specialization of an ordinary member function in a
2118 template class. */
2119 return DECL_TI_TEMPLATE (fn);
2120 }
2121
2122 /* It was a specialization of a template. */
2123 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2124 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2125 {
2126 *targs_out = copy_node (targs);
2127 SET_TMPL_ARGS_LEVEL (*targs_out,
2128 TMPL_ARGS_DEPTH (*targs_out),
2129 TREE_PURPOSE (templates));
2130 }
2131 else
2132 *targs_out = TREE_PURPOSE (templates);
2133 return TREE_VALUE (templates);
2134 }
2135
2136 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2137 but with the default argument values filled in from those in the
2138 TMPL_TYPES. */
2139
2140 static tree
2141 copy_default_args_to_explicit_spec_1 (tree spec_types,
2142 tree tmpl_types)
2143 {
2144 tree new_spec_types;
2145
2146 if (!spec_types)
2147 return NULL_TREE;
2148
2149 if (spec_types == void_list_node)
2150 return void_list_node;
2151
2152 /* Substitute into the rest of the list. */
2153 new_spec_types =
2154 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2155 TREE_CHAIN (tmpl_types));
2156
2157 /* Add the default argument for this parameter. */
2158 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2159 TREE_VALUE (spec_types),
2160 new_spec_types);
2161 }
2162
2163 /* DECL is an explicit specialization. Replicate default arguments
2164 from the template it specializes. (That way, code like:
2165
2166 template <class T> void f(T = 3);
2167 template <> void f(double);
2168 void g () { f (); }
2169
2170 works, as required.) An alternative approach would be to look up
2171 the correct default arguments at the call-site, but this approach
2172 is consistent with how implicit instantiations are handled. */
2173
2174 static void
2175 copy_default_args_to_explicit_spec (tree decl)
2176 {
2177 tree tmpl;
2178 tree spec_types;
2179 tree tmpl_types;
2180 tree new_spec_types;
2181 tree old_type;
2182 tree new_type;
2183 tree t;
2184 tree object_type = NULL_TREE;
2185 tree in_charge = NULL_TREE;
2186 tree vtt = NULL_TREE;
2187
2188 /* See if there's anything we need to do. */
2189 tmpl = DECL_TI_TEMPLATE (decl);
2190 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2191 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2192 if (TREE_PURPOSE (t))
2193 break;
2194 if (!t)
2195 return;
2196
2197 old_type = TREE_TYPE (decl);
2198 spec_types = TYPE_ARG_TYPES (old_type);
2199
2200 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2201 {
2202 /* Remove the this pointer, but remember the object's type for
2203 CV quals. */
2204 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2205 spec_types = TREE_CHAIN (spec_types);
2206 tmpl_types = TREE_CHAIN (tmpl_types);
2207
2208 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2209 {
2210 /* DECL may contain more parameters than TMPL due to the extra
2211 in-charge parameter in constructors and destructors. */
2212 in_charge = spec_types;
2213 spec_types = TREE_CHAIN (spec_types);
2214 }
2215 if (DECL_HAS_VTT_PARM_P (decl))
2216 {
2217 vtt = spec_types;
2218 spec_types = TREE_CHAIN (spec_types);
2219 }
2220 }
2221
2222 /* Compute the merged default arguments. */
2223 new_spec_types =
2224 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2225
2226 /* Compute the new FUNCTION_TYPE. */
2227 if (object_type)
2228 {
2229 if (vtt)
2230 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2231 TREE_VALUE (vtt),
2232 new_spec_types);
2233
2234 if (in_charge)
2235 /* Put the in-charge parameter back. */
2236 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2237 TREE_VALUE (in_charge),
2238 new_spec_types);
2239
2240 new_type = build_method_type_directly (object_type,
2241 TREE_TYPE (old_type),
2242 new_spec_types);
2243 }
2244 else
2245 new_type = build_function_type (TREE_TYPE (old_type),
2246 new_spec_types);
2247 new_type = cp_build_type_attribute_variant (new_type,
2248 TYPE_ATTRIBUTES (old_type));
2249 new_type = build_exception_variant (new_type,
2250 TYPE_RAISES_EXCEPTIONS (old_type));
2251 TREE_TYPE (decl) = new_type;
2252 }
2253
2254 /* Return the number of template headers we expect to see for a definition
2255 or specialization of CTYPE or one of its non-template members. */
2256
2257 int
2258 num_template_headers_for_class (tree ctype)
2259 {
2260 int num_templates = 0;
2261
2262 while (ctype && CLASS_TYPE_P (ctype))
2263 {
2264 /* You're supposed to have one `template <...>' for every
2265 template class, but you don't need one for a full
2266 specialization. For example:
2267
2268 template <class T> struct S{};
2269 template <> struct S<int> { void f(); };
2270 void S<int>::f () {}
2271
2272 is correct; there shouldn't be a `template <>' for the
2273 definition of `S<int>::f'. */
2274 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2275 /* If CTYPE does not have template information of any
2276 kind, then it is not a template, nor is it nested
2277 within a template. */
2278 break;
2279 if (explicit_class_specialization_p (ctype))
2280 break;
2281 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2282 ++num_templates;
2283
2284 ctype = TYPE_CONTEXT (ctype);
2285 }
2286
2287 return num_templates;
2288 }
2289
2290 /* Do a simple sanity check on the template headers that precede the
2291 variable declaration DECL. */
2292
2293 void
2294 check_template_variable (tree decl)
2295 {
2296 tree ctx = CP_DECL_CONTEXT (decl);
2297 int wanted = num_template_headers_for_class (ctx);
2298 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2299 permerror (DECL_SOURCE_LOCATION (decl),
2300 "%qD is not a static data member of a class template", decl);
2301 else if (template_header_count > wanted)
2302 {
2303 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2304 "too many template headers for %D (should be %d)",
2305 decl, wanted);
2306 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2307 inform (DECL_SOURCE_LOCATION (decl),
2308 "members of an explicitly specialized class are defined "
2309 "without a template header");
2310 }
2311 }
2312
2313 /* Check to see if the function just declared, as indicated in
2314 DECLARATOR, and in DECL, is a specialization of a function
2315 template. We may also discover that the declaration is an explicit
2316 instantiation at this point.
2317
2318 Returns DECL, or an equivalent declaration that should be used
2319 instead if all goes well. Issues an error message if something is
2320 amiss. Returns error_mark_node if the error is not easily
2321 recoverable.
2322
2323 FLAGS is a bitmask consisting of the following flags:
2324
2325 2: The function has a definition.
2326 4: The function is a friend.
2327
2328 The TEMPLATE_COUNT is the number of references to qualifying
2329 template classes that appeared in the name of the function. For
2330 example, in
2331
2332 template <class T> struct S { void f(); };
2333 void S<int>::f();
2334
2335 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2336 classes are not counted in the TEMPLATE_COUNT, so that in
2337
2338 template <class T> struct S {};
2339 template <> struct S<int> { void f(); }
2340 template <> void S<int>::f();
2341
2342 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2343 invalid; there should be no template <>.)
2344
2345 If the function is a specialization, it is marked as such via
2346 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2347 is set up correctly, and it is added to the list of specializations
2348 for that template. */
2349
2350 tree
2351 check_explicit_specialization (tree declarator,
2352 tree decl,
2353 int template_count,
2354 int flags)
2355 {
2356 int have_def = flags & 2;
2357 int is_friend = flags & 4;
2358 int specialization = 0;
2359 int explicit_instantiation = 0;
2360 int member_specialization = 0;
2361 tree ctype = DECL_CLASS_CONTEXT (decl);
2362 tree dname = DECL_NAME (decl);
2363 tmpl_spec_kind tsk;
2364
2365 if (is_friend)
2366 {
2367 if (!processing_specialization)
2368 tsk = tsk_none;
2369 else
2370 tsk = tsk_excessive_parms;
2371 }
2372 else
2373 tsk = current_tmpl_spec_kind (template_count);
2374
2375 switch (tsk)
2376 {
2377 case tsk_none:
2378 if (processing_specialization)
2379 {
2380 specialization = 1;
2381 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2382 }
2383 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2384 {
2385 if (is_friend)
2386 /* This could be something like:
2387
2388 template <class T> void f(T);
2389 class S { friend void f<>(int); } */
2390 specialization = 1;
2391 else
2392 {
2393 /* This case handles bogus declarations like template <>
2394 template <class T> void f<int>(); */
2395
2396 error ("template-id %qD in declaration of primary template",
2397 declarator);
2398 return decl;
2399 }
2400 }
2401 break;
2402
2403 case tsk_invalid_member_spec:
2404 /* The error has already been reported in
2405 check_specialization_scope. */
2406 return error_mark_node;
2407
2408 case tsk_invalid_expl_inst:
2409 error ("template parameter list used in explicit instantiation");
2410
2411 /* Fall through. */
2412
2413 case tsk_expl_inst:
2414 if (have_def)
2415 error ("definition provided for explicit instantiation");
2416
2417 explicit_instantiation = 1;
2418 break;
2419
2420 case tsk_excessive_parms:
2421 case tsk_insufficient_parms:
2422 if (tsk == tsk_excessive_parms)
2423 error ("too many template parameter lists in declaration of %qD",
2424 decl);
2425 else if (template_header_count)
2426 error("too few template parameter lists in declaration of %qD", decl);
2427 else
2428 error("explicit specialization of %qD must be introduced by "
2429 "%<template <>%>", decl);
2430
2431 /* Fall through. */
2432 case tsk_expl_spec:
2433 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2434 if (ctype)
2435 member_specialization = 1;
2436 else
2437 specialization = 1;
2438 break;
2439
2440 case tsk_template:
2441 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2442 {
2443 /* This case handles bogus declarations like template <>
2444 template <class T> void f<int>(); */
2445
2446 if (uses_template_parms (declarator))
2447 error ("function template partial specialization %qD "
2448 "is not allowed", declarator);
2449 else
2450 error ("template-id %qD in declaration of primary template",
2451 declarator);
2452 return decl;
2453 }
2454
2455 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2456 /* This is a specialization of a member template, without
2457 specialization the containing class. Something like:
2458
2459 template <class T> struct S {
2460 template <class U> void f (U);
2461 };
2462 template <> template <class U> void S<int>::f(U) {}
2463
2464 That's a specialization -- but of the entire template. */
2465 specialization = 1;
2466 break;
2467
2468 default:
2469 gcc_unreachable ();
2470 }
2471
2472 if (specialization || member_specialization)
2473 {
2474 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2475 for (; t; t = TREE_CHAIN (t))
2476 if (TREE_PURPOSE (t))
2477 {
2478 permerror (input_location,
2479 "default argument specified in explicit specialization");
2480 break;
2481 }
2482 }
2483
2484 if (specialization || member_specialization || explicit_instantiation)
2485 {
2486 tree tmpl = NULL_TREE;
2487 tree targs = NULL_TREE;
2488
2489 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2490 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2491 {
2492 tree fns;
2493
2494 gcc_assert (identifier_p (declarator));
2495 if (ctype)
2496 fns = dname;
2497 else
2498 {
2499 /* If there is no class context, the explicit instantiation
2500 must be at namespace scope. */
2501 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2502
2503 /* Find the namespace binding, using the declaration
2504 context. */
2505 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2506 false, true);
2507 if (fns == error_mark_node || !is_overloaded_fn (fns))
2508 {
2509 error ("%qD is not a template function", dname);
2510 fns = error_mark_node;
2511 }
2512 else
2513 {
2514 tree fn = OVL_CURRENT (fns);
2515 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2516 CP_DECL_CONTEXT (fn)))
2517 error ("%qD is not declared in %qD",
2518 decl, current_namespace);
2519 }
2520 }
2521
2522 declarator = lookup_template_function (fns, NULL_TREE);
2523 }
2524
2525 if (declarator == error_mark_node)
2526 return error_mark_node;
2527
2528 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2529 {
2530 if (!explicit_instantiation)
2531 /* A specialization in class scope. This is invalid,
2532 but the error will already have been flagged by
2533 check_specialization_scope. */
2534 return error_mark_node;
2535 else
2536 {
2537 /* It's not valid to write an explicit instantiation in
2538 class scope, e.g.:
2539
2540 class C { template void f(); }
2541
2542 This case is caught by the parser. However, on
2543 something like:
2544
2545 template class C { void f(); };
2546
2547 (which is invalid) we can get here. The error will be
2548 issued later. */
2549 ;
2550 }
2551
2552 return decl;
2553 }
2554 else if (ctype != NULL_TREE
2555 && (identifier_p (TREE_OPERAND (declarator, 0))))
2556 {
2557 /* Find the list of functions in ctype that have the same
2558 name as the declared function. */
2559 tree name = TREE_OPERAND (declarator, 0);
2560 tree fns = NULL_TREE;
2561 int idx;
2562
2563 if (constructor_name_p (name, ctype))
2564 {
2565 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2566
2567 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2568 : !CLASSTYPE_DESTRUCTORS (ctype))
2569 {
2570 /* From [temp.expl.spec]:
2571
2572 If such an explicit specialization for the member
2573 of a class template names an implicitly-declared
2574 special member function (clause _special_), the
2575 program is ill-formed.
2576
2577 Similar language is found in [temp.explicit]. */
2578 error ("specialization of implicitly-declared special member function");
2579 return error_mark_node;
2580 }
2581
2582 name = is_constructor ? ctor_identifier : dtor_identifier;
2583 }
2584
2585 if (!DECL_CONV_FN_P (decl))
2586 {
2587 idx = lookup_fnfields_1 (ctype, name);
2588 if (idx >= 0)
2589 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2590 }
2591 else
2592 {
2593 vec<tree, va_gc> *methods;
2594 tree ovl;
2595
2596 /* For a type-conversion operator, we cannot do a
2597 name-based lookup. We might be looking for `operator
2598 int' which will be a specialization of `operator T'.
2599 So, we find *all* the conversion operators, and then
2600 select from them. */
2601 fns = NULL_TREE;
2602
2603 methods = CLASSTYPE_METHOD_VEC (ctype);
2604 if (methods)
2605 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2606 methods->iterate (idx, &ovl);
2607 ++idx)
2608 {
2609 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2610 /* There are no more conversion functions. */
2611 break;
2612
2613 /* Glue all these conversion functions together
2614 with those we already have. */
2615 for (; ovl; ovl = OVL_NEXT (ovl))
2616 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2617 }
2618 }
2619
2620 if (fns == NULL_TREE)
2621 {
2622 error ("no member function %qD declared in %qT", name, ctype);
2623 return error_mark_node;
2624 }
2625 else
2626 TREE_OPERAND (declarator, 0) = fns;
2627 }
2628
2629 /* Figure out what exactly is being specialized at this point.
2630 Note that for an explicit instantiation, even one for a
2631 member function, we cannot tell apriori whether the
2632 instantiation is for a member template, or just a member
2633 function of a template class. Even if a member template is
2634 being instantiated, the member template arguments may be
2635 elided if they can be deduced from the rest of the
2636 declaration. */
2637 tmpl = determine_specialization (declarator, decl,
2638 &targs,
2639 member_specialization,
2640 template_count,
2641 tsk);
2642
2643 if (!tmpl || tmpl == error_mark_node)
2644 /* We couldn't figure out what this declaration was
2645 specializing. */
2646 return error_mark_node;
2647 else
2648 {
2649 tree gen_tmpl = most_general_template (tmpl);
2650
2651 if (explicit_instantiation)
2652 {
2653 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2654 is done by do_decl_instantiation later. */
2655
2656 int arg_depth = TMPL_ARGS_DEPTH (targs);
2657 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2658
2659 if (arg_depth > parm_depth)
2660 {
2661 /* If TMPL is not the most general template (for
2662 example, if TMPL is a friend template that is
2663 injected into namespace scope), then there will
2664 be too many levels of TARGS. Remove some of them
2665 here. */
2666 int i;
2667 tree new_targs;
2668
2669 new_targs = make_tree_vec (parm_depth);
2670 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2671 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2672 = TREE_VEC_ELT (targs, i);
2673 targs = new_targs;
2674 }
2675
2676 return instantiate_template (tmpl, targs, tf_error);
2677 }
2678
2679 /* If we thought that the DECL was a member function, but it
2680 turns out to be specializing a static member function,
2681 make DECL a static member function as well. */
2682 if (DECL_STATIC_FUNCTION_P (tmpl)
2683 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2684 revert_static_member_fn (decl);
2685
2686 /* If this is a specialization of a member template of a
2687 template class, we want to return the TEMPLATE_DECL, not
2688 the specialization of it. */
2689 if (tsk == tsk_template)
2690 {
2691 tree result = DECL_TEMPLATE_RESULT (tmpl);
2692 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2693 DECL_INITIAL (result) = NULL_TREE;
2694 if (have_def)
2695 {
2696 tree parm;
2697 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2698 DECL_SOURCE_LOCATION (result)
2699 = DECL_SOURCE_LOCATION (decl);
2700 /* We want to use the argument list specified in the
2701 definition, not in the original declaration. */
2702 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2703 for (parm = DECL_ARGUMENTS (result); parm;
2704 parm = DECL_CHAIN (parm))
2705 DECL_CONTEXT (parm) = result;
2706 }
2707 return register_specialization (tmpl, gen_tmpl, targs,
2708 is_friend, 0);
2709 }
2710
2711 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2712 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2713
2714 /* Inherit default function arguments from the template
2715 DECL is specializing. */
2716 copy_default_args_to_explicit_spec (decl);
2717
2718 /* This specialization has the same protection as the
2719 template it specializes. */
2720 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2721 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2722
2723 /* 7.1.1-1 [dcl.stc]
2724
2725 A storage-class-specifier shall not be specified in an
2726 explicit specialization...
2727
2728 The parser rejects these, so unless action is taken here,
2729 explicit function specializations will always appear with
2730 global linkage.
2731
2732 The action recommended by the C++ CWG in response to C++
2733 defect report 605 is to make the storage class and linkage
2734 of the explicit specialization match the templated function:
2735
2736 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2737 */
2738 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2739 {
2740 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2741 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2742
2743 /* This specialization has the same linkage and visibility as
2744 the function template it specializes. */
2745 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2746 if (! TREE_PUBLIC (decl))
2747 {
2748 DECL_INTERFACE_KNOWN (decl) = 1;
2749 DECL_NOT_REALLY_EXTERN (decl) = 1;
2750 }
2751 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2752 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2753 {
2754 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2755 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2756 }
2757 }
2758
2759 /* If DECL is a friend declaration, declared using an
2760 unqualified name, the namespace associated with DECL may
2761 have been set incorrectly. For example, in:
2762
2763 template <typename T> void f(T);
2764 namespace N {
2765 struct S { friend void f<int>(int); }
2766 }
2767
2768 we will have set the DECL_CONTEXT for the friend
2769 declaration to N, rather than to the global namespace. */
2770 if (DECL_NAMESPACE_SCOPE_P (decl))
2771 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2772
2773 if (is_friend && !have_def)
2774 /* This is not really a declaration of a specialization.
2775 It's just the name of an instantiation. But, it's not
2776 a request for an instantiation, either. */
2777 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2778 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2779 /* This is indeed a specialization. In case of constructors
2780 and destructors, we need in-charge and not-in-charge
2781 versions in V3 ABI. */
2782 clone_function_decl (decl, /*update_method_vec_p=*/0);
2783
2784 /* Register this specialization so that we can find it
2785 again. */
2786 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2787 }
2788 }
2789
2790 return decl;
2791 }
2792
2793 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2794 parameters. These are represented in the same format used for
2795 DECL_TEMPLATE_PARMS. */
2796
2797 int
2798 comp_template_parms (const_tree parms1, const_tree parms2)
2799 {
2800 const_tree p1;
2801 const_tree p2;
2802
2803 if (parms1 == parms2)
2804 return 1;
2805
2806 for (p1 = parms1, p2 = parms2;
2807 p1 != NULL_TREE && p2 != NULL_TREE;
2808 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2809 {
2810 tree t1 = TREE_VALUE (p1);
2811 tree t2 = TREE_VALUE (p2);
2812 int i;
2813
2814 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2815 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2816
2817 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2818 return 0;
2819
2820 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2821 {
2822 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2823 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2824
2825 /* If either of the template parameters are invalid, assume
2826 they match for the sake of error recovery. */
2827 if (parm1 == error_mark_node || parm2 == error_mark_node)
2828 return 1;
2829
2830 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2831 return 0;
2832
2833 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2834 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2835 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2836 continue;
2837 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2838 return 0;
2839 }
2840 }
2841
2842 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2843 /* One set of parameters has more parameters lists than the
2844 other. */
2845 return 0;
2846
2847 return 1;
2848 }
2849
2850 /* Determine whether PARM is a parameter pack. */
2851
2852 bool
2853 template_parameter_pack_p (const_tree parm)
2854 {
2855 /* Determine if we have a non-type template parameter pack. */
2856 if (TREE_CODE (parm) == PARM_DECL)
2857 return (DECL_TEMPLATE_PARM_P (parm)
2858 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2859 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2860 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2861
2862 /* If this is a list of template parameters, we could get a
2863 TYPE_DECL or a TEMPLATE_DECL. */
2864 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2865 parm = TREE_TYPE (parm);
2866
2867 /* Otherwise it must be a type template parameter. */
2868 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2869 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2870 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2871 }
2872
2873 /* Determine if T is a function parameter pack. */
2874
2875 bool
2876 function_parameter_pack_p (const_tree t)
2877 {
2878 if (t && TREE_CODE (t) == PARM_DECL)
2879 return DECL_PACK_P (t);
2880 return false;
2881 }
2882
2883 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2884 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2885
2886 tree
2887 get_function_template_decl (const_tree primary_func_tmpl_inst)
2888 {
2889 if (! primary_func_tmpl_inst
2890 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2891 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2892 return NULL;
2893
2894 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2895 }
2896
2897 /* Return true iff the function parameter PARAM_DECL was expanded
2898 from the function parameter pack PACK. */
2899
2900 bool
2901 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2902 {
2903 if (DECL_ARTIFICIAL (param_decl)
2904 || !function_parameter_pack_p (pack))
2905 return false;
2906
2907 /* The parameter pack and its pack arguments have the same
2908 DECL_PARM_INDEX. */
2909 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2910 }
2911
2912 /* Determine whether ARGS describes a variadic template args list,
2913 i.e., one that is terminated by a template argument pack. */
2914
2915 static bool
2916 template_args_variadic_p (tree args)
2917 {
2918 int nargs;
2919 tree last_parm;
2920
2921 if (args == NULL_TREE)
2922 return false;
2923
2924 args = INNERMOST_TEMPLATE_ARGS (args);
2925 nargs = TREE_VEC_LENGTH (args);
2926
2927 if (nargs == 0)
2928 return false;
2929
2930 last_parm = TREE_VEC_ELT (args, nargs - 1);
2931
2932 return ARGUMENT_PACK_P (last_parm);
2933 }
2934
2935 /* Generate a new name for the parameter pack name NAME (an
2936 IDENTIFIER_NODE) that incorporates its */
2937
2938 static tree
2939 make_ith_pack_parameter_name (tree name, int i)
2940 {
2941 /* Munge the name to include the parameter index. */
2942 #define NUMBUF_LEN 128
2943 char numbuf[NUMBUF_LEN];
2944 char* newname;
2945 int newname_len;
2946
2947 if (name == NULL_TREE)
2948 return name;
2949 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2950 newname_len = IDENTIFIER_LENGTH (name)
2951 + strlen (numbuf) + 2;
2952 newname = (char*)alloca (newname_len);
2953 snprintf (newname, newname_len,
2954 "%s#%i", IDENTIFIER_POINTER (name), i);
2955 return get_identifier (newname);
2956 }
2957
2958 /* Return true if T is a primary function, class or alias template
2959 instantiation. */
2960
2961 bool
2962 primary_template_instantiation_p (const_tree t)
2963 {
2964 if (!t)
2965 return false;
2966
2967 if (TREE_CODE (t) == FUNCTION_DECL)
2968 return DECL_LANG_SPECIFIC (t)
2969 && DECL_TEMPLATE_INSTANTIATION (t)
2970 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2971 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2972 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2973 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2974 else if (alias_template_specialization_p (t))
2975 return true;
2976 return false;
2977 }
2978
2979 /* Return true if PARM is a template template parameter. */
2980
2981 bool
2982 template_template_parameter_p (const_tree parm)
2983 {
2984 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2985 }
2986
2987 /* Return true iff PARM is a DECL representing a type template
2988 parameter. */
2989
2990 bool
2991 template_type_parameter_p (const_tree parm)
2992 {
2993 return (parm
2994 && (TREE_CODE (parm) == TYPE_DECL
2995 || TREE_CODE (parm) == TEMPLATE_DECL)
2996 && DECL_TEMPLATE_PARM_P (parm));
2997 }
2998
2999 /* Return the template parameters of T if T is a
3000 primary template instantiation, NULL otherwise. */
3001
3002 tree
3003 get_primary_template_innermost_parameters (const_tree t)
3004 {
3005 tree parms = NULL, template_info = NULL;
3006
3007 if ((template_info = get_template_info (t))
3008 && primary_template_instantiation_p (t))
3009 parms = INNERMOST_TEMPLATE_PARMS
3010 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3011
3012 return parms;
3013 }
3014
3015 /* Return the template parameters of the LEVELth level from the full list
3016 of template parameters PARMS. */
3017
3018 tree
3019 get_template_parms_at_level (tree parms, int level)
3020 {
3021 tree p;
3022 if (!parms
3023 || TREE_CODE (parms) != TREE_LIST
3024 || level > TMPL_PARMS_DEPTH (parms))
3025 return NULL_TREE;
3026
3027 for (p = parms; p; p = TREE_CHAIN (p))
3028 if (TMPL_PARMS_DEPTH (p) == level)
3029 return p;
3030
3031 return NULL_TREE;
3032 }
3033
3034 /* Returns the template arguments of T if T is a template instantiation,
3035 NULL otherwise. */
3036
3037 tree
3038 get_template_innermost_arguments (const_tree t)
3039 {
3040 tree args = NULL, template_info = NULL;
3041
3042 if ((template_info = get_template_info (t))
3043 && TI_ARGS (template_info))
3044 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3045
3046 return args;
3047 }
3048
3049 /* Return the argument pack elements of T if T is a template argument pack,
3050 NULL otherwise. */
3051
3052 tree
3053 get_template_argument_pack_elems (const_tree t)
3054 {
3055 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3056 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3057 return NULL;
3058
3059 return ARGUMENT_PACK_ARGS (t);
3060 }
3061
3062 /* Structure used to track the progress of find_parameter_packs_r. */
3063 struct find_parameter_pack_data
3064 {
3065 /* TREE_LIST that will contain all of the parameter packs found by
3066 the traversal. */
3067 tree* parameter_packs;
3068
3069 /* Set of AST nodes that have been visited by the traversal. */
3070 struct pointer_set_t *visited;
3071 };
3072
3073 /* Identifies all of the argument packs that occur in a template
3074 argument and appends them to the TREE_LIST inside DATA, which is a
3075 find_parameter_pack_data structure. This is a subroutine of
3076 make_pack_expansion and uses_parameter_packs. */
3077 static tree
3078 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3079 {
3080 tree t = *tp;
3081 struct find_parameter_pack_data* ppd =
3082 (struct find_parameter_pack_data*)data;
3083 bool parameter_pack_p = false;
3084
3085 /* Handle type aliases/typedefs. */
3086 if (TYPE_ALIAS_P (t))
3087 {
3088 if (TYPE_TEMPLATE_INFO (t))
3089 cp_walk_tree (&TYPE_TI_ARGS (t),
3090 &find_parameter_packs_r,
3091 ppd, ppd->visited);
3092 *walk_subtrees = 0;
3093 return NULL_TREE;
3094 }
3095
3096 /* Identify whether this is a parameter pack or not. */
3097 switch (TREE_CODE (t))
3098 {
3099 case TEMPLATE_PARM_INDEX:
3100 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3101 parameter_pack_p = true;
3102 break;
3103
3104 case TEMPLATE_TYPE_PARM:
3105 t = TYPE_MAIN_VARIANT (t);
3106 case TEMPLATE_TEMPLATE_PARM:
3107 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3108 parameter_pack_p = true;
3109 break;
3110
3111 case FIELD_DECL:
3112 case PARM_DECL:
3113 if (DECL_PACK_P (t))
3114 {
3115 /* We don't want to walk into the type of a PARM_DECL,
3116 because we don't want to see the type parameter pack. */
3117 *walk_subtrees = 0;
3118 parameter_pack_p = true;
3119 }
3120 break;
3121
3122 /* Look through a lambda capture proxy to the field pack. */
3123 case VAR_DECL:
3124 if (DECL_HAS_VALUE_EXPR_P (t))
3125 {
3126 tree v = DECL_VALUE_EXPR (t);
3127 cp_walk_tree (&v,
3128 &find_parameter_packs_r,
3129 ppd, ppd->visited);
3130 *walk_subtrees = 0;
3131 }
3132 break;
3133
3134 case BASES:
3135 parameter_pack_p = true;
3136 break;
3137 default:
3138 /* Not a parameter pack. */
3139 break;
3140 }
3141
3142 if (parameter_pack_p)
3143 {
3144 /* Add this parameter pack to the list. */
3145 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3146 }
3147
3148 if (TYPE_P (t))
3149 cp_walk_tree (&TYPE_CONTEXT (t),
3150 &find_parameter_packs_r, ppd, ppd->visited);
3151
3152 /* This switch statement will return immediately if we don't find a
3153 parameter pack. */
3154 switch (TREE_CODE (t))
3155 {
3156 case TEMPLATE_PARM_INDEX:
3157 return NULL_TREE;
3158
3159 case BOUND_TEMPLATE_TEMPLATE_PARM:
3160 /* Check the template itself. */
3161 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3162 &find_parameter_packs_r, ppd, ppd->visited);
3163 /* Check the template arguments. */
3164 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3165 ppd->visited);
3166 *walk_subtrees = 0;
3167 return NULL_TREE;
3168
3169 case TEMPLATE_TYPE_PARM:
3170 case TEMPLATE_TEMPLATE_PARM:
3171 return NULL_TREE;
3172
3173 case PARM_DECL:
3174 return NULL_TREE;
3175
3176 case RECORD_TYPE:
3177 if (TYPE_PTRMEMFUNC_P (t))
3178 return NULL_TREE;
3179 /* Fall through. */
3180
3181 case UNION_TYPE:
3182 case ENUMERAL_TYPE:
3183 if (TYPE_TEMPLATE_INFO (t))
3184 cp_walk_tree (&TYPE_TI_ARGS (t),
3185 &find_parameter_packs_r, ppd, ppd->visited);
3186
3187 *walk_subtrees = 0;
3188 return NULL_TREE;
3189
3190 case CONSTRUCTOR:
3191 case TEMPLATE_DECL:
3192 cp_walk_tree (&TREE_TYPE (t),
3193 &find_parameter_packs_r, ppd, ppd->visited);
3194 return NULL_TREE;
3195
3196 case TYPENAME_TYPE:
3197 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3198 ppd, ppd->visited);
3199 *walk_subtrees = 0;
3200 return NULL_TREE;
3201
3202 case TYPE_PACK_EXPANSION:
3203 case EXPR_PACK_EXPANSION:
3204 *walk_subtrees = 0;
3205 return NULL_TREE;
3206
3207 case INTEGER_TYPE:
3208 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3209 ppd, ppd->visited);
3210 *walk_subtrees = 0;
3211 return NULL_TREE;
3212
3213 case IDENTIFIER_NODE:
3214 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3215 ppd->visited);
3216 *walk_subtrees = 0;
3217 return NULL_TREE;
3218
3219 default:
3220 return NULL_TREE;
3221 }
3222
3223 return NULL_TREE;
3224 }
3225
3226 /* Determines if the expression or type T uses any parameter packs. */
3227 bool
3228 uses_parameter_packs (tree t)
3229 {
3230 tree parameter_packs = NULL_TREE;
3231 struct find_parameter_pack_data ppd;
3232 ppd.parameter_packs = &parameter_packs;
3233 ppd.visited = pointer_set_create ();
3234 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3235 pointer_set_destroy (ppd.visited);
3236 return parameter_packs != NULL_TREE;
3237 }
3238
3239 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3240 representation a base-class initializer into a parameter pack
3241 expansion. If all goes well, the resulting node will be an
3242 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3243 respectively. */
3244 tree
3245 make_pack_expansion (tree arg)
3246 {
3247 tree result;
3248 tree parameter_packs = NULL_TREE;
3249 bool for_types = false;
3250 struct find_parameter_pack_data ppd;
3251
3252 if (!arg || arg == error_mark_node)
3253 return arg;
3254
3255 if (TREE_CODE (arg) == TREE_LIST)
3256 {
3257 /* The only time we will see a TREE_LIST here is for a base
3258 class initializer. In this case, the TREE_PURPOSE will be a
3259 _TYPE node (representing the base class expansion we're
3260 initializing) and the TREE_VALUE will be a TREE_LIST
3261 containing the initialization arguments.
3262
3263 The resulting expansion looks somewhat different from most
3264 expansions. Rather than returning just one _EXPANSION, we
3265 return a TREE_LIST whose TREE_PURPOSE is a
3266 TYPE_PACK_EXPANSION containing the bases that will be
3267 initialized. The TREE_VALUE will be identical to the
3268 original TREE_VALUE, which is a list of arguments that will
3269 be passed to each base. We do not introduce any new pack
3270 expansion nodes into the TREE_VALUE (although it is possible
3271 that some already exist), because the TREE_PURPOSE and
3272 TREE_VALUE all need to be expanded together with the same
3273 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3274 resulting TREE_PURPOSE will mention the parameter packs in
3275 both the bases and the arguments to the bases. */
3276 tree purpose;
3277 tree value;
3278 tree parameter_packs = NULL_TREE;
3279
3280 /* Determine which parameter packs will be used by the base
3281 class expansion. */
3282 ppd.visited = pointer_set_create ();
3283 ppd.parameter_packs = &parameter_packs;
3284 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3285 &ppd, ppd.visited);
3286
3287 if (parameter_packs == NULL_TREE)
3288 {
3289 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3290 pointer_set_destroy (ppd.visited);
3291 return error_mark_node;
3292 }
3293
3294 if (TREE_VALUE (arg) != void_type_node)
3295 {
3296 /* Collect the sets of parameter packs used in each of the
3297 initialization arguments. */
3298 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3299 {
3300 /* Determine which parameter packs will be expanded in this
3301 argument. */
3302 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3303 &ppd, ppd.visited);
3304 }
3305 }
3306
3307 pointer_set_destroy (ppd.visited);
3308
3309 /* Create the pack expansion type for the base type. */
3310 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3311 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3312 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3313
3314 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3315 they will rarely be compared to anything. */
3316 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3317
3318 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3319 }
3320
3321 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3322 for_types = true;
3323
3324 /* Build the PACK_EXPANSION_* node. */
3325 result = for_types
3326 ? cxx_make_type (TYPE_PACK_EXPANSION)
3327 : make_node (EXPR_PACK_EXPANSION);
3328 SET_PACK_EXPANSION_PATTERN (result, arg);
3329 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3330 {
3331 /* Propagate type and const-expression information. */
3332 TREE_TYPE (result) = TREE_TYPE (arg);
3333 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3334 }
3335 else
3336 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3337 they will rarely be compared to anything. */
3338 SET_TYPE_STRUCTURAL_EQUALITY (result);
3339
3340 /* Determine which parameter packs will be expanded. */
3341 ppd.parameter_packs = &parameter_packs;
3342 ppd.visited = pointer_set_create ();
3343 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3344 pointer_set_destroy (ppd.visited);
3345
3346 /* Make sure we found some parameter packs. */
3347 if (parameter_packs == NULL_TREE)
3348 {
3349 if (TYPE_P (arg))
3350 error ("expansion pattern %<%T%> contains no argument packs", arg);
3351 else
3352 error ("expansion pattern %<%E%> contains no argument packs", arg);
3353 return error_mark_node;
3354 }
3355 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3356
3357 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3358
3359 return result;
3360 }
3361
3362 /* Checks T for any "bare" parameter packs, which have not yet been
3363 expanded, and issues an error if any are found. This operation can
3364 only be done on full expressions or types (e.g., an expression
3365 statement, "if" condition, etc.), because we could have expressions like:
3366
3367 foo(f(g(h(args)))...)
3368
3369 where "args" is a parameter pack. check_for_bare_parameter_packs
3370 should not be called for the subexpressions args, h(args),
3371 g(h(args)), or f(g(h(args))), because we would produce erroneous
3372 error messages.
3373
3374 Returns TRUE and emits an error if there were bare parameter packs,
3375 returns FALSE otherwise. */
3376 bool
3377 check_for_bare_parameter_packs (tree t)
3378 {
3379 tree parameter_packs = NULL_TREE;
3380 struct find_parameter_pack_data ppd;
3381
3382 if (!processing_template_decl || !t || t == error_mark_node)
3383 return false;
3384
3385 if (TREE_CODE (t) == TYPE_DECL)
3386 t = TREE_TYPE (t);
3387
3388 ppd.parameter_packs = &parameter_packs;
3389 ppd.visited = pointer_set_create ();
3390 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3391 pointer_set_destroy (ppd.visited);
3392
3393 if (parameter_packs)
3394 {
3395 error ("parameter packs not expanded with %<...%>:");
3396 while (parameter_packs)
3397 {
3398 tree pack = TREE_VALUE (parameter_packs);
3399 tree name = NULL_TREE;
3400
3401 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3402 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3403 name = TYPE_NAME (pack);
3404 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3405 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3406 else
3407 name = DECL_NAME (pack);
3408
3409 if (name)
3410 inform (input_location, " %qD", name);
3411 else
3412 inform (input_location, " <anonymous>");
3413
3414 parameter_packs = TREE_CHAIN (parameter_packs);
3415 }
3416
3417 return true;
3418 }
3419
3420 return false;
3421 }
3422
3423 /* Expand any parameter packs that occur in the template arguments in
3424 ARGS. */
3425 tree
3426 expand_template_argument_pack (tree args)
3427 {
3428 tree result_args = NULL_TREE;
3429 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3430 int num_result_args = -1;
3431 int non_default_args_count = -1;
3432
3433 /* First, determine if we need to expand anything, and the number of
3434 slots we'll need. */
3435 for (in_arg = 0; in_arg < nargs; ++in_arg)
3436 {
3437 tree arg = TREE_VEC_ELT (args, in_arg);
3438 if (arg == NULL_TREE)
3439 return args;
3440 if (ARGUMENT_PACK_P (arg))
3441 {
3442 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3443 if (num_result_args < 0)
3444 num_result_args = in_arg + num_packed;
3445 else
3446 num_result_args += num_packed;
3447 }
3448 else
3449 {
3450 if (num_result_args >= 0)
3451 num_result_args++;
3452 }
3453 }
3454
3455 /* If no expansion is necessary, we're done. */
3456 if (num_result_args < 0)
3457 return args;
3458
3459 /* Expand arguments. */
3460 result_args = make_tree_vec (num_result_args);
3461 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3462 non_default_args_count =
3463 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3464 for (in_arg = 0; in_arg < nargs; ++in_arg)
3465 {
3466 tree arg = TREE_VEC_ELT (args, in_arg);
3467 if (ARGUMENT_PACK_P (arg))
3468 {
3469 tree packed = ARGUMENT_PACK_ARGS (arg);
3470 int i, num_packed = TREE_VEC_LENGTH (packed);
3471 for (i = 0; i < num_packed; ++i, ++out_arg)
3472 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3473 if (non_default_args_count > 0)
3474 non_default_args_count += num_packed;
3475 }
3476 else
3477 {
3478 TREE_VEC_ELT (result_args, out_arg) = arg;
3479 ++out_arg;
3480 }
3481 }
3482 if (non_default_args_count >= 0)
3483 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3484 return result_args;
3485 }
3486
3487 /* Checks if DECL shadows a template parameter.
3488
3489 [temp.local]: A template-parameter shall not be redeclared within its
3490 scope (including nested scopes).
3491
3492 Emits an error and returns TRUE if the DECL shadows a parameter,
3493 returns FALSE otherwise. */
3494
3495 bool
3496 check_template_shadow (tree decl)
3497 {
3498 tree olddecl;
3499
3500 /* If we're not in a template, we can't possibly shadow a template
3501 parameter. */
3502 if (!current_template_parms)
3503 return true;
3504
3505 /* Figure out what we're shadowing. */
3506 if (TREE_CODE (decl) == OVERLOAD)
3507 decl = OVL_CURRENT (decl);
3508 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3509
3510 /* If there's no previous binding for this name, we're not shadowing
3511 anything, let alone a template parameter. */
3512 if (!olddecl)
3513 return true;
3514
3515 /* If we're not shadowing a template parameter, we're done. Note
3516 that OLDDECL might be an OVERLOAD (or perhaps even an
3517 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3518 node. */
3519 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3520 return true;
3521
3522 /* We check for decl != olddecl to avoid bogus errors for using a
3523 name inside a class. We check TPFI to avoid duplicate errors for
3524 inline member templates. */
3525 if (decl == olddecl
3526 || (DECL_TEMPLATE_PARM_P (decl)
3527 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3528 return true;
3529
3530 error ("declaration of %q+#D", decl);
3531 error (" shadows template parm %q+#D", olddecl);
3532 return false;
3533 }
3534
3535 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3536 ORIG_LEVEL, DECL, and TYPE. */
3537
3538 static tree
3539 build_template_parm_index (int index,
3540 int level,
3541 int orig_level,
3542 tree decl,
3543 tree type)
3544 {
3545 tree t = make_node (TEMPLATE_PARM_INDEX);
3546 TEMPLATE_PARM_IDX (t) = index;
3547 TEMPLATE_PARM_LEVEL (t) = level;
3548 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3549 TEMPLATE_PARM_DECL (t) = decl;
3550 TREE_TYPE (t) = type;
3551 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3552 TREE_READONLY (t) = TREE_READONLY (decl);
3553
3554 return t;
3555 }
3556
3557 /* Find the canonical type parameter for the given template type
3558 parameter. Returns the canonical type parameter, which may be TYPE
3559 if no such parameter existed. */
3560
3561 static tree
3562 canonical_type_parameter (tree type)
3563 {
3564 tree list;
3565 int idx = TEMPLATE_TYPE_IDX (type);
3566 if (!canonical_template_parms)
3567 vec_alloc (canonical_template_parms, idx+1);
3568
3569 while (canonical_template_parms->length () <= (unsigned)idx)
3570 vec_safe_push (canonical_template_parms, NULL_TREE);
3571
3572 list = (*canonical_template_parms)[idx];
3573 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3574 list = TREE_CHAIN (list);
3575
3576 if (list)
3577 return TREE_VALUE (list);
3578 else
3579 {
3580 (*canonical_template_parms)[idx]
3581 = tree_cons (NULL_TREE, type,
3582 (*canonical_template_parms)[idx]);
3583 return type;
3584 }
3585 }
3586
3587 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3588 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3589 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3590 new one is created. */
3591
3592 static tree
3593 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3594 tsubst_flags_t complain)
3595 {
3596 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3597 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3598 != TEMPLATE_PARM_LEVEL (index) - levels)
3599 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3600 {
3601 tree orig_decl = TEMPLATE_PARM_DECL (index);
3602 tree decl, t;
3603
3604 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3605 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3606 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3607 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3608 DECL_ARTIFICIAL (decl) = 1;
3609 SET_DECL_TEMPLATE_PARM_P (decl);
3610
3611 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3612 TEMPLATE_PARM_LEVEL (index) - levels,
3613 TEMPLATE_PARM_ORIG_LEVEL (index),
3614 decl, type);
3615 TEMPLATE_PARM_DESCENDANTS (index) = t;
3616 TEMPLATE_PARM_PARAMETER_PACK (t)
3617 = TEMPLATE_PARM_PARAMETER_PACK (index);
3618
3619 /* Template template parameters need this. */
3620 if (TREE_CODE (decl) == TEMPLATE_DECL)
3621 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3622 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3623 args, complain);
3624 }
3625
3626 return TEMPLATE_PARM_DESCENDANTS (index);
3627 }
3628
3629 /* Process information from new template parameter PARM and append it
3630 to the LIST being built. This new parameter is a non-type
3631 parameter iff IS_NON_TYPE is true. This new parameter is a
3632 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3633 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3634 parameter list PARM belongs to. This is used used to create a
3635 proper canonical type for the type of PARM that is to be created,
3636 iff PARM is a type. If the size is not known, this parameter shall
3637 be set to 0. */
3638
3639 tree
3640 process_template_parm (tree list, location_t parm_loc, tree parm,
3641 bool is_non_type, bool is_parameter_pack)
3642 {
3643 tree decl = 0;
3644 tree defval;
3645 tree err_parm_list;
3646 int idx = 0;
3647
3648 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3649 defval = TREE_PURPOSE (parm);
3650
3651 if (list)
3652 {
3653 tree p = tree_last (list);
3654
3655 if (p && TREE_VALUE (p) != error_mark_node)
3656 {
3657 p = TREE_VALUE (p);
3658 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3659 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3660 else
3661 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3662 }
3663
3664 ++idx;
3665 }
3666 else
3667 idx = 0;
3668
3669 if (is_non_type)
3670 {
3671 parm = TREE_VALUE (parm);
3672
3673 SET_DECL_TEMPLATE_PARM_P (parm);
3674
3675 if (TREE_TYPE (parm) == error_mark_node)
3676 {
3677 err_parm_list = build_tree_list (defval, parm);
3678 TREE_VALUE (err_parm_list) = error_mark_node;
3679 return chainon (list, err_parm_list);
3680 }
3681 else
3682 {
3683 /* [temp.param]
3684
3685 The top-level cv-qualifiers on the template-parameter are
3686 ignored when determining its type. */
3687 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3688 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3689 {
3690 err_parm_list = build_tree_list (defval, parm);
3691 TREE_VALUE (err_parm_list) = error_mark_node;
3692 return chainon (list, err_parm_list);
3693 }
3694
3695 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3696 {
3697 /* This template parameter is not a parameter pack, but it
3698 should be. Complain about "bare" parameter packs. */
3699 check_for_bare_parameter_packs (TREE_TYPE (parm));
3700
3701 /* Recover by calling this a parameter pack. */
3702 is_parameter_pack = true;
3703 }
3704 }
3705
3706 /* A template parameter is not modifiable. */
3707 TREE_CONSTANT (parm) = 1;
3708 TREE_READONLY (parm) = 1;
3709 decl = build_decl (parm_loc,
3710 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3711 TREE_CONSTANT (decl) = 1;
3712 TREE_READONLY (decl) = 1;
3713 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3714 = build_template_parm_index (idx, processing_template_decl,
3715 processing_template_decl,
3716 decl, TREE_TYPE (parm));
3717
3718 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3719 = is_parameter_pack;
3720 }
3721 else
3722 {
3723 tree t;
3724 parm = TREE_VALUE (TREE_VALUE (parm));
3725
3726 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3727 {
3728 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3729 /* This is for distinguishing between real templates and template
3730 template parameters */
3731 TREE_TYPE (parm) = t;
3732 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3733 decl = parm;
3734 }
3735 else
3736 {
3737 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3738 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3739 decl = build_decl (parm_loc,
3740 TYPE_DECL, parm, t);
3741 }
3742
3743 TYPE_NAME (t) = decl;
3744 TYPE_STUB_DECL (t) = decl;
3745 parm = decl;
3746 TEMPLATE_TYPE_PARM_INDEX (t)
3747 = build_template_parm_index (idx, processing_template_decl,
3748 processing_template_decl,
3749 decl, TREE_TYPE (parm));
3750 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3751 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3752 }
3753 DECL_ARTIFICIAL (decl) = 1;
3754 SET_DECL_TEMPLATE_PARM_P (decl);
3755 pushdecl (decl);
3756 parm = build_tree_list (defval, parm);
3757 return chainon (list, parm);
3758 }
3759
3760 /* The end of a template parameter list has been reached. Process the
3761 tree list into a parameter vector, converting each parameter into a more
3762 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3763 as PARM_DECLs. */
3764
3765 tree
3766 end_template_parm_list (tree parms)
3767 {
3768 int nparms;
3769 tree parm, next;
3770 tree saved_parmlist = make_tree_vec (list_length (parms));
3771
3772 current_template_parms
3773 = tree_cons (size_int (processing_template_decl),
3774 saved_parmlist, current_template_parms);
3775
3776 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3777 {
3778 next = TREE_CHAIN (parm);
3779 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3780 TREE_CHAIN (parm) = NULL_TREE;
3781 }
3782
3783 --processing_template_parmlist;
3784
3785 return saved_parmlist;
3786 }
3787
3788 /* end_template_decl is called after a template declaration is seen. */
3789
3790 void
3791 end_template_decl (void)
3792 {
3793 reset_specialization ();
3794
3795 if (! processing_template_decl)
3796 return;
3797
3798 /* This matches the pushlevel in begin_template_parm_list. */
3799 finish_scope ();
3800
3801 --processing_template_decl;
3802 current_template_parms = TREE_CHAIN (current_template_parms);
3803 }
3804
3805 /* Takes a TREE_LIST representing a template parameter and convert it
3806 into an argument suitable to be passed to the type substitution
3807 functions. Note that If the TREE_LIST contains an error_mark
3808 node, the returned argument is error_mark_node. */
3809
3810 static tree
3811 template_parm_to_arg (tree t)
3812 {
3813
3814 if (t == NULL_TREE
3815 || TREE_CODE (t) != TREE_LIST)
3816 return t;
3817
3818 if (error_operand_p (TREE_VALUE (t)))
3819 return error_mark_node;
3820
3821 t = TREE_VALUE (t);
3822
3823 if (TREE_CODE (t) == TYPE_DECL
3824 || TREE_CODE (t) == TEMPLATE_DECL)
3825 {
3826 t = TREE_TYPE (t);
3827
3828 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3829 {
3830 /* Turn this argument into a TYPE_ARGUMENT_PACK
3831 with a single element, which expands T. */
3832 tree vec = make_tree_vec (1);
3833 #ifdef ENABLE_CHECKING
3834 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3835 (vec, TREE_VEC_LENGTH (vec));
3836 #endif
3837 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3838
3839 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3840 SET_ARGUMENT_PACK_ARGS (t, vec);
3841 }
3842 }
3843 else
3844 {
3845 t = DECL_INITIAL (t);
3846
3847 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3848 {
3849 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3850 with a single element, which expands T. */
3851 tree vec = make_tree_vec (1);
3852 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3853 #ifdef ENABLE_CHECKING
3854 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3855 (vec, TREE_VEC_LENGTH (vec));
3856 #endif
3857 t = convert_from_reference (t);
3858 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3859
3860 t = make_node (NONTYPE_ARGUMENT_PACK);
3861 SET_ARGUMENT_PACK_ARGS (t, vec);
3862 TREE_TYPE (t) = type;
3863 }
3864 }
3865 return t;
3866 }
3867
3868 /* Given a set of template parameters, return them as a set of template
3869 arguments. The template parameters are represented as a TREE_VEC, in
3870 the form documented in cp-tree.h for template arguments. */
3871
3872 static tree
3873 template_parms_to_args (tree parms)
3874 {
3875 tree header;
3876 tree args = NULL_TREE;
3877 int length = TMPL_PARMS_DEPTH (parms);
3878 int l = length;
3879
3880 /* If there is only one level of template parameters, we do not
3881 create a TREE_VEC of TREE_VECs. Instead, we return a single
3882 TREE_VEC containing the arguments. */
3883 if (length > 1)
3884 args = make_tree_vec (length);
3885
3886 for (header = parms; header; header = TREE_CHAIN (header))
3887 {
3888 tree a = copy_node (TREE_VALUE (header));
3889 int i;
3890
3891 TREE_TYPE (a) = NULL_TREE;
3892 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3893 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3894
3895 #ifdef ENABLE_CHECKING
3896 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3897 #endif
3898
3899 if (length > 1)
3900 TREE_VEC_ELT (args, --l) = a;
3901 else
3902 args = a;
3903 }
3904
3905 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3906 /* This can happen for template parms of a template template
3907 parameter, e.g:
3908
3909 template<template<class T, class U> class TT> struct S;
3910
3911 Consider the level of the parms of TT; T and U both have
3912 level 2; TT has no template parm of level 1. So in this case
3913 the first element of full_template_args is NULL_TREE. If we
3914 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3915 of 2. This will make tsubst wrongly consider that T and U
3916 have level 1. Instead, let's create a dummy vector as the
3917 first element of full_template_args so that TMPL_ARGS_DEPTH
3918 returns the correct depth for args. */
3919 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3920 return args;
3921 }
3922
3923 /* Within the declaration of a template, return the currently active
3924 template parameters as an argument TREE_VEC. */
3925
3926 static tree
3927 current_template_args (void)
3928 {
3929 return template_parms_to_args (current_template_parms);
3930 }
3931
3932 /* Update the declared TYPE by doing any lookups which were thought to be
3933 dependent, but are not now that we know the SCOPE of the declarator. */
3934
3935 tree
3936 maybe_update_decl_type (tree orig_type, tree scope)
3937 {
3938 tree type = orig_type;
3939
3940 if (type == NULL_TREE)
3941 return type;
3942
3943 if (TREE_CODE (orig_type) == TYPE_DECL)
3944 type = TREE_TYPE (type);
3945
3946 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3947 && dependent_type_p (type)
3948 /* Don't bother building up the args in this case. */
3949 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3950 {
3951 /* tsubst in the args corresponding to the template parameters,
3952 including auto if present. Most things will be unchanged, but
3953 make_typename_type and tsubst_qualified_id will resolve
3954 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3955 tree args = current_template_args ();
3956 tree auto_node = type_uses_auto (type);
3957 tree pushed;
3958 if (auto_node)
3959 {
3960 tree auto_vec = make_tree_vec (1);
3961 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3962 args = add_to_template_args (args, auto_vec);
3963 }
3964 pushed = push_scope (scope);
3965 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3966 if (pushed)
3967 pop_scope (scope);
3968 }
3969
3970 if (type == error_mark_node)
3971 return orig_type;
3972
3973 if (TREE_CODE (orig_type) == TYPE_DECL)
3974 {
3975 if (same_type_p (type, TREE_TYPE (orig_type)))
3976 type = orig_type;
3977 else
3978 type = TYPE_NAME (type);
3979 }
3980 return type;
3981 }
3982
3983 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3984 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3985 a member template. Used by push_template_decl below. */
3986
3987 static tree
3988 build_template_decl (tree decl, tree parms, bool member_template_p)
3989 {
3990 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3991 DECL_TEMPLATE_PARMS (tmpl) = parms;
3992 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3993 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3994 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3995
3996 return tmpl;
3997 }
3998
3999 struct template_parm_data
4000 {
4001 /* The level of the template parameters we are currently
4002 processing. */
4003 int level;
4004
4005 /* The index of the specialization argument we are currently
4006 processing. */
4007 int current_arg;
4008
4009 /* An array whose size is the number of template parameters. The
4010 elements are nonzero if the parameter has been used in any one
4011 of the arguments processed so far. */
4012 int* parms;
4013
4014 /* An array whose size is the number of template arguments. The
4015 elements are nonzero if the argument makes use of template
4016 parameters of this level. */
4017 int* arg_uses_template_parms;
4018 };
4019
4020 /* Subroutine of push_template_decl used to see if each template
4021 parameter in a partial specialization is used in the explicit
4022 argument list. If T is of the LEVEL given in DATA (which is
4023 treated as a template_parm_data*), then DATA->PARMS is marked
4024 appropriately. */
4025
4026 static int
4027 mark_template_parm (tree t, void* data)
4028 {
4029 int level;
4030 int idx;
4031 struct template_parm_data* tpd = (struct template_parm_data*) data;
4032
4033 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4034 {
4035 level = TEMPLATE_PARM_LEVEL (t);
4036 idx = TEMPLATE_PARM_IDX (t);
4037 }
4038 else
4039 {
4040 level = TEMPLATE_TYPE_LEVEL (t);
4041 idx = TEMPLATE_TYPE_IDX (t);
4042 }
4043
4044 if (level == tpd->level)
4045 {
4046 tpd->parms[idx] = 1;
4047 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4048 }
4049
4050 /* Return zero so that for_each_template_parm will continue the
4051 traversal of the tree; we want to mark *every* template parm. */
4052 return 0;
4053 }
4054
4055 /* Process the partial specialization DECL. */
4056
4057 static tree
4058 process_partial_specialization (tree decl)
4059 {
4060 tree type = TREE_TYPE (decl);
4061 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4062 tree specargs = CLASSTYPE_TI_ARGS (type);
4063 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4064 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4065 tree inner_parms;
4066 tree inst;
4067 int nargs = TREE_VEC_LENGTH (inner_args);
4068 int ntparms;
4069 int i;
4070 bool did_error_intro = false;
4071 struct template_parm_data tpd;
4072 struct template_parm_data tpd2;
4073
4074 gcc_assert (current_template_parms);
4075
4076 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4077 ntparms = TREE_VEC_LENGTH (inner_parms);
4078
4079 /* We check that each of the template parameters given in the
4080 partial specialization is used in the argument list to the
4081 specialization. For example:
4082
4083 template <class T> struct S;
4084 template <class T> struct S<T*>;
4085
4086 The second declaration is OK because `T*' uses the template
4087 parameter T, whereas
4088
4089 template <class T> struct S<int>;
4090
4091 is no good. Even trickier is:
4092
4093 template <class T>
4094 struct S1
4095 {
4096 template <class U>
4097 struct S2;
4098 template <class U>
4099 struct S2<T>;
4100 };
4101
4102 The S2<T> declaration is actually invalid; it is a
4103 full-specialization. Of course,
4104
4105 template <class U>
4106 struct S2<T (*)(U)>;
4107
4108 or some such would have been OK. */
4109 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4110 tpd.parms = XALLOCAVEC (int, ntparms);
4111 memset (tpd.parms, 0, sizeof (int) * ntparms);
4112
4113 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4114 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4115 for (i = 0; i < nargs; ++i)
4116 {
4117 tpd.current_arg = i;
4118 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4119 &mark_template_parm,
4120 &tpd,
4121 NULL,
4122 /*include_nondeduced_p=*/false);
4123 }
4124 for (i = 0; i < ntparms; ++i)
4125 if (tpd.parms[i] == 0)
4126 {
4127 /* One of the template parms was not used in the
4128 specialization. */
4129 if (!did_error_intro)
4130 {
4131 error ("template parameters not used in partial specialization:");
4132 did_error_intro = true;
4133 }
4134
4135 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4136 }
4137
4138 if (did_error_intro)
4139 return error_mark_node;
4140
4141 /* [temp.class.spec]
4142
4143 The argument list of the specialization shall not be identical to
4144 the implicit argument list of the primary template. */
4145 if (comp_template_args
4146 (inner_args,
4147 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4148 (maintmpl)))))
4149 error ("partial specialization %qT does not specialize any template arguments", type);
4150
4151 /* A partial specialization that replaces multiple parameters of the
4152 primary template with a pack expansion is less specialized for those
4153 parameters. */
4154 if (nargs < DECL_NTPARMS (maintmpl))
4155 {
4156 error ("partial specialization is not more specialized than the "
4157 "primary template because it replaces multiple parameters "
4158 "with a pack expansion");
4159 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4160 return decl;
4161 }
4162
4163 /* [temp.class.spec]
4164
4165 A partially specialized non-type argument expression shall not
4166 involve template parameters of the partial specialization except
4167 when the argument expression is a simple identifier.
4168
4169 The type of a template parameter corresponding to a specialized
4170 non-type argument shall not be dependent on a parameter of the
4171 specialization.
4172
4173 Also, we verify that pack expansions only occur at the
4174 end of the argument list. */
4175 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4176 tpd2.parms = 0;
4177 for (i = 0; i < nargs; ++i)
4178 {
4179 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4180 tree arg = TREE_VEC_ELT (inner_args, i);
4181 tree packed_args = NULL_TREE;
4182 int j, len = 1;
4183
4184 if (ARGUMENT_PACK_P (arg))
4185 {
4186 /* Extract the arguments from the argument pack. We'll be
4187 iterating over these in the following loop. */
4188 packed_args = ARGUMENT_PACK_ARGS (arg);
4189 len = TREE_VEC_LENGTH (packed_args);
4190 }
4191
4192 for (j = 0; j < len; j++)
4193 {
4194 if (packed_args)
4195 /* Get the Jth argument in the parameter pack. */
4196 arg = TREE_VEC_ELT (packed_args, j);
4197
4198 if (PACK_EXPANSION_P (arg))
4199 {
4200 /* Pack expansions must come at the end of the
4201 argument list. */
4202 if ((packed_args && j < len - 1)
4203 || (!packed_args && i < nargs - 1))
4204 {
4205 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4206 error ("parameter pack argument %qE must be at the "
4207 "end of the template argument list", arg);
4208 else
4209 error ("parameter pack argument %qT must be at the "
4210 "end of the template argument list", arg);
4211 }
4212 }
4213
4214 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4215 /* We only care about the pattern. */
4216 arg = PACK_EXPANSION_PATTERN (arg);
4217
4218 if (/* These first two lines are the `non-type' bit. */
4219 !TYPE_P (arg)
4220 && TREE_CODE (arg) != TEMPLATE_DECL
4221 /* This next line is the `argument expression is not just a
4222 simple identifier' condition and also the `specialized
4223 non-type argument' bit. */
4224 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4225 {
4226 if ((!packed_args && tpd.arg_uses_template_parms[i])
4227 || (packed_args && uses_template_parms (arg)))
4228 error ("template argument %qE involves template parameter(s)",
4229 arg);
4230 else
4231 {
4232 /* Look at the corresponding template parameter,
4233 marking which template parameters its type depends
4234 upon. */
4235 tree type = TREE_TYPE (parm);
4236
4237 if (!tpd2.parms)
4238 {
4239 /* We haven't yet initialized TPD2. Do so now. */
4240 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4241 /* The number of parameters here is the number in the
4242 main template, which, as checked in the assertion
4243 above, is NARGS. */
4244 tpd2.parms = XALLOCAVEC (int, nargs);
4245 tpd2.level =
4246 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4247 }
4248
4249 /* Mark the template parameters. But this time, we're
4250 looking for the template parameters of the main
4251 template, not in the specialization. */
4252 tpd2.current_arg = i;
4253 tpd2.arg_uses_template_parms[i] = 0;
4254 memset (tpd2.parms, 0, sizeof (int) * nargs);
4255 for_each_template_parm (type,
4256 &mark_template_parm,
4257 &tpd2,
4258 NULL,
4259 /*include_nondeduced_p=*/false);
4260
4261 if (tpd2.arg_uses_template_parms [i])
4262 {
4263 /* The type depended on some template parameters.
4264 If they are fully specialized in the
4265 specialization, that's OK. */
4266 int j;
4267 int count = 0;
4268 for (j = 0; j < nargs; ++j)
4269 if (tpd2.parms[j] != 0
4270 && tpd.arg_uses_template_parms [j])
4271 ++count;
4272 if (count != 0)
4273 error_n (input_location, count,
4274 "type %qT of template argument %qE depends "
4275 "on a template parameter",
4276 "type %qT of template argument %qE depends "
4277 "on template parameters",
4278 type,
4279 arg);
4280 }
4281 }
4282 }
4283 }
4284 }
4285
4286 /* We should only get here once. */
4287 gcc_assert (!COMPLETE_TYPE_P (type));
4288
4289 tree tmpl = build_template_decl (decl, current_template_parms,
4290 DECL_MEMBER_TEMPLATE_P (maintmpl));
4291 TREE_TYPE (tmpl) = type;
4292 DECL_TEMPLATE_RESULT (tmpl) = decl;
4293 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4294 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4295 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4296
4297 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4298 = tree_cons (specargs, tmpl,
4299 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4300 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4301
4302 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4303 inst = TREE_CHAIN (inst))
4304 {
4305 tree inst_type = TREE_VALUE (inst);
4306 if (COMPLETE_TYPE_P (inst_type)
4307 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4308 {
4309 tree spec = most_specialized_class (inst_type, tf_none);
4310 if (spec && TREE_TYPE (spec) == type)
4311 permerror (input_location,
4312 "partial specialization of %qT after instantiation "
4313 "of %qT", type, inst_type);
4314 }
4315 }
4316
4317 return decl;
4318 }
4319
4320 /* Check that a template declaration's use of default arguments and
4321 parameter packs is not invalid. Here, PARMS are the template
4322 parameters. IS_PRIMARY is true if DECL is the thing declared by
4323 a primary template. IS_PARTIAL is true if DECL is a partial
4324 specialization.
4325
4326 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4327 declaration (but not a definition); 1 indicates a declaration, 2
4328 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4329 emitted for extraneous default arguments.
4330
4331 Returns TRUE if there were no errors found, FALSE otherwise. */
4332
4333 bool
4334 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4335 bool is_partial, int is_friend_decl)
4336 {
4337 const char *msg;
4338 int last_level_to_check;
4339 tree parm_level;
4340 bool no_errors = true;
4341
4342 /* [temp.param]
4343
4344 A default template-argument shall not be specified in a
4345 function template declaration or a function template definition, nor
4346 in the template-parameter-list of the definition of a member of a
4347 class template. */
4348
4349 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4350 /* You can't have a function template declaration in a local
4351 scope, nor you can you define a member of a class template in a
4352 local scope. */
4353 return true;
4354
4355 if (TREE_CODE (decl) == TYPE_DECL
4356 && TREE_TYPE (decl)
4357 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4358 /* A lambda doesn't have an explicit declaration; don't complain
4359 about the parms of the enclosing class. */
4360 return true;
4361
4362 if (current_class_type
4363 && !TYPE_BEING_DEFINED (current_class_type)
4364 && DECL_LANG_SPECIFIC (decl)
4365 && DECL_DECLARES_FUNCTION_P (decl)
4366 /* If this is either a friend defined in the scope of the class
4367 or a member function. */
4368 && (DECL_FUNCTION_MEMBER_P (decl)
4369 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4370 : DECL_FRIEND_CONTEXT (decl)
4371 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4372 : false)
4373 /* And, if it was a member function, it really was defined in
4374 the scope of the class. */
4375 && (!DECL_FUNCTION_MEMBER_P (decl)
4376 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4377 /* We already checked these parameters when the template was
4378 declared, so there's no need to do it again now. This function
4379 was defined in class scope, but we're processing its body now
4380 that the class is complete. */
4381 return true;
4382
4383 /* Core issue 226 (C++0x only): the following only applies to class
4384 templates. */
4385 if (is_primary
4386 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4387 {
4388 /* [temp.param]
4389
4390 If a template-parameter has a default template-argument, all
4391 subsequent template-parameters shall have a default
4392 template-argument supplied. */
4393 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4394 {
4395 tree inner_parms = TREE_VALUE (parm_level);
4396 int ntparms = TREE_VEC_LENGTH (inner_parms);
4397 int seen_def_arg_p = 0;
4398 int i;
4399
4400 for (i = 0; i < ntparms; ++i)
4401 {
4402 tree parm = TREE_VEC_ELT (inner_parms, i);
4403
4404 if (parm == error_mark_node)
4405 continue;
4406
4407 if (TREE_PURPOSE (parm))
4408 seen_def_arg_p = 1;
4409 else if (seen_def_arg_p
4410 && !template_parameter_pack_p (TREE_VALUE (parm)))
4411 {
4412 error ("no default argument for %qD", TREE_VALUE (parm));
4413 /* For better subsequent error-recovery, we indicate that
4414 there should have been a default argument. */
4415 TREE_PURPOSE (parm) = error_mark_node;
4416 no_errors = false;
4417 }
4418 else if (!is_partial
4419 && !is_friend_decl
4420 /* Don't complain about an enclosing partial
4421 specialization. */
4422 && parm_level == parms
4423 && TREE_CODE (decl) == TYPE_DECL
4424 && i < ntparms - 1
4425 && template_parameter_pack_p (TREE_VALUE (parm)))
4426 {
4427 /* A primary class template can only have one
4428 parameter pack, at the end of the template
4429 parameter list. */
4430
4431 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4432 error ("parameter pack %qE must be at the end of the"
4433 " template parameter list", TREE_VALUE (parm));
4434 else
4435 error ("parameter pack %qT must be at the end of the"
4436 " template parameter list",
4437 TREE_TYPE (TREE_VALUE (parm)));
4438
4439 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4440 = error_mark_node;
4441 no_errors = false;
4442 }
4443 }
4444 }
4445 }
4446
4447 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4448 || is_partial
4449 || !is_primary
4450 || is_friend_decl)
4451 /* For an ordinary class template, default template arguments are
4452 allowed at the innermost level, e.g.:
4453 template <class T = int>
4454 struct S {};
4455 but, in a partial specialization, they're not allowed even
4456 there, as we have in [temp.class.spec]:
4457
4458 The template parameter list of a specialization shall not
4459 contain default template argument values.
4460
4461 So, for a partial specialization, or for a function template
4462 (in C++98/C++03), we look at all of them. */
4463 ;
4464 else
4465 /* But, for a primary class template that is not a partial
4466 specialization we look at all template parameters except the
4467 innermost ones. */
4468 parms = TREE_CHAIN (parms);
4469
4470 /* Figure out what error message to issue. */
4471 if (is_friend_decl == 2)
4472 msg = G_("default template arguments may not be used in function template "
4473 "friend re-declaration");
4474 else if (is_friend_decl)
4475 msg = G_("default template arguments may not be used in function template "
4476 "friend declarations");
4477 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4478 msg = G_("default template arguments may not be used in function templates "
4479 "without -std=c++11 or -std=gnu++11");
4480 else if (is_partial)
4481 msg = G_("default template arguments may not be used in "
4482 "partial specializations");
4483 else
4484 msg = G_("default argument for template parameter for class enclosing %qD");
4485
4486 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4487 /* If we're inside a class definition, there's no need to
4488 examine the parameters to the class itself. On the one
4489 hand, they will be checked when the class is defined, and,
4490 on the other, default arguments are valid in things like:
4491 template <class T = double>
4492 struct S { template <class U> void f(U); };
4493 Here the default argument for `S' has no bearing on the
4494 declaration of `f'. */
4495 last_level_to_check = template_class_depth (current_class_type) + 1;
4496 else
4497 /* Check everything. */
4498 last_level_to_check = 0;
4499
4500 for (parm_level = parms;
4501 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4502 parm_level = TREE_CHAIN (parm_level))
4503 {
4504 tree inner_parms = TREE_VALUE (parm_level);
4505 int i;
4506 int ntparms;
4507
4508 ntparms = TREE_VEC_LENGTH (inner_parms);
4509 for (i = 0; i < ntparms; ++i)
4510 {
4511 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4512 continue;
4513
4514 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4515 {
4516 if (msg)
4517 {
4518 no_errors = false;
4519 if (is_friend_decl == 2)
4520 return no_errors;
4521
4522 error (msg, decl);
4523 msg = 0;
4524 }
4525
4526 /* Clear out the default argument so that we are not
4527 confused later. */
4528 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4529 }
4530 }
4531
4532 /* At this point, if we're still interested in issuing messages,
4533 they must apply to classes surrounding the object declared. */
4534 if (msg)
4535 msg = G_("default argument for template parameter for class "
4536 "enclosing %qD");
4537 }
4538
4539 return no_errors;
4540 }
4541
4542 /* Worker for push_template_decl_real, called via
4543 for_each_template_parm. DATA is really an int, indicating the
4544 level of the parameters we are interested in. If T is a template
4545 parameter of that level, return nonzero. */
4546
4547 static int
4548 template_parm_this_level_p (tree t, void* data)
4549 {
4550 int this_level = *(int *)data;
4551 int level;
4552
4553 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4554 level = TEMPLATE_PARM_LEVEL (t);
4555 else
4556 level = TEMPLATE_TYPE_LEVEL (t);
4557 return level == this_level;
4558 }
4559
4560 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4561 parameters given by current_template_args, or reuses a
4562 previously existing one, if appropriate. Returns the DECL, or an
4563 equivalent one, if it is replaced via a call to duplicate_decls.
4564
4565 If IS_FRIEND is true, DECL is a friend declaration. */
4566
4567 tree
4568 push_template_decl_real (tree decl, bool is_friend)
4569 {
4570 tree tmpl;
4571 tree args;
4572 tree info;
4573 tree ctx;
4574 bool is_primary;
4575 bool is_partial;
4576 int new_template_p = 0;
4577 /* True if the template is a member template, in the sense of
4578 [temp.mem]. */
4579 bool member_template_p = false;
4580
4581 if (decl == error_mark_node || !current_template_parms)
4582 return error_mark_node;
4583
4584 /* See if this is a partial specialization. */
4585 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4586 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4587 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4588
4589 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4590 is_friend = true;
4591
4592 if (is_friend)
4593 /* For a friend, we want the context of the friend function, not
4594 the type of which it is a friend. */
4595 ctx = CP_DECL_CONTEXT (decl);
4596 else if (CP_DECL_CONTEXT (decl)
4597 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4598 /* In the case of a virtual function, we want the class in which
4599 it is defined. */
4600 ctx = CP_DECL_CONTEXT (decl);
4601 else
4602 /* Otherwise, if we're currently defining some class, the DECL
4603 is assumed to be a member of the class. */
4604 ctx = current_scope ();
4605
4606 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4607 ctx = NULL_TREE;
4608
4609 if (!DECL_CONTEXT (decl))
4610 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4611
4612 /* See if this is a primary template. */
4613 if (is_friend && ctx)
4614 /* A friend template that specifies a class context, i.e.
4615 template <typename T> friend void A<T>::f();
4616 is not primary. */
4617 is_primary = false;
4618 else
4619 is_primary = template_parm_scope_p ();
4620
4621 if (is_primary)
4622 {
4623 if (DECL_CLASS_SCOPE_P (decl))
4624 member_template_p = true;
4625 if (TREE_CODE (decl) == TYPE_DECL
4626 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4627 {
4628 error ("template class without a name");
4629 return error_mark_node;
4630 }
4631 else if (TREE_CODE (decl) == FUNCTION_DECL)
4632 {
4633 if (DECL_DESTRUCTOR_P (decl))
4634 {
4635 /* [temp.mem]
4636
4637 A destructor shall not be a member template. */
4638 error ("destructor %qD declared as member template", decl);
4639 return error_mark_node;
4640 }
4641 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4642 && (!prototype_p (TREE_TYPE (decl))
4643 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4644 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4645 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4646 == void_list_node)))
4647 {
4648 /* [basic.stc.dynamic.allocation]
4649
4650 An allocation function can be a function
4651 template. ... Template allocation functions shall
4652 have two or more parameters. */
4653 error ("invalid template declaration of %qD", decl);
4654 return error_mark_node;
4655 }
4656 }
4657 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4658 && CLASS_TYPE_P (TREE_TYPE (decl)))
4659 /* OK */;
4660 else if (TREE_CODE (decl) == TYPE_DECL
4661 && TYPE_DECL_ALIAS_P (decl))
4662 /* alias-declaration */
4663 gcc_assert (!DECL_ARTIFICIAL (decl));
4664 else
4665 {
4666 error ("template declaration of %q#D", decl);
4667 return error_mark_node;
4668 }
4669 }
4670
4671 /* Check to see that the rules regarding the use of default
4672 arguments are not being violated. */
4673 check_default_tmpl_args (decl, current_template_parms,
4674 is_primary, is_partial, /*is_friend_decl=*/0);
4675
4676 /* Ensure that there are no parameter packs in the type of this
4677 declaration that have not been expanded. */
4678 if (TREE_CODE (decl) == FUNCTION_DECL)
4679 {
4680 /* Check each of the arguments individually to see if there are
4681 any bare parameter packs. */
4682 tree type = TREE_TYPE (decl);
4683 tree arg = DECL_ARGUMENTS (decl);
4684 tree argtype = TYPE_ARG_TYPES (type);
4685
4686 while (arg && argtype)
4687 {
4688 if (!DECL_PACK_P (arg)
4689 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4690 {
4691 /* This is a PARM_DECL that contains unexpanded parameter
4692 packs. We have already complained about this in the
4693 check_for_bare_parameter_packs call, so just replace
4694 these types with ERROR_MARK_NODE. */
4695 TREE_TYPE (arg) = error_mark_node;
4696 TREE_VALUE (argtype) = error_mark_node;
4697 }
4698
4699 arg = DECL_CHAIN (arg);
4700 argtype = TREE_CHAIN (argtype);
4701 }
4702
4703 /* Check for bare parameter packs in the return type and the
4704 exception specifiers. */
4705 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4706 /* Errors were already issued, set return type to int
4707 as the frontend doesn't expect error_mark_node as
4708 the return type. */
4709 TREE_TYPE (type) = integer_type_node;
4710 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4711 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4712 }
4713 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4714 && TYPE_DECL_ALIAS_P (decl))
4715 ? DECL_ORIGINAL_TYPE (decl)
4716 : TREE_TYPE (decl)))
4717 {
4718 TREE_TYPE (decl) = error_mark_node;
4719 return error_mark_node;
4720 }
4721
4722 if (is_partial)
4723 return process_partial_specialization (decl);
4724
4725 args = current_template_args ();
4726
4727 if (!ctx
4728 || TREE_CODE (ctx) == FUNCTION_DECL
4729 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4730 || (TREE_CODE (decl) == TYPE_DECL
4731 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4732 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4733 {
4734 if (DECL_LANG_SPECIFIC (decl)
4735 && DECL_TEMPLATE_INFO (decl)
4736 && DECL_TI_TEMPLATE (decl))
4737 tmpl = DECL_TI_TEMPLATE (decl);
4738 /* If DECL is a TYPE_DECL for a class-template, then there won't
4739 be DECL_LANG_SPECIFIC. The information equivalent to
4740 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4741 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4742 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4743 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4744 {
4745 /* Since a template declaration already existed for this
4746 class-type, we must be redeclaring it here. Make sure
4747 that the redeclaration is valid. */
4748 redeclare_class_template (TREE_TYPE (decl),
4749 current_template_parms);
4750 /* We don't need to create a new TEMPLATE_DECL; just use the
4751 one we already had. */
4752 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4753 }
4754 else
4755 {
4756 tmpl = build_template_decl (decl, current_template_parms,
4757 member_template_p);
4758 new_template_p = 1;
4759
4760 if (DECL_LANG_SPECIFIC (decl)
4761 && DECL_TEMPLATE_SPECIALIZATION (decl))
4762 {
4763 /* A specialization of a member template of a template
4764 class. */
4765 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4766 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4767 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4768 }
4769 }
4770 }
4771 else
4772 {
4773 tree a, t, current, parms;
4774 int i;
4775 tree tinfo = get_template_info (decl);
4776
4777 if (!tinfo)
4778 {
4779 error ("template definition of non-template %q#D", decl);
4780 return error_mark_node;
4781 }
4782
4783 tmpl = TI_TEMPLATE (tinfo);
4784
4785 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4786 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4787 && DECL_TEMPLATE_SPECIALIZATION (decl)
4788 && DECL_MEMBER_TEMPLATE_P (tmpl))
4789 {
4790 tree new_tmpl;
4791
4792 /* The declaration is a specialization of a member
4793 template, declared outside the class. Therefore, the
4794 innermost template arguments will be NULL, so we
4795 replace them with the arguments determined by the
4796 earlier call to check_explicit_specialization. */
4797 args = DECL_TI_ARGS (decl);
4798
4799 new_tmpl
4800 = build_template_decl (decl, current_template_parms,
4801 member_template_p);
4802 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4803 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4804 DECL_TI_TEMPLATE (decl) = new_tmpl;
4805 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4806 DECL_TEMPLATE_INFO (new_tmpl)
4807 = build_template_info (tmpl, args);
4808
4809 register_specialization (new_tmpl,
4810 most_general_template (tmpl),
4811 args,
4812 is_friend, 0);
4813 return decl;
4814 }
4815
4816 /* Make sure the template headers we got make sense. */
4817
4818 parms = DECL_TEMPLATE_PARMS (tmpl);
4819 i = TMPL_PARMS_DEPTH (parms);
4820 if (TMPL_ARGS_DEPTH (args) != i)
4821 {
4822 error ("expected %d levels of template parms for %q#D, got %d",
4823 i, decl, TMPL_ARGS_DEPTH (args));
4824 }
4825 else
4826 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4827 {
4828 a = TMPL_ARGS_LEVEL (args, i);
4829 t = INNERMOST_TEMPLATE_PARMS (parms);
4830
4831 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4832 {
4833 if (current == decl)
4834 error ("got %d template parameters for %q#D",
4835 TREE_VEC_LENGTH (a), decl);
4836 else
4837 error ("got %d template parameters for %q#T",
4838 TREE_VEC_LENGTH (a), current);
4839 error (" but %d required", TREE_VEC_LENGTH (t));
4840 /* Avoid crash in import_export_decl. */
4841 DECL_INTERFACE_KNOWN (decl) = 1;
4842 return error_mark_node;
4843 }
4844
4845 if (current == decl)
4846 current = ctx;
4847 else if (current == NULL_TREE)
4848 /* Can happen in erroneous input. */
4849 break;
4850 else
4851 current = get_containing_scope (current);
4852 }
4853
4854 /* Check that the parms are used in the appropriate qualifying scopes
4855 in the declarator. */
4856 if (!comp_template_args
4857 (TI_ARGS (tinfo),
4858 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4859 {
4860 error ("\
4861 template arguments to %qD do not match original template %qD",
4862 decl, DECL_TEMPLATE_RESULT (tmpl));
4863 if (!uses_template_parms (TI_ARGS (tinfo)))
4864 inform (input_location, "use template<> for an explicit specialization");
4865 /* Avoid crash in import_export_decl. */
4866 DECL_INTERFACE_KNOWN (decl) = 1;
4867 return error_mark_node;
4868 }
4869 }
4870
4871 DECL_TEMPLATE_RESULT (tmpl) = decl;
4872 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4873
4874 /* Push template declarations for global functions and types. Note
4875 that we do not try to push a global template friend declared in a
4876 template class; such a thing may well depend on the template
4877 parameters of the class. */
4878 if (new_template_p && !ctx
4879 && !(is_friend && template_class_depth (current_class_type) > 0))
4880 {
4881 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4882 if (tmpl == error_mark_node)
4883 return error_mark_node;
4884
4885 /* Hide template friend classes that haven't been declared yet. */
4886 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4887 {
4888 DECL_ANTICIPATED (tmpl) = 1;
4889 DECL_FRIEND_P (tmpl) = 1;
4890 }
4891 }
4892
4893 if (is_primary)
4894 {
4895 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4896 int i;
4897
4898 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4899 if (DECL_CONV_FN_P (tmpl))
4900 {
4901 int depth = TMPL_PARMS_DEPTH (parms);
4902
4903 /* It is a conversion operator. See if the type converted to
4904 depends on innermost template operands. */
4905
4906 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4907 depth))
4908 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4909 }
4910
4911 /* Give template template parms a DECL_CONTEXT of the template
4912 for which they are a parameter. */
4913 parms = INNERMOST_TEMPLATE_PARMS (parms);
4914 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4915 {
4916 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4917 if (TREE_CODE (parm) == TEMPLATE_DECL)
4918 DECL_CONTEXT (parm) = tmpl;
4919 }
4920 }
4921
4922 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4923 back to its most general template. If TMPL is a specialization,
4924 ARGS may only have the innermost set of arguments. Add the missing
4925 argument levels if necessary. */
4926 if (DECL_TEMPLATE_INFO (tmpl))
4927 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4928
4929 info = build_template_info (tmpl, args);
4930
4931 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4932 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4933 else
4934 {
4935 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4936 retrofit_lang_decl (decl);
4937 if (DECL_LANG_SPECIFIC (decl))
4938 DECL_TEMPLATE_INFO (decl) = info;
4939 }
4940
4941 return DECL_TEMPLATE_RESULT (tmpl);
4942 }
4943
4944 tree
4945 push_template_decl (tree decl)
4946 {
4947 return push_template_decl_real (decl, false);
4948 }
4949
4950 /* FN is an inheriting constructor that inherits from the constructor
4951 template INHERITED; turn FN into a constructor template with a matching
4952 template header. */
4953
4954 tree
4955 add_inherited_template_parms (tree fn, tree inherited)
4956 {
4957 tree inner_parms
4958 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4959 inner_parms = copy_node (inner_parms);
4960 tree parms
4961 = tree_cons (size_int (processing_template_decl + 1),
4962 inner_parms, current_template_parms);
4963 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4964 tree args = template_parms_to_args (parms);
4965 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4966 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4967 DECL_TEMPLATE_RESULT (tmpl) = fn;
4968 DECL_ARTIFICIAL (tmpl) = true;
4969 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4970 return tmpl;
4971 }
4972
4973 /* Called when a class template TYPE is redeclared with the indicated
4974 template PARMS, e.g.:
4975
4976 template <class T> struct S;
4977 template <class T> struct S {}; */
4978
4979 bool
4980 redeclare_class_template (tree type, tree parms)
4981 {
4982 tree tmpl;
4983 tree tmpl_parms;
4984 int i;
4985
4986 if (!TYPE_TEMPLATE_INFO (type))
4987 {
4988 error ("%qT is not a template type", type);
4989 return false;
4990 }
4991
4992 tmpl = TYPE_TI_TEMPLATE (type);
4993 if (!PRIMARY_TEMPLATE_P (tmpl))
4994 /* The type is nested in some template class. Nothing to worry
4995 about here; there are no new template parameters for the nested
4996 type. */
4997 return true;
4998
4999 if (!parms)
5000 {
5001 error ("template specifiers not specified in declaration of %qD",
5002 tmpl);
5003 return false;
5004 }
5005
5006 parms = INNERMOST_TEMPLATE_PARMS (parms);
5007 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5008
5009 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5010 {
5011 error_n (input_location, TREE_VEC_LENGTH (parms),
5012 "redeclared with %d template parameter",
5013 "redeclared with %d template parameters",
5014 TREE_VEC_LENGTH (parms));
5015 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5016 "previous declaration %q+D used %d template parameter",
5017 "previous declaration %q+D used %d template parameters",
5018 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5019 return false;
5020 }
5021
5022 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5023 {
5024 tree tmpl_parm;
5025 tree parm;
5026 tree tmpl_default;
5027 tree parm_default;
5028
5029 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5030 || TREE_VEC_ELT (parms, i) == error_mark_node)
5031 continue;
5032
5033 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5034 if (tmpl_parm == error_mark_node)
5035 return false;
5036
5037 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5038 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5039 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5040
5041 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5042 TEMPLATE_DECL. */
5043 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5044 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5045 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5046 || (TREE_CODE (tmpl_parm) != PARM_DECL
5047 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5048 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5049 || (TREE_CODE (tmpl_parm) == PARM_DECL
5050 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5051 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5052 {
5053 error ("template parameter %q+#D", tmpl_parm);
5054 error ("redeclared here as %q#D", parm);
5055 return false;
5056 }
5057
5058 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5059 {
5060 /* We have in [temp.param]:
5061
5062 A template-parameter may not be given default arguments
5063 by two different declarations in the same scope. */
5064 error_at (input_location, "redefinition of default argument for %q#D", parm);
5065 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5066 "original definition appeared here");
5067 return false;
5068 }
5069
5070 if (parm_default != NULL_TREE)
5071 /* Update the previous template parameters (which are the ones
5072 that will really count) with the new default value. */
5073 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5074 else if (tmpl_default != NULL_TREE)
5075 /* Update the new parameters, too; they'll be used as the
5076 parameters for any members. */
5077 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5078 }
5079
5080 return true;
5081 }
5082
5083 /* Simplify EXPR if it is a non-dependent expression. Returns the
5084 (possibly simplified) expression. */
5085
5086 tree
5087 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5088 {
5089 if (expr == NULL_TREE)
5090 return NULL_TREE;
5091
5092 /* If we're in a template, but EXPR isn't value dependent, simplify
5093 it. We're supposed to treat:
5094
5095 template <typename T> void f(T[1 + 1]);
5096 template <typename T> void f(T[2]);
5097
5098 as two declarations of the same function, for example. */
5099 if (processing_template_decl
5100 && !instantiation_dependent_expression_p (expr)
5101 && potential_constant_expression (expr))
5102 {
5103 HOST_WIDE_INT saved_processing_template_decl;
5104
5105 saved_processing_template_decl = processing_template_decl;
5106 processing_template_decl = 0;
5107 expr = tsubst_copy_and_build (expr,
5108 /*args=*/NULL_TREE,
5109 complain,
5110 /*in_decl=*/NULL_TREE,
5111 /*function_p=*/false,
5112 /*integral_constant_expression_p=*/true);
5113 processing_template_decl = saved_processing_template_decl;
5114 }
5115 return expr;
5116 }
5117
5118 tree
5119 fold_non_dependent_expr (tree expr)
5120 {
5121 return fold_non_dependent_expr_sfinae (expr, tf_error);
5122 }
5123
5124 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5125 template declaration, or a TYPE_DECL for an alias declaration. */
5126
5127 bool
5128 alias_type_or_template_p (tree t)
5129 {
5130 if (t == NULL_TREE)
5131 return false;
5132 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5133 || (TYPE_P (t)
5134 && TYPE_NAME (t)
5135 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5136 || DECL_ALIAS_TEMPLATE_P (t));
5137 }
5138
5139 /* Return TRUE iff is a specialization of an alias template. */
5140
5141 bool
5142 alias_template_specialization_p (const_tree t)
5143 {
5144 if (t == NULL_TREE)
5145 return false;
5146
5147 return (TYPE_P (t)
5148 && TYPE_TEMPLATE_INFO (t)
5149 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5150 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5151 }
5152
5153 /* Return the number of innermost template parameters in TMPL. */
5154
5155 static int
5156 num_innermost_template_parms (tree tmpl)
5157 {
5158 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5159 return TREE_VEC_LENGTH (parms);
5160 }
5161
5162 /* Return either TMPL or another template that it is equivalent to under DR
5163 1286: An alias that just changes the name of a template is equivalent to
5164 the other template. */
5165
5166 static tree
5167 get_underlying_template (tree tmpl)
5168 {
5169 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5170 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5171 {
5172 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5173 if (TYPE_TEMPLATE_INFO (result))
5174 {
5175 tree sub = TYPE_TI_TEMPLATE (result);
5176 if (PRIMARY_TEMPLATE_P (sub)
5177 && (num_innermost_template_parms (tmpl)
5178 == num_innermost_template_parms (sub))
5179 && same_type_p (result, TREE_TYPE (sub)))
5180 {
5181 /* The alias type is equivalent to the pattern of the
5182 underlying template, so strip the alias. */
5183 tmpl = sub;
5184 continue;
5185 }
5186 }
5187 break;
5188 }
5189 return tmpl;
5190 }
5191
5192 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5193 must be a function or a pointer-to-function type, as specified
5194 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5195 and check that the resulting function has external linkage. */
5196
5197 static tree
5198 convert_nontype_argument_function (tree type, tree expr)
5199 {
5200 tree fns = expr;
5201 tree fn, fn_no_ptr;
5202 linkage_kind linkage;
5203
5204 fn = instantiate_type (type, fns, tf_none);
5205 if (fn == error_mark_node)
5206 return error_mark_node;
5207
5208 fn_no_ptr = fn;
5209 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5210 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5211 if (BASELINK_P (fn_no_ptr))
5212 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5213
5214 /* [temp.arg.nontype]/1
5215
5216 A template-argument for a non-type, non-template template-parameter
5217 shall be one of:
5218 [...]
5219 -- the address of an object or function with external [C++11: or
5220 internal] linkage. */
5221
5222 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5223 {
5224 error ("%qE is not a valid template argument for type %qT", expr, type);
5225 if (TYPE_PTR_P (type))
5226 error ("it must be the address of a function with external linkage");
5227 else
5228 error ("it must be the name of a function with external linkage");
5229 return NULL_TREE;
5230 }
5231
5232 linkage = decl_linkage (fn_no_ptr);
5233 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5234 {
5235 if (cxx_dialect >= cxx11)
5236 error ("%qE is not a valid template argument for type %qT "
5237 "because %qD has no linkage",
5238 expr, type, fn_no_ptr);
5239 else
5240 error ("%qE is not a valid template argument for type %qT "
5241 "because %qD does not have external linkage",
5242 expr, type, fn_no_ptr);
5243 return NULL_TREE;
5244 }
5245
5246 return fn;
5247 }
5248
5249 /* Subroutine of convert_nontype_argument.
5250 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5251 Emit an error otherwise. */
5252
5253 static bool
5254 check_valid_ptrmem_cst_expr (tree type, tree expr,
5255 tsubst_flags_t complain)
5256 {
5257 STRIP_NOPS (expr);
5258 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5259 return true;
5260 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5261 return true;
5262 if (complain & tf_error)
5263 {
5264 error ("%qE is not a valid template argument for type %qT",
5265 expr, type);
5266 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5267 }
5268 return false;
5269 }
5270
5271 /* Returns TRUE iff the address of OP is value-dependent.
5272
5273 14.6.2.4 [temp.dep.temp]:
5274 A non-integral non-type template-argument is dependent if its type is
5275 dependent or it has either of the following forms
5276 qualified-id
5277 & qualified-id
5278 and contains a nested-name-specifier which specifies a class-name that
5279 names a dependent type.
5280
5281 We generalize this to just say that the address of a member of a
5282 dependent class is value-dependent; the above doesn't cover the
5283 address of a static data member named with an unqualified-id. */
5284
5285 static bool
5286 has_value_dependent_address (tree op)
5287 {
5288 /* We could use get_inner_reference here, but there's no need;
5289 this is only relevant for template non-type arguments, which
5290 can only be expressed as &id-expression. */
5291 if (DECL_P (op))
5292 {
5293 tree ctx = CP_DECL_CONTEXT (op);
5294 if (TYPE_P (ctx) && dependent_type_p (ctx))
5295 return true;
5296 }
5297
5298 return false;
5299 }
5300
5301 /* The next set of functions are used for providing helpful explanatory
5302 diagnostics for failed overload resolution. Their messages should be
5303 indented by two spaces for consistency with the messages in
5304 call.c */
5305
5306 static int
5307 unify_success (bool /*explain_p*/)
5308 {
5309 return 0;
5310 }
5311
5312 static int
5313 unify_parameter_deduction_failure (bool explain_p, tree parm)
5314 {
5315 if (explain_p)
5316 inform (input_location,
5317 " couldn't deduce template parameter %qD", parm);
5318 return 1;
5319 }
5320
5321 static int
5322 unify_invalid (bool /*explain_p*/)
5323 {
5324 return 1;
5325 }
5326
5327 static int
5328 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5329 {
5330 if (explain_p)
5331 inform (input_location,
5332 " types %qT and %qT have incompatible cv-qualifiers",
5333 parm, arg);
5334 return 1;
5335 }
5336
5337 static int
5338 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5339 {
5340 if (explain_p)
5341 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5342 return 1;
5343 }
5344
5345 static int
5346 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5347 {
5348 if (explain_p)
5349 inform (input_location,
5350 " template parameter %qD is not a parameter pack, but "
5351 "argument %qD is",
5352 parm, arg);
5353 return 1;
5354 }
5355
5356 static int
5357 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5358 {
5359 if (explain_p)
5360 inform (input_location,
5361 " template argument %qE does not match "
5362 "pointer-to-member constant %qE",
5363 arg, parm);
5364 return 1;
5365 }
5366
5367 static int
5368 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5369 {
5370 if (explain_p)
5371 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5372 return 1;
5373 }
5374
5375 static int
5376 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5377 {
5378 if (explain_p)
5379 inform (input_location,
5380 " inconsistent parameter pack deduction with %qT and %qT",
5381 old_arg, new_arg);
5382 return 1;
5383 }
5384
5385 static int
5386 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5387 {
5388 if (explain_p)
5389 {
5390 if (TYPE_P (parm))
5391 inform (input_location,
5392 " deduced conflicting types for parameter %qT (%qT and %qT)",
5393 parm, first, second);
5394 else
5395 inform (input_location,
5396 " deduced conflicting values for non-type parameter "
5397 "%qE (%qE and %qE)", parm, first, second);
5398 }
5399 return 1;
5400 }
5401
5402 static int
5403 unify_vla_arg (bool explain_p, tree arg)
5404 {
5405 if (explain_p)
5406 inform (input_location,
5407 " variable-sized array type %qT is not "
5408 "a valid template argument",
5409 arg);
5410 return 1;
5411 }
5412
5413 static int
5414 unify_method_type_error (bool explain_p, tree arg)
5415 {
5416 if (explain_p)
5417 inform (input_location,
5418 " member function type %qT is not a valid template argument",
5419 arg);
5420 return 1;
5421 }
5422
5423 static int
5424 unify_arity (bool explain_p, int have, int wanted)
5425 {
5426 if (explain_p)
5427 inform_n (input_location, wanted,
5428 " candidate expects %d argument, %d provided",
5429 " candidate expects %d arguments, %d provided",
5430 wanted, have);
5431 return 1;
5432 }
5433
5434 static int
5435 unify_too_many_arguments (bool explain_p, int have, int wanted)
5436 {
5437 return unify_arity (explain_p, have, wanted);
5438 }
5439
5440 static int
5441 unify_too_few_arguments (bool explain_p, int have, int wanted)
5442 {
5443 return unify_arity (explain_p, have, wanted);
5444 }
5445
5446 static int
5447 unify_arg_conversion (bool explain_p, tree to_type,
5448 tree from_type, tree arg)
5449 {
5450 if (explain_p)
5451 inform (EXPR_LOC_OR_LOC (arg, input_location),
5452 " cannot convert %qE (type %qT) to type %qT",
5453 arg, from_type, to_type);
5454 return 1;
5455 }
5456
5457 static int
5458 unify_no_common_base (bool explain_p, enum template_base_result r,
5459 tree parm, tree arg)
5460 {
5461 if (explain_p)
5462 switch (r)
5463 {
5464 case tbr_ambiguous_baseclass:
5465 inform (input_location, " %qT is an ambiguous base class of %qT",
5466 parm, arg);
5467 break;
5468 default:
5469 inform (input_location, " %qT is not derived from %qT", arg, parm);
5470 break;
5471 }
5472 return 1;
5473 }
5474
5475 static int
5476 unify_inconsistent_template_template_parameters (bool explain_p)
5477 {
5478 if (explain_p)
5479 inform (input_location,
5480 " template parameters of a template template argument are "
5481 "inconsistent with other deduced template arguments");
5482 return 1;
5483 }
5484
5485 static int
5486 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5487 {
5488 if (explain_p)
5489 inform (input_location,
5490 " can't deduce a template for %qT from non-template type %qT",
5491 parm, arg);
5492 return 1;
5493 }
5494
5495 static int
5496 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5497 {
5498 if (explain_p)
5499 inform (input_location,
5500 " template argument %qE does not match %qD", arg, parm);
5501 return 1;
5502 }
5503
5504 static int
5505 unify_overload_resolution_failure (bool explain_p, tree arg)
5506 {
5507 if (explain_p)
5508 inform (input_location,
5509 " could not resolve address from overloaded function %qE",
5510 arg);
5511 return 1;
5512 }
5513
5514 /* Attempt to convert the non-type template parameter EXPR to the
5515 indicated TYPE. If the conversion is successful, return the
5516 converted value. If the conversion is unsuccessful, return
5517 NULL_TREE if we issued an error message, or error_mark_node if we
5518 did not. We issue error messages for out-and-out bad template
5519 parameters, but not simply because the conversion failed, since we
5520 might be just trying to do argument deduction. Both TYPE and EXPR
5521 must be non-dependent.
5522
5523 The conversion follows the special rules described in
5524 [temp.arg.nontype], and it is much more strict than an implicit
5525 conversion.
5526
5527 This function is called twice for each template argument (see
5528 lookup_template_class for a more accurate description of this
5529 problem). This means that we need to handle expressions which
5530 are not valid in a C++ source, but can be created from the
5531 first call (for instance, casts to perform conversions). These
5532 hacks can go away after we fix the double coercion problem. */
5533
5534 static tree
5535 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5536 {
5537 tree expr_type;
5538
5539 /* Detect immediately string literals as invalid non-type argument.
5540 This special-case is not needed for correctness (we would easily
5541 catch this later), but only to provide better diagnostic for this
5542 common user mistake. As suggested by DR 100, we do not mention
5543 linkage issues in the diagnostic as this is not the point. */
5544 /* FIXME we're making this OK. */
5545 if (TREE_CODE (expr) == STRING_CST)
5546 {
5547 if (complain & tf_error)
5548 error ("%qE is not a valid template argument for type %qT "
5549 "because string literals can never be used in this context",
5550 expr, type);
5551 return NULL_TREE;
5552 }
5553
5554 /* Add the ADDR_EXPR now for the benefit of
5555 value_dependent_expression_p. */
5556 if (TYPE_PTROBV_P (type)
5557 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5558 {
5559 expr = decay_conversion (expr, complain);
5560 if (expr == error_mark_node)
5561 return error_mark_node;
5562 }
5563
5564 /* If we are in a template, EXPR may be non-dependent, but still
5565 have a syntactic, rather than semantic, form. For example, EXPR
5566 might be a SCOPE_REF, rather than the VAR_DECL to which the
5567 SCOPE_REF refers. Preserving the qualifying scope is necessary
5568 so that access checking can be performed when the template is
5569 instantiated -- but here we need the resolved form so that we can
5570 convert the argument. */
5571 if (TYPE_REF_OBJ_P (type)
5572 && has_value_dependent_address (expr))
5573 /* If we want the address and it's value-dependent, don't fold. */;
5574 else if (!type_unknown_p (expr))
5575 expr = fold_non_dependent_expr_sfinae (expr, complain);
5576 if (error_operand_p (expr))
5577 return error_mark_node;
5578 expr_type = TREE_TYPE (expr);
5579 if (TREE_CODE (type) == REFERENCE_TYPE)
5580 expr = mark_lvalue_use (expr);
5581 else
5582 expr = mark_rvalue_use (expr);
5583
5584 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5585 to a non-type argument of "nullptr". */
5586 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5587 expr = convert (type, expr);
5588
5589 /* In C++11, integral or enumeration non-type template arguments can be
5590 arbitrary constant expressions. Pointer and pointer to
5591 member arguments can be general constant expressions that evaluate
5592 to a null value, but otherwise still need to be of a specific form. */
5593 if (cxx_dialect >= cxx11)
5594 {
5595 if (TREE_CODE (expr) == PTRMEM_CST)
5596 /* A PTRMEM_CST is already constant, and a valid template
5597 argument for a parameter of pointer to member type, we just want
5598 to leave it in that form rather than lower it to a
5599 CONSTRUCTOR. */;
5600 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5601 expr = maybe_constant_value (expr);
5602 else if (TYPE_PTR_OR_PTRMEM_P (type))
5603 {
5604 tree folded = maybe_constant_value (expr);
5605 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5606 : null_member_pointer_value_p (folded))
5607 expr = folded;
5608 }
5609 }
5610
5611 /* HACK: Due to double coercion, we can get a
5612 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5613 which is the tree that we built on the first call (see
5614 below when coercing to reference to object or to reference to
5615 function). We just strip everything and get to the arg.
5616 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5617 for examples. */
5618 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5619 {
5620 tree probe_type, probe = expr;
5621 if (REFERENCE_REF_P (probe))
5622 probe = TREE_OPERAND (probe, 0);
5623 probe_type = TREE_TYPE (probe);
5624 if (TREE_CODE (probe) == NOP_EXPR)
5625 {
5626 /* ??? Maybe we could use convert_from_reference here, but we
5627 would need to relax its constraints because the NOP_EXPR
5628 could actually change the type to something more cv-qualified,
5629 and this is not folded by convert_from_reference. */
5630 tree addr = TREE_OPERAND (probe, 0);
5631 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5632 && TREE_CODE (addr) == ADDR_EXPR
5633 && TYPE_PTR_P (TREE_TYPE (addr))
5634 && (same_type_ignoring_top_level_qualifiers_p
5635 (TREE_TYPE (probe_type),
5636 TREE_TYPE (TREE_TYPE (addr)))))
5637 {
5638 expr = TREE_OPERAND (addr, 0);
5639 expr_type = TREE_TYPE (probe_type);
5640 }
5641 }
5642 }
5643
5644 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5645 parameter is a pointer to object, through decay and
5646 qualification conversion. Let's strip everything. */
5647 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5648 {
5649 tree probe = expr;
5650 STRIP_NOPS (probe);
5651 if (TREE_CODE (probe) == ADDR_EXPR
5652 && TYPE_PTR_P (TREE_TYPE (probe)))
5653 {
5654 /* Skip the ADDR_EXPR only if it is part of the decay for
5655 an array. Otherwise, it is part of the original argument
5656 in the source code. */
5657 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5658 probe = TREE_OPERAND (probe, 0);
5659 expr = probe;
5660 expr_type = TREE_TYPE (expr);
5661 }
5662 }
5663
5664 /* [temp.arg.nontype]/5, bullet 1
5665
5666 For a non-type template-parameter of integral or enumeration type,
5667 integral promotions (_conv.prom_) and integral conversions
5668 (_conv.integral_) are applied. */
5669 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5670 {
5671 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5672 t = maybe_constant_value (t);
5673 if (t != error_mark_node)
5674 expr = t;
5675
5676 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5677 return error_mark_node;
5678
5679 /* Notice that there are constant expressions like '4 % 0' which
5680 do not fold into integer constants. */
5681 if (TREE_CODE (expr) != INTEGER_CST)
5682 {
5683 if (complain & tf_error)
5684 {
5685 int errs = errorcount, warns = warningcount + werrorcount;
5686 if (processing_template_decl
5687 && !require_potential_constant_expression (expr))
5688 return NULL_TREE;
5689 expr = cxx_constant_value (expr);
5690 if (errorcount > errs || warningcount + werrorcount > warns)
5691 inform (EXPR_LOC_OR_LOC (expr, input_location),
5692 "in template argument for type %qT ", type);
5693 if (expr == error_mark_node)
5694 return NULL_TREE;
5695 /* else cxx_constant_value complained but gave us
5696 a real constant, so go ahead. */
5697 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5698 }
5699 else
5700 return NULL_TREE;
5701 }
5702
5703 /* Avoid typedef problems. */
5704 if (TREE_TYPE (expr) != type)
5705 expr = fold_convert (type, expr);
5706 }
5707 /* [temp.arg.nontype]/5, bullet 2
5708
5709 For a non-type template-parameter of type pointer to object,
5710 qualification conversions (_conv.qual_) and the array-to-pointer
5711 conversion (_conv.array_) are applied. */
5712 else if (TYPE_PTROBV_P (type))
5713 {
5714 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5715
5716 A template-argument for a non-type, non-template template-parameter
5717 shall be one of: [...]
5718
5719 -- the name of a non-type template-parameter;
5720 -- the address of an object or function with external linkage, [...]
5721 expressed as "& id-expression" where the & is optional if the name
5722 refers to a function or array, or if the corresponding
5723 template-parameter is a reference.
5724
5725 Here, we do not care about functions, as they are invalid anyway
5726 for a parameter of type pointer-to-object. */
5727
5728 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5729 /* Non-type template parameters are OK. */
5730 ;
5731 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5732 /* Null pointer values are OK in C++11. */;
5733 else if (TREE_CODE (expr) != ADDR_EXPR
5734 && TREE_CODE (expr_type) != ARRAY_TYPE)
5735 {
5736 if (VAR_P (expr))
5737 {
5738 error ("%qD is not a valid template argument "
5739 "because %qD is a variable, not the address of "
5740 "a variable",
5741 expr, expr);
5742 return NULL_TREE;
5743 }
5744 if (POINTER_TYPE_P (expr_type))
5745 {
5746 error ("%qE is not a valid template argument for %qT "
5747 "because it is not the address of a variable",
5748 expr, type);
5749 return NULL_TREE;
5750 }
5751 /* Other values, like integer constants, might be valid
5752 non-type arguments of some other type. */
5753 return error_mark_node;
5754 }
5755 else
5756 {
5757 tree decl;
5758
5759 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5760 ? TREE_OPERAND (expr, 0) : expr);
5761 if (!VAR_P (decl))
5762 {
5763 error ("%qE is not a valid template argument of type %qT "
5764 "because %qE is not a variable",
5765 expr, type, decl);
5766 return NULL_TREE;
5767 }
5768 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5769 {
5770 error ("%qE is not a valid template argument of type %qT "
5771 "because %qD does not have external linkage",
5772 expr, type, decl);
5773 return NULL_TREE;
5774 }
5775 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5776 {
5777 error ("%qE is not a valid template argument of type %qT "
5778 "because %qD has no linkage",
5779 expr, type, decl);
5780 return NULL_TREE;
5781 }
5782 }
5783
5784 expr = decay_conversion (expr, complain);
5785 if (expr == error_mark_node)
5786 return error_mark_node;
5787
5788 expr = perform_qualification_conversions (type, expr);
5789 if (expr == error_mark_node)
5790 return error_mark_node;
5791 }
5792 /* [temp.arg.nontype]/5, bullet 3
5793
5794 For a non-type template-parameter of type reference to object, no
5795 conversions apply. The type referred to by the reference may be more
5796 cv-qualified than the (otherwise identical) type of the
5797 template-argument. The template-parameter is bound directly to the
5798 template-argument, which must be an lvalue. */
5799 else if (TYPE_REF_OBJ_P (type))
5800 {
5801 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5802 expr_type))
5803 return error_mark_node;
5804
5805 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5806 {
5807 error ("%qE is not a valid template argument for type %qT "
5808 "because of conflicts in cv-qualification", expr, type);
5809 return NULL_TREE;
5810 }
5811
5812 if (!real_lvalue_p (expr))
5813 {
5814 error ("%qE is not a valid template argument for type %qT "
5815 "because it is not an lvalue", expr, type);
5816 return NULL_TREE;
5817 }
5818
5819 /* [temp.arg.nontype]/1
5820
5821 A template-argument for a non-type, non-template template-parameter
5822 shall be one of: [...]
5823
5824 -- the address of an object or function with external linkage. */
5825 if (INDIRECT_REF_P (expr)
5826 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5827 {
5828 expr = TREE_OPERAND (expr, 0);
5829 if (DECL_P (expr))
5830 {
5831 error ("%q#D is not a valid template argument for type %qT "
5832 "because a reference variable does not have a constant "
5833 "address", expr, type);
5834 return NULL_TREE;
5835 }
5836 }
5837
5838 if (!DECL_P (expr))
5839 {
5840 error ("%qE is not a valid template argument for type %qT "
5841 "because it is not an object with external linkage",
5842 expr, type);
5843 return NULL_TREE;
5844 }
5845
5846 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5847 {
5848 error ("%qE is not a valid template argument for type %qT "
5849 "because object %qD has not external linkage",
5850 expr, type, expr);
5851 return NULL_TREE;
5852 }
5853
5854 expr = build_nop (type, build_address (expr));
5855 }
5856 /* [temp.arg.nontype]/5, bullet 4
5857
5858 For a non-type template-parameter of type pointer to function, only
5859 the function-to-pointer conversion (_conv.func_) is applied. If the
5860 template-argument represents a set of overloaded functions (or a
5861 pointer to such), the matching function is selected from the set
5862 (_over.over_). */
5863 else if (TYPE_PTRFN_P (type))
5864 {
5865 /* If the argument is a template-id, we might not have enough
5866 context information to decay the pointer. */
5867 if (!type_unknown_p (expr_type))
5868 {
5869 expr = decay_conversion (expr, complain);
5870 if (expr == error_mark_node)
5871 return error_mark_node;
5872 }
5873
5874 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5875 /* Null pointer values are OK in C++11. */
5876 return perform_qualification_conversions (type, expr);
5877
5878 expr = convert_nontype_argument_function (type, expr);
5879 if (!expr || expr == error_mark_node)
5880 return expr;
5881 }
5882 /* [temp.arg.nontype]/5, bullet 5
5883
5884 For a non-type template-parameter of type reference to function, no
5885 conversions apply. If the template-argument represents a set of
5886 overloaded functions, the matching function is selected from the set
5887 (_over.over_). */
5888 else if (TYPE_REFFN_P (type))
5889 {
5890 if (TREE_CODE (expr) == ADDR_EXPR)
5891 {
5892 error ("%qE is not a valid template argument for type %qT "
5893 "because it is a pointer", expr, type);
5894 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5895 return NULL_TREE;
5896 }
5897
5898 expr = convert_nontype_argument_function (type, expr);
5899 if (!expr || expr == error_mark_node)
5900 return expr;
5901
5902 expr = build_nop (type, build_address (expr));
5903 }
5904 /* [temp.arg.nontype]/5, bullet 6
5905
5906 For a non-type template-parameter of type pointer to member function,
5907 no conversions apply. If the template-argument represents a set of
5908 overloaded member functions, the matching member function is selected
5909 from the set (_over.over_). */
5910 else if (TYPE_PTRMEMFUNC_P (type))
5911 {
5912 expr = instantiate_type (type, expr, tf_none);
5913 if (expr == error_mark_node)
5914 return error_mark_node;
5915
5916 /* [temp.arg.nontype] bullet 1 says the pointer to member
5917 expression must be a pointer-to-member constant. */
5918 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5919 return error_mark_node;
5920
5921 /* There is no way to disable standard conversions in
5922 resolve_address_of_overloaded_function (called by
5923 instantiate_type). It is possible that the call succeeded by
5924 converting &B::I to &D::I (where B is a base of D), so we need
5925 to reject this conversion here.
5926
5927 Actually, even if there was a way to disable standard conversions,
5928 it would still be better to reject them here so that we can
5929 provide a superior diagnostic. */
5930 if (!same_type_p (TREE_TYPE (expr), type))
5931 {
5932 error ("%qE is not a valid template argument for type %qT "
5933 "because it is of type %qT", expr, type,
5934 TREE_TYPE (expr));
5935 /* If we are just one standard conversion off, explain. */
5936 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5937 inform (input_location,
5938 "standard conversions are not allowed in this context");
5939 return NULL_TREE;
5940 }
5941 }
5942 /* [temp.arg.nontype]/5, bullet 7
5943
5944 For a non-type template-parameter of type pointer to data member,
5945 qualification conversions (_conv.qual_) are applied. */
5946 else if (TYPE_PTRDATAMEM_P (type))
5947 {
5948 /* [temp.arg.nontype] bullet 1 says the pointer to member
5949 expression must be a pointer-to-member constant. */
5950 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5951 return error_mark_node;
5952
5953 expr = perform_qualification_conversions (type, expr);
5954 if (expr == error_mark_node)
5955 return expr;
5956 }
5957 else if (NULLPTR_TYPE_P (type))
5958 {
5959 if (expr != nullptr_node)
5960 {
5961 error ("%qE is not a valid template argument for type %qT "
5962 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5963 return NULL_TREE;
5964 }
5965 return expr;
5966 }
5967 /* A template non-type parameter must be one of the above. */
5968 else
5969 gcc_unreachable ();
5970
5971 /* Sanity check: did we actually convert the argument to the
5972 right type? */
5973 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5974 (type, TREE_TYPE (expr)));
5975 return expr;
5976 }
5977
5978 /* Subroutine of coerce_template_template_parms, which returns 1 if
5979 PARM_PARM and ARG_PARM match using the rule for the template
5980 parameters of template template parameters. Both PARM and ARG are
5981 template parameters; the rest of the arguments are the same as for
5982 coerce_template_template_parms.
5983 */
5984 static int
5985 coerce_template_template_parm (tree parm,
5986 tree arg,
5987 tsubst_flags_t complain,
5988 tree in_decl,
5989 tree outer_args)
5990 {
5991 if (arg == NULL_TREE || arg == error_mark_node
5992 || parm == NULL_TREE || parm == error_mark_node)
5993 return 0;
5994
5995 if (TREE_CODE (arg) != TREE_CODE (parm))
5996 return 0;
5997
5998 switch (TREE_CODE (parm))
5999 {
6000 case TEMPLATE_DECL:
6001 /* We encounter instantiations of templates like
6002 template <template <template <class> class> class TT>
6003 class C; */
6004 {
6005 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6006 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6007
6008 if (!coerce_template_template_parms
6009 (parmparm, argparm, complain, in_decl, outer_args))
6010 return 0;
6011 }
6012 /* Fall through. */
6013
6014 case TYPE_DECL:
6015 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6016 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6017 /* Argument is a parameter pack but parameter is not. */
6018 return 0;
6019 break;
6020
6021 case PARM_DECL:
6022 /* The tsubst call is used to handle cases such as
6023
6024 template <int> class C {};
6025 template <class T, template <T> class TT> class D {};
6026 D<int, C> d;
6027
6028 i.e. the parameter list of TT depends on earlier parameters. */
6029 if (!uses_template_parms (TREE_TYPE (arg))
6030 && !same_type_p
6031 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6032 TREE_TYPE (arg)))
6033 return 0;
6034
6035 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6036 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6037 /* Argument is a parameter pack but parameter is not. */
6038 return 0;
6039
6040 break;
6041
6042 default:
6043 gcc_unreachable ();
6044 }
6045
6046 return 1;
6047 }
6048
6049
6050 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6051 template template parameters. Both PARM_PARMS and ARG_PARMS are
6052 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6053 or PARM_DECL.
6054
6055 Consider the example:
6056 template <class T> class A;
6057 template<template <class U> class TT> class B;
6058
6059 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6060 the parameters to A, and OUTER_ARGS contains A. */
6061
6062 static int
6063 coerce_template_template_parms (tree parm_parms,
6064 tree arg_parms,
6065 tsubst_flags_t complain,
6066 tree in_decl,
6067 tree outer_args)
6068 {
6069 int nparms, nargs, i;
6070 tree parm, arg;
6071 int variadic_p = 0;
6072
6073 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6074 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6075
6076 nparms = TREE_VEC_LENGTH (parm_parms);
6077 nargs = TREE_VEC_LENGTH (arg_parms);
6078
6079 /* Determine whether we have a parameter pack at the end of the
6080 template template parameter's template parameter list. */
6081 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6082 {
6083 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6084
6085 if (parm == error_mark_node)
6086 return 0;
6087
6088 switch (TREE_CODE (parm))
6089 {
6090 case TEMPLATE_DECL:
6091 case TYPE_DECL:
6092 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6093 variadic_p = 1;
6094 break;
6095
6096 case PARM_DECL:
6097 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6098 variadic_p = 1;
6099 break;
6100
6101 default:
6102 gcc_unreachable ();
6103 }
6104 }
6105
6106 if (nargs != nparms
6107 && !(variadic_p && nargs >= nparms - 1))
6108 return 0;
6109
6110 /* Check all of the template parameters except the parameter pack at
6111 the end (if any). */
6112 for (i = 0; i < nparms - variadic_p; ++i)
6113 {
6114 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6115 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6116 continue;
6117
6118 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6119 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6120
6121 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6122 outer_args))
6123 return 0;
6124
6125 }
6126
6127 if (variadic_p)
6128 {
6129 /* Check each of the template parameters in the template
6130 argument against the template parameter pack at the end of
6131 the template template parameter. */
6132 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6133 return 0;
6134
6135 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6136
6137 for (; i < nargs; ++i)
6138 {
6139 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6140 continue;
6141
6142 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6143
6144 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6145 outer_args))
6146 return 0;
6147 }
6148 }
6149
6150 return 1;
6151 }
6152
6153 /* Verifies that the deduced template arguments (in TARGS) for the
6154 template template parameters (in TPARMS) represent valid bindings,
6155 by comparing the template parameter list of each template argument
6156 to the template parameter list of its corresponding template
6157 template parameter, in accordance with DR150. This
6158 routine can only be called after all template arguments have been
6159 deduced. It will return TRUE if all of the template template
6160 parameter bindings are okay, FALSE otherwise. */
6161 bool
6162 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6163 {
6164 int i, ntparms = TREE_VEC_LENGTH (tparms);
6165 bool ret = true;
6166
6167 /* We're dealing with template parms in this process. */
6168 ++processing_template_decl;
6169
6170 targs = INNERMOST_TEMPLATE_ARGS (targs);
6171
6172 for (i = 0; i < ntparms; ++i)
6173 {
6174 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6175 tree targ = TREE_VEC_ELT (targs, i);
6176
6177 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6178 {
6179 tree packed_args = NULL_TREE;
6180 int idx, len = 1;
6181
6182 if (ARGUMENT_PACK_P (targ))
6183 {
6184 /* Look inside the argument pack. */
6185 packed_args = ARGUMENT_PACK_ARGS (targ);
6186 len = TREE_VEC_LENGTH (packed_args);
6187 }
6188
6189 for (idx = 0; idx < len; ++idx)
6190 {
6191 tree targ_parms = NULL_TREE;
6192
6193 if (packed_args)
6194 /* Extract the next argument from the argument
6195 pack. */
6196 targ = TREE_VEC_ELT (packed_args, idx);
6197
6198 if (PACK_EXPANSION_P (targ))
6199 /* Look at the pattern of the pack expansion. */
6200 targ = PACK_EXPANSION_PATTERN (targ);
6201
6202 /* Extract the template parameters from the template
6203 argument. */
6204 if (TREE_CODE (targ) == TEMPLATE_DECL)
6205 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6206 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6207 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6208
6209 /* Verify that we can coerce the template template
6210 parameters from the template argument to the template
6211 parameter. This requires an exact match. */
6212 if (targ_parms
6213 && !coerce_template_template_parms
6214 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6215 targ_parms,
6216 tf_none,
6217 tparm,
6218 targs))
6219 {
6220 ret = false;
6221 goto out;
6222 }
6223 }
6224 }
6225 }
6226
6227 out:
6228
6229 --processing_template_decl;
6230 return ret;
6231 }
6232
6233 /* Since type attributes aren't mangled, we need to strip them from
6234 template type arguments. */
6235
6236 static tree
6237 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6238 {
6239 tree mv;
6240 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6241 return arg;
6242 mv = TYPE_MAIN_VARIANT (arg);
6243 arg = strip_typedefs (arg);
6244 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6245 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6246 {
6247 if (complain & tf_warning)
6248 warning (0, "ignoring attributes on template argument %qT", arg);
6249 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6250 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6251 }
6252 return arg;
6253 }
6254
6255 /* Convert the indicated template ARG as necessary to match the
6256 indicated template PARM. Returns the converted ARG, or
6257 error_mark_node if the conversion was unsuccessful. Error and
6258 warning messages are issued under control of COMPLAIN. This
6259 conversion is for the Ith parameter in the parameter list. ARGS is
6260 the full set of template arguments deduced so far. */
6261
6262 static tree
6263 convert_template_argument (tree parm,
6264 tree arg,
6265 tree args,
6266 tsubst_flags_t complain,
6267 int i,
6268 tree in_decl)
6269 {
6270 tree orig_arg;
6271 tree val;
6272 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6273
6274 if (TREE_CODE (arg) == TREE_LIST
6275 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6276 {
6277 /* The template argument was the name of some
6278 member function. That's usually
6279 invalid, but static members are OK. In any
6280 case, grab the underlying fields/functions
6281 and issue an error later if required. */
6282 orig_arg = TREE_VALUE (arg);
6283 TREE_TYPE (arg) = unknown_type_node;
6284 }
6285
6286 orig_arg = arg;
6287
6288 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6289 requires_type = (TREE_CODE (parm) == TYPE_DECL
6290 || requires_tmpl_type);
6291
6292 /* When determining whether an argument pack expansion is a template,
6293 look at the pattern. */
6294 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6295 arg = PACK_EXPANSION_PATTERN (arg);
6296
6297 /* Deal with an injected-class-name used as a template template arg. */
6298 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6299 {
6300 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6301 if (TREE_CODE (t) == TEMPLATE_DECL)
6302 {
6303 if (cxx_dialect >= cxx11)
6304 /* OK under DR 1004. */;
6305 else if (complain & tf_warning_or_error)
6306 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6307 " used as template template argument", TYPE_NAME (arg));
6308 else if (flag_pedantic_errors)
6309 t = arg;
6310
6311 arg = t;
6312 }
6313 }
6314
6315 is_tmpl_type =
6316 ((TREE_CODE (arg) == TEMPLATE_DECL
6317 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6318 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6319 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6320 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6321
6322 if (is_tmpl_type
6323 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6324 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6325 arg = TYPE_STUB_DECL (arg);
6326
6327 is_type = TYPE_P (arg) || is_tmpl_type;
6328
6329 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6330 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6331 {
6332 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6333 {
6334 if (complain & tf_error)
6335 error ("invalid use of destructor %qE as a type", orig_arg);
6336 return error_mark_node;
6337 }
6338
6339 permerror (input_location,
6340 "to refer to a type member of a template parameter, "
6341 "use %<typename %E%>", orig_arg);
6342
6343 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6344 TREE_OPERAND (arg, 1),
6345 typename_type,
6346 complain);
6347 arg = orig_arg;
6348 is_type = 1;
6349 }
6350 if (is_type != requires_type)
6351 {
6352 if (in_decl)
6353 {
6354 if (complain & tf_error)
6355 {
6356 error ("type/value mismatch at argument %d in template "
6357 "parameter list for %qD",
6358 i + 1, in_decl);
6359 if (is_type)
6360 error (" expected a constant of type %qT, got %qT",
6361 TREE_TYPE (parm),
6362 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6363 else if (requires_tmpl_type)
6364 error (" expected a class template, got %qE", orig_arg);
6365 else
6366 error (" expected a type, got %qE", orig_arg);
6367 }
6368 }
6369 return error_mark_node;
6370 }
6371 if (is_tmpl_type ^ requires_tmpl_type)
6372 {
6373 if (in_decl && (complain & tf_error))
6374 {
6375 error ("type/value mismatch at argument %d in template "
6376 "parameter list for %qD",
6377 i + 1, in_decl);
6378 if (is_tmpl_type)
6379 error (" expected a type, got %qT", DECL_NAME (arg));
6380 else
6381 error (" expected a class template, got %qT", orig_arg);
6382 }
6383 return error_mark_node;
6384 }
6385
6386 if (is_type)
6387 {
6388 if (requires_tmpl_type)
6389 {
6390 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6391 val = orig_arg;
6392 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6393 /* The number of argument required is not known yet.
6394 Just accept it for now. */
6395 val = TREE_TYPE (arg);
6396 else
6397 {
6398 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6399 tree argparm;
6400
6401 /* Strip alias templates that are equivalent to another
6402 template. */
6403 arg = get_underlying_template (arg);
6404 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6405
6406 if (coerce_template_template_parms (parmparm, argparm,
6407 complain, in_decl,
6408 args))
6409 {
6410 val = arg;
6411
6412 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6413 TEMPLATE_DECL. */
6414 if (val != error_mark_node)
6415 {
6416 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6417 val = TREE_TYPE (val);
6418 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6419 val = make_pack_expansion (val);
6420 }
6421 }
6422 else
6423 {
6424 if (in_decl && (complain & tf_error))
6425 {
6426 error ("type/value mismatch at argument %d in "
6427 "template parameter list for %qD",
6428 i + 1, in_decl);
6429 error (" expected a template of type %qD, got %qT",
6430 parm, orig_arg);
6431 }
6432
6433 val = error_mark_node;
6434 }
6435 }
6436 }
6437 else
6438 val = orig_arg;
6439 /* We only form one instance of each template specialization.
6440 Therefore, if we use a non-canonical variant (i.e., a
6441 typedef), any future messages referring to the type will use
6442 the typedef, which is confusing if those future uses do not
6443 themselves also use the typedef. */
6444 if (TYPE_P (val))
6445 val = canonicalize_type_argument (val, complain);
6446 }
6447 else
6448 {
6449 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6450
6451 if (invalid_nontype_parm_type_p (t, complain))
6452 return error_mark_node;
6453
6454 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6455 {
6456 if (same_type_p (t, TREE_TYPE (orig_arg)))
6457 val = orig_arg;
6458 else
6459 {
6460 /* Not sure if this is reachable, but it doesn't hurt
6461 to be robust. */
6462 error ("type mismatch in nontype parameter pack");
6463 val = error_mark_node;
6464 }
6465 }
6466 else if (!dependent_template_arg_p (orig_arg)
6467 && !uses_template_parms (t))
6468 /* We used to call digest_init here. However, digest_init
6469 will report errors, which we don't want when complain
6470 is zero. More importantly, digest_init will try too
6471 hard to convert things: for example, `0' should not be
6472 converted to pointer type at this point according to
6473 the standard. Accepting this is not merely an
6474 extension, since deciding whether or not these
6475 conversions can occur is part of determining which
6476 function template to call, or whether a given explicit
6477 argument specification is valid. */
6478 val = convert_nontype_argument (t, orig_arg, complain);
6479 else
6480 val = strip_typedefs_expr (orig_arg);
6481
6482 if (val == NULL_TREE)
6483 val = error_mark_node;
6484 else if (val == error_mark_node && (complain & tf_error))
6485 error ("could not convert template argument %qE to %qT", orig_arg, t);
6486
6487 if (TREE_CODE (val) == SCOPE_REF)
6488 {
6489 /* Strip typedefs from the SCOPE_REF. */
6490 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6491 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6492 complain);
6493 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6494 QUALIFIED_NAME_IS_TEMPLATE (val));
6495 }
6496 }
6497
6498 return val;
6499 }
6500
6501 /* Coerces the remaining template arguments in INNER_ARGS (from
6502 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6503 Returns the coerced argument pack. PARM_IDX is the position of this
6504 parameter in the template parameter list. ARGS is the original
6505 template argument list. */
6506 static tree
6507 coerce_template_parameter_pack (tree parms,
6508 int parm_idx,
6509 tree args,
6510 tree inner_args,
6511 int arg_idx,
6512 tree new_args,
6513 int* lost,
6514 tree in_decl,
6515 tsubst_flags_t complain)
6516 {
6517 tree parm = TREE_VEC_ELT (parms, parm_idx);
6518 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6519 tree packed_args;
6520 tree argument_pack;
6521 tree packed_types = NULL_TREE;
6522
6523 if (arg_idx > nargs)
6524 arg_idx = nargs;
6525
6526 packed_args = make_tree_vec (nargs - arg_idx);
6527
6528 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6529 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6530 {
6531 /* When the template parameter is a non-type template
6532 parameter pack whose type uses parameter packs, we need
6533 to look at each of the template arguments
6534 separately. Build a vector of the types for these
6535 non-type template parameters in PACKED_TYPES. */
6536 tree expansion
6537 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6538 packed_types = tsubst_pack_expansion (expansion, args,
6539 complain, in_decl);
6540
6541 if (packed_types == error_mark_node)
6542 return error_mark_node;
6543
6544 /* Check that we have the right number of arguments. */
6545 if (arg_idx < nargs
6546 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6547 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6548 {
6549 int needed_parms
6550 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6551 error ("wrong number of template arguments (%d, should be %d)",
6552 nargs, needed_parms);
6553 return error_mark_node;
6554 }
6555
6556 /* If we aren't able to check the actual arguments now
6557 (because they haven't been expanded yet), we can at least
6558 verify that all of the types used for the non-type
6559 template parameter pack are, in fact, valid for non-type
6560 template parameters. */
6561 if (arg_idx < nargs
6562 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6563 {
6564 int j, len = TREE_VEC_LENGTH (packed_types);
6565 for (j = 0; j < len; ++j)
6566 {
6567 tree t = TREE_VEC_ELT (packed_types, j);
6568 if (invalid_nontype_parm_type_p (t, complain))
6569 return error_mark_node;
6570 }
6571 }
6572 }
6573
6574 /* Convert the remaining arguments, which will be a part of the
6575 parameter pack "parm". */
6576 for (; arg_idx < nargs; ++arg_idx)
6577 {
6578 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6579 tree actual_parm = TREE_VALUE (parm);
6580
6581 if (packed_types && !PACK_EXPANSION_P (arg))
6582 {
6583 /* When we have a vector of types (corresponding to the
6584 non-type template parameter pack that uses parameter
6585 packs in its type, as mention above), and the
6586 argument is not an expansion (which expands to a
6587 currently unknown number of arguments), clone the
6588 parm and give it the next type in PACKED_TYPES. */
6589 actual_parm = copy_node (actual_parm);
6590 TREE_TYPE (actual_parm) =
6591 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6592 }
6593
6594 if (arg == error_mark_node)
6595 {
6596 if (complain & tf_error)
6597 error ("template argument %d is invalid", arg_idx + 1);
6598 }
6599 else
6600 arg = convert_template_argument (actual_parm,
6601 arg, new_args, complain, parm_idx,
6602 in_decl);
6603 if (arg == error_mark_node)
6604 (*lost)++;
6605 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6606 }
6607
6608 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6609 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6610 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6611 else
6612 {
6613 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6614 TREE_TYPE (argument_pack)
6615 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6616 TREE_CONSTANT (argument_pack) = 1;
6617 }
6618
6619 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6620 #ifdef ENABLE_CHECKING
6621 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6622 TREE_VEC_LENGTH (packed_args));
6623 #endif
6624 return argument_pack;
6625 }
6626
6627 /* Returns the number of pack expansions in the template argument vector
6628 ARGS. */
6629
6630 static int
6631 pack_expansion_args_count (tree args)
6632 {
6633 int i;
6634 int count = 0;
6635 if (args)
6636 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6637 {
6638 tree elt = TREE_VEC_ELT (args, i);
6639 if (elt && PACK_EXPANSION_P (elt))
6640 ++count;
6641 }
6642 return count;
6643 }
6644
6645 /* Convert all template arguments to their appropriate types, and
6646 return a vector containing the innermost resulting template
6647 arguments. If any error occurs, return error_mark_node. Error and
6648 warning messages are issued under control of COMPLAIN.
6649
6650 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6651 for arguments not specified in ARGS. Otherwise, if
6652 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6653 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6654 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6655 ARGS. */
6656
6657 static tree
6658 coerce_template_parms (tree parms,
6659 tree args,
6660 tree in_decl,
6661 tsubst_flags_t complain,
6662 bool require_all_args,
6663 bool use_default_args)
6664 {
6665 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6666 tree inner_args;
6667 tree new_args;
6668 tree new_inner_args;
6669 int saved_unevaluated_operand;
6670 int saved_inhibit_evaluation_warnings;
6671
6672 /* When used as a boolean value, indicates whether this is a
6673 variadic template parameter list. Since it's an int, we can also
6674 subtract it from nparms to get the number of non-variadic
6675 parameters. */
6676 int variadic_p = 0;
6677 int variadic_args_p = 0;
6678 int post_variadic_parms = 0;
6679
6680 if (args == error_mark_node)
6681 return error_mark_node;
6682
6683 nparms = TREE_VEC_LENGTH (parms);
6684
6685 /* Determine if there are any parameter packs. */
6686 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6687 {
6688 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6689 if (variadic_p)
6690 ++post_variadic_parms;
6691 if (template_parameter_pack_p (tparm))
6692 ++variadic_p;
6693 }
6694
6695 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6696 /* If there are no parameters that follow a parameter pack, we need to
6697 expand any argument packs so that we can deduce a parameter pack from
6698 some non-packed args followed by an argument pack, as in variadic85.C.
6699 If there are such parameters, we need to leave argument packs intact
6700 so the arguments are assigned properly. This can happen when dealing
6701 with a nested class inside a partial specialization of a class
6702 template, as in variadic92.C, or when deducing a template parameter pack
6703 from a sub-declarator, as in variadic114.C. */
6704 if (!post_variadic_parms)
6705 inner_args = expand_template_argument_pack (inner_args);
6706
6707 /* Count any pack expansion args. */
6708 variadic_args_p = pack_expansion_args_count (inner_args);
6709
6710 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6711 if ((nargs > nparms && !variadic_p)
6712 || (nargs < nparms - variadic_p
6713 && require_all_args
6714 && !variadic_args_p
6715 && (!use_default_args
6716 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6717 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6718 {
6719 if (complain & tf_error)
6720 {
6721 if (variadic_p)
6722 {
6723 nparms -= variadic_p;
6724 error ("wrong number of template arguments "
6725 "(%d, should be %d or more)", nargs, nparms);
6726 }
6727 else
6728 error ("wrong number of template arguments "
6729 "(%d, should be %d)", nargs, nparms);
6730
6731 if (in_decl)
6732 error ("provided for %q+D", in_decl);
6733 }
6734
6735 return error_mark_node;
6736 }
6737 /* We can't pass a pack expansion to a non-pack parameter of an alias
6738 template (DR 1430). */
6739 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6740 && variadic_args_p
6741 && nargs - variadic_args_p < nparms - variadic_p)
6742 {
6743 if (complain & tf_error)
6744 {
6745 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6746 {
6747 tree arg = TREE_VEC_ELT (inner_args, i);
6748 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6749
6750 if (PACK_EXPANSION_P (arg)
6751 && !template_parameter_pack_p (parm))
6752 {
6753 error ("pack expansion argument for non-pack parameter "
6754 "%qD of alias template %qD", parm, in_decl);
6755 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6756 goto found;
6757 }
6758 }
6759 gcc_unreachable ();
6760 found:;
6761 }
6762 return error_mark_node;
6763 }
6764
6765 /* We need to evaluate the template arguments, even though this
6766 template-id may be nested within a "sizeof". */
6767 saved_unevaluated_operand = cp_unevaluated_operand;
6768 cp_unevaluated_operand = 0;
6769 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6770 c_inhibit_evaluation_warnings = 0;
6771 new_inner_args = make_tree_vec (nparms);
6772 new_args = add_outermost_template_args (args, new_inner_args);
6773 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6774 {
6775 tree arg;
6776 tree parm;
6777
6778 /* Get the Ith template parameter. */
6779 parm = TREE_VEC_ELT (parms, parm_idx);
6780
6781 if (parm == error_mark_node)
6782 {
6783 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6784 continue;
6785 }
6786
6787 /* Calculate the next argument. */
6788 if (arg_idx < nargs)
6789 arg = TREE_VEC_ELT (inner_args, arg_idx);
6790 else
6791 arg = NULL_TREE;
6792
6793 if (template_parameter_pack_p (TREE_VALUE (parm))
6794 && !(arg && ARGUMENT_PACK_P (arg)))
6795 {
6796 /* All remaining arguments will be placed in the
6797 template parameter pack PARM. */
6798 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6799 inner_args, arg_idx,
6800 new_args, &lost,
6801 in_decl, complain);
6802
6803 /* Store this argument. */
6804 if (arg == error_mark_node)
6805 lost++;
6806 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6807
6808 /* We are done with all of the arguments. */
6809 arg_idx = nargs;
6810
6811 continue;
6812 }
6813 else if (arg)
6814 {
6815 if (PACK_EXPANSION_P (arg))
6816 {
6817 /* We don't know how many args we have yet, just
6818 use the unconverted ones for now. */
6819 new_inner_args = inner_args;
6820 break;
6821 }
6822 }
6823 else if (require_all_args)
6824 {
6825 /* There must be a default arg in this case. */
6826 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6827 complain, in_decl);
6828 /* The position of the first default template argument,
6829 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6830 Record that. */
6831 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6832 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6833 }
6834 else
6835 break;
6836
6837 if (arg == error_mark_node)
6838 {
6839 if (complain & tf_error)
6840 error ("template argument %d is invalid", arg_idx + 1);
6841 }
6842 else if (!arg)
6843 /* This only occurs if there was an error in the template
6844 parameter list itself (which we would already have
6845 reported) that we are trying to recover from, e.g., a class
6846 template with a parameter list such as
6847 template<typename..., typename>. */
6848 ++lost;
6849 else
6850 arg = convert_template_argument (TREE_VALUE (parm),
6851 arg, new_args, complain,
6852 parm_idx, in_decl);
6853
6854 if (arg == error_mark_node)
6855 lost++;
6856 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6857 }
6858 cp_unevaluated_operand = saved_unevaluated_operand;
6859 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6860
6861 if (lost)
6862 return error_mark_node;
6863
6864 #ifdef ENABLE_CHECKING
6865 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6866 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6867 TREE_VEC_LENGTH (new_inner_args));
6868 #endif
6869
6870 return new_inner_args;
6871 }
6872
6873 /* Like coerce_template_parms. If PARMS represents all template
6874 parameters levels, this function returns a vector of vectors
6875 representing all the resulting argument levels. Note that in this
6876 case, only the innermost arguments are coerced because the
6877 outermost ones are supposed to have been coerced already.
6878
6879 Otherwise, if PARMS represents only (the innermost) vector of
6880 parameters, this function returns a vector containing just the
6881 innermost resulting arguments. */
6882
6883 static tree
6884 coerce_innermost_template_parms (tree parms,
6885 tree args,
6886 tree in_decl,
6887 tsubst_flags_t complain,
6888 bool require_all_args,
6889 bool use_default_args)
6890 {
6891 int parms_depth = TMPL_PARMS_DEPTH (parms);
6892 int args_depth = TMPL_ARGS_DEPTH (args);
6893 tree coerced_args;
6894
6895 if (parms_depth > 1)
6896 {
6897 coerced_args = make_tree_vec (parms_depth);
6898 tree level;
6899 int cur_depth;
6900
6901 for (level = parms, cur_depth = parms_depth;
6902 parms_depth > 0 && level != NULL_TREE;
6903 level = TREE_CHAIN (level), --cur_depth)
6904 {
6905 tree l;
6906 if (cur_depth == args_depth)
6907 l = coerce_template_parms (TREE_VALUE (level),
6908 args, in_decl, complain,
6909 require_all_args,
6910 use_default_args);
6911 else
6912 l = TMPL_ARGS_LEVEL (args, cur_depth);
6913
6914 if (l == error_mark_node)
6915 return error_mark_node;
6916
6917 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6918 }
6919 }
6920 else
6921 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6922 args, in_decl, complain,
6923 require_all_args,
6924 use_default_args);
6925 return coerced_args;
6926 }
6927
6928 /* Returns 1 if template args OT and NT are equivalent. */
6929
6930 static int
6931 template_args_equal (tree ot, tree nt)
6932 {
6933 if (nt == ot)
6934 return 1;
6935 if (nt == NULL_TREE || ot == NULL_TREE)
6936 return false;
6937
6938 if (TREE_CODE (nt) == TREE_VEC)
6939 /* For member templates */
6940 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6941 else if (PACK_EXPANSION_P (ot))
6942 return (PACK_EXPANSION_P (nt)
6943 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6944 PACK_EXPANSION_PATTERN (nt))
6945 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6946 PACK_EXPANSION_EXTRA_ARGS (nt)));
6947 else if (ARGUMENT_PACK_P (ot))
6948 {
6949 int i, len;
6950 tree opack, npack;
6951
6952 if (!ARGUMENT_PACK_P (nt))
6953 return 0;
6954
6955 opack = ARGUMENT_PACK_ARGS (ot);
6956 npack = ARGUMENT_PACK_ARGS (nt);
6957 len = TREE_VEC_LENGTH (opack);
6958 if (TREE_VEC_LENGTH (npack) != len)
6959 return 0;
6960 for (i = 0; i < len; ++i)
6961 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6962 TREE_VEC_ELT (npack, i)))
6963 return 0;
6964 return 1;
6965 }
6966 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6967 {
6968 /* We get here probably because we are in the middle of substituting
6969 into the pattern of a pack expansion. In that case the
6970 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6971 interested in. So we want to use the initial pack argument for
6972 the comparison. */
6973 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6974 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6975 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6976 return template_args_equal (ot, nt);
6977 }
6978 else if (TYPE_P (nt))
6979 return TYPE_P (ot) && same_type_p (ot, nt);
6980 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6981 return 0;
6982 else
6983 return cp_tree_equal (ot, nt);
6984 }
6985
6986 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6987 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6988 NEWARG_PTR with the offending arguments if they are non-NULL. */
6989
6990 static int
6991 comp_template_args_with_info (tree oldargs, tree newargs,
6992 tree *oldarg_ptr, tree *newarg_ptr)
6993 {
6994 int i;
6995
6996 if (oldargs == newargs)
6997 return 1;
6998
6999 if (!oldargs || !newargs)
7000 return 0;
7001
7002 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7003 return 0;
7004
7005 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7006 {
7007 tree nt = TREE_VEC_ELT (newargs, i);
7008 tree ot = TREE_VEC_ELT (oldargs, i);
7009
7010 if (! template_args_equal (ot, nt))
7011 {
7012 if (oldarg_ptr != NULL)
7013 *oldarg_ptr = ot;
7014 if (newarg_ptr != NULL)
7015 *newarg_ptr = nt;
7016 return 0;
7017 }
7018 }
7019 return 1;
7020 }
7021
7022 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7023 of template arguments. Returns 0 otherwise. */
7024
7025 int
7026 comp_template_args (tree oldargs, tree newargs)
7027 {
7028 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7029 }
7030
7031 static void
7032 add_pending_template (tree d)
7033 {
7034 tree ti = (TYPE_P (d)
7035 ? CLASSTYPE_TEMPLATE_INFO (d)
7036 : DECL_TEMPLATE_INFO (d));
7037 struct pending_template *pt;
7038 int level;
7039
7040 if (TI_PENDING_TEMPLATE_FLAG (ti))
7041 return;
7042
7043 /* We are called both from instantiate_decl, where we've already had a
7044 tinst_level pushed, and instantiate_template, where we haven't.
7045 Compensate. */
7046 level = !current_tinst_level || current_tinst_level->decl != d;
7047
7048 if (level)
7049 push_tinst_level (d);
7050
7051 pt = ggc_alloc_pending_template ();
7052 pt->next = NULL;
7053 pt->tinst = current_tinst_level;
7054 if (last_pending_template)
7055 last_pending_template->next = pt;
7056 else
7057 pending_templates = pt;
7058
7059 last_pending_template = pt;
7060
7061 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7062
7063 if (level)
7064 pop_tinst_level ();
7065 }
7066
7067
7068 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7069 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7070 documentation for TEMPLATE_ID_EXPR. */
7071
7072 tree
7073 lookup_template_function (tree fns, tree arglist)
7074 {
7075 tree type;
7076
7077 if (fns == error_mark_node || arglist == error_mark_node)
7078 return error_mark_node;
7079
7080 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7081
7082 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7083 {
7084 error ("%q#D is not a function template", fns);
7085 return error_mark_node;
7086 }
7087
7088 if (BASELINK_P (fns))
7089 {
7090 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7091 unknown_type_node,
7092 BASELINK_FUNCTIONS (fns),
7093 arglist);
7094 return fns;
7095 }
7096
7097 type = TREE_TYPE (fns);
7098 if (TREE_CODE (fns) == OVERLOAD || !type)
7099 type = unknown_type_node;
7100
7101 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7102 }
7103
7104 /* Within the scope of a template class S<T>, the name S gets bound
7105 (in build_self_reference) to a TYPE_DECL for the class, not a
7106 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7107 or one of its enclosing classes, and that type is a template,
7108 return the associated TEMPLATE_DECL. Otherwise, the original
7109 DECL is returned.
7110
7111 Also handle the case when DECL is a TREE_LIST of ambiguous
7112 injected-class-names from different bases. */
7113
7114 tree
7115 maybe_get_template_decl_from_type_decl (tree decl)
7116 {
7117 if (decl == NULL_TREE)
7118 return decl;
7119
7120 /* DR 176: A lookup that finds an injected-class-name (10.2
7121 [class.member.lookup]) can result in an ambiguity in certain cases
7122 (for example, if it is found in more than one base class). If all of
7123 the injected-class-names that are found refer to specializations of
7124 the same class template, and if the name is followed by a
7125 template-argument-list, the reference refers to the class template
7126 itself and not a specialization thereof, and is not ambiguous. */
7127 if (TREE_CODE (decl) == TREE_LIST)
7128 {
7129 tree t, tmpl = NULL_TREE;
7130 for (t = decl; t; t = TREE_CHAIN (t))
7131 {
7132 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7133 if (!tmpl)
7134 tmpl = elt;
7135 else if (tmpl != elt)
7136 break;
7137 }
7138 if (tmpl && t == NULL_TREE)
7139 return tmpl;
7140 else
7141 return decl;
7142 }
7143
7144 return (decl != NULL_TREE
7145 && DECL_SELF_REFERENCE_P (decl)
7146 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7147 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7148 }
7149
7150 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7151 parameters, find the desired type.
7152
7153 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7154
7155 IN_DECL, if non-NULL, is the template declaration we are trying to
7156 instantiate.
7157
7158 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7159 the class we are looking up.
7160
7161 Issue error and warning messages under control of COMPLAIN.
7162
7163 If the template class is really a local class in a template
7164 function, then the FUNCTION_CONTEXT is the function in which it is
7165 being instantiated.
7166
7167 ??? Note that this function is currently called *twice* for each
7168 template-id: the first time from the parser, while creating the
7169 incomplete type (finish_template_type), and the second type during the
7170 real instantiation (instantiate_template_class). This is surely something
7171 that we want to avoid. It also causes some problems with argument
7172 coercion (see convert_nontype_argument for more information on this). */
7173
7174 static tree
7175 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7176 int entering_scope, tsubst_flags_t complain)
7177 {
7178 tree templ = NULL_TREE, parmlist;
7179 tree t;
7180 void **slot;
7181 spec_entry *entry;
7182 spec_entry elt;
7183 hashval_t hash;
7184
7185 if (identifier_p (d1))
7186 {
7187 tree value = innermost_non_namespace_value (d1);
7188 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7189 templ = value;
7190 else
7191 {
7192 if (context)
7193 push_decl_namespace (context);
7194 templ = lookup_name (d1);
7195 templ = maybe_get_template_decl_from_type_decl (templ);
7196 if (context)
7197 pop_decl_namespace ();
7198 }
7199 if (templ)
7200 context = DECL_CONTEXT (templ);
7201 }
7202 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7203 {
7204 tree type = TREE_TYPE (d1);
7205
7206 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7207 an implicit typename for the second A. Deal with it. */
7208 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7209 type = TREE_TYPE (type);
7210
7211 if (CLASSTYPE_TEMPLATE_INFO (type))
7212 {
7213 templ = CLASSTYPE_TI_TEMPLATE (type);
7214 d1 = DECL_NAME (templ);
7215 }
7216 }
7217 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7218 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7219 {
7220 templ = TYPE_TI_TEMPLATE (d1);
7221 d1 = DECL_NAME (templ);
7222 }
7223 else if (TREE_CODE (d1) == TEMPLATE_DECL
7224 && DECL_TEMPLATE_RESULT (d1)
7225 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7226 {
7227 templ = d1;
7228 d1 = DECL_NAME (templ);
7229 context = DECL_CONTEXT (templ);
7230 }
7231 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7232 {
7233 templ = d1;
7234 d1 = DECL_NAME (templ);
7235 }
7236
7237 /* Issue an error message if we didn't find a template. */
7238 if (! templ)
7239 {
7240 if (complain & tf_error)
7241 error ("%qT is not a template", d1);
7242 return error_mark_node;
7243 }
7244
7245 if (TREE_CODE (templ) != TEMPLATE_DECL
7246 /* Make sure it's a user visible template, if it was named by
7247 the user. */
7248 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7249 && !PRIMARY_TEMPLATE_P (templ)))
7250 {
7251 if (complain & tf_error)
7252 {
7253 error ("non-template type %qT used as a template", d1);
7254 if (in_decl)
7255 error ("for template declaration %q+D", in_decl);
7256 }
7257 return error_mark_node;
7258 }
7259
7260 complain &= ~tf_user;
7261
7262 /* An alias that just changes the name of a template is equivalent to the
7263 other template, so if any of the arguments are pack expansions, strip
7264 the alias to avoid problems with a pack expansion passed to a non-pack
7265 alias template parameter (DR 1430). */
7266 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7267 templ = get_underlying_template (templ);
7268
7269 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7270 {
7271 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7272 template arguments */
7273
7274 tree parm;
7275 tree arglist2;
7276 tree outer;
7277
7278 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7279
7280 /* Consider an example where a template template parameter declared as
7281
7282 template <class T, class U = std::allocator<T> > class TT
7283
7284 The template parameter level of T and U are one level larger than
7285 of TT. To proper process the default argument of U, say when an
7286 instantiation `TT<int>' is seen, we need to build the full
7287 arguments containing {int} as the innermost level. Outer levels,
7288 available when not appearing as default template argument, can be
7289 obtained from the arguments of the enclosing template.
7290
7291 Suppose that TT is later substituted with std::vector. The above
7292 instantiation is `TT<int, std::allocator<T> >' with TT at
7293 level 1, and T at level 2, while the template arguments at level 1
7294 becomes {std::vector} and the inner level 2 is {int}. */
7295
7296 outer = DECL_CONTEXT (templ);
7297 if (outer)
7298 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7299 else if (current_template_parms)
7300 /* This is an argument of the current template, so we haven't set
7301 DECL_CONTEXT yet. */
7302 outer = current_template_args ();
7303
7304 if (outer)
7305 arglist = add_to_template_args (outer, arglist);
7306
7307 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7308 complain,
7309 /*require_all_args=*/true,
7310 /*use_default_args=*/true);
7311 if (arglist2 == error_mark_node
7312 || (!uses_template_parms (arglist2)
7313 && check_instantiated_args (templ, arglist2, complain)))
7314 return error_mark_node;
7315
7316 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7317 return parm;
7318 }
7319 else
7320 {
7321 tree template_type = TREE_TYPE (templ);
7322 tree gen_tmpl;
7323 tree type_decl;
7324 tree found = NULL_TREE;
7325 int arg_depth;
7326 int parm_depth;
7327 int is_dependent_type;
7328 int use_partial_inst_tmpl = false;
7329
7330 if (template_type == error_mark_node)
7331 /* An error occurred while building the template TEMPL, and a
7332 diagnostic has most certainly been emitted for that
7333 already. Let's propagate that error. */
7334 return error_mark_node;
7335
7336 gen_tmpl = most_general_template (templ);
7337 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7338 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7339 arg_depth = TMPL_ARGS_DEPTH (arglist);
7340
7341 if (arg_depth == 1 && parm_depth > 1)
7342 {
7343 /* We've been given an incomplete set of template arguments.
7344 For example, given:
7345
7346 template <class T> struct S1 {
7347 template <class U> struct S2 {};
7348 template <class U> struct S2<U*> {};
7349 };
7350
7351 we will be called with an ARGLIST of `U*', but the
7352 TEMPLATE will be `template <class T> template
7353 <class U> struct S1<T>::S2'. We must fill in the missing
7354 arguments. */
7355 arglist
7356 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7357 arglist);
7358 arg_depth = TMPL_ARGS_DEPTH (arglist);
7359 }
7360
7361 /* Now we should have enough arguments. */
7362 gcc_assert (parm_depth == arg_depth);
7363
7364 /* From here on, we're only interested in the most general
7365 template. */
7366
7367 /* Calculate the BOUND_ARGS. These will be the args that are
7368 actually tsubst'd into the definition to create the
7369 instantiation. */
7370 if (parm_depth > 1)
7371 {
7372 /* We have multiple levels of arguments to coerce, at once. */
7373 int i;
7374 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7375
7376 tree bound_args = make_tree_vec (parm_depth);
7377
7378 for (i = saved_depth,
7379 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7380 i > 0 && t != NULL_TREE;
7381 --i, t = TREE_CHAIN (t))
7382 {
7383 tree a;
7384 if (i == saved_depth)
7385 a = coerce_template_parms (TREE_VALUE (t),
7386 arglist, gen_tmpl,
7387 complain,
7388 /*require_all_args=*/true,
7389 /*use_default_args=*/true);
7390 else
7391 /* Outer levels should have already been coerced. */
7392 a = TMPL_ARGS_LEVEL (arglist, i);
7393
7394 /* Don't process further if one of the levels fails. */
7395 if (a == error_mark_node)
7396 {
7397 /* Restore the ARGLIST to its full size. */
7398 TREE_VEC_LENGTH (arglist) = saved_depth;
7399 return error_mark_node;
7400 }
7401
7402 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7403
7404 /* We temporarily reduce the length of the ARGLIST so
7405 that coerce_template_parms will see only the arguments
7406 corresponding to the template parameters it is
7407 examining. */
7408 TREE_VEC_LENGTH (arglist)--;
7409 }
7410
7411 /* Restore the ARGLIST to its full size. */
7412 TREE_VEC_LENGTH (arglist) = saved_depth;
7413
7414 arglist = bound_args;
7415 }
7416 else
7417 arglist
7418 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7419 INNERMOST_TEMPLATE_ARGS (arglist),
7420 gen_tmpl,
7421 complain,
7422 /*require_all_args=*/true,
7423 /*use_default_args=*/true);
7424
7425 if (arglist == error_mark_node)
7426 /* We were unable to bind the arguments. */
7427 return error_mark_node;
7428
7429 /* In the scope of a template class, explicit references to the
7430 template class refer to the type of the template, not any
7431 instantiation of it. For example, in:
7432
7433 template <class T> class C { void f(C<T>); }
7434
7435 the `C<T>' is just the same as `C'. Outside of the
7436 class, however, such a reference is an instantiation. */
7437 if ((entering_scope
7438 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7439 || currently_open_class (template_type))
7440 /* comp_template_args is expensive, check it last. */
7441 && comp_template_args (TYPE_TI_ARGS (template_type),
7442 arglist))
7443 return template_type;
7444
7445 /* If we already have this specialization, return it. */
7446 elt.tmpl = gen_tmpl;
7447 elt.args = arglist;
7448 hash = hash_specialization (&elt);
7449 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7450 &elt, hash);
7451
7452 if (entry)
7453 return entry->spec;
7454
7455 is_dependent_type = uses_template_parms (arglist);
7456
7457 /* If the deduced arguments are invalid, then the binding
7458 failed. */
7459 if (!is_dependent_type
7460 && check_instantiated_args (gen_tmpl,
7461 INNERMOST_TEMPLATE_ARGS (arglist),
7462 complain))
7463 return error_mark_node;
7464
7465 if (!is_dependent_type
7466 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7467 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7468 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7469 {
7470 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7471 DECL_NAME (gen_tmpl),
7472 /*tag_scope=*/ts_global);
7473 return found;
7474 }
7475
7476 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7477 complain, in_decl);
7478 if (context == error_mark_node)
7479 return error_mark_node;
7480
7481 if (!context)
7482 context = global_namespace;
7483
7484 /* Create the type. */
7485 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7486 {
7487 /* The user referred to a specialization of an alias
7488 template represented by GEN_TMPL.
7489
7490 [temp.alias]/2 says:
7491
7492 When a template-id refers to the specialization of an
7493 alias template, it is equivalent to the associated
7494 type obtained by substitution of its
7495 template-arguments for the template-parameters in the
7496 type-id of the alias template. */
7497
7498 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7499 /* Note that the call above (by indirectly calling
7500 register_specialization in tsubst_decl) registers the
7501 TYPE_DECL representing the specialization of the alias
7502 template. So next time someone substitutes ARGLIST for
7503 the template parms into the alias template (GEN_TMPL),
7504 she'll get that TYPE_DECL back. */
7505
7506 if (t == error_mark_node)
7507 return t;
7508 }
7509 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7510 {
7511 if (!is_dependent_type)
7512 {
7513 set_current_access_from_decl (TYPE_NAME (template_type));
7514 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7515 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7516 arglist, complain, in_decl),
7517 SCOPED_ENUM_P (template_type), NULL);
7518 }
7519 else
7520 {
7521 /* We don't want to call start_enum for this type, since
7522 the values for the enumeration constants may involve
7523 template parameters. And, no one should be interested
7524 in the enumeration constants for such a type. */
7525 t = cxx_make_type (ENUMERAL_TYPE);
7526 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7527 }
7528 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7529 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7530 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7531 }
7532 else if (CLASS_TYPE_P (template_type))
7533 {
7534 t = make_class_type (TREE_CODE (template_type));
7535 CLASSTYPE_DECLARED_CLASS (t)
7536 = CLASSTYPE_DECLARED_CLASS (template_type);
7537 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7538 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7539
7540 /* A local class. Make sure the decl gets registered properly. */
7541 if (context == current_function_decl)
7542 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7543
7544 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7545 /* This instantiation is another name for the primary
7546 template type. Set the TYPE_CANONICAL field
7547 appropriately. */
7548 TYPE_CANONICAL (t) = template_type;
7549 else if (any_template_arguments_need_structural_equality_p (arglist))
7550 /* Some of the template arguments require structural
7551 equality testing, so this template class requires
7552 structural equality testing. */
7553 SET_TYPE_STRUCTURAL_EQUALITY (t);
7554 }
7555 else
7556 gcc_unreachable ();
7557
7558 /* If we called start_enum or pushtag above, this information
7559 will already be set up. */
7560 if (!TYPE_NAME (t))
7561 {
7562 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7563
7564 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7565 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7566 DECL_SOURCE_LOCATION (type_decl)
7567 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7568 }
7569 else
7570 type_decl = TYPE_NAME (t);
7571
7572 if (CLASS_TYPE_P (template_type))
7573 {
7574 TREE_PRIVATE (type_decl)
7575 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7576 TREE_PROTECTED (type_decl)
7577 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7578 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7579 {
7580 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7581 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7582 }
7583 }
7584
7585 /* Let's consider the explicit specialization of a member
7586 of a class template specialization that is implicitely instantiated,
7587 e.g.:
7588 template<class T>
7589 struct S
7590 {
7591 template<class U> struct M {}; //#0
7592 };
7593
7594 template<>
7595 template<>
7596 struct S<int>::M<char> //#1
7597 {
7598 int i;
7599 };
7600 [temp.expl.spec]/4 says this is valid.
7601
7602 In this case, when we write:
7603 S<int>::M<char> m;
7604
7605 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7606 the one of #0.
7607
7608 When we encounter #1, we want to store the partial instantiation
7609 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7610
7611 For all cases other than this "explicit specialization of member of a
7612 class template", we just want to store the most general template into
7613 the CLASSTYPE_TI_TEMPLATE of M.
7614
7615 This case of "explicit specialization of member of a class template"
7616 only happens when:
7617 1/ the enclosing class is an instantiation of, and therefore not
7618 the same as, the context of the most general template, and
7619 2/ we aren't looking at the partial instantiation itself, i.e.
7620 the innermost arguments are not the same as the innermost parms of
7621 the most general template.
7622
7623 So it's only when 1/ and 2/ happens that we want to use the partial
7624 instantiation of the member template in lieu of its most general
7625 template. */
7626
7627 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7628 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7629 /* the enclosing class must be an instantiation... */
7630 && CLASS_TYPE_P (context)
7631 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7632 {
7633 tree partial_inst_args;
7634 TREE_VEC_LENGTH (arglist)--;
7635 ++processing_template_decl;
7636 partial_inst_args =
7637 tsubst (INNERMOST_TEMPLATE_ARGS
7638 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7639 arglist, complain, NULL_TREE);
7640 --processing_template_decl;
7641 TREE_VEC_LENGTH (arglist)++;
7642 use_partial_inst_tmpl =
7643 /*...and we must not be looking at the partial instantiation
7644 itself. */
7645 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7646 partial_inst_args);
7647 }
7648
7649 if (!use_partial_inst_tmpl)
7650 /* This case is easy; there are no member templates involved. */
7651 found = gen_tmpl;
7652 else
7653 {
7654 /* This is a full instantiation of a member template. Find
7655 the partial instantiation of which this is an instance. */
7656
7657 /* Temporarily reduce by one the number of levels in the ARGLIST
7658 so as to avoid comparing the last set of arguments. */
7659 TREE_VEC_LENGTH (arglist)--;
7660 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7661 TREE_VEC_LENGTH (arglist)++;
7662 /* FOUND is either a proper class type, or an alias
7663 template specialization. In the later case, it's a
7664 TYPE_DECL, resulting from the substituting of arguments
7665 for parameters in the TYPE_DECL of the alias template
7666 done earlier. So be careful while getting the template
7667 of FOUND. */
7668 found = TREE_CODE (found) == TYPE_DECL
7669 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7670 : CLASSTYPE_TI_TEMPLATE (found);
7671 }
7672
7673 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7674
7675 elt.spec = t;
7676 slot = htab_find_slot_with_hash (type_specializations,
7677 &elt, hash, INSERT);
7678 entry = ggc_alloc_spec_entry ();
7679 *entry = elt;
7680 *slot = entry;
7681
7682 /* Note this use of the partial instantiation so we can check it
7683 later in maybe_process_partial_specialization. */
7684 DECL_TEMPLATE_INSTANTIATIONS (templ)
7685 = tree_cons (arglist, t,
7686 DECL_TEMPLATE_INSTANTIATIONS (templ));
7687
7688 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7689 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7690 /* Now that the type has been registered on the instantiations
7691 list, we set up the enumerators. Because the enumeration
7692 constants may involve the enumeration type itself, we make
7693 sure to register the type first, and then create the
7694 constants. That way, doing tsubst_expr for the enumeration
7695 constants won't result in recursive calls here; we'll find
7696 the instantiation and exit above. */
7697 tsubst_enum (template_type, t, arglist);
7698
7699 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7700 /* If the type makes use of template parameters, the
7701 code that generates debugging information will crash. */
7702 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7703
7704 /* Possibly limit visibility based on template args. */
7705 TREE_PUBLIC (type_decl) = 1;
7706 determine_visibility (type_decl);
7707
7708 return t;
7709 }
7710 }
7711
7712 /* Wrapper for lookup_template_class_1. */
7713
7714 tree
7715 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7716 int entering_scope, tsubst_flags_t complain)
7717 {
7718 tree ret;
7719 timevar_push (TV_TEMPLATE_INST);
7720 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7721 entering_scope, complain);
7722 timevar_pop (TV_TEMPLATE_INST);
7723 return ret;
7724 }
7725 \f
7726 struct pair_fn_data
7727 {
7728 tree_fn_t fn;
7729 void *data;
7730 /* True when we should also visit template parameters that occur in
7731 non-deduced contexts. */
7732 bool include_nondeduced_p;
7733 struct pointer_set_t *visited;
7734 };
7735
7736 /* Called from for_each_template_parm via walk_tree. */
7737
7738 static tree
7739 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7740 {
7741 tree t = *tp;
7742 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7743 tree_fn_t fn = pfd->fn;
7744 void *data = pfd->data;
7745
7746 if (TYPE_P (t)
7747 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7748 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7749 pfd->include_nondeduced_p))
7750 return error_mark_node;
7751
7752 switch (TREE_CODE (t))
7753 {
7754 case RECORD_TYPE:
7755 if (TYPE_PTRMEMFUNC_P (t))
7756 break;
7757 /* Fall through. */
7758
7759 case UNION_TYPE:
7760 case ENUMERAL_TYPE:
7761 if (!TYPE_TEMPLATE_INFO (t))
7762 *walk_subtrees = 0;
7763 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7764 fn, data, pfd->visited,
7765 pfd->include_nondeduced_p))
7766 return error_mark_node;
7767 break;
7768
7769 case INTEGER_TYPE:
7770 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7771 fn, data, pfd->visited,
7772 pfd->include_nondeduced_p)
7773 || for_each_template_parm (TYPE_MAX_VALUE (t),
7774 fn, data, pfd->visited,
7775 pfd->include_nondeduced_p))
7776 return error_mark_node;
7777 break;
7778
7779 case METHOD_TYPE:
7780 /* Since we're not going to walk subtrees, we have to do this
7781 explicitly here. */
7782 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7783 pfd->visited, pfd->include_nondeduced_p))
7784 return error_mark_node;
7785 /* Fall through. */
7786
7787 case FUNCTION_TYPE:
7788 /* Check the return type. */
7789 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7790 pfd->include_nondeduced_p))
7791 return error_mark_node;
7792
7793 /* Check the parameter types. Since default arguments are not
7794 instantiated until they are needed, the TYPE_ARG_TYPES may
7795 contain expressions that involve template parameters. But,
7796 no-one should be looking at them yet. And, once they're
7797 instantiated, they don't contain template parameters, so
7798 there's no point in looking at them then, either. */
7799 {
7800 tree parm;
7801
7802 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7803 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7804 pfd->visited, pfd->include_nondeduced_p))
7805 return error_mark_node;
7806
7807 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7808 want walk_tree walking into them itself. */
7809 *walk_subtrees = 0;
7810 }
7811 break;
7812
7813 case TYPEOF_TYPE:
7814 case UNDERLYING_TYPE:
7815 if (pfd->include_nondeduced_p
7816 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7817 pfd->visited,
7818 pfd->include_nondeduced_p))
7819 return error_mark_node;
7820 break;
7821
7822 case FUNCTION_DECL:
7823 case VAR_DECL:
7824 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7825 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7826 pfd->visited, pfd->include_nondeduced_p))
7827 return error_mark_node;
7828 /* Fall through. */
7829
7830 case PARM_DECL:
7831 case CONST_DECL:
7832 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7833 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7834 pfd->visited, pfd->include_nondeduced_p))
7835 return error_mark_node;
7836 if (DECL_CONTEXT (t)
7837 && pfd->include_nondeduced_p
7838 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7839 pfd->visited, pfd->include_nondeduced_p))
7840 return error_mark_node;
7841 break;
7842
7843 case BOUND_TEMPLATE_TEMPLATE_PARM:
7844 /* Record template parameters such as `T' inside `TT<T>'. */
7845 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7846 pfd->include_nondeduced_p))
7847 return error_mark_node;
7848 /* Fall through. */
7849
7850 case TEMPLATE_TEMPLATE_PARM:
7851 case TEMPLATE_TYPE_PARM:
7852 case TEMPLATE_PARM_INDEX:
7853 if (fn && (*fn)(t, data))
7854 return error_mark_node;
7855 else if (!fn)
7856 return error_mark_node;
7857 break;
7858
7859 case TEMPLATE_DECL:
7860 /* A template template parameter is encountered. */
7861 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7862 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7863 pfd->include_nondeduced_p))
7864 return error_mark_node;
7865
7866 /* Already substituted template template parameter */
7867 *walk_subtrees = 0;
7868 break;
7869
7870 case TYPENAME_TYPE:
7871 if (!fn
7872 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7873 data, pfd->visited,
7874 pfd->include_nondeduced_p))
7875 return error_mark_node;
7876 break;
7877
7878 case CONSTRUCTOR:
7879 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7880 && pfd->include_nondeduced_p
7881 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7882 (TREE_TYPE (t)), fn, data,
7883 pfd->visited, pfd->include_nondeduced_p))
7884 return error_mark_node;
7885 break;
7886
7887 case INDIRECT_REF:
7888 case COMPONENT_REF:
7889 /* If there's no type, then this thing must be some expression
7890 involving template parameters. */
7891 if (!fn && !TREE_TYPE (t))
7892 return error_mark_node;
7893 break;
7894
7895 case MODOP_EXPR:
7896 case CAST_EXPR:
7897 case IMPLICIT_CONV_EXPR:
7898 case REINTERPRET_CAST_EXPR:
7899 case CONST_CAST_EXPR:
7900 case STATIC_CAST_EXPR:
7901 case DYNAMIC_CAST_EXPR:
7902 case ARROW_EXPR:
7903 case DOTSTAR_EXPR:
7904 case TYPEID_EXPR:
7905 case PSEUDO_DTOR_EXPR:
7906 if (!fn)
7907 return error_mark_node;
7908 break;
7909
7910 default:
7911 break;
7912 }
7913
7914 /* We didn't find any template parameters we liked. */
7915 return NULL_TREE;
7916 }
7917
7918 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7919 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7920 call FN with the parameter and the DATA.
7921 If FN returns nonzero, the iteration is terminated, and
7922 for_each_template_parm returns 1. Otherwise, the iteration
7923 continues. If FN never returns a nonzero value, the value
7924 returned by for_each_template_parm is 0. If FN is NULL, it is
7925 considered to be the function which always returns 1.
7926
7927 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7928 parameters that occur in non-deduced contexts. When false, only
7929 visits those template parameters that can be deduced. */
7930
7931 static int
7932 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7933 struct pointer_set_t *visited,
7934 bool include_nondeduced_p)
7935 {
7936 struct pair_fn_data pfd;
7937 int result;
7938
7939 /* Set up. */
7940 pfd.fn = fn;
7941 pfd.data = data;
7942 pfd.include_nondeduced_p = include_nondeduced_p;
7943
7944 /* Walk the tree. (Conceptually, we would like to walk without
7945 duplicates, but for_each_template_parm_r recursively calls
7946 for_each_template_parm, so we would need to reorganize a fair
7947 bit to use walk_tree_without_duplicates, so we keep our own
7948 visited list.) */
7949 if (visited)
7950 pfd.visited = visited;
7951 else
7952 pfd.visited = pointer_set_create ();
7953 result = cp_walk_tree (&t,
7954 for_each_template_parm_r,
7955 &pfd,
7956 pfd.visited) != NULL_TREE;
7957
7958 /* Clean up. */
7959 if (!visited)
7960 {
7961 pointer_set_destroy (pfd.visited);
7962 pfd.visited = 0;
7963 }
7964
7965 return result;
7966 }
7967
7968 /* Returns true if T depends on any template parameter. */
7969
7970 int
7971 uses_template_parms (tree t)
7972 {
7973 bool dependent_p;
7974 int saved_processing_template_decl;
7975
7976 saved_processing_template_decl = processing_template_decl;
7977 if (!saved_processing_template_decl)
7978 processing_template_decl = 1;
7979 if (TYPE_P (t))
7980 dependent_p = dependent_type_p (t);
7981 else if (TREE_CODE (t) == TREE_VEC)
7982 dependent_p = any_dependent_template_arguments_p (t);
7983 else if (TREE_CODE (t) == TREE_LIST)
7984 dependent_p = (uses_template_parms (TREE_VALUE (t))
7985 || uses_template_parms (TREE_CHAIN (t)));
7986 else if (TREE_CODE (t) == TYPE_DECL)
7987 dependent_p = dependent_type_p (TREE_TYPE (t));
7988 else if (DECL_P (t)
7989 || EXPR_P (t)
7990 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7991 || TREE_CODE (t) == OVERLOAD
7992 || BASELINK_P (t)
7993 || identifier_p (t)
7994 || TREE_CODE (t) == TRAIT_EXPR
7995 || TREE_CODE (t) == CONSTRUCTOR
7996 || CONSTANT_CLASS_P (t))
7997 dependent_p = (type_dependent_expression_p (t)
7998 || value_dependent_expression_p (t));
7999 else
8000 {
8001 gcc_assert (t == error_mark_node);
8002 dependent_p = false;
8003 }
8004
8005 processing_template_decl = saved_processing_template_decl;
8006
8007 return dependent_p;
8008 }
8009
8010 /* Returns true iff current_function_decl is an incompletely instantiated
8011 template. Useful instead of processing_template_decl because the latter
8012 is set to 0 during fold_non_dependent_expr. */
8013
8014 bool
8015 in_template_function (void)
8016 {
8017 tree fn = current_function_decl;
8018 bool ret;
8019 ++processing_template_decl;
8020 ret = (fn && DECL_LANG_SPECIFIC (fn)
8021 && DECL_TEMPLATE_INFO (fn)
8022 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8023 --processing_template_decl;
8024 return ret;
8025 }
8026
8027 /* Returns true if T depends on any template parameter with level LEVEL. */
8028
8029 int
8030 uses_template_parms_level (tree t, int level)
8031 {
8032 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8033 /*include_nondeduced_p=*/true);
8034 }
8035
8036 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8037 ill-formed translation unit, i.e. a variable or function that isn't
8038 usable in a constant expression. */
8039
8040 static inline bool
8041 neglectable_inst_p (tree d)
8042 {
8043 return (DECL_P (d)
8044 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8045 : decl_maybe_constant_var_p (d)));
8046 }
8047
8048 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8049 neglectable and instantiated from within an erroneous instantiation. */
8050
8051 static bool
8052 limit_bad_template_recursion (tree decl)
8053 {
8054 struct tinst_level *lev = current_tinst_level;
8055 int errs = errorcount + sorrycount;
8056 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8057 return false;
8058
8059 for (; lev; lev = lev->next)
8060 if (neglectable_inst_p (lev->decl))
8061 break;
8062
8063 return (lev && errs > lev->errors);
8064 }
8065
8066 static int tinst_depth;
8067 extern int max_tinst_depth;
8068 int depth_reached;
8069
8070 static GTY(()) struct tinst_level *last_error_tinst_level;
8071
8072 /* We're starting to instantiate D; record the template instantiation context
8073 for diagnostics and to restore it later. */
8074
8075 int
8076 push_tinst_level (tree d)
8077 {
8078 struct tinst_level *new_level;
8079
8080 if (tinst_depth >= max_tinst_depth)
8081 {
8082 last_error_tinst_level = current_tinst_level;
8083 if (TREE_CODE (d) == TREE_LIST)
8084 error ("template instantiation depth exceeds maximum of %d (use "
8085 "-ftemplate-depth= to increase the maximum) substituting %qS",
8086 max_tinst_depth, d);
8087 else
8088 error ("template instantiation depth exceeds maximum of %d (use "
8089 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8090 max_tinst_depth, d);
8091
8092 print_instantiation_context ();
8093
8094 return 0;
8095 }
8096
8097 /* If the current instantiation caused problems, don't let it instantiate
8098 anything else. Do allow deduction substitution and decls usable in
8099 constant expressions. */
8100 if (limit_bad_template_recursion (d))
8101 return 0;
8102
8103 new_level = ggc_alloc_tinst_level ();
8104 new_level->decl = d;
8105 new_level->locus = input_location;
8106 new_level->errors = errorcount+sorrycount;
8107 new_level->in_system_header_p = in_system_header_at (input_location);
8108 new_level->next = current_tinst_level;
8109 current_tinst_level = new_level;
8110
8111 ++tinst_depth;
8112 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8113 depth_reached = tinst_depth;
8114
8115 return 1;
8116 }
8117
8118 /* We're done instantiating this template; return to the instantiation
8119 context. */
8120
8121 void
8122 pop_tinst_level (void)
8123 {
8124 /* Restore the filename and line number stashed away when we started
8125 this instantiation. */
8126 input_location = current_tinst_level->locus;
8127 current_tinst_level = current_tinst_level->next;
8128 --tinst_depth;
8129 }
8130
8131 /* We're instantiating a deferred template; restore the template
8132 instantiation context in which the instantiation was requested, which
8133 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8134
8135 static tree
8136 reopen_tinst_level (struct tinst_level *level)
8137 {
8138 struct tinst_level *t;
8139
8140 tinst_depth = 0;
8141 for (t = level; t; t = t->next)
8142 ++tinst_depth;
8143
8144 current_tinst_level = level;
8145 pop_tinst_level ();
8146 if (current_tinst_level)
8147 current_tinst_level->errors = errorcount+sorrycount;
8148 return level->decl;
8149 }
8150
8151 /* Returns the TINST_LEVEL which gives the original instantiation
8152 context. */
8153
8154 struct tinst_level *
8155 outermost_tinst_level (void)
8156 {
8157 struct tinst_level *level = current_tinst_level;
8158 if (level)
8159 while (level->next)
8160 level = level->next;
8161 return level;
8162 }
8163
8164 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8165 vector of template arguments, as for tsubst.
8166
8167 Returns an appropriate tsubst'd friend declaration. */
8168
8169 static tree
8170 tsubst_friend_function (tree decl, tree args)
8171 {
8172 tree new_friend;
8173
8174 if (TREE_CODE (decl) == FUNCTION_DECL
8175 && DECL_TEMPLATE_INSTANTIATION (decl)
8176 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8177 /* This was a friend declared with an explicit template
8178 argument list, e.g.:
8179
8180 friend void f<>(T);
8181
8182 to indicate that f was a template instantiation, not a new
8183 function declaration. Now, we have to figure out what
8184 instantiation of what template. */
8185 {
8186 tree template_id, arglist, fns;
8187 tree new_args;
8188 tree tmpl;
8189 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8190
8191 /* Friend functions are looked up in the containing namespace scope.
8192 We must enter that scope, to avoid finding member functions of the
8193 current class with same name. */
8194 push_nested_namespace (ns);
8195 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8196 tf_warning_or_error, NULL_TREE,
8197 /*integral_constant_expression_p=*/false);
8198 pop_nested_namespace (ns);
8199 arglist = tsubst (DECL_TI_ARGS (decl), args,
8200 tf_warning_or_error, NULL_TREE);
8201 template_id = lookup_template_function (fns, arglist);
8202
8203 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8204 tmpl = determine_specialization (template_id, new_friend,
8205 &new_args,
8206 /*need_member_template=*/0,
8207 TREE_VEC_LENGTH (args),
8208 tsk_none);
8209 return instantiate_template (tmpl, new_args, tf_error);
8210 }
8211
8212 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8213
8214 /* The NEW_FRIEND will look like an instantiation, to the
8215 compiler, but is not an instantiation from the point of view of
8216 the language. For example, we might have had:
8217
8218 template <class T> struct S {
8219 template <class U> friend void f(T, U);
8220 };
8221
8222 Then, in S<int>, template <class U> void f(int, U) is not an
8223 instantiation of anything. */
8224 if (new_friend == error_mark_node)
8225 return error_mark_node;
8226
8227 DECL_USE_TEMPLATE (new_friend) = 0;
8228 if (TREE_CODE (decl) == TEMPLATE_DECL)
8229 {
8230 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8231 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8232 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8233 }
8234
8235 /* The mangled name for the NEW_FRIEND is incorrect. The function
8236 is not a template instantiation and should not be mangled like
8237 one. Therefore, we forget the mangling here; we'll recompute it
8238 later if we need it. */
8239 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8240 {
8241 SET_DECL_RTL (new_friend, NULL);
8242 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8243 }
8244
8245 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8246 {
8247 tree old_decl;
8248 tree new_friend_template_info;
8249 tree new_friend_result_template_info;
8250 tree ns;
8251 int new_friend_is_defn;
8252
8253 /* We must save some information from NEW_FRIEND before calling
8254 duplicate decls since that function will free NEW_FRIEND if
8255 possible. */
8256 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8257 new_friend_is_defn =
8258 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8259 (template_for_substitution (new_friend)))
8260 != NULL_TREE);
8261 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8262 {
8263 /* This declaration is a `primary' template. */
8264 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8265
8266 new_friend_result_template_info
8267 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8268 }
8269 else
8270 new_friend_result_template_info = NULL_TREE;
8271
8272 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8273 if (new_friend_is_defn)
8274 DECL_INITIAL (new_friend) = error_mark_node;
8275
8276 /* Inside pushdecl_namespace_level, we will push into the
8277 current namespace. However, the friend function should go
8278 into the namespace of the template. */
8279 ns = decl_namespace_context (new_friend);
8280 push_nested_namespace (ns);
8281 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8282 pop_nested_namespace (ns);
8283
8284 if (old_decl == error_mark_node)
8285 return error_mark_node;
8286
8287 if (old_decl != new_friend)
8288 {
8289 /* This new friend declaration matched an existing
8290 declaration. For example, given:
8291
8292 template <class T> void f(T);
8293 template <class U> class C {
8294 template <class T> friend void f(T) {}
8295 };
8296
8297 the friend declaration actually provides the definition
8298 of `f', once C has been instantiated for some type. So,
8299 old_decl will be the out-of-class template declaration,
8300 while new_friend is the in-class definition.
8301
8302 But, if `f' was called before this point, the
8303 instantiation of `f' will have DECL_TI_ARGS corresponding
8304 to `T' but not to `U', references to which might appear
8305 in the definition of `f'. Previously, the most general
8306 template for an instantiation of `f' was the out-of-class
8307 version; now it is the in-class version. Therefore, we
8308 run through all specialization of `f', adding to their
8309 DECL_TI_ARGS appropriately. In particular, they need a
8310 new set of outer arguments, corresponding to the
8311 arguments for this class instantiation.
8312
8313 The same situation can arise with something like this:
8314
8315 friend void f(int);
8316 template <class T> class C {
8317 friend void f(T) {}
8318 };
8319
8320 when `C<int>' is instantiated. Now, `f(int)' is defined
8321 in the class. */
8322
8323 if (!new_friend_is_defn)
8324 /* On the other hand, if the in-class declaration does
8325 *not* provide a definition, then we don't want to alter
8326 existing definitions. We can just leave everything
8327 alone. */
8328 ;
8329 else
8330 {
8331 tree new_template = TI_TEMPLATE (new_friend_template_info);
8332 tree new_args = TI_ARGS (new_friend_template_info);
8333
8334 /* Overwrite whatever template info was there before, if
8335 any, with the new template information pertaining to
8336 the declaration. */
8337 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8338
8339 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8340 {
8341 /* We should have called reregister_specialization in
8342 duplicate_decls. */
8343 gcc_assert (retrieve_specialization (new_template,
8344 new_args, 0)
8345 == old_decl);
8346
8347 /* Instantiate it if the global has already been used. */
8348 if (DECL_ODR_USED (old_decl))
8349 instantiate_decl (old_decl, /*defer_ok=*/true,
8350 /*expl_inst_class_mem_p=*/false);
8351 }
8352 else
8353 {
8354 tree t;
8355
8356 /* Indicate that the old function template is a partial
8357 instantiation. */
8358 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8359 = new_friend_result_template_info;
8360
8361 gcc_assert (new_template
8362 == most_general_template (new_template));
8363 gcc_assert (new_template != old_decl);
8364
8365 /* Reassign any specializations already in the hash table
8366 to the new more general template, and add the
8367 additional template args. */
8368 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8369 t != NULL_TREE;
8370 t = TREE_CHAIN (t))
8371 {
8372 tree spec = TREE_VALUE (t);
8373 spec_entry elt;
8374
8375 elt.tmpl = old_decl;
8376 elt.args = DECL_TI_ARGS (spec);
8377 elt.spec = NULL_TREE;
8378
8379 htab_remove_elt (decl_specializations, &elt);
8380
8381 DECL_TI_ARGS (spec)
8382 = add_outermost_template_args (new_args,
8383 DECL_TI_ARGS (spec));
8384
8385 register_specialization
8386 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8387
8388 }
8389 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8390 }
8391 }
8392
8393 /* The information from NEW_FRIEND has been merged into OLD_DECL
8394 by duplicate_decls. */
8395 new_friend = old_decl;
8396 }
8397 }
8398 else
8399 {
8400 tree context = DECL_CONTEXT (new_friend);
8401 bool dependent_p;
8402
8403 /* In the code
8404 template <class T> class C {
8405 template <class U> friend void C1<U>::f (); // case 1
8406 friend void C2<T>::f (); // case 2
8407 };
8408 we only need to make sure CONTEXT is a complete type for
8409 case 2. To distinguish between the two cases, we note that
8410 CONTEXT of case 1 remains dependent type after tsubst while
8411 this isn't true for case 2. */
8412 ++processing_template_decl;
8413 dependent_p = dependent_type_p (context);
8414 --processing_template_decl;
8415
8416 if (!dependent_p
8417 && !complete_type_or_else (context, NULL_TREE))
8418 return error_mark_node;
8419
8420 if (COMPLETE_TYPE_P (context))
8421 {
8422 /* Check to see that the declaration is really present, and,
8423 possibly obtain an improved declaration. */
8424 tree fn = check_classfn (context,
8425 new_friend, NULL_TREE);
8426
8427 if (fn)
8428 new_friend = fn;
8429 }
8430 }
8431
8432 return new_friend;
8433 }
8434
8435 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8436 template arguments, as for tsubst.
8437
8438 Returns an appropriate tsubst'd friend type or error_mark_node on
8439 failure. */
8440
8441 static tree
8442 tsubst_friend_class (tree friend_tmpl, tree args)
8443 {
8444 tree friend_type;
8445 tree tmpl;
8446 tree context;
8447
8448 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8449 {
8450 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8451 return TREE_TYPE (t);
8452 }
8453
8454 context = CP_DECL_CONTEXT (friend_tmpl);
8455
8456 if (context != global_namespace)
8457 {
8458 if (TREE_CODE (context) == NAMESPACE_DECL)
8459 push_nested_namespace (context);
8460 else
8461 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8462 }
8463
8464 /* Look for a class template declaration. We look for hidden names
8465 because two friend declarations of the same template are the
8466 same. For example, in:
8467
8468 struct A {
8469 template <typename> friend class F;
8470 };
8471 template <typename> struct B {
8472 template <typename> friend class F;
8473 };
8474
8475 both F templates are the same. */
8476 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8477 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8478
8479 /* But, if we don't find one, it might be because we're in a
8480 situation like this:
8481
8482 template <class T>
8483 struct S {
8484 template <class U>
8485 friend struct S;
8486 };
8487
8488 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8489 for `S<int>', not the TEMPLATE_DECL. */
8490 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8491 {
8492 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8493 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8494 }
8495
8496 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8497 {
8498 /* The friend template has already been declared. Just
8499 check to see that the declarations match, and install any new
8500 default parameters. We must tsubst the default parameters,
8501 of course. We only need the innermost template parameters
8502 because that is all that redeclare_class_template will look
8503 at. */
8504 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8505 > TMPL_ARGS_DEPTH (args))
8506 {
8507 tree parms;
8508 location_t saved_input_location;
8509 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8510 args, tf_warning_or_error);
8511
8512 saved_input_location = input_location;
8513 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8514 redeclare_class_template (TREE_TYPE (tmpl), parms);
8515 input_location = saved_input_location;
8516
8517 }
8518
8519 friend_type = TREE_TYPE (tmpl);
8520 }
8521 else
8522 {
8523 /* The friend template has not already been declared. In this
8524 case, the instantiation of the template class will cause the
8525 injection of this template into the global scope. */
8526 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8527 if (tmpl == error_mark_node)
8528 return error_mark_node;
8529
8530 /* The new TMPL is not an instantiation of anything, so we
8531 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8532 the new type because that is supposed to be the corresponding
8533 template decl, i.e., TMPL. */
8534 DECL_USE_TEMPLATE (tmpl) = 0;
8535 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8536 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8537 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8538 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8539
8540 /* Inject this template into the global scope. */
8541 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8542 }
8543
8544 if (context != global_namespace)
8545 {
8546 if (TREE_CODE (context) == NAMESPACE_DECL)
8547 pop_nested_namespace (context);
8548 else
8549 pop_nested_class ();
8550 }
8551
8552 return friend_type;
8553 }
8554
8555 /* Returns zero if TYPE cannot be completed later due to circularity.
8556 Otherwise returns one. */
8557
8558 static int
8559 can_complete_type_without_circularity (tree type)
8560 {
8561 if (type == NULL_TREE || type == error_mark_node)
8562 return 0;
8563 else if (COMPLETE_TYPE_P (type))
8564 return 1;
8565 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8566 return can_complete_type_without_circularity (TREE_TYPE (type));
8567 else if (CLASS_TYPE_P (type)
8568 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8569 return 0;
8570 else
8571 return 1;
8572 }
8573
8574 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8575
8576 /* Apply any attributes which had to be deferred until instantiation
8577 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8578 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8579
8580 static void
8581 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8582 tree args, tsubst_flags_t complain, tree in_decl)
8583 {
8584 tree last_dep = NULL_TREE;
8585 tree t;
8586 tree *p;
8587
8588 for (t = attributes; t; t = TREE_CHAIN (t))
8589 if (ATTR_IS_DEPENDENT (t))
8590 {
8591 last_dep = t;
8592 attributes = copy_list (attributes);
8593 break;
8594 }
8595
8596 if (DECL_P (*decl_p))
8597 {
8598 if (TREE_TYPE (*decl_p) == error_mark_node)
8599 return;
8600 p = &DECL_ATTRIBUTES (*decl_p);
8601 }
8602 else
8603 p = &TYPE_ATTRIBUTES (*decl_p);
8604
8605 if (last_dep)
8606 {
8607 tree late_attrs = NULL_TREE;
8608 tree *q = &late_attrs;
8609
8610 for (*p = attributes; *p; )
8611 {
8612 t = *p;
8613 if (ATTR_IS_DEPENDENT (t))
8614 {
8615 *p = TREE_CHAIN (t);
8616 TREE_CHAIN (t) = NULL_TREE;
8617 if ((flag_openmp || flag_cilkplus)
8618 && is_attribute_p ("omp declare simd",
8619 get_attribute_name (t))
8620 && TREE_VALUE (t))
8621 {
8622 tree clauses = TREE_VALUE (TREE_VALUE (t));
8623 clauses = tsubst_omp_clauses (clauses, true, args,
8624 complain, in_decl);
8625 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8626 clauses = finish_omp_clauses (clauses);
8627 tree parms = DECL_ARGUMENTS (*decl_p);
8628 clauses
8629 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8630 if (clauses)
8631 TREE_VALUE (TREE_VALUE (t)) = clauses;
8632 else
8633 TREE_VALUE (t) = NULL_TREE;
8634 }
8635 /* If the first attribute argument is an identifier, don't
8636 pass it through tsubst. Attributes like mode, format,
8637 cleanup and several target specific attributes expect it
8638 unmodified. */
8639 else if (attribute_takes_identifier_p (get_attribute_name (t))
8640 && TREE_VALUE (t))
8641 {
8642 tree chain
8643 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8644 in_decl,
8645 /*integral_constant_expression_p=*/false);
8646 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8647 TREE_VALUE (t)
8648 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8649 chain);
8650 }
8651 else
8652 TREE_VALUE (t)
8653 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8654 /*integral_constant_expression_p=*/false);
8655 *q = t;
8656 q = &TREE_CHAIN (t);
8657 }
8658 else
8659 p = &TREE_CHAIN (t);
8660 }
8661
8662 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8663 }
8664 }
8665
8666 /* Perform (or defer) access check for typedefs that were referenced
8667 from within the template TMPL code.
8668 This is a subroutine of instantiate_decl and instantiate_class_template.
8669 TMPL is the template to consider and TARGS is the list of arguments of
8670 that template. */
8671
8672 static void
8673 perform_typedefs_access_check (tree tmpl, tree targs)
8674 {
8675 location_t saved_location;
8676 unsigned i;
8677 qualified_typedef_usage_t *iter;
8678
8679 if (!tmpl
8680 || (!CLASS_TYPE_P (tmpl)
8681 && TREE_CODE (tmpl) != FUNCTION_DECL))
8682 return;
8683
8684 saved_location = input_location;
8685 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8686 {
8687 tree type_decl = iter->typedef_decl;
8688 tree type_scope = iter->context;
8689
8690 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8691 continue;
8692
8693 if (uses_template_parms (type_decl))
8694 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8695 if (uses_template_parms (type_scope))
8696 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8697
8698 /* Make access check error messages point to the location
8699 of the use of the typedef. */
8700 input_location = iter->locus;
8701 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8702 type_decl, type_decl,
8703 tf_warning_or_error);
8704 }
8705 input_location = saved_location;
8706 }
8707
8708 static tree
8709 instantiate_class_template_1 (tree type)
8710 {
8711 tree templ, args, pattern, t, member;
8712 tree typedecl;
8713 tree pbinfo;
8714 tree base_list;
8715 unsigned int saved_maximum_field_alignment;
8716 tree fn_context;
8717
8718 if (type == error_mark_node)
8719 return error_mark_node;
8720
8721 if (COMPLETE_OR_OPEN_TYPE_P (type)
8722 || uses_template_parms (type))
8723 return type;
8724
8725 /* Figure out which template is being instantiated. */
8726 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8727 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8728
8729 /* Determine what specialization of the original template to
8730 instantiate. */
8731 t = most_specialized_class (type, tf_warning_or_error);
8732 if (t == error_mark_node)
8733 {
8734 TYPE_BEING_DEFINED (type) = 1;
8735 return error_mark_node;
8736 }
8737 else if (t)
8738 {
8739 /* This TYPE is actually an instantiation of a partial
8740 specialization. We replace the innermost set of ARGS with
8741 the arguments appropriate for substitution. For example,
8742 given:
8743
8744 template <class T> struct S {};
8745 template <class T> struct S<T*> {};
8746
8747 and supposing that we are instantiating S<int*>, ARGS will
8748 presently be {int*} -- but we need {int}. */
8749 pattern = TREE_TYPE (t);
8750 args = TREE_PURPOSE (t);
8751 }
8752 else
8753 {
8754 pattern = TREE_TYPE (templ);
8755 args = CLASSTYPE_TI_ARGS (type);
8756 }
8757
8758 /* If the template we're instantiating is incomplete, then clearly
8759 there's nothing we can do. */
8760 if (!COMPLETE_TYPE_P (pattern))
8761 return type;
8762
8763 /* If we've recursively instantiated too many templates, stop. */
8764 if (! push_tinst_level (type))
8765 return type;
8766
8767 /* Now we're really doing the instantiation. Mark the type as in
8768 the process of being defined. */
8769 TYPE_BEING_DEFINED (type) = 1;
8770
8771 /* We may be in the middle of deferred access check. Disable
8772 it now. */
8773 push_deferring_access_checks (dk_no_deferred);
8774
8775 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8776 if (!fn_context)
8777 push_to_top_level ();
8778 /* Use #pragma pack from the template context. */
8779 saved_maximum_field_alignment = maximum_field_alignment;
8780 maximum_field_alignment = TYPE_PRECISION (pattern);
8781
8782 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8783
8784 /* Set the input location to the most specialized template definition.
8785 This is needed if tsubsting causes an error. */
8786 typedecl = TYPE_MAIN_DECL (pattern);
8787 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8788 DECL_SOURCE_LOCATION (typedecl);
8789
8790 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8791 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8792 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8793 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8794 if (ANON_AGGR_TYPE_P (pattern))
8795 SET_ANON_AGGR_TYPE_P (type);
8796 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8797 {
8798 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8799 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8800 /* Adjust visibility for template arguments. */
8801 determine_visibility (TYPE_MAIN_DECL (type));
8802 }
8803 if (CLASS_TYPE_P (type))
8804 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8805
8806 pbinfo = TYPE_BINFO (pattern);
8807
8808 /* We should never instantiate a nested class before its enclosing
8809 class; we need to look up the nested class by name before we can
8810 instantiate it, and that lookup should instantiate the enclosing
8811 class. */
8812 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8813 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8814
8815 base_list = NULL_TREE;
8816 if (BINFO_N_BASE_BINFOS (pbinfo))
8817 {
8818 tree pbase_binfo;
8819 tree pushed_scope;
8820 int i;
8821
8822 /* We must enter the scope containing the type, as that is where
8823 the accessibility of types named in dependent bases are
8824 looked up from. */
8825 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8826
8827 /* Substitute into each of the bases to determine the actual
8828 basetypes. */
8829 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8830 {
8831 tree base;
8832 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8833 tree expanded_bases = NULL_TREE;
8834 int idx, len = 1;
8835
8836 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8837 {
8838 expanded_bases =
8839 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8840 args, tf_error, NULL_TREE);
8841 if (expanded_bases == error_mark_node)
8842 continue;
8843
8844 len = TREE_VEC_LENGTH (expanded_bases);
8845 }
8846
8847 for (idx = 0; idx < len; idx++)
8848 {
8849 if (expanded_bases)
8850 /* Extract the already-expanded base class. */
8851 base = TREE_VEC_ELT (expanded_bases, idx);
8852 else
8853 /* Substitute to figure out the base class. */
8854 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8855 NULL_TREE);
8856
8857 if (base == error_mark_node)
8858 continue;
8859
8860 base_list = tree_cons (access, base, base_list);
8861 if (BINFO_VIRTUAL_P (pbase_binfo))
8862 TREE_TYPE (base_list) = integer_type_node;
8863 }
8864 }
8865
8866 /* The list is now in reverse order; correct that. */
8867 base_list = nreverse (base_list);
8868
8869 if (pushed_scope)
8870 pop_scope (pushed_scope);
8871 }
8872 /* Now call xref_basetypes to set up all the base-class
8873 information. */
8874 xref_basetypes (type, base_list);
8875
8876 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8877 (int) ATTR_FLAG_TYPE_IN_PLACE,
8878 args, tf_error, NULL_TREE);
8879 fixup_attribute_variants (type);
8880
8881 /* Now that our base classes are set up, enter the scope of the
8882 class, so that name lookups into base classes, etc. will work
8883 correctly. This is precisely analogous to what we do in
8884 begin_class_definition when defining an ordinary non-template
8885 class, except we also need to push the enclosing classes. */
8886 push_nested_class (type);
8887
8888 /* Now members are processed in the order of declaration. */
8889 for (member = CLASSTYPE_DECL_LIST (pattern);
8890 member; member = TREE_CHAIN (member))
8891 {
8892 tree t = TREE_VALUE (member);
8893
8894 if (TREE_PURPOSE (member))
8895 {
8896 if (TYPE_P (t))
8897 {
8898 /* Build new CLASSTYPE_NESTED_UTDS. */
8899
8900 tree newtag;
8901 bool class_template_p;
8902
8903 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8904 && TYPE_LANG_SPECIFIC (t)
8905 && CLASSTYPE_IS_TEMPLATE (t));
8906 /* If the member is a class template, then -- even after
8907 substitution -- there may be dependent types in the
8908 template argument list for the class. We increment
8909 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8910 that function will assume that no types are dependent
8911 when outside of a template. */
8912 if (class_template_p)
8913 ++processing_template_decl;
8914 newtag = tsubst (t, args, tf_error, NULL_TREE);
8915 if (class_template_p)
8916 --processing_template_decl;
8917 if (newtag == error_mark_node)
8918 continue;
8919
8920 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8921 {
8922 tree name = TYPE_IDENTIFIER (t);
8923
8924 if (class_template_p)
8925 /* Unfortunately, lookup_template_class sets
8926 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8927 instantiation (i.e., for the type of a member
8928 template class nested within a template class.)
8929 This behavior is required for
8930 maybe_process_partial_specialization to work
8931 correctly, but is not accurate in this case;
8932 the TAG is not an instantiation of anything.
8933 (The corresponding TEMPLATE_DECL is an
8934 instantiation, but the TYPE is not.) */
8935 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8936
8937 /* Now, we call pushtag to put this NEWTAG into the scope of
8938 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8939 pushtag calling push_template_decl. We don't have to do
8940 this for enums because it will already have been done in
8941 tsubst_enum. */
8942 if (name)
8943 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8944 pushtag (name, newtag, /*tag_scope=*/ts_current);
8945 }
8946 }
8947 else if (DECL_DECLARES_FUNCTION_P (t))
8948 {
8949 /* Build new TYPE_METHODS. */
8950 tree r;
8951
8952 if (TREE_CODE (t) == TEMPLATE_DECL)
8953 ++processing_template_decl;
8954 r = tsubst (t, args, tf_error, NULL_TREE);
8955 if (TREE_CODE (t) == TEMPLATE_DECL)
8956 --processing_template_decl;
8957 set_current_access_from_decl (r);
8958 finish_member_declaration (r);
8959 /* Instantiate members marked with attribute used. */
8960 if (r != error_mark_node && DECL_PRESERVE_P (r))
8961 mark_used (r);
8962 if (TREE_CODE (r) == FUNCTION_DECL
8963 && DECL_OMP_DECLARE_REDUCTION_P (r))
8964 cp_check_omp_declare_reduction (r);
8965 }
8966 else
8967 {
8968 /* Build new TYPE_FIELDS. */
8969 if (TREE_CODE (t) == STATIC_ASSERT)
8970 {
8971 tree condition;
8972
8973 ++c_inhibit_evaluation_warnings;
8974 condition =
8975 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8976 tf_warning_or_error, NULL_TREE,
8977 /*integral_constant_expression_p=*/true);
8978 --c_inhibit_evaluation_warnings;
8979
8980 finish_static_assert (condition,
8981 STATIC_ASSERT_MESSAGE (t),
8982 STATIC_ASSERT_SOURCE_LOCATION (t),
8983 /*member_p=*/true);
8984 }
8985 else if (TREE_CODE (t) != CONST_DECL)
8986 {
8987 tree r;
8988 tree vec = NULL_TREE;
8989 int len = 1;
8990
8991 /* The file and line for this declaration, to
8992 assist in error message reporting. Since we
8993 called push_tinst_level above, we don't need to
8994 restore these. */
8995 input_location = DECL_SOURCE_LOCATION (t);
8996
8997 if (TREE_CODE (t) == TEMPLATE_DECL)
8998 ++processing_template_decl;
8999 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9000 if (TREE_CODE (t) == TEMPLATE_DECL)
9001 --processing_template_decl;
9002
9003 if (TREE_CODE (r) == TREE_VEC)
9004 {
9005 /* A capture pack became multiple fields. */
9006 vec = r;
9007 len = TREE_VEC_LENGTH (vec);
9008 }
9009
9010 for (int i = 0; i < len; ++i)
9011 {
9012 if (vec)
9013 r = TREE_VEC_ELT (vec, i);
9014 if (VAR_P (r))
9015 {
9016 /* In [temp.inst]:
9017
9018 [t]he initialization (and any associated
9019 side-effects) of a static data member does
9020 not occur unless the static data member is
9021 itself used in a way that requires the
9022 definition of the static data member to
9023 exist.
9024
9025 Therefore, we do not substitute into the
9026 initialized for the static data member here. */
9027 finish_static_data_member_decl
9028 (r,
9029 /*init=*/NULL_TREE,
9030 /*init_const_expr_p=*/false,
9031 /*asmspec_tree=*/NULL_TREE,
9032 /*flags=*/0);
9033 /* Instantiate members marked with attribute used. */
9034 if (r != error_mark_node && DECL_PRESERVE_P (r))
9035 mark_used (r);
9036 }
9037 else if (TREE_CODE (r) == FIELD_DECL)
9038 {
9039 /* Determine whether R has a valid type and can be
9040 completed later. If R is invalid, then its type
9041 is replaced by error_mark_node. */
9042 tree rtype = TREE_TYPE (r);
9043 if (can_complete_type_without_circularity (rtype))
9044 complete_type (rtype);
9045
9046 if (!COMPLETE_TYPE_P (rtype))
9047 {
9048 cxx_incomplete_type_error (r, rtype);
9049 TREE_TYPE (r) = error_mark_node;
9050 }
9051 }
9052
9053 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9054 such a thing will already have been added to the field
9055 list by tsubst_enum in finish_member_declaration in the
9056 CLASSTYPE_NESTED_UTDS case above. */
9057 if (!(TREE_CODE (r) == TYPE_DECL
9058 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9059 && DECL_ARTIFICIAL (r)))
9060 {
9061 set_current_access_from_decl (r);
9062 finish_member_declaration (r);
9063 }
9064 }
9065 }
9066 }
9067 }
9068 else
9069 {
9070 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9071 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9072 {
9073 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9074
9075 tree friend_type = t;
9076 bool adjust_processing_template_decl = false;
9077
9078 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9079 {
9080 /* template <class T> friend class C; */
9081 friend_type = tsubst_friend_class (friend_type, args);
9082 adjust_processing_template_decl = true;
9083 }
9084 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9085 {
9086 /* template <class T> friend class C::D; */
9087 friend_type = tsubst (friend_type, args,
9088 tf_warning_or_error, NULL_TREE);
9089 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9090 friend_type = TREE_TYPE (friend_type);
9091 adjust_processing_template_decl = true;
9092 }
9093 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9094 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9095 {
9096 /* This could be either
9097
9098 friend class T::C;
9099
9100 when dependent_type_p is false or
9101
9102 template <class U> friend class T::C;
9103
9104 otherwise. */
9105 friend_type = tsubst (friend_type, args,
9106 tf_warning_or_error, NULL_TREE);
9107 /* Bump processing_template_decl for correct
9108 dependent_type_p calculation. */
9109 ++processing_template_decl;
9110 if (dependent_type_p (friend_type))
9111 adjust_processing_template_decl = true;
9112 --processing_template_decl;
9113 }
9114 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9115 && hidden_name_p (TYPE_NAME (friend_type)))
9116 {
9117 /* friend class C;
9118
9119 where C hasn't been declared yet. Let's lookup name
9120 from namespace scope directly, bypassing any name that
9121 come from dependent base class. */
9122 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9123
9124 /* The call to xref_tag_from_type does injection for friend
9125 classes. */
9126 push_nested_namespace (ns);
9127 friend_type =
9128 xref_tag_from_type (friend_type, NULL_TREE,
9129 /*tag_scope=*/ts_current);
9130 pop_nested_namespace (ns);
9131 }
9132 else if (uses_template_parms (friend_type))
9133 /* friend class C<T>; */
9134 friend_type = tsubst (friend_type, args,
9135 tf_warning_or_error, NULL_TREE);
9136 /* Otherwise it's
9137
9138 friend class C;
9139
9140 where C is already declared or
9141
9142 friend class C<int>;
9143
9144 We don't have to do anything in these cases. */
9145
9146 if (adjust_processing_template_decl)
9147 /* Trick make_friend_class into realizing that the friend
9148 we're adding is a template, not an ordinary class. It's
9149 important that we use make_friend_class since it will
9150 perform some error-checking and output cross-reference
9151 information. */
9152 ++processing_template_decl;
9153
9154 if (friend_type != error_mark_node)
9155 make_friend_class (type, friend_type, /*complain=*/false);
9156
9157 if (adjust_processing_template_decl)
9158 --processing_template_decl;
9159 }
9160 else
9161 {
9162 /* Build new DECL_FRIENDLIST. */
9163 tree r;
9164
9165 /* The file and line for this declaration, to
9166 assist in error message reporting. Since we
9167 called push_tinst_level above, we don't need to
9168 restore these. */
9169 input_location = DECL_SOURCE_LOCATION (t);
9170
9171 if (TREE_CODE (t) == TEMPLATE_DECL)
9172 {
9173 ++processing_template_decl;
9174 push_deferring_access_checks (dk_no_check);
9175 }
9176
9177 r = tsubst_friend_function (t, args);
9178 add_friend (type, r, /*complain=*/false);
9179 if (TREE_CODE (t) == TEMPLATE_DECL)
9180 {
9181 pop_deferring_access_checks ();
9182 --processing_template_decl;
9183 }
9184 }
9185 }
9186 }
9187
9188 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9189 {
9190 tree decl = lambda_function (type);
9191 if (decl)
9192 {
9193 if (!DECL_TEMPLATE_INFO (decl)
9194 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9195 instantiate_decl (decl, false, false);
9196
9197 /* We need to instantiate the capture list from the template
9198 after we've instantiated the closure members, but before we
9199 consider adding the conversion op. Also keep any captures
9200 that may have been added during instantiation of the op(). */
9201 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9202 tree tmpl_cap
9203 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9204 args, tf_warning_or_error, NULL_TREE,
9205 false, false);
9206
9207 LAMBDA_EXPR_CAPTURE_LIST (expr)
9208 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9209
9210 maybe_add_lambda_conv_op (type);
9211 }
9212 else
9213 gcc_assert (errorcount);
9214 }
9215
9216 /* Set the file and line number information to whatever is given for
9217 the class itself. This puts error messages involving generated
9218 implicit functions at a predictable point, and the same point
9219 that would be used for non-template classes. */
9220 input_location = DECL_SOURCE_LOCATION (typedecl);
9221
9222 unreverse_member_declarations (type);
9223 finish_struct_1 (type);
9224 TYPE_BEING_DEFINED (type) = 0;
9225
9226 /* We don't instantiate default arguments for member functions. 14.7.1:
9227
9228 The implicit instantiation of a class template specialization causes
9229 the implicit instantiation of the declarations, but not of the
9230 definitions or default arguments, of the class member functions,
9231 member classes, static data members and member templates.... */
9232
9233 /* Some typedefs referenced from within the template code need to be access
9234 checked at template instantiation time, i.e now. These types were
9235 added to the template at parsing time. Let's get those and perform
9236 the access checks then. */
9237 perform_typedefs_access_check (pattern, args);
9238 perform_deferred_access_checks (tf_warning_or_error);
9239 pop_nested_class ();
9240 maximum_field_alignment = saved_maximum_field_alignment;
9241 if (!fn_context)
9242 pop_from_top_level ();
9243 pop_deferring_access_checks ();
9244 pop_tinst_level ();
9245
9246 /* The vtable for a template class can be emitted in any translation
9247 unit in which the class is instantiated. When there is no key
9248 method, however, finish_struct_1 will already have added TYPE to
9249 the keyed_classes list. */
9250 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9251 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9252
9253 return type;
9254 }
9255
9256 /* Wrapper for instantiate_class_template_1. */
9257
9258 tree
9259 instantiate_class_template (tree type)
9260 {
9261 tree ret;
9262 timevar_push (TV_TEMPLATE_INST);
9263 ret = instantiate_class_template_1 (type);
9264 timevar_pop (TV_TEMPLATE_INST);
9265 return ret;
9266 }
9267
9268 static tree
9269 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9270 {
9271 tree r;
9272
9273 if (!t)
9274 r = t;
9275 else if (TYPE_P (t))
9276 r = tsubst (t, args, complain, in_decl);
9277 else
9278 {
9279 if (!(complain & tf_warning))
9280 ++c_inhibit_evaluation_warnings;
9281 r = tsubst_expr (t, args, complain, in_decl,
9282 /*integral_constant_expression_p=*/true);
9283 if (!(complain & tf_warning))
9284 --c_inhibit_evaluation_warnings;
9285 }
9286 return r;
9287 }
9288
9289 /* Given a function parameter pack TMPL_PARM and some function parameters
9290 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9291 and set *SPEC_P to point at the next point in the list. */
9292
9293 static tree
9294 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9295 {
9296 /* Collect all of the extra "packed" parameters into an
9297 argument pack. */
9298 tree parmvec;
9299 tree parmtypevec;
9300 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9301 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9302 tree spec_parm = *spec_p;
9303 int i, len;
9304
9305 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9306 if (tmpl_parm
9307 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9308 break;
9309
9310 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9311 parmvec = make_tree_vec (len);
9312 parmtypevec = make_tree_vec (len);
9313 spec_parm = *spec_p;
9314 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9315 {
9316 TREE_VEC_ELT (parmvec, i) = spec_parm;
9317 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9318 }
9319
9320 /* Build the argument packs. */
9321 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9322 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9323 TREE_TYPE (argpack) = argtypepack;
9324 *spec_p = spec_parm;
9325
9326 return argpack;
9327 }
9328
9329 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9330 NONTYPE_ARGUMENT_PACK. */
9331
9332 static tree
9333 make_fnparm_pack (tree spec_parm)
9334 {
9335 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9336 }
9337
9338 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9339 pack expansion. */
9340
9341 static bool
9342 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9343 {
9344 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9345 if (i >= TREE_VEC_LENGTH (vec))
9346 return false;
9347 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9348 }
9349
9350
9351 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9352
9353 static tree
9354 make_argument_pack_select (tree arg_pack, unsigned index)
9355 {
9356 tree aps = make_node (ARGUMENT_PACK_SELECT);
9357
9358 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9359 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9360
9361 return aps;
9362 }
9363
9364 /* This is a subroutine of tsubst_pack_expansion.
9365
9366 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9367 mechanism to store the (non complete list of) arguments of the
9368 substitution and return a non substituted pack expansion, in order
9369 to wait for when we have enough arguments to really perform the
9370 substitution. */
9371
9372 static bool
9373 use_pack_expansion_extra_args_p (tree parm_packs,
9374 int arg_pack_len,
9375 bool has_empty_arg)
9376 {
9377 /* If one pack has an expansion and another pack has a normal
9378 argument or if one pack has an empty argument and an another
9379 one hasn't then tsubst_pack_expansion cannot perform the
9380 substitution and need to fall back on the
9381 PACK_EXPANSION_EXTRA mechanism. */
9382 if (parm_packs == NULL_TREE)
9383 return false;
9384 else if (has_empty_arg)
9385 return true;
9386
9387 bool has_expansion_arg = false;
9388 for (int i = 0 ; i < arg_pack_len; ++i)
9389 {
9390 bool has_non_expansion_arg = false;
9391 for (tree parm_pack = parm_packs;
9392 parm_pack;
9393 parm_pack = TREE_CHAIN (parm_pack))
9394 {
9395 tree arg = TREE_VALUE (parm_pack);
9396
9397 if (argument_pack_element_is_expansion_p (arg, i))
9398 has_expansion_arg = true;
9399 else
9400 has_non_expansion_arg = true;
9401 }
9402
9403 if (has_expansion_arg && has_non_expansion_arg)
9404 return true;
9405 }
9406 return false;
9407 }
9408
9409 /* [temp.variadic]/6 says that:
9410
9411 The instantiation of a pack expansion [...]
9412 produces a list E1,E2, ..., En, where N is the number of elements
9413 in the pack expansion parameters.
9414
9415 This subroutine of tsubst_pack_expansion produces one of these Ei.
9416
9417 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9418 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9419 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9420 INDEX is the index 'i' of the element Ei to produce. ARGS,
9421 COMPLAIN, and IN_DECL are the same parameters as for the
9422 tsubst_pack_expansion function.
9423
9424 The function returns the resulting Ei upon successful completion,
9425 or error_mark_node.
9426
9427 Note that this function possibly modifies the ARGS parameter, so
9428 it's the responsibility of the caller to restore it. */
9429
9430 static tree
9431 gen_elem_of_pack_expansion_instantiation (tree pattern,
9432 tree parm_packs,
9433 unsigned index,
9434 tree args /* This parm gets
9435 modified. */,
9436 tsubst_flags_t complain,
9437 tree in_decl)
9438 {
9439 tree t;
9440 bool ith_elem_is_expansion = false;
9441
9442 /* For each parameter pack, change the substitution of the parameter
9443 pack to the ith argument in its argument pack, then expand the
9444 pattern. */
9445 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9446 {
9447 tree parm = TREE_PURPOSE (pack);
9448 tree arg_pack = TREE_VALUE (pack);
9449 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9450
9451 ith_elem_is_expansion |=
9452 argument_pack_element_is_expansion_p (arg_pack, index);
9453
9454 /* Select the Ith argument from the pack. */
9455 if (TREE_CODE (parm) == PARM_DECL
9456 || TREE_CODE (parm) == FIELD_DECL)
9457 {
9458 if (index == 0)
9459 {
9460 aps = make_argument_pack_select (arg_pack, index);
9461 mark_used (parm);
9462 register_local_specialization (aps, parm);
9463 }
9464 else
9465 aps = retrieve_local_specialization (parm);
9466 }
9467 else
9468 {
9469 int idx, level;
9470 template_parm_level_and_index (parm, &level, &idx);
9471
9472 if (index == 0)
9473 {
9474 aps = make_argument_pack_select (arg_pack, index);
9475 /* Update the corresponding argument. */
9476 TMPL_ARG (args, level, idx) = aps;
9477 }
9478 else
9479 /* Re-use the ARGUMENT_PACK_SELECT. */
9480 aps = TMPL_ARG (args, level, idx);
9481 }
9482 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9483 }
9484
9485 /* Substitute into the PATTERN with the (possibly altered)
9486 arguments. */
9487 if (!TYPE_P (pattern))
9488 t = tsubst_expr (pattern, args, complain, in_decl,
9489 /*integral_constant_expression_p=*/false);
9490 else
9491 t = tsubst (pattern, args, complain, in_decl);
9492
9493 /* If the Ith argument pack element is a pack expansion, then
9494 the Ith element resulting from the substituting is going to
9495 be a pack expansion as well. */
9496 if (ith_elem_is_expansion)
9497 t = make_pack_expansion (t);
9498
9499 return t;
9500 }
9501
9502 /* Substitute ARGS into T, which is an pack expansion
9503 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9504 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9505 (if only a partial substitution could be performed) or
9506 ERROR_MARK_NODE if there was an error. */
9507 tree
9508 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9509 tree in_decl)
9510 {
9511 tree pattern;
9512 tree pack, packs = NULL_TREE;
9513 bool unsubstituted_packs = false;
9514 int i, len = -1;
9515 tree result;
9516 struct pointer_map_t *saved_local_specializations = NULL;
9517 bool need_local_specializations = false;
9518 int levels;
9519
9520 gcc_assert (PACK_EXPANSION_P (t));
9521 pattern = PACK_EXPANSION_PATTERN (t);
9522
9523 /* Add in any args remembered from an earlier partial instantiation. */
9524 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9525
9526 levels = TMPL_ARGS_DEPTH (args);
9527
9528 /* Determine the argument packs that will instantiate the parameter
9529 packs used in the expansion expression. While we're at it,
9530 compute the number of arguments to be expanded and make sure it
9531 is consistent. */
9532 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9533 pack = TREE_CHAIN (pack))
9534 {
9535 tree parm_pack = TREE_VALUE (pack);
9536 tree arg_pack = NULL_TREE;
9537 tree orig_arg = NULL_TREE;
9538 int level = 0;
9539
9540 if (TREE_CODE (parm_pack) == BASES)
9541 {
9542 if (BASES_DIRECT (parm_pack))
9543 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9544 args, complain, in_decl, false));
9545 else
9546 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9547 args, complain, in_decl, false));
9548 }
9549 if (TREE_CODE (parm_pack) == PARM_DECL)
9550 {
9551 if (PACK_EXPANSION_LOCAL_P (t))
9552 arg_pack = retrieve_local_specialization (parm_pack);
9553 else
9554 {
9555 /* We can't rely on local_specializations for a parameter
9556 name used later in a function declaration (such as in a
9557 late-specified return type). Even if it exists, it might
9558 have the wrong value for a recursive call. Just make a
9559 dummy decl, since it's only used for its type. */
9560 arg_pack = tsubst_decl (parm_pack, args, complain);
9561 if (arg_pack && DECL_PACK_P (arg_pack))
9562 /* Partial instantiation of the parm_pack, we can't build
9563 up an argument pack yet. */
9564 arg_pack = NULL_TREE;
9565 else
9566 arg_pack = make_fnparm_pack (arg_pack);
9567 need_local_specializations = true;
9568 }
9569 }
9570 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9571 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9572 else
9573 {
9574 int idx;
9575 template_parm_level_and_index (parm_pack, &level, &idx);
9576
9577 if (level <= levels)
9578 arg_pack = TMPL_ARG (args, level, idx);
9579 }
9580
9581 orig_arg = arg_pack;
9582 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9583 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9584
9585 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9586 /* This can only happen if we forget to expand an argument
9587 pack somewhere else. Just return an error, silently. */
9588 {
9589 result = make_tree_vec (1);
9590 TREE_VEC_ELT (result, 0) = error_mark_node;
9591 return result;
9592 }
9593
9594 if (arg_pack)
9595 {
9596 int my_len =
9597 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9598
9599 /* Don't bother trying to do a partial substitution with
9600 incomplete packs; we'll try again after deduction. */
9601 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9602 return t;
9603
9604 if (len < 0)
9605 len = my_len;
9606 else if (len != my_len)
9607 {
9608 if (!(complain & tf_error))
9609 /* Fail quietly. */;
9610 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9611 error ("mismatched argument pack lengths while expanding "
9612 "%<%T%>",
9613 pattern);
9614 else
9615 error ("mismatched argument pack lengths while expanding "
9616 "%<%E%>",
9617 pattern);
9618 return error_mark_node;
9619 }
9620
9621 /* Keep track of the parameter packs and their corresponding
9622 argument packs. */
9623 packs = tree_cons (parm_pack, arg_pack, packs);
9624 TREE_TYPE (packs) = orig_arg;
9625 }
9626 else
9627 {
9628 /* We can't substitute for this parameter pack. We use a flag as
9629 well as the missing_level counter because function parameter
9630 packs don't have a level. */
9631 unsubstituted_packs = true;
9632 }
9633 }
9634
9635 /* We cannot expand this expansion expression, because we don't have
9636 all of the argument packs we need. */
9637 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9638 {
9639 /* We got some full packs, but we can't substitute them in until we
9640 have values for all the packs. So remember these until then. */
9641
9642 t = make_pack_expansion (pattern);
9643 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9644 return t;
9645 }
9646 else if (unsubstituted_packs)
9647 {
9648 /* There were no real arguments, we're just replacing a parameter
9649 pack with another version of itself. Substitute into the
9650 pattern and return a PACK_EXPANSION_*. The caller will need to
9651 deal with that. */
9652 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9653 t = tsubst_expr (pattern, args, complain, in_decl,
9654 /*integral_constant_expression_p=*/false);
9655 else
9656 t = tsubst (pattern, args, complain, in_decl);
9657 t = make_pack_expansion (t);
9658 return t;
9659 }
9660
9661 gcc_assert (len >= 0);
9662
9663 if (need_local_specializations)
9664 {
9665 /* We're in a late-specified return type, so create our own local
9666 specializations map; the current map is either NULL or (in the
9667 case of recursive unification) might have bindings that we don't
9668 want to use or alter. */
9669 saved_local_specializations = local_specializations;
9670 local_specializations = pointer_map_create ();
9671 }
9672
9673 /* For each argument in each argument pack, substitute into the
9674 pattern. */
9675 result = make_tree_vec (len);
9676 for (i = 0; i < len; ++i)
9677 {
9678 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9679 i,
9680 args, complain,
9681 in_decl);
9682 TREE_VEC_ELT (result, i) = t;
9683 if (t == error_mark_node)
9684 {
9685 result = error_mark_node;
9686 break;
9687 }
9688 }
9689
9690 /* Update ARGS to restore the substitution from parameter packs to
9691 their argument packs. */
9692 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9693 {
9694 tree parm = TREE_PURPOSE (pack);
9695
9696 if (TREE_CODE (parm) == PARM_DECL
9697 || TREE_CODE (parm) == FIELD_DECL)
9698 register_local_specialization (TREE_TYPE (pack), parm);
9699 else
9700 {
9701 int idx, level;
9702
9703 if (TREE_VALUE (pack) == NULL_TREE)
9704 continue;
9705
9706 template_parm_level_and_index (parm, &level, &idx);
9707
9708 /* Update the corresponding argument. */
9709 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9710 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9711 TREE_TYPE (pack);
9712 else
9713 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9714 }
9715 }
9716
9717 if (need_local_specializations)
9718 {
9719 pointer_map_destroy (local_specializations);
9720 local_specializations = saved_local_specializations;
9721 }
9722
9723 return result;
9724 }
9725
9726 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9727 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9728 parameter packs; all parms generated from a function parameter pack will
9729 have the same DECL_PARM_INDEX. */
9730
9731 tree
9732 get_pattern_parm (tree parm, tree tmpl)
9733 {
9734 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9735 tree patparm;
9736
9737 if (DECL_ARTIFICIAL (parm))
9738 {
9739 for (patparm = DECL_ARGUMENTS (pattern);
9740 patparm; patparm = DECL_CHAIN (patparm))
9741 if (DECL_ARTIFICIAL (patparm)
9742 && DECL_NAME (parm) == DECL_NAME (patparm))
9743 break;
9744 }
9745 else
9746 {
9747 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9748 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9749 gcc_assert (DECL_PARM_INDEX (patparm)
9750 == DECL_PARM_INDEX (parm));
9751 }
9752
9753 return patparm;
9754 }
9755
9756 /* Substitute ARGS into the vector or list of template arguments T. */
9757
9758 static tree
9759 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9760 {
9761 tree orig_t = t;
9762 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9763 tree *elts;
9764
9765 if (t == error_mark_node)
9766 return error_mark_node;
9767
9768 len = TREE_VEC_LENGTH (t);
9769 elts = XALLOCAVEC (tree, len);
9770
9771 for (i = 0; i < len; i++)
9772 {
9773 tree orig_arg = TREE_VEC_ELT (t, i);
9774 tree new_arg;
9775
9776 if (TREE_CODE (orig_arg) == TREE_VEC)
9777 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9778 else if (PACK_EXPANSION_P (orig_arg))
9779 {
9780 /* Substitute into an expansion expression. */
9781 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9782
9783 if (TREE_CODE (new_arg) == TREE_VEC)
9784 /* Add to the expanded length adjustment the number of
9785 expanded arguments. We subtract one from this
9786 measurement, because the argument pack expression
9787 itself is already counted as 1 in
9788 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9789 the argument pack is empty. */
9790 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9791 }
9792 else if (ARGUMENT_PACK_P (orig_arg))
9793 {
9794 /* Substitute into each of the arguments. */
9795 new_arg = TYPE_P (orig_arg)
9796 ? cxx_make_type (TREE_CODE (orig_arg))
9797 : make_node (TREE_CODE (orig_arg));
9798
9799 SET_ARGUMENT_PACK_ARGS (
9800 new_arg,
9801 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9802 args, complain, in_decl));
9803
9804 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9805 new_arg = error_mark_node;
9806
9807 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9808 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9809 complain, in_decl);
9810 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9811
9812 if (TREE_TYPE (new_arg) == error_mark_node)
9813 new_arg = error_mark_node;
9814 }
9815 }
9816 else
9817 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9818
9819 if (new_arg == error_mark_node)
9820 return error_mark_node;
9821
9822 elts[i] = new_arg;
9823 if (new_arg != orig_arg)
9824 need_new = 1;
9825 }
9826
9827 if (!need_new)
9828 return t;
9829
9830 /* Make space for the expanded arguments coming from template
9831 argument packs. */
9832 t = make_tree_vec (len + expanded_len_adjust);
9833 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9834 arguments for a member template.
9835 In that case each TREE_VEC in ORIG_T represents a level of template
9836 arguments, and ORIG_T won't carry any non defaulted argument count.
9837 It will rather be the nested TREE_VECs that will carry one.
9838 In other words, ORIG_T carries a non defaulted argument count only
9839 if it doesn't contain any nested TREE_VEC. */
9840 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9841 {
9842 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9843 count += expanded_len_adjust;
9844 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9845 }
9846 for (i = 0, out = 0; i < len; i++)
9847 {
9848 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9849 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9850 && TREE_CODE (elts[i]) == TREE_VEC)
9851 {
9852 int idx;
9853
9854 /* Now expand the template argument pack "in place". */
9855 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9856 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9857 }
9858 else
9859 {
9860 TREE_VEC_ELT (t, out) = elts[i];
9861 out++;
9862 }
9863 }
9864
9865 return t;
9866 }
9867
9868 /* Return the result of substituting ARGS into the template parameters
9869 given by PARMS. If there are m levels of ARGS and m + n levels of
9870 PARMS, then the result will contain n levels of PARMS. For
9871 example, if PARMS is `template <class T> template <class U>
9872 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9873 result will be `template <int*, double, class V>'. */
9874
9875 static tree
9876 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9877 {
9878 tree r = NULL_TREE;
9879 tree* new_parms;
9880
9881 /* When substituting into a template, we must set
9882 PROCESSING_TEMPLATE_DECL as the template parameters may be
9883 dependent if they are based on one-another, and the dependency
9884 predicates are short-circuit outside of templates. */
9885 ++processing_template_decl;
9886
9887 for (new_parms = &r;
9888 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9889 new_parms = &(TREE_CHAIN (*new_parms)),
9890 parms = TREE_CHAIN (parms))
9891 {
9892 tree new_vec =
9893 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9894 int i;
9895
9896 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9897 {
9898 tree tuple;
9899
9900 if (parms == error_mark_node)
9901 continue;
9902
9903 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9904
9905 if (tuple == error_mark_node)
9906 continue;
9907
9908 TREE_VEC_ELT (new_vec, i) =
9909 tsubst_template_parm (tuple, args, complain);
9910 }
9911
9912 *new_parms =
9913 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9914 - TMPL_ARGS_DEPTH (args)),
9915 new_vec, NULL_TREE);
9916 }
9917
9918 --processing_template_decl;
9919
9920 return r;
9921 }
9922
9923 /* Return the result of substituting ARGS into one template parameter
9924 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9925 parameter and which TREE_PURPOSE is the default argument of the
9926 template parameter. */
9927
9928 static tree
9929 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9930 {
9931 tree default_value, parm_decl;
9932
9933 if (args == NULL_TREE
9934 || t == NULL_TREE
9935 || t == error_mark_node)
9936 return t;
9937
9938 gcc_assert (TREE_CODE (t) == TREE_LIST);
9939
9940 default_value = TREE_PURPOSE (t);
9941 parm_decl = TREE_VALUE (t);
9942
9943 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9944 if (TREE_CODE (parm_decl) == PARM_DECL
9945 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9946 parm_decl = error_mark_node;
9947 default_value = tsubst_template_arg (default_value, args,
9948 complain, NULL_TREE);
9949
9950 return build_tree_list (default_value, parm_decl);
9951 }
9952
9953 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9954 type T. If T is not an aggregate or enumeration type, it is
9955 handled as if by tsubst. IN_DECL is as for tsubst. If
9956 ENTERING_SCOPE is nonzero, T is the context for a template which
9957 we are presently tsubst'ing. Return the substituted value. */
9958
9959 static tree
9960 tsubst_aggr_type (tree t,
9961 tree args,
9962 tsubst_flags_t complain,
9963 tree in_decl,
9964 int entering_scope)
9965 {
9966 if (t == NULL_TREE)
9967 return NULL_TREE;
9968
9969 switch (TREE_CODE (t))
9970 {
9971 case RECORD_TYPE:
9972 if (TYPE_PTRMEMFUNC_P (t))
9973 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9974
9975 /* Else fall through. */
9976 case ENUMERAL_TYPE:
9977 case UNION_TYPE:
9978 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9979 {
9980 tree argvec;
9981 tree context;
9982 tree r;
9983 int saved_unevaluated_operand;
9984 int saved_inhibit_evaluation_warnings;
9985
9986 /* In "sizeof(X<I>)" we need to evaluate "I". */
9987 saved_unevaluated_operand = cp_unevaluated_operand;
9988 cp_unevaluated_operand = 0;
9989 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9990 c_inhibit_evaluation_warnings = 0;
9991
9992 /* First, determine the context for the type we are looking
9993 up. */
9994 context = TYPE_CONTEXT (t);
9995 if (context && TYPE_P (context))
9996 {
9997 context = tsubst_aggr_type (context, args, complain,
9998 in_decl, /*entering_scope=*/1);
9999 /* If context is a nested class inside a class template,
10000 it may still need to be instantiated (c++/33959). */
10001 context = complete_type (context);
10002 }
10003
10004 /* Then, figure out what arguments are appropriate for the
10005 type we are trying to find. For example, given:
10006
10007 template <class T> struct S;
10008 template <class T, class U> void f(T, U) { S<U> su; }
10009
10010 and supposing that we are instantiating f<int, double>,
10011 then our ARGS will be {int, double}, but, when looking up
10012 S we only want {double}. */
10013 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10014 complain, in_decl);
10015 if (argvec == error_mark_node)
10016 r = error_mark_node;
10017 else
10018 {
10019 r = lookup_template_class (t, argvec, in_decl, context,
10020 entering_scope, complain);
10021 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10022 }
10023
10024 cp_unevaluated_operand = saved_unevaluated_operand;
10025 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10026
10027 return r;
10028 }
10029 else
10030 /* This is not a template type, so there's nothing to do. */
10031 return t;
10032
10033 default:
10034 return tsubst (t, args, complain, in_decl);
10035 }
10036 }
10037
10038 /* Substitute into the default argument ARG (a default argument for
10039 FN), which has the indicated TYPE. */
10040
10041 tree
10042 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10043 {
10044 tree saved_class_ptr = NULL_TREE;
10045 tree saved_class_ref = NULL_TREE;
10046 int errs = errorcount + sorrycount;
10047
10048 /* This can happen in invalid code. */
10049 if (TREE_CODE (arg) == DEFAULT_ARG)
10050 return arg;
10051
10052 /* This default argument came from a template. Instantiate the
10053 default argument here, not in tsubst. In the case of
10054 something like:
10055
10056 template <class T>
10057 struct S {
10058 static T t();
10059 void f(T = t());
10060 };
10061
10062 we must be careful to do name lookup in the scope of S<T>,
10063 rather than in the current class. */
10064 push_access_scope (fn);
10065 /* The "this" pointer is not valid in a default argument. */
10066 if (cfun)
10067 {
10068 saved_class_ptr = current_class_ptr;
10069 cp_function_chain->x_current_class_ptr = NULL_TREE;
10070 saved_class_ref = current_class_ref;
10071 cp_function_chain->x_current_class_ref = NULL_TREE;
10072 }
10073
10074 push_deferring_access_checks(dk_no_deferred);
10075 /* The default argument expression may cause implicitly defined
10076 member functions to be synthesized, which will result in garbage
10077 collection. We must treat this situation as if we were within
10078 the body of function so as to avoid collecting live data on the
10079 stack. */
10080 ++function_depth;
10081 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10082 complain, NULL_TREE,
10083 /*integral_constant_expression_p=*/false);
10084 --function_depth;
10085 pop_deferring_access_checks();
10086
10087 /* Restore the "this" pointer. */
10088 if (cfun)
10089 {
10090 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10091 cp_function_chain->x_current_class_ref = saved_class_ref;
10092 }
10093
10094 if (errorcount+sorrycount > errs
10095 && (complain & tf_warning_or_error))
10096 inform (input_location,
10097 " when instantiating default argument for call to %D", fn);
10098
10099 /* Make sure the default argument is reasonable. */
10100 arg = check_default_argument (type, arg, complain);
10101
10102 pop_access_scope (fn);
10103
10104 return arg;
10105 }
10106
10107 /* Substitute into all the default arguments for FN. */
10108
10109 static void
10110 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10111 {
10112 tree arg;
10113 tree tmpl_args;
10114
10115 tmpl_args = DECL_TI_ARGS (fn);
10116
10117 /* If this function is not yet instantiated, we certainly don't need
10118 its default arguments. */
10119 if (uses_template_parms (tmpl_args))
10120 return;
10121 /* Don't do this again for clones. */
10122 if (DECL_CLONED_FUNCTION_P (fn))
10123 return;
10124
10125 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10126 arg;
10127 arg = TREE_CHAIN (arg))
10128 if (TREE_PURPOSE (arg))
10129 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10130 TREE_VALUE (arg),
10131 TREE_PURPOSE (arg),
10132 complain);
10133 }
10134
10135 /* Substitute the ARGS into the T, which is a _DECL. Return the
10136 result of the substitution. Issue error and warning messages under
10137 control of COMPLAIN. */
10138
10139 static tree
10140 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10141 {
10142 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10143 location_t saved_loc;
10144 tree r = NULL_TREE;
10145 tree in_decl = t;
10146 hashval_t hash = 0;
10147
10148 /* Set the filename and linenumber to improve error-reporting. */
10149 saved_loc = input_location;
10150 input_location = DECL_SOURCE_LOCATION (t);
10151
10152 switch (TREE_CODE (t))
10153 {
10154 case TEMPLATE_DECL:
10155 {
10156 /* We can get here when processing a member function template,
10157 member class template, or template template parameter. */
10158 tree decl = DECL_TEMPLATE_RESULT (t);
10159 tree spec;
10160 tree tmpl_args;
10161 tree full_args;
10162
10163 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10164 {
10165 /* Template template parameter is treated here. */
10166 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10167 if (new_type == error_mark_node)
10168 RETURN (error_mark_node);
10169 /* If we get a real template back, return it. This can happen in
10170 the context of most_specialized_class. */
10171 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10172 return new_type;
10173
10174 r = copy_decl (t);
10175 DECL_CHAIN (r) = NULL_TREE;
10176 TREE_TYPE (r) = new_type;
10177 DECL_TEMPLATE_RESULT (r)
10178 = build_decl (DECL_SOURCE_LOCATION (decl),
10179 TYPE_DECL, DECL_NAME (decl), new_type);
10180 DECL_TEMPLATE_PARMS (r)
10181 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10182 complain);
10183 TYPE_NAME (new_type) = r;
10184 break;
10185 }
10186
10187 /* We might already have an instance of this template.
10188 The ARGS are for the surrounding class type, so the
10189 full args contain the tsubst'd args for the context,
10190 plus the innermost args from the template decl. */
10191 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10192 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10193 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10194 /* Because this is a template, the arguments will still be
10195 dependent, even after substitution. If
10196 PROCESSING_TEMPLATE_DECL is not set, the dependency
10197 predicates will short-circuit. */
10198 ++processing_template_decl;
10199 full_args = tsubst_template_args (tmpl_args, args,
10200 complain, in_decl);
10201 --processing_template_decl;
10202 if (full_args == error_mark_node)
10203 RETURN (error_mark_node);
10204
10205 /* If this is a default template template argument,
10206 tsubst might not have changed anything. */
10207 if (full_args == tmpl_args)
10208 RETURN (t);
10209
10210 hash = hash_tmpl_and_args (t, full_args);
10211 spec = retrieve_specialization (t, full_args, hash);
10212 if (spec != NULL_TREE)
10213 {
10214 r = spec;
10215 break;
10216 }
10217
10218 /* Make a new template decl. It will be similar to the
10219 original, but will record the current template arguments.
10220 We also create a new function declaration, which is just
10221 like the old one, but points to this new template, rather
10222 than the old one. */
10223 r = copy_decl (t);
10224 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10225 DECL_CHAIN (r) = NULL_TREE;
10226
10227 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10228
10229 if (TREE_CODE (decl) == TYPE_DECL
10230 && !TYPE_DECL_ALIAS_P (decl))
10231 {
10232 tree new_type;
10233 ++processing_template_decl;
10234 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10235 --processing_template_decl;
10236 if (new_type == error_mark_node)
10237 RETURN (error_mark_node);
10238
10239 TREE_TYPE (r) = new_type;
10240 /* For a partial specialization, we need to keep pointing to
10241 the primary template. */
10242 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10243 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10244 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10245 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10246 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10247 }
10248 else
10249 {
10250 tree new_decl;
10251 ++processing_template_decl;
10252 new_decl = tsubst (decl, args, complain, in_decl);
10253 --processing_template_decl;
10254 if (new_decl == error_mark_node)
10255 RETURN (error_mark_node);
10256
10257 DECL_TEMPLATE_RESULT (r) = new_decl;
10258 DECL_TI_TEMPLATE (new_decl) = r;
10259 TREE_TYPE (r) = TREE_TYPE (new_decl);
10260 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10261 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10262 }
10263
10264 SET_DECL_IMPLICIT_INSTANTIATION (r);
10265 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10266 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10267
10268 /* The template parameters for this new template are all the
10269 template parameters for the old template, except the
10270 outermost level of parameters. */
10271 DECL_TEMPLATE_PARMS (r)
10272 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10273 complain);
10274
10275 if (PRIMARY_TEMPLATE_P (t))
10276 DECL_PRIMARY_TEMPLATE (r) = r;
10277
10278 if (TREE_CODE (decl) != TYPE_DECL)
10279 /* Record this non-type partial instantiation. */
10280 register_specialization (r, t,
10281 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10282 false, hash);
10283 }
10284 break;
10285
10286 case FUNCTION_DECL:
10287 {
10288 tree ctx;
10289 tree argvec = NULL_TREE;
10290 tree *friends;
10291 tree gen_tmpl;
10292 tree type;
10293 int member;
10294 int args_depth;
10295 int parms_depth;
10296
10297 /* Nobody should be tsubst'ing into non-template functions. */
10298 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10299
10300 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10301 {
10302 tree spec;
10303 bool dependent_p;
10304
10305 /* If T is not dependent, just return it. We have to
10306 increment PROCESSING_TEMPLATE_DECL because
10307 value_dependent_expression_p assumes that nothing is
10308 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10309 ++processing_template_decl;
10310 dependent_p = value_dependent_expression_p (t);
10311 --processing_template_decl;
10312 if (!dependent_p)
10313 RETURN (t);
10314
10315 /* Calculate the most general template of which R is a
10316 specialization, and the complete set of arguments used to
10317 specialize R. */
10318 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10319 argvec = tsubst_template_args (DECL_TI_ARGS
10320 (DECL_TEMPLATE_RESULT
10321 (DECL_TI_TEMPLATE (t))),
10322 args, complain, in_decl);
10323 if (argvec == error_mark_node)
10324 RETURN (error_mark_node);
10325
10326 /* Check to see if we already have this specialization. */
10327 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10328 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10329
10330 if (spec)
10331 {
10332 r = spec;
10333 break;
10334 }
10335
10336 /* We can see more levels of arguments than parameters if
10337 there was a specialization of a member template, like
10338 this:
10339
10340 template <class T> struct S { template <class U> void f(); }
10341 template <> template <class U> void S<int>::f(U);
10342
10343 Here, we'll be substituting into the specialization,
10344 because that's where we can find the code we actually
10345 want to generate, but we'll have enough arguments for
10346 the most general template.
10347
10348 We also deal with the peculiar case:
10349
10350 template <class T> struct S {
10351 template <class U> friend void f();
10352 };
10353 template <class U> void f() {}
10354 template S<int>;
10355 template void f<double>();
10356
10357 Here, the ARGS for the instantiation of will be {int,
10358 double}. But, we only need as many ARGS as there are
10359 levels of template parameters in CODE_PATTERN. We are
10360 careful not to get fooled into reducing the ARGS in
10361 situations like:
10362
10363 template <class T> struct S { template <class U> void f(U); }
10364 template <class T> template <> void S<T>::f(int) {}
10365
10366 which we can spot because the pattern will be a
10367 specialization in this case. */
10368 args_depth = TMPL_ARGS_DEPTH (args);
10369 parms_depth =
10370 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10371 if (args_depth > parms_depth
10372 && !DECL_TEMPLATE_SPECIALIZATION (t))
10373 args = get_innermost_template_args (args, parms_depth);
10374 }
10375 else
10376 {
10377 /* This special case arises when we have something like this:
10378
10379 template <class T> struct S {
10380 friend void f<int>(int, double);
10381 };
10382
10383 Here, the DECL_TI_TEMPLATE for the friend declaration
10384 will be an IDENTIFIER_NODE. We are being called from
10385 tsubst_friend_function, and we want only to create a
10386 new decl (R) with appropriate types so that we can call
10387 determine_specialization. */
10388 gen_tmpl = NULL_TREE;
10389 }
10390
10391 if (DECL_CLASS_SCOPE_P (t))
10392 {
10393 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10394 member = 2;
10395 else
10396 member = 1;
10397 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10398 complain, t, /*entering_scope=*/1);
10399 }
10400 else
10401 {
10402 member = 0;
10403 ctx = DECL_CONTEXT (t);
10404 }
10405 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10406 if (type == error_mark_node)
10407 RETURN (error_mark_node);
10408
10409 /* If we hit excessive deduction depth, the type is bogus even if
10410 it isn't error_mark_node, so don't build a decl. */
10411 if (excessive_deduction_depth)
10412 RETURN (error_mark_node);
10413
10414 /* We do NOT check for matching decls pushed separately at this
10415 point, as they may not represent instantiations of this
10416 template, and in any case are considered separate under the
10417 discrete model. */
10418 r = copy_decl (t);
10419 DECL_USE_TEMPLATE (r) = 0;
10420 TREE_TYPE (r) = type;
10421 /* Clear out the mangled name and RTL for the instantiation. */
10422 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10423 SET_DECL_RTL (r, NULL);
10424 /* Leave DECL_INITIAL set on deleted instantiations. */
10425 if (!DECL_DELETED_FN (r))
10426 DECL_INITIAL (r) = NULL_TREE;
10427 DECL_CONTEXT (r) = ctx;
10428
10429 /* OpenMP UDRs have the only argument a reference to the declared
10430 type. We want to diagnose if the declared type is a reference,
10431 which is invalid, but as references to references are usually
10432 quietly merged, diagnose it here. */
10433 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10434 {
10435 tree argtype
10436 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10437 argtype = tsubst (argtype, args, complain, in_decl);
10438 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10439 error_at (DECL_SOURCE_LOCATION (t),
10440 "reference type %qT in "
10441 "%<#pragma omp declare reduction%>", argtype);
10442 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10443 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10444 argtype);
10445 }
10446
10447 if (member && DECL_CONV_FN_P (r))
10448 /* Type-conversion operator. Reconstruct the name, in
10449 case it's the name of one of the template's parameters. */
10450 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10451
10452 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10453 complain, t);
10454 DECL_RESULT (r) = NULL_TREE;
10455
10456 TREE_STATIC (r) = 0;
10457 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10458 DECL_EXTERNAL (r) = 1;
10459 /* If this is an instantiation of a function with internal
10460 linkage, we already know what object file linkage will be
10461 assigned to the instantiation. */
10462 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10463 DECL_DEFER_OUTPUT (r) = 0;
10464 DECL_CHAIN (r) = NULL_TREE;
10465 DECL_PENDING_INLINE_INFO (r) = 0;
10466 DECL_PENDING_INLINE_P (r) = 0;
10467 DECL_SAVED_TREE (r) = NULL_TREE;
10468 DECL_STRUCT_FUNCTION (r) = NULL;
10469 TREE_USED (r) = 0;
10470 /* We'll re-clone as appropriate in instantiate_template. */
10471 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10472
10473 /* If we aren't complaining now, return on error before we register
10474 the specialization so that we'll complain eventually. */
10475 if ((complain & tf_error) == 0
10476 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10477 && !grok_op_properties (r, /*complain=*/false))
10478 RETURN (error_mark_node);
10479
10480 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10481 this in the special friend case mentioned above where
10482 GEN_TMPL is NULL. */
10483 if (gen_tmpl)
10484 {
10485 DECL_TEMPLATE_INFO (r)
10486 = build_template_info (gen_tmpl, argvec);
10487 SET_DECL_IMPLICIT_INSTANTIATION (r);
10488
10489 tree new_r
10490 = register_specialization (r, gen_tmpl, argvec, false, hash);
10491 if (new_r != r)
10492 /* We instantiated this while substituting into
10493 the type earlier (template/friend54.C). */
10494 RETURN (new_r);
10495
10496 /* We're not supposed to instantiate default arguments
10497 until they are called, for a template. But, for a
10498 declaration like:
10499
10500 template <class T> void f ()
10501 { extern void g(int i = T()); }
10502
10503 we should do the substitution when the template is
10504 instantiated. We handle the member function case in
10505 instantiate_class_template since the default arguments
10506 might refer to other members of the class. */
10507 if (!member
10508 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10509 && !uses_template_parms (argvec))
10510 tsubst_default_arguments (r, complain);
10511 }
10512 else
10513 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10514
10515 /* Copy the list of befriending classes. */
10516 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10517 *friends;
10518 friends = &TREE_CHAIN (*friends))
10519 {
10520 *friends = copy_node (*friends);
10521 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10522 args, complain,
10523 in_decl);
10524 }
10525
10526 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10527 {
10528 maybe_retrofit_in_chrg (r);
10529 if (DECL_CONSTRUCTOR_P (r))
10530 grok_ctor_properties (ctx, r);
10531 if (DECL_INHERITED_CTOR_BASE (r))
10532 deduce_inheriting_ctor (r);
10533 /* If this is an instantiation of a member template, clone it.
10534 If it isn't, that'll be handled by
10535 clone_constructors_and_destructors. */
10536 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10537 clone_function_decl (r, /*update_method_vec_p=*/0);
10538 }
10539 else if ((complain & tf_error) != 0
10540 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10541 && !grok_op_properties (r, /*complain=*/true))
10542 RETURN (error_mark_node);
10543
10544 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10545 SET_DECL_FRIEND_CONTEXT (r,
10546 tsubst (DECL_FRIEND_CONTEXT (t),
10547 args, complain, in_decl));
10548
10549 /* Possibly limit visibility based on template args. */
10550 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10551 if (DECL_VISIBILITY_SPECIFIED (t))
10552 {
10553 DECL_VISIBILITY_SPECIFIED (r) = 0;
10554 DECL_ATTRIBUTES (r)
10555 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10556 }
10557 determine_visibility (r);
10558 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10559 && !processing_template_decl)
10560 defaulted_late_check (r);
10561
10562 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10563 args, complain, in_decl);
10564 }
10565 break;
10566
10567 case PARM_DECL:
10568 {
10569 tree type = NULL_TREE;
10570 int i, len = 1;
10571 tree expanded_types = NULL_TREE;
10572 tree prev_r = NULL_TREE;
10573 tree first_r = NULL_TREE;
10574
10575 if (DECL_PACK_P (t))
10576 {
10577 /* If there is a local specialization that isn't a
10578 parameter pack, it means that we're doing a "simple"
10579 substitution from inside tsubst_pack_expansion. Just
10580 return the local specialization (which will be a single
10581 parm). */
10582 tree spec = retrieve_local_specialization (t);
10583 if (spec
10584 && TREE_CODE (spec) == PARM_DECL
10585 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10586 RETURN (spec);
10587
10588 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10589 the parameters in this function parameter pack. */
10590 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10591 complain, in_decl);
10592 if (TREE_CODE (expanded_types) == TREE_VEC)
10593 {
10594 len = TREE_VEC_LENGTH (expanded_types);
10595
10596 /* Zero-length parameter packs are boring. Just substitute
10597 into the chain. */
10598 if (len == 0)
10599 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10600 TREE_CHAIN (t)));
10601 }
10602 else
10603 {
10604 /* All we did was update the type. Make a note of that. */
10605 type = expanded_types;
10606 expanded_types = NULL_TREE;
10607 }
10608 }
10609
10610 /* Loop through all of the parameters we'll build. When T is
10611 a function parameter pack, LEN is the number of expanded
10612 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10613 r = NULL_TREE;
10614 for (i = 0; i < len; ++i)
10615 {
10616 prev_r = r;
10617 r = copy_node (t);
10618 if (DECL_TEMPLATE_PARM_P (t))
10619 SET_DECL_TEMPLATE_PARM_P (r);
10620
10621 if (expanded_types)
10622 /* We're on the Ith parameter of the function parameter
10623 pack. */
10624 {
10625 /* Get the Ith type. */
10626 type = TREE_VEC_ELT (expanded_types, i);
10627
10628 /* Rename the parameter to include the index. */
10629 DECL_NAME (r)
10630 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10631 }
10632 else if (!type)
10633 /* We're dealing with a normal parameter. */
10634 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10635
10636 type = type_decays_to (type);
10637 TREE_TYPE (r) = type;
10638 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10639
10640 if (DECL_INITIAL (r))
10641 {
10642 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10643 DECL_INITIAL (r) = TREE_TYPE (r);
10644 else
10645 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10646 complain, in_decl);
10647 }
10648
10649 DECL_CONTEXT (r) = NULL_TREE;
10650
10651 if (!DECL_TEMPLATE_PARM_P (r))
10652 DECL_ARG_TYPE (r) = type_passed_as (type);
10653
10654 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10655 args, complain, in_decl);
10656
10657 /* Keep track of the first new parameter we
10658 generate. That's what will be returned to the
10659 caller. */
10660 if (!first_r)
10661 first_r = r;
10662
10663 /* Build a proper chain of parameters when substituting
10664 into a function parameter pack. */
10665 if (prev_r)
10666 DECL_CHAIN (prev_r) = r;
10667 }
10668
10669 /* If cp_unevaluated_operand is set, we're just looking for a
10670 single dummy parameter, so don't keep going. */
10671 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10672 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10673 complain, DECL_CHAIN (t));
10674
10675 /* FIRST_R contains the start of the chain we've built. */
10676 r = first_r;
10677 }
10678 break;
10679
10680 case FIELD_DECL:
10681 {
10682 tree type = NULL_TREE;
10683 tree vec = NULL_TREE;
10684 tree expanded_types = NULL_TREE;
10685 int len = 1;
10686
10687 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10688 {
10689 /* This field is a lambda capture pack. Return a TREE_VEC of
10690 the expanded fields to instantiate_class_template_1 and
10691 store them in the specializations hash table as a
10692 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10693 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10694 complain, in_decl);
10695 if (TREE_CODE (expanded_types) == TREE_VEC)
10696 {
10697 len = TREE_VEC_LENGTH (expanded_types);
10698 vec = make_tree_vec (len);
10699 }
10700 else
10701 {
10702 /* All we did was update the type. Make a note of that. */
10703 type = expanded_types;
10704 expanded_types = NULL_TREE;
10705 }
10706 }
10707
10708 for (int i = 0; i < len; ++i)
10709 {
10710 r = copy_decl (t);
10711 if (expanded_types)
10712 {
10713 type = TREE_VEC_ELT (expanded_types, i);
10714 DECL_NAME (r)
10715 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10716 }
10717 else if (!type)
10718 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10719
10720 if (type == error_mark_node)
10721 RETURN (error_mark_node);
10722 TREE_TYPE (r) = type;
10723 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10724
10725 if (DECL_C_BIT_FIELD (r))
10726 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10727 non-bit-fields DECL_INITIAL is a non-static data member
10728 initializer, which gets deferred instantiation. */
10729 DECL_INITIAL (r)
10730 = tsubst_expr (DECL_INITIAL (t), args,
10731 complain, in_decl,
10732 /*integral_constant_expression_p=*/true);
10733 else if (DECL_INITIAL (t))
10734 {
10735 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10736 NSDMI in perform_member_init. Still set DECL_INITIAL
10737 so that we know there is one. */
10738 DECL_INITIAL (r) = void_zero_node;
10739 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10740 retrofit_lang_decl (r);
10741 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10742 }
10743 /* We don't have to set DECL_CONTEXT here; it is set by
10744 finish_member_declaration. */
10745 DECL_CHAIN (r) = NULL_TREE;
10746
10747 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10748 args, complain, in_decl);
10749
10750 if (vec)
10751 TREE_VEC_ELT (vec, i) = r;
10752 }
10753
10754 if (vec)
10755 {
10756 r = vec;
10757 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10758 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10759 SET_ARGUMENT_PACK_ARGS (pack, vec);
10760 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10761 TREE_TYPE (pack) = tpack;
10762 register_specialization (pack, t, args, false, 0);
10763 }
10764 }
10765 break;
10766
10767 case USING_DECL:
10768 /* We reach here only for member using decls. We also need to check
10769 uses_template_parms because DECL_DEPENDENT_P is not set for a
10770 using-declaration that designates a member of the current
10771 instantiation (c++/53549). */
10772 if (DECL_DEPENDENT_P (t)
10773 || uses_template_parms (USING_DECL_SCOPE (t)))
10774 {
10775 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10776 complain, in_decl);
10777 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10778 r = do_class_using_decl (inst_scope, name);
10779 if (!r)
10780 r = error_mark_node;
10781 else
10782 {
10783 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10784 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10785 }
10786 }
10787 else
10788 {
10789 r = copy_node (t);
10790 DECL_CHAIN (r) = NULL_TREE;
10791 }
10792 break;
10793
10794 case TYPE_DECL:
10795 case VAR_DECL:
10796 {
10797 tree argvec = NULL_TREE;
10798 tree gen_tmpl = NULL_TREE;
10799 tree spec;
10800 tree tmpl = NULL_TREE;
10801 tree ctx;
10802 tree type = NULL_TREE;
10803 bool local_p;
10804
10805 if (TREE_CODE (t) == TYPE_DECL
10806 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10807 {
10808 /* If this is the canonical decl, we don't have to
10809 mess with instantiations, and often we can't (for
10810 typename, template type parms and such). Note that
10811 TYPE_NAME is not correct for the above test if
10812 we've copied the type for a typedef. */
10813 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10814 if (type == error_mark_node)
10815 RETURN (error_mark_node);
10816 r = TYPE_NAME (type);
10817 break;
10818 }
10819
10820 /* Check to see if we already have the specialization we
10821 need. */
10822 spec = NULL_TREE;
10823 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10824 {
10825 /* T is a static data member or namespace-scope entity.
10826 We have to substitute into namespace-scope variables
10827 (even though such entities are never templates) because
10828 of cases like:
10829
10830 template <class T> void f() { extern T t; }
10831
10832 where the entity referenced is not known until
10833 instantiation time. */
10834 local_p = false;
10835 ctx = DECL_CONTEXT (t);
10836 if (DECL_CLASS_SCOPE_P (t))
10837 {
10838 ctx = tsubst_aggr_type (ctx, args,
10839 complain,
10840 in_decl, /*entering_scope=*/1);
10841 /* If CTX is unchanged, then T is in fact the
10842 specialization we want. That situation occurs when
10843 referencing a static data member within in its own
10844 class. We can use pointer equality, rather than
10845 same_type_p, because DECL_CONTEXT is always
10846 canonical... */
10847 if (ctx == DECL_CONTEXT (t)
10848 && (TREE_CODE (t) != TYPE_DECL
10849 /* ... unless T is a member template; in which
10850 case our caller can be willing to create a
10851 specialization of that template represented
10852 by T. */
10853 || !(DECL_TI_TEMPLATE (t)
10854 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10855 spec = t;
10856 }
10857
10858 if (!spec)
10859 {
10860 tmpl = DECL_TI_TEMPLATE (t);
10861 gen_tmpl = most_general_template (tmpl);
10862 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10863 if (argvec == error_mark_node)
10864 RETURN (error_mark_node);
10865 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10866 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10867 }
10868 }
10869 else
10870 {
10871 /* A local variable. */
10872 local_p = true;
10873 /* Subsequent calls to pushdecl will fill this in. */
10874 ctx = NULL_TREE;
10875 spec = retrieve_local_specialization (t);
10876 }
10877 /* If we already have the specialization we need, there is
10878 nothing more to do. */
10879 if (spec)
10880 {
10881 r = spec;
10882 break;
10883 }
10884
10885 /* Create a new node for the specialization we need. */
10886 r = copy_decl (t);
10887 if (type == NULL_TREE)
10888 {
10889 if (is_typedef_decl (t))
10890 type = DECL_ORIGINAL_TYPE (t);
10891 else
10892 type = TREE_TYPE (t);
10893 if (VAR_P (t)
10894 && VAR_HAD_UNKNOWN_BOUND (t)
10895 && type != error_mark_node)
10896 type = strip_array_domain (type);
10897 type = tsubst (type, args, complain, in_decl);
10898 }
10899 if (VAR_P (r))
10900 {
10901 /* Even if the original location is out of scope, the
10902 newly substituted one is not. */
10903 DECL_DEAD_FOR_LOCAL (r) = 0;
10904 DECL_INITIALIZED_P (r) = 0;
10905 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10906 if (type == error_mark_node)
10907 RETURN (error_mark_node);
10908 if (TREE_CODE (type) == FUNCTION_TYPE)
10909 {
10910 /* It may seem that this case cannot occur, since:
10911
10912 typedef void f();
10913 void g() { f x; }
10914
10915 declares a function, not a variable. However:
10916
10917 typedef void f();
10918 template <typename T> void g() { T t; }
10919 template void g<f>();
10920
10921 is an attempt to declare a variable with function
10922 type. */
10923 error ("variable %qD has function type",
10924 /* R is not yet sufficiently initialized, so we
10925 just use its name. */
10926 DECL_NAME (r));
10927 RETURN (error_mark_node);
10928 }
10929 type = complete_type (type);
10930 /* Wait until cp_finish_decl to set this again, to handle
10931 circular dependency (template/instantiate6.C). */
10932 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10933 type = check_var_type (DECL_NAME (r), type);
10934
10935 if (DECL_HAS_VALUE_EXPR_P (t))
10936 {
10937 tree ve = DECL_VALUE_EXPR (t);
10938 ve = tsubst_expr (ve, args, complain, in_decl,
10939 /*constant_expression_p=*/false);
10940 if (REFERENCE_REF_P (ve))
10941 {
10942 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10943 ve = TREE_OPERAND (ve, 0);
10944 }
10945 SET_DECL_VALUE_EXPR (r, ve);
10946 }
10947 }
10948 else if (DECL_SELF_REFERENCE_P (t))
10949 SET_DECL_SELF_REFERENCE_P (r);
10950 TREE_TYPE (r) = type;
10951 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10952 DECL_CONTEXT (r) = ctx;
10953 /* Clear out the mangled name and RTL for the instantiation. */
10954 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10955 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10956 SET_DECL_RTL (r, NULL);
10957 /* The initializer must not be expanded until it is required;
10958 see [temp.inst]. */
10959 DECL_INITIAL (r) = NULL_TREE;
10960 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10961 SET_DECL_RTL (r, NULL);
10962 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10963 if (VAR_P (r))
10964 {
10965 /* Possibly limit visibility based on template args. */
10966 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10967 if (DECL_VISIBILITY_SPECIFIED (t))
10968 {
10969 DECL_VISIBILITY_SPECIFIED (r) = 0;
10970 DECL_ATTRIBUTES (r)
10971 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10972 }
10973 determine_visibility (r);
10974 }
10975
10976 if (!local_p)
10977 {
10978 /* A static data member declaration is always marked
10979 external when it is declared in-class, even if an
10980 initializer is present. We mimic the non-template
10981 processing here. */
10982 DECL_EXTERNAL (r) = 1;
10983
10984 register_specialization (r, gen_tmpl, argvec, false, hash);
10985 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10986 SET_DECL_IMPLICIT_INSTANTIATION (r);
10987 }
10988 else if (cp_unevaluated_operand)
10989 gcc_unreachable ();
10990 else
10991 register_local_specialization (r, t);
10992
10993 DECL_CHAIN (r) = NULL_TREE;
10994
10995 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10996 /*flags=*/0,
10997 args, complain, in_decl);
10998
10999 /* Preserve a typedef that names a type. */
11000 if (is_typedef_decl (r))
11001 {
11002 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11003 set_underlying_type (r);
11004 }
11005
11006 layout_decl (r, 0);
11007 }
11008 break;
11009
11010 default:
11011 gcc_unreachable ();
11012 }
11013 #undef RETURN
11014
11015 out:
11016 /* Restore the file and line information. */
11017 input_location = saved_loc;
11018
11019 return r;
11020 }
11021
11022 /* Substitute into the ARG_TYPES of a function type.
11023 If END is a TREE_CHAIN, leave it and any following types
11024 un-substituted. */
11025
11026 static tree
11027 tsubst_arg_types (tree arg_types,
11028 tree args,
11029 tree end,
11030 tsubst_flags_t complain,
11031 tree in_decl)
11032 {
11033 tree remaining_arg_types;
11034 tree type = NULL_TREE;
11035 int i = 1;
11036 tree expanded_args = NULL_TREE;
11037 tree default_arg;
11038
11039 if (!arg_types || arg_types == void_list_node || arg_types == end)
11040 return arg_types;
11041
11042 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11043 args, end, complain, in_decl);
11044 if (remaining_arg_types == error_mark_node)
11045 return error_mark_node;
11046
11047 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11048 {
11049 /* For a pack expansion, perform substitution on the
11050 entire expression. Later on, we'll handle the arguments
11051 one-by-one. */
11052 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11053 args, complain, in_decl);
11054
11055 if (TREE_CODE (expanded_args) == TREE_VEC)
11056 /* So that we'll spin through the parameters, one by one. */
11057 i = TREE_VEC_LENGTH (expanded_args);
11058 else
11059 {
11060 /* We only partially substituted into the parameter
11061 pack. Our type is TYPE_PACK_EXPANSION. */
11062 type = expanded_args;
11063 expanded_args = NULL_TREE;
11064 }
11065 }
11066
11067 while (i > 0) {
11068 --i;
11069
11070 if (expanded_args)
11071 type = TREE_VEC_ELT (expanded_args, i);
11072 else if (!type)
11073 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11074
11075 if (type == error_mark_node)
11076 return error_mark_node;
11077 if (VOID_TYPE_P (type))
11078 {
11079 if (complain & tf_error)
11080 {
11081 error ("invalid parameter type %qT", type);
11082 if (in_decl)
11083 error ("in declaration %q+D", in_decl);
11084 }
11085 return error_mark_node;
11086 }
11087 /* DR 657. */
11088 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11089 return error_mark_node;
11090
11091 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11092 top-level qualifiers as required. */
11093 type = cv_unqualified (type_decays_to (type));
11094
11095 /* We do not substitute into default arguments here. The standard
11096 mandates that they be instantiated only when needed, which is
11097 done in build_over_call. */
11098 default_arg = TREE_PURPOSE (arg_types);
11099
11100 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11101 {
11102 /* We've instantiated a template before its default arguments
11103 have been parsed. This can happen for a nested template
11104 class, and is not an error unless we require the default
11105 argument in a call of this function. */
11106 remaining_arg_types =
11107 tree_cons (default_arg, type, remaining_arg_types);
11108 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11109 }
11110 else
11111 remaining_arg_types =
11112 hash_tree_cons (default_arg, type, remaining_arg_types);
11113 }
11114
11115 return remaining_arg_types;
11116 }
11117
11118 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11119 *not* handle the exception-specification for FNTYPE, because the
11120 initial substitution of explicitly provided template parameters
11121 during argument deduction forbids substitution into the
11122 exception-specification:
11123
11124 [temp.deduct]
11125
11126 All references in the function type of the function template to the
11127 corresponding template parameters are replaced by the specified tem-
11128 plate argument values. If a substitution in a template parameter or
11129 in the function type of the function template results in an invalid
11130 type, type deduction fails. [Note: The equivalent substitution in
11131 exception specifications is done only when the function is instanti-
11132 ated, at which point a program is ill-formed if the substitution
11133 results in an invalid type.] */
11134
11135 static tree
11136 tsubst_function_type (tree t,
11137 tree args,
11138 tsubst_flags_t complain,
11139 tree in_decl)
11140 {
11141 tree return_type;
11142 tree arg_types;
11143 tree fntype;
11144
11145 /* The TYPE_CONTEXT is not used for function/method types. */
11146 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11147
11148 /* Substitute the return type. */
11149 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11150 if (return_type == error_mark_node)
11151 return error_mark_node;
11152 /* DR 486 clarifies that creation of a function type with an
11153 invalid return type is a deduction failure. */
11154 if (TREE_CODE (return_type) == ARRAY_TYPE
11155 || TREE_CODE (return_type) == FUNCTION_TYPE)
11156 {
11157 if (complain & tf_error)
11158 {
11159 if (TREE_CODE (return_type) == ARRAY_TYPE)
11160 error ("function returning an array");
11161 else
11162 error ("function returning a function");
11163 }
11164 return error_mark_node;
11165 }
11166 /* And DR 657. */
11167 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11168 return error_mark_node;
11169
11170 /* Substitute the argument types. */
11171 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11172 complain, in_decl);
11173 if (arg_types == error_mark_node)
11174 return error_mark_node;
11175
11176 /* Construct a new type node and return it. */
11177 if (TREE_CODE (t) == FUNCTION_TYPE)
11178 {
11179 fntype = build_function_type (return_type, arg_types);
11180 fntype = apply_memfn_quals (fntype,
11181 type_memfn_quals (t),
11182 type_memfn_rqual (t));
11183 }
11184 else
11185 {
11186 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11187 if (! MAYBE_CLASS_TYPE_P (r))
11188 {
11189 /* [temp.deduct]
11190
11191 Type deduction may fail for any of the following
11192 reasons:
11193
11194 -- Attempting to create "pointer to member of T" when T
11195 is not a class type. */
11196 if (complain & tf_error)
11197 error ("creating pointer to member function of non-class type %qT",
11198 r);
11199 return error_mark_node;
11200 }
11201
11202 fntype = build_method_type_directly (r, return_type,
11203 TREE_CHAIN (arg_types));
11204 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11205 }
11206 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11207
11208 return fntype;
11209 }
11210
11211 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11212 ARGS into that specification, and return the substituted
11213 specification. If there is no specification, return NULL_TREE. */
11214
11215 static tree
11216 tsubst_exception_specification (tree fntype,
11217 tree args,
11218 tsubst_flags_t complain,
11219 tree in_decl,
11220 bool defer_ok)
11221 {
11222 tree specs;
11223 tree new_specs;
11224
11225 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11226 new_specs = NULL_TREE;
11227 if (specs && TREE_PURPOSE (specs))
11228 {
11229 /* A noexcept-specifier. */
11230 tree expr = TREE_PURPOSE (specs);
11231 if (TREE_CODE (expr) == INTEGER_CST)
11232 new_specs = expr;
11233 else if (defer_ok)
11234 {
11235 /* Defer instantiation of noexcept-specifiers to avoid
11236 excessive instantiations (c++/49107). */
11237 new_specs = make_node (DEFERRED_NOEXCEPT);
11238 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11239 {
11240 /* We already partially instantiated this member template,
11241 so combine the new args with the old. */
11242 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11243 = DEFERRED_NOEXCEPT_PATTERN (expr);
11244 DEFERRED_NOEXCEPT_ARGS (new_specs)
11245 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11246 }
11247 else
11248 {
11249 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11250 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11251 }
11252 }
11253 else
11254 new_specs = tsubst_copy_and_build
11255 (expr, args, complain, in_decl, /*function_p=*/false,
11256 /*integral_constant_expression_p=*/true);
11257 new_specs = build_noexcept_spec (new_specs, complain);
11258 }
11259 else if (specs)
11260 {
11261 if (! TREE_VALUE (specs))
11262 new_specs = specs;
11263 else
11264 while (specs)
11265 {
11266 tree spec;
11267 int i, len = 1;
11268 tree expanded_specs = NULL_TREE;
11269
11270 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11271 {
11272 /* Expand the pack expansion type. */
11273 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11274 args, complain,
11275 in_decl);
11276
11277 if (expanded_specs == error_mark_node)
11278 return error_mark_node;
11279 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11280 len = TREE_VEC_LENGTH (expanded_specs);
11281 else
11282 {
11283 /* We're substituting into a member template, so
11284 we got a TYPE_PACK_EXPANSION back. Add that
11285 expansion and move on. */
11286 gcc_assert (TREE_CODE (expanded_specs)
11287 == TYPE_PACK_EXPANSION);
11288 new_specs = add_exception_specifier (new_specs,
11289 expanded_specs,
11290 complain);
11291 specs = TREE_CHAIN (specs);
11292 continue;
11293 }
11294 }
11295
11296 for (i = 0; i < len; ++i)
11297 {
11298 if (expanded_specs)
11299 spec = TREE_VEC_ELT (expanded_specs, i);
11300 else
11301 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11302 if (spec == error_mark_node)
11303 return spec;
11304 new_specs = add_exception_specifier (new_specs, spec,
11305 complain);
11306 }
11307
11308 specs = TREE_CHAIN (specs);
11309 }
11310 }
11311 return new_specs;
11312 }
11313
11314 /* Take the tree structure T and replace template parameters used
11315 therein with the argument vector ARGS. IN_DECL is an associated
11316 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11317 Issue error and warning messages under control of COMPLAIN. Note
11318 that we must be relatively non-tolerant of extensions here, in
11319 order to preserve conformance; if we allow substitutions that
11320 should not be allowed, we may allow argument deductions that should
11321 not succeed, and therefore report ambiguous overload situations
11322 where there are none. In theory, we could allow the substitution,
11323 but indicate that it should have failed, and allow our caller to
11324 make sure that the right thing happens, but we don't try to do this
11325 yet.
11326
11327 This function is used for dealing with types, decls and the like;
11328 for expressions, use tsubst_expr or tsubst_copy. */
11329
11330 tree
11331 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11332 {
11333 enum tree_code code;
11334 tree type, r = NULL_TREE;
11335
11336 if (t == NULL_TREE || t == error_mark_node
11337 || t == integer_type_node
11338 || t == void_type_node
11339 || t == char_type_node
11340 || t == unknown_type_node
11341 || TREE_CODE (t) == NAMESPACE_DECL
11342 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11343 return t;
11344
11345 if (DECL_P (t))
11346 return tsubst_decl (t, args, complain);
11347
11348 if (args == NULL_TREE)
11349 return t;
11350
11351 code = TREE_CODE (t);
11352
11353 if (code == IDENTIFIER_NODE)
11354 type = IDENTIFIER_TYPE_VALUE (t);
11355 else
11356 type = TREE_TYPE (t);
11357
11358 gcc_assert (type != unknown_type_node);
11359
11360 /* Reuse typedefs. We need to do this to handle dependent attributes,
11361 such as attribute aligned. */
11362 if (TYPE_P (t)
11363 && typedef_variant_p (t))
11364 {
11365 tree decl = TYPE_NAME (t);
11366
11367 if (alias_template_specialization_p (t))
11368 {
11369 /* DECL represents an alias template and we want to
11370 instantiate it. */
11371 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11372 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11373 r = instantiate_alias_template (tmpl, gen_args, complain);
11374 }
11375 else if (DECL_CLASS_SCOPE_P (decl)
11376 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11377 && uses_template_parms (DECL_CONTEXT (decl)))
11378 {
11379 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11380 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11381 r = retrieve_specialization (tmpl, gen_args, 0);
11382 }
11383 else if (DECL_FUNCTION_SCOPE_P (decl)
11384 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11385 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11386 r = retrieve_local_specialization (decl);
11387 else
11388 /* The typedef is from a non-template context. */
11389 return t;
11390
11391 if (r)
11392 {
11393 r = TREE_TYPE (r);
11394 r = cp_build_qualified_type_real
11395 (r, cp_type_quals (t) | cp_type_quals (r),
11396 complain | tf_ignore_bad_quals);
11397 return r;
11398 }
11399 else
11400 {
11401 /* We don't have an instantiation yet, so drop the typedef. */
11402 int quals = cp_type_quals (t);
11403 t = DECL_ORIGINAL_TYPE (decl);
11404 t = cp_build_qualified_type_real (t, quals,
11405 complain | tf_ignore_bad_quals);
11406 }
11407 }
11408
11409 if (type
11410 && code != TYPENAME_TYPE
11411 && code != TEMPLATE_TYPE_PARM
11412 && code != IDENTIFIER_NODE
11413 && code != FUNCTION_TYPE
11414 && code != METHOD_TYPE)
11415 type = tsubst (type, args, complain, in_decl);
11416 if (type == error_mark_node)
11417 return error_mark_node;
11418
11419 switch (code)
11420 {
11421 case RECORD_TYPE:
11422 case UNION_TYPE:
11423 case ENUMERAL_TYPE:
11424 return tsubst_aggr_type (t, args, complain, in_decl,
11425 /*entering_scope=*/0);
11426
11427 case ERROR_MARK:
11428 case IDENTIFIER_NODE:
11429 case VOID_TYPE:
11430 case REAL_TYPE:
11431 case COMPLEX_TYPE:
11432 case VECTOR_TYPE:
11433 case BOOLEAN_TYPE:
11434 case NULLPTR_TYPE:
11435 case LANG_TYPE:
11436 return t;
11437
11438 case INTEGER_TYPE:
11439 if (t == integer_type_node)
11440 return t;
11441
11442 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11443 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11444 return t;
11445
11446 {
11447 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11448
11449 max = tsubst_expr (omax, args, complain, in_decl,
11450 /*integral_constant_expression_p=*/false);
11451
11452 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11453 needed. */
11454 if (TREE_CODE (max) == NOP_EXPR
11455 && TREE_SIDE_EFFECTS (omax)
11456 && !TREE_TYPE (max))
11457 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11458
11459 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11460 with TREE_SIDE_EFFECTS that indicates this is not an integral
11461 constant expression. */
11462 if (processing_template_decl
11463 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11464 {
11465 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11466 TREE_SIDE_EFFECTS (max) = 1;
11467 }
11468
11469 return compute_array_index_type (NULL_TREE, max, complain);
11470 }
11471
11472 case TEMPLATE_TYPE_PARM:
11473 case TEMPLATE_TEMPLATE_PARM:
11474 case BOUND_TEMPLATE_TEMPLATE_PARM:
11475 case TEMPLATE_PARM_INDEX:
11476 {
11477 int idx;
11478 int level;
11479 int levels;
11480 tree arg = NULL_TREE;
11481
11482 r = NULL_TREE;
11483
11484 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11485 template_parm_level_and_index (t, &level, &idx);
11486
11487 levels = TMPL_ARGS_DEPTH (args);
11488 if (level <= levels)
11489 {
11490 arg = TMPL_ARG (args, level, idx);
11491
11492 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11493 {
11494 /* See through ARGUMENT_PACK_SELECT arguments. */
11495 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11496 /* If the selected argument is an expansion E, that most
11497 likely means we were called from
11498 gen_elem_of_pack_expansion_instantiation during the
11499 substituting of pack an argument pack (which Ith
11500 element is a pack expansion, where I is
11501 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11502 In this case, the Ith element resulting from this
11503 substituting is going to be a pack expansion, which
11504 pattern is the pattern of E. Let's return the
11505 pattern of E, and
11506 gen_elem_of_pack_expansion_instantiation will
11507 build the resulting pack expansion from it. */
11508 if (PACK_EXPANSION_P (arg))
11509 arg = PACK_EXPANSION_PATTERN (arg);
11510 }
11511 }
11512
11513 if (arg == error_mark_node)
11514 return error_mark_node;
11515 else if (arg != NULL_TREE)
11516 {
11517 if (ARGUMENT_PACK_P (arg))
11518 /* If ARG is an argument pack, we don't actually want to
11519 perform a substitution here, because substitutions
11520 for argument packs are only done
11521 element-by-element. We can get to this point when
11522 substituting the type of a non-type template
11523 parameter pack, when that type actually contains
11524 template parameter packs from an outer template, e.g.,
11525
11526 template<typename... Types> struct A {
11527 template<Types... Values> struct B { };
11528 }; */
11529 return t;
11530
11531 if (code == TEMPLATE_TYPE_PARM)
11532 {
11533 int quals;
11534 gcc_assert (TYPE_P (arg));
11535
11536 quals = cp_type_quals (arg) | cp_type_quals (t);
11537
11538 return cp_build_qualified_type_real
11539 (arg, quals, complain | tf_ignore_bad_quals);
11540 }
11541 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11542 {
11543 /* We are processing a type constructed from a
11544 template template parameter. */
11545 tree argvec = tsubst (TYPE_TI_ARGS (t),
11546 args, complain, in_decl);
11547 if (argvec == error_mark_node)
11548 return error_mark_node;
11549
11550 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11551 || TREE_CODE (arg) == TEMPLATE_DECL
11552 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11553
11554 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11555 /* Consider this code:
11556
11557 template <template <class> class Template>
11558 struct Internal {
11559 template <class Arg> using Bind = Template<Arg>;
11560 };
11561
11562 template <template <class> class Template, class Arg>
11563 using Instantiate = Template<Arg>; //#0
11564
11565 template <template <class> class Template,
11566 class Argument>
11567 using Bind =
11568 Instantiate<Internal<Template>::template Bind,
11569 Argument>; //#1
11570
11571 When #1 is parsed, the
11572 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11573 parameter `Template' in #0 matches the
11574 UNBOUND_CLASS_TEMPLATE representing the argument
11575 `Internal<Template>::template Bind'; We then want
11576 to assemble the type `Bind<Argument>' that can't
11577 be fully created right now, because
11578 `Internal<Template>' not being complete, the Bind
11579 template cannot be looked up in that context. So
11580 we need to "store" `Bind<Argument>' for later
11581 when the context of Bind becomes complete. Let's
11582 store that in a TYPENAME_TYPE. */
11583 return make_typename_type (TYPE_CONTEXT (arg),
11584 build_nt (TEMPLATE_ID_EXPR,
11585 TYPE_IDENTIFIER (arg),
11586 argvec),
11587 typename_type,
11588 complain);
11589
11590 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11591 are resolving nested-types in the signature of a
11592 member function templates. Otherwise ARG is a
11593 TEMPLATE_DECL and is the real template to be
11594 instantiated. */
11595 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11596 arg = TYPE_NAME (arg);
11597
11598 r = lookup_template_class (arg,
11599 argvec, in_decl,
11600 DECL_CONTEXT (arg),
11601 /*entering_scope=*/0,
11602 complain);
11603 return cp_build_qualified_type_real
11604 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11605 }
11606 else
11607 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11608 return convert_from_reference (unshare_expr (arg));
11609 }
11610
11611 if (level == 1)
11612 /* This can happen during the attempted tsubst'ing in
11613 unify. This means that we don't yet have any information
11614 about the template parameter in question. */
11615 return t;
11616
11617 /* Early in template argument deduction substitution, we don't
11618 want to reduce the level of 'auto', or it will be confused
11619 with a normal template parm in subsequent deduction. */
11620 if (is_auto (t) && (complain & tf_partial))
11621 return t;
11622
11623 /* If we get here, we must have been looking at a parm for a
11624 more deeply nested template. Make a new version of this
11625 template parameter, but with a lower level. */
11626 switch (code)
11627 {
11628 case TEMPLATE_TYPE_PARM:
11629 case TEMPLATE_TEMPLATE_PARM:
11630 case BOUND_TEMPLATE_TEMPLATE_PARM:
11631 if (cp_type_quals (t))
11632 {
11633 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11634 r = cp_build_qualified_type_real
11635 (r, cp_type_quals (t),
11636 complain | (code == TEMPLATE_TYPE_PARM
11637 ? tf_ignore_bad_quals : 0));
11638 }
11639 else
11640 {
11641 r = copy_type (t);
11642 TEMPLATE_TYPE_PARM_INDEX (r)
11643 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11644 r, levels, args, complain);
11645 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11646 TYPE_MAIN_VARIANT (r) = r;
11647 TYPE_POINTER_TO (r) = NULL_TREE;
11648 TYPE_REFERENCE_TO (r) = NULL_TREE;
11649
11650 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11651 /* We have reduced the level of the template
11652 template parameter, but not the levels of its
11653 template parameters, so canonical_type_parameter
11654 will not be able to find the canonical template
11655 template parameter for this level. Thus, we
11656 require structural equality checking to compare
11657 TEMPLATE_TEMPLATE_PARMs. */
11658 SET_TYPE_STRUCTURAL_EQUALITY (r);
11659 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11660 SET_TYPE_STRUCTURAL_EQUALITY (r);
11661 else
11662 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11663
11664 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11665 {
11666 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11667 complain, in_decl);
11668 if (argvec == error_mark_node)
11669 return error_mark_node;
11670
11671 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11672 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11673 }
11674 }
11675 break;
11676
11677 case TEMPLATE_PARM_INDEX:
11678 r = reduce_template_parm_level (t, type, levels, args, complain);
11679 break;
11680
11681 default:
11682 gcc_unreachable ();
11683 }
11684
11685 return r;
11686 }
11687
11688 case TREE_LIST:
11689 {
11690 tree purpose, value, chain;
11691
11692 if (t == void_list_node)
11693 return t;
11694
11695 purpose = TREE_PURPOSE (t);
11696 if (purpose)
11697 {
11698 purpose = tsubst (purpose, args, complain, in_decl);
11699 if (purpose == error_mark_node)
11700 return error_mark_node;
11701 }
11702 value = TREE_VALUE (t);
11703 if (value)
11704 {
11705 value = tsubst (value, args, complain, in_decl);
11706 if (value == error_mark_node)
11707 return error_mark_node;
11708 }
11709 chain = TREE_CHAIN (t);
11710 if (chain && chain != void_type_node)
11711 {
11712 chain = tsubst (chain, args, complain, in_decl);
11713 if (chain == error_mark_node)
11714 return error_mark_node;
11715 }
11716 if (purpose == TREE_PURPOSE (t)
11717 && value == TREE_VALUE (t)
11718 && chain == TREE_CHAIN (t))
11719 return t;
11720 return hash_tree_cons (purpose, value, chain);
11721 }
11722
11723 case TREE_BINFO:
11724 /* We should never be tsubsting a binfo. */
11725 gcc_unreachable ();
11726
11727 case TREE_VEC:
11728 /* A vector of template arguments. */
11729 gcc_assert (!type);
11730 return tsubst_template_args (t, args, complain, in_decl);
11731
11732 case POINTER_TYPE:
11733 case REFERENCE_TYPE:
11734 {
11735 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11736 return t;
11737
11738 /* [temp.deduct]
11739
11740 Type deduction may fail for any of the following
11741 reasons:
11742
11743 -- Attempting to create a pointer to reference type.
11744 -- Attempting to create a reference to a reference type or
11745 a reference to void.
11746
11747 Core issue 106 says that creating a reference to a reference
11748 during instantiation is no longer a cause for failure. We
11749 only enforce this check in strict C++98 mode. */
11750 if ((TREE_CODE (type) == REFERENCE_TYPE
11751 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11752 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11753 {
11754 static location_t last_loc;
11755
11756 /* We keep track of the last time we issued this error
11757 message to avoid spewing a ton of messages during a
11758 single bad template instantiation. */
11759 if (complain & tf_error
11760 && last_loc != input_location)
11761 {
11762 if (VOID_TYPE_P (type))
11763 error ("forming reference to void");
11764 else if (code == POINTER_TYPE)
11765 error ("forming pointer to reference type %qT", type);
11766 else
11767 error ("forming reference to reference type %qT", type);
11768 last_loc = input_location;
11769 }
11770
11771 return error_mark_node;
11772 }
11773 else if (TREE_CODE (type) == FUNCTION_TYPE
11774 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11775 || type_memfn_rqual (type) != REF_QUAL_NONE))
11776 {
11777 if (complain & tf_error)
11778 {
11779 if (code == POINTER_TYPE)
11780 error ("forming pointer to qualified function type %qT",
11781 type);
11782 else
11783 error ("forming reference to qualified function type %qT",
11784 type);
11785 }
11786 return error_mark_node;
11787 }
11788 else if (code == POINTER_TYPE)
11789 {
11790 r = build_pointer_type (type);
11791 if (TREE_CODE (type) == METHOD_TYPE)
11792 r = build_ptrmemfunc_type (r);
11793 }
11794 else if (TREE_CODE (type) == REFERENCE_TYPE)
11795 /* In C++0x, during template argument substitution, when there is an
11796 attempt to create a reference to a reference type, reference
11797 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11798
11799 "If a template-argument for a template-parameter T names a type
11800 that is a reference to a type A, an attempt to create the type
11801 'lvalue reference to cv T' creates the type 'lvalue reference to
11802 A,' while an attempt to create the type type rvalue reference to
11803 cv T' creates the type T"
11804 */
11805 r = cp_build_reference_type
11806 (TREE_TYPE (type),
11807 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11808 else
11809 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11810 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11811
11812 if (cxx_dialect >= cxx1y
11813 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11814 && array_of_runtime_bound_p (type))
11815 {
11816 if (complain & tf_warning_or_error)
11817 pedwarn
11818 (input_location, OPT_Wvla,
11819 code == REFERENCE_TYPE
11820 ? G_("cannot declare reference to array of runtime bound")
11821 : G_("cannot declare pointer to array of runtime bound"));
11822 else
11823 r = error_mark_node;
11824 }
11825
11826 if (r != error_mark_node)
11827 /* Will this ever be needed for TYPE_..._TO values? */
11828 layout_type (r);
11829
11830 return r;
11831 }
11832 case OFFSET_TYPE:
11833 {
11834 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11835 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11836 {
11837 /* [temp.deduct]
11838
11839 Type deduction may fail for any of the following
11840 reasons:
11841
11842 -- Attempting to create "pointer to member of T" when T
11843 is not a class type. */
11844 if (complain & tf_error)
11845 error ("creating pointer to member of non-class type %qT", r);
11846 return error_mark_node;
11847 }
11848 if (TREE_CODE (type) == REFERENCE_TYPE)
11849 {
11850 if (complain & tf_error)
11851 error ("creating pointer to member reference type %qT", type);
11852 return error_mark_node;
11853 }
11854 if (VOID_TYPE_P (type))
11855 {
11856 if (complain & tf_error)
11857 error ("creating pointer to member of type void");
11858 return error_mark_node;
11859 }
11860 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11861 if (TREE_CODE (type) == FUNCTION_TYPE)
11862 {
11863 /* The type of the implicit object parameter gets its
11864 cv-qualifiers from the FUNCTION_TYPE. */
11865 tree memptr;
11866 tree method_type
11867 = build_memfn_type (type, r, type_memfn_quals (type),
11868 type_memfn_rqual (type));
11869 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11870 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11871 complain);
11872 }
11873 else
11874 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11875 cp_type_quals (t),
11876 complain);
11877 }
11878 case FUNCTION_TYPE:
11879 case METHOD_TYPE:
11880 {
11881 tree fntype;
11882 tree specs;
11883 fntype = tsubst_function_type (t, args, complain, in_decl);
11884 if (fntype == error_mark_node)
11885 return error_mark_node;
11886
11887 /* Substitute the exception specification. */
11888 specs = tsubst_exception_specification (t, args, complain,
11889 in_decl, /*defer_ok*/true);
11890 if (specs == error_mark_node)
11891 return error_mark_node;
11892 if (specs)
11893 fntype = build_exception_variant (fntype, specs);
11894 return fntype;
11895 }
11896 case ARRAY_TYPE:
11897 {
11898 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11899 if (domain == error_mark_node)
11900 return error_mark_node;
11901
11902 /* As an optimization, we avoid regenerating the array type if
11903 it will obviously be the same as T. */
11904 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11905 return t;
11906
11907 /* These checks should match the ones in grokdeclarator.
11908
11909 [temp.deduct]
11910
11911 The deduction may fail for any of the following reasons:
11912
11913 -- Attempting to create an array with an element type that
11914 is void, a function type, or a reference type, or [DR337]
11915 an abstract class type. */
11916 if (VOID_TYPE_P (type)
11917 || TREE_CODE (type) == FUNCTION_TYPE
11918 || TREE_CODE (type) == REFERENCE_TYPE)
11919 {
11920 if (complain & tf_error)
11921 error ("creating array of %qT", type);
11922 return error_mark_node;
11923 }
11924
11925 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11926 return error_mark_node;
11927
11928 r = build_cplus_array_type (type, domain);
11929
11930 if (TYPE_USER_ALIGN (t))
11931 {
11932 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11933 TYPE_USER_ALIGN (r) = 1;
11934 }
11935
11936 return r;
11937 }
11938
11939 case TYPENAME_TYPE:
11940 {
11941 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11942 in_decl, /*entering_scope=*/1);
11943 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11944 complain, in_decl);
11945
11946 if (ctx == error_mark_node || f == error_mark_node)
11947 return error_mark_node;
11948
11949 if (!MAYBE_CLASS_TYPE_P (ctx))
11950 {
11951 if (complain & tf_error)
11952 error ("%qT is not a class, struct, or union type", ctx);
11953 return error_mark_node;
11954 }
11955 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11956 {
11957 /* Normally, make_typename_type does not require that the CTX
11958 have complete type in order to allow things like:
11959
11960 template <class T> struct S { typename S<T>::X Y; };
11961
11962 But, such constructs have already been resolved by this
11963 point, so here CTX really should have complete type, unless
11964 it's a partial instantiation. */
11965 ctx = complete_type (ctx);
11966 if (!COMPLETE_TYPE_P (ctx))
11967 {
11968 if (complain & tf_error)
11969 cxx_incomplete_type_error (NULL_TREE, ctx);
11970 return error_mark_node;
11971 }
11972 }
11973
11974 f = make_typename_type (ctx, f, typename_type,
11975 complain | tf_keep_type_decl);
11976 if (f == error_mark_node)
11977 return f;
11978 if (TREE_CODE (f) == TYPE_DECL)
11979 {
11980 complain |= tf_ignore_bad_quals;
11981 f = TREE_TYPE (f);
11982 }
11983
11984 if (TREE_CODE (f) != TYPENAME_TYPE)
11985 {
11986 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11987 {
11988 if (complain & tf_error)
11989 error ("%qT resolves to %qT, which is not an enumeration type",
11990 t, f);
11991 else
11992 return error_mark_node;
11993 }
11994 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11995 {
11996 if (complain & tf_error)
11997 error ("%qT resolves to %qT, which is is not a class type",
11998 t, f);
11999 else
12000 return error_mark_node;
12001 }
12002 }
12003
12004 return cp_build_qualified_type_real
12005 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12006 }
12007
12008 case UNBOUND_CLASS_TEMPLATE:
12009 {
12010 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12011 in_decl, /*entering_scope=*/1);
12012 tree name = TYPE_IDENTIFIER (t);
12013 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12014
12015 if (ctx == error_mark_node || name == error_mark_node)
12016 return error_mark_node;
12017
12018 if (parm_list)
12019 parm_list = tsubst_template_parms (parm_list, args, complain);
12020 return make_unbound_class_template (ctx, name, parm_list, complain);
12021 }
12022
12023 case TYPEOF_TYPE:
12024 {
12025 tree type;
12026
12027 ++cp_unevaluated_operand;
12028 ++c_inhibit_evaluation_warnings;
12029
12030 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12031 complain, in_decl,
12032 /*integral_constant_expression_p=*/false);
12033
12034 --cp_unevaluated_operand;
12035 --c_inhibit_evaluation_warnings;
12036
12037 type = finish_typeof (type);
12038 return cp_build_qualified_type_real (type,
12039 cp_type_quals (t)
12040 | cp_type_quals (type),
12041 complain);
12042 }
12043
12044 case DECLTYPE_TYPE:
12045 {
12046 tree type;
12047
12048 ++cp_unevaluated_operand;
12049 ++c_inhibit_evaluation_warnings;
12050
12051 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12052 complain|tf_decltype, in_decl,
12053 /*function_p*/false,
12054 /*integral_constant_expression*/false);
12055
12056 --cp_unevaluated_operand;
12057 --c_inhibit_evaluation_warnings;
12058
12059 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12060 type = lambda_capture_field_type (type,
12061 DECLTYPE_FOR_INIT_CAPTURE (t));
12062 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12063 type = lambda_proxy_type (type);
12064 else
12065 {
12066 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12067 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12068 && EXPR_P (type))
12069 /* In a template ~id could be either a complement expression
12070 or an unqualified-id naming a destructor; if instantiating
12071 it produces an expression, it's not an id-expression or
12072 member access. */
12073 id = false;
12074 type = finish_decltype_type (type, id, complain);
12075 }
12076 return cp_build_qualified_type_real (type,
12077 cp_type_quals (t)
12078 | cp_type_quals (type),
12079 complain);
12080 }
12081
12082 case UNDERLYING_TYPE:
12083 {
12084 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12085 complain, in_decl);
12086 return finish_underlying_type (type);
12087 }
12088
12089 case TYPE_ARGUMENT_PACK:
12090 case NONTYPE_ARGUMENT_PACK:
12091 {
12092 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12093 tree packed_out =
12094 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12095 args,
12096 complain,
12097 in_decl);
12098 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12099
12100 /* For template nontype argument packs, also substitute into
12101 the type. */
12102 if (code == NONTYPE_ARGUMENT_PACK)
12103 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12104
12105 return r;
12106 }
12107 break;
12108
12109 case INTEGER_CST:
12110 case REAL_CST:
12111 case STRING_CST:
12112 case PLUS_EXPR:
12113 case MINUS_EXPR:
12114 case NEGATE_EXPR:
12115 case NOP_EXPR:
12116 case INDIRECT_REF:
12117 case ADDR_EXPR:
12118 case CALL_EXPR:
12119 case ARRAY_REF:
12120 case SCOPE_REF:
12121 /* We should use one of the expression tsubsts for these codes. */
12122 gcc_unreachable ();
12123
12124 default:
12125 sorry ("use of %qs in template", get_tree_code_name (code));
12126 return error_mark_node;
12127 }
12128 }
12129
12130 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12131 type of the expression on the left-hand side of the "." or "->"
12132 operator. */
12133
12134 static tree
12135 tsubst_baselink (tree baselink, tree object_type,
12136 tree args, tsubst_flags_t complain, tree in_decl)
12137 {
12138 tree name;
12139 tree qualifying_scope;
12140 tree fns;
12141 tree optype;
12142 tree template_args = 0;
12143 bool template_id_p = false;
12144 bool qualified = BASELINK_QUALIFIED_P (baselink);
12145
12146 /* A baselink indicates a function from a base class. Both the
12147 BASELINK_ACCESS_BINFO and the base class referenced may
12148 indicate bases of the template class, rather than the
12149 instantiated class. In addition, lookups that were not
12150 ambiguous before may be ambiguous now. Therefore, we perform
12151 the lookup again. */
12152 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12153 qualifying_scope = tsubst (qualifying_scope, args,
12154 complain, in_decl);
12155 fns = BASELINK_FUNCTIONS (baselink);
12156 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12157 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12158 {
12159 template_id_p = true;
12160 template_args = TREE_OPERAND (fns, 1);
12161 fns = TREE_OPERAND (fns, 0);
12162 if (template_args)
12163 template_args = tsubst_template_args (template_args, args,
12164 complain, in_decl);
12165 }
12166 name = DECL_NAME (get_first_fn (fns));
12167 if (IDENTIFIER_TYPENAME_P (name))
12168 name = mangle_conv_op_name_for_type (optype);
12169 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12170 if (!baselink)
12171 return error_mark_node;
12172
12173 /* If lookup found a single function, mark it as used at this
12174 point. (If it lookup found multiple functions the one selected
12175 later by overload resolution will be marked as used at that
12176 point.) */
12177 if (BASELINK_P (baselink))
12178 fns = BASELINK_FUNCTIONS (baselink);
12179 if (!template_id_p && !really_overloaded_fn (fns))
12180 mark_used (OVL_CURRENT (fns));
12181
12182 /* Add back the template arguments, if present. */
12183 if (BASELINK_P (baselink) && template_id_p)
12184 BASELINK_FUNCTIONS (baselink)
12185 = build_nt (TEMPLATE_ID_EXPR,
12186 BASELINK_FUNCTIONS (baselink),
12187 template_args);
12188 /* Update the conversion operator type. */
12189 BASELINK_OPTYPE (baselink) = optype;
12190
12191 if (!object_type)
12192 object_type = current_class_type;
12193
12194 if (qualified)
12195 baselink = adjust_result_of_qualified_name_lookup (baselink,
12196 qualifying_scope,
12197 object_type);
12198 return baselink;
12199 }
12200
12201 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12202 true if the qualified-id will be a postfix-expression in-and-of
12203 itself; false if more of the postfix-expression follows the
12204 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12205 of "&". */
12206
12207 static tree
12208 tsubst_qualified_id (tree qualified_id, tree args,
12209 tsubst_flags_t complain, tree in_decl,
12210 bool done, bool address_p)
12211 {
12212 tree expr;
12213 tree scope;
12214 tree name;
12215 bool is_template;
12216 tree template_args;
12217 location_t loc = UNKNOWN_LOCATION;
12218
12219 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12220
12221 /* Figure out what name to look up. */
12222 name = TREE_OPERAND (qualified_id, 1);
12223 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12224 {
12225 is_template = true;
12226 loc = EXPR_LOCATION (name);
12227 template_args = TREE_OPERAND (name, 1);
12228 if (template_args)
12229 template_args = tsubst_template_args (template_args, args,
12230 complain, in_decl);
12231 name = TREE_OPERAND (name, 0);
12232 }
12233 else
12234 {
12235 is_template = false;
12236 template_args = NULL_TREE;
12237 }
12238
12239 /* Substitute into the qualifying scope. When there are no ARGS, we
12240 are just trying to simplify a non-dependent expression. In that
12241 case the qualifying scope may be dependent, and, in any case,
12242 substituting will not help. */
12243 scope = TREE_OPERAND (qualified_id, 0);
12244 if (args)
12245 {
12246 scope = tsubst (scope, args, complain, in_decl);
12247 expr = tsubst_copy (name, args, complain, in_decl);
12248 }
12249 else
12250 expr = name;
12251
12252 if (dependent_scope_p (scope))
12253 {
12254 if (is_template)
12255 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12256 return build_qualified_name (NULL_TREE, scope, expr,
12257 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12258 }
12259
12260 if (!BASELINK_P (name) && !DECL_P (expr))
12261 {
12262 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12263 {
12264 /* A BIT_NOT_EXPR is used to represent a destructor. */
12265 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12266 {
12267 error ("qualifying type %qT does not match destructor name ~%qT",
12268 scope, TREE_OPERAND (expr, 0));
12269 expr = error_mark_node;
12270 }
12271 else
12272 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12273 /*is_type_p=*/0, false);
12274 }
12275 else
12276 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12277 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12278 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12279 {
12280 if (complain & tf_error)
12281 {
12282 error ("dependent-name %qE is parsed as a non-type, but "
12283 "instantiation yields a type", qualified_id);
12284 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12285 }
12286 return error_mark_node;
12287 }
12288 }
12289
12290 if (DECL_P (expr))
12291 {
12292 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12293 scope);
12294 /* Remember that there was a reference to this entity. */
12295 mark_used (expr);
12296 }
12297
12298 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12299 {
12300 if (complain & tf_error)
12301 qualified_name_lookup_error (scope,
12302 TREE_OPERAND (qualified_id, 1),
12303 expr, input_location);
12304 return error_mark_node;
12305 }
12306
12307 if (is_template)
12308 expr = lookup_template_function (expr, template_args);
12309
12310 if (expr == error_mark_node && complain & tf_error)
12311 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12312 expr, input_location);
12313 else if (TYPE_P (scope))
12314 {
12315 expr = (adjust_result_of_qualified_name_lookup
12316 (expr, scope, current_nonlambda_class_type ()));
12317 expr = (finish_qualified_id_expr
12318 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12319 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12320 /*template_arg_p=*/false, complain));
12321 }
12322
12323 /* Expressions do not generally have reference type. */
12324 if (TREE_CODE (expr) != SCOPE_REF
12325 /* However, if we're about to form a pointer-to-member, we just
12326 want the referenced member referenced. */
12327 && TREE_CODE (expr) != OFFSET_REF)
12328 expr = convert_from_reference (expr);
12329
12330 return expr;
12331 }
12332
12333 /* Like tsubst, but deals with expressions. This function just replaces
12334 template parms; to finish processing the resultant expression, use
12335 tsubst_copy_and_build or tsubst_expr. */
12336
12337 static tree
12338 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12339 {
12340 enum tree_code code;
12341 tree r;
12342
12343 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12344 return t;
12345
12346 code = TREE_CODE (t);
12347
12348 switch (code)
12349 {
12350 case PARM_DECL:
12351 r = retrieve_local_specialization (t);
12352
12353 if (r == NULL_TREE)
12354 {
12355 /* We get here for a use of 'this' in an NSDMI. */
12356 if (DECL_NAME (t) == this_identifier
12357 && at_function_scope_p ()
12358 && DECL_CONSTRUCTOR_P (current_function_decl))
12359 return current_class_ptr;
12360
12361 /* This can happen for a parameter name used later in a function
12362 declaration (such as in a late-specified return type). Just
12363 make a dummy decl, since it's only used for its type. */
12364 gcc_assert (cp_unevaluated_operand != 0);
12365 r = tsubst_decl (t, args, complain);
12366 /* Give it the template pattern as its context; its true context
12367 hasn't been instantiated yet and this is good enough for
12368 mangling. */
12369 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12370 }
12371
12372 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12373 r = ARGUMENT_PACK_SELECT_ARG (r);
12374 mark_used (r);
12375 return r;
12376
12377 case CONST_DECL:
12378 {
12379 tree enum_type;
12380 tree v;
12381
12382 if (DECL_TEMPLATE_PARM_P (t))
12383 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12384 /* There is no need to substitute into namespace-scope
12385 enumerators. */
12386 if (DECL_NAMESPACE_SCOPE_P (t))
12387 return t;
12388 /* If ARGS is NULL, then T is known to be non-dependent. */
12389 if (args == NULL_TREE)
12390 return integral_constant_value (t);
12391
12392 /* Unfortunately, we cannot just call lookup_name here.
12393 Consider:
12394
12395 template <int I> int f() {
12396 enum E { a = I };
12397 struct S { void g() { E e = a; } };
12398 };
12399
12400 When we instantiate f<7>::S::g(), say, lookup_name is not
12401 clever enough to find f<7>::a. */
12402 enum_type
12403 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12404 /*entering_scope=*/0);
12405
12406 for (v = TYPE_VALUES (enum_type);
12407 v != NULL_TREE;
12408 v = TREE_CHAIN (v))
12409 if (TREE_PURPOSE (v) == DECL_NAME (t))
12410 return TREE_VALUE (v);
12411
12412 /* We didn't find the name. That should never happen; if
12413 name-lookup found it during preliminary parsing, we
12414 should find it again here during instantiation. */
12415 gcc_unreachable ();
12416 }
12417 return t;
12418
12419 case FIELD_DECL:
12420 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12421 {
12422 /* Check for a local specialization set up by
12423 tsubst_pack_expansion. */
12424 tree r = retrieve_local_specialization (t);
12425 if (r)
12426 {
12427 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12428 r = ARGUMENT_PACK_SELECT_ARG (r);
12429 return r;
12430 }
12431
12432 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12433 tsubst_decl put in the hash table. */
12434 return retrieve_specialization (t, args, 0);
12435 }
12436
12437 if (DECL_CONTEXT (t))
12438 {
12439 tree ctx;
12440
12441 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12442 /*entering_scope=*/1);
12443 if (ctx != DECL_CONTEXT (t))
12444 {
12445 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12446 if (!r)
12447 {
12448 if (complain & tf_error)
12449 error ("using invalid field %qD", t);
12450 return error_mark_node;
12451 }
12452 return r;
12453 }
12454 }
12455
12456 return t;
12457
12458 case VAR_DECL:
12459 case FUNCTION_DECL:
12460 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12461 r = tsubst (t, args, complain, in_decl);
12462 else if (local_variable_p (t))
12463 {
12464 r = retrieve_local_specialization (t);
12465 if (r == NULL_TREE)
12466 {
12467 if (DECL_ANON_UNION_VAR_P (t))
12468 {
12469 /* Just use name lookup to find a member alias for an
12470 anonymous union, but then add it to the hash table. */
12471 r = lookup_name (DECL_NAME (t));
12472 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12473 register_local_specialization (r, t);
12474 }
12475 else
12476 {
12477 gcc_assert (errorcount || sorrycount);
12478 return error_mark_node;
12479 }
12480 }
12481 }
12482 else
12483 r = t;
12484 mark_used (r);
12485 return r;
12486
12487 case NAMESPACE_DECL:
12488 return t;
12489
12490 case OVERLOAD:
12491 /* An OVERLOAD will always be a non-dependent overload set; an
12492 overload set from function scope will just be represented with an
12493 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12494 gcc_assert (!uses_template_parms (t));
12495 return t;
12496
12497 case BASELINK:
12498 return tsubst_baselink (t, current_nonlambda_class_type (),
12499 args, complain, in_decl);
12500
12501 case TEMPLATE_DECL:
12502 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12503 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12504 args, complain, in_decl);
12505 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12506 return tsubst (t, args, complain, in_decl);
12507 else if (DECL_CLASS_SCOPE_P (t)
12508 && uses_template_parms (DECL_CONTEXT (t)))
12509 {
12510 /* Template template argument like the following example need
12511 special treatment:
12512
12513 template <template <class> class TT> struct C {};
12514 template <class T> struct D {
12515 template <class U> struct E {};
12516 C<E> c; // #1
12517 };
12518 D<int> d; // #2
12519
12520 We are processing the template argument `E' in #1 for
12521 the template instantiation #2. Originally, `E' is a
12522 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12523 have to substitute this with one having context `D<int>'. */
12524
12525 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12526 return lookup_field (context, DECL_NAME(t), 0, false);
12527 }
12528 else
12529 /* Ordinary template template argument. */
12530 return t;
12531
12532 case CAST_EXPR:
12533 case REINTERPRET_CAST_EXPR:
12534 case CONST_CAST_EXPR:
12535 case STATIC_CAST_EXPR:
12536 case DYNAMIC_CAST_EXPR:
12537 case IMPLICIT_CONV_EXPR:
12538 case CONVERT_EXPR:
12539 case NOP_EXPR:
12540 return build1
12541 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12542 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12543
12544 case SIZEOF_EXPR:
12545 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12546 {
12547
12548 tree expanded, op = TREE_OPERAND (t, 0);
12549 int len = 0;
12550
12551 if (SIZEOF_EXPR_TYPE_P (t))
12552 op = TREE_TYPE (op);
12553
12554 ++cp_unevaluated_operand;
12555 ++c_inhibit_evaluation_warnings;
12556 /* We only want to compute the number of arguments. */
12557 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12558 --cp_unevaluated_operand;
12559 --c_inhibit_evaluation_warnings;
12560
12561 if (TREE_CODE (expanded) == TREE_VEC)
12562 len = TREE_VEC_LENGTH (expanded);
12563
12564 if (expanded == error_mark_node)
12565 return error_mark_node;
12566 else if (PACK_EXPANSION_P (expanded)
12567 || (TREE_CODE (expanded) == TREE_VEC
12568 && len > 0
12569 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12570 {
12571 if (TREE_CODE (expanded) == TREE_VEC)
12572 expanded = TREE_VEC_ELT (expanded, len - 1);
12573
12574 if (TYPE_P (expanded))
12575 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12576 complain & tf_error);
12577 else
12578 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12579 complain & tf_error);
12580 }
12581 else
12582 return build_int_cst (size_type_node, len);
12583 }
12584 if (SIZEOF_EXPR_TYPE_P (t))
12585 {
12586 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12587 args, complain, in_decl);
12588 r = build1 (NOP_EXPR, r, error_mark_node);
12589 r = build1 (SIZEOF_EXPR,
12590 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12591 SIZEOF_EXPR_TYPE_P (r) = 1;
12592 return r;
12593 }
12594 /* Fall through */
12595
12596 case INDIRECT_REF:
12597 case NEGATE_EXPR:
12598 case TRUTH_NOT_EXPR:
12599 case BIT_NOT_EXPR:
12600 case ADDR_EXPR:
12601 case UNARY_PLUS_EXPR: /* Unary + */
12602 case ALIGNOF_EXPR:
12603 case AT_ENCODE_EXPR:
12604 case ARROW_EXPR:
12605 case THROW_EXPR:
12606 case TYPEID_EXPR:
12607 case REALPART_EXPR:
12608 case IMAGPART_EXPR:
12609 case PAREN_EXPR:
12610 return build1
12611 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12612 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12613
12614 case COMPONENT_REF:
12615 {
12616 tree object;
12617 tree name;
12618
12619 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12620 name = TREE_OPERAND (t, 1);
12621 if (TREE_CODE (name) == BIT_NOT_EXPR)
12622 {
12623 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12624 complain, in_decl);
12625 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12626 }
12627 else if (TREE_CODE (name) == SCOPE_REF
12628 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12629 {
12630 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12631 complain, in_decl);
12632 name = TREE_OPERAND (name, 1);
12633 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12634 complain, in_decl);
12635 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12636 name = build_qualified_name (/*type=*/NULL_TREE,
12637 base, name,
12638 /*template_p=*/false);
12639 }
12640 else if (BASELINK_P (name))
12641 name = tsubst_baselink (name,
12642 non_reference (TREE_TYPE (object)),
12643 args, complain,
12644 in_decl);
12645 else
12646 name = tsubst_copy (name, args, complain, in_decl);
12647 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12648 }
12649
12650 case PLUS_EXPR:
12651 case MINUS_EXPR:
12652 case MULT_EXPR:
12653 case TRUNC_DIV_EXPR:
12654 case CEIL_DIV_EXPR:
12655 case FLOOR_DIV_EXPR:
12656 case ROUND_DIV_EXPR:
12657 case EXACT_DIV_EXPR:
12658 case BIT_AND_EXPR:
12659 case BIT_IOR_EXPR:
12660 case BIT_XOR_EXPR:
12661 case TRUNC_MOD_EXPR:
12662 case FLOOR_MOD_EXPR:
12663 case TRUTH_ANDIF_EXPR:
12664 case TRUTH_ORIF_EXPR:
12665 case TRUTH_AND_EXPR:
12666 case TRUTH_OR_EXPR:
12667 case RSHIFT_EXPR:
12668 case LSHIFT_EXPR:
12669 case RROTATE_EXPR:
12670 case LROTATE_EXPR:
12671 case EQ_EXPR:
12672 case NE_EXPR:
12673 case MAX_EXPR:
12674 case MIN_EXPR:
12675 case LE_EXPR:
12676 case GE_EXPR:
12677 case LT_EXPR:
12678 case GT_EXPR:
12679 case COMPOUND_EXPR:
12680 case DOTSTAR_EXPR:
12681 case MEMBER_REF:
12682 case PREDECREMENT_EXPR:
12683 case PREINCREMENT_EXPR:
12684 case POSTDECREMENT_EXPR:
12685 case POSTINCREMENT_EXPR:
12686 return build_nt
12687 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12688 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12689
12690 case SCOPE_REF:
12691 return build_qualified_name (/*type=*/NULL_TREE,
12692 tsubst_copy (TREE_OPERAND (t, 0),
12693 args, complain, in_decl),
12694 tsubst_copy (TREE_OPERAND (t, 1),
12695 args, complain, in_decl),
12696 QUALIFIED_NAME_IS_TEMPLATE (t));
12697
12698 case ARRAY_REF:
12699 return build_nt
12700 (ARRAY_REF,
12701 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12702 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12703 NULL_TREE, NULL_TREE);
12704
12705 case CALL_EXPR:
12706 {
12707 int n = VL_EXP_OPERAND_LENGTH (t);
12708 tree result = build_vl_exp (CALL_EXPR, n);
12709 int i;
12710 for (i = 0; i < n; i++)
12711 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12712 complain, in_decl);
12713 return result;
12714 }
12715
12716 case COND_EXPR:
12717 case MODOP_EXPR:
12718 case PSEUDO_DTOR_EXPR:
12719 case VEC_PERM_EXPR:
12720 {
12721 r = build_nt
12722 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12723 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12724 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12725 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12726 return r;
12727 }
12728
12729 case NEW_EXPR:
12730 {
12731 r = build_nt
12732 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12733 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12734 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12735 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12736 return r;
12737 }
12738
12739 case DELETE_EXPR:
12740 {
12741 r = build_nt
12742 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12743 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12744 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12745 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12746 return r;
12747 }
12748
12749 case TEMPLATE_ID_EXPR:
12750 {
12751 /* Substituted template arguments */
12752 tree fn = TREE_OPERAND (t, 0);
12753 tree targs = TREE_OPERAND (t, 1);
12754
12755 fn = tsubst_copy (fn, args, complain, in_decl);
12756 if (targs)
12757 targs = tsubst_template_args (targs, args, complain, in_decl);
12758
12759 return lookup_template_function (fn, targs);
12760 }
12761
12762 case TREE_LIST:
12763 {
12764 tree purpose, value, chain;
12765
12766 if (t == void_list_node)
12767 return t;
12768
12769 purpose = TREE_PURPOSE (t);
12770 if (purpose)
12771 purpose = tsubst_copy (purpose, args, complain, in_decl);
12772 value = TREE_VALUE (t);
12773 if (value)
12774 value = tsubst_copy (value, args, complain, in_decl);
12775 chain = TREE_CHAIN (t);
12776 if (chain && chain != void_type_node)
12777 chain = tsubst_copy (chain, args, complain, in_decl);
12778 if (purpose == TREE_PURPOSE (t)
12779 && value == TREE_VALUE (t)
12780 && chain == TREE_CHAIN (t))
12781 return t;
12782 return tree_cons (purpose, value, chain);
12783 }
12784
12785 case RECORD_TYPE:
12786 case UNION_TYPE:
12787 case ENUMERAL_TYPE:
12788 case INTEGER_TYPE:
12789 case TEMPLATE_TYPE_PARM:
12790 case TEMPLATE_TEMPLATE_PARM:
12791 case BOUND_TEMPLATE_TEMPLATE_PARM:
12792 case TEMPLATE_PARM_INDEX:
12793 case POINTER_TYPE:
12794 case REFERENCE_TYPE:
12795 case OFFSET_TYPE:
12796 case FUNCTION_TYPE:
12797 case METHOD_TYPE:
12798 case ARRAY_TYPE:
12799 case TYPENAME_TYPE:
12800 case UNBOUND_CLASS_TEMPLATE:
12801 case TYPEOF_TYPE:
12802 case DECLTYPE_TYPE:
12803 case TYPE_DECL:
12804 return tsubst (t, args, complain, in_decl);
12805
12806 case USING_DECL:
12807 t = DECL_NAME (t);
12808 /* Fall through. */
12809 case IDENTIFIER_NODE:
12810 if (IDENTIFIER_TYPENAME_P (t))
12811 {
12812 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12813 return mangle_conv_op_name_for_type (new_type);
12814 }
12815 else
12816 return t;
12817
12818 case CONSTRUCTOR:
12819 /* This is handled by tsubst_copy_and_build. */
12820 gcc_unreachable ();
12821
12822 case VA_ARG_EXPR:
12823 return build_x_va_arg (EXPR_LOCATION (t),
12824 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12825 in_decl),
12826 tsubst (TREE_TYPE (t), args, complain, in_decl));
12827
12828 case CLEANUP_POINT_EXPR:
12829 /* We shouldn't have built any of these during initial template
12830 generation. Instead, they should be built during instantiation
12831 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12832 gcc_unreachable ();
12833
12834 case OFFSET_REF:
12835 r = build2
12836 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12837 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12838 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12839 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12840 mark_used (TREE_OPERAND (r, 1));
12841 return r;
12842
12843 case EXPR_PACK_EXPANSION:
12844 error ("invalid use of pack expansion expression");
12845 return error_mark_node;
12846
12847 case NONTYPE_ARGUMENT_PACK:
12848 error ("use %<...%> to expand argument pack");
12849 return error_mark_node;
12850
12851 case INTEGER_CST:
12852 case REAL_CST:
12853 case STRING_CST:
12854 case COMPLEX_CST:
12855 {
12856 /* Instantiate any typedefs in the type. */
12857 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12858 r = fold_convert (type, t);
12859 gcc_assert (TREE_CODE (r) == code);
12860 return r;
12861 }
12862
12863 case PTRMEM_CST:
12864 /* These can sometimes show up in a partial instantiation, but never
12865 involve template parms. */
12866 gcc_assert (!uses_template_parms (t));
12867 return t;
12868
12869 default:
12870 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12871 gcc_checking_assert (false);
12872 return t;
12873 }
12874 }
12875
12876 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12877
12878 static tree
12879 tsubst_omp_clauses (tree clauses, bool declare_simd,
12880 tree args, tsubst_flags_t complain, tree in_decl)
12881 {
12882 tree new_clauses = NULL, nc, oc;
12883
12884 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12885 {
12886 nc = copy_node (oc);
12887 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12888 new_clauses = nc;
12889
12890 switch (OMP_CLAUSE_CODE (nc))
12891 {
12892 case OMP_CLAUSE_LASTPRIVATE:
12893 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12894 {
12895 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12896 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12897 in_decl, /*integral_constant_expression_p=*/false);
12898 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12899 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12900 }
12901 /* FALLTHRU */
12902 case OMP_CLAUSE_PRIVATE:
12903 case OMP_CLAUSE_SHARED:
12904 case OMP_CLAUSE_FIRSTPRIVATE:
12905 case OMP_CLAUSE_COPYIN:
12906 case OMP_CLAUSE_COPYPRIVATE:
12907 case OMP_CLAUSE_IF:
12908 case OMP_CLAUSE_NUM_THREADS:
12909 case OMP_CLAUSE_SCHEDULE:
12910 case OMP_CLAUSE_COLLAPSE:
12911 case OMP_CLAUSE_FINAL:
12912 case OMP_CLAUSE_DEPEND:
12913 case OMP_CLAUSE_FROM:
12914 case OMP_CLAUSE_TO:
12915 case OMP_CLAUSE_UNIFORM:
12916 case OMP_CLAUSE_MAP:
12917 case OMP_CLAUSE_DEVICE:
12918 case OMP_CLAUSE_DIST_SCHEDULE:
12919 case OMP_CLAUSE_NUM_TEAMS:
12920 case OMP_CLAUSE_THREAD_LIMIT:
12921 case OMP_CLAUSE_SAFELEN:
12922 case OMP_CLAUSE_SIMDLEN:
12923 OMP_CLAUSE_OPERAND (nc, 0)
12924 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12925 in_decl, /*integral_constant_expression_p=*/false);
12926 break;
12927 case OMP_CLAUSE_REDUCTION:
12928 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12929 {
12930 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12931 if (TREE_CODE (placeholder) == SCOPE_REF)
12932 {
12933 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12934 complain, in_decl);
12935 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12936 = build_qualified_name (NULL_TREE, scope,
12937 TREE_OPERAND (placeholder, 1),
12938 false);
12939 }
12940 else
12941 gcc_assert (identifier_p (placeholder));
12942 }
12943 OMP_CLAUSE_OPERAND (nc, 0)
12944 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12945 in_decl, /*integral_constant_expression_p=*/false);
12946 break;
12947 case OMP_CLAUSE_LINEAR:
12948 case OMP_CLAUSE_ALIGNED:
12949 OMP_CLAUSE_OPERAND (nc, 0)
12950 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12951 in_decl, /*integral_constant_expression_p=*/false);
12952 OMP_CLAUSE_OPERAND (nc, 1)
12953 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12954 in_decl, /*integral_constant_expression_p=*/false);
12955 break;
12956
12957 case OMP_CLAUSE_NOWAIT:
12958 case OMP_CLAUSE_ORDERED:
12959 case OMP_CLAUSE_DEFAULT:
12960 case OMP_CLAUSE_UNTIED:
12961 case OMP_CLAUSE_MERGEABLE:
12962 case OMP_CLAUSE_INBRANCH:
12963 case OMP_CLAUSE_NOTINBRANCH:
12964 case OMP_CLAUSE_PROC_BIND:
12965 case OMP_CLAUSE_FOR:
12966 case OMP_CLAUSE_PARALLEL:
12967 case OMP_CLAUSE_SECTIONS:
12968 case OMP_CLAUSE_TASKGROUP:
12969 break;
12970 default:
12971 gcc_unreachable ();
12972 }
12973 }
12974
12975 new_clauses = nreverse (new_clauses);
12976 if (!declare_simd)
12977 new_clauses = finish_omp_clauses (new_clauses);
12978 return new_clauses;
12979 }
12980
12981 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12982
12983 static tree
12984 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12985 tree in_decl)
12986 {
12987 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12988
12989 tree purpose, value, chain;
12990
12991 if (t == NULL)
12992 return t;
12993
12994 if (TREE_CODE (t) != TREE_LIST)
12995 return tsubst_copy_and_build (t, args, complain, in_decl,
12996 /*function_p=*/false,
12997 /*integral_constant_expression_p=*/false);
12998
12999 if (t == void_list_node)
13000 return t;
13001
13002 purpose = TREE_PURPOSE (t);
13003 if (purpose)
13004 purpose = RECUR (purpose);
13005 value = TREE_VALUE (t);
13006 if (value)
13007 {
13008 if (TREE_CODE (value) != LABEL_DECL)
13009 value = RECUR (value);
13010 else
13011 {
13012 value = lookup_label (DECL_NAME (value));
13013 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13014 TREE_USED (value) = 1;
13015 }
13016 }
13017 chain = TREE_CHAIN (t);
13018 if (chain && chain != void_type_node)
13019 chain = RECUR (chain);
13020 return tree_cons (purpose, value, chain);
13021 #undef RECUR
13022 }
13023
13024 /* Substitute one OMP_FOR iterator. */
13025
13026 static void
13027 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13028 tree condv, tree incrv, tree *clauses,
13029 tree args, tsubst_flags_t complain, tree in_decl,
13030 bool integral_constant_expression_p)
13031 {
13032 #define RECUR(NODE) \
13033 tsubst_expr ((NODE), args, complain, in_decl, \
13034 integral_constant_expression_p)
13035 tree decl, init, cond, incr;
13036 bool init_decl;
13037
13038 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13039 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13040 decl = TREE_OPERAND (init, 0);
13041 init = TREE_OPERAND (init, 1);
13042 /* Do this before substituting into decl to handle 'auto'. */
13043 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13044 init = RECUR (init);
13045 decl = RECUR (decl);
13046
13047 if (decl == error_mark_node || init == error_mark_node)
13048 return;
13049
13050 if (init_decl)
13051 {
13052 gcc_assert (!processing_template_decl);
13053 init = DECL_INITIAL (decl);
13054 DECL_INITIAL (decl) = NULL_TREE;
13055 }
13056
13057 gcc_assert (!type_dependent_expression_p (decl));
13058
13059 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13060 {
13061 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13062 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13063 if (TREE_CODE (incr) == MODIFY_EXPR)
13064 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13065 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13066 RECUR (TREE_OPERAND (incr, 1)),
13067 complain);
13068 else
13069 incr = RECUR (incr);
13070 TREE_VEC_ELT (declv, i) = decl;
13071 TREE_VEC_ELT (initv, i) = init;
13072 TREE_VEC_ELT (condv, i) = cond;
13073 TREE_VEC_ELT (incrv, i) = incr;
13074 return;
13075 }
13076
13077 if (init && !init_decl)
13078 {
13079 tree c;
13080 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13081 {
13082 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13083 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13084 && OMP_CLAUSE_DECL (c) == decl)
13085 break;
13086 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13087 && OMP_CLAUSE_DECL (c) == decl)
13088 error ("iteration variable %qD should not be firstprivate", decl);
13089 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13090 && OMP_CLAUSE_DECL (c) == decl)
13091 error ("iteration variable %qD should not be reduction", decl);
13092 }
13093 if (c == NULL)
13094 {
13095 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13096 OMP_CLAUSE_DECL (c) = decl;
13097 c = finish_omp_clauses (c);
13098 if (c)
13099 {
13100 OMP_CLAUSE_CHAIN (c) = *clauses;
13101 *clauses = c;
13102 }
13103 }
13104 }
13105 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13106 if (COMPARISON_CLASS_P (cond))
13107 cond = build2 (TREE_CODE (cond), boolean_type_node,
13108 RECUR (TREE_OPERAND (cond, 0)),
13109 RECUR (TREE_OPERAND (cond, 1)));
13110 else
13111 cond = RECUR (cond);
13112 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13113 switch (TREE_CODE (incr))
13114 {
13115 case PREINCREMENT_EXPR:
13116 case PREDECREMENT_EXPR:
13117 case POSTINCREMENT_EXPR:
13118 case POSTDECREMENT_EXPR:
13119 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13120 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13121 break;
13122 case MODIFY_EXPR:
13123 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13124 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13125 {
13126 tree rhs = TREE_OPERAND (incr, 1);
13127 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13128 RECUR (TREE_OPERAND (incr, 0)),
13129 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13130 RECUR (TREE_OPERAND (rhs, 0)),
13131 RECUR (TREE_OPERAND (rhs, 1))));
13132 }
13133 else
13134 incr = RECUR (incr);
13135 break;
13136 case MODOP_EXPR:
13137 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13138 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13139 {
13140 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13141 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13142 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13143 TREE_TYPE (decl), lhs,
13144 RECUR (TREE_OPERAND (incr, 2))));
13145 }
13146 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13147 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13148 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13149 {
13150 tree rhs = TREE_OPERAND (incr, 2);
13151 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13152 RECUR (TREE_OPERAND (incr, 0)),
13153 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13154 RECUR (TREE_OPERAND (rhs, 0)),
13155 RECUR (TREE_OPERAND (rhs, 1))));
13156 }
13157 else
13158 incr = RECUR (incr);
13159 break;
13160 default:
13161 incr = RECUR (incr);
13162 break;
13163 }
13164
13165 TREE_VEC_ELT (declv, i) = decl;
13166 TREE_VEC_ELT (initv, i) = init;
13167 TREE_VEC_ELT (condv, i) = cond;
13168 TREE_VEC_ELT (incrv, i) = incr;
13169 #undef RECUR
13170 }
13171
13172 /* Like tsubst_copy for expressions, etc. but also does semantic
13173 processing. */
13174
13175 static tree
13176 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13177 bool integral_constant_expression_p)
13178 {
13179 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13180 #define RECUR(NODE) \
13181 tsubst_expr ((NODE), args, complain, in_decl, \
13182 integral_constant_expression_p)
13183
13184 tree stmt, tmp;
13185 tree r;
13186 location_t loc;
13187
13188 if (t == NULL_TREE || t == error_mark_node)
13189 return t;
13190
13191 loc = input_location;
13192 if (EXPR_HAS_LOCATION (t))
13193 input_location = EXPR_LOCATION (t);
13194 if (STATEMENT_CODE_P (TREE_CODE (t)))
13195 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13196
13197 switch (TREE_CODE (t))
13198 {
13199 case STATEMENT_LIST:
13200 {
13201 tree_stmt_iterator i;
13202 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13203 RECUR (tsi_stmt (i));
13204 break;
13205 }
13206
13207 case CTOR_INITIALIZER:
13208 finish_mem_initializers (tsubst_initializer_list
13209 (TREE_OPERAND (t, 0), args));
13210 break;
13211
13212 case RETURN_EXPR:
13213 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13214 break;
13215
13216 case EXPR_STMT:
13217 tmp = RECUR (EXPR_STMT_EXPR (t));
13218 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13219 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13220 else
13221 finish_expr_stmt (tmp);
13222 break;
13223
13224 case USING_STMT:
13225 do_using_directive (USING_STMT_NAMESPACE (t));
13226 break;
13227
13228 case DECL_EXPR:
13229 {
13230 tree decl, pattern_decl;
13231 tree init;
13232
13233 pattern_decl = decl = DECL_EXPR_DECL (t);
13234 if (TREE_CODE (decl) == LABEL_DECL)
13235 finish_label_decl (DECL_NAME (decl));
13236 else if (TREE_CODE (decl) == USING_DECL)
13237 {
13238 tree scope = USING_DECL_SCOPE (decl);
13239 tree name = DECL_NAME (decl);
13240 tree decl;
13241
13242 scope = tsubst (scope, args, complain, in_decl);
13243 decl = lookup_qualified_name (scope, name,
13244 /*is_type_p=*/false,
13245 /*complain=*/false);
13246 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13247 qualified_name_lookup_error (scope, name, decl, input_location);
13248 else
13249 do_local_using_decl (decl, scope, name);
13250 }
13251 else if (DECL_PACK_P (decl))
13252 {
13253 /* Don't build up decls for a variadic capture proxy, we'll
13254 instantiate the elements directly as needed. */
13255 break;
13256 }
13257 else
13258 {
13259 init = DECL_INITIAL (decl);
13260 decl = tsubst (decl, args, complain, in_decl);
13261 if (decl != error_mark_node)
13262 {
13263 /* By marking the declaration as instantiated, we avoid
13264 trying to instantiate it. Since instantiate_decl can't
13265 handle local variables, and since we've already done
13266 all that needs to be done, that's the right thing to
13267 do. */
13268 if (VAR_P (decl))
13269 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13270 if (VAR_P (decl)
13271 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13272 /* Anonymous aggregates are a special case. */
13273 finish_anon_union (decl);
13274 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13275 {
13276 DECL_CONTEXT (decl) = current_function_decl;
13277 if (DECL_NAME (decl) == this_identifier)
13278 {
13279 tree lam = DECL_CONTEXT (current_function_decl);
13280 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13281 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13282 }
13283 insert_capture_proxy (decl);
13284 }
13285 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13286 /* We already did a pushtag. */;
13287 else if (TREE_CODE (decl) == FUNCTION_DECL
13288 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13289 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13290 {
13291 DECL_CONTEXT (decl) = NULL_TREE;
13292 pushdecl (decl);
13293 DECL_CONTEXT (decl) = current_function_decl;
13294 cp_check_omp_declare_reduction (decl);
13295 }
13296 else
13297 {
13298 int const_init = false;
13299 maybe_push_decl (decl);
13300 if (VAR_P (decl)
13301 && DECL_PRETTY_FUNCTION_P (decl))
13302 {
13303 /* For __PRETTY_FUNCTION__ we have to adjust the
13304 initializer. */
13305 const char *const name
13306 = cxx_printable_name (current_function_decl, 2);
13307 init = cp_fname_init (name, &TREE_TYPE (decl));
13308 }
13309 else
13310 {
13311 tree t = RECUR (init);
13312
13313 if (init && !t)
13314 {
13315 /* If we had an initializer but it
13316 instantiated to nothing,
13317 value-initialize the object. This will
13318 only occur when the initializer was a
13319 pack expansion where the parameter packs
13320 used in that expansion were of length
13321 zero. */
13322 init = build_value_init (TREE_TYPE (decl),
13323 complain);
13324 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13325 init = get_target_expr_sfinae (init, complain);
13326 }
13327 else
13328 init = t;
13329 }
13330
13331 if (VAR_P (decl))
13332 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13333 (pattern_decl));
13334 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13335 }
13336 }
13337 }
13338
13339 break;
13340 }
13341
13342 case FOR_STMT:
13343 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13344 RECUR (FOR_INIT_STMT (t));
13345 finish_for_init_stmt (stmt);
13346 tmp = RECUR (FOR_COND (t));
13347 finish_for_cond (tmp, stmt, false);
13348 tmp = RECUR (FOR_EXPR (t));
13349 finish_for_expr (tmp, stmt);
13350 RECUR (FOR_BODY (t));
13351 finish_for_stmt (stmt);
13352 break;
13353
13354 case RANGE_FOR_STMT:
13355 {
13356 tree decl, expr;
13357 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13358 decl = RANGE_FOR_DECL (t);
13359 decl = tsubst (decl, args, complain, in_decl);
13360 maybe_push_decl (decl);
13361 expr = RECUR (RANGE_FOR_EXPR (t));
13362 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13363 RECUR (RANGE_FOR_BODY (t));
13364 finish_for_stmt (stmt);
13365 }
13366 break;
13367
13368 case WHILE_STMT:
13369 stmt = begin_while_stmt ();
13370 tmp = RECUR (WHILE_COND (t));
13371 finish_while_stmt_cond (tmp, stmt, false);
13372 RECUR (WHILE_BODY (t));
13373 finish_while_stmt (stmt);
13374 break;
13375
13376 case DO_STMT:
13377 stmt = begin_do_stmt ();
13378 RECUR (DO_BODY (t));
13379 finish_do_body (stmt);
13380 tmp = RECUR (DO_COND (t));
13381 finish_do_stmt (tmp, stmt, false);
13382 break;
13383
13384 case IF_STMT:
13385 stmt = begin_if_stmt ();
13386 tmp = RECUR (IF_COND (t));
13387 finish_if_stmt_cond (tmp, stmt);
13388 RECUR (THEN_CLAUSE (t));
13389 finish_then_clause (stmt);
13390
13391 if (ELSE_CLAUSE (t))
13392 {
13393 begin_else_clause (stmt);
13394 RECUR (ELSE_CLAUSE (t));
13395 finish_else_clause (stmt);
13396 }
13397
13398 finish_if_stmt (stmt);
13399 break;
13400
13401 case BIND_EXPR:
13402 if (BIND_EXPR_BODY_BLOCK (t))
13403 stmt = begin_function_body ();
13404 else
13405 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13406 ? BCS_TRY_BLOCK : 0);
13407
13408 RECUR (BIND_EXPR_BODY (t));
13409
13410 if (BIND_EXPR_BODY_BLOCK (t))
13411 finish_function_body (stmt);
13412 else
13413 finish_compound_stmt (stmt);
13414 break;
13415
13416 case BREAK_STMT:
13417 finish_break_stmt ();
13418 break;
13419
13420 case CONTINUE_STMT:
13421 finish_continue_stmt ();
13422 break;
13423
13424 case SWITCH_STMT:
13425 stmt = begin_switch_stmt ();
13426 tmp = RECUR (SWITCH_STMT_COND (t));
13427 finish_switch_cond (tmp, stmt);
13428 RECUR (SWITCH_STMT_BODY (t));
13429 finish_switch_stmt (stmt);
13430 break;
13431
13432 case CASE_LABEL_EXPR:
13433 finish_case_label (EXPR_LOCATION (t),
13434 RECUR (CASE_LOW (t)),
13435 RECUR (CASE_HIGH (t)));
13436 break;
13437
13438 case LABEL_EXPR:
13439 {
13440 tree decl = LABEL_EXPR_LABEL (t);
13441 tree label;
13442
13443 label = finish_label_stmt (DECL_NAME (decl));
13444 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13445 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13446 }
13447 break;
13448
13449 case GOTO_EXPR:
13450 tmp = GOTO_DESTINATION (t);
13451 if (TREE_CODE (tmp) != LABEL_DECL)
13452 /* Computed goto's must be tsubst'd into. On the other hand,
13453 non-computed gotos must not be; the identifier in question
13454 will have no binding. */
13455 tmp = RECUR (tmp);
13456 else
13457 tmp = DECL_NAME (tmp);
13458 finish_goto_stmt (tmp);
13459 break;
13460
13461 case ASM_EXPR:
13462 tmp = finish_asm_stmt
13463 (ASM_VOLATILE_P (t),
13464 RECUR (ASM_STRING (t)),
13465 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13466 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13467 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13468 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13469 {
13470 tree asm_expr = tmp;
13471 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13472 asm_expr = TREE_OPERAND (asm_expr, 0);
13473 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13474 }
13475 break;
13476
13477 case TRY_BLOCK:
13478 if (CLEANUP_P (t))
13479 {
13480 stmt = begin_try_block ();
13481 RECUR (TRY_STMTS (t));
13482 finish_cleanup_try_block (stmt);
13483 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13484 }
13485 else
13486 {
13487 tree compound_stmt = NULL_TREE;
13488
13489 if (FN_TRY_BLOCK_P (t))
13490 stmt = begin_function_try_block (&compound_stmt);
13491 else
13492 stmt = begin_try_block ();
13493
13494 RECUR (TRY_STMTS (t));
13495
13496 if (FN_TRY_BLOCK_P (t))
13497 finish_function_try_block (stmt);
13498 else
13499 finish_try_block (stmt);
13500
13501 RECUR (TRY_HANDLERS (t));
13502 if (FN_TRY_BLOCK_P (t))
13503 finish_function_handler_sequence (stmt, compound_stmt);
13504 else
13505 finish_handler_sequence (stmt);
13506 }
13507 break;
13508
13509 case HANDLER:
13510 {
13511 tree decl = HANDLER_PARMS (t);
13512
13513 if (decl)
13514 {
13515 decl = tsubst (decl, args, complain, in_decl);
13516 /* Prevent instantiate_decl from trying to instantiate
13517 this variable. We've already done all that needs to be
13518 done. */
13519 if (decl != error_mark_node)
13520 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13521 }
13522 stmt = begin_handler ();
13523 finish_handler_parms (decl, stmt);
13524 RECUR (HANDLER_BODY (t));
13525 finish_handler (stmt);
13526 }
13527 break;
13528
13529 case TAG_DEFN:
13530 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13531 if (CLASS_TYPE_P (tmp))
13532 {
13533 /* Local classes are not independent templates; they are
13534 instantiated along with their containing function. And this
13535 way we don't have to deal with pushing out of one local class
13536 to instantiate a member of another local class. */
13537 tree fn;
13538 /* Closures are handled by the LAMBDA_EXPR. */
13539 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13540 complete_type (tmp);
13541 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13542 if (!DECL_ARTIFICIAL (fn))
13543 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13544 }
13545 break;
13546
13547 case STATIC_ASSERT:
13548 {
13549 tree condition;
13550
13551 ++c_inhibit_evaluation_warnings;
13552 condition =
13553 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13554 args,
13555 complain, in_decl,
13556 /*integral_constant_expression_p=*/true);
13557 --c_inhibit_evaluation_warnings;
13558
13559 finish_static_assert (condition,
13560 STATIC_ASSERT_MESSAGE (t),
13561 STATIC_ASSERT_SOURCE_LOCATION (t),
13562 /*member_p=*/false);
13563 }
13564 break;
13565
13566 case OMP_PARALLEL:
13567 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13568 args, complain, in_decl);
13569 stmt = begin_omp_parallel ();
13570 RECUR (OMP_PARALLEL_BODY (t));
13571 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13572 = OMP_PARALLEL_COMBINED (t);
13573 break;
13574
13575 case OMP_TASK:
13576 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13577 args, complain, in_decl);
13578 stmt = begin_omp_task ();
13579 RECUR (OMP_TASK_BODY (t));
13580 finish_omp_task (tmp, stmt);
13581 break;
13582
13583 case OMP_FOR:
13584 case OMP_SIMD:
13585 case CILK_SIMD:
13586 case OMP_DISTRIBUTE:
13587 {
13588 tree clauses, body, pre_body;
13589 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13590 tree incrv = NULL_TREE;
13591 int i;
13592
13593 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13594 args, complain, in_decl);
13595 if (OMP_FOR_INIT (t) != NULL_TREE)
13596 {
13597 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13598 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13599 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13600 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13601 }
13602
13603 stmt = begin_omp_structured_block ();
13604
13605 pre_body = push_stmt_list ();
13606 RECUR (OMP_FOR_PRE_BODY (t));
13607 pre_body = pop_stmt_list (pre_body);
13608
13609 if (OMP_FOR_INIT (t) != NULL_TREE)
13610 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13611 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13612 &clauses, args, complain, in_decl,
13613 integral_constant_expression_p);
13614
13615 body = push_stmt_list ();
13616 RECUR (OMP_FOR_BODY (t));
13617 body = pop_stmt_list (body);
13618
13619 if (OMP_FOR_INIT (t) != NULL_TREE)
13620 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13621 condv, incrv, body, pre_body, clauses);
13622 else
13623 {
13624 t = make_node (TREE_CODE (t));
13625 TREE_TYPE (t) = void_type_node;
13626 OMP_FOR_BODY (t) = body;
13627 OMP_FOR_PRE_BODY (t) = pre_body;
13628 OMP_FOR_CLAUSES (t) = clauses;
13629 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13630 add_stmt (t);
13631 }
13632
13633 add_stmt (finish_omp_structured_block (stmt));
13634 }
13635 break;
13636
13637 case OMP_SECTIONS:
13638 case OMP_SINGLE:
13639 case OMP_TEAMS:
13640 case OMP_TARGET_DATA:
13641 case OMP_TARGET:
13642 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13643 args, complain, in_decl);
13644 stmt = push_stmt_list ();
13645 RECUR (OMP_BODY (t));
13646 stmt = pop_stmt_list (stmt);
13647
13648 t = copy_node (t);
13649 OMP_BODY (t) = stmt;
13650 OMP_CLAUSES (t) = tmp;
13651 add_stmt (t);
13652 break;
13653
13654 case OMP_TARGET_UPDATE:
13655 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13656 args, complain, in_decl);
13657 t = copy_node (t);
13658 OMP_CLAUSES (t) = tmp;
13659 add_stmt (t);
13660 break;
13661
13662 case OMP_SECTION:
13663 case OMP_CRITICAL:
13664 case OMP_MASTER:
13665 case OMP_TASKGROUP:
13666 case OMP_ORDERED:
13667 stmt = push_stmt_list ();
13668 RECUR (OMP_BODY (t));
13669 stmt = pop_stmt_list (stmt);
13670
13671 t = copy_node (t);
13672 OMP_BODY (t) = stmt;
13673 add_stmt (t);
13674 break;
13675
13676 case OMP_ATOMIC:
13677 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13678 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13679 {
13680 tree op1 = TREE_OPERAND (t, 1);
13681 tree rhs1 = NULL_TREE;
13682 tree lhs, rhs;
13683 if (TREE_CODE (op1) == COMPOUND_EXPR)
13684 {
13685 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13686 op1 = TREE_OPERAND (op1, 1);
13687 }
13688 lhs = RECUR (TREE_OPERAND (op1, 0));
13689 rhs = RECUR (TREE_OPERAND (op1, 1));
13690 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13691 NULL_TREE, NULL_TREE, rhs1,
13692 OMP_ATOMIC_SEQ_CST (t));
13693 }
13694 else
13695 {
13696 tree op1 = TREE_OPERAND (t, 1);
13697 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13698 tree rhs1 = NULL_TREE;
13699 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13700 enum tree_code opcode = NOP_EXPR;
13701 if (code == OMP_ATOMIC_READ)
13702 {
13703 v = RECUR (TREE_OPERAND (op1, 0));
13704 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13705 }
13706 else if (code == OMP_ATOMIC_CAPTURE_OLD
13707 || code == OMP_ATOMIC_CAPTURE_NEW)
13708 {
13709 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13710 v = RECUR (TREE_OPERAND (op1, 0));
13711 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13712 if (TREE_CODE (op11) == COMPOUND_EXPR)
13713 {
13714 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13715 op11 = TREE_OPERAND (op11, 1);
13716 }
13717 lhs = RECUR (TREE_OPERAND (op11, 0));
13718 rhs = RECUR (TREE_OPERAND (op11, 1));
13719 opcode = TREE_CODE (op11);
13720 if (opcode == MODIFY_EXPR)
13721 opcode = NOP_EXPR;
13722 }
13723 else
13724 {
13725 code = OMP_ATOMIC;
13726 lhs = RECUR (TREE_OPERAND (op1, 0));
13727 rhs = RECUR (TREE_OPERAND (op1, 1));
13728 }
13729 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13730 OMP_ATOMIC_SEQ_CST (t));
13731 }
13732 break;
13733
13734 case TRANSACTION_EXPR:
13735 {
13736 int flags = 0;
13737 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13738 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13739
13740 if (TRANSACTION_EXPR_IS_STMT (t))
13741 {
13742 tree body = TRANSACTION_EXPR_BODY (t);
13743 tree noex = NULL_TREE;
13744 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13745 {
13746 noex = MUST_NOT_THROW_COND (body);
13747 if (noex == NULL_TREE)
13748 noex = boolean_true_node;
13749 body = TREE_OPERAND (body, 0);
13750 }
13751 stmt = begin_transaction_stmt (input_location, NULL, flags);
13752 RECUR (body);
13753 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13754 }
13755 else
13756 {
13757 stmt = build_transaction_expr (EXPR_LOCATION (t),
13758 RECUR (TRANSACTION_EXPR_BODY (t)),
13759 flags, NULL_TREE);
13760 RETURN (stmt);
13761 }
13762 }
13763 break;
13764
13765 case MUST_NOT_THROW_EXPR:
13766 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13767 RECUR (MUST_NOT_THROW_COND (t))));
13768
13769 case EXPR_PACK_EXPANSION:
13770 error ("invalid use of pack expansion expression");
13771 RETURN (error_mark_node);
13772
13773 case NONTYPE_ARGUMENT_PACK:
13774 error ("use %<...%> to expand argument pack");
13775 RETURN (error_mark_node);
13776
13777 case CILK_SPAWN_STMT:
13778 cfun->calls_cilk_spawn = 1;
13779 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
13780
13781 case CILK_SYNC_STMT:
13782 RETURN (build_cilk_sync ());
13783
13784 case COMPOUND_EXPR:
13785 tmp = RECUR (TREE_OPERAND (t, 0));
13786 if (tmp == NULL_TREE)
13787 /* If the first operand was a statement, we're done with it. */
13788 RETURN (RECUR (TREE_OPERAND (t, 1)));
13789 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13790 RECUR (TREE_OPERAND (t, 1)),
13791 complain));
13792
13793 default:
13794 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13795
13796 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13797 /*function_p=*/false,
13798 integral_constant_expression_p));
13799 }
13800
13801 RETURN (NULL_TREE);
13802 out:
13803 input_location = loc;
13804 return r;
13805 #undef RECUR
13806 #undef RETURN
13807 }
13808
13809 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13810 function. For description of the body see comment above
13811 cp_parser_omp_declare_reduction_exprs. */
13812
13813 static void
13814 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13815 {
13816 if (t == NULL_TREE || t == error_mark_node)
13817 return;
13818
13819 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13820
13821 tree_stmt_iterator tsi;
13822 int i;
13823 tree stmts[7];
13824 memset (stmts, 0, sizeof stmts);
13825 for (i = 0, tsi = tsi_start (t);
13826 i < 7 && !tsi_end_p (tsi);
13827 i++, tsi_next (&tsi))
13828 stmts[i] = tsi_stmt (tsi);
13829 gcc_assert (tsi_end_p (tsi));
13830
13831 if (i >= 3)
13832 {
13833 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13834 && TREE_CODE (stmts[1]) == DECL_EXPR);
13835 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13836 args, complain, in_decl);
13837 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13838 args, complain, in_decl);
13839 DECL_CONTEXT (omp_out) = current_function_decl;
13840 DECL_CONTEXT (omp_in) = current_function_decl;
13841 keep_next_level (true);
13842 tree block = begin_omp_structured_block ();
13843 tsubst_expr (stmts[2], args, complain, in_decl, false);
13844 block = finish_omp_structured_block (block);
13845 block = maybe_cleanup_point_expr_void (block);
13846 add_decl_expr (omp_out);
13847 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13848 TREE_NO_WARNING (omp_out) = 1;
13849 add_decl_expr (omp_in);
13850 finish_expr_stmt (block);
13851 }
13852 if (i >= 6)
13853 {
13854 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13855 && TREE_CODE (stmts[4]) == DECL_EXPR);
13856 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13857 args, complain, in_decl);
13858 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13859 args, complain, in_decl);
13860 DECL_CONTEXT (omp_priv) = current_function_decl;
13861 DECL_CONTEXT (omp_orig) = current_function_decl;
13862 keep_next_level (true);
13863 tree block = begin_omp_structured_block ();
13864 tsubst_expr (stmts[5], args, complain, in_decl, false);
13865 block = finish_omp_structured_block (block);
13866 block = maybe_cleanup_point_expr_void (block);
13867 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13868 add_decl_expr (omp_priv);
13869 add_decl_expr (omp_orig);
13870 finish_expr_stmt (block);
13871 if (i == 7)
13872 add_decl_expr (omp_orig);
13873 }
13874 }
13875
13876 /* T is a postfix-expression that is not being used in a function
13877 call. Return the substituted version of T. */
13878
13879 static tree
13880 tsubst_non_call_postfix_expression (tree t, tree args,
13881 tsubst_flags_t complain,
13882 tree in_decl)
13883 {
13884 if (TREE_CODE (t) == SCOPE_REF)
13885 t = tsubst_qualified_id (t, args, complain, in_decl,
13886 /*done=*/false, /*address_p=*/false);
13887 else
13888 t = tsubst_copy_and_build (t, args, complain, in_decl,
13889 /*function_p=*/false,
13890 /*integral_constant_expression_p=*/false);
13891
13892 return t;
13893 }
13894
13895 /* Like tsubst but deals with expressions and performs semantic
13896 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13897
13898 tree
13899 tsubst_copy_and_build (tree t,
13900 tree args,
13901 tsubst_flags_t complain,
13902 tree in_decl,
13903 bool function_p,
13904 bool integral_constant_expression_p)
13905 {
13906 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13907 #define RECUR(NODE) \
13908 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13909 /*function_p=*/false, \
13910 integral_constant_expression_p)
13911
13912 tree retval, op1;
13913 location_t loc;
13914
13915 if (t == NULL_TREE || t == error_mark_node)
13916 return t;
13917
13918 loc = input_location;
13919 if (EXPR_HAS_LOCATION (t))
13920 input_location = EXPR_LOCATION (t);
13921
13922 /* N3276 decltype magic only applies to calls at the top level or on the
13923 right side of a comma. */
13924 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13925 complain &= ~tf_decltype;
13926
13927 switch (TREE_CODE (t))
13928 {
13929 case USING_DECL:
13930 t = DECL_NAME (t);
13931 /* Fall through. */
13932 case IDENTIFIER_NODE:
13933 {
13934 tree decl;
13935 cp_id_kind idk;
13936 bool non_integral_constant_expression_p;
13937 const char *error_msg;
13938
13939 if (IDENTIFIER_TYPENAME_P (t))
13940 {
13941 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13942 t = mangle_conv_op_name_for_type (new_type);
13943 }
13944
13945 /* Look up the name. */
13946 decl = lookup_name (t);
13947
13948 /* By convention, expressions use ERROR_MARK_NODE to indicate
13949 failure, not NULL_TREE. */
13950 if (decl == NULL_TREE)
13951 decl = error_mark_node;
13952
13953 decl = finish_id_expression (t, decl, NULL_TREE,
13954 &idk,
13955 integral_constant_expression_p,
13956 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13957 &non_integral_constant_expression_p,
13958 /*template_p=*/false,
13959 /*done=*/true,
13960 /*address_p=*/false,
13961 /*template_arg_p=*/false,
13962 &error_msg,
13963 input_location);
13964 if (error_msg)
13965 error (error_msg);
13966 if (!function_p && identifier_p (decl))
13967 {
13968 if (complain & tf_error)
13969 unqualified_name_lookup_error (decl);
13970 decl = error_mark_node;
13971 }
13972 RETURN (decl);
13973 }
13974
13975 case TEMPLATE_ID_EXPR:
13976 {
13977 tree object;
13978 tree templ = RECUR (TREE_OPERAND (t, 0));
13979 tree targs = TREE_OPERAND (t, 1);
13980
13981 if (targs)
13982 targs = tsubst_template_args (targs, args, complain, in_decl);
13983
13984 if (TREE_CODE (templ) == COMPONENT_REF)
13985 {
13986 object = TREE_OPERAND (templ, 0);
13987 templ = TREE_OPERAND (templ, 1);
13988 }
13989 else
13990 object = NULL_TREE;
13991 templ = lookup_template_function (templ, targs);
13992
13993 if (object)
13994 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13995 object, templ, NULL_TREE));
13996 else
13997 RETURN (baselink_for_fns (templ));
13998 }
13999
14000 case INDIRECT_REF:
14001 {
14002 tree r = RECUR (TREE_OPERAND (t, 0));
14003
14004 if (REFERENCE_REF_P (t))
14005 {
14006 /* A type conversion to reference type will be enclosed in
14007 such an indirect ref, but the substitution of the cast
14008 will have also added such an indirect ref. */
14009 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14010 r = convert_from_reference (r);
14011 }
14012 else
14013 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14014 complain|decltype_flag);
14015 RETURN (r);
14016 }
14017
14018 case NOP_EXPR:
14019 RETURN (build_nop
14020 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14021 RECUR (TREE_OPERAND (t, 0))));
14022
14023 case IMPLICIT_CONV_EXPR:
14024 {
14025 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14026 tree expr = RECUR (TREE_OPERAND (t, 0));
14027 int flags = LOOKUP_IMPLICIT;
14028 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14029 flags = LOOKUP_NORMAL;
14030 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14031 flags));
14032 }
14033
14034 case CONVERT_EXPR:
14035 RETURN (build1
14036 (CONVERT_EXPR,
14037 tsubst (TREE_TYPE (t), args, complain, in_decl),
14038 RECUR (TREE_OPERAND (t, 0))));
14039
14040 case CAST_EXPR:
14041 case REINTERPRET_CAST_EXPR:
14042 case CONST_CAST_EXPR:
14043 case DYNAMIC_CAST_EXPR:
14044 case STATIC_CAST_EXPR:
14045 {
14046 tree type;
14047 tree op, r = NULL_TREE;
14048
14049 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14050 if (integral_constant_expression_p
14051 && !cast_valid_in_integral_constant_expression_p (type))
14052 {
14053 if (complain & tf_error)
14054 error ("a cast to a type other than an integral or "
14055 "enumeration type cannot appear in a constant-expression");
14056 RETURN (error_mark_node);
14057 }
14058
14059 op = RECUR (TREE_OPERAND (t, 0));
14060
14061 ++c_inhibit_evaluation_warnings;
14062 switch (TREE_CODE (t))
14063 {
14064 case CAST_EXPR:
14065 r = build_functional_cast (type, op, complain);
14066 break;
14067 case REINTERPRET_CAST_EXPR:
14068 r = build_reinterpret_cast (type, op, complain);
14069 break;
14070 case CONST_CAST_EXPR:
14071 r = build_const_cast (type, op, complain);
14072 break;
14073 case DYNAMIC_CAST_EXPR:
14074 r = build_dynamic_cast (type, op, complain);
14075 break;
14076 case STATIC_CAST_EXPR:
14077 r = build_static_cast (type, op, complain);
14078 break;
14079 default:
14080 gcc_unreachable ();
14081 }
14082 --c_inhibit_evaluation_warnings;
14083
14084 RETURN (r);
14085 }
14086
14087 case POSTDECREMENT_EXPR:
14088 case POSTINCREMENT_EXPR:
14089 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14090 args, complain, in_decl);
14091 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14092 complain|decltype_flag));
14093
14094 case PREDECREMENT_EXPR:
14095 case PREINCREMENT_EXPR:
14096 case NEGATE_EXPR:
14097 case BIT_NOT_EXPR:
14098 case ABS_EXPR:
14099 case TRUTH_NOT_EXPR:
14100 case UNARY_PLUS_EXPR: /* Unary + */
14101 case REALPART_EXPR:
14102 case IMAGPART_EXPR:
14103 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14104 RECUR (TREE_OPERAND (t, 0)),
14105 complain|decltype_flag));
14106
14107 case FIX_TRUNC_EXPR:
14108 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14109 0, complain));
14110
14111 case ADDR_EXPR:
14112 op1 = TREE_OPERAND (t, 0);
14113 if (TREE_CODE (op1) == LABEL_DECL)
14114 RETURN (finish_label_address_expr (DECL_NAME (op1),
14115 EXPR_LOCATION (op1)));
14116 if (TREE_CODE (op1) == SCOPE_REF)
14117 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14118 /*done=*/true, /*address_p=*/true);
14119 else
14120 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14121 in_decl);
14122 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14123 complain|decltype_flag));
14124
14125 case PLUS_EXPR:
14126 case MINUS_EXPR:
14127 case MULT_EXPR:
14128 case TRUNC_DIV_EXPR:
14129 case CEIL_DIV_EXPR:
14130 case FLOOR_DIV_EXPR:
14131 case ROUND_DIV_EXPR:
14132 case EXACT_DIV_EXPR:
14133 case BIT_AND_EXPR:
14134 case BIT_IOR_EXPR:
14135 case BIT_XOR_EXPR:
14136 case TRUNC_MOD_EXPR:
14137 case FLOOR_MOD_EXPR:
14138 case TRUTH_ANDIF_EXPR:
14139 case TRUTH_ORIF_EXPR:
14140 case TRUTH_AND_EXPR:
14141 case TRUTH_OR_EXPR:
14142 case RSHIFT_EXPR:
14143 case LSHIFT_EXPR:
14144 case RROTATE_EXPR:
14145 case LROTATE_EXPR:
14146 case EQ_EXPR:
14147 case NE_EXPR:
14148 case MAX_EXPR:
14149 case MIN_EXPR:
14150 case LE_EXPR:
14151 case GE_EXPR:
14152 case LT_EXPR:
14153 case GT_EXPR:
14154 case MEMBER_REF:
14155 case DOTSTAR_EXPR:
14156 {
14157 tree r;
14158
14159 ++c_inhibit_evaluation_warnings;
14160
14161 r = build_x_binary_op
14162 (input_location, TREE_CODE (t),
14163 RECUR (TREE_OPERAND (t, 0)),
14164 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14165 ? ERROR_MARK
14166 : TREE_CODE (TREE_OPERAND (t, 0))),
14167 RECUR (TREE_OPERAND (t, 1)),
14168 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14169 ? ERROR_MARK
14170 : TREE_CODE (TREE_OPERAND (t, 1))),
14171 /*overload=*/NULL,
14172 complain|decltype_flag);
14173 if (EXPR_P (r) && TREE_NO_WARNING (t))
14174 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14175
14176 --c_inhibit_evaluation_warnings;
14177
14178 RETURN (r);
14179 }
14180
14181 case POINTER_PLUS_EXPR:
14182 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14183 RECUR (TREE_OPERAND (t, 1)));
14184
14185 case SCOPE_REF:
14186 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14187 /*address_p=*/false));
14188 case ARRAY_REF:
14189 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14190 args, complain, in_decl);
14191 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14192 RECUR (TREE_OPERAND (t, 1)),
14193 complain|decltype_flag));
14194
14195 case ARRAY_NOTATION_REF:
14196 {
14197 tree start_index, length, stride;
14198 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14199 args, complain, in_decl);
14200 start_index = RECUR (ARRAY_NOTATION_START (t));
14201 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14202 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14203 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14204 length, stride, TREE_TYPE (op1)));
14205 }
14206 case SIZEOF_EXPR:
14207 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14208 RETURN (tsubst_copy (t, args, complain, in_decl));
14209 /* Fall through */
14210
14211 case ALIGNOF_EXPR:
14212 {
14213 tree r;
14214
14215 op1 = TREE_OPERAND (t, 0);
14216 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14217 op1 = TREE_TYPE (op1);
14218 if (!args)
14219 {
14220 /* When there are no ARGS, we are trying to evaluate a
14221 non-dependent expression from the parser. Trying to do
14222 the substitutions may not work. */
14223 if (!TYPE_P (op1))
14224 op1 = TREE_TYPE (op1);
14225 }
14226 else
14227 {
14228 ++cp_unevaluated_operand;
14229 ++c_inhibit_evaluation_warnings;
14230 if (TYPE_P (op1))
14231 op1 = tsubst (op1, args, complain, in_decl);
14232 else
14233 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14234 /*function_p=*/false,
14235 /*integral_constant_expression_p=*/
14236 false);
14237 --cp_unevaluated_operand;
14238 --c_inhibit_evaluation_warnings;
14239 }
14240 if (TYPE_P (op1))
14241 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14242 complain & tf_error);
14243 else
14244 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14245 complain & tf_error);
14246 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14247 {
14248 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14249 {
14250 if (!processing_template_decl && TYPE_P (op1))
14251 {
14252 r = build_min (SIZEOF_EXPR, size_type_node,
14253 build1 (NOP_EXPR, op1, error_mark_node));
14254 SIZEOF_EXPR_TYPE_P (r) = 1;
14255 }
14256 else
14257 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14258 TREE_SIDE_EFFECTS (r) = 0;
14259 TREE_READONLY (r) = 1;
14260 }
14261 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14262 }
14263 RETURN (r);
14264 }
14265
14266 case AT_ENCODE_EXPR:
14267 {
14268 op1 = TREE_OPERAND (t, 0);
14269 ++cp_unevaluated_operand;
14270 ++c_inhibit_evaluation_warnings;
14271 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14272 /*function_p=*/false,
14273 /*integral_constant_expression_p=*/false);
14274 --cp_unevaluated_operand;
14275 --c_inhibit_evaluation_warnings;
14276 RETURN (objc_build_encode_expr (op1));
14277 }
14278
14279 case NOEXCEPT_EXPR:
14280 op1 = TREE_OPERAND (t, 0);
14281 ++cp_unevaluated_operand;
14282 ++c_inhibit_evaluation_warnings;
14283 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14284 /*function_p=*/false,
14285 /*integral_constant_expression_p=*/false);
14286 --cp_unevaluated_operand;
14287 --c_inhibit_evaluation_warnings;
14288 RETURN (finish_noexcept_expr (op1, complain));
14289
14290 case MODOP_EXPR:
14291 {
14292 tree r;
14293
14294 ++c_inhibit_evaluation_warnings;
14295
14296 r = build_x_modify_expr
14297 (EXPR_LOCATION (t),
14298 RECUR (TREE_OPERAND (t, 0)),
14299 TREE_CODE (TREE_OPERAND (t, 1)),
14300 RECUR (TREE_OPERAND (t, 2)),
14301 complain|decltype_flag);
14302 /* TREE_NO_WARNING must be set if either the expression was
14303 parenthesized or it uses an operator such as >>= rather
14304 than plain assignment. In the former case, it was already
14305 set and must be copied. In the latter case,
14306 build_x_modify_expr sets it and it must not be reset
14307 here. */
14308 if (TREE_NO_WARNING (t))
14309 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14310
14311 --c_inhibit_evaluation_warnings;
14312
14313 RETURN (r);
14314 }
14315
14316 case ARROW_EXPR:
14317 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14318 args, complain, in_decl);
14319 /* Remember that there was a reference to this entity. */
14320 if (DECL_P (op1))
14321 mark_used (op1);
14322 RETURN (build_x_arrow (input_location, op1, complain));
14323
14324 case NEW_EXPR:
14325 {
14326 tree placement = RECUR (TREE_OPERAND (t, 0));
14327 tree init = RECUR (TREE_OPERAND (t, 3));
14328 vec<tree, va_gc> *placement_vec;
14329 vec<tree, va_gc> *init_vec;
14330 tree ret;
14331
14332 if (placement == NULL_TREE)
14333 placement_vec = NULL;
14334 else
14335 {
14336 placement_vec = make_tree_vector ();
14337 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14338 vec_safe_push (placement_vec, TREE_VALUE (placement));
14339 }
14340
14341 /* If there was an initializer in the original tree, but it
14342 instantiated to an empty list, then we should pass a
14343 non-NULL empty vector to tell build_new that it was an
14344 empty initializer() rather than no initializer. This can
14345 only happen when the initializer is a pack expansion whose
14346 parameter packs are of length zero. */
14347 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14348 init_vec = NULL;
14349 else
14350 {
14351 init_vec = make_tree_vector ();
14352 if (init == void_zero_node)
14353 gcc_assert (init_vec != NULL);
14354 else
14355 {
14356 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14357 vec_safe_push (init_vec, TREE_VALUE (init));
14358 }
14359 }
14360
14361 ret = build_new (&placement_vec,
14362 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14363 RECUR (TREE_OPERAND (t, 2)),
14364 &init_vec,
14365 NEW_EXPR_USE_GLOBAL (t),
14366 complain);
14367
14368 if (placement_vec != NULL)
14369 release_tree_vector (placement_vec);
14370 if (init_vec != NULL)
14371 release_tree_vector (init_vec);
14372
14373 RETURN (ret);
14374 }
14375
14376 case DELETE_EXPR:
14377 RETURN (delete_sanity
14378 (RECUR (TREE_OPERAND (t, 0)),
14379 RECUR (TREE_OPERAND (t, 1)),
14380 DELETE_EXPR_USE_VEC (t),
14381 DELETE_EXPR_USE_GLOBAL (t),
14382 complain));
14383
14384 case COMPOUND_EXPR:
14385 {
14386 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14387 complain & ~tf_decltype, in_decl,
14388 /*function_p=*/false,
14389 integral_constant_expression_p);
14390 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14391 op0,
14392 RECUR (TREE_OPERAND (t, 1)),
14393 complain|decltype_flag));
14394 }
14395
14396 case CALL_EXPR:
14397 {
14398 tree function;
14399 vec<tree, va_gc> *call_args;
14400 unsigned int nargs, i;
14401 bool qualified_p;
14402 bool koenig_p;
14403 tree ret;
14404
14405 function = CALL_EXPR_FN (t);
14406 /* When we parsed the expression, we determined whether or
14407 not Koenig lookup should be performed. */
14408 koenig_p = KOENIG_LOOKUP_P (t);
14409 if (TREE_CODE (function) == SCOPE_REF)
14410 {
14411 qualified_p = true;
14412 function = tsubst_qualified_id (function, args, complain, in_decl,
14413 /*done=*/false,
14414 /*address_p=*/false);
14415 }
14416 else if (koenig_p && identifier_p (function))
14417 {
14418 /* Do nothing; calling tsubst_copy_and_build on an identifier
14419 would incorrectly perform unqualified lookup again.
14420
14421 Note that we can also have an IDENTIFIER_NODE if the earlier
14422 unqualified lookup found a member function; in that case
14423 koenig_p will be false and we do want to do the lookup
14424 again to find the instantiated member function.
14425
14426 FIXME but doing that causes c++/15272, so we need to stop
14427 using IDENTIFIER_NODE in that situation. */
14428 qualified_p = false;
14429 }
14430 else
14431 {
14432 if (TREE_CODE (function) == COMPONENT_REF)
14433 {
14434 tree op = TREE_OPERAND (function, 1);
14435
14436 qualified_p = (TREE_CODE (op) == SCOPE_REF
14437 || (BASELINK_P (op)
14438 && BASELINK_QUALIFIED_P (op)));
14439 }
14440 else
14441 qualified_p = false;
14442
14443 if (TREE_CODE (function) == ADDR_EXPR
14444 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14445 /* Avoid error about taking the address of a constructor. */
14446 function = TREE_OPERAND (function, 0);
14447
14448 function = tsubst_copy_and_build (function, args, complain,
14449 in_decl,
14450 !qualified_p,
14451 integral_constant_expression_p);
14452
14453 if (BASELINK_P (function))
14454 qualified_p = true;
14455 }
14456
14457 nargs = call_expr_nargs (t);
14458 call_args = make_tree_vector ();
14459 for (i = 0; i < nargs; ++i)
14460 {
14461 tree arg = CALL_EXPR_ARG (t, i);
14462
14463 if (!PACK_EXPANSION_P (arg))
14464 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14465 else
14466 {
14467 /* Expand the pack expansion and push each entry onto
14468 CALL_ARGS. */
14469 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14470 if (TREE_CODE (arg) == TREE_VEC)
14471 {
14472 unsigned int len, j;
14473
14474 len = TREE_VEC_LENGTH (arg);
14475 for (j = 0; j < len; ++j)
14476 {
14477 tree value = TREE_VEC_ELT (arg, j);
14478 if (value != NULL_TREE)
14479 value = convert_from_reference (value);
14480 vec_safe_push (call_args, value);
14481 }
14482 }
14483 else
14484 {
14485 /* A partial substitution. Add one entry. */
14486 vec_safe_push (call_args, arg);
14487 }
14488 }
14489 }
14490
14491 /* We do not perform argument-dependent lookup if normal
14492 lookup finds a non-function, in accordance with the
14493 expected resolution of DR 218. */
14494 if (koenig_p
14495 && ((is_overloaded_fn (function)
14496 /* If lookup found a member function, the Koenig lookup is
14497 not appropriate, even if an unqualified-name was used
14498 to denote the function. */
14499 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14500 || identifier_p (function))
14501 /* Only do this when substitution turns a dependent call
14502 into a non-dependent call. */
14503 && type_dependent_expression_p_push (t)
14504 && !any_type_dependent_arguments_p (call_args))
14505 function = perform_koenig_lookup (function, call_args, tf_none);
14506
14507 if (identifier_p (function)
14508 && !any_type_dependent_arguments_p (call_args))
14509 {
14510 if (koenig_p && (complain & tf_warning_or_error))
14511 {
14512 /* For backwards compatibility and good diagnostics, try
14513 the unqualified lookup again if we aren't in SFINAE
14514 context. */
14515 tree unq = (tsubst_copy_and_build
14516 (function, args, complain, in_decl, true,
14517 integral_constant_expression_p));
14518 if (unq == error_mark_node)
14519 RETURN (error_mark_node);
14520
14521 if (unq != function)
14522 {
14523 tree fn = unq;
14524 if (INDIRECT_REF_P (fn))
14525 fn = TREE_OPERAND (fn, 0);
14526 if (TREE_CODE (fn) == COMPONENT_REF)
14527 fn = TREE_OPERAND (fn, 1);
14528 if (is_overloaded_fn (fn))
14529 fn = get_first_fn (fn);
14530 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
14531 "%qD was not declared in this scope, "
14532 "and no declarations were found by "
14533 "argument-dependent lookup at the point "
14534 "of instantiation", function))
14535 {
14536 if (!DECL_P (fn))
14537 /* Can't say anything more. */;
14538 else if (DECL_CLASS_SCOPE_P (fn))
14539 {
14540 location_t loc = EXPR_LOC_OR_LOC (t,
14541 input_location);
14542 inform (loc,
14543 "declarations in dependent base %qT are "
14544 "not found by unqualified lookup",
14545 DECL_CLASS_CONTEXT (fn));
14546 if (current_class_ptr)
14547 inform (loc,
14548 "use %<this->%D%> instead", function);
14549 else
14550 inform (loc,
14551 "use %<%T::%D%> instead",
14552 current_class_name, function);
14553 }
14554 else
14555 inform (0, "%q+D declared here, later in the "
14556 "translation unit", fn);
14557 }
14558 function = unq;
14559 }
14560 }
14561 if (identifier_p (function))
14562 {
14563 if (complain & tf_error)
14564 unqualified_name_lookup_error (function);
14565 release_tree_vector (call_args);
14566 RETURN (error_mark_node);
14567 }
14568 }
14569
14570 /* Remember that there was a reference to this entity. */
14571 if (DECL_P (function))
14572 mark_used (function);
14573
14574 /* Put back tf_decltype for the actual call. */
14575 complain |= decltype_flag;
14576
14577 if (TREE_CODE (function) == OFFSET_REF)
14578 ret = build_offset_ref_call_from_tree (function, &call_args,
14579 complain);
14580 else if (TREE_CODE (function) == COMPONENT_REF)
14581 {
14582 tree instance = TREE_OPERAND (function, 0);
14583 tree fn = TREE_OPERAND (function, 1);
14584
14585 if (processing_template_decl
14586 && (type_dependent_expression_p (instance)
14587 || (!BASELINK_P (fn)
14588 && TREE_CODE (fn) != FIELD_DECL)
14589 || type_dependent_expression_p (fn)
14590 || any_type_dependent_arguments_p (call_args)))
14591 ret = build_nt_call_vec (function, call_args);
14592 else if (!BASELINK_P (fn))
14593 ret = finish_call_expr (function, &call_args,
14594 /*disallow_virtual=*/false,
14595 /*koenig_p=*/false,
14596 complain);
14597 else
14598 ret = (build_new_method_call
14599 (instance, fn,
14600 &call_args, NULL_TREE,
14601 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14602 /*fn_p=*/NULL,
14603 complain));
14604 }
14605 else
14606 ret = finish_call_expr (function, &call_args,
14607 /*disallow_virtual=*/qualified_p,
14608 koenig_p,
14609 complain);
14610
14611 release_tree_vector (call_args);
14612
14613 RETURN (ret);
14614 }
14615
14616 case COND_EXPR:
14617 {
14618 tree cond = RECUR (TREE_OPERAND (t, 0));
14619 tree exp1, exp2;
14620
14621 if (TREE_CODE (cond) == INTEGER_CST)
14622 {
14623 if (integer_zerop (cond))
14624 {
14625 ++c_inhibit_evaluation_warnings;
14626 exp1 = RECUR (TREE_OPERAND (t, 1));
14627 --c_inhibit_evaluation_warnings;
14628 exp2 = RECUR (TREE_OPERAND (t, 2));
14629 }
14630 else
14631 {
14632 exp1 = RECUR (TREE_OPERAND (t, 1));
14633 ++c_inhibit_evaluation_warnings;
14634 exp2 = RECUR (TREE_OPERAND (t, 2));
14635 --c_inhibit_evaluation_warnings;
14636 }
14637 }
14638 else
14639 {
14640 exp1 = RECUR (TREE_OPERAND (t, 1));
14641 exp2 = RECUR (TREE_OPERAND (t, 2));
14642 }
14643
14644 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14645 cond, exp1, exp2, complain));
14646 }
14647
14648 case PSEUDO_DTOR_EXPR:
14649 RETURN (finish_pseudo_destructor_expr
14650 (RECUR (TREE_OPERAND (t, 0)),
14651 RECUR (TREE_OPERAND (t, 1)),
14652 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14653 input_location));
14654
14655 case TREE_LIST:
14656 {
14657 tree purpose, value, chain;
14658
14659 if (t == void_list_node)
14660 RETURN (t);
14661
14662 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14663 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14664 {
14665 /* We have pack expansions, so expand those and
14666 create a new list out of it. */
14667 tree purposevec = NULL_TREE;
14668 tree valuevec = NULL_TREE;
14669 tree chain;
14670 int i, len = -1;
14671
14672 /* Expand the argument expressions. */
14673 if (TREE_PURPOSE (t))
14674 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14675 complain, in_decl);
14676 if (TREE_VALUE (t))
14677 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14678 complain, in_decl);
14679
14680 /* Build the rest of the list. */
14681 chain = TREE_CHAIN (t);
14682 if (chain && chain != void_type_node)
14683 chain = RECUR (chain);
14684
14685 /* Determine the number of arguments. */
14686 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14687 {
14688 len = TREE_VEC_LENGTH (purposevec);
14689 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14690 }
14691 else if (TREE_CODE (valuevec) == TREE_VEC)
14692 len = TREE_VEC_LENGTH (valuevec);
14693 else
14694 {
14695 /* Since we only performed a partial substitution into
14696 the argument pack, we only RETURN (a single list
14697 node. */
14698 if (purposevec == TREE_PURPOSE (t)
14699 && valuevec == TREE_VALUE (t)
14700 && chain == TREE_CHAIN (t))
14701 RETURN (t);
14702
14703 RETURN (tree_cons (purposevec, valuevec, chain));
14704 }
14705
14706 /* Convert the argument vectors into a TREE_LIST */
14707 i = len;
14708 while (i > 0)
14709 {
14710 /* Grab the Ith values. */
14711 i--;
14712 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14713 : NULL_TREE;
14714 value
14715 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14716 : NULL_TREE;
14717
14718 /* Build the list (backwards). */
14719 chain = tree_cons (purpose, value, chain);
14720 }
14721
14722 RETURN (chain);
14723 }
14724
14725 purpose = TREE_PURPOSE (t);
14726 if (purpose)
14727 purpose = RECUR (purpose);
14728 value = TREE_VALUE (t);
14729 if (value)
14730 value = RECUR (value);
14731 chain = TREE_CHAIN (t);
14732 if (chain && chain != void_type_node)
14733 chain = RECUR (chain);
14734 if (purpose == TREE_PURPOSE (t)
14735 && value == TREE_VALUE (t)
14736 && chain == TREE_CHAIN (t))
14737 RETURN (t);
14738 RETURN (tree_cons (purpose, value, chain));
14739 }
14740
14741 case COMPONENT_REF:
14742 {
14743 tree object;
14744 tree object_type;
14745 tree member;
14746
14747 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14748 args, complain, in_decl);
14749 /* Remember that there was a reference to this entity. */
14750 if (DECL_P (object))
14751 mark_used (object);
14752 object_type = TREE_TYPE (object);
14753
14754 member = TREE_OPERAND (t, 1);
14755 if (BASELINK_P (member))
14756 member = tsubst_baselink (member,
14757 non_reference (TREE_TYPE (object)),
14758 args, complain, in_decl);
14759 else
14760 member = tsubst_copy (member, args, complain, in_decl);
14761 if (member == error_mark_node)
14762 RETURN (error_mark_node);
14763
14764 if (type_dependent_expression_p (object))
14765 /* We can't do much here. */;
14766 else if (!CLASS_TYPE_P (object_type))
14767 {
14768 if (scalarish_type_p (object_type))
14769 {
14770 tree s = NULL_TREE;
14771 tree dtor = member;
14772
14773 if (TREE_CODE (dtor) == SCOPE_REF)
14774 {
14775 s = TREE_OPERAND (dtor, 0);
14776 dtor = TREE_OPERAND (dtor, 1);
14777 }
14778 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14779 {
14780 dtor = TREE_OPERAND (dtor, 0);
14781 if (TYPE_P (dtor))
14782 RETURN (finish_pseudo_destructor_expr
14783 (object, s, dtor, input_location));
14784 }
14785 }
14786 }
14787 else if (TREE_CODE (member) == SCOPE_REF
14788 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14789 {
14790 /* Lookup the template functions now that we know what the
14791 scope is. */
14792 tree scope = TREE_OPERAND (member, 0);
14793 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14794 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14795 member = lookup_qualified_name (scope, tmpl,
14796 /*is_type_p=*/false,
14797 /*complain=*/false);
14798 if (BASELINK_P (member))
14799 {
14800 BASELINK_FUNCTIONS (member)
14801 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14802 args);
14803 member = (adjust_result_of_qualified_name_lookup
14804 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14805 object_type));
14806 }
14807 else
14808 {
14809 qualified_name_lookup_error (scope, tmpl, member,
14810 input_location);
14811 RETURN (error_mark_node);
14812 }
14813 }
14814 else if (TREE_CODE (member) == SCOPE_REF
14815 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14816 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14817 {
14818 if (complain & tf_error)
14819 {
14820 if (TYPE_P (TREE_OPERAND (member, 0)))
14821 error ("%qT is not a class or namespace",
14822 TREE_OPERAND (member, 0));
14823 else
14824 error ("%qD is not a class or namespace",
14825 TREE_OPERAND (member, 0));
14826 }
14827 RETURN (error_mark_node);
14828 }
14829 else if (TREE_CODE (member) == FIELD_DECL)
14830 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14831
14832 RETURN (finish_class_member_access_expr (object, member,
14833 /*template_p=*/false,
14834 complain));
14835 }
14836
14837 case THROW_EXPR:
14838 RETURN (build_throw
14839 (RECUR (TREE_OPERAND (t, 0))));
14840
14841 case CONSTRUCTOR:
14842 {
14843 vec<constructor_elt, va_gc> *n;
14844 constructor_elt *ce;
14845 unsigned HOST_WIDE_INT idx;
14846 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14847 bool process_index_p;
14848 int newlen;
14849 bool need_copy_p = false;
14850 tree r;
14851
14852 if (type == error_mark_node)
14853 RETURN (error_mark_node);
14854
14855 /* digest_init will do the wrong thing if we let it. */
14856 if (type && TYPE_PTRMEMFUNC_P (type))
14857 RETURN (t);
14858
14859 /* We do not want to process the index of aggregate
14860 initializers as they are identifier nodes which will be
14861 looked up by digest_init. */
14862 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14863
14864 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14865 newlen = vec_safe_length (n);
14866 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14867 {
14868 if (ce->index && process_index_p
14869 /* An identifier index is looked up in the type
14870 being initialized, not the current scope. */
14871 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14872 ce->index = RECUR (ce->index);
14873
14874 if (PACK_EXPANSION_P (ce->value))
14875 {
14876 /* Substitute into the pack expansion. */
14877 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14878 in_decl);
14879
14880 if (ce->value == error_mark_node
14881 || PACK_EXPANSION_P (ce->value))
14882 ;
14883 else if (TREE_VEC_LENGTH (ce->value) == 1)
14884 /* Just move the argument into place. */
14885 ce->value = TREE_VEC_ELT (ce->value, 0);
14886 else
14887 {
14888 /* Update the length of the final CONSTRUCTOR
14889 arguments vector, and note that we will need to
14890 copy.*/
14891 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14892 need_copy_p = true;
14893 }
14894 }
14895 else
14896 ce->value = RECUR (ce->value);
14897 }
14898
14899 if (need_copy_p)
14900 {
14901 vec<constructor_elt, va_gc> *old_n = n;
14902
14903 vec_alloc (n, newlen);
14904 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14905 {
14906 if (TREE_CODE (ce->value) == TREE_VEC)
14907 {
14908 int i, len = TREE_VEC_LENGTH (ce->value);
14909 for (i = 0; i < len; ++i)
14910 CONSTRUCTOR_APPEND_ELT (n, 0,
14911 TREE_VEC_ELT (ce->value, i));
14912 }
14913 else
14914 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14915 }
14916 }
14917
14918 r = build_constructor (init_list_type_node, n);
14919 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14920
14921 if (TREE_HAS_CONSTRUCTOR (t))
14922 RETURN (finish_compound_literal (type, r, complain));
14923
14924 TREE_TYPE (r) = type;
14925 RETURN (r);
14926 }
14927
14928 case TYPEID_EXPR:
14929 {
14930 tree operand_0 = TREE_OPERAND (t, 0);
14931 if (TYPE_P (operand_0))
14932 {
14933 operand_0 = tsubst (operand_0, args, complain, in_decl);
14934 RETURN (get_typeid (operand_0, complain));
14935 }
14936 else
14937 {
14938 operand_0 = RECUR (operand_0);
14939 RETURN (build_typeid (operand_0, complain));
14940 }
14941 }
14942
14943 case VAR_DECL:
14944 if (!args)
14945 RETURN (t);
14946 else if (DECL_PACK_P (t))
14947 {
14948 /* We don't build decls for an instantiation of a
14949 variadic capture proxy, we instantiate the elements
14950 when needed. */
14951 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14952 return RECUR (DECL_VALUE_EXPR (t));
14953 }
14954 /* Fall through */
14955
14956 case PARM_DECL:
14957 {
14958 tree r = tsubst_copy (t, args, complain, in_decl);
14959
14960 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14961 /* If the original type was a reference, we'll be wrapped in
14962 the appropriate INDIRECT_REF. */
14963 r = convert_from_reference (r);
14964 RETURN (r);
14965 }
14966
14967 case VA_ARG_EXPR:
14968 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14969 RECUR (TREE_OPERAND (t, 0)),
14970 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14971
14972 case OFFSETOF_EXPR:
14973 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14974
14975 case TRAIT_EXPR:
14976 {
14977 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
14978 complain, in_decl);
14979
14980 tree type2 = TRAIT_EXPR_TYPE2 (t);
14981 if (type2)
14982 type2 = tsubst (type2, args, complain, in_decl);
14983
14984 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14985 }
14986
14987 case STMT_EXPR:
14988 {
14989 tree old_stmt_expr = cur_stmt_expr;
14990 tree stmt_expr = begin_stmt_expr ();
14991
14992 cur_stmt_expr = stmt_expr;
14993 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14994 integral_constant_expression_p);
14995 stmt_expr = finish_stmt_expr (stmt_expr, false);
14996 cur_stmt_expr = old_stmt_expr;
14997
14998 /* If the resulting list of expression statement is empty,
14999 fold it further into void_zero_node. */
15000 if (empty_expr_stmt_p (stmt_expr))
15001 stmt_expr = void_zero_node;
15002
15003 RETURN (stmt_expr);
15004 }
15005
15006 case LAMBDA_EXPR:
15007 {
15008 tree r = build_lambda_expr ();
15009
15010 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15011 LAMBDA_EXPR_CLOSURE (r) = type;
15012 CLASSTYPE_LAMBDA_EXPR (type) = r;
15013
15014 LAMBDA_EXPR_LOCATION (r)
15015 = LAMBDA_EXPR_LOCATION (t);
15016 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15017 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15018 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15019 LAMBDA_EXPR_DISCRIMINATOR (r)
15020 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15021 /* For a function scope, we want to use tsubst so that we don't
15022 complain about referring to an auto function before its return
15023 type has been deduced. Otherwise, we want to use tsubst_copy so
15024 that we look up the existing field/parameter/variable rather
15025 than build a new one. */
15026 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15027 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15028 scope = tsubst (scope, args, complain, in_decl);
15029 else if (scope && TREE_CODE (scope) == PARM_DECL)
15030 {
15031 /* Look up the parameter we want directly, as tsubst_copy
15032 doesn't do what we need. */
15033 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15034 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15035 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15036 parm = DECL_CHAIN (parm);
15037 scope = parm;
15038 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15039 if (DECL_CONTEXT (scope) == NULL_TREE)
15040 DECL_CONTEXT (scope) = fn;
15041 }
15042 else
15043 scope = RECUR (scope);
15044 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15045 LAMBDA_EXPR_RETURN_TYPE (r)
15046 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15047
15048 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15049 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15050
15051 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15052 determine_visibility (TYPE_NAME (type));
15053 /* Now that we know visibility, instantiate the type so we have a
15054 declaration of the op() for later calls to lambda_function. */
15055 complete_type (type);
15056
15057 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15058
15059 RETURN (build_lambda_object (r));
15060 }
15061
15062 case TARGET_EXPR:
15063 /* We can get here for a constant initializer of non-dependent type.
15064 FIXME stop folding in cp_parser_initializer_clause. */
15065 {
15066 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15067 complain);
15068 RETURN (r);
15069 }
15070
15071 case TRANSACTION_EXPR:
15072 RETURN (tsubst_expr(t, args, complain, in_decl,
15073 integral_constant_expression_p));
15074
15075 case PAREN_EXPR:
15076 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15077
15078 case VEC_PERM_EXPR:
15079 RETURN (build_x_vec_perm_expr (input_location,
15080 RECUR (TREE_OPERAND (t, 0)),
15081 RECUR (TREE_OPERAND (t, 1)),
15082 RECUR (TREE_OPERAND (t, 2)),
15083 complain));
15084
15085 default:
15086 /* Handle Objective-C++ constructs, if appropriate. */
15087 {
15088 tree subst
15089 = objcp_tsubst_copy_and_build (t, args, complain,
15090 in_decl, /*function_p=*/false);
15091 if (subst)
15092 RETURN (subst);
15093 }
15094 RETURN (tsubst_copy (t, args, complain, in_decl));
15095 }
15096
15097 #undef RECUR
15098 #undef RETURN
15099 out:
15100 input_location = loc;
15101 return retval;
15102 }
15103
15104 /* Verify that the instantiated ARGS are valid. For type arguments,
15105 make sure that the type's linkage is ok. For non-type arguments,
15106 make sure they are constants if they are integral or enumerations.
15107 Emit an error under control of COMPLAIN, and return TRUE on error. */
15108
15109 static bool
15110 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15111 {
15112 if (dependent_template_arg_p (t))
15113 return false;
15114 if (ARGUMENT_PACK_P (t))
15115 {
15116 tree vec = ARGUMENT_PACK_ARGS (t);
15117 int len = TREE_VEC_LENGTH (vec);
15118 bool result = false;
15119 int i;
15120
15121 for (i = 0; i < len; ++i)
15122 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15123 result = true;
15124 return result;
15125 }
15126 else if (TYPE_P (t))
15127 {
15128 /* [basic.link]: A name with no linkage (notably, the name
15129 of a class or enumeration declared in a local scope)
15130 shall not be used to declare an entity with linkage.
15131 This implies that names with no linkage cannot be used as
15132 template arguments
15133
15134 DR 757 relaxes this restriction for C++0x. */
15135 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15136 : no_linkage_check (t, /*relaxed_p=*/false));
15137
15138 if (nt)
15139 {
15140 /* DR 488 makes use of a type with no linkage cause
15141 type deduction to fail. */
15142 if (complain & tf_error)
15143 {
15144 if (TYPE_ANONYMOUS_P (nt))
15145 error ("%qT is/uses anonymous type", t);
15146 else
15147 error ("template argument for %qD uses local type %qT",
15148 tmpl, t);
15149 }
15150 return true;
15151 }
15152 /* In order to avoid all sorts of complications, we do not
15153 allow variably-modified types as template arguments. */
15154 else if (variably_modified_type_p (t, NULL_TREE))
15155 {
15156 if (complain & tf_error)
15157 error ("%qT is a variably modified type", t);
15158 return true;
15159 }
15160 }
15161 /* Class template and alias template arguments should be OK. */
15162 else if (DECL_TYPE_TEMPLATE_P (t))
15163 ;
15164 /* A non-type argument of integral or enumerated type must be a
15165 constant. */
15166 else if (TREE_TYPE (t)
15167 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15168 && !TREE_CONSTANT (t))
15169 {
15170 if (complain & tf_error)
15171 error ("integral expression %qE is not constant", t);
15172 return true;
15173 }
15174 return false;
15175 }
15176
15177 static bool
15178 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15179 {
15180 int ix, len = DECL_NTPARMS (tmpl);
15181 bool result = false;
15182
15183 for (ix = 0; ix != len; ix++)
15184 {
15185 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15186 result = true;
15187 }
15188 if (result && (complain & tf_error))
15189 error (" trying to instantiate %qD", tmpl);
15190 return result;
15191 }
15192
15193 /* We're out of SFINAE context now, so generate diagnostics for the access
15194 errors we saw earlier when instantiating D from TMPL and ARGS. */
15195
15196 static void
15197 recheck_decl_substitution (tree d, tree tmpl, tree args)
15198 {
15199 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15200 tree type = TREE_TYPE (pattern);
15201 location_t loc = input_location;
15202
15203 push_access_scope (d);
15204 push_deferring_access_checks (dk_no_deferred);
15205 input_location = DECL_SOURCE_LOCATION (pattern);
15206 tsubst (type, args, tf_warning_or_error, d);
15207 input_location = loc;
15208 pop_deferring_access_checks ();
15209 pop_access_scope (d);
15210 }
15211
15212 /* Instantiate the indicated variable, function, or alias template TMPL with
15213 the template arguments in TARG_PTR. */
15214
15215 static tree
15216 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15217 {
15218 tree targ_ptr = orig_args;
15219 tree fndecl;
15220 tree gen_tmpl;
15221 tree spec;
15222 bool access_ok = true;
15223
15224 if (tmpl == error_mark_node)
15225 return error_mark_node;
15226
15227 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15228
15229 /* If this function is a clone, handle it specially. */
15230 if (DECL_CLONED_FUNCTION_P (tmpl))
15231 {
15232 tree spec;
15233 tree clone;
15234
15235 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15236 DECL_CLONED_FUNCTION. */
15237 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15238 targ_ptr, complain);
15239 if (spec == error_mark_node)
15240 return error_mark_node;
15241
15242 /* Look for the clone. */
15243 FOR_EACH_CLONE (clone, spec)
15244 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15245 return clone;
15246 /* We should always have found the clone by now. */
15247 gcc_unreachable ();
15248 return NULL_TREE;
15249 }
15250
15251 /* Check to see if we already have this specialization. */
15252 gen_tmpl = most_general_template (tmpl);
15253 if (tmpl != gen_tmpl)
15254 /* The TMPL is a partial instantiation. To get a full set of
15255 arguments we must add the arguments used to perform the
15256 partial instantiation. */
15257 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15258 targ_ptr);
15259
15260 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15261 but it doesn't seem to be on the hot path. */
15262 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15263
15264 gcc_assert (tmpl == gen_tmpl
15265 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15266 == spec)
15267 || fndecl == NULL_TREE);
15268
15269 if (spec != NULL_TREE)
15270 {
15271 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15272 {
15273 if (complain & tf_error)
15274 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15275 return error_mark_node;
15276 }
15277 return spec;
15278 }
15279
15280 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15281 complain))
15282 return error_mark_node;
15283
15284 /* We are building a FUNCTION_DECL, during which the access of its
15285 parameters and return types have to be checked. However this
15286 FUNCTION_DECL which is the desired context for access checking
15287 is not built yet. We solve this chicken-and-egg problem by
15288 deferring all checks until we have the FUNCTION_DECL. */
15289 push_deferring_access_checks (dk_deferred);
15290
15291 /* Instantiation of the function happens in the context of the function
15292 template, not the context of the overload resolution we're doing. */
15293 push_to_top_level ();
15294 /* If there are dependent arguments, e.g. because we're doing partial
15295 ordering, make sure processing_template_decl stays set. */
15296 if (uses_template_parms (targ_ptr))
15297 ++processing_template_decl;
15298 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15299 {
15300 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15301 complain, gen_tmpl);
15302 push_nested_class (ctx);
15303 }
15304 /* Substitute template parameters to obtain the specialization. */
15305 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15306 targ_ptr, complain, gen_tmpl);
15307 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15308 pop_nested_class ();
15309 pop_from_top_level ();
15310
15311 if (fndecl == error_mark_node)
15312 {
15313 pop_deferring_access_checks ();
15314 return error_mark_node;
15315 }
15316
15317 /* The DECL_TI_TEMPLATE should always be the immediate parent
15318 template, not the most general template. */
15319 DECL_TI_TEMPLATE (fndecl) = tmpl;
15320
15321 /* Now we know the specialization, compute access previously
15322 deferred. */
15323 push_access_scope (fndecl);
15324 if (!perform_deferred_access_checks (complain))
15325 access_ok = false;
15326 pop_access_scope (fndecl);
15327 pop_deferring_access_checks ();
15328
15329 /* If we've just instantiated the main entry point for a function,
15330 instantiate all the alternate entry points as well. We do this
15331 by cloning the instantiation of the main entry point, not by
15332 instantiating the template clones. */
15333 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15334 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15335
15336 if (!access_ok)
15337 {
15338 if (!(complain & tf_error))
15339 {
15340 /* Remember to reinstantiate when we're out of SFINAE so the user
15341 can see the errors. */
15342 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15343 }
15344 return error_mark_node;
15345 }
15346 return fndecl;
15347 }
15348
15349 /* Wrapper for instantiate_template_1. */
15350
15351 tree
15352 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15353 {
15354 tree ret;
15355 timevar_push (TV_TEMPLATE_INST);
15356 ret = instantiate_template_1 (tmpl, orig_args, complain);
15357 timevar_pop (TV_TEMPLATE_INST);
15358 return ret;
15359 }
15360
15361 /* Instantiate the alias template TMPL with ARGS. Also push a template
15362 instantiation level, which instantiate_template doesn't do because
15363 functions and variables have sufficient context established by the
15364 callers. */
15365
15366 static tree
15367 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15368 {
15369 struct pending_template *old_last_pend = last_pending_template;
15370 struct tinst_level *old_error_tinst = last_error_tinst_level;
15371 if (tmpl == error_mark_node || args == error_mark_node)
15372 return error_mark_node;
15373 tree tinst = build_tree_list (tmpl, args);
15374 if (!push_tinst_level (tinst))
15375 {
15376 ggc_free (tinst);
15377 return error_mark_node;
15378 }
15379
15380 args =
15381 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15382 args, tmpl, complain,
15383 /*require_all_args=*/true,
15384 /*use_default_args=*/true);
15385
15386 tree r = instantiate_template (tmpl, args, complain);
15387 pop_tinst_level ();
15388 /* We can't free this if a pending_template entry or last_error_tinst_level
15389 is pointing at it. */
15390 if (last_pending_template == old_last_pend
15391 && last_error_tinst_level == old_error_tinst)
15392 ggc_free (tinst);
15393
15394 return r;
15395 }
15396
15397 /* PARM is a template parameter pack for FN. Returns true iff
15398 PARM is used in a deducible way in the argument list of FN. */
15399
15400 static bool
15401 pack_deducible_p (tree parm, tree fn)
15402 {
15403 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15404 for (; t; t = TREE_CHAIN (t))
15405 {
15406 tree type = TREE_VALUE (t);
15407 tree packs;
15408 if (!PACK_EXPANSION_P (type))
15409 continue;
15410 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15411 packs; packs = TREE_CHAIN (packs))
15412 if (TREE_VALUE (packs) == parm)
15413 {
15414 /* The template parameter pack is used in a function parameter
15415 pack. If this is the end of the parameter list, the
15416 template parameter pack is deducible. */
15417 if (TREE_CHAIN (t) == void_list_node)
15418 return true;
15419 else
15420 /* Otherwise, not. Well, it could be deduced from
15421 a non-pack parameter, but doing so would end up with
15422 a deduction mismatch, so don't bother. */
15423 return false;
15424 }
15425 }
15426 /* The template parameter pack isn't used in any function parameter
15427 packs, but it might be used deeper, e.g. tuple<Args...>. */
15428 return true;
15429 }
15430
15431 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15432 NARGS elements of the arguments that are being used when calling
15433 it. TARGS is a vector into which the deduced template arguments
15434 are placed.
15435
15436 Returns either a FUNCTION_DECL for the matching specialization of FN or
15437 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15438 true, diagnostics will be printed to explain why it failed.
15439
15440 If FN is a conversion operator, or we are trying to produce a specific
15441 specialization, RETURN_TYPE is the return type desired.
15442
15443 The EXPLICIT_TARGS are explicit template arguments provided via a
15444 template-id.
15445
15446 The parameter STRICT is one of:
15447
15448 DEDUCE_CALL:
15449 We are deducing arguments for a function call, as in
15450 [temp.deduct.call].
15451
15452 DEDUCE_CONV:
15453 We are deducing arguments for a conversion function, as in
15454 [temp.deduct.conv].
15455
15456 DEDUCE_EXACT:
15457 We are deducing arguments when doing an explicit instantiation
15458 as in [temp.explicit], when determining an explicit specialization
15459 as in [temp.expl.spec], or when taking the address of a function
15460 template, as in [temp.deduct.funcaddr]. */
15461
15462 tree
15463 fn_type_unification (tree fn,
15464 tree explicit_targs,
15465 tree targs,
15466 const tree *args,
15467 unsigned int nargs,
15468 tree return_type,
15469 unification_kind_t strict,
15470 int flags,
15471 bool explain_p,
15472 bool decltype_p)
15473 {
15474 tree parms;
15475 tree fntype;
15476 tree decl = NULL_TREE;
15477 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15478 bool ok;
15479 static int deduction_depth;
15480 struct pending_template *old_last_pend = last_pending_template;
15481 struct tinst_level *old_error_tinst = last_error_tinst_level;
15482 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15483 tree tinst;
15484 tree r = error_mark_node;
15485
15486 if (decltype_p)
15487 complain |= tf_decltype;
15488
15489 /* In C++0x, it's possible to have a function template whose type depends
15490 on itself recursively. This is most obvious with decltype, but can also
15491 occur with enumeration scope (c++/48969). So we need to catch infinite
15492 recursion and reject the substitution at deduction time; this function
15493 will return error_mark_node for any repeated substitution.
15494
15495 This also catches excessive recursion such as when f<N> depends on
15496 f<N-1> across all integers, and returns error_mark_node for all the
15497 substitutions back up to the initial one.
15498
15499 This is, of course, not reentrant. */
15500 if (excessive_deduction_depth)
15501 return error_mark_node;
15502 tinst = build_tree_list (fn, NULL_TREE);
15503 ++deduction_depth;
15504
15505 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15506
15507 fntype = TREE_TYPE (fn);
15508 if (explicit_targs)
15509 {
15510 /* [temp.deduct]
15511
15512 The specified template arguments must match the template
15513 parameters in kind (i.e., type, nontype, template), and there
15514 must not be more arguments than there are parameters;
15515 otherwise type deduction fails.
15516
15517 Nontype arguments must match the types of the corresponding
15518 nontype template parameters, or must be convertible to the
15519 types of the corresponding nontype parameters as specified in
15520 _temp.arg.nontype_, otherwise type deduction fails.
15521
15522 All references in the function type of the function template
15523 to the corresponding template parameters are replaced by the
15524 specified template argument values. If a substitution in a
15525 template parameter or in the function type of the function
15526 template results in an invalid type, type deduction fails. */
15527 int i, len = TREE_VEC_LENGTH (tparms);
15528 location_t loc = input_location;
15529 bool incomplete = false;
15530
15531 /* Adjust any explicit template arguments before entering the
15532 substitution context. */
15533 explicit_targs
15534 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15535 complain,
15536 /*require_all_args=*/false,
15537 /*use_default_args=*/false));
15538 if (explicit_targs == error_mark_node)
15539 goto fail;
15540
15541 /* Substitute the explicit args into the function type. This is
15542 necessary so that, for instance, explicitly declared function
15543 arguments can match null pointed constants. If we were given
15544 an incomplete set of explicit args, we must not do semantic
15545 processing during substitution as we could create partial
15546 instantiations. */
15547 for (i = 0; i < len; i++)
15548 {
15549 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15550 bool parameter_pack = false;
15551 tree targ = TREE_VEC_ELT (explicit_targs, i);
15552
15553 /* Dig out the actual parm. */
15554 if (TREE_CODE (parm) == TYPE_DECL
15555 || TREE_CODE (parm) == TEMPLATE_DECL)
15556 {
15557 parm = TREE_TYPE (parm);
15558 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15559 }
15560 else if (TREE_CODE (parm) == PARM_DECL)
15561 {
15562 parm = DECL_INITIAL (parm);
15563 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15564 }
15565
15566 if (!parameter_pack && targ == NULL_TREE)
15567 /* No explicit argument for this template parameter. */
15568 incomplete = true;
15569
15570 if (parameter_pack && pack_deducible_p (parm, fn))
15571 {
15572 /* Mark the argument pack as "incomplete". We could
15573 still deduce more arguments during unification.
15574 We remove this mark in type_unification_real. */
15575 if (targ)
15576 {
15577 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15578 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15579 = ARGUMENT_PACK_ARGS (targ);
15580 }
15581
15582 /* We have some incomplete argument packs. */
15583 incomplete = true;
15584 }
15585 }
15586
15587 TREE_VALUE (tinst) = explicit_targs;
15588 if (!push_tinst_level (tinst))
15589 {
15590 excessive_deduction_depth = true;
15591 goto fail;
15592 }
15593 processing_template_decl += incomplete;
15594 input_location = DECL_SOURCE_LOCATION (fn);
15595 /* Ignore any access checks; we'll see them again in
15596 instantiate_template and they might have the wrong
15597 access path at this point. */
15598 push_deferring_access_checks (dk_deferred);
15599 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15600 complain | tf_partial, NULL_TREE);
15601 pop_deferring_access_checks ();
15602 input_location = loc;
15603 processing_template_decl -= incomplete;
15604 pop_tinst_level ();
15605
15606 if (fntype == error_mark_node)
15607 goto fail;
15608
15609 /* Place the explicitly specified arguments in TARGS. */
15610 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15611 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15612 }
15613
15614 /* Never do unification on the 'this' parameter. */
15615 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15616
15617 if (return_type)
15618 {
15619 tree *new_args;
15620
15621 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15622 new_args = XALLOCAVEC (tree, nargs + 1);
15623 new_args[0] = return_type;
15624 memcpy (new_args + 1, args, nargs * sizeof (tree));
15625 args = new_args;
15626 ++nargs;
15627 }
15628
15629 /* We allow incomplete unification without an error message here
15630 because the standard doesn't seem to explicitly prohibit it. Our
15631 callers must be ready to deal with unification failures in any
15632 event. */
15633
15634 TREE_VALUE (tinst) = targs;
15635 /* If we aren't explaining yet, push tinst context so we can see where
15636 any errors (e.g. from class instantiations triggered by instantiation
15637 of default template arguments) come from. If we are explaining, this
15638 context is redundant. */
15639 if (!explain_p && !push_tinst_level (tinst))
15640 {
15641 excessive_deduction_depth = true;
15642 goto fail;
15643 }
15644
15645 /* type_unification_real will pass back any access checks from default
15646 template argument substitution. */
15647 vec<deferred_access_check, va_gc> *checks;
15648 checks = NULL;
15649
15650 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15651 targs, parms, args, nargs, /*subr=*/0,
15652 strict, flags, &checks, explain_p);
15653 if (!explain_p)
15654 pop_tinst_level ();
15655 if (!ok)
15656 goto fail;
15657
15658 /* Now that we have bindings for all of the template arguments,
15659 ensure that the arguments deduced for the template template
15660 parameters have compatible template parameter lists. We cannot
15661 check this property before we have deduced all template
15662 arguments, because the template parameter types of a template
15663 template parameter might depend on prior template parameters
15664 deduced after the template template parameter. The following
15665 ill-formed example illustrates this issue:
15666
15667 template<typename T, template<T> class C> void f(C<5>, T);
15668
15669 template<int N> struct X {};
15670
15671 void g() {
15672 f(X<5>(), 5l); // error: template argument deduction fails
15673 }
15674
15675 The template parameter list of 'C' depends on the template type
15676 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15677 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15678 time that we deduce 'C'. */
15679 if (!template_template_parm_bindings_ok_p
15680 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15681 {
15682 unify_inconsistent_template_template_parameters (explain_p);
15683 goto fail;
15684 }
15685
15686 /* All is well so far. Now, check:
15687
15688 [temp.deduct]
15689
15690 When all template arguments have been deduced, all uses of
15691 template parameters in nondeduced contexts are replaced with
15692 the corresponding deduced argument values. If the
15693 substitution results in an invalid type, as described above,
15694 type deduction fails. */
15695 TREE_VALUE (tinst) = targs;
15696 if (!push_tinst_level (tinst))
15697 {
15698 excessive_deduction_depth = true;
15699 goto fail;
15700 }
15701
15702 /* Also collect access checks from the instantiation. */
15703 reopen_deferring_access_checks (checks);
15704
15705 decl = instantiate_template (fn, targs, complain);
15706
15707 checks = get_deferred_access_checks ();
15708 pop_deferring_access_checks ();
15709
15710 pop_tinst_level ();
15711
15712 if (decl == error_mark_node)
15713 goto fail;
15714
15715 /* Now perform any access checks encountered during substitution. */
15716 push_access_scope (decl);
15717 ok = perform_access_checks (checks, complain);
15718 pop_access_scope (decl);
15719 if (!ok)
15720 goto fail;
15721
15722 /* If we're looking for an exact match, check that what we got
15723 is indeed an exact match. It might not be if some template
15724 parameters are used in non-deduced contexts. */
15725 if (strict == DEDUCE_EXACT)
15726 {
15727 tree substed = TREE_TYPE (decl);
15728 unsigned int i;
15729
15730 tree sarg
15731 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15732 if (return_type)
15733 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15734 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15735 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15736 {
15737 unify_type_mismatch (explain_p, args[i],
15738 TREE_VALUE (sarg));
15739 goto fail;
15740 }
15741 }
15742
15743 r = decl;
15744
15745 fail:
15746 --deduction_depth;
15747 if (excessive_deduction_depth)
15748 {
15749 if (deduction_depth == 0)
15750 /* Reset once we're all the way out. */
15751 excessive_deduction_depth = false;
15752 }
15753
15754 /* We can't free this if a pending_template entry or last_error_tinst_level
15755 is pointing at it. */
15756 if (last_pending_template == old_last_pend
15757 && last_error_tinst_level == old_error_tinst)
15758 ggc_free (tinst);
15759
15760 return r;
15761 }
15762
15763 /* Adjust types before performing type deduction, as described in
15764 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15765 sections are symmetric. PARM is the type of a function parameter
15766 or the return type of the conversion function. ARG is the type of
15767 the argument passed to the call, or the type of the value
15768 initialized with the result of the conversion function.
15769 ARG_EXPR is the original argument expression, which may be null. */
15770
15771 static int
15772 maybe_adjust_types_for_deduction (unification_kind_t strict,
15773 tree* parm,
15774 tree* arg,
15775 tree arg_expr)
15776 {
15777 int result = 0;
15778
15779 switch (strict)
15780 {
15781 case DEDUCE_CALL:
15782 break;
15783
15784 case DEDUCE_CONV:
15785 {
15786 /* Swap PARM and ARG throughout the remainder of this
15787 function; the handling is precisely symmetric since PARM
15788 will initialize ARG rather than vice versa. */
15789 tree* temp = parm;
15790 parm = arg;
15791 arg = temp;
15792 break;
15793 }
15794
15795 case DEDUCE_EXACT:
15796 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15797 too, but here handle it by stripping the reference from PARM
15798 rather than by adding it to ARG. */
15799 if (TREE_CODE (*parm) == REFERENCE_TYPE
15800 && TYPE_REF_IS_RVALUE (*parm)
15801 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15802 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15803 && TREE_CODE (*arg) == REFERENCE_TYPE
15804 && !TYPE_REF_IS_RVALUE (*arg))
15805 *parm = TREE_TYPE (*parm);
15806 /* Nothing else to do in this case. */
15807 return 0;
15808
15809 default:
15810 gcc_unreachable ();
15811 }
15812
15813 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15814 {
15815 /* [temp.deduct.call]
15816
15817 If P is not a reference type:
15818
15819 --If A is an array type, the pointer type produced by the
15820 array-to-pointer standard conversion (_conv.array_) is
15821 used in place of A for type deduction; otherwise,
15822
15823 --If A is a function type, the pointer type produced by
15824 the function-to-pointer standard conversion
15825 (_conv.func_) is used in place of A for type deduction;
15826 otherwise,
15827
15828 --If A is a cv-qualified type, the top level
15829 cv-qualifiers of A's type are ignored for type
15830 deduction. */
15831 if (TREE_CODE (*arg) == ARRAY_TYPE)
15832 *arg = build_pointer_type (TREE_TYPE (*arg));
15833 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15834 *arg = build_pointer_type (*arg);
15835 else
15836 *arg = TYPE_MAIN_VARIANT (*arg);
15837 }
15838
15839 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15840 of the form T&&, where T is a template parameter, and the argument
15841 is an lvalue, T is deduced as A& */
15842 if (TREE_CODE (*parm) == REFERENCE_TYPE
15843 && TYPE_REF_IS_RVALUE (*parm)
15844 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15845 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15846 && (arg_expr ? real_lvalue_p (arg_expr)
15847 /* try_one_overload doesn't provide an arg_expr, but
15848 functions are always lvalues. */
15849 : TREE_CODE (*arg) == FUNCTION_TYPE))
15850 *arg = build_reference_type (*arg);
15851
15852 /* [temp.deduct.call]
15853
15854 If P is a cv-qualified type, the top level cv-qualifiers
15855 of P's type are ignored for type deduction. If P is a
15856 reference type, the type referred to by P is used for
15857 type deduction. */
15858 *parm = TYPE_MAIN_VARIANT (*parm);
15859 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15860 {
15861 *parm = TREE_TYPE (*parm);
15862 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15863 }
15864
15865 /* DR 322. For conversion deduction, remove a reference type on parm
15866 too (which has been swapped into ARG). */
15867 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15868 *arg = TREE_TYPE (*arg);
15869
15870 return result;
15871 }
15872
15873 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15874 template which does contain any deducible template parameters; check if
15875 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15876 unify_one_argument. */
15877
15878 static int
15879 check_non_deducible_conversion (tree parm, tree arg, int strict,
15880 int flags, bool explain_p)
15881 {
15882 tree type;
15883
15884 if (!TYPE_P (arg))
15885 type = TREE_TYPE (arg);
15886 else
15887 type = arg;
15888
15889 if (same_type_p (parm, type))
15890 return unify_success (explain_p);
15891
15892 if (strict == DEDUCE_CONV)
15893 {
15894 if (can_convert_arg (type, parm, NULL_TREE, flags,
15895 explain_p ? tf_warning_or_error : tf_none))
15896 return unify_success (explain_p);
15897 }
15898 else if (strict != DEDUCE_EXACT)
15899 {
15900 if (can_convert_arg (parm, type,
15901 TYPE_P (arg) ? NULL_TREE : arg,
15902 flags, explain_p ? tf_warning_or_error : tf_none))
15903 return unify_success (explain_p);
15904 }
15905
15906 if (strict == DEDUCE_EXACT)
15907 return unify_type_mismatch (explain_p, parm, arg);
15908 else
15909 return unify_arg_conversion (explain_p, parm, type, arg);
15910 }
15911
15912 static bool uses_deducible_template_parms (tree type);
15913
15914 /* Returns true iff the expression EXPR is one from which a template
15915 argument can be deduced. In other words, if it's an undecorated
15916 use of a template non-type parameter. */
15917
15918 static bool
15919 deducible_expression (tree expr)
15920 {
15921 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15922 }
15923
15924 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15925 deducible way; that is, if it has a max value of <PARM> - 1. */
15926
15927 static bool
15928 deducible_array_bound (tree domain)
15929 {
15930 if (domain == NULL_TREE)
15931 return false;
15932
15933 tree max = TYPE_MAX_VALUE (domain);
15934 if (TREE_CODE (max) != MINUS_EXPR)
15935 return false;
15936
15937 return deducible_expression (TREE_OPERAND (max, 0));
15938 }
15939
15940 /* Returns true iff the template arguments ARGS use a template parameter
15941 in a deducible way. */
15942
15943 static bool
15944 deducible_template_args (tree args)
15945 {
15946 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15947 {
15948 bool deducible;
15949 tree elt = TREE_VEC_ELT (args, i);
15950 if (ARGUMENT_PACK_P (elt))
15951 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15952 else
15953 {
15954 if (PACK_EXPANSION_P (elt))
15955 elt = PACK_EXPANSION_PATTERN (elt);
15956 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15957 deducible = true;
15958 else if (TYPE_P (elt))
15959 deducible = uses_deducible_template_parms (elt);
15960 else
15961 deducible = deducible_expression (elt);
15962 }
15963 if (deducible)
15964 return true;
15965 }
15966 return false;
15967 }
15968
15969 /* Returns true iff TYPE contains any deducible references to template
15970 parameters, as per 14.8.2.5. */
15971
15972 static bool
15973 uses_deducible_template_parms (tree type)
15974 {
15975 if (PACK_EXPANSION_P (type))
15976 type = PACK_EXPANSION_PATTERN (type);
15977
15978 /* T
15979 cv-list T
15980 TT<T>
15981 TT<i>
15982 TT<> */
15983 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15984 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15985 return true;
15986
15987 /* T*
15988 T&
15989 T&& */
15990 if (POINTER_TYPE_P (type))
15991 return uses_deducible_template_parms (TREE_TYPE (type));
15992
15993 /* T[integer-constant ]
15994 type [i] */
15995 if (TREE_CODE (type) == ARRAY_TYPE)
15996 return (uses_deducible_template_parms (TREE_TYPE (type))
15997 || deducible_array_bound (TYPE_DOMAIN (type)));
15998
15999 /* T type ::*
16000 type T::*
16001 T T::*
16002 T (type ::*)()
16003 type (T::*)()
16004 type (type ::*)(T)
16005 type (T::*)(T)
16006 T (type ::*)(T)
16007 T (T::*)()
16008 T (T::*)(T) */
16009 if (TYPE_PTRMEM_P (type))
16010 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16011 || (uses_deducible_template_parms
16012 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16013
16014 /* template-name <T> (where template-name refers to a class template)
16015 template-name <i> (where template-name refers to a class template) */
16016 if (CLASS_TYPE_P (type)
16017 && CLASSTYPE_TEMPLATE_INFO (type)
16018 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16019 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16020 (CLASSTYPE_TI_ARGS (type)));
16021
16022 /* type (T)
16023 T()
16024 T(T) */
16025 if (TREE_CODE (type) == FUNCTION_TYPE
16026 || TREE_CODE (type) == METHOD_TYPE)
16027 {
16028 if (uses_deducible_template_parms (TREE_TYPE (type)))
16029 return true;
16030 tree parm = TYPE_ARG_TYPES (type);
16031 if (TREE_CODE (type) == METHOD_TYPE)
16032 parm = TREE_CHAIN (parm);
16033 for (; parm; parm = TREE_CHAIN (parm))
16034 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16035 return true;
16036 }
16037
16038 return false;
16039 }
16040
16041 /* Subroutine of type_unification_real and unify_pack_expansion to
16042 handle unification of a single P/A pair. Parameters are as
16043 for those functions. */
16044
16045 static int
16046 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16047 int subr, unification_kind_t strict, int flags,
16048 bool explain_p)
16049 {
16050 tree arg_expr = NULL_TREE;
16051 int arg_strict;
16052
16053 if (arg == error_mark_node || parm == error_mark_node)
16054 return unify_invalid (explain_p);
16055 if (arg == unknown_type_node)
16056 /* We can't deduce anything from this, but we might get all the
16057 template args from other function args. */
16058 return unify_success (explain_p);
16059
16060 /* Implicit conversions (Clause 4) will be performed on a function
16061 argument to convert it to the type of the corresponding function
16062 parameter if the parameter type contains no template-parameters that
16063 participate in template argument deduction. */
16064 if (TYPE_P (parm) && !uses_template_parms (parm))
16065 /* For function parameters that contain no template-parameters at all,
16066 we have historically checked for convertibility in order to shortcut
16067 consideration of this candidate. */
16068 return check_non_deducible_conversion (parm, arg, strict, flags,
16069 explain_p);
16070 else if (strict == DEDUCE_CALL
16071 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16072 /* For function parameters with only non-deducible template parameters,
16073 just return. */
16074 return unify_success (explain_p);
16075
16076 switch (strict)
16077 {
16078 case DEDUCE_CALL:
16079 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16080 | UNIFY_ALLOW_MORE_CV_QUAL
16081 | UNIFY_ALLOW_DERIVED);
16082 break;
16083
16084 case DEDUCE_CONV:
16085 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16086 break;
16087
16088 case DEDUCE_EXACT:
16089 arg_strict = UNIFY_ALLOW_NONE;
16090 break;
16091
16092 default:
16093 gcc_unreachable ();
16094 }
16095
16096 /* We only do these transformations if this is the top-level
16097 parameter_type_list in a call or declaration matching; in other
16098 situations (nested function declarators, template argument lists) we
16099 won't be comparing a type to an expression, and we don't do any type
16100 adjustments. */
16101 if (!subr)
16102 {
16103 if (!TYPE_P (arg))
16104 {
16105 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16106 if (type_unknown_p (arg))
16107 {
16108 /* [temp.deduct.type] A template-argument can be
16109 deduced from a pointer to function or pointer
16110 to member function argument if the set of
16111 overloaded functions does not contain function
16112 templates and at most one of a set of
16113 overloaded functions provides a unique
16114 match. */
16115
16116 if (resolve_overloaded_unification
16117 (tparms, targs, parm, arg, strict,
16118 arg_strict, explain_p))
16119 return unify_success (explain_p);
16120 return unify_overload_resolution_failure (explain_p, arg);
16121 }
16122
16123 arg_expr = arg;
16124 arg = unlowered_expr_type (arg);
16125 if (arg == error_mark_node)
16126 return unify_invalid (explain_p);
16127 }
16128
16129 arg_strict |=
16130 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16131 }
16132 else
16133 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16134 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16135
16136 /* For deduction from an init-list we need the actual list. */
16137 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16138 arg = arg_expr;
16139 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16140 }
16141
16142 /* Most parms like fn_type_unification.
16143
16144 If SUBR is 1, we're being called recursively (to unify the
16145 arguments of a function or method parameter of a function
16146 template).
16147
16148 CHECKS is a pointer to a vector of access checks encountered while
16149 substituting default template arguments. */
16150
16151 static int
16152 type_unification_real (tree tparms,
16153 tree targs,
16154 tree xparms,
16155 const tree *xargs,
16156 unsigned int xnargs,
16157 int subr,
16158 unification_kind_t strict,
16159 int flags,
16160 vec<deferred_access_check, va_gc> **checks,
16161 bool explain_p)
16162 {
16163 tree parm, arg;
16164 int i;
16165 int ntparms = TREE_VEC_LENGTH (tparms);
16166 int saw_undeduced = 0;
16167 tree parms;
16168 const tree *args;
16169 unsigned int nargs;
16170 unsigned int ia;
16171
16172 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16173 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16174 gcc_assert (ntparms > 0);
16175
16176 /* Reset the number of non-defaulted template arguments contained
16177 in TARGS. */
16178 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16179
16180 again:
16181 parms = xparms;
16182 args = xargs;
16183 nargs = xnargs;
16184
16185 ia = 0;
16186 while (parms && parms != void_list_node
16187 && ia < nargs)
16188 {
16189 parm = TREE_VALUE (parms);
16190
16191 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16192 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16193 /* For a function parameter pack that occurs at the end of the
16194 parameter-declaration-list, the type A of each remaining
16195 argument of the call is compared with the type P of the
16196 declarator-id of the function parameter pack. */
16197 break;
16198
16199 parms = TREE_CHAIN (parms);
16200
16201 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16202 /* For a function parameter pack that does not occur at the
16203 end of the parameter-declaration-list, the type of the
16204 parameter pack is a non-deduced context. */
16205 continue;
16206
16207 arg = args[ia];
16208 ++ia;
16209
16210 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16211 flags, explain_p))
16212 return 1;
16213 }
16214
16215 if (parms
16216 && parms != void_list_node
16217 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16218 {
16219 /* Unify the remaining arguments with the pack expansion type. */
16220 tree argvec;
16221 tree parmvec = make_tree_vec (1);
16222
16223 /* Allocate a TREE_VEC and copy in all of the arguments */
16224 argvec = make_tree_vec (nargs - ia);
16225 for (i = 0; ia < nargs; ++ia, ++i)
16226 TREE_VEC_ELT (argvec, i) = args[ia];
16227
16228 /* Copy the parameter into parmvec. */
16229 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16230 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16231 /*subr=*/subr, explain_p))
16232 return 1;
16233
16234 /* Advance to the end of the list of parameters. */
16235 parms = TREE_CHAIN (parms);
16236 }
16237
16238 /* Fail if we've reached the end of the parm list, and more args
16239 are present, and the parm list isn't variadic. */
16240 if (ia < nargs && parms == void_list_node)
16241 return unify_too_many_arguments (explain_p, nargs, ia);
16242 /* Fail if parms are left and they don't have default values. */
16243 if (parms && parms != void_list_node
16244 && TREE_PURPOSE (parms) == NULL_TREE)
16245 {
16246 unsigned int count = nargs;
16247 tree p = parms;
16248 while (p && p != void_list_node)
16249 {
16250 count++;
16251 p = TREE_CHAIN (p);
16252 }
16253 return unify_too_few_arguments (explain_p, ia, count);
16254 }
16255
16256 if (!subr)
16257 {
16258 tsubst_flags_t complain = (explain_p
16259 ? tf_warning_or_error
16260 : tf_none);
16261
16262 for (i = 0; i < ntparms; i++)
16263 {
16264 tree targ = TREE_VEC_ELT (targs, i);
16265 tree tparm = TREE_VEC_ELT (tparms, i);
16266
16267 /* Clear the "incomplete" flags on all argument packs now so that
16268 substituting them into later default arguments works. */
16269 if (targ && ARGUMENT_PACK_P (targ))
16270 {
16271 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16272 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16273 }
16274
16275 if (targ || tparm == error_mark_node)
16276 continue;
16277 tparm = TREE_VALUE (tparm);
16278
16279 /* If this is an undeduced nontype parameter that depends on
16280 a type parameter, try another pass; its type may have been
16281 deduced from a later argument than the one from which
16282 this parameter can be deduced. */
16283 if (TREE_CODE (tparm) == PARM_DECL
16284 && uses_template_parms (TREE_TYPE (tparm))
16285 && !saw_undeduced++)
16286 goto again;
16287
16288 /* Core issue #226 (C++0x) [temp.deduct]:
16289
16290 If a template argument has not been deduced, its
16291 default template argument, if any, is used.
16292
16293 When we are in C++98 mode, TREE_PURPOSE will either
16294 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16295 to explicitly check cxx_dialect here. */
16296 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16297 {
16298 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16299 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16300 reopen_deferring_access_checks (*checks);
16301 location_t save_loc = input_location;
16302 if (DECL_P (parm))
16303 input_location = DECL_SOURCE_LOCATION (parm);
16304 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16305 arg = convert_template_argument (parm, arg, targs, complain,
16306 i, NULL_TREE);
16307 input_location = save_loc;
16308 *checks = get_deferred_access_checks ();
16309 pop_deferring_access_checks ();
16310 if (arg == error_mark_node)
16311 return 1;
16312 else
16313 {
16314 TREE_VEC_ELT (targs, i) = arg;
16315 /* The position of the first default template argument,
16316 is also the number of non-defaulted arguments in TARGS.
16317 Record that. */
16318 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16319 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16320 continue;
16321 }
16322 }
16323
16324 /* If the type parameter is a parameter pack, then it will
16325 be deduced to an empty parameter pack. */
16326 if (template_parameter_pack_p (tparm))
16327 {
16328 tree arg;
16329
16330 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16331 {
16332 arg = make_node (NONTYPE_ARGUMENT_PACK);
16333 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16334 TREE_CONSTANT (arg) = 1;
16335 }
16336 else
16337 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16338
16339 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16340
16341 TREE_VEC_ELT (targs, i) = arg;
16342 continue;
16343 }
16344
16345 return unify_parameter_deduction_failure (explain_p, tparm);
16346 }
16347 }
16348 #ifdef ENABLE_CHECKING
16349 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16350 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16351 #endif
16352
16353 return unify_success (explain_p);
16354 }
16355
16356 /* Subroutine of type_unification_real. Args are like the variables
16357 at the call site. ARG is an overloaded function (or template-id);
16358 we try deducing template args from each of the overloads, and if
16359 only one succeeds, we go with that. Modifies TARGS and returns
16360 true on success. */
16361
16362 static bool
16363 resolve_overloaded_unification (tree tparms,
16364 tree targs,
16365 tree parm,
16366 tree arg,
16367 unification_kind_t strict,
16368 int sub_strict,
16369 bool explain_p)
16370 {
16371 tree tempargs = copy_node (targs);
16372 int good = 0;
16373 tree goodfn = NULL_TREE;
16374 bool addr_p;
16375
16376 if (TREE_CODE (arg) == ADDR_EXPR)
16377 {
16378 arg = TREE_OPERAND (arg, 0);
16379 addr_p = true;
16380 }
16381 else
16382 addr_p = false;
16383
16384 if (TREE_CODE (arg) == COMPONENT_REF)
16385 /* Handle `&x' where `x' is some static or non-static member
16386 function name. */
16387 arg = TREE_OPERAND (arg, 1);
16388
16389 if (TREE_CODE (arg) == OFFSET_REF)
16390 arg = TREE_OPERAND (arg, 1);
16391
16392 /* Strip baselink information. */
16393 if (BASELINK_P (arg))
16394 arg = BASELINK_FUNCTIONS (arg);
16395
16396 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16397 {
16398 /* If we got some explicit template args, we need to plug them into
16399 the affected templates before we try to unify, in case the
16400 explicit args will completely resolve the templates in question. */
16401
16402 int ok = 0;
16403 tree expl_subargs = TREE_OPERAND (arg, 1);
16404 arg = TREE_OPERAND (arg, 0);
16405
16406 for (; arg; arg = OVL_NEXT (arg))
16407 {
16408 tree fn = OVL_CURRENT (arg);
16409 tree subargs, elem;
16410
16411 if (TREE_CODE (fn) != TEMPLATE_DECL)
16412 continue;
16413
16414 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16415 expl_subargs, NULL_TREE, tf_none,
16416 /*require_all_args=*/true,
16417 /*use_default_args=*/true);
16418 if (subargs != error_mark_node
16419 && !any_dependent_template_arguments_p (subargs))
16420 {
16421 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
16422 if (try_one_overload (tparms, targs, tempargs, parm,
16423 elem, strict, sub_strict, addr_p, explain_p)
16424 && (!goodfn || !same_type_p (goodfn, elem)))
16425 {
16426 goodfn = elem;
16427 ++good;
16428 }
16429 }
16430 else if (subargs)
16431 ++ok;
16432 }
16433 /* If no templates (or more than one) are fully resolved by the
16434 explicit arguments, this template-id is a non-deduced context; it
16435 could still be OK if we deduce all template arguments for the
16436 enclosing call through other arguments. */
16437 if (good != 1)
16438 good = ok;
16439 }
16440 else if (TREE_CODE (arg) != OVERLOAD
16441 && TREE_CODE (arg) != FUNCTION_DECL)
16442 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16443 -- but the deduction does not succeed because the expression is
16444 not just the function on its own. */
16445 return false;
16446 else
16447 for (; arg; arg = OVL_NEXT (arg))
16448 if (try_one_overload (tparms, targs, tempargs, parm,
16449 TREE_TYPE (OVL_CURRENT (arg)),
16450 strict, sub_strict, addr_p, explain_p)
16451 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16452 {
16453 goodfn = OVL_CURRENT (arg);
16454 ++good;
16455 }
16456
16457 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16458 to function or pointer to member function argument if the set of
16459 overloaded functions does not contain function templates and at most
16460 one of a set of overloaded functions provides a unique match.
16461
16462 So if we found multiple possibilities, we return success but don't
16463 deduce anything. */
16464
16465 if (good == 1)
16466 {
16467 int i = TREE_VEC_LENGTH (targs);
16468 for (; i--; )
16469 if (TREE_VEC_ELT (tempargs, i))
16470 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16471 }
16472 if (good)
16473 return true;
16474
16475 return false;
16476 }
16477
16478 /* Core DR 115: In contexts where deduction is done and fails, or in
16479 contexts where deduction is not done, if a template argument list is
16480 specified and it, along with any default template arguments, identifies
16481 a single function template specialization, then the template-id is an
16482 lvalue for the function template specialization. */
16483
16484 tree
16485 resolve_nondeduced_context (tree orig_expr)
16486 {
16487 tree expr, offset, baselink;
16488 bool addr;
16489
16490 if (!type_unknown_p (orig_expr))
16491 return orig_expr;
16492
16493 expr = orig_expr;
16494 addr = false;
16495 offset = NULL_TREE;
16496 baselink = NULL_TREE;
16497
16498 if (TREE_CODE (expr) == ADDR_EXPR)
16499 {
16500 expr = TREE_OPERAND (expr, 0);
16501 addr = true;
16502 }
16503 if (TREE_CODE (expr) == OFFSET_REF)
16504 {
16505 offset = expr;
16506 expr = TREE_OPERAND (expr, 1);
16507 }
16508 if (BASELINK_P (expr))
16509 {
16510 baselink = expr;
16511 expr = BASELINK_FUNCTIONS (expr);
16512 }
16513
16514 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16515 {
16516 int good = 0;
16517 tree goodfn = NULL_TREE;
16518
16519 /* If we got some explicit template args, we need to plug them into
16520 the affected templates before we try to unify, in case the
16521 explicit args will completely resolve the templates in question. */
16522
16523 tree expl_subargs = TREE_OPERAND (expr, 1);
16524 tree arg = TREE_OPERAND (expr, 0);
16525 tree badfn = NULL_TREE;
16526 tree badargs = NULL_TREE;
16527
16528 for (; arg; arg = OVL_NEXT (arg))
16529 {
16530 tree fn = OVL_CURRENT (arg);
16531 tree subargs, elem;
16532
16533 if (TREE_CODE (fn) != TEMPLATE_DECL)
16534 continue;
16535
16536 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16537 expl_subargs, NULL_TREE, tf_none,
16538 /*require_all_args=*/true,
16539 /*use_default_args=*/true);
16540 if (subargs != error_mark_node
16541 && !any_dependent_template_arguments_p (subargs))
16542 {
16543 elem = instantiate_template (fn, subargs, tf_none);
16544 if (elem == error_mark_node)
16545 {
16546 badfn = fn;
16547 badargs = subargs;
16548 }
16549 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16550 {
16551 goodfn = elem;
16552 ++good;
16553 }
16554 }
16555 }
16556 if (good == 1)
16557 {
16558 mark_used (goodfn);
16559 expr = goodfn;
16560 if (baselink)
16561 expr = build_baselink (BASELINK_BINFO (baselink),
16562 BASELINK_ACCESS_BINFO (baselink),
16563 expr, BASELINK_OPTYPE (baselink));
16564 if (offset)
16565 {
16566 tree base
16567 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16568 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16569 }
16570 if (addr)
16571 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16572 return expr;
16573 }
16574 else if (good == 0 && badargs)
16575 /* There were no good options and at least one bad one, so let the
16576 user know what the problem is. */
16577 instantiate_template (badfn, badargs, tf_warning_or_error);
16578 }
16579 return orig_expr;
16580 }
16581
16582 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16583 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16584 different overloads deduce different arguments for a given parm.
16585 ADDR_P is true if the expression for which deduction is being
16586 performed was of the form "& fn" rather than simply "fn".
16587
16588 Returns 1 on success. */
16589
16590 static int
16591 try_one_overload (tree tparms,
16592 tree orig_targs,
16593 tree targs,
16594 tree parm,
16595 tree arg,
16596 unification_kind_t strict,
16597 int sub_strict,
16598 bool addr_p,
16599 bool explain_p)
16600 {
16601 int nargs;
16602 tree tempargs;
16603 int i;
16604
16605 if (arg == error_mark_node)
16606 return 0;
16607
16608 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16609 to function or pointer to member function argument if the set of
16610 overloaded functions does not contain function templates and at most
16611 one of a set of overloaded functions provides a unique match.
16612
16613 So if this is a template, just return success. */
16614
16615 if (uses_template_parms (arg))
16616 return 1;
16617
16618 if (TREE_CODE (arg) == METHOD_TYPE)
16619 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16620 else if (addr_p)
16621 arg = build_pointer_type (arg);
16622
16623 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16624
16625 /* We don't copy orig_targs for this because if we have already deduced
16626 some template args from previous args, unify would complain when we
16627 try to deduce a template parameter for the same argument, even though
16628 there isn't really a conflict. */
16629 nargs = TREE_VEC_LENGTH (targs);
16630 tempargs = make_tree_vec (nargs);
16631
16632 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16633 return 0;
16634
16635 /* First make sure we didn't deduce anything that conflicts with
16636 explicitly specified args. */
16637 for (i = nargs; i--; )
16638 {
16639 tree elt = TREE_VEC_ELT (tempargs, i);
16640 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16641
16642 if (!elt)
16643 /*NOP*/;
16644 else if (uses_template_parms (elt))
16645 /* Since we're unifying against ourselves, we will fill in
16646 template args used in the function parm list with our own
16647 template parms. Discard them. */
16648 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16649 else if (oldelt && !template_args_equal (oldelt, elt))
16650 return 0;
16651 }
16652
16653 for (i = nargs; i--; )
16654 {
16655 tree elt = TREE_VEC_ELT (tempargs, i);
16656
16657 if (elt)
16658 TREE_VEC_ELT (targs, i) = elt;
16659 }
16660
16661 return 1;
16662 }
16663
16664 /* PARM is a template class (perhaps with unbound template
16665 parameters). ARG is a fully instantiated type. If ARG can be
16666 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16667 TARGS are as for unify. */
16668
16669 static tree
16670 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16671 bool explain_p)
16672 {
16673 tree copy_of_targs;
16674
16675 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16676 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16677 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16678 return NULL_TREE;
16679
16680 /* We need to make a new template argument vector for the call to
16681 unify. If we used TARGS, we'd clutter it up with the result of
16682 the attempted unification, even if this class didn't work out.
16683 We also don't want to commit ourselves to all the unifications
16684 we've already done, since unification is supposed to be done on
16685 an argument-by-argument basis. In other words, consider the
16686 following pathological case:
16687
16688 template <int I, int J, int K>
16689 struct S {};
16690
16691 template <int I, int J>
16692 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16693
16694 template <int I, int J, int K>
16695 void f(S<I, J, K>, S<I, I, I>);
16696
16697 void g() {
16698 S<0, 0, 0> s0;
16699 S<0, 1, 2> s2;
16700
16701 f(s0, s2);
16702 }
16703
16704 Now, by the time we consider the unification involving `s2', we
16705 already know that we must have `f<0, 0, 0>'. But, even though
16706 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16707 because there are two ways to unify base classes of S<0, 1, 2>
16708 with S<I, I, I>. If we kept the already deduced knowledge, we
16709 would reject the possibility I=1. */
16710 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16711
16712 /* If unification failed, we're done. */
16713 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16714 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16715 return NULL_TREE;
16716
16717 return arg;
16718 }
16719
16720 /* Given a template type PARM and a class type ARG, find the unique
16721 base type in ARG that is an instance of PARM. We do not examine
16722 ARG itself; only its base-classes. If there is not exactly one
16723 appropriate base class, return NULL_TREE. PARM may be the type of
16724 a partial specialization, as well as a plain template type. Used
16725 by unify. */
16726
16727 static enum template_base_result
16728 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16729 bool explain_p, tree *result)
16730 {
16731 tree rval = NULL_TREE;
16732 tree binfo;
16733
16734 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16735
16736 binfo = TYPE_BINFO (complete_type (arg));
16737 if (!binfo)
16738 {
16739 /* The type could not be completed. */
16740 *result = NULL_TREE;
16741 return tbr_incomplete_type;
16742 }
16743
16744 /* Walk in inheritance graph order. The search order is not
16745 important, and this avoids multiple walks of virtual bases. */
16746 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16747 {
16748 tree r = try_class_unification (tparms, targs, parm,
16749 BINFO_TYPE (binfo), explain_p);
16750
16751 if (r)
16752 {
16753 /* If there is more than one satisfactory baseclass, then:
16754
16755 [temp.deduct.call]
16756
16757 If they yield more than one possible deduced A, the type
16758 deduction fails.
16759
16760 applies. */
16761 if (rval && !same_type_p (r, rval))
16762 {
16763 *result = NULL_TREE;
16764 return tbr_ambiguous_baseclass;
16765 }
16766
16767 rval = r;
16768 }
16769 }
16770
16771 *result = rval;
16772 return tbr_success;
16773 }
16774
16775 /* Returns the level of DECL, which declares a template parameter. */
16776
16777 static int
16778 template_decl_level (tree decl)
16779 {
16780 switch (TREE_CODE (decl))
16781 {
16782 case TYPE_DECL:
16783 case TEMPLATE_DECL:
16784 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16785
16786 case PARM_DECL:
16787 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16788
16789 default:
16790 gcc_unreachable ();
16791 }
16792 return 0;
16793 }
16794
16795 /* Decide whether ARG can be unified with PARM, considering only the
16796 cv-qualifiers of each type, given STRICT as documented for unify.
16797 Returns nonzero iff the unification is OK on that basis. */
16798
16799 static int
16800 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16801 {
16802 int arg_quals = cp_type_quals (arg);
16803 int parm_quals = cp_type_quals (parm);
16804
16805 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16806 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16807 {
16808 /* Although a CVR qualifier is ignored when being applied to a
16809 substituted template parameter ([8.3.2]/1 for example), that
16810 does not allow us to unify "const T" with "int&" because both
16811 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16812 It is ok when we're allowing additional CV qualifiers
16813 at the outer level [14.8.2.1]/3,1st bullet. */
16814 if ((TREE_CODE (arg) == REFERENCE_TYPE
16815 || TREE_CODE (arg) == FUNCTION_TYPE
16816 || TREE_CODE (arg) == METHOD_TYPE)
16817 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16818 return 0;
16819
16820 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16821 && (parm_quals & TYPE_QUAL_RESTRICT))
16822 return 0;
16823 }
16824
16825 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16826 && (arg_quals & parm_quals) != parm_quals)
16827 return 0;
16828
16829 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16830 && (parm_quals & arg_quals) != arg_quals)
16831 return 0;
16832
16833 return 1;
16834 }
16835
16836 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16837 void
16838 template_parm_level_and_index (tree parm, int* level, int* index)
16839 {
16840 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16841 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16842 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16843 {
16844 *index = TEMPLATE_TYPE_IDX (parm);
16845 *level = TEMPLATE_TYPE_LEVEL (parm);
16846 }
16847 else
16848 {
16849 *index = TEMPLATE_PARM_IDX (parm);
16850 *level = TEMPLATE_PARM_LEVEL (parm);
16851 }
16852 }
16853
16854 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16855 do { \
16856 if (unify (TP, TA, P, A, S, EP)) \
16857 return 1; \
16858 } while (0);
16859
16860 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16861 expansion at the end of PACKED_PARMS. Returns 0 if the type
16862 deduction succeeds, 1 otherwise. STRICT is the same as in
16863 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16864 call argument list. We'll need to adjust the arguments to make them
16865 types. SUBR tells us if this is from a recursive call to
16866 type_unification_real, or for comparing two template argument
16867 lists. */
16868
16869 static int
16870 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16871 tree packed_args, unification_kind_t strict,
16872 bool subr, bool explain_p)
16873 {
16874 tree parm
16875 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16876 tree pattern = PACK_EXPANSION_PATTERN (parm);
16877 tree pack, packs = NULL_TREE;
16878 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16879 int len = TREE_VEC_LENGTH (packed_args);
16880
16881 /* Determine the parameter packs we will be deducing from the
16882 pattern, and record their current deductions. */
16883 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16884 pack; pack = TREE_CHAIN (pack))
16885 {
16886 tree parm_pack = TREE_VALUE (pack);
16887 int idx, level;
16888
16889 /* Determine the index and level of this parameter pack. */
16890 template_parm_level_and_index (parm_pack, &level, &idx);
16891
16892 /* Keep track of the parameter packs and their corresponding
16893 argument packs. */
16894 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16895 TREE_TYPE (packs) = make_tree_vec (len - start);
16896 }
16897
16898 /* Loop through all of the arguments that have not yet been
16899 unified and unify each with the pattern. */
16900 for (i = start; i < len; i++)
16901 {
16902 tree parm;
16903 bool any_explicit = false;
16904 tree arg = TREE_VEC_ELT (packed_args, i);
16905
16906 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16907 or the element of its argument pack at the current index if
16908 this argument was explicitly specified. */
16909 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16910 {
16911 int idx, level;
16912 tree arg, pargs;
16913 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16914
16915 arg = NULL_TREE;
16916 if (TREE_VALUE (pack)
16917 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16918 && (i - start < TREE_VEC_LENGTH (pargs)))
16919 {
16920 any_explicit = true;
16921 arg = TREE_VEC_ELT (pargs, i - start);
16922 }
16923 TMPL_ARG (targs, level, idx) = arg;
16924 }
16925
16926 /* If we had explicit template arguments, substitute them into the
16927 pattern before deduction. */
16928 if (any_explicit)
16929 {
16930 /* Some arguments might still be unspecified or dependent. */
16931 bool dependent;
16932 ++processing_template_decl;
16933 dependent = any_dependent_template_arguments_p (targs);
16934 if (!dependent)
16935 --processing_template_decl;
16936 parm = tsubst (pattern, targs,
16937 explain_p ? tf_warning_or_error : tf_none,
16938 NULL_TREE);
16939 if (dependent)
16940 --processing_template_decl;
16941 if (parm == error_mark_node)
16942 return 1;
16943 }
16944 else
16945 parm = pattern;
16946
16947 /* Unify the pattern with the current argument. */
16948 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16949 LOOKUP_IMPLICIT, explain_p))
16950 return 1;
16951
16952 /* For each parameter pack, collect the deduced value. */
16953 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16954 {
16955 int idx, level;
16956 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16957
16958 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16959 TMPL_ARG (targs, level, idx);
16960 }
16961 }
16962
16963 /* Verify that the results of unification with the parameter packs
16964 produce results consistent with what we've seen before, and make
16965 the deduced argument packs available. */
16966 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16967 {
16968 tree old_pack = TREE_VALUE (pack);
16969 tree new_args = TREE_TYPE (pack);
16970 int i, len = TREE_VEC_LENGTH (new_args);
16971 int idx, level;
16972 bool nondeduced_p = false;
16973
16974 /* By default keep the original deduced argument pack.
16975 If necessary, more specific code is going to update the
16976 resulting deduced argument later down in this function. */
16977 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16978 TMPL_ARG (targs, level, idx) = old_pack;
16979
16980 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16981 actually deduce anything. */
16982 for (i = 0; i < len && !nondeduced_p; ++i)
16983 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16984 nondeduced_p = true;
16985 if (nondeduced_p)
16986 continue;
16987
16988 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16989 {
16990 /* If we had fewer function args than explicit template args,
16991 just use the explicits. */
16992 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16993 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16994 if (len < explicit_len)
16995 new_args = explicit_args;
16996 }
16997
16998 if (!old_pack)
16999 {
17000 tree result;
17001 /* Build the deduced *_ARGUMENT_PACK. */
17002 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17003 {
17004 result = make_node (NONTYPE_ARGUMENT_PACK);
17005 TREE_TYPE (result) =
17006 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17007 TREE_CONSTANT (result) = 1;
17008 }
17009 else
17010 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17011
17012 SET_ARGUMENT_PACK_ARGS (result, new_args);
17013
17014 /* Note the deduced argument packs for this parameter
17015 pack. */
17016 TMPL_ARG (targs, level, idx) = result;
17017 }
17018 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17019 && (ARGUMENT_PACK_ARGS (old_pack)
17020 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17021 {
17022 /* We only had the explicitly-provided arguments before, but
17023 now we have a complete set of arguments. */
17024 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17025
17026 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17027 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17028 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17029 }
17030 else
17031 {
17032 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17033 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17034
17035 if (!comp_template_args_with_info (old_args, new_args,
17036 &bad_old_arg, &bad_new_arg))
17037 /* Inconsistent unification of this parameter pack. */
17038 return unify_parameter_pack_inconsistent (explain_p,
17039 bad_old_arg,
17040 bad_new_arg);
17041 }
17042 }
17043
17044 return unify_success (explain_p);
17045 }
17046
17047 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17048 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17049 parameters and return value are as for unify. */
17050
17051 static int
17052 unify_array_domain (tree tparms, tree targs,
17053 tree parm_dom, tree arg_dom,
17054 bool explain_p)
17055 {
17056 tree parm_max;
17057 tree arg_max;
17058 bool parm_cst;
17059 bool arg_cst;
17060
17061 /* Our representation of array types uses "N - 1" as the
17062 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17063 not an integer constant. We cannot unify arbitrarily
17064 complex expressions, so we eliminate the MINUS_EXPRs
17065 here. */
17066 parm_max = TYPE_MAX_VALUE (parm_dom);
17067 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17068 if (!parm_cst)
17069 {
17070 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17071 parm_max = TREE_OPERAND (parm_max, 0);
17072 }
17073 arg_max = TYPE_MAX_VALUE (arg_dom);
17074 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17075 if (!arg_cst)
17076 {
17077 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17078 trying to unify the type of a variable with the type
17079 of a template parameter. For example:
17080
17081 template <unsigned int N>
17082 void f (char (&) [N]);
17083 int g();
17084 void h(int i) {
17085 char a[g(i)];
17086 f(a);
17087 }
17088
17089 Here, the type of the ARG will be "int [g(i)]", and
17090 may be a SAVE_EXPR, etc. */
17091 if (TREE_CODE (arg_max) != MINUS_EXPR)
17092 return unify_vla_arg (explain_p, arg_dom);
17093 arg_max = TREE_OPERAND (arg_max, 0);
17094 }
17095
17096 /* If only one of the bounds used a MINUS_EXPR, compensate
17097 by adding one to the other bound. */
17098 if (parm_cst && !arg_cst)
17099 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17100 integer_type_node,
17101 parm_max,
17102 integer_one_node);
17103 else if (arg_cst && !parm_cst)
17104 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17105 integer_type_node,
17106 arg_max,
17107 integer_one_node);
17108
17109 return unify (tparms, targs, parm_max, arg_max,
17110 UNIFY_ALLOW_INTEGER, explain_p);
17111 }
17112
17113 /* Deduce the value of template parameters. TPARMS is the (innermost)
17114 set of template parameters to a template. TARGS is the bindings
17115 for those template parameters, as determined thus far; TARGS may
17116 include template arguments for outer levels of template parameters
17117 as well. PARM is a parameter to a template function, or a
17118 subcomponent of that parameter; ARG is the corresponding argument.
17119 This function attempts to match PARM with ARG in a manner
17120 consistent with the existing assignments in TARGS. If more values
17121 are deduced, then TARGS is updated.
17122
17123 Returns 0 if the type deduction succeeds, 1 otherwise. The
17124 parameter STRICT is a bitwise or of the following flags:
17125
17126 UNIFY_ALLOW_NONE:
17127 Require an exact match between PARM and ARG.
17128 UNIFY_ALLOW_MORE_CV_QUAL:
17129 Allow the deduced ARG to be more cv-qualified (by qualification
17130 conversion) than ARG.
17131 UNIFY_ALLOW_LESS_CV_QUAL:
17132 Allow the deduced ARG to be less cv-qualified than ARG.
17133 UNIFY_ALLOW_DERIVED:
17134 Allow the deduced ARG to be a template base class of ARG,
17135 or a pointer to a template base class of the type pointed to by
17136 ARG.
17137 UNIFY_ALLOW_INTEGER:
17138 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17139 case for more information.
17140 UNIFY_ALLOW_OUTER_LEVEL:
17141 This is the outermost level of a deduction. Used to determine validity
17142 of qualification conversions. A valid qualification conversion must
17143 have const qualified pointers leading up to the inner type which
17144 requires additional CV quals, except at the outer level, where const
17145 is not required [conv.qual]. It would be normal to set this flag in
17146 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17147 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17148 This is the outermost level of a deduction, and PARM can be more CV
17149 qualified at this point.
17150 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17151 This is the outermost level of a deduction, and PARM can be less CV
17152 qualified at this point. */
17153
17154 static int
17155 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17156 bool explain_p)
17157 {
17158 int idx;
17159 tree targ;
17160 tree tparm;
17161 int strict_in = strict;
17162
17163 /* I don't think this will do the right thing with respect to types.
17164 But the only case I've seen it in so far has been array bounds, where
17165 signedness is the only information lost, and I think that will be
17166 okay. */
17167 while (TREE_CODE (parm) == NOP_EXPR)
17168 parm = TREE_OPERAND (parm, 0);
17169
17170 if (arg == error_mark_node)
17171 return unify_invalid (explain_p);
17172 if (arg == unknown_type_node
17173 || arg == init_list_type_node)
17174 /* We can't deduce anything from this, but we might get all the
17175 template args from other function args. */
17176 return unify_success (explain_p);
17177
17178 /* If PARM uses template parameters, then we can't bail out here,
17179 even if ARG == PARM, since we won't record unifications for the
17180 template parameters. We might need them if we're trying to
17181 figure out which of two things is more specialized. */
17182 if (arg == parm && !uses_template_parms (parm))
17183 return unify_success (explain_p);
17184
17185 /* Handle init lists early, so the rest of the function can assume
17186 we're dealing with a type. */
17187 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17188 {
17189 tree elt, elttype;
17190 unsigned i;
17191 tree orig_parm = parm;
17192
17193 /* Replace T with std::initializer_list<T> for deduction. */
17194 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17195 && flag_deduce_init_list)
17196 parm = listify (parm);
17197
17198 if (!is_std_init_list (parm)
17199 && TREE_CODE (parm) != ARRAY_TYPE)
17200 /* We can only deduce from an initializer list argument if the
17201 parameter is std::initializer_list or an array; otherwise this
17202 is a non-deduced context. */
17203 return unify_success (explain_p);
17204
17205 if (TREE_CODE (parm) == ARRAY_TYPE)
17206 elttype = TREE_TYPE (parm);
17207 else
17208 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17209
17210 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17211 {
17212 int elt_strict = strict;
17213
17214 if (elt == error_mark_node)
17215 return unify_invalid (explain_p);
17216
17217 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17218 {
17219 tree type = TREE_TYPE (elt);
17220 /* It should only be possible to get here for a call. */
17221 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17222 elt_strict |= maybe_adjust_types_for_deduction
17223 (DEDUCE_CALL, &elttype, &type, elt);
17224 elt = type;
17225 }
17226
17227 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17228 explain_p);
17229 }
17230
17231 if (TREE_CODE (parm) == ARRAY_TYPE)
17232 {
17233 /* Also deduce from the length of the initializer list. */
17234 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17235 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17236 if (TYPE_DOMAIN (parm) != NULL_TREE)
17237 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17238 idx, explain_p);
17239 }
17240
17241 /* If the std::initializer_list<T> deduction worked, replace the
17242 deduced A with std::initializer_list<A>. */
17243 if (orig_parm != parm)
17244 {
17245 idx = TEMPLATE_TYPE_IDX (orig_parm);
17246 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17247 targ = listify (targ);
17248 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17249 }
17250 return unify_success (explain_p);
17251 }
17252
17253 /* Immediately reject some pairs that won't unify because of
17254 cv-qualification mismatches. */
17255 if (TREE_CODE (arg) == TREE_CODE (parm)
17256 && TYPE_P (arg)
17257 /* It is the elements of the array which hold the cv quals of an array
17258 type, and the elements might be template type parms. We'll check
17259 when we recurse. */
17260 && TREE_CODE (arg) != ARRAY_TYPE
17261 /* We check the cv-qualifiers when unifying with template type
17262 parameters below. We want to allow ARG `const T' to unify with
17263 PARM `T' for example, when computing which of two templates
17264 is more specialized, for example. */
17265 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17266 && !check_cv_quals_for_unify (strict_in, arg, parm))
17267 return unify_cv_qual_mismatch (explain_p, parm, arg);
17268
17269 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17270 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17271 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17272 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17273 strict &= ~UNIFY_ALLOW_DERIVED;
17274 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17275 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17276
17277 switch (TREE_CODE (parm))
17278 {
17279 case TYPENAME_TYPE:
17280 case SCOPE_REF:
17281 case UNBOUND_CLASS_TEMPLATE:
17282 /* In a type which contains a nested-name-specifier, template
17283 argument values cannot be deduced for template parameters used
17284 within the nested-name-specifier. */
17285 return unify_success (explain_p);
17286
17287 case TEMPLATE_TYPE_PARM:
17288 case TEMPLATE_TEMPLATE_PARM:
17289 case BOUND_TEMPLATE_TEMPLATE_PARM:
17290 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17291 if (tparm == error_mark_node)
17292 return unify_invalid (explain_p);
17293
17294 if (TEMPLATE_TYPE_LEVEL (parm)
17295 != template_decl_level (tparm))
17296 /* The PARM is not one we're trying to unify. Just check
17297 to see if it matches ARG. */
17298 {
17299 if (TREE_CODE (arg) == TREE_CODE (parm)
17300 && (is_auto (parm) ? is_auto (arg)
17301 : same_type_p (parm, arg)))
17302 return unify_success (explain_p);
17303 else
17304 return unify_type_mismatch (explain_p, parm, arg);
17305 }
17306 idx = TEMPLATE_TYPE_IDX (parm);
17307 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17308 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17309 if (tparm == error_mark_node)
17310 return unify_invalid (explain_p);
17311
17312 /* Check for mixed types and values. */
17313 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17314 && TREE_CODE (tparm) != TYPE_DECL)
17315 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17316 && TREE_CODE (tparm) != TEMPLATE_DECL))
17317 gcc_unreachable ();
17318
17319 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17320 {
17321 /* ARG must be constructed from a template class or a template
17322 template parameter. */
17323 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17324 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17325 return unify_template_deduction_failure (explain_p, parm, arg);
17326
17327 {
17328 tree parmvec = TYPE_TI_ARGS (parm);
17329 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17330 tree full_argvec = add_to_template_args (targs, argvec);
17331 tree parm_parms
17332 = DECL_INNERMOST_TEMPLATE_PARMS
17333 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17334 int i, len;
17335 int parm_variadic_p = 0;
17336
17337 /* The resolution to DR150 makes clear that default
17338 arguments for an N-argument may not be used to bind T
17339 to a template template parameter with fewer than N
17340 parameters. It is not safe to permit the binding of
17341 default arguments as an extension, as that may change
17342 the meaning of a conforming program. Consider:
17343
17344 struct Dense { static const unsigned int dim = 1; };
17345
17346 template <template <typename> class View,
17347 typename Block>
17348 void operator+(float, View<Block> const&);
17349
17350 template <typename Block,
17351 unsigned int Dim = Block::dim>
17352 struct Lvalue_proxy { operator float() const; };
17353
17354 void
17355 test_1d (void) {
17356 Lvalue_proxy<Dense> p;
17357 float b;
17358 b + p;
17359 }
17360
17361 Here, if Lvalue_proxy is permitted to bind to View, then
17362 the global operator+ will be used; if they are not, the
17363 Lvalue_proxy will be converted to float. */
17364 if (coerce_template_parms (parm_parms,
17365 full_argvec,
17366 TYPE_TI_TEMPLATE (parm),
17367 (explain_p
17368 ? tf_warning_or_error
17369 : tf_none),
17370 /*require_all_args=*/true,
17371 /*use_default_args=*/false)
17372 == error_mark_node)
17373 return 1;
17374
17375 /* Deduce arguments T, i from TT<T> or TT<i>.
17376 We check each element of PARMVEC and ARGVEC individually
17377 rather than the whole TREE_VEC since they can have
17378 different number of elements. */
17379
17380 parmvec = expand_template_argument_pack (parmvec);
17381 argvec = expand_template_argument_pack (argvec);
17382
17383 len = TREE_VEC_LENGTH (parmvec);
17384
17385 /* Check if the parameters end in a pack, making them
17386 variadic. */
17387 if (len > 0
17388 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17389 parm_variadic_p = 1;
17390
17391 for (i = 0; i < len - parm_variadic_p; ++i)
17392 /* If the template argument list of P contains a pack
17393 expansion that is not the last template argument, the
17394 entire template argument list is a non-deduced
17395 context. */
17396 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17397 return unify_success (explain_p);
17398
17399 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17400 return unify_too_few_arguments (explain_p,
17401 TREE_VEC_LENGTH (argvec), len);
17402
17403 for (i = 0; i < len - parm_variadic_p; ++i)
17404 {
17405 RECUR_AND_CHECK_FAILURE (tparms, targs,
17406 TREE_VEC_ELT (parmvec, i),
17407 TREE_VEC_ELT (argvec, i),
17408 UNIFY_ALLOW_NONE, explain_p);
17409 }
17410
17411 if (parm_variadic_p
17412 && unify_pack_expansion (tparms, targs,
17413 parmvec, argvec,
17414 DEDUCE_EXACT,
17415 /*subr=*/true, explain_p))
17416 return 1;
17417 }
17418 arg = TYPE_TI_TEMPLATE (arg);
17419
17420 /* Fall through to deduce template name. */
17421 }
17422
17423 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17424 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17425 {
17426 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17427
17428 /* Simple cases: Value already set, does match or doesn't. */
17429 if (targ != NULL_TREE && template_args_equal (targ, arg))
17430 return unify_success (explain_p);
17431 else if (targ)
17432 return unify_inconsistency (explain_p, parm, targ, arg);
17433 }
17434 else
17435 {
17436 /* If PARM is `const T' and ARG is only `int', we don't have
17437 a match unless we are allowing additional qualification.
17438 If ARG is `const int' and PARM is just `T' that's OK;
17439 that binds `const int' to `T'. */
17440 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17441 arg, parm))
17442 return unify_cv_qual_mismatch (explain_p, parm, arg);
17443
17444 /* Consider the case where ARG is `const volatile int' and
17445 PARM is `const T'. Then, T should be `volatile int'. */
17446 arg = cp_build_qualified_type_real
17447 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17448 if (arg == error_mark_node)
17449 return unify_invalid (explain_p);
17450
17451 /* Simple cases: Value already set, does match or doesn't. */
17452 if (targ != NULL_TREE && same_type_p (targ, arg))
17453 return unify_success (explain_p);
17454 else if (targ)
17455 return unify_inconsistency (explain_p, parm, targ, arg);
17456
17457 /* Make sure that ARG is not a variable-sized array. (Note
17458 that were talking about variable-sized arrays (like
17459 `int[n]'), rather than arrays of unknown size (like
17460 `int[]').) We'll get very confused by such a type since
17461 the bound of the array is not constant, and therefore
17462 not mangleable. Besides, such types are not allowed in
17463 ISO C++, so we can do as we please here. We do allow
17464 them for 'auto' deduction, since that isn't ABI-exposed. */
17465 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17466 return unify_vla_arg (explain_p, arg);
17467
17468 /* Strip typedefs as in convert_template_argument. */
17469 arg = canonicalize_type_argument (arg, tf_none);
17470 }
17471
17472 /* If ARG is a parameter pack or an expansion, we cannot unify
17473 against it unless PARM is also a parameter pack. */
17474 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17475 && !template_parameter_pack_p (parm))
17476 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17477
17478 /* If the argument deduction results is a METHOD_TYPE,
17479 then there is a problem.
17480 METHOD_TYPE doesn't map to any real C++ type the result of
17481 the deduction can not be of that type. */
17482 if (TREE_CODE (arg) == METHOD_TYPE)
17483 return unify_method_type_error (explain_p, arg);
17484
17485 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17486 return unify_success (explain_p);
17487
17488 case TEMPLATE_PARM_INDEX:
17489 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17490 if (tparm == error_mark_node)
17491 return unify_invalid (explain_p);
17492
17493 if (TEMPLATE_PARM_LEVEL (parm)
17494 != template_decl_level (tparm))
17495 {
17496 /* The PARM is not one we're trying to unify. Just check
17497 to see if it matches ARG. */
17498 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17499 && cp_tree_equal (parm, arg));
17500 if (result)
17501 unify_expression_unequal (explain_p, parm, arg);
17502 return result;
17503 }
17504
17505 idx = TEMPLATE_PARM_IDX (parm);
17506 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17507
17508 if (targ)
17509 {
17510 int x = !cp_tree_equal (targ, arg);
17511 if (x)
17512 unify_inconsistency (explain_p, parm, targ, arg);
17513 return x;
17514 }
17515
17516 /* [temp.deduct.type] If, in the declaration of a function template
17517 with a non-type template-parameter, the non-type
17518 template-parameter is used in an expression in the function
17519 parameter-list and, if the corresponding template-argument is
17520 deduced, the template-argument type shall match the type of the
17521 template-parameter exactly, except that a template-argument
17522 deduced from an array bound may be of any integral type.
17523 The non-type parameter might use already deduced type parameters. */
17524 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17525 if (!TREE_TYPE (arg))
17526 /* Template-parameter dependent expression. Just accept it for now.
17527 It will later be processed in convert_template_argument. */
17528 ;
17529 else if (same_type_p (TREE_TYPE (arg), tparm))
17530 /* OK */;
17531 else if ((strict & UNIFY_ALLOW_INTEGER)
17532 && CP_INTEGRAL_TYPE_P (tparm))
17533 /* Convert the ARG to the type of PARM; the deduced non-type
17534 template argument must exactly match the types of the
17535 corresponding parameter. */
17536 arg = fold (build_nop (tparm, arg));
17537 else if (uses_template_parms (tparm))
17538 /* We haven't deduced the type of this parameter yet. Try again
17539 later. */
17540 return unify_success (explain_p);
17541 else
17542 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17543
17544 /* If ARG is a parameter pack or an expansion, we cannot unify
17545 against it unless PARM is also a parameter pack. */
17546 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17547 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17548 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17549
17550 arg = strip_typedefs_expr (arg);
17551 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17552 return unify_success (explain_p);
17553
17554 case PTRMEM_CST:
17555 {
17556 /* A pointer-to-member constant can be unified only with
17557 another constant. */
17558 if (TREE_CODE (arg) != PTRMEM_CST)
17559 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17560
17561 /* Just unify the class member. It would be useless (and possibly
17562 wrong, depending on the strict flags) to unify also
17563 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17564 arg refer to the same variable, even if through different
17565 classes. For instance:
17566
17567 struct A { int x; };
17568 struct B : A { };
17569
17570 Unification of &A::x and &B::x must succeed. */
17571 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17572 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17573 }
17574
17575 case POINTER_TYPE:
17576 {
17577 if (!TYPE_PTR_P (arg))
17578 return unify_type_mismatch (explain_p, parm, arg);
17579
17580 /* [temp.deduct.call]
17581
17582 A can be another pointer or pointer to member type that can
17583 be converted to the deduced A via a qualification
17584 conversion (_conv.qual_).
17585
17586 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17587 This will allow for additional cv-qualification of the
17588 pointed-to types if appropriate. */
17589
17590 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17591 /* The derived-to-base conversion only persists through one
17592 level of pointers. */
17593 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17594
17595 return unify (tparms, targs, TREE_TYPE (parm),
17596 TREE_TYPE (arg), strict, explain_p);
17597 }
17598
17599 case REFERENCE_TYPE:
17600 if (TREE_CODE (arg) != REFERENCE_TYPE)
17601 return unify_type_mismatch (explain_p, parm, arg);
17602 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17603 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17604
17605 case ARRAY_TYPE:
17606 if (TREE_CODE (arg) != ARRAY_TYPE)
17607 return unify_type_mismatch (explain_p, parm, arg);
17608 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17609 != (TYPE_DOMAIN (arg) == NULL_TREE))
17610 return unify_type_mismatch (explain_p, parm, arg);
17611 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17612 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17613 if (TYPE_DOMAIN (parm) != NULL_TREE)
17614 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17615 TYPE_DOMAIN (arg), explain_p);
17616 return unify_success (explain_p);
17617
17618 case REAL_TYPE:
17619 case COMPLEX_TYPE:
17620 case VECTOR_TYPE:
17621 case INTEGER_TYPE:
17622 case BOOLEAN_TYPE:
17623 case ENUMERAL_TYPE:
17624 case VOID_TYPE:
17625 case NULLPTR_TYPE:
17626 if (TREE_CODE (arg) != TREE_CODE (parm))
17627 return unify_type_mismatch (explain_p, parm, arg);
17628
17629 /* We have already checked cv-qualification at the top of the
17630 function. */
17631 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17632 return unify_type_mismatch (explain_p, parm, arg);
17633
17634 /* As far as unification is concerned, this wins. Later checks
17635 will invalidate it if necessary. */
17636 return unify_success (explain_p);
17637
17638 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17639 /* Type INTEGER_CST can come from ordinary constant template args. */
17640 case INTEGER_CST:
17641 while (TREE_CODE (arg) == NOP_EXPR)
17642 arg = TREE_OPERAND (arg, 0);
17643
17644 if (TREE_CODE (arg) != INTEGER_CST)
17645 return unify_template_argument_mismatch (explain_p, parm, arg);
17646 return (tree_int_cst_equal (parm, arg)
17647 ? unify_success (explain_p)
17648 : unify_template_argument_mismatch (explain_p, parm, arg));
17649
17650 case TREE_VEC:
17651 {
17652 int i, len, argslen;
17653 int parm_variadic_p = 0;
17654
17655 if (TREE_CODE (arg) != TREE_VEC)
17656 return unify_template_argument_mismatch (explain_p, parm, arg);
17657
17658 len = TREE_VEC_LENGTH (parm);
17659 argslen = TREE_VEC_LENGTH (arg);
17660
17661 /* Check for pack expansions in the parameters. */
17662 for (i = 0; i < len; ++i)
17663 {
17664 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17665 {
17666 if (i == len - 1)
17667 /* We can unify against something with a trailing
17668 parameter pack. */
17669 parm_variadic_p = 1;
17670 else
17671 /* [temp.deduct.type]/9: If the template argument list of
17672 P contains a pack expansion that is not the last
17673 template argument, the entire template argument list
17674 is a non-deduced context. */
17675 return unify_success (explain_p);
17676 }
17677 }
17678
17679 /* If we don't have enough arguments to satisfy the parameters
17680 (not counting the pack expression at the end), or we have
17681 too many arguments for a parameter list that doesn't end in
17682 a pack expression, we can't unify. */
17683 if (parm_variadic_p
17684 ? argslen < len - parm_variadic_p
17685 : argslen != len)
17686 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17687
17688 /* Unify all of the parameters that precede the (optional)
17689 pack expression. */
17690 for (i = 0; i < len - parm_variadic_p; ++i)
17691 {
17692 RECUR_AND_CHECK_FAILURE (tparms, targs,
17693 TREE_VEC_ELT (parm, i),
17694 TREE_VEC_ELT (arg, i),
17695 UNIFY_ALLOW_NONE, explain_p);
17696 }
17697 if (parm_variadic_p)
17698 return unify_pack_expansion (tparms, targs, parm, arg,
17699 DEDUCE_EXACT,
17700 /*subr=*/true, explain_p);
17701 return unify_success (explain_p);
17702 }
17703
17704 case RECORD_TYPE:
17705 case UNION_TYPE:
17706 if (TREE_CODE (arg) != TREE_CODE (parm))
17707 return unify_type_mismatch (explain_p, parm, arg);
17708
17709 if (TYPE_PTRMEMFUNC_P (parm))
17710 {
17711 if (!TYPE_PTRMEMFUNC_P (arg))
17712 return unify_type_mismatch (explain_p, parm, arg);
17713
17714 return unify (tparms, targs,
17715 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17716 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17717 strict, explain_p);
17718 }
17719
17720 if (CLASSTYPE_TEMPLATE_INFO (parm))
17721 {
17722 tree t = NULL_TREE;
17723
17724 if (strict_in & UNIFY_ALLOW_DERIVED)
17725 {
17726 /* First, we try to unify the PARM and ARG directly. */
17727 t = try_class_unification (tparms, targs,
17728 parm, arg, explain_p);
17729
17730 if (!t)
17731 {
17732 /* Fallback to the special case allowed in
17733 [temp.deduct.call]:
17734
17735 If P is a class, and P has the form
17736 template-id, then A can be a derived class of
17737 the deduced A. Likewise, if P is a pointer to
17738 a class of the form template-id, A can be a
17739 pointer to a derived class pointed to by the
17740 deduced A. */
17741 enum template_base_result r;
17742 r = get_template_base (tparms, targs, parm, arg,
17743 explain_p, &t);
17744
17745 if (!t)
17746 return unify_no_common_base (explain_p, r, parm, arg);
17747 }
17748 }
17749 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17750 && (CLASSTYPE_TI_TEMPLATE (parm)
17751 == CLASSTYPE_TI_TEMPLATE (arg)))
17752 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17753 Then, we should unify `int' and `U'. */
17754 t = arg;
17755 else
17756 /* There's no chance of unification succeeding. */
17757 return unify_type_mismatch (explain_p, parm, arg);
17758
17759 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17760 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17761 }
17762 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17763 return unify_type_mismatch (explain_p, parm, arg);
17764 return unify_success (explain_p);
17765
17766 case METHOD_TYPE:
17767 case FUNCTION_TYPE:
17768 {
17769 unsigned int nargs;
17770 tree *args;
17771 tree a;
17772 unsigned int i;
17773
17774 if (TREE_CODE (arg) != TREE_CODE (parm))
17775 return unify_type_mismatch (explain_p, parm, arg);
17776
17777 /* CV qualifications for methods can never be deduced, they must
17778 match exactly. We need to check them explicitly here,
17779 because type_unification_real treats them as any other
17780 cv-qualified parameter. */
17781 if (TREE_CODE (parm) == METHOD_TYPE
17782 && (!check_cv_quals_for_unify
17783 (UNIFY_ALLOW_NONE,
17784 class_of_this_parm (arg),
17785 class_of_this_parm (parm))))
17786 return unify_cv_qual_mismatch (explain_p, parm, arg);
17787
17788 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17789 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17790
17791 nargs = list_length (TYPE_ARG_TYPES (arg));
17792 args = XALLOCAVEC (tree, nargs);
17793 for (a = TYPE_ARG_TYPES (arg), i = 0;
17794 a != NULL_TREE && a != void_list_node;
17795 a = TREE_CHAIN (a), ++i)
17796 args[i] = TREE_VALUE (a);
17797 nargs = i;
17798
17799 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17800 args, nargs, 1, DEDUCE_EXACT,
17801 LOOKUP_NORMAL, NULL, explain_p);
17802 }
17803
17804 case OFFSET_TYPE:
17805 /* Unify a pointer to member with a pointer to member function, which
17806 deduces the type of the member as a function type. */
17807 if (TYPE_PTRMEMFUNC_P (arg))
17808 {
17809 /* Check top-level cv qualifiers */
17810 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17811 return unify_cv_qual_mismatch (explain_p, parm, arg);
17812
17813 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17814 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17815 UNIFY_ALLOW_NONE, explain_p);
17816
17817 /* Determine the type of the function we are unifying against. */
17818 tree fntype = static_fn_type (arg);
17819
17820 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17821 }
17822
17823 if (TREE_CODE (arg) != OFFSET_TYPE)
17824 return unify_type_mismatch (explain_p, parm, arg);
17825 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17826 TYPE_OFFSET_BASETYPE (arg),
17827 UNIFY_ALLOW_NONE, explain_p);
17828 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17829 strict, explain_p);
17830
17831 case CONST_DECL:
17832 if (DECL_TEMPLATE_PARM_P (parm))
17833 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17834 if (arg != integral_constant_value (parm))
17835 return unify_template_argument_mismatch (explain_p, parm, arg);
17836 return unify_success (explain_p);
17837
17838 case FIELD_DECL:
17839 case TEMPLATE_DECL:
17840 /* Matched cases are handled by the ARG == PARM test above. */
17841 return unify_template_argument_mismatch (explain_p, parm, arg);
17842
17843 case VAR_DECL:
17844 /* A non-type template parameter that is a variable should be a
17845 an integral constant, in which case, it whould have been
17846 folded into its (constant) value. So we should not be getting
17847 a variable here. */
17848 gcc_unreachable ();
17849
17850 case TYPE_ARGUMENT_PACK:
17851 case NONTYPE_ARGUMENT_PACK:
17852 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17853 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17854
17855 case TYPEOF_TYPE:
17856 case DECLTYPE_TYPE:
17857 case UNDERLYING_TYPE:
17858 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17859 or UNDERLYING_TYPE nodes. */
17860 return unify_success (explain_p);
17861
17862 case ERROR_MARK:
17863 /* Unification fails if we hit an error node. */
17864 return unify_invalid (explain_p);
17865
17866 default:
17867 /* An unresolved overload is a nondeduced context. */
17868 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17869 return unify_success (explain_p);
17870 gcc_assert (EXPR_P (parm));
17871
17872 /* We must be looking at an expression. This can happen with
17873 something like:
17874
17875 template <int I>
17876 void foo(S<I>, S<I + 2>);
17877
17878 This is a "nondeduced context":
17879
17880 [deduct.type]
17881
17882 The nondeduced contexts are:
17883
17884 --A type that is a template-id in which one or more of
17885 the template-arguments is an expression that references
17886 a template-parameter.
17887
17888 In these cases, we assume deduction succeeded, but don't
17889 actually infer any unifications. */
17890
17891 if (!uses_template_parms (parm)
17892 && !template_args_equal (parm, arg))
17893 return unify_expression_unequal (explain_p, parm, arg);
17894 else
17895 return unify_success (explain_p);
17896 }
17897 }
17898 #undef RECUR_AND_CHECK_FAILURE
17899 \f
17900 /* Note that DECL can be defined in this translation unit, if
17901 required. */
17902
17903 static void
17904 mark_definable (tree decl)
17905 {
17906 tree clone;
17907 DECL_NOT_REALLY_EXTERN (decl) = 1;
17908 FOR_EACH_CLONE (clone, decl)
17909 DECL_NOT_REALLY_EXTERN (clone) = 1;
17910 }
17911
17912 /* Called if RESULT is explicitly instantiated, or is a member of an
17913 explicitly instantiated class. */
17914
17915 void
17916 mark_decl_instantiated (tree result, int extern_p)
17917 {
17918 SET_DECL_EXPLICIT_INSTANTIATION (result);
17919
17920 /* If this entity has already been written out, it's too late to
17921 make any modifications. */
17922 if (TREE_ASM_WRITTEN (result))
17923 return;
17924
17925 /* For anonymous namespace we don't need to do anything. */
17926 if (decl_anon_ns_mem_p (result))
17927 {
17928 gcc_assert (!TREE_PUBLIC (result));
17929 return;
17930 }
17931
17932 if (TREE_CODE (result) != FUNCTION_DECL)
17933 /* The TREE_PUBLIC flag for function declarations will have been
17934 set correctly by tsubst. */
17935 TREE_PUBLIC (result) = 1;
17936
17937 /* This might have been set by an earlier implicit instantiation. */
17938 DECL_COMDAT (result) = 0;
17939
17940 if (extern_p)
17941 DECL_NOT_REALLY_EXTERN (result) = 0;
17942 else
17943 {
17944 mark_definable (result);
17945 /* Always make artificials weak. */
17946 if (DECL_ARTIFICIAL (result) && flag_weak)
17947 comdat_linkage (result);
17948 /* For WIN32 we also want to put explicit instantiations in
17949 linkonce sections. */
17950 else if (TREE_PUBLIC (result))
17951 maybe_make_one_only (result);
17952 }
17953
17954 /* If EXTERN_P, then this function will not be emitted -- unless
17955 followed by an explicit instantiation, at which point its linkage
17956 will be adjusted. If !EXTERN_P, then this function will be
17957 emitted here. In neither circumstance do we want
17958 import_export_decl to adjust the linkage. */
17959 DECL_INTERFACE_KNOWN (result) = 1;
17960 }
17961
17962 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17963 important template arguments. If any are missing, we check whether
17964 they're important by using error_mark_node for substituting into any
17965 args that were used for partial ordering (the ones between ARGS and END)
17966 and seeing if it bubbles up. */
17967
17968 static bool
17969 check_undeduced_parms (tree targs, tree args, tree end)
17970 {
17971 bool found = false;
17972 int i;
17973 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17974 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17975 {
17976 found = true;
17977 TREE_VEC_ELT (targs, i) = error_mark_node;
17978 }
17979 if (found)
17980 {
17981 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17982 if (substed == error_mark_node)
17983 return true;
17984 }
17985 return false;
17986 }
17987
17988 /* Given two function templates PAT1 and PAT2, return:
17989
17990 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17991 -1 if PAT2 is more specialized than PAT1.
17992 0 if neither is more specialized.
17993
17994 LEN indicates the number of parameters we should consider
17995 (defaulted parameters should not be considered).
17996
17997 The 1998 std underspecified function template partial ordering, and
17998 DR214 addresses the issue. We take pairs of arguments, one from
17999 each of the templates, and deduce them against each other. One of
18000 the templates will be more specialized if all the *other*
18001 template's arguments deduce against its arguments and at least one
18002 of its arguments *does* *not* deduce against the other template's
18003 corresponding argument. Deduction is done as for class templates.
18004 The arguments used in deduction have reference and top level cv
18005 qualifiers removed. Iff both arguments were originally reference
18006 types *and* deduction succeeds in both directions, an lvalue reference
18007 wins against an rvalue reference and otherwise the template
18008 with the more cv-qualified argument wins for that pairing (if
18009 neither is more cv-qualified, they both are equal). Unlike regular
18010 deduction, after all the arguments have been deduced in this way,
18011 we do *not* verify the deduced template argument values can be
18012 substituted into non-deduced contexts.
18013
18014 The logic can be a bit confusing here, because we look at deduce1 and
18015 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18016 can find template arguments for pat1 to make arg1 look like arg2, that
18017 means that arg2 is at least as specialized as arg1. */
18018
18019 int
18020 more_specialized_fn (tree pat1, tree pat2, int len)
18021 {
18022 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18023 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18024 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18025 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18026 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18027 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18028 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18029 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18030 tree origs1, origs2;
18031 bool lose1 = false;
18032 bool lose2 = false;
18033
18034 /* Remove the this parameter from non-static member functions. If
18035 one is a non-static member function and the other is not a static
18036 member function, remove the first parameter from that function
18037 also. This situation occurs for operator functions where we
18038 locate both a member function (with this pointer) and non-member
18039 operator (with explicit first operand). */
18040 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18041 {
18042 len--; /* LEN is the number of significant arguments for DECL1 */
18043 args1 = TREE_CHAIN (args1);
18044 if (!DECL_STATIC_FUNCTION_P (decl2))
18045 args2 = TREE_CHAIN (args2);
18046 }
18047 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18048 {
18049 args2 = TREE_CHAIN (args2);
18050 if (!DECL_STATIC_FUNCTION_P (decl1))
18051 {
18052 len--;
18053 args1 = TREE_CHAIN (args1);
18054 }
18055 }
18056
18057 /* If only one is a conversion operator, they are unordered. */
18058 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18059 return 0;
18060
18061 /* Consider the return type for a conversion function */
18062 if (DECL_CONV_FN_P (decl1))
18063 {
18064 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18065 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18066 len++;
18067 }
18068
18069 processing_template_decl++;
18070
18071 origs1 = args1;
18072 origs2 = args2;
18073
18074 while (len--
18075 /* Stop when an ellipsis is seen. */
18076 && args1 != NULL_TREE && args2 != NULL_TREE)
18077 {
18078 tree arg1 = TREE_VALUE (args1);
18079 tree arg2 = TREE_VALUE (args2);
18080 int deduce1, deduce2;
18081 int quals1 = -1;
18082 int quals2 = -1;
18083 int ref1 = 0;
18084 int ref2 = 0;
18085
18086 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18087 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18088 {
18089 /* When both arguments are pack expansions, we need only
18090 unify the patterns themselves. */
18091 arg1 = PACK_EXPANSION_PATTERN (arg1);
18092 arg2 = PACK_EXPANSION_PATTERN (arg2);
18093
18094 /* This is the last comparison we need to do. */
18095 len = 0;
18096 }
18097
18098 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18099 {
18100 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18101 arg1 = TREE_TYPE (arg1);
18102 quals1 = cp_type_quals (arg1);
18103 }
18104
18105 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18106 {
18107 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18108 arg2 = TREE_TYPE (arg2);
18109 quals2 = cp_type_quals (arg2);
18110 }
18111
18112 arg1 = TYPE_MAIN_VARIANT (arg1);
18113 arg2 = TYPE_MAIN_VARIANT (arg2);
18114
18115 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18116 {
18117 int i, len2 = list_length (args2);
18118 tree parmvec = make_tree_vec (1);
18119 tree argvec = make_tree_vec (len2);
18120 tree ta = args2;
18121
18122 /* Setup the parameter vector, which contains only ARG1. */
18123 TREE_VEC_ELT (parmvec, 0) = arg1;
18124
18125 /* Setup the argument vector, which contains the remaining
18126 arguments. */
18127 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18128 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18129
18130 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18131 argvec, DEDUCE_EXACT,
18132 /*subr=*/true, /*explain_p=*/false)
18133 == 0);
18134
18135 /* We cannot deduce in the other direction, because ARG1 is
18136 a pack expansion but ARG2 is not. */
18137 deduce2 = 0;
18138 }
18139 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18140 {
18141 int i, len1 = list_length (args1);
18142 tree parmvec = make_tree_vec (1);
18143 tree argvec = make_tree_vec (len1);
18144 tree ta = args1;
18145
18146 /* Setup the parameter vector, which contains only ARG1. */
18147 TREE_VEC_ELT (parmvec, 0) = arg2;
18148
18149 /* Setup the argument vector, which contains the remaining
18150 arguments. */
18151 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18152 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18153
18154 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18155 argvec, DEDUCE_EXACT,
18156 /*subr=*/true, /*explain_p=*/false)
18157 == 0);
18158
18159 /* We cannot deduce in the other direction, because ARG2 is
18160 a pack expansion but ARG1 is not.*/
18161 deduce1 = 0;
18162 }
18163
18164 else
18165 {
18166 /* The normal case, where neither argument is a pack
18167 expansion. */
18168 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18169 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18170 == 0);
18171 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18172 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18173 == 0);
18174 }
18175
18176 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18177 arg2, then arg2 is not as specialized as arg1. */
18178 if (!deduce1)
18179 lose2 = true;
18180 if (!deduce2)
18181 lose1 = true;
18182
18183 /* "If, for a given type, deduction succeeds in both directions
18184 (i.e., the types are identical after the transformations above)
18185 and both P and A were reference types (before being replaced with
18186 the type referred to above):
18187 - if the type from the argument template was an lvalue reference and
18188 the type from the parameter template was not, the argument type is
18189 considered to be more specialized than the other; otherwise,
18190 - if the type from the argument template is more cv-qualified
18191 than the type from the parameter template (as described above),
18192 the argument type is considered to be more specialized than the other;
18193 otherwise,
18194 - neither type is more specialized than the other." */
18195
18196 if (deduce1 && deduce2)
18197 {
18198 if (ref1 && ref2 && ref1 != ref2)
18199 {
18200 if (ref1 > ref2)
18201 lose1 = true;
18202 else
18203 lose2 = true;
18204 }
18205 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18206 {
18207 if ((quals1 & quals2) == quals2)
18208 lose2 = true;
18209 if ((quals1 & quals2) == quals1)
18210 lose1 = true;
18211 }
18212 }
18213
18214 if (lose1 && lose2)
18215 /* We've failed to deduce something in either direction.
18216 These must be unordered. */
18217 break;
18218
18219 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18220 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18221 /* We have already processed all of the arguments in our
18222 handing of the pack expansion type. */
18223 len = 0;
18224
18225 args1 = TREE_CHAIN (args1);
18226 args2 = TREE_CHAIN (args2);
18227 }
18228
18229 /* "In most cases, all template parameters must have values in order for
18230 deduction to succeed, but for partial ordering purposes a template
18231 parameter may remain without a value provided it is not used in the
18232 types being used for partial ordering."
18233
18234 Thus, if we are missing any of the targs1 we need to substitute into
18235 origs1, then pat2 is not as specialized as pat1. This can happen when
18236 there is a nondeduced context. */
18237 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18238 lose2 = true;
18239 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18240 lose1 = true;
18241
18242 processing_template_decl--;
18243
18244 /* All things being equal, if the next argument is a pack expansion
18245 for one function but not for the other, prefer the
18246 non-variadic function. FIXME this is bogus; see c++/41958. */
18247 if (lose1 == lose2
18248 && args1 && TREE_VALUE (args1)
18249 && args2 && TREE_VALUE (args2))
18250 {
18251 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18252 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18253 }
18254
18255 if (lose1 == lose2)
18256 return 0;
18257 else if (!lose1)
18258 return 1;
18259 else
18260 return -1;
18261 }
18262
18263 /* Determine which of two partial specializations of TMPL is more
18264 specialized.
18265
18266 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18267 to the first partial specialization. The TREE_VALUE is the
18268 innermost set of template parameters for the partial
18269 specialization. PAT2 is similar, but for the second template.
18270
18271 Return 1 if the first partial specialization is more specialized;
18272 -1 if the second is more specialized; 0 if neither is more
18273 specialized.
18274
18275 See [temp.class.order] for information about determining which of
18276 two templates is more specialized. */
18277
18278 static int
18279 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18280 {
18281 tree targs;
18282 tree tmpl1, tmpl2;
18283 int winner = 0;
18284 bool any_deductions = false;
18285
18286 tmpl1 = TREE_TYPE (pat1);
18287 tmpl2 = TREE_TYPE (pat2);
18288
18289 /* Just like what happens for functions, if we are ordering between
18290 different class template specializations, we may encounter dependent
18291 types in the arguments, and we need our dependency check functions
18292 to behave correctly. */
18293 ++processing_template_decl;
18294 targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
18295 CLASSTYPE_TI_ARGS (tmpl1),
18296 CLASSTYPE_TI_ARGS (tmpl2));
18297 if (targs)
18298 {
18299 --winner;
18300 any_deductions = true;
18301 }
18302
18303 targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
18304 CLASSTYPE_TI_ARGS (tmpl2),
18305 CLASSTYPE_TI_ARGS (tmpl1));
18306 if (targs)
18307 {
18308 ++winner;
18309 any_deductions = true;
18310 }
18311 --processing_template_decl;
18312
18313 /* In the case of a tie where at least one of the class templates
18314 has a parameter pack at the end, the template with the most
18315 non-packed parameters wins. */
18316 if (winner == 0
18317 && any_deductions
18318 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18319 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18320 {
18321 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18322 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18323 int len1 = TREE_VEC_LENGTH (args1);
18324 int len2 = TREE_VEC_LENGTH (args2);
18325
18326 /* We don't count the pack expansion at the end. */
18327 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18328 --len1;
18329 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18330 --len2;
18331
18332 if (len1 > len2)
18333 return 1;
18334 else if (len1 < len2)
18335 return -1;
18336 }
18337
18338 return winner;
18339 }
18340
18341 /* Return the template arguments that will produce the function signature
18342 DECL from the function template FN, with the explicit template
18343 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18344 also match. Return NULL_TREE if no satisfactory arguments could be
18345 found. */
18346
18347 static tree
18348 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18349 {
18350 int ntparms = DECL_NTPARMS (fn);
18351 tree targs = make_tree_vec (ntparms);
18352 tree decl_type = TREE_TYPE (decl);
18353 tree decl_arg_types;
18354 tree *args;
18355 unsigned int nargs, ix;
18356 tree arg;
18357
18358 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18359
18360 /* Never do unification on the 'this' parameter. */
18361 decl_arg_types = skip_artificial_parms_for (decl,
18362 TYPE_ARG_TYPES (decl_type));
18363
18364 nargs = list_length (decl_arg_types);
18365 args = XALLOCAVEC (tree, nargs);
18366 for (arg = decl_arg_types, ix = 0;
18367 arg != NULL_TREE && arg != void_list_node;
18368 arg = TREE_CHAIN (arg), ++ix)
18369 args[ix] = TREE_VALUE (arg);
18370
18371 if (fn_type_unification (fn, explicit_args, targs,
18372 args, ix,
18373 (check_rettype || DECL_CONV_FN_P (fn)
18374 ? TREE_TYPE (decl_type) : NULL_TREE),
18375 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18376 /*decltype*/false)
18377 == error_mark_node)
18378 return NULL_TREE;
18379
18380 return targs;
18381 }
18382
18383 /* Return the innermost template arguments that, when applied to a partial
18384 specialization of TMPL whose innermost template parameters are
18385 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18386 ARGS.
18387
18388 For example, suppose we have:
18389
18390 template <class T, class U> struct S {};
18391 template <class T> struct S<T*, int> {};
18392
18393 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18394 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18395 int}. The resulting vector will be {double}, indicating that `T'
18396 is bound to `double'. */
18397
18398 static tree
18399 get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
18400 {
18401 int i, ntparms = TREE_VEC_LENGTH (tparms);
18402 tree deduced_args;
18403 tree innermost_deduced_args;
18404
18405 innermost_deduced_args = make_tree_vec (ntparms);
18406 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18407 {
18408 deduced_args = copy_node (args);
18409 SET_TMPL_ARGS_LEVEL (deduced_args,
18410 TMPL_ARGS_DEPTH (deduced_args),
18411 innermost_deduced_args);
18412 }
18413 else
18414 deduced_args = innermost_deduced_args;
18415
18416 if (unify (tparms, deduced_args,
18417 INNERMOST_TEMPLATE_ARGS (spec_args),
18418 INNERMOST_TEMPLATE_ARGS (args),
18419 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18420 return NULL_TREE;
18421
18422 for (i = 0; i < ntparms; ++i)
18423 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18424 return NULL_TREE;
18425
18426 /* Verify that nondeduced template arguments agree with the type
18427 obtained from argument deduction.
18428
18429 For example:
18430
18431 struct A { typedef int X; };
18432 template <class T, class U> struct C {};
18433 template <class T> struct C<T, typename T::X> {};
18434
18435 Then with the instantiation `C<A, int>', we can deduce that
18436 `T' is `A' but unify () does not check whether `typename T::X'
18437 is `int'. */
18438 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18439 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18440 spec_args, tmpl,
18441 tf_none, false, false);
18442 if (spec_args == error_mark_node
18443 /* We only need to check the innermost arguments; the other
18444 arguments will always agree. */
18445 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18446 INNERMOST_TEMPLATE_ARGS (args)))
18447 return NULL_TREE;
18448
18449 /* Now that we have bindings for all of the template arguments,
18450 ensure that the arguments deduced for the template template
18451 parameters have compatible template parameter lists. See the use
18452 of template_template_parm_bindings_ok_p in fn_type_unification
18453 for more information. */
18454 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18455 return NULL_TREE;
18456
18457 return deduced_args;
18458 }
18459
18460 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18461 Return the TREE_LIST node with the most specialized template, if
18462 any. If there is no most specialized template, the error_mark_node
18463 is returned.
18464
18465 Note that this function does not look at, or modify, the
18466 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18467 returned is one of the elements of INSTANTIATIONS, callers may
18468 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18469 and retrieve it from the value returned. */
18470
18471 tree
18472 most_specialized_instantiation (tree templates)
18473 {
18474 tree fn, champ;
18475
18476 ++processing_template_decl;
18477
18478 champ = templates;
18479 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18480 {
18481 int fate = 0;
18482
18483 if (get_bindings (TREE_VALUE (champ),
18484 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18485 NULL_TREE, /*check_ret=*/true))
18486 fate--;
18487
18488 if (get_bindings (TREE_VALUE (fn),
18489 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18490 NULL_TREE, /*check_ret=*/true))
18491 fate++;
18492
18493 if (fate == -1)
18494 champ = fn;
18495 else if (!fate)
18496 {
18497 /* Equally specialized, move to next function. If there
18498 is no next function, nothing's most specialized. */
18499 fn = TREE_CHAIN (fn);
18500 champ = fn;
18501 if (!fn)
18502 break;
18503 }
18504 }
18505
18506 if (champ)
18507 /* Now verify that champ is better than everything earlier in the
18508 instantiation list. */
18509 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18510 if (get_bindings (TREE_VALUE (champ),
18511 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18512 NULL_TREE, /*check_ret=*/true)
18513 || !get_bindings (TREE_VALUE (fn),
18514 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18515 NULL_TREE, /*check_ret=*/true))
18516 {
18517 champ = NULL_TREE;
18518 break;
18519 }
18520
18521 processing_template_decl--;
18522
18523 if (!champ)
18524 return error_mark_node;
18525
18526 return champ;
18527 }
18528
18529 /* If DECL is a specialization of some template, return the most
18530 general such template. Otherwise, returns NULL_TREE.
18531
18532 For example, given:
18533
18534 template <class T> struct S { template <class U> void f(U); };
18535
18536 if TMPL is `template <class U> void S<int>::f(U)' this will return
18537 the full template. This function will not trace past partial
18538 specializations, however. For example, given in addition:
18539
18540 template <class T> struct S<T*> { template <class U> void f(U); };
18541
18542 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18543 `template <class T> template <class U> S<T*>::f(U)'. */
18544
18545 tree
18546 most_general_template (tree decl)
18547 {
18548 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18549 an immediate specialization. */
18550 if (TREE_CODE (decl) == FUNCTION_DECL)
18551 {
18552 if (DECL_TEMPLATE_INFO (decl)) {
18553 decl = DECL_TI_TEMPLATE (decl);
18554
18555 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18556 template friend. */
18557 if (TREE_CODE (decl) != TEMPLATE_DECL)
18558 return NULL_TREE;
18559 } else
18560 return NULL_TREE;
18561 }
18562
18563 /* Look for more and more general templates. */
18564 while (DECL_TEMPLATE_INFO (decl))
18565 {
18566 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18567 (See cp-tree.h for details.) */
18568 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18569 break;
18570
18571 if (CLASS_TYPE_P (TREE_TYPE (decl))
18572 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18573 break;
18574
18575 /* Stop if we run into an explicitly specialized class template. */
18576 if (!DECL_NAMESPACE_SCOPE_P (decl)
18577 && DECL_CONTEXT (decl)
18578 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18579 break;
18580
18581 decl = DECL_TI_TEMPLATE (decl);
18582 }
18583
18584 return decl;
18585 }
18586
18587 /* Return the most specialized of the class template partial
18588 specializations which can produce TYPE, a specialization of some class
18589 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18590 a _TYPE node corresponding to the partial specialization, while the
18591 TREE_PURPOSE is the set of template arguments that must be
18592 substituted into the TREE_TYPE in order to generate TYPE.
18593
18594 If the choice of partial specialization is ambiguous, a diagnostic
18595 is issued, and the error_mark_node is returned. If there are no
18596 partial specializations matching TYPE, then NULL_TREE is
18597 returned, indicating that the primary template should be used. */
18598
18599 static tree
18600 most_specialized_class (tree type, tsubst_flags_t complain)
18601 {
18602 tree list = NULL_TREE;
18603 tree t;
18604 tree champ;
18605 int fate;
18606 bool ambiguous_p;
18607 tree outer_args = NULL_TREE;
18608
18609 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
18610 tree main_tmpl = most_general_template (tmpl);
18611 tree args = CLASSTYPE_TI_ARGS (type);
18612
18613 /* For determining which partial specialization to use, only the
18614 innermost args are interesting. */
18615 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18616 {
18617 outer_args = strip_innermost_template_args (args, 1);
18618 args = INNERMOST_TEMPLATE_ARGS (args);
18619 }
18620
18621 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
18622 {
18623 tree partial_spec_args;
18624 tree spec_args;
18625 tree spec_tmpl = TREE_VALUE (t);
18626 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18627
18628 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18629
18630 ++processing_template_decl;
18631
18632 if (outer_args)
18633 {
18634 /* Discard the outer levels of args, and then substitute in the
18635 template args from the enclosing class. */
18636 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18637 partial_spec_args = tsubst_template_args
18638 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18639
18640 /* And the same for the partial specialization TEMPLATE_DECL. */
18641 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18642 }
18643
18644 partial_spec_args =
18645 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18646 partial_spec_args,
18647 tmpl, tf_none,
18648 /*require_all_args=*/true,
18649 /*use_default_args=*/true);
18650
18651 --processing_template_decl;
18652
18653 if (partial_spec_args == error_mark_node)
18654 return error_mark_node;
18655 if (spec_tmpl == error_mark_node)
18656 return error_mark_node;
18657
18658 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18659 spec_args = get_class_bindings (tmpl, parms,
18660 partial_spec_args,
18661 args);
18662 if (spec_args)
18663 {
18664 if (outer_args)
18665 spec_args = add_to_template_args (outer_args, spec_args);
18666 list = tree_cons (spec_args, orig_parms, list);
18667 TREE_TYPE (list) = TREE_TYPE (t);
18668 }
18669 }
18670
18671 if (! list)
18672 return NULL_TREE;
18673
18674 ambiguous_p = false;
18675 t = list;
18676 champ = t;
18677 t = TREE_CHAIN (t);
18678 for (; t; t = TREE_CHAIN (t))
18679 {
18680 fate = more_specialized_class (tmpl, champ, t);
18681 if (fate == 1)
18682 ;
18683 else
18684 {
18685 if (fate == 0)
18686 {
18687 t = TREE_CHAIN (t);
18688 if (! t)
18689 {
18690 ambiguous_p = true;
18691 break;
18692 }
18693 }
18694 champ = t;
18695 }
18696 }
18697
18698 if (!ambiguous_p)
18699 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18700 {
18701 fate = more_specialized_class (tmpl, champ, t);
18702 if (fate != 1)
18703 {
18704 ambiguous_p = true;
18705 break;
18706 }
18707 }
18708
18709 if (ambiguous_p)
18710 {
18711 const char *str;
18712 char *spaces = NULL;
18713 if (!(complain & tf_error))
18714 return error_mark_node;
18715 error ("ambiguous class template instantiation for %q#T", type);
18716 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18717 for (t = list; t; t = TREE_CHAIN (t))
18718 {
18719 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18720 spaces = spaces ? spaces : get_spaces (str);
18721 }
18722 free (spaces);
18723 return error_mark_node;
18724 }
18725
18726 return champ;
18727 }
18728
18729 /* Explicitly instantiate DECL. */
18730
18731 void
18732 do_decl_instantiation (tree decl, tree storage)
18733 {
18734 tree result = NULL_TREE;
18735 int extern_p = 0;
18736
18737 if (!decl || decl == error_mark_node)
18738 /* An error occurred, for which grokdeclarator has already issued
18739 an appropriate message. */
18740 return;
18741 else if (! DECL_LANG_SPECIFIC (decl))
18742 {
18743 error ("explicit instantiation of non-template %q#D", decl);
18744 return;
18745 }
18746 else if (VAR_P (decl))
18747 {
18748 /* There is an asymmetry here in the way VAR_DECLs and
18749 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18750 the latter, the DECL we get back will be marked as a
18751 template instantiation, and the appropriate
18752 DECL_TEMPLATE_INFO will be set up. This does not happen for
18753 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18754 should handle VAR_DECLs as it currently handles
18755 FUNCTION_DECLs. */
18756 if (!DECL_CLASS_SCOPE_P (decl))
18757 {
18758 error ("%qD is not a static data member of a class template", decl);
18759 return;
18760 }
18761 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18762 if (!result || !VAR_P (result))
18763 {
18764 error ("no matching template for %qD found", decl);
18765 return;
18766 }
18767 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18768 {
18769 error ("type %qT for explicit instantiation %qD does not match "
18770 "declared type %qT", TREE_TYPE (result), decl,
18771 TREE_TYPE (decl));
18772 return;
18773 }
18774 }
18775 else if (TREE_CODE (decl) != FUNCTION_DECL)
18776 {
18777 error ("explicit instantiation of %q#D", decl);
18778 return;
18779 }
18780 else
18781 result = decl;
18782
18783 /* Check for various error cases. Note that if the explicit
18784 instantiation is valid the RESULT will currently be marked as an
18785 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18786 until we get here. */
18787
18788 if (DECL_TEMPLATE_SPECIALIZATION (result))
18789 {
18790 /* DR 259 [temp.spec].
18791
18792 Both an explicit instantiation and a declaration of an explicit
18793 specialization shall not appear in a program unless the explicit
18794 instantiation follows a declaration of the explicit specialization.
18795
18796 For a given set of template parameters, if an explicit
18797 instantiation of a template appears after a declaration of an
18798 explicit specialization for that template, the explicit
18799 instantiation has no effect. */
18800 return;
18801 }
18802 else if (DECL_EXPLICIT_INSTANTIATION (result))
18803 {
18804 /* [temp.spec]
18805
18806 No program shall explicitly instantiate any template more
18807 than once.
18808
18809 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18810 the first instantiation was `extern' and the second is not,
18811 and EXTERN_P for the opposite case. */
18812 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18813 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18814 /* If an "extern" explicit instantiation follows an ordinary
18815 explicit instantiation, the template is instantiated. */
18816 if (extern_p)
18817 return;
18818 }
18819 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18820 {
18821 error ("no matching template for %qD found", result);
18822 return;
18823 }
18824 else if (!DECL_TEMPLATE_INFO (result))
18825 {
18826 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18827 return;
18828 }
18829
18830 if (storage == NULL_TREE)
18831 ;
18832 else if (storage == ridpointers[(int) RID_EXTERN])
18833 {
18834 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
18835 pedwarn (input_location, OPT_Wpedantic,
18836 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18837 "instantiations");
18838 extern_p = 1;
18839 }
18840 else
18841 error ("storage class %qD applied to template instantiation", storage);
18842
18843 check_explicit_instantiation_namespace (result);
18844 mark_decl_instantiated (result, extern_p);
18845 if (! extern_p)
18846 instantiate_decl (result, /*defer_ok=*/1,
18847 /*expl_inst_class_mem_p=*/false);
18848 }
18849
18850 static void
18851 mark_class_instantiated (tree t, int extern_p)
18852 {
18853 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18854 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18855 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18856 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18857 if (! extern_p)
18858 {
18859 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18860 rest_of_type_compilation (t, 1);
18861 }
18862 }
18863
18864 /* Called from do_type_instantiation through binding_table_foreach to
18865 do recursive instantiation for the type bound in ENTRY. */
18866 static void
18867 bt_instantiate_type_proc (binding_entry entry, void *data)
18868 {
18869 tree storage = *(tree *) data;
18870
18871 if (MAYBE_CLASS_TYPE_P (entry->type)
18872 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18873 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18874 }
18875
18876 /* Called from do_type_instantiation to instantiate a member
18877 (a member function or a static member variable) of an
18878 explicitly instantiated class template. */
18879 static void
18880 instantiate_class_member (tree decl, int extern_p)
18881 {
18882 mark_decl_instantiated (decl, extern_p);
18883 if (! extern_p)
18884 instantiate_decl (decl, /*defer_ok=*/1,
18885 /*expl_inst_class_mem_p=*/true);
18886 }
18887
18888 /* Perform an explicit instantiation of template class T. STORAGE, if
18889 non-null, is the RID for extern, inline or static. COMPLAIN is
18890 nonzero if this is called from the parser, zero if called recursively,
18891 since the standard is unclear (as detailed below). */
18892
18893 void
18894 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18895 {
18896 int extern_p = 0;
18897 int nomem_p = 0;
18898 int static_p = 0;
18899 int previous_instantiation_extern_p = 0;
18900
18901 if (TREE_CODE (t) == TYPE_DECL)
18902 t = TREE_TYPE (t);
18903
18904 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18905 {
18906 tree tmpl =
18907 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18908 if (tmpl)
18909 error ("explicit instantiation of non-class template %qD", tmpl);
18910 else
18911 error ("explicit instantiation of non-template type %qT", t);
18912 return;
18913 }
18914
18915 complete_type (t);
18916
18917 if (!COMPLETE_TYPE_P (t))
18918 {
18919 if (complain & tf_error)
18920 error ("explicit instantiation of %q#T before definition of template",
18921 t);
18922 return;
18923 }
18924
18925 if (storage != NULL_TREE)
18926 {
18927 if (!in_system_header_at (input_location))
18928 {
18929 if (storage == ridpointers[(int) RID_EXTERN])
18930 {
18931 if (cxx_dialect == cxx98)
18932 pedwarn (input_location, OPT_Wpedantic,
18933 "ISO C++ 1998 forbids the use of %<extern%> on "
18934 "explicit instantiations");
18935 }
18936 else
18937 pedwarn (input_location, OPT_Wpedantic,
18938 "ISO C++ forbids the use of %qE"
18939 " on explicit instantiations", storage);
18940 }
18941
18942 if (storage == ridpointers[(int) RID_INLINE])
18943 nomem_p = 1;
18944 else if (storage == ridpointers[(int) RID_EXTERN])
18945 extern_p = 1;
18946 else if (storage == ridpointers[(int) RID_STATIC])
18947 static_p = 1;
18948 else
18949 {
18950 error ("storage class %qD applied to template instantiation",
18951 storage);
18952 extern_p = 0;
18953 }
18954 }
18955
18956 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18957 {
18958 /* DR 259 [temp.spec].
18959
18960 Both an explicit instantiation and a declaration of an explicit
18961 specialization shall not appear in a program unless the explicit
18962 instantiation follows a declaration of the explicit specialization.
18963
18964 For a given set of template parameters, if an explicit
18965 instantiation of a template appears after a declaration of an
18966 explicit specialization for that template, the explicit
18967 instantiation has no effect. */
18968 return;
18969 }
18970 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18971 {
18972 /* [temp.spec]
18973
18974 No program shall explicitly instantiate any template more
18975 than once.
18976
18977 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18978 instantiation was `extern'. If EXTERN_P then the second is.
18979 These cases are OK. */
18980 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18981
18982 if (!previous_instantiation_extern_p && !extern_p
18983 && (complain & tf_error))
18984 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18985
18986 /* If we've already instantiated the template, just return now. */
18987 if (!CLASSTYPE_INTERFACE_ONLY (t))
18988 return;
18989 }
18990
18991 check_explicit_instantiation_namespace (TYPE_NAME (t));
18992 mark_class_instantiated (t, extern_p);
18993
18994 if (nomem_p)
18995 return;
18996
18997 {
18998 tree tmp;
18999
19000 /* In contrast to implicit instantiation, where only the
19001 declarations, and not the definitions, of members are
19002 instantiated, we have here:
19003
19004 [temp.explicit]
19005
19006 The explicit instantiation of a class template specialization
19007 implies the instantiation of all of its members not
19008 previously explicitly specialized in the translation unit
19009 containing the explicit instantiation.
19010
19011 Of course, we can't instantiate member template classes, since
19012 we don't have any arguments for them. Note that the standard
19013 is unclear on whether the instantiation of the members are
19014 *explicit* instantiations or not. However, the most natural
19015 interpretation is that it should be an explicit instantiation. */
19016
19017 if (! static_p)
19018 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19019 if (TREE_CODE (tmp) == FUNCTION_DECL
19020 && DECL_TEMPLATE_INSTANTIATION (tmp))
19021 instantiate_class_member (tmp, extern_p);
19022
19023 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19024 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19025 instantiate_class_member (tmp, extern_p);
19026
19027 if (CLASSTYPE_NESTED_UTDS (t))
19028 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19029 bt_instantiate_type_proc, &storage);
19030 }
19031 }
19032
19033 /* Given a function DECL, which is a specialization of TMPL, modify
19034 DECL to be a re-instantiation of TMPL with the same template
19035 arguments. TMPL should be the template into which tsubst'ing
19036 should occur for DECL, not the most general template.
19037
19038 One reason for doing this is a scenario like this:
19039
19040 template <class T>
19041 void f(const T&, int i);
19042
19043 void g() { f(3, 7); }
19044
19045 template <class T>
19046 void f(const T& t, const int i) { }
19047
19048 Note that when the template is first instantiated, with
19049 instantiate_template, the resulting DECL will have no name for the
19050 first parameter, and the wrong type for the second. So, when we go
19051 to instantiate the DECL, we regenerate it. */
19052
19053 static void
19054 regenerate_decl_from_template (tree decl, tree tmpl)
19055 {
19056 /* The arguments used to instantiate DECL, from the most general
19057 template. */
19058 tree args;
19059 tree code_pattern;
19060
19061 args = DECL_TI_ARGS (decl);
19062 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19063
19064 /* Make sure that we can see identifiers, and compute access
19065 correctly. */
19066 push_access_scope (decl);
19067
19068 if (TREE_CODE (decl) == FUNCTION_DECL)
19069 {
19070 tree decl_parm;
19071 tree pattern_parm;
19072 tree specs;
19073 int args_depth;
19074 int parms_depth;
19075
19076 args_depth = TMPL_ARGS_DEPTH (args);
19077 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19078 if (args_depth > parms_depth)
19079 args = get_innermost_template_args (args, parms_depth);
19080
19081 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19082 args, tf_error, NULL_TREE,
19083 /*defer_ok*/false);
19084 if (specs && specs != error_mark_node)
19085 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19086 specs);
19087
19088 /* Merge parameter declarations. */
19089 decl_parm = skip_artificial_parms_for (decl,
19090 DECL_ARGUMENTS (decl));
19091 pattern_parm
19092 = skip_artificial_parms_for (code_pattern,
19093 DECL_ARGUMENTS (code_pattern));
19094 while (decl_parm && !DECL_PACK_P (pattern_parm))
19095 {
19096 tree parm_type;
19097 tree attributes;
19098
19099 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19100 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19101 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19102 NULL_TREE);
19103 parm_type = type_decays_to (parm_type);
19104 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19105 TREE_TYPE (decl_parm) = parm_type;
19106 attributes = DECL_ATTRIBUTES (pattern_parm);
19107 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19108 {
19109 DECL_ATTRIBUTES (decl_parm) = attributes;
19110 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19111 }
19112 decl_parm = DECL_CHAIN (decl_parm);
19113 pattern_parm = DECL_CHAIN (pattern_parm);
19114 }
19115 /* Merge any parameters that match with the function parameter
19116 pack. */
19117 if (pattern_parm && DECL_PACK_P (pattern_parm))
19118 {
19119 int i, len;
19120 tree expanded_types;
19121 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19122 the parameters in this function parameter pack. */
19123 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19124 args, tf_error, NULL_TREE);
19125 len = TREE_VEC_LENGTH (expanded_types);
19126 for (i = 0; i < len; i++)
19127 {
19128 tree parm_type;
19129 tree attributes;
19130
19131 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19132 /* Rename the parameter to include the index. */
19133 DECL_NAME (decl_parm) =
19134 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19135 parm_type = TREE_VEC_ELT (expanded_types, i);
19136 parm_type = type_decays_to (parm_type);
19137 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19138 TREE_TYPE (decl_parm) = parm_type;
19139 attributes = DECL_ATTRIBUTES (pattern_parm);
19140 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19141 {
19142 DECL_ATTRIBUTES (decl_parm) = attributes;
19143 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19144 }
19145 decl_parm = DECL_CHAIN (decl_parm);
19146 }
19147 }
19148 /* Merge additional specifiers from the CODE_PATTERN. */
19149 if (DECL_DECLARED_INLINE_P (code_pattern)
19150 && !DECL_DECLARED_INLINE_P (decl))
19151 DECL_DECLARED_INLINE_P (decl) = 1;
19152 }
19153 else if (VAR_P (decl))
19154 {
19155 DECL_INITIAL (decl) =
19156 tsubst_expr (DECL_INITIAL (code_pattern), args,
19157 tf_error, DECL_TI_TEMPLATE (decl),
19158 /*integral_constant_expression_p=*/false);
19159 if (VAR_HAD_UNKNOWN_BOUND (decl))
19160 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19161 tf_error, DECL_TI_TEMPLATE (decl));
19162 }
19163 else
19164 gcc_unreachable ();
19165
19166 pop_access_scope (decl);
19167 }
19168
19169 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19170 substituted to get DECL. */
19171
19172 tree
19173 template_for_substitution (tree decl)
19174 {
19175 tree tmpl = DECL_TI_TEMPLATE (decl);
19176
19177 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19178 for the instantiation. This is not always the most general
19179 template. Consider, for example:
19180
19181 template <class T>
19182 struct S { template <class U> void f();
19183 template <> void f<int>(); };
19184
19185 and an instantiation of S<double>::f<int>. We want TD to be the
19186 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19187 while (/* An instantiation cannot have a definition, so we need a
19188 more general template. */
19189 DECL_TEMPLATE_INSTANTIATION (tmpl)
19190 /* We must also deal with friend templates. Given:
19191
19192 template <class T> struct S {
19193 template <class U> friend void f() {};
19194 };
19195
19196 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19197 so far as the language is concerned, but that's still
19198 where we get the pattern for the instantiation from. On
19199 other hand, if the definition comes outside the class, say:
19200
19201 template <class T> struct S {
19202 template <class U> friend void f();
19203 };
19204 template <class U> friend void f() {}
19205
19206 we don't need to look any further. That's what the check for
19207 DECL_INITIAL is for. */
19208 || (TREE_CODE (decl) == FUNCTION_DECL
19209 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19210 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19211 {
19212 /* The present template, TD, should not be a definition. If it
19213 were a definition, we should be using it! Note that we
19214 cannot restructure the loop to just keep going until we find
19215 a template with a definition, since that might go too far if
19216 a specialization was declared, but not defined. */
19217 gcc_assert (!VAR_P (decl)
19218 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19219
19220 /* Fetch the more general template. */
19221 tmpl = DECL_TI_TEMPLATE (tmpl);
19222 }
19223
19224 return tmpl;
19225 }
19226
19227 /* Returns true if we need to instantiate this template instance even if we
19228 know we aren't going to emit it.. */
19229
19230 bool
19231 always_instantiate_p (tree decl)
19232 {
19233 /* We always instantiate inline functions so that we can inline them. An
19234 explicit instantiation declaration prohibits implicit instantiation of
19235 non-inline functions. With high levels of optimization, we would
19236 normally inline non-inline functions -- but we're not allowed to do
19237 that for "extern template" functions. Therefore, we check
19238 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19239 return ((TREE_CODE (decl) == FUNCTION_DECL
19240 && (DECL_DECLARED_INLINE_P (decl)
19241 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19242 /* And we need to instantiate static data members so that
19243 their initializers are available in integral constant
19244 expressions. */
19245 || (VAR_P (decl)
19246 && decl_maybe_constant_var_p (decl)));
19247 }
19248
19249 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19250 instantiate it now, modifying TREE_TYPE (fn). */
19251
19252 void
19253 maybe_instantiate_noexcept (tree fn)
19254 {
19255 tree fntype, spec, noex, clone;
19256
19257 if (DECL_CLONED_FUNCTION_P (fn))
19258 fn = DECL_CLONED_FUNCTION (fn);
19259 fntype = TREE_TYPE (fn);
19260 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19261
19262 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19263 return;
19264
19265 noex = TREE_PURPOSE (spec);
19266
19267 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19268 {
19269 if (push_tinst_level (fn))
19270 {
19271 push_access_scope (fn);
19272 push_deferring_access_checks (dk_no_deferred);
19273 input_location = DECL_SOURCE_LOCATION (fn);
19274 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19275 DEFERRED_NOEXCEPT_ARGS (noex),
19276 tf_warning_or_error, fn,
19277 /*function_p=*/false,
19278 /*integral_constant_expression_p=*/true);
19279 pop_deferring_access_checks ();
19280 pop_access_scope (fn);
19281 pop_tinst_level ();
19282 spec = build_noexcept_spec (noex, tf_warning_or_error);
19283 if (spec == error_mark_node)
19284 spec = noexcept_false_spec;
19285 }
19286 else
19287 spec = noexcept_false_spec;
19288 }
19289 else
19290 {
19291 /* This is an implicitly declared function, so NOEX is a list of
19292 other functions to evaluate and merge. */
19293 tree elt;
19294 spec = noexcept_true_spec;
19295 for (elt = noex; elt; elt = OVL_NEXT (elt))
19296 {
19297 tree fn = OVL_CURRENT (elt);
19298 tree subspec;
19299 maybe_instantiate_noexcept (fn);
19300 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19301 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19302 }
19303 }
19304
19305 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19306
19307 FOR_EACH_CLONE (clone, fn)
19308 {
19309 if (TREE_TYPE (clone) == fntype)
19310 TREE_TYPE (clone) = TREE_TYPE (fn);
19311 else
19312 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19313 }
19314 }
19315
19316 /* Produce the definition of D, a _DECL generated from a template. If
19317 DEFER_OK is nonzero, then we don't have to actually do the
19318 instantiation now; we just have to do it sometime. Normally it is
19319 an error if this is an explicit instantiation but D is undefined.
19320 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19321 explicitly instantiated class template. */
19322
19323 tree
19324 instantiate_decl (tree d, int defer_ok,
19325 bool expl_inst_class_mem_p)
19326 {
19327 tree tmpl = DECL_TI_TEMPLATE (d);
19328 tree gen_args;
19329 tree args;
19330 tree td;
19331 tree code_pattern;
19332 tree spec;
19333 tree gen_tmpl;
19334 bool pattern_defined;
19335 location_t saved_loc = input_location;
19336 int saved_unevaluated_operand = cp_unevaluated_operand;
19337 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19338 bool external_p;
19339 tree fn_context;
19340 bool nested;
19341
19342 /* This function should only be used to instantiate templates for
19343 functions and static member variables. */
19344 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19345
19346 /* Variables are never deferred; if instantiation is required, they
19347 are instantiated right away. That allows for better code in the
19348 case that an expression refers to the value of the variable --
19349 if the variable has a constant value the referring expression can
19350 take advantage of that fact. */
19351 if (VAR_P (d)
19352 || DECL_DECLARED_CONSTEXPR_P (d))
19353 defer_ok = 0;
19354
19355 /* Don't instantiate cloned functions. Instead, instantiate the
19356 functions they cloned. */
19357 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19358 d = DECL_CLONED_FUNCTION (d);
19359
19360 if (DECL_TEMPLATE_INSTANTIATED (d)
19361 || (TREE_CODE (d) == FUNCTION_DECL
19362 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19363 || DECL_TEMPLATE_SPECIALIZATION (d))
19364 /* D has already been instantiated or explicitly specialized, so
19365 there's nothing for us to do here.
19366
19367 It might seem reasonable to check whether or not D is an explicit
19368 instantiation, and, if so, stop here. But when an explicit
19369 instantiation is deferred until the end of the compilation,
19370 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19371 the instantiation. */
19372 return d;
19373
19374 /* Check to see whether we know that this template will be
19375 instantiated in some other file, as with "extern template"
19376 extension. */
19377 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19378
19379 /* In general, we do not instantiate such templates. */
19380 if (external_p && !always_instantiate_p (d))
19381 return d;
19382
19383 gen_tmpl = most_general_template (tmpl);
19384 gen_args = DECL_TI_ARGS (d);
19385
19386 if (tmpl != gen_tmpl)
19387 /* We should already have the extra args. */
19388 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19389 == TMPL_ARGS_DEPTH (gen_args));
19390 /* And what's in the hash table should match D. */
19391 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19392 || spec == NULL_TREE);
19393
19394 /* This needs to happen before any tsubsting. */
19395 if (! push_tinst_level (d))
19396 return d;
19397
19398 timevar_push (TV_TEMPLATE_INST);
19399
19400 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19401 for the instantiation. */
19402 td = template_for_substitution (d);
19403 code_pattern = DECL_TEMPLATE_RESULT (td);
19404
19405 /* We should never be trying to instantiate a member of a class
19406 template or partial specialization. */
19407 gcc_assert (d != code_pattern);
19408
19409 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19410 || DECL_TEMPLATE_SPECIALIZATION (td))
19411 /* In the case of a friend template whose definition is provided
19412 outside the class, we may have too many arguments. Drop the
19413 ones we don't need. The same is true for specializations. */
19414 args = get_innermost_template_args
19415 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19416 else
19417 args = gen_args;
19418
19419 if (TREE_CODE (d) == FUNCTION_DECL)
19420 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19421 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19422 else
19423 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19424
19425 /* We may be in the middle of deferred access check. Disable it now. */
19426 push_deferring_access_checks (dk_no_deferred);
19427
19428 /* Unless an explicit instantiation directive has already determined
19429 the linkage of D, remember that a definition is available for
19430 this entity. */
19431 if (pattern_defined
19432 && !DECL_INTERFACE_KNOWN (d)
19433 && !DECL_NOT_REALLY_EXTERN (d))
19434 mark_definable (d);
19435
19436 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19437 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19438 input_location = DECL_SOURCE_LOCATION (d);
19439
19440 /* If D is a member of an explicitly instantiated class template,
19441 and no definition is available, treat it like an implicit
19442 instantiation. */
19443 if (!pattern_defined && expl_inst_class_mem_p
19444 && DECL_EXPLICIT_INSTANTIATION (d))
19445 {
19446 /* Leave linkage flags alone on instantiations with anonymous
19447 visibility. */
19448 if (TREE_PUBLIC (d))
19449 {
19450 DECL_NOT_REALLY_EXTERN (d) = 0;
19451 DECL_INTERFACE_KNOWN (d) = 0;
19452 }
19453 SET_DECL_IMPLICIT_INSTANTIATION (d);
19454 }
19455
19456 if (TREE_CODE (d) == FUNCTION_DECL)
19457 maybe_instantiate_noexcept (d);
19458
19459 /* Defer all other templates, unless we have been explicitly
19460 forbidden from doing so. */
19461 if (/* If there is no definition, we cannot instantiate the
19462 template. */
19463 ! pattern_defined
19464 /* If it's OK to postpone instantiation, do so. */
19465 || defer_ok
19466 /* If this is a static data member that will be defined
19467 elsewhere, we don't want to instantiate the entire data
19468 member, but we do want to instantiate the initializer so that
19469 we can substitute that elsewhere. */
19470 || (external_p && VAR_P (d)))
19471 {
19472 /* The definition of the static data member is now required so
19473 we must substitute the initializer. */
19474 if (VAR_P (d)
19475 && !DECL_INITIAL (d)
19476 && DECL_INITIAL (code_pattern))
19477 {
19478 tree ns;
19479 tree init;
19480 bool const_init = false;
19481
19482 ns = decl_namespace_context (d);
19483 push_nested_namespace (ns);
19484 push_nested_class (DECL_CONTEXT (d));
19485 init = tsubst_expr (DECL_INITIAL (code_pattern),
19486 args,
19487 tf_warning_or_error, NULL_TREE,
19488 /*integral_constant_expression_p=*/false);
19489 /* Make sure the initializer is still constant, in case of
19490 circular dependency (template/instantiate6.C). */
19491 const_init
19492 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19493 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19494 /*asmspec_tree=*/NULL_TREE,
19495 LOOKUP_ONLYCONVERTING);
19496 pop_nested_class ();
19497 pop_nested_namespace (ns);
19498 }
19499
19500 /* We restore the source position here because it's used by
19501 add_pending_template. */
19502 input_location = saved_loc;
19503
19504 if (at_eof && !pattern_defined
19505 && DECL_EXPLICIT_INSTANTIATION (d)
19506 && DECL_NOT_REALLY_EXTERN (d))
19507 /* [temp.explicit]
19508
19509 The definition of a non-exported function template, a
19510 non-exported member function template, or a non-exported
19511 member function or static data member of a class template
19512 shall be present in every translation unit in which it is
19513 explicitly instantiated. */
19514 permerror (input_location, "explicit instantiation of %qD "
19515 "but no definition available", d);
19516
19517 /* If we're in unevaluated context, we just wanted to get the
19518 constant value; this isn't an odr use, so don't queue
19519 a full instantiation. */
19520 if (cp_unevaluated_operand != 0)
19521 goto out;
19522 /* ??? Historically, we have instantiated inline functions, even
19523 when marked as "extern template". */
19524 if (!(external_p && VAR_P (d)))
19525 add_pending_template (d);
19526 goto out;
19527 }
19528 /* Tell the repository that D is available in this translation unit
19529 -- and see if it is supposed to be instantiated here. */
19530 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19531 {
19532 /* In a PCH file, despite the fact that the repository hasn't
19533 requested instantiation in the PCH it is still possible that
19534 an instantiation will be required in a file that includes the
19535 PCH. */
19536 if (pch_file)
19537 add_pending_template (d);
19538 /* Instantiate inline functions so that the inliner can do its
19539 job, even though we'll not be emitting a copy of this
19540 function. */
19541 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19542 goto out;
19543 }
19544
19545 fn_context = decl_function_context (d);
19546 nested = (current_function_decl != NULL_TREE);
19547 if (!fn_context)
19548 push_to_top_level ();
19549 else
19550 {
19551 if (nested)
19552 push_function_context ();
19553 cp_unevaluated_operand = 0;
19554 c_inhibit_evaluation_warnings = 0;
19555 }
19556
19557 /* Mark D as instantiated so that recursive calls to
19558 instantiate_decl do not try to instantiate it again. */
19559 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19560
19561 /* Regenerate the declaration in case the template has been modified
19562 by a subsequent redeclaration. */
19563 regenerate_decl_from_template (d, td);
19564
19565 /* We already set the file and line above. Reset them now in case
19566 they changed as a result of calling regenerate_decl_from_template. */
19567 input_location = DECL_SOURCE_LOCATION (d);
19568
19569 if (VAR_P (d))
19570 {
19571 tree init;
19572 bool const_init = false;
19573
19574 /* Clear out DECL_RTL; whatever was there before may not be right
19575 since we've reset the type of the declaration. */
19576 SET_DECL_RTL (d, NULL);
19577 DECL_IN_AGGR_P (d) = 0;
19578
19579 /* The initializer is placed in DECL_INITIAL by
19580 regenerate_decl_from_template so we don't need to
19581 push/pop_access_scope again here. Pull it out so that
19582 cp_finish_decl can process it. */
19583 init = DECL_INITIAL (d);
19584 DECL_INITIAL (d) = NULL_TREE;
19585 DECL_INITIALIZED_P (d) = 0;
19586
19587 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19588 initializer. That function will defer actual emission until
19589 we have a chance to determine linkage. */
19590 DECL_EXTERNAL (d) = 0;
19591
19592 /* Enter the scope of D so that access-checking works correctly. */
19593 push_nested_class (DECL_CONTEXT (d));
19594 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19595 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19596 pop_nested_class ();
19597 }
19598 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19599 synthesize_method (d);
19600 else if (TREE_CODE (d) == FUNCTION_DECL)
19601 {
19602 struct pointer_map_t *saved_local_specializations;
19603 tree subst_decl;
19604 tree tmpl_parm;
19605 tree spec_parm;
19606 tree block = NULL_TREE;
19607
19608 /* Save away the current list, in case we are instantiating one
19609 template from within the body of another. */
19610 saved_local_specializations = local_specializations;
19611
19612 /* Set up the list of local specializations. */
19613 local_specializations = pointer_map_create ();
19614
19615 /* Set up context. */
19616 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19617 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19618 block = push_stmt_list ();
19619 else
19620 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19621
19622 /* Some typedefs referenced from within the template code need to be
19623 access checked at template instantiation time, i.e now. These
19624 types were added to the template at parsing time. Let's get those
19625 and perform the access checks then. */
19626 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19627 gen_args);
19628
19629 /* Create substitution entries for the parameters. */
19630 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19631 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19632 spec_parm = DECL_ARGUMENTS (d);
19633 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19634 {
19635 register_local_specialization (spec_parm, tmpl_parm);
19636 spec_parm = skip_artificial_parms_for (d, spec_parm);
19637 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19638 }
19639 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19640 {
19641 if (!DECL_PACK_P (tmpl_parm))
19642 {
19643 register_local_specialization (spec_parm, tmpl_parm);
19644 spec_parm = DECL_CHAIN (spec_parm);
19645 }
19646 else
19647 {
19648 /* Register the (value) argument pack as a specialization of
19649 TMPL_PARM, then move on. */
19650 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19651 register_local_specialization (argpack, tmpl_parm);
19652 }
19653 }
19654 gcc_assert (!spec_parm);
19655
19656 /* Substitute into the body of the function. */
19657 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19658 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19659 tf_warning_or_error, tmpl);
19660 else
19661 {
19662 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19663 tf_warning_or_error, tmpl,
19664 /*integral_constant_expression_p=*/false);
19665
19666 /* Set the current input_location to the end of the function
19667 so that finish_function knows where we are. */
19668 input_location
19669 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19670
19671 /* Remember if we saw an infinite loop in the template. */
19672 current_function_infinite_loop
19673 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
19674 }
19675
19676 /* We don't need the local specializations any more. */
19677 pointer_map_destroy (local_specializations);
19678 local_specializations = saved_local_specializations;
19679
19680 /* Finish the function. */
19681 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19682 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19683 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19684 else
19685 {
19686 d = finish_function (0);
19687 expand_or_defer_fn (d);
19688 }
19689
19690 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19691 cp_check_omp_declare_reduction (d);
19692 }
19693
19694 /* We're not deferring instantiation any more. */
19695 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19696
19697 if (!fn_context)
19698 pop_from_top_level ();
19699 else if (nested)
19700 pop_function_context ();
19701
19702 out:
19703 input_location = saved_loc;
19704 cp_unevaluated_operand = saved_unevaluated_operand;
19705 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19706 pop_deferring_access_checks ();
19707 pop_tinst_level ();
19708
19709 timevar_pop (TV_TEMPLATE_INST);
19710
19711 return d;
19712 }
19713
19714 /* Run through the list of templates that we wish we could
19715 instantiate, and instantiate any we can. RETRIES is the
19716 number of times we retry pending template instantiation. */
19717
19718 void
19719 instantiate_pending_templates (int retries)
19720 {
19721 int reconsider;
19722 location_t saved_loc = input_location;
19723
19724 /* Instantiating templates may trigger vtable generation. This in turn
19725 may require further template instantiations. We place a limit here
19726 to avoid infinite loop. */
19727 if (pending_templates && retries >= max_tinst_depth)
19728 {
19729 tree decl = pending_templates->tinst->decl;
19730
19731 error ("template instantiation depth exceeds maximum of %d"
19732 " instantiating %q+D, possibly from virtual table generation"
19733 " (use -ftemplate-depth= to increase the maximum)",
19734 max_tinst_depth, decl);
19735 if (TREE_CODE (decl) == FUNCTION_DECL)
19736 /* Pretend that we defined it. */
19737 DECL_INITIAL (decl) = error_mark_node;
19738 return;
19739 }
19740
19741 do
19742 {
19743 struct pending_template **t = &pending_templates;
19744 struct pending_template *last = NULL;
19745 reconsider = 0;
19746 while (*t)
19747 {
19748 tree instantiation = reopen_tinst_level ((*t)->tinst);
19749 bool complete = false;
19750
19751 if (TYPE_P (instantiation))
19752 {
19753 tree fn;
19754
19755 if (!COMPLETE_TYPE_P (instantiation))
19756 {
19757 instantiate_class_template (instantiation);
19758 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19759 for (fn = TYPE_METHODS (instantiation);
19760 fn;
19761 fn = TREE_CHAIN (fn))
19762 if (! DECL_ARTIFICIAL (fn))
19763 instantiate_decl (fn,
19764 /*defer_ok=*/0,
19765 /*expl_inst_class_mem_p=*/false);
19766 if (COMPLETE_TYPE_P (instantiation))
19767 reconsider = 1;
19768 }
19769
19770 complete = COMPLETE_TYPE_P (instantiation);
19771 }
19772 else
19773 {
19774 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19775 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19776 {
19777 instantiation
19778 = instantiate_decl (instantiation,
19779 /*defer_ok=*/0,
19780 /*expl_inst_class_mem_p=*/false);
19781 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19782 reconsider = 1;
19783 }
19784
19785 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19786 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19787 }
19788
19789 if (complete)
19790 /* If INSTANTIATION has been instantiated, then we don't
19791 need to consider it again in the future. */
19792 *t = (*t)->next;
19793 else
19794 {
19795 last = *t;
19796 t = &(*t)->next;
19797 }
19798 tinst_depth = 0;
19799 current_tinst_level = NULL;
19800 }
19801 last_pending_template = last;
19802 }
19803 while (reconsider);
19804
19805 input_location = saved_loc;
19806 }
19807
19808 /* Substitute ARGVEC into T, which is a list of initializers for
19809 either base class or a non-static data member. The TREE_PURPOSEs
19810 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19811 instantiate_decl. */
19812
19813 static tree
19814 tsubst_initializer_list (tree t, tree argvec)
19815 {
19816 tree inits = NULL_TREE;
19817
19818 for (; t; t = TREE_CHAIN (t))
19819 {
19820 tree decl;
19821 tree init;
19822 tree expanded_bases = NULL_TREE;
19823 tree expanded_arguments = NULL_TREE;
19824 int i, len = 1;
19825
19826 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19827 {
19828 tree expr;
19829 tree arg;
19830
19831 /* Expand the base class expansion type into separate base
19832 classes. */
19833 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19834 tf_warning_or_error,
19835 NULL_TREE);
19836 if (expanded_bases == error_mark_node)
19837 continue;
19838
19839 /* We'll be building separate TREE_LISTs of arguments for
19840 each base. */
19841 len = TREE_VEC_LENGTH (expanded_bases);
19842 expanded_arguments = make_tree_vec (len);
19843 for (i = 0; i < len; i++)
19844 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19845
19846 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19847 expand each argument in the TREE_VALUE of t. */
19848 expr = make_node (EXPR_PACK_EXPANSION);
19849 PACK_EXPANSION_LOCAL_P (expr) = true;
19850 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19851 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19852
19853 if (TREE_VALUE (t) == void_type_node)
19854 /* VOID_TYPE_NODE is used to indicate
19855 value-initialization. */
19856 {
19857 for (i = 0; i < len; i++)
19858 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19859 }
19860 else
19861 {
19862 /* Substitute parameter packs into each argument in the
19863 TREE_LIST. */
19864 in_base_initializer = 1;
19865 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19866 {
19867 tree expanded_exprs;
19868
19869 /* Expand the argument. */
19870 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19871 expanded_exprs
19872 = tsubst_pack_expansion (expr, argvec,
19873 tf_warning_or_error,
19874 NULL_TREE);
19875 if (expanded_exprs == error_mark_node)
19876 continue;
19877
19878 /* Prepend each of the expanded expressions to the
19879 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19880 for (i = 0; i < len; i++)
19881 {
19882 TREE_VEC_ELT (expanded_arguments, i) =
19883 tree_cons (NULL_TREE,
19884 TREE_VEC_ELT (expanded_exprs, i),
19885 TREE_VEC_ELT (expanded_arguments, i));
19886 }
19887 }
19888 in_base_initializer = 0;
19889
19890 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19891 since we built them backwards. */
19892 for (i = 0; i < len; i++)
19893 {
19894 TREE_VEC_ELT (expanded_arguments, i) =
19895 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19896 }
19897 }
19898 }
19899
19900 for (i = 0; i < len; ++i)
19901 {
19902 if (expanded_bases)
19903 {
19904 decl = TREE_VEC_ELT (expanded_bases, i);
19905 decl = expand_member_init (decl);
19906 init = TREE_VEC_ELT (expanded_arguments, i);
19907 }
19908 else
19909 {
19910 tree tmp;
19911 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19912 tf_warning_or_error, NULL_TREE);
19913
19914 decl = expand_member_init (decl);
19915 if (decl && !DECL_P (decl))
19916 in_base_initializer = 1;
19917
19918 init = TREE_VALUE (t);
19919 tmp = init;
19920 if (init != void_type_node)
19921 init = tsubst_expr (init, argvec,
19922 tf_warning_or_error, NULL_TREE,
19923 /*integral_constant_expression_p=*/false);
19924 if (init == NULL_TREE && tmp != NULL_TREE)
19925 /* If we had an initializer but it instantiated to nothing,
19926 value-initialize the object. This will only occur when
19927 the initializer was a pack expansion where the parameter
19928 packs used in that expansion were of length zero. */
19929 init = void_type_node;
19930 in_base_initializer = 0;
19931 }
19932
19933 if (decl)
19934 {
19935 init = build_tree_list (decl, init);
19936 TREE_CHAIN (init) = inits;
19937 inits = init;
19938 }
19939 }
19940 }
19941 return inits;
19942 }
19943
19944 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19945
19946 static void
19947 set_current_access_from_decl (tree decl)
19948 {
19949 if (TREE_PRIVATE (decl))
19950 current_access_specifier = access_private_node;
19951 else if (TREE_PROTECTED (decl))
19952 current_access_specifier = access_protected_node;
19953 else
19954 current_access_specifier = access_public_node;
19955 }
19956
19957 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19958 is the instantiation (which should have been created with
19959 start_enum) and ARGS are the template arguments to use. */
19960
19961 static void
19962 tsubst_enum (tree tag, tree newtag, tree args)
19963 {
19964 tree e;
19965
19966 if (SCOPED_ENUM_P (newtag))
19967 begin_scope (sk_scoped_enum, newtag);
19968
19969 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19970 {
19971 tree value;
19972 tree decl;
19973
19974 decl = TREE_VALUE (e);
19975 /* Note that in a template enum, the TREE_VALUE is the
19976 CONST_DECL, not the corresponding INTEGER_CST. */
19977 value = tsubst_expr (DECL_INITIAL (decl),
19978 args, tf_warning_or_error, NULL_TREE,
19979 /*integral_constant_expression_p=*/true);
19980
19981 /* Give this enumeration constant the correct access. */
19982 set_current_access_from_decl (decl);
19983
19984 /* Actually build the enumerator itself. */
19985 build_enumerator
19986 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19987 }
19988
19989 if (SCOPED_ENUM_P (newtag))
19990 finish_scope ();
19991
19992 finish_enum_value_list (newtag);
19993 finish_enum (newtag);
19994
19995 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19996 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19997 }
19998
19999 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20000 its type -- but without substituting the innermost set of template
20001 arguments. So, innermost set of template parameters will appear in
20002 the type. */
20003
20004 tree
20005 get_mostly_instantiated_function_type (tree decl)
20006 {
20007 tree fn_type;
20008 tree tmpl;
20009 tree targs;
20010 tree tparms;
20011 int parm_depth;
20012
20013 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20014 targs = DECL_TI_ARGS (decl);
20015 tparms = DECL_TEMPLATE_PARMS (tmpl);
20016 parm_depth = TMPL_PARMS_DEPTH (tparms);
20017
20018 /* There should be as many levels of arguments as there are levels
20019 of parameters. */
20020 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20021
20022 fn_type = TREE_TYPE (tmpl);
20023
20024 if (parm_depth == 1)
20025 /* No substitution is necessary. */
20026 ;
20027 else
20028 {
20029 int i;
20030 tree partial_args;
20031
20032 /* Replace the innermost level of the TARGS with NULL_TREEs to
20033 let tsubst know not to substitute for those parameters. */
20034 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20035 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20036 SET_TMPL_ARGS_LEVEL (partial_args, i,
20037 TMPL_ARGS_LEVEL (targs, i));
20038 SET_TMPL_ARGS_LEVEL (partial_args,
20039 TMPL_ARGS_DEPTH (targs),
20040 make_tree_vec (DECL_NTPARMS (tmpl)));
20041
20042 /* Make sure that we can see identifiers, and compute access
20043 correctly. */
20044 push_access_scope (decl);
20045
20046 ++processing_template_decl;
20047 /* Now, do the (partial) substitution to figure out the
20048 appropriate function type. */
20049 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20050 --processing_template_decl;
20051
20052 /* Substitute into the template parameters to obtain the real
20053 innermost set of parameters. This step is important if the
20054 innermost set of template parameters contains value
20055 parameters whose types depend on outer template parameters. */
20056 TREE_VEC_LENGTH (partial_args)--;
20057 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20058
20059 pop_access_scope (decl);
20060 }
20061
20062 return fn_type;
20063 }
20064
20065 /* Return truthvalue if we're processing a template different from
20066 the last one involved in diagnostics. */
20067 int
20068 problematic_instantiation_changed (void)
20069 {
20070 return current_tinst_level != last_error_tinst_level;
20071 }
20072
20073 /* Remember current template involved in diagnostics. */
20074 void
20075 record_last_problematic_instantiation (void)
20076 {
20077 last_error_tinst_level = current_tinst_level;
20078 }
20079
20080 struct tinst_level *
20081 current_instantiation (void)
20082 {
20083 return current_tinst_level;
20084 }
20085
20086 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20087 type. Return zero for ok, nonzero for disallowed. Issue error and
20088 warning messages under control of COMPLAIN. */
20089
20090 static int
20091 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20092 {
20093 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20094 return 0;
20095 else if (POINTER_TYPE_P (type))
20096 return 0;
20097 else if (TYPE_PTRMEM_P (type))
20098 return 0;
20099 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20100 return 0;
20101 else if (TREE_CODE (type) == TYPENAME_TYPE)
20102 return 0;
20103 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20104 return 0;
20105 else if (TREE_CODE (type) == NULLPTR_TYPE)
20106 return 0;
20107
20108 if (complain & tf_error)
20109 {
20110 if (type == error_mark_node)
20111 inform (input_location, "invalid template non-type parameter");
20112 else
20113 error ("%q#T is not a valid type for a template non-type parameter",
20114 type);
20115 }
20116 return 1;
20117 }
20118
20119 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20120 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20121
20122 static bool
20123 dependent_type_p_r (tree type)
20124 {
20125 tree scope;
20126
20127 /* [temp.dep.type]
20128
20129 A type is dependent if it is:
20130
20131 -- a template parameter. Template template parameters are types
20132 for us (since TYPE_P holds true for them) so we handle
20133 them here. */
20134 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20135 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20136 return true;
20137 /* -- a qualified-id with a nested-name-specifier which contains a
20138 class-name that names a dependent type or whose unqualified-id
20139 names a dependent type. */
20140 if (TREE_CODE (type) == TYPENAME_TYPE)
20141 return true;
20142 /* -- a cv-qualified type where the cv-unqualified type is
20143 dependent. */
20144 type = TYPE_MAIN_VARIANT (type);
20145 /* -- a compound type constructed from any dependent type. */
20146 if (TYPE_PTRMEM_P (type))
20147 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20148 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20149 (type)));
20150 else if (TYPE_PTR_P (type)
20151 || TREE_CODE (type) == REFERENCE_TYPE)
20152 return dependent_type_p (TREE_TYPE (type));
20153 else if (TREE_CODE (type) == FUNCTION_TYPE
20154 || TREE_CODE (type) == METHOD_TYPE)
20155 {
20156 tree arg_type;
20157
20158 if (dependent_type_p (TREE_TYPE (type)))
20159 return true;
20160 for (arg_type = TYPE_ARG_TYPES (type);
20161 arg_type;
20162 arg_type = TREE_CHAIN (arg_type))
20163 if (dependent_type_p (TREE_VALUE (arg_type)))
20164 return true;
20165 return false;
20166 }
20167 /* -- an array type constructed from any dependent type or whose
20168 size is specified by a constant expression that is
20169 value-dependent.
20170
20171 We checked for type- and value-dependence of the bounds in
20172 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20173 if (TREE_CODE (type) == ARRAY_TYPE)
20174 {
20175 if (TYPE_DOMAIN (type)
20176 && dependent_type_p (TYPE_DOMAIN (type)))
20177 return true;
20178 return dependent_type_p (TREE_TYPE (type));
20179 }
20180
20181 /* -- a template-id in which either the template name is a template
20182 parameter ... */
20183 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20184 return true;
20185 /* ... or any of the template arguments is a dependent type or
20186 an expression that is type-dependent or value-dependent. */
20187 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20188 && (any_dependent_template_arguments_p
20189 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20190 return true;
20191
20192 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20193 dependent; if the argument of the `typeof' expression is not
20194 type-dependent, then it should already been have resolved. */
20195 if (TREE_CODE (type) == TYPEOF_TYPE
20196 || TREE_CODE (type) == DECLTYPE_TYPE
20197 || TREE_CODE (type) == UNDERLYING_TYPE)
20198 return true;
20199
20200 /* A template argument pack is dependent if any of its packed
20201 arguments are. */
20202 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20203 {
20204 tree args = ARGUMENT_PACK_ARGS (type);
20205 int i, len = TREE_VEC_LENGTH (args);
20206 for (i = 0; i < len; ++i)
20207 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20208 return true;
20209 }
20210
20211 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20212 be template parameters. */
20213 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20214 return true;
20215
20216 /* The standard does not specifically mention types that are local
20217 to template functions or local classes, but they should be
20218 considered dependent too. For example:
20219
20220 template <int I> void f() {
20221 enum E { a = I };
20222 S<sizeof (E)> s;
20223 }
20224
20225 The size of `E' cannot be known until the value of `I' has been
20226 determined. Therefore, `E' must be considered dependent. */
20227 scope = TYPE_CONTEXT (type);
20228 if (scope && TYPE_P (scope))
20229 return dependent_type_p (scope);
20230 /* Don't use type_dependent_expression_p here, as it can lead
20231 to infinite recursion trying to determine whether a lambda
20232 nested in a lambda is dependent (c++/47687). */
20233 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20234 && DECL_LANG_SPECIFIC (scope)
20235 && DECL_TEMPLATE_INFO (scope)
20236 && (any_dependent_template_arguments_p
20237 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20238 return true;
20239
20240 /* Other types are non-dependent. */
20241 return false;
20242 }
20243
20244 /* Returns TRUE if TYPE is dependent, in the sense of
20245 [temp.dep.type]. Note that a NULL type is considered dependent. */
20246
20247 bool
20248 dependent_type_p (tree type)
20249 {
20250 /* If there are no template parameters in scope, then there can't be
20251 any dependent types. */
20252 if (!processing_template_decl)
20253 {
20254 /* If we are not processing a template, then nobody should be
20255 providing us with a dependent type. */
20256 gcc_assert (type);
20257 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20258 return false;
20259 }
20260
20261 /* If the type is NULL, we have not computed a type for the entity
20262 in question; in that case, the type is dependent. */
20263 if (!type)
20264 return true;
20265
20266 /* Erroneous types can be considered non-dependent. */
20267 if (type == error_mark_node)
20268 return false;
20269
20270 /* If we have not already computed the appropriate value for TYPE,
20271 do so now. */
20272 if (!TYPE_DEPENDENT_P_VALID (type))
20273 {
20274 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20275 TYPE_DEPENDENT_P_VALID (type) = 1;
20276 }
20277
20278 return TYPE_DEPENDENT_P (type);
20279 }
20280
20281 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20282 lookup. In other words, a dependent type that is not the current
20283 instantiation. */
20284
20285 bool
20286 dependent_scope_p (tree scope)
20287 {
20288 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20289 && !currently_open_class (scope));
20290 }
20291
20292 /* T is a SCOPE_REF; return whether we need to consider it
20293 instantiation-dependent so that we can check access at instantiation
20294 time even though we know which member it resolves to. */
20295
20296 static bool
20297 instantiation_dependent_scope_ref_p (tree t)
20298 {
20299 if (DECL_P (TREE_OPERAND (t, 1))
20300 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20301 && accessible_in_template_p (TREE_OPERAND (t, 0),
20302 TREE_OPERAND (t, 1)))
20303 return false;
20304 else
20305 return true;
20306 }
20307
20308 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20309 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20310 expression. */
20311
20312 /* Note that this predicate is not appropriate for general expressions;
20313 only constant expressions (that satisfy potential_constant_expression)
20314 can be tested for value dependence. */
20315
20316 bool
20317 value_dependent_expression_p (tree expression)
20318 {
20319 if (!processing_template_decl)
20320 return false;
20321
20322 /* A name declared with a dependent type. */
20323 if (DECL_P (expression) && type_dependent_expression_p (expression))
20324 return true;
20325
20326 switch (TREE_CODE (expression))
20327 {
20328 case IDENTIFIER_NODE:
20329 /* A name that has not been looked up -- must be dependent. */
20330 return true;
20331
20332 case TEMPLATE_PARM_INDEX:
20333 /* A non-type template parm. */
20334 return true;
20335
20336 case CONST_DECL:
20337 /* A non-type template parm. */
20338 if (DECL_TEMPLATE_PARM_P (expression))
20339 return true;
20340 return value_dependent_expression_p (DECL_INITIAL (expression));
20341
20342 case VAR_DECL:
20343 /* A constant with literal type and is initialized
20344 with an expression that is value-dependent.
20345
20346 Note that a non-dependent parenthesized initializer will have
20347 already been replaced with its constant value, so if we see
20348 a TREE_LIST it must be dependent. */
20349 if (DECL_INITIAL (expression)
20350 && decl_constant_var_p (expression)
20351 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20352 || value_dependent_expression_p (DECL_INITIAL (expression))))
20353 return true;
20354 return false;
20355
20356 case DYNAMIC_CAST_EXPR:
20357 case STATIC_CAST_EXPR:
20358 case CONST_CAST_EXPR:
20359 case REINTERPRET_CAST_EXPR:
20360 case CAST_EXPR:
20361 /* These expressions are value-dependent if the type to which
20362 the cast occurs is dependent or the expression being casted
20363 is value-dependent. */
20364 {
20365 tree type = TREE_TYPE (expression);
20366
20367 if (dependent_type_p (type))
20368 return true;
20369
20370 /* A functional cast has a list of operands. */
20371 expression = TREE_OPERAND (expression, 0);
20372 if (!expression)
20373 {
20374 /* If there are no operands, it must be an expression such
20375 as "int()". This should not happen for aggregate types
20376 because it would form non-constant expressions. */
20377 gcc_assert (cxx_dialect >= cxx11
20378 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20379
20380 return false;
20381 }
20382
20383 if (TREE_CODE (expression) == TREE_LIST)
20384 return any_value_dependent_elements_p (expression);
20385
20386 return value_dependent_expression_p (expression);
20387 }
20388
20389 case SIZEOF_EXPR:
20390 if (SIZEOF_EXPR_TYPE_P (expression))
20391 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20392 /* FALLTHRU */
20393 case ALIGNOF_EXPR:
20394 case TYPEID_EXPR:
20395 /* A `sizeof' expression is value-dependent if the operand is
20396 type-dependent or is a pack expansion. */
20397 expression = TREE_OPERAND (expression, 0);
20398 if (PACK_EXPANSION_P (expression))
20399 return true;
20400 else if (TYPE_P (expression))
20401 return dependent_type_p (expression);
20402 return instantiation_dependent_expression_p (expression);
20403
20404 case AT_ENCODE_EXPR:
20405 /* An 'encode' expression is value-dependent if the operand is
20406 type-dependent. */
20407 expression = TREE_OPERAND (expression, 0);
20408 return dependent_type_p (expression);
20409
20410 case NOEXCEPT_EXPR:
20411 expression = TREE_OPERAND (expression, 0);
20412 return instantiation_dependent_expression_p (expression);
20413
20414 case SCOPE_REF:
20415 /* All instantiation-dependent expressions should also be considered
20416 value-dependent. */
20417 return instantiation_dependent_scope_ref_p (expression);
20418
20419 case COMPONENT_REF:
20420 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20421 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20422
20423 case NONTYPE_ARGUMENT_PACK:
20424 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20425 is value-dependent. */
20426 {
20427 tree values = ARGUMENT_PACK_ARGS (expression);
20428 int i, len = TREE_VEC_LENGTH (values);
20429
20430 for (i = 0; i < len; ++i)
20431 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20432 return true;
20433
20434 return false;
20435 }
20436
20437 case TRAIT_EXPR:
20438 {
20439 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20440 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20441 || (type2 ? dependent_type_p (type2) : false));
20442 }
20443
20444 case MODOP_EXPR:
20445 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20446 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20447
20448 case ARRAY_REF:
20449 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20450 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20451
20452 case ADDR_EXPR:
20453 {
20454 tree op = TREE_OPERAND (expression, 0);
20455 return (value_dependent_expression_p (op)
20456 || has_value_dependent_address (op));
20457 }
20458
20459 case CALL_EXPR:
20460 {
20461 tree fn = get_callee_fndecl (expression);
20462 int i, nargs;
20463 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20464 return true;
20465 nargs = call_expr_nargs (expression);
20466 for (i = 0; i < nargs; ++i)
20467 {
20468 tree op = CALL_EXPR_ARG (expression, i);
20469 /* In a call to a constexpr member function, look through the
20470 implicit ADDR_EXPR on the object argument so that it doesn't
20471 cause the call to be considered value-dependent. We also
20472 look through it in potential_constant_expression. */
20473 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20474 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20475 && TREE_CODE (op) == ADDR_EXPR)
20476 op = TREE_OPERAND (op, 0);
20477 if (value_dependent_expression_p (op))
20478 return true;
20479 }
20480 return false;
20481 }
20482
20483 case TEMPLATE_ID_EXPR:
20484 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20485 type-dependent. */
20486 return type_dependent_expression_p (expression);
20487
20488 case CONSTRUCTOR:
20489 {
20490 unsigned ix;
20491 tree val;
20492 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20493 if (value_dependent_expression_p (val))
20494 return true;
20495 return false;
20496 }
20497
20498 case STMT_EXPR:
20499 /* Treat a GNU statement expression as dependent to avoid crashing
20500 under fold_non_dependent_expr; it can't be constant. */
20501 return true;
20502
20503 default:
20504 /* A constant expression is value-dependent if any subexpression is
20505 value-dependent. */
20506 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20507 {
20508 case tcc_reference:
20509 case tcc_unary:
20510 case tcc_comparison:
20511 case tcc_binary:
20512 case tcc_expression:
20513 case tcc_vl_exp:
20514 {
20515 int i, len = cp_tree_operand_length (expression);
20516
20517 for (i = 0; i < len; i++)
20518 {
20519 tree t = TREE_OPERAND (expression, i);
20520
20521 /* In some cases, some of the operands may be missing.l
20522 (For example, in the case of PREDECREMENT_EXPR, the
20523 amount to increment by may be missing.) That doesn't
20524 make the expression dependent. */
20525 if (t && value_dependent_expression_p (t))
20526 return true;
20527 }
20528 }
20529 break;
20530 default:
20531 break;
20532 }
20533 break;
20534 }
20535
20536 /* The expression is not value-dependent. */
20537 return false;
20538 }
20539
20540 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20541 [temp.dep.expr]. Note that an expression with no type is
20542 considered dependent. Other parts of the compiler arrange for an
20543 expression with type-dependent subexpressions to have no type, so
20544 this function doesn't have to be fully recursive. */
20545
20546 bool
20547 type_dependent_expression_p (tree expression)
20548 {
20549 if (!processing_template_decl)
20550 return false;
20551
20552 if (expression == NULL_TREE || expression == error_mark_node)
20553 return false;
20554
20555 /* An unresolved name is always dependent. */
20556 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20557 return true;
20558
20559 /* Some expression forms are never type-dependent. */
20560 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20561 || TREE_CODE (expression) == SIZEOF_EXPR
20562 || TREE_CODE (expression) == ALIGNOF_EXPR
20563 || TREE_CODE (expression) == AT_ENCODE_EXPR
20564 || TREE_CODE (expression) == NOEXCEPT_EXPR
20565 || TREE_CODE (expression) == TRAIT_EXPR
20566 || TREE_CODE (expression) == TYPEID_EXPR
20567 || TREE_CODE (expression) == DELETE_EXPR
20568 || TREE_CODE (expression) == VEC_DELETE_EXPR
20569 || TREE_CODE (expression) == THROW_EXPR)
20570 return false;
20571
20572 /* The types of these expressions depends only on the type to which
20573 the cast occurs. */
20574 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20575 || TREE_CODE (expression) == STATIC_CAST_EXPR
20576 || TREE_CODE (expression) == CONST_CAST_EXPR
20577 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20578 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20579 || TREE_CODE (expression) == CAST_EXPR)
20580 return dependent_type_p (TREE_TYPE (expression));
20581
20582 /* The types of these expressions depends only on the type created
20583 by the expression. */
20584 if (TREE_CODE (expression) == NEW_EXPR
20585 || TREE_CODE (expression) == VEC_NEW_EXPR)
20586 {
20587 /* For NEW_EXPR tree nodes created inside a template, either
20588 the object type itself or a TREE_LIST may appear as the
20589 operand 1. */
20590 tree type = TREE_OPERAND (expression, 1);
20591 if (TREE_CODE (type) == TREE_LIST)
20592 /* This is an array type. We need to check array dimensions
20593 as well. */
20594 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20595 || value_dependent_expression_p
20596 (TREE_OPERAND (TREE_VALUE (type), 1));
20597 else
20598 return dependent_type_p (type);
20599 }
20600
20601 if (TREE_CODE (expression) == SCOPE_REF)
20602 {
20603 tree scope = TREE_OPERAND (expression, 0);
20604 tree name = TREE_OPERAND (expression, 1);
20605
20606 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20607 contains an identifier associated by name lookup with one or more
20608 declarations declared with a dependent type, or...a
20609 nested-name-specifier or qualified-id that names a member of an
20610 unknown specialization. */
20611 return (type_dependent_expression_p (name)
20612 || dependent_scope_p (scope));
20613 }
20614
20615 if (TREE_CODE (expression) == FUNCTION_DECL
20616 && DECL_LANG_SPECIFIC (expression)
20617 && DECL_TEMPLATE_INFO (expression)
20618 && (any_dependent_template_arguments_p
20619 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20620 return true;
20621
20622 if (TREE_CODE (expression) == TEMPLATE_DECL
20623 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20624 return false;
20625
20626 if (TREE_CODE (expression) == STMT_EXPR)
20627 expression = stmt_expr_value_expr (expression);
20628
20629 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20630 {
20631 tree elt;
20632 unsigned i;
20633
20634 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20635 {
20636 if (type_dependent_expression_p (elt))
20637 return true;
20638 }
20639 return false;
20640 }
20641
20642 /* A static data member of the current instantiation with incomplete
20643 array type is type-dependent, as the definition and specializations
20644 can have different bounds. */
20645 if (VAR_P (expression)
20646 && DECL_CLASS_SCOPE_P (expression)
20647 && dependent_type_p (DECL_CONTEXT (expression))
20648 && VAR_HAD_UNKNOWN_BOUND (expression))
20649 return true;
20650
20651 /* An array of unknown bound depending on a variadic parameter, eg:
20652
20653 template<typename... Args>
20654 void foo (Args... args)
20655 {
20656 int arr[] = { args... };
20657 }
20658
20659 template<int... vals>
20660 void bar ()
20661 {
20662 int arr[] = { vals... };
20663 }
20664
20665 If the array has no length and has an initializer, it must be that
20666 we couldn't determine its length in cp_complete_array_type because
20667 it is dependent. */
20668 if (VAR_P (expression)
20669 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20670 && !TYPE_DOMAIN (TREE_TYPE (expression))
20671 && DECL_INITIAL (expression))
20672 return true;
20673
20674 if (TREE_TYPE (expression) == unknown_type_node)
20675 {
20676 if (TREE_CODE (expression) == ADDR_EXPR)
20677 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20678 if (TREE_CODE (expression) == COMPONENT_REF
20679 || TREE_CODE (expression) == OFFSET_REF)
20680 {
20681 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20682 return true;
20683 expression = TREE_OPERAND (expression, 1);
20684 if (identifier_p (expression))
20685 return false;
20686 }
20687 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20688 if (TREE_CODE (expression) == SCOPE_REF)
20689 return false;
20690
20691 /* Always dependent, on the number of arguments if nothing else. */
20692 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20693 return true;
20694
20695 if (BASELINK_P (expression))
20696 expression = BASELINK_FUNCTIONS (expression);
20697
20698 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20699 {
20700 if (any_dependent_template_arguments_p
20701 (TREE_OPERAND (expression, 1)))
20702 return true;
20703 expression = TREE_OPERAND (expression, 0);
20704 }
20705 gcc_assert (TREE_CODE (expression) == OVERLOAD
20706 || TREE_CODE (expression) == FUNCTION_DECL);
20707
20708 while (expression)
20709 {
20710 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20711 return true;
20712 expression = OVL_NEXT (expression);
20713 }
20714 return false;
20715 }
20716
20717 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20718
20719 return (dependent_type_p (TREE_TYPE (expression)));
20720 }
20721
20722 /* walk_tree callback function for instantiation_dependent_expression_p,
20723 below. Returns non-zero if a dependent subexpression is found. */
20724
20725 static tree
20726 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20727 void * /*data*/)
20728 {
20729 if (TYPE_P (*tp))
20730 {
20731 /* We don't have to worry about decltype currently because decltype
20732 of an instantiation-dependent expr is a dependent type. This
20733 might change depending on the resolution of DR 1172. */
20734 *walk_subtrees = false;
20735 return NULL_TREE;
20736 }
20737 enum tree_code code = TREE_CODE (*tp);
20738 switch (code)
20739 {
20740 /* Don't treat an argument list as dependent just because it has no
20741 TREE_TYPE. */
20742 case TREE_LIST:
20743 case TREE_VEC:
20744 return NULL_TREE;
20745
20746 case VAR_DECL:
20747 case CONST_DECL:
20748 /* A constant with a dependent initializer is dependent. */
20749 if (value_dependent_expression_p (*tp))
20750 return *tp;
20751 break;
20752
20753 case TEMPLATE_PARM_INDEX:
20754 return *tp;
20755
20756 /* Handle expressions with type operands. */
20757 case SIZEOF_EXPR:
20758 case ALIGNOF_EXPR:
20759 case TYPEID_EXPR:
20760 case AT_ENCODE_EXPR:
20761 {
20762 tree op = TREE_OPERAND (*tp, 0);
20763 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20764 op = TREE_TYPE (op);
20765 if (TYPE_P (op))
20766 {
20767 if (dependent_type_p (op))
20768 return *tp;
20769 else
20770 {
20771 *walk_subtrees = false;
20772 return NULL_TREE;
20773 }
20774 }
20775 break;
20776 }
20777
20778 case TRAIT_EXPR:
20779 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20780 || (TRAIT_EXPR_TYPE2 (*tp)
20781 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20782 return *tp;
20783 *walk_subtrees = false;
20784 return NULL_TREE;
20785
20786 case COMPONENT_REF:
20787 if (identifier_p (TREE_OPERAND (*tp, 1)))
20788 /* In a template, finish_class_member_access_expr creates a
20789 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20790 type-dependent, so that we can check access control at
20791 instantiation time (PR 42277). See also Core issue 1273. */
20792 return *tp;
20793 break;
20794
20795 case SCOPE_REF:
20796 if (instantiation_dependent_scope_ref_p (*tp))
20797 return *tp;
20798 else
20799 break;
20800
20801 /* Treat statement-expressions as dependent. */
20802 case BIND_EXPR:
20803 return *tp;
20804
20805 default:
20806 break;
20807 }
20808
20809 if (type_dependent_expression_p (*tp))
20810 return *tp;
20811 else
20812 return NULL_TREE;
20813 }
20814
20815 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20816 sense defined by the ABI:
20817
20818 "An expression is instantiation-dependent if it is type-dependent
20819 or value-dependent, or it has a subexpression that is type-dependent
20820 or value-dependent." */
20821
20822 bool
20823 instantiation_dependent_expression_p (tree expression)
20824 {
20825 tree result;
20826
20827 if (!processing_template_decl)
20828 return false;
20829
20830 if (expression == error_mark_node)
20831 return false;
20832
20833 result = cp_walk_tree_without_duplicates (&expression,
20834 instantiation_dependent_r, NULL);
20835 return result != NULL_TREE;
20836 }
20837
20838 /* Like type_dependent_expression_p, but it also works while not processing
20839 a template definition, i.e. during substitution or mangling. */
20840
20841 bool
20842 type_dependent_expression_p_push (tree expr)
20843 {
20844 bool b;
20845 ++processing_template_decl;
20846 b = type_dependent_expression_p (expr);
20847 --processing_template_decl;
20848 return b;
20849 }
20850
20851 /* Returns TRUE if ARGS contains a type-dependent expression. */
20852
20853 bool
20854 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20855 {
20856 unsigned int i;
20857 tree arg;
20858
20859 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20860 {
20861 if (type_dependent_expression_p (arg))
20862 return true;
20863 }
20864 return false;
20865 }
20866
20867 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20868 expressions) contains any type-dependent expressions. */
20869
20870 bool
20871 any_type_dependent_elements_p (const_tree list)
20872 {
20873 for (; list; list = TREE_CHAIN (list))
20874 if (type_dependent_expression_p (TREE_VALUE (list)))
20875 return true;
20876
20877 return false;
20878 }
20879
20880 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20881 expressions) contains any value-dependent expressions. */
20882
20883 bool
20884 any_value_dependent_elements_p (const_tree list)
20885 {
20886 for (; list; list = TREE_CHAIN (list))
20887 if (value_dependent_expression_p (TREE_VALUE (list)))
20888 return true;
20889
20890 return false;
20891 }
20892
20893 /* Returns TRUE if the ARG (a template argument) is dependent. */
20894
20895 bool
20896 dependent_template_arg_p (tree arg)
20897 {
20898 if (!processing_template_decl)
20899 return false;
20900
20901 /* Assume a template argument that was wrongly written by the user
20902 is dependent. This is consistent with what
20903 any_dependent_template_arguments_p [that calls this function]
20904 does. */
20905 if (!arg || arg == error_mark_node)
20906 return true;
20907
20908 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20909 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20910
20911 if (TREE_CODE (arg) == TEMPLATE_DECL
20912 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20913 return dependent_template_p (arg);
20914 else if (ARGUMENT_PACK_P (arg))
20915 {
20916 tree args = ARGUMENT_PACK_ARGS (arg);
20917 int i, len = TREE_VEC_LENGTH (args);
20918 for (i = 0; i < len; ++i)
20919 {
20920 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20921 return true;
20922 }
20923
20924 return false;
20925 }
20926 else if (TYPE_P (arg))
20927 return dependent_type_p (arg);
20928 else
20929 return (type_dependent_expression_p (arg)
20930 || value_dependent_expression_p (arg));
20931 }
20932
20933 /* Returns true if ARGS (a collection of template arguments) contains
20934 any types that require structural equality testing. */
20935
20936 bool
20937 any_template_arguments_need_structural_equality_p (tree args)
20938 {
20939 int i;
20940 int j;
20941
20942 if (!args)
20943 return false;
20944 if (args == error_mark_node)
20945 return true;
20946
20947 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20948 {
20949 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20950 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20951 {
20952 tree arg = TREE_VEC_ELT (level, j);
20953 tree packed_args = NULL_TREE;
20954 int k, len = 1;
20955
20956 if (ARGUMENT_PACK_P (arg))
20957 {
20958 /* Look inside the argument pack. */
20959 packed_args = ARGUMENT_PACK_ARGS (arg);
20960 len = TREE_VEC_LENGTH (packed_args);
20961 }
20962
20963 for (k = 0; k < len; ++k)
20964 {
20965 if (packed_args)
20966 arg = TREE_VEC_ELT (packed_args, k);
20967
20968 if (error_operand_p (arg))
20969 return true;
20970 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20971 continue;
20972 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20973 return true;
20974 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20975 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20976 return true;
20977 }
20978 }
20979 }
20980
20981 return false;
20982 }
20983
20984 /* Returns true if ARGS (a collection of template arguments) contains
20985 any dependent arguments. */
20986
20987 bool
20988 any_dependent_template_arguments_p (const_tree args)
20989 {
20990 int i;
20991 int j;
20992
20993 if (!args)
20994 return false;
20995 if (args == error_mark_node)
20996 return true;
20997
20998 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20999 {
21000 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21001 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21002 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21003 return true;
21004 }
21005
21006 return false;
21007 }
21008
21009 /* Returns TRUE if the template TMPL is dependent. */
21010
21011 bool
21012 dependent_template_p (tree tmpl)
21013 {
21014 if (TREE_CODE (tmpl) == OVERLOAD)
21015 {
21016 while (tmpl)
21017 {
21018 if (dependent_template_p (OVL_CURRENT (tmpl)))
21019 return true;
21020 tmpl = OVL_NEXT (tmpl);
21021 }
21022 return false;
21023 }
21024
21025 /* Template template parameters are dependent. */
21026 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21027 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21028 return true;
21029 /* So are names that have not been looked up. */
21030 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21031 return true;
21032 /* So are member templates of dependent classes. */
21033 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21034 return dependent_type_p (DECL_CONTEXT (tmpl));
21035 return false;
21036 }
21037
21038 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21039
21040 bool
21041 dependent_template_id_p (tree tmpl, tree args)
21042 {
21043 return (dependent_template_p (tmpl)
21044 || any_dependent_template_arguments_p (args));
21045 }
21046
21047 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21048 is dependent. */
21049
21050 bool
21051 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21052 {
21053 int i;
21054
21055 if (!processing_template_decl)
21056 return false;
21057
21058 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21059 {
21060 tree decl = TREE_VEC_ELT (declv, i);
21061 tree init = TREE_VEC_ELT (initv, i);
21062 tree cond = TREE_VEC_ELT (condv, i);
21063 tree incr = TREE_VEC_ELT (incrv, i);
21064
21065 if (type_dependent_expression_p (decl))
21066 return true;
21067
21068 if (init && type_dependent_expression_p (init))
21069 return true;
21070
21071 if (type_dependent_expression_p (cond))
21072 return true;
21073
21074 if (COMPARISON_CLASS_P (cond)
21075 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21076 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21077 return true;
21078
21079 if (TREE_CODE (incr) == MODOP_EXPR)
21080 {
21081 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21082 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21083 return true;
21084 }
21085 else if (type_dependent_expression_p (incr))
21086 return true;
21087 else if (TREE_CODE (incr) == MODIFY_EXPR)
21088 {
21089 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21090 return true;
21091 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21092 {
21093 tree t = TREE_OPERAND (incr, 1);
21094 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21095 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21096 return true;
21097 }
21098 }
21099 }
21100
21101 return false;
21102 }
21103
21104 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21105 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21106 no such TYPE can be found. Note that this function peers inside
21107 uninstantiated templates and therefore should be used only in
21108 extremely limited situations. ONLY_CURRENT_P restricts this
21109 peering to the currently open classes hierarchy (which is required
21110 when comparing types). */
21111
21112 tree
21113 resolve_typename_type (tree type, bool only_current_p)
21114 {
21115 tree scope;
21116 tree name;
21117 tree decl;
21118 int quals;
21119 tree pushed_scope;
21120 tree result;
21121
21122 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21123
21124 scope = TYPE_CONTEXT (type);
21125 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21126 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21127 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21128 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21129 identifier of the TYPENAME_TYPE anymore.
21130 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21131 TYPENAME_TYPE instead, we avoid messing up with a possible
21132 typedef variant case. */
21133 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21134
21135 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21136 it first before we can figure out what NAME refers to. */
21137 if (TREE_CODE (scope) == TYPENAME_TYPE)
21138 {
21139 if (TYPENAME_IS_RESOLVING_P (scope))
21140 /* Given a class template A with a dependent base with nested type C,
21141 typedef typename A::C::C C will land us here, as trying to resolve
21142 the initial A::C leads to the local C typedef, which leads back to
21143 A::C::C. So we break the recursion now. */
21144 return type;
21145 else
21146 scope = resolve_typename_type (scope, only_current_p);
21147 }
21148 /* If we don't know what SCOPE refers to, then we cannot resolve the
21149 TYPENAME_TYPE. */
21150 if (TREE_CODE (scope) == TYPENAME_TYPE)
21151 return type;
21152 /* If the SCOPE is a template type parameter, we have no way of
21153 resolving the name. */
21154 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21155 return type;
21156 /* If the SCOPE is not the current instantiation, there's no reason
21157 to look inside it. */
21158 if (only_current_p && !currently_open_class (scope))
21159 return type;
21160 /* If this is a typedef, we don't want to look inside (c++/11987). */
21161 if (typedef_variant_p (type))
21162 return type;
21163 /* If SCOPE isn't the template itself, it will not have a valid
21164 TYPE_FIELDS list. */
21165 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21166 /* scope is either the template itself or a compatible instantiation
21167 like X<T>, so look up the name in the original template. */
21168 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21169 else
21170 /* scope is a partial instantiation, so we can't do the lookup or we
21171 will lose the template arguments. */
21172 return type;
21173 /* Enter the SCOPE so that name lookup will be resolved as if we
21174 were in the class definition. In particular, SCOPE will no
21175 longer be considered a dependent type. */
21176 pushed_scope = push_scope (scope);
21177 /* Look up the declaration. */
21178 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21179 tf_warning_or_error);
21180
21181 result = NULL_TREE;
21182
21183 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21184 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21185 if (!decl)
21186 /*nop*/;
21187 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21188 && TREE_CODE (decl) == TYPE_DECL)
21189 {
21190 result = TREE_TYPE (decl);
21191 if (result == error_mark_node)
21192 result = NULL_TREE;
21193 }
21194 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21195 && DECL_CLASS_TEMPLATE_P (decl))
21196 {
21197 tree tmpl;
21198 tree args;
21199 /* Obtain the template and the arguments. */
21200 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21201 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21202 /* Instantiate the template. */
21203 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21204 /*entering_scope=*/0,
21205 tf_error | tf_user);
21206 if (result == error_mark_node)
21207 result = NULL_TREE;
21208 }
21209
21210 /* Leave the SCOPE. */
21211 if (pushed_scope)
21212 pop_scope (pushed_scope);
21213
21214 /* If we failed to resolve it, return the original typename. */
21215 if (!result)
21216 return type;
21217
21218 /* If lookup found a typename type, resolve that too. */
21219 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21220 {
21221 /* Ill-formed programs can cause infinite recursion here, so we
21222 must catch that. */
21223 TYPENAME_IS_RESOLVING_P (type) = 1;
21224 result = resolve_typename_type (result, only_current_p);
21225 TYPENAME_IS_RESOLVING_P (type) = 0;
21226 }
21227
21228 /* Qualify the resulting type. */
21229 quals = cp_type_quals (type);
21230 if (quals)
21231 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21232
21233 return result;
21234 }
21235
21236 /* EXPR is an expression which is not type-dependent. Return a proxy
21237 for EXPR that can be used to compute the types of larger
21238 expressions containing EXPR. */
21239
21240 tree
21241 build_non_dependent_expr (tree expr)
21242 {
21243 tree inner_expr;
21244
21245 #ifdef ENABLE_CHECKING
21246 /* Try to get a constant value for all non-dependent expressions in
21247 order to expose bugs in *_dependent_expression_p and constexpr. */
21248 if (cxx_dialect >= cxx11)
21249 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21250 #endif
21251
21252 /* Preserve OVERLOADs; the functions must be available to resolve
21253 types. */
21254 inner_expr = expr;
21255 if (TREE_CODE (inner_expr) == STMT_EXPR)
21256 inner_expr = stmt_expr_value_expr (inner_expr);
21257 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21258 inner_expr = TREE_OPERAND (inner_expr, 0);
21259 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21260 inner_expr = TREE_OPERAND (inner_expr, 1);
21261 if (is_overloaded_fn (inner_expr)
21262 || TREE_CODE (inner_expr) == OFFSET_REF)
21263 return expr;
21264 /* There is no need to return a proxy for a variable. */
21265 if (VAR_P (expr))
21266 return expr;
21267 /* Preserve string constants; conversions from string constants to
21268 "char *" are allowed, even though normally a "const char *"
21269 cannot be used to initialize a "char *". */
21270 if (TREE_CODE (expr) == STRING_CST)
21271 return expr;
21272 /* Preserve arithmetic constants, as an optimization -- there is no
21273 reason to create a new node. */
21274 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21275 return expr;
21276 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21277 There is at least one place where we want to know that a
21278 particular expression is a throw-expression: when checking a ?:
21279 expression, there are special rules if the second or third
21280 argument is a throw-expression. */
21281 if (TREE_CODE (expr) == THROW_EXPR)
21282 return expr;
21283
21284 /* Don't wrap an initializer list, we need to be able to look inside. */
21285 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21286 return expr;
21287
21288 /* Don't wrap a dummy object, we need to be able to test for it. */
21289 if (is_dummy_object (expr))
21290 return expr;
21291
21292 if (TREE_CODE (expr) == COND_EXPR)
21293 return build3 (COND_EXPR,
21294 TREE_TYPE (expr),
21295 TREE_OPERAND (expr, 0),
21296 (TREE_OPERAND (expr, 1)
21297 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21298 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21299 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21300 if (TREE_CODE (expr) == COMPOUND_EXPR
21301 && !COMPOUND_EXPR_OVERLOADED (expr))
21302 return build2 (COMPOUND_EXPR,
21303 TREE_TYPE (expr),
21304 TREE_OPERAND (expr, 0),
21305 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21306
21307 /* If the type is unknown, it can't really be non-dependent */
21308 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21309
21310 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21311 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21312 }
21313
21314 /* ARGS is a vector of expressions as arguments to a function call.
21315 Replace the arguments with equivalent non-dependent expressions.
21316 This modifies ARGS in place. */
21317
21318 void
21319 make_args_non_dependent (vec<tree, va_gc> *args)
21320 {
21321 unsigned int ix;
21322 tree arg;
21323
21324 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21325 {
21326 tree newarg = build_non_dependent_expr (arg);
21327 if (newarg != arg)
21328 (*args)[ix] = newarg;
21329 }
21330 }
21331
21332 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21333 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21334 parms. */
21335
21336 static tree
21337 make_auto_1 (tree name)
21338 {
21339 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21340 TYPE_NAME (au) = build_decl (input_location,
21341 TYPE_DECL, name, au);
21342 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21343 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21344 (0, processing_template_decl + 1, processing_template_decl + 1,
21345 TYPE_NAME (au), NULL_TREE);
21346 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21347 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21348 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21349
21350 return au;
21351 }
21352
21353 tree
21354 make_decltype_auto (void)
21355 {
21356 return make_auto_1 (get_identifier ("decltype(auto)"));
21357 }
21358
21359 tree
21360 make_auto (void)
21361 {
21362 return make_auto_1 (get_identifier ("auto"));
21363 }
21364
21365 /* Given type ARG, return std::initializer_list<ARG>. */
21366
21367 static tree
21368 listify (tree arg)
21369 {
21370 tree std_init_list = namespace_binding
21371 (get_identifier ("initializer_list"), std_node);
21372 tree argvec;
21373 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21374 {
21375 error ("deducing from brace-enclosed initializer list requires "
21376 "#include <initializer_list>");
21377 return error_mark_node;
21378 }
21379 argvec = make_tree_vec (1);
21380 TREE_VEC_ELT (argvec, 0) = arg;
21381 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21382 NULL_TREE, 0, tf_warning_or_error);
21383 }
21384
21385 /* Replace auto in TYPE with std::initializer_list<auto>. */
21386
21387 static tree
21388 listify_autos (tree type, tree auto_node)
21389 {
21390 tree init_auto = listify (auto_node);
21391 tree argvec = make_tree_vec (1);
21392 TREE_VEC_ELT (argvec, 0) = init_auto;
21393 if (processing_template_decl)
21394 argvec = add_to_template_args (current_template_args (), argvec);
21395 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21396 }
21397
21398 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21399 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21400
21401 tree
21402 do_auto_deduction (tree type, tree init, tree auto_node)
21403 {
21404 tree targs;
21405
21406 if (init == error_mark_node)
21407 return error_mark_node;
21408
21409 if (type_dependent_expression_p (init))
21410 /* Defining a subset of type-dependent expressions that we can deduce
21411 from ahead of time isn't worth the trouble. */
21412 return type;
21413
21414 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21415 with either a new invented type template parameter U or, if the
21416 initializer is a braced-init-list (8.5.4), with
21417 std::initializer_list<U>. */
21418 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21419 type = listify_autos (type, auto_node);
21420
21421 init = resolve_nondeduced_context (init);
21422
21423 targs = make_tree_vec (1);
21424 if (AUTO_IS_DECLTYPE (auto_node))
21425 {
21426 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21427 TREE_VEC_ELT (targs, 0)
21428 = finish_decltype_type (init, id, tf_warning_or_error);
21429 if (type != auto_node)
21430 {
21431 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21432 return error_mark_node;
21433 }
21434 }
21435 else
21436 {
21437 tree parms = build_tree_list (NULL_TREE, type);
21438 tree tparms = make_tree_vec (1);
21439 int val;
21440
21441 TREE_VEC_ELT (tparms, 0)
21442 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21443 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21444 DEDUCE_CALL, LOOKUP_NORMAL,
21445 NULL, /*explain_p=*/false);
21446 if (val > 0)
21447 {
21448 if (processing_template_decl)
21449 /* Try again at instantiation time. */
21450 return type;
21451 if (type && type != error_mark_node)
21452 /* If type is error_mark_node a diagnostic must have been
21453 emitted by now. Also, having a mention to '<type error>'
21454 in the diagnostic is not really useful to the user. */
21455 {
21456 if (cfun && auto_node == current_function_auto_return_pattern
21457 && LAMBDA_FUNCTION_P (current_function_decl))
21458 error ("unable to deduce lambda return type from %qE", init);
21459 else
21460 error ("unable to deduce %qT from %qE", type, init);
21461 }
21462 return error_mark_node;
21463 }
21464 }
21465
21466 /* If the list of declarators contains more than one declarator, the type
21467 of each declared variable is determined as described above. If the
21468 type deduced for the template parameter U is not the same in each
21469 deduction, the program is ill-formed. */
21470 if (TREE_TYPE (auto_node)
21471 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21472 {
21473 if (cfun && auto_node == current_function_auto_return_pattern
21474 && LAMBDA_FUNCTION_P (current_function_decl))
21475 error ("inconsistent types %qT and %qT deduced for "
21476 "lambda return type", TREE_TYPE (auto_node),
21477 TREE_VEC_ELT (targs, 0));
21478 else
21479 error ("inconsistent deduction for %qT: %qT and then %qT",
21480 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21481 return error_mark_node;
21482 }
21483 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21484
21485 if (processing_template_decl)
21486 targs = add_to_template_args (current_template_args (), targs);
21487 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21488 }
21489
21490 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21491 result. */
21492
21493 tree
21494 splice_late_return_type (tree type, tree late_return_type)
21495 {
21496 tree argvec;
21497
21498 if (late_return_type == NULL_TREE)
21499 return type;
21500 argvec = make_tree_vec (1);
21501 TREE_VEC_ELT (argvec, 0) = late_return_type;
21502 if (processing_template_parmlist)
21503 /* For a late-specified return type in a template type-parameter, we
21504 need to add a dummy argument level for its parmlist. */
21505 argvec = add_to_template_args
21506 (make_tree_vec (processing_template_parmlist), argvec);
21507 if (current_template_parms)
21508 argvec = add_to_template_args (current_template_args (), argvec);
21509 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21510 }
21511
21512 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21513 'decltype(auto)'. */
21514
21515 bool
21516 is_auto (const_tree type)
21517 {
21518 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21519 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21520 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21521 return true;
21522 else
21523 return false;
21524 }
21525
21526 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21527 a use of `auto'. Returns NULL_TREE otherwise. */
21528
21529 tree
21530 type_uses_auto (tree type)
21531 {
21532 return find_type_usage (type, is_auto);
21533 }
21534
21535 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21536 'decltype(auto)' or a concept. */
21537
21538 bool
21539 is_auto_or_concept (const_tree type)
21540 {
21541 return is_auto (type); // or concept
21542 }
21543
21544 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21545 a concept identifier) iff TYPE contains a use of a generic type. Returns
21546 NULL_TREE otherwise. */
21547
21548 tree
21549 type_uses_auto_or_concept (tree type)
21550 {
21551 return find_type_usage (type, is_auto_or_concept);
21552 }
21553
21554
21555 /* For a given template T, return the vector of typedefs referenced
21556 in T for which access check is needed at T instantiation time.
21557 T is either a FUNCTION_DECL or a RECORD_TYPE.
21558 Those typedefs were added to T by the function
21559 append_type_to_template_for_access_check. */
21560
21561 vec<qualified_typedef_usage_t, va_gc> *
21562 get_types_needing_access_check (tree t)
21563 {
21564 tree ti;
21565 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21566
21567 if (!t || t == error_mark_node)
21568 return NULL;
21569
21570 if (!(ti = get_template_info (t)))
21571 return NULL;
21572
21573 if (CLASS_TYPE_P (t)
21574 || TREE_CODE (t) == FUNCTION_DECL)
21575 {
21576 if (!TI_TEMPLATE (ti))
21577 return NULL;
21578
21579 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21580 }
21581
21582 return result;
21583 }
21584
21585 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21586 tied to T. That list of typedefs will be access checked at
21587 T instantiation time.
21588 T is either a FUNCTION_DECL or a RECORD_TYPE.
21589 TYPE_DECL is a TYPE_DECL node representing a typedef.
21590 SCOPE is the scope through which TYPE_DECL is accessed.
21591 LOCATION is the location of the usage point of TYPE_DECL.
21592
21593 This function is a subroutine of
21594 append_type_to_template_for_access_check. */
21595
21596 static void
21597 append_type_to_template_for_access_check_1 (tree t,
21598 tree type_decl,
21599 tree scope,
21600 location_t location)
21601 {
21602 qualified_typedef_usage_t typedef_usage;
21603 tree ti;
21604
21605 if (!t || t == error_mark_node)
21606 return;
21607
21608 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21609 || CLASS_TYPE_P (t))
21610 && type_decl
21611 && TREE_CODE (type_decl) == TYPE_DECL
21612 && scope);
21613
21614 if (!(ti = get_template_info (t)))
21615 return;
21616
21617 gcc_assert (TI_TEMPLATE (ti));
21618
21619 typedef_usage.typedef_decl = type_decl;
21620 typedef_usage.context = scope;
21621 typedef_usage.locus = location;
21622
21623 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21624 }
21625
21626 /* Append TYPE_DECL to the template TEMPL.
21627 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21628 At TEMPL instanciation time, TYPE_DECL will be checked to see
21629 if it can be accessed through SCOPE.
21630 LOCATION is the location of the usage point of TYPE_DECL.
21631
21632 e.g. consider the following code snippet:
21633
21634 class C
21635 {
21636 typedef int myint;
21637 };
21638
21639 template<class U> struct S
21640 {
21641 C::myint mi; // <-- usage point of the typedef C::myint
21642 };
21643
21644 S<char> s;
21645
21646 At S<char> instantiation time, we need to check the access of C::myint
21647 In other words, we need to check the access of the myint typedef through
21648 the C scope. For that purpose, this function will add the myint typedef
21649 and the scope C through which its being accessed to a list of typedefs
21650 tied to the template S. That list will be walked at template instantiation
21651 time and access check performed on each typedefs it contains.
21652 Note that this particular code snippet should yield an error because
21653 myint is private to C. */
21654
21655 void
21656 append_type_to_template_for_access_check (tree templ,
21657 tree type_decl,
21658 tree scope,
21659 location_t location)
21660 {
21661 qualified_typedef_usage_t *iter;
21662 unsigned i;
21663
21664 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21665
21666 /* Make sure we don't append the type to the template twice. */
21667 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21668 if (iter->typedef_decl == type_decl && scope == iter->context)
21669 return;
21670
21671 append_type_to_template_for_access_check_1 (templ, type_decl,
21672 scope, location);
21673 }
21674
21675 /* Convert the generic type parameters in PARM that match the types given in the
21676 range [START_IDX, END_IDX) from the current_template_parms into generic type
21677 packs. */
21678
21679 tree
21680 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21681 {
21682 tree current = current_template_parms;
21683 int depth = TMPL_PARMS_DEPTH (current);
21684 current = INNERMOST_TEMPLATE_PARMS (current);
21685 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21686
21687 for (int i = 0; i < start_idx; ++i)
21688 TREE_VEC_ELT (replacement, i)
21689 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21690
21691 for (int i = start_idx; i < end_idx; ++i)
21692 {
21693 /* Create a distinct parameter pack type from the current parm and add it
21694 to the replacement args to tsubst below into the generic function
21695 parameter. */
21696
21697 tree o = TREE_TYPE (TREE_VALUE
21698 (TREE_VEC_ELT (current, i)));
21699 tree t = copy_type (o);
21700 TEMPLATE_TYPE_PARM_INDEX (t)
21701 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21702 o, 0, 0, tf_none);
21703 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21704 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21705 TYPE_MAIN_VARIANT (t) = t;
21706 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21707 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21708 TREE_VEC_ELT (replacement, i) = t;
21709 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21710 }
21711
21712 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21713 TREE_VEC_ELT (replacement, i)
21714 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21715
21716 /* If there are more levels then build up the replacement with the outer
21717 template parms. */
21718 if (depth > 1)
21719 replacement = add_to_template_args (template_parms_to_args
21720 (TREE_CHAIN (current_template_parms)),
21721 replacement);
21722
21723 return tsubst (parm, replacement, tf_none, NULL_TREE);
21724 }
21725
21726
21727 /* Set up the hash tables for template instantiations. */
21728
21729 void
21730 init_template_processing (void)
21731 {
21732 decl_specializations = htab_create_ggc (37,
21733 hash_specialization,
21734 eq_specializations,
21735 ggc_free);
21736 type_specializations = htab_create_ggc (37,
21737 hash_specialization,
21738 eq_specializations,
21739 ggc_free);
21740 }
21741
21742 /* Print stats about the template hash tables for -fstats. */
21743
21744 void
21745 print_template_statistics (void)
21746 {
21747 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21748 "%f collisions\n", (long) htab_size (decl_specializations),
21749 (long) htab_elements (decl_specializations),
21750 htab_collisions (decl_specializations));
21751 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21752 "%f collisions\n", (long) htab_size (type_specializations),
21753 (long) htab_elements (type_specializations),
21754 htab_collisions (type_specializations));
21755 }
21756
21757 #include "gt-cp-pt.h"