* pt.c (maybe_process_partial_specialization): Handle aliases first.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 /* Known bugs or deficiencies include:
25
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "intl.h"
35 #include "pointer-set.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
42 #include "decl.h"
43 #include "toplev.h"
44 #include "timevar.h"
45 #include "tree-iterator.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 /* A map from local variable declarations in the body of the template
78 presently being instantiated to the corresponding instantiated
79 local variables. */
80 static struct pointer_map_t *local_specializations;
81
82 /* True if we've recursed into fn_type_unification too many times. */
83 static bool excessive_deduction_depth;
84
85 typedef struct GTY(()) spec_entry
86 {
87 tree tmpl;
88 tree args;
89 tree spec;
90 } spec_entry;
91
92 static GTY ((param_is (spec_entry)))
93 htab_t decl_specializations;
94
95 static GTY ((param_is (spec_entry)))
96 htab_t type_specializations;
97
98 /* Contains canonical template parameter types. The vector is indexed by
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
103
104 #define UNIFY_ALLOW_NONE 0
105 #define UNIFY_ALLOW_MORE_CV_QUAL 1
106 #define UNIFY_ALLOW_LESS_CV_QUAL 2
107 #define UNIFY_ALLOW_DERIVED 4
108 #define UNIFY_ALLOW_INTEGER 8
109 #define UNIFY_ALLOW_OUTER_LEVEL 16
110 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112
113 enum template_base_result {
114 tbr_incomplete_type,
115 tbr_ambiguous_baseclass,
116 tbr_success
117 };
118
119 static void push_access_scope (tree);
120 static void pop_access_scope (tree);
121 static bool resolve_overloaded_unification (tree, tree, tree, tree,
122 unification_kind_t, int,
123 bool);
124 static int try_one_overload (tree, tree, tree, tree, tree,
125 unification_kind_t, int, bool, bool);
126 static int unify (tree, tree, tree, tree, int, bool);
127 static void add_pending_template (tree);
128 static tree reopen_tinst_level (struct tinst_level *);
129 static tree tsubst_initializer_list (tree, tree);
130 static tree get_class_bindings (tree, tree, tree, tree);
131 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
132 bool, bool);
133 static void tsubst_enum (tree, tree, tree);
134 static tree add_to_template_args (tree, tree);
135 static tree add_outermost_template_args (tree, tree);
136 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
137 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
138 tree);
139 static int type_unification_real (tree, tree, tree, const tree *,
140 unsigned int, int, unification_kind_t, int,
141 bool);
142 static void note_template_header (int);
143 static tree convert_nontype_argument_function (tree, tree);
144 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
145 static tree convert_template_argument (tree, tree, tree,
146 tsubst_flags_t, int, tree);
147 static int for_each_template_parm (tree, tree_fn_t, void*,
148 struct pointer_set_t*, bool);
149 static tree expand_template_argument_pack (tree);
150 static tree build_template_parm_index (int, int, int, tree, tree);
151 static bool inline_needs_template_parms (tree);
152 static void push_inline_template_parms_recursive (tree, int);
153 static tree retrieve_local_specialization (tree);
154 static void register_local_specialization (tree, tree);
155 static hashval_t hash_specialization (const void *p);
156 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
157 static int mark_template_parm (tree, void *);
158 static int template_parm_this_level_p (tree, void *);
159 static tree tsubst_friend_function (tree, tree);
160 static tree tsubst_friend_class (tree, tree);
161 static int can_complete_type_without_circularity (tree);
162 static tree get_bindings (tree, tree, tree, bool);
163 static int template_decl_level (tree);
164 static int check_cv_quals_for_unify (int, tree, tree);
165 static void template_parm_level_and_index (tree, int*, int*);
166 static int unify_pack_expansion (tree, tree, tree,
167 tree, unification_kind_t, bool, bool);
168 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
169 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
170 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
171 static void regenerate_decl_from_template (tree, tree);
172 static tree most_specialized_class (tree, tree, tsubst_flags_t);
173 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
174 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
175 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
176 static bool check_specialization_scope (void);
177 static tree process_partial_specialization (tree);
178 static void set_current_access_from_decl (tree);
179 static enum template_base_result get_template_base (tree, tree, tree, tree,
180 bool , tree *);
181 static tree try_class_unification (tree, tree, tree, tree, bool);
182 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
183 tree, tree);
184 static bool template_template_parm_bindings_ok_p (tree, tree);
185 static int template_args_equal (tree, tree);
186 static void tsubst_default_arguments (tree);
187 static tree for_each_template_parm_r (tree *, int *, void *);
188 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
189 static void copy_default_args_to_explicit_spec (tree);
190 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
191 static bool dependent_template_arg_p (tree);
192 static bool any_template_arguments_need_structural_equality_p (tree);
193 static bool dependent_type_p_r (tree);
194 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
195 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
197 static tree tsubst_decl (tree, tree, tsubst_flags_t);
198 static void perform_typedefs_access_check (tree tmpl, tree targs);
199 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
200 location_t);
201 static tree listify (tree);
202 static tree listify_autos (tree, tree);
203 static tree template_parm_to_arg (tree t);
204 static bool arg_from_parm_pack_p (tree, tree);
205 static tree current_template_args (void);
206 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
207 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
208
209 /* Make the current scope suitable for access checking when we are
210 processing T. T can be FUNCTION_DECL for instantiated function
211 template, VAR_DECL for static member variable, or TYPE_DECL for
212 alias template (needed by instantiate_decl). */
213
214 static void
215 push_access_scope (tree t)
216 {
217 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
218 || TREE_CODE (t) == TYPE_DECL
219 || TREE_CODE (t) == VAR_DECL);
220
221 if (DECL_FRIEND_CONTEXT (t))
222 push_nested_class (DECL_FRIEND_CONTEXT (t));
223 else if (DECL_CLASS_SCOPE_P (t))
224 push_nested_class (DECL_CONTEXT (t));
225 else
226 push_to_top_level ();
227
228 if (TREE_CODE (t) == FUNCTION_DECL)
229 {
230 saved_access_scope = tree_cons
231 (NULL_TREE, current_function_decl, saved_access_scope);
232 current_function_decl = t;
233 }
234 }
235
236 /* Restore the scope set up by push_access_scope. T is the node we
237 are processing. */
238
239 static void
240 pop_access_scope (tree t)
241 {
242 if (TREE_CODE (t) == FUNCTION_DECL)
243 {
244 current_function_decl = TREE_VALUE (saved_access_scope);
245 saved_access_scope = TREE_CHAIN (saved_access_scope);
246 }
247
248 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
249 pop_nested_class ();
250 else
251 pop_from_top_level ();
252 }
253
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
258
259 tree
260 finish_member_template_decl (tree decl)
261 {
262 if (decl == error_mark_node)
263 return error_mark_node;
264
265 gcc_assert (DECL_P (decl));
266
267 if (TREE_CODE (decl) == TYPE_DECL)
268 {
269 tree type;
270
271 type = TREE_TYPE (decl);
272 if (type == error_mark_node)
273 return error_mark_node;
274 if (MAYBE_CLASS_TYPE_P (type)
275 && CLASSTYPE_TEMPLATE_INFO (type)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
277 {
278 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
279 check_member_template (tmpl);
280 return tmpl;
281 }
282 return NULL_TREE;
283 }
284 else if (TREE_CODE (decl) == FIELD_DECL)
285 error ("data member %qD cannot be a member template", decl);
286 else if (DECL_TEMPLATE_INFO (decl))
287 {
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
289 {
290 check_member_template (DECL_TI_TEMPLATE (decl));
291 return DECL_TI_TEMPLATE (decl);
292 }
293 else
294 return decl;
295 }
296 else
297 error ("invalid member template declaration %qD", decl);
298
299 return error_mark_node;
300 }
301
302 /* Create a template info node. */
303
304 tree
305 build_template_info (tree template_decl, tree template_args)
306 {
307 tree result = make_node (TEMPLATE_INFO);
308 TI_TEMPLATE (result) = template_decl;
309 TI_ARGS (result) = template_args;
310 return result;
311 }
312
313 /* Return the template info node corresponding to T, whatever T is. */
314
315 tree
316 get_template_info (const_tree t)
317 {
318 tree tinfo = NULL_TREE;
319
320 if (!t || t == error_mark_node)
321 return NULL;
322
323 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
324 tinfo = DECL_TEMPLATE_INFO (t);
325
326 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
327 t = TREE_TYPE (t);
328
329 if (TAGGED_TYPE_P (t))
330 tinfo = TYPE_TEMPLATE_INFO (t);
331 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
332 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
333
334 return tinfo;
335 }
336
337 /* Returns the template nesting level of the indicated class TYPE.
338
339 For example, in:
340 template <class T>
341 struct A
342 {
343 template <class U>
344 struct B {};
345 };
346
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
350
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
353 always safe. */
354
355 int
356 template_class_depth (tree type)
357 {
358 int depth;
359
360 for (depth = 0;
361 type && TREE_CODE (type) != NAMESPACE_DECL;
362 type = (TREE_CODE (type) == FUNCTION_DECL)
363 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
364 {
365 tree tinfo = get_template_info (type);
366
367 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
369 ++depth;
370 }
371
372 return depth;
373 }
374
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
377
378 static bool
379 inline_needs_template_parms (tree decl)
380 {
381 if (! DECL_TEMPLATE_INFO (decl))
382 return false;
383
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
385 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
386 }
387
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
391 innermost first. */
392
393 static void
394 push_inline_template_parms_recursive (tree parmlist, int levels)
395 {
396 tree parms = TREE_VALUE (parmlist);
397 int i;
398
399 if (levels > 1)
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
401
402 ++processing_template_decl;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl),
405 parms, current_template_parms);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
407
408 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
409 NULL);
410 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
411 {
412 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
413
414 if (parm == error_mark_node)
415 continue;
416
417 gcc_assert (DECL_P (parm));
418
419 switch (TREE_CODE (parm))
420 {
421 case TYPE_DECL:
422 case TEMPLATE_DECL:
423 pushdecl (parm);
424 break;
425
426 case PARM_DECL:
427 {
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
431 available. */
432 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
433 CONST_DECL, DECL_NAME (parm),
434 TREE_TYPE (parm));
435 DECL_ARTIFICIAL (decl) = 1;
436 TREE_CONSTANT (decl) = 1;
437 TREE_READONLY (decl) = 1;
438 DECL_INITIAL (decl) = DECL_INITIAL (parm);
439 SET_DECL_TEMPLATE_PARM_P (decl);
440 pushdecl (decl);
441 }
442 break;
443
444 default:
445 gcc_unreachable ();
446 }
447 }
448 }
449
450 /* Restore the template parameter context for a member template or
451 a friend template defined in a class definition. */
452
453 void
454 maybe_begin_member_template_processing (tree decl)
455 {
456 tree parms;
457 int levels = 0;
458
459 if (inline_needs_template_parms (decl))
460 {
461 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
462 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
463
464 if (DECL_TEMPLATE_SPECIALIZATION (decl))
465 {
466 --levels;
467 parms = TREE_CHAIN (parms);
468 }
469
470 push_inline_template_parms_recursive (parms, levels);
471 }
472
473 /* Remember how many levels of template parameters we pushed so that
474 we can pop them later. */
475 inline_parm_levels.safe_push (levels);
476 }
477
478 /* Undo the effects of maybe_begin_member_template_processing. */
479
480 void
481 maybe_end_member_template_processing (void)
482 {
483 int i;
484 int last;
485
486 if (inline_parm_levels.length () == 0)
487 return;
488
489 last = inline_parm_levels.pop ();
490 for (i = 0; i < last; ++i)
491 {
492 --processing_template_decl;
493 current_template_parms = TREE_CHAIN (current_template_parms);
494 poplevel (0, 0, 0);
495 }
496 }
497
498 /* Return a new template argument vector which contains all of ARGS,
499 but has as its innermost set of arguments the EXTRA_ARGS. */
500
501 static tree
502 add_to_template_args (tree args, tree extra_args)
503 {
504 tree new_args;
505 int extra_depth;
506 int i;
507 int j;
508
509 if (args == NULL_TREE || extra_args == error_mark_node)
510 return extra_args;
511
512 extra_depth = TMPL_ARGS_DEPTH (extra_args);
513 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
514
515 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
516 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
517
518 for (j = 1; j <= extra_depth; ++j, ++i)
519 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
520
521 return new_args;
522 }
523
524 /* Like add_to_template_args, but only the outermost ARGS are added to
525 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
526 (EXTRA_ARGS) levels are added. This function is used to combine
527 the template arguments from a partial instantiation with the
528 template arguments used to attain the full instantiation from the
529 partial instantiation. */
530
531 static tree
532 add_outermost_template_args (tree args, tree extra_args)
533 {
534 tree new_args;
535
536 /* If there are more levels of EXTRA_ARGS than there are ARGS,
537 something very fishy is going on. */
538 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
539
540 /* If *all* the new arguments will be the EXTRA_ARGS, just return
541 them. */
542 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
543 return extra_args;
544
545 /* For the moment, we make ARGS look like it contains fewer levels. */
546 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
547
548 new_args = add_to_template_args (args, extra_args);
549
550 /* Now, we restore ARGS to its full dimensions. */
551 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
552
553 return new_args;
554 }
555
556 /* Return the N levels of innermost template arguments from the ARGS. */
557
558 tree
559 get_innermost_template_args (tree args, int n)
560 {
561 tree new_args;
562 int extra_levels;
563 int i;
564
565 gcc_assert (n >= 0);
566
567 /* If N is 1, just return the innermost set of template arguments. */
568 if (n == 1)
569 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
570
571 /* If we're not removing anything, just return the arguments we were
572 given. */
573 extra_levels = TMPL_ARGS_DEPTH (args) - n;
574 gcc_assert (extra_levels >= 0);
575 if (extra_levels == 0)
576 return args;
577
578 /* Make a new set of arguments, not containing the outer arguments. */
579 new_args = make_tree_vec (n);
580 for (i = 1; i <= n; ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i,
582 TMPL_ARGS_LEVEL (args, i + extra_levels));
583
584 return new_args;
585 }
586
587 /* The inverse of get_innermost_template_args: Return all but the innermost
588 EXTRA_LEVELS levels of template arguments from the ARGS. */
589
590 static tree
591 strip_innermost_template_args (tree args, int extra_levels)
592 {
593 tree new_args;
594 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
595 int i;
596
597 gcc_assert (n >= 0);
598
599 /* If N is 1, just return the outermost set of template arguments. */
600 if (n == 1)
601 return TMPL_ARGS_LEVEL (args, 1);
602
603 /* If we're not removing anything, just return the arguments we were
604 given. */
605 gcc_assert (extra_levels >= 0);
606 if (extra_levels == 0)
607 return args;
608
609 /* Make a new set of arguments, not containing the inner arguments. */
610 new_args = make_tree_vec (n);
611 for (i = 1; i <= n; ++i)
612 SET_TMPL_ARGS_LEVEL (new_args, i,
613 TMPL_ARGS_LEVEL (args, i));
614
615 return new_args;
616 }
617
618 /* We've got a template header coming up; push to a new level for storing
619 the parms. */
620
621 void
622 begin_template_parm_list (void)
623 {
624 /* We use a non-tag-transparent scope here, which causes pushtag to
625 put tags in this scope, rather than in the enclosing class or
626 namespace scope. This is the right thing, since we want
627 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
628 global template class, push_template_decl handles putting the
629 TEMPLATE_DECL into top-level scope. For a nested template class,
630 e.g.:
631
632 template <class T> struct S1 {
633 template <class T> struct S2 {};
634 };
635
636 pushtag contains special code to call pushdecl_with_scope on the
637 TEMPLATE_DECL for S2. */
638 begin_scope (sk_template_parms, NULL);
639 ++processing_template_decl;
640 ++processing_template_parmlist;
641 note_template_header (0);
642 }
643
644 /* This routine is called when a specialization is declared. If it is
645 invalid to declare a specialization here, an error is reported and
646 false is returned, otherwise this routine will return true. */
647
648 static bool
649 check_specialization_scope (void)
650 {
651 tree scope = current_scope ();
652
653 /* [temp.expl.spec]
654
655 An explicit specialization shall be declared in the namespace of
656 which the template is a member, or, for member templates, in the
657 namespace of which the enclosing class or enclosing class
658 template is a member. An explicit specialization of a member
659 function, member class or static data member of a class template
660 shall be declared in the namespace of which the class template
661 is a member. */
662 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
663 {
664 error ("explicit specialization in non-namespace scope %qD", scope);
665 return false;
666 }
667
668 /* [temp.expl.spec]
669
670 In an explicit specialization declaration for a member of a class
671 template or a member template that appears in namespace scope,
672 the member template and some of its enclosing class templates may
673 remain unspecialized, except that the declaration shall not
674 explicitly specialize a class member template if its enclosing
675 class templates are not explicitly specialized as well. */
676 if (current_template_parms)
677 {
678 error ("enclosing class templates are not explicitly specialized");
679 return false;
680 }
681
682 return true;
683 }
684
685 /* We've just seen template <>. */
686
687 bool
688 begin_specialization (void)
689 {
690 begin_scope (sk_template_spec, NULL);
691 note_template_header (1);
692 return check_specialization_scope ();
693 }
694
695 /* Called at then end of processing a declaration preceded by
696 template<>. */
697
698 void
699 end_specialization (void)
700 {
701 finish_scope ();
702 reset_specialization ();
703 }
704
705 /* Any template <>'s that we have seen thus far are not referring to a
706 function specialization. */
707
708 void
709 reset_specialization (void)
710 {
711 processing_specialization = 0;
712 template_header_count = 0;
713 }
714
715 /* We've just seen a template header. If SPECIALIZATION is nonzero,
716 it was of the form template <>. */
717
718 static void
719 note_template_header (int specialization)
720 {
721 processing_specialization = specialization;
722 template_header_count++;
723 }
724
725 /* We're beginning an explicit instantiation. */
726
727 void
728 begin_explicit_instantiation (void)
729 {
730 gcc_assert (!processing_explicit_instantiation);
731 processing_explicit_instantiation = true;
732 }
733
734
735 void
736 end_explicit_instantiation (void)
737 {
738 gcc_assert (processing_explicit_instantiation);
739 processing_explicit_instantiation = false;
740 }
741
742 /* An explicit specialization or partial specialization of TMPL is being
743 declared. Check that the namespace in which the specialization is
744 occurring is permissible. Returns false iff it is invalid to
745 specialize TMPL in the current namespace. */
746
747 static bool
748 check_specialization_namespace (tree tmpl)
749 {
750 tree tpl_ns = decl_namespace_context (tmpl);
751
752 /* [tmpl.expl.spec]
753
754 An explicit specialization shall be declared in the namespace of
755 which the template is a member, or, for member templates, in the
756 namespace of which the enclosing class or enclosing class
757 template is a member. An explicit specialization of a member
758 function, member class or static data member of a class template
759 shall be declared in the namespace of which the class template is
760 a member. */
761 if (current_scope() != DECL_CONTEXT (tmpl)
762 && !at_namespace_scope_p ())
763 {
764 error ("specialization of %qD must appear at namespace scope", tmpl);
765 return false;
766 }
767 if (is_associated_namespace (current_namespace, tpl_ns))
768 /* Same or super-using namespace. */
769 return true;
770 else
771 {
772 permerror (input_location, "specialization of %qD in different namespace", tmpl);
773 permerror (input_location, " from definition of %q+#D", tmpl);
774 return false;
775 }
776 }
777
778 /* SPEC is an explicit instantiation. Check that it is valid to
779 perform this explicit instantiation in the current namespace. */
780
781 static void
782 check_explicit_instantiation_namespace (tree spec)
783 {
784 tree ns;
785
786 /* DR 275: An explicit instantiation shall appear in an enclosing
787 namespace of its template. */
788 ns = decl_namespace_context (spec);
789 if (!is_ancestor (current_namespace, ns))
790 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
791 "(which does not enclose namespace %qD)",
792 spec, current_namespace, ns);
793 }
794
795 /* The TYPE is being declared. If it is a template type, that means it
796 is a partial specialization. Do appropriate error-checking. */
797
798 tree
799 maybe_process_partial_specialization (tree type)
800 {
801 tree context;
802
803 if (type == error_mark_node)
804 return error_mark_node;
805
806 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
807 {
808 error ("name of class shadows template template parameter %qD",
809 TYPE_NAME (type));
810 return error_mark_node;
811 }
812
813 context = TYPE_CONTEXT (type);
814
815 if (TYPE_ALIAS_P (type))
816 {
817 if (TYPE_TEMPLATE_INFO (type)
818 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
819 error ("specialization of alias template %qD",
820 TYPE_TI_TEMPLATE (type));
821 else
822 error ("explicit specialization of non-template %qT", type);
823 return error_mark_node;
824 }
825 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
826 {
827 /* This is for ordinary explicit specialization and partial
828 specialization of a template class such as:
829
830 template <> class C<int>;
831
832 or:
833
834 template <class T> class C<T*>;
835
836 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
837
838 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
839 && !COMPLETE_TYPE_P (type))
840 {
841 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
842 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
843 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
844 if (processing_template_decl)
845 {
846 if (push_template_decl (TYPE_MAIN_DECL (type))
847 == error_mark_node)
848 return error_mark_node;
849 }
850 }
851 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
852 error ("specialization of %qT after instantiation", type);
853
854 }
855 else if (CLASS_TYPE_P (type)
856 && !CLASSTYPE_USE_TEMPLATE (type)
857 && CLASSTYPE_TEMPLATE_INFO (type)
858 && context && CLASS_TYPE_P (context)
859 && CLASSTYPE_TEMPLATE_INFO (context))
860 {
861 /* This is for an explicit specialization of member class
862 template according to [temp.expl.spec/18]:
863
864 template <> template <class U> class C<int>::D;
865
866 The context `C<int>' must be an implicit instantiation.
867 Otherwise this is just a member class template declared
868 earlier like:
869
870 template <> class C<int> { template <class U> class D; };
871 template <> template <class U> class C<int>::D;
872
873 In the first case, `C<int>::D' is a specialization of `C<T>::D'
874 while in the second case, `C<int>::D' is a primary template
875 and `C<T>::D' may not exist. */
876
877 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
878 && !COMPLETE_TYPE_P (type))
879 {
880 tree t;
881 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
882
883 if (current_namespace
884 != decl_namespace_context (tmpl))
885 {
886 permerror (input_location, "specializing %q#T in different namespace", type);
887 permerror (input_location, " from definition of %q+#D", tmpl);
888 }
889
890 /* Check for invalid specialization after instantiation:
891
892 template <> template <> class C<int>::D<int>;
893 template <> template <class U> class C<int>::D; */
894
895 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
896 t; t = TREE_CHAIN (t))
897 {
898 tree inst = TREE_VALUE (t);
899 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
900 {
901 /* We already have a full specialization of this partial
902 instantiation. Reassign it to the new member
903 specialization template. */
904 spec_entry elt;
905 spec_entry *entry;
906 void **slot;
907
908 elt.tmpl = most_general_template (tmpl);
909 elt.args = CLASSTYPE_TI_ARGS (inst);
910 elt.spec = inst;
911
912 htab_remove_elt (type_specializations, &elt);
913
914 elt.tmpl = tmpl;
915 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
916
917 slot = htab_find_slot (type_specializations, &elt, INSERT);
918 entry = ggc_alloc_spec_entry ();
919 *entry = elt;
920 *slot = entry;
921 }
922 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
923 /* But if we've had an implicit instantiation, that's a
924 problem ([temp.expl.spec]/6). */
925 error ("specialization %qT after instantiation %qT",
926 type, inst);
927 }
928
929 /* Mark TYPE as a specialization. And as a result, we only
930 have one level of template argument for the innermost
931 class template. */
932 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
933 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
934 CLASSTYPE_TI_ARGS (type)
935 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
936 }
937 }
938 else if (processing_specialization)
939 {
940 /* Someday C++0x may allow for enum template specialization. */
941 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
942 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
943 pedwarn (input_location, OPT_Wpedantic, "template specialization "
944 "of %qD not allowed by ISO C++", type);
945 else
946 {
947 error ("explicit specialization of non-template %qT", type);
948 return error_mark_node;
949 }
950 }
951
952 return type;
953 }
954
955 /* Returns nonzero if we can optimize the retrieval of specializations
956 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
957 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
958
959 static inline bool
960 optimize_specialization_lookup_p (tree tmpl)
961 {
962 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
963 && DECL_CLASS_SCOPE_P (tmpl)
964 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
965 parameter. */
966 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
967 /* The optimized lookup depends on the fact that the
968 template arguments for the member function template apply
969 purely to the containing class, which is not true if the
970 containing class is an explicit or partial
971 specialization. */
972 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
973 && !DECL_MEMBER_TEMPLATE_P (tmpl)
974 && !DECL_CONV_FN_P (tmpl)
975 /* It is possible to have a template that is not a member
976 template and is not a member of a template class:
977
978 template <typename T>
979 struct S { friend A::f(); };
980
981 Here, the friend function is a template, but the context does
982 not have template information. The optimized lookup relies
983 on having ARGS be the template arguments for both the class
984 and the function template. */
985 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
986 }
987
988 /* Retrieve the specialization (in the sense of [temp.spec] - a
989 specialization is either an instantiation or an explicit
990 specialization) of TMPL for the given template ARGS. If there is
991 no such specialization, return NULL_TREE. The ARGS are a vector of
992 arguments, or a vector of vectors of arguments, in the case of
993 templates with more than one level of parameters.
994
995 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
996 then we search for a partial specialization matching ARGS. This
997 parameter is ignored if TMPL is not a class template. */
998
999 static tree
1000 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1001 {
1002 if (args == error_mark_node)
1003 return NULL_TREE;
1004
1005 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1006
1007 /* There should be as many levels of arguments as there are
1008 levels of parameters. */
1009 gcc_assert (TMPL_ARGS_DEPTH (args)
1010 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1011
1012 if (optimize_specialization_lookup_p (tmpl))
1013 {
1014 tree class_template;
1015 tree class_specialization;
1016 vec<tree, va_gc> *methods;
1017 tree fns;
1018 int idx;
1019
1020 /* The template arguments actually apply to the containing
1021 class. Find the class specialization with those
1022 arguments. */
1023 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1024 class_specialization
1025 = retrieve_specialization (class_template, args, 0);
1026 if (!class_specialization)
1027 return NULL_TREE;
1028 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1029 for the specialization. */
1030 idx = class_method_index_for_fn (class_specialization, tmpl);
1031 if (idx == -1)
1032 return NULL_TREE;
1033 /* Iterate through the methods with the indicated name, looking
1034 for the one that has an instance of TMPL. */
1035 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1036 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1037 {
1038 tree fn = OVL_CURRENT (fns);
1039 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1040 /* using-declarations can add base methods to the method vec,
1041 and we don't want those here. */
1042 && DECL_CONTEXT (fn) == class_specialization)
1043 return fn;
1044 }
1045 return NULL_TREE;
1046 }
1047 else
1048 {
1049 spec_entry *found;
1050 spec_entry elt;
1051 htab_t specializations;
1052
1053 elt.tmpl = tmpl;
1054 elt.args = args;
1055 elt.spec = NULL_TREE;
1056
1057 if (DECL_CLASS_TEMPLATE_P (tmpl))
1058 specializations = type_specializations;
1059 else
1060 specializations = decl_specializations;
1061
1062 if (hash == 0)
1063 hash = hash_specialization (&elt);
1064 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1065 if (found)
1066 return found->spec;
1067 }
1068
1069 return NULL_TREE;
1070 }
1071
1072 /* Like retrieve_specialization, but for local declarations. */
1073
1074 static tree
1075 retrieve_local_specialization (tree tmpl)
1076 {
1077 void **slot;
1078
1079 if (local_specializations == NULL)
1080 return NULL_TREE;
1081
1082 slot = pointer_map_contains (local_specializations, tmpl);
1083 return slot ? (tree) *slot : NULL_TREE;
1084 }
1085
1086 /* Returns nonzero iff DECL is a specialization of TMPL. */
1087
1088 int
1089 is_specialization_of (tree decl, tree tmpl)
1090 {
1091 tree t;
1092
1093 if (TREE_CODE (decl) == FUNCTION_DECL)
1094 {
1095 for (t = decl;
1096 t != NULL_TREE;
1097 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1098 if (t == tmpl)
1099 return 1;
1100 }
1101 else
1102 {
1103 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1104
1105 for (t = TREE_TYPE (decl);
1106 t != NULL_TREE;
1107 t = CLASSTYPE_USE_TEMPLATE (t)
1108 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1109 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1110 return 1;
1111 }
1112
1113 return 0;
1114 }
1115
1116 /* Returns nonzero iff DECL is a specialization of friend declaration
1117 FRIEND_DECL according to [temp.friend]. */
1118
1119 bool
1120 is_specialization_of_friend (tree decl, tree friend_decl)
1121 {
1122 bool need_template = true;
1123 int template_depth;
1124
1125 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1126 || TREE_CODE (decl) == TYPE_DECL);
1127
1128 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1129 of a template class, we want to check if DECL is a specialization
1130 if this. */
1131 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1132 && DECL_TEMPLATE_INFO (friend_decl)
1133 && !DECL_USE_TEMPLATE (friend_decl))
1134 {
1135 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1136 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1137 need_template = false;
1138 }
1139 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1140 && !PRIMARY_TEMPLATE_P (friend_decl))
1141 need_template = false;
1142
1143 /* There is nothing to do if this is not a template friend. */
1144 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1145 return false;
1146
1147 if (is_specialization_of (decl, friend_decl))
1148 return true;
1149
1150 /* [temp.friend/6]
1151 A member of a class template may be declared to be a friend of a
1152 non-template class. In this case, the corresponding member of
1153 every specialization of the class template is a friend of the
1154 class granting friendship.
1155
1156 For example, given a template friend declaration
1157
1158 template <class T> friend void A<T>::f();
1159
1160 the member function below is considered a friend
1161
1162 template <> struct A<int> {
1163 void f();
1164 };
1165
1166 For this type of template friend, TEMPLATE_DEPTH below will be
1167 nonzero. To determine if DECL is a friend of FRIEND, we first
1168 check if the enclosing class is a specialization of another. */
1169
1170 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1171 if (template_depth
1172 && DECL_CLASS_SCOPE_P (decl)
1173 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1174 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1175 {
1176 /* Next, we check the members themselves. In order to handle
1177 a few tricky cases, such as when FRIEND_DECL's are
1178
1179 template <class T> friend void A<T>::g(T t);
1180 template <class T> template <T t> friend void A<T>::h();
1181
1182 and DECL's are
1183
1184 void A<int>::g(int);
1185 template <int> void A<int>::h();
1186
1187 we need to figure out ARGS, the template arguments from
1188 the context of DECL. This is required for template substitution
1189 of `T' in the function parameter of `g' and template parameter
1190 of `h' in the above examples. Here ARGS corresponds to `int'. */
1191
1192 tree context = DECL_CONTEXT (decl);
1193 tree args = NULL_TREE;
1194 int current_depth = 0;
1195
1196 while (current_depth < template_depth)
1197 {
1198 if (CLASSTYPE_TEMPLATE_INFO (context))
1199 {
1200 if (current_depth == 0)
1201 args = TYPE_TI_ARGS (context);
1202 else
1203 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1204 current_depth++;
1205 }
1206 context = TYPE_CONTEXT (context);
1207 }
1208
1209 if (TREE_CODE (decl) == FUNCTION_DECL)
1210 {
1211 bool is_template;
1212 tree friend_type;
1213 tree decl_type;
1214 tree friend_args_type;
1215 tree decl_args_type;
1216
1217 /* Make sure that both DECL and FRIEND_DECL are templates or
1218 non-templates. */
1219 is_template = DECL_TEMPLATE_INFO (decl)
1220 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1221 if (need_template ^ is_template)
1222 return false;
1223 else if (is_template)
1224 {
1225 /* If both are templates, check template parameter list. */
1226 tree friend_parms
1227 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1228 args, tf_none);
1229 if (!comp_template_parms
1230 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1231 friend_parms))
1232 return false;
1233
1234 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1235 }
1236 else
1237 decl_type = TREE_TYPE (decl);
1238
1239 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1240 tf_none, NULL_TREE);
1241 if (friend_type == error_mark_node)
1242 return false;
1243
1244 /* Check if return types match. */
1245 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1246 return false;
1247
1248 /* Check if function parameter types match, ignoring the
1249 `this' parameter. */
1250 friend_args_type = TYPE_ARG_TYPES (friend_type);
1251 decl_args_type = TYPE_ARG_TYPES (decl_type);
1252 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1253 friend_args_type = TREE_CHAIN (friend_args_type);
1254 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1255 decl_args_type = TREE_CHAIN (decl_args_type);
1256
1257 return compparms (decl_args_type, friend_args_type);
1258 }
1259 else
1260 {
1261 /* DECL is a TYPE_DECL */
1262 bool is_template;
1263 tree decl_type = TREE_TYPE (decl);
1264
1265 /* Make sure that both DECL and FRIEND_DECL are templates or
1266 non-templates. */
1267 is_template
1268 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1269 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1270
1271 if (need_template ^ is_template)
1272 return false;
1273 else if (is_template)
1274 {
1275 tree friend_parms;
1276 /* If both are templates, check the name of the two
1277 TEMPLATE_DECL's first because is_friend didn't. */
1278 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1279 != DECL_NAME (friend_decl))
1280 return false;
1281
1282 /* Now check template parameter list. */
1283 friend_parms
1284 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1285 args, tf_none);
1286 return comp_template_parms
1287 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1288 friend_parms);
1289 }
1290 else
1291 return (DECL_NAME (decl)
1292 == DECL_NAME (friend_decl));
1293 }
1294 }
1295 return false;
1296 }
1297
1298 /* Register the specialization SPEC as a specialization of TMPL with
1299 the indicated ARGS. IS_FRIEND indicates whether the specialization
1300 is actually just a friend declaration. Returns SPEC, or an
1301 equivalent prior declaration, if available. */
1302
1303 static tree
1304 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1305 hashval_t hash)
1306 {
1307 tree fn;
1308 void **slot = NULL;
1309 spec_entry elt;
1310
1311 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1312
1313 if (TREE_CODE (spec) == FUNCTION_DECL
1314 && uses_template_parms (DECL_TI_ARGS (spec)))
1315 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1316 register it; we want the corresponding TEMPLATE_DECL instead.
1317 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1318 the more obvious `uses_template_parms (spec)' to avoid problems
1319 with default function arguments. In particular, given
1320 something like this:
1321
1322 template <class T> void f(T t1, T t = T())
1323
1324 the default argument expression is not substituted for in an
1325 instantiation unless and until it is actually needed. */
1326 return spec;
1327
1328 if (optimize_specialization_lookup_p (tmpl))
1329 /* We don't put these specializations in the hash table, but we might
1330 want to give an error about a mismatch. */
1331 fn = retrieve_specialization (tmpl, args, 0);
1332 else
1333 {
1334 elt.tmpl = tmpl;
1335 elt.args = args;
1336 elt.spec = spec;
1337
1338 if (hash == 0)
1339 hash = hash_specialization (&elt);
1340
1341 slot =
1342 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1343 if (*slot)
1344 fn = ((spec_entry *) *slot)->spec;
1345 else
1346 fn = NULL_TREE;
1347 }
1348
1349 /* We can sometimes try to re-register a specialization that we've
1350 already got. In particular, regenerate_decl_from_template calls
1351 duplicate_decls which will update the specialization list. But,
1352 we'll still get called again here anyhow. It's more convenient
1353 to simply allow this than to try to prevent it. */
1354 if (fn == spec)
1355 return spec;
1356 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1357 {
1358 if (DECL_TEMPLATE_INSTANTIATION (fn))
1359 {
1360 if (DECL_ODR_USED (fn)
1361 || DECL_EXPLICIT_INSTANTIATION (fn))
1362 {
1363 error ("specialization of %qD after instantiation",
1364 fn);
1365 return error_mark_node;
1366 }
1367 else
1368 {
1369 tree clone;
1370 /* This situation should occur only if the first
1371 specialization is an implicit instantiation, the
1372 second is an explicit specialization, and the
1373 implicit instantiation has not yet been used. That
1374 situation can occur if we have implicitly
1375 instantiated a member function and then specialized
1376 it later.
1377
1378 We can also wind up here if a friend declaration that
1379 looked like an instantiation turns out to be a
1380 specialization:
1381
1382 template <class T> void foo(T);
1383 class S { friend void foo<>(int) };
1384 template <> void foo(int);
1385
1386 We transform the existing DECL in place so that any
1387 pointers to it become pointers to the updated
1388 declaration.
1389
1390 If there was a definition for the template, but not
1391 for the specialization, we want this to look as if
1392 there were no definition, and vice versa. */
1393 DECL_INITIAL (fn) = NULL_TREE;
1394 duplicate_decls (spec, fn, is_friend);
1395 /* The call to duplicate_decls will have applied
1396 [temp.expl.spec]:
1397
1398 An explicit specialization of a function template
1399 is inline only if it is explicitly declared to be,
1400 and independently of whether its function template
1401 is.
1402
1403 to the primary function; now copy the inline bits to
1404 the various clones. */
1405 FOR_EACH_CLONE (clone, fn)
1406 {
1407 DECL_DECLARED_INLINE_P (clone)
1408 = DECL_DECLARED_INLINE_P (fn);
1409 DECL_SOURCE_LOCATION (clone)
1410 = DECL_SOURCE_LOCATION (fn);
1411 }
1412 check_specialization_namespace (tmpl);
1413
1414 return fn;
1415 }
1416 }
1417 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1418 {
1419 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1420 /* Dup decl failed, but this is a new definition. Set the
1421 line number so any errors match this new
1422 definition. */
1423 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1424
1425 return fn;
1426 }
1427 }
1428 else if (fn)
1429 return duplicate_decls (spec, fn, is_friend);
1430
1431 /* A specialization must be declared in the same namespace as the
1432 template it is specializing. */
1433 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1434 && !check_specialization_namespace (tmpl))
1435 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1436
1437 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1438 {
1439 spec_entry *entry = ggc_alloc_spec_entry ();
1440 gcc_assert (tmpl && args && spec);
1441 *entry = elt;
1442 *slot = entry;
1443 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1444 && PRIMARY_TEMPLATE_P (tmpl)
1445 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1446 /* TMPL is a forward declaration of a template function; keep a list
1447 of all specializations in case we need to reassign them to a friend
1448 template later in tsubst_friend_function. */
1449 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1450 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1451 }
1452
1453 return spec;
1454 }
1455
1456 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1457 TMPL and ARGS members, ignores SPEC. */
1458
1459 static int
1460 eq_specializations (const void *p1, const void *p2)
1461 {
1462 const spec_entry *e1 = (const spec_entry *)p1;
1463 const spec_entry *e2 = (const spec_entry *)p2;
1464
1465 return (e1->tmpl == e2->tmpl
1466 && comp_template_args (e1->args, e2->args));
1467 }
1468
1469 /* Returns a hash for a template TMPL and template arguments ARGS. */
1470
1471 static hashval_t
1472 hash_tmpl_and_args (tree tmpl, tree args)
1473 {
1474 hashval_t val = DECL_UID (tmpl);
1475 return iterative_hash_template_arg (args, val);
1476 }
1477
1478 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1479 ignoring SPEC. */
1480
1481 static hashval_t
1482 hash_specialization (const void *p)
1483 {
1484 const spec_entry *e = (const spec_entry *)p;
1485 return hash_tmpl_and_args (e->tmpl, e->args);
1486 }
1487
1488 /* Recursively calculate a hash value for a template argument ARG, for use
1489 in the hash tables of template specializations. */
1490
1491 hashval_t
1492 iterative_hash_template_arg (tree arg, hashval_t val)
1493 {
1494 unsigned HOST_WIDE_INT i;
1495 enum tree_code code;
1496 char tclass;
1497
1498 if (arg == NULL_TREE)
1499 return iterative_hash_object (arg, val);
1500
1501 if (!TYPE_P (arg))
1502 STRIP_NOPS (arg);
1503
1504 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1505 /* We can get one of these when re-hashing a previous entry in the middle
1506 of substituting into a pack expansion. Just look through it. */
1507 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1508
1509 code = TREE_CODE (arg);
1510 tclass = TREE_CODE_CLASS (code);
1511
1512 val = iterative_hash_object (code, val);
1513
1514 switch (code)
1515 {
1516 case ERROR_MARK:
1517 return val;
1518
1519 case IDENTIFIER_NODE:
1520 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1521
1522 case TREE_VEC:
1523 {
1524 int i, len = TREE_VEC_LENGTH (arg);
1525 for (i = 0; i < len; ++i)
1526 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1527 return val;
1528 }
1529
1530 case TYPE_PACK_EXPANSION:
1531 case EXPR_PACK_EXPANSION:
1532 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1533 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1534
1535 case TYPE_ARGUMENT_PACK:
1536 case NONTYPE_ARGUMENT_PACK:
1537 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1538
1539 case TREE_LIST:
1540 for (; arg; arg = TREE_CHAIN (arg))
1541 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1542 return val;
1543
1544 case OVERLOAD:
1545 for (; arg; arg = OVL_NEXT (arg))
1546 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1547 return val;
1548
1549 case CONSTRUCTOR:
1550 {
1551 tree field, value;
1552 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1553 {
1554 val = iterative_hash_template_arg (field, val);
1555 val = iterative_hash_template_arg (value, val);
1556 }
1557 return val;
1558 }
1559
1560 case PARM_DECL:
1561 if (!DECL_ARTIFICIAL (arg))
1562 {
1563 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1564 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1565 }
1566 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1567
1568 case TARGET_EXPR:
1569 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1570
1571 case PTRMEM_CST:
1572 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1573 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1574
1575 case TEMPLATE_PARM_INDEX:
1576 val = iterative_hash_template_arg
1577 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1578 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1579 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1580
1581 case TRAIT_EXPR:
1582 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1583 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1584 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1585
1586 case BASELINK:
1587 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1588 val);
1589 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1590 val);
1591
1592 case MODOP_EXPR:
1593 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1594 code = TREE_CODE (TREE_OPERAND (arg, 1));
1595 val = iterative_hash_object (code, val);
1596 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1597
1598 case LAMBDA_EXPR:
1599 /* A lambda can't appear in a template arg, but don't crash on
1600 erroneous input. */
1601 gcc_assert (seen_error ());
1602 return val;
1603
1604 case CAST_EXPR:
1605 case IMPLICIT_CONV_EXPR:
1606 case STATIC_CAST_EXPR:
1607 case REINTERPRET_CAST_EXPR:
1608 case CONST_CAST_EXPR:
1609 case DYNAMIC_CAST_EXPR:
1610 case NEW_EXPR:
1611 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1612 /* Now hash operands as usual. */
1613 break;
1614
1615 default:
1616 break;
1617 }
1618
1619 switch (tclass)
1620 {
1621 case tcc_type:
1622 if (TYPE_CANONICAL (arg))
1623 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1624 val);
1625 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1626 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1627 /* Otherwise just compare the types during lookup. */
1628 return val;
1629
1630 case tcc_declaration:
1631 case tcc_constant:
1632 return iterative_hash_expr (arg, val);
1633
1634 default:
1635 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1636 {
1637 unsigned n = cp_tree_operand_length (arg);
1638 for (i = 0; i < n; ++i)
1639 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1640 return val;
1641 }
1642 }
1643 gcc_unreachable ();
1644 return 0;
1645 }
1646
1647 /* Unregister the specialization SPEC as a specialization of TMPL.
1648 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1649 if the SPEC was listed as a specialization of TMPL.
1650
1651 Note that SPEC has been ggc_freed, so we can't look inside it. */
1652
1653 bool
1654 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1655 {
1656 spec_entry *entry;
1657 spec_entry elt;
1658
1659 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1660 elt.args = TI_ARGS (tinfo);
1661 elt.spec = NULL_TREE;
1662
1663 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1664 if (entry != NULL)
1665 {
1666 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1667 gcc_assert (new_spec != NULL_TREE);
1668 entry->spec = new_spec;
1669 return 1;
1670 }
1671
1672 return 0;
1673 }
1674
1675 /* Like register_specialization, but for local declarations. We are
1676 registering SPEC, an instantiation of TMPL. */
1677
1678 static void
1679 register_local_specialization (tree spec, tree tmpl)
1680 {
1681 void **slot;
1682
1683 slot = pointer_map_insert (local_specializations, tmpl);
1684 *slot = spec;
1685 }
1686
1687 /* TYPE is a class type. Returns true if TYPE is an explicitly
1688 specialized class. */
1689
1690 bool
1691 explicit_class_specialization_p (tree type)
1692 {
1693 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1694 return false;
1695 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1696 }
1697
1698 /* Print the list of functions at FNS, going through all the overloads
1699 for each element of the list. Alternatively, FNS can not be a
1700 TREE_LIST, in which case it will be printed together with all the
1701 overloads.
1702
1703 MORE and *STR should respectively be FALSE and NULL when the function
1704 is called from the outside. They are used internally on recursive
1705 calls. print_candidates manages the two parameters and leaves NULL
1706 in *STR when it ends. */
1707
1708 static void
1709 print_candidates_1 (tree fns, bool more, const char **str)
1710 {
1711 tree fn, fn2;
1712 char *spaces = NULL;
1713
1714 for (fn = fns; fn; fn = OVL_NEXT (fn))
1715 if (TREE_CODE (fn) == TREE_LIST)
1716 {
1717 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1718 print_candidates_1 (TREE_VALUE (fn2),
1719 TREE_CHAIN (fn2) || more, str);
1720 }
1721 else
1722 {
1723 tree cand = OVL_CURRENT (fn);
1724 if (!*str)
1725 {
1726 /* Pick the prefix string. */
1727 if (!more && !OVL_NEXT (fns))
1728 {
1729 inform (DECL_SOURCE_LOCATION (cand),
1730 "candidate is: %#D", cand);
1731 continue;
1732 }
1733
1734 *str = _("candidates are:");
1735 spaces = get_spaces (*str);
1736 }
1737 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1738 *str = spaces ? spaces : *str;
1739 }
1740
1741 if (!more)
1742 {
1743 free (spaces);
1744 *str = NULL;
1745 }
1746 }
1747
1748 /* Print the list of candidate FNS in an error message. FNS can also
1749 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1750
1751 void
1752 print_candidates (tree fns)
1753 {
1754 const char *str = NULL;
1755 print_candidates_1 (fns, false, &str);
1756 gcc_assert (str == NULL);
1757 }
1758
1759 /* Returns the template (one of the functions given by TEMPLATE_ID)
1760 which can be specialized to match the indicated DECL with the
1761 explicit template args given in TEMPLATE_ID. The DECL may be
1762 NULL_TREE if none is available. In that case, the functions in
1763 TEMPLATE_ID are non-members.
1764
1765 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1766 specialization of a member template.
1767
1768 The TEMPLATE_COUNT is the number of references to qualifying
1769 template classes that appeared in the name of the function. See
1770 check_explicit_specialization for a more accurate description.
1771
1772 TSK indicates what kind of template declaration (if any) is being
1773 declared. TSK_TEMPLATE indicates that the declaration given by
1774 DECL, though a FUNCTION_DECL, has template parameters, and is
1775 therefore a template function.
1776
1777 The template args (those explicitly specified and those deduced)
1778 are output in a newly created vector *TARGS_OUT.
1779
1780 If it is impossible to determine the result, an error message is
1781 issued. The error_mark_node is returned to indicate failure. */
1782
1783 static tree
1784 determine_specialization (tree template_id,
1785 tree decl,
1786 tree* targs_out,
1787 int need_member_template,
1788 int template_count,
1789 tmpl_spec_kind tsk)
1790 {
1791 tree fns;
1792 tree targs;
1793 tree explicit_targs;
1794 tree candidates = NULL_TREE;
1795 /* A TREE_LIST of templates of which DECL may be a specialization.
1796 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1797 corresponding TREE_PURPOSE is the set of template arguments that,
1798 when used to instantiate the template, would produce a function
1799 with the signature of DECL. */
1800 tree templates = NULL_TREE;
1801 int header_count;
1802 cp_binding_level *b;
1803
1804 *targs_out = NULL_TREE;
1805
1806 if (template_id == error_mark_node || decl == error_mark_node)
1807 return error_mark_node;
1808
1809 /* We shouldn't be specializing a member template of an
1810 unspecialized class template; we already gave an error in
1811 check_specialization_scope, now avoid crashing. */
1812 if (template_count && DECL_CLASS_SCOPE_P (decl)
1813 && template_class_depth (DECL_CONTEXT (decl)) > 0)
1814 {
1815 gcc_assert (errorcount);
1816 return error_mark_node;
1817 }
1818
1819 fns = TREE_OPERAND (template_id, 0);
1820 explicit_targs = TREE_OPERAND (template_id, 1);
1821
1822 if (fns == error_mark_node)
1823 return error_mark_node;
1824
1825 /* Check for baselinks. */
1826 if (BASELINK_P (fns))
1827 fns = BASELINK_FUNCTIONS (fns);
1828
1829 if (!is_overloaded_fn (fns))
1830 {
1831 error ("%qD is not a function template", fns);
1832 return error_mark_node;
1833 }
1834
1835 /* Count the number of template headers specified for this
1836 specialization. */
1837 header_count = 0;
1838 for (b = current_binding_level;
1839 b->kind == sk_template_parms;
1840 b = b->level_chain)
1841 ++header_count;
1842
1843 for (; fns; fns = OVL_NEXT (fns))
1844 {
1845 tree fn = OVL_CURRENT (fns);
1846
1847 if (TREE_CODE (fn) == TEMPLATE_DECL)
1848 {
1849 tree decl_arg_types;
1850 tree fn_arg_types;
1851 tree insttype;
1852
1853 /* In case of explicit specialization, we need to check if
1854 the number of template headers appearing in the specialization
1855 is correct. This is usually done in check_explicit_specialization,
1856 but the check done there cannot be exhaustive when specializing
1857 member functions. Consider the following code:
1858
1859 template <> void A<int>::f(int);
1860 template <> template <> void A<int>::f(int);
1861
1862 Assuming that A<int> is not itself an explicit specialization
1863 already, the first line specializes "f" which is a non-template
1864 member function, whilst the second line specializes "f" which
1865 is a template member function. So both lines are syntactically
1866 correct, and check_explicit_specialization does not reject
1867 them.
1868
1869 Here, we can do better, as we are matching the specialization
1870 against the declarations. We count the number of template
1871 headers, and we check if they match TEMPLATE_COUNT + 1
1872 (TEMPLATE_COUNT is the number of qualifying template classes,
1873 plus there must be another header for the member template
1874 itself).
1875
1876 Notice that if header_count is zero, this is not a
1877 specialization but rather a template instantiation, so there
1878 is no check we can perform here. */
1879 if (header_count && header_count != template_count + 1)
1880 continue;
1881
1882 /* Check that the number of template arguments at the
1883 innermost level for DECL is the same as for FN. */
1884 if (current_binding_level->kind == sk_template_parms
1885 && !current_binding_level->explicit_spec_p
1886 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1887 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1888 (current_template_parms))))
1889 continue;
1890
1891 /* DECL might be a specialization of FN. */
1892 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1893 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1894
1895 /* For a non-static member function, we need to make sure
1896 that the const qualification is the same. Since
1897 get_bindings does not try to merge the "this" parameter,
1898 we must do the comparison explicitly. */
1899 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1900 && !same_type_p (TREE_VALUE (fn_arg_types),
1901 TREE_VALUE (decl_arg_types)))
1902 continue;
1903
1904 /* Skip the "this" parameter and, for constructors of
1905 classes with virtual bases, the VTT parameter. A
1906 full specialization of a constructor will have a VTT
1907 parameter, but a template never will. */
1908 decl_arg_types
1909 = skip_artificial_parms_for (decl, decl_arg_types);
1910 fn_arg_types
1911 = skip_artificial_parms_for (fn, fn_arg_types);
1912
1913 /* Function templates cannot be specializations; there are
1914 no partial specializations of functions. Therefore, if
1915 the type of DECL does not match FN, there is no
1916 match. */
1917 if (tsk == tsk_template)
1918 {
1919 if (compparms (fn_arg_types, decl_arg_types))
1920 candidates = tree_cons (NULL_TREE, fn, candidates);
1921 continue;
1922 }
1923
1924 /* See whether this function might be a specialization of this
1925 template. Suppress access control because we might be trying
1926 to make this specialization a friend, and we have already done
1927 access control for the declaration of the specialization. */
1928 push_deferring_access_checks (dk_no_check);
1929 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1930 pop_deferring_access_checks ();
1931
1932 if (!targs)
1933 /* We cannot deduce template arguments that when used to
1934 specialize TMPL will produce DECL. */
1935 continue;
1936
1937 /* Make sure that the deduced arguments actually work. */
1938 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1939 if (insttype == error_mark_node)
1940 continue;
1941 fn_arg_types
1942 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1943 if (!compparms (fn_arg_types, decl_arg_types))
1944 continue;
1945
1946 /* Save this template, and the arguments deduced. */
1947 templates = tree_cons (targs, fn, templates);
1948 }
1949 else if (need_member_template)
1950 /* FN is an ordinary member function, and we need a
1951 specialization of a member template. */
1952 ;
1953 else if (TREE_CODE (fn) != FUNCTION_DECL)
1954 /* We can get IDENTIFIER_NODEs here in certain erroneous
1955 cases. */
1956 ;
1957 else if (!DECL_FUNCTION_MEMBER_P (fn))
1958 /* This is just an ordinary non-member function. Nothing can
1959 be a specialization of that. */
1960 ;
1961 else if (DECL_ARTIFICIAL (fn))
1962 /* Cannot specialize functions that are created implicitly. */
1963 ;
1964 else
1965 {
1966 tree decl_arg_types;
1967
1968 /* This is an ordinary member function. However, since
1969 we're here, we can assume it's enclosing class is a
1970 template class. For example,
1971
1972 template <typename T> struct S { void f(); };
1973 template <> void S<int>::f() {}
1974
1975 Here, S<int>::f is a non-template, but S<int> is a
1976 template class. If FN has the same type as DECL, we
1977 might be in business. */
1978
1979 if (!DECL_TEMPLATE_INFO (fn))
1980 /* Its enclosing class is an explicit specialization
1981 of a template class. This is not a candidate. */
1982 continue;
1983
1984 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1985 TREE_TYPE (TREE_TYPE (fn))))
1986 /* The return types differ. */
1987 continue;
1988
1989 /* Adjust the type of DECL in case FN is a static member. */
1990 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1991 if (DECL_STATIC_FUNCTION_P (fn)
1992 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1993 decl_arg_types = TREE_CHAIN (decl_arg_types);
1994
1995 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1996 decl_arg_types))
1997 /* They match! */
1998 candidates = tree_cons (NULL_TREE, fn, candidates);
1999 }
2000 }
2001
2002 if (templates && TREE_CHAIN (templates))
2003 {
2004 /* We have:
2005
2006 [temp.expl.spec]
2007
2008 It is possible for a specialization with a given function
2009 signature to be instantiated from more than one function
2010 template. In such cases, explicit specification of the
2011 template arguments must be used to uniquely identify the
2012 function template specialization being specialized.
2013
2014 Note that here, there's no suggestion that we're supposed to
2015 determine which of the candidate templates is most
2016 specialized. However, we, also have:
2017
2018 [temp.func.order]
2019
2020 Partial ordering of overloaded function template
2021 declarations is used in the following contexts to select
2022 the function template to which a function template
2023 specialization refers:
2024
2025 -- when an explicit specialization refers to a function
2026 template.
2027
2028 So, we do use the partial ordering rules, at least for now.
2029 This extension can only serve to make invalid programs valid,
2030 so it's safe. And, there is strong anecdotal evidence that
2031 the committee intended the partial ordering rules to apply;
2032 the EDG front end has that behavior, and John Spicer claims
2033 that the committee simply forgot to delete the wording in
2034 [temp.expl.spec]. */
2035 tree tmpl = most_specialized_instantiation (templates);
2036 if (tmpl != error_mark_node)
2037 {
2038 templates = tmpl;
2039 TREE_CHAIN (templates) = NULL_TREE;
2040 }
2041 }
2042
2043 if (templates == NULL_TREE && candidates == NULL_TREE)
2044 {
2045 error ("template-id %qD for %q+D does not match any template "
2046 "declaration", template_id, decl);
2047 if (header_count && header_count != template_count + 1)
2048 inform (input_location, "saw %d %<template<>%>, need %d for "
2049 "specializing a member function template",
2050 header_count, template_count + 1);
2051 return error_mark_node;
2052 }
2053 else if ((templates && TREE_CHAIN (templates))
2054 || (candidates && TREE_CHAIN (candidates))
2055 || (templates && candidates))
2056 {
2057 error ("ambiguous template specialization %qD for %q+D",
2058 template_id, decl);
2059 candidates = chainon (candidates, templates);
2060 print_candidates (candidates);
2061 return error_mark_node;
2062 }
2063
2064 /* We have one, and exactly one, match. */
2065 if (candidates)
2066 {
2067 tree fn = TREE_VALUE (candidates);
2068 *targs_out = copy_node (DECL_TI_ARGS (fn));
2069 /* DECL is a re-declaration or partial instantiation of a template
2070 function. */
2071 if (TREE_CODE (fn) == TEMPLATE_DECL)
2072 return fn;
2073 /* It was a specialization of an ordinary member function in a
2074 template class. */
2075 return DECL_TI_TEMPLATE (fn);
2076 }
2077
2078 /* It was a specialization of a template. */
2079 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2080 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2081 {
2082 *targs_out = copy_node (targs);
2083 SET_TMPL_ARGS_LEVEL (*targs_out,
2084 TMPL_ARGS_DEPTH (*targs_out),
2085 TREE_PURPOSE (templates));
2086 }
2087 else
2088 *targs_out = TREE_PURPOSE (templates);
2089 return TREE_VALUE (templates);
2090 }
2091
2092 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2093 but with the default argument values filled in from those in the
2094 TMPL_TYPES. */
2095
2096 static tree
2097 copy_default_args_to_explicit_spec_1 (tree spec_types,
2098 tree tmpl_types)
2099 {
2100 tree new_spec_types;
2101
2102 if (!spec_types)
2103 return NULL_TREE;
2104
2105 if (spec_types == void_list_node)
2106 return void_list_node;
2107
2108 /* Substitute into the rest of the list. */
2109 new_spec_types =
2110 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2111 TREE_CHAIN (tmpl_types));
2112
2113 /* Add the default argument for this parameter. */
2114 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2115 TREE_VALUE (spec_types),
2116 new_spec_types);
2117 }
2118
2119 /* DECL is an explicit specialization. Replicate default arguments
2120 from the template it specializes. (That way, code like:
2121
2122 template <class T> void f(T = 3);
2123 template <> void f(double);
2124 void g () { f (); }
2125
2126 works, as required.) An alternative approach would be to look up
2127 the correct default arguments at the call-site, but this approach
2128 is consistent with how implicit instantiations are handled. */
2129
2130 static void
2131 copy_default_args_to_explicit_spec (tree decl)
2132 {
2133 tree tmpl;
2134 tree spec_types;
2135 tree tmpl_types;
2136 tree new_spec_types;
2137 tree old_type;
2138 tree new_type;
2139 tree t;
2140 tree object_type = NULL_TREE;
2141 tree in_charge = NULL_TREE;
2142 tree vtt = NULL_TREE;
2143
2144 /* See if there's anything we need to do. */
2145 tmpl = DECL_TI_TEMPLATE (decl);
2146 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2147 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2148 if (TREE_PURPOSE (t))
2149 break;
2150 if (!t)
2151 return;
2152
2153 old_type = TREE_TYPE (decl);
2154 spec_types = TYPE_ARG_TYPES (old_type);
2155
2156 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2157 {
2158 /* Remove the this pointer, but remember the object's type for
2159 CV quals. */
2160 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2161 spec_types = TREE_CHAIN (spec_types);
2162 tmpl_types = TREE_CHAIN (tmpl_types);
2163
2164 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2165 {
2166 /* DECL may contain more parameters than TMPL due to the extra
2167 in-charge parameter in constructors and destructors. */
2168 in_charge = spec_types;
2169 spec_types = TREE_CHAIN (spec_types);
2170 }
2171 if (DECL_HAS_VTT_PARM_P (decl))
2172 {
2173 vtt = spec_types;
2174 spec_types = TREE_CHAIN (spec_types);
2175 }
2176 }
2177
2178 /* Compute the merged default arguments. */
2179 new_spec_types =
2180 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2181
2182 /* Compute the new FUNCTION_TYPE. */
2183 if (object_type)
2184 {
2185 if (vtt)
2186 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2187 TREE_VALUE (vtt),
2188 new_spec_types);
2189
2190 if (in_charge)
2191 /* Put the in-charge parameter back. */
2192 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2193 TREE_VALUE (in_charge),
2194 new_spec_types);
2195
2196 new_type = build_method_type_directly (object_type,
2197 TREE_TYPE (old_type),
2198 new_spec_types);
2199 }
2200 else
2201 new_type = build_function_type (TREE_TYPE (old_type),
2202 new_spec_types);
2203 new_type = cp_build_type_attribute_variant (new_type,
2204 TYPE_ATTRIBUTES (old_type));
2205 new_type = build_exception_variant (new_type,
2206 TYPE_RAISES_EXCEPTIONS (old_type));
2207 TREE_TYPE (decl) = new_type;
2208 }
2209
2210 /* Return the number of template headers we expect to see for a definition
2211 or specialization of CTYPE or one of its non-template members. */
2212
2213 int
2214 num_template_headers_for_class (tree ctype)
2215 {
2216 int num_templates = 0;
2217
2218 while (ctype && CLASS_TYPE_P (ctype))
2219 {
2220 /* You're supposed to have one `template <...>' for every
2221 template class, but you don't need one for a full
2222 specialization. For example:
2223
2224 template <class T> struct S{};
2225 template <> struct S<int> { void f(); };
2226 void S<int>::f () {}
2227
2228 is correct; there shouldn't be a `template <>' for the
2229 definition of `S<int>::f'. */
2230 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2231 /* If CTYPE does not have template information of any
2232 kind, then it is not a template, nor is it nested
2233 within a template. */
2234 break;
2235 if (explicit_class_specialization_p (ctype))
2236 break;
2237 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2238 ++num_templates;
2239
2240 ctype = TYPE_CONTEXT (ctype);
2241 }
2242
2243 return num_templates;
2244 }
2245
2246 /* Do a simple sanity check on the template headers that precede the
2247 variable declaration DECL. */
2248
2249 void
2250 check_template_variable (tree decl)
2251 {
2252 tree ctx = CP_DECL_CONTEXT (decl);
2253 int wanted = num_template_headers_for_class (ctx);
2254 if (!TYPE_P (ctx) || !CLASSTYPE_TEMPLATE_INFO (ctx))
2255 permerror (DECL_SOURCE_LOCATION (decl),
2256 "%qD is not a static data member of a class template", decl);
2257 else if (template_header_count > wanted)
2258 {
2259 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2260 "too many template headers for %D (should be %d)",
2261 decl, wanted);
2262 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2263 inform (DECL_SOURCE_LOCATION (decl),
2264 "members of an explicitly specialized class are defined "
2265 "without a template header");
2266 }
2267 }
2268
2269 /* Check to see if the function just declared, as indicated in
2270 DECLARATOR, and in DECL, is a specialization of a function
2271 template. We may also discover that the declaration is an explicit
2272 instantiation at this point.
2273
2274 Returns DECL, or an equivalent declaration that should be used
2275 instead if all goes well. Issues an error message if something is
2276 amiss. Returns error_mark_node if the error is not easily
2277 recoverable.
2278
2279 FLAGS is a bitmask consisting of the following flags:
2280
2281 2: The function has a definition.
2282 4: The function is a friend.
2283
2284 The TEMPLATE_COUNT is the number of references to qualifying
2285 template classes that appeared in the name of the function. For
2286 example, in
2287
2288 template <class T> struct S { void f(); };
2289 void S<int>::f();
2290
2291 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2292 classes are not counted in the TEMPLATE_COUNT, so that in
2293
2294 template <class T> struct S {};
2295 template <> struct S<int> { void f(); }
2296 template <> void S<int>::f();
2297
2298 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2299 invalid; there should be no template <>.)
2300
2301 If the function is a specialization, it is marked as such via
2302 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2303 is set up correctly, and it is added to the list of specializations
2304 for that template. */
2305
2306 tree
2307 check_explicit_specialization (tree declarator,
2308 tree decl,
2309 int template_count,
2310 int flags)
2311 {
2312 int have_def = flags & 2;
2313 int is_friend = flags & 4;
2314 int specialization = 0;
2315 int explicit_instantiation = 0;
2316 int member_specialization = 0;
2317 tree ctype = DECL_CLASS_CONTEXT (decl);
2318 tree dname = DECL_NAME (decl);
2319 tmpl_spec_kind tsk;
2320
2321 if (is_friend)
2322 {
2323 if (!processing_specialization)
2324 tsk = tsk_none;
2325 else
2326 tsk = tsk_excessive_parms;
2327 }
2328 else
2329 tsk = current_tmpl_spec_kind (template_count);
2330
2331 switch (tsk)
2332 {
2333 case tsk_none:
2334 if (processing_specialization)
2335 {
2336 specialization = 1;
2337 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2338 }
2339 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2340 {
2341 if (is_friend)
2342 /* This could be something like:
2343
2344 template <class T> void f(T);
2345 class S { friend void f<>(int); } */
2346 specialization = 1;
2347 else
2348 {
2349 /* This case handles bogus declarations like template <>
2350 template <class T> void f<int>(); */
2351
2352 error ("template-id %qD in declaration of primary template",
2353 declarator);
2354 return decl;
2355 }
2356 }
2357 break;
2358
2359 case tsk_invalid_member_spec:
2360 /* The error has already been reported in
2361 check_specialization_scope. */
2362 return error_mark_node;
2363
2364 case tsk_invalid_expl_inst:
2365 error ("template parameter list used in explicit instantiation");
2366
2367 /* Fall through. */
2368
2369 case tsk_expl_inst:
2370 if (have_def)
2371 error ("definition provided for explicit instantiation");
2372
2373 explicit_instantiation = 1;
2374 break;
2375
2376 case tsk_excessive_parms:
2377 case tsk_insufficient_parms:
2378 if (tsk == tsk_excessive_parms)
2379 error ("too many template parameter lists in declaration of %qD",
2380 decl);
2381 else if (template_header_count)
2382 error("too few template parameter lists in declaration of %qD", decl);
2383 else
2384 error("explicit specialization of %qD must be introduced by "
2385 "%<template <>%>", decl);
2386
2387 /* Fall through. */
2388 case tsk_expl_spec:
2389 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2390 if (ctype)
2391 member_specialization = 1;
2392 else
2393 specialization = 1;
2394 break;
2395
2396 case tsk_template:
2397 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2398 {
2399 /* This case handles bogus declarations like template <>
2400 template <class T> void f<int>(); */
2401
2402 if (uses_template_parms (declarator))
2403 error ("function template partial specialization %qD "
2404 "is not allowed", declarator);
2405 else
2406 error ("template-id %qD in declaration of primary template",
2407 declarator);
2408 return decl;
2409 }
2410
2411 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2412 /* This is a specialization of a member template, without
2413 specialization the containing class. Something like:
2414
2415 template <class T> struct S {
2416 template <class U> void f (U);
2417 };
2418 template <> template <class U> void S<int>::f(U) {}
2419
2420 That's a specialization -- but of the entire template. */
2421 specialization = 1;
2422 break;
2423
2424 default:
2425 gcc_unreachable ();
2426 }
2427
2428 if (specialization || member_specialization)
2429 {
2430 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2431 for (; t; t = TREE_CHAIN (t))
2432 if (TREE_PURPOSE (t))
2433 {
2434 permerror (input_location,
2435 "default argument specified in explicit specialization");
2436 break;
2437 }
2438 }
2439
2440 if (specialization || member_specialization || explicit_instantiation)
2441 {
2442 tree tmpl = NULL_TREE;
2443 tree targs = NULL_TREE;
2444
2445 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2446 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2447 {
2448 tree fns;
2449
2450 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2451 if (ctype)
2452 fns = dname;
2453 else
2454 {
2455 /* If there is no class context, the explicit instantiation
2456 must be at namespace scope. */
2457 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2458
2459 /* Find the namespace binding, using the declaration
2460 context. */
2461 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2462 false, true);
2463 if (fns == error_mark_node || !is_overloaded_fn (fns))
2464 {
2465 error ("%qD is not a template function", dname);
2466 fns = error_mark_node;
2467 }
2468 else
2469 {
2470 tree fn = OVL_CURRENT (fns);
2471 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2472 CP_DECL_CONTEXT (fn)))
2473 error ("%qD is not declared in %qD",
2474 decl, current_namespace);
2475 }
2476 }
2477
2478 declarator = lookup_template_function (fns, NULL_TREE);
2479 }
2480
2481 if (declarator == error_mark_node)
2482 return error_mark_node;
2483
2484 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2485 {
2486 if (!explicit_instantiation)
2487 /* A specialization in class scope. This is invalid,
2488 but the error will already have been flagged by
2489 check_specialization_scope. */
2490 return error_mark_node;
2491 else
2492 {
2493 /* It's not valid to write an explicit instantiation in
2494 class scope, e.g.:
2495
2496 class C { template void f(); }
2497
2498 This case is caught by the parser. However, on
2499 something like:
2500
2501 template class C { void f(); };
2502
2503 (which is invalid) we can get here. The error will be
2504 issued later. */
2505 ;
2506 }
2507
2508 return decl;
2509 }
2510 else if (ctype != NULL_TREE
2511 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2512 IDENTIFIER_NODE))
2513 {
2514 /* Find the list of functions in ctype that have the same
2515 name as the declared function. */
2516 tree name = TREE_OPERAND (declarator, 0);
2517 tree fns = NULL_TREE;
2518 int idx;
2519
2520 if (constructor_name_p (name, ctype))
2521 {
2522 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2523
2524 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2525 : !CLASSTYPE_DESTRUCTORS (ctype))
2526 {
2527 /* From [temp.expl.spec]:
2528
2529 If such an explicit specialization for the member
2530 of a class template names an implicitly-declared
2531 special member function (clause _special_), the
2532 program is ill-formed.
2533
2534 Similar language is found in [temp.explicit]. */
2535 error ("specialization of implicitly-declared special member function");
2536 return error_mark_node;
2537 }
2538
2539 name = is_constructor ? ctor_identifier : dtor_identifier;
2540 }
2541
2542 if (!DECL_CONV_FN_P (decl))
2543 {
2544 idx = lookup_fnfields_1 (ctype, name);
2545 if (idx >= 0)
2546 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2547 }
2548 else
2549 {
2550 vec<tree, va_gc> *methods;
2551 tree ovl;
2552
2553 /* For a type-conversion operator, we cannot do a
2554 name-based lookup. We might be looking for `operator
2555 int' which will be a specialization of `operator T'.
2556 So, we find *all* the conversion operators, and then
2557 select from them. */
2558 fns = NULL_TREE;
2559
2560 methods = CLASSTYPE_METHOD_VEC (ctype);
2561 if (methods)
2562 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2563 methods->iterate (idx, &ovl);
2564 ++idx)
2565 {
2566 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2567 /* There are no more conversion functions. */
2568 break;
2569
2570 /* Glue all these conversion functions together
2571 with those we already have. */
2572 for (; ovl; ovl = OVL_NEXT (ovl))
2573 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2574 }
2575 }
2576
2577 if (fns == NULL_TREE)
2578 {
2579 error ("no member function %qD declared in %qT", name, ctype);
2580 return error_mark_node;
2581 }
2582 else
2583 TREE_OPERAND (declarator, 0) = fns;
2584 }
2585
2586 /* Figure out what exactly is being specialized at this point.
2587 Note that for an explicit instantiation, even one for a
2588 member function, we cannot tell apriori whether the
2589 instantiation is for a member template, or just a member
2590 function of a template class. Even if a member template is
2591 being instantiated, the member template arguments may be
2592 elided if they can be deduced from the rest of the
2593 declaration. */
2594 tmpl = determine_specialization (declarator, decl,
2595 &targs,
2596 member_specialization,
2597 template_count,
2598 tsk);
2599
2600 if (!tmpl || tmpl == error_mark_node)
2601 /* We couldn't figure out what this declaration was
2602 specializing. */
2603 return error_mark_node;
2604 else
2605 {
2606 tree gen_tmpl = most_general_template (tmpl);
2607
2608 if (explicit_instantiation)
2609 {
2610 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2611 is done by do_decl_instantiation later. */
2612
2613 int arg_depth = TMPL_ARGS_DEPTH (targs);
2614 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2615
2616 if (arg_depth > parm_depth)
2617 {
2618 /* If TMPL is not the most general template (for
2619 example, if TMPL is a friend template that is
2620 injected into namespace scope), then there will
2621 be too many levels of TARGS. Remove some of them
2622 here. */
2623 int i;
2624 tree new_targs;
2625
2626 new_targs = make_tree_vec (parm_depth);
2627 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2628 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2629 = TREE_VEC_ELT (targs, i);
2630 targs = new_targs;
2631 }
2632
2633 return instantiate_template (tmpl, targs, tf_error);
2634 }
2635
2636 /* If we thought that the DECL was a member function, but it
2637 turns out to be specializing a static member function,
2638 make DECL a static member function as well. */
2639 if (DECL_STATIC_FUNCTION_P (tmpl)
2640 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2641 revert_static_member_fn (decl);
2642
2643 /* If this is a specialization of a member template of a
2644 template class, we want to return the TEMPLATE_DECL, not
2645 the specialization of it. */
2646 if (tsk == tsk_template)
2647 {
2648 tree result = DECL_TEMPLATE_RESULT (tmpl);
2649 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2650 DECL_INITIAL (result) = NULL_TREE;
2651 if (have_def)
2652 {
2653 tree parm;
2654 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2655 DECL_SOURCE_LOCATION (result)
2656 = DECL_SOURCE_LOCATION (decl);
2657 /* We want to use the argument list specified in the
2658 definition, not in the original declaration. */
2659 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2660 for (parm = DECL_ARGUMENTS (result); parm;
2661 parm = DECL_CHAIN (parm))
2662 DECL_CONTEXT (parm) = result;
2663 }
2664 return register_specialization (tmpl, gen_tmpl, targs,
2665 is_friend, 0);
2666 }
2667
2668 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2669 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2670
2671 /* Inherit default function arguments from the template
2672 DECL is specializing. */
2673 copy_default_args_to_explicit_spec (decl);
2674
2675 /* This specialization has the same protection as the
2676 template it specializes. */
2677 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2678 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2679
2680 /* 7.1.1-1 [dcl.stc]
2681
2682 A storage-class-specifier shall not be specified in an
2683 explicit specialization...
2684
2685 The parser rejects these, so unless action is taken here,
2686 explicit function specializations will always appear with
2687 global linkage.
2688
2689 The action recommended by the C++ CWG in response to C++
2690 defect report 605 is to make the storage class and linkage
2691 of the explicit specialization match the templated function:
2692
2693 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2694 */
2695 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2696 {
2697 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2698 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2699
2700 /* This specialization has the same linkage and visibility as
2701 the function template it specializes. */
2702 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2703 if (! TREE_PUBLIC (decl))
2704 {
2705 DECL_INTERFACE_KNOWN (decl) = 1;
2706 DECL_NOT_REALLY_EXTERN (decl) = 1;
2707 }
2708 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2709 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2710 {
2711 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2712 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2713 }
2714 }
2715
2716 /* If DECL is a friend declaration, declared using an
2717 unqualified name, the namespace associated with DECL may
2718 have been set incorrectly. For example, in:
2719
2720 template <typename T> void f(T);
2721 namespace N {
2722 struct S { friend void f<int>(int); }
2723 }
2724
2725 we will have set the DECL_CONTEXT for the friend
2726 declaration to N, rather than to the global namespace. */
2727 if (DECL_NAMESPACE_SCOPE_P (decl))
2728 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2729
2730 if (is_friend && !have_def)
2731 /* This is not really a declaration of a specialization.
2732 It's just the name of an instantiation. But, it's not
2733 a request for an instantiation, either. */
2734 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2735 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2736 /* This is indeed a specialization. In case of constructors
2737 and destructors, we need in-charge and not-in-charge
2738 versions in V3 ABI. */
2739 clone_function_decl (decl, /*update_method_vec_p=*/0);
2740
2741 /* Register this specialization so that we can find it
2742 again. */
2743 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2744 }
2745 }
2746
2747 return decl;
2748 }
2749
2750 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2751 parameters. These are represented in the same format used for
2752 DECL_TEMPLATE_PARMS. */
2753
2754 int
2755 comp_template_parms (const_tree parms1, const_tree parms2)
2756 {
2757 const_tree p1;
2758 const_tree p2;
2759
2760 if (parms1 == parms2)
2761 return 1;
2762
2763 for (p1 = parms1, p2 = parms2;
2764 p1 != NULL_TREE && p2 != NULL_TREE;
2765 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2766 {
2767 tree t1 = TREE_VALUE (p1);
2768 tree t2 = TREE_VALUE (p2);
2769 int i;
2770
2771 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2772 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2773
2774 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2775 return 0;
2776
2777 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2778 {
2779 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2780 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2781
2782 /* If either of the template parameters are invalid, assume
2783 they match for the sake of error recovery. */
2784 if (parm1 == error_mark_node || parm2 == error_mark_node)
2785 return 1;
2786
2787 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2788 return 0;
2789
2790 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2791 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2792 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2793 continue;
2794 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2795 return 0;
2796 }
2797 }
2798
2799 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2800 /* One set of parameters has more parameters lists than the
2801 other. */
2802 return 0;
2803
2804 return 1;
2805 }
2806
2807 /* Determine whether PARM is a parameter pack. */
2808
2809 bool
2810 template_parameter_pack_p (const_tree parm)
2811 {
2812 /* Determine if we have a non-type template parameter pack. */
2813 if (TREE_CODE (parm) == PARM_DECL)
2814 return (DECL_TEMPLATE_PARM_P (parm)
2815 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2816 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2817 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2818
2819 /* If this is a list of template parameters, we could get a
2820 TYPE_DECL or a TEMPLATE_DECL. */
2821 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2822 parm = TREE_TYPE (parm);
2823
2824 /* Otherwise it must be a type template parameter. */
2825 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2826 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2827 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2828 }
2829
2830 /* Determine if T is a function parameter pack. */
2831
2832 bool
2833 function_parameter_pack_p (const_tree t)
2834 {
2835 if (t && TREE_CODE (t) == PARM_DECL)
2836 return FUNCTION_PARAMETER_PACK_P (t);
2837 return false;
2838 }
2839
2840 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2841 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2842
2843 tree
2844 get_function_template_decl (const_tree primary_func_tmpl_inst)
2845 {
2846 if (! primary_func_tmpl_inst
2847 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2848 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2849 return NULL;
2850
2851 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2852 }
2853
2854 /* Return true iff the function parameter PARAM_DECL was expanded
2855 from the function parameter pack PACK. */
2856
2857 bool
2858 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2859 {
2860 if (DECL_ARTIFICIAL (param_decl)
2861 || !function_parameter_pack_p (pack))
2862 return false;
2863
2864 /* The parameter pack and its pack arguments have the same
2865 DECL_PARM_INDEX. */
2866 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2867 }
2868
2869 /* Determine whether ARGS describes a variadic template args list,
2870 i.e., one that is terminated by a template argument pack. */
2871
2872 static bool
2873 template_args_variadic_p (tree args)
2874 {
2875 int nargs;
2876 tree last_parm;
2877
2878 if (args == NULL_TREE)
2879 return false;
2880
2881 args = INNERMOST_TEMPLATE_ARGS (args);
2882 nargs = TREE_VEC_LENGTH (args);
2883
2884 if (nargs == 0)
2885 return false;
2886
2887 last_parm = TREE_VEC_ELT (args, nargs - 1);
2888
2889 return ARGUMENT_PACK_P (last_parm);
2890 }
2891
2892 /* Generate a new name for the parameter pack name NAME (an
2893 IDENTIFIER_NODE) that incorporates its */
2894
2895 static tree
2896 make_ith_pack_parameter_name (tree name, int i)
2897 {
2898 /* Munge the name to include the parameter index. */
2899 #define NUMBUF_LEN 128
2900 char numbuf[NUMBUF_LEN];
2901 char* newname;
2902 int newname_len;
2903
2904 if (name == NULL_TREE)
2905 return name;
2906 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2907 newname_len = IDENTIFIER_LENGTH (name)
2908 + strlen (numbuf) + 2;
2909 newname = (char*)alloca (newname_len);
2910 snprintf (newname, newname_len,
2911 "%s#%i", IDENTIFIER_POINTER (name), i);
2912 return get_identifier (newname);
2913 }
2914
2915 /* Return true if T is a primary function, class or alias template
2916 instantiation. */
2917
2918 bool
2919 primary_template_instantiation_p (const_tree t)
2920 {
2921 if (!t)
2922 return false;
2923
2924 if (TREE_CODE (t) == FUNCTION_DECL)
2925 return DECL_LANG_SPECIFIC (t)
2926 && DECL_TEMPLATE_INSTANTIATION (t)
2927 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2928 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2929 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2930 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2931 else if (alias_template_specialization_p (t))
2932 return true;
2933 return false;
2934 }
2935
2936 /* Return true if PARM is a template template parameter. */
2937
2938 bool
2939 template_template_parameter_p (const_tree parm)
2940 {
2941 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2942 }
2943
2944 /* Return true iff PARM is a DECL representing a type template
2945 parameter. */
2946
2947 bool
2948 template_type_parameter_p (const_tree parm)
2949 {
2950 return (parm
2951 && (TREE_CODE (parm) == TYPE_DECL
2952 || TREE_CODE (parm) == TEMPLATE_DECL)
2953 && DECL_TEMPLATE_PARM_P (parm));
2954 }
2955
2956 /* Return the template parameters of T if T is a
2957 primary template instantiation, NULL otherwise. */
2958
2959 tree
2960 get_primary_template_innermost_parameters (const_tree t)
2961 {
2962 tree parms = NULL, template_info = NULL;
2963
2964 if ((template_info = get_template_info (t))
2965 && primary_template_instantiation_p (t))
2966 parms = INNERMOST_TEMPLATE_PARMS
2967 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2968
2969 return parms;
2970 }
2971
2972 /* Return the template parameters of the LEVELth level from the full list
2973 of template parameters PARMS. */
2974
2975 tree
2976 get_template_parms_at_level (tree parms, int level)
2977 {
2978 tree p;
2979 if (!parms
2980 || TREE_CODE (parms) != TREE_LIST
2981 || level > TMPL_PARMS_DEPTH (parms))
2982 return NULL_TREE;
2983
2984 for (p = parms; p; p = TREE_CHAIN (p))
2985 if (TMPL_PARMS_DEPTH (p) == level)
2986 return p;
2987
2988 return NULL_TREE;
2989 }
2990
2991 /* Returns the template arguments of T if T is a template instantiation,
2992 NULL otherwise. */
2993
2994 tree
2995 get_template_innermost_arguments (const_tree t)
2996 {
2997 tree args = NULL, template_info = NULL;
2998
2999 if ((template_info = get_template_info (t))
3000 && TI_ARGS (template_info))
3001 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3002
3003 return args;
3004 }
3005
3006 /* Return the argument pack elements of T if T is a template argument pack,
3007 NULL otherwise. */
3008
3009 tree
3010 get_template_argument_pack_elems (const_tree t)
3011 {
3012 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3013 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3014 return NULL;
3015
3016 return ARGUMENT_PACK_ARGS (t);
3017 }
3018
3019 /* Structure used to track the progress of find_parameter_packs_r. */
3020 struct find_parameter_pack_data
3021 {
3022 /* TREE_LIST that will contain all of the parameter packs found by
3023 the traversal. */
3024 tree* parameter_packs;
3025
3026 /* Set of AST nodes that have been visited by the traversal. */
3027 struct pointer_set_t *visited;
3028 };
3029
3030 /* Identifies all of the argument packs that occur in a template
3031 argument and appends them to the TREE_LIST inside DATA, which is a
3032 find_parameter_pack_data structure. This is a subroutine of
3033 make_pack_expansion and uses_parameter_packs. */
3034 static tree
3035 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3036 {
3037 tree t = *tp;
3038 struct find_parameter_pack_data* ppd =
3039 (struct find_parameter_pack_data*)data;
3040 bool parameter_pack_p = false;
3041
3042 /* Handle type aliases/typedefs. */
3043 if (TYPE_P (t)
3044 && TYPE_NAME (t)
3045 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
3046 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3047 {
3048 if (TYPE_TEMPLATE_INFO (t))
3049 cp_walk_tree (&TYPE_TI_ARGS (t),
3050 &find_parameter_packs_r,
3051 ppd, ppd->visited);
3052 *walk_subtrees = 0;
3053 return NULL_TREE;
3054 }
3055
3056 /* Identify whether this is a parameter pack or not. */
3057 switch (TREE_CODE (t))
3058 {
3059 case TEMPLATE_PARM_INDEX:
3060 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3061 parameter_pack_p = true;
3062 break;
3063
3064 case TEMPLATE_TYPE_PARM:
3065 t = TYPE_MAIN_VARIANT (t);
3066 case TEMPLATE_TEMPLATE_PARM:
3067 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3068 parameter_pack_p = true;
3069 break;
3070
3071 case PARM_DECL:
3072 if (FUNCTION_PARAMETER_PACK_P (t))
3073 {
3074 /* We don't want to walk into the type of a PARM_DECL,
3075 because we don't want to see the type parameter pack. */
3076 *walk_subtrees = 0;
3077 parameter_pack_p = true;
3078 }
3079 break;
3080
3081 case BASES:
3082 parameter_pack_p = true;
3083 break;
3084 default:
3085 /* Not a parameter pack. */
3086 break;
3087 }
3088
3089 if (parameter_pack_p)
3090 {
3091 /* Add this parameter pack to the list. */
3092 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3093 }
3094
3095 if (TYPE_P (t))
3096 cp_walk_tree (&TYPE_CONTEXT (t),
3097 &find_parameter_packs_r, ppd, ppd->visited);
3098
3099 /* This switch statement will return immediately if we don't find a
3100 parameter pack. */
3101 switch (TREE_CODE (t))
3102 {
3103 case TEMPLATE_PARM_INDEX:
3104 return NULL_TREE;
3105
3106 case BOUND_TEMPLATE_TEMPLATE_PARM:
3107 /* Check the template itself. */
3108 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3109 &find_parameter_packs_r, ppd, ppd->visited);
3110 /* Check the template arguments. */
3111 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3112 ppd->visited);
3113 *walk_subtrees = 0;
3114 return NULL_TREE;
3115
3116 case TEMPLATE_TYPE_PARM:
3117 case TEMPLATE_TEMPLATE_PARM:
3118 return NULL_TREE;
3119
3120 case PARM_DECL:
3121 return NULL_TREE;
3122
3123 case RECORD_TYPE:
3124 if (TYPE_PTRMEMFUNC_P (t))
3125 return NULL_TREE;
3126 /* Fall through. */
3127
3128 case UNION_TYPE:
3129 case ENUMERAL_TYPE:
3130 if (TYPE_TEMPLATE_INFO (t))
3131 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3132 &find_parameter_packs_r, ppd, ppd->visited);
3133
3134 *walk_subtrees = 0;
3135 return NULL_TREE;
3136
3137 case CONSTRUCTOR:
3138 case TEMPLATE_DECL:
3139 cp_walk_tree (&TREE_TYPE (t),
3140 &find_parameter_packs_r, ppd, ppd->visited);
3141 return NULL_TREE;
3142
3143 case TYPENAME_TYPE:
3144 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3145 ppd, ppd->visited);
3146 *walk_subtrees = 0;
3147 return NULL_TREE;
3148
3149 case TYPE_PACK_EXPANSION:
3150 case EXPR_PACK_EXPANSION:
3151 *walk_subtrees = 0;
3152 return NULL_TREE;
3153
3154 case INTEGER_TYPE:
3155 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3156 ppd, ppd->visited);
3157 *walk_subtrees = 0;
3158 return NULL_TREE;
3159
3160 case IDENTIFIER_NODE:
3161 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3162 ppd->visited);
3163 *walk_subtrees = 0;
3164 return NULL_TREE;
3165
3166 default:
3167 return NULL_TREE;
3168 }
3169
3170 return NULL_TREE;
3171 }
3172
3173 /* Determines if the expression or type T uses any parameter packs. */
3174 bool
3175 uses_parameter_packs (tree t)
3176 {
3177 tree parameter_packs = NULL_TREE;
3178 struct find_parameter_pack_data ppd;
3179 ppd.parameter_packs = &parameter_packs;
3180 ppd.visited = pointer_set_create ();
3181 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3182 pointer_set_destroy (ppd.visited);
3183 return parameter_packs != NULL_TREE;
3184 }
3185
3186 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3187 representation a base-class initializer into a parameter pack
3188 expansion. If all goes well, the resulting node will be an
3189 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3190 respectively. */
3191 tree
3192 make_pack_expansion (tree arg)
3193 {
3194 tree result;
3195 tree parameter_packs = NULL_TREE;
3196 bool for_types = false;
3197 struct find_parameter_pack_data ppd;
3198
3199 if (!arg || arg == error_mark_node)
3200 return arg;
3201
3202 if (TREE_CODE (arg) == TREE_LIST)
3203 {
3204 /* The only time we will see a TREE_LIST here is for a base
3205 class initializer. In this case, the TREE_PURPOSE will be a
3206 _TYPE node (representing the base class expansion we're
3207 initializing) and the TREE_VALUE will be a TREE_LIST
3208 containing the initialization arguments.
3209
3210 The resulting expansion looks somewhat different from most
3211 expansions. Rather than returning just one _EXPANSION, we
3212 return a TREE_LIST whose TREE_PURPOSE is a
3213 TYPE_PACK_EXPANSION containing the bases that will be
3214 initialized. The TREE_VALUE will be identical to the
3215 original TREE_VALUE, which is a list of arguments that will
3216 be passed to each base. We do not introduce any new pack
3217 expansion nodes into the TREE_VALUE (although it is possible
3218 that some already exist), because the TREE_PURPOSE and
3219 TREE_VALUE all need to be expanded together with the same
3220 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3221 resulting TREE_PURPOSE will mention the parameter packs in
3222 both the bases and the arguments to the bases. */
3223 tree purpose;
3224 tree value;
3225 tree parameter_packs = NULL_TREE;
3226
3227 /* Determine which parameter packs will be used by the base
3228 class expansion. */
3229 ppd.visited = pointer_set_create ();
3230 ppd.parameter_packs = &parameter_packs;
3231 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3232 &ppd, ppd.visited);
3233
3234 if (parameter_packs == NULL_TREE)
3235 {
3236 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3237 pointer_set_destroy (ppd.visited);
3238 return error_mark_node;
3239 }
3240
3241 if (TREE_VALUE (arg) != void_type_node)
3242 {
3243 /* Collect the sets of parameter packs used in each of the
3244 initialization arguments. */
3245 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3246 {
3247 /* Determine which parameter packs will be expanded in this
3248 argument. */
3249 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3250 &ppd, ppd.visited);
3251 }
3252 }
3253
3254 pointer_set_destroy (ppd.visited);
3255
3256 /* Create the pack expansion type for the base type. */
3257 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3258 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3259 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3260
3261 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3262 they will rarely be compared to anything. */
3263 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3264
3265 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3266 }
3267
3268 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3269 for_types = true;
3270
3271 /* Build the PACK_EXPANSION_* node. */
3272 result = for_types
3273 ? cxx_make_type (TYPE_PACK_EXPANSION)
3274 : make_node (EXPR_PACK_EXPANSION);
3275 SET_PACK_EXPANSION_PATTERN (result, arg);
3276 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3277 {
3278 /* Propagate type and const-expression information. */
3279 TREE_TYPE (result) = TREE_TYPE (arg);
3280 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3281 }
3282 else
3283 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3284 they will rarely be compared to anything. */
3285 SET_TYPE_STRUCTURAL_EQUALITY (result);
3286
3287 /* Determine which parameter packs will be expanded. */
3288 ppd.parameter_packs = &parameter_packs;
3289 ppd.visited = pointer_set_create ();
3290 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3291 pointer_set_destroy (ppd.visited);
3292
3293 /* Make sure we found some parameter packs. */
3294 if (parameter_packs == NULL_TREE)
3295 {
3296 if (TYPE_P (arg))
3297 error ("expansion pattern %<%T%> contains no argument packs", arg);
3298 else
3299 error ("expansion pattern %<%E%> contains no argument packs", arg);
3300 return error_mark_node;
3301 }
3302 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3303
3304 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3305
3306 return result;
3307 }
3308
3309 /* Checks T for any "bare" parameter packs, which have not yet been
3310 expanded, and issues an error if any are found. This operation can
3311 only be done on full expressions or types (e.g., an expression
3312 statement, "if" condition, etc.), because we could have expressions like:
3313
3314 foo(f(g(h(args)))...)
3315
3316 where "args" is a parameter pack. check_for_bare_parameter_packs
3317 should not be called for the subexpressions args, h(args),
3318 g(h(args)), or f(g(h(args))), because we would produce erroneous
3319 error messages.
3320
3321 Returns TRUE and emits an error if there were bare parameter packs,
3322 returns FALSE otherwise. */
3323 bool
3324 check_for_bare_parameter_packs (tree t)
3325 {
3326 tree parameter_packs = NULL_TREE;
3327 struct find_parameter_pack_data ppd;
3328
3329 if (!processing_template_decl || !t || t == error_mark_node)
3330 return false;
3331
3332 if (TREE_CODE (t) == TYPE_DECL)
3333 t = TREE_TYPE (t);
3334
3335 ppd.parameter_packs = &parameter_packs;
3336 ppd.visited = pointer_set_create ();
3337 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3338 pointer_set_destroy (ppd.visited);
3339
3340 if (parameter_packs)
3341 {
3342 error ("parameter packs not expanded with %<...%>:");
3343 while (parameter_packs)
3344 {
3345 tree pack = TREE_VALUE (parameter_packs);
3346 tree name = NULL_TREE;
3347
3348 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3349 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3350 name = TYPE_NAME (pack);
3351 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3352 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3353 else
3354 name = DECL_NAME (pack);
3355
3356 if (name)
3357 inform (input_location, " %qD", name);
3358 else
3359 inform (input_location, " <anonymous>");
3360
3361 parameter_packs = TREE_CHAIN (parameter_packs);
3362 }
3363
3364 return true;
3365 }
3366
3367 return false;
3368 }
3369
3370 /* Expand any parameter packs that occur in the template arguments in
3371 ARGS. */
3372 tree
3373 expand_template_argument_pack (tree args)
3374 {
3375 tree result_args = NULL_TREE;
3376 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3377 int num_result_args = -1;
3378 int non_default_args_count = -1;
3379
3380 /* First, determine if we need to expand anything, and the number of
3381 slots we'll need. */
3382 for (in_arg = 0; in_arg < nargs; ++in_arg)
3383 {
3384 tree arg = TREE_VEC_ELT (args, in_arg);
3385 if (arg == NULL_TREE)
3386 return args;
3387 if (ARGUMENT_PACK_P (arg))
3388 {
3389 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3390 if (num_result_args < 0)
3391 num_result_args = in_arg + num_packed;
3392 else
3393 num_result_args += num_packed;
3394 }
3395 else
3396 {
3397 if (num_result_args >= 0)
3398 num_result_args++;
3399 }
3400 }
3401
3402 /* If no expansion is necessary, we're done. */
3403 if (num_result_args < 0)
3404 return args;
3405
3406 /* Expand arguments. */
3407 result_args = make_tree_vec (num_result_args);
3408 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3409 non_default_args_count =
3410 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3411 for (in_arg = 0; in_arg < nargs; ++in_arg)
3412 {
3413 tree arg = TREE_VEC_ELT (args, in_arg);
3414 if (ARGUMENT_PACK_P (arg))
3415 {
3416 tree packed = ARGUMENT_PACK_ARGS (arg);
3417 int i, num_packed = TREE_VEC_LENGTH (packed);
3418 for (i = 0; i < num_packed; ++i, ++out_arg)
3419 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3420 if (non_default_args_count > 0)
3421 non_default_args_count += num_packed;
3422 }
3423 else
3424 {
3425 TREE_VEC_ELT (result_args, out_arg) = arg;
3426 ++out_arg;
3427 }
3428 }
3429 if (non_default_args_count >= 0)
3430 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3431 return result_args;
3432 }
3433
3434 /* Checks if DECL shadows a template parameter.
3435
3436 [temp.local]: A template-parameter shall not be redeclared within its
3437 scope (including nested scopes).
3438
3439 Emits an error and returns TRUE if the DECL shadows a parameter,
3440 returns FALSE otherwise. */
3441
3442 bool
3443 check_template_shadow (tree decl)
3444 {
3445 tree olddecl;
3446
3447 /* If we're not in a template, we can't possibly shadow a template
3448 parameter. */
3449 if (!current_template_parms)
3450 return true;
3451
3452 /* Figure out what we're shadowing. */
3453 if (TREE_CODE (decl) == OVERLOAD)
3454 decl = OVL_CURRENT (decl);
3455 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3456
3457 /* If there's no previous binding for this name, we're not shadowing
3458 anything, let alone a template parameter. */
3459 if (!olddecl)
3460 return true;
3461
3462 /* If we're not shadowing a template parameter, we're done. Note
3463 that OLDDECL might be an OVERLOAD (or perhaps even an
3464 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3465 node. */
3466 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3467 return true;
3468
3469 /* We check for decl != olddecl to avoid bogus errors for using a
3470 name inside a class. We check TPFI to avoid duplicate errors for
3471 inline member templates. */
3472 if (decl == olddecl
3473 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3474 return true;
3475
3476 error ("declaration of %q+#D", decl);
3477 error (" shadows template parm %q+#D", olddecl);
3478 return false;
3479 }
3480
3481 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3482 ORIG_LEVEL, DECL, and TYPE. */
3483
3484 static tree
3485 build_template_parm_index (int index,
3486 int level,
3487 int orig_level,
3488 tree decl,
3489 tree type)
3490 {
3491 tree t = make_node (TEMPLATE_PARM_INDEX);
3492 TEMPLATE_PARM_IDX (t) = index;
3493 TEMPLATE_PARM_LEVEL (t) = level;
3494 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3495 TEMPLATE_PARM_DECL (t) = decl;
3496 TREE_TYPE (t) = type;
3497 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3498 TREE_READONLY (t) = TREE_READONLY (decl);
3499
3500 return t;
3501 }
3502
3503 /* Find the canonical type parameter for the given template type
3504 parameter. Returns the canonical type parameter, which may be TYPE
3505 if no such parameter existed. */
3506
3507 static tree
3508 canonical_type_parameter (tree type)
3509 {
3510 tree list;
3511 int idx = TEMPLATE_TYPE_IDX (type);
3512 if (!canonical_template_parms)
3513 vec_alloc (canonical_template_parms, idx+1);
3514
3515 while (canonical_template_parms->length () <= (unsigned)idx)
3516 vec_safe_push (canonical_template_parms, NULL_TREE);
3517
3518 list = (*canonical_template_parms)[idx];
3519 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3520 list = TREE_CHAIN (list);
3521
3522 if (list)
3523 return TREE_VALUE (list);
3524 else
3525 {
3526 (*canonical_template_parms)[idx]
3527 = tree_cons (NULL_TREE, type,
3528 (*canonical_template_parms)[idx]);
3529 return type;
3530 }
3531 }
3532
3533 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3534 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3535 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3536 new one is created. */
3537
3538 static tree
3539 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3540 tsubst_flags_t complain)
3541 {
3542 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3543 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3544 != TEMPLATE_PARM_LEVEL (index) - levels)
3545 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3546 {
3547 tree orig_decl = TEMPLATE_PARM_DECL (index);
3548 tree decl, t;
3549
3550 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3551 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3552 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3553 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3554 DECL_ARTIFICIAL (decl) = 1;
3555 SET_DECL_TEMPLATE_PARM_P (decl);
3556
3557 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3558 TEMPLATE_PARM_LEVEL (index) - levels,
3559 TEMPLATE_PARM_ORIG_LEVEL (index),
3560 decl, type);
3561 TEMPLATE_PARM_DESCENDANTS (index) = t;
3562 TEMPLATE_PARM_PARAMETER_PACK (t)
3563 = TEMPLATE_PARM_PARAMETER_PACK (index);
3564
3565 /* Template template parameters need this. */
3566 if (TREE_CODE (decl) == TEMPLATE_DECL)
3567 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3568 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3569 args, complain);
3570 }
3571
3572 return TEMPLATE_PARM_DESCENDANTS (index);
3573 }
3574
3575 /* Process information from new template parameter PARM and append it
3576 to the LIST being built. This new parameter is a non-type
3577 parameter iff IS_NON_TYPE is true. This new parameter is a
3578 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3579 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3580 parameter list PARM belongs to. This is used used to create a
3581 proper canonical type for the type of PARM that is to be created,
3582 iff PARM is a type. If the size is not known, this parameter shall
3583 be set to 0. */
3584
3585 tree
3586 process_template_parm (tree list, location_t parm_loc, tree parm,
3587 bool is_non_type, bool is_parameter_pack)
3588 {
3589 tree decl = 0;
3590 tree defval;
3591 tree err_parm_list;
3592 int idx = 0;
3593
3594 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3595 defval = TREE_PURPOSE (parm);
3596
3597 if (list)
3598 {
3599 tree p = tree_last (list);
3600
3601 if (p && TREE_VALUE (p) != error_mark_node)
3602 {
3603 p = TREE_VALUE (p);
3604 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3605 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3606 else
3607 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3608 }
3609
3610 ++idx;
3611 }
3612 else
3613 idx = 0;
3614
3615 if (is_non_type)
3616 {
3617 parm = TREE_VALUE (parm);
3618
3619 SET_DECL_TEMPLATE_PARM_P (parm);
3620
3621 if (TREE_TYPE (parm) == error_mark_node)
3622 {
3623 err_parm_list = build_tree_list (defval, parm);
3624 TREE_VALUE (err_parm_list) = error_mark_node;
3625 return chainon (list, err_parm_list);
3626 }
3627 else
3628 {
3629 /* [temp.param]
3630
3631 The top-level cv-qualifiers on the template-parameter are
3632 ignored when determining its type. */
3633 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3634 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3635 {
3636 err_parm_list = build_tree_list (defval, parm);
3637 TREE_VALUE (err_parm_list) = error_mark_node;
3638 return chainon (list, err_parm_list);
3639 }
3640
3641 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3642 {
3643 /* This template parameter is not a parameter pack, but it
3644 should be. Complain about "bare" parameter packs. */
3645 check_for_bare_parameter_packs (TREE_TYPE (parm));
3646
3647 /* Recover by calling this a parameter pack. */
3648 is_parameter_pack = true;
3649 }
3650 }
3651
3652 /* A template parameter is not modifiable. */
3653 TREE_CONSTANT (parm) = 1;
3654 TREE_READONLY (parm) = 1;
3655 decl = build_decl (parm_loc,
3656 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3657 TREE_CONSTANT (decl) = 1;
3658 TREE_READONLY (decl) = 1;
3659 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3660 = build_template_parm_index (idx, processing_template_decl,
3661 processing_template_decl,
3662 decl, TREE_TYPE (parm));
3663
3664 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3665 = is_parameter_pack;
3666 }
3667 else
3668 {
3669 tree t;
3670 parm = TREE_VALUE (TREE_VALUE (parm));
3671
3672 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3673 {
3674 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3675 /* This is for distinguishing between real templates and template
3676 template parameters */
3677 TREE_TYPE (parm) = t;
3678 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3679 decl = parm;
3680 }
3681 else
3682 {
3683 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3684 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3685 decl = build_decl (parm_loc,
3686 TYPE_DECL, parm, t);
3687 }
3688
3689 TYPE_NAME (t) = decl;
3690 TYPE_STUB_DECL (t) = decl;
3691 parm = decl;
3692 TEMPLATE_TYPE_PARM_INDEX (t)
3693 = build_template_parm_index (idx, processing_template_decl,
3694 processing_template_decl,
3695 decl, TREE_TYPE (parm));
3696 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3697 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3698 }
3699 DECL_ARTIFICIAL (decl) = 1;
3700 SET_DECL_TEMPLATE_PARM_P (decl);
3701 pushdecl (decl);
3702 parm = build_tree_list (defval, parm);
3703 return chainon (list, parm);
3704 }
3705
3706 /* The end of a template parameter list has been reached. Process the
3707 tree list into a parameter vector, converting each parameter into a more
3708 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3709 as PARM_DECLs. */
3710
3711 tree
3712 end_template_parm_list (tree parms)
3713 {
3714 int nparms;
3715 tree parm, next;
3716 tree saved_parmlist = make_tree_vec (list_length (parms));
3717
3718 current_template_parms
3719 = tree_cons (size_int (processing_template_decl),
3720 saved_parmlist, current_template_parms);
3721
3722 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3723 {
3724 next = TREE_CHAIN (parm);
3725 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3726 TREE_CHAIN (parm) = NULL_TREE;
3727 }
3728
3729 --processing_template_parmlist;
3730
3731 return saved_parmlist;
3732 }
3733
3734 /* end_template_decl is called after a template declaration is seen. */
3735
3736 void
3737 end_template_decl (void)
3738 {
3739 reset_specialization ();
3740
3741 if (! processing_template_decl)
3742 return;
3743
3744 /* This matches the pushlevel in begin_template_parm_list. */
3745 finish_scope ();
3746
3747 --processing_template_decl;
3748 current_template_parms = TREE_CHAIN (current_template_parms);
3749 }
3750
3751 /* Takes a TREE_LIST representing a template parameter and convert it
3752 into an argument suitable to be passed to the type substitution
3753 functions. Note that If the TREE_LIST contains an error_mark
3754 node, the returned argument is error_mark_node. */
3755
3756 static tree
3757 template_parm_to_arg (tree t)
3758 {
3759
3760 if (t == NULL_TREE
3761 || TREE_CODE (t) != TREE_LIST)
3762 return t;
3763
3764 if (error_operand_p (TREE_VALUE (t)))
3765 return error_mark_node;
3766
3767 t = TREE_VALUE (t);
3768
3769 if (TREE_CODE (t) == TYPE_DECL
3770 || TREE_CODE (t) == TEMPLATE_DECL)
3771 {
3772 t = TREE_TYPE (t);
3773
3774 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3775 {
3776 /* Turn this argument into a TYPE_ARGUMENT_PACK
3777 with a single element, which expands T. */
3778 tree vec = make_tree_vec (1);
3779 #ifdef ENABLE_CHECKING
3780 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3781 (vec, TREE_VEC_LENGTH (vec));
3782 #endif
3783 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3784
3785 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3786 SET_ARGUMENT_PACK_ARGS (t, vec);
3787 }
3788 }
3789 else
3790 {
3791 t = DECL_INITIAL (t);
3792
3793 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3794 {
3795 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3796 with a single element, which expands T. */
3797 tree vec = make_tree_vec (1);
3798 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3799 #ifdef ENABLE_CHECKING
3800 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3801 (vec, TREE_VEC_LENGTH (vec));
3802 #endif
3803 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3804
3805 t = make_node (NONTYPE_ARGUMENT_PACK);
3806 SET_ARGUMENT_PACK_ARGS (t, vec);
3807 TREE_TYPE (t) = type;
3808 }
3809 }
3810 return t;
3811 }
3812
3813 /* This function returns TRUE if PARM_PACK is a template parameter
3814 pack and if ARG_PACK is what template_parm_to_arg returned when
3815 passed PARM_PACK. */
3816
3817 static bool
3818 arg_from_parm_pack_p (tree arg_pack, tree parm_pack)
3819 {
3820 /* For clarity in the comments below let's use the representation
3821 argument_pack<elements>' to denote an argument pack and its
3822 elements.
3823
3824 In the 'if' block below, we want to detect cases where
3825 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
3826 check if ARG_PACK is an argument pack which sole element is
3827 the expansion of PARM_PACK. That argument pack is typically
3828 created by template_parm_to_arg when passed a parameter
3829 pack. */
3830
3831 if (arg_pack
3832 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
3833 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
3834 {
3835 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
3836 tree pattern = PACK_EXPANSION_PATTERN (expansion);
3837 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
3838 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
3839 /* The argument pack that the parameter maps to is just an
3840 expansion of the parameter itself, such as one would
3841 find in the implicit typedef of a class inside the
3842 class itself. Consider this parameter "unsubstituted",
3843 so that we will maintain the outer pack expansion. */
3844 return true;
3845 }
3846 return false;
3847 }
3848
3849 /* Given a set of template parameters, return them as a set of template
3850 arguments. The template parameters are represented as a TREE_VEC, in
3851 the form documented in cp-tree.h for template arguments. */
3852
3853 static tree
3854 template_parms_to_args (tree parms)
3855 {
3856 tree header;
3857 tree args = NULL_TREE;
3858 int length = TMPL_PARMS_DEPTH (parms);
3859 int l = length;
3860
3861 /* If there is only one level of template parameters, we do not
3862 create a TREE_VEC of TREE_VECs. Instead, we return a single
3863 TREE_VEC containing the arguments. */
3864 if (length > 1)
3865 args = make_tree_vec (length);
3866
3867 for (header = parms; header; header = TREE_CHAIN (header))
3868 {
3869 tree a = copy_node (TREE_VALUE (header));
3870 int i;
3871
3872 TREE_TYPE (a) = NULL_TREE;
3873 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3874 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3875
3876 #ifdef ENABLE_CHECKING
3877 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3878 #endif
3879
3880 if (length > 1)
3881 TREE_VEC_ELT (args, --l) = a;
3882 else
3883 args = a;
3884 }
3885
3886 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3887 /* This can happen for template parms of a template template
3888 parameter, e.g:
3889
3890 template<template<class T, class U> class TT> struct S;
3891
3892 Consider the level of the parms of TT; T and U both have
3893 level 2; TT has no template parm of level 1. So in this case
3894 the first element of full_template_args is NULL_TREE. If we
3895 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3896 of 2. This will make tsubst wrongly consider that T and U
3897 have level 1. Instead, let's create a dummy vector as the
3898 first element of full_template_args so that TMPL_ARG_DEPTH
3899 returns the correct depth for args. */
3900 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3901 return args;
3902 }
3903
3904 /* Within the declaration of a template, return the currently active
3905 template parameters as an argument TREE_VEC. */
3906
3907 static tree
3908 current_template_args (void)
3909 {
3910 return template_parms_to_args (current_template_parms);
3911 }
3912
3913 /* Update the declared TYPE by doing any lookups which were thought to be
3914 dependent, but are not now that we know the SCOPE of the declarator. */
3915
3916 tree
3917 maybe_update_decl_type (tree orig_type, tree scope)
3918 {
3919 tree type = orig_type;
3920
3921 if (type == NULL_TREE)
3922 return type;
3923
3924 if (TREE_CODE (orig_type) == TYPE_DECL)
3925 type = TREE_TYPE (type);
3926
3927 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3928 && dependent_type_p (type)
3929 /* Don't bother building up the args in this case. */
3930 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3931 {
3932 /* tsubst in the args corresponding to the template parameters,
3933 including auto if present. Most things will be unchanged, but
3934 make_typename_type and tsubst_qualified_id will resolve
3935 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3936 tree args = current_template_args ();
3937 tree auto_node = type_uses_auto (type);
3938 tree pushed;
3939 if (auto_node)
3940 {
3941 tree auto_vec = make_tree_vec (1);
3942 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3943 args = add_to_template_args (args, auto_vec);
3944 }
3945 pushed = push_scope (scope);
3946 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3947 if (pushed)
3948 pop_scope (scope);
3949 }
3950
3951 if (type == error_mark_node)
3952 return orig_type;
3953
3954 if (TREE_CODE (orig_type) == TYPE_DECL)
3955 {
3956 if (same_type_p (type, TREE_TYPE (orig_type)))
3957 type = orig_type;
3958 else
3959 type = TYPE_NAME (type);
3960 }
3961 return type;
3962 }
3963
3964 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3965 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3966 a member template. Used by push_template_decl below. */
3967
3968 static tree
3969 build_template_decl (tree decl, tree parms, bool member_template_p)
3970 {
3971 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3972 DECL_TEMPLATE_PARMS (tmpl) = parms;
3973 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3974 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3975 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3976
3977 return tmpl;
3978 }
3979
3980 struct template_parm_data
3981 {
3982 /* The level of the template parameters we are currently
3983 processing. */
3984 int level;
3985
3986 /* The index of the specialization argument we are currently
3987 processing. */
3988 int current_arg;
3989
3990 /* An array whose size is the number of template parameters. The
3991 elements are nonzero if the parameter has been used in any one
3992 of the arguments processed so far. */
3993 int* parms;
3994
3995 /* An array whose size is the number of template arguments. The
3996 elements are nonzero if the argument makes use of template
3997 parameters of this level. */
3998 int* arg_uses_template_parms;
3999 };
4000
4001 /* Subroutine of push_template_decl used to see if each template
4002 parameter in a partial specialization is used in the explicit
4003 argument list. If T is of the LEVEL given in DATA (which is
4004 treated as a template_parm_data*), then DATA->PARMS is marked
4005 appropriately. */
4006
4007 static int
4008 mark_template_parm (tree t, void* data)
4009 {
4010 int level;
4011 int idx;
4012 struct template_parm_data* tpd = (struct template_parm_data*) data;
4013
4014 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4015 {
4016 level = TEMPLATE_PARM_LEVEL (t);
4017 idx = TEMPLATE_PARM_IDX (t);
4018 }
4019 else
4020 {
4021 level = TEMPLATE_TYPE_LEVEL (t);
4022 idx = TEMPLATE_TYPE_IDX (t);
4023 }
4024
4025 if (level == tpd->level)
4026 {
4027 tpd->parms[idx] = 1;
4028 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4029 }
4030
4031 /* Return zero so that for_each_template_parm will continue the
4032 traversal of the tree; we want to mark *every* template parm. */
4033 return 0;
4034 }
4035
4036 /* Process the partial specialization DECL. */
4037
4038 static tree
4039 process_partial_specialization (tree decl)
4040 {
4041 tree type = TREE_TYPE (decl);
4042 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4043 tree specargs = CLASSTYPE_TI_ARGS (type);
4044 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4045 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4046 tree inner_parms;
4047 tree inst;
4048 int nargs = TREE_VEC_LENGTH (inner_args);
4049 int ntparms;
4050 int i;
4051 bool did_error_intro = false;
4052 struct template_parm_data tpd;
4053 struct template_parm_data tpd2;
4054
4055 gcc_assert (current_template_parms);
4056
4057 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4058 ntparms = TREE_VEC_LENGTH (inner_parms);
4059
4060 /* We check that each of the template parameters given in the
4061 partial specialization is used in the argument list to the
4062 specialization. For example:
4063
4064 template <class T> struct S;
4065 template <class T> struct S<T*>;
4066
4067 The second declaration is OK because `T*' uses the template
4068 parameter T, whereas
4069
4070 template <class T> struct S<int>;
4071
4072 is no good. Even trickier is:
4073
4074 template <class T>
4075 struct S1
4076 {
4077 template <class U>
4078 struct S2;
4079 template <class U>
4080 struct S2<T>;
4081 };
4082
4083 The S2<T> declaration is actually invalid; it is a
4084 full-specialization. Of course,
4085
4086 template <class U>
4087 struct S2<T (*)(U)>;
4088
4089 or some such would have been OK. */
4090 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4091 tpd.parms = XALLOCAVEC (int, ntparms);
4092 memset (tpd.parms, 0, sizeof (int) * ntparms);
4093
4094 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4095 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4096 for (i = 0; i < nargs; ++i)
4097 {
4098 tpd.current_arg = i;
4099 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4100 &mark_template_parm,
4101 &tpd,
4102 NULL,
4103 /*include_nondeduced_p=*/false);
4104 }
4105 for (i = 0; i < ntparms; ++i)
4106 if (tpd.parms[i] == 0)
4107 {
4108 /* One of the template parms was not used in the
4109 specialization. */
4110 if (!did_error_intro)
4111 {
4112 error ("template parameters not used in partial specialization:");
4113 did_error_intro = true;
4114 }
4115
4116 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4117 }
4118
4119 if (did_error_intro)
4120 return error_mark_node;
4121
4122 /* [temp.class.spec]
4123
4124 The argument list of the specialization shall not be identical to
4125 the implicit argument list of the primary template. */
4126 if (comp_template_args
4127 (inner_args,
4128 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4129 (maintmpl)))))
4130 error ("partial specialization %qT does not specialize any template arguments", type);
4131
4132 /* A partial specialization that replaces multiple parameters of the
4133 primary template with a pack expansion is less specialized for those
4134 parameters. */
4135 if (nargs < DECL_NTPARMS (maintmpl))
4136 {
4137 error ("partial specialization is not more specialized than the "
4138 "primary template because it replaces multiple parameters "
4139 "with a pack expansion");
4140 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4141 return decl;
4142 }
4143
4144 /* [temp.class.spec]
4145
4146 A partially specialized non-type argument expression shall not
4147 involve template parameters of the partial specialization except
4148 when the argument expression is a simple identifier.
4149
4150 The type of a template parameter corresponding to a specialized
4151 non-type argument shall not be dependent on a parameter of the
4152 specialization.
4153
4154 Also, we verify that pack expansions only occur at the
4155 end of the argument list. */
4156 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4157 tpd2.parms = 0;
4158 for (i = 0; i < nargs; ++i)
4159 {
4160 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4161 tree arg = TREE_VEC_ELT (inner_args, i);
4162 tree packed_args = NULL_TREE;
4163 int j, len = 1;
4164
4165 if (ARGUMENT_PACK_P (arg))
4166 {
4167 /* Extract the arguments from the argument pack. We'll be
4168 iterating over these in the following loop. */
4169 packed_args = ARGUMENT_PACK_ARGS (arg);
4170 len = TREE_VEC_LENGTH (packed_args);
4171 }
4172
4173 for (j = 0; j < len; j++)
4174 {
4175 if (packed_args)
4176 /* Get the Jth argument in the parameter pack. */
4177 arg = TREE_VEC_ELT (packed_args, j);
4178
4179 if (PACK_EXPANSION_P (arg))
4180 {
4181 /* Pack expansions must come at the end of the
4182 argument list. */
4183 if ((packed_args && j < len - 1)
4184 || (!packed_args && i < nargs - 1))
4185 {
4186 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4187 error ("parameter pack argument %qE must be at the "
4188 "end of the template argument list", arg);
4189 else
4190 error ("parameter pack argument %qT must be at the "
4191 "end of the template argument list", arg);
4192 }
4193 }
4194
4195 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4196 /* We only care about the pattern. */
4197 arg = PACK_EXPANSION_PATTERN (arg);
4198
4199 if (/* These first two lines are the `non-type' bit. */
4200 !TYPE_P (arg)
4201 && TREE_CODE (arg) != TEMPLATE_DECL
4202 /* This next line is the `argument expression is not just a
4203 simple identifier' condition and also the `specialized
4204 non-type argument' bit. */
4205 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4206 {
4207 if ((!packed_args && tpd.arg_uses_template_parms[i])
4208 || (packed_args && uses_template_parms (arg)))
4209 error ("template argument %qE involves template parameter(s)",
4210 arg);
4211 else
4212 {
4213 /* Look at the corresponding template parameter,
4214 marking which template parameters its type depends
4215 upon. */
4216 tree type = TREE_TYPE (parm);
4217
4218 if (!tpd2.parms)
4219 {
4220 /* We haven't yet initialized TPD2. Do so now. */
4221 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4222 /* The number of parameters here is the number in the
4223 main template, which, as checked in the assertion
4224 above, is NARGS. */
4225 tpd2.parms = XALLOCAVEC (int, nargs);
4226 tpd2.level =
4227 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4228 }
4229
4230 /* Mark the template parameters. But this time, we're
4231 looking for the template parameters of the main
4232 template, not in the specialization. */
4233 tpd2.current_arg = i;
4234 tpd2.arg_uses_template_parms[i] = 0;
4235 memset (tpd2.parms, 0, sizeof (int) * nargs);
4236 for_each_template_parm (type,
4237 &mark_template_parm,
4238 &tpd2,
4239 NULL,
4240 /*include_nondeduced_p=*/false);
4241
4242 if (tpd2.arg_uses_template_parms [i])
4243 {
4244 /* The type depended on some template parameters.
4245 If they are fully specialized in the
4246 specialization, that's OK. */
4247 int j;
4248 int count = 0;
4249 for (j = 0; j < nargs; ++j)
4250 if (tpd2.parms[j] != 0
4251 && tpd.arg_uses_template_parms [j])
4252 ++count;
4253 if (count != 0)
4254 error_n (input_location, count,
4255 "type %qT of template argument %qE depends "
4256 "on a template parameter",
4257 "type %qT of template argument %qE depends "
4258 "on template parameters",
4259 type,
4260 arg);
4261 }
4262 }
4263 }
4264 }
4265 }
4266
4267 /* We should only get here once. */
4268 gcc_assert (!COMPLETE_TYPE_P (type));
4269
4270 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4271 = tree_cons (specargs, inner_parms,
4272 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4273 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4274
4275 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4276 inst = TREE_CHAIN (inst))
4277 {
4278 tree inst_type = TREE_VALUE (inst);
4279 if (COMPLETE_TYPE_P (inst_type)
4280 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4281 {
4282 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4283 if (spec && TREE_TYPE (spec) == type)
4284 permerror (input_location,
4285 "partial specialization of %qT after instantiation "
4286 "of %qT", type, inst_type);
4287 }
4288 }
4289
4290 return decl;
4291 }
4292
4293 /* Check that a template declaration's use of default arguments and
4294 parameter packs is not invalid. Here, PARMS are the template
4295 parameters. IS_PRIMARY is true if DECL is the thing declared by
4296 a primary template. IS_PARTIAL is true if DECL is a partial
4297 specialization.
4298
4299 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4300 declaration (but not a definition); 1 indicates a declaration, 2
4301 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4302 emitted for extraneous default arguments.
4303
4304 Returns TRUE if there were no errors found, FALSE otherwise. */
4305
4306 bool
4307 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4308 bool is_partial, int is_friend_decl)
4309 {
4310 const char *msg;
4311 int last_level_to_check;
4312 tree parm_level;
4313 bool no_errors = true;
4314
4315 /* [temp.param]
4316
4317 A default template-argument shall not be specified in a
4318 function template declaration or a function template definition, nor
4319 in the template-parameter-list of the definition of a member of a
4320 class template. */
4321
4322 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4323 /* You can't have a function template declaration in a local
4324 scope, nor you can you define a member of a class template in a
4325 local scope. */
4326 return true;
4327
4328 if (current_class_type
4329 && !TYPE_BEING_DEFINED (current_class_type)
4330 && DECL_LANG_SPECIFIC (decl)
4331 && DECL_DECLARES_FUNCTION_P (decl)
4332 /* If this is either a friend defined in the scope of the class
4333 or a member function. */
4334 && (DECL_FUNCTION_MEMBER_P (decl)
4335 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4336 : DECL_FRIEND_CONTEXT (decl)
4337 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4338 : false)
4339 /* And, if it was a member function, it really was defined in
4340 the scope of the class. */
4341 && (!DECL_FUNCTION_MEMBER_P (decl)
4342 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4343 /* We already checked these parameters when the template was
4344 declared, so there's no need to do it again now. This function
4345 was defined in class scope, but we're processing it's body now
4346 that the class is complete. */
4347 return true;
4348
4349 /* Core issue 226 (C++0x only): the following only applies to class
4350 templates. */
4351 if (is_primary
4352 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4353 {
4354 /* [temp.param]
4355
4356 If a template-parameter has a default template-argument, all
4357 subsequent template-parameters shall have a default
4358 template-argument supplied. */
4359 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4360 {
4361 tree inner_parms = TREE_VALUE (parm_level);
4362 int ntparms = TREE_VEC_LENGTH (inner_parms);
4363 int seen_def_arg_p = 0;
4364 int i;
4365
4366 for (i = 0; i < ntparms; ++i)
4367 {
4368 tree parm = TREE_VEC_ELT (inner_parms, i);
4369
4370 if (parm == error_mark_node)
4371 continue;
4372
4373 if (TREE_PURPOSE (parm))
4374 seen_def_arg_p = 1;
4375 else if (seen_def_arg_p
4376 && !template_parameter_pack_p (TREE_VALUE (parm)))
4377 {
4378 error ("no default argument for %qD", TREE_VALUE (parm));
4379 /* For better subsequent error-recovery, we indicate that
4380 there should have been a default argument. */
4381 TREE_PURPOSE (parm) = error_mark_node;
4382 no_errors = false;
4383 }
4384 else if (!is_partial
4385 && !is_friend_decl
4386 /* Don't complain about an enclosing partial
4387 specialization. */
4388 && parm_level == parms
4389 && TREE_CODE (decl) == TYPE_DECL
4390 && i < ntparms - 1
4391 && template_parameter_pack_p (TREE_VALUE (parm)))
4392 {
4393 /* A primary class template can only have one
4394 parameter pack, at the end of the template
4395 parameter list. */
4396
4397 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4398 error ("parameter pack %qE must be at the end of the"
4399 " template parameter list", TREE_VALUE (parm));
4400 else
4401 error ("parameter pack %qT must be at the end of the"
4402 " template parameter list",
4403 TREE_TYPE (TREE_VALUE (parm)));
4404
4405 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4406 = error_mark_node;
4407 no_errors = false;
4408 }
4409 }
4410 }
4411 }
4412
4413 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4414 || is_partial
4415 || !is_primary
4416 || is_friend_decl)
4417 /* For an ordinary class template, default template arguments are
4418 allowed at the innermost level, e.g.:
4419 template <class T = int>
4420 struct S {};
4421 but, in a partial specialization, they're not allowed even
4422 there, as we have in [temp.class.spec]:
4423
4424 The template parameter list of a specialization shall not
4425 contain default template argument values.
4426
4427 So, for a partial specialization, or for a function template
4428 (in C++98/C++03), we look at all of them. */
4429 ;
4430 else
4431 /* But, for a primary class template that is not a partial
4432 specialization we look at all template parameters except the
4433 innermost ones. */
4434 parms = TREE_CHAIN (parms);
4435
4436 /* Figure out what error message to issue. */
4437 if (is_friend_decl == 2)
4438 msg = G_("default template arguments may not be used in function template "
4439 "friend re-declaration");
4440 else if (is_friend_decl)
4441 msg = G_("default template arguments may not be used in function template "
4442 "friend declarations");
4443 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4444 msg = G_("default template arguments may not be used in function templates "
4445 "without -std=c++11 or -std=gnu++11");
4446 else if (is_partial)
4447 msg = G_("default template arguments may not be used in "
4448 "partial specializations");
4449 else
4450 msg = G_("default argument for template parameter for class enclosing %qD");
4451
4452 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4453 /* If we're inside a class definition, there's no need to
4454 examine the parameters to the class itself. On the one
4455 hand, they will be checked when the class is defined, and,
4456 on the other, default arguments are valid in things like:
4457 template <class T = double>
4458 struct S { template <class U> void f(U); };
4459 Here the default argument for `S' has no bearing on the
4460 declaration of `f'. */
4461 last_level_to_check = template_class_depth (current_class_type) + 1;
4462 else
4463 /* Check everything. */
4464 last_level_to_check = 0;
4465
4466 for (parm_level = parms;
4467 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4468 parm_level = TREE_CHAIN (parm_level))
4469 {
4470 tree inner_parms = TREE_VALUE (parm_level);
4471 int i;
4472 int ntparms;
4473
4474 ntparms = TREE_VEC_LENGTH (inner_parms);
4475 for (i = 0; i < ntparms; ++i)
4476 {
4477 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4478 continue;
4479
4480 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4481 {
4482 if (msg)
4483 {
4484 no_errors = false;
4485 if (is_friend_decl == 2)
4486 return no_errors;
4487
4488 error (msg, decl);
4489 msg = 0;
4490 }
4491
4492 /* Clear out the default argument so that we are not
4493 confused later. */
4494 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4495 }
4496 }
4497
4498 /* At this point, if we're still interested in issuing messages,
4499 they must apply to classes surrounding the object declared. */
4500 if (msg)
4501 msg = G_("default argument for template parameter for class "
4502 "enclosing %qD");
4503 }
4504
4505 return no_errors;
4506 }
4507
4508 /* Worker for push_template_decl_real, called via
4509 for_each_template_parm. DATA is really an int, indicating the
4510 level of the parameters we are interested in. If T is a template
4511 parameter of that level, return nonzero. */
4512
4513 static int
4514 template_parm_this_level_p (tree t, void* data)
4515 {
4516 int this_level = *(int *)data;
4517 int level;
4518
4519 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4520 level = TEMPLATE_PARM_LEVEL (t);
4521 else
4522 level = TEMPLATE_TYPE_LEVEL (t);
4523 return level == this_level;
4524 }
4525
4526 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4527 parameters given by current_template_args, or reuses a
4528 previously existing one, if appropriate. Returns the DECL, or an
4529 equivalent one, if it is replaced via a call to duplicate_decls.
4530
4531 If IS_FRIEND is true, DECL is a friend declaration. */
4532
4533 tree
4534 push_template_decl_real (tree decl, bool is_friend)
4535 {
4536 tree tmpl;
4537 tree args;
4538 tree info;
4539 tree ctx;
4540 bool is_primary;
4541 bool is_partial;
4542 int new_template_p = 0;
4543 /* True if the template is a member template, in the sense of
4544 [temp.mem]. */
4545 bool member_template_p = false;
4546
4547 if (decl == error_mark_node || !current_template_parms)
4548 return error_mark_node;
4549
4550 /* See if this is a partial specialization. */
4551 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4552 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4553 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4554
4555 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4556 is_friend = true;
4557
4558 if (is_friend)
4559 /* For a friend, we want the context of the friend function, not
4560 the type of which it is a friend. */
4561 ctx = CP_DECL_CONTEXT (decl);
4562 else if (CP_DECL_CONTEXT (decl)
4563 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4564 /* In the case of a virtual function, we want the class in which
4565 it is defined. */
4566 ctx = CP_DECL_CONTEXT (decl);
4567 else
4568 /* Otherwise, if we're currently defining some class, the DECL
4569 is assumed to be a member of the class. */
4570 ctx = current_scope ();
4571
4572 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4573 ctx = NULL_TREE;
4574
4575 if (!DECL_CONTEXT (decl))
4576 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4577
4578 /* See if this is a primary template. */
4579 if (is_friend && ctx)
4580 /* A friend template that specifies a class context, i.e.
4581 template <typename T> friend void A<T>::f();
4582 is not primary. */
4583 is_primary = false;
4584 else
4585 is_primary = template_parm_scope_p ();
4586
4587 if (is_primary)
4588 {
4589 if (DECL_CLASS_SCOPE_P (decl))
4590 member_template_p = true;
4591 if (TREE_CODE (decl) == TYPE_DECL
4592 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4593 {
4594 error ("template class without a name");
4595 return error_mark_node;
4596 }
4597 else if (TREE_CODE (decl) == FUNCTION_DECL)
4598 {
4599 if (DECL_DESTRUCTOR_P (decl))
4600 {
4601 /* [temp.mem]
4602
4603 A destructor shall not be a member template. */
4604 error ("destructor %qD declared as member template", decl);
4605 return error_mark_node;
4606 }
4607 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4608 && (!prototype_p (TREE_TYPE (decl))
4609 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4610 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4611 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4612 == void_list_node)))
4613 {
4614 /* [basic.stc.dynamic.allocation]
4615
4616 An allocation function can be a function
4617 template. ... Template allocation functions shall
4618 have two or more parameters. */
4619 error ("invalid template declaration of %qD", decl);
4620 return error_mark_node;
4621 }
4622 }
4623 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4624 && CLASS_TYPE_P (TREE_TYPE (decl)))
4625 /* OK */;
4626 else if (TREE_CODE (decl) == TYPE_DECL
4627 && TYPE_DECL_ALIAS_P (decl))
4628 /* alias-declaration */
4629 gcc_assert (!DECL_ARTIFICIAL (decl));
4630 else
4631 {
4632 error ("template declaration of %q#D", decl);
4633 return error_mark_node;
4634 }
4635 }
4636
4637 /* Check to see that the rules regarding the use of default
4638 arguments are not being violated. */
4639 check_default_tmpl_args (decl, current_template_parms,
4640 is_primary, is_partial, /*is_friend_decl=*/0);
4641
4642 /* Ensure that there are no parameter packs in the type of this
4643 declaration that have not been expanded. */
4644 if (TREE_CODE (decl) == FUNCTION_DECL)
4645 {
4646 /* Check each of the arguments individually to see if there are
4647 any bare parameter packs. */
4648 tree type = TREE_TYPE (decl);
4649 tree arg = DECL_ARGUMENTS (decl);
4650 tree argtype = TYPE_ARG_TYPES (type);
4651
4652 while (arg && argtype)
4653 {
4654 if (!FUNCTION_PARAMETER_PACK_P (arg)
4655 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4656 {
4657 /* This is a PARM_DECL that contains unexpanded parameter
4658 packs. We have already complained about this in the
4659 check_for_bare_parameter_packs call, so just replace
4660 these types with ERROR_MARK_NODE. */
4661 TREE_TYPE (arg) = error_mark_node;
4662 TREE_VALUE (argtype) = error_mark_node;
4663 }
4664
4665 arg = DECL_CHAIN (arg);
4666 argtype = TREE_CHAIN (argtype);
4667 }
4668
4669 /* Check for bare parameter packs in the return type and the
4670 exception specifiers. */
4671 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4672 /* Errors were already issued, set return type to int
4673 as the frontend doesn't expect error_mark_node as
4674 the return type. */
4675 TREE_TYPE (type) = integer_type_node;
4676 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4677 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4678 }
4679 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4680 && TYPE_DECL_ALIAS_P (decl))
4681 ? DECL_ORIGINAL_TYPE (decl)
4682 : TREE_TYPE (decl)))
4683 {
4684 TREE_TYPE (decl) = error_mark_node;
4685 return error_mark_node;
4686 }
4687
4688 if (is_partial)
4689 return process_partial_specialization (decl);
4690
4691 args = current_template_args ();
4692
4693 if (!ctx
4694 || TREE_CODE (ctx) == FUNCTION_DECL
4695 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4696 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4697 {
4698 if (DECL_LANG_SPECIFIC (decl)
4699 && DECL_TEMPLATE_INFO (decl)
4700 && DECL_TI_TEMPLATE (decl))
4701 tmpl = DECL_TI_TEMPLATE (decl);
4702 /* If DECL is a TYPE_DECL for a class-template, then there won't
4703 be DECL_LANG_SPECIFIC. The information equivalent to
4704 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4705 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4706 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4707 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4708 {
4709 /* Since a template declaration already existed for this
4710 class-type, we must be redeclaring it here. Make sure
4711 that the redeclaration is valid. */
4712 redeclare_class_template (TREE_TYPE (decl),
4713 current_template_parms);
4714 /* We don't need to create a new TEMPLATE_DECL; just use the
4715 one we already had. */
4716 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4717 }
4718 else
4719 {
4720 tmpl = build_template_decl (decl, current_template_parms,
4721 member_template_p);
4722 new_template_p = 1;
4723
4724 if (DECL_LANG_SPECIFIC (decl)
4725 && DECL_TEMPLATE_SPECIALIZATION (decl))
4726 {
4727 /* A specialization of a member template of a template
4728 class. */
4729 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4730 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4731 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4732 }
4733 }
4734 }
4735 else
4736 {
4737 tree a, t, current, parms;
4738 int i;
4739 tree tinfo = get_template_info (decl);
4740
4741 if (!tinfo)
4742 {
4743 error ("template definition of non-template %q#D", decl);
4744 return error_mark_node;
4745 }
4746
4747 tmpl = TI_TEMPLATE (tinfo);
4748
4749 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4750 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4751 && DECL_TEMPLATE_SPECIALIZATION (decl)
4752 && DECL_MEMBER_TEMPLATE_P (tmpl))
4753 {
4754 tree new_tmpl;
4755
4756 /* The declaration is a specialization of a member
4757 template, declared outside the class. Therefore, the
4758 innermost template arguments will be NULL, so we
4759 replace them with the arguments determined by the
4760 earlier call to check_explicit_specialization. */
4761 args = DECL_TI_ARGS (decl);
4762
4763 new_tmpl
4764 = build_template_decl (decl, current_template_parms,
4765 member_template_p);
4766 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4767 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4768 DECL_TI_TEMPLATE (decl) = new_tmpl;
4769 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4770 DECL_TEMPLATE_INFO (new_tmpl)
4771 = build_template_info (tmpl, args);
4772
4773 register_specialization (new_tmpl,
4774 most_general_template (tmpl),
4775 args,
4776 is_friend, 0);
4777 return decl;
4778 }
4779
4780 /* Make sure the template headers we got make sense. */
4781
4782 parms = DECL_TEMPLATE_PARMS (tmpl);
4783 i = TMPL_PARMS_DEPTH (parms);
4784 if (TMPL_ARGS_DEPTH (args) != i)
4785 {
4786 error ("expected %d levels of template parms for %q#D, got %d",
4787 i, decl, TMPL_ARGS_DEPTH (args));
4788 }
4789 else
4790 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4791 {
4792 a = TMPL_ARGS_LEVEL (args, i);
4793 t = INNERMOST_TEMPLATE_PARMS (parms);
4794
4795 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4796 {
4797 if (current == decl)
4798 error ("got %d template parameters for %q#D",
4799 TREE_VEC_LENGTH (a), decl);
4800 else
4801 error ("got %d template parameters for %q#T",
4802 TREE_VEC_LENGTH (a), current);
4803 error (" but %d required", TREE_VEC_LENGTH (t));
4804 return error_mark_node;
4805 }
4806
4807 if (current == decl)
4808 current = ctx;
4809 else if (current == NULL_TREE)
4810 /* Can happen in erroneous input. */
4811 break;
4812 else
4813 current = (TYPE_P (current)
4814 ? TYPE_CONTEXT (current)
4815 : DECL_CONTEXT (current));
4816 }
4817
4818 /* Check that the parms are used in the appropriate qualifying scopes
4819 in the declarator. */
4820 if (!comp_template_args
4821 (TI_ARGS (tinfo),
4822 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4823 {
4824 error ("\
4825 template arguments to %qD do not match original template %qD",
4826 decl, DECL_TEMPLATE_RESULT (tmpl));
4827 if (!uses_template_parms (TI_ARGS (tinfo)))
4828 inform (input_location, "use template<> for an explicit specialization");
4829 /* Avoid crash in import_export_decl. */
4830 DECL_INTERFACE_KNOWN (decl) = 1;
4831 return error_mark_node;
4832 }
4833 }
4834
4835 DECL_TEMPLATE_RESULT (tmpl) = decl;
4836 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4837
4838 /* Push template declarations for global functions and types. Note
4839 that we do not try to push a global template friend declared in a
4840 template class; such a thing may well depend on the template
4841 parameters of the class. */
4842 if (new_template_p && !ctx
4843 && !(is_friend && template_class_depth (current_class_type) > 0))
4844 {
4845 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4846 if (tmpl == error_mark_node)
4847 return error_mark_node;
4848
4849 /* Hide template friend classes that haven't been declared yet. */
4850 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4851 {
4852 DECL_ANTICIPATED (tmpl) = 1;
4853 DECL_FRIEND_P (tmpl) = 1;
4854 }
4855 }
4856
4857 if (is_primary)
4858 {
4859 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4860 int i;
4861
4862 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4863 if (DECL_CONV_FN_P (tmpl))
4864 {
4865 int depth = TMPL_PARMS_DEPTH (parms);
4866
4867 /* It is a conversion operator. See if the type converted to
4868 depends on innermost template operands. */
4869
4870 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4871 depth))
4872 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4873 }
4874
4875 /* Give template template parms a DECL_CONTEXT of the template
4876 for which they are a parameter. */
4877 parms = INNERMOST_TEMPLATE_PARMS (parms);
4878 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4879 {
4880 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4881 if (TREE_CODE (parm) == TEMPLATE_DECL)
4882 DECL_CONTEXT (parm) = tmpl;
4883 }
4884 }
4885
4886 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4887 back to its most general template. If TMPL is a specialization,
4888 ARGS may only have the innermost set of arguments. Add the missing
4889 argument levels if necessary. */
4890 if (DECL_TEMPLATE_INFO (tmpl))
4891 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4892
4893 info = build_template_info (tmpl, args);
4894
4895 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4896 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4897 else
4898 {
4899 if (is_primary && !DECL_LANG_SPECIFIC (decl))
4900 retrofit_lang_decl (decl);
4901 if (DECL_LANG_SPECIFIC (decl))
4902 DECL_TEMPLATE_INFO (decl) = info;
4903 }
4904
4905 return DECL_TEMPLATE_RESULT (tmpl);
4906 }
4907
4908 tree
4909 push_template_decl (tree decl)
4910 {
4911 return push_template_decl_real (decl, false);
4912 }
4913
4914 /* FN is an inheriting constructor that inherits from the constructor
4915 template INHERITED; turn FN into a constructor template with a matching
4916 template header. */
4917
4918 tree
4919 add_inherited_template_parms (tree fn, tree inherited)
4920 {
4921 tree inner_parms
4922 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
4923 inner_parms = copy_node (inner_parms);
4924 tree parms
4925 = tree_cons (size_int (processing_template_decl + 1),
4926 inner_parms, current_template_parms);
4927 tree tmpl = build_template_decl (fn, parms, /*member*/true);
4928 tree args = template_parms_to_args (parms);
4929 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
4930 TREE_TYPE (tmpl) = TREE_TYPE (fn);
4931 DECL_TEMPLATE_RESULT (tmpl) = fn;
4932 DECL_ARTIFICIAL (tmpl) = true;
4933 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4934 return tmpl;
4935 }
4936
4937 /* Called when a class template TYPE is redeclared with the indicated
4938 template PARMS, e.g.:
4939
4940 template <class T> struct S;
4941 template <class T> struct S {}; */
4942
4943 bool
4944 redeclare_class_template (tree type, tree parms)
4945 {
4946 tree tmpl;
4947 tree tmpl_parms;
4948 int i;
4949
4950 if (!TYPE_TEMPLATE_INFO (type))
4951 {
4952 error ("%qT is not a template type", type);
4953 return false;
4954 }
4955
4956 tmpl = TYPE_TI_TEMPLATE (type);
4957 if (!PRIMARY_TEMPLATE_P (tmpl))
4958 /* The type is nested in some template class. Nothing to worry
4959 about here; there are no new template parameters for the nested
4960 type. */
4961 return true;
4962
4963 if (!parms)
4964 {
4965 error ("template specifiers not specified in declaration of %qD",
4966 tmpl);
4967 return false;
4968 }
4969
4970 parms = INNERMOST_TEMPLATE_PARMS (parms);
4971 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4972
4973 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4974 {
4975 error_n (input_location, TREE_VEC_LENGTH (parms),
4976 "redeclared with %d template parameter",
4977 "redeclared with %d template parameters",
4978 TREE_VEC_LENGTH (parms));
4979 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4980 "previous declaration %q+D used %d template parameter",
4981 "previous declaration %q+D used %d template parameters",
4982 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4983 return false;
4984 }
4985
4986 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4987 {
4988 tree tmpl_parm;
4989 tree parm;
4990 tree tmpl_default;
4991 tree parm_default;
4992
4993 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4994 || TREE_VEC_ELT (parms, i) == error_mark_node)
4995 continue;
4996
4997 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4998 if (tmpl_parm == error_mark_node)
4999 return false;
5000
5001 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5002 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5003 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5004
5005 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5006 TEMPLATE_DECL. */
5007 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5008 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5009 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5010 || (TREE_CODE (tmpl_parm) != PARM_DECL
5011 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5012 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5013 || (TREE_CODE (tmpl_parm) == PARM_DECL
5014 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5015 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5016 {
5017 error ("template parameter %q+#D", tmpl_parm);
5018 error ("redeclared here as %q#D", parm);
5019 return false;
5020 }
5021
5022 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5023 {
5024 /* We have in [temp.param]:
5025
5026 A template-parameter may not be given default arguments
5027 by two different declarations in the same scope. */
5028 error_at (input_location, "redefinition of default argument for %q#D", parm);
5029 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5030 "original definition appeared here");
5031 return false;
5032 }
5033
5034 if (parm_default != NULL_TREE)
5035 /* Update the previous template parameters (which are the ones
5036 that will really count) with the new default value. */
5037 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5038 else if (tmpl_default != NULL_TREE)
5039 /* Update the new parameters, too; they'll be used as the
5040 parameters for any members. */
5041 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5042 }
5043
5044 return true;
5045 }
5046
5047 /* Simplify EXPR if it is a non-dependent expression. Returns the
5048 (possibly simplified) expression. */
5049
5050 tree
5051 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5052 {
5053 if (expr == NULL_TREE)
5054 return NULL_TREE;
5055
5056 /* If we're in a template, but EXPR isn't value dependent, simplify
5057 it. We're supposed to treat:
5058
5059 template <typename T> void f(T[1 + 1]);
5060 template <typename T> void f(T[2]);
5061
5062 as two declarations of the same function, for example. */
5063 if (processing_template_decl
5064 && !type_dependent_expression_p (expr)
5065 && potential_constant_expression (expr)
5066 && !value_dependent_expression_p (expr))
5067 {
5068 HOST_WIDE_INT saved_processing_template_decl;
5069
5070 saved_processing_template_decl = processing_template_decl;
5071 processing_template_decl = 0;
5072 expr = tsubst_copy_and_build (expr,
5073 /*args=*/NULL_TREE,
5074 complain,
5075 /*in_decl=*/NULL_TREE,
5076 /*function_p=*/false,
5077 /*integral_constant_expression_p=*/true);
5078 processing_template_decl = saved_processing_template_decl;
5079 }
5080 return expr;
5081 }
5082
5083 tree
5084 fold_non_dependent_expr (tree expr)
5085 {
5086 return fold_non_dependent_expr_sfinae (expr, tf_error);
5087 }
5088
5089 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5090 template declaration, or a TYPE_DECL for an alias declaration. */
5091
5092 bool
5093 alias_type_or_template_p (tree t)
5094 {
5095 if (t == NULL_TREE)
5096 return false;
5097 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5098 || (TYPE_P (t)
5099 && TYPE_NAME (t)
5100 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5101 || DECL_ALIAS_TEMPLATE_P (t));
5102 }
5103
5104 /* Return TRUE iff is a specialization of an alias template. */
5105
5106 bool
5107 alias_template_specialization_p (const_tree t)
5108 {
5109 if (t == NULL_TREE)
5110 return false;
5111
5112 return (TYPE_P (t)
5113 && TYPE_TEMPLATE_INFO (t)
5114 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
5115 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5116 }
5117
5118 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5119 must be a function or a pointer-to-function type, as specified
5120 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5121 and check that the resulting function has external linkage. */
5122
5123 static tree
5124 convert_nontype_argument_function (tree type, tree expr)
5125 {
5126 tree fns = expr;
5127 tree fn, fn_no_ptr;
5128 linkage_kind linkage;
5129
5130 fn = instantiate_type (type, fns, tf_none);
5131 if (fn == error_mark_node)
5132 return error_mark_node;
5133
5134 fn_no_ptr = fn;
5135 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5136 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5137 if (BASELINK_P (fn_no_ptr))
5138 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5139
5140 /* [temp.arg.nontype]/1
5141
5142 A template-argument for a non-type, non-template template-parameter
5143 shall be one of:
5144 [...]
5145 -- the address of an object or function with external [C++11: or
5146 internal] linkage. */
5147 linkage = decl_linkage (fn_no_ptr);
5148 if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
5149 {
5150 if (cxx_dialect >= cxx0x)
5151 error ("%qE is not a valid template argument for type %qT "
5152 "because %qD has no linkage",
5153 expr, type, fn_no_ptr);
5154 else
5155 error ("%qE is not a valid template argument for type %qT "
5156 "because %qD does not have external linkage",
5157 expr, type, fn_no_ptr);
5158 return NULL_TREE;
5159 }
5160
5161 return fn;
5162 }
5163
5164 /* Subroutine of convert_nontype_argument.
5165 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5166 Emit an error otherwise. */
5167
5168 static bool
5169 check_valid_ptrmem_cst_expr (tree type, tree expr,
5170 tsubst_flags_t complain)
5171 {
5172 STRIP_NOPS (expr);
5173 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5174 return true;
5175 if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr))
5176 return true;
5177 if (complain & tf_error)
5178 {
5179 error ("%qE is not a valid template argument for type %qT",
5180 expr, type);
5181 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5182 }
5183 return false;
5184 }
5185
5186 /* Returns TRUE iff the address of OP is value-dependent.
5187
5188 14.6.2.4 [temp.dep.temp]:
5189 A non-integral non-type template-argument is dependent if its type is
5190 dependent or it has either of the following forms
5191 qualified-id
5192 & qualified-id
5193 and contains a nested-name-specifier which specifies a class-name that
5194 names a dependent type.
5195
5196 We generalize this to just say that the address of a member of a
5197 dependent class is value-dependent; the above doesn't cover the
5198 address of a static data member named with an unqualified-id. */
5199
5200 static bool
5201 has_value_dependent_address (tree op)
5202 {
5203 /* We could use get_inner_reference here, but there's no need;
5204 this is only relevant for template non-type arguments, which
5205 can only be expressed as &id-expression. */
5206 if (DECL_P (op))
5207 {
5208 tree ctx = CP_DECL_CONTEXT (op);
5209 if (TYPE_P (ctx) && dependent_type_p (ctx))
5210 return true;
5211 }
5212
5213 return false;
5214 }
5215
5216 /* The next set of functions are used for providing helpful explanatory
5217 diagnostics for failed overload resolution. Their messages should be
5218 indented by two spaces for consistency with the messages in
5219 call.c */
5220
5221 static int
5222 unify_success (bool /*explain_p*/)
5223 {
5224 return 0;
5225 }
5226
5227 static int
5228 unify_parameter_deduction_failure (bool explain_p, tree parm)
5229 {
5230 if (explain_p)
5231 inform (input_location,
5232 " couldn't deduce template parameter %qD", parm);
5233 return 1;
5234 }
5235
5236 static int
5237 unify_invalid (bool /*explain_p*/)
5238 {
5239 return 1;
5240 }
5241
5242 static int
5243 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5244 {
5245 if (explain_p)
5246 inform (input_location,
5247 " types %qT and %qT have incompatible cv-qualifiers",
5248 parm, arg);
5249 return 1;
5250 }
5251
5252 static int
5253 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5254 {
5255 if (explain_p)
5256 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5257 return 1;
5258 }
5259
5260 static int
5261 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5262 {
5263 if (explain_p)
5264 inform (input_location,
5265 " template parameter %qD is not a parameter pack, but "
5266 "argument %qD is",
5267 parm, arg);
5268 return 1;
5269 }
5270
5271 static int
5272 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5273 {
5274 if (explain_p)
5275 inform (input_location,
5276 " template argument %qE does not match "
5277 "pointer-to-member constant %qE",
5278 arg, parm);
5279 return 1;
5280 }
5281
5282 static int
5283 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5284 {
5285 if (explain_p)
5286 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5287 return 1;
5288 }
5289
5290 static int
5291 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5292 {
5293 if (explain_p)
5294 inform (input_location,
5295 " inconsistent parameter pack deduction with %qT and %qT",
5296 old_arg, new_arg);
5297 return 1;
5298 }
5299
5300 static int
5301 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5302 {
5303 if (explain_p)
5304 {
5305 if (TYPE_P (parm))
5306 inform (input_location,
5307 " deduced conflicting types for parameter %qT (%qT and %qT)",
5308 parm, first, second);
5309 else
5310 inform (input_location,
5311 " deduced conflicting values for non-type parameter "
5312 "%qE (%qE and %qE)", parm, first, second);
5313 }
5314 return 1;
5315 }
5316
5317 static int
5318 unify_vla_arg (bool explain_p, tree arg)
5319 {
5320 if (explain_p)
5321 inform (input_location,
5322 " variable-sized array type %qT is not "
5323 "a valid template argument",
5324 arg);
5325 return 1;
5326 }
5327
5328 static int
5329 unify_method_type_error (bool explain_p, tree arg)
5330 {
5331 if (explain_p)
5332 inform (input_location,
5333 " member function type %qT is not a valid template argument",
5334 arg);
5335 return 1;
5336 }
5337
5338 static int
5339 unify_arity (bool explain_p, int have, int wanted)
5340 {
5341 if (explain_p)
5342 inform_n (input_location, wanted,
5343 " candidate expects %d argument, %d provided",
5344 " candidate expects %d arguments, %d provided",
5345 wanted, have);
5346 return 1;
5347 }
5348
5349 static int
5350 unify_too_many_arguments (bool explain_p, int have, int wanted)
5351 {
5352 return unify_arity (explain_p, have, wanted);
5353 }
5354
5355 static int
5356 unify_too_few_arguments (bool explain_p, int have, int wanted)
5357 {
5358 return unify_arity (explain_p, have, wanted);
5359 }
5360
5361 static int
5362 unify_arg_conversion (bool explain_p, tree to_type,
5363 tree from_type, tree arg)
5364 {
5365 if (explain_p)
5366 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5367 arg, from_type, to_type);
5368 return 1;
5369 }
5370
5371 static int
5372 unify_no_common_base (bool explain_p, enum template_base_result r,
5373 tree parm, tree arg)
5374 {
5375 if (explain_p)
5376 switch (r)
5377 {
5378 case tbr_ambiguous_baseclass:
5379 inform (input_location, " %qT is an ambiguous base class of %qT",
5380 arg, parm);
5381 break;
5382 default:
5383 inform (input_location, " %qT is not derived from %qT", arg, parm);
5384 break;
5385 }
5386 return 1;
5387 }
5388
5389 static int
5390 unify_inconsistent_template_template_parameters (bool explain_p)
5391 {
5392 if (explain_p)
5393 inform (input_location,
5394 " template parameters of a template template argument are "
5395 "inconsistent with other deduced template arguments");
5396 return 1;
5397 }
5398
5399 static int
5400 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5401 {
5402 if (explain_p)
5403 inform (input_location,
5404 " can't deduce a template for %qT from non-template type %qT",
5405 parm, arg);
5406 return 1;
5407 }
5408
5409 static int
5410 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5411 {
5412 if (explain_p)
5413 inform (input_location,
5414 " template argument %qE does not match %qD", arg, parm);
5415 return 1;
5416 }
5417
5418 static int
5419 unify_overload_resolution_failure (bool explain_p, tree arg)
5420 {
5421 if (explain_p)
5422 inform (input_location,
5423 " could not resolve address from overloaded function %qE",
5424 arg);
5425 return 1;
5426 }
5427
5428 /* Attempt to convert the non-type template parameter EXPR to the
5429 indicated TYPE. If the conversion is successful, return the
5430 converted value. If the conversion is unsuccessful, return
5431 NULL_TREE if we issued an error message, or error_mark_node if we
5432 did not. We issue error messages for out-and-out bad template
5433 parameters, but not simply because the conversion failed, since we
5434 might be just trying to do argument deduction. Both TYPE and EXPR
5435 must be non-dependent.
5436
5437 The conversion follows the special rules described in
5438 [temp.arg.nontype], and it is much more strict than an implicit
5439 conversion.
5440
5441 This function is called twice for each template argument (see
5442 lookup_template_class for a more accurate description of this
5443 problem). This means that we need to handle expressions which
5444 are not valid in a C++ source, but can be created from the
5445 first call (for instance, casts to perform conversions). These
5446 hacks can go away after we fix the double coercion problem. */
5447
5448 static tree
5449 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5450 {
5451 tree expr_type;
5452
5453 /* Detect immediately string literals as invalid non-type argument.
5454 This special-case is not needed for correctness (we would easily
5455 catch this later), but only to provide better diagnostic for this
5456 common user mistake. As suggested by DR 100, we do not mention
5457 linkage issues in the diagnostic as this is not the point. */
5458 /* FIXME we're making this OK. */
5459 if (TREE_CODE (expr) == STRING_CST)
5460 {
5461 if (complain & tf_error)
5462 error ("%qE is not a valid template argument for type %qT "
5463 "because string literals can never be used in this context",
5464 expr, type);
5465 return NULL_TREE;
5466 }
5467
5468 /* Add the ADDR_EXPR now for the benefit of
5469 value_dependent_expression_p. */
5470 if (TYPE_PTROBV_P (type)
5471 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5472 {
5473 expr = decay_conversion (expr, complain);
5474 if (expr == error_mark_node)
5475 return error_mark_node;
5476 }
5477
5478 /* If we are in a template, EXPR may be non-dependent, but still
5479 have a syntactic, rather than semantic, form. For example, EXPR
5480 might be a SCOPE_REF, rather than the VAR_DECL to which the
5481 SCOPE_REF refers. Preserving the qualifying scope is necessary
5482 so that access checking can be performed when the template is
5483 instantiated -- but here we need the resolved form so that we can
5484 convert the argument. */
5485 if (TYPE_REF_OBJ_P (type)
5486 && has_value_dependent_address (expr))
5487 /* If we want the address and it's value-dependent, don't fold. */;
5488 else if (!type_unknown_p (expr))
5489 expr = fold_non_dependent_expr_sfinae (expr, complain);
5490 if (error_operand_p (expr))
5491 return error_mark_node;
5492 expr_type = TREE_TYPE (expr);
5493 if (TREE_CODE (type) == REFERENCE_TYPE)
5494 expr = mark_lvalue_use (expr);
5495 else
5496 expr = mark_rvalue_use (expr);
5497
5498 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5499 to a non-type argument of "nullptr". */
5500 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
5501 expr = convert (type, expr);
5502
5503 /* In C++11, integral or enumeration non-type template arguments can be
5504 arbitrary constant expressions. Pointer and pointer to
5505 member arguments can be general constant expressions that evaluate
5506 to a null value, but otherwise still need to be of a specific form. */
5507 if (cxx_dialect >= cxx0x)
5508 {
5509 if (TREE_CODE (expr) == PTRMEM_CST)
5510 /* A PTRMEM_CST is already constant, and a valid template
5511 argument for a parameter of pointer to member type, we just want
5512 to leave it in that form rather than lower it to a
5513 CONSTRUCTOR. */;
5514 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5515 expr = maybe_constant_value (expr);
5516 else if (TYPE_PTR_OR_PTRMEM_P (type))
5517 {
5518 tree folded = maybe_constant_value (expr);
5519 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5520 : null_member_pointer_value_p (folded))
5521 expr = folded;
5522 }
5523 }
5524
5525 /* HACK: Due to double coercion, we can get a
5526 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5527 which is the tree that we built on the first call (see
5528 below when coercing to reference to object or to reference to
5529 function). We just strip everything and get to the arg.
5530 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5531 for examples. */
5532 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5533 {
5534 tree probe_type, probe = expr;
5535 if (REFERENCE_REF_P (probe))
5536 probe = TREE_OPERAND (probe, 0);
5537 probe_type = TREE_TYPE (probe);
5538 if (TREE_CODE (probe) == NOP_EXPR)
5539 {
5540 /* ??? Maybe we could use convert_from_reference here, but we
5541 would need to relax its constraints because the NOP_EXPR
5542 could actually change the type to something more cv-qualified,
5543 and this is not folded by convert_from_reference. */
5544 tree addr = TREE_OPERAND (probe, 0);
5545 gcc_assert (TREE_CODE (probe_type) == REFERENCE_TYPE);
5546 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5547 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5548 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5549 (TREE_TYPE (probe_type),
5550 TREE_TYPE (TREE_TYPE (addr))));
5551
5552 expr = TREE_OPERAND (addr, 0);
5553 expr_type = TREE_TYPE (expr);
5554 }
5555 }
5556
5557 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5558 parameter is a pointer to object, through decay and
5559 qualification conversion. Let's strip everything. */
5560 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5561 {
5562 STRIP_NOPS (expr);
5563 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5564 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5565 /* Skip the ADDR_EXPR only if it is part of the decay for
5566 an array. Otherwise, it is part of the original argument
5567 in the source code. */
5568 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5569 expr = TREE_OPERAND (expr, 0);
5570 expr_type = TREE_TYPE (expr);
5571 }
5572
5573 /* [temp.arg.nontype]/5, bullet 1
5574
5575 For a non-type template-parameter of integral or enumeration type,
5576 integral promotions (_conv.prom_) and integral conversions
5577 (_conv.integral_) are applied. */
5578 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5579 {
5580 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5581 t = maybe_constant_value (t);
5582 if (t != error_mark_node)
5583 expr = t;
5584
5585 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5586 return error_mark_node;
5587
5588 /* Notice that there are constant expressions like '4 % 0' which
5589 do not fold into integer constants. */
5590 if (TREE_CODE (expr) != INTEGER_CST)
5591 {
5592 if (complain & tf_error)
5593 {
5594 int errs = errorcount, warns = warningcount;
5595 if (processing_template_decl
5596 && !require_potential_constant_expression (expr))
5597 return NULL_TREE;
5598 expr = cxx_constant_value (expr);
5599 if (errorcount > errs || warningcount > warns)
5600 inform (EXPR_LOC_OR_HERE (expr),
5601 "in template argument for type %qT ", type);
5602 if (expr == error_mark_node)
5603 return NULL_TREE;
5604 /* else cxx_constant_value complained but gave us
5605 a real constant, so go ahead. */
5606 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5607 }
5608 else
5609 return NULL_TREE;
5610 }
5611 }
5612 /* [temp.arg.nontype]/5, bullet 2
5613
5614 For a non-type template-parameter of type pointer to object,
5615 qualification conversions (_conv.qual_) and the array-to-pointer
5616 conversion (_conv.array_) are applied. */
5617 else if (TYPE_PTROBV_P (type))
5618 {
5619 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5620
5621 A template-argument for a non-type, non-template template-parameter
5622 shall be one of: [...]
5623
5624 -- the name of a non-type template-parameter;
5625 -- the address of an object or function with external linkage, [...]
5626 expressed as "& id-expression" where the & is optional if the name
5627 refers to a function or array, or if the corresponding
5628 template-parameter is a reference.
5629
5630 Here, we do not care about functions, as they are invalid anyway
5631 for a parameter of type pointer-to-object. */
5632
5633 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5634 /* Non-type template parameters are OK. */
5635 ;
5636 else if (cxx_dialect >= cxx0x && integer_zerop (expr))
5637 /* Null pointer values are OK in C++11. */;
5638 else if (TREE_CODE (expr) != ADDR_EXPR
5639 && TREE_CODE (expr_type) != ARRAY_TYPE)
5640 {
5641 if (TREE_CODE (expr) == VAR_DECL)
5642 {
5643 error ("%qD is not a valid template argument "
5644 "because %qD is a variable, not the address of "
5645 "a variable",
5646 expr, expr);
5647 return NULL_TREE;
5648 }
5649 /* Other values, like integer constants, might be valid
5650 non-type arguments of some other type. */
5651 return error_mark_node;
5652 }
5653 else
5654 {
5655 tree decl;
5656
5657 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5658 ? TREE_OPERAND (expr, 0) : expr);
5659 if (TREE_CODE (decl) != VAR_DECL)
5660 {
5661 error ("%qE is not a valid template argument of type %qT "
5662 "because %qE is not a variable",
5663 expr, type, decl);
5664 return NULL_TREE;
5665 }
5666 else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
5667 {
5668 error ("%qE is not a valid template argument of type %qT "
5669 "because %qD does not have external linkage",
5670 expr, type, decl);
5671 return NULL_TREE;
5672 }
5673 else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
5674 {
5675 error ("%qE is not a valid template argument of type %qT "
5676 "because %qD has no linkage",
5677 expr, type, decl);
5678 return NULL_TREE;
5679 }
5680 }
5681
5682 expr = decay_conversion (expr, complain);
5683 if (expr == error_mark_node)
5684 return error_mark_node;
5685
5686 expr = perform_qualification_conversions (type, expr);
5687 if (expr == error_mark_node)
5688 return error_mark_node;
5689 }
5690 /* [temp.arg.nontype]/5, bullet 3
5691
5692 For a non-type template-parameter of type reference to object, no
5693 conversions apply. The type referred to by the reference may be more
5694 cv-qualified than the (otherwise identical) type of the
5695 template-argument. The template-parameter is bound directly to the
5696 template-argument, which must be an lvalue. */
5697 else if (TYPE_REF_OBJ_P (type))
5698 {
5699 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5700 expr_type))
5701 return error_mark_node;
5702
5703 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5704 {
5705 error ("%qE is not a valid template argument for type %qT "
5706 "because of conflicts in cv-qualification", expr, type);
5707 return NULL_TREE;
5708 }
5709
5710 if (!real_lvalue_p (expr))
5711 {
5712 error ("%qE is not a valid template argument for type %qT "
5713 "because it is not an lvalue", expr, type);
5714 return NULL_TREE;
5715 }
5716
5717 /* [temp.arg.nontype]/1
5718
5719 A template-argument for a non-type, non-template template-parameter
5720 shall be one of: [...]
5721
5722 -- the address of an object or function with external linkage. */
5723 if (TREE_CODE (expr) == INDIRECT_REF
5724 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5725 {
5726 expr = TREE_OPERAND (expr, 0);
5727 if (DECL_P (expr))
5728 {
5729 error ("%q#D is not a valid template argument for type %qT "
5730 "because a reference variable does not have a constant "
5731 "address", expr, type);
5732 return NULL_TREE;
5733 }
5734 }
5735
5736 if (!DECL_P (expr))
5737 {
5738 error ("%qE is not a valid template argument for type %qT "
5739 "because it is not an object with external linkage",
5740 expr, type);
5741 return NULL_TREE;
5742 }
5743
5744 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5745 {
5746 error ("%qE is not a valid template argument for type %qT "
5747 "because object %qD has not external linkage",
5748 expr, type, expr);
5749 return NULL_TREE;
5750 }
5751
5752 expr = build_nop (type, build_address (expr));
5753 }
5754 /* [temp.arg.nontype]/5, bullet 4
5755
5756 For a non-type template-parameter of type pointer to function, only
5757 the function-to-pointer conversion (_conv.func_) is applied. If the
5758 template-argument represents a set of overloaded functions (or a
5759 pointer to such), the matching function is selected from the set
5760 (_over.over_). */
5761 else if (TYPE_PTRFN_P (type))
5762 {
5763 /* If the argument is a template-id, we might not have enough
5764 context information to decay the pointer. */
5765 if (!type_unknown_p (expr_type))
5766 {
5767 expr = decay_conversion (expr, complain);
5768 if (expr == error_mark_node)
5769 return error_mark_node;
5770 }
5771
5772 if (cxx_dialect >= cxx0x && integer_zerop (expr))
5773 /* Null pointer values are OK in C++11. */
5774 return perform_qualification_conversions (type, expr);
5775
5776 expr = convert_nontype_argument_function (type, expr);
5777 if (!expr || expr == error_mark_node)
5778 return expr;
5779
5780 if (TREE_CODE (expr) != ADDR_EXPR)
5781 {
5782 error ("%qE is not a valid template argument for type %qT", expr, type);
5783 error ("it must be the address of a function with external linkage");
5784 return NULL_TREE;
5785 }
5786 }
5787 /* [temp.arg.nontype]/5, bullet 5
5788
5789 For a non-type template-parameter of type reference to function, no
5790 conversions apply. If the template-argument represents a set of
5791 overloaded functions, the matching function is selected from the set
5792 (_over.over_). */
5793 else if (TYPE_REFFN_P (type))
5794 {
5795 if (TREE_CODE (expr) == ADDR_EXPR)
5796 {
5797 error ("%qE is not a valid template argument for type %qT "
5798 "because it is a pointer", expr, type);
5799 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5800 return NULL_TREE;
5801 }
5802
5803 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5804 if (!expr || expr == error_mark_node)
5805 return expr;
5806
5807 expr = build_nop (type, build_address (expr));
5808 }
5809 /* [temp.arg.nontype]/5, bullet 6
5810
5811 For a non-type template-parameter of type pointer to member function,
5812 no conversions apply. If the template-argument represents a set of
5813 overloaded member functions, the matching member function is selected
5814 from the set (_over.over_). */
5815 else if (TYPE_PTRMEMFUNC_P (type))
5816 {
5817 expr = instantiate_type (type, expr, tf_none);
5818 if (expr == error_mark_node)
5819 return error_mark_node;
5820
5821 /* [temp.arg.nontype] bullet 1 says the pointer to member
5822 expression must be a pointer-to-member constant. */
5823 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5824 return error_mark_node;
5825
5826 /* There is no way to disable standard conversions in
5827 resolve_address_of_overloaded_function (called by
5828 instantiate_type). It is possible that the call succeeded by
5829 converting &B::I to &D::I (where B is a base of D), so we need
5830 to reject this conversion here.
5831
5832 Actually, even if there was a way to disable standard conversions,
5833 it would still be better to reject them here so that we can
5834 provide a superior diagnostic. */
5835 if (!same_type_p (TREE_TYPE (expr), type))
5836 {
5837 error ("%qE is not a valid template argument for type %qT "
5838 "because it is of type %qT", expr, type,
5839 TREE_TYPE (expr));
5840 /* If we are just one standard conversion off, explain. */
5841 if (can_convert (type, TREE_TYPE (expr), complain))
5842 inform (input_location,
5843 "standard conversions are not allowed in this context");
5844 return NULL_TREE;
5845 }
5846 }
5847 /* [temp.arg.nontype]/5, bullet 7
5848
5849 For a non-type template-parameter of type pointer to data member,
5850 qualification conversions (_conv.qual_) are applied. */
5851 else if (TYPE_PTRDATAMEM_P (type))
5852 {
5853 /* [temp.arg.nontype] bullet 1 says the pointer to member
5854 expression must be a pointer-to-member constant. */
5855 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5856 return error_mark_node;
5857
5858 expr = perform_qualification_conversions (type, expr);
5859 if (expr == error_mark_node)
5860 return expr;
5861 }
5862 else if (NULLPTR_TYPE_P (type))
5863 {
5864 if (expr != nullptr_node)
5865 {
5866 error ("%qE is not a valid template argument for type %qT "
5867 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5868 return NULL_TREE;
5869 }
5870 return expr;
5871 }
5872 /* A template non-type parameter must be one of the above. */
5873 else
5874 gcc_unreachable ();
5875
5876 /* Sanity check: did we actually convert the argument to the
5877 right type? */
5878 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5879 (type, TREE_TYPE (expr)));
5880 return expr;
5881 }
5882
5883 /* Subroutine of coerce_template_template_parms, which returns 1 if
5884 PARM_PARM and ARG_PARM match using the rule for the template
5885 parameters of template template parameters. Both PARM and ARG are
5886 template parameters; the rest of the arguments are the same as for
5887 coerce_template_template_parms.
5888 */
5889 static int
5890 coerce_template_template_parm (tree parm,
5891 tree arg,
5892 tsubst_flags_t complain,
5893 tree in_decl,
5894 tree outer_args)
5895 {
5896 if (arg == NULL_TREE || arg == error_mark_node
5897 || parm == NULL_TREE || parm == error_mark_node)
5898 return 0;
5899
5900 if (TREE_CODE (arg) != TREE_CODE (parm))
5901 return 0;
5902
5903 switch (TREE_CODE (parm))
5904 {
5905 case TEMPLATE_DECL:
5906 /* We encounter instantiations of templates like
5907 template <template <template <class> class> class TT>
5908 class C; */
5909 {
5910 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5911 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5912
5913 if (!coerce_template_template_parms
5914 (parmparm, argparm, complain, in_decl, outer_args))
5915 return 0;
5916 }
5917 /* Fall through. */
5918
5919 case TYPE_DECL:
5920 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5921 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5922 /* Argument is a parameter pack but parameter is not. */
5923 return 0;
5924 break;
5925
5926 case PARM_DECL:
5927 /* The tsubst call is used to handle cases such as
5928
5929 template <int> class C {};
5930 template <class T, template <T> class TT> class D {};
5931 D<int, C> d;
5932
5933 i.e. the parameter list of TT depends on earlier parameters. */
5934 if (!uses_template_parms (TREE_TYPE (arg))
5935 && !same_type_p
5936 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5937 TREE_TYPE (arg)))
5938 return 0;
5939
5940 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5941 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5942 /* Argument is a parameter pack but parameter is not. */
5943 return 0;
5944
5945 break;
5946
5947 default:
5948 gcc_unreachable ();
5949 }
5950
5951 return 1;
5952 }
5953
5954
5955 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5956 template template parameters. Both PARM_PARMS and ARG_PARMS are
5957 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5958 or PARM_DECL.
5959
5960 Consider the example:
5961 template <class T> class A;
5962 template<template <class U> class TT> class B;
5963
5964 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5965 the parameters to A, and OUTER_ARGS contains A. */
5966
5967 static int
5968 coerce_template_template_parms (tree parm_parms,
5969 tree arg_parms,
5970 tsubst_flags_t complain,
5971 tree in_decl,
5972 tree outer_args)
5973 {
5974 int nparms, nargs, i;
5975 tree parm, arg;
5976 int variadic_p = 0;
5977
5978 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5979 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5980
5981 nparms = TREE_VEC_LENGTH (parm_parms);
5982 nargs = TREE_VEC_LENGTH (arg_parms);
5983
5984 /* Determine whether we have a parameter pack at the end of the
5985 template template parameter's template parameter list. */
5986 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5987 {
5988 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5989
5990 if (parm == error_mark_node)
5991 return 0;
5992
5993 switch (TREE_CODE (parm))
5994 {
5995 case TEMPLATE_DECL:
5996 case TYPE_DECL:
5997 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5998 variadic_p = 1;
5999 break;
6000
6001 case PARM_DECL:
6002 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6003 variadic_p = 1;
6004 break;
6005
6006 default:
6007 gcc_unreachable ();
6008 }
6009 }
6010
6011 if (nargs != nparms
6012 && !(variadic_p && nargs >= nparms - 1))
6013 return 0;
6014
6015 /* Check all of the template parameters except the parameter pack at
6016 the end (if any). */
6017 for (i = 0; i < nparms - variadic_p; ++i)
6018 {
6019 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
6020 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6021 continue;
6022
6023 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6024 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6025
6026 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6027 outer_args))
6028 return 0;
6029
6030 }
6031
6032 if (variadic_p)
6033 {
6034 /* Check each of the template parameters in the template
6035 argument against the template parameter pack at the end of
6036 the template template parameter. */
6037 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
6038 return 0;
6039
6040 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
6041
6042 for (; i < nargs; ++i)
6043 {
6044 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
6045 continue;
6046
6047 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
6048
6049 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
6050 outer_args))
6051 return 0;
6052 }
6053 }
6054
6055 return 1;
6056 }
6057
6058 /* Verifies that the deduced template arguments (in TARGS) for the
6059 template template parameters (in TPARMS) represent valid bindings,
6060 by comparing the template parameter list of each template argument
6061 to the template parameter list of its corresponding template
6062 template parameter, in accordance with DR150. This
6063 routine can only be called after all template arguments have been
6064 deduced. It will return TRUE if all of the template template
6065 parameter bindings are okay, FALSE otherwise. */
6066 bool
6067 template_template_parm_bindings_ok_p (tree tparms, tree targs)
6068 {
6069 int i, ntparms = TREE_VEC_LENGTH (tparms);
6070 bool ret = true;
6071
6072 /* We're dealing with template parms in this process. */
6073 ++processing_template_decl;
6074
6075 targs = INNERMOST_TEMPLATE_ARGS (targs);
6076
6077 for (i = 0; i < ntparms; ++i)
6078 {
6079 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
6080 tree targ = TREE_VEC_ELT (targs, i);
6081
6082 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6083 {
6084 tree packed_args = NULL_TREE;
6085 int idx, len = 1;
6086
6087 if (ARGUMENT_PACK_P (targ))
6088 {
6089 /* Look inside the argument pack. */
6090 packed_args = ARGUMENT_PACK_ARGS (targ);
6091 len = TREE_VEC_LENGTH (packed_args);
6092 }
6093
6094 for (idx = 0; idx < len; ++idx)
6095 {
6096 tree targ_parms = NULL_TREE;
6097
6098 if (packed_args)
6099 /* Extract the next argument from the argument
6100 pack. */
6101 targ = TREE_VEC_ELT (packed_args, idx);
6102
6103 if (PACK_EXPANSION_P (targ))
6104 /* Look at the pattern of the pack expansion. */
6105 targ = PACK_EXPANSION_PATTERN (targ);
6106
6107 /* Extract the template parameters from the template
6108 argument. */
6109 if (TREE_CODE (targ) == TEMPLATE_DECL)
6110 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6111 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6112 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6113
6114 /* Verify that we can coerce the template template
6115 parameters from the template argument to the template
6116 parameter. This requires an exact match. */
6117 if (targ_parms
6118 && !coerce_template_template_parms
6119 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6120 targ_parms,
6121 tf_none,
6122 tparm,
6123 targs))
6124 {
6125 ret = false;
6126 goto out;
6127 }
6128 }
6129 }
6130 }
6131
6132 out:
6133
6134 --processing_template_decl;
6135 return ret;
6136 }
6137
6138 /* Since type attributes aren't mangled, we need to strip them from
6139 template type arguments. */
6140
6141 static tree
6142 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6143 {
6144 tree mv;
6145 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6146 return arg;
6147 mv = TYPE_MAIN_VARIANT (arg);
6148 arg = strip_typedefs (arg);
6149 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6150 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6151 {
6152 if (complain & tf_warning)
6153 warning (0, "ignoring attributes on template argument %qT", arg);
6154 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6155 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6156 }
6157 return arg;
6158 }
6159
6160 /* Convert the indicated template ARG as necessary to match the
6161 indicated template PARM. Returns the converted ARG, or
6162 error_mark_node if the conversion was unsuccessful. Error and
6163 warning messages are issued under control of COMPLAIN. This
6164 conversion is for the Ith parameter in the parameter list. ARGS is
6165 the full set of template arguments deduced so far. */
6166
6167 static tree
6168 convert_template_argument (tree parm,
6169 tree arg,
6170 tree args,
6171 tsubst_flags_t complain,
6172 int i,
6173 tree in_decl)
6174 {
6175 tree orig_arg;
6176 tree val;
6177 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6178
6179 if (TREE_CODE (arg) == TREE_LIST
6180 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6181 {
6182 /* The template argument was the name of some
6183 member function. That's usually
6184 invalid, but static members are OK. In any
6185 case, grab the underlying fields/functions
6186 and issue an error later if required. */
6187 orig_arg = TREE_VALUE (arg);
6188 TREE_TYPE (arg) = unknown_type_node;
6189 }
6190
6191 orig_arg = arg;
6192
6193 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6194 requires_type = (TREE_CODE (parm) == TYPE_DECL
6195 || requires_tmpl_type);
6196
6197 /* When determining whether an argument pack expansion is a template,
6198 look at the pattern. */
6199 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6200 arg = PACK_EXPANSION_PATTERN (arg);
6201
6202 /* Deal with an injected-class-name used as a template template arg. */
6203 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6204 {
6205 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6206 if (TREE_CODE (t) == TEMPLATE_DECL)
6207 {
6208 if (cxx_dialect >= cxx0x)
6209 /* OK under DR 1004. */;
6210 else if (complain & tf_warning_or_error)
6211 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
6212 " used as template template argument", TYPE_NAME (arg));
6213 else if (flag_pedantic_errors)
6214 t = arg;
6215
6216 arg = t;
6217 }
6218 }
6219
6220 is_tmpl_type =
6221 ((TREE_CODE (arg) == TEMPLATE_DECL
6222 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6223 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6224 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6225 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6226
6227 if (is_tmpl_type
6228 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6229 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6230 arg = TYPE_STUB_DECL (arg);
6231
6232 is_type = TYPE_P (arg) || is_tmpl_type;
6233
6234 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6235 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6236 {
6237 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6238 {
6239 if (complain & tf_error)
6240 error ("invalid use of destructor %qE as a type", orig_arg);
6241 return error_mark_node;
6242 }
6243
6244 permerror (input_location,
6245 "to refer to a type member of a template parameter, "
6246 "use %<typename %E%>", orig_arg);
6247
6248 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6249 TREE_OPERAND (arg, 1),
6250 typename_type,
6251 complain);
6252 arg = orig_arg;
6253 is_type = 1;
6254 }
6255 if (is_type != requires_type)
6256 {
6257 if (in_decl)
6258 {
6259 if (complain & tf_error)
6260 {
6261 error ("type/value mismatch at argument %d in template "
6262 "parameter list for %qD",
6263 i + 1, in_decl);
6264 if (is_type)
6265 error (" expected a constant of type %qT, got %qT",
6266 TREE_TYPE (parm),
6267 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6268 else if (requires_tmpl_type)
6269 error (" expected a class template, got %qE", orig_arg);
6270 else
6271 error (" expected a type, got %qE", orig_arg);
6272 }
6273 }
6274 return error_mark_node;
6275 }
6276 if (is_tmpl_type ^ requires_tmpl_type)
6277 {
6278 if (in_decl && (complain & tf_error))
6279 {
6280 error ("type/value mismatch at argument %d in template "
6281 "parameter list for %qD",
6282 i + 1, in_decl);
6283 if (is_tmpl_type)
6284 error (" expected a type, got %qT", DECL_NAME (arg));
6285 else
6286 error (" expected a class template, got %qT", orig_arg);
6287 }
6288 return error_mark_node;
6289 }
6290
6291 if (is_type)
6292 {
6293 if (requires_tmpl_type)
6294 {
6295 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6296 val = orig_arg;
6297 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6298 /* The number of argument required is not known yet.
6299 Just accept it for now. */
6300 val = TREE_TYPE (arg);
6301 else
6302 {
6303 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6304 tree argparm;
6305
6306 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6307
6308 if (coerce_template_template_parms (parmparm, argparm,
6309 complain, in_decl,
6310 args))
6311 {
6312 val = arg;
6313
6314 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6315 TEMPLATE_DECL. */
6316 if (val != error_mark_node)
6317 {
6318 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6319 val = TREE_TYPE (val);
6320 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6321 val = make_pack_expansion (val);
6322 }
6323 }
6324 else
6325 {
6326 if (in_decl && (complain & tf_error))
6327 {
6328 error ("type/value mismatch at argument %d in "
6329 "template parameter list for %qD",
6330 i + 1, in_decl);
6331 error (" expected a template of type %qD, got %qT",
6332 parm, orig_arg);
6333 }
6334
6335 val = error_mark_node;
6336 }
6337 }
6338 }
6339 else
6340 val = orig_arg;
6341 /* We only form one instance of each template specialization.
6342 Therefore, if we use a non-canonical variant (i.e., a
6343 typedef), any future messages referring to the type will use
6344 the typedef, which is confusing if those future uses do not
6345 themselves also use the typedef. */
6346 if (TYPE_P (val))
6347 val = canonicalize_type_argument (val, complain);
6348 }
6349 else
6350 {
6351 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6352
6353 if (invalid_nontype_parm_type_p (t, complain))
6354 return error_mark_node;
6355
6356 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6357 {
6358 if (same_type_p (t, TREE_TYPE (orig_arg)))
6359 val = orig_arg;
6360 else
6361 {
6362 /* Not sure if this is reachable, but it doesn't hurt
6363 to be robust. */
6364 error ("type mismatch in nontype parameter pack");
6365 val = error_mark_node;
6366 }
6367 }
6368 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6369 /* We used to call digest_init here. However, digest_init
6370 will report errors, which we don't want when complain
6371 is zero. More importantly, digest_init will try too
6372 hard to convert things: for example, `0' should not be
6373 converted to pointer type at this point according to
6374 the standard. Accepting this is not merely an
6375 extension, since deciding whether or not these
6376 conversions can occur is part of determining which
6377 function template to call, or whether a given explicit
6378 argument specification is valid. */
6379 val = convert_nontype_argument (t, orig_arg, complain);
6380 else
6381 val = strip_typedefs_expr (orig_arg);
6382
6383 if (val == NULL_TREE)
6384 val = error_mark_node;
6385 else if (val == error_mark_node && (complain & tf_error))
6386 error ("could not convert template argument %qE to %qT", orig_arg, t);
6387
6388 if (TREE_CODE (val) == SCOPE_REF)
6389 {
6390 /* Strip typedefs from the SCOPE_REF. */
6391 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6392 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6393 complain);
6394 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6395 QUALIFIED_NAME_IS_TEMPLATE (val));
6396 }
6397 }
6398
6399 return val;
6400 }
6401
6402 /* Coerces the remaining template arguments in INNER_ARGS (from
6403 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6404 Returns the coerced argument pack. PARM_IDX is the position of this
6405 parameter in the template parameter list. ARGS is the original
6406 template argument list. */
6407 static tree
6408 coerce_template_parameter_pack (tree parms,
6409 int parm_idx,
6410 tree args,
6411 tree inner_args,
6412 int arg_idx,
6413 tree new_args,
6414 int* lost,
6415 tree in_decl,
6416 tsubst_flags_t complain)
6417 {
6418 tree parm = TREE_VEC_ELT (parms, parm_idx);
6419 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6420 tree packed_args;
6421 tree argument_pack;
6422 tree packed_types = NULL_TREE;
6423
6424 if (arg_idx > nargs)
6425 arg_idx = nargs;
6426
6427 packed_args = make_tree_vec (nargs - arg_idx);
6428
6429 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6430 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6431 {
6432 /* When the template parameter is a non-type template
6433 parameter pack whose type uses parameter packs, we need
6434 to look at each of the template arguments
6435 separately. Build a vector of the types for these
6436 non-type template parameters in PACKED_TYPES. */
6437 tree expansion
6438 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6439 packed_types = tsubst_pack_expansion (expansion, args,
6440 complain, in_decl);
6441
6442 if (packed_types == error_mark_node)
6443 return error_mark_node;
6444
6445 /* Check that we have the right number of arguments. */
6446 if (arg_idx < nargs
6447 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6448 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6449 {
6450 int needed_parms
6451 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6452 error ("wrong number of template arguments (%d, should be %d)",
6453 nargs, needed_parms);
6454 return error_mark_node;
6455 }
6456
6457 /* If we aren't able to check the actual arguments now
6458 (because they haven't been expanded yet), we can at least
6459 verify that all of the types used for the non-type
6460 template parameter pack are, in fact, valid for non-type
6461 template parameters. */
6462 if (arg_idx < nargs
6463 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6464 {
6465 int j, len = TREE_VEC_LENGTH (packed_types);
6466 for (j = 0; j < len; ++j)
6467 {
6468 tree t = TREE_VEC_ELT (packed_types, j);
6469 if (invalid_nontype_parm_type_p (t, complain))
6470 return error_mark_node;
6471 }
6472 }
6473 }
6474
6475 /* Convert the remaining arguments, which will be a part of the
6476 parameter pack "parm". */
6477 for (; arg_idx < nargs; ++arg_idx)
6478 {
6479 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6480 tree actual_parm = TREE_VALUE (parm);
6481
6482 if (packed_types && !PACK_EXPANSION_P (arg))
6483 {
6484 /* When we have a vector of types (corresponding to the
6485 non-type template parameter pack that uses parameter
6486 packs in its type, as mention above), and the
6487 argument is not an expansion (which expands to a
6488 currently unknown number of arguments), clone the
6489 parm and give it the next type in PACKED_TYPES. */
6490 actual_parm = copy_node (actual_parm);
6491 TREE_TYPE (actual_parm) =
6492 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6493 }
6494
6495 if (arg == error_mark_node)
6496 {
6497 if (complain & tf_error)
6498 error ("template argument %d is invalid", arg_idx + 1);
6499 }
6500 else
6501 arg = convert_template_argument (actual_parm,
6502 arg, new_args, complain, parm_idx,
6503 in_decl);
6504 if (arg == error_mark_node)
6505 (*lost)++;
6506 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6507 }
6508
6509 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6510 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6511 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6512 else
6513 {
6514 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6515 TREE_TYPE (argument_pack)
6516 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6517 TREE_CONSTANT (argument_pack) = 1;
6518 }
6519
6520 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6521 #ifdef ENABLE_CHECKING
6522 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6523 TREE_VEC_LENGTH (packed_args));
6524 #endif
6525 return argument_pack;
6526 }
6527
6528 /* Returns true if the template argument vector ARGS contains
6529 any pack expansions, false otherwise. */
6530
6531 static bool
6532 any_pack_expanson_args_p (tree args)
6533 {
6534 int i;
6535 if (args)
6536 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6537 if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
6538 return true;
6539 return false;
6540 }
6541
6542 /* Convert all template arguments to their appropriate types, and
6543 return a vector containing the innermost resulting template
6544 arguments. If any error occurs, return error_mark_node. Error and
6545 warning messages are issued under control of COMPLAIN.
6546
6547 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6548 for arguments not specified in ARGS. Otherwise, if
6549 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6550 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6551 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6552 ARGS. */
6553
6554 static tree
6555 coerce_template_parms (tree parms,
6556 tree args,
6557 tree in_decl,
6558 tsubst_flags_t complain,
6559 bool require_all_args,
6560 bool use_default_args)
6561 {
6562 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6563 tree inner_args;
6564 tree new_args;
6565 tree new_inner_args;
6566 int saved_unevaluated_operand;
6567 int saved_inhibit_evaluation_warnings;
6568
6569 /* When used as a boolean value, indicates whether this is a
6570 variadic template parameter list. Since it's an int, we can also
6571 subtract it from nparms to get the number of non-variadic
6572 parameters. */
6573 int variadic_p = 0;
6574 int post_variadic_parms = 0;
6575
6576 if (args == error_mark_node)
6577 return error_mark_node;
6578
6579 nparms = TREE_VEC_LENGTH (parms);
6580
6581 /* Determine if there are any parameter packs. */
6582 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6583 {
6584 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6585 if (variadic_p)
6586 ++post_variadic_parms;
6587 if (template_parameter_pack_p (tparm))
6588 ++variadic_p;
6589 }
6590
6591 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6592 /* If there are no parameters that follow a parameter pack, we need to
6593 expand any argument packs so that we can deduce a parameter pack from
6594 some non-packed args followed by an argument pack, as in variadic85.C.
6595 If there are such parameters, we need to leave argument packs intact
6596 so the arguments are assigned properly. This can happen when dealing
6597 with a nested class inside a partial specialization of a class
6598 template, as in variadic92.C, or when deducing a template parameter pack
6599 from a sub-declarator, as in variadic114.C. */
6600 if (!post_variadic_parms)
6601 inner_args = expand_template_argument_pack (inner_args);
6602
6603 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6604 if ((nargs > nparms && !variadic_p)
6605 || (nargs < nparms - variadic_p
6606 && require_all_args
6607 && !any_pack_expanson_args_p (inner_args)
6608 && (!use_default_args
6609 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6610 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6611 {
6612 if (complain & tf_error)
6613 {
6614 if (variadic_p)
6615 {
6616 nparms -= variadic_p;
6617 error ("wrong number of template arguments "
6618 "(%d, should be %d or more)", nargs, nparms);
6619 }
6620 else
6621 error ("wrong number of template arguments "
6622 "(%d, should be %d)", nargs, nparms);
6623
6624 if (in_decl)
6625 error ("provided for %q+D", in_decl);
6626 }
6627
6628 return error_mark_node;
6629 }
6630
6631 /* We need to evaluate the template arguments, even though this
6632 template-id may be nested within a "sizeof". */
6633 saved_unevaluated_operand = cp_unevaluated_operand;
6634 cp_unevaluated_operand = 0;
6635 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6636 c_inhibit_evaluation_warnings = 0;
6637 new_inner_args = make_tree_vec (nparms);
6638 new_args = add_outermost_template_args (args, new_inner_args);
6639 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6640 {
6641 tree arg;
6642 tree parm;
6643
6644 /* Get the Ith template parameter. */
6645 parm = TREE_VEC_ELT (parms, parm_idx);
6646
6647 if (parm == error_mark_node)
6648 {
6649 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6650 continue;
6651 }
6652
6653 /* Calculate the next argument. */
6654 if (arg_idx < nargs)
6655 arg = TREE_VEC_ELT (inner_args, arg_idx);
6656 else
6657 arg = NULL_TREE;
6658
6659 if (template_parameter_pack_p (TREE_VALUE (parm))
6660 && !(arg && ARGUMENT_PACK_P (arg)))
6661 {
6662 /* All remaining arguments will be placed in the
6663 template parameter pack PARM. */
6664 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6665 inner_args, arg_idx,
6666 new_args, &lost,
6667 in_decl, complain);
6668
6669 /* Store this argument. */
6670 if (arg == error_mark_node)
6671 lost++;
6672 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6673
6674 /* We are done with all of the arguments. */
6675 arg_idx = nargs;
6676
6677 continue;
6678 }
6679 else if (arg)
6680 {
6681 if (PACK_EXPANSION_P (arg))
6682 {
6683 /* We don't know how many args we have yet, just
6684 use the unconverted ones for now. */
6685 new_inner_args = inner_args;
6686 break;
6687 }
6688 }
6689 else if (require_all_args)
6690 {
6691 /* There must be a default arg in this case. */
6692 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6693 complain, in_decl);
6694 /* The position of the first default template argument,
6695 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6696 Record that. */
6697 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6698 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6699 }
6700 else
6701 break;
6702
6703 if (arg == error_mark_node)
6704 {
6705 if (complain & tf_error)
6706 error ("template argument %d is invalid", arg_idx + 1);
6707 }
6708 else if (!arg)
6709 /* This only occurs if there was an error in the template
6710 parameter list itself (which we would already have
6711 reported) that we are trying to recover from, e.g., a class
6712 template with a parameter list such as
6713 template<typename..., typename>. */
6714 ++lost;
6715 else
6716 arg = convert_template_argument (TREE_VALUE (parm),
6717 arg, new_args, complain,
6718 parm_idx, in_decl);
6719
6720 if (arg == error_mark_node)
6721 lost++;
6722 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6723 }
6724 cp_unevaluated_operand = saved_unevaluated_operand;
6725 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6726
6727 if (lost)
6728 return error_mark_node;
6729
6730 #ifdef ENABLE_CHECKING
6731 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6732 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6733 TREE_VEC_LENGTH (new_inner_args));
6734 #endif
6735
6736 return new_inner_args;
6737 }
6738
6739 /* Returns 1 if template args OT and NT are equivalent. */
6740
6741 static int
6742 template_args_equal (tree ot, tree nt)
6743 {
6744 if (nt == ot)
6745 return 1;
6746 if (nt == NULL_TREE || ot == NULL_TREE)
6747 return false;
6748
6749 if (TREE_CODE (nt) == TREE_VEC)
6750 /* For member templates */
6751 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6752 else if (PACK_EXPANSION_P (ot))
6753 return (PACK_EXPANSION_P (nt)
6754 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6755 PACK_EXPANSION_PATTERN (nt))
6756 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6757 PACK_EXPANSION_EXTRA_ARGS (nt)));
6758 else if (ARGUMENT_PACK_P (ot))
6759 {
6760 int i, len;
6761 tree opack, npack;
6762
6763 if (!ARGUMENT_PACK_P (nt))
6764 return 0;
6765
6766 opack = ARGUMENT_PACK_ARGS (ot);
6767 npack = ARGUMENT_PACK_ARGS (nt);
6768 len = TREE_VEC_LENGTH (opack);
6769 if (TREE_VEC_LENGTH (npack) != len)
6770 return 0;
6771 for (i = 0; i < len; ++i)
6772 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6773 TREE_VEC_ELT (npack, i)))
6774 return 0;
6775 return 1;
6776 }
6777 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6778 {
6779 /* We get here probably because we are in the middle of substituting
6780 into the pattern of a pack expansion. In that case the
6781 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6782 interested in. So we want to use the initial pack argument for
6783 the comparison. */
6784 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6785 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6786 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6787 return template_args_equal (ot, nt);
6788 }
6789 else if (TYPE_P (nt))
6790 return TYPE_P (ot) && same_type_p (ot, nt);
6791 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6792 return 0;
6793 else
6794 return cp_tree_equal (ot, nt);
6795 }
6796
6797 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6798 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6799 NEWARG_PTR with the offending arguments if they are non-NULL. */
6800
6801 static int
6802 comp_template_args_with_info (tree oldargs, tree newargs,
6803 tree *oldarg_ptr, tree *newarg_ptr)
6804 {
6805 int i;
6806
6807 if (oldargs == newargs)
6808 return 1;
6809
6810 if (!oldargs || !newargs)
6811 return 0;
6812
6813 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6814 return 0;
6815
6816 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6817 {
6818 tree nt = TREE_VEC_ELT (newargs, i);
6819 tree ot = TREE_VEC_ELT (oldargs, i);
6820
6821 if (! template_args_equal (ot, nt))
6822 {
6823 if (oldarg_ptr != NULL)
6824 *oldarg_ptr = ot;
6825 if (newarg_ptr != NULL)
6826 *newarg_ptr = nt;
6827 return 0;
6828 }
6829 }
6830 return 1;
6831 }
6832
6833 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6834 of template arguments. Returns 0 otherwise. */
6835
6836 int
6837 comp_template_args (tree oldargs, tree newargs)
6838 {
6839 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6840 }
6841
6842 static void
6843 add_pending_template (tree d)
6844 {
6845 tree ti = (TYPE_P (d)
6846 ? CLASSTYPE_TEMPLATE_INFO (d)
6847 : DECL_TEMPLATE_INFO (d));
6848 struct pending_template *pt;
6849 int level;
6850
6851 if (TI_PENDING_TEMPLATE_FLAG (ti))
6852 return;
6853
6854 /* We are called both from instantiate_decl, where we've already had a
6855 tinst_level pushed, and instantiate_template, where we haven't.
6856 Compensate. */
6857 level = !current_tinst_level || current_tinst_level->decl != d;
6858
6859 if (level)
6860 push_tinst_level (d);
6861
6862 pt = ggc_alloc_pending_template ();
6863 pt->next = NULL;
6864 pt->tinst = current_tinst_level;
6865 if (last_pending_template)
6866 last_pending_template->next = pt;
6867 else
6868 pending_templates = pt;
6869
6870 last_pending_template = pt;
6871
6872 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6873
6874 if (level)
6875 pop_tinst_level ();
6876 }
6877
6878
6879 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6880 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6881 documentation for TEMPLATE_ID_EXPR. */
6882
6883 tree
6884 lookup_template_function (tree fns, tree arglist)
6885 {
6886 tree type;
6887
6888 if (fns == error_mark_node || arglist == error_mark_node)
6889 return error_mark_node;
6890
6891 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6892
6893 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6894 {
6895 error ("%q#D is not a function template", fns);
6896 return error_mark_node;
6897 }
6898
6899 if (BASELINK_P (fns))
6900 {
6901 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6902 unknown_type_node,
6903 BASELINK_FUNCTIONS (fns),
6904 arglist);
6905 return fns;
6906 }
6907
6908 type = TREE_TYPE (fns);
6909 if (TREE_CODE (fns) == OVERLOAD || !type)
6910 type = unknown_type_node;
6911
6912 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6913 }
6914
6915 /* Within the scope of a template class S<T>, the name S gets bound
6916 (in build_self_reference) to a TYPE_DECL for the class, not a
6917 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6918 or one of its enclosing classes, and that type is a template,
6919 return the associated TEMPLATE_DECL. Otherwise, the original
6920 DECL is returned.
6921
6922 Also handle the case when DECL is a TREE_LIST of ambiguous
6923 injected-class-names from different bases. */
6924
6925 tree
6926 maybe_get_template_decl_from_type_decl (tree decl)
6927 {
6928 if (decl == NULL_TREE)
6929 return decl;
6930
6931 /* DR 176: A lookup that finds an injected-class-name (10.2
6932 [class.member.lookup]) can result in an ambiguity in certain cases
6933 (for example, if it is found in more than one base class). If all of
6934 the injected-class-names that are found refer to specializations of
6935 the same class template, and if the name is followed by a
6936 template-argument-list, the reference refers to the class template
6937 itself and not a specialization thereof, and is not ambiguous. */
6938 if (TREE_CODE (decl) == TREE_LIST)
6939 {
6940 tree t, tmpl = NULL_TREE;
6941 for (t = decl; t; t = TREE_CHAIN (t))
6942 {
6943 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6944 if (!tmpl)
6945 tmpl = elt;
6946 else if (tmpl != elt)
6947 break;
6948 }
6949 if (tmpl && t == NULL_TREE)
6950 return tmpl;
6951 else
6952 return decl;
6953 }
6954
6955 return (decl != NULL_TREE
6956 && DECL_SELF_REFERENCE_P (decl)
6957 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6958 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6959 }
6960
6961 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6962 parameters, find the desired type.
6963
6964 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6965
6966 IN_DECL, if non-NULL, is the template declaration we are trying to
6967 instantiate.
6968
6969 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6970 the class we are looking up.
6971
6972 Issue error and warning messages under control of COMPLAIN.
6973
6974 If the template class is really a local class in a template
6975 function, then the FUNCTION_CONTEXT is the function in which it is
6976 being instantiated.
6977
6978 ??? Note that this function is currently called *twice* for each
6979 template-id: the first time from the parser, while creating the
6980 incomplete type (finish_template_type), and the second type during the
6981 real instantiation (instantiate_template_class). This is surely something
6982 that we want to avoid. It also causes some problems with argument
6983 coercion (see convert_nontype_argument for more information on this). */
6984
6985 static tree
6986 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
6987 int entering_scope, tsubst_flags_t complain)
6988 {
6989 tree templ = NULL_TREE, parmlist;
6990 tree t;
6991 void **slot;
6992 spec_entry *entry;
6993 spec_entry elt;
6994 hashval_t hash;
6995
6996 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6997 {
6998 tree value = innermost_non_namespace_value (d1);
6999 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
7000 templ = value;
7001 else
7002 {
7003 if (context)
7004 push_decl_namespace (context);
7005 templ = lookup_name (d1);
7006 templ = maybe_get_template_decl_from_type_decl (templ);
7007 if (context)
7008 pop_decl_namespace ();
7009 }
7010 if (templ)
7011 context = DECL_CONTEXT (templ);
7012 }
7013 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
7014 {
7015 tree type = TREE_TYPE (d1);
7016
7017 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7018 an implicit typename for the second A. Deal with it. */
7019 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
7020 type = TREE_TYPE (type);
7021
7022 if (CLASSTYPE_TEMPLATE_INFO (type))
7023 {
7024 templ = CLASSTYPE_TI_TEMPLATE (type);
7025 d1 = DECL_NAME (templ);
7026 }
7027 }
7028 else if (TREE_CODE (d1) == ENUMERAL_TYPE
7029 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
7030 {
7031 templ = TYPE_TI_TEMPLATE (d1);
7032 d1 = DECL_NAME (templ);
7033 }
7034 else if (TREE_CODE (d1) == TEMPLATE_DECL
7035 && DECL_TEMPLATE_RESULT (d1)
7036 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
7037 {
7038 templ = d1;
7039 d1 = DECL_NAME (templ);
7040 context = DECL_CONTEXT (templ);
7041 }
7042
7043 /* Issue an error message if we didn't find a template. */
7044 if (! templ)
7045 {
7046 if (complain & tf_error)
7047 error ("%qT is not a template", d1);
7048 return error_mark_node;
7049 }
7050
7051 if (TREE_CODE (templ) != TEMPLATE_DECL
7052 /* Make sure it's a user visible template, if it was named by
7053 the user. */
7054 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
7055 && !PRIMARY_TEMPLATE_P (templ)))
7056 {
7057 if (complain & tf_error)
7058 {
7059 error ("non-template type %qT used as a template", d1);
7060 if (in_decl)
7061 error ("for template declaration %q+D", in_decl);
7062 }
7063 return error_mark_node;
7064 }
7065
7066 complain &= ~tf_user;
7067
7068 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
7069 {
7070 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7071 template arguments */
7072
7073 tree parm;
7074 tree arglist2;
7075 tree outer;
7076
7077 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7078
7079 /* Consider an example where a template template parameter declared as
7080
7081 template <class T, class U = std::allocator<T> > class TT
7082
7083 The template parameter level of T and U are one level larger than
7084 of TT. To proper process the default argument of U, say when an
7085 instantiation `TT<int>' is seen, we need to build the full
7086 arguments containing {int} as the innermost level. Outer levels,
7087 available when not appearing as default template argument, can be
7088 obtained from the arguments of the enclosing template.
7089
7090 Suppose that TT is later substituted with std::vector. The above
7091 instantiation is `TT<int, std::allocator<T> >' with TT at
7092 level 1, and T at level 2, while the template arguments at level 1
7093 becomes {std::vector} and the inner level 2 is {int}. */
7094
7095 outer = DECL_CONTEXT (templ);
7096 if (outer)
7097 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7098 else if (current_template_parms)
7099 /* This is an argument of the current template, so we haven't set
7100 DECL_CONTEXT yet. */
7101 outer = current_template_args ();
7102
7103 if (outer)
7104 arglist = add_to_template_args (outer, arglist);
7105
7106 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7107 complain,
7108 /*require_all_args=*/true,
7109 /*use_default_args=*/true);
7110 if (arglist2 == error_mark_node
7111 || (!uses_template_parms (arglist2)
7112 && check_instantiated_args (templ, arglist2, complain)))
7113 return error_mark_node;
7114
7115 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7116 return parm;
7117 }
7118 else
7119 {
7120 tree template_type = TREE_TYPE (templ);
7121 tree gen_tmpl;
7122 tree type_decl;
7123 tree found = NULL_TREE;
7124 int arg_depth;
7125 int parm_depth;
7126 int is_dependent_type;
7127 int use_partial_inst_tmpl = false;
7128
7129 if (template_type == error_mark_node)
7130 /* An error occured while building the template TEMPL, and a
7131 diagnostic has most certainly been emitted for that
7132 already. Let's propagate that error. */
7133 return error_mark_node;
7134
7135 gen_tmpl = most_general_template (templ);
7136 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7137 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7138 arg_depth = TMPL_ARGS_DEPTH (arglist);
7139
7140 if (arg_depth == 1 && parm_depth > 1)
7141 {
7142 /* We've been given an incomplete set of template arguments.
7143 For example, given:
7144
7145 template <class T> struct S1 {
7146 template <class U> struct S2 {};
7147 template <class U> struct S2<U*> {};
7148 };
7149
7150 we will be called with an ARGLIST of `U*', but the
7151 TEMPLATE will be `template <class T> template
7152 <class U> struct S1<T>::S2'. We must fill in the missing
7153 arguments. */
7154 arglist
7155 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7156 arglist);
7157 arg_depth = TMPL_ARGS_DEPTH (arglist);
7158 }
7159
7160 /* Now we should have enough arguments. */
7161 gcc_assert (parm_depth == arg_depth);
7162
7163 /* From here on, we're only interested in the most general
7164 template. */
7165
7166 /* Calculate the BOUND_ARGS. These will be the args that are
7167 actually tsubst'd into the definition to create the
7168 instantiation. */
7169 if (parm_depth > 1)
7170 {
7171 /* We have multiple levels of arguments to coerce, at once. */
7172 int i;
7173 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7174
7175 tree bound_args = make_tree_vec (parm_depth);
7176
7177 for (i = saved_depth,
7178 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7179 i > 0 && t != NULL_TREE;
7180 --i, t = TREE_CHAIN (t))
7181 {
7182 tree a;
7183 if (i == saved_depth)
7184 a = coerce_template_parms (TREE_VALUE (t),
7185 arglist, gen_tmpl,
7186 complain,
7187 /*require_all_args=*/true,
7188 /*use_default_args=*/true);
7189 else
7190 /* Outer levels should have already been coerced. */
7191 a = TMPL_ARGS_LEVEL (arglist, i);
7192
7193 /* Don't process further if one of the levels fails. */
7194 if (a == error_mark_node)
7195 {
7196 /* Restore the ARGLIST to its full size. */
7197 TREE_VEC_LENGTH (arglist) = saved_depth;
7198 return error_mark_node;
7199 }
7200
7201 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7202
7203 /* We temporarily reduce the length of the ARGLIST so
7204 that coerce_template_parms will see only the arguments
7205 corresponding to the template parameters it is
7206 examining. */
7207 TREE_VEC_LENGTH (arglist)--;
7208 }
7209
7210 /* Restore the ARGLIST to its full size. */
7211 TREE_VEC_LENGTH (arglist) = saved_depth;
7212
7213 arglist = bound_args;
7214 }
7215 else
7216 arglist
7217 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7218 INNERMOST_TEMPLATE_ARGS (arglist),
7219 gen_tmpl,
7220 complain,
7221 /*require_all_args=*/true,
7222 /*use_default_args=*/true);
7223
7224 if (arglist == error_mark_node)
7225 /* We were unable to bind the arguments. */
7226 return error_mark_node;
7227
7228 /* In the scope of a template class, explicit references to the
7229 template class refer to the type of the template, not any
7230 instantiation of it. For example, in:
7231
7232 template <class T> class C { void f(C<T>); }
7233
7234 the `C<T>' is just the same as `C'. Outside of the
7235 class, however, such a reference is an instantiation. */
7236 if ((entering_scope
7237 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7238 || currently_open_class (template_type))
7239 /* comp_template_args is expensive, check it last. */
7240 && comp_template_args (TYPE_TI_ARGS (template_type),
7241 arglist))
7242 return template_type;
7243
7244 /* If we already have this specialization, return it. */
7245 elt.tmpl = gen_tmpl;
7246 elt.args = arglist;
7247 hash = hash_specialization (&elt);
7248 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7249 &elt, hash);
7250
7251 if (entry)
7252 return entry->spec;
7253
7254 is_dependent_type = uses_template_parms (arglist);
7255
7256 /* If the deduced arguments are invalid, then the binding
7257 failed. */
7258 if (!is_dependent_type
7259 && check_instantiated_args (gen_tmpl,
7260 INNERMOST_TEMPLATE_ARGS (arglist),
7261 complain))
7262 return error_mark_node;
7263
7264 if (!is_dependent_type
7265 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7266 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7267 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7268 {
7269 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7270 DECL_NAME (gen_tmpl),
7271 /*tag_scope=*/ts_global);
7272 return found;
7273 }
7274
7275 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7276 complain, in_decl);
7277 if (context == error_mark_node)
7278 return error_mark_node;
7279
7280 if (!context)
7281 context = global_namespace;
7282
7283 /* Create the type. */
7284 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7285 {
7286 if (!is_dependent_type)
7287 {
7288 set_current_access_from_decl (TYPE_NAME (template_type));
7289 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7290 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7291 arglist, complain, in_decl),
7292 SCOPED_ENUM_P (template_type), NULL);
7293 }
7294 else
7295 {
7296 /* We don't want to call start_enum for this type, since
7297 the values for the enumeration constants may involve
7298 template parameters. And, no one should be interested
7299 in the enumeration constants for such a type. */
7300 t = cxx_make_type (ENUMERAL_TYPE);
7301 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7302 }
7303 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7304 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7305 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7306 }
7307 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7308 {
7309 /* The user referred to a specialization of an alias
7310 template represented by GEN_TMPL.
7311
7312 [temp.alias]/2 says:
7313
7314 When a template-id refers to the specialization of an
7315 alias template, it is equivalent to the associated
7316 type obtained by substitution of its
7317 template-arguments for the template-parameters in the
7318 type-id of the alias template. */
7319
7320 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7321 /* Note that the call above (by indirectly calling
7322 register_specialization in tsubst_decl) registers the
7323 TYPE_DECL representing the specialization of the alias
7324 template. So next time someone substitutes ARGLIST for
7325 the template parms into the alias template (GEN_TMPL),
7326 she'll get that TYPE_DECL back. */
7327
7328 if (t == error_mark_node)
7329 return t;
7330 }
7331 else if (CLASS_TYPE_P (template_type))
7332 {
7333 t = make_class_type (TREE_CODE (template_type));
7334 CLASSTYPE_DECLARED_CLASS (t)
7335 = CLASSTYPE_DECLARED_CLASS (template_type);
7336 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7337 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7338
7339 /* A local class. Make sure the decl gets registered properly. */
7340 if (context == current_function_decl)
7341 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7342
7343 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7344 /* This instantiation is another name for the primary
7345 template type. Set the TYPE_CANONICAL field
7346 appropriately. */
7347 TYPE_CANONICAL (t) = template_type;
7348 else if (any_template_arguments_need_structural_equality_p (arglist))
7349 /* Some of the template arguments require structural
7350 equality testing, so this template class requires
7351 structural equality testing. */
7352 SET_TYPE_STRUCTURAL_EQUALITY (t);
7353 }
7354 else
7355 gcc_unreachable ();
7356
7357 /* If we called start_enum or pushtag above, this information
7358 will already be set up. */
7359 if (!TYPE_NAME (t))
7360 {
7361 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7362
7363 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7364 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7365 DECL_SOURCE_LOCATION (type_decl)
7366 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7367 }
7368 else
7369 type_decl = TYPE_NAME (t);
7370
7371 if (CLASS_TYPE_P (template_type))
7372 {
7373 TREE_PRIVATE (type_decl)
7374 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
7375 TREE_PROTECTED (type_decl)
7376 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
7377 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7378 {
7379 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7380 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7381 }
7382 }
7383
7384 /* Let's consider the explicit specialization of a member
7385 of a class template specialization that is implicitely instantiated,
7386 e.g.:
7387 template<class T>
7388 struct S
7389 {
7390 template<class U> struct M {}; //#0
7391 };
7392
7393 template<>
7394 template<>
7395 struct S<int>::M<char> //#1
7396 {
7397 int i;
7398 };
7399 [temp.expl.spec]/4 says this is valid.
7400
7401 In this case, when we write:
7402 S<int>::M<char> m;
7403
7404 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7405 the one of #0.
7406
7407 When we encounter #1, we want to store the partial instantiation
7408 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7409
7410 For all cases other than this "explicit specialization of member of a
7411 class template", we just want to store the most general template into
7412 the CLASSTYPE_TI_TEMPLATE of M.
7413
7414 This case of "explicit specialization of member of a class template"
7415 only happens when:
7416 1/ the enclosing class is an instantiation of, and therefore not
7417 the same as, the context of the most general template, and
7418 2/ we aren't looking at the partial instantiation itself, i.e.
7419 the innermost arguments are not the same as the innermost parms of
7420 the most general template.
7421
7422 So it's only when 1/ and 2/ happens that we want to use the partial
7423 instantiation of the member template in lieu of its most general
7424 template. */
7425
7426 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7427 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7428 /* the enclosing class must be an instantiation... */
7429 && CLASS_TYPE_P (context)
7430 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7431 {
7432 tree partial_inst_args;
7433 TREE_VEC_LENGTH (arglist)--;
7434 ++processing_template_decl;
7435 partial_inst_args =
7436 tsubst (INNERMOST_TEMPLATE_ARGS
7437 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7438 arglist, complain, NULL_TREE);
7439 --processing_template_decl;
7440 TREE_VEC_LENGTH (arglist)++;
7441 use_partial_inst_tmpl =
7442 /*...and we must not be looking at the partial instantiation
7443 itself. */
7444 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7445 partial_inst_args);
7446 }
7447
7448 if (!use_partial_inst_tmpl)
7449 /* This case is easy; there are no member templates involved. */
7450 found = gen_tmpl;
7451 else
7452 {
7453 /* This is a full instantiation of a member template. Find
7454 the partial instantiation of which this is an instance. */
7455
7456 /* Temporarily reduce by one the number of levels in the ARGLIST
7457 so as to avoid comparing the last set of arguments. */
7458 TREE_VEC_LENGTH (arglist)--;
7459 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7460 TREE_VEC_LENGTH (arglist)++;
7461 /* FOUND is either a proper class type, or an alias
7462 template specialization. In the later case, it's a
7463 TYPE_DECL, resulting from the substituting of arguments
7464 for parameters in the TYPE_DECL of the alias template
7465 done earlier. So be careful while getting the template
7466 of FOUND. */
7467 found = TREE_CODE (found) == TYPE_DECL
7468 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7469 : CLASSTYPE_TI_TEMPLATE (found);
7470 }
7471
7472 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7473
7474 elt.spec = t;
7475 slot = htab_find_slot_with_hash (type_specializations,
7476 &elt, hash, INSERT);
7477 entry = ggc_alloc_spec_entry ();
7478 *entry = elt;
7479 *slot = entry;
7480
7481 /* Note this use of the partial instantiation so we can check it
7482 later in maybe_process_partial_specialization. */
7483 DECL_TEMPLATE_INSTANTIATIONS (templ)
7484 = tree_cons (arglist, t,
7485 DECL_TEMPLATE_INSTANTIATIONS (templ));
7486
7487 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type)
7488 /* Now that the type has been registered on the instantiations
7489 list, we set up the enumerators. Because the enumeration
7490 constants may involve the enumeration type itself, we make
7491 sure to register the type first, and then create the
7492 constants. That way, doing tsubst_expr for the enumeration
7493 constants won't result in recursive calls here; we'll find
7494 the instantiation and exit above. */
7495 tsubst_enum (template_type, t, arglist);
7496
7497 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7498 /* If the type makes use of template parameters, the
7499 code that generates debugging information will crash. */
7500 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7501
7502 /* Possibly limit visibility based on template args. */
7503 TREE_PUBLIC (type_decl) = 1;
7504 determine_visibility (type_decl);
7505
7506 return t;
7507 }
7508 }
7509
7510 /* Wrapper for lookup_template_class_1. */
7511
7512 tree
7513 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7514 int entering_scope, tsubst_flags_t complain)
7515 {
7516 tree ret;
7517 timevar_push (TV_TEMPLATE_INST);
7518 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7519 entering_scope, complain);
7520 timevar_pop (TV_TEMPLATE_INST);
7521 return ret;
7522 }
7523 \f
7524 struct pair_fn_data
7525 {
7526 tree_fn_t fn;
7527 void *data;
7528 /* True when we should also visit template parameters that occur in
7529 non-deduced contexts. */
7530 bool include_nondeduced_p;
7531 struct pointer_set_t *visited;
7532 };
7533
7534 /* Called from for_each_template_parm via walk_tree. */
7535
7536 static tree
7537 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7538 {
7539 tree t = *tp;
7540 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7541 tree_fn_t fn = pfd->fn;
7542 void *data = pfd->data;
7543
7544 if (TYPE_P (t)
7545 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7546 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7547 pfd->include_nondeduced_p))
7548 return error_mark_node;
7549
7550 switch (TREE_CODE (t))
7551 {
7552 case RECORD_TYPE:
7553 if (TYPE_PTRMEMFUNC_P (t))
7554 break;
7555 /* Fall through. */
7556
7557 case UNION_TYPE:
7558 case ENUMERAL_TYPE:
7559 if (!TYPE_TEMPLATE_INFO (t))
7560 *walk_subtrees = 0;
7561 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7562 fn, data, pfd->visited,
7563 pfd->include_nondeduced_p))
7564 return error_mark_node;
7565 break;
7566
7567 case INTEGER_TYPE:
7568 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7569 fn, data, pfd->visited,
7570 pfd->include_nondeduced_p)
7571 || for_each_template_parm (TYPE_MAX_VALUE (t),
7572 fn, data, pfd->visited,
7573 pfd->include_nondeduced_p))
7574 return error_mark_node;
7575 break;
7576
7577 case METHOD_TYPE:
7578 /* Since we're not going to walk subtrees, we have to do this
7579 explicitly here. */
7580 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7581 pfd->visited, pfd->include_nondeduced_p))
7582 return error_mark_node;
7583 /* Fall through. */
7584
7585 case FUNCTION_TYPE:
7586 /* Check the return type. */
7587 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7588 pfd->include_nondeduced_p))
7589 return error_mark_node;
7590
7591 /* Check the parameter types. Since default arguments are not
7592 instantiated until they are needed, the TYPE_ARG_TYPES may
7593 contain expressions that involve template parameters. But,
7594 no-one should be looking at them yet. And, once they're
7595 instantiated, they don't contain template parameters, so
7596 there's no point in looking at them then, either. */
7597 {
7598 tree parm;
7599
7600 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7601 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7602 pfd->visited, pfd->include_nondeduced_p))
7603 return error_mark_node;
7604
7605 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7606 want walk_tree walking into them itself. */
7607 *walk_subtrees = 0;
7608 }
7609 break;
7610
7611 case TYPEOF_TYPE:
7612 case UNDERLYING_TYPE:
7613 if (pfd->include_nondeduced_p
7614 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7615 pfd->visited,
7616 pfd->include_nondeduced_p))
7617 return error_mark_node;
7618 break;
7619
7620 case FUNCTION_DECL:
7621 case VAR_DECL:
7622 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7623 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7624 pfd->visited, pfd->include_nondeduced_p))
7625 return error_mark_node;
7626 /* Fall through. */
7627
7628 case PARM_DECL:
7629 case CONST_DECL:
7630 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7631 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7632 pfd->visited, pfd->include_nondeduced_p))
7633 return error_mark_node;
7634 if (DECL_CONTEXT (t)
7635 && pfd->include_nondeduced_p
7636 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7637 pfd->visited, pfd->include_nondeduced_p))
7638 return error_mark_node;
7639 break;
7640
7641 case BOUND_TEMPLATE_TEMPLATE_PARM:
7642 /* Record template parameters such as `T' inside `TT<T>'. */
7643 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7644 pfd->include_nondeduced_p))
7645 return error_mark_node;
7646 /* Fall through. */
7647
7648 case TEMPLATE_TEMPLATE_PARM:
7649 case TEMPLATE_TYPE_PARM:
7650 case TEMPLATE_PARM_INDEX:
7651 if (fn && (*fn)(t, data))
7652 return error_mark_node;
7653 else if (!fn)
7654 return error_mark_node;
7655 break;
7656
7657 case TEMPLATE_DECL:
7658 /* A template template parameter is encountered. */
7659 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7660 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7661 pfd->include_nondeduced_p))
7662 return error_mark_node;
7663
7664 /* Already substituted template template parameter */
7665 *walk_subtrees = 0;
7666 break;
7667
7668 case TYPENAME_TYPE:
7669 if (!fn
7670 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7671 data, pfd->visited,
7672 pfd->include_nondeduced_p))
7673 return error_mark_node;
7674 break;
7675
7676 case CONSTRUCTOR:
7677 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7678 && pfd->include_nondeduced_p
7679 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7680 (TREE_TYPE (t)), fn, data,
7681 pfd->visited, pfd->include_nondeduced_p))
7682 return error_mark_node;
7683 break;
7684
7685 case INDIRECT_REF:
7686 case COMPONENT_REF:
7687 /* If there's no type, then this thing must be some expression
7688 involving template parameters. */
7689 if (!fn && !TREE_TYPE (t))
7690 return error_mark_node;
7691 break;
7692
7693 case MODOP_EXPR:
7694 case CAST_EXPR:
7695 case IMPLICIT_CONV_EXPR:
7696 case REINTERPRET_CAST_EXPR:
7697 case CONST_CAST_EXPR:
7698 case STATIC_CAST_EXPR:
7699 case DYNAMIC_CAST_EXPR:
7700 case ARROW_EXPR:
7701 case DOTSTAR_EXPR:
7702 case TYPEID_EXPR:
7703 case PSEUDO_DTOR_EXPR:
7704 if (!fn)
7705 return error_mark_node;
7706 break;
7707
7708 default:
7709 break;
7710 }
7711
7712 /* We didn't find any template parameters we liked. */
7713 return NULL_TREE;
7714 }
7715
7716 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7717 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7718 call FN with the parameter and the DATA.
7719 If FN returns nonzero, the iteration is terminated, and
7720 for_each_template_parm returns 1. Otherwise, the iteration
7721 continues. If FN never returns a nonzero value, the value
7722 returned by for_each_template_parm is 0. If FN is NULL, it is
7723 considered to be the function which always returns 1.
7724
7725 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7726 parameters that occur in non-deduced contexts. When false, only
7727 visits those template parameters that can be deduced. */
7728
7729 static int
7730 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7731 struct pointer_set_t *visited,
7732 bool include_nondeduced_p)
7733 {
7734 struct pair_fn_data pfd;
7735 int result;
7736
7737 /* Set up. */
7738 pfd.fn = fn;
7739 pfd.data = data;
7740 pfd.include_nondeduced_p = include_nondeduced_p;
7741
7742 /* Walk the tree. (Conceptually, we would like to walk without
7743 duplicates, but for_each_template_parm_r recursively calls
7744 for_each_template_parm, so we would need to reorganize a fair
7745 bit to use walk_tree_without_duplicates, so we keep our own
7746 visited list.) */
7747 if (visited)
7748 pfd.visited = visited;
7749 else
7750 pfd.visited = pointer_set_create ();
7751 result = cp_walk_tree (&t,
7752 for_each_template_parm_r,
7753 &pfd,
7754 pfd.visited) != NULL_TREE;
7755
7756 /* Clean up. */
7757 if (!visited)
7758 {
7759 pointer_set_destroy (pfd.visited);
7760 pfd.visited = 0;
7761 }
7762
7763 return result;
7764 }
7765
7766 /* Returns true if T depends on any template parameter. */
7767
7768 int
7769 uses_template_parms (tree t)
7770 {
7771 bool dependent_p;
7772 int saved_processing_template_decl;
7773
7774 saved_processing_template_decl = processing_template_decl;
7775 if (!saved_processing_template_decl)
7776 processing_template_decl = 1;
7777 if (TYPE_P (t))
7778 dependent_p = dependent_type_p (t);
7779 else if (TREE_CODE (t) == TREE_VEC)
7780 dependent_p = any_dependent_template_arguments_p (t);
7781 else if (TREE_CODE (t) == TREE_LIST)
7782 dependent_p = (uses_template_parms (TREE_VALUE (t))
7783 || uses_template_parms (TREE_CHAIN (t)));
7784 else if (TREE_CODE (t) == TYPE_DECL)
7785 dependent_p = dependent_type_p (TREE_TYPE (t));
7786 else if (DECL_P (t)
7787 || EXPR_P (t)
7788 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7789 || TREE_CODE (t) == OVERLOAD
7790 || BASELINK_P (t)
7791 || TREE_CODE (t) == IDENTIFIER_NODE
7792 || TREE_CODE (t) == TRAIT_EXPR
7793 || TREE_CODE (t) == CONSTRUCTOR
7794 || CONSTANT_CLASS_P (t))
7795 dependent_p = (type_dependent_expression_p (t)
7796 || value_dependent_expression_p (t));
7797 else
7798 {
7799 gcc_assert (t == error_mark_node);
7800 dependent_p = false;
7801 }
7802
7803 processing_template_decl = saved_processing_template_decl;
7804
7805 return dependent_p;
7806 }
7807
7808 /* Returns true iff current_function_decl is an incompletely instantiated
7809 template. Useful instead of processing_template_decl because the latter
7810 is set to 0 during fold_non_dependent_expr. */
7811
7812 bool
7813 in_template_function (void)
7814 {
7815 tree fn = current_function_decl;
7816 bool ret;
7817 ++processing_template_decl;
7818 ret = (fn && DECL_LANG_SPECIFIC (fn)
7819 && DECL_TEMPLATE_INFO (fn)
7820 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
7821 --processing_template_decl;
7822 return ret;
7823 }
7824
7825 /* Returns true if T depends on any template parameter with level LEVEL. */
7826
7827 int
7828 uses_template_parms_level (tree t, int level)
7829 {
7830 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7831 /*include_nondeduced_p=*/true);
7832 }
7833
7834 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7835 ill-formed translation unit, i.e. a variable or function that isn't
7836 usable in a constant expression. */
7837
7838 static inline bool
7839 neglectable_inst_p (tree d)
7840 {
7841 return (DECL_P (d)
7842 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7843 : decl_maybe_constant_var_p (d)));
7844 }
7845
7846 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7847 neglectable and instantiated from within an erroneous instantiation. */
7848
7849 static bool
7850 limit_bad_template_recursion (tree decl)
7851 {
7852 struct tinst_level *lev = current_tinst_level;
7853 int errs = errorcount + sorrycount;
7854 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7855 return false;
7856
7857 for (; lev; lev = lev->next)
7858 if (neglectable_inst_p (lev->decl))
7859 break;
7860
7861 return (lev && errs > lev->errors);
7862 }
7863
7864 static int tinst_depth;
7865 extern int max_tinst_depth;
7866 int depth_reached;
7867
7868 static GTY(()) struct tinst_level *last_error_tinst_level;
7869
7870 /* We're starting to instantiate D; record the template instantiation context
7871 for diagnostics and to restore it later. */
7872
7873 int
7874 push_tinst_level (tree d)
7875 {
7876 struct tinst_level *new_level;
7877
7878 if (tinst_depth >= max_tinst_depth)
7879 {
7880 last_error_tinst_level = current_tinst_level;
7881 if (TREE_CODE (d) == TREE_LIST)
7882 error ("template instantiation depth exceeds maximum of %d (use "
7883 "-ftemplate-depth= to increase the maximum) substituting %qS",
7884 max_tinst_depth, d);
7885 else
7886 error ("template instantiation depth exceeds maximum of %d (use "
7887 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7888 max_tinst_depth, d);
7889
7890 print_instantiation_context ();
7891
7892 return 0;
7893 }
7894
7895 /* If the current instantiation caused problems, don't let it instantiate
7896 anything else. Do allow deduction substitution and decls usable in
7897 constant expressions. */
7898 if (limit_bad_template_recursion (d))
7899 return 0;
7900
7901 new_level = ggc_alloc_tinst_level ();
7902 new_level->decl = d;
7903 new_level->locus = input_location;
7904 new_level->errors = errorcount+sorrycount;
7905 new_level->in_system_header_p = in_system_header;
7906 new_level->next = current_tinst_level;
7907 current_tinst_level = new_level;
7908
7909 ++tinst_depth;
7910 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
7911 depth_reached = tinst_depth;
7912
7913 return 1;
7914 }
7915
7916 /* We're done instantiating this template; return to the instantiation
7917 context. */
7918
7919 void
7920 pop_tinst_level (void)
7921 {
7922 /* Restore the filename and line number stashed away when we started
7923 this instantiation. */
7924 input_location = current_tinst_level->locus;
7925 current_tinst_level = current_tinst_level->next;
7926 --tinst_depth;
7927 }
7928
7929 /* We're instantiating a deferred template; restore the template
7930 instantiation context in which the instantiation was requested, which
7931 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7932
7933 static tree
7934 reopen_tinst_level (struct tinst_level *level)
7935 {
7936 struct tinst_level *t;
7937
7938 tinst_depth = 0;
7939 for (t = level; t; t = t->next)
7940 ++tinst_depth;
7941
7942 current_tinst_level = level;
7943 pop_tinst_level ();
7944 if (current_tinst_level)
7945 current_tinst_level->errors = errorcount+sorrycount;
7946 return level->decl;
7947 }
7948
7949 /* Returns the TINST_LEVEL which gives the original instantiation
7950 context. */
7951
7952 struct tinst_level *
7953 outermost_tinst_level (void)
7954 {
7955 struct tinst_level *level = current_tinst_level;
7956 if (level)
7957 while (level->next)
7958 level = level->next;
7959 return level;
7960 }
7961
7962 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7963 vector of template arguments, as for tsubst.
7964
7965 Returns an appropriate tsubst'd friend declaration. */
7966
7967 static tree
7968 tsubst_friend_function (tree decl, tree args)
7969 {
7970 tree new_friend;
7971
7972 if (TREE_CODE (decl) == FUNCTION_DECL
7973 && DECL_TEMPLATE_INSTANTIATION (decl)
7974 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7975 /* This was a friend declared with an explicit template
7976 argument list, e.g.:
7977
7978 friend void f<>(T);
7979
7980 to indicate that f was a template instantiation, not a new
7981 function declaration. Now, we have to figure out what
7982 instantiation of what template. */
7983 {
7984 tree template_id, arglist, fns;
7985 tree new_args;
7986 tree tmpl;
7987 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7988
7989 /* Friend functions are looked up in the containing namespace scope.
7990 We must enter that scope, to avoid finding member functions of the
7991 current class with same name. */
7992 push_nested_namespace (ns);
7993 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7994 tf_warning_or_error, NULL_TREE,
7995 /*integral_constant_expression_p=*/false);
7996 pop_nested_namespace (ns);
7997 arglist = tsubst (DECL_TI_ARGS (decl), args,
7998 tf_warning_or_error, NULL_TREE);
7999 template_id = lookup_template_function (fns, arglist);
8000
8001 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8002 tmpl = determine_specialization (template_id, new_friend,
8003 &new_args,
8004 /*need_member_template=*/0,
8005 TREE_VEC_LENGTH (args),
8006 tsk_none);
8007 return instantiate_template (tmpl, new_args, tf_error);
8008 }
8009
8010 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
8011
8012 /* The NEW_FRIEND will look like an instantiation, to the
8013 compiler, but is not an instantiation from the point of view of
8014 the language. For example, we might have had:
8015
8016 template <class T> struct S {
8017 template <class U> friend void f(T, U);
8018 };
8019
8020 Then, in S<int>, template <class U> void f(int, U) is not an
8021 instantiation of anything. */
8022 if (new_friend == error_mark_node)
8023 return error_mark_node;
8024
8025 DECL_USE_TEMPLATE (new_friend) = 0;
8026 if (TREE_CODE (decl) == TEMPLATE_DECL)
8027 {
8028 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
8029 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
8030 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
8031 }
8032
8033 /* The mangled name for the NEW_FRIEND is incorrect. The function
8034 is not a template instantiation and should not be mangled like
8035 one. Therefore, we forget the mangling here; we'll recompute it
8036 later if we need it. */
8037 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
8038 {
8039 SET_DECL_RTL (new_friend, NULL);
8040 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
8041 }
8042
8043 if (DECL_NAMESPACE_SCOPE_P (new_friend))
8044 {
8045 tree old_decl;
8046 tree new_friend_template_info;
8047 tree new_friend_result_template_info;
8048 tree ns;
8049 int new_friend_is_defn;
8050
8051 /* We must save some information from NEW_FRIEND before calling
8052 duplicate decls since that function will free NEW_FRIEND if
8053 possible. */
8054 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
8055 new_friend_is_defn =
8056 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8057 (template_for_substitution (new_friend)))
8058 != NULL_TREE);
8059 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
8060 {
8061 /* This declaration is a `primary' template. */
8062 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
8063
8064 new_friend_result_template_info
8065 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
8066 }
8067 else
8068 new_friend_result_template_info = NULL_TREE;
8069
8070 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8071 if (new_friend_is_defn)
8072 DECL_INITIAL (new_friend) = error_mark_node;
8073
8074 /* Inside pushdecl_namespace_level, we will push into the
8075 current namespace. However, the friend function should go
8076 into the namespace of the template. */
8077 ns = decl_namespace_context (new_friend);
8078 push_nested_namespace (ns);
8079 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8080 pop_nested_namespace (ns);
8081
8082 if (old_decl == error_mark_node)
8083 return error_mark_node;
8084
8085 if (old_decl != new_friend)
8086 {
8087 /* This new friend declaration matched an existing
8088 declaration. For example, given:
8089
8090 template <class T> void f(T);
8091 template <class U> class C {
8092 template <class T> friend void f(T) {}
8093 };
8094
8095 the friend declaration actually provides the definition
8096 of `f', once C has been instantiated for some type. So,
8097 old_decl will be the out-of-class template declaration,
8098 while new_friend is the in-class definition.
8099
8100 But, if `f' was called before this point, the
8101 instantiation of `f' will have DECL_TI_ARGS corresponding
8102 to `T' but not to `U', references to which might appear
8103 in the definition of `f'. Previously, the most general
8104 template for an instantiation of `f' was the out-of-class
8105 version; now it is the in-class version. Therefore, we
8106 run through all specialization of `f', adding to their
8107 DECL_TI_ARGS appropriately. In particular, they need a
8108 new set of outer arguments, corresponding to the
8109 arguments for this class instantiation.
8110
8111 The same situation can arise with something like this:
8112
8113 friend void f(int);
8114 template <class T> class C {
8115 friend void f(T) {}
8116 };
8117
8118 when `C<int>' is instantiated. Now, `f(int)' is defined
8119 in the class. */
8120
8121 if (!new_friend_is_defn)
8122 /* On the other hand, if the in-class declaration does
8123 *not* provide a definition, then we don't want to alter
8124 existing definitions. We can just leave everything
8125 alone. */
8126 ;
8127 else
8128 {
8129 tree new_template = TI_TEMPLATE (new_friend_template_info);
8130 tree new_args = TI_ARGS (new_friend_template_info);
8131
8132 /* Overwrite whatever template info was there before, if
8133 any, with the new template information pertaining to
8134 the declaration. */
8135 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8136
8137 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8138 {
8139 /* We should have called reregister_specialization in
8140 duplicate_decls. */
8141 gcc_assert (retrieve_specialization (new_template,
8142 new_args, 0)
8143 == old_decl);
8144
8145 /* Instantiate it if the global has already been used. */
8146 if (DECL_ODR_USED (old_decl))
8147 instantiate_decl (old_decl, /*defer_ok=*/true,
8148 /*expl_inst_class_mem_p=*/false);
8149 }
8150 else
8151 {
8152 tree t;
8153
8154 /* Indicate that the old function template is a partial
8155 instantiation. */
8156 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8157 = new_friend_result_template_info;
8158
8159 gcc_assert (new_template
8160 == most_general_template (new_template));
8161 gcc_assert (new_template != old_decl);
8162
8163 /* Reassign any specializations already in the hash table
8164 to the new more general template, and add the
8165 additional template args. */
8166 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8167 t != NULL_TREE;
8168 t = TREE_CHAIN (t))
8169 {
8170 tree spec = TREE_VALUE (t);
8171 spec_entry elt;
8172
8173 elt.tmpl = old_decl;
8174 elt.args = DECL_TI_ARGS (spec);
8175 elt.spec = NULL_TREE;
8176
8177 htab_remove_elt (decl_specializations, &elt);
8178
8179 DECL_TI_ARGS (spec)
8180 = add_outermost_template_args (new_args,
8181 DECL_TI_ARGS (spec));
8182
8183 register_specialization
8184 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8185
8186 }
8187 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8188 }
8189 }
8190
8191 /* The information from NEW_FRIEND has been merged into OLD_DECL
8192 by duplicate_decls. */
8193 new_friend = old_decl;
8194 }
8195 }
8196 else
8197 {
8198 tree context = DECL_CONTEXT (new_friend);
8199 bool dependent_p;
8200
8201 /* In the code
8202 template <class T> class C {
8203 template <class U> friend void C1<U>::f (); // case 1
8204 friend void C2<T>::f (); // case 2
8205 };
8206 we only need to make sure CONTEXT is a complete type for
8207 case 2. To distinguish between the two cases, we note that
8208 CONTEXT of case 1 remains dependent type after tsubst while
8209 this isn't true for case 2. */
8210 ++processing_template_decl;
8211 dependent_p = dependent_type_p (context);
8212 --processing_template_decl;
8213
8214 if (!dependent_p
8215 && !complete_type_or_else (context, NULL_TREE))
8216 return error_mark_node;
8217
8218 if (COMPLETE_TYPE_P (context))
8219 {
8220 /* Check to see that the declaration is really present, and,
8221 possibly obtain an improved declaration. */
8222 tree fn = check_classfn (context,
8223 new_friend, NULL_TREE);
8224
8225 if (fn)
8226 new_friend = fn;
8227 }
8228 }
8229
8230 return new_friend;
8231 }
8232
8233 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8234 template arguments, as for tsubst.
8235
8236 Returns an appropriate tsubst'd friend type or error_mark_node on
8237 failure. */
8238
8239 static tree
8240 tsubst_friend_class (tree friend_tmpl, tree args)
8241 {
8242 tree friend_type;
8243 tree tmpl;
8244 tree context;
8245
8246 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
8247 {
8248 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
8249 return TREE_TYPE (t);
8250 }
8251
8252 context = CP_DECL_CONTEXT (friend_tmpl);
8253
8254 if (context != global_namespace)
8255 {
8256 if (TREE_CODE (context) == NAMESPACE_DECL)
8257 push_nested_namespace (context);
8258 else
8259 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8260 }
8261
8262 /* Look for a class template declaration. We look for hidden names
8263 because two friend declarations of the same template are the
8264 same. For example, in:
8265
8266 struct A {
8267 template <typename> friend class F;
8268 };
8269 template <typename> struct B {
8270 template <typename> friend class F;
8271 };
8272
8273 both F templates are the same. */
8274 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8275 /*block_p=*/true, 0, LOOKUP_HIDDEN);
8276
8277 /* But, if we don't find one, it might be because we're in a
8278 situation like this:
8279
8280 template <class T>
8281 struct S {
8282 template <class U>
8283 friend struct S;
8284 };
8285
8286 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8287 for `S<int>', not the TEMPLATE_DECL. */
8288 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8289 {
8290 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8291 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8292 }
8293
8294 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8295 {
8296 /* The friend template has already been declared. Just
8297 check to see that the declarations match, and install any new
8298 default parameters. We must tsubst the default parameters,
8299 of course. We only need the innermost template parameters
8300 because that is all that redeclare_class_template will look
8301 at. */
8302 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8303 > TMPL_ARGS_DEPTH (args))
8304 {
8305 tree parms;
8306 location_t saved_input_location;
8307 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8308 args, tf_warning_or_error);
8309
8310 saved_input_location = input_location;
8311 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8312 redeclare_class_template (TREE_TYPE (tmpl), parms);
8313 input_location = saved_input_location;
8314
8315 }
8316
8317 friend_type = TREE_TYPE (tmpl);
8318 }
8319 else
8320 {
8321 /* The friend template has not already been declared. In this
8322 case, the instantiation of the template class will cause the
8323 injection of this template into the global scope. */
8324 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8325 if (tmpl == error_mark_node)
8326 return error_mark_node;
8327
8328 /* The new TMPL is not an instantiation of anything, so we
8329 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8330 the new type because that is supposed to be the corresponding
8331 template decl, i.e., TMPL. */
8332 DECL_USE_TEMPLATE (tmpl) = 0;
8333 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8334 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8335 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8336 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8337
8338 /* Inject this template into the global scope. */
8339 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8340 }
8341
8342 if (context != global_namespace)
8343 {
8344 if (TREE_CODE (context) == NAMESPACE_DECL)
8345 pop_nested_namespace (context);
8346 else
8347 pop_nested_class ();
8348 }
8349
8350 return friend_type;
8351 }
8352
8353 /* Returns zero if TYPE cannot be completed later due to circularity.
8354 Otherwise returns one. */
8355
8356 static int
8357 can_complete_type_without_circularity (tree type)
8358 {
8359 if (type == NULL_TREE || type == error_mark_node)
8360 return 0;
8361 else if (COMPLETE_TYPE_P (type))
8362 return 1;
8363 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8364 return can_complete_type_without_circularity (TREE_TYPE (type));
8365 else if (CLASS_TYPE_P (type)
8366 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8367 return 0;
8368 else
8369 return 1;
8370 }
8371
8372 /* Apply any attributes which had to be deferred until instantiation
8373 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8374 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8375
8376 static void
8377 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8378 tree args, tsubst_flags_t complain, tree in_decl)
8379 {
8380 tree last_dep = NULL_TREE;
8381 tree t;
8382 tree *p;
8383
8384 for (t = attributes; t; t = TREE_CHAIN (t))
8385 if (ATTR_IS_DEPENDENT (t))
8386 {
8387 last_dep = t;
8388 attributes = copy_list (attributes);
8389 break;
8390 }
8391
8392 if (DECL_P (*decl_p))
8393 {
8394 if (TREE_TYPE (*decl_p) == error_mark_node)
8395 return;
8396 p = &DECL_ATTRIBUTES (*decl_p);
8397 }
8398 else
8399 p = &TYPE_ATTRIBUTES (*decl_p);
8400
8401 if (last_dep)
8402 {
8403 tree late_attrs = NULL_TREE;
8404 tree *q = &late_attrs;
8405
8406 for (*p = attributes; *p; )
8407 {
8408 t = *p;
8409 if (ATTR_IS_DEPENDENT (t))
8410 {
8411 *p = TREE_CHAIN (t);
8412 TREE_CHAIN (t) = NULL_TREE;
8413 /* If the first attribute argument is an identifier, don't
8414 pass it through tsubst. Attributes like mode, format,
8415 cleanup and several target specific attributes expect it
8416 unmodified. */
8417 if (TREE_VALUE (t)
8418 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8419 && TREE_VALUE (TREE_VALUE (t))
8420 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8421 == IDENTIFIER_NODE))
8422 {
8423 tree chain
8424 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8425 in_decl,
8426 /*integral_constant_expression_p=*/false);
8427 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8428 TREE_VALUE (t)
8429 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8430 chain);
8431 }
8432 else
8433 TREE_VALUE (t)
8434 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8435 /*integral_constant_expression_p=*/false);
8436 *q = t;
8437 q = &TREE_CHAIN (t);
8438 }
8439 else
8440 p = &TREE_CHAIN (t);
8441 }
8442
8443 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8444 }
8445 }
8446
8447 /* Perform (or defer) access check for typedefs that were referenced
8448 from within the template TMPL code.
8449 This is a subroutine of instantiate_decl and instantiate_class_template.
8450 TMPL is the template to consider and TARGS is the list of arguments of
8451 that template. */
8452
8453 static void
8454 perform_typedefs_access_check (tree tmpl, tree targs)
8455 {
8456 location_t saved_location;
8457 unsigned i;
8458 qualified_typedef_usage_t *iter;
8459
8460 if (!tmpl
8461 || (!CLASS_TYPE_P (tmpl)
8462 && TREE_CODE (tmpl) != FUNCTION_DECL))
8463 return;
8464
8465 saved_location = input_location;
8466 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
8467 {
8468 tree type_decl = iter->typedef_decl;
8469 tree type_scope = iter->context;
8470
8471 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8472 continue;
8473
8474 if (uses_template_parms (type_decl))
8475 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8476 if (uses_template_parms (type_scope))
8477 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8478
8479 /* Make access check error messages point to the location
8480 of the use of the typedef. */
8481 input_location = iter->locus;
8482 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8483 type_decl, type_decl,
8484 tf_warning_or_error);
8485 }
8486 input_location = saved_location;
8487 }
8488
8489 static tree
8490 instantiate_class_template_1 (tree type)
8491 {
8492 tree templ, args, pattern, t, member;
8493 tree typedecl;
8494 tree pbinfo;
8495 tree base_list;
8496 unsigned int saved_maximum_field_alignment;
8497 tree fn_context;
8498
8499 if (type == error_mark_node)
8500 return error_mark_node;
8501
8502 if (COMPLETE_OR_OPEN_TYPE_P (type)
8503 || uses_template_parms (type))
8504 return type;
8505
8506 /* Figure out which template is being instantiated. */
8507 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8508 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8509
8510 /* Determine what specialization of the original template to
8511 instantiate. */
8512 t = most_specialized_class (type, templ, tf_warning_or_error);
8513 if (t == error_mark_node)
8514 {
8515 TYPE_BEING_DEFINED (type) = 1;
8516 return error_mark_node;
8517 }
8518 else if (t)
8519 {
8520 /* This TYPE is actually an instantiation of a partial
8521 specialization. We replace the innermost set of ARGS with
8522 the arguments appropriate for substitution. For example,
8523 given:
8524
8525 template <class T> struct S {};
8526 template <class T> struct S<T*> {};
8527
8528 and supposing that we are instantiating S<int*>, ARGS will
8529 presently be {int*} -- but we need {int}. */
8530 pattern = TREE_TYPE (t);
8531 args = TREE_PURPOSE (t);
8532 }
8533 else
8534 {
8535 pattern = TREE_TYPE (templ);
8536 args = CLASSTYPE_TI_ARGS (type);
8537 }
8538
8539 /* If the template we're instantiating is incomplete, then clearly
8540 there's nothing we can do. */
8541 if (!COMPLETE_TYPE_P (pattern))
8542 return type;
8543
8544 /* If we've recursively instantiated too many templates, stop. */
8545 if (! push_tinst_level (type))
8546 return type;
8547
8548 /* Now we're really doing the instantiation. Mark the type as in
8549 the process of being defined. */
8550 TYPE_BEING_DEFINED (type) = 1;
8551
8552 /* We may be in the middle of deferred access check. Disable
8553 it now. */
8554 push_deferring_access_checks (dk_no_deferred);
8555
8556 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8557 if (!fn_context)
8558 push_to_top_level ();
8559 /* Use #pragma pack from the template context. */
8560 saved_maximum_field_alignment = maximum_field_alignment;
8561 maximum_field_alignment = TYPE_PRECISION (pattern);
8562
8563 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8564
8565 /* Set the input location to the most specialized template definition.
8566 This is needed if tsubsting causes an error. */
8567 typedecl = TYPE_MAIN_DECL (pattern);
8568 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8569 DECL_SOURCE_LOCATION (typedecl);
8570
8571 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8572 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8573 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8574 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8575 if (ANON_AGGR_TYPE_P (pattern))
8576 SET_ANON_AGGR_TYPE_P (type);
8577 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8578 {
8579 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8580 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8581 /* Adjust visibility for template arguments. */
8582 determine_visibility (TYPE_MAIN_DECL (type));
8583 }
8584 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8585
8586 pbinfo = TYPE_BINFO (pattern);
8587
8588 /* We should never instantiate a nested class before its enclosing
8589 class; we need to look up the nested class by name before we can
8590 instantiate it, and that lookup should instantiate the enclosing
8591 class. */
8592 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8593 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8594
8595 base_list = NULL_TREE;
8596 if (BINFO_N_BASE_BINFOS (pbinfo))
8597 {
8598 tree pbase_binfo;
8599 tree pushed_scope;
8600 int i;
8601
8602 /* We must enter the scope containing the type, as that is where
8603 the accessibility of types named in dependent bases are
8604 looked up from. */
8605 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8606
8607 /* Substitute into each of the bases to determine the actual
8608 basetypes. */
8609 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8610 {
8611 tree base;
8612 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8613 tree expanded_bases = NULL_TREE;
8614 int idx, len = 1;
8615
8616 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8617 {
8618 expanded_bases =
8619 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8620 args, tf_error, NULL_TREE);
8621 if (expanded_bases == error_mark_node)
8622 continue;
8623
8624 len = TREE_VEC_LENGTH (expanded_bases);
8625 }
8626
8627 for (idx = 0; idx < len; idx++)
8628 {
8629 if (expanded_bases)
8630 /* Extract the already-expanded base class. */
8631 base = TREE_VEC_ELT (expanded_bases, idx);
8632 else
8633 /* Substitute to figure out the base class. */
8634 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8635 NULL_TREE);
8636
8637 if (base == error_mark_node)
8638 continue;
8639
8640 base_list = tree_cons (access, base, base_list);
8641 if (BINFO_VIRTUAL_P (pbase_binfo))
8642 TREE_TYPE (base_list) = integer_type_node;
8643 }
8644 }
8645
8646 /* The list is now in reverse order; correct that. */
8647 base_list = nreverse (base_list);
8648
8649 if (pushed_scope)
8650 pop_scope (pushed_scope);
8651 }
8652 /* Now call xref_basetypes to set up all the base-class
8653 information. */
8654 xref_basetypes (type, base_list);
8655
8656 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8657 (int) ATTR_FLAG_TYPE_IN_PLACE,
8658 args, tf_error, NULL_TREE);
8659 fixup_attribute_variants (type);
8660
8661 /* Now that our base classes are set up, enter the scope of the
8662 class, so that name lookups into base classes, etc. will work
8663 correctly. This is precisely analogous to what we do in
8664 begin_class_definition when defining an ordinary non-template
8665 class, except we also need to push the enclosing classes. */
8666 push_nested_class (type);
8667
8668 /* Now members are processed in the order of declaration. */
8669 for (member = CLASSTYPE_DECL_LIST (pattern);
8670 member; member = TREE_CHAIN (member))
8671 {
8672 tree t = TREE_VALUE (member);
8673
8674 if (TREE_PURPOSE (member))
8675 {
8676 if (TYPE_P (t))
8677 {
8678 /* Build new CLASSTYPE_NESTED_UTDS. */
8679
8680 tree newtag;
8681 bool class_template_p;
8682
8683 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8684 && TYPE_LANG_SPECIFIC (t)
8685 && CLASSTYPE_IS_TEMPLATE (t));
8686 /* If the member is a class template, then -- even after
8687 substitution -- there may be dependent types in the
8688 template argument list for the class. We increment
8689 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8690 that function will assume that no types are dependent
8691 when outside of a template. */
8692 if (class_template_p)
8693 ++processing_template_decl;
8694 newtag = tsubst (t, args, tf_error, NULL_TREE);
8695 if (class_template_p)
8696 --processing_template_decl;
8697 if (newtag == error_mark_node)
8698 continue;
8699
8700 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8701 {
8702 tree name = TYPE_IDENTIFIER (t);
8703
8704 if (class_template_p)
8705 /* Unfortunately, lookup_template_class sets
8706 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8707 instantiation (i.e., for the type of a member
8708 template class nested within a template class.)
8709 This behavior is required for
8710 maybe_process_partial_specialization to work
8711 correctly, but is not accurate in this case;
8712 the TAG is not an instantiation of anything.
8713 (The corresponding TEMPLATE_DECL is an
8714 instantiation, but the TYPE is not.) */
8715 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8716
8717 /* Now, we call pushtag to put this NEWTAG into the scope of
8718 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8719 pushtag calling push_template_decl. We don't have to do
8720 this for enums because it will already have been done in
8721 tsubst_enum. */
8722 if (name)
8723 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8724 pushtag (name, newtag, /*tag_scope=*/ts_current);
8725 }
8726 }
8727 else if (TREE_CODE (t) == FUNCTION_DECL
8728 || DECL_FUNCTION_TEMPLATE_P (t))
8729 {
8730 /* Build new TYPE_METHODS. */
8731 tree r;
8732
8733 if (TREE_CODE (t) == TEMPLATE_DECL)
8734 ++processing_template_decl;
8735 r = tsubst (t, args, tf_error, NULL_TREE);
8736 if (TREE_CODE (t) == TEMPLATE_DECL)
8737 --processing_template_decl;
8738 set_current_access_from_decl (r);
8739 finish_member_declaration (r);
8740 /* Instantiate members marked with attribute used. */
8741 if (r != error_mark_node && DECL_PRESERVE_P (r))
8742 mark_used (r);
8743 }
8744 else
8745 {
8746 /* Build new TYPE_FIELDS. */
8747 if (TREE_CODE (t) == STATIC_ASSERT)
8748 {
8749 tree condition;
8750
8751 ++c_inhibit_evaluation_warnings;
8752 condition =
8753 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8754 tf_warning_or_error, NULL_TREE,
8755 /*integral_constant_expression_p=*/true);
8756 --c_inhibit_evaluation_warnings;
8757
8758 finish_static_assert (condition,
8759 STATIC_ASSERT_MESSAGE (t),
8760 STATIC_ASSERT_SOURCE_LOCATION (t),
8761 /*member_p=*/true);
8762 }
8763 else if (TREE_CODE (t) != CONST_DECL)
8764 {
8765 tree r;
8766
8767 /* The file and line for this declaration, to
8768 assist in error message reporting. Since we
8769 called push_tinst_level above, we don't need to
8770 restore these. */
8771 input_location = DECL_SOURCE_LOCATION (t);
8772
8773 if (TREE_CODE (t) == TEMPLATE_DECL)
8774 ++processing_template_decl;
8775 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8776 if (TREE_CODE (t) == TEMPLATE_DECL)
8777 --processing_template_decl;
8778 if (TREE_CODE (r) == VAR_DECL)
8779 {
8780 /* In [temp.inst]:
8781
8782 [t]he initialization (and any associated
8783 side-effects) of a static data member does
8784 not occur unless the static data member is
8785 itself used in a way that requires the
8786 definition of the static data member to
8787 exist.
8788
8789 Therefore, we do not substitute into the
8790 initialized for the static data member here. */
8791 finish_static_data_member_decl
8792 (r,
8793 /*init=*/NULL_TREE,
8794 /*init_const_expr_p=*/false,
8795 /*asmspec_tree=*/NULL_TREE,
8796 /*flags=*/0);
8797 /* Instantiate members marked with attribute used. */
8798 if (r != error_mark_node && DECL_PRESERVE_P (r))
8799 mark_used (r);
8800 }
8801 else if (TREE_CODE (r) == FIELD_DECL)
8802 {
8803 /* Determine whether R has a valid type and can be
8804 completed later. If R is invalid, then it is
8805 replaced by error_mark_node so that it will not be
8806 added to TYPE_FIELDS. */
8807 tree rtype = TREE_TYPE (r);
8808 if (can_complete_type_without_circularity (rtype))
8809 complete_type (rtype);
8810
8811 if (!COMPLETE_TYPE_P (rtype))
8812 {
8813 cxx_incomplete_type_error (r, rtype);
8814 r = error_mark_node;
8815 }
8816 }
8817
8818 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8819 such a thing will already have been added to the field
8820 list by tsubst_enum in finish_member_declaration in the
8821 CLASSTYPE_NESTED_UTDS case above. */
8822 if (!(TREE_CODE (r) == TYPE_DECL
8823 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8824 && DECL_ARTIFICIAL (r)))
8825 {
8826 set_current_access_from_decl (r);
8827 finish_member_declaration (r);
8828 }
8829 }
8830 }
8831 }
8832 else
8833 {
8834 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
8835 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8836 {
8837 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8838
8839 tree friend_type = t;
8840 bool adjust_processing_template_decl = false;
8841
8842 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8843 {
8844 /* template <class T> friend class C; */
8845 friend_type = tsubst_friend_class (friend_type, args);
8846 adjust_processing_template_decl = true;
8847 }
8848 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8849 {
8850 /* template <class T> friend class C::D; */
8851 friend_type = tsubst (friend_type, args,
8852 tf_warning_or_error, NULL_TREE);
8853 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8854 friend_type = TREE_TYPE (friend_type);
8855 adjust_processing_template_decl = true;
8856 }
8857 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8858 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8859 {
8860 /* This could be either
8861
8862 friend class T::C;
8863
8864 when dependent_type_p is false or
8865
8866 template <class U> friend class T::C;
8867
8868 otherwise. */
8869 friend_type = tsubst (friend_type, args,
8870 tf_warning_or_error, NULL_TREE);
8871 /* Bump processing_template_decl for correct
8872 dependent_type_p calculation. */
8873 ++processing_template_decl;
8874 if (dependent_type_p (friend_type))
8875 adjust_processing_template_decl = true;
8876 --processing_template_decl;
8877 }
8878 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8879 && hidden_name_p (TYPE_NAME (friend_type)))
8880 {
8881 /* friend class C;
8882
8883 where C hasn't been declared yet. Let's lookup name
8884 from namespace scope directly, bypassing any name that
8885 come from dependent base class. */
8886 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8887
8888 /* The call to xref_tag_from_type does injection for friend
8889 classes. */
8890 push_nested_namespace (ns);
8891 friend_type =
8892 xref_tag_from_type (friend_type, NULL_TREE,
8893 /*tag_scope=*/ts_current);
8894 pop_nested_namespace (ns);
8895 }
8896 else if (uses_template_parms (friend_type))
8897 /* friend class C<T>; */
8898 friend_type = tsubst (friend_type, args,
8899 tf_warning_or_error, NULL_TREE);
8900 /* Otherwise it's
8901
8902 friend class C;
8903
8904 where C is already declared or
8905
8906 friend class C<int>;
8907
8908 We don't have to do anything in these cases. */
8909
8910 if (adjust_processing_template_decl)
8911 /* Trick make_friend_class into realizing that the friend
8912 we're adding is a template, not an ordinary class. It's
8913 important that we use make_friend_class since it will
8914 perform some error-checking and output cross-reference
8915 information. */
8916 ++processing_template_decl;
8917
8918 if (friend_type != error_mark_node)
8919 make_friend_class (type, friend_type, /*complain=*/false);
8920
8921 if (adjust_processing_template_decl)
8922 --processing_template_decl;
8923 }
8924 else
8925 {
8926 /* Build new DECL_FRIENDLIST. */
8927 tree r;
8928
8929 /* The file and line for this declaration, to
8930 assist in error message reporting. Since we
8931 called push_tinst_level above, we don't need to
8932 restore these. */
8933 input_location = DECL_SOURCE_LOCATION (t);
8934
8935 if (TREE_CODE (t) == TEMPLATE_DECL)
8936 {
8937 ++processing_template_decl;
8938 push_deferring_access_checks (dk_no_check);
8939 }
8940
8941 r = tsubst_friend_function (t, args);
8942 add_friend (type, r, /*complain=*/false);
8943 if (TREE_CODE (t) == TEMPLATE_DECL)
8944 {
8945 pop_deferring_access_checks ();
8946 --processing_template_decl;
8947 }
8948 }
8949 }
8950 }
8951
8952 if (CLASSTYPE_LAMBDA_EXPR (type))
8953 {
8954 tree decl = lambda_function (type);
8955 if (decl)
8956 {
8957 instantiate_decl (decl, false, false);
8958 maybe_add_lambda_conv_op (type);
8959 }
8960 else
8961 gcc_assert (errorcount);
8962 }
8963
8964 /* Set the file and line number information to whatever is given for
8965 the class itself. This puts error messages involving generated
8966 implicit functions at a predictable point, and the same point
8967 that would be used for non-template classes. */
8968 input_location = DECL_SOURCE_LOCATION (typedecl);
8969
8970 unreverse_member_declarations (type);
8971 finish_struct_1 (type);
8972 TYPE_BEING_DEFINED (type) = 0;
8973
8974 /* We don't instantiate default arguments for member functions. 14.7.1:
8975
8976 The implicit instantiation of a class template specialization causes
8977 the implicit instantiation of the declarations, but not of the
8978 definitions or default arguments, of the class member functions,
8979 member classes, static data members and member templates.... */
8980
8981 /* Some typedefs referenced from within the template code need to be access
8982 checked at template instantiation time, i.e now. These types were
8983 added to the template at parsing time. Let's get those and perform
8984 the access checks then. */
8985 perform_typedefs_access_check (pattern, args);
8986 perform_deferred_access_checks (tf_warning_or_error);
8987 pop_nested_class ();
8988 maximum_field_alignment = saved_maximum_field_alignment;
8989 if (!fn_context)
8990 pop_from_top_level ();
8991 pop_deferring_access_checks ();
8992 pop_tinst_level ();
8993
8994 /* The vtable for a template class can be emitted in any translation
8995 unit in which the class is instantiated. When there is no key
8996 method, however, finish_struct_1 will already have added TYPE to
8997 the keyed_classes list. */
8998 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8999 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
9000
9001 return type;
9002 }
9003
9004 /* Wrapper for instantiate_class_template_1. */
9005
9006 tree
9007 instantiate_class_template (tree type)
9008 {
9009 tree ret;
9010 timevar_push (TV_TEMPLATE_INST);
9011 ret = instantiate_class_template_1 (type);
9012 timevar_pop (TV_TEMPLATE_INST);
9013 return ret;
9014 }
9015
9016 static tree
9017 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9018 {
9019 tree r;
9020
9021 if (!t)
9022 r = t;
9023 else if (TYPE_P (t))
9024 r = tsubst (t, args, complain, in_decl);
9025 else
9026 {
9027 if (!(complain & tf_warning))
9028 ++c_inhibit_evaluation_warnings;
9029 r = tsubst_expr (t, args, complain, in_decl,
9030 /*integral_constant_expression_p=*/true);
9031 if (!(complain & tf_warning))
9032 --c_inhibit_evaluation_warnings;
9033 /* Preserve the raw-reference nature of T. */
9034 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
9035 && REFERENCE_REF_P (r))
9036 r = TREE_OPERAND (r, 0);
9037 }
9038 return r;
9039 }
9040
9041 /* Given a function parameter pack TMPL_PARM and some function parameters
9042 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9043 and set *SPEC_P to point at the next point in the list. */
9044
9045 static tree
9046 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
9047 {
9048 /* Collect all of the extra "packed" parameters into an
9049 argument pack. */
9050 tree parmvec;
9051 tree parmtypevec;
9052 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
9053 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
9054 tree spec_parm = *spec_p;
9055 int i, len;
9056
9057 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
9058 if (tmpl_parm
9059 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
9060 break;
9061
9062 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9063 parmvec = make_tree_vec (len);
9064 parmtypevec = make_tree_vec (len);
9065 spec_parm = *spec_p;
9066 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
9067 {
9068 TREE_VEC_ELT (parmvec, i) = spec_parm;
9069 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
9070 }
9071
9072 /* Build the argument packs. */
9073 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9074 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9075 TREE_TYPE (argpack) = argtypepack;
9076 *spec_p = spec_parm;
9077
9078 return argpack;
9079 }
9080
9081 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9082 NONTYPE_ARGUMENT_PACK. */
9083
9084 static tree
9085 make_fnparm_pack (tree spec_parm)
9086 {
9087 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9088 }
9089
9090 /* Substitute ARGS into T, which is an pack expansion
9091 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9092 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9093 (if only a partial substitution could be performed) or
9094 ERROR_MARK_NODE if there was an error. */
9095 tree
9096 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9097 tree in_decl)
9098 {
9099 tree pattern;
9100 tree pack, packs = NULL_TREE;
9101 bool unsubstituted_packs = false;
9102 bool real_packs = false;
9103 int missing_level = 0;
9104 int i, len = -1;
9105 tree result;
9106 struct pointer_map_t *saved_local_specializations = NULL;
9107 bool need_local_specializations = false;
9108 int levels;
9109
9110 gcc_assert (PACK_EXPANSION_P (t));
9111 pattern = PACK_EXPANSION_PATTERN (t);
9112
9113 /* Add in any args remembered from an earlier partial instantiation. */
9114 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9115
9116 levels = TMPL_ARGS_DEPTH (args);
9117
9118 /* Determine the argument packs that will instantiate the parameter
9119 packs used in the expansion expression. While we're at it,
9120 compute the number of arguments to be expanded and make sure it
9121 is consistent. */
9122 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9123 pack = TREE_CHAIN (pack))
9124 {
9125 tree parm_pack = TREE_VALUE (pack);
9126 tree arg_pack = NULL_TREE;
9127 tree orig_arg = NULL_TREE;
9128 int level = 0;
9129
9130 if (TREE_CODE (parm_pack) == BASES)
9131 {
9132 if (BASES_DIRECT (parm_pack))
9133 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9134 args, complain, in_decl, false));
9135 else
9136 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9137 args, complain, in_decl, false));
9138 }
9139 if (TREE_CODE (parm_pack) == PARM_DECL)
9140 {
9141 if (PACK_EXPANSION_LOCAL_P (t))
9142 arg_pack = retrieve_local_specialization (parm_pack);
9143 else
9144 {
9145 /* We can't rely on local_specializations for a parameter
9146 name used later in a function declaration (such as in a
9147 late-specified return type). Even if it exists, it might
9148 have the wrong value for a recursive call. Just make a
9149 dummy decl, since it's only used for its type. */
9150 arg_pack = tsubst_decl (parm_pack, args, complain);
9151 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9152 /* Partial instantiation of the parm_pack, we can't build
9153 up an argument pack yet. */
9154 arg_pack = NULL_TREE;
9155 else
9156 arg_pack = make_fnparm_pack (arg_pack);
9157 need_local_specializations = true;
9158 }
9159 }
9160 else
9161 {
9162 int idx;
9163 template_parm_level_and_index (parm_pack, &level, &idx);
9164
9165 if (level <= levels)
9166 arg_pack = TMPL_ARG (args, level, idx);
9167 }
9168
9169 orig_arg = arg_pack;
9170 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9171 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9172
9173 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9174 /* This can only happen if we forget to expand an argument
9175 pack somewhere else. Just return an error, silently. */
9176 {
9177 result = make_tree_vec (1);
9178 TREE_VEC_ELT (result, 0) = error_mark_node;
9179 return result;
9180 }
9181
9182 if (arg_from_parm_pack_p (arg_pack, parm_pack))
9183 /* The argument pack that the parameter maps to is just an
9184 expansion of the parameter itself, such as one would find
9185 in the implicit typedef of a class inside the class itself.
9186 Consider this parameter "unsubstituted", so that we will
9187 maintain the outer pack expansion. */
9188 arg_pack = NULL_TREE;
9189
9190 if (arg_pack)
9191 {
9192 int my_len =
9193 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9194
9195 /* Don't bother trying to do a partial substitution with
9196 incomplete packs; we'll try again after deduction. */
9197 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9198 return t;
9199
9200 if (len < 0)
9201 len = my_len;
9202 else if (len != my_len)
9203 {
9204 if (!(complain & tf_error))
9205 /* Fail quietly. */;
9206 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9207 error ("mismatched argument pack lengths while expanding "
9208 "%<%T%>",
9209 pattern);
9210 else
9211 error ("mismatched argument pack lengths while expanding "
9212 "%<%E%>",
9213 pattern);
9214 return error_mark_node;
9215 }
9216
9217 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
9218 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack),
9219 0)))
9220 /* This isn't a real argument pack yet. */;
9221 else
9222 real_packs = true;
9223
9224 /* Keep track of the parameter packs and their corresponding
9225 argument packs. */
9226 packs = tree_cons (parm_pack, arg_pack, packs);
9227 TREE_TYPE (packs) = orig_arg;
9228 }
9229 else
9230 {
9231 /* We can't substitute for this parameter pack. We use a flag as
9232 well as the missing_level counter because function parameter
9233 packs don't have a level. */
9234 unsubstituted_packs = true;
9235 if (!missing_level || missing_level > level)
9236 missing_level = level;
9237 }
9238 }
9239
9240 /* We cannot expand this expansion expression, because we don't have
9241 all of the argument packs we need. */
9242 if (unsubstituted_packs)
9243 {
9244 if (real_packs)
9245 {
9246 /* We got some full packs, but we can't substitute them in until we
9247 have values for all the packs. So remember these until then. */
9248 tree save_args;
9249
9250 t = make_pack_expansion (pattern);
9251
9252 /* The call to add_to_template_args above assumes no overlap
9253 between saved args and new args, so prune away any fake
9254 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9255 if (missing_level && levels >= missing_level)
9256 {
9257 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
9258 && missing_level > 1);
9259 TREE_VEC_LENGTH (args) = missing_level - 1;
9260 save_args = copy_node (args);
9261 TREE_VEC_LENGTH (args) = levels;
9262 }
9263 else
9264 save_args = args;
9265
9266 PACK_EXPANSION_EXTRA_ARGS (t) = save_args;
9267 }
9268 else
9269 {
9270 /* There were no real arguments, we're just replacing a parameter
9271 pack with another version of itself. Substitute into the
9272 pattern and return a PACK_EXPANSION_*. The caller will need to
9273 deal with that. */
9274 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9275 t = tsubst_expr (pattern, args, complain, in_decl,
9276 /*integral_constant_expression_p=*/false);
9277 else
9278 t = tsubst (pattern, args, complain, in_decl);
9279 t = make_pack_expansion (t);
9280 }
9281 return t;
9282 }
9283
9284 /* We could not find any argument packs that work. */
9285 if (len < 0)
9286 return error_mark_node;
9287
9288 if (need_local_specializations)
9289 {
9290 /* We're in a late-specified return type, so create our own local
9291 specializations map; the current map is either NULL or (in the
9292 case of recursive unification) might have bindings that we don't
9293 want to use or alter. */
9294 saved_local_specializations = local_specializations;
9295 local_specializations = pointer_map_create ();
9296 }
9297
9298 /* For each argument in each argument pack, substitute into the
9299 pattern. */
9300 result = make_tree_vec (len);
9301 for (i = 0; i < len; ++i)
9302 {
9303 /* For parameter pack, change the substitution of the parameter
9304 pack to the ith argument in its argument pack, then expand
9305 the pattern. */
9306 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9307 {
9308 tree parm = TREE_PURPOSE (pack);
9309 tree arg;
9310
9311 /* Select the Ith argument from the pack. */
9312 if (TREE_CODE (parm) == PARM_DECL)
9313 {
9314 if (i == 0)
9315 {
9316 arg = make_node (ARGUMENT_PACK_SELECT);
9317 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9318 mark_used (parm);
9319 register_local_specialization (arg, parm);
9320 }
9321 else
9322 arg = retrieve_local_specialization (parm);
9323 }
9324 else
9325 {
9326 int idx, level;
9327 template_parm_level_and_index (parm, &level, &idx);
9328
9329 if (i == 0)
9330 {
9331 arg = make_node (ARGUMENT_PACK_SELECT);
9332 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9333 /* Update the corresponding argument. */
9334 TMPL_ARG (args, level, idx) = arg;
9335 }
9336 else
9337 /* Re-use the ARGUMENT_PACK_SELECT. */
9338 arg = TMPL_ARG (args, level, idx);
9339 }
9340 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
9341 }
9342
9343 /* Substitute into the PATTERN with the altered arguments. */
9344 if (!TYPE_P (pattern))
9345 TREE_VEC_ELT (result, i) =
9346 tsubst_expr (pattern, args, complain, in_decl,
9347 /*integral_constant_expression_p=*/false);
9348 else
9349 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
9350
9351 if (TREE_VEC_ELT (result, i) == error_mark_node)
9352 {
9353 result = error_mark_node;
9354 break;
9355 }
9356 }
9357
9358 /* Update ARGS to restore the substitution from parameter packs to
9359 their argument packs. */
9360 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9361 {
9362 tree parm = TREE_PURPOSE (pack);
9363
9364 if (TREE_CODE (parm) == PARM_DECL)
9365 register_local_specialization (TREE_TYPE (pack), parm);
9366 else
9367 {
9368 int idx, level;
9369 template_parm_level_and_index (parm, &level, &idx);
9370
9371 /* Update the corresponding argument. */
9372 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9373 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9374 TREE_TYPE (pack);
9375 else
9376 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9377 }
9378 }
9379
9380 if (need_local_specializations)
9381 {
9382 pointer_map_destroy (local_specializations);
9383 local_specializations = saved_local_specializations;
9384 }
9385
9386 return result;
9387 }
9388
9389 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9390 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9391 parameter packs; all parms generated from a function parameter pack will
9392 have the same DECL_PARM_INDEX. */
9393
9394 tree
9395 get_pattern_parm (tree parm, tree tmpl)
9396 {
9397 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9398 tree patparm;
9399
9400 if (DECL_ARTIFICIAL (parm))
9401 {
9402 for (patparm = DECL_ARGUMENTS (pattern);
9403 patparm; patparm = DECL_CHAIN (patparm))
9404 if (DECL_ARTIFICIAL (patparm)
9405 && DECL_NAME (parm) == DECL_NAME (patparm))
9406 break;
9407 }
9408 else
9409 {
9410 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9411 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9412 gcc_assert (DECL_PARM_INDEX (patparm)
9413 == DECL_PARM_INDEX (parm));
9414 }
9415
9416 return patparm;
9417 }
9418
9419 /* Substitute ARGS into the vector or list of template arguments T. */
9420
9421 static tree
9422 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9423 {
9424 tree orig_t = t;
9425 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9426 tree *elts;
9427
9428 if (t == error_mark_node)
9429 return error_mark_node;
9430
9431 len = TREE_VEC_LENGTH (t);
9432 elts = XALLOCAVEC (tree, len);
9433
9434 for (i = 0; i < len; i++)
9435 {
9436 tree orig_arg = TREE_VEC_ELT (t, i);
9437 tree new_arg;
9438
9439 if (TREE_CODE (orig_arg) == TREE_VEC)
9440 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9441 else if (PACK_EXPANSION_P (orig_arg))
9442 {
9443 /* Substitute into an expansion expression. */
9444 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9445
9446 if (TREE_CODE (new_arg) == TREE_VEC)
9447 /* Add to the expanded length adjustment the number of
9448 expanded arguments. We subtract one from this
9449 measurement, because the argument pack expression
9450 itself is already counted as 1 in
9451 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9452 the argument pack is empty. */
9453 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9454 }
9455 else if (ARGUMENT_PACK_P (orig_arg))
9456 {
9457 /* Substitute into each of the arguments. */
9458 new_arg = TYPE_P (orig_arg)
9459 ? cxx_make_type (TREE_CODE (orig_arg))
9460 : make_node (TREE_CODE (orig_arg));
9461
9462 SET_ARGUMENT_PACK_ARGS (
9463 new_arg,
9464 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9465 args, complain, in_decl));
9466
9467 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9468 new_arg = error_mark_node;
9469
9470 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9471 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9472 complain, in_decl);
9473 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9474
9475 if (TREE_TYPE (new_arg) == error_mark_node)
9476 new_arg = error_mark_node;
9477 }
9478 }
9479 else
9480 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9481
9482 if (new_arg == error_mark_node)
9483 return error_mark_node;
9484
9485 elts[i] = new_arg;
9486 if (new_arg != orig_arg)
9487 need_new = 1;
9488 }
9489
9490 if (!need_new)
9491 return t;
9492
9493 /* Make space for the expanded arguments coming from template
9494 argument packs. */
9495 t = make_tree_vec (len + expanded_len_adjust);
9496 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9497 arguments for a member template.
9498 In that case each TREE_VEC in ORIG_T represents a level of template
9499 arguments, and ORIG_T won't carry any non defaulted argument count.
9500 It will rather be the nested TREE_VECs that will carry one.
9501 In other words, ORIG_T carries a non defaulted argument count only
9502 if it doesn't contain any nested TREE_VEC. */
9503 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9504 {
9505 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9506 count += expanded_len_adjust;
9507 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9508 }
9509 for (i = 0, out = 0; i < len; i++)
9510 {
9511 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9512 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9513 && TREE_CODE (elts[i]) == TREE_VEC)
9514 {
9515 int idx;
9516
9517 /* Now expand the template argument pack "in place". */
9518 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9519 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9520 }
9521 else
9522 {
9523 TREE_VEC_ELT (t, out) = elts[i];
9524 out++;
9525 }
9526 }
9527
9528 return t;
9529 }
9530
9531 /* Return the result of substituting ARGS into the template parameters
9532 given by PARMS. If there are m levels of ARGS and m + n levels of
9533 PARMS, then the result will contain n levels of PARMS. For
9534 example, if PARMS is `template <class T> template <class U>
9535 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9536 result will be `template <int*, double, class V>'. */
9537
9538 static tree
9539 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9540 {
9541 tree r = NULL_TREE;
9542 tree* new_parms;
9543
9544 /* When substituting into a template, we must set
9545 PROCESSING_TEMPLATE_DECL as the template parameters may be
9546 dependent if they are based on one-another, and the dependency
9547 predicates are short-circuit outside of templates. */
9548 ++processing_template_decl;
9549
9550 for (new_parms = &r;
9551 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9552 new_parms = &(TREE_CHAIN (*new_parms)),
9553 parms = TREE_CHAIN (parms))
9554 {
9555 tree new_vec =
9556 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9557 int i;
9558
9559 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9560 {
9561 tree tuple;
9562
9563 if (parms == error_mark_node)
9564 continue;
9565
9566 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9567
9568 if (tuple == error_mark_node)
9569 continue;
9570
9571 TREE_VEC_ELT (new_vec, i) =
9572 tsubst_template_parm (tuple, args, complain);
9573 }
9574
9575 *new_parms =
9576 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9577 - TMPL_ARGS_DEPTH (args)),
9578 new_vec, NULL_TREE);
9579 }
9580
9581 --processing_template_decl;
9582
9583 return r;
9584 }
9585
9586 /* Return the result of substituting ARGS into one template parameter
9587 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9588 parameter and which TREE_PURPOSE is the default argument of the
9589 template parameter. */
9590
9591 static tree
9592 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9593 {
9594 tree default_value, parm_decl;
9595
9596 if (args == NULL_TREE
9597 || t == NULL_TREE
9598 || t == error_mark_node)
9599 return t;
9600
9601 gcc_assert (TREE_CODE (t) == TREE_LIST);
9602
9603 default_value = TREE_PURPOSE (t);
9604 parm_decl = TREE_VALUE (t);
9605
9606 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9607 if (TREE_CODE (parm_decl) == PARM_DECL
9608 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9609 parm_decl = error_mark_node;
9610 default_value = tsubst_template_arg (default_value, args,
9611 complain, NULL_TREE);
9612
9613 return build_tree_list (default_value, parm_decl);
9614 }
9615
9616 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9617 type T. If T is not an aggregate or enumeration type, it is
9618 handled as if by tsubst. IN_DECL is as for tsubst. If
9619 ENTERING_SCOPE is nonzero, T is the context for a template which
9620 we are presently tsubst'ing. Return the substituted value. */
9621
9622 static tree
9623 tsubst_aggr_type (tree t,
9624 tree args,
9625 tsubst_flags_t complain,
9626 tree in_decl,
9627 int entering_scope)
9628 {
9629 if (t == NULL_TREE)
9630 return NULL_TREE;
9631
9632 switch (TREE_CODE (t))
9633 {
9634 case RECORD_TYPE:
9635 if (TYPE_PTRMEMFUNC_P (t))
9636 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9637
9638 /* Else fall through. */
9639 case ENUMERAL_TYPE:
9640 case UNION_TYPE:
9641 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9642 {
9643 tree argvec;
9644 tree context;
9645 tree r;
9646 int saved_unevaluated_operand;
9647 int saved_inhibit_evaluation_warnings;
9648
9649 /* In "sizeof(X<I>)" we need to evaluate "I". */
9650 saved_unevaluated_operand = cp_unevaluated_operand;
9651 cp_unevaluated_operand = 0;
9652 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9653 c_inhibit_evaluation_warnings = 0;
9654
9655 /* First, determine the context for the type we are looking
9656 up. */
9657 context = TYPE_CONTEXT (t);
9658 if (context && TYPE_P (context))
9659 {
9660 context = tsubst_aggr_type (context, args, complain,
9661 in_decl, /*entering_scope=*/1);
9662 /* If context is a nested class inside a class template,
9663 it may still need to be instantiated (c++/33959). */
9664 context = complete_type (context);
9665 }
9666
9667 /* Then, figure out what arguments are appropriate for the
9668 type we are trying to find. For example, given:
9669
9670 template <class T> struct S;
9671 template <class T, class U> void f(T, U) { S<U> su; }
9672
9673 and supposing that we are instantiating f<int, double>,
9674 then our ARGS will be {int, double}, but, when looking up
9675 S we only want {double}. */
9676 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9677 complain, in_decl);
9678 if (argvec == error_mark_node)
9679 r = error_mark_node;
9680 else
9681 {
9682 r = lookup_template_class (t, argvec, in_decl, context,
9683 entering_scope, complain);
9684 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9685 }
9686
9687 cp_unevaluated_operand = saved_unevaluated_operand;
9688 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9689
9690 return r;
9691 }
9692 else
9693 /* This is not a template type, so there's nothing to do. */
9694 return t;
9695
9696 default:
9697 return tsubst (t, args, complain, in_decl);
9698 }
9699 }
9700
9701 /* Substitute into the default argument ARG (a default argument for
9702 FN), which has the indicated TYPE. */
9703
9704 tree
9705 tsubst_default_argument (tree fn, tree type, tree arg)
9706 {
9707 tree saved_class_ptr = NULL_TREE;
9708 tree saved_class_ref = NULL_TREE;
9709 int errs = errorcount + sorrycount;
9710
9711 /* This can happen in invalid code. */
9712 if (TREE_CODE (arg) == DEFAULT_ARG)
9713 return arg;
9714
9715 /* This default argument came from a template. Instantiate the
9716 default argument here, not in tsubst. In the case of
9717 something like:
9718
9719 template <class T>
9720 struct S {
9721 static T t();
9722 void f(T = t());
9723 };
9724
9725 we must be careful to do name lookup in the scope of S<T>,
9726 rather than in the current class. */
9727 push_access_scope (fn);
9728 /* The "this" pointer is not valid in a default argument. */
9729 if (cfun)
9730 {
9731 saved_class_ptr = current_class_ptr;
9732 cp_function_chain->x_current_class_ptr = NULL_TREE;
9733 saved_class_ref = current_class_ref;
9734 cp_function_chain->x_current_class_ref = NULL_TREE;
9735 }
9736
9737 push_deferring_access_checks(dk_no_deferred);
9738 /* The default argument expression may cause implicitly defined
9739 member functions to be synthesized, which will result in garbage
9740 collection. We must treat this situation as if we were within
9741 the body of function so as to avoid collecting live data on the
9742 stack. */
9743 ++function_depth;
9744 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9745 tf_warning_or_error, NULL_TREE,
9746 /*integral_constant_expression_p=*/false);
9747 --function_depth;
9748 pop_deferring_access_checks();
9749
9750 /* Restore the "this" pointer. */
9751 if (cfun)
9752 {
9753 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9754 cp_function_chain->x_current_class_ref = saved_class_ref;
9755 }
9756
9757 if (errorcount+sorrycount > errs)
9758 inform (input_location,
9759 " when instantiating default argument for call to %D", fn);
9760
9761 /* Make sure the default argument is reasonable. */
9762 arg = check_default_argument (type, arg);
9763
9764 pop_access_scope (fn);
9765
9766 return arg;
9767 }
9768
9769 /* Substitute into all the default arguments for FN. */
9770
9771 static void
9772 tsubst_default_arguments (tree fn)
9773 {
9774 tree arg;
9775 tree tmpl_args;
9776
9777 tmpl_args = DECL_TI_ARGS (fn);
9778
9779 /* If this function is not yet instantiated, we certainly don't need
9780 its default arguments. */
9781 if (uses_template_parms (tmpl_args))
9782 return;
9783 /* Don't do this again for clones. */
9784 if (DECL_CLONED_FUNCTION_P (fn))
9785 return;
9786
9787 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9788 arg;
9789 arg = TREE_CHAIN (arg))
9790 if (TREE_PURPOSE (arg))
9791 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9792 TREE_VALUE (arg),
9793 TREE_PURPOSE (arg));
9794 }
9795
9796 /* Substitute the ARGS into the T, which is a _DECL. Return the
9797 result of the substitution. Issue error and warning messages under
9798 control of COMPLAIN. */
9799
9800 static tree
9801 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9802 {
9803 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9804 location_t saved_loc;
9805 tree r = NULL_TREE;
9806 tree in_decl = t;
9807 hashval_t hash = 0;
9808
9809 /* Set the filename and linenumber to improve error-reporting. */
9810 saved_loc = input_location;
9811 input_location = DECL_SOURCE_LOCATION (t);
9812
9813 switch (TREE_CODE (t))
9814 {
9815 case TEMPLATE_DECL:
9816 {
9817 /* We can get here when processing a member function template,
9818 member class template, or template template parameter. */
9819 tree decl = DECL_TEMPLATE_RESULT (t);
9820 tree spec;
9821 tree tmpl_args;
9822 tree full_args;
9823
9824 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9825 {
9826 /* Template template parameter is treated here. */
9827 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9828 if (new_type == error_mark_node)
9829 RETURN (error_mark_node);
9830 /* If we get a real template back, return it. This can happen in
9831 the context of most_specialized_class. */
9832 if (TREE_CODE (new_type) == TEMPLATE_DECL)
9833 return new_type;
9834
9835 r = copy_decl (t);
9836 DECL_CHAIN (r) = NULL_TREE;
9837 TREE_TYPE (r) = new_type;
9838 DECL_TEMPLATE_RESULT (r)
9839 = build_decl (DECL_SOURCE_LOCATION (decl),
9840 TYPE_DECL, DECL_NAME (decl), new_type);
9841 DECL_TEMPLATE_PARMS (r)
9842 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9843 complain);
9844 TYPE_NAME (new_type) = r;
9845 break;
9846 }
9847
9848 /* We might already have an instance of this template.
9849 The ARGS are for the surrounding class type, so the
9850 full args contain the tsubst'd args for the context,
9851 plus the innermost args from the template decl. */
9852 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9853 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9854 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9855 /* Because this is a template, the arguments will still be
9856 dependent, even after substitution. If
9857 PROCESSING_TEMPLATE_DECL is not set, the dependency
9858 predicates will short-circuit. */
9859 ++processing_template_decl;
9860 full_args = tsubst_template_args (tmpl_args, args,
9861 complain, in_decl);
9862 --processing_template_decl;
9863 if (full_args == error_mark_node)
9864 RETURN (error_mark_node);
9865
9866 /* If this is a default template template argument,
9867 tsubst might not have changed anything. */
9868 if (full_args == tmpl_args)
9869 RETURN (t);
9870
9871 hash = hash_tmpl_and_args (t, full_args);
9872 spec = retrieve_specialization (t, full_args, hash);
9873 if (spec != NULL_TREE)
9874 {
9875 r = spec;
9876 break;
9877 }
9878
9879 /* Make a new template decl. It will be similar to the
9880 original, but will record the current template arguments.
9881 We also create a new function declaration, which is just
9882 like the old one, but points to this new template, rather
9883 than the old one. */
9884 r = copy_decl (t);
9885 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9886 DECL_CHAIN (r) = NULL_TREE;
9887
9888 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9889
9890 if (TREE_CODE (decl) == TYPE_DECL
9891 && !TYPE_DECL_ALIAS_P (decl))
9892 {
9893 tree new_type;
9894 ++processing_template_decl;
9895 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9896 --processing_template_decl;
9897 if (new_type == error_mark_node)
9898 RETURN (error_mark_node);
9899
9900 TREE_TYPE (r) = new_type;
9901 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9902 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9903 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9904 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9905 }
9906 else
9907 {
9908 tree new_decl;
9909 ++processing_template_decl;
9910 new_decl = tsubst (decl, args, complain, in_decl);
9911 --processing_template_decl;
9912 if (new_decl == error_mark_node)
9913 RETURN (error_mark_node);
9914
9915 DECL_TEMPLATE_RESULT (r) = new_decl;
9916 DECL_TI_TEMPLATE (new_decl) = r;
9917 TREE_TYPE (r) = TREE_TYPE (new_decl);
9918 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9919 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9920 }
9921
9922 SET_DECL_IMPLICIT_INSTANTIATION (r);
9923 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9924 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9925
9926 /* The template parameters for this new template are all the
9927 template parameters for the old template, except the
9928 outermost level of parameters. */
9929 DECL_TEMPLATE_PARMS (r)
9930 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9931 complain);
9932
9933 if (PRIMARY_TEMPLATE_P (t))
9934 DECL_PRIMARY_TEMPLATE (r) = r;
9935
9936 if (TREE_CODE (decl) != TYPE_DECL)
9937 /* Record this non-type partial instantiation. */
9938 register_specialization (r, t,
9939 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9940 false, hash);
9941 }
9942 break;
9943
9944 case FUNCTION_DECL:
9945 {
9946 tree ctx;
9947 tree argvec = NULL_TREE;
9948 tree *friends;
9949 tree gen_tmpl;
9950 tree type;
9951 int member;
9952 int args_depth;
9953 int parms_depth;
9954
9955 /* Nobody should be tsubst'ing into non-template functions. */
9956 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9957
9958 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9959 {
9960 tree spec;
9961 bool dependent_p;
9962
9963 /* If T is not dependent, just return it. We have to
9964 increment PROCESSING_TEMPLATE_DECL because
9965 value_dependent_expression_p assumes that nothing is
9966 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9967 ++processing_template_decl;
9968 dependent_p = value_dependent_expression_p (t);
9969 --processing_template_decl;
9970 if (!dependent_p)
9971 RETURN (t);
9972
9973 /* Calculate the most general template of which R is a
9974 specialization, and the complete set of arguments used to
9975 specialize R. */
9976 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9977 argvec = tsubst_template_args (DECL_TI_ARGS
9978 (DECL_TEMPLATE_RESULT
9979 (DECL_TI_TEMPLATE (t))),
9980 args, complain, in_decl);
9981 if (argvec == error_mark_node)
9982 RETURN (error_mark_node);
9983
9984 /* Check to see if we already have this specialization. */
9985 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9986 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9987
9988 if (spec)
9989 {
9990 r = spec;
9991 break;
9992 }
9993
9994 /* We can see more levels of arguments than parameters if
9995 there was a specialization of a member template, like
9996 this:
9997
9998 template <class T> struct S { template <class U> void f(); }
9999 template <> template <class U> void S<int>::f(U);
10000
10001 Here, we'll be substituting into the specialization,
10002 because that's where we can find the code we actually
10003 want to generate, but we'll have enough arguments for
10004 the most general template.
10005
10006 We also deal with the peculiar case:
10007
10008 template <class T> struct S {
10009 template <class U> friend void f();
10010 };
10011 template <class U> void f() {}
10012 template S<int>;
10013 template void f<double>();
10014
10015 Here, the ARGS for the instantiation of will be {int,
10016 double}. But, we only need as many ARGS as there are
10017 levels of template parameters in CODE_PATTERN. We are
10018 careful not to get fooled into reducing the ARGS in
10019 situations like:
10020
10021 template <class T> struct S { template <class U> void f(U); }
10022 template <class T> template <> void S<T>::f(int) {}
10023
10024 which we can spot because the pattern will be a
10025 specialization in this case. */
10026 args_depth = TMPL_ARGS_DEPTH (args);
10027 parms_depth =
10028 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
10029 if (args_depth > parms_depth
10030 && !DECL_TEMPLATE_SPECIALIZATION (t))
10031 args = get_innermost_template_args (args, parms_depth);
10032 }
10033 else
10034 {
10035 /* This special case arises when we have something like this:
10036
10037 template <class T> struct S {
10038 friend void f<int>(int, double);
10039 };
10040
10041 Here, the DECL_TI_TEMPLATE for the friend declaration
10042 will be an IDENTIFIER_NODE. We are being called from
10043 tsubst_friend_function, and we want only to create a
10044 new decl (R) with appropriate types so that we can call
10045 determine_specialization. */
10046 gen_tmpl = NULL_TREE;
10047 }
10048
10049 if (DECL_CLASS_SCOPE_P (t))
10050 {
10051 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
10052 member = 2;
10053 else
10054 member = 1;
10055 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
10056 complain, t, /*entering_scope=*/1);
10057 }
10058 else
10059 {
10060 member = 0;
10061 ctx = DECL_CONTEXT (t);
10062 }
10063 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10064 if (type == error_mark_node)
10065 RETURN (error_mark_node);
10066
10067 /* If we hit excessive deduction depth, the type is bogus even if
10068 it isn't error_mark_node, so don't build a decl. */
10069 if (excessive_deduction_depth)
10070 RETURN (error_mark_node);
10071
10072 /* We do NOT check for matching decls pushed separately at this
10073 point, as they may not represent instantiations of this
10074 template, and in any case are considered separate under the
10075 discrete model. */
10076 r = copy_decl (t);
10077 DECL_USE_TEMPLATE (r) = 0;
10078 TREE_TYPE (r) = type;
10079 /* Clear out the mangled name and RTL for the instantiation. */
10080 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10081 SET_DECL_RTL (r, NULL);
10082 /* Leave DECL_INITIAL set on deleted instantiations. */
10083 if (!DECL_DELETED_FN (r))
10084 DECL_INITIAL (r) = NULL_TREE;
10085 DECL_CONTEXT (r) = ctx;
10086
10087 if (member && DECL_CONV_FN_P (r))
10088 /* Type-conversion operator. Reconstruct the name, in
10089 case it's the name of one of the template's parameters. */
10090 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10091
10092 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10093 complain, t);
10094 DECL_RESULT (r) = NULL_TREE;
10095
10096 TREE_STATIC (r) = 0;
10097 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10098 DECL_EXTERNAL (r) = 1;
10099 /* If this is an instantiation of a function with internal
10100 linkage, we already know what object file linkage will be
10101 assigned to the instantiation. */
10102 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10103 DECL_DEFER_OUTPUT (r) = 0;
10104 DECL_CHAIN (r) = NULL_TREE;
10105 DECL_PENDING_INLINE_INFO (r) = 0;
10106 DECL_PENDING_INLINE_P (r) = 0;
10107 DECL_SAVED_TREE (r) = NULL_TREE;
10108 DECL_STRUCT_FUNCTION (r) = NULL;
10109 TREE_USED (r) = 0;
10110 /* We'll re-clone as appropriate in instantiate_template. */
10111 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10112
10113 /* If we aren't complaining now, return on error before we register
10114 the specialization so that we'll complain eventually. */
10115 if ((complain & tf_error) == 0
10116 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10117 && !grok_op_properties (r, /*complain=*/false))
10118 RETURN (error_mark_node);
10119
10120 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10121 this in the special friend case mentioned above where
10122 GEN_TMPL is NULL. */
10123 if (gen_tmpl)
10124 {
10125 DECL_TEMPLATE_INFO (r)
10126 = build_template_info (gen_tmpl, argvec);
10127 SET_DECL_IMPLICIT_INSTANTIATION (r);
10128 register_specialization (r, gen_tmpl, argvec, false, hash);
10129
10130 /* We're not supposed to instantiate default arguments
10131 until they are called, for a template. But, for a
10132 declaration like:
10133
10134 template <class T> void f ()
10135 { extern void g(int i = T()); }
10136
10137 we should do the substitution when the template is
10138 instantiated. We handle the member function case in
10139 instantiate_class_template since the default arguments
10140 might refer to other members of the class. */
10141 if (!member
10142 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10143 && !uses_template_parms (argvec))
10144 tsubst_default_arguments (r);
10145 }
10146 else
10147 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10148
10149 /* Copy the list of befriending classes. */
10150 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10151 *friends;
10152 friends = &TREE_CHAIN (*friends))
10153 {
10154 *friends = copy_node (*friends);
10155 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10156 args, complain,
10157 in_decl);
10158 }
10159
10160 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10161 {
10162 maybe_retrofit_in_chrg (r);
10163 if (DECL_CONSTRUCTOR_P (r))
10164 grok_ctor_properties (ctx, r);
10165 if (DECL_INHERITED_CTOR_BASE (r))
10166 deduce_inheriting_ctor (r);
10167 /* If this is an instantiation of a member template, clone it.
10168 If it isn't, that'll be handled by
10169 clone_constructors_and_destructors. */
10170 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10171 clone_function_decl (r, /*update_method_vec_p=*/0);
10172 }
10173 else if ((complain & tf_error) != 0
10174 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10175 && !grok_op_properties (r, /*complain=*/true))
10176 RETURN (error_mark_node);
10177
10178 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10179 SET_DECL_FRIEND_CONTEXT (r,
10180 tsubst (DECL_FRIEND_CONTEXT (t),
10181 args, complain, in_decl));
10182
10183 /* Possibly limit visibility based on template args. */
10184 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10185 if (DECL_VISIBILITY_SPECIFIED (t))
10186 {
10187 DECL_VISIBILITY_SPECIFIED (r) = 0;
10188 DECL_ATTRIBUTES (r)
10189 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10190 }
10191 determine_visibility (r);
10192 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10193 && !processing_template_decl)
10194 defaulted_late_check (r);
10195
10196 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10197 args, complain, in_decl);
10198 }
10199 break;
10200
10201 case PARM_DECL:
10202 {
10203 tree type = NULL_TREE;
10204 int i, len = 1;
10205 tree expanded_types = NULL_TREE;
10206 tree prev_r = NULL_TREE;
10207 tree first_r = NULL_TREE;
10208
10209 if (FUNCTION_PARAMETER_PACK_P (t))
10210 {
10211 /* If there is a local specialization that isn't a
10212 parameter pack, it means that we're doing a "simple"
10213 substitution from inside tsubst_pack_expansion. Just
10214 return the local specialization (which will be a single
10215 parm). */
10216 tree spec = retrieve_local_specialization (t);
10217 if (spec
10218 && TREE_CODE (spec) == PARM_DECL
10219 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10220 RETURN (spec);
10221
10222 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10223 the parameters in this function parameter pack. */
10224 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10225 complain, in_decl);
10226 if (TREE_CODE (expanded_types) == TREE_VEC)
10227 {
10228 len = TREE_VEC_LENGTH (expanded_types);
10229
10230 /* Zero-length parameter packs are boring. Just substitute
10231 into the chain. */
10232 if (len == 0)
10233 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10234 TREE_CHAIN (t)));
10235 }
10236 else
10237 {
10238 /* All we did was update the type. Make a note of that. */
10239 type = expanded_types;
10240 expanded_types = NULL_TREE;
10241 }
10242 }
10243
10244 /* Loop through all of the parameter's we'll build. When T is
10245 a function parameter pack, LEN is the number of expanded
10246 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10247 r = NULL_TREE;
10248 for (i = 0; i < len; ++i)
10249 {
10250 prev_r = r;
10251 r = copy_node (t);
10252 if (DECL_TEMPLATE_PARM_P (t))
10253 SET_DECL_TEMPLATE_PARM_P (r);
10254
10255 if (expanded_types)
10256 /* We're on the Ith parameter of the function parameter
10257 pack. */
10258 {
10259 /* An argument of a function parameter pack is not a parameter
10260 pack. */
10261 FUNCTION_PARAMETER_PACK_P (r) = false;
10262
10263 /* Get the Ith type. */
10264 type = TREE_VEC_ELT (expanded_types, i);
10265
10266 /* Rename the parameter to include the index. */
10267 DECL_NAME (r)
10268 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10269 }
10270 else if (!type)
10271 /* We're dealing with a normal parameter. */
10272 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10273
10274 type = type_decays_to (type);
10275 TREE_TYPE (r) = type;
10276 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10277
10278 if (DECL_INITIAL (r))
10279 {
10280 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10281 DECL_INITIAL (r) = TREE_TYPE (r);
10282 else
10283 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10284 complain, in_decl);
10285 }
10286
10287 DECL_CONTEXT (r) = NULL_TREE;
10288
10289 if (!DECL_TEMPLATE_PARM_P (r))
10290 DECL_ARG_TYPE (r) = type_passed_as (type);
10291
10292 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10293 args, complain, in_decl);
10294
10295 /* Keep track of the first new parameter we
10296 generate. That's what will be returned to the
10297 caller. */
10298 if (!first_r)
10299 first_r = r;
10300
10301 /* Build a proper chain of parameters when substituting
10302 into a function parameter pack. */
10303 if (prev_r)
10304 DECL_CHAIN (prev_r) = r;
10305 }
10306
10307 /* If cp_unevaluated_operand is set, we're just looking for a
10308 single dummy parameter, so don't keep going. */
10309 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
10310 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10311 complain, DECL_CHAIN (t));
10312
10313 /* FIRST_R contains the start of the chain we've built. */
10314 r = first_r;
10315 }
10316 break;
10317
10318 case FIELD_DECL:
10319 {
10320 tree type;
10321
10322 r = copy_decl (t);
10323 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10324 if (type == error_mark_node)
10325 RETURN (error_mark_node);
10326 TREE_TYPE (r) = type;
10327 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10328
10329 if (DECL_C_BIT_FIELD (r))
10330 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10331 non-bit-fields DECL_INITIAL is a non-static data member
10332 initializer, which gets deferred instantiation. */
10333 DECL_INITIAL (r)
10334 = tsubst_expr (DECL_INITIAL (t), args,
10335 complain, in_decl,
10336 /*integral_constant_expression_p=*/true);
10337 else if (DECL_INITIAL (t))
10338 {
10339 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10340 NSDMI in perform_member_init. Still set DECL_INITIAL
10341 so that we know there is one. */
10342 DECL_INITIAL (r) = void_zero_node;
10343 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10344 retrofit_lang_decl (r);
10345 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10346 }
10347 /* We don't have to set DECL_CONTEXT here; it is set by
10348 finish_member_declaration. */
10349 DECL_CHAIN (r) = NULL_TREE;
10350 if (VOID_TYPE_P (type))
10351 error ("instantiation of %q+D as type %qT", r, type);
10352
10353 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10354 args, complain, in_decl);
10355 }
10356 break;
10357
10358 case USING_DECL:
10359 /* We reach here only for member using decls. We also need to check
10360 uses_template_parms because DECL_DEPENDENT_P is not set for a
10361 using-declaration that designates a member of the current
10362 instantiation (c++/53549). */
10363 if (DECL_DEPENDENT_P (t)
10364 || uses_template_parms (USING_DECL_SCOPE (t)))
10365 {
10366 tree scope = USING_DECL_SCOPE (t);
10367 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
10368 complain, in_decl);
10369 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
10370 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM
10371 && name == TYPE_IDENTIFIER (scope))
10372 name = TYPE_IDENTIFIER (inst_scope);
10373 r = do_class_using_decl (inst_scope, name);
10374 if (!r)
10375 r = error_mark_node;
10376 else
10377 {
10378 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10379 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10380 }
10381 }
10382 else
10383 {
10384 r = copy_node (t);
10385 DECL_CHAIN (r) = NULL_TREE;
10386 }
10387 break;
10388
10389 case TYPE_DECL:
10390 case VAR_DECL:
10391 {
10392 tree argvec = NULL_TREE;
10393 tree gen_tmpl = NULL_TREE;
10394 tree spec;
10395 tree tmpl = NULL_TREE;
10396 tree ctx;
10397 tree type = NULL_TREE;
10398 bool local_p;
10399
10400 if (TREE_CODE (t) == TYPE_DECL
10401 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10402 {
10403 /* If this is the canonical decl, we don't have to
10404 mess with instantiations, and often we can't (for
10405 typename, template type parms and such). Note that
10406 TYPE_NAME is not correct for the above test if
10407 we've copied the type for a typedef. */
10408 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10409 if (type == error_mark_node)
10410 RETURN (error_mark_node);
10411 r = TYPE_NAME (type);
10412 break;
10413 }
10414
10415 /* Check to see if we already have the specialization we
10416 need. */
10417 spec = NULL_TREE;
10418 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10419 {
10420 /* T is a static data member or namespace-scope entity.
10421 We have to substitute into namespace-scope variables
10422 (even though such entities are never templates) because
10423 of cases like:
10424
10425 template <class T> void f() { extern T t; }
10426
10427 where the entity referenced is not known until
10428 instantiation time. */
10429 local_p = false;
10430 ctx = DECL_CONTEXT (t);
10431 if (DECL_CLASS_SCOPE_P (t))
10432 {
10433 ctx = tsubst_aggr_type (ctx, args,
10434 complain,
10435 in_decl, /*entering_scope=*/1);
10436 /* If CTX is unchanged, then T is in fact the
10437 specialization we want. That situation occurs when
10438 referencing a static data member within in its own
10439 class. We can use pointer equality, rather than
10440 same_type_p, because DECL_CONTEXT is always
10441 canonical... */
10442 if (ctx == DECL_CONTEXT (t)
10443 && (TREE_CODE (t) != TYPE_DECL
10444 /* ... unless T is a member template; in which
10445 case our caller can be willing to create a
10446 specialization of that template represented
10447 by T. */
10448 || !(DECL_TI_TEMPLATE (t)
10449 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10450 spec = t;
10451 }
10452
10453 if (!spec)
10454 {
10455 tmpl = DECL_TI_TEMPLATE (t);
10456 gen_tmpl = most_general_template (tmpl);
10457 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10458 if (argvec == error_mark_node)
10459 RETURN (error_mark_node);
10460 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10461 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10462 }
10463 }
10464 else
10465 {
10466 /* A local variable. */
10467 local_p = true;
10468 /* Subsequent calls to pushdecl will fill this in. */
10469 ctx = NULL_TREE;
10470 spec = retrieve_local_specialization (t);
10471 }
10472 /* If we already have the specialization we need, there is
10473 nothing more to do. */
10474 if (spec)
10475 {
10476 r = spec;
10477 break;
10478 }
10479
10480 if (TREE_CODE (t) == VAR_DECL && DECL_ANON_UNION_VAR_P (t))
10481 {
10482 /* Just use name lookup to find a member alias for an anonymous
10483 union, but then add it to the hash table. */
10484 r = lookup_name (DECL_NAME (t));
10485 gcc_assert (DECL_ANON_UNION_VAR_P (r));
10486 register_local_specialization (r, t);
10487 break;
10488 }
10489
10490 /* Create a new node for the specialization we need. */
10491 r = copy_decl (t);
10492 if (type == NULL_TREE)
10493 {
10494 if (is_typedef_decl (t))
10495 type = DECL_ORIGINAL_TYPE (t);
10496 else
10497 type = TREE_TYPE (t);
10498 if (TREE_CODE (t) == VAR_DECL
10499 && VAR_HAD_UNKNOWN_BOUND (t)
10500 && type != error_mark_node)
10501 type = strip_array_domain (type);
10502 type = tsubst (type, args, complain, in_decl);
10503 }
10504 if (TREE_CODE (r) == VAR_DECL)
10505 {
10506 /* Even if the original location is out of scope, the
10507 newly substituted one is not. */
10508 DECL_DEAD_FOR_LOCAL (r) = 0;
10509 DECL_INITIALIZED_P (r) = 0;
10510 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10511 if (type == error_mark_node)
10512 RETURN (error_mark_node);
10513 if (TREE_CODE (type) == FUNCTION_TYPE)
10514 {
10515 /* It may seem that this case cannot occur, since:
10516
10517 typedef void f();
10518 void g() { f x; }
10519
10520 declares a function, not a variable. However:
10521
10522 typedef void f();
10523 template <typename T> void g() { T t; }
10524 template void g<f>();
10525
10526 is an attempt to declare a variable with function
10527 type. */
10528 error ("variable %qD has function type",
10529 /* R is not yet sufficiently initialized, so we
10530 just use its name. */
10531 DECL_NAME (r));
10532 RETURN (error_mark_node);
10533 }
10534 type = complete_type (type);
10535 /* Wait until cp_finish_decl to set this again, to handle
10536 circular dependency (template/instantiate6.C). */
10537 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10538 type = check_var_type (DECL_NAME (r), type);
10539
10540 if (DECL_HAS_VALUE_EXPR_P (t))
10541 {
10542 tree ve = DECL_VALUE_EXPR (t);
10543 ve = tsubst_expr (ve, args, complain, in_decl,
10544 /*constant_expression_p=*/false);
10545 if (REFERENCE_REF_P (ve))
10546 {
10547 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10548 ve = TREE_OPERAND (ve, 0);
10549 }
10550 SET_DECL_VALUE_EXPR (r, ve);
10551 }
10552 }
10553 else if (DECL_SELF_REFERENCE_P (t))
10554 SET_DECL_SELF_REFERENCE_P (r);
10555 TREE_TYPE (r) = type;
10556 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10557 DECL_CONTEXT (r) = ctx;
10558 /* Clear out the mangled name and RTL for the instantiation. */
10559 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10560 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10561 SET_DECL_RTL (r, NULL);
10562 /* The initializer must not be expanded until it is required;
10563 see [temp.inst]. */
10564 DECL_INITIAL (r) = NULL_TREE;
10565 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10566 SET_DECL_RTL (r, NULL);
10567 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10568 if (TREE_CODE (r) == VAR_DECL)
10569 {
10570 /* Possibly limit visibility based on template args. */
10571 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10572 if (DECL_VISIBILITY_SPECIFIED (t))
10573 {
10574 DECL_VISIBILITY_SPECIFIED (r) = 0;
10575 DECL_ATTRIBUTES (r)
10576 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10577 }
10578 determine_visibility (r);
10579 }
10580
10581 if (!local_p)
10582 {
10583 /* A static data member declaration is always marked
10584 external when it is declared in-class, even if an
10585 initializer is present. We mimic the non-template
10586 processing here. */
10587 DECL_EXTERNAL (r) = 1;
10588
10589 register_specialization (r, gen_tmpl, argvec, false, hash);
10590 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10591 SET_DECL_IMPLICIT_INSTANTIATION (r);
10592 }
10593 else if (cp_unevaluated_operand)
10594 {
10595 /* We're substituting this var in a decltype outside of its
10596 scope, such as for a lambda return type. Don't add it to
10597 local_specializations, do perform auto deduction. */
10598 tree auto_node = type_uses_auto (type);
10599 if (auto_node)
10600 {
10601 tree init
10602 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10603 /*constant_expression_p=*/false);
10604 init = resolve_nondeduced_context (init);
10605 TREE_TYPE (r) = type
10606 = do_auto_deduction (type, init, auto_node);
10607 }
10608 }
10609 else
10610 register_local_specialization (r, t);
10611
10612 DECL_CHAIN (r) = NULL_TREE;
10613
10614 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10615 /*flags=*/0,
10616 args, complain, in_decl);
10617
10618 /* Preserve a typedef that names a type. */
10619 if (is_typedef_decl (r))
10620 {
10621 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10622 set_underlying_type (r);
10623 }
10624
10625 layout_decl (r, 0);
10626 }
10627 break;
10628
10629 default:
10630 gcc_unreachable ();
10631 }
10632 #undef RETURN
10633
10634 out:
10635 /* Restore the file and line information. */
10636 input_location = saved_loc;
10637
10638 return r;
10639 }
10640
10641 /* Substitute into the ARG_TYPES of a function type.
10642 If END is a TREE_CHAIN, leave it and any following types
10643 un-substituted. */
10644
10645 static tree
10646 tsubst_arg_types (tree arg_types,
10647 tree args,
10648 tree end,
10649 tsubst_flags_t complain,
10650 tree in_decl)
10651 {
10652 tree remaining_arg_types;
10653 tree type = NULL_TREE;
10654 int i = 1;
10655 tree expanded_args = NULL_TREE;
10656 tree default_arg;
10657
10658 if (!arg_types || arg_types == void_list_node || arg_types == end)
10659 return arg_types;
10660
10661 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10662 args, end, complain, in_decl);
10663 if (remaining_arg_types == error_mark_node)
10664 return error_mark_node;
10665
10666 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10667 {
10668 /* For a pack expansion, perform substitution on the
10669 entire expression. Later on, we'll handle the arguments
10670 one-by-one. */
10671 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10672 args, complain, in_decl);
10673
10674 if (TREE_CODE (expanded_args) == TREE_VEC)
10675 /* So that we'll spin through the parameters, one by one. */
10676 i = TREE_VEC_LENGTH (expanded_args);
10677 else
10678 {
10679 /* We only partially substituted into the parameter
10680 pack. Our type is TYPE_PACK_EXPANSION. */
10681 type = expanded_args;
10682 expanded_args = NULL_TREE;
10683 }
10684 }
10685
10686 while (i > 0) {
10687 --i;
10688
10689 if (expanded_args)
10690 type = TREE_VEC_ELT (expanded_args, i);
10691 else if (!type)
10692 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10693
10694 if (type == error_mark_node)
10695 return error_mark_node;
10696 if (VOID_TYPE_P (type))
10697 {
10698 if (complain & tf_error)
10699 {
10700 error ("invalid parameter type %qT", type);
10701 if (in_decl)
10702 error ("in declaration %q+D", in_decl);
10703 }
10704 return error_mark_node;
10705 }
10706
10707 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10708 top-level qualifiers as required. */
10709 type = cv_unqualified (type_decays_to (type));
10710
10711 /* We do not substitute into default arguments here. The standard
10712 mandates that they be instantiated only when needed, which is
10713 done in build_over_call. */
10714 default_arg = TREE_PURPOSE (arg_types);
10715
10716 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10717 {
10718 /* We've instantiated a template before its default arguments
10719 have been parsed. This can happen for a nested template
10720 class, and is not an error unless we require the default
10721 argument in a call of this function. */
10722 remaining_arg_types =
10723 tree_cons (default_arg, type, remaining_arg_types);
10724 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
10725 }
10726 else
10727 remaining_arg_types =
10728 hash_tree_cons (default_arg, type, remaining_arg_types);
10729 }
10730
10731 return remaining_arg_types;
10732 }
10733
10734 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10735 *not* handle the exception-specification for FNTYPE, because the
10736 initial substitution of explicitly provided template parameters
10737 during argument deduction forbids substitution into the
10738 exception-specification:
10739
10740 [temp.deduct]
10741
10742 All references in the function type of the function template to the
10743 corresponding template parameters are replaced by the specified tem-
10744 plate argument values. If a substitution in a template parameter or
10745 in the function type of the function template results in an invalid
10746 type, type deduction fails. [Note: The equivalent substitution in
10747 exception specifications is done only when the function is instanti-
10748 ated, at which point a program is ill-formed if the substitution
10749 results in an invalid type.] */
10750
10751 static tree
10752 tsubst_function_type (tree t,
10753 tree args,
10754 tsubst_flags_t complain,
10755 tree in_decl)
10756 {
10757 tree return_type;
10758 tree arg_types;
10759 tree fntype;
10760
10761 /* The TYPE_CONTEXT is not used for function/method types. */
10762 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10763
10764 /* Substitute the return type. */
10765 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10766 if (return_type == error_mark_node)
10767 return error_mark_node;
10768 /* The standard does not presently indicate that creation of a
10769 function type with an invalid return type is a deduction failure.
10770 However, that is clearly analogous to creating an array of "void"
10771 or a reference to a reference. This is core issue #486. */
10772 if (TREE_CODE (return_type) == ARRAY_TYPE
10773 || TREE_CODE (return_type) == FUNCTION_TYPE)
10774 {
10775 if (complain & tf_error)
10776 {
10777 if (TREE_CODE (return_type) == ARRAY_TYPE)
10778 error ("function returning an array");
10779 else
10780 error ("function returning a function");
10781 }
10782 return error_mark_node;
10783 }
10784
10785 /* Substitute the argument types. */
10786 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
10787 complain, in_decl);
10788 if (arg_types == error_mark_node)
10789 return error_mark_node;
10790
10791 /* Construct a new type node and return it. */
10792 if (TREE_CODE (t) == FUNCTION_TYPE)
10793 {
10794 fntype = build_function_type (return_type, arg_types);
10795 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10796 }
10797 else
10798 {
10799 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10800 if (! MAYBE_CLASS_TYPE_P (r))
10801 {
10802 /* [temp.deduct]
10803
10804 Type deduction may fail for any of the following
10805 reasons:
10806
10807 -- Attempting to create "pointer to member of T" when T
10808 is not a class type. */
10809 if (complain & tf_error)
10810 error ("creating pointer to member function of non-class type %qT",
10811 r);
10812 return error_mark_node;
10813 }
10814
10815 fntype = build_method_type_directly (r, return_type,
10816 TREE_CHAIN (arg_types));
10817 }
10818 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10819
10820 return fntype;
10821 }
10822
10823 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10824 ARGS into that specification, and return the substituted
10825 specification. If there is no specification, return NULL_TREE. */
10826
10827 static tree
10828 tsubst_exception_specification (tree fntype,
10829 tree args,
10830 tsubst_flags_t complain,
10831 tree in_decl,
10832 bool defer_ok)
10833 {
10834 tree specs;
10835 tree new_specs;
10836
10837 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10838 new_specs = NULL_TREE;
10839 if (specs && TREE_PURPOSE (specs))
10840 {
10841 /* A noexcept-specifier. */
10842 tree expr = TREE_PURPOSE (specs);
10843 if (TREE_CODE (expr) == INTEGER_CST)
10844 new_specs = expr;
10845 else if (defer_ok)
10846 {
10847 /* Defer instantiation of noexcept-specifiers to avoid
10848 excessive instantiations (c++/49107). */
10849 new_specs = make_node (DEFERRED_NOEXCEPT);
10850 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10851 {
10852 /* We already partially instantiated this member template,
10853 so combine the new args with the old. */
10854 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10855 = DEFERRED_NOEXCEPT_PATTERN (expr);
10856 DEFERRED_NOEXCEPT_ARGS (new_specs)
10857 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10858 }
10859 else
10860 {
10861 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10862 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10863 }
10864 }
10865 else
10866 new_specs = tsubst_copy_and_build
10867 (expr, args, complain, in_decl, /*function_p=*/false,
10868 /*integral_constant_expression_p=*/true);
10869 new_specs = build_noexcept_spec (new_specs, complain);
10870 }
10871 else if (specs)
10872 {
10873 if (! TREE_VALUE (specs))
10874 new_specs = specs;
10875 else
10876 while (specs)
10877 {
10878 tree spec;
10879 int i, len = 1;
10880 tree expanded_specs = NULL_TREE;
10881
10882 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10883 {
10884 /* Expand the pack expansion type. */
10885 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10886 args, complain,
10887 in_decl);
10888
10889 if (expanded_specs == error_mark_node)
10890 return error_mark_node;
10891 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10892 len = TREE_VEC_LENGTH (expanded_specs);
10893 else
10894 {
10895 /* We're substituting into a member template, so
10896 we got a TYPE_PACK_EXPANSION back. Add that
10897 expansion and move on. */
10898 gcc_assert (TREE_CODE (expanded_specs)
10899 == TYPE_PACK_EXPANSION);
10900 new_specs = add_exception_specifier (new_specs,
10901 expanded_specs,
10902 complain);
10903 specs = TREE_CHAIN (specs);
10904 continue;
10905 }
10906 }
10907
10908 for (i = 0; i < len; ++i)
10909 {
10910 if (expanded_specs)
10911 spec = TREE_VEC_ELT (expanded_specs, i);
10912 else
10913 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10914 if (spec == error_mark_node)
10915 return spec;
10916 new_specs = add_exception_specifier (new_specs, spec,
10917 complain);
10918 }
10919
10920 specs = TREE_CHAIN (specs);
10921 }
10922 }
10923 return new_specs;
10924 }
10925
10926 /* Take the tree structure T and replace template parameters used
10927 therein with the argument vector ARGS. IN_DECL is an associated
10928 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10929 Issue error and warning messages under control of COMPLAIN. Note
10930 that we must be relatively non-tolerant of extensions here, in
10931 order to preserve conformance; if we allow substitutions that
10932 should not be allowed, we may allow argument deductions that should
10933 not succeed, and therefore report ambiguous overload situations
10934 where there are none. In theory, we could allow the substitution,
10935 but indicate that it should have failed, and allow our caller to
10936 make sure that the right thing happens, but we don't try to do this
10937 yet.
10938
10939 This function is used for dealing with types, decls and the like;
10940 for expressions, use tsubst_expr or tsubst_copy. */
10941
10942 tree
10943 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10944 {
10945 enum tree_code code;
10946 tree type, r = NULL_TREE;
10947
10948 if (t == NULL_TREE || t == error_mark_node
10949 || t == integer_type_node
10950 || t == void_type_node
10951 || t == char_type_node
10952 || t == unknown_type_node
10953 || TREE_CODE (t) == NAMESPACE_DECL
10954 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10955 return t;
10956
10957 if (DECL_P (t))
10958 return tsubst_decl (t, args, complain);
10959
10960 if (args == NULL_TREE)
10961 return t;
10962
10963 code = TREE_CODE (t);
10964
10965 if (code == IDENTIFIER_NODE)
10966 type = IDENTIFIER_TYPE_VALUE (t);
10967 else
10968 type = TREE_TYPE (t);
10969
10970 gcc_assert (type != unknown_type_node);
10971
10972 /* Reuse typedefs. We need to do this to handle dependent attributes,
10973 such as attribute aligned. */
10974 if (TYPE_P (t)
10975 && typedef_variant_p (t))
10976 {
10977 tree decl = TYPE_NAME (t);
10978
10979 if (alias_template_specialization_p (t))
10980 {
10981 /* DECL represents an alias template and we want to
10982 instantiate it. */
10983 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10984 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10985 r = instantiate_alias_template (tmpl, gen_args, complain);
10986 }
10987 else if (DECL_CLASS_SCOPE_P (decl)
10988 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10989 && uses_template_parms (DECL_CONTEXT (decl)))
10990 {
10991 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10992 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10993 r = retrieve_specialization (tmpl, gen_args, 0);
10994 }
10995 else if (DECL_FUNCTION_SCOPE_P (decl)
10996 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10997 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10998 r = retrieve_local_specialization (decl);
10999 else
11000 /* The typedef is from a non-template context. */
11001 return t;
11002
11003 if (r)
11004 {
11005 r = TREE_TYPE (r);
11006 r = cp_build_qualified_type_real
11007 (r, cp_type_quals (t) | cp_type_quals (r),
11008 complain | tf_ignore_bad_quals);
11009 return r;
11010 }
11011 else
11012 {
11013 /* We don't have an instantiation yet, so drop the typedef. */
11014 int quals = cp_type_quals (t);
11015 t = DECL_ORIGINAL_TYPE (decl);
11016 t = cp_build_qualified_type_real (t, quals,
11017 complain | tf_ignore_bad_quals);
11018 }
11019 }
11020
11021 if (type
11022 && code != TYPENAME_TYPE
11023 && code != TEMPLATE_TYPE_PARM
11024 && code != IDENTIFIER_NODE
11025 && code != FUNCTION_TYPE
11026 && code != METHOD_TYPE)
11027 type = tsubst (type, args, complain, in_decl);
11028 if (type == error_mark_node)
11029 return error_mark_node;
11030
11031 switch (code)
11032 {
11033 case RECORD_TYPE:
11034 case UNION_TYPE:
11035 case ENUMERAL_TYPE:
11036 return tsubst_aggr_type (t, args, complain, in_decl,
11037 /*entering_scope=*/0);
11038
11039 case ERROR_MARK:
11040 case IDENTIFIER_NODE:
11041 case VOID_TYPE:
11042 case REAL_TYPE:
11043 case COMPLEX_TYPE:
11044 case VECTOR_TYPE:
11045 case BOOLEAN_TYPE:
11046 case NULLPTR_TYPE:
11047 case LANG_TYPE:
11048 return t;
11049
11050 case INTEGER_TYPE:
11051 if (t == integer_type_node)
11052 return t;
11053
11054 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
11055 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
11056 return t;
11057
11058 {
11059 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
11060
11061 max = tsubst_expr (omax, args, complain, in_decl,
11062 /*integral_constant_expression_p=*/false);
11063
11064 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11065 needed. */
11066 if (TREE_CODE (max) == NOP_EXPR
11067 && TREE_SIDE_EFFECTS (omax)
11068 && !TREE_TYPE (max))
11069 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
11070
11071 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11072 with TREE_SIDE_EFFECTS that indicates this is not an integral
11073 constant expression. */
11074 if (processing_template_decl
11075 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
11076 {
11077 gcc_assert (TREE_CODE (max) == NOP_EXPR);
11078 TREE_SIDE_EFFECTS (max) = 1;
11079 }
11080
11081 return compute_array_index_type (NULL_TREE, max, complain);
11082 }
11083
11084 case TEMPLATE_TYPE_PARM:
11085 case TEMPLATE_TEMPLATE_PARM:
11086 case BOUND_TEMPLATE_TEMPLATE_PARM:
11087 case TEMPLATE_PARM_INDEX:
11088 {
11089 int idx;
11090 int level;
11091 int levels;
11092 tree arg = NULL_TREE;
11093
11094 r = NULL_TREE;
11095
11096 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11097 template_parm_level_and_index (t, &level, &idx);
11098
11099 levels = TMPL_ARGS_DEPTH (args);
11100 if (level <= levels)
11101 {
11102 arg = TMPL_ARG (args, level, idx);
11103
11104 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11105 /* See through ARGUMENT_PACK_SELECT arguments. */
11106 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11107 }
11108
11109 if (arg == error_mark_node)
11110 return error_mark_node;
11111 else if (arg != NULL_TREE)
11112 {
11113 if (ARGUMENT_PACK_P (arg))
11114 /* If ARG is an argument pack, we don't actually want to
11115 perform a substitution here, because substitutions
11116 for argument packs are only done
11117 element-by-element. We can get to this point when
11118 substituting the type of a non-type template
11119 parameter pack, when that type actually contains
11120 template parameter packs from an outer template, e.g.,
11121
11122 template<typename... Types> struct A {
11123 template<Types... Values> struct B { };
11124 }; */
11125 return t;
11126
11127 if (code == TEMPLATE_TYPE_PARM)
11128 {
11129 int quals;
11130 gcc_assert (TYPE_P (arg));
11131
11132 quals = cp_type_quals (arg) | cp_type_quals (t);
11133
11134 return cp_build_qualified_type_real
11135 (arg, quals, complain | tf_ignore_bad_quals);
11136 }
11137 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11138 {
11139 /* We are processing a type constructed from a
11140 template template parameter. */
11141 tree argvec = tsubst (TYPE_TI_ARGS (t),
11142 args, complain, in_decl);
11143 if (argvec == error_mark_node)
11144 return error_mark_node;
11145
11146 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11147 || TREE_CODE (arg) == TEMPLATE_DECL
11148 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11149
11150 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11151 /* Consider this code:
11152
11153 template <template <class> class Template>
11154 struct Internal {
11155 template <class Arg> using Bind = Template<Arg>;
11156 };
11157
11158 template <template <class> class Template, class Arg>
11159 using Instantiate = Template<Arg>; //#0
11160
11161 template <template <class> class Template,
11162 class Argument>
11163 using Bind =
11164 Instantiate<Internal<Template>::template Bind,
11165 Argument>; //#1
11166
11167 When #1 is parsed, the
11168 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11169 parameter `Template' in #0 matches the
11170 UNBOUND_CLASS_TEMPLATE representing the argument
11171 `Internal<Template>::template Bind'; We then want
11172 to assemble the type `Bind<Argument>' that can't
11173 be fully created right now, because
11174 `Internal<Template>' not being complete, the Bind
11175 template cannot be looked up in that context. So
11176 we need to "store" `Bind<Argument>' for later
11177 when the context of Bind becomes complete. Let's
11178 store that in a TYPENAME_TYPE. */
11179 return make_typename_type (TYPE_CONTEXT (arg),
11180 build_nt (TEMPLATE_ID_EXPR,
11181 TYPE_IDENTIFIER (arg),
11182 argvec),
11183 typename_type,
11184 complain);
11185
11186 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11187 are resolving nested-types in the signature of a
11188 member function templates. Otherwise ARG is a
11189 TEMPLATE_DECL and is the real template to be
11190 instantiated. */
11191 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11192 arg = TYPE_NAME (arg);
11193
11194 r = lookup_template_class (arg,
11195 argvec, in_decl,
11196 DECL_CONTEXT (arg),
11197 /*entering_scope=*/0,
11198 complain);
11199 return cp_build_qualified_type_real
11200 (r, cp_type_quals (t), complain);
11201 }
11202 else
11203 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11204 return convert_from_reference (unshare_expr (arg));
11205 }
11206
11207 if (level == 1)
11208 /* This can happen during the attempted tsubst'ing in
11209 unify. This means that we don't yet have any information
11210 about the template parameter in question. */
11211 return t;
11212
11213 /* Early in template argument deduction substitution, we don't
11214 want to reduce the level of 'auto', or it will be confused
11215 with a normal template parm in subsequent deduction. */
11216 if (is_auto (t) && (complain & tf_partial))
11217 return t;
11218
11219 /* If we get here, we must have been looking at a parm for a
11220 more deeply nested template. Make a new version of this
11221 template parameter, but with a lower level. */
11222 switch (code)
11223 {
11224 case TEMPLATE_TYPE_PARM:
11225 case TEMPLATE_TEMPLATE_PARM:
11226 case BOUND_TEMPLATE_TEMPLATE_PARM:
11227 if (cp_type_quals (t))
11228 {
11229 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11230 r = cp_build_qualified_type_real
11231 (r, cp_type_quals (t),
11232 complain | (code == TEMPLATE_TYPE_PARM
11233 ? tf_ignore_bad_quals : 0));
11234 }
11235 else
11236 {
11237 r = copy_type (t);
11238 TEMPLATE_TYPE_PARM_INDEX (r)
11239 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11240 r, levels, args, complain);
11241 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11242 TYPE_MAIN_VARIANT (r) = r;
11243 TYPE_POINTER_TO (r) = NULL_TREE;
11244 TYPE_REFERENCE_TO (r) = NULL_TREE;
11245
11246 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11247 /* We have reduced the level of the template
11248 template parameter, but not the levels of its
11249 template parameters, so canonical_type_parameter
11250 will not be able to find the canonical template
11251 template parameter for this level. Thus, we
11252 require structural equality checking to compare
11253 TEMPLATE_TEMPLATE_PARMs. */
11254 SET_TYPE_STRUCTURAL_EQUALITY (r);
11255 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11256 SET_TYPE_STRUCTURAL_EQUALITY (r);
11257 else
11258 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11259
11260 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11261 {
11262 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11263 complain, in_decl);
11264 if (argvec == error_mark_node)
11265 return error_mark_node;
11266
11267 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11268 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11269 }
11270 }
11271 break;
11272
11273 case TEMPLATE_PARM_INDEX:
11274 r = reduce_template_parm_level (t, type, levels, args, complain);
11275 break;
11276
11277 default:
11278 gcc_unreachable ();
11279 }
11280
11281 return r;
11282 }
11283
11284 case TREE_LIST:
11285 {
11286 tree purpose, value, chain;
11287
11288 if (t == void_list_node)
11289 return t;
11290
11291 purpose = TREE_PURPOSE (t);
11292 if (purpose)
11293 {
11294 purpose = tsubst (purpose, args, complain, in_decl);
11295 if (purpose == error_mark_node)
11296 return error_mark_node;
11297 }
11298 value = TREE_VALUE (t);
11299 if (value)
11300 {
11301 value = tsubst (value, args, complain, in_decl);
11302 if (value == error_mark_node)
11303 return error_mark_node;
11304 }
11305 chain = TREE_CHAIN (t);
11306 if (chain && chain != void_type_node)
11307 {
11308 chain = tsubst (chain, args, complain, in_decl);
11309 if (chain == error_mark_node)
11310 return error_mark_node;
11311 }
11312 if (purpose == TREE_PURPOSE (t)
11313 && value == TREE_VALUE (t)
11314 && chain == TREE_CHAIN (t))
11315 return t;
11316 return hash_tree_cons (purpose, value, chain);
11317 }
11318
11319 case TREE_BINFO:
11320 /* We should never be tsubsting a binfo. */
11321 gcc_unreachable ();
11322
11323 case TREE_VEC:
11324 /* A vector of template arguments. */
11325 gcc_assert (!type);
11326 return tsubst_template_args (t, args, complain, in_decl);
11327
11328 case POINTER_TYPE:
11329 case REFERENCE_TYPE:
11330 {
11331 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11332 return t;
11333
11334 /* [temp.deduct]
11335
11336 Type deduction may fail for any of the following
11337 reasons:
11338
11339 -- Attempting to create a pointer to reference type.
11340 -- Attempting to create a reference to a reference type or
11341 a reference to void.
11342
11343 Core issue 106 says that creating a reference to a reference
11344 during instantiation is no longer a cause for failure. We
11345 only enforce this check in strict C++98 mode. */
11346 if ((TREE_CODE (type) == REFERENCE_TYPE
11347 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11348 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11349 {
11350 static location_t last_loc;
11351
11352 /* We keep track of the last time we issued this error
11353 message to avoid spewing a ton of messages during a
11354 single bad template instantiation. */
11355 if (complain & tf_error
11356 && last_loc != input_location)
11357 {
11358 if (TREE_CODE (type) == VOID_TYPE)
11359 error ("forming reference to void");
11360 else if (code == POINTER_TYPE)
11361 error ("forming pointer to reference type %qT", type);
11362 else
11363 error ("forming reference to reference type %qT", type);
11364 last_loc = input_location;
11365 }
11366
11367 return error_mark_node;
11368 }
11369 else if (code == POINTER_TYPE)
11370 {
11371 r = build_pointer_type (type);
11372 if (TREE_CODE (type) == METHOD_TYPE)
11373 r = build_ptrmemfunc_type (r);
11374 }
11375 else if (TREE_CODE (type) == REFERENCE_TYPE)
11376 /* In C++0x, during template argument substitution, when there is an
11377 attempt to create a reference to a reference type, reference
11378 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11379
11380 "If a template-argument for a template-parameter T names a type
11381 that is a reference to a type A, an attempt to create the type
11382 'lvalue reference to cv T' creates the type 'lvalue reference to
11383 A,' while an attempt to create the type type rvalue reference to
11384 cv T' creates the type T"
11385 */
11386 r = cp_build_reference_type
11387 (TREE_TYPE (type),
11388 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11389 else
11390 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11391 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11392
11393 if (r != error_mark_node)
11394 /* Will this ever be needed for TYPE_..._TO values? */
11395 layout_type (r);
11396
11397 return r;
11398 }
11399 case OFFSET_TYPE:
11400 {
11401 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11402 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11403 {
11404 /* [temp.deduct]
11405
11406 Type deduction may fail for any of the following
11407 reasons:
11408
11409 -- Attempting to create "pointer to member of T" when T
11410 is not a class type. */
11411 if (complain & tf_error)
11412 error ("creating pointer to member of non-class type %qT", r);
11413 return error_mark_node;
11414 }
11415 if (TREE_CODE (type) == REFERENCE_TYPE)
11416 {
11417 if (complain & tf_error)
11418 error ("creating pointer to member reference type %qT", type);
11419 return error_mark_node;
11420 }
11421 if (TREE_CODE (type) == VOID_TYPE)
11422 {
11423 if (complain & tf_error)
11424 error ("creating pointer to member of type void");
11425 return error_mark_node;
11426 }
11427 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11428 if (TREE_CODE (type) == FUNCTION_TYPE)
11429 {
11430 /* The type of the implicit object parameter gets its
11431 cv-qualifiers from the FUNCTION_TYPE. */
11432 tree memptr;
11433 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11434 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11435 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11436 complain);
11437 }
11438 else
11439 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11440 cp_type_quals (t),
11441 complain);
11442 }
11443 case FUNCTION_TYPE:
11444 case METHOD_TYPE:
11445 {
11446 tree fntype;
11447 tree specs;
11448 fntype = tsubst_function_type (t, args, complain, in_decl);
11449 if (fntype == error_mark_node)
11450 return error_mark_node;
11451
11452 /* Substitute the exception specification. */
11453 specs = tsubst_exception_specification (t, args, complain,
11454 in_decl, /*defer_ok*/true);
11455 if (specs == error_mark_node)
11456 return error_mark_node;
11457 if (specs)
11458 fntype = build_exception_variant (fntype, specs);
11459 return fntype;
11460 }
11461 case ARRAY_TYPE:
11462 {
11463 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11464 if (domain == error_mark_node)
11465 return error_mark_node;
11466
11467 /* As an optimization, we avoid regenerating the array type if
11468 it will obviously be the same as T. */
11469 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11470 return t;
11471
11472 /* These checks should match the ones in grokdeclarator.
11473
11474 [temp.deduct]
11475
11476 The deduction may fail for any of the following reasons:
11477
11478 -- Attempting to create an array with an element type that
11479 is void, a function type, or a reference type, or [DR337]
11480 an abstract class type. */
11481 if (TREE_CODE (type) == VOID_TYPE
11482 || TREE_CODE (type) == FUNCTION_TYPE
11483 || TREE_CODE (type) == REFERENCE_TYPE)
11484 {
11485 if (complain & tf_error)
11486 error ("creating array of %qT", type);
11487 return error_mark_node;
11488 }
11489 if (ABSTRACT_CLASS_TYPE_P (type))
11490 {
11491 if (complain & tf_error)
11492 error ("creating array of %qT, which is an abstract class type",
11493 type);
11494 return error_mark_node;
11495 }
11496
11497 r = build_cplus_array_type (type, domain);
11498
11499 if (TYPE_USER_ALIGN (t))
11500 {
11501 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11502 TYPE_USER_ALIGN (r) = 1;
11503 }
11504
11505 return r;
11506 }
11507
11508 case TYPENAME_TYPE:
11509 {
11510 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11511 in_decl, /*entering_scope=*/1);
11512 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11513 complain, in_decl);
11514
11515 if (ctx == error_mark_node || f == error_mark_node)
11516 return error_mark_node;
11517
11518 if (!MAYBE_CLASS_TYPE_P (ctx))
11519 {
11520 if (complain & tf_error)
11521 error ("%qT is not a class, struct, or union type", ctx);
11522 return error_mark_node;
11523 }
11524 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11525 {
11526 /* Normally, make_typename_type does not require that the CTX
11527 have complete type in order to allow things like:
11528
11529 template <class T> struct S { typename S<T>::X Y; };
11530
11531 But, such constructs have already been resolved by this
11532 point, so here CTX really should have complete type, unless
11533 it's a partial instantiation. */
11534 ctx = complete_type (ctx);
11535 if (!COMPLETE_TYPE_P (ctx))
11536 {
11537 if (complain & tf_error)
11538 cxx_incomplete_type_error (NULL_TREE, ctx);
11539 return error_mark_node;
11540 }
11541 }
11542
11543 f = make_typename_type (ctx, f, typename_type,
11544 complain | tf_keep_type_decl);
11545 if (f == error_mark_node)
11546 return f;
11547 if (TREE_CODE (f) == TYPE_DECL)
11548 {
11549 complain |= tf_ignore_bad_quals;
11550 f = TREE_TYPE (f);
11551 }
11552
11553 if (TREE_CODE (f) != TYPENAME_TYPE)
11554 {
11555 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11556 {
11557 if (complain & tf_error)
11558 error ("%qT resolves to %qT, which is not an enumeration type",
11559 t, f);
11560 else
11561 return error_mark_node;
11562 }
11563 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11564 {
11565 if (complain & tf_error)
11566 error ("%qT resolves to %qT, which is is not a class type",
11567 t, f);
11568 else
11569 return error_mark_node;
11570 }
11571 }
11572
11573 return cp_build_qualified_type_real
11574 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11575 }
11576
11577 case UNBOUND_CLASS_TEMPLATE:
11578 {
11579 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11580 in_decl, /*entering_scope=*/1);
11581 tree name = TYPE_IDENTIFIER (t);
11582 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11583
11584 if (ctx == error_mark_node || name == error_mark_node)
11585 return error_mark_node;
11586
11587 if (parm_list)
11588 parm_list = tsubst_template_parms (parm_list, args, complain);
11589 return make_unbound_class_template (ctx, name, parm_list, complain);
11590 }
11591
11592 case TYPEOF_TYPE:
11593 {
11594 tree type;
11595
11596 ++cp_unevaluated_operand;
11597 ++c_inhibit_evaluation_warnings;
11598
11599 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11600 complain, in_decl,
11601 /*integral_constant_expression_p=*/false);
11602
11603 --cp_unevaluated_operand;
11604 --c_inhibit_evaluation_warnings;
11605
11606 type = finish_typeof (type);
11607 return cp_build_qualified_type_real (type,
11608 cp_type_quals (t)
11609 | cp_type_quals (type),
11610 complain);
11611 }
11612
11613 case DECLTYPE_TYPE:
11614 {
11615 tree type;
11616
11617 ++cp_unevaluated_operand;
11618 ++c_inhibit_evaluation_warnings;
11619
11620 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11621 complain, in_decl,
11622 /*integral_constant_expression_p=*/false);
11623
11624 --cp_unevaluated_operand;
11625 --c_inhibit_evaluation_warnings;
11626
11627 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11628 type = lambda_capture_field_type (type);
11629 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11630 type = lambda_proxy_type (type);
11631 else
11632 type = finish_decltype_type
11633 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11634 return cp_build_qualified_type_real (type,
11635 cp_type_quals (t)
11636 | cp_type_quals (type),
11637 complain);
11638 }
11639
11640 case UNDERLYING_TYPE:
11641 {
11642 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11643 complain, in_decl);
11644 return finish_underlying_type (type);
11645 }
11646
11647 case TYPE_ARGUMENT_PACK:
11648 case NONTYPE_ARGUMENT_PACK:
11649 {
11650 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11651 tree packed_out =
11652 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11653 args,
11654 complain,
11655 in_decl);
11656 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11657
11658 /* For template nontype argument packs, also substitute into
11659 the type. */
11660 if (code == NONTYPE_ARGUMENT_PACK)
11661 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11662
11663 return r;
11664 }
11665 break;
11666
11667 case INTEGER_CST:
11668 case REAL_CST:
11669 case STRING_CST:
11670 case PLUS_EXPR:
11671 case MINUS_EXPR:
11672 case NEGATE_EXPR:
11673 case NOP_EXPR:
11674 case INDIRECT_REF:
11675 case ADDR_EXPR:
11676 case CALL_EXPR:
11677 case ARRAY_REF:
11678 case SCOPE_REF:
11679 /* We should use one of the expression tsubsts for these codes. */
11680 gcc_unreachable ();
11681
11682 default:
11683 sorry ("use of %qs in template", tree_code_name [(int) code]);
11684 return error_mark_node;
11685 }
11686 }
11687
11688 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11689 type of the expression on the left-hand side of the "." or "->"
11690 operator. */
11691
11692 static tree
11693 tsubst_baselink (tree baselink, tree object_type,
11694 tree args, tsubst_flags_t complain, tree in_decl)
11695 {
11696 tree name;
11697 tree qualifying_scope;
11698 tree fns;
11699 tree optype;
11700 tree template_args = 0;
11701 bool template_id_p = false;
11702 bool qualified = BASELINK_QUALIFIED_P (baselink);
11703
11704 /* A baselink indicates a function from a base class. Both the
11705 BASELINK_ACCESS_BINFO and the base class referenced may
11706 indicate bases of the template class, rather than the
11707 instantiated class. In addition, lookups that were not
11708 ambiguous before may be ambiguous now. Therefore, we perform
11709 the lookup again. */
11710 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11711 qualifying_scope = tsubst (qualifying_scope, args,
11712 complain, in_decl);
11713 fns = BASELINK_FUNCTIONS (baselink);
11714 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11715 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11716 {
11717 template_id_p = true;
11718 template_args = TREE_OPERAND (fns, 1);
11719 fns = TREE_OPERAND (fns, 0);
11720 if (template_args)
11721 template_args = tsubst_template_args (template_args, args,
11722 complain, in_decl);
11723 }
11724 name = DECL_NAME (get_first_fn (fns));
11725 if (IDENTIFIER_TYPENAME_P (name))
11726 name = mangle_conv_op_name_for_type (optype);
11727 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11728 if (!baselink)
11729 return error_mark_node;
11730
11731 /* If lookup found a single function, mark it as used at this
11732 point. (If it lookup found multiple functions the one selected
11733 later by overload resolution will be marked as used at that
11734 point.) */
11735 if (BASELINK_P (baselink))
11736 fns = BASELINK_FUNCTIONS (baselink);
11737 if (!template_id_p && !really_overloaded_fn (fns))
11738 mark_used (OVL_CURRENT (fns));
11739
11740 /* Add back the template arguments, if present. */
11741 if (BASELINK_P (baselink) && template_id_p)
11742 BASELINK_FUNCTIONS (baselink)
11743 = build_nt (TEMPLATE_ID_EXPR,
11744 BASELINK_FUNCTIONS (baselink),
11745 template_args);
11746 /* Update the conversion operator type. */
11747 BASELINK_OPTYPE (baselink) = optype;
11748
11749 if (!object_type)
11750 object_type = current_class_type;
11751
11752 if (qualified)
11753 baselink = adjust_result_of_qualified_name_lookup (baselink,
11754 qualifying_scope,
11755 object_type);
11756 return baselink;
11757 }
11758
11759 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11760 true if the qualified-id will be a postfix-expression in-and-of
11761 itself; false if more of the postfix-expression follows the
11762 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11763 of "&". */
11764
11765 static tree
11766 tsubst_qualified_id (tree qualified_id, tree args,
11767 tsubst_flags_t complain, tree in_decl,
11768 bool done, bool address_p)
11769 {
11770 tree expr;
11771 tree scope;
11772 tree name;
11773 bool is_template;
11774 tree template_args;
11775 location_t loc = UNKNOWN_LOCATION;
11776
11777 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11778
11779 /* Figure out what name to look up. */
11780 name = TREE_OPERAND (qualified_id, 1);
11781 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11782 {
11783 is_template = true;
11784 loc = EXPR_LOCATION (name);
11785 template_args = TREE_OPERAND (name, 1);
11786 if (template_args)
11787 template_args = tsubst_template_args (template_args, args,
11788 complain, in_decl);
11789 name = TREE_OPERAND (name, 0);
11790 }
11791 else
11792 {
11793 is_template = false;
11794 template_args = NULL_TREE;
11795 }
11796
11797 /* Substitute into the qualifying scope. When there are no ARGS, we
11798 are just trying to simplify a non-dependent expression. In that
11799 case the qualifying scope may be dependent, and, in any case,
11800 substituting will not help. */
11801 scope = TREE_OPERAND (qualified_id, 0);
11802 if (args)
11803 {
11804 scope = tsubst (scope, args, complain, in_decl);
11805 expr = tsubst_copy (name, args, complain, in_decl);
11806 }
11807 else
11808 expr = name;
11809
11810 if (dependent_scope_p (scope))
11811 {
11812 if (is_template)
11813 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
11814 return build_qualified_name (NULL_TREE, scope, expr,
11815 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11816 }
11817
11818 if (!BASELINK_P (name) && !DECL_P (expr))
11819 {
11820 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11821 {
11822 /* A BIT_NOT_EXPR is used to represent a destructor. */
11823 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11824 {
11825 error ("qualifying type %qT does not match destructor name ~%qT",
11826 scope, TREE_OPERAND (expr, 0));
11827 expr = error_mark_node;
11828 }
11829 else
11830 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11831 /*is_type_p=*/0, false);
11832 }
11833 else
11834 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11835 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11836 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11837 {
11838 if (complain & tf_error)
11839 {
11840 error ("dependent-name %qE is parsed as a non-type, but "
11841 "instantiation yields a type", qualified_id);
11842 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11843 }
11844 return error_mark_node;
11845 }
11846 }
11847
11848 if (DECL_P (expr))
11849 {
11850 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11851 scope);
11852 /* Remember that there was a reference to this entity. */
11853 mark_used (expr);
11854 }
11855
11856 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11857 {
11858 if (complain & tf_error)
11859 qualified_name_lookup_error (scope,
11860 TREE_OPERAND (qualified_id, 1),
11861 expr, input_location);
11862 return error_mark_node;
11863 }
11864
11865 if (is_template)
11866 expr = lookup_template_function (expr, template_args);
11867
11868 if (expr == error_mark_node && complain & tf_error)
11869 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11870 expr, input_location);
11871 else if (TYPE_P (scope))
11872 {
11873 expr = (adjust_result_of_qualified_name_lookup
11874 (expr, scope, current_class_type));
11875 expr = (finish_qualified_id_expr
11876 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
11877 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11878 /*template_arg_p=*/false));
11879 }
11880
11881 /* Expressions do not generally have reference type. */
11882 if (TREE_CODE (expr) != SCOPE_REF
11883 /* However, if we're about to form a pointer-to-member, we just
11884 want the referenced member referenced. */
11885 && TREE_CODE (expr) != OFFSET_REF)
11886 expr = convert_from_reference (expr);
11887
11888 return expr;
11889 }
11890
11891 /* Like tsubst, but deals with expressions. This function just replaces
11892 template parms; to finish processing the resultant expression, use
11893 tsubst_copy_and_build or tsubst_expr. */
11894
11895 static tree
11896 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11897 {
11898 enum tree_code code;
11899 tree r;
11900
11901 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11902 return t;
11903
11904 code = TREE_CODE (t);
11905
11906 switch (code)
11907 {
11908 case PARM_DECL:
11909 r = retrieve_local_specialization (t);
11910
11911 if (r == NULL_TREE)
11912 {
11913 /* We get here for a use of 'this' in an NSDMI. */
11914 if (DECL_NAME (t) == this_identifier
11915 && at_function_scope_p ()
11916 && DECL_CONSTRUCTOR_P (current_function_decl))
11917 return current_class_ptr;
11918
11919 /* This can happen for a parameter name used later in a function
11920 declaration (such as in a late-specified return type). Just
11921 make a dummy decl, since it's only used for its type. */
11922 gcc_assert (cp_unevaluated_operand != 0);
11923 r = tsubst_decl (t, args, complain);
11924 /* Give it the template pattern as its context; its true context
11925 hasn't been instantiated yet and this is good enough for
11926 mangling. */
11927 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11928 }
11929
11930 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11931 r = ARGUMENT_PACK_SELECT_ARG (r);
11932 mark_used (r);
11933 return r;
11934
11935 case CONST_DECL:
11936 {
11937 tree enum_type;
11938 tree v;
11939
11940 if (DECL_TEMPLATE_PARM_P (t))
11941 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11942 /* There is no need to substitute into namespace-scope
11943 enumerators. */
11944 if (DECL_NAMESPACE_SCOPE_P (t))
11945 return t;
11946 /* If ARGS is NULL, then T is known to be non-dependent. */
11947 if (args == NULL_TREE)
11948 return integral_constant_value (t);
11949
11950 /* Unfortunately, we cannot just call lookup_name here.
11951 Consider:
11952
11953 template <int I> int f() {
11954 enum E { a = I };
11955 struct S { void g() { E e = a; } };
11956 };
11957
11958 When we instantiate f<7>::S::g(), say, lookup_name is not
11959 clever enough to find f<7>::a. */
11960 enum_type
11961 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11962 /*entering_scope=*/0);
11963
11964 for (v = TYPE_VALUES (enum_type);
11965 v != NULL_TREE;
11966 v = TREE_CHAIN (v))
11967 if (TREE_PURPOSE (v) == DECL_NAME (t))
11968 return TREE_VALUE (v);
11969
11970 /* We didn't find the name. That should never happen; if
11971 name-lookup found it during preliminary parsing, we
11972 should find it again here during instantiation. */
11973 gcc_unreachable ();
11974 }
11975 return t;
11976
11977 case FIELD_DECL:
11978 if (DECL_CONTEXT (t))
11979 {
11980 tree ctx;
11981
11982 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11983 /*entering_scope=*/1);
11984 if (ctx != DECL_CONTEXT (t))
11985 {
11986 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11987 if (!r)
11988 {
11989 if (complain & tf_error)
11990 error ("using invalid field %qD", t);
11991 return error_mark_node;
11992 }
11993 return r;
11994 }
11995 }
11996
11997 return t;
11998
11999 case VAR_DECL:
12000 case FUNCTION_DECL:
12001 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
12002 || local_variable_p (t))
12003 t = tsubst (t, args, complain, in_decl);
12004 mark_used (t);
12005 return t;
12006
12007 case NAMESPACE_DECL:
12008 return t;
12009
12010 case OVERLOAD:
12011 /* An OVERLOAD will always be a non-dependent overload set; an
12012 overload set from function scope will just be represented with an
12013 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12014 gcc_assert (!uses_template_parms (t));
12015 return t;
12016
12017 case BASELINK:
12018 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
12019
12020 case TEMPLATE_DECL:
12021 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12022 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
12023 args, complain, in_decl);
12024 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
12025 return tsubst (t, args, complain, in_decl);
12026 else if (DECL_CLASS_SCOPE_P (t)
12027 && uses_template_parms (DECL_CONTEXT (t)))
12028 {
12029 /* Template template argument like the following example need
12030 special treatment:
12031
12032 template <template <class> class TT> struct C {};
12033 template <class T> struct D {
12034 template <class U> struct E {};
12035 C<E> c; // #1
12036 };
12037 D<int> d; // #2
12038
12039 We are processing the template argument `E' in #1 for
12040 the template instantiation #2. Originally, `E' is a
12041 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12042 have to substitute this with one having context `D<int>'. */
12043
12044 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12045 return lookup_field (context, DECL_NAME(t), 0, false);
12046 }
12047 else
12048 /* Ordinary template template argument. */
12049 return t;
12050
12051 case CAST_EXPR:
12052 case REINTERPRET_CAST_EXPR:
12053 case CONST_CAST_EXPR:
12054 case STATIC_CAST_EXPR:
12055 case DYNAMIC_CAST_EXPR:
12056 case IMPLICIT_CONV_EXPR:
12057 case CONVERT_EXPR:
12058 case NOP_EXPR:
12059 return build1
12060 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12061 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12062
12063 case SIZEOF_EXPR:
12064 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12065 {
12066
12067 tree expanded, op = TREE_OPERAND (t, 0);
12068 int len = 0;
12069
12070 if (SIZEOF_EXPR_TYPE_P (t))
12071 op = TREE_TYPE (op);
12072
12073 ++cp_unevaluated_operand;
12074 ++c_inhibit_evaluation_warnings;
12075 /* We only want to compute the number of arguments. */
12076 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
12077 --cp_unevaluated_operand;
12078 --c_inhibit_evaluation_warnings;
12079
12080 if (TREE_CODE (expanded) == TREE_VEC)
12081 len = TREE_VEC_LENGTH (expanded);
12082
12083 if (expanded == error_mark_node)
12084 return error_mark_node;
12085 else if (PACK_EXPANSION_P (expanded)
12086 || (TREE_CODE (expanded) == TREE_VEC
12087 && len > 0
12088 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
12089 {
12090 if (TREE_CODE (expanded) == TREE_VEC)
12091 expanded = TREE_VEC_ELT (expanded, len - 1);
12092
12093 if (TYPE_P (expanded))
12094 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12095 complain & tf_error);
12096 else
12097 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12098 complain & tf_error);
12099 }
12100 else
12101 return build_int_cst (size_type_node, len);
12102 }
12103 if (SIZEOF_EXPR_TYPE_P (t))
12104 {
12105 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
12106 args, complain, in_decl);
12107 r = build1 (NOP_EXPR, r, error_mark_node);
12108 r = build1 (SIZEOF_EXPR,
12109 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
12110 SIZEOF_EXPR_TYPE_P (r) = 1;
12111 return r;
12112 }
12113 /* Fall through */
12114
12115 case INDIRECT_REF:
12116 case NEGATE_EXPR:
12117 case TRUTH_NOT_EXPR:
12118 case BIT_NOT_EXPR:
12119 case ADDR_EXPR:
12120 case UNARY_PLUS_EXPR: /* Unary + */
12121 case ALIGNOF_EXPR:
12122 case AT_ENCODE_EXPR:
12123 case ARROW_EXPR:
12124 case THROW_EXPR:
12125 case TYPEID_EXPR:
12126 case REALPART_EXPR:
12127 case IMAGPART_EXPR:
12128 return build1
12129 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12130 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12131
12132 case COMPONENT_REF:
12133 {
12134 tree object;
12135 tree name;
12136
12137 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12138 name = TREE_OPERAND (t, 1);
12139 if (TREE_CODE (name) == BIT_NOT_EXPR)
12140 {
12141 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12142 complain, in_decl);
12143 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12144 }
12145 else if (TREE_CODE (name) == SCOPE_REF
12146 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12147 {
12148 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12149 complain, in_decl);
12150 name = TREE_OPERAND (name, 1);
12151 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12152 complain, in_decl);
12153 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12154 name = build_qualified_name (/*type=*/NULL_TREE,
12155 base, name,
12156 /*template_p=*/false);
12157 }
12158 else if (BASELINK_P (name))
12159 name = tsubst_baselink (name,
12160 non_reference (TREE_TYPE (object)),
12161 args, complain,
12162 in_decl);
12163 else
12164 name = tsubst_copy (name, args, complain, in_decl);
12165 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12166 }
12167
12168 case PLUS_EXPR:
12169 case MINUS_EXPR:
12170 case MULT_EXPR:
12171 case TRUNC_DIV_EXPR:
12172 case CEIL_DIV_EXPR:
12173 case FLOOR_DIV_EXPR:
12174 case ROUND_DIV_EXPR:
12175 case EXACT_DIV_EXPR:
12176 case BIT_AND_EXPR:
12177 case BIT_IOR_EXPR:
12178 case BIT_XOR_EXPR:
12179 case TRUNC_MOD_EXPR:
12180 case FLOOR_MOD_EXPR:
12181 case TRUTH_ANDIF_EXPR:
12182 case TRUTH_ORIF_EXPR:
12183 case TRUTH_AND_EXPR:
12184 case TRUTH_OR_EXPR:
12185 case RSHIFT_EXPR:
12186 case LSHIFT_EXPR:
12187 case RROTATE_EXPR:
12188 case LROTATE_EXPR:
12189 case EQ_EXPR:
12190 case NE_EXPR:
12191 case MAX_EXPR:
12192 case MIN_EXPR:
12193 case LE_EXPR:
12194 case GE_EXPR:
12195 case LT_EXPR:
12196 case GT_EXPR:
12197 case COMPOUND_EXPR:
12198 case DOTSTAR_EXPR:
12199 case MEMBER_REF:
12200 case PREDECREMENT_EXPR:
12201 case PREINCREMENT_EXPR:
12202 case POSTDECREMENT_EXPR:
12203 case POSTINCREMENT_EXPR:
12204 return build_nt
12205 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12206 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12207
12208 case SCOPE_REF:
12209 return build_qualified_name (/*type=*/NULL_TREE,
12210 tsubst_copy (TREE_OPERAND (t, 0),
12211 args, complain, in_decl),
12212 tsubst_copy (TREE_OPERAND (t, 1),
12213 args, complain, in_decl),
12214 QUALIFIED_NAME_IS_TEMPLATE (t));
12215
12216 case ARRAY_REF:
12217 return build_nt
12218 (ARRAY_REF,
12219 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12220 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12221 NULL_TREE, NULL_TREE);
12222
12223 case CALL_EXPR:
12224 {
12225 int n = VL_EXP_OPERAND_LENGTH (t);
12226 tree result = build_vl_exp (CALL_EXPR, n);
12227 int i;
12228 for (i = 0; i < n; i++)
12229 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12230 complain, in_decl);
12231 return result;
12232 }
12233
12234 case COND_EXPR:
12235 case MODOP_EXPR:
12236 case PSEUDO_DTOR_EXPR:
12237 {
12238 r = build_nt
12239 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12240 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12241 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12242 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12243 return r;
12244 }
12245
12246 case NEW_EXPR:
12247 {
12248 r = build_nt
12249 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12250 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12251 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12252 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12253 return r;
12254 }
12255
12256 case DELETE_EXPR:
12257 {
12258 r = build_nt
12259 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12260 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12261 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12262 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12263 return r;
12264 }
12265
12266 case TEMPLATE_ID_EXPR:
12267 {
12268 /* Substituted template arguments */
12269 tree fn = TREE_OPERAND (t, 0);
12270 tree targs = TREE_OPERAND (t, 1);
12271
12272 fn = tsubst_copy (fn, args, complain, in_decl);
12273 if (targs)
12274 targs = tsubst_template_args (targs, args, complain, in_decl);
12275
12276 return lookup_template_function (fn, targs);
12277 }
12278
12279 case TREE_LIST:
12280 {
12281 tree purpose, value, chain;
12282
12283 if (t == void_list_node)
12284 return t;
12285
12286 purpose = TREE_PURPOSE (t);
12287 if (purpose)
12288 purpose = tsubst_copy (purpose, args, complain, in_decl);
12289 value = TREE_VALUE (t);
12290 if (value)
12291 value = tsubst_copy (value, args, complain, in_decl);
12292 chain = TREE_CHAIN (t);
12293 if (chain && chain != void_type_node)
12294 chain = tsubst_copy (chain, args, complain, in_decl);
12295 if (purpose == TREE_PURPOSE (t)
12296 && value == TREE_VALUE (t)
12297 && chain == TREE_CHAIN (t))
12298 return t;
12299 return tree_cons (purpose, value, chain);
12300 }
12301
12302 case RECORD_TYPE:
12303 case UNION_TYPE:
12304 case ENUMERAL_TYPE:
12305 case INTEGER_TYPE:
12306 case TEMPLATE_TYPE_PARM:
12307 case TEMPLATE_TEMPLATE_PARM:
12308 case BOUND_TEMPLATE_TEMPLATE_PARM:
12309 case TEMPLATE_PARM_INDEX:
12310 case POINTER_TYPE:
12311 case REFERENCE_TYPE:
12312 case OFFSET_TYPE:
12313 case FUNCTION_TYPE:
12314 case METHOD_TYPE:
12315 case ARRAY_TYPE:
12316 case TYPENAME_TYPE:
12317 case UNBOUND_CLASS_TEMPLATE:
12318 case TYPEOF_TYPE:
12319 case DECLTYPE_TYPE:
12320 case TYPE_DECL:
12321 return tsubst (t, args, complain, in_decl);
12322
12323 case IDENTIFIER_NODE:
12324 if (IDENTIFIER_TYPENAME_P (t))
12325 {
12326 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12327 return mangle_conv_op_name_for_type (new_type);
12328 }
12329 else
12330 return t;
12331
12332 case CONSTRUCTOR:
12333 /* This is handled by tsubst_copy_and_build. */
12334 gcc_unreachable ();
12335
12336 case VA_ARG_EXPR:
12337 return build_x_va_arg (EXPR_LOCATION (t),
12338 tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12339 in_decl),
12340 tsubst (TREE_TYPE (t), args, complain, in_decl));
12341
12342 case CLEANUP_POINT_EXPR:
12343 /* We shouldn't have built any of these during initial template
12344 generation. Instead, they should be built during instantiation
12345 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12346 gcc_unreachable ();
12347
12348 case OFFSET_REF:
12349 r = build2
12350 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12351 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12352 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12353 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12354 mark_used (TREE_OPERAND (r, 1));
12355 return r;
12356
12357 case EXPR_PACK_EXPANSION:
12358 error ("invalid use of pack expansion expression");
12359 return error_mark_node;
12360
12361 case NONTYPE_ARGUMENT_PACK:
12362 error ("use %<...%> to expand argument pack");
12363 return error_mark_node;
12364
12365 case INTEGER_CST:
12366 case REAL_CST:
12367 case STRING_CST:
12368 case COMPLEX_CST:
12369 {
12370 /* Instantiate any typedefs in the type. */
12371 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12372 r = fold_convert (type, t);
12373 gcc_assert (TREE_CODE (r) == code);
12374 return r;
12375 }
12376
12377 case PTRMEM_CST:
12378 /* These can sometimes show up in a partial instantiation, but never
12379 involve template parms. */
12380 gcc_assert (!uses_template_parms (t));
12381 return t;
12382
12383 default:
12384 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12385 gcc_checking_assert (false);
12386 return t;
12387 }
12388 }
12389
12390 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12391
12392 static tree
12393 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12394 tree in_decl)
12395 {
12396 tree new_clauses = NULL, nc, oc;
12397
12398 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12399 {
12400 nc = copy_node (oc);
12401 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12402 new_clauses = nc;
12403
12404 switch (OMP_CLAUSE_CODE (nc))
12405 {
12406 case OMP_CLAUSE_LASTPRIVATE:
12407 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12408 {
12409 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12410 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12411 in_decl, /*integral_constant_expression_p=*/false);
12412 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12413 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12414 }
12415 /* FALLTHRU */
12416 case OMP_CLAUSE_PRIVATE:
12417 case OMP_CLAUSE_SHARED:
12418 case OMP_CLAUSE_FIRSTPRIVATE:
12419 case OMP_CLAUSE_REDUCTION:
12420 case OMP_CLAUSE_COPYIN:
12421 case OMP_CLAUSE_COPYPRIVATE:
12422 case OMP_CLAUSE_IF:
12423 case OMP_CLAUSE_NUM_THREADS:
12424 case OMP_CLAUSE_SCHEDULE:
12425 case OMP_CLAUSE_COLLAPSE:
12426 case OMP_CLAUSE_FINAL:
12427 OMP_CLAUSE_OPERAND (nc, 0)
12428 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12429 in_decl, /*integral_constant_expression_p=*/false);
12430 break;
12431 case OMP_CLAUSE_NOWAIT:
12432 case OMP_CLAUSE_ORDERED:
12433 case OMP_CLAUSE_DEFAULT:
12434 case OMP_CLAUSE_UNTIED:
12435 case OMP_CLAUSE_MERGEABLE:
12436 break;
12437 default:
12438 gcc_unreachable ();
12439 }
12440 }
12441
12442 return finish_omp_clauses (nreverse (new_clauses));
12443 }
12444
12445 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12446
12447 static tree
12448 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12449 tree in_decl)
12450 {
12451 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12452
12453 tree purpose, value, chain;
12454
12455 if (t == NULL)
12456 return t;
12457
12458 if (TREE_CODE (t) != TREE_LIST)
12459 return tsubst_copy_and_build (t, args, complain, in_decl,
12460 /*function_p=*/false,
12461 /*integral_constant_expression_p=*/false);
12462
12463 if (t == void_list_node)
12464 return t;
12465
12466 purpose = TREE_PURPOSE (t);
12467 if (purpose)
12468 purpose = RECUR (purpose);
12469 value = TREE_VALUE (t);
12470 if (value)
12471 {
12472 if (TREE_CODE (value) != LABEL_DECL)
12473 value = RECUR (value);
12474 else
12475 {
12476 value = lookup_label (DECL_NAME (value));
12477 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12478 TREE_USED (value) = 1;
12479 }
12480 }
12481 chain = TREE_CHAIN (t);
12482 if (chain && chain != void_type_node)
12483 chain = RECUR (chain);
12484 return tree_cons (purpose, value, chain);
12485 #undef RECUR
12486 }
12487
12488 /* Substitute one OMP_FOR iterator. */
12489
12490 static void
12491 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12492 tree condv, tree incrv, tree *clauses,
12493 tree args, tsubst_flags_t complain, tree in_decl,
12494 bool integral_constant_expression_p)
12495 {
12496 #define RECUR(NODE) \
12497 tsubst_expr ((NODE), args, complain, in_decl, \
12498 integral_constant_expression_p)
12499 tree decl, init, cond, incr;
12500 bool init_decl;
12501
12502 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12503 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12504 decl = TREE_OPERAND (init, 0);
12505 init = TREE_OPERAND (init, 1);
12506 /* Do this before substituting into decl to handle 'auto'. */
12507 init_decl = (init && TREE_CODE (init) == DECL_EXPR);
12508 init = RECUR (init);
12509 decl = RECUR (decl);
12510 if (init_decl)
12511 {
12512 gcc_assert (!processing_template_decl);
12513 init = DECL_INITIAL (decl);
12514 DECL_INITIAL (decl) = NULL_TREE;
12515 }
12516
12517 gcc_assert (!type_dependent_expression_p (decl));
12518
12519 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12520 {
12521 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12522 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12523 if (TREE_CODE (incr) == MODIFY_EXPR)
12524 incr = build_x_modify_expr (EXPR_LOCATION (incr),
12525 RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12526 RECUR (TREE_OPERAND (incr, 1)),
12527 complain);
12528 else
12529 incr = RECUR (incr);
12530 TREE_VEC_ELT (declv, i) = decl;
12531 TREE_VEC_ELT (initv, i) = init;
12532 TREE_VEC_ELT (condv, i) = cond;
12533 TREE_VEC_ELT (incrv, i) = incr;
12534 return;
12535 }
12536
12537 if (init && !init_decl)
12538 {
12539 tree c;
12540 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12541 {
12542 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12543 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12544 && OMP_CLAUSE_DECL (c) == decl)
12545 break;
12546 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12547 && OMP_CLAUSE_DECL (c) == decl)
12548 error ("iteration variable %qD should not be firstprivate", decl);
12549 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12550 && OMP_CLAUSE_DECL (c) == decl)
12551 error ("iteration variable %qD should not be reduction", decl);
12552 }
12553 if (c == NULL)
12554 {
12555 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12556 OMP_CLAUSE_DECL (c) = decl;
12557 c = finish_omp_clauses (c);
12558 if (c)
12559 {
12560 OMP_CLAUSE_CHAIN (c) = *clauses;
12561 *clauses = c;
12562 }
12563 }
12564 }
12565 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12566 if (COMPARISON_CLASS_P (cond))
12567 cond = build2 (TREE_CODE (cond), boolean_type_node,
12568 RECUR (TREE_OPERAND (cond, 0)),
12569 RECUR (TREE_OPERAND (cond, 1)));
12570 else
12571 cond = RECUR (cond);
12572 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12573 switch (TREE_CODE (incr))
12574 {
12575 case PREINCREMENT_EXPR:
12576 case PREDECREMENT_EXPR:
12577 case POSTINCREMENT_EXPR:
12578 case POSTDECREMENT_EXPR:
12579 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12580 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12581 break;
12582 case MODIFY_EXPR:
12583 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12584 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12585 {
12586 tree rhs = TREE_OPERAND (incr, 1);
12587 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12588 RECUR (TREE_OPERAND (incr, 0)),
12589 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12590 RECUR (TREE_OPERAND (rhs, 0)),
12591 RECUR (TREE_OPERAND (rhs, 1))));
12592 }
12593 else
12594 incr = RECUR (incr);
12595 break;
12596 case MODOP_EXPR:
12597 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12598 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12599 {
12600 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12601 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12602 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12603 TREE_TYPE (decl), lhs,
12604 RECUR (TREE_OPERAND (incr, 2))));
12605 }
12606 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12607 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12608 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12609 {
12610 tree rhs = TREE_OPERAND (incr, 2);
12611 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12612 RECUR (TREE_OPERAND (incr, 0)),
12613 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12614 RECUR (TREE_OPERAND (rhs, 0)),
12615 RECUR (TREE_OPERAND (rhs, 1))));
12616 }
12617 else
12618 incr = RECUR (incr);
12619 break;
12620 default:
12621 incr = RECUR (incr);
12622 break;
12623 }
12624
12625 TREE_VEC_ELT (declv, i) = decl;
12626 TREE_VEC_ELT (initv, i) = init;
12627 TREE_VEC_ELT (condv, i) = cond;
12628 TREE_VEC_ELT (incrv, i) = incr;
12629 #undef RECUR
12630 }
12631
12632 /* Like tsubst_copy for expressions, etc. but also does semantic
12633 processing. */
12634
12635 static tree
12636 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12637 bool integral_constant_expression_p)
12638 {
12639 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12640 #define RECUR(NODE) \
12641 tsubst_expr ((NODE), args, complain, in_decl, \
12642 integral_constant_expression_p)
12643
12644 tree stmt, tmp;
12645 tree r;
12646 location_t loc;
12647
12648 if (t == NULL_TREE || t == error_mark_node)
12649 return t;
12650
12651 loc = input_location;
12652 if (EXPR_HAS_LOCATION (t))
12653 input_location = EXPR_LOCATION (t);
12654 if (STATEMENT_CODE_P (TREE_CODE (t)))
12655 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12656
12657 switch (TREE_CODE (t))
12658 {
12659 case STATEMENT_LIST:
12660 {
12661 tree_stmt_iterator i;
12662 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12663 RECUR (tsi_stmt (i));
12664 break;
12665 }
12666
12667 case CTOR_INITIALIZER:
12668 finish_mem_initializers (tsubst_initializer_list
12669 (TREE_OPERAND (t, 0), args));
12670 break;
12671
12672 case RETURN_EXPR:
12673 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12674 break;
12675
12676 case EXPR_STMT:
12677 tmp = RECUR (EXPR_STMT_EXPR (t));
12678 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12679 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12680 else
12681 finish_expr_stmt (tmp);
12682 break;
12683
12684 case USING_STMT:
12685 do_using_directive (USING_STMT_NAMESPACE (t));
12686 break;
12687
12688 case DECL_EXPR:
12689 {
12690 tree decl, pattern_decl;
12691 tree init;
12692
12693 pattern_decl = decl = DECL_EXPR_DECL (t);
12694 if (TREE_CODE (decl) == LABEL_DECL)
12695 finish_label_decl (DECL_NAME (decl));
12696 else if (TREE_CODE (decl) == USING_DECL)
12697 {
12698 tree scope = USING_DECL_SCOPE (decl);
12699 tree name = DECL_NAME (decl);
12700 tree decl;
12701
12702 scope = tsubst (scope, args, complain, in_decl);
12703 decl = lookup_qualified_name (scope, name,
12704 /*is_type_p=*/false,
12705 /*complain=*/false);
12706 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12707 qualified_name_lookup_error (scope, name, decl, input_location);
12708 else
12709 do_local_using_decl (decl, scope, name);
12710 }
12711 else
12712 {
12713 init = DECL_INITIAL (decl);
12714 decl = tsubst (decl, args, complain, in_decl);
12715 if (decl != error_mark_node)
12716 {
12717 /* By marking the declaration as instantiated, we avoid
12718 trying to instantiate it. Since instantiate_decl can't
12719 handle local variables, and since we've already done
12720 all that needs to be done, that's the right thing to
12721 do. */
12722 if (TREE_CODE (decl) == VAR_DECL)
12723 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12724 if (TREE_CODE (decl) == VAR_DECL
12725 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12726 /* Anonymous aggregates are a special case. */
12727 finish_anon_union (decl);
12728 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
12729 {
12730 DECL_CONTEXT (decl) = current_function_decl;
12731 if (DECL_NAME (decl) == this_identifier)
12732 {
12733 tree lam = DECL_CONTEXT (current_function_decl);
12734 lam = CLASSTYPE_LAMBDA_EXPR (lam);
12735 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
12736 }
12737 insert_capture_proxy (decl);
12738 }
12739 else if (DECL_IMPLICIT_TYPEDEF_P (t))
12740 /* We already did a pushtag. */;
12741 else
12742 {
12743 int const_init = false;
12744 maybe_push_decl (decl);
12745 if (TREE_CODE (decl) == VAR_DECL
12746 && DECL_PRETTY_FUNCTION_P (decl))
12747 {
12748 /* For __PRETTY_FUNCTION__ we have to adjust the
12749 initializer. */
12750 const char *const name
12751 = cxx_printable_name (current_function_decl, 2);
12752 init = cp_fname_init (name, &TREE_TYPE (decl));
12753 }
12754 else
12755 {
12756 tree t = RECUR (init);
12757
12758 if (init && !t)
12759 {
12760 /* If we had an initializer but it
12761 instantiated to nothing,
12762 value-initialize the object. This will
12763 only occur when the initializer was a
12764 pack expansion where the parameter packs
12765 used in that expansion were of length
12766 zero. */
12767 init = build_value_init (TREE_TYPE (decl),
12768 complain);
12769 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12770 init = get_target_expr_sfinae (init, complain);
12771 }
12772 else
12773 init = t;
12774 }
12775
12776 if (TREE_CODE (decl) == VAR_DECL)
12777 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12778 (pattern_decl));
12779 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12780 }
12781 }
12782 }
12783
12784 break;
12785 }
12786
12787 case FOR_STMT:
12788 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12789 RECUR (FOR_INIT_STMT (t));
12790 finish_for_init_stmt (stmt);
12791 tmp = RECUR (FOR_COND (t));
12792 finish_for_cond (tmp, stmt);
12793 tmp = RECUR (FOR_EXPR (t));
12794 finish_for_expr (tmp, stmt);
12795 RECUR (FOR_BODY (t));
12796 finish_for_stmt (stmt);
12797 break;
12798
12799 case RANGE_FOR_STMT:
12800 {
12801 tree decl, expr;
12802 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12803 decl = RANGE_FOR_DECL (t);
12804 decl = tsubst (decl, args, complain, in_decl);
12805 maybe_push_decl (decl);
12806 expr = RECUR (RANGE_FOR_EXPR (t));
12807 stmt = cp_convert_range_for (stmt, decl, expr);
12808 RECUR (RANGE_FOR_BODY (t));
12809 finish_for_stmt (stmt);
12810 }
12811 break;
12812
12813 case WHILE_STMT:
12814 stmt = begin_while_stmt ();
12815 tmp = RECUR (WHILE_COND (t));
12816 finish_while_stmt_cond (tmp, stmt);
12817 RECUR (WHILE_BODY (t));
12818 finish_while_stmt (stmt);
12819 break;
12820
12821 case DO_STMT:
12822 stmt = begin_do_stmt ();
12823 RECUR (DO_BODY (t));
12824 finish_do_body (stmt);
12825 tmp = RECUR (DO_COND (t));
12826 finish_do_stmt (tmp, stmt);
12827 break;
12828
12829 case IF_STMT:
12830 stmt = begin_if_stmt ();
12831 tmp = RECUR (IF_COND (t));
12832 finish_if_stmt_cond (tmp, stmt);
12833 RECUR (THEN_CLAUSE (t));
12834 finish_then_clause (stmt);
12835
12836 if (ELSE_CLAUSE (t))
12837 {
12838 begin_else_clause (stmt);
12839 RECUR (ELSE_CLAUSE (t));
12840 finish_else_clause (stmt);
12841 }
12842
12843 finish_if_stmt (stmt);
12844 break;
12845
12846 case BIND_EXPR:
12847 if (BIND_EXPR_BODY_BLOCK (t))
12848 stmt = begin_function_body ();
12849 else
12850 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12851 ? BCS_TRY_BLOCK : 0);
12852
12853 RECUR (BIND_EXPR_BODY (t));
12854
12855 if (BIND_EXPR_BODY_BLOCK (t))
12856 finish_function_body (stmt);
12857 else
12858 finish_compound_stmt (stmt);
12859 break;
12860
12861 case BREAK_STMT:
12862 finish_break_stmt ();
12863 break;
12864
12865 case CONTINUE_STMT:
12866 finish_continue_stmt ();
12867 break;
12868
12869 case SWITCH_STMT:
12870 stmt = begin_switch_stmt ();
12871 tmp = RECUR (SWITCH_STMT_COND (t));
12872 finish_switch_cond (tmp, stmt);
12873 RECUR (SWITCH_STMT_BODY (t));
12874 finish_switch_stmt (stmt);
12875 break;
12876
12877 case CASE_LABEL_EXPR:
12878 finish_case_label (EXPR_LOCATION (t),
12879 RECUR (CASE_LOW (t)),
12880 RECUR (CASE_HIGH (t)));
12881 break;
12882
12883 case LABEL_EXPR:
12884 {
12885 tree decl = LABEL_EXPR_LABEL (t);
12886 tree label;
12887
12888 label = finish_label_stmt (DECL_NAME (decl));
12889 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12890 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12891 }
12892 break;
12893
12894 case GOTO_EXPR:
12895 tmp = GOTO_DESTINATION (t);
12896 if (TREE_CODE (tmp) != LABEL_DECL)
12897 /* Computed goto's must be tsubst'd into. On the other hand,
12898 non-computed gotos must not be; the identifier in question
12899 will have no binding. */
12900 tmp = RECUR (tmp);
12901 else
12902 tmp = DECL_NAME (tmp);
12903 finish_goto_stmt (tmp);
12904 break;
12905
12906 case ASM_EXPR:
12907 tmp = finish_asm_stmt
12908 (ASM_VOLATILE_P (t),
12909 RECUR (ASM_STRING (t)),
12910 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12911 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12912 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12913 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12914 {
12915 tree asm_expr = tmp;
12916 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12917 asm_expr = TREE_OPERAND (asm_expr, 0);
12918 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12919 }
12920 break;
12921
12922 case TRY_BLOCK:
12923 if (CLEANUP_P (t))
12924 {
12925 stmt = begin_try_block ();
12926 RECUR (TRY_STMTS (t));
12927 finish_cleanup_try_block (stmt);
12928 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12929 }
12930 else
12931 {
12932 tree compound_stmt = NULL_TREE;
12933
12934 if (FN_TRY_BLOCK_P (t))
12935 stmt = begin_function_try_block (&compound_stmt);
12936 else
12937 stmt = begin_try_block ();
12938
12939 RECUR (TRY_STMTS (t));
12940
12941 if (FN_TRY_BLOCK_P (t))
12942 finish_function_try_block (stmt);
12943 else
12944 finish_try_block (stmt);
12945
12946 RECUR (TRY_HANDLERS (t));
12947 if (FN_TRY_BLOCK_P (t))
12948 finish_function_handler_sequence (stmt, compound_stmt);
12949 else
12950 finish_handler_sequence (stmt);
12951 }
12952 break;
12953
12954 case HANDLER:
12955 {
12956 tree decl = HANDLER_PARMS (t);
12957
12958 if (decl)
12959 {
12960 decl = tsubst (decl, args, complain, in_decl);
12961 /* Prevent instantiate_decl from trying to instantiate
12962 this variable. We've already done all that needs to be
12963 done. */
12964 if (decl != error_mark_node)
12965 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12966 }
12967 stmt = begin_handler ();
12968 finish_handler_parms (decl, stmt);
12969 RECUR (HANDLER_BODY (t));
12970 finish_handler (stmt);
12971 }
12972 break;
12973
12974 case TAG_DEFN:
12975 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12976 if (CLASS_TYPE_P (tmp))
12977 {
12978 /* Local classes are not independent templates; they are
12979 instantiated along with their containing function. And this
12980 way we don't have to deal with pushing out of one local class
12981 to instantiate a member of another local class. */
12982 tree fn;
12983 /* Closures are handled by the LAMBDA_EXPR. */
12984 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
12985 complete_type (tmp);
12986 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
12987 if (!DECL_ARTIFICIAL (fn))
12988 instantiate_decl (fn, /*defer_ok*/0, /*expl_inst_class*/false);
12989 }
12990 break;
12991
12992 case STATIC_ASSERT:
12993 {
12994 tree condition;
12995
12996 ++c_inhibit_evaluation_warnings;
12997 condition =
12998 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12999 args,
13000 complain, in_decl,
13001 /*integral_constant_expression_p=*/true);
13002 --c_inhibit_evaluation_warnings;
13003
13004 finish_static_assert (condition,
13005 STATIC_ASSERT_MESSAGE (t),
13006 STATIC_ASSERT_SOURCE_LOCATION (t),
13007 /*member_p=*/false);
13008 }
13009 break;
13010
13011 case OMP_PARALLEL:
13012 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
13013 args, complain, in_decl);
13014 stmt = begin_omp_parallel ();
13015 RECUR (OMP_PARALLEL_BODY (t));
13016 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
13017 = OMP_PARALLEL_COMBINED (t);
13018 break;
13019
13020 case OMP_TASK:
13021 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
13022 args, complain, in_decl);
13023 stmt = begin_omp_task ();
13024 RECUR (OMP_TASK_BODY (t));
13025 finish_omp_task (tmp, stmt);
13026 break;
13027
13028 case OMP_FOR:
13029 {
13030 tree clauses, body, pre_body;
13031 tree declv, initv, condv, incrv;
13032 int i;
13033
13034 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
13035 args, complain, in_decl);
13036 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13037 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13038 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13039 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
13040
13041 stmt = begin_omp_structured_block ();
13042
13043 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
13044 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
13045 &clauses, args, complain, in_decl,
13046 integral_constant_expression_p);
13047
13048 pre_body = push_stmt_list ();
13049 RECUR (OMP_FOR_PRE_BODY (t));
13050 pre_body = pop_stmt_list (pre_body);
13051
13052 body = push_stmt_list ();
13053 RECUR (OMP_FOR_BODY (t));
13054 body = pop_stmt_list (body);
13055
13056 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
13057 body, pre_body, clauses);
13058
13059 add_stmt (finish_omp_structured_block (stmt));
13060 }
13061 break;
13062
13063 case OMP_SECTIONS:
13064 case OMP_SINGLE:
13065 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
13066 stmt = push_stmt_list ();
13067 RECUR (OMP_BODY (t));
13068 stmt = pop_stmt_list (stmt);
13069
13070 t = copy_node (t);
13071 OMP_BODY (t) = stmt;
13072 OMP_CLAUSES (t) = tmp;
13073 add_stmt (t);
13074 break;
13075
13076 case OMP_SECTION:
13077 case OMP_CRITICAL:
13078 case OMP_MASTER:
13079 case OMP_ORDERED:
13080 stmt = push_stmt_list ();
13081 RECUR (OMP_BODY (t));
13082 stmt = pop_stmt_list (stmt);
13083
13084 t = copy_node (t);
13085 OMP_BODY (t) = stmt;
13086 add_stmt (t);
13087 break;
13088
13089 case OMP_ATOMIC:
13090 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
13091 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
13092 {
13093 tree op1 = TREE_OPERAND (t, 1);
13094 tree rhs1 = NULL_TREE;
13095 tree lhs, rhs;
13096 if (TREE_CODE (op1) == COMPOUND_EXPR)
13097 {
13098 rhs1 = RECUR (TREE_OPERAND (op1, 0));
13099 op1 = TREE_OPERAND (op1, 1);
13100 }
13101 lhs = RECUR (TREE_OPERAND (op1, 0));
13102 rhs = RECUR (TREE_OPERAND (op1, 1));
13103 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
13104 NULL_TREE, NULL_TREE, rhs1);
13105 }
13106 else
13107 {
13108 tree op1 = TREE_OPERAND (t, 1);
13109 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13110 tree rhs1 = NULL_TREE;
13111 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13112 enum tree_code opcode = NOP_EXPR;
13113 if (code == OMP_ATOMIC_READ)
13114 {
13115 v = RECUR (TREE_OPERAND (op1, 0));
13116 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13117 }
13118 else if (code == OMP_ATOMIC_CAPTURE_OLD
13119 || code == OMP_ATOMIC_CAPTURE_NEW)
13120 {
13121 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13122 v = RECUR (TREE_OPERAND (op1, 0));
13123 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13124 if (TREE_CODE (op11) == COMPOUND_EXPR)
13125 {
13126 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13127 op11 = TREE_OPERAND (op11, 1);
13128 }
13129 lhs = RECUR (TREE_OPERAND (op11, 0));
13130 rhs = RECUR (TREE_OPERAND (op11, 1));
13131 opcode = TREE_CODE (op11);
13132 }
13133 else
13134 {
13135 code = OMP_ATOMIC;
13136 lhs = RECUR (TREE_OPERAND (op1, 0));
13137 rhs = RECUR (TREE_OPERAND (op1, 1));
13138 }
13139 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
13140 }
13141 break;
13142
13143 case TRANSACTION_EXPR:
13144 {
13145 int flags = 0;
13146 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13147 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13148
13149 if (TRANSACTION_EXPR_IS_STMT (t))
13150 {
13151 tree body = TRANSACTION_EXPR_BODY (t);
13152 tree noex = NULL_TREE;
13153 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13154 {
13155 noex = MUST_NOT_THROW_COND (body);
13156 if (noex == NULL_TREE)
13157 noex = boolean_true_node;
13158 body = TREE_OPERAND (body, 0);
13159 }
13160 stmt = begin_transaction_stmt (input_location, NULL, flags);
13161 RECUR (body);
13162 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13163 }
13164 else
13165 {
13166 stmt = build_transaction_expr (EXPR_LOCATION (t),
13167 RECUR (TRANSACTION_EXPR_BODY (t)),
13168 flags, NULL_TREE);
13169 RETURN (stmt);
13170 }
13171 }
13172 break;
13173
13174 case MUST_NOT_THROW_EXPR:
13175 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13176 RECUR (MUST_NOT_THROW_COND (t))));
13177
13178 case EXPR_PACK_EXPANSION:
13179 error ("invalid use of pack expansion expression");
13180 RETURN (error_mark_node);
13181
13182 case NONTYPE_ARGUMENT_PACK:
13183 error ("use %<...%> to expand argument pack");
13184 RETURN (error_mark_node);
13185
13186 case COMPOUND_EXPR:
13187 tmp = RECUR (TREE_OPERAND (t, 0));
13188 if (tmp == NULL_TREE)
13189 /* If the first operand was a statement, we're done with it. */
13190 RETURN (RECUR (TREE_OPERAND (t, 1)));
13191 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
13192 RECUR (TREE_OPERAND (t, 1)),
13193 complain));
13194
13195 default:
13196 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13197
13198 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
13199 /*function_p=*/false,
13200 integral_constant_expression_p));
13201 }
13202
13203 RETURN (NULL_TREE);
13204 out:
13205 input_location = loc;
13206 return r;
13207 #undef RECUR
13208 #undef RETURN
13209 }
13210
13211 /* T is a postfix-expression that is not being used in a function
13212 call. Return the substituted version of T. */
13213
13214 static tree
13215 tsubst_non_call_postfix_expression (tree t, tree args,
13216 tsubst_flags_t complain,
13217 tree in_decl)
13218 {
13219 if (TREE_CODE (t) == SCOPE_REF)
13220 t = tsubst_qualified_id (t, args, complain, in_decl,
13221 /*done=*/false, /*address_p=*/false);
13222 else
13223 t = tsubst_copy_and_build (t, args, complain, in_decl,
13224 /*function_p=*/false,
13225 /*integral_constant_expression_p=*/false);
13226
13227 return t;
13228 }
13229
13230 /* Like tsubst but deals with expressions and performs semantic
13231 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13232
13233 tree
13234 tsubst_copy_and_build (tree t,
13235 tree args,
13236 tsubst_flags_t complain,
13237 tree in_decl,
13238 bool function_p,
13239 bool integral_constant_expression_p)
13240 {
13241 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13242 #define RECUR(NODE) \
13243 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13244 /*function_p=*/false, \
13245 integral_constant_expression_p)
13246
13247 tree retval, op1;
13248 location_t loc;
13249
13250 if (t == NULL_TREE || t == error_mark_node)
13251 return t;
13252
13253 loc = input_location;
13254 if (EXPR_HAS_LOCATION (t))
13255 input_location = EXPR_LOCATION (t);
13256
13257 switch (TREE_CODE (t))
13258 {
13259 case USING_DECL:
13260 t = DECL_NAME (t);
13261 /* Fall through. */
13262 case IDENTIFIER_NODE:
13263 {
13264 tree decl;
13265 cp_id_kind idk;
13266 bool non_integral_constant_expression_p;
13267 const char *error_msg;
13268
13269 if (IDENTIFIER_TYPENAME_P (t))
13270 {
13271 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13272 t = mangle_conv_op_name_for_type (new_type);
13273 }
13274
13275 /* Look up the name. */
13276 decl = lookup_name (t);
13277
13278 /* By convention, expressions use ERROR_MARK_NODE to indicate
13279 failure, not NULL_TREE. */
13280 if (decl == NULL_TREE)
13281 decl = error_mark_node;
13282
13283 decl = finish_id_expression (t, decl, NULL_TREE,
13284 &idk,
13285 integral_constant_expression_p,
13286 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
13287 &non_integral_constant_expression_p,
13288 /*template_p=*/false,
13289 /*done=*/true,
13290 /*address_p=*/false,
13291 /*template_arg_p=*/false,
13292 &error_msg,
13293 input_location);
13294 if (error_msg)
13295 error (error_msg);
13296 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
13297 {
13298 if (complain & tf_error)
13299 unqualified_name_lookup_error (decl);
13300 decl = error_mark_node;
13301 }
13302 RETURN (decl);
13303 }
13304
13305 case TEMPLATE_ID_EXPR:
13306 {
13307 tree object;
13308 tree templ = RECUR (TREE_OPERAND (t, 0));
13309 tree targs = TREE_OPERAND (t, 1);
13310
13311 if (targs)
13312 targs = tsubst_template_args (targs, args, complain, in_decl);
13313
13314 if (TREE_CODE (templ) == COMPONENT_REF)
13315 {
13316 object = TREE_OPERAND (templ, 0);
13317 templ = TREE_OPERAND (templ, 1);
13318 }
13319 else
13320 object = NULL_TREE;
13321 templ = lookup_template_function (templ, targs);
13322
13323 if (object)
13324 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
13325 object, templ, NULL_TREE));
13326 else
13327 RETURN (baselink_for_fns (templ));
13328 }
13329
13330 case INDIRECT_REF:
13331 {
13332 tree r = RECUR (TREE_OPERAND (t, 0));
13333
13334 if (REFERENCE_REF_P (t))
13335 {
13336 /* A type conversion to reference type will be enclosed in
13337 such an indirect ref, but the substitution of the cast
13338 will have also added such an indirect ref. */
13339 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13340 r = convert_from_reference (r);
13341 }
13342 else
13343 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, complain);
13344 RETURN (r);
13345 }
13346
13347 case NOP_EXPR:
13348 RETURN (build_nop
13349 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13350 RECUR (TREE_OPERAND (t, 0))));
13351
13352 case IMPLICIT_CONV_EXPR:
13353 {
13354 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13355 tree expr = RECUR (TREE_OPERAND (t, 0));
13356 int flags = LOOKUP_IMPLICIT;
13357 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13358 flags = LOOKUP_NORMAL;
13359 RETURN (perform_implicit_conversion_flags (type, expr, complain,
13360 flags));
13361 }
13362
13363 case CONVERT_EXPR:
13364 RETURN (build1
13365 (CONVERT_EXPR,
13366 tsubst (TREE_TYPE (t), args, complain, in_decl),
13367 RECUR (TREE_OPERAND (t, 0))));
13368
13369 case CAST_EXPR:
13370 case REINTERPRET_CAST_EXPR:
13371 case CONST_CAST_EXPR:
13372 case DYNAMIC_CAST_EXPR:
13373 case STATIC_CAST_EXPR:
13374 {
13375 tree type;
13376 tree op, r = NULL_TREE;
13377
13378 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13379 if (integral_constant_expression_p
13380 && !cast_valid_in_integral_constant_expression_p (type))
13381 {
13382 if (complain & tf_error)
13383 error ("a cast to a type other than an integral or "
13384 "enumeration type cannot appear in a constant-expression");
13385 RETURN (error_mark_node);
13386 }
13387
13388 op = RECUR (TREE_OPERAND (t, 0));
13389
13390 ++c_inhibit_evaluation_warnings;
13391 switch (TREE_CODE (t))
13392 {
13393 case CAST_EXPR:
13394 r = build_functional_cast (type, op, complain);
13395 break;
13396 case REINTERPRET_CAST_EXPR:
13397 r = build_reinterpret_cast (type, op, complain);
13398 break;
13399 case CONST_CAST_EXPR:
13400 r = build_const_cast (type, op, complain);
13401 break;
13402 case DYNAMIC_CAST_EXPR:
13403 r = build_dynamic_cast (type, op, complain);
13404 break;
13405 case STATIC_CAST_EXPR:
13406 r = build_static_cast (type, op, complain);
13407 break;
13408 default:
13409 gcc_unreachable ();
13410 }
13411 --c_inhibit_evaluation_warnings;
13412
13413 RETURN (r);
13414 }
13415
13416 case POSTDECREMENT_EXPR:
13417 case POSTINCREMENT_EXPR:
13418 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13419 args, complain, in_decl);
13420 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, complain));
13421
13422 case PREDECREMENT_EXPR:
13423 case PREINCREMENT_EXPR:
13424 case NEGATE_EXPR:
13425 case BIT_NOT_EXPR:
13426 case ABS_EXPR:
13427 case TRUTH_NOT_EXPR:
13428 case UNARY_PLUS_EXPR: /* Unary + */
13429 case REALPART_EXPR:
13430 case IMAGPART_EXPR:
13431 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
13432 RECUR (TREE_OPERAND (t, 0)), complain));
13433
13434 case FIX_TRUNC_EXPR:
13435 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
13436 0, complain));
13437
13438 case ADDR_EXPR:
13439 op1 = TREE_OPERAND (t, 0);
13440 if (TREE_CODE (op1) == LABEL_DECL)
13441 RETURN (finish_label_address_expr (DECL_NAME (op1),
13442 EXPR_LOCATION (op1)));
13443 if (TREE_CODE (op1) == SCOPE_REF)
13444 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13445 /*done=*/true, /*address_p=*/true);
13446 else
13447 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13448 in_decl);
13449 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, complain));
13450
13451 case PLUS_EXPR:
13452 case MINUS_EXPR:
13453 case MULT_EXPR:
13454 case TRUNC_DIV_EXPR:
13455 case CEIL_DIV_EXPR:
13456 case FLOOR_DIV_EXPR:
13457 case ROUND_DIV_EXPR:
13458 case EXACT_DIV_EXPR:
13459 case BIT_AND_EXPR:
13460 case BIT_IOR_EXPR:
13461 case BIT_XOR_EXPR:
13462 case TRUNC_MOD_EXPR:
13463 case FLOOR_MOD_EXPR:
13464 case TRUTH_ANDIF_EXPR:
13465 case TRUTH_ORIF_EXPR:
13466 case TRUTH_AND_EXPR:
13467 case TRUTH_OR_EXPR:
13468 case RSHIFT_EXPR:
13469 case LSHIFT_EXPR:
13470 case RROTATE_EXPR:
13471 case LROTATE_EXPR:
13472 case EQ_EXPR:
13473 case NE_EXPR:
13474 case MAX_EXPR:
13475 case MIN_EXPR:
13476 case LE_EXPR:
13477 case GE_EXPR:
13478 case LT_EXPR:
13479 case GT_EXPR:
13480 case MEMBER_REF:
13481 case DOTSTAR_EXPR:
13482 {
13483 tree r;
13484
13485 ++c_inhibit_evaluation_warnings;
13486
13487 r = build_x_binary_op
13488 (input_location, TREE_CODE (t),
13489 RECUR (TREE_OPERAND (t, 0)),
13490 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13491 ? ERROR_MARK
13492 : TREE_CODE (TREE_OPERAND (t, 0))),
13493 RECUR (TREE_OPERAND (t, 1)),
13494 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13495 ? ERROR_MARK
13496 : TREE_CODE (TREE_OPERAND (t, 1))),
13497 /*overload=*/NULL,
13498 complain);
13499 if (EXPR_P (r) && TREE_NO_WARNING (t))
13500 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13501
13502 --c_inhibit_evaluation_warnings;
13503
13504 RETURN (r);
13505 }
13506
13507 case SCOPE_REF:
13508 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13509 /*address_p=*/false));
13510 case ARRAY_REF:
13511 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13512 args, complain, in_decl);
13513 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
13514 RECUR (TREE_OPERAND (t, 1)), complain));
13515
13516 case SIZEOF_EXPR:
13517 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13518 RETURN (tsubst_copy (t, args, complain, in_decl));
13519 /* Fall through */
13520
13521 case ALIGNOF_EXPR:
13522 {
13523 tree r;
13524
13525 op1 = TREE_OPERAND (t, 0);
13526 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
13527 op1 = TREE_TYPE (op1);
13528 if (!args)
13529 {
13530 /* When there are no ARGS, we are trying to evaluate a
13531 non-dependent expression from the parser. Trying to do
13532 the substitutions may not work. */
13533 if (!TYPE_P (op1))
13534 op1 = TREE_TYPE (op1);
13535 }
13536 else
13537 {
13538 ++cp_unevaluated_operand;
13539 ++c_inhibit_evaluation_warnings;
13540 if (TYPE_P (op1))
13541 op1 = tsubst (op1, args, complain, in_decl);
13542 else
13543 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13544 /*function_p=*/false,
13545 /*integral_constant_expression_p=*/
13546 false);
13547 --cp_unevaluated_operand;
13548 --c_inhibit_evaluation_warnings;
13549 }
13550 if (TYPE_P (op1))
13551 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13552 complain & tf_error);
13553 else
13554 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13555 complain & tf_error);
13556 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
13557 {
13558 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
13559 {
13560 if (TYPE_P (op1))
13561 {
13562 r = build_min (SIZEOF_EXPR, size_type_node,
13563 build1 (NOP_EXPR, op1, error_mark_node));
13564 SIZEOF_EXPR_TYPE_P (r) = 1;
13565 }
13566 else
13567 r = build_min (SIZEOF_EXPR, size_type_node, op1);
13568 TREE_SIDE_EFFECTS (r) = 0;
13569 TREE_READONLY (r) = 1;
13570 }
13571 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
13572 }
13573 RETURN (r);
13574 }
13575
13576 case AT_ENCODE_EXPR:
13577 {
13578 op1 = TREE_OPERAND (t, 0);
13579 ++cp_unevaluated_operand;
13580 ++c_inhibit_evaluation_warnings;
13581 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13582 /*function_p=*/false,
13583 /*integral_constant_expression_p=*/false);
13584 --cp_unevaluated_operand;
13585 --c_inhibit_evaluation_warnings;
13586 RETURN (objc_build_encode_expr (op1));
13587 }
13588
13589 case NOEXCEPT_EXPR:
13590 op1 = TREE_OPERAND (t, 0);
13591 ++cp_unevaluated_operand;
13592 ++c_inhibit_evaluation_warnings;
13593 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13594 /*function_p=*/false,
13595 /*integral_constant_expression_p=*/false);
13596 --cp_unevaluated_operand;
13597 --c_inhibit_evaluation_warnings;
13598 RETURN (finish_noexcept_expr (op1, complain));
13599
13600 case MODOP_EXPR:
13601 {
13602 tree r = build_x_modify_expr
13603 (EXPR_LOCATION (t),
13604 RECUR (TREE_OPERAND (t, 0)),
13605 TREE_CODE (TREE_OPERAND (t, 1)),
13606 RECUR (TREE_OPERAND (t, 2)),
13607 complain);
13608 /* TREE_NO_WARNING must be set if either the expression was
13609 parenthesized or it uses an operator such as >>= rather
13610 than plain assignment. In the former case, it was already
13611 set and must be copied. In the latter case,
13612 build_x_modify_expr sets it and it must not be reset
13613 here. */
13614 if (TREE_NO_WARNING (t))
13615 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13616 RETURN (r);
13617 }
13618
13619 case ARROW_EXPR:
13620 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13621 args, complain, in_decl);
13622 /* Remember that there was a reference to this entity. */
13623 if (DECL_P (op1))
13624 mark_used (op1);
13625 RETURN (build_x_arrow (input_location, op1, complain));
13626
13627 case NEW_EXPR:
13628 {
13629 tree placement = RECUR (TREE_OPERAND (t, 0));
13630 tree init = RECUR (TREE_OPERAND (t, 3));
13631 vec<tree, va_gc> *placement_vec;
13632 vec<tree, va_gc> *init_vec;
13633 tree ret;
13634
13635 if (placement == NULL_TREE)
13636 placement_vec = NULL;
13637 else
13638 {
13639 placement_vec = make_tree_vector ();
13640 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13641 vec_safe_push (placement_vec, TREE_VALUE (placement));
13642 }
13643
13644 /* If there was an initializer in the original tree, but it
13645 instantiated to an empty list, then we should pass a
13646 non-NULL empty vector to tell build_new that it was an
13647 empty initializer() rather than no initializer. This can
13648 only happen when the initializer is a pack expansion whose
13649 parameter packs are of length zero. */
13650 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13651 init_vec = NULL;
13652 else
13653 {
13654 init_vec = make_tree_vector ();
13655 if (init == void_zero_node)
13656 gcc_assert (init_vec != NULL);
13657 else
13658 {
13659 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13660 vec_safe_push (init_vec, TREE_VALUE (init));
13661 }
13662 }
13663
13664 ret = build_new (&placement_vec,
13665 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13666 RECUR (TREE_OPERAND (t, 2)),
13667 &init_vec,
13668 NEW_EXPR_USE_GLOBAL (t),
13669 complain);
13670
13671 if (placement_vec != NULL)
13672 release_tree_vector (placement_vec);
13673 if (init_vec != NULL)
13674 release_tree_vector (init_vec);
13675
13676 RETURN (ret);
13677 }
13678
13679 case DELETE_EXPR:
13680 RETURN (delete_sanity
13681 (RECUR (TREE_OPERAND (t, 0)),
13682 RECUR (TREE_OPERAND (t, 1)),
13683 DELETE_EXPR_USE_VEC (t),
13684 DELETE_EXPR_USE_GLOBAL (t),
13685 complain));
13686
13687 case COMPOUND_EXPR:
13688 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
13689 RECUR (TREE_OPERAND (t, 0)),
13690 RECUR (TREE_OPERAND (t, 1)),
13691 complain));
13692
13693 case CALL_EXPR:
13694 {
13695 tree function;
13696 vec<tree, va_gc> *call_args;
13697 unsigned int nargs, i;
13698 bool qualified_p;
13699 bool koenig_p;
13700 tree ret;
13701
13702 function = CALL_EXPR_FN (t);
13703 /* When we parsed the expression, we determined whether or
13704 not Koenig lookup should be performed. */
13705 koenig_p = KOENIG_LOOKUP_P (t);
13706 if (TREE_CODE (function) == SCOPE_REF)
13707 {
13708 qualified_p = true;
13709 function = tsubst_qualified_id (function, args, complain, in_decl,
13710 /*done=*/false,
13711 /*address_p=*/false);
13712 }
13713 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13714 {
13715 /* Do nothing; calling tsubst_copy_and_build on an identifier
13716 would incorrectly perform unqualified lookup again.
13717
13718 Note that we can also have an IDENTIFIER_NODE if the earlier
13719 unqualified lookup found a member function; in that case
13720 koenig_p will be false and we do want to do the lookup
13721 again to find the instantiated member function.
13722
13723 FIXME but doing that causes c++/15272, so we need to stop
13724 using IDENTIFIER_NODE in that situation. */
13725 qualified_p = false;
13726 }
13727 else
13728 {
13729 if (TREE_CODE (function) == COMPONENT_REF)
13730 {
13731 tree op = TREE_OPERAND (function, 1);
13732
13733 qualified_p = (TREE_CODE (op) == SCOPE_REF
13734 || (BASELINK_P (op)
13735 && BASELINK_QUALIFIED_P (op)));
13736 }
13737 else
13738 qualified_p = false;
13739
13740 function = tsubst_copy_and_build (function, args, complain,
13741 in_decl,
13742 !qualified_p,
13743 integral_constant_expression_p);
13744
13745 if (BASELINK_P (function))
13746 qualified_p = true;
13747 }
13748
13749 nargs = call_expr_nargs (t);
13750 call_args = make_tree_vector ();
13751 for (i = 0; i < nargs; ++i)
13752 {
13753 tree arg = CALL_EXPR_ARG (t, i);
13754
13755 if (!PACK_EXPANSION_P (arg))
13756 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
13757 else
13758 {
13759 /* Expand the pack expansion and push each entry onto
13760 CALL_ARGS. */
13761 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13762 if (TREE_CODE (arg) == TREE_VEC)
13763 {
13764 unsigned int len, j;
13765
13766 len = TREE_VEC_LENGTH (arg);
13767 for (j = 0; j < len; ++j)
13768 {
13769 tree value = TREE_VEC_ELT (arg, j);
13770 if (value != NULL_TREE)
13771 value = convert_from_reference (value);
13772 vec_safe_push (call_args, value);
13773 }
13774 }
13775 else
13776 {
13777 /* A partial substitution. Add one entry. */
13778 vec_safe_push (call_args, arg);
13779 }
13780 }
13781 }
13782
13783 /* We do not perform argument-dependent lookup if normal
13784 lookup finds a non-function, in accordance with the
13785 expected resolution of DR 218. */
13786 if (koenig_p
13787 && ((is_overloaded_fn (function)
13788 /* If lookup found a member function, the Koenig lookup is
13789 not appropriate, even if an unqualified-name was used
13790 to denote the function. */
13791 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13792 || TREE_CODE (function) == IDENTIFIER_NODE)
13793 /* Only do this when substitution turns a dependent call
13794 into a non-dependent call. */
13795 && type_dependent_expression_p_push (t)
13796 && !any_type_dependent_arguments_p (call_args))
13797 function = perform_koenig_lookup (function, call_args, false,
13798 tf_none);
13799
13800 if (TREE_CODE (function) == IDENTIFIER_NODE
13801 && !any_type_dependent_arguments_p (call_args))
13802 {
13803 if (koenig_p && (complain & tf_warning_or_error))
13804 {
13805 /* For backwards compatibility and good diagnostics, try
13806 the unqualified lookup again if we aren't in SFINAE
13807 context. */
13808 tree unq = (tsubst_copy_and_build
13809 (function, args, complain, in_decl, true,
13810 integral_constant_expression_p));
13811 if (unq == error_mark_node)
13812 RETURN (error_mark_node);
13813
13814 if (unq != function)
13815 {
13816 tree fn = unq;
13817 if (TREE_CODE (fn) == INDIRECT_REF)
13818 fn = TREE_OPERAND (fn, 0);
13819 if (TREE_CODE (fn) == COMPONENT_REF)
13820 fn = TREE_OPERAND (fn, 1);
13821 if (is_overloaded_fn (fn))
13822 fn = get_first_fn (fn);
13823 permerror (EXPR_LOC_OR_HERE (t),
13824 "%qD was not declared in this scope, "
13825 "and no declarations were found by "
13826 "argument-dependent lookup at the point "
13827 "of instantiation", function);
13828 if (!DECL_P (fn))
13829 /* Can't say anything more. */;
13830 else if (DECL_CLASS_SCOPE_P (fn))
13831 {
13832 inform (EXPR_LOC_OR_HERE (t),
13833 "declarations in dependent base %qT are "
13834 "not found by unqualified lookup",
13835 DECL_CLASS_CONTEXT (fn));
13836 if (current_class_ptr)
13837 inform (EXPR_LOC_OR_HERE (t),
13838 "use %<this->%D%> instead", function);
13839 else
13840 inform (EXPR_LOC_OR_HERE (t),
13841 "use %<%T::%D%> instead",
13842 current_class_name, function);
13843 }
13844 else
13845 inform (0, "%q+D declared here, later in the "
13846 "translation unit", fn);
13847 function = unq;
13848 }
13849 }
13850 if (TREE_CODE (function) == IDENTIFIER_NODE)
13851 {
13852 if (complain & tf_error)
13853 unqualified_name_lookup_error (function);
13854 release_tree_vector (call_args);
13855 RETURN (error_mark_node);
13856 }
13857 }
13858
13859 /* Remember that there was a reference to this entity. */
13860 if (DECL_P (function))
13861 mark_used (function);
13862
13863 if (TREE_CODE (function) == OFFSET_REF)
13864 ret = build_offset_ref_call_from_tree (function, &call_args,
13865 complain);
13866 else if (TREE_CODE (function) == COMPONENT_REF)
13867 {
13868 tree instance = TREE_OPERAND (function, 0);
13869 tree fn = TREE_OPERAND (function, 1);
13870
13871 if (processing_template_decl
13872 && (type_dependent_expression_p (instance)
13873 || (!BASELINK_P (fn)
13874 && TREE_CODE (fn) != FIELD_DECL)
13875 || type_dependent_expression_p (fn)
13876 || any_type_dependent_arguments_p (call_args)))
13877 ret = build_nt_call_vec (function, call_args);
13878 else if (!BASELINK_P (fn))
13879 ret = finish_call_expr (function, &call_args,
13880 /*disallow_virtual=*/false,
13881 /*koenig_p=*/false,
13882 complain);
13883 else
13884 ret = (build_new_method_call
13885 (instance, fn,
13886 &call_args, NULL_TREE,
13887 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
13888 /*fn_p=*/NULL,
13889 complain));
13890 }
13891 else
13892 ret = finish_call_expr (function, &call_args,
13893 /*disallow_virtual=*/qualified_p,
13894 koenig_p,
13895 complain);
13896
13897 release_tree_vector (call_args);
13898
13899 RETURN (ret);
13900 }
13901
13902 case COND_EXPR:
13903 {
13904 tree cond = RECUR (TREE_OPERAND (t, 0));
13905 tree exp1, exp2;
13906
13907 if (TREE_CODE (cond) == INTEGER_CST)
13908 {
13909 if (integer_zerop (cond))
13910 {
13911 ++c_inhibit_evaluation_warnings;
13912 exp1 = RECUR (TREE_OPERAND (t, 1));
13913 --c_inhibit_evaluation_warnings;
13914 exp2 = RECUR (TREE_OPERAND (t, 2));
13915 }
13916 else
13917 {
13918 exp1 = RECUR (TREE_OPERAND (t, 1));
13919 ++c_inhibit_evaluation_warnings;
13920 exp2 = RECUR (TREE_OPERAND (t, 2));
13921 --c_inhibit_evaluation_warnings;
13922 }
13923 }
13924 else
13925 {
13926 exp1 = RECUR (TREE_OPERAND (t, 1));
13927 exp2 = RECUR (TREE_OPERAND (t, 2));
13928 }
13929
13930 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
13931 cond, exp1, exp2, complain));
13932 }
13933
13934 case PSEUDO_DTOR_EXPR:
13935 RETURN (finish_pseudo_destructor_expr
13936 (RECUR (TREE_OPERAND (t, 0)),
13937 RECUR (TREE_OPERAND (t, 1)),
13938 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl)));
13939
13940 case TREE_LIST:
13941 {
13942 tree purpose, value, chain;
13943
13944 if (t == void_list_node)
13945 RETURN (t);
13946
13947 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13948 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13949 {
13950 /* We have pack expansions, so expand those and
13951 create a new list out of it. */
13952 tree purposevec = NULL_TREE;
13953 tree valuevec = NULL_TREE;
13954 tree chain;
13955 int i, len = -1;
13956
13957 /* Expand the argument expressions. */
13958 if (TREE_PURPOSE (t))
13959 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13960 complain, in_decl);
13961 if (TREE_VALUE (t))
13962 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13963 complain, in_decl);
13964
13965 /* Build the rest of the list. */
13966 chain = TREE_CHAIN (t);
13967 if (chain && chain != void_type_node)
13968 chain = RECUR (chain);
13969
13970 /* Determine the number of arguments. */
13971 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13972 {
13973 len = TREE_VEC_LENGTH (purposevec);
13974 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13975 }
13976 else if (TREE_CODE (valuevec) == TREE_VEC)
13977 len = TREE_VEC_LENGTH (valuevec);
13978 else
13979 {
13980 /* Since we only performed a partial substitution into
13981 the argument pack, we only RETURN (a single list
13982 node. */
13983 if (purposevec == TREE_PURPOSE (t)
13984 && valuevec == TREE_VALUE (t)
13985 && chain == TREE_CHAIN (t))
13986 RETURN (t);
13987
13988 RETURN (tree_cons (purposevec, valuevec, chain));
13989 }
13990
13991 /* Convert the argument vectors into a TREE_LIST */
13992 i = len;
13993 while (i > 0)
13994 {
13995 /* Grab the Ith values. */
13996 i--;
13997 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13998 : NULL_TREE;
13999 value
14000 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
14001 : NULL_TREE;
14002
14003 /* Build the list (backwards). */
14004 chain = tree_cons (purpose, value, chain);
14005 }
14006
14007 RETURN (chain);
14008 }
14009
14010 purpose = TREE_PURPOSE (t);
14011 if (purpose)
14012 purpose = RECUR (purpose);
14013 value = TREE_VALUE (t);
14014 if (value)
14015 value = RECUR (value);
14016 chain = TREE_CHAIN (t);
14017 if (chain && chain != void_type_node)
14018 chain = RECUR (chain);
14019 if (purpose == TREE_PURPOSE (t)
14020 && value == TREE_VALUE (t)
14021 && chain == TREE_CHAIN (t))
14022 RETURN (t);
14023 RETURN (tree_cons (purpose, value, chain));
14024 }
14025
14026 case COMPONENT_REF:
14027 {
14028 tree object;
14029 tree object_type;
14030 tree member;
14031
14032 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
14033 args, complain, in_decl);
14034 /* Remember that there was a reference to this entity. */
14035 if (DECL_P (object))
14036 mark_used (object);
14037 object_type = TREE_TYPE (object);
14038
14039 member = TREE_OPERAND (t, 1);
14040 if (BASELINK_P (member))
14041 member = tsubst_baselink (member,
14042 non_reference (TREE_TYPE (object)),
14043 args, complain, in_decl);
14044 else
14045 member = tsubst_copy (member, args, complain, in_decl);
14046 if (member == error_mark_node)
14047 RETURN (error_mark_node);
14048
14049 if (type_dependent_expression_p (object))
14050 /* We can't do much here. */;
14051 else if (!CLASS_TYPE_P (object_type))
14052 {
14053 if (scalarish_type_p (object_type))
14054 {
14055 tree s = NULL_TREE;
14056 tree dtor = member;
14057
14058 if (TREE_CODE (dtor) == SCOPE_REF)
14059 {
14060 s = TREE_OPERAND (dtor, 0);
14061 dtor = TREE_OPERAND (dtor, 1);
14062 }
14063 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
14064 {
14065 dtor = TREE_OPERAND (dtor, 0);
14066 if (TYPE_P (dtor))
14067 RETURN (finish_pseudo_destructor_expr (object, s, dtor));
14068 }
14069 }
14070 }
14071 else if (TREE_CODE (member) == SCOPE_REF
14072 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
14073 {
14074 /* Lookup the template functions now that we know what the
14075 scope is. */
14076 tree scope = TREE_OPERAND (member, 0);
14077 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
14078 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
14079 member = lookup_qualified_name (scope, tmpl,
14080 /*is_type_p=*/false,
14081 /*complain=*/false);
14082 if (BASELINK_P (member))
14083 {
14084 BASELINK_FUNCTIONS (member)
14085 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
14086 args);
14087 member = (adjust_result_of_qualified_name_lookup
14088 (member, BINFO_TYPE (BASELINK_BINFO (member)),
14089 object_type));
14090 }
14091 else
14092 {
14093 qualified_name_lookup_error (scope, tmpl, member,
14094 input_location);
14095 RETURN (error_mark_node);
14096 }
14097 }
14098 else if (TREE_CODE (member) == SCOPE_REF
14099 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
14100 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
14101 {
14102 if (complain & tf_error)
14103 {
14104 if (TYPE_P (TREE_OPERAND (member, 0)))
14105 error ("%qT is not a class or namespace",
14106 TREE_OPERAND (member, 0));
14107 else
14108 error ("%qD is not a class or namespace",
14109 TREE_OPERAND (member, 0));
14110 }
14111 RETURN (error_mark_node);
14112 }
14113 else if (TREE_CODE (member) == FIELD_DECL)
14114 RETURN (finish_non_static_data_member (member, object, NULL_TREE));
14115
14116 RETURN (finish_class_member_access_expr (object, member,
14117 /*template_p=*/false,
14118 complain));
14119 }
14120
14121 case THROW_EXPR:
14122 RETURN (build_throw
14123 (RECUR (TREE_OPERAND (t, 0))));
14124
14125 case CONSTRUCTOR:
14126 {
14127 vec<constructor_elt, va_gc> *n;
14128 constructor_elt *ce;
14129 unsigned HOST_WIDE_INT idx;
14130 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14131 bool process_index_p;
14132 int newlen;
14133 bool need_copy_p = false;
14134 tree r;
14135
14136 if (type == error_mark_node)
14137 RETURN (error_mark_node);
14138
14139 /* digest_init will do the wrong thing if we let it. */
14140 if (type && TYPE_PTRMEMFUNC_P (type))
14141 RETURN (t);
14142
14143 /* We do not want to process the index of aggregate
14144 initializers as they are identifier nodes which will be
14145 looked up by digest_init. */
14146 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
14147
14148 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
14149 newlen = vec_safe_length (n);
14150 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
14151 {
14152 if (ce->index && process_index_p)
14153 ce->index = RECUR (ce->index);
14154
14155 if (PACK_EXPANSION_P (ce->value))
14156 {
14157 /* Substitute into the pack expansion. */
14158 ce->value = tsubst_pack_expansion (ce->value, args, complain,
14159 in_decl);
14160
14161 if (ce->value == error_mark_node
14162 || PACK_EXPANSION_P (ce->value))
14163 ;
14164 else if (TREE_VEC_LENGTH (ce->value) == 1)
14165 /* Just move the argument into place. */
14166 ce->value = TREE_VEC_ELT (ce->value, 0);
14167 else
14168 {
14169 /* Update the length of the final CONSTRUCTOR
14170 arguments vector, and note that we will need to
14171 copy.*/
14172 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
14173 need_copy_p = true;
14174 }
14175 }
14176 else
14177 ce->value = RECUR (ce->value);
14178 }
14179
14180 if (need_copy_p)
14181 {
14182 vec<constructor_elt, va_gc> *old_n = n;
14183
14184 vec_alloc (n, newlen);
14185 FOR_EACH_VEC_ELT (*old_n, idx, ce)
14186 {
14187 if (TREE_CODE (ce->value) == TREE_VEC)
14188 {
14189 int i, len = TREE_VEC_LENGTH (ce->value);
14190 for (i = 0; i < len; ++i)
14191 CONSTRUCTOR_APPEND_ELT (n, 0,
14192 TREE_VEC_ELT (ce->value, i));
14193 }
14194 else
14195 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14196 }
14197 }
14198
14199 r = build_constructor (init_list_type_node, n);
14200 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14201
14202 if (TREE_HAS_CONSTRUCTOR (t))
14203 RETURN (finish_compound_literal (type, r, complain));
14204
14205 TREE_TYPE (r) = type;
14206 RETURN (r);
14207 }
14208
14209 case TYPEID_EXPR:
14210 {
14211 tree operand_0 = TREE_OPERAND (t, 0);
14212 if (TYPE_P (operand_0))
14213 {
14214 operand_0 = tsubst (operand_0, args, complain, in_decl);
14215 RETURN (get_typeid (operand_0));
14216 }
14217 else
14218 {
14219 operand_0 = RECUR (operand_0);
14220 RETURN (build_typeid (operand_0));
14221 }
14222 }
14223
14224 case VAR_DECL:
14225 if (!args)
14226 RETURN (t);
14227 /* Fall through */
14228
14229 case PARM_DECL:
14230 {
14231 tree r = tsubst_copy (t, args, complain, in_decl);
14232
14233 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14234 /* If the original type was a reference, we'll be wrapped in
14235 the appropriate INDIRECT_REF. */
14236 r = convert_from_reference (r);
14237 RETURN (r);
14238 }
14239
14240 case VA_ARG_EXPR:
14241 RETURN (build_x_va_arg (EXPR_LOCATION (t),
14242 RECUR (TREE_OPERAND (t, 0)),
14243 tsubst (TREE_TYPE (t), args, complain, in_decl)));
14244
14245 case OFFSETOF_EXPR:
14246 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t, 0))));
14247
14248 case TRAIT_EXPR:
14249 {
14250 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14251 complain, in_decl);
14252
14253 tree type2 = TRAIT_EXPR_TYPE2 (t);
14254 if (type2)
14255 type2 = tsubst_copy (type2, args, complain, in_decl);
14256
14257 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
14258 }
14259
14260 case STMT_EXPR:
14261 {
14262 tree old_stmt_expr = cur_stmt_expr;
14263 tree stmt_expr = begin_stmt_expr ();
14264
14265 cur_stmt_expr = stmt_expr;
14266 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14267 integral_constant_expression_p);
14268 stmt_expr = finish_stmt_expr (stmt_expr, false);
14269 cur_stmt_expr = old_stmt_expr;
14270
14271 /* If the resulting list of expression statement is empty,
14272 fold it further into void_zero_node. */
14273 if (empty_expr_stmt_p (stmt_expr))
14274 stmt_expr = void_zero_node;
14275
14276 RETURN (stmt_expr);
14277 }
14278
14279 case LAMBDA_EXPR:
14280 {
14281 tree r = build_lambda_expr ();
14282
14283 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14284 LAMBDA_EXPR_CLOSURE (r) = type;
14285 CLASSTYPE_LAMBDA_EXPR (type) = r;
14286
14287 LAMBDA_EXPR_LOCATION (r)
14288 = LAMBDA_EXPR_LOCATION (t);
14289 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14290 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14291 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14292 LAMBDA_EXPR_DISCRIMINATOR (r)
14293 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14294 /* For a function scope, we want to use tsubst so that we don't
14295 complain about referring to an auto function before its return
14296 type has been deduced. Otherwise, we want to use tsubst_copy so
14297 that we look up the existing field/parameter/variable rather
14298 than build a new one. */
14299 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
14300 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
14301 scope = tsubst (LAMBDA_EXPR_EXTRA_SCOPE (t), args,
14302 complain, in_decl);
14303 else
14304 scope = RECUR (scope);
14305 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
14306 LAMBDA_EXPR_RETURN_TYPE (r)
14307 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
14308
14309 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
14310 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
14311
14312 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14313 determine_visibility (TYPE_NAME (type));
14314 /* Now that we know visibility, instantiate the type so we have a
14315 declaration of the op() for later calls to lambda_function. */
14316 complete_type (type);
14317
14318 /* The capture list refers to closure members, so this needs to
14319 wait until after we finish instantiating the type. */
14320 LAMBDA_EXPR_CAPTURE_LIST (r)
14321 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
14322 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
14323
14324 RETURN (build_lambda_object (r));
14325 }
14326
14327 case TARGET_EXPR:
14328 /* We can get here for a constant initializer of non-dependent type.
14329 FIXME stop folding in cp_parser_initializer_clause. */
14330 gcc_assert (TREE_CONSTANT (t));
14331 {
14332 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
14333 complain);
14334 TREE_CONSTANT (r) = true;
14335 RETURN (r);
14336 }
14337
14338 case TRANSACTION_EXPR:
14339 RETURN (tsubst_expr(t, args, complain, in_decl,
14340 integral_constant_expression_p));
14341
14342 default:
14343 /* Handle Objective-C++ constructs, if appropriate. */
14344 {
14345 tree subst
14346 = objcp_tsubst_copy_and_build (t, args, complain,
14347 in_decl, /*function_p=*/false);
14348 if (subst)
14349 RETURN (subst);
14350 }
14351 RETURN (tsubst_copy (t, args, complain, in_decl));
14352 }
14353
14354 #undef RECUR
14355 #undef RETURN
14356 out:
14357 input_location = loc;
14358 return retval;
14359 }
14360
14361 /* Verify that the instantiated ARGS are valid. For type arguments,
14362 make sure that the type's linkage is ok. For non-type arguments,
14363 make sure they are constants if they are integral or enumerations.
14364 Emit an error under control of COMPLAIN, and return TRUE on error. */
14365
14366 static bool
14367 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
14368 {
14369 if (dependent_template_arg_p (t))
14370 return false;
14371 if (ARGUMENT_PACK_P (t))
14372 {
14373 tree vec = ARGUMENT_PACK_ARGS (t);
14374 int len = TREE_VEC_LENGTH (vec);
14375 bool result = false;
14376 int i;
14377
14378 for (i = 0; i < len; ++i)
14379 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
14380 result = true;
14381 return result;
14382 }
14383 else if (TYPE_P (t))
14384 {
14385 /* [basic.link]: A name with no linkage (notably, the name
14386 of a class or enumeration declared in a local scope)
14387 shall not be used to declare an entity with linkage.
14388 This implies that names with no linkage cannot be used as
14389 template arguments
14390
14391 DR 757 relaxes this restriction for C++0x. */
14392 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
14393 : no_linkage_check (t, /*relaxed_p=*/false));
14394
14395 if (nt)
14396 {
14397 /* DR 488 makes use of a type with no linkage cause
14398 type deduction to fail. */
14399 if (complain & tf_error)
14400 {
14401 if (TYPE_ANONYMOUS_P (nt))
14402 error ("%qT is/uses anonymous type", t);
14403 else
14404 error ("template argument for %qD uses local type %qT",
14405 tmpl, t);
14406 }
14407 return true;
14408 }
14409 /* In order to avoid all sorts of complications, we do not
14410 allow variably-modified types as template arguments. */
14411 else if (variably_modified_type_p (t, NULL_TREE))
14412 {
14413 if (complain & tf_error)
14414 error ("%qT is a variably modified type", t);
14415 return true;
14416 }
14417 }
14418 /* A non-type argument of integral or enumerated type must be a
14419 constant. */
14420 else if (TREE_TYPE (t)
14421 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
14422 && !TREE_CONSTANT (t))
14423 {
14424 if (complain & tf_error)
14425 error ("integral expression %qE is not constant", t);
14426 return true;
14427 }
14428 return false;
14429 }
14430
14431 static bool
14432 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
14433 {
14434 int ix, len = DECL_NTPARMS (tmpl);
14435 bool result = false;
14436
14437 for (ix = 0; ix != len; ix++)
14438 {
14439 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
14440 result = true;
14441 }
14442 if (result && (complain & tf_error))
14443 error (" trying to instantiate %qD", tmpl);
14444 return result;
14445 }
14446
14447 /* We're out of SFINAE context now, so generate diagnostics for the access
14448 errors we saw earlier when instantiating D from TMPL and ARGS. */
14449
14450 static void
14451 recheck_decl_substitution (tree d, tree tmpl, tree args)
14452 {
14453 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
14454 tree type = TREE_TYPE (pattern);
14455 location_t loc = input_location;
14456
14457 push_access_scope (d);
14458 push_deferring_access_checks (dk_no_deferred);
14459 input_location = DECL_SOURCE_LOCATION (pattern);
14460 tsubst (type, args, tf_warning_or_error, d);
14461 input_location = loc;
14462 pop_deferring_access_checks ();
14463 pop_access_scope (d);
14464 }
14465
14466 /* Instantiate the indicated variable, function, or alias template TMPL with
14467 the template arguments in TARG_PTR. */
14468
14469 static tree
14470 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14471 {
14472 tree targ_ptr = orig_args;
14473 tree fndecl;
14474 tree gen_tmpl;
14475 tree spec;
14476 bool access_ok = true;
14477
14478 if (tmpl == error_mark_node)
14479 return error_mark_node;
14480
14481 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14482
14483 /* If this function is a clone, handle it specially. */
14484 if (DECL_CLONED_FUNCTION_P (tmpl))
14485 {
14486 tree spec;
14487 tree clone;
14488
14489 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14490 DECL_CLONED_FUNCTION. */
14491 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14492 targ_ptr, complain);
14493 if (spec == error_mark_node)
14494 return error_mark_node;
14495
14496 /* Look for the clone. */
14497 FOR_EACH_CLONE (clone, spec)
14498 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14499 return clone;
14500 /* We should always have found the clone by now. */
14501 gcc_unreachable ();
14502 return NULL_TREE;
14503 }
14504
14505 /* Check to see if we already have this specialization. */
14506 gen_tmpl = most_general_template (tmpl);
14507 if (tmpl != gen_tmpl)
14508 /* The TMPL is a partial instantiation. To get a full set of
14509 arguments we must add the arguments used to perform the
14510 partial instantiation. */
14511 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14512 targ_ptr);
14513
14514 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14515 but it doesn't seem to be on the hot path. */
14516 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14517
14518 gcc_assert (tmpl == gen_tmpl
14519 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14520 == spec)
14521 || fndecl == NULL_TREE);
14522
14523 if (spec != NULL_TREE)
14524 {
14525 if (FNDECL_HAS_ACCESS_ERRORS (spec))
14526 {
14527 if (complain & tf_error)
14528 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
14529 return error_mark_node;
14530 }
14531 return spec;
14532 }
14533
14534 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14535 complain))
14536 return error_mark_node;
14537
14538 /* We are building a FUNCTION_DECL, during which the access of its
14539 parameters and return types have to be checked. However this
14540 FUNCTION_DECL which is the desired context for access checking
14541 is not built yet. We solve this chicken-and-egg problem by
14542 deferring all checks until we have the FUNCTION_DECL. */
14543 push_deferring_access_checks (dk_deferred);
14544
14545 /* Instantiation of the function happens in the context of the function
14546 template, not the context of the overload resolution we're doing. */
14547 push_to_top_level ();
14548 /* If there are dependent arguments, e.g. because we're doing partial
14549 ordering, make sure processing_template_decl stays set. */
14550 if (uses_template_parms (targ_ptr))
14551 ++processing_template_decl;
14552 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14553 {
14554 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
14555 complain, gen_tmpl);
14556 push_nested_class (ctx);
14557 }
14558 /* Substitute template parameters to obtain the specialization. */
14559 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14560 targ_ptr, complain, gen_tmpl);
14561 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14562 pop_nested_class ();
14563 pop_from_top_level ();
14564
14565 if (fndecl == error_mark_node)
14566 {
14567 pop_deferring_access_checks ();
14568 return error_mark_node;
14569 }
14570
14571 /* The DECL_TI_TEMPLATE should always be the immediate parent
14572 template, not the most general template. */
14573 DECL_TI_TEMPLATE (fndecl) = tmpl;
14574
14575 /* Now we know the specialization, compute access previously
14576 deferred. */
14577 push_access_scope (fndecl);
14578 if (!perform_deferred_access_checks (complain))
14579 access_ok = false;
14580 pop_access_scope (fndecl);
14581 pop_deferring_access_checks ();
14582
14583 /* If we've just instantiated the main entry point for a function,
14584 instantiate all the alternate entry points as well. We do this
14585 by cloning the instantiation of the main entry point, not by
14586 instantiating the template clones. */
14587 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14588 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14589
14590 if (!access_ok)
14591 {
14592 if (!(complain & tf_error))
14593 {
14594 /* Remember to reinstantiate when we're out of SFINAE so the user
14595 can see the errors. */
14596 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
14597 }
14598 return error_mark_node;
14599 }
14600 return fndecl;
14601 }
14602
14603 /* Wrapper for instantiate_template_1. */
14604
14605 tree
14606 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14607 {
14608 tree ret;
14609 timevar_push (TV_TEMPLATE_INST);
14610 ret = instantiate_template_1 (tmpl, orig_args, complain);
14611 timevar_pop (TV_TEMPLATE_INST);
14612 return ret;
14613 }
14614
14615 /* Instantiate the alias template TMPL with ARGS. Also push a template
14616 instantiation level, which instantiate_template doesn't do because
14617 functions and variables have sufficient context established by the
14618 callers. */
14619
14620 static tree
14621 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
14622 {
14623 struct pending_template *old_last_pend = last_pending_template;
14624 struct tinst_level *old_error_tinst = last_error_tinst_level;
14625 if (tmpl == error_mark_node || args == error_mark_node)
14626 return error_mark_node;
14627 tree tinst = build_tree_list (tmpl, args);
14628 if (!push_tinst_level (tinst))
14629 {
14630 ggc_free (tinst);
14631 return error_mark_node;
14632 }
14633 tree r = instantiate_template (tmpl, args, complain);
14634 pop_tinst_level ();
14635 /* We can't free this if a pending_template entry or last_error_tinst_level
14636 is pointing at it. */
14637 if (last_pending_template == old_last_pend
14638 && last_error_tinst_level == old_error_tinst)
14639 ggc_free (tinst);
14640
14641 return r;
14642 }
14643
14644 /* PARM is a template parameter pack for FN. Returns true iff
14645 PARM is used in a deducible way in the argument list of FN. */
14646
14647 static bool
14648 pack_deducible_p (tree parm, tree fn)
14649 {
14650 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
14651 for (; t; t = TREE_CHAIN (t))
14652 {
14653 tree type = TREE_VALUE (t);
14654 tree packs;
14655 if (!PACK_EXPANSION_P (type))
14656 continue;
14657 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
14658 packs; packs = TREE_CHAIN (packs))
14659 if (TREE_VALUE (packs) == parm)
14660 {
14661 /* The template parameter pack is used in a function parameter
14662 pack. If this is the end of the parameter list, the
14663 template parameter pack is deducible. */
14664 if (TREE_CHAIN (t) == void_list_node)
14665 return true;
14666 else
14667 /* Otherwise, not. Well, it could be deduced from
14668 a non-pack parameter, but doing so would end up with
14669 a deduction mismatch, so don't bother. */
14670 return false;
14671 }
14672 }
14673 /* The template parameter pack isn't used in any function parameter
14674 packs, but it might be used deeper, e.g. tuple<Args...>. */
14675 return true;
14676 }
14677
14678 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14679 NARGS elements of the arguments that are being used when calling
14680 it. TARGS is a vector into which the deduced template arguments
14681 are placed.
14682
14683 Return zero for success, 2 for an incomplete match that doesn't resolve
14684 all the types, and 1 for complete failure. An error message will be
14685 printed only for an incomplete match.
14686
14687 If FN is a conversion operator, or we are trying to produce a specific
14688 specialization, RETURN_TYPE is the return type desired.
14689
14690 The EXPLICIT_TARGS are explicit template arguments provided via a
14691 template-id.
14692
14693 The parameter STRICT is one of:
14694
14695 DEDUCE_CALL:
14696 We are deducing arguments for a function call, as in
14697 [temp.deduct.call].
14698
14699 DEDUCE_CONV:
14700 We are deducing arguments for a conversion function, as in
14701 [temp.deduct.conv].
14702
14703 DEDUCE_EXACT:
14704 We are deducing arguments when doing an explicit instantiation
14705 as in [temp.explicit], when determining an explicit specialization
14706 as in [temp.expl.spec], or when taking the address of a function
14707 template, as in [temp.deduct.funcaddr]. */
14708
14709 tree
14710 fn_type_unification (tree fn,
14711 tree explicit_targs,
14712 tree targs,
14713 const tree *args,
14714 unsigned int nargs,
14715 tree return_type,
14716 unification_kind_t strict,
14717 int flags,
14718 bool explain_p)
14719 {
14720 tree parms;
14721 tree fntype;
14722 tree decl = NULL_TREE;
14723 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
14724 bool ok;
14725 static int deduction_depth;
14726 struct pending_template *old_last_pend = last_pending_template;
14727 struct tinst_level *old_error_tinst = last_error_tinst_level;
14728 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14729 tree tinst;
14730 tree r = error_mark_node;
14731
14732 /* Adjust any explicit template arguments before entering the
14733 substitution context. */
14734 if (explicit_targs)
14735 {
14736 explicit_targs
14737 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
14738 complain,
14739 /*require_all_args=*/false,
14740 /*use_default_args=*/false));
14741 if (explicit_targs == error_mark_node)
14742 return error_mark_node;
14743 }
14744
14745 /* In C++0x, it's possible to have a function template whose type depends
14746 on itself recursively. This is most obvious with decltype, but can also
14747 occur with enumeration scope (c++/48969). So we need to catch infinite
14748 recursion and reject the substitution at deduction time; this function
14749 will return error_mark_node for any repeated substitution.
14750
14751 This also catches excessive recursion such as when f<N> depends on
14752 f<N-1> across all integers, and returns error_mark_node for all the
14753 substitutions back up to the initial one.
14754
14755 This is, of course, not reentrant. */
14756 if (excessive_deduction_depth)
14757 return error_mark_node;
14758 tinst = build_tree_list (fn, targs);
14759 if (!push_tinst_level (tinst))
14760 {
14761 excessive_deduction_depth = true;
14762 ggc_free (tinst);
14763 return error_mark_node;
14764 }
14765 ++deduction_depth;
14766 push_deferring_access_checks (dk_deferred);
14767
14768 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14769
14770 fntype = TREE_TYPE (fn);
14771 if (explicit_targs)
14772 {
14773 /* [temp.deduct]
14774
14775 The specified template arguments must match the template
14776 parameters in kind (i.e., type, nontype, template), and there
14777 must not be more arguments than there are parameters;
14778 otherwise type deduction fails.
14779
14780 Nontype arguments must match the types of the corresponding
14781 nontype template parameters, or must be convertible to the
14782 types of the corresponding nontype parameters as specified in
14783 _temp.arg.nontype_, otherwise type deduction fails.
14784
14785 All references in the function type of the function template
14786 to the corresponding template parameters are replaced by the
14787 specified template argument values. If a substitution in a
14788 template parameter or in the function type of the function
14789 template results in an invalid type, type deduction fails. */
14790 int i, len = TREE_VEC_LENGTH (tparms);
14791 location_t loc = input_location;
14792 bool incomplete = false;
14793
14794 /* Substitute the explicit args into the function type. This is
14795 necessary so that, for instance, explicitly declared function
14796 arguments can match null pointed constants. If we were given
14797 an incomplete set of explicit args, we must not do semantic
14798 processing during substitution as we could create partial
14799 instantiations. */
14800 for (i = 0; i < len; i++)
14801 {
14802 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14803 bool parameter_pack = false;
14804 tree targ = TREE_VEC_ELT (explicit_targs, i);
14805
14806 /* Dig out the actual parm. */
14807 if (TREE_CODE (parm) == TYPE_DECL
14808 || TREE_CODE (parm) == TEMPLATE_DECL)
14809 {
14810 parm = TREE_TYPE (parm);
14811 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
14812 }
14813 else if (TREE_CODE (parm) == PARM_DECL)
14814 {
14815 parm = DECL_INITIAL (parm);
14816 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
14817 }
14818
14819 if (!parameter_pack && targ == NULL_TREE)
14820 /* No explicit argument for this template parameter. */
14821 incomplete = true;
14822
14823 if (parameter_pack && pack_deducible_p (parm, fn))
14824 {
14825 /* Mark the argument pack as "incomplete". We could
14826 still deduce more arguments during unification.
14827 We remove this mark in type_unification_real. */
14828 if (targ)
14829 {
14830 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
14831 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
14832 = ARGUMENT_PACK_ARGS (targ);
14833 }
14834
14835 /* We have some incomplete argument packs. */
14836 incomplete = true;
14837 }
14838 }
14839
14840 processing_template_decl += incomplete;
14841 input_location = DECL_SOURCE_LOCATION (fn);
14842 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
14843 complain | tf_partial, NULL_TREE);
14844 input_location = loc;
14845 processing_template_decl -= incomplete;
14846
14847 if (fntype == error_mark_node)
14848 goto fail;
14849
14850 /* Place the explicitly specified arguments in TARGS. */
14851 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
14852 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
14853 }
14854
14855 /* Never do unification on the 'this' parameter. */
14856 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
14857
14858 if (return_type)
14859 {
14860 tree *new_args;
14861
14862 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
14863 new_args = XALLOCAVEC (tree, nargs + 1);
14864 new_args[0] = return_type;
14865 memcpy (new_args + 1, args, nargs * sizeof (tree));
14866 args = new_args;
14867 ++nargs;
14868 }
14869
14870 /* We allow incomplete unification without an error message here
14871 because the standard doesn't seem to explicitly prohibit it. Our
14872 callers must be ready to deal with unification failures in any
14873 event. */
14874
14875 pop_tinst_level ();
14876 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
14877 targs, parms, args, nargs, /*subr=*/0,
14878 strict, flags, explain_p);
14879 push_tinst_level (tinst);
14880 if (!ok)
14881 goto fail;
14882
14883 /* Now that we have bindings for all of the template arguments,
14884 ensure that the arguments deduced for the template template
14885 parameters have compatible template parameter lists. We cannot
14886 check this property before we have deduced all template
14887 arguments, because the template parameter types of a template
14888 template parameter might depend on prior template parameters
14889 deduced after the template template parameter. The following
14890 ill-formed example illustrates this issue:
14891
14892 template<typename T, template<T> class C> void f(C<5>, T);
14893
14894 template<int N> struct X {};
14895
14896 void g() {
14897 f(X<5>(), 5l); // error: template argument deduction fails
14898 }
14899
14900 The template parameter list of 'C' depends on the template type
14901 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14902 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14903 time that we deduce 'C'. */
14904 if (!template_template_parm_bindings_ok_p
14905 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
14906 {
14907 unify_inconsistent_template_template_parameters (explain_p);
14908 goto fail;
14909 }
14910
14911 /* All is well so far. Now, check:
14912
14913 [temp.deduct]
14914
14915 When all template arguments have been deduced, all uses of
14916 template parameters in nondeduced contexts are replaced with
14917 the corresponding deduced argument values. If the
14918 substitution results in an invalid type, as described above,
14919 type deduction fails. */
14920 decl = instantiate_template (fn, targs, complain);
14921 if (decl == error_mark_node)
14922 goto fail;
14923
14924 /* Now perform any access checks encountered during deduction, such as
14925 for default template arguments. */
14926 push_access_scope (decl);
14927 ok = perform_deferred_access_checks (complain);
14928 pop_access_scope (decl);
14929 if (!ok)
14930 goto fail;
14931
14932 /* If we're looking for an exact match, check that what we got
14933 is indeed an exact match. It might not be if some template
14934 parameters are used in non-deduced contexts. */
14935 if (strict == DEDUCE_EXACT)
14936 {
14937 tree substed = TREE_TYPE (decl);
14938 unsigned int i;
14939
14940 tree sarg
14941 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
14942 if (return_type)
14943 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
14944 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
14945 if (!same_type_p (args[i], TREE_VALUE (sarg)))
14946 {
14947 unify_type_mismatch (explain_p, args[i],
14948 TREE_VALUE (sarg));
14949 goto fail;
14950 }
14951 }
14952
14953 r = decl;
14954
14955 fail:
14956 pop_deferring_access_checks ();
14957 --deduction_depth;
14958 if (excessive_deduction_depth)
14959 {
14960 if (deduction_depth == 0)
14961 /* Reset once we're all the way out. */
14962 excessive_deduction_depth = false;
14963 }
14964
14965 pop_tinst_level ();
14966 /* We can't free this if a pending_template entry or last_error_tinst_level
14967 is pointing at it. */
14968 if (last_pending_template == old_last_pend
14969 && last_error_tinst_level == old_error_tinst)
14970 ggc_free (tinst);
14971
14972 return r;
14973 }
14974
14975 /* Adjust types before performing type deduction, as described in
14976 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14977 sections are symmetric. PARM is the type of a function parameter
14978 or the return type of the conversion function. ARG is the type of
14979 the argument passed to the call, or the type of the value
14980 initialized with the result of the conversion function.
14981 ARG_EXPR is the original argument expression, which may be null. */
14982
14983 static int
14984 maybe_adjust_types_for_deduction (unification_kind_t strict,
14985 tree* parm,
14986 tree* arg,
14987 tree arg_expr)
14988 {
14989 int result = 0;
14990
14991 switch (strict)
14992 {
14993 case DEDUCE_CALL:
14994 break;
14995
14996 case DEDUCE_CONV:
14997 {
14998 /* Swap PARM and ARG throughout the remainder of this
14999 function; the handling is precisely symmetric since PARM
15000 will initialize ARG rather than vice versa. */
15001 tree* temp = parm;
15002 parm = arg;
15003 arg = temp;
15004 break;
15005 }
15006
15007 case DEDUCE_EXACT:
15008 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15009 too, but here handle it by stripping the reference from PARM
15010 rather than by adding it to ARG. */
15011 if (TREE_CODE (*parm) == REFERENCE_TYPE
15012 && TYPE_REF_IS_RVALUE (*parm)
15013 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15014 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15015 && TREE_CODE (*arg) == REFERENCE_TYPE
15016 && !TYPE_REF_IS_RVALUE (*arg))
15017 *parm = TREE_TYPE (*parm);
15018 /* Nothing else to do in this case. */
15019 return 0;
15020
15021 default:
15022 gcc_unreachable ();
15023 }
15024
15025 if (TREE_CODE (*parm) != REFERENCE_TYPE)
15026 {
15027 /* [temp.deduct.call]
15028
15029 If P is not a reference type:
15030
15031 --If A is an array type, the pointer type produced by the
15032 array-to-pointer standard conversion (_conv.array_) is
15033 used in place of A for type deduction; otherwise,
15034
15035 --If A is a function type, the pointer type produced by
15036 the function-to-pointer standard conversion
15037 (_conv.func_) is used in place of A for type deduction;
15038 otherwise,
15039
15040 --If A is a cv-qualified type, the top level
15041 cv-qualifiers of A's type are ignored for type
15042 deduction. */
15043 if (TREE_CODE (*arg) == ARRAY_TYPE)
15044 *arg = build_pointer_type (TREE_TYPE (*arg));
15045 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
15046 *arg = build_pointer_type (*arg);
15047 else
15048 *arg = TYPE_MAIN_VARIANT (*arg);
15049 }
15050
15051 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15052 of the form T&&, where T is a template parameter, and the argument
15053 is an lvalue, T is deduced as A& */
15054 if (TREE_CODE (*parm) == REFERENCE_TYPE
15055 && TYPE_REF_IS_RVALUE (*parm)
15056 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
15057 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
15058 && (arg_expr ? real_lvalue_p (arg_expr)
15059 /* try_one_overload doesn't provide an arg_expr, but
15060 functions are always lvalues. */
15061 : TREE_CODE (*arg) == FUNCTION_TYPE))
15062 *arg = build_reference_type (*arg);
15063
15064 /* [temp.deduct.call]
15065
15066 If P is a cv-qualified type, the top level cv-qualifiers
15067 of P's type are ignored for type deduction. If P is a
15068 reference type, the type referred to by P is used for
15069 type deduction. */
15070 *parm = TYPE_MAIN_VARIANT (*parm);
15071 if (TREE_CODE (*parm) == REFERENCE_TYPE)
15072 {
15073 *parm = TREE_TYPE (*parm);
15074 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15075 }
15076
15077 /* DR 322. For conversion deduction, remove a reference type on parm
15078 too (which has been swapped into ARG). */
15079 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
15080 *arg = TREE_TYPE (*arg);
15081
15082 return result;
15083 }
15084
15085 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15086 template which does contain any deducible template parameters; check if
15087 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15088 unify_one_argument. */
15089
15090 static int
15091 check_non_deducible_conversion (tree parm, tree arg, int strict,
15092 int flags, bool explain_p)
15093 {
15094 tree type;
15095
15096 if (!TYPE_P (arg))
15097 type = TREE_TYPE (arg);
15098 else
15099 type = arg;
15100
15101 if (same_type_p (parm, type))
15102 return unify_success (explain_p);
15103
15104 if (strict == DEDUCE_CONV)
15105 {
15106 if (can_convert_arg (type, parm, NULL_TREE, flags,
15107 explain_p ? tf_warning_or_error : tf_none))
15108 return unify_success (explain_p);
15109 }
15110 else if (strict != DEDUCE_EXACT)
15111 {
15112 if (can_convert_arg (parm, type,
15113 TYPE_P (arg) ? NULL_TREE : arg,
15114 flags, explain_p ? tf_warning_or_error : tf_none))
15115 return unify_success (explain_p);
15116 }
15117
15118 if (strict == DEDUCE_EXACT)
15119 return unify_type_mismatch (explain_p, parm, arg);
15120 else
15121 return unify_arg_conversion (explain_p, parm, type, arg);
15122 }
15123
15124 /* Subroutine of type_unification_real and unify_pack_expansion to
15125 handle unification of a single P/A pair. Parameters are as
15126 for those functions. */
15127
15128 static int
15129 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
15130 int subr, unification_kind_t strict, int flags,
15131 bool explain_p)
15132 {
15133 tree arg_expr = NULL_TREE;
15134 int arg_strict;
15135
15136 if (arg == error_mark_node || parm == error_mark_node)
15137 return unify_invalid (explain_p);
15138 if (arg == unknown_type_node)
15139 /* We can't deduce anything from this, but we might get all the
15140 template args from other function args. */
15141 return unify_success (explain_p);
15142
15143 /* FIXME uses_deducible_template_parms */
15144 if (TYPE_P (parm) && !uses_template_parms (parm))
15145 return check_non_deducible_conversion (parm, arg, strict, flags,
15146 explain_p);
15147
15148 switch (strict)
15149 {
15150 case DEDUCE_CALL:
15151 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
15152 | UNIFY_ALLOW_MORE_CV_QUAL
15153 | UNIFY_ALLOW_DERIVED);
15154 break;
15155
15156 case DEDUCE_CONV:
15157 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
15158 break;
15159
15160 case DEDUCE_EXACT:
15161 arg_strict = UNIFY_ALLOW_NONE;
15162 break;
15163
15164 default:
15165 gcc_unreachable ();
15166 }
15167
15168 /* We only do these transformations if this is the top-level
15169 parameter_type_list in a call or declaration matching; in other
15170 situations (nested function declarators, template argument lists) we
15171 won't be comparing a type to an expression, and we don't do any type
15172 adjustments. */
15173 if (!subr)
15174 {
15175 if (!TYPE_P (arg))
15176 {
15177 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
15178 if (type_unknown_p (arg))
15179 {
15180 /* [temp.deduct.type] A template-argument can be
15181 deduced from a pointer to function or pointer
15182 to member function argument if the set of
15183 overloaded functions does not contain function
15184 templates and at most one of a set of
15185 overloaded functions provides a unique
15186 match. */
15187
15188 if (resolve_overloaded_unification
15189 (tparms, targs, parm, arg, strict,
15190 arg_strict, explain_p))
15191 return unify_success (explain_p);
15192 return unify_overload_resolution_failure (explain_p, arg);
15193 }
15194
15195 arg_expr = arg;
15196 arg = unlowered_expr_type (arg);
15197 if (arg == error_mark_node)
15198 return unify_invalid (explain_p);
15199 }
15200
15201 arg_strict |=
15202 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
15203 }
15204 else
15205 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
15206 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
15207
15208 /* For deduction from an init-list we need the actual list. */
15209 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
15210 arg = arg_expr;
15211 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
15212 }
15213
15214 /* Most parms like fn_type_unification.
15215
15216 If SUBR is 1, we're being called recursively (to unify the
15217 arguments of a function or method parameter of a function
15218 template). */
15219
15220 static int
15221 type_unification_real (tree tparms,
15222 tree targs,
15223 tree xparms,
15224 const tree *xargs,
15225 unsigned int xnargs,
15226 int subr,
15227 unification_kind_t strict,
15228 int flags,
15229 bool explain_p)
15230 {
15231 tree parm, arg;
15232 int i;
15233 int ntparms = TREE_VEC_LENGTH (tparms);
15234 int saw_undeduced = 0;
15235 tree parms;
15236 const tree *args;
15237 unsigned int nargs;
15238 unsigned int ia;
15239
15240 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
15241 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
15242 gcc_assert (ntparms > 0);
15243
15244 /* Reset the number of non-defaulted template arguments contained
15245 in TARGS. */
15246 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
15247
15248 again:
15249 parms = xparms;
15250 args = xargs;
15251 nargs = xnargs;
15252
15253 ia = 0;
15254 while (parms && parms != void_list_node
15255 && ia < nargs)
15256 {
15257 parm = TREE_VALUE (parms);
15258
15259 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
15260 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
15261 /* For a function parameter pack that occurs at the end of the
15262 parameter-declaration-list, the type A of each remaining
15263 argument of the call is compared with the type P of the
15264 declarator-id of the function parameter pack. */
15265 break;
15266
15267 parms = TREE_CHAIN (parms);
15268
15269 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
15270 /* For a function parameter pack that does not occur at the
15271 end of the parameter-declaration-list, the type of the
15272 parameter pack is a non-deduced context. */
15273 continue;
15274
15275 arg = args[ia];
15276 ++ia;
15277
15278 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15279 flags, explain_p))
15280 return 1;
15281 }
15282
15283 if (parms
15284 && parms != void_list_node
15285 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
15286 {
15287 /* Unify the remaining arguments with the pack expansion type. */
15288 tree argvec;
15289 tree parmvec = make_tree_vec (1);
15290
15291 /* Allocate a TREE_VEC and copy in all of the arguments */
15292 argvec = make_tree_vec (nargs - ia);
15293 for (i = 0; ia < nargs; ++ia, ++i)
15294 TREE_VEC_ELT (argvec, i) = args[ia];
15295
15296 /* Copy the parameter into parmvec. */
15297 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
15298 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
15299 /*subr=*/subr, explain_p))
15300 return 1;
15301
15302 /* Advance to the end of the list of parameters. */
15303 parms = TREE_CHAIN (parms);
15304 }
15305
15306 /* Fail if we've reached the end of the parm list, and more args
15307 are present, and the parm list isn't variadic. */
15308 if (ia < nargs && parms == void_list_node)
15309 return unify_too_many_arguments (explain_p, nargs, ia);
15310 /* Fail if parms are left and they don't have default values. */
15311 if (parms && parms != void_list_node
15312 && TREE_PURPOSE (parms) == NULL_TREE)
15313 {
15314 unsigned int count = nargs;
15315 tree p = parms;
15316 while (p && p != void_list_node)
15317 {
15318 count++;
15319 p = TREE_CHAIN (p);
15320 }
15321 return unify_too_few_arguments (explain_p, ia, count);
15322 }
15323
15324 if (!subr)
15325 {
15326 tsubst_flags_t complain = (explain_p
15327 ? tf_warning_or_error
15328 : tf_none);
15329
15330 /* Check to see if we need another pass before we start clearing
15331 ARGUMENT_PACK_INCOMPLETE_P. */
15332 for (i = 0; i < ntparms; i++)
15333 {
15334 tree targ = TREE_VEC_ELT (targs, i);
15335 tree tparm = TREE_VEC_ELT (tparms, i);
15336
15337 if (targ || tparm == error_mark_node)
15338 continue;
15339 tparm = TREE_VALUE (tparm);
15340
15341 /* If this is an undeduced nontype parameter that depends on
15342 a type parameter, try another pass; its type may have been
15343 deduced from a later argument than the one from which
15344 this parameter can be deduced. */
15345 if (TREE_CODE (tparm) == PARM_DECL
15346 && uses_template_parms (TREE_TYPE (tparm))
15347 && !saw_undeduced++)
15348 goto again;
15349 }
15350
15351 for (i = 0; i < ntparms; i++)
15352 {
15353 tree targ = TREE_VEC_ELT (targs, i);
15354 tree tparm = TREE_VEC_ELT (tparms, i);
15355
15356 /* Clear the "incomplete" flags on all argument packs now so that
15357 substituting them into later default arguments works. */
15358 if (targ && ARGUMENT_PACK_P (targ))
15359 {
15360 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
15361 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
15362 }
15363
15364 if (targ || tparm == error_mark_node)
15365 continue;
15366 tparm = TREE_VALUE (tparm);
15367
15368 /* Core issue #226 (C++0x) [temp.deduct]:
15369
15370 If a template argument has not been deduced, its
15371 default template argument, if any, is used.
15372
15373 When we are in C++98 mode, TREE_PURPOSE will either
15374 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15375 to explicitly check cxx_dialect here. */
15376 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
15377 {
15378 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15379 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
15380 location_t save_loc = input_location;
15381 if (DECL_P (parm))
15382 input_location = DECL_SOURCE_LOCATION (parm);
15383 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
15384 arg = convert_template_argument (parm, arg, targs, complain,
15385 i, NULL_TREE);
15386 input_location = save_loc;
15387 if (arg == error_mark_node)
15388 return 1;
15389 else
15390 {
15391 TREE_VEC_ELT (targs, i) = arg;
15392 /* The position of the first default template argument,
15393 is also the number of non-defaulted arguments in TARGS.
15394 Record that. */
15395 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15396 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
15397 continue;
15398 }
15399 }
15400
15401 /* If the type parameter is a parameter pack, then it will
15402 be deduced to an empty parameter pack. */
15403 if (template_parameter_pack_p (tparm))
15404 {
15405 tree arg;
15406
15407 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
15408 {
15409 arg = make_node (NONTYPE_ARGUMENT_PACK);
15410 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
15411 TREE_CONSTANT (arg) = 1;
15412 }
15413 else
15414 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
15415
15416 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
15417
15418 TREE_VEC_ELT (targs, i) = arg;
15419 continue;
15420 }
15421
15422 return unify_parameter_deduction_failure (explain_p, tparm);
15423 }
15424 }
15425 #ifdef ENABLE_CHECKING
15426 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15427 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
15428 #endif
15429
15430 return unify_success (explain_p);
15431 }
15432
15433 /* Subroutine of type_unification_real. Args are like the variables
15434 at the call site. ARG is an overloaded function (or template-id);
15435 we try deducing template args from each of the overloads, and if
15436 only one succeeds, we go with that. Modifies TARGS and returns
15437 true on success. */
15438
15439 static bool
15440 resolve_overloaded_unification (tree tparms,
15441 tree targs,
15442 tree parm,
15443 tree arg,
15444 unification_kind_t strict,
15445 int sub_strict,
15446 bool explain_p)
15447 {
15448 tree tempargs = copy_node (targs);
15449 int good = 0;
15450 tree goodfn = NULL_TREE;
15451 bool addr_p;
15452
15453 if (TREE_CODE (arg) == ADDR_EXPR)
15454 {
15455 arg = TREE_OPERAND (arg, 0);
15456 addr_p = true;
15457 }
15458 else
15459 addr_p = false;
15460
15461 if (TREE_CODE (arg) == COMPONENT_REF)
15462 /* Handle `&x' where `x' is some static or non-static member
15463 function name. */
15464 arg = TREE_OPERAND (arg, 1);
15465
15466 if (TREE_CODE (arg) == OFFSET_REF)
15467 arg = TREE_OPERAND (arg, 1);
15468
15469 /* Strip baselink information. */
15470 if (BASELINK_P (arg))
15471 arg = BASELINK_FUNCTIONS (arg);
15472
15473 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
15474 {
15475 /* If we got some explicit template args, we need to plug them into
15476 the affected templates before we try to unify, in case the
15477 explicit args will completely resolve the templates in question. */
15478
15479 int ok = 0;
15480 tree expl_subargs = TREE_OPERAND (arg, 1);
15481 arg = TREE_OPERAND (arg, 0);
15482
15483 for (; arg; arg = OVL_NEXT (arg))
15484 {
15485 tree fn = OVL_CURRENT (arg);
15486 tree subargs, elem;
15487
15488 if (TREE_CODE (fn) != TEMPLATE_DECL)
15489 continue;
15490
15491 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15492 expl_subargs, NULL_TREE, tf_none,
15493 /*require_all_args=*/true,
15494 /*use_default_args=*/true);
15495 if (subargs != error_mark_node
15496 && !any_dependent_template_arguments_p (subargs))
15497 {
15498 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
15499 if (try_one_overload (tparms, targs, tempargs, parm,
15500 elem, strict, sub_strict, addr_p, explain_p)
15501 && (!goodfn || !same_type_p (goodfn, elem)))
15502 {
15503 goodfn = elem;
15504 ++good;
15505 }
15506 }
15507 else if (subargs)
15508 ++ok;
15509 }
15510 /* If no templates (or more than one) are fully resolved by the
15511 explicit arguments, this template-id is a non-deduced context; it
15512 could still be OK if we deduce all template arguments for the
15513 enclosing call through other arguments. */
15514 if (good != 1)
15515 good = ok;
15516 }
15517 else if (TREE_CODE (arg) != OVERLOAD
15518 && TREE_CODE (arg) != FUNCTION_DECL)
15519 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15520 -- but the deduction does not succeed because the expression is
15521 not just the function on its own. */
15522 return false;
15523 else
15524 for (; arg; arg = OVL_NEXT (arg))
15525 if (try_one_overload (tparms, targs, tempargs, parm,
15526 TREE_TYPE (OVL_CURRENT (arg)),
15527 strict, sub_strict, addr_p, explain_p)
15528 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
15529 {
15530 goodfn = OVL_CURRENT (arg);
15531 ++good;
15532 }
15533
15534 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15535 to function or pointer to member function argument if the set of
15536 overloaded functions does not contain function templates and at most
15537 one of a set of overloaded functions provides a unique match.
15538
15539 So if we found multiple possibilities, we return success but don't
15540 deduce anything. */
15541
15542 if (good == 1)
15543 {
15544 int i = TREE_VEC_LENGTH (targs);
15545 for (; i--; )
15546 if (TREE_VEC_ELT (tempargs, i))
15547 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
15548 }
15549 if (good)
15550 return true;
15551
15552 return false;
15553 }
15554
15555 /* Core DR 115: In contexts where deduction is done and fails, or in
15556 contexts where deduction is not done, if a template argument list is
15557 specified and it, along with any default template arguments, identifies
15558 a single function template specialization, then the template-id is an
15559 lvalue for the function template specialization. */
15560
15561 tree
15562 resolve_nondeduced_context (tree orig_expr)
15563 {
15564 tree expr, offset, baselink;
15565 bool addr;
15566
15567 if (!type_unknown_p (orig_expr))
15568 return orig_expr;
15569
15570 expr = orig_expr;
15571 addr = false;
15572 offset = NULL_TREE;
15573 baselink = NULL_TREE;
15574
15575 if (TREE_CODE (expr) == ADDR_EXPR)
15576 {
15577 expr = TREE_OPERAND (expr, 0);
15578 addr = true;
15579 }
15580 if (TREE_CODE (expr) == OFFSET_REF)
15581 {
15582 offset = expr;
15583 expr = TREE_OPERAND (expr, 1);
15584 }
15585 if (BASELINK_P (expr))
15586 {
15587 baselink = expr;
15588 expr = BASELINK_FUNCTIONS (expr);
15589 }
15590
15591 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
15592 {
15593 int good = 0;
15594 tree goodfn = NULL_TREE;
15595
15596 /* If we got some explicit template args, we need to plug them into
15597 the affected templates before we try to unify, in case the
15598 explicit args will completely resolve the templates in question. */
15599
15600 tree expl_subargs = TREE_OPERAND (expr, 1);
15601 tree arg = TREE_OPERAND (expr, 0);
15602 tree badfn = NULL_TREE;
15603 tree badargs = NULL_TREE;
15604
15605 for (; arg; arg = OVL_NEXT (arg))
15606 {
15607 tree fn = OVL_CURRENT (arg);
15608 tree subargs, elem;
15609
15610 if (TREE_CODE (fn) != TEMPLATE_DECL)
15611 continue;
15612
15613 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
15614 expl_subargs, NULL_TREE, tf_none,
15615 /*require_all_args=*/true,
15616 /*use_default_args=*/true);
15617 if (subargs != error_mark_node
15618 && !any_dependent_template_arguments_p (subargs))
15619 {
15620 elem = instantiate_template (fn, subargs, tf_none);
15621 if (elem == error_mark_node)
15622 {
15623 badfn = fn;
15624 badargs = subargs;
15625 }
15626 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15627 {
15628 goodfn = elem;
15629 ++good;
15630 }
15631 }
15632 }
15633 if (good == 1)
15634 {
15635 mark_used (goodfn);
15636 expr = goodfn;
15637 if (baselink)
15638 expr = build_baselink (BASELINK_BINFO (baselink),
15639 BASELINK_ACCESS_BINFO (baselink),
15640 expr, BASELINK_OPTYPE (baselink));
15641 if (offset)
15642 {
15643 tree base
15644 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15645 expr = build_offset_ref (base, expr, addr);
15646 }
15647 if (addr)
15648 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15649 return expr;
15650 }
15651 else if (good == 0 && badargs)
15652 /* There were no good options and at least one bad one, so let the
15653 user know what the problem is. */
15654 instantiate_template (badfn, badargs, tf_warning_or_error);
15655 }
15656 return orig_expr;
15657 }
15658
15659 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15660 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15661 different overloads deduce different arguments for a given parm.
15662 ADDR_P is true if the expression for which deduction is being
15663 performed was of the form "& fn" rather than simply "fn".
15664
15665 Returns 1 on success. */
15666
15667 static int
15668 try_one_overload (tree tparms,
15669 tree orig_targs,
15670 tree targs,
15671 tree parm,
15672 tree arg,
15673 unification_kind_t strict,
15674 int sub_strict,
15675 bool addr_p,
15676 bool explain_p)
15677 {
15678 int nargs;
15679 tree tempargs;
15680 int i;
15681
15682 if (arg == error_mark_node)
15683 return 0;
15684
15685 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15686 to function or pointer to member function argument if the set of
15687 overloaded functions does not contain function templates and at most
15688 one of a set of overloaded functions provides a unique match.
15689
15690 So if this is a template, just return success. */
15691
15692 if (uses_template_parms (arg))
15693 return 1;
15694
15695 if (TREE_CODE (arg) == METHOD_TYPE)
15696 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15697 else if (addr_p)
15698 arg = build_pointer_type (arg);
15699
15700 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15701
15702 /* We don't copy orig_targs for this because if we have already deduced
15703 some template args from previous args, unify would complain when we
15704 try to deduce a template parameter for the same argument, even though
15705 there isn't really a conflict. */
15706 nargs = TREE_VEC_LENGTH (targs);
15707 tempargs = make_tree_vec (nargs);
15708
15709 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15710 return 0;
15711
15712 /* First make sure we didn't deduce anything that conflicts with
15713 explicitly specified args. */
15714 for (i = nargs; i--; )
15715 {
15716 tree elt = TREE_VEC_ELT (tempargs, i);
15717 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15718
15719 if (!elt)
15720 /*NOP*/;
15721 else if (uses_template_parms (elt))
15722 /* Since we're unifying against ourselves, we will fill in
15723 template args used in the function parm list with our own
15724 template parms. Discard them. */
15725 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15726 else if (oldelt && !template_args_equal (oldelt, elt))
15727 return 0;
15728 }
15729
15730 for (i = nargs; i--; )
15731 {
15732 tree elt = TREE_VEC_ELT (tempargs, i);
15733
15734 if (elt)
15735 TREE_VEC_ELT (targs, i) = elt;
15736 }
15737
15738 return 1;
15739 }
15740
15741 /* PARM is a template class (perhaps with unbound template
15742 parameters). ARG is a fully instantiated type. If ARG can be
15743 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15744 TARGS are as for unify. */
15745
15746 static tree
15747 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15748 bool explain_p)
15749 {
15750 tree copy_of_targs;
15751
15752 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15753 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15754 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15755 return NULL_TREE;
15756
15757 /* We need to make a new template argument vector for the call to
15758 unify. If we used TARGS, we'd clutter it up with the result of
15759 the attempted unification, even if this class didn't work out.
15760 We also don't want to commit ourselves to all the unifications
15761 we've already done, since unification is supposed to be done on
15762 an argument-by-argument basis. In other words, consider the
15763 following pathological case:
15764
15765 template <int I, int J, int K>
15766 struct S {};
15767
15768 template <int I, int J>
15769 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15770
15771 template <int I, int J, int K>
15772 void f(S<I, J, K>, S<I, I, I>);
15773
15774 void g() {
15775 S<0, 0, 0> s0;
15776 S<0, 1, 2> s2;
15777
15778 f(s0, s2);
15779 }
15780
15781 Now, by the time we consider the unification involving `s2', we
15782 already know that we must have `f<0, 0, 0>'. But, even though
15783 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15784 because there are two ways to unify base classes of S<0, 1, 2>
15785 with S<I, I, I>. If we kept the already deduced knowledge, we
15786 would reject the possibility I=1. */
15787 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
15788
15789 /* If unification failed, we're done. */
15790 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
15791 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
15792 return NULL_TREE;
15793
15794 return arg;
15795 }
15796
15797 /* Given a template type PARM and a class type ARG, find the unique
15798 base type in ARG that is an instance of PARM. We do not examine
15799 ARG itself; only its base-classes. If there is not exactly one
15800 appropriate base class, return NULL_TREE. PARM may be the type of
15801 a partial specialization, as well as a plain template type. Used
15802 by unify. */
15803
15804 static enum template_base_result
15805 get_template_base (tree tparms, tree targs, tree parm, tree arg,
15806 bool explain_p, tree *result)
15807 {
15808 tree rval = NULL_TREE;
15809 tree binfo;
15810
15811 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
15812
15813 binfo = TYPE_BINFO (complete_type (arg));
15814 if (!binfo)
15815 {
15816 /* The type could not be completed. */
15817 *result = NULL_TREE;
15818 return tbr_incomplete_type;
15819 }
15820
15821 /* Walk in inheritance graph order. The search order is not
15822 important, and this avoids multiple walks of virtual bases. */
15823 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
15824 {
15825 tree r = try_class_unification (tparms, targs, parm,
15826 BINFO_TYPE (binfo), explain_p);
15827
15828 if (r)
15829 {
15830 /* If there is more than one satisfactory baseclass, then:
15831
15832 [temp.deduct.call]
15833
15834 If they yield more than one possible deduced A, the type
15835 deduction fails.
15836
15837 applies. */
15838 if (rval && !same_type_p (r, rval))
15839 {
15840 *result = NULL_TREE;
15841 return tbr_ambiguous_baseclass;
15842 }
15843
15844 rval = r;
15845 }
15846 }
15847
15848 *result = rval;
15849 return tbr_success;
15850 }
15851
15852 /* Returns the level of DECL, which declares a template parameter. */
15853
15854 static int
15855 template_decl_level (tree decl)
15856 {
15857 switch (TREE_CODE (decl))
15858 {
15859 case TYPE_DECL:
15860 case TEMPLATE_DECL:
15861 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
15862
15863 case PARM_DECL:
15864 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
15865
15866 default:
15867 gcc_unreachable ();
15868 }
15869 return 0;
15870 }
15871
15872 /* Decide whether ARG can be unified with PARM, considering only the
15873 cv-qualifiers of each type, given STRICT as documented for unify.
15874 Returns nonzero iff the unification is OK on that basis. */
15875
15876 static int
15877 check_cv_quals_for_unify (int strict, tree arg, tree parm)
15878 {
15879 int arg_quals = cp_type_quals (arg);
15880 int parm_quals = cp_type_quals (parm);
15881
15882 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15883 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15884 {
15885 /* Although a CVR qualifier is ignored when being applied to a
15886 substituted template parameter ([8.3.2]/1 for example), that
15887 does not allow us to unify "const T" with "int&" because both
15888 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15889 It is ok when we're allowing additional CV qualifiers
15890 at the outer level [14.8.2.1]/3,1st bullet. */
15891 if ((TREE_CODE (arg) == REFERENCE_TYPE
15892 || TREE_CODE (arg) == FUNCTION_TYPE
15893 || TREE_CODE (arg) == METHOD_TYPE)
15894 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
15895 return 0;
15896
15897 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
15898 && (parm_quals & TYPE_QUAL_RESTRICT))
15899 return 0;
15900 }
15901
15902 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15903 && (arg_quals & parm_quals) != parm_quals)
15904 return 0;
15905
15906 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
15907 && (parm_quals & arg_quals) != arg_quals)
15908 return 0;
15909
15910 return 1;
15911 }
15912
15913 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15914 void
15915 template_parm_level_and_index (tree parm, int* level, int* index)
15916 {
15917 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15918 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15919 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15920 {
15921 *index = TEMPLATE_TYPE_IDX (parm);
15922 *level = TEMPLATE_TYPE_LEVEL (parm);
15923 }
15924 else
15925 {
15926 *index = TEMPLATE_PARM_IDX (parm);
15927 *level = TEMPLATE_PARM_LEVEL (parm);
15928 }
15929 }
15930
15931 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15932 do { \
15933 if (unify (TP, TA, P, A, S, EP)) \
15934 return 1; \
15935 } while (0);
15936
15937 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15938 expansion at the end of PACKED_PARMS. Returns 0 if the type
15939 deduction succeeds, 1 otherwise. STRICT is the same as in
15940 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15941 call argument list. We'll need to adjust the arguments to make them
15942 types. SUBR tells us if this is from a recursive call to
15943 type_unification_real, or for comparing two template argument
15944 lists. */
15945
15946 static int
15947 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
15948 tree packed_args, unification_kind_t strict,
15949 bool subr, bool explain_p)
15950 {
15951 tree parm
15952 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
15953 tree pattern = PACK_EXPANSION_PATTERN (parm);
15954 tree pack, packs = NULL_TREE;
15955 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
15956 int len = TREE_VEC_LENGTH (packed_args);
15957
15958 /* Determine the parameter packs we will be deducing from the
15959 pattern, and record their current deductions. */
15960 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
15961 pack; pack = TREE_CHAIN (pack))
15962 {
15963 tree parm_pack = TREE_VALUE (pack);
15964 int idx, level;
15965
15966 /* Determine the index and level of this parameter pack. */
15967 template_parm_level_and_index (parm_pack, &level, &idx);
15968
15969 /* Keep track of the parameter packs and their corresponding
15970 argument packs. */
15971 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
15972 TREE_TYPE (packs) = make_tree_vec (len - start);
15973 }
15974
15975 /* Loop through all of the arguments that have not yet been
15976 unified and unify each with the pattern. */
15977 for (i = start; i < len; i++)
15978 {
15979 tree parm;
15980 bool any_explicit = false;
15981 tree arg = TREE_VEC_ELT (packed_args, i);
15982
15983 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15984 or the element of its argument pack at the current index if
15985 this argument was explicitly specified. */
15986 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15987 {
15988 int idx, level;
15989 tree arg, pargs;
15990 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15991
15992 arg = NULL_TREE;
15993 if (TREE_VALUE (pack)
15994 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
15995 && (i < TREE_VEC_LENGTH (pargs)))
15996 {
15997 any_explicit = true;
15998 arg = TREE_VEC_ELT (pargs, i);
15999 }
16000 TMPL_ARG (targs, level, idx) = arg;
16001 }
16002
16003 /* If we had explicit template arguments, substitute them into the
16004 pattern before deduction. */
16005 if (any_explicit)
16006 {
16007 /* Some arguments might still be unspecified or dependent. */
16008 bool dependent;
16009 ++processing_template_decl;
16010 dependent = any_dependent_template_arguments_p (targs);
16011 if (!dependent)
16012 --processing_template_decl;
16013 parm = tsubst (pattern, targs,
16014 explain_p ? tf_warning_or_error : tf_none,
16015 NULL_TREE);
16016 if (dependent)
16017 --processing_template_decl;
16018 if (parm == error_mark_node)
16019 return 1;
16020 }
16021 else
16022 parm = pattern;
16023
16024 /* Unify the pattern with the current argument. */
16025 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
16026 LOOKUP_IMPLICIT, explain_p))
16027 return 1;
16028
16029 /* For each parameter pack, collect the deduced value. */
16030 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16031 {
16032 int idx, level;
16033 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16034
16035 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
16036 TMPL_ARG (targs, level, idx);
16037 }
16038 }
16039
16040 /* Verify that the results of unification with the parameter packs
16041 produce results consistent with what we've seen before, and make
16042 the deduced argument packs available. */
16043 for (pack = packs; pack; pack = TREE_CHAIN (pack))
16044 {
16045 tree old_pack = TREE_VALUE (pack);
16046 tree new_args = TREE_TYPE (pack);
16047 int i, len = TREE_VEC_LENGTH (new_args);
16048 int idx, level;
16049 bool nondeduced_p = false;
16050
16051 /* By default keep the original deduced argument pack.
16052 If necessary, more specific code is going to update the
16053 resulting deduced argument later down in this function. */
16054 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
16055 TMPL_ARG (targs, level, idx) = old_pack;
16056
16057 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16058 actually deduce anything. */
16059 for (i = 0; i < len && !nondeduced_p; ++i)
16060 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
16061 nondeduced_p = true;
16062 if (nondeduced_p)
16063 continue;
16064
16065 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
16066 {
16067 /* If we had fewer function args than explicit template args,
16068 just use the explicits. */
16069 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16070 int explicit_len = TREE_VEC_LENGTH (explicit_args);
16071 if (len < explicit_len)
16072 new_args = explicit_args;
16073 }
16074
16075 if (!old_pack)
16076 {
16077 tree result;
16078 /* Build the deduced *_ARGUMENT_PACK. */
16079 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
16080 {
16081 result = make_node (NONTYPE_ARGUMENT_PACK);
16082 TREE_TYPE (result) =
16083 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
16084 TREE_CONSTANT (result) = 1;
16085 }
16086 else
16087 result = cxx_make_type (TYPE_ARGUMENT_PACK);
16088
16089 SET_ARGUMENT_PACK_ARGS (result, new_args);
16090
16091 /* Note the deduced argument packs for this parameter
16092 pack. */
16093 TMPL_ARG (targs, level, idx) = result;
16094 }
16095 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
16096 && (ARGUMENT_PACK_ARGS (old_pack)
16097 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
16098 {
16099 /* We only had the explicitly-provided arguments before, but
16100 now we have a complete set of arguments. */
16101 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
16102
16103 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
16104 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
16105 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
16106 }
16107 else
16108 {
16109 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
16110 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
16111
16112 if (!comp_template_args_with_info (old_args, new_args,
16113 &bad_old_arg, &bad_new_arg))
16114 /* Inconsistent unification of this parameter pack. */
16115 return unify_parameter_pack_inconsistent (explain_p,
16116 bad_old_arg,
16117 bad_new_arg);
16118 }
16119 }
16120
16121 return unify_success (explain_p);
16122 }
16123
16124 /* Deduce the value of template parameters. TPARMS is the (innermost)
16125 set of template parameters to a template. TARGS is the bindings
16126 for those template parameters, as determined thus far; TARGS may
16127 include template arguments for outer levels of template parameters
16128 as well. PARM is a parameter to a template function, or a
16129 subcomponent of that parameter; ARG is the corresponding argument.
16130 This function attempts to match PARM with ARG in a manner
16131 consistent with the existing assignments in TARGS. If more values
16132 are deduced, then TARGS is updated.
16133
16134 Returns 0 if the type deduction succeeds, 1 otherwise. The
16135 parameter STRICT is a bitwise or of the following flags:
16136
16137 UNIFY_ALLOW_NONE:
16138 Require an exact match between PARM and ARG.
16139 UNIFY_ALLOW_MORE_CV_QUAL:
16140 Allow the deduced ARG to be more cv-qualified (by qualification
16141 conversion) than ARG.
16142 UNIFY_ALLOW_LESS_CV_QUAL:
16143 Allow the deduced ARG to be less cv-qualified than ARG.
16144 UNIFY_ALLOW_DERIVED:
16145 Allow the deduced ARG to be a template base class of ARG,
16146 or a pointer to a template base class of the type pointed to by
16147 ARG.
16148 UNIFY_ALLOW_INTEGER:
16149 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16150 case for more information.
16151 UNIFY_ALLOW_OUTER_LEVEL:
16152 This is the outermost level of a deduction. Used to determine validity
16153 of qualification conversions. A valid qualification conversion must
16154 have const qualified pointers leading up to the inner type which
16155 requires additional CV quals, except at the outer level, where const
16156 is not required [conv.qual]. It would be normal to set this flag in
16157 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16158 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16159 This is the outermost level of a deduction, and PARM can be more CV
16160 qualified at this point.
16161 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16162 This is the outermost level of a deduction, and PARM can be less CV
16163 qualified at this point. */
16164
16165 static int
16166 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
16167 bool explain_p)
16168 {
16169 int idx;
16170 tree targ;
16171 tree tparm;
16172 int strict_in = strict;
16173
16174 /* I don't think this will do the right thing with respect to types.
16175 But the only case I've seen it in so far has been array bounds, where
16176 signedness is the only information lost, and I think that will be
16177 okay. */
16178 while (TREE_CODE (parm) == NOP_EXPR)
16179 parm = TREE_OPERAND (parm, 0);
16180
16181 if (arg == error_mark_node)
16182 return unify_invalid (explain_p);
16183 if (arg == unknown_type_node
16184 || arg == init_list_type_node)
16185 /* We can't deduce anything from this, but we might get all the
16186 template args from other function args. */
16187 return unify_success (explain_p);
16188
16189 /* If PARM uses template parameters, then we can't bail out here,
16190 even if ARG == PARM, since we won't record unifications for the
16191 template parameters. We might need them if we're trying to
16192 figure out which of two things is more specialized. */
16193 if (arg == parm && !uses_template_parms (parm))
16194 return unify_success (explain_p);
16195
16196 /* Handle init lists early, so the rest of the function can assume
16197 we're dealing with a type. */
16198 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
16199 {
16200 tree elt, elttype;
16201 unsigned i;
16202 tree orig_parm = parm;
16203
16204 /* Replace T with std::initializer_list<T> for deduction. */
16205 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16206 && flag_deduce_init_list)
16207 parm = listify (parm);
16208
16209 if (!is_std_init_list (parm))
16210 /* We can only deduce from an initializer list argument if the
16211 parameter is std::initializer_list; otherwise this is a
16212 non-deduced context. */
16213 return unify_success (explain_p);
16214
16215 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
16216
16217 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
16218 {
16219 int elt_strict = strict;
16220
16221 if (elt == error_mark_node)
16222 return unify_invalid (explain_p);
16223
16224 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
16225 {
16226 tree type = TREE_TYPE (elt);
16227 /* It should only be possible to get here for a call. */
16228 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
16229 elt_strict |= maybe_adjust_types_for_deduction
16230 (DEDUCE_CALL, &elttype, &type, elt);
16231 elt = type;
16232 }
16233
16234 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
16235 explain_p);
16236 }
16237
16238 /* If the std::initializer_list<T> deduction worked, replace the
16239 deduced A with std::initializer_list<A>. */
16240 if (orig_parm != parm)
16241 {
16242 idx = TEMPLATE_TYPE_IDX (orig_parm);
16243 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16244 targ = listify (targ);
16245 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
16246 }
16247 return unify_success (explain_p);
16248 }
16249
16250 /* Immediately reject some pairs that won't unify because of
16251 cv-qualification mismatches. */
16252 if (TREE_CODE (arg) == TREE_CODE (parm)
16253 && TYPE_P (arg)
16254 /* It is the elements of the array which hold the cv quals of an array
16255 type, and the elements might be template type parms. We'll check
16256 when we recurse. */
16257 && TREE_CODE (arg) != ARRAY_TYPE
16258 /* We check the cv-qualifiers when unifying with template type
16259 parameters below. We want to allow ARG `const T' to unify with
16260 PARM `T' for example, when computing which of two templates
16261 is more specialized, for example. */
16262 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
16263 && !check_cv_quals_for_unify (strict_in, arg, parm))
16264 return unify_cv_qual_mismatch (explain_p, parm, arg);
16265
16266 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
16267 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
16268 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
16269 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
16270 strict &= ~UNIFY_ALLOW_DERIVED;
16271 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16272 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
16273
16274 switch (TREE_CODE (parm))
16275 {
16276 case TYPENAME_TYPE:
16277 case SCOPE_REF:
16278 case UNBOUND_CLASS_TEMPLATE:
16279 /* In a type which contains a nested-name-specifier, template
16280 argument values cannot be deduced for template parameters used
16281 within the nested-name-specifier. */
16282 return unify_success (explain_p);
16283
16284 case TEMPLATE_TYPE_PARM:
16285 case TEMPLATE_TEMPLATE_PARM:
16286 case BOUND_TEMPLATE_TEMPLATE_PARM:
16287 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16288 if (tparm == error_mark_node)
16289 return unify_invalid (explain_p);
16290
16291 if (TEMPLATE_TYPE_LEVEL (parm)
16292 != template_decl_level (tparm))
16293 /* The PARM is not one we're trying to unify. Just check
16294 to see if it matches ARG. */
16295 {
16296 if (TREE_CODE (arg) == TREE_CODE (parm)
16297 && (is_auto (parm) ? is_auto (arg)
16298 : same_type_p (parm, arg)))
16299 return unify_success (explain_p);
16300 else
16301 return unify_type_mismatch (explain_p, parm, arg);
16302 }
16303 idx = TEMPLATE_TYPE_IDX (parm);
16304 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16305 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
16306 if (tparm == error_mark_node)
16307 return unify_invalid (explain_p);
16308
16309 /* Check for mixed types and values. */
16310 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16311 && TREE_CODE (tparm) != TYPE_DECL)
16312 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16313 && TREE_CODE (tparm) != TEMPLATE_DECL))
16314 gcc_unreachable ();
16315
16316 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16317 {
16318 /* ARG must be constructed from a template class or a template
16319 template parameter. */
16320 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
16321 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
16322 return unify_template_deduction_failure (explain_p, parm, arg);
16323
16324 {
16325 tree parmvec = TYPE_TI_ARGS (parm);
16326 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
16327 tree full_argvec = add_to_template_args (targs, argvec);
16328 tree parm_parms
16329 = DECL_INNERMOST_TEMPLATE_PARMS
16330 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
16331 int i, len;
16332 int parm_variadic_p = 0;
16333
16334 /* The resolution to DR150 makes clear that default
16335 arguments for an N-argument may not be used to bind T
16336 to a template template parameter with fewer than N
16337 parameters. It is not safe to permit the binding of
16338 default arguments as an extension, as that may change
16339 the meaning of a conforming program. Consider:
16340
16341 struct Dense { static const unsigned int dim = 1; };
16342
16343 template <template <typename> class View,
16344 typename Block>
16345 void operator+(float, View<Block> const&);
16346
16347 template <typename Block,
16348 unsigned int Dim = Block::dim>
16349 struct Lvalue_proxy { operator float() const; };
16350
16351 void
16352 test_1d (void) {
16353 Lvalue_proxy<Dense> p;
16354 float b;
16355 b + p;
16356 }
16357
16358 Here, if Lvalue_proxy is permitted to bind to View, then
16359 the global operator+ will be used; if they are not, the
16360 Lvalue_proxy will be converted to float. */
16361 if (coerce_template_parms (parm_parms,
16362 full_argvec,
16363 TYPE_TI_TEMPLATE (parm),
16364 (explain_p
16365 ? tf_warning_or_error
16366 : tf_none),
16367 /*require_all_args=*/true,
16368 /*use_default_args=*/false)
16369 == error_mark_node)
16370 return 1;
16371
16372 /* Deduce arguments T, i from TT<T> or TT<i>.
16373 We check each element of PARMVEC and ARGVEC individually
16374 rather than the whole TREE_VEC since they can have
16375 different number of elements. */
16376
16377 parmvec = expand_template_argument_pack (parmvec);
16378 argvec = expand_template_argument_pack (argvec);
16379
16380 len = TREE_VEC_LENGTH (parmvec);
16381
16382 /* Check if the parameters end in a pack, making them
16383 variadic. */
16384 if (len > 0
16385 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
16386 parm_variadic_p = 1;
16387
16388 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
16389 return unify_too_few_arguments (explain_p,
16390 TREE_VEC_LENGTH (argvec), len);
16391
16392 for (i = 0; i < len - parm_variadic_p; ++i)
16393 {
16394 RECUR_AND_CHECK_FAILURE (tparms, targs,
16395 TREE_VEC_ELT (parmvec, i),
16396 TREE_VEC_ELT (argvec, i),
16397 UNIFY_ALLOW_NONE, explain_p);
16398 }
16399
16400 if (parm_variadic_p
16401 && unify_pack_expansion (tparms, targs,
16402 parmvec, argvec,
16403 DEDUCE_EXACT,
16404 /*subr=*/true, explain_p))
16405 return 1;
16406 }
16407 arg = TYPE_TI_TEMPLATE (arg);
16408
16409 /* Fall through to deduce template name. */
16410 }
16411
16412 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16413 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16414 {
16415 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16416
16417 /* Simple cases: Value already set, does match or doesn't. */
16418 if (targ != NULL_TREE && template_args_equal (targ, arg))
16419 return unify_success (explain_p);
16420 else if (targ)
16421 return unify_inconsistency (explain_p, parm, targ, arg);
16422 }
16423 else
16424 {
16425 /* If PARM is `const T' and ARG is only `int', we don't have
16426 a match unless we are allowing additional qualification.
16427 If ARG is `const int' and PARM is just `T' that's OK;
16428 that binds `const int' to `T'. */
16429 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
16430 arg, parm))
16431 return unify_cv_qual_mismatch (explain_p, parm, arg);
16432
16433 /* Consider the case where ARG is `const volatile int' and
16434 PARM is `const T'. Then, T should be `volatile int'. */
16435 arg = cp_build_qualified_type_real
16436 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
16437 if (arg == error_mark_node)
16438 return unify_invalid (explain_p);
16439
16440 /* Simple cases: Value already set, does match or doesn't. */
16441 if (targ != NULL_TREE && same_type_p (targ, arg))
16442 return unify_success (explain_p);
16443 else if (targ)
16444 return unify_inconsistency (explain_p, parm, targ, arg);
16445
16446 /* Make sure that ARG is not a variable-sized array. (Note
16447 that were talking about variable-sized arrays (like
16448 `int[n]'), rather than arrays of unknown size (like
16449 `int[]').) We'll get very confused by such a type since
16450 the bound of the array is not constant, and therefore
16451 not mangleable. Besides, such types are not allowed in
16452 ISO C++, so we can do as we please here. We do allow
16453 them for 'auto' deduction, since that isn't ABI-exposed. */
16454 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
16455 return unify_vla_arg (explain_p, arg);
16456
16457 /* Strip typedefs as in convert_template_argument. */
16458 arg = canonicalize_type_argument (arg, tf_none);
16459 }
16460
16461 /* If ARG is a parameter pack or an expansion, we cannot unify
16462 against it unless PARM is also a parameter pack. */
16463 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16464 && !template_parameter_pack_p (parm))
16465 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16466
16467 /* If the argument deduction results is a METHOD_TYPE,
16468 then there is a problem.
16469 METHOD_TYPE doesn't map to any real C++ type the result of
16470 the deduction can not be of that type. */
16471 if (TREE_CODE (arg) == METHOD_TYPE)
16472 return unify_method_type_error (explain_p, arg);
16473
16474 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16475 return unify_success (explain_p);
16476
16477 case TEMPLATE_PARM_INDEX:
16478 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16479 if (tparm == error_mark_node)
16480 return unify_invalid (explain_p);
16481
16482 if (TEMPLATE_PARM_LEVEL (parm)
16483 != template_decl_level (tparm))
16484 {
16485 /* The PARM is not one we're trying to unify. Just check
16486 to see if it matches ARG. */
16487 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
16488 && cp_tree_equal (parm, arg));
16489 if (result)
16490 unify_expression_unequal (explain_p, parm, arg);
16491 return result;
16492 }
16493
16494 idx = TEMPLATE_PARM_IDX (parm);
16495 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16496
16497 if (targ)
16498 {
16499 int x = !cp_tree_equal (targ, arg);
16500 if (x)
16501 unify_inconsistency (explain_p, parm, targ, arg);
16502 return x;
16503 }
16504
16505 /* [temp.deduct.type] If, in the declaration of a function template
16506 with a non-type template-parameter, the non-type
16507 template-parameter is used in an expression in the function
16508 parameter-list and, if the corresponding template-argument is
16509 deduced, the template-argument type shall match the type of the
16510 template-parameter exactly, except that a template-argument
16511 deduced from an array bound may be of any integral type.
16512 The non-type parameter might use already deduced type parameters. */
16513 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
16514 if (!TREE_TYPE (arg))
16515 /* Template-parameter dependent expression. Just accept it for now.
16516 It will later be processed in convert_template_argument. */
16517 ;
16518 else if (same_type_p (TREE_TYPE (arg), tparm))
16519 /* OK */;
16520 else if ((strict & UNIFY_ALLOW_INTEGER)
16521 && (TREE_CODE (tparm) == INTEGER_TYPE
16522 || TREE_CODE (tparm) == BOOLEAN_TYPE))
16523 /* Convert the ARG to the type of PARM; the deduced non-type
16524 template argument must exactly match the types of the
16525 corresponding parameter. */
16526 arg = fold (build_nop (tparm, arg));
16527 else if (uses_template_parms (tparm))
16528 /* We haven't deduced the type of this parameter yet. Try again
16529 later. */
16530 return unify_success (explain_p);
16531 else
16532 return unify_type_mismatch (explain_p, tparm, arg);
16533
16534 /* If ARG is a parameter pack or an expansion, we cannot unify
16535 against it unless PARM is also a parameter pack. */
16536 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16537 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
16538 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16539
16540 arg = strip_typedefs_expr (arg);
16541 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16542 return unify_success (explain_p);
16543
16544 case PTRMEM_CST:
16545 {
16546 /* A pointer-to-member constant can be unified only with
16547 another constant. */
16548 if (TREE_CODE (arg) != PTRMEM_CST)
16549 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
16550
16551 /* Just unify the class member. It would be useless (and possibly
16552 wrong, depending on the strict flags) to unify also
16553 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16554 arg refer to the same variable, even if through different
16555 classes. For instance:
16556
16557 struct A { int x; };
16558 struct B : A { };
16559
16560 Unification of &A::x and &B::x must succeed. */
16561 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
16562 PTRMEM_CST_MEMBER (arg), strict, explain_p);
16563 }
16564
16565 case POINTER_TYPE:
16566 {
16567 if (TREE_CODE (arg) != POINTER_TYPE)
16568 return unify_type_mismatch (explain_p, parm, arg);
16569
16570 /* [temp.deduct.call]
16571
16572 A can be another pointer or pointer to member type that can
16573 be converted to the deduced A via a qualification
16574 conversion (_conv.qual_).
16575
16576 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16577 This will allow for additional cv-qualification of the
16578 pointed-to types if appropriate. */
16579
16580 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16581 /* The derived-to-base conversion only persists through one
16582 level of pointers. */
16583 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16584
16585 return unify (tparms, targs, TREE_TYPE (parm),
16586 TREE_TYPE (arg), strict, explain_p);
16587 }
16588
16589 case REFERENCE_TYPE:
16590 if (TREE_CODE (arg) != REFERENCE_TYPE)
16591 return unify_type_mismatch (explain_p, parm, arg);
16592 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16593 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16594
16595 case ARRAY_TYPE:
16596 if (TREE_CODE (arg) != ARRAY_TYPE)
16597 return unify_type_mismatch (explain_p, parm, arg);
16598 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16599 != (TYPE_DOMAIN (arg) == NULL_TREE))
16600 return unify_type_mismatch (explain_p, parm, arg);
16601 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16602 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16603 if (TYPE_DOMAIN (parm) != NULL_TREE)
16604 {
16605 tree parm_max;
16606 tree arg_max;
16607 bool parm_cst;
16608 bool arg_cst;
16609
16610 /* Our representation of array types uses "N - 1" as the
16611 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16612 not an integer constant. We cannot unify arbitrarily
16613 complex expressions, so we eliminate the MINUS_EXPRs
16614 here. */
16615 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16616 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16617 if (!parm_cst)
16618 {
16619 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16620 parm_max = TREE_OPERAND (parm_max, 0);
16621 }
16622 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16623 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16624 if (!arg_cst)
16625 {
16626 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16627 trying to unify the type of a variable with the type
16628 of a template parameter. For example:
16629
16630 template <unsigned int N>
16631 void f (char (&) [N]);
16632 int g();
16633 void h(int i) {
16634 char a[g(i)];
16635 f(a);
16636 }
16637
16638 Here, the type of the ARG will be "int [g(i)]", and
16639 may be a SAVE_EXPR, etc. */
16640 if (TREE_CODE (arg_max) != MINUS_EXPR)
16641 return unify_vla_arg (explain_p, arg);
16642 arg_max = TREE_OPERAND (arg_max, 0);
16643 }
16644
16645 /* If only one of the bounds used a MINUS_EXPR, compensate
16646 by adding one to the other bound. */
16647 if (parm_cst && !arg_cst)
16648 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16649 integer_type_node,
16650 parm_max,
16651 integer_one_node);
16652 else if (arg_cst && !parm_cst)
16653 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16654 integer_type_node,
16655 arg_max,
16656 integer_one_node);
16657
16658 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16659 UNIFY_ALLOW_INTEGER, explain_p);
16660 }
16661 return unify_success (explain_p);
16662
16663 case REAL_TYPE:
16664 case COMPLEX_TYPE:
16665 case VECTOR_TYPE:
16666 case INTEGER_TYPE:
16667 case BOOLEAN_TYPE:
16668 case ENUMERAL_TYPE:
16669 case VOID_TYPE:
16670 case NULLPTR_TYPE:
16671 if (TREE_CODE (arg) != TREE_CODE (parm))
16672 return unify_type_mismatch (explain_p, parm, arg);
16673
16674 /* We have already checked cv-qualification at the top of the
16675 function. */
16676 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16677 return unify_type_mismatch (explain_p, parm, arg);
16678
16679 /* As far as unification is concerned, this wins. Later checks
16680 will invalidate it if necessary. */
16681 return unify_success (explain_p);
16682
16683 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16684 /* Type INTEGER_CST can come from ordinary constant template args. */
16685 case INTEGER_CST:
16686 while (TREE_CODE (arg) == NOP_EXPR)
16687 arg = TREE_OPERAND (arg, 0);
16688
16689 if (TREE_CODE (arg) != INTEGER_CST)
16690 return unify_template_argument_mismatch (explain_p, parm, arg);
16691 return (tree_int_cst_equal (parm, arg)
16692 ? unify_success (explain_p)
16693 : unify_template_argument_mismatch (explain_p, parm, arg));
16694
16695 case TREE_VEC:
16696 {
16697 int i, len, argslen;
16698 int parm_variadic_p = 0;
16699
16700 if (TREE_CODE (arg) != TREE_VEC)
16701 return unify_template_argument_mismatch (explain_p, parm, arg);
16702
16703 len = TREE_VEC_LENGTH (parm);
16704 argslen = TREE_VEC_LENGTH (arg);
16705
16706 /* Check for pack expansions in the parameters. */
16707 for (i = 0; i < len; ++i)
16708 {
16709 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
16710 {
16711 if (i == len - 1)
16712 /* We can unify against something with a trailing
16713 parameter pack. */
16714 parm_variadic_p = 1;
16715 else
16716 /* [temp.deduct.type]/9: If the template argument list of
16717 P contains a pack expansion that is not the last
16718 template argument, the entire template argument list
16719 is a non-deduced context. */
16720 return unify_success (explain_p);
16721 }
16722 }
16723
16724 /* If we don't have enough arguments to satisfy the parameters
16725 (not counting the pack expression at the end), or we have
16726 too many arguments for a parameter list that doesn't end in
16727 a pack expression, we can't unify. */
16728 if (parm_variadic_p
16729 ? argslen < len - parm_variadic_p
16730 : argslen != len)
16731 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
16732
16733 /* Unify all of the parameters that precede the (optional)
16734 pack expression. */
16735 for (i = 0; i < len - parm_variadic_p; ++i)
16736 {
16737 RECUR_AND_CHECK_FAILURE (tparms, targs,
16738 TREE_VEC_ELT (parm, i),
16739 TREE_VEC_ELT (arg, i),
16740 UNIFY_ALLOW_NONE, explain_p);
16741 }
16742 if (parm_variadic_p)
16743 return unify_pack_expansion (tparms, targs, parm, arg,
16744 DEDUCE_EXACT,
16745 /*subr=*/true, explain_p);
16746 return unify_success (explain_p);
16747 }
16748
16749 case RECORD_TYPE:
16750 case UNION_TYPE:
16751 if (TREE_CODE (arg) != TREE_CODE (parm))
16752 return unify_type_mismatch (explain_p, parm, arg);
16753
16754 if (TYPE_PTRMEMFUNC_P (parm))
16755 {
16756 if (!TYPE_PTRMEMFUNC_P (arg))
16757 return unify_type_mismatch (explain_p, parm, arg);
16758
16759 return unify (tparms, targs,
16760 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16761 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16762 strict, explain_p);
16763 }
16764
16765 if (CLASSTYPE_TEMPLATE_INFO (parm))
16766 {
16767 tree t = NULL_TREE;
16768
16769 if (strict_in & UNIFY_ALLOW_DERIVED)
16770 {
16771 /* First, we try to unify the PARM and ARG directly. */
16772 t = try_class_unification (tparms, targs,
16773 parm, arg, explain_p);
16774
16775 if (!t)
16776 {
16777 /* Fallback to the special case allowed in
16778 [temp.deduct.call]:
16779
16780 If P is a class, and P has the form
16781 template-id, then A can be a derived class of
16782 the deduced A. Likewise, if P is a pointer to
16783 a class of the form template-id, A can be a
16784 pointer to a derived class pointed to by the
16785 deduced A. */
16786 enum template_base_result r;
16787 r = get_template_base (tparms, targs, parm, arg,
16788 explain_p, &t);
16789
16790 if (!t)
16791 return unify_no_common_base (explain_p, r, parm, arg);
16792 }
16793 }
16794 else if (CLASSTYPE_TEMPLATE_INFO (arg)
16795 && (CLASSTYPE_TI_TEMPLATE (parm)
16796 == CLASSTYPE_TI_TEMPLATE (arg)))
16797 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16798 Then, we should unify `int' and `U'. */
16799 t = arg;
16800 else
16801 /* There's no chance of unification succeeding. */
16802 return unify_type_mismatch (explain_p, parm, arg);
16803
16804 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
16805 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
16806 }
16807 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
16808 return unify_type_mismatch (explain_p, parm, arg);
16809 return unify_success (explain_p);
16810
16811 case METHOD_TYPE:
16812 case FUNCTION_TYPE:
16813 {
16814 unsigned int nargs;
16815 tree *args;
16816 tree a;
16817 unsigned int i;
16818
16819 if (TREE_CODE (arg) != TREE_CODE (parm))
16820 return unify_type_mismatch (explain_p, parm, arg);
16821
16822 /* CV qualifications for methods can never be deduced, they must
16823 match exactly. We need to check them explicitly here,
16824 because type_unification_real treats them as any other
16825 cv-qualified parameter. */
16826 if (TREE_CODE (parm) == METHOD_TYPE
16827 && (!check_cv_quals_for_unify
16828 (UNIFY_ALLOW_NONE,
16829 class_of_this_parm (arg),
16830 class_of_this_parm (parm))))
16831 return unify_cv_qual_mismatch (explain_p, parm, arg);
16832
16833 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
16834 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
16835
16836 nargs = list_length (TYPE_ARG_TYPES (arg));
16837 args = XALLOCAVEC (tree, nargs);
16838 for (a = TYPE_ARG_TYPES (arg), i = 0;
16839 a != NULL_TREE && a != void_list_node;
16840 a = TREE_CHAIN (a), ++i)
16841 args[i] = TREE_VALUE (a);
16842 nargs = i;
16843
16844 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
16845 args, nargs, 1, DEDUCE_EXACT,
16846 LOOKUP_NORMAL, explain_p);
16847 }
16848
16849 case OFFSET_TYPE:
16850 /* Unify a pointer to member with a pointer to member function, which
16851 deduces the type of the member as a function type. */
16852 if (TYPE_PTRMEMFUNC_P (arg))
16853 {
16854 tree method_type;
16855 tree fntype;
16856
16857 /* Check top-level cv qualifiers */
16858 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
16859 return unify_cv_qual_mismatch (explain_p, parm, arg);
16860
16861 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16862 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
16863 UNIFY_ALLOW_NONE, explain_p);
16864
16865 /* Determine the type of the function we are unifying against. */
16866 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
16867 fntype =
16868 build_function_type (TREE_TYPE (method_type),
16869 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
16870
16871 /* Extract the cv-qualifiers of the member function from the
16872 implicit object parameter and place them on the function
16873 type to be restored later. */
16874 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
16875 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
16876 }
16877
16878 if (TREE_CODE (arg) != OFFSET_TYPE)
16879 return unify_type_mismatch (explain_p, parm, arg);
16880 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16881 TYPE_OFFSET_BASETYPE (arg),
16882 UNIFY_ALLOW_NONE, explain_p);
16883 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16884 strict, explain_p);
16885
16886 case CONST_DECL:
16887 if (DECL_TEMPLATE_PARM_P (parm))
16888 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
16889 if (arg != integral_constant_value (parm))
16890 return unify_template_argument_mismatch (explain_p, parm, arg);
16891 return unify_success (explain_p);
16892
16893 case FIELD_DECL:
16894 case TEMPLATE_DECL:
16895 /* Matched cases are handled by the ARG == PARM test above. */
16896 return unify_template_argument_mismatch (explain_p, parm, arg);
16897
16898 case VAR_DECL:
16899 /* A non-type template parameter that is a variable should be a
16900 an integral constant, in which case, it whould have been
16901 folded into its (constant) value. So we should not be getting
16902 a variable here. */
16903 gcc_unreachable ();
16904
16905 case TYPE_ARGUMENT_PACK:
16906 case NONTYPE_ARGUMENT_PACK:
16907 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
16908 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
16909
16910 case TYPEOF_TYPE:
16911 case DECLTYPE_TYPE:
16912 case UNDERLYING_TYPE:
16913 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16914 or UNDERLYING_TYPE nodes. */
16915 return unify_success (explain_p);
16916
16917 case ERROR_MARK:
16918 /* Unification fails if we hit an error node. */
16919 return unify_invalid (explain_p);
16920
16921 default:
16922 /* An unresolved overload is a nondeduced context. */
16923 if (is_overloaded_fn (parm) || type_unknown_p (parm))
16924 return unify_success (explain_p);
16925 gcc_assert (EXPR_P (parm));
16926
16927 /* We must be looking at an expression. This can happen with
16928 something like:
16929
16930 template <int I>
16931 void foo(S<I>, S<I + 2>);
16932
16933 This is a "nondeduced context":
16934
16935 [deduct.type]
16936
16937 The nondeduced contexts are:
16938
16939 --A type that is a template-id in which one or more of
16940 the template-arguments is an expression that references
16941 a template-parameter.
16942
16943 In these cases, we assume deduction succeeded, but don't
16944 actually infer any unifications. */
16945
16946 if (!uses_template_parms (parm)
16947 && !template_args_equal (parm, arg))
16948 return unify_expression_unequal (explain_p, parm, arg);
16949 else
16950 return unify_success (explain_p);
16951 }
16952 }
16953 #undef RECUR_AND_CHECK_FAILURE
16954 \f
16955 /* Note that DECL can be defined in this translation unit, if
16956 required. */
16957
16958 static void
16959 mark_definable (tree decl)
16960 {
16961 tree clone;
16962 DECL_NOT_REALLY_EXTERN (decl) = 1;
16963 FOR_EACH_CLONE (clone, decl)
16964 DECL_NOT_REALLY_EXTERN (clone) = 1;
16965 }
16966
16967 /* Called if RESULT is explicitly instantiated, or is a member of an
16968 explicitly instantiated class. */
16969
16970 void
16971 mark_decl_instantiated (tree result, int extern_p)
16972 {
16973 SET_DECL_EXPLICIT_INSTANTIATION (result);
16974
16975 /* If this entity has already been written out, it's too late to
16976 make any modifications. */
16977 if (TREE_ASM_WRITTEN (result))
16978 return;
16979
16980 if (TREE_CODE (result) != FUNCTION_DECL)
16981 /* The TREE_PUBLIC flag for function declarations will have been
16982 set correctly by tsubst. */
16983 TREE_PUBLIC (result) = 1;
16984
16985 /* This might have been set by an earlier implicit instantiation. */
16986 DECL_COMDAT (result) = 0;
16987
16988 if (extern_p)
16989 DECL_NOT_REALLY_EXTERN (result) = 0;
16990 else
16991 {
16992 mark_definable (result);
16993 /* Always make artificials weak. */
16994 if (DECL_ARTIFICIAL (result) && flag_weak)
16995 comdat_linkage (result);
16996 /* For WIN32 we also want to put explicit instantiations in
16997 linkonce sections. */
16998 else if (TREE_PUBLIC (result))
16999 maybe_make_one_only (result);
17000 }
17001
17002 /* If EXTERN_P, then this function will not be emitted -- unless
17003 followed by an explicit instantiation, at which point its linkage
17004 will be adjusted. If !EXTERN_P, then this function will be
17005 emitted here. In neither circumstance do we want
17006 import_export_decl to adjust the linkage. */
17007 DECL_INTERFACE_KNOWN (result) = 1;
17008 }
17009
17010 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17011 important template arguments. If any are missing, we check whether
17012 they're important by using error_mark_node for substituting into any
17013 args that were used for partial ordering (the ones between ARGS and END)
17014 and seeing if it bubbles up. */
17015
17016 static bool
17017 check_undeduced_parms (tree targs, tree args, tree end)
17018 {
17019 bool found = false;
17020 int i;
17021 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
17022 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
17023 {
17024 found = true;
17025 TREE_VEC_ELT (targs, i) = error_mark_node;
17026 }
17027 if (found)
17028 {
17029 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
17030 if (substed == error_mark_node)
17031 return true;
17032 }
17033 return false;
17034 }
17035
17036 /* Given two function templates PAT1 and PAT2, return:
17037
17038 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17039 -1 if PAT2 is more specialized than PAT1.
17040 0 if neither is more specialized.
17041
17042 LEN indicates the number of parameters we should consider
17043 (defaulted parameters should not be considered).
17044
17045 The 1998 std underspecified function template partial ordering, and
17046 DR214 addresses the issue. We take pairs of arguments, one from
17047 each of the templates, and deduce them against each other. One of
17048 the templates will be more specialized if all the *other*
17049 template's arguments deduce against its arguments and at least one
17050 of its arguments *does* *not* deduce against the other template's
17051 corresponding argument. Deduction is done as for class templates.
17052 The arguments used in deduction have reference and top level cv
17053 qualifiers removed. Iff both arguments were originally reference
17054 types *and* deduction succeeds in both directions, the template
17055 with the more cv-qualified argument wins for that pairing (if
17056 neither is more cv-qualified, they both are equal). Unlike regular
17057 deduction, after all the arguments have been deduced in this way,
17058 we do *not* verify the deduced template argument values can be
17059 substituted into non-deduced contexts.
17060
17061 The logic can be a bit confusing here, because we look at deduce1 and
17062 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17063 can find template arguments for pat1 to make arg1 look like arg2, that
17064 means that arg2 is at least as specialized as arg1. */
17065
17066 int
17067 more_specialized_fn (tree pat1, tree pat2, int len)
17068 {
17069 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
17070 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
17071 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
17072 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
17073 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
17074 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
17075 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
17076 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
17077 tree origs1, origs2;
17078 bool lose1 = false;
17079 bool lose2 = false;
17080
17081 /* Remove the this parameter from non-static member functions. If
17082 one is a non-static member function and the other is not a static
17083 member function, remove the first parameter from that function
17084 also. This situation occurs for operator functions where we
17085 locate both a member function (with this pointer) and non-member
17086 operator (with explicit first operand). */
17087 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
17088 {
17089 len--; /* LEN is the number of significant arguments for DECL1 */
17090 args1 = TREE_CHAIN (args1);
17091 if (!DECL_STATIC_FUNCTION_P (decl2))
17092 args2 = TREE_CHAIN (args2);
17093 }
17094 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
17095 {
17096 args2 = TREE_CHAIN (args2);
17097 if (!DECL_STATIC_FUNCTION_P (decl1))
17098 {
17099 len--;
17100 args1 = TREE_CHAIN (args1);
17101 }
17102 }
17103
17104 /* If only one is a conversion operator, they are unordered. */
17105 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
17106 return 0;
17107
17108 /* Consider the return type for a conversion function */
17109 if (DECL_CONV_FN_P (decl1))
17110 {
17111 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
17112 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
17113 len++;
17114 }
17115
17116 processing_template_decl++;
17117
17118 origs1 = args1;
17119 origs2 = args2;
17120
17121 while (len--
17122 /* Stop when an ellipsis is seen. */
17123 && args1 != NULL_TREE && args2 != NULL_TREE)
17124 {
17125 tree arg1 = TREE_VALUE (args1);
17126 tree arg2 = TREE_VALUE (args2);
17127 int deduce1, deduce2;
17128 int quals1 = -1;
17129 int quals2 = -1;
17130
17131 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17132 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17133 {
17134 /* When both arguments are pack expansions, we need only
17135 unify the patterns themselves. */
17136 arg1 = PACK_EXPANSION_PATTERN (arg1);
17137 arg2 = PACK_EXPANSION_PATTERN (arg2);
17138
17139 /* This is the last comparison we need to do. */
17140 len = 0;
17141 }
17142
17143 if (TREE_CODE (arg1) == REFERENCE_TYPE)
17144 {
17145 arg1 = TREE_TYPE (arg1);
17146 quals1 = cp_type_quals (arg1);
17147 }
17148
17149 if (TREE_CODE (arg2) == REFERENCE_TYPE)
17150 {
17151 arg2 = TREE_TYPE (arg2);
17152 quals2 = cp_type_quals (arg2);
17153 }
17154
17155 arg1 = TYPE_MAIN_VARIANT (arg1);
17156 arg2 = TYPE_MAIN_VARIANT (arg2);
17157
17158 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
17159 {
17160 int i, len2 = list_length (args2);
17161 tree parmvec = make_tree_vec (1);
17162 tree argvec = make_tree_vec (len2);
17163 tree ta = args2;
17164
17165 /* Setup the parameter vector, which contains only ARG1. */
17166 TREE_VEC_ELT (parmvec, 0) = arg1;
17167
17168 /* Setup the argument vector, which contains the remaining
17169 arguments. */
17170 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
17171 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17172
17173 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
17174 argvec, DEDUCE_EXACT,
17175 /*subr=*/true, /*explain_p=*/false)
17176 == 0);
17177
17178 /* We cannot deduce in the other direction, because ARG1 is
17179 a pack expansion but ARG2 is not. */
17180 deduce2 = 0;
17181 }
17182 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17183 {
17184 int i, len1 = list_length (args1);
17185 tree parmvec = make_tree_vec (1);
17186 tree argvec = make_tree_vec (len1);
17187 tree ta = args1;
17188
17189 /* Setup the parameter vector, which contains only ARG1. */
17190 TREE_VEC_ELT (parmvec, 0) = arg2;
17191
17192 /* Setup the argument vector, which contains the remaining
17193 arguments. */
17194 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
17195 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
17196
17197 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
17198 argvec, DEDUCE_EXACT,
17199 /*subr=*/true, /*explain_p=*/false)
17200 == 0);
17201
17202 /* We cannot deduce in the other direction, because ARG2 is
17203 a pack expansion but ARG1 is not.*/
17204 deduce1 = 0;
17205 }
17206
17207 else
17208 {
17209 /* The normal case, where neither argument is a pack
17210 expansion. */
17211 deduce1 = (unify (tparms1, targs1, arg1, arg2,
17212 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17213 == 0);
17214 deduce2 = (unify (tparms2, targs2, arg2, arg1,
17215 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17216 == 0);
17217 }
17218
17219 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17220 arg2, then arg2 is not as specialized as arg1. */
17221 if (!deduce1)
17222 lose2 = true;
17223 if (!deduce2)
17224 lose1 = true;
17225
17226 /* "If, for a given type, deduction succeeds in both directions
17227 (i.e., the types are identical after the transformations above)
17228 and if the type from the argument template is more cv-qualified
17229 than the type from the parameter template (as described above)
17230 that type is considered to be more specialized than the other. If
17231 neither type is more cv-qualified than the other then neither type
17232 is more specialized than the other." */
17233
17234 if (deduce1 && deduce2
17235 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
17236 {
17237 if ((quals1 & quals2) == quals2)
17238 lose2 = true;
17239 if ((quals1 & quals2) == quals1)
17240 lose1 = true;
17241 }
17242
17243 if (lose1 && lose2)
17244 /* We've failed to deduce something in either direction.
17245 These must be unordered. */
17246 break;
17247
17248 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17249 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17250 /* We have already processed all of the arguments in our
17251 handing of the pack expansion type. */
17252 len = 0;
17253
17254 args1 = TREE_CHAIN (args1);
17255 args2 = TREE_CHAIN (args2);
17256 }
17257
17258 /* "In most cases, all template parameters must have values in order for
17259 deduction to succeed, but for partial ordering purposes a template
17260 parameter may remain without a value provided it is not used in the
17261 types being used for partial ordering."
17262
17263 Thus, if we are missing any of the targs1 we need to substitute into
17264 origs1, then pat2 is not as specialized as pat1. This can happen when
17265 there is a nondeduced context. */
17266 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
17267 lose2 = true;
17268 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
17269 lose1 = true;
17270
17271 processing_template_decl--;
17272
17273 /* All things being equal, if the next argument is a pack expansion
17274 for one function but not for the other, prefer the
17275 non-variadic function. FIXME this is bogus; see c++/41958. */
17276 if (lose1 == lose2
17277 && args1 && TREE_VALUE (args1)
17278 && args2 && TREE_VALUE (args2))
17279 {
17280 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
17281 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
17282 }
17283
17284 if (lose1 == lose2)
17285 return 0;
17286 else if (!lose1)
17287 return 1;
17288 else
17289 return -1;
17290 }
17291
17292 /* Determine which of two partial specializations of MAIN_TMPL is more
17293 specialized.
17294
17295 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17296 to the first partial specialization. The TREE_VALUE is the
17297 innermost set of template parameters for the partial
17298 specialization. PAT2 is similar, but for the second template.
17299
17300 Return 1 if the first partial specialization is more specialized;
17301 -1 if the second is more specialized; 0 if neither is more
17302 specialized.
17303
17304 See [temp.class.order] for information about determining which of
17305 two templates is more specialized. */
17306
17307 static int
17308 more_specialized_class (tree main_tmpl, tree pat1, tree pat2)
17309 {
17310 tree targs;
17311 tree tmpl1, tmpl2;
17312 int winner = 0;
17313 bool any_deductions = false;
17314
17315 tmpl1 = TREE_TYPE (pat1);
17316 tmpl2 = TREE_TYPE (pat2);
17317
17318 /* Just like what happens for functions, if we are ordering between
17319 different class template specializations, we may encounter dependent
17320 types in the arguments, and we need our dependency check functions
17321 to behave correctly. */
17322 ++processing_template_decl;
17323 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat1),
17324 CLASSTYPE_TI_ARGS (tmpl1),
17325 CLASSTYPE_TI_ARGS (tmpl2));
17326 if (targs)
17327 {
17328 --winner;
17329 any_deductions = true;
17330 }
17331
17332 targs = get_class_bindings (main_tmpl, TREE_VALUE (pat2),
17333 CLASSTYPE_TI_ARGS (tmpl2),
17334 CLASSTYPE_TI_ARGS (tmpl1));
17335 if (targs)
17336 {
17337 ++winner;
17338 any_deductions = true;
17339 }
17340 --processing_template_decl;
17341
17342 /* In the case of a tie where at least one of the class templates
17343 has a parameter pack at the end, the template with the most
17344 non-packed parameters wins. */
17345 if (winner == 0
17346 && any_deductions
17347 && (template_args_variadic_p (TREE_PURPOSE (pat1))
17348 || template_args_variadic_p (TREE_PURPOSE (pat2))))
17349 {
17350 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
17351 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
17352 int len1 = TREE_VEC_LENGTH (args1);
17353 int len2 = TREE_VEC_LENGTH (args2);
17354
17355 /* We don't count the pack expansion at the end. */
17356 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
17357 --len1;
17358 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
17359 --len2;
17360
17361 if (len1 > len2)
17362 return 1;
17363 else if (len1 < len2)
17364 return -1;
17365 }
17366
17367 return winner;
17368 }
17369
17370 /* Return the template arguments that will produce the function signature
17371 DECL from the function template FN, with the explicit template
17372 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17373 also match. Return NULL_TREE if no satisfactory arguments could be
17374 found. */
17375
17376 static tree
17377 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
17378 {
17379 int ntparms = DECL_NTPARMS (fn);
17380 tree targs = make_tree_vec (ntparms);
17381 tree decl_type = TREE_TYPE (decl);
17382 tree decl_arg_types;
17383 tree *args;
17384 unsigned int nargs, ix;
17385 tree arg;
17386
17387 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
17388
17389 /* Never do unification on the 'this' parameter. */
17390 decl_arg_types = skip_artificial_parms_for (decl,
17391 TYPE_ARG_TYPES (decl_type));
17392
17393 nargs = list_length (decl_arg_types);
17394 args = XALLOCAVEC (tree, nargs);
17395 for (arg = decl_arg_types, ix = 0;
17396 arg != NULL_TREE && arg != void_list_node;
17397 arg = TREE_CHAIN (arg), ++ix)
17398 args[ix] = TREE_VALUE (arg);
17399
17400 if (fn_type_unification (fn, explicit_args, targs,
17401 args, ix,
17402 (check_rettype || DECL_CONV_FN_P (fn)
17403 ? TREE_TYPE (decl_type) : NULL_TREE),
17404 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false)
17405 == error_mark_node)
17406 return NULL_TREE;
17407
17408 return targs;
17409 }
17410
17411 /* Return the innermost template arguments that, when applied to a partial
17412 specialization of MAIN_TMPL whose innermost template parameters are
17413 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17414 ARGS.
17415
17416 For example, suppose we have:
17417
17418 template <class T, class U> struct S {};
17419 template <class T> struct S<T*, int> {};
17420
17421 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17422 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17423 int}. The resulting vector will be {double}, indicating that `T'
17424 is bound to `double'. */
17425
17426 static tree
17427 get_class_bindings (tree main_tmpl, tree tparms, tree spec_args, tree args)
17428 {
17429 int i, ntparms = TREE_VEC_LENGTH (tparms);
17430 tree deduced_args;
17431 tree innermost_deduced_args;
17432
17433 innermost_deduced_args = make_tree_vec (ntparms);
17434 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17435 {
17436 deduced_args = copy_node (args);
17437 SET_TMPL_ARGS_LEVEL (deduced_args,
17438 TMPL_ARGS_DEPTH (deduced_args),
17439 innermost_deduced_args);
17440 }
17441 else
17442 deduced_args = innermost_deduced_args;
17443
17444 if (unify (tparms, deduced_args,
17445 INNERMOST_TEMPLATE_ARGS (spec_args),
17446 INNERMOST_TEMPLATE_ARGS (args),
17447 UNIFY_ALLOW_NONE, /*explain_p=*/false))
17448 return NULL_TREE;
17449
17450 for (i = 0; i < ntparms; ++i)
17451 if (! TREE_VEC_ELT (innermost_deduced_args, i))
17452 return NULL_TREE;
17453
17454 /* Verify that nondeduced template arguments agree with the type
17455 obtained from argument deduction.
17456
17457 For example:
17458
17459 struct A { typedef int X; };
17460 template <class T, class U> struct C {};
17461 template <class T> struct C<T, typename T::X> {};
17462
17463 Then with the instantiation `C<A, int>', we can deduce that
17464 `T' is `A' but unify () does not check whether `typename T::X'
17465 is `int'. */
17466 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
17467 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl),
17468 spec_args, main_tmpl,
17469 tf_none, false, false);
17470 if (spec_args == error_mark_node
17471 /* We only need to check the innermost arguments; the other
17472 arguments will always agree. */
17473 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
17474 INNERMOST_TEMPLATE_ARGS (args)))
17475 return NULL_TREE;
17476
17477 /* Now that we have bindings for all of the template arguments,
17478 ensure that the arguments deduced for the template template
17479 parameters have compatible template parameter lists. See the use
17480 of template_template_parm_bindings_ok_p in fn_type_unification
17481 for more information. */
17482 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
17483 return NULL_TREE;
17484
17485 return deduced_args;
17486 }
17487
17488 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17489 Return the TREE_LIST node with the most specialized template, if
17490 any. If there is no most specialized template, the error_mark_node
17491 is returned.
17492
17493 Note that this function does not look at, or modify, the
17494 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17495 returned is one of the elements of INSTANTIATIONS, callers may
17496 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17497 and retrieve it from the value returned. */
17498
17499 tree
17500 most_specialized_instantiation (tree templates)
17501 {
17502 tree fn, champ;
17503
17504 ++processing_template_decl;
17505
17506 champ = templates;
17507 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17508 {
17509 int fate = 0;
17510
17511 if (get_bindings (TREE_VALUE (champ),
17512 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17513 NULL_TREE, /*check_ret=*/true))
17514 fate--;
17515
17516 if (get_bindings (TREE_VALUE (fn),
17517 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17518 NULL_TREE, /*check_ret=*/true))
17519 fate++;
17520
17521 if (fate == -1)
17522 champ = fn;
17523 else if (!fate)
17524 {
17525 /* Equally specialized, move to next function. If there
17526 is no next function, nothing's most specialized. */
17527 fn = TREE_CHAIN (fn);
17528 champ = fn;
17529 if (!fn)
17530 break;
17531 }
17532 }
17533
17534 if (champ)
17535 /* Now verify that champ is better than everything earlier in the
17536 instantiation list. */
17537 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17538 if (get_bindings (TREE_VALUE (champ),
17539 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17540 NULL_TREE, /*check_ret=*/true)
17541 || !get_bindings (TREE_VALUE (fn),
17542 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17543 NULL_TREE, /*check_ret=*/true))
17544 {
17545 champ = NULL_TREE;
17546 break;
17547 }
17548
17549 processing_template_decl--;
17550
17551 if (!champ)
17552 return error_mark_node;
17553
17554 return champ;
17555 }
17556
17557 /* If DECL is a specialization of some template, return the most
17558 general such template. Otherwise, returns NULL_TREE.
17559
17560 For example, given:
17561
17562 template <class T> struct S { template <class U> void f(U); };
17563
17564 if TMPL is `template <class U> void S<int>::f(U)' this will return
17565 the full template. This function will not trace past partial
17566 specializations, however. For example, given in addition:
17567
17568 template <class T> struct S<T*> { template <class U> void f(U); };
17569
17570 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17571 `template <class T> template <class U> S<T*>::f(U)'. */
17572
17573 tree
17574 most_general_template (tree decl)
17575 {
17576 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17577 an immediate specialization. */
17578 if (TREE_CODE (decl) == FUNCTION_DECL)
17579 {
17580 if (DECL_TEMPLATE_INFO (decl)) {
17581 decl = DECL_TI_TEMPLATE (decl);
17582
17583 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17584 template friend. */
17585 if (TREE_CODE (decl) != TEMPLATE_DECL)
17586 return NULL_TREE;
17587 } else
17588 return NULL_TREE;
17589 }
17590
17591 /* Look for more and more general templates. */
17592 while (DECL_TEMPLATE_INFO (decl))
17593 {
17594 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17595 (See cp-tree.h for details.) */
17596 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17597 break;
17598
17599 if (CLASS_TYPE_P (TREE_TYPE (decl))
17600 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17601 break;
17602
17603 /* Stop if we run into an explicitly specialized class template. */
17604 if (!DECL_NAMESPACE_SCOPE_P (decl)
17605 && DECL_CONTEXT (decl)
17606 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17607 break;
17608
17609 decl = DECL_TI_TEMPLATE (decl);
17610 }
17611
17612 return decl;
17613 }
17614
17615 /* Return the most specialized of the class template partial
17616 specializations of TMPL which can produce TYPE, a specialization of
17617 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17618 a _TYPE node corresponding to the partial specialization, while the
17619 TREE_PURPOSE is the set of template arguments that must be
17620 substituted into the TREE_TYPE in order to generate TYPE.
17621
17622 If the choice of partial specialization is ambiguous, a diagnostic
17623 is issued, and the error_mark_node is returned. If there are no
17624 partial specializations of TMPL matching TYPE, then NULL_TREE is
17625 returned. */
17626
17627 static tree
17628 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17629 {
17630 tree list = NULL_TREE;
17631 tree t;
17632 tree champ;
17633 int fate;
17634 bool ambiguous_p;
17635 tree args;
17636 tree outer_args = NULL_TREE;
17637
17638 tmpl = most_general_template (tmpl);
17639 args = CLASSTYPE_TI_ARGS (type);
17640
17641 /* For determining which partial specialization to use, only the
17642 innermost args are interesting. */
17643 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17644 {
17645 outer_args = strip_innermost_template_args (args, 1);
17646 args = INNERMOST_TEMPLATE_ARGS (args);
17647 }
17648
17649 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17650 {
17651 tree partial_spec_args;
17652 tree spec_args;
17653 tree parms = TREE_VALUE (t);
17654
17655 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17656
17657 ++processing_template_decl;
17658
17659 if (outer_args)
17660 {
17661 int i;
17662
17663 /* Discard the outer levels of args, and then substitute in the
17664 template args from the enclosing class. */
17665 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17666 partial_spec_args = tsubst_template_args
17667 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17668
17669 /* PARMS already refers to just the innermost parms, but the
17670 template parms in partial_spec_args had their levels lowered
17671 by tsubst, so we need to do the same for the parm list. We
17672 can't just tsubst the TREE_VEC itself, as tsubst wants to
17673 treat a TREE_VEC as an argument vector. */
17674 parms = copy_node (parms);
17675 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17676 TREE_VEC_ELT (parms, i) =
17677 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17678
17679 }
17680
17681 partial_spec_args =
17682 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17683 add_to_template_args (outer_args,
17684 partial_spec_args),
17685 tmpl, tf_none,
17686 /*require_all_args=*/true,
17687 /*use_default_args=*/true);
17688
17689 --processing_template_decl;
17690
17691 if (partial_spec_args == error_mark_node)
17692 return error_mark_node;
17693
17694 spec_args = get_class_bindings (tmpl, parms,
17695 partial_spec_args,
17696 args);
17697 if (spec_args)
17698 {
17699 if (outer_args)
17700 spec_args = add_to_template_args (outer_args, spec_args);
17701 list = tree_cons (spec_args, TREE_VALUE (t), list);
17702 TREE_TYPE (list) = TREE_TYPE (t);
17703 }
17704 }
17705
17706 if (! list)
17707 return NULL_TREE;
17708
17709 ambiguous_p = false;
17710 t = list;
17711 champ = t;
17712 t = TREE_CHAIN (t);
17713 for (; t; t = TREE_CHAIN (t))
17714 {
17715 fate = more_specialized_class (tmpl, champ, t);
17716 if (fate == 1)
17717 ;
17718 else
17719 {
17720 if (fate == 0)
17721 {
17722 t = TREE_CHAIN (t);
17723 if (! t)
17724 {
17725 ambiguous_p = true;
17726 break;
17727 }
17728 }
17729 champ = t;
17730 }
17731 }
17732
17733 if (!ambiguous_p)
17734 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17735 {
17736 fate = more_specialized_class (tmpl, champ, t);
17737 if (fate != 1)
17738 {
17739 ambiguous_p = true;
17740 break;
17741 }
17742 }
17743
17744 if (ambiguous_p)
17745 {
17746 const char *str;
17747 char *spaces = NULL;
17748 if (!(complain & tf_error))
17749 return error_mark_node;
17750 error ("ambiguous class template instantiation for %q#T", type);
17751 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17752 for (t = list; t; t = TREE_CHAIN (t))
17753 {
17754 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17755 spaces = spaces ? spaces : get_spaces (str);
17756 }
17757 free (spaces);
17758 return error_mark_node;
17759 }
17760
17761 return champ;
17762 }
17763
17764 /* Explicitly instantiate DECL. */
17765
17766 void
17767 do_decl_instantiation (tree decl, tree storage)
17768 {
17769 tree result = NULL_TREE;
17770 int extern_p = 0;
17771
17772 if (!decl || decl == error_mark_node)
17773 /* An error occurred, for which grokdeclarator has already issued
17774 an appropriate message. */
17775 return;
17776 else if (! DECL_LANG_SPECIFIC (decl))
17777 {
17778 error ("explicit instantiation of non-template %q#D", decl);
17779 return;
17780 }
17781 else if (TREE_CODE (decl) == VAR_DECL)
17782 {
17783 /* There is an asymmetry here in the way VAR_DECLs and
17784 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17785 the latter, the DECL we get back will be marked as a
17786 template instantiation, and the appropriate
17787 DECL_TEMPLATE_INFO will be set up. This does not happen for
17788 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17789 should handle VAR_DECLs as it currently handles
17790 FUNCTION_DECLs. */
17791 if (!DECL_CLASS_SCOPE_P (decl))
17792 {
17793 error ("%qD is not a static data member of a class template", decl);
17794 return;
17795 }
17796 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
17797 if (!result || TREE_CODE (result) != VAR_DECL)
17798 {
17799 error ("no matching template for %qD found", decl);
17800 return;
17801 }
17802 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
17803 {
17804 error ("type %qT for explicit instantiation %qD does not match "
17805 "declared type %qT", TREE_TYPE (result), decl,
17806 TREE_TYPE (decl));
17807 return;
17808 }
17809 }
17810 else if (TREE_CODE (decl) != FUNCTION_DECL)
17811 {
17812 error ("explicit instantiation of %q#D", decl);
17813 return;
17814 }
17815 else
17816 result = decl;
17817
17818 /* Check for various error cases. Note that if the explicit
17819 instantiation is valid the RESULT will currently be marked as an
17820 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17821 until we get here. */
17822
17823 if (DECL_TEMPLATE_SPECIALIZATION (result))
17824 {
17825 /* DR 259 [temp.spec].
17826
17827 Both an explicit instantiation and a declaration of an explicit
17828 specialization shall not appear in a program unless the explicit
17829 instantiation follows a declaration of the explicit specialization.
17830
17831 For a given set of template parameters, if an explicit
17832 instantiation of a template appears after a declaration of an
17833 explicit specialization for that template, the explicit
17834 instantiation has no effect. */
17835 return;
17836 }
17837 else if (DECL_EXPLICIT_INSTANTIATION (result))
17838 {
17839 /* [temp.spec]
17840
17841 No program shall explicitly instantiate any template more
17842 than once.
17843
17844 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17845 the first instantiation was `extern' and the second is not,
17846 and EXTERN_P for the opposite case. */
17847 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
17848 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
17849 /* If an "extern" explicit instantiation follows an ordinary
17850 explicit instantiation, the template is instantiated. */
17851 if (extern_p)
17852 return;
17853 }
17854 else if (!DECL_IMPLICIT_INSTANTIATION (result))
17855 {
17856 error ("no matching template for %qD found", result);
17857 return;
17858 }
17859 else if (!DECL_TEMPLATE_INFO (result))
17860 {
17861 permerror (input_location, "explicit instantiation of non-template %q#D", result);
17862 return;
17863 }
17864
17865 if (storage == NULL_TREE)
17866 ;
17867 else if (storage == ridpointers[(int) RID_EXTERN])
17868 {
17869 if (!in_system_header && (cxx_dialect == cxx98))
17870 pedwarn (input_location, OPT_Wpedantic,
17871 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17872 "instantiations");
17873 extern_p = 1;
17874 }
17875 else
17876 error ("storage class %qD applied to template instantiation", storage);
17877
17878 check_explicit_instantiation_namespace (result);
17879 mark_decl_instantiated (result, extern_p);
17880 if (! extern_p)
17881 instantiate_decl (result, /*defer_ok=*/1,
17882 /*expl_inst_class_mem_p=*/false);
17883 }
17884
17885 static void
17886 mark_class_instantiated (tree t, int extern_p)
17887 {
17888 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
17889 SET_CLASSTYPE_INTERFACE_KNOWN (t);
17890 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
17891 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
17892 if (! extern_p)
17893 {
17894 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
17895 rest_of_type_compilation (t, 1);
17896 }
17897 }
17898
17899 /* Called from do_type_instantiation through binding_table_foreach to
17900 do recursive instantiation for the type bound in ENTRY. */
17901 static void
17902 bt_instantiate_type_proc (binding_entry entry, void *data)
17903 {
17904 tree storage = *(tree *) data;
17905
17906 if (MAYBE_CLASS_TYPE_P (entry->type)
17907 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
17908 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
17909 }
17910
17911 /* Called from do_type_instantiation to instantiate a member
17912 (a member function or a static member variable) of an
17913 explicitly instantiated class template. */
17914 static void
17915 instantiate_class_member (tree decl, int extern_p)
17916 {
17917 mark_decl_instantiated (decl, extern_p);
17918 if (! extern_p)
17919 instantiate_decl (decl, /*defer_ok=*/1,
17920 /*expl_inst_class_mem_p=*/true);
17921 }
17922
17923 /* Perform an explicit instantiation of template class T. STORAGE, if
17924 non-null, is the RID for extern, inline or static. COMPLAIN is
17925 nonzero if this is called from the parser, zero if called recursively,
17926 since the standard is unclear (as detailed below). */
17927
17928 void
17929 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
17930 {
17931 int extern_p = 0;
17932 int nomem_p = 0;
17933 int static_p = 0;
17934 int previous_instantiation_extern_p = 0;
17935
17936 if (TREE_CODE (t) == TYPE_DECL)
17937 t = TREE_TYPE (t);
17938
17939 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
17940 {
17941 tree tmpl =
17942 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
17943 if (tmpl)
17944 error ("explicit instantiation of non-class template %qD", tmpl);
17945 else
17946 error ("explicit instantiation of non-template type %qT", t);
17947 return;
17948 }
17949
17950 complete_type (t);
17951
17952 if (!COMPLETE_TYPE_P (t))
17953 {
17954 if (complain & tf_error)
17955 error ("explicit instantiation of %q#T before definition of template",
17956 t);
17957 return;
17958 }
17959
17960 if (storage != NULL_TREE)
17961 {
17962 if (!in_system_header)
17963 {
17964 if (storage == ridpointers[(int) RID_EXTERN])
17965 {
17966 if (cxx_dialect == cxx98)
17967 pedwarn (input_location, OPT_Wpedantic,
17968 "ISO C++ 1998 forbids the use of %<extern%> on "
17969 "explicit instantiations");
17970 }
17971 else
17972 pedwarn (input_location, OPT_Wpedantic,
17973 "ISO C++ forbids the use of %qE"
17974 " on explicit instantiations", storage);
17975 }
17976
17977 if (storage == ridpointers[(int) RID_INLINE])
17978 nomem_p = 1;
17979 else if (storage == ridpointers[(int) RID_EXTERN])
17980 extern_p = 1;
17981 else if (storage == ridpointers[(int) RID_STATIC])
17982 static_p = 1;
17983 else
17984 {
17985 error ("storage class %qD applied to template instantiation",
17986 storage);
17987 extern_p = 0;
17988 }
17989 }
17990
17991 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
17992 {
17993 /* DR 259 [temp.spec].
17994
17995 Both an explicit instantiation and a declaration of an explicit
17996 specialization shall not appear in a program unless the explicit
17997 instantiation follows a declaration of the explicit specialization.
17998
17999 For a given set of template parameters, if an explicit
18000 instantiation of a template appears after a declaration of an
18001 explicit specialization for that template, the explicit
18002 instantiation has no effect. */
18003 return;
18004 }
18005 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
18006 {
18007 /* [temp.spec]
18008
18009 No program shall explicitly instantiate any template more
18010 than once.
18011
18012 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18013 instantiation was `extern'. If EXTERN_P then the second is.
18014 These cases are OK. */
18015 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
18016
18017 if (!previous_instantiation_extern_p && !extern_p
18018 && (complain & tf_error))
18019 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
18020
18021 /* If we've already instantiated the template, just return now. */
18022 if (!CLASSTYPE_INTERFACE_ONLY (t))
18023 return;
18024 }
18025
18026 check_explicit_instantiation_namespace (TYPE_NAME (t));
18027 mark_class_instantiated (t, extern_p);
18028
18029 if (nomem_p)
18030 return;
18031
18032 {
18033 tree tmp;
18034
18035 /* In contrast to implicit instantiation, where only the
18036 declarations, and not the definitions, of members are
18037 instantiated, we have here:
18038
18039 [temp.explicit]
18040
18041 The explicit instantiation of a class template specialization
18042 implies the instantiation of all of its members not
18043 previously explicitly specialized in the translation unit
18044 containing the explicit instantiation.
18045
18046 Of course, we can't instantiate member template classes, since
18047 we don't have any arguments for them. Note that the standard
18048 is unclear on whether the instantiation of the members are
18049 *explicit* instantiations or not. However, the most natural
18050 interpretation is that it should be an explicit instantiation. */
18051
18052 if (! static_p)
18053 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
18054 if (TREE_CODE (tmp) == FUNCTION_DECL
18055 && DECL_TEMPLATE_INSTANTIATION (tmp))
18056 instantiate_class_member (tmp, extern_p);
18057
18058 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
18059 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
18060 instantiate_class_member (tmp, extern_p);
18061
18062 if (CLASSTYPE_NESTED_UTDS (t))
18063 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
18064 bt_instantiate_type_proc, &storage);
18065 }
18066 }
18067
18068 /* Given a function DECL, which is a specialization of TMPL, modify
18069 DECL to be a re-instantiation of TMPL with the same template
18070 arguments. TMPL should be the template into which tsubst'ing
18071 should occur for DECL, not the most general template.
18072
18073 One reason for doing this is a scenario like this:
18074
18075 template <class T>
18076 void f(const T&, int i);
18077
18078 void g() { f(3, 7); }
18079
18080 template <class T>
18081 void f(const T& t, const int i) { }
18082
18083 Note that when the template is first instantiated, with
18084 instantiate_template, the resulting DECL will have no name for the
18085 first parameter, and the wrong type for the second. So, when we go
18086 to instantiate the DECL, we regenerate it. */
18087
18088 static void
18089 regenerate_decl_from_template (tree decl, tree tmpl)
18090 {
18091 /* The arguments used to instantiate DECL, from the most general
18092 template. */
18093 tree args;
18094 tree code_pattern;
18095
18096 args = DECL_TI_ARGS (decl);
18097 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
18098
18099 /* Make sure that we can see identifiers, and compute access
18100 correctly. */
18101 push_access_scope (decl);
18102
18103 if (TREE_CODE (decl) == FUNCTION_DECL)
18104 {
18105 tree decl_parm;
18106 tree pattern_parm;
18107 tree specs;
18108 int args_depth;
18109 int parms_depth;
18110
18111 args_depth = TMPL_ARGS_DEPTH (args);
18112 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
18113 if (args_depth > parms_depth)
18114 args = get_innermost_template_args (args, parms_depth);
18115
18116 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
18117 args, tf_error, NULL_TREE,
18118 /*defer_ok*/false);
18119 if (specs && specs != error_mark_node)
18120 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
18121 specs);
18122
18123 /* Merge parameter declarations. */
18124 decl_parm = skip_artificial_parms_for (decl,
18125 DECL_ARGUMENTS (decl));
18126 pattern_parm
18127 = skip_artificial_parms_for (code_pattern,
18128 DECL_ARGUMENTS (code_pattern));
18129 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
18130 {
18131 tree parm_type;
18132 tree attributes;
18133
18134 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18135 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
18136 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
18137 NULL_TREE);
18138 parm_type = type_decays_to (parm_type);
18139 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18140 TREE_TYPE (decl_parm) = parm_type;
18141 attributes = DECL_ATTRIBUTES (pattern_parm);
18142 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18143 {
18144 DECL_ATTRIBUTES (decl_parm) = attributes;
18145 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18146 }
18147 decl_parm = DECL_CHAIN (decl_parm);
18148 pattern_parm = DECL_CHAIN (pattern_parm);
18149 }
18150 /* Merge any parameters that match with the function parameter
18151 pack. */
18152 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
18153 {
18154 int i, len;
18155 tree expanded_types;
18156 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18157 the parameters in this function parameter pack. */
18158 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
18159 args, tf_error, NULL_TREE);
18160 len = TREE_VEC_LENGTH (expanded_types);
18161 for (i = 0; i < len; i++)
18162 {
18163 tree parm_type;
18164 tree attributes;
18165
18166 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
18167 /* Rename the parameter to include the index. */
18168 DECL_NAME (decl_parm) =
18169 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
18170 parm_type = TREE_VEC_ELT (expanded_types, i);
18171 parm_type = type_decays_to (parm_type);
18172 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
18173 TREE_TYPE (decl_parm) = parm_type;
18174 attributes = DECL_ATTRIBUTES (pattern_parm);
18175 if (DECL_ATTRIBUTES (decl_parm) != attributes)
18176 {
18177 DECL_ATTRIBUTES (decl_parm) = attributes;
18178 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
18179 }
18180 decl_parm = DECL_CHAIN (decl_parm);
18181 }
18182 }
18183 /* Merge additional specifiers from the CODE_PATTERN. */
18184 if (DECL_DECLARED_INLINE_P (code_pattern)
18185 && !DECL_DECLARED_INLINE_P (decl))
18186 DECL_DECLARED_INLINE_P (decl) = 1;
18187 }
18188 else if (TREE_CODE (decl) == VAR_DECL)
18189 {
18190 DECL_INITIAL (decl) =
18191 tsubst_expr (DECL_INITIAL (code_pattern), args,
18192 tf_error, DECL_TI_TEMPLATE (decl),
18193 /*integral_constant_expression_p=*/false);
18194 if (VAR_HAD_UNKNOWN_BOUND (decl))
18195 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
18196 tf_error, DECL_TI_TEMPLATE (decl));
18197 }
18198 else
18199 gcc_unreachable ();
18200
18201 pop_access_scope (decl);
18202 }
18203
18204 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18205 substituted to get DECL. */
18206
18207 tree
18208 template_for_substitution (tree decl)
18209 {
18210 tree tmpl = DECL_TI_TEMPLATE (decl);
18211
18212 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18213 for the instantiation. This is not always the most general
18214 template. Consider, for example:
18215
18216 template <class T>
18217 struct S { template <class U> void f();
18218 template <> void f<int>(); };
18219
18220 and an instantiation of S<double>::f<int>. We want TD to be the
18221 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18222 while (/* An instantiation cannot have a definition, so we need a
18223 more general template. */
18224 DECL_TEMPLATE_INSTANTIATION (tmpl)
18225 /* We must also deal with friend templates. Given:
18226
18227 template <class T> struct S {
18228 template <class U> friend void f() {};
18229 };
18230
18231 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18232 so far as the language is concerned, but that's still
18233 where we get the pattern for the instantiation from. On
18234 other hand, if the definition comes outside the class, say:
18235
18236 template <class T> struct S {
18237 template <class U> friend void f();
18238 };
18239 template <class U> friend void f() {}
18240
18241 we don't need to look any further. That's what the check for
18242 DECL_INITIAL is for. */
18243 || (TREE_CODE (decl) == FUNCTION_DECL
18244 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
18245 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
18246 {
18247 /* The present template, TD, should not be a definition. If it
18248 were a definition, we should be using it! Note that we
18249 cannot restructure the loop to just keep going until we find
18250 a template with a definition, since that might go too far if
18251 a specialization was declared, but not defined. */
18252 gcc_assert (TREE_CODE (decl) != VAR_DECL
18253 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
18254
18255 /* Fetch the more general template. */
18256 tmpl = DECL_TI_TEMPLATE (tmpl);
18257 }
18258
18259 return tmpl;
18260 }
18261
18262 /* Returns true if we need to instantiate this template instance even if we
18263 know we aren't going to emit it.. */
18264
18265 bool
18266 always_instantiate_p (tree decl)
18267 {
18268 /* We always instantiate inline functions so that we can inline them. An
18269 explicit instantiation declaration prohibits implicit instantiation of
18270 non-inline functions. With high levels of optimization, we would
18271 normally inline non-inline functions -- but we're not allowed to do
18272 that for "extern template" functions. Therefore, we check
18273 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18274 return ((TREE_CODE (decl) == FUNCTION_DECL
18275 && (DECL_DECLARED_INLINE_P (decl)
18276 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
18277 /* And we need to instantiate static data members so that
18278 their initializers are available in integral constant
18279 expressions. */
18280 || (TREE_CODE (decl) == VAR_DECL
18281 && decl_maybe_constant_var_p (decl)));
18282 }
18283
18284 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18285 instantiate it now, modifying TREE_TYPE (fn). */
18286
18287 void
18288 maybe_instantiate_noexcept (tree fn)
18289 {
18290 tree fntype, spec, noex, clone;
18291
18292 if (DECL_CLONED_FUNCTION_P (fn))
18293 fn = DECL_CLONED_FUNCTION (fn);
18294 fntype = TREE_TYPE (fn);
18295 spec = TYPE_RAISES_EXCEPTIONS (fntype);
18296
18297 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
18298 return;
18299
18300 noex = TREE_PURPOSE (spec);
18301
18302 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
18303 {
18304 if (push_tinst_level (fn))
18305 {
18306 push_access_scope (fn);
18307 input_location = DECL_SOURCE_LOCATION (fn);
18308 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
18309 DEFERRED_NOEXCEPT_ARGS (noex),
18310 tf_warning_or_error, fn,
18311 /*function_p=*/false,
18312 /*integral_constant_expression_p=*/true);
18313 pop_access_scope (fn);
18314 pop_tinst_level ();
18315 spec = build_noexcept_spec (noex, tf_warning_or_error);
18316 if (spec == error_mark_node)
18317 spec = noexcept_false_spec;
18318 }
18319 else
18320 spec = noexcept_false_spec;
18321 }
18322 else
18323 {
18324 /* This is an implicitly declared function, so NOEX is a list of
18325 other functions to evaluate and merge. */
18326 tree elt;
18327 spec = noexcept_true_spec;
18328 for (elt = noex; elt; elt = OVL_NEXT (elt))
18329 {
18330 tree fn = OVL_CURRENT (elt);
18331 tree subspec;
18332 maybe_instantiate_noexcept (fn);
18333 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
18334 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
18335 }
18336 }
18337
18338 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
18339
18340 FOR_EACH_CLONE (clone, fn)
18341 {
18342 if (TREE_TYPE (clone) == fntype)
18343 TREE_TYPE (clone) = TREE_TYPE (fn);
18344 else
18345 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
18346 }
18347 }
18348
18349 /* Produce the definition of D, a _DECL generated from a template. If
18350 DEFER_OK is nonzero, then we don't have to actually do the
18351 instantiation now; we just have to do it sometime. Normally it is
18352 an error if this is an explicit instantiation but D is undefined.
18353 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18354 explicitly instantiated class template. */
18355
18356 tree
18357 instantiate_decl (tree d, int defer_ok,
18358 bool expl_inst_class_mem_p)
18359 {
18360 tree tmpl = DECL_TI_TEMPLATE (d);
18361 tree gen_args;
18362 tree args;
18363 tree td;
18364 tree code_pattern;
18365 tree spec;
18366 tree gen_tmpl;
18367 bool pattern_defined;
18368 location_t saved_loc = input_location;
18369 bool external_p;
18370 tree fn_context;
18371 bool nested;
18372
18373 /* This function should only be used to instantiate templates for
18374 functions and static member variables. */
18375 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
18376 || TREE_CODE (d) == VAR_DECL);
18377
18378 /* Variables are never deferred; if instantiation is required, they
18379 are instantiated right away. That allows for better code in the
18380 case that an expression refers to the value of the variable --
18381 if the variable has a constant value the referring expression can
18382 take advantage of that fact. */
18383 if (TREE_CODE (d) == VAR_DECL
18384 || DECL_DECLARED_CONSTEXPR_P (d))
18385 defer_ok = 0;
18386
18387 /* Don't instantiate cloned functions. Instead, instantiate the
18388 functions they cloned. */
18389 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
18390 d = DECL_CLONED_FUNCTION (d);
18391
18392 if (DECL_TEMPLATE_INSTANTIATED (d)
18393 || (TREE_CODE (d) == FUNCTION_DECL
18394 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
18395 || DECL_TEMPLATE_SPECIALIZATION (d))
18396 /* D has already been instantiated or explicitly specialized, so
18397 there's nothing for us to do here.
18398
18399 It might seem reasonable to check whether or not D is an explicit
18400 instantiation, and, if so, stop here. But when an explicit
18401 instantiation is deferred until the end of the compilation,
18402 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18403 the instantiation. */
18404 return d;
18405
18406 /* Check to see whether we know that this template will be
18407 instantiated in some other file, as with "extern template"
18408 extension. */
18409 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
18410
18411 /* In general, we do not instantiate such templates. */
18412 if (external_p && !always_instantiate_p (d))
18413 return d;
18414
18415 gen_tmpl = most_general_template (tmpl);
18416 gen_args = DECL_TI_ARGS (d);
18417
18418 if (tmpl != gen_tmpl)
18419 /* We should already have the extra args. */
18420 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
18421 == TMPL_ARGS_DEPTH (gen_args));
18422 /* And what's in the hash table should match D. */
18423 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
18424 || spec == NULL_TREE);
18425
18426 /* This needs to happen before any tsubsting. */
18427 if (! push_tinst_level (d))
18428 return d;
18429
18430 timevar_push (TV_TEMPLATE_INST);
18431
18432 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18433 for the instantiation. */
18434 td = template_for_substitution (d);
18435 code_pattern = DECL_TEMPLATE_RESULT (td);
18436
18437 /* We should never be trying to instantiate a member of a class
18438 template or partial specialization. */
18439 gcc_assert (d != code_pattern);
18440
18441 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
18442 || DECL_TEMPLATE_SPECIALIZATION (td))
18443 /* In the case of a friend template whose definition is provided
18444 outside the class, we may have too many arguments. Drop the
18445 ones we don't need. The same is true for specializations. */
18446 args = get_innermost_template_args
18447 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
18448 else
18449 args = gen_args;
18450
18451 if (TREE_CODE (d) == FUNCTION_DECL)
18452 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
18453 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
18454 else
18455 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
18456
18457 /* We may be in the middle of deferred access check. Disable it now. */
18458 push_deferring_access_checks (dk_no_deferred);
18459
18460 /* Unless an explicit instantiation directive has already determined
18461 the linkage of D, remember that a definition is available for
18462 this entity. */
18463 if (pattern_defined
18464 && !DECL_INTERFACE_KNOWN (d)
18465 && !DECL_NOT_REALLY_EXTERN (d))
18466 mark_definable (d);
18467
18468 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
18469 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
18470 input_location = DECL_SOURCE_LOCATION (d);
18471
18472 /* If D is a member of an explicitly instantiated class template,
18473 and no definition is available, treat it like an implicit
18474 instantiation. */
18475 if (!pattern_defined && expl_inst_class_mem_p
18476 && DECL_EXPLICIT_INSTANTIATION (d))
18477 {
18478 /* Leave linkage flags alone on instantiations with anonymous
18479 visibility. */
18480 if (TREE_PUBLIC (d))
18481 {
18482 DECL_NOT_REALLY_EXTERN (d) = 0;
18483 DECL_INTERFACE_KNOWN (d) = 0;
18484 }
18485 SET_DECL_IMPLICIT_INSTANTIATION (d);
18486 }
18487
18488 if (TREE_CODE (d) == FUNCTION_DECL)
18489 maybe_instantiate_noexcept (d);
18490
18491 /* Defer all other templates, unless we have been explicitly
18492 forbidden from doing so. */
18493 if (/* If there is no definition, we cannot instantiate the
18494 template. */
18495 ! pattern_defined
18496 /* If it's OK to postpone instantiation, do so. */
18497 || defer_ok
18498 /* If this is a static data member that will be defined
18499 elsewhere, we don't want to instantiate the entire data
18500 member, but we do want to instantiate the initializer so that
18501 we can substitute that elsewhere. */
18502 || (external_p && TREE_CODE (d) == VAR_DECL))
18503 {
18504 /* The definition of the static data member is now required so
18505 we must substitute the initializer. */
18506 if (TREE_CODE (d) == VAR_DECL
18507 && !DECL_INITIAL (d)
18508 && DECL_INITIAL (code_pattern))
18509 {
18510 tree ns;
18511 tree init;
18512 bool const_init = false;
18513
18514 ns = decl_namespace_context (d);
18515 push_nested_namespace (ns);
18516 push_nested_class (DECL_CONTEXT (d));
18517 init = tsubst_expr (DECL_INITIAL (code_pattern),
18518 args,
18519 tf_warning_or_error, NULL_TREE,
18520 /*integral_constant_expression_p=*/false);
18521 /* Make sure the initializer is still constant, in case of
18522 circular dependency (template/instantiate6.C). */
18523 const_init
18524 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18525 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18526 /*asmspec_tree=*/NULL_TREE,
18527 LOOKUP_ONLYCONVERTING);
18528 pop_nested_class ();
18529 pop_nested_namespace (ns);
18530 }
18531
18532 /* We restore the source position here because it's used by
18533 add_pending_template. */
18534 input_location = saved_loc;
18535
18536 if (at_eof && !pattern_defined
18537 && DECL_EXPLICIT_INSTANTIATION (d)
18538 && DECL_NOT_REALLY_EXTERN (d))
18539 /* [temp.explicit]
18540
18541 The definition of a non-exported function template, a
18542 non-exported member function template, or a non-exported
18543 member function or static data member of a class template
18544 shall be present in every translation unit in which it is
18545 explicitly instantiated. */
18546 permerror (input_location, "explicit instantiation of %qD "
18547 "but no definition available", d);
18548
18549 /* If we're in unevaluated context, we just wanted to get the
18550 constant value; this isn't an odr use, so don't queue
18551 a full instantiation. */
18552 if (cp_unevaluated_operand != 0)
18553 goto out;
18554 /* ??? Historically, we have instantiated inline functions, even
18555 when marked as "extern template". */
18556 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18557 add_pending_template (d);
18558 goto out;
18559 }
18560 /* Tell the repository that D is available in this translation unit
18561 -- and see if it is supposed to be instantiated here. */
18562 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18563 {
18564 /* In a PCH file, despite the fact that the repository hasn't
18565 requested instantiation in the PCH it is still possible that
18566 an instantiation will be required in a file that includes the
18567 PCH. */
18568 if (pch_file)
18569 add_pending_template (d);
18570 /* Instantiate inline functions so that the inliner can do its
18571 job, even though we'll not be emitting a copy of this
18572 function. */
18573 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18574 goto out;
18575 }
18576
18577 fn_context = decl_function_context (d);
18578 nested = (current_function_decl != NULL_TREE);
18579 if (!fn_context)
18580 push_to_top_level ();
18581 else if (nested)
18582 push_function_context ();
18583
18584 /* Mark D as instantiated so that recursive calls to
18585 instantiate_decl do not try to instantiate it again. */
18586 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18587
18588 /* Regenerate the declaration in case the template has been modified
18589 by a subsequent redeclaration. */
18590 regenerate_decl_from_template (d, td);
18591
18592 /* We already set the file and line above. Reset them now in case
18593 they changed as a result of calling regenerate_decl_from_template. */
18594 input_location = DECL_SOURCE_LOCATION (d);
18595
18596 if (TREE_CODE (d) == VAR_DECL)
18597 {
18598 tree init;
18599 bool const_init = false;
18600
18601 /* Clear out DECL_RTL; whatever was there before may not be right
18602 since we've reset the type of the declaration. */
18603 SET_DECL_RTL (d, NULL);
18604 DECL_IN_AGGR_P (d) = 0;
18605
18606 /* The initializer is placed in DECL_INITIAL by
18607 regenerate_decl_from_template so we don't need to
18608 push/pop_access_scope again here. Pull it out so that
18609 cp_finish_decl can process it. */
18610 init = DECL_INITIAL (d);
18611 DECL_INITIAL (d) = NULL_TREE;
18612 DECL_INITIALIZED_P (d) = 0;
18613
18614 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18615 initializer. That function will defer actual emission until
18616 we have a chance to determine linkage. */
18617 DECL_EXTERNAL (d) = 0;
18618
18619 /* Enter the scope of D so that access-checking works correctly. */
18620 push_nested_class (DECL_CONTEXT (d));
18621 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18622 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18623 pop_nested_class ();
18624 }
18625 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18626 synthesize_method (d);
18627 else if (TREE_CODE (d) == FUNCTION_DECL)
18628 {
18629 struct pointer_map_t *saved_local_specializations;
18630 tree subst_decl;
18631 tree tmpl_parm;
18632 tree spec_parm;
18633
18634 /* Save away the current list, in case we are instantiating one
18635 template from within the body of another. */
18636 saved_local_specializations = local_specializations;
18637
18638 /* Set up the list of local specializations. */
18639 local_specializations = pointer_map_create ();
18640
18641 /* Set up context. */
18642 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18643
18644 /* Some typedefs referenced from within the template code need to be
18645 access checked at template instantiation time, i.e now. These
18646 types were added to the template at parsing time. Let's get those
18647 and perform the access checks then. */
18648 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl),
18649 gen_args);
18650
18651 /* Create substitution entries for the parameters. */
18652 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18653 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18654 spec_parm = DECL_ARGUMENTS (d);
18655 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18656 {
18657 register_local_specialization (spec_parm, tmpl_parm);
18658 spec_parm = skip_artificial_parms_for (d, spec_parm);
18659 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18660 }
18661 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18662 {
18663 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18664 {
18665 register_local_specialization (spec_parm, tmpl_parm);
18666 spec_parm = DECL_CHAIN (spec_parm);
18667 }
18668 else
18669 {
18670 /* Register the (value) argument pack as a specialization of
18671 TMPL_PARM, then move on. */
18672 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18673 register_local_specialization (argpack, tmpl_parm);
18674 }
18675 }
18676 gcc_assert (!spec_parm);
18677
18678 /* Substitute into the body of the function. */
18679 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18680 tf_warning_or_error, tmpl,
18681 /*integral_constant_expression_p=*/false);
18682
18683 /* Set the current input_location to the end of the function
18684 so that finish_function knows where we are. */
18685 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18686
18687 /* We don't need the local specializations any more. */
18688 pointer_map_destroy (local_specializations);
18689 local_specializations = saved_local_specializations;
18690
18691 /* Finish the function. */
18692 d = finish_function (0);
18693 expand_or_defer_fn (d);
18694 }
18695
18696 /* We're not deferring instantiation any more. */
18697 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18698
18699 if (!fn_context)
18700 pop_from_top_level ();
18701 else if (nested)
18702 pop_function_context ();
18703
18704 out:
18705 input_location = saved_loc;
18706 pop_deferring_access_checks ();
18707 pop_tinst_level ();
18708
18709 timevar_pop (TV_TEMPLATE_INST);
18710
18711 return d;
18712 }
18713
18714 /* Run through the list of templates that we wish we could
18715 instantiate, and instantiate any we can. RETRIES is the
18716 number of times we retry pending template instantiation. */
18717
18718 void
18719 instantiate_pending_templates (int retries)
18720 {
18721 int reconsider;
18722 location_t saved_loc = input_location;
18723
18724 /* Instantiating templates may trigger vtable generation. This in turn
18725 may require further template instantiations. We place a limit here
18726 to avoid infinite loop. */
18727 if (pending_templates && retries >= max_tinst_depth)
18728 {
18729 tree decl = pending_templates->tinst->decl;
18730
18731 error ("template instantiation depth exceeds maximum of %d"
18732 " instantiating %q+D, possibly from virtual table generation"
18733 " (use -ftemplate-depth= to increase the maximum)",
18734 max_tinst_depth, decl);
18735 if (TREE_CODE (decl) == FUNCTION_DECL)
18736 /* Pretend that we defined it. */
18737 DECL_INITIAL (decl) = error_mark_node;
18738 return;
18739 }
18740
18741 do
18742 {
18743 struct pending_template **t = &pending_templates;
18744 struct pending_template *last = NULL;
18745 reconsider = 0;
18746 while (*t)
18747 {
18748 tree instantiation = reopen_tinst_level ((*t)->tinst);
18749 bool complete = false;
18750
18751 if (TYPE_P (instantiation))
18752 {
18753 tree fn;
18754
18755 if (!COMPLETE_TYPE_P (instantiation))
18756 {
18757 instantiate_class_template (instantiation);
18758 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18759 for (fn = TYPE_METHODS (instantiation);
18760 fn;
18761 fn = TREE_CHAIN (fn))
18762 if (! DECL_ARTIFICIAL (fn))
18763 instantiate_decl (fn,
18764 /*defer_ok=*/0,
18765 /*expl_inst_class_mem_p=*/false);
18766 if (COMPLETE_TYPE_P (instantiation))
18767 reconsider = 1;
18768 }
18769
18770 complete = COMPLETE_TYPE_P (instantiation);
18771 }
18772 else
18773 {
18774 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
18775 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
18776 {
18777 instantiation
18778 = instantiate_decl (instantiation,
18779 /*defer_ok=*/0,
18780 /*expl_inst_class_mem_p=*/false);
18781 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
18782 reconsider = 1;
18783 }
18784
18785 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
18786 || DECL_TEMPLATE_INSTANTIATED (instantiation));
18787 }
18788
18789 if (complete)
18790 /* If INSTANTIATION has been instantiated, then we don't
18791 need to consider it again in the future. */
18792 *t = (*t)->next;
18793 else
18794 {
18795 last = *t;
18796 t = &(*t)->next;
18797 }
18798 tinst_depth = 0;
18799 current_tinst_level = NULL;
18800 }
18801 last_pending_template = last;
18802 }
18803 while (reconsider);
18804
18805 input_location = saved_loc;
18806 }
18807
18808 /* Substitute ARGVEC into T, which is a list of initializers for
18809 either base class or a non-static data member. The TREE_PURPOSEs
18810 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18811 instantiate_decl. */
18812
18813 static tree
18814 tsubst_initializer_list (tree t, tree argvec)
18815 {
18816 tree inits = NULL_TREE;
18817
18818 for (; t; t = TREE_CHAIN (t))
18819 {
18820 tree decl;
18821 tree init;
18822 tree expanded_bases = NULL_TREE;
18823 tree expanded_arguments = NULL_TREE;
18824 int i, len = 1;
18825
18826 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
18827 {
18828 tree expr;
18829 tree arg;
18830
18831 /* Expand the base class expansion type into separate base
18832 classes. */
18833 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
18834 tf_warning_or_error,
18835 NULL_TREE);
18836 if (expanded_bases == error_mark_node)
18837 continue;
18838
18839 /* We'll be building separate TREE_LISTs of arguments for
18840 each base. */
18841 len = TREE_VEC_LENGTH (expanded_bases);
18842 expanded_arguments = make_tree_vec (len);
18843 for (i = 0; i < len; i++)
18844 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
18845
18846 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18847 expand each argument in the TREE_VALUE of t. */
18848 expr = make_node (EXPR_PACK_EXPANSION);
18849 PACK_EXPANSION_LOCAL_P (expr) = true;
18850 PACK_EXPANSION_PARAMETER_PACKS (expr) =
18851 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
18852
18853 if (TREE_VALUE (t) == void_type_node)
18854 /* VOID_TYPE_NODE is used to indicate
18855 value-initialization. */
18856 {
18857 for (i = 0; i < len; i++)
18858 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
18859 }
18860 else
18861 {
18862 /* Substitute parameter packs into each argument in the
18863 TREE_LIST. */
18864 in_base_initializer = 1;
18865 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
18866 {
18867 tree expanded_exprs;
18868
18869 /* Expand the argument. */
18870 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
18871 expanded_exprs
18872 = tsubst_pack_expansion (expr, argvec,
18873 tf_warning_or_error,
18874 NULL_TREE);
18875 if (expanded_exprs == error_mark_node)
18876 continue;
18877
18878 /* Prepend each of the expanded expressions to the
18879 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18880 for (i = 0; i < len; i++)
18881 {
18882 TREE_VEC_ELT (expanded_arguments, i) =
18883 tree_cons (NULL_TREE,
18884 TREE_VEC_ELT (expanded_exprs, i),
18885 TREE_VEC_ELT (expanded_arguments, i));
18886 }
18887 }
18888 in_base_initializer = 0;
18889
18890 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18891 since we built them backwards. */
18892 for (i = 0; i < len; i++)
18893 {
18894 TREE_VEC_ELT (expanded_arguments, i) =
18895 nreverse (TREE_VEC_ELT (expanded_arguments, i));
18896 }
18897 }
18898 }
18899
18900 for (i = 0; i < len; ++i)
18901 {
18902 if (expanded_bases)
18903 {
18904 decl = TREE_VEC_ELT (expanded_bases, i);
18905 decl = expand_member_init (decl);
18906 init = TREE_VEC_ELT (expanded_arguments, i);
18907 }
18908 else
18909 {
18910 tree tmp;
18911 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
18912 tf_warning_or_error, NULL_TREE);
18913
18914 decl = expand_member_init (decl);
18915 if (decl && !DECL_P (decl))
18916 in_base_initializer = 1;
18917
18918 init = TREE_VALUE (t);
18919 tmp = init;
18920 if (init != void_type_node)
18921 init = tsubst_expr (init, argvec,
18922 tf_warning_or_error, NULL_TREE,
18923 /*integral_constant_expression_p=*/false);
18924 if (init == NULL_TREE && tmp != NULL_TREE)
18925 /* If we had an initializer but it instantiated to nothing,
18926 value-initialize the object. This will only occur when
18927 the initializer was a pack expansion where the parameter
18928 packs used in that expansion were of length zero. */
18929 init = void_type_node;
18930 in_base_initializer = 0;
18931 }
18932
18933 if (decl)
18934 {
18935 init = build_tree_list (decl, init);
18936 TREE_CHAIN (init) = inits;
18937 inits = init;
18938 }
18939 }
18940 }
18941 return inits;
18942 }
18943
18944 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18945
18946 static void
18947 set_current_access_from_decl (tree decl)
18948 {
18949 if (TREE_PRIVATE (decl))
18950 current_access_specifier = access_private_node;
18951 else if (TREE_PROTECTED (decl))
18952 current_access_specifier = access_protected_node;
18953 else
18954 current_access_specifier = access_public_node;
18955 }
18956
18957 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18958 is the instantiation (which should have been created with
18959 start_enum) and ARGS are the template arguments to use. */
18960
18961 static void
18962 tsubst_enum (tree tag, tree newtag, tree args)
18963 {
18964 tree e;
18965
18966 if (SCOPED_ENUM_P (newtag))
18967 begin_scope (sk_scoped_enum, newtag);
18968
18969 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
18970 {
18971 tree value;
18972 tree decl;
18973
18974 decl = TREE_VALUE (e);
18975 /* Note that in a template enum, the TREE_VALUE is the
18976 CONST_DECL, not the corresponding INTEGER_CST. */
18977 value = tsubst_expr (DECL_INITIAL (decl),
18978 args, tf_warning_or_error, NULL_TREE,
18979 /*integral_constant_expression_p=*/true);
18980
18981 /* Give this enumeration constant the correct access. */
18982 set_current_access_from_decl (decl);
18983
18984 /* Actually build the enumerator itself. */
18985 build_enumerator
18986 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
18987 }
18988
18989 if (SCOPED_ENUM_P (newtag))
18990 finish_scope ();
18991
18992 finish_enum_value_list (newtag);
18993 finish_enum (newtag);
18994
18995 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
18996 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
18997 }
18998
18999 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19000 its type -- but without substituting the innermost set of template
19001 arguments. So, innermost set of template parameters will appear in
19002 the type. */
19003
19004 tree
19005 get_mostly_instantiated_function_type (tree decl)
19006 {
19007 tree fn_type;
19008 tree tmpl;
19009 tree targs;
19010 tree tparms;
19011 int parm_depth;
19012
19013 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
19014 targs = DECL_TI_ARGS (decl);
19015 tparms = DECL_TEMPLATE_PARMS (tmpl);
19016 parm_depth = TMPL_PARMS_DEPTH (tparms);
19017
19018 /* There should be as many levels of arguments as there are levels
19019 of parameters. */
19020 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
19021
19022 fn_type = TREE_TYPE (tmpl);
19023
19024 if (parm_depth == 1)
19025 /* No substitution is necessary. */
19026 ;
19027 else
19028 {
19029 int i;
19030 tree partial_args;
19031
19032 /* Replace the innermost level of the TARGS with NULL_TREEs to
19033 let tsubst know not to substitute for those parameters. */
19034 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
19035 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
19036 SET_TMPL_ARGS_LEVEL (partial_args, i,
19037 TMPL_ARGS_LEVEL (targs, i));
19038 SET_TMPL_ARGS_LEVEL (partial_args,
19039 TMPL_ARGS_DEPTH (targs),
19040 make_tree_vec (DECL_NTPARMS (tmpl)));
19041
19042 /* Make sure that we can see identifiers, and compute access
19043 correctly. */
19044 push_access_scope (decl);
19045
19046 ++processing_template_decl;
19047 /* Now, do the (partial) substitution to figure out the
19048 appropriate function type. */
19049 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
19050 --processing_template_decl;
19051
19052 /* Substitute into the template parameters to obtain the real
19053 innermost set of parameters. This step is important if the
19054 innermost set of template parameters contains value
19055 parameters whose types depend on outer template parameters. */
19056 TREE_VEC_LENGTH (partial_args)--;
19057 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
19058
19059 pop_access_scope (decl);
19060 }
19061
19062 return fn_type;
19063 }
19064
19065 /* Return truthvalue if we're processing a template different from
19066 the last one involved in diagnostics. */
19067 int
19068 problematic_instantiation_changed (void)
19069 {
19070 return current_tinst_level != last_error_tinst_level;
19071 }
19072
19073 /* Remember current template involved in diagnostics. */
19074 void
19075 record_last_problematic_instantiation (void)
19076 {
19077 last_error_tinst_level = current_tinst_level;
19078 }
19079
19080 struct tinst_level *
19081 current_instantiation (void)
19082 {
19083 return current_tinst_level;
19084 }
19085
19086 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19087 type. Return zero for ok, nonzero for disallowed. Issue error and
19088 warning messages under control of COMPLAIN. */
19089
19090 static int
19091 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
19092 {
19093 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
19094 return 0;
19095 else if (POINTER_TYPE_P (type))
19096 return 0;
19097 else if (TYPE_PTRMEM_P (type))
19098 return 0;
19099 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
19100 return 0;
19101 else if (TREE_CODE (type) == TYPENAME_TYPE)
19102 return 0;
19103 else if (TREE_CODE (type) == DECLTYPE_TYPE)
19104 return 0;
19105 else if (TREE_CODE (type) == NULLPTR_TYPE)
19106 return 0;
19107
19108 if (complain & tf_error)
19109 {
19110 if (type == error_mark_node)
19111 inform (input_location, "invalid template non-type parameter");
19112 else
19113 error ("%q#T is not a valid type for a template non-type parameter",
19114 type);
19115 }
19116 return 1;
19117 }
19118
19119 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19120 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19121
19122 static bool
19123 dependent_type_p_r (tree type)
19124 {
19125 tree scope;
19126
19127 /* [temp.dep.type]
19128
19129 A type is dependent if it is:
19130
19131 -- a template parameter. Template template parameters are types
19132 for us (since TYPE_P holds true for them) so we handle
19133 them here. */
19134 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19135 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
19136 return true;
19137 /* -- a qualified-id with a nested-name-specifier which contains a
19138 class-name that names a dependent type or whose unqualified-id
19139 names a dependent type. */
19140 if (TREE_CODE (type) == TYPENAME_TYPE)
19141 return true;
19142 /* -- a cv-qualified type where the cv-unqualified type is
19143 dependent. */
19144 type = TYPE_MAIN_VARIANT (type);
19145 /* -- a compound type constructed from any dependent type. */
19146 if (TYPE_PTRMEM_P (type))
19147 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
19148 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19149 (type)));
19150 else if (TREE_CODE (type) == POINTER_TYPE
19151 || TREE_CODE (type) == REFERENCE_TYPE)
19152 return dependent_type_p (TREE_TYPE (type));
19153 else if (TREE_CODE (type) == FUNCTION_TYPE
19154 || TREE_CODE (type) == METHOD_TYPE)
19155 {
19156 tree arg_type;
19157
19158 if (dependent_type_p (TREE_TYPE (type)))
19159 return true;
19160 for (arg_type = TYPE_ARG_TYPES (type);
19161 arg_type;
19162 arg_type = TREE_CHAIN (arg_type))
19163 if (dependent_type_p (TREE_VALUE (arg_type)))
19164 return true;
19165 return false;
19166 }
19167 /* -- an array type constructed from any dependent type or whose
19168 size is specified by a constant expression that is
19169 value-dependent.
19170
19171 We checked for type- and value-dependence of the bounds in
19172 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19173 if (TREE_CODE (type) == ARRAY_TYPE)
19174 {
19175 if (TYPE_DOMAIN (type)
19176 && dependent_type_p (TYPE_DOMAIN (type)))
19177 return true;
19178 return dependent_type_p (TREE_TYPE (type));
19179 }
19180
19181 /* -- a template-id in which either the template name is a template
19182 parameter ... */
19183 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19184 return true;
19185 /* ... or any of the template arguments is a dependent type or
19186 an expression that is type-dependent or value-dependent. */
19187 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
19188 && (any_dependent_template_arguments_p
19189 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
19190 return true;
19191
19192 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19193 dependent; if the argument of the `typeof' expression is not
19194 type-dependent, then it should already been have resolved. */
19195 if (TREE_CODE (type) == TYPEOF_TYPE
19196 || TREE_CODE (type) == DECLTYPE_TYPE
19197 || TREE_CODE (type) == UNDERLYING_TYPE)
19198 return true;
19199
19200 /* A template argument pack is dependent if any of its packed
19201 arguments are. */
19202 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
19203 {
19204 tree args = ARGUMENT_PACK_ARGS (type);
19205 int i, len = TREE_VEC_LENGTH (args);
19206 for (i = 0; i < len; ++i)
19207 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19208 return true;
19209 }
19210
19211 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19212 be template parameters. */
19213 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
19214 return true;
19215
19216 /* The standard does not specifically mention types that are local
19217 to template functions or local classes, but they should be
19218 considered dependent too. For example:
19219
19220 template <int I> void f() {
19221 enum E { a = I };
19222 S<sizeof (E)> s;
19223 }
19224
19225 The size of `E' cannot be known until the value of `I' has been
19226 determined. Therefore, `E' must be considered dependent. */
19227 scope = TYPE_CONTEXT (type);
19228 if (scope && TYPE_P (scope))
19229 return dependent_type_p (scope);
19230 /* Don't use type_dependent_expression_p here, as it can lead
19231 to infinite recursion trying to determine whether a lambda
19232 nested in a lambda is dependent (c++/47687). */
19233 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
19234 && DECL_LANG_SPECIFIC (scope)
19235 && DECL_TEMPLATE_INFO (scope)
19236 && (any_dependent_template_arguments_p
19237 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
19238 return true;
19239
19240 /* Other types are non-dependent. */
19241 return false;
19242 }
19243
19244 /* Returns TRUE if TYPE is dependent, in the sense of
19245 [temp.dep.type]. Note that a NULL type is considered dependent. */
19246
19247 bool
19248 dependent_type_p (tree type)
19249 {
19250 /* If there are no template parameters in scope, then there can't be
19251 any dependent types. */
19252 if (!processing_template_decl)
19253 {
19254 /* If we are not processing a template, then nobody should be
19255 providing us with a dependent type. */
19256 gcc_assert (type);
19257 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
19258 return false;
19259 }
19260
19261 /* If the type is NULL, we have not computed a type for the entity
19262 in question; in that case, the type is dependent. */
19263 if (!type)
19264 return true;
19265
19266 /* Erroneous types can be considered non-dependent. */
19267 if (type == error_mark_node)
19268 return false;
19269
19270 /* If we have not already computed the appropriate value for TYPE,
19271 do so now. */
19272 if (!TYPE_DEPENDENT_P_VALID (type))
19273 {
19274 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
19275 TYPE_DEPENDENT_P_VALID (type) = 1;
19276 }
19277
19278 return TYPE_DEPENDENT_P (type);
19279 }
19280
19281 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19282 lookup. In other words, a dependent type that is not the current
19283 instantiation. */
19284
19285 bool
19286 dependent_scope_p (tree scope)
19287 {
19288 return (scope && TYPE_P (scope) && dependent_type_p (scope)
19289 && !currently_open_class (scope));
19290 }
19291
19292 /* T is a SCOPE_REF; return whether we need to consider it
19293 instantiation-dependent so that we can check access at instantiation
19294 time even though we know which member it resolves to. */
19295
19296 static bool
19297 instantiation_dependent_scope_ref_p (tree t)
19298 {
19299 if (DECL_P (TREE_OPERAND (t, 1))
19300 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
19301 && accessible_in_template_p (TREE_OPERAND (t, 0),
19302 TREE_OPERAND (t, 1)))
19303 return false;
19304 else
19305 return true;
19306 }
19307
19308 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19309 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19310 expression. */
19311
19312 /* Note that this predicate is not appropriate for general expressions;
19313 only constant expressions (that satisfy potential_constant_expression)
19314 can be tested for value dependence. */
19315
19316 bool
19317 value_dependent_expression_p (tree expression)
19318 {
19319 if (!processing_template_decl)
19320 return false;
19321
19322 /* A name declared with a dependent type. */
19323 if (DECL_P (expression) && type_dependent_expression_p (expression))
19324 return true;
19325
19326 switch (TREE_CODE (expression))
19327 {
19328 case IDENTIFIER_NODE:
19329 /* A name that has not been looked up -- must be dependent. */
19330 return true;
19331
19332 case TEMPLATE_PARM_INDEX:
19333 /* A non-type template parm. */
19334 return true;
19335
19336 case CONST_DECL:
19337 /* A non-type template parm. */
19338 if (DECL_TEMPLATE_PARM_P (expression))
19339 return true;
19340 return value_dependent_expression_p (DECL_INITIAL (expression));
19341
19342 case VAR_DECL:
19343 /* A constant with literal type and is initialized
19344 with an expression that is value-dependent.
19345
19346 Note that a non-dependent parenthesized initializer will have
19347 already been replaced with its constant value, so if we see
19348 a TREE_LIST it must be dependent. */
19349 if (DECL_INITIAL (expression)
19350 && decl_constant_var_p (expression)
19351 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
19352 || value_dependent_expression_p (DECL_INITIAL (expression))))
19353 return true;
19354 return false;
19355
19356 case DYNAMIC_CAST_EXPR:
19357 case STATIC_CAST_EXPR:
19358 case CONST_CAST_EXPR:
19359 case REINTERPRET_CAST_EXPR:
19360 case CAST_EXPR:
19361 /* These expressions are value-dependent if the type to which
19362 the cast occurs is dependent or the expression being casted
19363 is value-dependent. */
19364 {
19365 tree type = TREE_TYPE (expression);
19366
19367 if (dependent_type_p (type))
19368 return true;
19369
19370 /* A functional cast has a list of operands. */
19371 expression = TREE_OPERAND (expression, 0);
19372 if (!expression)
19373 {
19374 /* If there are no operands, it must be an expression such
19375 as "int()". This should not happen for aggregate types
19376 because it would form non-constant expressions. */
19377 gcc_assert (cxx_dialect >= cxx0x
19378 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
19379
19380 return false;
19381 }
19382
19383 if (TREE_CODE (expression) == TREE_LIST)
19384 return any_value_dependent_elements_p (expression);
19385
19386 return value_dependent_expression_p (expression);
19387 }
19388
19389 case SIZEOF_EXPR:
19390 if (SIZEOF_EXPR_TYPE_P (expression))
19391 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
19392 /* FALLTHRU */
19393 case ALIGNOF_EXPR:
19394 case TYPEID_EXPR:
19395 /* A `sizeof' expression is value-dependent if the operand is
19396 type-dependent or is a pack expansion. */
19397 expression = TREE_OPERAND (expression, 0);
19398 if (PACK_EXPANSION_P (expression))
19399 return true;
19400 else if (TYPE_P (expression))
19401 return dependent_type_p (expression);
19402 return instantiation_dependent_expression_p (expression);
19403
19404 case AT_ENCODE_EXPR:
19405 /* An 'encode' expression is value-dependent if the operand is
19406 type-dependent. */
19407 expression = TREE_OPERAND (expression, 0);
19408 return dependent_type_p (expression);
19409
19410 case NOEXCEPT_EXPR:
19411 expression = TREE_OPERAND (expression, 0);
19412 return instantiation_dependent_expression_p (expression);
19413
19414 case SCOPE_REF:
19415 /* All instantiation-dependent expressions should also be considered
19416 value-dependent. */
19417 return instantiation_dependent_scope_ref_p (expression);
19418
19419 case COMPONENT_REF:
19420 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
19421 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
19422
19423 case NONTYPE_ARGUMENT_PACK:
19424 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19425 is value-dependent. */
19426 {
19427 tree values = ARGUMENT_PACK_ARGS (expression);
19428 int i, len = TREE_VEC_LENGTH (values);
19429
19430 for (i = 0; i < len; ++i)
19431 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
19432 return true;
19433
19434 return false;
19435 }
19436
19437 case TRAIT_EXPR:
19438 {
19439 tree type2 = TRAIT_EXPR_TYPE2 (expression);
19440 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
19441 || (type2 ? dependent_type_p (type2) : false));
19442 }
19443
19444 case MODOP_EXPR:
19445 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19446 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
19447
19448 case ARRAY_REF:
19449 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19450 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
19451
19452 case ADDR_EXPR:
19453 {
19454 tree op = TREE_OPERAND (expression, 0);
19455 return (value_dependent_expression_p (op)
19456 || has_value_dependent_address (op));
19457 }
19458
19459 case CALL_EXPR:
19460 {
19461 tree fn = get_callee_fndecl (expression);
19462 int i, nargs;
19463 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
19464 return true;
19465 nargs = call_expr_nargs (expression);
19466 for (i = 0; i < nargs; ++i)
19467 {
19468 tree op = CALL_EXPR_ARG (expression, i);
19469 /* In a call to a constexpr member function, look through the
19470 implicit ADDR_EXPR on the object argument so that it doesn't
19471 cause the call to be considered value-dependent. We also
19472 look through it in potential_constant_expression. */
19473 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
19474 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19475 && TREE_CODE (op) == ADDR_EXPR)
19476 op = TREE_OPERAND (op, 0);
19477 if (value_dependent_expression_p (op))
19478 return true;
19479 }
19480 return false;
19481 }
19482
19483 case TEMPLATE_ID_EXPR:
19484 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19485 type-dependent. */
19486 return type_dependent_expression_p (expression);
19487
19488 case CONSTRUCTOR:
19489 {
19490 unsigned ix;
19491 tree val;
19492 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
19493 if (value_dependent_expression_p (val))
19494 return true;
19495 return false;
19496 }
19497
19498 case STMT_EXPR:
19499 /* Treat a GNU statement expression as dependent to avoid crashing
19500 under fold_non_dependent_expr; it can't be constant. */
19501 return true;
19502
19503 default:
19504 /* A constant expression is value-dependent if any subexpression is
19505 value-dependent. */
19506 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
19507 {
19508 case tcc_reference:
19509 case tcc_unary:
19510 case tcc_comparison:
19511 case tcc_binary:
19512 case tcc_expression:
19513 case tcc_vl_exp:
19514 {
19515 int i, len = cp_tree_operand_length (expression);
19516
19517 for (i = 0; i < len; i++)
19518 {
19519 tree t = TREE_OPERAND (expression, i);
19520
19521 /* In some cases, some of the operands may be missing.l
19522 (For example, in the case of PREDECREMENT_EXPR, the
19523 amount to increment by may be missing.) That doesn't
19524 make the expression dependent. */
19525 if (t && value_dependent_expression_p (t))
19526 return true;
19527 }
19528 }
19529 break;
19530 default:
19531 break;
19532 }
19533 break;
19534 }
19535
19536 /* The expression is not value-dependent. */
19537 return false;
19538 }
19539
19540 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19541 [temp.dep.expr]. Note that an expression with no type is
19542 considered dependent. Other parts of the compiler arrange for an
19543 expression with type-dependent subexpressions to have no type, so
19544 this function doesn't have to be fully recursive. */
19545
19546 bool
19547 type_dependent_expression_p (tree expression)
19548 {
19549 if (!processing_template_decl)
19550 return false;
19551
19552 if (expression == error_mark_node)
19553 return false;
19554
19555 /* An unresolved name is always dependent. */
19556 if (TREE_CODE (expression) == IDENTIFIER_NODE
19557 || TREE_CODE (expression) == USING_DECL)
19558 return true;
19559
19560 /* Some expression forms are never type-dependent. */
19561 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19562 || TREE_CODE (expression) == SIZEOF_EXPR
19563 || TREE_CODE (expression) == ALIGNOF_EXPR
19564 || TREE_CODE (expression) == AT_ENCODE_EXPR
19565 || TREE_CODE (expression) == NOEXCEPT_EXPR
19566 || TREE_CODE (expression) == TRAIT_EXPR
19567 || TREE_CODE (expression) == TYPEID_EXPR
19568 || TREE_CODE (expression) == DELETE_EXPR
19569 || TREE_CODE (expression) == VEC_DELETE_EXPR
19570 || TREE_CODE (expression) == THROW_EXPR)
19571 return false;
19572
19573 /* The types of these expressions depends only on the type to which
19574 the cast occurs. */
19575 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19576 || TREE_CODE (expression) == STATIC_CAST_EXPR
19577 || TREE_CODE (expression) == CONST_CAST_EXPR
19578 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19579 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
19580 || TREE_CODE (expression) == CAST_EXPR)
19581 return dependent_type_p (TREE_TYPE (expression));
19582
19583 /* The types of these expressions depends only on the type created
19584 by the expression. */
19585 if (TREE_CODE (expression) == NEW_EXPR
19586 || TREE_CODE (expression) == VEC_NEW_EXPR)
19587 {
19588 /* For NEW_EXPR tree nodes created inside a template, either
19589 the object type itself or a TREE_LIST may appear as the
19590 operand 1. */
19591 tree type = TREE_OPERAND (expression, 1);
19592 if (TREE_CODE (type) == TREE_LIST)
19593 /* This is an array type. We need to check array dimensions
19594 as well. */
19595 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19596 || value_dependent_expression_p
19597 (TREE_OPERAND (TREE_VALUE (type), 1));
19598 else
19599 return dependent_type_p (type);
19600 }
19601
19602 if (TREE_CODE (expression) == SCOPE_REF)
19603 {
19604 tree scope = TREE_OPERAND (expression, 0);
19605 tree name = TREE_OPERAND (expression, 1);
19606
19607 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19608 contains an identifier associated by name lookup with one or more
19609 declarations declared with a dependent type, or...a
19610 nested-name-specifier or qualified-id that names a member of an
19611 unknown specialization. */
19612 return (type_dependent_expression_p (name)
19613 || dependent_scope_p (scope));
19614 }
19615
19616 if (TREE_CODE (expression) == FUNCTION_DECL
19617 && DECL_LANG_SPECIFIC (expression)
19618 && DECL_TEMPLATE_INFO (expression)
19619 && (any_dependent_template_arguments_p
19620 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19621 return true;
19622
19623 if (TREE_CODE (expression) == TEMPLATE_DECL
19624 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19625 return false;
19626
19627 if (TREE_CODE (expression) == STMT_EXPR)
19628 expression = stmt_expr_value_expr (expression);
19629
19630 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19631 {
19632 tree elt;
19633 unsigned i;
19634
19635 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19636 {
19637 if (type_dependent_expression_p (elt))
19638 return true;
19639 }
19640 return false;
19641 }
19642
19643 /* A static data member of the current instantiation with incomplete
19644 array type is type-dependent, as the definition and specializations
19645 can have different bounds. */
19646 if (TREE_CODE (expression) == VAR_DECL
19647 && DECL_CLASS_SCOPE_P (expression)
19648 && dependent_type_p (DECL_CONTEXT (expression))
19649 && VAR_HAD_UNKNOWN_BOUND (expression))
19650 return true;
19651
19652 if (TREE_TYPE (expression) == unknown_type_node)
19653 {
19654 if (TREE_CODE (expression) == ADDR_EXPR)
19655 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19656 if (TREE_CODE (expression) == COMPONENT_REF
19657 || TREE_CODE (expression) == OFFSET_REF)
19658 {
19659 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19660 return true;
19661 expression = TREE_OPERAND (expression, 1);
19662 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19663 return false;
19664 }
19665 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19666 if (TREE_CODE (expression) == SCOPE_REF)
19667 return false;
19668
19669 if (BASELINK_P (expression))
19670 expression = BASELINK_FUNCTIONS (expression);
19671
19672 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19673 {
19674 if (any_dependent_template_arguments_p
19675 (TREE_OPERAND (expression, 1)))
19676 return true;
19677 expression = TREE_OPERAND (expression, 0);
19678 }
19679 gcc_assert (TREE_CODE (expression) == OVERLOAD
19680 || TREE_CODE (expression) == FUNCTION_DECL);
19681
19682 while (expression)
19683 {
19684 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19685 return true;
19686 expression = OVL_NEXT (expression);
19687 }
19688 return false;
19689 }
19690
19691 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19692
19693 return (dependent_type_p (TREE_TYPE (expression)));
19694 }
19695
19696 /* walk_tree callback function for instantiation_dependent_expression_p,
19697 below. Returns non-zero if a dependent subexpression is found. */
19698
19699 static tree
19700 instantiation_dependent_r (tree *tp, int *walk_subtrees,
19701 void * /*data*/)
19702 {
19703 if (TYPE_P (*tp))
19704 {
19705 /* We don't have to worry about decltype currently because decltype
19706 of an instantiation-dependent expr is a dependent type. This
19707 might change depending on the resolution of DR 1172. */
19708 *walk_subtrees = false;
19709 return NULL_TREE;
19710 }
19711 enum tree_code code = TREE_CODE (*tp);
19712 switch (code)
19713 {
19714 /* Don't treat an argument list as dependent just because it has no
19715 TREE_TYPE. */
19716 case TREE_LIST:
19717 case TREE_VEC:
19718 return NULL_TREE;
19719
19720 case TEMPLATE_PARM_INDEX:
19721 return *tp;
19722
19723 /* Handle expressions with type operands. */
19724 case SIZEOF_EXPR:
19725 case ALIGNOF_EXPR:
19726 case TYPEID_EXPR:
19727 case AT_ENCODE_EXPR:
19728 case TRAIT_EXPR:
19729 {
19730 tree op = TREE_OPERAND (*tp, 0);
19731 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
19732 op = TREE_TYPE (op);
19733 if (TYPE_P (op))
19734 {
19735 if (dependent_type_p (op)
19736 || (code == TRAIT_EXPR
19737 && dependent_type_p (TREE_OPERAND (*tp, 1))))
19738 return *tp;
19739 else
19740 {
19741 *walk_subtrees = false;
19742 return NULL_TREE;
19743 }
19744 }
19745 break;
19746 }
19747
19748 case COMPONENT_REF:
19749 if (TREE_CODE (TREE_OPERAND (*tp, 1)) == IDENTIFIER_NODE)
19750 /* In a template, finish_class_member_access_expr creates a
19751 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19752 type-dependent, so that we can check access control at
19753 instantiation time (PR 42277). See also Core issue 1273. */
19754 return *tp;
19755 break;
19756
19757 case SCOPE_REF:
19758 if (instantiation_dependent_scope_ref_p (*tp))
19759 return *tp;
19760 else
19761 break;
19762
19763 default:
19764 break;
19765 }
19766
19767 if (type_dependent_expression_p (*tp))
19768 return *tp;
19769 else
19770 return NULL_TREE;
19771 }
19772
19773 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19774 sense defined by the ABI:
19775
19776 "An expression is instantiation-dependent if it is type-dependent
19777 or value-dependent, or it has a subexpression that is type-dependent
19778 or value-dependent." */
19779
19780 bool
19781 instantiation_dependent_expression_p (tree expression)
19782 {
19783 tree result;
19784
19785 if (!processing_template_decl)
19786 return false;
19787
19788 if (expression == error_mark_node)
19789 return false;
19790
19791 result = cp_walk_tree_without_duplicates (&expression,
19792 instantiation_dependent_r, NULL);
19793 return result != NULL_TREE;
19794 }
19795
19796 /* Like type_dependent_expression_p, but it also works while not processing
19797 a template definition, i.e. during substitution or mangling. */
19798
19799 bool
19800 type_dependent_expression_p_push (tree expr)
19801 {
19802 bool b;
19803 ++processing_template_decl;
19804 b = type_dependent_expression_p (expr);
19805 --processing_template_decl;
19806 return b;
19807 }
19808
19809 /* Returns TRUE if ARGS contains a type-dependent expression. */
19810
19811 bool
19812 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
19813 {
19814 unsigned int i;
19815 tree arg;
19816
19817 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
19818 {
19819 if (type_dependent_expression_p (arg))
19820 return true;
19821 }
19822 return false;
19823 }
19824
19825 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19826 expressions) contains any type-dependent expressions. */
19827
19828 bool
19829 any_type_dependent_elements_p (const_tree list)
19830 {
19831 for (; list; list = TREE_CHAIN (list))
19832 if (value_dependent_expression_p (TREE_VALUE (list)))
19833 return true;
19834
19835 return false;
19836 }
19837
19838 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19839 expressions) contains any value-dependent expressions. */
19840
19841 bool
19842 any_value_dependent_elements_p (const_tree list)
19843 {
19844 for (; list; list = TREE_CHAIN (list))
19845 if (value_dependent_expression_p (TREE_VALUE (list)))
19846 return true;
19847
19848 return false;
19849 }
19850
19851 /* Returns TRUE if the ARG (a template argument) is dependent. */
19852
19853 bool
19854 dependent_template_arg_p (tree arg)
19855 {
19856 if (!processing_template_decl)
19857 return false;
19858
19859 /* Assume a template argument that was wrongly written by the user
19860 is dependent. This is consistent with what
19861 any_dependent_template_arguments_p [that calls this function]
19862 does. */
19863 if (!arg || arg == error_mark_node)
19864 return true;
19865
19866 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
19867 arg = ARGUMENT_PACK_SELECT_ARG (arg);
19868
19869 if (TREE_CODE (arg) == TEMPLATE_DECL
19870 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19871 return dependent_template_p (arg);
19872 else if (ARGUMENT_PACK_P (arg))
19873 {
19874 tree args = ARGUMENT_PACK_ARGS (arg);
19875 int i, len = TREE_VEC_LENGTH (args);
19876 for (i = 0; i < len; ++i)
19877 {
19878 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19879 return true;
19880 }
19881
19882 return false;
19883 }
19884 else if (TYPE_P (arg))
19885 return dependent_type_p (arg);
19886 else
19887 return (type_dependent_expression_p (arg)
19888 || value_dependent_expression_p (arg));
19889 }
19890
19891 /* Returns true if ARGS (a collection of template arguments) contains
19892 any types that require structural equality testing. */
19893
19894 bool
19895 any_template_arguments_need_structural_equality_p (tree args)
19896 {
19897 int i;
19898 int j;
19899
19900 if (!args)
19901 return false;
19902 if (args == error_mark_node)
19903 return true;
19904
19905 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19906 {
19907 tree level = TMPL_ARGS_LEVEL (args, i + 1);
19908 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19909 {
19910 tree arg = TREE_VEC_ELT (level, j);
19911 tree packed_args = NULL_TREE;
19912 int k, len = 1;
19913
19914 if (ARGUMENT_PACK_P (arg))
19915 {
19916 /* Look inside the argument pack. */
19917 packed_args = ARGUMENT_PACK_ARGS (arg);
19918 len = TREE_VEC_LENGTH (packed_args);
19919 }
19920
19921 for (k = 0; k < len; ++k)
19922 {
19923 if (packed_args)
19924 arg = TREE_VEC_ELT (packed_args, k);
19925
19926 if (error_operand_p (arg))
19927 return true;
19928 else if (TREE_CODE (arg) == TEMPLATE_DECL
19929 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19930 continue;
19931 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
19932 return true;
19933 else if (!TYPE_P (arg) && TREE_TYPE (arg)
19934 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
19935 return true;
19936 }
19937 }
19938 }
19939
19940 return false;
19941 }
19942
19943 /* Returns true if ARGS (a collection of template arguments) contains
19944 any dependent arguments. */
19945
19946 bool
19947 any_dependent_template_arguments_p (const_tree args)
19948 {
19949 int i;
19950 int j;
19951
19952 if (!args)
19953 return false;
19954 if (args == error_mark_node)
19955 return true;
19956
19957 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19958 {
19959 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
19960 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19961 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
19962 return true;
19963 }
19964
19965 return false;
19966 }
19967
19968 /* Returns TRUE if the template TMPL is dependent. */
19969
19970 bool
19971 dependent_template_p (tree tmpl)
19972 {
19973 if (TREE_CODE (tmpl) == OVERLOAD)
19974 {
19975 while (tmpl)
19976 {
19977 if (dependent_template_p (OVL_CURRENT (tmpl)))
19978 return true;
19979 tmpl = OVL_NEXT (tmpl);
19980 }
19981 return false;
19982 }
19983
19984 /* Template template parameters are dependent. */
19985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
19986 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
19987 return true;
19988 /* So are names that have not been looked up. */
19989 if (TREE_CODE (tmpl) == SCOPE_REF
19990 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
19991 return true;
19992 /* So are member templates of dependent classes. */
19993 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
19994 return dependent_type_p (DECL_CONTEXT (tmpl));
19995 return false;
19996 }
19997
19998 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19999
20000 bool
20001 dependent_template_id_p (tree tmpl, tree args)
20002 {
20003 return (dependent_template_p (tmpl)
20004 || any_dependent_template_arguments_p (args));
20005 }
20006
20007 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
20008 is dependent. */
20009
20010 bool
20011 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
20012 {
20013 int i;
20014
20015 if (!processing_template_decl)
20016 return false;
20017
20018 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
20019 {
20020 tree decl = TREE_VEC_ELT (declv, i);
20021 tree init = TREE_VEC_ELT (initv, i);
20022 tree cond = TREE_VEC_ELT (condv, i);
20023 tree incr = TREE_VEC_ELT (incrv, i);
20024
20025 if (type_dependent_expression_p (decl))
20026 return true;
20027
20028 if (init && type_dependent_expression_p (init))
20029 return true;
20030
20031 if (type_dependent_expression_p (cond))
20032 return true;
20033
20034 if (COMPARISON_CLASS_P (cond)
20035 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
20036 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
20037 return true;
20038
20039 if (TREE_CODE (incr) == MODOP_EXPR)
20040 {
20041 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
20042 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
20043 return true;
20044 }
20045 else if (type_dependent_expression_p (incr))
20046 return true;
20047 else if (TREE_CODE (incr) == MODIFY_EXPR)
20048 {
20049 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
20050 return true;
20051 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
20052 {
20053 tree t = TREE_OPERAND (incr, 1);
20054 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
20055 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
20056 return true;
20057 }
20058 }
20059 }
20060
20061 return false;
20062 }
20063
20064 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20065 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20066 no such TYPE can be found. Note that this function peers inside
20067 uninstantiated templates and therefore should be used only in
20068 extremely limited situations. ONLY_CURRENT_P restricts this
20069 peering to the currently open classes hierarchy (which is required
20070 when comparing types). */
20071
20072 tree
20073 resolve_typename_type (tree type, bool only_current_p)
20074 {
20075 tree scope;
20076 tree name;
20077 tree decl;
20078 int quals;
20079 tree pushed_scope;
20080 tree result;
20081
20082 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
20083
20084 scope = TYPE_CONTEXT (type);
20085 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20086 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20087 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20088 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20089 identifier of the TYPENAME_TYPE anymore.
20090 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20091 TYPENAME_TYPE instead, we avoid messing up with a possible
20092 typedef variant case. */
20093 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
20094
20095 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20096 it first before we can figure out what NAME refers to. */
20097 if (TREE_CODE (scope) == TYPENAME_TYPE)
20098 {
20099 if (TYPENAME_IS_RESOLVING_P (scope))
20100 /* Given a class template A with a dependent base with nested type C,
20101 typedef typename A::C::C C will land us here, as trying to resolve
20102 the initial A::C leads to the local C typedef, which leads back to
20103 A::C::C. So we break the recursion now. */
20104 return type;
20105 else
20106 scope = resolve_typename_type (scope, only_current_p);
20107 }
20108 /* If we don't know what SCOPE refers to, then we cannot resolve the
20109 TYPENAME_TYPE. */
20110 if (TREE_CODE (scope) == TYPENAME_TYPE)
20111 return type;
20112 /* If the SCOPE is a template type parameter, we have no way of
20113 resolving the name. */
20114 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
20115 return type;
20116 /* If the SCOPE is not the current instantiation, there's no reason
20117 to look inside it. */
20118 if (only_current_p && !currently_open_class (scope))
20119 return type;
20120 /* If this is a typedef, we don't want to look inside (c++/11987). */
20121 if (typedef_variant_p (type))
20122 return type;
20123 /* If SCOPE isn't the template itself, it will not have a valid
20124 TYPE_FIELDS list. */
20125 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
20126 /* scope is either the template itself or a compatible instantiation
20127 like X<T>, so look up the name in the original template. */
20128 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
20129 else
20130 /* scope is a partial instantiation, so we can't do the lookup or we
20131 will lose the template arguments. */
20132 return type;
20133 /* Enter the SCOPE so that name lookup will be resolved as if we
20134 were in the class definition. In particular, SCOPE will no
20135 longer be considered a dependent type. */
20136 pushed_scope = push_scope (scope);
20137 /* Look up the declaration. */
20138 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
20139 tf_warning_or_error);
20140
20141 result = NULL_TREE;
20142
20143 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20144 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20145 if (!decl)
20146 /*nop*/;
20147 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
20148 && TREE_CODE (decl) == TYPE_DECL)
20149 {
20150 result = TREE_TYPE (decl);
20151 if (result == error_mark_node)
20152 result = NULL_TREE;
20153 }
20154 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
20155 && DECL_CLASS_TEMPLATE_P (decl))
20156 {
20157 tree tmpl;
20158 tree args;
20159 /* Obtain the template and the arguments. */
20160 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
20161 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
20162 /* Instantiate the template. */
20163 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
20164 /*entering_scope=*/0,
20165 tf_error | tf_user);
20166 if (result == error_mark_node)
20167 result = NULL_TREE;
20168 }
20169
20170 /* Leave the SCOPE. */
20171 if (pushed_scope)
20172 pop_scope (pushed_scope);
20173
20174 /* If we failed to resolve it, return the original typename. */
20175 if (!result)
20176 return type;
20177
20178 /* If lookup found a typename type, resolve that too. */
20179 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
20180 {
20181 /* Ill-formed programs can cause infinite recursion here, so we
20182 must catch that. */
20183 TYPENAME_IS_RESOLVING_P (type) = 1;
20184 result = resolve_typename_type (result, only_current_p);
20185 TYPENAME_IS_RESOLVING_P (type) = 0;
20186 }
20187
20188 /* Qualify the resulting type. */
20189 quals = cp_type_quals (type);
20190 if (quals)
20191 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
20192
20193 return result;
20194 }
20195
20196 /* EXPR is an expression which is not type-dependent. Return a proxy
20197 for EXPR that can be used to compute the types of larger
20198 expressions containing EXPR. */
20199
20200 tree
20201 build_non_dependent_expr (tree expr)
20202 {
20203 tree inner_expr;
20204
20205 #ifdef ENABLE_CHECKING
20206 /* Try to get a constant value for all non-type-dependent expressions in
20207 order to expose bugs in *_dependent_expression_p and constexpr. */
20208 if (cxx_dialect >= cxx0x)
20209 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
20210 #endif
20211
20212 /* Preserve OVERLOADs; the functions must be available to resolve
20213 types. */
20214 inner_expr = expr;
20215 if (TREE_CODE (inner_expr) == STMT_EXPR)
20216 inner_expr = stmt_expr_value_expr (inner_expr);
20217 if (TREE_CODE (inner_expr) == ADDR_EXPR)
20218 inner_expr = TREE_OPERAND (inner_expr, 0);
20219 if (TREE_CODE (inner_expr) == COMPONENT_REF)
20220 inner_expr = TREE_OPERAND (inner_expr, 1);
20221 if (is_overloaded_fn (inner_expr)
20222 || TREE_CODE (inner_expr) == OFFSET_REF)
20223 return expr;
20224 /* There is no need to return a proxy for a variable. */
20225 if (TREE_CODE (expr) == VAR_DECL)
20226 return expr;
20227 /* Preserve string constants; conversions from string constants to
20228 "char *" are allowed, even though normally a "const char *"
20229 cannot be used to initialize a "char *". */
20230 if (TREE_CODE (expr) == STRING_CST)
20231 return expr;
20232 /* Preserve arithmetic constants, as an optimization -- there is no
20233 reason to create a new node. */
20234 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
20235 return expr;
20236 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20237 There is at least one place where we want to know that a
20238 particular expression is a throw-expression: when checking a ?:
20239 expression, there are special rules if the second or third
20240 argument is a throw-expression. */
20241 if (TREE_CODE (expr) == THROW_EXPR)
20242 return expr;
20243
20244 /* Don't wrap an initializer list, we need to be able to look inside. */
20245 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
20246 return expr;
20247
20248 /* Don't wrap a dummy object, we need to be able to test for it. */
20249 if (is_dummy_object (expr))
20250 return expr;
20251
20252 if (TREE_CODE (expr) == COND_EXPR)
20253 return build3 (COND_EXPR,
20254 TREE_TYPE (expr),
20255 TREE_OPERAND (expr, 0),
20256 (TREE_OPERAND (expr, 1)
20257 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
20258 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
20259 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
20260 if (TREE_CODE (expr) == COMPOUND_EXPR
20261 && !COMPOUND_EXPR_OVERLOADED (expr))
20262 return build2 (COMPOUND_EXPR,
20263 TREE_TYPE (expr),
20264 TREE_OPERAND (expr, 0),
20265 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
20266
20267 /* If the type is unknown, it can't really be non-dependent */
20268 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
20269
20270 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20271 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
20272 }
20273
20274 /* ARGS is a vector of expressions as arguments to a function call.
20275 Replace the arguments with equivalent non-dependent expressions.
20276 This modifies ARGS in place. */
20277
20278 void
20279 make_args_non_dependent (vec<tree, va_gc> *args)
20280 {
20281 unsigned int ix;
20282 tree arg;
20283
20284 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
20285 {
20286 tree newarg = build_non_dependent_expr (arg);
20287 if (newarg != arg)
20288 (*args)[ix] = newarg;
20289 }
20290 }
20291
20292 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20293 with a level one deeper than the actual template parms. */
20294
20295 tree
20296 make_auto (void)
20297 {
20298 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
20299 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
20300 TYPE_DECL, get_identifier ("auto"), au);
20301 TYPE_STUB_DECL (au) = TYPE_NAME (au);
20302 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
20303 (0, processing_template_decl + 1, processing_template_decl + 1,
20304 TYPE_NAME (au), NULL_TREE);
20305 TYPE_CANONICAL (au) = canonical_type_parameter (au);
20306 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
20307 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
20308
20309 return au;
20310 }
20311
20312 /* Given type ARG, return std::initializer_list<ARG>. */
20313
20314 static tree
20315 listify (tree arg)
20316 {
20317 tree std_init_list = namespace_binding
20318 (get_identifier ("initializer_list"), std_node);
20319 tree argvec;
20320 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
20321 {
20322 error ("deducing from brace-enclosed initializer list requires "
20323 "#include <initializer_list>");
20324 return error_mark_node;
20325 }
20326 argvec = make_tree_vec (1);
20327 TREE_VEC_ELT (argvec, 0) = arg;
20328 return lookup_template_class (std_init_list, argvec, NULL_TREE,
20329 NULL_TREE, 0, tf_warning_or_error);
20330 }
20331
20332 /* Replace auto in TYPE with std::initializer_list<auto>. */
20333
20334 static tree
20335 listify_autos (tree type, tree auto_node)
20336 {
20337 tree init_auto = listify (auto_node);
20338 tree argvec = make_tree_vec (1);
20339 TREE_VEC_ELT (argvec, 0) = init_auto;
20340 if (processing_template_decl)
20341 argvec = add_to_template_args (current_template_args (), argvec);
20342 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20343 }
20344
20345 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20346 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20347
20348 tree
20349 do_auto_deduction (tree type, tree init, tree auto_node)
20350 {
20351 tree parms, tparms, targs;
20352 tree args[1];
20353 int val;
20354
20355 if (init == error_mark_node)
20356 return error_mark_node;
20357
20358 if (type_dependent_expression_p (init))
20359 /* Defining a subset of type-dependent expressions that we can deduce
20360 from ahead of time isn't worth the trouble. */
20361 return type;
20362
20363 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20364 with either a new invented type template parameter U or, if the
20365 initializer is a braced-init-list (8.5.4), with
20366 std::initializer_list<U>. */
20367 if (BRACE_ENCLOSED_INITIALIZER_P (init))
20368 type = listify_autos (type, auto_node);
20369
20370 init = resolve_nondeduced_context (init);
20371
20372 parms = build_tree_list (NULL_TREE, type);
20373 args[0] = init;
20374 tparms = make_tree_vec (1);
20375 targs = make_tree_vec (1);
20376 TREE_VEC_ELT (tparms, 0)
20377 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
20378 val = type_unification_real (tparms, targs, parms, args, 1, 0,
20379 DEDUCE_CALL, LOOKUP_NORMAL,
20380 /*explain_p=*/false);
20381 if (val > 0)
20382 {
20383 if (processing_template_decl)
20384 /* Try again at instantiation time. */
20385 return type;
20386 if (type && type != error_mark_node)
20387 /* If type is error_mark_node a diagnostic must have been
20388 emitted by now. Also, having a mention to '<type error>'
20389 in the diagnostic is not really useful to the user. */
20390 {
20391 if (cfun && auto_node == current_function_auto_return_pattern
20392 && LAMBDA_FUNCTION_P (current_function_decl))
20393 error ("unable to deduce lambda return type from %qE", init);
20394 else
20395 error ("unable to deduce %qT from %qE", type, init);
20396 }
20397 return error_mark_node;
20398 }
20399
20400 /* If the list of declarators contains more than one declarator, the type
20401 of each declared variable is determined as described above. If the
20402 type deduced for the template parameter U is not the same in each
20403 deduction, the program is ill-formed. */
20404 if (TREE_TYPE (auto_node)
20405 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
20406 {
20407 if (cfun && auto_node == current_function_auto_return_pattern
20408 && LAMBDA_FUNCTION_P (current_function_decl))
20409 error ("inconsistent types %qT and %qT deduced for "
20410 "lambda return type", TREE_TYPE (auto_node),
20411 TREE_VEC_ELT (targs, 0));
20412 else
20413 error ("inconsistent deduction for %qT: %qT and then %qT",
20414 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
20415 return error_mark_node;
20416 }
20417 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
20418
20419 if (processing_template_decl)
20420 targs = add_to_template_args (current_template_args (), targs);
20421 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
20422 }
20423
20424 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20425 result. */
20426
20427 tree
20428 splice_late_return_type (tree type, tree late_return_type)
20429 {
20430 tree argvec;
20431
20432 if (late_return_type == NULL_TREE)
20433 return type;
20434 argvec = make_tree_vec (1);
20435 TREE_VEC_ELT (argvec, 0) = late_return_type;
20436 if (processing_template_parmlist)
20437 /* For a late-specified return type in a template type-parameter, we
20438 need to add a dummy argument level for its parmlist. */
20439 argvec = add_to_template_args
20440 (make_tree_vec (processing_template_parmlist), argvec);
20441 if (current_template_parms)
20442 argvec = add_to_template_args (current_template_args (), argvec);
20443 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20444 }
20445
20446 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20447
20448 bool
20449 is_auto (const_tree type)
20450 {
20451 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20452 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
20453 return true;
20454 else
20455 return false;
20456 }
20457
20458 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20459 appear as a type-specifier for the declaration in question, we don't
20460 have to look through the whole type. */
20461
20462 tree
20463 type_uses_auto (tree type)
20464 {
20465 enum tree_code code;
20466 if (is_auto (type))
20467 return type;
20468
20469 code = TREE_CODE (type);
20470
20471 if (code == POINTER_TYPE || code == REFERENCE_TYPE
20472 || code == OFFSET_TYPE || code == FUNCTION_TYPE
20473 || code == METHOD_TYPE || code == ARRAY_TYPE)
20474 return type_uses_auto (TREE_TYPE (type));
20475
20476 if (TYPE_PTRMEMFUNC_P (type))
20477 return type_uses_auto (TREE_TYPE (TREE_TYPE
20478 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
20479
20480 return NULL_TREE;
20481 }
20482
20483 /* For a given template T, return the vector of typedefs referenced
20484 in T for which access check is needed at T instantiation time.
20485 T is either a FUNCTION_DECL or a RECORD_TYPE.
20486 Those typedefs were added to T by the function
20487 append_type_to_template_for_access_check. */
20488
20489 vec<qualified_typedef_usage_t, va_gc> *
20490 get_types_needing_access_check (tree t)
20491 {
20492 tree ti;
20493 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
20494
20495 if (!t || t == error_mark_node)
20496 return NULL;
20497
20498 if (!(ti = get_template_info (t)))
20499 return NULL;
20500
20501 if (CLASS_TYPE_P (t)
20502 || TREE_CODE (t) == FUNCTION_DECL)
20503 {
20504 if (!TI_TEMPLATE (ti))
20505 return NULL;
20506
20507 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
20508 }
20509
20510 return result;
20511 }
20512
20513 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20514 tied to T. That list of typedefs will be access checked at
20515 T instantiation time.
20516 T is either a FUNCTION_DECL or a RECORD_TYPE.
20517 TYPE_DECL is a TYPE_DECL node representing a typedef.
20518 SCOPE is the scope through which TYPE_DECL is accessed.
20519 LOCATION is the location of the usage point of TYPE_DECL.
20520
20521 This function is a subroutine of
20522 append_type_to_template_for_access_check. */
20523
20524 static void
20525 append_type_to_template_for_access_check_1 (tree t,
20526 tree type_decl,
20527 tree scope,
20528 location_t location)
20529 {
20530 qualified_typedef_usage_t typedef_usage;
20531 tree ti;
20532
20533 if (!t || t == error_mark_node)
20534 return;
20535
20536 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
20537 || CLASS_TYPE_P (t))
20538 && type_decl
20539 && TREE_CODE (type_decl) == TYPE_DECL
20540 && scope);
20541
20542 if (!(ti = get_template_info (t)))
20543 return;
20544
20545 gcc_assert (TI_TEMPLATE (ti));
20546
20547 typedef_usage.typedef_decl = type_decl;
20548 typedef_usage.context = scope;
20549 typedef_usage.locus = location;
20550
20551 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
20552 }
20553
20554 /* Append TYPE_DECL to the template TEMPL.
20555 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20556 At TEMPL instanciation time, TYPE_DECL will be checked to see
20557 if it can be accessed through SCOPE.
20558 LOCATION is the location of the usage point of TYPE_DECL.
20559
20560 e.g. consider the following code snippet:
20561
20562 class C
20563 {
20564 typedef int myint;
20565 };
20566
20567 template<class U> struct S
20568 {
20569 C::myint mi; // <-- usage point of the typedef C::myint
20570 };
20571
20572 S<char> s;
20573
20574 At S<char> instantiation time, we need to check the access of C::myint
20575 In other words, we need to check the access of the myint typedef through
20576 the C scope. For that purpose, this function will add the myint typedef
20577 and the scope C through which its being accessed to a list of typedefs
20578 tied to the template S. That list will be walked at template instantiation
20579 time and access check performed on each typedefs it contains.
20580 Note that this particular code snippet should yield an error because
20581 myint is private to C. */
20582
20583 void
20584 append_type_to_template_for_access_check (tree templ,
20585 tree type_decl,
20586 tree scope,
20587 location_t location)
20588 {
20589 qualified_typedef_usage_t *iter;
20590 unsigned i;
20591
20592 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
20593
20594 /* Make sure we don't append the type to the template twice. */
20595 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
20596 if (iter->typedef_decl == type_decl && scope == iter->context)
20597 return;
20598
20599 append_type_to_template_for_access_check_1 (templ, type_decl,
20600 scope, location);
20601 }
20602
20603 /* Set up the hash tables for template instantiations. */
20604
20605 void
20606 init_template_processing (void)
20607 {
20608 decl_specializations = htab_create_ggc (37,
20609 hash_specialization,
20610 eq_specializations,
20611 ggc_free);
20612 type_specializations = htab_create_ggc (37,
20613 hash_specialization,
20614 eq_specializations,
20615 ggc_free);
20616 }
20617
20618 /* Print stats about the template hash tables for -fstats. */
20619
20620 void
20621 print_template_statistics (void)
20622 {
20623 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
20624 "%f collisions\n", (long) htab_size (decl_specializations),
20625 (long) htab_elements (decl_specializations),
20626 htab_collisions (decl_specializations));
20627 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
20628 "%f collisions\n", (long) htab_size (type_specializations),
20629 (long) htab_elements (type_specializations),
20630 htab_collisions (type_specializations));
20631 }
20632
20633 #include "gt-cp-pt.h"