re PR c++/57887 (nested non-type template parameters not declared in this scope)
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992-2013 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 "intl.h"
33 #include "pointer-set.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "toplev.h"
42 #include "timevar.h"
43 #include "tree-iterator.h"
44 #include "type-utils.h"
45 #include "gimple.h"
46 #include "gimplify.h"
47
48 /* The type of functions taking a tree, and some additional data, and
49 returning an int. */
50 typedef int (*tree_fn_t) (tree, void*);
51
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY ((chain_next ("%h.next"))) pending_template {
56 struct pending_template *next;
57 struct tinst_level *tinst;
58 };
59
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
62
63 int processing_template_parmlist;
64 static int template_header_count;
65
66 static GTY(()) tree saved_trees;
67 static vec<int> inline_parm_levels;
68
69 static GTY(()) struct tinst_level *current_tinst_level;
70
71 static GTY(()) tree saved_access_scope;
72
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
77
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81 static struct pointer_map_t *local_specializations;
82
83 /* True if we've recursed into fn_type_unification too many times. */
84 static bool excessive_deduction_depth;
85
86 typedef struct GTY(()) spec_entry
87 {
88 tree tmpl;
89 tree args;
90 tree spec;
91 } spec_entry;
92
93 static GTY ((param_is (spec_entry)))
94 htab_t decl_specializations;
95
96 static GTY ((param_is (spec_entry)))
97 htab_t type_specializations;
98
99 /* Contains canonical template parameter types. The vector is indexed by
100 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
101 TREE_LIST, whose TREE_VALUEs contain the canonical template
102 parameters of various types and levels. */
103 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
104
105 #define UNIFY_ALLOW_NONE 0
106 #define UNIFY_ALLOW_MORE_CV_QUAL 1
107 #define UNIFY_ALLOW_LESS_CV_QUAL 2
108 #define UNIFY_ALLOW_DERIVED 4
109 #define UNIFY_ALLOW_INTEGER 8
110 #define UNIFY_ALLOW_OUTER_LEVEL 16
111 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
112 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
113
114 enum template_base_result {
115 tbr_incomplete_type,
116 tbr_ambiguous_baseclass,
117 tbr_success
118 };
119
120 static void push_access_scope (tree);
121 static void pop_access_scope (tree);
122 static bool resolve_overloaded_unification (tree, tree, tree, tree,
123 unification_kind_t, int,
124 bool);
125 static int try_one_overload (tree, tree, tree, tree, tree,
126 unification_kind_t, int, bool, bool);
127 static int unify (tree, tree, tree, tree, int, bool);
128 static void add_pending_template (tree);
129 static tree reopen_tinst_level (struct tinst_level *);
130 static tree tsubst_initializer_list (tree, tree);
131 static tree get_class_bindings (tree, tree, tree, tree);
132 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
133 bool, bool);
134 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
135 bool, bool);
136 static void tsubst_enum (tree, tree, tree);
137 static tree add_to_template_args (tree, tree);
138 static tree add_outermost_template_args (tree, tree);
139 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
140 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
141 tree);
142 static int type_unification_real (tree, tree, tree, const tree *,
143 unsigned int, int, unification_kind_t, int,
144 vec<deferred_access_check, va_gc> **,
145 bool);
146 static void note_template_header (int);
147 static tree convert_nontype_argument_function (tree, tree);
148 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
149 static tree convert_template_argument (tree, tree, tree,
150 tsubst_flags_t, int, tree);
151 static int for_each_template_parm (tree, tree_fn_t, void*,
152 struct pointer_set_t*, bool);
153 static tree expand_template_argument_pack (tree);
154 static tree build_template_parm_index (int, int, int, tree, tree);
155 static bool inline_needs_template_parms (tree, bool);
156 static void push_inline_template_parms_recursive (tree, int);
157 static tree retrieve_local_specialization (tree);
158 static void register_local_specialization (tree, tree);
159 static hashval_t hash_specialization (const void *p);
160 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
161 static int mark_template_parm (tree, void *);
162 static int template_parm_this_level_p (tree, void *);
163 static tree tsubst_friend_function (tree, tree);
164 static tree tsubst_friend_class (tree, tree);
165 static int can_complete_type_without_circularity (tree);
166 static tree get_bindings (tree, tree, tree, bool);
167 static int template_decl_level (tree);
168 static int check_cv_quals_for_unify (int, tree, tree);
169 static void template_parm_level_and_index (tree, int*, int*);
170 static int unify_pack_expansion (tree, tree, tree,
171 tree, unification_kind_t, bool, bool);
172 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
173 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
175 static void regenerate_decl_from_template (tree, tree);
176 static tree most_specialized_class (tree, tree, tsubst_flags_t);
177 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
178 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
179 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
180 static bool check_specialization_scope (void);
181 static tree process_partial_specialization (tree);
182 static void set_current_access_from_decl (tree);
183 static enum template_base_result get_template_base (tree, tree, tree, tree,
184 bool , tree *);
185 static tree try_class_unification (tree, tree, tree, tree, bool);
186 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
187 tree, tree);
188 static bool template_template_parm_bindings_ok_p (tree, tree);
189 static int template_args_equal (tree, tree);
190 static void tsubst_default_arguments (tree, tsubst_flags_t);
191 static tree for_each_template_parm_r (tree *, int *, void *);
192 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
193 static void copy_default_args_to_explicit_spec (tree);
194 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
195 static bool dependent_template_arg_p (tree);
196 static bool any_template_arguments_need_structural_equality_p (tree);
197 static bool dependent_type_p_r (tree);
198 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
199 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
200 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
201 static tree tsubst_decl (tree, tree, tsubst_flags_t);
202 static void perform_typedefs_access_check (tree tmpl, tree targs);
203 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
204 location_t);
205 static tree listify (tree);
206 static tree listify_autos (tree, tree);
207 static tree template_parm_to_arg (tree t);
208 static tree current_template_args (void);
209 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
210 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
211
212 /* Make the current scope suitable for access checking when we are
213 processing T. T can be FUNCTION_DECL for instantiated function
214 template, VAR_DECL for static member variable, or TYPE_DECL for
215 alias template (needed by instantiate_decl). */
216
217 static void
218 push_access_scope (tree t)
219 {
220 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
221 || TREE_CODE (t) == TYPE_DECL);
222
223 if (DECL_FRIEND_CONTEXT (t))
224 push_nested_class (DECL_FRIEND_CONTEXT (t));
225 else if (DECL_CLASS_SCOPE_P (t))
226 push_nested_class (DECL_CONTEXT (t));
227 else
228 push_to_top_level ();
229
230 if (TREE_CODE (t) == FUNCTION_DECL)
231 {
232 saved_access_scope = tree_cons
233 (NULL_TREE, current_function_decl, saved_access_scope);
234 current_function_decl = t;
235 }
236 }
237
238 /* Restore the scope set up by push_access_scope. T is the node we
239 are processing. */
240
241 static void
242 pop_access_scope (tree t)
243 {
244 if (TREE_CODE (t) == FUNCTION_DECL)
245 {
246 current_function_decl = TREE_VALUE (saved_access_scope);
247 saved_access_scope = TREE_CHAIN (saved_access_scope);
248 }
249
250 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
251 pop_nested_class ();
252 else
253 pop_from_top_level ();
254 }
255
256 /* Do any processing required when DECL (a member template
257 declaration) is finished. Returns the TEMPLATE_DECL corresponding
258 to DECL, unless it is a specialization, in which case the DECL
259 itself is returned. */
260
261 tree
262 finish_member_template_decl (tree decl)
263 {
264 if (decl == error_mark_node)
265 return error_mark_node;
266
267 gcc_assert (DECL_P (decl));
268
269 if (TREE_CODE (decl) == TYPE_DECL)
270 {
271 tree type;
272
273 type = TREE_TYPE (decl);
274 if (type == error_mark_node)
275 return error_mark_node;
276 if (MAYBE_CLASS_TYPE_P (type)
277 && CLASSTYPE_TEMPLATE_INFO (type)
278 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
279 {
280 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
281 check_member_template (tmpl);
282 return tmpl;
283 }
284 return NULL_TREE;
285 }
286 else if (TREE_CODE (decl) == FIELD_DECL)
287 error ("data member %qD cannot be a member template", decl);
288 else if (DECL_TEMPLATE_INFO (decl))
289 {
290 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
291 {
292 check_member_template (DECL_TI_TEMPLATE (decl));
293 return DECL_TI_TEMPLATE (decl);
294 }
295 else
296 return decl;
297 }
298 else
299 error ("invalid member template declaration %qD", decl);
300
301 return error_mark_node;
302 }
303
304 /* Create a template info node. */
305
306 tree
307 build_template_info (tree template_decl, tree template_args)
308 {
309 tree result = make_node (TEMPLATE_INFO);
310 TI_TEMPLATE (result) = template_decl;
311 TI_ARGS (result) = template_args;
312 return result;
313 }
314
315 /* Return the template info node corresponding to T, whatever T is. */
316
317 tree
318 get_template_info (const_tree t)
319 {
320 tree tinfo = NULL_TREE;
321
322 if (!t || t == error_mark_node)
323 return NULL;
324
325 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
326 tinfo = DECL_TEMPLATE_INFO (t);
327
328 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
329 t = TREE_TYPE (t);
330
331 if (OVERLOAD_TYPE_P (t))
332 tinfo = TYPE_TEMPLATE_INFO (t);
333 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
334 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
335
336 return tinfo;
337 }
338
339 /* Returns the template nesting level of the indicated class TYPE.
340
341 For example, in:
342 template <class T>
343 struct A
344 {
345 template <class U>
346 struct B {};
347 };
348
349 A<T>::B<U> has depth two, while A<T> has depth one.
350 Both A<T>::B<int> and A<int>::B<U> have depth one, if
351 they are instantiations, not specializations.
352
353 This function is guaranteed to return 0 if passed NULL_TREE so
354 that, for example, `template_class_depth (current_class_type)' is
355 always safe. */
356
357 int
358 template_class_depth (tree type)
359 {
360 int depth;
361
362 for (depth = 0;
363 type && TREE_CODE (type) != NAMESPACE_DECL;
364 type = (TREE_CODE (type) == FUNCTION_DECL)
365 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
366 {
367 tree tinfo = get_template_info (type);
368
369 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
370 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
371 ++depth;
372 }
373
374 return depth;
375 }
376
377 /* Subroutine of maybe_begin_member_template_processing.
378 Returns true if processing DECL needs us to push template parms. */
379
380 static bool
381 inline_needs_template_parms (tree decl, bool nsdmi)
382 {
383 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
384 return false;
385
386 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
387 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
388 }
389
390 /* Subroutine of maybe_begin_member_template_processing.
391 Push the template parms in PARMS, starting from LEVELS steps into the
392 chain, and ending at the beginning, since template parms are listed
393 innermost first. */
394
395 static void
396 push_inline_template_parms_recursive (tree parmlist, int levels)
397 {
398 tree parms = TREE_VALUE (parmlist);
399 int i;
400
401 if (levels > 1)
402 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
403
404 ++processing_template_decl;
405 current_template_parms
406 = tree_cons (size_int (processing_template_decl),
407 parms, current_template_parms);
408 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
409
410 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
411 NULL);
412 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
413 {
414 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
415
416 if (parm == error_mark_node)
417 continue;
418
419 gcc_assert (DECL_P (parm));
420
421 switch (TREE_CODE (parm))
422 {
423 case TYPE_DECL:
424 case TEMPLATE_DECL:
425 pushdecl (parm);
426 break;
427
428 case PARM_DECL:
429 {
430 /* Make a CONST_DECL as is done in process_template_parm.
431 It is ugly that we recreate this here; the original
432 version built in process_template_parm is no longer
433 available. */
434 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
435 CONST_DECL, DECL_NAME (parm),
436 TREE_TYPE (parm));
437 DECL_ARTIFICIAL (decl) = 1;
438 TREE_CONSTANT (decl) = 1;
439 TREE_READONLY (decl) = 1;
440 DECL_INITIAL (decl) = DECL_INITIAL (parm);
441 SET_DECL_TEMPLATE_PARM_P (decl);
442 pushdecl (decl);
443 }
444 break;
445
446 default:
447 gcc_unreachable ();
448 }
449 }
450 }
451
452 /* Restore the template parameter context for a member template, a
453 friend template defined in a class definition, or a non-template
454 member of template class. */
455
456 void
457 maybe_begin_member_template_processing (tree decl)
458 {
459 tree parms;
460 int levels = 0;
461 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
462
463 if (nsdmi)
464 decl = (CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
465 ? CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (decl))
466 : NULL_TREE);
467
468 if (inline_needs_template_parms (decl, nsdmi))
469 {
470 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
471 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
472
473 if (DECL_TEMPLATE_SPECIALIZATION (decl))
474 {
475 --levels;
476 parms = TREE_CHAIN (parms);
477 }
478
479 push_inline_template_parms_recursive (parms, levels);
480 }
481
482 /* Remember how many levels of template parameters we pushed so that
483 we can pop them later. */
484 inline_parm_levels.safe_push (levels);
485 }
486
487 /* Undo the effects of maybe_begin_member_template_processing. */
488
489 void
490 maybe_end_member_template_processing (void)
491 {
492 int i;
493 int last;
494
495 if (inline_parm_levels.length () == 0)
496 return;
497
498 last = inline_parm_levels.pop ();
499 for (i = 0; i < last; ++i)
500 {
501 --processing_template_decl;
502 current_template_parms = TREE_CHAIN (current_template_parms);
503 poplevel (0, 0, 0);
504 }
505 }
506
507 /* Return a new template argument vector which contains all of ARGS,
508 but has as its innermost set of arguments the EXTRA_ARGS. */
509
510 static tree
511 add_to_template_args (tree args, tree extra_args)
512 {
513 tree new_args;
514 int extra_depth;
515 int i;
516 int j;
517
518 if (args == NULL_TREE || extra_args == error_mark_node)
519 return extra_args;
520
521 extra_depth = TMPL_ARGS_DEPTH (extra_args);
522 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
523
524 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
525 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
526
527 for (j = 1; j <= extra_depth; ++j, ++i)
528 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
529
530 return new_args;
531 }
532
533 /* Like add_to_template_args, but only the outermost ARGS are added to
534 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
535 (EXTRA_ARGS) levels are added. This function is used to combine
536 the template arguments from a partial instantiation with the
537 template arguments used to attain the full instantiation from the
538 partial instantiation. */
539
540 static tree
541 add_outermost_template_args (tree args, tree extra_args)
542 {
543 tree new_args;
544
545 /* If there are more levels of EXTRA_ARGS than there are ARGS,
546 something very fishy is going on. */
547 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
548
549 /* If *all* the new arguments will be the EXTRA_ARGS, just return
550 them. */
551 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
552 return extra_args;
553
554 /* For the moment, we make ARGS look like it contains fewer levels. */
555 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
556
557 new_args = add_to_template_args (args, extra_args);
558
559 /* Now, we restore ARGS to its full dimensions. */
560 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
561
562 return new_args;
563 }
564
565 /* Return the N levels of innermost template arguments from the ARGS. */
566
567 tree
568 get_innermost_template_args (tree args, int n)
569 {
570 tree new_args;
571 int extra_levels;
572 int i;
573
574 gcc_assert (n >= 0);
575
576 /* If N is 1, just return the innermost set of template arguments. */
577 if (n == 1)
578 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
579
580 /* If we're not removing anything, just return the arguments we were
581 given. */
582 extra_levels = TMPL_ARGS_DEPTH (args) - n;
583 gcc_assert (extra_levels >= 0);
584 if (extra_levels == 0)
585 return args;
586
587 /* Make a new set of arguments, not containing the outer arguments. */
588 new_args = make_tree_vec (n);
589 for (i = 1; i <= n; ++i)
590 SET_TMPL_ARGS_LEVEL (new_args, i,
591 TMPL_ARGS_LEVEL (args, i + extra_levels));
592
593 return new_args;
594 }
595
596 /* The inverse of get_innermost_template_args: Return all but the innermost
597 EXTRA_LEVELS levels of template arguments from the ARGS. */
598
599 static tree
600 strip_innermost_template_args (tree args, int extra_levels)
601 {
602 tree new_args;
603 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
604 int i;
605
606 gcc_assert (n >= 0);
607
608 /* If N is 1, just return the outermost set of template arguments. */
609 if (n == 1)
610 return TMPL_ARGS_LEVEL (args, 1);
611
612 /* If we're not removing anything, just return the arguments we were
613 given. */
614 gcc_assert (extra_levels >= 0);
615 if (extra_levels == 0)
616 return args;
617
618 /* Make a new set of arguments, not containing the inner arguments. */
619 new_args = make_tree_vec (n);
620 for (i = 1; i <= n; ++i)
621 SET_TMPL_ARGS_LEVEL (new_args, i,
622 TMPL_ARGS_LEVEL (args, i));
623
624 return new_args;
625 }
626
627 /* We've got a template header coming up; push to a new level for storing
628 the parms. */
629
630 void
631 begin_template_parm_list (void)
632 {
633 /* We use a non-tag-transparent scope here, which causes pushtag to
634 put tags in this scope, rather than in the enclosing class or
635 namespace scope. This is the right thing, since we want
636 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
637 global template class, push_template_decl handles putting the
638 TEMPLATE_DECL into top-level scope. For a nested template class,
639 e.g.:
640
641 template <class T> struct S1 {
642 template <class T> struct S2 {};
643 };
644
645 pushtag contains special code to call pushdecl_with_scope on the
646 TEMPLATE_DECL for S2. */
647 begin_scope (sk_template_parms, NULL);
648 ++processing_template_decl;
649 ++processing_template_parmlist;
650 note_template_header (0);
651 }
652
653 /* This routine is called when a specialization is declared. If it is
654 invalid to declare a specialization here, an error is reported and
655 false is returned, otherwise this routine will return true. */
656
657 static bool
658 check_specialization_scope (void)
659 {
660 tree scope = current_scope ();
661
662 /* [temp.expl.spec]
663
664 An explicit specialization shall be declared in the namespace of
665 which the template is a member, or, for member templates, in the
666 namespace of which the enclosing class or enclosing class
667 template is a member. An explicit specialization of a member
668 function, member class or static data member of a class template
669 shall be declared in the namespace of which the class template
670 is a member. */
671 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
672 {
673 error ("explicit specialization in non-namespace scope %qD", scope);
674 return false;
675 }
676
677 /* [temp.expl.spec]
678
679 In an explicit specialization declaration for a member of a class
680 template or a member template that appears in namespace scope,
681 the member template and some of its enclosing class templates may
682 remain unspecialized, except that the declaration shall not
683 explicitly specialize a class member template if its enclosing
684 class templates are not explicitly specialized as well. */
685 if (current_template_parms)
686 {
687 error ("enclosing class templates are not explicitly specialized");
688 return false;
689 }
690
691 return true;
692 }
693
694 /* We've just seen template <>. */
695
696 bool
697 begin_specialization (void)
698 {
699 begin_scope (sk_template_spec, NULL);
700 note_template_header (1);
701 return check_specialization_scope ();
702 }
703
704 /* Called at then end of processing a declaration preceded by
705 template<>. */
706
707 void
708 end_specialization (void)
709 {
710 finish_scope ();
711 reset_specialization ();
712 }
713
714 /* Any template <>'s that we have seen thus far are not referring to a
715 function specialization. */
716
717 void
718 reset_specialization (void)
719 {
720 processing_specialization = 0;
721 template_header_count = 0;
722 }
723
724 /* We've just seen a template header. If SPECIALIZATION is nonzero,
725 it was of the form template <>. */
726
727 static void
728 note_template_header (int specialization)
729 {
730 processing_specialization = specialization;
731 template_header_count++;
732 }
733
734 /* We're beginning an explicit instantiation. */
735
736 void
737 begin_explicit_instantiation (void)
738 {
739 gcc_assert (!processing_explicit_instantiation);
740 processing_explicit_instantiation = true;
741 }
742
743
744 void
745 end_explicit_instantiation (void)
746 {
747 gcc_assert (processing_explicit_instantiation);
748 processing_explicit_instantiation = false;
749 }
750
751 /* An explicit specialization or partial specialization of TMPL is being
752 declared. Check that the namespace in which the specialization is
753 occurring is permissible. Returns false iff it is invalid to
754 specialize TMPL in the current namespace. */
755
756 static bool
757 check_specialization_namespace (tree tmpl)
758 {
759 tree tpl_ns = decl_namespace_context (tmpl);
760
761 /* [tmpl.expl.spec]
762
763 An explicit specialization shall be declared in the namespace of
764 which the template is a member, or, for member templates, in the
765 namespace of which the enclosing class or enclosing class
766 template is a member. An explicit specialization of a member
767 function, member class or static data member of a class template
768 shall be declared in the namespace of which the class template is
769 a member. */
770 if (current_scope() != DECL_CONTEXT (tmpl)
771 && !at_namespace_scope_p ())
772 {
773 error ("specialization of %qD must appear at namespace scope", tmpl);
774 return false;
775 }
776 if (is_associated_namespace (current_namespace, tpl_ns))
777 /* Same or super-using namespace. */
778 return true;
779 else
780 {
781 permerror (input_location, "specialization of %qD in different namespace", tmpl);
782 permerror (input_location, " from definition of %q+#D", tmpl);
783 return false;
784 }
785 }
786
787 /* SPEC is an explicit instantiation. Check that it is valid to
788 perform this explicit instantiation in the current namespace. */
789
790 static void
791 check_explicit_instantiation_namespace (tree spec)
792 {
793 tree ns;
794
795 /* DR 275: An explicit instantiation shall appear in an enclosing
796 namespace of its template. */
797 ns = decl_namespace_context (spec);
798 if (!is_ancestor (current_namespace, ns))
799 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
800 "(which does not enclose namespace %qD)",
801 spec, current_namespace, ns);
802 }
803
804 /* The TYPE is being declared. If it is a template type, that means it
805 is a partial specialization. Do appropriate error-checking. */
806
807 tree
808 maybe_process_partial_specialization (tree type)
809 {
810 tree context;
811
812 if (type == error_mark_node)
813 return error_mark_node;
814
815 /* A lambda that appears in specialization context is not itself a
816 specialization. */
817 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
818 return type;
819
820 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
821 {
822 error ("name of class shadows template template parameter %qD",
823 TYPE_NAME (type));
824 return error_mark_node;
825 }
826
827 context = TYPE_CONTEXT (type);
828
829 if (TYPE_ALIAS_P (type))
830 {
831 if (TYPE_TEMPLATE_INFO (type)
832 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
833 error ("specialization of alias template %qD",
834 TYPE_TI_TEMPLATE (type));
835 else
836 error ("explicit specialization of non-template %qT", type);
837 return error_mark_node;
838 }
839 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
840 {
841 /* This is for ordinary explicit specialization and partial
842 specialization of a template class such as:
843
844 template <> class C<int>;
845
846 or:
847
848 template <class T> class C<T*>;
849
850 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
851
852 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
853 && !COMPLETE_TYPE_P (type))
854 {
855 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
856 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
857 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
858 if (processing_template_decl)
859 {
860 if (push_template_decl (TYPE_MAIN_DECL (type))
861 == error_mark_node)
862 return error_mark_node;
863 }
864 }
865 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
866 error ("specialization of %qT after instantiation", type);
867 else if (errorcount && !processing_specialization
868 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
869 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
870 /* Trying to define a specialization either without a template<> header
871 or in an inappropriate place. We've already given an error, so just
872 bail now so we don't actually define the specialization. */
873 return error_mark_node;
874 }
875 else if (CLASS_TYPE_P (type)
876 && !CLASSTYPE_USE_TEMPLATE (type)
877 && CLASSTYPE_TEMPLATE_INFO (type)
878 && context && CLASS_TYPE_P (context)
879 && CLASSTYPE_TEMPLATE_INFO (context))
880 {
881 /* This is for an explicit specialization of member class
882 template according to [temp.expl.spec/18]:
883
884 template <> template <class U> class C<int>::D;
885
886 The context `C<int>' must be an implicit instantiation.
887 Otherwise this is just a member class template declared
888 earlier like:
889
890 template <> class C<int> { template <class U> class D; };
891 template <> template <class U> class C<int>::D;
892
893 In the first case, `C<int>::D' is a specialization of `C<T>::D'
894 while in the second case, `C<int>::D' is a primary template
895 and `C<T>::D' may not exist. */
896
897 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
898 && !COMPLETE_TYPE_P (type))
899 {
900 tree t;
901 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
902
903 if (current_namespace
904 != decl_namespace_context (tmpl))
905 {
906 permerror (input_location, "specializing %q#T in different namespace", type);
907 permerror (input_location, " from definition of %q+#D", tmpl);
908 }
909
910 /* Check for invalid specialization after instantiation:
911
912 template <> template <> class C<int>::D<int>;
913 template <> template <class U> class C<int>::D; */
914
915 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
916 t; t = TREE_CHAIN (t))
917 {
918 tree inst = TREE_VALUE (t);
919 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
920 {
921 /* We already have a full specialization of this partial
922 instantiation. Reassign it to the new member
923 specialization template. */
924 spec_entry elt;
925 spec_entry *entry;
926 void **slot;
927
928 elt.tmpl = most_general_template (tmpl);
929 elt.args = CLASSTYPE_TI_ARGS (inst);
930 elt.spec = inst;
931
932 htab_remove_elt (type_specializations, &elt);
933
934 elt.tmpl = tmpl;
935 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
936
937 slot = htab_find_slot (type_specializations, &elt, INSERT);
938 entry = ggc_alloc_spec_entry ();
939 *entry = elt;
940 *slot = entry;
941 }
942 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
943 /* But if we've had an implicit instantiation, that's a
944 problem ([temp.expl.spec]/6). */
945 error ("specialization %qT after instantiation %qT",
946 type, inst);
947 }
948
949 /* Mark TYPE as a specialization. And as a result, we only
950 have one level of template argument for the innermost
951 class template. */
952 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
953 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
954 CLASSTYPE_TI_ARGS (type)
955 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
956 }
957 }
958 else if (processing_specialization)
959 {
960 /* Someday C++0x may allow for enum template specialization. */
961 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
962 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
963 pedwarn (input_location, OPT_Wpedantic, "template specialization "
964 "of %qD not allowed by ISO C++", type);
965 else
966 {
967 error ("explicit specialization of non-template %qT", type);
968 return error_mark_node;
969 }
970 }
971
972 return type;
973 }
974
975 /* Returns nonzero if we can optimize the retrieval of specializations
976 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
977 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
978
979 static inline bool
980 optimize_specialization_lookup_p (tree tmpl)
981 {
982 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
983 && DECL_CLASS_SCOPE_P (tmpl)
984 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
985 parameter. */
986 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
987 /* The optimized lookup depends on the fact that the
988 template arguments for the member function template apply
989 purely to the containing class, which is not true if the
990 containing class is an explicit or partial
991 specialization. */
992 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
993 && !DECL_MEMBER_TEMPLATE_P (tmpl)
994 && !DECL_CONV_FN_P (tmpl)
995 /* It is possible to have a template that is not a member
996 template and is not a member of a template class:
997
998 template <typename T>
999 struct S { friend A::f(); };
1000
1001 Here, the friend function is a template, but the context does
1002 not have template information. The optimized lookup relies
1003 on having ARGS be the template arguments for both the class
1004 and the function template. */
1005 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1006 }
1007
1008 /* Retrieve the specialization (in the sense of [temp.spec] - a
1009 specialization is either an instantiation or an explicit
1010 specialization) of TMPL for the given template ARGS. If there is
1011 no such specialization, return NULL_TREE. The ARGS are a vector of
1012 arguments, or a vector of vectors of arguments, in the case of
1013 templates with more than one level of parameters.
1014
1015 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1016 then we search for a partial specialization matching ARGS. This
1017 parameter is ignored if TMPL is not a class template.
1018
1019 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1020 result is a NONTYPE_ARGUMENT_PACK. */
1021
1022 static tree
1023 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1024 {
1025 if (tmpl == NULL_TREE)
1026 return NULL_TREE;
1027
1028 if (args == error_mark_node)
1029 return NULL_TREE;
1030
1031 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1032 || TREE_CODE (tmpl) == FIELD_DECL);
1033
1034 /* There should be as many levels of arguments as there are
1035 levels of parameters. */
1036 gcc_assert (TMPL_ARGS_DEPTH (args)
1037 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1038 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1039 : template_class_depth (DECL_CONTEXT (tmpl))));
1040
1041 if (optimize_specialization_lookup_p (tmpl))
1042 {
1043 tree class_template;
1044 tree class_specialization;
1045 vec<tree, va_gc> *methods;
1046 tree fns;
1047 int idx;
1048
1049 /* The template arguments actually apply to the containing
1050 class. Find the class specialization with those
1051 arguments. */
1052 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1053 class_specialization
1054 = retrieve_specialization (class_template, args, 0);
1055 if (!class_specialization)
1056 return NULL_TREE;
1057 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1058 for the specialization. */
1059 idx = class_method_index_for_fn (class_specialization, tmpl);
1060 if (idx == -1)
1061 return NULL_TREE;
1062 /* Iterate through the methods with the indicated name, looking
1063 for the one that has an instance of TMPL. */
1064 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1065 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1066 {
1067 tree fn = OVL_CURRENT (fns);
1068 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1069 /* using-declarations can add base methods to the method vec,
1070 and we don't want those here. */
1071 && DECL_CONTEXT (fn) == class_specialization)
1072 return fn;
1073 }
1074 return NULL_TREE;
1075 }
1076 else
1077 {
1078 spec_entry *found;
1079 spec_entry elt;
1080 htab_t specializations;
1081
1082 elt.tmpl = tmpl;
1083 elt.args = args;
1084 elt.spec = NULL_TREE;
1085
1086 if (DECL_CLASS_TEMPLATE_P (tmpl))
1087 specializations = type_specializations;
1088 else
1089 specializations = decl_specializations;
1090
1091 if (hash == 0)
1092 hash = hash_specialization (&elt);
1093 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1094 if (found)
1095 return found->spec;
1096 }
1097
1098 return NULL_TREE;
1099 }
1100
1101 /* Like retrieve_specialization, but for local declarations. */
1102
1103 static tree
1104 retrieve_local_specialization (tree tmpl)
1105 {
1106 void **slot;
1107
1108 if (local_specializations == NULL)
1109 return NULL_TREE;
1110
1111 slot = pointer_map_contains (local_specializations, tmpl);
1112 return slot ? (tree) *slot : NULL_TREE;
1113 }
1114
1115 /* Returns nonzero iff DECL is a specialization of TMPL. */
1116
1117 int
1118 is_specialization_of (tree decl, tree tmpl)
1119 {
1120 tree t;
1121
1122 if (TREE_CODE (decl) == FUNCTION_DECL)
1123 {
1124 for (t = decl;
1125 t != NULL_TREE;
1126 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1127 if (t == tmpl)
1128 return 1;
1129 }
1130 else
1131 {
1132 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1133
1134 for (t = TREE_TYPE (decl);
1135 t != NULL_TREE;
1136 t = CLASSTYPE_USE_TEMPLATE (t)
1137 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1138 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1139 return 1;
1140 }
1141
1142 return 0;
1143 }
1144
1145 /* Returns nonzero iff DECL is a specialization of friend declaration
1146 FRIEND_DECL according to [temp.friend]. */
1147
1148 bool
1149 is_specialization_of_friend (tree decl, tree friend_decl)
1150 {
1151 bool need_template = true;
1152 int template_depth;
1153
1154 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1155 || TREE_CODE (decl) == TYPE_DECL);
1156
1157 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1158 of a template class, we want to check if DECL is a specialization
1159 if this. */
1160 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1161 && DECL_TEMPLATE_INFO (friend_decl)
1162 && !DECL_USE_TEMPLATE (friend_decl))
1163 {
1164 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1165 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1166 need_template = false;
1167 }
1168 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1169 && !PRIMARY_TEMPLATE_P (friend_decl))
1170 need_template = false;
1171
1172 /* There is nothing to do if this is not a template friend. */
1173 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1174 return false;
1175
1176 if (is_specialization_of (decl, friend_decl))
1177 return true;
1178
1179 /* [temp.friend/6]
1180 A member of a class template may be declared to be a friend of a
1181 non-template class. In this case, the corresponding member of
1182 every specialization of the class template is a friend of the
1183 class granting friendship.
1184
1185 For example, given a template friend declaration
1186
1187 template <class T> friend void A<T>::f();
1188
1189 the member function below is considered a friend
1190
1191 template <> struct A<int> {
1192 void f();
1193 };
1194
1195 For this type of template friend, TEMPLATE_DEPTH below will be
1196 nonzero. To determine if DECL is a friend of FRIEND, we first
1197 check if the enclosing class is a specialization of another. */
1198
1199 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1200 if (template_depth
1201 && DECL_CLASS_SCOPE_P (decl)
1202 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1203 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1204 {
1205 /* Next, we check the members themselves. In order to handle
1206 a few tricky cases, such as when FRIEND_DECL's are
1207
1208 template <class T> friend void A<T>::g(T t);
1209 template <class T> template <T t> friend void A<T>::h();
1210
1211 and DECL's are
1212
1213 void A<int>::g(int);
1214 template <int> void A<int>::h();
1215
1216 we need to figure out ARGS, the template arguments from
1217 the context of DECL. This is required for template substitution
1218 of `T' in the function parameter of `g' and template parameter
1219 of `h' in the above examples. Here ARGS corresponds to `int'. */
1220
1221 tree context = DECL_CONTEXT (decl);
1222 tree args = NULL_TREE;
1223 int current_depth = 0;
1224
1225 while (current_depth < template_depth)
1226 {
1227 if (CLASSTYPE_TEMPLATE_INFO (context))
1228 {
1229 if (current_depth == 0)
1230 args = TYPE_TI_ARGS (context);
1231 else
1232 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1233 current_depth++;
1234 }
1235 context = TYPE_CONTEXT (context);
1236 }
1237
1238 if (TREE_CODE (decl) == FUNCTION_DECL)
1239 {
1240 bool is_template;
1241 tree friend_type;
1242 tree decl_type;
1243 tree friend_args_type;
1244 tree decl_args_type;
1245
1246 /* Make sure that both DECL and FRIEND_DECL are templates or
1247 non-templates. */
1248 is_template = DECL_TEMPLATE_INFO (decl)
1249 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1250 if (need_template ^ is_template)
1251 return false;
1252 else if (is_template)
1253 {
1254 /* If both are templates, check template parameter list. */
1255 tree friend_parms
1256 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1257 args, tf_none);
1258 if (!comp_template_parms
1259 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1260 friend_parms))
1261 return false;
1262
1263 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1264 }
1265 else
1266 decl_type = TREE_TYPE (decl);
1267
1268 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1269 tf_none, NULL_TREE);
1270 if (friend_type == error_mark_node)
1271 return false;
1272
1273 /* Check if return types match. */
1274 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1275 return false;
1276
1277 /* Check if function parameter types match, ignoring the
1278 `this' parameter. */
1279 friend_args_type = TYPE_ARG_TYPES (friend_type);
1280 decl_args_type = TYPE_ARG_TYPES (decl_type);
1281 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1282 friend_args_type = TREE_CHAIN (friend_args_type);
1283 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1284 decl_args_type = TREE_CHAIN (decl_args_type);
1285
1286 return compparms (decl_args_type, friend_args_type);
1287 }
1288 else
1289 {
1290 /* DECL is a TYPE_DECL */
1291 bool is_template;
1292 tree decl_type = TREE_TYPE (decl);
1293
1294 /* Make sure that both DECL and FRIEND_DECL are templates or
1295 non-templates. */
1296 is_template
1297 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1298 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1299
1300 if (need_template ^ is_template)
1301 return false;
1302 else if (is_template)
1303 {
1304 tree friend_parms;
1305 /* If both are templates, check the name of the two
1306 TEMPLATE_DECL's first because is_friend didn't. */
1307 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1308 != DECL_NAME (friend_decl))
1309 return false;
1310
1311 /* Now check template parameter list. */
1312 friend_parms
1313 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1314 args, tf_none);
1315 return comp_template_parms
1316 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1317 friend_parms);
1318 }
1319 else
1320 return (DECL_NAME (decl)
1321 == DECL_NAME (friend_decl));
1322 }
1323 }
1324 return false;
1325 }
1326
1327 /* Register the specialization SPEC as a specialization of TMPL with
1328 the indicated ARGS. IS_FRIEND indicates whether the specialization
1329 is actually just a friend declaration. Returns SPEC, or an
1330 equivalent prior declaration, if available.
1331
1332 We also store instantiations of field packs in the hash table, even
1333 though they are not themselves templates, to make lookup easier. */
1334
1335 static tree
1336 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1337 hashval_t hash)
1338 {
1339 tree fn;
1340 void **slot = NULL;
1341 spec_entry elt;
1342
1343 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1344 || (TREE_CODE (tmpl) == FIELD_DECL
1345 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1346
1347 if (TREE_CODE (spec) == FUNCTION_DECL
1348 && uses_template_parms (DECL_TI_ARGS (spec)))
1349 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1350 register it; we want the corresponding TEMPLATE_DECL instead.
1351 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1352 the more obvious `uses_template_parms (spec)' to avoid problems
1353 with default function arguments. In particular, given
1354 something like this:
1355
1356 template <class T> void f(T t1, T t = T())
1357
1358 the default argument expression is not substituted for in an
1359 instantiation unless and until it is actually needed. */
1360 return spec;
1361
1362 if (optimize_specialization_lookup_p (tmpl))
1363 /* We don't put these specializations in the hash table, but we might
1364 want to give an error about a mismatch. */
1365 fn = retrieve_specialization (tmpl, args, 0);
1366 else
1367 {
1368 elt.tmpl = tmpl;
1369 elt.args = args;
1370 elt.spec = spec;
1371
1372 if (hash == 0)
1373 hash = hash_specialization (&elt);
1374
1375 slot =
1376 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1377 if (*slot)
1378 fn = ((spec_entry *) *slot)->spec;
1379 else
1380 fn = NULL_TREE;
1381 }
1382
1383 /* We can sometimes try to re-register a specialization that we've
1384 already got. In particular, regenerate_decl_from_template calls
1385 duplicate_decls which will update the specialization list. But,
1386 we'll still get called again here anyhow. It's more convenient
1387 to simply allow this than to try to prevent it. */
1388 if (fn == spec)
1389 return spec;
1390 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1391 {
1392 if (DECL_TEMPLATE_INSTANTIATION (fn))
1393 {
1394 if (DECL_ODR_USED (fn)
1395 || DECL_EXPLICIT_INSTANTIATION (fn))
1396 {
1397 error ("specialization of %qD after instantiation",
1398 fn);
1399 return error_mark_node;
1400 }
1401 else
1402 {
1403 tree clone;
1404 /* This situation should occur only if the first
1405 specialization is an implicit instantiation, the
1406 second is an explicit specialization, and the
1407 implicit instantiation has not yet been used. That
1408 situation can occur if we have implicitly
1409 instantiated a member function and then specialized
1410 it later.
1411
1412 We can also wind up here if a friend declaration that
1413 looked like an instantiation turns out to be a
1414 specialization:
1415
1416 template <class T> void foo(T);
1417 class S { friend void foo<>(int) };
1418 template <> void foo(int);
1419
1420 We transform the existing DECL in place so that any
1421 pointers to it become pointers to the updated
1422 declaration.
1423
1424 If there was a definition for the template, but not
1425 for the specialization, we want this to look as if
1426 there were no definition, and vice versa. */
1427 DECL_INITIAL (fn) = NULL_TREE;
1428 duplicate_decls (spec, fn, is_friend);
1429 /* The call to duplicate_decls will have applied
1430 [temp.expl.spec]:
1431
1432 An explicit specialization of a function template
1433 is inline only if it is explicitly declared to be,
1434 and independently of whether its function template
1435 is.
1436
1437 to the primary function; now copy the inline bits to
1438 the various clones. */
1439 FOR_EACH_CLONE (clone, fn)
1440 {
1441 DECL_DECLARED_INLINE_P (clone)
1442 = DECL_DECLARED_INLINE_P (fn);
1443 DECL_SOURCE_LOCATION (clone)
1444 = DECL_SOURCE_LOCATION (fn);
1445 }
1446 check_specialization_namespace (tmpl);
1447
1448 return fn;
1449 }
1450 }
1451 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1452 {
1453 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1454 /* Dup decl failed, but this is a new definition. Set the
1455 line number so any errors match this new
1456 definition. */
1457 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1458
1459 return fn;
1460 }
1461 }
1462 else if (fn)
1463 return duplicate_decls (spec, fn, is_friend);
1464
1465 /* A specialization must be declared in the same namespace as the
1466 template it is specializing. */
1467 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1468 && !check_specialization_namespace (tmpl))
1469 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1470
1471 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1472 {
1473 spec_entry *entry = ggc_alloc_spec_entry ();
1474 gcc_assert (tmpl && args && spec);
1475 *entry = elt;
1476 *slot = entry;
1477 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1478 && PRIMARY_TEMPLATE_P (tmpl)
1479 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1480 /* TMPL is a forward declaration of a template function; keep a list
1481 of all specializations in case we need to reassign them to a friend
1482 template later in tsubst_friend_function. */
1483 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1484 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1485 }
1486
1487 return spec;
1488 }
1489
1490 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1491 TMPL and ARGS members, ignores SPEC. */
1492
1493 int comparing_specializations;
1494
1495 static int
1496 eq_specializations (const void *p1, const void *p2)
1497 {
1498 const spec_entry *e1 = (const spec_entry *)p1;
1499 const spec_entry *e2 = (const spec_entry *)p2;
1500 int equal;
1501
1502 ++comparing_specializations;
1503 equal = (e1->tmpl == e2->tmpl
1504 && comp_template_args (e1->args, e2->args));
1505 --comparing_specializations;
1506
1507 return equal;
1508 }
1509
1510 /* Returns a hash for a template TMPL and template arguments ARGS. */
1511
1512 static hashval_t
1513 hash_tmpl_and_args (tree tmpl, tree args)
1514 {
1515 hashval_t val = DECL_UID (tmpl);
1516 return iterative_hash_template_arg (args, val);
1517 }
1518
1519 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1520 ignoring SPEC. */
1521
1522 static hashval_t
1523 hash_specialization (const void *p)
1524 {
1525 const spec_entry *e = (const spec_entry *)p;
1526 return hash_tmpl_and_args (e->tmpl, e->args);
1527 }
1528
1529 /* Recursively calculate a hash value for a template argument ARG, for use
1530 in the hash tables of template specializations. */
1531
1532 hashval_t
1533 iterative_hash_template_arg (tree arg, hashval_t val)
1534 {
1535 unsigned HOST_WIDE_INT i;
1536 enum tree_code code;
1537 char tclass;
1538
1539 if (arg == NULL_TREE)
1540 return iterative_hash_object (arg, val);
1541
1542 if (!TYPE_P (arg))
1543 STRIP_NOPS (arg);
1544
1545 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1546 /* We can get one of these when re-hashing a previous entry in the middle
1547 of substituting into a pack expansion. Just look through it. */
1548 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1549
1550 code = TREE_CODE (arg);
1551 tclass = TREE_CODE_CLASS (code);
1552
1553 val = iterative_hash_object (code, val);
1554
1555 switch (code)
1556 {
1557 case ERROR_MARK:
1558 return val;
1559
1560 case IDENTIFIER_NODE:
1561 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1562
1563 case TREE_VEC:
1564 {
1565 int i, len = TREE_VEC_LENGTH (arg);
1566 for (i = 0; i < len; ++i)
1567 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1568 return val;
1569 }
1570
1571 case TYPE_PACK_EXPANSION:
1572 case EXPR_PACK_EXPANSION:
1573 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1574 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1575
1576 case TYPE_ARGUMENT_PACK:
1577 case NONTYPE_ARGUMENT_PACK:
1578 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1579
1580 case TREE_LIST:
1581 for (; arg; arg = TREE_CHAIN (arg))
1582 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1583 return val;
1584
1585 case OVERLOAD:
1586 for (; arg; arg = OVL_NEXT (arg))
1587 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1588 return val;
1589
1590 case CONSTRUCTOR:
1591 {
1592 tree field, value;
1593 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1594 {
1595 val = iterative_hash_template_arg (field, val);
1596 val = iterative_hash_template_arg (value, val);
1597 }
1598 return val;
1599 }
1600
1601 case PARM_DECL:
1602 if (!DECL_ARTIFICIAL (arg))
1603 {
1604 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1605 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1606 }
1607 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1608
1609 case TARGET_EXPR:
1610 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1611
1612 case PTRMEM_CST:
1613 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1614 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1615
1616 case TEMPLATE_PARM_INDEX:
1617 val = iterative_hash_template_arg
1618 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1619 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1620 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1621
1622 case TRAIT_EXPR:
1623 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1624 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1625 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1626
1627 case BASELINK:
1628 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1629 val);
1630 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1631 val);
1632
1633 case MODOP_EXPR:
1634 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1635 code = TREE_CODE (TREE_OPERAND (arg, 1));
1636 val = iterative_hash_object (code, val);
1637 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1638
1639 case LAMBDA_EXPR:
1640 /* A lambda can't appear in a template arg, but don't crash on
1641 erroneous input. */
1642 gcc_assert (seen_error ());
1643 return val;
1644
1645 case CAST_EXPR:
1646 case IMPLICIT_CONV_EXPR:
1647 case STATIC_CAST_EXPR:
1648 case REINTERPRET_CAST_EXPR:
1649 case CONST_CAST_EXPR:
1650 case DYNAMIC_CAST_EXPR:
1651 case NEW_EXPR:
1652 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1653 /* Now hash operands as usual. */
1654 break;
1655
1656 default:
1657 break;
1658 }
1659
1660 switch (tclass)
1661 {
1662 case tcc_type:
1663 if (TYPE_CANONICAL (arg))
1664 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1665 val);
1666 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1667 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1668 /* Otherwise just compare the types during lookup. */
1669 return val;
1670
1671 case tcc_declaration:
1672 case tcc_constant:
1673 return iterative_hash_expr (arg, val);
1674
1675 default:
1676 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1677 {
1678 unsigned n = cp_tree_operand_length (arg);
1679 for (i = 0; i < n; ++i)
1680 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1681 return val;
1682 }
1683 }
1684 gcc_unreachable ();
1685 return 0;
1686 }
1687
1688 /* Unregister the specialization SPEC as a specialization of TMPL.
1689 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1690 if the SPEC was listed as a specialization of TMPL.
1691
1692 Note that SPEC has been ggc_freed, so we can't look inside it. */
1693
1694 bool
1695 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1696 {
1697 spec_entry *entry;
1698 spec_entry elt;
1699
1700 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1701 elt.args = TI_ARGS (tinfo);
1702 elt.spec = NULL_TREE;
1703
1704 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1705 if (entry != NULL)
1706 {
1707 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1708 gcc_assert (new_spec != NULL_TREE);
1709 entry->spec = new_spec;
1710 return 1;
1711 }
1712
1713 return 0;
1714 }
1715
1716 /* Like register_specialization, but for local declarations. We are
1717 registering SPEC, an instantiation of TMPL. */
1718
1719 static void
1720 register_local_specialization (tree spec, tree tmpl)
1721 {
1722 void **slot;
1723
1724 slot = pointer_map_insert (local_specializations, tmpl);
1725 *slot = spec;
1726 }
1727
1728 /* TYPE is a class type. Returns true if TYPE is an explicitly
1729 specialized class. */
1730
1731 bool
1732 explicit_class_specialization_p (tree type)
1733 {
1734 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1735 return false;
1736 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1737 }
1738
1739 /* Print the list of functions at FNS, going through all the overloads
1740 for each element of the list. Alternatively, FNS can not be a
1741 TREE_LIST, in which case it will be printed together with all the
1742 overloads.
1743
1744 MORE and *STR should respectively be FALSE and NULL when the function
1745 is called from the outside. They are used internally on recursive
1746 calls. print_candidates manages the two parameters and leaves NULL
1747 in *STR when it ends. */
1748
1749 static void
1750 print_candidates_1 (tree fns, bool more, const char **str)
1751 {
1752 tree fn, fn2;
1753 char *spaces = NULL;
1754
1755 for (fn = fns; fn; fn = OVL_NEXT (fn))
1756 if (TREE_CODE (fn) == TREE_LIST)
1757 {
1758 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1759 print_candidates_1 (TREE_VALUE (fn2),
1760 TREE_CHAIN (fn2) || more, str);
1761 }
1762 else
1763 {
1764 tree cand = OVL_CURRENT (fn);
1765 if (!*str)
1766 {
1767 /* Pick the prefix string. */
1768 if (!more && !OVL_NEXT (fns))
1769 {
1770 inform (DECL_SOURCE_LOCATION (cand),
1771 "candidate is: %#D", cand);
1772 continue;
1773 }
1774
1775 *str = _("candidates are:");
1776 spaces = get_spaces (*str);
1777 }
1778 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1779 *str = spaces ? spaces : *str;
1780 }
1781
1782 if (!more)
1783 {
1784 free (spaces);
1785 *str = NULL;
1786 }
1787 }
1788
1789 /* Print the list of candidate FNS in an error message. FNS can also
1790 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1791
1792 void
1793 print_candidates (tree fns)
1794 {
1795 const char *str = NULL;
1796 print_candidates_1 (fns, false, &str);
1797 gcc_assert (str == NULL);
1798 }
1799
1800 /* Returns the template (one of the functions given by TEMPLATE_ID)
1801 which can be specialized to match the indicated DECL with the
1802 explicit template args given in TEMPLATE_ID. The DECL may be
1803 NULL_TREE if none is available. In that case, the functions in
1804 TEMPLATE_ID are non-members.
1805
1806 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1807 specialization of a member template.
1808
1809 The TEMPLATE_COUNT is the number of references to qualifying
1810 template classes that appeared in the name of the function. See
1811 check_explicit_specialization for a more accurate description.
1812
1813 TSK indicates what kind of template declaration (if any) is being
1814 declared. TSK_TEMPLATE indicates that the declaration given by
1815 DECL, though a FUNCTION_DECL, has template parameters, and is
1816 therefore a template function.
1817
1818 The template args (those explicitly specified and those deduced)
1819 are output in a newly created vector *TARGS_OUT.
1820
1821 If it is impossible to determine the result, an error message is
1822 issued. The error_mark_node is returned to indicate failure. */
1823
1824 static tree
1825 determine_specialization (tree template_id,
1826 tree decl,
1827 tree* targs_out,
1828 int need_member_template,
1829 int template_count,
1830 tmpl_spec_kind tsk)
1831 {
1832 tree fns;
1833 tree targs;
1834 tree explicit_targs;
1835 tree candidates = NULL_TREE;
1836 /* A TREE_LIST of templates of which DECL may be a specialization.
1837 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1838 corresponding TREE_PURPOSE is the set of template arguments that,
1839 when used to instantiate the template, would produce a function
1840 with the signature of DECL. */
1841 tree templates = NULL_TREE;
1842 int header_count;
1843 cp_binding_level *b;
1844
1845 *targs_out = NULL_TREE;
1846
1847 if (template_id == error_mark_node || decl == error_mark_node)
1848 return error_mark_node;
1849
1850 /* We shouldn't be specializing a member template of an
1851 unspecialized class template; we already gave an error in
1852 check_specialization_scope, now avoid crashing. */
1853 if (template_count && DECL_CLASS_SCOPE_P (decl)
1854 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1855 {
1856 gcc_assert (errorcount);
1857 return error_mark_node;
1858 }
1859
1860 fns = TREE_OPERAND (template_id, 0);
1861 explicit_targs = TREE_OPERAND (template_id, 1);
1862
1863 if (fns == error_mark_node)
1864 return error_mark_node;
1865
1866 /* Check for baselinks. */
1867 if (BASELINK_P (fns))
1868 fns = BASELINK_FUNCTIONS (fns);
1869
1870 if (!is_overloaded_fn (fns))
1871 {
1872 error ("%qD is not a function template", fns);
1873 return error_mark_node;
1874 }
1875
1876 /* Count the number of template headers specified for this
1877 specialization. */
1878 header_count = 0;
1879 for (b = current_binding_level;
1880 b->kind == sk_template_parms;
1881 b = b->level_chain)
1882 ++header_count;
1883
1884 for (; fns; fns = OVL_NEXT (fns))
1885 {
1886 tree fn = OVL_CURRENT (fns);
1887
1888 if (TREE_CODE (fn) == TEMPLATE_DECL)
1889 {
1890 tree decl_arg_types;
1891 tree fn_arg_types;
1892 tree insttype;
1893
1894 /* In case of explicit specialization, we need to check if
1895 the number of template headers appearing in the specialization
1896 is correct. This is usually done in check_explicit_specialization,
1897 but the check done there cannot be exhaustive when specializing
1898 member functions. Consider the following code:
1899
1900 template <> void A<int>::f(int);
1901 template <> template <> void A<int>::f(int);
1902
1903 Assuming that A<int> is not itself an explicit specialization
1904 already, the first line specializes "f" which is a non-template
1905 member function, whilst the second line specializes "f" which
1906 is a template member function. So both lines are syntactically
1907 correct, and check_explicit_specialization does not reject
1908 them.
1909
1910 Here, we can do better, as we are matching the specialization
1911 against the declarations. We count the number of template
1912 headers, and we check if they match TEMPLATE_COUNT + 1
1913 (TEMPLATE_COUNT is the number of qualifying template classes,
1914 plus there must be another header for the member template
1915 itself).
1916
1917 Notice that if header_count is zero, this is not a
1918 specialization but rather a template instantiation, so there
1919 is no check we can perform here. */
1920 if (header_count && header_count != template_count + 1)
1921 continue;
1922
1923 /* Check that the number of template arguments at the
1924 innermost level for DECL is the same as for FN. */
1925 if (current_binding_level->kind == sk_template_parms
1926 && !current_binding_level->explicit_spec_p
1927 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1928 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1929 (current_template_parms))))
1930 continue;
1931
1932 /* DECL might be a specialization of FN. */
1933 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1934 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1935
1936 /* For a non-static member function, we need to make sure
1937 that the const qualification is the same. Since
1938 get_bindings does not try to merge the "this" parameter,
1939 we must do the comparison explicitly. */
1940 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1941 && !same_type_p (TREE_VALUE (fn_arg_types),
1942 TREE_VALUE (decl_arg_types)))
1943 continue;
1944
1945 /* Skip the "this" parameter and, for constructors of
1946 classes with virtual bases, the VTT parameter. A
1947 full specialization of a constructor will have a VTT
1948 parameter, but a template never will. */
1949 decl_arg_types
1950 = skip_artificial_parms_for (decl, decl_arg_types);
1951 fn_arg_types
1952 = skip_artificial_parms_for (fn, fn_arg_types);
1953
1954 /* Function templates cannot be specializations; there are
1955 no partial specializations of functions. Therefore, if
1956 the type of DECL does not match FN, there is no
1957 match. */
1958 if (tsk == tsk_template)
1959 {
1960 if (compparms (fn_arg_types, decl_arg_types))
1961 candidates = tree_cons (NULL_TREE, fn, candidates);
1962 continue;
1963 }
1964
1965 /* See whether this function might be a specialization of this
1966 template. Suppress access control because we might be trying
1967 to make this specialization a friend, and we have already done
1968 access control for the declaration of the specialization. */
1969 push_deferring_access_checks (dk_no_check);
1970 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1971 pop_deferring_access_checks ();
1972
1973 if (!targs)
1974 /* We cannot deduce template arguments that when used to
1975 specialize TMPL will produce DECL. */
1976 continue;
1977
1978 /* Make sure that the deduced arguments actually work. */
1979 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1980 if (insttype == error_mark_node)
1981 continue;
1982 fn_arg_types
1983 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1984 if (!compparms (fn_arg_types, decl_arg_types))
1985 continue;
1986
1987 /* Save this template, and the arguments deduced. */
1988 templates = tree_cons (targs, fn, templates);
1989 }
1990 else if (need_member_template)
1991 /* FN is an ordinary member function, and we need a
1992 specialization of a member template. */
1993 ;
1994 else if (TREE_CODE (fn) != FUNCTION_DECL)
1995 /* We can get IDENTIFIER_NODEs here in certain erroneous
1996 cases. */
1997 ;
1998 else if (!DECL_FUNCTION_MEMBER_P (fn))
1999 /* This is just an ordinary non-member function. Nothing can
2000 be a specialization of that. */
2001 ;
2002 else if (DECL_ARTIFICIAL (fn))
2003 /* Cannot specialize functions that are created implicitly. */
2004 ;
2005 else
2006 {
2007 tree decl_arg_types;
2008
2009 /* This is an ordinary member function. However, since
2010 we're here, we can assume its enclosing class is a
2011 template class. For example,
2012
2013 template <typename T> struct S { void f(); };
2014 template <> void S<int>::f() {}
2015
2016 Here, S<int>::f is a non-template, but S<int> is a
2017 template class. If FN has the same type as DECL, we
2018 might be in business. */
2019
2020 if (!DECL_TEMPLATE_INFO (fn))
2021 /* Its enclosing class is an explicit specialization
2022 of a template class. This is not a candidate. */
2023 continue;
2024
2025 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2026 TREE_TYPE (TREE_TYPE (fn))))
2027 /* The return types differ. */
2028 continue;
2029
2030 /* Adjust the type of DECL in case FN is a static member. */
2031 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2032 if (DECL_STATIC_FUNCTION_P (fn)
2033 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2034 decl_arg_types = TREE_CHAIN (decl_arg_types);
2035
2036 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2037 decl_arg_types))
2038 /* They match! */
2039 candidates = tree_cons (NULL_TREE, fn, candidates);
2040 }
2041 }
2042
2043 if (templates && TREE_CHAIN (templates))
2044 {
2045 /* We have:
2046
2047 [temp.expl.spec]
2048
2049 It is possible for a specialization with a given function
2050 signature to be instantiated from more than one function
2051 template. In such cases, explicit specification of the
2052 template arguments must be used to uniquely identify the
2053 function template specialization being specialized.
2054
2055 Note that here, there's no suggestion that we're supposed to
2056 determine which of the candidate templates is most
2057 specialized. However, we, also have:
2058
2059 [temp.func.order]
2060
2061 Partial ordering of overloaded function template
2062 declarations is used in the following contexts to select
2063 the function template to which a function template
2064 specialization refers:
2065
2066 -- when an explicit specialization refers to a function
2067 template.
2068
2069 So, we do use the partial ordering rules, at least for now.
2070 This extension can only serve to make invalid programs valid,
2071 so it's safe. And, there is strong anecdotal evidence that
2072 the committee intended the partial ordering rules to apply;
2073 the EDG front end has that behavior, and John Spicer claims
2074 that the committee simply forgot to delete the wording in
2075 [temp.expl.spec]. */
2076 tree tmpl = most_specialized_instantiation (templates);
2077 if (tmpl != error_mark_node)
2078 {
2079 templates = tmpl;
2080 TREE_CHAIN (templates) = NULL_TREE;
2081 }
2082 }
2083
2084 if (templates == NULL_TREE && candidates == NULL_TREE)
2085 {
2086 error ("template-id %qD for %q+D does not match any template "
2087 "declaration", template_id, decl);
2088 if (header_count && header_count != template_count + 1)
2089 inform (input_location, "saw %d %<template<>%>, need %d for "
2090 "specializing a member function template",
2091 header_count, template_count + 1);
2092 return error_mark_node;
2093 }
2094 else if ((templates && TREE_CHAIN (templates))
2095 || (candidates && TREE_CHAIN (candidates))
2096 || (templates && candidates))
2097 {
2098 error ("ambiguous template specialization %qD for %q+D",
2099 template_id, decl);
2100 candidates = chainon (candidates, templates);
2101 print_candidates (candidates);
2102 return error_mark_node;
2103 }
2104
2105 /* We have one, and exactly one, match. */
2106 if (candidates)
2107 {
2108 tree fn = TREE_VALUE (candidates);
2109 *targs_out = copy_node (DECL_TI_ARGS (fn));
2110 /* DECL is a re-declaration or partial instantiation of a template
2111 function. */
2112 if (TREE_CODE (fn) == TEMPLATE_DECL)
2113 return fn;
2114 /* It was a specialization of an ordinary member function in a
2115 template class. */
2116 return DECL_TI_TEMPLATE (fn);
2117 }
2118
2119 /* It was a specialization of a template. */
2120 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2121 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2122 {
2123 *targs_out = copy_node (targs);
2124 SET_TMPL_ARGS_LEVEL (*targs_out,
2125 TMPL_ARGS_DEPTH (*targs_out),
2126 TREE_PURPOSE (templates));
2127 }
2128 else
2129 *targs_out = TREE_PURPOSE (templates);
2130 return TREE_VALUE (templates);
2131 }
2132
2133 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2134 but with the default argument values filled in from those in the
2135 TMPL_TYPES. */
2136
2137 static tree
2138 copy_default_args_to_explicit_spec_1 (tree spec_types,
2139 tree tmpl_types)
2140 {
2141 tree new_spec_types;
2142
2143 if (!spec_types)
2144 return NULL_TREE;
2145
2146 if (spec_types == void_list_node)
2147 return void_list_node;
2148
2149 /* Substitute into the rest of the list. */
2150 new_spec_types =
2151 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2152 TREE_CHAIN (tmpl_types));
2153
2154 /* Add the default argument for this parameter. */
2155 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2156 TREE_VALUE (spec_types),
2157 new_spec_types);
2158 }
2159
2160 /* DECL is an explicit specialization. Replicate default arguments
2161 from the template it specializes. (That way, code like:
2162
2163 template <class T> void f(T = 3);
2164 template <> void f(double);
2165 void g () { f (); }
2166
2167 works, as required.) An alternative approach would be to look up
2168 the correct default arguments at the call-site, but this approach
2169 is consistent with how implicit instantiations are handled. */
2170
2171 static void
2172 copy_default_args_to_explicit_spec (tree decl)
2173 {
2174 tree tmpl;
2175 tree spec_types;
2176 tree tmpl_types;
2177 tree new_spec_types;
2178 tree old_type;
2179 tree new_type;
2180 tree t;
2181 tree object_type = NULL_TREE;
2182 tree in_charge = NULL_TREE;
2183 tree vtt = NULL_TREE;
2184
2185 /* See if there's anything we need to do. */
2186 tmpl = DECL_TI_TEMPLATE (decl);
2187 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2188 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2189 if (TREE_PURPOSE (t))
2190 break;
2191 if (!t)
2192 return;
2193
2194 old_type = TREE_TYPE (decl);
2195 spec_types = TYPE_ARG_TYPES (old_type);
2196
2197 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2198 {
2199 /* Remove the this pointer, but remember the object's type for
2200 CV quals. */
2201 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2202 spec_types = TREE_CHAIN (spec_types);
2203 tmpl_types = TREE_CHAIN (tmpl_types);
2204
2205 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2206 {
2207 /* DECL may contain more parameters than TMPL due to the extra
2208 in-charge parameter in constructors and destructors. */
2209 in_charge = spec_types;
2210 spec_types = TREE_CHAIN (spec_types);
2211 }
2212 if (DECL_HAS_VTT_PARM_P (decl))
2213 {
2214 vtt = spec_types;
2215 spec_types = TREE_CHAIN (spec_types);
2216 }
2217 }
2218
2219 /* Compute the merged default arguments. */
2220 new_spec_types =
2221 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2222
2223 /* Compute the new FUNCTION_TYPE. */
2224 if (object_type)
2225 {
2226 if (vtt)
2227 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2228 TREE_VALUE (vtt),
2229 new_spec_types);
2230
2231 if (in_charge)
2232 /* Put the in-charge parameter back. */
2233 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2234 TREE_VALUE (in_charge),
2235 new_spec_types);
2236
2237 new_type = build_method_type_directly (object_type,
2238 TREE_TYPE (old_type),
2239 new_spec_types);
2240 }
2241 else
2242 new_type = build_function_type (TREE_TYPE (old_type),
2243 new_spec_types);
2244 new_type = cp_build_type_attribute_variant (new_type,
2245 TYPE_ATTRIBUTES (old_type));
2246 new_type = build_exception_variant (new_type,
2247 TYPE_RAISES_EXCEPTIONS (old_type));
2248 TREE_TYPE (decl) = new_type;
2249 }
2250
2251 /* Return the number of template headers we expect to see for a definition
2252 or specialization of CTYPE or one of its non-template members. */
2253
2254 int
2255 num_template_headers_for_class (tree ctype)
2256 {
2257 int num_templates = 0;
2258
2259 while (ctype && CLASS_TYPE_P (ctype))
2260 {
2261 /* You're supposed to have one `template <...>' for every
2262 template class, but you don't need one for a full
2263 specialization. For example:
2264
2265 template <class T> struct S{};
2266 template <> struct S<int> { void f(); };
2267 void S<int>::f () {}
2268
2269 is correct; there shouldn't be a `template <>' for the
2270 definition of `S<int>::f'. */
2271 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2272 /* If CTYPE does not have template information of any
2273 kind, then it is not a template, nor is it nested
2274 within a template. */
2275 break;
2276 if (explicit_class_specialization_p (ctype))
2277 break;
2278 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2279 ++num_templates;
2280
2281 ctype = TYPE_CONTEXT (ctype);
2282 }
2283
2284 return num_templates;
2285 }
2286
2287 /* Do a simple sanity check on the template headers that precede the
2288 variable declaration DECL. */
2289
2290 void
2291 check_template_variable (tree decl)
2292 {
2293 tree ctx = CP_DECL_CONTEXT (decl);
2294 int wanted = num_template_headers_for_class (ctx);
2295 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2296 permerror (DECL_SOURCE_LOCATION (decl),
2297 "%qD is not a static data member of a class template", decl);
2298 else if (template_header_count > wanted)
2299 {
2300 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2301 "too many template headers for %D (should be %d)",
2302 decl, wanted);
2303 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2304 inform (DECL_SOURCE_LOCATION (decl),
2305 "members of an explicitly specialized class are defined "
2306 "without a template header");
2307 }
2308 }
2309
2310 /* Check to see if the function just declared, as indicated in
2311 DECLARATOR, and in DECL, is a specialization of a function
2312 template. We may also discover that the declaration is an explicit
2313 instantiation at this point.
2314
2315 Returns DECL, or an equivalent declaration that should be used
2316 instead if all goes well. Issues an error message if something is
2317 amiss. Returns error_mark_node if the error is not easily
2318 recoverable.
2319
2320 FLAGS is a bitmask consisting of the following flags:
2321
2322 2: The function has a definition.
2323 4: The function is a friend.
2324
2325 The TEMPLATE_COUNT is the number of references to qualifying
2326 template classes that appeared in the name of the function. For
2327 example, in
2328
2329 template <class T> struct S { void f(); };
2330 void S<int>::f();
2331
2332 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2333 classes are not counted in the TEMPLATE_COUNT, so that in
2334
2335 template <class T> struct S {};
2336 template <> struct S<int> { void f(); }
2337 template <> void S<int>::f();
2338
2339 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2340 invalid; there should be no template <>.)
2341
2342 If the function is a specialization, it is marked as such via
2343 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2344 is set up correctly, and it is added to the list of specializations
2345 for that template. */
2346
2347 tree
2348 check_explicit_specialization (tree declarator,
2349 tree decl,
2350 int template_count,
2351 int flags)
2352 {
2353 int have_def = flags & 2;
2354 int is_friend = flags & 4;
2355 int specialization = 0;
2356 int explicit_instantiation = 0;
2357 int member_specialization = 0;
2358 tree ctype = DECL_CLASS_CONTEXT (decl);
2359 tree dname = DECL_NAME (decl);
2360 tmpl_spec_kind tsk;
2361
2362 if (is_friend)
2363 {
2364 if (!processing_specialization)
2365 tsk = tsk_none;
2366 else
2367 tsk = tsk_excessive_parms;
2368 }
2369 else
2370 tsk = current_tmpl_spec_kind (template_count);
2371
2372 switch (tsk)
2373 {
2374 case tsk_none:
2375 if (processing_specialization)
2376 {
2377 specialization = 1;
2378 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2379 }
2380 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2381 {
2382 if (is_friend)
2383 /* This could be something like:
2384
2385 template <class T> void f(T);
2386 class S { friend void f<>(int); } */
2387 specialization = 1;
2388 else
2389 {
2390 /* This case handles bogus declarations like template <>
2391 template <class T> void f<int>(); */
2392
2393 error ("template-id %qD in declaration of primary template",
2394 declarator);
2395 return decl;
2396 }
2397 }
2398 break;
2399
2400 case tsk_invalid_member_spec:
2401 /* The error has already been reported in
2402 check_specialization_scope. */
2403 return error_mark_node;
2404
2405 case tsk_invalid_expl_inst:
2406 error ("template parameter list used in explicit instantiation");
2407
2408 /* Fall through. */
2409
2410 case tsk_expl_inst:
2411 if (have_def)
2412 error ("definition provided for explicit instantiation");
2413
2414 explicit_instantiation = 1;
2415 break;
2416
2417 case tsk_excessive_parms:
2418 case tsk_insufficient_parms:
2419 if (tsk == tsk_excessive_parms)
2420 error ("too many template parameter lists in declaration of %qD",
2421 decl);
2422 else if (template_header_count)
2423 error("too few template parameter lists in declaration of %qD", decl);
2424 else
2425 error("explicit specialization of %qD must be introduced by "
2426 "%<template <>%>", decl);
2427
2428 /* Fall through. */
2429 case tsk_expl_spec:
2430 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2431 if (ctype)
2432 member_specialization = 1;
2433 else
2434 specialization = 1;
2435 break;
2436
2437 case tsk_template:
2438 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2439 {
2440 /* This case handles bogus declarations like template <>
2441 template <class T> void f<int>(); */
2442
2443 if (uses_template_parms (declarator))
2444 error ("function template partial specialization %qD "
2445 "is not allowed", declarator);
2446 else
2447 error ("template-id %qD in declaration of primary template",
2448 declarator);
2449 return decl;
2450 }
2451
2452 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2453 /* This is a specialization of a member template, without
2454 specialization the containing class. Something like:
2455
2456 template <class T> struct S {
2457 template <class U> void f (U);
2458 };
2459 template <> template <class U> void S<int>::f(U) {}
2460
2461 That's a specialization -- but of the entire template. */
2462 specialization = 1;
2463 break;
2464
2465 default:
2466 gcc_unreachable ();
2467 }
2468
2469 if (specialization || member_specialization)
2470 {
2471 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2472 for (; t; t = TREE_CHAIN (t))
2473 if (TREE_PURPOSE (t))
2474 {
2475 permerror (input_location,
2476 "default argument specified in explicit specialization");
2477 break;
2478 }
2479 }
2480
2481 if (specialization || member_specialization || explicit_instantiation)
2482 {
2483 tree tmpl = NULL_TREE;
2484 tree targs = NULL_TREE;
2485
2486 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2487 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2488 {
2489 tree fns;
2490
2491 gcc_assert (identifier_p (declarator));
2492 if (ctype)
2493 fns = dname;
2494 else
2495 {
2496 /* If there is no class context, the explicit instantiation
2497 must be at namespace scope. */
2498 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2499
2500 /* Find the namespace binding, using the declaration
2501 context. */
2502 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2503 false, true);
2504 if (fns == error_mark_node || !is_overloaded_fn (fns))
2505 {
2506 error ("%qD is not a template function", dname);
2507 fns = error_mark_node;
2508 }
2509 else
2510 {
2511 tree fn = OVL_CURRENT (fns);
2512 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2513 CP_DECL_CONTEXT (fn)))
2514 error ("%qD is not declared in %qD",
2515 decl, current_namespace);
2516 }
2517 }
2518
2519 declarator = lookup_template_function (fns, NULL_TREE);
2520 }
2521
2522 if (declarator == error_mark_node)
2523 return error_mark_node;
2524
2525 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2526 {
2527 if (!explicit_instantiation)
2528 /* A specialization in class scope. This is invalid,
2529 but the error will already have been flagged by
2530 check_specialization_scope. */
2531 return error_mark_node;
2532 else
2533 {
2534 /* It's not valid to write an explicit instantiation in
2535 class scope, e.g.:
2536
2537 class C { template void f(); }
2538
2539 This case is caught by the parser. However, on
2540 something like:
2541
2542 template class C { void f(); };
2543
2544 (which is invalid) we can get here. The error will be
2545 issued later. */
2546 ;
2547 }
2548
2549 return decl;
2550 }
2551 else if (ctype != NULL_TREE
2552 && (identifier_p (TREE_OPERAND (declarator, 0))))
2553 {
2554 /* Find the list of functions in ctype that have the same
2555 name as the declared function. */
2556 tree name = TREE_OPERAND (declarator, 0);
2557 tree fns = NULL_TREE;
2558 int idx;
2559
2560 if (constructor_name_p (name, ctype))
2561 {
2562 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2563
2564 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2565 : !CLASSTYPE_DESTRUCTORS (ctype))
2566 {
2567 /* From [temp.expl.spec]:
2568
2569 If such an explicit specialization for the member
2570 of a class template names an implicitly-declared
2571 special member function (clause _special_), the
2572 program is ill-formed.
2573
2574 Similar language is found in [temp.explicit]. */
2575 error ("specialization of implicitly-declared special member function");
2576 return error_mark_node;
2577 }
2578
2579 name = is_constructor ? ctor_identifier : dtor_identifier;
2580 }
2581
2582 if (!DECL_CONV_FN_P (decl))
2583 {
2584 idx = lookup_fnfields_1 (ctype, name);
2585 if (idx >= 0)
2586 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2587 }
2588 else
2589 {
2590 vec<tree, va_gc> *methods;
2591 tree ovl;
2592
2593 /* For a type-conversion operator, we cannot do a
2594 name-based lookup. We might be looking for `operator
2595 int' which will be a specialization of `operator T'.
2596 So, we find *all* the conversion operators, and then
2597 select from them. */
2598 fns = NULL_TREE;
2599
2600 methods = CLASSTYPE_METHOD_VEC (ctype);
2601 if (methods)
2602 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2603 methods->iterate (idx, &ovl);
2604 ++idx)
2605 {
2606 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2607 /* There are no more conversion functions. */
2608 break;
2609
2610 /* Glue all these conversion functions together
2611 with those we already have. */
2612 for (; ovl; ovl = OVL_NEXT (ovl))
2613 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2614 }
2615 }
2616
2617 if (fns == NULL_TREE)
2618 {
2619 error ("no member function %qD declared in %qT", name, ctype);
2620 return error_mark_node;
2621 }
2622 else
2623 TREE_OPERAND (declarator, 0) = fns;
2624 }
2625
2626 /* Figure out what exactly is being specialized at this point.
2627 Note that for an explicit instantiation, even one for a
2628 member function, we cannot tell apriori whether the
2629 instantiation is for a member template, or just a member
2630 function of a template class. Even if a member template is
2631 being instantiated, the member template arguments may be
2632 elided if they can be deduced from the rest of the
2633 declaration. */
2634 tmpl = determine_specialization (declarator, decl,
2635 &targs,
2636 member_specialization,
2637 template_count,
2638 tsk);
2639
2640 if (!tmpl || tmpl == error_mark_node)
2641 /* We couldn't figure out what this declaration was
2642 specializing. */
2643 return error_mark_node;
2644 else
2645 {
2646 tree gen_tmpl = most_general_template (tmpl);
2647
2648 if (explicit_instantiation)
2649 {
2650 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2651 is done by do_decl_instantiation later. */
2652
2653 int arg_depth = TMPL_ARGS_DEPTH (targs);
2654 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2655
2656 if (arg_depth > parm_depth)
2657 {
2658 /* If TMPL is not the most general template (for
2659 example, if TMPL is a friend template that is
2660 injected into namespace scope), then there will
2661 be too many levels of TARGS. Remove some of them
2662 here. */
2663 int i;
2664 tree new_targs;
2665
2666 new_targs = make_tree_vec (parm_depth);
2667 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2668 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2669 = TREE_VEC_ELT (targs, i);
2670 targs = new_targs;
2671 }
2672
2673 return instantiate_template (tmpl, targs, tf_error);
2674 }
2675
2676 /* If we thought that the DECL was a member function, but it
2677 turns out to be specializing a static member function,
2678 make DECL a static member function as well. */
2679 if (DECL_STATIC_FUNCTION_P (tmpl)
2680 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2681 revert_static_member_fn (decl);
2682
2683 /* If this is a specialization of a member template of a
2684 template class, we want to return the TEMPLATE_DECL, not
2685 the specialization of it. */
2686 if (tsk == tsk_template)
2687 {
2688 tree result = DECL_TEMPLATE_RESULT (tmpl);
2689 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2690 DECL_INITIAL (result) = NULL_TREE;
2691 if (have_def)
2692 {
2693 tree parm;
2694 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2695 DECL_SOURCE_LOCATION (result)
2696 = DECL_SOURCE_LOCATION (decl);
2697 /* We want to use the argument list specified in the
2698 definition, not in the original declaration. */
2699 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2700 for (parm = DECL_ARGUMENTS (result); parm;
2701 parm = DECL_CHAIN (parm))
2702 DECL_CONTEXT (parm) = result;
2703 }
2704 return register_specialization (tmpl, gen_tmpl, targs,
2705 is_friend, 0);
2706 }
2707
2708 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2709 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2710
2711 /* Inherit default function arguments from the template
2712 DECL is specializing. */
2713 copy_default_args_to_explicit_spec (decl);
2714
2715 /* This specialization has the same protection as the
2716 template it specializes. */
2717 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2718 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2719
2720 /* 7.1.1-1 [dcl.stc]
2721
2722 A storage-class-specifier shall not be specified in an
2723 explicit specialization...
2724
2725 The parser rejects these, so unless action is taken here,
2726 explicit function specializations will always appear with
2727 global linkage.
2728
2729 The action recommended by the C++ CWG in response to C++
2730 defect report 605 is to make the storage class and linkage
2731 of the explicit specialization match the templated function:
2732
2733 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2734 */
2735 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2736 {
2737 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2738 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2739
2740 /* This specialization has the same linkage and visibility as
2741 the function template it specializes. */
2742 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2743 if (! TREE_PUBLIC (decl))
2744 {
2745 DECL_INTERFACE_KNOWN (decl) = 1;
2746 DECL_NOT_REALLY_EXTERN (decl) = 1;
2747 }
2748 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2749 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2750 {
2751 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2752 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2753 }
2754 }
2755
2756 /* If DECL is a friend declaration, declared using an
2757 unqualified name, the namespace associated with DECL may
2758 have been set incorrectly. For example, in:
2759
2760 template <typename T> void f(T);
2761 namespace N {
2762 struct S { friend void f<int>(int); }
2763 }
2764
2765 we will have set the DECL_CONTEXT for the friend
2766 declaration to N, rather than to the global namespace. */
2767 if (DECL_NAMESPACE_SCOPE_P (decl))
2768 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2769
2770 if (is_friend && !have_def)
2771 /* This is not really a declaration of a specialization.
2772 It's just the name of an instantiation. But, it's not
2773 a request for an instantiation, either. */
2774 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2775 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2776 /* This is indeed a specialization. In case of constructors
2777 and destructors, we need in-charge and not-in-charge
2778 versions in V3 ABI. */
2779 clone_function_decl (decl, /*update_method_vec_p=*/0);
2780
2781 /* Register this specialization so that we can find it
2782 again. */
2783 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2784 }
2785 }
2786
2787 return decl;
2788 }
2789
2790 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2791 parameters. These are represented in the same format used for
2792 DECL_TEMPLATE_PARMS. */
2793
2794 int
2795 comp_template_parms (const_tree parms1, const_tree parms2)
2796 {
2797 const_tree p1;
2798 const_tree p2;
2799
2800 if (parms1 == parms2)
2801 return 1;
2802
2803 for (p1 = parms1, p2 = parms2;
2804 p1 != NULL_TREE && p2 != NULL_TREE;
2805 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2806 {
2807 tree t1 = TREE_VALUE (p1);
2808 tree t2 = TREE_VALUE (p2);
2809 int i;
2810
2811 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2812 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2813
2814 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2815 return 0;
2816
2817 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2818 {
2819 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2820 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2821
2822 /* If either of the template parameters are invalid, assume
2823 they match for the sake of error recovery. */
2824 if (parm1 == error_mark_node || parm2 == error_mark_node)
2825 return 1;
2826
2827 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2828 return 0;
2829
2830 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2831 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2832 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2833 continue;
2834 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2835 return 0;
2836 }
2837 }
2838
2839 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2840 /* One set of parameters has more parameters lists than the
2841 other. */
2842 return 0;
2843
2844 return 1;
2845 }
2846
2847 /* Determine whether PARM is a parameter pack. */
2848
2849 bool
2850 template_parameter_pack_p (const_tree parm)
2851 {
2852 /* Determine if we have a non-type template parameter pack. */
2853 if (TREE_CODE (parm) == PARM_DECL)
2854 return (DECL_TEMPLATE_PARM_P (parm)
2855 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2856 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2857 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2858
2859 /* If this is a list of template parameters, we could get a
2860 TYPE_DECL or a TEMPLATE_DECL. */
2861 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2862 parm = TREE_TYPE (parm);
2863
2864 /* Otherwise it must be a type template parameter. */
2865 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2866 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2867 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2868 }
2869
2870 /* Determine if T is a function parameter pack. */
2871
2872 bool
2873 function_parameter_pack_p (const_tree t)
2874 {
2875 if (t && TREE_CODE (t) == PARM_DECL)
2876 return DECL_PACK_P (t);
2877 return false;
2878 }
2879
2880 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2881 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2882
2883 tree
2884 get_function_template_decl (const_tree primary_func_tmpl_inst)
2885 {
2886 if (! primary_func_tmpl_inst
2887 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2888 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2889 return NULL;
2890
2891 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2892 }
2893
2894 /* Return true iff the function parameter PARAM_DECL was expanded
2895 from the function parameter pack PACK. */
2896
2897 bool
2898 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2899 {
2900 if (DECL_ARTIFICIAL (param_decl)
2901 || !function_parameter_pack_p (pack))
2902 return false;
2903
2904 /* The parameter pack and its pack arguments have the same
2905 DECL_PARM_INDEX. */
2906 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2907 }
2908
2909 /* Determine whether ARGS describes a variadic template args list,
2910 i.e., one that is terminated by a template argument pack. */
2911
2912 static bool
2913 template_args_variadic_p (tree args)
2914 {
2915 int nargs;
2916 tree last_parm;
2917
2918 if (args == NULL_TREE)
2919 return false;
2920
2921 args = INNERMOST_TEMPLATE_ARGS (args);
2922 nargs = TREE_VEC_LENGTH (args);
2923
2924 if (nargs == 0)
2925 return false;
2926
2927 last_parm = TREE_VEC_ELT (args, nargs - 1);
2928
2929 return ARGUMENT_PACK_P (last_parm);
2930 }
2931
2932 /* Generate a new name for the parameter pack name NAME (an
2933 IDENTIFIER_NODE) that incorporates its */
2934
2935 static tree
2936 make_ith_pack_parameter_name (tree name, int i)
2937 {
2938 /* Munge the name to include the parameter index. */
2939 #define NUMBUF_LEN 128
2940 char numbuf[NUMBUF_LEN];
2941 char* newname;
2942 int newname_len;
2943
2944 if (name == NULL_TREE)
2945 return name;
2946 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2947 newname_len = IDENTIFIER_LENGTH (name)
2948 + strlen (numbuf) + 2;
2949 newname = (char*)alloca (newname_len);
2950 snprintf (newname, newname_len,
2951 "%s#%i", IDENTIFIER_POINTER (name), i);
2952 return get_identifier (newname);
2953 }
2954
2955 /* Return true if T is a primary function, class or alias template
2956 instantiation. */
2957
2958 bool
2959 primary_template_instantiation_p (const_tree t)
2960 {
2961 if (!t)
2962 return false;
2963
2964 if (TREE_CODE (t) == FUNCTION_DECL)
2965 return DECL_LANG_SPECIFIC (t)
2966 && DECL_TEMPLATE_INSTANTIATION (t)
2967 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2968 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2969 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2970 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2971 else if (alias_template_specialization_p (t))
2972 return true;
2973 return false;
2974 }
2975
2976 /* Return true if PARM is a template template parameter. */
2977
2978 bool
2979 template_template_parameter_p (const_tree parm)
2980 {
2981 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2982 }
2983
2984 /* Return true iff PARM is a DECL representing a type template
2985 parameter. */
2986
2987 bool
2988 template_type_parameter_p (const_tree parm)
2989 {
2990 return (parm
2991 && (TREE_CODE (parm) == TYPE_DECL
2992 || TREE_CODE (parm) == TEMPLATE_DECL)
2993 && DECL_TEMPLATE_PARM_P (parm));
2994 }
2995
2996 /* Return the template parameters of T if T is a
2997 primary template instantiation, NULL otherwise. */
2998
2999 tree
3000 get_primary_template_innermost_parameters (const_tree t)
3001 {
3002 tree parms = NULL, template_info = NULL;
3003
3004 if ((template_info = get_template_info (t))
3005 && primary_template_instantiation_p (t))
3006 parms = INNERMOST_TEMPLATE_PARMS
3007 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3008
3009 return parms;
3010 }
3011
3012 /* Return the template parameters of the LEVELth level from the full list
3013 of template parameters PARMS. */
3014
3015 tree
3016 get_template_parms_at_level (tree parms, int level)
3017 {
3018 tree p;
3019 if (!parms
3020 || TREE_CODE (parms) != TREE_LIST
3021 || level > TMPL_PARMS_DEPTH (parms))
3022 return NULL_TREE;
3023
3024 for (p = parms; p; p = TREE_CHAIN (p))
3025 if (TMPL_PARMS_DEPTH (p) == level)
3026 return p;
3027
3028 return NULL_TREE;
3029 }
3030
3031 /* Returns the template arguments of T if T is a template instantiation,
3032 NULL otherwise. */
3033
3034 tree
3035 get_template_innermost_arguments (const_tree t)
3036 {
3037 tree args = NULL, template_info = NULL;
3038
3039 if ((template_info = get_template_info (t))
3040 && TI_ARGS (template_info))
3041 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3042
3043 return args;
3044 }
3045
3046 /* Return the argument pack elements of T if T is a template argument pack,
3047 NULL otherwise. */
3048
3049 tree
3050 get_template_argument_pack_elems (const_tree t)
3051 {
3052 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3053 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3054 return NULL;
3055
3056 return ARGUMENT_PACK_ARGS (t);
3057 }
3058
3059 /* Structure used to track the progress of find_parameter_packs_r. */
3060 struct find_parameter_pack_data
3061 {
3062 /* TREE_LIST that will contain all of the parameter packs found by
3063 the traversal. */
3064 tree* parameter_packs;
3065
3066 /* Set of AST nodes that have been visited by the traversal. */
3067 struct pointer_set_t *visited;
3068 };
3069
3070 /* Identifies all of the argument packs that occur in a template
3071 argument and appends them to the TREE_LIST inside DATA, which is a
3072 find_parameter_pack_data structure. This is a subroutine of
3073 make_pack_expansion and uses_parameter_packs. */
3074 static tree
3075 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3076 {
3077 tree t = *tp;
3078 struct find_parameter_pack_data* ppd =
3079 (struct find_parameter_pack_data*)data;
3080 bool parameter_pack_p = false;
3081
3082 /* Handle type aliases/typedefs. */
3083 if (TYPE_ALIAS_P (t))
3084 {
3085 if (TYPE_TEMPLATE_INFO (t))
3086 cp_walk_tree (&TYPE_TI_ARGS (t),
3087 &find_parameter_packs_r,
3088 ppd, ppd->visited);
3089 *walk_subtrees = 0;
3090 return NULL_TREE;
3091 }
3092
3093 /* Identify whether this is a parameter pack or not. */
3094 switch (TREE_CODE (t))
3095 {
3096 case TEMPLATE_PARM_INDEX:
3097 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3098 parameter_pack_p = true;
3099 break;
3100
3101 case TEMPLATE_TYPE_PARM:
3102 t = TYPE_MAIN_VARIANT (t);
3103 case TEMPLATE_TEMPLATE_PARM:
3104 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3105 parameter_pack_p = true;
3106 break;
3107
3108 case FIELD_DECL:
3109 case PARM_DECL:
3110 if (DECL_PACK_P (t))
3111 {
3112 /* We don't want to walk into the type of a PARM_DECL,
3113 because we don't want to see the type parameter pack. */
3114 *walk_subtrees = 0;
3115 parameter_pack_p = true;
3116 }
3117 break;
3118
3119 /* Look through a lambda capture proxy to the field pack. */
3120 case VAR_DECL:
3121 if (DECL_HAS_VALUE_EXPR_P (t))
3122 {
3123 tree v = DECL_VALUE_EXPR (t);
3124 cp_walk_tree (&v,
3125 &find_parameter_packs_r,
3126 ppd, ppd->visited);
3127 *walk_subtrees = 0;
3128 }
3129 break;
3130
3131 case BASES:
3132 parameter_pack_p = true;
3133 break;
3134 default:
3135 /* Not a parameter pack. */
3136 break;
3137 }
3138
3139 if (parameter_pack_p)
3140 {
3141 /* Add this parameter pack to the list. */
3142 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3143 }
3144
3145 if (TYPE_P (t))
3146 cp_walk_tree (&TYPE_CONTEXT (t),
3147 &find_parameter_packs_r, ppd, ppd->visited);
3148
3149 /* This switch statement will return immediately if we don't find a
3150 parameter pack. */
3151 switch (TREE_CODE (t))
3152 {
3153 case TEMPLATE_PARM_INDEX:
3154 return NULL_TREE;
3155
3156 case BOUND_TEMPLATE_TEMPLATE_PARM:
3157 /* Check the template itself. */
3158 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3159 &find_parameter_packs_r, ppd, ppd->visited);
3160 /* Check the template arguments. */
3161 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3162 ppd->visited);
3163 *walk_subtrees = 0;
3164 return NULL_TREE;
3165
3166 case TEMPLATE_TYPE_PARM:
3167 case TEMPLATE_TEMPLATE_PARM:
3168 return NULL_TREE;
3169
3170 case PARM_DECL:
3171 return NULL_TREE;
3172
3173 case RECORD_TYPE:
3174 if (TYPE_PTRMEMFUNC_P (t))
3175 return NULL_TREE;
3176 /* Fall through. */
3177
3178 case UNION_TYPE:
3179 case ENUMERAL_TYPE:
3180 if (TYPE_TEMPLATE_INFO (t))
3181 cp_walk_tree (&TYPE_TI_ARGS (t),
3182 &find_parameter_packs_r, ppd, ppd->visited);
3183
3184 *walk_subtrees = 0;
3185 return NULL_TREE;
3186
3187 case CONSTRUCTOR:
3188 case TEMPLATE_DECL:
3189 cp_walk_tree (&TREE_TYPE (t),
3190 &find_parameter_packs_r, ppd, ppd->visited);
3191 return NULL_TREE;
3192
3193 case TYPENAME_TYPE:
3194 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3195 ppd, ppd->visited);
3196 *walk_subtrees = 0;
3197 return NULL_TREE;
3198
3199 case TYPE_PACK_EXPANSION:
3200 case EXPR_PACK_EXPANSION:
3201 *walk_subtrees = 0;
3202 return NULL_TREE;
3203
3204 case INTEGER_TYPE:
3205 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3206 ppd, ppd->visited);
3207 *walk_subtrees = 0;
3208 return NULL_TREE;
3209
3210 case IDENTIFIER_NODE:
3211 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3212 ppd->visited);
3213 *walk_subtrees = 0;
3214 return NULL_TREE;
3215
3216 default:
3217 return NULL_TREE;
3218 }
3219
3220 return NULL_TREE;
3221 }
3222
3223 /* Determines if the expression or type T uses any parameter packs. */
3224 bool
3225 uses_parameter_packs (tree t)
3226 {
3227 tree parameter_packs = NULL_TREE;
3228 struct find_parameter_pack_data ppd;
3229 ppd.parameter_packs = &parameter_packs;
3230 ppd.visited = pointer_set_create ();
3231 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3232 pointer_set_destroy (ppd.visited);
3233 return parameter_packs != NULL_TREE;
3234 }
3235
3236 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3237 representation a base-class initializer into a parameter pack
3238 expansion. If all goes well, the resulting node will be an
3239 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3240 respectively. */
3241 tree
3242 make_pack_expansion (tree arg)
3243 {
3244 tree result;
3245 tree parameter_packs = NULL_TREE;
3246 bool for_types = false;
3247 struct find_parameter_pack_data ppd;
3248
3249 if (!arg || arg == error_mark_node)
3250 return arg;
3251
3252 if (TREE_CODE (arg) == TREE_LIST)
3253 {
3254 /* The only time we will see a TREE_LIST here is for a base
3255 class initializer. In this case, the TREE_PURPOSE will be a
3256 _TYPE node (representing the base class expansion we're
3257 initializing) and the TREE_VALUE will be a TREE_LIST
3258 containing the initialization arguments.
3259
3260 The resulting expansion looks somewhat different from most
3261 expansions. Rather than returning just one _EXPANSION, we
3262 return a TREE_LIST whose TREE_PURPOSE is a
3263 TYPE_PACK_EXPANSION containing the bases that will be
3264 initialized. The TREE_VALUE will be identical to the
3265 original TREE_VALUE, which is a list of arguments that will
3266 be passed to each base. We do not introduce any new pack
3267 expansion nodes into the TREE_VALUE (although it is possible
3268 that some already exist), because the TREE_PURPOSE and
3269 TREE_VALUE all need to be expanded together with the same
3270 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3271 resulting TREE_PURPOSE will mention the parameter packs in
3272 both the bases and the arguments to the bases. */
3273 tree purpose;
3274 tree value;
3275 tree parameter_packs = NULL_TREE;
3276
3277 /* Determine which parameter packs will be used by the base
3278 class expansion. */
3279 ppd.visited = pointer_set_create ();
3280 ppd.parameter_packs = &parameter_packs;
3281 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3282 &ppd, ppd.visited);
3283
3284 if (parameter_packs == NULL_TREE)
3285 {
3286 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3287 pointer_set_destroy (ppd.visited);
3288 return error_mark_node;
3289 }
3290
3291 if (TREE_VALUE (arg) != void_type_node)
3292 {
3293 /* Collect the sets of parameter packs used in each of the
3294 initialization arguments. */
3295 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3296 {
3297 /* Determine which parameter packs will be expanded in this
3298 argument. */
3299 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3300 &ppd, ppd.visited);
3301 }
3302 }
3303
3304 pointer_set_destroy (ppd.visited);
3305
3306 /* Create the pack expansion type for the base type. */
3307 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3308 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3309 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3310
3311 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3312 they will rarely be compared to anything. */
3313 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3314
3315 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3316 }
3317
3318 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3319 for_types = true;
3320
3321 /* Build the PACK_EXPANSION_* node. */
3322 result = for_types
3323 ? cxx_make_type (TYPE_PACK_EXPANSION)
3324 : make_node (EXPR_PACK_EXPANSION);
3325 SET_PACK_EXPANSION_PATTERN (result, arg);
3326 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3327 {
3328 /* Propagate type and const-expression information. */
3329 TREE_TYPE (result) = TREE_TYPE (arg);
3330 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3331 }
3332 else
3333 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3334 they will rarely be compared to anything. */
3335 SET_TYPE_STRUCTURAL_EQUALITY (result);
3336
3337 /* Determine which parameter packs will be expanded. */
3338 ppd.parameter_packs = &parameter_packs;
3339 ppd.visited = pointer_set_create ();
3340 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3341 pointer_set_destroy (ppd.visited);
3342
3343 /* Make sure we found some parameter packs. */
3344 if (parameter_packs == NULL_TREE)
3345 {
3346 if (TYPE_P (arg))
3347 error ("expansion pattern %<%T%> contains no argument packs", arg);
3348 else
3349 error ("expansion pattern %<%E%> contains no argument packs", arg);
3350 return error_mark_node;
3351 }
3352 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3353
3354 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3355
3356 return result;
3357 }
3358
3359 /* Checks T for any "bare" parameter packs, which have not yet been
3360 expanded, and issues an error if any are found. This operation can
3361 only be done on full expressions or types (e.g., an expression
3362 statement, "if" condition, etc.), because we could have expressions like:
3363
3364 foo(f(g(h(args)))...)
3365
3366 where "args" is a parameter pack. check_for_bare_parameter_packs
3367 should not be called for the subexpressions args, h(args),
3368 g(h(args)), or f(g(h(args))), because we would produce erroneous
3369 error messages.
3370
3371 Returns TRUE and emits an error if there were bare parameter packs,
3372 returns FALSE otherwise. */
3373 bool
3374 check_for_bare_parameter_packs (tree t)
3375 {
3376 tree parameter_packs = NULL_TREE;
3377 struct find_parameter_pack_data ppd;
3378
3379 if (!processing_template_decl || !t || t == error_mark_node)
3380 return false;
3381
3382 if (TREE_CODE (t) == TYPE_DECL)
3383 t = TREE_TYPE (t);
3384
3385 ppd.parameter_packs = &parameter_packs;
3386 ppd.visited = pointer_set_create ();
3387 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3388 pointer_set_destroy (ppd.visited);
3389
3390 if (parameter_packs)
3391 {
3392 error ("parameter packs not expanded with %<...%>:");
3393 while (parameter_packs)
3394 {
3395 tree pack = TREE_VALUE (parameter_packs);
3396 tree name = NULL_TREE;
3397
3398 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3399 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3400 name = TYPE_NAME (pack);
3401 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3402 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3403 else
3404 name = DECL_NAME (pack);
3405
3406 if (name)
3407 inform (input_location, " %qD", name);
3408 else
3409 inform (input_location, " <anonymous>");
3410
3411 parameter_packs = TREE_CHAIN (parameter_packs);
3412 }
3413
3414 return true;
3415 }
3416
3417 return false;
3418 }
3419
3420 /* Expand any parameter packs that occur in the template arguments in
3421 ARGS. */
3422 tree
3423 expand_template_argument_pack (tree args)
3424 {
3425 tree result_args = NULL_TREE;
3426 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3427 int num_result_args = -1;
3428 int non_default_args_count = -1;
3429
3430 /* First, determine if we need to expand anything, and the number of
3431 slots we'll need. */
3432 for (in_arg = 0; in_arg < nargs; ++in_arg)
3433 {
3434 tree arg = TREE_VEC_ELT (args, in_arg);
3435 if (arg == NULL_TREE)
3436 return args;
3437 if (ARGUMENT_PACK_P (arg))
3438 {
3439 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3440 if (num_result_args < 0)
3441 num_result_args = in_arg + num_packed;
3442 else
3443 num_result_args += num_packed;
3444 }
3445 else
3446 {
3447 if (num_result_args >= 0)
3448 num_result_args++;
3449 }
3450 }
3451
3452 /* If no expansion is necessary, we're done. */
3453 if (num_result_args < 0)
3454 return args;
3455
3456 /* Expand arguments. */
3457 result_args = make_tree_vec (num_result_args);
3458 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3459 non_default_args_count =
3460 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3461 for (in_arg = 0; in_arg < nargs; ++in_arg)
3462 {
3463 tree arg = TREE_VEC_ELT (args, in_arg);
3464 if (ARGUMENT_PACK_P (arg))
3465 {
3466 tree packed = ARGUMENT_PACK_ARGS (arg);
3467 int i, num_packed = TREE_VEC_LENGTH (packed);
3468 for (i = 0; i < num_packed; ++i, ++out_arg)
3469 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3470 if (non_default_args_count > 0)
3471 non_default_args_count += num_packed;
3472 }
3473 else
3474 {
3475 TREE_VEC_ELT (result_args, out_arg) = arg;
3476 ++out_arg;
3477 }
3478 }
3479 if (non_default_args_count >= 0)
3480 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3481 return result_args;
3482 }
3483
3484 /* Checks if DECL shadows a template parameter.
3485
3486 [temp.local]: A template-parameter shall not be redeclared within its
3487 scope (including nested scopes).
3488
3489 Emits an error and returns TRUE if the DECL shadows a parameter,
3490 returns FALSE otherwise. */
3491
3492 bool
3493 check_template_shadow (tree decl)
3494 {
3495 tree olddecl;
3496
3497 /* If we're not in a template, we can't possibly shadow a template
3498 parameter. */
3499 if (!current_template_parms)
3500 return true;
3501
3502 /* Figure out what we're shadowing. */
3503 if (TREE_CODE (decl) == OVERLOAD)
3504 decl = OVL_CURRENT (decl);
3505 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3506
3507 /* If there's no previous binding for this name, we're not shadowing
3508 anything, let alone a template parameter. */
3509 if (!olddecl)
3510 return true;
3511
3512 /* If we're not shadowing a template parameter, we're done. Note
3513 that OLDDECL might be an OVERLOAD (or perhaps even an
3514 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3515 node. */
3516 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3517 return true;
3518
3519 /* We check for decl != olddecl to avoid bogus errors for using a
3520 name inside a class. We check TPFI to avoid duplicate errors for
3521 inline member templates. */
3522 if (decl == olddecl
3523 || (DECL_TEMPLATE_PARM_P (decl)
3524 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3525 return true;
3526
3527 error ("declaration of %q+#D", decl);
3528 error (" shadows template parm %q+#D", olddecl);
3529 return false;
3530 }
3531
3532 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3533 ORIG_LEVEL, DECL, and TYPE. */
3534
3535 static tree
3536 build_template_parm_index (int index,
3537 int level,
3538 int orig_level,
3539 tree decl,
3540 tree type)
3541 {
3542 tree t = make_node (TEMPLATE_PARM_INDEX);
3543 TEMPLATE_PARM_IDX (t) = index;
3544 TEMPLATE_PARM_LEVEL (t) = level;
3545 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3546 TEMPLATE_PARM_DECL (t) = decl;
3547 TREE_TYPE (t) = type;
3548 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3549 TREE_READONLY (t) = TREE_READONLY (decl);
3550
3551 return t;
3552 }
3553
3554 /* Find the canonical type parameter for the given template type
3555 parameter. Returns the canonical type parameter, which may be TYPE
3556 if no such parameter existed. */
3557
3558 static tree
3559 canonical_type_parameter (tree type)
3560 {
3561 tree list;
3562 int idx = TEMPLATE_TYPE_IDX (type);
3563 if (!canonical_template_parms)
3564 vec_alloc (canonical_template_parms, idx+1);
3565
3566 while (canonical_template_parms->length () <= (unsigned)idx)
3567 vec_safe_push (canonical_template_parms, NULL_TREE);
3568
3569 list = (*canonical_template_parms)[idx];
3570 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3571 list = TREE_CHAIN (list);
3572
3573 if (list)
3574 return TREE_VALUE (list);
3575 else
3576 {
3577 (*canonical_template_parms)[idx]
3578 = tree_cons (NULL_TREE, type,
3579 (*canonical_template_parms)[idx]);
3580 return type;
3581 }
3582 }
3583
3584 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3585 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3586 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3587 new one is created. */
3588
3589 static tree
3590 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3591 tsubst_flags_t complain)
3592 {
3593 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3594 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3595 != TEMPLATE_PARM_LEVEL (index) - levels)
3596 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3597 {
3598 tree orig_decl = TEMPLATE_PARM_DECL (index);
3599 tree decl, t;
3600
3601 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3602 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3603 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3604 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3605 DECL_ARTIFICIAL (decl) = 1;
3606 SET_DECL_TEMPLATE_PARM_P (decl);
3607
3608 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3609 TEMPLATE_PARM_LEVEL (index) - levels,
3610 TEMPLATE_PARM_ORIG_LEVEL (index),
3611 decl, type);
3612 TEMPLATE_PARM_DESCENDANTS (index) = t;
3613 TEMPLATE_PARM_PARAMETER_PACK (t)
3614 = TEMPLATE_PARM_PARAMETER_PACK (index);
3615
3616 /* Template template parameters need this. */
3617 if (TREE_CODE (decl) == TEMPLATE_DECL)
3618 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3619 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3620 args, complain);
3621 }
3622
3623 return TEMPLATE_PARM_DESCENDANTS (index);
3624 }
3625
3626 /* Process information from new template parameter PARM and append it
3627 to the LIST being built. This new parameter is a non-type
3628 parameter iff IS_NON_TYPE is true. This new parameter is a
3629 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3630 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3631 parameter list PARM belongs to. This is used used to create a
3632 proper canonical type for the type of PARM that is to be created,
3633 iff PARM is a type. If the size is not known, this parameter shall
3634 be set to 0. */
3635
3636 tree
3637 process_template_parm (tree list, location_t parm_loc, tree parm,
3638 bool is_non_type, bool is_parameter_pack)
3639 {
3640 tree decl = 0;
3641 tree defval;
3642 tree err_parm_list;
3643 int idx = 0;
3644
3645 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3646 defval = TREE_PURPOSE (parm);
3647
3648 if (list)
3649 {
3650 tree p = tree_last (list);
3651
3652 if (p && TREE_VALUE (p) != error_mark_node)
3653 {
3654 p = TREE_VALUE (p);
3655 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3656 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3657 else
3658 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3659 }
3660
3661 ++idx;
3662 }
3663 else
3664 idx = 0;
3665
3666 if (is_non_type)
3667 {
3668 parm = TREE_VALUE (parm);
3669
3670 SET_DECL_TEMPLATE_PARM_P (parm);
3671
3672 if (TREE_TYPE (parm) == error_mark_node)
3673 {
3674 err_parm_list = build_tree_list (defval, parm);
3675 TREE_VALUE (err_parm_list) = error_mark_node;
3676 return chainon (list, err_parm_list);
3677 }
3678 else
3679 {
3680 /* [temp.param]
3681
3682 The top-level cv-qualifiers on the template-parameter are
3683 ignored when determining its type. */
3684 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3685 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3686 {
3687 err_parm_list = build_tree_list (defval, parm);
3688 TREE_VALUE (err_parm_list) = error_mark_node;
3689 return chainon (list, err_parm_list);
3690 }
3691
3692 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3693 {
3694 /* This template parameter is not a parameter pack, but it
3695 should be. Complain about "bare" parameter packs. */
3696 check_for_bare_parameter_packs (TREE_TYPE (parm));
3697
3698 /* Recover by calling this a parameter pack. */
3699 is_parameter_pack = true;
3700 }
3701 }
3702
3703 /* A template parameter is not modifiable. */
3704 TREE_CONSTANT (parm) = 1;
3705 TREE_READONLY (parm) = 1;
3706 decl = build_decl (parm_loc,
3707 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3708 TREE_CONSTANT (decl) = 1;
3709 TREE_READONLY (decl) = 1;
3710 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3711 = build_template_parm_index (idx, processing_template_decl,
3712 processing_template_decl,
3713 decl, TREE_TYPE (parm));
3714
3715 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3716 = is_parameter_pack;
3717 }
3718 else
3719 {
3720 tree t;
3721 parm = TREE_VALUE (TREE_VALUE (parm));
3722
3723 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3724 {
3725 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3726 /* This is for distinguishing between real templates and template
3727 template parameters */
3728 TREE_TYPE (parm) = t;
3729 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3730 decl = parm;
3731 }
3732 else
3733 {
3734 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3735 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3736 decl = build_decl (parm_loc,
3737 TYPE_DECL, parm, t);
3738 }
3739
3740 TYPE_NAME (t) = decl;
3741 TYPE_STUB_DECL (t) = decl;
3742 parm = decl;
3743 TEMPLATE_TYPE_PARM_INDEX (t)
3744 = build_template_parm_index (idx, processing_template_decl,
3745 processing_template_decl,
3746 decl, TREE_TYPE (parm));
3747 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3748 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3749 }
3750 DECL_ARTIFICIAL (decl) = 1;
3751 SET_DECL_TEMPLATE_PARM_P (decl);
3752 pushdecl (decl);
3753 parm = build_tree_list (defval, parm);
3754 return chainon (list, parm);
3755 }
3756
3757 /* The end of a template parameter list has been reached. Process the
3758 tree list into a parameter vector, converting each parameter into a more
3759 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3760 as PARM_DECLs. */
3761
3762 tree
3763 end_template_parm_list (tree parms)
3764 {
3765 int nparms;
3766 tree parm, next;
3767 tree saved_parmlist = make_tree_vec (list_length (parms));
3768
3769 current_template_parms
3770 = tree_cons (size_int (processing_template_decl),
3771 saved_parmlist, current_template_parms);
3772
3773 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3774 {
3775 next = TREE_CHAIN (parm);
3776 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3777 TREE_CHAIN (parm) = NULL_TREE;
3778 }
3779
3780 --processing_template_parmlist;
3781
3782 return saved_parmlist;
3783 }
3784
3785 /* end_template_decl is called after a template declaration is seen. */
3786
3787 void
3788 end_template_decl (void)
3789 {
3790 reset_specialization ();
3791
3792 if (! processing_template_decl)
3793 return;
3794
3795 /* This matches the pushlevel in begin_template_parm_list. */
3796 finish_scope ();
3797
3798 --processing_template_decl;
3799 current_template_parms = TREE_CHAIN (current_template_parms);
3800 }
3801
3802 /* Takes a TREE_LIST representing a template parameter and convert it
3803 into an argument suitable to be passed to the type substitution
3804 functions. Note that If the TREE_LIST contains an error_mark
3805 node, the returned argument is error_mark_node. */
3806
3807 static tree
3808 template_parm_to_arg (tree t)
3809 {
3810
3811 if (t == NULL_TREE
3812 || TREE_CODE (t) != TREE_LIST)
3813 return t;
3814
3815 if (error_operand_p (TREE_VALUE (t)))
3816 return error_mark_node;
3817
3818 t = TREE_VALUE (t);
3819
3820 if (TREE_CODE (t) == TYPE_DECL
3821 || TREE_CODE (t) == TEMPLATE_DECL)
3822 {
3823 t = TREE_TYPE (t);
3824
3825 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3826 {
3827 /* Turn this argument into a TYPE_ARGUMENT_PACK
3828 with a single element, which expands T. */
3829 tree vec = make_tree_vec (1);
3830 #ifdef ENABLE_CHECKING
3831 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3832 (vec, TREE_VEC_LENGTH (vec));
3833 #endif
3834 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3835
3836 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3837 SET_ARGUMENT_PACK_ARGS (t, vec);
3838 }
3839 }
3840 else
3841 {
3842 t = DECL_INITIAL (t);
3843
3844 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3845 {
3846 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3847 with a single element, which expands T. */
3848 tree vec = make_tree_vec (1);
3849 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3850 #ifdef ENABLE_CHECKING
3851 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3852 (vec, TREE_VEC_LENGTH (vec));
3853 #endif
3854 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3855
3856 t = make_node (NONTYPE_ARGUMENT_PACK);
3857 SET_ARGUMENT_PACK_ARGS (t, vec);
3858 TREE_TYPE (t) = type;
3859 }
3860 }
3861 return t;
3862 }
3863
3864 /* Given a set of template parameters, return them as a set of template
3865 arguments. The template parameters are represented as a TREE_VEC, in
3866 the form documented in cp-tree.h for template arguments. */
3867
3868 static tree
3869 template_parms_to_args (tree parms)
3870 {
3871 tree header;
3872 tree args = NULL_TREE;
3873 int length = TMPL_PARMS_DEPTH (parms);
3874 int l = length;
3875
3876 /* If there is only one level of template parameters, we do not
3877 create a TREE_VEC of TREE_VECs. Instead, we return a single
3878 TREE_VEC containing the arguments. */
3879 if (length > 1)
3880 args = make_tree_vec (length);
3881
3882 for (header = parms; header; header = TREE_CHAIN (header))
3883 {
3884 tree a = copy_node (TREE_VALUE (header));
3885 int i;
3886
3887 TREE_TYPE (a) = NULL_TREE;
3888 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3889 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3890
3891 #ifdef ENABLE_CHECKING
3892 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3893 #endif
3894
3895 if (length > 1)
3896 TREE_VEC_ELT (args, --l) = a;
3897 else
3898 args = a;
3899 }
3900
3901 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3902 /* This can happen for template parms of a template template
3903 parameter, e.g:
3904
3905 template<template<class T, class U> class TT> struct S;
3906
3907 Consider the level of the parms of TT; T and U both have
3908 level 2; TT has no template parm of level 1. So in this case
3909 the first element of full_template_args is NULL_TREE. If we
3910 leave it like this TMPL_ARGS_DEPTH on args returns 1 instead
3911 of 2. This will make tsubst wrongly consider that T and U
3912 have level 1. Instead, let's create a dummy vector as the
3913 first element of full_template_args so that TMPL_ARGS_DEPTH
3914 returns the correct depth for args. */
3915 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3916 return args;
3917 }
3918
3919 /* Within the declaration of a template, return the currently active
3920 template parameters as an argument TREE_VEC. */
3921
3922 static tree
3923 current_template_args (void)
3924 {
3925 return template_parms_to_args (current_template_parms);
3926 }
3927
3928 /* Update the declared TYPE by doing any lookups which were thought to be
3929 dependent, but are not now that we know the SCOPE of the declarator. */
3930
3931 tree
3932 maybe_update_decl_type (tree orig_type, tree scope)
3933 {
3934 tree type = orig_type;
3935
3936 if (type == NULL_TREE)
3937 return type;
3938
3939 if (TREE_CODE (orig_type) == TYPE_DECL)
3940 type = TREE_TYPE (type);
3941
3942 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3943 && dependent_type_p (type)
3944 /* Don't bother building up the args in this case. */
3945 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3946 {
3947 /* tsubst in the args corresponding to the template parameters,
3948 including auto if present. Most things will be unchanged, but
3949 make_typename_type and tsubst_qualified_id will resolve
3950 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3951 tree args = current_template_args ();
3952 tree auto_node = type_uses_auto (type);
3953 tree pushed;
3954 if (auto_node)
3955 {
3956 tree auto_vec = make_tree_vec (1);
3957 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3958 args = add_to_template_args (args, auto_vec);
3959 }
3960 pushed = push_scope (scope);
3961 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3962 if (pushed)
3963 pop_scope (scope);
3964 }
3965
3966 if (type == error_mark_node)
3967 return orig_type;
3968
3969 if (TREE_CODE (orig_type) == TYPE_DECL)
3970 {
3971 if (same_type_p (type, TREE_TYPE (orig_type)))
3972 type = orig_type;
3973 else
3974 type = TYPE_NAME (type);
3975 }
3976 return type;
3977 }
3978
3979 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3980 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3981 a member template. Used by push_template_decl below. */
3982
3983 static tree
3984 build_template_decl (tree decl, tree parms, bool member_template_p)
3985 {
3986 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3987 DECL_TEMPLATE_PARMS (tmpl) = parms;
3988 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3989 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3990 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3991
3992 return tmpl;
3993 }
3994
3995 struct template_parm_data
3996 {
3997 /* The level of the template parameters we are currently
3998 processing. */
3999 int level;
4000
4001 /* The index of the specialization argument we are currently
4002 processing. */
4003 int current_arg;
4004
4005 /* An array whose size is the number of template parameters. The
4006 elements are nonzero if the parameter has been used in any one
4007 of the arguments processed so far. */
4008 int* parms;
4009
4010 /* An array whose size is the number of template arguments. The
4011 elements are nonzero if the argument makes use of template
4012 parameters of this level. */
4013 int* arg_uses_template_parms;
4014 };
4015
4016 /* Subroutine of push_template_decl used to see if each template
4017 parameter in a partial specialization is used in the explicit
4018 argument list. If T is of the LEVEL given in DATA (which is
4019 treated as a template_parm_data*), then DATA->PARMS is marked
4020 appropriately. */
4021
4022 static int
4023 mark_template_parm (tree t, void* data)
4024 {
4025 int level;
4026 int idx;
4027 struct template_parm_data* tpd = (struct template_parm_data*) data;
4028
4029 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4030 {
4031 level = TEMPLATE_PARM_LEVEL (t);
4032 idx = TEMPLATE_PARM_IDX (t);
4033 }
4034 else
4035 {
4036 level = TEMPLATE_TYPE_LEVEL (t);
4037 idx = TEMPLATE_TYPE_IDX (t);
4038 }
4039
4040 if (level == tpd->level)
4041 {
4042 tpd->parms[idx] = 1;
4043 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4044 }
4045
4046 /* Return zero so that for_each_template_parm will continue the
4047 traversal of the tree; we want to mark *every* template parm. */
4048 return 0;
4049 }
4050
4051 /* Process the partial specialization DECL. */
4052
4053 static tree
4054 process_partial_specialization (tree decl)
4055 {
4056 tree type = TREE_TYPE (decl);
4057 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4058 tree specargs = CLASSTYPE_TI_ARGS (type);
4059 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4060 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4061 tree inner_parms;
4062 tree inst;
4063 int nargs = TREE_VEC_LENGTH (inner_args);
4064 int ntparms;
4065 int i;
4066 bool did_error_intro = false;
4067 struct template_parm_data tpd;
4068 struct template_parm_data tpd2;
4069
4070 gcc_assert (current_template_parms);
4071
4072 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4073 ntparms = TREE_VEC_LENGTH (inner_parms);
4074
4075 /* We check that each of the template parameters given in the
4076 partial specialization is used in the argument list to the
4077 specialization. For example:
4078
4079 template <class T> struct S;
4080 template <class T> struct S<T*>;
4081
4082 The second declaration is OK because `T*' uses the template
4083 parameter T, whereas
4084
4085 template <class T> struct S<int>;
4086
4087 is no good. Even trickier is:
4088
4089 template <class T>
4090 struct S1
4091 {
4092 template <class U>
4093 struct S2;
4094 template <class U>
4095 struct S2<T>;
4096 };
4097
4098 The S2<T> declaration is actually invalid; it is a
4099 full-specialization. Of course,
4100
4101 template <class U>
4102 struct S2<T (*)(U)>;
4103
4104 or some such would have been OK. */
4105 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4106 tpd.parms = XALLOCAVEC (int, ntparms);
4107 memset (tpd.parms, 0, sizeof (int) * ntparms);
4108
4109 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4110 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4111 for (i = 0; i < nargs; ++i)
4112 {
4113 tpd.current_arg = i;
4114 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4115 &mark_template_parm,
4116 &tpd,
4117 NULL,
4118 /*include_nondeduced_p=*/false);
4119 }
4120 for (i = 0; i < ntparms; ++i)
4121 if (tpd.parms[i] == 0)
4122 {
4123 /* One of the template parms was not used in the
4124 specialization. */
4125 if (!did_error_intro)
4126 {
4127 error ("template parameters not used in partial specialization:");
4128 did_error_intro = true;
4129 }
4130
4131 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4132 }
4133
4134 if (did_error_intro)
4135 return error_mark_node;
4136
4137 /* [temp.class.spec]
4138
4139 The argument list of the specialization shall not be identical to
4140 the implicit argument list of the primary template. */
4141 if (comp_template_args
4142 (inner_args,
4143 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4144 (maintmpl)))))
4145 error ("partial specialization %qT does not specialize any template arguments", type);
4146
4147 /* A partial specialization that replaces multiple parameters of the
4148 primary template with a pack expansion is less specialized for those
4149 parameters. */
4150 if (nargs < DECL_NTPARMS (maintmpl))
4151 {
4152 error ("partial specialization is not more specialized than the "
4153 "primary template because it replaces multiple parameters "
4154 "with a pack expansion");
4155 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4156 return decl;
4157 }
4158
4159 /* [temp.class.spec]
4160
4161 A partially specialized non-type argument expression shall not
4162 involve template parameters of the partial specialization except
4163 when the argument expression is a simple identifier.
4164
4165 The type of a template parameter corresponding to a specialized
4166 non-type argument shall not be dependent on a parameter of the
4167 specialization.
4168
4169 Also, we verify that pack expansions only occur at the
4170 end of the argument list. */
4171 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4172 tpd2.parms = 0;
4173 for (i = 0; i < nargs; ++i)
4174 {
4175 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4176 tree arg = TREE_VEC_ELT (inner_args, i);
4177 tree packed_args = NULL_TREE;
4178 int j, len = 1;
4179
4180 if (ARGUMENT_PACK_P (arg))
4181 {
4182 /* Extract the arguments from the argument pack. We'll be
4183 iterating over these in the following loop. */
4184 packed_args = ARGUMENT_PACK_ARGS (arg);
4185 len = TREE_VEC_LENGTH (packed_args);
4186 }
4187
4188 for (j = 0; j < len; j++)
4189 {
4190 if (packed_args)
4191 /* Get the Jth argument in the parameter pack. */
4192 arg = TREE_VEC_ELT (packed_args, j);
4193
4194 if (PACK_EXPANSION_P (arg))
4195 {
4196 /* Pack expansions must come at the end of the
4197 argument list. */
4198 if ((packed_args && j < len - 1)
4199 || (!packed_args && i < nargs - 1))
4200 {
4201 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4202 error ("parameter pack argument %qE must be at the "
4203 "end of the template argument list", arg);
4204 else
4205 error ("parameter pack argument %qT must be at the "
4206 "end of the template argument list", arg);
4207 }
4208 }
4209
4210 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4211 /* We only care about the pattern. */
4212 arg = PACK_EXPANSION_PATTERN (arg);
4213
4214 if (/* These first two lines are the `non-type' bit. */
4215 !TYPE_P (arg)
4216 && TREE_CODE (arg) != TEMPLATE_DECL
4217 /* This next line is the `argument expression is not just a
4218 simple identifier' condition and also the `specialized
4219 non-type argument' bit. */
4220 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4221 {
4222 if ((!packed_args && tpd.arg_uses_template_parms[i])
4223 || (packed_args && uses_template_parms (arg)))
4224 error ("template argument %qE involves template parameter(s)",
4225 arg);
4226 else
4227 {
4228 /* Look at the corresponding template parameter,
4229 marking which template parameters its type depends
4230 upon. */
4231 tree type = TREE_TYPE (parm);
4232
4233 if (!tpd2.parms)
4234 {
4235 /* We haven't yet initialized TPD2. Do so now. */
4236 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4237 /* The number of parameters here is the number in the
4238 main template, which, as checked in the assertion
4239 above, is NARGS. */
4240 tpd2.parms = XALLOCAVEC (int, nargs);
4241 tpd2.level =
4242 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4243 }
4244
4245 /* Mark the template parameters. But this time, we're
4246 looking for the template parameters of the main
4247 template, not in the specialization. */
4248 tpd2.current_arg = i;
4249 tpd2.arg_uses_template_parms[i] = 0;
4250 memset (tpd2.parms, 0, sizeof (int) * nargs);
4251 for_each_template_parm (type,
4252 &mark_template_parm,
4253 &tpd2,
4254 NULL,
4255 /*include_nondeduced_p=*/false);
4256
4257 if (tpd2.arg_uses_template_parms [i])
4258 {
4259 /* The type depended on some template parameters.
4260 If they are fully specialized in the
4261 specialization, that's OK. */
4262 int j;
4263 int count = 0;
4264 for (j = 0; j < nargs; ++j)
4265 if (tpd2.parms[j] != 0
4266 && tpd.arg_uses_template_parms [j])
4267 ++count;
4268 if (count != 0)
4269 error_n (input_location, count,
4270 "type %qT of template argument %qE depends "
4271 "on a template parameter",
4272 "type %qT of template argument %qE depends "
4273 "on template parameters",
4274 type,
4275 arg);
4276 }
4277 }
4278 }
4279 }
4280 }
4281
4282 /* We should only get here once. */
4283 gcc_assert (!COMPLETE_TYPE_P (type));
4284
4285 tree tmpl = build_template_decl (decl, current_template_parms,
4286 DECL_MEMBER_TEMPLATE_P (maintmpl));
4287 TREE_TYPE (tmpl) = type;
4288 DECL_TEMPLATE_RESULT (tmpl) = decl;
4289 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4290 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4291 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4292
4293 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4294 = tree_cons (specargs, tmpl,
4295 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4296 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4297
4298 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4299 inst = TREE_CHAIN (inst))
4300 {
4301 tree inst_type = TREE_VALUE (inst);
4302 if (COMPLETE_TYPE_P (inst_type)
4303 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4304 {
4305 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4306 if (spec && TREE_TYPE (spec) == type)
4307 permerror (input_location,
4308 "partial specialization of %qT after instantiation "
4309 "of %qT", type, inst_type);
4310 }
4311 }
4312
4313 return decl;
4314 }
4315
4316 /* Check that a template declaration's use of default arguments and
4317 parameter packs is not invalid. Here, PARMS are the template
4318 parameters. IS_PRIMARY is true if DECL is the thing declared by
4319 a primary template. IS_PARTIAL is true if DECL is a partial
4320 specialization.
4321
4322 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4323 declaration (but not a definition); 1 indicates a declaration, 2
4324 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4325 emitted for extraneous default arguments.
4326
4327 Returns TRUE if there were no errors found, FALSE otherwise. */
4328
4329 bool
4330 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4331 bool is_partial, int is_friend_decl)
4332 {
4333 const char *msg;
4334 int last_level_to_check;
4335 tree parm_level;
4336 bool no_errors = true;
4337
4338 /* [temp.param]
4339
4340 A default template-argument shall not be specified in a
4341 function template declaration or a function template definition, nor
4342 in the template-parameter-list of the definition of a member of a
4343 class template. */
4344
4345 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4346 /* You can't have a function template declaration in a local
4347 scope, nor you can you define a member of a class template in a
4348 local scope. */
4349 return true;
4350
4351 if (TREE_CODE (decl) == TYPE_DECL
4352 && TREE_TYPE (decl)
4353 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4354 /* A lambda doesn't have an explicit declaration; don't complain
4355 about the parms of the enclosing class. */
4356 return true;
4357
4358 if (current_class_type
4359 && !TYPE_BEING_DEFINED (current_class_type)
4360 && DECL_LANG_SPECIFIC (decl)
4361 && DECL_DECLARES_FUNCTION_P (decl)
4362 /* If this is either a friend defined in the scope of the class
4363 or a member function. */
4364 && (DECL_FUNCTION_MEMBER_P (decl)
4365 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4366 : DECL_FRIEND_CONTEXT (decl)
4367 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4368 : false)
4369 /* And, if it was a member function, it really was defined in
4370 the scope of the class. */
4371 && (!DECL_FUNCTION_MEMBER_P (decl)
4372 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4373 /* We already checked these parameters when the template was
4374 declared, so there's no need to do it again now. This function
4375 was defined in class scope, but we're processing its body now
4376 that the class is complete. */
4377 return true;
4378
4379 /* Core issue 226 (C++0x only): the following only applies to class
4380 templates. */
4381 if (is_primary
4382 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4383 {
4384 /* [temp.param]
4385
4386 If a template-parameter has a default template-argument, all
4387 subsequent template-parameters shall have a default
4388 template-argument supplied. */
4389 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4390 {
4391 tree inner_parms = TREE_VALUE (parm_level);
4392 int ntparms = TREE_VEC_LENGTH (inner_parms);
4393 int seen_def_arg_p = 0;
4394 int i;
4395
4396 for (i = 0; i < ntparms; ++i)
4397 {
4398 tree parm = TREE_VEC_ELT (inner_parms, i);
4399
4400 if (parm == error_mark_node)
4401 continue;
4402
4403 if (TREE_PURPOSE (parm))
4404 seen_def_arg_p = 1;
4405 else if (seen_def_arg_p
4406 && !template_parameter_pack_p (TREE_VALUE (parm)))
4407 {
4408 error ("no default argument for %qD", TREE_VALUE (parm));
4409 /* For better subsequent error-recovery, we indicate that
4410 there should have been a default argument. */
4411 TREE_PURPOSE (parm) = error_mark_node;
4412 no_errors = false;
4413 }
4414 else if (!is_partial
4415 && !is_friend_decl
4416 /* Don't complain about an enclosing partial
4417 specialization. */
4418 && parm_level == parms
4419 && TREE_CODE (decl) == TYPE_DECL
4420 && i < ntparms - 1
4421 && template_parameter_pack_p (TREE_VALUE (parm)))
4422 {
4423 /* A primary class template can only have one
4424 parameter pack, at the end of the template
4425 parameter list. */
4426
4427 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4428 error ("parameter pack %qE must be at the end of the"
4429 " template parameter list", TREE_VALUE (parm));
4430 else
4431 error ("parameter pack %qT must be at the end of the"
4432 " template parameter list",
4433 TREE_TYPE (TREE_VALUE (parm)));
4434
4435 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4436 = error_mark_node;
4437 no_errors = false;
4438 }
4439 }
4440 }
4441 }
4442
4443 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4444 || is_partial
4445 || !is_primary
4446 || is_friend_decl)
4447 /* For an ordinary class template, default template arguments are
4448 allowed at the innermost level, e.g.:
4449 template <class T = int>
4450 struct S {};
4451 but, in a partial specialization, they're not allowed even
4452 there, as we have in [temp.class.spec]:
4453
4454 The template parameter list of a specialization shall not
4455 contain default template argument values.
4456
4457 So, for a partial specialization, or for a function template
4458 (in C++98/C++03), we look at all of them. */
4459 ;
4460 else
4461 /* But, for a primary class template that is not a partial
4462 specialization we look at all template parameters except the
4463 innermost ones. */
4464 parms = TREE_CHAIN (parms);
4465
4466 /* Figure out what error message to issue. */
4467 if (is_friend_decl == 2)
4468 msg = G_("default template arguments may not be used in function template "
4469 "friend re-declaration");
4470 else if (is_friend_decl)
4471 msg = G_("default template arguments may not be used in function template "
4472 "friend declarations");
4473 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4474 msg = G_("default template arguments may not be used in function templates "
4475 "without -std=c++11 or -std=gnu++11");
4476 else if (is_partial)
4477 msg = G_("default template arguments may not be used in "
4478 "partial specializations");
4479 else
4480 msg = G_("default argument for template parameter for class enclosing %qD");
4481
4482 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4483 /* If we're inside a class definition, there's no need to
4484 examine the parameters to the class itself. On the one
4485 hand, they will be checked when the class is defined, and,
4486 on the other, default arguments are valid in things like:
4487 template <class T = double>
4488 struct S { template <class U> void f(U); };
4489 Here the default argument for `S' has no bearing on the
4490 declaration of `f'. */
4491 last_level_to_check = template_class_depth (current_class_type) + 1;
4492 else
4493 /* Check everything. */
4494 last_level_to_check = 0;
4495
4496 for (parm_level = parms;
4497 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4498 parm_level = TREE_CHAIN (parm_level))
4499 {
4500 tree inner_parms = TREE_VALUE (parm_level);
4501 int i;
4502 int ntparms;
4503
4504 ntparms = TREE_VEC_LENGTH (inner_parms);
4505 for (i = 0; i < ntparms; ++i)
4506 {
4507 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4508 continue;
4509
4510 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4511 {
4512 if (msg)
4513 {
4514 no_errors = false;
4515 if (is_friend_decl == 2)
4516 return no_errors;
4517
4518 error (msg, decl);
4519 msg = 0;
4520 }
4521
4522 /* Clear out the default argument so that we are not
4523 confused later. */
4524 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4525 }
4526 }
4527
4528 /* At this point, if we're still interested in issuing messages,
4529 they must apply to classes surrounding the object declared. */
4530 if (msg)
4531 msg = G_("default argument for template parameter for class "
4532 "enclosing %qD");
4533 }
4534
4535 return no_errors;
4536 }
4537
4538 /* Worker for push_template_decl_real, called via
4539 for_each_template_parm. DATA is really an int, indicating the
4540 level of the parameters we are interested in. If T is a template
4541 parameter of that level, return nonzero. */
4542
4543 static int
4544 template_parm_this_level_p (tree t, void* data)
4545 {
4546 int this_level = *(int *)data;
4547 int level;
4548
4549 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4550 level = TEMPLATE_PARM_LEVEL (t);
4551 else
4552 level = TEMPLATE_TYPE_LEVEL (t);
4553 return level == this_level;
4554 }
4555
4556 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4557 parameters given by current_template_args, or reuses a
4558 previously existing one, if appropriate. Returns the DECL, or an
4559 equivalent one, if it is replaced via a call to duplicate_decls.
4560
4561 If IS_FRIEND is true, DECL is a friend declaration. */
4562
4563 tree
4564 push_template_decl_real (tree decl, bool is_friend)
4565 {
4566 tree tmpl;
4567 tree args;
4568 tree info;
4569 tree ctx;
4570 bool is_primary;
4571 bool is_partial;
4572 int new_template_p = 0;
4573 /* True if the template is a member template, in the sense of
4574 [temp.mem]. */
4575 bool member_template_p = false;
4576
4577 if (decl == error_mark_node || !current_template_parms)
4578 return error_mark_node;
4579
4580 /* See if this is a partial specialization. */
4581 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4582 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4583 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4584
4585 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4586 is_friend = true;
4587
4588 if (is_friend)
4589 /* For a friend, we want the context of the friend function, not
4590 the type of which it is a friend. */
4591 ctx = CP_DECL_CONTEXT (decl);
4592 else if (CP_DECL_CONTEXT (decl)
4593 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4594 /* In the case of a virtual function, we want the class in which
4595 it is defined. */
4596 ctx = CP_DECL_CONTEXT (decl);
4597 else
4598 /* Otherwise, if we're currently defining some class, the DECL
4599 is assumed to be a member of the class. */
4600 ctx = current_scope ();
4601
4602 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4603 ctx = NULL_TREE;
4604
4605 if (!DECL_CONTEXT (decl))
4606 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4607
4608 /* See if this is a primary template. */
4609 if (is_friend && ctx)
4610 /* A friend template that specifies a class context, i.e.
4611 template <typename T> friend void A<T>::f();
4612 is not primary. */
4613 is_primary = false;
4614 else
4615 is_primary = template_parm_scope_p ();
4616
4617 if (is_primary)
4618 {
4619 if (DECL_CLASS_SCOPE_P (decl))
4620 member_template_p = true;
4621 if (TREE_CODE (decl) == TYPE_DECL
4622 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4623 {
4624 error ("template class without a name");
4625 return error_mark_node;
4626 }
4627 else if (TREE_CODE (decl) == FUNCTION_DECL)
4628 {
4629 if (DECL_DESTRUCTOR_P (decl))
4630 {
4631 /* [temp.mem]
4632
4633 A destructor shall not be a member template. */
4634 error ("destructor %qD declared as member template", decl);
4635 return error_mark_node;
4636 }
4637 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4638 && (!prototype_p (TREE_TYPE (decl))
4639 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4640 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4641 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4642 == void_list_node)))
4643 {
4644 /* [basic.stc.dynamic.allocation]
4645
4646 An allocation function can be a function
4647 template. ... Template allocation functions shall
4648 have two or more parameters. */
4649 error ("invalid template declaration of %qD", decl);
4650 return error_mark_node;
4651 }
4652 }
4653 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4654 && CLASS_TYPE_P (TREE_TYPE (decl)))
4655 /* OK */;
4656 else if (TREE_CODE (decl) == TYPE_DECL
4657 && TYPE_DECL_ALIAS_P (decl))
4658 /* alias-declaration */
4659 gcc_assert (!DECL_ARTIFICIAL (decl));
4660 else
4661 {
4662 error ("template declaration of %q#D", decl);
4663 return error_mark_node;
4664 }
4665 }
4666
4667 /* Check to see that the rules regarding the use of default
4668 arguments are not being violated. */
4669 check_default_tmpl_args (decl, current_template_parms,
4670 is_primary, is_partial, /*is_friend_decl=*/0);
4671
4672 /* Ensure that there are no parameter packs in the type of this
4673 declaration that have not been expanded. */
4674 if (TREE_CODE (decl) == FUNCTION_DECL)
4675 {
4676 /* Check each of the arguments individually to see if there are
4677 any bare parameter packs. */
4678 tree type = TREE_TYPE (decl);
4679 tree arg = DECL_ARGUMENTS (decl);
4680 tree argtype = TYPE_ARG_TYPES (type);
4681
4682 while (arg && argtype)
4683 {
4684 if (!DECL_PACK_P (arg)
4685 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4686 {
4687 /* This is a PARM_DECL that contains unexpanded parameter
4688 packs. We have already complained about this in the
4689 check_for_bare_parameter_packs call, so just replace
4690 these types with ERROR_MARK_NODE. */
4691 TREE_TYPE (arg) = error_mark_node;
4692 TREE_VALUE (argtype) = error_mark_node;
4693 }
4694
4695 arg = DECL_CHAIN (arg);
4696 argtype = TREE_CHAIN (argtype);
4697 }
4698
4699 /* Check for bare parameter packs in the return type and the
4700 exception specifiers. */
4701 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4702 /* Errors were already issued, set return type to int
4703 as the frontend doesn't expect error_mark_node as
4704 the return type. */
4705 TREE_TYPE (type) = integer_type_node;
4706 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4707 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4708 }
4709 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4710 && TYPE_DECL_ALIAS_P (decl))
4711 ? DECL_ORIGINAL_TYPE (decl)
4712 : TREE_TYPE (decl)))
4713 {
4714 TREE_TYPE (decl) = error_mark_node;
4715 return error_mark_node;
4716 }
4717
4718 if (is_partial)
4719 return process_partial_specialization (decl);
4720
4721 args = current_template_args ();
4722
4723 if (!ctx
4724 || TREE_CODE (ctx) == FUNCTION_DECL
4725 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4726 || (TREE_CODE (decl) == TYPE_DECL
4727 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4728 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4729 {
4730 if (DECL_LANG_SPECIFIC (decl)
4731 && DECL_TEMPLATE_INFO (decl)
4732 && DECL_TI_TEMPLATE (decl))
4733 tmpl = DECL_TI_TEMPLATE (decl);
4734 /* If DECL is a TYPE_DECL for a class-template, then there won't
4735 be DECL_LANG_SPECIFIC. The information equivalent to
4736 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4737 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4738 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4739 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4740 {
4741 /* Since a template declaration already existed for this
4742 class-type, we must be redeclaring it here. Make sure
4743 that the redeclaration is valid. */
4744 redeclare_class_template (TREE_TYPE (decl),
4745 current_template_parms);
4746 /* We don't need to create a new TEMPLATE_DECL; just use the
4747 one we already had. */
4748 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4749 }
4750 else
4751 {
4752 tmpl = build_template_decl (decl, current_template_parms,
4753 member_template_p);
4754 new_template_p = 1;
4755
4756 if (DECL_LANG_SPECIFIC (decl)
4757 && DECL_TEMPLATE_SPECIALIZATION (decl))
4758 {
4759 /* A specialization of a member template of a template
4760 class. */
4761 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4762 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4763 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4764 }
4765 }
4766 }
4767 else
4768 {
4769 tree a, t, current, parms;
4770 int i;
4771 tree tinfo = get_template_info (decl);
4772
4773 if (!tinfo)
4774 {
4775 error ("template definition of non-template %q#D", decl);
4776 return error_mark_node;
4777 }
4778
4779 tmpl = TI_TEMPLATE (tinfo);
4780
4781 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4782 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4783 && DECL_TEMPLATE_SPECIALIZATION (decl)
4784 && DECL_MEMBER_TEMPLATE_P (tmpl))
4785 {
4786 tree new_tmpl;
4787
4788 /* The declaration is a specialization of a member
4789 template, declared outside the class. Therefore, the
4790 innermost template arguments will be NULL, so we
4791 replace them with the arguments determined by the
4792 earlier call to check_explicit_specialization. */
4793 args = DECL_TI_ARGS (decl);
4794
4795 new_tmpl
4796 = build_template_decl (decl, current_template_parms,
4797 member_template_p);
4798 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4799 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4800 DECL_TI_TEMPLATE (decl) = new_tmpl;
4801 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4802 DECL_TEMPLATE_INFO (new_tmpl)
4803 = build_template_info (tmpl, args);
4804
4805 register_specialization (new_tmpl,
4806 most_general_template (tmpl),
4807 args,
4808 is_friend, 0);
4809 return decl;
4810 }
4811
4812 /* Make sure the template headers we got make sense. */
4813
4814 parms = DECL_TEMPLATE_PARMS (tmpl);
4815 i = TMPL_PARMS_DEPTH (parms);
4816 if (TMPL_ARGS_DEPTH (args) != i)
4817 {
4818 error ("expected %d levels of template parms for %q#D, got %d",
4819 i, decl, TMPL_ARGS_DEPTH (args));
4820 }
4821 else
4822 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4823 {
4824 a = TMPL_ARGS_LEVEL (args, i);
4825 t = INNERMOST_TEMPLATE_PARMS (parms);
4826
4827 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4828 {
4829 if (current == decl)
4830 error ("got %d template parameters for %q#D",
4831 TREE_VEC_LENGTH (a), decl);
4832 else
4833 error ("got %d template parameters for %q#T",
4834 TREE_VEC_LENGTH (a), current);
4835 error (" but %d required", TREE_VEC_LENGTH (t));
4836 /* Avoid crash in import_export_decl. */
4837 DECL_INTERFACE_KNOWN (decl) = 1;
4838 return error_mark_node;
4839 }
4840
4841 if (current == decl)
4842 current = ctx;
4843 else if (current == NULL_TREE)
4844 /* Can happen in erroneous input. */
4845 break;
4846 else
4847 current = get_containing_scope (current);
4848 }
4849
4850 /* Check that the parms are used in the appropriate qualifying scopes
4851 in the declarator. */
4852 if (!comp_template_args
4853 (TI_ARGS (tinfo),
4854 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4855 {
4856 error ("\
4857 template arguments to %qD do not match original template %qD",
4858 decl, DECL_TEMPLATE_RESULT (tmpl));
4859 if (!uses_template_parms (TI_ARGS (tinfo)))
4860 inform (input_location, "use template<> for an explicit specialization");
4861 /* Avoid crash in import_export_decl. */
4862 DECL_INTERFACE_KNOWN (decl) = 1;
4863 return error_mark_node;
4864 }
4865 }
4866
4867 DECL_TEMPLATE_RESULT (tmpl) = decl;
4868 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4869
4870 /* Push template declarations for global functions and types. Note
4871 that we do not try to push a global template friend declared in a
4872 template class; such a thing may well depend on the template
4873 parameters of the class. */
4874 if (new_template_p && !ctx
4875 && !(is_friend && template_class_depth (current_class_type) > 0))
4876 {
4877 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4878 if (tmpl == error_mark_node)
4879 return error_mark_node;
4880
4881 /* Hide template friend classes that haven't been declared yet. */
4882 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4883 {
4884 DECL_ANTICIPATED (tmpl) = 1;
4885 DECL_FRIEND_P (tmpl) = 1;
4886 }
4887 }
4888
4889 if (is_primary)
4890 {
4891 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4892 int i;
4893
4894 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4895 if (DECL_CONV_FN_P (tmpl))
4896 {
4897 int depth = TMPL_PARMS_DEPTH (parms);
4898
4899 /* It is a conversion operator. See if the type converted to
4900 depends on innermost template operands. */
4901
4902 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4903 depth))
4904 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4905 }
4906
4907 /* Give template template parms a DECL_CONTEXT of the template
4908 for which they are a parameter. */
4909 parms = INNERMOST_TEMPLATE_PARMS (parms);
4910 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4911 {
4912 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4913 if (TREE_CODE (parm) == TEMPLATE_DECL)
4914 DECL_CONTEXT (parm) = tmpl;
4915 }
4916 }
4917
4918 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4919 back to its most general template. If TMPL is a specialization,
4920 ARGS may only have the innermost set of arguments. Add the missing
4921 argument levels if necessary. */
4922 if (DECL_TEMPLATE_INFO (tmpl))
4923 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4924
4925 info = build_template_info (tmpl, args);
4926
4927 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4928 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4929 else
4930 {
4931 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4932 retrofit_lang_decl (decl);
4933 if (DECL_LANG_SPECIFIC (decl))
4934 DECL_TEMPLATE_INFO (decl) = info;
4935 }
4936
4937 return DECL_TEMPLATE_RESULT (tmpl);
4938 }
4939
4940 tree
4941 push_template_decl (tree decl)
4942 {
4943 return push_template_decl_real (decl, false);
4944 }
4945
4946 /* FN is an inheriting constructor that inherits from the constructor
4947 template INHERITED; turn FN into a constructor template with a matching
4948 template header. */
4949
4950 tree
4951 add_inherited_template_parms (tree fn, tree inherited)
4952 {
4953 tree inner_parms
4954 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4955 inner_parms = copy_node (inner_parms);
4956 tree parms
4957 = tree_cons (size_int (processing_template_decl + 1),
4958 inner_parms, current_template_parms);
4959 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4960 tree args = template_parms_to_args (parms);
4961 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4962 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4963 DECL_TEMPLATE_RESULT (tmpl) = fn;
4964 DECL_ARTIFICIAL (tmpl) = true;
4965 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4966 return tmpl;
4967 }
4968
4969 /* Called when a class template TYPE is redeclared with the indicated
4970 template PARMS, e.g.:
4971
4972 template <class T> struct S;
4973 template <class T> struct S {}; */
4974
4975 bool
4976 redeclare_class_template (tree type, tree parms)
4977 {
4978 tree tmpl;
4979 tree tmpl_parms;
4980 int i;
4981
4982 if (!TYPE_TEMPLATE_INFO (type))
4983 {
4984 error ("%qT is not a template type", type);
4985 return false;
4986 }
4987
4988 tmpl = TYPE_TI_TEMPLATE (type);
4989 if (!PRIMARY_TEMPLATE_P (tmpl))
4990 /* The type is nested in some template class. Nothing to worry
4991 about here; there are no new template parameters for the nested
4992 type. */
4993 return true;
4994
4995 if (!parms)
4996 {
4997 error ("template specifiers not specified in declaration of %qD",
4998 tmpl);
4999 return false;
5000 }
5001
5002 parms = INNERMOST_TEMPLATE_PARMS (parms);
5003 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5004
5005 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5006 {
5007 error_n (input_location, TREE_VEC_LENGTH (parms),
5008 "redeclared with %d template parameter",
5009 "redeclared with %d template parameters",
5010 TREE_VEC_LENGTH (parms));
5011 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5012 "previous declaration %q+D used %d template parameter",
5013 "previous declaration %q+D used %d template parameters",
5014 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5015 return false;
5016 }
5017
5018 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5019 {
5020 tree tmpl_parm;
5021 tree parm;
5022 tree tmpl_default;
5023 tree parm_default;
5024
5025 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5026 || TREE_VEC_ELT (parms, i) == error_mark_node)
5027 continue;
5028
5029 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5030 if (tmpl_parm == error_mark_node)
5031 return false;
5032
5033 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5034 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5035 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5036
5037 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5038 TEMPLATE_DECL. */
5039 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5040 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5041 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5042 || (TREE_CODE (tmpl_parm) != PARM_DECL
5043 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5044 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5045 || (TREE_CODE (tmpl_parm) == PARM_DECL
5046 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5047 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5048 {
5049 error ("template parameter %q+#D", tmpl_parm);
5050 error ("redeclared here as %q#D", parm);
5051 return false;
5052 }
5053
5054 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5055 {
5056 /* We have in [temp.param]:
5057
5058 A template-parameter may not be given default arguments
5059 by two different declarations in the same scope. */
5060 error_at (input_location, "redefinition of default argument for %q#D", parm);
5061 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5062 "original definition appeared here");
5063 return false;
5064 }
5065
5066 if (parm_default != NULL_TREE)
5067 /* Update the previous template parameters (which are the ones
5068 that will really count) with the new default value. */
5069 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5070 else if (tmpl_default != NULL_TREE)
5071 /* Update the new parameters, too; they'll be used as the
5072 parameters for any members. */
5073 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5074 }
5075
5076 return true;
5077 }
5078
5079 /* Simplify EXPR if it is a non-dependent expression. Returns the
5080 (possibly simplified) expression. */
5081
5082 tree
5083 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5084 {
5085 if (expr == NULL_TREE)
5086 return NULL_TREE;
5087
5088 /* If we're in a template, but EXPR isn't value dependent, simplify
5089 it. We're supposed to treat:
5090
5091 template <typename T> void f(T[1 + 1]);
5092 template <typename T> void f(T[2]);
5093
5094 as two declarations of the same function, for example. */
5095 if (processing_template_decl
5096 && !instantiation_dependent_expression_p (expr)
5097 && potential_constant_expression (expr))
5098 {
5099 HOST_WIDE_INT saved_processing_template_decl;
5100
5101 saved_processing_template_decl = processing_template_decl;
5102 processing_template_decl = 0;
5103 expr = tsubst_copy_and_build (expr,
5104 /*args=*/NULL_TREE,
5105 complain,
5106 /*in_decl=*/NULL_TREE,
5107 /*function_p=*/false,
5108 /*integral_constant_expression_p=*/true);
5109 processing_template_decl = saved_processing_template_decl;
5110 }
5111 return expr;
5112 }
5113
5114 tree
5115 fold_non_dependent_expr (tree expr)
5116 {
5117 return fold_non_dependent_expr_sfinae (expr, tf_error);
5118 }
5119
5120 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5121 template declaration, or a TYPE_DECL for an alias declaration. */
5122
5123 bool
5124 alias_type_or_template_p (tree t)
5125 {
5126 if (t == NULL_TREE)
5127 return false;
5128 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5129 || (TYPE_P (t)
5130 && TYPE_NAME (t)
5131 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5132 || DECL_ALIAS_TEMPLATE_P (t));
5133 }
5134
5135 /* Return TRUE iff is a specialization of an alias template. */
5136
5137 bool
5138 alias_template_specialization_p (const_tree t)
5139 {
5140 if (t == NULL_TREE)
5141 return false;
5142
5143 return (TYPE_P (t)
5144 && TYPE_TEMPLATE_INFO (t)
5145 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5146 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5147 }
5148
5149 /* Return either TMPL or another template that it is equivalent to under DR
5150 1286: An alias that just changes the name of a template is equivalent to
5151 the other template. */
5152
5153 static tree
5154 get_underlying_template (tree tmpl)
5155 {
5156 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5157 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5158 {
5159 tree result = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5160 if (TYPE_TEMPLATE_INFO (result))
5161 {
5162 tree sub = TYPE_TI_TEMPLATE (result);
5163 if (PRIMARY_TEMPLATE_P (sub)
5164 && same_type_p (result, TREE_TYPE (sub)))
5165 {
5166 /* The alias type is equivalent to the pattern of the
5167 underlying template, so strip the alias. */
5168 tmpl = sub;
5169 continue;
5170 }
5171 }
5172 break;
5173 }
5174 return tmpl;
5175 }
5176
5177 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5178 must be a function or a pointer-to-function type, as specified
5179 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5180 and check that the resulting function has external linkage. */
5181
5182 static tree
5183 convert_nontype_argument_function (tree type, tree expr)
5184 {
5185 tree fns = expr;
5186 tree fn, fn_no_ptr;
5187 linkage_kind linkage;
5188
5189 fn = instantiate_type (type, fns, tf_none);
5190 if (fn == error_mark_node)
5191 return error_mark_node;
5192
5193 fn_no_ptr = fn;
5194 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5195 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5196 if (BASELINK_P (fn_no_ptr))
5197 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5198
5199 /* [temp.arg.nontype]/1
5200
5201 A template-argument for a non-type, non-template template-parameter
5202 shall be one of:
5203 [...]
5204 -- the address of an object or function with external [C++11: or
5205 internal] linkage. */
5206
5207 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5208 {
5209 error ("%qE is not a valid template argument for type %qT", expr, type);
5210 if (TYPE_PTR_P (type))
5211 error ("it must be the address of a function with external linkage");
5212 else
5213 error ("it must be the name of a function with external linkage");
5214 return NULL_TREE;
5215 }
5216
5217 linkage = decl_linkage (fn_no_ptr);
5218 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5219 {
5220 if (cxx_dialect >= cxx11)
5221 error ("%qE is not a valid template argument for type %qT "
5222 "because %qD has no linkage",
5223 expr, type, fn_no_ptr);
5224 else
5225 error ("%qE is not a valid template argument for type %qT "
5226 "because %qD does not have external linkage",
5227 expr, type, fn_no_ptr);
5228 return NULL_TREE;
5229 }
5230
5231 return fn;
5232 }
5233
5234 /* Subroutine of convert_nontype_argument.
5235 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5236 Emit an error otherwise. */
5237
5238 static bool
5239 check_valid_ptrmem_cst_expr (tree type, tree expr,
5240 tsubst_flags_t complain)
5241 {
5242 STRIP_NOPS (expr);
5243 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5244 return true;
5245 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
5246 return true;
5247 if (complain & tf_error)
5248 {
5249 error ("%qE is not a valid template argument for type %qT",
5250 expr, type);
5251 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5252 }
5253 return false;
5254 }
5255
5256 /* Returns TRUE iff the address of OP is value-dependent.
5257
5258 14.6.2.4 [temp.dep.temp]:
5259 A non-integral non-type template-argument is dependent if its type is
5260 dependent or it has either of the following forms
5261 qualified-id
5262 & qualified-id
5263 and contains a nested-name-specifier which specifies a class-name that
5264 names a dependent type.
5265
5266 We generalize this to just say that the address of a member of a
5267 dependent class is value-dependent; the above doesn't cover the
5268 address of a static data member named with an unqualified-id. */
5269
5270 static bool
5271 has_value_dependent_address (tree op)
5272 {
5273 /* We could use get_inner_reference here, but there's no need;
5274 this is only relevant for template non-type arguments, which
5275 can only be expressed as &id-expression. */
5276 if (DECL_P (op))
5277 {
5278 tree ctx = CP_DECL_CONTEXT (op);
5279 if (TYPE_P (ctx) && dependent_type_p (ctx))
5280 return true;
5281 }
5282
5283 return false;
5284 }
5285
5286 /* The next set of functions are used for providing helpful explanatory
5287 diagnostics for failed overload resolution. Their messages should be
5288 indented by two spaces for consistency with the messages in
5289 call.c */
5290
5291 static int
5292 unify_success (bool /*explain_p*/)
5293 {
5294 return 0;
5295 }
5296
5297 static int
5298 unify_parameter_deduction_failure (bool explain_p, tree parm)
5299 {
5300 if (explain_p)
5301 inform (input_location,
5302 " couldn't deduce template parameter %qD", parm);
5303 return 1;
5304 }
5305
5306 static int
5307 unify_invalid (bool /*explain_p*/)
5308 {
5309 return 1;
5310 }
5311
5312 static int
5313 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5314 {
5315 if (explain_p)
5316 inform (input_location,
5317 " types %qT and %qT have incompatible cv-qualifiers",
5318 parm, arg);
5319 return 1;
5320 }
5321
5322 static int
5323 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5324 {
5325 if (explain_p)
5326 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5327 return 1;
5328 }
5329
5330 static int
5331 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5332 {
5333 if (explain_p)
5334 inform (input_location,
5335 " template parameter %qD is not a parameter pack, but "
5336 "argument %qD is",
5337 parm, arg);
5338 return 1;
5339 }
5340
5341 static int
5342 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5343 {
5344 if (explain_p)
5345 inform (input_location,
5346 " template argument %qE does not match "
5347 "pointer-to-member constant %qE",
5348 arg, parm);
5349 return 1;
5350 }
5351
5352 static int
5353 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5354 {
5355 if (explain_p)
5356 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5357 return 1;
5358 }
5359
5360 static int
5361 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5362 {
5363 if (explain_p)
5364 inform (input_location,
5365 " inconsistent parameter pack deduction with %qT and %qT",
5366 old_arg, new_arg);
5367 return 1;
5368 }
5369
5370 static int
5371 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5372 {
5373 if (explain_p)
5374 {
5375 if (TYPE_P (parm))
5376 inform (input_location,
5377 " deduced conflicting types for parameter %qT (%qT and %qT)",
5378 parm, first, second);
5379 else
5380 inform (input_location,
5381 " deduced conflicting values for non-type parameter "
5382 "%qE (%qE and %qE)", parm, first, second);
5383 }
5384 return 1;
5385 }
5386
5387 static int
5388 unify_vla_arg (bool explain_p, tree arg)
5389 {
5390 if (explain_p)
5391 inform (input_location,
5392 " variable-sized array type %qT is not "
5393 "a valid template argument",
5394 arg);
5395 return 1;
5396 }
5397
5398 static int
5399 unify_method_type_error (bool explain_p, tree arg)
5400 {
5401 if (explain_p)
5402 inform (input_location,
5403 " member function type %qT is not a valid template argument",
5404 arg);
5405 return 1;
5406 }
5407
5408 static int
5409 unify_arity (bool explain_p, int have, int wanted)
5410 {
5411 if (explain_p)
5412 inform_n (input_location, wanted,
5413 " candidate expects %d argument, %d provided",
5414 " candidate expects %d arguments, %d provided",
5415 wanted, have);
5416 return 1;
5417 }
5418
5419 static int
5420 unify_too_many_arguments (bool explain_p, int have, int wanted)
5421 {
5422 return unify_arity (explain_p, have, wanted);
5423 }
5424
5425 static int
5426 unify_too_few_arguments (bool explain_p, int have, int wanted)
5427 {
5428 return unify_arity (explain_p, have, wanted);
5429 }
5430
5431 static int
5432 unify_arg_conversion (bool explain_p, tree to_type,
5433 tree from_type, tree arg)
5434 {
5435 if (explain_p)
5436 inform (EXPR_LOC_OR_HERE (arg),
5437 " cannot convert %qE (type %qT) to type %qT",
5438 arg, from_type, to_type);
5439 return 1;
5440 }
5441
5442 static int
5443 unify_no_common_base (bool explain_p, enum template_base_result r,
5444 tree parm, tree arg)
5445 {
5446 if (explain_p)
5447 switch (r)
5448 {
5449 case tbr_ambiguous_baseclass:
5450 inform (input_location, " %qT is an ambiguous base class of %qT",
5451 parm, arg);
5452 break;
5453 default:
5454 inform (input_location, " %qT is not derived from %qT", arg, parm);
5455 break;
5456 }
5457 return 1;
5458 }
5459
5460 static int
5461 unify_inconsistent_template_template_parameters (bool explain_p)
5462 {
5463 if (explain_p)
5464 inform (input_location,
5465 " template parameters of a template template argument are "
5466 "inconsistent with other deduced template arguments");
5467 return 1;
5468 }
5469
5470 static int
5471 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5472 {
5473 if (explain_p)
5474 inform (input_location,
5475 " can't deduce a template for %qT from non-template type %qT",
5476 parm, arg);
5477 return 1;
5478 }
5479
5480 static int
5481 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5482 {
5483 if (explain_p)
5484 inform (input_location,
5485 " template argument %qE does not match %qD", arg, parm);
5486 return 1;
5487 }
5488
5489 static int
5490 unify_overload_resolution_failure (bool explain_p, tree arg)
5491 {
5492 if (explain_p)
5493 inform (input_location,
5494 " could not resolve address from overloaded function %qE",
5495 arg);
5496 return 1;
5497 }
5498
5499 /* Attempt to convert the non-type template parameter EXPR to the
5500 indicated TYPE. If the conversion is successful, return the
5501 converted value. If the conversion is unsuccessful, return
5502 NULL_TREE if we issued an error message, or error_mark_node if we
5503 did not. We issue error messages for out-and-out bad template
5504 parameters, but not simply because the conversion failed, since we
5505 might be just trying to do argument deduction. Both TYPE and EXPR
5506 must be non-dependent.
5507
5508 The conversion follows the special rules described in
5509 [temp.arg.nontype], and it is much more strict than an implicit
5510 conversion.
5511
5512 This function is called twice for each template argument (see
5513 lookup_template_class for a more accurate description of this
5514 problem). This means that we need to handle expressions which
5515 are not valid in a C++ source, but can be created from the
5516 first call (for instance, casts to perform conversions). These
5517 hacks can go away after we fix the double coercion problem. */
5518
5519 static tree
5520 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5521 {
5522 tree expr_type;
5523
5524 /* Detect immediately string literals as invalid non-type argument.
5525 This special-case is not needed for correctness (we would easily
5526 catch this later), but only to provide better diagnostic for this
5527 common user mistake. As suggested by DR 100, we do not mention
5528 linkage issues in the diagnostic as this is not the point. */
5529 /* FIXME we're making this OK. */
5530 if (TREE_CODE (expr) == STRING_CST)
5531 {
5532 if (complain & tf_error)
5533 error ("%qE is not a valid template argument for type %qT "
5534 "because string literals can never be used in this context",
5535 expr, type);
5536 return NULL_TREE;
5537 }
5538
5539 /* Add the ADDR_EXPR now for the benefit of
5540 value_dependent_expression_p. */
5541 if (TYPE_PTROBV_P (type)
5542 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5543 {
5544 expr = decay_conversion (expr, complain);
5545 if (expr == error_mark_node)
5546 return error_mark_node;
5547 }
5548
5549 /* If we are in a template, EXPR may be non-dependent, but still
5550 have a syntactic, rather than semantic, form. For example, EXPR
5551 might be a SCOPE_REF, rather than the VAR_DECL to which the
5552 SCOPE_REF refers. Preserving the qualifying scope is necessary
5553 so that access checking can be performed when the template is
5554 instantiated -- but here we need the resolved form so that we can
5555 convert the argument. */
5556 if (TYPE_REF_OBJ_P (type)
5557 && has_value_dependent_address (expr))
5558 /* If we want the address and it's value-dependent, don't fold. */;
5559 else if (!type_unknown_p (expr))
5560 expr = fold_non_dependent_expr_sfinae (expr, complain);
5561 if (error_operand_p (expr))
5562 return error_mark_node;
5563 expr_type = TREE_TYPE (expr);
5564 if (TREE_CODE (type) == REFERENCE_TYPE)
5565 expr = mark_lvalue_use (expr);
5566 else
5567 expr = mark_rvalue_use (expr);
5568
5569 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5570 to a non-type argument of "nullptr". */
5571 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5572 expr = convert (type, expr);
5573
5574 /* In C++11, integral or enumeration non-type template arguments can be
5575 arbitrary constant expressions. Pointer and pointer to
5576 member arguments can be general constant expressions that evaluate
5577 to a null value, but otherwise still need to be of a specific form. */
5578 if (cxx_dialect >= cxx11)
5579 {
5580 if (TREE_CODE (expr) == PTRMEM_CST)
5581 /* A PTRMEM_CST is already constant, and a valid template
5582 argument for a parameter of pointer to member type, we just want
5583 to leave it in that form rather than lower it to a
5584 CONSTRUCTOR. */;
5585 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5586 expr = maybe_constant_value (expr);
5587 else if (TYPE_PTR_OR_PTRMEM_P (type))
5588 {
5589 tree folded = maybe_constant_value (expr);
5590 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5591 : null_member_pointer_value_p (folded))
5592 expr = folded;
5593 }
5594 }
5595
5596 /* HACK: Due to double coercion, we can get a
5597 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5598 which is the tree that we built on the first call (see
5599 below when coercing to reference to object or to reference to
5600 function). We just strip everything and get to the arg.
5601 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5602 for examples. */
5603 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5604 {
5605 tree probe_type, probe = expr;
5606 if (REFERENCE_REF_P (probe))
5607 probe = TREE_OPERAND (probe, 0);
5608 probe_type = TREE_TYPE (probe);
5609 if (TREE_CODE (probe) == NOP_EXPR)
5610 {
5611 /* ??? Maybe we could use convert_from_reference here, but we
5612 would need to relax its constraints because the NOP_EXPR
5613 could actually change the type to something more cv-qualified,
5614 and this is not folded by convert_from_reference. */
5615 tree addr = TREE_OPERAND (probe, 0);
5616 if (TREE_CODE (probe_type) == REFERENCE_TYPE
5617 && TREE_CODE (addr) == ADDR_EXPR
5618 && TYPE_PTR_P (TREE_TYPE (addr))
5619 && (same_type_ignoring_top_level_qualifiers_p
5620 (TREE_TYPE (probe_type),
5621 TREE_TYPE (TREE_TYPE (addr)))))
5622 {
5623 expr = TREE_OPERAND (addr, 0);
5624 expr_type = TREE_TYPE (probe_type);
5625 }
5626 }
5627 }
5628
5629 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5630 parameter is a pointer to object, through decay and
5631 qualification conversion. Let's strip everything. */
5632 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5633 {
5634 tree probe = expr;
5635 STRIP_NOPS (probe);
5636 if (TREE_CODE (probe) == ADDR_EXPR
5637 && TYPE_PTR_P (TREE_TYPE (probe)))
5638 {
5639 /* Skip the ADDR_EXPR only if it is part of the decay for
5640 an array. Otherwise, it is part of the original argument
5641 in the source code. */
5642 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
5643 probe = TREE_OPERAND (probe, 0);
5644 expr = probe;
5645 expr_type = TREE_TYPE (expr);
5646 }
5647 }
5648
5649 /* [temp.arg.nontype]/5, bullet 1
5650
5651 For a non-type template-parameter of integral or enumeration type,
5652 integral promotions (_conv.prom_) and integral conversions
5653 (_conv.integral_) are applied. */
5654 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5655 {
5656 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5657 t = maybe_constant_value (t);
5658 if (t != error_mark_node)
5659 expr = t;
5660
5661 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5662 return error_mark_node;
5663
5664 /* Notice that there are constant expressions like '4 % 0' which
5665 do not fold into integer constants. */
5666 if (TREE_CODE (expr) != INTEGER_CST)
5667 {
5668 if (complain & tf_error)
5669 {
5670 int errs = errorcount, warns = warningcount + werrorcount;
5671 if (processing_template_decl
5672 && !require_potential_constant_expression (expr))
5673 return NULL_TREE;
5674 expr = cxx_constant_value (expr);
5675 if (errorcount > errs || warningcount + werrorcount > warns)
5676 inform (EXPR_LOC_OR_HERE (expr),
5677 "in template argument for type %qT ", type);
5678 if (expr == error_mark_node)
5679 return NULL_TREE;
5680 /* else cxx_constant_value complained but gave us
5681 a real constant, so go ahead. */
5682 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5683 }
5684 else
5685 return NULL_TREE;
5686 }
5687
5688 /* Avoid typedef problems. */
5689 if (TREE_TYPE (expr) != type)
5690 expr = fold_convert (type, expr);
5691 }
5692 /* [temp.arg.nontype]/5, bullet 2
5693
5694 For a non-type template-parameter of type pointer to object,
5695 qualification conversions (_conv.qual_) and the array-to-pointer
5696 conversion (_conv.array_) are applied. */
5697 else if (TYPE_PTROBV_P (type))
5698 {
5699 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5700
5701 A template-argument for a non-type, non-template template-parameter
5702 shall be one of: [...]
5703
5704 -- the name of a non-type template-parameter;
5705 -- the address of an object or function with external linkage, [...]
5706 expressed as "& id-expression" where the & is optional if the name
5707 refers to a function or array, or if the corresponding
5708 template-parameter is a reference.
5709
5710 Here, we do not care about functions, as they are invalid anyway
5711 for a parameter of type pointer-to-object. */
5712
5713 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5714 /* Non-type template parameters are OK. */
5715 ;
5716 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
5717 /* Null pointer values are OK in C++11. */;
5718 else if (TREE_CODE (expr) != ADDR_EXPR
5719 && TREE_CODE (expr_type) != ARRAY_TYPE)
5720 {
5721 if (VAR_P (expr))
5722 {
5723 error ("%qD is not a valid template argument "
5724 "because %qD is a variable, not the address of "
5725 "a variable",
5726 expr, expr);
5727 return NULL_TREE;
5728 }
5729 if (POINTER_TYPE_P (expr_type))
5730 {
5731 error ("%qE is not a valid template argument for %qT "
5732 "because it is not the address of a variable",
5733 expr, type);
5734 return NULL_TREE;
5735 }
5736 /* Other values, like integer constants, might be valid
5737 non-type arguments of some other type. */
5738 return error_mark_node;
5739 }
5740 else
5741 {
5742 tree decl;
5743
5744 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5745 ? TREE_OPERAND (expr, 0) : expr);
5746 if (!VAR_P (decl))
5747 {
5748 error ("%qE is not a valid template argument of type %qT "
5749 "because %qE is not a variable",
5750 expr, type, decl);
5751 return NULL_TREE;
5752 }
5753 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
5754 {
5755 error ("%qE is not a valid template argument of type %qT "
5756 "because %qD does not have external linkage",
5757 expr, type, decl);
5758 return NULL_TREE;
5759 }
5760 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
5761 {
5762 error ("%qE is not a valid template argument of type %qT "
5763 "because %qD has no linkage",
5764 expr, type, decl);
5765 return NULL_TREE;
5766 }
5767 }
5768
5769 expr = decay_conversion (expr, complain);
5770 if (expr == error_mark_node)
5771 return error_mark_node;
5772
5773 expr = perform_qualification_conversions (type, expr);
5774 if (expr == error_mark_node)
5775 return error_mark_node;
5776 }
5777 /* [temp.arg.nontype]/5, bullet 3
5778
5779 For a non-type template-parameter of type reference to object, no
5780 conversions apply. The type referred to by the reference may be more
5781 cv-qualified than the (otherwise identical) type of the
5782 template-argument. The template-parameter is bound directly to the
5783 template-argument, which must be an lvalue. */
5784 else if (TYPE_REF_OBJ_P (type))
5785 {
5786 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5787 expr_type))
5788 return error_mark_node;
5789
5790 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5791 {
5792 error ("%qE is not a valid template argument for type %qT "
5793 "because of conflicts in cv-qualification", expr, type);
5794 return NULL_TREE;
5795 }
5796
5797 if (!real_lvalue_p (expr))
5798 {
5799 error ("%qE is not a valid template argument for type %qT "
5800 "because it is not an lvalue", expr, type);
5801 return NULL_TREE;
5802 }
5803
5804 /* [temp.arg.nontype]/1
5805
5806 A template-argument for a non-type, non-template template-parameter
5807 shall be one of: [...]
5808
5809 -- the address of an object or function with external linkage. */
5810 if (INDIRECT_REF_P (expr)
5811 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5812 {
5813 expr = TREE_OPERAND (expr, 0);
5814 if (DECL_P (expr))
5815 {
5816 error ("%q#D is not a valid template argument for type %qT "
5817 "because a reference variable does not have a constant "
5818 "address", expr, type);
5819 return NULL_TREE;
5820 }
5821 }
5822
5823 if (!DECL_P (expr))
5824 {
5825 error ("%qE is not a valid template argument for type %qT "
5826 "because it is not an object with external linkage",
5827 expr, type);
5828 return NULL_TREE;
5829 }
5830
5831 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5832 {
5833 error ("%qE is not a valid template argument for type %qT "
5834 "because object %qD has not external linkage",
5835 expr, type, expr);
5836 return NULL_TREE;
5837 }
5838
5839 expr = build_nop (type, build_address (expr));
5840 }
5841 /* [temp.arg.nontype]/5, bullet 4
5842
5843 For a non-type template-parameter of type pointer to function, only
5844 the function-to-pointer conversion (_conv.func_) is applied. If the
5845 template-argument represents a set of overloaded functions (or a
5846 pointer to such), the matching function is selected from the set
5847 (_over.over_). */
5848 else if (TYPE_PTRFN_P (type))
5849 {
5850 /* If the argument is a template-id, we might not have enough
5851 context information to decay the pointer. */
5852 if (!type_unknown_p (expr_type))
5853 {
5854 expr = decay_conversion (expr, complain);
5855 if (expr == error_mark_node)
5856 return error_mark_node;
5857 }
5858
5859 if (cxx_dialect >= cxx11 && integer_zerop (expr))
5860 /* Null pointer values are OK in C++11. */
5861 return perform_qualification_conversions (type, expr);
5862
5863 expr = convert_nontype_argument_function (type, expr);
5864 if (!expr || expr == error_mark_node)
5865 return expr;
5866 }
5867 /* [temp.arg.nontype]/5, bullet 5
5868
5869 For a non-type template-parameter of type reference to function, no
5870 conversions apply. If the template-argument represents a set of
5871 overloaded functions, the matching function is selected from the set
5872 (_over.over_). */
5873 else if (TYPE_REFFN_P (type))
5874 {
5875 if (TREE_CODE (expr) == ADDR_EXPR)
5876 {
5877 error ("%qE is not a valid template argument for type %qT "
5878 "because it is a pointer", expr, type);
5879 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5880 return NULL_TREE;
5881 }
5882
5883 expr = convert_nontype_argument_function (type, expr);
5884 if (!expr || expr == error_mark_node)
5885 return expr;
5886
5887 expr = build_nop (type, build_address (expr));
5888 }
5889 /* [temp.arg.nontype]/5, bullet 6
5890
5891 For a non-type template-parameter of type pointer to member function,
5892 no conversions apply. If the template-argument represents a set of
5893 overloaded member functions, the matching member function is selected
5894 from the set (_over.over_). */
5895 else if (TYPE_PTRMEMFUNC_P (type))
5896 {
5897 expr = instantiate_type (type, expr, tf_none);
5898 if (expr == error_mark_node)
5899 return error_mark_node;
5900
5901 /* [temp.arg.nontype] bullet 1 says the pointer to member
5902 expression must be a pointer-to-member constant. */
5903 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5904 return error_mark_node;
5905
5906 /* There is no way to disable standard conversions in
5907 resolve_address_of_overloaded_function (called by
5908 instantiate_type). It is possible that the call succeeded by
5909 converting &B::I to &D::I (where B is a base of D), so we need
5910 to reject this conversion here.
5911
5912 Actually, even if there was a way to disable standard conversions,
5913 it would still be better to reject them here so that we can
5914 provide a superior diagnostic. */
5915 if (!same_type_p (TREE_TYPE (expr), type))
5916 {
5917 error ("%qE is not a valid template argument for type %qT "
5918 "because it is of type %qT", expr, type,
5919 TREE_TYPE (expr));
5920 /* If we are just one standard conversion off, explain. */
5921 if (can_convert_standard (type, TREE_TYPE (expr), complain))
5922 inform (input_location,
5923 "standard conversions are not allowed in this context");
5924 return NULL_TREE;
5925 }
5926 }
5927 /* [temp.arg.nontype]/5, bullet 7
5928
5929 For a non-type template-parameter of type pointer to data member,
5930 qualification conversions (_conv.qual_) are applied. */
5931 else if (TYPE_PTRDATAMEM_P (type))
5932 {
5933 /* [temp.arg.nontype] bullet 1 says the pointer to member
5934 expression must be a pointer-to-member constant. */
5935 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5936 return error_mark_node;
5937
5938 expr = perform_qualification_conversions (type, expr);
5939 if (expr == error_mark_node)
5940 return expr;
5941 }
5942 else if (NULLPTR_TYPE_P (type))
5943 {
5944 if (expr != nullptr_node)
5945 {
5946 error ("%qE is not a valid template argument for type %qT "
5947 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5948 return NULL_TREE;
5949 }
5950 return expr;
5951 }
5952 /* A template non-type parameter must be one of the above. */
5953 else
5954 gcc_unreachable ();
5955
5956 /* Sanity check: did we actually convert the argument to the
5957 right type? */
5958 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5959 (type, TREE_TYPE (expr)));
5960 return expr;
5961 }
5962
5963 /* Subroutine of coerce_template_template_parms, which returns 1 if
5964 PARM_PARM and ARG_PARM match using the rule for the template
5965 parameters of template template parameters. Both PARM and ARG are
5966 template parameters; the rest of the arguments are the same as for
5967 coerce_template_template_parms.
5968 */
5969 static int
5970 coerce_template_template_parm (tree parm,
5971 tree arg,
5972 tsubst_flags_t complain,
5973 tree in_decl,
5974 tree outer_args)
5975 {
5976 if (arg == NULL_TREE || arg == error_mark_node
5977 || parm == NULL_TREE || parm == error_mark_node)
5978 return 0;
5979
5980 if (TREE_CODE (arg) != TREE_CODE (parm))
5981 return 0;
5982
5983 switch (TREE_CODE (parm))
5984 {
5985 case TEMPLATE_DECL:
5986 /* We encounter instantiations of templates like
5987 template <template <template <class> class> class TT>
5988 class C; */
5989 {
5990 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5991 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5992
5993 if (!coerce_template_template_parms
5994 (parmparm, argparm, complain, in_decl, outer_args))
5995 return 0;
5996 }
5997 /* Fall through. */
5998
5999 case TYPE_DECL:
6000 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6001 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6002 /* Argument is a parameter pack but parameter is not. */
6003 return 0;
6004 break;
6005
6006 case PARM_DECL:
6007 /* The tsubst call is used to handle cases such as
6008
6009 template <int> class C {};
6010 template <class T, template <T> class TT> class D {};
6011 D<int, C> d;
6012
6013 i.e. the parameter list of TT depends on earlier parameters. */
6014 if (!uses_template_parms (TREE_TYPE (arg))
6015 && !same_type_p
6016 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
6017 TREE_TYPE (arg)))
6018 return 0;
6019
6020 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6021 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6022 /* Argument is a parameter pack but parameter is not. */
6023 return 0;
6024
6025 break;
6026
6027 default:
6028 gcc_unreachable ();
6029 }
6030
6031 return 1;
6032 }
6033
6034
6035 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6036 template template parameters. Both PARM_PARMS and ARG_PARMS are
6037 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6038 or PARM_DECL.
6039
6040 Consider the example:
6041 template <class T> class A;
6042 template<template <class U> class TT> class B;
6043
6044 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6045 the parameters to A, and OUTER_ARGS contains A. */
6046
6047 static int
6048 coerce_template_template_parms (tree parm_parms,
6049 tree arg_parms,
6050 tsubst_flags_t complain,
6051 tree in_decl,
6052 tree outer_args)
6053 {
6054 int nparms, nargs, i;
6055 tree parm, arg;
6056 int variadic_p = 0;
6057
6058 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
6059 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
6060
6061 nparms = TREE_VEC_LENGTH (parm_parms);
6062 nargs = TREE_VEC_LENGTH (arg_parms);
6063
6064 /* Determine whether we have a parameter pack at the end of the
6065 template template parameter's template parameter list. */
6066 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
6067 {
6068 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
6069
6070 if (parm == error_mark_node)
6071 return 0;
6072
6073 switch (TREE_CODE (parm))
6074 {
6075 case TEMPLATE_DECL:
6076 case TYPE_DECL:
6077 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6078 variadic_p = 1;
6079 break;
6080
6081 case PARM_DECL:
6082 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6083 variadic_p = 1;
6084 break;
6085
6086 default:
6087 gcc_unreachable ();
6088 }
6089 }
6090
6091 if (nargs != nparms
6092 && !(variadic_p && nargs >= nparms - 1))
6093 return 0;
6094
6095 /* Check all of the template parameters except the parameter pack at
6096 the end (if any). */
6097 for (i = 0; i < nparms - variadic_p; ++i)
6098 {
6099 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6100 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6101 continue;
6102
6103 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6104 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6105
6106 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6107 outer_args))
6108 return 0;
6109
6110 }
6111
6112 if (variadic_p)
6113 {
6114 /* Check each of the template parameters in the template
6115 argument against the template parameter pack at the end of
6116 the template template parameter. */
6117 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6118 return 0;
6119
6120 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6121
6122 for (; i < nargs; ++i)
6123 {
6124 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6125 continue;
6126
6127 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6128
6129 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6130 outer_args))
6131 return 0;
6132 }
6133 }
6134
6135 return 1;
6136 }
6137
6138 /* Verifies that the deduced template arguments (in TARGS) for the
6139 template template parameters (in TPARMS) represent valid bindings,
6140 by comparing the template parameter list of each template argument
6141 to the template parameter list of its corresponding template
6142 template parameter, in accordance with DR150. This
6143 routine can only be called after all template arguments have been
6144 deduced. It will return TRUE if all of the template template
6145 parameter bindings are okay, FALSE otherwise. */
6146 bool
6147 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6148 {
6149 int i, ntparms = TREE_VEC_LENGTH (tparms);
6150 bool ret = true;
6151
6152 /* We're dealing with template parms in this process. */
6153 ++processing_template_decl;
6154
6155 targs = INNERMOST_TEMPLATE_ARGS (targs);
6156
6157 for (i = 0; i < ntparms; ++i)
6158 {
6159 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6160 tree targ = TREE_VEC_ELT (targs, i);
6161
6162 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6163 {
6164 tree packed_args = NULL_TREE;
6165 int idx, len = 1;
6166
6167 if (ARGUMENT_PACK_P (targ))
6168 {
6169 /* Look inside the argument pack. */
6170 packed_args = ARGUMENT_PACK_ARGS (targ);
6171 len = TREE_VEC_LENGTH (packed_args);
6172 }
6173
6174 for (idx = 0; idx < len; ++idx)
6175 {
6176 tree targ_parms = NULL_TREE;
6177
6178 if (packed_args)
6179 /* Extract the next argument from the argument
6180 pack. */
6181 targ = TREE_VEC_ELT (packed_args, idx);
6182
6183 if (PACK_EXPANSION_P (targ))
6184 /* Look at the pattern of the pack expansion. */
6185 targ = PACK_EXPANSION_PATTERN (targ);
6186
6187 /* Extract the template parameters from the template
6188 argument. */
6189 if (TREE_CODE (targ) == TEMPLATE_DECL)
6190 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6191 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6192 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6193
6194 /* Verify that we can coerce the template template
6195 parameters from the template argument to the template
6196 parameter. This requires an exact match. */
6197 if (targ_parms
6198 && !coerce_template_template_parms
6199 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6200 targ_parms,
6201 tf_none,
6202 tparm,
6203 targs))
6204 {
6205 ret = false;
6206 goto out;
6207 }
6208 }
6209 }
6210 }
6211
6212 out:
6213
6214 --processing_template_decl;
6215 return ret;
6216 }
6217
6218 /* Since type attributes aren't mangled, we need to strip them from
6219 template type arguments. */
6220
6221 static tree
6222 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6223 {
6224 tree mv;
6225 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6226 return arg;
6227 mv = TYPE_MAIN_VARIANT (arg);
6228 arg = strip_typedefs (arg);
6229 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6230 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6231 {
6232 if (complain & tf_warning)
6233 warning (0, "ignoring attributes on template argument %qT", arg);
6234 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6235 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6236 }
6237 return arg;
6238 }
6239
6240 /* Convert the indicated template ARG as necessary to match the
6241 indicated template PARM. Returns the converted ARG, or
6242 error_mark_node if the conversion was unsuccessful. Error and
6243 warning messages are issued under control of COMPLAIN. This
6244 conversion is for the Ith parameter in the parameter list. ARGS is
6245 the full set of template arguments deduced so far. */
6246
6247 static tree
6248 convert_template_argument (tree parm,
6249 tree arg,
6250 tree args,
6251 tsubst_flags_t complain,
6252 int i,
6253 tree in_decl)
6254 {
6255 tree orig_arg;
6256 tree val;
6257 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6258
6259 if (TREE_CODE (arg) == TREE_LIST
6260 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6261 {
6262 /* The template argument was the name of some
6263 member function. That's usually
6264 invalid, but static members are OK. In any
6265 case, grab the underlying fields/functions
6266 and issue an error later if required. */
6267 orig_arg = TREE_VALUE (arg);
6268 TREE_TYPE (arg) = unknown_type_node;
6269 }
6270
6271 orig_arg = arg;
6272
6273 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6274 requires_type = (TREE_CODE (parm) == TYPE_DECL
6275 || requires_tmpl_type);
6276
6277 /* When determining whether an argument pack expansion is a template,
6278 look at the pattern. */
6279 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6280 arg = PACK_EXPANSION_PATTERN (arg);
6281
6282 /* Deal with an injected-class-name used as a template template arg. */
6283 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6284 {
6285 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6286 if (TREE_CODE (t) == TEMPLATE_DECL)
6287 {
6288 if (cxx_dialect >= cxx11)
6289 /* OK under DR 1004. */;
6290 else if (complain & tf_warning_or_error)
6291 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6292 " used as template template argument", TYPE_NAME (arg));
6293 else if (flag_pedantic_errors)
6294 t = arg;
6295
6296 arg = t;
6297 }
6298 }
6299
6300 is_tmpl_type =
6301 ((TREE_CODE (arg) == TEMPLATE_DECL
6302 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6303 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6304 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6305 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6306
6307 if (is_tmpl_type
6308 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6309 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6310 arg = TYPE_STUB_DECL (arg);
6311
6312 is_type = TYPE_P (arg) || is_tmpl_type;
6313
6314 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6315 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6316 {
6317 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6318 {
6319 if (complain & tf_error)
6320 error ("invalid use of destructor %qE as a type", orig_arg);
6321 return error_mark_node;
6322 }
6323
6324 permerror (input_location,
6325 "to refer to a type member of a template parameter, "
6326 "use %<typename %E%>", orig_arg);
6327
6328 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6329 TREE_OPERAND (arg, 1),
6330 typename_type,
6331 complain);
6332 arg = orig_arg;
6333 is_type = 1;
6334 }
6335 if (is_type != requires_type)
6336 {
6337 if (in_decl)
6338 {
6339 if (complain & tf_error)
6340 {
6341 error ("type/value mismatch at argument %d in template "
6342 "parameter list for %qD",
6343 i + 1, in_decl);
6344 if (is_type)
6345 error (" expected a constant of type %qT, got %qT",
6346 TREE_TYPE (parm),
6347 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6348 else if (requires_tmpl_type)
6349 error (" expected a class template, got %qE", orig_arg);
6350 else
6351 error (" expected a type, got %qE", orig_arg);
6352 }
6353 }
6354 return error_mark_node;
6355 }
6356 if (is_tmpl_type ^ requires_tmpl_type)
6357 {
6358 if (in_decl && (complain & tf_error))
6359 {
6360 error ("type/value mismatch at argument %d in template "
6361 "parameter list for %qD",
6362 i + 1, in_decl);
6363 if (is_tmpl_type)
6364 error (" expected a type, got %qT", DECL_NAME (arg));
6365 else
6366 error (" expected a class template, got %qT", orig_arg);
6367 }
6368 return error_mark_node;
6369 }
6370
6371 if (is_type)
6372 {
6373 if (requires_tmpl_type)
6374 {
6375 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6376 val = orig_arg;
6377 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6378 /* The number of argument required is not known yet.
6379 Just accept it for now. */
6380 val = TREE_TYPE (arg);
6381 else
6382 {
6383 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6384 tree argparm;
6385
6386 /* Strip alias templates that are equivalent to another
6387 template. */
6388 arg = get_underlying_template (arg);
6389 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6390
6391 if (coerce_template_template_parms (parmparm, argparm,
6392 complain, in_decl,
6393 args))
6394 {
6395 val = arg;
6396
6397 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6398 TEMPLATE_DECL. */
6399 if (val != error_mark_node)
6400 {
6401 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6402 val = TREE_TYPE (val);
6403 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6404 val = make_pack_expansion (val);
6405 }
6406 }
6407 else
6408 {
6409 if (in_decl && (complain & tf_error))
6410 {
6411 error ("type/value mismatch at argument %d in "
6412 "template parameter list for %qD",
6413 i + 1, in_decl);
6414 error (" expected a template of type %qD, got %qT",
6415 parm, orig_arg);
6416 }
6417
6418 val = error_mark_node;
6419 }
6420 }
6421 }
6422 else
6423 val = orig_arg;
6424 /* We only form one instance of each template specialization.
6425 Therefore, if we use a non-canonical variant (i.e., a
6426 typedef), any future messages referring to the type will use
6427 the typedef, which is confusing if those future uses do not
6428 themselves also use the typedef. */
6429 if (TYPE_P (val))
6430 val = canonicalize_type_argument (val, complain);
6431 }
6432 else
6433 {
6434 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6435
6436 if (invalid_nontype_parm_type_p (t, complain))
6437 return error_mark_node;
6438
6439 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6440 {
6441 if (same_type_p (t, TREE_TYPE (orig_arg)))
6442 val = orig_arg;
6443 else
6444 {
6445 /* Not sure if this is reachable, but it doesn't hurt
6446 to be robust. */
6447 error ("type mismatch in nontype parameter pack");
6448 val = error_mark_node;
6449 }
6450 }
6451 else if (!dependent_template_arg_p (orig_arg)
6452 && !uses_template_parms (t))
6453 /* We used to call digest_init here. However, digest_init
6454 will report errors, which we don't want when complain
6455 is zero. More importantly, digest_init will try too
6456 hard to convert things: for example, `0' should not be
6457 converted to pointer type at this point according to
6458 the standard. Accepting this is not merely an
6459 extension, since deciding whether or not these
6460 conversions can occur is part of determining which
6461 function template to call, or whether a given explicit
6462 argument specification is valid. */
6463 val = convert_nontype_argument (t, orig_arg, complain);
6464 else
6465 val = strip_typedefs_expr (orig_arg);
6466
6467 if (val == NULL_TREE)
6468 val = error_mark_node;
6469 else if (val == error_mark_node && (complain & tf_error))
6470 error ("could not convert template argument %qE to %qT", orig_arg, t);
6471
6472 if (TREE_CODE (val) == SCOPE_REF)
6473 {
6474 /* Strip typedefs from the SCOPE_REF. */
6475 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6476 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6477 complain);
6478 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6479 QUALIFIED_NAME_IS_TEMPLATE (val));
6480 }
6481 }
6482
6483 return val;
6484 }
6485
6486 /* Coerces the remaining template arguments in INNER_ARGS (from
6487 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6488 Returns the coerced argument pack. PARM_IDX is the position of this
6489 parameter in the template parameter list. ARGS is the original
6490 template argument list. */
6491 static tree
6492 coerce_template_parameter_pack (tree parms,
6493 int parm_idx,
6494 tree args,
6495 tree inner_args,
6496 int arg_idx,
6497 tree new_args,
6498 int* lost,
6499 tree in_decl,
6500 tsubst_flags_t complain)
6501 {
6502 tree parm = TREE_VEC_ELT (parms, parm_idx);
6503 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6504 tree packed_args;
6505 tree argument_pack;
6506 tree packed_types = NULL_TREE;
6507
6508 if (arg_idx > nargs)
6509 arg_idx = nargs;
6510
6511 packed_args = make_tree_vec (nargs - arg_idx);
6512
6513 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6514 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6515 {
6516 /* When the template parameter is a non-type template
6517 parameter pack whose type uses parameter packs, we need
6518 to look at each of the template arguments
6519 separately. Build a vector of the types for these
6520 non-type template parameters in PACKED_TYPES. */
6521 tree expansion
6522 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6523 packed_types = tsubst_pack_expansion (expansion, args,
6524 complain, in_decl);
6525
6526 if (packed_types == error_mark_node)
6527 return error_mark_node;
6528
6529 /* Check that we have the right number of arguments. */
6530 if (arg_idx < nargs
6531 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6532 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6533 {
6534 int needed_parms
6535 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6536 error ("wrong number of template arguments (%d, should be %d)",
6537 nargs, needed_parms);
6538 return error_mark_node;
6539 }
6540
6541 /* If we aren't able to check the actual arguments now
6542 (because they haven't been expanded yet), we can at least
6543 verify that all of the types used for the non-type
6544 template parameter pack are, in fact, valid for non-type
6545 template parameters. */
6546 if (arg_idx < nargs
6547 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6548 {
6549 int j, len = TREE_VEC_LENGTH (packed_types);
6550 for (j = 0; j < len; ++j)
6551 {
6552 tree t = TREE_VEC_ELT (packed_types, j);
6553 if (invalid_nontype_parm_type_p (t, complain))
6554 return error_mark_node;
6555 }
6556 }
6557 }
6558
6559 /* Convert the remaining arguments, which will be a part of the
6560 parameter pack "parm". */
6561 for (; arg_idx < nargs; ++arg_idx)
6562 {
6563 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6564 tree actual_parm = TREE_VALUE (parm);
6565
6566 if (packed_types && !PACK_EXPANSION_P (arg))
6567 {
6568 /* When we have a vector of types (corresponding to the
6569 non-type template parameter pack that uses parameter
6570 packs in its type, as mention above), and the
6571 argument is not an expansion (which expands to a
6572 currently unknown number of arguments), clone the
6573 parm and give it the next type in PACKED_TYPES. */
6574 actual_parm = copy_node (actual_parm);
6575 TREE_TYPE (actual_parm) =
6576 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6577 }
6578
6579 if (arg == error_mark_node)
6580 {
6581 if (complain & tf_error)
6582 error ("template argument %d is invalid", arg_idx + 1);
6583 }
6584 else
6585 arg = convert_template_argument (actual_parm,
6586 arg, new_args, complain, parm_idx,
6587 in_decl);
6588 if (arg == error_mark_node)
6589 (*lost)++;
6590 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6591 }
6592
6593 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6594 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6595 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6596 else
6597 {
6598 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6599 TREE_TYPE (argument_pack)
6600 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6601 TREE_CONSTANT (argument_pack) = 1;
6602 }
6603
6604 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6605 #ifdef ENABLE_CHECKING
6606 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6607 TREE_VEC_LENGTH (packed_args));
6608 #endif
6609 return argument_pack;
6610 }
6611
6612 /* Returns the number of pack expansions in the template argument vector
6613 ARGS. */
6614
6615 static int
6616 pack_expansion_args_count (tree args)
6617 {
6618 int i;
6619 int count = 0;
6620 if (args)
6621 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6622 {
6623 tree elt = TREE_VEC_ELT (args, i);
6624 if (elt && PACK_EXPANSION_P (elt))
6625 ++count;
6626 }
6627 return count;
6628 }
6629
6630 /* Convert all template arguments to their appropriate types, and
6631 return a vector containing the innermost resulting template
6632 arguments. If any error occurs, return error_mark_node. Error and
6633 warning messages are issued under control of COMPLAIN.
6634
6635 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6636 for arguments not specified in ARGS. Otherwise, if
6637 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6638 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6639 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6640 ARGS. */
6641
6642 static tree
6643 coerce_template_parms (tree parms,
6644 tree args,
6645 tree in_decl,
6646 tsubst_flags_t complain,
6647 bool require_all_args,
6648 bool use_default_args)
6649 {
6650 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6651 tree inner_args;
6652 tree new_args;
6653 tree new_inner_args;
6654 int saved_unevaluated_operand;
6655 int saved_inhibit_evaluation_warnings;
6656
6657 /* When used as a boolean value, indicates whether this is a
6658 variadic template parameter list. Since it's an int, we can also
6659 subtract it from nparms to get the number of non-variadic
6660 parameters. */
6661 int variadic_p = 0;
6662 int variadic_args_p = 0;
6663 int post_variadic_parms = 0;
6664
6665 if (args == error_mark_node)
6666 return error_mark_node;
6667
6668 nparms = TREE_VEC_LENGTH (parms);
6669
6670 /* Determine if there are any parameter packs. */
6671 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6672 {
6673 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6674 if (variadic_p)
6675 ++post_variadic_parms;
6676 if (template_parameter_pack_p (tparm))
6677 ++variadic_p;
6678 }
6679
6680 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6681 /* If there are no parameters that follow a parameter pack, we need to
6682 expand any argument packs so that we can deduce a parameter pack from
6683 some non-packed args followed by an argument pack, as in variadic85.C.
6684 If there are such parameters, we need to leave argument packs intact
6685 so the arguments are assigned properly. This can happen when dealing
6686 with a nested class inside a partial specialization of a class
6687 template, as in variadic92.C, or when deducing a template parameter pack
6688 from a sub-declarator, as in variadic114.C. */
6689 if (!post_variadic_parms)
6690 inner_args = expand_template_argument_pack (inner_args);
6691
6692 /* Count any pack expansion args. */
6693 variadic_args_p = pack_expansion_args_count (inner_args);
6694
6695 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6696 if ((nargs > nparms && !variadic_p)
6697 || (nargs < nparms - variadic_p
6698 && require_all_args
6699 && !variadic_args_p
6700 && (!use_default_args
6701 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6702 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6703 {
6704 if (complain & tf_error)
6705 {
6706 if (variadic_p)
6707 {
6708 nparms -= variadic_p;
6709 error ("wrong number of template arguments "
6710 "(%d, should be %d or more)", nargs, nparms);
6711 }
6712 else
6713 error ("wrong number of template arguments "
6714 "(%d, should be %d)", nargs, nparms);
6715
6716 if (in_decl)
6717 error ("provided for %q+D", in_decl);
6718 }
6719
6720 return error_mark_node;
6721 }
6722 /* We can't pass a pack expansion to a non-pack parameter of an alias
6723 template (DR 1430). */
6724 else if (in_decl && DECL_ALIAS_TEMPLATE_P (in_decl)
6725 && variadic_args_p
6726 && nargs - variadic_args_p < nparms - variadic_p)
6727 {
6728 if (complain & tf_error)
6729 {
6730 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
6731 {
6732 tree arg = TREE_VEC_ELT (inner_args, i);
6733 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6734
6735 if (PACK_EXPANSION_P (arg)
6736 && !template_parameter_pack_p (parm))
6737 {
6738 error ("pack expansion argument for non-pack parameter "
6739 "%qD of alias template %qD", parm, in_decl);
6740 inform (DECL_SOURCE_LOCATION (parm), "declared here");
6741 goto found;
6742 }
6743 }
6744 gcc_unreachable ();
6745 found:;
6746 }
6747 return error_mark_node;
6748 }
6749
6750 /* We need to evaluate the template arguments, even though this
6751 template-id may be nested within a "sizeof". */
6752 saved_unevaluated_operand = cp_unevaluated_operand;
6753 cp_unevaluated_operand = 0;
6754 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6755 c_inhibit_evaluation_warnings = 0;
6756 new_inner_args = make_tree_vec (nparms);
6757 new_args = add_outermost_template_args (args, new_inner_args);
6758 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6759 {
6760 tree arg;
6761 tree parm;
6762
6763 /* Get the Ith template parameter. */
6764 parm = TREE_VEC_ELT (parms, parm_idx);
6765
6766 if (parm == error_mark_node)
6767 {
6768 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6769 continue;
6770 }
6771
6772 /* Calculate the next argument. */
6773 if (arg_idx < nargs)
6774 arg = TREE_VEC_ELT (inner_args, arg_idx);
6775 else
6776 arg = NULL_TREE;
6777
6778 if (template_parameter_pack_p (TREE_VALUE (parm))
6779 && !(arg && ARGUMENT_PACK_P (arg)))
6780 {
6781 /* All remaining arguments will be placed in the
6782 template parameter pack PARM. */
6783 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6784 inner_args, arg_idx,
6785 new_args, &lost,
6786 in_decl, complain);
6787
6788 /* Store this argument. */
6789 if (arg == error_mark_node)
6790 lost++;
6791 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6792
6793 /* We are done with all of the arguments. */
6794 arg_idx = nargs;
6795
6796 continue;
6797 }
6798 else if (arg)
6799 {
6800 if (PACK_EXPANSION_P (arg))
6801 {
6802 /* We don't know how many args we have yet, just
6803 use the unconverted ones for now. */
6804 new_inner_args = inner_args;
6805 break;
6806 }
6807 }
6808 else if (require_all_args)
6809 {
6810 /* There must be a default arg in this case. */
6811 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6812 complain, in_decl);
6813 /* The position of the first default template argument,
6814 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6815 Record that. */
6816 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6817 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6818 }
6819 else
6820 break;
6821
6822 if (arg == error_mark_node)
6823 {
6824 if (complain & tf_error)
6825 error ("template argument %d is invalid", arg_idx + 1);
6826 }
6827 else if (!arg)
6828 /* This only occurs if there was an error in the template
6829 parameter list itself (which we would already have
6830 reported) that we are trying to recover from, e.g., a class
6831 template with a parameter list such as
6832 template<typename..., typename>. */
6833 ++lost;
6834 else
6835 arg = convert_template_argument (TREE_VALUE (parm),
6836 arg, new_args, complain,
6837 parm_idx, in_decl);
6838
6839 if (arg == error_mark_node)
6840 lost++;
6841 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6842 }
6843 cp_unevaluated_operand = saved_unevaluated_operand;
6844 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6845
6846 if (lost)
6847 return error_mark_node;
6848
6849 #ifdef ENABLE_CHECKING
6850 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6851 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6852 TREE_VEC_LENGTH (new_inner_args));
6853 #endif
6854
6855 return new_inner_args;
6856 }
6857
6858 /* Like coerce_template_parms. If PARMS represents all template
6859 parameters levels, this function returns a vector of vectors
6860 representing all the resulting argument levels. Note that in this
6861 case, only the innermost arguments are coerced because the
6862 outermost ones are supposed to have been coerced already.
6863
6864 Otherwise, if PARMS represents only (the innermost) vector of
6865 parameters, this function returns a vector containing just the
6866 innermost resulting arguments. */
6867
6868 static tree
6869 coerce_innermost_template_parms (tree parms,
6870 tree args,
6871 tree in_decl,
6872 tsubst_flags_t complain,
6873 bool require_all_args,
6874 bool use_default_args)
6875 {
6876 int parms_depth = TMPL_PARMS_DEPTH (parms);
6877 int args_depth = TMPL_ARGS_DEPTH (args);
6878 tree coerced_args;
6879
6880 if (parms_depth > 1)
6881 {
6882 coerced_args = make_tree_vec (parms_depth);
6883 tree level;
6884 int cur_depth;
6885
6886 for (level = parms, cur_depth = parms_depth;
6887 parms_depth > 0 && level != NULL_TREE;
6888 level = TREE_CHAIN (level), --cur_depth)
6889 {
6890 tree l;
6891 if (cur_depth == args_depth)
6892 l = coerce_template_parms (TREE_VALUE (level),
6893 args, in_decl, complain,
6894 require_all_args,
6895 use_default_args);
6896 else
6897 l = TMPL_ARGS_LEVEL (args, cur_depth);
6898
6899 if (l == error_mark_node)
6900 return error_mark_node;
6901
6902 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
6903 }
6904 }
6905 else
6906 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
6907 args, in_decl, complain,
6908 require_all_args,
6909 use_default_args);
6910 return coerced_args;
6911 }
6912
6913 /* Returns 1 if template args OT and NT are equivalent. */
6914
6915 static int
6916 template_args_equal (tree ot, tree nt)
6917 {
6918 if (nt == ot)
6919 return 1;
6920 if (nt == NULL_TREE || ot == NULL_TREE)
6921 return false;
6922
6923 if (TREE_CODE (nt) == TREE_VEC)
6924 /* For member templates */
6925 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6926 else if (PACK_EXPANSION_P (ot))
6927 return (PACK_EXPANSION_P (nt)
6928 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6929 PACK_EXPANSION_PATTERN (nt))
6930 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6931 PACK_EXPANSION_EXTRA_ARGS (nt)));
6932 else if (ARGUMENT_PACK_P (ot))
6933 {
6934 int i, len;
6935 tree opack, npack;
6936
6937 if (!ARGUMENT_PACK_P (nt))
6938 return 0;
6939
6940 opack = ARGUMENT_PACK_ARGS (ot);
6941 npack = ARGUMENT_PACK_ARGS (nt);
6942 len = TREE_VEC_LENGTH (opack);
6943 if (TREE_VEC_LENGTH (npack) != len)
6944 return 0;
6945 for (i = 0; i < len; ++i)
6946 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6947 TREE_VEC_ELT (npack, i)))
6948 return 0;
6949 return 1;
6950 }
6951 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6952 {
6953 /* We get here probably because we are in the middle of substituting
6954 into the pattern of a pack expansion. In that case the
6955 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6956 interested in. So we want to use the initial pack argument for
6957 the comparison. */
6958 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6959 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6960 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6961 return template_args_equal (ot, nt);
6962 }
6963 else if (TYPE_P (nt))
6964 return TYPE_P (ot) && same_type_p (ot, nt);
6965 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6966 return 0;
6967 else
6968 return cp_tree_equal (ot, nt);
6969 }
6970
6971 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6972 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6973 NEWARG_PTR with the offending arguments if they are non-NULL. */
6974
6975 static int
6976 comp_template_args_with_info (tree oldargs, tree newargs,
6977 tree *oldarg_ptr, tree *newarg_ptr)
6978 {
6979 int i;
6980
6981 if (oldargs == newargs)
6982 return 1;
6983
6984 if (!oldargs || !newargs)
6985 return 0;
6986
6987 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6988 return 0;
6989
6990 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6991 {
6992 tree nt = TREE_VEC_ELT (newargs, i);
6993 tree ot = TREE_VEC_ELT (oldargs, i);
6994
6995 if (! template_args_equal (ot, nt))
6996 {
6997 if (oldarg_ptr != NULL)
6998 *oldarg_ptr = ot;
6999 if (newarg_ptr != NULL)
7000 *newarg_ptr = nt;
7001 return 0;
7002 }
7003 }
7004 return 1;
7005 }
7006
7007 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7008 of template arguments. Returns 0 otherwise. */
7009
7010 int
7011 comp_template_args (tree oldargs, tree newargs)
7012 {
7013 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
7014 }
7015
7016 static void
7017 add_pending_template (tree d)
7018 {
7019 tree ti = (TYPE_P (d)
7020 ? CLASSTYPE_TEMPLATE_INFO (d)
7021 : DECL_TEMPLATE_INFO (d));
7022 struct pending_template *pt;
7023 int level;
7024
7025 if (TI_PENDING_TEMPLATE_FLAG (ti))
7026 return;
7027
7028 /* We are called both from instantiate_decl, where we've already had a
7029 tinst_level pushed, and instantiate_template, where we haven't.
7030 Compensate. */
7031 level = !current_tinst_level || current_tinst_level->decl != d;
7032
7033 if (level)
7034 push_tinst_level (d);
7035
7036 pt = ggc_alloc_pending_template ();
7037 pt->next = NULL;
7038 pt->tinst = current_tinst_level;
7039 if (last_pending_template)
7040 last_pending_template->next = pt;
7041 else
7042 pending_templates = pt;
7043
7044 last_pending_template = pt;
7045
7046 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
7047
7048 if (level)
7049 pop_tinst_level ();
7050 }
7051
7052
7053 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7054 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7055 documentation for TEMPLATE_ID_EXPR. */
7056
7057 tree
7058 lookup_template_function (tree fns, tree arglist)
7059 {
7060 tree type;
7061
7062 if (fns == error_mark_node || arglist == error_mark_node)
7063 return error_mark_node;
7064
7065 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
7066
7067 if (!is_overloaded_fn (fns) && !identifier_p (fns))
7068 {
7069 error ("%q#D is not a function template", fns);
7070 return error_mark_node;
7071 }
7072
7073 if (BASELINK_P (fns))
7074 {
7075 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
7076 unknown_type_node,
7077 BASELINK_FUNCTIONS (fns),
7078 arglist);
7079 return fns;
7080 }
7081
7082 type = TREE_TYPE (fns);
7083 if (TREE_CODE (fns) == OVERLOAD || !type)
7084 type = unknown_type_node;
7085
7086 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
7087 }
7088
7089 /* Within the scope of a template class S<T>, the name S gets bound
7090 (in build_self_reference) to a TYPE_DECL for the class, not a
7091 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7092 or one of its enclosing classes, and that type is a template,
7093 return the associated TEMPLATE_DECL. Otherwise, the original
7094 DECL is returned.
7095
7096 Also handle the case when DECL is a TREE_LIST of ambiguous
7097 injected-class-names from different bases. */
7098
7099 tree
7100 maybe_get_template_decl_from_type_decl (tree decl)
7101 {
7102 if (decl == NULL_TREE)
7103 return decl;
7104
7105 /* DR 176: A lookup that finds an injected-class-name (10.2
7106 [class.member.lookup]) can result in an ambiguity in certain cases
7107 (for example, if it is found in more than one base class). If all of
7108 the injected-class-names that are found refer to specializations of
7109 the same class template, and if the name is followed by a
7110 template-argument-list, the reference refers to the class template
7111 itself and not a specialization thereof, and is not ambiguous. */
7112 if (TREE_CODE (decl) == TREE_LIST)
7113 {
7114 tree t, tmpl = NULL_TREE;
7115 for (t = decl; t; t = TREE_CHAIN (t))
7116 {
7117 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
7118 if (!tmpl)
7119 tmpl = elt;
7120 else if (tmpl != elt)
7121 break;
7122 }
7123 if (tmpl && t == NULL_TREE)
7124 return tmpl;
7125 else
7126 return decl;
7127 }
7128
7129 return (decl != NULL_TREE
7130 && DECL_SELF_REFERENCE_P (decl)
7131 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
7132 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
7133 }
7134
7135 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
7136 parameters, find the desired type.
7137
7138 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7139
7140 IN_DECL, if non-NULL, is the template declaration we are trying to
7141 instantiate.
7142
7143 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7144 the class we are looking up.
7145
7146 Issue error and warning messages under control of COMPLAIN.
7147
7148 If the template class is really a local class in a template
7149 function, then the FUNCTION_CONTEXT is the function in which it is
7150 being instantiated.
7151
7152 ??? Note that this function is currently called *twice* for each
7153 template-id: the first time from the parser, while creating the
7154 incomplete type (finish_template_type), and the second type during the
7155 real instantiation (instantiate_template_class). This is surely something
7156 that we want to avoid. It also causes some problems with argument
7157 coercion (see convert_nontype_argument for more information on this). */
7158
7159 static tree
7160 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
7161 int entering_scope, tsubst_flags_t complain)
7162 {
7163 tree templ = NULL_TREE, parmlist;
7164 tree t;
7165 void **slot;
7166 spec_entry *entry;
7167 spec_entry elt;
7168 hashval_t hash;
7169
7170 if (identifier_p (d1))
7171 {
7172 tree value = innermost_non_namespace_value (d1);
7173 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7174 templ = value;
7175 else
7176 {
7177 if (context)
7178 push_decl_namespace (context);
7179 templ = lookup_name (d1);
7180 templ = maybe_get_template_decl_from_type_decl (templ);
7181 if (context)
7182 pop_decl_namespace ();
7183 }
7184 if (templ)
7185 context = DECL_CONTEXT (templ);
7186 }
7187 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7188 {
7189 tree type = TREE_TYPE (d1);
7190
7191 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7192 an implicit typename for the second A. Deal with it. */
7193 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7194 type = TREE_TYPE (type);
7195
7196 if (CLASSTYPE_TEMPLATE_INFO (type))
7197 {
7198 templ = CLASSTYPE_TI_TEMPLATE (type);
7199 d1 = DECL_NAME (templ);
7200 }
7201 }
7202 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7203 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7204 {
7205 templ = TYPE_TI_TEMPLATE (d1);
7206 d1 = DECL_NAME (templ);
7207 }
7208 else if (TREE_CODE (d1) == TEMPLATE_DECL
7209 && DECL_TEMPLATE_RESULT (d1)
7210 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7211 {
7212 templ = d1;
7213 d1 = DECL_NAME (templ);
7214 context = DECL_CONTEXT (templ);
7215 }
7216 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
7217 {
7218 templ = d1;
7219 d1 = DECL_NAME (templ);
7220 }
7221
7222 /* Issue an error message if we didn't find a template. */
7223 if (! templ)
7224 {
7225 if (complain & tf_error)
7226 error ("%qT is not a template", d1);
7227 return error_mark_node;
7228 }
7229
7230 if (TREE_CODE (templ) != TEMPLATE_DECL
7231 /* Make sure it's a user visible template, if it was named by
7232 the user. */
7233 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7234 && !PRIMARY_TEMPLATE_P (templ)))
7235 {
7236 if (complain & tf_error)
7237 {
7238 error ("non-template type %qT used as a template", d1);
7239 if (in_decl)
7240 error ("for template declaration %q+D", in_decl);
7241 }
7242 return error_mark_node;
7243 }
7244
7245 complain &= ~tf_user;
7246
7247 /* An alias that just changes the name of a template is equivalent to the
7248 other template, so if any of the arguments are pack expansions, strip
7249 the alias to avoid problems with a pack expansion passed to a non-pack
7250 alias template parameter (DR 1430). */
7251 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
7252 templ = get_underlying_template (templ);
7253
7254 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7255 {
7256 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7257 template arguments */
7258
7259 tree parm;
7260 tree arglist2;
7261 tree outer;
7262
7263 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7264
7265 /* Consider an example where a template template parameter declared as
7266
7267 template <class T, class U = std::allocator<T> > class TT
7268
7269 The template parameter level of T and U are one level larger than
7270 of TT. To proper process the default argument of U, say when an
7271 instantiation `TT<int>' is seen, we need to build the full
7272 arguments containing {int} as the innermost level. Outer levels,
7273 available when not appearing as default template argument, can be
7274 obtained from the arguments of the enclosing template.
7275
7276 Suppose that TT is later substituted with std::vector. The above
7277 instantiation is `TT<int, std::allocator<T> >' with TT at
7278 level 1, and T at level 2, while the template arguments at level 1
7279 becomes {std::vector} and the inner level 2 is {int}. */
7280
7281 outer = DECL_CONTEXT (templ);
7282 if (outer)
7283 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7284 else if (current_template_parms)
7285 /* This is an argument of the current template, so we haven't set
7286 DECL_CONTEXT yet. */
7287 outer = current_template_args ();
7288
7289 if (outer)
7290 arglist = add_to_template_args (outer, arglist);
7291
7292 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7293 complain,
7294 /*require_all_args=*/true,
7295 /*use_default_args=*/true);
7296 if (arglist2 == error_mark_node
7297 || (!uses_template_parms (arglist2)
7298 && check_instantiated_args (templ, arglist2, complain)))
7299 return error_mark_node;
7300
7301 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7302 return parm;
7303 }
7304 else
7305 {
7306 tree template_type = TREE_TYPE (templ);
7307 tree gen_tmpl;
7308 tree type_decl;
7309 tree found = NULL_TREE;
7310 int arg_depth;
7311 int parm_depth;
7312 int is_dependent_type;
7313 int use_partial_inst_tmpl = false;
7314
7315 if (template_type == error_mark_node)
7316 /* An error occurred while building the template TEMPL, and a
7317 diagnostic has most certainly been emitted for that
7318 already. Let's propagate that error. */
7319 return error_mark_node;
7320
7321 gen_tmpl = most_general_template (templ);
7322 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7323 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7324 arg_depth = TMPL_ARGS_DEPTH (arglist);
7325
7326 if (arg_depth == 1 && parm_depth > 1)
7327 {
7328 /* We've been given an incomplete set of template arguments.
7329 For example, given:
7330
7331 template <class T> struct S1 {
7332 template <class U> struct S2 {};
7333 template <class U> struct S2<U*> {};
7334 };
7335
7336 we will be called with an ARGLIST of `U*', but the
7337 TEMPLATE will be `template <class T> template
7338 <class U> struct S1<T>::S2'. We must fill in the missing
7339 arguments. */
7340 arglist
7341 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7342 arglist);
7343 arg_depth = TMPL_ARGS_DEPTH (arglist);
7344 }
7345
7346 /* Now we should have enough arguments. */
7347 gcc_assert (parm_depth == arg_depth);
7348
7349 /* From here on, we're only interested in the most general
7350 template. */
7351
7352 /* Calculate the BOUND_ARGS. These will be the args that are
7353 actually tsubst'd into the definition to create the
7354 instantiation. */
7355 if (parm_depth > 1)
7356 {
7357 /* We have multiple levels of arguments to coerce, at once. */
7358 int i;
7359 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7360
7361 tree bound_args = make_tree_vec (parm_depth);
7362
7363 for (i = saved_depth,
7364 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7365 i > 0 && t != NULL_TREE;
7366 --i, t = TREE_CHAIN (t))
7367 {
7368 tree a;
7369 if (i == saved_depth)
7370 a = coerce_template_parms (TREE_VALUE (t),
7371 arglist, gen_tmpl,
7372 complain,
7373 /*require_all_args=*/true,
7374 /*use_default_args=*/true);
7375 else
7376 /* Outer levels should have already been coerced. */
7377 a = TMPL_ARGS_LEVEL (arglist, i);
7378
7379 /* Don't process further if one of the levels fails. */
7380 if (a == error_mark_node)
7381 {
7382 /* Restore the ARGLIST to its full size. */
7383 TREE_VEC_LENGTH (arglist) = saved_depth;
7384 return error_mark_node;
7385 }
7386
7387 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7388
7389 /* We temporarily reduce the length of the ARGLIST so
7390 that coerce_template_parms will see only the arguments
7391 corresponding to the template parameters it is
7392 examining. */
7393 TREE_VEC_LENGTH (arglist)--;
7394 }
7395
7396 /* Restore the ARGLIST to its full size. */
7397 TREE_VEC_LENGTH (arglist) = saved_depth;
7398
7399 arglist = bound_args;
7400 }
7401 else
7402 arglist
7403 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7404 INNERMOST_TEMPLATE_ARGS (arglist),
7405 gen_tmpl,
7406 complain,
7407 /*require_all_args=*/true,
7408 /*use_default_args=*/true);
7409
7410 if (arglist == error_mark_node)
7411 /* We were unable to bind the arguments. */
7412 return error_mark_node;
7413
7414 /* In the scope of a template class, explicit references to the
7415 template class refer to the type of the template, not any
7416 instantiation of it. For example, in:
7417
7418 template <class T> class C { void f(C<T>); }
7419
7420 the `C<T>' is just the same as `C'. Outside of the
7421 class, however, such a reference is an instantiation. */
7422 if ((entering_scope
7423 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7424 || currently_open_class (template_type))
7425 /* comp_template_args is expensive, check it last. */
7426 && comp_template_args (TYPE_TI_ARGS (template_type),
7427 arglist))
7428 return template_type;
7429
7430 /* If we already have this specialization, return it. */
7431 elt.tmpl = gen_tmpl;
7432 elt.args = arglist;
7433 hash = hash_specialization (&elt);
7434 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7435 &elt, hash);
7436
7437 if (entry)
7438 return entry->spec;
7439
7440 is_dependent_type = uses_template_parms (arglist);
7441
7442 /* If the deduced arguments are invalid, then the binding
7443 failed. */
7444 if (!is_dependent_type
7445 && check_instantiated_args (gen_tmpl,
7446 INNERMOST_TEMPLATE_ARGS (arglist),
7447 complain))
7448 return error_mark_node;
7449
7450 if (!is_dependent_type
7451 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7452 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7453 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7454 {
7455 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7456 DECL_NAME (gen_tmpl),
7457 /*tag_scope=*/ts_global);
7458 return found;
7459 }
7460
7461 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7462 complain, in_decl);
7463 if (context == error_mark_node)
7464 return error_mark_node;
7465
7466 if (!context)
7467 context = global_namespace;
7468
7469 /* Create the type. */
7470 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7471 {
7472 /* The user referred to a specialization of an alias
7473 template represented by GEN_TMPL.
7474
7475 [temp.alias]/2 says:
7476
7477 When a template-id refers to the specialization of an
7478 alias template, it is equivalent to the associated
7479 type obtained by substitution of its
7480 template-arguments for the template-parameters in the
7481 type-id of the alias template. */
7482
7483 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7484 /* Note that the call above (by indirectly calling
7485 register_specialization in tsubst_decl) registers the
7486 TYPE_DECL representing the specialization of the alias
7487 template. So next time someone substitutes ARGLIST for
7488 the template parms into the alias template (GEN_TMPL),
7489 she'll get that TYPE_DECL back. */
7490
7491 if (t == error_mark_node)
7492 return t;
7493 }
7494 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7495 {
7496 if (!is_dependent_type)
7497 {
7498 set_current_access_from_decl (TYPE_NAME (template_type));
7499 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7500 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7501 arglist, complain, in_decl),
7502 SCOPED_ENUM_P (template_type), NULL);
7503 }
7504 else
7505 {
7506 /* We don't want to call start_enum for this type, since
7507 the values for the enumeration constants may involve
7508 template parameters. And, no one should be interested
7509 in the enumeration constants for such a type. */
7510 t = cxx_make_type (ENUMERAL_TYPE);
7511 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7512 }
7513 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7514 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7515 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7516 }
7517 else if (CLASS_TYPE_P (template_type))
7518 {
7519 t = make_class_type (TREE_CODE (template_type));
7520 CLASSTYPE_DECLARED_CLASS (t)
7521 = CLASSTYPE_DECLARED_CLASS (template_type);
7522 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7523 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7524
7525 /* A local class. Make sure the decl gets registered properly. */
7526 if (context == current_function_decl)
7527 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7528
7529 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7530 /* This instantiation is another name for the primary
7531 template type. Set the TYPE_CANONICAL field
7532 appropriately. */
7533 TYPE_CANONICAL (t) = template_type;
7534 else if (any_template_arguments_need_structural_equality_p (arglist))
7535 /* Some of the template arguments require structural
7536 equality testing, so this template class requires
7537 structural equality testing. */
7538 SET_TYPE_STRUCTURAL_EQUALITY (t);
7539 }
7540 else
7541 gcc_unreachable ();
7542
7543 /* If we called start_enum or pushtag above, this information
7544 will already be set up. */
7545 if (!TYPE_NAME (t))
7546 {
7547 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7548
7549 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7550 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7551 DECL_SOURCE_LOCATION (type_decl)
7552 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7553 }
7554 else
7555 type_decl = TYPE_NAME (t);
7556
7557 if (CLASS_TYPE_P (template_type))
7558 {
7559 TREE_PRIVATE (type_decl)
7560 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7561 TREE_PROTECTED (type_decl)
7562 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7563 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7564 {
7565 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7566 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7567 }
7568 }
7569
7570 /* Let's consider the explicit specialization of a member
7571 of a class template specialization that is implicitely instantiated,
7572 e.g.:
7573 template<class T>
7574 struct S
7575 {
7576 template<class U> struct M {}; //#0
7577 };
7578
7579 template<>
7580 template<>
7581 struct S<int>::M<char> //#1
7582 {
7583 int i;
7584 };
7585 [temp.expl.spec]/4 says this is valid.
7586
7587 In this case, when we write:
7588 S<int>::M<char> m;
7589
7590 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7591 the one of #0.
7592
7593 When we encounter #1, we want to store the partial instantiation
7594 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
7595
7596 For all cases other than this "explicit specialization of member of a
7597 class template", we just want to store the most general template into
7598 the CLASSTYPE_TI_TEMPLATE of M.
7599
7600 This case of "explicit specialization of member of a class template"
7601 only happens when:
7602 1/ the enclosing class is an instantiation of, and therefore not
7603 the same as, the context of the most general template, and
7604 2/ we aren't looking at the partial instantiation itself, i.e.
7605 the innermost arguments are not the same as the innermost parms of
7606 the most general template.
7607
7608 So it's only when 1/ and 2/ happens that we want to use the partial
7609 instantiation of the member template in lieu of its most general
7610 template. */
7611
7612 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7613 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7614 /* the enclosing class must be an instantiation... */
7615 && CLASS_TYPE_P (context)
7616 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7617 {
7618 tree partial_inst_args;
7619 TREE_VEC_LENGTH (arglist)--;
7620 ++processing_template_decl;
7621 partial_inst_args =
7622 tsubst (INNERMOST_TEMPLATE_ARGS
7623 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7624 arglist, complain, NULL_TREE);
7625 --processing_template_decl;
7626 TREE_VEC_LENGTH (arglist)++;
7627 use_partial_inst_tmpl =
7628 /*...and we must not be looking at the partial instantiation
7629 itself. */
7630 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7631 partial_inst_args);
7632 }
7633
7634 if (!use_partial_inst_tmpl)
7635 /* This case is easy; there are no member templates involved. */
7636 found = gen_tmpl;
7637 else
7638 {
7639 /* This is a full instantiation of a member template. Find
7640 the partial instantiation of which this is an instance. */
7641
7642 /* Temporarily reduce by one the number of levels in the ARGLIST
7643 so as to avoid comparing the last set of arguments. */
7644 TREE_VEC_LENGTH (arglist)--;
7645 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7646 TREE_VEC_LENGTH (arglist)++;
7647 /* FOUND is either a proper class type, or an alias
7648 template specialization. In the later case, it's a
7649 TYPE_DECL, resulting from the substituting of arguments
7650 for parameters in the TYPE_DECL of the alias template
7651 done earlier. So be careful while getting the template
7652 of FOUND. */
7653 found = TREE_CODE (found) == TYPE_DECL
7654 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7655 : CLASSTYPE_TI_TEMPLATE (found);
7656 }
7657
7658 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7659
7660 elt.spec = t;
7661 slot = htab_find_slot_with_hash (type_specializations,
7662 &elt, hash, INSERT);
7663 entry = ggc_alloc_spec_entry ();
7664 *entry = elt;
7665 *slot = entry;
7666
7667 /* Note this use of the partial instantiation so we can check it
7668 later in maybe_process_partial_specialization. */
7669 DECL_TEMPLATE_INSTANTIATIONS (templ)
7670 = tree_cons (arglist, t,
7671 DECL_TEMPLATE_INSTANTIATIONS (templ));
7672
7673 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
7674 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7675 /* Now that the type has been registered on the instantiations
7676 list, we set up the enumerators. Because the enumeration
7677 constants may involve the enumeration type itself, we make
7678 sure to register the type first, and then create the
7679 constants. That way, doing tsubst_expr for the enumeration
7680 constants won't result in recursive calls here; we'll find
7681 the instantiation and exit above. */
7682 tsubst_enum (template_type, t, arglist);
7683
7684 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7685 /* If the type makes use of template parameters, the
7686 code that generates debugging information will crash. */
7687 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
7688
7689 /* Possibly limit visibility based on template args. */
7690 TREE_PUBLIC (type_decl) = 1;
7691 determine_visibility (type_decl);
7692
7693 return t;
7694 }
7695 }
7696
7697 /* Wrapper for lookup_template_class_1. */
7698
7699 tree
7700 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7701 int entering_scope, tsubst_flags_t complain)
7702 {
7703 tree ret;
7704 timevar_push (TV_TEMPLATE_INST);
7705 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7706 entering_scope, complain);
7707 timevar_pop (TV_TEMPLATE_INST);
7708 return ret;
7709 }
7710 \f
7711 struct pair_fn_data
7712 {
7713 tree_fn_t fn;
7714 void *data;
7715 /* True when we should also visit template parameters that occur in
7716 non-deduced contexts. */
7717 bool include_nondeduced_p;
7718 struct pointer_set_t *visited;
7719 };
7720
7721 /* Called from for_each_template_parm via walk_tree. */
7722
7723 static tree
7724 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7725 {
7726 tree t = *tp;
7727 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7728 tree_fn_t fn = pfd->fn;
7729 void *data = pfd->data;
7730
7731 if (TYPE_P (t)
7732 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7733 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7734 pfd->include_nondeduced_p))
7735 return error_mark_node;
7736
7737 switch (TREE_CODE (t))
7738 {
7739 case RECORD_TYPE:
7740 if (TYPE_PTRMEMFUNC_P (t))
7741 break;
7742 /* Fall through. */
7743
7744 case UNION_TYPE:
7745 case ENUMERAL_TYPE:
7746 if (!TYPE_TEMPLATE_INFO (t))
7747 *walk_subtrees = 0;
7748 else if (for_each_template_parm (TYPE_TI_ARGS (t),
7749 fn, data, pfd->visited,
7750 pfd->include_nondeduced_p))
7751 return error_mark_node;
7752 break;
7753
7754 case INTEGER_TYPE:
7755 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7756 fn, data, pfd->visited,
7757 pfd->include_nondeduced_p)
7758 || for_each_template_parm (TYPE_MAX_VALUE (t),
7759 fn, data, pfd->visited,
7760 pfd->include_nondeduced_p))
7761 return error_mark_node;
7762 break;
7763
7764 case METHOD_TYPE:
7765 /* Since we're not going to walk subtrees, we have to do this
7766 explicitly here. */
7767 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7768 pfd->visited, pfd->include_nondeduced_p))
7769 return error_mark_node;
7770 /* Fall through. */
7771
7772 case FUNCTION_TYPE:
7773 /* Check the return type. */
7774 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7775 pfd->include_nondeduced_p))
7776 return error_mark_node;
7777
7778 /* Check the parameter types. Since default arguments are not
7779 instantiated until they are needed, the TYPE_ARG_TYPES may
7780 contain expressions that involve template parameters. But,
7781 no-one should be looking at them yet. And, once they're
7782 instantiated, they don't contain template parameters, so
7783 there's no point in looking at them then, either. */
7784 {
7785 tree parm;
7786
7787 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7788 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7789 pfd->visited, pfd->include_nondeduced_p))
7790 return error_mark_node;
7791
7792 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7793 want walk_tree walking into them itself. */
7794 *walk_subtrees = 0;
7795 }
7796 break;
7797
7798 case TYPEOF_TYPE:
7799 case UNDERLYING_TYPE:
7800 if (pfd->include_nondeduced_p
7801 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7802 pfd->visited,
7803 pfd->include_nondeduced_p))
7804 return error_mark_node;
7805 break;
7806
7807 case FUNCTION_DECL:
7808 case VAR_DECL:
7809 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7810 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7811 pfd->visited, pfd->include_nondeduced_p))
7812 return error_mark_node;
7813 /* Fall through. */
7814
7815 case PARM_DECL:
7816 case CONST_DECL:
7817 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7818 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7819 pfd->visited, pfd->include_nondeduced_p))
7820 return error_mark_node;
7821 if (DECL_CONTEXT (t)
7822 && pfd->include_nondeduced_p
7823 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7824 pfd->visited, pfd->include_nondeduced_p))
7825 return error_mark_node;
7826 break;
7827
7828 case BOUND_TEMPLATE_TEMPLATE_PARM:
7829 /* Record template parameters such as `T' inside `TT<T>'. */
7830 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7831 pfd->include_nondeduced_p))
7832 return error_mark_node;
7833 /* Fall through. */
7834
7835 case TEMPLATE_TEMPLATE_PARM:
7836 case TEMPLATE_TYPE_PARM:
7837 case TEMPLATE_PARM_INDEX:
7838 if (fn && (*fn)(t, data))
7839 return error_mark_node;
7840 else if (!fn)
7841 return error_mark_node;
7842 break;
7843
7844 case TEMPLATE_DECL:
7845 /* A template template parameter is encountered. */
7846 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7847 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7848 pfd->include_nondeduced_p))
7849 return error_mark_node;
7850
7851 /* Already substituted template template parameter */
7852 *walk_subtrees = 0;
7853 break;
7854
7855 case TYPENAME_TYPE:
7856 if (!fn
7857 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7858 data, pfd->visited,
7859 pfd->include_nondeduced_p))
7860 return error_mark_node;
7861 break;
7862
7863 case CONSTRUCTOR:
7864 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7865 && pfd->include_nondeduced_p
7866 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7867 (TREE_TYPE (t)), fn, data,
7868 pfd->visited, pfd->include_nondeduced_p))
7869 return error_mark_node;
7870 break;
7871
7872 case INDIRECT_REF:
7873 case COMPONENT_REF:
7874 /* If there's no type, then this thing must be some expression
7875 involving template parameters. */
7876 if (!fn && !TREE_TYPE (t))
7877 return error_mark_node;
7878 break;
7879
7880 case MODOP_EXPR:
7881 case CAST_EXPR:
7882 case IMPLICIT_CONV_EXPR:
7883 case REINTERPRET_CAST_EXPR:
7884 case CONST_CAST_EXPR:
7885 case STATIC_CAST_EXPR:
7886 case DYNAMIC_CAST_EXPR:
7887 case ARROW_EXPR:
7888 case DOTSTAR_EXPR:
7889 case TYPEID_EXPR:
7890 case PSEUDO_DTOR_EXPR:
7891 if (!fn)
7892 return error_mark_node;
7893 break;
7894
7895 default:
7896 break;
7897 }
7898
7899 /* We didn't find any template parameters we liked. */
7900 return NULL_TREE;
7901 }
7902
7903 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7904 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7905 call FN with the parameter and the DATA.
7906 If FN returns nonzero, the iteration is terminated, and
7907 for_each_template_parm returns 1. Otherwise, the iteration
7908 continues. If FN never returns a nonzero value, the value
7909 returned by for_each_template_parm is 0. If FN is NULL, it is
7910 considered to be the function which always returns 1.
7911
7912 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7913 parameters that occur in non-deduced contexts. When false, only
7914 visits those template parameters that can be deduced. */
7915
7916 static int
7917 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7918 struct pointer_set_t *visited,
7919 bool include_nondeduced_p)
7920 {
7921 struct pair_fn_data pfd;
7922 int result;
7923
7924 /* Set up. */
7925 pfd.fn = fn;
7926 pfd.data = data;
7927 pfd.include_nondeduced_p = include_nondeduced_p;
7928
7929 /* Walk the tree. (Conceptually, we would like to walk without
7930 duplicates, but for_each_template_parm_r recursively calls
7931 for_each_template_parm, so we would need to reorganize a fair
7932 bit to use walk_tree_without_duplicates, so we keep our own
7933 visited list.) */
7934 if (visited)
7935 pfd.visited = visited;
7936 else
7937 pfd.visited = pointer_set_create ();
7938 result = cp_walk_tree (&t,
7939 for_each_template_parm_r,
7940 &pfd,
7941 pfd.visited) != NULL_TREE;
7942
7943 /* Clean up. */
7944 if (!visited)
7945 {
7946 pointer_set_destroy (pfd.visited);
7947 pfd.visited = 0;
7948 }
7949
7950 return result;
7951 }
7952
7953 /* Returns true if T depends on any template parameter. */
7954
7955 int
7956 uses_template_parms (tree t)
7957 {
7958 bool dependent_p;
7959 int saved_processing_template_decl;
7960
7961 saved_processing_template_decl = processing_template_decl;
7962 if (!saved_processing_template_decl)
7963 processing_template_decl = 1;
7964 if (TYPE_P (t))
7965 dependent_p = dependent_type_p (t);
7966 else if (TREE_CODE (t) == TREE_VEC)
7967 dependent_p = any_dependent_template_arguments_p (t);
7968 else if (TREE_CODE (t) == TREE_LIST)
7969 dependent_p = (uses_template_parms (TREE_VALUE (t))
7970 || uses_template_parms (TREE_CHAIN (t)));
7971 else if (TREE_CODE (t) == TYPE_DECL)
7972 dependent_p = dependent_type_p (TREE_TYPE (t));
7973 else if (DECL_P (t)
7974 || EXPR_P (t)
7975 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7976 || TREE_CODE (t) == OVERLOAD
7977 || BASELINK_P (t)
7978 || identifier_p (t)
7979 || TREE_CODE (t) == TRAIT_EXPR
7980 || TREE_CODE (t) == CONSTRUCTOR
7981 || CONSTANT_CLASS_P (t))
7982 dependent_p = (type_dependent_expression_p (t)
7983 || value_dependent_expression_p (t));
7984 else
7985 {
7986 gcc_assert (t == error_mark_node);
7987 dependent_p = false;
7988 }
7989
7990 processing_template_decl = saved_processing_template_decl;
7991
7992 return dependent_p;
7993 }
7994
7995 /* Returns true iff current_function_decl is an incompletely instantiated
7996 template. Useful instead of processing_template_decl because the latter
7997 is set to 0 during fold_non_dependent_expr. */
7998
7999 bool
8000 in_template_function (void)
8001 {
8002 tree fn = current_function_decl;
8003 bool ret;
8004 ++processing_template_decl;
8005 ret = (fn && DECL_LANG_SPECIFIC (fn)
8006 && DECL_TEMPLATE_INFO (fn)
8007 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
8008 --processing_template_decl;
8009 return ret;
8010 }
8011
8012 /* Returns true if T depends on any template parameter with level LEVEL. */
8013
8014 int
8015 uses_template_parms_level (tree t, int level)
8016 {
8017 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
8018 /*include_nondeduced_p=*/true);
8019 }
8020
8021 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8022 ill-formed translation unit, i.e. a variable or function that isn't
8023 usable in a constant expression. */
8024
8025 static inline bool
8026 neglectable_inst_p (tree d)
8027 {
8028 return (DECL_P (d)
8029 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
8030 : decl_maybe_constant_var_p (d)));
8031 }
8032
8033 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8034 neglectable and instantiated from within an erroneous instantiation. */
8035
8036 static bool
8037 limit_bad_template_recursion (tree decl)
8038 {
8039 struct tinst_level *lev = current_tinst_level;
8040 int errs = errorcount + sorrycount;
8041 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
8042 return false;
8043
8044 for (; lev; lev = lev->next)
8045 if (neglectable_inst_p (lev->decl))
8046 break;
8047
8048 return (lev && errs > lev->errors);
8049 }
8050
8051 static int tinst_depth;
8052 extern int max_tinst_depth;
8053 int depth_reached;
8054
8055 static GTY(()) struct tinst_level *last_error_tinst_level;
8056
8057 /* We're starting to instantiate D; record the template instantiation context
8058 for diagnostics and to restore it later. */
8059
8060 int
8061 push_tinst_level (tree d)
8062 {
8063 struct tinst_level *new_level;
8064
8065 if (tinst_depth >= max_tinst_depth)
8066 {
8067 last_error_tinst_level = current_tinst_level;
8068 if (TREE_CODE (d) == TREE_LIST)
8069 error ("template instantiation depth exceeds maximum of %d (use "
8070 "-ftemplate-depth= to increase the maximum) substituting %qS",
8071 max_tinst_depth, d);
8072 else
8073 error ("template instantiation depth exceeds maximum of %d (use "
8074 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8075 max_tinst_depth, d);
8076
8077 print_instantiation_context ();
8078
8079 return 0;
8080 }
8081
8082 /* If the current instantiation caused problems, don't let it instantiate
8083 anything else. Do allow deduction substitution and decls usable in
8084 constant expressions. */
8085 if (limit_bad_template_recursion (d))
8086 return 0;
8087
8088 new_level = ggc_alloc_tinst_level ();
8089 new_level->decl = d;
8090 new_level->locus = input_location;
8091 new_level->errors = errorcount+sorrycount;
8092 new_level->in_system_header_p = in_system_header;
8093 new_level->next = current_tinst_level;
8094 current_tinst_level = new_level;
8095
8096 ++tinst_depth;
8097 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
8098 depth_reached = tinst_depth;
8099
8100 return 1;
8101 }
8102
8103 /* We're done instantiating this template; return to the instantiation
8104 context. */
8105
8106 void
8107 pop_tinst_level (void)
8108 {
8109 /* Restore the filename and line number stashed away when we started
8110 this instantiation. */
8111 input_location = current_tinst_level->locus;
8112 current_tinst_level = current_tinst_level->next;
8113 --tinst_depth;
8114 }
8115
8116 /* We're instantiating a deferred template; restore the template
8117 instantiation context in which the instantiation was requested, which
8118 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8119
8120 static tree
8121 reopen_tinst_level (struct tinst_level *level)
8122 {
8123 struct tinst_level *t;
8124
8125 tinst_depth = 0;
8126 for (t = level; t; t = t->next)
8127 ++tinst_depth;
8128
8129 current_tinst_level = level;
8130 pop_tinst_level ();
8131 if (current_tinst_level)
8132 current_tinst_level->errors = errorcount+sorrycount;
8133 return level->decl;
8134 }
8135
8136 /* Returns the TINST_LEVEL which gives the original instantiation
8137 context. */
8138
8139 struct tinst_level *
8140 outermost_tinst_level (void)
8141 {
8142 struct tinst_level *level = current_tinst_level;
8143 if (level)
8144 while (level->next)
8145 level = level->next;
8146 return level;
8147 }
8148
8149 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8150 vector of template arguments, as for tsubst.
8151
8152 Returns an appropriate tsubst'd friend declaration. */
8153
8154 static tree
8155 tsubst_friend_function (tree decl, tree args)
8156 {
8157 tree new_friend;
8158
8159 if (TREE_CODE (decl) == FUNCTION_DECL
8160 && DECL_TEMPLATE_INSTANTIATION (decl)
8161 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
8162 /* This was a friend declared with an explicit template
8163 argument list, e.g.:
8164
8165 friend void f<>(T);
8166
8167 to indicate that f was a template instantiation, not a new
8168 function declaration. Now, we have to figure out what
8169 instantiation of what template. */
8170 {
8171 tree template_id, arglist, fns;
8172 tree new_args;
8173 tree tmpl;
8174 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
8175
8176 /* Friend functions are looked up in the containing namespace scope.
8177 We must enter that scope, to avoid finding member functions of the
8178 current class with same name. */
8179 push_nested_namespace (ns);
8180 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
8181 tf_warning_or_error, NULL_TREE,
8182 /*integral_constant_expression_p=*/false);
8183 pop_nested_namespace (ns);
8184 arglist = tsubst (DECL_TI_ARGS (decl), args,
8185 tf_warning_or_error, NULL_TREE);
8186 template_id = lookup_template_function (fns, arglist);
8187
8188 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8189 tmpl = determine_specialization (template_id, new_friend,
8190 &new_args,
8191 /*need_member_template=*/0,
8192 TREE_VEC_LENGTH (args),
8193 tsk_none);
8194 return instantiate_template (tmpl, new_args, tf_error);
8195 }
8196
8197 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8198
8199 /* The NEW_FRIEND will look like an instantiation, to the
8200 compiler, but is not an instantiation from the point of view of
8201 the language. For example, we might have had:
8202
8203 template <class T> struct S {
8204 template <class U> friend void f(T, U);
8205 };
8206
8207 Then, in S<int>, template <class U> void f(int, U) is not an
8208 instantiation of anything. */
8209 if (new_friend == error_mark_node)
8210 return error_mark_node;
8211
8212 DECL_USE_TEMPLATE (new_friend) = 0;
8213 if (TREE_CODE (decl) == TEMPLATE_DECL)
8214 {
8215 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8216 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8217 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8218 }
8219
8220 /* The mangled name for the NEW_FRIEND is incorrect. The function
8221 is not a template instantiation and should not be mangled like
8222 one. Therefore, we forget the mangling here; we'll recompute it
8223 later if we need it. */
8224 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8225 {
8226 SET_DECL_RTL (new_friend, NULL);
8227 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8228 }
8229
8230 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8231 {
8232 tree old_decl;
8233 tree new_friend_template_info;
8234 tree new_friend_result_template_info;
8235 tree ns;
8236 int new_friend_is_defn;
8237
8238 /* We must save some information from NEW_FRIEND before calling
8239 duplicate decls since that function will free NEW_FRIEND if
8240 possible. */
8241 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8242 new_friend_is_defn =
8243 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8244 (template_for_substitution (new_friend)))
8245 != NULL_TREE);
8246 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8247 {
8248 /* This declaration is a `primary' template. */
8249 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8250
8251 new_friend_result_template_info
8252 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8253 }
8254 else
8255 new_friend_result_template_info = NULL_TREE;
8256
8257 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8258 if (new_friend_is_defn)
8259 DECL_INITIAL (new_friend) = error_mark_node;
8260
8261 /* Inside pushdecl_namespace_level, we will push into the
8262 current namespace. However, the friend function should go
8263 into the namespace of the template. */
8264 ns = decl_namespace_context (new_friend);
8265 push_nested_namespace (ns);
8266 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8267 pop_nested_namespace (ns);
8268
8269 if (old_decl == error_mark_node)
8270 return error_mark_node;
8271
8272 if (old_decl != new_friend)
8273 {
8274 /* This new friend declaration matched an existing
8275 declaration. For example, given:
8276
8277 template <class T> void f(T);
8278 template <class U> class C {
8279 template <class T> friend void f(T) {}
8280 };
8281
8282 the friend declaration actually provides the definition
8283 of `f', once C has been instantiated for some type. So,
8284 old_decl will be the out-of-class template declaration,
8285 while new_friend is the in-class definition.
8286
8287 But, if `f' was called before this point, the
8288 instantiation of `f' will have DECL_TI_ARGS corresponding
8289 to `T' but not to `U', references to which might appear
8290 in the definition of `f'. Previously, the most general
8291 template for an instantiation of `f' was the out-of-class
8292 version; now it is the in-class version. Therefore, we
8293 run through all specialization of `f', adding to their
8294 DECL_TI_ARGS appropriately. In particular, they need a
8295 new set of outer arguments, corresponding to the
8296 arguments for this class instantiation.
8297
8298 The same situation can arise with something like this:
8299
8300 friend void f(int);
8301 template <class T> class C {
8302 friend void f(T) {}
8303 };
8304
8305 when `C<int>' is instantiated. Now, `f(int)' is defined
8306 in the class. */
8307
8308 if (!new_friend_is_defn)
8309 /* On the other hand, if the in-class declaration does
8310 *not* provide a definition, then we don't want to alter
8311 existing definitions. We can just leave everything
8312 alone. */
8313 ;
8314 else
8315 {
8316 tree new_template = TI_TEMPLATE (new_friend_template_info);
8317 tree new_args = TI_ARGS (new_friend_template_info);
8318
8319 /* Overwrite whatever template info was there before, if
8320 any, with the new template information pertaining to
8321 the declaration. */
8322 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8323
8324 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8325 {
8326 /* We should have called reregister_specialization in
8327 duplicate_decls. */
8328 gcc_assert (retrieve_specialization (new_template,
8329 new_args, 0)
8330 == old_decl);
8331
8332 /* Instantiate it if the global has already been used. */
8333 if (DECL_ODR_USED (old_decl))
8334 instantiate_decl (old_decl, /*defer_ok=*/true,
8335 /*expl_inst_class_mem_p=*/false);
8336 }
8337 else
8338 {
8339 tree t;
8340
8341 /* Indicate that the old function template is a partial
8342 instantiation. */
8343 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8344 = new_friend_result_template_info;
8345
8346 gcc_assert (new_template
8347 == most_general_template (new_template));
8348 gcc_assert (new_template != old_decl);
8349
8350 /* Reassign any specializations already in the hash table
8351 to the new more general template, and add the
8352 additional template args. */
8353 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8354 t != NULL_TREE;
8355 t = TREE_CHAIN (t))
8356 {
8357 tree spec = TREE_VALUE (t);
8358 spec_entry elt;
8359
8360 elt.tmpl = old_decl;
8361 elt.args = DECL_TI_ARGS (spec);
8362 elt.spec = NULL_TREE;
8363
8364 htab_remove_elt (decl_specializations, &elt);
8365
8366 DECL_TI_ARGS (spec)
8367 = add_outermost_template_args (new_args,
8368 DECL_TI_ARGS (spec));
8369
8370 register_specialization
8371 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8372
8373 }
8374 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8375 }
8376 }
8377
8378 /* The information from NEW_FRIEND has been merged into OLD_DECL
8379 by duplicate_decls. */
8380 new_friend = old_decl;
8381 }
8382 }
8383 else
8384 {
8385 tree context = DECL_CONTEXT (new_friend);
8386 bool dependent_p;
8387
8388 /* In the code
8389 template <class T> class C {
8390 template <class U> friend void C1<U>::f (); // case 1
8391 friend void C2<T>::f (); // case 2
8392 };
8393 we only need to make sure CONTEXT is a complete type for
8394 case 2. To distinguish between the two cases, we note that
8395 CONTEXT of case 1 remains dependent type after tsubst while
8396 this isn't true for case 2. */
8397 ++processing_template_decl;
8398 dependent_p = dependent_type_p (context);
8399 --processing_template_decl;
8400
8401 if (!dependent_p
8402 && !complete_type_or_else (context, NULL_TREE))
8403 return error_mark_node;
8404
8405 if (COMPLETE_TYPE_P (context))
8406 {
8407 /* Check to see that the declaration is really present, and,
8408 possibly obtain an improved declaration. */
8409 tree fn = check_classfn (context,
8410 new_friend, NULL_TREE);
8411
8412 if (fn)
8413 new_friend = fn;
8414 }
8415 }
8416
8417 return new_friend;
8418 }
8419
8420 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8421 template arguments, as for tsubst.
8422
8423 Returns an appropriate tsubst'd friend type or error_mark_node on
8424 failure. */
8425
8426 static tree
8427 tsubst_friend_class (tree friend_tmpl, tree args)
8428 {
8429 tree friend_type;
8430 tree tmpl;
8431 tree context;
8432
8433 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8434 {
8435 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8436 return TREE_TYPE (t);
8437 }
8438
8439 context = CP_DECL_CONTEXT (friend_tmpl);
8440
8441 if (context != global_namespace)
8442 {
8443 if (TREE_CODE (context) == NAMESPACE_DECL)
8444 push_nested_namespace (context);
8445 else
8446 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8447 }
8448
8449 /* Look for a class template declaration. We look for hidden names
8450 because two friend declarations of the same template are the
8451 same. For example, in:
8452
8453 struct A {
8454 template <typename> friend class F;
8455 };
8456 template <typename> struct B {
8457 template <typename> friend class F;
8458 };
8459
8460 both F templates are the same. */
8461 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8462 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8463
8464 /* But, if we don't find one, it might be because we're in a
8465 situation like this:
8466
8467 template <class T>
8468 struct S {
8469 template <class U>
8470 friend struct S;
8471 };
8472
8473 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8474 for `S<int>', not the TEMPLATE_DECL. */
8475 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8476 {
8477 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8478 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8479 }
8480
8481 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8482 {
8483 /* The friend template has already been declared. Just
8484 check to see that the declarations match, and install any new
8485 default parameters. We must tsubst the default parameters,
8486 of course. We only need the innermost template parameters
8487 because that is all that redeclare_class_template will look
8488 at. */
8489 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8490 > TMPL_ARGS_DEPTH (args))
8491 {
8492 tree parms;
8493 location_t saved_input_location;
8494 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8495 args, tf_warning_or_error);
8496
8497 saved_input_location = input_location;
8498 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8499 redeclare_class_template (TREE_TYPE (tmpl), parms);
8500 input_location = saved_input_location;
8501
8502 }
8503
8504 friend_type = TREE_TYPE (tmpl);
8505 }
8506 else
8507 {
8508 /* The friend template has not already been declared. In this
8509 case, the instantiation of the template class will cause the
8510 injection of this template into the global scope. */
8511 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8512 if (tmpl == error_mark_node)
8513 return error_mark_node;
8514
8515 /* The new TMPL is not an instantiation of anything, so we
8516 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8517 the new type because that is supposed to be the corresponding
8518 template decl, i.e., TMPL. */
8519 DECL_USE_TEMPLATE (tmpl) = 0;
8520 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8521 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8522 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8523 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8524
8525 /* Inject this template into the global scope. */
8526 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8527 }
8528
8529 if (context != global_namespace)
8530 {
8531 if (TREE_CODE (context) == NAMESPACE_DECL)
8532 pop_nested_namespace (context);
8533 else
8534 pop_nested_class ();
8535 }
8536
8537 return friend_type;
8538 }
8539
8540 /* Returns zero if TYPE cannot be completed later due to circularity.
8541 Otherwise returns one. */
8542
8543 static int
8544 can_complete_type_without_circularity (tree type)
8545 {
8546 if (type == NULL_TREE || type == error_mark_node)
8547 return 0;
8548 else if (COMPLETE_TYPE_P (type))
8549 return 1;
8550 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8551 return can_complete_type_without_circularity (TREE_TYPE (type));
8552 else if (CLASS_TYPE_P (type)
8553 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8554 return 0;
8555 else
8556 return 1;
8557 }
8558
8559 static tree tsubst_omp_clauses (tree, bool, tree, tsubst_flags_t, tree);
8560
8561 /* Apply any attributes which had to be deferred until instantiation
8562 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8563 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8564
8565 static void
8566 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8567 tree args, tsubst_flags_t complain, tree in_decl)
8568 {
8569 tree last_dep = NULL_TREE;
8570 tree t;
8571 tree *p;
8572
8573 for (t = attributes; t; t = TREE_CHAIN (t))
8574 if (ATTR_IS_DEPENDENT (t))
8575 {
8576 last_dep = t;
8577 attributes = copy_list (attributes);
8578 break;
8579 }
8580
8581 if (DECL_P (*decl_p))
8582 {
8583 if (TREE_TYPE (*decl_p) == error_mark_node)
8584 return;
8585 p = &DECL_ATTRIBUTES (*decl_p);
8586 }
8587 else
8588 p = &TYPE_ATTRIBUTES (*decl_p);
8589
8590 if (last_dep)
8591 {
8592 tree late_attrs = NULL_TREE;
8593 tree *q = &late_attrs;
8594
8595 for (*p = attributes; *p; )
8596 {
8597 t = *p;
8598 if (ATTR_IS_DEPENDENT (t))
8599 {
8600 *p = TREE_CHAIN (t);
8601 TREE_CHAIN (t) = NULL_TREE;
8602 if (flag_openmp
8603 && is_attribute_p ("omp declare simd",
8604 get_attribute_name (t))
8605 && TREE_VALUE (t))
8606 {
8607 tree clauses = TREE_VALUE (TREE_VALUE (t));
8608 clauses = tsubst_omp_clauses (clauses, true, args,
8609 complain, in_decl);
8610 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
8611 clauses = finish_omp_clauses (clauses);
8612 tree parms = DECL_ARGUMENTS (*decl_p);
8613 clauses
8614 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
8615 if (clauses)
8616 TREE_VALUE (TREE_VALUE (t)) = clauses;
8617 else
8618 TREE_VALUE (t) = NULL_TREE;
8619 }
8620 /* If the first attribute argument is an identifier, don't
8621 pass it through tsubst. Attributes like mode, format,
8622 cleanup and several target specific attributes expect it
8623 unmodified. */
8624 else if (attribute_takes_identifier_p (get_attribute_name (t)))
8625 {
8626 tree chain
8627 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8628 in_decl,
8629 /*integral_constant_expression_p=*/false);
8630 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8631 TREE_VALUE (t)
8632 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8633 chain);
8634 }
8635 else
8636 TREE_VALUE (t)
8637 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8638 /*integral_constant_expression_p=*/false);
8639 *q = t;
8640 q = &TREE_CHAIN (t);
8641 }
8642 else
8643 p = &TREE_CHAIN (t);
8644 }
8645
8646 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8647 }
8648 }
8649
8650 /* Perform (or defer) access check for typedefs that were referenced
8651 from within the template TMPL code.
8652 This is a subroutine of instantiate_decl and instantiate_class_template.
8653 TMPL is the template to consider and TARGS is the list of arguments of
8654 that template. */
8655
8656 static void
8657 perform_typedefs_access_check (tree tmpl, tree targs)
8658 {
8659 location_t saved_location;
8660 unsigned i;
8661 qualified_typedef_usage_t *iter;
8662
8663 if (!tmpl
8664 || (!CLASS_TYPE_P (tmpl)
8665 && TREE_CODE (tmpl) != FUNCTION_DECL))
8666 return;
8667
8668 saved_location = input_location;
8669 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8670 {
8671 tree type_decl = iter->typedef_decl;
8672 tree type_scope = iter->context;
8673
8674 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8675 continue;
8676
8677 if (uses_template_parms (type_decl))
8678 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8679 if (uses_template_parms (type_scope))
8680 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8681
8682 /* Make access check error messages point to the location
8683 of the use of the typedef. */
8684 input_location = iter->locus;
8685 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8686 type_decl, type_decl,
8687 tf_warning_or_error);
8688 }
8689 input_location = saved_location;
8690 }
8691
8692 static tree
8693 instantiate_class_template_1 (tree type)
8694 {
8695 tree templ, args, pattern, t, member;
8696 tree typedecl;
8697 tree pbinfo;
8698 tree base_list;
8699 unsigned int saved_maximum_field_alignment;
8700 tree fn_context;
8701
8702 if (type == error_mark_node)
8703 return error_mark_node;
8704
8705 if (COMPLETE_OR_OPEN_TYPE_P (type)
8706 || uses_template_parms (type))
8707 return type;
8708
8709 /* Figure out which template is being instantiated. */
8710 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8711 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8712
8713 /* Determine what specialization of the original template to
8714 instantiate. */
8715 t = most_specialized_class (type, templ, tf_warning_or_error);
8716 if (t == error_mark_node)
8717 {
8718 TYPE_BEING_DEFINED (type) = 1;
8719 return error_mark_node;
8720 }
8721 else if (t)
8722 {
8723 /* This TYPE is actually an instantiation of a partial
8724 specialization. We replace the innermost set of ARGS with
8725 the arguments appropriate for substitution. For example,
8726 given:
8727
8728 template <class T> struct S {};
8729 template <class T> struct S<T*> {};
8730
8731 and supposing that we are instantiating S<int*>, ARGS will
8732 presently be {int*} -- but we need {int}. */
8733 pattern = TREE_TYPE (t);
8734 args = TREE_PURPOSE (t);
8735 }
8736 else
8737 {
8738 pattern = TREE_TYPE (templ);
8739 args = CLASSTYPE_TI_ARGS (type);
8740 }
8741
8742 /* If the template we're instantiating is incomplete, then clearly
8743 there's nothing we can do. */
8744 if (!COMPLETE_TYPE_P (pattern))
8745 return type;
8746
8747 /* If we've recursively instantiated too many templates, stop. */
8748 if (! push_tinst_level (type))
8749 return type;
8750
8751 /* Now we're really doing the instantiation. Mark the type as in
8752 the process of being defined. */
8753 TYPE_BEING_DEFINED (type) = 1;
8754
8755 /* We may be in the middle of deferred access check. Disable
8756 it now. */
8757 push_deferring_access_checks (dk_no_deferred);
8758
8759 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8760 if (!fn_context)
8761 push_to_top_level ();
8762 /* Use #pragma pack from the template context. */
8763 saved_maximum_field_alignment = maximum_field_alignment;
8764 maximum_field_alignment = TYPE_PRECISION (pattern);
8765
8766 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8767
8768 /* Set the input location to the most specialized template definition.
8769 This is needed if tsubsting causes an error. */
8770 typedecl = TYPE_MAIN_DECL (pattern);
8771 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8772 DECL_SOURCE_LOCATION (typedecl);
8773
8774 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8775 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8776 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8777 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8778 if (ANON_AGGR_TYPE_P (pattern))
8779 SET_ANON_AGGR_TYPE_P (type);
8780 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8781 {
8782 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8783 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8784 /* Adjust visibility for template arguments. */
8785 determine_visibility (TYPE_MAIN_DECL (type));
8786 }
8787 if (CLASS_TYPE_P (type))
8788 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8789
8790 pbinfo = TYPE_BINFO (pattern);
8791
8792 /* We should never instantiate a nested class before its enclosing
8793 class; we need to look up the nested class by name before we can
8794 instantiate it, and that lookup should instantiate the enclosing
8795 class. */
8796 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8797 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8798
8799 base_list = NULL_TREE;
8800 if (BINFO_N_BASE_BINFOS (pbinfo))
8801 {
8802 tree pbase_binfo;
8803 tree pushed_scope;
8804 int i;
8805
8806 /* We must enter the scope containing the type, as that is where
8807 the accessibility of types named in dependent bases are
8808 looked up from. */
8809 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8810
8811 /* Substitute into each of the bases to determine the actual
8812 basetypes. */
8813 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8814 {
8815 tree base;
8816 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8817 tree expanded_bases = NULL_TREE;
8818 int idx, len = 1;
8819
8820 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8821 {
8822 expanded_bases =
8823 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8824 args, tf_error, NULL_TREE);
8825 if (expanded_bases == error_mark_node)
8826 continue;
8827
8828 len = TREE_VEC_LENGTH (expanded_bases);
8829 }
8830
8831 for (idx = 0; idx < len; idx++)
8832 {
8833 if (expanded_bases)
8834 /* Extract the already-expanded base class. */
8835 base = TREE_VEC_ELT (expanded_bases, idx);
8836 else
8837 /* Substitute to figure out the base class. */
8838 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8839 NULL_TREE);
8840
8841 if (base == error_mark_node)
8842 continue;
8843
8844 base_list = tree_cons (access, base, base_list);
8845 if (BINFO_VIRTUAL_P (pbase_binfo))
8846 TREE_TYPE (base_list) = integer_type_node;
8847 }
8848 }
8849
8850 /* The list is now in reverse order; correct that. */
8851 base_list = nreverse (base_list);
8852
8853 if (pushed_scope)
8854 pop_scope (pushed_scope);
8855 }
8856 /* Now call xref_basetypes to set up all the base-class
8857 information. */
8858 xref_basetypes (type, base_list);
8859
8860 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8861 (int) ATTR_FLAG_TYPE_IN_PLACE,
8862 args, tf_error, NULL_TREE);
8863 fixup_attribute_variants (type);
8864
8865 /* Now that our base classes are set up, enter the scope of the
8866 class, so that name lookups into base classes, etc. will work
8867 correctly. This is precisely analogous to what we do in
8868 begin_class_definition when defining an ordinary non-template
8869 class, except we also need to push the enclosing classes. */
8870 push_nested_class (type);
8871
8872 /* Now members are processed in the order of declaration. */
8873 for (member = CLASSTYPE_DECL_LIST (pattern);
8874 member; member = TREE_CHAIN (member))
8875 {
8876 tree t = TREE_VALUE (member);
8877
8878 if (TREE_PURPOSE (member))
8879 {
8880 if (TYPE_P (t))
8881 {
8882 /* Build new CLASSTYPE_NESTED_UTDS. */
8883
8884 tree newtag;
8885 bool class_template_p;
8886
8887 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8888 && TYPE_LANG_SPECIFIC (t)
8889 && CLASSTYPE_IS_TEMPLATE (t));
8890 /* If the member is a class template, then -- even after
8891 substitution -- there may be dependent types in the
8892 template argument list for the class. We increment
8893 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8894 that function will assume that no types are dependent
8895 when outside of a template. */
8896 if (class_template_p)
8897 ++processing_template_decl;
8898 newtag = tsubst (t, args, tf_error, NULL_TREE);
8899 if (class_template_p)
8900 --processing_template_decl;
8901 if (newtag == error_mark_node)
8902 continue;
8903
8904 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8905 {
8906 tree name = TYPE_IDENTIFIER (t);
8907
8908 if (class_template_p)
8909 /* Unfortunately, lookup_template_class sets
8910 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8911 instantiation (i.e., for the type of a member
8912 template class nested within a template class.)
8913 This behavior is required for
8914 maybe_process_partial_specialization to work
8915 correctly, but is not accurate in this case;
8916 the TAG is not an instantiation of anything.
8917 (The corresponding TEMPLATE_DECL is an
8918 instantiation, but the TYPE is not.) */
8919 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8920
8921 /* Now, we call pushtag to put this NEWTAG into the scope of
8922 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8923 pushtag calling push_template_decl. We don't have to do
8924 this for enums because it will already have been done in
8925 tsubst_enum. */
8926 if (name)
8927 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8928 pushtag (name, newtag, /*tag_scope=*/ts_current);
8929 }
8930 }
8931 else if (DECL_DECLARES_FUNCTION_P (t))
8932 {
8933 /* Build new TYPE_METHODS. */
8934 tree r;
8935
8936 if (TREE_CODE (t) == TEMPLATE_DECL)
8937 ++processing_template_decl;
8938 r = tsubst (t, args, tf_error, NULL_TREE);
8939 if (TREE_CODE (t) == TEMPLATE_DECL)
8940 --processing_template_decl;
8941 set_current_access_from_decl (r);
8942 finish_member_declaration (r);
8943 /* Instantiate members marked with attribute used. */
8944 if (r != error_mark_node && DECL_PRESERVE_P (r))
8945 mark_used (r);
8946 if (TREE_CODE (r) == FUNCTION_DECL
8947 && DECL_OMP_DECLARE_REDUCTION_P (r))
8948 cp_check_omp_declare_reduction (r);
8949 }
8950 else
8951 {
8952 /* Build new TYPE_FIELDS. */
8953 if (TREE_CODE (t) == STATIC_ASSERT)
8954 {
8955 tree condition;
8956
8957 ++c_inhibit_evaluation_warnings;
8958 condition =
8959 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8960 tf_warning_or_error, NULL_TREE,
8961 /*integral_constant_expression_p=*/true);
8962 --c_inhibit_evaluation_warnings;
8963
8964 finish_static_assert (condition,
8965 STATIC_ASSERT_MESSAGE (t),
8966 STATIC_ASSERT_SOURCE_LOCATION (t),
8967 /*member_p=*/true);
8968 }
8969 else if (TREE_CODE (t) != CONST_DECL)
8970 {
8971 tree r;
8972 tree vec = NULL_TREE;
8973 int len = 1;
8974
8975 /* The file and line for this declaration, to
8976 assist in error message reporting. Since we
8977 called push_tinst_level above, we don't need to
8978 restore these. */
8979 input_location = DECL_SOURCE_LOCATION (t);
8980
8981 if (TREE_CODE (t) == TEMPLATE_DECL)
8982 ++processing_template_decl;
8983 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8984 if (TREE_CODE (t) == TEMPLATE_DECL)
8985 --processing_template_decl;
8986
8987 if (TREE_CODE (r) == TREE_VEC)
8988 {
8989 /* A capture pack became multiple fields. */
8990 vec = r;
8991 len = TREE_VEC_LENGTH (vec);
8992 }
8993
8994 for (int i = 0; i < len; ++i)
8995 {
8996 if (vec)
8997 r = TREE_VEC_ELT (vec, i);
8998 if (VAR_P (r))
8999 {
9000 /* In [temp.inst]:
9001
9002 [t]he initialization (and any associated
9003 side-effects) of a static data member does
9004 not occur unless the static data member is
9005 itself used in a way that requires the
9006 definition of the static data member to
9007 exist.
9008
9009 Therefore, we do not substitute into the
9010 initialized for the static data member here. */
9011 finish_static_data_member_decl
9012 (r,
9013 /*init=*/NULL_TREE,
9014 /*init_const_expr_p=*/false,
9015 /*asmspec_tree=*/NULL_TREE,
9016 /*flags=*/0);
9017 /* Instantiate members marked with attribute used. */
9018 if (r != error_mark_node && DECL_PRESERVE_P (r))
9019 mark_used (r);
9020 }
9021 else if (TREE_CODE (r) == FIELD_DECL)
9022 {
9023 /* Determine whether R has a valid type and can be
9024 completed later. If R is invalid, then its type
9025 is replaced by error_mark_node. */
9026 tree rtype = TREE_TYPE (r);
9027 if (can_complete_type_without_circularity (rtype))
9028 complete_type (rtype);
9029
9030 if (!COMPLETE_TYPE_P (rtype))
9031 {
9032 cxx_incomplete_type_error (r, rtype);
9033 TREE_TYPE (r) = error_mark_node;
9034 }
9035 }
9036
9037 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9038 such a thing will already have been added to the field
9039 list by tsubst_enum in finish_member_declaration in the
9040 CLASSTYPE_NESTED_UTDS case above. */
9041 if (!(TREE_CODE (r) == TYPE_DECL
9042 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
9043 && DECL_ARTIFICIAL (r)))
9044 {
9045 set_current_access_from_decl (r);
9046 finish_member_declaration (r);
9047 }
9048 }
9049 }
9050 }
9051 }
9052 else
9053 {
9054 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
9055 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9056 {
9057 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9058
9059 tree friend_type = t;
9060 bool adjust_processing_template_decl = false;
9061
9062 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9063 {
9064 /* template <class T> friend class C; */
9065 friend_type = tsubst_friend_class (friend_type, args);
9066 adjust_processing_template_decl = true;
9067 }
9068 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
9069 {
9070 /* template <class T> friend class C::D; */
9071 friend_type = tsubst (friend_type, args,
9072 tf_warning_or_error, NULL_TREE);
9073 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
9074 friend_type = TREE_TYPE (friend_type);
9075 adjust_processing_template_decl = true;
9076 }
9077 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
9078 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
9079 {
9080 /* This could be either
9081
9082 friend class T::C;
9083
9084 when dependent_type_p is false or
9085
9086 template <class U> friend class T::C;
9087
9088 otherwise. */
9089 friend_type = tsubst (friend_type, args,
9090 tf_warning_or_error, NULL_TREE);
9091 /* Bump processing_template_decl for correct
9092 dependent_type_p calculation. */
9093 ++processing_template_decl;
9094 if (dependent_type_p (friend_type))
9095 adjust_processing_template_decl = true;
9096 --processing_template_decl;
9097 }
9098 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
9099 && hidden_name_p (TYPE_NAME (friend_type)))
9100 {
9101 /* friend class C;
9102
9103 where C hasn't been declared yet. Let's lookup name
9104 from namespace scope directly, bypassing any name that
9105 come from dependent base class. */
9106 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
9107
9108 /* The call to xref_tag_from_type does injection for friend
9109 classes. */
9110 push_nested_namespace (ns);
9111 friend_type =
9112 xref_tag_from_type (friend_type, NULL_TREE,
9113 /*tag_scope=*/ts_current);
9114 pop_nested_namespace (ns);
9115 }
9116 else if (uses_template_parms (friend_type))
9117 /* friend class C<T>; */
9118 friend_type = tsubst (friend_type, args,
9119 tf_warning_or_error, NULL_TREE);
9120 /* Otherwise it's
9121
9122 friend class C;
9123
9124 where C is already declared or
9125
9126 friend class C<int>;
9127
9128 We don't have to do anything in these cases. */
9129
9130 if (adjust_processing_template_decl)
9131 /* Trick make_friend_class into realizing that the friend
9132 we're adding is a template, not an ordinary class. It's
9133 important that we use make_friend_class since it will
9134 perform some error-checking and output cross-reference
9135 information. */
9136 ++processing_template_decl;
9137
9138 if (friend_type != error_mark_node)
9139 make_friend_class (type, friend_type, /*complain=*/false);
9140
9141 if (adjust_processing_template_decl)
9142 --processing_template_decl;
9143 }
9144 else
9145 {
9146 /* Build new DECL_FRIENDLIST. */
9147 tree r;
9148
9149 /* The file and line for this declaration, to
9150 assist in error message reporting. Since we
9151 called push_tinst_level above, we don't need to
9152 restore these. */
9153 input_location = DECL_SOURCE_LOCATION (t);
9154
9155 if (TREE_CODE (t) == TEMPLATE_DECL)
9156 {
9157 ++processing_template_decl;
9158 push_deferring_access_checks (dk_no_check);
9159 }
9160
9161 r = tsubst_friend_function (t, args);
9162 add_friend (type, r, /*complain=*/false);
9163 if (TREE_CODE (t) == TEMPLATE_DECL)
9164 {
9165 pop_deferring_access_checks ();
9166 --processing_template_decl;
9167 }
9168 }
9169 }
9170 }
9171
9172 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
9173 {
9174 tree decl = lambda_function (type);
9175 if (decl)
9176 {
9177 if (!DECL_TEMPLATE_INFO (decl)
9178 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
9179 instantiate_decl (decl, false, false);
9180
9181 /* We need to instantiate the capture list from the template
9182 after we've instantiated the closure members, but before we
9183 consider adding the conversion op. Also keep any captures
9184 that may have been added during instantiation of the op(). */
9185 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
9186 tree tmpl_cap
9187 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
9188 args, tf_warning_or_error, NULL_TREE,
9189 false, false);
9190
9191 LAMBDA_EXPR_CAPTURE_LIST (expr)
9192 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
9193
9194 maybe_add_lambda_conv_op (type);
9195 }
9196 else
9197 gcc_assert (errorcount);
9198 }
9199
9200 /* Set the file and line number information to whatever is given for
9201 the class itself. This puts error messages involving generated
9202 implicit functions at a predictable point, and the same point
9203 that would be used for non-template classes. */
9204 input_location = DECL_SOURCE_LOCATION (typedecl);
9205
9206 unreverse_member_declarations (type);
9207 finish_struct_1 (type);
9208 TYPE_BEING_DEFINED (type) = 0;
9209
9210 /* We don't instantiate default arguments for member functions. 14.7.1:
9211
9212 The implicit instantiation of a class template specialization causes
9213 the implicit instantiation of the declarations, but not of the
9214 definitions or default arguments, of the class member functions,
9215 member classes, static data members and member templates.... */
9216
9217 /* Some typedefs referenced from within the template code need to be access
9218 checked at template instantiation time, i.e now. These types were
9219 added to the template at parsing time. Let's get those and perform
9220 the access checks then. */
9221 perform_typedefs_access_check (pattern, args);
9222 perform_deferred_access_checks (tf_warning_or_error);
9223 pop_nested_class ();
9224 maximum_field_alignment = saved_maximum_field_alignment;
9225 if (!fn_context)
9226 pop_from_top_level ();
9227 pop_deferring_access_checks ();
9228 pop_tinst_level ();
9229
9230 /* The vtable for a template class can be emitted in any translation
9231 unit in which the class is instantiated. When there is no key
9232 method, however, finish_struct_1 will already have added TYPE to
9233 the keyed_classes list. */
9234 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9235 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9236
9237 return type;
9238 }
9239
9240 /* Wrapper for instantiate_class_template_1. */
9241
9242 tree
9243 instantiate_class_template (tree type)
9244 {
9245 tree ret;
9246 timevar_push (TV_TEMPLATE_INST);
9247 ret = instantiate_class_template_1 (type);
9248 timevar_pop (TV_TEMPLATE_INST);
9249 return ret;
9250 }
9251
9252 static tree
9253 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9254 {
9255 tree r;
9256
9257 if (!t)
9258 r = t;
9259 else if (TYPE_P (t))
9260 r = tsubst (t, args, complain, in_decl);
9261 else
9262 {
9263 if (!(complain & tf_warning))
9264 ++c_inhibit_evaluation_warnings;
9265 r = tsubst_expr (t, args, complain, in_decl,
9266 /*integral_constant_expression_p=*/true);
9267 if (!(complain & tf_warning))
9268 --c_inhibit_evaluation_warnings;
9269 /* Preserve the raw-reference nature of T. */
9270 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9271 && REFERENCE_REF_P (r))
9272 r = TREE_OPERAND (r, 0);
9273 }
9274 return r;
9275 }
9276
9277 /* Given a function parameter pack TMPL_PARM and some function parameters
9278 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9279 and set *SPEC_P to point at the next point in the list. */
9280
9281 static tree
9282 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9283 {
9284 /* Collect all of the extra "packed" parameters into an
9285 argument pack. */
9286 tree parmvec;
9287 tree parmtypevec;
9288 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9289 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9290 tree spec_parm = *spec_p;
9291 int i, len;
9292
9293 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9294 if (tmpl_parm
9295 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9296 break;
9297
9298 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9299 parmvec = make_tree_vec (len);
9300 parmtypevec = make_tree_vec (len);
9301 spec_parm = *spec_p;
9302 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9303 {
9304 TREE_VEC_ELT (parmvec, i) = spec_parm;
9305 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9306 }
9307
9308 /* Build the argument packs. */
9309 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9310 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9311 TREE_TYPE (argpack) = argtypepack;
9312 *spec_p = spec_parm;
9313
9314 return argpack;
9315 }
9316
9317 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9318 NONTYPE_ARGUMENT_PACK. */
9319
9320 static tree
9321 make_fnparm_pack (tree spec_parm)
9322 {
9323 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9324 }
9325
9326 /* Return true iff the Ith element of the argument pack ARG_PACK is a
9327 pack expansion. */
9328
9329 static bool
9330 argument_pack_element_is_expansion_p (tree arg_pack, int i)
9331 {
9332 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
9333 if (i >= TREE_VEC_LENGTH (vec))
9334 return false;
9335 return PACK_EXPANSION_P (TREE_VEC_ELT (vec, i));
9336 }
9337
9338
9339 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
9340
9341 static tree
9342 make_argument_pack_select (tree arg_pack, unsigned index)
9343 {
9344 tree aps = make_node (ARGUMENT_PACK_SELECT);
9345
9346 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
9347 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9348
9349 return aps;
9350 }
9351
9352 /* This is a subroutine of tsubst_pack_expansion.
9353
9354 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
9355 mechanism to store the (non complete list of) arguments of the
9356 substitution and return a non substituted pack expansion, in order
9357 to wait for when we have enough arguments to really perform the
9358 substitution. */
9359
9360 static bool
9361 use_pack_expansion_extra_args_p (tree parm_packs,
9362 int arg_pack_len,
9363 bool has_empty_arg)
9364 {
9365 /* If one pack has an expansion and another pack has a normal
9366 argument or if one pack has an empty argument and an another
9367 one hasn't then tsubst_pack_expansion cannot perform the
9368 substitution and need to fall back on the
9369 PACK_EXPANSION_EXTRA mechanism. */
9370 if (parm_packs == NULL_TREE)
9371 return false;
9372 else if (has_empty_arg)
9373 return true;
9374
9375 bool has_expansion_arg = false;
9376 for (int i = 0 ; i < arg_pack_len; ++i)
9377 {
9378 bool has_non_expansion_arg = false;
9379 for (tree parm_pack = parm_packs;
9380 parm_pack;
9381 parm_pack = TREE_CHAIN (parm_pack))
9382 {
9383 tree arg = TREE_VALUE (parm_pack);
9384
9385 if (argument_pack_element_is_expansion_p (arg, i))
9386 has_expansion_arg = true;
9387 else
9388 has_non_expansion_arg = true;
9389 }
9390
9391 if (has_expansion_arg && has_non_expansion_arg)
9392 return true;
9393 }
9394 return false;
9395 }
9396
9397 /* [temp.variadic]/6 says that:
9398
9399 The instantiation of a pack expansion [...]
9400 produces a list E1,E2, ..., En, where N is the number of elements
9401 in the pack expansion parameters.
9402
9403 This subroutine of tsubst_pack_expansion produces one of these Ei.
9404
9405 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
9406 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
9407 PATTERN, and each TREE_VALUE is its corresponding argument pack.
9408 INDEX is the index 'i' of the element Ei to produce. ARGS,
9409 COMPLAIN, and IN_DECL are the same parameters as for the
9410 tsubst_pack_expansion function.
9411
9412 The function returns the resulting Ei upon successful completion,
9413 or error_mark_node.
9414
9415 Note that this function possibly modifies the ARGS parameter, so
9416 it's the responsibility of the caller to restore it. */
9417
9418 static tree
9419 gen_elem_of_pack_expansion_instantiation (tree pattern,
9420 tree parm_packs,
9421 unsigned index,
9422 tree args /* This parm gets
9423 modified. */,
9424 tsubst_flags_t complain,
9425 tree in_decl)
9426 {
9427 tree t;
9428 bool ith_elem_is_expansion = false;
9429
9430 /* For each parameter pack, change the substitution of the parameter
9431 pack to the ith argument in its argument pack, then expand the
9432 pattern. */
9433 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
9434 {
9435 tree parm = TREE_PURPOSE (pack);
9436 tree arg_pack = TREE_VALUE (pack);
9437 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
9438
9439 ith_elem_is_expansion |=
9440 argument_pack_element_is_expansion_p (arg_pack, index);
9441
9442 /* Select the Ith argument from the pack. */
9443 if (TREE_CODE (parm) == PARM_DECL
9444 || TREE_CODE (parm) == FIELD_DECL)
9445 {
9446 if (index == 0)
9447 {
9448 aps = make_argument_pack_select (arg_pack, index);
9449 mark_used (parm);
9450 register_local_specialization (aps, parm);
9451 }
9452 else
9453 aps = retrieve_local_specialization (parm);
9454 }
9455 else
9456 {
9457 int idx, level;
9458 template_parm_level_and_index (parm, &level, &idx);
9459
9460 if (index == 0)
9461 {
9462 aps = make_argument_pack_select (arg_pack, index);
9463 /* Update the corresponding argument. */
9464 TMPL_ARG (args, level, idx) = aps;
9465 }
9466 else
9467 /* Re-use the ARGUMENT_PACK_SELECT. */
9468 aps = TMPL_ARG (args, level, idx);
9469 }
9470 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
9471 }
9472
9473 /* Substitute into the PATTERN with the (possibly altered)
9474 arguments. */
9475 if (!TYPE_P (pattern))
9476 t = tsubst_expr (pattern, args, complain, in_decl,
9477 /*integral_constant_expression_p=*/false);
9478 else
9479 t = tsubst (pattern, args, complain, in_decl);
9480
9481 /* If the Ith argument pack element is a pack expansion, then
9482 the Ith element resulting from the substituting is going to
9483 be a pack expansion as well. */
9484 if (ith_elem_is_expansion)
9485 t = make_pack_expansion (t);
9486
9487 return t;
9488 }
9489
9490 /* Substitute ARGS into T, which is an pack expansion
9491 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9492 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9493 (if only a partial substitution could be performed) or
9494 ERROR_MARK_NODE if there was an error. */
9495 tree
9496 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9497 tree in_decl)
9498 {
9499 tree pattern;
9500 tree pack, packs = NULL_TREE;
9501 bool unsubstituted_packs = false;
9502 int i, len = -1;
9503 tree result;
9504 struct pointer_map_t *saved_local_specializations = NULL;
9505 bool need_local_specializations = false;
9506 int levels;
9507
9508 gcc_assert (PACK_EXPANSION_P (t));
9509 pattern = PACK_EXPANSION_PATTERN (t);
9510
9511 /* Add in any args remembered from an earlier partial instantiation. */
9512 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9513
9514 levels = TMPL_ARGS_DEPTH (args);
9515
9516 /* Determine the argument packs that will instantiate the parameter
9517 packs used in the expansion expression. While we're at it,
9518 compute the number of arguments to be expanded and make sure it
9519 is consistent. */
9520 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9521 pack = TREE_CHAIN (pack))
9522 {
9523 tree parm_pack = TREE_VALUE (pack);
9524 tree arg_pack = NULL_TREE;
9525 tree orig_arg = NULL_TREE;
9526 int level = 0;
9527
9528 if (TREE_CODE (parm_pack) == BASES)
9529 {
9530 if (BASES_DIRECT (parm_pack))
9531 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9532 args, complain, in_decl, false));
9533 else
9534 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9535 args, complain, in_decl, false));
9536 }
9537 if (TREE_CODE (parm_pack) == PARM_DECL)
9538 {
9539 if (PACK_EXPANSION_LOCAL_P (t))
9540 arg_pack = retrieve_local_specialization (parm_pack);
9541 else
9542 {
9543 /* We can't rely on local_specializations for a parameter
9544 name used later in a function declaration (such as in a
9545 late-specified return type). Even if it exists, it might
9546 have the wrong value for a recursive call. Just make a
9547 dummy decl, since it's only used for its type. */
9548 arg_pack = tsubst_decl (parm_pack, args, complain);
9549 if (arg_pack && DECL_PACK_P (arg_pack))
9550 /* Partial instantiation of the parm_pack, we can't build
9551 up an argument pack yet. */
9552 arg_pack = NULL_TREE;
9553 else
9554 arg_pack = make_fnparm_pack (arg_pack);
9555 need_local_specializations = true;
9556 }
9557 }
9558 else if (TREE_CODE (parm_pack) == FIELD_DECL)
9559 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
9560 else
9561 {
9562 int idx;
9563 template_parm_level_and_index (parm_pack, &level, &idx);
9564
9565 if (level <= levels)
9566 arg_pack = TMPL_ARG (args, level, idx);
9567 }
9568
9569 orig_arg = arg_pack;
9570 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9571 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9572
9573 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9574 /* This can only happen if we forget to expand an argument
9575 pack somewhere else. Just return an error, silently. */
9576 {
9577 result = make_tree_vec (1);
9578 TREE_VEC_ELT (result, 0) = error_mark_node;
9579 return result;
9580 }
9581
9582 if (arg_pack)
9583 {
9584 int my_len =
9585 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9586
9587 /* Don't bother trying to do a partial substitution with
9588 incomplete packs; we'll try again after deduction. */
9589 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9590 return t;
9591
9592 if (len < 0)
9593 len = my_len;
9594 else if (len != my_len)
9595 {
9596 if (!(complain & tf_error))
9597 /* Fail quietly. */;
9598 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9599 error ("mismatched argument pack lengths while expanding "
9600 "%<%T%>",
9601 pattern);
9602 else
9603 error ("mismatched argument pack lengths while expanding "
9604 "%<%E%>",
9605 pattern);
9606 return error_mark_node;
9607 }
9608
9609 /* Keep track of the parameter packs and their corresponding
9610 argument packs. */
9611 packs = tree_cons (parm_pack, arg_pack, packs);
9612 TREE_TYPE (packs) = orig_arg;
9613 }
9614 else
9615 {
9616 /* We can't substitute for this parameter pack. We use a flag as
9617 well as the missing_level counter because function parameter
9618 packs don't have a level. */
9619 unsubstituted_packs = true;
9620 }
9621 }
9622
9623 /* We cannot expand this expansion expression, because we don't have
9624 all of the argument packs we need. */
9625 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
9626 {
9627 /* We got some full packs, but we can't substitute them in until we
9628 have values for all the packs. So remember these until then. */
9629
9630 t = make_pack_expansion (pattern);
9631 PACK_EXPANSION_EXTRA_ARGS (t) = args;
9632 return t;
9633 }
9634 else if (unsubstituted_packs)
9635 {
9636 /* There were no real arguments, we're just replacing a parameter
9637 pack with another version of itself. Substitute into the
9638 pattern and return a PACK_EXPANSION_*. The caller will need to
9639 deal with that. */
9640 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9641 t = tsubst_expr (pattern, args, complain, in_decl,
9642 /*integral_constant_expression_p=*/false);
9643 else
9644 t = tsubst (pattern, args, complain, in_decl);
9645 t = make_pack_expansion (t);
9646 return t;
9647 }
9648
9649 gcc_assert (len >= 0);
9650
9651 if (need_local_specializations)
9652 {
9653 /* We're in a late-specified return type, so create our own local
9654 specializations map; the current map is either NULL or (in the
9655 case of recursive unification) might have bindings that we don't
9656 want to use or alter. */
9657 saved_local_specializations = local_specializations;
9658 local_specializations = pointer_map_create ();
9659 }
9660
9661 /* For each argument in each argument pack, substitute into the
9662 pattern. */
9663 result = make_tree_vec (len);
9664 for (i = 0; i < len; ++i)
9665 {
9666 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
9667 i,
9668 args, complain,
9669 in_decl);
9670 TREE_VEC_ELT (result, i) = t;
9671 if (t == error_mark_node)
9672 {
9673 result = error_mark_node;
9674 break;
9675 }
9676 }
9677
9678 /* Update ARGS to restore the substitution from parameter packs to
9679 their argument packs. */
9680 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9681 {
9682 tree parm = TREE_PURPOSE (pack);
9683
9684 if (TREE_CODE (parm) == PARM_DECL
9685 || TREE_CODE (parm) == FIELD_DECL)
9686 register_local_specialization (TREE_TYPE (pack), parm);
9687 else
9688 {
9689 int idx, level;
9690
9691 if (TREE_VALUE (pack) == NULL_TREE)
9692 continue;
9693
9694 template_parm_level_and_index (parm, &level, &idx);
9695
9696 /* Update the corresponding argument. */
9697 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9698 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9699 TREE_TYPE (pack);
9700 else
9701 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9702 }
9703 }
9704
9705 if (need_local_specializations)
9706 {
9707 pointer_map_destroy (local_specializations);
9708 local_specializations = saved_local_specializations;
9709 }
9710
9711 return result;
9712 }
9713
9714 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9715 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9716 parameter packs; all parms generated from a function parameter pack will
9717 have the same DECL_PARM_INDEX. */
9718
9719 tree
9720 get_pattern_parm (tree parm, tree tmpl)
9721 {
9722 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9723 tree patparm;
9724
9725 if (DECL_ARTIFICIAL (parm))
9726 {
9727 for (patparm = DECL_ARGUMENTS (pattern);
9728 patparm; patparm = DECL_CHAIN (patparm))
9729 if (DECL_ARTIFICIAL (patparm)
9730 && DECL_NAME (parm) == DECL_NAME (patparm))
9731 break;
9732 }
9733 else
9734 {
9735 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9736 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9737 gcc_assert (DECL_PARM_INDEX (patparm)
9738 == DECL_PARM_INDEX (parm));
9739 }
9740
9741 return patparm;
9742 }
9743
9744 /* Substitute ARGS into the vector or list of template arguments T. */
9745
9746 static tree
9747 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9748 {
9749 tree orig_t = t;
9750 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9751 tree *elts;
9752
9753 if (t == error_mark_node)
9754 return error_mark_node;
9755
9756 len = TREE_VEC_LENGTH (t);
9757 elts = XALLOCAVEC (tree, len);
9758
9759 for (i = 0; i < len; i++)
9760 {
9761 tree orig_arg = TREE_VEC_ELT (t, i);
9762 tree new_arg;
9763
9764 if (TREE_CODE (orig_arg) == TREE_VEC)
9765 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9766 else if (PACK_EXPANSION_P (orig_arg))
9767 {
9768 /* Substitute into an expansion expression. */
9769 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9770
9771 if (TREE_CODE (new_arg) == TREE_VEC)
9772 /* Add to the expanded length adjustment the number of
9773 expanded arguments. We subtract one from this
9774 measurement, because the argument pack expression
9775 itself is already counted as 1 in
9776 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9777 the argument pack is empty. */
9778 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9779 }
9780 else if (ARGUMENT_PACK_P (orig_arg))
9781 {
9782 /* Substitute into each of the arguments. */
9783 new_arg = TYPE_P (orig_arg)
9784 ? cxx_make_type (TREE_CODE (orig_arg))
9785 : make_node (TREE_CODE (orig_arg));
9786
9787 SET_ARGUMENT_PACK_ARGS (
9788 new_arg,
9789 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9790 args, complain, in_decl));
9791
9792 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9793 new_arg = error_mark_node;
9794
9795 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9796 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9797 complain, in_decl);
9798 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9799
9800 if (TREE_TYPE (new_arg) == error_mark_node)
9801 new_arg = error_mark_node;
9802 }
9803 }
9804 else
9805 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9806
9807 if (new_arg == error_mark_node)
9808 return error_mark_node;
9809
9810 elts[i] = new_arg;
9811 if (new_arg != orig_arg)
9812 need_new = 1;
9813 }
9814
9815 if (!need_new)
9816 return t;
9817
9818 /* Make space for the expanded arguments coming from template
9819 argument packs. */
9820 t = make_tree_vec (len + expanded_len_adjust);
9821 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9822 arguments for a member template.
9823 In that case each TREE_VEC in ORIG_T represents a level of template
9824 arguments, and ORIG_T won't carry any non defaulted argument count.
9825 It will rather be the nested TREE_VECs that will carry one.
9826 In other words, ORIG_T carries a non defaulted argument count only
9827 if it doesn't contain any nested TREE_VEC. */
9828 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9829 {
9830 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9831 count += expanded_len_adjust;
9832 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9833 }
9834 for (i = 0, out = 0; i < len; i++)
9835 {
9836 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9837 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9838 && TREE_CODE (elts[i]) == TREE_VEC)
9839 {
9840 int idx;
9841
9842 /* Now expand the template argument pack "in place". */
9843 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9844 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9845 }
9846 else
9847 {
9848 TREE_VEC_ELT (t, out) = elts[i];
9849 out++;
9850 }
9851 }
9852
9853 return t;
9854 }
9855
9856 /* Return the result of substituting ARGS into the template parameters
9857 given by PARMS. If there are m levels of ARGS and m + n levels of
9858 PARMS, then the result will contain n levels of PARMS. For
9859 example, if PARMS is `template <class T> template <class U>
9860 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9861 result will be `template <int*, double, class V>'. */
9862
9863 static tree
9864 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9865 {
9866 tree r = NULL_TREE;
9867 tree* new_parms;
9868
9869 /* When substituting into a template, we must set
9870 PROCESSING_TEMPLATE_DECL as the template parameters may be
9871 dependent if they are based on one-another, and the dependency
9872 predicates are short-circuit outside of templates. */
9873 ++processing_template_decl;
9874
9875 for (new_parms = &r;
9876 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9877 new_parms = &(TREE_CHAIN (*new_parms)),
9878 parms = TREE_CHAIN (parms))
9879 {
9880 tree new_vec =
9881 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9882 int i;
9883
9884 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9885 {
9886 tree tuple;
9887
9888 if (parms == error_mark_node)
9889 continue;
9890
9891 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9892
9893 if (tuple == error_mark_node)
9894 continue;
9895
9896 TREE_VEC_ELT (new_vec, i) =
9897 tsubst_template_parm (tuple, args, complain);
9898 }
9899
9900 *new_parms =
9901 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9902 - TMPL_ARGS_DEPTH (args)),
9903 new_vec, NULL_TREE);
9904 }
9905
9906 --processing_template_decl;
9907
9908 return r;
9909 }
9910
9911 /* Return the result of substituting ARGS into one template parameter
9912 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9913 parameter and which TREE_PURPOSE is the default argument of the
9914 template parameter. */
9915
9916 static tree
9917 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9918 {
9919 tree default_value, parm_decl;
9920
9921 if (args == NULL_TREE
9922 || t == NULL_TREE
9923 || t == error_mark_node)
9924 return t;
9925
9926 gcc_assert (TREE_CODE (t) == TREE_LIST);
9927
9928 default_value = TREE_PURPOSE (t);
9929 parm_decl = TREE_VALUE (t);
9930
9931 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9932 if (TREE_CODE (parm_decl) == PARM_DECL
9933 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9934 parm_decl = error_mark_node;
9935 default_value = tsubst_template_arg (default_value, args,
9936 complain, NULL_TREE);
9937
9938 return build_tree_list (default_value, parm_decl);
9939 }
9940
9941 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9942 type T. If T is not an aggregate or enumeration type, it is
9943 handled as if by tsubst. IN_DECL is as for tsubst. If
9944 ENTERING_SCOPE is nonzero, T is the context for a template which
9945 we are presently tsubst'ing. Return the substituted value. */
9946
9947 static tree
9948 tsubst_aggr_type (tree t,
9949 tree args,
9950 tsubst_flags_t complain,
9951 tree in_decl,
9952 int entering_scope)
9953 {
9954 if (t == NULL_TREE)
9955 return NULL_TREE;
9956
9957 switch (TREE_CODE (t))
9958 {
9959 case RECORD_TYPE:
9960 if (TYPE_PTRMEMFUNC_P (t))
9961 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9962
9963 /* Else fall through. */
9964 case ENUMERAL_TYPE:
9965 case UNION_TYPE:
9966 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9967 {
9968 tree argvec;
9969 tree context;
9970 tree r;
9971 int saved_unevaluated_operand;
9972 int saved_inhibit_evaluation_warnings;
9973
9974 /* In "sizeof(X<I>)" we need to evaluate "I". */
9975 saved_unevaluated_operand = cp_unevaluated_operand;
9976 cp_unevaluated_operand = 0;
9977 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9978 c_inhibit_evaluation_warnings = 0;
9979
9980 /* First, determine the context for the type we are looking
9981 up. */
9982 context = TYPE_CONTEXT (t);
9983 if (context && TYPE_P (context))
9984 {
9985 context = tsubst_aggr_type (context, args, complain,
9986 in_decl, /*entering_scope=*/1);
9987 /* If context is a nested class inside a class template,
9988 it may still need to be instantiated (c++/33959). */
9989 context = complete_type (context);
9990 }
9991
9992 /* Then, figure out what arguments are appropriate for the
9993 type we are trying to find. For example, given:
9994
9995 template <class T> struct S;
9996 template <class T, class U> void f(T, U) { S<U> su; }
9997
9998 and supposing that we are instantiating f<int, double>,
9999 then our ARGS will be {int, double}, but, when looking up
10000 S we only want {double}. */
10001 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
10002 complain, in_decl);
10003 if (argvec == error_mark_node)
10004 r = error_mark_node;
10005 else
10006 {
10007 r = lookup_template_class (t, argvec, in_decl, context,
10008 entering_scope, complain);
10009 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10010 }
10011
10012 cp_unevaluated_operand = saved_unevaluated_operand;
10013 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10014
10015 return r;
10016 }
10017 else
10018 /* This is not a template type, so there's nothing to do. */
10019 return t;
10020
10021 default:
10022 return tsubst (t, args, complain, in_decl);
10023 }
10024 }
10025
10026 /* Substitute into the default argument ARG (a default argument for
10027 FN), which has the indicated TYPE. */
10028
10029 tree
10030 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
10031 {
10032 tree saved_class_ptr = NULL_TREE;
10033 tree saved_class_ref = NULL_TREE;
10034 int errs = errorcount + sorrycount;
10035
10036 /* This can happen in invalid code. */
10037 if (TREE_CODE (arg) == DEFAULT_ARG)
10038 return arg;
10039
10040 /* This default argument came from a template. Instantiate the
10041 default argument here, not in tsubst. In the case of
10042 something like:
10043
10044 template <class T>
10045 struct S {
10046 static T t();
10047 void f(T = t());
10048 };
10049
10050 we must be careful to do name lookup in the scope of S<T>,
10051 rather than in the current class. */
10052 push_access_scope (fn);
10053 /* The "this" pointer is not valid in a default argument. */
10054 if (cfun)
10055 {
10056 saved_class_ptr = current_class_ptr;
10057 cp_function_chain->x_current_class_ptr = NULL_TREE;
10058 saved_class_ref = current_class_ref;
10059 cp_function_chain->x_current_class_ref = NULL_TREE;
10060 }
10061
10062 push_deferring_access_checks(dk_no_deferred);
10063 /* The default argument expression may cause implicitly defined
10064 member functions to be synthesized, which will result in garbage
10065 collection. We must treat this situation as if we were within
10066 the body of function so as to avoid collecting live data on the
10067 stack. */
10068 ++function_depth;
10069 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
10070 complain, NULL_TREE,
10071 /*integral_constant_expression_p=*/false);
10072 --function_depth;
10073 pop_deferring_access_checks();
10074
10075 /* Restore the "this" pointer. */
10076 if (cfun)
10077 {
10078 cp_function_chain->x_current_class_ptr = saved_class_ptr;
10079 cp_function_chain->x_current_class_ref = saved_class_ref;
10080 }
10081
10082 if (errorcount+sorrycount > errs
10083 && (complain & tf_warning_or_error))
10084 inform (input_location,
10085 " when instantiating default argument for call to %D", fn);
10086
10087 /* Make sure the default argument is reasonable. */
10088 arg = check_default_argument (type, arg, complain);
10089
10090 pop_access_scope (fn);
10091
10092 return arg;
10093 }
10094
10095 /* Substitute into all the default arguments for FN. */
10096
10097 static void
10098 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
10099 {
10100 tree arg;
10101 tree tmpl_args;
10102
10103 tmpl_args = DECL_TI_ARGS (fn);
10104
10105 /* If this function is not yet instantiated, we certainly don't need
10106 its default arguments. */
10107 if (uses_template_parms (tmpl_args))
10108 return;
10109 /* Don't do this again for clones. */
10110 if (DECL_CLONED_FUNCTION_P (fn))
10111 return;
10112
10113 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
10114 arg;
10115 arg = TREE_CHAIN (arg))
10116 if (TREE_PURPOSE (arg))
10117 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
10118 TREE_VALUE (arg),
10119 TREE_PURPOSE (arg),
10120 complain);
10121 }
10122
10123 /* Substitute the ARGS into the T, which is a _DECL. Return the
10124 result of the substitution. Issue error and warning messages under
10125 control of COMPLAIN. */
10126
10127 static tree
10128 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
10129 {
10130 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10131 location_t saved_loc;
10132 tree r = NULL_TREE;
10133 tree in_decl = t;
10134 hashval_t hash = 0;
10135
10136 /* Set the filename and linenumber to improve error-reporting. */
10137 saved_loc = input_location;
10138 input_location = DECL_SOURCE_LOCATION (t);
10139
10140 switch (TREE_CODE (t))
10141 {
10142 case TEMPLATE_DECL:
10143 {
10144 /* We can get here when processing a member function template,
10145 member class template, or template template parameter. */
10146 tree decl = DECL_TEMPLATE_RESULT (t);
10147 tree spec;
10148 tree tmpl_args;
10149 tree full_args;
10150
10151 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10152 {
10153 /* Template template parameter is treated here. */
10154 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10155 if (new_type == error_mark_node)
10156 RETURN (error_mark_node);
10157 /* If we get a real template back, return it. This can happen in
10158 the context of most_specialized_class. */
10159 if (TREE_CODE (new_type) == TEMPLATE_DECL)
10160 return new_type;
10161
10162 r = copy_decl (t);
10163 DECL_CHAIN (r) = NULL_TREE;
10164 TREE_TYPE (r) = new_type;
10165 DECL_TEMPLATE_RESULT (r)
10166 = build_decl (DECL_SOURCE_LOCATION (decl),
10167 TYPE_DECL, DECL_NAME (decl), new_type);
10168 DECL_TEMPLATE_PARMS (r)
10169 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10170 complain);
10171 TYPE_NAME (new_type) = r;
10172 break;
10173 }
10174
10175 /* We might already have an instance of this template.
10176 The ARGS are for the surrounding class type, so the
10177 full args contain the tsubst'd args for the context,
10178 plus the innermost args from the template decl. */
10179 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
10180 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
10181 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
10182 /* Because this is a template, the arguments will still be
10183 dependent, even after substitution. If
10184 PROCESSING_TEMPLATE_DECL is not set, the dependency
10185 predicates will short-circuit. */
10186 ++processing_template_decl;
10187 full_args = tsubst_template_args (tmpl_args, args,
10188 complain, in_decl);
10189 --processing_template_decl;
10190 if (full_args == error_mark_node)
10191 RETURN (error_mark_node);
10192
10193 /* If this is a default template template argument,
10194 tsubst might not have changed anything. */
10195 if (full_args == tmpl_args)
10196 RETURN (t);
10197
10198 hash = hash_tmpl_and_args (t, full_args);
10199 spec = retrieve_specialization (t, full_args, hash);
10200 if (spec != NULL_TREE)
10201 {
10202 r = spec;
10203 break;
10204 }
10205
10206 /* Make a new template decl. It will be similar to the
10207 original, but will record the current template arguments.
10208 We also create a new function declaration, which is just
10209 like the old one, but points to this new template, rather
10210 than the old one. */
10211 r = copy_decl (t);
10212 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
10213 DECL_CHAIN (r) = NULL_TREE;
10214
10215 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10216
10217 if (TREE_CODE (decl) == TYPE_DECL
10218 && !TYPE_DECL_ALIAS_P (decl))
10219 {
10220 tree new_type;
10221 ++processing_template_decl;
10222 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10223 --processing_template_decl;
10224 if (new_type == error_mark_node)
10225 RETURN (error_mark_node);
10226
10227 TREE_TYPE (r) = new_type;
10228 /* For a partial specialization, we need to keep pointing to
10229 the primary template. */
10230 if (!DECL_TEMPLATE_SPECIALIZATION (t))
10231 CLASSTYPE_TI_TEMPLATE (new_type) = r;
10232 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
10233 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
10234 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
10235 }
10236 else
10237 {
10238 tree new_decl;
10239 ++processing_template_decl;
10240 new_decl = tsubst (decl, args, complain, in_decl);
10241 --processing_template_decl;
10242 if (new_decl == error_mark_node)
10243 RETURN (error_mark_node);
10244
10245 DECL_TEMPLATE_RESULT (r) = new_decl;
10246 DECL_TI_TEMPLATE (new_decl) = r;
10247 TREE_TYPE (r) = TREE_TYPE (new_decl);
10248 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
10249 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
10250 }
10251
10252 SET_DECL_IMPLICIT_INSTANTIATION (r);
10253 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
10254 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
10255
10256 /* The template parameters for this new template are all the
10257 template parameters for the old template, except the
10258 outermost level of parameters. */
10259 DECL_TEMPLATE_PARMS (r)
10260 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
10261 complain);
10262
10263 if (PRIMARY_TEMPLATE_P (t))
10264 DECL_PRIMARY_TEMPLATE (r) = r;
10265
10266 if (TREE_CODE (decl) != TYPE_DECL)
10267 /* Record this non-type partial instantiation. */
10268 register_specialization (r, t,
10269 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
10270 false, hash);
10271 }
10272 break;
10273
10274 case FUNCTION_DECL:
10275 {
10276 tree ctx;
10277 tree argvec = NULL_TREE;
10278 tree *friends;
10279 tree gen_tmpl;
10280 tree type;
10281 int member;
10282 int args_depth;
10283 int parms_depth;
10284
10285 /* Nobody should be tsubst'ing into non-template functions. */
10286 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
10287
10288 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
10289 {
10290 tree spec;
10291 bool dependent_p;
10292
10293 /* If T is not dependent, just return it. We have to
10294 increment PROCESSING_TEMPLATE_DECL because
10295 value_dependent_expression_p assumes that nothing is
10296 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10297 ++processing_template_decl;
10298 dependent_p = value_dependent_expression_p (t);
10299 --processing_template_decl;
10300 if (!dependent_p)
10301 RETURN (t);
10302
10303 /* Calculate the most general template of which R is a
10304 specialization, and the complete set of arguments used to
10305 specialize R. */
10306 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
10307 argvec = tsubst_template_args (DECL_TI_ARGS
10308 (DECL_TEMPLATE_RESULT
10309 (DECL_TI_TEMPLATE (t))),
10310 args, complain, in_decl);
10311 if (argvec == error_mark_node)
10312 RETURN (error_mark_node);
10313
10314 /* Check to see if we already have this specialization. */
10315 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10316 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10317
10318 if (spec)
10319 {
10320 r = spec;
10321 break;
10322 }
10323
10324 /* We can see more levels of arguments than parameters if
10325 there was a specialization of a member template, like
10326 this:
10327
10328 template <class T> struct S { template <class U> void f(); }
10329 template <> template <class U> void S<int>::f(U);
10330
10331 Here, we'll be substituting into the specialization,
10332 because that's where we can find the code we actually
10333 want to generate, but we'll have enough arguments for
10334 the most general template.
10335
10336 We also deal with the peculiar case:
10337
10338 template <class T> struct S {
10339 template <class U> friend void f();
10340 };
10341 template <class U> void f() {}
10342 template S<int>;
10343 template void f<double>();
10344
10345 Here, the ARGS for the instantiation of will be {int,
10346 double}. But, we only need as many ARGS as there are
10347 levels of template parameters in CODE_PATTERN. We are
10348 careful not to get fooled into reducing the ARGS in
10349 situations like:
10350
10351 template <class T> struct S { template <class U> void f(U); }
10352 template <class T> template <> void S<T>::f(int) {}
10353
10354 which we can spot because the pattern will be a
10355 specialization in this case. */
10356 args_depth = TMPL_ARGS_DEPTH (args);
10357 parms_depth =
10358 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10359 if (args_depth > parms_depth
10360 && !DECL_TEMPLATE_SPECIALIZATION (t))
10361 args = get_innermost_template_args (args, parms_depth);
10362 }
10363 else
10364 {
10365 /* This special case arises when we have something like this:
10366
10367 template <class T> struct S {
10368 friend void f<int>(int, double);
10369 };
10370
10371 Here, the DECL_TI_TEMPLATE for the friend declaration
10372 will be an IDENTIFIER_NODE. We are being called from
10373 tsubst_friend_function, and we want only to create a
10374 new decl (R) with appropriate types so that we can call
10375 determine_specialization. */
10376 gen_tmpl = NULL_TREE;
10377 }
10378
10379 if (DECL_CLASS_SCOPE_P (t))
10380 {
10381 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10382 member = 2;
10383 else
10384 member = 1;
10385 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10386 complain, t, /*entering_scope=*/1);
10387 }
10388 else
10389 {
10390 member = 0;
10391 ctx = DECL_CONTEXT (t);
10392 }
10393 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10394 if (type == error_mark_node)
10395 RETURN (error_mark_node);
10396
10397 /* If we hit excessive deduction depth, the type is bogus even if
10398 it isn't error_mark_node, so don't build a decl. */
10399 if (excessive_deduction_depth)
10400 RETURN (error_mark_node);
10401
10402 /* We do NOT check for matching decls pushed separately at this
10403 point, as they may not represent instantiations of this
10404 template, and in any case are considered separate under the
10405 discrete model. */
10406 r = copy_decl (t);
10407 DECL_USE_TEMPLATE (r) = 0;
10408 TREE_TYPE (r) = type;
10409 /* Clear out the mangled name and RTL for the instantiation. */
10410 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10411 SET_DECL_RTL (r, NULL);
10412 /* Leave DECL_INITIAL set on deleted instantiations. */
10413 if (!DECL_DELETED_FN (r))
10414 DECL_INITIAL (r) = NULL_TREE;
10415 DECL_CONTEXT (r) = ctx;
10416
10417 /* OpenMP UDRs have the only argument a reference to the declared
10418 type. We want to diagnose if the declared type is a reference,
10419 which is invalid, but as references to references are usually
10420 quietly merged, diagnose it here. */
10421 if (DECL_OMP_DECLARE_REDUCTION_P (t))
10422 {
10423 tree argtype
10424 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
10425 argtype = tsubst (argtype, args, complain, in_decl);
10426 if (TREE_CODE (argtype) == REFERENCE_TYPE)
10427 error_at (DECL_SOURCE_LOCATION (t),
10428 "reference type %qT in "
10429 "%<#pragma omp declare reduction%>", argtype);
10430 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
10431 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
10432 argtype);
10433 }
10434
10435 if (member && DECL_CONV_FN_P (r))
10436 /* Type-conversion operator. Reconstruct the name, in
10437 case it's the name of one of the template's parameters. */
10438 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10439
10440 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10441 complain, t);
10442 DECL_RESULT (r) = NULL_TREE;
10443
10444 TREE_STATIC (r) = 0;
10445 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10446 DECL_EXTERNAL (r) = 1;
10447 /* If this is an instantiation of a function with internal
10448 linkage, we already know what object file linkage will be
10449 assigned to the instantiation. */
10450 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10451 DECL_DEFER_OUTPUT (r) = 0;
10452 DECL_CHAIN (r) = NULL_TREE;
10453 DECL_PENDING_INLINE_INFO (r) = 0;
10454 DECL_PENDING_INLINE_P (r) = 0;
10455 DECL_SAVED_TREE (r) = NULL_TREE;
10456 DECL_STRUCT_FUNCTION (r) = NULL;
10457 TREE_USED (r) = 0;
10458 /* We'll re-clone as appropriate in instantiate_template. */
10459 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10460
10461 /* If we aren't complaining now, return on error before we register
10462 the specialization so that we'll complain eventually. */
10463 if ((complain & tf_error) == 0
10464 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10465 && !grok_op_properties (r, /*complain=*/false))
10466 RETURN (error_mark_node);
10467
10468 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10469 this in the special friend case mentioned above where
10470 GEN_TMPL is NULL. */
10471 if (gen_tmpl)
10472 {
10473 DECL_TEMPLATE_INFO (r)
10474 = build_template_info (gen_tmpl, argvec);
10475 SET_DECL_IMPLICIT_INSTANTIATION (r);
10476
10477 tree new_r
10478 = register_specialization (r, gen_tmpl, argvec, false, hash);
10479 if (new_r != r)
10480 /* We instantiated this while substituting into
10481 the type earlier (template/friend54.C). */
10482 RETURN (new_r);
10483
10484 /* We're not supposed to instantiate default arguments
10485 until they are called, for a template. But, for a
10486 declaration like:
10487
10488 template <class T> void f ()
10489 { extern void g(int i = T()); }
10490
10491 we should do the substitution when the template is
10492 instantiated. We handle the member function case in
10493 instantiate_class_template since the default arguments
10494 might refer to other members of the class. */
10495 if (!member
10496 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10497 && !uses_template_parms (argvec))
10498 tsubst_default_arguments (r, complain);
10499 }
10500 else
10501 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10502
10503 /* Copy the list of befriending classes. */
10504 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10505 *friends;
10506 friends = &TREE_CHAIN (*friends))
10507 {
10508 *friends = copy_node (*friends);
10509 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10510 args, complain,
10511 in_decl);
10512 }
10513
10514 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10515 {
10516 maybe_retrofit_in_chrg (r);
10517 if (DECL_CONSTRUCTOR_P (r))
10518 grok_ctor_properties (ctx, r);
10519 if (DECL_INHERITED_CTOR_BASE (r))
10520 deduce_inheriting_ctor (r);
10521 /* If this is an instantiation of a member template, clone it.
10522 If it isn't, that'll be handled by
10523 clone_constructors_and_destructors. */
10524 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10525 clone_function_decl (r, /*update_method_vec_p=*/0);
10526 }
10527 else if ((complain & tf_error) != 0
10528 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10529 && !grok_op_properties (r, /*complain=*/true))
10530 RETURN (error_mark_node);
10531
10532 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10533 SET_DECL_FRIEND_CONTEXT (r,
10534 tsubst (DECL_FRIEND_CONTEXT (t),
10535 args, complain, in_decl));
10536
10537 /* Possibly limit visibility based on template args. */
10538 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10539 if (DECL_VISIBILITY_SPECIFIED (t))
10540 {
10541 DECL_VISIBILITY_SPECIFIED (r) = 0;
10542 DECL_ATTRIBUTES (r)
10543 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10544 }
10545 determine_visibility (r);
10546 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10547 && !processing_template_decl)
10548 defaulted_late_check (r);
10549
10550 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10551 args, complain, in_decl);
10552 }
10553 break;
10554
10555 case PARM_DECL:
10556 {
10557 tree type = NULL_TREE;
10558 int i, len = 1;
10559 tree expanded_types = NULL_TREE;
10560 tree prev_r = NULL_TREE;
10561 tree first_r = NULL_TREE;
10562
10563 if (DECL_PACK_P (t))
10564 {
10565 /* If there is a local specialization that isn't a
10566 parameter pack, it means that we're doing a "simple"
10567 substitution from inside tsubst_pack_expansion. Just
10568 return the local specialization (which will be a single
10569 parm). */
10570 tree spec = retrieve_local_specialization (t);
10571 if (spec
10572 && TREE_CODE (spec) == PARM_DECL
10573 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10574 RETURN (spec);
10575
10576 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10577 the parameters in this function parameter pack. */
10578 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10579 complain, in_decl);
10580 if (TREE_CODE (expanded_types) == TREE_VEC)
10581 {
10582 len = TREE_VEC_LENGTH (expanded_types);
10583
10584 /* Zero-length parameter packs are boring. Just substitute
10585 into the chain. */
10586 if (len == 0)
10587 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10588 TREE_CHAIN (t)));
10589 }
10590 else
10591 {
10592 /* All we did was update the type. Make a note of that. */
10593 type = expanded_types;
10594 expanded_types = NULL_TREE;
10595 }
10596 }
10597
10598 /* Loop through all of the parameters we'll build. When T is
10599 a function parameter pack, LEN is the number of expanded
10600 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10601 r = NULL_TREE;
10602 for (i = 0; i < len; ++i)
10603 {
10604 prev_r = r;
10605 r = copy_node (t);
10606 if (DECL_TEMPLATE_PARM_P (t))
10607 SET_DECL_TEMPLATE_PARM_P (r);
10608
10609 if (expanded_types)
10610 /* We're on the Ith parameter of the function parameter
10611 pack. */
10612 {
10613 /* Get the Ith type. */
10614 type = TREE_VEC_ELT (expanded_types, i);
10615
10616 /* Rename the parameter to include the index. */
10617 DECL_NAME (r)
10618 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10619 }
10620 else if (!type)
10621 /* We're dealing with a normal parameter. */
10622 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10623
10624 type = type_decays_to (type);
10625 TREE_TYPE (r) = type;
10626 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10627
10628 if (DECL_INITIAL (r))
10629 {
10630 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10631 DECL_INITIAL (r) = TREE_TYPE (r);
10632 else
10633 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10634 complain, in_decl);
10635 }
10636
10637 DECL_CONTEXT (r) = NULL_TREE;
10638
10639 if (!DECL_TEMPLATE_PARM_P (r))
10640 DECL_ARG_TYPE (r) = type_passed_as (type);
10641
10642 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10643 args, complain, in_decl);
10644
10645 /* Keep track of the first new parameter we
10646 generate. That's what will be returned to the
10647 caller. */
10648 if (!first_r)
10649 first_r = r;
10650
10651 /* Build a proper chain of parameters when substituting
10652 into a function parameter pack. */
10653 if (prev_r)
10654 DECL_CHAIN (prev_r) = r;
10655 }
10656
10657 /* If cp_unevaluated_operand is set, we're just looking for a
10658 single dummy parameter, so don't keep going. */
10659 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10660 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10661 complain, DECL_CHAIN (t));
10662
10663 /* FIRST_R contains the start of the chain we've built. */
10664 r = first_r;
10665 }
10666 break;
10667
10668 case FIELD_DECL:
10669 {
10670 tree type = NULL_TREE;
10671 tree vec = NULL_TREE;
10672 tree expanded_types = NULL_TREE;
10673 int len = 1;
10674
10675 if (PACK_EXPANSION_P (TREE_TYPE (t)))
10676 {
10677 /* This field is a lambda capture pack. Return a TREE_VEC of
10678 the expanded fields to instantiate_class_template_1 and
10679 store them in the specializations hash table as a
10680 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
10681 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10682 complain, in_decl);
10683 if (TREE_CODE (expanded_types) == TREE_VEC)
10684 {
10685 len = TREE_VEC_LENGTH (expanded_types);
10686 vec = make_tree_vec (len);
10687 }
10688 else
10689 {
10690 /* All we did was update the type. Make a note of that. */
10691 type = expanded_types;
10692 expanded_types = NULL_TREE;
10693 }
10694 }
10695
10696 for (int i = 0; i < len; ++i)
10697 {
10698 r = copy_decl (t);
10699 if (expanded_types)
10700 {
10701 type = TREE_VEC_ELT (expanded_types, i);
10702 DECL_NAME (r)
10703 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10704 }
10705 else if (!type)
10706 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10707
10708 if (type == error_mark_node)
10709 RETURN (error_mark_node);
10710 TREE_TYPE (r) = type;
10711 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10712
10713 if (DECL_C_BIT_FIELD (r))
10714 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10715 non-bit-fields DECL_INITIAL is a non-static data member
10716 initializer, which gets deferred instantiation. */
10717 DECL_INITIAL (r)
10718 = tsubst_expr (DECL_INITIAL (t), args,
10719 complain, in_decl,
10720 /*integral_constant_expression_p=*/true);
10721 else if (DECL_INITIAL (t))
10722 {
10723 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10724 NSDMI in perform_member_init. Still set DECL_INITIAL
10725 so that we know there is one. */
10726 DECL_INITIAL (r) = void_zero_node;
10727 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10728 retrofit_lang_decl (r);
10729 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10730 }
10731 /* We don't have to set DECL_CONTEXT here; it is set by
10732 finish_member_declaration. */
10733 DECL_CHAIN (r) = NULL_TREE;
10734
10735 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10736 args, complain, in_decl);
10737
10738 if (vec)
10739 TREE_VEC_ELT (vec, i) = r;
10740 }
10741
10742 if (vec)
10743 {
10744 r = vec;
10745 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
10746 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
10747 SET_ARGUMENT_PACK_ARGS (pack, vec);
10748 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
10749 TREE_TYPE (pack) = tpack;
10750 register_specialization (pack, t, args, false, 0);
10751 }
10752 }
10753 break;
10754
10755 case USING_DECL:
10756 /* We reach here only for member using decls. We also need to check
10757 uses_template_parms because DECL_DEPENDENT_P is not set for a
10758 using-declaration that designates a member of the current
10759 instantiation (c++/53549). */
10760 if (DECL_DEPENDENT_P (t)
10761 || uses_template_parms (USING_DECL_SCOPE (t)))
10762 {
10763 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10764 complain, in_decl);
10765 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10766 r = do_class_using_decl (inst_scope, name);
10767 if (!r)
10768 r = error_mark_node;
10769 else
10770 {
10771 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10772 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10773 }
10774 }
10775 else
10776 {
10777 r = copy_node (t);
10778 DECL_CHAIN (r) = NULL_TREE;
10779 }
10780 break;
10781
10782 case TYPE_DECL:
10783 case VAR_DECL:
10784 {
10785 tree argvec = NULL_TREE;
10786 tree gen_tmpl = NULL_TREE;
10787 tree spec;
10788 tree tmpl = NULL_TREE;
10789 tree ctx;
10790 tree type = NULL_TREE;
10791 bool local_p;
10792
10793 if (TREE_CODE (t) == TYPE_DECL
10794 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10795 {
10796 /* If this is the canonical decl, we don't have to
10797 mess with instantiations, and often we can't (for
10798 typename, template type parms and such). Note that
10799 TYPE_NAME is not correct for the above test if
10800 we've copied the type for a typedef. */
10801 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10802 if (type == error_mark_node)
10803 RETURN (error_mark_node);
10804 r = TYPE_NAME (type);
10805 break;
10806 }
10807
10808 /* Check to see if we already have the specialization we
10809 need. */
10810 spec = NULL_TREE;
10811 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10812 {
10813 /* T is a static data member or namespace-scope entity.
10814 We have to substitute into namespace-scope variables
10815 (even though such entities are never templates) because
10816 of cases like:
10817
10818 template <class T> void f() { extern T t; }
10819
10820 where the entity referenced is not known until
10821 instantiation time. */
10822 local_p = false;
10823 ctx = DECL_CONTEXT (t);
10824 if (DECL_CLASS_SCOPE_P (t))
10825 {
10826 ctx = tsubst_aggr_type (ctx, args,
10827 complain,
10828 in_decl, /*entering_scope=*/1);
10829 /* If CTX is unchanged, then T is in fact the
10830 specialization we want. That situation occurs when
10831 referencing a static data member within in its own
10832 class. We can use pointer equality, rather than
10833 same_type_p, because DECL_CONTEXT is always
10834 canonical... */
10835 if (ctx == DECL_CONTEXT (t)
10836 && (TREE_CODE (t) != TYPE_DECL
10837 /* ... unless T is a member template; in which
10838 case our caller can be willing to create a
10839 specialization of that template represented
10840 by T. */
10841 || !(DECL_TI_TEMPLATE (t)
10842 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10843 spec = t;
10844 }
10845
10846 if (!spec)
10847 {
10848 tmpl = DECL_TI_TEMPLATE (t);
10849 gen_tmpl = most_general_template (tmpl);
10850 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10851 if (argvec == error_mark_node)
10852 RETURN (error_mark_node);
10853 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10854 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10855 }
10856 }
10857 else
10858 {
10859 /* A local variable. */
10860 local_p = true;
10861 /* Subsequent calls to pushdecl will fill this in. */
10862 ctx = NULL_TREE;
10863 spec = retrieve_local_specialization (t);
10864 }
10865 /* If we already have the specialization we need, there is
10866 nothing more to do. */
10867 if (spec)
10868 {
10869 r = spec;
10870 break;
10871 }
10872
10873 /* Create a new node for the specialization we need. */
10874 r = copy_decl (t);
10875 if (type == NULL_TREE)
10876 {
10877 if (is_typedef_decl (t))
10878 type = DECL_ORIGINAL_TYPE (t);
10879 else
10880 type = TREE_TYPE (t);
10881 if (VAR_P (t)
10882 && VAR_HAD_UNKNOWN_BOUND (t)
10883 && type != error_mark_node)
10884 type = strip_array_domain (type);
10885 type = tsubst (type, args, complain, in_decl);
10886 }
10887 if (VAR_P (r))
10888 {
10889 /* Even if the original location is out of scope, the
10890 newly substituted one is not. */
10891 DECL_DEAD_FOR_LOCAL (r) = 0;
10892 DECL_INITIALIZED_P (r) = 0;
10893 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10894 if (type == error_mark_node)
10895 RETURN (error_mark_node);
10896 if (TREE_CODE (type) == FUNCTION_TYPE)
10897 {
10898 /* It may seem that this case cannot occur, since:
10899
10900 typedef void f();
10901 void g() { f x; }
10902
10903 declares a function, not a variable. However:
10904
10905 typedef void f();
10906 template <typename T> void g() { T t; }
10907 template void g<f>();
10908
10909 is an attempt to declare a variable with function
10910 type. */
10911 error ("variable %qD has function type",
10912 /* R is not yet sufficiently initialized, so we
10913 just use its name. */
10914 DECL_NAME (r));
10915 RETURN (error_mark_node);
10916 }
10917 type = complete_type (type);
10918 /* Wait until cp_finish_decl to set this again, to handle
10919 circular dependency (template/instantiate6.C). */
10920 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10921 type = check_var_type (DECL_NAME (r), type);
10922
10923 if (DECL_HAS_VALUE_EXPR_P (t))
10924 {
10925 tree ve = DECL_VALUE_EXPR (t);
10926 ve = tsubst_expr (ve, args, complain, in_decl,
10927 /*constant_expression_p=*/false);
10928 if (REFERENCE_REF_P (ve))
10929 {
10930 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10931 ve = TREE_OPERAND (ve, 0);
10932 }
10933 SET_DECL_VALUE_EXPR (r, ve);
10934 }
10935 }
10936 else if (DECL_SELF_REFERENCE_P (t))
10937 SET_DECL_SELF_REFERENCE_P (r);
10938 TREE_TYPE (r) = type;
10939 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10940 DECL_CONTEXT (r) = ctx;
10941 /* Clear out the mangled name and RTL for the instantiation. */
10942 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10943 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10944 SET_DECL_RTL (r, NULL);
10945 /* The initializer must not be expanded until it is required;
10946 see [temp.inst]. */
10947 DECL_INITIAL (r) = NULL_TREE;
10948 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10949 SET_DECL_RTL (r, NULL);
10950 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10951 if (VAR_P (r))
10952 {
10953 /* Possibly limit visibility based on template args. */
10954 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10955 if (DECL_VISIBILITY_SPECIFIED (t))
10956 {
10957 DECL_VISIBILITY_SPECIFIED (r) = 0;
10958 DECL_ATTRIBUTES (r)
10959 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10960 }
10961 determine_visibility (r);
10962 }
10963
10964 if (!local_p)
10965 {
10966 /* A static data member declaration is always marked
10967 external when it is declared in-class, even if an
10968 initializer is present. We mimic the non-template
10969 processing here. */
10970 DECL_EXTERNAL (r) = 1;
10971
10972 register_specialization (r, gen_tmpl, argvec, false, hash);
10973 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10974 SET_DECL_IMPLICIT_INSTANTIATION (r);
10975 }
10976 else if (cp_unevaluated_operand)
10977 gcc_unreachable ();
10978 else
10979 register_local_specialization (r, t);
10980
10981 DECL_CHAIN (r) = NULL_TREE;
10982
10983 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10984 /*flags=*/0,
10985 args, complain, in_decl);
10986
10987 /* Preserve a typedef that names a type. */
10988 if (is_typedef_decl (r))
10989 {
10990 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10991 set_underlying_type (r);
10992 }
10993
10994 layout_decl (r, 0);
10995 }
10996 break;
10997
10998 default:
10999 gcc_unreachable ();
11000 }
11001 #undef RETURN
11002
11003 out:
11004 /* Restore the file and line information. */
11005 input_location = saved_loc;
11006
11007 return r;
11008 }
11009
11010 /* Substitute into the ARG_TYPES of a function type.
11011 If END is a TREE_CHAIN, leave it and any following types
11012 un-substituted. */
11013
11014 static tree
11015 tsubst_arg_types (tree arg_types,
11016 tree args,
11017 tree end,
11018 tsubst_flags_t complain,
11019 tree in_decl)
11020 {
11021 tree remaining_arg_types;
11022 tree type = NULL_TREE;
11023 int i = 1;
11024 tree expanded_args = NULL_TREE;
11025 tree default_arg;
11026
11027 if (!arg_types || arg_types == void_list_node || arg_types == end)
11028 return arg_types;
11029
11030 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
11031 args, end, complain, in_decl);
11032 if (remaining_arg_types == error_mark_node)
11033 return error_mark_node;
11034
11035 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
11036 {
11037 /* For a pack expansion, perform substitution on the
11038 entire expression. Later on, we'll handle the arguments
11039 one-by-one. */
11040 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
11041 args, complain, in_decl);
11042
11043 if (TREE_CODE (expanded_args) == TREE_VEC)
11044 /* So that we'll spin through the parameters, one by one. */
11045 i = TREE_VEC_LENGTH (expanded_args);
11046 else
11047 {
11048 /* We only partially substituted into the parameter
11049 pack. Our type is TYPE_PACK_EXPANSION. */
11050 type = expanded_args;
11051 expanded_args = NULL_TREE;
11052 }
11053 }
11054
11055 while (i > 0) {
11056 --i;
11057
11058 if (expanded_args)
11059 type = TREE_VEC_ELT (expanded_args, i);
11060 else if (!type)
11061 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
11062
11063 if (type == error_mark_node)
11064 return error_mark_node;
11065 if (VOID_TYPE_P (type))
11066 {
11067 if (complain & tf_error)
11068 {
11069 error ("invalid parameter type %qT", type);
11070 if (in_decl)
11071 error ("in declaration %q+D", in_decl);
11072 }
11073 return error_mark_node;
11074 }
11075 /* DR 657. */
11076 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
11077 return error_mark_node;
11078
11079 /* Do array-to-pointer, function-to-pointer conversion, and ignore
11080 top-level qualifiers as required. */
11081 type = cv_unqualified (type_decays_to (type));
11082
11083 /* We do not substitute into default arguments here. The standard
11084 mandates that they be instantiated only when needed, which is
11085 done in build_over_call. */
11086 default_arg = TREE_PURPOSE (arg_types);
11087
11088 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
11089 {
11090 /* We've instantiated a template before its default arguments
11091 have been parsed. This can happen for a nested template
11092 class, and is not an error unless we require the default
11093 argument in a call of this function. */
11094 remaining_arg_types =
11095 tree_cons (default_arg, type, remaining_arg_types);
11096 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
11097 }
11098 else
11099 remaining_arg_types =
11100 hash_tree_cons (default_arg, type, remaining_arg_types);
11101 }
11102
11103 return remaining_arg_types;
11104 }
11105
11106 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
11107 *not* handle the exception-specification for FNTYPE, because the
11108 initial substitution of explicitly provided template parameters
11109 during argument deduction forbids substitution into the
11110 exception-specification:
11111
11112 [temp.deduct]
11113
11114 All references in the function type of the function template to the
11115 corresponding template parameters are replaced by the specified tem-
11116 plate argument values. If a substitution in a template parameter or
11117 in the function type of the function template results in an invalid
11118 type, type deduction fails. [Note: The equivalent substitution in
11119 exception specifications is done only when the function is instanti-
11120 ated, at which point a program is ill-formed if the substitution
11121 results in an invalid type.] */
11122
11123 static tree
11124 tsubst_function_type (tree t,
11125 tree args,
11126 tsubst_flags_t complain,
11127 tree in_decl)
11128 {
11129 tree return_type;
11130 tree arg_types;
11131 tree fntype;
11132
11133 /* The TYPE_CONTEXT is not used for function/method types. */
11134 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
11135
11136 /* Substitute the return type. */
11137 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11138 if (return_type == error_mark_node)
11139 return error_mark_node;
11140 /* DR 486 clarifies that creation of a function type with an
11141 invalid return type is a deduction failure. */
11142 if (TREE_CODE (return_type) == ARRAY_TYPE
11143 || TREE_CODE (return_type) == FUNCTION_TYPE)
11144 {
11145 if (complain & tf_error)
11146 {
11147 if (TREE_CODE (return_type) == ARRAY_TYPE)
11148 error ("function returning an array");
11149 else
11150 error ("function returning a function");
11151 }
11152 return error_mark_node;
11153 }
11154 /* And DR 657. */
11155 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
11156 return error_mark_node;
11157
11158 /* Substitute the argument types. */
11159 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
11160 complain, in_decl);
11161 if (arg_types == error_mark_node)
11162 return error_mark_node;
11163
11164 /* Construct a new type node and return it. */
11165 if (TREE_CODE (t) == FUNCTION_TYPE)
11166 {
11167 fntype = build_function_type (return_type, arg_types);
11168 fntype = apply_memfn_quals (fntype,
11169 type_memfn_quals (t),
11170 type_memfn_rqual (t));
11171 }
11172 else
11173 {
11174 tree r = TREE_TYPE (TREE_VALUE (arg_types));
11175 if (! MAYBE_CLASS_TYPE_P (r))
11176 {
11177 /* [temp.deduct]
11178
11179 Type deduction may fail for any of the following
11180 reasons:
11181
11182 -- Attempting to create "pointer to member of T" when T
11183 is not a class type. */
11184 if (complain & tf_error)
11185 error ("creating pointer to member function of non-class type %qT",
11186 r);
11187 return error_mark_node;
11188 }
11189
11190 fntype = build_method_type_directly (r, return_type,
11191 TREE_CHAIN (arg_types));
11192 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
11193 }
11194 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
11195
11196 return fntype;
11197 }
11198
11199 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
11200 ARGS into that specification, and return the substituted
11201 specification. If there is no specification, return NULL_TREE. */
11202
11203 static tree
11204 tsubst_exception_specification (tree fntype,
11205 tree args,
11206 tsubst_flags_t complain,
11207 tree in_decl,
11208 bool defer_ok)
11209 {
11210 tree specs;
11211 tree new_specs;
11212
11213 specs = TYPE_RAISES_EXCEPTIONS (fntype);
11214 new_specs = NULL_TREE;
11215 if (specs && TREE_PURPOSE (specs))
11216 {
11217 /* A noexcept-specifier. */
11218 tree expr = TREE_PURPOSE (specs);
11219 if (TREE_CODE (expr) == INTEGER_CST)
11220 new_specs = expr;
11221 else if (defer_ok)
11222 {
11223 /* Defer instantiation of noexcept-specifiers to avoid
11224 excessive instantiations (c++/49107). */
11225 new_specs = make_node (DEFERRED_NOEXCEPT);
11226 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
11227 {
11228 /* We already partially instantiated this member template,
11229 so combine the new args with the old. */
11230 DEFERRED_NOEXCEPT_PATTERN (new_specs)
11231 = DEFERRED_NOEXCEPT_PATTERN (expr);
11232 DEFERRED_NOEXCEPT_ARGS (new_specs)
11233 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
11234 }
11235 else
11236 {
11237 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
11238 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
11239 }
11240 }
11241 else
11242 new_specs = tsubst_copy_and_build
11243 (expr, args, complain, in_decl, /*function_p=*/false,
11244 /*integral_constant_expression_p=*/true);
11245 new_specs = build_noexcept_spec (new_specs, complain);
11246 }
11247 else if (specs)
11248 {
11249 if (! TREE_VALUE (specs))
11250 new_specs = specs;
11251 else
11252 while (specs)
11253 {
11254 tree spec;
11255 int i, len = 1;
11256 tree expanded_specs = NULL_TREE;
11257
11258 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
11259 {
11260 /* Expand the pack expansion type. */
11261 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
11262 args, complain,
11263 in_decl);
11264
11265 if (expanded_specs == error_mark_node)
11266 return error_mark_node;
11267 else if (TREE_CODE (expanded_specs) == TREE_VEC)
11268 len = TREE_VEC_LENGTH (expanded_specs);
11269 else
11270 {
11271 /* We're substituting into a member template, so
11272 we got a TYPE_PACK_EXPANSION back. Add that
11273 expansion and move on. */
11274 gcc_assert (TREE_CODE (expanded_specs)
11275 == TYPE_PACK_EXPANSION);
11276 new_specs = add_exception_specifier (new_specs,
11277 expanded_specs,
11278 complain);
11279 specs = TREE_CHAIN (specs);
11280 continue;
11281 }
11282 }
11283
11284 for (i = 0; i < len; ++i)
11285 {
11286 if (expanded_specs)
11287 spec = TREE_VEC_ELT (expanded_specs, i);
11288 else
11289 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
11290 if (spec == error_mark_node)
11291 return spec;
11292 new_specs = add_exception_specifier (new_specs, spec,
11293 complain);
11294 }
11295
11296 specs = TREE_CHAIN (specs);
11297 }
11298 }
11299 return new_specs;
11300 }
11301
11302 /* Take the tree structure T and replace template parameters used
11303 therein with the argument vector ARGS. IN_DECL is an associated
11304 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11305 Issue error and warning messages under control of COMPLAIN. Note
11306 that we must be relatively non-tolerant of extensions here, in
11307 order to preserve conformance; if we allow substitutions that
11308 should not be allowed, we may allow argument deductions that should
11309 not succeed, and therefore report ambiguous overload situations
11310 where there are none. In theory, we could allow the substitution,
11311 but indicate that it should have failed, and allow our caller to
11312 make sure that the right thing happens, but we don't try to do this
11313 yet.
11314
11315 This function is used for dealing with types, decls and the like;
11316 for expressions, use tsubst_expr or tsubst_copy. */
11317
11318 tree
11319 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11320 {
11321 enum tree_code code;
11322 tree type, r = NULL_TREE;
11323
11324 if (t == NULL_TREE || t == error_mark_node
11325 || t == integer_type_node
11326 || t == void_type_node
11327 || t == char_type_node
11328 || t == unknown_type_node
11329 || TREE_CODE (t) == NAMESPACE_DECL
11330 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
11331 return t;
11332
11333 if (DECL_P (t))
11334 return tsubst_decl (t, args, complain);
11335
11336 if (args == NULL_TREE)
11337 return t;
11338
11339 code = TREE_CODE (t);
11340
11341 if (code == IDENTIFIER_NODE)
11342 type = IDENTIFIER_TYPE_VALUE (t);
11343 else
11344 type = TREE_TYPE (t);
11345
11346 gcc_assert (type != unknown_type_node);
11347
11348 /* Reuse typedefs. We need to do this to handle dependent attributes,
11349 such as attribute aligned. */
11350 if (TYPE_P (t)
11351 && typedef_variant_p (t))
11352 {
11353 tree decl = TYPE_NAME (t);
11354
11355 if (alias_template_specialization_p (t))
11356 {
11357 /* DECL represents an alias template and we want to
11358 instantiate it. */
11359 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11360 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11361 r = instantiate_alias_template (tmpl, gen_args, complain);
11362 }
11363 else if (DECL_CLASS_SCOPE_P (decl)
11364 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
11365 && uses_template_parms (DECL_CONTEXT (decl)))
11366 {
11367 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11368 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
11369 r = retrieve_specialization (tmpl, gen_args, 0);
11370 }
11371 else if (DECL_FUNCTION_SCOPE_P (decl)
11372 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
11373 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
11374 r = retrieve_local_specialization (decl);
11375 else
11376 /* The typedef is from a non-template context. */
11377 return t;
11378
11379 if (r)
11380 {
11381 r = TREE_TYPE (r);
11382 r = cp_build_qualified_type_real
11383 (r, cp_type_quals (t) | cp_type_quals (r),
11384 complain | tf_ignore_bad_quals);
11385 return r;
11386 }
11387 else
11388 {
11389 /* We don't have an instantiation yet, so drop the typedef. */
11390 int quals = cp_type_quals (t);
11391 t = DECL_ORIGINAL_TYPE (decl);
11392 t = cp_build_qualified_type_real (t, quals,
11393 complain | tf_ignore_bad_quals);
11394 }
11395 }
11396
11397 if (type
11398 && code != TYPENAME_TYPE
11399 && code != TEMPLATE_TYPE_PARM
11400 && code != IDENTIFIER_NODE
11401 && code != FUNCTION_TYPE
11402 && code != METHOD_TYPE)
11403 type = tsubst (type, args, complain, in_decl);
11404 if (type == error_mark_node)
11405 return error_mark_node;
11406
11407 switch (code)
11408 {
11409 case RECORD_TYPE:
11410 case UNION_TYPE:
11411 case ENUMERAL_TYPE:
11412 return tsubst_aggr_type (t, args, complain, in_decl,
11413 /*entering_scope=*/0);
11414
11415 case ERROR_MARK:
11416 case IDENTIFIER_NODE:
11417 case VOID_TYPE:
11418 case REAL_TYPE:
11419 case COMPLEX_TYPE:
11420 case VECTOR_TYPE:
11421 case BOOLEAN_TYPE:
11422 case NULLPTR_TYPE:
11423 case LANG_TYPE:
11424 return t;
11425
11426 case INTEGER_TYPE:
11427 if (t == integer_type_node)
11428 return t;
11429
11430 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11431 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11432 return t;
11433
11434 {
11435 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11436
11437 max = tsubst_expr (omax, args, complain, in_decl,
11438 /*integral_constant_expression_p=*/false);
11439
11440 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11441 needed. */
11442 if (TREE_CODE (max) == NOP_EXPR
11443 && TREE_SIDE_EFFECTS (omax)
11444 && !TREE_TYPE (max))
11445 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11446
11447 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11448 with TREE_SIDE_EFFECTS that indicates this is not an integral
11449 constant expression. */
11450 if (processing_template_decl
11451 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11452 {
11453 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11454 TREE_SIDE_EFFECTS (max) = 1;
11455 }
11456
11457 return compute_array_index_type (NULL_TREE, max, complain);
11458 }
11459
11460 case TEMPLATE_TYPE_PARM:
11461 case TEMPLATE_TEMPLATE_PARM:
11462 case BOUND_TEMPLATE_TEMPLATE_PARM:
11463 case TEMPLATE_PARM_INDEX:
11464 {
11465 int idx;
11466 int level;
11467 int levels;
11468 tree arg = NULL_TREE;
11469
11470 r = NULL_TREE;
11471
11472 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11473 template_parm_level_and_index (t, &level, &idx);
11474
11475 levels = TMPL_ARGS_DEPTH (args);
11476 if (level <= levels)
11477 {
11478 arg = TMPL_ARG (args, level, idx);
11479
11480 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11481 {
11482 /* See through ARGUMENT_PACK_SELECT arguments. */
11483 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11484 /* If the selected argument is an expansion E, that most
11485 likely means we were called from
11486 gen_elem_of_pack_expansion_instantiation during the
11487 substituting of pack an argument pack (which Ith
11488 element is a pack expansion, where I is
11489 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
11490 In this case, the Ith element resulting from this
11491 substituting is going to be a pack expansion, which
11492 pattern is the pattern of E. Let's return the
11493 pattern of E, and
11494 gen_elem_of_pack_expansion_instantiation will
11495 build the resulting pack expansion from it. */
11496 if (PACK_EXPANSION_P (arg))
11497 arg = PACK_EXPANSION_PATTERN (arg);
11498 }
11499 }
11500
11501 if (arg == error_mark_node)
11502 return error_mark_node;
11503 else if (arg != NULL_TREE)
11504 {
11505 if (ARGUMENT_PACK_P (arg))
11506 /* If ARG is an argument pack, we don't actually want to
11507 perform a substitution here, because substitutions
11508 for argument packs are only done
11509 element-by-element. We can get to this point when
11510 substituting the type of a non-type template
11511 parameter pack, when that type actually contains
11512 template parameter packs from an outer template, e.g.,
11513
11514 template<typename... Types> struct A {
11515 template<Types... Values> struct B { };
11516 }; */
11517 return t;
11518
11519 if (code == TEMPLATE_TYPE_PARM)
11520 {
11521 int quals;
11522 gcc_assert (TYPE_P (arg));
11523
11524 quals = cp_type_quals (arg) | cp_type_quals (t);
11525
11526 return cp_build_qualified_type_real
11527 (arg, quals, complain | tf_ignore_bad_quals);
11528 }
11529 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11530 {
11531 /* We are processing a type constructed from a
11532 template template parameter. */
11533 tree argvec = tsubst (TYPE_TI_ARGS (t),
11534 args, complain, in_decl);
11535 if (argvec == error_mark_node)
11536 return error_mark_node;
11537
11538 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11539 || TREE_CODE (arg) == TEMPLATE_DECL
11540 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11541
11542 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11543 /* Consider this code:
11544
11545 template <template <class> class Template>
11546 struct Internal {
11547 template <class Arg> using Bind = Template<Arg>;
11548 };
11549
11550 template <template <class> class Template, class Arg>
11551 using Instantiate = Template<Arg>; //#0
11552
11553 template <template <class> class Template,
11554 class Argument>
11555 using Bind =
11556 Instantiate<Internal<Template>::template Bind,
11557 Argument>; //#1
11558
11559 When #1 is parsed, the
11560 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11561 parameter `Template' in #0 matches the
11562 UNBOUND_CLASS_TEMPLATE representing the argument
11563 `Internal<Template>::template Bind'; We then want
11564 to assemble the type `Bind<Argument>' that can't
11565 be fully created right now, because
11566 `Internal<Template>' not being complete, the Bind
11567 template cannot be looked up in that context. So
11568 we need to "store" `Bind<Argument>' for later
11569 when the context of Bind becomes complete. Let's
11570 store that in a TYPENAME_TYPE. */
11571 return make_typename_type (TYPE_CONTEXT (arg),
11572 build_nt (TEMPLATE_ID_EXPR,
11573 TYPE_IDENTIFIER (arg),
11574 argvec),
11575 typename_type,
11576 complain);
11577
11578 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11579 are resolving nested-types in the signature of a
11580 member function templates. Otherwise ARG is a
11581 TEMPLATE_DECL and is the real template to be
11582 instantiated. */
11583 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11584 arg = TYPE_NAME (arg);
11585
11586 r = lookup_template_class (arg,
11587 argvec, in_decl,
11588 DECL_CONTEXT (arg),
11589 /*entering_scope=*/0,
11590 complain);
11591 return cp_build_qualified_type_real
11592 (r, cp_type_quals (t) | cp_type_quals (r), complain);
11593 }
11594 else
11595 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11596 return convert_from_reference (unshare_expr (arg));
11597 }
11598
11599 if (level == 1)
11600 /* This can happen during the attempted tsubst'ing in
11601 unify. This means that we don't yet have any information
11602 about the template parameter in question. */
11603 return t;
11604
11605 /* Early in template argument deduction substitution, we don't
11606 want to reduce the level of 'auto', or it will be confused
11607 with a normal template parm in subsequent deduction. */
11608 if (is_auto (t) && (complain & tf_partial))
11609 return t;
11610
11611 /* If we get here, we must have been looking at a parm for a
11612 more deeply nested template. Make a new version of this
11613 template parameter, but with a lower level. */
11614 switch (code)
11615 {
11616 case TEMPLATE_TYPE_PARM:
11617 case TEMPLATE_TEMPLATE_PARM:
11618 case BOUND_TEMPLATE_TEMPLATE_PARM:
11619 if (cp_type_quals (t))
11620 {
11621 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11622 r = cp_build_qualified_type_real
11623 (r, cp_type_quals (t),
11624 complain | (code == TEMPLATE_TYPE_PARM
11625 ? tf_ignore_bad_quals : 0));
11626 }
11627 else
11628 {
11629 r = copy_type (t);
11630 TEMPLATE_TYPE_PARM_INDEX (r)
11631 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11632 r, levels, args, complain);
11633 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11634 TYPE_MAIN_VARIANT (r) = r;
11635 TYPE_POINTER_TO (r) = NULL_TREE;
11636 TYPE_REFERENCE_TO (r) = NULL_TREE;
11637
11638 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11639 /* We have reduced the level of the template
11640 template parameter, but not the levels of its
11641 template parameters, so canonical_type_parameter
11642 will not be able to find the canonical template
11643 template parameter for this level. Thus, we
11644 require structural equality checking to compare
11645 TEMPLATE_TEMPLATE_PARMs. */
11646 SET_TYPE_STRUCTURAL_EQUALITY (r);
11647 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11648 SET_TYPE_STRUCTURAL_EQUALITY (r);
11649 else
11650 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11651
11652 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11653 {
11654 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11655 complain, in_decl);
11656 if (argvec == error_mark_node)
11657 return error_mark_node;
11658
11659 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11660 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11661 }
11662 }
11663 break;
11664
11665 case TEMPLATE_PARM_INDEX:
11666 r = reduce_template_parm_level (t, type, levels, args, complain);
11667 break;
11668
11669 default:
11670 gcc_unreachable ();
11671 }
11672
11673 return r;
11674 }
11675
11676 case TREE_LIST:
11677 {
11678 tree purpose, value, chain;
11679
11680 if (t == void_list_node)
11681 return t;
11682
11683 purpose = TREE_PURPOSE (t);
11684 if (purpose)
11685 {
11686 purpose = tsubst (purpose, args, complain, in_decl);
11687 if (purpose == error_mark_node)
11688 return error_mark_node;
11689 }
11690 value = TREE_VALUE (t);
11691 if (value)
11692 {
11693 value = tsubst (value, args, complain, in_decl);
11694 if (value == error_mark_node)
11695 return error_mark_node;
11696 }
11697 chain = TREE_CHAIN (t);
11698 if (chain && chain != void_type_node)
11699 {
11700 chain = tsubst (chain, args, complain, in_decl);
11701 if (chain == error_mark_node)
11702 return error_mark_node;
11703 }
11704 if (purpose == TREE_PURPOSE (t)
11705 && value == TREE_VALUE (t)
11706 && chain == TREE_CHAIN (t))
11707 return t;
11708 return hash_tree_cons (purpose, value, chain);
11709 }
11710
11711 case TREE_BINFO:
11712 /* We should never be tsubsting a binfo. */
11713 gcc_unreachable ();
11714
11715 case TREE_VEC:
11716 /* A vector of template arguments. */
11717 gcc_assert (!type);
11718 return tsubst_template_args (t, args, complain, in_decl);
11719
11720 case POINTER_TYPE:
11721 case REFERENCE_TYPE:
11722 {
11723 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11724 return t;
11725
11726 /* [temp.deduct]
11727
11728 Type deduction may fail for any of the following
11729 reasons:
11730
11731 -- Attempting to create a pointer to reference type.
11732 -- Attempting to create a reference to a reference type or
11733 a reference to void.
11734
11735 Core issue 106 says that creating a reference to a reference
11736 during instantiation is no longer a cause for failure. We
11737 only enforce this check in strict C++98 mode. */
11738 if ((TREE_CODE (type) == REFERENCE_TYPE
11739 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11740 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
11741 {
11742 static location_t last_loc;
11743
11744 /* We keep track of the last time we issued this error
11745 message to avoid spewing a ton of messages during a
11746 single bad template instantiation. */
11747 if (complain & tf_error
11748 && last_loc != input_location)
11749 {
11750 if (VOID_TYPE_P (type))
11751 error ("forming reference to void");
11752 else if (code == POINTER_TYPE)
11753 error ("forming pointer to reference type %qT", type);
11754 else
11755 error ("forming reference to reference type %qT", type);
11756 last_loc = input_location;
11757 }
11758
11759 return error_mark_node;
11760 }
11761 else if (TREE_CODE (type) == FUNCTION_TYPE
11762 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
11763 || type_memfn_rqual (type) != REF_QUAL_NONE))
11764 {
11765 if (complain & tf_error)
11766 {
11767 if (code == POINTER_TYPE)
11768 error ("forming pointer to qualified function type %qT",
11769 type);
11770 else
11771 error ("forming reference to qualified function type %qT",
11772 type);
11773 }
11774 return error_mark_node;
11775 }
11776 else if (code == POINTER_TYPE)
11777 {
11778 r = build_pointer_type (type);
11779 if (TREE_CODE (type) == METHOD_TYPE)
11780 r = build_ptrmemfunc_type (r);
11781 }
11782 else if (TREE_CODE (type) == REFERENCE_TYPE)
11783 /* In C++0x, during template argument substitution, when there is an
11784 attempt to create a reference to a reference type, reference
11785 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11786
11787 "If a template-argument for a template-parameter T names a type
11788 that is a reference to a type A, an attempt to create the type
11789 'lvalue reference to cv T' creates the type 'lvalue reference to
11790 A,' while an attempt to create the type type rvalue reference to
11791 cv T' creates the type T"
11792 */
11793 r = cp_build_reference_type
11794 (TREE_TYPE (type),
11795 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11796 else
11797 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11798 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11799
11800 if (cxx_dialect >= cxx1y
11801 && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t))
11802 && array_of_runtime_bound_p (type))
11803 {
11804 if (complain & tf_warning_or_error)
11805 pedwarn
11806 (input_location, OPT_Wvla,
11807 code == REFERENCE_TYPE
11808 ? G_("cannot declare reference to array of runtime bound")
11809 : G_("cannot declare pointer to array of runtime bound"));
11810 else
11811 r = error_mark_node;
11812 }
11813
11814 if (r != error_mark_node)
11815 /* Will this ever be needed for TYPE_..._TO values? */
11816 layout_type (r);
11817
11818 return r;
11819 }
11820 case OFFSET_TYPE:
11821 {
11822 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11823 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11824 {
11825 /* [temp.deduct]
11826
11827 Type deduction may fail for any of the following
11828 reasons:
11829
11830 -- Attempting to create "pointer to member of T" when T
11831 is not a class type. */
11832 if (complain & tf_error)
11833 error ("creating pointer to member of non-class type %qT", r);
11834 return error_mark_node;
11835 }
11836 if (TREE_CODE (type) == REFERENCE_TYPE)
11837 {
11838 if (complain & tf_error)
11839 error ("creating pointer to member reference type %qT", type);
11840 return error_mark_node;
11841 }
11842 if (VOID_TYPE_P (type))
11843 {
11844 if (complain & tf_error)
11845 error ("creating pointer to member of type void");
11846 return error_mark_node;
11847 }
11848 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11849 if (TREE_CODE (type) == FUNCTION_TYPE)
11850 {
11851 /* The type of the implicit object parameter gets its
11852 cv-qualifiers from the FUNCTION_TYPE. */
11853 tree memptr;
11854 tree method_type
11855 = build_memfn_type (type, r, type_memfn_quals (type),
11856 type_memfn_rqual (type));
11857 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11858 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11859 complain);
11860 }
11861 else
11862 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11863 cp_type_quals (t),
11864 complain);
11865 }
11866 case FUNCTION_TYPE:
11867 case METHOD_TYPE:
11868 {
11869 tree fntype;
11870 tree specs;
11871 fntype = tsubst_function_type (t, args, complain, in_decl);
11872 if (fntype == error_mark_node)
11873 return error_mark_node;
11874
11875 /* Substitute the exception specification. */
11876 specs = tsubst_exception_specification (t, args, complain,
11877 in_decl, /*defer_ok*/true);
11878 if (specs == error_mark_node)
11879 return error_mark_node;
11880 if (specs)
11881 fntype = build_exception_variant (fntype, specs);
11882 return fntype;
11883 }
11884 case ARRAY_TYPE:
11885 {
11886 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11887 if (domain == error_mark_node)
11888 return error_mark_node;
11889
11890 /* As an optimization, we avoid regenerating the array type if
11891 it will obviously be the same as T. */
11892 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11893 return t;
11894
11895 /* These checks should match the ones in grokdeclarator.
11896
11897 [temp.deduct]
11898
11899 The deduction may fail for any of the following reasons:
11900
11901 -- Attempting to create an array with an element type that
11902 is void, a function type, or a reference type, or [DR337]
11903 an abstract class type. */
11904 if (VOID_TYPE_P (type)
11905 || TREE_CODE (type) == FUNCTION_TYPE
11906 || TREE_CODE (type) == REFERENCE_TYPE)
11907 {
11908 if (complain & tf_error)
11909 error ("creating array of %qT", type);
11910 return error_mark_node;
11911 }
11912
11913 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
11914 return error_mark_node;
11915
11916 r = build_cplus_array_type (type, domain);
11917
11918 if (TYPE_USER_ALIGN (t))
11919 {
11920 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11921 TYPE_USER_ALIGN (r) = 1;
11922 }
11923
11924 return r;
11925 }
11926
11927 case TYPENAME_TYPE:
11928 {
11929 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11930 in_decl, /*entering_scope=*/1);
11931 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11932 complain, in_decl);
11933
11934 if (ctx == error_mark_node || f == error_mark_node)
11935 return error_mark_node;
11936
11937 if (!MAYBE_CLASS_TYPE_P (ctx))
11938 {
11939 if (complain & tf_error)
11940 error ("%qT is not a class, struct, or union type", ctx);
11941 return error_mark_node;
11942 }
11943 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11944 {
11945 /* Normally, make_typename_type does not require that the CTX
11946 have complete type in order to allow things like:
11947
11948 template <class T> struct S { typename S<T>::X Y; };
11949
11950 But, such constructs have already been resolved by this
11951 point, so here CTX really should have complete type, unless
11952 it's a partial instantiation. */
11953 ctx = complete_type (ctx);
11954 if (!COMPLETE_TYPE_P (ctx))
11955 {
11956 if (complain & tf_error)
11957 cxx_incomplete_type_error (NULL_TREE, ctx);
11958 return error_mark_node;
11959 }
11960 }
11961
11962 f = make_typename_type (ctx, f, typename_type,
11963 complain | tf_keep_type_decl);
11964 if (f == error_mark_node)
11965 return f;
11966 if (TREE_CODE (f) == TYPE_DECL)
11967 {
11968 complain |= tf_ignore_bad_quals;
11969 f = TREE_TYPE (f);
11970 }
11971
11972 if (TREE_CODE (f) != TYPENAME_TYPE)
11973 {
11974 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11975 {
11976 if (complain & tf_error)
11977 error ("%qT resolves to %qT, which is not an enumeration type",
11978 t, f);
11979 else
11980 return error_mark_node;
11981 }
11982 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11983 {
11984 if (complain & tf_error)
11985 error ("%qT resolves to %qT, which is is not a class type",
11986 t, f);
11987 else
11988 return error_mark_node;
11989 }
11990 }
11991
11992 return cp_build_qualified_type_real
11993 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11994 }
11995
11996 case UNBOUND_CLASS_TEMPLATE:
11997 {
11998 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11999 in_decl, /*entering_scope=*/1);
12000 tree name = TYPE_IDENTIFIER (t);
12001 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
12002
12003 if (ctx == error_mark_node || name == error_mark_node)
12004 return error_mark_node;
12005
12006 if (parm_list)
12007 parm_list = tsubst_template_parms (parm_list, args, complain);
12008 return make_unbound_class_template (ctx, name, parm_list, complain);
12009 }
12010
12011 case TYPEOF_TYPE:
12012 {
12013 tree type;
12014
12015 ++cp_unevaluated_operand;
12016 ++c_inhibit_evaluation_warnings;
12017
12018 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
12019 complain, in_decl,
12020 /*integral_constant_expression_p=*/false);
12021
12022 --cp_unevaluated_operand;
12023 --c_inhibit_evaluation_warnings;
12024
12025 type = finish_typeof (type);
12026 return cp_build_qualified_type_real (type,
12027 cp_type_quals (t)
12028 | cp_type_quals (type),
12029 complain);
12030 }
12031
12032 case DECLTYPE_TYPE:
12033 {
12034 tree type;
12035
12036 ++cp_unevaluated_operand;
12037 ++c_inhibit_evaluation_warnings;
12038
12039 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
12040 complain|tf_decltype, in_decl,
12041 /*function_p*/false,
12042 /*integral_constant_expression*/false);
12043
12044 --cp_unevaluated_operand;
12045 --c_inhibit_evaluation_warnings;
12046
12047 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
12048 type = lambda_capture_field_type (type,
12049 DECLTYPE_FOR_INIT_CAPTURE (t));
12050 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
12051 type = lambda_proxy_type (type);
12052 else
12053 {
12054 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
12055 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
12056 && EXPR_P (type))
12057 /* In a template ~id could be either a complement expression
12058 or an unqualified-id naming a destructor; if instantiating
12059 it produces an expression, it's not an id-expression or
12060 member access. */
12061 id = false;
12062 type = finish_decltype_type (type, id, complain);
12063 }
12064 return cp_build_qualified_type_real (type,
12065 cp_type_quals (t)
12066 | cp_type_quals (type),
12067 complain);
12068 }
12069
12070 case UNDERLYING_TYPE:
12071 {
12072 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
12073 complain, in_decl);
12074 return finish_underlying_type (type);
12075 }
12076
12077 case TYPE_ARGUMENT_PACK:
12078 case NONTYPE_ARGUMENT_PACK:
12079 {
12080 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
12081 tree packed_out =
12082 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
12083 args,
12084 complain,
12085 in_decl);
12086 SET_ARGUMENT_PACK_ARGS (r, packed_out);
12087
12088 /* For template nontype argument packs, also substitute into
12089 the type. */
12090 if (code == NONTYPE_ARGUMENT_PACK)
12091 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
12092
12093 return r;
12094 }
12095 break;
12096
12097 case INTEGER_CST:
12098 case REAL_CST:
12099 case STRING_CST:
12100 case PLUS_EXPR:
12101 case MINUS_EXPR:
12102 case NEGATE_EXPR:
12103 case NOP_EXPR:
12104 case INDIRECT_REF:
12105 case ADDR_EXPR:
12106 case CALL_EXPR:
12107 case ARRAY_REF:
12108 case SCOPE_REF:
12109 /* We should use one of the expression tsubsts for these codes. */
12110 gcc_unreachable ();
12111
12112 default:
12113 sorry ("use of %qs in template", get_tree_code_name (code));
12114 return error_mark_node;
12115 }
12116 }
12117
12118 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
12119 type of the expression on the left-hand side of the "." or "->"
12120 operator. */
12121
12122 static tree
12123 tsubst_baselink (tree baselink, tree object_type,
12124 tree args, tsubst_flags_t complain, tree in_decl)
12125 {
12126 tree name;
12127 tree qualifying_scope;
12128 tree fns;
12129 tree optype;
12130 tree template_args = 0;
12131 bool template_id_p = false;
12132 bool qualified = BASELINK_QUALIFIED_P (baselink);
12133
12134 /* A baselink indicates a function from a base class. Both the
12135 BASELINK_ACCESS_BINFO and the base class referenced may
12136 indicate bases of the template class, rather than the
12137 instantiated class. In addition, lookups that were not
12138 ambiguous before may be ambiguous now. Therefore, we perform
12139 the lookup again. */
12140 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
12141 qualifying_scope = tsubst (qualifying_scope, args,
12142 complain, in_decl);
12143 fns = BASELINK_FUNCTIONS (baselink);
12144 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
12145 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
12146 {
12147 template_id_p = true;
12148 template_args = TREE_OPERAND (fns, 1);
12149 fns = TREE_OPERAND (fns, 0);
12150 if (template_args)
12151 template_args = tsubst_template_args (template_args, args,
12152 complain, in_decl);
12153 }
12154 name = DECL_NAME (get_first_fn (fns));
12155 if (IDENTIFIER_TYPENAME_P (name))
12156 name = mangle_conv_op_name_for_type (optype);
12157 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
12158 if (!baselink)
12159 return error_mark_node;
12160
12161 /* If lookup found a single function, mark it as used at this
12162 point. (If it lookup found multiple functions the one selected
12163 later by overload resolution will be marked as used at that
12164 point.) */
12165 if (BASELINK_P (baselink))
12166 fns = BASELINK_FUNCTIONS (baselink);
12167 if (!template_id_p && !really_overloaded_fn (fns))
12168 mark_used (OVL_CURRENT (fns));
12169
12170 /* Add back the template arguments, if present. */
12171 if (BASELINK_P (baselink) && template_id_p)
12172 BASELINK_FUNCTIONS (baselink)
12173 = build_nt (TEMPLATE_ID_EXPR,
12174 BASELINK_FUNCTIONS (baselink),
12175 template_args);
12176 /* Update the conversion operator type. */
12177 BASELINK_OPTYPE (baselink) = optype;
12178
12179 if (!object_type)
12180 object_type = current_class_type;
12181
12182 if (qualified)
12183 baselink = adjust_result_of_qualified_name_lookup (baselink,
12184 qualifying_scope,
12185 object_type);
12186 return baselink;
12187 }
12188
12189 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
12190 true if the qualified-id will be a postfix-expression in-and-of
12191 itself; false if more of the postfix-expression follows the
12192 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
12193 of "&". */
12194
12195 static tree
12196 tsubst_qualified_id (tree qualified_id, tree args,
12197 tsubst_flags_t complain, tree in_decl,
12198 bool done, bool address_p)
12199 {
12200 tree expr;
12201 tree scope;
12202 tree name;
12203 bool is_template;
12204 tree template_args;
12205 location_t loc = UNKNOWN_LOCATION;
12206
12207 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
12208
12209 /* Figure out what name to look up. */
12210 name = TREE_OPERAND (qualified_id, 1);
12211 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12212 {
12213 is_template = true;
12214 loc = EXPR_LOCATION (name);
12215 template_args = TREE_OPERAND (name, 1);
12216 if (template_args)
12217 template_args = tsubst_template_args (template_args, args,
12218 complain, in_decl);
12219 name = TREE_OPERAND (name, 0);
12220 }
12221 else
12222 {
12223 is_template = false;
12224 template_args = NULL_TREE;
12225 }
12226
12227 /* Substitute into the qualifying scope. When there are no ARGS, we
12228 are just trying to simplify a non-dependent expression. In that
12229 case the qualifying scope may be dependent, and, in any case,
12230 substituting will not help. */
12231 scope = TREE_OPERAND (qualified_id, 0);
12232 if (args)
12233 {
12234 scope = tsubst (scope, args, complain, in_decl);
12235 expr = tsubst_copy (name, args, complain, in_decl);
12236 }
12237 else
12238 expr = name;
12239
12240 if (dependent_scope_p (scope))
12241 {
12242 if (is_template)
12243 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
12244 return build_qualified_name (NULL_TREE, scope, expr,
12245 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
12246 }
12247
12248 if (!BASELINK_P (name) && !DECL_P (expr))
12249 {
12250 if (TREE_CODE (expr) == BIT_NOT_EXPR)
12251 {
12252 /* A BIT_NOT_EXPR is used to represent a destructor. */
12253 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
12254 {
12255 error ("qualifying type %qT does not match destructor name ~%qT",
12256 scope, TREE_OPERAND (expr, 0));
12257 expr = error_mark_node;
12258 }
12259 else
12260 expr = lookup_qualified_name (scope, complete_dtor_identifier,
12261 /*is_type_p=*/0, false);
12262 }
12263 else
12264 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12265 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
12266 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
12267 {
12268 if (complain & tf_error)
12269 {
12270 error ("dependent-name %qE is parsed as a non-type, but "
12271 "instantiation yields a type", qualified_id);
12272 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
12273 }
12274 return error_mark_node;
12275 }
12276 }
12277
12278 if (DECL_P (expr))
12279 {
12280 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
12281 scope);
12282 /* Remember that there was a reference to this entity. */
12283 mark_used (expr);
12284 }
12285
12286 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
12287 {
12288 if (complain & tf_error)
12289 qualified_name_lookup_error (scope,
12290 TREE_OPERAND (qualified_id, 1),
12291 expr, input_location);
12292 return error_mark_node;
12293 }
12294
12295 if (is_template)
12296 expr = lookup_template_function (expr, template_args);
12297
12298 if (expr == error_mark_node && complain & tf_error)
12299 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
12300 expr, input_location);
12301 else if (TYPE_P (scope))
12302 {
12303 expr = (adjust_result_of_qualified_name_lookup
12304 (expr, scope, current_nonlambda_class_type ()));
12305 expr = (finish_qualified_id_expr
12306 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
12307 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
12308 /*template_arg_p=*/false, complain));
12309 }
12310
12311 /* Expressions do not generally have reference type. */
12312 if (TREE_CODE (expr) != SCOPE_REF
12313 /* However, if we're about to form a pointer-to-member, we just
12314 want the referenced member referenced. */
12315 && TREE_CODE (expr) != OFFSET_REF)
12316 expr = convert_from_reference (expr);
12317
12318 return expr;
12319 }
12320
12321 /* Like tsubst, but deals with expressions. This function just replaces
12322 template parms; to finish processing the resultant expression, use
12323 tsubst_copy_and_build or tsubst_expr. */
12324
12325 static tree
12326 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12327 {
12328 enum tree_code code;
12329 tree r;
12330
12331 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
12332 return t;
12333
12334 code = TREE_CODE (t);
12335
12336 switch (code)
12337 {
12338 case PARM_DECL:
12339 r = retrieve_local_specialization (t);
12340
12341 if (r == NULL_TREE)
12342 {
12343 /* We get here for a use of 'this' in an NSDMI. */
12344 if (DECL_NAME (t) == this_identifier
12345 && at_function_scope_p ()
12346 && DECL_CONSTRUCTOR_P (current_function_decl))
12347 return current_class_ptr;
12348
12349 /* This can happen for a parameter name used later in a function
12350 declaration (such as in a late-specified return type). Just
12351 make a dummy decl, since it's only used for its type. */
12352 gcc_assert (cp_unevaluated_operand != 0);
12353 r = tsubst_decl (t, args, complain);
12354 /* Give it the template pattern as its context; its true context
12355 hasn't been instantiated yet and this is good enough for
12356 mangling. */
12357 DECL_CONTEXT (r) = DECL_CONTEXT (t);
12358 }
12359
12360 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12361 r = ARGUMENT_PACK_SELECT_ARG (r);
12362 mark_used (r);
12363 return r;
12364
12365 case CONST_DECL:
12366 {
12367 tree enum_type;
12368 tree v;
12369
12370 if (DECL_TEMPLATE_PARM_P (t))
12371 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
12372 /* There is no need to substitute into namespace-scope
12373 enumerators. */
12374 if (DECL_NAMESPACE_SCOPE_P (t))
12375 return t;
12376 /* If ARGS is NULL, then T is known to be non-dependent. */
12377 if (args == NULL_TREE)
12378 return integral_constant_value (t);
12379
12380 /* Unfortunately, we cannot just call lookup_name here.
12381 Consider:
12382
12383 template <int I> int f() {
12384 enum E { a = I };
12385 struct S { void g() { E e = a; } };
12386 };
12387
12388 When we instantiate f<7>::S::g(), say, lookup_name is not
12389 clever enough to find f<7>::a. */
12390 enum_type
12391 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12392 /*entering_scope=*/0);
12393
12394 for (v = TYPE_VALUES (enum_type);
12395 v != NULL_TREE;
12396 v = TREE_CHAIN (v))
12397 if (TREE_PURPOSE (v) == DECL_NAME (t))
12398 return TREE_VALUE (v);
12399
12400 /* We didn't find the name. That should never happen; if
12401 name-lookup found it during preliminary parsing, we
12402 should find it again here during instantiation. */
12403 gcc_unreachable ();
12404 }
12405 return t;
12406
12407 case FIELD_DECL:
12408 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12409 {
12410 /* Check for a local specialization set up by
12411 tsubst_pack_expansion. */
12412 tree r = retrieve_local_specialization (t);
12413 if (r)
12414 {
12415 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
12416 r = ARGUMENT_PACK_SELECT_ARG (r);
12417 return r;
12418 }
12419
12420 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
12421 tsubst_decl put in the hash table. */
12422 return retrieve_specialization (t, args, 0);
12423 }
12424
12425 if (DECL_CONTEXT (t))
12426 {
12427 tree ctx;
12428
12429 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
12430 /*entering_scope=*/1);
12431 if (ctx != DECL_CONTEXT (t))
12432 {
12433 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
12434 if (!r)
12435 {
12436 if (complain & tf_error)
12437 error ("using invalid field %qD", t);
12438 return error_mark_node;
12439 }
12440 return r;
12441 }
12442 }
12443
12444 return t;
12445
12446 case VAR_DECL:
12447 case FUNCTION_DECL:
12448 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12449 r = tsubst (t, args, complain, in_decl);
12450 else if (local_variable_p (t))
12451 {
12452 r = retrieve_local_specialization (t);
12453 if (r == NULL_TREE)
12454 {
12455 if (DECL_ANON_UNION_VAR_P (t))
12456 {
12457 /* Just use name lookup to find a member alias for an
12458 anonymous union, but then add it to the hash table. */
12459 r = lookup_name (DECL_NAME (t));
12460 gcc_assert (DECL_ANON_UNION_VAR_P (r));
12461 register_local_specialization (r, t);
12462 }
12463 else
12464 {
12465 gcc_assert (errorcount || sorrycount);
12466 return error_mark_node;
12467 }
12468 }
12469 }
12470 else
12471 r = t;
12472 mark_used (r);
12473 return r;
12474
12475 case NAMESPACE_DECL:
12476 return t;
12477
12478 case OVERLOAD:
12479 /* An OVERLOAD will always be a non-dependent overload set; an
12480 overload set from function scope will just be represented with an
12481 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12482 gcc_assert (!uses_template_parms (t));
12483 return t;
12484
12485 case BASELINK:
12486 return tsubst_baselink (t, current_nonlambda_class_type (),
12487 args, complain, in_decl);
12488
12489 case TEMPLATE_DECL:
12490 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12491 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12492 args, complain, in_decl);
12493 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12494 return tsubst (t, args, complain, in_decl);
12495 else if (DECL_CLASS_SCOPE_P (t)
12496 && uses_template_parms (DECL_CONTEXT (t)))
12497 {
12498 /* Template template argument like the following example need
12499 special treatment:
12500
12501 template <template <class> class TT> struct C {};
12502 template <class T> struct D {
12503 template <class U> struct E {};
12504 C<E> c; // #1
12505 };
12506 D<int> d; // #2
12507
12508 We are processing the template argument `E' in #1 for
12509 the template instantiation #2. Originally, `E' is a
12510 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12511 have to substitute this with one having context `D<int>'. */
12512
12513 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12514 return lookup_field (context, DECL_NAME(t), 0, false);
12515 }
12516 else
12517 /* Ordinary template template argument. */
12518 return t;
12519
12520 case CAST_EXPR:
12521 case REINTERPRET_CAST_EXPR:
12522 case CONST_CAST_EXPR:
12523 case STATIC_CAST_EXPR:
12524 case DYNAMIC_CAST_EXPR:
12525 case IMPLICIT_CONV_EXPR:
12526 case CONVERT_EXPR:
12527 case NOP_EXPR:
12528 return build1
12529 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12530 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12531
12532 case SIZEOF_EXPR:
12533 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12534 {
12535
12536 tree expanded, op = TREE_OPERAND (t, 0);
12537 int len = 0;
12538
12539 if (SIZEOF_EXPR_TYPE_P (t))
12540 op = TREE_TYPE (op);
12541
12542 ++cp_unevaluated_operand;
12543 ++c_inhibit_evaluation_warnings;
12544 /* We only want to compute the number of arguments. */
12545 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12546 --cp_unevaluated_operand;
12547 --c_inhibit_evaluation_warnings;
12548
12549 if (TREE_CODE (expanded) == TREE_VEC)
12550 len = TREE_VEC_LENGTH (expanded);
12551
12552 if (expanded == error_mark_node)
12553 return error_mark_node;
12554 else if (PACK_EXPANSION_P (expanded)
12555 || (TREE_CODE (expanded) == TREE_VEC
12556 && len > 0
12557 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12558 {
12559 if (TREE_CODE (expanded) == TREE_VEC)
12560 expanded = TREE_VEC_ELT (expanded, len - 1);
12561
12562 if (TYPE_P (expanded))
12563 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12564 complain & tf_error);
12565 else
12566 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12567 complain & tf_error);
12568 }
12569 else
12570 return build_int_cst (size_type_node, len);
12571 }
12572 if (SIZEOF_EXPR_TYPE_P (t))
12573 {
12574 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12575 args, complain, in_decl);
12576 r = build1 (NOP_EXPR, r, error_mark_node);
12577 r = build1 (SIZEOF_EXPR,
12578 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12579 SIZEOF_EXPR_TYPE_P (r) = 1;
12580 return r;
12581 }
12582 /* Fall through */
12583
12584 case INDIRECT_REF:
12585 case NEGATE_EXPR:
12586 case TRUTH_NOT_EXPR:
12587 case BIT_NOT_EXPR:
12588 case ADDR_EXPR:
12589 case UNARY_PLUS_EXPR: /* Unary + */
12590 case ALIGNOF_EXPR:
12591 case AT_ENCODE_EXPR:
12592 case ARROW_EXPR:
12593 case THROW_EXPR:
12594 case TYPEID_EXPR:
12595 case REALPART_EXPR:
12596 case IMAGPART_EXPR:
12597 case PAREN_EXPR:
12598 return build1
12599 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12600 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12601
12602 case COMPONENT_REF:
12603 {
12604 tree object;
12605 tree name;
12606
12607 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12608 name = TREE_OPERAND (t, 1);
12609 if (TREE_CODE (name) == BIT_NOT_EXPR)
12610 {
12611 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12612 complain, in_decl);
12613 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12614 }
12615 else if (TREE_CODE (name) == SCOPE_REF
12616 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12617 {
12618 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12619 complain, in_decl);
12620 name = TREE_OPERAND (name, 1);
12621 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12622 complain, in_decl);
12623 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12624 name = build_qualified_name (/*type=*/NULL_TREE,
12625 base, name,
12626 /*template_p=*/false);
12627 }
12628 else if (BASELINK_P (name))
12629 name = tsubst_baselink (name,
12630 non_reference (TREE_TYPE (object)),
12631 args, complain,
12632 in_decl);
12633 else
12634 name = tsubst_copy (name, args, complain, in_decl);
12635 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12636 }
12637
12638 case PLUS_EXPR:
12639 case MINUS_EXPR:
12640 case MULT_EXPR:
12641 case TRUNC_DIV_EXPR:
12642 case CEIL_DIV_EXPR:
12643 case FLOOR_DIV_EXPR:
12644 case ROUND_DIV_EXPR:
12645 case EXACT_DIV_EXPR:
12646 case BIT_AND_EXPR:
12647 case BIT_IOR_EXPR:
12648 case BIT_XOR_EXPR:
12649 case TRUNC_MOD_EXPR:
12650 case FLOOR_MOD_EXPR:
12651 case TRUTH_ANDIF_EXPR:
12652 case TRUTH_ORIF_EXPR:
12653 case TRUTH_AND_EXPR:
12654 case TRUTH_OR_EXPR:
12655 case RSHIFT_EXPR:
12656 case LSHIFT_EXPR:
12657 case RROTATE_EXPR:
12658 case LROTATE_EXPR:
12659 case EQ_EXPR:
12660 case NE_EXPR:
12661 case MAX_EXPR:
12662 case MIN_EXPR:
12663 case LE_EXPR:
12664 case GE_EXPR:
12665 case LT_EXPR:
12666 case GT_EXPR:
12667 case COMPOUND_EXPR:
12668 case DOTSTAR_EXPR:
12669 case MEMBER_REF:
12670 case PREDECREMENT_EXPR:
12671 case PREINCREMENT_EXPR:
12672 case POSTDECREMENT_EXPR:
12673 case POSTINCREMENT_EXPR:
12674 return build_nt
12675 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12676 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12677
12678 case SCOPE_REF:
12679 return build_qualified_name (/*type=*/NULL_TREE,
12680 tsubst_copy (TREE_OPERAND (t, 0),
12681 args, complain, in_decl),
12682 tsubst_copy (TREE_OPERAND (t, 1),
12683 args, complain, in_decl),
12684 QUALIFIED_NAME_IS_TEMPLATE (t));
12685
12686 case ARRAY_REF:
12687 return build_nt
12688 (ARRAY_REF,
12689 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12690 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12691 NULL_TREE, NULL_TREE);
12692
12693 case CALL_EXPR:
12694 {
12695 int n = VL_EXP_OPERAND_LENGTH (t);
12696 tree result = build_vl_exp (CALL_EXPR, n);
12697 int i;
12698 for (i = 0; i < n; i++)
12699 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12700 complain, in_decl);
12701 return result;
12702 }
12703
12704 case COND_EXPR:
12705 case MODOP_EXPR:
12706 case PSEUDO_DTOR_EXPR:
12707 case VEC_PERM_EXPR:
12708 {
12709 r = build_nt
12710 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12711 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12712 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12713 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12714 return r;
12715 }
12716
12717 case NEW_EXPR:
12718 {
12719 r = build_nt
12720 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12721 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12722 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12723 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12724 return r;
12725 }
12726
12727 case DELETE_EXPR:
12728 {
12729 r = build_nt
12730 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12731 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12732 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12733 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12734 return r;
12735 }
12736
12737 case TEMPLATE_ID_EXPR:
12738 {
12739 /* Substituted template arguments */
12740 tree fn = TREE_OPERAND (t, 0);
12741 tree targs = TREE_OPERAND (t, 1);
12742
12743 fn = tsubst_copy (fn, args, complain, in_decl);
12744 if (targs)
12745 targs = tsubst_template_args (targs, args, complain, in_decl);
12746
12747 return lookup_template_function (fn, targs);
12748 }
12749
12750 case TREE_LIST:
12751 {
12752 tree purpose, value, chain;
12753
12754 if (t == void_list_node)
12755 return t;
12756
12757 purpose = TREE_PURPOSE (t);
12758 if (purpose)
12759 purpose = tsubst_copy (purpose, args, complain, in_decl);
12760 value = TREE_VALUE (t);
12761 if (value)
12762 value = tsubst_copy (value, args, complain, in_decl);
12763 chain = TREE_CHAIN (t);
12764 if (chain && chain != void_type_node)
12765 chain = tsubst_copy (chain, args, complain, in_decl);
12766 if (purpose == TREE_PURPOSE (t)
12767 && value == TREE_VALUE (t)
12768 && chain == TREE_CHAIN (t))
12769 return t;
12770 return tree_cons (purpose, value, chain);
12771 }
12772
12773 case RECORD_TYPE:
12774 case UNION_TYPE:
12775 case ENUMERAL_TYPE:
12776 case INTEGER_TYPE:
12777 case TEMPLATE_TYPE_PARM:
12778 case TEMPLATE_TEMPLATE_PARM:
12779 case BOUND_TEMPLATE_TEMPLATE_PARM:
12780 case TEMPLATE_PARM_INDEX:
12781 case POINTER_TYPE:
12782 case REFERENCE_TYPE:
12783 case OFFSET_TYPE:
12784 case FUNCTION_TYPE:
12785 case METHOD_TYPE:
12786 case ARRAY_TYPE:
12787 case TYPENAME_TYPE:
12788 case UNBOUND_CLASS_TEMPLATE:
12789 case TYPEOF_TYPE:
12790 case DECLTYPE_TYPE:
12791 case TYPE_DECL:
12792 return tsubst (t, args, complain, in_decl);
12793
12794 case USING_DECL:
12795 t = DECL_NAME (t);
12796 /* Fall through. */
12797 case IDENTIFIER_NODE:
12798 if (IDENTIFIER_TYPENAME_P (t))
12799 {
12800 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12801 return mangle_conv_op_name_for_type (new_type);
12802 }
12803 else
12804 return t;
12805
12806 case CONSTRUCTOR:
12807 /* This is handled by tsubst_copy_and_build. */
12808 gcc_unreachable ();
12809
12810 case VA_ARG_EXPR:
12811 return build_x_va_arg (EXPR_LOCATION (t),
12812 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12813 in_decl),
12814 tsubst (TREE_TYPE (t), args, complain, in_decl));
12815
12816 case CLEANUP_POINT_EXPR:
12817 /* We shouldn't have built any of these during initial template
12818 generation. Instead, they should be built during instantiation
12819 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12820 gcc_unreachable ();
12821
12822 case OFFSET_REF:
12823 r = build2
12824 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12825 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12826 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12827 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12828 mark_used (TREE_OPERAND (r, 1));
12829 return r;
12830
12831 case EXPR_PACK_EXPANSION:
12832 error ("invalid use of pack expansion expression");
12833 return error_mark_node;
12834
12835 case NONTYPE_ARGUMENT_PACK:
12836 error ("use %<...%> to expand argument pack");
12837 return error_mark_node;
12838
12839 case INTEGER_CST:
12840 case REAL_CST:
12841 case STRING_CST:
12842 case COMPLEX_CST:
12843 {
12844 /* Instantiate any typedefs in the type. */
12845 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12846 r = fold_convert (type, t);
12847 gcc_assert (TREE_CODE (r) == code);
12848 return r;
12849 }
12850
12851 case PTRMEM_CST:
12852 /* These can sometimes show up in a partial instantiation, but never
12853 involve template parms. */
12854 gcc_assert (!uses_template_parms (t));
12855 return t;
12856
12857 default:
12858 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12859 gcc_checking_assert (false);
12860 return t;
12861 }
12862 }
12863
12864 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12865
12866 static tree
12867 tsubst_omp_clauses (tree clauses, bool declare_simd,
12868 tree args, tsubst_flags_t complain, tree in_decl)
12869 {
12870 tree new_clauses = NULL, nc, oc;
12871
12872 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12873 {
12874 nc = copy_node (oc);
12875 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12876 new_clauses = nc;
12877
12878 switch (OMP_CLAUSE_CODE (nc))
12879 {
12880 case OMP_CLAUSE_LASTPRIVATE:
12881 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12882 {
12883 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12884 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12885 in_decl, /*integral_constant_expression_p=*/false);
12886 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12887 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12888 }
12889 /* FALLTHRU */
12890 case OMP_CLAUSE_PRIVATE:
12891 case OMP_CLAUSE_SHARED:
12892 case OMP_CLAUSE_FIRSTPRIVATE:
12893 case OMP_CLAUSE_COPYIN:
12894 case OMP_CLAUSE_COPYPRIVATE:
12895 case OMP_CLAUSE_IF:
12896 case OMP_CLAUSE_NUM_THREADS:
12897 case OMP_CLAUSE_SCHEDULE:
12898 case OMP_CLAUSE_COLLAPSE:
12899 case OMP_CLAUSE_FINAL:
12900 case OMP_CLAUSE_DEPEND:
12901 case OMP_CLAUSE_FROM:
12902 case OMP_CLAUSE_TO:
12903 case OMP_CLAUSE_UNIFORM:
12904 case OMP_CLAUSE_MAP:
12905 case OMP_CLAUSE_DEVICE:
12906 case OMP_CLAUSE_DIST_SCHEDULE:
12907 case OMP_CLAUSE_NUM_TEAMS:
12908 case OMP_CLAUSE_THREAD_LIMIT:
12909 case OMP_CLAUSE_SAFELEN:
12910 case OMP_CLAUSE_SIMDLEN:
12911 OMP_CLAUSE_OPERAND (nc, 0)
12912 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12913 in_decl, /*integral_constant_expression_p=*/false);
12914 break;
12915 case OMP_CLAUSE_REDUCTION:
12916 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
12917 {
12918 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
12919 if (TREE_CODE (placeholder) == SCOPE_REF)
12920 {
12921 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
12922 complain, in_decl);
12923 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
12924 = build_qualified_name (NULL_TREE, scope,
12925 TREE_OPERAND (placeholder, 1),
12926 false);
12927 }
12928 else
12929 gcc_assert (identifier_p (placeholder));
12930 }
12931 OMP_CLAUSE_OPERAND (nc, 0)
12932 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12933 in_decl, /*integral_constant_expression_p=*/false);
12934 break;
12935 case OMP_CLAUSE_LINEAR:
12936 case OMP_CLAUSE_ALIGNED:
12937 OMP_CLAUSE_OPERAND (nc, 0)
12938 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12939 in_decl, /*integral_constant_expression_p=*/false);
12940 OMP_CLAUSE_OPERAND (nc, 1)
12941 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
12942 in_decl, /*integral_constant_expression_p=*/false);
12943 break;
12944
12945 case OMP_CLAUSE_NOWAIT:
12946 case OMP_CLAUSE_ORDERED:
12947 case OMP_CLAUSE_DEFAULT:
12948 case OMP_CLAUSE_UNTIED:
12949 case OMP_CLAUSE_MERGEABLE:
12950 case OMP_CLAUSE_INBRANCH:
12951 case OMP_CLAUSE_NOTINBRANCH:
12952 case OMP_CLAUSE_PROC_BIND:
12953 case OMP_CLAUSE_FOR:
12954 case OMP_CLAUSE_PARALLEL:
12955 case OMP_CLAUSE_SECTIONS:
12956 case OMP_CLAUSE_TASKGROUP:
12957 break;
12958 default:
12959 gcc_unreachable ();
12960 }
12961 }
12962
12963 new_clauses = nreverse (new_clauses);
12964 if (!declare_simd)
12965 new_clauses = finish_omp_clauses (new_clauses);
12966 return new_clauses;
12967 }
12968
12969 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12970
12971 static tree
12972 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12973 tree in_decl)
12974 {
12975 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12976
12977 tree purpose, value, chain;
12978
12979 if (t == NULL)
12980 return t;
12981
12982 if (TREE_CODE (t) != TREE_LIST)
12983 return tsubst_copy_and_build (t, args, complain, in_decl,
12984 /*function_p=*/false,
12985 /*integral_constant_expression_p=*/false);
12986
12987 if (t == void_list_node)
12988 return t;
12989
12990 purpose = TREE_PURPOSE (t);
12991 if (purpose)
12992 purpose = RECUR (purpose);
12993 value = TREE_VALUE (t);
12994 if (value)
12995 {
12996 if (TREE_CODE (value) != LABEL_DECL)
12997 value = RECUR (value);
12998 else
12999 {
13000 value = lookup_label (DECL_NAME (value));
13001 gcc_assert (TREE_CODE (value) == LABEL_DECL);
13002 TREE_USED (value) = 1;
13003 }
13004 }
13005 chain = TREE_CHAIN (t);
13006 if (chain && chain != void_type_node)
13007 chain = RECUR (chain);
13008 return tree_cons (purpose, value, chain);
13009 #undef RECUR
13010 }
13011
13012 /* Substitute one OMP_FOR iterator. */
13013
13014 static void
13015 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
13016 tree condv, tree incrv, tree *clauses,
13017 tree args, tsubst_flags_t complain, tree in_decl,
13018 bool integral_constant_expression_p)
13019 {
13020 #define RECUR(NODE) \
13021 tsubst_expr ((NODE), args, complain, in_decl, \
13022 integral_constant_expression_p)
13023 tree decl, init, cond, incr;
13024 bool init_decl;
13025
13026 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
13027 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
13028 decl = TREE_OPERAND (init, 0);
13029 init = TREE_OPERAND (init, 1);
13030 /* Do this before substituting into decl to handle 'auto'. */
13031 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
13032 init = RECUR (init);
13033 decl = RECUR (decl);
13034 if (init_decl)
13035 {
13036 gcc_assert (!processing_template_decl);
13037 init = DECL_INITIAL (decl);
13038 DECL_INITIAL (decl) = NULL_TREE;
13039 }
13040
13041 gcc_assert (!type_dependent_expression_p (decl));
13042
13043 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
13044 {
13045 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
13046 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13047 if (TREE_CODE (incr) == MODIFY_EXPR)
13048 incr = build_x_modify_expr (EXPR_LOCATION (incr),
13049 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
13050 RECUR (TREE_OPERAND (incr, 1)),
13051 complain);
13052 else
13053 incr = RECUR (incr);
13054 TREE_VEC_ELT (declv, i) = decl;
13055 TREE_VEC_ELT (initv, i) = init;
13056 TREE_VEC_ELT (condv, i) = cond;
13057 TREE_VEC_ELT (incrv, i) = incr;
13058 return;
13059 }
13060
13061 if (init && !init_decl)
13062 {
13063 tree c;
13064 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
13065 {
13066 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13067 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
13068 && OMP_CLAUSE_DECL (c) == decl)
13069 break;
13070 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13071 && OMP_CLAUSE_DECL (c) == decl)
13072 error ("iteration variable %qD should not be firstprivate", decl);
13073 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13074 && OMP_CLAUSE_DECL (c) == decl)
13075 error ("iteration variable %qD should not be reduction", decl);
13076 }
13077 if (c == NULL)
13078 {
13079 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
13080 OMP_CLAUSE_DECL (c) = decl;
13081 c = finish_omp_clauses (c);
13082 if (c)
13083 {
13084 OMP_CLAUSE_CHAIN (c) = *clauses;
13085 *clauses = c;
13086 }
13087 }
13088 }
13089 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
13090 if (COMPARISON_CLASS_P (cond))
13091 cond = build2 (TREE_CODE (cond), boolean_type_node,
13092 RECUR (TREE_OPERAND (cond, 0)),
13093 RECUR (TREE_OPERAND (cond, 1)));
13094 else
13095 cond = RECUR (cond);
13096 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
13097 switch (TREE_CODE (incr))
13098 {
13099 case PREINCREMENT_EXPR:
13100 case PREDECREMENT_EXPR:
13101 case POSTINCREMENT_EXPR:
13102 case POSTDECREMENT_EXPR:
13103 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
13104 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
13105 break;
13106 case MODIFY_EXPR:
13107 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13108 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13109 {
13110 tree rhs = TREE_OPERAND (incr, 1);
13111 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13112 RECUR (TREE_OPERAND (incr, 0)),
13113 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13114 RECUR (TREE_OPERAND (rhs, 0)),
13115 RECUR (TREE_OPERAND (rhs, 1))));
13116 }
13117 else
13118 incr = RECUR (incr);
13119 break;
13120 case MODOP_EXPR:
13121 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
13122 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
13123 {
13124 tree lhs = RECUR (TREE_OPERAND (incr, 0));
13125 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
13126 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
13127 TREE_TYPE (decl), lhs,
13128 RECUR (TREE_OPERAND (incr, 2))));
13129 }
13130 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
13131 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
13132 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
13133 {
13134 tree rhs = TREE_OPERAND (incr, 2);
13135 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
13136 RECUR (TREE_OPERAND (incr, 0)),
13137 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
13138 RECUR (TREE_OPERAND (rhs, 0)),
13139 RECUR (TREE_OPERAND (rhs, 1))));
13140 }
13141 else
13142 incr = RECUR (incr);
13143 break;
13144 default:
13145 incr = RECUR (incr);
13146 break;
13147 }
13148
13149 TREE_VEC_ELT (declv, i) = decl;
13150 TREE_VEC_ELT (initv, i) = init;
13151 TREE_VEC_ELT (condv, i) = cond;
13152 TREE_VEC_ELT (incrv, i) = incr;
13153 #undef RECUR
13154 }
13155
13156 /* Like tsubst_copy for expressions, etc. but also does semantic
13157 processing. */
13158
13159 static tree
13160 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
13161 bool integral_constant_expression_p)
13162 {
13163 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13164 #define RECUR(NODE) \
13165 tsubst_expr ((NODE), args, complain, in_decl, \
13166 integral_constant_expression_p)
13167
13168 tree stmt, tmp;
13169 tree r;
13170 location_t loc;
13171
13172 if (t == NULL_TREE || t == error_mark_node)
13173 return t;
13174
13175 loc = input_location;
13176 if (EXPR_HAS_LOCATION (t))
13177 input_location = EXPR_LOCATION (t);
13178 if (STATEMENT_CODE_P (TREE_CODE (t)))
13179 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
13180
13181 switch (TREE_CODE (t))
13182 {
13183 case STATEMENT_LIST:
13184 {
13185 tree_stmt_iterator i;
13186 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
13187 RECUR (tsi_stmt (i));
13188 break;
13189 }
13190
13191 case CTOR_INITIALIZER:
13192 finish_mem_initializers (tsubst_initializer_list
13193 (TREE_OPERAND (t, 0), args));
13194 break;
13195
13196 case RETURN_EXPR:
13197 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
13198 break;
13199
13200 case EXPR_STMT:
13201 tmp = RECUR (EXPR_STMT_EXPR (t));
13202 if (EXPR_STMT_STMT_EXPR_RESULT (t))
13203 finish_stmt_expr_expr (tmp, cur_stmt_expr);
13204 else
13205 finish_expr_stmt (tmp);
13206 break;
13207
13208 case USING_STMT:
13209 do_using_directive (USING_STMT_NAMESPACE (t));
13210 break;
13211
13212 case DECL_EXPR:
13213 {
13214 tree decl, pattern_decl;
13215 tree init;
13216
13217 pattern_decl = decl = DECL_EXPR_DECL (t);
13218 if (TREE_CODE (decl) == LABEL_DECL)
13219 finish_label_decl (DECL_NAME (decl));
13220 else if (TREE_CODE (decl) == USING_DECL)
13221 {
13222 tree scope = USING_DECL_SCOPE (decl);
13223 tree name = DECL_NAME (decl);
13224 tree decl;
13225
13226 scope = tsubst (scope, args, complain, in_decl);
13227 decl = lookup_qualified_name (scope, name,
13228 /*is_type_p=*/false,
13229 /*complain=*/false);
13230 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
13231 qualified_name_lookup_error (scope, name, decl, input_location);
13232 else
13233 do_local_using_decl (decl, scope, name);
13234 }
13235 else if (DECL_PACK_P (decl))
13236 {
13237 /* Don't build up decls for a variadic capture proxy, we'll
13238 instantiate the elements directly as needed. */
13239 break;
13240 }
13241 else
13242 {
13243 init = DECL_INITIAL (decl);
13244 decl = tsubst (decl, args, complain, in_decl);
13245 if (decl != error_mark_node)
13246 {
13247 /* By marking the declaration as instantiated, we avoid
13248 trying to instantiate it. Since instantiate_decl can't
13249 handle local variables, and since we've already done
13250 all that needs to be done, that's the right thing to
13251 do. */
13252 if (VAR_P (decl))
13253 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13254 if (VAR_P (decl)
13255 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13256 /* Anonymous aggregates are a special case. */
13257 finish_anon_union (decl);
13258 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
13259 {
13260 DECL_CONTEXT (decl) = current_function_decl;
13261 if (DECL_NAME (decl) == this_identifier)
13262 {
13263 tree lam = DECL_CONTEXT (current_function_decl);
13264 lam = CLASSTYPE_LAMBDA_EXPR (lam);
13265 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
13266 }
13267 insert_capture_proxy (decl);
13268 }
13269 else if (DECL_IMPLICIT_TYPEDEF_P (t))
13270 /* We already did a pushtag. */;
13271 else if (TREE_CODE (decl) == FUNCTION_DECL
13272 && DECL_OMP_DECLARE_REDUCTION_P (decl)
13273 && DECL_FUNCTION_SCOPE_P (pattern_decl))
13274 {
13275 DECL_CONTEXT (decl) = NULL_TREE;
13276 pushdecl (decl);
13277 DECL_CONTEXT (decl) = current_function_decl;
13278 cp_check_omp_declare_reduction (decl);
13279 }
13280 else
13281 {
13282 int const_init = false;
13283 maybe_push_decl (decl);
13284 if (VAR_P (decl)
13285 && DECL_PRETTY_FUNCTION_P (decl))
13286 {
13287 /* For __PRETTY_FUNCTION__ we have to adjust the
13288 initializer. */
13289 const char *const name
13290 = cxx_printable_name (current_function_decl, 2);
13291 init = cp_fname_init (name, &TREE_TYPE (decl));
13292 }
13293 else
13294 {
13295 tree t = RECUR (init);
13296
13297 if (init && !t)
13298 {
13299 /* If we had an initializer but it
13300 instantiated to nothing,
13301 value-initialize the object. This will
13302 only occur when the initializer was a
13303 pack expansion where the parameter packs
13304 used in that expansion were of length
13305 zero. */
13306 init = build_value_init (TREE_TYPE (decl),
13307 complain);
13308 if (TREE_CODE (init) == AGGR_INIT_EXPR)
13309 init = get_target_expr_sfinae (init, complain);
13310 }
13311 else
13312 init = t;
13313 }
13314
13315 if (VAR_P (decl))
13316 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
13317 (pattern_decl));
13318 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
13319 }
13320 }
13321 }
13322
13323 break;
13324 }
13325
13326 case FOR_STMT:
13327 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13328 RECUR (FOR_INIT_STMT (t));
13329 finish_for_init_stmt (stmt);
13330 tmp = RECUR (FOR_COND (t));
13331 finish_for_cond (tmp, stmt, false);
13332 tmp = RECUR (FOR_EXPR (t));
13333 finish_for_expr (tmp, stmt);
13334 RECUR (FOR_BODY (t));
13335 finish_for_stmt (stmt);
13336 break;
13337
13338 case RANGE_FOR_STMT:
13339 {
13340 tree decl, expr;
13341 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
13342 decl = RANGE_FOR_DECL (t);
13343 decl = tsubst (decl, args, complain, in_decl);
13344 maybe_push_decl (decl);
13345 expr = RECUR (RANGE_FOR_EXPR (t));
13346 stmt = cp_convert_range_for (stmt, decl, expr, RANGE_FOR_IVDEP (t));
13347 RECUR (RANGE_FOR_BODY (t));
13348 finish_for_stmt (stmt);
13349 }
13350 break;
13351
13352 case WHILE_STMT:
13353 stmt = begin_while_stmt ();
13354 tmp = RECUR (WHILE_COND (t));
13355 finish_while_stmt_cond (tmp, stmt, false);
13356 RECUR (WHILE_BODY (t));
13357 finish_while_stmt (stmt);
13358 break;
13359
13360 case DO_STMT:
13361 stmt = begin_do_stmt ();
13362 RECUR (DO_BODY (t));
13363 finish_do_body (stmt);
13364 tmp = RECUR (DO_COND (t));
13365 finish_do_stmt (tmp, stmt, false);
13366 break;
13367
13368 case IF_STMT:
13369 stmt = begin_if_stmt ();
13370 tmp = RECUR (IF_COND (t));
13371 finish_if_stmt_cond (tmp, stmt);
13372 RECUR (THEN_CLAUSE (t));
13373 finish_then_clause (stmt);
13374
13375 if (ELSE_CLAUSE (t))
13376 {
13377 begin_else_clause (stmt);
13378 RECUR (ELSE_CLAUSE (t));
13379 finish_else_clause (stmt);
13380 }
13381
13382 finish_if_stmt (stmt);
13383 break;
13384
13385 case BIND_EXPR:
13386 if (BIND_EXPR_BODY_BLOCK (t))
13387 stmt = begin_function_body ();
13388 else
13389 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
13390 ? BCS_TRY_BLOCK : 0);
13391
13392 RECUR (BIND_EXPR_BODY (t));
13393
13394 if (BIND_EXPR_BODY_BLOCK (t))
13395 finish_function_body (stmt);
13396 else
13397 finish_compound_stmt (stmt);
13398 break;
13399
13400 case BREAK_STMT:
13401 finish_break_stmt ();
13402 break;
13403
13404 case CONTINUE_STMT:
13405 finish_continue_stmt ();
13406 break;
13407
13408 case SWITCH_STMT:
13409 stmt = begin_switch_stmt ();
13410 tmp = RECUR (SWITCH_STMT_COND (t));
13411 finish_switch_cond (tmp, stmt);
13412 RECUR (SWITCH_STMT_BODY (t));
13413 finish_switch_stmt (stmt);
13414 break;
13415
13416 case CASE_LABEL_EXPR:
13417 finish_case_label (EXPR_LOCATION (t),
13418 RECUR (CASE_LOW (t)),
13419 RECUR (CASE_HIGH (t)));
13420 break;
13421
13422 case LABEL_EXPR:
13423 {
13424 tree decl = LABEL_EXPR_LABEL (t);
13425 tree label;
13426
13427 label = finish_label_stmt (DECL_NAME (decl));
13428 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
13429 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
13430 }
13431 break;
13432
13433 case GOTO_EXPR:
13434 tmp = GOTO_DESTINATION (t);
13435 if (TREE_CODE (tmp) != LABEL_DECL)
13436 /* Computed goto's must be tsubst'd into. On the other hand,
13437 non-computed gotos must not be; the identifier in question
13438 will have no binding. */
13439 tmp = RECUR (tmp);
13440 else
13441 tmp = DECL_NAME (tmp);
13442 finish_goto_stmt (tmp);
13443 break;
13444
13445 case ASM_EXPR:
13446 tmp = finish_asm_stmt
13447 (ASM_VOLATILE_P (t),
13448 RECUR (ASM_STRING (t)),
13449 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
13450 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
13451 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
13452 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
13453 {
13454 tree asm_expr = tmp;
13455 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
13456 asm_expr = TREE_OPERAND (asm_expr, 0);
13457 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
13458 }
13459 break;
13460
13461 case TRY_BLOCK:
13462 if (CLEANUP_P (t))
13463 {
13464 stmt = begin_try_block ();
13465 RECUR (TRY_STMTS (t));
13466 finish_cleanup_try_block (stmt);
13467 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
13468 }
13469 else
13470 {
13471 tree compound_stmt = NULL_TREE;
13472
13473 if (FN_TRY_BLOCK_P (t))
13474 stmt = begin_function_try_block (&compound_stmt);
13475 else
13476 stmt = begin_try_block ();
13477
13478 RECUR (TRY_STMTS (t));
13479
13480 if (FN_TRY_BLOCK_P (t))
13481 finish_function_try_block (stmt);
13482 else
13483 finish_try_block (stmt);
13484
13485 RECUR (TRY_HANDLERS (t));
13486 if (FN_TRY_BLOCK_P (t))
13487 finish_function_handler_sequence (stmt, compound_stmt);
13488 else
13489 finish_handler_sequence (stmt);
13490 }
13491 break;
13492
13493 case HANDLER:
13494 {
13495 tree decl = HANDLER_PARMS (t);
13496
13497 if (decl)
13498 {
13499 decl = tsubst (decl, args, complain, in_decl);
13500 /* Prevent instantiate_decl from trying to instantiate
13501 this variable. We've already done all that needs to be
13502 done. */
13503 if (decl != error_mark_node)
13504 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
13505 }
13506 stmt = begin_handler ();
13507 finish_handler_parms (decl, stmt);
13508 RECUR (HANDLER_BODY (t));
13509 finish_handler (stmt);
13510 }
13511 break;
13512
13513 case TAG_DEFN:
13514 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13515 if (CLASS_TYPE_P (tmp))
13516 {
13517 /* Local classes are not independent templates; they are
13518 instantiated along with their containing function. And this
13519 way we don't have to deal with pushing out of one local class
13520 to instantiate a member of another local class. */
13521 tree fn;
13522 /* Closures are handled by the LAMBDA_EXPR. */
13523 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
13524 complete_type (tmp);
13525 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
13526 if (!DECL_ARTIFICIAL (fn))
13527 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
13528 }
13529 break;
13530
13531 case STATIC_ASSERT:
13532 {
13533 tree condition;
13534
13535 ++c_inhibit_evaluation_warnings;
13536 condition =
13537 tsubst_expr (STATIC_ASSERT_CONDITION (t),
13538 args,
13539 complain, in_decl,
13540 /*integral_constant_expression_p=*/true);
13541 --c_inhibit_evaluation_warnings;
13542
13543 finish_static_assert (condition,
13544 STATIC_ASSERT_MESSAGE (t),
13545 STATIC_ASSERT_SOURCE_LOCATION (t),
13546 /*member_p=*/false);
13547 }
13548 break;
13549
13550 case OMP_PARALLEL:
13551 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false,
13552 args, complain, in_decl);
13553 stmt = begin_omp_parallel ();
13554 RECUR (OMP_PARALLEL_BODY (t));
13555 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13556 = OMP_PARALLEL_COMBINED (t);
13557 break;
13558
13559 case OMP_TASK:
13560 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false,
13561 args, complain, in_decl);
13562 stmt = begin_omp_task ();
13563 RECUR (OMP_TASK_BODY (t));
13564 finish_omp_task (tmp, stmt);
13565 break;
13566
13567 case OMP_FOR:
13568 case OMP_SIMD:
13569 case OMP_DISTRIBUTE:
13570 {
13571 tree clauses, body, pre_body;
13572 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
13573 tree incrv = NULL_TREE;
13574 int i;
13575
13576 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
13577 args, complain, in_decl);
13578 if (OMP_FOR_INIT (t) != NULL_TREE)
13579 {
13580 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13581 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13582 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13583 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13584 }
13585
13586 stmt = begin_omp_structured_block ();
13587
13588 pre_body = push_stmt_list ();
13589 RECUR (OMP_FOR_PRE_BODY (t));
13590 pre_body = pop_stmt_list (pre_body);
13591
13592 if (OMP_FOR_INIT (t) != NULL_TREE)
13593 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13594 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13595 &clauses, args, complain, in_decl,
13596 integral_constant_expression_p);
13597
13598 body = push_stmt_list ();
13599 RECUR (OMP_FOR_BODY (t));
13600 body = pop_stmt_list (body);
13601
13602 if (OMP_FOR_INIT (t) != NULL_TREE)
13603 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, initv,
13604 condv, incrv, body, pre_body, clauses);
13605 else
13606 {
13607 t = make_node (TREE_CODE (t));
13608 TREE_TYPE (t) = void_type_node;
13609 OMP_FOR_BODY (t) = body;
13610 OMP_FOR_PRE_BODY (t) = pre_body;
13611 OMP_FOR_CLAUSES (t) = clauses;
13612 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
13613 add_stmt (t);
13614 }
13615
13616 add_stmt (finish_omp_structured_block (stmt));
13617 }
13618 break;
13619
13620 case OMP_SECTIONS:
13621 case OMP_SINGLE:
13622 case OMP_TEAMS:
13623 case OMP_TARGET_DATA:
13624 case OMP_TARGET:
13625 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
13626 args, complain, in_decl);
13627 stmt = push_stmt_list ();
13628 RECUR (OMP_BODY (t));
13629 stmt = pop_stmt_list (stmt);
13630
13631 t = copy_node (t);
13632 OMP_BODY (t) = stmt;
13633 OMP_CLAUSES (t) = tmp;
13634 add_stmt (t);
13635 break;
13636
13637 case OMP_TARGET_UPDATE:
13638 tmp = tsubst_omp_clauses (OMP_TARGET_UPDATE_CLAUSES (t), false,
13639 args, complain, in_decl);
13640 t = copy_node (t);
13641 OMP_CLAUSES (t) = tmp;
13642 add_stmt (t);
13643 break;
13644
13645 case OMP_SECTION:
13646 case OMP_CRITICAL:
13647 case OMP_MASTER:
13648 case OMP_TASKGROUP:
13649 case OMP_ORDERED:
13650 stmt = push_stmt_list ();
13651 RECUR (OMP_BODY (t));
13652 stmt = pop_stmt_list (stmt);
13653
13654 t = copy_node (t);
13655 OMP_BODY (t) = stmt;
13656 add_stmt (t);
13657 break;
13658
13659 case OMP_ATOMIC:
13660 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13661 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13662 {
13663 tree op1 = TREE_OPERAND (t, 1);
13664 tree rhs1 = NULL_TREE;
13665 tree lhs, rhs;
13666 if (TREE_CODE (op1) == COMPOUND_EXPR)
13667 {
13668 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13669 op1 = TREE_OPERAND (op1, 1);
13670 }
13671 lhs = RECUR (TREE_OPERAND (op1, 0));
13672 rhs = RECUR (TREE_OPERAND (op1, 1));
13673 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13674 NULL_TREE, NULL_TREE, rhs1,
13675 OMP_ATOMIC_SEQ_CST (t));
13676 }
13677 else
13678 {
13679 tree op1 = TREE_OPERAND (t, 1);
13680 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13681 tree rhs1 = NULL_TREE;
13682 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13683 enum tree_code opcode = NOP_EXPR;
13684 if (code == OMP_ATOMIC_READ)
13685 {
13686 v = RECUR (TREE_OPERAND (op1, 0));
13687 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13688 }
13689 else if (code == OMP_ATOMIC_CAPTURE_OLD
13690 || code == OMP_ATOMIC_CAPTURE_NEW)
13691 {
13692 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13693 v = RECUR (TREE_OPERAND (op1, 0));
13694 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13695 if (TREE_CODE (op11) == COMPOUND_EXPR)
13696 {
13697 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13698 op11 = TREE_OPERAND (op11, 1);
13699 }
13700 lhs = RECUR (TREE_OPERAND (op11, 0));
13701 rhs = RECUR (TREE_OPERAND (op11, 1));
13702 opcode = TREE_CODE (op11);
13703 if (opcode == MODIFY_EXPR)
13704 opcode = NOP_EXPR;
13705 }
13706 else
13707 {
13708 code = OMP_ATOMIC;
13709 lhs = RECUR (TREE_OPERAND (op1, 0));
13710 rhs = RECUR (TREE_OPERAND (op1, 1));
13711 }
13712 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
13713 OMP_ATOMIC_SEQ_CST (t));
13714 }
13715 break;
13716
13717 case TRANSACTION_EXPR:
13718 {
13719 int flags = 0;
13720 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13721 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13722
13723 if (TRANSACTION_EXPR_IS_STMT (t))
13724 {
13725 tree body = TRANSACTION_EXPR_BODY (t);
13726 tree noex = NULL_TREE;
13727 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13728 {
13729 noex = MUST_NOT_THROW_COND (body);
13730 if (noex == NULL_TREE)
13731 noex = boolean_true_node;
13732 body = TREE_OPERAND (body, 0);
13733 }
13734 stmt = begin_transaction_stmt (input_location, NULL, flags);
13735 RECUR (body);
13736 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13737 }
13738 else
13739 {
13740 stmt = build_transaction_expr (EXPR_LOCATION (t),
13741 RECUR (TRANSACTION_EXPR_BODY (t)),
13742 flags, NULL_TREE);
13743 RETURN (stmt);
13744 }
13745 }
13746 break;
13747
13748 case MUST_NOT_THROW_EXPR:
13749 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13750 RECUR (MUST_NOT_THROW_COND (t))));
13751
13752 case EXPR_PACK_EXPANSION:
13753 error ("invalid use of pack expansion expression");
13754 RETURN (error_mark_node);
13755
13756 case NONTYPE_ARGUMENT_PACK:
13757 error ("use %<...%> to expand argument pack");
13758 RETURN (error_mark_node);
13759
13760 case COMPOUND_EXPR:
13761 tmp = RECUR (TREE_OPERAND (t, 0));
13762 if (tmp == NULL_TREE)
13763 /* If the first operand was a statement, we're done with it. */
13764 RETURN (RECUR (TREE_OPERAND (t, 1)));
13765 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13766 RECUR (TREE_OPERAND (t, 1)),
13767 complain));
13768
13769 default:
13770 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13771
13772 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13773 /*function_p=*/false,
13774 integral_constant_expression_p));
13775 }
13776
13777 RETURN (NULL_TREE);
13778 out:
13779 input_location = loc;
13780 return r;
13781 #undef RECUR
13782 #undef RETURN
13783 }
13784
13785 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
13786 function. For description of the body see comment above
13787 cp_parser_omp_declare_reduction_exprs. */
13788
13789 static void
13790 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13791 {
13792 if (t == NULL_TREE || t == error_mark_node)
13793 return;
13794
13795 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
13796
13797 tree_stmt_iterator tsi;
13798 int i;
13799 tree stmts[7];
13800 memset (stmts, 0, sizeof stmts);
13801 for (i = 0, tsi = tsi_start (t);
13802 i < 7 && !tsi_end_p (tsi);
13803 i++, tsi_next (&tsi))
13804 stmts[i] = tsi_stmt (tsi);
13805 gcc_assert (tsi_end_p (tsi));
13806
13807 if (i >= 3)
13808 {
13809 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
13810 && TREE_CODE (stmts[1]) == DECL_EXPR);
13811 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
13812 args, complain, in_decl);
13813 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
13814 args, complain, in_decl);
13815 DECL_CONTEXT (omp_out) = current_function_decl;
13816 DECL_CONTEXT (omp_in) = current_function_decl;
13817 keep_next_level (true);
13818 tree block = begin_omp_structured_block ();
13819 tsubst_expr (stmts[2], args, complain, in_decl, false);
13820 block = finish_omp_structured_block (block);
13821 block = maybe_cleanup_point_expr_void (block);
13822 add_decl_expr (omp_out);
13823 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
13824 TREE_NO_WARNING (omp_out) = 1;
13825 add_decl_expr (omp_in);
13826 finish_expr_stmt (block);
13827 }
13828 if (i >= 6)
13829 {
13830 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
13831 && TREE_CODE (stmts[4]) == DECL_EXPR);
13832 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
13833 args, complain, in_decl);
13834 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
13835 args, complain, in_decl);
13836 DECL_CONTEXT (omp_priv) = current_function_decl;
13837 DECL_CONTEXT (omp_orig) = current_function_decl;
13838 keep_next_level (true);
13839 tree block = begin_omp_structured_block ();
13840 tsubst_expr (stmts[5], args, complain, in_decl, false);
13841 block = finish_omp_structured_block (block);
13842 block = maybe_cleanup_point_expr_void (block);
13843 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
13844 add_decl_expr (omp_priv);
13845 add_decl_expr (omp_orig);
13846 finish_expr_stmt (block);
13847 if (i == 7)
13848 add_decl_expr (omp_orig);
13849 }
13850 }
13851
13852 /* T is a postfix-expression that is not being used in a function
13853 call. Return the substituted version of T. */
13854
13855 static tree
13856 tsubst_non_call_postfix_expression (tree t, tree args,
13857 tsubst_flags_t complain,
13858 tree in_decl)
13859 {
13860 if (TREE_CODE (t) == SCOPE_REF)
13861 t = tsubst_qualified_id (t, args, complain, in_decl,
13862 /*done=*/false, /*address_p=*/false);
13863 else
13864 t = tsubst_copy_and_build (t, args, complain, in_decl,
13865 /*function_p=*/false,
13866 /*integral_constant_expression_p=*/false);
13867
13868 return t;
13869 }
13870
13871 /* Like tsubst but deals with expressions and performs semantic
13872 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13873
13874 tree
13875 tsubst_copy_and_build (tree t,
13876 tree args,
13877 tsubst_flags_t complain,
13878 tree in_decl,
13879 bool function_p,
13880 bool integral_constant_expression_p)
13881 {
13882 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13883 #define RECUR(NODE) \
13884 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13885 /*function_p=*/false, \
13886 integral_constant_expression_p)
13887
13888 tree retval, op1;
13889 location_t loc;
13890
13891 if (t == NULL_TREE || t == error_mark_node)
13892 return t;
13893
13894 loc = input_location;
13895 if (EXPR_HAS_LOCATION (t))
13896 input_location = EXPR_LOCATION (t);
13897
13898 /* N3276 decltype magic only applies to calls at the top level or on the
13899 right side of a comma. */
13900 tsubst_flags_t decltype_flag = (complain & tf_decltype);
13901 complain &= ~tf_decltype;
13902
13903 switch (TREE_CODE (t))
13904 {
13905 case USING_DECL:
13906 t = DECL_NAME (t);
13907 /* Fall through. */
13908 case IDENTIFIER_NODE:
13909 {
13910 tree decl;
13911 cp_id_kind idk;
13912 bool non_integral_constant_expression_p;
13913 const char *error_msg;
13914
13915 if (IDENTIFIER_TYPENAME_P (t))
13916 {
13917 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13918 t = mangle_conv_op_name_for_type (new_type);
13919 }
13920
13921 /* Look up the name. */
13922 decl = lookup_name (t);
13923
13924 /* By convention, expressions use ERROR_MARK_NODE to indicate
13925 failure, not NULL_TREE. */
13926 if (decl == NULL_TREE)
13927 decl = error_mark_node;
13928
13929 decl = finish_id_expression (t, decl, NULL_TREE,
13930 &idk,
13931 integral_constant_expression_p,
13932 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
13933 &non_integral_constant_expression_p,
13934 /*template_p=*/false,
13935 /*done=*/true,
13936 /*address_p=*/false,
13937 /*template_arg_p=*/false,
13938 &error_msg,
13939 input_location);
13940 if (error_msg)
13941 error (error_msg);
13942 if (!function_p && identifier_p (decl))
13943 {
13944 if (complain & tf_error)
13945 unqualified_name_lookup_error (decl);
13946 decl = error_mark_node;
13947 }
13948 RETURN (decl);
13949 }
13950
13951 case TEMPLATE_ID_EXPR:
13952 {
13953 tree object;
13954 tree templ = RECUR (TREE_OPERAND (t, 0));
13955 tree targs = TREE_OPERAND (t, 1);
13956
13957 if (targs)
13958 targs = tsubst_template_args (targs, args, complain, in_decl);
13959
13960 if (TREE_CODE (templ) == COMPONENT_REF)
13961 {
13962 object = TREE_OPERAND (templ, 0);
13963 templ = TREE_OPERAND (templ, 1);
13964 }
13965 else
13966 object = NULL_TREE;
13967 templ = lookup_template_function (templ, targs);
13968
13969 if (object)
13970 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13971 object, templ, NULL_TREE));
13972 else
13973 RETURN (baselink_for_fns (templ));
13974 }
13975
13976 case INDIRECT_REF:
13977 {
13978 tree r = RECUR (TREE_OPERAND (t, 0));
13979
13980 if (REFERENCE_REF_P (t))
13981 {
13982 /* A type conversion to reference type will be enclosed in
13983 such an indirect ref, but the substitution of the cast
13984 will have also added such an indirect ref. */
13985 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13986 r = convert_from_reference (r);
13987 }
13988 else
13989 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
13990 complain|decltype_flag);
13991 RETURN (r);
13992 }
13993
13994 case NOP_EXPR:
13995 RETURN (build_nop
13996 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13997 RECUR (TREE_OPERAND (t, 0))));
13998
13999 case IMPLICIT_CONV_EXPR:
14000 {
14001 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14002 tree expr = RECUR (TREE_OPERAND (t, 0));
14003 int flags = LOOKUP_IMPLICIT;
14004 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
14005 flags = LOOKUP_NORMAL;
14006 RETURN (perform_implicit_conversion_flags (type, expr, complain,
14007 flags));
14008 }
14009
14010 case CONVERT_EXPR:
14011 RETURN (build1
14012 (CONVERT_EXPR,
14013 tsubst (TREE_TYPE (t), args, complain, in_decl),
14014 RECUR (TREE_OPERAND (t, 0))));
14015
14016 case CAST_EXPR:
14017 case REINTERPRET_CAST_EXPR:
14018 case CONST_CAST_EXPR:
14019 case DYNAMIC_CAST_EXPR:
14020 case STATIC_CAST_EXPR:
14021 {
14022 tree type;
14023 tree op, r = NULL_TREE;
14024
14025 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14026 if (integral_constant_expression_p
14027 && !cast_valid_in_integral_constant_expression_p (type))
14028 {
14029 if (complain & tf_error)
14030 error ("a cast to a type other than an integral or "
14031 "enumeration type cannot appear in a constant-expression");
14032 RETURN (error_mark_node);
14033 }
14034
14035 op = RECUR (TREE_OPERAND (t, 0));
14036
14037 ++c_inhibit_evaluation_warnings;
14038 switch (TREE_CODE (t))
14039 {
14040 case CAST_EXPR:
14041 r = build_functional_cast (type, op, complain);
14042 break;
14043 case REINTERPRET_CAST_EXPR:
14044 r = build_reinterpret_cast (type, op, complain);
14045 break;
14046 case CONST_CAST_EXPR:
14047 r = build_const_cast (type, op, complain);
14048 break;
14049 case DYNAMIC_CAST_EXPR:
14050 r = build_dynamic_cast (type, op, complain);
14051 break;
14052 case STATIC_CAST_EXPR:
14053 r = build_static_cast (type, op, complain);
14054 break;
14055 default:
14056 gcc_unreachable ();
14057 }
14058 --c_inhibit_evaluation_warnings;
14059
14060 RETURN (r);
14061 }
14062
14063 case POSTDECREMENT_EXPR:
14064 case POSTINCREMENT_EXPR:
14065 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14066 args, complain, in_decl);
14067 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
14068 complain|decltype_flag));
14069
14070 case PREDECREMENT_EXPR:
14071 case PREINCREMENT_EXPR:
14072 case NEGATE_EXPR:
14073 case BIT_NOT_EXPR:
14074 case ABS_EXPR:
14075 case TRUTH_NOT_EXPR:
14076 case UNARY_PLUS_EXPR: /* Unary + */
14077 case REALPART_EXPR:
14078 case IMAGPART_EXPR:
14079 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
14080 RECUR (TREE_OPERAND (t, 0)),
14081 complain|decltype_flag));
14082
14083 case FIX_TRUNC_EXPR:
14084 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
14085 0, complain));
14086
14087 case ADDR_EXPR:
14088 op1 = TREE_OPERAND (t, 0);
14089 if (TREE_CODE (op1) == LABEL_DECL)
14090 RETURN (finish_label_address_expr (DECL_NAME (op1),
14091 EXPR_LOCATION (op1)));
14092 if (TREE_CODE (op1) == SCOPE_REF)
14093 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
14094 /*done=*/true, /*address_p=*/true);
14095 else
14096 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
14097 in_decl);
14098 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
14099 complain|decltype_flag));
14100
14101 case PLUS_EXPR:
14102 case MINUS_EXPR:
14103 case MULT_EXPR:
14104 case TRUNC_DIV_EXPR:
14105 case CEIL_DIV_EXPR:
14106 case FLOOR_DIV_EXPR:
14107 case ROUND_DIV_EXPR:
14108 case EXACT_DIV_EXPR:
14109 case BIT_AND_EXPR:
14110 case BIT_IOR_EXPR:
14111 case BIT_XOR_EXPR:
14112 case TRUNC_MOD_EXPR:
14113 case FLOOR_MOD_EXPR:
14114 case TRUTH_ANDIF_EXPR:
14115 case TRUTH_ORIF_EXPR:
14116 case TRUTH_AND_EXPR:
14117 case TRUTH_OR_EXPR:
14118 case RSHIFT_EXPR:
14119 case LSHIFT_EXPR:
14120 case RROTATE_EXPR:
14121 case LROTATE_EXPR:
14122 case EQ_EXPR:
14123 case NE_EXPR:
14124 case MAX_EXPR:
14125 case MIN_EXPR:
14126 case LE_EXPR:
14127 case GE_EXPR:
14128 case LT_EXPR:
14129 case GT_EXPR:
14130 case MEMBER_REF:
14131 case DOTSTAR_EXPR:
14132 {
14133 tree r;
14134
14135 ++c_inhibit_evaluation_warnings;
14136
14137 r = build_x_binary_op
14138 (input_location, TREE_CODE (t),
14139 RECUR (TREE_OPERAND (t, 0)),
14140 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
14141 ? ERROR_MARK
14142 : TREE_CODE (TREE_OPERAND (t, 0))),
14143 RECUR (TREE_OPERAND (t, 1)),
14144 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
14145 ? ERROR_MARK
14146 : TREE_CODE (TREE_OPERAND (t, 1))),
14147 /*overload=*/NULL,
14148 complain|decltype_flag);
14149 if (EXPR_P (r) && TREE_NO_WARNING (t))
14150 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14151
14152 --c_inhibit_evaluation_warnings;
14153
14154 RETURN (r);
14155 }
14156
14157 case SCOPE_REF:
14158 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
14159 /*address_p=*/false));
14160 case ARRAY_REF:
14161 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14162 args, complain, in_decl);
14163 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
14164 RECUR (TREE_OPERAND (t, 1)),
14165 complain|decltype_flag));
14166
14167 case ARRAY_NOTATION_REF:
14168 {
14169 tree start_index, length, stride;
14170 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
14171 args, complain, in_decl);
14172 start_index = RECUR (ARRAY_NOTATION_START (t));
14173 length = RECUR (ARRAY_NOTATION_LENGTH (t));
14174 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
14175 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
14176 length, stride, TREE_TYPE (op1)));
14177 }
14178 case SIZEOF_EXPR:
14179 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
14180 RETURN (tsubst_copy (t, args, complain, in_decl));
14181 /* Fall through */
14182
14183 case ALIGNOF_EXPR:
14184 {
14185 tree r;
14186
14187 op1 = TREE_OPERAND (t, 0);
14188 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
14189 op1 = TREE_TYPE (op1);
14190 if (!args)
14191 {
14192 /* When there are no ARGS, we are trying to evaluate a
14193 non-dependent expression from the parser. Trying to do
14194 the substitutions may not work. */
14195 if (!TYPE_P (op1))
14196 op1 = TREE_TYPE (op1);
14197 }
14198 else
14199 {
14200 ++cp_unevaluated_operand;
14201 ++c_inhibit_evaluation_warnings;
14202 if (TYPE_P (op1))
14203 op1 = tsubst (op1, args, complain, in_decl);
14204 else
14205 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14206 /*function_p=*/false,
14207 /*integral_constant_expression_p=*/
14208 false);
14209 --cp_unevaluated_operand;
14210 --c_inhibit_evaluation_warnings;
14211 }
14212 if (TYPE_P (op1))
14213 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
14214 complain & tf_error);
14215 else
14216 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
14217 complain & tf_error);
14218 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
14219 {
14220 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
14221 {
14222 if (!processing_template_decl && TYPE_P (op1))
14223 {
14224 r = build_min (SIZEOF_EXPR, size_type_node,
14225 build1 (NOP_EXPR, op1, error_mark_node));
14226 SIZEOF_EXPR_TYPE_P (r) = 1;
14227 }
14228 else
14229 r = build_min (SIZEOF_EXPR, size_type_node, op1);
14230 TREE_SIDE_EFFECTS (r) = 0;
14231 TREE_READONLY (r) = 1;
14232 }
14233 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
14234 }
14235 RETURN (r);
14236 }
14237
14238 case AT_ENCODE_EXPR:
14239 {
14240 op1 = TREE_OPERAND (t, 0);
14241 ++cp_unevaluated_operand;
14242 ++c_inhibit_evaluation_warnings;
14243 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14244 /*function_p=*/false,
14245 /*integral_constant_expression_p=*/false);
14246 --cp_unevaluated_operand;
14247 --c_inhibit_evaluation_warnings;
14248 RETURN (objc_build_encode_expr (op1));
14249 }
14250
14251 case NOEXCEPT_EXPR:
14252 op1 = TREE_OPERAND (t, 0);
14253 ++cp_unevaluated_operand;
14254 ++c_inhibit_evaluation_warnings;
14255 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
14256 /*function_p=*/false,
14257 /*integral_constant_expression_p=*/false);
14258 --cp_unevaluated_operand;
14259 --c_inhibit_evaluation_warnings;
14260 RETURN (finish_noexcept_expr (op1, complain));
14261
14262 case MODOP_EXPR:
14263 {
14264 tree r;
14265
14266 ++c_inhibit_evaluation_warnings;
14267
14268 r = build_x_modify_expr
14269 (EXPR_LOCATION (t),
14270 RECUR (TREE_OPERAND (t, 0)),
14271 TREE_CODE (TREE_OPERAND (t, 1)),
14272 RECUR (TREE_OPERAND (t, 2)),
14273 complain|decltype_flag);
14274 /* TREE_NO_WARNING must be set if either the expression was
14275 parenthesized or it uses an operator such as >>= rather
14276 than plain assignment. In the former case, it was already
14277 set and must be copied. In the latter case,
14278 build_x_modify_expr sets it and it must not be reset
14279 here. */
14280 if (TREE_NO_WARNING (t))
14281 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14282
14283 --c_inhibit_evaluation_warnings;
14284
14285 RETURN (r);
14286 }
14287
14288 case ARROW_EXPR:
14289 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14290 args, complain, in_decl);
14291 /* Remember that there was a reference to this entity. */
14292 if (DECL_P (op1))
14293 mark_used (op1);
14294 RETURN (build_x_arrow (input_location, op1, complain));
14295
14296 case NEW_EXPR:
14297 {
14298 tree placement = RECUR (TREE_OPERAND (t, 0));
14299 tree init = RECUR (TREE_OPERAND (t, 3));
14300 vec<tree, va_gc> *placement_vec;
14301 vec<tree, va_gc> *init_vec;
14302 tree ret;
14303
14304 if (placement == NULL_TREE)
14305 placement_vec = NULL;
14306 else
14307 {
14308 placement_vec = make_tree_vector ();
14309 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
14310 vec_safe_push (placement_vec, TREE_VALUE (placement));
14311 }
14312
14313 /* If there was an initializer in the original tree, but it
14314 instantiated to an empty list, then we should pass a
14315 non-NULL empty vector to tell build_new that it was an
14316 empty initializer() rather than no initializer. This can
14317 only happen when the initializer is a pack expansion whose
14318 parameter packs are of length zero. */
14319 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
14320 init_vec = NULL;
14321 else
14322 {
14323 init_vec = make_tree_vector ();
14324 if (init == void_zero_node)
14325 gcc_assert (init_vec != NULL);
14326 else
14327 {
14328 for (; init != NULL_TREE; init = TREE_CHAIN (init))
14329 vec_safe_push (init_vec, TREE_VALUE (init));
14330 }
14331 }
14332
14333 ret = build_new (&placement_vec,
14334 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
14335 RECUR (TREE_OPERAND (t, 2)),
14336 &init_vec,
14337 NEW_EXPR_USE_GLOBAL (t),
14338 complain);
14339
14340 if (placement_vec != NULL)
14341 release_tree_vector (placement_vec);
14342 if (init_vec != NULL)
14343 release_tree_vector (init_vec);
14344
14345 RETURN (ret);
14346 }
14347
14348 case DELETE_EXPR:
14349 RETURN (delete_sanity
14350 (RECUR (TREE_OPERAND (t, 0)),
14351 RECUR (TREE_OPERAND (t, 1)),
14352 DELETE_EXPR_USE_VEC (t),
14353 DELETE_EXPR_USE_GLOBAL (t),
14354 complain));
14355
14356 case COMPOUND_EXPR:
14357 {
14358 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
14359 complain & ~tf_decltype, in_decl,
14360 /*function_p=*/false,
14361 integral_constant_expression_p);
14362 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
14363 op0,
14364 RECUR (TREE_OPERAND (t, 1)),
14365 complain|decltype_flag));
14366 }
14367
14368 case CALL_EXPR:
14369 {
14370 tree function;
14371 vec<tree, va_gc> *call_args;
14372 unsigned int nargs, i;
14373 bool qualified_p;
14374 bool koenig_p;
14375 tree ret;
14376
14377 function = CALL_EXPR_FN (t);
14378 /* When we parsed the expression, we determined whether or
14379 not Koenig lookup should be performed. */
14380 koenig_p = KOENIG_LOOKUP_P (t);
14381 if (TREE_CODE (function) == SCOPE_REF)
14382 {
14383 qualified_p = true;
14384 function = tsubst_qualified_id (function, args, complain, in_decl,
14385 /*done=*/false,
14386 /*address_p=*/false);
14387 }
14388 else if (koenig_p && identifier_p (function))
14389 {
14390 /* Do nothing; calling tsubst_copy_and_build on an identifier
14391 would incorrectly perform unqualified lookup again.
14392
14393 Note that we can also have an IDENTIFIER_NODE if the earlier
14394 unqualified lookup found a member function; in that case
14395 koenig_p will be false and we do want to do the lookup
14396 again to find the instantiated member function.
14397
14398 FIXME but doing that causes c++/15272, so we need to stop
14399 using IDENTIFIER_NODE in that situation. */
14400 qualified_p = false;
14401 }
14402 else
14403 {
14404 if (TREE_CODE (function) == COMPONENT_REF)
14405 {
14406 tree op = TREE_OPERAND (function, 1);
14407
14408 qualified_p = (TREE_CODE (op) == SCOPE_REF
14409 || (BASELINK_P (op)
14410 && BASELINK_QUALIFIED_P (op)));
14411 }
14412 else
14413 qualified_p = false;
14414
14415 if (TREE_CODE (function) == ADDR_EXPR
14416 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
14417 /* Avoid error about taking the address of a constructor. */
14418 function = TREE_OPERAND (function, 0);
14419
14420 function = tsubst_copy_and_build (function, args, complain,
14421 in_decl,
14422 !qualified_p,
14423 integral_constant_expression_p);
14424
14425 if (BASELINK_P (function))
14426 qualified_p = true;
14427 }
14428
14429 nargs = call_expr_nargs (t);
14430 call_args = make_tree_vector ();
14431 for (i = 0; i < nargs; ++i)
14432 {
14433 tree arg = CALL_EXPR_ARG (t, i);
14434
14435 if (!PACK_EXPANSION_P (arg))
14436 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
14437 else
14438 {
14439 /* Expand the pack expansion and push each entry onto
14440 CALL_ARGS. */
14441 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
14442 if (TREE_CODE (arg) == TREE_VEC)
14443 {
14444 unsigned int len, j;
14445
14446 len = TREE_VEC_LENGTH (arg);
14447 for (j = 0; j < len; ++j)
14448 {
14449 tree value = TREE_VEC_ELT (arg, j);
14450 if (value != NULL_TREE)
14451 value = convert_from_reference (value);
14452 vec_safe_push (call_args, value);
14453 }
14454 }
14455 else
14456 {
14457 /* A partial substitution. Add one entry. */
14458 vec_safe_push (call_args, arg);
14459 }
14460 }
14461 }
14462
14463 /* We do not perform argument-dependent lookup if normal
14464 lookup finds a non-function, in accordance with the
14465 expected resolution of DR 218. */
14466 if (koenig_p
14467 && ((is_overloaded_fn (function)
14468 /* If lookup found a member function, the Koenig lookup is
14469 not appropriate, even if an unqualified-name was used
14470 to denote the function. */
14471 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
14472 || identifier_p (function))
14473 /* Only do this when substitution turns a dependent call
14474 into a non-dependent call. */
14475 && type_dependent_expression_p_push (t)
14476 && !any_type_dependent_arguments_p (call_args))
14477 function = perform_koenig_lookup (function, call_args, false,
14478 tf_none);
14479
14480 if (identifier_p (function)
14481 && !any_type_dependent_arguments_p (call_args))
14482 {
14483 if (koenig_p && (complain & tf_warning_or_error))
14484 {
14485 /* For backwards compatibility and good diagnostics, try
14486 the unqualified lookup again if we aren't in SFINAE
14487 context. */
14488 tree unq = (tsubst_copy_and_build
14489 (function, args, complain, in_decl, true,
14490 integral_constant_expression_p));
14491 if (unq == error_mark_node)
14492 RETURN (error_mark_node);
14493
14494 if (unq != function)
14495 {
14496 tree fn = unq;
14497 if (INDIRECT_REF_P (fn))
14498 fn = TREE_OPERAND (fn, 0);
14499 if (TREE_CODE (fn) == COMPONENT_REF)
14500 fn = TREE_OPERAND (fn, 1);
14501 if (is_overloaded_fn (fn))
14502 fn = get_first_fn (fn);
14503 if (permerror (EXPR_LOC_OR_HERE (t),
14504 "%qD was not declared in this scope, "
14505 "and no declarations were found by "
14506 "argument-dependent lookup at the point "
14507 "of instantiation", function))
14508 {
14509 if (!DECL_P (fn))
14510 /* Can't say anything more. */;
14511 else if (DECL_CLASS_SCOPE_P (fn))
14512 {
14513 inform (EXPR_LOC_OR_HERE (t),
14514 "declarations in dependent base %qT are "
14515 "not found by unqualified lookup",
14516 DECL_CLASS_CONTEXT (fn));
14517 if (current_class_ptr)
14518 inform (EXPR_LOC_OR_HERE (t),
14519 "use %<this->%D%> instead", function);
14520 else
14521 inform (EXPR_LOC_OR_HERE (t),
14522 "use %<%T::%D%> instead",
14523 current_class_name, function);
14524 }
14525 else
14526 inform (0, "%q+D declared here, later in the "
14527 "translation unit", fn);
14528 }
14529 function = unq;
14530 }
14531 }
14532 if (identifier_p (function))
14533 {
14534 if (complain & tf_error)
14535 unqualified_name_lookup_error (function);
14536 release_tree_vector (call_args);
14537 RETURN (error_mark_node);
14538 }
14539 }
14540
14541 /* Remember that there was a reference to this entity. */
14542 if (DECL_P (function))
14543 mark_used (function);
14544
14545 /* Put back tf_decltype for the actual call. */
14546 complain |= decltype_flag;
14547
14548 if (TREE_CODE (function) == OFFSET_REF)
14549 ret = build_offset_ref_call_from_tree (function, &call_args,
14550 complain);
14551 else if (TREE_CODE (function) == COMPONENT_REF)
14552 {
14553 tree instance = TREE_OPERAND (function, 0);
14554 tree fn = TREE_OPERAND (function, 1);
14555
14556 if (processing_template_decl
14557 && (type_dependent_expression_p (instance)
14558 || (!BASELINK_P (fn)
14559 && TREE_CODE (fn) != FIELD_DECL)
14560 || type_dependent_expression_p (fn)
14561 || any_type_dependent_arguments_p (call_args)))
14562 ret = build_nt_call_vec (function, call_args);
14563 else if (!BASELINK_P (fn))
14564 ret = finish_call_expr (function, &call_args,
14565 /*disallow_virtual=*/false,
14566 /*koenig_p=*/false,
14567 complain);
14568 else
14569 ret = (build_new_method_call
14570 (instance, fn,
14571 &call_args, NULL_TREE,
14572 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
14573 /*fn_p=*/NULL,
14574 complain));
14575 }
14576 else
14577 ret = finish_call_expr (function, &call_args,
14578 /*disallow_virtual=*/qualified_p,
14579 koenig_p,
14580 complain);
14581
14582 release_tree_vector (call_args);
14583
14584 RETURN (ret);
14585 }
14586
14587 case COND_EXPR:
14588 {
14589 tree cond = RECUR (TREE_OPERAND (t, 0));
14590 tree exp1, exp2;
14591
14592 if (TREE_CODE (cond) == INTEGER_CST)
14593 {
14594 if (integer_zerop (cond))
14595 {
14596 ++c_inhibit_evaluation_warnings;
14597 exp1 = RECUR (TREE_OPERAND (t, 1));
14598 --c_inhibit_evaluation_warnings;
14599 exp2 = RECUR (TREE_OPERAND (t, 2));
14600 }
14601 else
14602 {
14603 exp1 = RECUR (TREE_OPERAND (t, 1));
14604 ++c_inhibit_evaluation_warnings;
14605 exp2 = RECUR (TREE_OPERAND (t, 2));
14606 --c_inhibit_evaluation_warnings;
14607 }
14608 }
14609 else
14610 {
14611 exp1 = RECUR (TREE_OPERAND (t, 1));
14612 exp2 = RECUR (TREE_OPERAND (t, 2));
14613 }
14614
14615 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
14616 cond, exp1, exp2, complain));
14617 }
14618
14619 case PSEUDO_DTOR_EXPR:
14620 RETURN (finish_pseudo_destructor_expr
14621 (RECUR (TREE_OPERAND (t, 0)),
14622 RECUR (TREE_OPERAND (t, 1)),
14623 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl),
14624 input_location));
14625
14626 case TREE_LIST:
14627 {
14628 tree purpose, value, chain;
14629
14630 if (t == void_list_node)
14631 RETURN (t);
14632
14633 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
14634 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
14635 {
14636 /* We have pack expansions, so expand those and
14637 create a new list out of it. */
14638 tree purposevec = NULL_TREE;
14639 tree valuevec = NULL_TREE;
14640 tree chain;
14641 int i, len = -1;
14642
14643 /* Expand the argument expressions. */
14644 if (TREE_PURPOSE (t))
14645 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
14646 complain, in_decl);
14647 if (TREE_VALUE (t))
14648 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
14649 complain, in_decl);
14650
14651 /* Build the rest of the list. */
14652 chain = TREE_CHAIN (t);
14653 if (chain && chain != void_type_node)
14654 chain = RECUR (chain);
14655
14656 /* Determine the number of arguments. */
14657 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
14658 {
14659 len = TREE_VEC_LENGTH (purposevec);
14660 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
14661 }
14662 else if (TREE_CODE (valuevec) == TREE_VEC)
14663 len = TREE_VEC_LENGTH (valuevec);
14664 else
14665 {
14666 /* Since we only performed a partial substitution into
14667 the argument pack, we only RETURN (a single list
14668 node. */
14669 if (purposevec == TREE_PURPOSE (t)
14670 && valuevec == TREE_VALUE (t)
14671 && chain == TREE_CHAIN (t))
14672 RETURN (t);
14673
14674 RETURN (tree_cons (purposevec, valuevec, chain));
14675 }
14676
14677 /* Convert the argument vectors into a TREE_LIST */
14678 i = len;
14679 while (i > 0)
14680 {
14681 /* Grab the Ith values. */
14682 i--;
14683 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
14684 : NULL_TREE;
14685 value
14686 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14687 : NULL_TREE;
14688
14689 /* Build the list (backwards). */
14690 chain = tree_cons (purpose, value, chain);
14691 }
14692
14693 RETURN (chain);
14694 }
14695
14696 purpose = TREE_PURPOSE (t);
14697 if (purpose)
14698 purpose = RECUR (purpose);
14699 value = TREE_VALUE (t);
14700 if (value)
14701 value = RECUR (value);
14702 chain = TREE_CHAIN (t);
14703 if (chain && chain != void_type_node)
14704 chain = RECUR (chain);
14705 if (purpose == TREE_PURPOSE (t)
14706 && value == TREE_VALUE (t)
14707 && chain == TREE_CHAIN (t))
14708 RETURN (t);
14709 RETURN (tree_cons (purpose, value, chain));
14710 }
14711
14712 case COMPONENT_REF:
14713 {
14714 tree object;
14715 tree object_type;
14716 tree member;
14717
14718 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14719 args, complain, in_decl);
14720 /* Remember that there was a reference to this entity. */
14721 if (DECL_P (object))
14722 mark_used (object);
14723 object_type = TREE_TYPE (object);
14724
14725 member = TREE_OPERAND (t, 1);
14726 if (BASELINK_P (member))
14727 member = tsubst_baselink (member,
14728 non_reference (TREE_TYPE (object)),
14729 args, complain, in_decl);
14730 else
14731 member = tsubst_copy (member, args, complain, in_decl);
14732 if (member == error_mark_node)
14733 RETURN (error_mark_node);
14734
14735 if (type_dependent_expression_p (object))
14736 /* We can't do much here. */;
14737 else if (!CLASS_TYPE_P (object_type))
14738 {
14739 if (scalarish_type_p (object_type))
14740 {
14741 tree s = NULL_TREE;
14742 tree dtor = member;
14743
14744 if (TREE_CODE (dtor) == SCOPE_REF)
14745 {
14746 s = TREE_OPERAND (dtor, 0);
14747 dtor = TREE_OPERAND (dtor, 1);
14748 }
14749 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14750 {
14751 dtor = TREE_OPERAND (dtor, 0);
14752 if (TYPE_P (dtor))
14753 RETURN (finish_pseudo_destructor_expr
14754 (object, s, dtor, input_location));
14755 }
14756 }
14757 }
14758 else if (TREE_CODE (member) == SCOPE_REF
14759 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14760 {
14761 /* Lookup the template functions now that we know what the
14762 scope is. */
14763 tree scope = TREE_OPERAND (member, 0);
14764 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14765 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14766 member = lookup_qualified_name (scope, tmpl,
14767 /*is_type_p=*/false,
14768 /*complain=*/false);
14769 if (BASELINK_P (member))
14770 {
14771 BASELINK_FUNCTIONS (member)
14772 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14773 args);
14774 member = (adjust_result_of_qualified_name_lookup
14775 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14776 object_type));
14777 }
14778 else
14779 {
14780 qualified_name_lookup_error (scope, tmpl, member,
14781 input_location);
14782 RETURN (error_mark_node);
14783 }
14784 }
14785 else if (TREE_CODE (member) == SCOPE_REF
14786 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14787 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14788 {
14789 if (complain & tf_error)
14790 {
14791 if (TYPE_P (TREE_OPERAND (member, 0)))
14792 error ("%qT is not a class or namespace",
14793 TREE_OPERAND (member, 0));
14794 else
14795 error ("%qD is not a class or namespace",
14796 TREE_OPERAND (member, 0));
14797 }
14798 RETURN (error_mark_node);
14799 }
14800 else if (TREE_CODE (member) == FIELD_DECL)
14801 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14802
14803 RETURN (finish_class_member_access_expr (object, member,
14804 /*template_p=*/false,
14805 complain));
14806 }
14807
14808 case THROW_EXPR:
14809 RETURN (build_throw
14810 (RECUR (TREE_OPERAND (t, 0))));
14811
14812 case CONSTRUCTOR:
14813 {
14814 vec<constructor_elt, va_gc> *n;
14815 constructor_elt *ce;
14816 unsigned HOST_WIDE_INT idx;
14817 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14818 bool process_index_p;
14819 int newlen;
14820 bool need_copy_p = false;
14821 tree r;
14822
14823 if (type == error_mark_node)
14824 RETURN (error_mark_node);
14825
14826 /* digest_init will do the wrong thing if we let it. */
14827 if (type && TYPE_PTRMEMFUNC_P (type))
14828 RETURN (t);
14829
14830 /* We do not want to process the index of aggregate
14831 initializers as they are identifier nodes which will be
14832 looked up by digest_init. */
14833 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14834
14835 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14836 newlen = vec_safe_length (n);
14837 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14838 {
14839 if (ce->index && process_index_p
14840 /* An identifier index is looked up in the type
14841 being initialized, not the current scope. */
14842 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
14843 ce->index = RECUR (ce->index);
14844
14845 if (PACK_EXPANSION_P (ce->value))
14846 {
14847 /* Substitute into the pack expansion. */
14848 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14849 in_decl);
14850
14851 if (ce->value == error_mark_node
14852 || PACK_EXPANSION_P (ce->value))
14853 ;
14854 else if (TREE_VEC_LENGTH (ce->value) == 1)
14855 /* Just move the argument into place. */
14856 ce->value = TREE_VEC_ELT (ce->value, 0);
14857 else
14858 {
14859 /* Update the length of the final CONSTRUCTOR
14860 arguments vector, and note that we will need to
14861 copy.*/
14862 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14863 need_copy_p = true;
14864 }
14865 }
14866 else
14867 ce->value = RECUR (ce->value);
14868 }
14869
14870 if (need_copy_p)
14871 {
14872 vec<constructor_elt, va_gc> *old_n = n;
14873
14874 vec_alloc (n, newlen);
14875 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14876 {
14877 if (TREE_CODE (ce->value) == TREE_VEC)
14878 {
14879 int i, len = TREE_VEC_LENGTH (ce->value);
14880 for (i = 0; i < len; ++i)
14881 CONSTRUCTOR_APPEND_ELT (n, 0,
14882 TREE_VEC_ELT (ce->value, i));
14883 }
14884 else
14885 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14886 }
14887 }
14888
14889 r = build_constructor (init_list_type_node, n);
14890 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14891
14892 if (TREE_HAS_CONSTRUCTOR (t))
14893 RETURN (finish_compound_literal (type, r, complain));
14894
14895 TREE_TYPE (r) = type;
14896 RETURN (r);
14897 }
14898
14899 case TYPEID_EXPR:
14900 {
14901 tree operand_0 = TREE_OPERAND (t, 0);
14902 if (TYPE_P (operand_0))
14903 {
14904 operand_0 = tsubst (operand_0, args, complain, in_decl);
14905 RETURN (get_typeid (operand_0, complain));
14906 }
14907 else
14908 {
14909 operand_0 = RECUR (operand_0);
14910 RETURN (build_typeid (operand_0, complain));
14911 }
14912 }
14913
14914 case VAR_DECL:
14915 if (!args)
14916 RETURN (t);
14917 else if (DECL_PACK_P (t))
14918 {
14919 /* We don't build decls for an instantiation of a
14920 variadic capture proxy, we instantiate the elements
14921 when needed. */
14922 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
14923 return RECUR (DECL_VALUE_EXPR (t));
14924 }
14925 /* Fall through */
14926
14927 case PARM_DECL:
14928 {
14929 tree r = tsubst_copy (t, args, complain, in_decl);
14930
14931 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14932 /* If the original type was a reference, we'll be wrapped in
14933 the appropriate INDIRECT_REF. */
14934 r = convert_from_reference (r);
14935 RETURN (r);
14936 }
14937
14938 case VA_ARG_EXPR:
14939 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14940 RECUR (TREE_OPERAND (t, 0)),
14941 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14942
14943 case OFFSETOF_EXPR:
14944 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14945
14946 case TRAIT_EXPR:
14947 {
14948 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14949 complain, in_decl);
14950
14951 tree type2 = TRAIT_EXPR_TYPE2 (t);
14952 if (type2)
14953 type2 = tsubst_copy (type2, args, complain, in_decl);
14954
14955 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14956 }
14957
14958 case STMT_EXPR:
14959 {
14960 tree old_stmt_expr = cur_stmt_expr;
14961 tree stmt_expr = begin_stmt_expr ();
14962
14963 cur_stmt_expr = stmt_expr;
14964 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14965 integral_constant_expression_p);
14966 stmt_expr = finish_stmt_expr (stmt_expr, false);
14967 cur_stmt_expr = old_stmt_expr;
14968
14969 /* If the resulting list of expression statement is empty,
14970 fold it further into void_zero_node. */
14971 if (empty_expr_stmt_p (stmt_expr))
14972 stmt_expr = void_zero_node;
14973
14974 RETURN (stmt_expr);
14975 }
14976
14977 case LAMBDA_EXPR:
14978 {
14979 tree r = build_lambda_expr ();
14980
14981 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14982 LAMBDA_EXPR_CLOSURE (r) = type;
14983 CLASSTYPE_LAMBDA_EXPR (type) = r;
14984
14985 LAMBDA_EXPR_LOCATION (r)
14986 = LAMBDA_EXPR_LOCATION (t);
14987 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14988 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14989 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14990 LAMBDA_EXPR_DISCRIMINATOR (r)
14991 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14992 /* For a function scope, we want to use tsubst so that we don't
14993 complain about referring to an auto function before its return
14994 type has been deduced. Otherwise, we want to use tsubst_copy so
14995 that we look up the existing field/parameter/variable rather
14996 than build a new one. */
14997 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14998 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14999 scope = tsubst (scope, args, complain, in_decl);
15000 else if (scope && TREE_CODE (scope) == PARM_DECL)
15001 {
15002 /* Look up the parameter we want directly, as tsubst_copy
15003 doesn't do what we need. */
15004 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
15005 tree parm = FUNCTION_FIRST_USER_PARM (fn);
15006 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
15007 parm = DECL_CHAIN (parm);
15008 scope = parm;
15009 /* FIXME Work around the parm not having DECL_CONTEXT set. */
15010 if (DECL_CONTEXT (scope) == NULL_TREE)
15011 DECL_CONTEXT (scope) = fn;
15012 }
15013 else
15014 scope = RECUR (scope);
15015 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
15016 LAMBDA_EXPR_RETURN_TYPE (r)
15017 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
15018
15019 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
15020 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
15021
15022 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
15023 determine_visibility (TYPE_NAME (type));
15024 /* Now that we know visibility, instantiate the type so we have a
15025 declaration of the op() for later calls to lambda_function. */
15026 complete_type (type);
15027
15028 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
15029
15030 RETURN (build_lambda_object (r));
15031 }
15032
15033 case TARGET_EXPR:
15034 /* We can get here for a constant initializer of non-dependent type.
15035 FIXME stop folding in cp_parser_initializer_clause. */
15036 {
15037 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
15038 complain);
15039 RETURN (r);
15040 }
15041
15042 case TRANSACTION_EXPR:
15043 RETURN (tsubst_expr(t, args, complain, in_decl,
15044 integral_constant_expression_p));
15045
15046 case PAREN_EXPR:
15047 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
15048
15049 case VEC_PERM_EXPR:
15050 RETURN (build_x_vec_perm_expr (input_location,
15051 RECUR (TREE_OPERAND (t, 0)),
15052 RECUR (TREE_OPERAND (t, 1)),
15053 RECUR (TREE_OPERAND (t, 2)),
15054 complain));
15055
15056 default:
15057 /* Handle Objective-C++ constructs, if appropriate. */
15058 {
15059 tree subst
15060 = objcp_tsubst_copy_and_build (t, args, complain,
15061 in_decl, /*function_p=*/false);
15062 if (subst)
15063 RETURN (subst);
15064 }
15065 RETURN (tsubst_copy (t, args, complain, in_decl));
15066 }
15067
15068 #undef RECUR
15069 #undef RETURN
15070 out:
15071 input_location = loc;
15072 return retval;
15073 }
15074
15075 /* Verify that the instantiated ARGS are valid. For type arguments,
15076 make sure that the type's linkage is ok. For non-type arguments,
15077 make sure they are constants if they are integral or enumerations.
15078 Emit an error under control of COMPLAIN, and return TRUE on error. */
15079
15080 static bool
15081 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
15082 {
15083 if (dependent_template_arg_p (t))
15084 return false;
15085 if (ARGUMENT_PACK_P (t))
15086 {
15087 tree vec = ARGUMENT_PACK_ARGS (t);
15088 int len = TREE_VEC_LENGTH (vec);
15089 bool result = false;
15090 int i;
15091
15092 for (i = 0; i < len; ++i)
15093 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
15094 result = true;
15095 return result;
15096 }
15097 else if (TYPE_P (t))
15098 {
15099 /* [basic.link]: A name with no linkage (notably, the name
15100 of a class or enumeration declared in a local scope)
15101 shall not be used to declare an entity with linkage.
15102 This implies that names with no linkage cannot be used as
15103 template arguments
15104
15105 DR 757 relaxes this restriction for C++0x. */
15106 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
15107 : no_linkage_check (t, /*relaxed_p=*/false));
15108
15109 if (nt)
15110 {
15111 /* DR 488 makes use of a type with no linkage cause
15112 type deduction to fail. */
15113 if (complain & tf_error)
15114 {
15115 if (TYPE_ANONYMOUS_P (nt))
15116 error ("%qT is/uses anonymous type", t);
15117 else
15118 error ("template argument for %qD uses local type %qT",
15119 tmpl, t);
15120 }
15121 return true;
15122 }
15123 /* In order to avoid all sorts of complications, we do not
15124 allow variably-modified types as template arguments. */
15125 else if (variably_modified_type_p (t, NULL_TREE))
15126 {
15127 if (complain & tf_error)
15128 error ("%qT is a variably modified type", t);
15129 return true;
15130 }
15131 }
15132 /* Class template and alias template arguments should be OK. */
15133 else if (DECL_TYPE_TEMPLATE_P (t))
15134 ;
15135 /* A non-type argument of integral or enumerated type must be a
15136 constant. */
15137 else if (TREE_TYPE (t)
15138 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
15139 && !TREE_CONSTANT (t))
15140 {
15141 if (complain & tf_error)
15142 error ("integral expression %qE is not constant", t);
15143 return true;
15144 }
15145 return false;
15146 }
15147
15148 static bool
15149 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
15150 {
15151 int ix, len = DECL_NTPARMS (tmpl);
15152 bool result = false;
15153
15154 for (ix = 0; ix != len; ix++)
15155 {
15156 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
15157 result = true;
15158 }
15159 if (result && (complain & tf_error))
15160 error (" trying to instantiate %qD", tmpl);
15161 return result;
15162 }
15163
15164 /* We're out of SFINAE context now, so generate diagnostics for the access
15165 errors we saw earlier when instantiating D from TMPL and ARGS. */
15166
15167 static void
15168 recheck_decl_substitution (tree d, tree tmpl, tree args)
15169 {
15170 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
15171 tree type = TREE_TYPE (pattern);
15172 location_t loc = input_location;
15173
15174 push_access_scope (d);
15175 push_deferring_access_checks (dk_no_deferred);
15176 input_location = DECL_SOURCE_LOCATION (pattern);
15177 tsubst (type, args, tf_warning_or_error, d);
15178 input_location = loc;
15179 pop_deferring_access_checks ();
15180 pop_access_scope (d);
15181 }
15182
15183 /* Instantiate the indicated variable, function, or alias template TMPL with
15184 the template arguments in TARG_PTR. */
15185
15186 static tree
15187 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
15188 {
15189 tree targ_ptr = orig_args;
15190 tree fndecl;
15191 tree gen_tmpl;
15192 tree spec;
15193 bool access_ok = true;
15194
15195 if (tmpl == error_mark_node)
15196 return error_mark_node;
15197
15198 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
15199
15200 /* If this function is a clone, handle it specially. */
15201 if (DECL_CLONED_FUNCTION_P (tmpl))
15202 {
15203 tree spec;
15204 tree clone;
15205
15206 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
15207 DECL_CLONED_FUNCTION. */
15208 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
15209 targ_ptr, complain);
15210 if (spec == error_mark_node)
15211 return error_mark_node;
15212
15213 /* Look for the clone. */
15214 FOR_EACH_CLONE (clone, spec)
15215 if (DECL_NAME (clone) == DECL_NAME (tmpl))
15216 return clone;
15217 /* We should always have found the clone by now. */
15218 gcc_unreachable ();
15219 return NULL_TREE;
15220 }
15221
15222 /* Check to see if we already have this specialization. */
15223 gen_tmpl = most_general_template (tmpl);
15224 if (tmpl != gen_tmpl)
15225 /* The TMPL is a partial instantiation. To get a full set of
15226 arguments we must add the arguments used to perform the
15227 partial instantiation. */
15228 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
15229 targ_ptr);
15230
15231 /* It would be nice to avoid hashing here and then again in tsubst_decl,
15232 but it doesn't seem to be on the hot path. */
15233 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
15234
15235 gcc_assert (tmpl == gen_tmpl
15236 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
15237 == spec)
15238 || fndecl == NULL_TREE);
15239
15240 if (spec != NULL_TREE)
15241 {
15242 if (FNDECL_HAS_ACCESS_ERRORS (spec))
15243 {
15244 if (complain & tf_error)
15245 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
15246 return error_mark_node;
15247 }
15248 return spec;
15249 }
15250
15251 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
15252 complain))
15253 return error_mark_node;
15254
15255 /* We are building a FUNCTION_DECL, during which the access of its
15256 parameters and return types have to be checked. However this
15257 FUNCTION_DECL which is the desired context for access checking
15258 is not built yet. We solve this chicken-and-egg problem by
15259 deferring all checks until we have the FUNCTION_DECL. */
15260 push_deferring_access_checks (dk_deferred);
15261
15262 /* Instantiation of the function happens in the context of the function
15263 template, not the context of the overload resolution we're doing. */
15264 push_to_top_level ();
15265 /* If there are dependent arguments, e.g. because we're doing partial
15266 ordering, make sure processing_template_decl stays set. */
15267 if (uses_template_parms (targ_ptr))
15268 ++processing_template_decl;
15269 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15270 {
15271 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
15272 complain, gen_tmpl);
15273 push_nested_class (ctx);
15274 }
15275 /* Substitute template parameters to obtain the specialization. */
15276 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
15277 targ_ptr, complain, gen_tmpl);
15278 if (DECL_CLASS_SCOPE_P (gen_tmpl))
15279 pop_nested_class ();
15280 pop_from_top_level ();
15281
15282 if (fndecl == error_mark_node)
15283 {
15284 pop_deferring_access_checks ();
15285 return error_mark_node;
15286 }
15287
15288 /* The DECL_TI_TEMPLATE should always be the immediate parent
15289 template, not the most general template. */
15290 DECL_TI_TEMPLATE (fndecl) = tmpl;
15291
15292 /* Now we know the specialization, compute access previously
15293 deferred. */
15294 push_access_scope (fndecl);
15295 if (!perform_deferred_access_checks (complain))
15296 access_ok = false;
15297 pop_access_scope (fndecl);
15298 pop_deferring_access_checks ();
15299
15300 /* If we've just instantiated the main entry point for a function,
15301 instantiate all the alternate entry points as well. We do this
15302 by cloning the instantiation of the main entry point, not by
15303 instantiating the template clones. */
15304 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
15305 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
15306
15307 if (!access_ok)
15308 {
15309 if (!(complain & tf_error))
15310 {
15311 /* Remember to reinstantiate when we're out of SFINAE so the user
15312 can see the errors. */
15313 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
15314 }
15315 return error_mark_node;
15316 }
15317 return fndecl;
15318 }
15319
15320 /* Wrapper for instantiate_template_1. */
15321
15322 tree
15323 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
15324 {
15325 tree ret;
15326 timevar_push (TV_TEMPLATE_INST);
15327 ret = instantiate_template_1 (tmpl, orig_args, complain);
15328 timevar_pop (TV_TEMPLATE_INST);
15329 return ret;
15330 }
15331
15332 /* Instantiate the alias template TMPL with ARGS. Also push a template
15333 instantiation level, which instantiate_template doesn't do because
15334 functions and variables have sufficient context established by the
15335 callers. */
15336
15337 static tree
15338 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
15339 {
15340 struct pending_template *old_last_pend = last_pending_template;
15341 struct tinst_level *old_error_tinst = last_error_tinst_level;
15342 if (tmpl == error_mark_node || args == error_mark_node)
15343 return error_mark_node;
15344 tree tinst = build_tree_list (tmpl, args);
15345 if (!push_tinst_level (tinst))
15346 {
15347 ggc_free (tinst);
15348 return error_mark_node;
15349 }
15350
15351 args =
15352 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
15353 args, tmpl, complain,
15354 /*require_all_args=*/true,
15355 /*use_default_args=*/true);
15356
15357 tree r = instantiate_template (tmpl, args, complain);
15358 pop_tinst_level ();
15359 /* We can't free this if a pending_template entry or last_error_tinst_level
15360 is pointing at it. */
15361 if (last_pending_template == old_last_pend
15362 && last_error_tinst_level == old_error_tinst)
15363 ggc_free (tinst);
15364
15365 return r;
15366 }
15367
15368 /* PARM is a template parameter pack for FN. Returns true iff
15369 PARM is used in a deducible way in the argument list of FN. */
15370
15371 static bool
15372 pack_deducible_p (tree parm, tree fn)
15373 {
15374 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
15375 for (; t; t = TREE_CHAIN (t))
15376 {
15377 tree type = TREE_VALUE (t);
15378 tree packs;
15379 if (!PACK_EXPANSION_P (type))
15380 continue;
15381 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
15382 packs; packs = TREE_CHAIN (packs))
15383 if (TREE_VALUE (packs) == parm)
15384 {
15385 /* The template parameter pack is used in a function parameter
15386 pack. If this is the end of the parameter list, the
15387 template parameter pack is deducible. */
15388 if (TREE_CHAIN (t) == void_list_node)
15389 return true;
15390 else
15391 /* Otherwise, not. Well, it could be deduced from
15392 a non-pack parameter, but doing so would end up with
15393 a deduction mismatch, so don't bother. */
15394 return false;
15395 }
15396 }
15397 /* The template parameter pack isn't used in any function parameter
15398 packs, but it might be used deeper, e.g. tuple<Args...>. */
15399 return true;
15400 }
15401
15402 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
15403 NARGS elements of the arguments that are being used when calling
15404 it. TARGS is a vector into which the deduced template arguments
15405 are placed.
15406
15407 Return zero for success, 2 for an incomplete match that doesn't resolve
15408 all the types, and 1 for complete failure. An error message will be
15409 printed only for an incomplete match.
15410
15411 If FN is a conversion operator, or we are trying to produce a specific
15412 specialization, RETURN_TYPE is the return type desired.
15413
15414 The EXPLICIT_TARGS are explicit template arguments provided via a
15415 template-id.
15416
15417 The parameter STRICT is one of:
15418
15419 DEDUCE_CALL:
15420 We are deducing arguments for a function call, as in
15421 [temp.deduct.call].
15422
15423 DEDUCE_CONV:
15424 We are deducing arguments for a conversion function, as in
15425 [temp.deduct.conv].
15426
15427 DEDUCE_EXACT:
15428 We are deducing arguments when doing an explicit instantiation
15429 as in [temp.explicit], when determining an explicit specialization
15430 as in [temp.expl.spec], or when taking the address of a function
15431 template, as in [temp.deduct.funcaddr]. */
15432
15433 tree
15434 fn_type_unification (tree fn,
15435 tree explicit_targs,
15436 tree targs,
15437 const tree *args,
15438 unsigned int nargs,
15439 tree return_type,
15440 unification_kind_t strict,
15441 int flags,
15442 bool explain_p,
15443 bool decltype_p)
15444 {
15445 tree parms;
15446 tree fntype;
15447 tree decl = NULL_TREE;
15448 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
15449 bool ok;
15450 static int deduction_depth;
15451 struct pending_template *old_last_pend = last_pending_template;
15452 struct tinst_level *old_error_tinst = last_error_tinst_level;
15453 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
15454 tree tinst;
15455 tree r = error_mark_node;
15456
15457 if (decltype_p)
15458 complain |= tf_decltype;
15459
15460 /* In C++0x, it's possible to have a function template whose type depends
15461 on itself recursively. This is most obvious with decltype, but can also
15462 occur with enumeration scope (c++/48969). So we need to catch infinite
15463 recursion and reject the substitution at deduction time; this function
15464 will return error_mark_node for any repeated substitution.
15465
15466 This also catches excessive recursion such as when f<N> depends on
15467 f<N-1> across all integers, and returns error_mark_node for all the
15468 substitutions back up to the initial one.
15469
15470 This is, of course, not reentrant. */
15471 if (excessive_deduction_depth)
15472 return error_mark_node;
15473 tinst = build_tree_list (fn, NULL_TREE);
15474 ++deduction_depth;
15475
15476 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
15477
15478 fntype = TREE_TYPE (fn);
15479 if (explicit_targs)
15480 {
15481 /* [temp.deduct]
15482
15483 The specified template arguments must match the template
15484 parameters in kind (i.e., type, nontype, template), and there
15485 must not be more arguments than there are parameters;
15486 otherwise type deduction fails.
15487
15488 Nontype arguments must match the types of the corresponding
15489 nontype template parameters, or must be convertible to the
15490 types of the corresponding nontype parameters as specified in
15491 _temp.arg.nontype_, otherwise type deduction fails.
15492
15493 All references in the function type of the function template
15494 to the corresponding template parameters are replaced by the
15495 specified template argument values. If a substitution in a
15496 template parameter or in the function type of the function
15497 template results in an invalid type, type deduction fails. */
15498 int i, len = TREE_VEC_LENGTH (tparms);
15499 location_t loc = input_location;
15500 bool incomplete = false;
15501
15502 /* Adjust any explicit template arguments before entering the
15503 substitution context. */
15504 explicit_targs
15505 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
15506 complain,
15507 /*require_all_args=*/false,
15508 /*use_default_args=*/false));
15509 if (explicit_targs == error_mark_node)
15510 goto fail;
15511
15512 /* Substitute the explicit args into the function type. This is
15513 necessary so that, for instance, explicitly declared function
15514 arguments can match null pointed constants. If we were given
15515 an incomplete set of explicit args, we must not do semantic
15516 processing during substitution as we could create partial
15517 instantiations. */
15518 for (i = 0; i < len; i++)
15519 {
15520 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15521 bool parameter_pack = false;
15522 tree targ = TREE_VEC_ELT (explicit_targs, i);
15523
15524 /* Dig out the actual parm. */
15525 if (TREE_CODE (parm) == TYPE_DECL
15526 || TREE_CODE (parm) == TEMPLATE_DECL)
15527 {
15528 parm = TREE_TYPE (parm);
15529 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
15530 }
15531 else if (TREE_CODE (parm) == PARM_DECL)
15532 {
15533 parm = DECL_INITIAL (parm);
15534 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
15535 }
15536
15537 if (!parameter_pack && targ == NULL_TREE)
15538 /* No explicit argument for this template parameter. */
15539 incomplete = true;
15540
15541 if (parameter_pack && pack_deducible_p (parm, fn))
15542 {
15543 /* Mark the argument pack as "incomplete". We could
15544 still deduce more arguments during unification.
15545 We remove this mark in type_unification_real. */
15546 if (targ)
15547 {
15548 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
15549 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
15550 = ARGUMENT_PACK_ARGS (targ);
15551 }
15552
15553 /* We have some incomplete argument packs. */
15554 incomplete = true;
15555 }
15556 }
15557
15558 TREE_VALUE (tinst) = explicit_targs;
15559 if (!push_tinst_level (tinst))
15560 {
15561 excessive_deduction_depth = true;
15562 goto fail;
15563 }
15564 processing_template_decl += incomplete;
15565 input_location = DECL_SOURCE_LOCATION (fn);
15566 /* Ignore any access checks; we'll see them again in
15567 instantiate_template and they might have the wrong
15568 access path at this point. */
15569 push_deferring_access_checks (dk_deferred);
15570 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
15571 complain | tf_partial, NULL_TREE);
15572 pop_deferring_access_checks ();
15573 input_location = loc;
15574 processing_template_decl -= incomplete;
15575 pop_tinst_level ();
15576
15577 if (fntype == error_mark_node)
15578 goto fail;
15579
15580 /* Place the explicitly specified arguments in TARGS. */
15581 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
15582 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
15583 }
15584
15585 /* Never do unification on the 'this' parameter. */
15586 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
15587
15588 if (return_type)
15589 {
15590 tree *new_args;
15591
15592 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
15593 new_args = XALLOCAVEC (tree, nargs + 1);
15594 new_args[0] = return_type;
15595 memcpy (new_args + 1, args, nargs * sizeof (tree));
15596 args = new_args;
15597 ++nargs;
15598 }
15599
15600 /* We allow incomplete unification without an error message here
15601 because the standard doesn't seem to explicitly prohibit it. Our
15602 callers must be ready to deal with unification failures in any
15603 event. */
15604
15605 TREE_VALUE (tinst) = targs;
15606 /* If we aren't explaining yet, push tinst context so we can see where
15607 any errors (e.g. from class instantiations triggered by instantiation
15608 of default template arguments) come from. If we are explaining, this
15609 context is redundant. */
15610 if (!explain_p && !push_tinst_level (tinst))
15611 {
15612 excessive_deduction_depth = true;
15613 goto fail;
15614 }
15615
15616 /* type_unification_real will pass back any access checks from default
15617 template argument substitution. */
15618 vec<deferred_access_check, va_gc> *checks;
15619 checks = NULL;
15620
15621 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15622 targs, parms, args, nargs, /*subr=*/0,
15623 strict, flags, &checks, explain_p);
15624 if (!explain_p)
15625 pop_tinst_level ();
15626 if (!ok)
15627 goto fail;
15628
15629 /* Now that we have bindings for all of the template arguments,
15630 ensure that the arguments deduced for the template template
15631 parameters have compatible template parameter lists. We cannot
15632 check this property before we have deduced all template
15633 arguments, because the template parameter types of a template
15634 template parameter might depend on prior template parameters
15635 deduced after the template template parameter. The following
15636 ill-formed example illustrates this issue:
15637
15638 template<typename T, template<T> class C> void f(C<5>, T);
15639
15640 template<int N> struct X {};
15641
15642 void g() {
15643 f(X<5>(), 5l); // error: template argument deduction fails
15644 }
15645
15646 The template parameter list of 'C' depends on the template type
15647 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
15648 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
15649 time that we deduce 'C'. */
15650 if (!template_template_parm_bindings_ok_p
15651 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
15652 {
15653 unify_inconsistent_template_template_parameters (explain_p);
15654 goto fail;
15655 }
15656
15657 /* All is well so far. Now, check:
15658
15659 [temp.deduct]
15660
15661 When all template arguments have been deduced, all uses of
15662 template parameters in nondeduced contexts are replaced with
15663 the corresponding deduced argument values. If the
15664 substitution results in an invalid type, as described above,
15665 type deduction fails. */
15666 TREE_VALUE (tinst) = targs;
15667 if (!push_tinst_level (tinst))
15668 {
15669 excessive_deduction_depth = true;
15670 goto fail;
15671 }
15672
15673 /* Also collect access checks from the instantiation. */
15674 reopen_deferring_access_checks (checks);
15675
15676 decl = instantiate_template (fn, targs, complain);
15677
15678 checks = get_deferred_access_checks ();
15679 pop_deferring_access_checks ();
15680
15681 pop_tinst_level ();
15682
15683 if (decl == error_mark_node)
15684 goto fail;
15685
15686 /* Now perform any access checks encountered during substitution. */
15687 push_access_scope (decl);
15688 ok = perform_access_checks (checks, complain);
15689 pop_access_scope (decl);
15690 if (!ok)
15691 goto fail;
15692
15693 /* If we're looking for an exact match, check that what we got
15694 is indeed an exact match. It might not be if some template
15695 parameters are used in non-deduced contexts. */
15696 if (strict == DEDUCE_EXACT)
15697 {
15698 tree substed = TREE_TYPE (decl);
15699 unsigned int i;
15700
15701 tree sarg
15702 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
15703 if (return_type)
15704 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
15705 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
15706 if (!same_type_p (args[i], TREE_VALUE (sarg)))
15707 {
15708 unify_type_mismatch (explain_p, args[i],
15709 TREE_VALUE (sarg));
15710 goto fail;
15711 }
15712 }
15713
15714 r = decl;
15715
15716 fail:
15717 --deduction_depth;
15718 if (excessive_deduction_depth)
15719 {
15720 if (deduction_depth == 0)
15721 /* Reset once we're all the way out. */
15722 excessive_deduction_depth = false;
15723 }
15724
15725 /* We can't free this if a pending_template entry or last_error_tinst_level
15726 is pointing at it. */
15727 if (last_pending_template == old_last_pend
15728 && last_error_tinst_level == old_error_tinst)
15729 ggc_free (tinst);
15730
15731 return r;
15732 }
15733
15734 /* Adjust types before performing type deduction, as described in
15735 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15736 sections are symmetric. PARM is the type of a function parameter
15737 or the return type of the conversion function. ARG is the type of
15738 the argument passed to the call, or the type of the value
15739 initialized with the result of the conversion function.
15740 ARG_EXPR is the original argument expression, which may be null. */
15741
15742 static int
15743 maybe_adjust_types_for_deduction (unification_kind_t strict,
15744 tree* parm,
15745 tree* arg,
15746 tree arg_expr)
15747 {
15748 int result = 0;
15749
15750 switch (strict)
15751 {
15752 case DEDUCE_CALL:
15753 break;
15754
15755 case DEDUCE_CONV:
15756 {
15757 /* Swap PARM and ARG throughout the remainder of this
15758 function; the handling is precisely symmetric since PARM
15759 will initialize ARG rather than vice versa. */
15760 tree* temp = parm;
15761 parm = arg;
15762 arg = temp;
15763 break;
15764 }
15765
15766 case DEDUCE_EXACT:
15767 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15768 too, but here handle it by stripping the reference from PARM
15769 rather than by adding it to ARG. */
15770 if (TREE_CODE (*parm) == REFERENCE_TYPE
15771 && TYPE_REF_IS_RVALUE (*parm)
15772 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15773 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15774 && TREE_CODE (*arg) == REFERENCE_TYPE
15775 && !TYPE_REF_IS_RVALUE (*arg))
15776 *parm = TREE_TYPE (*parm);
15777 /* Nothing else to do in this case. */
15778 return 0;
15779
15780 default:
15781 gcc_unreachable ();
15782 }
15783
15784 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15785 {
15786 /* [temp.deduct.call]
15787
15788 If P is not a reference type:
15789
15790 --If A is an array type, the pointer type produced by the
15791 array-to-pointer standard conversion (_conv.array_) is
15792 used in place of A for type deduction; otherwise,
15793
15794 --If A is a function type, the pointer type produced by
15795 the function-to-pointer standard conversion
15796 (_conv.func_) is used in place of A for type deduction;
15797 otherwise,
15798
15799 --If A is a cv-qualified type, the top level
15800 cv-qualifiers of A's type are ignored for type
15801 deduction. */
15802 if (TREE_CODE (*arg) == ARRAY_TYPE)
15803 *arg = build_pointer_type (TREE_TYPE (*arg));
15804 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15805 *arg = build_pointer_type (*arg);
15806 else
15807 *arg = TYPE_MAIN_VARIANT (*arg);
15808 }
15809
15810 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15811 of the form T&&, where T is a template parameter, and the argument
15812 is an lvalue, T is deduced as A& */
15813 if (TREE_CODE (*parm) == REFERENCE_TYPE
15814 && TYPE_REF_IS_RVALUE (*parm)
15815 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15816 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15817 && (arg_expr ? real_lvalue_p (arg_expr)
15818 /* try_one_overload doesn't provide an arg_expr, but
15819 functions are always lvalues. */
15820 : TREE_CODE (*arg) == FUNCTION_TYPE))
15821 *arg = build_reference_type (*arg);
15822
15823 /* [temp.deduct.call]
15824
15825 If P is a cv-qualified type, the top level cv-qualifiers
15826 of P's type are ignored for type deduction. If P is a
15827 reference type, the type referred to by P is used for
15828 type deduction. */
15829 *parm = TYPE_MAIN_VARIANT (*parm);
15830 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15831 {
15832 *parm = TREE_TYPE (*parm);
15833 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15834 }
15835
15836 /* DR 322. For conversion deduction, remove a reference type on parm
15837 too (which has been swapped into ARG). */
15838 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15839 *arg = TREE_TYPE (*arg);
15840
15841 return result;
15842 }
15843
15844 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15845 template which does contain any deducible template parameters; check if
15846 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15847 unify_one_argument. */
15848
15849 static int
15850 check_non_deducible_conversion (tree parm, tree arg, int strict,
15851 int flags, bool explain_p)
15852 {
15853 tree type;
15854
15855 if (!TYPE_P (arg))
15856 type = TREE_TYPE (arg);
15857 else
15858 type = arg;
15859
15860 if (same_type_p (parm, type))
15861 return unify_success (explain_p);
15862
15863 if (strict == DEDUCE_CONV)
15864 {
15865 if (can_convert_arg (type, parm, NULL_TREE, flags,
15866 explain_p ? tf_warning_or_error : tf_none))
15867 return unify_success (explain_p);
15868 }
15869 else if (strict != DEDUCE_EXACT)
15870 {
15871 if (can_convert_arg (parm, type,
15872 TYPE_P (arg) ? NULL_TREE : arg,
15873 flags, explain_p ? tf_warning_or_error : tf_none))
15874 return unify_success (explain_p);
15875 }
15876
15877 if (strict == DEDUCE_EXACT)
15878 return unify_type_mismatch (explain_p, parm, arg);
15879 else
15880 return unify_arg_conversion (explain_p, parm, type, arg);
15881 }
15882
15883 static bool uses_deducible_template_parms (tree type);
15884
15885 /* Returns true iff the expression EXPR is one from which a template
15886 argument can be deduced. In other words, if it's an undecorated
15887 use of a template non-type parameter. */
15888
15889 static bool
15890 deducible_expression (tree expr)
15891 {
15892 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
15893 }
15894
15895 /* Returns true iff the array domain DOMAIN uses a template parameter in a
15896 deducible way; that is, if it has a max value of <PARM> - 1. */
15897
15898 static bool
15899 deducible_array_bound (tree domain)
15900 {
15901 if (domain == NULL_TREE)
15902 return false;
15903
15904 tree max = TYPE_MAX_VALUE (domain);
15905 if (TREE_CODE (max) != MINUS_EXPR)
15906 return false;
15907
15908 return deducible_expression (TREE_OPERAND (max, 0));
15909 }
15910
15911 /* Returns true iff the template arguments ARGS use a template parameter
15912 in a deducible way. */
15913
15914 static bool
15915 deducible_template_args (tree args)
15916 {
15917 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
15918 {
15919 bool deducible;
15920 tree elt = TREE_VEC_ELT (args, i);
15921 if (ARGUMENT_PACK_P (elt))
15922 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
15923 else
15924 {
15925 if (PACK_EXPANSION_P (elt))
15926 elt = PACK_EXPANSION_PATTERN (elt);
15927 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
15928 deducible = true;
15929 else if (TYPE_P (elt))
15930 deducible = uses_deducible_template_parms (elt);
15931 else
15932 deducible = deducible_expression (elt);
15933 }
15934 if (deducible)
15935 return true;
15936 }
15937 return false;
15938 }
15939
15940 /* Returns true iff TYPE contains any deducible references to template
15941 parameters, as per 14.8.2.5. */
15942
15943 static bool
15944 uses_deducible_template_parms (tree type)
15945 {
15946 if (PACK_EXPANSION_P (type))
15947 type = PACK_EXPANSION_PATTERN (type);
15948
15949 /* T
15950 cv-list T
15951 TT<T>
15952 TT<i>
15953 TT<> */
15954 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
15955 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15956 return true;
15957
15958 /* T*
15959 T&
15960 T&& */
15961 if (POINTER_TYPE_P (type))
15962 return uses_deducible_template_parms (TREE_TYPE (type));
15963
15964 /* T[integer-constant ]
15965 type [i] */
15966 if (TREE_CODE (type) == ARRAY_TYPE)
15967 return (uses_deducible_template_parms (TREE_TYPE (type))
15968 || deducible_array_bound (TYPE_DOMAIN (type)));
15969
15970 /* T type ::*
15971 type T::*
15972 T T::*
15973 T (type ::*)()
15974 type (T::*)()
15975 type (type ::*)(T)
15976 type (T::*)(T)
15977 T (type ::*)(T)
15978 T (T::*)()
15979 T (T::*)(T) */
15980 if (TYPE_PTRMEM_P (type))
15981 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
15982 || (uses_deducible_template_parms
15983 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
15984
15985 /* template-name <T> (where template-name refers to a class template)
15986 template-name <i> (where template-name refers to a class template) */
15987 if (CLASS_TYPE_P (type)
15988 && CLASSTYPE_TEMPLATE_INFO (type)
15989 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
15990 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
15991 (CLASSTYPE_TI_ARGS (type)));
15992
15993 /* type (T)
15994 T()
15995 T(T) */
15996 if (TREE_CODE (type) == FUNCTION_TYPE
15997 || TREE_CODE (type) == METHOD_TYPE)
15998 {
15999 if (uses_deducible_template_parms (TREE_TYPE (type)))
16000 return true;
16001 tree parm = TYPE_ARG_TYPES (type);
16002 if (TREE_CODE (type) == METHOD_TYPE)
16003 parm = TREE_CHAIN (parm);
16004 for (; parm; parm = TREE_CHAIN (parm))
16005 if (uses_deducible_template_parms (TREE_VALUE (parm)))
16006 return true;
16007 }
16008
16009 return false;
16010 }
16011
16012 /* Subroutine of type_unification_real and unify_pack_expansion to
16013 handle unification of a single P/A pair. Parameters are as
16014 for those functions. */
16015
16016 static int
16017 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
16018 int subr, unification_kind_t strict, int flags,
16019 bool explain_p)
16020 {
16021 tree arg_expr = NULL_TREE;
16022 int arg_strict;
16023
16024 if (arg == error_mark_node || parm == error_mark_node)
16025 return unify_invalid (explain_p);
16026 if (arg == unknown_type_node)
16027 /* We can't deduce anything from this, but we might get all the
16028 template args from other function args. */
16029 return unify_success (explain_p);
16030
16031 /* Implicit conversions (Clause 4) will be performed on a function
16032 argument to convert it to the type of the corresponding function
16033 parameter if the parameter type contains no template-parameters that
16034 participate in template argument deduction. */
16035 if (TYPE_P (parm) && !uses_template_parms (parm))
16036 /* For function parameters that contain no template-parameters at all,
16037 we have historically checked for convertibility in order to shortcut
16038 consideration of this candidate. */
16039 return check_non_deducible_conversion (parm, arg, strict, flags,
16040 explain_p);
16041 else if (strict == DEDUCE_CALL
16042 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
16043 /* For function parameters with only non-deducible template parameters,
16044 just return. */
16045 return unify_success (explain_p);
16046
16047 switch (strict)
16048 {
16049 case DEDUCE_CALL:
16050 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
16051 | UNIFY_ALLOW_MORE_CV_QUAL
16052 | UNIFY_ALLOW_DERIVED);
16053 break;
16054
16055 case DEDUCE_CONV:
16056 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
16057 break;
16058
16059 case DEDUCE_EXACT:
16060 arg_strict = UNIFY_ALLOW_NONE;
16061 break;
16062
16063 default:
16064 gcc_unreachable ();
16065 }
16066
16067 /* We only do these transformations if this is the top-level
16068 parameter_type_list in a call or declaration matching; in other
16069 situations (nested function declarators, template argument lists) we
16070 won't be comparing a type to an expression, and we don't do any type
16071 adjustments. */
16072 if (!subr)
16073 {
16074 if (!TYPE_P (arg))
16075 {
16076 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
16077 if (type_unknown_p (arg))
16078 {
16079 /* [temp.deduct.type] A template-argument can be
16080 deduced from a pointer to function or pointer
16081 to member function argument if the set of
16082 overloaded functions does not contain function
16083 templates and at most one of a set of
16084 overloaded functions provides a unique
16085 match. */
16086
16087 if (resolve_overloaded_unification
16088 (tparms, targs, parm, arg, strict,
16089 arg_strict, explain_p))
16090 return unify_success (explain_p);
16091 return unify_overload_resolution_failure (explain_p, arg);
16092 }
16093
16094 arg_expr = arg;
16095 arg = unlowered_expr_type (arg);
16096 if (arg == error_mark_node)
16097 return unify_invalid (explain_p);
16098 }
16099
16100 arg_strict |=
16101 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
16102 }
16103 else
16104 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
16105 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
16106
16107 /* For deduction from an init-list we need the actual list. */
16108 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
16109 arg = arg_expr;
16110 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
16111 }
16112
16113 /* Most parms like fn_type_unification.
16114
16115 If SUBR is 1, we're being called recursively (to unify the
16116 arguments of a function or method parameter of a function
16117 template).
16118
16119 CHECKS is a pointer to a vector of access checks encountered while
16120 substituting default template arguments. */
16121
16122 static int
16123 type_unification_real (tree tparms,
16124 tree targs,
16125 tree xparms,
16126 const tree *xargs,
16127 unsigned int xnargs,
16128 int subr,
16129 unification_kind_t strict,
16130 int flags,
16131 vec<deferred_access_check, va_gc> **checks,
16132 bool explain_p)
16133 {
16134 tree parm, arg;
16135 int i;
16136 int ntparms = TREE_VEC_LENGTH (tparms);
16137 int saw_undeduced = 0;
16138 tree parms;
16139 const tree *args;
16140 unsigned int nargs;
16141 unsigned int ia;
16142
16143 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
16144 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
16145 gcc_assert (ntparms > 0);
16146
16147 /* Reset the number of non-defaulted template arguments contained
16148 in TARGS. */
16149 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
16150
16151 again:
16152 parms = xparms;
16153 args = xargs;
16154 nargs = xnargs;
16155
16156 ia = 0;
16157 while (parms && parms != void_list_node
16158 && ia < nargs)
16159 {
16160 parm = TREE_VALUE (parms);
16161
16162 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
16163 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
16164 /* For a function parameter pack that occurs at the end of the
16165 parameter-declaration-list, the type A of each remaining
16166 argument of the call is compared with the type P of the
16167 declarator-id of the function parameter pack. */
16168 break;
16169
16170 parms = TREE_CHAIN (parms);
16171
16172 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
16173 /* For a function parameter pack that does not occur at the
16174 end of the parameter-declaration-list, the type of the
16175 parameter pack is a non-deduced context. */
16176 continue;
16177
16178 arg = args[ia];
16179 ++ia;
16180
16181 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16182 flags, explain_p))
16183 return 1;
16184 }
16185
16186 if (parms
16187 && parms != void_list_node
16188 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
16189 {
16190 /* Unify the remaining arguments with the pack expansion type. */
16191 tree argvec;
16192 tree parmvec = make_tree_vec (1);
16193
16194 /* Allocate a TREE_VEC and copy in all of the arguments */
16195 argvec = make_tree_vec (nargs - ia);
16196 for (i = 0; ia < nargs; ++ia, ++i)
16197 TREE_VEC_ELT (argvec, i) = args[ia];
16198
16199 /* Copy the parameter into parmvec. */
16200 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
16201 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
16202 /*subr=*/subr, explain_p))
16203 return 1;
16204
16205 /* Advance to the end of the list of parameters. */
16206 parms = TREE_CHAIN (parms);
16207 }
16208
16209 /* Fail if we've reached the end of the parm list, and more args
16210 are present, and the parm list isn't variadic. */
16211 if (ia < nargs && parms == void_list_node)
16212 return unify_too_many_arguments (explain_p, nargs, ia);
16213 /* Fail if parms are left and they don't have default values. */
16214 if (parms && parms != void_list_node
16215 && TREE_PURPOSE (parms) == NULL_TREE)
16216 {
16217 unsigned int count = nargs;
16218 tree p = parms;
16219 while (p && p != void_list_node)
16220 {
16221 count++;
16222 p = TREE_CHAIN (p);
16223 }
16224 return unify_too_few_arguments (explain_p, ia, count);
16225 }
16226
16227 if (!subr)
16228 {
16229 tsubst_flags_t complain = (explain_p
16230 ? tf_warning_or_error
16231 : tf_none);
16232
16233 for (i = 0; i < ntparms; i++)
16234 {
16235 tree targ = TREE_VEC_ELT (targs, i);
16236 tree tparm = TREE_VEC_ELT (tparms, i);
16237
16238 /* Clear the "incomplete" flags on all argument packs now so that
16239 substituting them into later default arguments works. */
16240 if (targ && ARGUMENT_PACK_P (targ))
16241 {
16242 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
16243 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
16244 }
16245
16246 if (targ || tparm == error_mark_node)
16247 continue;
16248 tparm = TREE_VALUE (tparm);
16249
16250 /* If this is an undeduced nontype parameter that depends on
16251 a type parameter, try another pass; its type may have been
16252 deduced from a later argument than the one from which
16253 this parameter can be deduced. */
16254 if (TREE_CODE (tparm) == PARM_DECL
16255 && uses_template_parms (TREE_TYPE (tparm))
16256 && !saw_undeduced++)
16257 goto again;
16258
16259 /* Core issue #226 (C++0x) [temp.deduct]:
16260
16261 If a template argument has not been deduced, its
16262 default template argument, if any, is used.
16263
16264 When we are in C++98 mode, TREE_PURPOSE will either
16265 be NULL_TREE or ERROR_MARK_NODE, so we do not need
16266 to explicitly check cxx_dialect here. */
16267 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
16268 {
16269 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
16270 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
16271 reopen_deferring_access_checks (*checks);
16272 location_t save_loc = input_location;
16273 if (DECL_P (parm))
16274 input_location = DECL_SOURCE_LOCATION (parm);
16275 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
16276 arg = convert_template_argument (parm, arg, targs, complain,
16277 i, NULL_TREE);
16278 input_location = save_loc;
16279 *checks = get_deferred_access_checks ();
16280 pop_deferring_access_checks ();
16281 if (arg == error_mark_node)
16282 return 1;
16283 else
16284 {
16285 TREE_VEC_ELT (targs, i) = arg;
16286 /* The position of the first default template argument,
16287 is also the number of non-defaulted arguments in TARGS.
16288 Record that. */
16289 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16290 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
16291 continue;
16292 }
16293 }
16294
16295 /* If the type parameter is a parameter pack, then it will
16296 be deduced to an empty parameter pack. */
16297 if (template_parameter_pack_p (tparm))
16298 {
16299 tree arg;
16300
16301 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
16302 {
16303 arg = make_node (NONTYPE_ARGUMENT_PACK);
16304 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
16305 TREE_CONSTANT (arg) = 1;
16306 }
16307 else
16308 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
16309
16310 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
16311
16312 TREE_VEC_ELT (targs, i) = arg;
16313 continue;
16314 }
16315
16316 return unify_parameter_deduction_failure (explain_p, tparm);
16317 }
16318 }
16319 #ifdef ENABLE_CHECKING
16320 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
16321 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
16322 #endif
16323
16324 return unify_success (explain_p);
16325 }
16326
16327 /* Subroutine of type_unification_real. Args are like the variables
16328 at the call site. ARG is an overloaded function (or template-id);
16329 we try deducing template args from each of the overloads, and if
16330 only one succeeds, we go with that. Modifies TARGS and returns
16331 true on success. */
16332
16333 static bool
16334 resolve_overloaded_unification (tree tparms,
16335 tree targs,
16336 tree parm,
16337 tree arg,
16338 unification_kind_t strict,
16339 int sub_strict,
16340 bool explain_p)
16341 {
16342 tree tempargs = copy_node (targs);
16343 int good = 0;
16344 tree goodfn = NULL_TREE;
16345 bool addr_p;
16346
16347 if (TREE_CODE (arg) == ADDR_EXPR)
16348 {
16349 arg = TREE_OPERAND (arg, 0);
16350 addr_p = true;
16351 }
16352 else
16353 addr_p = false;
16354
16355 if (TREE_CODE (arg) == COMPONENT_REF)
16356 /* Handle `&x' where `x' is some static or non-static member
16357 function name. */
16358 arg = TREE_OPERAND (arg, 1);
16359
16360 if (TREE_CODE (arg) == OFFSET_REF)
16361 arg = TREE_OPERAND (arg, 1);
16362
16363 /* Strip baselink information. */
16364 if (BASELINK_P (arg))
16365 arg = BASELINK_FUNCTIONS (arg);
16366
16367 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
16368 {
16369 /* If we got some explicit template args, we need to plug them into
16370 the affected templates before we try to unify, in case the
16371 explicit args will completely resolve the templates in question. */
16372
16373 int ok = 0;
16374 tree expl_subargs = TREE_OPERAND (arg, 1);
16375 arg = TREE_OPERAND (arg, 0);
16376
16377 for (; arg; arg = OVL_NEXT (arg))
16378 {
16379 tree fn = OVL_CURRENT (arg);
16380 tree subargs, elem;
16381
16382 if (TREE_CODE (fn) != TEMPLATE_DECL)
16383 continue;
16384
16385 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16386 expl_subargs, NULL_TREE, tf_none,
16387 /*require_all_args=*/true,
16388 /*use_default_args=*/true);
16389 if (subargs != error_mark_node
16390 && !any_dependent_template_arguments_p (subargs))
16391 {
16392 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
16393 if (try_one_overload (tparms, targs, tempargs, parm,
16394 elem, strict, sub_strict, addr_p, explain_p)
16395 && (!goodfn || !same_type_p (goodfn, elem)))
16396 {
16397 goodfn = elem;
16398 ++good;
16399 }
16400 }
16401 else if (subargs)
16402 ++ok;
16403 }
16404 /* If no templates (or more than one) are fully resolved by the
16405 explicit arguments, this template-id is a non-deduced context; it
16406 could still be OK if we deduce all template arguments for the
16407 enclosing call through other arguments. */
16408 if (good != 1)
16409 good = ok;
16410 }
16411 else if (TREE_CODE (arg) != OVERLOAD
16412 && TREE_CODE (arg) != FUNCTION_DECL)
16413 /* If ARG is, for example, "(0, &f)" then its type will be unknown
16414 -- but the deduction does not succeed because the expression is
16415 not just the function on its own. */
16416 return false;
16417 else
16418 for (; arg; arg = OVL_NEXT (arg))
16419 if (try_one_overload (tparms, targs, tempargs, parm,
16420 TREE_TYPE (OVL_CURRENT (arg)),
16421 strict, sub_strict, addr_p, explain_p)
16422 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
16423 {
16424 goodfn = OVL_CURRENT (arg);
16425 ++good;
16426 }
16427
16428 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16429 to function or pointer to member function argument if the set of
16430 overloaded functions does not contain function templates and at most
16431 one of a set of overloaded functions provides a unique match.
16432
16433 So if we found multiple possibilities, we return success but don't
16434 deduce anything. */
16435
16436 if (good == 1)
16437 {
16438 int i = TREE_VEC_LENGTH (targs);
16439 for (; i--; )
16440 if (TREE_VEC_ELT (tempargs, i))
16441 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
16442 }
16443 if (good)
16444 return true;
16445
16446 return false;
16447 }
16448
16449 /* Core DR 115: In contexts where deduction is done and fails, or in
16450 contexts where deduction is not done, if a template argument list is
16451 specified and it, along with any default template arguments, identifies
16452 a single function template specialization, then the template-id is an
16453 lvalue for the function template specialization. */
16454
16455 tree
16456 resolve_nondeduced_context (tree orig_expr)
16457 {
16458 tree expr, offset, baselink;
16459 bool addr;
16460
16461 if (!type_unknown_p (orig_expr))
16462 return orig_expr;
16463
16464 expr = orig_expr;
16465 addr = false;
16466 offset = NULL_TREE;
16467 baselink = NULL_TREE;
16468
16469 if (TREE_CODE (expr) == ADDR_EXPR)
16470 {
16471 expr = TREE_OPERAND (expr, 0);
16472 addr = true;
16473 }
16474 if (TREE_CODE (expr) == OFFSET_REF)
16475 {
16476 offset = expr;
16477 expr = TREE_OPERAND (expr, 1);
16478 }
16479 if (BASELINK_P (expr))
16480 {
16481 baselink = expr;
16482 expr = BASELINK_FUNCTIONS (expr);
16483 }
16484
16485 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
16486 {
16487 int good = 0;
16488 tree goodfn = NULL_TREE;
16489
16490 /* If we got some explicit template args, we need to plug them into
16491 the affected templates before we try to unify, in case the
16492 explicit args will completely resolve the templates in question. */
16493
16494 tree expl_subargs = TREE_OPERAND (expr, 1);
16495 tree arg = TREE_OPERAND (expr, 0);
16496 tree badfn = NULL_TREE;
16497 tree badargs = NULL_TREE;
16498
16499 for (; arg; arg = OVL_NEXT (arg))
16500 {
16501 tree fn = OVL_CURRENT (arg);
16502 tree subargs, elem;
16503
16504 if (TREE_CODE (fn) != TEMPLATE_DECL)
16505 continue;
16506
16507 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
16508 expl_subargs, NULL_TREE, tf_none,
16509 /*require_all_args=*/true,
16510 /*use_default_args=*/true);
16511 if (subargs != error_mark_node
16512 && !any_dependent_template_arguments_p (subargs))
16513 {
16514 elem = instantiate_template (fn, subargs, tf_none);
16515 if (elem == error_mark_node)
16516 {
16517 badfn = fn;
16518 badargs = subargs;
16519 }
16520 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
16521 {
16522 goodfn = elem;
16523 ++good;
16524 }
16525 }
16526 }
16527 if (good == 1)
16528 {
16529 mark_used (goodfn);
16530 expr = goodfn;
16531 if (baselink)
16532 expr = build_baselink (BASELINK_BINFO (baselink),
16533 BASELINK_ACCESS_BINFO (baselink),
16534 expr, BASELINK_OPTYPE (baselink));
16535 if (offset)
16536 {
16537 tree base
16538 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
16539 expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
16540 }
16541 if (addr)
16542 expr = cp_build_addr_expr (expr, tf_warning_or_error);
16543 return expr;
16544 }
16545 else if (good == 0 && badargs)
16546 /* There were no good options and at least one bad one, so let the
16547 user know what the problem is. */
16548 instantiate_template (badfn, badargs, tf_warning_or_error);
16549 }
16550 return orig_expr;
16551 }
16552
16553 /* Subroutine of resolve_overloaded_unification; does deduction for a single
16554 overload. Fills TARGS with any deduced arguments, or error_mark_node if
16555 different overloads deduce different arguments for a given parm.
16556 ADDR_P is true if the expression for which deduction is being
16557 performed was of the form "& fn" rather than simply "fn".
16558
16559 Returns 1 on success. */
16560
16561 static int
16562 try_one_overload (tree tparms,
16563 tree orig_targs,
16564 tree targs,
16565 tree parm,
16566 tree arg,
16567 unification_kind_t strict,
16568 int sub_strict,
16569 bool addr_p,
16570 bool explain_p)
16571 {
16572 int nargs;
16573 tree tempargs;
16574 int i;
16575
16576 if (arg == error_mark_node)
16577 return 0;
16578
16579 /* [temp.deduct.type] A template-argument can be deduced from a pointer
16580 to function or pointer to member function argument if the set of
16581 overloaded functions does not contain function templates and at most
16582 one of a set of overloaded functions provides a unique match.
16583
16584 So if this is a template, just return success. */
16585
16586 if (uses_template_parms (arg))
16587 return 1;
16588
16589 if (TREE_CODE (arg) == METHOD_TYPE)
16590 arg = build_ptrmemfunc_type (build_pointer_type (arg));
16591 else if (addr_p)
16592 arg = build_pointer_type (arg);
16593
16594 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
16595
16596 /* We don't copy orig_targs for this because if we have already deduced
16597 some template args from previous args, unify would complain when we
16598 try to deduce a template parameter for the same argument, even though
16599 there isn't really a conflict. */
16600 nargs = TREE_VEC_LENGTH (targs);
16601 tempargs = make_tree_vec (nargs);
16602
16603 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
16604 return 0;
16605
16606 /* First make sure we didn't deduce anything that conflicts with
16607 explicitly specified args. */
16608 for (i = nargs; i--; )
16609 {
16610 tree elt = TREE_VEC_ELT (tempargs, i);
16611 tree oldelt = TREE_VEC_ELT (orig_targs, i);
16612
16613 if (!elt)
16614 /*NOP*/;
16615 else if (uses_template_parms (elt))
16616 /* Since we're unifying against ourselves, we will fill in
16617 template args used in the function parm list with our own
16618 template parms. Discard them. */
16619 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
16620 else if (oldelt && !template_args_equal (oldelt, elt))
16621 return 0;
16622 }
16623
16624 for (i = nargs; i--; )
16625 {
16626 tree elt = TREE_VEC_ELT (tempargs, i);
16627
16628 if (elt)
16629 TREE_VEC_ELT (targs, i) = elt;
16630 }
16631
16632 return 1;
16633 }
16634
16635 /* PARM is a template class (perhaps with unbound template
16636 parameters). ARG is a fully instantiated type. If ARG can be
16637 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
16638 TARGS are as for unify. */
16639
16640 static tree
16641 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
16642 bool explain_p)
16643 {
16644 tree copy_of_targs;
16645
16646 if (!CLASSTYPE_TEMPLATE_INFO (arg)
16647 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
16648 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
16649 return NULL_TREE;
16650
16651 /* We need to make a new template argument vector for the call to
16652 unify. If we used TARGS, we'd clutter it up with the result of
16653 the attempted unification, even if this class didn't work out.
16654 We also don't want to commit ourselves to all the unifications
16655 we've already done, since unification is supposed to be done on
16656 an argument-by-argument basis. In other words, consider the
16657 following pathological case:
16658
16659 template <int I, int J, int K>
16660 struct S {};
16661
16662 template <int I, int J>
16663 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
16664
16665 template <int I, int J, int K>
16666 void f(S<I, J, K>, S<I, I, I>);
16667
16668 void g() {
16669 S<0, 0, 0> s0;
16670 S<0, 1, 2> s2;
16671
16672 f(s0, s2);
16673 }
16674
16675 Now, by the time we consider the unification involving `s2', we
16676 already know that we must have `f<0, 0, 0>'. But, even though
16677 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
16678 because there are two ways to unify base classes of S<0, 1, 2>
16679 with S<I, I, I>. If we kept the already deduced knowledge, we
16680 would reject the possibility I=1. */
16681 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
16682
16683 /* If unification failed, we're done. */
16684 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
16685 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
16686 return NULL_TREE;
16687
16688 return arg;
16689 }
16690
16691 /* Given a template type PARM and a class type ARG, find the unique
16692 base type in ARG that is an instance of PARM. We do not examine
16693 ARG itself; only its base-classes. If there is not exactly one
16694 appropriate base class, return NULL_TREE. PARM may be the type of
16695 a partial specialization, as well as a plain template type. Used
16696 by unify. */
16697
16698 static enum template_base_result
16699 get_template_base (tree tparms, tree targs, tree parm, tree arg,
16700 bool explain_p, tree *result)
16701 {
16702 tree rval = NULL_TREE;
16703 tree binfo;
16704
16705 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
16706
16707 binfo = TYPE_BINFO (complete_type (arg));
16708 if (!binfo)
16709 {
16710 /* The type could not be completed. */
16711 *result = NULL_TREE;
16712 return tbr_incomplete_type;
16713 }
16714
16715 /* Walk in inheritance graph order. The search order is not
16716 important, and this avoids multiple walks of virtual bases. */
16717 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
16718 {
16719 tree r = try_class_unification (tparms, targs, parm,
16720 BINFO_TYPE (binfo), explain_p);
16721
16722 if (r)
16723 {
16724 /* If there is more than one satisfactory baseclass, then:
16725
16726 [temp.deduct.call]
16727
16728 If they yield more than one possible deduced A, the type
16729 deduction fails.
16730
16731 applies. */
16732 if (rval && !same_type_p (r, rval))
16733 {
16734 *result = NULL_TREE;
16735 return tbr_ambiguous_baseclass;
16736 }
16737
16738 rval = r;
16739 }
16740 }
16741
16742 *result = rval;
16743 return tbr_success;
16744 }
16745
16746 /* Returns the level of DECL, which declares a template parameter. */
16747
16748 static int
16749 template_decl_level (tree decl)
16750 {
16751 switch (TREE_CODE (decl))
16752 {
16753 case TYPE_DECL:
16754 case TEMPLATE_DECL:
16755 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
16756
16757 case PARM_DECL:
16758 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
16759
16760 default:
16761 gcc_unreachable ();
16762 }
16763 return 0;
16764 }
16765
16766 /* Decide whether ARG can be unified with PARM, considering only the
16767 cv-qualifiers of each type, given STRICT as documented for unify.
16768 Returns nonzero iff the unification is OK on that basis. */
16769
16770 static int
16771 check_cv_quals_for_unify (int strict, tree arg, tree parm)
16772 {
16773 int arg_quals = cp_type_quals (arg);
16774 int parm_quals = cp_type_quals (parm);
16775
16776 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16777 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16778 {
16779 /* Although a CVR qualifier is ignored when being applied to a
16780 substituted template parameter ([8.3.2]/1 for example), that
16781 does not allow us to unify "const T" with "int&" because both
16782 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
16783 It is ok when we're allowing additional CV qualifiers
16784 at the outer level [14.8.2.1]/3,1st bullet. */
16785 if ((TREE_CODE (arg) == REFERENCE_TYPE
16786 || TREE_CODE (arg) == FUNCTION_TYPE
16787 || TREE_CODE (arg) == METHOD_TYPE)
16788 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
16789 return 0;
16790
16791 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
16792 && (parm_quals & TYPE_QUAL_RESTRICT))
16793 return 0;
16794 }
16795
16796 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
16797 && (arg_quals & parm_quals) != parm_quals)
16798 return 0;
16799
16800 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
16801 && (parm_quals & arg_quals) != arg_quals)
16802 return 0;
16803
16804 return 1;
16805 }
16806
16807 /* Determines the LEVEL and INDEX for the template parameter PARM. */
16808 void
16809 template_parm_level_and_index (tree parm, int* level, int* index)
16810 {
16811 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16812 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16813 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16814 {
16815 *index = TEMPLATE_TYPE_IDX (parm);
16816 *level = TEMPLATE_TYPE_LEVEL (parm);
16817 }
16818 else
16819 {
16820 *index = TEMPLATE_PARM_IDX (parm);
16821 *level = TEMPLATE_PARM_LEVEL (parm);
16822 }
16823 }
16824
16825 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
16826 do { \
16827 if (unify (TP, TA, P, A, S, EP)) \
16828 return 1; \
16829 } while (0);
16830
16831 /* Unifies the remaining arguments in PACKED_ARGS with the pack
16832 expansion at the end of PACKED_PARMS. Returns 0 if the type
16833 deduction succeeds, 1 otherwise. STRICT is the same as in
16834 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
16835 call argument list. We'll need to adjust the arguments to make them
16836 types. SUBR tells us if this is from a recursive call to
16837 type_unification_real, or for comparing two template argument
16838 lists. */
16839
16840 static int
16841 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
16842 tree packed_args, unification_kind_t strict,
16843 bool subr, bool explain_p)
16844 {
16845 tree parm
16846 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
16847 tree pattern = PACK_EXPANSION_PATTERN (parm);
16848 tree pack, packs = NULL_TREE;
16849 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
16850 int len = TREE_VEC_LENGTH (packed_args);
16851
16852 /* Determine the parameter packs we will be deducing from the
16853 pattern, and record their current deductions. */
16854 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
16855 pack; pack = TREE_CHAIN (pack))
16856 {
16857 tree parm_pack = TREE_VALUE (pack);
16858 int idx, level;
16859
16860 /* Determine the index and level of this parameter pack. */
16861 template_parm_level_and_index (parm_pack, &level, &idx);
16862
16863 /* Keep track of the parameter packs and their corresponding
16864 argument packs. */
16865 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
16866 TREE_TYPE (packs) = make_tree_vec (len - start);
16867 }
16868
16869 /* Loop through all of the arguments that have not yet been
16870 unified and unify each with the pattern. */
16871 for (i = start; i < len; i++)
16872 {
16873 tree parm;
16874 bool any_explicit = false;
16875 tree arg = TREE_VEC_ELT (packed_args, i);
16876
16877 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16878 or the element of its argument pack at the current index if
16879 this argument was explicitly specified. */
16880 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16881 {
16882 int idx, level;
16883 tree arg, pargs;
16884 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16885
16886 arg = NULL_TREE;
16887 if (TREE_VALUE (pack)
16888 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
16889 && (i - start < TREE_VEC_LENGTH (pargs)))
16890 {
16891 any_explicit = true;
16892 arg = TREE_VEC_ELT (pargs, i - start);
16893 }
16894 TMPL_ARG (targs, level, idx) = arg;
16895 }
16896
16897 /* If we had explicit template arguments, substitute them into the
16898 pattern before deduction. */
16899 if (any_explicit)
16900 {
16901 /* Some arguments might still be unspecified or dependent. */
16902 bool dependent;
16903 ++processing_template_decl;
16904 dependent = any_dependent_template_arguments_p (targs);
16905 if (!dependent)
16906 --processing_template_decl;
16907 parm = tsubst (pattern, targs,
16908 explain_p ? tf_warning_or_error : tf_none,
16909 NULL_TREE);
16910 if (dependent)
16911 --processing_template_decl;
16912 if (parm == error_mark_node)
16913 return 1;
16914 }
16915 else
16916 parm = pattern;
16917
16918 /* Unify the pattern with the current argument. */
16919 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16920 LOOKUP_IMPLICIT, explain_p))
16921 return 1;
16922
16923 /* For each parameter pack, collect the deduced value. */
16924 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16925 {
16926 int idx, level;
16927 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16928
16929 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16930 TMPL_ARG (targs, level, idx);
16931 }
16932 }
16933
16934 /* Verify that the results of unification with the parameter packs
16935 produce results consistent with what we've seen before, and make
16936 the deduced argument packs available. */
16937 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16938 {
16939 tree old_pack = TREE_VALUE (pack);
16940 tree new_args = TREE_TYPE (pack);
16941 int i, len = TREE_VEC_LENGTH (new_args);
16942 int idx, level;
16943 bool nondeduced_p = false;
16944
16945 /* By default keep the original deduced argument pack.
16946 If necessary, more specific code is going to update the
16947 resulting deduced argument later down in this function. */
16948 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16949 TMPL_ARG (targs, level, idx) = old_pack;
16950
16951 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16952 actually deduce anything. */
16953 for (i = 0; i < len && !nondeduced_p; ++i)
16954 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16955 nondeduced_p = true;
16956 if (nondeduced_p)
16957 continue;
16958
16959 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16960 {
16961 /* If we had fewer function args than explicit template args,
16962 just use the explicits. */
16963 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16964 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16965 if (len < explicit_len)
16966 new_args = explicit_args;
16967 }
16968
16969 if (!old_pack)
16970 {
16971 tree result;
16972 /* Build the deduced *_ARGUMENT_PACK. */
16973 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16974 {
16975 result = make_node (NONTYPE_ARGUMENT_PACK);
16976 TREE_TYPE (result) =
16977 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16978 TREE_CONSTANT (result) = 1;
16979 }
16980 else
16981 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16982
16983 SET_ARGUMENT_PACK_ARGS (result, new_args);
16984
16985 /* Note the deduced argument packs for this parameter
16986 pack. */
16987 TMPL_ARG (targs, level, idx) = result;
16988 }
16989 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16990 && (ARGUMENT_PACK_ARGS (old_pack)
16991 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16992 {
16993 /* We only had the explicitly-provided arguments before, but
16994 now we have a complete set of arguments. */
16995 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16996
16997 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16998 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16999 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
17000 }
17001 else
17002 {
17003 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
17004 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
17005
17006 if (!comp_template_args_with_info (old_args, new_args,
17007 &bad_old_arg, &bad_new_arg))
17008 /* Inconsistent unification of this parameter pack. */
17009 return unify_parameter_pack_inconsistent (explain_p,
17010 bad_old_arg,
17011 bad_new_arg);
17012 }
17013 }
17014
17015 return unify_success (explain_p);
17016 }
17017
17018 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
17019 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
17020 parameters and return value are as for unify. */
17021
17022 static int
17023 unify_array_domain (tree tparms, tree targs,
17024 tree parm_dom, tree arg_dom,
17025 bool explain_p)
17026 {
17027 tree parm_max;
17028 tree arg_max;
17029 bool parm_cst;
17030 bool arg_cst;
17031
17032 /* Our representation of array types uses "N - 1" as the
17033 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
17034 not an integer constant. We cannot unify arbitrarily
17035 complex expressions, so we eliminate the MINUS_EXPRs
17036 here. */
17037 parm_max = TYPE_MAX_VALUE (parm_dom);
17038 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
17039 if (!parm_cst)
17040 {
17041 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
17042 parm_max = TREE_OPERAND (parm_max, 0);
17043 }
17044 arg_max = TYPE_MAX_VALUE (arg_dom);
17045 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
17046 if (!arg_cst)
17047 {
17048 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
17049 trying to unify the type of a variable with the type
17050 of a template parameter. For example:
17051
17052 template <unsigned int N>
17053 void f (char (&) [N]);
17054 int g();
17055 void h(int i) {
17056 char a[g(i)];
17057 f(a);
17058 }
17059
17060 Here, the type of the ARG will be "int [g(i)]", and
17061 may be a SAVE_EXPR, etc. */
17062 if (TREE_CODE (arg_max) != MINUS_EXPR)
17063 return unify_vla_arg (explain_p, arg_dom);
17064 arg_max = TREE_OPERAND (arg_max, 0);
17065 }
17066
17067 /* If only one of the bounds used a MINUS_EXPR, compensate
17068 by adding one to the other bound. */
17069 if (parm_cst && !arg_cst)
17070 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
17071 integer_type_node,
17072 parm_max,
17073 integer_one_node);
17074 else if (arg_cst && !parm_cst)
17075 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
17076 integer_type_node,
17077 arg_max,
17078 integer_one_node);
17079
17080 return unify (tparms, targs, parm_max, arg_max,
17081 UNIFY_ALLOW_INTEGER, explain_p);
17082 }
17083
17084 /* Deduce the value of template parameters. TPARMS is the (innermost)
17085 set of template parameters to a template. TARGS is the bindings
17086 for those template parameters, as determined thus far; TARGS may
17087 include template arguments for outer levels of template parameters
17088 as well. PARM is a parameter to a template function, or a
17089 subcomponent of that parameter; ARG is the corresponding argument.
17090 This function attempts to match PARM with ARG in a manner
17091 consistent with the existing assignments in TARGS. If more values
17092 are deduced, then TARGS is updated.
17093
17094 Returns 0 if the type deduction succeeds, 1 otherwise. The
17095 parameter STRICT is a bitwise or of the following flags:
17096
17097 UNIFY_ALLOW_NONE:
17098 Require an exact match between PARM and ARG.
17099 UNIFY_ALLOW_MORE_CV_QUAL:
17100 Allow the deduced ARG to be more cv-qualified (by qualification
17101 conversion) than ARG.
17102 UNIFY_ALLOW_LESS_CV_QUAL:
17103 Allow the deduced ARG to be less cv-qualified than ARG.
17104 UNIFY_ALLOW_DERIVED:
17105 Allow the deduced ARG to be a template base class of ARG,
17106 or a pointer to a template base class of the type pointed to by
17107 ARG.
17108 UNIFY_ALLOW_INTEGER:
17109 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
17110 case for more information.
17111 UNIFY_ALLOW_OUTER_LEVEL:
17112 This is the outermost level of a deduction. Used to determine validity
17113 of qualification conversions. A valid qualification conversion must
17114 have const qualified pointers leading up to the inner type which
17115 requires additional CV quals, except at the outer level, where const
17116 is not required [conv.qual]. It would be normal to set this flag in
17117 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
17118 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
17119 This is the outermost level of a deduction, and PARM can be more CV
17120 qualified at this point.
17121 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
17122 This is the outermost level of a deduction, and PARM can be less CV
17123 qualified at this point. */
17124
17125 static int
17126 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
17127 bool explain_p)
17128 {
17129 int idx;
17130 tree targ;
17131 tree tparm;
17132 int strict_in = strict;
17133
17134 /* I don't think this will do the right thing with respect to types.
17135 But the only case I've seen it in so far has been array bounds, where
17136 signedness is the only information lost, and I think that will be
17137 okay. */
17138 while (TREE_CODE (parm) == NOP_EXPR)
17139 parm = TREE_OPERAND (parm, 0);
17140
17141 if (arg == error_mark_node)
17142 return unify_invalid (explain_p);
17143 if (arg == unknown_type_node
17144 || arg == init_list_type_node)
17145 /* We can't deduce anything from this, but we might get all the
17146 template args from other function args. */
17147 return unify_success (explain_p);
17148
17149 /* If PARM uses template parameters, then we can't bail out here,
17150 even if ARG == PARM, since we won't record unifications for the
17151 template parameters. We might need them if we're trying to
17152 figure out which of two things is more specialized. */
17153 if (arg == parm && !uses_template_parms (parm))
17154 return unify_success (explain_p);
17155
17156 /* Handle init lists early, so the rest of the function can assume
17157 we're dealing with a type. */
17158 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
17159 {
17160 tree elt, elttype;
17161 unsigned i;
17162 tree orig_parm = parm;
17163
17164 /* Replace T with std::initializer_list<T> for deduction. */
17165 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17166 && flag_deduce_init_list)
17167 parm = listify (parm);
17168
17169 if (!is_std_init_list (parm)
17170 && TREE_CODE (parm) != ARRAY_TYPE)
17171 /* We can only deduce from an initializer list argument if the
17172 parameter is std::initializer_list or an array; otherwise this
17173 is a non-deduced context. */
17174 return unify_success (explain_p);
17175
17176 if (TREE_CODE (parm) == ARRAY_TYPE)
17177 elttype = TREE_TYPE (parm);
17178 else
17179 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
17180
17181 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
17182 {
17183 int elt_strict = strict;
17184
17185 if (elt == error_mark_node)
17186 return unify_invalid (explain_p);
17187
17188 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
17189 {
17190 tree type = TREE_TYPE (elt);
17191 /* It should only be possible to get here for a call. */
17192 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
17193 elt_strict |= maybe_adjust_types_for_deduction
17194 (DEDUCE_CALL, &elttype, &type, elt);
17195 elt = type;
17196 }
17197
17198 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
17199 explain_p);
17200 }
17201
17202 if (TREE_CODE (parm) == ARRAY_TYPE)
17203 {
17204 /* Also deduce from the length of the initializer list. */
17205 tree max = size_int (CONSTRUCTOR_NELTS (arg));
17206 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
17207 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17208 idx, explain_p);
17209 }
17210
17211 /* If the std::initializer_list<T> deduction worked, replace the
17212 deduced A with std::initializer_list<A>. */
17213 if (orig_parm != parm)
17214 {
17215 idx = TEMPLATE_TYPE_IDX (orig_parm);
17216 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17217 targ = listify (targ);
17218 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
17219 }
17220 return unify_success (explain_p);
17221 }
17222
17223 /* Immediately reject some pairs that won't unify because of
17224 cv-qualification mismatches. */
17225 if (TREE_CODE (arg) == TREE_CODE (parm)
17226 && TYPE_P (arg)
17227 /* It is the elements of the array which hold the cv quals of an array
17228 type, and the elements might be template type parms. We'll check
17229 when we recurse. */
17230 && TREE_CODE (arg) != ARRAY_TYPE
17231 /* We check the cv-qualifiers when unifying with template type
17232 parameters below. We want to allow ARG `const T' to unify with
17233 PARM `T' for example, when computing which of two templates
17234 is more specialized, for example. */
17235 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
17236 && !check_cv_quals_for_unify (strict_in, arg, parm))
17237 return unify_cv_qual_mismatch (explain_p, parm, arg);
17238
17239 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
17240 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
17241 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
17242 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
17243 strict &= ~UNIFY_ALLOW_DERIVED;
17244 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
17245 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
17246
17247 switch (TREE_CODE (parm))
17248 {
17249 case TYPENAME_TYPE:
17250 case SCOPE_REF:
17251 case UNBOUND_CLASS_TEMPLATE:
17252 /* In a type which contains a nested-name-specifier, template
17253 argument values cannot be deduced for template parameters used
17254 within the nested-name-specifier. */
17255 return unify_success (explain_p);
17256
17257 case TEMPLATE_TYPE_PARM:
17258 case TEMPLATE_TEMPLATE_PARM:
17259 case BOUND_TEMPLATE_TEMPLATE_PARM:
17260 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17261 if (tparm == error_mark_node)
17262 return unify_invalid (explain_p);
17263
17264 if (TEMPLATE_TYPE_LEVEL (parm)
17265 != template_decl_level (tparm))
17266 /* The PARM is not one we're trying to unify. Just check
17267 to see if it matches ARG. */
17268 {
17269 if (TREE_CODE (arg) == TREE_CODE (parm)
17270 && (is_auto (parm) ? is_auto (arg)
17271 : same_type_p (parm, arg)))
17272 return unify_success (explain_p);
17273 else
17274 return unify_type_mismatch (explain_p, parm, arg);
17275 }
17276 idx = TEMPLATE_TYPE_IDX (parm);
17277 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17278 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
17279 if (tparm == error_mark_node)
17280 return unify_invalid (explain_p);
17281
17282 /* Check for mixed types and values. */
17283 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
17284 && TREE_CODE (tparm) != TYPE_DECL)
17285 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17286 && TREE_CODE (tparm) != TEMPLATE_DECL))
17287 gcc_unreachable ();
17288
17289 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17290 {
17291 /* ARG must be constructed from a template class or a template
17292 template parameter. */
17293 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
17294 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
17295 return unify_template_deduction_failure (explain_p, parm, arg);
17296
17297 {
17298 tree parmvec = TYPE_TI_ARGS (parm);
17299 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
17300 tree full_argvec = add_to_template_args (targs, argvec);
17301 tree parm_parms
17302 = DECL_INNERMOST_TEMPLATE_PARMS
17303 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
17304 int i, len;
17305 int parm_variadic_p = 0;
17306
17307 /* The resolution to DR150 makes clear that default
17308 arguments for an N-argument may not be used to bind T
17309 to a template template parameter with fewer than N
17310 parameters. It is not safe to permit the binding of
17311 default arguments as an extension, as that may change
17312 the meaning of a conforming program. Consider:
17313
17314 struct Dense { static const unsigned int dim = 1; };
17315
17316 template <template <typename> class View,
17317 typename Block>
17318 void operator+(float, View<Block> const&);
17319
17320 template <typename Block,
17321 unsigned int Dim = Block::dim>
17322 struct Lvalue_proxy { operator float() const; };
17323
17324 void
17325 test_1d (void) {
17326 Lvalue_proxy<Dense> p;
17327 float b;
17328 b + p;
17329 }
17330
17331 Here, if Lvalue_proxy is permitted to bind to View, then
17332 the global operator+ will be used; if they are not, the
17333 Lvalue_proxy will be converted to float. */
17334 if (coerce_template_parms (parm_parms,
17335 full_argvec,
17336 TYPE_TI_TEMPLATE (parm),
17337 (explain_p
17338 ? tf_warning_or_error
17339 : tf_none),
17340 /*require_all_args=*/true,
17341 /*use_default_args=*/false)
17342 == error_mark_node)
17343 return 1;
17344
17345 /* Deduce arguments T, i from TT<T> or TT<i>.
17346 We check each element of PARMVEC and ARGVEC individually
17347 rather than the whole TREE_VEC since they can have
17348 different number of elements. */
17349
17350 parmvec = expand_template_argument_pack (parmvec);
17351 argvec = expand_template_argument_pack (argvec);
17352
17353 len = TREE_VEC_LENGTH (parmvec);
17354
17355 /* Check if the parameters end in a pack, making them
17356 variadic. */
17357 if (len > 0
17358 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
17359 parm_variadic_p = 1;
17360
17361 for (i = 0; i < len - parm_variadic_p; ++i)
17362 /* If the template argument list of P contains a pack
17363 expansion that is not the last template argument, the
17364 entire template argument list is a non-deduced
17365 context. */
17366 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
17367 return unify_success (explain_p);
17368
17369 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
17370 return unify_too_few_arguments (explain_p,
17371 TREE_VEC_LENGTH (argvec), len);
17372
17373 for (i = 0; i < len - parm_variadic_p; ++i)
17374 {
17375 RECUR_AND_CHECK_FAILURE (tparms, targs,
17376 TREE_VEC_ELT (parmvec, i),
17377 TREE_VEC_ELT (argvec, i),
17378 UNIFY_ALLOW_NONE, explain_p);
17379 }
17380
17381 if (parm_variadic_p
17382 && unify_pack_expansion (tparms, targs,
17383 parmvec, argvec,
17384 DEDUCE_EXACT,
17385 /*subr=*/true, explain_p))
17386 return 1;
17387 }
17388 arg = TYPE_TI_TEMPLATE (arg);
17389
17390 /* Fall through to deduce template name. */
17391 }
17392
17393 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
17394 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
17395 {
17396 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
17397
17398 /* Simple cases: Value already set, does match or doesn't. */
17399 if (targ != NULL_TREE && template_args_equal (targ, arg))
17400 return unify_success (explain_p);
17401 else if (targ)
17402 return unify_inconsistency (explain_p, parm, targ, arg);
17403 }
17404 else
17405 {
17406 /* If PARM is `const T' and ARG is only `int', we don't have
17407 a match unless we are allowing additional qualification.
17408 If ARG is `const int' and PARM is just `T' that's OK;
17409 that binds `const int' to `T'. */
17410 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
17411 arg, parm))
17412 return unify_cv_qual_mismatch (explain_p, parm, arg);
17413
17414 /* Consider the case where ARG is `const volatile int' and
17415 PARM is `const T'. Then, T should be `volatile int'. */
17416 arg = cp_build_qualified_type_real
17417 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
17418 if (arg == error_mark_node)
17419 return unify_invalid (explain_p);
17420
17421 /* Simple cases: Value already set, does match or doesn't. */
17422 if (targ != NULL_TREE && same_type_p (targ, arg))
17423 return unify_success (explain_p);
17424 else if (targ)
17425 return unify_inconsistency (explain_p, parm, targ, arg);
17426
17427 /* Make sure that ARG is not a variable-sized array. (Note
17428 that were talking about variable-sized arrays (like
17429 `int[n]'), rather than arrays of unknown size (like
17430 `int[]').) We'll get very confused by such a type since
17431 the bound of the array is not constant, and therefore
17432 not mangleable. Besides, such types are not allowed in
17433 ISO C++, so we can do as we please here. We do allow
17434 them for 'auto' deduction, since that isn't ABI-exposed. */
17435 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
17436 return unify_vla_arg (explain_p, arg);
17437
17438 /* Strip typedefs as in convert_template_argument. */
17439 arg = canonicalize_type_argument (arg, tf_none);
17440 }
17441
17442 /* If ARG is a parameter pack or an expansion, we cannot unify
17443 against it unless PARM is also a parameter pack. */
17444 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17445 && !template_parameter_pack_p (parm))
17446 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17447
17448 /* If the argument deduction results is a METHOD_TYPE,
17449 then there is a problem.
17450 METHOD_TYPE doesn't map to any real C++ type the result of
17451 the deduction can not be of that type. */
17452 if (TREE_CODE (arg) == METHOD_TYPE)
17453 return unify_method_type_error (explain_p, arg);
17454
17455 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17456 return unify_success (explain_p);
17457
17458 case TEMPLATE_PARM_INDEX:
17459 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
17460 if (tparm == error_mark_node)
17461 return unify_invalid (explain_p);
17462
17463 if (TEMPLATE_PARM_LEVEL (parm)
17464 != template_decl_level (tparm))
17465 {
17466 /* The PARM is not one we're trying to unify. Just check
17467 to see if it matches ARG. */
17468 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
17469 && cp_tree_equal (parm, arg));
17470 if (result)
17471 unify_expression_unequal (explain_p, parm, arg);
17472 return result;
17473 }
17474
17475 idx = TEMPLATE_PARM_IDX (parm);
17476 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
17477
17478 if (targ)
17479 {
17480 int x = !cp_tree_equal (targ, arg);
17481 if (x)
17482 unify_inconsistency (explain_p, parm, targ, arg);
17483 return x;
17484 }
17485
17486 /* [temp.deduct.type] If, in the declaration of a function template
17487 with a non-type template-parameter, the non-type
17488 template-parameter is used in an expression in the function
17489 parameter-list and, if the corresponding template-argument is
17490 deduced, the template-argument type shall match the type of the
17491 template-parameter exactly, except that a template-argument
17492 deduced from an array bound may be of any integral type.
17493 The non-type parameter might use already deduced type parameters. */
17494 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
17495 if (!TREE_TYPE (arg))
17496 /* Template-parameter dependent expression. Just accept it for now.
17497 It will later be processed in convert_template_argument. */
17498 ;
17499 else if (same_type_p (TREE_TYPE (arg), tparm))
17500 /* OK */;
17501 else if ((strict & UNIFY_ALLOW_INTEGER)
17502 && CP_INTEGRAL_TYPE_P (tparm))
17503 /* Convert the ARG to the type of PARM; the deduced non-type
17504 template argument must exactly match the types of the
17505 corresponding parameter. */
17506 arg = fold (build_nop (tparm, arg));
17507 else if (uses_template_parms (tparm))
17508 /* We haven't deduced the type of this parameter yet. Try again
17509 later. */
17510 return unify_success (explain_p);
17511 else
17512 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
17513
17514 /* If ARG is a parameter pack or an expansion, we cannot unify
17515 against it unless PARM is also a parameter pack. */
17516 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
17517 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
17518 return unify_parameter_pack_mismatch (explain_p, parm, arg);
17519
17520 arg = strip_typedefs_expr (arg);
17521 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
17522 return unify_success (explain_p);
17523
17524 case PTRMEM_CST:
17525 {
17526 /* A pointer-to-member constant can be unified only with
17527 another constant. */
17528 if (TREE_CODE (arg) != PTRMEM_CST)
17529 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
17530
17531 /* Just unify the class member. It would be useless (and possibly
17532 wrong, depending on the strict flags) to unify also
17533 PTRMEM_CST_CLASS, because we want to be sure that both parm and
17534 arg refer to the same variable, even if through different
17535 classes. For instance:
17536
17537 struct A { int x; };
17538 struct B : A { };
17539
17540 Unification of &A::x and &B::x must succeed. */
17541 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
17542 PTRMEM_CST_MEMBER (arg), strict, explain_p);
17543 }
17544
17545 case POINTER_TYPE:
17546 {
17547 if (!TYPE_PTR_P (arg))
17548 return unify_type_mismatch (explain_p, parm, arg);
17549
17550 /* [temp.deduct.call]
17551
17552 A can be another pointer or pointer to member type that can
17553 be converted to the deduced A via a qualification
17554 conversion (_conv.qual_).
17555
17556 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
17557 This will allow for additional cv-qualification of the
17558 pointed-to types if appropriate. */
17559
17560 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
17561 /* The derived-to-base conversion only persists through one
17562 level of pointers. */
17563 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
17564
17565 return unify (tparms, targs, TREE_TYPE (parm),
17566 TREE_TYPE (arg), strict, explain_p);
17567 }
17568
17569 case REFERENCE_TYPE:
17570 if (TREE_CODE (arg) != REFERENCE_TYPE)
17571 return unify_type_mismatch (explain_p, parm, arg);
17572 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17573 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17574
17575 case ARRAY_TYPE:
17576 if (TREE_CODE (arg) != ARRAY_TYPE)
17577 return unify_type_mismatch (explain_p, parm, arg);
17578 if ((TYPE_DOMAIN (parm) == NULL_TREE)
17579 != (TYPE_DOMAIN (arg) == NULL_TREE))
17580 return unify_type_mismatch (explain_p, parm, arg);
17581 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17582 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
17583 if (TYPE_DOMAIN (parm) != NULL_TREE)
17584 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
17585 TYPE_DOMAIN (arg), explain_p);
17586 return unify_success (explain_p);
17587
17588 case REAL_TYPE:
17589 case COMPLEX_TYPE:
17590 case VECTOR_TYPE:
17591 case INTEGER_TYPE:
17592 case BOOLEAN_TYPE:
17593 case ENUMERAL_TYPE:
17594 case VOID_TYPE:
17595 case NULLPTR_TYPE:
17596 if (TREE_CODE (arg) != TREE_CODE (parm))
17597 return unify_type_mismatch (explain_p, parm, arg);
17598
17599 /* We have already checked cv-qualification at the top of the
17600 function. */
17601 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
17602 return unify_type_mismatch (explain_p, parm, arg);
17603
17604 /* As far as unification is concerned, this wins. Later checks
17605 will invalidate it if necessary. */
17606 return unify_success (explain_p);
17607
17608 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
17609 /* Type INTEGER_CST can come from ordinary constant template args. */
17610 case INTEGER_CST:
17611 while (TREE_CODE (arg) == NOP_EXPR)
17612 arg = TREE_OPERAND (arg, 0);
17613
17614 if (TREE_CODE (arg) != INTEGER_CST)
17615 return unify_template_argument_mismatch (explain_p, parm, arg);
17616 return (tree_int_cst_equal (parm, arg)
17617 ? unify_success (explain_p)
17618 : unify_template_argument_mismatch (explain_p, parm, arg));
17619
17620 case TREE_VEC:
17621 {
17622 int i, len, argslen;
17623 int parm_variadic_p = 0;
17624
17625 if (TREE_CODE (arg) != TREE_VEC)
17626 return unify_template_argument_mismatch (explain_p, parm, arg);
17627
17628 len = TREE_VEC_LENGTH (parm);
17629 argslen = TREE_VEC_LENGTH (arg);
17630
17631 /* Check for pack expansions in the parameters. */
17632 for (i = 0; i < len; ++i)
17633 {
17634 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
17635 {
17636 if (i == len - 1)
17637 /* We can unify against something with a trailing
17638 parameter pack. */
17639 parm_variadic_p = 1;
17640 else
17641 /* [temp.deduct.type]/9: If the template argument list of
17642 P contains a pack expansion that is not the last
17643 template argument, the entire template argument list
17644 is a non-deduced context. */
17645 return unify_success (explain_p);
17646 }
17647 }
17648
17649 /* If we don't have enough arguments to satisfy the parameters
17650 (not counting the pack expression at the end), or we have
17651 too many arguments for a parameter list that doesn't end in
17652 a pack expression, we can't unify. */
17653 if (parm_variadic_p
17654 ? argslen < len - parm_variadic_p
17655 : argslen != len)
17656 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
17657
17658 /* Unify all of the parameters that precede the (optional)
17659 pack expression. */
17660 for (i = 0; i < len - parm_variadic_p; ++i)
17661 {
17662 RECUR_AND_CHECK_FAILURE (tparms, targs,
17663 TREE_VEC_ELT (parm, i),
17664 TREE_VEC_ELT (arg, i),
17665 UNIFY_ALLOW_NONE, explain_p);
17666 }
17667 if (parm_variadic_p)
17668 return unify_pack_expansion (tparms, targs, parm, arg,
17669 DEDUCE_EXACT,
17670 /*subr=*/true, explain_p);
17671 return unify_success (explain_p);
17672 }
17673
17674 case RECORD_TYPE:
17675 case UNION_TYPE:
17676 if (TREE_CODE (arg) != TREE_CODE (parm))
17677 return unify_type_mismatch (explain_p, parm, arg);
17678
17679 if (TYPE_PTRMEMFUNC_P (parm))
17680 {
17681 if (!TYPE_PTRMEMFUNC_P (arg))
17682 return unify_type_mismatch (explain_p, parm, arg);
17683
17684 return unify (tparms, targs,
17685 TYPE_PTRMEMFUNC_FN_TYPE (parm),
17686 TYPE_PTRMEMFUNC_FN_TYPE (arg),
17687 strict, explain_p);
17688 }
17689
17690 if (CLASSTYPE_TEMPLATE_INFO (parm))
17691 {
17692 tree t = NULL_TREE;
17693
17694 if (strict_in & UNIFY_ALLOW_DERIVED)
17695 {
17696 /* First, we try to unify the PARM and ARG directly. */
17697 t = try_class_unification (tparms, targs,
17698 parm, arg, explain_p);
17699
17700 if (!t)
17701 {
17702 /* Fallback to the special case allowed in
17703 [temp.deduct.call]:
17704
17705 If P is a class, and P has the form
17706 template-id, then A can be a derived class of
17707 the deduced A. Likewise, if P is a pointer to
17708 a class of the form template-id, A can be a
17709 pointer to a derived class pointed to by the
17710 deduced A. */
17711 enum template_base_result r;
17712 r = get_template_base (tparms, targs, parm, arg,
17713 explain_p, &t);
17714
17715 if (!t)
17716 return unify_no_common_base (explain_p, r, parm, arg);
17717 }
17718 }
17719 else if (CLASSTYPE_TEMPLATE_INFO (arg)
17720 && (CLASSTYPE_TI_TEMPLATE (parm)
17721 == CLASSTYPE_TI_TEMPLATE (arg)))
17722 /* Perhaps PARM is something like S<U> and ARG is S<int>.
17723 Then, we should unify `int' and `U'. */
17724 t = arg;
17725 else
17726 /* There's no chance of unification succeeding. */
17727 return unify_type_mismatch (explain_p, parm, arg);
17728
17729 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
17730 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
17731 }
17732 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
17733 return unify_type_mismatch (explain_p, parm, arg);
17734 return unify_success (explain_p);
17735
17736 case METHOD_TYPE:
17737 case FUNCTION_TYPE:
17738 {
17739 unsigned int nargs;
17740 tree *args;
17741 tree a;
17742 unsigned int i;
17743
17744 if (TREE_CODE (arg) != TREE_CODE (parm))
17745 return unify_type_mismatch (explain_p, parm, arg);
17746
17747 /* CV qualifications for methods can never be deduced, they must
17748 match exactly. We need to check them explicitly here,
17749 because type_unification_real treats them as any other
17750 cv-qualified parameter. */
17751 if (TREE_CODE (parm) == METHOD_TYPE
17752 && (!check_cv_quals_for_unify
17753 (UNIFY_ALLOW_NONE,
17754 class_of_this_parm (arg),
17755 class_of_this_parm (parm))))
17756 return unify_cv_qual_mismatch (explain_p, parm, arg);
17757
17758 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
17759 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
17760
17761 nargs = list_length (TYPE_ARG_TYPES (arg));
17762 args = XALLOCAVEC (tree, nargs);
17763 for (a = TYPE_ARG_TYPES (arg), i = 0;
17764 a != NULL_TREE && a != void_list_node;
17765 a = TREE_CHAIN (a), ++i)
17766 args[i] = TREE_VALUE (a);
17767 nargs = i;
17768
17769 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
17770 args, nargs, 1, DEDUCE_EXACT,
17771 LOOKUP_NORMAL, NULL, explain_p);
17772 }
17773
17774 case OFFSET_TYPE:
17775 /* Unify a pointer to member with a pointer to member function, which
17776 deduces the type of the member as a function type. */
17777 if (TYPE_PTRMEMFUNC_P (arg))
17778 {
17779 /* Check top-level cv qualifiers */
17780 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
17781 return unify_cv_qual_mismatch (explain_p, parm, arg);
17782
17783 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17784 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
17785 UNIFY_ALLOW_NONE, explain_p);
17786
17787 /* Determine the type of the function we are unifying against. */
17788 tree fntype = static_fn_type (arg);
17789
17790 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
17791 }
17792
17793 if (TREE_CODE (arg) != OFFSET_TYPE)
17794 return unify_type_mismatch (explain_p, parm, arg);
17795 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
17796 TYPE_OFFSET_BASETYPE (arg),
17797 UNIFY_ALLOW_NONE, explain_p);
17798 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
17799 strict, explain_p);
17800
17801 case CONST_DECL:
17802 if (DECL_TEMPLATE_PARM_P (parm))
17803 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
17804 if (arg != integral_constant_value (parm))
17805 return unify_template_argument_mismatch (explain_p, parm, arg);
17806 return unify_success (explain_p);
17807
17808 case FIELD_DECL:
17809 case TEMPLATE_DECL:
17810 /* Matched cases are handled by the ARG == PARM test above. */
17811 return unify_template_argument_mismatch (explain_p, parm, arg);
17812
17813 case VAR_DECL:
17814 /* A non-type template parameter that is a variable should be a
17815 an integral constant, in which case, it whould have been
17816 folded into its (constant) value. So we should not be getting
17817 a variable here. */
17818 gcc_unreachable ();
17819
17820 case TYPE_ARGUMENT_PACK:
17821 case NONTYPE_ARGUMENT_PACK:
17822 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
17823 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
17824
17825 case TYPEOF_TYPE:
17826 case DECLTYPE_TYPE:
17827 case UNDERLYING_TYPE:
17828 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
17829 or UNDERLYING_TYPE nodes. */
17830 return unify_success (explain_p);
17831
17832 case ERROR_MARK:
17833 /* Unification fails if we hit an error node. */
17834 return unify_invalid (explain_p);
17835
17836 default:
17837 /* An unresolved overload is a nondeduced context. */
17838 if (is_overloaded_fn (parm) || type_unknown_p (parm))
17839 return unify_success (explain_p);
17840 gcc_assert (EXPR_P (parm));
17841
17842 /* We must be looking at an expression. This can happen with
17843 something like:
17844
17845 template <int I>
17846 void foo(S<I>, S<I + 2>);
17847
17848 This is a "nondeduced context":
17849
17850 [deduct.type]
17851
17852 The nondeduced contexts are:
17853
17854 --A type that is a template-id in which one or more of
17855 the template-arguments is an expression that references
17856 a template-parameter.
17857
17858 In these cases, we assume deduction succeeded, but don't
17859 actually infer any unifications. */
17860
17861 if (!uses_template_parms (parm)
17862 && !template_args_equal (parm, arg))
17863 return unify_expression_unequal (explain_p, parm, arg);
17864 else
17865 return unify_success (explain_p);
17866 }
17867 }
17868 #undef RECUR_AND_CHECK_FAILURE
17869 \f
17870 /* Note that DECL can be defined in this translation unit, if
17871 required. */
17872
17873 static void
17874 mark_definable (tree decl)
17875 {
17876 tree clone;
17877 DECL_NOT_REALLY_EXTERN (decl) = 1;
17878 FOR_EACH_CLONE (clone, decl)
17879 DECL_NOT_REALLY_EXTERN (clone) = 1;
17880 }
17881
17882 /* Called if RESULT is explicitly instantiated, or is a member of an
17883 explicitly instantiated class. */
17884
17885 void
17886 mark_decl_instantiated (tree result, int extern_p)
17887 {
17888 SET_DECL_EXPLICIT_INSTANTIATION (result);
17889
17890 /* If this entity has already been written out, it's too late to
17891 make any modifications. */
17892 if (TREE_ASM_WRITTEN (result))
17893 return;
17894
17895 /* For anonymous namespace we don't need to do anything. */
17896 if (decl_anon_ns_mem_p (result))
17897 {
17898 gcc_assert (!TREE_PUBLIC (result));
17899 return;
17900 }
17901
17902 if (TREE_CODE (result) != FUNCTION_DECL)
17903 /* The TREE_PUBLIC flag for function declarations will have been
17904 set correctly by tsubst. */
17905 TREE_PUBLIC (result) = 1;
17906
17907 /* This might have been set by an earlier implicit instantiation. */
17908 DECL_COMDAT (result) = 0;
17909
17910 if (extern_p)
17911 DECL_NOT_REALLY_EXTERN (result) = 0;
17912 else
17913 {
17914 mark_definable (result);
17915 /* Always make artificials weak. */
17916 if (DECL_ARTIFICIAL (result) && flag_weak)
17917 comdat_linkage (result);
17918 /* For WIN32 we also want to put explicit instantiations in
17919 linkonce sections. */
17920 else if (TREE_PUBLIC (result))
17921 maybe_make_one_only (result);
17922 }
17923
17924 /* If EXTERN_P, then this function will not be emitted -- unless
17925 followed by an explicit instantiation, at which point its linkage
17926 will be adjusted. If !EXTERN_P, then this function will be
17927 emitted here. In neither circumstance do we want
17928 import_export_decl to adjust the linkage. */
17929 DECL_INTERFACE_KNOWN (result) = 1;
17930 }
17931
17932 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17933 important template arguments. If any are missing, we check whether
17934 they're important by using error_mark_node for substituting into any
17935 args that were used for partial ordering (the ones between ARGS and END)
17936 and seeing if it bubbles up. */
17937
17938 static bool
17939 check_undeduced_parms (tree targs, tree args, tree end)
17940 {
17941 bool found = false;
17942 int i;
17943 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17944 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17945 {
17946 found = true;
17947 TREE_VEC_ELT (targs, i) = error_mark_node;
17948 }
17949 if (found)
17950 {
17951 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17952 if (substed == error_mark_node)
17953 return true;
17954 }
17955 return false;
17956 }
17957
17958 /* Given two function templates PAT1 and PAT2, return:
17959
17960 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17961 -1 if PAT2 is more specialized than PAT1.
17962 0 if neither is more specialized.
17963
17964 LEN indicates the number of parameters we should consider
17965 (defaulted parameters should not be considered).
17966
17967 The 1998 std underspecified function template partial ordering, and
17968 DR214 addresses the issue. We take pairs of arguments, one from
17969 each of the templates, and deduce them against each other. One of
17970 the templates will be more specialized if all the *other*
17971 template's arguments deduce against its arguments and at least one
17972 of its arguments *does* *not* deduce against the other template's
17973 corresponding argument. Deduction is done as for class templates.
17974 The arguments used in deduction have reference and top level cv
17975 qualifiers removed. Iff both arguments were originally reference
17976 types *and* deduction succeeds in both directions, an lvalue reference
17977 wins against an rvalue reference and otherwise the template
17978 with the more cv-qualified argument wins for that pairing (if
17979 neither is more cv-qualified, they both are equal). Unlike regular
17980 deduction, after all the arguments have been deduced in this way,
17981 we do *not* verify the deduced template argument values can be
17982 substituted into non-deduced contexts.
17983
17984 The logic can be a bit confusing here, because we look at deduce1 and
17985 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17986 can find template arguments for pat1 to make arg1 look like arg2, that
17987 means that arg2 is at least as specialized as arg1. */
17988
17989 int
17990 more_specialized_fn (tree pat1, tree pat2, int len)
17991 {
17992 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17993 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17994 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17995 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17996 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17997 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17998 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17999 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
18000 tree origs1, origs2;
18001 bool lose1 = false;
18002 bool lose2 = false;
18003
18004 /* Remove the this parameter from non-static member functions. If
18005 one is a non-static member function and the other is not a static
18006 member function, remove the first parameter from that function
18007 also. This situation occurs for operator functions where we
18008 locate both a member function (with this pointer) and non-member
18009 operator (with explicit first operand). */
18010 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
18011 {
18012 len--; /* LEN is the number of significant arguments for DECL1 */
18013 args1 = TREE_CHAIN (args1);
18014 if (!DECL_STATIC_FUNCTION_P (decl2))
18015 args2 = TREE_CHAIN (args2);
18016 }
18017 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
18018 {
18019 args2 = TREE_CHAIN (args2);
18020 if (!DECL_STATIC_FUNCTION_P (decl1))
18021 {
18022 len--;
18023 args1 = TREE_CHAIN (args1);
18024 }
18025 }
18026
18027 /* If only one is a conversion operator, they are unordered. */
18028 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
18029 return 0;
18030
18031 /* Consider the return type for a conversion function */
18032 if (DECL_CONV_FN_P (decl1))
18033 {
18034 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
18035 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
18036 len++;
18037 }
18038
18039 processing_template_decl++;
18040
18041 origs1 = args1;
18042 origs2 = args2;
18043
18044 while (len--
18045 /* Stop when an ellipsis is seen. */
18046 && args1 != NULL_TREE && args2 != NULL_TREE)
18047 {
18048 tree arg1 = TREE_VALUE (args1);
18049 tree arg2 = TREE_VALUE (args2);
18050 int deduce1, deduce2;
18051 int quals1 = -1;
18052 int quals2 = -1;
18053 int ref1 = 0;
18054 int ref2 = 0;
18055
18056 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18057 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18058 {
18059 /* When both arguments are pack expansions, we need only
18060 unify the patterns themselves. */
18061 arg1 = PACK_EXPANSION_PATTERN (arg1);
18062 arg2 = PACK_EXPANSION_PATTERN (arg2);
18063
18064 /* This is the last comparison we need to do. */
18065 len = 0;
18066 }
18067
18068 if (TREE_CODE (arg1) == REFERENCE_TYPE)
18069 {
18070 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
18071 arg1 = TREE_TYPE (arg1);
18072 quals1 = cp_type_quals (arg1);
18073 }
18074
18075 if (TREE_CODE (arg2) == REFERENCE_TYPE)
18076 {
18077 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
18078 arg2 = TREE_TYPE (arg2);
18079 quals2 = cp_type_quals (arg2);
18080 }
18081
18082 arg1 = TYPE_MAIN_VARIANT (arg1);
18083 arg2 = TYPE_MAIN_VARIANT (arg2);
18084
18085 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
18086 {
18087 int i, len2 = list_length (args2);
18088 tree parmvec = make_tree_vec (1);
18089 tree argvec = make_tree_vec (len2);
18090 tree ta = args2;
18091
18092 /* Setup the parameter vector, which contains only ARG1. */
18093 TREE_VEC_ELT (parmvec, 0) = arg1;
18094
18095 /* Setup the argument vector, which contains the remaining
18096 arguments. */
18097 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
18098 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18099
18100 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
18101 argvec, DEDUCE_EXACT,
18102 /*subr=*/true, /*explain_p=*/false)
18103 == 0);
18104
18105 /* We cannot deduce in the other direction, because ARG1 is
18106 a pack expansion but ARG2 is not. */
18107 deduce2 = 0;
18108 }
18109 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18110 {
18111 int i, len1 = list_length (args1);
18112 tree parmvec = make_tree_vec (1);
18113 tree argvec = make_tree_vec (len1);
18114 tree ta = args1;
18115
18116 /* Setup the parameter vector, which contains only ARG1. */
18117 TREE_VEC_ELT (parmvec, 0) = arg2;
18118
18119 /* Setup the argument vector, which contains the remaining
18120 arguments. */
18121 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
18122 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
18123
18124 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
18125 argvec, DEDUCE_EXACT,
18126 /*subr=*/true, /*explain_p=*/false)
18127 == 0);
18128
18129 /* We cannot deduce in the other direction, because ARG2 is
18130 a pack expansion but ARG1 is not.*/
18131 deduce1 = 0;
18132 }
18133
18134 else
18135 {
18136 /* The normal case, where neither argument is a pack
18137 expansion. */
18138 deduce1 = (unify (tparms1, targs1, arg1, arg2,
18139 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18140 == 0);
18141 deduce2 = (unify (tparms2, targs2, arg2, arg1,
18142 UNIFY_ALLOW_NONE, /*explain_p=*/false)
18143 == 0);
18144 }
18145
18146 /* If we couldn't deduce arguments for tparms1 to make arg1 match
18147 arg2, then arg2 is not as specialized as arg1. */
18148 if (!deduce1)
18149 lose2 = true;
18150 if (!deduce2)
18151 lose1 = true;
18152
18153 /* "If, for a given type, deduction succeeds in both directions
18154 (i.e., the types are identical after the transformations above)
18155 and both P and A were reference types (before being replaced with
18156 the type referred to above):
18157 - if the type from the argument template was an lvalue reference and
18158 the type from the parameter template was not, the argument type is
18159 considered to be more specialized than the other; otherwise,
18160 - if the type from the argument template is more cv-qualified
18161 than the type from the parameter template (as described above),
18162 the argument type is considered to be more specialized than the other;
18163 otherwise,
18164 - neither type is more specialized than the other." */
18165
18166 if (deduce1 && deduce2)
18167 {
18168 if (ref1 && ref2 && ref1 != ref2)
18169 {
18170 if (ref1 > ref2)
18171 lose1 = true;
18172 else
18173 lose2 = true;
18174 }
18175 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
18176 {
18177 if ((quals1 & quals2) == quals2)
18178 lose2 = true;
18179 if ((quals1 & quals2) == quals1)
18180 lose1 = true;
18181 }
18182 }
18183
18184 if (lose1 && lose2)
18185 /* We've failed to deduce something in either direction.
18186 These must be unordered. */
18187 break;
18188
18189 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
18190 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
18191 /* We have already processed all of the arguments in our
18192 handing of the pack expansion type. */
18193 len = 0;
18194
18195 args1 = TREE_CHAIN (args1);
18196 args2 = TREE_CHAIN (args2);
18197 }
18198
18199 /* "In most cases, all template parameters must have values in order for
18200 deduction to succeed, but for partial ordering purposes a template
18201 parameter may remain without a value provided it is not used in the
18202 types being used for partial ordering."
18203
18204 Thus, if we are missing any of the targs1 we need to substitute into
18205 origs1, then pat2 is not as specialized as pat1. This can happen when
18206 there is a nondeduced context. */
18207 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
18208 lose2 = true;
18209 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
18210 lose1 = true;
18211
18212 processing_template_decl--;
18213
18214 /* All things being equal, if the next argument is a pack expansion
18215 for one function but not for the other, prefer the
18216 non-variadic function. FIXME this is bogus; see c++/41958. */
18217 if (lose1 == lose2
18218 && args1 && TREE_VALUE (args1)
18219 && args2 && TREE_VALUE (args2))
18220 {
18221 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
18222 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
18223 }
18224
18225 if (lose1 == lose2)
18226 return 0;
18227 else if (!lose1)
18228 return 1;
18229 else
18230 return -1;
18231 }
18232
18233 /* Determine which of two partial specializations of MAIN_TMPL is more
18234 specialized.
18235
18236 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
18237 to the first partial specialization. The TREE_VALUE is the
18238 innermost set of template parameters for the partial
18239 specialization. PAT2 is similar, but for the second template.
18240
18241 Return 1 if the first partial specialization is more specialized;
18242 -1 if the second is more specialized; 0 if neither is more
18243 specialized.
18244
18245 See [temp.class.order] for information about determining which of
18246 two templates is more specialized. */
18247
18248 static int
18249 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
18250 {
18251 tree targs;
18252 tree tmpl1, tmpl2;
18253 int winner = 0;
18254 bool any_deductions = false;
18255
18256 tmpl1 = TREE_TYPE (pat1);
18257 tmpl2 = TREE_TYPE (pat2);
18258
18259 /* Just like what happens for functions, if we are ordering between
18260 different class template specializations, we may encounter dependent
18261 types in the arguments, and we need our dependency check functions
18262 to behave correctly. */
18263 ++processing_template_decl;
18264 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
18265 CLASSTYPE_TI_ARGS (tmpl1),
18266 CLASSTYPE_TI_ARGS (tmpl2));
18267 if (targs)
18268 {
18269 --winner;
18270 any_deductions = true;
18271 }
18272
18273 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
18274 CLASSTYPE_TI_ARGS (tmpl2),
18275 CLASSTYPE_TI_ARGS (tmpl1));
18276 if (targs)
18277 {
18278 ++winner;
18279 any_deductions = true;
18280 }
18281 --processing_template_decl;
18282
18283 /* In the case of a tie where at least one of the class templates
18284 has a parameter pack at the end, the template with the most
18285 non-packed parameters wins. */
18286 if (winner == 0
18287 && any_deductions
18288 && (template_args_variadic_p (TREE_PURPOSE (pat1))
18289 || template_args_variadic_p (TREE_PURPOSE (pat2))))
18290 {
18291 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
18292 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
18293 int len1 = TREE_VEC_LENGTH (args1);
18294 int len2 = TREE_VEC_LENGTH (args2);
18295
18296 /* We don't count the pack expansion at the end. */
18297 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
18298 --len1;
18299 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
18300 --len2;
18301
18302 if (len1 > len2)
18303 return 1;
18304 else if (len1 < len2)
18305 return -1;
18306 }
18307
18308 return winner;
18309 }
18310
18311 /* Return the template arguments that will produce the function signature
18312 DECL from the function template FN, with the explicit template
18313 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
18314 also match. Return NULL_TREE if no satisfactory arguments could be
18315 found. */
18316
18317 static tree
18318 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
18319 {
18320 int ntparms = DECL_NTPARMS (fn);
18321 tree targs = make_tree_vec (ntparms);
18322 tree decl_type = TREE_TYPE (decl);
18323 tree decl_arg_types;
18324 tree *args;
18325 unsigned int nargs, ix;
18326 tree arg;
18327
18328 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
18329
18330 /* Never do unification on the 'this' parameter. */
18331 decl_arg_types = skip_artificial_parms_for (decl,
18332 TYPE_ARG_TYPES (decl_type));
18333
18334 nargs = list_length (decl_arg_types);
18335 args = XALLOCAVEC (tree, nargs);
18336 for (arg = decl_arg_types, ix = 0;
18337 arg != NULL_TREE && arg != void_list_node;
18338 arg = TREE_CHAIN (arg), ++ix)
18339 args[ix] = TREE_VALUE (arg);
18340
18341 if (fn_type_unification (fn, explicit_args, targs,
18342 args, ix,
18343 (check_rettype || DECL_CONV_FN_P (fn)
18344 ? TREE_TYPE (decl_type) : NULL_TREE),
18345 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
18346 /*decltype*/false)
18347 == error_mark_node)
18348 return NULL_TREE;
18349
18350 return targs;
18351 }
18352
18353 /* Return the innermost template arguments that, when applied to a partial
18354 specialization of MAIN_TMPL whose innermost template parameters are
18355 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
18356 ARGS.
18357
18358 For example, suppose we have:
18359
18360 template <class T, class U> struct S {};
18361 template <class T> struct S<T*, int> {};
18362
18363 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
18364 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
18365 int}. The resulting vector will be {double}, indicating that `T'
18366 is bound to `double'. */
18367
18368 static tree
18369 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
18370 {
18371 int i, ntparms = TREE_VEC_LENGTH (tparms);
18372 tree deduced_args;
18373 tree innermost_deduced_args;
18374
18375 innermost_deduced_args = make_tree_vec (ntparms);
18376 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18377 {
18378 deduced_args = copy_node (args);
18379 SET_TMPL_ARGS_LEVEL (deduced_args,
18380 TMPL_ARGS_DEPTH (deduced_args),
18381 innermost_deduced_args);
18382 }
18383 else
18384 deduced_args = innermost_deduced_args;
18385
18386 if (unify (tparms, deduced_args,
18387 INNERMOST_TEMPLATE_ARGS (spec_args),
18388 INNERMOST_TEMPLATE_ARGS (args),
18389 UNIFY_ALLOW_NONE, /*explain_p=*/false))
18390 return NULL_TREE;
18391
18392 for (i = 0; i < ntparms; ++i)
18393 if (! TREE_VEC_ELT (innermost_deduced_args, i))
18394 return NULL_TREE;
18395
18396 /* Verify that nondeduced template arguments agree with the type
18397 obtained from argument deduction.
18398
18399 For example:
18400
18401 struct A { typedef int X; };
18402 template <class T, class U> struct C {};
18403 template <class T> struct C<T, typename T::X> {};
18404
18405 Then with the instantiation `C<A, int>', we can deduce that
18406 `T' is `A' but unify () does not check whether `typename T::X'
18407 is `int'. */
18408 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
18409 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
18410 spec_args, main_tmpl,
18411 tf_none, false, false);
18412 if (spec_args == error_mark_node
18413 /* We only need to check the innermost arguments; the other
18414 arguments will always agree. */
18415 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
18416 INNERMOST_TEMPLATE_ARGS (args)))
18417 return NULL_TREE;
18418
18419 /* Now that we have bindings for all of the template arguments,
18420 ensure that the arguments deduced for the template template
18421 parameters have compatible template parameter lists. See the use
18422 of template_template_parm_bindings_ok_p in fn_type_unification
18423 for more information. */
18424 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
18425 return NULL_TREE;
18426
18427 return deduced_args;
18428 }
18429
18430 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
18431 Return the TREE_LIST node with the most specialized template, if
18432 any. If there is no most specialized template, the error_mark_node
18433 is returned.
18434
18435 Note that this function does not look at, or modify, the
18436 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
18437 returned is one of the elements of INSTANTIATIONS, callers may
18438 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
18439 and retrieve it from the value returned. */
18440
18441 tree
18442 most_specialized_instantiation (tree templates)
18443 {
18444 tree fn, champ;
18445
18446 ++processing_template_decl;
18447
18448 champ = templates;
18449 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
18450 {
18451 int fate = 0;
18452
18453 if (get_bindings (TREE_VALUE (champ),
18454 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18455 NULL_TREE, /*check_ret=*/true))
18456 fate--;
18457
18458 if (get_bindings (TREE_VALUE (fn),
18459 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18460 NULL_TREE, /*check_ret=*/true))
18461 fate++;
18462
18463 if (fate == -1)
18464 champ = fn;
18465 else if (!fate)
18466 {
18467 /* Equally specialized, move to next function. If there
18468 is no next function, nothing's most specialized. */
18469 fn = TREE_CHAIN (fn);
18470 champ = fn;
18471 if (!fn)
18472 break;
18473 }
18474 }
18475
18476 if (champ)
18477 /* Now verify that champ is better than everything earlier in the
18478 instantiation list. */
18479 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
18480 if (get_bindings (TREE_VALUE (champ),
18481 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
18482 NULL_TREE, /*check_ret=*/true)
18483 || !get_bindings (TREE_VALUE (fn),
18484 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
18485 NULL_TREE, /*check_ret=*/true))
18486 {
18487 champ = NULL_TREE;
18488 break;
18489 }
18490
18491 processing_template_decl--;
18492
18493 if (!champ)
18494 return error_mark_node;
18495
18496 return champ;
18497 }
18498
18499 /* If DECL is a specialization of some template, return the most
18500 general such template. Otherwise, returns NULL_TREE.
18501
18502 For example, given:
18503
18504 template <class T> struct S { template <class U> void f(U); };
18505
18506 if TMPL is `template <class U> void S<int>::f(U)' this will return
18507 the full template. This function will not trace past partial
18508 specializations, however. For example, given in addition:
18509
18510 template <class T> struct S<T*> { template <class U> void f(U); };
18511
18512 if TMPL is `template <class U> void S<int*>::f(U)' this will return
18513 `template <class T> template <class U> S<T*>::f(U)'. */
18514
18515 tree
18516 most_general_template (tree decl)
18517 {
18518 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
18519 an immediate specialization. */
18520 if (TREE_CODE (decl) == FUNCTION_DECL)
18521 {
18522 if (DECL_TEMPLATE_INFO (decl)) {
18523 decl = DECL_TI_TEMPLATE (decl);
18524
18525 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
18526 template friend. */
18527 if (TREE_CODE (decl) != TEMPLATE_DECL)
18528 return NULL_TREE;
18529 } else
18530 return NULL_TREE;
18531 }
18532
18533 /* Look for more and more general templates. */
18534 while (DECL_TEMPLATE_INFO (decl))
18535 {
18536 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
18537 (See cp-tree.h for details.) */
18538 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
18539 break;
18540
18541 if (CLASS_TYPE_P (TREE_TYPE (decl))
18542 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
18543 break;
18544
18545 /* Stop if we run into an explicitly specialized class template. */
18546 if (!DECL_NAMESPACE_SCOPE_P (decl)
18547 && DECL_CONTEXT (decl)
18548 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
18549 break;
18550
18551 decl = DECL_TI_TEMPLATE (decl);
18552 }
18553
18554 return decl;
18555 }
18556
18557 /* Return the most specialized of the class template partial
18558 specializations of TMPL which can produce TYPE, a specialization of
18559 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
18560 a _TYPE node corresponding to the partial specialization, while the
18561 TREE_PURPOSE is the set of template arguments that must be
18562 substituted into the TREE_TYPE in order to generate TYPE.
18563
18564 If the choice of partial specialization is ambiguous, a diagnostic
18565 is issued, and the error_mark_node is returned. If there are no
18566 partial specializations of TMPL matching TYPE, then NULL_TREE is
18567 returned. */
18568
18569 static tree
18570 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
18571 {
18572 tree list = NULL_TREE;
18573 tree t;
18574 tree champ;
18575 int fate;
18576 bool ambiguous_p;
18577 tree args;
18578 tree outer_args = NULL_TREE;
18579
18580 tmpl = most_general_template (tmpl);
18581 args = CLASSTYPE_TI_ARGS (type);
18582
18583 /* For determining which partial specialization to use, only the
18584 innermost args are interesting. */
18585 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
18586 {
18587 outer_args = strip_innermost_template_args (args, 1);
18588 args = INNERMOST_TEMPLATE_ARGS (args);
18589 }
18590
18591 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
18592 {
18593 tree partial_spec_args;
18594 tree spec_args;
18595 tree spec_tmpl = TREE_VALUE (t);
18596 tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18597
18598 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
18599
18600 ++processing_template_decl;
18601
18602 if (outer_args)
18603 {
18604 /* Discard the outer levels of args, and then substitute in the
18605 template args from the enclosing class. */
18606 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
18607 partial_spec_args = tsubst_template_args
18608 (partial_spec_args, outer_args, tf_none, NULL_TREE);
18609
18610 /* And the same for the partial specialization TEMPLATE_DECL. */
18611 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
18612 }
18613
18614 partial_spec_args =
18615 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
18616 add_to_template_args (outer_args,
18617 partial_spec_args),
18618 tmpl, tf_none,
18619 /*require_all_args=*/true,
18620 /*use_default_args=*/true);
18621
18622 --processing_template_decl;
18623
18624 if (partial_spec_args == error_mark_node)
18625 return error_mark_node;
18626 if (spec_tmpl == error_mark_node)
18627 return error_mark_node;
18628
18629 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
18630 spec_args = get_class_bindings (tmpl, parms,
18631 partial_spec_args,
18632 args);
18633 if (spec_args)
18634 {
18635 if (outer_args)
18636 spec_args = add_to_template_args (outer_args, spec_args);
18637 list = tree_cons (spec_args, orig_parms, list);
18638 TREE_TYPE (list) = TREE_TYPE (t);
18639 }
18640 }
18641
18642 if (! list)
18643 return NULL_TREE;
18644
18645 ambiguous_p = false;
18646 t = list;
18647 champ = t;
18648 t = TREE_CHAIN (t);
18649 for (; t; t = TREE_CHAIN (t))
18650 {
18651 fate = more_specialized_class (tmpl, champ, t);
18652 if (fate == 1)
18653 ;
18654 else
18655 {
18656 if (fate == 0)
18657 {
18658 t = TREE_CHAIN (t);
18659 if (! t)
18660 {
18661 ambiguous_p = true;
18662 break;
18663 }
18664 }
18665 champ = t;
18666 }
18667 }
18668
18669 if (!ambiguous_p)
18670 for (t = list; t && t != champ; t = TREE_CHAIN (t))
18671 {
18672 fate = more_specialized_class (tmpl, champ, t);
18673 if (fate != 1)
18674 {
18675 ambiguous_p = true;
18676 break;
18677 }
18678 }
18679
18680 if (ambiguous_p)
18681 {
18682 const char *str;
18683 char *spaces = NULL;
18684 if (!(complain & tf_error))
18685 return error_mark_node;
18686 error ("ambiguous class template instantiation for %q#T", type);
18687 str = ngettext ("candidate is:", "candidates are:", list_length (list));
18688 for (t = list; t; t = TREE_CHAIN (t))
18689 {
18690 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
18691 spaces = spaces ? spaces : get_spaces (str);
18692 }
18693 free (spaces);
18694 return error_mark_node;
18695 }
18696
18697 return champ;
18698 }
18699
18700 /* Explicitly instantiate DECL. */
18701
18702 void
18703 do_decl_instantiation (tree decl, tree storage)
18704 {
18705 tree result = NULL_TREE;
18706 int extern_p = 0;
18707
18708 if (!decl || decl == error_mark_node)
18709 /* An error occurred, for which grokdeclarator has already issued
18710 an appropriate message. */
18711 return;
18712 else if (! DECL_LANG_SPECIFIC (decl))
18713 {
18714 error ("explicit instantiation of non-template %q#D", decl);
18715 return;
18716 }
18717 else if (VAR_P (decl))
18718 {
18719 /* There is an asymmetry here in the way VAR_DECLs and
18720 FUNCTION_DECLs are handled by grokdeclarator. In the case of
18721 the latter, the DECL we get back will be marked as a
18722 template instantiation, and the appropriate
18723 DECL_TEMPLATE_INFO will be set up. This does not happen for
18724 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
18725 should handle VAR_DECLs as it currently handles
18726 FUNCTION_DECLs. */
18727 if (!DECL_CLASS_SCOPE_P (decl))
18728 {
18729 error ("%qD is not a static data member of a class template", decl);
18730 return;
18731 }
18732 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
18733 if (!result || !VAR_P (result))
18734 {
18735 error ("no matching template for %qD found", decl);
18736 return;
18737 }
18738 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
18739 {
18740 error ("type %qT for explicit instantiation %qD does not match "
18741 "declared type %qT", TREE_TYPE (result), decl,
18742 TREE_TYPE (decl));
18743 return;
18744 }
18745 }
18746 else if (TREE_CODE (decl) != FUNCTION_DECL)
18747 {
18748 error ("explicit instantiation of %q#D", decl);
18749 return;
18750 }
18751 else
18752 result = decl;
18753
18754 /* Check for various error cases. Note that if the explicit
18755 instantiation is valid the RESULT will currently be marked as an
18756 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
18757 until we get here. */
18758
18759 if (DECL_TEMPLATE_SPECIALIZATION (result))
18760 {
18761 /* DR 259 [temp.spec].
18762
18763 Both an explicit instantiation and a declaration of an explicit
18764 specialization shall not appear in a program unless the explicit
18765 instantiation follows a declaration of the explicit specialization.
18766
18767 For a given set of template parameters, if an explicit
18768 instantiation of a template appears after a declaration of an
18769 explicit specialization for that template, the explicit
18770 instantiation has no effect. */
18771 return;
18772 }
18773 else if (DECL_EXPLICIT_INSTANTIATION (result))
18774 {
18775 /* [temp.spec]
18776
18777 No program shall explicitly instantiate any template more
18778 than once.
18779
18780 We check DECL_NOT_REALLY_EXTERN so as not to complain when
18781 the first instantiation was `extern' and the second is not,
18782 and EXTERN_P for the opposite case. */
18783 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
18784 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
18785 /* If an "extern" explicit instantiation follows an ordinary
18786 explicit instantiation, the template is instantiated. */
18787 if (extern_p)
18788 return;
18789 }
18790 else if (!DECL_IMPLICIT_INSTANTIATION (result))
18791 {
18792 error ("no matching template for %qD found", result);
18793 return;
18794 }
18795 else if (!DECL_TEMPLATE_INFO (result))
18796 {
18797 permerror (input_location, "explicit instantiation of non-template %q#D", result);
18798 return;
18799 }
18800
18801 if (storage == NULL_TREE)
18802 ;
18803 else if (storage == ridpointers[(int) RID_EXTERN])
18804 {
18805 if (!in_system_header && (cxx_dialect == cxx98))
18806 pedwarn (input_location, OPT_Wpedantic,
18807 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
18808 "instantiations");
18809 extern_p = 1;
18810 }
18811 else
18812 error ("storage class %qD applied to template instantiation", storage);
18813
18814 check_explicit_instantiation_namespace (result);
18815 mark_decl_instantiated (result, extern_p);
18816 if (! extern_p)
18817 instantiate_decl (result, /*defer_ok=*/1,
18818 /*expl_inst_class_mem_p=*/false);
18819 }
18820
18821 static void
18822 mark_class_instantiated (tree t, int extern_p)
18823 {
18824 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
18825 SET_CLASSTYPE_INTERFACE_KNOWN (t);
18826 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
18827 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
18828 if (! extern_p)
18829 {
18830 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
18831 rest_of_type_compilation (t, 1);
18832 }
18833 }
18834
18835 /* Called from do_type_instantiation through binding_table_foreach to
18836 do recursive instantiation for the type bound in ENTRY. */
18837 static void
18838 bt_instantiate_type_proc (binding_entry entry, void *data)
18839 {
18840 tree storage = *(tree *) data;
18841
18842 if (MAYBE_CLASS_TYPE_P (entry->type)
18843 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
18844 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
18845 }
18846
18847 /* Called from do_type_instantiation to instantiate a member
18848 (a member function or a static member variable) of an
18849 explicitly instantiated class template. */
18850 static void
18851 instantiate_class_member (tree decl, int extern_p)
18852 {
18853 mark_decl_instantiated (decl, extern_p);
18854 if (! extern_p)
18855 instantiate_decl (decl, /*defer_ok=*/1,
18856 /*expl_inst_class_mem_p=*/true);
18857 }
18858
18859 /* Perform an explicit instantiation of template class T. STORAGE, if
18860 non-null, is the RID for extern, inline or static. COMPLAIN is
18861 nonzero if this is called from the parser, zero if called recursively,
18862 since the standard is unclear (as detailed below). */
18863
18864 void
18865 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
18866 {
18867 int extern_p = 0;
18868 int nomem_p = 0;
18869 int static_p = 0;
18870 int previous_instantiation_extern_p = 0;
18871
18872 if (TREE_CODE (t) == TYPE_DECL)
18873 t = TREE_TYPE (t);
18874
18875 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
18876 {
18877 tree tmpl =
18878 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
18879 if (tmpl)
18880 error ("explicit instantiation of non-class template %qD", tmpl);
18881 else
18882 error ("explicit instantiation of non-template type %qT", t);
18883 return;
18884 }
18885
18886 complete_type (t);
18887
18888 if (!COMPLETE_TYPE_P (t))
18889 {
18890 if (complain & tf_error)
18891 error ("explicit instantiation of %q#T before definition of template",
18892 t);
18893 return;
18894 }
18895
18896 if (storage != NULL_TREE)
18897 {
18898 if (!in_system_header)
18899 {
18900 if (storage == ridpointers[(int) RID_EXTERN])
18901 {
18902 if (cxx_dialect == cxx98)
18903 pedwarn (input_location, OPT_Wpedantic,
18904 "ISO C++ 1998 forbids the use of %<extern%> on "
18905 "explicit instantiations");
18906 }
18907 else
18908 pedwarn (input_location, OPT_Wpedantic,
18909 "ISO C++ forbids the use of %qE"
18910 " on explicit instantiations", storage);
18911 }
18912
18913 if (storage == ridpointers[(int) RID_INLINE])
18914 nomem_p = 1;
18915 else if (storage == ridpointers[(int) RID_EXTERN])
18916 extern_p = 1;
18917 else if (storage == ridpointers[(int) RID_STATIC])
18918 static_p = 1;
18919 else
18920 {
18921 error ("storage class %qD applied to template instantiation",
18922 storage);
18923 extern_p = 0;
18924 }
18925 }
18926
18927 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
18928 {
18929 /* DR 259 [temp.spec].
18930
18931 Both an explicit instantiation and a declaration of an explicit
18932 specialization shall not appear in a program unless the explicit
18933 instantiation follows a declaration of the explicit specialization.
18934
18935 For a given set of template parameters, if an explicit
18936 instantiation of a template appears after a declaration of an
18937 explicit specialization for that template, the explicit
18938 instantiation has no effect. */
18939 return;
18940 }
18941 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18942 {
18943 /* [temp.spec]
18944
18945 No program shall explicitly instantiate any template more
18946 than once.
18947
18948 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18949 instantiation was `extern'. If EXTERN_P then the second is.
18950 These cases are OK. */
18951 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18952
18953 if (!previous_instantiation_extern_p && !extern_p
18954 && (complain & tf_error))
18955 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18956
18957 /* If we've already instantiated the template, just return now. */
18958 if (!CLASSTYPE_INTERFACE_ONLY (t))
18959 return;
18960 }
18961
18962 check_explicit_instantiation_namespace (TYPE_NAME (t));
18963 mark_class_instantiated (t, extern_p);
18964
18965 if (nomem_p)
18966 return;
18967
18968 {
18969 tree tmp;
18970
18971 /* In contrast to implicit instantiation, where only the
18972 declarations, and not the definitions, of members are
18973 instantiated, we have here:
18974
18975 [temp.explicit]
18976
18977 The explicit instantiation of a class template specialization
18978 implies the instantiation of all of its members not
18979 previously explicitly specialized in the translation unit
18980 containing the explicit instantiation.
18981
18982 Of course, we can't instantiate member template classes, since
18983 we don't have any arguments for them. Note that the standard
18984 is unclear on whether the instantiation of the members are
18985 *explicit* instantiations or not. However, the most natural
18986 interpretation is that it should be an explicit instantiation. */
18987
18988 if (! static_p)
18989 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18990 if (TREE_CODE (tmp) == FUNCTION_DECL
18991 && DECL_TEMPLATE_INSTANTIATION (tmp))
18992 instantiate_class_member (tmp, extern_p);
18993
18994 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18995 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
18996 instantiate_class_member (tmp, extern_p);
18997
18998 if (CLASSTYPE_NESTED_UTDS (t))
18999 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
19000 bt_instantiate_type_proc, &storage);
19001 }
19002 }
19003
19004 /* Given a function DECL, which is a specialization of TMPL, modify
19005 DECL to be a re-instantiation of TMPL with the same template
19006 arguments. TMPL should be the template into which tsubst'ing
19007 should occur for DECL, not the most general template.
19008
19009 One reason for doing this is a scenario like this:
19010
19011 template <class T>
19012 void f(const T&, int i);
19013
19014 void g() { f(3, 7); }
19015
19016 template <class T>
19017 void f(const T& t, const int i) { }
19018
19019 Note that when the template is first instantiated, with
19020 instantiate_template, the resulting DECL will have no name for the
19021 first parameter, and the wrong type for the second. So, when we go
19022 to instantiate the DECL, we regenerate it. */
19023
19024 static void
19025 regenerate_decl_from_template (tree decl, tree tmpl)
19026 {
19027 /* The arguments used to instantiate DECL, from the most general
19028 template. */
19029 tree args;
19030 tree code_pattern;
19031
19032 args = DECL_TI_ARGS (decl);
19033 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
19034
19035 /* Make sure that we can see identifiers, and compute access
19036 correctly. */
19037 push_access_scope (decl);
19038
19039 if (TREE_CODE (decl) == FUNCTION_DECL)
19040 {
19041 tree decl_parm;
19042 tree pattern_parm;
19043 tree specs;
19044 int args_depth;
19045 int parms_depth;
19046
19047 args_depth = TMPL_ARGS_DEPTH (args);
19048 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
19049 if (args_depth > parms_depth)
19050 args = get_innermost_template_args (args, parms_depth);
19051
19052 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
19053 args, tf_error, NULL_TREE,
19054 /*defer_ok*/false);
19055 if (specs && specs != error_mark_node)
19056 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
19057 specs);
19058
19059 /* Merge parameter declarations. */
19060 decl_parm = skip_artificial_parms_for (decl,
19061 DECL_ARGUMENTS (decl));
19062 pattern_parm
19063 = skip_artificial_parms_for (code_pattern,
19064 DECL_ARGUMENTS (code_pattern));
19065 while (decl_parm && !DECL_PACK_P (pattern_parm))
19066 {
19067 tree parm_type;
19068 tree attributes;
19069
19070 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19071 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
19072 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
19073 NULL_TREE);
19074 parm_type = type_decays_to (parm_type);
19075 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19076 TREE_TYPE (decl_parm) = parm_type;
19077 attributes = DECL_ATTRIBUTES (pattern_parm);
19078 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19079 {
19080 DECL_ATTRIBUTES (decl_parm) = attributes;
19081 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19082 }
19083 decl_parm = DECL_CHAIN (decl_parm);
19084 pattern_parm = DECL_CHAIN (pattern_parm);
19085 }
19086 /* Merge any parameters that match with the function parameter
19087 pack. */
19088 if (pattern_parm && DECL_PACK_P (pattern_parm))
19089 {
19090 int i, len;
19091 tree expanded_types;
19092 /* Expand the TYPE_PACK_EXPANSION that provides the types for
19093 the parameters in this function parameter pack. */
19094 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
19095 args, tf_error, NULL_TREE);
19096 len = TREE_VEC_LENGTH (expanded_types);
19097 for (i = 0; i < len; i++)
19098 {
19099 tree parm_type;
19100 tree attributes;
19101
19102 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
19103 /* Rename the parameter to include the index. */
19104 DECL_NAME (decl_parm) =
19105 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
19106 parm_type = TREE_VEC_ELT (expanded_types, i);
19107 parm_type = type_decays_to (parm_type);
19108 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
19109 TREE_TYPE (decl_parm) = parm_type;
19110 attributes = DECL_ATTRIBUTES (pattern_parm);
19111 if (DECL_ATTRIBUTES (decl_parm) != attributes)
19112 {
19113 DECL_ATTRIBUTES (decl_parm) = attributes;
19114 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
19115 }
19116 decl_parm = DECL_CHAIN (decl_parm);
19117 }
19118 }
19119 /* Merge additional specifiers from the CODE_PATTERN. */
19120 if (DECL_DECLARED_INLINE_P (code_pattern)
19121 && !DECL_DECLARED_INLINE_P (decl))
19122 DECL_DECLARED_INLINE_P (decl) = 1;
19123 }
19124 else if (VAR_P (decl))
19125 {
19126 DECL_INITIAL (decl) =
19127 tsubst_expr (DECL_INITIAL (code_pattern), args,
19128 tf_error, DECL_TI_TEMPLATE (decl),
19129 /*integral_constant_expression_p=*/false);
19130 if (VAR_HAD_UNKNOWN_BOUND (decl))
19131 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
19132 tf_error, DECL_TI_TEMPLATE (decl));
19133 }
19134 else
19135 gcc_unreachable ();
19136
19137 pop_access_scope (decl);
19138 }
19139
19140 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
19141 substituted to get DECL. */
19142
19143 tree
19144 template_for_substitution (tree decl)
19145 {
19146 tree tmpl = DECL_TI_TEMPLATE (decl);
19147
19148 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
19149 for the instantiation. This is not always the most general
19150 template. Consider, for example:
19151
19152 template <class T>
19153 struct S { template <class U> void f();
19154 template <> void f<int>(); };
19155
19156 and an instantiation of S<double>::f<int>. We want TD to be the
19157 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
19158 while (/* An instantiation cannot have a definition, so we need a
19159 more general template. */
19160 DECL_TEMPLATE_INSTANTIATION (tmpl)
19161 /* We must also deal with friend templates. Given:
19162
19163 template <class T> struct S {
19164 template <class U> friend void f() {};
19165 };
19166
19167 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
19168 so far as the language is concerned, but that's still
19169 where we get the pattern for the instantiation from. On
19170 other hand, if the definition comes outside the class, say:
19171
19172 template <class T> struct S {
19173 template <class U> friend void f();
19174 };
19175 template <class U> friend void f() {}
19176
19177 we don't need to look any further. That's what the check for
19178 DECL_INITIAL is for. */
19179 || (TREE_CODE (decl) == FUNCTION_DECL
19180 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
19181 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
19182 {
19183 /* The present template, TD, should not be a definition. If it
19184 were a definition, we should be using it! Note that we
19185 cannot restructure the loop to just keep going until we find
19186 a template with a definition, since that might go too far if
19187 a specialization was declared, but not defined. */
19188 gcc_assert (!VAR_P (decl)
19189 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
19190
19191 /* Fetch the more general template. */
19192 tmpl = DECL_TI_TEMPLATE (tmpl);
19193 }
19194
19195 return tmpl;
19196 }
19197
19198 /* Returns true if we need to instantiate this template instance even if we
19199 know we aren't going to emit it.. */
19200
19201 bool
19202 always_instantiate_p (tree decl)
19203 {
19204 /* We always instantiate inline functions so that we can inline them. An
19205 explicit instantiation declaration prohibits implicit instantiation of
19206 non-inline functions. With high levels of optimization, we would
19207 normally inline non-inline functions -- but we're not allowed to do
19208 that for "extern template" functions. Therefore, we check
19209 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
19210 return ((TREE_CODE (decl) == FUNCTION_DECL
19211 && (DECL_DECLARED_INLINE_P (decl)
19212 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
19213 /* And we need to instantiate static data members so that
19214 their initializers are available in integral constant
19215 expressions. */
19216 || (VAR_P (decl)
19217 && decl_maybe_constant_var_p (decl)));
19218 }
19219
19220 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
19221 instantiate it now, modifying TREE_TYPE (fn). */
19222
19223 void
19224 maybe_instantiate_noexcept (tree fn)
19225 {
19226 tree fntype, spec, noex, clone;
19227
19228 if (DECL_CLONED_FUNCTION_P (fn))
19229 fn = DECL_CLONED_FUNCTION (fn);
19230 fntype = TREE_TYPE (fn);
19231 spec = TYPE_RAISES_EXCEPTIONS (fntype);
19232
19233 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
19234 return;
19235
19236 noex = TREE_PURPOSE (spec);
19237
19238 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
19239 {
19240 if (push_tinst_level (fn))
19241 {
19242 push_access_scope (fn);
19243 push_deferring_access_checks (dk_no_deferred);
19244 input_location = DECL_SOURCE_LOCATION (fn);
19245 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
19246 DEFERRED_NOEXCEPT_ARGS (noex),
19247 tf_warning_or_error, fn,
19248 /*function_p=*/false,
19249 /*integral_constant_expression_p=*/true);
19250 pop_deferring_access_checks ();
19251 pop_access_scope (fn);
19252 pop_tinst_level ();
19253 spec = build_noexcept_spec (noex, tf_warning_or_error);
19254 if (spec == error_mark_node)
19255 spec = noexcept_false_spec;
19256 }
19257 else
19258 spec = noexcept_false_spec;
19259 }
19260 else
19261 {
19262 /* This is an implicitly declared function, so NOEX is a list of
19263 other functions to evaluate and merge. */
19264 tree elt;
19265 spec = noexcept_true_spec;
19266 for (elt = noex; elt; elt = OVL_NEXT (elt))
19267 {
19268 tree fn = OVL_CURRENT (elt);
19269 tree subspec;
19270 maybe_instantiate_noexcept (fn);
19271 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
19272 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
19273 }
19274 }
19275
19276 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
19277
19278 FOR_EACH_CLONE (clone, fn)
19279 {
19280 if (TREE_TYPE (clone) == fntype)
19281 TREE_TYPE (clone) = TREE_TYPE (fn);
19282 else
19283 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
19284 }
19285 }
19286
19287 /* Produce the definition of D, a _DECL generated from a template. If
19288 DEFER_OK is nonzero, then we don't have to actually do the
19289 instantiation now; we just have to do it sometime. Normally it is
19290 an error if this is an explicit instantiation but D is undefined.
19291 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
19292 explicitly instantiated class template. */
19293
19294 tree
19295 instantiate_decl (tree d, int defer_ok,
19296 bool expl_inst_class_mem_p)
19297 {
19298 tree tmpl = DECL_TI_TEMPLATE (d);
19299 tree gen_args;
19300 tree args;
19301 tree td;
19302 tree code_pattern;
19303 tree spec;
19304 tree gen_tmpl;
19305 bool pattern_defined;
19306 location_t saved_loc = input_location;
19307 int saved_unevaluated_operand = cp_unevaluated_operand;
19308 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19309 bool external_p;
19310 tree fn_context;
19311 bool nested;
19312
19313 /* This function should only be used to instantiate templates for
19314 functions and static member variables. */
19315 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
19316
19317 /* Variables are never deferred; if instantiation is required, they
19318 are instantiated right away. That allows for better code in the
19319 case that an expression refers to the value of the variable --
19320 if the variable has a constant value the referring expression can
19321 take advantage of that fact. */
19322 if (VAR_P (d)
19323 || DECL_DECLARED_CONSTEXPR_P (d))
19324 defer_ok = 0;
19325
19326 /* Don't instantiate cloned functions. Instead, instantiate the
19327 functions they cloned. */
19328 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
19329 d = DECL_CLONED_FUNCTION (d);
19330
19331 if (DECL_TEMPLATE_INSTANTIATED (d)
19332 || (TREE_CODE (d) == FUNCTION_DECL
19333 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
19334 || DECL_TEMPLATE_SPECIALIZATION (d))
19335 /* D has already been instantiated or explicitly specialized, so
19336 there's nothing for us to do here.
19337
19338 It might seem reasonable to check whether or not D is an explicit
19339 instantiation, and, if so, stop here. But when an explicit
19340 instantiation is deferred until the end of the compilation,
19341 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
19342 the instantiation. */
19343 return d;
19344
19345 /* Check to see whether we know that this template will be
19346 instantiated in some other file, as with "extern template"
19347 extension. */
19348 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
19349
19350 /* In general, we do not instantiate such templates. */
19351 if (external_p && !always_instantiate_p (d))
19352 return d;
19353
19354 gen_tmpl = most_general_template (tmpl);
19355 gen_args = DECL_TI_ARGS (d);
19356
19357 if (tmpl != gen_tmpl)
19358 /* We should already have the extra args. */
19359 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
19360 == TMPL_ARGS_DEPTH (gen_args));
19361 /* And what's in the hash table should match D. */
19362 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
19363 || spec == NULL_TREE);
19364
19365 /* This needs to happen before any tsubsting. */
19366 if (! push_tinst_level (d))
19367 return d;
19368
19369 timevar_push (TV_TEMPLATE_INST);
19370
19371 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
19372 for the instantiation. */
19373 td = template_for_substitution (d);
19374 code_pattern = DECL_TEMPLATE_RESULT (td);
19375
19376 /* We should never be trying to instantiate a member of a class
19377 template or partial specialization. */
19378 gcc_assert (d != code_pattern);
19379
19380 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
19381 || DECL_TEMPLATE_SPECIALIZATION (td))
19382 /* In the case of a friend template whose definition is provided
19383 outside the class, we may have too many arguments. Drop the
19384 ones we don't need. The same is true for specializations. */
19385 args = get_innermost_template_args
19386 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
19387 else
19388 args = gen_args;
19389
19390 if (TREE_CODE (d) == FUNCTION_DECL)
19391 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
19392 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
19393 else
19394 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
19395
19396 /* We may be in the middle of deferred access check. Disable it now. */
19397 push_deferring_access_checks (dk_no_deferred);
19398
19399 /* Unless an explicit instantiation directive has already determined
19400 the linkage of D, remember that a definition is available for
19401 this entity. */
19402 if (pattern_defined
19403 && !DECL_INTERFACE_KNOWN (d)
19404 && !DECL_NOT_REALLY_EXTERN (d))
19405 mark_definable (d);
19406
19407 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
19408 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
19409 input_location = DECL_SOURCE_LOCATION (d);
19410
19411 /* If D is a member of an explicitly instantiated class template,
19412 and no definition is available, treat it like an implicit
19413 instantiation. */
19414 if (!pattern_defined && expl_inst_class_mem_p
19415 && DECL_EXPLICIT_INSTANTIATION (d))
19416 {
19417 /* Leave linkage flags alone on instantiations with anonymous
19418 visibility. */
19419 if (TREE_PUBLIC (d))
19420 {
19421 DECL_NOT_REALLY_EXTERN (d) = 0;
19422 DECL_INTERFACE_KNOWN (d) = 0;
19423 }
19424 SET_DECL_IMPLICIT_INSTANTIATION (d);
19425 }
19426
19427 if (TREE_CODE (d) == FUNCTION_DECL)
19428 maybe_instantiate_noexcept (d);
19429
19430 /* Defer all other templates, unless we have been explicitly
19431 forbidden from doing so. */
19432 if (/* If there is no definition, we cannot instantiate the
19433 template. */
19434 ! pattern_defined
19435 /* If it's OK to postpone instantiation, do so. */
19436 || defer_ok
19437 /* If this is a static data member that will be defined
19438 elsewhere, we don't want to instantiate the entire data
19439 member, but we do want to instantiate the initializer so that
19440 we can substitute that elsewhere. */
19441 || (external_p && VAR_P (d)))
19442 {
19443 /* The definition of the static data member is now required so
19444 we must substitute the initializer. */
19445 if (VAR_P (d)
19446 && !DECL_INITIAL (d)
19447 && DECL_INITIAL (code_pattern))
19448 {
19449 tree ns;
19450 tree init;
19451 bool const_init = false;
19452
19453 ns = decl_namespace_context (d);
19454 push_nested_namespace (ns);
19455 push_nested_class (DECL_CONTEXT (d));
19456 init = tsubst_expr (DECL_INITIAL (code_pattern),
19457 args,
19458 tf_warning_or_error, NULL_TREE,
19459 /*integral_constant_expression_p=*/false);
19460 /* Make sure the initializer is still constant, in case of
19461 circular dependency (template/instantiate6.C). */
19462 const_init
19463 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19464 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
19465 /*asmspec_tree=*/NULL_TREE,
19466 LOOKUP_ONLYCONVERTING);
19467 pop_nested_class ();
19468 pop_nested_namespace (ns);
19469 }
19470
19471 /* We restore the source position here because it's used by
19472 add_pending_template. */
19473 input_location = saved_loc;
19474
19475 if (at_eof && !pattern_defined
19476 && DECL_EXPLICIT_INSTANTIATION (d)
19477 && DECL_NOT_REALLY_EXTERN (d))
19478 /* [temp.explicit]
19479
19480 The definition of a non-exported function template, a
19481 non-exported member function template, or a non-exported
19482 member function or static data member of a class template
19483 shall be present in every translation unit in which it is
19484 explicitly instantiated. */
19485 permerror (input_location, "explicit instantiation of %qD "
19486 "but no definition available", d);
19487
19488 /* If we're in unevaluated context, we just wanted to get the
19489 constant value; this isn't an odr use, so don't queue
19490 a full instantiation. */
19491 if (cp_unevaluated_operand != 0)
19492 goto out;
19493 /* ??? Historically, we have instantiated inline functions, even
19494 when marked as "extern template". */
19495 if (!(external_p && VAR_P (d)))
19496 add_pending_template (d);
19497 goto out;
19498 }
19499 /* Tell the repository that D is available in this translation unit
19500 -- and see if it is supposed to be instantiated here. */
19501 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
19502 {
19503 /* In a PCH file, despite the fact that the repository hasn't
19504 requested instantiation in the PCH it is still possible that
19505 an instantiation will be required in a file that includes the
19506 PCH. */
19507 if (pch_file)
19508 add_pending_template (d);
19509 /* Instantiate inline functions so that the inliner can do its
19510 job, even though we'll not be emitting a copy of this
19511 function. */
19512 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
19513 goto out;
19514 }
19515
19516 fn_context = decl_function_context (d);
19517 nested = (current_function_decl != NULL_TREE);
19518 if (!fn_context)
19519 push_to_top_level ();
19520 else
19521 {
19522 if (nested)
19523 push_function_context ();
19524 cp_unevaluated_operand = 0;
19525 c_inhibit_evaluation_warnings = 0;
19526 }
19527
19528 /* Mark D as instantiated so that recursive calls to
19529 instantiate_decl do not try to instantiate it again. */
19530 DECL_TEMPLATE_INSTANTIATED (d) = 1;
19531
19532 /* Regenerate the declaration in case the template has been modified
19533 by a subsequent redeclaration. */
19534 regenerate_decl_from_template (d, td);
19535
19536 /* We already set the file and line above. Reset them now in case
19537 they changed as a result of calling regenerate_decl_from_template. */
19538 input_location = DECL_SOURCE_LOCATION (d);
19539
19540 if (VAR_P (d))
19541 {
19542 tree init;
19543 bool const_init = false;
19544
19545 /* Clear out DECL_RTL; whatever was there before may not be right
19546 since we've reset the type of the declaration. */
19547 SET_DECL_RTL (d, NULL);
19548 DECL_IN_AGGR_P (d) = 0;
19549
19550 /* The initializer is placed in DECL_INITIAL by
19551 regenerate_decl_from_template so we don't need to
19552 push/pop_access_scope again here. Pull it out so that
19553 cp_finish_decl can process it. */
19554 init = DECL_INITIAL (d);
19555 DECL_INITIAL (d) = NULL_TREE;
19556 DECL_INITIALIZED_P (d) = 0;
19557
19558 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
19559 initializer. That function will defer actual emission until
19560 we have a chance to determine linkage. */
19561 DECL_EXTERNAL (d) = 0;
19562
19563 /* Enter the scope of D so that access-checking works correctly. */
19564 push_nested_class (DECL_CONTEXT (d));
19565 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
19566 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
19567 pop_nested_class ();
19568 }
19569 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
19570 synthesize_method (d);
19571 else if (TREE_CODE (d) == FUNCTION_DECL)
19572 {
19573 struct pointer_map_t *saved_local_specializations;
19574 tree subst_decl;
19575 tree tmpl_parm;
19576 tree spec_parm;
19577 tree block = NULL_TREE;
19578
19579 /* Save away the current list, in case we are instantiating one
19580 template from within the body of another. */
19581 saved_local_specializations = local_specializations;
19582
19583 /* Set up the list of local specializations. */
19584 local_specializations = pointer_map_create ();
19585
19586 /* Set up context. */
19587 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19588 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19589 block = push_stmt_list ();
19590 else
19591 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
19592
19593 /* Some typedefs referenced from within the template code need to be
19594 access checked at template instantiation time, i.e now. These
19595 types were added to the template at parsing time. Let's get those
19596 and perform the access checks then. */
19597 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
19598 gen_args);
19599
19600 /* Create substitution entries for the parameters. */
19601 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
19602 tmpl_parm = DECL_ARGUMENTS (subst_decl);
19603 spec_parm = DECL_ARGUMENTS (d);
19604 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
19605 {
19606 register_local_specialization (spec_parm, tmpl_parm);
19607 spec_parm = skip_artificial_parms_for (d, spec_parm);
19608 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
19609 }
19610 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
19611 {
19612 if (!DECL_PACK_P (tmpl_parm))
19613 {
19614 register_local_specialization (spec_parm, tmpl_parm);
19615 spec_parm = DECL_CHAIN (spec_parm);
19616 }
19617 else
19618 {
19619 /* Register the (value) argument pack as a specialization of
19620 TMPL_PARM, then move on. */
19621 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
19622 register_local_specialization (argpack, tmpl_parm);
19623 }
19624 }
19625 gcc_assert (!spec_parm);
19626
19627 /* Substitute into the body of the function. */
19628 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19629 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
19630 tf_warning_or_error, tmpl);
19631 else
19632 {
19633 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
19634 tf_warning_or_error, tmpl,
19635 /*integral_constant_expression_p=*/false);
19636
19637 /* Set the current input_location to the end of the function
19638 so that finish_function knows where we are. */
19639 input_location
19640 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
19641 }
19642
19643 /* We don't need the local specializations any more. */
19644 pointer_map_destroy (local_specializations);
19645 local_specializations = saved_local_specializations;
19646
19647 /* Finish the function. */
19648 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
19649 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
19650 DECL_SAVED_TREE (d) = pop_stmt_list (block);
19651 else
19652 {
19653 d = finish_function (0);
19654 expand_or_defer_fn (d);
19655 }
19656
19657 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
19658 cp_check_omp_declare_reduction (d);
19659 }
19660
19661 /* We're not deferring instantiation any more. */
19662 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
19663
19664 if (!fn_context)
19665 pop_from_top_level ();
19666 else if (nested)
19667 pop_function_context ();
19668
19669 out:
19670 input_location = saved_loc;
19671 cp_unevaluated_operand = saved_unevaluated_operand;
19672 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19673 pop_deferring_access_checks ();
19674 pop_tinst_level ();
19675
19676 timevar_pop (TV_TEMPLATE_INST);
19677
19678 return d;
19679 }
19680
19681 /* Run through the list of templates that we wish we could
19682 instantiate, and instantiate any we can. RETRIES is the
19683 number of times we retry pending template instantiation. */
19684
19685 void
19686 instantiate_pending_templates (int retries)
19687 {
19688 int reconsider;
19689 location_t saved_loc = input_location;
19690
19691 /* Instantiating templates may trigger vtable generation. This in turn
19692 may require further template instantiations. We place a limit here
19693 to avoid infinite loop. */
19694 if (pending_templates && retries >= max_tinst_depth)
19695 {
19696 tree decl = pending_templates->tinst->decl;
19697
19698 error ("template instantiation depth exceeds maximum of %d"
19699 " instantiating %q+D, possibly from virtual table generation"
19700 " (use -ftemplate-depth= to increase the maximum)",
19701 max_tinst_depth, decl);
19702 if (TREE_CODE (decl) == FUNCTION_DECL)
19703 /* Pretend that we defined it. */
19704 DECL_INITIAL (decl) = error_mark_node;
19705 return;
19706 }
19707
19708 do
19709 {
19710 struct pending_template **t = &pending_templates;
19711 struct pending_template *last = NULL;
19712 reconsider = 0;
19713 while (*t)
19714 {
19715 tree instantiation = reopen_tinst_level ((*t)->tinst);
19716 bool complete = false;
19717
19718 if (TYPE_P (instantiation))
19719 {
19720 tree fn;
19721
19722 if (!COMPLETE_TYPE_P (instantiation))
19723 {
19724 instantiate_class_template (instantiation);
19725 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
19726 for (fn = TYPE_METHODS (instantiation);
19727 fn;
19728 fn = TREE_CHAIN (fn))
19729 if (! DECL_ARTIFICIAL (fn))
19730 instantiate_decl (fn,
19731 /*defer_ok=*/0,
19732 /*expl_inst_class_mem_p=*/false);
19733 if (COMPLETE_TYPE_P (instantiation))
19734 reconsider = 1;
19735 }
19736
19737 complete = COMPLETE_TYPE_P (instantiation);
19738 }
19739 else
19740 {
19741 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
19742 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
19743 {
19744 instantiation
19745 = instantiate_decl (instantiation,
19746 /*defer_ok=*/0,
19747 /*expl_inst_class_mem_p=*/false);
19748 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
19749 reconsider = 1;
19750 }
19751
19752 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
19753 || DECL_TEMPLATE_INSTANTIATED (instantiation));
19754 }
19755
19756 if (complete)
19757 /* If INSTANTIATION has been instantiated, then we don't
19758 need to consider it again in the future. */
19759 *t = (*t)->next;
19760 else
19761 {
19762 last = *t;
19763 t = &(*t)->next;
19764 }
19765 tinst_depth = 0;
19766 current_tinst_level = NULL;
19767 }
19768 last_pending_template = last;
19769 }
19770 while (reconsider);
19771
19772 input_location = saved_loc;
19773 }
19774
19775 /* Substitute ARGVEC into T, which is a list of initializers for
19776 either base class or a non-static data member. The TREE_PURPOSEs
19777 are DECLs, and the TREE_VALUEs are the initializer values. Used by
19778 instantiate_decl. */
19779
19780 static tree
19781 tsubst_initializer_list (tree t, tree argvec)
19782 {
19783 tree inits = NULL_TREE;
19784
19785 for (; t; t = TREE_CHAIN (t))
19786 {
19787 tree decl;
19788 tree init;
19789 tree expanded_bases = NULL_TREE;
19790 tree expanded_arguments = NULL_TREE;
19791 int i, len = 1;
19792
19793 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
19794 {
19795 tree expr;
19796 tree arg;
19797
19798 /* Expand the base class expansion type into separate base
19799 classes. */
19800 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
19801 tf_warning_or_error,
19802 NULL_TREE);
19803 if (expanded_bases == error_mark_node)
19804 continue;
19805
19806 /* We'll be building separate TREE_LISTs of arguments for
19807 each base. */
19808 len = TREE_VEC_LENGTH (expanded_bases);
19809 expanded_arguments = make_tree_vec (len);
19810 for (i = 0; i < len; i++)
19811 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
19812
19813 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
19814 expand each argument in the TREE_VALUE of t. */
19815 expr = make_node (EXPR_PACK_EXPANSION);
19816 PACK_EXPANSION_LOCAL_P (expr) = true;
19817 PACK_EXPANSION_PARAMETER_PACKS (expr) =
19818 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
19819
19820 if (TREE_VALUE (t) == void_type_node)
19821 /* VOID_TYPE_NODE is used to indicate
19822 value-initialization. */
19823 {
19824 for (i = 0; i < len; i++)
19825 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
19826 }
19827 else
19828 {
19829 /* Substitute parameter packs into each argument in the
19830 TREE_LIST. */
19831 in_base_initializer = 1;
19832 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
19833 {
19834 tree expanded_exprs;
19835
19836 /* Expand the argument. */
19837 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
19838 expanded_exprs
19839 = tsubst_pack_expansion (expr, argvec,
19840 tf_warning_or_error,
19841 NULL_TREE);
19842 if (expanded_exprs == error_mark_node)
19843 continue;
19844
19845 /* Prepend each of the expanded expressions to the
19846 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
19847 for (i = 0; i < len; i++)
19848 {
19849 TREE_VEC_ELT (expanded_arguments, i) =
19850 tree_cons (NULL_TREE,
19851 TREE_VEC_ELT (expanded_exprs, i),
19852 TREE_VEC_ELT (expanded_arguments, i));
19853 }
19854 }
19855 in_base_initializer = 0;
19856
19857 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
19858 since we built them backwards. */
19859 for (i = 0; i < len; i++)
19860 {
19861 TREE_VEC_ELT (expanded_arguments, i) =
19862 nreverse (TREE_VEC_ELT (expanded_arguments, i));
19863 }
19864 }
19865 }
19866
19867 for (i = 0; i < len; ++i)
19868 {
19869 if (expanded_bases)
19870 {
19871 decl = TREE_VEC_ELT (expanded_bases, i);
19872 decl = expand_member_init (decl);
19873 init = TREE_VEC_ELT (expanded_arguments, i);
19874 }
19875 else
19876 {
19877 tree tmp;
19878 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
19879 tf_warning_or_error, NULL_TREE);
19880
19881 decl = expand_member_init (decl);
19882 if (decl && !DECL_P (decl))
19883 in_base_initializer = 1;
19884
19885 init = TREE_VALUE (t);
19886 tmp = init;
19887 if (init != void_type_node)
19888 init = tsubst_expr (init, argvec,
19889 tf_warning_or_error, NULL_TREE,
19890 /*integral_constant_expression_p=*/false);
19891 if (init == NULL_TREE && tmp != NULL_TREE)
19892 /* If we had an initializer but it instantiated to nothing,
19893 value-initialize the object. This will only occur when
19894 the initializer was a pack expansion where the parameter
19895 packs used in that expansion were of length zero. */
19896 init = void_type_node;
19897 in_base_initializer = 0;
19898 }
19899
19900 if (decl)
19901 {
19902 init = build_tree_list (decl, init);
19903 TREE_CHAIN (init) = inits;
19904 inits = init;
19905 }
19906 }
19907 }
19908 return inits;
19909 }
19910
19911 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19912
19913 static void
19914 set_current_access_from_decl (tree decl)
19915 {
19916 if (TREE_PRIVATE (decl))
19917 current_access_specifier = access_private_node;
19918 else if (TREE_PROTECTED (decl))
19919 current_access_specifier = access_protected_node;
19920 else
19921 current_access_specifier = access_public_node;
19922 }
19923
19924 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19925 is the instantiation (which should have been created with
19926 start_enum) and ARGS are the template arguments to use. */
19927
19928 static void
19929 tsubst_enum (tree tag, tree newtag, tree args)
19930 {
19931 tree e;
19932
19933 if (SCOPED_ENUM_P (newtag))
19934 begin_scope (sk_scoped_enum, newtag);
19935
19936 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
19937 {
19938 tree value;
19939 tree decl;
19940
19941 decl = TREE_VALUE (e);
19942 /* Note that in a template enum, the TREE_VALUE is the
19943 CONST_DECL, not the corresponding INTEGER_CST. */
19944 value = tsubst_expr (DECL_INITIAL (decl),
19945 args, tf_warning_or_error, NULL_TREE,
19946 /*integral_constant_expression_p=*/true);
19947
19948 /* Give this enumeration constant the correct access. */
19949 set_current_access_from_decl (decl);
19950
19951 /* Actually build the enumerator itself. */
19952 build_enumerator
19953 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
19954 }
19955
19956 if (SCOPED_ENUM_P (newtag))
19957 finish_scope ();
19958
19959 finish_enum_value_list (newtag);
19960 finish_enum (newtag);
19961
19962 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
19963 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
19964 }
19965
19966 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19967 its type -- but without substituting the innermost set of template
19968 arguments. So, innermost set of template parameters will appear in
19969 the type. */
19970
19971 tree
19972 get_mostly_instantiated_function_type (tree decl)
19973 {
19974 tree fn_type;
19975 tree tmpl;
19976 tree targs;
19977 tree tparms;
19978 int parm_depth;
19979
19980 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19981 targs = DECL_TI_ARGS (decl);
19982 tparms = DECL_TEMPLATE_PARMS (tmpl);
19983 parm_depth = TMPL_PARMS_DEPTH (tparms);
19984
19985 /* There should be as many levels of arguments as there are levels
19986 of parameters. */
19987 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19988
19989 fn_type = TREE_TYPE (tmpl);
19990
19991 if (parm_depth == 1)
19992 /* No substitution is necessary. */
19993 ;
19994 else
19995 {
19996 int i;
19997 tree partial_args;
19998
19999 /* Replace the innermost level of the TARGS with NULL_TREEs to
20000 let tsubst know not to substitute for those parameters. */
20001 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
20002 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
20003 SET_TMPL_ARGS_LEVEL (partial_args, i,
20004 TMPL_ARGS_LEVEL (targs, i));
20005 SET_TMPL_ARGS_LEVEL (partial_args,
20006 TMPL_ARGS_DEPTH (targs),
20007 make_tree_vec (DECL_NTPARMS (tmpl)));
20008
20009 /* Make sure that we can see identifiers, and compute access
20010 correctly. */
20011 push_access_scope (decl);
20012
20013 ++processing_template_decl;
20014 /* Now, do the (partial) substitution to figure out the
20015 appropriate function type. */
20016 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
20017 --processing_template_decl;
20018
20019 /* Substitute into the template parameters to obtain the real
20020 innermost set of parameters. This step is important if the
20021 innermost set of template parameters contains value
20022 parameters whose types depend on outer template parameters. */
20023 TREE_VEC_LENGTH (partial_args)--;
20024 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
20025
20026 pop_access_scope (decl);
20027 }
20028
20029 return fn_type;
20030 }
20031
20032 /* Return truthvalue if we're processing a template different from
20033 the last one involved in diagnostics. */
20034 int
20035 problematic_instantiation_changed (void)
20036 {
20037 return current_tinst_level != last_error_tinst_level;
20038 }
20039
20040 /* Remember current template involved in diagnostics. */
20041 void
20042 record_last_problematic_instantiation (void)
20043 {
20044 last_error_tinst_level = current_tinst_level;
20045 }
20046
20047 struct tinst_level *
20048 current_instantiation (void)
20049 {
20050 return current_tinst_level;
20051 }
20052
20053 /* [temp.param] Check that template non-type parm TYPE is of an allowable
20054 type. Return zero for ok, nonzero for disallowed. Issue error and
20055 warning messages under control of COMPLAIN. */
20056
20057 static int
20058 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
20059 {
20060 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
20061 return 0;
20062 else if (POINTER_TYPE_P (type))
20063 return 0;
20064 else if (TYPE_PTRMEM_P (type))
20065 return 0;
20066 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
20067 return 0;
20068 else if (TREE_CODE (type) == TYPENAME_TYPE)
20069 return 0;
20070 else if (TREE_CODE (type) == DECLTYPE_TYPE)
20071 return 0;
20072 else if (TREE_CODE (type) == NULLPTR_TYPE)
20073 return 0;
20074
20075 if (complain & tf_error)
20076 {
20077 if (type == error_mark_node)
20078 inform (input_location, "invalid template non-type parameter");
20079 else
20080 error ("%q#T is not a valid type for a template non-type parameter",
20081 type);
20082 }
20083 return 1;
20084 }
20085
20086 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
20087 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
20088
20089 static bool
20090 dependent_type_p_r (tree type)
20091 {
20092 tree scope;
20093
20094 /* [temp.dep.type]
20095
20096 A type is dependent if it is:
20097
20098 -- a template parameter. Template template parameters are types
20099 for us (since TYPE_P holds true for them) so we handle
20100 them here. */
20101 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20102 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
20103 return true;
20104 /* -- a qualified-id with a nested-name-specifier which contains a
20105 class-name that names a dependent type or whose unqualified-id
20106 names a dependent type. */
20107 if (TREE_CODE (type) == TYPENAME_TYPE)
20108 return true;
20109 /* -- a cv-qualified type where the cv-unqualified type is
20110 dependent. */
20111 type = TYPE_MAIN_VARIANT (type);
20112 /* -- a compound type constructed from any dependent type. */
20113 if (TYPE_PTRMEM_P (type))
20114 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
20115 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
20116 (type)));
20117 else if (TYPE_PTR_P (type)
20118 || TREE_CODE (type) == REFERENCE_TYPE)
20119 return dependent_type_p (TREE_TYPE (type));
20120 else if (TREE_CODE (type) == FUNCTION_TYPE
20121 || TREE_CODE (type) == METHOD_TYPE)
20122 {
20123 tree arg_type;
20124
20125 if (dependent_type_p (TREE_TYPE (type)))
20126 return true;
20127 for (arg_type = TYPE_ARG_TYPES (type);
20128 arg_type;
20129 arg_type = TREE_CHAIN (arg_type))
20130 if (dependent_type_p (TREE_VALUE (arg_type)))
20131 return true;
20132 return false;
20133 }
20134 /* -- an array type constructed from any dependent type or whose
20135 size is specified by a constant expression that is
20136 value-dependent.
20137
20138 We checked for type- and value-dependence of the bounds in
20139 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
20140 if (TREE_CODE (type) == ARRAY_TYPE)
20141 {
20142 if (TYPE_DOMAIN (type)
20143 && dependent_type_p (TYPE_DOMAIN (type)))
20144 return true;
20145 return dependent_type_p (TREE_TYPE (type));
20146 }
20147
20148 /* -- a template-id in which either the template name is a template
20149 parameter ... */
20150 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20151 return true;
20152 /* ... or any of the template arguments is a dependent type or
20153 an expression that is type-dependent or value-dependent. */
20154 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
20155 && (any_dependent_template_arguments_p
20156 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
20157 return true;
20158
20159 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
20160 dependent; if the argument of the `typeof' expression is not
20161 type-dependent, then it should already been have resolved. */
20162 if (TREE_CODE (type) == TYPEOF_TYPE
20163 || TREE_CODE (type) == DECLTYPE_TYPE
20164 || TREE_CODE (type) == UNDERLYING_TYPE)
20165 return true;
20166
20167 /* A template argument pack is dependent if any of its packed
20168 arguments are. */
20169 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
20170 {
20171 tree args = ARGUMENT_PACK_ARGS (type);
20172 int i, len = TREE_VEC_LENGTH (args);
20173 for (i = 0; i < len; ++i)
20174 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20175 return true;
20176 }
20177
20178 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
20179 be template parameters. */
20180 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
20181 return true;
20182
20183 /* The standard does not specifically mention types that are local
20184 to template functions or local classes, but they should be
20185 considered dependent too. For example:
20186
20187 template <int I> void f() {
20188 enum E { a = I };
20189 S<sizeof (E)> s;
20190 }
20191
20192 The size of `E' cannot be known until the value of `I' has been
20193 determined. Therefore, `E' must be considered dependent. */
20194 scope = TYPE_CONTEXT (type);
20195 if (scope && TYPE_P (scope))
20196 return dependent_type_p (scope);
20197 /* Don't use type_dependent_expression_p here, as it can lead
20198 to infinite recursion trying to determine whether a lambda
20199 nested in a lambda is dependent (c++/47687). */
20200 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
20201 && DECL_LANG_SPECIFIC (scope)
20202 && DECL_TEMPLATE_INFO (scope)
20203 && (any_dependent_template_arguments_p
20204 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
20205 return true;
20206
20207 /* Other types are non-dependent. */
20208 return false;
20209 }
20210
20211 /* Returns TRUE if TYPE is dependent, in the sense of
20212 [temp.dep.type]. Note that a NULL type is considered dependent. */
20213
20214 bool
20215 dependent_type_p (tree type)
20216 {
20217 /* If there are no template parameters in scope, then there can't be
20218 any dependent types. */
20219 if (!processing_template_decl)
20220 {
20221 /* If we are not processing a template, then nobody should be
20222 providing us with a dependent type. */
20223 gcc_assert (type);
20224 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
20225 return false;
20226 }
20227
20228 /* If the type is NULL, we have not computed a type for the entity
20229 in question; in that case, the type is dependent. */
20230 if (!type)
20231 return true;
20232
20233 /* Erroneous types can be considered non-dependent. */
20234 if (type == error_mark_node)
20235 return false;
20236
20237 /* If we have not already computed the appropriate value for TYPE,
20238 do so now. */
20239 if (!TYPE_DEPENDENT_P_VALID (type))
20240 {
20241 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
20242 TYPE_DEPENDENT_P_VALID (type) = 1;
20243 }
20244
20245 return TYPE_DEPENDENT_P (type);
20246 }
20247
20248 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
20249 lookup. In other words, a dependent type that is not the current
20250 instantiation. */
20251
20252 bool
20253 dependent_scope_p (tree scope)
20254 {
20255 return (scope && TYPE_P (scope) && dependent_type_p (scope)
20256 && !currently_open_class (scope));
20257 }
20258
20259 /* T is a SCOPE_REF; return whether we need to consider it
20260 instantiation-dependent so that we can check access at instantiation
20261 time even though we know which member it resolves to. */
20262
20263 static bool
20264 instantiation_dependent_scope_ref_p (tree t)
20265 {
20266 if (DECL_P (TREE_OPERAND (t, 1))
20267 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
20268 && accessible_in_template_p (TREE_OPERAND (t, 0),
20269 TREE_OPERAND (t, 1)))
20270 return false;
20271 else
20272 return true;
20273 }
20274
20275 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
20276 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
20277 expression. */
20278
20279 /* Note that this predicate is not appropriate for general expressions;
20280 only constant expressions (that satisfy potential_constant_expression)
20281 can be tested for value dependence. */
20282
20283 bool
20284 value_dependent_expression_p (tree expression)
20285 {
20286 if (!processing_template_decl)
20287 return false;
20288
20289 /* A name declared with a dependent type. */
20290 if (DECL_P (expression) && type_dependent_expression_p (expression))
20291 return true;
20292
20293 switch (TREE_CODE (expression))
20294 {
20295 case IDENTIFIER_NODE:
20296 /* A name that has not been looked up -- must be dependent. */
20297 return true;
20298
20299 case TEMPLATE_PARM_INDEX:
20300 /* A non-type template parm. */
20301 return true;
20302
20303 case CONST_DECL:
20304 /* A non-type template parm. */
20305 if (DECL_TEMPLATE_PARM_P (expression))
20306 return true;
20307 return value_dependent_expression_p (DECL_INITIAL (expression));
20308
20309 case VAR_DECL:
20310 /* A constant with literal type and is initialized
20311 with an expression that is value-dependent.
20312
20313 Note that a non-dependent parenthesized initializer will have
20314 already been replaced with its constant value, so if we see
20315 a TREE_LIST it must be dependent. */
20316 if (DECL_INITIAL (expression)
20317 && decl_constant_var_p (expression)
20318 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
20319 || value_dependent_expression_p (DECL_INITIAL (expression))))
20320 return true;
20321 return false;
20322
20323 case DYNAMIC_CAST_EXPR:
20324 case STATIC_CAST_EXPR:
20325 case CONST_CAST_EXPR:
20326 case REINTERPRET_CAST_EXPR:
20327 case CAST_EXPR:
20328 /* These expressions are value-dependent if the type to which
20329 the cast occurs is dependent or the expression being casted
20330 is value-dependent. */
20331 {
20332 tree type = TREE_TYPE (expression);
20333
20334 if (dependent_type_p (type))
20335 return true;
20336
20337 /* A functional cast has a list of operands. */
20338 expression = TREE_OPERAND (expression, 0);
20339 if (!expression)
20340 {
20341 /* If there are no operands, it must be an expression such
20342 as "int()". This should not happen for aggregate types
20343 because it would form non-constant expressions. */
20344 gcc_assert (cxx_dialect >= cxx11
20345 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
20346
20347 return false;
20348 }
20349
20350 if (TREE_CODE (expression) == TREE_LIST)
20351 return any_value_dependent_elements_p (expression);
20352
20353 return value_dependent_expression_p (expression);
20354 }
20355
20356 case SIZEOF_EXPR:
20357 if (SIZEOF_EXPR_TYPE_P (expression))
20358 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
20359 /* FALLTHRU */
20360 case ALIGNOF_EXPR:
20361 case TYPEID_EXPR:
20362 /* A `sizeof' expression is value-dependent if the operand is
20363 type-dependent or is a pack expansion. */
20364 expression = TREE_OPERAND (expression, 0);
20365 if (PACK_EXPANSION_P (expression))
20366 return true;
20367 else if (TYPE_P (expression))
20368 return dependent_type_p (expression);
20369 return instantiation_dependent_expression_p (expression);
20370
20371 case AT_ENCODE_EXPR:
20372 /* An 'encode' expression is value-dependent if the operand is
20373 type-dependent. */
20374 expression = TREE_OPERAND (expression, 0);
20375 return dependent_type_p (expression);
20376
20377 case NOEXCEPT_EXPR:
20378 expression = TREE_OPERAND (expression, 0);
20379 return instantiation_dependent_expression_p (expression);
20380
20381 case SCOPE_REF:
20382 /* All instantiation-dependent expressions should also be considered
20383 value-dependent. */
20384 return instantiation_dependent_scope_ref_p (expression);
20385
20386 case COMPONENT_REF:
20387 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
20388 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
20389
20390 case NONTYPE_ARGUMENT_PACK:
20391 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
20392 is value-dependent. */
20393 {
20394 tree values = ARGUMENT_PACK_ARGS (expression);
20395 int i, len = TREE_VEC_LENGTH (values);
20396
20397 for (i = 0; i < len; ++i)
20398 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
20399 return true;
20400
20401 return false;
20402 }
20403
20404 case TRAIT_EXPR:
20405 {
20406 tree type2 = TRAIT_EXPR_TYPE2 (expression);
20407 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
20408 || (type2 ? dependent_type_p (type2) : false));
20409 }
20410
20411 case MODOP_EXPR:
20412 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20413 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
20414
20415 case ARRAY_REF:
20416 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
20417 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
20418
20419 case ADDR_EXPR:
20420 {
20421 tree op = TREE_OPERAND (expression, 0);
20422 return (value_dependent_expression_p (op)
20423 || has_value_dependent_address (op));
20424 }
20425
20426 case CALL_EXPR:
20427 {
20428 tree fn = get_callee_fndecl (expression);
20429 int i, nargs;
20430 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
20431 return true;
20432 nargs = call_expr_nargs (expression);
20433 for (i = 0; i < nargs; ++i)
20434 {
20435 tree op = CALL_EXPR_ARG (expression, i);
20436 /* In a call to a constexpr member function, look through the
20437 implicit ADDR_EXPR on the object argument so that it doesn't
20438 cause the call to be considered value-dependent. We also
20439 look through it in potential_constant_expression. */
20440 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
20441 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20442 && TREE_CODE (op) == ADDR_EXPR)
20443 op = TREE_OPERAND (op, 0);
20444 if (value_dependent_expression_p (op))
20445 return true;
20446 }
20447 return false;
20448 }
20449
20450 case TEMPLATE_ID_EXPR:
20451 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
20452 type-dependent. */
20453 return type_dependent_expression_p (expression);
20454
20455 case CONSTRUCTOR:
20456 {
20457 unsigned ix;
20458 tree val;
20459 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
20460 if (value_dependent_expression_p (val))
20461 return true;
20462 return false;
20463 }
20464
20465 case STMT_EXPR:
20466 /* Treat a GNU statement expression as dependent to avoid crashing
20467 under fold_non_dependent_expr; it can't be constant. */
20468 return true;
20469
20470 default:
20471 /* A constant expression is value-dependent if any subexpression is
20472 value-dependent. */
20473 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
20474 {
20475 case tcc_reference:
20476 case tcc_unary:
20477 case tcc_comparison:
20478 case tcc_binary:
20479 case tcc_expression:
20480 case tcc_vl_exp:
20481 {
20482 int i, len = cp_tree_operand_length (expression);
20483
20484 for (i = 0; i < len; i++)
20485 {
20486 tree t = TREE_OPERAND (expression, i);
20487
20488 /* In some cases, some of the operands may be missing.l
20489 (For example, in the case of PREDECREMENT_EXPR, the
20490 amount to increment by may be missing.) That doesn't
20491 make the expression dependent. */
20492 if (t && value_dependent_expression_p (t))
20493 return true;
20494 }
20495 }
20496 break;
20497 default:
20498 break;
20499 }
20500 break;
20501 }
20502
20503 /* The expression is not value-dependent. */
20504 return false;
20505 }
20506
20507 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
20508 [temp.dep.expr]. Note that an expression with no type is
20509 considered dependent. Other parts of the compiler arrange for an
20510 expression with type-dependent subexpressions to have no type, so
20511 this function doesn't have to be fully recursive. */
20512
20513 bool
20514 type_dependent_expression_p (tree expression)
20515 {
20516 if (!processing_template_decl)
20517 return false;
20518
20519 if (expression == NULL_TREE || expression == error_mark_node)
20520 return false;
20521
20522 /* An unresolved name is always dependent. */
20523 if (identifier_p (expression) || TREE_CODE (expression) == USING_DECL)
20524 return true;
20525
20526 /* Some expression forms are never type-dependent. */
20527 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
20528 || TREE_CODE (expression) == SIZEOF_EXPR
20529 || TREE_CODE (expression) == ALIGNOF_EXPR
20530 || TREE_CODE (expression) == AT_ENCODE_EXPR
20531 || TREE_CODE (expression) == NOEXCEPT_EXPR
20532 || TREE_CODE (expression) == TRAIT_EXPR
20533 || TREE_CODE (expression) == TYPEID_EXPR
20534 || TREE_CODE (expression) == DELETE_EXPR
20535 || TREE_CODE (expression) == VEC_DELETE_EXPR
20536 || TREE_CODE (expression) == THROW_EXPR)
20537 return false;
20538
20539 /* The types of these expressions depends only on the type to which
20540 the cast occurs. */
20541 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
20542 || TREE_CODE (expression) == STATIC_CAST_EXPR
20543 || TREE_CODE (expression) == CONST_CAST_EXPR
20544 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
20545 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
20546 || TREE_CODE (expression) == CAST_EXPR)
20547 return dependent_type_p (TREE_TYPE (expression));
20548
20549 /* The types of these expressions depends only on the type created
20550 by the expression. */
20551 if (TREE_CODE (expression) == NEW_EXPR
20552 || TREE_CODE (expression) == VEC_NEW_EXPR)
20553 {
20554 /* For NEW_EXPR tree nodes created inside a template, either
20555 the object type itself or a TREE_LIST may appear as the
20556 operand 1. */
20557 tree type = TREE_OPERAND (expression, 1);
20558 if (TREE_CODE (type) == TREE_LIST)
20559 /* This is an array type. We need to check array dimensions
20560 as well. */
20561 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
20562 || value_dependent_expression_p
20563 (TREE_OPERAND (TREE_VALUE (type), 1));
20564 else
20565 return dependent_type_p (type);
20566 }
20567
20568 if (TREE_CODE (expression) == SCOPE_REF)
20569 {
20570 tree scope = TREE_OPERAND (expression, 0);
20571 tree name = TREE_OPERAND (expression, 1);
20572
20573 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
20574 contains an identifier associated by name lookup with one or more
20575 declarations declared with a dependent type, or...a
20576 nested-name-specifier or qualified-id that names a member of an
20577 unknown specialization. */
20578 return (type_dependent_expression_p (name)
20579 || dependent_scope_p (scope));
20580 }
20581
20582 if (TREE_CODE (expression) == FUNCTION_DECL
20583 && DECL_LANG_SPECIFIC (expression)
20584 && DECL_TEMPLATE_INFO (expression)
20585 && (any_dependent_template_arguments_p
20586 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
20587 return true;
20588
20589 if (TREE_CODE (expression) == TEMPLATE_DECL
20590 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
20591 return false;
20592
20593 if (TREE_CODE (expression) == STMT_EXPR)
20594 expression = stmt_expr_value_expr (expression);
20595
20596 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
20597 {
20598 tree elt;
20599 unsigned i;
20600
20601 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
20602 {
20603 if (type_dependent_expression_p (elt))
20604 return true;
20605 }
20606 return false;
20607 }
20608
20609 /* A static data member of the current instantiation with incomplete
20610 array type is type-dependent, as the definition and specializations
20611 can have different bounds. */
20612 if (VAR_P (expression)
20613 && DECL_CLASS_SCOPE_P (expression)
20614 && dependent_type_p (DECL_CONTEXT (expression))
20615 && VAR_HAD_UNKNOWN_BOUND (expression))
20616 return true;
20617
20618 /* An array of unknown bound depending on a variadic parameter, eg:
20619
20620 template<typename... Args>
20621 void foo (Args... args)
20622 {
20623 int arr[] = { args... };
20624 }
20625
20626 template<int... vals>
20627 void bar ()
20628 {
20629 int arr[] = { vals... };
20630 }
20631
20632 If the array has no length and has an initializer, it must be that
20633 we couldn't determine its length in cp_complete_array_type because
20634 it is dependent. */
20635 if (VAR_P (expression)
20636 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
20637 && !TYPE_DOMAIN (TREE_TYPE (expression))
20638 && DECL_INITIAL (expression))
20639 return true;
20640
20641 if (TREE_TYPE (expression) == unknown_type_node)
20642 {
20643 if (TREE_CODE (expression) == ADDR_EXPR)
20644 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
20645 if (TREE_CODE (expression) == COMPONENT_REF
20646 || TREE_CODE (expression) == OFFSET_REF)
20647 {
20648 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
20649 return true;
20650 expression = TREE_OPERAND (expression, 1);
20651 if (identifier_p (expression))
20652 return false;
20653 }
20654 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
20655 if (TREE_CODE (expression) == SCOPE_REF)
20656 return false;
20657
20658 /* Always dependent, on the number of arguments if nothing else. */
20659 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
20660 return true;
20661
20662 if (BASELINK_P (expression))
20663 expression = BASELINK_FUNCTIONS (expression);
20664
20665 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
20666 {
20667 if (any_dependent_template_arguments_p
20668 (TREE_OPERAND (expression, 1)))
20669 return true;
20670 expression = TREE_OPERAND (expression, 0);
20671 }
20672 gcc_assert (TREE_CODE (expression) == OVERLOAD
20673 || TREE_CODE (expression) == FUNCTION_DECL);
20674
20675 while (expression)
20676 {
20677 if (type_dependent_expression_p (OVL_CURRENT (expression)))
20678 return true;
20679 expression = OVL_NEXT (expression);
20680 }
20681 return false;
20682 }
20683
20684 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
20685
20686 return (dependent_type_p (TREE_TYPE (expression)));
20687 }
20688
20689 /* walk_tree callback function for instantiation_dependent_expression_p,
20690 below. Returns non-zero if a dependent subexpression is found. */
20691
20692 static tree
20693 instantiation_dependent_r (tree *tp, int *walk_subtrees,
20694 void * /*data*/)
20695 {
20696 if (TYPE_P (*tp))
20697 {
20698 /* We don't have to worry about decltype currently because decltype
20699 of an instantiation-dependent expr is a dependent type. This
20700 might change depending on the resolution of DR 1172. */
20701 *walk_subtrees = false;
20702 return NULL_TREE;
20703 }
20704 enum tree_code code = TREE_CODE (*tp);
20705 switch (code)
20706 {
20707 /* Don't treat an argument list as dependent just because it has no
20708 TREE_TYPE. */
20709 case TREE_LIST:
20710 case TREE_VEC:
20711 return NULL_TREE;
20712
20713 case VAR_DECL:
20714 case CONST_DECL:
20715 /* A constant with a dependent initializer is dependent. */
20716 if (value_dependent_expression_p (*tp))
20717 return *tp;
20718 break;
20719
20720 case TEMPLATE_PARM_INDEX:
20721 return *tp;
20722
20723 /* Handle expressions with type operands. */
20724 case SIZEOF_EXPR:
20725 case ALIGNOF_EXPR:
20726 case TYPEID_EXPR:
20727 case AT_ENCODE_EXPR:
20728 {
20729 tree op = TREE_OPERAND (*tp, 0);
20730 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
20731 op = TREE_TYPE (op);
20732 if (TYPE_P (op))
20733 {
20734 if (dependent_type_p (op))
20735 return *tp;
20736 else
20737 {
20738 *walk_subtrees = false;
20739 return NULL_TREE;
20740 }
20741 }
20742 break;
20743 }
20744
20745 case TRAIT_EXPR:
20746 if (dependent_type_p (TRAIT_EXPR_TYPE1 (*tp))
20747 || (TRAIT_EXPR_TYPE2 (*tp)
20748 && dependent_type_p (TRAIT_EXPR_TYPE2 (*tp))))
20749 return *tp;
20750 *walk_subtrees = false;
20751 return NULL_TREE;
20752
20753 case COMPONENT_REF:
20754 if (identifier_p (TREE_OPERAND (*tp, 1)))
20755 /* In a template, finish_class_member_access_expr creates a
20756 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
20757 type-dependent, so that we can check access control at
20758 instantiation time (PR 42277). See also Core issue 1273. */
20759 return *tp;
20760 break;
20761
20762 case SCOPE_REF:
20763 if (instantiation_dependent_scope_ref_p (*tp))
20764 return *tp;
20765 else
20766 break;
20767
20768 /* Treat statement-expressions as dependent. */
20769 case BIND_EXPR:
20770 return *tp;
20771
20772 default:
20773 break;
20774 }
20775
20776 if (type_dependent_expression_p (*tp))
20777 return *tp;
20778 else
20779 return NULL_TREE;
20780 }
20781
20782 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
20783 sense defined by the ABI:
20784
20785 "An expression is instantiation-dependent if it is type-dependent
20786 or value-dependent, or it has a subexpression that is type-dependent
20787 or value-dependent." */
20788
20789 bool
20790 instantiation_dependent_expression_p (tree expression)
20791 {
20792 tree result;
20793
20794 if (!processing_template_decl)
20795 return false;
20796
20797 if (expression == error_mark_node)
20798 return false;
20799
20800 result = cp_walk_tree_without_duplicates (&expression,
20801 instantiation_dependent_r, NULL);
20802 return result != NULL_TREE;
20803 }
20804
20805 /* Like type_dependent_expression_p, but it also works while not processing
20806 a template definition, i.e. during substitution or mangling. */
20807
20808 bool
20809 type_dependent_expression_p_push (tree expr)
20810 {
20811 bool b;
20812 ++processing_template_decl;
20813 b = type_dependent_expression_p (expr);
20814 --processing_template_decl;
20815 return b;
20816 }
20817
20818 /* Returns TRUE if ARGS contains a type-dependent expression. */
20819
20820 bool
20821 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
20822 {
20823 unsigned int i;
20824 tree arg;
20825
20826 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
20827 {
20828 if (type_dependent_expression_p (arg))
20829 return true;
20830 }
20831 return false;
20832 }
20833
20834 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20835 expressions) contains any type-dependent expressions. */
20836
20837 bool
20838 any_type_dependent_elements_p (const_tree list)
20839 {
20840 for (; list; list = TREE_CHAIN (list))
20841 if (type_dependent_expression_p (TREE_VALUE (list)))
20842 return true;
20843
20844 return false;
20845 }
20846
20847 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
20848 expressions) contains any value-dependent expressions. */
20849
20850 bool
20851 any_value_dependent_elements_p (const_tree list)
20852 {
20853 for (; list; list = TREE_CHAIN (list))
20854 if (value_dependent_expression_p (TREE_VALUE (list)))
20855 return true;
20856
20857 return false;
20858 }
20859
20860 /* Returns TRUE if the ARG (a template argument) is dependent. */
20861
20862 bool
20863 dependent_template_arg_p (tree arg)
20864 {
20865 if (!processing_template_decl)
20866 return false;
20867
20868 /* Assume a template argument that was wrongly written by the user
20869 is dependent. This is consistent with what
20870 any_dependent_template_arguments_p [that calls this function]
20871 does. */
20872 if (!arg || arg == error_mark_node)
20873 return true;
20874
20875 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
20876 arg = ARGUMENT_PACK_SELECT_ARG (arg);
20877
20878 if (TREE_CODE (arg) == TEMPLATE_DECL
20879 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
20880 return dependent_template_p (arg);
20881 else if (ARGUMENT_PACK_P (arg))
20882 {
20883 tree args = ARGUMENT_PACK_ARGS (arg);
20884 int i, len = TREE_VEC_LENGTH (args);
20885 for (i = 0; i < len; ++i)
20886 {
20887 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
20888 return true;
20889 }
20890
20891 return false;
20892 }
20893 else if (TYPE_P (arg))
20894 return dependent_type_p (arg);
20895 else
20896 return (type_dependent_expression_p (arg)
20897 || value_dependent_expression_p (arg));
20898 }
20899
20900 /* Returns true if ARGS (a collection of template arguments) contains
20901 any types that require structural equality testing. */
20902
20903 bool
20904 any_template_arguments_need_structural_equality_p (tree args)
20905 {
20906 int i;
20907 int j;
20908
20909 if (!args)
20910 return false;
20911 if (args == error_mark_node)
20912 return true;
20913
20914 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20915 {
20916 tree level = TMPL_ARGS_LEVEL (args, i + 1);
20917 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20918 {
20919 tree arg = TREE_VEC_ELT (level, j);
20920 tree packed_args = NULL_TREE;
20921 int k, len = 1;
20922
20923 if (ARGUMENT_PACK_P (arg))
20924 {
20925 /* Look inside the argument pack. */
20926 packed_args = ARGUMENT_PACK_ARGS (arg);
20927 len = TREE_VEC_LENGTH (packed_args);
20928 }
20929
20930 for (k = 0; k < len; ++k)
20931 {
20932 if (packed_args)
20933 arg = TREE_VEC_ELT (packed_args, k);
20934
20935 if (error_operand_p (arg))
20936 return true;
20937 else if (TREE_CODE (arg) == TEMPLATE_DECL)
20938 continue;
20939 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
20940 return true;
20941 else if (!TYPE_P (arg) && TREE_TYPE (arg)
20942 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
20943 return true;
20944 }
20945 }
20946 }
20947
20948 return false;
20949 }
20950
20951 /* Returns true if ARGS (a collection of template arguments) contains
20952 any dependent arguments. */
20953
20954 bool
20955 any_dependent_template_arguments_p (const_tree args)
20956 {
20957 int i;
20958 int j;
20959
20960 if (!args)
20961 return false;
20962 if (args == error_mark_node)
20963 return true;
20964
20965 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
20966 {
20967 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
20968 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
20969 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
20970 return true;
20971 }
20972
20973 return false;
20974 }
20975
20976 /* Returns TRUE if the template TMPL is dependent. */
20977
20978 bool
20979 dependent_template_p (tree tmpl)
20980 {
20981 if (TREE_CODE (tmpl) == OVERLOAD)
20982 {
20983 while (tmpl)
20984 {
20985 if (dependent_template_p (OVL_CURRENT (tmpl)))
20986 return true;
20987 tmpl = OVL_NEXT (tmpl);
20988 }
20989 return false;
20990 }
20991
20992 /* Template template parameters are dependent. */
20993 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
20994 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
20995 return true;
20996 /* So are names that have not been looked up. */
20997 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
20998 return true;
20999 /* So are member templates of dependent classes. */
21000 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
21001 return dependent_type_p (DECL_CONTEXT (tmpl));
21002 return false;
21003 }
21004
21005 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
21006
21007 bool
21008 dependent_template_id_p (tree tmpl, tree args)
21009 {
21010 return (dependent_template_p (tmpl)
21011 || any_dependent_template_arguments_p (args));
21012 }
21013
21014 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
21015 is dependent. */
21016
21017 bool
21018 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
21019 {
21020 int i;
21021
21022 if (!processing_template_decl)
21023 return false;
21024
21025 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
21026 {
21027 tree decl = TREE_VEC_ELT (declv, i);
21028 tree init = TREE_VEC_ELT (initv, i);
21029 tree cond = TREE_VEC_ELT (condv, i);
21030 tree incr = TREE_VEC_ELT (incrv, i);
21031
21032 if (type_dependent_expression_p (decl))
21033 return true;
21034
21035 if (init && type_dependent_expression_p (init))
21036 return true;
21037
21038 if (type_dependent_expression_p (cond))
21039 return true;
21040
21041 if (COMPARISON_CLASS_P (cond)
21042 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
21043 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
21044 return true;
21045
21046 if (TREE_CODE (incr) == MODOP_EXPR)
21047 {
21048 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
21049 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
21050 return true;
21051 }
21052 else if (type_dependent_expression_p (incr))
21053 return true;
21054 else if (TREE_CODE (incr) == MODIFY_EXPR)
21055 {
21056 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
21057 return true;
21058 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
21059 {
21060 tree t = TREE_OPERAND (incr, 1);
21061 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
21062 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
21063 return true;
21064 }
21065 }
21066 }
21067
21068 return false;
21069 }
21070
21071 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
21072 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
21073 no such TYPE can be found. Note that this function peers inside
21074 uninstantiated templates and therefore should be used only in
21075 extremely limited situations. ONLY_CURRENT_P restricts this
21076 peering to the currently open classes hierarchy (which is required
21077 when comparing types). */
21078
21079 tree
21080 resolve_typename_type (tree type, bool only_current_p)
21081 {
21082 tree scope;
21083 tree name;
21084 tree decl;
21085 int quals;
21086 tree pushed_scope;
21087 tree result;
21088
21089 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
21090
21091 scope = TYPE_CONTEXT (type);
21092 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
21093 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
21094 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
21095 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
21096 identifier of the TYPENAME_TYPE anymore.
21097 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
21098 TYPENAME_TYPE instead, we avoid messing up with a possible
21099 typedef variant case. */
21100 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
21101
21102 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
21103 it first before we can figure out what NAME refers to. */
21104 if (TREE_CODE (scope) == TYPENAME_TYPE)
21105 {
21106 if (TYPENAME_IS_RESOLVING_P (scope))
21107 /* Given a class template A with a dependent base with nested type C,
21108 typedef typename A::C::C C will land us here, as trying to resolve
21109 the initial A::C leads to the local C typedef, which leads back to
21110 A::C::C. So we break the recursion now. */
21111 return type;
21112 else
21113 scope = resolve_typename_type (scope, only_current_p);
21114 }
21115 /* If we don't know what SCOPE refers to, then we cannot resolve the
21116 TYPENAME_TYPE. */
21117 if (TREE_CODE (scope) == TYPENAME_TYPE)
21118 return type;
21119 /* If the SCOPE is a template type parameter, we have no way of
21120 resolving the name. */
21121 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
21122 return type;
21123 /* If the SCOPE is not the current instantiation, there's no reason
21124 to look inside it. */
21125 if (only_current_p && !currently_open_class (scope))
21126 return type;
21127 /* If this is a typedef, we don't want to look inside (c++/11987). */
21128 if (typedef_variant_p (type))
21129 return type;
21130 /* If SCOPE isn't the template itself, it will not have a valid
21131 TYPE_FIELDS list. */
21132 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
21133 /* scope is either the template itself or a compatible instantiation
21134 like X<T>, so look up the name in the original template. */
21135 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
21136 else
21137 /* scope is a partial instantiation, so we can't do the lookup or we
21138 will lose the template arguments. */
21139 return type;
21140 /* Enter the SCOPE so that name lookup will be resolved as if we
21141 were in the class definition. In particular, SCOPE will no
21142 longer be considered a dependent type. */
21143 pushed_scope = push_scope (scope);
21144 /* Look up the declaration. */
21145 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
21146 tf_warning_or_error);
21147
21148 result = NULL_TREE;
21149
21150 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
21151 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
21152 if (!decl)
21153 /*nop*/;
21154 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
21155 && TREE_CODE (decl) == TYPE_DECL)
21156 {
21157 result = TREE_TYPE (decl);
21158 if (result == error_mark_node)
21159 result = NULL_TREE;
21160 }
21161 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
21162 && DECL_CLASS_TEMPLATE_P (decl))
21163 {
21164 tree tmpl;
21165 tree args;
21166 /* Obtain the template and the arguments. */
21167 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
21168 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
21169 /* Instantiate the template. */
21170 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
21171 /*entering_scope=*/0,
21172 tf_error | tf_user);
21173 if (result == error_mark_node)
21174 result = NULL_TREE;
21175 }
21176
21177 /* Leave the SCOPE. */
21178 if (pushed_scope)
21179 pop_scope (pushed_scope);
21180
21181 /* If we failed to resolve it, return the original typename. */
21182 if (!result)
21183 return type;
21184
21185 /* If lookup found a typename type, resolve that too. */
21186 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
21187 {
21188 /* Ill-formed programs can cause infinite recursion here, so we
21189 must catch that. */
21190 TYPENAME_IS_RESOLVING_P (type) = 1;
21191 result = resolve_typename_type (result, only_current_p);
21192 TYPENAME_IS_RESOLVING_P (type) = 0;
21193 }
21194
21195 /* Qualify the resulting type. */
21196 quals = cp_type_quals (type);
21197 if (quals)
21198 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
21199
21200 return result;
21201 }
21202
21203 /* EXPR is an expression which is not type-dependent. Return a proxy
21204 for EXPR that can be used to compute the types of larger
21205 expressions containing EXPR. */
21206
21207 tree
21208 build_non_dependent_expr (tree expr)
21209 {
21210 tree inner_expr;
21211
21212 #ifdef ENABLE_CHECKING
21213 /* Try to get a constant value for all non-dependent expressions in
21214 order to expose bugs in *_dependent_expression_p and constexpr. */
21215 if (cxx_dialect >= cxx11)
21216 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
21217 #endif
21218
21219 /* Preserve OVERLOADs; the functions must be available to resolve
21220 types. */
21221 inner_expr = expr;
21222 if (TREE_CODE (inner_expr) == STMT_EXPR)
21223 inner_expr = stmt_expr_value_expr (inner_expr);
21224 if (TREE_CODE (inner_expr) == ADDR_EXPR)
21225 inner_expr = TREE_OPERAND (inner_expr, 0);
21226 if (TREE_CODE (inner_expr) == COMPONENT_REF)
21227 inner_expr = TREE_OPERAND (inner_expr, 1);
21228 if (is_overloaded_fn (inner_expr)
21229 || TREE_CODE (inner_expr) == OFFSET_REF)
21230 return expr;
21231 /* There is no need to return a proxy for a variable. */
21232 if (VAR_P (expr))
21233 return expr;
21234 /* Preserve string constants; conversions from string constants to
21235 "char *" are allowed, even though normally a "const char *"
21236 cannot be used to initialize a "char *". */
21237 if (TREE_CODE (expr) == STRING_CST)
21238 return expr;
21239 /* Preserve arithmetic constants, as an optimization -- there is no
21240 reason to create a new node. */
21241 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
21242 return expr;
21243 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
21244 There is at least one place where we want to know that a
21245 particular expression is a throw-expression: when checking a ?:
21246 expression, there are special rules if the second or third
21247 argument is a throw-expression. */
21248 if (TREE_CODE (expr) == THROW_EXPR)
21249 return expr;
21250
21251 /* Don't wrap an initializer list, we need to be able to look inside. */
21252 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
21253 return expr;
21254
21255 /* Don't wrap a dummy object, we need to be able to test for it. */
21256 if (is_dummy_object (expr))
21257 return expr;
21258
21259 if (TREE_CODE (expr) == COND_EXPR)
21260 return build3 (COND_EXPR,
21261 TREE_TYPE (expr),
21262 TREE_OPERAND (expr, 0),
21263 (TREE_OPERAND (expr, 1)
21264 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
21265 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
21266 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
21267 if (TREE_CODE (expr) == COMPOUND_EXPR
21268 && !COMPOUND_EXPR_OVERLOADED (expr))
21269 return build2 (COMPOUND_EXPR,
21270 TREE_TYPE (expr),
21271 TREE_OPERAND (expr, 0),
21272 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
21273
21274 /* If the type is unknown, it can't really be non-dependent */
21275 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
21276
21277 /* Otherwise, build a NON_DEPENDENT_EXPR. */
21278 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
21279 }
21280
21281 /* ARGS is a vector of expressions as arguments to a function call.
21282 Replace the arguments with equivalent non-dependent expressions.
21283 This modifies ARGS in place. */
21284
21285 void
21286 make_args_non_dependent (vec<tree, va_gc> *args)
21287 {
21288 unsigned int ix;
21289 tree arg;
21290
21291 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
21292 {
21293 tree newarg = build_non_dependent_expr (arg);
21294 if (newarg != arg)
21295 (*args)[ix] = newarg;
21296 }
21297 }
21298
21299 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
21300 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
21301 parms. */
21302
21303 static tree
21304 make_auto_1 (tree name)
21305 {
21306 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
21307 TYPE_NAME (au) = build_decl (input_location,
21308 TYPE_DECL, name, au);
21309 TYPE_STUB_DECL (au) = TYPE_NAME (au);
21310 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
21311 (0, processing_template_decl + 1, processing_template_decl + 1,
21312 TYPE_NAME (au), NULL_TREE);
21313 TYPE_CANONICAL (au) = canonical_type_parameter (au);
21314 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
21315 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
21316
21317 return au;
21318 }
21319
21320 tree
21321 make_decltype_auto (void)
21322 {
21323 return make_auto_1 (get_identifier ("decltype(auto)"));
21324 }
21325
21326 tree
21327 make_auto (void)
21328 {
21329 return make_auto_1 (get_identifier ("auto"));
21330 }
21331
21332 /* Given type ARG, return std::initializer_list<ARG>. */
21333
21334 static tree
21335 listify (tree arg)
21336 {
21337 tree std_init_list = namespace_binding
21338 (get_identifier ("initializer_list"), std_node);
21339 tree argvec;
21340 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
21341 {
21342 error ("deducing from brace-enclosed initializer list requires "
21343 "#include <initializer_list>");
21344 return error_mark_node;
21345 }
21346 argvec = make_tree_vec (1);
21347 TREE_VEC_ELT (argvec, 0) = arg;
21348 return lookup_template_class (std_init_list, argvec, NULL_TREE,
21349 NULL_TREE, 0, tf_warning_or_error);
21350 }
21351
21352 /* Replace auto in TYPE with std::initializer_list<auto>. */
21353
21354 static tree
21355 listify_autos (tree type, tree auto_node)
21356 {
21357 tree init_auto = listify (auto_node);
21358 tree argvec = make_tree_vec (1);
21359 TREE_VEC_ELT (argvec, 0) = init_auto;
21360 if (processing_template_decl)
21361 argvec = add_to_template_args (current_template_args (), argvec);
21362 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21363 }
21364
21365 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
21366 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
21367
21368 tree
21369 do_auto_deduction (tree type, tree init, tree auto_node)
21370 {
21371 tree targs;
21372
21373 if (init == error_mark_node)
21374 return error_mark_node;
21375
21376 if (type_dependent_expression_p (init))
21377 /* Defining a subset of type-dependent expressions that we can deduce
21378 from ahead of time isn't worth the trouble. */
21379 return type;
21380
21381 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
21382 with either a new invented type template parameter U or, if the
21383 initializer is a braced-init-list (8.5.4), with
21384 std::initializer_list<U>. */
21385 if (BRACE_ENCLOSED_INITIALIZER_P (init))
21386 type = listify_autos (type, auto_node);
21387
21388 init = resolve_nondeduced_context (init);
21389
21390 targs = make_tree_vec (1);
21391 if (AUTO_IS_DECLTYPE (auto_node))
21392 {
21393 bool id = (DECL_P (init) || TREE_CODE (init) == COMPONENT_REF);
21394 TREE_VEC_ELT (targs, 0)
21395 = finish_decltype_type (init, id, tf_warning_or_error);
21396 if (type != auto_node)
21397 {
21398 error ("%qT as type rather than plain %<decltype(auto)%>", type);
21399 return error_mark_node;
21400 }
21401 }
21402 else
21403 {
21404 tree parms = build_tree_list (NULL_TREE, type);
21405 tree tparms = make_tree_vec (1);
21406 int val;
21407
21408 TREE_VEC_ELT (tparms, 0)
21409 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
21410 val = type_unification_real (tparms, targs, parms, &init, 1, 0,
21411 DEDUCE_CALL, LOOKUP_NORMAL,
21412 NULL, /*explain_p=*/false);
21413 if (val > 0)
21414 {
21415 if (processing_template_decl)
21416 /* Try again at instantiation time. */
21417 return type;
21418 if (type && type != error_mark_node)
21419 /* If type is error_mark_node a diagnostic must have been
21420 emitted by now. Also, having a mention to '<type error>'
21421 in the diagnostic is not really useful to the user. */
21422 {
21423 if (cfun && auto_node == current_function_auto_return_pattern
21424 && LAMBDA_FUNCTION_P (current_function_decl))
21425 error ("unable to deduce lambda return type from %qE", init);
21426 else
21427 error ("unable to deduce %qT from %qE", type, init);
21428 }
21429 return error_mark_node;
21430 }
21431 }
21432
21433 /* If the list of declarators contains more than one declarator, the type
21434 of each declared variable is determined as described above. If the
21435 type deduced for the template parameter U is not the same in each
21436 deduction, the program is ill-formed. */
21437 if (TREE_TYPE (auto_node)
21438 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
21439 {
21440 if (cfun && auto_node == current_function_auto_return_pattern
21441 && LAMBDA_FUNCTION_P (current_function_decl))
21442 error ("inconsistent types %qT and %qT deduced for "
21443 "lambda return type", TREE_TYPE (auto_node),
21444 TREE_VEC_ELT (targs, 0));
21445 else
21446 error ("inconsistent deduction for %qT: %qT and then %qT",
21447 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
21448 return error_mark_node;
21449 }
21450 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
21451
21452 if (processing_template_decl)
21453 targs = add_to_template_args (current_template_args (), targs);
21454 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
21455 }
21456
21457 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
21458 result. */
21459
21460 tree
21461 splice_late_return_type (tree type, tree late_return_type)
21462 {
21463 tree argvec;
21464
21465 if (late_return_type == NULL_TREE)
21466 return type;
21467 argvec = make_tree_vec (1);
21468 TREE_VEC_ELT (argvec, 0) = late_return_type;
21469 if (processing_template_parmlist)
21470 /* For a late-specified return type in a template type-parameter, we
21471 need to add a dummy argument level for its parmlist. */
21472 argvec = add_to_template_args
21473 (make_tree_vec (processing_template_parmlist), argvec);
21474 if (current_template_parms)
21475 argvec = add_to_template_args (current_template_args (), argvec);
21476 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
21477 }
21478
21479 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
21480 'decltype(auto)'. */
21481
21482 bool
21483 is_auto (const_tree type)
21484 {
21485 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21486 && (TYPE_IDENTIFIER (type) == get_identifier ("auto")
21487 || TYPE_IDENTIFIER (type) == get_identifier ("decltype(auto)")))
21488 return true;
21489 else
21490 return false;
21491 }
21492
21493 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
21494 a use of `auto'. Returns NULL_TREE otherwise. */
21495
21496 tree
21497 type_uses_auto (tree type)
21498 {
21499 return find_type_usage (type, is_auto);
21500 }
21501
21502 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
21503 'decltype(auto)' or a concept. */
21504
21505 bool
21506 is_auto_or_concept (const_tree type)
21507 {
21508 return is_auto (type); // or concept
21509 }
21510
21511 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
21512 a concept identifier) iff TYPE contains a use of a generic type. Returns
21513 NULL_TREE otherwise. */
21514
21515 tree
21516 type_uses_auto_or_concept (tree type)
21517 {
21518 return find_type_usage (type, is_auto_or_concept);
21519 }
21520
21521
21522 /* For a given template T, return the vector of typedefs referenced
21523 in T for which access check is needed at T instantiation time.
21524 T is either a FUNCTION_DECL or a RECORD_TYPE.
21525 Those typedefs were added to T by the function
21526 append_type_to_template_for_access_check. */
21527
21528 vec<qualified_typedef_usage_t, va_gc> *
21529 get_types_needing_access_check (tree t)
21530 {
21531 tree ti;
21532 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
21533
21534 if (!t || t == error_mark_node)
21535 return NULL;
21536
21537 if (!(ti = get_template_info (t)))
21538 return NULL;
21539
21540 if (CLASS_TYPE_P (t)
21541 || TREE_CODE (t) == FUNCTION_DECL)
21542 {
21543 if (!TI_TEMPLATE (ti))
21544 return NULL;
21545
21546 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
21547 }
21548
21549 return result;
21550 }
21551
21552 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
21553 tied to T. That list of typedefs will be access checked at
21554 T instantiation time.
21555 T is either a FUNCTION_DECL or a RECORD_TYPE.
21556 TYPE_DECL is a TYPE_DECL node representing a typedef.
21557 SCOPE is the scope through which TYPE_DECL is accessed.
21558 LOCATION is the location of the usage point of TYPE_DECL.
21559
21560 This function is a subroutine of
21561 append_type_to_template_for_access_check. */
21562
21563 static void
21564 append_type_to_template_for_access_check_1 (tree t,
21565 tree type_decl,
21566 tree scope,
21567 location_t location)
21568 {
21569 qualified_typedef_usage_t typedef_usage;
21570 tree ti;
21571
21572 if (!t || t == error_mark_node)
21573 return;
21574
21575 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
21576 || CLASS_TYPE_P (t))
21577 && type_decl
21578 && TREE_CODE (type_decl) == TYPE_DECL
21579 && scope);
21580
21581 if (!(ti = get_template_info (t)))
21582 return;
21583
21584 gcc_assert (TI_TEMPLATE (ti));
21585
21586 typedef_usage.typedef_decl = type_decl;
21587 typedef_usage.context = scope;
21588 typedef_usage.locus = location;
21589
21590 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
21591 }
21592
21593 /* Append TYPE_DECL to the template TEMPL.
21594 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
21595 At TEMPL instanciation time, TYPE_DECL will be checked to see
21596 if it can be accessed through SCOPE.
21597 LOCATION is the location of the usage point of TYPE_DECL.
21598
21599 e.g. consider the following code snippet:
21600
21601 class C
21602 {
21603 typedef int myint;
21604 };
21605
21606 template<class U> struct S
21607 {
21608 C::myint mi; // <-- usage point of the typedef C::myint
21609 };
21610
21611 S<char> s;
21612
21613 At S<char> instantiation time, we need to check the access of C::myint
21614 In other words, we need to check the access of the myint typedef through
21615 the C scope. For that purpose, this function will add the myint typedef
21616 and the scope C through which its being accessed to a list of typedefs
21617 tied to the template S. That list will be walked at template instantiation
21618 time and access check performed on each typedefs it contains.
21619 Note that this particular code snippet should yield an error because
21620 myint is private to C. */
21621
21622 void
21623 append_type_to_template_for_access_check (tree templ,
21624 tree type_decl,
21625 tree scope,
21626 location_t location)
21627 {
21628 qualified_typedef_usage_t *iter;
21629 unsigned i;
21630
21631 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
21632
21633 /* Make sure we don't append the type to the template twice. */
21634 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
21635 if (iter->typedef_decl == type_decl && scope == iter->context)
21636 return;
21637
21638 append_type_to_template_for_access_check_1 (templ, type_decl,
21639 scope, location);
21640 }
21641
21642 /* Convert the generic type parameters in PARM that match the types given in the
21643 range [START_IDX, END_IDX) from the current_template_parms into generic type
21644 packs. */
21645
21646 tree
21647 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
21648 {
21649 tree current = current_template_parms;
21650 int depth = TMPL_PARMS_DEPTH (current);
21651 current = INNERMOST_TEMPLATE_PARMS (current);
21652 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
21653
21654 for (int i = 0; i < start_idx; ++i)
21655 TREE_VEC_ELT (replacement, i)
21656 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21657
21658 for (int i = start_idx; i < end_idx; ++i)
21659 {
21660 /* Create a distinct parameter pack type from the current parm and add it
21661 to the replacement args to tsubst below into the generic function
21662 parameter. */
21663
21664 tree o = TREE_TYPE (TREE_VALUE
21665 (TREE_VEC_ELT (current, i)));
21666 tree t = copy_type (o);
21667 TEMPLATE_TYPE_PARM_INDEX (t)
21668 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
21669 o, 0, 0, tf_none);
21670 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
21671 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
21672 TYPE_MAIN_VARIANT (t) = t;
21673 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
21674 TYPE_CANONICAL (t) = canonical_type_parameter (t);
21675 TREE_VEC_ELT (replacement, i) = t;
21676 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
21677 }
21678
21679 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
21680 TREE_VEC_ELT (replacement, i)
21681 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
21682
21683 /* If there are more levels then build up the replacement with the outer
21684 template parms. */
21685 if (depth > 1)
21686 replacement = add_to_template_args (template_parms_to_args
21687 (TREE_CHAIN (current_template_parms)),
21688 replacement);
21689
21690 return tsubst (parm, replacement, tf_none, NULL_TREE);
21691 }
21692
21693
21694 /* Set up the hash tables for template instantiations. */
21695
21696 void
21697 init_template_processing (void)
21698 {
21699 decl_specializations = htab_create_ggc (37,
21700 hash_specialization,
21701 eq_specializations,
21702 ggc_free);
21703 type_specializations = htab_create_ggc (37,
21704 hash_specialization,
21705 eq_specializations,
21706 ggc_free);
21707 }
21708
21709 /* Print stats about the template hash tables for -fstats. */
21710
21711 void
21712 print_template_statistics (void)
21713 {
21714 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
21715 "%f collisions\n", (long) htab_size (decl_specializations),
21716 (long) htab_elements (decl_specializations),
21717 htab_collisions (decl_specializations));
21718 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
21719 "%f collisions\n", (long) htab_size (type_specializations),
21720 (long) htab_elements (type_specializations),
21721 htab_collisions (type_specializations));
21722 }
21723
21724 #include "gt-cp-pt.h"