re PR c++/58674 ([c++11] ICE with template using declaration)
[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 /* Don't complain about the injected class name, as we've already
3531 complained about the class itself. */
3532 if (DECL_SELF_REFERENCE_P (decl))
3533 return false;
3534
3535 error ("declaration of %q+#D", decl);
3536 error (" shadows template parm %q+#D", olddecl);
3537 return false;
3538 }
3539
3540 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3541 ORIG_LEVEL, DECL, and TYPE. */
3542
3543 static tree
3544 build_template_parm_index (int index,
3545 int level,
3546 int orig_level,
3547 tree decl,
3548 tree type)
3549 {
3550 tree t = make_node (TEMPLATE_PARM_INDEX);
3551 TEMPLATE_PARM_IDX (t) = index;
3552 TEMPLATE_PARM_LEVEL (t) = level;
3553 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3554 TEMPLATE_PARM_DECL (t) = decl;
3555 TREE_TYPE (t) = type;
3556 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3557 TREE_READONLY (t) = TREE_READONLY (decl);
3558
3559 return t;
3560 }
3561
3562 /* Find the canonical type parameter for the given template type
3563 parameter. Returns the canonical type parameter, which may be TYPE
3564 if no such parameter existed. */
3565
3566 static tree
3567 canonical_type_parameter (tree type)
3568 {
3569 tree list;
3570 int idx = TEMPLATE_TYPE_IDX (type);
3571 if (!canonical_template_parms)
3572 vec_alloc (canonical_template_parms, idx+1);
3573
3574 while (canonical_template_parms->length () <= (unsigned)idx)
3575 vec_safe_push (canonical_template_parms, NULL_TREE);
3576
3577 list = (*canonical_template_parms)[idx];
3578 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3579 list = TREE_CHAIN (list);
3580
3581 if (list)
3582 return TREE_VALUE (list);
3583 else
3584 {
3585 (*canonical_template_parms)[idx]
3586 = tree_cons (NULL_TREE, type,
3587 (*canonical_template_parms)[idx]);
3588 return type;
3589 }
3590 }
3591
3592 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3593 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3594 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3595 new one is created. */
3596
3597 static tree
3598 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3599 tsubst_flags_t complain)
3600 {
3601 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3602 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3603 != TEMPLATE_PARM_LEVEL (index) - levels)
3604 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3605 {
3606 tree orig_decl = TEMPLATE_PARM_DECL (index);
3607 tree decl, t;
3608
3609 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3610 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3611 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3612 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3613 DECL_ARTIFICIAL (decl) = 1;
3614 SET_DECL_TEMPLATE_PARM_P (decl);
3615
3616 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3617 TEMPLATE_PARM_LEVEL (index) - levels,
3618 TEMPLATE_PARM_ORIG_LEVEL (index),
3619 decl, type);
3620 TEMPLATE_PARM_DESCENDANTS (index) = t;
3621 TEMPLATE_PARM_PARAMETER_PACK (t)
3622 = TEMPLATE_PARM_PARAMETER_PACK (index);
3623
3624 /* Template template parameters need this. */
3625 if (TREE_CODE (decl) == TEMPLATE_DECL)
3626 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3627 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3628 args, complain);
3629 }
3630
3631 return TEMPLATE_PARM_DESCENDANTS (index);
3632 }
3633
3634 /* Process information from new template parameter PARM and append it
3635 to the LIST being built. This new parameter is a non-type
3636 parameter iff IS_NON_TYPE is true. This new parameter is a
3637 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3638 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3639 parameter list PARM belongs to. This is used used to create a
3640 proper canonical type for the type of PARM that is to be created,
3641 iff PARM is a type. If the size is not known, this parameter shall
3642 be set to 0. */
3643
3644 tree
3645 process_template_parm (tree list, location_t parm_loc, tree parm,
3646 bool is_non_type, bool is_parameter_pack)
3647 {
3648 tree decl = 0;
3649 tree defval;
3650 tree err_parm_list;
3651 int idx = 0;
3652
3653 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3654 defval = TREE_PURPOSE (parm);
3655
3656 if (list)
3657 {
3658 tree p = tree_last (list);
3659
3660 if (p && TREE_VALUE (p) != error_mark_node)
3661 {
3662 p = TREE_VALUE (p);
3663 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3664 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3665 else
3666 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3667 }
3668
3669 ++idx;
3670 }
3671 else
3672 idx = 0;
3673
3674 if (is_non_type)
3675 {
3676 parm = TREE_VALUE (parm);
3677
3678 SET_DECL_TEMPLATE_PARM_P (parm);
3679
3680 if (TREE_TYPE (parm) == error_mark_node)
3681 {
3682 err_parm_list = build_tree_list (defval, parm);
3683 TREE_VALUE (err_parm_list) = error_mark_node;
3684 return chainon (list, err_parm_list);
3685 }
3686 else
3687 {
3688 /* [temp.param]
3689
3690 The top-level cv-qualifiers on the template-parameter are
3691 ignored when determining its type. */
3692 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3693 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3694 {
3695 err_parm_list = build_tree_list (defval, parm);
3696 TREE_VALUE (err_parm_list) = error_mark_node;
3697 return chainon (list, err_parm_list);
3698 }
3699
3700 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3701 {
3702 /* This template parameter is not a parameter pack, but it
3703 should be. Complain about "bare" parameter packs. */
3704 check_for_bare_parameter_packs (TREE_TYPE (parm));
3705
3706 /* Recover by calling this a parameter pack. */
3707 is_parameter_pack = true;
3708 }
3709 }
3710
3711 /* A template parameter is not modifiable. */
3712 TREE_CONSTANT (parm) = 1;
3713 TREE_READONLY (parm) = 1;
3714 decl = build_decl (parm_loc,
3715 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3716 TREE_CONSTANT (decl) = 1;
3717 TREE_READONLY (decl) = 1;
3718 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3719 = build_template_parm_index (idx, processing_template_decl,
3720 processing_template_decl,
3721 decl, TREE_TYPE (parm));
3722
3723 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3724 = is_parameter_pack;
3725 }
3726 else
3727 {
3728 tree t;
3729 parm = TREE_VALUE (TREE_VALUE (parm));
3730
3731 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3732 {
3733 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3734 /* This is for distinguishing between real templates and template
3735 template parameters */
3736 TREE_TYPE (parm) = t;
3737 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3738 decl = parm;
3739 }
3740 else
3741 {
3742 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3743 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3744 decl = build_decl (parm_loc,
3745 TYPE_DECL, parm, t);
3746 }
3747
3748 TYPE_NAME (t) = decl;
3749 TYPE_STUB_DECL (t) = decl;
3750 parm = decl;
3751 TEMPLATE_TYPE_PARM_INDEX (t)
3752 = build_template_parm_index (idx, processing_template_decl,
3753 processing_template_decl,
3754 decl, TREE_TYPE (parm));
3755 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3756 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3757 }
3758 DECL_ARTIFICIAL (decl) = 1;
3759 SET_DECL_TEMPLATE_PARM_P (decl);
3760 pushdecl (decl);
3761 parm = build_tree_list (defval, parm);
3762 return chainon (list, parm);
3763 }
3764
3765 /* The end of a template parameter list has been reached. Process the
3766 tree list into a parameter vector, converting each parameter into a more
3767 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3768 as PARM_DECLs. */
3769
3770 tree
3771 end_template_parm_list (tree parms)
3772 {
3773 int nparms;
3774 tree parm, next;
3775 tree saved_parmlist = make_tree_vec (list_length (parms));
3776
3777 current_template_parms
3778 = tree_cons (size_int (processing_template_decl),
3779 saved_parmlist, current_template_parms);
3780
3781 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3782 {
3783 next = TREE_CHAIN (parm);
3784 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3785 TREE_CHAIN (parm) = NULL_TREE;
3786 }
3787
3788 --processing_template_parmlist;
3789
3790 return saved_parmlist;
3791 }
3792
3793 /* end_template_decl is called after a template declaration is seen. */
3794
3795 void
3796 end_template_decl (void)
3797 {
3798 reset_specialization ();
3799
3800 if (! processing_template_decl)
3801 return;
3802
3803 /* This matches the pushlevel in begin_template_parm_list. */
3804 finish_scope ();
3805
3806 --processing_template_decl;
3807 current_template_parms = TREE_CHAIN (current_template_parms);
3808 }
3809
3810 /* Takes a TREE_LIST representing a template parameter and convert it
3811 into an argument suitable to be passed to the type substitution
3812 functions. Note that If the TREE_LIST contains an error_mark
3813 node, the returned argument is error_mark_node. */
3814
3815 static tree
3816 template_parm_to_arg (tree t)
3817 {
3818
3819 if (t == NULL_TREE
3820 || TREE_CODE (t) != TREE_LIST)
3821 return t;
3822
3823 if (error_operand_p (TREE_VALUE (t)))
3824 return error_mark_node;
3825
3826 t = TREE_VALUE (t);
3827
3828 if (TREE_CODE (t) == TYPE_DECL
3829 || TREE_CODE (t) == TEMPLATE_DECL)
3830 {
3831 t = TREE_TYPE (t);
3832
3833 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3834 {
3835 /* Turn this argument into a TYPE_ARGUMENT_PACK
3836 with a single element, which expands T. */
3837 tree vec = make_tree_vec (1);
3838 #ifdef ENABLE_CHECKING
3839 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3840 (vec, TREE_VEC_LENGTH (vec));
3841 #endif
3842 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3843
3844 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3845 SET_ARGUMENT_PACK_ARGS (t, vec);
3846 }
3847 }
3848 else
3849 {
3850 t = DECL_INITIAL (t);
3851
3852 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3853 {
3854 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3855 with a single element, which expands T. */
3856 tree vec = make_tree_vec (1);
3857 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3858 #ifdef ENABLE_CHECKING
3859 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3860 (vec, TREE_VEC_LENGTH (vec));
3861 #endif
3862 t = convert_from_reference (t);
3863 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3864
3865 t = make_node (NONTYPE_ARGUMENT_PACK);
3866 SET_ARGUMENT_PACK_ARGS (t, vec);
3867 TREE_TYPE (t) = type;
3868 }
3869 }
3870 return t;
3871 }
3872
3873 /* Given a set of template parameters, return them as a set of template
3874 arguments. The template parameters are represented as a TREE_VEC, in
3875 the form documented in cp-tree.h for template arguments. */
3876
3877 static tree
3878 template_parms_to_args (tree parms)
3879 {
3880 tree header;
3881 tree args = NULL_TREE;
3882 int length = TMPL_PARMS_DEPTH (parms);
3883 int l = length;
3884
3885 /* If there is only one level of template parameters, we do not
3886 create a TREE_VEC of TREE_VECs. Instead, we return a single
3887 TREE_VEC containing the arguments. */
3888 if (length > 1)
3889 args = make_tree_vec (length);
3890
3891 for (header = parms; header; header = TREE_CHAIN (header))
3892 {
3893 tree a = copy_node (TREE_VALUE (header));
3894 int i;
3895
3896 TREE_TYPE (a) = NULL_TREE;
3897 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3898 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3899
3900 #ifdef ENABLE_CHECKING
3901 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3902 #endif
3903
3904 if (length > 1)
3905 TREE_VEC_ELT (args, --l) = a;
3906 else
3907 args = a;
3908 }
3909
3910 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3911 /* This can happen for template parms of a template template
3912 parameter, e.g:
3913
3914 template<template<class T, class U> class TT> struct S;
3915
3916 Consider the level of the parms of TT; T and U both have
3917 level 2; TT has no template parm of level 1. So in this case
3918 the first element of full_template_args is NULL_TREE. If we
3919 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3920 of 2. This will make tsubst wrongly consider that T and U
3921 have level 1. Instead, let's create a dummy vector as the
3922 first element of full_template_args so that TMPL_ARGS_DEPTH
3923 returns the correct depth for args. */
3924 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3925 return args;
3926 }
3927
3928 /* Within the declaration of a template, return the currently active
3929 template parameters as an argument TREE_VEC. */
3930
3931 static tree
3932 current_template_args (void)
3933 {
3934 return template_parms_to_args (current_template_parms);
3935 }
3936
3937 /* Update the declared TYPE by doing any lookups which were thought to be
3938 dependent, but are not now that we know the SCOPE of the declarator. */
3939
3940 tree
3941 maybe_update_decl_type (tree orig_type, tree scope)
3942 {
3943 tree type = orig_type;
3944
3945 if (type == NULL_TREE)
3946 return type;
3947
3948 if (TREE_CODE (orig_type) == TYPE_DECL)
3949 type = TREE_TYPE (type);
3950
3951 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3952 && dependent_type_p (type)
3953 /* Don't bother building up the args in this case. */
3954 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3955 {
3956 /* tsubst in the args corresponding to the template parameters,
3957 including auto if present. Most things will be unchanged, but
3958 make_typename_type and tsubst_qualified_id will resolve
3959 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3960 tree args = current_template_args ();
3961 tree auto_node = type_uses_auto (type);
3962 tree pushed;
3963 if (auto_node)
3964 {
3965 tree auto_vec = make_tree_vec (1);
3966 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3967 args = add_to_template_args (args, auto_vec);
3968 }
3969 pushed = push_scope (scope);
3970 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3971 if (pushed)
3972 pop_scope (scope);
3973 }
3974
3975 if (type == error_mark_node)
3976 return orig_type;
3977
3978 if (TREE_CODE (orig_type) == TYPE_DECL)
3979 {
3980 if (same_type_p (type, TREE_TYPE (orig_type)))
3981 type = orig_type;
3982 else
3983 type = TYPE_NAME (type);
3984 }
3985 return type;
3986 }
3987
3988 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3989 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3990 a member template. Used by push_template_decl below. */
3991
3992 static tree
3993 build_template_decl (tree decl, tree parms, bool member_template_p)
3994 {
3995 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3996 DECL_TEMPLATE_PARMS (tmpl) = parms;
3997 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3998 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3999 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4000
4001 return tmpl;
4002 }
4003
4004 struct template_parm_data
4005 {
4006 /* The level of the template parameters we are currently
4007 processing. */
4008 int level;
4009
4010 /* The index of the specialization argument we are currently
4011 processing. */
4012 int current_arg;
4013
4014 /* An array whose size is the number of template parameters. The
4015 elements are nonzero if the parameter has been used in any one
4016 of the arguments processed so far. */
4017 int* parms;
4018
4019 /* An array whose size is the number of template arguments. The
4020 elements are nonzero if the argument makes use of template
4021 parameters of this level. */
4022 int* arg_uses_template_parms;
4023 };
4024
4025 /* Subroutine of push_template_decl used to see if each template
4026 parameter in a partial specialization is used in the explicit
4027 argument list. If T is of the LEVEL given in DATA (which is
4028 treated as a template_parm_data*), then DATA->PARMS is marked
4029 appropriately. */
4030
4031 static int
4032 mark_template_parm (tree t, void* data)
4033 {
4034 int level;
4035 int idx;
4036 struct template_parm_data* tpd = (struct template_parm_data*) data;
4037
4038 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4039 {
4040 level = TEMPLATE_PARM_LEVEL (t);
4041 idx = TEMPLATE_PARM_IDX (t);
4042 }
4043 else
4044 {
4045 level = TEMPLATE_TYPE_LEVEL (t);
4046 idx = TEMPLATE_TYPE_IDX (t);
4047 }
4048
4049 if (level == tpd->level)
4050 {
4051 tpd->parms[idx] = 1;
4052 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4053 }
4054
4055 /* Return zero so that for_each_template_parm will continue the
4056 traversal of the tree; we want to mark *every* template parm. */
4057 return 0;
4058 }
4059
4060 /* Process the partial specialization DECL. */
4061
4062 static tree
4063 process_partial_specialization (tree decl)
4064 {
4065 tree type = TREE_TYPE (decl);
4066 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4067 tree specargs = CLASSTYPE_TI_ARGS (type);
4068 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4069 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4070 tree inner_parms;
4071 tree inst;
4072 int nargs = TREE_VEC_LENGTH (inner_args);
4073 int ntparms;
4074 int i;
4075 bool did_error_intro = false;
4076 struct template_parm_data tpd;
4077 struct template_parm_data tpd2;
4078
4079 gcc_assert (current_template_parms);
4080
4081 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4082 ntparms = TREE_VEC_LENGTH (inner_parms);
4083
4084 /* We check that each of the template parameters given in the
4085 partial specialization is used in the argument list to the
4086 specialization. For example:
4087
4088 template <class T> struct S;
4089 template <class T> struct S<T*>;
4090
4091 The second declaration is OK because `T*' uses the template
4092 parameter T, whereas
4093
4094 template <class T> struct S<int>;
4095
4096 is no good. Even trickier is:
4097
4098 template <class T>
4099 struct S1
4100 {
4101 template <class U>
4102 struct S2;
4103 template <class U>
4104 struct S2<T>;
4105 };
4106
4107 The S2<T> declaration is actually invalid; it is a
4108 full-specialization. Of course,
4109
4110 template <class U>
4111 struct S2<T (*)(U)>;
4112
4113 or some such would have been OK. */
4114 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4115 tpd.parms = XALLOCAVEC (int, ntparms);
4116 memset (tpd.parms, 0, sizeof (int) * ntparms);
4117
4118 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4119 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4120 for (i = 0; i < nargs; ++i)
4121 {
4122 tpd.current_arg = i;
4123 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4124 &mark_template_parm,
4125 &tpd,
4126 NULL,
4127 /*include_nondeduced_p=*/false);
4128 }
4129 for (i = 0; i < ntparms; ++i)
4130 if (tpd.parms[i] == 0)
4131 {
4132 /* One of the template parms was not used in the
4133 specialization. */
4134 if (!did_error_intro)
4135 {
4136 error ("template parameters not used in partial specialization:");
4137 did_error_intro = true;
4138 }
4139
4140 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4141 }
4142
4143 if (did_error_intro)
4144 return error_mark_node;
4145
4146 /* [temp.class.spec]
4147
4148 The argument list of the specialization shall not be identical to
4149 the implicit argument list of the primary template. */
4150 if (comp_template_args
4151 (inner_args,
4152 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4153 (maintmpl)))))
4154 error ("partial specialization %qT does not specialize any template arguments", type);
4155
4156 /* A partial specialization that replaces multiple parameters of the
4157 primary template with a pack expansion is less specialized for those
4158 parameters. */
4159 if (nargs < DECL_NTPARMS (maintmpl))
4160 {
4161 error ("partial specialization is not more specialized than the "
4162 "primary template because it replaces multiple parameters "
4163 "with a pack expansion");
4164 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4165 return decl;
4166 }
4167
4168 /* [temp.class.spec]
4169
4170 A partially specialized non-type argument expression shall not
4171 involve template parameters of the partial specialization except
4172 when the argument expression is a simple identifier.
4173
4174 The type of a template parameter corresponding to a specialized
4175 non-type argument shall not be dependent on a parameter of the
4176 specialization.
4177
4178 Also, we verify that pack expansions only occur at the
4179 end of the argument list. */
4180 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4181 tpd2.parms = 0;
4182 for (i = 0; i < nargs; ++i)
4183 {
4184 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4185 tree arg = TREE_VEC_ELT (inner_args, i);
4186 tree packed_args = NULL_TREE;
4187 int j, len = 1;
4188
4189 if (ARGUMENT_PACK_P (arg))
4190 {
4191 /* Extract the arguments from the argument pack. We'll be
4192 iterating over these in the following loop. */
4193 packed_args = ARGUMENT_PACK_ARGS (arg);
4194 len = TREE_VEC_LENGTH (packed_args);
4195 }
4196
4197 for (j = 0; j < len; j++)
4198 {
4199 if (packed_args)
4200 /* Get the Jth argument in the parameter pack. */
4201 arg = TREE_VEC_ELT (packed_args, j);
4202
4203 if (PACK_EXPANSION_P (arg))
4204 {
4205 /* Pack expansions must come at the end of the
4206 argument list. */
4207 if ((packed_args && j < len - 1)
4208 || (!packed_args && i < nargs - 1))
4209 {
4210 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4211 error ("parameter pack argument %qE must be at the "
4212 "end of the template argument list", arg);
4213 else
4214 error ("parameter pack argument %qT must be at the "
4215 "end of the template argument list", arg);
4216 }
4217 }
4218
4219 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4220 /* We only care about the pattern. */
4221 arg = PACK_EXPANSION_PATTERN (arg);
4222
4223 if (/* These first two lines are the `non-type' bit. */
4224 !TYPE_P (arg)
4225 && TREE_CODE (arg) != TEMPLATE_DECL
4226 /* This next line is the `argument expression is not just a
4227 simple identifier' condition and also the `specialized
4228 non-type argument' bit. */
4229 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4230 {
4231 if ((!packed_args && tpd.arg_uses_template_parms[i])
4232 || (packed_args && uses_template_parms (arg)))
4233 error ("template argument %qE involves template parameter(s)",
4234 arg);
4235 else
4236 {
4237 /* Look at the corresponding template parameter,
4238 marking which template parameters its type depends
4239 upon. */
4240 tree type = TREE_TYPE (parm);
4241
4242 if (!tpd2.parms)
4243 {
4244 /* We haven't yet initialized TPD2. Do so now. */
4245 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4246 /* The number of parameters here is the number in the
4247 main template, which, as checked in the assertion
4248 above, is NARGS. */
4249 tpd2.parms = XALLOCAVEC (int, nargs);
4250 tpd2.level =
4251 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4252 }
4253
4254 /* Mark the template parameters. But this time, we're
4255 looking for the template parameters of the main
4256 template, not in the specialization. */
4257 tpd2.current_arg = i;
4258 tpd2.arg_uses_template_parms[i] = 0;
4259 memset (tpd2.parms, 0, sizeof (int) * nargs);
4260 for_each_template_parm (type,
4261 &mark_template_parm,
4262 &tpd2,
4263 NULL,
4264 /*include_nondeduced_p=*/false);
4265
4266 if (tpd2.arg_uses_template_parms [i])
4267 {
4268 /* The type depended on some template parameters.
4269 If they are fully specialized in the
4270 specialization, that's OK. */
4271 int j;
4272 int count = 0;
4273 for (j = 0; j < nargs; ++j)
4274 if (tpd2.parms[j] != 0
4275 && tpd.arg_uses_template_parms [j])
4276 ++count;
4277 if (count != 0)
4278 error_n (input_location, count,
4279 "type %qT of template argument %qE depends "
4280 "on a template parameter",
4281 "type %qT of template argument %qE depends "
4282 "on template parameters",
4283 type,
4284 arg);
4285 }
4286 }
4287 }
4288 }
4289 }
4290
4291 /* We should only get here once. */
4292 gcc_assert (!COMPLETE_TYPE_P (type));
4293
4294 tree tmpl = build_template_decl (decl, current_template_parms,
4295 DECL_MEMBER_TEMPLATE_P (maintmpl));
4296 TREE_TYPE (tmpl) = type;
4297 DECL_TEMPLATE_RESULT (tmpl) = decl;
4298 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4299 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4300 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4301
4302 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4303 = tree_cons (specargs, tmpl,
4304 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4305 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4306
4307 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4308 inst = TREE_CHAIN (inst))
4309 {
4310 tree inst_type = TREE_VALUE (inst);
4311 if (COMPLETE_TYPE_P (inst_type)
4312 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4313 {
4314 tree spec = most_specialized_class (inst_type, tf_none);
4315 if (spec && TREE_TYPE (spec) == type)
4316 permerror (input_location,
4317 "partial specialization of %qT after instantiation "
4318 "of %qT", type, inst_type);
4319 }
4320 }
4321
4322 return decl;
4323 }
4324
4325 /* Check that a template declaration's use of default arguments and
4326 parameter packs is not invalid. Here, PARMS are the template
4327 parameters. IS_PRIMARY is true if DECL is the thing declared by
4328 a primary template. IS_PARTIAL is true if DECL is a partial
4329 specialization.
4330
4331 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4332 declaration (but not a definition); 1 indicates a declaration, 2
4333 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4334 emitted for extraneous default arguments.
4335
4336 Returns TRUE if there were no errors found, FALSE otherwise. */
4337
4338 bool
4339 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4340 bool is_partial, int is_friend_decl)
4341 {
4342 const char *msg;
4343 int last_level_to_check;
4344 tree parm_level;
4345 bool no_errors = true;
4346
4347 /* [temp.param]
4348
4349 A default template-argument shall not be specified in a
4350 function template declaration or a function template definition, nor
4351 in the template-parameter-list of the definition of a member of a
4352 class template. */
4353
4354 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4355 /* You can't have a function template declaration in a local
4356 scope, nor you can you define a member of a class template in a
4357 local scope. */
4358 return true;
4359
4360 if (TREE_CODE (decl) == TYPE_DECL
4361 && TREE_TYPE (decl)
4362 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4363 /* A lambda doesn't have an explicit declaration; don't complain
4364 about the parms of the enclosing class. */
4365 return true;
4366
4367 if (current_class_type
4368 && !TYPE_BEING_DEFINED (current_class_type)
4369 && DECL_LANG_SPECIFIC (decl)
4370 && DECL_DECLARES_FUNCTION_P (decl)
4371 /* If this is either a friend defined in the scope of the class
4372 or a member function. */
4373 && (DECL_FUNCTION_MEMBER_P (decl)
4374 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4375 : DECL_FRIEND_CONTEXT (decl)
4376 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4377 : false)
4378 /* And, if it was a member function, it really was defined in
4379 the scope of the class. */
4380 && (!DECL_FUNCTION_MEMBER_P (decl)
4381 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4382 /* We already checked these parameters when the template was
4383 declared, so there's no need to do it again now. This function
4384 was defined in class scope, but we're processing its body now
4385 that the class is complete. */
4386 return true;
4387
4388 /* Core issue 226 (C++0x only): the following only applies to class
4389 templates. */
4390 if (is_primary
4391 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4392 {
4393 /* [temp.param]
4394
4395 If a template-parameter has a default template-argument, all
4396 subsequent template-parameters shall have a default
4397 template-argument supplied. */
4398 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4399 {
4400 tree inner_parms = TREE_VALUE (parm_level);
4401 int ntparms = TREE_VEC_LENGTH (inner_parms);
4402 int seen_def_arg_p = 0;
4403 int i;
4404
4405 for (i = 0; i < ntparms; ++i)
4406 {
4407 tree parm = TREE_VEC_ELT (inner_parms, i);
4408
4409 if (parm == error_mark_node)
4410 continue;
4411
4412 if (TREE_PURPOSE (parm))
4413 seen_def_arg_p = 1;
4414 else if (seen_def_arg_p
4415 && !template_parameter_pack_p (TREE_VALUE (parm)))
4416 {
4417 error ("no default argument for %qD", TREE_VALUE (parm));
4418 /* For better subsequent error-recovery, we indicate that
4419 there should have been a default argument. */
4420 TREE_PURPOSE (parm) = error_mark_node;
4421 no_errors = false;
4422 }
4423 else if (!is_partial
4424 && !is_friend_decl
4425 /* Don't complain about an enclosing partial
4426 specialization. */
4427 && parm_level == parms
4428 && TREE_CODE (decl) == TYPE_DECL
4429 && i < ntparms - 1
4430 && template_parameter_pack_p (TREE_VALUE (parm)))
4431 {
4432 /* A primary class template can only have one
4433 parameter pack, at the end of the template
4434 parameter list. */
4435
4436 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4437 error ("parameter pack %qE must be at the end of the"
4438 " template parameter list", TREE_VALUE (parm));
4439 else
4440 error ("parameter pack %qT must be at the end of the"
4441 " template parameter list",
4442 TREE_TYPE (TREE_VALUE (parm)));
4443
4444 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4445 = error_mark_node;
4446 no_errors = false;
4447 }
4448 }
4449 }
4450 }
4451
4452 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4453 || is_partial
4454 || !is_primary
4455 || is_friend_decl)
4456 /* For an ordinary class template, default template arguments are
4457 allowed at the innermost level, e.g.:
4458 template <class T = int>
4459 struct S {};
4460 but, in a partial specialization, they're not allowed even
4461 there, as we have in [temp.class.spec]:
4462
4463 The template parameter list of a specialization shall not
4464 contain default template argument values.
4465
4466 So, for a partial specialization, or for a function template
4467 (in C++98/C++03), we look at all of them. */
4468 ;
4469 else
4470 /* But, for a primary class template that is not a partial
4471 specialization we look at all template parameters except the
4472 innermost ones. */
4473 parms = TREE_CHAIN (parms);
4474
4475 /* Figure out what error message to issue. */
4476 if (is_friend_decl == 2)
4477 msg = G_("default template arguments may not be used in function template "
4478 "friend re-declaration");
4479 else if (is_friend_decl)
4480 msg = G_("default template arguments may not be used in function template "
4481 "friend declarations");
4482 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4483 msg = G_("default template arguments may not be used in function templates "
4484 "without -std=c++11 or -std=gnu++11");
4485 else if (is_partial)
4486 msg = G_("default template arguments may not be used in "
4487 "partial specializations");
4488 else
4489 msg = G_("default argument for template parameter for class enclosing %qD");
4490
4491 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4492 /* If we're inside a class definition, there's no need to
4493 examine the parameters to the class itself. On the one
4494 hand, they will be checked when the class is defined, and,
4495 on the other, default arguments are valid in things like:
4496 template <class T = double>
4497 struct S { template <class U> void f(U); };
4498 Here the default argument for `S' has no bearing on the
4499 declaration of `f'. */
4500 last_level_to_check = template_class_depth (current_class_type) + 1;
4501 else
4502 /* Check everything. */
4503 last_level_to_check = 0;
4504
4505 for (parm_level = parms;
4506 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4507 parm_level = TREE_CHAIN (parm_level))
4508 {
4509 tree inner_parms = TREE_VALUE (parm_level);
4510 int i;
4511 int ntparms;
4512
4513 ntparms = TREE_VEC_LENGTH (inner_parms);
4514 for (i = 0; i < ntparms; ++i)
4515 {
4516 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4517 continue;
4518
4519 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4520 {
4521 if (msg)
4522 {
4523 no_errors = false;
4524 if (is_friend_decl == 2)
4525 return no_errors;
4526
4527 error (msg, decl);
4528 msg = 0;
4529 }
4530
4531 /* Clear out the default argument so that we are not
4532 confused later. */
4533 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4534 }
4535 }
4536
4537 /* At this point, if we're still interested in issuing messages,
4538 they must apply to classes surrounding the object declared. */
4539 if (msg)
4540 msg = G_("default argument for template parameter for class "
4541 "enclosing %qD");
4542 }
4543
4544 return no_errors;
4545 }
4546
4547 /* Worker for push_template_decl_real, called via
4548 for_each_template_parm. DATA is really an int, indicating the
4549 level of the parameters we are interested in. If T is a template
4550 parameter of that level, return nonzero. */
4551
4552 static int
4553 template_parm_this_level_p (tree t, void* data)
4554 {
4555 int this_level = *(int *)data;
4556 int level;
4557
4558 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4559 level = TEMPLATE_PARM_LEVEL (t);
4560 else
4561 level = TEMPLATE_TYPE_LEVEL (t);
4562 return level == this_level;
4563 }
4564
4565 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4566 parameters given by current_template_args, or reuses a
4567 previously existing one, if appropriate. Returns the DECL, or an
4568 equivalent one, if it is replaced via a call to duplicate_decls.
4569
4570 If IS_FRIEND is true, DECL is a friend declaration. */
4571
4572 tree
4573 push_template_decl_real (tree decl, bool is_friend)
4574 {
4575 tree tmpl;
4576 tree args;
4577 tree info;
4578 tree ctx;
4579 bool is_primary;
4580 bool is_partial;
4581 int new_template_p = 0;
4582 /* True if the template is a member template, in the sense of
4583 [temp.mem]. */
4584 bool member_template_p = false;
4585
4586 if (decl == error_mark_node || !current_template_parms)
4587 return error_mark_node;
4588
4589 /* See if this is a partial specialization. */
4590 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4591 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4592 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4593
4594 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4595 is_friend = true;
4596
4597 if (is_friend)
4598 /* For a friend, we want the context of the friend function, not
4599 the type of which it is a friend. */
4600 ctx = CP_DECL_CONTEXT (decl);
4601 else if (CP_DECL_CONTEXT (decl)
4602 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4603 /* In the case of a virtual function, we want the class in which
4604 it is defined. */
4605 ctx = CP_DECL_CONTEXT (decl);
4606 else
4607 /* Otherwise, if we're currently defining some class, the DECL
4608 is assumed to be a member of the class. */
4609 ctx = current_scope ();
4610
4611 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4612 ctx = NULL_TREE;
4613
4614 if (!DECL_CONTEXT (decl))
4615 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4616
4617 /* See if this is a primary template. */
4618 if (is_friend && ctx)
4619 /* A friend template that specifies a class context, i.e.
4620 template <typename T> friend void A<T>::f();
4621 is not primary. */
4622 is_primary = false;
4623 else
4624 is_primary = template_parm_scope_p ();
4625
4626 if (is_primary)
4627 {
4628 if (DECL_CLASS_SCOPE_P (decl))
4629 member_template_p = true;
4630 if (TREE_CODE (decl) == TYPE_DECL
4631 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4632 {
4633 error ("template class without a name");
4634 return error_mark_node;
4635 }
4636 else if (TREE_CODE (decl) == FUNCTION_DECL)
4637 {
4638 if (DECL_DESTRUCTOR_P (decl))
4639 {
4640 /* [temp.mem]
4641
4642 A destructor shall not be a member template. */
4643 error ("destructor %qD declared as member template", decl);
4644 return error_mark_node;
4645 }
4646 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4647 && (!prototype_p (TREE_TYPE (decl))
4648 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4649 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4650 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4651 == void_list_node)))
4652 {
4653 /* [basic.stc.dynamic.allocation]
4654
4655 An allocation function can be a function
4656 template. ... Template allocation functions shall
4657 have two or more parameters. */
4658 error ("invalid template declaration of %qD", decl);
4659 return error_mark_node;
4660 }
4661 }
4662 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4663 && CLASS_TYPE_P (TREE_TYPE (decl)))
4664 /* OK */;
4665 else if (TREE_CODE (decl) == TYPE_DECL
4666 && TYPE_DECL_ALIAS_P (decl))
4667 /* alias-declaration */
4668 gcc_assert (!DECL_ARTIFICIAL (decl));
4669 else
4670 {
4671 error ("template declaration of %q#D", decl);
4672 return error_mark_node;
4673 }
4674 }
4675
4676 /* Check to see that the rules regarding the use of default
4677 arguments are not being violated. */
4678 check_default_tmpl_args (decl, current_template_parms,
4679 is_primary, is_partial, /*is_friend_decl=*/0);
4680
4681 /* Ensure that there are no parameter packs in the type of this
4682 declaration that have not been expanded. */
4683 if (TREE_CODE (decl) == FUNCTION_DECL)
4684 {
4685 /* Check each of the arguments individually to see if there are
4686 any bare parameter packs. */
4687 tree type = TREE_TYPE (decl);
4688 tree arg = DECL_ARGUMENTS (decl);
4689 tree argtype = TYPE_ARG_TYPES (type);
4690
4691 while (arg && argtype)
4692 {
4693 if (!DECL_PACK_P (arg)
4694 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4695 {
4696 /* This is a PARM_DECL that contains unexpanded parameter
4697 packs. We have already complained about this in the
4698 check_for_bare_parameter_packs call, so just replace
4699 these types with ERROR_MARK_NODE. */
4700 TREE_TYPE (arg) = error_mark_node;
4701 TREE_VALUE (argtype) = error_mark_node;
4702 }
4703
4704 arg = DECL_CHAIN (arg);
4705 argtype = TREE_CHAIN (argtype);
4706 }
4707
4708 /* Check for bare parameter packs in the return type and the
4709 exception specifiers. */
4710 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4711 /* Errors were already issued, set return type to int
4712 as the frontend doesn't expect error_mark_node as
4713 the return type. */
4714 TREE_TYPE (type) = integer_type_node;
4715 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4716 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4717 }
4718 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4719 && TYPE_DECL_ALIAS_P (decl))
4720 ? DECL_ORIGINAL_TYPE (decl)
4721 : TREE_TYPE (decl)))
4722 {
4723 TREE_TYPE (decl) = error_mark_node;
4724 return error_mark_node;
4725 }
4726
4727 if (is_partial)
4728 return process_partial_specialization (decl);
4729
4730 args = current_template_args ();
4731
4732 if (!ctx
4733 || TREE_CODE (ctx) == FUNCTION_DECL
4734 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4735 || (TREE_CODE (decl) == TYPE_DECL
4736 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4737 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4738 {
4739 if (DECL_LANG_SPECIFIC (decl)
4740 && DECL_TEMPLATE_INFO (decl)
4741 && DECL_TI_TEMPLATE (decl))
4742 tmpl = DECL_TI_TEMPLATE (decl);
4743 /* If DECL is a TYPE_DECL for a class-template, then there won't
4744 be DECL_LANG_SPECIFIC. The information equivalent to
4745 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4746 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4747 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4748 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4749 {
4750 /* Since a template declaration already existed for this
4751 class-type, we must be redeclaring it here. Make sure
4752 that the redeclaration is valid. */
4753 redeclare_class_template (TREE_TYPE (decl),
4754 current_template_parms);
4755 /* We don't need to create a new TEMPLATE_DECL; just use the
4756 one we already had. */
4757 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4758 }
4759 else
4760 {
4761 tmpl = build_template_decl (decl, current_template_parms,
4762 member_template_p);
4763 new_template_p = 1;
4764
4765 if (DECL_LANG_SPECIFIC (decl)
4766 && DECL_TEMPLATE_SPECIALIZATION (decl))
4767 {
4768 /* A specialization of a member template of a template
4769 class. */
4770 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4771 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4772 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4773 }
4774 }
4775 }
4776 else
4777 {
4778 tree a, t, current, parms;
4779 int i;
4780 tree tinfo = get_template_info (decl);
4781
4782 if (!tinfo)
4783 {
4784 error ("template definition of non-template %q#D", decl);
4785 return error_mark_node;
4786 }
4787
4788 tmpl = TI_TEMPLATE (tinfo);
4789
4790 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4791 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4792 && DECL_TEMPLATE_SPECIALIZATION (decl)
4793 && DECL_MEMBER_TEMPLATE_P (tmpl))
4794 {
4795 tree new_tmpl;
4796
4797 /* The declaration is a specialization of a member
4798 template, declared outside the class. Therefore, the
4799 innermost template arguments will be NULL, so we
4800 replace them with the arguments determined by the
4801 earlier call to check_explicit_specialization. */
4802 args = DECL_TI_ARGS (decl);
4803
4804 new_tmpl
4805 = build_template_decl (decl, current_template_parms,
4806 member_template_p);
4807 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4808 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4809 DECL_TI_TEMPLATE (decl) = new_tmpl;
4810 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4811 DECL_TEMPLATE_INFO (new_tmpl)
4812 = build_template_info (tmpl, args);
4813
4814 register_specialization (new_tmpl,
4815 most_general_template (tmpl),
4816 args,
4817 is_friend, 0);
4818 return decl;
4819 }
4820
4821 /* Make sure the template headers we got make sense. */
4822
4823 parms = DECL_TEMPLATE_PARMS (tmpl);
4824 i = TMPL_PARMS_DEPTH (parms);
4825 if (TMPL_ARGS_DEPTH (args) != i)
4826 {
4827 error ("expected %d levels of template parms for %q#D, got %d",
4828 i, decl, TMPL_ARGS_DEPTH (args));
4829 }
4830 else
4831 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4832 {
4833 a = TMPL_ARGS_LEVEL (args, i);
4834 t = INNERMOST_TEMPLATE_PARMS (parms);
4835
4836 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4837 {
4838 if (current == decl)
4839 error ("got %d template parameters for %q#D",
4840 TREE_VEC_LENGTH (a), decl);
4841 else
4842 error ("got %d template parameters for %q#T",
4843 TREE_VEC_LENGTH (a), current);
4844 error (" but %d required", TREE_VEC_LENGTH (t));
4845 /* Avoid crash in import_export_decl. */
4846 DECL_INTERFACE_KNOWN (decl) = 1;
4847 return error_mark_node;
4848 }
4849
4850 if (current == decl)
4851 current = ctx;
4852 else if (current == NULL_TREE)
4853 /* Can happen in erroneous input. */
4854 break;
4855 else
4856 current = get_containing_scope (current);
4857 }
4858
4859 /* Check that the parms are used in the appropriate qualifying scopes
4860 in the declarator. */
4861 if (!comp_template_args
4862 (TI_ARGS (tinfo),
4863 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4864 {
4865 error ("\
4866 template arguments to %qD do not match original template %qD",
4867 decl, DECL_TEMPLATE_RESULT (tmpl));
4868 if (!uses_template_parms (TI_ARGS (tinfo)))
4869 inform (input_location, "use template<> for an explicit specialization");
4870 /* Avoid crash in import_export_decl. */
4871 DECL_INTERFACE_KNOWN (decl) = 1;
4872 return error_mark_node;
4873 }
4874 }
4875
4876 DECL_TEMPLATE_RESULT (tmpl) = decl;
4877 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4878
4879 /* Push template declarations for global functions and types. Note
4880 that we do not try to push a global template friend declared in a
4881 template class; such a thing may well depend on the template
4882 parameters of the class. */
4883 if (new_template_p && !ctx
4884 && !(is_friend && template_class_depth (current_class_type) > 0))
4885 {
4886 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4887 if (tmpl == error_mark_node)
4888 return error_mark_node;
4889
4890 /* Hide template friend classes that haven't been declared yet. */
4891 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4892 {
4893 DECL_ANTICIPATED (tmpl) = 1;
4894 DECL_FRIEND_P (tmpl) = 1;
4895 }
4896 }
4897
4898 if (is_primary)
4899 {
4900 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4901 int i;
4902
4903 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4904 if (DECL_CONV_FN_P (tmpl))
4905 {
4906 int depth = TMPL_PARMS_DEPTH (parms);
4907
4908 /* It is a conversion operator. See if the type converted to
4909 depends on innermost template operands. */
4910
4911 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4912 depth))
4913 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4914 }
4915
4916 /* Give template template parms a DECL_CONTEXT of the template
4917 for which they are a parameter. */
4918 parms = INNERMOST_TEMPLATE_PARMS (parms);
4919 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4920 {
4921 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4922 if (TREE_CODE (parm) == TEMPLATE_DECL)
4923 DECL_CONTEXT (parm) = tmpl;
4924 }
4925 }
4926
4927 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4928 back to its most general template. If TMPL is a specialization,
4929 ARGS may only have the innermost set of arguments. Add the missing
4930 argument levels if necessary. */
4931 if (DECL_TEMPLATE_INFO (tmpl))
4932 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4933
4934 info = build_template_info (tmpl, args);
4935
4936 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4937 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4938 else
4939 {
4940 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4941 retrofit_lang_decl (decl);
4942 if (DECL_LANG_SPECIFIC (decl))
4943 DECL_TEMPLATE_INFO (decl) = info;
4944 }
4945
4946 return DECL_TEMPLATE_RESULT (tmpl);
4947 }
4948
4949 tree
4950 push_template_decl (tree decl)
4951 {
4952 return push_template_decl_real (decl, false);
4953 }
4954
4955 /* FN is an inheriting constructor that inherits from the constructor
4956 template INHERITED; turn FN into a constructor template with a matching
4957 template header. */
4958
4959 tree
4960 add_inherited_template_parms (tree fn, tree inherited)
4961 {
4962 tree inner_parms
4963 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4964 inner_parms = copy_node (inner_parms);
4965 tree parms
4966 = tree_cons (size_int (processing_template_decl + 1),
4967 inner_parms, current_template_parms);
4968 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4969 tree args = template_parms_to_args (parms);
4970 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4971 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4972 DECL_TEMPLATE_RESULT (tmpl) = fn;
4973 DECL_ARTIFICIAL (tmpl) = true;
4974 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4975 return tmpl;
4976 }
4977
4978 /* Called when a class template TYPE is redeclared with the indicated
4979 template PARMS, e.g.:
4980
4981 template <class T> struct S;
4982 template <class T> struct S {}; */
4983
4984 bool
4985 redeclare_class_template (tree type, tree parms)
4986 {
4987 tree tmpl;
4988 tree tmpl_parms;
4989 int i;
4990
4991 if (!TYPE_TEMPLATE_INFO (type))
4992 {
4993 error ("%qT is not a template type", type);
4994 return false;
4995 }
4996
4997 tmpl = TYPE_TI_TEMPLATE (type);
4998 if (!PRIMARY_TEMPLATE_P (tmpl))
4999 /* The type is nested in some template class. Nothing to worry
5000 about here; there are no new template parameters for the nested
5001 type. */
5002 return true;
5003
5004 if (!parms)
5005 {
5006 error ("template specifiers not specified in declaration of %qD",
5007 tmpl);
5008 return false;
5009 }
5010
5011 parms = INNERMOST_TEMPLATE_PARMS (parms);
5012 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5013
5014 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5015 {
5016 error_n (input_location, TREE_VEC_LENGTH (parms),
5017 "redeclared with %d template parameter",
5018 "redeclared with %d template parameters",
5019 TREE_VEC_LENGTH (parms));
5020 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5021 "previous declaration %q+D used %d template parameter",
5022 "previous declaration %q+D used %d template parameters",
5023 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5024 return false;
5025 }
5026
5027 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5028 {
5029 tree tmpl_parm;
5030 tree parm;
5031 tree tmpl_default;
5032 tree parm_default;
5033
5034 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5035 || TREE_VEC_ELT (parms, i) == error_mark_node)
5036 continue;
5037
5038 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5039 if (tmpl_parm == error_mark_node)
5040 return false;
5041
5042 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5043 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5044 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5045
5046 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5047 TEMPLATE_DECL. */
5048 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5049 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5050 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5051 || (TREE_CODE (tmpl_parm) != PARM_DECL
5052 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5053 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5054 || (TREE_CODE (tmpl_parm) == PARM_DECL
5055 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5056 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5057 {
5058 error ("template parameter %q+#D", tmpl_parm);
5059 error ("redeclared here as %q#D", parm);
5060 return false;
5061 }
5062
5063 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5064 {
5065 /* We have in [temp.param]:
5066
5067 A template-parameter may not be given default arguments
5068 by two different declarations in the same scope. */
5069 error_at (input_location, "redefinition of default argument for %q#D", parm);
5070 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5071 "original definition appeared here");
5072 return false;
5073 }
5074
5075 if (parm_default != NULL_TREE)
5076 /* Update the previous template parameters (which are the ones
5077 that will really count) with the new default value. */
5078 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5079 else if (tmpl_default != NULL_TREE)
5080 /* Update the new parameters, too; they'll be used as the
5081 parameters for any members. */
5082 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5083 }
5084
5085 return true;
5086 }
5087
5088 /* Simplify EXPR if it is a non-dependent expression. Returns the
5089 (possibly simplified) expression. */
5090
5091 tree
5092 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5093 {
5094 if (expr == NULL_TREE)
5095 return NULL_TREE;
5096
5097 /* If we're in a template, but EXPR isn't value dependent, simplify
5098 it. We're supposed to treat:
5099
5100 template <typename T> void f(T[1 + 1]);
5101 template <typename T> void f(T[2]);
5102
5103 as two declarations of the same function, for example. */
5104 if (processing_template_decl
5105 && !instantiation_dependent_expression_p (expr)
5106 && potential_constant_expression (expr))
5107 {
5108 HOST_WIDE_INT saved_processing_template_decl;
5109
5110 saved_processing_template_decl = processing_template_decl;
5111 processing_template_decl = 0;
5112 expr = tsubst_copy_and_build (expr,
5113 /*args=*/NULL_TREE,
5114 complain,
5115 /*in_decl=*/NULL_TREE,
5116 /*function_p=*/false,
5117 /*integral_constant_expression_p=*/true);
5118 processing_template_decl = saved_processing_template_decl;
5119 }
5120 return expr;
5121 }
5122
5123 tree
5124 fold_non_dependent_expr (tree expr)
5125 {
5126 return fold_non_dependent_expr_sfinae (expr, tf_error);
5127 }
5128
5129 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5130 template declaration, or a TYPE_DECL for an alias declaration. */
5131
5132 bool
5133 alias_type_or_template_p (tree t)
5134 {
5135 if (t == NULL_TREE)
5136 return false;
5137 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5138 || (TYPE_P (t)
5139 && TYPE_NAME (t)
5140 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5141 || DECL_ALIAS_TEMPLATE_P (t));
5142 }
5143
5144 /* Return TRUE iff is a specialization of an alias template. */
5145
5146 bool
5147 alias_template_specialization_p (const_tree t)
5148 {
5149 if (t == NULL_TREE)
5150 return false;
5151
5152 return (TYPE_P (t)
5153 && TYPE_TEMPLATE_INFO (t)
5154 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5155 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5156 }
5157
5158 /* Return the number of innermost template parameters in TMPL. */
5159
5160 static int
5161 num_innermost_template_parms (tree tmpl)
5162 {
5163 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5164 return TREE_VEC_LENGTH (parms);
5165 }
5166
5167 /* Return either TMPL or another template that it is equivalent to under DR
5168 1286: An alias that just changes the name of a template is equivalent to
5169 the other template. */
5170
5171 static tree
5172 get_underlying_template (tree tmpl)
5173 {
5174 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5175 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5176 {
5177 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5178 if (TYPE_TEMPLATE_INFO (result))
5179 {
5180 tree sub = TYPE_TI_TEMPLATE (result);
5181 if (PRIMARY_TEMPLATE_P (sub)
5182 && (num_innermost_template_parms (tmpl)
5183 == num_innermost_template_parms (sub))
5184 && same_type_p (result, TREE_TYPE (sub)))
5185 {
5186 /* The alias type is equivalent to the pattern of the
5187 underlying template, so strip the alias. */
5188 tmpl = sub;
5189 continue;
5190 }
5191 }
5192 break;
5193 }
5194 return tmpl;
5195 }
5196
5197 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5198 must be a function or a pointer-to-function type, as specified
5199 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5200 and check that the resulting function has external linkage. */
5201
5202 static tree
5203 convert_nontype_argument_function (tree type, tree expr)
5204 {
5205 tree fns = expr;
5206 tree fn, fn_no_ptr;
5207 linkage_kind linkage;
5208
5209 fn = instantiate_type (type, fns, tf_none);
5210 if (fn == error_mark_node)
5211 return error_mark_node;
5212
5213 fn_no_ptr = fn;
5214 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5215 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5216 if (BASELINK_P (fn_no_ptr))
5217 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5218
5219 /* [temp.arg.nontype]/1
5220
5221 A template-argument for a non-type, non-template template-parameter
5222 shall be one of:
5223 [...]
5224 -- the address of an object or function with external [C++11: or
5225 internal] linkage. */
5226
5227 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5228 {
5229 error ("%qE is not a valid template argument for type %qT", expr, type);
5230 if (TYPE_PTR_P (type))
5231 error ("it must be the address of a function with external linkage");
5232 else
5233 error ("it must be the name of a function with external linkage");
5234 return NULL_TREE;
5235 }
5236
5237 linkage = decl_linkage (fn_no_ptr);
5238 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5239 {
5240 if (cxx_dialect >= cxx11)
5241 error ("%qE is not a valid template argument for type %qT "
5242 "because %qD has no linkage",
5243 expr, type, fn_no_ptr);
5244 else
5245 error ("%qE is not a valid template argument for type %qT "
5246 "because %qD does not have external linkage",
5247 expr, type, fn_no_ptr);
5248 return NULL_TREE;
5249 }
5250
5251 return fn;
5252 }
5253
5254 /* Subroutine of convert_nontype_argument.
5255 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5256 Emit an error otherwise. */
5257
5258 static bool
5259 check_valid_ptrmem_cst_expr (tree type, tree expr,
5260 tsubst_flags_t complain)
5261 {
5262 STRIP_NOPS (expr);
5263 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5264 return true;
5265 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5266 return true;
5267 if (complain & tf_error)
5268 {
5269 error ("%qE is not a valid template argument for type %qT",
5270 expr, type);
5271 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5272 }
5273 return false;
5274 }
5275
5276 /* Returns TRUE iff the address of OP is value-dependent.
5277
5278 14.6.2.4 [temp.dep.temp]:
5279 A non-integral non-type template-argument is dependent if its type is
5280 dependent or it has either of the following forms
5281 qualified-id
5282 & qualified-id
5283 and contains a nested-name-specifier which specifies a class-name that
5284 names a dependent type.
5285
5286 We generalize this to just say that the address of a member of a
5287 dependent class is value-dependent; the above doesn't cover the
5288 address of a static data member named with an unqualified-id. */
5289
5290 static bool
5291 has_value_dependent_address (tree op)
5292 {
5293 /* We could use get_inner_reference here, but there's no need;
5294 this is only relevant for template non-type arguments, which
5295 can only be expressed as &id-expression. */
5296 if (DECL_P (op))
5297 {
5298 tree ctx = CP_DECL_CONTEXT (op);
5299 if (TYPE_P (ctx) && dependent_type_p (ctx))
5300 return true;
5301 }
5302
5303 return false;
5304 }
5305
5306 /* The next set of functions are used for providing helpful explanatory
5307 diagnostics for failed overload resolution. Their messages should be
5308 indented by two spaces for consistency with the messages in
5309 call.c */
5310
5311 static int
5312 unify_success (bool /*explain_p*/)
5313 {
5314 return 0;
5315 }
5316
5317 static int
5318 unify_parameter_deduction_failure (bool explain_p, tree parm)
5319 {
5320 if (explain_p)
5321 inform (input_location,
5322 " couldn't deduce template parameter %qD", parm);
5323 return 1;
5324 }
5325
5326 static int
5327 unify_invalid (bool /*explain_p*/)
5328 {
5329 return 1;
5330 }
5331
5332 static int
5333 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5334 {
5335 if (explain_p)
5336 inform (input_location,
5337 " types %qT and %qT have incompatible cv-qualifiers",
5338 parm, arg);
5339 return 1;
5340 }
5341
5342 static int
5343 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5344 {
5345 if (explain_p)
5346 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5347 return 1;
5348 }
5349
5350 static int
5351 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5352 {
5353 if (explain_p)
5354 inform (input_location,
5355 " template parameter %qD is not a parameter pack, but "
5356 "argument %qD is",
5357 parm, arg);
5358 return 1;
5359 }
5360
5361 static int
5362 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5363 {
5364 if (explain_p)
5365 inform (input_location,
5366 " template argument %qE does not match "
5367 "pointer-to-member constant %qE",
5368 arg, parm);
5369 return 1;
5370 }
5371
5372 static int
5373 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5374 {
5375 if (explain_p)
5376 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5377 return 1;
5378 }
5379
5380 static int
5381 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5382 {
5383 if (explain_p)
5384 inform (input_location,
5385 " inconsistent parameter pack deduction with %qT and %qT",
5386 old_arg, new_arg);
5387 return 1;
5388 }
5389
5390 static int
5391 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5392 {
5393 if (explain_p)
5394 {
5395 if (TYPE_P (parm))
5396 inform (input_location,
5397 " deduced conflicting types for parameter %qT (%qT and %qT)",
5398 parm, first, second);
5399 else
5400 inform (input_location,
5401 " deduced conflicting values for non-type parameter "
5402 "%qE (%qE and %qE)", parm, first, second);
5403 }
5404 return 1;
5405 }
5406
5407 static int
5408 unify_vla_arg (bool explain_p, tree arg)
5409 {
5410 if (explain_p)
5411 inform (input_location,
5412 " variable-sized array type %qT is not "
5413 "a valid template argument",
5414 arg);
5415 return 1;
5416 }
5417
5418 static int
5419 unify_method_type_error (bool explain_p, tree arg)
5420 {
5421 if (explain_p)
5422 inform (input_location,
5423 " member function type %qT is not a valid template argument",
5424 arg);
5425 return 1;
5426 }
5427
5428 static int
5429 unify_arity (bool explain_p, int have, int wanted)
5430 {
5431 if (explain_p)
5432 inform_n (input_location, wanted,
5433 " candidate expects %d argument, %d provided",
5434 " candidate expects %d arguments, %d provided",
5435 wanted, have);
5436 return 1;
5437 }
5438
5439 static int
5440 unify_too_many_arguments (bool explain_p, int have, int wanted)
5441 {
5442 return unify_arity (explain_p, have, wanted);
5443 }
5444
5445 static int
5446 unify_too_few_arguments (bool explain_p, int have, int wanted)
5447 {
5448 return unify_arity (explain_p, have, wanted);
5449 }
5450
5451 static int
5452 unify_arg_conversion (bool explain_p, tree to_type,
5453 tree from_type, tree arg)
5454 {
5455 if (explain_p)
5456 inform (EXPR_LOC_OR_LOC (arg, input_location),
5457 " cannot convert %qE (type %qT) to type %qT",
5458 arg, from_type, to_type);
5459 return 1;
5460 }
5461
5462 static int
5463 unify_no_common_base (bool explain_p, enum template_base_result r,
5464 tree parm, tree arg)
5465 {
5466 if (explain_p)
5467 switch (r)
5468 {
5469 case tbr_ambiguous_baseclass:
5470 inform (input_location, " %qT is an ambiguous base class of %qT",
5471 parm, arg);
5472 break;
5473 default:
5474 inform (input_location, " %qT is not derived from %qT", arg, parm);
5475 break;
5476 }
5477 return 1;
5478 }
5479
5480 static int
5481 unify_inconsistent_template_template_parameters (bool explain_p)
5482 {
5483 if (explain_p)
5484 inform (input_location,
5485 " template parameters of a template template argument are "
5486 "inconsistent with other deduced template arguments");
5487 return 1;
5488 }
5489
5490 static int
5491 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5492 {
5493 if (explain_p)
5494 inform (input_location,
5495 " can't deduce a template for %qT from non-template type %qT",
5496 parm, arg);
5497 return 1;
5498 }
5499
5500 static int
5501 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5502 {
5503 if (explain_p)
5504 inform (input_location,
5505 " template argument %qE does not match %qD", arg, parm);
5506 return 1;
5507 }
5508
5509 static int
5510 unify_overload_resolution_failure (bool explain_p, tree arg)
5511 {
5512 if (explain_p)
5513 inform (input_location,
5514 " could not resolve address from overloaded function %qE",
5515 arg);
5516 return 1;
5517 }
5518
5519 /* Attempt to convert the non-type template parameter EXPR to the
5520 indicated TYPE. If the conversion is successful, return the
5521 converted value. If the conversion is unsuccessful, return
5522 NULL_TREE if we issued an error message, or error_mark_node if we
5523 did not. We issue error messages for out-and-out bad template
5524 parameters, but not simply because the conversion failed, since we
5525 might be just trying to do argument deduction. Both TYPE and EXPR
5526 must be non-dependent.
5527
5528 The conversion follows the special rules described in
5529 [temp.arg.nontype], and it is much more strict than an implicit
5530 conversion.
5531
5532 This function is called twice for each template argument (see
5533 lookup_template_class for a more accurate description of this
5534 problem). This means that we need to handle expressions which
5535 are not valid in a C++ source, but can be created from the
5536 first call (for instance, casts to perform conversions). These
5537 hacks can go away after we fix the double coercion problem. */
5538
5539 static tree
5540 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5541 {
5542 tree expr_type;
5543
5544 /* Detect immediately string literals as invalid non-type argument.
5545 This special-case is not needed for correctness (we would easily
5546 catch this later), but only to provide better diagnostic for this
5547 common user mistake. As suggested by DR 100, we do not mention
5548 linkage issues in the diagnostic as this is not the point. */
5549 /* FIXME we're making this OK. */
5550 if (TREE_CODE (expr) == STRING_CST)
5551 {
5552 if (complain & tf_error)
5553 error ("%qE is not a valid template argument for type %qT "
5554 "because string literals can never be used in this context",
5555 expr, type);
5556 return NULL_TREE;
5557 }
5558
5559 /* Add the ADDR_EXPR now for the benefit of
5560 value_dependent_expression_p. */
5561 if (TYPE_PTROBV_P (type)
5562 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5563 {
5564 expr = decay_conversion (expr, complain);
5565 if (expr == error_mark_node)
5566 return error_mark_node;
5567 }
5568
5569 /* If we are in a template, EXPR may be non-dependent, but still
5570 have a syntactic, rather than semantic, form. For example, EXPR
5571 might be a SCOPE_REF, rather than the VAR_DECL to which the
5572 SCOPE_REF refers. Preserving the qualifying scope is necessary
5573 so that access checking can be performed when the template is
5574 instantiated -- but here we need the resolved form so that we can
5575 convert the argument. */
5576 if (TYPE_REF_OBJ_P (type)
5577 && has_value_dependent_address (expr))
5578 /* If we want the address and it's value-dependent, don't fold. */;
5579 else if (!type_unknown_p (expr))
5580 expr = fold_non_dependent_expr_sfinae (expr, complain);
5581 if (error_operand_p (expr))
5582 return error_mark_node;
5583 expr_type = TREE_TYPE (expr);
5584 if (TREE_CODE (type) == REFERENCE_TYPE)
5585 expr = mark_lvalue_use (expr);
5586 else
5587 expr = mark_rvalue_use (expr);
5588
5589 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5590 to a non-type argument of "nullptr". */
5591 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5592 expr = convert (type, expr);
5593
5594 /* In C++11, integral or enumeration non-type template arguments can be
5595 arbitrary constant expressions. Pointer and pointer to
5596 member arguments can be general constant expressions that evaluate
5597 to a null value, but otherwise still need to be of a specific form. */
5598 if (cxx_dialect >= cxx11)
5599 {
5600 if (TREE_CODE (expr) == PTRMEM_CST)
5601 /* A PTRMEM_CST is already constant, and a valid template
5602 argument for a parameter of pointer to member type, we just want
5603 to leave it in that form rather than lower it to a
5604 CONSTRUCTOR. */;
5605 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5606 expr = maybe_constant_value (expr);
5607 else if (TYPE_PTR_OR_PTRMEM_P (type))
5608 {
5609 tree folded = maybe_constant_value (expr);
5610 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5611 : null_member_pointer_value_p (folded))
5612 expr = folded;
5613 }
5614 }
5615
5616 /* HACK: Due to double coercion, we can get a
5617 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5618 which is the tree that we built on the first call (see
5619 below when coercing to reference to object or to reference to
5620 function). We just strip everything and get to the arg.
5621 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5622 for examples. */
5623 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5624 {
5625 tree probe_type, probe = expr;
5626 if (REFERENCE_REF_P (probe))
5627 probe = TREE_OPERAND (probe, 0);
5628 probe_type = TREE_TYPE (probe);
5629 if (TREE_CODE (probe) == NOP_EXPR)
5630 {
5631 /* ??? Maybe we could use convert_from_reference here, but we
5632 would need to relax its constraints because the NOP_EXPR
5633 could actually change the type to something more cv-qualified,
5634 and this is not folded by convert_from_reference. */
5635 tree addr = TREE_OPERAND (probe, 0);
5636 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5637 && TREE_CODE (addr) == ADDR_EXPR
5638 && TYPE_PTR_P (TREE_TYPE (addr))
5639 && (same_type_ignoring_top_level_qualifiers_p
5640 (TREE_TYPE (probe_type),
5641 TREE_TYPE (TREE_TYPE (addr)))))
5642 {
5643 expr = TREE_OPERAND (addr, 0);
5644 expr_type = TREE_TYPE (probe_type);
5645 }
5646 }
5647 }
5648
5649 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5650 parameter is a pointer to object, through decay and
5651 qualification conversion. Let's strip everything. */
5652 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5653 {
5654 tree probe = expr;
5655 STRIP_NOPS (probe);
5656 if (TREE_CODE (probe) == ADDR_EXPR
5657 && TYPE_PTR_P (TREE_TYPE (probe)))
5658 {
5659 /* Skip the ADDR_EXPR only if it is part of the decay for
5660 an array. Otherwise, it is part of the original argument
5661 in the source code. */
5662 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5663 probe = TREE_OPERAND (probe, 0);
5664 expr = probe;
5665 expr_type = TREE_TYPE (expr);
5666 }
5667 }
5668
5669 /* [temp.arg.nontype]/5, bullet 1
5670
5671 For a non-type template-parameter of integral or enumeration type,
5672 integral promotions (_conv.prom_) and integral conversions
5673 (_conv.integral_) are applied. */
5674 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5675 {
5676 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5677 t = maybe_constant_value (t);
5678 if (t != error_mark_node)
5679 expr = t;
5680
5681 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5682 return error_mark_node;
5683
5684 /* Notice that there are constant expressions like '4 % 0' which
5685 do not fold into integer constants. */
5686 if (TREE_CODE (expr) != INTEGER_CST)
5687 {
5688 if (complain & tf_error)
5689 {
5690 int errs = errorcount, warns = warningcount + werrorcount;
5691 if (processing_template_decl
5692 && !require_potential_constant_expression (expr))
5693 return NULL_TREE;
5694 expr = cxx_constant_value (expr);
5695 if (errorcount > errs || warningcount + werrorcount > warns)
5696 inform (EXPR_LOC_OR_LOC (expr, input_location),
5697 "in template argument for type %qT ", type);
5698 if (expr == error_mark_node)
5699 return NULL_TREE;
5700 /* else cxx_constant_value complained but gave us
5701 a real constant, so go ahead. */
5702 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5703 }
5704 else
5705 return NULL_TREE;
5706 }
5707
5708 /* Avoid typedef problems. */
5709 if (TREE_TYPE (expr) != type)
5710 expr = fold_convert (type, expr);
5711 }
5712 /* [temp.arg.nontype]/5, bullet 2
5713
5714 For a non-type template-parameter of type pointer to object,
5715 qualification conversions (_conv.qual_) and the array-to-pointer
5716 conversion (_conv.array_) are applied. */
5717 else if (TYPE_PTROBV_P (type))
5718 {
5719 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5720
5721 A template-argument for a non-type, non-template template-parameter
5722 shall be one of: [...]
5723
5724 -- the name of a non-type template-parameter;
5725 -- the address of an object or function with external linkage, [...]
5726 expressed as "& id-expression" where the & is optional if the name
5727 refers to a function or array, or if the corresponding
5728 template-parameter is a reference.
5729
5730 Here, we do not care about functions, as they are invalid anyway
5731 for a parameter of type pointer-to-object. */
5732
5733 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5734 /* Non-type template parameters are OK. */
5735 ;
5736 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5737 /* Null pointer values are OK in C++11. */;
5738 else if (TREE_CODE (expr) != ADDR_EXPR
5739 && TREE_CODE (expr_type) != ARRAY_TYPE)
5740 {
5741 if (VAR_P (expr))
5742 {
5743 error ("%qD is not a valid template argument "
5744 "because %qD is a variable, not the address of "
5745 "a variable",
5746 expr, expr);
5747 return NULL_TREE;
5748 }
5749 if (POINTER_TYPE_P (expr_type))
5750 {
5751 error ("%qE is not a valid template argument for %qT "
5752 "because it is not the address of a variable",
5753 expr, type);
5754 return NULL_TREE;
5755 }
5756 /* Other values, like integer constants, might be valid
5757 non-type arguments of some other type. */
5758 return error_mark_node;
5759 }
5760 else
5761 {
5762 tree decl;
5763
5764 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5765 ? TREE_OPERAND (expr, 0) : expr);
5766 if (!VAR_P (decl))
5767 {
5768 error ("%qE is not a valid template argument of type %qT "
5769 "because %qE is not a variable",
5770 expr, type, decl);
5771 return NULL_TREE;
5772 }
5773 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5774 {
5775 error ("%qE is not a valid template argument of type %qT "
5776 "because %qD does not have external linkage",
5777 expr, type, decl);
5778 return NULL_TREE;
5779 }
5780 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5781 {
5782 error ("%qE is not a valid template argument of type %qT "
5783 "because %qD has no linkage",
5784 expr, type, decl);
5785 return NULL_TREE;
5786 }
5787 }
5788
5789 expr = decay_conversion (expr, complain);
5790 if (expr == error_mark_node)
5791 return error_mark_node;
5792
5793 expr = perform_qualification_conversions (type, expr);
5794 if (expr == error_mark_node)
5795 return error_mark_node;
5796 }
5797 /* [temp.arg.nontype]/5, bullet 3
5798
5799 For a non-type template-parameter of type reference to object, no
5800 conversions apply. The type referred to by the reference may be more
5801 cv-qualified than the (otherwise identical) type of the
5802 template-argument. The template-parameter is bound directly to the
5803 template-argument, which must be an lvalue. */
5804 else if (TYPE_REF_OBJ_P (type))
5805 {
5806 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5807 expr_type))
5808 return error_mark_node;
5809
5810 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5811 {
5812 error ("%qE is not a valid template argument for type %qT "
5813 "because of conflicts in cv-qualification", expr, type);
5814 return NULL_TREE;
5815 }
5816
5817 if (!real_lvalue_p (expr))
5818 {
5819 error ("%qE is not a valid template argument for type %qT "
5820 "because it is not an lvalue", expr, type);
5821 return NULL_TREE;
5822 }
5823
5824 /* [temp.arg.nontype]/1
5825
5826 A template-argument for a non-type, non-template template-parameter
5827 shall be one of: [...]
5828
5829 -- the address of an object or function with external linkage. */
5830 if (INDIRECT_REF_P (expr)
5831 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5832 {
5833 expr = TREE_OPERAND (expr, 0);
5834 if (DECL_P (expr))
5835 {
5836 error ("%q#D is not a valid template argument for type %qT "
5837 "because a reference variable does not have a constant "
5838 "address", expr, type);
5839 return NULL_TREE;
5840 }
5841 }
5842
5843 if (!DECL_P (expr))
5844 {
5845 error ("%qE is not a valid template argument for type %qT "
5846 "because it is not an object with external linkage",
5847 expr, type);
5848 return NULL_TREE;
5849 }
5850
5851 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5852 {
5853 error ("%qE is not a valid template argument for type %qT "
5854 "because object %qD has not external linkage",
5855 expr, type, expr);
5856 return NULL_TREE;
5857 }
5858
5859 expr = build_nop (type, build_address (expr));
5860 }
5861 /* [temp.arg.nontype]/5, bullet 4
5862
5863 For a non-type template-parameter of type pointer to function, only
5864 the function-to-pointer conversion (_conv.func_) is applied. If the
5865 template-argument represents a set of overloaded functions (or a
5866 pointer to such), the matching function is selected from the set
5867 (_over.over_). */
5868 else if (TYPE_PTRFN_P (type))
5869 {
5870 /* If the argument is a template-id, we might not have enough
5871 context information to decay the pointer. */
5872 if (!type_unknown_p (expr_type))
5873 {
5874 expr = decay_conversion (expr, complain);
5875 if (expr == error_mark_node)
5876 return error_mark_node;
5877 }
5878
5879 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5880 /* Null pointer values are OK in C++11. */
5881 return perform_qualification_conversions (type, expr);
5882
5883 expr = convert_nontype_argument_function (type, expr);
5884 if (!expr || expr == error_mark_node)
5885 return expr;
5886 }
5887 /* [temp.arg.nontype]/5, bullet 5
5888
5889 For a non-type template-parameter of type reference to function, no
5890 conversions apply. If the template-argument represents a set of
5891 overloaded functions, the matching function is selected from the set
5892 (_over.over_). */
5893 else if (TYPE_REFFN_P (type))
5894 {
5895 if (TREE_CODE (expr) == ADDR_EXPR)
5896 {
5897 error ("%qE is not a valid template argument for type %qT "
5898 "because it is a pointer", expr, type);
5899 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5900 return NULL_TREE;
5901 }
5902
5903 expr = convert_nontype_argument_function (type, expr);
5904 if (!expr || expr == error_mark_node)
5905 return expr;
5906
5907 expr = build_nop (type, build_address (expr));
5908 }
5909 /* [temp.arg.nontype]/5, bullet 6
5910
5911 For a non-type template-parameter of type pointer to member function,
5912 no conversions apply. If the template-argument represents a set of
5913 overloaded member functions, the matching member function is selected
5914 from the set (_over.over_). */
5915 else if (TYPE_PTRMEMFUNC_P (type))
5916 {
5917 expr = instantiate_type (type, expr, tf_none);
5918 if (expr == error_mark_node)
5919 return error_mark_node;
5920
5921 /* [temp.arg.nontype] bullet 1 says the pointer to member
5922 expression must be a pointer-to-member constant. */
5923 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5924 return error_mark_node;
5925
5926 /* There is no way to disable standard conversions in
5927 resolve_address_of_overloaded_function (called by
5928 instantiate_type). It is possible that the call succeeded by
5929 converting &B::I to &D::I (where B is a base of D), so we need
5930 to reject this conversion here.
5931
5932 Actually, even if there was a way to disable standard conversions,
5933 it would still be better to reject them here so that we can
5934 provide a superior diagnostic. */
5935 if (!same_type_p (TREE_TYPE (expr), type))
5936 {
5937 error ("%qE is not a valid template argument for type %qT "
5938 "because it is of type %qT", expr, type,
5939 TREE_TYPE (expr));
5940 /* If we are just one standard conversion off, explain. */
5941 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5942 inform (input_location,
5943 "standard conversions are not allowed in this context");
5944 return NULL_TREE;
5945 }
5946 }
5947 /* [temp.arg.nontype]/5, bullet 7
5948
5949 For a non-type template-parameter of type pointer to data member,
5950 qualification conversions (_conv.qual_) are applied. */
5951 else if (TYPE_PTRDATAMEM_P (type))
5952 {
5953 /* [temp.arg.nontype] bullet 1 says the pointer to member
5954 expression must be a pointer-to-member constant. */
5955 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5956 return error_mark_node;
5957
5958 expr = perform_qualification_conversions (type, expr);
5959 if (expr == error_mark_node)
5960 return expr;
5961 }
5962 else if (NULLPTR_TYPE_P (type))
5963 {
5964 if (expr != nullptr_node)
5965 {
5966 error ("%qE is not a valid template argument for type %qT "
5967 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5968 return NULL_TREE;
5969 }
5970 return expr;
5971 }
5972 /* A template non-type parameter must be one of the above. */
5973 else
5974 gcc_unreachable ();
5975
5976 /* Sanity check: did we actually convert the argument to the
5977 right type? */
5978 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5979 (type, TREE_TYPE (expr)));
5980 return expr;
5981 }
5982
5983 /* Subroutine of coerce_template_template_parms, which returns 1 if
5984 PARM_PARM and ARG_PARM match using the rule for the template
5985 parameters of template template parameters. Both PARM and ARG are
5986 template parameters; the rest of the arguments are the same as for
5987 coerce_template_template_parms.
5988 */
5989 static int
5990 coerce_template_template_parm (tree parm,
5991 tree arg,
5992 tsubst_flags_t complain,
5993 tree in_decl,
5994 tree outer_args)
5995 {
5996 if (arg == NULL_TREE || arg == error_mark_node
5997 || parm == NULL_TREE || parm == error_mark_node)
5998 return 0;
5999
6000 if (TREE_CODE (arg) != TREE_CODE (parm))
6001 return 0;
6002
6003 switch (TREE_CODE (parm))
6004 {
6005 case TEMPLATE_DECL:
6006 /* We encounter instantiations of templates like
6007 template <template <template <class> class> class TT>
6008 class C; */
6009 {
6010 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6011 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6012
6013 if (!coerce_template_template_parms
6014 (parmparm, argparm, complain, in_decl, outer_args))
6015 return 0;
6016 }
6017 /* Fall through. */
6018
6019 case TYPE_DECL:
6020 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6021 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6022 /* Argument is a parameter pack but parameter is not. */
6023 return 0;
6024 break;
6025
6026 case PARM_DECL:
6027 /* The tsubst call is used to handle cases such as
6028
6029 template <int> class C {};
6030 template <class T, template <T> class TT> class D {};
6031 D<int, C> d;
6032
6033 i.e. the parameter list of TT depends on earlier parameters. */
6034 if (!uses_template_parms (TREE_TYPE (arg))
6035 && !same_type_p
6036 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6037 TREE_TYPE (arg)))
6038 return 0;
6039
6040 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6041 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6042 /* Argument is a parameter pack but parameter is not. */
6043 return 0;
6044
6045 break;
6046
6047 default:
6048 gcc_unreachable ();
6049 }
6050
6051 return 1;
6052 }
6053
6054
6055 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6056 template template parameters. Both PARM_PARMS and ARG_PARMS are
6057 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6058 or PARM_DECL.
6059
6060 Consider the example:
6061 template <class T> class A;
6062 template<template <class U> class TT> class B;
6063
6064 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6065 the parameters to A, and OUTER_ARGS contains A. */
6066
6067 static int
6068 coerce_template_template_parms (tree parm_parms,
6069 tree arg_parms,
6070 tsubst_flags_t complain,
6071 tree in_decl,
6072 tree outer_args)
6073 {
6074 int nparms, nargs, i;
6075 tree parm, arg;
6076 int variadic_p = 0;
6077
6078 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6079 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6080
6081 nparms = TREE_VEC_LENGTH (parm_parms);
6082 nargs = TREE_VEC_LENGTH (arg_parms);
6083
6084 /* Determine whether we have a parameter pack at the end of the
6085 template template parameter's template parameter list. */
6086 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6087 {
6088 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6089
6090 if (parm == error_mark_node)
6091 return 0;
6092
6093 switch (TREE_CODE (parm))
6094 {
6095 case TEMPLATE_DECL:
6096 case TYPE_DECL:
6097 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6098 variadic_p = 1;
6099 break;
6100
6101 case PARM_DECL:
6102 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6103 variadic_p = 1;
6104 break;
6105
6106 default:
6107 gcc_unreachable ();
6108 }
6109 }
6110
6111 if (nargs != nparms
6112 && !(variadic_p && nargs >= nparms - 1))
6113 return 0;
6114
6115 /* Check all of the template parameters except the parameter pack at
6116 the end (if any). */
6117 for (i = 0; i < nparms - variadic_p; ++i)
6118 {
6119 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6120 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6121 continue;
6122
6123 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6124 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6125
6126 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6127 outer_args))
6128 return 0;
6129
6130 }
6131
6132 if (variadic_p)
6133 {
6134 /* Check each of the template parameters in the template
6135 argument against the template parameter pack at the end of
6136 the template template parameter. */
6137 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6138 return 0;
6139
6140 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6141
6142 for (; i < nargs; ++i)
6143 {
6144 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6145 continue;
6146
6147 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6148
6149 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6150 outer_args))
6151 return 0;
6152 }
6153 }
6154
6155 return 1;
6156 }
6157
6158 /* Verifies that the deduced template arguments (in TARGS) for the
6159 template template parameters (in TPARMS) represent valid bindings,
6160 by comparing the template parameter list of each template argument
6161 to the template parameter list of its corresponding template
6162 template parameter, in accordance with DR150. This
6163 routine can only be called after all template arguments have been
6164 deduced. It will return TRUE if all of the template template
6165 parameter bindings are okay, FALSE otherwise. */
6166 bool
6167 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6168 {
6169 int i, ntparms = TREE_VEC_LENGTH (tparms);
6170 bool ret = true;
6171
6172 /* We're dealing with template parms in this process. */
6173 ++processing_template_decl;
6174
6175 targs = INNERMOST_TEMPLATE_ARGS (targs);
6176
6177 for (i = 0; i < ntparms; ++i)
6178 {
6179 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6180 tree targ = TREE_VEC_ELT (targs, i);
6181
6182 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6183 {
6184 tree packed_args = NULL_TREE;
6185 int idx, len = 1;
6186
6187 if (ARGUMENT_PACK_P (targ))
6188 {
6189 /* Look inside the argument pack. */
6190 packed_args = ARGUMENT_PACK_ARGS (targ);
6191 len = TREE_VEC_LENGTH (packed_args);
6192 }
6193
6194 for (idx = 0; idx < len; ++idx)
6195 {
6196 tree targ_parms = NULL_TREE;
6197
6198 if (packed_args)
6199 /* Extract the next argument from the argument
6200 pack. */
6201 targ = TREE_VEC_ELT (packed_args, idx);
6202
6203 if (PACK_EXPANSION_P (targ))
6204 /* Look at the pattern of the pack expansion. */
6205 targ = PACK_EXPANSION_PATTERN (targ);
6206
6207 /* Extract the template parameters from the template
6208 argument. */
6209 if (TREE_CODE (targ) == TEMPLATE_DECL)
6210 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6211 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6212 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6213
6214 /* Verify that we can coerce the template template
6215 parameters from the template argument to the template
6216 parameter. This requires an exact match. */
6217 if (targ_parms
6218 && !coerce_template_template_parms
6219 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6220 targ_parms,
6221 tf_none,
6222 tparm,
6223 targs))
6224 {
6225 ret = false;
6226 goto out;
6227 }
6228 }
6229 }
6230 }
6231
6232 out:
6233
6234 --processing_template_decl;
6235 return ret;
6236 }
6237
6238 /* Since type attributes aren't mangled, we need to strip them from
6239 template type arguments. */
6240
6241 static tree
6242 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6243 {
6244 tree mv;
6245 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6246 return arg;
6247 mv = TYPE_MAIN_VARIANT (arg);
6248 arg = strip_typedefs (arg);
6249 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6250 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6251 {
6252 if (complain & tf_warning)
6253 warning (0, "ignoring attributes on template argument %qT", arg);
6254 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6255 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6256 }
6257 return arg;
6258 }
6259
6260 /* Convert the indicated template ARG as necessary to match the
6261 indicated template PARM. Returns the converted ARG, or
6262 error_mark_node if the conversion was unsuccessful. Error and
6263 warning messages are issued under control of COMPLAIN. This
6264 conversion is for the Ith parameter in the parameter list. ARGS is
6265 the full set of template arguments deduced so far. */
6266
6267 static tree
6268 convert_template_argument (tree parm,
6269 tree arg,
6270 tree args,
6271 tsubst_flags_t complain,
6272 int i,
6273 tree in_decl)
6274 {
6275 tree orig_arg;
6276 tree val;
6277 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6278
6279 if (TREE_CODE (arg) == TREE_LIST
6280 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6281 {
6282 /* The template argument was the name of some
6283 member function. That's usually
6284 invalid, but static members are OK. In any
6285 case, grab the underlying fields/functions
6286 and issue an error later if required. */
6287 orig_arg = TREE_VALUE (arg);
6288 TREE_TYPE (arg) = unknown_type_node;
6289 }
6290
6291 orig_arg = arg;
6292
6293 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6294 requires_type = (TREE_CODE (parm) == TYPE_DECL
6295 || requires_tmpl_type);
6296
6297 /* When determining whether an argument pack expansion is a template,
6298 look at the pattern. */
6299 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6300 arg = PACK_EXPANSION_PATTERN (arg);
6301
6302 /* Deal with an injected-class-name used as a template template arg. */
6303 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6304 {
6305 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6306 if (TREE_CODE (t) == TEMPLATE_DECL)
6307 {
6308 if (cxx_dialect >= cxx11)
6309 /* OK under DR 1004. */;
6310 else if (complain & tf_warning_or_error)
6311 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6312 " used as template template argument", TYPE_NAME (arg));
6313 else if (flag_pedantic_errors)
6314 t = arg;
6315
6316 arg = t;
6317 }
6318 }
6319
6320 is_tmpl_type =
6321 ((TREE_CODE (arg) == TEMPLATE_DECL
6322 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6323 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6324 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6325 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6326
6327 if (is_tmpl_type
6328 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6329 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6330 arg = TYPE_STUB_DECL (arg);
6331
6332 is_type = TYPE_P (arg) || is_tmpl_type;
6333
6334 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6335 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6336 {
6337 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6338 {
6339 if (complain & tf_error)
6340 error ("invalid use of destructor %qE as a type", orig_arg);
6341 return error_mark_node;
6342 }
6343
6344 permerror (input_location,
6345 "to refer to a type member of a template parameter, "
6346 "use %<typename %E%>", orig_arg);
6347
6348 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6349 TREE_OPERAND (arg, 1),
6350 typename_type,
6351 complain);
6352 arg = orig_arg;
6353 is_type = 1;
6354 }
6355 if (is_type != requires_type)
6356 {
6357 if (in_decl)
6358 {
6359 if (complain & tf_error)
6360 {
6361 error ("type/value mismatch at argument %d in template "
6362 "parameter list for %qD",
6363 i + 1, in_decl);
6364 if (is_type)
6365 error (" expected a constant of type %qT, got %qT",
6366 TREE_TYPE (parm),
6367 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6368 else if (requires_tmpl_type)
6369 error (" expected a class template, got %qE", orig_arg);
6370 else
6371 error (" expected a type, got %qE", orig_arg);
6372 }
6373 }
6374 return error_mark_node;
6375 }
6376 if (is_tmpl_type ^ requires_tmpl_type)
6377 {
6378 if (in_decl && (complain & tf_error))
6379 {
6380 error ("type/value mismatch at argument %d in template "
6381 "parameter list for %qD",
6382 i + 1, in_decl);
6383 if (is_tmpl_type)
6384 error (" expected a type, got %qT", DECL_NAME (arg));
6385 else
6386 error (" expected a class template, got %qT", orig_arg);
6387 }
6388 return error_mark_node;
6389 }
6390
6391 if (is_type)
6392 {
6393 if (requires_tmpl_type)
6394 {
6395 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6396 val = orig_arg;
6397 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6398 /* The number of argument required is not known yet.
6399 Just accept it for now. */
6400 val = TREE_TYPE (arg);
6401 else
6402 {
6403 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6404 tree argparm;
6405
6406 /* Strip alias templates that are equivalent to another
6407 template. */
6408 arg = get_underlying_template (arg);
6409 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6410
6411 if (coerce_template_template_parms (parmparm, argparm,
6412 complain, in_decl,
6413 args))
6414 {
6415 val = arg;
6416
6417 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6418 TEMPLATE_DECL. */
6419 if (val != error_mark_node)
6420 {
6421 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6422 val = TREE_TYPE (val);
6423 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6424 val = make_pack_expansion (val);
6425 }
6426 }
6427 else
6428 {
6429 if (in_decl && (complain & tf_error))
6430 {
6431 error ("type/value mismatch at argument %d in "
6432 "template parameter list for %qD",
6433 i + 1, in_decl);
6434 error (" expected a template of type %qD, got %qT",
6435 parm, orig_arg);
6436 }
6437
6438 val = error_mark_node;
6439 }
6440 }
6441 }
6442 else
6443 val = orig_arg;
6444 /* We only form one instance of each template specialization.
6445 Therefore, if we use a non-canonical variant (i.e., a
6446 typedef), any future messages referring to the type will use
6447 the typedef, which is confusing if those future uses do not
6448 themselves also use the typedef. */
6449 if (TYPE_P (val))
6450 val = canonicalize_type_argument (val, complain);
6451 }
6452 else
6453 {
6454 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6455
6456 if (invalid_nontype_parm_type_p (t, complain))
6457 return error_mark_node;
6458
6459 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6460 {
6461 if (same_type_p (t, TREE_TYPE (orig_arg)))
6462 val = orig_arg;
6463 else
6464 {
6465 /* Not sure if this is reachable, but it doesn't hurt
6466 to be robust. */
6467 error ("type mismatch in nontype parameter pack");
6468 val = error_mark_node;
6469 }
6470 }
6471 else if (!dependent_template_arg_p (orig_arg)
6472 && !uses_template_parms (t))
6473 /* We used to call digest_init here. However, digest_init
6474 will report errors, which we don't want when complain
6475 is zero. More importantly, digest_init will try too
6476 hard to convert things: for example, `0' should not be
6477 converted to pointer type at this point according to
6478 the standard. Accepting this is not merely an
6479 extension, since deciding whether or not these
6480 conversions can occur is part of determining which
6481 function template to call, or whether a given explicit
6482 argument specification is valid. */
6483 val = convert_nontype_argument (t, orig_arg, complain);
6484 else
6485 val = strip_typedefs_expr (orig_arg);
6486
6487 if (val == NULL_TREE)
6488 val = error_mark_node;
6489 else if (val == error_mark_node && (complain & tf_error))
6490 error ("could not convert template argument %qE to %qT", orig_arg, t);
6491
6492 if (TREE_CODE (val) == SCOPE_REF)
6493 {
6494 /* Strip typedefs from the SCOPE_REF. */
6495 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6496 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6497 complain);
6498 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6499 QUALIFIED_NAME_IS_TEMPLATE (val));
6500 }
6501 }
6502
6503 return val;
6504 }
6505
6506 /* Coerces the remaining template arguments in INNER_ARGS (from
6507 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6508 Returns the coerced argument pack. PARM_IDX is the position of this
6509 parameter in the template parameter list. ARGS is the original
6510 template argument list. */
6511 static tree
6512 coerce_template_parameter_pack (tree parms,
6513 int parm_idx,
6514 tree args,
6515 tree inner_args,
6516 int arg_idx,
6517 tree new_args,
6518 int* lost,
6519 tree in_decl,
6520 tsubst_flags_t complain)
6521 {
6522 tree parm = TREE_VEC_ELT (parms, parm_idx);
6523 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6524 tree packed_args;
6525 tree argument_pack;
6526 tree packed_types = NULL_TREE;
6527
6528 if (arg_idx > nargs)
6529 arg_idx = nargs;
6530
6531 packed_args = make_tree_vec (nargs - arg_idx);
6532
6533 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6534 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6535 {
6536 /* When the template parameter is a non-type template
6537 parameter pack whose type uses parameter packs, we need
6538 to look at each of the template arguments
6539 separately. Build a vector of the types for these
6540 non-type template parameters in PACKED_TYPES. */
6541 tree expansion
6542 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6543 packed_types = tsubst_pack_expansion (expansion, args,
6544 complain, in_decl);
6545
6546 if (packed_types == error_mark_node)
6547 return error_mark_node;
6548
6549 /* Check that we have the right number of arguments. */
6550 if (arg_idx < nargs
6551 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6552 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6553 {
6554 int needed_parms
6555 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6556 error ("wrong number of template arguments (%d, should be %d)",
6557 nargs, needed_parms);
6558 return error_mark_node;
6559 }
6560
6561 /* If we aren't able to check the actual arguments now
6562 (because they haven't been expanded yet), we can at least
6563 verify that all of the types used for the non-type
6564 template parameter pack are, in fact, valid for non-type
6565 template parameters. */
6566 if (arg_idx < nargs
6567 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6568 {
6569 int j, len = TREE_VEC_LENGTH (packed_types);
6570 for (j = 0; j < len; ++j)
6571 {
6572 tree t = TREE_VEC_ELT (packed_types, j);
6573 if (invalid_nontype_parm_type_p (t, complain))
6574 return error_mark_node;
6575 }
6576 }
6577 }
6578
6579 /* Convert the remaining arguments, which will be a part of the
6580 parameter pack "parm". */
6581 for (; arg_idx < nargs; ++arg_idx)
6582 {
6583 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6584 tree actual_parm = TREE_VALUE (parm);
6585
6586 if (packed_types && !PACK_EXPANSION_P (arg))
6587 {
6588 /* When we have a vector of types (corresponding to the
6589 non-type template parameter pack that uses parameter
6590 packs in its type, as mention above), and the
6591 argument is not an expansion (which expands to a
6592 currently unknown number of arguments), clone the
6593 parm and give it the next type in PACKED_TYPES. */
6594 actual_parm = copy_node (actual_parm);
6595 TREE_TYPE (actual_parm) =
6596 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6597 }
6598
6599 if (arg == error_mark_node)
6600 {
6601 if (complain & tf_error)
6602 error ("template argument %d is invalid", arg_idx + 1);
6603 }
6604 else
6605 arg = convert_template_argument (actual_parm,
6606 arg, new_args, complain, parm_idx,
6607 in_decl);
6608 if (arg == error_mark_node)
6609 (*lost)++;
6610 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6611 }
6612
6613 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6614 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6615 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6616 else
6617 {
6618 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6619 TREE_TYPE (argument_pack)
6620 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6621 TREE_CONSTANT (argument_pack) = 1;
6622 }
6623
6624 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6625 #ifdef ENABLE_CHECKING
6626 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6627 TREE_VEC_LENGTH (packed_args));
6628 #endif
6629 return argument_pack;
6630 }
6631
6632 /* Returns the number of pack expansions in the template argument vector
6633 ARGS. */
6634
6635 static int
6636 pack_expansion_args_count (tree args)
6637 {
6638 int i;
6639 int count = 0;
6640 if (args)
6641 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6642 {
6643 tree elt = TREE_VEC_ELT (args, i);
6644 if (elt && PACK_EXPANSION_P (elt))
6645 ++count;
6646 }
6647 return count;
6648 }
6649
6650 /* Convert all template arguments to their appropriate types, and
6651 return a vector containing the innermost resulting template
6652 arguments. If any error occurs, return error_mark_node. Error and
6653 warning messages are issued under control of COMPLAIN.
6654
6655 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6656 for arguments not specified in ARGS. Otherwise, if
6657 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6658 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6659 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6660 ARGS. */
6661
6662 static tree
6663 coerce_template_parms (tree parms,
6664 tree args,
6665 tree in_decl,
6666 tsubst_flags_t complain,
6667 bool require_all_args,
6668 bool use_default_args)
6669 {
6670 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6671 tree inner_args;
6672 tree new_args;
6673 tree new_inner_args;
6674 int saved_unevaluated_operand;
6675 int saved_inhibit_evaluation_warnings;
6676
6677 /* When used as a boolean value, indicates whether this is a
6678 variadic template parameter list. Since it's an int, we can also
6679 subtract it from nparms to get the number of non-variadic
6680 parameters. */
6681 int variadic_p = 0;
6682 int variadic_args_p = 0;
6683 int post_variadic_parms = 0;
6684
6685 if (args == error_mark_node)
6686 return error_mark_node;
6687
6688 nparms = TREE_VEC_LENGTH (parms);
6689
6690 /* Determine if there are any parameter packs. */
6691 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6692 {
6693 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6694 if (variadic_p)
6695 ++post_variadic_parms;
6696 if (template_parameter_pack_p (tparm))
6697 ++variadic_p;
6698 }
6699
6700 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6701 /* If there are no parameters that follow a parameter pack, we need to
6702 expand any argument packs so that we can deduce a parameter pack from
6703 some non-packed args followed by an argument pack, as in variadic85.C.
6704 If there are such parameters, we need to leave argument packs intact
6705 so the arguments are assigned properly. This can happen when dealing
6706 with a nested class inside a partial specialization of a class
6707 template, as in variadic92.C, or when deducing a template parameter pack
6708 from a sub-declarator, as in variadic114.C. */
6709 if (!post_variadic_parms)
6710 inner_args = expand_template_argument_pack (inner_args);
6711
6712 /* Count any pack expansion args. */
6713 variadic_args_p = pack_expansion_args_count (inner_args);
6714
6715 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6716 if ((nargs > nparms && !variadic_p)
6717 || (nargs < nparms - variadic_p
6718 && require_all_args
6719 && !variadic_args_p
6720 && (!use_default_args
6721 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6722 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6723 {
6724 if (complain & tf_error)
6725 {
6726 if (variadic_p)
6727 {
6728 nparms -= variadic_p;
6729 error ("wrong number of template arguments "
6730 "(%d, should be %d or more)", nargs, nparms);
6731 }
6732 else
6733 error ("wrong number of template arguments "
6734 "(%d, should be %d)", nargs, nparms);
6735
6736 if (in_decl)
6737 error ("provided for %q+D", in_decl);
6738 }
6739
6740 return error_mark_node;
6741 }
6742 /* We can't pass a pack expansion to a non-pack parameter of an alias
6743 template (DR 1430). */
6744 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6745 && variadic_args_p
6746 && nargs - variadic_args_p < nparms - variadic_p)
6747 {
6748 if (complain & tf_error)
6749 {
6750 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6751 {
6752 tree arg = TREE_VEC_ELT (inner_args, i);
6753 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6754
6755 if (PACK_EXPANSION_P (arg)
6756 && !template_parameter_pack_p (parm))
6757 {
6758 error ("pack expansion argument for non-pack parameter "
6759 "%qD of alias template %qD", parm, in_decl);
6760 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6761 goto found;
6762 }
6763 }
6764 gcc_unreachable ();
6765 found:;
6766 }
6767 return error_mark_node;
6768 }
6769
6770 /* We need to evaluate the template arguments, even though this
6771 template-id may be nested within a "sizeof". */
6772 saved_unevaluated_operand = cp_unevaluated_operand;
6773 cp_unevaluated_operand = 0;
6774 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6775 c_inhibit_evaluation_warnings = 0;
6776 new_inner_args = make_tree_vec (nparms);
6777 new_args = add_outermost_template_args (args, new_inner_args);
6778 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6779 {
6780 tree arg;
6781 tree parm;
6782
6783 /* Get the Ith template parameter. */
6784 parm = TREE_VEC_ELT (parms, parm_idx);
6785
6786 if (parm == error_mark_node)
6787 {
6788 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6789 continue;
6790 }
6791
6792 /* Calculate the next argument. */
6793 if (arg_idx < nargs)
6794 arg = TREE_VEC_ELT (inner_args, arg_idx);
6795 else
6796 arg = NULL_TREE;
6797
6798 if (template_parameter_pack_p (TREE_VALUE (parm))
6799 && !(arg && ARGUMENT_PACK_P (arg)))
6800 {
6801 /* All remaining arguments will be placed in the
6802 template parameter pack PARM. */
6803 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6804 inner_args, arg_idx,
6805 new_args, &lost,
6806 in_decl, complain);
6807
6808 /* Store this argument. */
6809 if (arg == error_mark_node)
6810 lost++;
6811 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6812
6813 /* We are done with all of the arguments. */
6814 arg_idx = nargs;
6815
6816 continue;
6817 }
6818 else if (arg)
6819 {
6820 if (PACK_EXPANSION_P (arg))
6821 {
6822 /* We don't know how many args we have yet, just
6823 use the unconverted ones for now. */
6824 new_inner_args = inner_args;
6825 break;
6826 }
6827 }
6828 else if (require_all_args)
6829 {
6830 /* There must be a default arg in this case. */
6831 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6832 complain, in_decl);
6833 /* The position of the first default template argument,
6834 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6835 Record that. */
6836 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6837 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6838 }
6839 else
6840 break;
6841
6842 if (arg == error_mark_node)
6843 {
6844 if (complain & tf_error)
6845 error ("template argument %d is invalid", arg_idx + 1);
6846 }
6847 else if (!arg)
6848 /* This only occurs if there was an error in the template
6849 parameter list itself (which we would already have
6850 reported) that we are trying to recover from, e.g., a class
6851 template with a parameter list such as
6852 template<typename..., typename>. */
6853 ++lost;
6854 else
6855 arg = convert_template_argument (TREE_VALUE (parm),
6856 arg, new_args, complain,
6857 parm_idx, in_decl);
6858
6859 if (arg == error_mark_node)
6860 lost++;
6861 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6862 }
6863 cp_unevaluated_operand = saved_unevaluated_operand;
6864 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6865
6866 if (lost)
6867 return error_mark_node;
6868
6869 #ifdef ENABLE_CHECKING
6870 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6871 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6872 TREE_VEC_LENGTH (new_inner_args));
6873 #endif
6874
6875 return new_inner_args;
6876 }
6877
6878 /* Like coerce_template_parms. If PARMS represents all template
6879 parameters levels, this function returns a vector of vectors
6880 representing all the resulting argument levels. Note that in this
6881 case, only the innermost arguments are coerced because the
6882 outermost ones are supposed to have been coerced already.
6883
6884 Otherwise, if PARMS represents only (the innermost) vector of
6885 parameters, this function returns a vector containing just the
6886 innermost resulting arguments. */
6887
6888 static tree
6889 coerce_innermost_template_parms (tree parms,
6890 tree args,
6891 tree in_decl,
6892 tsubst_flags_t complain,
6893 bool require_all_args,
6894 bool use_default_args)
6895 {
6896 int parms_depth = TMPL_PARMS_DEPTH (parms);
6897 int args_depth = TMPL_ARGS_DEPTH (args);
6898 tree coerced_args;
6899
6900 if (parms_depth > 1)
6901 {
6902 coerced_args = make_tree_vec (parms_depth);
6903 tree level;
6904 int cur_depth;
6905
6906 for (level = parms, cur_depth = parms_depth;
6907 parms_depth > 0 && level != NULL_TREE;
6908 level = TREE_CHAIN (level), --cur_depth)
6909 {
6910 tree l;
6911 if (cur_depth == args_depth)
6912 l = coerce_template_parms (TREE_VALUE (level),
6913 args, in_decl, complain,
6914 require_all_args,
6915 use_default_args);
6916 else
6917 l = TMPL_ARGS_LEVEL (args, cur_depth);
6918
6919 if (l == error_mark_node)
6920 return error_mark_node;
6921
6922 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6923 }
6924 }
6925 else
6926 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6927 args, in_decl, complain,
6928 require_all_args,
6929 use_default_args);
6930 return coerced_args;
6931 }
6932
6933 /* Returns 1 if template args OT and NT are equivalent. */
6934
6935 static int
6936 template_args_equal (tree ot, tree nt)
6937 {
6938 if (nt == ot)
6939 return 1;
6940 if (nt == NULL_TREE || ot == NULL_TREE)
6941 return false;
6942
6943 if (TREE_CODE (nt) == TREE_VEC)
6944 /* For member templates */
6945 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6946 else if (PACK_EXPANSION_P (ot))
6947 return (PACK_EXPANSION_P (nt)
6948 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6949 PACK_EXPANSION_PATTERN (nt))
6950 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6951 PACK_EXPANSION_EXTRA_ARGS (nt)));
6952 else if (ARGUMENT_PACK_P (ot))
6953 {
6954 int i, len;
6955 tree opack, npack;
6956
6957 if (!ARGUMENT_PACK_P (nt))
6958 return 0;
6959
6960 opack = ARGUMENT_PACK_ARGS (ot);
6961 npack = ARGUMENT_PACK_ARGS (nt);
6962 len = TREE_VEC_LENGTH (opack);
6963 if (TREE_VEC_LENGTH (npack) != len)
6964 return 0;
6965 for (i = 0; i < len; ++i)
6966 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6967 TREE_VEC_ELT (npack, i)))
6968 return 0;
6969 return 1;
6970 }
6971 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6972 {
6973 /* We get here probably because we are in the middle of substituting
6974 into the pattern of a pack expansion. In that case the
6975 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6976 interested in. So we want to use the initial pack argument for
6977 the comparison. */
6978 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6979 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6980 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6981 return template_args_equal (ot, nt);
6982 }
6983 else if (TYPE_P (nt))
6984 return TYPE_P (ot) && same_type_p (ot, nt);
6985 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6986 return 0;
6987 else
6988 return cp_tree_equal (ot, nt);
6989 }
6990
6991 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6992 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6993 NEWARG_PTR with the offending arguments if they are non-NULL. */
6994
6995 static int
6996 comp_template_args_with_info (tree oldargs, tree newargs,
6997 tree *oldarg_ptr, tree *newarg_ptr)
6998 {
6999 int i;
7000
7001 if (oldargs == newargs)
7002 return 1;
7003
7004 if (!oldargs || !newargs)
7005 return 0;
7006
7007 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
7008 return 0;
7009
7010 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
7011 {
7012 tree nt = TREE_VEC_ELT (newargs, i);
7013 tree ot = TREE_VEC_ELT (oldargs, i);
7014
7015 if (! template_args_equal (ot, nt))
7016 {
7017 if (oldarg_ptr != NULL)
7018 *oldarg_ptr = ot;
7019 if (newarg_ptr != NULL)
7020 *newarg_ptr = nt;
7021 return 0;
7022 }
7023 }
7024 return 1;
7025 }
7026
7027 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7028 of template arguments. Returns 0 otherwise. */
7029
7030 int
7031 comp_template_args (tree oldargs, tree newargs)
7032 {
7033 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7034 }
7035
7036 static void
7037 add_pending_template (tree d)
7038 {
7039 tree ti = (TYPE_P (d)
7040 ? CLASSTYPE_TEMPLATE_INFO (d)
7041 : DECL_TEMPLATE_INFO (d));
7042 struct pending_template *pt;
7043 int level;
7044
7045 if (TI_PENDING_TEMPLATE_FLAG (ti))
7046 return;
7047
7048 /* We are called both from instantiate_decl, where we've already had a
7049 tinst_level pushed, and instantiate_template, where we haven't.
7050 Compensate. */
7051 level = !current_tinst_level || current_tinst_level->decl != d;
7052
7053 if (level)
7054 push_tinst_level (d);
7055
7056 pt = ggc_alloc_pending_template ();
7057 pt->next = NULL;
7058 pt->tinst = current_tinst_level;
7059 if (last_pending_template)
7060 last_pending_template->next = pt;
7061 else
7062 pending_templates = pt;
7063
7064 last_pending_template = pt;
7065
7066 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7067
7068 if (level)
7069 pop_tinst_level ();
7070 }
7071
7072
7073 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7074 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7075 documentation for TEMPLATE_ID_EXPR. */
7076
7077 tree
7078 lookup_template_function (tree fns, tree arglist)
7079 {
7080 tree type;
7081
7082 if (fns == error_mark_node || arglist == error_mark_node)
7083 return error_mark_node;
7084
7085 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7086
7087 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7088 {
7089 error ("%q#D is not a function template", fns);
7090 return error_mark_node;
7091 }
7092
7093 if (BASELINK_P (fns))
7094 {
7095 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7096 unknown_type_node,
7097 BASELINK_FUNCTIONS (fns),
7098 arglist);
7099 return fns;
7100 }
7101
7102 type = TREE_TYPE (fns);
7103 if (TREE_CODE (fns) == OVERLOAD || !type)
7104 type = unknown_type_node;
7105
7106 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7107 }
7108
7109 /* Within the scope of a template class S<T>, the name S gets bound
7110 (in build_self_reference) to a TYPE_DECL for the class, not a
7111 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7112 or one of its enclosing classes, and that type is a template,
7113 return the associated TEMPLATE_DECL. Otherwise, the original
7114 DECL is returned.
7115
7116 Also handle the case when DECL is a TREE_LIST of ambiguous
7117 injected-class-names from different bases. */
7118
7119 tree
7120 maybe_get_template_decl_from_type_decl (tree decl)
7121 {
7122 if (decl == NULL_TREE)
7123 return decl;
7124
7125 /* DR 176: A lookup that finds an injected-class-name (10.2
7126 [class.member.lookup]) can result in an ambiguity in certain cases
7127 (for example, if it is found in more than one base class). If all of
7128 the injected-class-names that are found refer to specializations of
7129 the same class template, and if the name is followed by a
7130 template-argument-list, the reference refers to the class template
7131 itself and not a specialization thereof, and is not ambiguous. */
7132 if (TREE_CODE (decl) == TREE_LIST)
7133 {
7134 tree t, tmpl = NULL_TREE;
7135 for (t = decl; t; t = TREE_CHAIN (t))
7136 {
7137 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7138 if (!tmpl)
7139 tmpl = elt;
7140 else if (tmpl != elt)
7141 break;
7142 }
7143 if (tmpl && t == NULL_TREE)
7144 return tmpl;
7145 else
7146 return decl;
7147 }
7148
7149 return (decl != NULL_TREE
7150 && DECL_SELF_REFERENCE_P (decl)
7151 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7152 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7153 }
7154
7155 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7156 parameters, find the desired type.
7157
7158 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7159
7160 IN_DECL, if non-NULL, is the template declaration we are trying to
7161 instantiate.
7162
7163 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7164 the class we are looking up.
7165
7166 Issue error and warning messages under control of COMPLAIN.
7167
7168 If the template class is really a local class in a template
7169 function, then the FUNCTION_CONTEXT is the function in which it is
7170 being instantiated.
7171
7172 ??? Note that this function is currently called *twice* for each
7173 template-id: the first time from the parser, while creating the
7174 incomplete type (finish_template_type), and the second type during the
7175 real instantiation (instantiate_template_class). This is surely something
7176 that we want to avoid. It also causes some problems with argument
7177 coercion (see convert_nontype_argument for more information on this). */
7178
7179 static tree
7180 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7181 int entering_scope, tsubst_flags_t complain)
7182 {
7183 tree templ = NULL_TREE, parmlist;
7184 tree t;
7185 void **slot;
7186 spec_entry *entry;
7187 spec_entry elt;
7188 hashval_t hash;
7189
7190 if (identifier_p (d1))
7191 {
7192 tree value = innermost_non_namespace_value (d1);
7193 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7194 templ = value;
7195 else
7196 {
7197 if (context)
7198 push_decl_namespace (context);
7199 templ = lookup_name (d1);
7200 templ = maybe_get_template_decl_from_type_decl (templ);
7201 if (context)
7202 pop_decl_namespace ();
7203 }
7204 if (templ)
7205 context = DECL_CONTEXT (templ);
7206 }
7207 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7208 {
7209 tree type = TREE_TYPE (d1);
7210
7211 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7212 an implicit typename for the second A. Deal with it. */
7213 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7214 type = TREE_TYPE (type);
7215
7216 if (CLASSTYPE_TEMPLATE_INFO (type))
7217 {
7218 templ = CLASSTYPE_TI_TEMPLATE (type);
7219 d1 = DECL_NAME (templ);
7220 }
7221 }
7222 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7223 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7224 {
7225 templ = TYPE_TI_TEMPLATE (d1);
7226 d1 = DECL_NAME (templ);
7227 }
7228 else if (TREE_CODE (d1) == TEMPLATE_DECL
7229 && DECL_TEMPLATE_RESULT (d1)
7230 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7231 {
7232 templ = d1;
7233 d1 = DECL_NAME (templ);
7234 context = DECL_CONTEXT (templ);
7235 }
7236 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7237 {
7238 templ = d1;
7239 d1 = DECL_NAME (templ);
7240 }
7241
7242 /* Issue an error message if we didn't find a template. */
7243 if (! templ)
7244 {
7245 if (complain & tf_error)
7246 error ("%qT is not a template", d1);
7247 return error_mark_node;
7248 }
7249
7250 if (TREE_CODE (templ) != TEMPLATE_DECL
7251 /* Make sure it's a user visible template, if it was named by
7252 the user. */
7253 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7254 && !PRIMARY_TEMPLATE_P (templ)))
7255 {
7256 if (complain & tf_error)
7257 {
7258 error ("non-template type %qT used as a template", d1);
7259 if (in_decl)
7260 error ("for template declaration %q+D", in_decl);
7261 }
7262 return error_mark_node;
7263 }
7264
7265 complain &= ~tf_user;
7266
7267 /* An alias that just changes the name of a template is equivalent to the
7268 other template, so if any of the arguments are pack expansions, strip
7269 the alias to avoid problems with a pack expansion passed to a non-pack
7270 alias template parameter (DR 1430). */
7271 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7272 templ = get_underlying_template (templ);
7273
7274 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7275 {
7276 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7277 template arguments */
7278
7279 tree parm;
7280 tree arglist2;
7281 tree outer;
7282
7283 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7284
7285 /* Consider an example where a template template parameter declared as
7286
7287 template <class T, class U = std::allocator<T> > class TT
7288
7289 The template parameter level of T and U are one level larger than
7290 of TT. To proper process the default argument of U, say when an
7291 instantiation `TT<int>' is seen, we need to build the full
7292 arguments containing {int} as the innermost level. Outer levels,
7293 available when not appearing as default template argument, can be
7294 obtained from the arguments of the enclosing template.
7295
7296 Suppose that TT is later substituted with std::vector. The above
7297 instantiation is `TT<int, std::allocator<T> >' with TT at
7298 level 1, and T at level 2, while the template arguments at level 1
7299 becomes {std::vector} and the inner level 2 is {int}. */
7300
7301 outer = DECL_CONTEXT (templ);
7302 if (outer)
7303 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7304 else if (current_template_parms)
7305 /* This is an argument of the current template, so we haven't set
7306 DECL_CONTEXT yet. */
7307 outer = current_template_args ();
7308
7309 if (outer)
7310 arglist = add_to_template_args (outer, arglist);
7311
7312 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7313 complain,
7314 /*require_all_args=*/true,
7315 /*use_default_args=*/true);
7316 if (arglist2 == error_mark_node
7317 || (!uses_template_parms (arglist2)
7318 && check_instantiated_args (templ, arglist2, complain)))
7319 return error_mark_node;
7320
7321 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7322 return parm;
7323 }
7324 else
7325 {
7326 tree template_type = TREE_TYPE (templ);
7327 tree gen_tmpl;
7328 tree type_decl;
7329 tree found = NULL_TREE;
7330 int arg_depth;
7331 int parm_depth;
7332 int is_dependent_type;
7333 int use_partial_inst_tmpl = false;
7334
7335 if (template_type == error_mark_node)
7336 /* An error occurred while building the template TEMPL, and a
7337 diagnostic has most certainly been emitted for that
7338 already. Let's propagate that error. */
7339 return error_mark_node;
7340
7341 gen_tmpl = most_general_template (templ);
7342 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7343 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7344 arg_depth = TMPL_ARGS_DEPTH (arglist);
7345
7346 if (arg_depth == 1 && parm_depth > 1)
7347 {
7348 /* We've been given an incomplete set of template arguments.
7349 For example, given:
7350
7351 template <class T> struct S1 {
7352 template <class U> struct S2 {};
7353 template <class U> struct S2<U*> {};
7354 };
7355
7356 we will be called with an ARGLIST of `U*', but the
7357 TEMPLATE will be `template <class T> template
7358 <class U> struct S1<T>::S2'. We must fill in the missing
7359 arguments. */
7360 arglist
7361 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7362 arglist);
7363 arg_depth = TMPL_ARGS_DEPTH (arglist);
7364 }
7365
7366 /* Now we should have enough arguments. */
7367 gcc_assert (parm_depth == arg_depth);
7368
7369 /* From here on, we're only interested in the most general
7370 template. */
7371
7372 /* Calculate the BOUND_ARGS. These will be the args that are
7373 actually tsubst'd into the definition to create the
7374 instantiation. */
7375 if (parm_depth > 1)
7376 {
7377 /* We have multiple levels of arguments to coerce, at once. */
7378 int i;
7379 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7380
7381 tree bound_args = make_tree_vec (parm_depth);
7382
7383 for (i = saved_depth,
7384 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7385 i > 0 && t != NULL_TREE;
7386 --i, t = TREE_CHAIN (t))
7387 {
7388 tree a;
7389 if (i == saved_depth)
7390 a = coerce_template_parms (TREE_VALUE (t),
7391 arglist, gen_tmpl,
7392 complain,
7393 /*require_all_args=*/true,
7394 /*use_default_args=*/true);
7395 else
7396 /* Outer levels should have already been coerced. */
7397 a = TMPL_ARGS_LEVEL (arglist, i);
7398
7399 /* Don't process further if one of the levels fails. */
7400 if (a == error_mark_node)
7401 {
7402 /* Restore the ARGLIST to its full size. */
7403 TREE_VEC_LENGTH (arglist) = saved_depth;
7404 return error_mark_node;
7405 }
7406
7407 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7408
7409 /* We temporarily reduce the length of the ARGLIST so
7410 that coerce_template_parms will see only the arguments
7411 corresponding to the template parameters it is
7412 examining. */
7413 TREE_VEC_LENGTH (arglist)--;
7414 }
7415
7416 /* Restore the ARGLIST to its full size. */
7417 TREE_VEC_LENGTH (arglist) = saved_depth;
7418
7419 arglist = bound_args;
7420 }
7421 else
7422 arglist
7423 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7424 INNERMOST_TEMPLATE_ARGS (arglist),
7425 gen_tmpl,
7426 complain,
7427 /*require_all_args=*/true,
7428 /*use_default_args=*/true);
7429
7430 if (arglist == error_mark_node)
7431 /* We were unable to bind the arguments. */
7432 return error_mark_node;
7433
7434 /* In the scope of a template class, explicit references to the
7435 template class refer to the type of the template, not any
7436 instantiation of it. For example, in:
7437
7438 template <class T> class C { void f(C<T>); }
7439
7440 the `C<T>' is just the same as `C'. Outside of the
7441 class, however, such a reference is an instantiation. */
7442 if ((entering_scope
7443 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7444 || currently_open_class (template_type))
7445 /* comp_template_args is expensive, check it last. */
7446 && comp_template_args (TYPE_TI_ARGS (template_type),
7447 arglist))
7448 return template_type;
7449
7450 /* If we already have this specialization, return it. */
7451 elt.tmpl = gen_tmpl;
7452 elt.args = arglist;
7453 hash = hash_specialization (&elt);
7454 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7455 &elt, hash);
7456
7457 if (entry)
7458 return entry->spec;
7459
7460 is_dependent_type = uses_template_parms (arglist);
7461
7462 /* If the deduced arguments are invalid, then the binding
7463 failed. */
7464 if (!is_dependent_type
7465 && check_instantiated_args (gen_tmpl,
7466 INNERMOST_TEMPLATE_ARGS (arglist),
7467 complain))
7468 return error_mark_node;
7469
7470 if (!is_dependent_type
7471 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7472 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7473 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7474 {
7475 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7476 DECL_NAME (gen_tmpl),
7477 /*tag_scope=*/ts_global);
7478 return found;
7479 }
7480
7481 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7482 complain, in_decl);
7483 if (context == error_mark_node)
7484 return error_mark_node;
7485
7486 if (!context)
7487 context = global_namespace;
7488
7489 /* Create the type. */
7490 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7491 {
7492 /* The user referred to a specialization of an alias
7493 template represented by GEN_TMPL.
7494
7495 [temp.alias]/2 says:
7496
7497 When a template-id refers to the specialization of an
7498 alias template, it is equivalent to the associated
7499 type obtained by substitution of its
7500 template-arguments for the template-parameters in the
7501 type-id of the alias template. */
7502
7503 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7504 /* Note that the call above (by indirectly calling
7505 register_specialization in tsubst_decl) registers the
7506 TYPE_DECL representing the specialization of the alias
7507 template. So next time someone substitutes ARGLIST for
7508 the template parms into the alias template (GEN_TMPL),
7509 she'll get that TYPE_DECL back. */
7510
7511 if (t == error_mark_node)
7512 return t;
7513 }
7514 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7515 {
7516 if (!is_dependent_type)
7517 {
7518 set_current_access_from_decl (TYPE_NAME (template_type));
7519 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7520 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7521 arglist, complain, in_decl),
7522 SCOPED_ENUM_P (template_type), NULL);
7523 }
7524 else
7525 {
7526 /* We don't want to call start_enum for this type, since
7527 the values for the enumeration constants may involve
7528 template parameters. And, no one should be interested
7529 in the enumeration constants for such a type. */
7530 t = cxx_make_type (ENUMERAL_TYPE);
7531 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7532 }
7533 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7534 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7535 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7536 }
7537 else if (CLASS_TYPE_P (template_type))
7538 {
7539 t = make_class_type (TREE_CODE (template_type));
7540 CLASSTYPE_DECLARED_CLASS (t)
7541 = CLASSTYPE_DECLARED_CLASS (template_type);
7542 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7543 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7544
7545 /* A local class. Make sure the decl gets registered properly. */
7546 if (context == current_function_decl)
7547 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7548
7549 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7550 /* This instantiation is another name for the primary
7551 template type. Set the TYPE_CANONICAL field
7552 appropriately. */
7553 TYPE_CANONICAL (t) = template_type;
7554 else if (any_template_arguments_need_structural_equality_p (arglist))
7555 /* Some of the template arguments require structural
7556 equality testing, so this template class requires
7557 structural equality testing. */
7558 SET_TYPE_STRUCTURAL_EQUALITY (t);
7559 }
7560 else
7561 gcc_unreachable ();
7562
7563 /* If we called start_enum or pushtag above, this information
7564 will already be set up. */
7565 if (!TYPE_NAME (t))
7566 {
7567 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7568
7569 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7570 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7571 DECL_SOURCE_LOCATION (type_decl)
7572 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7573 }
7574 else
7575 type_decl = TYPE_NAME (t);
7576
7577 if (CLASS_TYPE_P (template_type))
7578 {
7579 TREE_PRIVATE (type_decl)
7580 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7581 TREE_PROTECTED (type_decl)
7582 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7583 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7584 {
7585 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7586 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7587 }
7588 }
7589
7590 /* Let's consider the explicit specialization of a member
7591 of a class template specialization that is implicitely instantiated,
7592 e.g.:
7593 template<class T>
7594 struct S
7595 {
7596 template<class U> struct M {}; //#0
7597 };
7598
7599 template<>
7600 template<>
7601 struct S<int>::M<char> //#1
7602 {
7603 int i;
7604 };
7605 [temp.expl.spec]/4 says this is valid.
7606
7607 In this case, when we write:
7608 S<int>::M<char> m;
7609
7610 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7611 the one of #0.
7612
7613 When we encounter #1, we want to store the partial instantiation
7614 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7615
7616 For all cases other than this "explicit specialization of member of a
7617 class template", we just want to store the most general template into
7618 the CLASSTYPE_TI_TEMPLATE of M.
7619
7620 This case of "explicit specialization of member of a class template"
7621 only happens when:
7622 1/ the enclosing class is an instantiation of, and therefore not
7623 the same as, the context of the most general template, and
7624 2/ we aren't looking at the partial instantiation itself, i.e.
7625 the innermost arguments are not the same as the innermost parms of
7626 the most general template.
7627
7628 So it's only when 1/ and 2/ happens that we want to use the partial
7629 instantiation of the member template in lieu of its most general
7630 template. */
7631
7632 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7633 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7634 /* the enclosing class must be an instantiation... */
7635 && CLASS_TYPE_P (context)
7636 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7637 {
7638 tree partial_inst_args;
7639 TREE_VEC_LENGTH (arglist)--;
7640 ++processing_template_decl;
7641 partial_inst_args =
7642 tsubst (INNERMOST_TEMPLATE_ARGS
7643 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7644 arglist, complain, NULL_TREE);
7645 --processing_template_decl;
7646 TREE_VEC_LENGTH (arglist)++;
7647 use_partial_inst_tmpl =
7648 /*...and we must not be looking at the partial instantiation
7649 itself. */
7650 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7651 partial_inst_args);
7652 }
7653
7654 if (!use_partial_inst_tmpl)
7655 /* This case is easy; there are no member templates involved. */
7656 found = gen_tmpl;
7657 else
7658 {
7659 /* This is a full instantiation of a member template. Find
7660 the partial instantiation of which this is an instance. */
7661
7662 /* Temporarily reduce by one the number of levels in the ARGLIST
7663 so as to avoid comparing the last set of arguments. */
7664 TREE_VEC_LENGTH (arglist)--;
7665 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7666 TREE_VEC_LENGTH (arglist)++;
7667 /* FOUND is either a proper class type, or an alias
7668 template specialization. In the later case, it's a
7669 TYPE_DECL, resulting from the substituting of arguments
7670 for parameters in the TYPE_DECL of the alias template
7671 done earlier. So be careful while getting the template
7672 of FOUND. */
7673 found = TREE_CODE (found) == TYPE_DECL
7674 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7675 : CLASSTYPE_TI_TEMPLATE (found);
7676 }
7677
7678 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7679
7680 elt.spec = t;
7681 slot = htab_find_slot_with_hash (type_specializations,
7682 &elt, hash, INSERT);
7683 entry = ggc_alloc_spec_entry ();
7684 *entry = elt;
7685 *slot = entry;
7686
7687 /* Note this use of the partial instantiation so we can check it
7688 later in maybe_process_partial_specialization. */
7689 DECL_TEMPLATE_INSTANTIATIONS (templ)
7690 = tree_cons (arglist, t,
7691 DECL_TEMPLATE_INSTANTIATIONS (templ));
7692
7693 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7694 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7695 /* Now that the type has been registered on the instantiations
7696 list, we set up the enumerators. Because the enumeration
7697 constants may involve the enumeration type itself, we make
7698 sure to register the type first, and then create the
7699 constants. That way, doing tsubst_expr for the enumeration
7700 constants won't result in recursive calls here; we'll find
7701 the instantiation and exit above. */
7702 tsubst_enum (template_type, t, arglist);
7703
7704 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7705 /* If the type makes use of template parameters, the
7706 code that generates debugging information will crash. */
7707 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7708
7709 /* Possibly limit visibility based on template args. */
7710 TREE_PUBLIC (type_decl) = 1;
7711 determine_visibility (type_decl);
7712
7713 return t;
7714 }
7715 }
7716
7717 /* Wrapper for lookup_template_class_1. */
7718
7719 tree
7720 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7721 int entering_scope, tsubst_flags_t complain)
7722 {
7723 tree ret;
7724 timevar_push (TV_TEMPLATE_INST);
7725 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7726 entering_scope, complain);
7727 timevar_pop (TV_TEMPLATE_INST);
7728 return ret;
7729 }
7730 \f
7731 struct pair_fn_data
7732 {
7733 tree_fn_t fn;
7734 void *data;
7735 /* True when we should also visit template parameters that occur in
7736 non-deduced contexts. */
7737 bool include_nondeduced_p;
7738 struct pointer_set_t *visited;
7739 };
7740
7741 /* Called from for_each_template_parm via walk_tree. */
7742
7743 static tree
7744 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7745 {
7746 tree t = *tp;
7747 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7748 tree_fn_t fn = pfd->fn;
7749 void *data = pfd->data;
7750
7751 if (TYPE_P (t)
7752 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7753 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7754 pfd->include_nondeduced_p))
7755 return error_mark_node;
7756
7757 switch (TREE_CODE (t))
7758 {
7759 case RECORD_TYPE:
7760 if (TYPE_PTRMEMFUNC_P (t))
7761 break;
7762 /* Fall through. */
7763
7764 case UNION_TYPE:
7765 case ENUMERAL_TYPE:
7766 if (!TYPE_TEMPLATE_INFO (t))
7767 *walk_subtrees = 0;
7768 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7769 fn, data, pfd->visited,
7770 pfd->include_nondeduced_p))
7771 return error_mark_node;
7772 break;
7773
7774 case INTEGER_TYPE:
7775 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7776 fn, data, pfd->visited,
7777 pfd->include_nondeduced_p)
7778 || for_each_template_parm (TYPE_MAX_VALUE (t),
7779 fn, data, pfd->visited,
7780 pfd->include_nondeduced_p))
7781 return error_mark_node;
7782 break;
7783
7784 case METHOD_TYPE:
7785 /* Since we're not going to walk subtrees, we have to do this
7786 explicitly here. */
7787 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7788 pfd->visited, pfd->include_nondeduced_p))
7789 return error_mark_node;
7790 /* Fall through. */
7791
7792 case FUNCTION_TYPE:
7793 /* Check the return type. */
7794 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7795 pfd->include_nondeduced_p))
7796 return error_mark_node;
7797
7798 /* Check the parameter types. Since default arguments are not
7799 instantiated until they are needed, the TYPE_ARG_TYPES may
7800 contain expressions that involve template parameters. But,
7801 no-one should be looking at them yet. And, once they're
7802 instantiated, they don't contain template parameters, so
7803 there's no point in looking at them then, either. */
7804 {
7805 tree parm;
7806
7807 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7808 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7809 pfd->visited, pfd->include_nondeduced_p))
7810 return error_mark_node;
7811
7812 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7813 want walk_tree walking into them itself. */
7814 *walk_subtrees = 0;
7815 }
7816 break;
7817
7818 case TYPEOF_TYPE:
7819 case UNDERLYING_TYPE:
7820 if (pfd->include_nondeduced_p
7821 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7822 pfd->visited,
7823 pfd->include_nondeduced_p))
7824 return error_mark_node;
7825 break;
7826
7827 case FUNCTION_DECL:
7828 case VAR_DECL:
7829 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7830 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7831 pfd->visited, pfd->include_nondeduced_p))
7832 return error_mark_node;
7833 /* Fall through. */
7834
7835 case PARM_DECL:
7836 case CONST_DECL:
7837 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7838 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7839 pfd->visited, pfd->include_nondeduced_p))
7840 return error_mark_node;
7841 if (DECL_CONTEXT (t)
7842 && pfd->include_nondeduced_p
7843 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7844 pfd->visited, pfd->include_nondeduced_p))
7845 return error_mark_node;
7846 break;
7847
7848 case BOUND_TEMPLATE_TEMPLATE_PARM:
7849 /* Record template parameters such as `T' inside `TT<T>'. */
7850 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7851 pfd->include_nondeduced_p))
7852 return error_mark_node;
7853 /* Fall through. */
7854
7855 case TEMPLATE_TEMPLATE_PARM:
7856 case TEMPLATE_TYPE_PARM:
7857 case TEMPLATE_PARM_INDEX:
7858 if (fn && (*fn)(t, data))
7859 return error_mark_node;
7860 else if (!fn)
7861 return error_mark_node;
7862 break;
7863
7864 case TEMPLATE_DECL:
7865 /* A template template parameter is encountered. */
7866 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7867 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7868 pfd->include_nondeduced_p))
7869 return error_mark_node;
7870
7871 /* Already substituted template template parameter */
7872 *walk_subtrees = 0;
7873 break;
7874
7875 case TYPENAME_TYPE:
7876 if (!fn
7877 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7878 data, pfd->visited,
7879 pfd->include_nondeduced_p))
7880 return error_mark_node;
7881 break;
7882
7883 case CONSTRUCTOR:
7884 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7885 && pfd->include_nondeduced_p
7886 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7887 (TREE_TYPE (t)), fn, data,
7888 pfd->visited, pfd->include_nondeduced_p))
7889 return error_mark_node;
7890 break;
7891
7892 case INDIRECT_REF:
7893 case COMPONENT_REF:
7894 /* If there's no type, then this thing must be some expression
7895 involving template parameters. */
7896 if (!fn && !TREE_TYPE (t))
7897 return error_mark_node;
7898 break;
7899
7900 case MODOP_EXPR:
7901 case CAST_EXPR:
7902 case IMPLICIT_CONV_EXPR:
7903 case REINTERPRET_CAST_EXPR:
7904 case CONST_CAST_EXPR:
7905 case STATIC_CAST_EXPR:
7906 case DYNAMIC_CAST_EXPR:
7907 case ARROW_EXPR:
7908 case DOTSTAR_EXPR:
7909 case TYPEID_EXPR:
7910 case PSEUDO_DTOR_EXPR:
7911 if (!fn)
7912 return error_mark_node;
7913 break;
7914
7915 default:
7916 break;
7917 }
7918
7919 /* We didn't find any template parameters we liked. */
7920 return NULL_TREE;
7921 }
7922
7923 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7924 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7925 call FN with the parameter and the DATA.
7926 If FN returns nonzero, the iteration is terminated, and
7927 for_each_template_parm returns 1. Otherwise, the iteration
7928 continues. If FN never returns a nonzero value, the value
7929 returned by for_each_template_parm is 0. If FN is NULL, it is
7930 considered to be the function which always returns 1.
7931
7932 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7933 parameters that occur in non-deduced contexts. When false, only
7934 visits those template parameters that can be deduced. */
7935
7936 static int
7937 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7938 struct pointer_set_t *visited,
7939 bool include_nondeduced_p)
7940 {
7941 struct pair_fn_data pfd;
7942 int result;
7943
7944 /* Set up. */
7945 pfd.fn = fn;
7946 pfd.data = data;
7947 pfd.include_nondeduced_p = include_nondeduced_p;
7948
7949 /* Walk the tree. (Conceptually, we would like to walk without
7950 duplicates, but for_each_template_parm_r recursively calls
7951 for_each_template_parm, so we would need to reorganize a fair
7952 bit to use walk_tree_without_duplicates, so we keep our own
7953 visited list.) */
7954 if (visited)
7955 pfd.visited = visited;
7956 else
7957 pfd.visited = pointer_set_create ();
7958 result = cp_walk_tree (&t,
7959 for_each_template_parm_r,
7960 &pfd,
7961 pfd.visited) != NULL_TREE;
7962
7963 /* Clean up. */
7964 if (!visited)
7965 {
7966 pointer_set_destroy (pfd.visited);
7967 pfd.visited = 0;
7968 }
7969
7970 return result;
7971 }
7972
7973 /* Returns true if T depends on any template parameter. */
7974
7975 int
7976 uses_template_parms (tree t)
7977 {
7978 bool dependent_p;
7979 int saved_processing_template_decl;
7980
7981 saved_processing_template_decl = processing_template_decl;
7982 if (!saved_processing_template_decl)
7983 processing_template_decl = 1;
7984 if (TYPE_P (t))
7985 dependent_p = dependent_type_p (t);
7986 else if (TREE_CODE (t) == TREE_VEC)
7987 dependent_p = any_dependent_template_arguments_p (t);
7988 else if (TREE_CODE (t) == TREE_LIST)
7989 dependent_p = (uses_template_parms (TREE_VALUE (t))
7990 || uses_template_parms (TREE_CHAIN (t)));
7991 else if (TREE_CODE (t) == TYPE_DECL)
7992 dependent_p = dependent_type_p (TREE_TYPE (t));
7993 else if (DECL_P (t)
7994 || EXPR_P (t)
7995 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7996 || TREE_CODE (t) == OVERLOAD
7997 || BASELINK_P (t)
7998 || identifier_p (t)
7999 || TREE_CODE (t) == TRAIT_EXPR
8000 || TREE_CODE (t) == CONSTRUCTOR
8001 || CONSTANT_CLASS_P (t))
8002 dependent_p = (type_dependent_expression_p (t)
8003 || value_dependent_expression_p (t));
8004 else
8005 {
8006 gcc_assert (t == error_mark_node);
8007 dependent_p = false;
8008 }
8009
8010 processing_template_decl = saved_processing_template_decl;
8011
8012 return dependent_p;
8013 }
8014
8015 /* Returns true iff current_function_decl is an incompletely instantiated
8016 template. Useful instead of processing_template_decl because the latter
8017 is set to 0 during fold_non_dependent_expr. */
8018
8019 bool
8020 in_template_function (void)
8021 {
8022 tree fn = current_function_decl;
8023 bool ret;
8024 ++processing_template_decl;
8025 ret = (fn && DECL_LANG_SPECIFIC (fn)
8026 && DECL_TEMPLATE_INFO (fn)
8027 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8028 --processing_template_decl;
8029 return ret;
8030 }
8031
8032 /* Returns true if T depends on any template parameter with level LEVEL. */
8033
8034 int
8035 uses_template_parms_level (tree t, int level)
8036 {
8037 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8038 /*include_nondeduced_p=*/true);
8039 }
8040
8041 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8042 ill-formed translation unit, i.e. a variable or function that isn't
8043 usable in a constant expression. */
8044
8045 static inline bool
8046 neglectable_inst_p (tree d)
8047 {
8048 return (DECL_P (d)
8049 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8050 : decl_maybe_constant_var_p (d)));
8051 }
8052
8053 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8054 neglectable and instantiated from within an erroneous instantiation. */
8055
8056 static bool
8057 limit_bad_template_recursion (tree decl)
8058 {
8059 struct tinst_level *lev = current_tinst_level;
8060 int errs = errorcount + sorrycount;
8061 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8062 return false;
8063
8064 for (; lev; lev = lev->next)
8065 if (neglectable_inst_p (lev->decl))
8066 break;
8067
8068 return (lev && errs > lev->errors);
8069 }
8070
8071 static int tinst_depth;
8072 extern int max_tinst_depth;
8073 int depth_reached;
8074
8075 static GTY(()) struct tinst_level *last_error_tinst_level;
8076
8077 /* We're starting to instantiate D; record the template instantiation context
8078 for diagnostics and to restore it later. */
8079
8080 int
8081 push_tinst_level (tree d)
8082 {
8083 struct tinst_level *new_level;
8084
8085 if (tinst_depth >= max_tinst_depth)
8086 {
8087 last_error_tinst_level = current_tinst_level;
8088 if (TREE_CODE (d) == TREE_LIST)
8089 error ("template instantiation depth exceeds maximum of %d (use "
8090 "-ftemplate-depth= to increase the maximum) substituting %qS",
8091 max_tinst_depth, d);
8092 else
8093 error ("template instantiation depth exceeds maximum of %d (use "
8094 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8095 max_tinst_depth, d);
8096
8097 print_instantiation_context ();
8098
8099 return 0;
8100 }
8101
8102 /* If the current instantiation caused problems, don't let it instantiate
8103 anything else. Do allow deduction substitution and decls usable in
8104 constant expressions. */
8105 if (limit_bad_template_recursion (d))
8106 return 0;
8107
8108 new_level = ggc_alloc_tinst_level ();
8109 new_level->decl = d;
8110 new_level->locus = input_location;
8111 new_level->errors = errorcount+sorrycount;
8112 new_level->in_system_header_p = in_system_header_at (input_location);
8113 new_level->next = current_tinst_level;
8114 current_tinst_level = new_level;
8115
8116 ++tinst_depth;
8117 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8118 depth_reached = tinst_depth;
8119
8120 return 1;
8121 }
8122
8123 /* We're done instantiating this template; return to the instantiation
8124 context. */
8125
8126 void
8127 pop_tinst_level (void)
8128 {
8129 /* Restore the filename and line number stashed away when we started
8130 this instantiation. */
8131 input_location = current_tinst_level->locus;
8132 current_tinst_level = current_tinst_level->next;
8133 --tinst_depth;
8134 }
8135
8136 /* We're instantiating a deferred template; restore the template
8137 instantiation context in which the instantiation was requested, which
8138 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8139
8140 static tree
8141 reopen_tinst_level (struct tinst_level *level)
8142 {
8143 struct tinst_level *t;
8144
8145 tinst_depth = 0;
8146 for (t = level; t; t = t->next)
8147 ++tinst_depth;
8148
8149 current_tinst_level = level;
8150 pop_tinst_level ();
8151 if (current_tinst_level)
8152 current_tinst_level->errors = errorcount+sorrycount;
8153 return level->decl;
8154 }
8155
8156 /* Returns the TINST_LEVEL which gives the original instantiation
8157 context. */
8158
8159 struct tinst_level *
8160 outermost_tinst_level (void)
8161 {
8162 struct tinst_level *level = current_tinst_level;
8163 if (level)
8164 while (level->next)
8165 level = level->next;
8166 return level;
8167 }
8168
8169 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8170 vector of template arguments, as for tsubst.
8171
8172 Returns an appropriate tsubst'd friend declaration. */
8173
8174 static tree
8175 tsubst_friend_function (tree decl, tree args)
8176 {
8177 tree new_friend;
8178
8179 if (TREE_CODE (decl) == FUNCTION_DECL
8180 && DECL_TEMPLATE_INSTANTIATION (decl)
8181 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8182 /* This was a friend declared with an explicit template
8183 argument list, e.g.:
8184
8185 friend void f<>(T);
8186
8187 to indicate that f was a template instantiation, not a new
8188 function declaration. Now, we have to figure out what
8189 instantiation of what template. */
8190 {
8191 tree template_id, arglist, fns;
8192 tree new_args;
8193 tree tmpl;
8194 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8195
8196 /* Friend functions are looked up in the containing namespace scope.
8197 We must enter that scope, to avoid finding member functions of the
8198 current class with same name. */
8199 push_nested_namespace (ns);
8200 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8201 tf_warning_or_error, NULL_TREE,
8202 /*integral_constant_expression_p=*/false);
8203 pop_nested_namespace (ns);
8204 arglist = tsubst (DECL_TI_ARGS (decl), args,
8205 tf_warning_or_error, NULL_TREE);
8206 template_id = lookup_template_function (fns, arglist);
8207
8208 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8209 tmpl = determine_specialization (template_id, new_friend,
8210 &new_args,
8211 /*need_member_template=*/0,
8212 TREE_VEC_LENGTH (args),
8213 tsk_none);
8214 return instantiate_template (tmpl, new_args, tf_error);
8215 }
8216
8217 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8218
8219 /* The NEW_FRIEND will look like an instantiation, to the
8220 compiler, but is not an instantiation from the point of view of
8221 the language. For example, we might have had:
8222
8223 template <class T> struct S {
8224 template <class U> friend void f(T, U);
8225 };
8226
8227 Then, in S<int>, template <class U> void f(int, U) is not an
8228 instantiation of anything. */
8229 if (new_friend == error_mark_node)
8230 return error_mark_node;
8231
8232 DECL_USE_TEMPLATE (new_friend) = 0;
8233 if (TREE_CODE (decl) == TEMPLATE_DECL)
8234 {
8235 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8236 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8237 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8238 }
8239
8240 /* The mangled name for the NEW_FRIEND is incorrect. The function
8241 is not a template instantiation and should not be mangled like
8242 one. Therefore, we forget the mangling here; we'll recompute it
8243 later if we need it. */
8244 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8245 {
8246 SET_DECL_RTL (new_friend, NULL);
8247 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8248 }
8249
8250 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8251 {
8252 tree old_decl;
8253 tree new_friend_template_info;
8254 tree new_friend_result_template_info;
8255 tree ns;
8256 int new_friend_is_defn;
8257
8258 /* We must save some information from NEW_FRIEND before calling
8259 duplicate decls since that function will free NEW_FRIEND if
8260 possible. */
8261 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8262 new_friend_is_defn =
8263 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8264 (template_for_substitution (new_friend)))
8265 != NULL_TREE);
8266 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8267 {
8268 /* This declaration is a `primary' template. */
8269 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8270
8271 new_friend_result_template_info
8272 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8273 }
8274 else
8275 new_friend_result_template_info = NULL_TREE;
8276
8277 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8278 if (new_friend_is_defn)
8279 DECL_INITIAL (new_friend) = error_mark_node;
8280
8281 /* Inside pushdecl_namespace_level, we will push into the
8282 current namespace. However, the friend function should go
8283 into the namespace of the template. */
8284 ns = decl_namespace_context (new_friend);
8285 push_nested_namespace (ns);
8286 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8287 pop_nested_namespace (ns);
8288
8289 if (old_decl == error_mark_node)
8290 return error_mark_node;
8291
8292 if (old_decl != new_friend)
8293 {
8294 /* This new friend declaration matched an existing
8295 declaration. For example, given:
8296
8297 template <class T> void f(T);
8298 template <class U> class C {
8299 template <class T> friend void f(T) {}
8300 };
8301
8302 the friend declaration actually provides the definition
8303 of `f', once C has been instantiated for some type. So,
8304 old_decl will be the out-of-class template declaration,
8305 while new_friend is the in-class definition.
8306
8307 But, if `f' was called before this point, the
8308 instantiation of `f' will have DECL_TI_ARGS corresponding
8309 to `T' but not to `U', references to which might appear
8310 in the definition of `f'. Previously, the most general
8311 template for an instantiation of `f' was the out-of-class
8312 version; now it is the in-class version. Therefore, we
8313 run through all specialization of `f', adding to their
8314 DECL_TI_ARGS appropriately. In particular, they need a
8315 new set of outer arguments, corresponding to the
8316 arguments for this class instantiation.
8317
8318 The same situation can arise with something like this:
8319
8320 friend void f(int);
8321 template <class T> class C {
8322 friend void f(T) {}
8323 };
8324
8325 when `C<int>' is instantiated. Now, `f(int)' is defined
8326 in the class. */
8327
8328 if (!new_friend_is_defn)
8329 /* On the other hand, if the in-class declaration does
8330 *not* provide a definition, then we don't want to alter
8331 existing definitions. We can just leave everything
8332 alone. */
8333 ;
8334 else
8335 {
8336 tree new_template = TI_TEMPLATE (new_friend_template_info);
8337 tree new_args = TI_ARGS (new_friend_template_info);
8338
8339 /* Overwrite whatever template info was there before, if
8340 any, with the new template information pertaining to
8341 the declaration. */
8342 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8343
8344 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8345 {
8346 /* We should have called reregister_specialization in
8347 duplicate_decls. */
8348 gcc_assert (retrieve_specialization (new_template,
8349 new_args, 0)
8350 == old_decl);
8351
8352 /* Instantiate it if the global has already been used. */
8353 if (DECL_ODR_USED (old_decl))
8354 instantiate_decl (old_decl, /*defer_ok=*/true,
8355 /*expl_inst_class_mem_p=*/false);
8356 }
8357 else
8358 {
8359 tree t;
8360
8361 /* Indicate that the old function template is a partial
8362 instantiation. */
8363 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8364 = new_friend_result_template_info;
8365
8366 gcc_assert (new_template
8367 == most_general_template (new_template));
8368 gcc_assert (new_template != old_decl);
8369
8370 /* Reassign any specializations already in the hash table
8371 to the new more general template, and add the
8372 additional template args. */
8373 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8374 t != NULL_TREE;
8375 t = TREE_CHAIN (t))
8376 {
8377 tree spec = TREE_VALUE (t);
8378 spec_entry elt;
8379
8380 elt.tmpl = old_decl;
8381 elt.args = DECL_TI_ARGS (spec);
8382 elt.spec = NULL_TREE;
8383
8384 htab_remove_elt (decl_specializations, &elt);
8385
8386 DECL_TI_ARGS (spec)
8387 = add_outermost_template_args (new_args,
8388 DECL_TI_ARGS (spec));
8389
8390 register_specialization
8391 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8392
8393 }
8394 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8395 }
8396 }
8397
8398 /* The information from NEW_FRIEND has been merged into OLD_DECL
8399 by duplicate_decls. */
8400 new_friend = old_decl;
8401 }
8402 }
8403 else
8404 {
8405 tree context = DECL_CONTEXT (new_friend);
8406 bool dependent_p;
8407
8408 /* In the code
8409 template <class T> class C {
8410 template <class U> friend void C1<U>::f (); // case 1
8411 friend void C2<T>::f (); // case 2
8412 };
8413 we only need to make sure CONTEXT is a complete type for
8414 case 2. To distinguish between the two cases, we note that
8415 CONTEXT of case 1 remains dependent type after tsubst while
8416 this isn't true for case 2. */
8417 ++processing_template_decl;
8418 dependent_p = dependent_type_p (context);
8419 --processing_template_decl;
8420
8421 if (!dependent_p
8422 && !complete_type_or_else (context, NULL_TREE))
8423 return error_mark_node;
8424
8425 if (COMPLETE_TYPE_P (context))
8426 {
8427 /* Check to see that the declaration is really present, and,
8428 possibly obtain an improved declaration. */
8429 tree fn = check_classfn (context,
8430 new_friend, NULL_TREE);
8431
8432 if (fn)
8433 new_friend = fn;
8434 }
8435 }
8436
8437 return new_friend;
8438 }
8439
8440 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8441 template arguments, as for tsubst.
8442
8443 Returns an appropriate tsubst'd friend type or error_mark_node on
8444 failure. */
8445
8446 static tree
8447 tsubst_friend_class (tree friend_tmpl, tree args)
8448 {
8449 tree friend_type;
8450 tree tmpl;
8451 tree context;
8452
8453 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8454 {
8455 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8456 return TREE_TYPE (t);
8457 }
8458
8459 context = CP_DECL_CONTEXT (friend_tmpl);
8460
8461 if (context != global_namespace)
8462 {
8463 if (TREE_CODE (context) == NAMESPACE_DECL)
8464 push_nested_namespace (context);
8465 else
8466 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8467 }
8468
8469 /* Look for a class template declaration. We look for hidden names
8470 because two friend declarations of the same template are the
8471 same. For example, in:
8472
8473 struct A {
8474 template <typename> friend class F;
8475 };
8476 template <typename> struct B {
8477 template <typename> friend class F;
8478 };
8479
8480 both F templates are the same. */
8481 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8482 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8483
8484 /* But, if we don't find one, it might be because we're in a
8485 situation like this:
8486
8487 template <class T>
8488 struct S {
8489 template <class U>
8490 friend struct S;
8491 };
8492
8493 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8494 for `S<int>', not the TEMPLATE_DECL. */
8495 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8496 {
8497 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8498 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8499 }
8500
8501 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8502 {
8503 /* The friend template has already been declared. Just
8504 check to see that the declarations match, and install any new
8505 default parameters. We must tsubst the default parameters,
8506 of course. We only need the innermost template parameters
8507 because that is all that redeclare_class_template will look
8508 at. */
8509 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8510 > TMPL_ARGS_DEPTH (args))
8511 {
8512 tree parms;
8513 location_t saved_input_location;
8514 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8515 args, tf_warning_or_error);
8516
8517 saved_input_location = input_location;
8518 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8519 redeclare_class_template (TREE_TYPE (tmpl), parms);
8520 input_location = saved_input_location;
8521
8522 }
8523
8524 friend_type = TREE_TYPE (tmpl);
8525 }
8526 else
8527 {
8528 /* The friend template has not already been declared. In this
8529 case, the instantiation of the template class will cause the
8530 injection of this template into the global scope. */
8531 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8532 if (tmpl == error_mark_node)
8533 return error_mark_node;
8534
8535 /* The new TMPL is not an instantiation of anything, so we
8536 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8537 the new type because that is supposed to be the corresponding
8538 template decl, i.e., TMPL. */
8539 DECL_USE_TEMPLATE (tmpl) = 0;
8540 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8541 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8542 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8543 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8544
8545 /* Inject this template into the global scope. */
8546 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8547 }
8548
8549 if (context != global_namespace)
8550 {
8551 if (TREE_CODE (context) == NAMESPACE_DECL)
8552 pop_nested_namespace (context);
8553 else
8554 pop_nested_class ();
8555 }
8556
8557 return friend_type;
8558 }
8559
8560 /* Returns zero if TYPE cannot be completed later due to circularity.
8561 Otherwise returns one. */
8562
8563 static int
8564 can_complete_type_without_circularity (tree type)
8565 {
8566 if (type == NULL_TREE || type == error_mark_node)
8567 return 0;
8568 else if (COMPLETE_TYPE_P (type))
8569 return 1;
8570 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8571 return can_complete_type_without_circularity (TREE_TYPE (type));
8572 else if (CLASS_TYPE_P (type)
8573 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8574 return 0;
8575 else
8576 return 1;
8577 }
8578
8579 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8580
8581 /* Apply any attributes which had to be deferred until instantiation
8582 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8583 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8584
8585 static void
8586 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8587 tree args, tsubst_flags_t complain, tree in_decl)
8588 {
8589 tree last_dep = NULL_TREE;
8590 tree t;
8591 tree *p;
8592
8593 for (t = attributes; t; t = TREE_CHAIN (t))
8594 if (ATTR_IS_DEPENDENT (t))
8595 {
8596 last_dep = t;
8597 attributes = copy_list (attributes);
8598 break;
8599 }
8600
8601 if (DECL_P (*decl_p))
8602 {
8603 if (TREE_TYPE (*decl_p) == error_mark_node)
8604 return;
8605 p = &DECL_ATTRIBUTES (*decl_p);
8606 }
8607 else
8608 p = &TYPE_ATTRIBUTES (*decl_p);
8609
8610 if (last_dep)
8611 {
8612 tree late_attrs = NULL_TREE;
8613 tree *q = &late_attrs;
8614
8615 for (*p = attributes; *p; )
8616 {
8617 t = *p;
8618 if (ATTR_IS_DEPENDENT (t))
8619 {
8620 *p = TREE_CHAIN (t);
8621 TREE_CHAIN (t) = NULL_TREE;
8622 if ((flag_openmp || flag_cilkplus)
8623 && is_attribute_p ("omp declare simd",
8624 get_attribute_name (t))
8625 && TREE_VALUE (t))
8626 {
8627 tree clauses = TREE_VALUE (TREE_VALUE (t));
8628 clauses = tsubst_omp_clauses (clauses, true, args,
8629 complain, in_decl);
8630 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8631 clauses = finish_omp_clauses (clauses);
8632 tree parms = DECL_ARGUMENTS (*decl_p);
8633 clauses
8634 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8635 if (clauses)
8636 TREE_VALUE (TREE_VALUE (t)) = clauses;
8637 else
8638 TREE_VALUE (t) = NULL_TREE;
8639 }
8640 /* If the first attribute argument is an identifier, don't
8641 pass it through tsubst. Attributes like mode, format,
8642 cleanup and several target specific attributes expect it
8643 unmodified. */
8644 else if (attribute_takes_identifier_p (get_attribute_name (t))
8645 && TREE_VALUE (t))
8646 {
8647 tree chain
8648 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8649 in_decl,
8650 /*integral_constant_expression_p=*/false);
8651 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8652 TREE_VALUE (t)
8653 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8654 chain);
8655 }
8656 else
8657 TREE_VALUE (t)
8658 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8659 /*integral_constant_expression_p=*/false);
8660 *q = t;
8661 q = &TREE_CHAIN (t);
8662 }
8663 else
8664 p = &TREE_CHAIN (t);
8665 }
8666
8667 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8668 }
8669 }
8670
8671 /* Perform (or defer) access check for typedefs that were referenced
8672 from within the template TMPL code.
8673 This is a subroutine of instantiate_decl and instantiate_class_template.
8674 TMPL is the template to consider and TARGS is the list of arguments of
8675 that template. */
8676
8677 static void
8678 perform_typedefs_access_check (tree tmpl, tree targs)
8679 {
8680 location_t saved_location;
8681 unsigned i;
8682 qualified_typedef_usage_t *iter;
8683
8684 if (!tmpl
8685 || (!CLASS_TYPE_P (tmpl)
8686 && TREE_CODE (tmpl) != FUNCTION_DECL))
8687 return;
8688
8689 saved_location = input_location;
8690 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8691 {
8692 tree type_decl = iter->typedef_decl;
8693 tree type_scope = iter->context;
8694
8695 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8696 continue;
8697
8698 if (uses_template_parms (type_decl))
8699 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8700 if (uses_template_parms (type_scope))
8701 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8702
8703 /* Make access check error messages point to the location
8704 of the use of the typedef. */
8705 input_location = iter->locus;
8706 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8707 type_decl, type_decl,
8708 tf_warning_or_error);
8709 }
8710 input_location = saved_location;
8711 }
8712
8713 static tree
8714 instantiate_class_template_1 (tree type)
8715 {
8716 tree templ, args, pattern, t, member;
8717 tree typedecl;
8718 tree pbinfo;
8719 tree base_list;
8720 unsigned int saved_maximum_field_alignment;
8721 tree fn_context;
8722
8723 if (type == error_mark_node)
8724 return error_mark_node;
8725
8726 if (COMPLETE_OR_OPEN_TYPE_P (type)
8727 || uses_template_parms (type))
8728 return type;
8729
8730 /* Figure out which template is being instantiated. */
8731 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8732 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8733
8734 /* Determine what specialization of the original template to
8735 instantiate. */
8736 t = most_specialized_class (type, tf_warning_or_error);
8737 if (t == error_mark_node)
8738 {
8739 TYPE_BEING_DEFINED (type) = 1;
8740 return error_mark_node;
8741 }
8742 else if (t)
8743 {
8744 /* This TYPE is actually an instantiation of a partial
8745 specialization. We replace the innermost set of ARGS with
8746 the arguments appropriate for substitution. For example,
8747 given:
8748
8749 template <class T> struct S {};
8750 template <class T> struct S<T*> {};
8751
8752 and supposing that we are instantiating S<int*>, ARGS will
8753 presently be {int*} -- but we need {int}. */
8754 pattern = TREE_TYPE (t);
8755 args = TREE_PURPOSE (t);
8756 }
8757 else
8758 {
8759 pattern = TREE_TYPE (templ);
8760 args = CLASSTYPE_TI_ARGS (type);
8761 }
8762
8763 /* If the template we're instantiating is incomplete, then clearly
8764 there's nothing we can do. */
8765 if (!COMPLETE_TYPE_P (pattern))
8766 return type;
8767
8768 /* If we've recursively instantiated too many templates, stop. */
8769 if (! push_tinst_level (type))
8770 return type;
8771
8772 /* Now we're really doing the instantiation. Mark the type as in
8773 the process of being defined. */
8774 TYPE_BEING_DEFINED (type) = 1;
8775
8776 /* We may be in the middle of deferred access check. Disable
8777 it now. */
8778 push_deferring_access_checks (dk_no_deferred);
8779
8780 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8781 if (!fn_context)
8782 push_to_top_level ();
8783 /* Use #pragma pack from the template context. */
8784 saved_maximum_field_alignment = maximum_field_alignment;
8785 maximum_field_alignment = TYPE_PRECISION (pattern);
8786
8787 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8788
8789 /* Set the input location to the most specialized template definition.
8790 This is needed if tsubsting causes an error. */
8791 typedecl = TYPE_MAIN_DECL (pattern);
8792 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8793 DECL_SOURCE_LOCATION (typedecl);
8794
8795 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8796 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8797 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8798 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8799 if (ANON_AGGR_TYPE_P (pattern))
8800 SET_ANON_AGGR_TYPE_P (type);
8801 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8802 {
8803 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8804 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8805 /* Adjust visibility for template arguments. */
8806 determine_visibility (TYPE_MAIN_DECL (type));
8807 }
8808 if (CLASS_TYPE_P (type))
8809 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8810
8811 pbinfo = TYPE_BINFO (pattern);
8812
8813 /* We should never instantiate a nested class before its enclosing
8814 class; we need to look up the nested class by name before we can
8815 instantiate it, and that lookup should instantiate the enclosing
8816 class. */
8817 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8818 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8819
8820 base_list = NULL_TREE;
8821 if (BINFO_N_BASE_BINFOS (pbinfo))
8822 {
8823 tree pbase_binfo;
8824 tree pushed_scope;
8825 int i;
8826
8827 /* We must enter the scope containing the type, as that is where
8828 the accessibility of types named in dependent bases are
8829 looked up from. */
8830 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8831
8832 /* Substitute into each of the bases to determine the actual
8833 basetypes. */
8834 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8835 {
8836 tree base;
8837 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8838 tree expanded_bases = NULL_TREE;
8839 int idx, len = 1;
8840
8841 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8842 {
8843 expanded_bases =
8844 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8845 args, tf_error, NULL_TREE);
8846 if (expanded_bases == error_mark_node)
8847 continue;
8848
8849 len = TREE_VEC_LENGTH (expanded_bases);
8850 }
8851
8852 for (idx = 0; idx < len; idx++)
8853 {
8854 if (expanded_bases)
8855 /* Extract the already-expanded base class. */
8856 base = TREE_VEC_ELT (expanded_bases, idx);
8857 else
8858 /* Substitute to figure out the base class. */
8859 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8860 NULL_TREE);
8861
8862 if (base == error_mark_node)
8863 continue;
8864
8865 base_list = tree_cons (access, base, base_list);
8866 if (BINFO_VIRTUAL_P (pbase_binfo))
8867 TREE_TYPE (base_list) = integer_type_node;
8868 }
8869 }
8870
8871 /* The list is now in reverse order; correct that. */
8872 base_list = nreverse (base_list);
8873
8874 if (pushed_scope)
8875 pop_scope (pushed_scope);
8876 }
8877 /* Now call xref_basetypes to set up all the base-class
8878 information. */
8879 xref_basetypes (type, base_list);
8880
8881 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8882 (int) ATTR_FLAG_TYPE_IN_PLACE,
8883 args, tf_error, NULL_TREE);
8884 fixup_attribute_variants (type);
8885
8886 /* Now that our base classes are set up, enter the scope of the
8887 class, so that name lookups into base classes, etc. will work
8888 correctly. This is precisely analogous to what we do in
8889 begin_class_definition when defining an ordinary non-template
8890 class, except we also need to push the enclosing classes. */
8891 push_nested_class (type);
8892
8893 /* Now members are processed in the order of declaration. */
8894 for (member = CLASSTYPE_DECL_LIST (pattern);
8895 member; member = TREE_CHAIN (member))
8896 {
8897 tree t = TREE_VALUE (member);
8898
8899 if (TREE_PURPOSE (member))
8900 {
8901 if (TYPE_P (t))
8902 {
8903 /* Build new CLASSTYPE_NESTED_UTDS. */
8904
8905 tree newtag;
8906 bool class_template_p;
8907
8908 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8909 && TYPE_LANG_SPECIFIC (t)
8910 && CLASSTYPE_IS_TEMPLATE (t));
8911 /* If the member is a class template, then -- even after
8912 substitution -- there may be dependent types in the
8913 template argument list for the class. We increment
8914 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8915 that function will assume that no types are dependent
8916 when outside of a template. */
8917 if (class_template_p)
8918 ++processing_template_decl;
8919 newtag = tsubst (t, args, tf_error, NULL_TREE);
8920 if (class_template_p)
8921 --processing_template_decl;
8922 if (newtag == error_mark_node)
8923 continue;
8924
8925 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8926 {
8927 tree name = TYPE_IDENTIFIER (t);
8928
8929 if (class_template_p)
8930 /* Unfortunately, lookup_template_class sets
8931 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8932 instantiation (i.e., for the type of a member
8933 template class nested within a template class.)
8934 This behavior is required for
8935 maybe_process_partial_specialization to work
8936 correctly, but is not accurate in this case;
8937 the TAG is not an instantiation of anything.
8938 (The corresponding TEMPLATE_DECL is an
8939 instantiation, but the TYPE is not.) */
8940 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8941
8942 /* Now, we call pushtag to put this NEWTAG into the scope of
8943 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8944 pushtag calling push_template_decl. We don't have to do
8945 this for enums because it will already have been done in
8946 tsubst_enum. */
8947 if (name)
8948 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8949 pushtag (name, newtag, /*tag_scope=*/ts_current);
8950 }
8951 }
8952 else if (DECL_DECLARES_FUNCTION_P (t))
8953 {
8954 /* Build new TYPE_METHODS. */
8955 tree r;
8956
8957 if (TREE_CODE (t) == TEMPLATE_DECL)
8958 ++processing_template_decl;
8959 r = tsubst (t, args, tf_error, NULL_TREE);
8960 if (TREE_CODE (t) == TEMPLATE_DECL)
8961 --processing_template_decl;
8962 set_current_access_from_decl (r);
8963 finish_member_declaration (r);
8964 /* Instantiate members marked with attribute used. */
8965 if (r != error_mark_node && DECL_PRESERVE_P (r))
8966 mark_used (r);
8967 if (TREE_CODE (r) == FUNCTION_DECL
8968 && DECL_OMP_DECLARE_REDUCTION_P (r))
8969 cp_check_omp_declare_reduction (r);
8970 }
8971 else
8972 {
8973 /* Build new TYPE_FIELDS. */
8974 if (TREE_CODE (t) == STATIC_ASSERT)
8975 {
8976 tree condition;
8977
8978 ++c_inhibit_evaluation_warnings;
8979 condition =
8980 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8981 tf_warning_or_error, NULL_TREE,
8982 /*integral_constant_expression_p=*/true);
8983 --c_inhibit_evaluation_warnings;
8984
8985 finish_static_assert (condition,
8986 STATIC_ASSERT_MESSAGE (t),
8987 STATIC_ASSERT_SOURCE_LOCATION (t),
8988 /*member_p=*/true);
8989 }
8990 else if (TREE_CODE (t) != CONST_DECL)
8991 {
8992 tree r;
8993 tree vec = NULL_TREE;
8994 int len = 1;
8995
8996 /* The file and line for this declaration, to
8997 assist in error message reporting. Since we
8998 called push_tinst_level above, we don't need to
8999 restore these. */
9000 input_location = DECL_SOURCE_LOCATION (t);
9001
9002 if (TREE_CODE (t) == TEMPLATE_DECL)
9003 ++processing_template_decl;
9004 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
9005 if (TREE_CODE (t) == TEMPLATE_DECL)
9006 --processing_template_decl;
9007
9008 if (TREE_CODE (r) == TREE_VEC)
9009 {
9010 /* A capture pack became multiple fields. */
9011 vec = r;
9012 len = TREE_VEC_LENGTH (vec);
9013 }
9014
9015 for (int i = 0; i < len; ++i)
9016 {
9017 if (vec)
9018 r = TREE_VEC_ELT (vec, i);
9019 if (VAR_P (r))
9020 {
9021 /* In [temp.inst]:
9022
9023 [t]he initialization (and any associated
9024 side-effects) of a static data member does
9025 not occur unless the static data member is
9026 itself used in a way that requires the
9027 definition of the static data member to
9028 exist.
9029
9030 Therefore, we do not substitute into the
9031 initialized for the static data member here. */
9032 finish_static_data_member_decl
9033 (r,
9034 /*init=*/NULL_TREE,
9035 /*init_const_expr_p=*/false,
9036 /*asmspec_tree=*/NULL_TREE,
9037 /*flags=*/0);
9038 /* Instantiate members marked with attribute used. */
9039 if (r != error_mark_node && DECL_PRESERVE_P (r))
9040 mark_used (r);
9041 }
9042 else if (TREE_CODE (r) == FIELD_DECL)
9043 {
9044 /* Determine whether R has a valid type and can be
9045 completed later. If R is invalid, then its type
9046 is replaced by error_mark_node. */
9047 tree rtype = TREE_TYPE (r);
9048 if (can_complete_type_without_circularity (rtype))
9049 complete_type (rtype);
9050
9051 if (!COMPLETE_TYPE_P (rtype))
9052 {
9053 cxx_incomplete_type_error (r, rtype);
9054 TREE_TYPE (r) = error_mark_node;
9055 }
9056 }
9057
9058 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9059 such a thing will already have been added to the field
9060 list by tsubst_enum in finish_member_declaration in the
9061 CLASSTYPE_NESTED_UTDS case above. */
9062 if (!(TREE_CODE (r) == TYPE_DECL
9063 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9064 && DECL_ARTIFICIAL (r)))
9065 {
9066 set_current_access_from_decl (r);
9067 finish_member_declaration (r);
9068 }
9069 }
9070 }
9071 }
9072 }
9073 else
9074 {
9075 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9076 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9077 {
9078 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9079
9080 tree friend_type = t;
9081 bool adjust_processing_template_decl = false;
9082
9083 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9084 {
9085 /* template <class T> friend class C; */
9086 friend_type = tsubst_friend_class (friend_type, args);
9087 adjust_processing_template_decl = true;
9088 }
9089 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9090 {
9091 /* template <class T> friend class C::D; */
9092 friend_type = tsubst (friend_type, args,
9093 tf_warning_or_error, NULL_TREE);
9094 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9095 friend_type = TREE_TYPE (friend_type);
9096 adjust_processing_template_decl = true;
9097 }
9098 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9099 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9100 {
9101 /* This could be either
9102
9103 friend class T::C;
9104
9105 when dependent_type_p is false or
9106
9107 template <class U> friend class T::C;
9108
9109 otherwise. */
9110 friend_type = tsubst (friend_type, args,
9111 tf_warning_or_error, NULL_TREE);
9112 /* Bump processing_template_decl for correct
9113 dependent_type_p calculation. */
9114 ++processing_template_decl;
9115 if (dependent_type_p (friend_type))
9116 adjust_processing_template_decl = true;
9117 --processing_template_decl;
9118 }
9119 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9120 && hidden_name_p (TYPE_NAME (friend_type)))
9121 {
9122 /* friend class C;
9123
9124 where C hasn't been declared yet. Let's lookup name
9125 from namespace scope directly, bypassing any name that
9126 come from dependent base class. */
9127 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9128
9129 /* The call to xref_tag_from_type does injection for friend
9130 classes. */
9131 push_nested_namespace (ns);
9132 friend_type =
9133 xref_tag_from_type (friend_type, NULL_TREE,
9134 /*tag_scope=*/ts_current);
9135 pop_nested_namespace (ns);
9136 }
9137 else if (uses_template_parms (friend_type))
9138 /* friend class C<T>; */
9139 friend_type = tsubst (friend_type, args,
9140 tf_warning_or_error, NULL_TREE);
9141 /* Otherwise it's
9142
9143 friend class C;
9144
9145 where C is already declared or
9146
9147 friend class C<int>;
9148
9149 We don't have to do anything in these cases. */
9150
9151 if (adjust_processing_template_decl)
9152 /* Trick make_friend_class into realizing that the friend
9153 we're adding is a template, not an ordinary class. It's
9154 important that we use make_friend_class since it will
9155 perform some error-checking and output cross-reference
9156 information. */
9157 ++processing_template_decl;
9158
9159 if (friend_type != error_mark_node)
9160 make_friend_class (type, friend_type, /*complain=*/false);
9161
9162 if (adjust_processing_template_decl)
9163 --processing_template_decl;
9164 }
9165 else
9166 {
9167 /* Build new DECL_FRIENDLIST. */
9168 tree r;
9169
9170 /* The file and line for this declaration, to
9171 assist in error message reporting. Since we
9172 called push_tinst_level above, we don't need to
9173 restore these. */
9174 input_location = DECL_SOURCE_LOCATION (t);
9175
9176 if (TREE_CODE (t) == TEMPLATE_DECL)
9177 {
9178 ++processing_template_decl;
9179 push_deferring_access_checks (dk_no_check);
9180 }
9181
9182 r = tsubst_friend_function (t, args);
9183 add_friend (type, r, /*complain=*/false);
9184 if (TREE_CODE (t) == TEMPLATE_DECL)
9185 {
9186 pop_deferring_access_checks ();
9187 --processing_template_decl;
9188 }
9189 }
9190 }
9191 }
9192
9193 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9194 {
9195 tree decl = lambda_function (type);
9196 if (decl)
9197 {
9198 if (!DECL_TEMPLATE_INFO (decl)
9199 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9200 instantiate_decl (decl, false, false);
9201
9202 /* We need to instantiate the capture list from the template
9203 after we've instantiated the closure members, but before we
9204 consider adding the conversion op. Also keep any captures
9205 that may have been added during instantiation of the op(). */
9206 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9207 tree tmpl_cap
9208 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9209 args, tf_warning_or_error, NULL_TREE,
9210 false, false);
9211
9212 LAMBDA_EXPR_CAPTURE_LIST (expr)
9213 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9214
9215 maybe_add_lambda_conv_op (type);
9216 }
9217 else
9218 gcc_assert (errorcount);
9219 }
9220
9221 /* Set the file and line number information to whatever is given for
9222 the class itself. This puts error messages involving generated
9223 implicit functions at a predictable point, and the same point
9224 that would be used for non-template classes. */
9225 input_location = DECL_SOURCE_LOCATION (typedecl);
9226
9227 unreverse_member_declarations (type);
9228 finish_struct_1 (type);
9229 TYPE_BEING_DEFINED (type) = 0;
9230
9231 /* We don't instantiate default arguments for member functions. 14.7.1:
9232
9233 The implicit instantiation of a class template specialization causes
9234 the implicit instantiation of the declarations, but not of the
9235 definitions or default arguments, of the class member functions,
9236 member classes, static data members and member templates.... */
9237
9238 /* Some typedefs referenced from within the template code need to be access
9239 checked at template instantiation time, i.e now. These types were
9240 added to the template at parsing time. Let's get those and perform
9241 the access checks then. */
9242 perform_typedefs_access_check (pattern, args);
9243 perform_deferred_access_checks (tf_warning_or_error);
9244 pop_nested_class ();
9245 maximum_field_alignment = saved_maximum_field_alignment;
9246 if (!fn_context)
9247 pop_from_top_level ();
9248 pop_deferring_access_checks ();
9249 pop_tinst_level ();
9250
9251 /* The vtable for a template class can be emitted in any translation
9252 unit in which the class is instantiated. When there is no key
9253 method, however, finish_struct_1 will already have added TYPE to
9254 the keyed_classes list. */
9255 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9256 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9257
9258 return type;
9259 }
9260
9261 /* Wrapper for instantiate_class_template_1. */
9262
9263 tree
9264 instantiate_class_template (tree type)
9265 {
9266 tree ret;
9267 timevar_push (TV_TEMPLATE_INST);
9268 ret = instantiate_class_template_1 (type);
9269 timevar_pop (TV_TEMPLATE_INST);
9270 return ret;
9271 }
9272
9273 static tree
9274 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9275 {
9276 tree r;
9277
9278 if (!t)
9279 r = t;
9280 else if (TYPE_P (t))
9281 r = tsubst (t, args, complain, in_decl);
9282 else
9283 {
9284 if (!(complain & tf_warning))
9285 ++c_inhibit_evaluation_warnings;
9286 r = tsubst_expr (t, args, complain, in_decl,
9287 /*integral_constant_expression_p=*/true);
9288 if (!(complain & tf_warning))
9289 --c_inhibit_evaluation_warnings;
9290 }
9291 return r;
9292 }
9293
9294 /* Given a function parameter pack TMPL_PARM and some function parameters
9295 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9296 and set *SPEC_P to point at the next point in the list. */
9297
9298 static tree
9299 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9300 {
9301 /* Collect all of the extra "packed" parameters into an
9302 argument pack. */
9303 tree parmvec;
9304 tree parmtypevec;
9305 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9306 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9307 tree spec_parm = *spec_p;
9308 int i, len;
9309
9310 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9311 if (tmpl_parm
9312 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9313 break;
9314
9315 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9316 parmvec = make_tree_vec (len);
9317 parmtypevec = make_tree_vec (len);
9318 spec_parm = *spec_p;
9319 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9320 {
9321 TREE_VEC_ELT (parmvec, i) = spec_parm;
9322 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9323 }
9324
9325 /* Build the argument packs. */
9326 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9327 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9328 TREE_TYPE (argpack) = argtypepack;
9329 *spec_p = spec_parm;
9330
9331 return argpack;
9332 }
9333
9334 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9335 NONTYPE_ARGUMENT_PACK. */
9336
9337 static tree
9338 make_fnparm_pack (tree spec_parm)
9339 {
9340 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9341 }
9342
9343 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9344 pack expansion. */
9345
9346 static bool
9347 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9348 {
9349 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9350 if (i >= TREE_VEC_LENGTH (vec))
9351 return false;
9352 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9353 }
9354
9355
9356 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9357
9358 static tree
9359 make_argument_pack_select (tree arg_pack, unsigned index)
9360 {
9361 tree aps = make_node (ARGUMENT_PACK_SELECT);
9362
9363 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9364 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9365
9366 return aps;
9367 }
9368
9369 /* This is a subroutine of tsubst_pack_expansion.
9370
9371 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9372 mechanism to store the (non complete list of) arguments of the
9373 substitution and return a non substituted pack expansion, in order
9374 to wait for when we have enough arguments to really perform the
9375 substitution. */
9376
9377 static bool
9378 use_pack_expansion_extra_args_p (tree parm_packs,
9379 int arg_pack_len,
9380 bool has_empty_arg)
9381 {
9382 /* If one pack has an expansion and another pack has a normal
9383 argument or if one pack has an empty argument and an another
9384 one hasn't then tsubst_pack_expansion cannot perform the
9385 substitution and need to fall back on the
9386 PACK_EXPANSION_EXTRA mechanism. */
9387 if (parm_packs == NULL_TREE)
9388 return false;
9389 else if (has_empty_arg)
9390 return true;
9391
9392 bool has_expansion_arg = false;
9393 for (int i = 0 ; i < arg_pack_len; ++i)
9394 {
9395 bool has_non_expansion_arg = false;
9396 for (tree parm_pack = parm_packs;
9397 parm_pack;
9398 parm_pack = TREE_CHAIN (parm_pack))
9399 {
9400 tree arg = TREE_VALUE (parm_pack);
9401
9402 if (argument_pack_element_is_expansion_p (arg, i))
9403 has_expansion_arg = true;
9404 else
9405 has_non_expansion_arg = true;
9406 }
9407
9408 if (has_expansion_arg && has_non_expansion_arg)
9409 return true;
9410 }
9411 return false;
9412 }
9413
9414 /* [temp.variadic]/6 says that:
9415
9416 The instantiation of a pack expansion [...]
9417 produces a list E1,E2, ..., En, where N is the number of elements
9418 in the pack expansion parameters.
9419
9420 This subroutine of tsubst_pack_expansion produces one of these Ei.
9421
9422 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9423 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9424 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9425 INDEX is the index 'i' of the element Ei to produce. ARGS,
9426 COMPLAIN, and IN_DECL are the same parameters as for the
9427 tsubst_pack_expansion function.
9428
9429 The function returns the resulting Ei upon successful completion,
9430 or error_mark_node.
9431
9432 Note that this function possibly modifies the ARGS parameter, so
9433 it's the responsibility of the caller to restore it. */
9434
9435 static tree
9436 gen_elem_of_pack_expansion_instantiation (tree pattern,
9437 tree parm_packs,
9438 unsigned index,
9439 tree args /* This parm gets
9440 modified. */,
9441 tsubst_flags_t complain,
9442 tree in_decl)
9443 {
9444 tree t;
9445 bool ith_elem_is_expansion = false;
9446
9447 /* For each parameter pack, change the substitution of the parameter
9448 pack to the ith argument in its argument pack, then expand the
9449 pattern. */
9450 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9451 {
9452 tree parm = TREE_PURPOSE (pack);
9453 tree arg_pack = TREE_VALUE (pack);
9454 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9455
9456 ith_elem_is_expansion |=
9457 argument_pack_element_is_expansion_p (arg_pack, index);
9458
9459 /* Select the Ith argument from the pack. */
9460 if (TREE_CODE (parm) == PARM_DECL
9461 || TREE_CODE (parm) == FIELD_DECL)
9462 {
9463 if (index == 0)
9464 {
9465 aps = make_argument_pack_select (arg_pack, index);
9466 mark_used (parm);
9467 register_local_specialization (aps, parm);
9468 }
9469 else
9470 aps = retrieve_local_specialization (parm);
9471 }
9472 else
9473 {
9474 int idx, level;
9475 template_parm_level_and_index (parm, &level, &idx);
9476
9477 if (index == 0)
9478 {
9479 aps = make_argument_pack_select (arg_pack, index);
9480 /* Update the corresponding argument. */
9481 TMPL_ARG (args, level, idx) = aps;
9482 }
9483 else
9484 /* Re-use the ARGUMENT_PACK_SELECT. */
9485 aps = TMPL_ARG (args, level, idx);
9486 }
9487 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9488 }
9489
9490 /* Substitute into the PATTERN with the (possibly altered)
9491 arguments. */
9492 if (!TYPE_P (pattern))
9493 t = tsubst_expr (pattern, args, complain, in_decl,
9494 /*integral_constant_expression_p=*/false);
9495 else
9496 t = tsubst (pattern, args, complain, in_decl);
9497
9498 /* If the Ith argument pack element is a pack expansion, then
9499 the Ith element resulting from the substituting is going to
9500 be a pack expansion as well. */
9501 if (ith_elem_is_expansion)
9502 t = make_pack_expansion (t);
9503
9504 return t;
9505 }
9506
9507 /* Substitute ARGS into T, which is an pack expansion
9508 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9509 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9510 (if only a partial substitution could be performed) or
9511 ERROR_MARK_NODE if there was an error. */
9512 tree
9513 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9514 tree in_decl)
9515 {
9516 tree pattern;
9517 tree pack, packs = NULL_TREE;
9518 bool unsubstituted_packs = false;
9519 int i, len = -1;
9520 tree result;
9521 struct pointer_map_t *saved_local_specializations = NULL;
9522 bool need_local_specializations = false;
9523 int levels;
9524
9525 gcc_assert (PACK_EXPANSION_P (t));
9526 pattern = PACK_EXPANSION_PATTERN (t);
9527
9528 /* Add in any args remembered from an earlier partial instantiation. */
9529 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9530
9531 levels = TMPL_ARGS_DEPTH (args);
9532
9533 /* Determine the argument packs that will instantiate the parameter
9534 packs used in the expansion expression. While we're at it,
9535 compute the number of arguments to be expanded and make sure it
9536 is consistent. */
9537 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9538 pack = TREE_CHAIN (pack))
9539 {
9540 tree parm_pack = TREE_VALUE (pack);
9541 tree arg_pack = NULL_TREE;
9542 tree orig_arg = NULL_TREE;
9543 int level = 0;
9544
9545 if (TREE_CODE (parm_pack) == BASES)
9546 {
9547 if (BASES_DIRECT (parm_pack))
9548 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9549 args, complain, in_decl, false));
9550 else
9551 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9552 args, complain, in_decl, false));
9553 }
9554 if (TREE_CODE (parm_pack) == PARM_DECL)
9555 {
9556 if (PACK_EXPANSION_LOCAL_P (t))
9557 arg_pack = retrieve_local_specialization (parm_pack);
9558 else
9559 {
9560 /* We can't rely on local_specializations for a parameter
9561 name used later in a function declaration (such as in a
9562 late-specified return type). Even if it exists, it might
9563 have the wrong value for a recursive call. Just make a
9564 dummy decl, since it's only used for its type. */
9565 arg_pack = tsubst_decl (parm_pack, args, complain);
9566 if (arg_pack && DECL_PACK_P (arg_pack))
9567 /* Partial instantiation of the parm_pack, we can't build
9568 up an argument pack yet. */
9569 arg_pack = NULL_TREE;
9570 else
9571 arg_pack = make_fnparm_pack (arg_pack);
9572 need_local_specializations = true;
9573 }
9574 }
9575 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9576 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9577 else
9578 {
9579 int idx;
9580 template_parm_level_and_index (parm_pack, &level, &idx);
9581
9582 if (level <= levels)
9583 arg_pack = TMPL_ARG (args, level, idx);
9584 }
9585
9586 orig_arg = arg_pack;
9587 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9588 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9589
9590 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9591 /* This can only happen if we forget to expand an argument
9592 pack somewhere else. Just return an error, silently. */
9593 {
9594 result = make_tree_vec (1);
9595 TREE_VEC_ELT (result, 0) = error_mark_node;
9596 return result;
9597 }
9598
9599 if (arg_pack)
9600 {
9601 int my_len =
9602 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9603
9604 /* Don't bother trying to do a partial substitution with
9605 incomplete packs; we'll try again after deduction. */
9606 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9607 return t;
9608
9609 if (len < 0)
9610 len = my_len;
9611 else if (len != my_len)
9612 {
9613 if (!(complain & tf_error))
9614 /* Fail quietly. */;
9615 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9616 error ("mismatched argument pack lengths while expanding "
9617 "%<%T%>",
9618 pattern);
9619 else
9620 error ("mismatched argument pack lengths while expanding "
9621 "%<%E%>",
9622 pattern);
9623 return error_mark_node;
9624 }
9625
9626 /* Keep track of the parameter packs and their corresponding
9627 argument packs. */
9628 packs = tree_cons (parm_pack, arg_pack, packs);
9629 TREE_TYPE (packs) = orig_arg;
9630 }
9631 else
9632 {
9633 /* We can't substitute for this parameter pack. We use a flag as
9634 well as the missing_level counter because function parameter
9635 packs don't have a level. */
9636 unsubstituted_packs = true;
9637 }
9638 }
9639
9640 /* We cannot expand this expansion expression, because we don't have
9641 all of the argument packs we need. */
9642 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9643 {
9644 /* We got some full packs, but we can't substitute them in until we
9645 have values for all the packs. So remember these until then. */
9646
9647 t = make_pack_expansion (pattern);
9648 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9649 return t;
9650 }
9651 else if (unsubstituted_packs)
9652 {
9653 /* There were no real arguments, we're just replacing a parameter
9654 pack with another version of itself. Substitute into the
9655 pattern and return a PACK_EXPANSION_*. The caller will need to
9656 deal with that. */
9657 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9658 t = tsubst_expr (pattern, args, complain, in_decl,
9659 /*integral_constant_expression_p=*/false);
9660 else
9661 t = tsubst (pattern, args, complain, in_decl);
9662 t = make_pack_expansion (t);
9663 return t;
9664 }
9665
9666 gcc_assert (len >= 0);
9667
9668 if (need_local_specializations)
9669 {
9670 /* We're in a late-specified return type, so create our own local
9671 specializations map; the current map is either NULL or (in the
9672 case of recursive unification) might have bindings that we don't
9673 want to use or alter. */
9674 saved_local_specializations = local_specializations;
9675 local_specializations = pointer_map_create ();
9676 }
9677
9678 /* For each argument in each argument pack, substitute into the
9679 pattern. */
9680 result = make_tree_vec (len);
9681 for (i = 0; i < len; ++i)
9682 {
9683 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9684 i,
9685 args, complain,
9686 in_decl);
9687 TREE_VEC_ELT (result, i) = t;
9688 if (t == error_mark_node)
9689 {
9690 result = error_mark_node;
9691 break;
9692 }
9693 }
9694
9695 /* Update ARGS to restore the substitution from parameter packs to
9696 their argument packs. */
9697 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9698 {
9699 tree parm = TREE_PURPOSE (pack);
9700
9701 if (TREE_CODE (parm) == PARM_DECL
9702 || TREE_CODE (parm) == FIELD_DECL)
9703 register_local_specialization (TREE_TYPE (pack), parm);
9704 else
9705 {
9706 int idx, level;
9707
9708 if (TREE_VALUE (pack) == NULL_TREE)
9709 continue;
9710
9711 template_parm_level_and_index (parm, &level, &idx);
9712
9713 /* Update the corresponding argument. */
9714 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9715 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9716 TREE_TYPE (pack);
9717 else
9718 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9719 }
9720 }
9721
9722 if (need_local_specializations)
9723 {
9724 pointer_map_destroy (local_specializations);
9725 local_specializations = saved_local_specializations;
9726 }
9727
9728 return result;
9729 }
9730
9731 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9732 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9733 parameter packs; all parms generated from a function parameter pack will
9734 have the same DECL_PARM_INDEX. */
9735
9736 tree
9737 get_pattern_parm (tree parm, tree tmpl)
9738 {
9739 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9740 tree patparm;
9741
9742 if (DECL_ARTIFICIAL (parm))
9743 {
9744 for (patparm = DECL_ARGUMENTS (pattern);
9745 patparm; patparm = DECL_CHAIN (patparm))
9746 if (DECL_ARTIFICIAL (patparm)
9747 && DECL_NAME (parm) == DECL_NAME (patparm))
9748 break;
9749 }
9750 else
9751 {
9752 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9753 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9754 gcc_assert (DECL_PARM_INDEX (patparm)
9755 == DECL_PARM_INDEX (parm));
9756 }
9757
9758 return patparm;
9759 }
9760
9761 /* Substitute ARGS into the vector or list of template arguments T. */
9762
9763 static tree
9764 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9765 {
9766 tree orig_t = t;
9767 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9768 tree *elts;
9769
9770 if (t == error_mark_node)
9771 return error_mark_node;
9772
9773 len = TREE_VEC_LENGTH (t);
9774 elts = XALLOCAVEC (tree, len);
9775
9776 for (i = 0; i < len; i++)
9777 {
9778 tree orig_arg = TREE_VEC_ELT (t, i);
9779 tree new_arg;
9780
9781 if (TREE_CODE (orig_arg) == TREE_VEC)
9782 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9783 else if (PACK_EXPANSION_P (orig_arg))
9784 {
9785 /* Substitute into an expansion expression. */
9786 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9787
9788 if (TREE_CODE (new_arg) == TREE_VEC)
9789 /* Add to the expanded length adjustment the number of
9790 expanded arguments. We subtract one from this
9791 measurement, because the argument pack expression
9792 itself is already counted as 1 in
9793 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9794 the argument pack is empty. */
9795 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9796 }
9797 else if (ARGUMENT_PACK_P (orig_arg))
9798 {
9799 /* Substitute into each of the arguments. */
9800 new_arg = TYPE_P (orig_arg)
9801 ? cxx_make_type (TREE_CODE (orig_arg))
9802 : make_node (TREE_CODE (orig_arg));
9803
9804 SET_ARGUMENT_PACK_ARGS (
9805 new_arg,
9806 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9807 args, complain, in_decl));
9808
9809 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9810 new_arg = error_mark_node;
9811
9812 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9813 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9814 complain, in_decl);
9815 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9816
9817 if (TREE_TYPE (new_arg) == error_mark_node)
9818 new_arg = error_mark_node;
9819 }
9820 }
9821 else
9822 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9823
9824 if (new_arg == error_mark_node)
9825 return error_mark_node;
9826
9827 elts[i] = new_arg;
9828 if (new_arg != orig_arg)
9829 need_new = 1;
9830 }
9831
9832 if (!need_new)
9833 return t;
9834
9835 /* Make space for the expanded arguments coming from template
9836 argument packs. */
9837 t = make_tree_vec (len + expanded_len_adjust);
9838 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9839 arguments for a member template.
9840 In that case each TREE_VEC in ORIG_T represents a level of template
9841 arguments, and ORIG_T won't carry any non defaulted argument count.
9842 It will rather be the nested TREE_VECs that will carry one.
9843 In other words, ORIG_T carries a non defaulted argument count only
9844 if it doesn't contain any nested TREE_VEC. */
9845 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9846 {
9847 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9848 count += expanded_len_adjust;
9849 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9850 }
9851 for (i = 0, out = 0; i < len; i++)
9852 {
9853 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9854 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9855 && TREE_CODE (elts[i]) == TREE_VEC)
9856 {
9857 int idx;
9858
9859 /* Now expand the template argument pack "in place". */
9860 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9861 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9862 }
9863 else
9864 {
9865 TREE_VEC_ELT (t, out) = elts[i];
9866 out++;
9867 }
9868 }
9869
9870 return t;
9871 }
9872
9873 /* Return the result of substituting ARGS into the template parameters
9874 given by PARMS. If there are m levels of ARGS and m + n levels of
9875 PARMS, then the result will contain n levels of PARMS. For
9876 example, if PARMS is `template <class T> template <class U>
9877 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9878 result will be `template <int*, double, class V>'. */
9879
9880 static tree
9881 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9882 {
9883 tree r = NULL_TREE;
9884 tree* new_parms;
9885
9886 /* When substituting into a template, we must set
9887 PROCESSING_TEMPLATE_DECL as the template parameters may be
9888 dependent if they are based on one-another, and the dependency
9889 predicates are short-circuit outside of templates. */
9890 ++processing_template_decl;
9891
9892 for (new_parms = &r;
9893 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9894 new_parms = &(TREE_CHAIN (*new_parms)),
9895 parms = TREE_CHAIN (parms))
9896 {
9897 tree new_vec =
9898 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9899 int i;
9900
9901 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9902 {
9903 tree tuple;
9904
9905 if (parms == error_mark_node)
9906 continue;
9907
9908 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9909
9910 if (tuple == error_mark_node)
9911 continue;
9912
9913 TREE_VEC_ELT (new_vec, i) =
9914 tsubst_template_parm (tuple, args, complain);
9915 }
9916
9917 *new_parms =
9918 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9919 - TMPL_ARGS_DEPTH (args)),
9920 new_vec, NULL_TREE);
9921 }
9922
9923 --processing_template_decl;
9924
9925 return r;
9926 }
9927
9928 /* Return the result of substituting ARGS into one template parameter
9929 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9930 parameter and which TREE_PURPOSE is the default argument of the
9931 template parameter. */
9932
9933 static tree
9934 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9935 {
9936 tree default_value, parm_decl;
9937
9938 if (args == NULL_TREE
9939 || t == NULL_TREE
9940 || t == error_mark_node)
9941 return t;
9942
9943 gcc_assert (TREE_CODE (t) == TREE_LIST);
9944
9945 default_value = TREE_PURPOSE (t);
9946 parm_decl = TREE_VALUE (t);
9947
9948 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9949 if (TREE_CODE (parm_decl) == PARM_DECL
9950 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9951 parm_decl = error_mark_node;
9952 default_value = tsubst_template_arg (default_value, args,
9953 complain, NULL_TREE);
9954
9955 return build_tree_list (default_value, parm_decl);
9956 }
9957
9958 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9959 type T. If T is not an aggregate or enumeration type, it is
9960 handled as if by tsubst. IN_DECL is as for tsubst. If
9961 ENTERING_SCOPE is nonzero, T is the context for a template which
9962 we are presently tsubst'ing. Return the substituted value. */
9963
9964 static tree
9965 tsubst_aggr_type (tree t,
9966 tree args,
9967 tsubst_flags_t complain,
9968 tree in_decl,
9969 int entering_scope)
9970 {
9971 if (t == NULL_TREE)
9972 return NULL_TREE;
9973
9974 switch (TREE_CODE (t))
9975 {
9976 case RECORD_TYPE:
9977 if (TYPE_PTRMEMFUNC_P (t))
9978 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9979
9980 /* Else fall through. */
9981 case ENUMERAL_TYPE:
9982 case UNION_TYPE:
9983 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9984 {
9985 tree argvec;
9986 tree context;
9987 tree r;
9988 int saved_unevaluated_operand;
9989 int saved_inhibit_evaluation_warnings;
9990
9991 /* In "sizeof(X<I>)" we need to evaluate "I". */
9992 saved_unevaluated_operand = cp_unevaluated_operand;
9993 cp_unevaluated_operand = 0;
9994 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9995 c_inhibit_evaluation_warnings = 0;
9996
9997 /* First, determine the context for the type we are looking
9998 up. */
9999 context = TYPE_CONTEXT (t);
10000 if (context && TYPE_P (context))
10001 {
10002 context = tsubst_aggr_type (context, args, complain,
10003 in_decl, /*entering_scope=*/1);
10004 /* If context is a nested class inside a class template,
10005 it may still need to be instantiated (c++/33959). */
10006 context = complete_type (context);
10007 }
10008
10009 /* Then, figure out what arguments are appropriate for the
10010 type we are trying to find. For example, given:
10011
10012 template <class T> struct S;
10013 template <class T, class U> void f(T, U) { S<U> su; }
10014
10015 and supposing that we are instantiating f<int, double>,
10016 then our ARGS will be {int, double}, but, when looking up
10017 S we only want {double}. */
10018 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10019 complain, in_decl);
10020 if (argvec == error_mark_node)
10021 r = error_mark_node;
10022 else
10023 {
10024 r = lookup_template_class (t, argvec, in_decl, context,
10025 entering_scope, complain);
10026 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10027 }
10028
10029 cp_unevaluated_operand = saved_unevaluated_operand;
10030 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10031
10032 return r;
10033 }
10034 else
10035 /* This is not a template type, so there's nothing to do. */
10036 return t;
10037
10038 default:
10039 return tsubst (t, args, complain, in_decl);
10040 }
10041 }
10042
10043 /* Substitute into the default argument ARG (a default argument for
10044 FN), which has the indicated TYPE. */
10045
10046 tree
10047 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10048 {
10049 tree saved_class_ptr = NULL_TREE;
10050 tree saved_class_ref = NULL_TREE;
10051 int errs = errorcount + sorrycount;
10052
10053 /* This can happen in invalid code. */
10054 if (TREE_CODE (arg) == DEFAULT_ARG)
10055 return arg;
10056
10057 /* This default argument came from a template. Instantiate the
10058 default argument here, not in tsubst. In the case of
10059 something like:
10060
10061 template <class T>
10062 struct S {
10063 static T t();
10064 void f(T = t());
10065 };
10066
10067 we must be careful to do name lookup in the scope of S<T>,
10068 rather than in the current class. */
10069 push_access_scope (fn);
10070 /* The "this" pointer is not valid in a default argument. */
10071 if (cfun)
10072 {
10073 saved_class_ptr = current_class_ptr;
10074 cp_function_chain->x_current_class_ptr = NULL_TREE;
10075 saved_class_ref = current_class_ref;
10076 cp_function_chain->x_current_class_ref = NULL_TREE;
10077 }
10078
10079 push_deferring_access_checks(dk_no_deferred);
10080 /* The default argument expression may cause implicitly defined
10081 member functions to be synthesized, which will result in garbage
10082 collection. We must treat this situation as if we were within
10083 the body of function so as to avoid collecting live data on the
10084 stack. */
10085 ++function_depth;
10086 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10087 complain, NULL_TREE,
10088 /*integral_constant_expression_p=*/false);
10089 --function_depth;
10090 pop_deferring_access_checks();
10091
10092 /* Restore the "this" pointer. */
10093 if (cfun)
10094 {
10095 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10096 cp_function_chain->x_current_class_ref = saved_class_ref;
10097 }
10098
10099 if (errorcount+sorrycount > errs
10100 && (complain & tf_warning_or_error))
10101 inform (input_location,
10102 " when instantiating default argument for call to %D", fn);
10103
10104 /* Make sure the default argument is reasonable. */
10105 arg = check_default_argument (type, arg, complain);
10106
10107 pop_access_scope (fn);
10108
10109 return arg;
10110 }
10111
10112 /* Substitute into all the default arguments for FN. */
10113
10114 static void
10115 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10116 {
10117 tree arg;
10118 tree tmpl_args;
10119
10120 tmpl_args = DECL_TI_ARGS (fn);
10121
10122 /* If this function is not yet instantiated, we certainly don't need
10123 its default arguments. */
10124 if (uses_template_parms (tmpl_args))
10125 return;
10126 /* Don't do this again for clones. */
10127 if (DECL_CLONED_FUNCTION_P (fn))
10128 return;
10129
10130 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10131 arg;
10132 arg = TREE_CHAIN (arg))
10133 if (TREE_PURPOSE (arg))
10134 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10135 TREE_VALUE (arg),
10136 TREE_PURPOSE (arg),
10137 complain);
10138 }
10139
10140 /* Substitute the ARGS into the T, which is a _DECL. Return the
10141 result of the substitution. Issue error and warning messages under
10142 control of COMPLAIN. */
10143
10144 static tree
10145 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10146 {
10147 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10148 location_t saved_loc;
10149 tree r = NULL_TREE;
10150 tree in_decl = t;
10151 hashval_t hash = 0;
10152
10153 /* Set the filename and linenumber to improve error-reporting. */
10154 saved_loc = input_location;
10155 input_location = DECL_SOURCE_LOCATION (t);
10156
10157 switch (TREE_CODE (t))
10158 {
10159 case TEMPLATE_DECL:
10160 {
10161 /* We can get here when processing a member function template,
10162 member class template, or template template parameter. */
10163 tree decl = DECL_TEMPLATE_RESULT (t);
10164 tree spec;
10165 tree tmpl_args;
10166 tree full_args;
10167
10168 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10169 {
10170 /* Template template parameter is treated here. */
10171 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10172 if (new_type == error_mark_node)
10173 RETURN (error_mark_node);
10174 /* If we get a real template back, return it. This can happen in
10175 the context of most_specialized_class. */
10176 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10177 return new_type;
10178
10179 r = copy_decl (t);
10180 DECL_CHAIN (r) = NULL_TREE;
10181 TREE_TYPE (r) = new_type;
10182 DECL_TEMPLATE_RESULT (r)
10183 = build_decl (DECL_SOURCE_LOCATION (decl),
10184 TYPE_DECL, DECL_NAME (decl), new_type);
10185 DECL_TEMPLATE_PARMS (r)
10186 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10187 complain);
10188 TYPE_NAME (new_type) = r;
10189 break;
10190 }
10191
10192 /* We might already have an instance of this template.
10193 The ARGS are for the surrounding class type, so the
10194 full args contain the tsubst'd args for the context,
10195 plus the innermost args from the template decl. */
10196 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10197 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10198 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10199 /* Because this is a template, the arguments will still be
10200 dependent, even after substitution. If
10201 PROCESSING_TEMPLATE_DECL is not set, the dependency
10202 predicates will short-circuit. */
10203 ++processing_template_decl;
10204 full_args = tsubst_template_args (tmpl_args, args,
10205 complain, in_decl);
10206 --processing_template_decl;
10207 if (full_args == error_mark_node)
10208 RETURN (error_mark_node);
10209
10210 /* If this is a default template template argument,
10211 tsubst might not have changed anything. */
10212 if (full_args == tmpl_args)
10213 RETURN (t);
10214
10215 hash = hash_tmpl_and_args (t, full_args);
10216 spec = retrieve_specialization (t, full_args, hash);
10217 if (spec != NULL_TREE)
10218 {
10219 r = spec;
10220 break;
10221 }
10222
10223 /* Make a new template decl. It will be similar to the
10224 original, but will record the current template arguments.
10225 We also create a new function declaration, which is just
10226 like the old one, but points to this new template, rather
10227 than the old one. */
10228 r = copy_decl (t);
10229 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10230 DECL_CHAIN (r) = NULL_TREE;
10231
10232 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10233
10234 if (TREE_CODE (decl) == TYPE_DECL
10235 && !TYPE_DECL_ALIAS_P (decl))
10236 {
10237 tree new_type;
10238 ++processing_template_decl;
10239 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10240 --processing_template_decl;
10241 if (new_type == error_mark_node)
10242 RETURN (error_mark_node);
10243
10244 TREE_TYPE (r) = new_type;
10245 /* For a partial specialization, we need to keep pointing to
10246 the primary template. */
10247 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10248 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10249 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10250 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10251 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10252 }
10253 else
10254 {
10255 tree new_decl;
10256 ++processing_template_decl;
10257 new_decl = tsubst (decl, args, complain, in_decl);
10258 --processing_template_decl;
10259 if (new_decl == error_mark_node)
10260 RETURN (error_mark_node);
10261
10262 DECL_TEMPLATE_RESULT (r) = new_decl;
10263 DECL_TI_TEMPLATE (new_decl) = r;
10264 TREE_TYPE (r) = TREE_TYPE (new_decl);
10265 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10266 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10267 }
10268
10269 SET_DECL_IMPLICIT_INSTANTIATION (r);
10270 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10271 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10272
10273 /* The template parameters for this new template are all the
10274 template parameters for the old template, except the
10275 outermost level of parameters. */
10276 DECL_TEMPLATE_PARMS (r)
10277 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10278 complain);
10279
10280 if (PRIMARY_TEMPLATE_P (t))
10281 DECL_PRIMARY_TEMPLATE (r) = r;
10282
10283 if (TREE_CODE (decl) != TYPE_DECL)
10284 /* Record this non-type partial instantiation. */
10285 register_specialization (r, t,
10286 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10287 false, hash);
10288 }
10289 break;
10290
10291 case FUNCTION_DECL:
10292 {
10293 tree ctx;
10294 tree argvec = NULL_TREE;
10295 tree *friends;
10296 tree gen_tmpl;
10297 tree type;
10298 int member;
10299 int args_depth;
10300 int parms_depth;
10301
10302 /* Nobody should be tsubst'ing into non-template functions. */
10303 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10304
10305 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10306 {
10307 tree spec;
10308 bool dependent_p;
10309
10310 /* If T is not dependent, just return it. We have to
10311 increment PROCESSING_TEMPLATE_DECL because
10312 value_dependent_expression_p assumes that nothing is
10313 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10314 ++processing_template_decl;
10315 dependent_p = value_dependent_expression_p (t);
10316 --processing_template_decl;
10317 if (!dependent_p)
10318 RETURN (t);
10319
10320 /* Calculate the most general template of which R is a
10321 specialization, and the complete set of arguments used to
10322 specialize R. */
10323 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10324 argvec = tsubst_template_args (DECL_TI_ARGS
10325 (DECL_TEMPLATE_RESULT
10326 (DECL_TI_TEMPLATE (t))),
10327 args, complain, in_decl);
10328 if (argvec == error_mark_node)
10329 RETURN (error_mark_node);
10330
10331 /* Check to see if we already have this specialization. */
10332 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10333 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10334
10335 if (spec)
10336 {
10337 r = spec;
10338 break;
10339 }
10340
10341 /* We can see more levels of arguments than parameters if
10342 there was a specialization of a member template, like
10343 this:
10344
10345 template <class T> struct S { template <class U> void f(); }
10346 template <> template <class U> void S<int>::f(U);
10347
10348 Here, we'll be substituting into the specialization,
10349 because that's where we can find the code we actually
10350 want to generate, but we'll have enough arguments for
10351 the most general template.
10352
10353 We also deal with the peculiar case:
10354
10355 template <class T> struct S {
10356 template <class U> friend void f();
10357 };
10358 template <class U> void f() {}
10359 template S<int>;
10360 template void f<double>();
10361
10362 Here, the ARGS for the instantiation of will be {int,
10363 double}. But, we only need as many ARGS as there are
10364 levels of template parameters in CODE_PATTERN. We are
10365 careful not to get fooled into reducing the ARGS in
10366 situations like:
10367
10368 template <class T> struct S { template <class U> void f(U); }
10369 template <class T> template <> void S<T>::f(int) {}
10370
10371 which we can spot because the pattern will be a
10372 specialization in this case. */
10373 args_depth = TMPL_ARGS_DEPTH (args);
10374 parms_depth =
10375 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10376 if (args_depth > parms_depth
10377 && !DECL_TEMPLATE_SPECIALIZATION (t))
10378 args = get_innermost_template_args (args, parms_depth);
10379 }
10380 else
10381 {
10382 /* This special case arises when we have something like this:
10383
10384 template <class T> struct S {
10385 friend void f<int>(int, double);
10386 };
10387
10388 Here, the DECL_TI_TEMPLATE for the friend declaration
10389 will be an IDENTIFIER_NODE. We are being called from
10390 tsubst_friend_function, and we want only to create a
10391 new decl (R) with appropriate types so that we can call
10392 determine_specialization. */
10393 gen_tmpl = NULL_TREE;
10394 }
10395
10396 if (DECL_CLASS_SCOPE_P (t))
10397 {
10398 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10399 member = 2;
10400 else
10401 member = 1;
10402 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10403 complain, t, /*entering_scope=*/1);
10404 }
10405 else
10406 {
10407 member = 0;
10408 ctx = DECL_CONTEXT (t);
10409 }
10410 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10411 if (type == error_mark_node)
10412 RETURN (error_mark_node);
10413
10414 /* If we hit excessive deduction depth, the type is bogus even if
10415 it isn't error_mark_node, so don't build a decl. */
10416 if (excessive_deduction_depth)
10417 RETURN (error_mark_node);
10418
10419 /* We do NOT check for matching decls pushed separately at this
10420 point, as they may not represent instantiations of this
10421 template, and in any case are considered separate under the
10422 discrete model. */
10423 r = copy_decl (t);
10424 DECL_USE_TEMPLATE (r) = 0;
10425 TREE_TYPE (r) = type;
10426 /* Clear out the mangled name and RTL for the instantiation. */
10427 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10428 SET_DECL_RTL (r, NULL);
10429 /* Leave DECL_INITIAL set on deleted instantiations. */
10430 if (!DECL_DELETED_FN (r))
10431 DECL_INITIAL (r) = NULL_TREE;
10432 DECL_CONTEXT (r) = ctx;
10433
10434 /* OpenMP UDRs have the only argument a reference to the declared
10435 type. We want to diagnose if the declared type is a reference,
10436 which is invalid, but as references to references are usually
10437 quietly merged, diagnose it here. */
10438 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10439 {
10440 tree argtype
10441 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10442 argtype = tsubst (argtype, args, complain, in_decl);
10443 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10444 error_at (DECL_SOURCE_LOCATION (t),
10445 "reference type %qT in "
10446 "%<#pragma omp declare reduction%>", argtype);
10447 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10448 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10449 argtype);
10450 }
10451
10452 if (member && DECL_CONV_FN_P (r))
10453 /* Type-conversion operator. Reconstruct the name, in
10454 case it's the name of one of the template's parameters. */
10455 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10456
10457 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10458 complain, t);
10459 DECL_RESULT (r) = NULL_TREE;
10460
10461 TREE_STATIC (r) = 0;
10462 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10463 DECL_EXTERNAL (r) = 1;
10464 /* If this is an instantiation of a function with internal
10465 linkage, we already know what object file linkage will be
10466 assigned to the instantiation. */
10467 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10468 DECL_DEFER_OUTPUT (r) = 0;
10469 DECL_CHAIN (r) = NULL_TREE;
10470 DECL_PENDING_INLINE_INFO (r) = 0;
10471 DECL_PENDING_INLINE_P (r) = 0;
10472 DECL_SAVED_TREE (r) = NULL_TREE;
10473 DECL_STRUCT_FUNCTION (r) = NULL;
10474 TREE_USED (r) = 0;
10475 /* We'll re-clone as appropriate in instantiate_template. */
10476 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10477
10478 /* If we aren't complaining now, return on error before we register
10479 the specialization so that we'll complain eventually. */
10480 if ((complain & tf_error) == 0
10481 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10482 && !grok_op_properties (r, /*complain=*/false))
10483 RETURN (error_mark_node);
10484
10485 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10486 this in the special friend case mentioned above where
10487 GEN_TMPL is NULL. */
10488 if (gen_tmpl)
10489 {
10490 DECL_TEMPLATE_INFO (r)
10491 = build_template_info (gen_tmpl, argvec);
10492 SET_DECL_IMPLICIT_INSTANTIATION (r);
10493
10494 tree new_r
10495 = register_specialization (r, gen_tmpl, argvec, false, hash);
10496 if (new_r != r)
10497 /* We instantiated this while substituting into
10498 the type earlier (template/friend54.C). */
10499 RETURN (new_r);
10500
10501 /* We're not supposed to instantiate default arguments
10502 until they are called, for a template. But, for a
10503 declaration like:
10504
10505 template <class T> void f ()
10506 { extern void g(int i = T()); }
10507
10508 we should do the substitution when the template is
10509 instantiated. We handle the member function case in
10510 instantiate_class_template since the default arguments
10511 might refer to other members of the class. */
10512 if (!member
10513 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10514 && !uses_template_parms (argvec))
10515 tsubst_default_arguments (r, complain);
10516 }
10517 else
10518 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10519
10520 /* Copy the list of befriending classes. */
10521 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10522 *friends;
10523 friends = &TREE_CHAIN (*friends))
10524 {
10525 *friends = copy_node (*friends);
10526 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10527 args, complain,
10528 in_decl);
10529 }
10530
10531 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10532 {
10533 maybe_retrofit_in_chrg (r);
10534 if (DECL_CONSTRUCTOR_P (r))
10535 grok_ctor_properties (ctx, r);
10536 if (DECL_INHERITED_CTOR_BASE (r))
10537 deduce_inheriting_ctor (r);
10538 /* If this is an instantiation of a member template, clone it.
10539 If it isn't, that'll be handled by
10540 clone_constructors_and_destructors. */
10541 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10542 clone_function_decl (r, /*update_method_vec_p=*/0);
10543 }
10544 else if ((complain & tf_error) != 0
10545 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10546 && !grok_op_properties (r, /*complain=*/true))
10547 RETURN (error_mark_node);
10548
10549 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10550 SET_DECL_FRIEND_CONTEXT (r,
10551 tsubst (DECL_FRIEND_CONTEXT (t),
10552 args, complain, in_decl));
10553
10554 /* Possibly limit visibility based on template args. */
10555 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10556 if (DECL_VISIBILITY_SPECIFIED (t))
10557 {
10558 DECL_VISIBILITY_SPECIFIED (r) = 0;
10559 DECL_ATTRIBUTES (r)
10560 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10561 }
10562 determine_visibility (r);
10563 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10564 && !processing_template_decl)
10565 defaulted_late_check (r);
10566
10567 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10568 args, complain, in_decl);
10569 }
10570 break;
10571
10572 case PARM_DECL:
10573 {
10574 tree type = NULL_TREE;
10575 int i, len = 1;
10576 tree expanded_types = NULL_TREE;
10577 tree prev_r = NULL_TREE;
10578 tree first_r = NULL_TREE;
10579
10580 if (DECL_PACK_P (t))
10581 {
10582 /* If there is a local specialization that isn't a
10583 parameter pack, it means that we're doing a "simple"
10584 substitution from inside tsubst_pack_expansion. Just
10585 return the local specialization (which will be a single
10586 parm). */
10587 tree spec = retrieve_local_specialization (t);
10588 if (spec
10589 && TREE_CODE (spec) == PARM_DECL
10590 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10591 RETURN (spec);
10592
10593 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10594 the parameters in this function parameter pack. */
10595 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10596 complain, in_decl);
10597 if (TREE_CODE (expanded_types) == TREE_VEC)
10598 {
10599 len = TREE_VEC_LENGTH (expanded_types);
10600
10601 /* Zero-length parameter packs are boring. Just substitute
10602 into the chain. */
10603 if (len == 0)
10604 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10605 TREE_CHAIN (t)));
10606 }
10607 else
10608 {
10609 /* All we did was update the type. Make a note of that. */
10610 type = expanded_types;
10611 expanded_types = NULL_TREE;
10612 }
10613 }
10614
10615 /* Loop through all of the parameters we'll build. When T is
10616 a function parameter pack, LEN is the number of expanded
10617 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10618 r = NULL_TREE;
10619 for (i = 0; i < len; ++i)
10620 {
10621 prev_r = r;
10622 r = copy_node (t);
10623 if (DECL_TEMPLATE_PARM_P (t))
10624 SET_DECL_TEMPLATE_PARM_P (r);
10625
10626 if (expanded_types)
10627 /* We're on the Ith parameter of the function parameter
10628 pack. */
10629 {
10630 /* Get the Ith type. */
10631 type = TREE_VEC_ELT (expanded_types, i);
10632
10633 /* Rename the parameter to include the index. */
10634 DECL_NAME (r)
10635 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10636 }
10637 else if (!type)
10638 /* We're dealing with a normal parameter. */
10639 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10640
10641 type = type_decays_to (type);
10642 TREE_TYPE (r) = type;
10643 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10644
10645 if (DECL_INITIAL (r))
10646 {
10647 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10648 DECL_INITIAL (r) = TREE_TYPE (r);
10649 else
10650 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10651 complain, in_decl);
10652 }
10653
10654 DECL_CONTEXT (r) = NULL_TREE;
10655
10656 if (!DECL_TEMPLATE_PARM_P (r))
10657 DECL_ARG_TYPE (r) = type_passed_as (type);
10658
10659 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10660 args, complain, in_decl);
10661
10662 /* Keep track of the first new parameter we
10663 generate. That's what will be returned to the
10664 caller. */
10665 if (!first_r)
10666 first_r = r;
10667
10668 /* Build a proper chain of parameters when substituting
10669 into a function parameter pack. */
10670 if (prev_r)
10671 DECL_CHAIN (prev_r) = r;
10672 }
10673
10674 /* If cp_unevaluated_operand is set, we're just looking for a
10675 single dummy parameter, so don't keep going. */
10676 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10677 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10678 complain, DECL_CHAIN (t));
10679
10680 /* FIRST_R contains the start of the chain we've built. */
10681 r = first_r;
10682 }
10683 break;
10684
10685 case FIELD_DECL:
10686 {
10687 tree type = NULL_TREE;
10688 tree vec = NULL_TREE;
10689 tree expanded_types = NULL_TREE;
10690 int len = 1;
10691
10692 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10693 {
10694 /* This field is a lambda capture pack. Return a TREE_VEC of
10695 the expanded fields to instantiate_class_template_1 and
10696 store them in the specializations hash table as a
10697 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10698 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10699 complain, in_decl);
10700 if (TREE_CODE (expanded_types) == TREE_VEC)
10701 {
10702 len = TREE_VEC_LENGTH (expanded_types);
10703 vec = make_tree_vec (len);
10704 }
10705 else
10706 {
10707 /* All we did was update the type. Make a note of that. */
10708 type = expanded_types;
10709 expanded_types = NULL_TREE;
10710 }
10711 }
10712
10713 for (int i = 0; i < len; ++i)
10714 {
10715 r = copy_decl (t);
10716 if (expanded_types)
10717 {
10718 type = TREE_VEC_ELT (expanded_types, i);
10719 DECL_NAME (r)
10720 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10721 }
10722 else if (!type)
10723 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10724
10725 if (type == error_mark_node)
10726 RETURN (error_mark_node);
10727 TREE_TYPE (r) = type;
10728 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10729
10730 if (DECL_C_BIT_FIELD (r))
10731 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10732 non-bit-fields DECL_INITIAL is a non-static data member
10733 initializer, which gets deferred instantiation. */
10734 DECL_INITIAL (r)
10735 = tsubst_expr (DECL_INITIAL (t), args,
10736 complain, in_decl,
10737 /*integral_constant_expression_p=*/true);
10738 else if (DECL_INITIAL (t))
10739 {
10740 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10741 NSDMI in perform_member_init. Still set DECL_INITIAL
10742 so that we know there is one. */
10743 DECL_INITIAL (r) = void_zero_node;
10744 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10745 retrofit_lang_decl (r);
10746 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10747 }
10748 /* We don't have to set DECL_CONTEXT here; it is set by
10749 finish_member_declaration. */
10750 DECL_CHAIN (r) = NULL_TREE;
10751
10752 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10753 args, complain, in_decl);
10754
10755 if (vec)
10756 TREE_VEC_ELT (vec, i) = r;
10757 }
10758
10759 if (vec)
10760 {
10761 r = vec;
10762 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10763 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10764 SET_ARGUMENT_PACK_ARGS (pack, vec);
10765 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10766 TREE_TYPE (pack) = tpack;
10767 register_specialization (pack, t, args, false, 0);
10768 }
10769 }
10770 break;
10771
10772 case USING_DECL:
10773 /* We reach here only for member using decls. We also need to check
10774 uses_template_parms because DECL_DEPENDENT_P is not set for a
10775 using-declaration that designates a member of the current
10776 instantiation (c++/53549). */
10777 if (DECL_DEPENDENT_P (t)
10778 || uses_template_parms (USING_DECL_SCOPE (t)))
10779 {
10780 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10781 complain, in_decl);
10782 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10783 r = do_class_using_decl (inst_scope, name);
10784 if (!r)
10785 r = error_mark_node;
10786 else
10787 {
10788 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10789 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10790 }
10791 }
10792 else
10793 {
10794 r = copy_node (t);
10795 DECL_CHAIN (r) = NULL_TREE;
10796 }
10797 break;
10798
10799 case TYPE_DECL:
10800 case VAR_DECL:
10801 {
10802 tree argvec = NULL_TREE;
10803 tree gen_tmpl = NULL_TREE;
10804 tree spec;
10805 tree tmpl = NULL_TREE;
10806 tree ctx;
10807 tree type = NULL_TREE;
10808 bool local_p;
10809
10810 if (TREE_CODE (t) == TYPE_DECL
10811 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10812 {
10813 /* If this is the canonical decl, we don't have to
10814 mess with instantiations, and often we can't (for
10815 typename, template type parms and such). Note that
10816 TYPE_NAME is not correct for the above test if
10817 we've copied the type for a typedef. */
10818 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10819 if (type == error_mark_node)
10820 RETURN (error_mark_node);
10821 r = TYPE_NAME (type);
10822 break;
10823 }
10824
10825 /* Check to see if we already have the specialization we
10826 need. */
10827 spec = NULL_TREE;
10828 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10829 {
10830 /* T is a static data member or namespace-scope entity.
10831 We have to substitute into namespace-scope variables
10832 (even though such entities are never templates) because
10833 of cases like:
10834
10835 template <class T> void f() { extern T t; }
10836
10837 where the entity referenced is not known until
10838 instantiation time. */
10839 local_p = false;
10840 ctx = DECL_CONTEXT (t);
10841 if (DECL_CLASS_SCOPE_P (t))
10842 {
10843 ctx = tsubst_aggr_type (ctx, args,
10844 complain,
10845 in_decl, /*entering_scope=*/1);
10846 /* If CTX is unchanged, then T is in fact the
10847 specialization we want. That situation occurs when
10848 referencing a static data member within in its own
10849 class. We can use pointer equality, rather than
10850 same_type_p, because DECL_CONTEXT is always
10851 canonical... */
10852 if (ctx == DECL_CONTEXT (t)
10853 && (TREE_CODE (t) != TYPE_DECL
10854 /* ... unless T is a member template; in which
10855 case our caller can be willing to create a
10856 specialization of that template represented
10857 by T. */
10858 || !(DECL_TI_TEMPLATE (t)
10859 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10860 spec = t;
10861 }
10862
10863 if (!spec)
10864 {
10865 tmpl = DECL_TI_TEMPLATE (t);
10866 gen_tmpl = most_general_template (tmpl);
10867 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10868 if (argvec == error_mark_node)
10869 RETURN (error_mark_node);
10870 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10871 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10872 }
10873 }
10874 else
10875 {
10876 /* A local variable. */
10877 local_p = true;
10878 /* Subsequent calls to pushdecl will fill this in. */
10879 ctx = NULL_TREE;
10880 spec = retrieve_local_specialization (t);
10881 }
10882 /* If we already have the specialization we need, there is
10883 nothing more to do. */
10884 if (spec)
10885 {
10886 r = spec;
10887 break;
10888 }
10889
10890 /* Create a new node for the specialization we need. */
10891 r = copy_decl (t);
10892 if (type == NULL_TREE)
10893 {
10894 if (is_typedef_decl (t))
10895 type = DECL_ORIGINAL_TYPE (t);
10896 else
10897 type = TREE_TYPE (t);
10898 if (VAR_P (t)
10899 && VAR_HAD_UNKNOWN_BOUND (t)
10900 && type != error_mark_node)
10901 type = strip_array_domain (type);
10902 type = tsubst (type, args, complain, in_decl);
10903 }
10904 if (VAR_P (r))
10905 {
10906 /* Even if the original location is out of scope, the
10907 newly substituted one is not. */
10908 DECL_DEAD_FOR_LOCAL (r) = 0;
10909 DECL_INITIALIZED_P (r) = 0;
10910 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10911 if (type == error_mark_node)
10912 RETURN (error_mark_node);
10913 if (TREE_CODE (type) == FUNCTION_TYPE)
10914 {
10915 /* It may seem that this case cannot occur, since:
10916
10917 typedef void f();
10918 void g() { f x; }
10919
10920 declares a function, not a variable. However:
10921
10922 typedef void f();
10923 template <typename T> void g() { T t; }
10924 template void g<f>();
10925
10926 is an attempt to declare a variable with function
10927 type. */
10928 error ("variable %qD has function type",
10929 /* R is not yet sufficiently initialized, so we
10930 just use its name. */
10931 DECL_NAME (r));
10932 RETURN (error_mark_node);
10933 }
10934 type = complete_type (type);
10935 /* Wait until cp_finish_decl to set this again, to handle
10936 circular dependency (template/instantiate6.C). */
10937 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10938 type = check_var_type (DECL_NAME (r), type);
10939
10940 if (DECL_HAS_VALUE_EXPR_P (t))
10941 {
10942 tree ve = DECL_VALUE_EXPR (t);
10943 ve = tsubst_expr (ve, args, complain, in_decl,
10944 /*constant_expression_p=*/false);
10945 if (REFERENCE_REF_P (ve))
10946 {
10947 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10948 ve = TREE_OPERAND (ve, 0);
10949 }
10950 SET_DECL_VALUE_EXPR (r, ve);
10951 }
10952 }
10953 else if (DECL_SELF_REFERENCE_P (t))
10954 SET_DECL_SELF_REFERENCE_P (r);
10955 TREE_TYPE (r) = type;
10956 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10957 DECL_CONTEXT (r) = ctx;
10958 /* Clear out the mangled name and RTL for the instantiation. */
10959 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10960 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10961 SET_DECL_RTL (r, NULL);
10962 /* The initializer must not be expanded until it is required;
10963 see [temp.inst]. */
10964 DECL_INITIAL (r) = NULL_TREE;
10965 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10966 SET_DECL_RTL (r, NULL);
10967 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10968 if (VAR_P (r))
10969 {
10970 /* Possibly limit visibility based on template args. */
10971 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10972 if (DECL_VISIBILITY_SPECIFIED (t))
10973 {
10974 DECL_VISIBILITY_SPECIFIED (r) = 0;
10975 DECL_ATTRIBUTES (r)
10976 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10977 }
10978 determine_visibility (r);
10979 }
10980
10981 if (!local_p)
10982 {
10983 /* A static data member declaration is always marked
10984 external when it is declared in-class, even if an
10985 initializer is present. We mimic the non-template
10986 processing here. */
10987 DECL_EXTERNAL (r) = 1;
10988
10989 register_specialization (r, gen_tmpl, argvec, false, hash);
10990 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10991 SET_DECL_IMPLICIT_INSTANTIATION (r);
10992 }
10993 else if (!cp_unevaluated_operand)
10994 register_local_specialization (r, t);
10995
10996 DECL_CHAIN (r) = NULL_TREE;
10997
10998 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10999 /*flags=*/0,
11000 args, complain, in_decl);
11001
11002 /* Preserve a typedef that names a type. */
11003 if (is_typedef_decl (r))
11004 {
11005 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
11006 set_underlying_type (r);
11007 }
11008
11009 layout_decl (r, 0);
11010 }
11011 break;
11012
11013 default:
11014 gcc_unreachable ();
11015 }
11016 #undef RETURN
11017
11018 out:
11019 /* Restore the file and line information. */
11020 input_location = saved_loc;
11021
11022 return r;
11023 }
11024
11025 /* Substitute into the ARG_TYPES of a function type.
11026 If END is a TREE_CHAIN, leave it and any following types
11027 un-substituted. */
11028
11029 static tree
11030 tsubst_arg_types (tree arg_types,
11031 tree args,
11032 tree end,
11033 tsubst_flags_t complain,
11034 tree in_decl)
11035 {
11036 tree remaining_arg_types;
11037 tree type = NULL_TREE;
11038 int i = 1;
11039 tree expanded_args = NULL_TREE;
11040 tree default_arg;
11041
11042 if (!arg_types || arg_types == void_list_node || arg_types == end)
11043 return arg_types;
11044
11045 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11046 args, end, complain, in_decl);
11047 if (remaining_arg_types == error_mark_node)
11048 return error_mark_node;
11049
11050 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11051 {
11052 /* For a pack expansion, perform substitution on the
11053 entire expression. Later on, we'll handle the arguments
11054 one-by-one. */
11055 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11056 args, complain, in_decl);
11057
11058 if (TREE_CODE (expanded_args) == TREE_VEC)
11059 /* So that we'll spin through the parameters, one by one. */
11060 i = TREE_VEC_LENGTH (expanded_args);
11061 else
11062 {
11063 /* We only partially substituted into the parameter
11064 pack. Our type is TYPE_PACK_EXPANSION. */
11065 type = expanded_args;
11066 expanded_args = NULL_TREE;
11067 }
11068 }
11069
11070 while (i > 0) {
11071 --i;
11072
11073 if (expanded_args)
11074 type = TREE_VEC_ELT (expanded_args, i);
11075 else if (!type)
11076 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11077
11078 if (type == error_mark_node)
11079 return error_mark_node;
11080 if (VOID_TYPE_P (type))
11081 {
11082 if (complain & tf_error)
11083 {
11084 error ("invalid parameter type %qT", type);
11085 if (in_decl)
11086 error ("in declaration %q+D", in_decl);
11087 }
11088 return error_mark_node;
11089 }
11090 /* DR 657. */
11091 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11092 return error_mark_node;
11093
11094 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11095 top-level qualifiers as required. */
11096 type = cv_unqualified (type_decays_to (type));
11097
11098 /* We do not substitute into default arguments here. The standard
11099 mandates that they be instantiated only when needed, which is
11100 done in build_over_call. */
11101 default_arg = TREE_PURPOSE (arg_types);
11102
11103 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11104 {
11105 /* We've instantiated a template before its default arguments
11106 have been parsed. This can happen for a nested template
11107 class, and is not an error unless we require the default
11108 argument in a call of this function. */
11109 remaining_arg_types =
11110 tree_cons (default_arg, type, remaining_arg_types);
11111 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11112 }
11113 else
11114 remaining_arg_types =
11115 hash_tree_cons (default_arg, type, remaining_arg_types);
11116 }
11117
11118 return remaining_arg_types;
11119 }
11120
11121 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11122 *not* handle the exception-specification for FNTYPE, because the
11123 initial substitution of explicitly provided template parameters
11124 during argument deduction forbids substitution into the
11125 exception-specification:
11126
11127 [temp.deduct]
11128
11129 All references in the function type of the function template to the
11130 corresponding template parameters are replaced by the specified tem-
11131 plate argument values. If a substitution in a template parameter or
11132 in the function type of the function template results in an invalid
11133 type, type deduction fails. [Note: The equivalent substitution in
11134 exception specifications is done only when the function is instanti-
11135 ated, at which point a program is ill-formed if the substitution
11136 results in an invalid type.] */
11137
11138 static tree
11139 tsubst_function_type (tree t,
11140 tree args,
11141 tsubst_flags_t complain,
11142 tree in_decl)
11143 {
11144 tree return_type;
11145 tree arg_types;
11146 tree fntype;
11147
11148 /* The TYPE_CONTEXT is not used for function/method types. */
11149 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11150
11151 /* Substitute the return type. */
11152 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11153 if (return_type == error_mark_node)
11154 return error_mark_node;
11155 /* DR 486 clarifies that creation of a function type with an
11156 invalid return type is a deduction failure. */
11157 if (TREE_CODE (return_type) == ARRAY_TYPE
11158 || TREE_CODE (return_type) == FUNCTION_TYPE)
11159 {
11160 if (complain & tf_error)
11161 {
11162 if (TREE_CODE (return_type) == ARRAY_TYPE)
11163 error ("function returning an array");
11164 else
11165 error ("function returning a function");
11166 }
11167 return error_mark_node;
11168 }
11169 /* And DR 657. */
11170 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11171 return error_mark_node;
11172
11173 /* Substitute the argument types. */
11174 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11175 complain, in_decl);
11176 if (arg_types == error_mark_node)
11177 return error_mark_node;
11178
11179 /* Construct a new type node and return it. */
11180 if (TREE_CODE (t) == FUNCTION_TYPE)
11181 {
11182 fntype = build_function_type (return_type, arg_types);
11183 fntype = apply_memfn_quals (fntype,
11184 type_memfn_quals (t),
11185 type_memfn_rqual (t));
11186 }
11187 else
11188 {
11189 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11190 /* Don't pick up extra function qualifiers from the basetype. */
11191 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
11192 if (! MAYBE_CLASS_TYPE_P (r))
11193 {
11194 /* [temp.deduct]
11195
11196 Type deduction may fail for any of the following
11197 reasons:
11198
11199 -- Attempting to create "pointer to member of T" when T
11200 is not a class type. */
11201 if (complain & tf_error)
11202 error ("creating pointer to member function of non-class type %qT",
11203 r);
11204 return error_mark_node;
11205 }
11206
11207 fntype = build_method_type_directly (r, return_type,
11208 TREE_CHAIN (arg_types));
11209 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11210 }
11211 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11212
11213 return fntype;
11214 }
11215
11216 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11217 ARGS into that specification, and return the substituted
11218 specification. If there is no specification, return NULL_TREE. */
11219
11220 static tree
11221 tsubst_exception_specification (tree fntype,
11222 tree args,
11223 tsubst_flags_t complain,
11224 tree in_decl,
11225 bool defer_ok)
11226 {
11227 tree specs;
11228 tree new_specs;
11229
11230 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11231 new_specs = NULL_TREE;
11232 if (specs && TREE_PURPOSE (specs))
11233 {
11234 /* A noexcept-specifier. */
11235 tree expr = TREE_PURPOSE (specs);
11236 if (TREE_CODE (expr) == INTEGER_CST)
11237 new_specs = expr;
11238 else if (defer_ok)
11239 {
11240 /* Defer instantiation of noexcept-specifiers to avoid
11241 excessive instantiations (c++/49107). */
11242 new_specs = make_node (DEFERRED_NOEXCEPT);
11243 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11244 {
11245 /* We already partially instantiated this member template,
11246 so combine the new args with the old. */
11247 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11248 = DEFERRED_NOEXCEPT_PATTERN (expr);
11249 DEFERRED_NOEXCEPT_ARGS (new_specs)
11250 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11251 }
11252 else
11253 {
11254 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11255 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11256 }
11257 }
11258 else
11259 new_specs = tsubst_copy_and_build
11260 (expr, args, complain, in_decl, /*function_p=*/false,
11261 /*integral_constant_expression_p=*/true);
11262 new_specs = build_noexcept_spec (new_specs, complain);
11263 }
11264 else if (specs)
11265 {
11266 if (! TREE_VALUE (specs))
11267 new_specs = specs;
11268 else
11269 while (specs)
11270 {
11271 tree spec;
11272 int i, len = 1;
11273 tree expanded_specs = NULL_TREE;
11274
11275 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11276 {
11277 /* Expand the pack expansion type. */
11278 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11279 args, complain,
11280 in_decl);
11281
11282 if (expanded_specs == error_mark_node)
11283 return error_mark_node;
11284 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11285 len = TREE_VEC_LENGTH (expanded_specs);
11286 else
11287 {
11288 /* We're substituting into a member template, so
11289 we got a TYPE_PACK_EXPANSION back. Add that
11290 expansion and move on. */
11291 gcc_assert (TREE_CODE (expanded_specs)
11292 == TYPE_PACK_EXPANSION);
11293 new_specs = add_exception_specifier (new_specs,
11294 expanded_specs,
11295 complain);
11296 specs = TREE_CHAIN (specs);
11297 continue;
11298 }
11299 }
11300
11301 for (i = 0; i < len; ++i)
11302 {
11303 if (expanded_specs)
11304 spec = TREE_VEC_ELT (expanded_specs, i);
11305 else
11306 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11307 if (spec == error_mark_node)
11308 return spec;
11309 new_specs = add_exception_specifier (new_specs, spec,
11310 complain);
11311 }
11312
11313 specs = TREE_CHAIN (specs);
11314 }
11315 }
11316 return new_specs;
11317 }
11318
11319 /* Take the tree structure T and replace template parameters used
11320 therein with the argument vector ARGS. IN_DECL is an associated
11321 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11322 Issue error and warning messages under control of COMPLAIN. Note
11323 that we must be relatively non-tolerant of extensions here, in
11324 order to preserve conformance; if we allow substitutions that
11325 should not be allowed, we may allow argument deductions that should
11326 not succeed, and therefore report ambiguous overload situations
11327 where there are none. In theory, we could allow the substitution,
11328 but indicate that it should have failed, and allow our caller to
11329 make sure that the right thing happens, but we don't try to do this
11330 yet.
11331
11332 This function is used for dealing with types, decls and the like;
11333 for expressions, use tsubst_expr or tsubst_copy. */
11334
11335 tree
11336 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11337 {
11338 enum tree_code code;
11339 tree type, r = NULL_TREE;
11340
11341 if (t == NULL_TREE || t == error_mark_node
11342 || t == integer_type_node
11343 || t == void_type_node
11344 || t == char_type_node
11345 || t == unknown_type_node
11346 || TREE_CODE (t) == NAMESPACE_DECL
11347 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11348 return t;
11349
11350 if (DECL_P (t))
11351 return tsubst_decl (t, args, complain);
11352
11353 if (args == NULL_TREE)
11354 return t;
11355
11356 code = TREE_CODE (t);
11357
11358 if (code == IDENTIFIER_NODE)
11359 type = IDENTIFIER_TYPE_VALUE (t);
11360 else
11361 type = TREE_TYPE (t);
11362
11363 gcc_assert (type != unknown_type_node);
11364
11365 /* Reuse typedefs. We need to do this to handle dependent attributes,
11366 such as attribute aligned. */
11367 if (TYPE_P (t)
11368 && typedef_variant_p (t))
11369 {
11370 tree decl = TYPE_NAME (t);
11371
11372 if (alias_template_specialization_p (t))
11373 {
11374 /* DECL represents an alias template and we want to
11375 instantiate it. */
11376 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11377 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11378 r = instantiate_alias_template (tmpl, gen_args, complain);
11379 }
11380 else if (DECL_CLASS_SCOPE_P (decl)
11381 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11382 && uses_template_parms (DECL_CONTEXT (decl)))
11383 {
11384 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11385 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11386 r = retrieve_specialization (tmpl, gen_args, 0);
11387 }
11388 else if (DECL_FUNCTION_SCOPE_P (decl)
11389 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11390 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11391 r = retrieve_local_specialization (decl);
11392 else
11393 /* The typedef is from a non-template context. */
11394 return t;
11395
11396 if (r)
11397 {
11398 r = TREE_TYPE (r);
11399 r = cp_build_qualified_type_real
11400 (r, cp_type_quals (t) | cp_type_quals (r),
11401 complain | tf_ignore_bad_quals);
11402 return r;
11403 }
11404 else
11405 {
11406 /* We don't have an instantiation yet, so drop the typedef. */
11407 int quals = cp_type_quals (t);
11408 t = DECL_ORIGINAL_TYPE (decl);
11409 t = cp_build_qualified_type_real (t, quals,
11410 complain | tf_ignore_bad_quals);
11411 }
11412 }
11413
11414 if (type
11415 && code != TYPENAME_TYPE
11416 && code != TEMPLATE_TYPE_PARM
11417 && code != IDENTIFIER_NODE
11418 && code != FUNCTION_TYPE
11419 && code != METHOD_TYPE)
11420 type = tsubst (type, args, complain, in_decl);
11421 if (type == error_mark_node)
11422 return error_mark_node;
11423
11424 switch (code)
11425 {
11426 case RECORD_TYPE:
11427 case UNION_TYPE:
11428 case ENUMERAL_TYPE:
11429 return tsubst_aggr_type (t, args, complain, in_decl,
11430 /*entering_scope=*/0);
11431
11432 case ERROR_MARK:
11433 case IDENTIFIER_NODE:
11434 case VOID_TYPE:
11435 case REAL_TYPE:
11436 case COMPLEX_TYPE:
11437 case VECTOR_TYPE:
11438 case BOOLEAN_TYPE:
11439 case NULLPTR_TYPE:
11440 case LANG_TYPE:
11441 return t;
11442
11443 case INTEGER_TYPE:
11444 if (t == integer_type_node)
11445 return t;
11446
11447 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11448 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11449 return t;
11450
11451 {
11452 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11453
11454 max = tsubst_expr (omax, args, complain, in_decl,
11455 /*integral_constant_expression_p=*/false);
11456
11457 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11458 needed. */
11459 if (TREE_CODE (max) == NOP_EXPR
11460 && TREE_SIDE_EFFECTS (omax)
11461 && !TREE_TYPE (max))
11462 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11463
11464 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11465 with TREE_SIDE_EFFECTS that indicates this is not an integral
11466 constant expression. */
11467 if (processing_template_decl
11468 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11469 {
11470 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11471 TREE_SIDE_EFFECTS (max) = 1;
11472 }
11473
11474 return compute_array_index_type (NULL_TREE, max, complain);
11475 }
11476
11477 case TEMPLATE_TYPE_PARM:
11478 case TEMPLATE_TEMPLATE_PARM:
11479 case BOUND_TEMPLATE_TEMPLATE_PARM:
11480 case TEMPLATE_PARM_INDEX:
11481 {
11482 int idx;
11483 int level;
11484 int levels;
11485 tree arg = NULL_TREE;
11486
11487 r = NULL_TREE;
11488
11489 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11490 template_parm_level_and_index (t, &level, &idx);
11491
11492 levels = TMPL_ARGS_DEPTH (args);
11493 if (level <= levels)
11494 {
11495 arg = TMPL_ARG (args, level, idx);
11496
11497 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11498 {
11499 /* See through ARGUMENT_PACK_SELECT arguments. */
11500 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11501 /* If the selected argument is an expansion E, that most
11502 likely means we were called from
11503 gen_elem_of_pack_expansion_instantiation during the
11504 substituting of pack an argument pack (which Ith
11505 element is a pack expansion, where I is
11506 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11507 In this case, the Ith element resulting from this
11508 substituting is going to be a pack expansion, which
11509 pattern is the pattern of E. Let's return the
11510 pattern of E, and
11511 gen_elem_of_pack_expansion_instantiation will
11512 build the resulting pack expansion from it. */
11513 if (PACK_EXPANSION_P (arg))
11514 arg = PACK_EXPANSION_PATTERN (arg);
11515 }
11516 }
11517
11518 if (arg == error_mark_node)
11519 return error_mark_node;
11520 else if (arg != NULL_TREE)
11521 {
11522 if (ARGUMENT_PACK_P (arg))
11523 /* If ARG is an argument pack, we don't actually want to
11524 perform a substitution here, because substitutions
11525 for argument packs are only done
11526 element-by-element. We can get to this point when
11527 substituting the type of a non-type template
11528 parameter pack, when that type actually contains
11529 template parameter packs from an outer template, e.g.,
11530
11531 template<typename... Types> struct A {
11532 template<Types... Values> struct B { };
11533 }; */
11534 return t;
11535
11536 if (code == TEMPLATE_TYPE_PARM)
11537 {
11538 int quals;
11539 gcc_assert (TYPE_P (arg));
11540
11541 quals = cp_type_quals (arg) | cp_type_quals (t);
11542
11543 return cp_build_qualified_type_real
11544 (arg, quals, complain | tf_ignore_bad_quals);
11545 }
11546 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11547 {
11548 /* We are processing a type constructed from a
11549 template template parameter. */
11550 tree argvec = tsubst (TYPE_TI_ARGS (t),
11551 args, complain, in_decl);
11552 if (argvec == error_mark_node)
11553 return error_mark_node;
11554
11555 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11556 || TREE_CODE (arg) == TEMPLATE_DECL
11557 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11558
11559 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11560 /* Consider this code:
11561
11562 template <template <class> class Template>
11563 struct Internal {
11564 template <class Arg> using Bind = Template<Arg>;
11565 };
11566
11567 template <template <class> class Template, class Arg>
11568 using Instantiate = Template<Arg>; //#0
11569
11570 template <template <class> class Template,
11571 class Argument>
11572 using Bind =
11573 Instantiate<Internal<Template>::template Bind,
11574 Argument>; //#1
11575
11576 When #1 is parsed, the
11577 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11578 parameter `Template' in #0 matches the
11579 UNBOUND_CLASS_TEMPLATE representing the argument
11580 `Internal<Template>::template Bind'; We then want
11581 to assemble the type `Bind<Argument>' that can't
11582 be fully created right now, because
11583 `Internal<Template>' not being complete, the Bind
11584 template cannot be looked up in that context. So
11585 we need to "store" `Bind<Argument>' for later
11586 when the context of Bind becomes complete. Let's
11587 store that in a TYPENAME_TYPE. */
11588 return make_typename_type (TYPE_CONTEXT (arg),
11589 build_nt (TEMPLATE_ID_EXPR,
11590 TYPE_IDENTIFIER (arg),
11591 argvec),
11592 typename_type,
11593 complain);
11594
11595 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11596 are resolving nested-types in the signature of a
11597 member function templates. Otherwise ARG is a
11598 TEMPLATE_DECL and is the real template to be
11599 instantiated. */
11600 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11601 arg = TYPE_NAME (arg);
11602
11603 r = lookup_template_class (arg,
11604 argvec, in_decl,
11605 DECL_CONTEXT (arg),
11606 /*entering_scope=*/0,
11607 complain);
11608 return cp_build_qualified_type_real
11609 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11610 }
11611 else
11612 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11613 return convert_from_reference (unshare_expr (arg));
11614 }
11615
11616 if (level == 1)
11617 /* This can happen during the attempted tsubst'ing in
11618 unify. This means that we don't yet have any information
11619 about the template parameter in question. */
11620 return t;
11621
11622 /* Early in template argument deduction substitution, we don't
11623 want to reduce the level of 'auto', or it will be confused
11624 with a normal template parm in subsequent deduction. */
11625 if (is_auto (t) && (complain & tf_partial))
11626 return t;
11627
11628 /* If we get here, we must have been looking at a parm for a
11629 more deeply nested template. Make a new version of this
11630 template parameter, but with a lower level. */
11631 switch (code)
11632 {
11633 case TEMPLATE_TYPE_PARM:
11634 case TEMPLATE_TEMPLATE_PARM:
11635 case BOUND_TEMPLATE_TEMPLATE_PARM:
11636 if (cp_type_quals (t))
11637 {
11638 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11639 r = cp_build_qualified_type_real
11640 (r, cp_type_quals (t),
11641 complain | (code == TEMPLATE_TYPE_PARM
11642 ? tf_ignore_bad_quals : 0));
11643 }
11644 else
11645 {
11646 r = copy_type (t);
11647 TEMPLATE_TYPE_PARM_INDEX (r)
11648 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11649 r, levels, args, complain);
11650 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11651 TYPE_MAIN_VARIANT (r) = r;
11652 TYPE_POINTER_TO (r) = NULL_TREE;
11653 TYPE_REFERENCE_TO (r) = NULL_TREE;
11654
11655 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11656 /* We have reduced the level of the template
11657 template parameter, but not the levels of its
11658 template parameters, so canonical_type_parameter
11659 will not be able to find the canonical template
11660 template parameter for this level. Thus, we
11661 require structural equality checking to compare
11662 TEMPLATE_TEMPLATE_PARMs. */
11663 SET_TYPE_STRUCTURAL_EQUALITY (r);
11664 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11665 SET_TYPE_STRUCTURAL_EQUALITY (r);
11666 else
11667 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11668
11669 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11670 {
11671 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11672 complain, in_decl);
11673 if (argvec == error_mark_node)
11674 return error_mark_node;
11675
11676 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11677 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11678 }
11679 }
11680 break;
11681
11682 case TEMPLATE_PARM_INDEX:
11683 r = reduce_template_parm_level (t, type, levels, args, complain);
11684 break;
11685
11686 default:
11687 gcc_unreachable ();
11688 }
11689
11690 return r;
11691 }
11692
11693 case TREE_LIST:
11694 {
11695 tree purpose, value, chain;
11696
11697 if (t == void_list_node)
11698 return t;
11699
11700 purpose = TREE_PURPOSE (t);
11701 if (purpose)
11702 {
11703 purpose = tsubst (purpose, args, complain, in_decl);
11704 if (purpose == error_mark_node)
11705 return error_mark_node;
11706 }
11707 value = TREE_VALUE (t);
11708 if (value)
11709 {
11710 value = tsubst (value, args, complain, in_decl);
11711 if (value == error_mark_node)
11712 return error_mark_node;
11713 }
11714 chain = TREE_CHAIN (t);
11715 if (chain && chain != void_type_node)
11716 {
11717 chain = tsubst (chain, args, complain, in_decl);
11718 if (chain == error_mark_node)
11719 return error_mark_node;
11720 }
11721 if (purpose == TREE_PURPOSE (t)
11722 && value == TREE_VALUE (t)
11723 && chain == TREE_CHAIN (t))
11724 return t;
11725 return hash_tree_cons (purpose, value, chain);
11726 }
11727
11728 case TREE_BINFO:
11729 /* We should never be tsubsting a binfo. */
11730 gcc_unreachable ();
11731
11732 case TREE_VEC:
11733 /* A vector of template arguments. */
11734 gcc_assert (!type);
11735 return tsubst_template_args (t, args, complain, in_decl);
11736
11737 case POINTER_TYPE:
11738 case REFERENCE_TYPE:
11739 {
11740 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11741 return t;
11742
11743 /* [temp.deduct]
11744
11745 Type deduction may fail for any of the following
11746 reasons:
11747
11748 -- Attempting to create a pointer to reference type.
11749 -- Attempting to create a reference to a reference type or
11750 a reference to void.
11751
11752 Core issue 106 says that creating a reference to a reference
11753 during instantiation is no longer a cause for failure. We
11754 only enforce this check in strict C++98 mode. */
11755 if ((TREE_CODE (type) == REFERENCE_TYPE
11756 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11757 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11758 {
11759 static location_t last_loc;
11760
11761 /* We keep track of the last time we issued this error
11762 message to avoid spewing a ton of messages during a
11763 single bad template instantiation. */
11764 if (complain & tf_error
11765 && last_loc != input_location)
11766 {
11767 if (VOID_TYPE_P (type))
11768 error ("forming reference to void");
11769 else if (code == POINTER_TYPE)
11770 error ("forming pointer to reference type %qT", type);
11771 else
11772 error ("forming reference to reference type %qT", type);
11773 last_loc = input_location;
11774 }
11775
11776 return error_mark_node;
11777 }
11778 else if (TREE_CODE (type) == FUNCTION_TYPE
11779 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11780 || type_memfn_rqual (type) != REF_QUAL_NONE))
11781 {
11782 if (complain & tf_error)
11783 {
11784 if (code == POINTER_TYPE)
11785 error ("forming pointer to qualified function type %qT",
11786 type);
11787 else
11788 error ("forming reference to qualified function type %qT",
11789 type);
11790 }
11791 return error_mark_node;
11792 }
11793 else if (code == POINTER_TYPE)
11794 {
11795 r = build_pointer_type (type);
11796 if (TREE_CODE (type) == METHOD_TYPE)
11797 r = build_ptrmemfunc_type (r);
11798 }
11799 else if (TREE_CODE (type) == REFERENCE_TYPE)
11800 /* In C++0x, during template argument substitution, when there is an
11801 attempt to create a reference to a reference type, reference
11802 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11803
11804 "If a template-argument for a template-parameter T names a type
11805 that is a reference to a type A, an attempt to create the type
11806 'lvalue reference to cv T' creates the type 'lvalue reference to
11807 A,' while an attempt to create the type type rvalue reference to
11808 cv T' creates the type T"
11809 */
11810 r = cp_build_reference_type
11811 (TREE_TYPE (type),
11812 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11813 else
11814 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11815 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11816
11817 if (cxx_dialect >= cxx1y
11818 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11819 && array_of_runtime_bound_p (type))
11820 {
11821 if (complain & tf_warning_or_error)
11822 pedwarn
11823 (input_location, OPT_Wvla,
11824 code == REFERENCE_TYPE
11825 ? G_("cannot declare reference to array of runtime bound")
11826 : G_("cannot declare pointer to array of runtime bound"));
11827 else
11828 r = error_mark_node;
11829 }
11830
11831 if (r != error_mark_node)
11832 /* Will this ever be needed for TYPE_..._TO values? */
11833 layout_type (r);
11834
11835 return r;
11836 }
11837 case OFFSET_TYPE:
11838 {
11839 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11840 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11841 {
11842 /* [temp.deduct]
11843
11844 Type deduction may fail for any of the following
11845 reasons:
11846
11847 -- Attempting to create "pointer to member of T" when T
11848 is not a class type. */
11849 if (complain & tf_error)
11850 error ("creating pointer to member of non-class type %qT", r);
11851 return error_mark_node;
11852 }
11853 if (TREE_CODE (type) == REFERENCE_TYPE)
11854 {
11855 if (complain & tf_error)
11856 error ("creating pointer to member reference type %qT", type);
11857 return error_mark_node;
11858 }
11859 if (VOID_TYPE_P (type))
11860 {
11861 if (complain & tf_error)
11862 error ("creating pointer to member of type void");
11863 return error_mark_node;
11864 }
11865 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11866 if (TREE_CODE (type) == FUNCTION_TYPE)
11867 {
11868 /* The type of the implicit object parameter gets its
11869 cv-qualifiers from the FUNCTION_TYPE. */
11870 tree memptr;
11871 tree method_type
11872 = build_memfn_type (type, r, type_memfn_quals (type),
11873 type_memfn_rqual (type));
11874 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11875 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11876 complain);
11877 }
11878 else
11879 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11880 cp_type_quals (t),
11881 complain);
11882 }
11883 case FUNCTION_TYPE:
11884 case METHOD_TYPE:
11885 {
11886 tree fntype;
11887 tree specs;
11888 fntype = tsubst_function_type (t, args, complain, in_decl);
11889 if (fntype == error_mark_node)
11890 return error_mark_node;
11891
11892 /* Substitute the exception specification. */
11893 specs = tsubst_exception_specification (t, args, complain,
11894 in_decl, /*defer_ok*/true);
11895 if (specs == error_mark_node)
11896 return error_mark_node;
11897 if (specs)
11898 fntype = build_exception_variant (fntype, specs);
11899 return fntype;
11900 }
11901 case ARRAY_TYPE:
11902 {
11903 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11904 if (domain == error_mark_node)
11905 return error_mark_node;
11906
11907 /* As an optimization, we avoid regenerating the array type if
11908 it will obviously be the same as T. */
11909 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11910 return t;
11911
11912 /* These checks should match the ones in grokdeclarator.
11913
11914 [temp.deduct]
11915
11916 The deduction may fail for any of the following reasons:
11917
11918 -- Attempting to create an array with an element type that
11919 is void, a function type, or a reference type, or [DR337]
11920 an abstract class type. */
11921 if (VOID_TYPE_P (type)
11922 || TREE_CODE (type) == FUNCTION_TYPE
11923 || TREE_CODE (type) == REFERENCE_TYPE)
11924 {
11925 if (complain & tf_error)
11926 error ("creating array of %qT", type);
11927 return error_mark_node;
11928 }
11929
11930 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11931 return error_mark_node;
11932
11933 r = build_cplus_array_type (type, domain);
11934
11935 if (TYPE_USER_ALIGN (t))
11936 {
11937 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11938 TYPE_USER_ALIGN (r) = 1;
11939 }
11940
11941 return r;
11942 }
11943
11944 case TYPENAME_TYPE:
11945 {
11946 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11947 in_decl, /*entering_scope=*/1);
11948 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11949 complain, in_decl);
11950
11951 if (ctx == error_mark_node || f == error_mark_node)
11952 return error_mark_node;
11953
11954 if (!MAYBE_CLASS_TYPE_P (ctx))
11955 {
11956 if (complain & tf_error)
11957 error ("%qT is not a class, struct, or union type", ctx);
11958 return error_mark_node;
11959 }
11960 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11961 {
11962 /* Normally, make_typename_type does not require that the CTX
11963 have complete type in order to allow things like:
11964
11965 template <class T> struct S { typename S<T>::X Y; };
11966
11967 But, such constructs have already been resolved by this
11968 point, so here CTX really should have complete type, unless
11969 it's a partial instantiation. */
11970 ctx = complete_type (ctx);
11971 if (!COMPLETE_TYPE_P (ctx))
11972 {
11973 if (complain & tf_error)
11974 cxx_incomplete_type_error (NULL_TREE, ctx);
11975 return error_mark_node;
11976 }
11977 }
11978
11979 f = make_typename_type (ctx, f, typename_type,
11980 complain | tf_keep_type_decl);
11981 if (f == error_mark_node)
11982 return f;
11983 if (TREE_CODE (f) == TYPE_DECL)
11984 {
11985 complain |= tf_ignore_bad_quals;
11986 f = TREE_TYPE (f);
11987 }
11988
11989 if (TREE_CODE (f) != TYPENAME_TYPE)
11990 {
11991 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11992 {
11993 if (complain & tf_error)
11994 error ("%qT resolves to %qT, which is not an enumeration type",
11995 t, f);
11996 else
11997 return error_mark_node;
11998 }
11999 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
12000 {
12001 if (complain & tf_error)
12002 error ("%qT resolves to %qT, which is is not a class type",
12003 t, f);
12004 else
12005 return error_mark_node;
12006 }
12007 }
12008
12009 return cp_build_qualified_type_real
12010 (f, cp_type_quals (f) | cp_type_quals (t), complain);
12011 }
12012
12013 case UNBOUND_CLASS_TEMPLATE:
12014 {
12015 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
12016 in_decl, /*entering_scope=*/1);
12017 tree name = TYPE_IDENTIFIER (t);
12018 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12019
12020 if (ctx == error_mark_node || name == error_mark_node)
12021 return error_mark_node;
12022
12023 if (parm_list)
12024 parm_list = tsubst_template_parms (parm_list, args, complain);
12025 return make_unbound_class_template (ctx, name, parm_list, complain);
12026 }
12027
12028 case TYPEOF_TYPE:
12029 {
12030 tree type;
12031
12032 ++cp_unevaluated_operand;
12033 ++c_inhibit_evaluation_warnings;
12034
12035 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12036 complain, in_decl,
12037 /*integral_constant_expression_p=*/false);
12038
12039 --cp_unevaluated_operand;
12040 --c_inhibit_evaluation_warnings;
12041
12042 type = finish_typeof (type);
12043 return cp_build_qualified_type_real (type,
12044 cp_type_quals (t)
12045 | cp_type_quals (type),
12046 complain);
12047 }
12048
12049 case DECLTYPE_TYPE:
12050 {
12051 tree type;
12052
12053 ++cp_unevaluated_operand;
12054 ++c_inhibit_evaluation_warnings;
12055
12056 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12057 complain|tf_decltype, in_decl,
12058 /*function_p*/false,
12059 /*integral_constant_expression*/false);
12060
12061 --cp_unevaluated_operand;
12062 --c_inhibit_evaluation_warnings;
12063
12064 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12065 type = lambda_capture_field_type (type,
12066 DECLTYPE_FOR_INIT_CAPTURE (t));
12067 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12068 type = lambda_proxy_type (type);
12069 else
12070 {
12071 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12072 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12073 && EXPR_P (type))
12074 /* In a template ~id could be either a complement expression
12075 or an unqualified-id naming a destructor; if instantiating
12076 it produces an expression, it's not an id-expression or
12077 member access. */
12078 id = false;
12079 type = finish_decltype_type (type, id, complain);
12080 }
12081 return cp_build_qualified_type_real (type,
12082 cp_type_quals (t)
12083 | cp_type_quals (type),
12084 complain);
12085 }
12086
12087 case UNDERLYING_TYPE:
12088 {
12089 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12090 complain, in_decl);
12091 return finish_underlying_type (type);
12092 }
12093
12094 case TYPE_ARGUMENT_PACK:
12095 case NONTYPE_ARGUMENT_PACK:
12096 {
12097 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12098 tree packed_out =
12099 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12100 args,
12101 complain,
12102 in_decl);
12103 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12104
12105 /* For template nontype argument packs, also substitute into
12106 the type. */
12107 if (code == NONTYPE_ARGUMENT_PACK)
12108 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12109
12110 return r;
12111 }
12112 break;
12113
12114 case INTEGER_CST:
12115 case REAL_CST:
12116 case STRING_CST:
12117 case PLUS_EXPR:
12118 case MINUS_EXPR:
12119 case NEGATE_EXPR:
12120 case NOP_EXPR:
12121 case INDIRECT_REF:
12122 case ADDR_EXPR:
12123 case CALL_EXPR:
12124 case ARRAY_REF:
12125 case SCOPE_REF:
12126 /* We should use one of the expression tsubsts for these codes. */
12127 gcc_unreachable ();
12128
12129 default:
12130 sorry ("use of %qs in template", get_tree_code_name (code));
12131 return error_mark_node;
12132 }
12133 }
12134
12135 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12136 type of the expression on the left-hand side of the "." or "->"
12137 operator. */
12138
12139 static tree
12140 tsubst_baselink (tree baselink, tree object_type,
12141 tree args, tsubst_flags_t complain, tree in_decl)
12142 {
12143 tree name;
12144 tree qualifying_scope;
12145 tree fns;
12146 tree optype;
12147 tree template_args = 0;
12148 bool template_id_p = false;
12149 bool qualified = BASELINK_QUALIFIED_P (baselink);
12150
12151 /* A baselink indicates a function from a base class. Both the
12152 BASELINK_ACCESS_BINFO and the base class referenced may
12153 indicate bases of the template class, rather than the
12154 instantiated class. In addition, lookups that were not
12155 ambiguous before may be ambiguous now. Therefore, we perform
12156 the lookup again. */
12157 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12158 qualifying_scope = tsubst (qualifying_scope, args,
12159 complain, in_decl);
12160 fns = BASELINK_FUNCTIONS (baselink);
12161 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12162 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12163 {
12164 template_id_p = true;
12165 template_args = TREE_OPERAND (fns, 1);
12166 fns = TREE_OPERAND (fns, 0);
12167 if (template_args)
12168 template_args = tsubst_template_args (template_args, args,
12169 complain, in_decl);
12170 }
12171 name = DECL_NAME (get_first_fn (fns));
12172 if (IDENTIFIER_TYPENAME_P (name))
12173 name = mangle_conv_op_name_for_type (optype);
12174 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12175 if (!baselink)
12176 return error_mark_node;
12177
12178 /* If lookup found a single function, mark it as used at this
12179 point. (If it lookup found multiple functions the one selected
12180 later by overload resolution will be marked as used at that
12181 point.) */
12182 if (BASELINK_P (baselink))
12183 fns = BASELINK_FUNCTIONS (baselink);
12184 if (!template_id_p && !really_overloaded_fn (fns))
12185 mark_used (OVL_CURRENT (fns));
12186
12187 /* Add back the template arguments, if present. */
12188 if (BASELINK_P (baselink) && template_id_p)
12189 BASELINK_FUNCTIONS (baselink)
12190 = build_nt (TEMPLATE_ID_EXPR,
12191 BASELINK_FUNCTIONS (baselink),
12192 template_args);
12193 /* Update the conversion operator type. */
12194 BASELINK_OPTYPE (baselink) = optype;
12195
12196 if (!object_type)
12197 object_type = current_class_type;
12198
12199 if (qualified)
12200 baselink = adjust_result_of_qualified_name_lookup (baselink,
12201 qualifying_scope,
12202 object_type);
12203 return baselink;
12204 }
12205
12206 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12207 true if the qualified-id will be a postfix-expression in-and-of
12208 itself; false if more of the postfix-expression follows the
12209 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12210 of "&". */
12211
12212 static tree
12213 tsubst_qualified_id (tree qualified_id, tree args,
12214 tsubst_flags_t complain, tree in_decl,
12215 bool done, bool address_p)
12216 {
12217 tree expr;
12218 tree scope;
12219 tree name;
12220 bool is_template;
12221 tree template_args;
12222 location_t loc = UNKNOWN_LOCATION;
12223
12224 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12225
12226 /* Figure out what name to look up. */
12227 name = TREE_OPERAND (qualified_id, 1);
12228 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12229 {
12230 is_template = true;
12231 loc = EXPR_LOCATION (name);
12232 template_args = TREE_OPERAND (name, 1);
12233 if (template_args)
12234 template_args = tsubst_template_args (template_args, args,
12235 complain, in_decl);
12236 name = TREE_OPERAND (name, 0);
12237 }
12238 else
12239 {
12240 is_template = false;
12241 template_args = NULL_TREE;
12242 }
12243
12244 /* Substitute into the qualifying scope. When there are no ARGS, we
12245 are just trying to simplify a non-dependent expression. In that
12246 case the qualifying scope may be dependent, and, in any case,
12247 substituting will not help. */
12248 scope = TREE_OPERAND (qualified_id, 0);
12249 if (args)
12250 {
12251 scope = tsubst (scope, args, complain, in_decl);
12252 expr = tsubst_copy (name, args, complain, in_decl);
12253 }
12254 else
12255 expr = name;
12256
12257 if (dependent_scope_p (scope))
12258 {
12259 if (is_template)
12260 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12261 return build_qualified_name (NULL_TREE, scope, expr,
12262 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12263 }
12264
12265 if (!BASELINK_P (name) && !DECL_P (expr))
12266 {
12267 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12268 {
12269 /* A BIT_NOT_EXPR is used to represent a destructor. */
12270 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12271 {
12272 error ("qualifying type %qT does not match destructor name ~%qT",
12273 scope, TREE_OPERAND (expr, 0));
12274 expr = error_mark_node;
12275 }
12276 else
12277 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12278 /*is_type_p=*/0, false);
12279 }
12280 else
12281 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12282 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12283 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12284 {
12285 if (complain & tf_error)
12286 {
12287 error ("dependent-name %qE is parsed as a non-type, but "
12288 "instantiation yields a type", qualified_id);
12289 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12290 }
12291 return error_mark_node;
12292 }
12293 }
12294
12295 if (DECL_P (expr))
12296 {
12297 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12298 scope);
12299 /* Remember that there was a reference to this entity. */
12300 mark_used (expr);
12301 }
12302
12303 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12304 {
12305 if (complain & tf_error)
12306 qualified_name_lookup_error (scope,
12307 TREE_OPERAND (qualified_id, 1),
12308 expr, input_location);
12309 return error_mark_node;
12310 }
12311
12312 if (is_template)
12313 expr = lookup_template_function (expr, template_args);
12314
12315 if (expr == error_mark_node && complain & tf_error)
12316 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12317 expr, input_location);
12318 else if (TYPE_P (scope))
12319 {
12320 expr = (adjust_result_of_qualified_name_lookup
12321 (expr, scope, current_nonlambda_class_type ()));
12322 expr = (finish_qualified_id_expr
12323 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12324 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12325 /*template_arg_p=*/false, complain));
12326 }
12327
12328 /* Expressions do not generally have reference type. */
12329 if (TREE_CODE (expr) != SCOPE_REF
12330 /* However, if we're about to form a pointer-to-member, we just
12331 want the referenced member referenced. */
12332 && TREE_CODE (expr) != OFFSET_REF)
12333 expr = convert_from_reference (expr);
12334
12335 return expr;
12336 }
12337
12338 /* Like tsubst, but deals with expressions. This function just replaces
12339 template parms; to finish processing the resultant expression, use
12340 tsubst_copy_and_build or tsubst_expr. */
12341
12342 static tree
12343 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12344 {
12345 enum tree_code code;
12346 tree r;
12347
12348 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12349 return t;
12350
12351 code = TREE_CODE (t);
12352
12353 switch (code)
12354 {
12355 case PARM_DECL:
12356 r = retrieve_local_specialization (t);
12357
12358 if (r == NULL_TREE)
12359 {
12360 /* We get here for a use of 'this' in an NSDMI. */
12361 if (DECL_NAME (t) == this_identifier
12362 && at_function_scope_p ()
12363 && DECL_CONSTRUCTOR_P (current_function_decl))
12364 return current_class_ptr;
12365
12366 /* This can happen for a parameter name used later in a function
12367 declaration (such as in a late-specified return type). Just
12368 make a dummy decl, since it's only used for its type. */
12369 gcc_assert (cp_unevaluated_operand != 0);
12370 r = tsubst_decl (t, args, complain);
12371 /* Give it the template pattern as its context; its true context
12372 hasn't been instantiated yet and this is good enough for
12373 mangling. */
12374 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12375 }
12376
12377 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12378 r = ARGUMENT_PACK_SELECT_ARG (r);
12379 mark_used (r);
12380 return r;
12381
12382 case CONST_DECL:
12383 {
12384 tree enum_type;
12385 tree v;
12386
12387 if (DECL_TEMPLATE_PARM_P (t))
12388 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12389 /* There is no need to substitute into namespace-scope
12390 enumerators. */
12391 if (DECL_NAMESPACE_SCOPE_P (t))
12392 return t;
12393 /* If ARGS is NULL, then T is known to be non-dependent. */
12394 if (args == NULL_TREE)
12395 return integral_constant_value (t);
12396
12397 /* Unfortunately, we cannot just call lookup_name here.
12398 Consider:
12399
12400 template <int I> int f() {
12401 enum E { a = I };
12402 struct S { void g() { E e = a; } };
12403 };
12404
12405 When we instantiate f<7>::S::g(), say, lookup_name is not
12406 clever enough to find f<7>::a. */
12407 enum_type
12408 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12409 /*entering_scope=*/0);
12410
12411 for (v = TYPE_VALUES (enum_type);
12412 v != NULL_TREE;
12413 v = TREE_CHAIN (v))
12414 if (TREE_PURPOSE (v) == DECL_NAME (t))
12415 return TREE_VALUE (v);
12416
12417 /* We didn't find the name. That should never happen; if
12418 name-lookup found it during preliminary parsing, we
12419 should find it again here during instantiation. */
12420 gcc_unreachable ();
12421 }
12422 return t;
12423
12424 case FIELD_DECL:
12425 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12426 {
12427 /* Check for a local specialization set up by
12428 tsubst_pack_expansion. */
12429 tree r = retrieve_local_specialization (t);
12430 if (r)
12431 {
12432 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12433 r = ARGUMENT_PACK_SELECT_ARG (r);
12434 return r;
12435 }
12436
12437 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12438 tsubst_decl put in the hash table. */
12439 return retrieve_specialization (t, args, 0);
12440 }
12441
12442 if (DECL_CONTEXT (t))
12443 {
12444 tree ctx;
12445
12446 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12447 /*entering_scope=*/1);
12448 if (ctx != DECL_CONTEXT (t))
12449 {
12450 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12451 if (!r)
12452 {
12453 if (complain & tf_error)
12454 error ("using invalid field %qD", t);
12455 return error_mark_node;
12456 }
12457 return r;
12458 }
12459 }
12460
12461 return t;
12462
12463 case VAR_DECL:
12464 case FUNCTION_DECL:
12465 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12466 r = tsubst (t, args, complain, in_decl);
12467 else if (local_variable_p (t))
12468 {
12469 r = retrieve_local_specialization (t);
12470 if (r == NULL_TREE)
12471 {
12472 if (DECL_ANON_UNION_VAR_P (t))
12473 {
12474 /* Just use name lookup to find a member alias for an
12475 anonymous union, but then add it to the hash table. */
12476 r = lookup_name (DECL_NAME (t));
12477 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12478 register_local_specialization (r, t);
12479 }
12480 else
12481 {
12482 /* This can happen for a variable used in a late-specified
12483 return type of a local lambda. Just make a dummy decl
12484 since it's only used for its type. */
12485 if (cp_unevaluated_operand)
12486 return tsubst_decl (t, args, complain);
12487 gcc_assert (errorcount || sorrycount);
12488 return error_mark_node;
12489 }
12490 }
12491 }
12492 else
12493 r = t;
12494 mark_used (r);
12495 return r;
12496
12497 case NAMESPACE_DECL:
12498 return t;
12499
12500 case OVERLOAD:
12501 /* An OVERLOAD will always be a non-dependent overload set; an
12502 overload set from function scope will just be represented with an
12503 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12504 gcc_assert (!uses_template_parms (t));
12505 return t;
12506
12507 case BASELINK:
12508 return tsubst_baselink (t, current_nonlambda_class_type (),
12509 args, complain, in_decl);
12510
12511 case TEMPLATE_DECL:
12512 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12513 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12514 args, complain, in_decl);
12515 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12516 return tsubst (t, args, complain, in_decl);
12517 else if (DECL_CLASS_SCOPE_P (t)
12518 && uses_template_parms (DECL_CONTEXT (t)))
12519 {
12520 /* Template template argument like the following example need
12521 special treatment:
12522
12523 template <template <class> class TT> struct C {};
12524 template <class T> struct D {
12525 template <class U> struct E {};
12526 C<E> c; // #1
12527 };
12528 D<int> d; // #2
12529
12530 We are processing the template argument `E' in #1 for
12531 the template instantiation #2. Originally, `E' is a
12532 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12533 have to substitute this with one having context `D<int>'. */
12534
12535 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12536 return lookup_field (context, DECL_NAME(t), 0, false);
12537 }
12538 else
12539 /* Ordinary template template argument. */
12540 return t;
12541
12542 case CAST_EXPR:
12543 case REINTERPRET_CAST_EXPR:
12544 case CONST_CAST_EXPR:
12545 case STATIC_CAST_EXPR:
12546 case DYNAMIC_CAST_EXPR:
12547 case IMPLICIT_CONV_EXPR:
12548 case CONVERT_EXPR:
12549 case NOP_EXPR:
12550 return build1
12551 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12552 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12553
12554 case SIZEOF_EXPR:
12555 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12556 {
12557
12558 tree expanded, op = TREE_OPERAND (t, 0);
12559 int len = 0;
12560
12561 if (SIZEOF_EXPR_TYPE_P (t))
12562 op = TREE_TYPE (op);
12563
12564 ++cp_unevaluated_operand;
12565 ++c_inhibit_evaluation_warnings;
12566 /* We only want to compute the number of arguments. */
12567 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12568 --cp_unevaluated_operand;
12569 --c_inhibit_evaluation_warnings;
12570
12571 if (TREE_CODE (expanded) == TREE_VEC)
12572 len = TREE_VEC_LENGTH (expanded);
12573
12574 if (expanded == error_mark_node)
12575 return error_mark_node;
12576 else if (PACK_EXPANSION_P (expanded)
12577 || (TREE_CODE (expanded) == TREE_VEC
12578 && len > 0
12579 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12580 {
12581 if (TREE_CODE (expanded) == TREE_VEC)
12582 expanded = TREE_VEC_ELT (expanded, len - 1);
12583
12584 if (TYPE_P (expanded))
12585 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12586 complain & tf_error);
12587 else
12588 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12589 complain & tf_error);
12590 }
12591 else
12592 return build_int_cst (size_type_node, len);
12593 }
12594 if (SIZEOF_EXPR_TYPE_P (t))
12595 {
12596 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12597 args, complain, in_decl);
12598 r = build1 (NOP_EXPR, r, error_mark_node);
12599 r = build1 (SIZEOF_EXPR,
12600 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12601 SIZEOF_EXPR_TYPE_P (r) = 1;
12602 return r;
12603 }
12604 /* Fall through */
12605
12606 case INDIRECT_REF:
12607 case NEGATE_EXPR:
12608 case TRUTH_NOT_EXPR:
12609 case BIT_NOT_EXPR:
12610 case ADDR_EXPR:
12611 case UNARY_PLUS_EXPR: /* Unary + */
12612 case ALIGNOF_EXPR:
12613 case AT_ENCODE_EXPR:
12614 case ARROW_EXPR:
12615 case THROW_EXPR:
12616 case TYPEID_EXPR:
12617 case REALPART_EXPR:
12618 case IMAGPART_EXPR:
12619 case PAREN_EXPR:
12620 return build1
12621 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12622 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12623
12624 case COMPONENT_REF:
12625 {
12626 tree object;
12627 tree name;
12628
12629 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12630 name = TREE_OPERAND (t, 1);
12631 if (TREE_CODE (name) == BIT_NOT_EXPR)
12632 {
12633 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12634 complain, in_decl);
12635 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12636 }
12637 else if (TREE_CODE (name) == SCOPE_REF
12638 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12639 {
12640 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12641 complain, in_decl);
12642 name = TREE_OPERAND (name, 1);
12643 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12644 complain, in_decl);
12645 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12646 name = build_qualified_name (/*type=*/NULL_TREE,
12647 base, name,
12648 /*template_p=*/false);
12649 }
12650 else if (BASELINK_P (name))
12651 name = tsubst_baselink (name,
12652 non_reference (TREE_TYPE (object)),
12653 args, complain,
12654 in_decl);
12655 else
12656 name = tsubst_copy (name, args, complain, in_decl);
12657 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12658 }
12659
12660 case PLUS_EXPR:
12661 case MINUS_EXPR:
12662 case MULT_EXPR:
12663 case TRUNC_DIV_EXPR:
12664 case CEIL_DIV_EXPR:
12665 case FLOOR_DIV_EXPR:
12666 case ROUND_DIV_EXPR:
12667 case EXACT_DIV_EXPR:
12668 case BIT_AND_EXPR:
12669 case BIT_IOR_EXPR:
12670 case BIT_XOR_EXPR:
12671 case TRUNC_MOD_EXPR:
12672 case FLOOR_MOD_EXPR:
12673 case TRUTH_ANDIF_EXPR:
12674 case TRUTH_ORIF_EXPR:
12675 case TRUTH_AND_EXPR:
12676 case TRUTH_OR_EXPR:
12677 case RSHIFT_EXPR:
12678 case LSHIFT_EXPR:
12679 case RROTATE_EXPR:
12680 case LROTATE_EXPR:
12681 case EQ_EXPR:
12682 case NE_EXPR:
12683 case MAX_EXPR:
12684 case MIN_EXPR:
12685 case LE_EXPR:
12686 case GE_EXPR:
12687 case LT_EXPR:
12688 case GT_EXPR:
12689 case COMPOUND_EXPR:
12690 case DOTSTAR_EXPR:
12691 case MEMBER_REF:
12692 case PREDECREMENT_EXPR:
12693 case PREINCREMENT_EXPR:
12694 case POSTDECREMENT_EXPR:
12695 case POSTINCREMENT_EXPR:
12696 return build_nt
12697 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12698 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12699
12700 case SCOPE_REF:
12701 return build_qualified_name (/*type=*/NULL_TREE,
12702 tsubst_copy (TREE_OPERAND (t, 0),
12703 args, complain, in_decl),
12704 tsubst_copy (TREE_OPERAND (t, 1),
12705 args, complain, in_decl),
12706 QUALIFIED_NAME_IS_TEMPLATE (t));
12707
12708 case ARRAY_REF:
12709 return build_nt
12710 (ARRAY_REF,
12711 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12712 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12713 NULL_TREE, NULL_TREE);
12714
12715 case CALL_EXPR:
12716 {
12717 int n = VL_EXP_OPERAND_LENGTH (t);
12718 tree result = build_vl_exp (CALL_EXPR, n);
12719 int i;
12720 for (i = 0; i < n; i++)
12721 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12722 complain, in_decl);
12723 return result;
12724 }
12725
12726 case COND_EXPR:
12727 case MODOP_EXPR:
12728 case PSEUDO_DTOR_EXPR:
12729 case VEC_PERM_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 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12736 return r;
12737 }
12738
12739 case NEW_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 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12745 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12746 return r;
12747 }
12748
12749 case DELETE_EXPR:
12750 {
12751 r = build_nt
12752 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12753 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12754 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12755 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12756 return r;
12757 }
12758
12759 case TEMPLATE_ID_EXPR:
12760 {
12761 /* Substituted template arguments */
12762 tree fn = TREE_OPERAND (t, 0);
12763 tree targs = TREE_OPERAND (t, 1);
12764
12765 fn = tsubst_copy (fn, args, complain, in_decl);
12766 if (targs)
12767 targs = tsubst_template_args (targs, args, complain, in_decl);
12768
12769 return lookup_template_function (fn, targs);
12770 }
12771
12772 case TREE_LIST:
12773 {
12774 tree purpose, value, chain;
12775
12776 if (t == void_list_node)
12777 return t;
12778
12779 purpose = TREE_PURPOSE (t);
12780 if (purpose)
12781 purpose = tsubst_copy (purpose, args, complain, in_decl);
12782 value = TREE_VALUE (t);
12783 if (value)
12784 value = tsubst_copy (value, args, complain, in_decl);
12785 chain = TREE_CHAIN (t);
12786 if (chain && chain != void_type_node)
12787 chain = tsubst_copy (chain, args, complain, in_decl);
12788 if (purpose == TREE_PURPOSE (t)
12789 && value == TREE_VALUE (t)
12790 && chain == TREE_CHAIN (t))
12791 return t;
12792 return tree_cons (purpose, value, chain);
12793 }
12794
12795 case RECORD_TYPE:
12796 case UNION_TYPE:
12797 case ENUMERAL_TYPE:
12798 case INTEGER_TYPE:
12799 case TEMPLATE_TYPE_PARM:
12800 case TEMPLATE_TEMPLATE_PARM:
12801 case BOUND_TEMPLATE_TEMPLATE_PARM:
12802 case TEMPLATE_PARM_INDEX:
12803 case POINTER_TYPE:
12804 case REFERENCE_TYPE:
12805 case OFFSET_TYPE:
12806 case FUNCTION_TYPE:
12807 case METHOD_TYPE:
12808 case ARRAY_TYPE:
12809 case TYPENAME_TYPE:
12810 case UNBOUND_CLASS_TEMPLATE:
12811 case TYPEOF_TYPE:
12812 case DECLTYPE_TYPE:
12813 case TYPE_DECL:
12814 return tsubst (t, args, complain, in_decl);
12815
12816 case USING_DECL:
12817 t = DECL_NAME (t);
12818 /* Fall through. */
12819 case IDENTIFIER_NODE:
12820 if (IDENTIFIER_TYPENAME_P (t))
12821 {
12822 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12823 return mangle_conv_op_name_for_type (new_type);
12824 }
12825 else
12826 return t;
12827
12828 case CONSTRUCTOR:
12829 /* This is handled by tsubst_copy_and_build. */
12830 gcc_unreachable ();
12831
12832 case VA_ARG_EXPR:
12833 return build_x_va_arg (EXPR_LOCATION (t),
12834 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12835 in_decl),
12836 tsubst (TREE_TYPE (t), args, complain, in_decl));
12837
12838 case CLEANUP_POINT_EXPR:
12839 /* We shouldn't have built any of these during initial template
12840 generation. Instead, they should be built during instantiation
12841 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12842 gcc_unreachable ();
12843
12844 case OFFSET_REF:
12845 r = build2
12846 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12847 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12848 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12849 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12850 mark_used (TREE_OPERAND (r, 1));
12851 return r;
12852
12853 case EXPR_PACK_EXPANSION:
12854 error ("invalid use of pack expansion expression");
12855 return error_mark_node;
12856
12857 case NONTYPE_ARGUMENT_PACK:
12858 error ("use %<...%> to expand argument pack");
12859 return error_mark_node;
12860
12861 case INTEGER_CST:
12862 case REAL_CST:
12863 case STRING_CST:
12864 case COMPLEX_CST:
12865 {
12866 /* Instantiate any typedefs in the type. */
12867 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12868 r = fold_convert (type, t);
12869 gcc_assert (TREE_CODE (r) == code);
12870 return r;
12871 }
12872
12873 case PTRMEM_CST:
12874 /* These can sometimes show up in a partial instantiation, but never
12875 involve template parms. */
12876 gcc_assert (!uses_template_parms (t));
12877 return t;
12878
12879 default:
12880 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12881 gcc_checking_assert (false);
12882 return t;
12883 }
12884 }
12885
12886 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12887
12888 static tree
12889 tsubst_omp_clauses (tree clauses, bool declare_simd,
12890 tree args, tsubst_flags_t complain, tree in_decl)
12891 {
12892 tree new_clauses = NULL, nc, oc;
12893
12894 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12895 {
12896 nc = copy_node (oc);
12897 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12898 new_clauses = nc;
12899
12900 switch (OMP_CLAUSE_CODE (nc))
12901 {
12902 case OMP_CLAUSE_LASTPRIVATE:
12903 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12904 {
12905 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12906 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12907 in_decl, /*integral_constant_expression_p=*/false);
12908 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12909 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12910 }
12911 /* FALLTHRU */
12912 case OMP_CLAUSE_PRIVATE:
12913 case OMP_CLAUSE_SHARED:
12914 case OMP_CLAUSE_FIRSTPRIVATE:
12915 case OMP_CLAUSE_COPYIN:
12916 case OMP_CLAUSE_COPYPRIVATE:
12917 case OMP_CLAUSE_IF:
12918 case OMP_CLAUSE_NUM_THREADS:
12919 case OMP_CLAUSE_SCHEDULE:
12920 case OMP_CLAUSE_COLLAPSE:
12921 case OMP_CLAUSE_FINAL:
12922 case OMP_CLAUSE_DEPEND:
12923 case OMP_CLAUSE_FROM:
12924 case OMP_CLAUSE_TO:
12925 case OMP_CLAUSE_UNIFORM:
12926 case OMP_CLAUSE_MAP:
12927 case OMP_CLAUSE_DEVICE:
12928 case OMP_CLAUSE_DIST_SCHEDULE:
12929 case OMP_CLAUSE_NUM_TEAMS:
12930 case OMP_CLAUSE_THREAD_LIMIT:
12931 case OMP_CLAUSE_SAFELEN:
12932 case OMP_CLAUSE_SIMDLEN:
12933 OMP_CLAUSE_OPERAND (nc, 0)
12934 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12935 in_decl, /*integral_constant_expression_p=*/false);
12936 break;
12937 case OMP_CLAUSE_REDUCTION:
12938 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12939 {
12940 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12941 if (TREE_CODE (placeholder) == SCOPE_REF)
12942 {
12943 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12944 complain, in_decl);
12945 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12946 = build_qualified_name (NULL_TREE, scope,
12947 TREE_OPERAND (placeholder, 1),
12948 false);
12949 }
12950 else
12951 gcc_assert (identifier_p (placeholder));
12952 }
12953 OMP_CLAUSE_OPERAND (nc, 0)
12954 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12955 in_decl, /*integral_constant_expression_p=*/false);
12956 break;
12957 case OMP_CLAUSE_LINEAR:
12958 case OMP_CLAUSE_ALIGNED:
12959 OMP_CLAUSE_OPERAND (nc, 0)
12960 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12961 in_decl, /*integral_constant_expression_p=*/false);
12962 OMP_CLAUSE_OPERAND (nc, 1)
12963 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12964 in_decl, /*integral_constant_expression_p=*/false);
12965 break;
12966
12967 case OMP_CLAUSE_NOWAIT:
12968 case OMP_CLAUSE_ORDERED:
12969 case OMP_CLAUSE_DEFAULT:
12970 case OMP_CLAUSE_UNTIED:
12971 case OMP_CLAUSE_MERGEABLE:
12972 case OMP_CLAUSE_INBRANCH:
12973 case OMP_CLAUSE_NOTINBRANCH:
12974 case OMP_CLAUSE_PROC_BIND:
12975 case OMP_CLAUSE_FOR:
12976 case OMP_CLAUSE_PARALLEL:
12977 case OMP_CLAUSE_SECTIONS:
12978 case OMP_CLAUSE_TASKGROUP:
12979 break;
12980 default:
12981 gcc_unreachable ();
12982 }
12983 }
12984
12985 new_clauses = nreverse (new_clauses);
12986 if (!declare_simd)
12987 new_clauses = finish_omp_clauses (new_clauses);
12988 return new_clauses;
12989 }
12990
12991 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12992
12993 static tree
12994 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12995 tree in_decl)
12996 {
12997 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12998
12999 tree purpose, value, chain;
13000
13001 if (t == NULL)
13002 return t;
13003
13004 if (TREE_CODE (t) != TREE_LIST)
13005 return tsubst_copy_and_build (t, args, complain, in_decl,
13006 /*function_p=*/false,
13007 /*integral_constant_expression_p=*/false);
13008
13009 if (t == void_list_node)
13010 return t;
13011
13012 purpose = TREE_PURPOSE (t);
13013 if (purpose)
13014 purpose = RECUR (purpose);
13015 value = TREE_VALUE (t);
13016 if (value)
13017 {
13018 if (TREE_CODE (value) != LABEL_DECL)
13019 value = RECUR (value);
13020 else
13021 {
13022 value = lookup_label (DECL_NAME (value));
13023 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13024 TREE_USED (value) = 1;
13025 }
13026 }
13027 chain = TREE_CHAIN (t);
13028 if (chain && chain != void_type_node)
13029 chain = RECUR (chain);
13030 return tree_cons (purpose, value, chain);
13031 #undef RECUR
13032 }
13033
13034 /* Substitute one OMP_FOR iterator. */
13035
13036 static void
13037 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13038 tree condv, tree incrv, tree *clauses,
13039 tree args, tsubst_flags_t complain, tree in_decl,
13040 bool integral_constant_expression_p)
13041 {
13042 #define RECUR(NODE) \
13043 tsubst_expr ((NODE), args, complain, in_decl, \
13044 integral_constant_expression_p)
13045 tree decl, init, cond, incr;
13046 bool init_decl;
13047
13048 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13049 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13050 decl = TREE_OPERAND (init, 0);
13051 init = TREE_OPERAND (init, 1);
13052 /* Do this before substituting into decl to handle 'auto'. */
13053 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13054 init = RECUR (init);
13055 decl = RECUR (decl);
13056
13057 if (decl == error_mark_node || init == error_mark_node)
13058 return;
13059
13060 if (init_decl)
13061 {
13062 gcc_assert (!processing_template_decl);
13063 init = DECL_INITIAL (decl);
13064 DECL_INITIAL (decl) = NULL_TREE;
13065 }
13066
13067 gcc_assert (!type_dependent_expression_p (decl));
13068
13069 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13070 {
13071 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13072 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13073 if (TREE_CODE (incr) == MODIFY_EXPR)
13074 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13075 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13076 RECUR (TREE_OPERAND (incr, 1)),
13077 complain);
13078 else
13079 incr = RECUR (incr);
13080 TREE_VEC_ELT (declv, i) = decl;
13081 TREE_VEC_ELT (initv, i) = init;
13082 TREE_VEC_ELT (condv, i) = cond;
13083 TREE_VEC_ELT (incrv, i) = incr;
13084 return;
13085 }
13086
13087 if (init && !init_decl)
13088 {
13089 tree c;
13090 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13091 {
13092 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13093 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13094 && OMP_CLAUSE_DECL (c) == decl)
13095 break;
13096 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13097 && OMP_CLAUSE_DECL (c) == decl)
13098 error ("iteration variable %qD should not be firstprivate", decl);
13099 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13100 && OMP_CLAUSE_DECL (c) == decl)
13101 error ("iteration variable %qD should not be reduction", decl);
13102 }
13103 if (c == NULL)
13104 {
13105 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13106 OMP_CLAUSE_DECL (c) = decl;
13107 c = finish_omp_clauses (c);
13108 if (c)
13109 {
13110 OMP_CLAUSE_CHAIN (c) = *clauses;
13111 *clauses = c;
13112 }
13113 }
13114 }
13115 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13116 if (COMPARISON_CLASS_P (cond))
13117 cond = build2 (TREE_CODE (cond), boolean_type_node,
13118 RECUR (TREE_OPERAND (cond, 0)),
13119 RECUR (TREE_OPERAND (cond, 1)));
13120 else
13121 cond = RECUR (cond);
13122 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13123 switch (TREE_CODE (incr))
13124 {
13125 case PREINCREMENT_EXPR:
13126 case PREDECREMENT_EXPR:
13127 case POSTINCREMENT_EXPR:
13128 case POSTDECREMENT_EXPR:
13129 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13130 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13131 break;
13132 case MODIFY_EXPR:
13133 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13134 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13135 {
13136 tree rhs = TREE_OPERAND (incr, 1);
13137 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13138 RECUR (TREE_OPERAND (incr, 0)),
13139 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13140 RECUR (TREE_OPERAND (rhs, 0)),
13141 RECUR (TREE_OPERAND (rhs, 1))));
13142 }
13143 else
13144 incr = RECUR (incr);
13145 break;
13146 case MODOP_EXPR:
13147 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13148 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13149 {
13150 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13151 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13152 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13153 TREE_TYPE (decl), lhs,
13154 RECUR (TREE_OPERAND (incr, 2))));
13155 }
13156 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13157 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13158 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13159 {
13160 tree rhs = TREE_OPERAND (incr, 2);
13161 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13162 RECUR (TREE_OPERAND (incr, 0)),
13163 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13164 RECUR (TREE_OPERAND (rhs, 0)),
13165 RECUR (TREE_OPERAND (rhs, 1))));
13166 }
13167 else
13168 incr = RECUR (incr);
13169 break;
13170 default:
13171 incr = RECUR (incr);
13172 break;
13173 }
13174
13175 TREE_VEC_ELT (declv, i) = decl;
13176 TREE_VEC_ELT (initv, i) = init;
13177 TREE_VEC_ELT (condv, i) = cond;
13178 TREE_VEC_ELT (incrv, i) = incr;
13179 #undef RECUR
13180 }
13181
13182 /* Like tsubst_copy for expressions, etc. but also does semantic
13183 processing. */
13184
13185 static tree
13186 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13187 bool integral_constant_expression_p)
13188 {
13189 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13190 #define RECUR(NODE) \
13191 tsubst_expr ((NODE), args, complain, in_decl, \
13192 integral_constant_expression_p)
13193
13194 tree stmt, tmp;
13195 tree r;
13196 location_t loc;
13197
13198 if (t == NULL_TREE || t == error_mark_node)
13199 return t;
13200
13201 loc = input_location;
13202 if (EXPR_HAS_LOCATION (t))
13203 input_location = EXPR_LOCATION (t);
13204 if (STATEMENT_CODE_P (TREE_CODE (t)))
13205 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13206
13207 switch (TREE_CODE (t))
13208 {
13209 case STATEMENT_LIST:
13210 {
13211 tree_stmt_iterator i;
13212 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13213 RECUR (tsi_stmt (i));
13214 break;
13215 }
13216
13217 case CTOR_INITIALIZER:
13218 finish_mem_initializers (tsubst_initializer_list
13219 (TREE_OPERAND (t, 0), args));
13220 break;
13221
13222 case RETURN_EXPR:
13223 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13224 break;
13225
13226 case EXPR_STMT:
13227 tmp = RECUR (EXPR_STMT_EXPR (t));
13228 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13229 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13230 else
13231 finish_expr_stmt (tmp);
13232 break;
13233
13234 case USING_STMT:
13235 do_using_directive (USING_STMT_NAMESPACE (t));
13236 break;
13237
13238 case DECL_EXPR:
13239 {
13240 tree decl, pattern_decl;
13241 tree init;
13242
13243 pattern_decl = decl = DECL_EXPR_DECL (t);
13244 if (TREE_CODE (decl) == LABEL_DECL)
13245 finish_label_decl (DECL_NAME (decl));
13246 else if (TREE_CODE (decl) == USING_DECL)
13247 {
13248 tree scope = USING_DECL_SCOPE (decl);
13249 tree name = DECL_NAME (decl);
13250 tree decl;
13251
13252 scope = tsubst (scope, args, complain, in_decl);
13253 decl = lookup_qualified_name (scope, name,
13254 /*is_type_p=*/false,
13255 /*complain=*/false);
13256 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13257 qualified_name_lookup_error (scope, name, decl, input_location);
13258 else
13259 do_local_using_decl (decl, scope, name);
13260 }
13261 else if (DECL_PACK_P (decl))
13262 {
13263 /* Don't build up decls for a variadic capture proxy, we'll
13264 instantiate the elements directly as needed. */
13265 break;
13266 }
13267 else
13268 {
13269 init = DECL_INITIAL (decl);
13270 decl = tsubst (decl, args, complain, in_decl);
13271 if (decl != error_mark_node)
13272 {
13273 /* By marking the declaration as instantiated, we avoid
13274 trying to instantiate it. Since instantiate_decl can't
13275 handle local variables, and since we've already done
13276 all that needs to be done, that's the right thing to
13277 do. */
13278 if (VAR_P (decl))
13279 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13280 if (VAR_P (decl)
13281 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13282 /* Anonymous aggregates are a special case. */
13283 finish_anon_union (decl);
13284 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13285 {
13286 DECL_CONTEXT (decl) = current_function_decl;
13287 if (DECL_NAME (decl) == this_identifier)
13288 {
13289 tree lam = DECL_CONTEXT (current_function_decl);
13290 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13291 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13292 }
13293 insert_capture_proxy (decl);
13294 }
13295 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13296 /* We already did a pushtag. */;
13297 else if (TREE_CODE (decl) == FUNCTION_DECL
13298 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13299 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13300 {
13301 DECL_CONTEXT (decl) = NULL_TREE;
13302 pushdecl (decl);
13303 DECL_CONTEXT (decl) = current_function_decl;
13304 cp_check_omp_declare_reduction (decl);
13305 }
13306 else
13307 {
13308 int const_init = false;
13309 maybe_push_decl (decl);
13310 if (VAR_P (decl)
13311 && DECL_PRETTY_FUNCTION_P (decl))
13312 {
13313 /* For __PRETTY_FUNCTION__ we have to adjust the
13314 initializer. */
13315 const char *const name
13316 = cxx_printable_name (current_function_decl, 2);
13317 init = cp_fname_init (name, &TREE_TYPE (decl));
13318 }
13319 else
13320 {
13321 tree t = RECUR (init);
13322
13323 if (init && !t)
13324 {
13325 /* If we had an initializer but it
13326 instantiated to nothing,
13327 value-initialize the object. This will
13328 only occur when the initializer was a
13329 pack expansion where the parameter packs
13330 used in that expansion were of length
13331 zero. */
13332 init = build_value_init (TREE_TYPE (decl),
13333 complain);
13334 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13335 init = get_target_expr_sfinae (init, complain);
13336 }
13337 else
13338 init = t;
13339 }
13340
13341 if (VAR_P (decl))
13342 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13343 (pattern_decl));
13344 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13345 }
13346 }
13347 }
13348
13349 break;
13350 }
13351
13352 case FOR_STMT:
13353 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13354 RECUR (FOR_INIT_STMT (t));
13355 finish_for_init_stmt (stmt);
13356 tmp = RECUR (FOR_COND (t));
13357 finish_for_cond (tmp, stmt, false);
13358 tmp = RECUR (FOR_EXPR (t));
13359 finish_for_expr (tmp, stmt);
13360 RECUR (FOR_BODY (t));
13361 finish_for_stmt (stmt);
13362 break;
13363
13364 case RANGE_FOR_STMT:
13365 {
13366 tree decl, expr;
13367 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13368 decl = RANGE_FOR_DECL (t);
13369 decl = tsubst (decl, args, complain, in_decl);
13370 maybe_push_decl (decl);
13371 expr = RECUR (RANGE_FOR_EXPR (t));
13372 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13373 RECUR (RANGE_FOR_BODY (t));
13374 finish_for_stmt (stmt);
13375 }
13376 break;
13377
13378 case WHILE_STMT:
13379 stmt = begin_while_stmt ();
13380 tmp = RECUR (WHILE_COND (t));
13381 finish_while_stmt_cond (tmp, stmt, false);
13382 RECUR (WHILE_BODY (t));
13383 finish_while_stmt (stmt);
13384 break;
13385
13386 case DO_STMT:
13387 stmt = begin_do_stmt ();
13388 RECUR (DO_BODY (t));
13389 finish_do_body (stmt);
13390 tmp = RECUR (DO_COND (t));
13391 finish_do_stmt (tmp, stmt, false);
13392 break;
13393
13394 case IF_STMT:
13395 stmt = begin_if_stmt ();
13396 tmp = RECUR (IF_COND (t));
13397 finish_if_stmt_cond (tmp, stmt);
13398 RECUR (THEN_CLAUSE (t));
13399 finish_then_clause (stmt);
13400
13401 if (ELSE_CLAUSE (t))
13402 {
13403 begin_else_clause (stmt);
13404 RECUR (ELSE_CLAUSE (t));
13405 finish_else_clause (stmt);
13406 }
13407
13408 finish_if_stmt (stmt);
13409 break;
13410
13411 case BIND_EXPR:
13412 if (BIND_EXPR_BODY_BLOCK (t))
13413 stmt = begin_function_body ();
13414 else
13415 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13416 ? BCS_TRY_BLOCK : 0);
13417
13418 RECUR (BIND_EXPR_BODY (t));
13419
13420 if (BIND_EXPR_BODY_BLOCK (t))
13421 finish_function_body (stmt);
13422 else
13423 finish_compound_stmt (stmt);
13424 break;
13425
13426 case BREAK_STMT:
13427 finish_break_stmt ();
13428 break;
13429
13430 case CONTINUE_STMT:
13431 finish_continue_stmt ();
13432 break;
13433
13434 case SWITCH_STMT:
13435 stmt = begin_switch_stmt ();
13436 tmp = RECUR (SWITCH_STMT_COND (t));
13437 finish_switch_cond (tmp, stmt);
13438 RECUR (SWITCH_STMT_BODY (t));
13439 finish_switch_stmt (stmt);
13440 break;
13441
13442 case CASE_LABEL_EXPR:
13443 finish_case_label (EXPR_LOCATION (t),
13444 RECUR (CASE_LOW (t)),
13445 RECUR (CASE_HIGH (t)));
13446 break;
13447
13448 case LABEL_EXPR:
13449 {
13450 tree decl = LABEL_EXPR_LABEL (t);
13451 tree label;
13452
13453 label = finish_label_stmt (DECL_NAME (decl));
13454 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13455 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13456 }
13457 break;
13458
13459 case GOTO_EXPR:
13460 tmp = GOTO_DESTINATION (t);
13461 if (TREE_CODE (tmp) != LABEL_DECL)
13462 /* Computed goto's must be tsubst'd into. On the other hand,
13463 non-computed gotos must not be; the identifier in question
13464 will have no binding. */
13465 tmp = RECUR (tmp);
13466 else
13467 tmp = DECL_NAME (tmp);
13468 finish_goto_stmt (tmp);
13469 break;
13470
13471 case ASM_EXPR:
13472 tmp = finish_asm_stmt
13473 (ASM_VOLATILE_P (t),
13474 RECUR (ASM_STRING (t)),
13475 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13476 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13477 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13478 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13479 {
13480 tree asm_expr = tmp;
13481 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13482 asm_expr = TREE_OPERAND (asm_expr, 0);
13483 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13484 }
13485 break;
13486
13487 case TRY_BLOCK:
13488 if (CLEANUP_P (t))
13489 {
13490 stmt = begin_try_block ();
13491 RECUR (TRY_STMTS (t));
13492 finish_cleanup_try_block (stmt);
13493 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13494 }
13495 else
13496 {
13497 tree compound_stmt = NULL_TREE;
13498
13499 if (FN_TRY_BLOCK_P (t))
13500 stmt = begin_function_try_block (&compound_stmt);
13501 else
13502 stmt = begin_try_block ();
13503
13504 RECUR (TRY_STMTS (t));
13505
13506 if (FN_TRY_BLOCK_P (t))
13507 finish_function_try_block (stmt);
13508 else
13509 finish_try_block (stmt);
13510
13511 RECUR (TRY_HANDLERS (t));
13512 if (FN_TRY_BLOCK_P (t))
13513 finish_function_handler_sequence (stmt, compound_stmt);
13514 else
13515 finish_handler_sequence (stmt);
13516 }
13517 break;
13518
13519 case HANDLER:
13520 {
13521 tree decl = HANDLER_PARMS (t);
13522
13523 if (decl)
13524 {
13525 decl = tsubst (decl, args, complain, in_decl);
13526 /* Prevent instantiate_decl from trying to instantiate
13527 this variable. We've already done all that needs to be
13528 done. */
13529 if (decl != error_mark_node)
13530 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13531 }
13532 stmt = begin_handler ();
13533 finish_handler_parms (decl, stmt);
13534 RECUR (HANDLER_BODY (t));
13535 finish_handler (stmt);
13536 }
13537 break;
13538
13539 case TAG_DEFN:
13540 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13541 if (CLASS_TYPE_P (tmp))
13542 {
13543 /* Local classes are not independent templates; they are
13544 instantiated along with their containing function. And this
13545 way we don't have to deal with pushing out of one local class
13546 to instantiate a member of another local class. */
13547 tree fn;
13548 /* Closures are handled by the LAMBDA_EXPR. */
13549 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13550 complete_type (tmp);
13551 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13552 if (!DECL_ARTIFICIAL (fn))
13553 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13554 }
13555 break;
13556
13557 case STATIC_ASSERT:
13558 {
13559 tree condition;
13560
13561 ++c_inhibit_evaluation_warnings;
13562 condition =
13563 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13564 args,
13565 complain, in_decl,
13566 /*integral_constant_expression_p=*/true);
13567 --c_inhibit_evaluation_warnings;
13568
13569 finish_static_assert (condition,
13570 STATIC_ASSERT_MESSAGE (t),
13571 STATIC_ASSERT_SOURCE_LOCATION (t),
13572 /*member_p=*/false);
13573 }
13574 break;
13575
13576 case OMP_PARALLEL:
13577 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13578 args, complain, in_decl);
13579 stmt = begin_omp_parallel ();
13580 RECUR (OMP_PARALLEL_BODY (t));
13581 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13582 = OMP_PARALLEL_COMBINED (t);
13583 break;
13584
13585 case OMP_TASK:
13586 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13587 args, complain, in_decl);
13588 stmt = begin_omp_task ();
13589 RECUR (OMP_TASK_BODY (t));
13590 finish_omp_task (tmp, stmt);
13591 break;
13592
13593 case OMP_FOR:
13594 case OMP_SIMD:
13595 case CILK_SIMD:
13596 case OMP_DISTRIBUTE:
13597 {
13598 tree clauses, body, pre_body;
13599 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13600 tree incrv = NULL_TREE;
13601 int i;
13602
13603 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13604 args, complain, in_decl);
13605 if (OMP_FOR_INIT (t) != NULL_TREE)
13606 {
13607 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13608 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13609 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13610 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13611 }
13612
13613 stmt = begin_omp_structured_block ();
13614
13615 pre_body = push_stmt_list ();
13616 RECUR (OMP_FOR_PRE_BODY (t));
13617 pre_body = pop_stmt_list (pre_body);
13618
13619 if (OMP_FOR_INIT (t) != NULL_TREE)
13620 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13621 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13622 &clauses, args, complain, in_decl,
13623 integral_constant_expression_p);
13624
13625 body = push_stmt_list ();
13626 RECUR (OMP_FOR_BODY (t));
13627 body = pop_stmt_list (body);
13628
13629 if (OMP_FOR_INIT (t) != NULL_TREE)
13630 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13631 condv, incrv, body, pre_body, clauses);
13632 else
13633 {
13634 t = make_node (TREE_CODE (t));
13635 TREE_TYPE (t) = void_type_node;
13636 OMP_FOR_BODY (t) = body;
13637 OMP_FOR_PRE_BODY (t) = pre_body;
13638 OMP_FOR_CLAUSES (t) = clauses;
13639 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13640 add_stmt (t);
13641 }
13642
13643 add_stmt (finish_omp_structured_block (stmt));
13644 }
13645 break;
13646
13647 case OMP_SECTIONS:
13648 case OMP_SINGLE:
13649 case OMP_TEAMS:
13650 case OMP_TARGET_DATA:
13651 case OMP_TARGET:
13652 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13653 args, complain, in_decl);
13654 stmt = push_stmt_list ();
13655 RECUR (OMP_BODY (t));
13656 stmt = pop_stmt_list (stmt);
13657
13658 t = copy_node (t);
13659 OMP_BODY (t) = stmt;
13660 OMP_CLAUSES (t) = tmp;
13661 add_stmt (t);
13662 break;
13663
13664 case OMP_TARGET_UPDATE:
13665 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13666 args, complain, in_decl);
13667 t = copy_node (t);
13668 OMP_CLAUSES (t) = tmp;
13669 add_stmt (t);
13670 break;
13671
13672 case OMP_SECTION:
13673 case OMP_CRITICAL:
13674 case OMP_MASTER:
13675 case OMP_TASKGROUP:
13676 case OMP_ORDERED:
13677 stmt = push_stmt_list ();
13678 RECUR (OMP_BODY (t));
13679 stmt = pop_stmt_list (stmt);
13680
13681 t = copy_node (t);
13682 OMP_BODY (t) = stmt;
13683 add_stmt (t);
13684 break;
13685
13686 case OMP_ATOMIC:
13687 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13688 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13689 {
13690 tree op1 = TREE_OPERAND (t, 1);
13691 tree rhs1 = NULL_TREE;
13692 tree lhs, rhs;
13693 if (TREE_CODE (op1) == COMPOUND_EXPR)
13694 {
13695 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13696 op1 = TREE_OPERAND (op1, 1);
13697 }
13698 lhs = RECUR (TREE_OPERAND (op1, 0));
13699 rhs = RECUR (TREE_OPERAND (op1, 1));
13700 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13701 NULL_TREE, NULL_TREE, rhs1,
13702 OMP_ATOMIC_SEQ_CST (t));
13703 }
13704 else
13705 {
13706 tree op1 = TREE_OPERAND (t, 1);
13707 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13708 tree rhs1 = NULL_TREE;
13709 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13710 enum tree_code opcode = NOP_EXPR;
13711 if (code == OMP_ATOMIC_READ)
13712 {
13713 v = RECUR (TREE_OPERAND (op1, 0));
13714 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13715 }
13716 else if (code == OMP_ATOMIC_CAPTURE_OLD
13717 || code == OMP_ATOMIC_CAPTURE_NEW)
13718 {
13719 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13720 v = RECUR (TREE_OPERAND (op1, 0));
13721 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13722 if (TREE_CODE (op11) == COMPOUND_EXPR)
13723 {
13724 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13725 op11 = TREE_OPERAND (op11, 1);
13726 }
13727 lhs = RECUR (TREE_OPERAND (op11, 0));
13728 rhs = RECUR (TREE_OPERAND (op11, 1));
13729 opcode = TREE_CODE (op11);
13730 if (opcode == MODIFY_EXPR)
13731 opcode = NOP_EXPR;
13732 }
13733 else
13734 {
13735 code = OMP_ATOMIC;
13736 lhs = RECUR (TREE_OPERAND (op1, 0));
13737 rhs = RECUR (TREE_OPERAND (op1, 1));
13738 }
13739 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13740 OMP_ATOMIC_SEQ_CST (t));
13741 }
13742 break;
13743
13744 case TRANSACTION_EXPR:
13745 {
13746 int flags = 0;
13747 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13748 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13749
13750 if (TRANSACTION_EXPR_IS_STMT (t))
13751 {
13752 tree body = TRANSACTION_EXPR_BODY (t);
13753 tree noex = NULL_TREE;
13754 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13755 {
13756 noex = MUST_NOT_THROW_COND (body);
13757 if (noex == NULL_TREE)
13758 noex = boolean_true_node;
13759 body = TREE_OPERAND (body, 0);
13760 }
13761 stmt = begin_transaction_stmt (input_location, NULL, flags);
13762 RECUR (body);
13763 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13764 }
13765 else
13766 {
13767 stmt = build_transaction_expr (EXPR_LOCATION (t),
13768 RECUR (TRANSACTION_EXPR_BODY (t)),
13769 flags, NULL_TREE);
13770 RETURN (stmt);
13771 }
13772 }
13773 break;
13774
13775 case MUST_NOT_THROW_EXPR:
13776 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13777 RECUR (MUST_NOT_THROW_COND (t))));
13778
13779 case EXPR_PACK_EXPANSION:
13780 error ("invalid use of pack expansion expression");
13781 RETURN (error_mark_node);
13782
13783 case NONTYPE_ARGUMENT_PACK:
13784 error ("use %<...%> to expand argument pack");
13785 RETURN (error_mark_node);
13786
13787 case CILK_SPAWN_STMT:
13788 cfun->calls_cilk_spawn = 1;
13789 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
13790
13791 case CILK_SYNC_STMT:
13792 RETURN (build_cilk_sync ());
13793
13794 case COMPOUND_EXPR:
13795 tmp = RECUR (TREE_OPERAND (t, 0));
13796 if (tmp == NULL_TREE)
13797 /* If the first operand was a statement, we're done with it. */
13798 RETURN (RECUR (TREE_OPERAND (t, 1)));
13799 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13800 RECUR (TREE_OPERAND (t, 1)),
13801 complain));
13802
13803 default:
13804 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13805
13806 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13807 /*function_p=*/false,
13808 integral_constant_expression_p));
13809 }
13810
13811 RETURN (NULL_TREE);
13812 out:
13813 input_location = loc;
13814 return r;
13815 #undef RECUR
13816 #undef RETURN
13817 }
13818
13819 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13820 function. For description of the body see comment above
13821 cp_parser_omp_declare_reduction_exprs. */
13822
13823 static void
13824 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13825 {
13826 if (t == NULL_TREE || t == error_mark_node)
13827 return;
13828
13829 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13830
13831 tree_stmt_iterator tsi;
13832 int i;
13833 tree stmts[7];
13834 memset (stmts, 0, sizeof stmts);
13835 for (i = 0, tsi = tsi_start (t);
13836 i < 7 && !tsi_end_p (tsi);
13837 i++, tsi_next (&tsi))
13838 stmts[i] = tsi_stmt (tsi);
13839 gcc_assert (tsi_end_p (tsi));
13840
13841 if (i >= 3)
13842 {
13843 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13844 && TREE_CODE (stmts[1]) == DECL_EXPR);
13845 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13846 args, complain, in_decl);
13847 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13848 args, complain, in_decl);
13849 DECL_CONTEXT (omp_out) = current_function_decl;
13850 DECL_CONTEXT (omp_in) = current_function_decl;
13851 keep_next_level (true);
13852 tree block = begin_omp_structured_block ();
13853 tsubst_expr (stmts[2], args, complain, in_decl, false);
13854 block = finish_omp_structured_block (block);
13855 block = maybe_cleanup_point_expr_void (block);
13856 add_decl_expr (omp_out);
13857 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13858 TREE_NO_WARNING (omp_out) = 1;
13859 add_decl_expr (omp_in);
13860 finish_expr_stmt (block);
13861 }
13862 if (i >= 6)
13863 {
13864 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13865 && TREE_CODE (stmts[4]) == DECL_EXPR);
13866 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13867 args, complain, in_decl);
13868 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13869 args, complain, in_decl);
13870 DECL_CONTEXT (omp_priv) = current_function_decl;
13871 DECL_CONTEXT (omp_orig) = current_function_decl;
13872 keep_next_level (true);
13873 tree block = begin_omp_structured_block ();
13874 tsubst_expr (stmts[5], args, complain, in_decl, false);
13875 block = finish_omp_structured_block (block);
13876 block = maybe_cleanup_point_expr_void (block);
13877 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13878 add_decl_expr (omp_priv);
13879 add_decl_expr (omp_orig);
13880 finish_expr_stmt (block);
13881 if (i == 7)
13882 add_decl_expr (omp_orig);
13883 }
13884 }
13885
13886 /* T is a postfix-expression that is not being used in a function
13887 call. Return the substituted version of T. */
13888
13889 static tree
13890 tsubst_non_call_postfix_expression (tree t, tree args,
13891 tsubst_flags_t complain,
13892 tree in_decl)
13893 {
13894 if (TREE_CODE (t) == SCOPE_REF)
13895 t = tsubst_qualified_id (t, args, complain, in_decl,
13896 /*done=*/false, /*address_p=*/false);
13897 else
13898 t = tsubst_copy_and_build (t, args, complain, in_decl,
13899 /*function_p=*/false,
13900 /*integral_constant_expression_p=*/false);
13901
13902 return t;
13903 }
13904
13905 /* Like tsubst but deals with expressions and performs semantic
13906 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13907
13908 tree
13909 tsubst_copy_and_build (tree t,
13910 tree args,
13911 tsubst_flags_t complain,
13912 tree in_decl,
13913 bool function_p,
13914 bool integral_constant_expression_p)
13915 {
13916 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13917 #define RECUR(NODE) \
13918 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13919 /*function_p=*/false, \
13920 integral_constant_expression_p)
13921
13922 tree retval, op1;
13923 location_t loc;
13924
13925 if (t == NULL_TREE || t == error_mark_node)
13926 return t;
13927
13928 loc = input_location;
13929 if (EXPR_HAS_LOCATION (t))
13930 input_location = EXPR_LOCATION (t);
13931
13932 /* N3276 decltype magic only applies to calls at the top level or on the
13933 right side of a comma. */
13934 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13935 complain &= ~tf_decltype;
13936
13937 switch (TREE_CODE (t))
13938 {
13939 case USING_DECL:
13940 t = DECL_NAME (t);
13941 /* Fall through. */
13942 case IDENTIFIER_NODE:
13943 {
13944 tree decl;
13945 cp_id_kind idk;
13946 bool non_integral_constant_expression_p;
13947 const char *error_msg;
13948
13949 if (IDENTIFIER_TYPENAME_P (t))
13950 {
13951 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13952 t = mangle_conv_op_name_for_type (new_type);
13953 }
13954
13955 /* Look up the name. */
13956 decl = lookup_name (t);
13957
13958 /* By convention, expressions use ERROR_MARK_NODE to indicate
13959 failure, not NULL_TREE. */
13960 if (decl == NULL_TREE)
13961 decl = error_mark_node;
13962
13963 decl = finish_id_expression (t, decl, NULL_TREE,
13964 &idk,
13965 integral_constant_expression_p,
13966 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13967 &non_integral_constant_expression_p,
13968 /*template_p=*/false,
13969 /*done=*/true,
13970 /*address_p=*/false,
13971 /*template_arg_p=*/false,
13972 &error_msg,
13973 input_location);
13974 if (error_msg)
13975 error (error_msg);
13976 if (!function_p && identifier_p (decl))
13977 {
13978 if (complain & tf_error)
13979 unqualified_name_lookup_error (decl);
13980 decl = error_mark_node;
13981 }
13982 RETURN (decl);
13983 }
13984
13985 case TEMPLATE_ID_EXPR:
13986 {
13987 tree object;
13988 tree templ = RECUR (TREE_OPERAND (t, 0));
13989 tree targs = TREE_OPERAND (t, 1);
13990
13991 if (targs)
13992 targs = tsubst_template_args (targs, args, complain, in_decl);
13993
13994 if (TREE_CODE (templ) == COMPONENT_REF)
13995 {
13996 object = TREE_OPERAND (templ, 0);
13997 templ = TREE_OPERAND (templ, 1);
13998 }
13999 else
14000 object = NULL_TREE;
14001 templ = lookup_template_function (templ, targs);
14002
14003 if (object)
14004 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
14005 object, templ, NULL_TREE));
14006 else
14007 RETURN (baselink_for_fns (templ));
14008 }
14009
14010 case INDIRECT_REF:
14011 {
14012 tree r = RECUR (TREE_OPERAND (t, 0));
14013
14014 if (REFERENCE_REF_P (t))
14015 {
14016 /* A type conversion to reference type will be enclosed in
14017 such an indirect ref, but the substitution of the cast
14018 will have also added such an indirect ref. */
14019 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
14020 r = convert_from_reference (r);
14021 }
14022 else
14023 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
14024 complain|decltype_flag);
14025 RETURN (r);
14026 }
14027
14028 case NOP_EXPR:
14029 RETURN (build_nop
14030 (tsubst (TREE_TYPE (t), args, complain, in_decl),
14031 RECUR (TREE_OPERAND (t, 0))));
14032
14033 case IMPLICIT_CONV_EXPR:
14034 {
14035 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14036 tree expr = RECUR (TREE_OPERAND (t, 0));
14037 int flags = LOOKUP_IMPLICIT;
14038 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14039 flags = LOOKUP_NORMAL;
14040 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14041 flags));
14042 }
14043
14044 case CONVERT_EXPR:
14045 RETURN (build1
14046 (CONVERT_EXPR,
14047 tsubst (TREE_TYPE (t), args, complain, in_decl),
14048 RECUR (TREE_OPERAND (t, 0))));
14049
14050 case CAST_EXPR:
14051 case REINTERPRET_CAST_EXPR:
14052 case CONST_CAST_EXPR:
14053 case DYNAMIC_CAST_EXPR:
14054 case STATIC_CAST_EXPR:
14055 {
14056 tree type;
14057 tree op, r = NULL_TREE;
14058
14059 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14060 if (integral_constant_expression_p
14061 && !cast_valid_in_integral_constant_expression_p (type))
14062 {
14063 if (complain & tf_error)
14064 error ("a cast to a type other than an integral or "
14065 "enumeration type cannot appear in a constant-expression");
14066 RETURN (error_mark_node);
14067 }
14068
14069 op = RECUR (TREE_OPERAND (t, 0));
14070
14071 ++c_inhibit_evaluation_warnings;
14072 switch (TREE_CODE (t))
14073 {
14074 case CAST_EXPR:
14075 r = build_functional_cast (type, op, complain);
14076 break;
14077 case REINTERPRET_CAST_EXPR:
14078 r = build_reinterpret_cast (type, op, complain);
14079 break;
14080 case CONST_CAST_EXPR:
14081 r = build_const_cast (type, op, complain);
14082 break;
14083 case DYNAMIC_CAST_EXPR:
14084 r = build_dynamic_cast (type, op, complain);
14085 break;
14086 case STATIC_CAST_EXPR:
14087 r = build_static_cast (type, op, complain);
14088 break;
14089 default:
14090 gcc_unreachable ();
14091 }
14092 --c_inhibit_evaluation_warnings;
14093
14094 RETURN (r);
14095 }
14096
14097 case POSTDECREMENT_EXPR:
14098 case POSTINCREMENT_EXPR:
14099 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14100 args, complain, in_decl);
14101 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14102 complain|decltype_flag));
14103
14104 case PREDECREMENT_EXPR:
14105 case PREINCREMENT_EXPR:
14106 case NEGATE_EXPR:
14107 case BIT_NOT_EXPR:
14108 case ABS_EXPR:
14109 case TRUTH_NOT_EXPR:
14110 case UNARY_PLUS_EXPR: /* Unary + */
14111 case REALPART_EXPR:
14112 case IMAGPART_EXPR:
14113 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14114 RECUR (TREE_OPERAND (t, 0)),
14115 complain|decltype_flag));
14116
14117 case FIX_TRUNC_EXPR:
14118 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14119 0, complain));
14120
14121 case ADDR_EXPR:
14122 op1 = TREE_OPERAND (t, 0);
14123 if (TREE_CODE (op1) == LABEL_DECL)
14124 RETURN (finish_label_address_expr (DECL_NAME (op1),
14125 EXPR_LOCATION (op1)));
14126 if (TREE_CODE (op1) == SCOPE_REF)
14127 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14128 /*done=*/true, /*address_p=*/true);
14129 else
14130 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14131 in_decl);
14132 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14133 complain|decltype_flag));
14134
14135 case PLUS_EXPR:
14136 case MINUS_EXPR:
14137 case MULT_EXPR:
14138 case TRUNC_DIV_EXPR:
14139 case CEIL_DIV_EXPR:
14140 case FLOOR_DIV_EXPR:
14141 case ROUND_DIV_EXPR:
14142 case EXACT_DIV_EXPR:
14143 case BIT_AND_EXPR:
14144 case BIT_IOR_EXPR:
14145 case BIT_XOR_EXPR:
14146 case TRUNC_MOD_EXPR:
14147 case FLOOR_MOD_EXPR:
14148 case TRUTH_ANDIF_EXPR:
14149 case TRUTH_ORIF_EXPR:
14150 case TRUTH_AND_EXPR:
14151 case TRUTH_OR_EXPR:
14152 case RSHIFT_EXPR:
14153 case LSHIFT_EXPR:
14154 case RROTATE_EXPR:
14155 case LROTATE_EXPR:
14156 case EQ_EXPR:
14157 case NE_EXPR:
14158 case MAX_EXPR:
14159 case MIN_EXPR:
14160 case LE_EXPR:
14161 case GE_EXPR:
14162 case LT_EXPR:
14163 case GT_EXPR:
14164 case MEMBER_REF:
14165 case DOTSTAR_EXPR:
14166 {
14167 tree r;
14168
14169 ++c_inhibit_evaluation_warnings;
14170
14171 r = build_x_binary_op
14172 (input_location, TREE_CODE (t),
14173 RECUR (TREE_OPERAND (t, 0)),
14174 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14175 ? ERROR_MARK
14176 : TREE_CODE (TREE_OPERAND (t, 0))),
14177 RECUR (TREE_OPERAND (t, 1)),
14178 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14179 ? ERROR_MARK
14180 : TREE_CODE (TREE_OPERAND (t, 1))),
14181 /*overload=*/NULL,
14182 complain|decltype_flag);
14183 if (EXPR_P (r) && TREE_NO_WARNING (t))
14184 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14185
14186 --c_inhibit_evaluation_warnings;
14187
14188 RETURN (r);
14189 }
14190
14191 case POINTER_PLUS_EXPR:
14192 return fold_build_pointer_plus (RECUR (TREE_OPERAND (t, 0)),
14193 RECUR (TREE_OPERAND (t, 1)));
14194
14195 case SCOPE_REF:
14196 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14197 /*address_p=*/false));
14198 case ARRAY_REF:
14199 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14200 args, complain, in_decl);
14201 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14202 RECUR (TREE_OPERAND (t, 1)),
14203 complain|decltype_flag));
14204
14205 case ARRAY_NOTATION_REF:
14206 {
14207 tree start_index, length, stride;
14208 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14209 args, complain, in_decl);
14210 start_index = RECUR (ARRAY_NOTATION_START (t));
14211 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14212 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14213 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14214 length, stride, TREE_TYPE (op1)));
14215 }
14216 case SIZEOF_EXPR:
14217 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14218 RETURN (tsubst_copy (t, args, complain, in_decl));
14219 /* Fall through */
14220
14221 case ALIGNOF_EXPR:
14222 {
14223 tree r;
14224
14225 op1 = TREE_OPERAND (t, 0);
14226 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14227 op1 = TREE_TYPE (op1);
14228 if (!args)
14229 {
14230 /* When there are no ARGS, we are trying to evaluate a
14231 non-dependent expression from the parser. Trying to do
14232 the substitutions may not work. */
14233 if (!TYPE_P (op1))
14234 op1 = TREE_TYPE (op1);
14235 }
14236 else
14237 {
14238 ++cp_unevaluated_operand;
14239 ++c_inhibit_evaluation_warnings;
14240 if (TYPE_P (op1))
14241 op1 = tsubst (op1, args, complain, in_decl);
14242 else
14243 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14244 /*function_p=*/false,
14245 /*integral_constant_expression_p=*/
14246 false);
14247 --cp_unevaluated_operand;
14248 --c_inhibit_evaluation_warnings;
14249 }
14250 if (TYPE_P (op1))
14251 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14252 complain & tf_error);
14253 else
14254 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14255 complain & tf_error);
14256 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14257 {
14258 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14259 {
14260 if (!processing_template_decl && TYPE_P (op1))
14261 {
14262 r = build_min (SIZEOF_EXPR, size_type_node,
14263 build1 (NOP_EXPR, op1, error_mark_node));
14264 SIZEOF_EXPR_TYPE_P (r) = 1;
14265 }
14266 else
14267 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14268 TREE_SIDE_EFFECTS (r) = 0;
14269 TREE_READONLY (r) = 1;
14270 }
14271 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14272 }
14273 RETURN (r);
14274 }
14275
14276 case AT_ENCODE_EXPR:
14277 {
14278 op1 = TREE_OPERAND (t, 0);
14279 ++cp_unevaluated_operand;
14280 ++c_inhibit_evaluation_warnings;
14281 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14282 /*function_p=*/false,
14283 /*integral_constant_expression_p=*/false);
14284 --cp_unevaluated_operand;
14285 --c_inhibit_evaluation_warnings;
14286 RETURN (objc_build_encode_expr (op1));
14287 }
14288
14289 case NOEXCEPT_EXPR:
14290 op1 = TREE_OPERAND (t, 0);
14291 ++cp_unevaluated_operand;
14292 ++c_inhibit_evaluation_warnings;
14293 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14294 /*function_p=*/false,
14295 /*integral_constant_expression_p=*/false);
14296 --cp_unevaluated_operand;
14297 --c_inhibit_evaluation_warnings;
14298 RETURN (finish_noexcept_expr (op1, complain));
14299
14300 case MODOP_EXPR:
14301 {
14302 tree r;
14303
14304 ++c_inhibit_evaluation_warnings;
14305
14306 r = build_x_modify_expr
14307 (EXPR_LOCATION (t),
14308 RECUR (TREE_OPERAND (t, 0)),
14309 TREE_CODE (TREE_OPERAND (t, 1)),
14310 RECUR (TREE_OPERAND (t, 2)),
14311 complain|decltype_flag);
14312 /* TREE_NO_WARNING must be set if either the expression was
14313 parenthesized or it uses an operator such as >>= rather
14314 than plain assignment. In the former case, it was already
14315 set and must be copied. In the latter case,
14316 build_x_modify_expr sets it and it must not be reset
14317 here. */
14318 if (TREE_NO_WARNING (t))
14319 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14320
14321 --c_inhibit_evaluation_warnings;
14322
14323 RETURN (r);
14324 }
14325
14326 case ARROW_EXPR:
14327 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14328 args, complain, in_decl);
14329 /* Remember that there was a reference to this entity. */
14330 if (DECL_P (op1))
14331 mark_used (op1);
14332 RETURN (build_x_arrow (input_location, op1, complain));
14333
14334 case NEW_EXPR:
14335 {
14336 tree placement = RECUR (TREE_OPERAND (t, 0));
14337 tree init = RECUR (TREE_OPERAND (t, 3));
14338 vec<tree, va_gc> *placement_vec;
14339 vec<tree, va_gc> *init_vec;
14340 tree ret;
14341
14342 if (placement == NULL_TREE)
14343 placement_vec = NULL;
14344 else
14345 {
14346 placement_vec = make_tree_vector ();
14347 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14348 vec_safe_push (placement_vec, TREE_VALUE (placement));
14349 }
14350
14351 /* If there was an initializer in the original tree, but it
14352 instantiated to an empty list, then we should pass a
14353 non-NULL empty vector to tell build_new that it was an
14354 empty initializer() rather than no initializer. This can
14355 only happen when the initializer is a pack expansion whose
14356 parameter packs are of length zero. */
14357 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14358 init_vec = NULL;
14359 else
14360 {
14361 init_vec = make_tree_vector ();
14362 if (init == void_zero_node)
14363 gcc_assert (init_vec != NULL);
14364 else
14365 {
14366 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14367 vec_safe_push (init_vec, TREE_VALUE (init));
14368 }
14369 }
14370
14371 ret = build_new (&placement_vec,
14372 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14373 RECUR (TREE_OPERAND (t, 2)),
14374 &init_vec,
14375 NEW_EXPR_USE_GLOBAL (t),
14376 complain);
14377
14378 if (placement_vec != NULL)
14379 release_tree_vector (placement_vec);
14380 if (init_vec != NULL)
14381 release_tree_vector (init_vec);
14382
14383 RETURN (ret);
14384 }
14385
14386 case DELETE_EXPR:
14387 RETURN (delete_sanity
14388 (RECUR (TREE_OPERAND (t, 0)),
14389 RECUR (TREE_OPERAND (t, 1)),
14390 DELETE_EXPR_USE_VEC (t),
14391 DELETE_EXPR_USE_GLOBAL (t),
14392 complain));
14393
14394 case COMPOUND_EXPR:
14395 {
14396 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14397 complain & ~tf_decltype, in_decl,
14398 /*function_p=*/false,
14399 integral_constant_expression_p);
14400 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14401 op0,
14402 RECUR (TREE_OPERAND (t, 1)),
14403 complain|decltype_flag));
14404 }
14405
14406 case CALL_EXPR:
14407 {
14408 tree function;
14409 vec<tree, va_gc> *call_args;
14410 unsigned int nargs, i;
14411 bool qualified_p;
14412 bool koenig_p;
14413 tree ret;
14414
14415 function = CALL_EXPR_FN (t);
14416 /* When we parsed the expression, we determined whether or
14417 not Koenig lookup should be performed. */
14418 koenig_p = KOENIG_LOOKUP_P (t);
14419 if (TREE_CODE (function) == SCOPE_REF)
14420 {
14421 qualified_p = true;
14422 function = tsubst_qualified_id (function, args, complain, in_decl,
14423 /*done=*/false,
14424 /*address_p=*/false);
14425 }
14426 else if (koenig_p && identifier_p (function))
14427 {
14428 /* Do nothing; calling tsubst_copy_and_build on an identifier
14429 would incorrectly perform unqualified lookup again.
14430
14431 Note that we can also have an IDENTIFIER_NODE if the earlier
14432 unqualified lookup found a member function; in that case
14433 koenig_p will be false and we do want to do the lookup
14434 again to find the instantiated member function.
14435
14436 FIXME but doing that causes c++/15272, so we need to stop
14437 using IDENTIFIER_NODE in that situation. */
14438 qualified_p = false;
14439 }
14440 else
14441 {
14442 if (TREE_CODE (function) == COMPONENT_REF)
14443 {
14444 tree op = TREE_OPERAND (function, 1);
14445
14446 qualified_p = (TREE_CODE (op) == SCOPE_REF
14447 || (BASELINK_P (op)
14448 && BASELINK_QUALIFIED_P (op)));
14449 }
14450 else
14451 qualified_p = false;
14452
14453 if (TREE_CODE (function) == ADDR_EXPR
14454 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14455 /* Avoid error about taking the address of a constructor. */
14456 function = TREE_OPERAND (function, 0);
14457
14458 function = tsubst_copy_and_build (function, args, complain,
14459 in_decl,
14460 !qualified_p,
14461 integral_constant_expression_p);
14462
14463 if (BASELINK_P (function))
14464 qualified_p = true;
14465 }
14466
14467 nargs = call_expr_nargs (t);
14468 call_args = make_tree_vector ();
14469 for (i = 0; i < nargs; ++i)
14470 {
14471 tree arg = CALL_EXPR_ARG (t, i);
14472
14473 if (!PACK_EXPANSION_P (arg))
14474 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14475 else
14476 {
14477 /* Expand the pack expansion and push each entry onto
14478 CALL_ARGS. */
14479 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14480 if (TREE_CODE (arg) == TREE_VEC)
14481 {
14482 unsigned int len, j;
14483
14484 len = TREE_VEC_LENGTH (arg);
14485 for (j = 0; j < len; ++j)
14486 {
14487 tree value = TREE_VEC_ELT (arg, j);
14488 if (value != NULL_TREE)
14489 value = convert_from_reference (value);
14490 vec_safe_push (call_args, value);
14491 }
14492 }
14493 else
14494 {
14495 /* A partial substitution. Add one entry. */
14496 vec_safe_push (call_args, arg);
14497 }
14498 }
14499 }
14500
14501 /* We do not perform argument-dependent lookup if normal
14502 lookup finds a non-function, in accordance with the
14503 expected resolution of DR 218. */
14504 if (koenig_p
14505 && ((is_overloaded_fn (function)
14506 /* If lookup found a member function, the Koenig lookup is
14507 not appropriate, even if an unqualified-name was used
14508 to denote the function. */
14509 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14510 || identifier_p (function))
14511 /* Only do this when substitution turns a dependent call
14512 into a non-dependent call. */
14513 && type_dependent_expression_p_push (t)
14514 && !any_type_dependent_arguments_p (call_args))
14515 function = perform_koenig_lookup (function, call_args, tf_none);
14516
14517 if (identifier_p (function)
14518 && !any_type_dependent_arguments_p (call_args))
14519 {
14520 if (koenig_p && (complain & tf_warning_or_error))
14521 {
14522 /* For backwards compatibility and good diagnostics, try
14523 the unqualified lookup again if we aren't in SFINAE
14524 context. */
14525 tree unq = (tsubst_copy_and_build
14526 (function, args, complain, in_decl, true,
14527 integral_constant_expression_p));
14528 if (unq == error_mark_node)
14529 RETURN (error_mark_node);
14530
14531 if (unq != function)
14532 {
14533 tree fn = unq;
14534 if (INDIRECT_REF_P (fn))
14535 fn = TREE_OPERAND (fn, 0);
14536 if (TREE_CODE (fn) == COMPONENT_REF)
14537 fn = TREE_OPERAND (fn, 1);
14538 if (is_overloaded_fn (fn))
14539 fn = get_first_fn (fn);
14540 if (permerror (EXPR_LOC_OR_LOC (t, input_location),
14541 "%qD was not declared in this scope, "
14542 "and no declarations were found by "
14543 "argument-dependent lookup at the point "
14544 "of instantiation", function))
14545 {
14546 if (!DECL_P (fn))
14547 /* Can't say anything more. */;
14548 else if (DECL_CLASS_SCOPE_P (fn))
14549 {
14550 location_t loc = EXPR_LOC_OR_LOC (t,
14551 input_location);
14552 inform (loc,
14553 "declarations in dependent base %qT are "
14554 "not found by unqualified lookup",
14555 DECL_CLASS_CONTEXT (fn));
14556 if (current_class_ptr)
14557 inform (loc,
14558 "use %<this->%D%> instead", function);
14559 else
14560 inform (loc,
14561 "use %<%T::%D%> instead",
14562 current_class_name, function);
14563 }
14564 else
14565 inform (0, "%q+D declared here, later in the "
14566 "translation unit", fn);
14567 }
14568 function = unq;
14569 }
14570 }
14571 if (identifier_p (function))
14572 {
14573 if (complain & tf_error)
14574 unqualified_name_lookup_error (function);
14575 release_tree_vector (call_args);
14576 RETURN (error_mark_node);
14577 }
14578 }
14579
14580 /* Remember that there was a reference to this entity. */
14581 if (DECL_P (function))
14582 mark_used (function);
14583
14584 /* Put back tf_decltype for the actual call. */
14585 complain |= decltype_flag;
14586
14587 if (TREE_CODE (function) == OFFSET_REF)
14588 ret = build_offset_ref_call_from_tree (function, &call_args,
14589 complain);
14590 else if (TREE_CODE (function) == COMPONENT_REF)
14591 {
14592 tree instance = TREE_OPERAND (function, 0);
14593 tree fn = TREE_OPERAND (function, 1);
14594
14595 if (processing_template_decl
14596 && (type_dependent_expression_p (instance)
14597 || (!BASELINK_P (fn)
14598 && TREE_CODE (fn) != FIELD_DECL)
14599 || type_dependent_expression_p (fn)
14600 || any_type_dependent_arguments_p (call_args)))
14601 ret = build_nt_call_vec (function, call_args);
14602 else if (!BASELINK_P (fn))
14603 ret = finish_call_expr (function, &call_args,
14604 /*disallow_virtual=*/false,
14605 /*koenig_p=*/false,
14606 complain);
14607 else
14608 ret = (build_new_method_call
14609 (instance, fn,
14610 &call_args, NULL_TREE,
14611 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14612 /*fn_p=*/NULL,
14613 complain));
14614 }
14615 else
14616 ret = finish_call_expr (function, &call_args,
14617 /*disallow_virtual=*/qualified_p,
14618 koenig_p,
14619 complain);
14620
14621 release_tree_vector (call_args);
14622
14623 RETURN (ret);
14624 }
14625
14626 case COND_EXPR:
14627 {
14628 tree cond = RECUR (TREE_OPERAND (t, 0));
14629 tree exp1, exp2;
14630
14631 if (TREE_CODE (cond) == INTEGER_CST)
14632 {
14633 if (integer_zerop (cond))
14634 {
14635 ++c_inhibit_evaluation_warnings;
14636 exp1 = RECUR (TREE_OPERAND (t, 1));
14637 --c_inhibit_evaluation_warnings;
14638 exp2 = RECUR (TREE_OPERAND (t, 2));
14639 }
14640 else
14641 {
14642 exp1 = RECUR (TREE_OPERAND (t, 1));
14643 ++c_inhibit_evaluation_warnings;
14644 exp2 = RECUR (TREE_OPERAND (t, 2));
14645 --c_inhibit_evaluation_warnings;
14646 }
14647 }
14648 else
14649 {
14650 exp1 = RECUR (TREE_OPERAND (t, 1));
14651 exp2 = RECUR (TREE_OPERAND (t, 2));
14652 }
14653
14654 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14655 cond, exp1, exp2, complain));
14656 }
14657
14658 case PSEUDO_DTOR_EXPR:
14659 RETURN (finish_pseudo_destructor_expr
14660 (RECUR (TREE_OPERAND (t, 0)),
14661 RECUR (TREE_OPERAND (t, 1)),
14662 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14663 input_location));
14664
14665 case TREE_LIST:
14666 {
14667 tree purpose, value, chain;
14668
14669 if (t == void_list_node)
14670 RETURN (t);
14671
14672 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14673 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14674 {
14675 /* We have pack expansions, so expand those and
14676 create a new list out of it. */
14677 tree purposevec = NULL_TREE;
14678 tree valuevec = NULL_TREE;
14679 tree chain;
14680 int i, len = -1;
14681
14682 /* Expand the argument expressions. */
14683 if (TREE_PURPOSE (t))
14684 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14685 complain, in_decl);
14686 if (TREE_VALUE (t))
14687 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14688 complain, in_decl);
14689
14690 /* Build the rest of the list. */
14691 chain = TREE_CHAIN (t);
14692 if (chain && chain != void_type_node)
14693 chain = RECUR (chain);
14694
14695 /* Determine the number of arguments. */
14696 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14697 {
14698 len = TREE_VEC_LENGTH (purposevec);
14699 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14700 }
14701 else if (TREE_CODE (valuevec) == TREE_VEC)
14702 len = TREE_VEC_LENGTH (valuevec);
14703 else
14704 {
14705 /* Since we only performed a partial substitution into
14706 the argument pack, we only RETURN (a single list
14707 node. */
14708 if (purposevec == TREE_PURPOSE (t)
14709 && valuevec == TREE_VALUE (t)
14710 && chain == TREE_CHAIN (t))
14711 RETURN (t);
14712
14713 RETURN (tree_cons (purposevec, valuevec, chain));
14714 }
14715
14716 /* Convert the argument vectors into a TREE_LIST */
14717 i = len;
14718 while (i > 0)
14719 {
14720 /* Grab the Ith values. */
14721 i--;
14722 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14723 : NULL_TREE;
14724 value
14725 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14726 : NULL_TREE;
14727
14728 /* Build the list (backwards). */
14729 chain = tree_cons (purpose, value, chain);
14730 }
14731
14732 RETURN (chain);
14733 }
14734
14735 purpose = TREE_PURPOSE (t);
14736 if (purpose)
14737 purpose = RECUR (purpose);
14738 value = TREE_VALUE (t);
14739 if (value)
14740 value = RECUR (value);
14741 chain = TREE_CHAIN (t);
14742 if (chain && chain != void_type_node)
14743 chain = RECUR (chain);
14744 if (purpose == TREE_PURPOSE (t)
14745 && value == TREE_VALUE (t)
14746 && chain == TREE_CHAIN (t))
14747 RETURN (t);
14748 RETURN (tree_cons (purpose, value, chain));
14749 }
14750
14751 case COMPONENT_REF:
14752 {
14753 tree object;
14754 tree object_type;
14755 tree member;
14756
14757 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14758 args, complain, in_decl);
14759 /* Remember that there was a reference to this entity. */
14760 if (DECL_P (object))
14761 mark_used (object);
14762 object_type = TREE_TYPE (object);
14763
14764 member = TREE_OPERAND (t, 1);
14765 if (BASELINK_P (member))
14766 member = tsubst_baselink (member,
14767 non_reference (TREE_TYPE (object)),
14768 args, complain, in_decl);
14769 else
14770 member = tsubst_copy (member, args, complain, in_decl);
14771 if (member == error_mark_node)
14772 RETURN (error_mark_node);
14773
14774 if (type_dependent_expression_p (object))
14775 /* We can't do much here. */;
14776 else if (!CLASS_TYPE_P (object_type))
14777 {
14778 if (scalarish_type_p (object_type))
14779 {
14780 tree s = NULL_TREE;
14781 tree dtor = member;
14782
14783 if (TREE_CODE (dtor) == SCOPE_REF)
14784 {
14785 s = TREE_OPERAND (dtor, 0);
14786 dtor = TREE_OPERAND (dtor, 1);
14787 }
14788 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14789 {
14790 dtor = TREE_OPERAND (dtor, 0);
14791 if (TYPE_P (dtor))
14792 RETURN (finish_pseudo_destructor_expr
14793 (object, s, dtor, input_location));
14794 }
14795 }
14796 }
14797 else if (TREE_CODE (member) == SCOPE_REF
14798 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14799 {
14800 /* Lookup the template functions now that we know what the
14801 scope is. */
14802 tree scope = TREE_OPERAND (member, 0);
14803 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14804 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14805 member = lookup_qualified_name (scope, tmpl,
14806 /*is_type_p=*/false,
14807 /*complain=*/false);
14808 if (BASELINK_P (member))
14809 {
14810 BASELINK_FUNCTIONS (member)
14811 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14812 args);
14813 member = (adjust_result_of_qualified_name_lookup
14814 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14815 object_type));
14816 }
14817 else
14818 {
14819 qualified_name_lookup_error (scope, tmpl, member,
14820 input_location);
14821 RETURN (error_mark_node);
14822 }
14823 }
14824 else if (TREE_CODE (member) == SCOPE_REF
14825 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14826 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14827 {
14828 if (complain & tf_error)
14829 {
14830 if (TYPE_P (TREE_OPERAND (member, 0)))
14831 error ("%qT is not a class or namespace",
14832 TREE_OPERAND (member, 0));
14833 else
14834 error ("%qD is not a class or namespace",
14835 TREE_OPERAND (member, 0));
14836 }
14837 RETURN (error_mark_node);
14838 }
14839 else if (TREE_CODE (member) == FIELD_DECL)
14840 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14841
14842 RETURN (finish_class_member_access_expr (object, member,
14843 /*template_p=*/false,
14844 complain));
14845 }
14846
14847 case THROW_EXPR:
14848 RETURN (build_throw
14849 (RECUR (TREE_OPERAND (t, 0))));
14850
14851 case CONSTRUCTOR:
14852 {
14853 vec<constructor_elt, va_gc> *n;
14854 constructor_elt *ce;
14855 unsigned HOST_WIDE_INT idx;
14856 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14857 bool process_index_p;
14858 int newlen;
14859 bool need_copy_p = false;
14860 tree r;
14861
14862 if (type == error_mark_node)
14863 RETURN (error_mark_node);
14864
14865 /* digest_init will do the wrong thing if we let it. */
14866 if (type && TYPE_PTRMEMFUNC_P (type))
14867 RETURN (t);
14868
14869 /* We do not want to process the index of aggregate
14870 initializers as they are identifier nodes which will be
14871 looked up by digest_init. */
14872 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14873
14874 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14875 newlen = vec_safe_length (n);
14876 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14877 {
14878 if (ce->index && process_index_p
14879 /* An identifier index is looked up in the type
14880 being initialized, not the current scope. */
14881 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14882 ce->index = RECUR (ce->index);
14883
14884 if (PACK_EXPANSION_P (ce->value))
14885 {
14886 /* Substitute into the pack expansion. */
14887 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14888 in_decl);
14889
14890 if (ce->value == error_mark_node
14891 || PACK_EXPANSION_P (ce->value))
14892 ;
14893 else if (TREE_VEC_LENGTH (ce->value) == 1)
14894 /* Just move the argument into place. */
14895 ce->value = TREE_VEC_ELT (ce->value, 0);
14896 else
14897 {
14898 /* Update the length of the final CONSTRUCTOR
14899 arguments vector, and note that we will need to
14900 copy.*/
14901 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14902 need_copy_p = true;
14903 }
14904 }
14905 else
14906 ce->value = RECUR (ce->value);
14907 }
14908
14909 if (need_copy_p)
14910 {
14911 vec<constructor_elt, va_gc> *old_n = n;
14912
14913 vec_alloc (n, newlen);
14914 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14915 {
14916 if (TREE_CODE (ce->value) == TREE_VEC)
14917 {
14918 int i, len = TREE_VEC_LENGTH (ce->value);
14919 for (i = 0; i < len; ++i)
14920 CONSTRUCTOR_APPEND_ELT (n, 0,
14921 TREE_VEC_ELT (ce->value, i));
14922 }
14923 else
14924 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14925 }
14926 }
14927
14928 r = build_constructor (init_list_type_node, n);
14929 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14930
14931 if (TREE_HAS_CONSTRUCTOR (t))
14932 RETURN (finish_compound_literal (type, r, complain));
14933
14934 TREE_TYPE (r) = type;
14935 RETURN (r);
14936 }
14937
14938 case TYPEID_EXPR:
14939 {
14940 tree operand_0 = TREE_OPERAND (t, 0);
14941 if (TYPE_P (operand_0))
14942 {
14943 operand_0 = tsubst (operand_0, args, complain, in_decl);
14944 RETURN (get_typeid (operand_0, complain));
14945 }
14946 else
14947 {
14948 operand_0 = RECUR (operand_0);
14949 RETURN (build_typeid (operand_0, complain));
14950 }
14951 }
14952
14953 case VAR_DECL:
14954 if (!args)
14955 RETURN (t);
14956 else if (DECL_PACK_P (t))
14957 {
14958 /* We don't build decls for an instantiation of a
14959 variadic capture proxy, we instantiate the elements
14960 when needed. */
14961 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14962 return RECUR (DECL_VALUE_EXPR (t));
14963 }
14964 /* Fall through */
14965
14966 case PARM_DECL:
14967 {
14968 tree r = tsubst_copy (t, args, complain, in_decl);
14969
14970 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14971 /* If the original type was a reference, we'll be wrapped in
14972 the appropriate INDIRECT_REF. */
14973 r = convert_from_reference (r);
14974 RETURN (r);
14975 }
14976
14977 case VA_ARG_EXPR:
14978 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14979 RECUR (TREE_OPERAND (t, 0)),
14980 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14981
14982 case OFFSETOF_EXPR:
14983 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14984
14985 case TRAIT_EXPR:
14986 {
14987 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
14988 complain, in_decl);
14989
14990 tree type2 = TRAIT_EXPR_TYPE2 (t);
14991 if (type2)
14992 type2 = tsubst (type2, args, complain, in_decl);
14993
14994 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14995 }
14996
14997 case STMT_EXPR:
14998 {
14999 tree old_stmt_expr = cur_stmt_expr;
15000 tree stmt_expr = begin_stmt_expr ();
15001
15002 cur_stmt_expr = stmt_expr;
15003 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
15004 integral_constant_expression_p);
15005 stmt_expr = finish_stmt_expr (stmt_expr, false);
15006 cur_stmt_expr = old_stmt_expr;
15007
15008 /* If the resulting list of expression statement is empty,
15009 fold it further into void_zero_node. */
15010 if (empty_expr_stmt_p (stmt_expr))
15011 stmt_expr = void_zero_node;
15012
15013 RETURN (stmt_expr);
15014 }
15015
15016 case LAMBDA_EXPR:
15017 {
15018 tree r = build_lambda_expr ();
15019
15020 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
15021 LAMBDA_EXPR_CLOSURE (r) = type;
15022 CLASSTYPE_LAMBDA_EXPR (type) = r;
15023
15024 LAMBDA_EXPR_LOCATION (r)
15025 = LAMBDA_EXPR_LOCATION (t);
15026 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
15027 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
15028 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
15029 LAMBDA_EXPR_DISCRIMINATOR (r)
15030 = (LAMBDA_EXPR_DISCRIMINATOR (t));
15031 /* For a function scope, we want to use tsubst so that we don't
15032 complain about referring to an auto function before its return
15033 type has been deduced. Otherwise, we want to use tsubst_copy so
15034 that we look up the existing field/parameter/variable rather
15035 than build a new one. */
15036 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
15037 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15038 scope = tsubst (scope, args, complain, in_decl);
15039 else if (scope && TREE_CODE (scope) == PARM_DECL)
15040 {
15041 /* Look up the parameter we want directly, as tsubst_copy
15042 doesn't do what we need. */
15043 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15044 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15045 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15046 parm = DECL_CHAIN (parm);
15047 scope = parm;
15048 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15049 if (DECL_CONTEXT (scope) == NULL_TREE)
15050 DECL_CONTEXT (scope) = fn;
15051 }
15052 else
15053 scope = RECUR (scope);
15054 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15055 LAMBDA_EXPR_RETURN_TYPE (r)
15056 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15057
15058 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15059 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15060
15061 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15062 determine_visibility (TYPE_NAME (type));
15063 /* Now that we know visibility, instantiate the type so we have a
15064 declaration of the op() for later calls to lambda_function. */
15065 complete_type (type);
15066
15067 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15068
15069 RETURN (build_lambda_object (r));
15070 }
15071
15072 case TARGET_EXPR:
15073 /* We can get here for a constant initializer of non-dependent type.
15074 FIXME stop folding in cp_parser_initializer_clause. */
15075 {
15076 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15077 complain);
15078 RETURN (r);
15079 }
15080
15081 case TRANSACTION_EXPR:
15082 RETURN (tsubst_expr(t, args, complain, in_decl,
15083 integral_constant_expression_p));
15084
15085 case PAREN_EXPR:
15086 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15087
15088 case VEC_PERM_EXPR:
15089 RETURN (build_x_vec_perm_expr (input_location,
15090 RECUR (TREE_OPERAND (t, 0)),
15091 RECUR (TREE_OPERAND (t, 1)),
15092 RECUR (TREE_OPERAND (t, 2)),
15093 complain));
15094
15095 default:
15096 /* Handle Objective-C++ constructs, if appropriate. */
15097 {
15098 tree subst
15099 = objcp_tsubst_copy_and_build (t, args, complain,
15100 in_decl, /*function_p=*/false);
15101 if (subst)
15102 RETURN (subst);
15103 }
15104 RETURN (tsubst_copy (t, args, complain, in_decl));
15105 }
15106
15107 #undef RECUR
15108 #undef RETURN
15109 out:
15110 input_location = loc;
15111 return retval;
15112 }
15113
15114 /* Verify that the instantiated ARGS are valid. For type arguments,
15115 make sure that the type's linkage is ok. For non-type arguments,
15116 make sure they are constants if they are integral or enumerations.
15117 Emit an error under control of COMPLAIN, and return TRUE on error. */
15118
15119 static bool
15120 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15121 {
15122 if (dependent_template_arg_p (t))
15123 return false;
15124 if (ARGUMENT_PACK_P (t))
15125 {
15126 tree vec = ARGUMENT_PACK_ARGS (t);
15127 int len = TREE_VEC_LENGTH (vec);
15128 bool result = false;
15129 int i;
15130
15131 for (i = 0; i < len; ++i)
15132 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15133 result = true;
15134 return result;
15135 }
15136 else if (TYPE_P (t))
15137 {
15138 /* [basic.link]: A name with no linkage (notably, the name
15139 of a class or enumeration declared in a local scope)
15140 shall not be used to declare an entity with linkage.
15141 This implies that names with no linkage cannot be used as
15142 template arguments
15143
15144 DR 757 relaxes this restriction for C++0x. */
15145 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15146 : no_linkage_check (t, /*relaxed_p=*/false));
15147
15148 if (nt)
15149 {
15150 /* DR 488 makes use of a type with no linkage cause
15151 type deduction to fail. */
15152 if (complain & tf_error)
15153 {
15154 if (TYPE_ANONYMOUS_P (nt))
15155 error ("%qT is/uses anonymous type", t);
15156 else
15157 error ("template argument for %qD uses local type %qT",
15158 tmpl, t);
15159 }
15160 return true;
15161 }
15162 /* In order to avoid all sorts of complications, we do not
15163 allow variably-modified types as template arguments. */
15164 else if (variably_modified_type_p (t, NULL_TREE))
15165 {
15166 if (complain & tf_error)
15167 error ("%qT is a variably modified type", t);
15168 return true;
15169 }
15170 }
15171 /* Class template and alias template arguments should be OK. */
15172 else if (DECL_TYPE_TEMPLATE_P (t))
15173 ;
15174 /* A non-type argument of integral or enumerated type must be a
15175 constant. */
15176 else if (TREE_TYPE (t)
15177 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15178 && !TREE_CONSTANT (t))
15179 {
15180 if (complain & tf_error)
15181 error ("integral expression %qE is not constant", t);
15182 return true;
15183 }
15184 return false;
15185 }
15186
15187 static bool
15188 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15189 {
15190 int ix, len = DECL_NTPARMS (tmpl);
15191 bool result = false;
15192
15193 for (ix = 0; ix != len; ix++)
15194 {
15195 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15196 result = true;
15197 }
15198 if (result && (complain & tf_error))
15199 error (" trying to instantiate %qD", tmpl);
15200 return result;
15201 }
15202
15203 /* We're out of SFINAE context now, so generate diagnostics for the access
15204 errors we saw earlier when instantiating D from TMPL and ARGS. */
15205
15206 static void
15207 recheck_decl_substitution (tree d, tree tmpl, tree args)
15208 {
15209 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15210 tree type = TREE_TYPE (pattern);
15211 location_t loc = input_location;
15212
15213 push_access_scope (d);
15214 push_deferring_access_checks (dk_no_deferred);
15215 input_location = DECL_SOURCE_LOCATION (pattern);
15216 tsubst (type, args, tf_warning_or_error, d);
15217 input_location = loc;
15218 pop_deferring_access_checks ();
15219 pop_access_scope (d);
15220 }
15221
15222 /* Instantiate the indicated variable, function, or alias template TMPL with
15223 the template arguments in TARG_PTR. */
15224
15225 static tree
15226 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15227 {
15228 tree targ_ptr = orig_args;
15229 tree fndecl;
15230 tree gen_tmpl;
15231 tree spec;
15232 bool access_ok = true;
15233
15234 if (tmpl == error_mark_node)
15235 return error_mark_node;
15236
15237 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15238
15239 /* If this function is a clone, handle it specially. */
15240 if (DECL_CLONED_FUNCTION_P (tmpl))
15241 {
15242 tree spec;
15243 tree clone;
15244
15245 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15246 DECL_CLONED_FUNCTION. */
15247 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15248 targ_ptr, complain);
15249 if (spec == error_mark_node)
15250 return error_mark_node;
15251
15252 /* Look for the clone. */
15253 FOR_EACH_CLONE (clone, spec)
15254 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15255 return clone;
15256 /* We should always have found the clone by now. */
15257 gcc_unreachable ();
15258 return NULL_TREE;
15259 }
15260
15261 if (targ_ptr == error_mark_node)
15262 return error_mark_node;
15263
15264 /* Check to see if we already have this specialization. */
15265 gen_tmpl = most_general_template (tmpl);
15266 if (tmpl != gen_tmpl)
15267 /* The TMPL is a partial instantiation. To get a full set of
15268 arguments we must add the arguments used to perform the
15269 partial instantiation. */
15270 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15271 targ_ptr);
15272
15273 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15274 but it doesn't seem to be on the hot path. */
15275 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15276
15277 gcc_assert (tmpl == gen_tmpl
15278 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15279 == spec)
15280 || fndecl == NULL_TREE);
15281
15282 if (spec != NULL_TREE)
15283 {
15284 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15285 {
15286 if (complain & tf_error)
15287 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15288 return error_mark_node;
15289 }
15290 return spec;
15291 }
15292
15293 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15294 complain))
15295 return error_mark_node;
15296
15297 /* We are building a FUNCTION_DECL, during which the access of its
15298 parameters and return types have to be checked. However this
15299 FUNCTION_DECL which is the desired context for access checking
15300 is not built yet. We solve this chicken-and-egg problem by
15301 deferring all checks until we have the FUNCTION_DECL. */
15302 push_deferring_access_checks (dk_deferred);
15303
15304 /* Instantiation of the function happens in the context of the function
15305 template, not the context of the overload resolution we're doing. */
15306 push_to_top_level ();
15307 /* If there are dependent arguments, e.g. because we're doing partial
15308 ordering, make sure processing_template_decl stays set. */
15309 if (uses_template_parms (targ_ptr))
15310 ++processing_template_decl;
15311 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15312 {
15313 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15314 complain, gen_tmpl);
15315 push_nested_class (ctx);
15316 }
15317 /* Substitute template parameters to obtain the specialization. */
15318 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15319 targ_ptr, complain, gen_tmpl);
15320 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15321 pop_nested_class ();
15322 pop_from_top_level ();
15323
15324 if (fndecl == error_mark_node)
15325 {
15326 pop_deferring_access_checks ();
15327 return error_mark_node;
15328 }
15329
15330 /* The DECL_TI_TEMPLATE should always be the immediate parent
15331 template, not the most general template. */
15332 DECL_TI_TEMPLATE (fndecl) = tmpl;
15333
15334 /* Now we know the specialization, compute access previously
15335 deferred. */
15336 push_access_scope (fndecl);
15337 if (!perform_deferred_access_checks (complain))
15338 access_ok = false;
15339 pop_access_scope (fndecl);
15340 pop_deferring_access_checks ();
15341
15342 /* If we've just instantiated the main entry point for a function,
15343 instantiate all the alternate entry points as well. We do this
15344 by cloning the instantiation of the main entry point, not by
15345 instantiating the template clones. */
15346 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15347 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15348
15349 if (!access_ok)
15350 {
15351 if (!(complain & tf_error))
15352 {
15353 /* Remember to reinstantiate when we're out of SFINAE so the user
15354 can see the errors. */
15355 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15356 }
15357 return error_mark_node;
15358 }
15359 return fndecl;
15360 }
15361
15362 /* Wrapper for instantiate_template_1. */
15363
15364 tree
15365 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15366 {
15367 tree ret;
15368 timevar_push (TV_TEMPLATE_INST);
15369 ret = instantiate_template_1 (tmpl, orig_args, complain);
15370 timevar_pop (TV_TEMPLATE_INST);
15371 return ret;
15372 }
15373
15374 /* Instantiate the alias template TMPL with ARGS. Also push a template
15375 instantiation level, which instantiate_template doesn't do because
15376 functions and variables have sufficient context established by the
15377 callers. */
15378
15379 static tree
15380 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15381 {
15382 struct pending_template *old_last_pend = last_pending_template;
15383 struct tinst_level *old_error_tinst = last_error_tinst_level;
15384 if (tmpl == error_mark_node || args == error_mark_node)
15385 return error_mark_node;
15386 tree tinst = build_tree_list (tmpl, args);
15387 if (!push_tinst_level (tinst))
15388 {
15389 ggc_free (tinst);
15390 return error_mark_node;
15391 }
15392
15393 args =
15394 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15395 args, tmpl, complain,
15396 /*require_all_args=*/true,
15397 /*use_default_args=*/true);
15398
15399 tree r = instantiate_template (tmpl, args, complain);
15400 pop_tinst_level ();
15401 /* We can't free this if a pending_template entry or last_error_tinst_level
15402 is pointing at it. */
15403 if (last_pending_template == old_last_pend
15404 && last_error_tinst_level == old_error_tinst)
15405 ggc_free (tinst);
15406
15407 return r;
15408 }
15409
15410 /* PARM is a template parameter pack for FN. Returns true iff
15411 PARM is used in a deducible way in the argument list of FN. */
15412
15413 static bool
15414 pack_deducible_p (tree parm, tree fn)
15415 {
15416 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15417 for (; t; t = TREE_CHAIN (t))
15418 {
15419 tree type = TREE_VALUE (t);
15420 tree packs;
15421 if (!PACK_EXPANSION_P (type))
15422 continue;
15423 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15424 packs; packs = TREE_CHAIN (packs))
15425 if (TREE_VALUE (packs) == parm)
15426 {
15427 /* The template parameter pack is used in a function parameter
15428 pack. If this is the end of the parameter list, the
15429 template parameter pack is deducible. */
15430 if (TREE_CHAIN (t) == void_list_node)
15431 return true;
15432 else
15433 /* Otherwise, not. Well, it could be deduced from
15434 a non-pack parameter, but doing so would end up with
15435 a deduction mismatch, so don't bother. */
15436 return false;
15437 }
15438 }
15439 /* The template parameter pack isn't used in any function parameter
15440 packs, but it might be used deeper, e.g. tuple<Args...>. */
15441 return true;
15442 }
15443
15444 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15445 NARGS elements of the arguments that are being used when calling
15446 it. TARGS is a vector into which the deduced template arguments
15447 are placed.
15448
15449 Returns either a FUNCTION_DECL for the matching specialization of FN or
15450 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
15451 true, diagnostics will be printed to explain why it failed.
15452
15453 If FN is a conversion operator, or we are trying to produce a specific
15454 specialization, RETURN_TYPE is the return type desired.
15455
15456 The EXPLICIT_TARGS are explicit template arguments provided via a
15457 template-id.
15458
15459 The parameter STRICT is one of:
15460
15461 DEDUCE_CALL:
15462 We are deducing arguments for a function call, as in
15463 [temp.deduct.call].
15464
15465 DEDUCE_CONV:
15466 We are deducing arguments for a conversion function, as in
15467 [temp.deduct.conv].
15468
15469 DEDUCE_EXACT:
15470 We are deducing arguments when doing an explicit instantiation
15471 as in [temp.explicit], when determining an explicit specialization
15472 as in [temp.expl.spec], or when taking the address of a function
15473 template, as in [temp.deduct.funcaddr]. */
15474
15475 tree
15476 fn_type_unification (tree fn,
15477 tree explicit_targs,
15478 tree targs,
15479 const tree *args,
15480 unsigned int nargs,
15481 tree return_type,
15482 unification_kind_t strict,
15483 int flags,
15484 bool explain_p,
15485 bool decltype_p)
15486 {
15487 tree parms;
15488 tree fntype;
15489 tree decl = NULL_TREE;
15490 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15491 bool ok;
15492 static int deduction_depth;
15493 struct pending_template *old_last_pend = last_pending_template;
15494 struct tinst_level *old_error_tinst = last_error_tinst_level;
15495 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15496 tree tinst;
15497 tree r = error_mark_node;
15498
15499 if (decltype_p)
15500 complain |= tf_decltype;
15501
15502 /* In C++0x, it's possible to have a function template whose type depends
15503 on itself recursively. This is most obvious with decltype, but can also
15504 occur with enumeration scope (c++/48969). So we need to catch infinite
15505 recursion and reject the substitution at deduction time; this function
15506 will return error_mark_node for any repeated substitution.
15507
15508 This also catches excessive recursion such as when f<N> depends on
15509 f<N-1> across all integers, and returns error_mark_node for all the
15510 substitutions back up to the initial one.
15511
15512 This is, of course, not reentrant. */
15513 if (excessive_deduction_depth)
15514 return error_mark_node;
15515 tinst = build_tree_list (fn, NULL_TREE);
15516 ++deduction_depth;
15517
15518 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15519
15520 fntype = TREE_TYPE (fn);
15521 if (explicit_targs)
15522 {
15523 /* [temp.deduct]
15524
15525 The specified template arguments must match the template
15526 parameters in kind (i.e., type, nontype, template), and there
15527 must not be more arguments than there are parameters;
15528 otherwise type deduction fails.
15529
15530 Nontype arguments must match the types of the corresponding
15531 nontype template parameters, or must be convertible to the
15532 types of the corresponding nontype parameters as specified in
15533 _temp.arg.nontype_, otherwise type deduction fails.
15534
15535 All references in the function type of the function template
15536 to the corresponding template parameters are replaced by the
15537 specified template argument values. If a substitution in a
15538 template parameter or in the function type of the function
15539 template results in an invalid type, type deduction fails. */
15540 int i, len = TREE_VEC_LENGTH (tparms);
15541 location_t loc = input_location;
15542 bool incomplete = false;
15543
15544 /* Adjust any explicit template arguments before entering the
15545 substitution context. */
15546 explicit_targs
15547 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15548 complain,
15549 /*require_all_args=*/false,
15550 /*use_default_args=*/false));
15551 if (explicit_targs == error_mark_node)
15552 goto fail;
15553
15554 /* Substitute the explicit args into the function type. This is
15555 necessary so that, for instance, explicitly declared function
15556 arguments can match null pointed constants. If we were given
15557 an incomplete set of explicit args, we must not do semantic
15558 processing during substitution as we could create partial
15559 instantiations. */
15560 for (i = 0; i < len; i++)
15561 {
15562 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15563 bool parameter_pack = false;
15564 tree targ = TREE_VEC_ELT (explicit_targs, i);
15565
15566 /* Dig out the actual parm. */
15567 if (TREE_CODE (parm) == TYPE_DECL
15568 || TREE_CODE (parm) == TEMPLATE_DECL)
15569 {
15570 parm = TREE_TYPE (parm);
15571 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15572 }
15573 else if (TREE_CODE (parm) == PARM_DECL)
15574 {
15575 parm = DECL_INITIAL (parm);
15576 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15577 }
15578
15579 if (!parameter_pack && targ == NULL_TREE)
15580 /* No explicit argument for this template parameter. */
15581 incomplete = true;
15582
15583 if (parameter_pack && pack_deducible_p (parm, fn))
15584 {
15585 /* Mark the argument pack as "incomplete". We could
15586 still deduce more arguments during unification.
15587 We remove this mark in type_unification_real. */
15588 if (targ)
15589 {
15590 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15591 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15592 = ARGUMENT_PACK_ARGS (targ);
15593 }
15594
15595 /* We have some incomplete argument packs. */
15596 incomplete = true;
15597 }
15598 }
15599
15600 TREE_VALUE (tinst) = explicit_targs;
15601 if (!push_tinst_level (tinst))
15602 {
15603 excessive_deduction_depth = true;
15604 goto fail;
15605 }
15606 processing_template_decl += incomplete;
15607 input_location = DECL_SOURCE_LOCATION (fn);
15608 /* Ignore any access checks; we'll see them again in
15609 instantiate_template and they might have the wrong
15610 access path at this point. */
15611 push_deferring_access_checks (dk_deferred);
15612 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15613 complain | tf_partial, NULL_TREE);
15614 pop_deferring_access_checks ();
15615 input_location = loc;
15616 processing_template_decl -= incomplete;
15617 pop_tinst_level ();
15618
15619 if (fntype == error_mark_node)
15620 goto fail;
15621
15622 /* Place the explicitly specified arguments in TARGS. */
15623 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15624 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15625 }
15626
15627 /* Never do unification on the 'this' parameter. */
15628 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15629
15630 if (return_type)
15631 {
15632 tree *new_args;
15633
15634 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15635 new_args = XALLOCAVEC (tree, nargs + 1);
15636 new_args[0] = return_type;
15637 memcpy (new_args + 1, args, nargs * sizeof (tree));
15638 args = new_args;
15639 ++nargs;
15640 }
15641
15642 /* We allow incomplete unification without an error message here
15643 because the standard doesn't seem to explicitly prohibit it. Our
15644 callers must be ready to deal with unification failures in any
15645 event. */
15646
15647 TREE_VALUE (tinst) = targs;
15648 /* If we aren't explaining yet, push tinst context so we can see where
15649 any errors (e.g. from class instantiations triggered by instantiation
15650 of default template arguments) come from. If we are explaining, this
15651 context is redundant. */
15652 if (!explain_p && !push_tinst_level (tinst))
15653 {
15654 excessive_deduction_depth = true;
15655 goto fail;
15656 }
15657
15658 /* type_unification_real will pass back any access checks from default
15659 template argument substitution. */
15660 vec<deferred_access_check, va_gc> *checks;
15661 checks = NULL;
15662
15663 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15664 targs, parms, args, nargs, /*subr=*/0,
15665 strict, flags, &checks, explain_p);
15666 if (!explain_p)
15667 pop_tinst_level ();
15668 if (!ok)
15669 goto fail;
15670
15671 /* Now that we have bindings for all of the template arguments,
15672 ensure that the arguments deduced for the template template
15673 parameters have compatible template parameter lists. We cannot
15674 check this property before we have deduced all template
15675 arguments, because the template parameter types of a template
15676 template parameter might depend on prior template parameters
15677 deduced after the template template parameter. The following
15678 ill-formed example illustrates this issue:
15679
15680 template<typename T, template<T> class C> void f(C<5>, T);
15681
15682 template<int N> struct X {};
15683
15684 void g() {
15685 f(X<5>(), 5l); // error: template argument deduction fails
15686 }
15687
15688 The template parameter list of 'C' depends on the template type
15689 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15690 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15691 time that we deduce 'C'. */
15692 if (!template_template_parm_bindings_ok_p
15693 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15694 {
15695 unify_inconsistent_template_template_parameters (explain_p);
15696 goto fail;
15697 }
15698
15699 /* All is well so far. Now, check:
15700
15701 [temp.deduct]
15702
15703 When all template arguments have been deduced, all uses of
15704 template parameters in nondeduced contexts are replaced with
15705 the corresponding deduced argument values. If the
15706 substitution results in an invalid type, as described above,
15707 type deduction fails. */
15708 TREE_VALUE (tinst) = targs;
15709 if (!push_tinst_level (tinst))
15710 {
15711 excessive_deduction_depth = true;
15712 goto fail;
15713 }
15714
15715 /* Also collect access checks from the instantiation. */
15716 reopen_deferring_access_checks (checks);
15717
15718 decl = instantiate_template (fn, targs, complain);
15719
15720 checks = get_deferred_access_checks ();
15721 pop_deferring_access_checks ();
15722
15723 pop_tinst_level ();
15724
15725 if (decl == error_mark_node)
15726 goto fail;
15727
15728 /* Now perform any access checks encountered during substitution. */
15729 push_access_scope (decl);
15730 ok = perform_access_checks (checks, complain);
15731 pop_access_scope (decl);
15732 if (!ok)
15733 goto fail;
15734
15735 /* If we're looking for an exact match, check that what we got
15736 is indeed an exact match. It might not be if some template
15737 parameters are used in non-deduced contexts. */
15738 if (strict == DEDUCE_EXACT)
15739 {
15740 tree substed = TREE_TYPE (decl);
15741 unsigned int i;
15742
15743 tree sarg
15744 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15745 if (return_type)
15746 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15747 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15748 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15749 {
15750 unify_type_mismatch (explain_p, args[i],
15751 TREE_VALUE (sarg));
15752 goto fail;
15753 }
15754 }
15755
15756 r = decl;
15757
15758 fail:
15759 --deduction_depth;
15760 if (excessive_deduction_depth)
15761 {
15762 if (deduction_depth == 0)
15763 /* Reset once we're all the way out. */
15764 excessive_deduction_depth = false;
15765 }
15766
15767 /* We can't free this if a pending_template entry or last_error_tinst_level
15768 is pointing at it. */
15769 if (last_pending_template == old_last_pend
15770 && last_error_tinst_level == old_error_tinst)
15771 ggc_free (tinst);
15772
15773 return r;
15774 }
15775
15776 /* Adjust types before performing type deduction, as described in
15777 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15778 sections are symmetric. PARM is the type of a function parameter
15779 or the return type of the conversion function. ARG is the type of
15780 the argument passed to the call, or the type of the value
15781 initialized with the result of the conversion function.
15782 ARG_EXPR is the original argument expression, which may be null. */
15783
15784 static int
15785 maybe_adjust_types_for_deduction (unification_kind_t strict,
15786 tree* parm,
15787 tree* arg,
15788 tree arg_expr)
15789 {
15790 int result = 0;
15791
15792 switch (strict)
15793 {
15794 case DEDUCE_CALL:
15795 break;
15796
15797 case DEDUCE_CONV:
15798 {
15799 /* Swap PARM and ARG throughout the remainder of this
15800 function; the handling is precisely symmetric since PARM
15801 will initialize ARG rather than vice versa. */
15802 tree* temp = parm;
15803 parm = arg;
15804 arg = temp;
15805 break;
15806 }
15807
15808 case DEDUCE_EXACT:
15809 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15810 too, but here handle it by stripping the reference from PARM
15811 rather than by adding it to ARG. */
15812 if (TREE_CODE (*parm) == REFERENCE_TYPE
15813 && TYPE_REF_IS_RVALUE (*parm)
15814 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15815 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15816 && TREE_CODE (*arg) == REFERENCE_TYPE
15817 && !TYPE_REF_IS_RVALUE (*arg))
15818 *parm = TREE_TYPE (*parm);
15819 /* Nothing else to do in this case. */
15820 return 0;
15821
15822 default:
15823 gcc_unreachable ();
15824 }
15825
15826 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15827 {
15828 /* [temp.deduct.call]
15829
15830 If P is not a reference type:
15831
15832 --If A is an array type, the pointer type produced by the
15833 array-to-pointer standard conversion (_conv.array_) is
15834 used in place of A for type deduction; otherwise,
15835
15836 --If A is a function type, the pointer type produced by
15837 the function-to-pointer standard conversion
15838 (_conv.func_) is used in place of A for type deduction;
15839 otherwise,
15840
15841 --If A is a cv-qualified type, the top level
15842 cv-qualifiers of A's type are ignored for type
15843 deduction. */
15844 if (TREE_CODE (*arg) == ARRAY_TYPE)
15845 *arg = build_pointer_type (TREE_TYPE (*arg));
15846 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15847 *arg = build_pointer_type (*arg);
15848 else
15849 *arg = TYPE_MAIN_VARIANT (*arg);
15850 }
15851
15852 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15853 of the form T&&, where T is a template parameter, and the argument
15854 is an lvalue, T is deduced as A& */
15855 if (TREE_CODE (*parm) == REFERENCE_TYPE
15856 && TYPE_REF_IS_RVALUE (*parm)
15857 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15858 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15859 && (arg_expr ? real_lvalue_p (arg_expr)
15860 /* try_one_overload doesn't provide an arg_expr, but
15861 functions are always lvalues. */
15862 : TREE_CODE (*arg) == FUNCTION_TYPE))
15863 *arg = build_reference_type (*arg);
15864
15865 /* [temp.deduct.call]
15866
15867 If P is a cv-qualified type, the top level cv-qualifiers
15868 of P's type are ignored for type deduction. If P is a
15869 reference type, the type referred to by P is used for
15870 type deduction. */
15871 *parm = TYPE_MAIN_VARIANT (*parm);
15872 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15873 {
15874 *parm = TREE_TYPE (*parm);
15875 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15876 }
15877
15878 /* DR 322. For conversion deduction, remove a reference type on parm
15879 too (which has been swapped into ARG). */
15880 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15881 *arg = TREE_TYPE (*arg);
15882
15883 return result;
15884 }
15885
15886 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15887 template which does contain any deducible template parameters; check if
15888 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15889 unify_one_argument. */
15890
15891 static int
15892 check_non_deducible_conversion (tree parm, tree arg, int strict,
15893 int flags, bool explain_p)
15894 {
15895 tree type;
15896
15897 if (!TYPE_P (arg))
15898 type = TREE_TYPE (arg);
15899 else
15900 type = arg;
15901
15902 if (same_type_p (parm, type))
15903 return unify_success (explain_p);
15904
15905 if (strict == DEDUCE_CONV)
15906 {
15907 if (can_convert_arg (type, parm, NULL_TREE, flags,
15908 explain_p ? tf_warning_or_error : tf_none))
15909 return unify_success (explain_p);
15910 }
15911 else if (strict != DEDUCE_EXACT)
15912 {
15913 if (can_convert_arg (parm, type,
15914 TYPE_P (arg) ? NULL_TREE : arg,
15915 flags, explain_p ? tf_warning_or_error : tf_none))
15916 return unify_success (explain_p);
15917 }
15918
15919 if (strict == DEDUCE_EXACT)
15920 return unify_type_mismatch (explain_p, parm, arg);
15921 else
15922 return unify_arg_conversion (explain_p, parm, type, arg);
15923 }
15924
15925 static bool uses_deducible_template_parms (tree type);
15926
15927 /* Returns true iff the expression EXPR is one from which a template
15928 argument can be deduced. In other words, if it's an undecorated
15929 use of a template non-type parameter. */
15930
15931 static bool
15932 deducible_expression (tree expr)
15933 {
15934 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15935 }
15936
15937 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15938 deducible way; that is, if it has a max value of <PARM> - 1. */
15939
15940 static bool
15941 deducible_array_bound (tree domain)
15942 {
15943 if (domain == NULL_TREE)
15944 return false;
15945
15946 tree max = TYPE_MAX_VALUE (domain);
15947 if (TREE_CODE (max) != MINUS_EXPR)
15948 return false;
15949
15950 return deducible_expression (TREE_OPERAND (max, 0));
15951 }
15952
15953 /* Returns true iff the template arguments ARGS use a template parameter
15954 in a deducible way. */
15955
15956 static bool
15957 deducible_template_args (tree args)
15958 {
15959 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15960 {
15961 bool deducible;
15962 tree elt = TREE_VEC_ELT (args, i);
15963 if (ARGUMENT_PACK_P (elt))
15964 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15965 else
15966 {
15967 if (PACK_EXPANSION_P (elt))
15968 elt = PACK_EXPANSION_PATTERN (elt);
15969 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15970 deducible = true;
15971 else if (TYPE_P (elt))
15972 deducible = uses_deducible_template_parms (elt);
15973 else
15974 deducible = deducible_expression (elt);
15975 }
15976 if (deducible)
15977 return true;
15978 }
15979 return false;
15980 }
15981
15982 /* Returns true iff TYPE contains any deducible references to template
15983 parameters, as per 14.8.2.5. */
15984
15985 static bool
15986 uses_deducible_template_parms (tree type)
15987 {
15988 if (PACK_EXPANSION_P (type))
15989 type = PACK_EXPANSION_PATTERN (type);
15990
15991 /* T
15992 cv-list T
15993 TT<T>
15994 TT<i>
15995 TT<> */
15996 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15997 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15998 return true;
15999
16000 /* T*
16001 T&
16002 T&& */
16003 if (POINTER_TYPE_P (type))
16004 return uses_deducible_template_parms (TREE_TYPE (type));
16005
16006 /* T[integer-constant ]
16007 type [i] */
16008 if (TREE_CODE (type) == ARRAY_TYPE)
16009 return (uses_deducible_template_parms (TREE_TYPE (type))
16010 || deducible_array_bound (TYPE_DOMAIN (type)));
16011
16012 /* T type ::*
16013 type T::*
16014 T T::*
16015 T (type ::*)()
16016 type (T::*)()
16017 type (type ::*)(T)
16018 type (T::*)(T)
16019 T (type ::*)(T)
16020 T (T::*)()
16021 T (T::*)(T) */
16022 if (TYPE_PTRMEM_P (type))
16023 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
16024 || (uses_deducible_template_parms
16025 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
16026
16027 /* template-name <T> (where template-name refers to a class template)
16028 template-name <i> (where template-name refers to a class template) */
16029 if (CLASS_TYPE_P (type)
16030 && CLASSTYPE_TEMPLATE_INFO (type)
16031 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
16032 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
16033 (CLASSTYPE_TI_ARGS (type)));
16034
16035 /* type (T)
16036 T()
16037 T(T) */
16038 if (TREE_CODE (type) == FUNCTION_TYPE
16039 || TREE_CODE (type) == METHOD_TYPE)
16040 {
16041 if (uses_deducible_template_parms (TREE_TYPE (type)))
16042 return true;
16043 tree parm = TYPE_ARG_TYPES (type);
16044 if (TREE_CODE (type) == METHOD_TYPE)
16045 parm = TREE_CHAIN (parm);
16046 for (; parm; parm = TREE_CHAIN (parm))
16047 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16048 return true;
16049 }
16050
16051 return false;
16052 }
16053
16054 /* Subroutine of type_unification_real and unify_pack_expansion to
16055 handle unification of a single P/A pair. Parameters are as
16056 for those functions. */
16057
16058 static int
16059 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16060 int subr, unification_kind_t strict, int flags,
16061 bool explain_p)
16062 {
16063 tree arg_expr = NULL_TREE;
16064 int arg_strict;
16065
16066 if (arg == error_mark_node || parm == error_mark_node)
16067 return unify_invalid (explain_p);
16068 if (arg == unknown_type_node)
16069 /* We can't deduce anything from this, but we might get all the
16070 template args from other function args. */
16071 return unify_success (explain_p);
16072
16073 /* Implicit conversions (Clause 4) will be performed on a function
16074 argument to convert it to the type of the corresponding function
16075 parameter if the parameter type contains no template-parameters that
16076 participate in template argument deduction. */
16077 if (TYPE_P (parm) && !uses_template_parms (parm))
16078 /* For function parameters that contain no template-parameters at all,
16079 we have historically checked for convertibility in order to shortcut
16080 consideration of this candidate. */
16081 return check_non_deducible_conversion (parm, arg, strict, flags,
16082 explain_p);
16083 else if (strict == DEDUCE_CALL
16084 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16085 /* For function parameters with only non-deducible template parameters,
16086 just return. */
16087 return unify_success (explain_p);
16088
16089 switch (strict)
16090 {
16091 case DEDUCE_CALL:
16092 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16093 | UNIFY_ALLOW_MORE_CV_QUAL
16094 | UNIFY_ALLOW_DERIVED);
16095 break;
16096
16097 case DEDUCE_CONV:
16098 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16099 break;
16100
16101 case DEDUCE_EXACT:
16102 arg_strict = UNIFY_ALLOW_NONE;
16103 break;
16104
16105 default:
16106 gcc_unreachable ();
16107 }
16108
16109 /* We only do these transformations if this is the top-level
16110 parameter_type_list in a call or declaration matching; in other
16111 situations (nested function declarators, template argument lists) we
16112 won't be comparing a type to an expression, and we don't do any type
16113 adjustments. */
16114 if (!subr)
16115 {
16116 if (!TYPE_P (arg))
16117 {
16118 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16119 if (type_unknown_p (arg))
16120 {
16121 /* [temp.deduct.type] A template-argument can be
16122 deduced from a pointer to function or pointer
16123 to member function argument if the set of
16124 overloaded functions does not contain function
16125 templates and at most one of a set of
16126 overloaded functions provides a unique
16127 match. */
16128
16129 if (resolve_overloaded_unification
16130 (tparms, targs, parm, arg, strict,
16131 arg_strict, explain_p))
16132 return unify_success (explain_p);
16133 return unify_overload_resolution_failure (explain_p, arg);
16134 }
16135
16136 arg_expr = arg;
16137 arg = unlowered_expr_type (arg);
16138 if (arg == error_mark_node)
16139 return unify_invalid (explain_p);
16140 }
16141
16142 arg_strict |=
16143 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16144 }
16145 else
16146 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16147 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16148
16149 /* For deduction from an init-list we need the actual list. */
16150 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16151 arg = arg_expr;
16152 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16153 }
16154
16155 /* Most parms like fn_type_unification.
16156
16157 If SUBR is 1, we're being called recursively (to unify the
16158 arguments of a function or method parameter of a function
16159 template).
16160
16161 CHECKS is a pointer to a vector of access checks encountered while
16162 substituting default template arguments. */
16163
16164 static int
16165 type_unification_real (tree tparms,
16166 tree targs,
16167 tree xparms,
16168 const tree *xargs,
16169 unsigned int xnargs,
16170 int subr,
16171 unification_kind_t strict,
16172 int flags,
16173 vec<deferred_access_check, va_gc> **checks,
16174 bool explain_p)
16175 {
16176 tree parm, arg;
16177 int i;
16178 int ntparms = TREE_VEC_LENGTH (tparms);
16179 int saw_undeduced = 0;
16180 tree parms;
16181 const tree *args;
16182 unsigned int nargs;
16183 unsigned int ia;
16184
16185 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16186 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16187 gcc_assert (ntparms > 0);
16188
16189 /* Reset the number of non-defaulted template arguments contained
16190 in TARGS. */
16191 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16192
16193 again:
16194 parms = xparms;
16195 args = xargs;
16196 nargs = xnargs;
16197
16198 ia = 0;
16199 while (parms && parms != void_list_node
16200 && ia < nargs)
16201 {
16202 parm = TREE_VALUE (parms);
16203
16204 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16205 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16206 /* For a function parameter pack that occurs at the end of the
16207 parameter-declaration-list, the type A of each remaining
16208 argument of the call is compared with the type P of the
16209 declarator-id of the function parameter pack. */
16210 break;
16211
16212 parms = TREE_CHAIN (parms);
16213
16214 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16215 /* For a function parameter pack that does not occur at the
16216 end of the parameter-declaration-list, the type of the
16217 parameter pack is a non-deduced context. */
16218 continue;
16219
16220 arg = args[ia];
16221 ++ia;
16222
16223 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16224 flags, explain_p))
16225 return 1;
16226 }
16227
16228 if (parms
16229 && parms != void_list_node
16230 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16231 {
16232 /* Unify the remaining arguments with the pack expansion type. */
16233 tree argvec;
16234 tree parmvec = make_tree_vec (1);
16235
16236 /* Allocate a TREE_VEC and copy in all of the arguments */
16237 argvec = make_tree_vec (nargs - ia);
16238 for (i = 0; ia < nargs; ++ia, ++i)
16239 TREE_VEC_ELT (argvec, i) = args[ia];
16240
16241 /* Copy the parameter into parmvec. */
16242 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16243 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16244 /*subr=*/subr, explain_p))
16245 return 1;
16246
16247 /* Advance to the end of the list of parameters. */
16248 parms = TREE_CHAIN (parms);
16249 }
16250
16251 /* Fail if we've reached the end of the parm list, and more args
16252 are present, and the parm list isn't variadic. */
16253 if (ia < nargs && parms == void_list_node)
16254 return unify_too_many_arguments (explain_p, nargs, ia);
16255 /* Fail if parms are left and they don't have default values. */
16256 if (parms && parms != void_list_node
16257 && TREE_PURPOSE (parms) == NULL_TREE)
16258 {
16259 unsigned int count = nargs;
16260 tree p = parms;
16261 while (p && p != void_list_node)
16262 {
16263 count++;
16264 p = TREE_CHAIN (p);
16265 }
16266 return unify_too_few_arguments (explain_p, ia, count);
16267 }
16268
16269 if (!subr)
16270 {
16271 tsubst_flags_t complain = (explain_p
16272 ? tf_warning_or_error
16273 : tf_none);
16274
16275 for (i = 0; i < ntparms; i++)
16276 {
16277 tree targ = TREE_VEC_ELT (targs, i);
16278 tree tparm = TREE_VEC_ELT (tparms, i);
16279
16280 /* Clear the "incomplete" flags on all argument packs now so that
16281 substituting them into later default arguments works. */
16282 if (targ && ARGUMENT_PACK_P (targ))
16283 {
16284 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16285 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16286 }
16287
16288 if (targ || tparm == error_mark_node)
16289 continue;
16290 tparm = TREE_VALUE (tparm);
16291
16292 /* If this is an undeduced nontype parameter that depends on
16293 a type parameter, try another pass; its type may have been
16294 deduced from a later argument than the one from which
16295 this parameter can be deduced. */
16296 if (TREE_CODE (tparm) == PARM_DECL
16297 && uses_template_parms (TREE_TYPE (tparm))
16298 && !saw_undeduced++)
16299 goto again;
16300
16301 /* Core issue #226 (C++0x) [temp.deduct]:
16302
16303 If a template argument has not been deduced, its
16304 default template argument, if any, is used.
16305
16306 When we are in C++98 mode, TREE_PURPOSE will either
16307 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16308 to explicitly check cxx_dialect here. */
16309 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16310 {
16311 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16312 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16313 reopen_deferring_access_checks (*checks);
16314 location_t save_loc = input_location;
16315 if (DECL_P (parm))
16316 input_location = DECL_SOURCE_LOCATION (parm);
16317 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16318 arg = convert_template_argument (parm, arg, targs, complain,
16319 i, NULL_TREE);
16320 input_location = save_loc;
16321 *checks = get_deferred_access_checks ();
16322 pop_deferring_access_checks ();
16323 if (arg == error_mark_node)
16324 return 1;
16325 else
16326 {
16327 TREE_VEC_ELT (targs, i) = arg;
16328 /* The position of the first default template argument,
16329 is also the number of non-defaulted arguments in TARGS.
16330 Record that. */
16331 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16332 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16333 continue;
16334 }
16335 }
16336
16337 /* If the type parameter is a parameter pack, then it will
16338 be deduced to an empty parameter pack. */
16339 if (template_parameter_pack_p (tparm))
16340 {
16341 tree arg;
16342
16343 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16344 {
16345 arg = make_node (NONTYPE_ARGUMENT_PACK);
16346 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16347 TREE_CONSTANT (arg) = 1;
16348 }
16349 else
16350 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16351
16352 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16353
16354 TREE_VEC_ELT (targs, i) = arg;
16355 continue;
16356 }
16357
16358 return unify_parameter_deduction_failure (explain_p, tparm);
16359 }
16360 }
16361 #ifdef ENABLE_CHECKING
16362 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16363 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16364 #endif
16365
16366 return unify_success (explain_p);
16367 }
16368
16369 /* Subroutine of type_unification_real. Args are like the variables
16370 at the call site. ARG is an overloaded function (or template-id);
16371 we try deducing template args from each of the overloads, and if
16372 only one succeeds, we go with that. Modifies TARGS and returns
16373 true on success. */
16374
16375 static bool
16376 resolve_overloaded_unification (tree tparms,
16377 tree targs,
16378 tree parm,
16379 tree arg,
16380 unification_kind_t strict,
16381 int sub_strict,
16382 bool explain_p)
16383 {
16384 tree tempargs = copy_node (targs);
16385 int good = 0;
16386 tree goodfn = NULL_TREE;
16387 bool addr_p;
16388
16389 if (TREE_CODE (arg) == ADDR_EXPR)
16390 {
16391 arg = TREE_OPERAND (arg, 0);
16392 addr_p = true;
16393 }
16394 else
16395 addr_p = false;
16396
16397 if (TREE_CODE (arg) == COMPONENT_REF)
16398 /* Handle `&x' where `x' is some static or non-static member
16399 function name. */
16400 arg = TREE_OPERAND (arg, 1);
16401
16402 if (TREE_CODE (arg) == OFFSET_REF)
16403 arg = TREE_OPERAND (arg, 1);
16404
16405 /* Strip baselink information. */
16406 if (BASELINK_P (arg))
16407 arg = BASELINK_FUNCTIONS (arg);
16408
16409 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16410 {
16411 /* If we got some explicit template args, we need to plug them into
16412 the affected templates before we try to unify, in case the
16413 explicit args will completely resolve the templates in question. */
16414
16415 int ok = 0;
16416 tree expl_subargs = TREE_OPERAND (arg, 1);
16417 arg = TREE_OPERAND (arg, 0);
16418
16419 for (; arg; arg = OVL_NEXT (arg))
16420 {
16421 tree fn = OVL_CURRENT (arg);
16422 tree subargs, elem;
16423
16424 if (TREE_CODE (fn) != TEMPLATE_DECL)
16425 continue;
16426
16427 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16428 expl_subargs, NULL_TREE, tf_none,
16429 /*require_all_args=*/true,
16430 /*use_default_args=*/true);
16431 if (subargs != error_mark_node
16432 && !any_dependent_template_arguments_p (subargs))
16433 {
16434 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
16435 if (try_one_overload (tparms, targs, tempargs, parm,
16436 elem, strict, sub_strict, addr_p, explain_p)
16437 && (!goodfn || !same_type_p (goodfn, elem)))
16438 {
16439 goodfn = elem;
16440 ++good;
16441 }
16442 }
16443 else if (subargs)
16444 ++ok;
16445 }
16446 /* If no templates (or more than one) are fully resolved by the
16447 explicit arguments, this template-id is a non-deduced context; it
16448 could still be OK if we deduce all template arguments for the
16449 enclosing call through other arguments. */
16450 if (good != 1)
16451 good = ok;
16452 }
16453 else if (TREE_CODE (arg) != OVERLOAD
16454 && TREE_CODE (arg) != FUNCTION_DECL)
16455 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16456 -- but the deduction does not succeed because the expression is
16457 not just the function on its own. */
16458 return false;
16459 else
16460 for (; arg; arg = OVL_NEXT (arg))
16461 if (try_one_overload (tparms, targs, tempargs, parm,
16462 TREE_TYPE (OVL_CURRENT (arg)),
16463 strict, sub_strict, addr_p, explain_p)
16464 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16465 {
16466 goodfn = OVL_CURRENT (arg);
16467 ++good;
16468 }
16469
16470 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16471 to function or pointer to member function argument if the set of
16472 overloaded functions does not contain function templates and at most
16473 one of a set of overloaded functions provides a unique match.
16474
16475 So if we found multiple possibilities, we return success but don't
16476 deduce anything. */
16477
16478 if (good == 1)
16479 {
16480 int i = TREE_VEC_LENGTH (targs);
16481 for (; i--; )
16482 if (TREE_VEC_ELT (tempargs, i))
16483 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16484 }
16485 if (good)
16486 return true;
16487
16488 return false;
16489 }
16490
16491 /* Core DR 115: In contexts where deduction is done and fails, or in
16492 contexts where deduction is not done, if a template argument list is
16493 specified and it, along with any default template arguments, identifies
16494 a single function template specialization, then the template-id is an
16495 lvalue for the function template specialization. */
16496
16497 tree
16498 resolve_nondeduced_context (tree orig_expr)
16499 {
16500 tree expr, offset, baselink;
16501 bool addr;
16502
16503 if (!type_unknown_p (orig_expr))
16504 return orig_expr;
16505
16506 expr = orig_expr;
16507 addr = false;
16508 offset = NULL_TREE;
16509 baselink = NULL_TREE;
16510
16511 if (TREE_CODE (expr) == ADDR_EXPR)
16512 {
16513 expr = TREE_OPERAND (expr, 0);
16514 addr = true;
16515 }
16516 if (TREE_CODE (expr) == OFFSET_REF)
16517 {
16518 offset = expr;
16519 expr = TREE_OPERAND (expr, 1);
16520 }
16521 if (BASELINK_P (expr))
16522 {
16523 baselink = expr;
16524 expr = BASELINK_FUNCTIONS (expr);
16525 }
16526
16527 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16528 {
16529 int good = 0;
16530 tree goodfn = NULL_TREE;
16531
16532 /* If we got some explicit template args, we need to plug them into
16533 the affected templates before we try to unify, in case the
16534 explicit args will completely resolve the templates in question. */
16535
16536 tree expl_subargs = TREE_OPERAND (expr, 1);
16537 tree arg = TREE_OPERAND (expr, 0);
16538 tree badfn = NULL_TREE;
16539 tree badargs = NULL_TREE;
16540
16541 for (; arg; arg = OVL_NEXT (arg))
16542 {
16543 tree fn = OVL_CURRENT (arg);
16544 tree subargs, elem;
16545
16546 if (TREE_CODE (fn) != TEMPLATE_DECL)
16547 continue;
16548
16549 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16550 expl_subargs, NULL_TREE, tf_none,
16551 /*require_all_args=*/true,
16552 /*use_default_args=*/true);
16553 if (subargs != error_mark_node
16554 && !any_dependent_template_arguments_p (subargs))
16555 {
16556 elem = instantiate_template (fn, subargs, tf_none);
16557 if (elem == error_mark_node)
16558 {
16559 badfn = fn;
16560 badargs = subargs;
16561 }
16562 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16563 {
16564 goodfn = elem;
16565 ++good;
16566 }
16567 }
16568 }
16569 if (good == 1)
16570 {
16571 mark_used (goodfn);
16572 expr = goodfn;
16573 if (baselink)
16574 expr = build_baselink (BASELINK_BINFO (baselink),
16575 BASELINK_ACCESS_BINFO (baselink),
16576 expr, BASELINK_OPTYPE (baselink));
16577 if (offset)
16578 {
16579 tree base
16580 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16581 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16582 }
16583 if (addr)
16584 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16585 return expr;
16586 }
16587 else if (good == 0 && badargs)
16588 /* There were no good options and at least one bad one, so let the
16589 user know what the problem is. */
16590 instantiate_template (badfn, badargs, tf_warning_or_error);
16591 }
16592 return orig_expr;
16593 }
16594
16595 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16596 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16597 different overloads deduce different arguments for a given parm.
16598 ADDR_P is true if the expression for which deduction is being
16599 performed was of the form "& fn" rather than simply "fn".
16600
16601 Returns 1 on success. */
16602
16603 static int
16604 try_one_overload (tree tparms,
16605 tree orig_targs,
16606 tree targs,
16607 tree parm,
16608 tree arg,
16609 unification_kind_t strict,
16610 int sub_strict,
16611 bool addr_p,
16612 bool explain_p)
16613 {
16614 int nargs;
16615 tree tempargs;
16616 int i;
16617
16618 if (arg == error_mark_node)
16619 return 0;
16620
16621 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16622 to function or pointer to member function argument if the set of
16623 overloaded functions does not contain function templates and at most
16624 one of a set of overloaded functions provides a unique match.
16625
16626 So if this is a template, just return success. */
16627
16628 if (uses_template_parms (arg))
16629 return 1;
16630
16631 if (TREE_CODE (arg) == METHOD_TYPE)
16632 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16633 else if (addr_p)
16634 arg = build_pointer_type (arg);
16635
16636 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16637
16638 /* We don't copy orig_targs for this because if we have already deduced
16639 some template args from previous args, unify would complain when we
16640 try to deduce a template parameter for the same argument, even though
16641 there isn't really a conflict. */
16642 nargs = TREE_VEC_LENGTH (targs);
16643 tempargs = make_tree_vec (nargs);
16644
16645 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16646 return 0;
16647
16648 /* First make sure we didn't deduce anything that conflicts with
16649 explicitly specified args. */
16650 for (i = nargs; i--; )
16651 {
16652 tree elt = TREE_VEC_ELT (tempargs, i);
16653 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16654
16655 if (!elt)
16656 /*NOP*/;
16657 else if (uses_template_parms (elt))
16658 /* Since we're unifying against ourselves, we will fill in
16659 template args used in the function parm list with our own
16660 template parms. Discard them. */
16661 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16662 else if (oldelt && !template_args_equal (oldelt, elt))
16663 return 0;
16664 }
16665
16666 for (i = nargs; i--; )
16667 {
16668 tree elt = TREE_VEC_ELT (tempargs, i);
16669
16670 if (elt)
16671 TREE_VEC_ELT (targs, i) = elt;
16672 }
16673
16674 return 1;
16675 }
16676
16677 /* PARM is a template class (perhaps with unbound template
16678 parameters). ARG is a fully instantiated type. If ARG can be
16679 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16680 TARGS are as for unify. */
16681
16682 static tree
16683 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16684 bool explain_p)
16685 {
16686 tree copy_of_targs;
16687
16688 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16689 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16690 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16691 return NULL_TREE;
16692
16693 /* We need to make a new template argument vector for the call to
16694 unify. If we used TARGS, we'd clutter it up with the result of
16695 the attempted unification, even if this class didn't work out.
16696 We also don't want to commit ourselves to all the unifications
16697 we've already done, since unification is supposed to be done on
16698 an argument-by-argument basis. In other words, consider the
16699 following pathological case:
16700
16701 template <int I, int J, int K>
16702 struct S {};
16703
16704 template <int I, int J>
16705 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16706
16707 template <int I, int J, int K>
16708 void f(S<I, J, K>, S<I, I, I>);
16709
16710 void g() {
16711 S<0, 0, 0> s0;
16712 S<0, 1, 2> s2;
16713
16714 f(s0, s2);
16715 }
16716
16717 Now, by the time we consider the unification involving `s2', we
16718 already know that we must have `f<0, 0, 0>'. But, even though
16719 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16720 because there are two ways to unify base classes of S<0, 1, 2>
16721 with S<I, I, I>. If we kept the already deduced knowledge, we
16722 would reject the possibility I=1. */
16723 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16724
16725 /* If unification failed, we're done. */
16726 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16727 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16728 return NULL_TREE;
16729
16730 return arg;
16731 }
16732
16733 /* Given a template type PARM and a class type ARG, find the unique
16734 base type in ARG that is an instance of PARM. We do not examine
16735 ARG itself; only its base-classes. If there is not exactly one
16736 appropriate base class, return NULL_TREE. PARM may be the type of
16737 a partial specialization, as well as a plain template type. Used
16738 by unify. */
16739
16740 static enum template_base_result
16741 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16742 bool explain_p, tree *result)
16743 {
16744 tree rval = NULL_TREE;
16745 tree binfo;
16746
16747 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16748
16749 binfo = TYPE_BINFO (complete_type (arg));
16750 if (!binfo)
16751 {
16752 /* The type could not be completed. */
16753 *result = NULL_TREE;
16754 return tbr_incomplete_type;
16755 }
16756
16757 /* Walk in inheritance graph order. The search order is not
16758 important, and this avoids multiple walks of virtual bases. */
16759 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16760 {
16761 tree r = try_class_unification (tparms, targs, parm,
16762 BINFO_TYPE (binfo), explain_p);
16763
16764 if (r)
16765 {
16766 /* If there is more than one satisfactory baseclass, then:
16767
16768 [temp.deduct.call]
16769
16770 If they yield more than one possible deduced A, the type
16771 deduction fails.
16772
16773 applies. */
16774 if (rval && !same_type_p (r, rval))
16775 {
16776 *result = NULL_TREE;
16777 return tbr_ambiguous_baseclass;
16778 }
16779
16780 rval = r;
16781 }
16782 }
16783
16784 *result = rval;
16785 return tbr_success;
16786 }
16787
16788 /* Returns the level of DECL, which declares a template parameter. */
16789
16790 static int
16791 template_decl_level (tree decl)
16792 {
16793 switch (TREE_CODE (decl))
16794 {
16795 case TYPE_DECL:
16796 case TEMPLATE_DECL:
16797 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16798
16799 case PARM_DECL:
16800 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16801
16802 default:
16803 gcc_unreachable ();
16804 }
16805 return 0;
16806 }
16807
16808 /* Decide whether ARG can be unified with PARM, considering only the
16809 cv-qualifiers of each type, given STRICT as documented for unify.
16810 Returns nonzero iff the unification is OK on that basis. */
16811
16812 static int
16813 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16814 {
16815 int arg_quals = cp_type_quals (arg);
16816 int parm_quals = cp_type_quals (parm);
16817
16818 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16819 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16820 {
16821 /* Although a CVR qualifier is ignored when being applied to a
16822 substituted template parameter ([8.3.2]/1 for example), that
16823 does not allow us to unify "const T" with "int&" because both
16824 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16825 It is ok when we're allowing additional CV qualifiers
16826 at the outer level [14.8.2.1]/3,1st bullet. */
16827 if ((TREE_CODE (arg) == REFERENCE_TYPE
16828 || TREE_CODE (arg) == FUNCTION_TYPE
16829 || TREE_CODE (arg) == METHOD_TYPE)
16830 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16831 return 0;
16832
16833 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16834 && (parm_quals & TYPE_QUAL_RESTRICT))
16835 return 0;
16836 }
16837
16838 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16839 && (arg_quals & parm_quals) != parm_quals)
16840 return 0;
16841
16842 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16843 && (parm_quals & arg_quals) != arg_quals)
16844 return 0;
16845
16846 return 1;
16847 }
16848
16849 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16850 void
16851 template_parm_level_and_index (tree parm, int* level, int* index)
16852 {
16853 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16854 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16855 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16856 {
16857 *index = TEMPLATE_TYPE_IDX (parm);
16858 *level = TEMPLATE_TYPE_LEVEL (parm);
16859 }
16860 else
16861 {
16862 *index = TEMPLATE_PARM_IDX (parm);
16863 *level = TEMPLATE_PARM_LEVEL (parm);
16864 }
16865 }
16866
16867 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16868 do { \
16869 if (unify (TP, TA, P, A, S, EP)) \
16870 return 1; \
16871 } while (0);
16872
16873 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16874 expansion at the end of PACKED_PARMS. Returns 0 if the type
16875 deduction succeeds, 1 otherwise. STRICT is the same as in
16876 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16877 call argument list. We'll need to adjust the arguments to make them
16878 types. SUBR tells us if this is from a recursive call to
16879 type_unification_real, or for comparing two template argument
16880 lists. */
16881
16882 static int
16883 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16884 tree packed_args, unification_kind_t strict,
16885 bool subr, bool explain_p)
16886 {
16887 tree parm
16888 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16889 tree pattern = PACK_EXPANSION_PATTERN (parm);
16890 tree pack, packs = NULL_TREE;
16891 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16892 int len = TREE_VEC_LENGTH (packed_args);
16893
16894 /* Determine the parameter packs we will be deducing from the
16895 pattern, and record their current deductions. */
16896 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16897 pack; pack = TREE_CHAIN (pack))
16898 {
16899 tree parm_pack = TREE_VALUE (pack);
16900 int idx, level;
16901
16902 /* Determine the index and level of this parameter pack. */
16903 template_parm_level_and_index (parm_pack, &level, &idx);
16904
16905 /* Keep track of the parameter packs and their corresponding
16906 argument packs. */
16907 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16908 TREE_TYPE (packs) = make_tree_vec (len - start);
16909 }
16910
16911 /* Loop through all of the arguments that have not yet been
16912 unified and unify each with the pattern. */
16913 for (i = start; i < len; i++)
16914 {
16915 tree parm;
16916 bool any_explicit = false;
16917 tree arg = TREE_VEC_ELT (packed_args, i);
16918
16919 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16920 or the element of its argument pack at the current index if
16921 this argument was explicitly specified. */
16922 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16923 {
16924 int idx, level;
16925 tree arg, pargs;
16926 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16927
16928 arg = NULL_TREE;
16929 if (TREE_VALUE (pack)
16930 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16931 && (i - start < TREE_VEC_LENGTH (pargs)))
16932 {
16933 any_explicit = true;
16934 arg = TREE_VEC_ELT (pargs, i - start);
16935 }
16936 TMPL_ARG (targs, level, idx) = arg;
16937 }
16938
16939 /* If we had explicit template arguments, substitute them into the
16940 pattern before deduction. */
16941 if (any_explicit)
16942 {
16943 /* Some arguments might still be unspecified or dependent. */
16944 bool dependent;
16945 ++processing_template_decl;
16946 dependent = any_dependent_template_arguments_p (targs);
16947 if (!dependent)
16948 --processing_template_decl;
16949 parm = tsubst (pattern, targs,
16950 explain_p ? tf_warning_or_error : tf_none,
16951 NULL_TREE);
16952 if (dependent)
16953 --processing_template_decl;
16954 if (parm == error_mark_node)
16955 return 1;
16956 }
16957 else
16958 parm = pattern;
16959
16960 /* Unify the pattern with the current argument. */
16961 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16962 LOOKUP_IMPLICIT, explain_p))
16963 return 1;
16964
16965 /* For each parameter pack, collect the deduced value. */
16966 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16967 {
16968 int idx, level;
16969 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16970
16971 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16972 TMPL_ARG (targs, level, idx);
16973 }
16974 }
16975
16976 /* Verify that the results of unification with the parameter packs
16977 produce results consistent with what we've seen before, and make
16978 the deduced argument packs available. */
16979 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16980 {
16981 tree old_pack = TREE_VALUE (pack);
16982 tree new_args = TREE_TYPE (pack);
16983 int i, len = TREE_VEC_LENGTH (new_args);
16984 int idx, level;
16985 bool nondeduced_p = false;
16986
16987 /* By default keep the original deduced argument pack.
16988 If necessary, more specific code is going to update the
16989 resulting deduced argument later down in this function. */
16990 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16991 TMPL_ARG (targs, level, idx) = old_pack;
16992
16993 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16994 actually deduce anything. */
16995 for (i = 0; i < len && !nondeduced_p; ++i)
16996 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16997 nondeduced_p = true;
16998 if (nondeduced_p)
16999 continue;
17000
17001 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
17002 {
17003 /* If we had fewer function args than explicit template args,
17004 just use the explicits. */
17005 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17006 int explicit_len = TREE_VEC_LENGTH (explicit_args);
17007 if (len < explicit_len)
17008 new_args = explicit_args;
17009 }
17010
17011 if (!old_pack)
17012 {
17013 tree result;
17014 /* Build the deduced *_ARGUMENT_PACK. */
17015 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
17016 {
17017 result = make_node (NONTYPE_ARGUMENT_PACK);
17018 TREE_TYPE (result) =
17019 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
17020 TREE_CONSTANT (result) = 1;
17021 }
17022 else
17023 result = cxx_make_type (TYPE_ARGUMENT_PACK);
17024
17025 SET_ARGUMENT_PACK_ARGS (result, new_args);
17026
17027 /* Note the deduced argument packs for this parameter
17028 pack. */
17029 TMPL_ARG (targs, level, idx) = result;
17030 }
17031 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
17032 && (ARGUMENT_PACK_ARGS (old_pack)
17033 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
17034 {
17035 /* We only had the explicitly-provided arguments before, but
17036 now we have a complete set of arguments. */
17037 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
17038
17039 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
17040 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
17041 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17042 }
17043 else
17044 {
17045 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17046 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17047
17048 if (!comp_template_args_with_info (old_args, new_args,
17049 &bad_old_arg, &bad_new_arg))
17050 /* Inconsistent unification of this parameter pack. */
17051 return unify_parameter_pack_inconsistent (explain_p,
17052 bad_old_arg,
17053 bad_new_arg);
17054 }
17055 }
17056
17057 return unify_success (explain_p);
17058 }
17059
17060 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17061 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17062 parameters and return value are as for unify. */
17063
17064 static int
17065 unify_array_domain (tree tparms, tree targs,
17066 tree parm_dom, tree arg_dom,
17067 bool explain_p)
17068 {
17069 tree parm_max;
17070 tree arg_max;
17071 bool parm_cst;
17072 bool arg_cst;
17073
17074 /* Our representation of array types uses "N - 1" as the
17075 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17076 not an integer constant. We cannot unify arbitrarily
17077 complex expressions, so we eliminate the MINUS_EXPRs
17078 here. */
17079 parm_max = TYPE_MAX_VALUE (parm_dom);
17080 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17081 if (!parm_cst)
17082 {
17083 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17084 parm_max = TREE_OPERAND (parm_max, 0);
17085 }
17086 arg_max = TYPE_MAX_VALUE (arg_dom);
17087 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17088 if (!arg_cst)
17089 {
17090 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17091 trying to unify the type of a variable with the type
17092 of a template parameter. For example:
17093
17094 template <unsigned int N>
17095 void f (char (&) [N]);
17096 int g();
17097 void h(int i) {
17098 char a[g(i)];
17099 f(a);
17100 }
17101
17102 Here, the type of the ARG will be "int [g(i)]", and
17103 may be a SAVE_EXPR, etc. */
17104 if (TREE_CODE (arg_max) != MINUS_EXPR)
17105 return unify_vla_arg (explain_p, arg_dom);
17106 arg_max = TREE_OPERAND (arg_max, 0);
17107 }
17108
17109 /* If only one of the bounds used a MINUS_EXPR, compensate
17110 by adding one to the other bound. */
17111 if (parm_cst && !arg_cst)
17112 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17113 integer_type_node,
17114 parm_max,
17115 integer_one_node);
17116 else if (arg_cst && !parm_cst)
17117 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17118 integer_type_node,
17119 arg_max,
17120 integer_one_node);
17121
17122 return unify (tparms, targs, parm_max, arg_max,
17123 UNIFY_ALLOW_INTEGER, explain_p);
17124 }
17125
17126 /* Deduce the value of template parameters. TPARMS is the (innermost)
17127 set of template parameters to a template. TARGS is the bindings
17128 for those template parameters, as determined thus far; TARGS may
17129 include template arguments for outer levels of template parameters
17130 as well. PARM is a parameter to a template function, or a
17131 subcomponent of that parameter; ARG is the corresponding argument.
17132 This function attempts to match PARM with ARG in a manner
17133 consistent with the existing assignments in TARGS. If more values
17134 are deduced, then TARGS is updated.
17135
17136 Returns 0 if the type deduction succeeds, 1 otherwise. The
17137 parameter STRICT is a bitwise or of the following flags:
17138
17139 UNIFY_ALLOW_NONE:
17140 Require an exact match between PARM and ARG.
17141 UNIFY_ALLOW_MORE_CV_QUAL:
17142 Allow the deduced ARG to be more cv-qualified (by qualification
17143 conversion) than ARG.
17144 UNIFY_ALLOW_LESS_CV_QUAL:
17145 Allow the deduced ARG to be less cv-qualified than ARG.
17146 UNIFY_ALLOW_DERIVED:
17147 Allow the deduced ARG to be a template base class of ARG,
17148 or a pointer to a template base class of the type pointed to by
17149 ARG.
17150 UNIFY_ALLOW_INTEGER:
17151 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17152 case for more information.
17153 UNIFY_ALLOW_OUTER_LEVEL:
17154 This is the outermost level of a deduction. Used to determine validity
17155 of qualification conversions. A valid qualification conversion must
17156 have const qualified pointers leading up to the inner type which
17157 requires additional CV quals, except at the outer level, where const
17158 is not required [conv.qual]. It would be normal to set this flag in
17159 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17160 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17161 This is the outermost level of a deduction, and PARM can be more CV
17162 qualified at this point.
17163 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17164 This is the outermost level of a deduction, and PARM can be less CV
17165 qualified at this point. */
17166
17167 static int
17168 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17169 bool explain_p)
17170 {
17171 int idx;
17172 tree targ;
17173 tree tparm;
17174 int strict_in = strict;
17175
17176 /* I don't think this will do the right thing with respect to types.
17177 But the only case I've seen it in so far has been array bounds, where
17178 signedness is the only information lost, and I think that will be
17179 okay. */
17180 while (TREE_CODE (parm) == NOP_EXPR)
17181 parm = TREE_OPERAND (parm, 0);
17182
17183 if (arg == error_mark_node)
17184 return unify_invalid (explain_p);
17185 if (arg == unknown_type_node
17186 || arg == init_list_type_node)
17187 /* We can't deduce anything from this, but we might get all the
17188 template args from other function args. */
17189 return unify_success (explain_p);
17190
17191 /* If PARM uses template parameters, then we can't bail out here,
17192 even if ARG == PARM, since we won't record unifications for the
17193 template parameters. We might need them if we're trying to
17194 figure out which of two things is more specialized. */
17195 if (arg == parm && !uses_template_parms (parm))
17196 return unify_success (explain_p);
17197
17198 /* Handle init lists early, so the rest of the function can assume
17199 we're dealing with a type. */
17200 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17201 {
17202 tree elt, elttype;
17203 unsigned i;
17204 tree orig_parm = parm;
17205
17206 /* Replace T with std::initializer_list<T> for deduction. */
17207 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17208 && flag_deduce_init_list)
17209 parm = listify (parm);
17210
17211 if (!is_std_init_list (parm)
17212 && TREE_CODE (parm) != ARRAY_TYPE)
17213 /* We can only deduce from an initializer list argument if the
17214 parameter is std::initializer_list or an array; otherwise this
17215 is a non-deduced context. */
17216 return unify_success (explain_p);
17217
17218 if (TREE_CODE (parm) == ARRAY_TYPE)
17219 elttype = TREE_TYPE (parm);
17220 else
17221 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17222
17223 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17224 {
17225 int elt_strict = strict;
17226
17227 if (elt == error_mark_node)
17228 return unify_invalid (explain_p);
17229
17230 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17231 {
17232 tree type = TREE_TYPE (elt);
17233 /* It should only be possible to get here for a call. */
17234 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17235 elt_strict |= maybe_adjust_types_for_deduction
17236 (DEDUCE_CALL, &elttype, &type, elt);
17237 elt = type;
17238 }
17239
17240 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17241 explain_p);
17242 }
17243
17244 if (TREE_CODE (parm) == ARRAY_TYPE)
17245 {
17246 /* Also deduce from the length of the initializer list. */
17247 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17248 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17249 if (TYPE_DOMAIN (parm) != NULL_TREE)
17250 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17251 idx, explain_p);
17252 }
17253
17254 /* If the std::initializer_list<T> deduction worked, replace the
17255 deduced A with std::initializer_list<A>. */
17256 if (orig_parm != parm)
17257 {
17258 idx = TEMPLATE_TYPE_IDX (orig_parm);
17259 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17260 targ = listify (targ);
17261 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17262 }
17263 return unify_success (explain_p);
17264 }
17265
17266 /* Immediately reject some pairs that won't unify because of
17267 cv-qualification mismatches. */
17268 if (TREE_CODE (arg) == TREE_CODE (parm)
17269 && TYPE_P (arg)
17270 /* It is the elements of the array which hold the cv quals of an array
17271 type, and the elements might be template type parms. We'll check
17272 when we recurse. */
17273 && TREE_CODE (arg) != ARRAY_TYPE
17274 /* We check the cv-qualifiers when unifying with template type
17275 parameters below. We want to allow ARG `const T' to unify with
17276 PARM `T' for example, when computing which of two templates
17277 is more specialized, for example. */
17278 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17279 && !check_cv_quals_for_unify (strict_in, arg, parm))
17280 return unify_cv_qual_mismatch (explain_p, parm, arg);
17281
17282 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17283 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17284 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17285 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17286 strict &= ~UNIFY_ALLOW_DERIVED;
17287 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17288 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17289
17290 switch (TREE_CODE (parm))
17291 {
17292 case TYPENAME_TYPE:
17293 case SCOPE_REF:
17294 case UNBOUND_CLASS_TEMPLATE:
17295 /* In a type which contains a nested-name-specifier, template
17296 argument values cannot be deduced for template parameters used
17297 within the nested-name-specifier. */
17298 return unify_success (explain_p);
17299
17300 case TEMPLATE_TYPE_PARM:
17301 case TEMPLATE_TEMPLATE_PARM:
17302 case BOUND_TEMPLATE_TEMPLATE_PARM:
17303 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17304 if (tparm == error_mark_node)
17305 return unify_invalid (explain_p);
17306
17307 if (TEMPLATE_TYPE_LEVEL (parm)
17308 != template_decl_level (tparm))
17309 /* The PARM is not one we're trying to unify. Just check
17310 to see if it matches ARG. */
17311 {
17312 if (TREE_CODE (arg) == TREE_CODE (parm)
17313 && (is_auto (parm) ? is_auto (arg)
17314 : same_type_p (parm, arg)))
17315 return unify_success (explain_p);
17316 else
17317 return unify_type_mismatch (explain_p, parm, arg);
17318 }
17319 idx = TEMPLATE_TYPE_IDX (parm);
17320 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17321 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17322 if (tparm == error_mark_node)
17323 return unify_invalid (explain_p);
17324
17325 /* Check for mixed types and values. */
17326 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17327 && TREE_CODE (tparm) != TYPE_DECL)
17328 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17329 && TREE_CODE (tparm) != TEMPLATE_DECL))
17330 gcc_unreachable ();
17331
17332 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17333 {
17334 /* ARG must be constructed from a template class or a template
17335 template parameter. */
17336 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17337 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17338 return unify_template_deduction_failure (explain_p, parm, arg);
17339
17340 {
17341 tree parmvec = TYPE_TI_ARGS (parm);
17342 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17343 tree full_argvec = add_to_template_args (targs, argvec);
17344 tree parm_parms
17345 = DECL_INNERMOST_TEMPLATE_PARMS
17346 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17347 int i, len;
17348 int parm_variadic_p = 0;
17349
17350 /* The resolution to DR150 makes clear that default
17351 arguments for an N-argument may not be used to bind T
17352 to a template template parameter with fewer than N
17353 parameters. It is not safe to permit the binding of
17354 default arguments as an extension, as that may change
17355 the meaning of a conforming program. Consider:
17356
17357 struct Dense { static const unsigned int dim = 1; };
17358
17359 template <template <typename> class View,
17360 typename Block>
17361 void operator+(float, View<Block> const&);
17362
17363 template <typename Block,
17364 unsigned int Dim = Block::dim>
17365 struct Lvalue_proxy { operator float() const; };
17366
17367 void
17368 test_1d (void) {
17369 Lvalue_proxy<Dense> p;
17370 float b;
17371 b + p;
17372 }
17373
17374 Here, if Lvalue_proxy is permitted to bind to View, then
17375 the global operator+ will be used; if they are not, the
17376 Lvalue_proxy will be converted to float. */
17377 if (coerce_template_parms (parm_parms,
17378 full_argvec,
17379 TYPE_TI_TEMPLATE (parm),
17380 (explain_p
17381 ? tf_warning_or_error
17382 : tf_none),
17383 /*require_all_args=*/true,
17384 /*use_default_args=*/false)
17385 == error_mark_node)
17386 return 1;
17387
17388 /* Deduce arguments T, i from TT<T> or TT<i>.
17389 We check each element of PARMVEC and ARGVEC individually
17390 rather than the whole TREE_VEC since they can have
17391 different number of elements. */
17392
17393 parmvec = expand_template_argument_pack (parmvec);
17394 argvec = expand_template_argument_pack (argvec);
17395
17396 len = TREE_VEC_LENGTH (parmvec);
17397
17398 /* Check if the parameters end in a pack, making them
17399 variadic. */
17400 if (len > 0
17401 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17402 parm_variadic_p = 1;
17403
17404 for (i = 0; i < len - parm_variadic_p; ++i)
17405 /* If the template argument list of P contains a pack
17406 expansion that is not the last template argument, the
17407 entire template argument list is a non-deduced
17408 context. */
17409 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17410 return unify_success (explain_p);
17411
17412 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17413 return unify_too_few_arguments (explain_p,
17414 TREE_VEC_LENGTH (argvec), len);
17415
17416 for (i = 0; i < len - parm_variadic_p; ++i)
17417 {
17418 RECUR_AND_CHECK_FAILURE (tparms, targs,
17419 TREE_VEC_ELT (parmvec, i),
17420 TREE_VEC_ELT (argvec, i),
17421 UNIFY_ALLOW_NONE, explain_p);
17422 }
17423
17424 if (parm_variadic_p
17425 && unify_pack_expansion (tparms, targs,
17426 parmvec, argvec,
17427 DEDUCE_EXACT,
17428 /*subr=*/true, explain_p))
17429 return 1;
17430 }
17431 arg = TYPE_TI_TEMPLATE (arg);
17432
17433 /* Fall through to deduce template name. */
17434 }
17435
17436 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17437 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17438 {
17439 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17440
17441 /* Simple cases: Value already set, does match or doesn't. */
17442 if (targ != NULL_TREE && template_args_equal (targ, arg))
17443 return unify_success (explain_p);
17444 else if (targ)
17445 return unify_inconsistency (explain_p, parm, targ, arg);
17446 }
17447 else
17448 {
17449 /* If PARM is `const T' and ARG is only `int', we don't have
17450 a match unless we are allowing additional qualification.
17451 If ARG is `const int' and PARM is just `T' that's OK;
17452 that binds `const int' to `T'. */
17453 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17454 arg, parm))
17455 return unify_cv_qual_mismatch (explain_p, parm, arg);
17456
17457 /* Consider the case where ARG is `const volatile int' and
17458 PARM is `const T'. Then, T should be `volatile int'. */
17459 arg = cp_build_qualified_type_real
17460 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17461 if (arg == error_mark_node)
17462 return unify_invalid (explain_p);
17463
17464 /* Simple cases: Value already set, does match or doesn't. */
17465 if (targ != NULL_TREE && same_type_p (targ, arg))
17466 return unify_success (explain_p);
17467 else if (targ)
17468 return unify_inconsistency (explain_p, parm, targ, arg);
17469
17470 /* Make sure that ARG is not a variable-sized array. (Note
17471 that were talking about variable-sized arrays (like
17472 `int[n]'), rather than arrays of unknown size (like
17473 `int[]').) We'll get very confused by such a type since
17474 the bound of the array is not constant, and therefore
17475 not mangleable. Besides, such types are not allowed in
17476 ISO C++, so we can do as we please here. We do allow
17477 them for 'auto' deduction, since that isn't ABI-exposed. */
17478 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17479 return unify_vla_arg (explain_p, arg);
17480
17481 /* Strip typedefs as in convert_template_argument. */
17482 arg = canonicalize_type_argument (arg, tf_none);
17483 }
17484
17485 /* If ARG is a parameter pack or an expansion, we cannot unify
17486 against it unless PARM is also a parameter pack. */
17487 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17488 && !template_parameter_pack_p (parm))
17489 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17490
17491 /* If the argument deduction results is a METHOD_TYPE,
17492 then there is a problem.
17493 METHOD_TYPE doesn't map to any real C++ type the result of
17494 the deduction can not be of that type. */
17495 if (TREE_CODE (arg) == METHOD_TYPE)
17496 return unify_method_type_error (explain_p, arg);
17497
17498 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17499 return unify_success (explain_p);
17500
17501 case TEMPLATE_PARM_INDEX:
17502 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17503 if (tparm == error_mark_node)
17504 return unify_invalid (explain_p);
17505
17506 if (TEMPLATE_PARM_LEVEL (parm)
17507 != template_decl_level (tparm))
17508 {
17509 /* The PARM is not one we're trying to unify. Just check
17510 to see if it matches ARG. */
17511 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17512 && cp_tree_equal (parm, arg));
17513 if (result)
17514 unify_expression_unequal (explain_p, parm, arg);
17515 return result;
17516 }
17517
17518 idx = TEMPLATE_PARM_IDX (parm);
17519 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17520
17521 if (targ)
17522 {
17523 int x = !cp_tree_equal (targ, arg);
17524 if (x)
17525 unify_inconsistency (explain_p, parm, targ, arg);
17526 return x;
17527 }
17528
17529 /* [temp.deduct.type] If, in the declaration of a function template
17530 with a non-type template-parameter, the non-type
17531 template-parameter is used in an expression in the function
17532 parameter-list and, if the corresponding template-argument is
17533 deduced, the template-argument type shall match the type of the
17534 template-parameter exactly, except that a template-argument
17535 deduced from an array bound may be of any integral type.
17536 The non-type parameter might use already deduced type parameters. */
17537 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17538 if (!TREE_TYPE (arg))
17539 /* Template-parameter dependent expression. Just accept it for now.
17540 It will later be processed in convert_template_argument. */
17541 ;
17542 else if (same_type_p (TREE_TYPE (arg), tparm))
17543 /* OK */;
17544 else if ((strict & UNIFY_ALLOW_INTEGER)
17545 && CP_INTEGRAL_TYPE_P (tparm))
17546 /* Convert the ARG to the type of PARM; the deduced non-type
17547 template argument must exactly match the types of the
17548 corresponding parameter. */
17549 arg = fold (build_nop (tparm, arg));
17550 else if (uses_template_parms (tparm))
17551 /* We haven't deduced the type of this parameter yet. Try again
17552 later. */
17553 return unify_success (explain_p);
17554 else
17555 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17556
17557 /* If ARG is a parameter pack or an expansion, we cannot unify
17558 against it unless PARM is also a parameter pack. */
17559 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17560 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17561 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17562
17563 arg = strip_typedefs_expr (arg);
17564 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17565 return unify_success (explain_p);
17566
17567 case PTRMEM_CST:
17568 {
17569 /* A pointer-to-member constant can be unified only with
17570 another constant. */
17571 if (TREE_CODE (arg) != PTRMEM_CST)
17572 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17573
17574 /* Just unify the class member. It would be useless (and possibly
17575 wrong, depending on the strict flags) to unify also
17576 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17577 arg refer to the same variable, even if through different
17578 classes. For instance:
17579
17580 struct A { int x; };
17581 struct B : A { };
17582
17583 Unification of &A::x and &B::x must succeed. */
17584 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17585 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17586 }
17587
17588 case POINTER_TYPE:
17589 {
17590 if (!TYPE_PTR_P (arg))
17591 return unify_type_mismatch (explain_p, parm, arg);
17592
17593 /* [temp.deduct.call]
17594
17595 A can be another pointer or pointer to member type that can
17596 be converted to the deduced A via a qualification
17597 conversion (_conv.qual_).
17598
17599 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17600 This will allow for additional cv-qualification of the
17601 pointed-to types if appropriate. */
17602
17603 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17604 /* The derived-to-base conversion only persists through one
17605 level of pointers. */
17606 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17607
17608 return unify (tparms, targs, TREE_TYPE (parm),
17609 TREE_TYPE (arg), strict, explain_p);
17610 }
17611
17612 case REFERENCE_TYPE:
17613 if (TREE_CODE (arg) != REFERENCE_TYPE)
17614 return unify_type_mismatch (explain_p, parm, arg);
17615 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17616 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17617
17618 case ARRAY_TYPE:
17619 if (TREE_CODE (arg) != ARRAY_TYPE)
17620 return unify_type_mismatch (explain_p, parm, arg);
17621 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17622 != (TYPE_DOMAIN (arg) == NULL_TREE))
17623 return unify_type_mismatch (explain_p, parm, arg);
17624 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17625 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17626 if (TYPE_DOMAIN (parm) != NULL_TREE)
17627 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17628 TYPE_DOMAIN (arg), explain_p);
17629 return unify_success (explain_p);
17630
17631 case REAL_TYPE:
17632 case COMPLEX_TYPE:
17633 case VECTOR_TYPE:
17634 case INTEGER_TYPE:
17635 case BOOLEAN_TYPE:
17636 case ENUMERAL_TYPE:
17637 case VOID_TYPE:
17638 case NULLPTR_TYPE:
17639 if (TREE_CODE (arg) != TREE_CODE (parm))
17640 return unify_type_mismatch (explain_p, parm, arg);
17641
17642 /* We have already checked cv-qualification at the top of the
17643 function. */
17644 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17645 return unify_type_mismatch (explain_p, parm, arg);
17646
17647 /* As far as unification is concerned, this wins. Later checks
17648 will invalidate it if necessary. */
17649 return unify_success (explain_p);
17650
17651 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17652 /* Type INTEGER_CST can come from ordinary constant template args. */
17653 case INTEGER_CST:
17654 while (TREE_CODE (arg) == NOP_EXPR)
17655 arg = TREE_OPERAND (arg, 0);
17656
17657 if (TREE_CODE (arg) != INTEGER_CST)
17658 return unify_template_argument_mismatch (explain_p, parm, arg);
17659 return (tree_int_cst_equal (parm, arg)
17660 ? unify_success (explain_p)
17661 : unify_template_argument_mismatch (explain_p, parm, arg));
17662
17663 case TREE_VEC:
17664 {
17665 int i, len, argslen;
17666 int parm_variadic_p = 0;
17667
17668 if (TREE_CODE (arg) != TREE_VEC)
17669 return unify_template_argument_mismatch (explain_p, parm, arg);
17670
17671 len = TREE_VEC_LENGTH (parm);
17672 argslen = TREE_VEC_LENGTH (arg);
17673
17674 /* Check for pack expansions in the parameters. */
17675 for (i = 0; i < len; ++i)
17676 {
17677 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17678 {
17679 if (i == len - 1)
17680 /* We can unify against something with a trailing
17681 parameter pack. */
17682 parm_variadic_p = 1;
17683 else
17684 /* [temp.deduct.type]/9: If the template argument list of
17685 P contains a pack expansion that is not the last
17686 template argument, the entire template argument list
17687 is a non-deduced context. */
17688 return unify_success (explain_p);
17689 }
17690 }
17691
17692 /* If we don't have enough arguments to satisfy the parameters
17693 (not counting the pack expression at the end), or we have
17694 too many arguments for a parameter list that doesn't end in
17695 a pack expression, we can't unify. */
17696 if (parm_variadic_p
17697 ? argslen < len - parm_variadic_p
17698 : argslen != len)
17699 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17700
17701 /* Unify all of the parameters that precede the (optional)
17702 pack expression. */
17703 for (i = 0; i < len - parm_variadic_p; ++i)
17704 {
17705 RECUR_AND_CHECK_FAILURE (tparms, targs,
17706 TREE_VEC_ELT (parm, i),
17707 TREE_VEC_ELT (arg, i),
17708 UNIFY_ALLOW_NONE, explain_p);
17709 }
17710 if (parm_variadic_p)
17711 return unify_pack_expansion (tparms, targs, parm, arg,
17712 DEDUCE_EXACT,
17713 /*subr=*/true, explain_p);
17714 return unify_success (explain_p);
17715 }
17716
17717 case RECORD_TYPE:
17718 case UNION_TYPE:
17719 if (TREE_CODE (arg) != TREE_CODE (parm))
17720 return unify_type_mismatch (explain_p, parm, arg);
17721
17722 if (TYPE_PTRMEMFUNC_P (parm))
17723 {
17724 if (!TYPE_PTRMEMFUNC_P (arg))
17725 return unify_type_mismatch (explain_p, parm, arg);
17726
17727 return unify (tparms, targs,
17728 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17729 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17730 strict, explain_p);
17731 }
17732
17733 if (CLASSTYPE_TEMPLATE_INFO (parm))
17734 {
17735 tree t = NULL_TREE;
17736
17737 if (strict_in & UNIFY_ALLOW_DERIVED)
17738 {
17739 /* First, we try to unify the PARM and ARG directly. */
17740 t = try_class_unification (tparms, targs,
17741 parm, arg, explain_p);
17742
17743 if (!t)
17744 {
17745 /* Fallback to the special case allowed in
17746 [temp.deduct.call]:
17747
17748 If P is a class, and P has the form
17749 template-id, then A can be a derived class of
17750 the deduced A. Likewise, if P is a pointer to
17751 a class of the form template-id, A can be a
17752 pointer to a derived class pointed to by the
17753 deduced A. */
17754 enum template_base_result r;
17755 r = get_template_base (tparms, targs, parm, arg,
17756 explain_p, &t);
17757
17758 if (!t)
17759 return unify_no_common_base (explain_p, r, parm, arg);
17760 }
17761 }
17762 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17763 && (CLASSTYPE_TI_TEMPLATE (parm)
17764 == CLASSTYPE_TI_TEMPLATE (arg)))
17765 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17766 Then, we should unify `int' and `U'. */
17767 t = arg;
17768 else
17769 /* There's no chance of unification succeeding. */
17770 return unify_type_mismatch (explain_p, parm, arg);
17771
17772 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17773 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17774 }
17775 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17776 return unify_type_mismatch (explain_p, parm, arg);
17777 return unify_success (explain_p);
17778
17779 case METHOD_TYPE:
17780 case FUNCTION_TYPE:
17781 {
17782 unsigned int nargs;
17783 tree *args;
17784 tree a;
17785 unsigned int i;
17786
17787 if (TREE_CODE (arg) != TREE_CODE (parm))
17788 return unify_type_mismatch (explain_p, parm, arg);
17789
17790 /* CV qualifications for methods can never be deduced, they must
17791 match exactly. We need to check them explicitly here,
17792 because type_unification_real treats them as any other
17793 cv-qualified parameter. */
17794 if (TREE_CODE (parm) == METHOD_TYPE
17795 && (!check_cv_quals_for_unify
17796 (UNIFY_ALLOW_NONE,
17797 class_of_this_parm (arg),
17798 class_of_this_parm (parm))))
17799 return unify_cv_qual_mismatch (explain_p, parm, arg);
17800
17801 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17802 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17803
17804 nargs = list_length (TYPE_ARG_TYPES (arg));
17805 args = XALLOCAVEC (tree, nargs);
17806 for (a = TYPE_ARG_TYPES (arg), i = 0;
17807 a != NULL_TREE && a != void_list_node;
17808 a = TREE_CHAIN (a), ++i)
17809 args[i] = TREE_VALUE (a);
17810 nargs = i;
17811
17812 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17813 args, nargs, 1, DEDUCE_EXACT,
17814 LOOKUP_NORMAL, NULL, explain_p);
17815 }
17816
17817 case OFFSET_TYPE:
17818 /* Unify a pointer to member with a pointer to member function, which
17819 deduces the type of the member as a function type. */
17820 if (TYPE_PTRMEMFUNC_P (arg))
17821 {
17822 /* Check top-level cv qualifiers */
17823 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17824 return unify_cv_qual_mismatch (explain_p, parm, arg);
17825
17826 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17827 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17828 UNIFY_ALLOW_NONE, explain_p);
17829
17830 /* Determine the type of the function we are unifying against. */
17831 tree fntype = static_fn_type (arg);
17832
17833 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17834 }
17835
17836 if (TREE_CODE (arg) != OFFSET_TYPE)
17837 return unify_type_mismatch (explain_p, parm, arg);
17838 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17839 TYPE_OFFSET_BASETYPE (arg),
17840 UNIFY_ALLOW_NONE, explain_p);
17841 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17842 strict, explain_p);
17843
17844 case CONST_DECL:
17845 if (DECL_TEMPLATE_PARM_P (parm))
17846 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17847 if (arg != integral_constant_value (parm))
17848 return unify_template_argument_mismatch (explain_p, parm, arg);
17849 return unify_success (explain_p);
17850
17851 case FIELD_DECL:
17852 case TEMPLATE_DECL:
17853 /* Matched cases are handled by the ARG == PARM test above. */
17854 return unify_template_argument_mismatch (explain_p, parm, arg);
17855
17856 case VAR_DECL:
17857 /* A non-type template parameter that is a variable should be a
17858 an integral constant, in which case, it whould have been
17859 folded into its (constant) value. So we should not be getting
17860 a variable here. */
17861 gcc_unreachable ();
17862
17863 case TYPE_ARGUMENT_PACK:
17864 case NONTYPE_ARGUMENT_PACK:
17865 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17866 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17867
17868 case TYPEOF_TYPE:
17869 case DECLTYPE_TYPE:
17870 case UNDERLYING_TYPE:
17871 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17872 or UNDERLYING_TYPE nodes. */
17873 return unify_success (explain_p);
17874
17875 case ERROR_MARK:
17876 /* Unification fails if we hit an error node. */
17877 return unify_invalid (explain_p);
17878
17879 default:
17880 /* An unresolved overload is a nondeduced context. */
17881 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17882 return unify_success (explain_p);
17883 gcc_assert (EXPR_P (parm));
17884
17885 /* We must be looking at an expression. This can happen with
17886 something like:
17887
17888 template <int I>
17889 void foo(S<I>, S<I + 2>);
17890
17891 This is a "nondeduced context":
17892
17893 [deduct.type]
17894
17895 The nondeduced contexts are:
17896
17897 --A type that is a template-id in which one or more of
17898 the template-arguments is an expression that references
17899 a template-parameter.
17900
17901 In these cases, we assume deduction succeeded, but don't
17902 actually infer any unifications. */
17903
17904 if (!uses_template_parms (parm)
17905 && !template_args_equal (parm, arg))
17906 return unify_expression_unequal (explain_p, parm, arg);
17907 else
17908 return unify_success (explain_p);
17909 }
17910 }
17911 #undef RECUR_AND_CHECK_FAILURE
17912 \f
17913 /* Note that DECL can be defined in this translation unit, if
17914 required. */
17915
17916 static void
17917 mark_definable (tree decl)
17918 {
17919 tree clone;
17920 DECL_NOT_REALLY_EXTERN (decl) = 1;
17921 FOR_EACH_CLONE (clone, decl)
17922 DECL_NOT_REALLY_EXTERN (clone) = 1;
17923 }
17924
17925 /* Called if RESULT is explicitly instantiated, or is a member of an
17926 explicitly instantiated class. */
17927
17928 void
17929 mark_decl_instantiated (tree result, int extern_p)
17930 {
17931 SET_DECL_EXPLICIT_INSTANTIATION (result);
17932
17933 /* If this entity has already been written out, it's too late to
17934 make any modifications. */
17935 if (TREE_ASM_WRITTEN (result))
17936 return;
17937
17938 /* For anonymous namespace we don't need to do anything. */
17939 if (decl_anon_ns_mem_p (result))
17940 {
17941 gcc_assert (!TREE_PUBLIC (result));
17942 return;
17943 }
17944
17945 if (TREE_CODE (result) != FUNCTION_DECL)
17946 /* The TREE_PUBLIC flag for function declarations will have been
17947 set correctly by tsubst. */
17948 TREE_PUBLIC (result) = 1;
17949
17950 /* This might have been set by an earlier implicit instantiation. */
17951 DECL_COMDAT (result) = 0;
17952
17953 if (extern_p)
17954 DECL_NOT_REALLY_EXTERN (result) = 0;
17955 else
17956 {
17957 mark_definable (result);
17958 /* Always make artificials weak. */
17959 if (DECL_ARTIFICIAL (result) && flag_weak)
17960 comdat_linkage (result);
17961 /* For WIN32 we also want to put explicit instantiations in
17962 linkonce sections. */
17963 else if (TREE_PUBLIC (result))
17964 maybe_make_one_only (result);
17965 }
17966
17967 /* If EXTERN_P, then this function will not be emitted -- unless
17968 followed by an explicit instantiation, at which point its linkage
17969 will be adjusted. If !EXTERN_P, then this function will be
17970 emitted here. In neither circumstance do we want
17971 import_export_decl to adjust the linkage. */
17972 DECL_INTERFACE_KNOWN (result) = 1;
17973 }
17974
17975 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17976 important template arguments. If any are missing, we check whether
17977 they're important by using error_mark_node for substituting into any
17978 args that were used for partial ordering (the ones between ARGS and END)
17979 and seeing if it bubbles up. */
17980
17981 static bool
17982 check_undeduced_parms (tree targs, tree args, tree end)
17983 {
17984 bool found = false;
17985 int i;
17986 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17987 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17988 {
17989 found = true;
17990 TREE_VEC_ELT (targs, i) = error_mark_node;
17991 }
17992 if (found)
17993 {
17994 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17995 if (substed == error_mark_node)
17996 return true;
17997 }
17998 return false;
17999 }
18000
18001 /* Given two function templates PAT1 and PAT2, return:
18002
18003 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
18004 -1 if PAT2 is more specialized than PAT1.
18005 0 if neither is more specialized.
18006
18007 LEN indicates the number of parameters we should consider
18008 (defaulted parameters should not be considered).
18009
18010 The 1998 std underspecified function template partial ordering, and
18011 DR214 addresses the issue. We take pairs of arguments, one from
18012 each of the templates, and deduce them against each other. One of
18013 the templates will be more specialized if all the *other*
18014 template's arguments deduce against its arguments and at least one
18015 of its arguments *does* *not* deduce against the other template's
18016 corresponding argument. Deduction is done as for class templates.
18017 The arguments used in deduction have reference and top level cv
18018 qualifiers removed. Iff both arguments were originally reference
18019 types *and* deduction succeeds in both directions, an lvalue reference
18020 wins against an rvalue reference and otherwise the template
18021 with the more cv-qualified argument wins for that pairing (if
18022 neither is more cv-qualified, they both are equal). Unlike regular
18023 deduction, after all the arguments have been deduced in this way,
18024 we do *not* verify the deduced template argument values can be
18025 substituted into non-deduced contexts.
18026
18027 The logic can be a bit confusing here, because we look at deduce1 and
18028 targs1 to see if pat2 is at least as specialized, and vice versa; if we
18029 can find template arguments for pat1 to make arg1 look like arg2, that
18030 means that arg2 is at least as specialized as arg1. */
18031
18032 int
18033 more_specialized_fn (tree pat1, tree pat2, int len)
18034 {
18035 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
18036 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
18037 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
18038 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
18039 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
18040 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
18041 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
18042 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18043 tree origs1, origs2;
18044 bool lose1 = false;
18045 bool lose2 = false;
18046
18047 /* Remove the this parameter from non-static member functions. If
18048 one is a non-static member function and the other is not a static
18049 member function, remove the first parameter from that function
18050 also. This situation occurs for operator functions where we
18051 locate both a member function (with this pointer) and non-member
18052 operator (with explicit first operand). */
18053 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18054 {
18055 len--; /* LEN is the number of significant arguments for DECL1 */
18056 args1 = TREE_CHAIN (args1);
18057 if (!DECL_STATIC_FUNCTION_P (decl2))
18058 args2 = TREE_CHAIN (args2);
18059 }
18060 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18061 {
18062 args2 = TREE_CHAIN (args2);
18063 if (!DECL_STATIC_FUNCTION_P (decl1))
18064 {
18065 len--;
18066 args1 = TREE_CHAIN (args1);
18067 }
18068 }
18069
18070 /* If only one is a conversion operator, they are unordered. */
18071 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18072 return 0;
18073
18074 /* Consider the return type for a conversion function */
18075 if (DECL_CONV_FN_P (decl1))
18076 {
18077 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18078 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18079 len++;
18080 }
18081
18082 processing_template_decl++;
18083
18084 origs1 = args1;
18085 origs2 = args2;
18086
18087 while (len--
18088 /* Stop when an ellipsis is seen. */
18089 && args1 != NULL_TREE && args2 != NULL_TREE)
18090 {
18091 tree arg1 = TREE_VALUE (args1);
18092 tree arg2 = TREE_VALUE (args2);
18093 int deduce1, deduce2;
18094 int quals1 = -1;
18095 int quals2 = -1;
18096 int ref1 = 0;
18097 int ref2 = 0;
18098
18099 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18100 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18101 {
18102 /* When both arguments are pack expansions, we need only
18103 unify the patterns themselves. */
18104 arg1 = PACK_EXPANSION_PATTERN (arg1);
18105 arg2 = PACK_EXPANSION_PATTERN (arg2);
18106
18107 /* This is the last comparison we need to do. */
18108 len = 0;
18109 }
18110
18111 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18112 {
18113 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18114 arg1 = TREE_TYPE (arg1);
18115 quals1 = cp_type_quals (arg1);
18116 }
18117
18118 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18119 {
18120 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18121 arg2 = TREE_TYPE (arg2);
18122 quals2 = cp_type_quals (arg2);
18123 }
18124
18125 arg1 = TYPE_MAIN_VARIANT (arg1);
18126 arg2 = TYPE_MAIN_VARIANT (arg2);
18127
18128 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18129 {
18130 int i, len2 = list_length (args2);
18131 tree parmvec = make_tree_vec (1);
18132 tree argvec = make_tree_vec (len2);
18133 tree ta = args2;
18134
18135 /* Setup the parameter vector, which contains only ARG1. */
18136 TREE_VEC_ELT (parmvec, 0) = arg1;
18137
18138 /* Setup the argument vector, which contains the remaining
18139 arguments. */
18140 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18141 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18142
18143 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18144 argvec, DEDUCE_EXACT,
18145 /*subr=*/true, /*explain_p=*/false)
18146 == 0);
18147
18148 /* We cannot deduce in the other direction, because ARG1 is
18149 a pack expansion but ARG2 is not. */
18150 deduce2 = 0;
18151 }
18152 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18153 {
18154 int i, len1 = list_length (args1);
18155 tree parmvec = make_tree_vec (1);
18156 tree argvec = make_tree_vec (len1);
18157 tree ta = args1;
18158
18159 /* Setup the parameter vector, which contains only ARG1. */
18160 TREE_VEC_ELT (parmvec, 0) = arg2;
18161
18162 /* Setup the argument vector, which contains the remaining
18163 arguments. */
18164 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18165 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18166
18167 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18168 argvec, DEDUCE_EXACT,
18169 /*subr=*/true, /*explain_p=*/false)
18170 == 0);
18171
18172 /* We cannot deduce in the other direction, because ARG2 is
18173 a pack expansion but ARG1 is not.*/
18174 deduce1 = 0;
18175 }
18176
18177 else
18178 {
18179 /* The normal case, where neither argument is a pack
18180 expansion. */
18181 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18182 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18183 == 0);
18184 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18185 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18186 == 0);
18187 }
18188
18189 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18190 arg2, then arg2 is not as specialized as arg1. */
18191 if (!deduce1)
18192 lose2 = true;
18193 if (!deduce2)
18194 lose1 = true;
18195
18196 /* "If, for a given type, deduction succeeds in both directions
18197 (i.e., the types are identical after the transformations above)
18198 and both P and A were reference types (before being replaced with
18199 the type referred to above):
18200 - if the type from the argument template was an lvalue reference and
18201 the type from the parameter template was not, the argument type is
18202 considered to be more specialized than the other; otherwise,
18203 - if the type from the argument template is more cv-qualified
18204 than the type from the parameter template (as described above),
18205 the argument type is considered to be more specialized than the other;
18206 otherwise,
18207 - neither type is more specialized than the other." */
18208
18209 if (deduce1 && deduce2)
18210 {
18211 if (ref1 && ref2 && ref1 != ref2)
18212 {
18213 if (ref1 > ref2)
18214 lose1 = true;
18215 else
18216 lose2 = true;
18217 }
18218 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18219 {
18220 if ((quals1 & quals2) == quals2)
18221 lose2 = true;
18222 if ((quals1 & quals2) == quals1)
18223 lose1 = true;
18224 }
18225 }
18226
18227 if (lose1 && lose2)
18228 /* We've failed to deduce something in either direction.
18229 These must be unordered. */
18230 break;
18231
18232 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18233 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18234 /* We have already processed all of the arguments in our
18235 handing of the pack expansion type. */
18236 len = 0;
18237
18238 args1 = TREE_CHAIN (args1);
18239 args2 = TREE_CHAIN (args2);
18240 }
18241
18242 /* "In most cases, all template parameters must have values in order for
18243 deduction to succeed, but for partial ordering purposes a template
18244 parameter may remain without a value provided it is not used in the
18245 types being used for partial ordering."
18246
18247 Thus, if we are missing any of the targs1 we need to substitute into
18248 origs1, then pat2 is not as specialized as pat1. This can happen when
18249 there is a nondeduced context. */
18250 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18251 lose2 = true;
18252 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18253 lose1 = true;
18254
18255 processing_template_decl--;
18256
18257 /* All things being equal, if the next argument is a pack expansion
18258 for one function but not for the other, prefer the
18259 non-variadic function. FIXME this is bogus; see c++/41958. */
18260 if (lose1 == lose2
18261 && args1 && TREE_VALUE (args1)
18262 && args2 && TREE_VALUE (args2))
18263 {
18264 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18265 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18266 }
18267
18268 if (lose1 == lose2)
18269 return 0;
18270 else if (!lose1)
18271 return 1;
18272 else
18273 return -1;
18274 }
18275
18276 /* Determine which of two partial specializations of TMPL is more
18277 specialized.
18278
18279 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18280 to the first partial specialization. The TREE_VALUE is the
18281 innermost set of template parameters for the partial
18282 specialization. PAT2 is similar, but for the second template.
18283
18284 Return 1 if the first partial specialization is more specialized;
18285 -1 if the second is more specialized; 0 if neither is more
18286 specialized.
18287
18288 See [temp.class.order] for information about determining which of
18289 two templates is more specialized. */
18290
18291 static int
18292 more_specialized_class (tree tmpl, tree pat1, tree pat2)
18293 {
18294 tree targs;
18295 tree tmpl1, tmpl2;
18296 int winner = 0;
18297 bool any_deductions = false;
18298
18299 tmpl1 = TREE_TYPE (pat1);
18300 tmpl2 = TREE_TYPE (pat2);
18301
18302 /* Just like what happens for functions, if we are ordering between
18303 different class template specializations, we may encounter dependent
18304 types in the arguments, and we need our dependency check functions
18305 to behave correctly. */
18306 ++processing_template_decl;
18307 targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
18308 CLASSTYPE_TI_ARGS (tmpl1),
18309 CLASSTYPE_TI_ARGS (tmpl2));
18310 if (targs)
18311 {
18312 --winner;
18313 any_deductions = true;
18314 }
18315
18316 targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
18317 CLASSTYPE_TI_ARGS (tmpl2),
18318 CLASSTYPE_TI_ARGS (tmpl1));
18319 if (targs)
18320 {
18321 ++winner;
18322 any_deductions = true;
18323 }
18324 --processing_template_decl;
18325
18326 /* In the case of a tie where at least one of the class templates
18327 has a parameter pack at the end, the template with the most
18328 non-packed parameters wins. */
18329 if (winner == 0
18330 && any_deductions
18331 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18332 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18333 {
18334 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18335 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18336 int len1 = TREE_VEC_LENGTH (args1);
18337 int len2 = TREE_VEC_LENGTH (args2);
18338
18339 /* We don't count the pack expansion at the end. */
18340 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18341 --len1;
18342 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18343 --len2;
18344
18345 if (len1 > len2)
18346 return 1;
18347 else if (len1 < len2)
18348 return -1;
18349 }
18350
18351 return winner;
18352 }
18353
18354 /* Return the template arguments that will produce the function signature
18355 DECL from the function template FN, with the explicit template
18356 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18357 also match. Return NULL_TREE if no satisfactory arguments could be
18358 found. */
18359
18360 static tree
18361 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18362 {
18363 int ntparms = DECL_NTPARMS (fn);
18364 tree targs = make_tree_vec (ntparms);
18365 tree decl_type = TREE_TYPE (decl);
18366 tree decl_arg_types;
18367 tree *args;
18368 unsigned int nargs, ix;
18369 tree arg;
18370
18371 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18372
18373 /* Never do unification on the 'this' parameter. */
18374 decl_arg_types = skip_artificial_parms_for (decl,
18375 TYPE_ARG_TYPES (decl_type));
18376
18377 nargs = list_length (decl_arg_types);
18378 args = XALLOCAVEC (tree, nargs);
18379 for (arg = decl_arg_types, ix = 0;
18380 arg != NULL_TREE && arg != void_list_node;
18381 arg = TREE_CHAIN (arg), ++ix)
18382 args[ix] = TREE_VALUE (arg);
18383
18384 if (fn_type_unification (fn, explicit_args, targs,
18385 args, ix,
18386 (check_rettype || DECL_CONV_FN_P (fn)
18387 ? TREE_TYPE (decl_type) : NULL_TREE),
18388 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18389 /*decltype*/false)
18390 == error_mark_node)
18391 return NULL_TREE;
18392
18393 return targs;
18394 }
18395
18396 /* Return the innermost template arguments that, when applied to a partial
18397 specialization of TMPL whose innermost template parameters are
18398 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18399 ARGS.
18400
18401 For example, suppose we have:
18402
18403 template <class T, class U> struct S {};
18404 template <class T> struct S<T*, int> {};
18405
18406 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18407 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18408 int}. The resulting vector will be {double}, indicating that `T'
18409 is bound to `double'. */
18410
18411 static tree
18412 get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
18413 {
18414 int i, ntparms = TREE_VEC_LENGTH (tparms);
18415 tree deduced_args;
18416 tree innermost_deduced_args;
18417
18418 innermost_deduced_args = make_tree_vec (ntparms);
18419 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18420 {
18421 deduced_args = copy_node (args);
18422 SET_TMPL_ARGS_LEVEL (deduced_args,
18423 TMPL_ARGS_DEPTH (deduced_args),
18424 innermost_deduced_args);
18425 }
18426 else
18427 deduced_args = innermost_deduced_args;
18428
18429 if (unify (tparms, deduced_args,
18430 INNERMOST_TEMPLATE_ARGS (spec_args),
18431 INNERMOST_TEMPLATE_ARGS (args),
18432 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18433 return NULL_TREE;
18434
18435 for (i = 0; i < ntparms; ++i)
18436 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18437 return NULL_TREE;
18438
18439 /* Verify that nondeduced template arguments agree with the type
18440 obtained from argument deduction.
18441
18442 For example:
18443
18444 struct A { typedef int X; };
18445 template <class T, class U> struct C {};
18446 template <class T> struct C<T, typename T::X> {};
18447
18448 Then with the instantiation `C<A, int>', we can deduce that
18449 `T' is `A' but unify () does not check whether `typename T::X'
18450 is `int'. */
18451 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18452 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18453 spec_args, tmpl,
18454 tf_none, false, false);
18455 if (spec_args == error_mark_node
18456 /* We only need to check the innermost arguments; the other
18457 arguments will always agree. */
18458 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18459 INNERMOST_TEMPLATE_ARGS (args)))
18460 return NULL_TREE;
18461
18462 /* Now that we have bindings for all of the template arguments,
18463 ensure that the arguments deduced for the template template
18464 parameters have compatible template parameter lists. See the use
18465 of template_template_parm_bindings_ok_p in fn_type_unification
18466 for more information. */
18467 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18468 return NULL_TREE;
18469
18470 return deduced_args;
18471 }
18472
18473 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18474 Return the TREE_LIST node with the most specialized template, if
18475 any. If there is no most specialized template, the error_mark_node
18476 is returned.
18477
18478 Note that this function does not look at, or modify, the
18479 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18480 returned is one of the elements of INSTANTIATIONS, callers may
18481 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18482 and retrieve it from the value returned. */
18483
18484 tree
18485 most_specialized_instantiation (tree templates)
18486 {
18487 tree fn, champ;
18488
18489 ++processing_template_decl;
18490
18491 champ = templates;
18492 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18493 {
18494 int fate = 0;
18495
18496 if (get_bindings (TREE_VALUE (champ),
18497 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18498 NULL_TREE, /*check_ret=*/true))
18499 fate--;
18500
18501 if (get_bindings (TREE_VALUE (fn),
18502 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18503 NULL_TREE, /*check_ret=*/true))
18504 fate++;
18505
18506 if (fate == -1)
18507 champ = fn;
18508 else if (!fate)
18509 {
18510 /* Equally specialized, move to next function. If there
18511 is no next function, nothing's most specialized. */
18512 fn = TREE_CHAIN (fn);
18513 champ = fn;
18514 if (!fn)
18515 break;
18516 }
18517 }
18518
18519 if (champ)
18520 /* Now verify that champ is better than everything earlier in the
18521 instantiation list. */
18522 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18523 if (get_bindings (TREE_VALUE (champ),
18524 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18525 NULL_TREE, /*check_ret=*/true)
18526 || !get_bindings (TREE_VALUE (fn),
18527 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18528 NULL_TREE, /*check_ret=*/true))
18529 {
18530 champ = NULL_TREE;
18531 break;
18532 }
18533
18534 processing_template_decl--;
18535
18536 if (!champ)
18537 return error_mark_node;
18538
18539 return champ;
18540 }
18541
18542 /* If DECL is a specialization of some template, return the most
18543 general such template. Otherwise, returns NULL_TREE.
18544
18545 For example, given:
18546
18547 template <class T> struct S { template <class U> void f(U); };
18548
18549 if TMPL is `template <class U> void S<int>::f(U)' this will return
18550 the full template. This function will not trace past partial
18551 specializations, however. For example, given in addition:
18552
18553 template <class T> struct S<T*> { template <class U> void f(U); };
18554
18555 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18556 `template <class T> template <class U> S<T*>::f(U)'. */
18557
18558 tree
18559 most_general_template (tree decl)
18560 {
18561 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18562 an immediate specialization. */
18563 if (TREE_CODE (decl) == FUNCTION_DECL)
18564 {
18565 if (DECL_TEMPLATE_INFO (decl)) {
18566 decl = DECL_TI_TEMPLATE (decl);
18567
18568 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18569 template friend. */
18570 if (TREE_CODE (decl) != TEMPLATE_DECL)
18571 return NULL_TREE;
18572 } else
18573 return NULL_TREE;
18574 }
18575
18576 /* Look for more and more general templates. */
18577 while (DECL_TEMPLATE_INFO (decl))
18578 {
18579 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18580 (See cp-tree.h for details.) */
18581 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18582 break;
18583
18584 if (CLASS_TYPE_P (TREE_TYPE (decl))
18585 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18586 break;
18587
18588 /* Stop if we run into an explicitly specialized class template. */
18589 if (!DECL_NAMESPACE_SCOPE_P (decl)
18590 && DECL_CONTEXT (decl)
18591 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18592 break;
18593
18594 decl = DECL_TI_TEMPLATE (decl);
18595 }
18596
18597 return decl;
18598 }
18599
18600 /* Return the most specialized of the class template partial
18601 specializations which can produce TYPE, a specialization of some class
18602 template. The value returned is actually a TREE_LIST; the TREE_TYPE is
18603 a _TYPE node corresponding to the partial specialization, while the
18604 TREE_PURPOSE is the set of template arguments that must be
18605 substituted into the TREE_TYPE in order to generate TYPE.
18606
18607 If the choice of partial specialization is ambiguous, a diagnostic
18608 is issued, and the error_mark_node is returned. If there are no
18609 partial specializations matching TYPE, then NULL_TREE is
18610 returned, indicating that the primary template should be used. */
18611
18612 static tree
18613 most_specialized_class (tree type, tsubst_flags_t complain)
18614 {
18615 tree list = NULL_TREE;
18616 tree t;
18617 tree champ;
18618 int fate;
18619 bool ambiguous_p;
18620 tree outer_args = NULL_TREE;
18621
18622 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
18623 tree main_tmpl = most_general_template (tmpl);
18624 tree args = CLASSTYPE_TI_ARGS (type);
18625
18626 /* For determining which partial specialization to use, only the
18627 innermost args are interesting. */
18628 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18629 {
18630 outer_args = strip_innermost_template_args (args, 1);
18631 args = INNERMOST_TEMPLATE_ARGS (args);
18632 }
18633
18634 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
18635 {
18636 tree partial_spec_args;
18637 tree spec_args;
18638 tree spec_tmpl = TREE_VALUE (t);
18639 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18640
18641 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18642
18643 ++processing_template_decl;
18644
18645 if (outer_args)
18646 {
18647 /* Discard the outer levels of args, and then substitute in the
18648 template args from the enclosing class. */
18649 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18650 partial_spec_args = tsubst_template_args
18651 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18652
18653 /* And the same for the partial specialization TEMPLATE_DECL. */
18654 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18655 }
18656
18657 partial_spec_args =
18658 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18659 partial_spec_args,
18660 tmpl, tf_none,
18661 /*require_all_args=*/true,
18662 /*use_default_args=*/true);
18663
18664 --processing_template_decl;
18665
18666 if (partial_spec_args == error_mark_node)
18667 return error_mark_node;
18668 if (spec_tmpl == error_mark_node)
18669 return error_mark_node;
18670
18671 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18672 spec_args = get_class_bindings (tmpl, parms,
18673 partial_spec_args,
18674 args);
18675 if (spec_args)
18676 {
18677 if (outer_args)
18678 spec_args = add_to_template_args (outer_args, spec_args);
18679 list = tree_cons (spec_args, orig_parms, list);
18680 TREE_TYPE (list) = TREE_TYPE (t);
18681 }
18682 }
18683
18684 if (! list)
18685 return NULL_TREE;
18686
18687 ambiguous_p = false;
18688 t = list;
18689 champ = t;
18690 t = TREE_CHAIN (t);
18691 for (; t; t = TREE_CHAIN (t))
18692 {
18693 fate = more_specialized_class (tmpl, champ, t);
18694 if (fate == 1)
18695 ;
18696 else
18697 {
18698 if (fate == 0)
18699 {
18700 t = TREE_CHAIN (t);
18701 if (! t)
18702 {
18703 ambiguous_p = true;
18704 break;
18705 }
18706 }
18707 champ = t;
18708 }
18709 }
18710
18711 if (!ambiguous_p)
18712 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18713 {
18714 fate = more_specialized_class (tmpl, champ, t);
18715 if (fate != 1)
18716 {
18717 ambiguous_p = true;
18718 break;
18719 }
18720 }
18721
18722 if (ambiguous_p)
18723 {
18724 const char *str;
18725 char *spaces = NULL;
18726 if (!(complain & tf_error))
18727 return error_mark_node;
18728 error ("ambiguous class template instantiation for %q#T", type);
18729 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18730 for (t = list; t; t = TREE_CHAIN (t))
18731 {
18732 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18733 spaces = spaces ? spaces : get_spaces (str);
18734 }
18735 free (spaces);
18736 return error_mark_node;
18737 }
18738
18739 return champ;
18740 }
18741
18742 /* Explicitly instantiate DECL. */
18743
18744 void
18745 do_decl_instantiation (tree decl, tree storage)
18746 {
18747 tree result = NULL_TREE;
18748 int extern_p = 0;
18749
18750 if (!decl || decl == error_mark_node)
18751 /* An error occurred, for which grokdeclarator has already issued
18752 an appropriate message. */
18753 return;
18754 else if (! DECL_LANG_SPECIFIC (decl))
18755 {
18756 error ("explicit instantiation of non-template %q#D", decl);
18757 return;
18758 }
18759 else if (VAR_P (decl))
18760 {
18761 /* There is an asymmetry here in the way VAR_DECLs and
18762 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18763 the latter, the DECL we get back will be marked as a
18764 template instantiation, and the appropriate
18765 DECL_TEMPLATE_INFO will be set up. This does not happen for
18766 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18767 should handle VAR_DECLs as it currently handles
18768 FUNCTION_DECLs. */
18769 if (!DECL_CLASS_SCOPE_P (decl))
18770 {
18771 error ("%qD is not a static data member of a class template", decl);
18772 return;
18773 }
18774 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18775 if (!result || !VAR_P (result))
18776 {
18777 error ("no matching template for %qD found", decl);
18778 return;
18779 }
18780 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18781 {
18782 error ("type %qT for explicit instantiation %qD does not match "
18783 "declared type %qT", TREE_TYPE (result), decl,
18784 TREE_TYPE (decl));
18785 return;
18786 }
18787 }
18788 else if (TREE_CODE (decl) != FUNCTION_DECL)
18789 {
18790 error ("explicit instantiation of %q#D", decl);
18791 return;
18792 }
18793 else
18794 result = decl;
18795
18796 /* Check for various error cases. Note that if the explicit
18797 instantiation is valid the RESULT will currently be marked as an
18798 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18799 until we get here. */
18800
18801 if (DECL_TEMPLATE_SPECIALIZATION (result))
18802 {
18803 /* DR 259 [temp.spec].
18804
18805 Both an explicit instantiation and a declaration of an explicit
18806 specialization shall not appear in a program unless the explicit
18807 instantiation follows a declaration of the explicit specialization.
18808
18809 For a given set of template parameters, if an explicit
18810 instantiation of a template appears after a declaration of an
18811 explicit specialization for that template, the explicit
18812 instantiation has no effect. */
18813 return;
18814 }
18815 else if (DECL_EXPLICIT_INSTANTIATION (result))
18816 {
18817 /* [temp.spec]
18818
18819 No program shall explicitly instantiate any template more
18820 than once.
18821
18822 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18823 the first instantiation was `extern' and the second is not,
18824 and EXTERN_P for the opposite case. */
18825 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18826 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18827 /* If an "extern" explicit instantiation follows an ordinary
18828 explicit instantiation, the template is instantiated. */
18829 if (extern_p)
18830 return;
18831 }
18832 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18833 {
18834 error ("no matching template for %qD found", result);
18835 return;
18836 }
18837 else if (!DECL_TEMPLATE_INFO (result))
18838 {
18839 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18840 return;
18841 }
18842
18843 if (storage == NULL_TREE)
18844 ;
18845 else if (storage == ridpointers[(int) RID_EXTERN])
18846 {
18847 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
18848 pedwarn (input_location, OPT_Wpedantic,
18849 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18850 "instantiations");
18851 extern_p = 1;
18852 }
18853 else
18854 error ("storage class %qD applied to template instantiation", storage);
18855
18856 check_explicit_instantiation_namespace (result);
18857 mark_decl_instantiated (result, extern_p);
18858 if (! extern_p)
18859 instantiate_decl (result, /*defer_ok=*/1,
18860 /*expl_inst_class_mem_p=*/false);
18861 }
18862
18863 static void
18864 mark_class_instantiated (tree t, int extern_p)
18865 {
18866 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18867 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18868 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18869 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18870 if (! extern_p)
18871 {
18872 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18873 rest_of_type_compilation (t, 1);
18874 }
18875 }
18876
18877 /* Called from do_type_instantiation through binding_table_foreach to
18878 do recursive instantiation for the type bound in ENTRY. */
18879 static void
18880 bt_instantiate_type_proc (binding_entry entry, void *data)
18881 {
18882 tree storage = *(tree *) data;
18883
18884 if (MAYBE_CLASS_TYPE_P (entry->type)
18885 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18886 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18887 }
18888
18889 /* Called from do_type_instantiation to instantiate a member
18890 (a member function or a static member variable) of an
18891 explicitly instantiated class template. */
18892 static void
18893 instantiate_class_member (tree decl, int extern_p)
18894 {
18895 mark_decl_instantiated (decl, extern_p);
18896 if (! extern_p)
18897 instantiate_decl (decl, /*defer_ok=*/1,
18898 /*expl_inst_class_mem_p=*/true);
18899 }
18900
18901 /* Perform an explicit instantiation of template class T. STORAGE, if
18902 non-null, is the RID for extern, inline or static. COMPLAIN is
18903 nonzero if this is called from the parser, zero if called recursively,
18904 since the standard is unclear (as detailed below). */
18905
18906 void
18907 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18908 {
18909 int extern_p = 0;
18910 int nomem_p = 0;
18911 int static_p = 0;
18912 int previous_instantiation_extern_p = 0;
18913
18914 if (TREE_CODE (t) == TYPE_DECL)
18915 t = TREE_TYPE (t);
18916
18917 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18918 {
18919 tree tmpl =
18920 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18921 if (tmpl)
18922 error ("explicit instantiation of non-class template %qD", tmpl);
18923 else
18924 error ("explicit instantiation of non-template type %qT", t);
18925 return;
18926 }
18927
18928 complete_type (t);
18929
18930 if (!COMPLETE_TYPE_P (t))
18931 {
18932 if (complain & tf_error)
18933 error ("explicit instantiation of %q#T before definition of template",
18934 t);
18935 return;
18936 }
18937
18938 if (storage != NULL_TREE)
18939 {
18940 if (!in_system_header_at (input_location))
18941 {
18942 if (storage == ridpointers[(int) RID_EXTERN])
18943 {
18944 if (cxx_dialect == cxx98)
18945 pedwarn (input_location, OPT_Wpedantic,
18946 "ISO C++ 1998 forbids the use of %<extern%> on "
18947 "explicit instantiations");
18948 }
18949 else
18950 pedwarn (input_location, OPT_Wpedantic,
18951 "ISO C++ forbids the use of %qE"
18952 " on explicit instantiations", storage);
18953 }
18954
18955 if (storage == ridpointers[(int) RID_INLINE])
18956 nomem_p = 1;
18957 else if (storage == ridpointers[(int) RID_EXTERN])
18958 extern_p = 1;
18959 else if (storage == ridpointers[(int) RID_STATIC])
18960 static_p = 1;
18961 else
18962 {
18963 error ("storage class %qD applied to template instantiation",
18964 storage);
18965 extern_p = 0;
18966 }
18967 }
18968
18969 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18970 {
18971 /* DR 259 [temp.spec].
18972
18973 Both an explicit instantiation and a declaration of an explicit
18974 specialization shall not appear in a program unless the explicit
18975 instantiation follows a declaration of the explicit specialization.
18976
18977 For a given set of template parameters, if an explicit
18978 instantiation of a template appears after a declaration of an
18979 explicit specialization for that template, the explicit
18980 instantiation has no effect. */
18981 return;
18982 }
18983 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18984 {
18985 /* [temp.spec]
18986
18987 No program shall explicitly instantiate any template more
18988 than once.
18989
18990 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18991 instantiation was `extern'. If EXTERN_P then the second is.
18992 These cases are OK. */
18993 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18994
18995 if (!previous_instantiation_extern_p && !extern_p
18996 && (complain & tf_error))
18997 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18998
18999 /* If we've already instantiated the template, just return now. */
19000 if (!CLASSTYPE_INTERFACE_ONLY (t))
19001 return;
19002 }
19003
19004 check_explicit_instantiation_namespace (TYPE_NAME (t));
19005 mark_class_instantiated (t, extern_p);
19006
19007 if (nomem_p)
19008 return;
19009
19010 {
19011 tree tmp;
19012
19013 /* In contrast to implicit instantiation, where only the
19014 declarations, and not the definitions, of members are
19015 instantiated, we have here:
19016
19017 [temp.explicit]
19018
19019 The explicit instantiation of a class template specialization
19020 implies the instantiation of all of its members not
19021 previously explicitly specialized in the translation unit
19022 containing the explicit instantiation.
19023
19024 Of course, we can't instantiate member template classes, since
19025 we don't have any arguments for them. Note that the standard
19026 is unclear on whether the instantiation of the members are
19027 *explicit* instantiations or not. However, the most natural
19028 interpretation is that it should be an explicit instantiation. */
19029
19030 if (! static_p)
19031 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
19032 if (TREE_CODE (tmp) == FUNCTION_DECL
19033 && DECL_TEMPLATE_INSTANTIATION (tmp))
19034 instantiate_class_member (tmp, extern_p);
19035
19036 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
19037 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
19038 instantiate_class_member (tmp, extern_p);
19039
19040 if (CLASSTYPE_NESTED_UTDS (t))
19041 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19042 bt_instantiate_type_proc, &storage);
19043 }
19044 }
19045
19046 /* Given a function DECL, which is a specialization of TMPL, modify
19047 DECL to be a re-instantiation of TMPL with the same template
19048 arguments. TMPL should be the template into which tsubst'ing
19049 should occur for DECL, not the most general template.
19050
19051 One reason for doing this is a scenario like this:
19052
19053 template <class T>
19054 void f(const T&, int i);
19055
19056 void g() { f(3, 7); }
19057
19058 template <class T>
19059 void f(const T& t, const int i) { }
19060
19061 Note that when the template is first instantiated, with
19062 instantiate_template, the resulting DECL will have no name for the
19063 first parameter, and the wrong type for the second. So, when we go
19064 to instantiate the DECL, we regenerate it. */
19065
19066 static void
19067 regenerate_decl_from_template (tree decl, tree tmpl)
19068 {
19069 /* The arguments used to instantiate DECL, from the most general
19070 template. */
19071 tree args;
19072 tree code_pattern;
19073
19074 args = DECL_TI_ARGS (decl);
19075 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19076
19077 /* Make sure that we can see identifiers, and compute access
19078 correctly. */
19079 push_access_scope (decl);
19080
19081 if (TREE_CODE (decl) == FUNCTION_DECL)
19082 {
19083 tree decl_parm;
19084 tree pattern_parm;
19085 tree specs;
19086 int args_depth;
19087 int parms_depth;
19088
19089 args_depth = TMPL_ARGS_DEPTH (args);
19090 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19091 if (args_depth > parms_depth)
19092 args = get_innermost_template_args (args, parms_depth);
19093
19094 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19095 args, tf_error, NULL_TREE,
19096 /*defer_ok*/false);
19097 if (specs && specs != error_mark_node)
19098 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19099 specs);
19100
19101 /* Merge parameter declarations. */
19102 decl_parm = skip_artificial_parms_for (decl,
19103 DECL_ARGUMENTS (decl));
19104 pattern_parm
19105 = skip_artificial_parms_for (code_pattern,
19106 DECL_ARGUMENTS (code_pattern));
19107 while (decl_parm && !DECL_PACK_P (pattern_parm))
19108 {
19109 tree parm_type;
19110 tree attributes;
19111
19112 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19113 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19114 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19115 NULL_TREE);
19116 parm_type = type_decays_to (parm_type);
19117 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19118 TREE_TYPE (decl_parm) = parm_type;
19119 attributes = DECL_ATTRIBUTES (pattern_parm);
19120 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19121 {
19122 DECL_ATTRIBUTES (decl_parm) = attributes;
19123 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19124 }
19125 decl_parm = DECL_CHAIN (decl_parm);
19126 pattern_parm = DECL_CHAIN (pattern_parm);
19127 }
19128 /* Merge any parameters that match with the function parameter
19129 pack. */
19130 if (pattern_parm && DECL_PACK_P (pattern_parm))
19131 {
19132 int i, len;
19133 tree expanded_types;
19134 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19135 the parameters in this function parameter pack. */
19136 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19137 args, tf_error, NULL_TREE);
19138 len = TREE_VEC_LENGTH (expanded_types);
19139 for (i = 0; i < len; i++)
19140 {
19141 tree parm_type;
19142 tree attributes;
19143
19144 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19145 /* Rename the parameter to include the index. */
19146 DECL_NAME (decl_parm) =
19147 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19148 parm_type = TREE_VEC_ELT (expanded_types, i);
19149 parm_type = type_decays_to (parm_type);
19150 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19151 TREE_TYPE (decl_parm) = parm_type;
19152 attributes = DECL_ATTRIBUTES (pattern_parm);
19153 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19154 {
19155 DECL_ATTRIBUTES (decl_parm) = attributes;
19156 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19157 }
19158 decl_parm = DECL_CHAIN (decl_parm);
19159 }
19160 }
19161 /* Merge additional specifiers from the CODE_PATTERN. */
19162 if (DECL_DECLARED_INLINE_P (code_pattern)
19163 && !DECL_DECLARED_INLINE_P (decl))
19164 DECL_DECLARED_INLINE_P (decl) = 1;
19165 }
19166 else if (VAR_P (decl))
19167 {
19168 DECL_INITIAL (decl) =
19169 tsubst_expr (DECL_INITIAL (code_pattern), args,
19170 tf_error, DECL_TI_TEMPLATE (decl),
19171 /*integral_constant_expression_p=*/false);
19172 if (VAR_HAD_UNKNOWN_BOUND (decl))
19173 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19174 tf_error, DECL_TI_TEMPLATE (decl));
19175 }
19176 else
19177 gcc_unreachable ();
19178
19179 pop_access_scope (decl);
19180 }
19181
19182 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19183 substituted to get DECL. */
19184
19185 tree
19186 template_for_substitution (tree decl)
19187 {
19188 tree tmpl = DECL_TI_TEMPLATE (decl);
19189
19190 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19191 for the instantiation. This is not always the most general
19192 template. Consider, for example:
19193
19194 template <class T>
19195 struct S { template <class U> void f();
19196 template <> void f<int>(); };
19197
19198 and an instantiation of S<double>::f<int>. We want TD to be the
19199 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19200 while (/* An instantiation cannot have a definition, so we need a
19201 more general template. */
19202 DECL_TEMPLATE_INSTANTIATION (tmpl)
19203 /* We must also deal with friend templates. Given:
19204
19205 template <class T> struct S {
19206 template <class U> friend void f() {};
19207 };
19208
19209 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19210 so far as the language is concerned, but that's still
19211 where we get the pattern for the instantiation from. On
19212 other hand, if the definition comes outside the class, say:
19213
19214 template <class T> struct S {
19215 template <class U> friend void f();
19216 };
19217 template <class U> friend void f() {}
19218
19219 we don't need to look any further. That's what the check for
19220 DECL_INITIAL is for. */
19221 || (TREE_CODE (decl) == FUNCTION_DECL
19222 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19223 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19224 {
19225 /* The present template, TD, should not be a definition. If it
19226 were a definition, we should be using it! Note that we
19227 cannot restructure the loop to just keep going until we find
19228 a template with a definition, since that might go too far if
19229 a specialization was declared, but not defined. */
19230 gcc_assert (!VAR_P (decl)
19231 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19232
19233 /* Fetch the more general template. */
19234 tmpl = DECL_TI_TEMPLATE (tmpl);
19235 }
19236
19237 return tmpl;
19238 }
19239
19240 /* Returns true if we need to instantiate this template instance even if we
19241 know we aren't going to emit it.. */
19242
19243 bool
19244 always_instantiate_p (tree decl)
19245 {
19246 /* We always instantiate inline functions so that we can inline them. An
19247 explicit instantiation declaration prohibits implicit instantiation of
19248 non-inline functions. With high levels of optimization, we would
19249 normally inline non-inline functions -- but we're not allowed to do
19250 that for "extern template" functions. Therefore, we check
19251 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19252 return ((TREE_CODE (decl) == FUNCTION_DECL
19253 && (DECL_DECLARED_INLINE_P (decl)
19254 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19255 /* And we need to instantiate static data members so that
19256 their initializers are available in integral constant
19257 expressions. */
19258 || (VAR_P (decl)
19259 && decl_maybe_constant_var_p (decl)));
19260 }
19261
19262 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19263 instantiate it now, modifying TREE_TYPE (fn). */
19264
19265 void
19266 maybe_instantiate_noexcept (tree fn)
19267 {
19268 tree fntype, spec, noex, clone;
19269
19270 if (DECL_CLONED_FUNCTION_P (fn))
19271 fn = DECL_CLONED_FUNCTION (fn);
19272 fntype = TREE_TYPE (fn);
19273 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19274
19275 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19276 return;
19277
19278 noex = TREE_PURPOSE (spec);
19279
19280 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19281 {
19282 if (push_tinst_level (fn))
19283 {
19284 push_access_scope (fn);
19285 push_deferring_access_checks (dk_no_deferred);
19286 input_location = DECL_SOURCE_LOCATION (fn);
19287 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19288 DEFERRED_NOEXCEPT_ARGS (noex),
19289 tf_warning_or_error, fn,
19290 /*function_p=*/false,
19291 /*integral_constant_expression_p=*/true);
19292 pop_deferring_access_checks ();
19293 pop_access_scope (fn);
19294 pop_tinst_level ();
19295 spec = build_noexcept_spec (noex, tf_warning_or_error);
19296 if (spec == error_mark_node)
19297 spec = noexcept_false_spec;
19298 }
19299 else
19300 spec = noexcept_false_spec;
19301 }
19302 else
19303 {
19304 /* This is an implicitly declared function, so NOEX is a list of
19305 other functions to evaluate and merge. */
19306 tree elt;
19307 spec = noexcept_true_spec;
19308 for (elt = noex; elt; elt = OVL_NEXT (elt))
19309 {
19310 tree fn = OVL_CURRENT (elt);
19311 tree subspec;
19312 maybe_instantiate_noexcept (fn);
19313 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19314 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19315 }
19316 }
19317
19318 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19319
19320 FOR_EACH_CLONE (clone, fn)
19321 {
19322 if (TREE_TYPE (clone) == fntype)
19323 TREE_TYPE (clone) = TREE_TYPE (fn);
19324 else
19325 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19326 }
19327 }
19328
19329 /* Produce the definition of D, a _DECL generated from a template. If
19330 DEFER_OK is nonzero, then we don't have to actually do the
19331 instantiation now; we just have to do it sometime. Normally it is
19332 an error if this is an explicit instantiation but D is undefined.
19333 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19334 explicitly instantiated class template. */
19335
19336 tree
19337 instantiate_decl (tree d, int defer_ok,
19338 bool expl_inst_class_mem_p)
19339 {
19340 tree tmpl = DECL_TI_TEMPLATE (d);
19341 tree gen_args;
19342 tree args;
19343 tree td;
19344 tree code_pattern;
19345 tree spec;
19346 tree gen_tmpl;
19347 bool pattern_defined;
19348 location_t saved_loc = input_location;
19349 int saved_unevaluated_operand = cp_unevaluated_operand;
19350 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19351 bool external_p;
19352 tree fn_context;
19353 bool nested;
19354
19355 /* This function should only be used to instantiate templates for
19356 functions and static member variables. */
19357 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19358
19359 /* Variables are never deferred; if instantiation is required, they
19360 are instantiated right away. That allows for better code in the
19361 case that an expression refers to the value of the variable --
19362 if the variable has a constant value the referring expression can
19363 take advantage of that fact. */
19364 if (VAR_P (d)
19365 || DECL_DECLARED_CONSTEXPR_P (d))
19366 defer_ok = 0;
19367
19368 /* Don't instantiate cloned functions. Instead, instantiate the
19369 functions they cloned. */
19370 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19371 d = DECL_CLONED_FUNCTION (d);
19372
19373 if (DECL_TEMPLATE_INSTANTIATED (d)
19374 || (TREE_CODE (d) == FUNCTION_DECL
19375 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19376 || DECL_TEMPLATE_SPECIALIZATION (d))
19377 /* D has already been instantiated or explicitly specialized, so
19378 there's nothing for us to do here.
19379
19380 It might seem reasonable to check whether or not D is an explicit
19381 instantiation, and, if so, stop here. But when an explicit
19382 instantiation is deferred until the end of the compilation,
19383 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19384 the instantiation. */
19385 return d;
19386
19387 /* Check to see whether we know that this template will be
19388 instantiated in some other file, as with "extern template"
19389 extension. */
19390 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19391
19392 /* In general, we do not instantiate such templates. */
19393 if (external_p && !always_instantiate_p (d))
19394 return d;
19395
19396 gen_tmpl = most_general_template (tmpl);
19397 gen_args = DECL_TI_ARGS (d);
19398
19399 if (tmpl != gen_tmpl)
19400 /* We should already have the extra args. */
19401 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19402 == TMPL_ARGS_DEPTH (gen_args));
19403 /* And what's in the hash table should match D. */
19404 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19405 || spec == NULL_TREE);
19406
19407 /* This needs to happen before any tsubsting. */
19408 if (! push_tinst_level (d))
19409 return d;
19410
19411 timevar_push (TV_TEMPLATE_INST);
19412
19413 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19414 for the instantiation. */
19415 td = template_for_substitution (d);
19416 code_pattern = DECL_TEMPLATE_RESULT (td);
19417
19418 /* We should never be trying to instantiate a member of a class
19419 template or partial specialization. */
19420 gcc_assert (d != code_pattern);
19421
19422 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19423 || DECL_TEMPLATE_SPECIALIZATION (td))
19424 /* In the case of a friend template whose definition is provided
19425 outside the class, we may have too many arguments. Drop the
19426 ones we don't need. The same is true for specializations. */
19427 args = get_innermost_template_args
19428 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19429 else
19430 args = gen_args;
19431
19432 if (TREE_CODE (d) == FUNCTION_DECL)
19433 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19434 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19435 else
19436 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19437
19438 /* We may be in the middle of deferred access check. Disable it now. */
19439 push_deferring_access_checks (dk_no_deferred);
19440
19441 /* Unless an explicit instantiation directive has already determined
19442 the linkage of D, remember that a definition is available for
19443 this entity. */
19444 if (pattern_defined
19445 && !DECL_INTERFACE_KNOWN (d)
19446 && !DECL_NOT_REALLY_EXTERN (d))
19447 mark_definable (d);
19448
19449 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19450 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19451 input_location = DECL_SOURCE_LOCATION (d);
19452
19453 /* If D is a member of an explicitly instantiated class template,
19454 and no definition is available, treat it like an implicit
19455 instantiation. */
19456 if (!pattern_defined && expl_inst_class_mem_p
19457 && DECL_EXPLICIT_INSTANTIATION (d))
19458 {
19459 /* Leave linkage flags alone on instantiations with anonymous
19460 visibility. */
19461 if (TREE_PUBLIC (d))
19462 {
19463 DECL_NOT_REALLY_EXTERN (d) = 0;
19464 DECL_INTERFACE_KNOWN (d) = 0;
19465 }
19466 SET_DECL_IMPLICIT_INSTANTIATION (d);
19467 }
19468
19469 if (TREE_CODE (d) == FUNCTION_DECL)
19470 maybe_instantiate_noexcept (d);
19471
19472 /* Defer all other templates, unless we have been explicitly
19473 forbidden from doing so. */
19474 if (/* If there is no definition, we cannot instantiate the
19475 template. */
19476 ! pattern_defined
19477 /* If it's OK to postpone instantiation, do so. */
19478 || defer_ok
19479 /* If this is a static data member that will be defined
19480 elsewhere, we don't want to instantiate the entire data
19481 member, but we do want to instantiate the initializer so that
19482 we can substitute that elsewhere. */
19483 || (external_p && VAR_P (d)))
19484 {
19485 /* The definition of the static data member is now required so
19486 we must substitute the initializer. */
19487 if (VAR_P (d)
19488 && !DECL_INITIAL (d)
19489 && DECL_INITIAL (code_pattern))
19490 {
19491 tree ns;
19492 tree init;
19493 bool const_init = false;
19494
19495 ns = decl_namespace_context (d);
19496 push_nested_namespace (ns);
19497 push_nested_class (DECL_CONTEXT (d));
19498 init = tsubst_expr (DECL_INITIAL (code_pattern),
19499 args,
19500 tf_warning_or_error, NULL_TREE,
19501 /*integral_constant_expression_p=*/false);
19502 /* Make sure the initializer is still constant, in case of
19503 circular dependency (template/instantiate6.C). */
19504 const_init
19505 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19506 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19507 /*asmspec_tree=*/NULL_TREE,
19508 LOOKUP_ONLYCONVERTING);
19509 pop_nested_class ();
19510 pop_nested_namespace (ns);
19511 }
19512
19513 /* We restore the source position here because it's used by
19514 add_pending_template. */
19515 input_location = saved_loc;
19516
19517 if (at_eof && !pattern_defined
19518 && DECL_EXPLICIT_INSTANTIATION (d)
19519 && DECL_NOT_REALLY_EXTERN (d))
19520 /* [temp.explicit]
19521
19522 The definition of a non-exported function template, a
19523 non-exported member function template, or a non-exported
19524 member function or static data member of a class template
19525 shall be present in every translation unit in which it is
19526 explicitly instantiated. */
19527 permerror (input_location, "explicit instantiation of %qD "
19528 "but no definition available", d);
19529
19530 /* If we're in unevaluated context, we just wanted to get the
19531 constant value; this isn't an odr use, so don't queue
19532 a full instantiation. */
19533 if (cp_unevaluated_operand != 0)
19534 goto out;
19535 /* ??? Historically, we have instantiated inline functions, even
19536 when marked as "extern template". */
19537 if (!(external_p && VAR_P (d)))
19538 add_pending_template (d);
19539 goto out;
19540 }
19541 /* Tell the repository that D is available in this translation unit
19542 -- and see if it is supposed to be instantiated here. */
19543 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19544 {
19545 /* In a PCH file, despite the fact that the repository hasn't
19546 requested instantiation in the PCH it is still possible that
19547 an instantiation will be required in a file that includes the
19548 PCH. */
19549 if (pch_file)
19550 add_pending_template (d);
19551 /* Instantiate inline functions so that the inliner can do its
19552 job, even though we'll not be emitting a copy of this
19553 function. */
19554 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19555 goto out;
19556 }
19557
19558 fn_context = decl_function_context (d);
19559 nested = (current_function_decl != NULL_TREE);
19560 if (!fn_context)
19561 push_to_top_level ();
19562 else
19563 {
19564 if (nested)
19565 push_function_context ();
19566 cp_unevaluated_operand = 0;
19567 c_inhibit_evaluation_warnings = 0;
19568 }
19569
19570 /* Mark D as instantiated so that recursive calls to
19571 instantiate_decl do not try to instantiate it again. */
19572 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19573
19574 /* Regenerate the declaration in case the template has been modified
19575 by a subsequent redeclaration. */
19576 regenerate_decl_from_template (d, td);
19577
19578 /* We already set the file and line above. Reset them now in case
19579 they changed as a result of calling regenerate_decl_from_template. */
19580 input_location = DECL_SOURCE_LOCATION (d);
19581
19582 if (VAR_P (d))
19583 {
19584 tree init;
19585 bool const_init = false;
19586
19587 /* Clear out DECL_RTL; whatever was there before may not be right
19588 since we've reset the type of the declaration. */
19589 SET_DECL_RTL (d, NULL);
19590 DECL_IN_AGGR_P (d) = 0;
19591
19592 /* The initializer is placed in DECL_INITIAL by
19593 regenerate_decl_from_template so we don't need to
19594 push/pop_access_scope again here. Pull it out so that
19595 cp_finish_decl can process it. */
19596 init = DECL_INITIAL (d);
19597 DECL_INITIAL (d) = NULL_TREE;
19598 DECL_INITIALIZED_P (d) = 0;
19599
19600 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19601 initializer. That function will defer actual emission until
19602 we have a chance to determine linkage. */
19603 DECL_EXTERNAL (d) = 0;
19604
19605 /* Enter the scope of D so that access-checking works correctly. */
19606 push_nested_class (DECL_CONTEXT (d));
19607 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19608 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19609 pop_nested_class ();
19610 }
19611 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19612 synthesize_method (d);
19613 else if (TREE_CODE (d) == FUNCTION_DECL)
19614 {
19615 struct pointer_map_t *saved_local_specializations;
19616 tree subst_decl;
19617 tree tmpl_parm;
19618 tree spec_parm;
19619 tree block = NULL_TREE;
19620
19621 /* Save away the current list, in case we are instantiating one
19622 template from within the body of another. */
19623 saved_local_specializations = local_specializations;
19624
19625 /* Set up the list of local specializations. */
19626 local_specializations = pointer_map_create ();
19627
19628 /* Set up context. */
19629 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19630 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19631 block = push_stmt_list ();
19632 else
19633 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19634
19635 /* Some typedefs referenced from within the template code need to be
19636 access checked at template instantiation time, i.e now. These
19637 types were added to the template at parsing time. Let's get those
19638 and perform the access checks then. */
19639 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19640 gen_args);
19641
19642 /* Create substitution entries for the parameters. */
19643 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19644 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19645 spec_parm = DECL_ARGUMENTS (d);
19646 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19647 {
19648 register_local_specialization (spec_parm, tmpl_parm);
19649 spec_parm = skip_artificial_parms_for (d, spec_parm);
19650 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19651 }
19652 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19653 {
19654 if (!DECL_PACK_P (tmpl_parm))
19655 {
19656 register_local_specialization (spec_parm, tmpl_parm);
19657 spec_parm = DECL_CHAIN (spec_parm);
19658 }
19659 else
19660 {
19661 /* Register the (value) argument pack as a specialization of
19662 TMPL_PARM, then move on. */
19663 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19664 register_local_specialization (argpack, tmpl_parm);
19665 }
19666 }
19667 gcc_assert (!spec_parm);
19668
19669 /* Substitute into the body of the function. */
19670 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19671 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19672 tf_warning_or_error, tmpl);
19673 else
19674 {
19675 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19676 tf_warning_or_error, tmpl,
19677 /*integral_constant_expression_p=*/false);
19678
19679 /* Set the current input_location to the end of the function
19680 so that finish_function knows where we are. */
19681 input_location
19682 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19683
19684 /* Remember if we saw an infinite loop in the template. */
19685 current_function_infinite_loop
19686 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
19687 }
19688
19689 /* We don't need the local specializations any more. */
19690 pointer_map_destroy (local_specializations);
19691 local_specializations = saved_local_specializations;
19692
19693 /* Finish the function. */
19694 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19695 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19696 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19697 else
19698 {
19699 d = finish_function (0);
19700 expand_or_defer_fn (d);
19701 }
19702
19703 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19704 cp_check_omp_declare_reduction (d);
19705 }
19706
19707 /* We're not deferring instantiation any more. */
19708 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19709
19710 if (!fn_context)
19711 pop_from_top_level ();
19712 else if (nested)
19713 pop_function_context ();
19714
19715 out:
19716 input_location = saved_loc;
19717 cp_unevaluated_operand = saved_unevaluated_operand;
19718 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19719 pop_deferring_access_checks ();
19720 pop_tinst_level ();
19721
19722 timevar_pop (TV_TEMPLATE_INST);
19723
19724 return d;
19725 }
19726
19727 /* Run through the list of templates that we wish we could
19728 instantiate, and instantiate any we can. RETRIES is the
19729 number of times we retry pending template instantiation. */
19730
19731 void
19732 instantiate_pending_templates (int retries)
19733 {
19734 int reconsider;
19735 location_t saved_loc = input_location;
19736
19737 /* Instantiating templates may trigger vtable generation. This in turn
19738 may require further template instantiations. We place a limit here
19739 to avoid infinite loop. */
19740 if (pending_templates && retries >= max_tinst_depth)
19741 {
19742 tree decl = pending_templates->tinst->decl;
19743
19744 error ("template instantiation depth exceeds maximum of %d"
19745 " instantiating %q+D, possibly from virtual table generation"
19746 " (use -ftemplate-depth= to increase the maximum)",
19747 max_tinst_depth, decl);
19748 if (TREE_CODE (decl) == FUNCTION_DECL)
19749 /* Pretend that we defined it. */
19750 DECL_INITIAL (decl) = error_mark_node;
19751 return;
19752 }
19753
19754 do
19755 {
19756 struct pending_template **t = &pending_templates;
19757 struct pending_template *last = NULL;
19758 reconsider = 0;
19759 while (*t)
19760 {
19761 tree instantiation = reopen_tinst_level ((*t)->tinst);
19762 bool complete = false;
19763
19764 if (TYPE_P (instantiation))
19765 {
19766 tree fn;
19767
19768 if (!COMPLETE_TYPE_P (instantiation))
19769 {
19770 instantiate_class_template (instantiation);
19771 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19772 for (fn = TYPE_METHODS (instantiation);
19773 fn;
19774 fn = TREE_CHAIN (fn))
19775 if (! DECL_ARTIFICIAL (fn))
19776 instantiate_decl (fn,
19777 /*defer_ok=*/0,
19778 /*expl_inst_class_mem_p=*/false);
19779 if (COMPLETE_TYPE_P (instantiation))
19780 reconsider = 1;
19781 }
19782
19783 complete = COMPLETE_TYPE_P (instantiation);
19784 }
19785 else
19786 {
19787 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19788 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19789 {
19790 instantiation
19791 = instantiate_decl (instantiation,
19792 /*defer_ok=*/0,
19793 /*expl_inst_class_mem_p=*/false);
19794 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19795 reconsider = 1;
19796 }
19797
19798 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19799 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19800 }
19801
19802 if (complete)
19803 /* If INSTANTIATION has been instantiated, then we don't
19804 need to consider it again in the future. */
19805 *t = (*t)->next;
19806 else
19807 {
19808 last = *t;
19809 t = &(*t)->next;
19810 }
19811 tinst_depth = 0;
19812 current_tinst_level = NULL;
19813 }
19814 last_pending_template = last;
19815 }
19816 while (reconsider);
19817
19818 input_location = saved_loc;
19819 }
19820
19821 /* Substitute ARGVEC into T, which is a list of initializers for
19822 either base class or a non-static data member. The TREE_PURPOSEs
19823 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19824 instantiate_decl. */
19825
19826 static tree
19827 tsubst_initializer_list (tree t, tree argvec)
19828 {
19829 tree inits = NULL_TREE;
19830
19831 for (; t; t = TREE_CHAIN (t))
19832 {
19833 tree decl;
19834 tree init;
19835 tree expanded_bases = NULL_TREE;
19836 tree expanded_arguments = NULL_TREE;
19837 int i, len = 1;
19838
19839 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19840 {
19841 tree expr;
19842 tree arg;
19843
19844 /* Expand the base class expansion type into separate base
19845 classes. */
19846 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19847 tf_warning_or_error,
19848 NULL_TREE);
19849 if (expanded_bases == error_mark_node)
19850 continue;
19851
19852 /* We'll be building separate TREE_LISTs of arguments for
19853 each base. */
19854 len = TREE_VEC_LENGTH (expanded_bases);
19855 expanded_arguments = make_tree_vec (len);
19856 for (i = 0; i < len; i++)
19857 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19858
19859 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19860 expand each argument in the TREE_VALUE of t. */
19861 expr = make_node (EXPR_PACK_EXPANSION);
19862 PACK_EXPANSION_LOCAL_P (expr) = true;
19863 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19864 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19865
19866 if (TREE_VALUE (t) == void_type_node)
19867 /* VOID_TYPE_NODE is used to indicate
19868 value-initialization. */
19869 {
19870 for (i = 0; i < len; i++)
19871 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19872 }
19873 else
19874 {
19875 /* Substitute parameter packs into each argument in the
19876 TREE_LIST. */
19877 in_base_initializer = 1;
19878 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19879 {
19880 tree expanded_exprs;
19881
19882 /* Expand the argument. */
19883 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19884 expanded_exprs
19885 = tsubst_pack_expansion (expr, argvec,
19886 tf_warning_or_error,
19887 NULL_TREE);
19888 if (expanded_exprs == error_mark_node)
19889 continue;
19890
19891 /* Prepend each of the expanded expressions to the
19892 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19893 for (i = 0; i < len; i++)
19894 {
19895 TREE_VEC_ELT (expanded_arguments, i) =
19896 tree_cons (NULL_TREE,
19897 TREE_VEC_ELT (expanded_exprs, i),
19898 TREE_VEC_ELT (expanded_arguments, i));
19899 }
19900 }
19901 in_base_initializer = 0;
19902
19903 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19904 since we built them backwards. */
19905 for (i = 0; i < len; i++)
19906 {
19907 TREE_VEC_ELT (expanded_arguments, i) =
19908 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19909 }
19910 }
19911 }
19912
19913 for (i = 0; i < len; ++i)
19914 {
19915 if (expanded_bases)
19916 {
19917 decl = TREE_VEC_ELT (expanded_bases, i);
19918 decl = expand_member_init (decl);
19919 init = TREE_VEC_ELT (expanded_arguments, i);
19920 }
19921 else
19922 {
19923 tree tmp;
19924 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19925 tf_warning_or_error, NULL_TREE);
19926
19927 decl = expand_member_init (decl);
19928 if (decl && !DECL_P (decl))
19929 in_base_initializer = 1;
19930
19931 init = TREE_VALUE (t);
19932 tmp = init;
19933 if (init != void_type_node)
19934 init = tsubst_expr (init, argvec,
19935 tf_warning_or_error, NULL_TREE,
19936 /*integral_constant_expression_p=*/false);
19937 if (init == NULL_TREE && tmp != NULL_TREE)
19938 /* If we had an initializer but it instantiated to nothing,
19939 value-initialize the object. This will only occur when
19940 the initializer was a pack expansion where the parameter
19941 packs used in that expansion were of length zero. */
19942 init = void_type_node;
19943 in_base_initializer = 0;
19944 }
19945
19946 if (decl)
19947 {
19948 init = build_tree_list (decl, init);
19949 TREE_CHAIN (init) = inits;
19950 inits = init;
19951 }
19952 }
19953 }
19954 return inits;
19955 }
19956
19957 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19958
19959 static void
19960 set_current_access_from_decl (tree decl)
19961 {
19962 if (TREE_PRIVATE (decl))
19963 current_access_specifier = access_private_node;
19964 else if (TREE_PROTECTED (decl))
19965 current_access_specifier = access_protected_node;
19966 else
19967 current_access_specifier = access_public_node;
19968 }
19969
19970 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19971 is the instantiation (which should have been created with
19972 start_enum) and ARGS are the template arguments to use. */
19973
19974 static void
19975 tsubst_enum (tree tag, tree newtag, tree args)
19976 {
19977 tree e;
19978
19979 if (SCOPED_ENUM_P (newtag))
19980 begin_scope (sk_scoped_enum, newtag);
19981
19982 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19983 {
19984 tree value;
19985 tree decl;
19986
19987 decl = TREE_VALUE (e);
19988 /* Note that in a template enum, the TREE_VALUE is the
19989 CONST_DECL, not the corresponding INTEGER_CST. */
19990 value = tsubst_expr (DECL_INITIAL (decl),
19991 args, tf_warning_or_error, NULL_TREE,
19992 /*integral_constant_expression_p=*/true);
19993
19994 /* Give this enumeration constant the correct access. */
19995 set_current_access_from_decl (decl);
19996
19997 /* Actually build the enumerator itself. */
19998 build_enumerator
19999 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
20000 }
20001
20002 if (SCOPED_ENUM_P (newtag))
20003 finish_scope ();
20004
20005 finish_enum_value_list (newtag);
20006 finish_enum (newtag);
20007
20008 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
20009 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
20010 }
20011
20012 /* DECL is a FUNCTION_DECL that is a template specialization. Return
20013 its type -- but without substituting the innermost set of template
20014 arguments. So, innermost set of template parameters will appear in
20015 the type. */
20016
20017 tree
20018 get_mostly_instantiated_function_type (tree decl)
20019 {
20020 tree fn_type;
20021 tree tmpl;
20022 tree targs;
20023 tree tparms;
20024 int parm_depth;
20025
20026 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
20027 targs = DECL_TI_ARGS (decl);
20028 tparms = DECL_TEMPLATE_PARMS (tmpl);
20029 parm_depth = TMPL_PARMS_DEPTH (tparms);
20030
20031 /* There should be as many levels of arguments as there are levels
20032 of parameters. */
20033 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
20034
20035 fn_type = TREE_TYPE (tmpl);
20036
20037 if (parm_depth == 1)
20038 /* No substitution is necessary. */
20039 ;
20040 else
20041 {
20042 int i;
20043 tree partial_args;
20044
20045 /* Replace the innermost level of the TARGS with NULL_TREEs to
20046 let tsubst know not to substitute for those parameters. */
20047 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20048 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20049 SET_TMPL_ARGS_LEVEL (partial_args, i,
20050 TMPL_ARGS_LEVEL (targs, i));
20051 SET_TMPL_ARGS_LEVEL (partial_args,
20052 TMPL_ARGS_DEPTH (targs),
20053 make_tree_vec (DECL_NTPARMS (tmpl)));
20054
20055 /* Make sure that we can see identifiers, and compute access
20056 correctly. */
20057 push_access_scope (decl);
20058
20059 ++processing_template_decl;
20060 /* Now, do the (partial) substitution to figure out the
20061 appropriate function type. */
20062 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20063 --processing_template_decl;
20064
20065 /* Substitute into the template parameters to obtain the real
20066 innermost set of parameters. This step is important if the
20067 innermost set of template parameters contains value
20068 parameters whose types depend on outer template parameters. */
20069 TREE_VEC_LENGTH (partial_args)--;
20070 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20071
20072 pop_access_scope (decl);
20073 }
20074
20075 return fn_type;
20076 }
20077
20078 /* Return truthvalue if we're processing a template different from
20079 the last one involved in diagnostics. */
20080 int
20081 problematic_instantiation_changed (void)
20082 {
20083 return current_tinst_level != last_error_tinst_level;
20084 }
20085
20086 /* Remember current template involved in diagnostics. */
20087 void
20088 record_last_problematic_instantiation (void)
20089 {
20090 last_error_tinst_level = current_tinst_level;
20091 }
20092
20093 struct tinst_level *
20094 current_instantiation (void)
20095 {
20096 return current_tinst_level;
20097 }
20098
20099 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20100 type. Return zero for ok, nonzero for disallowed. Issue error and
20101 warning messages under control of COMPLAIN. */
20102
20103 static int
20104 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20105 {
20106 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20107 return 0;
20108 else if (POINTER_TYPE_P (type))
20109 return 0;
20110 else if (TYPE_PTRMEM_P (type))
20111 return 0;
20112 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20113 return 0;
20114 else if (TREE_CODE (type) == TYPENAME_TYPE)
20115 return 0;
20116 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20117 return 0;
20118 else if (TREE_CODE (type) == NULLPTR_TYPE)
20119 return 0;
20120
20121 if (complain & tf_error)
20122 {
20123 if (type == error_mark_node)
20124 inform (input_location, "invalid template non-type parameter");
20125 else
20126 error ("%q#T is not a valid type for a template non-type parameter",
20127 type);
20128 }
20129 return 1;
20130 }
20131
20132 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20133 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20134
20135 static bool
20136 dependent_type_p_r (tree type)
20137 {
20138 tree scope;
20139
20140 /* [temp.dep.type]
20141
20142 A type is dependent if it is:
20143
20144 -- a template parameter. Template template parameters are types
20145 for us (since TYPE_P holds true for them) so we handle
20146 them here. */
20147 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20148 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20149 return true;
20150 /* -- a qualified-id with a nested-name-specifier which contains a
20151 class-name that names a dependent type or whose unqualified-id
20152 names a dependent type. */
20153 if (TREE_CODE (type) == TYPENAME_TYPE)
20154 return true;
20155 /* -- a cv-qualified type where the cv-unqualified type is
20156 dependent. */
20157 type = TYPE_MAIN_VARIANT (type);
20158 /* -- a compound type constructed from any dependent type. */
20159 if (TYPE_PTRMEM_P (type))
20160 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20161 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20162 (type)));
20163 else if (TYPE_PTR_P (type)
20164 || TREE_CODE (type) == REFERENCE_TYPE)
20165 return dependent_type_p (TREE_TYPE (type));
20166 else if (TREE_CODE (type) == FUNCTION_TYPE
20167 || TREE_CODE (type) == METHOD_TYPE)
20168 {
20169 tree arg_type;
20170
20171 if (dependent_type_p (TREE_TYPE (type)))
20172 return true;
20173 for (arg_type = TYPE_ARG_TYPES (type);
20174 arg_type;
20175 arg_type = TREE_CHAIN (arg_type))
20176 if (dependent_type_p (TREE_VALUE (arg_type)))
20177 return true;
20178 return false;
20179 }
20180 /* -- an array type constructed from any dependent type or whose
20181 size is specified by a constant expression that is
20182 value-dependent.
20183
20184 We checked for type- and value-dependence of the bounds in
20185 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20186 if (TREE_CODE (type) == ARRAY_TYPE)
20187 {
20188 if (TYPE_DOMAIN (type)
20189 && dependent_type_p (TYPE_DOMAIN (type)))
20190 return true;
20191 return dependent_type_p (TREE_TYPE (type));
20192 }
20193
20194 /* -- a template-id in which either the template name is a template
20195 parameter ... */
20196 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20197 return true;
20198 /* ... or any of the template arguments is a dependent type or
20199 an expression that is type-dependent or value-dependent. */
20200 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20201 && (any_dependent_template_arguments_p
20202 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20203 return true;
20204
20205 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20206 dependent; if the argument of the `typeof' expression is not
20207 type-dependent, then it should already been have resolved. */
20208 if (TREE_CODE (type) == TYPEOF_TYPE
20209 || TREE_CODE (type) == DECLTYPE_TYPE
20210 || TREE_CODE (type) == UNDERLYING_TYPE)
20211 return true;
20212
20213 /* A template argument pack is dependent if any of its packed
20214 arguments are. */
20215 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20216 {
20217 tree args = ARGUMENT_PACK_ARGS (type);
20218 int i, len = TREE_VEC_LENGTH (args);
20219 for (i = 0; i < len; ++i)
20220 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20221 return true;
20222 }
20223
20224 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20225 be template parameters. */
20226 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20227 return true;
20228
20229 /* The standard does not specifically mention types that are local
20230 to template functions or local classes, but they should be
20231 considered dependent too. For example:
20232
20233 template <int I> void f() {
20234 enum E { a = I };
20235 S<sizeof (E)> s;
20236 }
20237
20238 The size of `E' cannot be known until the value of `I' has been
20239 determined. Therefore, `E' must be considered dependent. */
20240 scope = TYPE_CONTEXT (type);
20241 if (scope && TYPE_P (scope))
20242 return dependent_type_p (scope);
20243 /* Don't use type_dependent_expression_p here, as it can lead
20244 to infinite recursion trying to determine whether a lambda
20245 nested in a lambda is dependent (c++/47687). */
20246 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20247 && DECL_LANG_SPECIFIC (scope)
20248 && DECL_TEMPLATE_INFO (scope)
20249 && (any_dependent_template_arguments_p
20250 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20251 return true;
20252
20253 /* Other types are non-dependent. */
20254 return false;
20255 }
20256
20257 /* Returns TRUE if TYPE is dependent, in the sense of
20258 [temp.dep.type]. Note that a NULL type is considered dependent. */
20259
20260 bool
20261 dependent_type_p (tree type)
20262 {
20263 /* If there are no template parameters in scope, then there can't be
20264 any dependent types. */
20265 if (!processing_template_decl)
20266 {
20267 /* If we are not processing a template, then nobody should be
20268 providing us with a dependent type. */
20269 gcc_assert (type);
20270 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20271 return false;
20272 }
20273
20274 /* If the type is NULL, we have not computed a type for the entity
20275 in question; in that case, the type is dependent. */
20276 if (!type)
20277 return true;
20278
20279 /* Erroneous types can be considered non-dependent. */
20280 if (type == error_mark_node)
20281 return false;
20282
20283 /* If we have not already computed the appropriate value for TYPE,
20284 do so now. */
20285 if (!TYPE_DEPENDENT_P_VALID (type))
20286 {
20287 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20288 TYPE_DEPENDENT_P_VALID (type) = 1;
20289 }
20290
20291 return TYPE_DEPENDENT_P (type);
20292 }
20293
20294 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20295 lookup. In other words, a dependent type that is not the current
20296 instantiation. */
20297
20298 bool
20299 dependent_scope_p (tree scope)
20300 {
20301 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20302 && !currently_open_class (scope));
20303 }
20304
20305 /* T is a SCOPE_REF; return whether we need to consider it
20306 instantiation-dependent so that we can check access at instantiation
20307 time even though we know which member it resolves to. */
20308
20309 static bool
20310 instantiation_dependent_scope_ref_p (tree t)
20311 {
20312 if (DECL_P (TREE_OPERAND (t, 1))
20313 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20314 && accessible_in_template_p (TREE_OPERAND (t, 0),
20315 TREE_OPERAND (t, 1)))
20316 return false;
20317 else
20318 return true;
20319 }
20320
20321 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20322 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20323 expression. */
20324
20325 /* Note that this predicate is not appropriate for general expressions;
20326 only constant expressions (that satisfy potential_constant_expression)
20327 can be tested for value dependence. */
20328
20329 bool
20330 value_dependent_expression_p (tree expression)
20331 {
20332 if (!processing_template_decl)
20333 return false;
20334
20335 /* A name declared with a dependent type. */
20336 if (DECL_P (expression) && type_dependent_expression_p (expression))
20337 return true;
20338
20339 switch (TREE_CODE (expression))
20340 {
20341 case IDENTIFIER_NODE:
20342 /* A name that has not been looked up -- must be dependent. */
20343 return true;
20344
20345 case TEMPLATE_PARM_INDEX:
20346 /* A non-type template parm. */
20347 return true;
20348
20349 case CONST_DECL:
20350 /* A non-type template parm. */
20351 if (DECL_TEMPLATE_PARM_P (expression))
20352 return true;
20353 return value_dependent_expression_p (DECL_INITIAL (expression));
20354
20355 case VAR_DECL:
20356 /* A constant with literal type and is initialized
20357 with an expression that is value-dependent.
20358
20359 Note that a non-dependent parenthesized initializer will have
20360 already been replaced with its constant value, so if we see
20361 a TREE_LIST it must be dependent. */
20362 if (DECL_INITIAL (expression)
20363 && decl_constant_var_p (expression)
20364 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20365 || value_dependent_expression_p (DECL_INITIAL (expression))))
20366 return true;
20367 return false;
20368
20369 case DYNAMIC_CAST_EXPR:
20370 case STATIC_CAST_EXPR:
20371 case CONST_CAST_EXPR:
20372 case REINTERPRET_CAST_EXPR:
20373 case CAST_EXPR:
20374 /* These expressions are value-dependent if the type to which
20375 the cast occurs is dependent or the expression being casted
20376 is value-dependent. */
20377 {
20378 tree type = TREE_TYPE (expression);
20379
20380 if (dependent_type_p (type))
20381 return true;
20382
20383 /* A functional cast has a list of operands. */
20384 expression = TREE_OPERAND (expression, 0);
20385 if (!expression)
20386 {
20387 /* If there are no operands, it must be an expression such
20388 as "int()". This should not happen for aggregate types
20389 because it would form non-constant expressions. */
20390 gcc_assert (cxx_dialect >= cxx11
20391 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20392
20393 return false;
20394 }
20395
20396 if (TREE_CODE (expression) == TREE_LIST)
20397 return any_value_dependent_elements_p (expression);
20398
20399 return value_dependent_expression_p (expression);
20400 }
20401
20402 case SIZEOF_EXPR:
20403 if (SIZEOF_EXPR_TYPE_P (expression))
20404 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20405 /* FALLTHRU */
20406 case ALIGNOF_EXPR:
20407 case TYPEID_EXPR:
20408 /* A `sizeof' expression is value-dependent if the operand is
20409 type-dependent or is a pack expansion. */
20410 expression = TREE_OPERAND (expression, 0);
20411 if (PACK_EXPANSION_P (expression))
20412 return true;
20413 else if (TYPE_P (expression))
20414 return dependent_type_p (expression);
20415 return instantiation_dependent_expression_p (expression);
20416
20417 case AT_ENCODE_EXPR:
20418 /* An 'encode' expression is value-dependent if the operand is
20419 type-dependent. */
20420 expression = TREE_OPERAND (expression, 0);
20421 return dependent_type_p (expression);
20422
20423 case NOEXCEPT_EXPR:
20424 expression = TREE_OPERAND (expression, 0);
20425 return instantiation_dependent_expression_p (expression);
20426
20427 case SCOPE_REF:
20428 /* All instantiation-dependent expressions should also be considered
20429 value-dependent. */
20430 return instantiation_dependent_scope_ref_p (expression);
20431
20432 case COMPONENT_REF:
20433 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20434 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20435
20436 case NONTYPE_ARGUMENT_PACK:
20437 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20438 is value-dependent. */
20439 {
20440 tree values = ARGUMENT_PACK_ARGS (expression);
20441 int i, len = TREE_VEC_LENGTH (values);
20442
20443 for (i = 0; i < len; ++i)
20444 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20445 return true;
20446
20447 return false;
20448 }
20449
20450 case TRAIT_EXPR:
20451 {
20452 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20453 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20454 || (type2 ? dependent_type_p (type2) : false));
20455 }
20456
20457 case MODOP_EXPR:
20458 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20459 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20460
20461 case ARRAY_REF:
20462 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20463 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20464
20465 case ADDR_EXPR:
20466 {
20467 tree op = TREE_OPERAND (expression, 0);
20468 return (value_dependent_expression_p (op)
20469 || has_value_dependent_address (op));
20470 }
20471
20472 case CALL_EXPR:
20473 {
20474 tree fn = get_callee_fndecl (expression);
20475 int i, nargs;
20476 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20477 return true;
20478 nargs = call_expr_nargs (expression);
20479 for (i = 0; i < nargs; ++i)
20480 {
20481 tree op = CALL_EXPR_ARG (expression, i);
20482 /* In a call to a constexpr member function, look through the
20483 implicit ADDR_EXPR on the object argument so that it doesn't
20484 cause the call to be considered value-dependent. We also
20485 look through it in potential_constant_expression. */
20486 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20487 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20488 && TREE_CODE (op) == ADDR_EXPR)
20489 op = TREE_OPERAND (op, 0);
20490 if (value_dependent_expression_p (op))
20491 return true;
20492 }
20493 return false;
20494 }
20495
20496 case TEMPLATE_ID_EXPR:
20497 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20498 type-dependent. */
20499 return type_dependent_expression_p (expression);
20500
20501 case CONSTRUCTOR:
20502 {
20503 unsigned ix;
20504 tree val;
20505 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20506 if (value_dependent_expression_p (val))
20507 return true;
20508 return false;
20509 }
20510
20511 case STMT_EXPR:
20512 /* Treat a GNU statement expression as dependent to avoid crashing
20513 under fold_non_dependent_expr; it can't be constant. */
20514 return true;
20515
20516 default:
20517 /* A constant expression is value-dependent if any subexpression is
20518 value-dependent. */
20519 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20520 {
20521 case tcc_reference:
20522 case tcc_unary:
20523 case tcc_comparison:
20524 case tcc_binary:
20525 case tcc_expression:
20526 case tcc_vl_exp:
20527 {
20528 int i, len = cp_tree_operand_length (expression);
20529
20530 for (i = 0; i < len; i++)
20531 {
20532 tree t = TREE_OPERAND (expression, i);
20533
20534 /* In some cases, some of the operands may be missing.l
20535 (For example, in the case of PREDECREMENT_EXPR, the
20536 amount to increment by may be missing.) That doesn't
20537 make the expression dependent. */
20538 if (t && value_dependent_expression_p (t))
20539 return true;
20540 }
20541 }
20542 break;
20543 default:
20544 break;
20545 }
20546 break;
20547 }
20548
20549 /* The expression is not value-dependent. */
20550 return false;
20551 }
20552
20553 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20554 [temp.dep.expr]. Note that an expression with no type is
20555 considered dependent. Other parts of the compiler arrange for an
20556 expression with type-dependent subexpressions to have no type, so
20557 this function doesn't have to be fully recursive. */
20558
20559 bool
20560 type_dependent_expression_p (tree expression)
20561 {
20562 if (!processing_template_decl)
20563 return false;
20564
20565 if (expression == NULL_TREE || expression == error_mark_node)
20566 return false;
20567
20568 /* An unresolved name is always dependent. */
20569 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20570 return true;
20571
20572 /* Some expression forms are never type-dependent. */
20573 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20574 || TREE_CODE (expression) == SIZEOF_EXPR
20575 || TREE_CODE (expression) == ALIGNOF_EXPR
20576 || TREE_CODE (expression) == AT_ENCODE_EXPR
20577 || TREE_CODE (expression) == NOEXCEPT_EXPR
20578 || TREE_CODE (expression) == TRAIT_EXPR
20579 || TREE_CODE (expression) == TYPEID_EXPR
20580 || TREE_CODE (expression) == DELETE_EXPR
20581 || TREE_CODE (expression) == VEC_DELETE_EXPR
20582 || TREE_CODE (expression) == THROW_EXPR)
20583 return false;
20584
20585 /* The types of these expressions depends only on the type to which
20586 the cast occurs. */
20587 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20588 || TREE_CODE (expression) == STATIC_CAST_EXPR
20589 || TREE_CODE (expression) == CONST_CAST_EXPR
20590 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20591 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20592 || TREE_CODE (expression) == CAST_EXPR)
20593 return dependent_type_p (TREE_TYPE (expression));
20594
20595 /* The types of these expressions depends only on the type created
20596 by the expression. */
20597 if (TREE_CODE (expression) == NEW_EXPR
20598 || TREE_CODE (expression) == VEC_NEW_EXPR)
20599 {
20600 /* For NEW_EXPR tree nodes created inside a template, either
20601 the object type itself or a TREE_LIST may appear as the
20602 operand 1. */
20603 tree type = TREE_OPERAND (expression, 1);
20604 if (TREE_CODE (type) == TREE_LIST)
20605 /* This is an array type. We need to check array dimensions
20606 as well. */
20607 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20608 || value_dependent_expression_p
20609 (TREE_OPERAND (TREE_VALUE (type), 1));
20610 else
20611 return dependent_type_p (type);
20612 }
20613
20614 if (TREE_CODE (expression) == SCOPE_REF)
20615 {
20616 tree scope = TREE_OPERAND (expression, 0);
20617 tree name = TREE_OPERAND (expression, 1);
20618
20619 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20620 contains an identifier associated by name lookup with one or more
20621 declarations declared with a dependent type, or...a
20622 nested-name-specifier or qualified-id that names a member of an
20623 unknown specialization. */
20624 return (type_dependent_expression_p (name)
20625 || dependent_scope_p (scope));
20626 }
20627
20628 if (TREE_CODE (expression) == FUNCTION_DECL
20629 && DECL_LANG_SPECIFIC (expression)
20630 && DECL_TEMPLATE_INFO (expression)
20631 && (any_dependent_template_arguments_p
20632 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20633 return true;
20634
20635 if (TREE_CODE (expression) == TEMPLATE_DECL
20636 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20637 return false;
20638
20639 if (TREE_CODE (expression) == STMT_EXPR)
20640 expression = stmt_expr_value_expr (expression);
20641
20642 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20643 {
20644 tree elt;
20645 unsigned i;
20646
20647 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20648 {
20649 if (type_dependent_expression_p (elt))
20650 return true;
20651 }
20652 return false;
20653 }
20654
20655 /* A static data member of the current instantiation with incomplete
20656 array type is type-dependent, as the definition and specializations
20657 can have different bounds. */
20658 if (VAR_P (expression)
20659 && DECL_CLASS_SCOPE_P (expression)
20660 && dependent_type_p (DECL_CONTEXT (expression))
20661 && VAR_HAD_UNKNOWN_BOUND (expression))
20662 return true;
20663
20664 /* An array of unknown bound depending on a variadic parameter, eg:
20665
20666 template<typename... Args>
20667 void foo (Args... args)
20668 {
20669 int arr[] = { args... };
20670 }
20671
20672 template<int... vals>
20673 void bar ()
20674 {
20675 int arr[] = { vals... };
20676 }
20677
20678 If the array has no length and has an initializer, it must be that
20679 we couldn't determine its length in cp_complete_array_type because
20680 it is dependent. */
20681 if (VAR_P (expression)
20682 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20683 && !TYPE_DOMAIN (TREE_TYPE (expression))
20684 && DECL_INITIAL (expression))
20685 return true;
20686
20687 if (TREE_TYPE (expression) == unknown_type_node)
20688 {
20689 if (TREE_CODE (expression) == ADDR_EXPR)
20690 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20691 if (TREE_CODE (expression) == COMPONENT_REF
20692 || TREE_CODE (expression) == OFFSET_REF)
20693 {
20694 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20695 return true;
20696 expression = TREE_OPERAND (expression, 1);
20697 if (identifier_p (expression))
20698 return false;
20699 }
20700 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20701 if (TREE_CODE (expression) == SCOPE_REF)
20702 return false;
20703
20704 /* Always dependent, on the number of arguments if nothing else. */
20705 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20706 return true;
20707
20708 if (BASELINK_P (expression))
20709 expression = BASELINK_FUNCTIONS (expression);
20710
20711 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20712 {
20713 if (any_dependent_template_arguments_p
20714 (TREE_OPERAND (expression, 1)))
20715 return true;
20716 expression = TREE_OPERAND (expression, 0);
20717 }
20718 gcc_assert (TREE_CODE (expression) == OVERLOAD
20719 || TREE_CODE (expression) == FUNCTION_DECL);
20720
20721 while (expression)
20722 {
20723 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20724 return true;
20725 expression = OVL_NEXT (expression);
20726 }
20727 return false;
20728 }
20729
20730 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20731
20732 return (dependent_type_p (TREE_TYPE (expression)));
20733 }
20734
20735 /* walk_tree callback function for instantiation_dependent_expression_p,
20736 below. Returns non-zero if a dependent subexpression is found. */
20737
20738 static tree
20739 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20740 void * /*data*/)
20741 {
20742 if (TYPE_P (*tp))
20743 {
20744 /* We don't have to worry about decltype currently because decltype
20745 of an instantiation-dependent expr is a dependent type. This
20746 might change depending on the resolution of DR 1172. */
20747 *walk_subtrees = false;
20748 return NULL_TREE;
20749 }
20750 enum tree_code code = TREE_CODE (*tp);
20751 switch (code)
20752 {
20753 /* Don't treat an argument list as dependent just because it has no
20754 TREE_TYPE. */
20755 case TREE_LIST:
20756 case TREE_VEC:
20757 return NULL_TREE;
20758
20759 case VAR_DECL:
20760 case CONST_DECL:
20761 /* A constant with a dependent initializer is dependent. */
20762 if (value_dependent_expression_p (*tp))
20763 return *tp;
20764 break;
20765
20766 case TEMPLATE_PARM_INDEX:
20767 return *tp;
20768
20769 /* Handle expressions with type operands. */
20770 case SIZEOF_EXPR:
20771 case ALIGNOF_EXPR:
20772 case TYPEID_EXPR:
20773 case AT_ENCODE_EXPR:
20774 {
20775 tree op = TREE_OPERAND (*tp, 0);
20776 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20777 op = TREE_TYPE (op);
20778 if (TYPE_P (op))
20779 {
20780 if (dependent_type_p (op))
20781 return *tp;
20782 else
20783 {
20784 *walk_subtrees = false;
20785 return NULL_TREE;
20786 }
20787 }
20788 break;
20789 }
20790
20791 case TRAIT_EXPR:
20792 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20793 || (TRAIT_EXPR_TYPE2 (*tp)
20794 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20795 return *tp;
20796 *walk_subtrees = false;
20797 return NULL_TREE;
20798
20799 case COMPONENT_REF:
20800 if (identifier_p (TREE_OPERAND (*tp, 1)))
20801 /* In a template, finish_class_member_access_expr creates a
20802 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20803 type-dependent, so that we can check access control at
20804 instantiation time (PR 42277). See also Core issue 1273. */
20805 return *tp;
20806 break;
20807
20808 case SCOPE_REF:
20809 if (instantiation_dependent_scope_ref_p (*tp))
20810 return *tp;
20811 else
20812 break;
20813
20814 /* Treat statement-expressions as dependent. */
20815 case BIND_EXPR:
20816 return *tp;
20817
20818 default:
20819 break;
20820 }
20821
20822 if (type_dependent_expression_p (*tp))
20823 return *tp;
20824 else
20825 return NULL_TREE;
20826 }
20827
20828 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20829 sense defined by the ABI:
20830
20831 "An expression is instantiation-dependent if it is type-dependent
20832 or value-dependent, or it has a subexpression that is type-dependent
20833 or value-dependent." */
20834
20835 bool
20836 instantiation_dependent_expression_p (tree expression)
20837 {
20838 tree result;
20839
20840 if (!processing_template_decl)
20841 return false;
20842
20843 if (expression == error_mark_node)
20844 return false;
20845
20846 result = cp_walk_tree_without_duplicates (&expression,
20847 instantiation_dependent_r, NULL);
20848 return result != NULL_TREE;
20849 }
20850
20851 /* Like type_dependent_expression_p, but it also works while not processing
20852 a template definition, i.e. during substitution or mangling. */
20853
20854 bool
20855 type_dependent_expression_p_push (tree expr)
20856 {
20857 bool b;
20858 ++processing_template_decl;
20859 b = type_dependent_expression_p (expr);
20860 --processing_template_decl;
20861 return b;
20862 }
20863
20864 /* Returns TRUE if ARGS contains a type-dependent expression. */
20865
20866 bool
20867 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20868 {
20869 unsigned int i;
20870 tree arg;
20871
20872 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20873 {
20874 if (type_dependent_expression_p (arg))
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 type-dependent expressions. */
20882
20883 bool
20884 any_type_dependent_elements_p (const_tree list)
20885 {
20886 for (; list; list = TREE_CHAIN (list))
20887 if (type_dependent_expression_p (TREE_VALUE (list)))
20888 return true;
20889
20890 return false;
20891 }
20892
20893 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20894 expressions) contains any value-dependent expressions. */
20895
20896 bool
20897 any_value_dependent_elements_p (const_tree list)
20898 {
20899 for (; list; list = TREE_CHAIN (list))
20900 if (value_dependent_expression_p (TREE_VALUE (list)))
20901 return true;
20902
20903 return false;
20904 }
20905
20906 /* Returns TRUE if the ARG (a template argument) is dependent. */
20907
20908 bool
20909 dependent_template_arg_p (tree arg)
20910 {
20911 if (!processing_template_decl)
20912 return false;
20913
20914 /* Assume a template argument that was wrongly written by the user
20915 is dependent. This is consistent with what
20916 any_dependent_template_arguments_p [that calls this function]
20917 does. */
20918 if (!arg || arg == error_mark_node)
20919 return true;
20920
20921 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20922 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20923
20924 if (TREE_CODE (arg) == TEMPLATE_DECL
20925 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20926 return dependent_template_p (arg);
20927 else if (ARGUMENT_PACK_P (arg))
20928 {
20929 tree args = ARGUMENT_PACK_ARGS (arg);
20930 int i, len = TREE_VEC_LENGTH (args);
20931 for (i = 0; i < len; ++i)
20932 {
20933 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20934 return true;
20935 }
20936
20937 return false;
20938 }
20939 else if (TYPE_P (arg))
20940 return dependent_type_p (arg);
20941 else
20942 return (type_dependent_expression_p (arg)
20943 || value_dependent_expression_p (arg));
20944 }
20945
20946 /* Returns true if ARGS (a collection of template arguments) contains
20947 any types that require structural equality testing. */
20948
20949 bool
20950 any_template_arguments_need_structural_equality_p (tree args)
20951 {
20952 int i;
20953 int j;
20954
20955 if (!args)
20956 return false;
20957 if (args == error_mark_node)
20958 return true;
20959
20960 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20961 {
20962 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20963 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20964 {
20965 tree arg = TREE_VEC_ELT (level, j);
20966 tree packed_args = NULL_TREE;
20967 int k, len = 1;
20968
20969 if (ARGUMENT_PACK_P (arg))
20970 {
20971 /* Look inside the argument pack. */
20972 packed_args = ARGUMENT_PACK_ARGS (arg);
20973 len = TREE_VEC_LENGTH (packed_args);
20974 }
20975
20976 for (k = 0; k < len; ++k)
20977 {
20978 if (packed_args)
20979 arg = TREE_VEC_ELT (packed_args, k);
20980
20981 if (error_operand_p (arg))
20982 return true;
20983 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20984 continue;
20985 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20986 return true;
20987 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20988 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20989 return true;
20990 }
20991 }
20992 }
20993
20994 return false;
20995 }
20996
20997 /* Returns true if ARGS (a collection of template arguments) contains
20998 any dependent arguments. */
20999
21000 bool
21001 any_dependent_template_arguments_p (const_tree args)
21002 {
21003 int i;
21004 int j;
21005
21006 if (!args)
21007 return false;
21008 if (args == error_mark_node)
21009 return true;
21010
21011 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
21012 {
21013 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
21014 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
21015 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
21016 return true;
21017 }
21018
21019 return false;
21020 }
21021
21022 /* Returns TRUE if the template TMPL is dependent. */
21023
21024 bool
21025 dependent_template_p (tree tmpl)
21026 {
21027 if (TREE_CODE (tmpl) == OVERLOAD)
21028 {
21029 while (tmpl)
21030 {
21031 if (dependent_template_p (OVL_CURRENT (tmpl)))
21032 return true;
21033 tmpl = OVL_NEXT (tmpl);
21034 }
21035 return false;
21036 }
21037
21038 /* Template template parameters are dependent. */
21039 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
21040 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
21041 return true;
21042 /* So are names that have not been looked up. */
21043 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
21044 return true;
21045 /* So are member templates of dependent classes. */
21046 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21047 return dependent_type_p (DECL_CONTEXT (tmpl));
21048 return false;
21049 }
21050
21051 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21052
21053 bool
21054 dependent_template_id_p (tree tmpl, tree args)
21055 {
21056 return (dependent_template_p (tmpl)
21057 || any_dependent_template_arguments_p (args));
21058 }
21059
21060 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21061 is dependent. */
21062
21063 bool
21064 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21065 {
21066 int i;
21067
21068 if (!processing_template_decl)
21069 return false;
21070
21071 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21072 {
21073 tree decl = TREE_VEC_ELT (declv, i);
21074 tree init = TREE_VEC_ELT (initv, i);
21075 tree cond = TREE_VEC_ELT (condv, i);
21076 tree incr = TREE_VEC_ELT (incrv, i);
21077
21078 if (type_dependent_expression_p (decl))
21079 return true;
21080
21081 if (init && type_dependent_expression_p (init))
21082 return true;
21083
21084 if (type_dependent_expression_p (cond))
21085 return true;
21086
21087 if (COMPARISON_CLASS_P (cond)
21088 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21089 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21090 return true;
21091
21092 if (TREE_CODE (incr) == MODOP_EXPR)
21093 {
21094 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21095 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21096 return true;
21097 }
21098 else if (type_dependent_expression_p (incr))
21099 return true;
21100 else if (TREE_CODE (incr) == MODIFY_EXPR)
21101 {
21102 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21103 return true;
21104 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21105 {
21106 tree t = TREE_OPERAND (incr, 1);
21107 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21108 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21109 return true;
21110 }
21111 }
21112 }
21113
21114 return false;
21115 }
21116
21117 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21118 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21119 no such TYPE can be found. Note that this function peers inside
21120 uninstantiated templates and therefore should be used only in
21121 extremely limited situations. ONLY_CURRENT_P restricts this
21122 peering to the currently open classes hierarchy (which is required
21123 when comparing types). */
21124
21125 tree
21126 resolve_typename_type (tree type, bool only_current_p)
21127 {
21128 tree scope;
21129 tree name;
21130 tree decl;
21131 int quals;
21132 tree pushed_scope;
21133 tree result;
21134
21135 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21136
21137 scope = TYPE_CONTEXT (type);
21138 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21139 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21140 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21141 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21142 identifier of the TYPENAME_TYPE anymore.
21143 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21144 TYPENAME_TYPE instead, we avoid messing up with a possible
21145 typedef variant case. */
21146 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21147
21148 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21149 it first before we can figure out what NAME refers to. */
21150 if (TREE_CODE (scope) == TYPENAME_TYPE)
21151 {
21152 if (TYPENAME_IS_RESOLVING_P (scope))
21153 /* Given a class template A with a dependent base with nested type C,
21154 typedef typename A::C::C C will land us here, as trying to resolve
21155 the initial A::C leads to the local C typedef, which leads back to
21156 A::C::C. So we break the recursion now. */
21157 return type;
21158 else
21159 scope = resolve_typename_type (scope, only_current_p);
21160 }
21161 /* If we don't know what SCOPE refers to, then we cannot resolve the
21162 TYPENAME_TYPE. */
21163 if (TREE_CODE (scope) == TYPENAME_TYPE)
21164 return type;
21165 /* If the SCOPE is a template type parameter, we have no way of
21166 resolving the name. */
21167 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21168 return type;
21169 /* If the SCOPE is not the current instantiation, there's no reason
21170 to look inside it. */
21171 if (only_current_p && !currently_open_class (scope))
21172 return type;
21173 /* If this is a typedef, we don't want to look inside (c++/11987). */
21174 if (typedef_variant_p (type))
21175 return type;
21176 /* If SCOPE isn't the template itself, it will not have a valid
21177 TYPE_FIELDS list. */
21178 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21179 /* scope is either the template itself or a compatible instantiation
21180 like X<T>, so look up the name in the original template. */
21181 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21182 else
21183 /* scope is a partial instantiation, so we can't do the lookup or we
21184 will lose the template arguments. */
21185 return type;
21186 /* Enter the SCOPE so that name lookup will be resolved as if we
21187 were in the class definition. In particular, SCOPE will no
21188 longer be considered a dependent type. */
21189 pushed_scope = push_scope (scope);
21190 /* Look up the declaration. */
21191 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21192 tf_warning_or_error);
21193
21194 result = NULL_TREE;
21195
21196 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21197 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21198 if (!decl)
21199 /*nop*/;
21200 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21201 && TREE_CODE (decl) == TYPE_DECL)
21202 {
21203 result = TREE_TYPE (decl);
21204 if (result == error_mark_node)
21205 result = NULL_TREE;
21206 }
21207 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21208 && DECL_CLASS_TEMPLATE_P (decl))
21209 {
21210 tree tmpl;
21211 tree args;
21212 /* Obtain the template and the arguments. */
21213 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21214 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21215 /* Instantiate the template. */
21216 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21217 /*entering_scope=*/0,
21218 tf_error | tf_user);
21219 if (result == error_mark_node)
21220 result = NULL_TREE;
21221 }
21222
21223 /* Leave the SCOPE. */
21224 if (pushed_scope)
21225 pop_scope (pushed_scope);
21226
21227 /* If we failed to resolve it, return the original typename. */
21228 if (!result)
21229 return type;
21230
21231 /* If lookup found a typename type, resolve that too. */
21232 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21233 {
21234 /* Ill-formed programs can cause infinite recursion here, so we
21235 must catch that. */
21236 TYPENAME_IS_RESOLVING_P (type) = 1;
21237 result = resolve_typename_type (result, only_current_p);
21238 TYPENAME_IS_RESOLVING_P (type) = 0;
21239 }
21240
21241 /* Qualify the resulting type. */
21242 quals = cp_type_quals (type);
21243 if (quals)
21244 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21245
21246 return result;
21247 }
21248
21249 /* EXPR is an expression which is not type-dependent. Return a proxy
21250 for EXPR that can be used to compute the types of larger
21251 expressions containing EXPR. */
21252
21253 tree
21254 build_non_dependent_expr (tree expr)
21255 {
21256 tree inner_expr;
21257
21258 #ifdef ENABLE_CHECKING
21259 /* Try to get a constant value for all non-dependent expressions in
21260 order to expose bugs in *_dependent_expression_p and constexpr. */
21261 if (cxx_dialect >= cxx11)
21262 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21263 #endif
21264
21265 /* Preserve OVERLOADs; the functions must be available to resolve
21266 types. */
21267 inner_expr = expr;
21268 if (TREE_CODE (inner_expr) == STMT_EXPR)
21269 inner_expr = stmt_expr_value_expr (inner_expr);
21270 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21271 inner_expr = TREE_OPERAND (inner_expr, 0);
21272 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21273 inner_expr = TREE_OPERAND (inner_expr, 1);
21274 if (is_overloaded_fn (inner_expr)
21275 || TREE_CODE (inner_expr) == OFFSET_REF)
21276 return expr;
21277 /* There is no need to return a proxy for a variable. */
21278 if (VAR_P (expr))
21279 return expr;
21280 /* Preserve string constants; conversions from string constants to
21281 "char *" are allowed, even though normally a "const char *"
21282 cannot be used to initialize a "char *". */
21283 if (TREE_CODE (expr) == STRING_CST)
21284 return expr;
21285 /* Preserve arithmetic constants, as an optimization -- there is no
21286 reason to create a new node. */
21287 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21288 return expr;
21289 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21290 There is at least one place where we want to know that a
21291 particular expression is a throw-expression: when checking a ?:
21292 expression, there are special rules if the second or third
21293 argument is a throw-expression. */
21294 if (TREE_CODE (expr) == THROW_EXPR)
21295 return expr;
21296
21297 /* Don't wrap an initializer list, we need to be able to look inside. */
21298 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21299 return expr;
21300
21301 /* Don't wrap a dummy object, we need to be able to test for it. */
21302 if (is_dummy_object (expr))
21303 return expr;
21304
21305 if (TREE_CODE (expr) == COND_EXPR)
21306 return build3 (COND_EXPR,
21307 TREE_TYPE (expr),
21308 TREE_OPERAND (expr, 0),
21309 (TREE_OPERAND (expr, 1)
21310 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21311 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21312 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21313 if (TREE_CODE (expr) == COMPOUND_EXPR
21314 && !COMPOUND_EXPR_OVERLOADED (expr))
21315 return build2 (COMPOUND_EXPR,
21316 TREE_TYPE (expr),
21317 TREE_OPERAND (expr, 0),
21318 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21319
21320 /* If the type is unknown, it can't really be non-dependent */
21321 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21322
21323 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21324 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21325 }
21326
21327 /* ARGS is a vector of expressions as arguments to a function call.
21328 Replace the arguments with equivalent non-dependent expressions.
21329 This modifies ARGS in place. */
21330
21331 void
21332 make_args_non_dependent (vec<tree, va_gc> *args)
21333 {
21334 unsigned int ix;
21335 tree arg;
21336
21337 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21338 {
21339 tree newarg = build_non_dependent_expr (arg);
21340 if (newarg != arg)
21341 (*args)[ix] = newarg;
21342 }
21343 }
21344
21345 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21346 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21347 parms. */
21348
21349 static tree
21350 make_auto_1 (tree name)
21351 {
21352 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21353 TYPE_NAME (au) = build_decl (input_location,
21354 TYPE_DECL, name, au);
21355 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21356 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21357 (0, processing_template_decl + 1, processing_template_decl + 1,
21358 TYPE_NAME (au), NULL_TREE);
21359 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21360 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21361 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21362
21363 return au;
21364 }
21365
21366 tree
21367 make_decltype_auto (void)
21368 {
21369 return make_auto_1 (get_identifier ("decltype(auto)"));
21370 }
21371
21372 tree
21373 make_auto (void)
21374 {
21375 return make_auto_1 (get_identifier ("auto"));
21376 }
21377
21378 /* Given type ARG, return std::initializer_list<ARG>. */
21379
21380 static tree
21381 listify (tree arg)
21382 {
21383 tree std_init_list = namespace_binding
21384 (get_identifier ("initializer_list"), std_node);
21385 tree argvec;
21386 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21387 {
21388 error ("deducing from brace-enclosed initializer list requires "
21389 "#include <initializer_list>");
21390 return error_mark_node;
21391 }
21392 argvec = make_tree_vec (1);
21393 TREE_VEC_ELT (argvec, 0) = arg;
21394 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21395 NULL_TREE, 0, tf_warning_or_error);
21396 }
21397
21398 /* Replace auto in TYPE with std::initializer_list<auto>. */
21399
21400 static tree
21401 listify_autos (tree type, tree auto_node)
21402 {
21403 tree init_auto = listify (auto_node);
21404 tree argvec = make_tree_vec (1);
21405 TREE_VEC_ELT (argvec, 0) = init_auto;
21406 if (processing_template_decl)
21407 argvec = add_to_template_args (current_template_args (), argvec);
21408 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21409 }
21410
21411 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21412 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21413
21414 tree
21415 do_auto_deduction (tree type, tree init, tree auto_node)
21416 {
21417 tree targs;
21418
21419 if (init == error_mark_node)
21420 return error_mark_node;
21421
21422 if (type_dependent_expression_p (init))
21423 /* Defining a subset of type-dependent expressions that we can deduce
21424 from ahead of time isn't worth the trouble. */
21425 return type;
21426
21427 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21428 with either a new invented type template parameter U or, if the
21429 initializer is a braced-init-list (8.5.4), with
21430 std::initializer_list<U>. */
21431 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21432 type = listify_autos (type, auto_node);
21433
21434 init = resolve_nondeduced_context (init);
21435
21436 targs = make_tree_vec (1);
21437 if (AUTO_IS_DECLTYPE (auto_node))
21438 {
21439 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21440 TREE_VEC_ELT (targs, 0)
21441 = finish_decltype_type (init, id, tf_warning_or_error);
21442 if (type != auto_node)
21443 {
21444 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21445 return error_mark_node;
21446 }
21447 }
21448 else
21449 {
21450 tree parms = build_tree_list (NULL_TREE, type);
21451 tree tparms = make_tree_vec (1);
21452 int val;
21453
21454 TREE_VEC_ELT (tparms, 0)
21455 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21456 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21457 DEDUCE_CALL, LOOKUP_NORMAL,
21458 NULL, /*explain_p=*/false);
21459 if (val > 0)
21460 {
21461 if (processing_template_decl)
21462 /* Try again at instantiation time. */
21463 return type;
21464 if (type && type != error_mark_node)
21465 /* If type is error_mark_node a diagnostic must have been
21466 emitted by now. Also, having a mention to '<type error>'
21467 in the diagnostic is not really useful to the user. */
21468 {
21469 if (cfun && auto_node == current_function_auto_return_pattern
21470 && LAMBDA_FUNCTION_P (current_function_decl))
21471 error ("unable to deduce lambda return type from %qE", init);
21472 else
21473 error ("unable to deduce %qT from %qE", type, init);
21474 }
21475 return error_mark_node;
21476 }
21477 }
21478
21479 /* If the list of declarators contains more than one declarator, the type
21480 of each declared variable is determined as described above. If the
21481 type deduced for the template parameter U is not the same in each
21482 deduction, the program is ill-formed. */
21483 if (TREE_TYPE (auto_node)
21484 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21485 {
21486 if (cfun && auto_node == current_function_auto_return_pattern
21487 && LAMBDA_FUNCTION_P (current_function_decl))
21488 error ("inconsistent types %qT and %qT deduced for "
21489 "lambda return type", TREE_TYPE (auto_node),
21490 TREE_VEC_ELT (targs, 0));
21491 else
21492 error ("inconsistent deduction for %qT: %qT and then %qT",
21493 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21494 return error_mark_node;
21495 }
21496 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21497
21498 if (processing_template_decl)
21499 targs = add_to_template_args (current_template_args (), targs);
21500 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21501 }
21502
21503 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21504 result. */
21505
21506 tree
21507 splice_late_return_type (tree type, tree late_return_type)
21508 {
21509 tree argvec;
21510
21511 if (late_return_type == NULL_TREE)
21512 return type;
21513 argvec = make_tree_vec (1);
21514 TREE_VEC_ELT (argvec, 0) = late_return_type;
21515 if (processing_template_parmlist)
21516 /* For a late-specified return type in a template type-parameter, we
21517 need to add a dummy argument level for its parmlist. */
21518 argvec = add_to_template_args
21519 (make_tree_vec (processing_template_parmlist), argvec);
21520 if (current_template_parms)
21521 argvec = add_to_template_args (current_template_args (), argvec);
21522 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21523 }
21524
21525 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21526 'decltype(auto)'. */
21527
21528 bool
21529 is_auto (const_tree type)
21530 {
21531 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21532 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21533 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21534 return true;
21535 else
21536 return false;
21537 }
21538
21539 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21540 a use of `auto'. Returns NULL_TREE otherwise. */
21541
21542 tree
21543 type_uses_auto (tree type)
21544 {
21545 return find_type_usage (type, is_auto);
21546 }
21547
21548 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21549 'decltype(auto)' or a concept. */
21550
21551 bool
21552 is_auto_or_concept (const_tree type)
21553 {
21554 return is_auto (type); // or concept
21555 }
21556
21557 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21558 a concept identifier) iff TYPE contains a use of a generic type. Returns
21559 NULL_TREE otherwise. */
21560
21561 tree
21562 type_uses_auto_or_concept (tree type)
21563 {
21564 return find_type_usage (type, is_auto_or_concept);
21565 }
21566
21567
21568 /* For a given template T, return the vector of typedefs referenced
21569 in T for which access check is needed at T instantiation time.
21570 T is either a FUNCTION_DECL or a RECORD_TYPE.
21571 Those typedefs were added to T by the function
21572 append_type_to_template_for_access_check. */
21573
21574 vec<qualified_typedef_usage_t, va_gc> *
21575 get_types_needing_access_check (tree t)
21576 {
21577 tree ti;
21578 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21579
21580 if (!t || t == error_mark_node)
21581 return NULL;
21582
21583 if (!(ti = get_template_info (t)))
21584 return NULL;
21585
21586 if (CLASS_TYPE_P (t)
21587 || TREE_CODE (t) == FUNCTION_DECL)
21588 {
21589 if (!TI_TEMPLATE (ti))
21590 return NULL;
21591
21592 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21593 }
21594
21595 return result;
21596 }
21597
21598 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21599 tied to T. That list of typedefs will be access checked at
21600 T instantiation time.
21601 T is either a FUNCTION_DECL or a RECORD_TYPE.
21602 TYPE_DECL is a TYPE_DECL node representing a typedef.
21603 SCOPE is the scope through which TYPE_DECL is accessed.
21604 LOCATION is the location of the usage point of TYPE_DECL.
21605
21606 This function is a subroutine of
21607 append_type_to_template_for_access_check. */
21608
21609 static void
21610 append_type_to_template_for_access_check_1 (tree t,
21611 tree type_decl,
21612 tree scope,
21613 location_t location)
21614 {
21615 qualified_typedef_usage_t typedef_usage;
21616 tree ti;
21617
21618 if (!t || t == error_mark_node)
21619 return;
21620
21621 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21622 || CLASS_TYPE_P (t))
21623 && type_decl
21624 && TREE_CODE (type_decl) == TYPE_DECL
21625 && scope);
21626
21627 if (!(ti = get_template_info (t)))
21628 return;
21629
21630 gcc_assert (TI_TEMPLATE (ti));
21631
21632 typedef_usage.typedef_decl = type_decl;
21633 typedef_usage.context = scope;
21634 typedef_usage.locus = location;
21635
21636 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21637 }
21638
21639 /* Append TYPE_DECL to the template TEMPL.
21640 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21641 At TEMPL instanciation time, TYPE_DECL will be checked to see
21642 if it can be accessed through SCOPE.
21643 LOCATION is the location of the usage point of TYPE_DECL.
21644
21645 e.g. consider the following code snippet:
21646
21647 class C
21648 {
21649 typedef int myint;
21650 };
21651
21652 template<class U> struct S
21653 {
21654 C::myint mi; // <-- usage point of the typedef C::myint
21655 };
21656
21657 S<char> s;
21658
21659 At S<char> instantiation time, we need to check the access of C::myint
21660 In other words, we need to check the access of the myint typedef through
21661 the C scope. For that purpose, this function will add the myint typedef
21662 and the scope C through which its being accessed to a list of typedefs
21663 tied to the template S. That list will be walked at template instantiation
21664 time and access check performed on each typedefs it contains.
21665 Note that this particular code snippet should yield an error because
21666 myint is private to C. */
21667
21668 void
21669 append_type_to_template_for_access_check (tree templ,
21670 tree type_decl,
21671 tree scope,
21672 location_t location)
21673 {
21674 qualified_typedef_usage_t *iter;
21675 unsigned i;
21676
21677 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21678
21679 /* Make sure we don't append the type to the template twice. */
21680 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21681 if (iter->typedef_decl == type_decl && scope == iter->context)
21682 return;
21683
21684 append_type_to_template_for_access_check_1 (templ, type_decl,
21685 scope, location);
21686 }
21687
21688 /* Convert the generic type parameters in PARM that match the types given in the
21689 range [START_IDX, END_IDX) from the current_template_parms into generic type
21690 packs. */
21691
21692 tree
21693 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21694 {
21695 tree current = current_template_parms;
21696 int depth = TMPL_PARMS_DEPTH (current);
21697 current = INNERMOST_TEMPLATE_PARMS (current);
21698 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21699
21700 for (int i = 0; i < start_idx; ++i)
21701 TREE_VEC_ELT (replacement, i)
21702 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21703
21704 for (int i = start_idx; i < end_idx; ++i)
21705 {
21706 /* Create a distinct parameter pack type from the current parm and add it
21707 to the replacement args to tsubst below into the generic function
21708 parameter. */
21709
21710 tree o = TREE_TYPE (TREE_VALUE
21711 (TREE_VEC_ELT (current, i)));
21712 tree t = copy_type (o);
21713 TEMPLATE_TYPE_PARM_INDEX (t)
21714 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21715 o, 0, 0, tf_none);
21716 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21717 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21718 TYPE_MAIN_VARIANT (t) = t;
21719 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21720 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21721 TREE_VEC_ELT (replacement, i) = t;
21722 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21723 }
21724
21725 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21726 TREE_VEC_ELT (replacement, i)
21727 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21728
21729 /* If there are more levels then build up the replacement with the outer
21730 template parms. */
21731 if (depth > 1)
21732 replacement = add_to_template_args (template_parms_to_args
21733 (TREE_CHAIN (current_template_parms)),
21734 replacement);
21735
21736 return tsubst (parm, replacement, tf_none, NULL_TREE);
21737 }
21738
21739
21740 /* Set up the hash tables for template instantiations. */
21741
21742 void
21743 init_template_processing (void)
21744 {
21745 decl_specializations = htab_create_ggc (37,
21746 hash_specialization,
21747 eq_specializations,
21748 ggc_free);
21749 type_specializations = htab_create_ggc (37,
21750 hash_specialization,
21751 eq_specializations,
21752 ggc_free);
21753 }
21754
21755 /* Print stats about the template hash tables for -fstats. */
21756
21757 void
21758 print_template_statistics (void)
21759 {
21760 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21761 "%f collisions\n", (long) htab_size (decl_specializations),
21762 (long) htab_elements (decl_specializations),
21763 htab_collisions (decl_specializations));
21764 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21765 "%f collisions\n", (long) htab_size (type_specializations),
21766 (long) htab_elements (type_specializations),
21767 htab_collisions (type_specializations));
21768 }
21769
21770 #include "gt-cp-pt.h"